2012年2月17日 星期五

bss section (0)

the 1st edition: 20120217 (5)
the 2nd edition: 20161215 (4)

bss.c
1 int i;
2 
3 int main(void)
4 {
5   i = 1;
6   return 0;
7 }

gcc -g -o bss bss.c

list 0 L128 ~ 130
128   i = 1;
129  80483d7: c7 05 18 a0 04 08 01  movl   $0x1,0x804a018
130  80483de: 00 00 00

i 變數會放在 bss section, 而在 linux 下, 這個 bss 位址為 0804a018, 所以 int = 1 就是把 1 寫 4 byte (00 00 00 01) 到 0804a018 這裡就對了。

list 0. objdump -dS bss
  1
  2 bss:     file format elf32-i386
  3
  4
  5 Disassembly of section .init:
  6

121 080483d4 <main>:
122 int i;
123
124 int main(void)
125 {
126  80483d4: 55                    push   %ebp
127  80483d5: 89 e5                 mov    %esp,%ebp
128   i = 1;
129  80483d7: c7 05 18 a0 04 08 01  movl   $0x1,0x804a018
130  80483de: 00 00 00
131   return 0;
132  80483e1: b8 00 00 00 00        mov    $0x0,%eax
133 }

list 1. L50, L208

 50  [26] .bss   NOBITS  0804a010 001010 00000c 00  WA  0   0  4
208  69: 0804a018  4 OBJECT  GLOBAL DEFAULT   26 i

bss section 始於 0804a010 長度為 c, L 208 指出變數 i 位址為 0804a018, 和上述的組合語言程式碼相呼應。

