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->exported_symbols);
189 INIT_LIST_HEAD(&mod->unresolved_symbols);
190 INIT_LIST_HEAD(&mod->missing_namespaces);
191 INIT_LIST_HEAD(&mod->imported_namespaces);
193 strcpy(mod->name, modname);
194 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
197 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
198 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
199 * modpost will exit wiht error anyway.
201 mod->is_gpl_compatible = true;
203 list_add_tail(&mod->list, &modules);
208 /* A hash of all exported symbols,
209 * struct symbol is also used for lists of unresolved symbols */
211 #define SYMBOL_HASH_SIZE 1024
215 struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
216 struct module *module;
221 bool is_static; /* true if symbol is not global */
222 enum export export; /* Type of export */
226 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
228 /* This is based on the hash algorithm from gdbm, via tdb */
229 static inline unsigned int tdb_hash(const char *name)
231 unsigned value; /* Used to compute the hash value. */
232 unsigned i; /* Used to cycle through random values. */
234 /* Set the initial value from the key size. */
235 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
236 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
238 return (1103515243 * value + 12345);
242 * Allocate a new symbols for use in the hash of exported symbols or
243 * the list of unresolved symbols per module
245 static struct symbol *alloc_symbol(const char *name)
247 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
249 memset(s, 0, sizeof(*s));
250 strcpy(s->name, name);
255 /* For the hash of exported symbols */
256 static void hash_add_symbol(struct symbol *sym)
260 hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
261 sym->next = symbolhash[hash];
262 symbolhash[hash] = sym;
265 static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
269 sym = alloc_symbol(name);
272 list_add_tail(&sym->list, &mod->unresolved_symbols);
275 static struct symbol *find_symbol(const char *name)
279 /* For our purposes, .foo matches foo. PPC64 needs this. */
283 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
284 if (strcmp(s->name, name) == 0)
290 struct namespace_list {
291 struct list_head list;
295 static bool contains_namespace(struct list_head *head, const char *namespace)
297 struct namespace_list *list;
299 list_for_each_entry(list, head, list) {
300 if (!strcmp(list->namespace, namespace))
307 static void add_namespace(struct list_head *head, const char *namespace)
309 struct namespace_list *ns_entry;
311 if (!contains_namespace(head, namespace)) {
312 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
313 strlen(namespace) + 1));
314 strcpy(ns_entry->namespace, namespace);
315 list_add_tail(&ns_entry->list, head);
319 static const struct {
323 { .str = "EXPORT_SYMBOL", .export = export_plain },
324 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
325 { .str = "(unknown)", .export = export_unknown },
329 static const char *export_str(enum export ex)
331 return export_list[ex].str;
334 static enum export export_no(const char *s)
339 return export_unknown;
340 for (i = 0; export_list[i].export != export_unknown; i++) {
341 if (strcmp(export_list[i].str, s) == 0)
342 return export_list[i].export;
344 return export_unknown;
347 static void *sym_get_data_by_offset(const struct elf_info *info,
348 unsigned int secindex, unsigned long offset)
350 Elf_Shdr *sechdr = &info->sechdrs[secindex];
352 if (info->hdr->e_type != ET_REL)
353 offset -= sechdr->sh_addr;
355 return (void *)info->hdr + sechdr->sh_offset + offset;
358 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
360 return sym_get_data_by_offset(info, get_secindex(info, sym),
364 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
366 return sym_get_data_by_offset(info, info->secindex_strings,
370 static const char *sec_name(const struct elf_info *info, int secindex)
372 return sech_name(info, &info->sechdrs[secindex]);
375 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
377 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
379 const char *secname = sec_name(elf, sec);
381 if (strstarts(secname, "___ksymtab+"))
383 else if (strstarts(secname, "___ksymtab_gpl+"))
386 return export_unknown;
389 static void sym_update_namespace(const char *symname, const char *namespace)
391 struct symbol *s = find_symbol(symname);
394 * That symbol should have been created earlier and thus this is
395 * actually an assertion.
398 error("Could not update namespace(%s) for symbol %s\n",
404 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
407 static struct symbol *sym_add_exported(const char *name, struct module *mod,
410 struct symbol *s = find_symbol(name);
412 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
413 error("%s: '%s' exported twice. Previous export was in %s%s\n",
414 mod->name, name, s->module->name,
415 s->module->is_vmlinux ? "" : ".ko");
418 s = alloc_symbol(name);
421 list_add_tail(&s->list, &mod->exported_symbols);
427 static void sym_set_crc(const char *name, unsigned int crc)
429 struct symbol *s = find_symbol(name);
432 * Ignore stand-alone __crc_*, which might be auto-generated symbols
433 * such as __*_veneer in ARM ELF.
442 static void *grab_file(const char *filename, size_t *size)
445 void *map = MAP_FAILED;
448 fd = open(filename, O_RDONLY);
455 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
459 if (map == MAP_FAILED)
464 static void release_file(void *file, size_t size)
469 static int parse_elf(struct elf_info *info, const char *filename)
475 const char *secstrings;
476 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
478 hdr = grab_file(filename, &info->size);
480 if (ignore_missing_files) {
481 fprintf(stderr, "%s: %s (ignored)\n", filename,
489 if (info->size < sizeof(*hdr)) {
490 /* file too small, assume this is an empty .o file */
493 /* Is this a valid ELF file? */
494 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
495 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
496 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
497 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
498 /* Not an ELF file - silently ignore it */
501 /* Fix endianness in ELF header */
502 hdr->e_type = TO_NATIVE(hdr->e_type);
503 hdr->e_machine = TO_NATIVE(hdr->e_machine);
504 hdr->e_version = TO_NATIVE(hdr->e_version);
505 hdr->e_entry = TO_NATIVE(hdr->e_entry);
506 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
507 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
508 hdr->e_flags = TO_NATIVE(hdr->e_flags);
509 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
510 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
511 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
512 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
513 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
514 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
515 sechdrs = (void *)hdr + hdr->e_shoff;
516 info->sechdrs = sechdrs;
518 /* Check if file offset is correct */
519 if (hdr->e_shoff > info->size) {
520 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
521 (unsigned long)hdr->e_shoff, filename, info->size);
525 if (hdr->e_shnum == SHN_UNDEF) {
527 * There are more than 64k sections,
528 * read count from .sh_size.
530 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
533 info->num_sections = hdr->e_shnum;
535 if (hdr->e_shstrndx == SHN_XINDEX) {
536 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
539 info->secindex_strings = hdr->e_shstrndx;
542 /* Fix endianness in section headers */
543 for (i = 0; i < info->num_sections; i++) {
544 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
545 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
546 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
547 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
548 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
549 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
550 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
551 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
552 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
553 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
555 /* Find symbol table. */
556 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
557 for (i = 1; i < info->num_sections; i++) {
559 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
561 if (!nobits && sechdrs[i].sh_offset > info->size) {
562 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
563 "sizeof(*hrd)=%zu\n", filename,
564 (unsigned long)sechdrs[i].sh_offset,
568 secname = secstrings + sechdrs[i].sh_name;
569 if (strcmp(secname, ".modinfo") == 0) {
571 fatal("%s has NOBITS .modinfo\n", filename);
572 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
573 info->modinfo_len = sechdrs[i].sh_size;
576 if (sechdrs[i].sh_type == SHT_SYMTAB) {
577 unsigned int sh_link_idx;
579 info->symtab_start = (void *)hdr +
580 sechdrs[i].sh_offset;
581 info->symtab_stop = (void *)hdr +
582 sechdrs[i].sh_offset + sechdrs[i].sh_size;
583 sh_link_idx = sechdrs[i].sh_link;
584 info->strtab = (void *)hdr +
585 sechdrs[sh_link_idx].sh_offset;
588 /* 32bit section no. table? ("more than 64k sections") */
589 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
590 symtab_shndx_idx = i;
591 info->symtab_shndx_start = (void *)hdr +
592 sechdrs[i].sh_offset;
593 info->symtab_shndx_stop = (void *)hdr +
594 sechdrs[i].sh_offset + sechdrs[i].sh_size;
597 if (!info->symtab_start)
598 fatal("%s has no symtab?\n", filename);
600 /* Fix endianness in symbols */
601 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
602 sym->st_shndx = TO_NATIVE(sym->st_shndx);
603 sym->st_name = TO_NATIVE(sym->st_name);
604 sym->st_value = TO_NATIVE(sym->st_value);
605 sym->st_size = TO_NATIVE(sym->st_size);
608 if (symtab_shndx_idx != ~0U) {
610 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
611 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
612 filename, sechdrs[symtab_shndx_idx].sh_link,
615 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
623 static void parse_elf_finish(struct elf_info *info)
625 release_file(info->hdr, info->size);
628 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
630 /* ignore __this_module, it will be resolved shortly */
631 if (strcmp(symname, "__this_module") == 0)
633 /* ignore global offset table */
634 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
636 if (info->hdr->e_machine == EM_PPC)
637 /* Special register function linked on all modules during final link of .ko */
638 if (strstarts(symname, "_restgpr_") ||
639 strstarts(symname, "_savegpr_") ||
640 strstarts(symname, "_rest32gpr_") ||
641 strstarts(symname, "_save32gpr_") ||
642 strstarts(symname, "_restvr_") ||
643 strstarts(symname, "_savevr_"))
645 if (info->hdr->e_machine == EM_PPC64)
646 /* Special register function linked on all modules during final link of .ko */
647 if (strstarts(symname, "_restgpr0_") ||
648 strstarts(symname, "_savegpr0_") ||
649 strstarts(symname, "_restvr_") ||
650 strstarts(symname, "_savevr_") ||
651 strcmp(symname, ".TOC.") == 0)
654 if (info->hdr->e_machine == EM_S390)
655 /* Expoline thunks are linked on all kernel modules during final link of .ko */
656 if (strstarts(symname, "__s390_indirect_jump_r"))
658 /* Do not ignore this symbol */
662 static void handle_modversion(const struct module *mod,
663 const struct elf_info *info,
664 const Elf_Sym *sym, const char *symname)
668 if (sym->st_shndx == SHN_UNDEF) {
669 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
670 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
671 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
677 if (sym->st_shndx == SHN_ABS) {
682 /* symbol points to the CRC in the ELF object */
683 crcp = sym_get_data(info, sym);
684 crc = TO_NATIVE(*crcp);
686 sym_set_crc(symname, crc);
689 static void handle_symbol(struct module *mod, struct elf_info *info,
690 const Elf_Sym *sym, const char *symname)
694 switch (sym->st_shndx) {
696 if (strstarts(symname, "__gnu_lto_")) {
697 /* Should warn here, but modpost runs before the linker */
699 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
702 /* undefined symbol */
703 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
704 ELF_ST_BIND(sym->st_info) != STB_WEAK)
706 if (ignore_undef_symbol(info, symname))
708 if (info->hdr->e_machine == EM_SPARC ||
709 info->hdr->e_machine == EM_SPARCV9) {
710 /* Ignore register directives. */
711 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
713 if (symname[0] == '.') {
714 char *munged = NOFAIL(strdup(symname));
716 munged[1] = toupper(munged[1]);
721 sym_add_unresolved(symname, mod,
722 ELF_ST_BIND(sym->st_info) == STB_WEAK);
725 /* All exported symbols */
726 if (strstarts(symname, "__ksymtab_")) {
729 name = symname + strlen("__ksymtab_");
730 export = export_from_secname(info,
731 get_secindex(info, sym));
732 sym_add_exported(name, mod, export);
734 if (strcmp(symname, "init_module") == 0)
735 mod->has_init = true;
736 if (strcmp(symname, "cleanup_module") == 0)
737 mod->has_cleanup = true;
743 * Parse tag=value strings from .modinfo section
745 static char *next_string(char *string, unsigned long *secsize)
747 /* Skip non-zero chars */
750 if ((*secsize)-- <= 1)
754 /* Skip any zero padding. */
757 if ((*secsize)-- <= 1)
763 static char *get_next_modinfo(struct elf_info *info, const char *tag,
767 unsigned int taglen = strlen(tag);
768 char *modinfo = info->modinfo;
769 unsigned long size = info->modinfo_len;
772 size -= prev - modinfo;
773 modinfo = next_string(prev, &size);
776 for (p = modinfo; p; p = next_string(p, &size)) {
777 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
778 return p + taglen + 1;
783 static char *get_modinfo(struct elf_info *info, const char *tag)
786 return get_next_modinfo(info, tag, NULL);
790 * Test if string s ends in string sub
793 static int strrcmp(const char *s, const char *sub)
801 sublen = strlen(sub);
803 if ((slen == 0) || (sublen == 0))
809 return memcmp(s + slen - sublen, sub, sublen);
812 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
815 return elf->strtab + sym->st_name;
820 /* The pattern is an array of simple patterns.
821 * "foo" will match an exact string equal to "foo"
822 * "*foo" will match a string that ends with "foo"
823 * "foo*" will match a string that begins with "foo"
824 * "*foo*" will match a string that contains "foo"
826 static int match(const char *sym, const char * const pat[])
833 endp = p + strlen(p) - 1;
836 if (*p == '*' && *endp == '*') {
837 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
838 char *here = strstr(sym, bare);
845 else if (*p == '*') {
846 if (strrcmp(sym, p + 1) == 0)
850 else if (*endp == '*') {
851 if (strncmp(sym, p, strlen(p) - 1) == 0)
856 if (strcmp(p, sym) == 0)
864 /* sections that we do not want to do full section mismatch check on */
865 static const char *const section_white_list[] =
869 ".cranges", /* sh64 */
870 ".zdebug*", /* Compressed debug sections. */
871 ".GCC.command.line", /* record-gcc-switches */
872 ".mdebug*", /* alpha, score, mips etc. */
873 ".pdr", /* alpha, score, mips etc. */
878 ".xt.prop", /* xtensa */
879 ".xt.lit", /* xtensa */
880 ".arcextmap*", /* arc */
881 ".gnu.linkonce.arcext*", /* arc : modules */
882 ".cmem*", /* EZchip */
883 ".fmt_slot*", /* EZchip */
890 * This is used to find sections missing the SHF_ALLOC flag.
891 * The cause of this is often a section specified in assembler
892 * without "ax" / "aw".
894 static void check_section(const char *modname, struct elf_info *elf,
897 const char *sec = sech_name(elf, sechdr);
899 if (sechdr->sh_type == SHT_PROGBITS &&
900 !(sechdr->sh_flags & SHF_ALLOC) &&
901 !match(sec, section_white_list)) {
902 warn("%s (%s): unexpected non-allocatable section.\n"
903 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
904 "Note that for example <linux/init.h> contains\n"
905 "section definitions for use in .S files.\n\n",
912 #define ALL_INIT_DATA_SECTIONS \
913 ".init.setup", ".init.rodata", ".meminit.rodata", \
914 ".init.data", ".meminit.data"
915 #define ALL_EXIT_DATA_SECTIONS \
916 ".exit.data", ".memexit.data"
918 #define ALL_INIT_TEXT_SECTIONS \
919 ".init.text", ".meminit.text"
920 #define ALL_EXIT_TEXT_SECTIONS \
921 ".exit.text", ".memexit.text"
923 #define ALL_PCI_INIT_SECTIONS \
924 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
925 ".pci_fixup_enable", ".pci_fixup_resume", \
926 ".pci_fixup_resume_early", ".pci_fixup_suspend"
928 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
929 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
931 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
932 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
934 #define DATA_SECTIONS ".data", ".data.rel"
935 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
936 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
937 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
938 ".fixup", ".entry.text", ".exception.text", ".text.*", \
939 ".coldtext", ".softirqentry.text"
941 #define INIT_SECTIONS ".init.*"
942 #define MEM_INIT_SECTIONS ".meminit.*"
944 #define EXIT_SECTIONS ".exit.*"
945 #define MEM_EXIT_SECTIONS ".memexit.*"
947 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
948 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
950 /* init data sections */
951 static const char *const init_data_sections[] =
952 { ALL_INIT_DATA_SECTIONS, NULL };
954 /* all init sections */
955 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
957 /* All init and exit sections (code + data) */
958 static const char *const init_exit_sections[] =
959 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
961 /* all text sections */
962 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
965 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
968 /* symbols in .data that may refer to init/exit sections */
969 #define DEFAULT_SYMBOL_WHITE_LIST \
971 "*_template", /* scsi uses *_template a lot */ \
972 "*_timer", /* arm uses ops structures named _timer a lot */ \
973 "*_sht", /* scsi also used *_sht to some extent */ \
979 static const char *const head_sections[] = { ".head.text*", NULL };
980 static const char *const linker_symbols[] =
981 { "__init_begin", "_sinittext", "_einittext", NULL };
982 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
989 XXXINIT_TO_SOME_INIT,
990 XXXEXIT_TO_SOME_EXIT,
991 ANY_INIT_TO_ANY_EXIT,
992 ANY_EXIT_TO_ANY_INIT,
998 * Describe how to match sections on different criteria:
1000 * @fromsec: Array of sections to be matched.
1002 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1003 * this array is forbidden (black-list). Can be empty.
1005 * @good_tosec: Relocations applied to a section in @fromsec must be
1006 * targeting sections in this array (white-list). Can be empty.
1008 * @mismatch: Type of mismatch.
1010 * @symbol_white_list: Do not match a relocation to a symbol in this list
1011 * even if it is targeting a section in @bad_to_sec.
1013 * @handler: Specific handler to call when a match is found. If NULL,
1014 * default_mismatch_handler() will be called.
1017 struct sectioncheck {
1018 const char *fromsec[20];
1019 const char *bad_tosec[20];
1020 const char *good_tosec[20];
1021 enum mismatch mismatch;
1022 const char *symbol_white_list[20];
1023 void (*handler)(const char *modname, struct elf_info *elf,
1024 const struct sectioncheck* const mismatch,
1025 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1029 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1030 const struct sectioncheck* const mismatch,
1031 Elf_Rela *r, Elf_Sym *sym,
1032 const char *fromsec);
1034 static const struct sectioncheck sectioncheck[] = {
1035 /* Do not reference init/exit code/data from
1036 * normal code and data
1039 .fromsec = { TEXT_SECTIONS, NULL },
1040 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1041 .mismatch = TEXT_TO_ANY_INIT,
1042 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1045 .fromsec = { DATA_SECTIONS, NULL },
1046 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1047 .mismatch = DATA_TO_ANY_INIT,
1048 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1051 .fromsec = { DATA_SECTIONS, NULL },
1052 .bad_tosec = { INIT_SECTIONS, NULL },
1053 .mismatch = DATA_TO_ANY_INIT,
1054 .symbol_white_list = {
1055 "*_template", "*_timer", "*_sht", "*_ops",
1056 "*_probe", "*_probe_one", "*_console", NULL
1060 .fromsec = { TEXT_SECTIONS, NULL },
1061 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1062 .mismatch = TEXT_TO_ANY_EXIT,
1063 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1066 .fromsec = { DATA_SECTIONS, NULL },
1067 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1068 .mismatch = DATA_TO_ANY_EXIT,
1069 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1071 /* Do not reference init code/data from meminit code/data */
1073 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1074 .bad_tosec = { INIT_SECTIONS, NULL },
1075 .mismatch = XXXINIT_TO_SOME_INIT,
1076 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1078 /* Do not reference exit code/data from memexit code/data */
1080 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1081 .bad_tosec = { EXIT_SECTIONS, NULL },
1082 .mismatch = XXXEXIT_TO_SOME_EXIT,
1083 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1085 /* Do not use exit code/data from init code */
1087 .fromsec = { ALL_INIT_SECTIONS, NULL },
1088 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1089 .mismatch = ANY_INIT_TO_ANY_EXIT,
1090 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1092 /* Do not use init code/data from exit code */
1094 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1095 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1096 .mismatch = ANY_EXIT_TO_ANY_INIT,
1097 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1100 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1101 .bad_tosec = { INIT_SECTIONS, NULL },
1102 .mismatch = ANY_INIT_TO_ANY_EXIT,
1103 .symbol_white_list = { NULL },
1105 /* Do not export init/exit functions or data */
1107 .fromsec = { "__ksymtab*", NULL },
1108 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1109 .mismatch = EXPORT_TO_INIT_EXIT,
1110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1113 .fromsec = { "__ex_table", NULL },
1114 /* If you're adding any new black-listed sections in here, consider
1115 * adding a special 'printer' for them in scripts/check_extable.
1117 .bad_tosec = { ".altinstr_replacement", NULL },
1118 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1119 .mismatch = EXTABLE_TO_NON_TEXT,
1120 .handler = extable_mismatch_handler,
1124 static const struct sectioncheck *section_mismatch(
1125 const char *fromsec, const char *tosec)
1128 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1129 const struct sectioncheck *check = §ioncheck[0];
1132 * The target section could be the SHT_NUL section when we're
1133 * handling relocations to un-resolved symbols, trying to match it
1134 * doesn't make much sense and causes build failures on parisc
1140 for (i = 0; i < elems; i++) {
1141 if (match(fromsec, check->fromsec)) {
1142 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1144 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1153 * Whitelist to allow certain references to pass with no warning.
1156 * If a module parameter is declared __initdata and permissions=0
1157 * then this is legal despite the warning generated.
1158 * We cannot see value of permissions here, so just ignore
1160 * The pattern is identified by:
1161 * tosec = .init.data
1166 * module_param_call() ops can refer to __init set function if permissions=0
1167 * The pattern is identified by:
1168 * tosec = .init.text
1170 * atsym = __param_ops_*
1173 * Many drivers utilise a *driver container with references to
1174 * add, remove, probe functions etc.
1175 * the pattern is identified by:
1176 * tosec = init or exit section
1177 * fromsec = data section
1178 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1179 * *probe_one, *_console, *_timer
1182 * Whitelist all references from .head.text to any init section
1185 * Some symbols belong to init section but still it is ok to reference
1186 * these from non-init sections as these symbols don't have any memory
1187 * allocated for them and symbol address and value are same. So even
1188 * if init section is freed, its ok to reference those symbols.
1189 * For ex. symbols marking the init section boundaries.
1190 * This pattern is identified by
1191 * refsymname = __init_begin, _sinittext, _einittext
1194 * GCC may optimize static inlines when fed constant arg(s) resulting
1195 * in functions like cpumask_empty() -- generating an associated symbol
1196 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1197 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1198 * meaningless section warning. May need to add isra symbols too...
1199 * This pattern is identified by
1200 * tosec = init section
1201 * fromsec = text section
1202 * refsymname = *.constprop.*
1205 * Hide section mismatch warnings for ELF local symbols. The goal
1206 * is to eliminate false positive modpost warnings caused by
1207 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1208 * Autogenerated symbol names bypass modpost's "Pattern 2"
1209 * whitelisting, which relies on pattern-matching against symbol
1210 * names to work. (One situation where gcc can autogenerate ELF
1211 * local symbols is when "-fsection-anchors" is used.)
1213 static int secref_whitelist(const struct sectioncheck *mismatch,
1214 const char *fromsec, const char *fromsym,
1215 const char *tosec, const char *tosym)
1217 /* Check for pattern 1 */
1218 if (match(tosec, init_data_sections) &&
1219 match(fromsec, data_sections) &&
1220 strstarts(fromsym, "__param"))
1223 /* Check for pattern 1a */
1224 if (strcmp(tosec, ".init.text") == 0 &&
1225 match(fromsec, data_sections) &&
1226 strstarts(fromsym, "__param_ops_"))
1229 /* Check for pattern 2 */
1230 if (match(tosec, init_exit_sections) &&
1231 match(fromsec, data_sections) &&
1232 match(fromsym, mismatch->symbol_white_list))
1235 /* Check for pattern 3 */
1236 if (match(fromsec, head_sections) &&
1237 match(tosec, init_sections))
1240 /* Check for pattern 4 */
1241 if (match(tosym, linker_symbols))
1244 /* Check for pattern 5 */
1245 if (match(fromsec, text_sections) &&
1246 match(tosec, init_sections) &&
1247 match(fromsym, optim_symbols))
1250 /* Check for pattern 6 */
1251 if (strstarts(fromsym, ".L"))
1257 static inline int is_arm_mapping_symbol(const char *str)
1259 return str[0] == '$' && strchr("axtd", str[1])
1260 && (str[2] == '\0' || str[2] == '.');
1264 * If there's no name there, ignore it; likewise, ignore it if it's
1265 * one of the magic symbols emitted used by current ARM tools.
1267 * Otherwise if find_symbols_between() returns those symbols, they'll
1268 * fail the whitelist tests and cause lots of false alarms ... fixable
1269 * only by merging __exit and __init sections into __text, bloating
1270 * the kernel (which is especially evil on embedded platforms).
1272 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1274 const char *name = elf->strtab + sym->st_name;
1276 if (!name || !strlen(name))
1278 return !is_arm_mapping_symbol(name);
1282 * Find symbol based on relocation record info.
1283 * In some cases the symbol supplied is a valid symbol so
1284 * return refsym. If st_name != 0 we assume this is a valid symbol.
1285 * In other cases the symbol needs to be looked up in the symbol table
1286 * based on section and address.
1288 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1292 Elf_Sym *near = NULL;
1293 Elf64_Sword distance = 20;
1295 unsigned int relsym_secindex;
1297 if (relsym->st_name != 0)
1300 relsym_secindex = get_secindex(elf, relsym);
1301 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1302 if (get_secindex(elf, sym) != relsym_secindex)
1304 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1306 if (!is_valid_name(elf, sym))
1308 if (sym->st_value == addr)
1310 /* Find a symbol nearby - addr are maybe negative */
1311 d = sym->st_value - addr;
1313 d = addr - sym->st_value;
1319 /* We need a close match */
1327 * Find symbols before or equal addr and after addr - in the section sec.
1328 * If we find two symbols with equal offset prefer one with a valid name.
1329 * The ELF format may have a better way to detect what type of symbol
1330 * it is, but this works for now.
1332 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1336 Elf_Sym *near = NULL;
1337 Elf_Addr distance = ~0;
1339 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1342 if (is_shndx_special(sym->st_shndx))
1344 symsec = sec_name(elf, get_secindex(elf, sym));
1345 if (strcmp(symsec, sec) != 0)
1347 if (!is_valid_name(elf, sym))
1349 if (sym->st_value <= addr) {
1350 if ((addr - sym->st_value) < distance) {
1351 distance = addr - sym->st_value;
1353 } else if ((addr - sym->st_value) == distance) {
1362 * Convert a section name to the function/data attribute
1363 * .init.text => __init
1364 * .memexitconst => __memconst
1367 * The memory of returned value has been allocated on a heap. The user of this
1368 * method should free it after usage.
1370 static char *sec2annotation(const char *s)
1372 if (match(s, init_exit_sections)) {
1373 char *p = NOFAIL(malloc(20));
1380 while (*s && *s != '.')
1385 if (strstr(s, "rodata") != NULL)
1386 strcat(p, "const ");
1387 else if (strstr(s, "data") != NULL)
1393 return NOFAIL(strdup(""));
1397 static int is_function(Elf_Sym *sym)
1400 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1405 static void print_section_list(const char * const list[20])
1407 const char *const *s = list;
1410 fprintf(stderr, "%s", *s);
1413 fprintf(stderr, ", ");
1415 fprintf(stderr, "\n");
1418 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1421 case 0: *name = "variable"; *name_p = ""; break;
1422 case 1: *name = "function"; *name_p = "()"; break;
1423 default: *name = "(unknown reference)"; *name_p = ""; break;
1428 * Print a warning about a section mismatch.
1429 * Try to find symbols near it so user can find it.
1430 * Check whitelist before warning - it may be a false positive.
1432 static void report_sec_mismatch(const char *modname,
1433 const struct sectioncheck *mismatch,
1434 const char *fromsec,
1435 unsigned long long fromaddr,
1436 const char *fromsym,
1438 const char *tosec, const char *tosym,
1441 const char *from, *from_p;
1442 const char *to, *to_p;
1446 sec_mismatch_count++;
1448 get_pretty_name(from_is_func, &from, &from_p);
1449 get_pretty_name(to_is_func, &to, &to_p);
1451 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1452 "to the %s %s:%s%s\n",
1453 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1456 switch (mismatch->mismatch) {
1457 case TEXT_TO_ANY_INIT:
1458 prl_from = sec2annotation(fromsec);
1459 prl_to = sec2annotation(tosec);
1461 "The function %s%s() references\n"
1463 "This is often because %s lacks a %s\n"
1464 "annotation or the annotation of %s is wrong.\n",
1466 to, prl_to, tosym, to_p,
1467 fromsym, prl_to, tosym);
1471 case DATA_TO_ANY_INIT: {
1472 prl_to = sec2annotation(tosec);
1474 "The variable %s references\n"
1476 "If the reference is valid then annotate the\n"
1477 "variable with __init* or __refdata (see linux/init.h) "
1478 "or name the variable:\n",
1479 fromsym, to, prl_to, tosym, to_p);
1480 print_section_list(mismatch->symbol_white_list);
1484 case TEXT_TO_ANY_EXIT:
1485 prl_to = sec2annotation(tosec);
1487 "The function %s() references a %s in an exit section.\n"
1488 "Often the %s %s%s has valid usage outside the exit section\n"
1489 "and the fix is to remove the %sannotation of %s.\n",
1490 fromsym, to, to, tosym, to_p, prl_to, tosym);
1493 case DATA_TO_ANY_EXIT: {
1494 prl_to = sec2annotation(tosec);
1496 "The variable %s references\n"
1498 "If the reference is valid then annotate the\n"
1499 "variable with __exit* (see linux/init.h) or "
1500 "name the variable:\n",
1501 fromsym, to, prl_to, tosym, to_p);
1502 print_section_list(mismatch->symbol_white_list);
1506 case XXXINIT_TO_SOME_INIT:
1507 case XXXEXIT_TO_SOME_EXIT:
1508 prl_from = sec2annotation(fromsec);
1509 prl_to = sec2annotation(tosec);
1511 "The %s %s%s%s references\n"
1513 "If %s is only used by %s then\n"
1514 "annotate %s with a matching annotation.\n",
1515 from, prl_from, fromsym, from_p,
1516 to, prl_to, tosym, to_p,
1517 tosym, fromsym, tosym);
1521 case ANY_INIT_TO_ANY_EXIT:
1522 prl_from = sec2annotation(fromsec);
1523 prl_to = sec2annotation(tosec);
1525 "The %s %s%s%s references\n"
1527 "This is often seen when error handling "
1528 "in the init function\n"
1529 "uses functionality in the exit path.\n"
1530 "The fix is often to remove the %sannotation of\n"
1531 "%s%s so it may be used outside an exit section.\n",
1532 from, prl_from, fromsym, from_p,
1533 to, prl_to, tosym, to_p,
1534 prl_to, tosym, to_p);
1538 case ANY_EXIT_TO_ANY_INIT:
1539 prl_from = sec2annotation(fromsec);
1540 prl_to = sec2annotation(tosec);
1542 "The %s %s%s%s references\n"
1544 "This is often seen when error handling "
1545 "in the exit function\n"
1546 "uses functionality in the init path.\n"
1547 "The fix is often to remove the %sannotation of\n"
1548 "%s%s so it may be used outside an init section.\n",
1549 from, prl_from, fromsym, from_p,
1550 to, prl_to, tosym, to_p,
1551 prl_to, tosym, to_p);
1555 case EXPORT_TO_INIT_EXIT:
1556 prl_to = sec2annotation(tosec);
1558 "The symbol %s is exported and annotated %s\n"
1559 "Fix this by removing the %sannotation of %s "
1560 "or drop the export.\n",
1561 tosym, prl_to, prl_to, tosym);
1564 case EXTABLE_TO_NON_TEXT:
1565 fatal("There's a special handler for this mismatch type, "
1566 "we should never get here.");
1569 fprintf(stderr, "\n");
1572 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1573 const struct sectioncheck* const mismatch,
1574 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1580 const char *fromsym;
1582 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1583 fromsym = sym_name(elf, from);
1585 if (strstarts(fromsym, "reference___initcall"))
1588 tosec = sec_name(elf, get_secindex(elf, sym));
1589 to = find_elf_symbol(elf, r->r_addend, sym);
1590 tosym = sym_name(elf, to);
1592 /* check whitelist - we may ignore it */
1593 if (secref_whitelist(mismatch,
1594 fromsec, fromsym, tosec, tosym)) {
1595 report_sec_mismatch(modname, mismatch,
1596 fromsec, r->r_offset, fromsym,
1597 is_function(from), tosec, tosym,
1602 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1604 if (section_index > elf->num_sections)
1605 fatal("section_index is outside elf->num_sections!\n");
1607 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1611 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1612 * to know the sizeof(struct exception_table_entry) for the target architecture.
1614 static unsigned int extable_entry_size = 0;
1615 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1618 * If we're currently checking the second relocation within __ex_table,
1619 * that relocation offset tells us the offsetof(struct
1620 * exception_table_entry, fixup) which is equal to sizeof(struct
1621 * exception_table_entry) divided by two. We use that to our advantage
1622 * since there's no portable way to get that size as every architecture
1623 * seems to go with different sized types. Not pretty but better than
1624 * hard-coding the size for every architecture..
1626 if (!extable_entry_size)
1627 extable_entry_size = r->r_offset * 2;
1630 static inline bool is_extable_fault_address(Elf_Rela *r)
1633 * extable_entry_size is only discovered after we've handled the
1634 * _second_ relocation in __ex_table, so only abort when we're not
1635 * handling the first reloc and extable_entry_size is zero.
1637 if (r->r_offset && extable_entry_size == 0)
1638 fatal("extable_entry size hasn't been discovered!\n");
1640 return ((r->r_offset == 0) ||
1641 (r->r_offset % extable_entry_size == 0));
1644 #define is_second_extable_reloc(Start, Cur, Sec) \
1645 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1647 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1648 const struct sectioncheck* const mismatch,
1649 Elf_Rela* r, Elf_Sym* sym,
1650 const char* fromsec, const char* tosec)
1652 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1653 const char* fromsym_name = sym_name(elf, fromsym);
1654 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1655 const char* tosym_name = sym_name(elf, tosym);
1656 const char* from_pretty_name;
1657 const char* from_pretty_name_p;
1658 const char* to_pretty_name;
1659 const char* to_pretty_name_p;
1661 get_pretty_name(is_function(fromsym),
1662 &from_pretty_name, &from_pretty_name_p);
1663 get_pretty_name(is_function(tosym),
1664 &to_pretty_name, &to_pretty_name_p);
1666 warn("%s(%s+0x%lx): Section mismatch in reference"
1667 " from the %s %s%s to the %s %s:%s%s\n",
1668 modname, fromsec, (long)r->r_offset, from_pretty_name,
1669 fromsym_name, from_pretty_name_p,
1670 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1672 if (!match(tosec, mismatch->bad_tosec) &&
1673 is_executable_section(elf, get_secindex(elf, sym)))
1675 "The relocation at %s+0x%lx references\n"
1676 "section \"%s\" which is not in the list of\n"
1677 "authorized sections. If you're adding a new section\n"
1678 "and/or if this reference is valid, add \"%s\" to the\n"
1679 "list of authorized sections to jump to on fault.\n"
1680 "This can be achieved by adding \"%s\" to \n"
1681 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1682 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1685 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1686 const struct sectioncheck* const mismatch,
1687 Elf_Rela* r, Elf_Sym* sym,
1688 const char *fromsec)
1690 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1692 sec_mismatch_count++;
1694 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1696 if (match(tosec, mismatch->bad_tosec))
1697 fatal("The relocation at %s+0x%lx references\n"
1698 "section \"%s\" which is black-listed.\n"
1699 "Something is seriously wrong and should be fixed.\n"
1700 "You might get more information about where this is\n"
1701 "coming from by using scripts/check_extable.sh %s\n",
1702 fromsec, (long)r->r_offset, tosec, modname);
1703 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1704 if (is_extable_fault_address(r))
1705 fatal("The relocation at %s+0x%lx references\n"
1706 "section \"%s\" which is not executable, IOW\n"
1707 "it is not possible for the kernel to fault\n"
1708 "at that address. Something is seriously wrong\n"
1709 "and should be fixed.\n",
1710 fromsec, (long)r->r_offset, tosec);
1712 fatal("The relocation at %s+0x%lx references\n"
1713 "section \"%s\" which is not executable, IOW\n"
1714 "the kernel will fault if it ever tries to\n"
1715 "jump to it. Something is seriously wrong\n"
1716 "and should be fixed.\n",
1717 fromsec, (long)r->r_offset, tosec);
1721 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1722 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1724 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1725 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1728 if (mismatch->handler)
1729 mismatch->handler(modname, elf, mismatch,
1732 default_mismatch_handler(modname, elf, mismatch,
1737 static unsigned int *reloc_location(struct elf_info *elf,
1738 Elf_Shdr *sechdr, Elf_Rela *r)
1740 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1743 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1745 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1746 unsigned int *location = reloc_location(elf, sechdr, r);
1750 r->r_addend = TO_NATIVE(*location);
1753 r->r_addend = TO_NATIVE(*location) + 4;
1754 /* For CONFIG_RELOCATABLE=y */
1755 if (elf->hdr->e_type == ET_EXEC)
1756 r->r_addend += r->r_offset;
1763 #define R_ARM_CALL 28
1765 #ifndef R_ARM_JUMP24
1766 #define R_ARM_JUMP24 29
1769 #ifndef R_ARM_THM_CALL
1770 #define R_ARM_THM_CALL 10
1772 #ifndef R_ARM_THM_JUMP24
1773 #define R_ARM_THM_JUMP24 30
1775 #ifndef R_ARM_THM_JUMP19
1776 #define R_ARM_THM_JUMP19 51
1779 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1781 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1785 /* From ARM ABI: (S + A) | T */
1786 r->r_addend = (int)(long)
1787 (elf->symtab_start + ELF_R_SYM(r->r_info));
1792 case R_ARM_THM_CALL:
1793 case R_ARM_THM_JUMP24:
1794 case R_ARM_THM_JUMP19:
1795 /* From ARM ABI: ((S + A) | T) - P */
1796 r->r_addend = (int)(long)(elf->hdr +
1798 (r->r_offset - sechdr->sh_addr));
1806 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1808 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1809 unsigned int *location = reloc_location(elf, sechdr, r);
1812 if (r_typ == R_MIPS_HI16)
1813 return 1; /* skip this */
1814 inst = TO_NATIVE(*location);
1817 r->r_addend = inst & 0xffff;
1820 r->r_addend = (inst & 0x03ffffff) << 2;
1830 #define EM_RISCV 243
1833 #ifndef R_RISCV_SUB32
1834 #define R_RISCV_SUB32 39
1837 static void section_rela(const char *modname, struct elf_info *elf,
1844 const char *fromsec;
1846 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1847 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1849 fromsec = sech_name(elf, sechdr);
1850 fromsec += strlen(".rela");
1851 /* if from section (name) is know good then skip it */
1852 if (match(fromsec, section_white_list))
1855 for (rela = start; rela < stop; rela++) {
1856 r.r_offset = TO_NATIVE(rela->r_offset);
1857 #if KERNEL_ELFCLASS == ELFCLASS64
1858 if (elf->hdr->e_machine == EM_MIPS) {
1860 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1861 r_sym = TO_NATIVE(r_sym);
1862 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1863 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1865 r.r_info = TO_NATIVE(rela->r_info);
1866 r_sym = ELF_R_SYM(r.r_info);
1869 r.r_info = TO_NATIVE(rela->r_info);
1870 r_sym = ELF_R_SYM(r.r_info);
1872 r.r_addend = TO_NATIVE(rela->r_addend);
1873 switch (elf->hdr->e_machine) {
1875 if (!strcmp("__ex_table", fromsec) &&
1876 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1880 sym = elf->symtab_start + r_sym;
1881 /* Skip special sections */
1882 if (is_shndx_special(sym->st_shndx))
1884 if (is_second_extable_reloc(start, rela, fromsec))
1885 find_extable_entry_size(fromsec, &r);
1886 check_section_mismatch(modname, elf, &r, sym, fromsec);
1890 static void section_rel(const char *modname, struct elf_info *elf,
1897 const char *fromsec;
1899 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1900 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1902 fromsec = sech_name(elf, sechdr);
1903 fromsec += strlen(".rel");
1904 /* if from section (name) is know good then skip it */
1905 if (match(fromsec, section_white_list))
1908 for (rel = start; rel < stop; rel++) {
1909 r.r_offset = TO_NATIVE(rel->r_offset);
1910 #if KERNEL_ELFCLASS == ELFCLASS64
1911 if (elf->hdr->e_machine == EM_MIPS) {
1913 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1914 r_sym = TO_NATIVE(r_sym);
1915 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1916 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1918 r.r_info = TO_NATIVE(rel->r_info);
1919 r_sym = ELF_R_SYM(r.r_info);
1922 r.r_info = TO_NATIVE(rel->r_info);
1923 r_sym = ELF_R_SYM(r.r_info);
1926 switch (elf->hdr->e_machine) {
1928 if (addend_386_rel(elf, sechdr, &r))
1932 if (addend_arm_rel(elf, sechdr, &r))
1936 if (addend_mips_rel(elf, sechdr, &r))
1940 sym = elf->symtab_start + r_sym;
1941 /* Skip special sections */
1942 if (is_shndx_special(sym->st_shndx))
1944 if (is_second_extable_reloc(start, rel, fromsec))
1945 find_extable_entry_size(fromsec, &r);
1946 check_section_mismatch(modname, elf, &r, sym, fromsec);
1951 * A module includes a number of sections that are discarded
1952 * either when loaded or when used as built-in.
1953 * For loaded modules all functions marked __init and all data
1954 * marked __initdata will be discarded when the module has been initialized.
1955 * Likewise for modules used built-in the sections marked __exit
1956 * are discarded because __exit marked function are supposed to be called
1957 * only when a module is unloaded which never happens for built-in modules.
1958 * The check_sec_ref() function traverses all relocation records
1959 * to find all references to a section that reference a section that will
1960 * be discarded and warns about it.
1962 static void check_sec_ref(struct module *mod, const char *modname,
1963 struct elf_info *elf)
1966 Elf_Shdr *sechdrs = elf->sechdrs;
1968 /* Walk through all sections */
1969 for (i = 0; i < elf->num_sections; i++) {
1970 check_section(modname, elf, &elf->sechdrs[i]);
1971 /* We want to process only relocation sections and not .init */
1972 if (sechdrs[i].sh_type == SHT_RELA)
1973 section_rela(modname, elf, &elf->sechdrs[i]);
1974 else if (sechdrs[i].sh_type == SHT_REL)
1975 section_rel(modname, elf, &elf->sechdrs[i]);
1979 static char *remove_dot(char *s)
1981 size_t n = strcspn(s, ".");
1984 size_t m = strspn(s + n + 1, "0123456789");
1985 if (m && (s[n + m] == '.' || s[n + m] == 0))
1988 /* strip trailing .prelink */
1989 if (strends(s, ".prelink"))
1990 s[strlen(s) - 8] = '\0';
1995 static void read_symbols(const char *modname)
1997 const char *symname;
2002 struct elf_info info = { };
2005 if (!parse_elf(&info, modname))
2011 /* strip trailing .o */
2012 tmp = NOFAIL(strdup(modname));
2013 tmp[strlen(tmp) - 2] = '\0';
2014 /* strip trailing .prelink */
2015 if (strends(tmp, ".prelink"))
2016 tmp[strlen(tmp) - 8] = '\0';
2017 mod = new_module(tmp);
2021 if (!mod->is_vmlinux) {
2022 license = get_modinfo(&info, "license");
2024 error("missing MODULE_LICENSE() in %s\n", modname);
2026 if (!license_is_gpl_compatible(license)) {
2027 mod->is_gpl_compatible = false;
2030 license = get_next_modinfo(&info, "license", license);
2033 namespace = get_modinfo(&info, "import_ns");
2035 add_namespace(&mod->imported_namespaces, namespace);
2036 namespace = get_next_modinfo(&info, "import_ns",
2041 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2042 symname = remove_dot(info.strtab + sym->st_name);
2044 handle_symbol(mod, &info, sym, symname);
2045 handle_moddevtable(mod, &info, sym, symname);
2048 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2049 symname = remove_dot(info.strtab + sym->st_name);
2051 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2052 if (strstarts(symname, "__kstrtabns_"))
2053 sym_update_namespace(symname + strlen("__kstrtabns_"),
2054 sym_get_data(&info, sym));
2055 if (strstarts(symname, "__crc_"))
2056 handle_modversion(mod, &info, sym,
2057 symname + strlen("__crc_"));
2060 // check for static EXPORT_SYMBOL_* functions && global vars
2061 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2062 unsigned char bind = ELF_ST_BIND(sym->st_info);
2064 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2066 find_symbol(remove_dot(info.strtab +
2070 s->is_static = false;
2074 check_sec_ref(mod, modname, &info);
2076 if (!mod->is_vmlinux) {
2077 version = get_modinfo(&info, "version");
2078 if (version || all_versions)
2079 get_src_version(mod->name, mod->srcversion,
2080 sizeof(mod->srcversion) - 1);
2083 parse_elf_finish(&info);
2085 /* Our trick to get versioning for module struct etc. - it's
2086 * never passed as an argument to an exported function, so
2087 * the automatic versioning doesn't pick it up, but it's really
2088 * important anyhow */
2090 sym_add_unresolved("module_layout", mod, false);
2093 static void read_symbols_from_files(const char *filename)
2096 char fname[PATH_MAX];
2098 if (strcmp(filename, "-") != 0) {
2099 in = fopen(filename, "r");
2101 fatal("Can't open filenames file %s: %m", filename);
2104 while (fgets(fname, PATH_MAX, in) != NULL) {
2105 if (strends(fname, "\n"))
2106 fname[strlen(fname)-1] = '\0';
2107 read_symbols(fname);
2116 /* We first write the generated file into memory using the
2117 * following helper, then compare to the file on disk and
2118 * only update the later if anything changed */
2120 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2121 const char *fmt, ...)
2128 len = vsnprintf(tmp, SZ, fmt, ap);
2129 buf_write(buf, tmp, len);
2133 void buf_write(struct buffer *buf, const char *s, int len)
2135 if (buf->size - buf->pos < len) {
2136 buf->size += len + SZ;
2137 buf->p = NOFAIL(realloc(buf->p, buf->size));
2139 strncpy(buf->p + buf->pos, s, len);
2143 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2147 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2151 case export_unknown:
2157 static void check_exports(struct module *mod)
2159 struct symbol *s, *exp;
2161 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2162 const char *basename;
2163 exp = find_symbol(s->name);
2165 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2166 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2167 "\"%s\" [%s.ko] undefined!\n",
2168 s->name, mod->name);
2171 if (exp->module == mod) {
2172 error("\"%s\" [%s.ko] was exported without definition\n",
2173 s->name, mod->name);
2177 s->module = exp->module;
2178 s->crc_valid = exp->crc_valid;
2181 basename = strrchr(mod->name, '/');
2185 basename = mod->name;
2187 if (exp->namespace &&
2188 !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
2189 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2190 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2191 basename, exp->name, exp->namespace);
2192 add_namespace(&mod->missing_namespaces, exp->namespace);
2195 if (!mod->is_gpl_compatible)
2196 check_for_gpl_usage(exp->export, basename, exp->name);
2200 static void check_modname_len(struct module *mod)
2202 const char *mod_name;
2204 mod_name = strrchr(mod->name, '/');
2205 if (mod_name == NULL)
2206 mod_name = mod->name;
2209 if (strlen(mod_name) >= MODULE_NAME_LEN)
2210 error("module name is too long [%s.ko]\n", mod->name);
2214 * Header for the generated file
2216 static void add_header(struct buffer *b, struct module *mod)
2218 buf_printf(b, "#include <linux/module.h>\n");
2220 * Include build-salt.h after module.h in order to
2221 * inherit the definitions.
2223 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2224 buf_printf(b, "#include <linux/build-salt.h>\n");
2225 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2226 buf_printf(b, "#include <linux/vermagic.h>\n");
2227 buf_printf(b, "#include <linux/compiler.h>\n");
2228 buf_printf(b, "\n");
2229 buf_printf(b, "BUILD_SALT;\n");
2230 buf_printf(b, "BUILD_LTO_INFO;\n");
2231 buf_printf(b, "\n");
2232 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2233 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2234 buf_printf(b, "\n");
2235 buf_printf(b, "__visible struct module __this_module\n");
2236 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2237 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2239 buf_printf(b, "\t.init = init_module,\n");
2240 if (mod->has_cleanup)
2241 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2242 "\t.exit = cleanup_module,\n"
2244 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2245 buf_printf(b, "};\n");
2248 static void add_intree_flag(struct buffer *b, int is_intree)
2251 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2254 /* Cannot check for assembler */
2255 static void add_retpoline(struct buffer *b)
2257 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2258 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2259 buf_printf(b, "#endif\n");
2262 static void add_staging_flag(struct buffer *b, const char *name)
2264 if (strstarts(name, "drivers/staging"))
2265 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2269 * Record CRCs for unresolved symbols
2271 static void add_versions(struct buffer *b, struct module *mod)
2278 buf_printf(b, "\n");
2279 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2280 buf_printf(b, "__used __section(\"__versions\") = {\n");
2282 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2285 if (!s->crc_valid) {
2286 warn("\"%s\" [%s.ko] has no CRC!\n",
2287 s->name, mod->name);
2290 if (strlen(s->name) >= MODULE_NAME_LEN) {
2291 error("too long symbol \"%s\" [%s.ko]\n",
2292 s->name, mod->name);
2295 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2299 buf_printf(b, "};\n");
2302 static void add_depends(struct buffer *b, struct module *mod)
2307 /* Clear ->seen flag of modules that own symbols needed by this. */
2308 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2310 s->module->seen = s->module->is_vmlinux;
2313 buf_printf(b, "\n");
2314 buf_printf(b, "MODULE_INFO(depends, \"");
2315 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2320 if (s->module->seen)
2323 s->module->seen = true;
2324 p = strrchr(s->module->name, '/');
2328 p = s->module->name;
2329 buf_printf(b, "%s%s", first ? "" : ",", p);
2332 buf_printf(b, "\");\n");
2335 static void add_srcversion(struct buffer *b, struct module *mod)
2337 if (mod->srcversion[0]) {
2338 buf_printf(b, "\n");
2339 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2344 static void write_buf(struct buffer *b, const char *fname)
2351 file = fopen(fname, "w");
2356 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2360 if (fclose(file) != 0) {
2366 static void write_if_changed(struct buffer *b, const char *fname)
2372 file = fopen(fname, "r");
2376 if (fstat(fileno(file), &st) < 0)
2379 if (st.st_size != b->pos)
2382 tmp = NOFAIL(malloc(b->pos));
2383 if (fread(tmp, 1, b->pos, file) != b->pos)
2386 if (memcmp(tmp, b->p, b->pos) != 0)
2398 write_buf(b, fname);
2401 /* parse Module.symvers file. line format:
2402 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2404 static void read_dump(const char *fname)
2406 char *buf, *pos, *line;
2408 buf = read_text_file(fname);
2410 /* No symbol versions, silently ignore */
2415 while ((line = get_line(&pos))) {
2416 char *symname, *namespace, *modname, *d, *export;
2421 if (!(symname = strchr(line, '\t')))
2424 if (!(modname = strchr(symname, '\t')))
2427 if (!(export = strchr(modname, '\t')))
2430 if (!(namespace = strchr(export, '\t')))
2432 *namespace++ = '\0';
2434 crc = strtoul(line, &d, 16);
2435 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2437 mod = find_module(modname);
2439 mod = new_module(modname);
2440 mod->from_dump = true;
2442 s = sym_add_exported(symname, mod, export_no(export));
2443 s->is_static = false;
2444 sym_set_crc(symname, crc);
2445 sym_update_namespace(symname, namespace);
2451 fatal("parse error in symbol dump file\n");
2454 static void write_dump(const char *fname)
2456 struct buffer buf = { };
2460 list_for_each_entry(mod, &modules, list) {
2463 list_for_each_entry(sym, &mod->exported_symbols, list) {
2464 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2465 sym->crc, sym->name, mod->name,
2466 export_str(sym->export),
2467 sym->namespace ?: "");
2470 write_buf(&buf, fname);
2474 static void write_namespace_deps_files(const char *fname)
2477 struct namespace_list *ns;
2478 struct buffer ns_deps_buf = {};
2480 list_for_each_entry(mod, &modules, list) {
2482 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2485 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2487 list_for_each_entry(ns, &mod->missing_namespaces, list)
2488 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2490 buf_printf(&ns_deps_buf, "\n");
2493 write_if_changed(&ns_deps_buf, fname);
2494 free(ns_deps_buf.p);
2498 struct list_head list;
2502 int main(int argc, char **argv)
2505 struct buffer buf = { };
2506 char *missing_namespace_deps = NULL;
2507 char *dump_write = NULL, *files_source = NULL;
2510 LIST_HEAD(dump_lists);
2511 struct dump_list *dl, *dl2;
2513 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2516 external_module = true;
2519 dl = NOFAIL(malloc(sizeof(*dl)));
2521 list_add_tail(&dl->list, &dump_lists);
2527 ignore_missing_files = true;
2530 dump_write = optarg;
2533 all_versions = true;
2536 files_source = optarg;
2539 warn_unresolved = true;
2542 sec_mismatch_warn_only = false;
2545 allow_missing_ns_imports = true;
2548 missing_namespace_deps = optarg;
2555 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2556 read_dump(dl->file);
2557 list_del(&dl->list);
2561 while (optind < argc)
2562 read_symbols(argv[optind++]);
2565 read_symbols_from_files(files_source);
2567 list_for_each_entry(mod, &modules, list) {
2568 char fname[PATH_MAX];
2571 if (mod->is_vmlinux || mod->from_dump)
2576 check_modname_len(mod);
2579 add_header(&buf, mod);
2580 add_intree_flag(&buf, !external_module);
2581 add_retpoline(&buf);
2582 add_staging_flag(&buf, mod->name);
2583 add_versions(&buf, mod);
2584 add_depends(&buf, mod);
2585 add_moddevtable(&buf, mod);
2586 add_srcversion(&buf, mod);
2588 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2589 if (ret >= sizeof(fname)) {
2590 error("%s: too long path was truncated\n", fname);
2594 write_if_changed(&buf, fname);
2597 if (missing_namespace_deps)
2598 write_namespace_deps_files(missing_namespace_deps);
2601 write_dump(dump_write);
2602 if (sec_mismatch_count && !sec_mismatch_warn_only)
2603 error("Section mismatches detected.\n"
2604 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2605 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2608 for (s = symbolhash[n]; s; s = s->next) {
2610 error("\"%s\" [%s] is a static %s\n",
2611 s->name, s->module->name,
2612 export_str(s->export));
2616 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2617 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2618 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2622 return error_occurred ? 1 : 0;