Question: Static variable initialization

Rob Landley rob at landley.net
Tue Feb 23 22:24:56 UTC 2010


On Monday 22 February 2010 07:32:28 Harald Becker wrote:
> Hi!
>
> Just a question about initialization of static variables in busybox: Is
> it proved that those variables get initialized to zero even without any
> initializer? In the past I had several troubles with programs on
> different systems relying on this assumption.

Not really a busybox question, but:

On Linux, the compiler puts uninitialized globals are in the .bss section, as 
required by the ELF spec.  See the System V Application Binary Interface, 
chapter 4, table 4-13, page 62-ish:

  http://refspecs.freestandards.org/elf/gabi41.pdf

Symbols in ELF are tracked by section, offset, and size, and if  the section 
they live in is the BSS then that's a big chunk of memory that gets zeroed 
memory allocated for it when the program is launched.  (Well actually it gets 
a big mmap() and then the actual physical pages are allocated on write faults.  
Or at least they used to implement it that way back when we had a zero page, 
I'm not quite sure what the kernel does these days.)

I have no idea what non-ELF systems (like Windows with PXE) does, and don't 
particularly care either.  (Well, I care about about what Mac does with mach-
o, but assume that's at least vaguely sane.)

Rob

P.S. The ELF specs are useful for understanding the format of executables, 
shared libraries, .o files, what the linker and dynamic linker actually do, and 
so on.  The c99 spec (http://busybox.net/~landley/c99-draft.html) is useful 
for understanding what C99 actually requires, and SUSv4 
http://www.opengroup.org/onlinepubs/9699919799/toc.htm (also known as the open 
group base specification, also known as posix... more or less the same thing 
these days, like ANSI/ISO C) describes lots of command line utilities and 
system calls and such.  (Unfortunatley, not all of 'em, but it's nice that 
what it _does_ define should work about the same on Linux and MacOS.)

Oh, and LP64 is nice to know about too, I have links to that from my old 
http://landley.net/code/toybox/design.html page.
-- 
Latency is more important than throughput. It's that simple. - Linus Torvalds


More information about the busybox mailing list