Calling Global Constructors: Difference between revisions

Add information about GCC extensions
[unchecked revision][unchecked revision]
(More work)
(Add information about GCC extensions)
Line 18:
 
The idea is that the these files toggether form _init and _fini functions during the linking. This is done by storing the _init function in the .init section, and the _fini function in the .fini section. Each file then contributes a bit to these sections and the linker makes glues together the fragments in the code specified on the command line. crti.o provides the function header, crtbegin.o and crtend.o provide the body, and crtn.o provide the footer (return statement).
 
== Using global constructors from C ==
As a special extension, GCC allows C programs to run functions as global constructors. For more information, consult the compiler documentation. This is normally used as:
 
<source lang="c">
__attribute__ ((constructor)) void foo(void)
{
printf("foo is running and printf is available at this point\n");
}
 
int main(int argc, char* argv[])
{
printf("%s: main is running with argc=%i\n", argv[0], argc);
}
</source>
 
== Using crti.o, crtbegin.o, crtend.o, and crtn.o in a Kernel ==
Anonymous user