Automatically initialize all global variables/arrays in GCC.

Daniel Lohmann daniel.lohmann@informatik.uni-erlangen.de
Sat Jul 7 22:06:00 GMT 2007


Hi Kevin,

In principle it is possible to do what you want.

We have to distinguish a few things here. Yes, ANSI-C requires that all 
uninitialized static/global variables have to be initialized with 0. 
However, it is not the compiler who does this, it is the startup code or 
the loader.

The C compiler usually puts variables that are supposed to be initialized 
with 0 in the .bss section instead of the .data section. Opposed to the 
.data section, the .bss section does not contain actual data, it just 
specifies the size of all elements it contains. The C compiler just 
*assumes* that the linker, loader, or the startup code of the C library 
initializes this block of memory with 0. This is an optimization; .data 
elements occupy space in the image (or ROM or flash memory) and in RAM 
whereas .bss elements neet to occupy RAM space only if they are initialized 
at run-time.
(Gcc provides even an option (-fno-zero-initialized-in-bss) to do not rely 
on this optimization, that is, to put all 0-initialized elements into the 
.data section as well.)

Therefore, you have a couple of options:

1) You may modify the startup or write some own initalization code to 
override the .bss section at runtime with whatever value you want.

2) You may use __attribute__((section, ".mysection)) on every uninitialized 
variable to instruct the compiler to put it into your own section. You may 
then use the linker or a runtime initialization routine to initialize this 
section.

The second solution is probably better, as other parts of the code 
(expecially SWIG) may rely on having 0-initialized globals. However, as you 
see all this is not so much a gcc thing, but more related to the binutils 
and library. You would probably have to extend or modify the linker script 
or exchange the startup code of the C library. I do not know the exact 
details about doing this on Linux (had to look them up) as I use gcc almost 
only on embedded targets. But what you want to achieve is definitely possible.

Daniel

Kevin Hung schrieb:
> Hi Michael,
> 
> Thanks for your reply. So I guess the answer is that this cannot be done 
> in C.
> 
> My area is in DSP and so our C code runs on the DSP chip. In DSP C
> linker, it allows us to fill a memory area with a magic constant. This
> is why I want to see if GCC allows us to do something like this and it
> seems the answer is no.
> 
> Based on your reply, it seems that the DSP linker violates the ISO C 
> standard.
> 
> 
> On 7/6/07, Michael Meissner <michael.meissner@amd.com> wrote:
>> On Thu, Jul 05, 2007 at 01:20:47PM -0700, Kevin Hung wrote:
>> > I am using gcc to compile our C code under Linux. After compiling and
>> > linking, we use SWIG to create python wrapper for the C code so that
>> > we can execute C functions in python.
>> >
>> > My question is: When our C program gets loaded into python, is it
>> > possible to have all global variables/arrays in our C code to be
>> > initialized to some magic constants (say all ones)? I think if this is
>> > possible, it would be a linker option under GCC. My gcc version is:
>>
>> The ISO C standard mandates that all static/global variables that aren't
>> otherwise initialized are initialized to 0.  Under Linux, variables 
>> that aren't
>> explicitly initialized get put into the bss sections that aren't 
>> allocated
>> space in the disk image, but the OS guarantees will be set to 0 when 
>> you touch
>> the page.
>>
>> In general, you probably don't want to do something like zap the bss 
>> area in a
>> startup routine, because you don't know what other modules might have 
>> put stuff
>> there.
>>
>> If you want to initialize a static/global to a value, you should 
>> create the
>> definition with an initializer.
>>
>> -- 
>> Michael Meissner, AMD
>> 90 Central Street, MS 83-29, Boxborough, MA, 01719, USA
>> michael.meissner@amd.com
>>
>>
>>



More information about the Gcc-help mailing list