28 extra bytes in bss

struct abc { char cc[32]; } mystruct; int main() { } 

When I run the above program, the .bss section has 64 bytes. I expected this to be 36 bytes. 32 bytes for mystruct and 4 bytes taken by other libraries.

If I change char cc[32] to char cc[31] , then I get 36 bytes in .bss.

 -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 in hexadecimal format

Why are these extra 28 bytes in .bss?

+6
source share
3 answers

The last column of readelf output is alignment, and the value shown is really "32". Thus, the size of the BSS is rounded to the nearest multiple of 32 bytes.

+4
source

objdump -sSx a.out gives, among other things, ...

 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 

item 24, in the "Algn" section we see 2 ** 5 or 2 ^ 5 aka 32. This means that bss is allocated in 32 byte chunks because it is 32-bit aligned.

+1
source

objdump -t a.out | grep bss gives you

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

expected (00000020 in hexadecimal format) of 32 bytes.

0
source

Source: https://habr.com/ru/post/951408/


All Articles