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

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
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

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

Post 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
Last edited by dchapiesky on Thu Jan 05, 2017 6:36 am, edited 2 times in total.
Plagiarize. Plagiarize. Let not one line escape thine eyes...
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Static initializers

Post by thepowersgang »

I'll be in .data, with a relocation entry ensuring that it has the correct value after linking.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: Static initializers

Post 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!
Plagiarize. Plagiarize. Let not one line escape thine eyes...
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Static initializers

Post 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.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Static initializers

Post 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.
Learn to read.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Static initializers

Post 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).
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

[SOLVED] Re: Static initializers

Post 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...
Plagiarize. Plagiarize. Let not one line escape thine eyes...
Post Reply