Anonymous user
Calling Global Constructors: Difference between revisions
m
no edit summary
[unchecked revision] | [unchecked revision] |
m (moved User talk:Sortie/Calling Global Constructors to Calling Global Constructors: Alright! This article is ready for prime time.) |
mNo edit summary |
||
Line 1:
This tutorial discusses how to
On most platforms, the global constructors/destructors are stored in a stored array of function pointers and invoking these is as simple as traversing the array and running each element. However, the compiler does not always give access to this list, and some compilers considers this implementation details. In this case you will have to cooperate with the compiler - fighting the compiler will only cause trouble.
Line 5:
= GNU Compiler Collection - System V ABI =
The System V ABI (as used by <tt>i586-elf-gcc</tt>, <tt>x86_64-elf-gcc</tt>, and other ELF platforms) specifies use of
This scheme allows the compiler great control in program initialization and makes things easy for you, but you have to cooperate with the compiler or bad things will happen. Your cross-compiler will provide you with <tt>crtbegin.o</tt> and <tt>crtend.o</tt>. These files contain the internals that the compiler wish to hide from you, but wants you to use. To get access to this information, you will need to provide your own implementation of <tt>crti.o</tt> and <tt>crtn.o</tt>. Fortunately, this is easy and described in detail in this tutorial. The fifth file <tt>crt0.o</tt> contains the program entry point (normally <tt>_start</tt>) and calls the special <tt>_init</tt> function that runs the "program initialization tasks" that <tt>crti.o</tt>, <tt>crtbegin.o</tt>, <tt>crtend.o</tt>, and <tt>crtn.o</tt> together form, and your exit function will normally call the <tt>_fini</tt> function made by these objects. However, <tt>crt0</tt>.o is out of scope of this article. (Note that the object file that contains <tt>_start</tt> acts as <tt>crt0.o</tt> in a kernel.)
Line 17:
<source lang="bash">i585-elf-ld crt0.o crti.o crtbegin.o foo.o bar.o crtend.o crtn.o</source>
The idea is that the these files together form the <tt>_init</tt> and <tt>_fini</tt> functions during the linking. This is
== Using global constructors from C ==
Line 36:
== Using crti.o, crtbegin.o, crtend.o, and crtn.o in a Kernel ==
In a kernel, you are not using a user-space C library. You may be using a special kernel "C library", or
<source lang="bash">i586-elf-gcc $CFLAGS -print-file-name=crtbegin.o</source>
Line 77:
== x86 (32-bit) ==
It is very simple to implement this under x86. You simply have to define the header of
<source lang="asm">
/*
.section .init
.global _init
Line 99:
<source lang="asm">
/*
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
Line 115:
== x86_64 (64-bit) ==
The system ABI on
<source lang="asm">
/*
.section .init
.global _init
Line 137:
<source lang="asm">
/*
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
|