Recent research on QEMU has similar code when initializing:
#define MODULE_INIT (function, type) \
static void __attribute__ ((constructor)) Do_qemu_init_ # # function (void ) { \
register_module_init (function, type); \
}
do_qemu_init_** must be the initialization of the module, but did not call the Do_qemu_init place, odd strange, why.
Take a closer look at this function and the modifier contains __attribute__ (constructor), which is probably what this guy did.
Write code test:
#include <stdio.h>
#include <stdlib.h>
void static __attribute__ ((constructor)) Before_main ()
{
printf ("before main\n");
}
void Static __attribute__ ((destructor)) After_main ()
{
printf ("after main\n");
}
int main (int argc, char** argv)
{
printf ("Hello world!\n");
}
Compile execution:
root@mothership:/home/source/test# gcc a.c-o a
root@mothership:/home/source/test#./A
before main
Hello world!
After main
root@mothership:/home/source/test#
Sure enough,
__attribute__ ((constructor))-decorated functions are executed before the main function
__attribute__ ((destructor))-decorated functions are executed after the main function