This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: __attribute__((constructor)) and order of calls


Hi,

>    Let suppose that I've 2 or more functions which are "characterized"
>    by the __attribute__((constructor)) attribute.
>

The functions which has attribute constructor will all be grouped in
__CTORS_LIST_ and those has destructor attribute will be grouped in
__DTORS_LIST_

while the program starts the function __do_global_ctors_aux which in .init
section will call the constructors starting from the tail of
__CTORS_LIST_ .
while the program ends the functions __do_global_dtors_aux which is in
.fini section will call the destructors from the head of __DTORS_LIST_ .

for eg.

...
static void f1() __attribute__ ((constructor));
static void f2() __attribute__ ((constructor));
static void f3() __attribute__ ((constructor));

static void f2() { ...; }
static void f1() { ...; }
static void f3() { ...; }
...

the order of call will be f3, f1, f2. the order will be reversed in case
of destructors.


--
arun.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]