Re: Make GCC initialize global variable using instructions instead of assembly directive such as .long or .quad

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 20, 2020 at 01:58:17PM -0500, William Tambe wrote:
> On Mon, Jan 20, 2020 at 1:28 PM Arvind Sankar <nivedita@xxxxxxxxxxxx> wrote:
> >
> > On Tue, Jan 07, 2020 at 04:04:33PM -0500, William Tambe wrote:
> > > On Tue, Jan 7, 2020 at 3:32 PM J Decker <d3ck0r@xxxxxxxxx> wrote:
> > > >
> > > > I do think having a the reason why you would want to do this instead of 'how can I do this' might help...    (something of an X-Y problem)
> > >
> > > I agree; I have added the reason to my previous email:
> > > Such that in the above example the address of myfunc that is used to
> > > initialize myglobal get computed at run-time instead of at compile
> > > time.
> > >
> >
> > The question is what actual problem are you facing with the way it's
> > done now. Do you have some special environment you need to run in where
> > the normal support code to make this work doesn't exist?
> 
> In my environment I do not have a dynamic linker; hence the reason I
> am looking for a solution where GCC will generate the initialization
> instruction for the global variable just like it would do it for a
> local variable.
> 
> >
> > In a normal system, even though the assembler output for your file looks
> > like myglobal is being initialized at compile time, it actually gets
> > initialized at run-time, by the dynamic linker (ld-linux.so) that's part
> > of glibc (or whatever libc you use). Only the offset from the base
> > address of the program is calculated and initialized at compile (or
> > really link) time.
> 
> I do not have a dynamic linker in my environment.
> Shouldn't it be possible for PIE executable to run without the need of
> a dynamic linker ?
> 

gcc and ld assume that the system library will provide the dynamic
linker. AFAIK, gcc has no facilities to generate code to do any
necessary run-time relocations. ld has --no-dynamic-linker but all that
does is leave out the INTERP header that normally indicates which
dynamic linker to load.

gcc does have a -static-pie option that is meant to do what you want,
i.e. produce a PIE executable that doesn't need an external dynamic
linker, but that still relies on having a libc. It links a special
startup object file rcrt1.o in place of the usual Scrt1.o. It basically
links part of the dynamic linker directly into your program. This is
supported by both musl and glibc (with configure option
--enable-static-pie for the latter).

If you're not going to be using a libc, your options would be to either
avoid constructs that result in run-time relocations being needed (such
as initializing global variables to the address of some
function/variable), or add at least some minimal relocation processing
in your startup code -- it might be possible to just extract the
static-pie bits you need from musl without having to bring in the whole
library, as these are pretty small, looks like less than 100 lines of
actual code + a few include files to get definitions.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux