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 bool modversions;
27 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28 static bool all_versions;
29 /* If we are modposting external module set to 1 */
30 static bool external_module;
31 /* Only warn about unresolved symbols */
32 static bool warn_unresolved;
33 /* How a symbol is exported */
34 static int sec_mismatch_count;
35 static bool sec_mismatch_warn_only = true;
36 /* ignore missing files */
37 static bool ignore_missing_files;
38 /* If set to 1, only warn (instead of error) about missing ns imports */
39 static bool 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 */
170 static struct module *find_module(const char *modname)
174 list_for_each_entry(mod, &modules, list) {
175 if (strcmp(mod->name, modname) == 0)
181 static struct module *new_module(const char *modname)
185 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
186 memset(mod, 0, sizeof(*mod));
188 INIT_LIST_HEAD(&mod->unresolved_symbols);
189 INIT_LIST_HEAD(&mod->missing_namespaces);
190 INIT_LIST_HEAD(&mod->imported_namespaces);
192 strcpy(mod->name, modname);
193 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
196 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
197 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
198 * modpost will exit wiht error anyway.
200 mod->is_gpl_compatible = true;
202 list_add_tail(&mod->list, &modules);
207 /* A hash of all exported symbols,
208 * struct symbol is also used for lists of unresolved symbols */
210 #define SYMBOL_HASH_SIZE 1024
214 struct list_head list; /* link to module::unresolved_symbols */
215 struct module *module;
220 bool is_static; /* true if symbol is not global */
221 enum export export; /* Type of export */
225 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
227 /* This is based on the hash algorithm from gdbm, via tdb */
228 static inline unsigned int tdb_hash(const char *name)
230 unsigned value; /* Used to compute the hash value. */
231 unsigned i; /* Used to cycle through random values. */
233 /* Set the initial value from the key size. */
234 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
235 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
237 return (1103515243 * value + 12345);
241 * Allocate a new symbols for use in the hash of exported symbols or
242 * the list of unresolved symbols per module
244 static struct symbol *alloc_symbol(const char *name, struct symbol *next)
246 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
248 memset(s, 0, sizeof(*s));
249 strcpy(s->name, name);
255 /* For the hash of exported symbols */
256 static struct symbol *new_symbol(const char *name, struct module *module,
261 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
262 symbolhash[hash] = alloc_symbol(name, symbolhash[hash]);
264 return symbolhash[hash];
267 static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
271 sym = alloc_symbol(name, NULL);
274 list_add_tail(&sym->list, &mod->unresolved_symbols);
277 static struct symbol *find_symbol(const char *name)
281 /* For our purposes, .foo matches foo. PPC64 needs this. */
285 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
286 if (strcmp(s->name, name) == 0)
292 struct namespace_list {
293 struct list_head list;
297 static bool contains_namespace(struct list_head *head, const char *namespace)
299 struct namespace_list *list;
301 list_for_each_entry(list, head, list) {
302 if (!strcmp(list->namespace, namespace))
309 static void add_namespace(struct list_head *head, const char *namespace)
311 struct namespace_list *ns_entry;
313 if (!contains_namespace(head, namespace)) {
314 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
315 strlen(namespace) + 1));
316 strcpy(ns_entry->namespace, namespace);
317 list_add_tail(&ns_entry->list, head);
321 static const struct {
325 { .str = "EXPORT_SYMBOL", .export = export_plain },
326 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
327 { .str = "(unknown)", .export = export_unknown },
331 static const char *export_str(enum export ex)
333 return export_list[ex].str;
336 static enum export export_no(const char *s)
341 return export_unknown;
342 for (i = 0; export_list[i].export != export_unknown; i++) {
343 if (strcmp(export_list[i].str, s) == 0)
344 return export_list[i].export;
346 return export_unknown;
349 static void *sym_get_data_by_offset(const struct elf_info *info,
350 unsigned int secindex, unsigned long offset)
352 Elf_Shdr *sechdr = &info->sechdrs[secindex];
354 if (info->hdr->e_type != ET_REL)
355 offset -= sechdr->sh_addr;
357 return (void *)info->hdr + sechdr->sh_offset + offset;
360 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
362 return sym_get_data_by_offset(info, get_secindex(info, sym),
366 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
368 return sym_get_data_by_offset(info, info->secindex_strings,
372 static const char *sec_name(const struct elf_info *info, int secindex)
374 return sech_name(info, &info->sechdrs[secindex]);
377 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
379 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
381 const char *secname = sec_name(elf, sec);
383 if (strstarts(secname, "___ksymtab+"))
385 else if (strstarts(secname, "___ksymtab_gpl+"))
388 return export_unknown;
391 static void sym_update_namespace(const char *symname, const char *namespace)
393 struct symbol *s = find_symbol(symname);
396 * That symbol should have been created earlier and thus this is
397 * actually an assertion.
400 error("Could not update namespace(%s) for symbol %s\n",
406 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
409 static struct symbol *sym_add_exported(const char *name, struct module *mod,
412 struct symbol *s = find_symbol(name);
415 s = new_symbol(name, mod, export);
416 } else if (!external_module || s->module->is_vmlinux ||
418 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
419 mod->name, name, s->module->name,
420 s->module->is_vmlinux ? "" : ".ko");
429 static void sym_set_crc(const char *name, unsigned int crc)
431 struct symbol *s = find_symbol(name);
434 * Ignore stand-alone __crc_*, which might be auto-generated symbols
435 * such as __*_veneer in ARM ELF.
444 static void *grab_file(const char *filename, size_t *size)
447 void *map = MAP_FAILED;
450 fd = open(filename, O_RDONLY);
457 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
461 if (map == MAP_FAILED)
466 static void release_file(void *file, size_t size)
471 static int parse_elf(struct elf_info *info, const char *filename)
477 const char *secstrings;
478 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
480 hdr = grab_file(filename, &info->size);
482 if (ignore_missing_files) {
483 fprintf(stderr, "%s: %s (ignored)\n", filename,
491 if (info->size < sizeof(*hdr)) {
492 /* file too small, assume this is an empty .o file */
495 /* Is this a valid ELF file? */
496 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
497 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
498 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
499 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
500 /* Not an ELF file - silently ignore it */
503 /* Fix endianness in ELF header */
504 hdr->e_type = TO_NATIVE(hdr->e_type);
505 hdr->e_machine = TO_NATIVE(hdr->e_machine);
506 hdr->e_version = TO_NATIVE(hdr->e_version);
507 hdr->e_entry = TO_NATIVE(hdr->e_entry);
508 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
509 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
510 hdr->e_flags = TO_NATIVE(hdr->e_flags);
511 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
512 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
513 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
514 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
515 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
516 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
517 sechdrs = (void *)hdr + hdr->e_shoff;
518 info->sechdrs = sechdrs;
520 /* Check if file offset is correct */
521 if (hdr->e_shoff > info->size) {
522 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
523 (unsigned long)hdr->e_shoff, filename, info->size);
527 if (hdr->e_shnum == SHN_UNDEF) {
529 * There are more than 64k sections,
530 * read count from .sh_size.
532 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
535 info->num_sections = hdr->e_shnum;
537 if (hdr->e_shstrndx == SHN_XINDEX) {
538 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
541 info->secindex_strings = hdr->e_shstrndx;
544 /* Fix endianness in section headers */
545 for (i = 0; i < info->num_sections; i++) {
546 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
547 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
548 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
549 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
550 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
551 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
552 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
553 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
554 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
555 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
557 /* Find symbol table. */
558 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
559 for (i = 1; i < info->num_sections; i++) {
561 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
563 if (!nobits && sechdrs[i].sh_offset > info->size) {
564 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
565 "sizeof(*hrd)=%zu\n", filename,
566 (unsigned long)sechdrs[i].sh_offset,
570 secname = secstrings + sechdrs[i].sh_name;
571 if (strcmp(secname, ".modinfo") == 0) {
573 fatal("%s has NOBITS .modinfo\n", filename);
574 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
575 info->modinfo_len = sechdrs[i].sh_size;
578 if (sechdrs[i].sh_type == SHT_SYMTAB) {
579 unsigned int sh_link_idx;
581 info->symtab_start = (void *)hdr +
582 sechdrs[i].sh_offset;
583 info->symtab_stop = (void *)hdr +
584 sechdrs[i].sh_offset + sechdrs[i].sh_size;
585 sh_link_idx = sechdrs[i].sh_link;
586 info->strtab = (void *)hdr +
587 sechdrs[sh_link_idx].sh_offset;
590 /* 32bit section no. table? ("more than 64k sections") */
591 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
592 symtab_shndx_idx = i;
593 info->symtab_shndx_start = (void *)hdr +
594 sechdrs[i].sh_offset;
595 info->symtab_shndx_stop = (void *)hdr +
596 sechdrs[i].sh_offset + sechdrs[i].sh_size;
599 if (!info->symtab_start)
600 fatal("%s has no symtab?\n", filename);
602 /* Fix endianness in symbols */
603 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
604 sym->st_shndx = TO_NATIVE(sym->st_shndx);
605 sym->st_name = TO_NATIVE(sym->st_name);
606 sym->st_value = TO_NATIVE(sym->st_value);
607 sym->st_size = TO_NATIVE(sym->st_size);
610 if (symtab_shndx_idx != ~0U) {
612 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
613 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
614 filename, sechdrs[symtab_shndx_idx].sh_link,
617 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
625 static void parse_elf_finish(struct elf_info *info)
627 release_file(info->hdr, info->size);
630 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
632 /* ignore __this_module, it will be resolved shortly */
633 if (strcmp(symname, "__this_module") == 0)
635 /* ignore global offset table */
636 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
638 if (info->hdr->e_machine == EM_PPC)
639 /* Special register function linked on all modules during final link of .ko */
640 if (strstarts(symname, "_restgpr_") ||
641 strstarts(symname, "_savegpr_") ||
642 strstarts(symname, "_rest32gpr_") ||
643 strstarts(symname, "_save32gpr_") ||
644 strstarts(symname, "_restvr_") ||
645 strstarts(symname, "_savevr_"))
647 if (info->hdr->e_machine == EM_PPC64)
648 /* Special register function linked on all modules during final link of .ko */
649 if (strstarts(symname, "_restgpr0_") ||
650 strstarts(symname, "_savegpr0_") ||
651 strstarts(symname, "_restvr_") ||
652 strstarts(symname, "_savevr_") ||
653 strcmp(symname, ".TOC.") == 0)
656 if (info->hdr->e_machine == EM_S390)
657 /* Expoline thunks are linked on all kernel modules during final link of .ko */
658 if (strstarts(symname, "__s390_indirect_jump_r"))
660 /* Do not ignore this symbol */
664 static void handle_modversion(const struct module *mod,
665 const struct elf_info *info,
666 const Elf_Sym *sym, const char *symname)
670 if (sym->st_shndx == SHN_UNDEF) {
671 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
672 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
673 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
679 if (sym->st_shndx == SHN_ABS) {
684 /* symbol points to the CRC in the ELF object */
685 crcp = sym_get_data(info, sym);
686 crc = TO_NATIVE(*crcp);
688 sym_set_crc(symname, crc);
691 static void handle_symbol(struct module *mod, struct elf_info *info,
692 const Elf_Sym *sym, const char *symname)
696 switch (sym->st_shndx) {
698 if (strstarts(symname, "__gnu_lto_")) {
699 /* Should warn here, but modpost runs before the linker */
701 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
704 /* undefined symbol */
705 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
706 ELF_ST_BIND(sym->st_info) != STB_WEAK)
708 if (ignore_undef_symbol(info, symname))
710 if (info->hdr->e_machine == EM_SPARC ||
711 info->hdr->e_machine == EM_SPARCV9) {
712 /* Ignore register directives. */
713 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
715 if (symname[0] == '.') {
716 char *munged = NOFAIL(strdup(symname));
718 munged[1] = toupper(munged[1]);
723 sym_add_unresolved(symname, mod,
724 ELF_ST_BIND(sym->st_info) == STB_WEAK);
727 /* All exported symbols */
728 if (strstarts(symname, "__ksymtab_")) {
731 name = symname + strlen("__ksymtab_");
732 export = export_from_secname(info,
733 get_secindex(info, sym));
734 sym_add_exported(name, mod, export);
736 if (strcmp(symname, "init_module") == 0)
737 mod->has_init = true;
738 if (strcmp(symname, "cleanup_module") == 0)
739 mod->has_cleanup = true;
745 * Parse tag=value strings from .modinfo section
747 static char *next_string(char *string, unsigned long *secsize)
749 /* Skip non-zero chars */
752 if ((*secsize)-- <= 1)
756 /* Skip any zero padding. */
759 if ((*secsize)-- <= 1)
765 static char *get_next_modinfo(struct elf_info *info, const char *tag,
769 unsigned int taglen = strlen(tag);
770 char *modinfo = info->modinfo;
771 unsigned long size = info->modinfo_len;
774 size -= prev - modinfo;
775 modinfo = next_string(prev, &size);
778 for (p = modinfo; p; p = next_string(p, &size)) {
779 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
780 return p + taglen + 1;
785 static char *get_modinfo(struct elf_info *info, const char *tag)
788 return get_next_modinfo(info, tag, NULL);
792 * Test if string s ends in string sub
795 static int strrcmp(const char *s, const char *sub)
803 sublen = strlen(sub);
805 if ((slen == 0) || (sublen == 0))
811 return memcmp(s + slen - sublen, sub, sublen);
814 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
817 return elf->strtab + sym->st_name;
822 /* The pattern is an array of simple patterns.
823 * "foo" will match an exact string equal to "foo"
824 * "*foo" will match a string that ends with "foo"
825 * "foo*" will match a string that begins with "foo"
826 * "*foo*" will match a string that contains "foo"
828 static int match(const char *sym, const char * const pat[])
835 endp = p + strlen(p) - 1;
838 if (*p == '*' && *endp == '*') {
839 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
840 char *here = strstr(sym, bare);
847 else if (*p == '*') {
848 if (strrcmp(sym, p + 1) == 0)
852 else if (*endp == '*') {
853 if (strncmp(sym, p, strlen(p) - 1) == 0)
858 if (strcmp(p, sym) == 0)
866 /* sections that we do not want to do full section mismatch check on */
867 static const char *const section_white_list[] =
871 ".cranges", /* sh64 */
872 ".zdebug*", /* Compressed debug sections. */
873 ".GCC.command.line", /* record-gcc-switches */
874 ".mdebug*", /* alpha, score, mips etc. */
875 ".pdr", /* alpha, score, mips etc. */
880 ".xt.prop", /* xtensa */
881 ".xt.lit", /* xtensa */
882 ".arcextmap*", /* arc */
883 ".gnu.linkonce.arcext*", /* arc : modules */
884 ".cmem*", /* EZchip */
885 ".fmt_slot*", /* EZchip */
892 * This is used to find sections missing the SHF_ALLOC flag.
893 * The cause of this is often a section specified in assembler
894 * without "ax" / "aw".
896 static void check_section(const char *modname, struct elf_info *elf,
899 const char *sec = sech_name(elf, sechdr);
901 if (sechdr->sh_type == SHT_PROGBITS &&
902 !(sechdr->sh_flags & SHF_ALLOC) &&
903 !match(sec, section_white_list)) {
904 warn("%s (%s): unexpected non-allocatable section.\n"
905 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
906 "Note that for example <linux/init.h> contains\n"
907 "section definitions for use in .S files.\n\n",
914 #define ALL_INIT_DATA_SECTIONS \
915 ".init.setup", ".init.rodata", ".meminit.rodata", \
916 ".init.data", ".meminit.data"
917 #define ALL_EXIT_DATA_SECTIONS \
918 ".exit.data", ".memexit.data"
920 #define ALL_INIT_TEXT_SECTIONS \
921 ".init.text", ".meminit.text"
922 #define ALL_EXIT_TEXT_SECTIONS \
923 ".exit.text", ".memexit.text"
925 #define ALL_PCI_INIT_SECTIONS \
926 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
927 ".pci_fixup_enable", ".pci_fixup_resume", \
928 ".pci_fixup_resume_early", ".pci_fixup_suspend"
930 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
931 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
933 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
934 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
936 #define DATA_SECTIONS ".data", ".data.rel"
937 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
938 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
939 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
940 ".fixup", ".entry.text", ".exception.text", ".text.*", \
941 ".coldtext", ".softirqentry.text"
943 #define INIT_SECTIONS ".init.*"
944 #define MEM_INIT_SECTIONS ".meminit.*"
946 #define EXIT_SECTIONS ".exit.*"
947 #define MEM_EXIT_SECTIONS ".memexit.*"
949 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
950 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
952 /* init data sections */
953 static const char *const init_data_sections[] =
954 { ALL_INIT_DATA_SECTIONS, NULL };
956 /* all init sections */
957 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
959 /* All init and exit sections (code + data) */
960 static const char *const init_exit_sections[] =
961 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
963 /* all text sections */
964 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
967 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
970 /* symbols in .data that may refer to init/exit sections */
971 #define DEFAULT_SYMBOL_WHITE_LIST \
973 "*_template", /* scsi uses *_template a lot */ \
974 "*_timer", /* arm uses ops structures named _timer a lot */ \
975 "*_sht", /* scsi also used *_sht to some extent */ \
981 static const char *const head_sections[] = { ".head.text*", NULL };
982 static const char *const linker_symbols[] =
983 { "__init_begin", "_sinittext", "_einittext", NULL };
984 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
991 XXXINIT_TO_SOME_INIT,
992 XXXEXIT_TO_SOME_EXIT,
993 ANY_INIT_TO_ANY_EXIT,
994 ANY_EXIT_TO_ANY_INIT,
1000 * Describe how to match sections on different criteria:
1002 * @fromsec: Array of sections to be matched.
1004 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1005 * this array is forbidden (black-list). Can be empty.
1007 * @good_tosec: Relocations applied to a section in @fromsec must be
1008 * targeting sections in this array (white-list). Can be empty.
1010 * @mismatch: Type of mismatch.
1012 * @symbol_white_list: Do not match a relocation to a symbol in this list
1013 * even if it is targeting a section in @bad_to_sec.
1015 * @handler: Specific handler to call when a match is found. If NULL,
1016 * default_mismatch_handler() will be called.
1019 struct sectioncheck {
1020 const char *fromsec[20];
1021 const char *bad_tosec[20];
1022 const char *good_tosec[20];
1023 enum mismatch mismatch;
1024 const char *symbol_white_list[20];
1025 void (*handler)(const char *modname, struct elf_info *elf,
1026 const struct sectioncheck* const mismatch,
1027 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1031 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1032 const struct sectioncheck* const mismatch,
1033 Elf_Rela *r, Elf_Sym *sym,
1034 const char *fromsec);
1036 static const struct sectioncheck sectioncheck[] = {
1037 /* Do not reference init/exit code/data from
1038 * normal code and data
1041 .fromsec = { TEXT_SECTIONS, NULL },
1042 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1043 .mismatch = TEXT_TO_ANY_INIT,
1044 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1047 .fromsec = { DATA_SECTIONS, NULL },
1048 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1049 .mismatch = DATA_TO_ANY_INIT,
1050 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1053 .fromsec = { DATA_SECTIONS, NULL },
1054 .bad_tosec = { INIT_SECTIONS, NULL },
1055 .mismatch = DATA_TO_ANY_INIT,
1056 .symbol_white_list = {
1057 "*_template", "*_timer", "*_sht", "*_ops",
1058 "*_probe", "*_probe_one", "*_console", NULL
1062 .fromsec = { TEXT_SECTIONS, NULL },
1063 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1064 .mismatch = TEXT_TO_ANY_EXIT,
1065 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1068 .fromsec = { DATA_SECTIONS, NULL },
1069 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1070 .mismatch = DATA_TO_ANY_EXIT,
1071 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1073 /* Do not reference init code/data from meminit code/data */
1075 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1076 .bad_tosec = { INIT_SECTIONS, NULL },
1077 .mismatch = XXXINIT_TO_SOME_INIT,
1078 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1080 /* Do not reference exit code/data from memexit code/data */
1082 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1083 .bad_tosec = { EXIT_SECTIONS, NULL },
1084 .mismatch = XXXEXIT_TO_SOME_EXIT,
1085 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1087 /* Do not use exit code/data from init code */
1089 .fromsec = { ALL_INIT_SECTIONS, NULL },
1090 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1091 .mismatch = ANY_INIT_TO_ANY_EXIT,
1092 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1094 /* Do not use init code/data from exit code */
1096 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1097 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1098 .mismatch = ANY_EXIT_TO_ANY_INIT,
1099 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1102 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1103 .bad_tosec = { INIT_SECTIONS, NULL },
1104 .mismatch = ANY_INIT_TO_ANY_EXIT,
1105 .symbol_white_list = { NULL },
1107 /* Do not export init/exit functions or data */
1109 .fromsec = { "__ksymtab*", NULL },
1110 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1111 .mismatch = EXPORT_TO_INIT_EXIT,
1112 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1115 .fromsec = { "__ex_table", NULL },
1116 /* If you're adding any new black-listed sections in here, consider
1117 * adding a special 'printer' for them in scripts/check_extable.
1119 .bad_tosec = { ".altinstr_replacement", NULL },
1120 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1121 .mismatch = EXTABLE_TO_NON_TEXT,
1122 .handler = extable_mismatch_handler,
1126 static const struct sectioncheck *section_mismatch(
1127 const char *fromsec, const char *tosec)
1130 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1131 const struct sectioncheck *check = §ioncheck[0];
1134 * The target section could be the SHT_NUL section when we're
1135 * handling relocations to un-resolved symbols, trying to match it
1136 * doesn't make much sense and causes build failures on parisc
1142 for (i = 0; i < elems; i++) {
1143 if (match(fromsec, check->fromsec)) {
1144 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1146 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1155 * Whitelist to allow certain references to pass with no warning.
1158 * If a module parameter is declared __initdata and permissions=0
1159 * then this is legal despite the warning generated.
1160 * We cannot see value of permissions here, so just ignore
1162 * The pattern is identified by:
1163 * tosec = .init.data
1168 * module_param_call() ops can refer to __init set function if permissions=0
1169 * The pattern is identified by:
1170 * tosec = .init.text
1172 * atsym = __param_ops_*
1175 * Many drivers utilise a *driver container with references to
1176 * add, remove, probe functions etc.
1177 * the pattern is identified by:
1178 * tosec = init or exit section
1179 * fromsec = data section
1180 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1181 * *probe_one, *_console, *_timer
1184 * Whitelist all references from .head.text to any init section
1187 * Some symbols belong to init section but still it is ok to reference
1188 * these from non-init sections as these symbols don't have any memory
1189 * allocated for them and symbol address and value are same. So even
1190 * if init section is freed, its ok to reference those symbols.
1191 * For ex. symbols marking the init section boundaries.
1192 * This pattern is identified by
1193 * refsymname = __init_begin, _sinittext, _einittext
1196 * GCC may optimize static inlines when fed constant arg(s) resulting
1197 * in functions like cpumask_empty() -- generating an associated symbol
1198 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1199 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1200 * meaningless section warning. May need to add isra symbols too...
1201 * This pattern is identified by
1202 * tosec = init section
1203 * fromsec = text section
1204 * refsymname = *.constprop.*
1207 * Hide section mismatch warnings for ELF local symbols. The goal
1208 * is to eliminate false positive modpost warnings caused by
1209 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1210 * Autogenerated symbol names bypass modpost's "Pattern 2"
1211 * whitelisting, which relies on pattern-matching against symbol
1212 * names to work. (One situation where gcc can autogenerate ELF
1213 * local symbols is when "-fsection-anchors" is used.)
1215 static int secref_whitelist(const struct sectioncheck *mismatch,
1216 const char *fromsec, const char *fromsym,
1217 const char *tosec, const char *tosym)
1219 /* Check for pattern 1 */
1220 if (match(tosec, init_data_sections) &&
1221 match(fromsec, data_sections) &&
1222 strstarts(fromsym, "__param"))
1225 /* Check for pattern 1a */
1226 if (strcmp(tosec, ".init.text") == 0 &&
1227 match(fromsec, data_sections) &&
1228 strstarts(fromsym, "__param_ops_"))
1231 /* Check for pattern 2 */
1232 if (match(tosec, init_exit_sections) &&
1233 match(fromsec, data_sections) &&
1234 match(fromsym, mismatch->symbol_white_list))
1237 /* Check for pattern 3 */
1238 if (match(fromsec, head_sections) &&
1239 match(tosec, init_sections))
1242 /* Check for pattern 4 */
1243 if (match(tosym, linker_symbols))
1246 /* Check for pattern 5 */
1247 if (match(fromsec, text_sections) &&
1248 match(tosec, init_sections) &&
1249 match(fromsym, optim_symbols))
1252 /* Check for pattern 6 */
1253 if (strstarts(fromsym, ".L"))
1259 static inline int is_arm_mapping_symbol(const char *str)
1261 return str[0] == '$' && strchr("axtd", str[1])
1262 && (str[2] == '\0' || str[2] == '.');
1266 * If there's no name there, ignore it; likewise, ignore it if it's
1267 * one of the magic symbols emitted used by current ARM tools.
1269 * Otherwise if find_symbols_between() returns those symbols, they'll
1270 * fail the whitelist tests and cause lots of false alarms ... fixable
1271 * only by merging __exit and __init sections into __text, bloating
1272 * the kernel (which is especially evil on embedded platforms).
1274 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1276 const char *name = elf->strtab + sym->st_name;
1278 if (!name || !strlen(name))
1280 return !is_arm_mapping_symbol(name);
1284 * Find symbol based on relocation record info.
1285 * In some cases the symbol supplied is a valid symbol so
1286 * return refsym. If st_name != 0 we assume this is a valid symbol.
1287 * In other cases the symbol needs to be looked up in the symbol table
1288 * based on section and address.
1290 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1294 Elf_Sym *near = NULL;
1295 Elf64_Sword distance = 20;
1297 unsigned int relsym_secindex;
1299 if (relsym->st_name != 0)
1302 relsym_secindex = get_secindex(elf, relsym);
1303 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1304 if (get_secindex(elf, sym) != relsym_secindex)
1306 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1308 if (!is_valid_name(elf, sym))
1310 if (sym->st_value == addr)
1312 /* Find a symbol nearby - addr are maybe negative */
1313 d = sym->st_value - addr;
1315 d = addr - sym->st_value;
1321 /* We need a close match */
1329 * Find symbols before or equal addr and after addr - in the section sec.
1330 * If we find two symbols with equal offset prefer one with a valid name.
1331 * The ELF format may have a better way to detect what type of symbol
1332 * it is, but this works for now.
1334 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1338 Elf_Sym *near = NULL;
1339 Elf_Addr distance = ~0;
1341 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1344 if (is_shndx_special(sym->st_shndx))
1346 symsec = sec_name(elf, get_secindex(elf, sym));
1347 if (strcmp(symsec, sec) != 0)
1349 if (!is_valid_name(elf, sym))
1351 if (sym->st_value <= addr) {
1352 if ((addr - sym->st_value) < distance) {
1353 distance = addr - sym->st_value;
1355 } else if ((addr - sym->st_value) == distance) {
1364 * Convert a section name to the function/data attribute
1365 * .init.text => __init
1366 * .memexitconst => __memconst
1369 * The memory of returned value has been allocated on a heap. The user of this
1370 * method should free it after usage.
1372 static char *sec2annotation(const char *s)
1374 if (match(s, init_exit_sections)) {
1375 char *p = NOFAIL(malloc(20));
1382 while (*s && *s != '.')
1387 if (strstr(s, "rodata") != NULL)
1388 strcat(p, "const ");
1389 else if (strstr(s, "data") != NULL)
1395 return NOFAIL(strdup(""));
1399 static int is_function(Elf_Sym *sym)
1402 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1407 static void print_section_list(const char * const list[20])
1409 const char *const *s = list;
1412 fprintf(stderr, "%s", *s);
1415 fprintf(stderr, ", ");
1417 fprintf(stderr, "\n");
1420 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1423 case 0: *name = "variable"; *name_p = ""; break;
1424 case 1: *name = "function"; *name_p = "()"; break;
1425 default: *name = "(unknown reference)"; *name_p = ""; break;
1430 * Print a warning about a section mismatch.
1431 * Try to find symbols near it so user can find it.
1432 * Check whitelist before warning - it may be a false positive.
1434 static void report_sec_mismatch(const char *modname,
1435 const struct sectioncheck *mismatch,
1436 const char *fromsec,
1437 unsigned long long fromaddr,
1438 const char *fromsym,
1440 const char *tosec, const char *tosym,
1443 const char *from, *from_p;
1444 const char *to, *to_p;
1448 sec_mismatch_count++;
1450 get_pretty_name(from_is_func, &from, &from_p);
1451 get_pretty_name(to_is_func, &to, &to_p);
1453 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1454 "to the %s %s:%s%s\n",
1455 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1458 switch (mismatch->mismatch) {
1459 case TEXT_TO_ANY_INIT:
1460 prl_from = sec2annotation(fromsec);
1461 prl_to = sec2annotation(tosec);
1463 "The function %s%s() references\n"
1465 "This is often because %s lacks a %s\n"
1466 "annotation or the annotation of %s is wrong.\n",
1468 to, prl_to, tosym, to_p,
1469 fromsym, prl_to, tosym);
1473 case DATA_TO_ANY_INIT: {
1474 prl_to = sec2annotation(tosec);
1476 "The variable %s references\n"
1478 "If the reference is valid then annotate the\n"
1479 "variable with __init* or __refdata (see linux/init.h) "
1480 "or name the variable:\n",
1481 fromsym, to, prl_to, tosym, to_p);
1482 print_section_list(mismatch->symbol_white_list);
1486 case TEXT_TO_ANY_EXIT:
1487 prl_to = sec2annotation(tosec);
1489 "The function %s() references a %s in an exit section.\n"
1490 "Often the %s %s%s has valid usage outside the exit section\n"
1491 "and the fix is to remove the %sannotation of %s.\n",
1492 fromsym, to, to, tosym, to_p, prl_to, tosym);
1495 case DATA_TO_ANY_EXIT: {
1496 prl_to = sec2annotation(tosec);
1498 "The variable %s references\n"
1500 "If the reference is valid then annotate the\n"
1501 "variable with __exit* (see linux/init.h) or "
1502 "name the variable:\n",
1503 fromsym, to, prl_to, tosym, to_p);
1504 print_section_list(mismatch->symbol_white_list);
1508 case XXXINIT_TO_SOME_INIT:
1509 case XXXEXIT_TO_SOME_EXIT:
1510 prl_from = sec2annotation(fromsec);
1511 prl_to = sec2annotation(tosec);
1513 "The %s %s%s%s references\n"
1515 "If %s is only used by %s then\n"
1516 "annotate %s with a matching annotation.\n",
1517 from, prl_from, fromsym, from_p,
1518 to, prl_to, tosym, to_p,
1519 tosym, fromsym, tosym);
1523 case ANY_INIT_TO_ANY_EXIT:
1524 prl_from = sec2annotation(fromsec);
1525 prl_to = sec2annotation(tosec);
1527 "The %s %s%s%s references\n"
1529 "This is often seen when error handling "
1530 "in the init function\n"
1531 "uses functionality in the exit path.\n"
1532 "The fix is often to remove the %sannotation of\n"
1533 "%s%s so it may be used outside an exit section.\n",
1534 from, prl_from, fromsym, from_p,
1535 to, prl_to, tosym, to_p,
1536 prl_to, tosym, to_p);
1540 case ANY_EXIT_TO_ANY_INIT:
1541 prl_from = sec2annotation(fromsec);
1542 prl_to = sec2annotation(tosec);
1544 "The %s %s%s%s references\n"
1546 "This is often seen when error handling "
1547 "in the exit function\n"
1548 "uses functionality in the init path.\n"
1549 "The fix is often to remove the %sannotation of\n"
1550 "%s%s so it may be used outside an init section.\n",
1551 from, prl_from, fromsym, from_p,
1552 to, prl_to, tosym, to_p,
1553 prl_to, tosym, to_p);
1557 case EXPORT_TO_INIT_EXIT:
1558 prl_to = sec2annotation(tosec);
1560 "The symbol %s is exported and annotated %s\n"
1561 "Fix this by removing the %sannotation of %s "
1562 "or drop the export.\n",
1563 tosym, prl_to, prl_to, tosym);
1566 case EXTABLE_TO_NON_TEXT:
1567 fatal("There's a special handler for this mismatch type, "
1568 "we should never get here.");
1571 fprintf(stderr, "\n");
1574 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1575 const struct sectioncheck* const mismatch,
1576 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1582 const char *fromsym;
1584 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1585 fromsym = sym_name(elf, from);
1587 if (strstarts(fromsym, "reference___initcall"))
1590 tosec = sec_name(elf, get_secindex(elf, sym));
1591 to = find_elf_symbol(elf, r->r_addend, sym);
1592 tosym = sym_name(elf, to);
1594 /* check whitelist - we may ignore it */
1595 if (secref_whitelist(mismatch,
1596 fromsec, fromsym, tosec, tosym)) {
1597 report_sec_mismatch(modname, mismatch,
1598 fromsec, r->r_offset, fromsym,
1599 is_function(from), tosec, tosym,
1604 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1606 if (section_index > elf->num_sections)
1607 fatal("section_index is outside elf->num_sections!\n");
1609 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1613 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1614 * to know the sizeof(struct exception_table_entry) for the target architecture.
1616 static unsigned int extable_entry_size = 0;
1617 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1620 * If we're currently checking the second relocation within __ex_table,
1621 * that relocation offset tells us the offsetof(struct
1622 * exception_table_entry, fixup) which is equal to sizeof(struct
1623 * exception_table_entry) divided by two. We use that to our advantage
1624 * since there's no portable way to get that size as every architecture
1625 * seems to go with different sized types. Not pretty but better than
1626 * hard-coding the size for every architecture..
1628 if (!extable_entry_size)
1629 extable_entry_size = r->r_offset * 2;
1632 static inline bool is_extable_fault_address(Elf_Rela *r)
1635 * extable_entry_size is only discovered after we've handled the
1636 * _second_ relocation in __ex_table, so only abort when we're not
1637 * handling the first reloc and extable_entry_size is zero.
1639 if (r->r_offset && extable_entry_size == 0)
1640 fatal("extable_entry size hasn't been discovered!\n");
1642 return ((r->r_offset == 0) ||
1643 (r->r_offset % extable_entry_size == 0));
1646 #define is_second_extable_reloc(Start, Cur, Sec) \
1647 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1649 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1650 const struct sectioncheck* const mismatch,
1651 Elf_Rela* r, Elf_Sym* sym,
1652 const char* fromsec, const char* tosec)
1654 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1655 const char* fromsym_name = sym_name(elf, fromsym);
1656 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1657 const char* tosym_name = sym_name(elf, tosym);
1658 const char* from_pretty_name;
1659 const char* from_pretty_name_p;
1660 const char* to_pretty_name;
1661 const char* to_pretty_name_p;
1663 get_pretty_name(is_function(fromsym),
1664 &from_pretty_name, &from_pretty_name_p);
1665 get_pretty_name(is_function(tosym),
1666 &to_pretty_name, &to_pretty_name_p);
1668 warn("%s(%s+0x%lx): Section mismatch in reference"
1669 " from the %s %s%s to the %s %s:%s%s\n",
1670 modname, fromsec, (long)r->r_offset, from_pretty_name,
1671 fromsym_name, from_pretty_name_p,
1672 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1674 if (!match(tosec, mismatch->bad_tosec) &&
1675 is_executable_section(elf, get_secindex(elf, sym)))
1677 "The relocation at %s+0x%lx references\n"
1678 "section \"%s\" which is not in the list of\n"
1679 "authorized sections. If you're adding a new section\n"
1680 "and/or if this reference is valid, add \"%s\" to the\n"
1681 "list of authorized sections to jump to on fault.\n"
1682 "This can be achieved by adding \"%s\" to \n"
1683 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1684 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1687 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1688 const struct sectioncheck* const mismatch,
1689 Elf_Rela* r, Elf_Sym* sym,
1690 const char *fromsec)
1692 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1694 sec_mismatch_count++;
1696 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1698 if (match(tosec, mismatch->bad_tosec))
1699 fatal("The relocation at %s+0x%lx references\n"
1700 "section \"%s\" which is black-listed.\n"
1701 "Something is seriously wrong and should be fixed.\n"
1702 "You might get more information about where this is\n"
1703 "coming from by using scripts/check_extable.sh %s\n",
1704 fromsec, (long)r->r_offset, tosec, modname);
1705 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1706 if (is_extable_fault_address(r))
1707 fatal("The relocation at %s+0x%lx references\n"
1708 "section \"%s\" which is not executable, IOW\n"
1709 "it is not possible for the kernel to fault\n"
1710 "at that address. Something is seriously wrong\n"
1711 "and should be fixed.\n",
1712 fromsec, (long)r->r_offset, tosec);
1714 fatal("The relocation at %s+0x%lx references\n"
1715 "section \"%s\" which is not executable, IOW\n"
1716 "the kernel will fault if it ever tries to\n"
1717 "jump to it. Something is seriously wrong\n"
1718 "and should be fixed.\n",
1719 fromsec, (long)r->r_offset, tosec);
1723 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1724 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1726 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1727 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1730 if (mismatch->handler)
1731 mismatch->handler(modname, elf, mismatch,
1734 default_mismatch_handler(modname, elf, mismatch,
1739 static unsigned int *reloc_location(struct elf_info *elf,
1740 Elf_Shdr *sechdr, Elf_Rela *r)
1742 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1745 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1747 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1748 unsigned int *location = reloc_location(elf, sechdr, r);
1752 r->r_addend = TO_NATIVE(*location);
1755 r->r_addend = TO_NATIVE(*location) + 4;
1756 /* For CONFIG_RELOCATABLE=y */
1757 if (elf->hdr->e_type == ET_EXEC)
1758 r->r_addend += r->r_offset;
1765 #define R_ARM_CALL 28
1767 #ifndef R_ARM_JUMP24
1768 #define R_ARM_JUMP24 29
1771 #ifndef R_ARM_THM_CALL
1772 #define R_ARM_THM_CALL 10
1774 #ifndef R_ARM_THM_JUMP24
1775 #define R_ARM_THM_JUMP24 30
1777 #ifndef R_ARM_THM_JUMP19
1778 #define R_ARM_THM_JUMP19 51
1781 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1783 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1787 /* From ARM ABI: (S + A) | T */
1788 r->r_addend = (int)(long)
1789 (elf->symtab_start + ELF_R_SYM(r->r_info));
1794 case R_ARM_THM_CALL:
1795 case R_ARM_THM_JUMP24:
1796 case R_ARM_THM_JUMP19:
1797 /* From ARM ABI: ((S + A) | T) - P */
1798 r->r_addend = (int)(long)(elf->hdr +
1800 (r->r_offset - sechdr->sh_addr));
1808 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1810 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1811 unsigned int *location = reloc_location(elf, sechdr, r);
1814 if (r_typ == R_MIPS_HI16)
1815 return 1; /* skip this */
1816 inst = TO_NATIVE(*location);
1819 r->r_addend = inst & 0xffff;
1822 r->r_addend = (inst & 0x03ffffff) << 2;
1832 #define EM_RISCV 243
1835 #ifndef R_RISCV_SUB32
1836 #define R_RISCV_SUB32 39
1839 static void section_rela(const char *modname, struct elf_info *elf,
1846 const char *fromsec;
1848 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1849 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1851 fromsec = sech_name(elf, sechdr);
1852 fromsec += strlen(".rela");
1853 /* if from section (name) is know good then skip it */
1854 if (match(fromsec, section_white_list))
1857 for (rela = start; rela < stop; rela++) {
1858 r.r_offset = TO_NATIVE(rela->r_offset);
1859 #if KERNEL_ELFCLASS == ELFCLASS64
1860 if (elf->hdr->e_machine == EM_MIPS) {
1862 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1863 r_sym = TO_NATIVE(r_sym);
1864 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1865 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1867 r.r_info = TO_NATIVE(rela->r_info);
1868 r_sym = ELF_R_SYM(r.r_info);
1871 r.r_info = TO_NATIVE(rela->r_info);
1872 r_sym = ELF_R_SYM(r.r_info);
1874 r.r_addend = TO_NATIVE(rela->r_addend);
1875 switch (elf->hdr->e_machine) {
1877 if (!strcmp("__ex_table", fromsec) &&
1878 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1882 sym = elf->symtab_start + r_sym;
1883 /* Skip special sections */
1884 if (is_shndx_special(sym->st_shndx))
1886 if (is_second_extable_reloc(start, rela, fromsec))
1887 find_extable_entry_size(fromsec, &r);
1888 check_section_mismatch(modname, elf, &r, sym, fromsec);
1892 static void section_rel(const char *modname, struct elf_info *elf,
1899 const char *fromsec;
1901 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1902 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1904 fromsec = sech_name(elf, sechdr);
1905 fromsec += strlen(".rel");
1906 /* if from section (name) is know good then skip it */
1907 if (match(fromsec, section_white_list))
1910 for (rel = start; rel < stop; rel++) {
1911 r.r_offset = TO_NATIVE(rel->r_offset);
1912 #if KERNEL_ELFCLASS == ELFCLASS64
1913 if (elf->hdr->e_machine == EM_MIPS) {
1915 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1916 r_sym = TO_NATIVE(r_sym);
1917 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1918 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1920 r.r_info = TO_NATIVE(rel->r_info);
1921 r_sym = ELF_R_SYM(r.r_info);
1924 r.r_info = TO_NATIVE(rel->r_info);
1925 r_sym = ELF_R_SYM(r.r_info);
1928 switch (elf->hdr->e_machine) {
1930 if (addend_386_rel(elf, sechdr, &r))
1934 if (addend_arm_rel(elf, sechdr, &r))
1938 if (addend_mips_rel(elf, sechdr, &r))
1942 sym = elf->symtab_start + r_sym;
1943 /* Skip special sections */
1944 if (is_shndx_special(sym->st_shndx))
1946 if (is_second_extable_reloc(start, rel, fromsec))
1947 find_extable_entry_size(fromsec, &r);
1948 check_section_mismatch(modname, elf, &r, sym, fromsec);
1953 * A module includes a number of sections that are discarded
1954 * either when loaded or when used as built-in.
1955 * For loaded modules all functions marked __init and all data
1956 * marked __initdata will be discarded when the module has been initialized.
1957 * Likewise for modules used built-in the sections marked __exit
1958 * are discarded because __exit marked function are supposed to be called
1959 * only when a module is unloaded which never happens for built-in modules.
1960 * The check_sec_ref() function traverses all relocation records
1961 * to find all references to a section that reference a section that will
1962 * be discarded and warns about it.
1964 static void check_sec_ref(struct module *mod, const char *modname,
1965 struct elf_info *elf)
1968 Elf_Shdr *sechdrs = elf->sechdrs;
1970 /* Walk through all sections */
1971 for (i = 0; i < elf->num_sections; i++) {
1972 check_section(modname, elf, &elf->sechdrs[i]);
1973 /* We want to process only relocation sections and not .init */
1974 if (sechdrs[i].sh_type == SHT_RELA)
1975 section_rela(modname, elf, &elf->sechdrs[i]);
1976 else if (sechdrs[i].sh_type == SHT_REL)
1977 section_rel(modname, elf, &elf->sechdrs[i]);
1981 static char *remove_dot(char *s)
1983 size_t n = strcspn(s, ".");
1986 size_t m = strspn(s + n + 1, "0123456789");
1987 if (m && (s[n + m] == '.' || s[n + m] == 0))
1990 /* strip trailing .prelink */
1991 if (strends(s, ".prelink"))
1992 s[strlen(s) - 8] = '\0';
1997 static void read_symbols(const char *modname)
1999 const char *symname;
2004 struct elf_info info = { };
2007 if (!parse_elf(&info, modname))
2013 /* strip trailing .o */
2014 tmp = NOFAIL(strdup(modname));
2015 tmp[strlen(tmp) - 2] = '\0';
2016 /* strip trailing .prelink */
2017 if (strends(tmp, ".prelink"))
2018 tmp[strlen(tmp) - 8] = '\0';
2019 mod = new_module(tmp);
2023 if (!mod->is_vmlinux) {
2024 license = get_modinfo(&info, "license");
2026 error("missing MODULE_LICENSE() in %s\n", modname);
2028 if (!license_is_gpl_compatible(license)) {
2029 mod->is_gpl_compatible = false;
2032 license = get_next_modinfo(&info, "license", license);
2035 namespace = get_modinfo(&info, "import_ns");
2037 add_namespace(&mod->imported_namespaces, namespace);
2038 namespace = get_next_modinfo(&info, "import_ns",
2043 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2044 symname = remove_dot(info.strtab + sym->st_name);
2046 handle_symbol(mod, &info, sym, symname);
2047 handle_moddevtable(mod, &info, sym, symname);
2050 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2051 symname = remove_dot(info.strtab + sym->st_name);
2053 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2054 if (strstarts(symname, "__kstrtabns_"))
2055 sym_update_namespace(symname + strlen("__kstrtabns_"),
2056 sym_get_data(&info, sym));
2057 if (strstarts(symname, "__crc_"))
2058 handle_modversion(mod, &info, sym,
2059 symname + strlen("__crc_"));
2062 // check for static EXPORT_SYMBOL_* functions && global vars
2063 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2064 unsigned char bind = ELF_ST_BIND(sym->st_info);
2066 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2068 find_symbol(remove_dot(info.strtab +
2072 s->is_static = false;
2076 check_sec_ref(mod, modname, &info);
2078 if (!mod->is_vmlinux) {
2079 version = get_modinfo(&info, "version");
2080 if (version || all_versions)
2081 get_src_version(mod->name, mod->srcversion,
2082 sizeof(mod->srcversion) - 1);
2085 parse_elf_finish(&info);
2087 /* Our trick to get versioning for module struct etc. - it's
2088 * never passed as an argument to an exported function, so
2089 * the automatic versioning doesn't pick it up, but it's really
2090 * important anyhow */
2092 sym_add_unresolved("module_layout", mod, false);
2095 static void read_symbols_from_files(const char *filename)
2098 char fname[PATH_MAX];
2100 if (strcmp(filename, "-") != 0) {
2101 in = fopen(filename, "r");
2103 fatal("Can't open filenames file %s: %m", filename);
2106 while (fgets(fname, PATH_MAX, in) != NULL) {
2107 if (strends(fname, "\n"))
2108 fname[strlen(fname)-1] = '\0';
2109 read_symbols(fname);
2118 /* We first write the generated file into memory using the
2119 * following helper, then compare to the file on disk and
2120 * only update the later if anything changed */
2122 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2123 const char *fmt, ...)
2130 len = vsnprintf(tmp, SZ, fmt, ap);
2131 buf_write(buf, tmp, len);
2135 void buf_write(struct buffer *buf, const char *s, int len)
2137 if (buf->size - buf->pos < len) {
2138 buf->size += len + SZ;
2139 buf->p = NOFAIL(realloc(buf->p, buf->size));
2141 strncpy(buf->p + buf->pos, s, len);
2145 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2149 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2153 case export_unknown:
2159 static void check_exports(struct module *mod)
2161 struct symbol *s, *exp;
2163 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2164 const char *basename;
2165 exp = find_symbol(s->name);
2167 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2168 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2169 "\"%s\" [%s.ko] undefined!\n",
2170 s->name, mod->name);
2173 if (exp->module == mod) {
2174 error("\"%s\" [%s.ko] was exported without definition\n",
2175 s->name, mod->name);
2179 s->module = exp->module;
2180 s->crc_valid = exp->crc_valid;
2183 basename = strrchr(mod->name, '/');
2187 basename = mod->name;
2189 if (exp->namespace &&
2190 !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
2191 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2192 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2193 basename, exp->name, exp->namespace);
2194 add_namespace(&mod->missing_namespaces, exp->namespace);
2197 if (!mod->is_gpl_compatible)
2198 check_for_gpl_usage(exp->export, basename, exp->name);
2202 static void check_modname_len(struct module *mod)
2204 const char *mod_name;
2206 mod_name = strrchr(mod->name, '/');
2207 if (mod_name == NULL)
2208 mod_name = mod->name;
2211 if (strlen(mod_name) >= MODULE_NAME_LEN)
2212 error("module name is too long [%s.ko]\n", mod->name);
2216 * Header for the generated file
2218 static void add_header(struct buffer *b, struct module *mod)
2220 buf_printf(b, "#include <linux/module.h>\n");
2222 * Include build-salt.h after module.h in order to
2223 * inherit the definitions.
2225 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2226 buf_printf(b, "#include <linux/build-salt.h>\n");
2227 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2228 buf_printf(b, "#include <linux/vermagic.h>\n");
2229 buf_printf(b, "#include <linux/compiler.h>\n");
2230 buf_printf(b, "\n");
2231 buf_printf(b, "BUILD_SALT;\n");
2232 buf_printf(b, "BUILD_LTO_INFO;\n");
2233 buf_printf(b, "\n");
2234 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2235 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2236 buf_printf(b, "\n");
2237 buf_printf(b, "__visible struct module __this_module\n");
2238 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2239 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2241 buf_printf(b, "\t.init = init_module,\n");
2242 if (mod->has_cleanup)
2243 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2244 "\t.exit = cleanup_module,\n"
2246 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2247 buf_printf(b, "};\n");
2250 static void add_intree_flag(struct buffer *b, int is_intree)
2253 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2256 /* Cannot check for assembler */
2257 static void add_retpoline(struct buffer *b)
2259 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2260 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2261 buf_printf(b, "#endif\n");
2264 static void add_staging_flag(struct buffer *b, const char *name)
2266 if (strstarts(name, "drivers/staging"))
2267 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2271 * Record CRCs for unresolved symbols
2273 static void add_versions(struct buffer *b, struct module *mod)
2280 buf_printf(b, "\n");
2281 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2282 buf_printf(b, "__used __section(\"__versions\") = {\n");
2284 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2287 if (!s->crc_valid) {
2288 warn("\"%s\" [%s.ko] has no CRC!\n",
2289 s->name, mod->name);
2292 if (strlen(s->name) >= MODULE_NAME_LEN) {
2293 error("too long symbol \"%s\" [%s.ko]\n",
2294 s->name, mod->name);
2297 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2301 buf_printf(b, "};\n");
2304 static void add_depends(struct buffer *b, struct module *mod)
2309 /* Clear ->seen flag of modules that own symbols needed by this. */
2310 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2312 s->module->seen = s->module->is_vmlinux;
2315 buf_printf(b, "\n");
2316 buf_printf(b, "MODULE_INFO(depends, \"");
2317 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2322 if (s->module->seen)
2325 s->module->seen = true;
2326 p = strrchr(s->module->name, '/');
2330 p = s->module->name;
2331 buf_printf(b, "%s%s", first ? "" : ",", p);
2334 buf_printf(b, "\");\n");
2337 static void add_srcversion(struct buffer *b, struct module *mod)
2339 if (mod->srcversion[0]) {
2340 buf_printf(b, "\n");
2341 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2346 static void write_buf(struct buffer *b, const char *fname)
2353 file = fopen(fname, "w");
2358 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2362 if (fclose(file) != 0) {
2368 static void write_if_changed(struct buffer *b, const char *fname)
2374 file = fopen(fname, "r");
2378 if (fstat(fileno(file), &st) < 0)
2381 if (st.st_size != b->pos)
2384 tmp = NOFAIL(malloc(b->pos));
2385 if (fread(tmp, 1, b->pos, file) != b->pos)
2388 if (memcmp(tmp, b->p, b->pos) != 0)
2400 write_buf(b, fname);
2403 /* parse Module.symvers file. line format:
2404 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2406 static void read_dump(const char *fname)
2408 char *buf, *pos, *line;
2410 buf = read_text_file(fname);
2412 /* No symbol versions, silently ignore */
2417 while ((line = get_line(&pos))) {
2418 char *symname, *namespace, *modname, *d, *export;
2423 if (!(symname = strchr(line, '\t')))
2426 if (!(modname = strchr(symname, '\t')))
2429 if (!(export = strchr(modname, '\t')))
2432 if (!(namespace = strchr(export, '\t')))
2434 *namespace++ = '\0';
2436 crc = strtoul(line, &d, 16);
2437 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2439 mod = find_module(modname);
2441 mod = new_module(modname);
2442 mod->from_dump = true;
2444 s = sym_add_exported(symname, mod, export_no(export));
2445 s->is_static = false;
2446 sym_set_crc(symname, crc);
2447 sym_update_namespace(symname, namespace);
2453 fatal("parse error in symbol dump file\n");
2456 static void write_dump(const char *fname)
2458 struct buffer buf = { };
2459 struct symbol *symbol;
2460 const char *namespace;
2463 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2464 symbol = symbolhash[n];
2466 if (!symbol->module->from_dump) {
2467 namespace = symbol->namespace;
2468 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2469 symbol->crc, symbol->name,
2470 symbol->module->name,
2471 export_str(symbol->export),
2472 namespace ? namespace : "");
2474 symbol = symbol->next;
2477 write_buf(&buf, fname);
2481 static void write_namespace_deps_files(const char *fname)
2484 struct namespace_list *ns;
2485 struct buffer ns_deps_buf = {};
2487 list_for_each_entry(mod, &modules, list) {
2489 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2492 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2494 list_for_each_entry(ns, &mod->missing_namespaces, list)
2495 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2497 buf_printf(&ns_deps_buf, "\n");
2500 write_if_changed(&ns_deps_buf, fname);
2501 free(ns_deps_buf.p);
2505 struct list_head list;
2509 int main(int argc, char **argv)
2512 struct buffer buf = { };
2513 char *missing_namespace_deps = NULL;
2514 char *dump_write = NULL, *files_source = NULL;
2517 LIST_HEAD(dump_lists);
2518 struct dump_list *dl, *dl2;
2520 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2523 external_module = true;
2526 dl = NOFAIL(malloc(sizeof(*dl)));
2528 list_add_tail(&dl->list, &dump_lists);
2534 ignore_missing_files = true;
2537 dump_write = optarg;
2540 all_versions = true;
2543 files_source = optarg;
2546 warn_unresolved = true;
2549 sec_mismatch_warn_only = false;
2552 allow_missing_ns_imports = true;
2555 missing_namespace_deps = optarg;
2562 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2563 read_dump(dl->file);
2564 list_del(&dl->list);
2568 while (optind < argc)
2569 read_symbols(argv[optind++]);
2572 read_symbols_from_files(files_source);
2574 list_for_each_entry(mod, &modules, list) {
2575 char fname[PATH_MAX];
2578 if (mod->is_vmlinux || mod->from_dump)
2583 check_modname_len(mod);
2586 add_header(&buf, mod);
2587 add_intree_flag(&buf, !external_module);
2588 add_retpoline(&buf);
2589 add_staging_flag(&buf, mod->name);
2590 add_versions(&buf, mod);
2591 add_depends(&buf, mod);
2592 add_moddevtable(&buf, mod);
2593 add_srcversion(&buf, mod);
2595 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2596 if (ret >= sizeof(fname)) {
2597 error("%s: too long path was truncated\n", fname);
2601 write_if_changed(&buf, fname);
2604 if (missing_namespace_deps)
2605 write_namespace_deps_files(missing_namespace_deps);
2608 write_dump(dump_write);
2609 if (sec_mismatch_count && !sec_mismatch_warn_only)
2610 error("Section mismatches detected.\n"
2611 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2612 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2615 for (s = symbolhash[n]; s; s = s->next) {
2617 error("\"%s\" [%s] is a static %s\n",
2618 s->name, s->module->name,
2619 export_str(s->export));
2623 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2624 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2625 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2629 return error_occurred ? 1 : 0;