Ambiguous behaviour of .bss segment in C program

后端 未结 2 923
栀梦
栀梦 2021-01-12 12:59

I wrote the simple C program (test.c) below:-

#include
int main()
{
   return 0;
}

and executed the follwing to understand s

相关标签:
2条回答
  • 2021-01-12 13:22

    When you compile a simple main program you are also linking startup code. This code is responsible, among other things, to init bss.

    That code is the code that "uses" 8 bytes you are seeing in .bss section.

    You can strip that code using -nostartfiles gcc option:

    -nostartfiles

    Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used

    To make a test use the following code

    #include<stdio.h>
    
    int _start()
    {
       return 0;
    }
    

    and compile it with

    gcc -nostartfiles test.c
    

    Youll see .bss set to 0

       text    data     bss     dec     hex filename
        206     224       0     430     1ae test
    
    0 讨论(0)
  • 2021-01-12 13:44

    Your first two snippets are identical since you aren't using the variable x.

    Try this

    #include<stdio.h>
    volatile int x;
    int main()
    {
       x = 1;
       return 0;
    }
    

    and you should see a change in .bss size.

    Please note that those 4/8 bytes are something inside the start-up code. What it is and why it varies in size isn't possible to tell without digging into all the details of mentioned start-up code.

    0 讨论(0)
提交回复
热议问题