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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












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 :



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







share|improve this question












migrated from unix.stackexchange.com Aug 22 at 13:32


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










  • 3




    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










  • 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






  • 2




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






  • 1




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






  • 1




    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














up vote
8
down vote

favorite












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 :



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







share|improve this question












migrated from unix.stackexchange.com Aug 22 at 13:32


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










  • 3




    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










  • 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






  • 2




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






  • 1




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






  • 1




    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












up vote
8
down vote

favorite









up vote
8
down vote

favorite











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 :



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







share|improve this question












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 :



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









share|improve this question











share|improve this question




share|improve this question










asked Aug 22 at 6:55







Time Traveller











migrated from unix.stackexchange.com Aug 22 at 13:32


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






migrated from unix.stackexchange.com Aug 22 at 13:32


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









  • 3




    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










  • 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






  • 2




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






  • 1




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






  • 1




    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












  • 3




    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










  • 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






  • 2




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






  • 1




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






  • 1




    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







3




3




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




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












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




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




2




2




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




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




1




1




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




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




1




1




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




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












2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










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.



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):



.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.






share|improve this answer






















  • 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






  • 1




    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

















up vote
8
down vote













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



$ 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.



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





share|improve this answer




















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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51968080%2flinux-size-command-why-are-bss-and-data-sections-not-zero%23new-answer', 'question_page');

);

Post as a guest





























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
8
down vote



accepted










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.



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):



.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.






share|improve this answer






















  • 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






  • 1




    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














up vote
8
down vote



accepted










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.



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):



.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.






share|improve this answer






















  • 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






  • 1




    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












up vote
8
down vote



accepted







up vote
8
down vote



accepted






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.



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):



.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.






share|improve this answer














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.



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):



.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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 22 at 13:45

























answered Aug 22 at 7:22









perror

4,386124362




4,386124362











  • 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






  • 1




    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
















  • 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






  • 1




    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















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




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




1




1




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




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




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












up vote
8
down vote













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



$ 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.



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





share|improve this answer




















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














up vote
8
down vote













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



$ 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.



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





share|improve this answer




















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












up vote
8
down vote










up vote
8
down vote









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



$ 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.



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





share|improve this answer












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



$ 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.



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






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 22 at 7:18









PSkocik

27.9k43762




27.9k43762











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
















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















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




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












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51968080%2flinux-size-command-why-are-bss-and-data-sections-not-zero%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Mutual Information Always Non-negative

Why am i infinitely getting the same tweet with the Twitter Search API?