C++ and bss section

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
proxy

C++ and bss section

Post by proxy »

as far as i know, the bss section of any program should be zeroed on initialization, however, when i try to do this in my code (usually just before calling my real main function) my code dies miserably. I'm using C++, so at first i thought i was perhaps trashing some vtables, but they wouldnt go into the bss section anyway..

any thoughts, anyone who has been using C++ in there kernel bother to do this?

proxy
Tim

Re:C++ and bss section

Post by Tim »

Well, it must be possible to zero the bss, otherwise nobody would do it.

The way I do it is to zero the bss from the assembly-language startup code, before any C code is run. This way the C code can't start writing values to the bss before it is zeroed. A simple REP STOSD is enough, along with symbols (defined in a linker script) for the start and end of the bss section.
Schol-R-LEA

Re:C++ and bss section

Post by Schol-R-LEA »

It shouldn't be necessary. actually. According to the as User Reference
When your program starts running, all the contents of the bss section are zeroed bytes.
HTH. Note tha the reason [tt]auto[/tt] variables need to cleared, even if the BSS and stack segments are cleared beforehand, is because they are allocated on the stack; if a function is called, and then returns, whatever arguments and local variables it used will still be there when the next called function sets up its own stack frame.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C++ and bss section

Post by Pype.Clicker »

Schol-R-LEA wrote: It shouldn't be necessary. actually.
it IS necessary:
When your program starts running, all the contents of the bss section are zeroed bytes.
is only true when you're in an existing execution environment. But if you're building your own environment (and if you're programming an OS, there are chances you are ...), you'll have to wipe the BSS so that it will fulfill the requirements of the usual programming environment.
proxy

Re:C++ and bss section

Post by proxy »

i figured out why i was having a problem :) my stack is an array (like in multiiboot example) which ends up in the BSS section. And by the time I was trying to zero things out, the stack was in use. So I was trashing the stack data...no good.

Thanks for the input, I just added a short block of asm in my initial boot.S which zeros out the bss section.

proxy
Post Reply