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
", 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.
- Why
int
takes up 8 bytes in BSS? The sizeof(x)
in my code shows that the int
actually takes up 4 bytes.
- The
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
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…