1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006-2008 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 /* Warn about section mismatch in vmlinux if set to 1 */
27 static int vmlinux_section_warnings = 1;
28 /* Only warn about unresolved symbols */
29 static int warn_unresolved = 0;
30 /* How a symbol is exported */
32 export_plain, export_unused, export_gpl,
33 export_unused_gpl, export_gpl_future, export_unknown
36 #define PRINTF __attribute__ ((format (printf, 1, 2)))
38 PRINTF void fatal(const char *fmt, ...)
42 fprintf(stderr, "FATAL: ");
44 va_start(arglist, fmt);
45 vfprintf(stderr, fmt, arglist);
51 PRINTF void warn(const char *fmt, ...)
55 fprintf(stderr, "WARNING: ");
57 va_start(arglist, fmt);
58 vfprintf(stderr, fmt, arglist);
62 PRINTF void merror(const char *fmt, ...)
66 fprintf(stderr, "ERROR: ");
68 va_start(arglist, fmt);
69 vfprintf(stderr, fmt, arglist);
73 static int is_vmlinux(const char *modname)
77 myname = strrchr(modname, '/');
83 return (strcmp(myname, "vmlinux") == 0) ||
84 (strcmp(myname, "vmlinux.o") == 0);
87 void *do_nofail(void *ptr, const char *expr)
90 fatal("modpost: Memory allocation failure: %s.\n", expr);
95 /* A list of all modules we processed */
96 static struct module *modules;
98 static struct module *find_module(char *modname)
102 for (mod = modules; mod; mod = mod->next)
103 if (strcmp(mod->name, modname) == 0)
108 static struct module *new_module(char *modname)
113 mod = NOFAIL(malloc(sizeof(*mod)));
114 memset(mod, 0, sizeof(*mod));
115 p = NOFAIL(strdup(modname));
117 /* strip trailing .o */
120 if (strcmp(s, ".o") == 0)
125 mod->gpl_compatible = -1;
132 /* A hash of all exported symbols,
133 * struct symbol is also used for lists of unresolved symbols */
135 #define SYMBOL_HASH_SIZE 1024
139 struct module *module;
143 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
144 unsigned int kernel:1; /* 1 if symbol is from kernel
145 * (only for external modules) **/
146 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
147 enum export export; /* Type of export */
151 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
153 /* This is based on the hash agorithm from gdbm, via tdb */
154 static inline unsigned int tdb_hash(const char *name)
156 unsigned value; /* Used to compute the hash value. */
157 unsigned i; /* Used to cycle through random values. */
159 /* Set the initial value from the key size. */
160 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
161 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
163 return (1103515243 * value + 12345);
167 * Allocate a new symbols for use in the hash of exported symbols or
168 * the list of unresolved symbols per module
170 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
173 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
175 memset(s, 0, sizeof(*s));
176 strcpy(s->name, name);
182 /* For the hash of exported symbols */
183 static struct symbol *new_symbol(const char *name, struct module *module,
189 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
190 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
191 new->module = module;
192 new->export = export;
196 static struct symbol *find_symbol(const char *name)
200 /* For our purposes, .foo matches foo. PPC64 needs this. */
204 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
205 if (strcmp(s->name, name) == 0)
215 { .str = "EXPORT_SYMBOL", .export = export_plain },
216 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
217 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
218 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
219 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
220 { .str = "(unknown)", .export = export_unknown },
224 static const char *export_str(enum export ex)
226 return export_list[ex].str;
229 static enum export export_no(const char *s)
234 return export_unknown;
235 for (i = 0; export_list[i].export != export_unknown; i++) {
236 if (strcmp(export_list[i].str, s) == 0)
237 return export_list[i].export;
239 return export_unknown;
242 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
244 if (sec == elf->export_sec)
246 else if (sec == elf->export_unused_sec)
247 return export_unused;
248 else if (sec == elf->export_gpl_sec)
250 else if (sec == elf->export_unused_gpl_sec)
251 return export_unused_gpl;
252 else if (sec == elf->export_gpl_future_sec)
253 return export_gpl_future;
255 return export_unknown;
259 * Add an exported symbol - it may have already been added without a
260 * CRC, in this case just update the CRC
262 static struct symbol *sym_add_exported(const char *name, struct module *mod,
265 struct symbol *s = find_symbol(name);
268 s = new_symbol(name, mod, export);
271 warn("%s: '%s' exported twice. Previous export "
272 "was in %s%s\n", mod->name, name,
274 is_vmlinux(s->module->name) ?"":".ko");
276 /* In case Modules.symvers was out of date */
281 s->vmlinux = is_vmlinux(mod->name);
287 static void sym_update_crc(const char *name, struct module *mod,
288 unsigned int crc, enum export export)
290 struct symbol *s = find_symbol(name);
293 s = new_symbol(name, mod, export);
298 void *grab_file(const char *filename, unsigned long *size)
304 fd = open(filename, O_RDONLY);
305 if (fd < 0 || fstat(fd, &st) != 0)
309 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
312 if (map == MAP_FAILED)
318 * Return a copy of the next line in a mmap'ed file.
319 * spaces in the beginning of the line is trimmed away.
320 * Return a pointer to a static buffer.
322 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
324 static char line[4096];
327 signed char *p = (signed char *)file + *pos;
330 for (; *pos < size ; (*pos)++) {
331 if (skip && isspace(*p)) {
336 if (*p != '\n' && (*pos < size)) {
340 break; /* Too long, stop */
351 void release_file(void *file, unsigned long size)
356 static int parse_elf(struct elf_info *info, const char *filename)
363 hdr = grab_file(filename, &info->size);
369 if (info->size < sizeof(*hdr)) {
370 /* file too small, assume this is an empty .o file */
373 /* Is this a valid ELF file? */
374 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
375 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
376 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
377 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
378 /* Not an ELF file - silently ignore it */
381 /* Fix endianness in ELF header */
382 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
383 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
384 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
385 hdr->e_machine = TO_NATIVE(hdr->e_machine);
386 hdr->e_type = TO_NATIVE(hdr->e_type);
387 sechdrs = (void *)hdr + hdr->e_shoff;
388 info->sechdrs = sechdrs;
390 /* Check if file offset is correct */
391 if (hdr->e_shoff > info->size) {
392 fatal("section header offset=%lu in file '%s' is bigger than "
393 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
394 filename, info->size);
398 /* Fix endianness in section headers */
399 for (i = 0; i < hdr->e_shnum; i++) {
400 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
401 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
402 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
403 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
404 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
405 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
406 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
408 /* Find symbol table. */
409 for (i = 1; i < hdr->e_shnum; i++) {
410 const char *secstrings
411 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
414 if (sechdrs[i].sh_offset > info->size) {
415 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
416 "sizeof(*hrd)=%zu\n", filename,
417 (unsigned long)sechdrs[i].sh_offset,
421 secname = secstrings + sechdrs[i].sh_name;
422 if (strcmp(secname, ".modinfo") == 0) {
423 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
424 info->modinfo_len = sechdrs[i].sh_size;
425 } else if (strcmp(secname, "__ksymtab") == 0)
426 info->export_sec = i;
427 else if (strcmp(secname, "__ksymtab_unused") == 0)
428 info->export_unused_sec = i;
429 else if (strcmp(secname, "__ksymtab_gpl") == 0)
430 info->export_gpl_sec = i;
431 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
432 info->export_unused_gpl_sec = i;
433 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
434 info->export_gpl_future_sec = i;
436 if (sechdrs[i].sh_type != SHT_SYMTAB)
439 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
440 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
441 + sechdrs[i].sh_size;
442 info->strtab = (void *)hdr +
443 sechdrs[sechdrs[i].sh_link].sh_offset;
445 if (!info->symtab_start)
446 fatal("%s has no symtab?\n", filename);
448 /* Fix endianness in symbols */
449 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
450 sym->st_shndx = TO_NATIVE(sym->st_shndx);
451 sym->st_name = TO_NATIVE(sym->st_name);
452 sym->st_value = TO_NATIVE(sym->st_value);
453 sym->st_size = TO_NATIVE(sym->st_size);
458 static void parse_elf_finish(struct elf_info *info)
460 release_file(info->hdr, info->size);
463 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
464 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
466 static void handle_modversions(struct module *mod, struct elf_info *info,
467 Elf_Sym *sym, const char *symname)
470 enum export export = export_from_sec(info, sym->st_shndx);
472 switch (sym->st_shndx) {
474 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
478 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
479 crc = (unsigned int) sym->st_value;
480 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
485 /* undefined symbol */
486 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
487 ELF_ST_BIND(sym->st_info) != STB_WEAK)
489 /* ignore global offset table */
490 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
492 /* ignore __this_module, it will be resolved shortly */
493 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
495 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
496 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
497 /* add compatibility with older glibc */
498 #ifndef STT_SPARC_REGISTER
499 #define STT_SPARC_REGISTER STT_REGISTER
501 if (info->hdr->e_machine == EM_SPARC ||
502 info->hdr->e_machine == EM_SPARCV9) {
503 /* Ignore register directives. */
504 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
506 if (symname[0] == '.') {
507 char *munged = strdup(symname);
509 munged[1] = toupper(munged[1]);
515 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
516 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
518 alloc_symbol(symname +
519 strlen(MODULE_SYMBOL_PREFIX),
520 ELF_ST_BIND(sym->st_info) == STB_WEAK,
525 /* All exported symbols */
526 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
527 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
530 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
532 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
533 mod->has_cleanup = 1;
539 * Parse tag=value strings from .modinfo section
541 static char *next_string(char *string, unsigned long *secsize)
543 /* Skip non-zero chars */
546 if ((*secsize)-- <= 1)
550 /* Skip any zero padding. */
553 if ((*secsize)-- <= 1)
559 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
560 const char *tag, char *info)
563 unsigned int taglen = strlen(tag);
564 unsigned long size = modinfo_len;
567 size -= info - (char *)modinfo;
568 modinfo = next_string(info, &size);
571 for (p = modinfo; p; p = next_string(p, &size)) {
572 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
573 return p + taglen + 1;
578 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
582 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
586 * Test if string s ends in string sub
589 static int strrcmp(const char *s, const char *sub)
597 sublen = strlen(sub);
599 if ((slen == 0) || (sublen == 0))
605 return memcmp(s + slen - sublen, sub, sublen);
609 * Functions used only during module init is marked __init and is stored in
610 * a .init.text section. Likewise data is marked __initdata and stored in
611 * a .init.data section.
612 * If this section is one of these sections return 1
613 * See include/linux/init.h for the details
615 static int init_section(const char *name)
617 if (strcmp(name, ".init") == 0)
619 if (strncmp(name, ".init.", strlen(".init.")) == 0)
625 * Functions used only during module exit is marked __exit and is stored in
626 * a .exit.text section. Likewise data is marked __exitdata and stored in
627 * a .exit.data section.
628 * If this section is one of these sections return 1
629 * See include/linux/init.h for the details
631 static int exit_section(const char *name)
633 if (strcmp(name, ".exit.text") == 0)
635 if (strcmp(name, ".exit.data") == 0)
642 * Data sections are named like this:
643 * .data | .data.rel | .data.rel.*
644 * Return 1 if the specified section is a data section
646 static int data_section(const char *name)
648 if ((strcmp(name, ".data") == 0) ||
649 (strcmp(name, ".data.rel") == 0) ||
650 (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0))
657 * Whitelist to allow certain references to pass with no warning.
660 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
661 * The pattern is identified by:
662 * fromsec = .text.init.refok* | .data.init.refok*
665 * If a module parameter is declared __initdata and permissions=0
666 * then this is legal despite the warning generated.
667 * We cannot see value of permissions here, so just ignore
669 * The pattern is identified by:
675 * Many drivers utilise a *driver container with references to
676 * add, remove, probe functions etc.
677 * These functions may often be marked __init and we do not want to
679 * the pattern is identified by:
680 * tosec = init or exit section
681 * fromsec = data section
682 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
683 * *probe_one, *_console, *_timer
686 * Whitelist all refereces from .text.head to .init.data
687 * Whitelist all refereces from .text.head to .init.text
690 * Some symbols belong to init section but still it is ok to reference
691 * these from non-init sections as these symbols don't have any memory
692 * allocated for them and symbol address and value are same. So even
693 * if init section is freed, its ok to reference those symbols.
694 * For ex. symbols marking the init section boundaries.
695 * This pattern is identified by
696 * refsymname = __init_begin, _sinittext, _einittext
699 * Xtensa uses literal sections for constants that are accessed PC-relative.
700 * Literal sections may safely reference their text sections.
701 * (Note that the name for the literal section omits any trailing '.text')
702 * tosec = <section>[.text]
703 * fromsec = <section>.literal
705 static int secref_whitelist(const char *modname, const char *tosec,
706 const char *fromsec, const char *atsym,
707 const char *refsymname)
711 const char *pat2sym[] = {
713 "_template", /* scsi uses *_template a lot */
714 "_timer", /* arm uses ops structures named _timer a lot */
715 "_sht", /* scsi also used *_sht to some extent */
723 const char *pat3refsym[] = {
730 /* Check for pattern 0 */
731 if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) ||
732 (strncmp(fromsec, ".exit.text.refok", strlen(".exit.text.refok")) == 0) ||
733 (strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0))
736 /* Check for pattern 1 */
737 if ((strcmp(tosec, ".init.data") == 0) &&
738 (strncmp(fromsec, ".data", strlen(".data")) == 0) &&
739 (strncmp(atsym, "__param", strlen("__param")) == 0))
742 /* Check for pattern 2 */
743 if ((init_section(tosec) || exit_section(tosec))
744 && data_section(fromsec))
745 for (s = pat2sym; *s; s++)
746 if (strrcmp(atsym, *s) == 0)
749 /* Check for pattern 3 */
750 if ((strcmp(fromsec, ".text.head") == 0) &&
751 ((strcmp(tosec, ".init.data") == 0) ||
752 (strcmp(tosec, ".init.text") == 0)))
755 /* Check for pattern 4 */
756 for (s = pat3refsym; *s; s++)
757 if (strcmp(refsymname, *s) == 0)
760 /* Check for pattern 5 */
761 if (strrcmp(tosec, ".text") == 0)
762 len = strlen(tosec) - strlen(".text");
765 if ((strncmp(tosec, fromsec, len) == 0) && (strlen(fromsec) > len) &&
766 (strcmp(fromsec + len, ".literal") == 0))
773 * Find symbol based on relocation record info.
774 * In some cases the symbol supplied is a valid symbol so
775 * return refsym. If st_name != 0 we assume this is a valid symbol.
776 * In other cases the symbol needs to be looked up in the symbol table
777 * based on section and address.
779 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
784 if (relsym->st_name != 0)
786 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
787 if (sym->st_shndx != relsym->st_shndx)
789 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
791 if (sym->st_value == addr)
797 static inline int is_arm_mapping_symbol(const char *str)
799 return str[0] == '$' && strchr("atd", str[1])
800 && (str[2] == '\0' || str[2] == '.');
804 * If there's no name there, ignore it; likewise, ignore it if it's
805 * one of the magic symbols emitted used by current ARM tools.
807 * Otherwise if find_symbols_between() returns those symbols, they'll
808 * fail the whitelist tests and cause lots of false alarms ... fixable
809 * only by merging __exit and __init sections into __text, bloating
810 * the kernel (which is especially evil on embedded platforms).
812 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
814 const char *name = elf->strtab + sym->st_name;
816 if (!name || !strlen(name))
818 return !is_arm_mapping_symbol(name);
822 * Find symbols before or equal addr and after addr - in the section sec.
823 * If we find two symbols with equal offset prefer one with a valid name.
824 * The ELF format may have a better way to detect what type of symbol
825 * it is, but this works for now.
827 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
829 Elf_Sym **before, Elf_Sym **after)
832 Elf_Ehdr *hdr = elf->hdr;
833 Elf_Addr beforediff = ~0;
834 Elf_Addr afterdiff = ~0;
835 const char *secstrings = (void *)hdr +
836 elf->sechdrs[hdr->e_shstrndx].sh_offset;
841 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
844 if (sym->st_shndx >= SHN_LORESERVE)
846 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
847 if (strcmp(symsec, sec) != 0)
849 if (!is_valid_name(elf, sym))
851 if (sym->st_value <= addr) {
852 if ((addr - sym->st_value) < beforediff) {
853 beforediff = addr - sym->st_value;
855 } else if ((addr - sym->st_value) == beforediff) {
859 if ((sym->st_value - addr) < afterdiff) {
860 afterdiff = sym->st_value - addr;
862 } else if ((sym->st_value - addr) == afterdiff)
869 * Print a warning about a section mismatch.
870 * Try to find symbols near it so user can find it.
871 * Check whitelist before warning - it may be a false positive.
873 static void warn_sec_mismatch(const char *modname, const char *fromsec,
874 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
876 const char *refsymname = "";
877 Elf_Sym *before, *after;
879 Elf_Ehdr *hdr = elf->hdr;
880 Elf_Shdr *sechdrs = elf->sechdrs;
881 const char *secstrings = (void *)hdr +
882 sechdrs[hdr->e_shstrndx].sh_offset;
883 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
885 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
887 refsym = find_elf_symbol(elf, r.r_addend, sym);
888 if (refsym && strlen(elf->strtab + refsym->st_name))
889 refsymname = elf->strtab + refsym->st_name;
891 /* check whitelist - we may ignore it */
892 if (secref_whitelist(modname, secname, fromsec,
893 before ? elf->strtab + before->st_name : "",
897 if (before && after) {
898 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
899 "(between '%s' and '%s')\n",
900 modname, fromsec, (unsigned long long)r.r_offset,
902 elf->strtab + before->st_name,
903 elf->strtab + after->st_name);
905 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
907 modname, fromsec, (unsigned long long)r.r_offset,
909 elf->strtab + before->st_name);
911 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
912 "before '%s' (at offset -0x%llx)\n",
913 modname, fromsec, (unsigned long long)r.r_offset,
915 elf->strtab + after->st_name,
916 (unsigned long long)r.r_offset);
918 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
919 modname, fromsec, (unsigned long long)r.r_offset,
920 secname, refsymname);
924 static unsigned int *reloc_location(struct elf_info *elf,
925 int rsection, Elf_Rela *r)
927 Elf_Shdr *sechdrs = elf->sechdrs;
928 int section = sechdrs[rsection].sh_info;
930 return (void *)elf->hdr + sechdrs[section].sh_offset +
931 (r->r_offset - sechdrs[section].sh_addr);
934 static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
936 unsigned int r_typ = ELF_R_TYPE(r->r_info);
937 unsigned int *location = reloc_location(elf, rsection, r);
941 r->r_addend = TO_NATIVE(*location);
944 r->r_addend = TO_NATIVE(*location) + 4;
945 /* For CONFIG_RELOCATABLE=y */
946 if (elf->hdr->e_type == ET_EXEC)
947 r->r_addend += r->r_offset;
953 static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
955 unsigned int r_typ = ELF_R_TYPE(r->r_info);
959 /* From ARM ABI: (S + A) | T */
960 r->r_addend = (int)(long)
961 (elf->symtab_start + ELF_R_SYM(r->r_info));
964 /* From ARM ABI: ((S + A) | T) - P */
965 r->r_addend = (int)(long)(elf->hdr +
966 elf->sechdrs[rsection].sh_offset +
967 (r->r_offset - elf->sechdrs[rsection].sh_addr));
975 static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
977 unsigned int r_typ = ELF_R_TYPE(r->r_info);
978 unsigned int *location = reloc_location(elf, rsection, r);
981 if (r_typ == R_MIPS_HI16)
982 return 1; /* skip this */
983 inst = TO_NATIVE(*location);
986 r->r_addend = inst & 0xffff;
989 r->r_addend = (inst & 0x03ffffff) << 2;
999 * A module includes a number of sections that are discarded
1000 * either when loaded or when used as built-in.
1001 * For loaded modules all functions marked __init and all data
1002 * marked __initdata will be discarded when the module has been intialized.
1003 * Likewise for modules used built-in the sections marked __exit
1004 * are discarded because __exit marked function are supposed to be called
1005 * only when a moduel is unloaded which never happes for built-in modules.
1006 * The check_sec_ref() function traverses all relocation records
1007 * to find all references to a section that reference a section that will
1008 * be discarded and warns about it.
1010 static void check_sec_ref(struct module *mod, const char *modname,
1011 struct elf_info *elf,
1012 int section(const char *),
1013 int section_ref_ok(const char *))
1017 Elf_Ehdr *hdr = elf->hdr;
1018 Elf_Shdr *sechdrs = elf->sechdrs;
1019 const char *secstrings = (void *)hdr +
1020 sechdrs[hdr->e_shstrndx].sh_offset;
1022 /* Walk through all sections */
1023 for (i = 0; i < hdr->e_shnum; i++) {
1024 const char *name = secstrings + sechdrs[i].sh_name;
1025 const char *secname;
1028 /* We want to process only relocation sections and not .init */
1029 if (sechdrs[i].sh_type == SHT_RELA) {
1031 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
1032 Elf_Rela *stop = (void *)start + sechdrs[i].sh_size;
1033 name += strlen(".rela");
1034 if (section_ref_ok(name))
1037 for (rela = start; rela < stop; rela++) {
1038 r.r_offset = TO_NATIVE(rela->r_offset);
1039 #if KERNEL_ELFCLASS == ELFCLASS64
1040 if (hdr->e_machine == EM_MIPS) {
1042 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1043 r_sym = TO_NATIVE(r_sym);
1044 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1045 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1047 r.r_info = TO_NATIVE(rela->r_info);
1048 r_sym = ELF_R_SYM(r.r_info);
1051 r.r_info = TO_NATIVE(rela->r_info);
1052 r_sym = ELF_R_SYM(r.r_info);
1054 r.r_addend = TO_NATIVE(rela->r_addend);
1055 sym = elf->symtab_start + r_sym;
1056 /* Skip special sections */
1057 if (sym->st_shndx >= SHN_LORESERVE)
1060 secname = secstrings +
1061 sechdrs[sym->st_shndx].sh_name;
1062 if (section(secname))
1063 warn_sec_mismatch(modname, name,
1066 } else if (sechdrs[i].sh_type == SHT_REL) {
1068 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
1069 Elf_Rel *stop = (void *)start + sechdrs[i].sh_size;
1070 name += strlen(".rel");
1071 if (section_ref_ok(name))
1074 for (rel = start; rel < stop; rel++) {
1075 r.r_offset = TO_NATIVE(rel->r_offset);
1076 #if KERNEL_ELFCLASS == ELFCLASS64
1077 if (hdr->e_machine == EM_MIPS) {
1079 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1080 r_sym = TO_NATIVE(r_sym);
1081 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1082 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1084 r.r_info = TO_NATIVE(rel->r_info);
1085 r_sym = ELF_R_SYM(r.r_info);
1088 r.r_info = TO_NATIVE(rel->r_info);
1089 r_sym = ELF_R_SYM(r.r_info);
1092 switch (hdr->e_machine) {
1094 if (addend_386_rel(elf, i, &r))
1098 if (addend_arm_rel(elf, i, &r))
1102 if (addend_mips_rel(elf, i, &r))
1106 sym = elf->symtab_start + r_sym;
1107 /* Skip special sections */
1108 if (sym->st_shndx >= SHN_LORESERVE)
1111 secname = secstrings +
1112 sechdrs[sym->st_shndx].sh_name;
1113 if (section(secname))
1114 warn_sec_mismatch(modname, name,
1122 * Identify sections from which references to either a
1123 * .init or a .exit section is OK.
1125 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1126 * For our future {in}sanity, add a comment that this is the ppc .opd
1127 * section, not the ia64 .opd section.
1128 * ia64 .opd should not point to discarded sections.
1129 * [.rodata] like for .init.text we ignore .rodata references -same reason
1131 static int initexit_section_ref_ok(const char *name)
1134 /* Absolute section names */
1135 const char *namelist1[] = {
1136 "__bug_table", /* used by powerpc for BUG() */
1139 ".cranges", /* used by sh64 */
1141 ".machvec", /* ia64 + powerpc uses these */
1143 ".opd", /* See comment [OPD] */
1145 ".parainstructions",
1147 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
1151 ".xt.prop", /* xtensa informational section */
1152 ".xt.lit", /* xtensa informational section */
1155 /* Start of section names */
1156 const char *namelist2[] = {
1159 ".note", /* ignore ELF notes - may contain anything */
1160 ".got", /* powerpc - global offset table */
1161 ".toc", /* powerpc - table of contents */
1164 /* part of section name */
1165 const char *namelist3 [] = {
1166 ".unwind", /* Sample: IA_64.unwind.exit.text */
1170 for (s = namelist1; *s; s++)
1171 if (strcmp(*s, name) == 0)
1173 for (s = namelist2; *s; s++)
1174 if (strncmp(*s, name, strlen(*s)) == 0)
1176 for (s = namelist3; *s; s++)
1177 if (strstr(name, *s) != NULL)
1184 * Identify sections from which references to a .init section is OK.
1186 * Unfortunately references to read only data that referenced .init
1187 * sections had to be excluded. Almost all of these are false
1188 * positives, they are created by gcc. The downside of excluding rodata
1189 * is that there really are some user references from rodata to
1190 * init code, e.g. drivers/video/vgacon.c:
1192 * const struct consw vga_con = {
1193 * con_startup: vgacon_startup,
1195 * where vgacon_startup is __init. If you want to wade through the false
1196 * positives, take out the check for rodata.
1198 static int init_section_ref_ok(const char *name)
1201 /* Absolute section names */
1202 const char *namelist1[] = {
1203 "__dbe_table", /* MIPS generate these */
1204 "__ftr_fixup", /* powerpc cpu feature fixup */
1205 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
1207 ".data.rel.ro", /* used by parisc64 */
1212 /* Start of section names */
1213 const char *namelist2[] = {
1220 if (initexit_section_ref_ok(name))
1223 for (s = namelist1; *s; s++)
1224 if (strcmp(*s, name) == 0)
1226 for (s = namelist2; *s; s++)
1227 if (strncmp(*s, name, strlen(*s)) == 0)
1230 /* If section name ends with ".init" we allow references
1231 * as is the case with .initcallN.init, .early_param.init,
1234 if (strrcmp(name, ".init") == 0)
1240 * Identify sections from which references to a .exit section is OK.
1242 static int exit_section_ref_ok(const char *name)
1245 /* Absolute section names */
1246 const char *namelist1[] = {
1254 if (initexit_section_ref_ok(name))
1257 for (s = namelist1; *s; s++)
1258 if (strcmp(*s, name) == 0)
1263 static void read_symbols(char *modname)
1265 const char *symname;
1269 struct elf_info info = { };
1272 if (!parse_elf(&info, modname))
1275 mod = new_module(modname);
1277 /* When there's no vmlinux, don't print warnings about
1278 * unresolved symbols (since there'll be too many ;) */
1279 if (is_vmlinux(modname)) {
1284 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1286 if (license_is_gpl_compatible(license))
1287 mod->gpl_compatible = 1;
1289 mod->gpl_compatible = 0;
1292 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1293 "license", license);
1296 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1297 symname = info.strtab + sym->st_name;
1299 handle_modversions(mod, &info, sym, symname);
1300 handle_moddevtable(mod, &info, sym, symname);
1302 if (!is_vmlinux(modname) ||
1303 (is_vmlinux(modname) && vmlinux_section_warnings)) {
1304 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1305 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1308 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1310 maybe_frob_rcs_version(modname, version, info.modinfo,
1311 version - (char *)info.hdr);
1312 if (version || (all_versions && !is_vmlinux(modname)))
1313 get_src_version(modname, mod->srcversion,
1314 sizeof(mod->srcversion)-1);
1316 parse_elf_finish(&info);
1318 /* Our trick to get versioning for struct_module - it's
1319 * never passed as an argument to an exported function, so
1320 * the automatic versioning doesn't pick it up, but it's really
1321 * important anyhow */
1323 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1328 /* We first write the generated file into memory using the
1329 * following helper, then compare to the file on disk and
1330 * only update the later if anything changed */
1332 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1333 const char *fmt, ...)
1340 len = vsnprintf(tmp, SZ, fmt, ap);
1341 buf_write(buf, tmp, len);
1345 void buf_write(struct buffer *buf, const char *s, int len)
1347 if (buf->size - buf->pos < len) {
1348 buf->size += len + SZ;
1349 buf->p = realloc(buf->p, buf->size);
1351 strncpy(buf->p + buf->pos, s, len);
1355 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1357 const char *e = is_vmlinux(m) ?"":".ko";
1361 fatal("modpost: GPL-incompatible module %s%s "
1362 "uses GPL-only symbol '%s'\n", m, e, s);
1364 case export_unused_gpl:
1365 fatal("modpost: GPL-incompatible module %s%s "
1366 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1368 case export_gpl_future:
1369 warn("modpost: GPL-incompatible module %s%s "
1370 "uses future GPL-only symbol '%s'\n", m, e, s);
1374 case export_unknown:
1380 static void check_for_unused(enum export exp, const char *m, const char *s)
1382 const char *e = is_vmlinux(m) ?"":".ko";
1386 case export_unused_gpl:
1387 warn("modpost: module %s%s "
1388 "uses symbol '%s' marked UNUSED\n", m, e, s);
1396 static void check_exports(struct module *mod)
1398 struct symbol *s, *exp;
1400 for (s = mod->unres; s; s = s->next) {
1401 const char *basename;
1402 exp = find_symbol(s->name);
1403 if (!exp || exp->module == mod)
1405 basename = strrchr(mod->name, '/');
1409 basename = mod->name;
1410 if (!mod->gpl_compatible)
1411 check_for_gpl_usage(exp->export, basename, exp->name);
1412 check_for_unused(exp->export, basename, exp->name);
1417 * Header for the generated file
1419 static void add_header(struct buffer *b, struct module *mod)
1421 buf_printf(b, "#include <linux/module.h>\n");
1422 buf_printf(b, "#include <linux/vermagic.h>\n");
1423 buf_printf(b, "#include <linux/compiler.h>\n");
1424 buf_printf(b, "\n");
1425 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1426 buf_printf(b, "\n");
1427 buf_printf(b, "struct module __this_module\n");
1428 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1429 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1431 buf_printf(b, " .init = init_module,\n");
1432 if (mod->has_cleanup)
1433 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1434 " .exit = cleanup_module,\n"
1436 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1437 buf_printf(b, "};\n");
1441 * Record CRCs for unresolved symbols
1443 static int add_versions(struct buffer *b, struct module *mod)
1445 struct symbol *s, *exp;
1448 for (s = mod->unres; s; s = s->next) {
1449 exp = find_symbol(s->name);
1450 if (!exp || exp->module == mod) {
1451 if (have_vmlinux && !s->weak) {
1452 if (warn_unresolved) {
1453 warn("\"%s\" [%s.ko] undefined!\n",
1454 s->name, mod->name);
1456 merror("\"%s\" [%s.ko] undefined!\n",
1457 s->name, mod->name);
1463 s->module = exp->module;
1464 s->crc_valid = exp->crc_valid;
1471 buf_printf(b, "\n");
1472 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1473 buf_printf(b, "__attribute_used__\n");
1474 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1476 for (s = mod->unres; s; s = s->next) {
1479 if (!s->crc_valid) {
1480 warn("\"%s\" [%s.ko] has no CRC!\n",
1481 s->name, mod->name);
1484 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1487 buf_printf(b, "};\n");
1492 static void add_depends(struct buffer *b, struct module *mod,
1493 struct module *modules)
1499 for (m = modules; m; m = m->next)
1500 m->seen = is_vmlinux(m->name);
1502 buf_printf(b, "\n");
1503 buf_printf(b, "static const char __module_depends[]\n");
1504 buf_printf(b, "__attribute_used__\n");
1505 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1506 buf_printf(b, "\"depends=");
1507 for (s = mod->unres; s; s = s->next) {
1512 if (s->module->seen)
1515 s->module->seen = 1;
1516 p = strrchr(s->module->name, '/');
1520 p = s->module->name;
1521 buf_printf(b, "%s%s", first ? "" : ",", p);
1524 buf_printf(b, "\";\n");
1527 static void add_srcversion(struct buffer *b, struct module *mod)
1529 if (mod->srcversion[0]) {
1530 buf_printf(b, "\n");
1531 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1536 static void write_if_changed(struct buffer *b, const char *fname)
1542 file = fopen(fname, "r");
1546 if (fstat(fileno(file), &st) < 0)
1549 if (st.st_size != b->pos)
1552 tmp = NOFAIL(malloc(b->pos));
1553 if (fread(tmp, 1, b->pos, file) != b->pos)
1556 if (memcmp(tmp, b->p, b->pos) != 0)
1568 file = fopen(fname, "w");
1573 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1580 /* parse Module.symvers file. line format:
1581 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1583 static void read_dump(const char *fname, unsigned int kernel)
1585 unsigned long size, pos = 0;
1586 void *file = grab_file(fname, &size);
1590 /* No symbol versions, silently ignore */
1593 while ((line = get_next_line(&pos, file, size))) {
1594 char *symname, *modname, *d, *export, *end;
1599 if (!(symname = strchr(line, '\t')))
1602 if (!(modname = strchr(symname, '\t')))
1605 if ((export = strchr(modname, '\t')) != NULL)
1607 if (export && ((end = strchr(export, '\t')) != NULL))
1609 crc = strtoul(line, &d, 16);
1610 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1612 mod = find_module(modname);
1614 if (is_vmlinux(modname))
1616 mod = new_module(NOFAIL(strdup(modname)));
1619 s = sym_add_exported(symname, mod, export_no(export));
1622 sym_update_crc(symname, mod, crc, export_no(export));
1626 fatal("parse error in symbol dump file\n");
1629 /* For normal builds always dump all symbols.
1630 * For external modules only dump symbols
1631 * that are not read from kernel Module.symvers.
1633 static int dump_sym(struct symbol *sym)
1635 if (!external_module)
1637 if (sym->vmlinux || sym->kernel)
1642 static void write_dump(const char *fname)
1644 struct buffer buf = { };
1645 struct symbol *symbol;
1648 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1649 symbol = symbolhash[n];
1651 if (dump_sym(symbol))
1652 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1653 symbol->crc, symbol->name,
1654 symbol->module->name,
1655 export_str(symbol->export));
1656 symbol = symbol->next;
1659 write_if_changed(&buf, fname);
1662 int main(int argc, char **argv)
1665 struct buffer buf = { };
1666 char *kernel_read = NULL, *module_read = NULL;
1667 char *dump_write = NULL;
1671 while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
1674 kernel_read = optarg;
1677 module_read = optarg;
1678 external_module = 1;
1684 dump_write = optarg;
1690 vmlinux_section_warnings = 0;
1693 warn_unresolved = 1;
1701 read_dump(kernel_read, 1);
1703 read_dump(module_read, 0);
1705 while (optind < argc)
1706 read_symbols(argv[optind++]);
1708 for (mod = modules; mod; mod = mod->next) {
1716 for (mod = modules; mod; mod = mod->next) {
1717 char fname[strlen(mod->name) + 10];
1724 add_header(&buf, mod);
1725 err |= add_versions(&buf, mod);
1726 add_depends(&buf, mod, modules);
1727 add_moddevtable(&buf, mod);
1728 add_srcversion(&buf, mod);
1730 sprintf(fname, "%s.mod.c", mod->name);
1731 write_if_changed(&buf, fname);
1735 write_dump(dump_write);