Linux size command, why are bss and data sections not zero?

Linux size command, why are bss and data sections not zero?



I came across the size command which gives the section size of the ELF file. While playing around with it, I created an output file for the simplest C++ program :


int main()return 0;



Clearly, I have not defined any initialized or uninitialized, data then why are my BSS and DATA sections of the size 512 and 8 bytes?



I thought it might be because of int main(), I tried creating object file for the following C program :


int main()


void main()



I still don't get 0 for BSS and DATA sections.



Is it because a certain minimum sized memory is allocated to those section?



EDIT- I thought it might be because of linked libraries but my object is dynamically linked so probably it shouldn't be the issue



This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.





There is a static library for the C runtime which is linked by the compiler, and which includes the real entry point which calls main(). You should see the real entry point function e.g. if you run objdump --disassemble. objdump -f will show information including the "start address".
– sourcejedi
Aug 22 at 7:13


objdump --disassemble


objdump -f





I see a few libraries linked to my object when i run objdump on it, the ouput of file a.out said it's dynamically linked ELF so i assumed there are no statically linked libraries, guess i was wrong.
– Time Traveller
Aug 22 at 7:20


file a.out





Related: A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux
– nxnev
Aug 22 at 7:37





Related: unix.stackexchange.com/questions/419697/… and unix.stackexchange.com/questions/418354/…
– Rui F Ribeiro
Aug 22 at 7:57





Compile it with -g and run nm on it - you will see all the symbols in the elf file.
– filo
Aug 22 at 11:22


nm




2 Answers
2



In fact, if you are compiling with the libc attached to the binary, there are functions that are added before (and after) the main() function. They are here mostly to load dynamic libraries (even if you do not need it in your case) and unload it properly once main() end.


main()


main()



These functions have global variables that require storage; uninitialized (zero initialized) global variables in the BSS segment and initialized global variables in the DATA segment.



This is why, you will always see BSS and DATA in all the binaries compiled with the libc. If you want to get rid of this, then you should write your own assembly program, like this (asm.s):


asm.s


.globl _start
 _start:
mov %eax, %ebx



And, then compile it without the libc:


$> gcc -nostdlib -o asm asm.s



You should reduce your footprint to the BSS and DATA segment on this ELF binary.





Thank you! I figured out my mistake, time to brush up my concepts of gcc and linking libraries.
– Time Traveller
Aug 22 at 7:25





No problem, this field is huge! You have to start somewhere! Good luck and keep going! :)
– perror
Aug 22 at 7:26





Please be more precise: "These functions use a little bit of memory that is taken from the BSS and the DATA segment." could better be told as "These functions have global variables that require storage; uninitialized (zero initialized) global variables in the BSS segment and initialized global variables in the DATA segment"
– Paul Ogilvie
Aug 22 at 13:43



int main()return 0; puts data in .text only.


int main()return 0;


.text


$ echo 'int main()return 0;' | gcc -xc - -c -o main.o && size main.o
text data bss dec hex filename
67 0 0 67 43 main.o



You're probably sizeing a fully linked executable.


size


$ gcc main.o -o main && size main
text data bss dec hex filename
1415 544 8 1967 7af main





thank you! i figured out my mistake :)
– Time Traveller
Aug 22 at 7:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)