Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous behaviour of .bss segment in C program

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

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

and executed the follwing to understand size changes in .bss segment.

gcc test.c -o test
size test

The output came out as:-

   text    data     bss     dec     hex filename
   1115     552       8    1675     68b test

I didn't declare anything globally or of static scope. So please explain why the bss segment size is of 8 bytes.

I made the following change:-

#include<stdio.h>
int x;    //declared global variable
int main()
{
   return 0;
}

But to my surprise, the output was same as previous:-

   text    data     bss     dec     hex filename
   1115     552       8    1675     68b test

Please explain. I then initialized the global:-

#include<stdio.h>
int x=67;    //initialized global variable
int main()
{
   return 0;
}

The data segment size increased as expected, but I didn't expect the size of bss segment to reduce to 4 (on the contrary to 8 when nothing was declared). Please explain.

text       data     bss     dec     hex filename
1115        556       4    1675     68b test

I also tried the comands objdump, and nm, but they too showed variable x occupying .bss (in 2nd case). However, no change in bss size is shown upon size command.

I followed the procedure according to: http://codingfox.com/10-7-memory-segments-code-data-bss/ where the outputs are coming perfectly as expected.

like image 794
Maharshi Roy Avatar asked Nov 18 '16 14:11

Maharshi Roy


2 Answers

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
like image 92
LPs Avatar answered Oct 20 '22 22:10

LPs


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.

like image 2
Lundin Avatar answered Oct 20 '22 21:10

Lundin