Make GCC initialize global variable using instructions instead of assembly directive such as .long or .quad
William Tambe
tambewilliam@gmail.com
Tue Jan 7 21:04:00 GMT 2020
On Tue, Jan 7, 2020 at 3:32 PM J Decker <d3ck0r@gmail.com> 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.
> I can certainly generate things that run code for initialization.
It must not be necessary to use __attribute__((constructor)) to achieve this.
I am looking for a solution where GCC will generate the initialization
instruction in a similar manner that it would do it if the
initialization statement was local to a function.
Possibly with the ability to specify to which function those
initialization instructions should be appended to.
>
> On Tue, Jan 7, 2020 at 12:09 PM William Tambe <tambewilliam@gmail.com> wrote:
>>
>> Given the following code example:
>>
>> void myfunc (void) {}
>> void(*)(void) initmyfunc(void) {return myfunc; }
>> void *myglobal = initmyfunc();
>>
>> void main (void) {
>> return;
>> }
>
> as c++...
> otherwise in C, no, without attribute constructor, or, attribute seg, and doing something to process a custom segment of initializers...
> I write a macro like
>
>
> #define PRELOAD(name) \
> void name##__LINE__(void) __attribute__((constructor)); \
> void name(void)
>
> PRELOAD( someUniqueName ) {
> /* some init that gets run */
> }
>
> could even refit it more like...
>
> #define PRELOAD(name) \
> name(void) __attribute__((constructor)); \
> void name
>
> void PRELOAD( someUniqueName ) (void) {
> /* some init that gets run */
> }
>
> But really, I recall all of that was mentioned before... maybe without specific examples...
> The actual thing I do for pre-main init is the constructor functions just call a registration of some other fucntion at some priority....
> https://github.com/d3x0r/SACK/blob/master/include/deadstart.h#L507-L514
>
> Constructor atttribute in C++ actually has a priority available too....
>
>
>
>> Is there a way to make GCC initialize global variable using
>> instructions instead of assembly directive such as .long or .quad ?
>> 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.
More information about the Gcc-help
mailing list