Page 1 of 1

[SOLVED] Static initializers (or check your sections now)

Posted: Wed Jan 04, 2017 9:50 pm
by dchapiesky
getenv_r.c

Code: Select all

extern char **environ;
static char ***p_environ = &environ;
Can someone explain where the initialization of p_environ takes place?

When relocated to form a static binary?
or when _init() traverses _init_array?

It shouldn't be in .bss as it is not zero

Re: Static initializers

Posted: Wed Jan 04, 2017 11:15 pm
by thepowersgang
I'll be in .data, with a relocation entry ensuring that it has the correct value after linking.

Re: Static initializers

Posted: Thu Jan 05, 2017 1:44 am
by dchapiesky
assuming I have my sections correct....

would objcopy'ing to binary format negate the initialization?

Code runs... but p_environ is 0 and not address &environ.

This is driving me nuts - I have checked init/fini, etc...

I did test of

Code: Select all

static int a = 100;

if (a != 100) {
  fail....
} 
a isn't even being set... so it must be sections...

thanks!

Re: Static initializers

Posted: Thu Jan 05, 2017 2:40 am
by iansjack
I believe that static variables can only be initialized using constant literals. I find it good practice to always explicitly assign an initial value to a static variable within a function.

Re: Static initializers

Posted: Thu Jan 05, 2017 5:32 am
by dozniak
dchapiesky wrote:assuming I have my sections correct....
Don't assume, check.
dchapiesky wrote:would objcopy'ing to binary format negate the initialization?
It should not, however check your .data section in the .o file with objdump and then compare with results of objcopy-ing it.

Re: Static initializers

Posted: Thu Jan 05, 2017 5:45 am
by alexfru
iansjack wrote:I believe that static variables can only be initialized using constant literals.
True for C. Not true for C++.
iansjack wrote:I find it good practice to always explicitly assign an initial value to a static variable within a function.
It stands out more. But static is sufficiently visible alone (first thing on a line).

[SOLVED] Re: Static initializers

Posted: Thu Jan 05, 2017 5:54 am
by dchapiesky
Thank you all for the input... it was the sections.. but it was also a typo...

an extra "f" in my ld script in DATA_SEGMENT_ALIGN()

was chucking these variables out into my kernel heap....

the kernel is loaded in binary format (no elf loader) so the code ran... right up to the point
that the kernel heap started using that area for something else.... #-o

interestingly I memset kernel heap regions to 0 when I allocate them... So..... the p_environ was showing 0 as well... had I used a canary value this might have been fixed alot sooner...
iansjack wrote:I believe that static variables can only be initialized using constant literals. I find it good practice to always explicitly assign an initial value to a static variable within a function.
The code snippet is from newlib's getenv_r.c file...

I though what you thought as well... that's why I opened the thread.

Thank you again everyone. :shock: my eyes are tired from reading hex...