Initialization and Cleanup Functions
The module initialization and clean-up functions are stored in the module instance in the .gnu.linkonce.module section. The instance is located in the abovementioned autogenerated extra file for each module. It is defined as follows12:
module module.mod.c struct module _this_module
_attribute_((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME, .init = init_module, #ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module, #endif
kbuild_modname contains the name of the module and is only defined if the code is compiled as a module.
If the code is to be permanently bound into the kernel, no_this_module object is generated because no post-processing of the module object is performed. module_arch_init is a pre-processor symbol that can point to architecture-specific initialization methods for modules. This is currently only required for m68k CPUs.
The module_init and module_exit macros in init.h> are used to define the init and exit functions.13 Each module includes code of the following kind that defines the init/exit functions14:
12The attribute directive of the GNU C compiler is used to place the data in the desired section. Other uses of this directive are described in Appendix C.
13The macros define init_module and exit_module functions that are created as aliases — a GCC enhancement — for the actual initialization and clean-up functions. This trick enables the kernel to always use the same names to refer to the functions; however, programmers can choose whichever names they want.
14If the code is not compiled as a module, module_init and module_exit convert the functions into regular init/exit calls.
#ifdef MODULE
static int _init xyz_init(void) {
/* Initialization code */
static void _exit xyz_cleanup (void) {
module_init(xyz_init); module_exit(xyz_exit); #endif
The_init and_exit prefixes help place the two functions in the right sections of the binary code:
#define _init _attribute_ ((_section_ (".init.text"))) _cold
#define _initdata _attribute_ ((_section_ (".init.data")))
#define _exitdata _attribute_ ((_section_(".exit.data")))
#define _exit_call _attribute_used_ _attribute_ ((_section_ (".exitcall.exit")
The data variants are used to place data (in contrast to functions) in the .init and .exit section.
Continue reading here: Exporting Symbols
Was this article helpful?