OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 5:42 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [SOLVED] Static initializers (or check your sections now)
PostPosted: Wed Jan 04, 2017 9:50 pm 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
getenv_r.c
Code:
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

_________________
Plagiarize. Plagiarize. Let not one line escape thine eyes...


Last edited by dchapiesky on Thu Jan 05, 2017 6:36 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Static initializers
PostPosted: Wed Jan 04, 2017 11:15 pm 
Offline
Member
Member
User avatar

Joined: Tue Dec 25, 2007 6:03 am
Posts: 734
Location: Perth, Western Australia
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


Top
 Profile  
 
 Post subject: Re: Static initializers
PostPosted: Thu Jan 05, 2017 1:44 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
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:
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...


Top
 Profile  
 
 Post subject: Re: Static initializers
PostPosted: Thu Jan 05, 2017 2:40 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Static initializers
PostPosted: Thu Jan 05, 2017 5:32 am 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Static initializers
PostPosted: Thu Jan 05, 2017 5:45 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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).


Top
 Profile  
 
 Post subject: [SOLVED] Re: Static initializers
PostPosted: Thu Jan 05, 2017 5:54 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 86 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group