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 if (info->hdr->e_type != ET_REL)
325 offset -= sechdr->sh_addr;
327 return (void *)info->hdr + sechdr->sh_offset + offset;
330 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
332 return sym_get_data_by_offset(info, get_secindex(info, sym),
336 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
338 return sym_get_data_by_offset(info, info->secindex_strings,
342 static const char *sec_name(const struct elf_info *info, int secindex)
344 return sech_name(info, &info->sechdrs[secindex]);
347 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
349 static void sym_update_namespace(const char *symname, const char *namespace)
351 struct symbol *s = find_symbol(symname);
354 * That symbol should have been created earlier and thus this is
355 * actually an assertion.
358 error("Could not update namespace(%s) for symbol %s\n",
364 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
367 static struct symbol *sym_add_exported(const char *name, struct module *mod,
370 struct symbol *s = find_symbol(name);
372 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
373 error("%s: '%s' exported twice. Previous export was in %s%s\n",
374 mod->name, name, s->module->name,
375 s->module->is_vmlinux ? "" : ".ko");
378 s = alloc_symbol(name);
380 s->is_gpl_only = gpl_only;
381 list_add_tail(&s->list, &mod->exported_symbols);
387 static void sym_set_crc(struct symbol *sym, unsigned int crc)
390 sym->crc_valid = true;
393 static void *grab_file(const char *filename, size_t *size)
396 void *map = MAP_FAILED;
399 fd = open(filename, O_RDONLY);
406 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
410 if (map == MAP_FAILED)
415 static void release_file(void *file, size_t size)
420 static int parse_elf(struct elf_info *info, const char *filename)
426 const char *secstrings;
427 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
429 hdr = grab_file(filename, &info->size);
431 if (ignore_missing_files) {
432 fprintf(stderr, "%s: %s (ignored)\n", filename,
440 if (info->size < sizeof(*hdr)) {
441 /* file too small, assume this is an empty .o file */
444 /* Is this a valid ELF file? */
445 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
446 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
447 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
448 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
449 /* Not an ELF file - silently ignore it */
452 /* Fix endianness in ELF header */
453 hdr->e_type = TO_NATIVE(hdr->e_type);
454 hdr->e_machine = TO_NATIVE(hdr->e_machine);
455 hdr->e_version = TO_NATIVE(hdr->e_version);
456 hdr->e_entry = TO_NATIVE(hdr->e_entry);
457 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
458 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
459 hdr->e_flags = TO_NATIVE(hdr->e_flags);
460 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
461 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
462 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
463 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
464 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
465 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
466 sechdrs = (void *)hdr + hdr->e_shoff;
467 info->sechdrs = sechdrs;
469 /* Check if file offset is correct */
470 if (hdr->e_shoff > info->size) {
471 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
472 (unsigned long)hdr->e_shoff, filename, info->size);
476 if (hdr->e_shnum == SHN_UNDEF) {
478 * There are more than 64k sections,
479 * read count from .sh_size.
481 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
484 info->num_sections = hdr->e_shnum;
486 if (hdr->e_shstrndx == SHN_XINDEX) {
487 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
490 info->secindex_strings = hdr->e_shstrndx;
493 /* Fix endianness in section headers */
494 for (i = 0; i < info->num_sections; i++) {
495 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
496 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
497 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
498 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
499 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
500 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
501 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
502 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
503 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
504 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
506 /* Find symbol table. */
507 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
508 for (i = 1; i < info->num_sections; i++) {
510 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
512 if (!nobits && sechdrs[i].sh_offset > info->size) {
513 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
514 "sizeof(*hrd)=%zu\n", filename,
515 (unsigned long)sechdrs[i].sh_offset,
519 secname = secstrings + sechdrs[i].sh_name;
520 if (strcmp(secname, ".modinfo") == 0) {
522 fatal("%s has NOBITS .modinfo\n", filename);
523 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
524 info->modinfo_len = sechdrs[i].sh_size;
527 if (sechdrs[i].sh_type == SHT_SYMTAB) {
528 unsigned int sh_link_idx;
530 info->symtab_start = (void *)hdr +
531 sechdrs[i].sh_offset;
532 info->symtab_stop = (void *)hdr +
533 sechdrs[i].sh_offset + sechdrs[i].sh_size;
534 sh_link_idx = sechdrs[i].sh_link;
535 info->strtab = (void *)hdr +
536 sechdrs[sh_link_idx].sh_offset;
539 /* 32bit section no. table? ("more than 64k sections") */
540 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
541 symtab_shndx_idx = i;
542 info->symtab_shndx_start = (void *)hdr +
543 sechdrs[i].sh_offset;
544 info->symtab_shndx_stop = (void *)hdr +
545 sechdrs[i].sh_offset + sechdrs[i].sh_size;
548 if (!info->symtab_start)
549 fatal("%s has no symtab?\n", filename);
551 /* Fix endianness in symbols */
552 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
553 sym->st_shndx = TO_NATIVE(sym->st_shndx);
554 sym->st_name = TO_NATIVE(sym->st_name);
555 sym->st_value = TO_NATIVE(sym->st_value);
556 sym->st_size = TO_NATIVE(sym->st_size);
559 if (symtab_shndx_idx != ~0U) {
561 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
562 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
563 filename, sechdrs[symtab_shndx_idx].sh_link,
566 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
574 static void parse_elf_finish(struct elf_info *info)
576 release_file(info->hdr, info->size);
579 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
581 /* ignore __this_module, it will be resolved shortly */
582 if (strcmp(symname, "__this_module") == 0)
584 /* ignore global offset table */
585 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
587 if (info->hdr->e_machine == EM_PPC)
588 /* Special register function linked on all modules during final link of .ko */
589 if (strstarts(symname, "_restgpr_") ||
590 strstarts(symname, "_savegpr_") ||
591 strstarts(symname, "_rest32gpr_") ||
592 strstarts(symname, "_save32gpr_") ||
593 strstarts(symname, "_restvr_") ||
594 strstarts(symname, "_savevr_"))
596 if (info->hdr->e_machine == EM_PPC64)
597 /* Special register function linked on all modules during final link of .ko */
598 if (strstarts(symname, "_restgpr0_") ||
599 strstarts(symname, "_savegpr0_") ||
600 strstarts(symname, "_restvr_") ||
601 strstarts(symname, "_savevr_") ||
602 strcmp(symname, ".TOC.") == 0)
605 if (info->hdr->e_machine == EM_S390)
606 /* Expoline thunks are linked on all kernel modules during final link of .ko */
607 if (strstarts(symname, "__s390_indirect_jump_r"))
609 /* Do not ignore this symbol */
613 static void handle_symbol(struct module *mod, struct elf_info *info,
614 const Elf_Sym *sym, const char *symname)
616 switch (sym->st_shndx) {
618 if (strstarts(symname, "__gnu_lto_")) {
619 /* Should warn here, but modpost runs before the linker */
621 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
624 /* undefined symbol */
625 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
626 ELF_ST_BIND(sym->st_info) != STB_WEAK)
628 if (ignore_undef_symbol(info, symname))
630 if (info->hdr->e_machine == EM_SPARC ||
631 info->hdr->e_machine == EM_SPARCV9) {
632 /* Ignore register directives. */
633 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
635 if (symname[0] == '.') {
636 char *munged = NOFAIL(strdup(symname));
638 munged[1] = toupper(munged[1]);
643 sym_add_unresolved(symname, mod,
644 ELF_ST_BIND(sym->st_info) == STB_WEAK);
647 /* All exported symbols */
648 if (strstarts(symname, "__ksymtab_")) {
649 const char *name, *secname;
651 name = symname + strlen("__ksymtab_");
652 secname = sec_name(info, get_secindex(info, sym));
654 if (strstarts(secname, "___ksymtab_gpl+"))
655 sym_add_exported(name, mod, true);
656 else if (strstarts(secname, "___ksymtab+"))
657 sym_add_exported(name, mod, false);
659 if (strcmp(symname, "init_module") == 0)
660 mod->has_init = true;
661 if (strcmp(symname, "cleanup_module") == 0)
662 mod->has_cleanup = true;
668 * Parse tag=value strings from .modinfo section
670 static char *next_string(char *string, unsigned long *secsize)
672 /* Skip non-zero chars */
675 if ((*secsize)-- <= 1)
679 /* Skip any zero padding. */
682 if ((*secsize)-- <= 1)
688 static char *get_next_modinfo(struct elf_info *info, const char *tag,
692 unsigned int taglen = strlen(tag);
693 char *modinfo = info->modinfo;
694 unsigned long size = info->modinfo_len;
697 size -= prev - modinfo;
698 modinfo = next_string(prev, &size);
701 for (p = modinfo; p; p = next_string(p, &size)) {
702 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
703 return p + taglen + 1;
708 static char *get_modinfo(struct elf_info *info, const char *tag)
711 return get_next_modinfo(info, tag, NULL);
714 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
717 return elf->strtab + sym->st_name;
723 * Check whether the 'string' argument matches one of the 'patterns',
724 * an array of shell wildcard patterns (glob).
726 * Return true is there is a match.
728 static bool match(const char *string, const char *const patterns[])
732 while ((pattern = *patterns++)) {
733 if (!fnmatch(pattern, string, 0))
740 /* sections that we do not want to do full section mismatch check on */
741 static const char *const section_white_list[] =
745 ".cranges", /* sh64 */
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;
1626 /* For CONFIG_RELOCATABLE=y */
1627 if (elf->hdr->e_type == ET_EXEC)
1628 r->r_addend += r->r_offset;
1635 #define R_ARM_CALL 28
1637 #ifndef R_ARM_JUMP24
1638 #define R_ARM_JUMP24 29
1641 #ifndef R_ARM_THM_CALL
1642 #define R_ARM_THM_CALL 10
1644 #ifndef R_ARM_THM_JUMP24
1645 #define R_ARM_THM_JUMP24 30
1647 #ifndef R_ARM_THM_JUMP19
1648 #define R_ARM_THM_JUMP19 51
1651 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1653 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1657 /* From ARM ABI: (S + A) | T */
1658 r->r_addend = (int)(long)
1659 (elf->symtab_start + ELF_R_SYM(r->r_info));
1664 case R_ARM_THM_CALL:
1665 case R_ARM_THM_JUMP24:
1666 case R_ARM_THM_JUMP19:
1667 /* From ARM ABI: ((S + A) | T) - P */
1668 r->r_addend = (int)(long)(elf->hdr +
1670 (r->r_offset - sechdr->sh_addr));
1678 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1680 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1681 unsigned int *location = reloc_location(elf, sechdr, r);
1684 if (r_typ == R_MIPS_HI16)
1685 return 1; /* skip this */
1686 inst = TO_NATIVE(*location);
1689 r->r_addend = inst & 0xffff;
1692 r->r_addend = (inst & 0x03ffffff) << 2;
1702 #define EM_RISCV 243
1705 #ifndef R_RISCV_SUB32
1706 #define R_RISCV_SUB32 39
1709 static void section_rela(const char *modname, struct elf_info *elf,
1716 const char *fromsec;
1718 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1719 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1721 fromsec = sech_name(elf, sechdr);
1722 fromsec += strlen(".rela");
1723 /* if from section (name) is know good then skip it */
1724 if (match(fromsec, section_white_list))
1727 for (rela = start; rela < stop; rela++) {
1728 r.r_offset = TO_NATIVE(rela->r_offset);
1729 #if KERNEL_ELFCLASS == ELFCLASS64
1730 if (elf->hdr->e_machine == EM_MIPS) {
1732 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1733 r_sym = TO_NATIVE(r_sym);
1734 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1735 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1737 r.r_info = TO_NATIVE(rela->r_info);
1738 r_sym = ELF_R_SYM(r.r_info);
1741 r.r_info = TO_NATIVE(rela->r_info);
1742 r_sym = ELF_R_SYM(r.r_info);
1744 r.r_addend = TO_NATIVE(rela->r_addend);
1745 switch (elf->hdr->e_machine) {
1747 if (!strcmp("__ex_table", fromsec) &&
1748 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1752 sym = elf->symtab_start + r_sym;
1753 /* Skip special sections */
1754 if (is_shndx_special(sym->st_shndx))
1756 if (is_second_extable_reloc(start, rela, fromsec))
1757 find_extable_entry_size(fromsec, &r);
1758 check_section_mismatch(modname, elf, &r, sym, fromsec);
1762 static void section_rel(const char *modname, struct elf_info *elf,
1769 const char *fromsec;
1771 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1772 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1774 fromsec = sech_name(elf, sechdr);
1775 fromsec += strlen(".rel");
1776 /* if from section (name) is know good then skip it */
1777 if (match(fromsec, section_white_list))
1780 for (rel = start; rel < stop; rel++) {
1781 r.r_offset = TO_NATIVE(rel->r_offset);
1782 #if KERNEL_ELFCLASS == ELFCLASS64
1783 if (elf->hdr->e_machine == EM_MIPS) {
1785 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1786 r_sym = TO_NATIVE(r_sym);
1787 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1788 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1790 r.r_info = TO_NATIVE(rel->r_info);
1791 r_sym = ELF_R_SYM(r.r_info);
1794 r.r_info = TO_NATIVE(rel->r_info);
1795 r_sym = ELF_R_SYM(r.r_info);
1798 switch (elf->hdr->e_machine) {
1800 if (addend_386_rel(elf, sechdr, &r))
1804 if (addend_arm_rel(elf, sechdr, &r))
1808 if (addend_mips_rel(elf, sechdr, &r))
1812 sym = elf->symtab_start + r_sym;
1813 /* Skip special sections */
1814 if (is_shndx_special(sym->st_shndx))
1816 if (is_second_extable_reloc(start, rel, fromsec))
1817 find_extable_entry_size(fromsec, &r);
1818 check_section_mismatch(modname, elf, &r, sym, fromsec);
1823 * A module includes a number of sections that are discarded
1824 * either when loaded or when used as built-in.
1825 * For loaded modules all functions marked __init and all data
1826 * marked __initdata will be discarded when the module has been initialized.
1827 * Likewise for modules used built-in the sections marked __exit
1828 * are discarded because __exit marked function are supposed to be called
1829 * only when a module is unloaded which never happens for built-in modules.
1830 * The check_sec_ref() function traverses all relocation records
1831 * to find all references to a section that reference a section that will
1832 * be discarded and warns about it.
1834 static void check_sec_ref(const char *modname, struct elf_info *elf)
1837 Elf_Shdr *sechdrs = elf->sechdrs;
1839 /* Walk through all sections */
1840 for (i = 0; i < elf->num_sections; i++) {
1841 check_section(modname, elf, &elf->sechdrs[i]);
1842 /* We want to process only relocation sections and not .init */
1843 if (sechdrs[i].sh_type == SHT_RELA)
1844 section_rela(modname, elf, &elf->sechdrs[i]);
1845 else if (sechdrs[i].sh_type == SHT_REL)
1846 section_rel(modname, elf, &elf->sechdrs[i]);
1850 static char *remove_dot(char *s)
1852 size_t n = strcspn(s, ".");
1855 size_t m = strspn(s + n + 1, "0123456789");
1856 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
1863 * The CRCs are recorded in .*.cmd files in the form of:
1864 * #SYMVER <name> <crc>
1866 static void extract_crcs_for_object(const char *object, struct module *mod)
1868 char cmd_file[PATH_MAX];
1873 base = strrchr(object, '/');
1876 dirlen = base - object;
1882 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
1883 dirlen, object, base);
1884 if (ret >= sizeof(cmd_file)) {
1885 error("%s: too long path was truncated\n", cmd_file);
1889 buf = read_text_file(cmd_file);
1892 while ((p = strstr(p, "\n#SYMVER "))) {
1898 name = p + strlen("\n#SYMVER ");
1900 p = strchr(name, ' ');
1908 continue; /* skip this line */
1910 crc = strtol(p, &p, 0);
1912 continue; /* skip this line */
1914 name[namelen] = '\0';
1917 * sym_find_with_module() may return NULL here.
1918 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
1919 * Since commit e1327a127703, genksyms calculates CRCs of all
1920 * symbols, including trimmed ones. Ignore orphan CRCs.
1922 sym = sym_find_with_module(name, mod);
1924 sym_set_crc(sym, crc);
1931 * The symbol versions (CRC) are recorded in the .*.cmd files.
1932 * Parse them to retrieve CRCs for the current module.
1934 static void mod_set_crcs(struct module *mod)
1936 char objlist[PATH_MAX];
1937 char *buf, *p, *obj;
1940 if (mod->is_vmlinux) {
1941 strcpy(objlist, ".vmlinux.objs");
1943 /* objects for a module are listed in the *.mod file. */
1944 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
1945 if (ret >= sizeof(objlist)) {
1946 error("%s: too long path was truncated\n", objlist);
1951 buf = read_text_file(objlist);
1954 while ((obj = strsep(&p, "\n")) && obj[0])
1955 extract_crcs_for_object(obj, mod);
1960 static void read_symbols(const char *modname)
1962 const char *symname;
1967 struct elf_info info = { };
1970 if (!parse_elf(&info, modname))
1973 if (!strends(modname, ".o")) {
1974 error("%s: filename must be suffixed with .o\n", modname);
1978 /* strip trailing .o */
1979 mod = new_module(modname, strlen(modname) - strlen(".o"));
1981 if (!mod->is_vmlinux) {
1982 license = get_modinfo(&info, "license");
1984 error("missing MODULE_LICENSE() in %s\n", modname);
1986 if (!license_is_gpl_compatible(license)) {
1987 mod->is_gpl_compatible = false;
1990 license = get_next_modinfo(&info, "license", license);
1993 namespace = get_modinfo(&info, "import_ns");
1995 add_namespace(&mod->imported_namespaces, namespace);
1996 namespace = get_next_modinfo(&info, "import_ns",
2001 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2002 symname = remove_dot(info.strtab + sym->st_name);
2004 handle_symbol(mod, &info, sym, symname);
2005 handle_moddevtable(mod, &info, sym, symname);
2008 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2009 symname = remove_dot(info.strtab + sym->st_name);
2011 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2012 if (strstarts(symname, "__kstrtabns_"))
2013 sym_update_namespace(symname + strlen("__kstrtabns_"),
2014 sym_get_data(&info, sym));
2017 check_sec_ref(modname, &info);
2019 if (!mod->is_vmlinux) {
2020 version = get_modinfo(&info, "version");
2021 if (version || all_versions)
2022 get_src_version(mod->name, mod->srcversion,
2023 sizeof(mod->srcversion) - 1);
2026 parse_elf_finish(&info);
2030 * Our trick to get versioning for module struct etc. - it's
2031 * never passed as an argument to an exported function, so
2032 * the automatic versioning doesn't pick it up, but it's really
2035 sym_add_unresolved("module_layout", mod, false);
2041 static void read_symbols_from_files(const char *filename)
2044 char fname[PATH_MAX];
2046 if (strcmp(filename, "-") != 0) {
2047 in = fopen(filename, "r");
2049 fatal("Can't open filenames file %s: %m", filename);
2052 while (fgets(fname, PATH_MAX, in) != NULL) {
2053 if (strends(fname, "\n"))
2054 fname[strlen(fname)-1] = '\0';
2055 read_symbols(fname);
2064 /* We first write the generated file into memory using the
2065 * following helper, then compare to the file on disk and
2066 * only update the later if anything changed */
2068 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2069 const char *fmt, ...)
2076 len = vsnprintf(tmp, SZ, fmt, ap);
2077 buf_write(buf, tmp, len);
2081 void buf_write(struct buffer *buf, const char *s, int len)
2083 if (buf->size - buf->pos < len) {
2084 buf->size += len + SZ;
2085 buf->p = NOFAIL(realloc(buf->p, buf->size));
2087 strncpy(buf->p + buf->pos, s, len);
2091 static void check_exports(struct module *mod)
2093 struct symbol *s, *exp;
2095 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2096 const char *basename;
2097 exp = find_symbol(s->name);
2099 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2100 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2101 "\"%s\" [%s.ko] undefined!\n",
2102 s->name, mod->name);
2105 if (exp->module == mod) {
2106 error("\"%s\" [%s.ko] was exported without definition\n",
2107 s->name, mod->name);
2111 s->module = exp->module;
2112 s->crc_valid = exp->crc_valid;
2115 basename = strrchr(mod->name, '/');
2119 basename = mod->name;
2121 if (exp->namespace &&
2122 !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
2123 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2124 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2125 basename, exp->name, exp->namespace);
2126 add_namespace(&mod->missing_namespaces, exp->namespace);
2129 if (!mod->is_gpl_compatible && exp->is_gpl_only)
2130 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2131 basename, exp->name);
2135 static void check_modname_len(struct module *mod)
2137 const char *mod_name;
2139 mod_name = strrchr(mod->name, '/');
2140 if (mod_name == NULL)
2141 mod_name = mod->name;
2144 if (strlen(mod_name) >= MODULE_NAME_LEN)
2145 error("module name is too long [%s.ko]\n", mod->name);
2149 * Header for the generated file
2151 static void add_header(struct buffer *b, struct module *mod)
2153 buf_printf(b, "#include <linux/module.h>\n");
2155 * Include build-salt.h after module.h in order to
2156 * inherit the definitions.
2158 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2159 buf_printf(b, "#include <linux/build-salt.h>\n");
2160 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2161 buf_printf(b, "#include <linux/export-internal.h>\n");
2162 buf_printf(b, "#include <linux/vermagic.h>\n");
2163 buf_printf(b, "#include <linux/compiler.h>\n");
2164 buf_printf(b, "\n");
2165 buf_printf(b, "BUILD_SALT;\n");
2166 buf_printf(b, "BUILD_LTO_INFO;\n");
2167 buf_printf(b, "\n");
2168 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2169 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2170 buf_printf(b, "\n");
2171 buf_printf(b, "__visible struct module __this_module\n");
2172 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2173 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2175 buf_printf(b, "\t.init = init_module,\n");
2176 if (mod->has_cleanup)
2177 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2178 "\t.exit = cleanup_module,\n"
2180 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2181 buf_printf(b, "};\n");
2183 if (!external_module)
2184 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2188 "#ifdef CONFIG_RETPOLINE\n"
2189 "MODULE_INFO(retpoline, \"Y\");\n"
2192 if (strstarts(mod->name, "drivers/staging"))
2193 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2196 static void add_exported_symbols(struct buffer *buf, struct module *mod)
2203 /* record CRCs for exported symbols */
2204 buf_printf(buf, "\n");
2205 list_for_each_entry(sym, &mod->exported_symbols, list) {
2206 if (!sym->crc_valid) {
2207 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
2208 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
2209 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
2214 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
2215 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
2220 * Record CRCs for unresolved symbols
2222 static void add_versions(struct buffer *b, struct module *mod)
2229 buf_printf(b, "\n");
2230 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2231 buf_printf(b, "__used __section(\"__versions\") = {\n");
2233 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2236 if (!s->crc_valid) {
2237 warn("\"%s\" [%s.ko] has no CRC!\n",
2238 s->name, mod->name);
2241 if (strlen(s->name) >= MODULE_NAME_LEN) {
2242 error("too long symbol \"%s\" [%s.ko]\n",
2243 s->name, mod->name);
2246 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2250 buf_printf(b, "};\n");
2253 static void add_depends(struct buffer *b, struct module *mod)
2258 /* Clear ->seen flag of modules that own symbols needed by this. */
2259 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2261 s->module->seen = s->module->is_vmlinux;
2264 buf_printf(b, "\n");
2265 buf_printf(b, "MODULE_INFO(depends, \"");
2266 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2271 if (s->module->seen)
2274 s->module->seen = true;
2275 p = strrchr(s->module->name, '/');
2279 p = s->module->name;
2280 buf_printf(b, "%s%s", first ? "" : ",", p);
2283 buf_printf(b, "\");\n");
2286 static void add_srcversion(struct buffer *b, struct module *mod)
2288 if (mod->srcversion[0]) {
2289 buf_printf(b, "\n");
2290 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2295 static void write_buf(struct buffer *b, const char *fname)
2302 file = fopen(fname, "w");
2307 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2311 if (fclose(file) != 0) {
2317 static void write_if_changed(struct buffer *b, const char *fname)
2323 file = fopen(fname, "r");
2327 if (fstat(fileno(file), &st) < 0)
2330 if (st.st_size != b->pos)
2333 tmp = NOFAIL(malloc(b->pos));
2334 if (fread(tmp, 1, b->pos, file) != b->pos)
2337 if (memcmp(tmp, b->p, b->pos) != 0)
2349 write_buf(b, fname);
2352 static void write_vmlinux_export_c_file(struct module *mod)
2354 struct buffer buf = { };
2357 "#include <linux/export-internal.h>\n");
2359 add_exported_symbols(&buf, mod);
2360 write_if_changed(&buf, ".vmlinux.export.c");
2364 /* do sanity checks, and generate *.mod.c file */
2365 static void write_mod_c_file(struct module *mod)
2367 struct buffer buf = { };
2368 char fname[PATH_MAX];
2371 check_modname_len(mod);
2374 add_header(&buf, mod);
2375 add_exported_symbols(&buf, mod);
2376 add_versions(&buf, mod);
2377 add_depends(&buf, mod);
2378 add_moddevtable(&buf, mod);
2379 add_srcversion(&buf, mod);
2381 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2382 if (ret >= sizeof(fname)) {
2383 error("%s: too long path was truncated\n", fname);
2387 write_if_changed(&buf, fname);
2393 /* parse Module.symvers file. line format:
2394 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2396 static void read_dump(const char *fname)
2398 char *buf, *pos, *line;
2400 buf = read_text_file(fname);
2402 /* No symbol versions, silently ignore */
2407 while ((line = get_line(&pos))) {
2408 char *symname, *namespace, *modname, *d, *export;
2414 if (!(symname = strchr(line, '\t')))
2417 if (!(modname = strchr(symname, '\t')))
2420 if (!(export = strchr(modname, '\t')))
2423 if (!(namespace = strchr(export, '\t')))
2425 *namespace++ = '\0';
2427 crc = strtoul(line, &d, 16);
2428 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2431 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2433 } else if (!strcmp(export, "EXPORT_SYMBOL")) {
2436 error("%s: unknown license %s. skip", symname, export);
2440 mod = find_module(modname);
2442 mod = new_module(modname, strlen(modname));
2443 mod->from_dump = true;
2445 s = sym_add_exported(symname, mod, gpl_only);
2446 sym_set_crc(s, crc);
2447 sym_update_namespace(symname, namespace);
2453 fatal("parse error in symbol dump file\n");
2456 static void write_dump(const char *fname)
2458 struct buffer buf = { };
2462 list_for_each_entry(mod, &modules, list) {
2465 list_for_each_entry(sym, &mod->exported_symbols, list) {
2466 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
2467 sym->crc, sym->name, mod->name,
2468 sym->is_gpl_only ? "_GPL" : "",
2469 sym->namespace ?: "");
2472 write_buf(&buf, fname);
2476 static void write_namespace_deps_files(const char *fname)
2479 struct namespace_list *ns;
2480 struct buffer ns_deps_buf = {};
2482 list_for_each_entry(mod, &modules, list) {
2484 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2487 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2489 list_for_each_entry(ns, &mod->missing_namespaces, list)
2490 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2492 buf_printf(&ns_deps_buf, "\n");
2495 write_if_changed(&ns_deps_buf, fname);
2496 free(ns_deps_buf.p);
2500 struct list_head list;
2504 int main(int argc, char **argv)
2507 char *missing_namespace_deps = NULL;
2508 char *dump_write = NULL, *files_source = NULL;
2510 LIST_HEAD(dump_lists);
2511 struct dump_list *dl, *dl2;
2513 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2516 external_module = true;
2519 dl = NOFAIL(malloc(sizeof(*dl)));
2521 list_add_tail(&dl->list, &dump_lists);
2527 ignore_missing_files = true;
2530 dump_write = optarg;
2533 all_versions = true;
2536 files_source = optarg;
2539 warn_unresolved = true;
2542 sec_mismatch_warn_only = false;
2545 allow_missing_ns_imports = true;
2548 missing_namespace_deps = optarg;
2555 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2556 read_dump(dl->file);
2557 list_del(&dl->list);
2561 while (optind < argc)
2562 read_symbols(argv[optind++]);
2565 read_symbols_from_files(files_source);
2567 list_for_each_entry(mod, &modules, list) {
2571 if (mod->is_vmlinux)
2572 write_vmlinux_export_c_file(mod);
2574 write_mod_c_file(mod);
2577 if (missing_namespace_deps)
2578 write_namespace_deps_files(missing_namespace_deps);
2581 write_dump(dump_write);
2582 if (sec_mismatch_count && !sec_mismatch_warn_only)
2583 error("Section mismatches detected.\n"
2584 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2586 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2587 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2588 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2590 return error_occurred ? 1 : 0;