Why BSS segment is "16" by default?
Asked Answered
C

1

4

As per my knowledge, segmentation for c program is:

        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 RHEL 5.4 64-bit machine, for below c program

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

when I do:

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

I am unable to understand why is

bss=16

As I am not declaring/initializing any global/static vars?

Connell answered 10/3, 2015 at 11:2 Comment(0)
E
2

It's worse on Windows with gcc:

main.c:

#include <stdio.h>

int main( int argc, char* argv[] )
{
    return 0;
}

compile:

C:\>gcc main.c

size:

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

bss includes the whole linked executable and in this case various libraries are linked in which do use static c initialisation.

Using -nostartfiles gets a much better result on windows. You can also try with -nostdlib and -nodefaultlibs

compile:

C:\>gcc -nostartfiles main.c

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 get an "executable" with exactly what you've compiled and a bss size of 0:

main.c:

#include <stdio.h>

int _main( int argc, char* argv[] )
{
    return 0;
}

compile:

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

size:

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

The executable won't run however!

Endora answered 10/3, 2015 at 11:22 Comment(3)
You mean to say c libs have global/static vars declared within them right?Connell
Not necessarily the c library, but various support libraries to get a working executable for your OS. Here, on Windows there's a lot of crud around main() to make it a working app. It's worth looking a the objdump -S ./a.out output on your system to see how much code surrounds what should be a simple piece of code. Your understanding is correct, it's just that the compiler adds support libraries "silently"Endora
@Connell it's also worth doing gcc -v main.c and checking the final linker step to see what libraries get linked against on your system. Under windows, for me, it's -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt c:/.../crtend.o Quite a few, although most are DLLsEndora

© 2022 - 2024 — McMap. All rights reserved.