I am trying to learn the structure of executable files of C program. My environment is GCC and 64bit Intel processor.
Consider the following C code a.cc
.
#include <cstdlib>
#include <cstdio>
int x;
int main(){
printf("%d\n", sizeof(x));
return 10;
}
The size -o a
shows
text data bss dec hex filename
1134 552 8 1694 69e a
After I added another initialized global variable y.
int y=10;
The size a
shows (where a
is the name of the executable file from a.cc)
text data bss dec hex filename
1134 556 12 1702 6a6 a
As we know, the BSS
section stores the size of uninitialized global variables and DATA
stores initialized ones.
int
takes up 8 bytes in BSS? The sizeof(x)
in my code shows that the int
actually takes up 4 bytes.int y=10
added 4 bytes to DATA which makes sense since int
should take 4 bytes. But, why does it adds 4 bytes to BSS?The difference between two size
commands stays the same after deleting the two lines #include ...
.
Update:
I think my understanding of BSS is wrong. It may not store the uninitialized global variables. As the Wikipedia says "The size that BSS will require at runtime is recorded in the object file, but BSS (unlike the data segment) doesn't take up any actual space in the object file." For example, even the one line C code int main(){}
has bss 8
.
Does the 8 or 16 of BSS comes from alignment
?
What is the difference between the Data and BSS sections? BSS refers to uninitialized global and static objects and Data refers to initialized global and static objects. Both BSS and Data usually refer to RAM objects.
In embedded software, the bss segment is mapped into memory that is initialized to zero by the C run-time system before main() is entered. Some C run-time systems may allow part of the bss segment not to be initialized; C variables must explicitly be placed into that portion of the bss segment.
bss section is used by the compiler for global and static variables. It is one of the default COFF sections that is used to reserve a specified amount of space in the memory map that can later be used for storing data. It is normally uninitialized. All global and static variables in a C program are placed in the .
'bss' is for the uninitialized data in RAM which is initialized with zero in the startup code.
It doesn't, it takes up 4 bytes regardless of which segment it's in. You can use the nm
tool (from the GNU binutils package) with the -S
argument to get the names and sizes of all of the symbols in the object file. You're likely seeing secondary affects of the compiler including or not including certain other symbols for whatever reasons.
For example:
$ cat a1.c
int x;
$ cat a2.c
int x = 1;
$ gcc -c a1.c a2.c
$ nm -S a1.o a2.o
a1.o:
0000000000000004 0000000000000004 C x
a2.o:
0000000000000000 0000000000000004 D x
One object file has a 4-byte object named x
in the uninitialized data segment (C
), while the other object file has a 4-byte object named x
in the initialized data segment (D
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With