Linux Kernel Architecture

(Jacob Rumans) #1

Chapter7:Modules


Module sections are not transferred to their final memory position unless theSHF_ALLOCflag is set in
their header.^16 For example, this flag is not set for sections with debugging information (produced when
thegccoption-gis used) because these data need not be present in memory and can be read from the
binary file if needed.

layout_sectionschecks whether the name of a section contains the.initstring. This enables a distinc-
tion to be made between initialization code and regular code; accordingly, the start position of the section
refers to the core or to the init section.

The result oflayout_sectionsis communicated using the following elements:

❑ sh_entsizein the ELF section data structure, of which there is one instance for each section,
indicates the relative position of the section in the core or initialization area. If a section is not to
be loaded, the value is set to~0UL.
To then differentiate between initialization and core sections, theINIT_OFFSET_MASKbit (defined
by(1UL << (BITS_PER_LONG-1)))issetinsh_entsize. This stores the relative position of all init
modules.
❑ core_sizeis used to transfer the total code size that is to reside in the kernel permanently, at
least until the module is unloaded.init_sizetotals the volumes of all sections that are required
for module initialization.

Transferring Data


Now that section distribution in memory is clear, the required memory space is reserved and initialized
with null bytes:

kernel/module.c
/* Do the allocs. */
ptr = module_alloc(mod->core_size);
...
memset(ptr, 0, mod->core_size);
mod->module_core = ptr;

ptr = module_alloc(mod->init_size);
...
memset(ptr, 0, mod->init_size);
mod->module_init = ptr;

module_allocis an architecture-specific function for allocating module memory. In most cases, it is
implemented by directly callingvmallocor one of its variants as described in Chapter 3. In other words,
the module resides in the memory area of the kernel that is mapped via page tables and not directly.

The data of all sections of theSHF_ALLOCtype are then copied to their final memory area using the infor-
mation obtained bylayout_sections;thesh_addrelements of each section are also set to the final
position of the section (previously they pointed to the section position in the temporary module area).

Querying the Module License


Technically insignificant but important from a legal point of view — the module license can now be read
from the.modinfosection and placed in the module data structure:

(^16) This is not quite correct because the kernel also defines a specific order for the various sections on the basis of their flags. However,
I need not discuss this here.

Free download pdf