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]

 



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 can certainly generate things that run code for initialization.

On Tue, Jan 7, 2020 at 12:09 PM William Tambe <tambewilliam@xxxxxxxxx>
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.
>



[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