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 ...
24 #include "../../include/linux/license.h"
26 /* Are we using CONFIG_MODVERSIONS? */
27 static bool modversions;
28 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29 static bool all_versions;
30 /* If we are modposting external module set to 1 */
31 static bool external_module;
32 /* Only warn about unresolved symbols */
33 static bool warn_unresolved;
35 static int sec_mismatch_count;
36 static bool sec_mismatch_warn_only = true;
37 /* ignore missing files */
38 static bool ignore_missing_files;
39 /* If set to 1, only warn (instead of error) about missing ns imports */
40 static bool allow_missing_ns_imports;
42 static bool error_occurred;
45 * Cut off the warnings when there are too many. This typically occurs when
46 * vmlinux is missing. ('make modules' without building vmlinux.)
48 #define MAX_UNRESOLVED_REPORTS 10
49 static unsigned int nr_unresolved;
51 /* In kernel, this size is defined in linux/module.h;
52 * here we use Elf_Addr instead of long for covering cross-compile
55 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
57 void __attribute__((format(printf, 2, 3)))
58 modpost_log(enum loglevel loglevel, const char *fmt, ...)
64 fprintf(stderr, "WARNING: ");
67 fprintf(stderr, "ERROR: ");
70 fprintf(stderr, "FATAL: ");
72 default: /* invalid loglevel, ignore */
76 fprintf(stderr, "modpost: ");
78 va_start(arglist, fmt);
79 vfprintf(stderr, fmt, arglist);
82 if (loglevel == LOG_FATAL)
84 if (loglevel == LOG_ERROR)
85 error_occurred = true;
88 static inline bool strends(const char *str, const char *postfix)
90 if (strlen(str) < strlen(postfix))
93 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96 void *do_nofail(void *ptr, const char *expr)
99 fatal("Memory allocation failure: %s.\n", expr);
104 char *read_text_file(const char *filename)
111 fd = open(filename, O_RDONLY);
117 if (fstat(fd, &st) < 0) {
122 buf = NOFAIL(malloc(st.st_size + 1));
129 bytes_read = read(fd, buf, nbytes);
130 if (bytes_read < 0) {
135 nbytes -= bytes_read;
137 buf[st.st_size] = '\0';
144 char *get_line(char **stringp)
146 char *orig = *stringp, *next;
148 /* do not return the unwanted extra line at EOF */
149 if (!orig || *orig == '\0')
152 /* don't use strsep here, it is not available everywhere */
153 next = strchr(orig, '\n');
162 /* A list of all modules we processed */
165 static struct module *find_module(const char *modname)
169 list_for_each_entry(mod, &modules, list) {
170 if (strcmp(mod->name, modname) == 0)
176 static struct module *new_module(const char *name, size_t namelen)
180 mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
181 memset(mod, 0, sizeof(*mod));
183 INIT_LIST_HEAD(&mod->exported_symbols);
184 INIT_LIST_HEAD(&mod->unresolved_symbols);
185 INIT_LIST_HEAD(&mod->missing_namespaces);
186 INIT_LIST_HEAD(&mod->imported_namespaces);
188 memcpy(mod->name, name, namelen);
189 mod->name[namelen] = '\0';
190 mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
193 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
194 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
195 * modpost will exit wiht error anyway.
197 mod->is_gpl_compatible = true;
199 list_add_tail(&mod->list, &modules);
204 /* A hash of all exported symbols,
205 * struct symbol is also used for lists of unresolved symbols */
207 #define SYMBOL_HASH_SIZE 1024
211 struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
212 struct module *module;
217 bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
221 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
223 /* This is based on the hash algorithm from gdbm, via tdb */
224 static inline unsigned int tdb_hash(const char *name)
226 unsigned value; /* Used to compute the hash value. */
227 unsigned i; /* Used to cycle through random values. */
229 /* Set the initial value from the key size. */
230 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
231 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
233 return (1103515243 * value + 12345);
237 * Allocate a new symbols for use in the hash of exported symbols or
238 * the list of unresolved symbols per module
240 static struct symbol *alloc_symbol(const char *name)
242 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
244 memset(s, 0, sizeof(*s));
245 strcpy(s->name, name);
250 /* For the hash of exported symbols */
251 static void hash_add_symbol(struct symbol *sym)
255 hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
256 sym->next = symbolhash[hash];
257 symbolhash[hash] = sym;
260 static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
264 sym = alloc_symbol(name);
267 list_add_tail(&sym->list, &mod->unresolved_symbols);
270 static struct symbol *sym_find_with_module(const char *name, struct module *mod)
274 /* For our purposes, .foo matches foo. PPC64 needs this. */
278 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
279 if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
285 static struct symbol *find_symbol(const char *name)
287 return sym_find_with_module(name, NULL);
290 struct namespace_list {
291 struct list_head list;
295 static bool contains_namespace(struct list_head *head, const char *namespace)
297 struct namespace_list *list;
299 list_for_each_entry(list, head, list) {
300 if (!strcmp(list->namespace, namespace))
307 static void add_namespace(struct list_head *head, const char *namespace)
309 struct namespace_list *ns_entry;
311 if (!contains_namespace(head, namespace)) {
312 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
313 strlen(namespace) + 1));
314 strcpy(ns_entry->namespace, namespace);
315 list_add_tail(&ns_entry->list, head);
319 static void *sym_get_data_by_offset(const struct elf_info *info,
320 unsigned int secindex, unsigned long offset)
322 Elf_Shdr *sechdr = &info->sechdrs[secindex];
324 return (void *)info->hdr + sechdr->sh_offset + offset;
327 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
329 return sym_get_data_by_offset(info, get_secindex(info, sym),
333 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
335 return sym_get_data_by_offset(info, info->secindex_strings,
339 static const char *sec_name(const struct elf_info *info, int secindex)
341 return sech_name(info, &info->sechdrs[secindex]);
344 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
346 static void sym_update_namespace(const char *symname, const char *namespace)
348 struct symbol *s = find_symbol(symname);
351 * That symbol should have been created earlier and thus this is
352 * actually an assertion.
355 error("Could not update namespace(%s) for symbol %s\n",
361 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
364 static struct symbol *sym_add_exported(const char *name, struct module *mod,
367 struct symbol *s = find_symbol(name);
369 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
370 error("%s: '%s' exported twice. Previous export was in %s%s\n",
371 mod->name, name, s->module->name,
372 s->module->is_vmlinux ? "" : ".ko");
375 s = alloc_symbol(name);
377 s->is_gpl_only = gpl_only;
378 list_add_tail(&s->list, &mod->exported_symbols);
384 static void sym_set_crc(struct symbol *sym, unsigned int crc)
387 sym->crc_valid = true;
390 static void *grab_file(const char *filename, size_t *size)
393 void *map = MAP_FAILED;
396 fd = open(filename, O_RDONLY);
403 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
407 if (map == MAP_FAILED)
412 static void release_file(void *file, size_t size)
417 static int parse_elf(struct elf_info *info, const char *filename)
423 const char *secstrings;
424 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
426 hdr = grab_file(filename, &info->size);
428 if (ignore_missing_files) {
429 fprintf(stderr, "%s: %s (ignored)\n", filename,
437 if (info->size < sizeof(*hdr)) {
438 /* file too small, assume this is an empty .o file */
441 /* Is this a valid ELF file? */
442 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
443 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
444 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
445 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
446 /* Not an ELF file - silently ignore it */
449 /* Fix endianness in ELF header */
450 hdr->e_type = TO_NATIVE(hdr->e_type);
451 hdr->e_machine = TO_NATIVE(hdr->e_machine);
452 hdr->e_version = TO_NATIVE(hdr->e_version);
453 hdr->e_entry = TO_NATIVE(hdr->e_entry);
454 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
455 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
456 hdr->e_flags = TO_NATIVE(hdr->e_flags);
457 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
458 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
459 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
460 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
461 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
462 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
463 sechdrs = (void *)hdr + hdr->e_shoff;
464 info->sechdrs = sechdrs;
466 /* modpost only works for relocatable objects */
467 if (hdr->e_type != ET_REL)
468 fatal("%s: not relocatable object.", filename);
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 filesize=%zu\n",
473 (unsigned long)hdr->e_shoff, filename, info->size);
477 if (hdr->e_shnum == SHN_UNDEF) {
479 * There are more than 64k sections,
480 * read count from .sh_size.
482 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
485 info->num_sections = hdr->e_shnum;
487 if (hdr->e_shstrndx == SHN_XINDEX) {
488 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
491 info->secindex_strings = hdr->e_shstrndx;
494 /* Fix endianness in section headers */
495 for (i = 0; i < info->num_sections; i++) {
496 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
497 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
498 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
499 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
500 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
501 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
502 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
503 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
504 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
505 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
507 /* Find symbol table. */
508 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
509 for (i = 1; i < info->num_sections; i++) {
511 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
513 if (!nobits && sechdrs[i].sh_offset > info->size) {
514 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
515 "sizeof(*hrd)=%zu\n", filename,
516 (unsigned long)sechdrs[i].sh_offset,
520 secname = secstrings + sechdrs[i].sh_name;
521 if (strcmp(secname, ".modinfo") == 0) {
523 fatal("%s has NOBITS .modinfo\n", filename);
524 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
525 info->modinfo_len = sechdrs[i].sh_size;
528 if (sechdrs[i].sh_type == SHT_SYMTAB) {
529 unsigned int sh_link_idx;
531 info->symtab_start = (void *)hdr +
532 sechdrs[i].sh_offset;
533 info->symtab_stop = (void *)hdr +
534 sechdrs[i].sh_offset + sechdrs[i].sh_size;
535 sh_link_idx = sechdrs[i].sh_link;
536 info->strtab = (void *)hdr +
537 sechdrs[sh_link_idx].sh_offset;
540 /* 32bit section no. table? ("more than 64k sections") */
541 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
542 symtab_shndx_idx = i;
543 info->symtab_shndx_start = (void *)hdr +
544 sechdrs[i].sh_offset;
545 info->symtab_shndx_stop = (void *)hdr +
546 sechdrs[i].sh_offset + sechdrs[i].sh_size;
549 if (!info->symtab_start)
550 fatal("%s has no symtab?\n", filename);
552 /* Fix endianness in symbols */
553 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
554 sym->st_shndx = TO_NATIVE(sym->st_shndx);
555 sym->st_name = TO_NATIVE(sym->st_name);
556 sym->st_value = TO_NATIVE(sym->st_value);
557 sym->st_size = TO_NATIVE(sym->st_size);
560 if (symtab_shndx_idx != ~0U) {
562 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
563 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
564 filename, sechdrs[symtab_shndx_idx].sh_link,
567 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
575 static void parse_elf_finish(struct elf_info *info)
577 release_file(info->hdr, info->size);
580 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
582 /* ignore __this_module, it will be resolved shortly */
583 if (strcmp(symname, "__this_module") == 0)
585 /* ignore global offset table */
586 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
588 if (info->hdr->e_machine == EM_PPC)
589 /* Special register function linked on all modules during final link of .ko */
590 if (strstarts(symname, "_restgpr_") ||
591 strstarts(symname, "_savegpr_") ||
592 strstarts(symname, "_rest32gpr_") ||
593 strstarts(symname, "_save32gpr_") ||
594 strstarts(symname, "_restvr_") ||
595 strstarts(symname, "_savevr_"))
597 if (info->hdr->e_machine == EM_PPC64)
598 /* Special register function linked on all modules during final link of .ko */
599 if (strstarts(symname, "_restgpr0_") ||
600 strstarts(symname, "_savegpr0_") ||
601 strstarts(symname, "_restvr_") ||
602 strstarts(symname, "_savevr_") ||
603 strcmp(symname, ".TOC.") == 0)
606 if (info->hdr->e_machine == EM_S390)
607 /* Expoline thunks are linked on all kernel modules during final link of .ko */
608 if (strstarts(symname, "__s390_indirect_jump_r"))
610 /* Do not ignore this symbol */
614 static void handle_symbol(struct module *mod, struct elf_info *info,
615 const Elf_Sym *sym, const char *symname)
617 switch (sym->st_shndx) {
619 if (strstarts(symname, "__gnu_lto_")) {
620 /* Should warn here, but modpost runs before the linker */
622 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
625 /* undefined symbol */
626 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
627 ELF_ST_BIND(sym->st_info) != STB_WEAK)
629 if (ignore_undef_symbol(info, symname))
631 if (info->hdr->e_machine == EM_SPARC ||
632 info->hdr->e_machine == EM_SPARCV9) {
633 /* Ignore register directives. */
634 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
636 if (symname[0] == '.') {
637 char *munged = NOFAIL(strdup(symname));
639 munged[1] = toupper(munged[1]);
644 sym_add_unresolved(symname, mod,
645 ELF_ST_BIND(sym->st_info) == STB_WEAK);
648 /* All exported symbols */
649 if (strstarts(symname, "__ksymtab_")) {
650 const char *name, *secname;
652 name = symname + strlen("__ksymtab_");
653 secname = sec_name(info, get_secindex(info, sym));
655 if (strstarts(secname, "___ksymtab_gpl+"))
656 sym_add_exported(name, mod, true);
657 else if (strstarts(secname, "___ksymtab+"))
658 sym_add_exported(name, mod, false);
660 if (strcmp(symname, "init_module") == 0)
661 mod->has_init = true;
662 if (strcmp(symname, "cleanup_module") == 0)
663 mod->has_cleanup = true;
669 * Parse tag=value strings from .modinfo section
671 static char *next_string(char *string, unsigned long *secsize)
673 /* Skip non-zero chars */
676 if ((*secsize)-- <= 1)
680 /* Skip any zero padding. */
683 if ((*secsize)-- <= 1)
689 static char *get_next_modinfo(struct elf_info *info, const char *tag,
693 unsigned int taglen = strlen(tag);
694 char *modinfo = info->modinfo;
695 unsigned long size = info->modinfo_len;
698 size -= prev - modinfo;
699 modinfo = next_string(prev, &size);
702 for (p = modinfo; p; p = next_string(p, &size)) {
703 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
704 return p + taglen + 1;
709 static char *get_modinfo(struct elf_info *info, const char *tag)
712 return get_next_modinfo(info, tag, NULL);
715 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
718 return elf->strtab + sym->st_name;
724 * Check whether the 'string' argument matches one of the 'patterns',
725 * an array of shell wildcard patterns (glob).
727 * Return true is there is a match.
729 static bool match(const char *string, const char *const patterns[])
733 while ((pattern = *patterns++)) {
734 if (!fnmatch(pattern, string, 0))
741 /* sections that we do not want to do full section mismatch check on */
742 static const char *const section_white_list[] =
746 ".zdebug*", /* Compressed debug sections. */
747 ".GCC.command.line", /* record-gcc-switches */
748 ".mdebug*", /* alpha, score, mips etc. */
749 ".pdr", /* alpha, score, mips etc. */
754 ".xt.prop", /* xtensa */
755 ".xt.lit", /* xtensa */
756 ".arcextmap*", /* arc */
757 ".gnu.linkonce.arcext*", /* arc : modules */
758 ".cmem*", /* EZchip */
759 ".fmt_slot*", /* EZchip */
766 * This is used to find sections missing the SHF_ALLOC flag.
767 * The cause of this is often a section specified in assembler
768 * without "ax" / "aw".
770 static void check_section(const char *modname, struct elf_info *elf,
773 const char *sec = sech_name(elf, sechdr);
775 if (sechdr->sh_type == SHT_PROGBITS &&
776 !(sechdr->sh_flags & SHF_ALLOC) &&
777 !match(sec, section_white_list)) {
778 warn("%s (%s): unexpected non-allocatable section.\n"
779 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
780 "Note that for example <linux/init.h> contains\n"
781 "section definitions for use in .S files.\n\n",
788 #define ALL_INIT_DATA_SECTIONS \
789 ".init.setup", ".init.rodata", ".meminit.rodata", \
790 ".init.data", ".meminit.data"
791 #define ALL_EXIT_DATA_SECTIONS \
792 ".exit.data", ".memexit.data"
794 #define ALL_INIT_TEXT_SECTIONS \
795 ".init.text", ".meminit.text"
796 #define ALL_EXIT_TEXT_SECTIONS \
797 ".exit.text", ".memexit.text"
799 #define ALL_PCI_INIT_SECTIONS \
800 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
801 ".pci_fixup_enable", ".pci_fixup_resume", \
802 ".pci_fixup_resume_early", ".pci_fixup_suspend"
804 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
805 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
807 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
808 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
810 #define DATA_SECTIONS ".data", ".data.rel"
811 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
812 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
813 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
814 ".fixup", ".entry.text", ".exception.text", ".text.*", \
815 ".coldtext", ".softirqentry.text"
817 #define INIT_SECTIONS ".init.*"
818 #define MEM_INIT_SECTIONS ".meminit.*"
820 #define EXIT_SECTIONS ".exit.*"
821 #define MEM_EXIT_SECTIONS ".memexit.*"
823 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
824 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
826 /* init data sections */
827 static const char *const init_data_sections[] =
828 { ALL_INIT_DATA_SECTIONS, NULL };
830 /* all init sections */
831 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
833 /* All init and exit sections (code + data) */
834 static const char *const init_exit_sections[] =
835 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
837 /* all text sections */
838 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
841 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
844 /* symbols in .data that may refer to init/exit sections */
845 #define DEFAULT_SYMBOL_WHITE_LIST \
847 "*_template", /* scsi uses *_template a lot */ \
848 "*_timer", /* arm uses ops structures named _timer a lot */ \
849 "*_sht", /* scsi also used *_sht to some extent */ \
855 static const char *const head_sections[] = { ".head.text*", NULL };
856 static const char *const linker_symbols[] =
857 { "__init_begin", "_sinittext", "_einittext", NULL };
858 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
865 XXXINIT_TO_SOME_INIT,
866 XXXEXIT_TO_SOME_EXIT,
867 ANY_INIT_TO_ANY_EXIT,
868 ANY_EXIT_TO_ANY_INIT,
874 * Describe how to match sections on different criteria:
876 * @fromsec: Array of sections to be matched.
878 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
879 * this array is forbidden (black-list). Can be empty.
881 * @good_tosec: Relocations applied to a section in @fromsec must be
882 * targeting sections in this array (white-list). Can be empty.
884 * @mismatch: Type of mismatch.
886 * @symbol_white_list: Do not match a relocation to a symbol in this list
887 * even if it is targeting a section in @bad_to_sec.
889 * @handler: Specific handler to call when a match is found. If NULL,
890 * default_mismatch_handler() will be called.
893 struct sectioncheck {
894 const char *fromsec[20];
895 const char *bad_tosec[20];
896 const char *good_tosec[20];
897 enum mismatch mismatch;
898 const char *symbol_white_list[20];
899 void (*handler)(const char *modname, struct elf_info *elf,
900 const struct sectioncheck* const mismatch,
901 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
905 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
906 const struct sectioncheck* const mismatch,
907 Elf_Rela *r, Elf_Sym *sym,
908 const char *fromsec);
910 static const struct sectioncheck sectioncheck[] = {
911 /* Do not reference init/exit code/data from
912 * normal code and data
915 .fromsec = { TEXT_SECTIONS, NULL },
916 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
917 .mismatch = TEXT_TO_ANY_INIT,
918 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
921 .fromsec = { DATA_SECTIONS, NULL },
922 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
923 .mismatch = DATA_TO_ANY_INIT,
924 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
927 .fromsec = { DATA_SECTIONS, NULL },
928 .bad_tosec = { INIT_SECTIONS, NULL },
929 .mismatch = DATA_TO_ANY_INIT,
930 .symbol_white_list = {
931 "*_template", "*_timer", "*_sht", "*_ops",
932 "*_probe", "*_probe_one", "*_console", NULL
936 .fromsec = { TEXT_SECTIONS, NULL },
937 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
938 .mismatch = TEXT_TO_ANY_EXIT,
939 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
942 .fromsec = { DATA_SECTIONS, NULL },
943 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
944 .mismatch = DATA_TO_ANY_EXIT,
945 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
947 /* Do not reference init code/data from meminit code/data */
949 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
950 .bad_tosec = { INIT_SECTIONS, NULL },
951 .mismatch = XXXINIT_TO_SOME_INIT,
952 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
954 /* Do not reference exit code/data from memexit code/data */
956 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
957 .bad_tosec = { EXIT_SECTIONS, NULL },
958 .mismatch = XXXEXIT_TO_SOME_EXIT,
959 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
961 /* Do not use exit code/data from init code */
963 .fromsec = { ALL_INIT_SECTIONS, NULL },
964 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
965 .mismatch = ANY_INIT_TO_ANY_EXIT,
966 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
968 /* Do not use init code/data from exit code */
970 .fromsec = { ALL_EXIT_SECTIONS, NULL },
971 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
972 .mismatch = ANY_EXIT_TO_ANY_INIT,
973 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
976 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
977 .bad_tosec = { INIT_SECTIONS, NULL },
978 .mismatch = ANY_INIT_TO_ANY_EXIT,
979 .symbol_white_list = { NULL },
981 /* Do not export init/exit functions or data */
983 .fromsec = { "___ksymtab*", NULL },
984 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
985 .mismatch = EXPORT_TO_INIT_EXIT,
986 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
989 .fromsec = { "__ex_table", NULL },
990 /* If you're adding any new black-listed sections in here, consider
991 * adding a special 'printer' for them in scripts/check_extable.
993 .bad_tosec = { ".altinstr_replacement", NULL },
994 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
995 .mismatch = EXTABLE_TO_NON_TEXT,
996 .handler = extable_mismatch_handler,
1000 static const struct sectioncheck *section_mismatch(
1001 const char *fromsec, const char *tosec)
1006 * The target section could be the SHT_NUL section when we're
1007 * handling relocations to un-resolved symbols, trying to match it
1008 * doesn't make much sense and causes build failures on parisc
1014 for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
1015 const struct sectioncheck *check = §ioncheck[i];
1017 if (match(fromsec, check->fromsec)) {
1018 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1020 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1028 * Whitelist to allow certain references to pass with no warning.
1031 * If a module parameter is declared __initdata and permissions=0
1032 * then this is legal despite the warning generated.
1033 * We cannot see value of permissions here, so just ignore
1035 * The pattern is identified by:
1036 * tosec = .init.data
1041 * module_param_call() ops can refer to __init set function if permissions=0
1042 * The pattern is identified by:
1043 * tosec = .init.text
1045 * atsym = __param_ops_*
1048 * Many drivers utilise a *driver container with references to
1049 * add, remove, probe functions etc.
1050 * the pattern is identified by:
1051 * tosec = init or exit section
1052 * fromsec = data section
1053 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1054 * *probe_one, *_console, *_timer
1057 * Whitelist all references from .head.text to any init section
1060 * Some symbols belong to init section but still it is ok to reference
1061 * these from non-init sections as these symbols don't have any memory
1062 * allocated for them and symbol address and value are same. So even
1063 * if init section is freed, its ok to reference those symbols.
1064 * For ex. symbols marking the init section boundaries.
1065 * This pattern is identified by
1066 * refsymname = __init_begin, _sinittext, _einittext
1069 * GCC may optimize static inlines when fed constant arg(s) resulting
1070 * in functions like cpumask_empty() -- generating an associated symbol
1071 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1072 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1073 * meaningless section warning. May need to add isra symbols too...
1074 * This pattern is identified by
1075 * tosec = init section
1076 * fromsec = text section
1077 * refsymname = *.constprop.*
1080 * Hide section mismatch warnings for ELF local symbols. The goal
1081 * is to eliminate false positive modpost warnings caused by
1082 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1083 * Autogenerated symbol names bypass modpost's "Pattern 2"
1084 * whitelisting, which relies on pattern-matching against symbol
1085 * names to work. (One situation where gcc can autogenerate ELF
1086 * local symbols is when "-fsection-anchors" is used.)
1088 static int secref_whitelist(const struct sectioncheck *mismatch,
1089 const char *fromsec, const char *fromsym,
1090 const char *tosec, const char *tosym)
1092 /* Check for pattern 1 */
1093 if (match(tosec, init_data_sections) &&
1094 match(fromsec, data_sections) &&
1095 strstarts(fromsym, "__param"))
1098 /* Check for pattern 1a */
1099 if (strcmp(tosec, ".init.text") == 0 &&
1100 match(fromsec, data_sections) &&
1101 strstarts(fromsym, "__param_ops_"))
1104 /* Check for pattern 2 */
1105 if (match(tosec, init_exit_sections) &&
1106 match(fromsec, data_sections) &&
1107 match(fromsym, mismatch->symbol_white_list))
1110 /* Check for pattern 3 */
1111 if (match(fromsec, head_sections) &&
1112 match(tosec, init_sections))
1115 /* Check for pattern 4 */
1116 if (match(tosym, linker_symbols))
1119 /* Check for pattern 5 */
1120 if (match(fromsec, text_sections) &&
1121 match(tosec, init_sections) &&
1122 match(fromsym, optim_symbols))
1125 /* Check for pattern 6 */
1126 if (strstarts(fromsym, ".L"))
1132 static inline int is_arm_mapping_symbol(const char *str)
1134 return str[0] == '$' &&
1135 (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x')
1136 && (str[2] == '\0' || str[2] == '.');
1140 * If there's no name there, ignore it; likewise, ignore it if it's
1141 * one of the magic symbols emitted used by current ARM tools.
1143 * Otherwise if find_symbols_between() returns those symbols, they'll
1144 * fail the whitelist tests and cause lots of false alarms ... fixable
1145 * only by merging __exit and __init sections into __text, bloating
1146 * the kernel (which is especially evil on embedded platforms).
1148 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1150 const char *name = elf->strtab + sym->st_name;
1152 if (!name || !strlen(name))
1154 return !is_arm_mapping_symbol(name);
1158 * Find symbol based on relocation record info.
1159 * In some cases the symbol supplied is a valid symbol so
1160 * return refsym. If st_name != 0 we assume this is a valid symbol.
1161 * In other cases the symbol needs to be looked up in the symbol table
1162 * based on section and address.
1164 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1168 Elf_Sym *near = NULL;
1169 Elf64_Sword distance = 20;
1171 unsigned int relsym_secindex;
1173 if (relsym->st_name != 0)
1176 relsym_secindex = get_secindex(elf, relsym);
1177 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1178 if (get_secindex(elf, sym) != relsym_secindex)
1180 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1182 if (!is_valid_name(elf, sym))
1184 if (sym->st_value == addr)
1186 /* Find a symbol nearby - addr are maybe negative */
1187 d = sym->st_value - addr;
1189 d = addr - sym->st_value;
1195 /* We need a close match */
1203 * Find symbols before or equal addr and after addr - in the section sec.
1204 * If we find two symbols with equal offset prefer one with a valid name.
1205 * The ELF format may have a better way to detect what type of symbol
1206 * it is, but this works for now.
1208 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1212 Elf_Sym *near = NULL;
1213 Elf_Addr distance = ~0;
1215 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1218 if (is_shndx_special(sym->st_shndx))
1220 symsec = sec_name(elf, get_secindex(elf, sym));
1221 if (strcmp(symsec, sec) != 0)
1223 if (!is_valid_name(elf, sym))
1225 if (sym->st_value <= addr && addr - sym->st_value <= distance) {
1226 distance = addr - sym->st_value;
1234 * Convert a section name to the function/data attribute
1235 * .init.text => __init
1236 * .memexitconst => __memconst
1239 * The memory of returned value has been allocated on a heap. The user of this
1240 * method should free it after usage.
1242 static char *sec2annotation(const char *s)
1244 if (match(s, init_exit_sections)) {
1245 char *p = NOFAIL(malloc(20));
1252 while (*s && *s != '.')
1257 if (strstr(s, "rodata") != NULL)
1258 strcat(p, "const ");
1259 else if (strstr(s, "data") != NULL)
1265 return NOFAIL(strdup(""));
1269 static int is_function(Elf_Sym *sym)
1272 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1277 static void print_section_list(const char * const list[20])
1279 const char *const *s = list;
1282 fprintf(stderr, "%s", *s);
1285 fprintf(stderr, ", ");
1287 fprintf(stderr, "\n");
1290 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1293 case 0: *name = "variable"; *name_p = ""; break;
1294 case 1: *name = "function"; *name_p = "()"; break;
1295 default: *name = "(unknown reference)"; *name_p = ""; break;
1300 * Print a warning about a section mismatch.
1301 * Try to find symbols near it so user can find it.
1302 * Check whitelist before warning - it may be a false positive.
1304 static void report_sec_mismatch(const char *modname,
1305 const struct sectioncheck *mismatch,
1306 const char *fromsec,
1307 unsigned long long fromaddr,
1308 const char *fromsym,
1310 const char *tosec, const char *tosym,
1313 const char *from, *from_p;
1314 const char *to, *to_p;
1318 sec_mismatch_count++;
1320 get_pretty_name(from_is_func, &from, &from_p);
1321 get_pretty_name(to_is_func, &to, &to_p);
1323 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1324 "to the %s %s:%s%s\n",
1325 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1328 switch (mismatch->mismatch) {
1329 case TEXT_TO_ANY_INIT:
1330 prl_from = sec2annotation(fromsec);
1331 prl_to = sec2annotation(tosec);
1333 "The function %s%s() references\n"
1335 "This is often because %s lacks a %s\n"
1336 "annotation or the annotation of %s is wrong.\n",
1338 to, prl_to, tosym, to_p,
1339 fromsym, prl_to, tosym);
1343 case DATA_TO_ANY_INIT: {
1344 prl_to = sec2annotation(tosec);
1346 "The variable %s references\n"
1348 "If the reference is valid then annotate the\n"
1349 "variable with __init* or __refdata (see linux/init.h) "
1350 "or name the variable:\n",
1351 fromsym, to, prl_to, tosym, to_p);
1352 print_section_list(mismatch->symbol_white_list);
1356 case TEXT_TO_ANY_EXIT:
1357 prl_to = sec2annotation(tosec);
1359 "The function %s() references a %s in an exit section.\n"
1360 "Often the %s %s%s has valid usage outside the exit section\n"
1361 "and the fix is to remove the %sannotation of %s.\n",
1362 fromsym, to, to, tosym, to_p, prl_to, tosym);
1365 case DATA_TO_ANY_EXIT: {
1366 prl_to = sec2annotation(tosec);
1368 "The variable %s references\n"
1370 "If the reference is valid then annotate the\n"
1371 "variable with __exit* (see linux/init.h) or "
1372 "name the variable:\n",
1373 fromsym, to, prl_to, tosym, to_p);
1374 print_section_list(mismatch->symbol_white_list);
1378 case XXXINIT_TO_SOME_INIT:
1379 case XXXEXIT_TO_SOME_EXIT:
1380 prl_from = sec2annotation(fromsec);
1381 prl_to = sec2annotation(tosec);
1383 "The %s %s%s%s references\n"
1385 "If %s is only used by %s then\n"
1386 "annotate %s with a matching annotation.\n",
1387 from, prl_from, fromsym, from_p,
1388 to, prl_to, tosym, to_p,
1389 tosym, fromsym, tosym);
1393 case ANY_INIT_TO_ANY_EXIT:
1394 prl_from = sec2annotation(fromsec);
1395 prl_to = sec2annotation(tosec);
1397 "The %s %s%s%s references\n"
1399 "This is often seen when error handling "
1400 "in the init function\n"
1401 "uses functionality in the exit path.\n"
1402 "The fix is often to remove the %sannotation of\n"
1403 "%s%s so it may be used outside an exit section.\n",
1404 from, prl_from, fromsym, from_p,
1405 to, prl_to, tosym, to_p,
1406 prl_to, tosym, to_p);
1410 case ANY_EXIT_TO_ANY_INIT:
1411 prl_from = sec2annotation(fromsec);
1412 prl_to = sec2annotation(tosec);
1414 "The %s %s%s%s references\n"
1416 "This is often seen when error handling "
1417 "in the exit function\n"
1418 "uses functionality in the init path.\n"
1419 "The fix is often to remove the %sannotation of\n"
1420 "%s%s so it may be used outside an init section.\n",
1421 from, prl_from, fromsym, from_p,
1422 to, prl_to, tosym, to_p,
1423 prl_to, tosym, to_p);
1427 case EXPORT_TO_INIT_EXIT:
1428 prl_to = sec2annotation(tosec);
1430 "The symbol %s is exported and annotated %s\n"
1431 "Fix this by removing the %sannotation of %s "
1432 "or drop the export.\n",
1433 tosym, prl_to, prl_to, tosym);
1436 case EXTABLE_TO_NON_TEXT:
1437 fatal("There's a special handler for this mismatch type, "
1438 "we should never get here.");
1441 fprintf(stderr, "\n");
1444 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1445 const struct sectioncheck* const mismatch,
1446 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1452 const char *fromsym;
1454 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1455 fromsym = sym_name(elf, from);
1457 if (strstarts(fromsym, "reference___initcall"))
1460 tosec = sec_name(elf, get_secindex(elf, sym));
1461 to = find_elf_symbol(elf, r->r_addend, sym);
1462 tosym = sym_name(elf, to);
1464 /* check whitelist - we may ignore it */
1465 if (secref_whitelist(mismatch,
1466 fromsec, fromsym, tosec, tosym)) {
1467 report_sec_mismatch(modname, mismatch,
1468 fromsec, r->r_offset, fromsym,
1469 is_function(from), tosec, tosym,
1474 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1476 if (section_index > elf->num_sections)
1477 fatal("section_index is outside elf->num_sections!\n");
1479 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1483 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1484 * to know the sizeof(struct exception_table_entry) for the target architecture.
1486 static unsigned int extable_entry_size = 0;
1487 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1490 * If we're currently checking the second relocation within __ex_table,
1491 * that relocation offset tells us the offsetof(struct
1492 * exception_table_entry, fixup) which is equal to sizeof(struct
1493 * exception_table_entry) divided by two. We use that to our advantage
1494 * since there's no portable way to get that size as every architecture
1495 * seems to go with different sized types. Not pretty but better than
1496 * hard-coding the size for every architecture..
1498 if (!extable_entry_size)
1499 extable_entry_size = r->r_offset * 2;
1502 static inline bool is_extable_fault_address(Elf_Rela *r)
1505 * extable_entry_size is only discovered after we've handled the
1506 * _second_ relocation in __ex_table, so only abort when we're not
1507 * handling the first reloc and extable_entry_size is zero.
1509 if (r->r_offset && extable_entry_size == 0)
1510 fatal("extable_entry size hasn't been discovered!\n");
1512 return ((r->r_offset == 0) ||
1513 (r->r_offset % extable_entry_size == 0));
1516 #define is_second_extable_reloc(Start, Cur, Sec) \
1517 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1519 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1520 const struct sectioncheck* const mismatch,
1521 Elf_Rela* r, Elf_Sym* sym,
1522 const char* fromsec, const char* tosec)
1524 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1525 const char* fromsym_name = sym_name(elf, fromsym);
1526 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1527 const char* tosym_name = sym_name(elf, tosym);
1528 const char* from_pretty_name;
1529 const char* from_pretty_name_p;
1530 const char* to_pretty_name;
1531 const char* to_pretty_name_p;
1533 get_pretty_name(is_function(fromsym),
1534 &from_pretty_name, &from_pretty_name_p);
1535 get_pretty_name(is_function(tosym),
1536 &to_pretty_name, &to_pretty_name_p);
1538 warn("%s(%s+0x%lx): Section mismatch in reference"
1539 " from the %s %s%s to the %s %s:%s%s\n",
1540 modname, fromsec, (long)r->r_offset, from_pretty_name,
1541 fromsym_name, from_pretty_name_p,
1542 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1544 if (!match(tosec, mismatch->bad_tosec) &&
1545 is_executable_section(elf, get_secindex(elf, sym)))
1547 "The relocation at %s+0x%lx references\n"
1548 "section \"%s\" which is not in the list of\n"
1549 "authorized sections. If you're adding a new section\n"
1550 "and/or if this reference is valid, add \"%s\" to the\n"
1551 "list of authorized sections to jump to on fault.\n"
1552 "This can be achieved by adding \"%s\" to \n"
1553 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1554 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1557 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1558 const struct sectioncheck* const mismatch,
1559 Elf_Rela* r, Elf_Sym* sym,
1560 const char *fromsec)
1562 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1564 sec_mismatch_count++;
1566 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1568 if (match(tosec, mismatch->bad_tosec))
1569 fatal("The relocation at %s+0x%lx references\n"
1570 "section \"%s\" which is black-listed.\n"
1571 "Something is seriously wrong and should be fixed.\n"
1572 "You might get more information about where this is\n"
1573 "coming from by using scripts/check_extable.sh %s\n",
1574 fromsec, (long)r->r_offset, tosec, modname);
1575 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1576 if (is_extable_fault_address(r))
1577 fatal("The relocation at %s+0x%lx references\n"
1578 "section \"%s\" which is not executable, IOW\n"
1579 "it is not possible for the kernel to fault\n"
1580 "at that address. Something is seriously wrong\n"
1581 "and should be fixed.\n",
1582 fromsec, (long)r->r_offset, tosec);
1584 fatal("The relocation at %s+0x%lx references\n"
1585 "section \"%s\" which is not executable, IOW\n"
1586 "the kernel will fault if it ever tries to\n"
1587 "jump to it. Something is seriously wrong\n"
1588 "and should be fixed.\n",
1589 fromsec, (long)r->r_offset, tosec);
1593 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1594 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1596 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1597 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1600 if (mismatch->handler)
1601 mismatch->handler(modname, elf, mismatch,
1604 default_mismatch_handler(modname, elf, mismatch,
1609 static unsigned int *reloc_location(struct elf_info *elf,
1610 Elf_Shdr *sechdr, Elf_Rela *r)
1612 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1615 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1617 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1618 unsigned int *location = reloc_location(elf, sechdr, r);
1622 r->r_addend = TO_NATIVE(*location);
1625 r->r_addend = TO_NATIVE(*location) + 4;
1632 #define R_ARM_CALL 28
1634 #ifndef R_ARM_JUMP24
1635 #define R_ARM_JUMP24 29
1638 #ifndef R_ARM_THM_CALL
1639 #define R_ARM_THM_CALL 10
1641 #ifndef R_ARM_THM_JUMP24
1642 #define R_ARM_THM_JUMP24 30
1644 #ifndef R_ARM_THM_JUMP19
1645 #define R_ARM_THM_JUMP19 51
1648 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1650 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1654 /* From ARM ABI: (S + A) | T */
1655 r->r_addend = (int)(long)
1656 (elf->symtab_start + ELF_R_SYM(r->r_info));
1661 case R_ARM_THM_CALL:
1662 case R_ARM_THM_JUMP24:
1663 case R_ARM_THM_JUMP19:
1664 /* From ARM ABI: ((S + A) | T) - P */
1665 r->r_addend = (int)(long)(elf->hdr +
1667 (r->r_offset - sechdr->sh_addr));
1675 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1677 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1678 unsigned int *location = reloc_location(elf, sechdr, r);
1681 if (r_typ == R_MIPS_HI16)
1682 return 1; /* skip this */
1683 inst = TO_NATIVE(*location);
1686 r->r_addend = inst & 0xffff;
1689 r->r_addend = (inst & 0x03ffffff) << 2;
1699 #define EM_RISCV 243
1702 #ifndef R_RISCV_SUB32
1703 #define R_RISCV_SUB32 39
1706 static void section_rela(const char *modname, struct elf_info *elf,
1713 const char *fromsec;
1715 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1716 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1718 fromsec = sech_name(elf, sechdr);
1719 fromsec += strlen(".rela");
1720 /* if from section (name) is know good then skip it */
1721 if (match(fromsec, section_white_list))
1724 for (rela = start; rela < stop; rela++) {
1725 r.r_offset = TO_NATIVE(rela->r_offset);
1726 #if KERNEL_ELFCLASS == ELFCLASS64
1727 if (elf->hdr->e_machine == EM_MIPS) {
1729 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1730 r_sym = TO_NATIVE(r_sym);
1731 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1732 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1734 r.r_info = TO_NATIVE(rela->r_info);
1735 r_sym = ELF_R_SYM(r.r_info);
1738 r.r_info = TO_NATIVE(rela->r_info);
1739 r_sym = ELF_R_SYM(r.r_info);
1741 r.r_addend = TO_NATIVE(rela->r_addend);
1742 switch (elf->hdr->e_machine) {
1744 if (!strcmp("__ex_table", fromsec) &&
1745 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1749 sym = elf->symtab_start + r_sym;
1750 /* Skip special sections */
1751 if (is_shndx_special(sym->st_shndx))
1753 if (is_second_extable_reloc(start, rela, fromsec))
1754 find_extable_entry_size(fromsec, &r);
1755 check_section_mismatch(modname, elf, &r, sym, fromsec);
1759 static void section_rel(const char *modname, struct elf_info *elf,
1766 const char *fromsec;
1768 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1769 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1771 fromsec = sech_name(elf, sechdr);
1772 fromsec += strlen(".rel");
1773 /* if from section (name) is know good then skip it */
1774 if (match(fromsec, section_white_list))
1777 for (rel = start; rel < stop; rel++) {
1778 r.r_offset = TO_NATIVE(rel->r_offset);
1779 #if KERNEL_ELFCLASS == ELFCLASS64
1780 if (elf->hdr->e_machine == EM_MIPS) {
1782 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1783 r_sym = TO_NATIVE(r_sym);
1784 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1785 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1787 r.r_info = TO_NATIVE(rel->r_info);
1788 r_sym = ELF_R_SYM(r.r_info);
1791 r.r_info = TO_NATIVE(rel->r_info);
1792 r_sym = ELF_R_SYM(r.r_info);
1795 switch (elf->hdr->e_machine) {
1797 if (addend_386_rel(elf, sechdr, &r))
1801 if (addend_arm_rel(elf, sechdr, &r))
1805 if (addend_mips_rel(elf, sechdr, &r))
1809 sym = elf->symtab_start + r_sym;
1810 /* Skip special sections */
1811 if (is_shndx_special(sym->st_shndx))
1813 if (is_second_extable_reloc(start, rel, fromsec))
1814 find_extable_entry_size(fromsec, &r);
1815 check_section_mismatch(modname, elf, &r, sym, fromsec);
1820 * A module includes a number of sections that are discarded
1821 * either when loaded or when used as built-in.
1822 * For loaded modules all functions marked __init and all data
1823 * marked __initdata will be discarded when the module has been initialized.
1824 * Likewise for modules used built-in the sections marked __exit
1825 * are discarded because __exit marked function are supposed to be called
1826 * only when a module is unloaded which never happens for built-in modules.
1827 * The check_sec_ref() function traverses all relocation records
1828 * to find all references to a section that reference a section that will
1829 * be discarded and warns about it.
1831 static void check_sec_ref(const char *modname, struct elf_info *elf)
1834 Elf_Shdr *sechdrs = elf->sechdrs;
1836 /* Walk through all sections */
1837 for (i = 0; i < elf->num_sections; i++) {
1838 check_section(modname, elf, &elf->sechdrs[i]);
1839 /* We want to process only relocation sections and not .init */
1840 if (sechdrs[i].sh_type == SHT_RELA)
1841 section_rela(modname, elf, &elf->sechdrs[i]);
1842 else if (sechdrs[i].sh_type == SHT_REL)
1843 section_rel(modname, elf, &elf->sechdrs[i]);
1847 static char *remove_dot(char *s)
1849 size_t n = strcspn(s, ".");
1852 size_t m = strspn(s + n + 1, "0123456789");
1853 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
1860 * The CRCs are recorded in .*.cmd files in the form of:
1861 * #SYMVER <name> <crc>
1863 static void extract_crcs_for_object(const char *object, struct module *mod)
1865 char cmd_file[PATH_MAX];
1870 base = strrchr(object, '/');
1873 dirlen = base - object;
1879 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
1880 dirlen, object, base);
1881 if (ret >= sizeof(cmd_file)) {
1882 error("%s: too long path was truncated\n", cmd_file);
1886 buf = read_text_file(cmd_file);
1889 while ((p = strstr(p, "\n#SYMVER "))) {
1895 name = p + strlen("\n#SYMVER ");
1897 p = strchr(name, ' ');
1905 continue; /* skip this line */
1907 crc = strtol(p, &p, 0);
1909 continue; /* skip this line */
1911 name[namelen] = '\0';
1914 * sym_find_with_module() may return NULL here.
1915 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
1916 * Since commit e1327a127703, genksyms calculates CRCs of all
1917 * symbols, including trimmed ones. Ignore orphan CRCs.
1919 sym = sym_find_with_module(name, mod);
1921 sym_set_crc(sym, crc);
1928 * The symbol versions (CRC) are recorded in the .*.cmd files.
1929 * Parse them to retrieve CRCs for the current module.
1931 static void mod_set_crcs(struct module *mod)
1933 char objlist[PATH_MAX];
1934 char *buf, *p, *obj;
1937 if (mod->is_vmlinux) {
1938 strcpy(objlist, ".vmlinux.objs");
1940 /* objects for a module are listed in the *.mod file. */
1941 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
1942 if (ret >= sizeof(objlist)) {
1943 error("%s: too long path was truncated\n", objlist);
1948 buf = read_text_file(objlist);
1951 while ((obj = strsep(&p, "\n")) && obj[0])
1952 extract_crcs_for_object(obj, mod);
1957 static void read_symbols(const char *modname)
1959 const char *symname;
1964 struct elf_info info = { };
1967 if (!parse_elf(&info, modname))
1970 if (!strends(modname, ".o")) {
1971 error("%s: filename must be suffixed with .o\n", modname);
1975 /* strip trailing .o */
1976 mod = new_module(modname, strlen(modname) - strlen(".o"));
1978 if (!mod->is_vmlinux) {
1979 license = get_modinfo(&info, "license");
1981 error("missing MODULE_LICENSE() in %s\n", modname);
1983 if (!license_is_gpl_compatible(license)) {
1984 mod->is_gpl_compatible = false;
1987 license = get_next_modinfo(&info, "license", license);
1990 namespace = get_modinfo(&info, "import_ns");
1992 add_namespace(&mod->imported_namespaces, namespace);
1993 namespace = get_next_modinfo(&info, "import_ns",
1998 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1999 symname = remove_dot(info.strtab + sym->st_name);
2001 handle_symbol(mod, &info, sym, symname);
2002 handle_moddevtable(mod, &info, sym, symname);
2005 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2006 symname = remove_dot(info.strtab + sym->st_name);
2008 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2009 if (strstarts(symname, "__kstrtabns_"))
2010 sym_update_namespace(symname + strlen("__kstrtabns_"),
2011 sym_get_data(&info, sym));
2014 check_sec_ref(modname, &info);
2016 if (!mod->is_vmlinux) {
2017 version = get_modinfo(&info, "version");
2018 if (version || all_versions)
2019 get_src_version(mod->name, mod->srcversion,
2020 sizeof(mod->srcversion) - 1);
2023 parse_elf_finish(&info);
2027 * Our trick to get versioning for module struct etc. - it's
2028 * never passed as an argument to an exported function, so
2029 * the automatic versioning doesn't pick it up, but it's really
2032 sym_add_unresolved("module_layout", mod, false);
2038 static void read_symbols_from_files(const char *filename)
2041 char fname[PATH_MAX];
2043 if (strcmp(filename, "-") != 0) {
2044 in = fopen(filename, "r");
2046 fatal("Can't open filenames file %s: %m", filename);
2049 while (fgets(fname, PATH_MAX, in) != NULL) {
2050 if (strends(fname, "\n"))
2051 fname[strlen(fname)-1] = '\0';
2052 read_symbols(fname);
2061 /* We first write the generated file into memory using the
2062 * following helper, then compare to the file on disk and
2063 * only update the later if anything changed */
2065 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2066 const char *fmt, ...)
2073 len = vsnprintf(tmp, SZ, fmt, ap);
2074 buf_write(buf, tmp, len);
2078 void buf_write(struct buffer *buf, const char *s, int len)
2080 if (buf->size - buf->pos < len) {
2081 buf->size += len + SZ;
2082 buf->p = NOFAIL(realloc(buf->p, buf->size));
2084 strncpy(buf->p + buf->pos, s, len);
2088 static void check_exports(struct module *mod)
2090 struct symbol *s, *exp;
2092 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2093 const char *basename;
2094 exp = find_symbol(s->name);
2096 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2097 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2098 "\"%s\" [%s.ko] undefined!\n",
2099 s->name, mod->name);
2102 if (exp->module == mod) {
2103 error("\"%s\" [%s.ko] was exported without definition\n",
2104 s->name, mod->name);
2108 s->module = exp->module;
2109 s->crc_valid = exp->crc_valid;
2112 basename = strrchr(mod->name, '/');
2116 basename = mod->name;
2118 if (exp->namespace &&
2119 !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
2120 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2121 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2122 basename, exp->name, exp->namespace);
2123 add_namespace(&mod->missing_namespaces, exp->namespace);
2126 if (!mod->is_gpl_compatible && exp->is_gpl_only)
2127 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2128 basename, exp->name);
2132 static void check_modname_len(struct module *mod)
2134 const char *mod_name;
2136 mod_name = strrchr(mod->name, '/');
2137 if (mod_name == NULL)
2138 mod_name = mod->name;
2141 if (strlen(mod_name) >= MODULE_NAME_LEN)
2142 error("module name is too long [%s.ko]\n", mod->name);
2146 * Header for the generated file
2148 static void add_header(struct buffer *b, struct module *mod)
2150 buf_printf(b, "#include <linux/module.h>\n");
2152 * Include build-salt.h after module.h in order to
2153 * inherit the definitions.
2155 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2156 buf_printf(b, "#include <linux/build-salt.h>\n");
2157 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2158 buf_printf(b, "#include <linux/export-internal.h>\n");
2159 buf_printf(b, "#include <linux/vermagic.h>\n");
2160 buf_printf(b, "#include <linux/compiler.h>\n");
2161 buf_printf(b, "\n");
2162 buf_printf(b, "BUILD_SALT;\n");
2163 buf_printf(b, "BUILD_LTO_INFO;\n");
2164 buf_printf(b, "\n");
2165 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2166 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2167 buf_printf(b, "\n");
2168 buf_printf(b, "__visible struct module __this_module\n");
2169 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2170 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2172 buf_printf(b, "\t.init = init_module,\n");
2173 if (mod->has_cleanup)
2174 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2175 "\t.exit = cleanup_module,\n"
2177 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2178 buf_printf(b, "};\n");
2180 if (!external_module)
2181 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2185 "#ifdef CONFIG_RETPOLINE\n"
2186 "MODULE_INFO(retpoline, \"Y\");\n"
2189 if (strstarts(mod->name, "drivers/staging"))
2190 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2193 static void add_exported_symbols(struct buffer *buf, struct module *mod)
2200 /* record CRCs for exported symbols */
2201 buf_printf(buf, "\n");
2202 list_for_each_entry(sym, &mod->exported_symbols, list) {
2203 if (!sym->crc_valid) {
2204 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
2205 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
2206 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
2211 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
2212 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
2217 * Record CRCs for unresolved symbols
2219 static void add_versions(struct buffer *b, struct module *mod)
2226 buf_printf(b, "\n");
2227 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2228 buf_printf(b, "__used __section(\"__versions\") = {\n");
2230 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2233 if (!s->crc_valid) {
2234 warn("\"%s\" [%s.ko] has no CRC!\n",
2235 s->name, mod->name);
2238 if (strlen(s->name) >= MODULE_NAME_LEN) {
2239 error("too long symbol \"%s\" [%s.ko]\n",
2240 s->name, mod->name);
2243 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2247 buf_printf(b, "};\n");
2250 static void add_depends(struct buffer *b, struct module *mod)
2255 /* Clear ->seen flag of modules that own symbols needed by this. */
2256 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2258 s->module->seen = s->module->is_vmlinux;
2261 buf_printf(b, "\n");
2262 buf_printf(b, "MODULE_INFO(depends, \"");
2263 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2268 if (s->module->seen)
2271 s->module->seen = true;
2272 p = strrchr(s->module->name, '/');
2276 p = s->module->name;
2277 buf_printf(b, "%s%s", first ? "" : ",", p);
2280 buf_printf(b, "\");\n");
2283 static void add_srcversion(struct buffer *b, struct module *mod)
2285 if (mod->srcversion[0]) {
2286 buf_printf(b, "\n");
2287 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2292 static void write_buf(struct buffer *b, const char *fname)
2299 file = fopen(fname, "w");
2304 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2308 if (fclose(file) != 0) {
2314 static void write_if_changed(struct buffer *b, const char *fname)
2320 file = fopen(fname, "r");
2324 if (fstat(fileno(file), &st) < 0)
2327 if (st.st_size != b->pos)
2330 tmp = NOFAIL(malloc(b->pos));
2331 if (fread(tmp, 1, b->pos, file) != b->pos)
2334 if (memcmp(tmp, b->p, b->pos) != 0)
2346 write_buf(b, fname);
2349 static void write_vmlinux_export_c_file(struct module *mod)
2351 struct buffer buf = { };
2354 "#include <linux/export-internal.h>\n");
2356 add_exported_symbols(&buf, mod);
2357 write_if_changed(&buf, ".vmlinux.export.c");
2361 /* do sanity checks, and generate *.mod.c file */
2362 static void write_mod_c_file(struct module *mod)
2364 struct buffer buf = { };
2365 char fname[PATH_MAX];
2368 check_modname_len(mod);
2371 add_header(&buf, mod);
2372 add_exported_symbols(&buf, mod);
2373 add_versions(&buf, mod);
2374 add_depends(&buf, mod);
2375 add_moddevtable(&buf, mod);
2376 add_srcversion(&buf, mod);
2378 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2379 if (ret >= sizeof(fname)) {
2380 error("%s: too long path was truncated\n", fname);
2384 write_if_changed(&buf, fname);
2390 /* parse Module.symvers file. line format:
2391 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2393 static void read_dump(const char *fname)
2395 char *buf, *pos, *line;
2397 buf = read_text_file(fname);
2399 /* No symbol versions, silently ignore */
2404 while ((line = get_line(&pos))) {
2405 char *symname, *namespace, *modname, *d, *export;
2411 if (!(symname = strchr(line, '\t')))
2414 if (!(modname = strchr(symname, '\t')))
2417 if (!(export = strchr(modname, '\t')))
2420 if (!(namespace = strchr(export, '\t')))
2422 *namespace++ = '\0';
2424 crc = strtoul(line, &d, 16);
2425 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2428 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2430 } else if (!strcmp(export, "EXPORT_SYMBOL")) {
2433 error("%s: unknown license %s. skip", symname, export);
2437 mod = find_module(modname);
2439 mod = new_module(modname, strlen(modname));
2440 mod->from_dump = true;
2442 s = sym_add_exported(symname, mod, gpl_only);
2443 sym_set_crc(s, crc);
2444 sym_update_namespace(symname, namespace);
2450 fatal("parse error in symbol dump file\n");
2453 static void write_dump(const char *fname)
2455 struct buffer buf = { };
2459 list_for_each_entry(mod, &modules, list) {
2462 list_for_each_entry(sym, &mod->exported_symbols, list) {
2463 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
2464 sym->crc, sym->name, mod->name,
2465 sym->is_gpl_only ? "_GPL" : "",
2466 sym->namespace ?: "");
2469 write_buf(&buf, fname);
2473 static void write_namespace_deps_files(const char *fname)
2476 struct namespace_list *ns;
2477 struct buffer ns_deps_buf = {};
2479 list_for_each_entry(mod, &modules, list) {
2481 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2484 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2486 list_for_each_entry(ns, &mod->missing_namespaces, list)
2487 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2489 buf_printf(&ns_deps_buf, "\n");
2492 write_if_changed(&ns_deps_buf, fname);
2493 free(ns_deps_buf.p);
2497 struct list_head list;
2501 int main(int argc, char **argv)
2504 char *missing_namespace_deps = NULL;
2505 char *dump_write = NULL, *files_source = NULL;
2507 LIST_HEAD(dump_lists);
2508 struct dump_list *dl, *dl2;
2510 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2513 external_module = true;
2516 dl = NOFAIL(malloc(sizeof(*dl)));
2518 list_add_tail(&dl->list, &dump_lists);
2524 ignore_missing_files = true;
2527 dump_write = optarg;
2530 all_versions = true;
2533 files_source = optarg;
2536 warn_unresolved = true;
2539 sec_mismatch_warn_only = false;
2542 allow_missing_ns_imports = true;
2545 missing_namespace_deps = optarg;
2552 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2553 read_dump(dl->file);
2554 list_del(&dl->list);
2558 while (optind < argc)
2559 read_symbols(argv[optind++]);
2562 read_symbols_from_files(files_source);
2564 list_for_each_entry(mod, &modules, list) {
2568 if (mod->is_vmlinux)
2569 write_vmlinux_export_c_file(mod);
2571 write_mod_c_file(mod);
2574 if (missing_namespace_deps)
2575 write_namespace_deps_files(missing_namespace_deps);
2578 write_dump(dump_write);
2579 if (sec_mismatch_count && !sec_mismatch_warn_only)
2580 error("Section mismatches detected.\n"
2581 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2583 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2584 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2585 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2587 return error_occurred ? 1 : 0;