1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006 Sam Ravnborg
6 * Based in part on module-init-tools/depmod.c,file2alias
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * Usage: modpost vmlinux module1.o module2.o ...
16 #include "../../include/linux/license.h"
18 /* Are we using CONFIG_MODVERSIONS? */
20 /* Warn about undefined symbols? (do so if we have vmlinux) */
22 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
23 static int all_versions = 0;
24 /* If we are modposting external module set to 1 */
25 static int external_module = 0;
26 /* Only warn about unresolved symbols */
27 static int warn_unresolved = 0;
28 /* How a symbol is exported */
30 export_plain, export_unused, export_gpl,
31 export_unused_gpl, export_gpl_future, export_unknown
34 void fatal(const char *fmt, ...)
38 fprintf(stderr, "FATAL: ");
40 va_start(arglist, fmt);
41 vfprintf(stderr, fmt, arglist);
47 void warn(const char *fmt, ...)
51 fprintf(stderr, "WARNING: ");
53 va_start(arglist, fmt);
54 vfprintf(stderr, fmt, arglist);
58 static int is_vmlinux(const char *modname)
62 if ((myname = strrchr(modname, '/')))
67 return strcmp(myname, "vmlinux") == 0;
70 void *do_nofail(void *ptr, const char *expr)
73 fatal("modpost: Memory allocation failure: %s.\n", expr);
78 /* A list of all modules we processed */
80 static struct module *modules;
82 static struct module *find_module(char *modname)
86 for (mod = modules; mod; mod = mod->next)
87 if (strcmp(mod->name, modname) == 0)
92 static struct module *new_module(char *modname)
97 mod = NOFAIL(malloc(sizeof(*mod)));
98 memset(mod, 0, sizeof(*mod));
99 p = NOFAIL(strdup(modname));
101 /* strip trailing .o */
102 if ((s = strrchr(p, '.')) != NULL)
103 if (strcmp(s, ".o") == 0)
108 mod->gpl_compatible = -1;
115 /* A hash of all exported symbols,
116 * struct symbol is also used for lists of unresolved symbols */
118 #define SYMBOL_HASH_SIZE 1024
122 struct module *module;
126 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
127 unsigned int kernel:1; /* 1 if symbol is from kernel
128 * (only for external modules) **/
129 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
130 enum export export; /* Type of export */
134 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
136 /* This is based on the hash agorithm from gdbm, via tdb */
137 static inline unsigned int tdb_hash(const char *name)
139 unsigned value; /* Used to compute the hash value. */
140 unsigned i; /* Used to cycle through random values. */
142 /* Set the initial value from the key size. */
143 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
144 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
146 return (1103515243 * value + 12345);
150 * Allocate a new symbols for use in the hash of exported symbols or
151 * the list of unresolved symbols per module
153 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
156 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
158 memset(s, 0, sizeof(*s));
159 strcpy(s->name, name);
165 /* For the hash of exported symbols */
166 static struct symbol *new_symbol(const char *name, struct module *module,
172 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
173 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
174 new->module = module;
175 new->export = export;
179 static struct symbol *find_symbol(const char *name)
183 /* For our purposes, .foo matches foo. PPC64 needs this. */
187 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
188 if (strcmp(s->name, name) == 0)
198 { .str = "EXPORT_SYMBOL", .export = export_plain },
199 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
200 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
201 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
202 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
203 { .str = "(unknown)", .export = export_unknown },
207 static const char *export_str(enum export ex)
209 return export_list[ex].str;
212 static enum export export_no(const char * s)
216 return export_unknown;
217 for (i = 0; export_list[i].export != export_unknown; i++) {
218 if (strcmp(export_list[i].str, s) == 0)
219 return export_list[i].export;
221 return export_unknown;
224 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
226 if (sec == elf->export_sec)
228 else if (sec == elf->export_unused_sec)
229 return export_unused;
230 else if (sec == elf->export_gpl_sec)
232 else if (sec == elf->export_unused_gpl_sec)
233 return export_unused_gpl;
234 else if (sec == elf->export_gpl_future_sec)
235 return export_gpl_future;
237 return export_unknown;
241 * Add an exported symbol - it may have already been added without a
242 * CRC, in this case just update the CRC
244 static struct symbol *sym_add_exported(const char *name, struct module *mod,
247 struct symbol *s = find_symbol(name);
250 s = new_symbol(name, mod, export);
253 warn("%s: '%s' exported twice. Previous export "
254 "was in %s%s\n", mod->name, name,
256 is_vmlinux(s->module->name) ?"":".ko");
260 s->vmlinux = is_vmlinux(mod->name);
266 static void sym_update_crc(const char *name, struct module *mod,
267 unsigned int crc, enum export export)
269 struct symbol *s = find_symbol(name);
272 s = new_symbol(name, mod, export);
277 void *grab_file(const char *filename, unsigned long *size)
283 fd = open(filename, O_RDONLY);
284 if (fd < 0 || fstat(fd, &st) != 0)
288 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
291 if (map == MAP_FAILED)
297 * Return a copy of the next line in a mmap'ed file.
298 * spaces in the beginning of the line is trimmed away.
299 * Return a pointer to a static buffer.
301 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
303 static char line[4096];
306 signed char *p = (signed char *)file + *pos;
309 for (; *pos < size ; (*pos)++)
311 if (skip && isspace(*p)) {
316 if (*p != '\n' && (*pos < size)) {
320 break; /* Too long, stop */
331 void release_file(void *file, unsigned long size)
336 static int parse_elf(struct elf_info *info, const char *filename)
343 hdr = grab_file(filename, &info->size);
349 if (info->size < sizeof(*hdr)) {
350 /* file too small, assume this is an empty .o file */
353 /* Is this a valid ELF file? */
354 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
355 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
356 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
357 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
358 /* Not an ELF file - silently ignore it */
361 /* Fix endianness in ELF header */
362 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
363 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
364 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
365 hdr->e_machine = TO_NATIVE(hdr->e_machine);
366 sechdrs = (void *)hdr + hdr->e_shoff;
367 info->sechdrs = sechdrs;
369 /* Fix endianness in section headers */
370 for (i = 0; i < hdr->e_shnum; i++) {
371 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
372 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
373 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
374 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
375 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
377 /* Find symbol table. */
378 for (i = 1; i < hdr->e_shnum; i++) {
379 const char *secstrings
380 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
383 if (sechdrs[i].sh_offset > info->size) {
384 fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
387 secname = secstrings + sechdrs[i].sh_name;
388 if (strcmp(secname, ".modinfo") == 0) {
389 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
390 info->modinfo_len = sechdrs[i].sh_size;
391 } else if (strcmp(secname, "__ksymtab") == 0)
392 info->export_sec = i;
393 else if (strcmp(secname, "__ksymtab_unused") == 0)
394 info->export_unused_sec = i;
395 else if (strcmp(secname, "__ksymtab_gpl") == 0)
396 info->export_gpl_sec = i;
397 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
398 info->export_unused_gpl_sec = i;
399 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
400 info->export_gpl_future_sec = i;
402 if (sechdrs[i].sh_type != SHT_SYMTAB)
405 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
406 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
407 + sechdrs[i].sh_size;
408 info->strtab = (void *)hdr +
409 sechdrs[sechdrs[i].sh_link].sh_offset;
411 if (!info->symtab_start) {
412 fatal("%s has no symtab?\n", filename);
414 /* Fix endianness in symbols */
415 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
416 sym->st_shndx = TO_NATIVE(sym->st_shndx);
417 sym->st_name = TO_NATIVE(sym->st_name);
418 sym->st_value = TO_NATIVE(sym->st_value);
419 sym->st_size = TO_NATIVE(sym->st_size);
424 static void parse_elf_finish(struct elf_info *info)
426 release_file(info->hdr, info->size);
429 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
430 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
432 static void handle_modversions(struct module *mod, struct elf_info *info,
433 Elf_Sym *sym, const char *symname)
436 enum export export = export_from_sec(info, sym->st_shndx);
438 switch (sym->st_shndx) {
440 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
444 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
445 crc = (unsigned int) sym->st_value;
446 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
451 /* undefined symbol */
452 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
453 ELF_ST_BIND(sym->st_info) != STB_WEAK)
455 /* ignore global offset table */
456 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
458 /* ignore __this_module, it will be resolved shortly */
459 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
461 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
462 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
463 /* add compatibility with older glibc */
464 #ifndef STT_SPARC_REGISTER
465 #define STT_SPARC_REGISTER STT_REGISTER
467 if (info->hdr->e_machine == EM_SPARC ||
468 info->hdr->e_machine == EM_SPARCV9) {
469 /* Ignore register directives. */
470 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
472 if (symname[0] == '.') {
473 char *munged = strdup(symname);
475 munged[1] = toupper(munged[1]);
481 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
482 strlen(MODULE_SYMBOL_PREFIX)) == 0)
483 mod->unres = alloc_symbol(symname +
484 strlen(MODULE_SYMBOL_PREFIX),
485 ELF_ST_BIND(sym->st_info) == STB_WEAK,
489 /* All exported symbols */
490 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
491 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
494 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
496 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
497 mod->has_cleanup = 1;
503 * Parse tag=value strings from .modinfo section
505 static char *next_string(char *string, unsigned long *secsize)
507 /* Skip non-zero chars */
510 if ((*secsize)-- <= 1)
514 /* Skip any zero padding. */
517 if ((*secsize)-- <= 1)
523 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
524 const char *tag, char *info)
527 unsigned int taglen = strlen(tag);
528 unsigned long size = modinfo_len;
531 size -= info - (char *)modinfo;
532 modinfo = next_string(info, &size);
535 for (p = modinfo; p; p = next_string(p, &size)) {
536 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
537 return p + taglen + 1;
542 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
546 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
550 * Test if string s ends in string sub
553 static int strrcmp(const char *s, const char *sub)
561 sublen = strlen(sub);
563 if ((slen == 0) || (sublen == 0))
569 return memcmp(s + slen - sublen, sub, sublen);
573 * Whitelist to allow certain references to pass with no warning.
575 * If a module parameter is declared __initdata and permissions=0
576 * then this is legal despite the warning generated.
577 * We cannot see value of permissions here, so just ignore
579 * The pattern is identified by:
585 * Many drivers utilise a *driver container with references to
586 * add, remove, probe functions etc.
587 * These functions may often be marked __init and we do not want to
589 * the pattern is identified by:
590 * tosec = .init.text | .exit.text | .init.data
592 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console
595 * Some symbols belong to init section but still it is ok to reference
596 * these from non-init sections as these symbols don't have any memory
597 * allocated for them and symbol address and value are same. So even
598 * if init section is freed, its ok to reference those symbols.
599 * For ex. symbols marking the init section boundaries.
600 * This pattern is identified by
601 * refsymname = __init_begin, _sinittext, _einittext
603 * During the early init phase we have references from .init.text to
604 * .text we have an intended section mismatch - do not warn about it.
605 * See kernel_init() in init/main.c
608 * atsym = kernel_init
609 * Some symbols belong to init section but still it is ok to reference
611 static int secref_whitelist(const char *modname, const char *tosec,
612 const char *fromsec, const char *atsym,
613 const char *refsymname)
617 const char *pat2sym[] = {
619 "_template", /* scsi uses *_template a lot */
620 "_sht", /* scsi also used *_sht to some extent */
628 const char *pat3refsym[] = {
635 /* Check for pattern 1 */
636 if (strcmp(tosec, ".init.data") != 0)
638 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
640 if (strncmp(atsym, "__param", strlen("__param")) != 0)
646 /* Check for pattern 2 */
647 if ((strcmp(tosec, ".init.text") != 0) &&
648 (strcmp(tosec, ".exit.text") != 0) &&
649 (strcmp(tosec, ".init.data") != 0))
651 if (strcmp(fromsec, ".data") != 0)
654 for (s = pat2sym; *s; s++)
655 if (strrcmp(atsym, *s) == 0)
660 /* Whitelist all references from .pci_fixup section if vmlinux
661 * Whitelist all refereces from .text.head to .init.data if vmlinux
662 * Whitelist all refereces from .text.head to .init.text if vmlinux
664 if (is_vmlinux(modname)) {
665 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
666 (strcmp(tosec, ".init.text") == 0))
669 if ((strcmp(fromsec, ".text.head") == 0) &&
670 ((strcmp(tosec, ".init.data") == 0) ||
671 (strcmp(tosec, ".init.text") == 0)))
674 /* Check for pattern 3 */
675 for (s = pat3refsym; *s; s++)
676 if (strcmp(refsymname, *s) == 0)
679 /* Check for pattern 4 */
680 if ((strcmp(tosec, ".init.text") == 0) &&
681 (strcmp(fromsec, ".text") == 0) &&
682 (strcmp(refsymname, "kernel_init") == 0))
688 * Find symbol based on relocation record info.
689 * In some cases the symbol supplied is a valid symbol so
690 * return refsym. If st_name != 0 we assume this is a valid symbol.
691 * In other cases the symbol needs to be looked up in the symbol table
692 * based on section and address.
694 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
699 if (relsym->st_name != 0)
701 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
702 if (sym->st_shndx != relsym->st_shndx)
704 if (sym->st_value == addr)
710 static inline int is_arm_mapping_symbol(const char *str)
712 return str[0] == '$' && strchr("atd", str[1])
713 && (str[2] == '\0' || str[2] == '.');
717 * If there's no name there, ignore it; likewise, ignore it if it's
718 * one of the magic symbols emitted used by current ARM tools.
720 * Otherwise if find_symbols_between() returns those symbols, they'll
721 * fail the whitelist tests and cause lots of false alarms ... fixable
722 * only by merging __exit and __init sections into __text, bloating
723 * the kernel (which is especially evil on embedded platforms).
725 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
727 const char *name = elf->strtab + sym->st_name;
729 if (!name || !strlen(name))
731 return !is_arm_mapping_symbol(name);
735 * Find symbols before or equal addr and after addr - in the section sec.
736 * If we find two symbols with equal offset prefer one with a valid name.
737 * The ELF format may have a better way to detect what type of symbol
738 * it is, but this works for now.
740 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
742 Elf_Sym **before, Elf_Sym **after)
745 Elf_Ehdr *hdr = elf->hdr;
746 Elf_Addr beforediff = ~0;
747 Elf_Addr afterdiff = ~0;
748 const char *secstrings = (void *)hdr +
749 elf->sechdrs[hdr->e_shstrndx].sh_offset;
754 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
757 if (sym->st_shndx >= SHN_LORESERVE)
759 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
760 if (strcmp(symsec, sec) != 0)
762 if (!is_valid_name(elf, sym))
764 if (sym->st_value <= addr) {
765 if ((addr - sym->st_value) < beforediff) {
766 beforediff = addr - sym->st_value;
769 else if ((addr - sym->st_value) == beforediff) {
775 if ((sym->st_value - addr) < afterdiff) {
776 afterdiff = sym->st_value - addr;
779 else if ((sym->st_value - addr) == afterdiff) {
787 * Print a warning about a section mismatch.
788 * Try to find symbols near it so user can find it.
789 * Check whitelist before warning - it may be a false positive.
791 static void warn_sec_mismatch(const char *modname, const char *fromsec,
792 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
794 const char *refsymname = "";
795 Elf_Sym *before, *after;
797 Elf_Ehdr *hdr = elf->hdr;
798 Elf_Shdr *sechdrs = elf->sechdrs;
799 const char *secstrings = (void *)hdr +
800 sechdrs[hdr->e_shstrndx].sh_offset;
801 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
803 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
805 refsym = find_elf_symbol(elf, r.r_addend, sym);
806 if (refsym && strlen(elf->strtab + refsym->st_name))
807 refsymname = elf->strtab + refsym->st_name;
809 /* check whitelist - we may ignore it */
811 secref_whitelist(modname, secname, fromsec,
812 elf->strtab + before->st_name, refsymname))
815 if (before && after) {
816 warn("%s - Section mismatch: reference to %s:%s from %s "
817 "between '%s' (at offset 0x%llx) and '%s'\n",
818 modname, secname, refsymname, fromsec,
819 elf->strtab + before->st_name,
820 (long long)r.r_offset,
821 elf->strtab + after->st_name);
823 warn("%s - Section mismatch: reference to %s:%s from %s "
824 "after '%s' (at offset 0x%llx)\n",
825 modname, secname, refsymname, fromsec,
826 elf->strtab + before->st_name,
827 (long long)r.r_offset);
829 warn("%s - Section mismatch: reference to %s:%s from %s "
830 "before '%s' (at offset -0x%llx)\n",
831 modname, secname, refsymname, fromsec,
832 elf->strtab + after->st_name,
833 (long long)r.r_offset);
835 warn("%s - Section mismatch: reference to %s:%s from %s "
837 modname, secname, fromsec, refsymname,
838 (long long)r.r_offset);
843 * A module includes a number of sections that are discarded
844 * either when loaded or when used as built-in.
845 * For loaded modules all functions marked __init and all data
846 * marked __initdata will be discarded when the module has been intialized.
847 * Likewise for modules used built-in the sections marked __exit
848 * are discarded because __exit marked function are supposed to be called
849 * only when a moduel is unloaded which never happes for built-in modules.
850 * The check_sec_ref() function traverses all relocation records
851 * to find all references to a section that reference a section that will
852 * be discarded and warns about it.
854 static void check_sec_ref(struct module *mod, const char *modname,
855 struct elf_info *elf,
856 int section(const char*),
857 int section_ref_ok(const char *))
861 Elf_Ehdr *hdr = elf->hdr;
862 Elf_Shdr *sechdrs = elf->sechdrs;
863 const char *secstrings = (void *)hdr +
864 sechdrs[hdr->e_shstrndx].sh_offset;
866 /* Walk through all sections */
867 for (i = 0; i < hdr->e_shnum; i++) {
868 const char *name = secstrings + sechdrs[i].sh_name;
872 /* We want to process only relocation sections and not .init */
873 if (sechdrs[i].sh_type == SHT_RELA) {
875 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
876 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
877 name += strlen(".rela");
878 if (section_ref_ok(name))
881 for (rela = start; rela < stop; rela++) {
882 r.r_offset = TO_NATIVE(rela->r_offset);
883 #if KERNEL_ELFCLASS == ELFCLASS64
884 if (hdr->e_machine == EM_MIPS) {
885 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
886 r_sym = TO_NATIVE(r_sym);
888 r.r_info = TO_NATIVE(rela->r_info);
889 r_sym = ELF_R_SYM(r.r_info);
892 r.r_info = TO_NATIVE(rela->r_info);
893 r_sym = ELF_R_SYM(r.r_info);
895 r.r_addend = TO_NATIVE(rela->r_addend);
896 sym = elf->symtab_start + r_sym;
897 /* Skip special sections */
898 if (sym->st_shndx >= SHN_LORESERVE)
901 secname = secstrings +
902 sechdrs[sym->st_shndx].sh_name;
903 if (section(secname))
904 warn_sec_mismatch(modname, name,
907 } else if (sechdrs[i].sh_type == SHT_REL) {
909 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
910 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
911 name += strlen(".rel");
912 if (section_ref_ok(name))
915 for (rel = start; rel < stop; rel++) {
916 r.r_offset = TO_NATIVE(rel->r_offset);
917 #if KERNEL_ELFCLASS == ELFCLASS64
918 if (hdr->e_machine == EM_MIPS) {
919 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
920 r_sym = TO_NATIVE(r_sym);
922 r.r_info = TO_NATIVE(rel->r_info);
923 r_sym = ELF_R_SYM(r.r_info);
926 r.r_info = TO_NATIVE(rel->r_info);
927 r_sym = ELF_R_SYM(r.r_info);
930 sym = elf->symtab_start + r_sym;
931 /* Skip special sections */
932 if (sym->st_shndx >= SHN_LORESERVE)
935 secname = secstrings +
936 sechdrs[sym->st_shndx].sh_name;
937 if (section(secname))
938 warn_sec_mismatch(modname, name,
946 * Functions used only during module init is marked __init and is stored in
947 * a .init.text section. Likewise data is marked __initdata and stored in
948 * a .init.data section.
949 * If this section is one of these sections return 1
950 * See include/linux/init.h for the details
952 static int init_section(const char *name)
954 if (strcmp(name, ".init") == 0)
956 if (strncmp(name, ".init.", strlen(".init.")) == 0)
962 * Identify sections from which references to a .init section is OK.
964 * Unfortunately references to read only data that referenced .init
965 * sections had to be excluded. Almost all of these are false
966 * positives, they are created by gcc. The downside of excluding rodata
967 * is that there really are some user references from rodata to
968 * init code, e.g. drivers/video/vgacon.c:
970 * const struct consw vga_con = {
971 * con_startup: vgacon_startup,
973 * where vgacon_startup is __init. If you want to wade through the false
974 * positives, take out the check for rodata.
976 static int init_section_ref_ok(const char *name)
979 /* Absolute section names */
980 const char *namelist1[] = {
982 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
983 ".toc1", /* used by ppc64 */
985 ".data.rel.ro", /* used by parisc64 */
988 "__bug_table", /* used by powerpc for BUG() */
996 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
997 "__ftr_fixup", /* powerpc cpu feature fixup */
998 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
1001 /* Start of section names */
1002 const char *namelist2[] = {
1007 ".parainstructions",
1011 /* part of section name */
1012 const char *namelist3 [] = {
1013 ".unwind", /* sample: IA_64.unwind.init.text */
1017 for (s = namelist1; *s; s++)
1018 if (strcmp(*s, name) == 0)
1020 for (s = namelist2; *s; s++)
1021 if (strncmp(*s, name, strlen(*s)) == 0)
1023 for (s = namelist3; *s; s++)
1024 if (strstr(name, *s) != NULL)
1026 if (strrcmp(name, ".init") == 0)
1032 * Functions used only during module exit is marked __exit and is stored in
1033 * a .exit.text section. Likewise data is marked __exitdata and stored in
1034 * a .exit.data section.
1035 * If this section is one of these sections return 1
1036 * See include/linux/init.h for the details
1038 static int exit_section(const char *name)
1040 if (strcmp(name, ".exit.text") == 0)
1042 if (strcmp(name, ".exit.data") == 0)
1049 * Identify sections from which references to a .exit section is OK.
1051 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1052 * For our future {in}sanity, add a comment that this is the ppc .opd
1053 * section, not the ia64 .opd section.
1054 * ia64 .opd should not point to discarded sections.
1055 * [.rodata] like for .init.text we ignore .rodata references -same reason
1057 static int exit_section_ref_ok(const char *name)
1060 /* Absolute section names */
1061 const char *namelist1[] = {
1066 ".opd", /* See comment [OPD] */
1067 ".toc1", /* used by ppc64 */
1070 "__bug_table", /* used by powerpc for BUG() */
1073 ".parainstructions",
1078 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
1081 /* Start of section names */
1082 const char *namelist2[] = {
1086 /* part of section name */
1087 const char *namelist3 [] = {
1088 ".unwind", /* Sample: IA_64.unwind.exit.text */
1092 for (s = namelist1; *s; s++)
1093 if (strcmp(*s, name) == 0)
1095 for (s = namelist2; *s; s++)
1096 if (strncmp(*s, name, strlen(*s)) == 0)
1098 for (s = namelist3; *s; s++)
1099 if (strstr(name, *s) != NULL)
1104 static void read_symbols(char *modname)
1106 const char *symname;
1110 struct elf_info info = { };
1113 if (!parse_elf(&info, modname))
1116 mod = new_module(modname);
1118 /* When there's no vmlinux, don't print warnings about
1119 * unresolved symbols (since there'll be too many ;) */
1120 if (is_vmlinux(modname)) {
1125 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1127 if (license_is_gpl_compatible(license))
1128 mod->gpl_compatible = 1;
1130 mod->gpl_compatible = 0;
1133 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1134 "license", license);
1137 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1138 symname = info.strtab + sym->st_name;
1140 handle_modversions(mod, &info, sym, symname);
1141 handle_moddevtable(mod, &info, sym, symname);
1143 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1144 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1146 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1148 maybe_frob_rcs_version(modname, version, info.modinfo,
1149 version - (char *)info.hdr);
1150 if (version || (all_versions && !is_vmlinux(modname)))
1151 get_src_version(modname, mod->srcversion,
1152 sizeof(mod->srcversion)-1);
1154 parse_elf_finish(&info);
1156 /* Our trick to get versioning for struct_module - it's
1157 * never passed as an argument to an exported function, so
1158 * the automatic versioning doesn't pick it up, but it's really
1159 * important anyhow */
1161 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1166 /* We first write the generated file into memory using the
1167 * following helper, then compare to the file on disk and
1168 * only update the later if anything changed */
1170 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1171 const char *fmt, ...)
1178 len = vsnprintf(tmp, SZ, fmt, ap);
1179 buf_write(buf, tmp, len);
1183 void buf_write(struct buffer *buf, const char *s, int len)
1185 if (buf->size - buf->pos < len) {
1186 buf->size += len + SZ;
1187 buf->p = realloc(buf->p, buf->size);
1189 strncpy(buf->p + buf->pos, s, len);
1193 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1195 const char *e = is_vmlinux(m) ?"":".ko";
1199 fatal("modpost: GPL-incompatible module %s%s "
1200 "uses GPL-only symbol '%s'\n", m, e, s);
1202 case export_unused_gpl:
1203 fatal("modpost: GPL-incompatible module %s%s "
1204 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1206 case export_gpl_future:
1207 warn("modpost: GPL-incompatible module %s%s "
1208 "uses future GPL-only symbol '%s'\n", m, e, s);
1212 case export_unknown:
1218 static void check_for_unused(enum export exp, const char* m, const char* s)
1220 const char *e = is_vmlinux(m) ?"":".ko";
1224 case export_unused_gpl:
1225 warn("modpost: module %s%s "
1226 "uses symbol '%s' marked UNUSED\n", m, e, s);
1234 static void check_exports(struct module *mod)
1236 struct symbol *s, *exp;
1238 for (s = mod->unres; s; s = s->next) {
1239 const char *basename;
1240 exp = find_symbol(s->name);
1241 if (!exp || exp->module == mod)
1243 basename = strrchr(mod->name, '/');
1247 basename = mod->name;
1248 if (!mod->gpl_compatible)
1249 check_for_gpl_usage(exp->export, basename, exp->name);
1250 check_for_unused(exp->export, basename, exp->name);
1255 * Header for the generated file
1257 static void add_header(struct buffer *b, struct module *mod)
1259 buf_printf(b, "#include <linux/module.h>\n");
1260 buf_printf(b, "#include <linux/vermagic.h>\n");
1261 buf_printf(b, "#include <linux/compiler.h>\n");
1262 buf_printf(b, "\n");
1263 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1264 buf_printf(b, "\n");
1265 buf_printf(b, "struct module __this_module\n");
1266 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1267 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1269 buf_printf(b, " .init = init_module,\n");
1270 if (mod->has_cleanup)
1271 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1272 " .exit = cleanup_module,\n"
1274 buf_printf(b, "};\n");
1278 * Record CRCs for unresolved symbols
1280 static int add_versions(struct buffer *b, struct module *mod)
1282 struct symbol *s, *exp;
1285 for (s = mod->unres; s; s = s->next) {
1286 exp = find_symbol(s->name);
1287 if (!exp || exp->module == mod) {
1288 if (have_vmlinux && !s->weak) {
1289 warn("\"%s\" [%s.ko] undefined!\n",
1290 s->name, mod->name);
1291 err = warn_unresolved ? 0 : 1;
1295 s->module = exp->module;
1296 s->crc_valid = exp->crc_valid;
1303 buf_printf(b, "\n");
1304 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1305 buf_printf(b, "__attribute_used__\n");
1306 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1308 for (s = mod->unres; s; s = s->next) {
1312 if (!s->crc_valid) {
1313 warn("\"%s\" [%s.ko] has no CRC!\n",
1314 s->name, mod->name);
1317 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1320 buf_printf(b, "};\n");
1325 static void add_depends(struct buffer *b, struct module *mod,
1326 struct module *modules)
1332 for (m = modules; m; m = m->next) {
1333 m->seen = is_vmlinux(m->name);
1336 buf_printf(b, "\n");
1337 buf_printf(b, "static const char __module_depends[]\n");
1338 buf_printf(b, "__attribute_used__\n");
1339 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1340 buf_printf(b, "\"depends=");
1341 for (s = mod->unres; s; s = s->next) {
1345 if (s->module->seen)
1348 s->module->seen = 1;
1349 buf_printf(b, "%s%s", first ? "" : ",",
1350 strrchr(s->module->name, '/') + 1);
1353 buf_printf(b, "\";\n");
1356 static void add_srcversion(struct buffer *b, struct module *mod)
1358 if (mod->srcversion[0]) {
1359 buf_printf(b, "\n");
1360 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1365 static void write_if_changed(struct buffer *b, const char *fname)
1371 file = fopen(fname, "r");
1375 if (fstat(fileno(file), &st) < 0)
1378 if (st.st_size != b->pos)
1381 tmp = NOFAIL(malloc(b->pos));
1382 if (fread(tmp, 1, b->pos, file) != b->pos)
1385 if (memcmp(tmp, b->p, b->pos) != 0)
1397 file = fopen(fname, "w");
1402 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1409 /* parse Module.symvers file. line format:
1410 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1412 static void read_dump(const char *fname, unsigned int kernel)
1414 unsigned long size, pos = 0;
1415 void *file = grab_file(fname, &size);
1419 /* No symbol versions, silently ignore */
1422 while ((line = get_next_line(&pos, file, size))) {
1423 char *symname, *modname, *d, *export, *end;
1428 if (!(symname = strchr(line, '\t')))
1431 if (!(modname = strchr(symname, '\t')))
1434 if ((export = strchr(modname, '\t')) != NULL)
1436 if (export && ((end = strchr(export, '\t')) != NULL))
1438 crc = strtoul(line, &d, 16);
1439 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1442 if (!(mod = find_module(modname))) {
1443 if (is_vmlinux(modname)) {
1446 mod = new_module(NOFAIL(strdup(modname)));
1449 s = sym_add_exported(symname, mod, export_no(export));
1452 sym_update_crc(symname, mod, crc, export_no(export));
1456 fatal("parse error in symbol dump file\n");
1459 /* For normal builds always dump all symbols.
1460 * For external modules only dump symbols
1461 * that are not read from kernel Module.symvers.
1463 static int dump_sym(struct symbol *sym)
1465 if (!external_module)
1467 if (sym->vmlinux || sym->kernel)
1472 static void write_dump(const char *fname)
1474 struct buffer buf = { };
1475 struct symbol *symbol;
1478 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1479 symbol = symbolhash[n];
1481 if (dump_sym(symbol))
1482 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1483 symbol->crc, symbol->name,
1484 symbol->module->name,
1485 export_str(symbol->export));
1486 symbol = symbol->next;
1489 write_if_changed(&buf, fname);
1492 int main(int argc, char **argv)
1495 struct buffer buf = { };
1497 char *kernel_read = NULL, *module_read = NULL;
1498 char *dump_write = NULL;
1502 while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
1505 kernel_read = optarg;
1508 module_read = optarg;
1509 external_module = 1;
1515 dump_write = optarg;
1521 warn_unresolved = 1;
1529 read_dump(kernel_read, 1);
1531 read_dump(module_read, 0);
1533 while (optind < argc) {
1534 read_symbols(argv[optind++]);
1537 for (mod = modules; mod; mod = mod->next) {
1545 for (mod = modules; mod; mod = mod->next) {
1551 add_header(&buf, mod);
1552 err |= add_versions(&buf, mod);
1553 add_depends(&buf, mod, modules);
1554 add_moddevtable(&buf, mod);
1555 add_srcversion(&buf, mod);
1557 sprintf(fname, "%s.mod.c", mod->name);
1558 write_if_changed(&buf, fname);
1562 write_dump(dump_write);