minor module.c code cleanups modinfo section
PATCH 1/1 minor module.c code cleanups modinfo section
Hi all,
Three patchess. First one is cosmetic changes from a big
if/else/if statement to a find_sec function for modules, and handle_section removal.
Second reintroduces the .modinfo section and puts vermagic, aliases, etc in there (to be supported fully, grab 0.9.10-pre module-init-tools).
Third one is Brian Gerst's patch to put the module structure in .mod.c rather than using linker tricks, with a couple of changes (the module name is converted correctly, and the section is called __module).
Very lightly tested: my test box doesn't stay up for very long under recent kernels 8(
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Name: Tidy Up module.c
Author: Rusty Russell
Status: Experimental
D: This open-codes handle_section, which simply does relocations now
D: Also adds "find_sec" and uses it to find the various sections.
kernel/module.c +44 -120
@@ -76,6 +76,22 @@ int init_module(void) }EXPORT_SYMBOL (init_module); +/* Find a module section: 0 means not found. */ +static unsigned int find_sec (Elf_Ehdr *hdr, +Elf_Shdr *sechdrs, +const char *secstrings, +const char *name) +{ +unsigned int i; + +for (i =1 ; i < hdr->e_shnum; i++) +/* Alloc bit cleared means "ignore it." */ +if ((sechdrs[i].sh_flags &SHF_ALLOC ) + &&strcmp (secstrings+sechdrs[i].sh_name, name) ==0 ) +return i; +return 0 ; +} +/* Find a symbol, return value and the symbol group */ static unsigned long __find_symbol(const char *name, struct kernel_symbol_group **group,
@@ -875,45 +891,6 @@ void *__symbol_get(const char *symbol) }EXPORT_SYMBOL_GPL (__symbol_get); -/* Deal with the given section */ -static int handle_section (const char *name, -Elf_Shdr *sechdrs, -unsigned int strindex, -unsigned int symindex, -unsigned int i, -struct module *mod) -{ -int ret; -const char *strtab = (char *)sechdrs[strindex].sh_addr; - -switch (sechdrs[i].sh_type) { -case SHT_REL : - ret =apply_relocate (sechdrs, strtab, symindex, i, mod); -break ; -case SHT_RELA : - ret =apply_relocate_add (sechdrs, strtab, symindex, i, mod); -break ; -default : -DEBUGP ("Ignoring section %u: %s\n" , i, - sechdrs[i].sh_type==SHT_NULL ?"NULL" : - sechdrs[i].sh_type==SHT_PROGBITS ?"PROGBITS" : - sechdrs[i].sh_type==SHT_SYMTAB ?"SYMTAB" : - sechdrs[i].sh_type==SHT_STRTAB ?"STRTAB" : - sechdrs[i].sh_type==SHT_RELA ?"RELA" : - sechdrs[i].sh_type==SHT_HASH ?"HASH" : - sechdrs[i].sh_type==SHT_DYNAMIC ?"DYNAMIC" : - sechdrs[i].sh_type==SHT_NOTE ?"NOTE" : - sechdrs[i].sh_type==SHT_NOBITS ?"NOBITS" : - sechdrs[i].sh_type==SHT_REL ?"REL" : - sechdrs[i].sh_type==SHT_SHLIB ?"SHLIB" : - sechdrs[i].sh_type==SHT_DYNSYM ?"DYNSYM" : - sechdrs[i].sh_type==SHT_NUM ?"NUM" : -"UNKNOWN" ); - ret =0 ; - } -return ret; -} -/* Change all symbols so that sh_value encodes the pointer directly. */ static int simplify_symbols(Elf_Shdr *sechdrs, unsigned int symindex,
@@ -1103,93 +1080,16 @@ static struct module *load_module(void * sechdrs = (void *)hdr + hdr->e_shoff; secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; -/* May not export symbols, or have setup params, so these may -not exist */ - exportindex = setupindex = obsparmindex = gplindex = licenseindex - = crcindex = gplcrcindex = versindex =0 ; - -/* And these should exist, but gcc whinges if we don't init them */ - symindex = strindex = exindex = modindex = vmagindex =0 ; - -/* Find where important sections are */ for (i =1 ; i < hdr->e_shnum; i++) {/* Mark all sections sh_addr with their address in the temporary image. */ sechdrs[i].sh_addr = (size_t )hdr + sechdrs[i].sh_offset; +/* Internal symbols and strings. */ if (sechdrs[i].sh_type ==SHT_SYMTAB ) { -/* Internal symbols */ -DEBUGP ("Symbol table in section %u\n" , i); symindex = i; -/* Strings */ strindex = sechdrs[i].sh_link; -DEBUGP ("String table found in section %u\n" , strindex); - }else if (strcmp (secstrings+sechdrs[i].sh_name, -".gnu.linkonce.this_module" ) ==0 ) { -/* The module struct */ -DEBUGP ("Module in section %u\n" , i); - modindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__ksymtab" ) - ==0 ) { -/* Exported symbols. */ -DEBUGP ("EXPORT table in section %u\n" , i); - exportindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name, -"__ksymtab_gpl" ) ==0 ) { -/* Exported symbols. (GPL) */ -DEBUGP ("GPL symbols found in section %u\n" , i); - gplindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__kcrctab" ) - ==0 ) { -/* Exported symbols CRCs. */ -DEBUGP ("CRC table in section %u\n" , i); - crcindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__kcrctab_gpl" ) - ==0 ) { -/* Exported symbols CRCs. (GPL)*/ -DEBUGP ("CRC table in section %u\n" , i); - gplcrcindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__param" ) - ==0 ) { -/* Setup parameter info */ -DEBUGP ("Setup table found in section %u\n" , i); - setupindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__ex_table" ) - ==0 ) { -/* Exception table */ -DEBUGP ("Exception table found in section %u\n" , i); - exindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,"__obsparm" ) - ==0 ) { -/* Obsolete MODULE_PARM() table */ -DEBUGP ("Obsolete param found in section %u\n" , i); - obsparmindex = i; - }else if (strcmp (secstrings+sechdrs[i].sh_name,".init.license" ) - ==0 ) { -/* MODULE_LICENSE() */ -DEBUGP ("Licence found in section %u\n" , i); - licenseindex = i; - sechdrs[i].sh_flags &= ~(unsigned long )SHF_ALLOC ; - }else if (strcmp (secstrings+sechdrs[i].sh_name, -"__vermagic" ) ==0 && - (sechdrs[i].sh_flags &SHF_ALLOC )) { -/* Version magic. */ -DEBUGP ("Version magic found in section %u\n" , i); - vmagindex = i; - sechdrs[i].sh_flags &= ~(unsigned long )SHF_ALLOC ; - }else if (strcmp (secstrings+sechdrs[i].sh_name, -"__versions" ) ==0 && - (sechdrs[i].sh_flags &SHF_ALLOC )) { -/* Module version info (both exported and needed) */ -DEBUGP ("Versions found in section %u\n" , i); - versindex = i; - sechdrs[i].sh_flags &= ~(unsigned long )SHF_ALLOC ; } -#ifdef CONFIG_KALLSYMS -/* symbol and string tables for decoding later. */ -if (sechdrs[i].sh_type ==SHT_SYMTAB || i == strindex) - sechdrs[i].sh_flags |=SHF_ALLOC ; -#endif #ifndef CONFIG_MODULE_UNLOAD /* Don't load .exit sections */ if (strstr (secstrings+sechdrs[i].sh_name,".exit" ))
@@ -1197,6 +1097,14 @@ static struct module *load_module(void *#endif } +#ifdef CONFIG_KALLSYMS +/* Keep symbol and string tables for decoding later. */ + sechdrs[symindex].sh_flags |=SHF_ALLOC ; + sechdrs[strindex].sh_flags |=SHF_ALLOC ; +#endif + + modindex =find_sec (hdr, sechdrs, secstrings, +".gnu.linkonce.this_module" );if (!modindex) {printk (KERN_WARNING "No module found in object\n" ); err = -ENOEXEC ;
@@ -1204,6 +1112,18 @@ static struct module *load_module(void * } mod = (void *)sechdrs[modindex].sh_addr; +/* Optional sections */ + exportindex =find_sec (hdr, sechdrs, secstrings,"__ksymtab" ); + gplindex =find_sec (hdr, sechdrs, secstrings,"__ksymtab_gpl" ); + crcindex =find_sec (hdr, sechdrs, secstrings,"__kcrctab" ); + gplcrcindex =find_sec (hdr, sechdrs, secstrings,"__kcrctab_gpl" ); + setupindex =find_sec (hdr, sechdrs, secstrings,"__param" ); + exindex =find_sec (hdr, sechdrs, secstrings,"__ex_table" ); + obsparmindex =find_sec (hdr, sechdrs, secstrings,"__obsparm" ); + licenseindex =find_sec (hdr, sechdrs, secstrings,".init.license" ); + vmagindex =find_sec (hdr, sechdrs, secstrings,"__vermagic" ); + versindex =find_sec (hdr, sechdrs, secstrings,"__versions" ); +/* Check module struct version now, before we try to use module. */ if (!check_modstruct_version (sechdrs, versindex, mod)) { err = -ENOEXEC ;
@@ -1336,10 +1256,14 @@ static struct module *load_module(void * mod->extable.entry = (void *)sechdrs[exindex].sh_addr; } -/* Now handle each section. */ +/* Now do relocations. */ for (i =1 ; i < hdr->e_shnum; i++) { - err =handle_section (secstrings + sechdrs[i].sh_name, - sechdrs, strindex, symindex, i, mod); +if (sechdrs[i].sh_type ==SHT_REL ) + err =apply_relocate (sechdrs, strtab, symindex, i, + mod); +else if (sechdrs[i].sh_type ==SHT_RELA ) + err =apply_relocate_add (sechdrs, strtab, symindex, i, + mod);if (err <0 ) goto cleanup; }
Name: Restore modinfo section
Author: Rusty Russell
Status: Tested on 2.5.62-bk2
Depends: Module/tidy.patch.gz
D: Restores .modinfo section, and uses it to store license and vermagic.
include/linux/module.h +25 -19
72 lines not on this page · raw message
kernel/module.c +60 -27
166 lines not on this page · raw message
scripts/modpost.c +1 -3
@@ -386,9 +386,7 @@ add_header(struct buffer *b)buf_printf (b,"#include <linux/vermagic.h>\n" );buf_printf (b,"#include <linux/compiler.h>\n" );buf_printf (b,"\n" ); -buf_printf (b,"const char vermagic[]\n" ); -buf_printf (b,"__attribute__((section(\"__vermagic\"))) =\n" ); -buf_printf (b,"VERMAGIC_STRING;\n" ); +buf_printf (b,"MODULE_INFO(vermagic, VERMAGIC_STRING);\n" ); }/* Record CRCs for unresolved symbols */
Name: Move module structure out of header
Author: Brian Gerst <[email protected]>
Status: Experimental
Depends: Module/modinfo.patch.gz
D: This patch moves the module structure to the generated .mod.c file,
D: instead of compiling it into each object and relying on the linker to
D: include it only once.
D:
D: Tweaked by Rusty.
arch/v850/Makefile +0 -5
@@ -22,11 +22,6 @@ CFLAGS += -ffixed-r16 -mno-prolog-functi CFLAGS += -fno-builtin CFLAGS += -D__linux__ -DUTS_SYSNAME=\"uClinux\" -# This prevents the linker from consolidating the .gnu.linkonce.this_module -# section into .text (which the v850 default linker script for -r does for -# some reason) -LDFLAGS_MODULE += --unique=.gnu.linkonce.this_module - LDFLAGS_BLOB := -b binary --oformat elf32-little OBJCOPY_FLAGS_BLOB := -I binary -O elf32-little -B v850e
include/linux/module.h +0 -13
@@ -412,19 +412,6 @@ static inline const char *module_address#ifdef MODULE extern struct module __this_module; -#ifdef KBUILD_MODNAME -/* We make the linker do some of the work. */ -struct module __this_module -__attribute__((section(".gnu.linkonce.this_module"))) = { - .name = __stringify(KBUILD_MODNAME), - .symbols = { .owner = &__this_module }, - .gpl_symbols = { .owner = &__this_module, .gplonly = 1 }, - .init = init_module, -#ifdef CONFIG_MODULE_UNLOAD - .exit = cleanup_module, -#endif -}; -#endif /* KBUILD_MODNAME */ #endif /* MODULE */ #define symbol_request (x) try_then_request_module(symbol_get(x), "symbol:" #x)
kernel/module.c +1 -2
@@ -1146,8 +1129,7 @@ static struct module *load_module(void *#endif } - modindex =find_sec (hdr, sechdrs, secstrings, -".gnu.linkonce.this_module" ); + modindex =find_sec (hdr, sechdrs, secstrings,"__module" );if (!modindex) {printk (KERN_WARNING "No module found in object\n" ); err = -ENOEXEC ;
scripts/modpost.c +42 -4
78 lines not on this page · raw message