链接器如何计算.bss部分的大小?

时间:2012-07-12 07:52:34

标签: c gcc ld gas

我想知道链接器如何计算.bss部分的大小?

我有一个带有两个变量的测试程序,一个是初始化为零,另一个是 初始化为非零。我希望.bss的大小是4,它是。

# cat test.c && gcc -o test.o -c test.c && ld -o test test.o && readelf -Ss test
int _start=0;
int a = 2;
......
Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 1] .data             PROGBITS         00000000006000b0  000000b0
       0000000000000004  0000000000000000  WA       0     0     4
  [ 2] .bss              NOBITS           00000000006000b4  000000b4
       0000000000000004  0000000000000000  WA       0     0     4
.....
Symbol table '.symtab' contains 11 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     6: 00000000006000b4     4 OBJECT  GLOBAL DEFAULT    2 _start
    10: 00000000006000b0     4 OBJECT  GLOBAL DEFAULT    1 a

然后我添加第二个零初始化变量。这次我希望.bss的大小是 是8,但它是12(0xc)。

# cat test.c && gcc -o test.o -c test.c && ld -o test test.o && readelf -Ss test
int _start=0;
int a = 2;
int b = 0;
......
Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 1] .data             PROGBITS         00000000006000b0  000000b0
       0000000000000004  0000000000000000  WA       0     0     4
  [ 2] .bss              NOBITS           00000000006000b4  000000b4
       000000000000000c  0000000000000000  WA       0     0     4
......
Symbol table '.symtab' contains 12 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     6: 00000000006000b8     4 OBJECT  GLOBAL DEFAULT    2 b
     7: 00000000006000b4     4 OBJECT  GLOBAL DEFAULT    2 _start
    11: 00000000006000b0     4 OBJECT  GLOBAL DEFAULT    1 a

我继续添加第三个零初始化变量。这次我希望.bss的大小是 是12,它是。

# cat test.c && gcc -o test.o -c test.c && ld -o test test.o && readelf -Ss test
int _start=0;
int a = 2;
int b = 0;
int c = 0;
......
Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .data             PROGBITS         00000000006000b0  000000b0
       0000000000000004  0000000000000000  WA       0     0     4
  [ 2] .bss              NOBITS           00000000006000b4  000000b4
       000000000000000c  0000000000000000  WA       0     0     4
......
Symbol table '.symtab' contains 13 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     6: 00000000006000b8     4 OBJECT  GLOBAL DEFAULT    2 b
     7: 00000000006000b4     4 OBJECT  GLOBAL DEFAULT    2 _start
     8: 00000000006000bc     4 OBJECT  GLOBAL DEFAULT    2 c
    12: 00000000006000b0     4 OBJECT  GLOBAL DEFAULT    1 a

我继续添加第四个零初始化变量。这次我希望.bss的大小是 是16,但它是20(0x14)。

# cat test.c && gcc -o test.o -c test.c && ld -o test test.o && readelf -Ss test
int _start=0;
int a = 2;
int b = 0;
int c = 0;
int d = 0;
......
Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 1] .data             PROGBITS         00000000006000b0  000000b0
       0000000000000004  0000000000000000  WA       0     0     4
  [ 2] .bss              NOBITS           00000000006000b4  000000b4
       0000000000000014  0000000000000000  WA       0     0     4
......
Symbol table '.symtab' contains 14 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     6: 00000000006000b8     4 OBJECT  GLOBAL DEFAULT    2 b
     7: 00000000006000b4     4 OBJECT  GLOBAL DEFAULT    2 _start
     8: 00000000006000bc     4 OBJECT  GLOBAL DEFAULT    2 c
    10: 00000000006000c0     4 OBJECT  GLOBAL DEFAULT    2 d
    13: 00000000006000b0     4 OBJECT  GLOBAL DEFAULT    1 a

有时结果符合我的期望,有时则没有。 是否还有其他因素会影响链接器的输出?

1 个答案:

答案 0 :(得分:1)

事实上它是对齐的。示例中bss的大小与您期望的不是8字节对齐,但这是因为您有4个字节的data。如果您添加另一个初始化变量,您会看到bss段的大小为8字节对齐。