3
votes

I developed a simple C program as shown below..

int main()
{
    return 0;
}

I compiled the program using gcc v5.2.1. When I ran the Unix command 'size' on the executable below are the sizes it displayed..

text = 1131, data = 552, bss = 8

As per my understanding the data section hold initialized global data and BSS holds uninitialized global data. Though there are no global variables why do the 'data' and 'BSS' section show non-zero values?

2
There's a whole lot of runtime code linked to your program, e.g. the startup code. - user2371524
@FelixPalmen Right. Any idea what exactly would be the data in both segments? - Parth Shah
This heavily depends on your target platform and what tasks your runtime has to perform before calling main(). It has to setup agrv[], for example. Most of the time, an initialization loop is involved because data you don't explicitly initialize is placed in .bss and must therefore be set to 0 before your program can start ... etc. pp. - user2371524

2 Answers

2
votes

In a nutshell: Because your final program has more code than just the part you write. It must contain some runtime that e.g. does all the setup required before it can call main() (like populating argv, initializing data in .bss to zero, and so on) as well as cleanup after exit. What exactly is done in this code depends entirely on your implementation.

0
votes

Answering your question: the startup code has its own data. That data is shown in this example.

How the segments are called is implementation defined but most startup code & linker scripts use this most popular one.

.text - your program code

.rodata - RO only data - for example string literals. Many implemetations put there objects with the const (const int x[2] = {1,2};) will go there

.bss - uninitialized data with the static storage (ie global)

In C, statically-allocated objects without an explicit initializer are initialized to zero (for arithmetic types) or a null pointer (for pointer types). Implementations of C typically represent zero values and null pointer values using a bit pattern consisting solely of zero-valued bits (though this is not required by the C standard). Hence, the BSS segment typically includes all uninitialized objects (both variables and constants) declared at file scope (i.e., outside any function) as well as uninitialized static local variables (local variables declared with the static keyword); static local constants must be initialized at declaration, however, as they do not have a separate declaration, and thus are typically not in the BSS section, though they may be implicitly or explicitly initialized to zero. An implementation may also assign statically-allocated variables and constants initialized with a value consisting solely of zero-valued bits to the BSS section.

.data -

The .data segment contains any global or static variables which have a pre-defined value and can be modified. That is any variables that are not defined within a function (and thus can be accessed from anywhere) or are defined in a function but are defined as static so they retain their address across subsequent calls.

example:

 static char x[] = "Hello world";

String literal ""Hello world" is stored in the .rodata segment and it copied during the startup to the char table x, located in the .data segment.