Make GCC initialize global variables generating instructions in crti.S _init

William Tambe tambewilliam@gmail.com
Tue Jan 7 01:35:00 GMT 2020


On Mon, Jan 6, 2020 at 11:52 AM Florian Weimer <fw@deneb.enyo.de> wrote:
>
> * William Tambe:
>
> > I would like for GCC to automatically generate instructions to
> > initialize global variables without having to manually create a
> > function with attribute((constructor)) to initialize those global
> > variables. In the example below from my previous email, GCC should
> > automatically generate instructions to initialize myglobal just like
> > it generates instructions to initialize mylocal; except those
> > instructions to initialize myglobal are to be generated within the
> > crti.S _init function:
> >
> >     void myfunc (void) {}
> >     void *myglobal = myfunc;
> >     void main (void) {
> >             void *mylocal = myfunc;
> >     }
>
> I believe this would be best implemented in the link editor.  It sees
> the relocations and can generate the appropriate code.  However, for

Assembly generated to initialize myglobal is as follow:

            .globl  myglobal
            .section        .data.rel.local,"aw",@progbits
            .align 8
            .type   myglobal, @object
            .size   myglobal, 8
    myglobal:
            .quad   myfunc

Is it actually possible to create a link editor script that convert
the above assembly into instructions in the function _init ?

> position-indepedent code, usually a table-based approach with a
> minimal loader is typically used.

Could I have a pointer into the source code showing how it is implemented ?



More information about the Gcc-help mailing list