Ambiguous behaviour of .bss segment in C program

戏子无情 提交于 2019-12-01 04:08:06

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!