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 ...
23 #include "../../include/linux/license.h"
25 /* Are we using CONFIG_MODVERSIONS? */
26 static int modversions;
27 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28 static int all_versions;
29 /* If we are modposting external module set to 1 */
30 static int external_module;
31 /* Only warn about unresolved symbols */
32 static int warn_unresolved;
33 /* How a symbol is exported */
34 static int sec_mismatch_count;
35 static int sec_mismatch_warn_only = true;
36 /* ignore missing files */
37 static int ignore_missing_files;
38 /* If set to 1, only warn (instead of error) about missing ns imports */
39 static int allow_missing_ns_imports;
41 static bool error_occurred;
44 * Cut off the warnings when there are too many. This typically occurs when
45 * vmlinux is missing. ('make modules' without building vmlinux.)
47 #define MAX_UNRESOLVED_REPORTS 10
48 static unsigned int nr_unresolved;
56 /* In kernel, this size is defined in linux/module.h;
57 * here we use Elf_Addr instead of long for covering cross-compile
60 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
62 void __attribute__((format(printf, 2, 3)))
63 modpost_log(enum loglevel loglevel, const char *fmt, ...)
69 fprintf(stderr, "WARNING: ");
72 fprintf(stderr, "ERROR: ");
75 fprintf(stderr, "FATAL: ");
77 default: /* invalid loglevel, ignore */
81 fprintf(stderr, "modpost: ");
83 va_start(arglist, fmt);
84 vfprintf(stderr, fmt, arglist);
87 if (loglevel == LOG_FATAL)
89 if (loglevel == LOG_ERROR)
90 error_occurred = true;
93 static inline bool strends(const char *str, const char *postfix)
95 if (strlen(str) < strlen(postfix))
98 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
101 void *do_nofail(void *ptr, const char *expr)
104 fatal("Memory allocation failure: %s.\n", expr);
109 char *read_text_file(const char *filename)
116 fd = open(filename, O_RDONLY);
122 if (fstat(fd, &st) < 0) {
127 buf = NOFAIL(malloc(st.st_size + 1));
134 bytes_read = read(fd, buf, nbytes);
135 if (bytes_read < 0) {
140 nbytes -= bytes_read;
142 buf[st.st_size] = '\0';
149 char *get_line(char **stringp)
151 char *orig = *stringp, *next;
153 /* do not return the unwanted extra line at EOF */
154 if (!orig || *orig == '\0')
157 /* don't use strsep here, it is not available everywhere */
158 next = strchr(orig, '\n');
167 /* A list of all modules we processed */
168 static struct module *modules;
170 static struct module *find_module(const char *modname)
174 for (mod = modules; mod; mod = mod->next)
175 if (strcmp(mod->name, modname) == 0)
180 static struct module *new_module(const char *modname)
184 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
185 memset(mod, 0, sizeof(*mod));
188 strcpy(mod->name, modname);
189 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
190 mod->gpl_compatible = -1;
197 /* A hash of all exported symbols,
198 * struct symbol is also used for lists of unresolved symbols */
200 #define SYMBOL_HASH_SIZE 1024
204 struct module *module;
209 unsigned int is_static:1; /* 1 if symbol is not global */
210 enum export export; /* Type of export */
214 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
216 /* This is based on the hash algorithm from gdbm, via tdb */
217 static inline unsigned int tdb_hash(const char *name)
219 unsigned value; /* Used to compute the hash value. */
220 unsigned i; /* Used to cycle through random values. */
222 /* Set the initial value from the key size. */
223 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
224 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
226 return (1103515243 * value + 12345);
230 * Allocate a new symbols for use in the hash of exported symbols or
231 * the list of unresolved symbols per module
233 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
236 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
238 memset(s, 0, sizeof(*s));
239 strcpy(s->name, name);
246 /* For the hash of exported symbols */
247 static struct symbol *new_symbol(const char *name, struct module *module,
252 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
253 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
255 return symbolhash[hash];
258 static struct symbol *find_symbol(const char *name)
262 /* For our purposes, .foo matches foo. PPC64 needs this. */
266 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
267 if (strcmp(s->name, name) == 0)
273 static bool contains_namespace(struct namespace_list *list,
274 const char *namespace)
276 for (; list; list = list->next)
277 if (!strcmp(list->namespace, namespace))
283 static void add_namespace(struct namespace_list **list, const char *namespace)
285 struct namespace_list *ns_entry;
287 if (!contains_namespace(*list, namespace)) {
288 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
289 strlen(namespace) + 1));
290 strcpy(ns_entry->namespace, namespace);
291 ns_entry->next = *list;
296 static bool module_imports_namespace(struct module *module,
297 const char *namespace)
299 return contains_namespace(module->imported_namespaces, namespace);
302 static const struct {
306 { .str = "EXPORT_SYMBOL", .export = export_plain },
307 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
308 { .str = "(unknown)", .export = export_unknown },
312 static const char *export_str(enum export ex)
314 return export_list[ex].str;
317 static enum export export_no(const char *s)
322 return export_unknown;
323 for (i = 0; export_list[i].export != export_unknown; i++) {
324 if (strcmp(export_list[i].str, s) == 0)
325 return export_list[i].export;
327 return export_unknown;
330 static void *sym_get_data_by_offset(const struct elf_info *info,
331 unsigned int secindex, unsigned long offset)
333 Elf_Shdr *sechdr = &info->sechdrs[secindex];
335 if (info->hdr->e_type != ET_REL)
336 offset -= sechdr->sh_addr;
338 return (void *)info->hdr + sechdr->sh_offset + offset;
341 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
343 return sym_get_data_by_offset(info, get_secindex(info, sym),
347 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
349 return sym_get_data_by_offset(info, info->secindex_strings,
353 static const char *sec_name(const struct elf_info *info, int secindex)
355 return sech_name(info, &info->sechdrs[secindex]);
358 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
360 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
362 const char *secname = sec_name(elf, sec);
364 if (strstarts(secname, "___ksymtab+"))
366 else if (strstarts(secname, "___ksymtab_gpl+"))
369 return export_unknown;
372 static const char *namespace_from_kstrtabns(const struct elf_info *info,
375 const char *value = sym_get_data(info, sym);
376 return value[0] ? value : NULL;
379 static void sym_update_namespace(const char *symname, const char *namespace)
381 struct symbol *s = find_symbol(symname);
384 * That symbol should have been created earlier and thus this is
385 * actually an assertion.
388 error("Could not update namespace(%s) for symbol %s\n",
395 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
399 * Add an exported symbol - it may have already been added without a
400 * CRC, in this case just update the CRC
402 static struct symbol *sym_add_exported(const char *name, struct module *mod,
405 struct symbol *s = find_symbol(name);
408 s = new_symbol(name, mod, export);
409 } else if (!external_module || s->module->is_vmlinux ||
411 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
412 mod->name, name, s->module->name,
413 s->module->is_vmlinux ? "" : ".ko");
422 static void sym_set_crc(const char *name, unsigned int crc)
424 struct symbol *s = find_symbol(name);
427 * Ignore stand-alone __crc_*, which might be auto-generated symbols
428 * such as __*_veneer in ARM ELF.
437 static void *grab_file(const char *filename, size_t *size)
440 void *map = MAP_FAILED;
443 fd = open(filename, O_RDONLY);
450 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
454 if (map == MAP_FAILED)
459 static void release_file(void *file, size_t size)
464 static int parse_elf(struct elf_info *info, const char *filename)
470 const char *secstrings;
471 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
473 hdr = grab_file(filename, &info->size);
475 if (ignore_missing_files) {
476 fprintf(stderr, "%s: %s (ignored)\n", filename,
484 if (info->size < sizeof(*hdr)) {
485 /* file too small, assume this is an empty .o file */
488 /* Is this a valid ELF file? */
489 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
490 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
491 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
492 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
493 /* Not an ELF file - silently ignore it */
496 /* Fix endianness in ELF header */
497 hdr->e_type = TO_NATIVE(hdr->e_type);
498 hdr->e_machine = TO_NATIVE(hdr->e_machine);
499 hdr->e_version = TO_NATIVE(hdr->e_version);
500 hdr->e_entry = TO_NATIVE(hdr->e_entry);
501 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
502 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
503 hdr->e_flags = TO_NATIVE(hdr->e_flags);
504 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
505 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
506 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
507 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
508 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
509 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
510 sechdrs = (void *)hdr + hdr->e_shoff;
511 info->sechdrs = sechdrs;
513 /* Check if file offset is correct */
514 if (hdr->e_shoff > info->size) {
515 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
516 (unsigned long)hdr->e_shoff, filename, info->size);
520 if (hdr->e_shnum == SHN_UNDEF) {
522 * There are more than 64k sections,
523 * read count from .sh_size.
525 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
528 info->num_sections = hdr->e_shnum;
530 if (hdr->e_shstrndx == SHN_XINDEX) {
531 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
534 info->secindex_strings = hdr->e_shstrndx;
537 /* Fix endianness in section headers */
538 for (i = 0; i < info->num_sections; i++) {
539 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
540 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
541 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
542 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
543 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
544 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
545 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
546 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
547 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
548 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
550 /* Find symbol table. */
551 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
552 for (i = 1; i < info->num_sections; i++) {
554 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
556 if (!nobits && sechdrs[i].sh_offset > info->size) {
557 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
558 "sizeof(*hrd)=%zu\n", filename,
559 (unsigned long)sechdrs[i].sh_offset,
563 secname = secstrings + sechdrs[i].sh_name;
564 if (strcmp(secname, ".modinfo") == 0) {
566 fatal("%s has NOBITS .modinfo\n", filename);
567 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
568 info->modinfo_len = sechdrs[i].sh_size;
571 if (sechdrs[i].sh_type == SHT_SYMTAB) {
572 unsigned int sh_link_idx;
574 info->symtab_start = (void *)hdr +
575 sechdrs[i].sh_offset;
576 info->symtab_stop = (void *)hdr +
577 sechdrs[i].sh_offset + sechdrs[i].sh_size;
578 sh_link_idx = sechdrs[i].sh_link;
579 info->strtab = (void *)hdr +
580 sechdrs[sh_link_idx].sh_offset;
583 /* 32bit section no. table? ("more than 64k sections") */
584 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
585 symtab_shndx_idx = i;
586 info->symtab_shndx_start = (void *)hdr +
587 sechdrs[i].sh_offset;
588 info->symtab_shndx_stop = (void *)hdr +
589 sechdrs[i].sh_offset + sechdrs[i].sh_size;
592 if (!info->symtab_start)
593 fatal("%s has no symtab?\n", filename);
595 /* Fix endianness in symbols */
596 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
597 sym->st_shndx = TO_NATIVE(sym->st_shndx);
598 sym->st_name = TO_NATIVE(sym->st_name);
599 sym->st_value = TO_NATIVE(sym->st_value);
600 sym->st_size = TO_NATIVE(sym->st_size);
603 if (symtab_shndx_idx != ~0U) {
605 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
606 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
607 filename, sechdrs[symtab_shndx_idx].sh_link,
610 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
618 static void parse_elf_finish(struct elf_info *info)
620 release_file(info->hdr, info->size);
623 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
625 /* ignore __this_module, it will be resolved shortly */
626 if (strcmp(symname, "__this_module") == 0)
628 /* ignore global offset table */
629 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
631 if (info->hdr->e_machine == EM_PPC)
632 /* Special register function linked on all modules during final link of .ko */
633 if (strstarts(symname, "_restgpr_") ||
634 strstarts(symname, "_savegpr_") ||
635 strstarts(symname, "_rest32gpr_") ||
636 strstarts(symname, "_save32gpr_") ||
637 strstarts(symname, "_restvr_") ||
638 strstarts(symname, "_savevr_"))
640 if (info->hdr->e_machine == EM_PPC64)
641 /* Special register function linked on all modules during final link of .ko */
642 if (strstarts(symname, "_restgpr0_") ||
643 strstarts(symname, "_savegpr0_") ||
644 strstarts(symname, "_restvr_") ||
645 strstarts(symname, "_savevr_") ||
646 strcmp(symname, ".TOC.") == 0)
649 if (info->hdr->e_machine == EM_S390)
650 /* Expoline thunks are linked on all kernel modules during final link of .ko */
651 if (strstarts(symname, "__s390_indirect_jump_r"))
653 /* Do not ignore this symbol */
657 static void handle_modversion(const struct module *mod,
658 const struct elf_info *info,
659 const Elf_Sym *sym, const char *symname)
663 if (sym->st_shndx == SHN_UNDEF) {
664 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
665 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
666 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
672 if (sym->st_shndx == SHN_ABS) {
677 /* symbol points to the CRC in the ELF object */
678 crcp = sym_get_data(info, sym);
679 crc = TO_NATIVE(*crcp);
681 sym_set_crc(symname, crc);
684 static void handle_symbol(struct module *mod, struct elf_info *info,
685 const Elf_Sym *sym, const char *symname)
689 switch (sym->st_shndx) {
691 if (strstarts(symname, "__gnu_lto_")) {
692 /* Should warn here, but modpost runs before the linker */
694 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
697 /* undefined symbol */
698 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
699 ELF_ST_BIND(sym->st_info) != STB_WEAK)
701 if (ignore_undef_symbol(info, symname))
703 if (info->hdr->e_machine == EM_SPARC ||
704 info->hdr->e_machine == EM_SPARCV9) {
705 /* Ignore register directives. */
706 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
708 if (symname[0] == '.') {
709 char *munged = NOFAIL(strdup(symname));
711 munged[1] = toupper(munged[1]);
716 mod->unres = alloc_symbol(symname,
717 ELF_ST_BIND(sym->st_info) == STB_WEAK,
721 /* All exported symbols */
722 if (strstarts(symname, "__ksymtab_")) {
725 name = symname + strlen("__ksymtab_");
726 export = export_from_secname(info,
727 get_secindex(info, sym));
728 sym_add_exported(name, mod, export);
730 if (strcmp(symname, "init_module") == 0)
732 if (strcmp(symname, "cleanup_module") == 0)
733 mod->has_cleanup = 1;
739 * Parse tag=value strings from .modinfo section
741 static char *next_string(char *string, unsigned long *secsize)
743 /* Skip non-zero chars */
746 if ((*secsize)-- <= 1)
750 /* Skip any zero padding. */
753 if ((*secsize)-- <= 1)
759 static char *get_next_modinfo(struct elf_info *info, const char *tag,
763 unsigned int taglen = strlen(tag);
764 char *modinfo = info->modinfo;
765 unsigned long size = info->modinfo_len;
768 size -= prev - modinfo;
769 modinfo = next_string(prev, &size);
772 for (p = modinfo; p; p = next_string(p, &size)) {
773 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
774 return p + taglen + 1;
779 static char *get_modinfo(struct elf_info *info, const char *tag)
782 return get_next_modinfo(info, tag, NULL);
786 * Test if string s ends in string sub
789 static int strrcmp(const char *s, const char *sub)
797 sublen = strlen(sub);
799 if ((slen == 0) || (sublen == 0))
805 return memcmp(s + slen - sublen, sub, sublen);
808 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
811 return elf->strtab + sym->st_name;
816 /* The pattern is an array of simple patterns.
817 * "foo" will match an exact string equal to "foo"
818 * "*foo" will match a string that ends with "foo"
819 * "foo*" will match a string that begins with "foo"
820 * "*foo*" will match a string that contains "foo"
822 static int match(const char *sym, const char * const pat[])
829 endp = p + strlen(p) - 1;
832 if (*p == '*' && *endp == '*') {
833 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
834 char *here = strstr(sym, bare);
841 else if (*p == '*') {
842 if (strrcmp(sym, p + 1) == 0)
846 else if (*endp == '*') {
847 if (strncmp(sym, p, strlen(p) - 1) == 0)
852 if (strcmp(p, sym) == 0)
860 /* sections that we do not want to do full section mismatch check on */
861 static const char *const section_white_list[] =
865 ".cranges", /* sh64 */
866 ".zdebug*", /* Compressed debug sections. */
867 ".GCC.command.line", /* record-gcc-switches */
868 ".mdebug*", /* alpha, score, mips etc. */
869 ".pdr", /* alpha, score, mips etc. */
874 ".xt.prop", /* xtensa */
875 ".xt.lit", /* xtensa */
876 ".arcextmap*", /* arc */
877 ".gnu.linkonce.arcext*", /* arc : modules */
878 ".cmem*", /* EZchip */
879 ".fmt_slot*", /* EZchip */
886 * This is used to find sections missing the SHF_ALLOC flag.
887 * The cause of this is often a section specified in assembler
888 * without "ax" / "aw".
890 static void check_section(const char *modname, struct elf_info *elf,
893 const char *sec = sech_name(elf, sechdr);
895 if (sechdr->sh_type == SHT_PROGBITS &&
896 !(sechdr->sh_flags & SHF_ALLOC) &&
897 !match(sec, section_white_list)) {
898 warn("%s (%s): unexpected non-allocatable section.\n"
899 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
900 "Note that for example <linux/init.h> contains\n"
901 "section definitions for use in .S files.\n\n",
908 #define ALL_INIT_DATA_SECTIONS \
909 ".init.setup", ".init.rodata", ".meminit.rodata", \
910 ".init.data", ".meminit.data"
911 #define ALL_EXIT_DATA_SECTIONS \
912 ".exit.data", ".memexit.data"
914 #define ALL_INIT_TEXT_SECTIONS \
915 ".init.text", ".meminit.text"
916 #define ALL_EXIT_TEXT_SECTIONS \
917 ".exit.text", ".memexit.text"
919 #define ALL_PCI_INIT_SECTIONS \
920 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
921 ".pci_fixup_enable", ".pci_fixup_resume", \
922 ".pci_fixup_resume_early", ".pci_fixup_suspend"
924 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
925 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
927 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
928 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
930 #define DATA_SECTIONS ".data", ".data.rel"
931 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
932 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
933 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
934 ".fixup", ".entry.text", ".exception.text", ".text.*", \
935 ".coldtext", ".softirqentry.text"
937 #define INIT_SECTIONS ".init.*"
938 #define MEM_INIT_SECTIONS ".meminit.*"
940 #define EXIT_SECTIONS ".exit.*"
941 #define MEM_EXIT_SECTIONS ".memexit.*"
943 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
944 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
946 /* init data sections */
947 static const char *const init_data_sections[] =
948 { ALL_INIT_DATA_SECTIONS, NULL };
950 /* all init sections */
951 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
953 /* All init and exit sections (code + data) */
954 static const char *const init_exit_sections[] =
955 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
957 /* all text sections */
958 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
961 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
964 /* symbols in .data that may refer to init/exit sections */
965 #define DEFAULT_SYMBOL_WHITE_LIST \
967 "*_template", /* scsi uses *_template a lot */ \
968 "*_timer", /* arm uses ops structures named _timer a lot */ \
969 "*_sht", /* scsi also used *_sht to some extent */ \
975 static const char *const head_sections[] = { ".head.text*", NULL };
976 static const char *const linker_symbols[] =
977 { "__init_begin", "_sinittext", "_einittext", NULL };
978 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
985 XXXINIT_TO_SOME_INIT,
986 XXXEXIT_TO_SOME_EXIT,
987 ANY_INIT_TO_ANY_EXIT,
988 ANY_EXIT_TO_ANY_INIT,
994 * Describe how to match sections on different criteria:
996 * @fromsec: Array of sections to be matched.
998 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
999 * this array is forbidden (black-list). Can be empty.
1001 * @good_tosec: Relocations applied to a section in @fromsec must be
1002 * targeting sections in this array (white-list). Can be empty.
1004 * @mismatch: Type of mismatch.
1006 * @symbol_white_list: Do not match a relocation to a symbol in this list
1007 * even if it is targeting a section in @bad_to_sec.
1009 * @handler: Specific handler to call when a match is found. If NULL,
1010 * default_mismatch_handler() will be called.
1013 struct sectioncheck {
1014 const char *fromsec[20];
1015 const char *bad_tosec[20];
1016 const char *good_tosec[20];
1017 enum mismatch mismatch;
1018 const char *symbol_white_list[20];
1019 void (*handler)(const char *modname, struct elf_info *elf,
1020 const struct sectioncheck* const mismatch,
1021 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1025 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1026 const struct sectioncheck* const mismatch,
1027 Elf_Rela *r, Elf_Sym *sym,
1028 const char *fromsec);
1030 static const struct sectioncheck sectioncheck[] = {
1031 /* Do not reference init/exit code/data from
1032 * normal code and data
1035 .fromsec = { TEXT_SECTIONS, NULL },
1036 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1037 .mismatch = TEXT_TO_ANY_INIT,
1038 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1041 .fromsec = { DATA_SECTIONS, NULL },
1042 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1043 .mismatch = DATA_TO_ANY_INIT,
1044 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1047 .fromsec = { DATA_SECTIONS, NULL },
1048 .bad_tosec = { INIT_SECTIONS, NULL },
1049 .mismatch = DATA_TO_ANY_INIT,
1050 .symbol_white_list = {
1051 "*_template", "*_timer", "*_sht", "*_ops",
1052 "*_probe", "*_probe_one", "*_console", NULL
1056 .fromsec = { TEXT_SECTIONS, NULL },
1057 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1058 .mismatch = TEXT_TO_ANY_EXIT,
1059 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1062 .fromsec = { DATA_SECTIONS, NULL },
1063 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1064 .mismatch = DATA_TO_ANY_EXIT,
1065 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1067 /* Do not reference init code/data from meminit code/data */
1069 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1070 .bad_tosec = { INIT_SECTIONS, NULL },
1071 .mismatch = XXXINIT_TO_SOME_INIT,
1072 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1074 /* Do not reference exit code/data from memexit code/data */
1076 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1077 .bad_tosec = { EXIT_SECTIONS, NULL },
1078 .mismatch = XXXEXIT_TO_SOME_EXIT,
1079 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1081 /* Do not use exit code/data from init code */
1083 .fromsec = { ALL_INIT_SECTIONS, NULL },
1084 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1085 .mismatch = ANY_INIT_TO_ANY_EXIT,
1086 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1088 /* Do not use init code/data from exit code */
1090 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1091 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1092 .mismatch = ANY_EXIT_TO_ANY_INIT,
1093 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1096 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1097 .bad_tosec = { INIT_SECTIONS, NULL },
1098 .mismatch = ANY_INIT_TO_ANY_EXIT,
1099 .symbol_white_list = { NULL },
1101 /* Do not export init/exit functions or data */
1103 .fromsec = { "__ksymtab*", NULL },
1104 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1105 .mismatch = EXPORT_TO_INIT_EXIT,
1106 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1109 .fromsec = { "__ex_table", NULL },
1110 /* If you're adding any new black-listed sections in here, consider
1111 * adding a special 'printer' for them in scripts/check_extable.
1113 .bad_tosec = { ".altinstr_replacement", NULL },
1114 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1115 .mismatch = EXTABLE_TO_NON_TEXT,
1116 .handler = extable_mismatch_handler,
1120 static const struct sectioncheck *section_mismatch(
1121 const char *fromsec, const char *tosec)
1124 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1125 const struct sectioncheck *check = §ioncheck[0];
1128 * The target section could be the SHT_NUL section when we're
1129 * handling relocations to un-resolved symbols, trying to match it
1130 * doesn't make much sense and causes build failures on parisc
1136 for (i = 0; i < elems; i++) {
1137 if (match(fromsec, check->fromsec)) {
1138 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1140 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1149 * Whitelist to allow certain references to pass with no warning.
1152 * If a module parameter is declared __initdata and permissions=0
1153 * then this is legal despite the warning generated.
1154 * We cannot see value of permissions here, so just ignore
1156 * The pattern is identified by:
1157 * tosec = .init.data
1162 * module_param_call() ops can refer to __init set function if permissions=0
1163 * The pattern is identified by:
1164 * tosec = .init.text
1166 * atsym = __param_ops_*
1169 * Many drivers utilise a *driver container with references to
1170 * add, remove, probe functions etc.
1171 * the pattern is identified by:
1172 * tosec = init or exit section
1173 * fromsec = data section
1174 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1175 * *probe_one, *_console, *_timer
1178 * Whitelist all references from .head.text to any init section
1181 * Some symbols belong to init section but still it is ok to reference
1182 * these from non-init sections as these symbols don't have any memory
1183 * allocated for them and symbol address and value are same. So even
1184 * if init section is freed, its ok to reference those symbols.
1185 * For ex. symbols marking the init section boundaries.
1186 * This pattern is identified by
1187 * refsymname = __init_begin, _sinittext, _einittext
1190 * GCC may optimize static inlines when fed constant arg(s) resulting
1191 * in functions like cpumask_empty() -- generating an associated symbol
1192 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1193 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1194 * meaningless section warning. May need to add isra symbols too...
1195 * This pattern is identified by
1196 * tosec = init section
1197 * fromsec = text section
1198 * refsymname = *.constprop.*
1201 * Hide section mismatch warnings for ELF local symbols. The goal
1202 * is to eliminate false positive modpost warnings caused by
1203 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1204 * Autogenerated symbol names bypass modpost's "Pattern 2"
1205 * whitelisting, which relies on pattern-matching against symbol
1206 * names to work. (One situation where gcc can autogenerate ELF
1207 * local symbols is when "-fsection-anchors" is used.)
1209 static int secref_whitelist(const struct sectioncheck *mismatch,
1210 const char *fromsec, const char *fromsym,
1211 const char *tosec, const char *tosym)
1213 /* Check for pattern 1 */
1214 if (match(tosec, init_data_sections) &&
1215 match(fromsec, data_sections) &&
1216 strstarts(fromsym, "__param"))
1219 /* Check for pattern 1a */
1220 if (strcmp(tosec, ".init.text") == 0 &&
1221 match(fromsec, data_sections) &&
1222 strstarts(fromsym, "__param_ops_"))
1225 /* Check for pattern 2 */
1226 if (match(tosec, init_exit_sections) &&
1227 match(fromsec, data_sections) &&
1228 match(fromsym, mismatch->symbol_white_list))
1231 /* Check for pattern 3 */
1232 if (match(fromsec, head_sections) &&
1233 match(tosec, init_sections))
1236 /* Check for pattern 4 */
1237 if (match(tosym, linker_symbols))
1240 /* Check for pattern 5 */
1241 if (match(fromsec, text_sections) &&
1242 match(tosec, init_sections) &&
1243 match(fromsym, optim_symbols))
1246 /* Check for pattern 6 */
1247 if (strstarts(fromsym, ".L"))
1253 static inline int is_arm_mapping_symbol(const char *str)
1255 return str[0] == '$' && strchr("axtd", str[1])
1256 && (str[2] == '\0' || str[2] == '.');
1260 * If there's no name there, ignore it; likewise, ignore it if it's
1261 * one of the magic symbols emitted used by current ARM tools.
1263 * Otherwise if find_symbols_between() returns those symbols, they'll
1264 * fail the whitelist tests and cause lots of false alarms ... fixable
1265 * only by merging __exit and __init sections into __text, bloating
1266 * the kernel (which is especially evil on embedded platforms).
1268 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1270 const char *name = elf->strtab + sym->st_name;
1272 if (!name || !strlen(name))
1274 return !is_arm_mapping_symbol(name);
1278 * Find symbol based on relocation record info.
1279 * In some cases the symbol supplied is a valid symbol so
1280 * return refsym. If st_name != 0 we assume this is a valid symbol.
1281 * In other cases the symbol needs to be looked up in the symbol table
1282 * based on section and address.
1284 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1288 Elf_Sym *near = NULL;
1289 Elf64_Sword distance = 20;
1291 unsigned int relsym_secindex;
1293 if (relsym->st_name != 0)
1296 relsym_secindex = get_secindex(elf, relsym);
1297 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1298 if (get_secindex(elf, sym) != relsym_secindex)
1300 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1302 if (!is_valid_name(elf, sym))
1304 if (sym->st_value == addr)
1306 /* Find a symbol nearby - addr are maybe negative */
1307 d = sym->st_value - addr;
1309 d = addr - sym->st_value;
1315 /* We need a close match */
1323 * Find symbols before or equal addr and after addr - in the section sec.
1324 * If we find two symbols with equal offset prefer one with a valid name.
1325 * The ELF format may have a better way to detect what type of symbol
1326 * it is, but this works for now.
1328 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1332 Elf_Sym *near = NULL;
1333 Elf_Addr distance = ~0;
1335 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1338 if (is_shndx_special(sym->st_shndx))
1340 symsec = sec_name(elf, get_secindex(elf, sym));
1341 if (strcmp(symsec, sec) != 0)
1343 if (!is_valid_name(elf, sym))
1345 if (sym->st_value <= addr) {
1346 if ((addr - sym->st_value) < distance) {
1347 distance = addr - sym->st_value;
1349 } else if ((addr - sym->st_value) == distance) {
1358 * Convert a section name to the function/data attribute
1359 * .init.text => __init
1360 * .memexitconst => __memconst
1363 * The memory of returned value has been allocated on a heap. The user of this
1364 * method should free it after usage.
1366 static char *sec2annotation(const char *s)
1368 if (match(s, init_exit_sections)) {
1369 char *p = NOFAIL(malloc(20));
1376 while (*s && *s != '.')
1381 if (strstr(s, "rodata") != NULL)
1382 strcat(p, "const ");
1383 else if (strstr(s, "data") != NULL)
1389 return NOFAIL(strdup(""));
1393 static int is_function(Elf_Sym *sym)
1396 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1401 static void print_section_list(const char * const list[20])
1403 const char *const *s = list;
1406 fprintf(stderr, "%s", *s);
1409 fprintf(stderr, ", ");
1411 fprintf(stderr, "\n");
1414 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1417 case 0: *name = "variable"; *name_p = ""; break;
1418 case 1: *name = "function"; *name_p = "()"; break;
1419 default: *name = "(unknown reference)"; *name_p = ""; break;
1424 * Print a warning about a section mismatch.
1425 * Try to find symbols near it so user can find it.
1426 * Check whitelist before warning - it may be a false positive.
1428 static void report_sec_mismatch(const char *modname,
1429 const struct sectioncheck *mismatch,
1430 const char *fromsec,
1431 unsigned long long fromaddr,
1432 const char *fromsym,
1434 const char *tosec, const char *tosym,
1437 const char *from, *from_p;
1438 const char *to, *to_p;
1442 sec_mismatch_count++;
1444 get_pretty_name(from_is_func, &from, &from_p);
1445 get_pretty_name(to_is_func, &to, &to_p);
1447 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1448 "to the %s %s:%s%s\n",
1449 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1452 switch (mismatch->mismatch) {
1453 case TEXT_TO_ANY_INIT:
1454 prl_from = sec2annotation(fromsec);
1455 prl_to = sec2annotation(tosec);
1457 "The function %s%s() references\n"
1459 "This is often because %s lacks a %s\n"
1460 "annotation or the annotation of %s is wrong.\n",
1462 to, prl_to, tosym, to_p,
1463 fromsym, prl_to, tosym);
1467 case DATA_TO_ANY_INIT: {
1468 prl_to = sec2annotation(tosec);
1470 "The variable %s references\n"
1472 "If the reference is valid then annotate the\n"
1473 "variable with __init* or __refdata (see linux/init.h) "
1474 "or name the variable:\n",
1475 fromsym, to, prl_to, tosym, to_p);
1476 print_section_list(mismatch->symbol_white_list);
1480 case TEXT_TO_ANY_EXIT:
1481 prl_to = sec2annotation(tosec);
1483 "The function %s() references a %s in an exit section.\n"
1484 "Often the %s %s%s has valid usage outside the exit section\n"
1485 "and the fix is to remove the %sannotation of %s.\n",
1486 fromsym, to, to, tosym, to_p, prl_to, tosym);
1489 case DATA_TO_ANY_EXIT: {
1490 prl_to = sec2annotation(tosec);
1492 "The variable %s references\n"
1494 "If the reference is valid then annotate the\n"
1495 "variable with __exit* (see linux/init.h) or "
1496 "name the variable:\n",
1497 fromsym, to, prl_to, tosym, to_p);
1498 print_section_list(mismatch->symbol_white_list);
1502 case XXXINIT_TO_SOME_INIT:
1503 case XXXEXIT_TO_SOME_EXIT:
1504 prl_from = sec2annotation(fromsec);
1505 prl_to = sec2annotation(tosec);
1507 "The %s %s%s%s references\n"
1509 "If %s is only used by %s then\n"
1510 "annotate %s with a matching annotation.\n",
1511 from, prl_from, fromsym, from_p,
1512 to, prl_to, tosym, to_p,
1513 tosym, fromsym, tosym);
1517 case ANY_INIT_TO_ANY_EXIT:
1518 prl_from = sec2annotation(fromsec);
1519 prl_to = sec2annotation(tosec);
1521 "The %s %s%s%s references\n"
1523 "This is often seen when error handling "
1524 "in the init function\n"
1525 "uses functionality in the exit path.\n"
1526 "The fix is often to remove the %sannotation of\n"
1527 "%s%s so it may be used outside an exit section.\n",
1528 from, prl_from, fromsym, from_p,
1529 to, prl_to, tosym, to_p,
1530 prl_to, tosym, to_p);
1534 case ANY_EXIT_TO_ANY_INIT:
1535 prl_from = sec2annotation(fromsec);
1536 prl_to = sec2annotation(tosec);
1538 "The %s %s%s%s references\n"
1540 "This is often seen when error handling "
1541 "in the exit function\n"
1542 "uses functionality in the init path.\n"
1543 "The fix is often to remove the %sannotation of\n"
1544 "%s%s so it may be used outside an init section.\n",
1545 from, prl_from, fromsym, from_p,
1546 to, prl_to, tosym, to_p,
1547 prl_to, tosym, to_p);
1551 case EXPORT_TO_INIT_EXIT:
1552 prl_to = sec2annotation(tosec);
1554 "The symbol %s is exported and annotated %s\n"
1555 "Fix this by removing the %sannotation of %s "
1556 "or drop the export.\n",
1557 tosym, prl_to, prl_to, tosym);
1560 case EXTABLE_TO_NON_TEXT:
1561 fatal("There's a special handler for this mismatch type, "
1562 "we should never get here.");
1565 fprintf(stderr, "\n");
1568 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1569 const struct sectioncheck* const mismatch,
1570 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1576 const char *fromsym;
1578 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1579 fromsym = sym_name(elf, from);
1581 if (strstarts(fromsym, "reference___initcall"))
1584 tosec = sec_name(elf, get_secindex(elf, sym));
1585 to = find_elf_symbol(elf, r->r_addend, sym);
1586 tosym = sym_name(elf, to);
1588 /* check whitelist - we may ignore it */
1589 if (secref_whitelist(mismatch,
1590 fromsec, fromsym, tosec, tosym)) {
1591 report_sec_mismatch(modname, mismatch,
1592 fromsec, r->r_offset, fromsym,
1593 is_function(from), tosec, tosym,
1598 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1600 if (section_index > elf->num_sections)
1601 fatal("section_index is outside elf->num_sections!\n");
1603 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1607 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1608 * to know the sizeof(struct exception_table_entry) for the target architecture.
1610 static unsigned int extable_entry_size = 0;
1611 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1614 * If we're currently checking the second relocation within __ex_table,
1615 * that relocation offset tells us the offsetof(struct
1616 * exception_table_entry, fixup) which is equal to sizeof(struct
1617 * exception_table_entry) divided by two. We use that to our advantage
1618 * since there's no portable way to get that size as every architecture
1619 * seems to go with different sized types. Not pretty but better than
1620 * hard-coding the size for every architecture..
1622 if (!extable_entry_size)
1623 extable_entry_size = r->r_offset * 2;
1626 static inline bool is_extable_fault_address(Elf_Rela *r)
1629 * extable_entry_size is only discovered after we've handled the
1630 * _second_ relocation in __ex_table, so only abort when we're not
1631 * handling the first reloc and extable_entry_size is zero.
1633 if (r->r_offset && extable_entry_size == 0)
1634 fatal("extable_entry size hasn't been discovered!\n");
1636 return ((r->r_offset == 0) ||
1637 (r->r_offset % extable_entry_size == 0));
1640 #define is_second_extable_reloc(Start, Cur, Sec) \
1641 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1643 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1644 const struct sectioncheck* const mismatch,
1645 Elf_Rela* r, Elf_Sym* sym,
1646 const char* fromsec, const char* tosec)
1648 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1649 const char* fromsym_name = sym_name(elf, fromsym);
1650 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1651 const char* tosym_name = sym_name(elf, tosym);
1652 const char* from_pretty_name;
1653 const char* from_pretty_name_p;
1654 const char* to_pretty_name;
1655 const char* to_pretty_name_p;
1657 get_pretty_name(is_function(fromsym),
1658 &from_pretty_name, &from_pretty_name_p);
1659 get_pretty_name(is_function(tosym),
1660 &to_pretty_name, &to_pretty_name_p);
1662 warn("%s(%s+0x%lx): Section mismatch in reference"
1663 " from the %s %s%s to the %s %s:%s%s\n",
1664 modname, fromsec, (long)r->r_offset, from_pretty_name,
1665 fromsym_name, from_pretty_name_p,
1666 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1668 if (!match(tosec, mismatch->bad_tosec) &&
1669 is_executable_section(elf, get_secindex(elf, sym)))
1671 "The relocation at %s+0x%lx references\n"
1672 "section \"%s\" which is not in the list of\n"
1673 "authorized sections. If you're adding a new section\n"
1674 "and/or if this reference is valid, add \"%s\" to the\n"
1675 "list of authorized sections to jump to on fault.\n"
1676 "This can be achieved by adding \"%s\" to \n"
1677 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1678 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1681 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1682 const struct sectioncheck* const mismatch,
1683 Elf_Rela* r, Elf_Sym* sym,
1684 const char *fromsec)
1686 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1688 sec_mismatch_count++;
1690 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1692 if (match(tosec, mismatch->bad_tosec))
1693 fatal("The relocation at %s+0x%lx references\n"
1694 "section \"%s\" which is black-listed.\n"
1695 "Something is seriously wrong and should be fixed.\n"
1696 "You might get more information about where this is\n"
1697 "coming from by using scripts/check_extable.sh %s\n",
1698 fromsec, (long)r->r_offset, tosec, modname);
1699 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1700 if (is_extable_fault_address(r))
1701 fatal("The relocation at %s+0x%lx references\n"
1702 "section \"%s\" which is not executable, IOW\n"
1703 "it is not possible for the kernel to fault\n"
1704 "at that address. Something is seriously wrong\n"
1705 "and should be fixed.\n",
1706 fromsec, (long)r->r_offset, tosec);
1708 fatal("The relocation at %s+0x%lx references\n"
1709 "section \"%s\" which is not executable, IOW\n"
1710 "the kernel will fault if it ever tries to\n"
1711 "jump to it. Something is seriously wrong\n"
1712 "and should be fixed.\n",
1713 fromsec, (long)r->r_offset, tosec);
1717 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1718 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1720 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1721 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1724 if (mismatch->handler)
1725 mismatch->handler(modname, elf, mismatch,
1728 default_mismatch_handler(modname, elf, mismatch,
1733 static unsigned int *reloc_location(struct elf_info *elf,
1734 Elf_Shdr *sechdr, Elf_Rela *r)
1736 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1739 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1741 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1742 unsigned int *location = reloc_location(elf, sechdr, r);
1746 r->r_addend = TO_NATIVE(*location);
1749 r->r_addend = TO_NATIVE(*location) + 4;
1750 /* For CONFIG_RELOCATABLE=y */
1751 if (elf->hdr->e_type == ET_EXEC)
1752 r->r_addend += r->r_offset;
1759 #define R_ARM_CALL 28
1761 #ifndef R_ARM_JUMP24
1762 #define R_ARM_JUMP24 29
1765 #ifndef R_ARM_THM_CALL
1766 #define R_ARM_THM_CALL 10
1768 #ifndef R_ARM_THM_JUMP24
1769 #define R_ARM_THM_JUMP24 30
1771 #ifndef R_ARM_THM_JUMP19
1772 #define R_ARM_THM_JUMP19 51
1775 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1777 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1781 /* From ARM ABI: (S + A) | T */
1782 r->r_addend = (int)(long)
1783 (elf->symtab_start + ELF_R_SYM(r->r_info));
1788 case R_ARM_THM_CALL:
1789 case R_ARM_THM_JUMP24:
1790 case R_ARM_THM_JUMP19:
1791 /* From ARM ABI: ((S + A) | T) - P */
1792 r->r_addend = (int)(long)(elf->hdr +
1794 (r->r_offset - sechdr->sh_addr));
1802 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1804 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1805 unsigned int *location = reloc_location(elf, sechdr, r);
1808 if (r_typ == R_MIPS_HI16)
1809 return 1; /* skip this */
1810 inst = TO_NATIVE(*location);
1813 r->r_addend = inst & 0xffff;
1816 r->r_addend = (inst & 0x03ffffff) << 2;
1826 #define EM_RISCV 243
1829 #ifndef R_RISCV_SUB32
1830 #define R_RISCV_SUB32 39
1833 static void section_rela(const char *modname, struct elf_info *elf,
1840 const char *fromsec;
1842 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1843 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1845 fromsec = sech_name(elf, sechdr);
1846 fromsec += strlen(".rela");
1847 /* if from section (name) is know good then skip it */
1848 if (match(fromsec, section_white_list))
1851 for (rela = start; rela < stop; rela++) {
1852 r.r_offset = TO_NATIVE(rela->r_offset);
1853 #if KERNEL_ELFCLASS == ELFCLASS64
1854 if (elf->hdr->e_machine == EM_MIPS) {
1856 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1857 r_sym = TO_NATIVE(r_sym);
1858 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1859 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1861 r.r_info = TO_NATIVE(rela->r_info);
1862 r_sym = ELF_R_SYM(r.r_info);
1865 r.r_info = TO_NATIVE(rela->r_info);
1866 r_sym = ELF_R_SYM(r.r_info);
1868 r.r_addend = TO_NATIVE(rela->r_addend);
1869 switch (elf->hdr->e_machine) {
1871 if (!strcmp("__ex_table", fromsec) &&
1872 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1876 sym = elf->symtab_start + r_sym;
1877 /* Skip special sections */
1878 if (is_shndx_special(sym->st_shndx))
1880 if (is_second_extable_reloc(start, rela, fromsec))
1881 find_extable_entry_size(fromsec, &r);
1882 check_section_mismatch(modname, elf, &r, sym, fromsec);
1886 static void section_rel(const char *modname, struct elf_info *elf,
1893 const char *fromsec;
1895 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1896 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1898 fromsec = sech_name(elf, sechdr);
1899 fromsec += strlen(".rel");
1900 /* if from section (name) is know good then skip it */
1901 if (match(fromsec, section_white_list))
1904 for (rel = start; rel < stop; rel++) {
1905 r.r_offset = TO_NATIVE(rel->r_offset);
1906 #if KERNEL_ELFCLASS == ELFCLASS64
1907 if (elf->hdr->e_machine == EM_MIPS) {
1909 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1910 r_sym = TO_NATIVE(r_sym);
1911 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1912 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1914 r.r_info = TO_NATIVE(rel->r_info);
1915 r_sym = ELF_R_SYM(r.r_info);
1918 r.r_info = TO_NATIVE(rel->r_info);
1919 r_sym = ELF_R_SYM(r.r_info);
1922 switch (elf->hdr->e_machine) {
1924 if (addend_386_rel(elf, sechdr, &r))
1928 if (addend_arm_rel(elf, sechdr, &r))
1932 if (addend_mips_rel(elf, sechdr, &r))
1936 sym = elf->symtab_start + r_sym;
1937 /* Skip special sections */
1938 if (is_shndx_special(sym->st_shndx))
1940 if (is_second_extable_reloc(start, rel, fromsec))
1941 find_extable_entry_size(fromsec, &r);
1942 check_section_mismatch(modname, elf, &r, sym, fromsec);
1947 * A module includes a number of sections that are discarded
1948 * either when loaded or when used as built-in.
1949 * For loaded modules all functions marked __init and all data
1950 * marked __initdata will be discarded when the module has been initialized.
1951 * Likewise for modules used built-in the sections marked __exit
1952 * are discarded because __exit marked function are supposed to be called
1953 * only when a module is unloaded which never happens for built-in modules.
1954 * The check_sec_ref() function traverses all relocation records
1955 * to find all references to a section that reference a section that will
1956 * be discarded and warns about it.
1958 static void check_sec_ref(struct module *mod, const char *modname,
1959 struct elf_info *elf)
1962 Elf_Shdr *sechdrs = elf->sechdrs;
1964 /* Walk through all sections */
1965 for (i = 0; i < elf->num_sections; i++) {
1966 check_section(modname, elf, &elf->sechdrs[i]);
1967 /* We want to process only relocation sections and not .init */
1968 if (sechdrs[i].sh_type == SHT_RELA)
1969 section_rela(modname, elf, &elf->sechdrs[i]);
1970 else if (sechdrs[i].sh_type == SHT_REL)
1971 section_rel(modname, elf, &elf->sechdrs[i]);
1975 static char *remove_dot(char *s)
1977 size_t n = strcspn(s, ".");
1980 size_t m = strspn(s + n + 1, "0123456789");
1981 if (m && (s[n + m] == '.' || s[n + m] == 0))
1984 /* strip trailing .prelink */
1985 if (strends(s, ".prelink"))
1986 s[strlen(s) - 8] = '\0';
1991 static void read_symbols(const char *modname)
1993 const char *symname;
1998 struct elf_info info = { };
2001 if (!parse_elf(&info, modname))
2007 /* strip trailing .o */
2008 tmp = NOFAIL(strdup(modname));
2009 tmp[strlen(tmp) - 2] = '\0';
2010 /* strip trailing .prelink */
2011 if (strends(tmp, ".prelink"))
2012 tmp[strlen(tmp) - 8] = '\0';
2013 mod = new_module(tmp);
2017 if (!mod->is_vmlinux) {
2018 license = get_modinfo(&info, "license");
2020 error("missing MODULE_LICENSE() in %s\n", modname);
2022 if (license_is_gpl_compatible(license))
2023 mod->gpl_compatible = 1;
2025 mod->gpl_compatible = 0;
2028 license = get_next_modinfo(&info, "license", license);
2031 namespace = get_modinfo(&info, "import_ns");
2033 add_namespace(&mod->imported_namespaces, namespace);
2034 namespace = get_next_modinfo(&info, "import_ns",
2039 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2040 symname = remove_dot(info.strtab + sym->st_name);
2042 handle_symbol(mod, &info, sym, symname);
2043 handle_moddevtable(mod, &info, sym, symname);
2046 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2047 symname = remove_dot(info.strtab + sym->st_name);
2049 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2050 if (strstarts(symname, "__kstrtabns_"))
2051 sym_update_namespace(symname + strlen("__kstrtabns_"),
2052 namespace_from_kstrtabns(&info,
2055 if (strstarts(symname, "__crc_"))
2056 handle_modversion(mod, &info, sym,
2057 symname + strlen("__crc_"));
2060 // check for static EXPORT_SYMBOL_* functions && global vars
2061 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2062 unsigned char bind = ELF_ST_BIND(sym->st_info);
2064 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2066 find_symbol(remove_dot(info.strtab +
2074 check_sec_ref(mod, modname, &info);
2076 if (!mod->is_vmlinux) {
2077 version = get_modinfo(&info, "version");
2078 if (version || all_versions)
2079 get_src_version(mod->name, mod->srcversion,
2080 sizeof(mod->srcversion) - 1);
2083 parse_elf_finish(&info);
2085 /* Our trick to get versioning for module struct etc. - it's
2086 * never passed as an argument to an exported function, so
2087 * the automatic versioning doesn't pick it up, but it's really
2088 * important anyhow */
2090 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
2093 static void read_symbols_from_files(const char *filename)
2096 char fname[PATH_MAX];
2098 if (strcmp(filename, "-") != 0) {
2099 in = fopen(filename, "r");
2101 fatal("Can't open filenames file %s: %m", filename);
2104 while (fgets(fname, PATH_MAX, in) != NULL) {
2105 if (strends(fname, "\n"))
2106 fname[strlen(fname)-1] = '\0';
2107 read_symbols(fname);
2116 /* We first write the generated file into memory using the
2117 * following helper, then compare to the file on disk and
2118 * only update the later if anything changed */
2120 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2121 const char *fmt, ...)
2128 len = vsnprintf(tmp, SZ, fmt, ap);
2129 buf_write(buf, tmp, len);
2133 void buf_write(struct buffer *buf, const char *s, int len)
2135 if (buf->size - buf->pos < len) {
2136 buf->size += len + SZ;
2137 buf->p = NOFAIL(realloc(buf->p, buf->size));
2139 strncpy(buf->p + buf->pos, s, len);
2143 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2147 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2151 case export_unknown:
2157 static void check_exports(struct module *mod)
2159 struct symbol *s, *exp;
2161 for (s = mod->unres; s; s = s->next) {
2162 const char *basename;
2163 exp = find_symbol(s->name);
2164 if (!exp || exp->module == mod) {
2165 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2166 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2167 "\"%s\" [%s.ko] undefined!\n",
2168 s->name, mod->name);
2171 basename = strrchr(mod->name, '/');
2175 basename = mod->name;
2177 if (exp->namespace &&
2178 !module_imports_namespace(mod, exp->namespace)) {
2179 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2180 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2181 basename, exp->name, exp->namespace);
2182 add_namespace(&mod->missing_namespaces, exp->namespace);
2185 if (!mod->gpl_compatible)
2186 check_for_gpl_usage(exp->export, basename, exp->name);
2190 static void check_modname_len(struct module *mod)
2192 const char *mod_name;
2194 mod_name = strrchr(mod->name, '/');
2195 if (mod_name == NULL)
2196 mod_name = mod->name;
2199 if (strlen(mod_name) >= MODULE_NAME_LEN)
2200 error("module name is too long [%s.ko]\n", mod->name);
2204 * Header for the generated file
2206 static void add_header(struct buffer *b, struct module *mod)
2208 buf_printf(b, "#include <linux/module.h>\n");
2210 * Include build-salt.h after module.h in order to
2211 * inherit the definitions.
2213 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2214 buf_printf(b, "#include <linux/build-salt.h>\n");
2215 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2216 buf_printf(b, "#include <linux/vermagic.h>\n");
2217 buf_printf(b, "#include <linux/compiler.h>\n");
2218 buf_printf(b, "\n");
2219 buf_printf(b, "BUILD_SALT;\n");
2220 buf_printf(b, "BUILD_LTO_INFO;\n");
2221 buf_printf(b, "\n");
2222 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2223 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2224 buf_printf(b, "\n");
2225 buf_printf(b, "__visible struct module __this_module\n");
2226 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2227 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2229 buf_printf(b, "\t.init = init_module,\n");
2230 if (mod->has_cleanup)
2231 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2232 "\t.exit = cleanup_module,\n"
2234 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2235 buf_printf(b, "};\n");
2238 static void add_intree_flag(struct buffer *b, int is_intree)
2241 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2244 /* Cannot check for assembler */
2245 static void add_retpoline(struct buffer *b)
2247 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2248 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2249 buf_printf(b, "#endif\n");
2252 static void add_staging_flag(struct buffer *b, const char *name)
2254 if (strstarts(name, "drivers/staging"))
2255 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2259 * Record CRCs for unresolved symbols
2261 static void add_versions(struct buffer *b, struct module *mod)
2263 struct symbol *s, *exp;
2265 for (s = mod->unres; s; s = s->next) {
2266 exp = find_symbol(s->name);
2267 if (!exp || exp->module == mod)
2269 s->module = exp->module;
2270 s->crc_valid = exp->crc_valid;
2277 buf_printf(b, "\n");
2278 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2279 buf_printf(b, "__used __section(\"__versions\") = {\n");
2281 for (s = mod->unres; s; s = s->next) {
2284 if (!s->crc_valid) {
2285 warn("\"%s\" [%s.ko] has no CRC!\n",
2286 s->name, mod->name);
2289 if (strlen(s->name) >= MODULE_NAME_LEN) {
2290 error("too long symbol \"%s\" [%s.ko]\n",
2291 s->name, mod->name);
2294 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2298 buf_printf(b, "};\n");
2301 static void add_depends(struct buffer *b, struct module *mod)
2306 /* Clear ->seen flag of modules that own symbols needed by this. */
2307 for (s = mod->unres; s; s = s->next)
2309 s->module->seen = s->module->is_vmlinux;
2311 buf_printf(b, "\n");
2312 buf_printf(b, "MODULE_INFO(depends, \"");
2313 for (s = mod->unres; s; s = s->next) {
2318 if (s->module->seen)
2321 s->module->seen = 1;
2322 p = strrchr(s->module->name, '/');
2326 p = s->module->name;
2327 buf_printf(b, "%s%s", first ? "" : ",", p);
2330 buf_printf(b, "\");\n");
2333 static void add_srcversion(struct buffer *b, struct module *mod)
2335 if (mod->srcversion[0]) {
2336 buf_printf(b, "\n");
2337 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2342 static void write_buf(struct buffer *b, const char *fname)
2346 file = fopen(fname, "w");
2351 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2355 if (fclose(file) != 0) {
2361 static void write_if_changed(struct buffer *b, const char *fname)
2367 file = fopen(fname, "r");
2371 if (fstat(fileno(file), &st) < 0)
2374 if (st.st_size != b->pos)
2377 tmp = NOFAIL(malloc(b->pos));
2378 if (fread(tmp, 1, b->pos, file) != b->pos)
2381 if (memcmp(tmp, b->p, b->pos) != 0)
2393 write_buf(b, fname);
2396 /* parse Module.symvers file. line format:
2397 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2399 static void read_dump(const char *fname)
2401 char *buf, *pos, *line;
2403 buf = read_text_file(fname);
2405 /* No symbol versions, silently ignore */
2410 while ((line = get_line(&pos))) {
2411 char *symname, *namespace, *modname, *d, *export;
2416 if (!(symname = strchr(line, '\t')))
2419 if (!(modname = strchr(symname, '\t')))
2422 if (!(export = strchr(modname, '\t')))
2425 if (!(namespace = strchr(export, '\t')))
2427 *namespace++ = '\0';
2429 crc = strtoul(line, &d, 16);
2430 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2432 mod = find_module(modname);
2434 mod = new_module(modname);
2437 s = sym_add_exported(symname, mod, export_no(export));
2439 sym_set_crc(symname, crc);
2440 sym_update_namespace(symname, namespace);
2446 fatal("parse error in symbol dump file\n");
2449 static void write_dump(const char *fname)
2451 struct buffer buf = { };
2452 struct symbol *symbol;
2453 const char *namespace;
2456 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2457 symbol = symbolhash[n];
2459 if (!symbol->module->from_dump) {
2460 namespace = symbol->namespace;
2461 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2462 symbol->crc, symbol->name,
2463 symbol->module->name,
2464 export_str(symbol->export),
2465 namespace ? namespace : "");
2467 symbol = symbol->next;
2470 write_buf(&buf, fname);
2474 static void write_namespace_deps_files(const char *fname)
2477 struct namespace_list *ns;
2478 struct buffer ns_deps_buf = {};
2480 for (mod = modules; mod; mod = mod->next) {
2482 if (mod->from_dump || !mod->missing_namespaces)
2485 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2487 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2488 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2490 buf_printf(&ns_deps_buf, "\n");
2493 write_if_changed(&ns_deps_buf, fname);
2494 free(ns_deps_buf.p);
2498 struct dump_list *next;
2502 int main(int argc, char **argv)
2505 struct buffer buf = { };
2506 char *missing_namespace_deps = NULL;
2507 char *dump_write = NULL, *files_source = NULL;
2510 struct dump_list *dump_read_start = NULL;
2511 struct dump_list **dump_read_iter = &dump_read_start;
2513 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2516 external_module = 1;
2520 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2521 (*dump_read_iter)->file = optarg;
2522 dump_read_iter = &(*dump_read_iter)->next;
2528 ignore_missing_files = 1;
2531 dump_write = optarg;
2537 files_source = optarg;
2540 warn_unresolved = 1;
2543 sec_mismatch_warn_only = false;
2546 allow_missing_ns_imports = 1;
2549 missing_namespace_deps = optarg;
2556 while (dump_read_start) {
2557 struct dump_list *tmp;
2559 read_dump(dump_read_start->file);
2560 tmp = dump_read_start->next;
2561 free(dump_read_start);
2562 dump_read_start = tmp;
2565 while (optind < argc)
2566 read_symbols(argv[optind++]);
2569 read_symbols_from_files(files_source);
2571 for (mod = modules; mod; mod = mod->next) {
2572 char fname[PATH_MAX];
2574 if (mod->is_vmlinux || mod->from_dump)
2579 check_modname_len(mod);
2582 add_header(&buf, mod);
2583 add_intree_flag(&buf, !external_module);
2584 add_retpoline(&buf);
2585 add_staging_flag(&buf, mod->name);
2586 add_versions(&buf, mod);
2587 add_depends(&buf, mod);
2588 add_moddevtable(&buf, mod);
2589 add_srcversion(&buf, mod);
2591 sprintf(fname, "%s.mod.c", mod->name);
2592 write_if_changed(&buf, fname);
2595 if (missing_namespace_deps)
2596 write_namespace_deps_files(missing_namespace_deps);
2599 write_dump(dump_write);
2600 if (sec_mismatch_count && !sec_mismatch_warn_only)
2601 error("Section mismatches detected.\n"
2602 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2603 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2606 for (s = symbolhash[n]; s; s = s->next) {
2608 error("\"%s\" [%s] is a static %s\n",
2609 s->name, s->module->name,
2610 export_str(s->export));
2614 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2615 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2616 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2620 return error_occurred ? 1 : 0;