1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006-2008 Sam Ravnborg
6 * Based in part on module-init-tools/depmod.c,file2alias
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * Usage: modpost vmlinux module1.o module2.o ...
23 #include "../../include/linux/license.h"
25 /* Are we using CONFIG_MODVERSIONS? */
26 static int modversions = 0;
27 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28 static int all_versions = 0;
29 /* If we are modposting external module set to 1 */
30 static int external_module = 0;
31 /* Only warn about unresolved symbols */
32 static int warn_unresolved = 0;
33 /* How a symbol is exported */
34 static int sec_mismatch_count = 0;
35 static int sec_mismatch_warn_only = true;
36 /* ignore missing files */
37 static int ignore_missing_files;
38 /* If set to 1, only warn (instead of error) about missing ns imports */
39 static int allow_missing_ns_imports;
41 static bool error_occurred;
44 * Cut off the warnings when there are too many. This typically occurs when
45 * vmlinux is missing. ('make modules' without building vmlinux.)
47 #define MAX_UNRESOLVED_REPORTS 10
48 static unsigned int nr_unresolved;
56 /* In kernel, this size is defined in linux/module.h;
57 * here we use Elf_Addr instead of long for covering cross-compile
60 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
62 void __attribute__((format(printf, 2, 3)))
63 modpost_log(enum loglevel loglevel, const char *fmt, ...)
69 fprintf(stderr, "WARNING: ");
72 fprintf(stderr, "ERROR: ");
75 fprintf(stderr, "FATAL: ");
77 default: /* invalid loglevel, ignore */
81 fprintf(stderr, "modpost: ");
83 va_start(arglist, fmt);
84 vfprintf(stderr, fmt, arglist);
87 if (loglevel == LOG_FATAL)
89 if (loglevel == LOG_ERROR)
90 error_occurred = true;
93 static inline bool strends(const char *str, const char *postfix)
95 if (strlen(str) < strlen(postfix))
98 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
101 void *do_nofail(void *ptr, const char *expr)
104 fatal("Memory allocation failure: %s.\n", expr);
109 char *read_text_file(const char *filename)
116 fd = open(filename, O_RDONLY);
122 if (fstat(fd, &st) < 0) {
127 buf = NOFAIL(malloc(st.st_size + 1));
134 bytes_read = read(fd, buf, nbytes);
135 if (bytes_read < 0) {
140 nbytes -= bytes_read;
142 buf[st.st_size] = '\0';
149 char *get_line(char **stringp)
151 char *orig = *stringp, *next;
153 /* do not return the unwanted extra line at EOF */
154 if (!orig || *orig == '\0')
157 /* don't use strsep here, it is not available everywhere */
158 next = strchr(orig, '\n');
167 /* A list of all modules we processed */
168 static struct module *modules;
170 static struct module *find_module(const char *modname)
174 for (mod = modules; mod; mod = mod->next)
175 if (strcmp(mod->name, modname) == 0)
180 static struct module *new_module(const char *modname)
184 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
185 memset(mod, 0, sizeof(*mod));
188 strcpy(mod->name, modname);
189 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
190 mod->gpl_compatible = -1;
197 /* A hash of all exported symbols,
198 * struct symbol is also used for lists of unresolved symbols */
200 #define SYMBOL_HASH_SIZE 1024
204 struct module *module;
209 unsigned int is_static:1; /* 1 if symbol is not global */
210 enum export export; /* Type of export */
214 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
216 /* This is based on the hash algorithm from gdbm, via tdb */
217 static inline unsigned int tdb_hash(const char *name)
219 unsigned value; /* Used to compute the hash value. */
220 unsigned i; /* Used to cycle through random values. */
222 /* Set the initial value from the key size. */
223 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
224 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
226 return (1103515243 * value + 12345);
230 * Allocate a new symbols for use in the hash of exported symbols or
231 * the list of unresolved symbols per module
233 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
236 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
238 memset(s, 0, sizeof(*s));
239 strcpy(s->name, name);
246 /* For the hash of exported symbols */
247 static struct symbol *new_symbol(const char *name, struct module *module,
252 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
253 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
255 return symbolhash[hash];
258 static struct symbol *find_symbol(const char *name)
262 /* For our purposes, .foo matches foo. PPC64 needs this. */
266 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
267 if (strcmp(s->name, name) == 0)
273 static bool contains_namespace(struct namespace_list *list,
274 const char *namespace)
276 for (; list; list = list->next)
277 if (!strcmp(list->namespace, namespace))
283 static void add_namespace(struct namespace_list **list, const char *namespace)
285 struct namespace_list *ns_entry;
287 if (!contains_namespace(*list, namespace)) {
288 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
289 strlen(namespace) + 1));
290 strcpy(ns_entry->namespace, namespace);
291 ns_entry->next = *list;
296 static bool module_imports_namespace(struct module *module,
297 const char *namespace)
299 return contains_namespace(module->imported_namespaces, namespace);
302 static const struct {
306 { .str = "EXPORT_SYMBOL", .export = export_plain },
307 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
308 { .str = "(unknown)", .export = export_unknown },
312 static const char *export_str(enum export ex)
314 return export_list[ex].str;
317 static enum export export_no(const char *s)
322 return export_unknown;
323 for (i = 0; export_list[i].export != export_unknown; i++) {
324 if (strcmp(export_list[i].str, s) == 0)
325 return export_list[i].export;
327 return export_unknown;
330 static void *sym_get_data_by_offset(const struct elf_info *info,
331 unsigned int secindex, unsigned long offset)
333 Elf_Shdr *sechdr = &info->sechdrs[secindex];
335 if (info->hdr->e_type != ET_REL)
336 offset -= sechdr->sh_addr;
338 return (void *)info->hdr + sechdr->sh_offset + offset;
341 static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
343 return sym_get_data_by_offset(info, get_secindex(info, sym),
347 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
349 return sym_get_data_by_offset(info, info->secindex_strings,
353 static const char *sec_name(const struct elf_info *info, int secindex)
355 return sech_name(info, &info->sechdrs[secindex]);
358 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
360 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
362 const char *secname = sec_name(elf, sec);
364 if (strstarts(secname, "___ksymtab+"))
366 else if (strstarts(secname, "___ksymtab_gpl+"))
369 return export_unknown;
372 static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
374 if (sec == elf->export_sec)
376 else if (sec == elf->export_gpl_sec)
379 return export_unknown;
382 static const char *namespace_from_kstrtabns(const struct elf_info *info,
385 const char *value = sym_get_data(info, sym);
386 return value[0] ? value : NULL;
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",
405 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
409 * Add an exported symbol - it may have already been added without a
410 * CRC, in this case just update the CRC
412 static struct symbol *sym_add_exported(const char *name, struct module *mod,
415 struct symbol *s = find_symbol(name);
418 s = new_symbol(name, mod, export);
419 } else if (!external_module || s->module->is_vmlinux ||
421 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
422 mod->name, name, s->module->name,
423 s->module->is_vmlinux ? "" : ".ko");
432 static void sym_set_crc(const char *name, unsigned int crc)
434 struct symbol *s = find_symbol(name);
437 * Ignore stand-alone __crc_*, which might be auto-generated symbols
438 * such as __*_veneer in ARM ELF.
447 static void *grab_file(const char *filename, size_t *size)
450 void *map = MAP_FAILED;
453 fd = open(filename, O_RDONLY);
460 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
464 if (map == MAP_FAILED)
469 static void release_file(void *file, size_t size)
474 static int parse_elf(struct elf_info *info, const char *filename)
480 const char *secstrings;
481 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
483 hdr = grab_file(filename, &info->size);
485 if (ignore_missing_files) {
486 fprintf(stderr, "%s: %s (ignored)\n", filename,
494 if (info->size < sizeof(*hdr)) {
495 /* file too small, assume this is an empty .o file */
498 /* Is this a valid ELF file? */
499 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
500 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
501 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
502 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
503 /* Not an ELF file - silently ignore it */
506 /* Fix endianness in ELF header */
507 hdr->e_type = TO_NATIVE(hdr->e_type);
508 hdr->e_machine = TO_NATIVE(hdr->e_machine);
509 hdr->e_version = TO_NATIVE(hdr->e_version);
510 hdr->e_entry = TO_NATIVE(hdr->e_entry);
511 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
512 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
513 hdr->e_flags = TO_NATIVE(hdr->e_flags);
514 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
515 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
516 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
517 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
518 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
519 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
520 sechdrs = (void *)hdr + hdr->e_shoff;
521 info->sechdrs = sechdrs;
523 /* Check if file offset is correct */
524 if (hdr->e_shoff > info->size) {
525 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
526 (unsigned long)hdr->e_shoff, filename, info->size);
530 if (hdr->e_shnum == SHN_UNDEF) {
532 * There are more than 64k sections,
533 * read count from .sh_size.
535 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
538 info->num_sections = hdr->e_shnum;
540 if (hdr->e_shstrndx == SHN_XINDEX) {
541 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
544 info->secindex_strings = hdr->e_shstrndx;
547 /* Fix endianness in section headers */
548 for (i = 0; i < info->num_sections; i++) {
549 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
550 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
551 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
552 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
553 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
554 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
555 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
556 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
557 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
558 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
560 /* Find symbol table. */
561 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
562 for (i = 1; i < info->num_sections; i++) {
564 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
566 if (!nobits && sechdrs[i].sh_offset > info->size) {
567 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
568 "sizeof(*hrd)=%zu\n", filename,
569 (unsigned long)sechdrs[i].sh_offset,
573 secname = secstrings + sechdrs[i].sh_name;
574 if (strcmp(secname, ".modinfo") == 0) {
576 fatal("%s has NOBITS .modinfo\n", filename);
577 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
578 info->modinfo_len = sechdrs[i].sh_size;
579 } else if (strcmp(secname, "__ksymtab") == 0)
580 info->export_sec = i;
581 else if (strcmp(secname, "__ksymtab_gpl") == 0)
582 info->export_gpl_sec = i;
584 if (sechdrs[i].sh_type == SHT_SYMTAB) {
585 unsigned int sh_link_idx;
587 info->symtab_start = (void *)hdr +
588 sechdrs[i].sh_offset;
589 info->symtab_stop = (void *)hdr +
590 sechdrs[i].sh_offset + sechdrs[i].sh_size;
591 sh_link_idx = sechdrs[i].sh_link;
592 info->strtab = (void *)hdr +
593 sechdrs[sh_link_idx].sh_offset;
596 /* 32bit section no. table? ("more than 64k sections") */
597 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
598 symtab_shndx_idx = i;
599 info->symtab_shndx_start = (void *)hdr +
600 sechdrs[i].sh_offset;
601 info->symtab_shndx_stop = (void *)hdr +
602 sechdrs[i].sh_offset + sechdrs[i].sh_size;
605 if (!info->symtab_start)
606 fatal("%s has no symtab?\n", filename);
608 /* Fix endianness in symbols */
609 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
610 sym->st_shndx = TO_NATIVE(sym->st_shndx);
611 sym->st_name = TO_NATIVE(sym->st_name);
612 sym->st_value = TO_NATIVE(sym->st_value);
613 sym->st_size = TO_NATIVE(sym->st_size);
616 if (symtab_shndx_idx != ~0U) {
618 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
619 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
620 filename, sechdrs[symtab_shndx_idx].sh_link,
623 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
631 static void parse_elf_finish(struct elf_info *info)
633 release_file(info->hdr, info->size);
636 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
638 /* ignore __this_module, it will be resolved shortly */
639 if (strcmp(symname, "__this_module") == 0)
641 /* ignore global offset table */
642 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
644 if (info->hdr->e_machine == EM_PPC)
645 /* Special register function linked on all modules during final link of .ko */
646 if (strstarts(symname, "_restgpr_") ||
647 strstarts(symname, "_savegpr_") ||
648 strstarts(symname, "_rest32gpr_") ||
649 strstarts(symname, "_save32gpr_") ||
650 strstarts(symname, "_restvr_") ||
651 strstarts(symname, "_savevr_"))
653 if (info->hdr->e_machine == EM_PPC64)
654 /* Special register function linked on all modules during final link of .ko */
655 if (strstarts(symname, "_restgpr0_") ||
656 strstarts(symname, "_savegpr0_") ||
657 strstarts(symname, "_restvr_") ||
658 strstarts(symname, "_savevr_") ||
659 strcmp(symname, ".TOC.") == 0)
661 /* Do not ignore this symbol */
665 static void handle_modversion(const struct module *mod,
666 const struct elf_info *info,
667 const Elf_Sym *sym, const char *symname)
671 if (sym->st_shndx == SHN_UNDEF) {
672 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
673 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
674 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
680 if (sym->st_shndx == SHN_ABS) {
685 /* symbol points to the CRC in the ELF object */
686 crcp = sym_get_data(info, sym);
687 crc = TO_NATIVE(*crcp);
689 sym_set_crc(symname, crc);
692 static void handle_symbol(struct module *mod, struct elf_info *info,
693 const Elf_Sym *sym, const char *symname)
698 if (strstarts(symname, "__ksymtab"))
699 export = export_from_secname(info, get_secindex(info, sym));
701 export = export_from_sec(info, get_secindex(info, sym));
703 switch (sym->st_shndx) {
705 if (strstarts(symname, "__gnu_lto_")) {
706 /* Should warn here, but modpost runs before the linker */
708 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
711 /* undefined symbol */
712 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
713 ELF_ST_BIND(sym->st_info) != STB_WEAK)
715 if (ignore_undef_symbol(info, symname))
717 if (info->hdr->e_machine == EM_SPARC ||
718 info->hdr->e_machine == EM_SPARCV9) {
719 /* Ignore register directives. */
720 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
722 if (symname[0] == '.') {
723 char *munged = NOFAIL(strdup(symname));
725 munged[1] = toupper(munged[1]);
730 mod->unres = alloc_symbol(symname,
731 ELF_ST_BIND(sym->st_info) == STB_WEAK,
735 /* All exported symbols */
736 if (strstarts(symname, "__ksymtab_")) {
737 name = symname + strlen("__ksymtab_");
738 sym_add_exported(name, mod, export);
740 if (strcmp(symname, "init_module") == 0)
742 if (strcmp(symname, "cleanup_module") == 0)
743 mod->has_cleanup = 1;
749 * Parse tag=value strings from .modinfo section
751 static char *next_string(char *string, unsigned long *secsize)
753 /* Skip non-zero chars */
756 if ((*secsize)-- <= 1)
760 /* Skip any zero padding. */
763 if ((*secsize)-- <= 1)
769 static char *get_next_modinfo(struct elf_info *info, const char *tag,
773 unsigned int taglen = strlen(tag);
774 char *modinfo = info->modinfo;
775 unsigned long size = info->modinfo_len;
778 size -= prev - modinfo;
779 modinfo = next_string(prev, &size);
782 for (p = modinfo; p; p = next_string(p, &size)) {
783 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
784 return p + taglen + 1;
789 static char *get_modinfo(struct elf_info *info, const char *tag)
792 return get_next_modinfo(info, tag, NULL);
796 * Test if string s ends in string sub
799 static int strrcmp(const char *s, const char *sub)
807 sublen = strlen(sub);
809 if ((slen == 0) || (sublen == 0))
815 return memcmp(s + slen - sublen, sub, sublen);
818 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
821 return elf->strtab + sym->st_name;
826 /* The pattern is an array of simple patterns.
827 * "foo" will match an exact string equal to "foo"
828 * "*foo" will match a string that ends with "foo"
829 * "foo*" will match a string that begins with "foo"
830 * "*foo*" will match a string that contains "foo"
832 static int match(const char *sym, const char * const pat[])
837 const char *endp = p + strlen(p) - 1;
840 if (*p == '*' && *endp == '*') {
841 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
842 char *here = strstr(sym, bare);
849 else if (*p == '*') {
850 if (strrcmp(sym, p + 1) == 0)
854 else if (*endp == '*') {
855 if (strncmp(sym, p, strlen(p) - 1) == 0)
860 if (strcmp(p, sym) == 0)
868 /* sections that we do not want to do full section mismatch check on */
869 static const char *const section_white_list[] =
873 ".cranges", /* sh64 */
874 ".zdebug*", /* Compressed debug sections. */
875 ".GCC.command.line", /* record-gcc-switches */
876 ".mdebug*", /* alpha, score, mips etc. */
877 ".pdr", /* alpha, score, mips etc. */
882 ".xt.prop", /* xtensa */
883 ".xt.lit", /* xtensa */
884 ".arcextmap*", /* arc */
885 ".gnu.linkonce.arcext*", /* arc : modules */
886 ".cmem*", /* EZchip */
887 ".fmt_slot*", /* EZchip */
894 * This is used to find sections missing the SHF_ALLOC flag.
895 * The cause of this is often a section specified in assembler
896 * without "ax" / "aw".
898 static void check_section(const char *modname, struct elf_info *elf,
901 const char *sec = sech_name(elf, sechdr);
903 if (sechdr->sh_type == SHT_PROGBITS &&
904 !(sechdr->sh_flags & SHF_ALLOC) &&
905 !match(sec, section_white_list)) {
906 warn("%s (%s): unexpected non-allocatable section.\n"
907 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
908 "Note that for example <linux/init.h> contains\n"
909 "section definitions for use in .S files.\n\n",
916 #define ALL_INIT_DATA_SECTIONS \
917 ".init.setup", ".init.rodata", ".meminit.rodata", \
918 ".init.data", ".meminit.data"
919 #define ALL_EXIT_DATA_SECTIONS \
920 ".exit.data", ".memexit.data"
922 #define ALL_INIT_TEXT_SECTIONS \
923 ".init.text", ".meminit.text"
924 #define ALL_EXIT_TEXT_SECTIONS \
925 ".exit.text", ".memexit.text"
927 #define ALL_PCI_INIT_SECTIONS \
928 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
929 ".pci_fixup_enable", ".pci_fixup_resume", \
930 ".pci_fixup_resume_early", ".pci_fixup_suspend"
932 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
933 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
935 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
936 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
938 #define DATA_SECTIONS ".data", ".data.rel"
939 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
940 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
941 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
942 ".fixup", ".entry.text", ".exception.text", ".text.*", \
943 ".coldtext", ".softirqentry.text"
945 #define INIT_SECTIONS ".init.*"
946 #define MEM_INIT_SECTIONS ".meminit.*"
948 #define EXIT_SECTIONS ".exit.*"
949 #define MEM_EXIT_SECTIONS ".memexit.*"
951 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
952 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
954 /* init data sections */
955 static const char *const init_data_sections[] =
956 { ALL_INIT_DATA_SECTIONS, NULL };
958 /* all init sections */
959 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
961 /* All init and exit sections (code + data) */
962 static const char *const init_exit_sections[] =
963 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
965 /* all text sections */
966 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
969 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
972 /* symbols in .data that may refer to init/exit sections */
973 #define DEFAULT_SYMBOL_WHITE_LIST \
975 "*_template", /* scsi uses *_template a lot */ \
976 "*_timer", /* arm uses ops structures named _timer a lot */ \
977 "*_sht", /* scsi also used *_sht to some extent */ \
983 static const char *const head_sections[] = { ".head.text*", NULL };
984 static const char *const linker_symbols[] =
985 { "__init_begin", "_sinittext", "_einittext", NULL };
986 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
993 XXXINIT_TO_SOME_INIT,
994 XXXEXIT_TO_SOME_EXIT,
995 ANY_INIT_TO_ANY_EXIT,
996 ANY_EXIT_TO_ANY_INIT,
1002 * Describe how to match sections on different criteria:
1004 * @fromsec: Array of sections to be matched.
1006 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1007 * this array is forbidden (black-list). Can be empty.
1009 * @good_tosec: Relocations applied to a section in @fromsec must be
1010 * targeting sections in this array (white-list). Can be empty.
1012 * @mismatch: Type of mismatch.
1014 * @symbol_white_list: Do not match a relocation to a symbol in this list
1015 * even if it is targeting a section in @bad_to_sec.
1017 * @handler: Specific handler to call when a match is found. If NULL,
1018 * default_mismatch_handler() will be called.
1021 struct sectioncheck {
1022 const char *fromsec[20];
1023 const char *bad_tosec[20];
1024 const char *good_tosec[20];
1025 enum mismatch mismatch;
1026 const char *symbol_white_list[20];
1027 void (*handler)(const char *modname, struct elf_info *elf,
1028 const struct sectioncheck* const mismatch,
1029 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1033 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1034 const struct sectioncheck* const mismatch,
1035 Elf_Rela *r, Elf_Sym *sym,
1036 const char *fromsec);
1038 static const struct sectioncheck sectioncheck[] = {
1039 /* Do not reference init/exit code/data from
1040 * normal code and data
1043 .fromsec = { TEXT_SECTIONS, NULL },
1044 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1045 .mismatch = TEXT_TO_ANY_INIT,
1046 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1049 .fromsec = { DATA_SECTIONS, NULL },
1050 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1051 .mismatch = DATA_TO_ANY_INIT,
1052 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1055 .fromsec = { DATA_SECTIONS, NULL },
1056 .bad_tosec = { INIT_SECTIONS, NULL },
1057 .mismatch = DATA_TO_ANY_INIT,
1058 .symbol_white_list = {
1059 "*_template", "*_timer", "*_sht", "*_ops",
1060 "*_probe", "*_probe_one", "*_console", NULL
1064 .fromsec = { TEXT_SECTIONS, NULL },
1065 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1066 .mismatch = TEXT_TO_ANY_EXIT,
1067 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1070 .fromsec = { DATA_SECTIONS, NULL },
1071 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1072 .mismatch = DATA_TO_ANY_EXIT,
1073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1075 /* Do not reference init code/data from meminit code/data */
1077 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1078 .bad_tosec = { INIT_SECTIONS, NULL },
1079 .mismatch = XXXINIT_TO_SOME_INIT,
1080 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1082 /* Do not reference exit code/data from memexit code/data */
1084 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1085 .bad_tosec = { EXIT_SECTIONS, NULL },
1086 .mismatch = XXXEXIT_TO_SOME_EXIT,
1087 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1089 /* Do not use exit code/data from init code */
1091 .fromsec = { ALL_INIT_SECTIONS, NULL },
1092 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1093 .mismatch = ANY_INIT_TO_ANY_EXIT,
1094 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1096 /* Do not use init code/data from exit code */
1098 .fromsec = { ALL_EXIT_SECTIONS, NULL },
1099 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1100 .mismatch = ANY_EXIT_TO_ANY_INIT,
1101 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1104 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1105 .bad_tosec = { INIT_SECTIONS, NULL },
1106 .mismatch = ANY_INIT_TO_ANY_EXIT,
1107 .symbol_white_list = { NULL },
1109 /* Do not export init/exit functions or data */
1111 .fromsec = { "___ksymtab*", NULL },
1112 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1113 .mismatch = EXPORT_TO_INIT_EXIT,
1114 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1117 .fromsec = { "__ex_table", NULL },
1118 /* If you're adding any new black-listed sections in here, consider
1119 * adding a special 'printer' for them in scripts/check_extable.
1121 .bad_tosec = { ".altinstr_replacement", NULL },
1122 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1123 .mismatch = EXTABLE_TO_NON_TEXT,
1124 .handler = extable_mismatch_handler,
1128 static const struct sectioncheck *section_mismatch(
1129 const char *fromsec, const char *tosec)
1132 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1133 const struct sectioncheck *check = §ioncheck[0];
1136 * The target section could be the SHT_NUL section when we're
1137 * handling relocations to un-resolved symbols, trying to match it
1138 * doesn't make much sense and causes build failures on parisc
1144 for (i = 0; i < elems; i++) {
1145 if (match(fromsec, check->fromsec)) {
1146 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1148 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1157 * Whitelist to allow certain references to pass with no warning.
1160 * If a module parameter is declared __initdata and permissions=0
1161 * then this is legal despite the warning generated.
1162 * We cannot see value of permissions here, so just ignore
1164 * The pattern is identified by:
1165 * tosec = .init.data
1170 * module_param_call() ops can refer to __init set function if permissions=0
1171 * The pattern is identified by:
1172 * tosec = .init.text
1174 * atsym = __param_ops_*
1177 * Many drivers utilise a *driver container with references to
1178 * add, remove, probe functions etc.
1179 * the pattern is identified by:
1180 * tosec = init or exit section
1181 * fromsec = data section
1182 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1183 * *probe_one, *_console, *_timer
1186 * Whitelist all references from .head.text to any init section
1189 * Some symbols belong to init section but still it is ok to reference
1190 * these from non-init sections as these symbols don't have any memory
1191 * allocated for them and symbol address and value are same. So even
1192 * if init section is freed, its ok to reference those symbols.
1193 * For ex. symbols marking the init section boundaries.
1194 * This pattern is identified by
1195 * refsymname = __init_begin, _sinittext, _einittext
1198 * GCC may optimize static inlines when fed constant arg(s) resulting
1199 * in functions like cpumask_empty() -- generating an associated symbol
1200 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1201 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1202 * meaningless section warning. May need to add isra symbols too...
1203 * This pattern is identified by
1204 * tosec = init section
1205 * fromsec = text section
1206 * refsymname = *.constprop.*
1209 * Hide section mismatch warnings for ELF local symbols. The goal
1210 * is to eliminate false positive modpost warnings caused by
1211 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1212 * Autogenerated symbol names bypass modpost's "Pattern 2"
1213 * whitelisting, which relies on pattern-matching against symbol
1214 * names to work. (One situation where gcc can autogenerate ELF
1215 * local symbols is when "-fsection-anchors" is used.)
1217 static int secref_whitelist(const struct sectioncheck *mismatch,
1218 const char *fromsec, const char *fromsym,
1219 const char *tosec, const char *tosym)
1221 /* Check for pattern 1 */
1222 if (match(tosec, init_data_sections) &&
1223 match(fromsec, data_sections) &&
1224 strstarts(fromsym, "__param"))
1227 /* Check for pattern 1a */
1228 if (strcmp(tosec, ".init.text") == 0 &&
1229 match(fromsec, data_sections) &&
1230 strstarts(fromsym, "__param_ops_"))
1233 /* Check for pattern 2 */
1234 if (match(tosec, init_exit_sections) &&
1235 match(fromsec, data_sections) &&
1236 match(fromsym, mismatch->symbol_white_list))
1239 /* Check for pattern 3 */
1240 if (match(fromsec, head_sections) &&
1241 match(tosec, init_sections))
1244 /* Check for pattern 4 */
1245 if (match(tosym, linker_symbols))
1248 /* Check for pattern 5 */
1249 if (match(fromsec, text_sections) &&
1250 match(tosec, init_sections) &&
1251 match(fromsym, optim_symbols))
1254 /* Check for pattern 6 */
1255 if (strstarts(fromsym, ".L"))
1261 static inline int is_arm_mapping_symbol(const char *str)
1263 return str[0] == '$' &&
1264 (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x')
1265 && (str[2] == '\0' || str[2] == '.');
1269 * If there's no name there, ignore it; likewise, ignore it if it's
1270 * one of the magic symbols emitted used by current ARM tools.
1272 * Otherwise if find_symbols_between() returns those symbols, they'll
1273 * fail the whitelist tests and cause lots of false alarms ... fixable
1274 * only by merging __exit and __init sections into __text, bloating
1275 * the kernel (which is especially evil on embedded platforms).
1277 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1279 const char *name = elf->strtab + sym->st_name;
1281 if (!name || !strlen(name))
1283 return !is_arm_mapping_symbol(name);
1287 * Find symbol based on relocation record info.
1288 * In some cases the symbol supplied is a valid symbol so
1289 * return refsym. If st_name != 0 we assume this is a valid symbol.
1290 * In other cases the symbol needs to be looked up in the symbol table
1291 * based on section and address.
1293 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1297 Elf_Sym *near = NULL;
1298 Elf64_Sword distance = 20;
1300 unsigned int relsym_secindex;
1302 if (relsym->st_name != 0)
1306 * Strive to find a better symbol name, but the resulting name may not
1307 * match the symbol referenced in the original code.
1309 relsym_secindex = get_secindex(elf, relsym);
1310 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1311 if (get_secindex(elf, sym) != relsym_secindex)
1313 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1315 if (!is_valid_name(elf, sym))
1317 if (sym->st_value == addr)
1319 /* Find a symbol nearby - addr are maybe negative */
1320 d = sym->st_value - addr;
1322 d = addr - sym->st_value;
1328 /* We need a close match */
1336 * Find symbols before or equal addr and after addr - in the section sec.
1337 * If we find two symbols with equal offset prefer one with a valid name.
1338 * The ELF format may have a better way to detect what type of symbol
1339 * it is, but this works for now.
1341 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1345 Elf_Sym *near = NULL;
1346 Elf_Addr distance = ~0;
1348 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1351 if (is_shndx_special(sym->st_shndx))
1353 symsec = sec_name(elf, get_secindex(elf, sym));
1354 if (strcmp(symsec, sec) != 0)
1356 if (!is_valid_name(elf, sym))
1358 if (sym->st_value <= addr) {
1359 if ((addr - sym->st_value) < distance) {
1360 distance = addr - sym->st_value;
1362 } else if ((addr - sym->st_value) == distance) {
1371 * Convert a section name to the function/data attribute
1372 * .init.text => __init
1373 * .memexitconst => __memconst
1376 * The memory of returned value has been allocated on a heap. The user of this
1377 * method should free it after usage.
1379 static char *sec2annotation(const char *s)
1381 if (match(s, init_exit_sections)) {
1382 char *p = NOFAIL(malloc(20));
1389 while (*s && *s != '.')
1394 if (strstr(s, "rodata") != NULL)
1395 strcat(p, "const ");
1396 else if (strstr(s, "data") != NULL)
1402 return NOFAIL(strdup(""));
1406 static int is_function(Elf_Sym *sym)
1409 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1414 static void print_section_list(const char * const list[20])
1416 const char *const *s = list;
1419 fprintf(stderr, "%s", *s);
1422 fprintf(stderr, ", ");
1424 fprintf(stderr, "\n");
1427 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1430 case 0: *name = "variable"; *name_p = ""; break;
1431 case 1: *name = "function"; *name_p = "()"; break;
1432 default: *name = "(unknown reference)"; *name_p = ""; break;
1437 * Print a warning about a section mismatch.
1438 * Try to find symbols near it so user can find it.
1439 * Check whitelist before warning - it may be a false positive.
1441 static void report_sec_mismatch(const char *modname,
1442 const struct sectioncheck *mismatch,
1443 const char *fromsec,
1444 unsigned long long fromaddr,
1445 const char *fromsym,
1447 const char *tosec, const char *tosym,
1450 const char *from, *from_p;
1451 const char *to, *to_p;
1455 sec_mismatch_count++;
1457 get_pretty_name(from_is_func, &from, &from_p);
1458 get_pretty_name(to_is_func, &to, &to_p);
1460 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1461 "to the %s %s:%s%s\n",
1462 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1465 switch (mismatch->mismatch) {
1466 case TEXT_TO_ANY_INIT:
1467 prl_from = sec2annotation(fromsec);
1468 prl_to = sec2annotation(tosec);
1470 "The function %s%s() references\n"
1472 "This is often because %s lacks a %s\n"
1473 "annotation or the annotation of %s is wrong.\n",
1475 to, prl_to, tosym, to_p,
1476 fromsym, prl_to, tosym);
1480 case DATA_TO_ANY_INIT: {
1481 prl_to = sec2annotation(tosec);
1483 "The variable %s references\n"
1485 "If the reference is valid then annotate the\n"
1486 "variable with __init* or __refdata (see linux/init.h) "
1487 "or name the variable:\n",
1488 fromsym, to, prl_to, tosym, to_p);
1489 print_section_list(mismatch->symbol_white_list);
1493 case TEXT_TO_ANY_EXIT:
1494 prl_to = sec2annotation(tosec);
1496 "The function %s() references a %s in an exit section.\n"
1497 "Often the %s %s%s has valid usage outside the exit section\n"
1498 "and the fix is to remove the %sannotation of %s.\n",
1499 fromsym, to, to, tosym, to_p, prl_to, tosym);
1502 case DATA_TO_ANY_EXIT: {
1503 prl_to = sec2annotation(tosec);
1505 "The variable %s references\n"
1507 "If the reference is valid then annotate the\n"
1508 "variable with __exit* (see linux/init.h) or "
1509 "name the variable:\n",
1510 fromsym, to, prl_to, tosym, to_p);
1511 print_section_list(mismatch->symbol_white_list);
1515 case XXXINIT_TO_SOME_INIT:
1516 case XXXEXIT_TO_SOME_EXIT:
1517 prl_from = sec2annotation(fromsec);
1518 prl_to = sec2annotation(tosec);
1520 "The %s %s%s%s references\n"
1522 "If %s is only used by %s then\n"
1523 "annotate %s with a matching annotation.\n",
1524 from, prl_from, fromsym, from_p,
1525 to, prl_to, tosym, to_p,
1526 tosym, fromsym, tosym);
1530 case ANY_INIT_TO_ANY_EXIT:
1531 prl_from = sec2annotation(fromsec);
1532 prl_to = sec2annotation(tosec);
1534 "The %s %s%s%s references\n"
1536 "This is often seen when error handling "
1537 "in the init function\n"
1538 "uses functionality in the exit path.\n"
1539 "The fix is often to remove the %sannotation of\n"
1540 "%s%s so it may be used outside an exit section.\n",
1541 from, prl_from, fromsym, from_p,
1542 to, prl_to, tosym, to_p,
1543 prl_to, tosym, to_p);
1547 case ANY_EXIT_TO_ANY_INIT:
1548 prl_from = sec2annotation(fromsec);
1549 prl_to = sec2annotation(tosec);
1551 "The %s %s%s%s references\n"
1553 "This is often seen when error handling "
1554 "in the exit function\n"
1555 "uses functionality in the init path.\n"
1556 "The fix is often to remove the %sannotation of\n"
1557 "%s%s so it may be used outside an init section.\n",
1558 from, prl_from, fromsym, from_p,
1559 to, prl_to, tosym, to_p,
1560 prl_to, tosym, to_p);
1564 case EXPORT_TO_INIT_EXIT:
1565 prl_to = sec2annotation(tosec);
1567 "The symbol %s is exported and annotated %s\n"
1568 "Fix this by removing the %sannotation of %s "
1569 "or drop the export.\n",
1570 tosym, prl_to, prl_to, tosym);
1573 case EXTABLE_TO_NON_TEXT:
1574 fatal("There's a special handler for this mismatch type, "
1575 "we should never get here.");
1578 fprintf(stderr, "\n");
1581 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1582 const struct sectioncheck* const mismatch,
1583 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1589 const char *fromsym;
1591 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1592 fromsym = sym_name(elf, from);
1594 if (strstarts(fromsym, "reference___initcall"))
1597 tosec = sec_name(elf, get_secindex(elf, sym));
1598 to = find_elf_symbol(elf, r->r_addend, sym);
1599 tosym = sym_name(elf, to);
1601 /* check whitelist - we may ignore it */
1602 if (secref_whitelist(mismatch,
1603 fromsec, fromsym, tosec, tosym)) {
1604 report_sec_mismatch(modname, mismatch,
1605 fromsec, r->r_offset, fromsym,
1606 is_function(from), tosec, tosym,
1611 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1613 if (section_index >= elf->num_sections)
1614 fatal("section_index is outside elf->num_sections!\n");
1616 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1619 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1620 const struct sectioncheck* const mismatch,
1621 Elf_Rela* r, Elf_Sym* sym,
1622 const char* fromsec, const char* tosec)
1624 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1625 const char* fromsym_name = sym_name(elf, fromsym);
1626 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1627 const char* tosym_name = sym_name(elf, tosym);
1628 const char* from_pretty_name;
1629 const char* from_pretty_name_p;
1630 const char* to_pretty_name;
1631 const char* to_pretty_name_p;
1633 get_pretty_name(is_function(fromsym),
1634 &from_pretty_name, &from_pretty_name_p);
1635 get_pretty_name(is_function(tosym),
1636 &to_pretty_name, &to_pretty_name_p);
1638 warn("%s(%s+0x%lx): Section mismatch in reference"
1639 " from the %s %s%s to the %s %s:%s%s\n",
1640 modname, fromsec, (long)r->r_offset, from_pretty_name,
1641 fromsym_name, from_pretty_name_p,
1642 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1644 if (!match(tosec, mismatch->bad_tosec) &&
1645 is_executable_section(elf, get_secindex(elf, sym)))
1647 "The relocation at %s+0x%lx references\n"
1648 "section \"%s\" which is not in the list of\n"
1649 "authorized sections. If you're adding a new section\n"
1650 "and/or if this reference is valid, add \"%s\" to the\n"
1651 "list of authorized sections to jump to on fault.\n"
1652 "This can be achieved by adding \"%s\" to \n"
1653 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1654 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1657 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1658 const struct sectioncheck* const mismatch,
1659 Elf_Rela* r, Elf_Sym* sym,
1660 const char *fromsec)
1662 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1664 sec_mismatch_count++;
1666 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1668 if (match(tosec, mismatch->bad_tosec))
1669 fatal("The relocation at %s+0x%lx references\n"
1670 "section \"%s\" which is black-listed.\n"
1671 "Something is seriously wrong and should be fixed.\n"
1672 "You might get more information about where this is\n"
1673 "coming from by using scripts/check_extable.sh %s\n",
1674 fromsec, (long)r->r_offset, tosec, modname);
1675 else if (!is_executable_section(elf, get_secindex(elf, sym)))
1676 error("%s+0x%lx references non-executable section '%s'\n",
1677 fromsec, (long)r->r_offset, tosec);
1680 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1681 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1683 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1684 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1687 if (mismatch->handler)
1688 mismatch->handler(modname, elf, mismatch,
1691 default_mismatch_handler(modname, elf, mismatch,
1696 static unsigned int *reloc_location(struct elf_info *elf,
1697 Elf_Shdr *sechdr, Elf_Rela *r)
1699 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1702 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1704 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1705 unsigned int *location = reloc_location(elf, sechdr, r);
1709 r->r_addend = TO_NATIVE(*location);
1712 r->r_addend = TO_NATIVE(*location) + 4;
1713 /* For CONFIG_RELOCATABLE=y */
1714 if (elf->hdr->e_type == ET_EXEC)
1715 r->r_addend += r->r_offset;
1722 #define R_ARM_CALL 28
1724 #ifndef R_ARM_JUMP24
1725 #define R_ARM_JUMP24 29
1728 #ifndef R_ARM_THM_CALL
1729 #define R_ARM_THM_CALL 10
1731 #ifndef R_ARM_THM_JUMP24
1732 #define R_ARM_THM_JUMP24 30
1734 #ifndef R_ARM_THM_JUMP19
1735 #define R_ARM_THM_JUMP19 51
1738 static int32_t sign_extend32(int32_t value, int index)
1740 uint8_t shift = 31 - index;
1742 return (int32_t)(value << shift) >> shift;
1745 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1747 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1748 Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info);
1749 void *loc = reloc_location(elf, sechdr, r);
1755 inst = TO_NATIVE(*(uint32_t *)loc);
1756 r->r_addend = inst + sym->st_value;
1761 inst = TO_NATIVE(*(uint32_t *)loc);
1762 offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
1763 r->r_addend = offset + sym->st_value + 8;
1765 case R_ARM_THM_CALL:
1766 case R_ARM_THM_JUMP24:
1767 case R_ARM_THM_JUMP19:
1768 /* From ARM ABI: ((S + A) | T) - P */
1769 r->r_addend = (int)(long)(elf->hdr +
1771 (r->r_offset - sechdr->sh_addr));
1779 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1781 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1782 unsigned int *location = reloc_location(elf, sechdr, r);
1785 if (r_typ == R_MIPS_HI16)
1786 return 1; /* skip this */
1787 inst = TO_NATIVE(*location);
1790 r->r_addend = inst & 0xffff;
1793 r->r_addend = (inst & 0x03ffffff) << 2;
1802 static void section_rela(const char *modname, struct elf_info *elf,
1809 const char *fromsec;
1811 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1812 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1814 fromsec = sech_name(elf, sechdr);
1815 fromsec += strlen(".rela");
1816 /* if from section (name) is know good then skip it */
1817 if (match(fromsec, section_white_list))
1820 for (rela = start; rela < stop; rela++) {
1821 r.r_offset = TO_NATIVE(rela->r_offset);
1822 #if KERNEL_ELFCLASS == ELFCLASS64
1823 if (elf->hdr->e_machine == EM_MIPS) {
1825 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1826 r_sym = TO_NATIVE(r_sym);
1827 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1828 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1830 r.r_info = TO_NATIVE(rela->r_info);
1831 r_sym = ELF_R_SYM(r.r_info);
1834 r.r_info = TO_NATIVE(rela->r_info);
1835 r_sym = ELF_R_SYM(r.r_info);
1837 r.r_addend = TO_NATIVE(rela->r_addend);
1838 sym = elf->symtab_start + r_sym;
1839 /* Skip special sections */
1840 if (is_shndx_special(sym->st_shndx))
1842 check_section_mismatch(modname, elf, &r, sym, fromsec);
1846 static void section_rel(const char *modname, struct elf_info *elf,
1853 const char *fromsec;
1855 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1856 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1858 fromsec = sech_name(elf, sechdr);
1859 fromsec += strlen(".rel");
1860 /* if from section (name) is know good then skip it */
1861 if (match(fromsec, section_white_list))
1864 for (rel = start; rel < stop; rel++) {
1865 r.r_offset = TO_NATIVE(rel->r_offset);
1866 #if KERNEL_ELFCLASS == ELFCLASS64
1867 if (elf->hdr->e_machine == EM_MIPS) {
1869 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1870 r_sym = TO_NATIVE(r_sym);
1871 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1872 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1874 r.r_info = TO_NATIVE(rel->r_info);
1875 r_sym = ELF_R_SYM(r.r_info);
1878 r.r_info = TO_NATIVE(rel->r_info);
1879 r_sym = ELF_R_SYM(r.r_info);
1882 switch (elf->hdr->e_machine) {
1884 if (addend_386_rel(elf, sechdr, &r))
1888 if (addend_arm_rel(elf, sechdr, &r))
1892 if (addend_mips_rel(elf, sechdr, &r))
1896 sym = elf->symtab_start + r_sym;
1897 /* Skip special sections */
1898 if (is_shndx_special(sym->st_shndx))
1900 check_section_mismatch(modname, elf, &r, sym, fromsec);
1905 * A module includes a number of sections that are discarded
1906 * either when loaded or when used as built-in.
1907 * For loaded modules all functions marked __init and all data
1908 * marked __initdata will be discarded when the module has been initialized.
1909 * Likewise for modules used built-in the sections marked __exit
1910 * are discarded because __exit marked function are supposed to be called
1911 * only when a module is unloaded which never happens for built-in modules.
1912 * The check_sec_ref() function traverses all relocation records
1913 * to find all references to a section that reference a section that will
1914 * be discarded and warns about it.
1916 static void check_sec_ref(struct module *mod, const char *modname,
1917 struct elf_info *elf)
1920 Elf_Shdr *sechdrs = elf->sechdrs;
1922 /* Walk through all sections */
1923 for (i = 0; i < elf->num_sections; i++) {
1924 check_section(modname, elf, &elf->sechdrs[i]);
1925 /* We want to process only relocation sections and not .init */
1926 if (sechdrs[i].sh_type == SHT_RELA)
1927 section_rela(modname, elf, &elf->sechdrs[i]);
1928 else if (sechdrs[i].sh_type == SHT_REL)
1929 section_rel(modname, elf, &elf->sechdrs[i]);
1933 static char *remove_dot(char *s)
1935 size_t n = strcspn(s, ".");
1938 size_t m = strspn(s + n + 1, "0123456789");
1939 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
1942 /* strip trailing .lto */
1943 if (strends(s, ".lto"))
1944 s[strlen(s) - 4] = '\0';
1949 static void read_symbols(const char *modname)
1951 const char *symname;
1956 struct elf_info info = { };
1959 if (!parse_elf(&info, modname))
1965 /* strip trailing .o */
1966 tmp = NOFAIL(strdup(modname));
1967 tmp[strlen(tmp) - 2] = '\0';
1968 /* strip trailing .lto */
1969 if (strends(tmp, ".lto"))
1970 tmp[strlen(tmp) - 4] = '\0';
1971 mod = new_module(tmp);
1975 if (!mod->is_vmlinux) {
1976 license = get_modinfo(&info, "license");
1978 error("missing MODULE_LICENSE() in %s\n", modname);
1980 if (license_is_gpl_compatible(license))
1981 mod->gpl_compatible = 1;
1983 mod->gpl_compatible = 0;
1986 license = get_next_modinfo(&info, "license", license);
1989 namespace = get_modinfo(&info, "import_ns");
1991 add_namespace(&mod->imported_namespaces, namespace);
1992 namespace = get_next_modinfo(&info, "import_ns",
1997 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1998 symname = remove_dot(info.strtab + sym->st_name);
2000 handle_symbol(mod, &info, sym, symname);
2001 handle_moddevtable(mod, &info, sym, symname);
2004 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2005 symname = remove_dot(info.strtab + sym->st_name);
2007 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2008 if (strstarts(symname, "__kstrtabns_"))
2009 sym_update_namespace(symname + strlen("__kstrtabns_"),
2010 namespace_from_kstrtabns(&info,
2013 if (strstarts(symname, "__crc_"))
2014 handle_modversion(mod, &info, sym,
2015 symname + strlen("__crc_"));
2018 // check for static EXPORT_SYMBOL_* functions && global vars
2019 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2020 unsigned char bind = ELF_ST_BIND(sym->st_info);
2022 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2024 find_symbol(remove_dot(info.strtab +
2032 check_sec_ref(mod, modname, &info);
2034 if (!mod->is_vmlinux) {
2035 version = get_modinfo(&info, "version");
2036 if (version || all_versions)
2037 get_src_version(mod->name, mod->srcversion,
2038 sizeof(mod->srcversion) - 1);
2041 parse_elf_finish(&info);
2043 /* Our trick to get versioning for module struct etc. - it's
2044 * never passed as an argument to an exported function, so
2045 * the automatic versioning doesn't pick it up, but it's really
2046 * important anyhow */
2048 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
2051 static void read_symbols_from_files(const char *filename)
2054 char fname[PATH_MAX];
2056 if (strcmp(filename, "-") != 0) {
2057 in = fopen(filename, "r");
2059 fatal("Can't open filenames file %s: %m", filename);
2062 while (fgets(fname, PATH_MAX, in) != NULL) {
2063 if (strends(fname, "\n"))
2064 fname[strlen(fname)-1] = '\0';
2065 read_symbols(fname);
2074 /* We first write the generated file into memory using the
2075 * following helper, then compare to the file on disk and
2076 * only update the later if anything changed */
2078 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2079 const char *fmt, ...)
2086 len = vsnprintf(tmp, SZ, fmt, ap);
2087 buf_write(buf, tmp, len);
2091 void buf_write(struct buffer *buf, const char *s, int len)
2093 if (buf->size - buf->pos < len) {
2094 buf->size += len + SZ;
2095 buf->p = NOFAIL(realloc(buf->p, buf->size));
2097 strncpy(buf->p + buf->pos, s, len);
2101 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2105 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2109 case export_unknown:
2115 static void check_exports(struct module *mod)
2117 struct symbol *s, *exp;
2119 for (s = mod->unres; s; s = s->next) {
2120 const char *basename;
2121 exp = find_symbol(s->name);
2122 if (!exp || exp->module == mod) {
2123 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
2124 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2125 "\"%s\" [%s.ko] undefined!\n",
2126 s->name, mod->name);
2129 basename = strrchr(mod->name, '/');
2133 basename = mod->name;
2135 if (exp->namespace &&
2136 !module_imports_namespace(mod, exp->namespace)) {
2137 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2138 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2139 basename, exp->name, exp->namespace);
2140 add_namespace(&mod->missing_namespaces, exp->namespace);
2143 if (!mod->gpl_compatible)
2144 check_for_gpl_usage(exp->export, basename, exp->name);
2148 static void check_modname_len(struct module *mod)
2150 const char *mod_name;
2152 mod_name = strrchr(mod->name, '/');
2153 if (mod_name == NULL)
2154 mod_name = mod->name;
2157 if (strlen(mod_name) >= MODULE_NAME_LEN)
2158 error("module name is too long [%s.ko]\n", mod->name);
2162 * Header for the generated file
2164 static void add_header(struct buffer *b, struct module *mod)
2166 buf_printf(b, "#include <linux/module.h>\n");
2168 * Include build-salt.h after module.h in order to
2169 * inherit the definitions.
2171 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
2172 buf_printf(b, "#include <linux/build-salt.h>\n");
2173 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
2174 buf_printf(b, "#include <linux/vermagic.h>\n");
2175 buf_printf(b, "#include <linux/compiler.h>\n");
2176 buf_printf(b, "\n");
2177 buf_printf(b, "BUILD_SALT;\n");
2178 buf_printf(b, "BUILD_LTO_INFO;\n");
2179 buf_printf(b, "\n");
2180 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2181 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2182 buf_printf(b, "\n");
2183 buf_printf(b, "__visible struct module __this_module\n");
2184 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
2185 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2187 buf_printf(b, "\t.init = init_module,\n");
2188 if (mod->has_cleanup)
2189 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2190 "\t.exit = cleanup_module,\n"
2192 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2193 buf_printf(b, "};\n");
2196 static void add_intree_flag(struct buffer *b, int is_intree)
2199 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2202 /* Cannot check for assembler */
2203 static void add_retpoline(struct buffer *b)
2205 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2206 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2207 buf_printf(b, "#endif\n");
2210 static void add_staging_flag(struct buffer *b, const char *name)
2212 if (strstarts(name, "drivers/staging"))
2213 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2217 * Record CRCs for unresolved symbols
2219 static void add_versions(struct buffer *b, struct module *mod)
2221 struct symbol *s, *exp;
2223 for (s = mod->unres; s; s = s->next) {
2224 exp = find_symbol(s->name);
2225 if (!exp || exp->module == mod)
2227 s->module = exp->module;
2228 s->crc_valid = exp->crc_valid;
2235 buf_printf(b, "\n");
2236 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2237 buf_printf(b, "__used __section(\"__versions\") = {\n");
2239 for (s = mod->unres; s; s = s->next) {
2242 if (!s->crc_valid) {
2243 warn("\"%s\" [%s.ko] has no CRC!\n",
2244 s->name, mod->name);
2247 if (strlen(s->name) >= MODULE_NAME_LEN) {
2248 error("too long symbol \"%s\" [%s.ko]\n",
2249 s->name, mod->name);
2252 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2256 buf_printf(b, "};\n");
2259 static void add_depends(struct buffer *b, struct module *mod)
2264 /* Clear ->seen flag of modules that own symbols needed by this. */
2265 for (s = mod->unres; s; s = s->next)
2267 s->module->seen = s->module->is_vmlinux;
2269 buf_printf(b, "\n");
2270 buf_printf(b, "MODULE_INFO(depends, \"");
2271 for (s = mod->unres; s; s = s->next) {
2276 if (s->module->seen)
2279 s->module->seen = 1;
2280 p = strrchr(s->module->name, '/');
2284 p = s->module->name;
2285 buf_printf(b, "%s%s", first ? "" : ",", p);
2288 buf_printf(b, "\");\n");
2291 static void add_srcversion(struct buffer *b, struct module *mod)
2293 if (mod->srcversion[0]) {
2294 buf_printf(b, "\n");
2295 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2300 static void write_buf(struct buffer *b, const char *fname)
2304 file = fopen(fname, "w");
2309 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2313 if (fclose(file) != 0) {
2319 static void write_if_changed(struct buffer *b, const char *fname)
2325 file = fopen(fname, "r");
2329 if (fstat(fileno(file), &st) < 0)
2332 if (st.st_size != b->pos)
2335 tmp = NOFAIL(malloc(b->pos));
2336 if (fread(tmp, 1, b->pos, file) != b->pos)
2339 if (memcmp(tmp, b->p, b->pos) != 0)
2351 write_buf(b, fname);
2354 /* parse Module.symvers file. line format:
2355 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2357 static void read_dump(const char *fname)
2359 char *buf, *pos, *line;
2361 buf = read_text_file(fname);
2363 /* No symbol versions, silently ignore */
2368 while ((line = get_line(&pos))) {
2369 char *symname, *namespace, *modname, *d, *export;
2374 if (!(symname = strchr(line, '\t')))
2377 if (!(modname = strchr(symname, '\t')))
2380 if (!(export = strchr(modname, '\t')))
2383 if (!(namespace = strchr(export, '\t')))
2385 *namespace++ = '\0';
2387 crc = strtoul(line, &d, 16);
2388 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2390 mod = find_module(modname);
2392 mod = new_module(modname);
2395 s = sym_add_exported(symname, mod, export_no(export));
2397 sym_set_crc(symname, crc);
2398 sym_update_namespace(symname, namespace);
2404 fatal("parse error in symbol dump file\n");
2407 static void write_dump(const char *fname)
2409 struct buffer buf = { };
2410 struct symbol *symbol;
2411 const char *namespace;
2414 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2415 symbol = symbolhash[n];
2417 if (!symbol->module->from_dump) {
2418 namespace = symbol->namespace;
2419 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2420 symbol->crc, symbol->name,
2421 symbol->module->name,
2422 export_str(symbol->export),
2423 namespace ? namespace : "");
2425 symbol = symbol->next;
2428 write_buf(&buf, fname);
2432 static void write_namespace_deps_files(const char *fname)
2435 struct namespace_list *ns;
2436 struct buffer ns_deps_buf = {};
2438 for (mod = modules; mod; mod = mod->next) {
2440 if (mod->from_dump || !mod->missing_namespaces)
2443 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2445 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2446 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2448 buf_printf(&ns_deps_buf, "\n");
2451 write_if_changed(&ns_deps_buf, fname);
2452 free(ns_deps_buf.p);
2456 struct dump_list *next;
2460 int main(int argc, char **argv)
2463 struct buffer buf = { };
2464 char *missing_namespace_deps = NULL;
2465 char *dump_write = NULL, *files_source = NULL;
2468 struct dump_list *dump_read_start = NULL;
2469 struct dump_list **dump_read_iter = &dump_read_start;
2471 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2474 external_module = 1;
2478 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2479 (*dump_read_iter)->file = optarg;
2480 dump_read_iter = &(*dump_read_iter)->next;
2486 ignore_missing_files = 1;
2489 dump_write = optarg;
2495 files_source = optarg;
2498 warn_unresolved = 1;
2501 sec_mismatch_warn_only = false;
2504 allow_missing_ns_imports = 1;
2507 missing_namespace_deps = optarg;
2514 while (dump_read_start) {
2515 struct dump_list *tmp;
2517 read_dump(dump_read_start->file);
2518 tmp = dump_read_start->next;
2519 free(dump_read_start);
2520 dump_read_start = tmp;
2523 while (optind < argc)
2524 read_symbols(argv[optind++]);
2527 read_symbols_from_files(files_source);
2529 for (mod = modules; mod; mod = mod->next) {
2530 char fname[PATH_MAX];
2532 if (mod->is_vmlinux || mod->from_dump)
2537 check_modname_len(mod);
2540 add_header(&buf, mod);
2541 add_intree_flag(&buf, !external_module);
2542 add_retpoline(&buf);
2543 add_staging_flag(&buf, mod->name);
2544 add_versions(&buf, mod);
2545 add_depends(&buf, mod);
2546 add_moddevtable(&buf, mod);
2547 add_srcversion(&buf, mod);
2549 sprintf(fname, "%s.mod.c", mod->name);
2550 write_if_changed(&buf, fname);
2553 if (missing_namespace_deps)
2554 write_namespace_deps_files(missing_namespace_deps);
2557 write_dump(dump_write);
2558 if (sec_mismatch_count && !sec_mismatch_warn_only)
2559 error("Section mismatches detected.\n"
2560 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2561 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2564 for (s = symbolhash[n]; s; s = s->next) {
2566 error("\"%s\" [%s] is a static %s\n",
2567 s->name, s->module->name,
2568 export_str(s->export));
2572 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2573 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2574 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2578 return error_occurred ? 1 : 0;