GCC static vs non-static global variables and .bss
-
- Member
- Posts: 1146
- Joined: Sat Mar 01, 2014 2:59 pm
GCC static vs non-static global variables and .bss
Hi,
I'm having a bit of trouble with GCC. If I declare a static global variable in a source file, objdump -h shows the size of the variable reflected in the size of the .bss segment in the object (.o) file. If I declare a non-static global variable, the size is not shown in the .bss segment. Where are non-static global variables allocated/reserved in the object file?
Thanks,
onlyonemac
I'm having a bit of trouble with GCC. If I declare a static global variable in a source file, objdump -h shows the size of the variable reflected in the size of the .bss segment in the object (.o) file. If I declare a non-static global variable, the size is not shown in the .bss segment. Where are non-static global variables allocated/reserved in the object file?
Thanks,
onlyonemac
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Re: GCC static vs non-static global variables and .bss
They are saved in the .data section where all your data (read, write) is saved.
Re: GCC static vs non-static global variables and .bss
This article can help.
Could you check 'objdump -t' to find out the section where the non-static global resides?
Could you check 'objdump -t' to find out the section where the non-static global resides?
-
- Member
- Posts: 1146
- Joined: Sat Mar 01, 2014 2:59 pm
Re: GCC static vs non-static global variables and .bss
The size of .data doesn't change when I make the variable non-static; only the size of .bss decreases by the size of the variable.Ch4ozz wrote:They are saved in the .data section where all your data (read, write) is saved.
If it's static then it says .bss; if it's non-static then it says *COM*. Reading the linked article I see what this means, although I'm not sure how to integrate that into my linkscript.linuxyne wrote:This article can help.
Could you check 'objdump -t' to find out the section where the non-static global resides?
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Re: GCC static vs non-static global variables and .bss
Of course it doesnt change, all other sections then .bss are alignedonlyonemac wrote:The size of .data doesn't change when I make the variable non-static; only the size of .bss decreases by the size of the variable.Ch4ozz wrote:They are saved in the .data section where all your data (read, write) is saved.If it's static then it says .bss; if it's non-static then it says *COM*. Reading the linked article I see what this means, although I'm not sure how to integrate that into my linkscript.linuxyne wrote:This article can help.
Could you check 'objdump -t' to find out the section where the non-static global resides?
Re: GCC static vs non-static global variables and .bss
* The symbol will end up in .bss, if it is uninitialised everywhere.onlyonemac wrote:I'm not sure how to integrate that into my linkscript.
* Or, use fno-common for immediate certainty within .o files.
-
- Member
- Posts: 1146
- Joined: Sat Mar 01, 2014 2:59 pm
Re: GCC static vs non-static global variables and .bss
Except it doesn't, unless it's declared static.linuxyne wrote:* The symbol will end up in .bss, if it is uninitialised everywhere.
Oh, and I'm also having trouble with my linkscript where .bss doesn't seem to be copied to the final binary.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Re: GCC static vs non-static global variables and .bss
I should have been more clear about my statements.
The non-static global symbol will end up in .bss section of the executable, if it is uninitialised in all .o files that define that symbol. This should not be very difficult to test and verify.
The non-static global symbol will end up in .bss section of the executable, if it is uninitialised in all .o files that define that symbol. This should not be very difficult to test and verify.
Re: GCC static vs non-static global variables and .bss
Are you saying there isn't even a section header referencing the .bss section? You shouldn't expect any more in the executable.onlyonemac wrote:Oh, and I'm also having trouble with my linkscript where .bss doesn't seem to be copied to the final binary.
-
- Member
- Posts: 1146
- Joined: Sat Mar 01, 2014 2:59 pm
Re: GCC static vs non-static global variables and .bss
Never mind I now have both static and non-static globals in their correct places in the final linked binary.
It's a flat binary, so no chance of checking sections here. The size wasn't changing when I added the non-static global, but it is now as it should be.linuxyne wrote:The non-static global symbol will end up in .bss section of the executable, if it is uninitialised in all .o files that define that symbol. This should not be very difficult to test and verify.
Again, it's a flat binary so there are no section headers. I was expecting it to actually reserve the space for the .bss section in the binary since it was outputting a flat binary, but it seems it didn't (until now ).iansjack wrote:Are you saying there isn't even a section header referencing the .bss section? You shouldn't expect any more in the executable.onlyonemac wrote:Oh, and I'm also having trouble with my linkscript where .bss doesn't seem to be copied to the final binary.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Re: GCC static vs non-static global variables and .bss
For reference, the COMMON section (what you saw labeled as *COM*) serves the same purpose as .bss and can be included in the linker script the same way (something like (*(.bss COMMON)") or bypassed with -fno-common as mentioned earlier.
Re: GCC static vs non-static global variables and .bss
If only it were so.Ch4ozz wrote:They are saved in the .data section where all your data (read, write) is saved.
Uninitialized variables or variables assigned as zero will end up in the .bss. Of course the .bss is technically data, however when creating flat
binaries if the loader (without the aid of a non existent header) doesn't initialize the .bss to zero you may not get the expected zero value.
Re: GCC static vs non-static global variables and .bss
Compile it with -fno-zero-initialized-in-bssmikegonta wrote:If only it were so.Ch4ozz wrote:They are saved in the .data section where all your data (read, write) is saved.
Uninitialized variables or variables assigned as zero will end up in the .bss. Of course the .bss is technically data, however when creating flat
binaries if the loader (without the aid of a non existent header) doesn't initialize the .bss to zero you may not get the expected zero value.
-
- Member
- Posts: 1146
- Joined: Sat Mar 01, 2014 2:59 pm
Re: GCC static vs non-static global variables and .bss
That's not a problem for me as I'm initialising everything myself. I never assume that an uninitialised value contains zero.mikegonta wrote:If only it were so.Ch4ozz wrote:They are saved in the .data section where all your data (read, write) is saved.
Uninitialized variables or variables assigned as zero will end up in the .bss. Of course the .bss is technically data, however when creating flat
binaries if the loader (without the aid of a non existent header) doesn't initialize the .bss to zero you may not get the expected zero value.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Re: GCC static vs non-static global variables and .bss
Sounds very dangerous because there are problems with the magic number zero. If you take a random memory location, it is very likely that it may just be zero. I can sense that this will cause you problems in the future, e.g. random bugs on conditions other than you used when testing your code. Another problem is compiler assumptions. Your explicit value initialisation may be optimized away if it is redundant.onlyonemac wrote:That's not a problem for me as I'm initialising everything myself. I never assume that an uninitialised value contains zero.