bss中有28个额外字节

时间:2013-08-09 17:12:12

标签: c memory-management

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(十六进制)

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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又称32.这表示bss以32字节块的形式分配,因为它是32位对齐的。

答案 2 :(得分: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十六进制)。