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 ...
22 #include "../../include/linux/license.h"
24 /* Are we using CONFIG_MODVERSIONS? */
25 static int modversions = 0;
26 /* Warn about undefined symbols? (do so if we have vmlinux) */
27 static int have_vmlinux = 0;
28 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29 static int all_versions = 0;
30 /* If we are modposting external module set to 1 */
31 static int external_module = 0;
32 /* Warn about section mismatch in vmlinux if set to 1 */
33 static int vmlinux_section_warnings = 1;
34 /* Only warn about unresolved symbols */
35 static int warn_unresolved = 0;
36 /* How a symbol is exported */
37 static int sec_mismatch_count = 0;
38 static int sec_mismatch_fatal = 0;
39 /* ignore missing files */
40 static int ignore_missing_files;
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
47 /* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
51 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53 #define PRINTF __attribute__ ((format (printf, 1, 2)))
55 PRINTF void fatal(const char *fmt, ...)
59 fprintf(stderr, "FATAL: ");
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
68 PRINTF void warn(const char *fmt, ...)
72 fprintf(stderr, "WARNING: ");
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
79 PRINTF void merror(const char *fmt, ...)
83 fprintf(stderr, "ERROR: ");
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
90 static inline bool strends(const char *str, const char *postfix)
92 if (strlen(str) < strlen(postfix))
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
98 static int is_vmlinux(const char *modname)
102 myname = strrchr(modname, '/');
108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
112 void *do_nofail(void *ptr, const char *expr)
115 fatal("modpost: Memory allocation failure: %s.\n", expr);
120 /* A list of all modules we processed */
121 static struct module *modules;
123 static struct module *find_module(const char *modname)
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
133 static struct module *new_module(const char *modname)
138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
142 /* strip trailing .o */
143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
150 mod->gpl_compatible = -1;
157 /* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
160 #define SYMBOL_HASH_SIZE 1024
164 struct module *module;
168 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
169 unsigned int kernel:1; /* 1 if symbol is from kernel
170 * (only for external modules) **/
171 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
172 unsigned int is_static:1; /* 1 if symbol is not global */
173 enum export export; /* Type of export */
177 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
179 /* This is based on the hash agorithm from gdbm, via tdb */
180 static inline unsigned int tdb_hash(const char *name)
182 unsigned value; /* Used to compute the hash value. */
183 unsigned i; /* Used to cycle through random values. */
185 /* Set the initial value from the key size. */
186 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
187 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
189 return (1103515243 * value + 12345);
193 * Allocate a new symbols for use in the hash of exported symbols or
194 * the list of unresolved symbols per module
196 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
199 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
201 memset(s, 0, sizeof(*s));
202 strcpy(s->name, name);
209 /* For the hash of exported symbols */
210 static struct symbol *new_symbol(const char *name, struct module *module,
216 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
217 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
218 new->module = module;
219 new->export = export;
223 static struct symbol *find_symbol(const char *name)
227 /* For our purposes, .foo matches foo. PPC64 needs this. */
231 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
232 if (strcmp(s->name, name) == 0)
238 static const struct {
242 { .str = "EXPORT_SYMBOL", .export = export_plain },
243 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
244 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
245 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
246 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
247 { .str = "(unknown)", .export = export_unknown },
251 static const char *export_str(enum export ex)
253 return export_list[ex].str;
256 static enum export export_no(const char *s)
261 return export_unknown;
262 for (i = 0; export_list[i].export != export_unknown; i++) {
263 if (strcmp(export_list[i].str, s) == 0)
264 return export_list[i].export;
266 return export_unknown;
269 static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
271 return (void *)elf->hdr +
272 elf->sechdrs[elf->secindex_strings].sh_offset +
276 static const char *sec_name(struct elf_info *elf, int secindex)
278 return sech_name(elf, &elf->sechdrs[secindex]);
281 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
283 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
285 const char *secname = sec_name(elf, sec);
287 if (strstarts(secname, "___ksymtab+"))
289 else if (strstarts(secname, "___ksymtab_unused+"))
290 return export_unused;
291 else if (strstarts(secname, "___ksymtab_gpl+"))
293 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
294 return export_unused_gpl;
295 else if (strstarts(secname, "___ksymtab_gpl_future+"))
296 return export_gpl_future;
298 return export_unknown;
301 static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
303 if (sec == elf->export_sec)
305 else if (sec == elf->export_unused_sec)
306 return export_unused;
307 else if (sec == elf->export_gpl_sec)
309 else if (sec == elf->export_unused_gpl_sec)
310 return export_unused_gpl;
311 else if (sec == elf->export_gpl_future_sec)
312 return export_gpl_future;
314 return export_unknown;
318 * Add an exported symbol - it may have already been added without a
319 * CRC, in this case just update the CRC
321 static struct symbol *sym_add_exported(const char *name, struct module *mod,
324 struct symbol *s = find_symbol(name);
327 s = new_symbol(name, mod, export);
330 warn("%s: '%s' exported twice. Previous export "
331 "was in %s%s\n", mod->name, name,
333 is_vmlinux(s->module->name) ?"":".ko");
335 /* In case Module.symvers was out of date */
340 s->vmlinux = is_vmlinux(mod->name);
346 static void sym_update_crc(const char *name, struct module *mod,
347 unsigned int crc, enum export export)
349 struct symbol *s = find_symbol(name);
352 s = new_symbol(name, mod, export);
353 /* Don't complain when we find it later. */
360 void *grab_file(const char *filename, unsigned long *size)
363 void *map = MAP_FAILED;
366 fd = open(filename, O_RDONLY);
373 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
377 if (map == MAP_FAILED)
383 * Return a copy of the next line in a mmap'ed file.
384 * spaces in the beginning of the line is trimmed away.
385 * Return a pointer to a static buffer.
387 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
389 static char line[4096];
392 signed char *p = (signed char *)file + *pos;
395 for (; *pos < size ; (*pos)++) {
396 if (skip && isspace(*p)) {
401 if (*p != '\n' && (*pos < size)) {
405 break; /* Too long, stop */
416 void release_file(void *file, unsigned long size)
421 static int parse_elf(struct elf_info *info, const char *filename)
427 const char *secstrings;
428 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
430 hdr = grab_file(filename, &info->size);
432 if (ignore_missing_files) {
433 fprintf(stderr, "%s: %s (ignored)\n", filename,
441 if (info->size < sizeof(*hdr)) {
442 /* file too small, assume this is an empty .o file */
445 /* Is this a valid ELF file? */
446 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
447 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
448 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
449 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
450 /* Not an ELF file - silently ignore it */
453 /* Fix endianness in ELF header */
454 hdr->e_type = TO_NATIVE(hdr->e_type);
455 hdr->e_machine = TO_NATIVE(hdr->e_machine);
456 hdr->e_version = TO_NATIVE(hdr->e_version);
457 hdr->e_entry = TO_NATIVE(hdr->e_entry);
458 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
459 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
460 hdr->e_flags = TO_NATIVE(hdr->e_flags);
461 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
462 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
463 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
464 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
465 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
466 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
467 sechdrs = (void *)hdr + hdr->e_shoff;
468 info->sechdrs = sechdrs;
470 /* Check if file offset is correct */
471 if (hdr->e_shoff > info->size) {
472 fatal("section header offset=%lu in file '%s' is bigger than "
473 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
474 filename, info->size);
478 if (hdr->e_shnum == SHN_UNDEF) {
480 * There are more than 64k sections,
481 * read count from .sh_size.
483 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
486 info->num_sections = hdr->e_shnum;
488 if (hdr->e_shstrndx == SHN_XINDEX) {
489 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
492 info->secindex_strings = hdr->e_shstrndx;
495 /* Fix endianness in section headers */
496 for (i = 0; i < info->num_sections; i++) {
497 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
498 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
499 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
500 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
501 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
502 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
503 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
504 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
505 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
506 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
508 /* Find symbol table. */
509 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
510 for (i = 1; i < info->num_sections; i++) {
512 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
514 if (!nobits && sechdrs[i].sh_offset > info->size) {
515 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
516 "sizeof(*hrd)=%zu\n", filename,
517 (unsigned long)sechdrs[i].sh_offset,
521 secname = secstrings + sechdrs[i].sh_name;
522 if (strcmp(secname, ".modinfo") == 0) {
524 fatal("%s has NOBITS .modinfo\n", filename);
525 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
526 info->modinfo_len = sechdrs[i].sh_size;
527 } else if (strcmp(secname, "__ksymtab") == 0)
528 info->export_sec = i;
529 else if (strcmp(secname, "__ksymtab_unused") == 0)
530 info->export_unused_sec = i;
531 else if (strcmp(secname, "__ksymtab_gpl") == 0)
532 info->export_gpl_sec = i;
533 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
534 info->export_unused_gpl_sec = i;
535 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
536 info->export_gpl_future_sec = i;
538 if (sechdrs[i].sh_type == SHT_SYMTAB) {
539 unsigned int sh_link_idx;
541 info->symtab_start = (void *)hdr +
542 sechdrs[i].sh_offset;
543 info->symtab_stop = (void *)hdr +
544 sechdrs[i].sh_offset + sechdrs[i].sh_size;
545 sh_link_idx = sechdrs[i].sh_link;
546 info->strtab = (void *)hdr +
547 sechdrs[sh_link_idx].sh_offset;
550 /* 32bit section no. table? ("more than 64k sections") */
551 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
552 symtab_shndx_idx = i;
553 info->symtab_shndx_start = (void *)hdr +
554 sechdrs[i].sh_offset;
555 info->symtab_shndx_stop = (void *)hdr +
556 sechdrs[i].sh_offset + sechdrs[i].sh_size;
559 if (!info->symtab_start)
560 fatal("%s has no symtab?\n", filename);
562 /* Fix endianness in symbols */
563 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
564 sym->st_shndx = TO_NATIVE(sym->st_shndx);
565 sym->st_name = TO_NATIVE(sym->st_name);
566 sym->st_value = TO_NATIVE(sym->st_value);
567 sym->st_size = TO_NATIVE(sym->st_size);
570 if (symtab_shndx_idx != ~0U) {
572 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
573 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
574 filename, sechdrs[symtab_shndx_idx].sh_link,
577 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
585 static void parse_elf_finish(struct elf_info *info)
587 release_file(info->hdr, info->size);
590 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
592 /* ignore __this_module, it will be resolved shortly */
593 if (strcmp(symname, "__this_module") == 0)
595 /* ignore global offset table */
596 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
598 if (info->hdr->e_machine == EM_PPC)
599 /* Special register function linked on all modules during final link of .ko */
600 if (strstarts(symname, "_restgpr_") ||
601 strstarts(symname, "_savegpr_") ||
602 strstarts(symname, "_rest32gpr_") ||
603 strstarts(symname, "_save32gpr_") ||
604 strstarts(symname, "_restvr_") ||
605 strstarts(symname, "_savevr_"))
607 if (info->hdr->e_machine == EM_PPC64)
608 /* Special register function linked on all modules during final link of .ko */
609 if (strstarts(symname, "_restgpr0_") ||
610 strstarts(symname, "_savegpr0_") ||
611 strstarts(symname, "_restvr_") ||
612 strstarts(symname, "_savevr_") ||
613 strcmp(symname, ".TOC.") == 0)
615 /* Do not ignore this symbol */
619 static void handle_modversions(struct module *mod, struct elf_info *info,
620 Elf_Sym *sym, const char *symname)
626 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
627 strstarts(symname, "__ksymtab"))
628 export = export_from_secname(info, get_secindex(info, sym));
630 export = export_from_sec(info, get_secindex(info, sym));
633 if (strstarts(symname, "__crc_")) {
635 crc = (unsigned int) sym->st_value;
636 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
639 /* symbol points to the CRC in the ELF object */
640 crcp = (void *)info->hdr + sym->st_value +
641 info->sechdrs[sym->st_shndx].sh_offset -
642 (info->hdr->e_type != ET_REL ?
643 info->sechdrs[sym->st_shndx].sh_addr : 0);
644 crc = TO_NATIVE(*crcp);
646 sym_update_crc(symname + strlen("__crc_"), mod, crc,
650 switch (sym->st_shndx) {
652 if (strstarts(symname, "__gnu_lto_")) {
653 /* Should warn here, but modpost runs before the linker */
655 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
658 /* undefined symbol */
659 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
660 ELF_ST_BIND(sym->st_info) != STB_WEAK)
662 if (ignore_undef_symbol(info, symname))
664 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
665 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
666 /* add compatibility with older glibc */
667 #ifndef STT_SPARC_REGISTER
668 #define STT_SPARC_REGISTER STT_REGISTER
670 if (info->hdr->e_machine == EM_SPARC ||
671 info->hdr->e_machine == EM_SPARCV9) {
672 /* Ignore register directives. */
673 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
675 if (symname[0] == '.') {
676 char *munged = NOFAIL(strdup(symname));
678 munged[1] = toupper(munged[1]);
685 const char *e = is_vmlinux(mod->name) ?"":".ko";
686 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
687 symname + strlen("__crc_"), mod->name, e);
689 mod->unres = alloc_symbol(symname,
690 ELF_ST_BIND(sym->st_info) == STB_WEAK,
694 /* All exported symbols */
695 if (strstarts(symname, "__ksymtab_")) {
696 sym_add_exported(symname + strlen("__ksymtab_"), mod,
699 if (strcmp(symname, "init_module") == 0)
701 if (strcmp(symname, "cleanup_module") == 0)
702 mod->has_cleanup = 1;
708 * Parse tag=value strings from .modinfo section
710 static char *next_string(char *string, unsigned long *secsize)
712 /* Skip non-zero chars */
715 if ((*secsize)-- <= 1)
719 /* Skip any zero padding. */
722 if ((*secsize)-- <= 1)
728 static char *get_next_modinfo(struct elf_info *info, const char *tag,
732 unsigned int taglen = strlen(tag);
733 char *modinfo = info->modinfo;
734 unsigned long size = info->modinfo_len;
737 size -= prev - modinfo;
738 modinfo = next_string(prev, &size);
741 for (p = modinfo; p; p = next_string(p, &size)) {
742 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
743 return p + taglen + 1;
748 static char *get_modinfo(struct elf_info *info, const char *tag)
751 return get_next_modinfo(info, tag, NULL);
755 * Test if string s ends in string sub
758 static int strrcmp(const char *s, const char *sub)
766 sublen = strlen(sub);
768 if ((slen == 0) || (sublen == 0))
774 return memcmp(s + slen - sublen, sub, sublen);
777 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
780 return elf->strtab + sym->st_name;
785 /* The pattern is an array of simple patterns.
786 * "foo" will match an exact string equal to "foo"
787 * "*foo" will match a string that ends with "foo"
788 * "foo*" will match a string that begins with "foo"
789 * "*foo*" will match a string that contains "foo"
791 static int match(const char *sym, const char * const pat[])
796 const char *endp = p + strlen(p) - 1;
799 if (*p == '*' && *endp == '*') {
800 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
801 char *here = strstr(sym, bare);
808 else if (*p == '*') {
809 if (strrcmp(sym, p + 1) == 0)
813 else if (*endp == '*') {
814 if (strncmp(sym, p, strlen(p) - 1) == 0)
819 if (strcmp(p, sym) == 0)
827 /* sections that we do not want to do full section mismatch check on */
828 static const char *const section_white_list[] =
832 ".cranges", /* sh64 */
833 ".zdebug*", /* Compressed debug sections. */
834 ".GCC.command.line", /* record-gcc-switches */
835 ".mdebug*", /* alpha, score, mips etc. */
836 ".pdr", /* alpha, score, mips etc. */
841 ".xt.prop", /* xtensa */
842 ".xt.lit", /* xtensa */
843 ".arcextmap*", /* arc */
844 ".gnu.linkonce.arcext*", /* arc : modules */
845 ".cmem*", /* EZchip */
846 ".fmt_slot*", /* EZchip */
853 * This is used to find sections missing the SHF_ALLOC flag.
854 * The cause of this is often a section specified in assembler
855 * without "ax" / "aw".
857 static void check_section(const char *modname, struct elf_info *elf,
860 const char *sec = sech_name(elf, sechdr);
862 if (sechdr->sh_type == SHT_PROGBITS &&
863 !(sechdr->sh_flags & SHF_ALLOC) &&
864 !match(sec, section_white_list)) {
865 warn("%s (%s): unexpected non-allocatable section.\n"
866 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
867 "Note that for example <linux/init.h> contains\n"
868 "section definitions for use in .S files.\n\n",
875 #define ALL_INIT_DATA_SECTIONS \
876 ".init.setup", ".init.rodata", ".meminit.rodata", \
877 ".init.data", ".meminit.data"
878 #define ALL_EXIT_DATA_SECTIONS \
879 ".exit.data", ".memexit.data"
881 #define ALL_INIT_TEXT_SECTIONS \
882 ".init.text", ".meminit.text"
883 #define ALL_EXIT_TEXT_SECTIONS \
884 ".exit.text", ".memexit.text"
886 #define ALL_PCI_INIT_SECTIONS \
887 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
888 ".pci_fixup_enable", ".pci_fixup_resume", \
889 ".pci_fixup_resume_early", ".pci_fixup_suspend"
891 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
892 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
894 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
895 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
897 #define DATA_SECTIONS ".data", ".data.rel"
898 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
899 ".kprobes.text", ".cpuidle.text"
900 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
901 ".fixup", ".entry.text", ".exception.text", ".text.*", \
904 #define INIT_SECTIONS ".init.*"
905 #define MEM_INIT_SECTIONS ".meminit.*"
907 #define EXIT_SECTIONS ".exit.*"
908 #define MEM_EXIT_SECTIONS ".memexit.*"
910 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
911 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
913 /* init data sections */
914 static const char *const init_data_sections[] =
915 { ALL_INIT_DATA_SECTIONS, NULL };
917 /* all init sections */
918 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
920 /* All init and exit sections (code + data) */
921 static const char *const init_exit_sections[] =
922 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
924 /* all text sections */
925 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
928 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
931 /* symbols in .data that may refer to init/exit sections */
932 #define DEFAULT_SYMBOL_WHITE_LIST \
934 "*_template", /* scsi uses *_template a lot */ \
935 "*_timer", /* arm uses ops structures named _timer a lot */ \
936 "*_sht", /* scsi also used *_sht to some extent */ \
942 static const char *const head_sections[] = { ".head.text*", NULL };
943 static const char *const linker_symbols[] =
944 { "__init_begin", "_sinittext", "_einittext", NULL };
945 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
952 XXXINIT_TO_SOME_INIT,
953 XXXEXIT_TO_SOME_EXIT,
954 ANY_INIT_TO_ANY_EXIT,
955 ANY_EXIT_TO_ANY_INIT,
961 * Describe how to match sections on different criterias:
963 * @fromsec: Array of sections to be matched.
965 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
966 * this array is forbidden (black-list). Can be empty.
968 * @good_tosec: Relocations applied to a section in @fromsec must be
969 * targetting sections in this array (white-list). Can be empty.
971 * @mismatch: Type of mismatch.
973 * @symbol_white_list: Do not match a relocation to a symbol in this list
974 * even if it is targetting a section in @bad_to_sec.
976 * @handler: Specific handler to call when a match is found. If NULL,
977 * default_mismatch_handler() will be called.
980 struct sectioncheck {
981 const char *fromsec[20];
982 const char *bad_tosec[20];
983 const char *good_tosec[20];
984 enum mismatch mismatch;
985 const char *symbol_white_list[20];
986 void (*handler)(const char *modname, struct elf_info *elf,
987 const struct sectioncheck* const mismatch,
988 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
992 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
993 const struct sectioncheck* const mismatch,
994 Elf_Rela *r, Elf_Sym *sym,
995 const char *fromsec);
997 static const struct sectioncheck sectioncheck[] = {
998 /* Do not reference init/exit code/data from
999 * normal code and data
1002 .fromsec = { TEXT_SECTIONS, NULL },
1003 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1004 .mismatch = TEXT_TO_ANY_INIT,
1005 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1008 .fromsec = { DATA_SECTIONS, NULL },
1009 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1010 .mismatch = DATA_TO_ANY_INIT,
1011 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1014 .fromsec = { DATA_SECTIONS, NULL },
1015 .bad_tosec = { INIT_SECTIONS, NULL },
1016 .mismatch = DATA_TO_ANY_INIT,
1017 .symbol_white_list = {
1018 "*_template", "*_timer", "*_sht", "*_ops",
1019 "*_probe", "*_probe_one", "*_console", NULL
1023 .fromsec = { TEXT_SECTIONS, NULL },
1024 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1025 .mismatch = TEXT_TO_ANY_EXIT,
1026 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1029 .fromsec = { DATA_SECTIONS, NULL },
1030 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1031 .mismatch = DATA_TO_ANY_EXIT,
1032 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1034 /* Do not reference init code/data from meminit code/data */
1036 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1037 .bad_tosec = { INIT_SECTIONS, NULL },
1038 .mismatch = XXXINIT_TO_SOME_INIT,
1039 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1041 /* Do not reference exit code/data from memexit code/data */
1043 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1044 .bad_tosec = { EXIT_SECTIONS, NULL },
1045 .mismatch = XXXEXIT_TO_SOME_EXIT,
1046 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1048 /* Do not use exit code/data from init code */
1050 .fromsec = { ALL_INIT_SECTIONS, NULL },
1051 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1052 .mismatch = ANY_INIT_TO_ANY_EXIT,
1053 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1055 /* Do not use init code/data from exit code */
1057 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1058 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1059 .mismatch = ANY_EXIT_TO_ANY_INIT,
1060 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1063 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1064 .bad_tosec = { INIT_SECTIONS, NULL },
1065 .mismatch = ANY_INIT_TO_ANY_EXIT,
1066 .symbol_white_list = { NULL },
1068 /* Do not export init/exit functions or data */
1070 .fromsec = { "__ksymtab*", NULL },
1071 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1072 .mismatch = EXPORT_TO_INIT_EXIT,
1073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1076 .fromsec = { "__ex_table", NULL },
1077 /* If you're adding any new black-listed sections in here, consider
1078 * adding a special 'printer' for them in scripts/check_extable.
1080 .bad_tosec = { ".altinstr_replacement", NULL },
1081 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1082 .mismatch = EXTABLE_TO_NON_TEXT,
1083 .handler = extable_mismatch_handler,
1087 static const struct sectioncheck *section_mismatch(
1088 const char *fromsec, const char *tosec)
1091 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1092 const struct sectioncheck *check = §ioncheck[0];
1095 * The target section could be the SHT_NUL section when we're
1096 * handling relocations to un-resolved symbols, trying to match it
1097 * doesn't make much sense and causes build failures on parisc
1103 for (i = 0; i < elems; i++) {
1104 if (match(fromsec, check->fromsec)) {
1105 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1107 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1116 * Whitelist to allow certain references to pass with no warning.
1119 * If a module parameter is declared __initdata and permissions=0
1120 * then this is legal despite the warning generated.
1121 * We cannot see value of permissions here, so just ignore
1123 * The pattern is identified by:
1124 * tosec = .init.data
1129 * module_param_call() ops can refer to __init set function if permissions=0
1130 * The pattern is identified by:
1131 * tosec = .init.text
1133 * atsym = __param_ops_*
1136 * Many drivers utilise a *driver container with references to
1137 * add, remove, probe functions etc.
1138 * the pattern is identified by:
1139 * tosec = init or exit section
1140 * fromsec = data section
1141 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1142 * *probe_one, *_console, *_timer
1145 * Whitelist all references from .head.text to any init section
1148 * Some symbols belong to init section but still it is ok to reference
1149 * these from non-init sections as these symbols don't have any memory
1150 * allocated for them and symbol address and value are same. So even
1151 * if init section is freed, its ok to reference those symbols.
1152 * For ex. symbols marking the init section boundaries.
1153 * This pattern is identified by
1154 * refsymname = __init_begin, _sinittext, _einittext
1157 * GCC may optimize static inlines when fed constant arg(s) resulting
1158 * in functions like cpumask_empty() -- generating an associated symbol
1159 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1160 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1161 * meaningless section warning. May need to add isra symbols too...
1162 * This pattern is identified by
1163 * tosec = init section
1164 * fromsec = text section
1165 * refsymname = *.constprop.*
1168 * Hide section mismatch warnings for ELF local symbols. The goal
1169 * is to eliminate false positive modpost warnings caused by
1170 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1171 * Autogenerated symbol names bypass modpost's "Pattern 2"
1172 * whitelisting, which relies on pattern-matching against symbol
1173 * names to work. (One situation where gcc can autogenerate ELF
1174 * local symbols is when "-fsection-anchors" is used.)
1176 static int secref_whitelist(const struct sectioncheck *mismatch,
1177 const char *fromsec, const char *fromsym,
1178 const char *tosec, const char *tosym)
1180 /* Check for pattern 1 */
1181 if (match(tosec, init_data_sections) &&
1182 match(fromsec, data_sections) &&
1183 strstarts(fromsym, "__param"))
1186 /* Check for pattern 1a */
1187 if (strcmp(tosec, ".init.text") == 0 &&
1188 match(fromsec, data_sections) &&
1189 strstarts(fromsym, "__param_ops_"))
1192 /* Check for pattern 2 */
1193 if (match(tosec, init_exit_sections) &&
1194 match(fromsec, data_sections) &&
1195 match(fromsym, mismatch->symbol_white_list))
1198 /* Check for pattern 3 */
1199 if (match(fromsec, head_sections) &&
1200 match(tosec, init_sections))
1203 /* Check for pattern 4 */
1204 if (match(tosym, linker_symbols))
1207 /* Check for pattern 5 */
1208 if (match(fromsec, text_sections) &&
1209 match(tosec, init_sections) &&
1210 match(fromsym, optim_symbols))
1213 /* Check for pattern 6 */
1214 if (strstarts(fromsym, ".L"))
1220 static inline int is_arm_mapping_symbol(const char *str)
1222 return str[0] == '$' && strchr("axtd", str[1])
1223 && (str[2] == '\0' || str[2] == '.');
1227 * If there's no name there, ignore it; likewise, ignore it if it's
1228 * one of the magic symbols emitted used by current ARM tools.
1230 * Otherwise if find_symbols_between() returns those symbols, they'll
1231 * fail the whitelist tests and cause lots of false alarms ... fixable
1232 * only by merging __exit and __init sections into __text, bloating
1233 * the kernel (which is especially evil on embedded platforms).
1235 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1237 const char *name = elf->strtab + sym->st_name;
1239 if (!name || !strlen(name))
1241 return !is_arm_mapping_symbol(name);
1245 * Find symbol based on relocation record info.
1246 * In some cases the symbol supplied is a valid symbol so
1247 * return refsym. If st_name != 0 we assume this is a valid symbol.
1248 * In other cases the symbol needs to be looked up in the symbol table
1249 * based on section and address.
1251 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1255 Elf_Sym *near = NULL;
1256 Elf64_Sword distance = 20;
1258 unsigned int relsym_secindex;
1260 if (relsym->st_name != 0)
1263 relsym_secindex = get_secindex(elf, relsym);
1264 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1265 if (get_secindex(elf, sym) != relsym_secindex)
1267 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1269 if (!is_valid_name(elf, sym))
1271 if (sym->st_value == addr)
1273 /* Find a symbol nearby - addr are maybe negative */
1274 d = sym->st_value - addr;
1276 d = addr - sym->st_value;
1282 /* We need a close match */
1290 * Find symbols before or equal addr and after addr - in the section sec.
1291 * If we find two symbols with equal offset prefer one with a valid name.
1292 * The ELF format may have a better way to detect what type of symbol
1293 * it is, but this works for now.
1295 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1299 Elf_Sym *near = NULL;
1300 Elf_Addr distance = ~0;
1302 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1305 if (is_shndx_special(sym->st_shndx))
1307 symsec = sec_name(elf, get_secindex(elf, sym));
1308 if (strcmp(symsec, sec) != 0)
1310 if (!is_valid_name(elf, sym))
1312 if (sym->st_value <= addr) {
1313 if ((addr - sym->st_value) < distance) {
1314 distance = addr - sym->st_value;
1316 } else if ((addr - sym->st_value) == distance) {
1325 * Convert a section name to the function/data attribute
1326 * .init.text => __init
1327 * .memexitconst => __memconst
1330 * The memory of returned value has been allocated on a heap. The user of this
1331 * method should free it after usage.
1333 static char *sec2annotation(const char *s)
1335 if (match(s, init_exit_sections)) {
1336 char *p = NOFAIL(malloc(20));
1343 while (*s && *s != '.')
1348 if (strstr(s, "rodata") != NULL)
1349 strcat(p, "const ");
1350 else if (strstr(s, "data") != NULL)
1356 return NOFAIL(strdup(""));
1360 static int is_function(Elf_Sym *sym)
1363 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1368 static void print_section_list(const char * const list[20])
1370 const char *const *s = list;
1373 fprintf(stderr, "%s", *s);
1376 fprintf(stderr, ", ");
1378 fprintf(stderr, "\n");
1381 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1384 case 0: *name = "variable"; *name_p = ""; break;
1385 case 1: *name = "function"; *name_p = "()"; break;
1386 default: *name = "(unknown reference)"; *name_p = ""; break;
1391 * Print a warning about a section mismatch.
1392 * Try to find symbols near it so user can find it.
1393 * Check whitelist before warning - it may be a false positive.
1395 static void report_sec_mismatch(const char *modname,
1396 const struct sectioncheck *mismatch,
1397 const char *fromsec,
1398 unsigned long long fromaddr,
1399 const char *fromsym,
1401 const char *tosec, const char *tosym,
1404 const char *from, *from_p;
1405 const char *to, *to_p;
1409 sec_mismatch_count++;
1411 get_pretty_name(from_is_func, &from, &from_p);
1412 get_pretty_name(to_is_func, &to, &to_p);
1414 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1415 "to the %s %s:%s%s\n",
1416 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1419 switch (mismatch->mismatch) {
1420 case TEXT_TO_ANY_INIT:
1421 prl_from = sec2annotation(fromsec);
1422 prl_to = sec2annotation(tosec);
1424 "The function %s%s() references\n"
1426 "This is often because %s lacks a %s\n"
1427 "annotation or the annotation of %s is wrong.\n",
1429 to, prl_to, tosym, to_p,
1430 fromsym, prl_to, tosym);
1434 case DATA_TO_ANY_INIT: {
1435 prl_to = sec2annotation(tosec);
1437 "The variable %s references\n"
1439 "If the reference is valid then annotate the\n"
1440 "variable with __init* or __refdata (see linux/init.h) "
1441 "or name the variable:\n",
1442 fromsym, to, prl_to, tosym, to_p);
1443 print_section_list(mismatch->symbol_white_list);
1447 case TEXT_TO_ANY_EXIT:
1448 prl_to = sec2annotation(tosec);
1450 "The function %s() references a %s in an exit section.\n"
1451 "Often the %s %s%s has valid usage outside the exit section\n"
1452 "and the fix is to remove the %sannotation of %s.\n",
1453 fromsym, to, to, tosym, to_p, prl_to, tosym);
1456 case DATA_TO_ANY_EXIT: {
1457 prl_to = sec2annotation(tosec);
1459 "The variable %s references\n"
1461 "If the reference is valid then annotate the\n"
1462 "variable with __exit* (see linux/init.h) or "
1463 "name the variable:\n",
1464 fromsym, to, prl_to, tosym, to_p);
1465 print_section_list(mismatch->symbol_white_list);
1469 case XXXINIT_TO_SOME_INIT:
1470 case XXXEXIT_TO_SOME_EXIT:
1471 prl_from = sec2annotation(fromsec);
1472 prl_to = sec2annotation(tosec);
1474 "The %s %s%s%s references\n"
1476 "If %s is only used by %s then\n"
1477 "annotate %s with a matching annotation.\n",
1478 from, prl_from, fromsym, from_p,
1479 to, prl_to, tosym, to_p,
1480 tosym, fromsym, tosym);
1484 case ANY_INIT_TO_ANY_EXIT:
1485 prl_from = sec2annotation(fromsec);
1486 prl_to = sec2annotation(tosec);
1488 "The %s %s%s%s references\n"
1490 "This is often seen when error handling "
1491 "in the init function\n"
1492 "uses functionality in the exit path.\n"
1493 "The fix is often to remove the %sannotation of\n"
1494 "%s%s so it may be used outside an exit section.\n",
1495 from, prl_from, fromsym, from_p,
1496 to, prl_to, tosym, to_p,
1497 prl_to, tosym, to_p);
1501 case ANY_EXIT_TO_ANY_INIT:
1502 prl_from = sec2annotation(fromsec);
1503 prl_to = sec2annotation(tosec);
1505 "The %s %s%s%s references\n"
1507 "This is often seen when error handling "
1508 "in the exit function\n"
1509 "uses functionality in the init path.\n"
1510 "The fix is often to remove the %sannotation of\n"
1511 "%s%s so it may be used outside an init section.\n",
1512 from, prl_from, fromsym, from_p,
1513 to, prl_to, tosym, to_p,
1514 prl_to, tosym, to_p);
1518 case EXPORT_TO_INIT_EXIT:
1519 prl_to = sec2annotation(tosec);
1521 "The symbol %s is exported and annotated %s\n"
1522 "Fix this by removing the %sannotation of %s "
1523 "or drop the export.\n",
1524 tosym, prl_to, prl_to, tosym);
1527 case EXTABLE_TO_NON_TEXT:
1528 fatal("There's a special handler for this mismatch type, "
1529 "we should never get here.");
1532 fprintf(stderr, "\n");
1535 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1536 const struct sectioncheck* const mismatch,
1537 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1543 const char *fromsym;
1545 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1546 fromsym = sym_name(elf, from);
1548 if (strstarts(fromsym, "reference___initcall"))
1551 tosec = sec_name(elf, get_secindex(elf, sym));
1552 to = find_elf_symbol(elf, r->r_addend, sym);
1553 tosym = sym_name(elf, to);
1555 /* check whitelist - we may ignore it */
1556 if (secref_whitelist(mismatch,
1557 fromsec, fromsym, tosec, tosym)) {
1558 report_sec_mismatch(modname, mismatch,
1559 fromsec, r->r_offset, fromsym,
1560 is_function(from), tosec, tosym,
1565 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1567 if (section_index > elf->num_sections)
1568 fatal("section_index is outside elf->num_sections!\n");
1570 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1574 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1575 * to know the sizeof(struct exception_table_entry) for the target architecture.
1577 static unsigned int extable_entry_size = 0;
1578 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1581 * If we're currently checking the second relocation within __ex_table,
1582 * that relocation offset tells us the offsetof(struct
1583 * exception_table_entry, fixup) which is equal to sizeof(struct
1584 * exception_table_entry) divided by two. We use that to our advantage
1585 * since there's no portable way to get that size as every architecture
1586 * seems to go with different sized types. Not pretty but better than
1587 * hard-coding the size for every architecture..
1589 if (!extable_entry_size)
1590 extable_entry_size = r->r_offset * 2;
1593 static inline bool is_extable_fault_address(Elf_Rela *r)
1596 * extable_entry_size is only discovered after we've handled the
1597 * _second_ relocation in __ex_table, so only abort when we're not
1598 * handling the first reloc and extable_entry_size is zero.
1600 if (r->r_offset && extable_entry_size == 0)
1601 fatal("extable_entry size hasn't been discovered!\n");
1603 return ((r->r_offset == 0) ||
1604 (r->r_offset % extable_entry_size == 0));
1607 #define is_second_extable_reloc(Start, Cur, Sec) \
1608 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1610 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1611 const struct sectioncheck* const mismatch,
1612 Elf_Rela* r, Elf_Sym* sym,
1613 const char* fromsec, const char* tosec)
1615 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1616 const char* fromsym_name = sym_name(elf, fromsym);
1617 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1618 const char* tosym_name = sym_name(elf, tosym);
1619 const char* from_pretty_name;
1620 const char* from_pretty_name_p;
1621 const char* to_pretty_name;
1622 const char* to_pretty_name_p;
1624 get_pretty_name(is_function(fromsym),
1625 &from_pretty_name, &from_pretty_name_p);
1626 get_pretty_name(is_function(tosym),
1627 &to_pretty_name, &to_pretty_name_p);
1629 warn("%s(%s+0x%lx): Section mismatch in reference"
1630 " from the %s %s%s to the %s %s:%s%s\n",
1631 modname, fromsec, (long)r->r_offset, from_pretty_name,
1632 fromsym_name, from_pretty_name_p,
1633 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1635 if (!match(tosec, mismatch->bad_tosec) &&
1636 is_executable_section(elf, get_secindex(elf, sym)))
1638 "The relocation at %s+0x%lx references\n"
1639 "section \"%s\" which is not in the list of\n"
1640 "authorized sections. If you're adding a new section\n"
1641 "and/or if this reference is valid, add \"%s\" to the\n"
1642 "list of authorized sections to jump to on fault.\n"
1643 "This can be achieved by adding \"%s\" to \n"
1644 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1645 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1648 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1649 const struct sectioncheck* const mismatch,
1650 Elf_Rela* r, Elf_Sym* sym,
1651 const char *fromsec)
1653 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1655 sec_mismatch_count++;
1657 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1659 if (match(tosec, mismatch->bad_tosec))
1660 fatal("The relocation at %s+0x%lx references\n"
1661 "section \"%s\" which is black-listed.\n"
1662 "Something is seriously wrong and should be fixed.\n"
1663 "You might get more information about where this is\n"
1664 "coming from by using scripts/check_extable.sh %s\n",
1665 fromsec, (long)r->r_offset, tosec, modname);
1666 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1667 if (is_extable_fault_address(r))
1668 fatal("The relocation at %s+0x%lx references\n"
1669 "section \"%s\" which is not executable, IOW\n"
1670 "it is not possible for the kernel to fault\n"
1671 "at that address. Something is seriously wrong\n"
1672 "and should be fixed.\n",
1673 fromsec, (long)r->r_offset, tosec);
1675 fatal("The relocation at %s+0x%lx references\n"
1676 "section \"%s\" which is not executable, IOW\n"
1677 "the kernel will fault if it ever tries to\n"
1678 "jump to it. Something is seriously wrong\n"
1679 "and should be fixed.\n",
1680 fromsec, (long)r->r_offset, tosec);
1684 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1685 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1687 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1688 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1691 if (mismatch->handler)
1692 mismatch->handler(modname, elf, mismatch,
1695 default_mismatch_handler(modname, elf, mismatch,
1700 static unsigned int *reloc_location(struct elf_info *elf,
1701 Elf_Shdr *sechdr, Elf_Rela *r)
1703 Elf_Shdr *sechdrs = elf->sechdrs;
1704 int section = sechdr->sh_info;
1706 return (void *)elf->hdr + sechdrs[section].sh_offset +
1710 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1712 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1713 unsigned int *location = reloc_location(elf, sechdr, r);
1717 r->r_addend = TO_NATIVE(*location);
1720 r->r_addend = TO_NATIVE(*location) + 4;
1721 /* For CONFIG_RELOCATABLE=y */
1722 if (elf->hdr->e_type == ET_EXEC)
1723 r->r_addend += r->r_offset;
1730 #define R_ARM_CALL 28
1732 #ifndef R_ARM_JUMP24
1733 #define R_ARM_JUMP24 29
1736 #ifndef R_ARM_THM_CALL
1737 #define R_ARM_THM_CALL 10
1739 #ifndef R_ARM_THM_JUMP24
1740 #define R_ARM_THM_JUMP24 30
1742 #ifndef R_ARM_THM_JUMP19
1743 #define R_ARM_THM_JUMP19 51
1746 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1748 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1752 /* From ARM ABI: (S + A) | T */
1753 r->r_addend = (int)(long)
1754 (elf->symtab_start + ELF_R_SYM(r->r_info));
1759 case R_ARM_THM_CALL:
1760 case R_ARM_THM_JUMP24:
1761 case R_ARM_THM_JUMP19:
1762 /* From ARM ABI: ((S + A) | T) - P */
1763 r->r_addend = (int)(long)(elf->hdr +
1765 (r->r_offset - sechdr->sh_addr));
1773 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1775 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1776 unsigned int *location = reloc_location(elf, sechdr, r);
1779 if (r_typ == R_MIPS_HI16)
1780 return 1; /* skip this */
1781 inst = TO_NATIVE(*location);
1784 r->r_addend = inst & 0xffff;
1787 r->r_addend = (inst & 0x03ffffff) << 2;
1796 static void section_rela(const char *modname, struct elf_info *elf,
1803 const char *fromsec;
1805 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1806 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1808 fromsec = sech_name(elf, sechdr);
1809 fromsec += strlen(".rela");
1810 /* if from section (name) is know good then skip it */
1811 if (match(fromsec, section_white_list))
1814 for (rela = start; rela < stop; rela++) {
1815 r.r_offset = TO_NATIVE(rela->r_offset);
1816 #if KERNEL_ELFCLASS == ELFCLASS64
1817 if (elf->hdr->e_machine == EM_MIPS) {
1819 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1820 r_sym = TO_NATIVE(r_sym);
1821 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1822 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1824 r.r_info = TO_NATIVE(rela->r_info);
1825 r_sym = ELF_R_SYM(r.r_info);
1828 r.r_info = TO_NATIVE(rela->r_info);
1829 r_sym = ELF_R_SYM(r.r_info);
1831 r.r_addend = TO_NATIVE(rela->r_addend);
1832 sym = elf->symtab_start + r_sym;
1833 /* Skip special sections */
1834 if (is_shndx_special(sym->st_shndx))
1836 if (is_second_extable_reloc(start, rela, fromsec))
1837 find_extable_entry_size(fromsec, &r);
1838 check_section_mismatch(modname, elf, &r, sym, fromsec);
1842 static void section_rel(const char *modname, struct elf_info *elf,
1849 const char *fromsec;
1851 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1852 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1854 fromsec = sech_name(elf, sechdr);
1855 fromsec += strlen(".rel");
1856 /* if from section (name) is know good then skip it */
1857 if (match(fromsec, section_white_list))
1860 for (rel = start; rel < stop; rel++) {
1861 r.r_offset = TO_NATIVE(rel->r_offset);
1862 #if KERNEL_ELFCLASS == ELFCLASS64
1863 if (elf->hdr->e_machine == EM_MIPS) {
1865 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1866 r_sym = TO_NATIVE(r_sym);
1867 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1868 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1870 r.r_info = TO_NATIVE(rel->r_info);
1871 r_sym = ELF_R_SYM(r.r_info);
1874 r.r_info = TO_NATIVE(rel->r_info);
1875 r_sym = ELF_R_SYM(r.r_info);
1878 switch (elf->hdr->e_machine) {
1880 if (addend_386_rel(elf, sechdr, &r))
1884 if (addend_arm_rel(elf, sechdr, &r))
1888 if (addend_mips_rel(elf, sechdr, &r))
1892 sym = elf->symtab_start + r_sym;
1893 /* Skip special sections */
1894 if (is_shndx_special(sym->st_shndx))
1896 if (is_second_extable_reloc(start, rel, fromsec))
1897 find_extable_entry_size(fromsec, &r);
1898 check_section_mismatch(modname, elf, &r, sym, fromsec);
1903 * A module includes a number of sections that are discarded
1904 * either when loaded or when used as built-in.
1905 * For loaded modules all functions marked __init and all data
1906 * marked __initdata will be discarded when the module has been initialized.
1907 * Likewise for modules used built-in the sections marked __exit
1908 * are discarded because __exit marked function are supposed to be called
1909 * only when a module is unloaded which never happens for built-in modules.
1910 * The check_sec_ref() function traverses all relocation records
1911 * to find all references to a section that reference a section that will
1912 * be discarded and warns about it.
1914 static void check_sec_ref(struct module *mod, const char *modname,
1915 struct elf_info *elf)
1918 Elf_Shdr *sechdrs = elf->sechdrs;
1920 /* Walk through all sections */
1921 for (i = 0; i < elf->num_sections; i++) {
1922 check_section(modname, elf, &elf->sechdrs[i]);
1923 /* We want to process only relocation sections and not .init */
1924 if (sechdrs[i].sh_type == SHT_RELA)
1925 section_rela(modname, elf, &elf->sechdrs[i]);
1926 else if (sechdrs[i].sh_type == SHT_REL)
1927 section_rel(modname, elf, &elf->sechdrs[i]);
1931 static char *remove_dot(char *s)
1933 size_t n = strcspn(s, ".");
1936 size_t m = strspn(s + n + 1, "0123456789");
1937 if (m && (s[n + m] == '.' || s[n + m] == 0))
1943 static void read_symbols(const char *modname)
1945 const char *symname;
1949 struct elf_info info = { };
1952 if (!parse_elf(&info, modname))
1955 mod = new_module(modname);
1957 /* When there's no vmlinux, don't print warnings about
1958 * unresolved symbols (since there'll be too many ;) */
1959 if (is_vmlinux(modname)) {
1964 license = get_modinfo(&info, "license");
1965 if (!license && !is_vmlinux(modname))
1966 warn("modpost: missing MODULE_LICENSE() in %s\n"
1967 "see include/linux/module.h for "
1968 "more information\n", modname);
1970 if (license_is_gpl_compatible(license))
1971 mod->gpl_compatible = 1;
1973 mod->gpl_compatible = 0;
1976 license = get_next_modinfo(&info, "license", license);
1979 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1980 symname = remove_dot(info.strtab + sym->st_name);
1982 handle_modversions(mod, &info, sym, symname);
1983 handle_moddevtable(mod, &info, sym, symname);
1986 // check for static EXPORT_SYMBOL_* functions && global vars
1987 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1988 unsigned char bind = ELF_ST_BIND(sym->st_info);
1990 if (bind == STB_GLOBAL || bind == STB_WEAK) {
1992 find_symbol(remove_dot(info.strtab +
2000 if (!is_vmlinux(modname) || vmlinux_section_warnings)
2001 check_sec_ref(mod, modname, &info);
2003 version = get_modinfo(&info, "version");
2005 maybe_frob_rcs_version(modname, version, info.modinfo,
2006 version - (char *)info.hdr);
2007 if (version || (all_versions && !is_vmlinux(modname)))
2008 get_src_version(modname, mod->srcversion,
2009 sizeof(mod->srcversion)-1);
2011 parse_elf_finish(&info);
2013 /* Our trick to get versioning for module struct etc. - it's
2014 * never passed as an argument to an exported function, so
2015 * the automatic versioning doesn't pick it up, but it's really
2016 * important anyhow */
2018 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
2021 static void read_symbols_from_files(const char *filename)
2024 char fname[PATH_MAX];
2026 if (strcmp(filename, "-") != 0) {
2027 in = fopen(filename, "r");
2029 fatal("Can't open filenames file %s: %m", filename);
2032 while (fgets(fname, PATH_MAX, in) != NULL) {
2033 if (strends(fname, "\n"))
2034 fname[strlen(fname)-1] = '\0';
2035 read_symbols(fname);
2044 /* We first write the generated file into memory using the
2045 * following helper, then compare to the file on disk and
2046 * only update the later if anything changed */
2048 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2049 const char *fmt, ...)
2056 len = vsnprintf(tmp, SZ, fmt, ap);
2057 buf_write(buf, tmp, len);
2061 void buf_write(struct buffer *buf, const char *s, int len)
2063 if (buf->size - buf->pos < len) {
2064 buf->size += len + SZ;
2065 buf->p = NOFAIL(realloc(buf->p, buf->size));
2067 strncpy(buf->p + buf->pos, s, len);
2071 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2073 const char *e = is_vmlinux(m) ?"":".ko";
2077 fatal("modpost: GPL-incompatible module %s%s "
2078 "uses GPL-only symbol '%s'\n", m, e, s);
2080 case export_unused_gpl:
2081 fatal("modpost: GPL-incompatible module %s%s "
2082 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2084 case export_gpl_future:
2085 warn("modpost: GPL-incompatible module %s%s "
2086 "uses future GPL-only symbol '%s'\n", m, e, s);
2090 case export_unknown:
2096 static void check_for_unused(enum export exp, const char *m, const char *s)
2098 const char *e = is_vmlinux(m) ?"":".ko";
2102 case export_unused_gpl:
2103 warn("modpost: module %s%s "
2104 "uses symbol '%s' marked UNUSED\n", m, e, s);
2112 static int check_exports(struct module *mod)
2114 struct symbol *s, *exp;
2117 for (s = mod->unres; s; s = s->next) {
2118 const char *basename;
2119 exp = find_symbol(s->name);
2120 if (!exp || exp->module == mod) {
2121 if (have_vmlinux && !s->weak) {
2122 if (warn_unresolved) {
2123 warn("\"%s\" [%s.ko] undefined!\n",
2124 s->name, mod->name);
2126 merror("\"%s\" [%s.ko] undefined!\n",
2127 s->name, mod->name);
2133 basename = strrchr(mod->name, '/');
2137 basename = mod->name;
2138 if (!mod->gpl_compatible)
2139 check_for_gpl_usage(exp->export, basename, exp->name);
2140 check_for_unused(exp->export, basename, exp->name);
2146 static int check_modname_len(struct module *mod)
2148 const char *mod_name;
2150 mod_name = strrchr(mod->name, '/');
2151 if (mod_name == NULL)
2152 mod_name = mod->name;
2155 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2156 merror("module name is too long [%s.ko]\n", mod->name);
2164 * Header for the generated file
2166 static void add_header(struct buffer *b, struct module *mod)
2168 buf_printf(b, "#include <linux/build-salt.h>\n");
2169 buf_printf(b, "#include <linux/module.h>\n");
2170 buf_printf(b, "#include <linux/vermagic.h>\n");
2171 buf_printf(b, "#include <linux/compiler.h>\n");
2172 buf_printf(b, "\n");
2173 buf_printf(b, "BUILD_SALT;\n");
2174 buf_printf(b, "\n");
2175 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2176 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2177 buf_printf(b, "\n");
2178 buf_printf(b, "__visible struct module __this_module\n");
2179 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
2180 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2182 buf_printf(b, "\t.init = init_module,\n");
2183 if (mod->has_cleanup)
2184 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2185 "\t.exit = cleanup_module,\n"
2187 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2188 buf_printf(b, "};\n");
2191 static void add_intree_flag(struct buffer *b, int is_intree)
2194 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2197 /* Cannot check for assembler */
2198 static void add_retpoline(struct buffer *b)
2200 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2201 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2202 buf_printf(b, "#endif\n");
2205 static void add_staging_flag(struct buffer *b, const char *name)
2207 if (strstarts(name, "drivers/staging"))
2208 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2212 * Record CRCs for unresolved symbols
2214 static int add_versions(struct buffer *b, struct module *mod)
2216 struct symbol *s, *exp;
2219 for (s = mod->unres; s; s = s->next) {
2220 exp = find_symbol(s->name);
2221 if (!exp || exp->module == mod)
2223 s->module = exp->module;
2224 s->crc_valid = exp->crc_valid;
2231 buf_printf(b, "\n");
2232 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2233 buf_printf(b, "__used __section(__versions) = {\n");
2235 for (s = mod->unres; s; s = s->next) {
2238 if (!s->crc_valid) {
2239 warn("\"%s\" [%s.ko] has no CRC!\n",
2240 s->name, mod->name);
2243 if (strlen(s->name) >= MODULE_NAME_LEN) {
2244 merror("too long symbol \"%s\" [%s.ko]\n",
2245 s->name, mod->name);
2249 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2253 buf_printf(b, "};\n");
2258 static void add_depends(struct buffer *b, struct module *mod)
2263 /* Clear ->seen flag of modules that own symbols needed by this. */
2264 for (s = mod->unres; s; s = s->next)
2266 s->module->seen = is_vmlinux(s->module->name);
2268 buf_printf(b, "\n");
2269 buf_printf(b, "MODULE_INFO(depends, \"");
2270 for (s = mod->unres; s; s = s->next) {
2275 if (s->module->seen)
2278 s->module->seen = 1;
2279 p = strrchr(s->module->name, '/');
2283 p = s->module->name;
2284 buf_printf(b, "%s%s", first ? "" : ",", p);
2287 buf_printf(b, "\");\n");
2290 static void add_srcversion(struct buffer *b, struct module *mod)
2292 if (mod->srcversion[0]) {
2293 buf_printf(b, "\n");
2294 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2299 static void write_if_changed(struct buffer *b, const char *fname)
2305 file = fopen(fname, "r");
2309 if (fstat(fileno(file), &st) < 0)
2312 if (st.st_size != b->pos)
2315 tmp = NOFAIL(malloc(b->pos));
2316 if (fread(tmp, 1, b->pos, file) != b->pos)
2319 if (memcmp(tmp, b->p, b->pos) != 0)
2331 file = fopen(fname, "w");
2336 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2343 /* parse Module.symvers file. line format:
2344 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
2346 static void read_dump(const char *fname, unsigned int kernel)
2348 unsigned long size, pos = 0;
2349 void *file = grab_file(fname, &size);
2353 /* No symbol versions, silently ignore */
2356 while ((line = get_next_line(&pos, file, size))) {
2357 char *symname, *modname, *d, *export, *end;
2362 if (!(symname = strchr(line, '\t')))
2365 if (!(modname = strchr(symname, '\t')))
2368 if ((export = strchr(modname, '\t')) != NULL)
2370 if (export && ((end = strchr(export, '\t')) != NULL))
2372 crc = strtoul(line, &d, 16);
2373 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2375 mod = find_module(modname);
2377 if (is_vmlinux(modname))
2379 mod = new_module(modname);
2382 s = sym_add_exported(symname, mod, export_no(export));
2386 sym_update_crc(symname, mod, crc, export_no(export));
2388 release_file(file, size);
2391 release_file(file, size);
2392 fatal("parse error in symbol dump file\n");
2395 /* For normal builds always dump all symbols.
2396 * For external modules only dump symbols
2397 * that are not read from kernel Module.symvers.
2399 static int dump_sym(struct symbol *sym)
2401 if (!external_module)
2403 if (sym->vmlinux || sym->kernel)
2408 static void write_dump(const char *fname)
2410 struct buffer buf = { };
2411 struct symbol *symbol;
2414 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2415 symbol = symbolhash[n];
2417 if (dump_sym(symbol))
2418 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
2419 symbol->crc, symbol->name,
2420 symbol->module->name,
2421 export_str(symbol->export));
2422 symbol = symbol->next;
2425 write_if_changed(&buf, fname);
2429 struct ext_sym_list {
2430 struct ext_sym_list *next;
2434 int main(int argc, char **argv)
2437 struct buffer buf = { };
2438 char *kernel_read = NULL, *module_read = NULL;
2439 char *dump_write = NULL, *files_source = NULL;
2443 struct ext_sym_list *extsym_iter;
2444 struct ext_sym_list *extsym_start = NULL;
2446 while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
2449 kernel_read = optarg;
2452 module_read = optarg;
2453 external_module = 1;
2456 external_module = 1;
2458 NOFAIL(malloc(sizeof(*extsym_iter)));
2459 extsym_iter->next = extsym_start;
2460 extsym_iter->file = optarg;
2461 extsym_start = extsym_iter;
2467 ignore_missing_files = 1;
2470 dump_write = optarg;
2476 vmlinux_section_warnings = 0;
2479 files_source = optarg;
2482 warn_unresolved = 1;
2485 sec_mismatch_fatal = 1;
2493 read_dump(kernel_read, 1);
2495 read_dump(module_read, 0);
2496 while (extsym_start) {
2497 read_dump(extsym_start->file, 0);
2498 extsym_iter = extsym_start->next;
2500 extsym_start = extsym_iter;
2503 while (optind < argc)
2504 read_symbols(argv[optind++]);
2507 read_symbols_from_files(files_source);
2511 for (mod = modules; mod; mod = mod->next) {
2512 char fname[PATH_MAX];
2519 err |= check_modname_len(mod);
2520 err |= check_exports(mod);
2521 add_header(&buf, mod);
2522 add_intree_flag(&buf, !external_module);
2523 add_retpoline(&buf);
2524 add_staging_flag(&buf, mod->name);
2525 err |= add_versions(&buf, mod);
2526 add_depends(&buf, mod);
2527 add_moddevtable(&buf, mod);
2528 add_srcversion(&buf, mod);
2530 sprintf(fname, "%s.mod.c", mod->name);
2531 write_if_changed(&buf, fname);
2534 write_dump(dump_write);
2535 if (sec_mismatch_count && sec_mismatch_fatal)
2536 fatal("modpost: Section mismatches detected.\n"
2537 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2538 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2539 struct symbol *s = symbolhash[n];
2543 warn("\"%s\" [%s] is a static %s\n",
2544 s->name, s->module->name,
2545 export_str(s->export));