When compiling this:
// external definitions
int value1 = 0;
static int value2 = 0;
the gcc compiler generates the following assembly:
.globl value1
.bss
.align 4
.type value1, @object
.size value1, 4
value1:
.zero 4
.local value2
.comm value2,4,4
However, when i initialize the variables to a value other than zero such as:
// external definitions
int value1 = 1;
static int value2 = 1;
the gcc compiler generated the following:
.globl value1
.data
.align 4
.type value1, @object
.size value1, 4
value1:
.long 1
.align 4
.type value2, @object
.size value2, 4
value2:
.long 1
My questions are:
- Why in the first case the values are allocated in the bss segment while in the second case in the data segment.
- Why value2 variable is defined as .local and .comm in the first case, while not in the second.
Generally speaking, the
bsssection contains uninitialized values and thedatasection contains initialized values. However, gcc places values that are initialized to zero into thebsssection instead of thedatasection, as thebsssection is zeroed out in runtime anyway, it doesn’t make much sense to store zeros in thedatasection, this saves some disk space, from man gcc:I’m not sure why
.commis used with static storage which is local to an object file, it is usually used to declare common symbols that, if not defined/initialized, should be merged by the linker with symbol that have the same name from other object files and that’s why it’s not used in the second example because the variables are initialized, from theasmanual