Linux Kernel Architecture

(Jacob Rumans) #1

Chapter7:Modules


temporary image. */
sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;

/* Internal symbols and strings. */
if (sechdrs[i].sh_type == SHT_SYMTAB) {
symindex = i;
strindex = sechdrs[i].sh_link;
strtab = (char *)hdr + sechdrs[strindex].sh_offset;
}
}

Iteration through all sections is used to find the position of the symbol table (the only section whose type
isSHT_SYMTAB) and of the associated symbol string table whose section is linked with the symbol table
using the ELF link feature.

Finding Section Addresses


In section.gnu.linkonce.this_module, there is an instance ofstruct module(find_secis an auxiliary
function that finds the index of an ELF section by reference to its name):

module/kernel.c
modindex = find_sec(hdr, sechdrs, secstrings,
".gnu.linkonce.this_module");
...
mod = (void *)sechdrs[modindex].sh_addr;

modnow points to an instance ofstruct modulein which the name and the pointers to the initialization
and clean-up functions are supplied but whose remaining elements are still initialized withNULLor 0.

find_secis also used to find the index positions of the remaining module sections (they are held in the
sectionindexvariable defined above):

kernel/module.c
/* Optional sections */
exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
...
versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);

The module loader then calls the architecture-specific functionmod_frob_arch_sectionsused by some
architectures to manipulate the contents of the individual sections. Because this is not usually needed
(the function is defined accordingly as ano-operation), it is not discussed here.

Organizing Data in Memory


layout_sectionsis used to decide which sections of the modulearetobeloadedatwhichpositionsin
memory or which modules must be copied from their temporary address. The sections are split into two
parts:coreandinit. While the first contains all code sections required during the entire run time of the
module, the kernel places all initialization data and functions in a separate part that is removed when
loading is completed.
Free download pdf