如何确定.data和.bss的对齐方式

Ami*_*nha 6 c memory-management

.data和.bss的对齐有时是4个字节,有时是32个字节.示例1:根据下面输出的最后一列,bss和数据的对齐是32个字节

bash-3.00$ readelf --sections libmodel.so
There are 39 section headers, starting at offset 0x1908a63c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
...
  [25] .data             PROGBITS        01e221e0 1e211e0 26ca54 00  WA  0   0 32
  [26] .bss              NOBITS          0208ec40 208dc34 374178 00  WA  0   0 32
...
Run Code Online (Sandbox Code Playgroud)

示例2:根据以下输出,对齐os .data和.bss是4个字节

bash-3.00$ readelf --sections ./a.out
There are 28 section headers, starting at offset 0x78c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
...
  [22] .data             PROGBITS        0804956c 00056c 000034 00  WA  0   0  4
  [23] .bss              NOBITS          080495a0 0005a0 000004 00  WA  0   0  4
...
Run Code Online (Sandbox Code Playgroud)

是什么决定了.bss和.data的对齐方式?为什么有时是4个字节,有时是32个字节?

小智 0

我认为这些不同的对齐值是由各自的ld脚本决定的。

  • 为了libmodel.sosection .data align=16, section .bss align=16
  • 为了a.outsection .data align=4, section .bss align=4