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 ...
24 #include "../../include/linux/license.h"
26 static bool module_enabled;
27 /* Are we using CONFIG_MODVERSIONS? */
28 static bool modversions;
29 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30 static bool all_versions;
31 /* If we are modposting external module set to 1 */
32 static bool external_module;
33 /* Only warn about unresolved symbols */
34 static bool warn_unresolved;
36 static int sec_mismatch_count;
37 static bool sec_mismatch_warn_only = true;
38 /* Trim EXPORT_SYMBOLs that are unused by in-tree modules */
39 static bool trim_unused_exports;
41 /* ignore missing files */
42 static bool ignore_missing_files;
43 /* If set to 1, only warn (instead of error) about missing ns imports */
44 static bool allow_missing_ns_imports;
46 static bool error_occurred;
48 static bool extra_warn;
51 * Cut off the warnings when there are too many. This typically occurs when
52 * vmlinux is missing. ('make modules' without building vmlinux.)
54 #define MAX_UNRESOLVED_REPORTS 10
55 static unsigned int nr_unresolved;
57 /* In kernel, this size is defined in linux/module.h;
58 * here we use Elf_Addr instead of long for covering cross-compile
61 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
63 void __attribute__((format(printf, 2, 3)))
64 modpost_log(enum loglevel loglevel, const char *fmt, ...)
70 fprintf(stderr, "WARNING: ");
73 fprintf(stderr, "ERROR: ");
76 fprintf(stderr, "FATAL: ");
78 default: /* invalid loglevel, ignore */
82 fprintf(stderr, "modpost: ");
84 va_start(arglist, fmt);
85 vfprintf(stderr, fmt, arglist);
88 if (loglevel == LOG_FATAL)
90 if (loglevel == LOG_ERROR)
91 error_occurred = true;
94 static inline bool strends(const char *str, const char *postfix)
96 if (strlen(str) < strlen(postfix))
99 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
102 void *do_nofail(void *ptr, const char *expr)
105 fatal("Memory allocation failure: %s.\n", expr);
110 char *read_text_file(const char *filename)
117 fd = open(filename, O_RDONLY);
123 if (fstat(fd, &st) < 0) {
128 buf = NOFAIL(malloc(st.st_size + 1));
135 bytes_read = read(fd, buf, nbytes);
136 if (bytes_read < 0) {
141 nbytes -= bytes_read;
143 buf[st.st_size] = '\0';
150 char *get_line(char **stringp)
152 char *orig = *stringp, *next;
154 /* do not return the unwanted extra line at EOF */
155 if (!orig || *orig == '\0')
158 /* don't use strsep here, it is not available everywhere */
159 next = strchr(orig, '\n');
168 /* A list of all modules we processed */
171 static struct module *find_module(const char *modname)
175 list_for_each_entry(mod, &modules, list) {
176 if (strcmp(mod->name, modname) == 0)
182 static struct module *new_module(const char *name, size_t namelen)
186 mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
187 memset(mod, 0, sizeof(*mod));
189 INIT_LIST_HEAD(&mod->exported_symbols);
190 INIT_LIST_HEAD(&mod->unresolved_symbols);
191 INIT_LIST_HEAD(&mod->missing_namespaces);
192 INIT_LIST_HEAD(&mod->imported_namespaces);
194 memcpy(mod->name, name, namelen);
195 mod->name[namelen] = '\0';
196 mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
199 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
200 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
201 * modpost will exit wiht error anyway.
203 mod->is_gpl_compatible = true;
205 list_add_tail(&mod->list, &modules);
210 /* A hash of all exported symbols,
211 * struct symbol is also used for lists of unresolved symbols */
213 #define SYMBOL_HASH_SIZE 1024
217 struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
218 struct module *module;
224 bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
225 bool used; /* there exists a user of this symbol */
229 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
231 /* This is based on the hash algorithm from gdbm, via tdb */
232 static inline unsigned int tdb_hash(const char *name)
234 unsigned value; /* Used to compute the hash value. */
235 unsigned i; /* Used to cycle through random values. */
237 /* Set the initial value from the key size. */
238 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
239 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
241 return (1103515243 * value + 12345);
245 * Allocate a new symbols for use in the hash of exported symbols or
246 * the list of unresolved symbols per module
248 static struct symbol *alloc_symbol(const char *name)
250 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
252 memset(s, 0, sizeof(*s));
253 strcpy(s->name, name);
258 /* For the hash of exported symbols */
259 static void hash_add_symbol(struct symbol *sym)
263 hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
264 sym->next = symbolhash[hash];
265 symbolhash[hash] = sym;
268 static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
272 sym = alloc_symbol(name);
275 list_add_tail(&sym->list, &mod->unresolved_symbols);
278 static struct symbol *sym_find_with_module(const char *name, struct module *mod)
282 /* For our purposes, .foo matches foo. PPC64 needs this. */
286 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
287 if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
293 static struct symbol *find_symbol(const char *name)
295 return sym_find_with_module(name, NULL);
298 struct namespace_list {
299 struct list_head list;
303 static bool contains_namespace(struct list_head *head, const char *namespace)
305 struct namespace_list *list;
308 * The default namespace is null string "", which is always implicitly
314 list_for_each_entry(list, head, list) {
315 if (!strcmp(list->namespace, namespace))
322 static void add_namespace(struct list_head *head, const char *namespace)
324 struct namespace_list *ns_entry;
326 if (!contains_namespace(head, namespace)) {
327 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
328 strlen(namespace) + 1));
329 strcpy(ns_entry->namespace, namespace);
330 list_add_tail(&ns_entry->list, head);
334 static void *sym_get_data_by_offset(const struct elf_info *info,
335 unsigned int secindex, unsigned long offset)
337 Elf_Shdr *sechdr = &info->sechdrs[secindex];
339 return (void *)info->hdr + sechdr->sh_offset + offset;
342 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
344 return sym_get_data_by_offset(info, get_secindex(info, sym),
348 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
350 return sym_get_data_by_offset(info, info->secindex_strings,
354 static const char *sec_name(const struct elf_info *info, unsigned int secindex)
357 * If sym->st_shndx is a special section index, there is no
358 * corresponding section header.
359 * Return "" if the index is out of range of info->sechdrs[] array.
361 if (secindex >= info->num_sections)
364 return sech_name(info, &info->sechdrs[secindex]);
367 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
369 static struct symbol *sym_add_exported(const char *name, struct module *mod,
370 bool gpl_only, const char *namespace)
372 struct symbol *s = find_symbol(name);
374 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
375 error("%s: '%s' exported twice. Previous export was in %s%s\n",
376 mod->name, name, s->module->name,
377 s->module->is_vmlinux ? "" : ".ko");
380 s = alloc_symbol(name);
382 s->is_gpl_only = gpl_only;
383 s->namespace = NOFAIL(strdup(namespace));
384 list_add_tail(&s->list, &mod->exported_symbols);
390 static void sym_set_crc(struct symbol *sym, unsigned int crc)
393 sym->crc_valid = true;
396 static void *grab_file(const char *filename, size_t *size)
399 void *map = MAP_FAILED;
402 fd = open(filename, O_RDONLY);
409 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
413 if (map == MAP_FAILED)
418 static void release_file(void *file, size_t size)
423 static int parse_elf(struct elf_info *info, const char *filename)
429 const char *secstrings;
430 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
432 hdr = grab_file(filename, &info->size);
434 if (ignore_missing_files) {
435 fprintf(stderr, "%s: %s (ignored)\n", filename,
443 if (info->size < sizeof(*hdr)) {
444 /* file too small, assume this is an empty .o file */
447 /* Is this a valid ELF file? */
448 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
449 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
450 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
451 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
452 /* Not an ELF file - silently ignore it */
455 /* Fix endianness in ELF header */
456 hdr->e_type = TO_NATIVE(hdr->e_type);
457 hdr->e_machine = TO_NATIVE(hdr->e_machine);
458 hdr->e_version = TO_NATIVE(hdr->e_version);
459 hdr->e_entry = TO_NATIVE(hdr->e_entry);
460 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
461 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
462 hdr->e_flags = TO_NATIVE(hdr->e_flags);
463 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
464 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
465 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
466 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
467 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
468 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
469 sechdrs = (void *)hdr + hdr->e_shoff;
470 info->sechdrs = sechdrs;
472 /* modpost only works for relocatable objects */
473 if (hdr->e_type != ET_REL)
474 fatal("%s: not relocatable object.", filename);
476 /* Check if file offset is correct */
477 if (hdr->e_shoff > info->size) {
478 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
479 (unsigned long)hdr->e_shoff, filename, info->size);
483 if (hdr->e_shnum == SHN_UNDEF) {
485 * There are more than 64k sections,
486 * read count from .sh_size.
488 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
491 info->num_sections = hdr->e_shnum;
493 if (hdr->e_shstrndx == SHN_XINDEX) {
494 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
497 info->secindex_strings = hdr->e_shstrndx;
500 /* Fix endianness in section headers */
501 for (i = 0; i < info->num_sections; i++) {
502 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
503 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
504 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
505 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
506 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
507 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
508 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
509 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
510 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
511 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
513 /* Find symbol table. */
514 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
515 for (i = 1; i < info->num_sections; i++) {
517 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
519 if (!nobits && sechdrs[i].sh_offset > info->size) {
520 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
521 filename, (unsigned long)sechdrs[i].sh_offset,
525 secname = secstrings + sechdrs[i].sh_name;
526 if (strcmp(secname, ".modinfo") == 0) {
528 fatal("%s has NOBITS .modinfo\n", filename);
529 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
530 info->modinfo_len = sechdrs[i].sh_size;
531 } else if (!strcmp(secname, ".export_symbol")) {
532 info->export_symbol_secndx = i;
535 if (sechdrs[i].sh_type == SHT_SYMTAB) {
536 unsigned int sh_link_idx;
538 info->symtab_start = (void *)hdr +
539 sechdrs[i].sh_offset;
540 info->symtab_stop = (void *)hdr +
541 sechdrs[i].sh_offset + sechdrs[i].sh_size;
542 sh_link_idx = sechdrs[i].sh_link;
543 info->strtab = (void *)hdr +
544 sechdrs[sh_link_idx].sh_offset;
547 /* 32bit section no. table? ("more than 64k sections") */
548 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
549 symtab_shndx_idx = i;
550 info->symtab_shndx_start = (void *)hdr +
551 sechdrs[i].sh_offset;
552 info->symtab_shndx_stop = (void *)hdr +
553 sechdrs[i].sh_offset + sechdrs[i].sh_size;
556 if (!info->symtab_start)
557 fatal("%s has no symtab?\n", filename);
559 /* Fix endianness in symbols */
560 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
561 sym->st_shndx = TO_NATIVE(sym->st_shndx);
562 sym->st_name = TO_NATIVE(sym->st_name);
563 sym->st_value = TO_NATIVE(sym->st_value);
564 sym->st_size = TO_NATIVE(sym->st_size);
567 if (symtab_shndx_idx != ~0U) {
569 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
570 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
571 filename, sechdrs[symtab_shndx_idx].sh_link,
574 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
579 symsearch_init(info);
584 static void parse_elf_finish(struct elf_info *info)
586 symsearch_finish(info);
587 release_file(info->hdr, info->size);
590 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
592 /* ignore __this_module, it will be resolved shortly */
593 if (strcmp(symname, "__this_module") == 0)
595 /* ignore global offset table */
596 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
598 if (info->hdr->e_machine == EM_PPC)
599 /* Special register function linked on all modules during final link of .ko */
600 if (strstarts(symname, "_restgpr_") ||
601 strstarts(symname, "_savegpr_") ||
602 strstarts(symname, "_rest32gpr_") ||
603 strstarts(symname, "_save32gpr_") ||
604 strstarts(symname, "_restvr_") ||
605 strstarts(symname, "_savevr_"))
607 if (info->hdr->e_machine == EM_PPC64)
608 /* Special register function linked on all modules during final link of .ko */
609 if (strstarts(symname, "_restgpr0_") ||
610 strstarts(symname, "_savegpr0_") ||
611 strstarts(symname, "_restvr_") ||
612 strstarts(symname, "_savevr_") ||
613 strcmp(symname, ".TOC.") == 0)
616 if (info->hdr->e_machine == EM_S390)
617 /* Expoline thunks are linked on all kernel modules during final link of .ko */
618 if (strstarts(symname, "__s390_indirect_jump_r"))
620 /* Do not ignore this symbol */
624 static void handle_symbol(struct module *mod, struct elf_info *info,
625 const Elf_Sym *sym, const char *symname)
627 switch (sym->st_shndx) {
629 if (strstarts(symname, "__gnu_lto_")) {
630 /* Should warn here, but modpost runs before the linker */
632 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
635 /* undefined symbol */
636 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
637 ELF_ST_BIND(sym->st_info) != STB_WEAK)
639 if (ignore_undef_symbol(info, symname))
641 if (info->hdr->e_machine == EM_SPARC ||
642 info->hdr->e_machine == EM_SPARCV9) {
643 /* Ignore register directives. */
644 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
646 if (symname[0] == '.') {
647 char *munged = NOFAIL(strdup(symname));
649 munged[1] = toupper(munged[1]);
654 sym_add_unresolved(symname, mod,
655 ELF_ST_BIND(sym->st_info) == STB_WEAK);
658 if (strcmp(symname, "init_module") == 0)
659 mod->has_init = true;
660 if (strcmp(symname, "cleanup_module") == 0)
661 mod->has_cleanup = true;
667 * Parse tag=value strings from .modinfo section
669 static char *next_string(char *string, unsigned long *secsize)
671 /* Skip non-zero chars */
674 if ((*secsize)-- <= 1)
678 /* Skip any zero padding. */
681 if ((*secsize)-- <= 1)
687 static char *get_next_modinfo(struct elf_info *info, const char *tag,
691 unsigned int taglen = strlen(tag);
692 char *modinfo = info->modinfo;
693 unsigned long size = info->modinfo_len;
696 size -= prev - modinfo;
697 modinfo = next_string(prev, &size);
700 for (p = modinfo; p; p = next_string(p, &size)) {
701 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
702 return p + taglen + 1;
707 static char *get_modinfo(struct elf_info *info, const char *tag)
710 return get_next_modinfo(info, tag, NULL);
713 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
716 return elf->strtab + sym->st_name;
722 * Check whether the 'string' argument matches one of the 'patterns',
723 * an array of shell wildcard patterns (glob).
725 * Return true is there is a match.
727 static bool match(const char *string, const char *const patterns[])
731 while ((pattern = *patterns++)) {
732 if (!fnmatch(pattern, string, 0))
739 /* useful to pass patterns to match() directly */
740 #define PATTERNS(...) \
742 static const char *const patterns[] = {__VA_ARGS__, NULL}; \
746 /* sections that we do not want to do full section mismatch check on */
747 static const char *const section_white_list[] =
751 ".zdebug*", /* Compressed debug sections. */
752 ".GCC.command.line", /* record-gcc-switches */
753 ".mdebug*", /* alpha, score, mips etc. */
754 ".pdr", /* alpha, score, mips etc. */
759 ".xt.prop", /* xtensa */
760 ".xt.lit", /* xtensa */
761 ".arcextmap*", /* arc */
762 ".gnu.linkonce.arcext*", /* arc : modules */
763 ".cmem*", /* EZchip */
764 ".fmt_slot*", /* EZchip */
767 ".llvm.call-graph-profile", /* call graph */
772 * This is used to find sections missing the SHF_ALLOC flag.
773 * The cause of this is often a section specified in assembler
774 * without "ax" / "aw".
776 static void check_section(const char *modname, struct elf_info *elf,
779 const char *sec = sech_name(elf, sechdr);
781 if (sechdr->sh_type == SHT_PROGBITS &&
782 !(sechdr->sh_flags & SHF_ALLOC) &&
783 !match(sec, section_white_list)) {
784 warn("%s (%s): unexpected non-allocatable section.\n"
785 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
786 "Note that for example <linux/init.h> contains\n"
787 "section definitions for use in .S files.\n\n",
794 #define ALL_INIT_DATA_SECTIONS \
795 ".init.setup", ".init.rodata", ".meminit.rodata", \
796 ".init.data", ".meminit.data"
798 #define ALL_PCI_INIT_SECTIONS \
799 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
800 ".pci_fixup_enable", ".pci_fixup_resume", \
801 ".pci_fixup_resume_early", ".pci_fixup_suspend"
803 #define ALL_XXXINIT_SECTIONS ".meminit.*"
805 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
806 #define ALL_EXIT_SECTIONS ".exit.*"
808 #define DATA_SECTIONS ".data", ".data.rel"
809 #define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
810 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
811 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
812 ".fixup", ".entry.text", ".exception.text", \
813 ".coldtext", ".softirqentry.text"
815 #define INIT_SECTIONS ".init.*"
817 #define ALL_TEXT_SECTIONS ".init.text", ".meminit.text", ".exit.text", \
818 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
821 TEXTDATA_TO_ANY_INIT_EXIT,
822 XXXINIT_TO_SOME_INIT,
823 ANY_INIT_TO_ANY_EXIT,
824 ANY_EXIT_TO_ANY_INIT,
829 * Describe how to match sections on different criteria:
831 * @fromsec: Array of sections to be matched.
833 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
834 * this array is forbidden (black-list). Can be empty.
836 * @good_tosec: Relocations applied to a section in @fromsec must be
837 * targeting sections in this array (white-list). Can be empty.
839 * @mismatch: Type of mismatch.
841 struct sectioncheck {
842 const char *fromsec[20];
843 const char *bad_tosec[20];
844 const char *good_tosec[20];
845 enum mismatch mismatch;
848 static const struct sectioncheck sectioncheck[] = {
849 /* Do not reference init/exit code/data from
850 * normal code and data
853 .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
854 .bad_tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
855 .mismatch = TEXTDATA_TO_ANY_INIT_EXIT,
857 /* Do not reference init code/data from meminit code/data */
859 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
860 .bad_tosec = { INIT_SECTIONS, NULL },
861 .mismatch = XXXINIT_TO_SOME_INIT,
863 /* Do not use exit code/data from init code */
865 .fromsec = { ALL_INIT_SECTIONS, NULL },
866 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
867 .mismatch = ANY_INIT_TO_ANY_EXIT,
869 /* Do not use init code/data from exit code */
871 .fromsec = { ALL_EXIT_SECTIONS, NULL },
872 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
873 .mismatch = ANY_EXIT_TO_ANY_INIT,
876 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
877 .bad_tosec = { INIT_SECTIONS, NULL },
878 .mismatch = ANY_INIT_TO_ANY_EXIT,
881 .fromsec = { "__ex_table", NULL },
882 /* If you're adding any new black-listed sections in here, consider
883 * adding a special 'printer' for them in scripts/check_extable.
885 .bad_tosec = { ".altinstr_replacement", NULL },
886 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
887 .mismatch = EXTABLE_TO_NON_TEXT,
891 static const struct sectioncheck *section_mismatch(
892 const char *fromsec, const char *tosec)
897 * The target section could be the SHT_NUL section when we're
898 * handling relocations to un-resolved symbols, trying to match it
899 * doesn't make much sense and causes build failures on parisc
905 for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
906 const struct sectioncheck *check = §ioncheck[i];
908 if (match(fromsec, check->fromsec)) {
909 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
911 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
919 * Whitelist to allow certain references to pass with no warning.
922 * If a module parameter is declared __initdata and permissions=0
923 * then this is legal despite the warning generated.
924 * We cannot see value of permissions here, so just ignore
926 * The pattern is identified by:
932 * module_param_call() ops can refer to __init set function if permissions=0
933 * The pattern is identified by:
936 * atsym = __param_ops_*
939 * Whitelist all references from .head.text to any init section
942 * Some symbols belong to init section but still it is ok to reference
943 * these from non-init sections as these symbols don't have any memory
944 * allocated for them and symbol address and value are same. So even
945 * if init section is freed, its ok to reference those symbols.
946 * For ex. symbols marking the init section boundaries.
947 * This pattern is identified by
948 * refsymname = __init_begin, _sinittext, _einittext
951 * GCC may optimize static inlines when fed constant arg(s) resulting
952 * in functions like cpumask_empty() -- generating an associated symbol
953 * cpumask_empty.constprop.3 that appears in the audit. If the const that
954 * is passed in comes from __init, like say nmi_ipi_mask, we get a
955 * meaningless section warning. May need to add isra symbols too...
956 * This pattern is identified by
957 * tosec = init section
958 * fromsec = text section
959 * refsymname = *.constprop.*
962 static int secref_whitelist(const char *fromsec, const char *fromsym,
963 const char *tosec, const char *tosym)
965 /* Check for pattern 1 */
966 if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) &&
967 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
968 strstarts(fromsym, "__param"))
971 /* Check for pattern 1a */
972 if (strcmp(tosec, ".init.text") == 0 &&
973 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
974 strstarts(fromsym, "__param_ops_"))
977 /* symbols in data sections that may refer to any init/exit sections */
978 if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
979 match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
980 match(fromsym, PATTERNS("*_ops", "*_probe", "*_console")))
984 * symbols in data sections must not refer to .exit.*, but there are
985 * quite a few offenders, so hide these unless for W=1 builds until
989 match(fromsec, PATTERNS(DATA_SECTIONS)) &&
990 match(tosec, PATTERNS(ALL_EXIT_SECTIONS)) &&
991 match(fromsym, PATTERNS("*driver")))
994 /* Check for pattern 3 */
995 if (strstarts(fromsec, ".head.text") &&
996 match(tosec, PATTERNS(ALL_INIT_SECTIONS)))
999 /* Check for pattern 4 */
1000 if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext")))
1003 /* Check for pattern 5 */
1004 if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
1005 match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
1006 match(fromsym, PATTERNS("*.constprop.*")))
1012 static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr,
1013 unsigned int secndx)
1015 return symsearch_find_nearest(elf, addr, secndx, false, ~0);
1018 static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
1020 /* If the supplied symbol has a valid name, return it */
1021 if (is_valid_name(elf, sym))
1025 * Strive to find a better symbol name, but the resulting name may not
1026 * match the symbol referenced in the original code.
1028 return symsearch_find_nearest(elf, addr, get_secindex(elf, sym),
1032 static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
1034 if (secndx >= elf->num_sections)
1037 return (elf->sechdrs[secndx].sh_flags & SHF_EXECINSTR) != 0;
1040 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1041 const struct sectioncheck* const mismatch,
1043 unsigned int fsecndx, const char *fromsec, Elf_Addr faddr,
1044 const char *tosec, Elf_Addr taddr)
1048 const char *fromsym;
1050 from = find_fromsym(elf, faddr, fsecndx);
1051 fromsym = sym_name(elf, from);
1053 tsym = find_tosym(elf, taddr, tsym);
1054 tosym = sym_name(elf, tsym);
1056 /* check whitelist - we may ignore it */
1057 if (!secref_whitelist(fromsec, fromsym, tosec, tosym))
1060 sec_mismatch_count++;
1062 warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n",
1063 modname, fromsym, (unsigned int)(faddr - from->st_value), fromsec, tosym, tosec);
1065 if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
1066 if (match(tosec, mismatch->bad_tosec))
1067 fatal("The relocation at %s+0x%lx references\n"
1068 "section \"%s\" which is black-listed.\n"
1069 "Something is seriously wrong and should be fixed.\n"
1070 "You might get more information about where this is\n"
1071 "coming from by using scripts/check_extable.sh %s\n",
1072 fromsec, (long)faddr, tosec, modname);
1073 else if (is_executable_section(elf, get_secindex(elf, tsym)))
1074 warn("The relocation at %s+0x%lx references\n"
1075 "section \"%s\" which is not in the list of\n"
1076 "authorized sections. If you're adding a new section\n"
1077 "and/or if this reference is valid, add \"%s\" to the\n"
1078 "list of authorized sections to jump to on fault.\n"
1079 "This can be achieved by adding \"%s\" to\n"
1080 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1081 fromsec, (long)faddr, tosec, tosec, tosec);
1083 error("%s+0x%lx references non-executable section '%s'\n",
1084 fromsec, (long)faddr, tosec);
1088 static void check_export_symbol(struct module *mod, struct elf_info *elf,
1089 Elf_Addr faddr, const char *secname,
1092 static const char *prefix = "__export_symbol_";
1093 const char *label_name, *name, *data;
1098 label = find_fromsym(elf, faddr, elf->export_symbol_secndx);
1099 label_name = sym_name(elf, label);
1101 if (!strstarts(label_name, prefix)) {
1102 error("%s: .export_symbol section contains strange symbol '%s'\n",
1103 mod->name, label_name);
1107 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
1108 ELF_ST_BIND(sym->st_info) != STB_WEAK) {
1109 error("%s: local symbol '%s' was exported\n", mod->name,
1110 label_name + strlen(prefix));
1114 name = sym_name(elf, sym);
1115 if (strcmp(label_name + strlen(prefix), name)) {
1116 error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",
1121 data = sym_get_data(elf, label); /* license */
1122 if (!strcmp(data, "GPL")) {
1124 } else if (!strcmp(data, "")) {
1127 error("%s: unknown license '%s' was specified for '%s'\n",
1128 mod->name, data, name);
1132 data += strlen(data) + 1; /* namespace */
1133 s = sym_add_exported(name, mod, is_gpl, data);
1136 * We need to be aware whether we are exporting a function or
1137 * a data on some architectures.
1139 s->is_func = (ELF_ST_TYPE(sym->st_info) == STT_FUNC);
1142 * For parisc64, symbols prefixed $$ from the library have the symbol type
1143 * STT_LOPROC. They should be handled as functions too.
1145 if (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64 &&
1146 elf->hdr->e_machine == EM_PARISC &&
1147 ELF_ST_TYPE(sym->st_info) == STT_LOPROC)
1150 if (match(secname, PATTERNS(ALL_INIT_SECTIONS)))
1151 warn("%s: %s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_SYMBOL.\n",
1153 else if (match(secname, PATTERNS(ALL_EXIT_SECTIONS)))
1154 warn("%s: %s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_SYMBOL.\n",
1158 static void check_section_mismatch(struct module *mod, struct elf_info *elf,
1160 unsigned int fsecndx, const char *fromsec,
1161 Elf_Addr faddr, Elf_Addr taddr)
1163 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1164 const struct sectioncheck *mismatch;
1166 if (module_enabled && elf->export_symbol_secndx == fsecndx) {
1167 check_export_symbol(mod, elf, faddr, tosec, sym);
1171 mismatch = section_mismatch(fromsec, tosec);
1175 default_mismatch_handler(mod->name, elf, mismatch, sym,
1176 fsecndx, fromsec, faddr,
1180 static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
1184 return TO_NATIVE(*location);
1186 return TO_NATIVE(*location) + 4;
1189 return (Elf_Addr)(-1);
1193 #define R_ARM_CALL 28
1195 #ifndef R_ARM_JUMP24
1196 #define R_ARM_JUMP24 29
1199 #ifndef R_ARM_THM_CALL
1200 #define R_ARM_THM_CALL 10
1202 #ifndef R_ARM_THM_JUMP24
1203 #define R_ARM_THM_JUMP24 30
1206 #ifndef R_ARM_MOVW_ABS_NC
1207 #define R_ARM_MOVW_ABS_NC 43
1210 #ifndef R_ARM_MOVT_ABS
1211 #define R_ARM_MOVT_ABS 44
1214 #ifndef R_ARM_THM_MOVW_ABS_NC
1215 #define R_ARM_THM_MOVW_ABS_NC 47
1218 #ifndef R_ARM_THM_MOVT_ABS
1219 #define R_ARM_THM_MOVT_ABS 48
1222 #ifndef R_ARM_THM_JUMP19
1223 #define R_ARM_THM_JUMP19 51
1226 static int32_t sign_extend32(int32_t value, int index)
1228 uint8_t shift = 31 - index;
1230 return (int32_t)(value << shift) >> shift;
1233 static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
1235 uint32_t inst, upper, lower, sign, j1, j2;
1241 inst = TO_NATIVE(*(uint32_t *)loc);
1242 return inst + sym->st_value;
1243 case R_ARM_MOVW_ABS_NC:
1244 case R_ARM_MOVT_ABS:
1245 inst = TO_NATIVE(*(uint32_t *)loc);
1246 offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff),
1248 return offset + sym->st_value;
1252 inst = TO_NATIVE(*(uint32_t *)loc);
1253 offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
1254 return offset + sym->st_value + 8;
1255 case R_ARM_THM_MOVW_ABS_NC:
1256 case R_ARM_THM_MOVT_ABS:
1257 upper = TO_NATIVE(*(uint16_t *)loc);
1258 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1259 offset = sign_extend32(((upper & 0x000f) << 12) |
1260 ((upper & 0x0400) << 1) |
1261 ((lower & 0x7000) >> 4) |
1264 return offset + sym->st_value;
1265 case R_ARM_THM_JUMP19:
1272 * imm11 = lower[10:0]
1273 * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
1275 upper = TO_NATIVE(*(uint16_t *)loc);
1276 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1278 sign = (upper >> 10) & 1;
1279 j1 = (lower >> 13) & 1;
1280 j2 = (lower >> 11) & 1;
1281 offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) |
1282 ((upper & 0x03f) << 12) |
1283 ((lower & 0x07ff) << 1),
1285 return offset + sym->st_value + 4;
1286 case R_ARM_THM_CALL:
1287 case R_ARM_THM_JUMP24:
1291 * imm10 = upper[9:0]
1294 * imm11 = lower[10:0]
1295 * I1 = NOT(J1 XOR S)
1296 * I2 = NOT(J2 XOR S)
1297 * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
1299 upper = TO_NATIVE(*(uint16_t *)loc);
1300 lower = TO_NATIVE(*((uint16_t *)loc + 1));
1302 sign = (upper >> 10) & 1;
1303 j1 = (lower >> 13) & 1;
1304 j2 = (lower >> 11) & 1;
1305 offset = sign_extend32((sign << 24) |
1306 ((~(j1 ^ sign) & 1) << 23) |
1307 ((~(j2 ^ sign) & 1) << 22) |
1308 ((upper & 0x03ff) << 12) |
1309 ((lower & 0x07ff) << 1),
1311 return offset + sym->st_value + 4;
1314 return (Elf_Addr)(-1);
1317 static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
1321 inst = TO_NATIVE(*location);
1324 return inst & 0xffff;
1326 return (inst & 0x03ffffff) << 2;
1330 return (Elf_Addr)(-1);
1334 #define EM_RISCV 243
1337 #ifndef R_RISCV_SUB32
1338 #define R_RISCV_SUB32 39
1341 #ifndef EM_LOONGARCH
1342 #define EM_LOONGARCH 258
1345 #ifndef R_LARCH_SUB32
1346 #define R_LARCH_SUB32 55
1349 static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info,
1350 unsigned int *r_type, unsigned int *r_sym)
1353 Elf64_Word r_sym; /* Symbol index */
1354 unsigned char r_ssym; /* Special symbol for 2nd relocation */
1355 unsigned char r_type3; /* 3rd relocation type */
1356 unsigned char r_type2; /* 2nd relocation type */
1357 unsigned char r_type; /* 1st relocation type */
1358 } Elf64_Mips_R_Info;
1360 bool is_64bit = (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64);
1362 if (elf->hdr->e_machine == EM_MIPS && is_64bit) {
1363 Elf64_Mips_R_Info *mips64_r_info = (void *)&r_info;
1365 *r_type = mips64_r_info->r_type;
1366 *r_sym = TO_NATIVE(mips64_r_info->r_sym);
1371 r_info = TO_NATIVE((Elf64_Xword)r_info);
1373 r_info = TO_NATIVE((Elf32_Word)r_info);
1375 *r_type = ELF_R_TYPE(r_info);
1376 *r_sym = ELF_R_SYM(r_info);
1379 static void section_rela(struct module *mod, struct elf_info *elf,
1380 unsigned int fsecndx, const char *fromsec,
1381 const Elf_Rela *start, const Elf_Rela *stop)
1383 const Elf_Rela *rela;
1385 for (rela = start; rela < stop; rela++) {
1387 Elf_Addr taddr, r_offset;
1388 unsigned int r_type, r_sym;
1390 r_offset = TO_NATIVE(rela->r_offset);
1391 get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym);
1393 tsym = elf->symtab_start + r_sym;
1394 taddr = tsym->st_value + TO_NATIVE(rela->r_addend);
1396 switch (elf->hdr->e_machine) {
1398 if (!strcmp("__ex_table", fromsec) &&
1399 r_type == R_RISCV_SUB32)
1403 if (!strcmp("__ex_table", fromsec) &&
1404 r_type == R_LARCH_SUB32)
1409 check_section_mismatch(mod, elf, tsym,
1410 fsecndx, fromsec, r_offset, taddr);
1414 static void section_rel(struct module *mod, struct elf_info *elf,
1415 unsigned int fsecndx, const char *fromsec,
1416 const Elf_Rel *start, const Elf_Rel *stop)
1420 for (rel = start; rel < stop; rel++) {
1422 Elf_Addr taddr = 0, r_offset;
1423 unsigned int r_type, r_sym;
1426 r_offset = TO_NATIVE(rel->r_offset);
1427 get_rel_type_and_sym(elf, rel->r_info, &r_type, &r_sym);
1429 loc = sym_get_data_by_offset(elf, fsecndx, r_offset);
1430 tsym = elf->symtab_start + r_sym;
1432 switch (elf->hdr->e_machine) {
1434 taddr = addend_386_rel(loc, r_type);
1437 taddr = addend_arm_rel(loc, tsym, r_type);
1440 taddr = addend_mips_rel(loc, r_type);
1443 fatal("Please add code to calculate addend for this architecture\n");
1446 check_section_mismatch(mod, elf, tsym,
1447 fsecndx, fromsec, r_offset, taddr);
1452 * A module includes a number of sections that are discarded
1453 * either when loaded or when used as built-in.
1454 * For loaded modules all functions marked __init and all data
1455 * marked __initdata will be discarded when the module has been initialized.
1456 * Likewise for modules used built-in the sections marked __exit
1457 * are discarded because __exit marked function are supposed to be called
1458 * only when a module is unloaded which never happens for built-in modules.
1459 * The check_sec_ref() function traverses all relocation records
1460 * to find all references to a section that reference a section that will
1461 * be discarded and warns about it.
1463 static void check_sec_ref(struct module *mod, struct elf_info *elf)
1467 /* Walk through all sections */
1468 for (i = 0; i < elf->num_sections; i++) {
1469 Elf_Shdr *sechdr = &elf->sechdrs[i];
1471 check_section(mod->name, elf, sechdr);
1472 /* We want to process only relocation sections and not .init */
1473 if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) {
1474 /* section to which the relocation applies */
1475 unsigned int secndx = sechdr->sh_info;
1476 const char *secname = sec_name(elf, secndx);
1477 const void *start, *stop;
1479 /* If the section is known good, skip it */
1480 if (match(secname, section_white_list))
1483 start = sym_get_data_by_offset(elf, i, 0);
1484 stop = start + sechdr->sh_size;
1486 if (sechdr->sh_type == SHT_RELA)
1487 section_rela(mod, elf, secndx, secname,
1490 section_rel(mod, elf, secndx, secname,
1496 static char *remove_dot(char *s)
1498 size_t n = strcspn(s, ".");
1501 size_t m = strspn(s + n + 1, "0123456789");
1502 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
1509 * The CRCs are recorded in .*.cmd files in the form of:
1510 * #SYMVER <name> <crc>
1512 static void extract_crcs_for_object(const char *object, struct module *mod)
1514 char cmd_file[PATH_MAX];
1519 base = strrchr(object, '/');
1522 dirlen = base - object;
1528 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
1529 dirlen, object, base);
1530 if (ret >= sizeof(cmd_file)) {
1531 error("%s: too long path was truncated\n", cmd_file);
1535 buf = read_text_file(cmd_file);
1538 while ((p = strstr(p, "\n#SYMVER "))) {
1544 name = p + strlen("\n#SYMVER ");
1546 p = strchr(name, ' ');
1554 continue; /* skip this line */
1556 crc = strtoul(p, &p, 0);
1558 continue; /* skip this line */
1560 name[namelen] = '\0';
1563 * sym_find_with_module() may return NULL here.
1564 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
1565 * Since commit e1327a127703, genksyms calculates CRCs of all
1566 * symbols, including trimmed ones. Ignore orphan CRCs.
1568 sym = sym_find_with_module(name, mod);
1570 sym_set_crc(sym, crc);
1577 * The symbol versions (CRC) are recorded in the .*.cmd files.
1578 * Parse them to retrieve CRCs for the current module.
1580 static void mod_set_crcs(struct module *mod)
1582 char objlist[PATH_MAX];
1583 char *buf, *p, *obj;
1586 if (mod->is_vmlinux) {
1587 strcpy(objlist, ".vmlinux.objs");
1589 /* objects for a module are listed in the *.mod file. */
1590 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
1591 if (ret >= sizeof(objlist)) {
1592 error("%s: too long path was truncated\n", objlist);
1597 buf = read_text_file(objlist);
1600 while ((obj = strsep(&p, "\n")) && obj[0])
1601 extract_crcs_for_object(obj, mod);
1606 static void read_symbols(const char *modname)
1608 const char *symname;
1613 struct elf_info info = { };
1616 if (!parse_elf(&info, modname))
1619 if (!strends(modname, ".o")) {
1620 error("%s: filename must be suffixed with .o\n", modname);
1624 /* strip trailing .o */
1625 mod = new_module(modname, strlen(modname) - strlen(".o"));
1627 if (!mod->is_vmlinux) {
1628 license = get_modinfo(&info, "license");
1630 error("missing MODULE_LICENSE() in %s\n", modname);
1632 if (!license_is_gpl_compatible(license)) {
1633 mod->is_gpl_compatible = false;
1636 license = get_next_modinfo(&info, "license", license);
1639 namespace = get_modinfo(&info, "import_ns");
1641 add_namespace(&mod->imported_namespaces, namespace);
1642 namespace = get_next_modinfo(&info, "import_ns",
1647 if (extra_warn && !get_modinfo(&info, "description"))
1648 warn("missing MODULE_DESCRIPTION() in %s\n", modname);
1649 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1650 symname = remove_dot(info.strtab + sym->st_name);
1652 handle_symbol(mod, &info, sym, symname);
1653 handle_moddevtable(mod, &info, sym, symname);
1656 check_sec_ref(mod, &info);
1658 if (!mod->is_vmlinux) {
1659 version = get_modinfo(&info, "version");
1660 if (version || all_versions)
1661 get_src_version(mod->name, mod->srcversion,
1662 sizeof(mod->srcversion) - 1);
1665 parse_elf_finish(&info);
1669 * Our trick to get versioning for module struct etc. - it's
1670 * never passed as an argument to an exported function, so
1671 * the automatic versioning doesn't pick it up, but it's really
1674 sym_add_unresolved("module_layout", mod, false);
1680 static void read_symbols_from_files(const char *filename)
1683 char fname[PATH_MAX];
1685 in = fopen(filename, "r");
1687 fatal("Can't open filenames file %s: %m", filename);
1689 while (fgets(fname, PATH_MAX, in) != NULL) {
1690 if (strends(fname, "\n"))
1691 fname[strlen(fname)-1] = '\0';
1692 read_symbols(fname);
1700 /* We first write the generated file into memory using the
1701 * following helper, then compare to the file on disk and
1702 * only update the later if anything changed */
1704 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1705 const char *fmt, ...)
1712 len = vsnprintf(tmp, SZ, fmt, ap);
1713 buf_write(buf, tmp, len);
1717 void buf_write(struct buffer *buf, const char *s, int len)
1719 if (buf->size - buf->pos < len) {
1720 buf->size += len + SZ;
1721 buf->p = NOFAIL(realloc(buf->p, buf->size));
1723 strncpy(buf->p + buf->pos, s, len);
1727 static void check_exports(struct module *mod)
1729 struct symbol *s, *exp;
1731 list_for_each_entry(s, &mod->unresolved_symbols, list) {
1732 const char *basename;
1733 exp = find_symbol(s->name);
1735 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
1736 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
1737 "\"%s\" [%s.ko] undefined!\n",
1738 s->name, mod->name);
1741 if (exp->module == mod) {
1742 error("\"%s\" [%s.ko] was exported without definition\n",
1743 s->name, mod->name);
1748 s->module = exp->module;
1749 s->crc_valid = exp->crc_valid;
1752 basename = strrchr(mod->name, '/');
1756 basename = mod->name;
1758 if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) {
1759 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
1760 "module %s uses symbol %s from namespace %s, but does not import it.\n",
1761 basename, exp->name, exp->namespace);
1762 add_namespace(&mod->missing_namespaces, exp->namespace);
1765 if (!mod->is_gpl_compatible && exp->is_gpl_only)
1766 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
1767 basename, exp->name);
1771 static void handle_white_list_exports(const char *white_list)
1773 char *buf, *p, *name;
1775 buf = read_text_file(white_list);
1778 while ((name = strsep(&p, "\n"))) {
1779 struct symbol *sym = find_symbol(name);
1788 static void check_modname_len(struct module *mod)
1790 const char *mod_name;
1792 mod_name = strrchr(mod->name, '/');
1793 if (mod_name == NULL)
1794 mod_name = mod->name;
1797 if (strlen(mod_name) >= MODULE_NAME_LEN)
1798 error("module name is too long [%s.ko]\n", mod->name);
1802 * Header for the generated file
1804 static void add_header(struct buffer *b, struct module *mod)
1806 buf_printf(b, "#include <linux/module.h>\n");
1808 * Include build-salt.h after module.h in order to
1809 * inherit the definitions.
1811 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
1812 buf_printf(b, "#include <linux/build-salt.h>\n");
1813 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
1814 buf_printf(b, "#include <linux/export-internal.h>\n");
1815 buf_printf(b, "#include <linux/vermagic.h>\n");
1816 buf_printf(b, "#include <linux/compiler.h>\n");
1817 buf_printf(b, "\n");
1818 buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n");
1819 buf_printf(b, "#include <asm/orc_header.h>\n");
1820 buf_printf(b, "ORC_HEADER;\n");
1821 buf_printf(b, "#endif\n");
1822 buf_printf(b, "\n");
1823 buf_printf(b, "BUILD_SALT;\n");
1824 buf_printf(b, "BUILD_LTO_INFO;\n");
1825 buf_printf(b, "\n");
1826 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1827 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
1828 buf_printf(b, "\n");
1829 buf_printf(b, "__visible struct module __this_module\n");
1830 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
1831 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
1833 buf_printf(b, "\t.init = init_module,\n");
1834 if (mod->has_cleanup)
1835 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1836 "\t.exit = cleanup_module,\n"
1838 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
1839 buf_printf(b, "};\n");
1841 if (!external_module)
1842 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1846 "#ifdef CONFIG_RETPOLINE\n"
1847 "MODULE_INFO(retpoline, \"Y\");\n"
1850 if (strstarts(mod->name, "drivers/staging"))
1851 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1853 if (strstarts(mod->name, "tools/testing"))
1854 buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
1857 static void add_exported_symbols(struct buffer *buf, struct module *mod)
1861 /* generate struct for exported symbols */
1862 buf_printf(buf, "\n");
1863 list_for_each_entry(sym, &mod->exported_symbols, list) {
1864 if (trim_unused_exports && !sym->used)
1867 buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n",
1868 sym->is_func ? "FUNC" : "DATA", sym->name,
1869 sym->is_gpl_only ? "_gpl" : "", sym->namespace);
1875 /* record CRCs for exported symbols */
1876 buf_printf(buf, "\n");
1877 list_for_each_entry(sym, &mod->exported_symbols, list) {
1878 if (trim_unused_exports && !sym->used)
1881 if (!sym->crc_valid)
1882 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
1883 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
1884 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
1887 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
1888 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
1893 * Record CRCs for unresolved symbols
1895 static void add_versions(struct buffer *b, struct module *mod)
1902 buf_printf(b, "\n");
1903 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1904 buf_printf(b, "__used __section(\"__versions\") = {\n");
1906 list_for_each_entry(s, &mod->unresolved_symbols, list) {
1909 if (!s->crc_valid) {
1910 warn("\"%s\" [%s.ko] has no CRC!\n",
1911 s->name, mod->name);
1914 if (strlen(s->name) >= MODULE_NAME_LEN) {
1915 error("too long symbol \"%s\" [%s.ko]\n",
1916 s->name, mod->name);
1919 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
1923 buf_printf(b, "};\n");
1926 static void add_depends(struct buffer *b, struct module *mod)
1931 /* Clear ->seen flag of modules that own symbols needed by this. */
1932 list_for_each_entry(s, &mod->unresolved_symbols, list) {
1934 s->module->seen = s->module->is_vmlinux;
1937 buf_printf(b, "\n");
1938 buf_printf(b, "MODULE_INFO(depends, \"");
1939 list_for_each_entry(s, &mod->unresolved_symbols, list) {
1944 if (s->module->seen)
1947 s->module->seen = true;
1948 p = strrchr(s->module->name, '/');
1952 p = s->module->name;
1953 buf_printf(b, "%s%s", first ? "" : ",", p);
1956 buf_printf(b, "\");\n");
1959 static void add_srcversion(struct buffer *b, struct module *mod)
1961 if (mod->srcversion[0]) {
1962 buf_printf(b, "\n");
1963 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1968 static void write_buf(struct buffer *b, const char *fname)
1975 file = fopen(fname, "w");
1980 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1984 if (fclose(file) != 0) {
1990 static void write_if_changed(struct buffer *b, const char *fname)
1996 file = fopen(fname, "r");
2000 if (fstat(fileno(file), &st) < 0)
2003 if (st.st_size != b->pos)
2006 tmp = NOFAIL(malloc(b->pos));
2007 if (fread(tmp, 1, b->pos, file) != b->pos)
2010 if (memcmp(tmp, b->p, b->pos) != 0)
2022 write_buf(b, fname);
2025 static void write_vmlinux_export_c_file(struct module *mod)
2027 struct buffer buf = { };
2030 "#include <linux/export-internal.h>\n");
2032 add_exported_symbols(&buf, mod);
2033 write_if_changed(&buf, ".vmlinux.export.c");
2037 /* do sanity checks, and generate *.mod.c file */
2038 static void write_mod_c_file(struct module *mod)
2040 struct buffer buf = { };
2041 char fname[PATH_MAX];
2044 add_header(&buf, mod);
2045 add_exported_symbols(&buf, mod);
2046 add_versions(&buf, mod);
2047 add_depends(&buf, mod);
2048 add_moddevtable(&buf, mod);
2049 add_srcversion(&buf, mod);
2051 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2052 if (ret >= sizeof(fname)) {
2053 error("%s: too long path was truncated\n", fname);
2057 write_if_changed(&buf, fname);
2063 /* parse Module.symvers file. line format:
2064 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2066 static void read_dump(const char *fname)
2068 char *buf, *pos, *line;
2070 buf = read_text_file(fname);
2072 /* No symbol versions, silently ignore */
2077 while ((line = get_line(&pos))) {
2078 char *symname, *namespace, *modname, *d, *export;
2084 if (!(symname = strchr(line, '\t')))
2087 if (!(modname = strchr(symname, '\t')))
2090 if (!(export = strchr(modname, '\t')))
2093 if (!(namespace = strchr(export, '\t')))
2095 *namespace++ = '\0';
2097 crc = strtoul(line, &d, 16);
2098 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2101 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2103 } else if (!strcmp(export, "EXPORT_SYMBOL")) {
2106 error("%s: unknown license %s. skip", symname, export);
2110 mod = find_module(modname);
2112 mod = new_module(modname, strlen(modname));
2113 mod->from_dump = true;
2115 s = sym_add_exported(symname, mod, gpl_only, namespace);
2116 sym_set_crc(s, crc);
2122 fatal("parse error in symbol dump file\n");
2125 static void write_dump(const char *fname)
2127 struct buffer buf = { };
2131 list_for_each_entry(mod, &modules, list) {
2134 list_for_each_entry(sym, &mod->exported_symbols, list) {
2135 if (trim_unused_exports && !sym->used)
2138 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
2139 sym->crc, sym->name, mod->name,
2140 sym->is_gpl_only ? "_GPL" : "",
2144 write_buf(&buf, fname);
2148 static void write_namespace_deps_files(const char *fname)
2151 struct namespace_list *ns;
2152 struct buffer ns_deps_buf = {};
2154 list_for_each_entry(mod, &modules, list) {
2156 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2159 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2161 list_for_each_entry(ns, &mod->missing_namespaces, list)
2162 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2164 buf_printf(&ns_deps_buf, "\n");
2167 write_if_changed(&ns_deps_buf, fname);
2168 free(ns_deps_buf.p);
2172 struct list_head list;
2176 int main(int argc, char **argv)
2179 char *missing_namespace_deps = NULL;
2180 char *unused_exports_white_list = NULL;
2181 char *dump_write = NULL, *files_source = NULL;
2183 LIST_HEAD(dump_lists);
2184 struct dump_list *dl, *dl2;
2186 while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:")) != -1) {
2189 external_module = true;
2192 dl = NOFAIL(malloc(sizeof(*dl)));
2194 list_add_tail(&dl->list, &dump_lists);
2197 module_enabled = true;
2203 ignore_missing_files = true;
2206 dump_write = optarg;
2209 all_versions = true;
2212 files_source = optarg;
2215 trim_unused_exports = true;
2218 unused_exports_white_list = optarg;
2224 warn_unresolved = true;
2227 sec_mismatch_warn_only = false;
2230 allow_missing_ns_imports = true;
2233 missing_namespace_deps = optarg;
2240 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2241 read_dump(dl->file);
2242 list_del(&dl->list);
2246 while (optind < argc)
2247 read_symbols(argv[optind++]);
2250 read_symbols_from_files(files_source);
2252 list_for_each_entry(mod, &modules, list) {
2253 if (mod->from_dump || mod->is_vmlinux)
2256 check_modname_len(mod);
2260 if (unused_exports_white_list)
2261 handle_white_list_exports(unused_exports_white_list);
2263 list_for_each_entry(mod, &modules, list) {
2267 if (mod->is_vmlinux)
2268 write_vmlinux_export_c_file(mod);
2270 write_mod_c_file(mod);
2273 if (missing_namespace_deps)
2274 write_namespace_deps_files(missing_namespace_deps);
2277 write_dump(dump_write);
2278 if (sec_mismatch_count && !sec_mismatch_warn_only)
2279 error("Section mismatches detected.\n"
2280 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2282 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2283 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2284 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2286 return error_occurred ? 1 : 0;