6
struct abc
{
   char cc[32];
} mystruct;

int  main()
{
}

当我运行上述程序时,.bss 部分有 64 个字节。我期待它是 36 字节。mystruct 32 个字节,其他库占用 4 个字节。

如果我更改char cc[32]为,char cc[31]那么我在 .bss 中得到 36 个字节。

-bash-3.00$ g++ bssSize.cc

-bash-3.00$ readelf --sections ./a.out | grep bss
  [23] .bss              NOBITS          08049580 000578 000040 00  WA  0   0 32
-bash-3.00$ 

64 是十六进制的 000040

为什么 .bss 中有这些额外的 28 个字节?

4

3 回答 3

4

输出的最后一列readelf是对齐,显示的值确实是“32”。因此,BSS 大小被四舍五入到最接近的 32 字节的倍数。

于 2013-08-09T17:41:58.120 回答
1

objdump -sSx a.out 产量,除其他外......

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .interp       0000001c  0000000000400238  0000000000400238  00000238  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .note.ABI-tag 00000020  0000000000400254  0000000000400254  00000254  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .note.gnu.build-id 00000024  0000000000400274  0000000000400274  00000274  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .gnu.hash     0000001c  0000000000400298  0000000000400298  00000298  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .dynsym       00000048  00000000004002b8  00000000004002b8  000002b8  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .dynstr       00000038  0000000000400300  0000000000400300  00000300  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .gnu.version  00000006  0000000000400338  0000000000400338  00000338  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .gnu.version_r 00000020  0000000000400340  0000000000400340  00000340  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  8 .rela.dyn     00000018  0000000000400360  0000000000400360  00000360  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  9 .rela.plt     00000018  0000000000400378  0000000000400378  00000378  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 10 .init         00000018  0000000000400390  0000000000400390  00000390  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 11 .plt          00000020  00000000004003b0  00000000004003b0  000003b0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .text         000001c8  00000000004003d0  00000000004003d0  000003d0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 13 .fini         0000000e  0000000000400598  0000000000400598  00000598  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 14 .rodata       00000004  00000000004005a8  00000000004005a8  000005a8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 15 .eh_frame_hdr 0000002c  00000000004005ac  00000000004005ac  000005ac  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 16 .eh_frame     000000a4  00000000004005d8  00000000004005d8  000005d8  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 17 .ctors        00000010  0000000000600e28  0000000000600e28  00000e28  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 18 .dtors        00000010  0000000000600e38  0000000000600e38  00000e38  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 19 .jcr          00000008  0000000000600e48  0000000000600e48  00000e48  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 20 .dynamic      00000190  0000000000600e50  0000000000600e50  00000e50  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 21 .got          00000008  0000000000600fe0  0000000000600fe0  00000fe0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 22 .got.plt      00000020  0000000000600fe8  0000000000600fe8  00000fe8  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 23 .data         00000010  0000000000601008  0000000000601008  00001008  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 24 .bss          00000040  0000000000601020  0000000000601020  00001018  2**5
                  ALLOC
 25 .comment      0000002a  0000000000000000  0000000000000000  00001018  2**0
                  CONTENTS, READONLY

第 24 项,在“Algn”下,我们看到 2**5 或 2^5 aka 32。这表明 bss 是按 32 字节块分配的,因为它是 32 位对齐的。

于 2013-08-09T18:00:26.920 回答
0

objdump -t a.out |grep bss 给你

    08049660 l    d  .bss   00000000              .bss
    08049660 l     O .bss   00000001              completed.5745
    08049660 g       *ABS*  00000000              __bss_start
    08049680 g     O .bss   00000020              mystruct

大小为 32 字节的预期(十六进制的 00000020)。

于 2013-08-09T19:25:45.220 回答