Linux Kernel Architecture

(Jacob Rumans) #1

Chapter7:Modules


Some of the above sections cannot be generated untilthe module itself and possibly all other kernel
modules have been compiled, for example, the section that lists all module dependencies. Because no
explicit dependency information is given in the source code, the kernel must get this information by
analyzing non-resolved references of the module involved as well as the exported symbols of all other
modules.

A multistep strategy is therefore adopted for generating modules:


  1. First, all C files in the source code of a module are compiled into normal.oobject files.

  2. Once the object files have been generated for all modules, the kernel can analyze them. The
    additional information found (e.g., on module dependencies) is stored in a separate file that
    is also compiled into a binary file.

  3. The two binary files are linked and thus produce the final module.


Appendix B describes the kernel build process in detail and deals with problems encountered when
compiling modules.

Standard Functions


The module initialization and clean-up functions are stored in themoduleinstance in the
.gnu.linkonce.modulesection. The instance is located in the abovementioned autogenerated
extra file for each module. It is defined as follows^12 :

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
.arch = MODULE_ARCH_INIT,
};

KBUILD_MODNAMEcontains 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_moduleobject is generated because no
post-processing of the module object is performed.MODULE_ARCH_INITis a pre-processor symbol that can
point to architecture-specific initialization methods for modules. This is currently only required for m68k
CPUs.

Themodule_initandmodule_exitmacros ininit.h>are used to define the init and exit functions.^13
Each module includes code of the following kind that defines the init/exit functions^14 :

(^12) Theattributedirective 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.
(^13) The macros defineinit_moduleandexit_modulefunctions 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; how-
ever, programmers can choose whichever names they want.
(^14) If the code is not compiled as a module,module_initandmodule_exitconvert the functions into regular init/exit calls.

Free download pdf