Why is the default BSS segment "16"?

As I know, segmentation for c-program:

High address |---------------------------| |env/cmd line args vars | |---------------------------| | stack segment |--> uninitialized auto vars |---------------------------| |---------------------------| |---------------------------| | heap segment |--> dynamic allocated memory |---------------------------| | BSS segment |--> uninitialized static/global vars |---------------------------| | data segment |--> initialized static/global vars |---------------------------| | text segment |--> initialized auto vars/exec instructions |---------------------------| Low address 

On my 64-bit RHEL 5.4 machine for the program below c

 #include <stdio.h> int main() { } 

when i do this:

 # size a.out text data bss dec hex filename 1259 540 16 1815 717 a.out 

I can’t understand why

PBS = 16

How do I not declare / initialize any global / static vars?

+4
c linux size
source share
1 answer

This is worse for Windows with gcc:

main.c:

 #include <stdio.h> int main( int argc, char* argv[] ) { return 0; } 

compilation:

 C:\>gcc main.c 

The size:

 C:\>size a.exe text data bss dec hex filename 6936 1580 1004 9520 2530 a.exe 

bss includes the entire associated executable, in which case different libraries are linked that use static c-initialization.

Usage - nostartfiles gives a much better result for windows. You can also try with -nostdlib and -nodefaultlibs

compilation:

 C:\>gcc -nostartfiles main.c 

The size:

 C:\>size a.exe text data bss dec hex filename 488 156 32 676 2a4 a.exe 

Remove all the libraries (including the c library) and you will get an “executable” with what you compiled and the size of bss is 0:

main.c:

 #include <stdio.h> int _main( int argc, char* argv[] ) { return 0; } 

compilation:

 C:\>gcc -nostartfiles -nostdlib -nodefaultlibs main.c 

The size:

 C:\>size a.exe text data bss dec hex filename 28 20 0 48 30 a.exe 

The executable will not work.

+2
source share

All Articles