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 struct namespace_list {
274 struct namespace_list *next;
278 static bool contains_namespace(struct namespace_list *list,
279 const char *namespace)
281 for (; list; list = list->next)
282 if (!strcmp(list->namespace, namespace))
288 static void add_namespace(struct namespace_list **list, const char *namespace)
290 struct namespace_list *ns_entry;
292 if (!contains_namespace(*list, namespace)) {
293 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
294 strlen(namespace) + 1));
295 strcpy(ns_entry->namespace, namespace);
296 ns_entry->next = *list;
301 static bool module_imports_namespace(struct module *module,
302 const char *namespace)
304 return contains_namespace(module->imported_namespaces, namespace);
307 static const struct {
311 { .str = "EXPORT_SYMBOL", .export = export_plain },
312 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
313 { .str = "(unknown)", .export = export_unknown },
317 static const char *export_str(enum export ex)
319 return export_list[ex].str;
322 static enum export export_no(const char *s)
327 return export_unknown;
328 for (i = 0; export_list[i].export != export_unknown; i++) {
329 if (strcmp(export_list[i].str, s) == 0)
330 return export_list[i].export;
332 return export_unknown;
335 static void *sym_get_data_by_offset(const struct elf_info *info,
336 unsigned int secindex, unsigned long offset)
338 Elf_Shdr *sechdr = &info->sechdrs[secindex];
340 if (info->hdr->e_type != ET_REL)
341 offset -= sechdr->sh_addr;
343 return (void *)info->hdr + sechdr->sh_offset + offset;
346 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
348 return sym_get_data_by_offset(info, get_secindex(info, sym),
352 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
354 return sym_get_data_by_offset(info, info->secindex_strings,
358 static const char *sec_name(const struct elf_info *info, int secindex)
360 return sech_name(info, &info->sechdrs[secindex]);
363 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
365 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
367 const char *secname = sec_name(elf, sec);
369 if (strstarts(secname, "___ksymtab+"))
371 else if (strstarts(secname, "___ksymtab_gpl+"))
374 return export_unknown;
377 static void sym_update_namespace(const char *symname, const char *namespace)
379 struct symbol *s = find_symbol(symname);
382 * That symbol should have been created earlier and thus this is
383 * actually an assertion.
386 error("Could not update namespace(%s) for symbol %s\n",
392 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
395 static struct symbol *sym_add_exported(const char *name, struct module *mod,
398 struct symbol *s = find_symbol(name);
401 s = new_symbol(name, mod, export);
402 } else if (!external_module || s->module->is_vmlinux ||
404 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
405 mod->name, name, s->module->name,
406 s->module->is_vmlinux ? "" : ".ko");
415 static void sym_set_crc(const char *name, unsigned int crc)
417 struct symbol *s = find_symbol(name);
420 * Ignore stand-alone __crc_*, which might be auto-generated symbols
421 * such as __*_veneer in ARM ELF.
430 static void *grab_file(const char *filename, size_t *size)
433 void *map = MAP_FAILED;
436 fd = open(filename, O_RDONLY);
443 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
447 if (map == MAP_FAILED)
452 static void release_file(void *file, size_t size)
457 static int parse_elf(struct elf_info *info, const char *filename)
463 const char *secstrings;
464 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
466 hdr = grab_file(filename, &info->size);
468 if (ignore_missing_files) {
469 fprintf(stderr, "%s: %s (ignored)\n", filename,
477 if (info->size < sizeof(*hdr)) {
478 /* file too small, assume this is an empty .o file */
481 /* Is this a valid ELF file? */
482 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
483 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
484 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
485 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
486 /* Not an ELF file - silently ignore it */
489 /* Fix endianness in ELF header */
490 hdr->e_type = TO_NATIVE(hdr->e_type);
491 hdr->e_machine = TO_NATIVE(hdr->e_machine);
492 hdr->e_version = TO_NATIVE(hdr->e_version);
493 hdr->e_entry = TO_NATIVE(hdr->e_entry);
494 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
495 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
496 hdr->e_flags = TO_NATIVE(hdr->e_flags);
497 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
498 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
499 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
500 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
501 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
502 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
503 sechdrs = (void *)hdr + hdr->e_shoff;
504 info->sechdrs = sechdrs;
506 /* Check if file offset is correct */
507 if (hdr->e_shoff > info->size) {
508 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
509 (unsigned long)hdr->e_shoff, filename, info->size);
513 if (hdr->e_shnum == SHN_UNDEF) {
515 * There are more than 64k sections,
516 * read count from .sh_size.
518 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
521 info->num_sections = hdr->e_shnum;
523 if (hdr->e_shstrndx == SHN_XINDEX) {
524 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
527 info->secindex_strings = hdr->e_shstrndx;
530 /* Fix endianness in section headers */
531 for (i = 0; i < info->num_sections; i++) {
532 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
533 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
534 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
535 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
536 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
537 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
538 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
539 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
540 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
541 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
543 /* Find symbol table. */
544 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
545 for (i = 1; i < info->num_sections; i++) {
547 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
549 if (!nobits && sechdrs[i].sh_offset > info->size) {
550 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
551 "sizeof(*hrd)=%zu\n", filename,
552 (unsigned long)sechdrs[i].sh_offset,
556 secname = secstrings + sechdrs[i].sh_name;
557 if (strcmp(secname, ".modinfo") == 0) {
559 fatal("%s has NOBITS .modinfo\n", filename);
560 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
561 info->modinfo_len = sechdrs[i].sh_size;
564 if (sechdrs[i].sh_type == SHT_SYMTAB) {
565 unsigned int sh_link_idx;
567 info->symtab_start = (void *)hdr +
568 sechdrs[i].sh_offset;
569 info->symtab_stop = (void *)hdr +
570 sechdrs[i].sh_offset + sechdrs[i].sh_size;
571 sh_link_idx = sechdrs[i].sh_link;
572 info->strtab = (void *)hdr +
573 sechdrs[sh_link_idx].sh_offset;
576 /* 32bit section no. table? ("more than 64k sections") */
577 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
578 symtab_shndx_idx = i;
579 info->symtab_shndx_start = (void *)hdr +
580 sechdrs[i].sh_offset;
581 info->symtab_shndx_stop = (void *)hdr +
582 sechdrs[i].sh_offset + sechdrs[i].sh_size;
585 if (!info->symtab_start)
586 fatal("%s has no symtab?\n", filename);
588 /* Fix endianness in symbols */
589 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
590 sym->st_shndx = TO_NATIVE(sym->st_shndx);
591 sym->st_name = TO_NATIVE(sym->st_name);
592 sym->st_value = TO_NATIVE(sym->st_value);
593 sym->st_size = TO_NATIVE(sym->st_size);
596 if (symtab_shndx_idx != ~0U) {
598 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
599 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
600 filename, sechdrs[symtab_shndx_idx].sh_link,
603 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
611 static void parse_elf_finish(struct elf_info *info)
613 release_file(info->hdr, info->size);
616 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
618 /* ignore __this_module, it will be resolved shortly */
619 if (strcmp(symname, "__this_module") == 0)
621 /* ignore global offset table */
622 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
624 if (info->hdr->e_machine == EM_PPC)
625 /* Special register function linked on all modules during final link of .ko */
626 if (strstarts(symname, "_restgpr_") ||
627 strstarts(symname, "_savegpr_") ||
628 strstarts(symname, "_rest32gpr_") ||
629 strstarts(symname, "_save32gpr_") ||
630 strstarts(symname, "_restvr_") ||
631 strstarts(symname, "_savevr_"))
633 if (info->hdr->e_machine == EM_PPC64)
634 /* Special register function linked on all modules during final link of .ko */
635 if (strstarts(symname, "_restgpr0_") ||
636 strstarts(symname, "_savegpr0_") ||
637 strstarts(symname, "_restvr_") ||
638 strstarts(symname, "_savevr_") ||
639 strcmp(symname, ".TOC.") == 0)
642 if (info->hdr->e_machine == EM_S390)
643 /* Expoline thunks are linked on all kernel modules during final link of .ko */
644 if (strstarts(symname, "__s390_indirect_jump_r"))
646 /* Do not ignore this symbol */
650 static void handle_modversion(const struct module *mod,
651 const struct elf_info *info,
652 const Elf_Sym *sym, const char *symname)
656 if (sym->st_shndx == SHN_UNDEF) {
657 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
658 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
659 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
665 if (sym->st_shndx == SHN_ABS) {
670 /* symbol points to the CRC in the ELF object */
671 crcp = sym_get_data(info, sym);
672 crc = TO_NATIVE(*crcp);
674 sym_set_crc(symname, crc);
677 static void handle_symbol(struct module *mod, struct elf_info *info,
678 const Elf_Sym *sym, const char *symname)
682 switch (sym->st_shndx) {
684 if (strstarts(symname, "__gnu_lto_")) {
685 /* Should warn here, but modpost runs before the linker */
687 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
690 /* undefined symbol */
691 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
692 ELF_ST_BIND(sym->st_info) != STB_WEAK)
694 if (ignore_undef_symbol(info, symname))
696 if (info->hdr->e_machine == EM_SPARC ||
697 info->hdr->e_machine == EM_SPARCV9) {
698 /* Ignore register directives. */
699 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
701 if (symname[0] == '.') {
702 char *munged = NOFAIL(strdup(symname));
704 munged[1] = toupper(munged[1]);
709 mod->unres = alloc_symbol(symname,
710 ELF_ST_BIND(sym->st_info) == STB_WEAK,
714 /* All exported symbols */
715 if (strstarts(symname, "__ksymtab_")) {
718 name = symname + strlen("__ksymtab_");
719 export = export_from_secname(info,
720 get_secindex(info, sym));
721 sym_add_exported(name, mod, export);
723 if (strcmp(symname, "init_module") == 0)
725 if (strcmp(symname, "cleanup_module") == 0)
726 mod->has_cleanup = 1;
732 * Parse tag=value strings from .modinfo section
734 static char *next_string(char *string, unsigned long *secsize)
736 /* Skip non-zero chars */
739 if ((*secsize)-- <= 1)
743 /* Skip any zero padding. */
746 if ((*secsize)-- <= 1)
752 static char *get_next_modinfo(struct elf_info *info, const char *tag,
756 unsigned int taglen = strlen(tag);
757 char *modinfo = info->modinfo;
758 unsigned long size = info->modinfo_len;
761 size -= prev - modinfo;
762 modinfo = next_string(prev, &size);
765 for (p = modinfo; p; p = next_string(p, &size)) {
766 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
767 return p + taglen + 1;
772 static char *get_modinfo(struct elf_info *info, const char *tag)
775 return get_next_modinfo(info, tag, NULL);
779 * Test if string s ends in string sub
782 static int strrcmp(const char *s, const char *sub)
790 sublen = strlen(sub);
792 if ((slen == 0) || (sublen == 0))
798 return memcmp(s + slen - sublen, sub, sublen);
801 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
804 return elf->strtab + sym->st_name;
809 /* The pattern is an array of simple patterns.
810 * "foo" will match an exact string equal to "foo"
811 * "*foo" will match a string that ends with "foo"
812 * "foo*" will match a string that begins with "foo"
813 * "*foo*" will match a string that contains "foo"
815 static int match(const char *sym, const char * const pat[])
822 endp = p + strlen(p) - 1;
825 if (*p == '*' && *endp == '*') {
826 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
827 char *here = strstr(sym, bare);
834 else if (*p == '*') {
835 if (strrcmp(sym, p + 1) == 0)
839 else if (*endp == '*') {
840 if (strncmp(sym, p, strlen(p) - 1) == 0)
845 if (strcmp(p, sym) == 0)
853 /* sections that we do not want to do full section mismatch check on */
854 static const char *const section_white_list[] =
858 ".cranges", /* sh64 */
859 ".zdebug*", /* Compressed debug sections. */
860 ".GCC.command.line", /* record-gcc-switches */
861 ".mdebug*", /* alpha, score, mips etc. */
862 ".pdr", /* alpha, score, mips etc. */
867 ".xt.prop", /* xtensa */
868 ".xt.lit", /* xtensa */
869 ".arcextmap*", /* arc */
870 ".gnu.linkonce.arcext*", /* arc : modules */
871 ".cmem*", /* EZchip */
872 ".fmt_slot*", /* EZchip */
879 * This is used to find sections missing the SHF_ALLOC flag.
880 * The cause of this is often a section specified in assembler
881 * without "ax" / "aw".
883 static void check_section(const char *modname, struct elf_info *elf,
886 const char *sec = sech_name(elf, sechdr);
888 if (sechdr->sh_type == SHT_PROGBITS &&
889 !(sechdr->sh_flags & SHF_ALLOC) &&
890 !match(sec, section_white_list)) {
891 warn("%s (%s): unexpected non-allocatable section.\n"
892 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
893 "Note that for example <linux/init.h> contains\n"
894 "section definitions for use in .S files.\n\n",
901 #define ALL_INIT_DATA_SECTIONS \
902 ".init.setup", ".init.rodata", ".meminit.rodata", \
903 ".init.data", ".meminit.data"
904 #define ALL_EXIT_DATA_SECTIONS \
905 ".exit.data", ".memexit.data"
907 #define ALL_INIT_TEXT_SECTIONS \
908 ".init.text", ".meminit.text"
909 #define ALL_EXIT_TEXT_SECTIONS \
910 ".exit.text", ".memexit.text"
912 #define ALL_PCI_INIT_SECTIONS \
913 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
914 ".pci_fixup_enable", ".pci_fixup_resume", \
915 ".pci_fixup_resume_early", ".pci_fixup_suspend"
917 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
918 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
920 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
921 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
923 #define DATA_SECTIONS ".data", ".data.rel"
924 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
925 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
926 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
927 ".fixup", ".entry.text", ".exception.text", ".text.*", \
928 ".coldtext", ".softirqentry.text"
930 #define INIT_SECTIONS ".init.*"
931 #define MEM_INIT_SECTIONS ".meminit.*"
933 #define EXIT_SECTIONS ".exit.*"
934 #define MEM_EXIT_SECTIONS ".memexit.*"
936 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
937 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
939 /* init data sections */
940 static const char *const init_data_sections[] =
941 { ALL_INIT_DATA_SECTIONS, NULL };
943 /* all init sections */
944 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
946 /* All init and exit sections (code + data) */
947 static const char *const init_exit_sections[] =
948 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
950 /* all text sections */
951 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
954 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
957 /* symbols in .data that may refer to init/exit sections */
958 #define DEFAULT_SYMBOL_WHITE_LIST \
960 "*_template", /* scsi uses *_template a lot */ \
961 "*_timer", /* arm uses ops structures named _timer a lot */ \
962 "*_sht", /* scsi also used *_sht to some extent */ \
968 static const char *const head_sections[] = { ".head.text*", NULL };
969 static const char *const linker_symbols[] =
970 { "__init_begin", "_sinittext", "_einittext", NULL };
971 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
978 XXXINIT_TO_SOME_INIT,
979 XXXEXIT_TO_SOME_EXIT,
980 ANY_INIT_TO_ANY_EXIT,
981 ANY_EXIT_TO_ANY_INIT,
987 * Describe how to match sections on different criteria:
989 * @fromsec: Array of sections to be matched.
991 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
992 * this array is forbidden (black-list). Can be empty.
994 * @good_tosec: Relocations applied to a section in @fromsec must be
995 * targeting sections in this array (white-list). Can be empty.
997 * @mismatch: Type of mismatch.
999 * @symbol_white_list: Do not match a relocation to a symbol in this list
1000 * even if it is targeting a section in @bad_to_sec.
1002 * @handler: Specific handler to call when a match is found. If NULL,
1003 * default_mismatch_handler() will be called.
1006 struct sectioncheck {
1007 const char *fromsec[20];
1008 const char *bad_tosec[20];
1009 const char *good_tosec[20];
1010 enum mismatch mismatch;
1011 const char *symbol_white_list[20];
1012 void (*handler)(const char *modname, struct elf_info *elf,
1013 const struct sectioncheck* const mismatch,
1014 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1018 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1019 const struct sectioncheck* const mismatch,
1020 Elf_Rela *r, Elf_Sym *sym,
1021 const char *fromsec);
1023 static const struct sectioncheck sectioncheck[] = {
1024 /* Do not reference init/exit code/data from
1025 * normal code and data
1028 .fromsec = { TEXT_SECTIONS, NULL },
1029 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1030 .mismatch = TEXT_TO_ANY_INIT,
1031 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1034 .fromsec = { DATA_SECTIONS, NULL },
1035 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1036 .mismatch = DATA_TO_ANY_INIT,
1037 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1040 .fromsec = { DATA_SECTIONS, NULL },
1041 .bad_tosec = { INIT_SECTIONS, NULL },
1042 .mismatch = DATA_TO_ANY_INIT,
1043 .symbol_white_list = {
1044 "*_template", "*_timer", "*_sht", "*_ops",
1045 "*_probe", "*_probe_one", "*_console", NULL
1049 .fromsec = { TEXT_SECTIONS, NULL },
1050 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1051 .mismatch = TEXT_TO_ANY_EXIT,
1052 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1055 .fromsec = { DATA_SECTIONS, NULL },
1056 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1057 .mismatch = DATA_TO_ANY_EXIT,
1058 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1060 /* Do not reference init code/data from meminit code/data */
1062 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1063 .bad_tosec = { INIT_SECTIONS, NULL },
1064 .mismatch = XXXINIT_TO_SOME_INIT,
1065 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1067 /* Do not reference exit code/data from memexit code/data */
1069 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1070 .bad_tosec = { EXIT_SECTIONS, NULL },
1071 .mismatch = XXXEXIT_TO_SOME_EXIT,
1072 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1074 /* Do not use exit code/data from init code */
1076 .fromsec = { ALL_INIT_SECTIONS, NULL },
1077 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1078 .mismatch = ANY_INIT_TO_ANY_EXIT,
1079 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1081 /* Do not use init code/data from exit code */
1083 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1084 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1085 .mismatch = ANY_EXIT_TO_ANY_INIT,
1086 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1089 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1090 .bad_tosec = { INIT_SECTIONS, NULL },
1091 .mismatch = ANY_INIT_TO_ANY_EXIT,
1092 .symbol_white_list = { NULL },
1094 /* Do not export init/exit functions or data */
1096 .fromsec = { "__ksymtab*", NULL },
1097 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1098 .mismatch = EXPORT_TO_INIT_EXIT,
1099 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1102 .fromsec = { "__ex_table", NULL },
1103 /* If you're adding any new black-listed sections in here, consider
1104 * adding a special 'printer' for them in scripts/check_extable.
1106 .bad_tosec = { ".altinstr_replacement", NULL },
1107 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1108 .mismatch = EXTABLE_TO_NON_TEXT,
1109 .handler = extable_mismatch_handler,
1113 static const struct sectioncheck *section_mismatch(
1114 const char *fromsec, const char *tosec)
1117 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1118 const struct sectioncheck *check = §ioncheck[0];
1121 * The target section could be the SHT_NUL section when we're
1122 * handling relocations to un-resolved symbols, trying to match it
1123 * doesn't make much sense and causes build failures on parisc
1129 for (i = 0; i < elems; i++) {
1130 if (match(fromsec, check->fromsec)) {
1131 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1133 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1142 * Whitelist to allow certain references to pass with no warning.
1145 * If a module parameter is declared __initdata and permissions=0
1146 * then this is legal despite the warning generated.
1147 * We cannot see value of permissions here, so just ignore
1149 * The pattern is identified by:
1150 * tosec = .init.data
1155 * module_param_call() ops can refer to __init set function if permissions=0
1156 * The pattern is identified by:
1157 * tosec = .init.text
1159 * atsym = __param_ops_*
1162 * Many drivers utilise a *driver container with references to
1163 * add, remove, probe functions etc.
1164 * the pattern is identified by:
1165 * tosec = init or exit section
1166 * fromsec = data section
1167 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1168 * *probe_one, *_console, *_timer
1171 * Whitelist all references from .head.text to any init section
1174 * Some symbols belong to init section but still it is ok to reference
1175 * these from non-init sections as these symbols don't have any memory
1176 * allocated for them and symbol address and value are same. So even
1177 * if init section is freed, its ok to reference those symbols.
1178 * For ex. symbols marking the init section boundaries.
1179 * This pattern is identified by
1180 * refsymname = __init_begin, _sinittext, _einittext
1183 * GCC may optimize static inlines when fed constant arg(s) resulting
1184 * in functions like cpumask_empty() -- generating an associated symbol
1185 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1186 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1187 * meaningless section warning. May need to add isra symbols too...
1188 * This pattern is identified by
1189 * tosec = init section
1190 * fromsec = text section
1191 * refsymname = *.constprop.*
1194 * Hide section mismatch warnings for ELF local symbols. The goal
1195 * is to eliminate false positive modpost warnings caused by
1196 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1197 * Autogenerated symbol names bypass modpost's "Pattern 2"
1198 * whitelisting, which relies on pattern-matching against symbol
1199 * names to work. (One situation where gcc can autogenerate ELF
1200 * local symbols is when "-fsection-anchors" is used.)
1202 static int secref_whitelist(const struct sectioncheck *mismatch,
1203 const char *fromsec, const char *fromsym,
1204 const char *tosec, const char *tosym)
1206 /* Check for pattern 1 */
1207 if (match(tosec, init_data_sections) &&
1208 match(fromsec, data_sections) &&
1209 strstarts(fromsym, "__param"))
1212 /* Check for pattern 1a */
1213 if (strcmp(tosec, ".init.text") == 0 &&
1214 match(fromsec, data_sections) &&
1215 strstarts(fromsym, "__param_ops_"))
1218 /* Check for pattern 2 */
1219 if (match(tosec, init_exit_sections) &&
1220 match(fromsec, data_sections) &&
1221 match(fromsym, mismatch->symbol_white_list))
1224 /* Check for pattern 3 */
1225 if (match(fromsec, head_sections) &&
1226 match(tosec, init_sections))
1229 /* Check for pattern 4 */
1230 if (match(tosym, linker_symbols))
1233 /* Check for pattern 5 */
1234 if (match(fromsec, text_sections) &&
1235 match(tosec, init_sections) &&
1236 match(fromsym, optim_symbols))
1239 /* Check for pattern 6 */
1240 if (strstarts(fromsym, ".L"))
1246 static inline int is_arm_mapping_symbol(const char *str)
1248 return str[0] == '$' && strchr("axtd", str[1])
1249 && (str[2] == '\0' || str[2] == '.');
1253 * If there's no name there, ignore it; likewise, ignore it if it's
1254 * one of the magic symbols emitted used by current ARM tools.
1256 * Otherwise if find_symbols_between() returns those symbols, they'll
1257 * fail the whitelist tests and cause lots of false alarms ... fixable
1258 * only by merging __exit and __init sections into __text, bloating
1259 * the kernel (which is especially evil on embedded platforms).
1261 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1263 const char *name = elf->strtab + sym->st_name;
1265 if (!name || !strlen(name))
1267 return !is_arm_mapping_symbol(name);
1271 * Find symbol based on relocation record info.
1272 * In some cases the symbol supplied is a valid symbol so
1273 * return refsym. If st_name != 0 we assume this is a valid symbol.
1274 * In other cases the symbol needs to be looked up in the symbol table
1275 * based on section and address.
1277 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1281 Elf_Sym *near = NULL;
1282 Elf64_Sword distance = 20;
1284 unsigned int relsym_secindex;
1286 if (relsym->st_name != 0)
1289 relsym_secindex = get_secindex(elf, relsym);
1290 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1291 if (get_secindex(elf, sym) != relsym_secindex)
1293 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1295 if (!is_valid_name(elf, sym))
1297 if (sym->st_value == addr)
1299 /* Find a symbol nearby - addr are maybe negative */
1300 d = sym->st_value - addr;
1302 d = addr - sym->st_value;
1308 /* We need a close match */
1316 * Find symbols before or equal addr and after addr - in the section sec.
1317 * If we find two symbols with equal offset prefer one with a valid name.
1318 * The ELF format may have a better way to detect what type of symbol
1319 * it is, but this works for now.
1321 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1325 Elf_Sym *near = NULL;
1326 Elf_Addr distance = ~0;
1328 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1331 if (is_shndx_special(sym->st_shndx))
1333 symsec = sec_name(elf, get_secindex(elf, sym));
1334 if (strcmp(symsec, sec) != 0)
1336 if (!is_valid_name(elf, sym))
1338 if (sym->st_value <= addr) {
1339 if ((addr - sym->st_value) < distance) {
1340 distance = addr - sym->st_value;
1342 } else if ((addr - sym->st_value) == distance) {
1351 * Convert a section name to the function/data attribute
1352 * .init.text => __init
1353 * .memexitconst => __memconst
1356 * The memory of returned value has been allocated on a heap. The user of this
1357 * method should free it after usage.
1359 static char *sec2annotation(const char *s)
1361 if (match(s, init_exit_sections)) {
1362 char *p = NOFAIL(malloc(20));
1369 while (*s && *s != '.')
1374 if (strstr(s, "rodata") != NULL)
1375 strcat(p, "const ");
1376 else if (strstr(s, "data") != NULL)
1382 return NOFAIL(strdup(""));
1386 static int is_function(Elf_Sym *sym)
1389 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1394 static void print_section_list(const char * const list[20])
1396 const char *const *s = list;
1399 fprintf(stderr, "%s", *s);
1402 fprintf(stderr, ", ");
1404 fprintf(stderr, "\n");
1407 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1410 case 0: *name = "variable"; *name_p = ""; break;
1411 case 1: *name = "function"; *name_p = "()"; break;
1412 default: *name = "(unknown reference)"; *name_p = ""; break;
1417 * Print a warning about a section mismatch.
1418 * Try to find symbols near it so user can find it.
1419 * Check whitelist before warning - it may be a false positive.
1421 static void report_sec_mismatch(const char *modname,
1422 const struct sectioncheck *mismatch,
1423 const char *fromsec,
1424 unsigned long long fromaddr,
1425 const char *fromsym,
1427 const char *tosec, const char *tosym,
1430 const char *from, *from_p;
1431 const char *to, *to_p;
1435 sec_mismatch_count++;
1437 get_pretty_name(from_is_func, &from, &from_p);
1438 get_pretty_name(to_is_func, &to, &to_p);
1440 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1441 "to the %s %s:%s%s\n",
1442 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1445 switch (mismatch->mismatch) {
1446 case TEXT_TO_ANY_INIT:
1447 prl_from = sec2annotation(fromsec);
1448 prl_to = sec2annotation(tosec);
1450 "The function %s%s() references\n"
1452 "This is often because %s lacks a %s\n"
1453 "annotation or the annotation of %s is wrong.\n",
1455 to, prl_to, tosym, to_p,
1456 fromsym, prl_to, tosym);
1460 case DATA_TO_ANY_INIT: {
1461 prl_to = sec2annotation(tosec);
1463 "The variable %s references\n"
1465 "If the reference is valid then annotate the\n"
1466 "variable with __init* or __refdata (see linux/init.h) "
1467 "or name the variable:\n",
1468 fromsym, to, prl_to, tosym, to_p);
1469 print_section_list(mismatch->symbol_white_list);
1473 case TEXT_TO_ANY_EXIT:
1474 prl_to = sec2annotation(tosec);
1476 "The function %s() references a %s in an exit section.\n"
1477 "Often the %s %s%s has valid usage outside the exit section\n"
1478 "and the fix is to remove the %sannotation of %s.\n",
1479 fromsym, to, to, tosym, to_p, prl_to, tosym);
1482 case DATA_TO_ANY_EXIT: {
1483 prl_to = sec2annotation(tosec);
1485 "The variable %s references\n"
1487 "If the reference is valid then annotate the\n"
1488 "variable with __exit* (see linux/init.h) or "
1489 "name the variable:\n",
1490 fromsym, to, prl_to, tosym, to_p);
1491 print_section_list(mismatch->symbol_white_list);
1495 case XXXINIT_TO_SOME_INIT:
1496 case XXXEXIT_TO_SOME_EXIT:
1497 prl_from = sec2annotation(fromsec);
1498 prl_to = sec2annotation(tosec);
1500 "The %s %s%s%s references\n"
1502 "If %s is only used by %s then\n"
1503 "annotate %s with a matching annotation.\n",
1504 from, prl_from, fromsym, from_p,
1505 to, prl_to, tosym, to_p,
1506 tosym, fromsym, tosym);
1510 case ANY_INIT_TO_ANY_EXIT:
1511 prl_from = sec2annotation(fromsec);
1512 prl_to = sec2annotation(tosec);
1514 "The %s %s%s%s references\n"
1516 "This is often seen when error handling "
1517 "in the init function\n"
1518 "uses functionality in the exit path.\n"
1519 "The fix is often to remove the %sannotation of\n"
1520 "%s%s so it may be used outside an exit section.\n",
1521 from, prl_from, fromsym, from_p,
1522 to, prl_to, tosym, to_p,
1523 prl_to, tosym, to_p);
1527 case ANY_EXIT_TO_ANY_INIT:
1528 prl_from = sec2annotation(fromsec);
1529 prl_to = sec2annotation(tosec);
1531 "The %s %s%s%s references\n"
1533 "This is often seen when error handling "
1534 "in the exit function\n"
1535 "uses functionality in the init path.\n"
1536 "The fix is often to remove the %sannotation of\n"
1537 "%s%s so it may be used outside an init section.\n",
1538 from, prl_from, fromsym, from_p,
1539 to, prl_to, tosym, to_p,
1540 prl_to, tosym, to_p);
1544 case EXPORT_TO_INIT_EXIT:
1545 prl_to = sec2annotation(tosec);
1547 "The symbol %s is exported and annotated %s\n"
1548 "Fix this by removing the %sannotation of %s "
1549 "or drop the export.\n",
1550 tosym, prl_to, prl_to, tosym);
1553 case EXTABLE_TO_NON_TEXT:
1554 fatal("There's a special handler for this mismatch type, "
1555 "we should never get here.");
1558 fprintf(stderr, "\n");
1561 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1562 const struct sectioncheck* const mismatch,
1563 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1569 const char *fromsym;
1571 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1572 fromsym = sym_name(elf, from);
1574 if (strstarts(fromsym, "reference___initcall"))
1577 tosec = sec_name(elf, get_secindex(elf, sym));
1578 to = find_elf_symbol(elf, r->r_addend, sym);
1579 tosym = sym_name(elf, to);
1581 /* check whitelist - we may ignore it */
1582 if (secref_whitelist(mismatch,
1583 fromsec, fromsym, tosec, tosym)) {
1584 report_sec_mismatch(modname, mismatch,
1585 fromsec, r->r_offset, fromsym,
1586 is_function(from), tosec, tosym,
1591 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1593 if (section_index > elf->num_sections)
1594 fatal("section_index is outside elf->num_sections!\n");
1596 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1600 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1601 * to know the sizeof(struct exception_table_entry) for the target architecture.
1603 static unsigned int extable_entry_size = 0;
1604 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1607 * If we're currently checking the second relocation within __ex_table,
1608 * that relocation offset tells us the offsetof(struct
1609 * exception_table_entry, fixup) which is equal to sizeof(struct
1610 * exception_table_entry) divided by two. We use that to our advantage
1611 * since there's no portable way to get that size as every architecture
1612 * seems to go with different sized types. Not pretty but better than
1613 * hard-coding the size for every architecture..
1615 if (!extable_entry_size)
1616 extable_entry_size = r->r_offset * 2;
1619 static inline bool is_extable_fault_address(Elf_Rela *r)
1622 * extable_entry_size is only discovered after we've handled the
1623 * _second_ relocation in __ex_table, so only abort when we're not
1624 * handling the first reloc and extable_entry_size is zero.
1626 if (r->r_offset && extable_entry_size == 0)
1627 fatal("extable_entry size hasn't been discovered!\n");
1629 return ((r->r_offset == 0) ||
1630 (r->r_offset % extable_entry_size == 0));
1633 #define is_second_extable_reloc(Start, Cur, Sec) \
1634 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1636 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1637 const struct sectioncheck* const mismatch,
1638 Elf_Rela* r, Elf_Sym* sym,
1639 const char* fromsec, const char* tosec)
1641 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1642 const char* fromsym_name = sym_name(elf, fromsym);
1643 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1644 const char* tosym_name = sym_name(elf, tosym);
1645 const char* from_pretty_name;
1646 const char* from_pretty_name_p;
1647 const char* to_pretty_name;
1648 const char* to_pretty_name_p;
1650 get_pretty_name(is_function(fromsym),
1651 &from_pretty_name, &from_pretty_name_p);
1652 get_pretty_name(is_function(tosym),
1653 &to_pretty_name, &to_pretty_name_p);
1655 warn("%s(%s+0x%lx): Section mismatch in reference"
1656 " from the %s %s%s to the %s %s:%s%s\n",
1657 modname, fromsec, (long)r->r_offset, from_pretty_name,
1658 fromsym_name, from_pretty_name_p,
1659 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1661 if (!match(tosec, mismatch->bad_tosec) &&
1662 is_executable_section(elf, get_secindex(elf, sym)))
1664 "The relocation at %s+0x%lx references\n"
1665 "section \"%s\" which is not in the list of\n"
1666 "authorized sections. If you're adding a new section\n"
1667 "and/or if this reference is valid, add \"%s\" to the\n"
1668 "list of authorized sections to jump to on fault.\n"
1669 "This can be achieved by adding \"%s\" to \n"
1670 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1671 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1674 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1675 const struct sectioncheck* const mismatch,
1676 Elf_Rela* r, Elf_Sym* sym,
1677 const char *fromsec)
1679 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1681 sec_mismatch_count++;
1683 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1685 if (match(tosec, mismatch->bad_tosec))
1686 fatal("The relocation at %s+0x%lx references\n"
1687 "section \"%s\" which is black-listed.\n"
1688 "Something is seriously wrong and should be fixed.\n"
1689 "You might get more information about where this is\n"
1690 "coming from by using scripts/check_extable.sh %s\n",
1691 fromsec, (long)r->r_offset, tosec, modname);
1692 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1693 if (is_extable_fault_address(r))
1694 fatal("The relocation at %s+0x%lx references\n"
1695 "section \"%s\" which is not executable, IOW\n"
1696 "it is not possible for the kernel to fault\n"
1697 "at that address. Something is seriously wrong\n"
1698 "and should be fixed.\n",
1699 fromsec, (long)r->r_offset, tosec);
1701 fatal("The relocation at %s+0x%lx references\n"
1702 "section \"%s\" which is not executable, IOW\n"
1703 "the kernel will fault if it ever tries to\n"
1704 "jump to it. Something is seriously wrong\n"
1705 "and should be fixed.\n",
1706 fromsec, (long)r->r_offset, tosec);
1710 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1711 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1713 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1714 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1717 if (mismatch->handler)
1718 mismatch->handler(modname, elf, mismatch,
1721 default_mismatch_handler(modname, elf, mismatch,
1726 static unsigned int *reloc_location(struct elf_info *elf,
1727 Elf_Shdr *sechdr, Elf_Rela *r)
1729 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1732 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1734 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1735 unsigned int *location = reloc_location(elf, sechdr, r);
1739 r->r_addend = TO_NATIVE(*location);
1742 r->r_addend = TO_NATIVE(*location) + 4;
1743 /* For CONFIG_RELOCATABLE=y */
1744 if (elf->hdr->e_type == ET_EXEC)
1745 r->r_addend += r->r_offset;
1752 #define R_ARM_CALL 28
1754 #ifndef R_ARM_JUMP24
1755 #define R_ARM_JUMP24 29
1758 #ifndef R_ARM_THM_CALL
1759 #define R_ARM_THM_CALL 10
1761 #ifndef R_ARM_THM_JUMP24
1762 #define R_ARM_THM_JUMP24 30
1764 #ifndef R_ARM_THM_JUMP19
1765 #define R_ARM_THM_JUMP19 51
1768 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1770 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1774 /* From ARM ABI: (S + A) | T */
1775 r->r_addend = (int)(long)
1776 (elf->symtab_start + ELF_R_SYM(r->r_info));
1781 case R_ARM_THM_CALL:
1782 case R_ARM_THM_JUMP24:
1783 case R_ARM_THM_JUMP19:
1784 /* From ARM ABI: ((S + A) | T) - P */
1785 r->r_addend = (int)(long)(elf->hdr +
1787 (r->r_offset - sechdr->sh_addr));
1795 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1797 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1798 unsigned int *location = reloc_location(elf, sechdr, r);
1801 if (r_typ == R_MIPS_HI16)
1802 return 1; /* skip this */
1803 inst = TO_NATIVE(*location);
1806 r->r_addend = inst & 0xffff;
1809 r->r_addend = (inst & 0x03ffffff) << 2;
1819 #define EM_RISCV 243
1822 #ifndef R_RISCV_SUB32
1823 #define R_RISCV_SUB32 39
1826 static void section_rela(const char *modname, struct elf_info *elf,
1833 const char *fromsec;
1835 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1836 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1838 fromsec = sech_name(elf, sechdr);
1839 fromsec += strlen(".rela");
1840 /* if from section (name) is know good then skip it */
1841 if (match(fromsec, section_white_list))
1844 for (rela = start; rela < stop; rela++) {
1845 r.r_offset = TO_NATIVE(rela->r_offset);
1846 #if KERNEL_ELFCLASS == ELFCLASS64
1847 if (elf->hdr->e_machine == EM_MIPS) {
1849 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1850 r_sym = TO_NATIVE(r_sym);
1851 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1852 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1854 r.r_info = TO_NATIVE(rela->r_info);
1855 r_sym = ELF_R_SYM(r.r_info);
1858 r.r_info = TO_NATIVE(rela->r_info);
1859 r_sym = ELF_R_SYM(r.r_info);
1861 r.r_addend = TO_NATIVE(rela->r_addend);
1862 switch (elf->hdr->e_machine) {
1864 if (!strcmp("__ex_table", fromsec) &&
1865 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1869 sym = elf->symtab_start + r_sym;
1870 /* Skip special sections */
1871 if (is_shndx_special(sym->st_shndx))
1873 if (is_second_extable_reloc(start, rela, fromsec))
1874 find_extable_entry_size(fromsec, &r);
1875 check_section_mismatch(modname, elf, &r, sym, fromsec);
1879 static void section_rel(const char *modname, struct elf_info *elf,
1886 const char *fromsec;
1888 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1889 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1891 fromsec = sech_name(elf, sechdr);
1892 fromsec += strlen(".rel");
1893 /* if from section (name) is know good then skip it */
1894 if (match(fromsec, section_white_list))
1897 for (rel = start; rel < stop; rel++) {
1898 r.r_offset = TO_NATIVE(rel->r_offset);
1899 #if KERNEL_ELFCLASS == ELFCLASS64
1900 if (elf->hdr->e_machine == EM_MIPS) {
1902 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1903 r_sym = TO_NATIVE(r_sym);
1904 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1905 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1907 r.r_info = TO_NATIVE(rel->r_info);
1908 r_sym = ELF_R_SYM(r.r_info);
1911 r.r_info = TO_NATIVE(rel->r_info);
1912 r_sym = ELF_R_SYM(r.r_info);
1915 switch (elf->hdr->e_machine) {
1917 if (addend_386_rel(elf, sechdr, &r))
1921 if (addend_arm_rel(elf, sechdr, &r))
1925 if (addend_mips_rel(elf, sechdr, &r))
1929 sym = elf->symtab_start + r_sym;
1930 /* Skip special sections */
1931 if (is_shndx_special(sym->st_shndx))
1933 if (is_second_extable_reloc(start, rel, fromsec))
1934 find_extable_entry_size(fromsec, &r);
1935 check_section_mismatch(modname, elf, &r, sym, fromsec);
1940 * A module includes a number of sections that are discarded
1941 * either when loaded or when used as built-in.
1942 * For loaded modules all functions marked __init and all data
1943 * marked __initdata will be discarded when the module has been initialized.
1944 * Likewise for modules used built-in the sections marked __exit
1945 * are discarded because __exit marked function are supposed to be called
1946 * only when a module is unloaded which never happens for built-in modules.
1947 * The check_sec_ref() function traverses all relocation records
1948 * to find all references to a section that reference a section that will
1949 * be discarded and warns about it.
1951 static void check_sec_ref(struct module *mod, const char *modname,
1952 struct elf_info *elf)
1955 Elf_Shdr *sechdrs = elf->sechdrs;
1957 /* Walk through all sections */
1958 for (i = 0; i < elf->num_sections; i++) {
1959 check_section(modname, elf, &elf->sechdrs[i]);
1960 /* We want to process only relocation sections and not .init */
1961 if (sechdrs[i].sh_type == SHT_RELA)
1962 section_rela(modname, elf, &elf->sechdrs[i]);
1963 else if (sechdrs[i].sh_type == SHT_REL)
1964 section_rel(modname, elf, &elf->sechdrs[i]);
1968 static char *remove_dot(char *s)
1970 size_t n = strcspn(s, ".");
1973 size_t m = strspn(s + n + 1, "0123456789");
1974 if (m && (s[n + m] == '.' || s[n + m] == 0))
1977 /* strip trailing .prelink */
1978 if (strends(s, ".prelink"))
1979 s[strlen(s) - 8] = '\0';
1984 static void read_symbols(const char *modname)
1986 const char *symname;
1991 struct elf_info info = { };
1994 if (!parse_elf(&info, modname))
2000 /* strip trailing .o */
2001 tmp = NOFAIL(strdup(modname));
2002 tmp[strlen(tmp) - 2] = '\0';
2003 /* strip trailing .prelink */
2004 if (strends(tmp, ".prelink"))
2005 tmp[strlen(tmp) - 8] = '\0';
2006 mod = new_module(tmp);
2010 if (!mod->is_vmlinux) {
2011 license = get_modinfo(&info, "license");
2013 error("missing MODULE_LICENSE() in %s\n", modname);
2015 if (license_is_gpl_compatible(license))
2016 mod->gpl_compatible = 1;
2018 mod->gpl_compatible = 0;
2021 license = get_next_modinfo(&info, "license", license);
2024 namespace = get_modinfo(&info, "import_ns");
2026 add_namespace(&mod->imported_namespaces, namespace);
2027 namespace = get_next_modinfo(&info, "import_ns",
2032 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2033 symname = remove_dot(info.strtab + sym->st_name);
2035 handle_symbol(mod, &info, sym, symname);
2036 handle_moddevtable(mod, &info, sym, symname);
2039 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2040 symname = remove_dot(info.strtab + sym->st_name);
2042 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2043 if (strstarts(symname, "__kstrtabns_"))
2044 sym_update_namespace(symname + strlen("__kstrtabns_"),
2045 sym_get_data(&info, sym));
2046 if (strstarts(symname, "__crc_"))
2047 handle_modversion(mod, &info, sym,
2048 symname + strlen("__crc_"));
2051 // check for static EXPORT_SYMBOL_* functions && global vars
2052 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2053 unsigned char bind = ELF_ST_BIND(sym->st_info);
2055 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2057 find_symbol(remove_dot(info.strtab +
2065 check_sec_ref(mod, modname, &info);
2067 if (!mod->is_vmlinux) {
2068 version = get_modinfo(&info, "version");
2069 if (version || all_versions)
2070 get_src_version(mod->name, mod->srcversion,
2071 sizeof(mod->srcversion) - 1);
2074 parse_elf_finish(&info);
2076 /* Our trick to get versioning for module struct etc. - it's
2077 * never passed as an argument to an exported function, so
2078 * the automatic versioning doesn't pick it up, but it's really
2079 * important anyhow */
2081 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
2084 static void read_symbols_from_files(const char *filename)
2087 char fname[PATH_MAX];
2089 if (strcmp(filename, "-") != 0) {
2090 in = fopen(filename, "r");
2092 fatal("Can't open filenames file %s: %m", filename);
2095 while (fgets(fname, PATH_MAX, in) != NULL) {
2096 if (strends(fname, "\n"))
2097 fname[strlen(fname)-1] = '\0';
2098 read_symbols(fname);
2107 /* We first write the generated file into memory using the
2108 * following helper, then compare to the file on disk and
2109 * only update the later if anything changed */
2111 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2112 const char *fmt, ...)
2119 len = vsnprintf(tmp, SZ, fmt, ap);
2120 buf_write(buf, tmp, len);
2124 void buf_write(struct buffer *buf, const char *s, int len)
2126 if (buf->size - buf->pos < len) {
2127 buf->size += len + SZ;
2128 buf->p = NOFAIL(realloc(buf->p, buf->size));
2130 strncpy(buf->p + buf->pos, s, len);
2134 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2138 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2142 case export_unknown:
2148 static void check_exports(struct module *mod)
2150 struct symbol *s, *exp;
2152 for (s = mod->unres; s; s = s->next) {
2153 const char *basename;
2154 exp = find_symbol(s->name);
2156 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2157 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2158 "\"%s\" [%s.ko] undefined!\n",
2159 s->name, mod->name);
2162 if (exp->module == mod) {
2163 error("\"%s\" [%s.ko] was exported without definition\n",
2164 s->name, mod->name);
2168 s->module = exp->module;
2169 s->crc_valid = exp->crc_valid;
2172 basename = strrchr(mod->name, '/');
2176 basename = mod->name;
2178 if (exp->namespace &&
2179 !module_imports_namespace(mod, exp->namespace)) {
2180 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2181 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2182 basename, exp->name, exp->namespace);
2183 add_namespace(&mod->missing_namespaces, exp->namespace);
2186 if (!mod->gpl_compatible)
2187 check_for_gpl_usage(exp->export, basename, exp->name);
2191 static void check_modname_len(struct module *mod)
2193 const char *mod_name;
2195 mod_name = strrchr(mod->name, '/');
2196 if (mod_name == NULL)
2197 mod_name = mod->name;
2200 if (strlen(mod_name) >= MODULE_NAME_LEN)
2201 error("module name is too long [%s.ko]\n", mod->name);
2205 * Header for the generated file
2207 static void add_header(struct buffer *b, struct module *mod)
2209 buf_printf(b, "#include <linux/module.h>\n");
2211 * Include build-salt.h after module.h in order to
2212 * inherit the definitions.
2214 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2215 buf_printf(b, "#include <linux/build-salt.h>\n");
2216 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2217 buf_printf(b, "#include <linux/vermagic.h>\n");
2218 buf_printf(b, "#include <linux/compiler.h>\n");
2219 buf_printf(b, "\n");
2220 buf_printf(b, "BUILD_SALT;\n");
2221 buf_printf(b, "BUILD_LTO_INFO;\n");
2222 buf_printf(b, "\n");
2223 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2224 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2225 buf_printf(b, "\n");
2226 buf_printf(b, "__visible struct module __this_module\n");
2227 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2228 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2230 buf_printf(b, "\t.init = init_module,\n");
2231 if (mod->has_cleanup)
2232 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2233 "\t.exit = cleanup_module,\n"
2235 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2236 buf_printf(b, "};\n");
2239 static void add_intree_flag(struct buffer *b, int is_intree)
2242 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2245 /* Cannot check for assembler */
2246 static void add_retpoline(struct buffer *b)
2248 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2249 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2250 buf_printf(b, "#endif\n");
2253 static void add_staging_flag(struct buffer *b, const char *name)
2255 if (strstarts(name, "drivers/staging"))
2256 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2260 * Record CRCs for unresolved symbols
2262 static void add_versions(struct buffer *b, struct module *mod)
2269 buf_printf(b, "\n");
2270 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2271 buf_printf(b, "__used __section(\"__versions\") = {\n");
2273 for (s = mod->unres; s; s = s->next) {
2276 if (!s->crc_valid) {
2277 warn("\"%s\" [%s.ko] has no CRC!\n",
2278 s->name, mod->name);
2281 if (strlen(s->name) >= MODULE_NAME_LEN) {
2282 error("too long symbol \"%s\" [%s.ko]\n",
2283 s->name, mod->name);
2286 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2290 buf_printf(b, "};\n");
2293 static void add_depends(struct buffer *b, struct module *mod)
2298 /* Clear ->seen flag of modules that own symbols needed by this. */
2299 for (s = mod->unres; s; s = s->next)
2301 s->module->seen = s->module->is_vmlinux;
2303 buf_printf(b, "\n");
2304 buf_printf(b, "MODULE_INFO(depends, \"");
2305 for (s = mod->unres; s; s = s->next) {
2310 if (s->module->seen)
2313 s->module->seen = 1;
2314 p = strrchr(s->module->name, '/');
2318 p = s->module->name;
2319 buf_printf(b, "%s%s", first ? "" : ",", p);
2322 buf_printf(b, "\");\n");
2325 static void add_srcversion(struct buffer *b, struct module *mod)
2327 if (mod->srcversion[0]) {
2328 buf_printf(b, "\n");
2329 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2334 static void write_buf(struct buffer *b, const char *fname)
2341 file = fopen(fname, "w");
2346 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2350 if (fclose(file) != 0) {
2356 static void write_if_changed(struct buffer *b, const char *fname)
2362 file = fopen(fname, "r");
2366 if (fstat(fileno(file), &st) < 0)
2369 if (st.st_size != b->pos)
2372 tmp = NOFAIL(malloc(b->pos));
2373 if (fread(tmp, 1, b->pos, file) != b->pos)
2376 if (memcmp(tmp, b->p, b->pos) != 0)
2388 write_buf(b, fname);
2391 /* parse Module.symvers file. line format:
2392 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2394 static void read_dump(const char *fname)
2396 char *buf, *pos, *line;
2398 buf = read_text_file(fname);
2400 /* No symbol versions, silently ignore */
2405 while ((line = get_line(&pos))) {
2406 char *symname, *namespace, *modname, *d, *export;
2411 if (!(symname = strchr(line, '\t')))
2414 if (!(modname = strchr(symname, '\t')))
2417 if (!(export = strchr(modname, '\t')))
2420 if (!(namespace = strchr(export, '\t')))
2422 *namespace++ = '\0';
2424 crc = strtoul(line, &d, 16);
2425 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2427 mod = find_module(modname);
2429 mod = new_module(modname);
2432 s = sym_add_exported(symname, mod, export_no(export));
2434 sym_set_crc(symname, crc);
2435 sym_update_namespace(symname, namespace);
2441 fatal("parse error in symbol dump file\n");
2444 static void write_dump(const char *fname)
2446 struct buffer buf = { };
2447 struct symbol *symbol;
2448 const char *namespace;
2451 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2452 symbol = symbolhash[n];
2454 if (!symbol->module->from_dump) {
2455 namespace = symbol->namespace;
2456 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2457 symbol->crc, symbol->name,
2458 symbol->module->name,
2459 export_str(symbol->export),
2460 namespace ? namespace : "");
2462 symbol = symbol->next;
2465 write_buf(&buf, fname);
2469 static void write_namespace_deps_files(const char *fname)
2472 struct namespace_list *ns;
2473 struct buffer ns_deps_buf = {};
2475 for (mod = modules; mod; mod = mod->next) {
2477 if (mod->from_dump || !mod->missing_namespaces)
2480 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2482 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2483 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2485 buf_printf(&ns_deps_buf, "\n");
2488 write_if_changed(&ns_deps_buf, fname);
2489 free(ns_deps_buf.p);
2493 struct dump_list *next;
2497 int main(int argc, char **argv)
2500 struct buffer buf = { };
2501 char *missing_namespace_deps = NULL;
2502 char *dump_write = NULL, *files_source = NULL;
2505 struct dump_list *dump_read_start = NULL;
2506 struct dump_list **dump_read_iter = &dump_read_start;
2508 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2511 external_module = 1;
2515 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2516 (*dump_read_iter)->file = optarg;
2517 dump_read_iter = &(*dump_read_iter)->next;
2523 ignore_missing_files = 1;
2526 dump_write = optarg;
2532 files_source = optarg;
2535 warn_unresolved = 1;
2538 sec_mismatch_warn_only = false;
2541 allow_missing_ns_imports = 1;
2544 missing_namespace_deps = optarg;
2551 while (dump_read_start) {
2552 struct dump_list *tmp;
2554 read_dump(dump_read_start->file);
2555 tmp = dump_read_start->next;
2556 free(dump_read_start);
2557 dump_read_start = tmp;
2560 while (optind < argc)
2561 read_symbols(argv[optind++]);
2564 read_symbols_from_files(files_source);
2566 for (mod = modules; mod; mod = mod->next) {
2567 char fname[PATH_MAX];
2570 if (mod->is_vmlinux || mod->from_dump)
2575 check_modname_len(mod);
2578 add_header(&buf, mod);
2579 add_intree_flag(&buf, !external_module);
2580 add_retpoline(&buf);
2581 add_staging_flag(&buf, mod->name);
2582 add_versions(&buf, mod);
2583 add_depends(&buf, mod);
2584 add_moddevtable(&buf, mod);
2585 add_srcversion(&buf, mod);
2587 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2588 if (ret >= sizeof(fname)) {
2589 error("%s: too long path was truncated\n", fname);
2593 write_if_changed(&buf, fname);
2596 if (missing_namespace_deps)
2597 write_namespace_deps_files(missing_namespace_deps);
2600 write_dump(dump_write);
2601 if (sec_mismatch_count && !sec_mismatch_warn_only)
2602 error("Section mismatches detected.\n"
2603 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2604 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2607 for (s = symbolhash[n]; s; s = s->next) {
2609 error("\"%s\" [%s] is a static %s\n",
2610 s->name, s->module->name,
2611 export_str(s->export));
2615 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2616 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2617 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2621 return error_occurred ? 1 : 0;