There are many extended features in gcc. Some of them are especially
useful in ELF. One of them is __attribute__
. One can place
a function on the __CTOR_LIST__
or
__DTOR_LIST__
with __attribute__
. For example,
static void foo () __attribute__ ((constructor)); static void bar () __attribute__ ((destructor)); static void foo () { } static void bar () { }
The __attribute__ ((constructor))
causes foo to be
called automatically before execution enters main. Similarly,
the __attribute__ ((destructor))
causes bar to be
executed automatically after main returns or when exit
has been called. Both foo and bar must take
no arguments and must be static void functions. Under ELF, this
feature works
in both normal executables and shared libraries.