list 1. readelf -a bss
  1 ELF Header:
  2   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  3   Class:                             ELF32
  4   Data:                              2's complement, little endian
  5   Version:                           1 (current)
  6   OS/ABI:                            UNIX - System V
  7   ABI Version:                       0
  8   Type:                              EXEC (Executable file)
  9   Machine:                           Intel 80386
 10   Version:                           0x1
 11   Entry point address:               0x8048320
 12   Start of program headers:          52 (bytes into file)
 13   Start of section headers:          4932 (bytes into file)
 14   Flags:                             0x0
 15   Size of this header:               52 (bytes)
 16   Size of program headers:           32 (bytes)
 17   Number of program headers:         9
 18   Size of section headers:           40 (bytes)
 19   Number of section headers:         39
 20   Section header string table index: 36
 21
 22 Section Headers:
 23   [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
 24   [ 0]                   NULL            00000000 000000 000000 00      0   0  0
 25   [ 1] .interp           PROGBITS        08048154 000154 000013 00   A  0   0  1
 26   [ 2] .note.ABI-tag     NOTE            08048168 000168 000020 00   A  0   0  4
 27   [ 3] .note.gnu.build-i NOTE            08048188 000188 000024 00   A  0   0  4
 28   [ 4] .hash             HASH            080481ac 0001ac 000024 04   A  6   0  4
 29   [ 5] .gnu.hash         GNU_HASH        080481d0 0001d0 000020 04   A  6   0  4
 30   [ 6] .dynsym           DYNSYM          080481f0 0001f0 000040 10   A  7   1  4
 31   [ 7] .dynstr           STRTAB          08048230 000230 000045 00   A  0   0  1
 32   [ 8] .gnu.version      VERSYM          08048276 000276 000008 02   A  6   0  2
 33   [ 9] .gnu.version_r    VERNEED         08048280 000280 000020 00   A  7   1  4
 34   [10] .rel.dyn          REL             080482a0 0002a0 000008 08   A  6   0  4
 35   [11] .rel.plt          REL             080482a8 0002a8 000010 08   A  6  13  4
 36   [12] .init             PROGBITS        080482b8 0002b8 000030 00  AX  0   0  4
 37   [13] .plt              PROGBITS        080482f0 0002f0 000030 04  AX  0   0 16
 38   [14] .text             PROGBITS        08048320 000320 00016c 00  AX  0   0 16
 39   [15] .fini             PROGBITS        0804848c 00048c 00001c 00  AX  0   0  4
 40   [16] .rodata           PROGBITS        080484a8 0004a8 000008 00   A  0   0  4
 41   [17] .eh_frame_hdr     PROGBITS        080484b0 0004b0 000014 00   A  0   0  4
 42   [18] .eh_frame         PROGBITS        080484c4 0004c4 000040 00   A  0   0  4
 43   [19] .ctors            PROGBITS        08049f0c 000f0c 000008 00  WA  0   0  4
 44   [20] .dtors            PROGBITS        08049f14 000f14 000008 00  WA  0   0  4
 45   [21] .jcr              PROGBITS        08049f1c 000f1c 000004 00  WA  0   0  4
 46   [22] .dynamic          DYNAMIC         08049f20 000f20 0000d0 08  WA  7   0  4
 47   [23] .got              PROGBITS        08049ff0 000ff0 000004 04  WA  0   0  4
 48   [24] .got.plt          PROGBITS        08049ff4 000ff4 000014 04  WA  0   0  4
 49   [25] .data             PROGBITS        0804a008 001008 000008 00  WA  0   0  4
 50   [26] .bss              NOBITS          0804a010 001010 00000c 00  WA  0   0  4
 51   [27] .comment          PROGBITS        00000000 001010 000023 01  MS  0   0  1
 52   [28] .debug_aranges    PROGBITS        00000000 001033 000020 00      0   0  1
 53   [29] .debug_pubnames   PROGBITS        00000000 001053 000021 00      0   0  1
 54   [30] .debug_info       PROGBITS        00000000 001074 000056 00      0   0  1
 55   [31] .debug_abbrev     PROGBITS        00000000 0010ca 000047 00      0   0  1
 56   [32] .debug_line       PROGBITS        00000000 001111 000036 00      0   0  1
 57   [33] .debug_frame      PROGBITS        00000000 001148 00002c 00      0   0  4
 58   [34] .debug_str        PROGBITS        00000000 001174 00003e 01  MS  0   0  1
 59   [35] .debug_loc        PROGBITS        00000000 0011b2 00002c 00      0   0  1
 60   [36] .shstrtab         STRTAB          00000000 0011de 000164 00      0   0  1
 61   [37] .symtab           SYMTAB          00000000 00195c 0004a0 10     38  54  4
 62   [38] .strtab           STRTAB          00000000 001dfc 0001eb 00      0   0  1
 63 Key to Flags:
 64   W (write), A (alloc), X (execute), M (merge), S (strings)
 65   I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
 66   O (extra OS processing required) o (OS specific), p (processor specific)
 67
137 Symbol table '.symtab' contains 74 entries:
138    Num:    Value  Size Type    Bind   Vis      Ndx Name
139      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
140      1: 08048154     0 SECTION LOCAL  DEFAULT    1
141      2: 08048168     0 SECTION LOCAL  DEFAULT    2
142      3: 08048188     0 SECTION LOCAL  DEFAULT    3
143      4: 080481ac     0 SECTION LOCAL  DEFAULT    4
144      5: 080481d0     0 SECTION LOCAL  DEFAULT    5
145      6: 080481f0     0 SECTION LOCAL  DEFAULT    6
146      7: 08048230     0 SECTION LOCAL  DEFAULT    7
147      8: 08048276     0 SECTION LOCAL  DEFAULT    8
148      9: 08048280     0 SECTION LOCAL  DEFAULT    9
149     10: 080482a0     0 SECTION LOCAL  DEFAULT   10
150     11: 080482a8     0 SECTION LOCAL  DEFAULT   11
151     12: 080482b8     0 SECTION LOCAL  DEFAULT   12
152     13: 080482f0     0 SECTION LOCAL  DEFAULT   13
153     14: 08048320     0 SECTION LOCAL  DEFAULT   14
154     15: 0804848c     0 SECTION LOCAL  DEFAULT   15
155     16: 080484a8     0 SECTION LOCAL  DEFAULT   16
156     17: 080484b0     0 SECTION LOCAL  DEFAULT   17
157     18: 080484c4     0 SECTION LOCAL  DEFAULT   18
158     19: 08049f0c     0 SECTION LOCAL  DEFAULT   19
159     20: 08049f14     0 SECTION LOCAL  DEFAULT   20
160     21: 08049f1c     0 SECTION LOCAL  DEFAULT   21
161     22: 08049f20     0 SECTION LOCAL  DEFAULT   22
162     23: 08049ff0     0 SECTION LOCAL  DEFAULT   23
163     24: 08049ff4     0 SECTION LOCAL  DEFAULT   24
164     25: 0804a008     0 SECTION LOCAL  DEFAULT   25
165     26: 0804a010     0 SECTION LOCAL  DEFAULT   26
166     27: 00000000     0 SECTION LOCAL  DEFAULT   27
167     28: 00000000     0 SECTION LOCAL  DEFAULT   28
168     29: 00000000     0 SECTION LOCAL  DEFAULT   29
169     30: 00000000     0 SECTION LOCAL  DEFAULT   30
170     31: 00000000     0 SECTION LOCAL  DEFAULT   31
171     32: 00000000     0 SECTION LOCAL  DEFAULT   32
172     33: 00000000     0 SECTION LOCAL  DEFAULT   33
173     34: 00000000     0 SECTION LOCAL  DEFAULT   34
174     35: 00000000     0 SECTION LOCAL  DEFAULT   35
175     36: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
176     37: 08049f0c     0 OBJECT  LOCAL  DEFAULT   19 __CTOR_LIST__
177     38: 08049f14     0 OBJECT  LOCAL  DEFAULT   20 __DTOR_LIST__
178     39: 08049f1c     0 OBJECT  LOCAL  DEFAULT   21 __JCR_LIST__
179     40: 08048350     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux
180     41: 0804a010     1 OBJECT  LOCAL  DEFAULT   26 completed.7021
181     42: 0804a014     4 OBJECT  LOCAL  DEFAULT   26 dtor_idx.7023
182     43: 080483b0     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
183     44: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
184     45: 08049f10     0 OBJECT  LOCAL  DEFAULT   19 __CTOR_END__
185     46: 08048500     0 OBJECT  LOCAL  DEFAULT   18 __FRAME_END__
186     47: 08049f1c     0 OBJECT  LOCAL  DEFAULT   21 __JCR_END__
187     48: 08048460     0 FUNC    LOCAL  DEFAULT   14 __do_global_ctors_aux
188     49: 00000000     0 FILE    LOCAL  DEFAULT  ABS bss.c
189     50: 08049f0c     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_end
190     51: 08049f20     0 OBJECT  LOCAL  DEFAULT   22 _DYNAMIC
191     52: 08049f0c     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start
192     53: 08049ff4     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_
193     54: 080483f0     5 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
194     55: 0804845a     0 FUNC    GLOBAL HIDDEN    14 __i686.get_pc_thunk.bx
195     56: 0804a008     0 NOTYPE  WEAK   DEFAULT   25 data_start
196     57: 0804a010     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
197     58: 0804848c     0 FUNC    GLOBAL DEFAULT   15 _fini
198     59: 08049f18     0 OBJECT  GLOBAL HIDDEN    20 __DTOR_END__
199     60: 0804a008     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
200     61: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
201     62: 0804a00c     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle
202     63: 080484ac     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
203     64: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
204     65: 08048400    90 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
205     66: 0804a01c     0 NOTYPE  GLOBAL DEFAULT  ABS _end
206     67: 08048320     0 FUNC    GLOBAL DEFAULT   14 _start
207     68: 080484a8     4 OBJECT  GLOBAL DEFAULT   16 _fp_hw
208     69: 0804a018     4 OBJECT  GLOBAL DEFAULT   26 i
209     70: 0804a010     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
210     71: 080483d4    20 FUNC    GLOBAL DEFAULT   14 main
211     72: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
212     73: 080482b8     0 FUNC    GLOBAL DEFAULT   12 _init
213
214 Histogram for bucket list length (total of 3 buckets):
215  Length  Number     % of total  Coverage
216       0  0          (  0.0%)
217       1  3          (100.0%)    100.0%
218

那 bss 區域怎麼來呢? 在作業系統裡頭有作業系統幫忙處理, 若程式不是由作業系統載入, 而是全部都要自己來要怎麼辦? 由誰來初始化呢? 若是沒有 bss 區域, 用了放在 bss 裡頭的變數, 那會怎樣?

我還在研究中 ...
bss section (1)》有了答案。

ref: linux c 編程 - 一站式學習 (p 237)

沒有留言:

張貼留言

使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。

我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。