Linux Kernel Architecture

(Jacob Rumans) #1

Chapter7:Modules


{
Elf_Ehdr *hdr;
Elf_Shdr *sechdrs;
char *secstrings, *args, *modmagic, *strtab = NULL;
unsigned int i;
unsigned int symindex = 0;
unsigned int strindex = 0;
unsigned int setupindex;
unsigned int exindex;
unsigned int exportindex;
unsigned int modindex;
unsigned int obsparmindex;
unsigned int infoindex;
unsigned int gplindex;
unsigned int crcindex;
unsigned int gplcrcindex;
...
struct module *mod;
long err = 0;
...
if (copy_from_user(hdr, umod, len) != 0) {
err = -EFAULT;
goto free_hdr;
}
...
/* Convenience variables */
sechdrs = (void *)hdr + hdr->e_shoff;
secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;

Once a large number of variables has been defined, the kernel loads the module binary data into kernel
memory usingcopy_from_user(I have dispensed with some index variables for ELF sections and infor-
mation on error handling — and will do so in the following sections — so as not to add unnecessarily to
the volume of this description).

hdrthen points to the start address of the binary data,in other words, to the ELF header of the module.

sechdrsandsecstringare set so that they point to the positions in memory where information on the
existing ELF sections and the string table with the section names is located. Therelativevalue in the ELF
header is added to theabsoluteaddress of the module in the kernel address space to determine the correct
position (we will come across this procedure frequently).

Rewriting Section Addresses


The addresses of all sections in the binary code are then rewritten into absolute values in the temporary
image^15 :

kernel/module.c
for (i = 1; i < hdr->e_shnum; i++) {
...
/* Mark all sections sh_addr with their address in the

(^15) e_shnumindicates the number of sections,sh_addris the address of a section, andsh_offsetis the identifier of the section
in the section table as described in detail in Appendix E.

Free download pdf