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"
25 #include "../../include/linux/module_symbol.h"
27 static bool module_enabled
;
28 /* Are we using CONFIG_MODVERSIONS? */
29 static bool modversions
;
30 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
31 static bool all_versions
;
32 /* If we are modposting external module set to 1 */
33 static bool external_module
;
34 /* Only warn about unresolved symbols */
35 static bool warn_unresolved
;
37 static int sec_mismatch_count
;
38 static bool sec_mismatch_warn_only
= true;
39 /* Trim EXPORT_SYMBOLs that are unused by in-tree modules */
40 static bool trim_unused_exports
;
42 /* ignore missing files */
43 static bool ignore_missing_files
;
44 /* If set to 1, only warn (instead of error) about missing ns imports */
45 static bool allow_missing_ns_imports
;
47 static bool error_occurred
;
49 static bool extra_warn
;
52 * Cut off the warnings when there are too many. This typically occurs when
53 * vmlinux is missing. ('make modules' without building vmlinux.)
55 #define MAX_UNRESOLVED_REPORTS 10
56 static unsigned int nr_unresolved
;
58 /* In kernel, this size is defined in linux/module.h;
59 * here we use Elf_Addr instead of long for covering cross-compile
62 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
64 void __attribute__((format(printf
, 2, 3)))
65 modpost_log(enum loglevel loglevel
, const char *fmt
, ...)
71 fprintf(stderr
, "WARNING: ");
74 fprintf(stderr
, "ERROR: ");
77 fprintf(stderr
, "FATAL: ");
79 default: /* invalid loglevel, ignore */
83 fprintf(stderr
, "modpost: ");
85 va_start(arglist
, fmt
);
86 vfprintf(stderr
, fmt
, arglist
);
89 if (loglevel
== LOG_FATAL
)
91 if (loglevel
== LOG_ERROR
)
92 error_occurred
= true;
95 static inline bool strends(const char *str
, const char *postfix
)
97 if (strlen(str
) < strlen(postfix
))
100 return strcmp(str
+ strlen(str
) - strlen(postfix
), postfix
) == 0;
103 void *do_nofail(void *ptr
, const char *expr
)
106 fatal("Memory allocation failure: %s.\n", expr
);
111 char *read_text_file(const char *filename
)
118 fd
= open(filename
, O_RDONLY
);
124 if (fstat(fd
, &st
) < 0) {
129 buf
= NOFAIL(malloc(st
.st_size
+ 1));
136 bytes_read
= read(fd
, buf
, nbytes
);
137 if (bytes_read
< 0) {
142 nbytes
-= bytes_read
;
144 buf
[st
.st_size
] = '\0';
151 char *get_line(char **stringp
)
153 char *orig
= *stringp
, *next
;
155 /* do not return the unwanted extra line at EOF */
156 if (!orig
|| *orig
== '\0')
159 /* don't use strsep here, it is not available everywhere */
160 next
= strchr(orig
, '\n');
169 /* A list of all modules we processed */
172 static struct module
*find_module(const char *modname
)
176 list_for_each_entry(mod
, &modules
, list
) {
177 if (strcmp(mod
->name
, modname
) == 0)
183 static struct module
*new_module(const char *name
, size_t namelen
)
187 mod
= NOFAIL(malloc(sizeof(*mod
) + namelen
+ 1));
188 memset(mod
, 0, sizeof(*mod
));
190 INIT_LIST_HEAD(&mod
->exported_symbols
);
191 INIT_LIST_HEAD(&mod
->unresolved_symbols
);
192 INIT_LIST_HEAD(&mod
->missing_namespaces
);
193 INIT_LIST_HEAD(&mod
->imported_namespaces
);
195 memcpy(mod
->name
, name
, namelen
);
196 mod
->name
[namelen
] = '\0';
197 mod
->is_vmlinux
= (strcmp(mod
->name
, "vmlinux") == 0);
200 * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
201 * is missing, do not check the use for EXPORT_SYMBOL_GPL() becasue
202 * modpost will exit wiht error anyway.
204 mod
->is_gpl_compatible
= true;
206 list_add_tail(&mod
->list
, &modules
);
211 /* A hash of all exported symbols,
212 * struct symbol is also used for lists of unresolved symbols */
214 #define SYMBOL_HASH_SIZE 1024
218 struct list_head list
; /* link to module::exported_symbols or module::unresolved_symbols */
219 struct module
*module
;
225 bool is_gpl_only
; /* exported by EXPORT_SYMBOL_GPL */
226 bool used
; /* there exists a user of this symbol */
230 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
232 /* This is based on the hash algorithm from gdbm, via tdb */
233 static inline unsigned int tdb_hash(const char *name
)
235 unsigned value
; /* Used to compute the hash value. */
236 unsigned i
; /* Used to cycle through random values. */
238 /* Set the initial value from the key size. */
239 for (value
= 0x238F13AF * strlen(name
), i
= 0; name
[i
]; i
++)
240 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
242 return (1103515243 * value
+ 12345);
246 * Allocate a new symbols for use in the hash of exported symbols or
247 * the list of unresolved symbols per module
249 static struct symbol
*alloc_symbol(const char *name
)
251 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
253 memset(s
, 0, sizeof(*s
));
254 strcpy(s
->name
, name
);
259 /* For the hash of exported symbols */
260 static void hash_add_symbol(struct symbol
*sym
)
264 hash
= tdb_hash(sym
->name
) % SYMBOL_HASH_SIZE
;
265 sym
->next
= symbolhash
[hash
];
266 symbolhash
[hash
] = sym
;
269 static void sym_add_unresolved(const char *name
, struct module
*mod
, bool weak
)
273 sym
= alloc_symbol(name
);
276 list_add_tail(&sym
->list
, &mod
->unresolved_symbols
);
279 static struct symbol
*sym_find_with_module(const char *name
, struct module
*mod
)
283 /* For our purposes, .foo matches foo. PPC64 needs this. */
287 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
= s
->next
) {
288 if (strcmp(s
->name
, name
) == 0 && (!mod
|| s
->module
== mod
))
294 static struct symbol
*find_symbol(const char *name
)
296 return sym_find_with_module(name
, NULL
);
299 struct namespace_list
{
300 struct list_head list
;
304 static bool contains_namespace(struct list_head
*head
, const char *namespace)
306 struct namespace_list
*list
;
309 * The default namespace is null string "", which is always implicitly
315 list_for_each_entry(list
, head
, list
) {
316 if (!strcmp(list
->namespace, namespace))
323 static void add_namespace(struct list_head
*head
, const char *namespace)
325 struct namespace_list
*ns_entry
;
327 if (!contains_namespace(head
, namespace)) {
328 ns_entry
= NOFAIL(malloc(sizeof(*ns_entry
) +
329 strlen(namespace) + 1));
330 strcpy(ns_entry
->namespace, namespace);
331 list_add_tail(&ns_entry
->list
, head
);
335 static void *sym_get_data_by_offset(const struct elf_info
*info
,
336 unsigned int secindex
, unsigned long offset
)
338 Elf_Shdr
*sechdr
= &info
->sechdrs
[secindex
];
340 return (void *)info
->hdr
+ sechdr
->sh_offset
+ offset
;
343 void *sym_get_data(const struct elf_info
*info
, const Elf_Sym
*sym
)
345 return sym_get_data_by_offset(info
, get_secindex(info
, sym
),
349 static const char *sech_name(const struct elf_info
*info
, Elf_Shdr
*sechdr
)
351 return sym_get_data_by_offset(info
, info
->secindex_strings
,
355 static const char *sec_name(const struct elf_info
*info
, unsigned int secindex
)
358 * If sym->st_shndx is a special section index, there is no
359 * corresponding section header.
360 * Return "" if the index is out of range of info->sechdrs[] array.
362 if (secindex
>= info
->num_sections
)
365 return sech_name(info
, &info
->sechdrs
[secindex
]);
368 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
370 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
371 bool gpl_only
, const char *namespace)
373 struct symbol
*s
= find_symbol(name
);
375 if (s
&& (!external_module
|| s
->module
->is_vmlinux
|| s
->module
== mod
)) {
376 error("%s: '%s' exported twice. Previous export was in %s%s\n",
377 mod
->name
, name
, s
->module
->name
,
378 s
->module
->is_vmlinux
? "" : ".ko");
381 s
= alloc_symbol(name
);
383 s
->is_gpl_only
= gpl_only
;
384 s
->namespace = NOFAIL(strdup(namespace));
385 list_add_tail(&s
->list
, &mod
->exported_symbols
);
391 static void sym_set_crc(struct symbol
*sym
, unsigned int crc
)
394 sym
->crc_valid
= true;
397 static void *grab_file(const char *filename
, size_t *size
)
400 void *map
= MAP_FAILED
;
403 fd
= open(filename
, O_RDONLY
);
410 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
414 if (map
== MAP_FAILED
)
419 static void release_file(void *file
, size_t size
)
424 static int parse_elf(struct elf_info
*info
, const char *filename
)
430 const char *secstrings
;
431 unsigned int symtab_idx
= ~0U, symtab_shndx_idx
= ~0U;
433 hdr
= grab_file(filename
, &info
->size
);
435 if (ignore_missing_files
) {
436 fprintf(stderr
, "%s: %s (ignored)\n", filename
,
444 if (info
->size
< sizeof(*hdr
)) {
445 /* file too small, assume this is an empty .o file */
448 /* Is this a valid ELF file? */
449 if ((hdr
->e_ident
[EI_MAG0
] != ELFMAG0
) ||
450 (hdr
->e_ident
[EI_MAG1
] != ELFMAG1
) ||
451 (hdr
->e_ident
[EI_MAG2
] != ELFMAG2
) ||
452 (hdr
->e_ident
[EI_MAG3
] != ELFMAG3
)) {
453 /* Not an ELF file - silently ignore it */
456 /* Fix endianness in ELF header */
457 hdr
->e_type
= TO_NATIVE(hdr
->e_type
);
458 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
459 hdr
->e_version
= TO_NATIVE(hdr
->e_version
);
460 hdr
->e_entry
= TO_NATIVE(hdr
->e_entry
);
461 hdr
->e_phoff
= TO_NATIVE(hdr
->e_phoff
);
462 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
463 hdr
->e_flags
= TO_NATIVE(hdr
->e_flags
);
464 hdr
->e_ehsize
= TO_NATIVE(hdr
->e_ehsize
);
465 hdr
->e_phentsize
= TO_NATIVE(hdr
->e_phentsize
);
466 hdr
->e_phnum
= TO_NATIVE(hdr
->e_phnum
);
467 hdr
->e_shentsize
= TO_NATIVE(hdr
->e_shentsize
);
468 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
469 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
470 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
471 info
->sechdrs
= sechdrs
;
473 /* modpost only works for relocatable objects */
474 if (hdr
->e_type
!= ET_REL
)
475 fatal("%s: not relocatable object.", filename
);
477 /* Check if file offset is correct */
478 if (hdr
->e_shoff
> info
->size
) {
479 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
480 (unsigned long)hdr
->e_shoff
, filename
, info
->size
);
484 if (hdr
->e_shnum
== SHN_UNDEF
) {
486 * There are more than 64k sections,
487 * read count from .sh_size.
489 info
->num_sections
= TO_NATIVE(sechdrs
[0].sh_size
);
492 info
->num_sections
= hdr
->e_shnum
;
494 if (hdr
->e_shstrndx
== SHN_XINDEX
) {
495 info
->secindex_strings
= TO_NATIVE(sechdrs
[0].sh_link
);
498 info
->secindex_strings
= hdr
->e_shstrndx
;
501 /* Fix endianness in section headers */
502 for (i
= 0; i
< info
->num_sections
; i
++) {
503 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
504 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
505 sechdrs
[i
].sh_flags
= TO_NATIVE(sechdrs
[i
].sh_flags
);
506 sechdrs
[i
].sh_addr
= TO_NATIVE(sechdrs
[i
].sh_addr
);
507 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
508 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
509 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
510 sechdrs
[i
].sh_info
= TO_NATIVE(sechdrs
[i
].sh_info
);
511 sechdrs
[i
].sh_addralign
= TO_NATIVE(sechdrs
[i
].sh_addralign
);
512 sechdrs
[i
].sh_entsize
= TO_NATIVE(sechdrs
[i
].sh_entsize
);
514 /* Find symbol table. */
515 secstrings
= (void *)hdr
+ sechdrs
[info
->secindex_strings
].sh_offset
;
516 for (i
= 1; i
< info
->num_sections
; i
++) {
518 int nobits
= sechdrs
[i
].sh_type
== SHT_NOBITS
;
520 if (!nobits
&& sechdrs
[i
].sh_offset
> info
->size
) {
521 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
522 filename
, (unsigned long)sechdrs
[i
].sh_offset
,
526 secname
= secstrings
+ sechdrs
[i
].sh_name
;
527 if (strcmp(secname
, ".modinfo") == 0) {
529 fatal("%s has NOBITS .modinfo\n", filename
);
530 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
531 info
->modinfo_len
= sechdrs
[i
].sh_size
;
532 } else if (!strcmp(secname
, ".export_symbol")) {
533 info
->export_symbol_secndx
= i
;
536 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
537 unsigned int sh_link_idx
;
539 info
->symtab_start
= (void *)hdr
+
540 sechdrs
[i
].sh_offset
;
541 info
->symtab_stop
= (void *)hdr
+
542 sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
;
543 sh_link_idx
= sechdrs
[i
].sh_link
;
544 info
->strtab
= (void *)hdr
+
545 sechdrs
[sh_link_idx
].sh_offset
;
548 /* 32bit section no. table? ("more than 64k sections") */
549 if (sechdrs
[i
].sh_type
== SHT_SYMTAB_SHNDX
) {
550 symtab_shndx_idx
= i
;
551 info
->symtab_shndx_start
= (void *)hdr
+
552 sechdrs
[i
].sh_offset
;
553 info
->symtab_shndx_stop
= (void *)hdr
+
554 sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
;
557 if (!info
->symtab_start
)
558 fatal("%s has no symtab?\n", filename
);
560 /* Fix endianness in symbols */
561 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
562 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
563 sym
->st_name
= TO_NATIVE(sym
->st_name
);
564 sym
->st_value
= TO_NATIVE(sym
->st_value
);
565 sym
->st_size
= TO_NATIVE(sym
->st_size
);
568 if (symtab_shndx_idx
!= ~0U) {
570 if (symtab_idx
!= sechdrs
[symtab_shndx_idx
].sh_link
)
571 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
572 filename
, sechdrs
[symtab_shndx_idx
].sh_link
,
575 for (p
= info
->symtab_shndx_start
; p
< info
->symtab_shndx_stop
;
583 static void parse_elf_finish(struct elf_info
*info
)
585 release_file(info
->hdr
, info
->size
);
588 static int ignore_undef_symbol(struct elf_info
*info
, const char *symname
)
590 /* ignore __this_module, it will be resolved shortly */
591 if (strcmp(symname
, "__this_module") == 0)
593 /* ignore global offset table */
594 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
596 if (info
->hdr
->e_machine
== EM_PPC
)
597 /* Special register function linked on all modules during final link of .ko */
598 if (strstarts(symname
, "_restgpr_") ||
599 strstarts(symname
, "_savegpr_") ||
600 strstarts(symname
, "_rest32gpr_") ||
601 strstarts(symname
, "_save32gpr_") ||
602 strstarts(symname
, "_restvr_") ||
603 strstarts(symname
, "_savevr_"))
605 if (info
->hdr
->e_machine
== EM_PPC64
)
606 /* Special register function linked on all modules during final link of .ko */
607 if (strstarts(symname
, "_restgpr0_") ||
608 strstarts(symname
, "_savegpr0_") ||
609 strstarts(symname
, "_restvr_") ||
610 strstarts(symname
, "_savevr_") ||
611 strcmp(symname
, ".TOC.") == 0)
614 if (info
->hdr
->e_machine
== EM_S390
)
615 /* Expoline thunks are linked on all kernel modules during final link of .ko */
616 if (strstarts(symname
, "__s390_indirect_jump_r"))
618 /* Do not ignore this symbol */
622 static void handle_symbol(struct module
*mod
, struct elf_info
*info
,
623 const Elf_Sym
*sym
, const char *symname
)
625 switch (sym
->st_shndx
) {
627 if (strstarts(symname
, "__gnu_lto_")) {
628 /* Should warn here, but modpost runs before the linker */
630 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
633 /* undefined symbol */
634 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
635 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
637 if (ignore_undef_symbol(info
, symname
))
639 if (info
->hdr
->e_machine
== EM_SPARC
||
640 info
->hdr
->e_machine
== EM_SPARCV9
) {
641 /* Ignore register directives. */
642 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
644 if (symname
[0] == '.') {
645 char *munged
= NOFAIL(strdup(symname
));
647 munged
[1] = toupper(munged
[1]);
652 sym_add_unresolved(symname
, mod
,
653 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
);
656 if (strcmp(symname
, "init_module") == 0)
657 mod
->has_init
= true;
658 if (strcmp(symname
, "cleanup_module") == 0)
659 mod
->has_cleanup
= true;
665 * Parse tag=value strings from .modinfo section
667 static char *next_string(char *string
, unsigned long *secsize
)
669 /* Skip non-zero chars */
672 if ((*secsize
)-- <= 1)
676 /* Skip any zero padding. */
679 if ((*secsize
)-- <= 1)
685 static char *get_next_modinfo(struct elf_info
*info
, const char *tag
,
689 unsigned int taglen
= strlen(tag
);
690 char *modinfo
= info
->modinfo
;
691 unsigned long size
= info
->modinfo_len
;
694 size
-= prev
- modinfo
;
695 modinfo
= next_string(prev
, &size
);
698 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
699 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
700 return p
+ taglen
+ 1;
705 static char *get_modinfo(struct elf_info
*info
, const char *tag
)
708 return get_next_modinfo(info
, tag
, NULL
);
711 static const char *sym_name(struct elf_info
*elf
, Elf_Sym
*sym
)
714 return elf
->strtab
+ sym
->st_name
;
720 * Check whether the 'string' argument matches one of the 'patterns',
721 * an array of shell wildcard patterns (glob).
723 * Return true is there is a match.
725 static bool match(const char *string
, const char *const patterns
[])
729 while ((pattern
= *patterns
++)) {
730 if (!fnmatch(pattern
, string
, 0))
737 /* useful to pass patterns to match() directly */
738 #define PATTERNS(...) \
740 static const char *const patterns[] = {__VA_ARGS__, NULL}; \
744 /* sections that we do not want to do full section mismatch check on */
745 static const char *const section_white_list
[] =
749 ".zdebug*", /* Compressed debug sections. */
750 ".GCC.command.line", /* record-gcc-switches */
751 ".mdebug*", /* alpha, score, mips etc. */
752 ".pdr", /* alpha, score, mips etc. */
757 ".xt.prop", /* xtensa */
758 ".xt.lit", /* xtensa */
759 ".arcextmap*", /* arc */
760 ".gnu.linkonce.arcext*", /* arc : modules */
761 ".cmem*", /* EZchip */
762 ".fmt_slot*", /* EZchip */
765 ".llvm.call-graph-profile", /* call graph */
770 * This is used to find sections missing the SHF_ALLOC flag.
771 * The cause of this is often a section specified in assembler
772 * without "ax" / "aw".
774 static void check_section(const char *modname
, struct elf_info
*elf
,
777 const char *sec
= sech_name(elf
, sechdr
);
779 if (sechdr
->sh_type
== SHT_PROGBITS
&&
780 !(sechdr
->sh_flags
& SHF_ALLOC
) &&
781 !match(sec
, section_white_list
)) {
782 warn("%s (%s): unexpected non-allocatable section.\n"
783 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
784 "Note that for example <linux/init.h> contains\n"
785 "section definitions for use in .S files.\n\n",
792 #define ALL_INIT_DATA_SECTIONS \
793 ".init.setup", ".init.rodata", ".meminit.rodata", \
794 ".init.data", ".meminit.data"
795 #define ALL_EXIT_DATA_SECTIONS \
796 ".exit.data", ".memexit.data"
798 #define ALL_INIT_TEXT_SECTIONS \
799 ".init.text", ".meminit.text"
800 #define ALL_EXIT_TEXT_SECTIONS \
801 ".exit.text", ".memexit.text"
803 #define ALL_PCI_INIT_SECTIONS \
804 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
805 ".pci_fixup_enable", ".pci_fixup_resume", \
806 ".pci_fixup_resume_early", ".pci_fixup_suspend"
808 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
809 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
811 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
812 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
814 #define DATA_SECTIONS ".data", ".data.rel"
815 #define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
816 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
817 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
818 ".fixup", ".entry.text", ".exception.text", \
819 ".coldtext", ".softirqentry.text"
821 #define INIT_SECTIONS ".init.*"
822 #define MEM_INIT_SECTIONS ".meminit.*"
824 #define EXIT_SECTIONS ".exit.*"
825 #define MEM_EXIT_SECTIONS ".memexit.*"
827 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
828 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
833 TEXTDATA_TO_ANY_EXIT
,
834 XXXINIT_TO_SOME_INIT
,
835 XXXEXIT_TO_SOME_EXIT
,
836 ANY_INIT_TO_ANY_EXIT
,
837 ANY_EXIT_TO_ANY_INIT
,
842 * Describe how to match sections on different criteria:
844 * @fromsec: Array of sections to be matched.
846 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
847 * this array is forbidden (black-list). Can be empty.
849 * @good_tosec: Relocations applied to a section in @fromsec must be
850 * targeting sections in this array (white-list). Can be empty.
852 * @mismatch: Type of mismatch.
854 struct sectioncheck
{
855 const char *fromsec
[20];
856 const char *bad_tosec
[20];
857 const char *good_tosec
[20];
858 enum mismatch mismatch
;
861 static const struct sectioncheck sectioncheck
[] = {
862 /* Do not reference init/exit code/data from
863 * normal code and data
866 .fromsec
= { TEXT_SECTIONS
, NULL
},
867 .bad_tosec
= { ALL_INIT_SECTIONS
, NULL
},
868 .mismatch
= TEXT_TO_ANY_INIT
,
871 .fromsec
= { DATA_SECTIONS
, NULL
},
872 .bad_tosec
= { ALL_XXXINIT_SECTIONS
, INIT_SECTIONS
, NULL
},
873 .mismatch
= DATA_TO_ANY_INIT
,
876 .fromsec
= { TEXT_SECTIONS
, DATA_SECTIONS
, NULL
},
877 .bad_tosec
= { ALL_EXIT_SECTIONS
, NULL
},
878 .mismatch
= TEXTDATA_TO_ANY_EXIT
,
880 /* Do not reference init code/data from meminit code/data */
882 .fromsec
= { ALL_XXXINIT_SECTIONS
, NULL
},
883 .bad_tosec
= { INIT_SECTIONS
, NULL
},
884 .mismatch
= XXXINIT_TO_SOME_INIT
,
886 /* Do not reference exit code/data from memexit code/data */
888 .fromsec
= { ALL_XXXEXIT_SECTIONS
, NULL
},
889 .bad_tosec
= { EXIT_SECTIONS
, NULL
},
890 .mismatch
= XXXEXIT_TO_SOME_EXIT
,
892 /* Do not use exit code/data from init code */
894 .fromsec
= { ALL_INIT_SECTIONS
, NULL
},
895 .bad_tosec
= { ALL_EXIT_SECTIONS
, NULL
},
896 .mismatch
= ANY_INIT_TO_ANY_EXIT
,
898 /* Do not use init code/data from exit code */
900 .fromsec
= { ALL_EXIT_SECTIONS
, NULL
},
901 .bad_tosec
= { ALL_INIT_SECTIONS
, NULL
},
902 .mismatch
= ANY_EXIT_TO_ANY_INIT
,
905 .fromsec
= { ALL_PCI_INIT_SECTIONS
, NULL
},
906 .bad_tosec
= { INIT_SECTIONS
, NULL
},
907 .mismatch
= ANY_INIT_TO_ANY_EXIT
,
910 .fromsec
= { "__ex_table", NULL
},
911 /* If you're adding any new black-listed sections in here, consider
912 * adding a special 'printer' for them in scripts/check_extable.
914 .bad_tosec
= { ".altinstr_replacement", NULL
},
915 .good_tosec
= {ALL_TEXT_SECTIONS
, NULL
},
916 .mismatch
= EXTABLE_TO_NON_TEXT
,
920 static const struct sectioncheck
*section_mismatch(
921 const char *fromsec
, const char *tosec
)
926 * The target section could be the SHT_NUL section when we're
927 * handling relocations to un-resolved symbols, trying to match it
928 * doesn't make much sense and causes build failures on parisc
934 for (i
= 0; i
< ARRAY_SIZE(sectioncheck
); i
++) {
935 const struct sectioncheck
*check
= §ioncheck
[i
];
937 if (match(fromsec
, check
->fromsec
)) {
938 if (check
->bad_tosec
[0] && match(tosec
, check
->bad_tosec
))
940 if (check
->good_tosec
[0] && !match(tosec
, check
->good_tosec
))
948 * Whitelist to allow certain references to pass with no warning.
951 * If a module parameter is declared __initdata and permissions=0
952 * then this is legal despite the warning generated.
953 * We cannot see value of permissions here, so just ignore
955 * The pattern is identified by:
961 * module_param_call() ops can refer to __init set function if permissions=0
962 * The pattern is identified by:
965 * atsym = __param_ops_*
968 * Whitelist all references from .head.text to any init section
971 * Some symbols belong to init section but still it is ok to reference
972 * these from non-init sections as these symbols don't have any memory
973 * allocated for them and symbol address and value are same. So even
974 * if init section is freed, its ok to reference those symbols.
975 * For ex. symbols marking the init section boundaries.
976 * This pattern is identified by
977 * refsymname = __init_begin, _sinittext, _einittext
980 * GCC may optimize static inlines when fed constant arg(s) resulting
981 * in functions like cpumask_empty() -- generating an associated symbol
982 * cpumask_empty.constprop.3 that appears in the audit. If the const that
983 * is passed in comes from __init, like say nmi_ipi_mask, we get a
984 * meaningless section warning. May need to add isra symbols too...
985 * This pattern is identified by
986 * tosec = init section
987 * fromsec = text section
988 * refsymname = *.constprop.*
991 static int secref_whitelist(const char *fromsec
, const char *fromsym
,
992 const char *tosec
, const char *tosym
)
994 /* Check for pattern 1 */
995 if (match(tosec
, PATTERNS(ALL_INIT_DATA_SECTIONS
)) &&
996 match(fromsec
, PATTERNS(DATA_SECTIONS
)) &&
997 strstarts(fromsym
, "__param"))
1000 /* Check for pattern 1a */
1001 if (strcmp(tosec
, ".init.text") == 0 &&
1002 match(fromsec
, PATTERNS(DATA_SECTIONS
)) &&
1003 strstarts(fromsym
, "__param_ops_"))
1006 /* symbols in data sections that may refer to any init/exit sections */
1007 if (match(fromsec
, PATTERNS(DATA_SECTIONS
)) &&
1008 match(tosec
, PATTERNS(ALL_INIT_SECTIONS
, ALL_EXIT_SECTIONS
)) &&
1009 match(fromsym
, PATTERNS("*_template", // scsi uses *_template a lot
1010 "*_timer", // arm uses ops structures named _timer a lot
1011 "*_sht", // scsi also used *_sht to some extent
1018 /* symbols in data sections that may refer to meminit/exit sections */
1019 if (match(fromsec
, PATTERNS(DATA_SECTIONS
)) &&
1020 match(tosec
, PATTERNS(ALL_XXXINIT_SECTIONS
, ALL_EXIT_SECTIONS
)) &&
1021 match(fromsym
, PATTERNS("*driver")))
1024 /* Check for pattern 3 */
1025 if (strstarts(fromsec
, ".head.text") &&
1026 match(tosec
, PATTERNS(ALL_INIT_SECTIONS
)))
1029 /* Check for pattern 4 */
1030 if (match(tosym
, PATTERNS("__init_begin", "_sinittext", "_einittext")))
1033 /* Check for pattern 5 */
1034 if (match(fromsec
, PATTERNS(ALL_TEXT_SECTIONS
)) &&
1035 match(tosec
, PATTERNS(ALL_INIT_SECTIONS
)) &&
1036 match(fromsym
, PATTERNS("*.constprop.*")))
1043 * If there's no name there, ignore it; likewise, ignore it if it's
1044 * one of the magic symbols emitted used by current tools.
1046 * Otherwise if find_symbols_between() returns those symbols, they'll
1047 * fail the whitelist tests and cause lots of false alarms ... fixable
1048 * only by merging __exit and __init sections into __text, bloating
1049 * the kernel (which is especially evil on embedded platforms).
1051 static inline int is_valid_name(struct elf_info
*elf
, Elf_Sym
*sym
)
1053 const char *name
= elf
->strtab
+ sym
->st_name
;
1055 if (!name
|| !strlen(name
))
1057 return !is_mapping_symbol(name
);
1060 /* Look up the nearest symbol based on the section and the address */
1061 static Elf_Sym
*find_nearest_sym(struct elf_info
*elf
, Elf_Addr addr
,
1062 unsigned int secndx
, bool allow_negative
,
1063 Elf_Addr min_distance
)
1066 Elf_Sym
*near
= NULL
;
1067 Elf_Addr sym_addr
, distance
;
1068 bool is_arm
= (elf
->hdr
->e_machine
== EM_ARM
);
1070 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
1071 if (get_secindex(elf
, sym
) != secndx
)
1073 if (!is_valid_name(elf
, sym
))
1076 sym_addr
= sym
->st_value
;
1079 * For ARM Thumb instruction, the bit 0 of st_value is set
1080 * if the symbol is STT_FUNC type. Mask it to get the address.
1082 if (is_arm
&& ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
)
1085 if (addr
>= sym_addr
)
1086 distance
= addr
- sym_addr
;
1087 else if (allow_negative
)
1088 distance
= sym_addr
- addr
;
1092 if (distance
<= min_distance
) {
1093 min_distance
= distance
;
1097 if (min_distance
== 0)
1103 static Elf_Sym
*find_fromsym(struct elf_info
*elf
, Elf_Addr addr
,
1104 unsigned int secndx
)
1106 return find_nearest_sym(elf
, addr
, secndx
, false, ~0);
1109 static Elf_Sym
*find_tosym(struct elf_info
*elf
, Elf_Addr addr
, Elf_Sym
*sym
)
1111 /* If the supplied symbol has a valid name, return it */
1112 if (is_valid_name(elf
, sym
))
1116 * Strive to find a better symbol name, but the resulting name may not
1117 * match the symbol referenced in the original code.
1119 return find_nearest_sym(elf
, addr
, get_secindex(elf
, sym
), true, 20);
1122 static bool is_executable_section(struct elf_info
*elf
, unsigned int secndx
)
1124 if (secndx
>= elf
->num_sections
)
1127 return (elf
->sechdrs
[secndx
].sh_flags
& SHF_EXECINSTR
) != 0;
1130 static void default_mismatch_handler(const char *modname
, struct elf_info
*elf
,
1131 const struct sectioncheck
* const mismatch
,
1133 unsigned int fsecndx
, const char *fromsec
, Elf_Addr faddr
,
1134 const char *tosec
, Elf_Addr taddr
)
1138 const char *fromsym
;
1140 from
= find_fromsym(elf
, faddr
, fsecndx
);
1141 fromsym
= sym_name(elf
, from
);
1143 tsym
= find_tosym(elf
, taddr
, tsym
);
1144 tosym
= sym_name(elf
, tsym
);
1146 /* check whitelist - we may ignore it */
1147 if (!secref_whitelist(fromsec
, fromsym
, tosec
, tosym
))
1150 sec_mismatch_count
++;
1152 warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n",
1153 modname
, fromsym
, (unsigned int)(faddr
- from
->st_value
), fromsec
, tosym
, tosec
);
1155 if (mismatch
->mismatch
== EXTABLE_TO_NON_TEXT
) {
1156 if (match(tosec
, mismatch
->bad_tosec
))
1157 fatal("The relocation at %s+0x%lx references\n"
1158 "section \"%s\" which is black-listed.\n"
1159 "Something is seriously wrong and should be fixed.\n"
1160 "You might get more information about where this is\n"
1161 "coming from by using scripts/check_extable.sh %s\n",
1162 fromsec
, (long)faddr
, tosec
, modname
);
1163 else if (is_executable_section(elf
, get_secindex(elf
, tsym
)))
1164 warn("The relocation at %s+0x%lx references\n"
1165 "section \"%s\" which is not in the list of\n"
1166 "authorized sections. If you're adding a new section\n"
1167 "and/or if this reference is valid, add \"%s\" to the\n"
1168 "list of authorized sections to jump to on fault.\n"
1169 "This can be achieved by adding \"%s\" to\n"
1170 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1171 fromsec
, (long)faddr
, tosec
, tosec
, tosec
);
1173 error("%s+0x%lx references non-executable section '%s'\n",
1174 fromsec
, (long)faddr
, tosec
);
1178 static void check_export_symbol(struct module
*mod
, struct elf_info
*elf
,
1179 Elf_Addr faddr
, const char *secname
,
1182 static const char *prefix
= "__export_symbol_";
1183 const char *label_name
, *name
, *data
;
1188 label
= find_fromsym(elf
, faddr
, elf
->export_symbol_secndx
);
1189 label_name
= sym_name(elf
, label
);
1191 if (!strstarts(label_name
, prefix
)) {
1192 error("%s: .export_symbol section contains strange symbol '%s'\n",
1193 mod
->name
, label_name
);
1197 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
1198 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
) {
1199 error("%s: local symbol '%s' was exported\n", mod
->name
,
1200 label_name
+ strlen(prefix
));
1204 name
= sym_name(elf
, sym
);
1205 if (strcmp(label_name
+ strlen(prefix
), name
)) {
1206 error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",
1211 data
= sym_get_data(elf
, label
); /* license */
1212 if (!strcmp(data
, "GPL")) {
1214 } else if (!strcmp(data
, "")) {
1217 error("%s: unknown license '%s' was specified for '%s'\n",
1218 mod
->name
, data
, name
);
1222 data
+= strlen(data
) + 1; /* namespace */
1223 s
= sym_add_exported(name
, mod
, is_gpl
, data
);
1226 * We need to be aware whether we are exporting a function or
1227 * a data on some architectures.
1229 s
->is_func
= (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
);
1232 * For parisc64, symbols prefixed $$ from the library have the symbol type
1233 * STT_LOPROC. They should be handled as functions too.
1235 if (elf
->hdr
->e_ident
[EI_CLASS
] == ELFCLASS64
&&
1236 elf
->hdr
->e_machine
== EM_PARISC
&&
1237 ELF_ST_TYPE(sym
->st_info
) == STT_LOPROC
)
1240 if (match(secname
, PATTERNS(INIT_SECTIONS
)))
1241 warn("%s: %s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_SYMBOL.\n",
1243 else if (match(secname
, PATTERNS(EXIT_SECTIONS
)))
1244 warn("%s: %s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_SYMBOL.\n",
1248 static void check_section_mismatch(struct module
*mod
, struct elf_info
*elf
,
1250 unsigned int fsecndx
, const char *fromsec
,
1251 Elf_Addr faddr
, Elf_Addr taddr
)
1253 const char *tosec
= sec_name(elf
, get_secindex(elf
, sym
));
1254 const struct sectioncheck
*mismatch
;
1256 if (module_enabled
&& elf
->export_symbol_secndx
== fsecndx
) {
1257 check_export_symbol(mod
, elf
, faddr
, tosec
, sym
);
1261 mismatch
= section_mismatch(fromsec
, tosec
);
1265 default_mismatch_handler(mod
->name
, elf
, mismatch
, sym
,
1266 fsecndx
, fromsec
, faddr
,
1270 static Elf_Addr
addend_386_rel(uint32_t *location
, unsigned int r_type
)
1274 return TO_NATIVE(*location
);
1276 return TO_NATIVE(*location
) + 4;
1279 return (Elf_Addr
)(-1);
1283 #define R_ARM_CALL 28
1285 #ifndef R_ARM_JUMP24
1286 #define R_ARM_JUMP24 29
1289 #ifndef R_ARM_THM_CALL
1290 #define R_ARM_THM_CALL 10
1292 #ifndef R_ARM_THM_JUMP24
1293 #define R_ARM_THM_JUMP24 30
1296 #ifndef R_ARM_MOVW_ABS_NC
1297 #define R_ARM_MOVW_ABS_NC 43
1300 #ifndef R_ARM_MOVT_ABS
1301 #define R_ARM_MOVT_ABS 44
1304 #ifndef R_ARM_THM_MOVW_ABS_NC
1305 #define R_ARM_THM_MOVW_ABS_NC 47
1308 #ifndef R_ARM_THM_MOVT_ABS
1309 #define R_ARM_THM_MOVT_ABS 48
1312 #ifndef R_ARM_THM_JUMP19
1313 #define R_ARM_THM_JUMP19 51
1316 static int32_t sign_extend32(int32_t value
, int index
)
1318 uint8_t shift
= 31 - index
;
1320 return (int32_t)(value
<< shift
) >> shift
;
1323 static Elf_Addr
addend_arm_rel(void *loc
, Elf_Sym
*sym
, unsigned int r_type
)
1325 uint32_t inst
, upper
, lower
, sign
, j1
, j2
;
1331 inst
= TO_NATIVE(*(uint32_t *)loc
);
1332 return inst
+ sym
->st_value
;
1333 case R_ARM_MOVW_ABS_NC
:
1334 case R_ARM_MOVT_ABS
:
1335 inst
= TO_NATIVE(*(uint32_t *)loc
);
1336 offset
= sign_extend32(((inst
& 0xf0000) >> 4) | (inst
& 0xfff),
1338 return offset
+ sym
->st_value
;
1342 inst
= TO_NATIVE(*(uint32_t *)loc
);
1343 offset
= sign_extend32((inst
& 0x00ffffff) << 2, 25);
1344 return offset
+ sym
->st_value
+ 8;
1345 case R_ARM_THM_MOVW_ABS_NC
:
1346 case R_ARM_THM_MOVT_ABS
:
1347 upper
= TO_NATIVE(*(uint16_t *)loc
);
1348 lower
= TO_NATIVE(*((uint16_t *)loc
+ 1));
1349 offset
= sign_extend32(((upper
& 0x000f) << 12) |
1350 ((upper
& 0x0400) << 1) |
1351 ((lower
& 0x7000) >> 4) |
1354 return offset
+ sym
->st_value
;
1355 case R_ARM_THM_JUMP19
:
1362 * imm11 = lower[10:0]
1363 * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
1365 upper
= TO_NATIVE(*(uint16_t *)loc
);
1366 lower
= TO_NATIVE(*((uint16_t *)loc
+ 1));
1368 sign
= (upper
>> 10) & 1;
1369 j1
= (lower
>> 13) & 1;
1370 j2
= (lower
>> 11) & 1;
1371 offset
= sign_extend32((sign
<< 20) | (j2
<< 19) | (j1
<< 18) |
1372 ((upper
& 0x03f) << 12) |
1373 ((lower
& 0x07ff) << 1),
1375 return offset
+ sym
->st_value
+ 4;
1376 case R_ARM_THM_CALL
:
1377 case R_ARM_THM_JUMP24
:
1381 * imm10 = upper[9:0]
1384 * imm11 = lower[10:0]
1385 * I1 = NOT(J1 XOR S)
1386 * I2 = NOT(J2 XOR S)
1387 * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
1389 upper
= TO_NATIVE(*(uint16_t *)loc
);
1390 lower
= TO_NATIVE(*((uint16_t *)loc
+ 1));
1392 sign
= (upper
>> 10) & 1;
1393 j1
= (lower
>> 13) & 1;
1394 j2
= (lower
>> 11) & 1;
1395 offset
= sign_extend32((sign
<< 24) |
1396 ((~(j1
^ sign
) & 1) << 23) |
1397 ((~(j2
^ sign
) & 1) << 22) |
1398 ((upper
& 0x03ff) << 12) |
1399 ((lower
& 0x07ff) << 1),
1401 return offset
+ sym
->st_value
+ 4;
1404 return (Elf_Addr
)(-1);
1407 static Elf_Addr
addend_mips_rel(uint32_t *location
, unsigned int r_type
)
1411 inst
= TO_NATIVE(*location
);
1414 return inst
& 0xffff;
1416 return (inst
& 0x03ffffff) << 2;
1420 return (Elf_Addr
)(-1);
1424 #define EM_RISCV 243
1427 #ifndef R_RISCV_SUB32
1428 #define R_RISCV_SUB32 39
1431 #ifndef EM_LOONGARCH
1432 #define EM_LOONGARCH 258
1435 #ifndef R_LARCH_SUB32
1436 #define R_LARCH_SUB32 55
1439 static void get_rel_type_and_sym(struct elf_info
*elf
, uint64_t r_info
,
1440 unsigned int *r_type
, unsigned int *r_sym
)
1443 Elf64_Word r_sym
; /* Symbol index */
1444 unsigned char r_ssym
; /* Special symbol for 2nd relocation */
1445 unsigned char r_type3
; /* 3rd relocation type */
1446 unsigned char r_type2
; /* 2nd relocation type */
1447 unsigned char r_type
; /* 1st relocation type */
1448 } Elf64_Mips_R_Info
;
1450 bool is_64bit
= (elf
->hdr
->e_ident
[EI_CLASS
] == ELFCLASS64
);
1452 if (elf
->hdr
->e_machine
== EM_MIPS
&& is_64bit
) {
1453 Elf64_Mips_R_Info
*mips64_r_info
= (void *)&r_info
;
1455 *r_type
= mips64_r_info
->r_type
;
1456 *r_sym
= TO_NATIVE(mips64_r_info
->r_sym
);
1461 Elf64_Xword r_info64
= r_info
;
1463 r_info
= TO_NATIVE(r_info64
);
1465 Elf32_Word r_info32
= r_info
;
1467 r_info
= TO_NATIVE(r_info32
);
1470 *r_type
= ELF_R_TYPE(r_info
);
1471 *r_sym
= ELF_R_SYM(r_info
);
1474 static void section_rela(struct module
*mod
, struct elf_info
*elf
,
1478 unsigned int fsecndx
= sechdr
->sh_info
;
1479 const char *fromsec
= sec_name(elf
, fsecndx
);
1480 Elf_Rela
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1481 Elf_Rela
*stop
= (void *)start
+ sechdr
->sh_size
;
1483 /* if from section (name) is know good then skip it */
1484 if (match(fromsec
, section_white_list
))
1487 for (rela
= start
; rela
< stop
; rela
++) {
1488 Elf_Addr taddr
, r_offset
;
1489 unsigned int r_type
, r_sym
;
1491 r_offset
= TO_NATIVE(rela
->r_offset
);
1492 get_rel_type_and_sym(elf
, rela
->r_info
, &r_type
, &r_sym
);
1494 taddr
= TO_NATIVE(rela
->r_addend
);
1496 switch (elf
->hdr
->e_machine
) {
1498 if (!strcmp("__ex_table", fromsec
) &&
1499 r_type
== R_RISCV_SUB32
)
1503 if (!strcmp("__ex_table", fromsec
) &&
1504 r_type
== R_LARCH_SUB32
)
1509 check_section_mismatch(mod
, elf
, elf
->symtab_start
+ r_sym
,
1510 fsecndx
, fromsec
, r_offset
, taddr
);
1514 static void section_rel(struct module
*mod
, struct elf_info
*elf
,
1518 unsigned int fsecndx
= sechdr
->sh_info
;
1519 const char *fromsec
= sec_name(elf
, fsecndx
);
1520 Elf_Rel
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1521 Elf_Rel
*stop
= (void *)start
+ sechdr
->sh_size
;
1523 /* if from section (name) is know good then skip it */
1524 if (match(fromsec
, section_white_list
))
1527 for (rel
= start
; rel
< stop
; rel
++) {
1529 Elf_Addr taddr
= 0, r_offset
;
1530 unsigned int r_type
, r_sym
;
1533 r_offset
= TO_NATIVE(rel
->r_offset
);
1534 get_rel_type_and_sym(elf
, rel
->r_info
, &r_type
, &r_sym
);
1536 loc
= sym_get_data_by_offset(elf
, fsecndx
, r_offset
);
1537 tsym
= elf
->symtab_start
+ r_sym
;
1539 switch (elf
->hdr
->e_machine
) {
1541 taddr
= addend_386_rel(loc
, r_type
);
1544 taddr
= addend_arm_rel(loc
, tsym
, r_type
);
1547 taddr
= addend_mips_rel(loc
, r_type
);
1550 fatal("Please add code to calculate addend for this architecture\n");
1553 check_section_mismatch(mod
, elf
, tsym
,
1554 fsecndx
, fromsec
, r_offset
, taddr
);
1559 * A module includes a number of sections that are discarded
1560 * either when loaded or when used as built-in.
1561 * For loaded modules all functions marked __init and all data
1562 * marked __initdata will be discarded when the module has been initialized.
1563 * Likewise for modules used built-in the sections marked __exit
1564 * are discarded because __exit marked function are supposed to be called
1565 * only when a module is unloaded which never happens for built-in modules.
1566 * The check_sec_ref() function traverses all relocation records
1567 * to find all references to a section that reference a section that will
1568 * be discarded and warns about it.
1570 static void check_sec_ref(struct module
*mod
, struct elf_info
*elf
)
1573 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
1575 /* Walk through all sections */
1576 for (i
= 0; i
< elf
->num_sections
; i
++) {
1577 check_section(mod
->name
, elf
, &elf
->sechdrs
[i
]);
1578 /* We want to process only relocation sections and not .init */
1579 if (sechdrs
[i
].sh_type
== SHT_RELA
)
1580 section_rela(mod
, elf
, &elf
->sechdrs
[i
]);
1581 else if (sechdrs
[i
].sh_type
== SHT_REL
)
1582 section_rel(mod
, elf
, &elf
->sechdrs
[i
]);
1586 static char *remove_dot(char *s
)
1588 size_t n
= strcspn(s
, ".");
1591 size_t m
= strspn(s
+ n
+ 1, "0123456789");
1592 if (m
&& (s
[n
+ m
+ 1] == '.' || s
[n
+ m
+ 1] == 0))
1599 * The CRCs are recorded in .*.cmd files in the form of:
1600 * #SYMVER <name> <crc>
1602 static void extract_crcs_for_object(const char *object
, struct module
*mod
)
1604 char cmd_file
[PATH_MAX
];
1609 base
= strrchr(object
, '/');
1612 dirlen
= base
- object
;
1618 ret
= snprintf(cmd_file
, sizeof(cmd_file
), "%.*s.%s.cmd",
1619 dirlen
, object
, base
);
1620 if (ret
>= sizeof(cmd_file
)) {
1621 error("%s: too long path was truncated\n", cmd_file
);
1625 buf
= read_text_file(cmd_file
);
1628 while ((p
= strstr(p
, "\n#SYMVER "))) {
1634 name
= p
+ strlen("\n#SYMVER ");
1636 p
= strchr(name
, ' ');
1644 continue; /* skip this line */
1646 crc
= strtoul(p
, &p
, 0);
1648 continue; /* skip this line */
1650 name
[namelen
] = '\0';
1653 * sym_find_with_module() may return NULL here.
1654 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
1655 * Since commit e1327a127703, genksyms calculates CRCs of all
1656 * symbols, including trimmed ones. Ignore orphan CRCs.
1658 sym
= sym_find_with_module(name
, mod
);
1660 sym_set_crc(sym
, crc
);
1667 * The symbol versions (CRC) are recorded in the .*.cmd files.
1668 * Parse them to retrieve CRCs for the current module.
1670 static void mod_set_crcs(struct module
*mod
)
1672 char objlist
[PATH_MAX
];
1673 char *buf
, *p
, *obj
;
1676 if (mod
->is_vmlinux
) {
1677 strcpy(objlist
, ".vmlinux.objs");
1679 /* objects for a module are listed in the *.mod file. */
1680 ret
= snprintf(objlist
, sizeof(objlist
), "%s.mod", mod
->name
);
1681 if (ret
>= sizeof(objlist
)) {
1682 error("%s: too long path was truncated\n", objlist
);
1687 buf
= read_text_file(objlist
);
1690 while ((obj
= strsep(&p
, "\n")) && obj
[0])
1691 extract_crcs_for_object(obj
, mod
);
1696 static void read_symbols(const char *modname
)
1698 const char *symname
;
1703 struct elf_info info
= { };
1706 if (!parse_elf(&info
, modname
))
1709 if (!strends(modname
, ".o")) {
1710 error("%s: filename must be suffixed with .o\n", modname
);
1714 /* strip trailing .o */
1715 mod
= new_module(modname
, strlen(modname
) - strlen(".o"));
1717 if (!mod
->is_vmlinux
) {
1718 license
= get_modinfo(&info
, "license");
1720 error("missing MODULE_LICENSE() in %s\n", modname
);
1722 if (!license_is_gpl_compatible(license
)) {
1723 mod
->is_gpl_compatible
= false;
1726 license
= get_next_modinfo(&info
, "license", license
);
1729 namespace = get_modinfo(&info
, "import_ns");
1731 add_namespace(&mod
->imported_namespaces
, namespace);
1732 namespace = get_next_modinfo(&info
, "import_ns",
1737 if (extra_warn
&& !get_modinfo(&info
, "description"))
1738 warn("missing MODULE_DESCRIPTION() in %s\n", modname
);
1739 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1740 symname
= remove_dot(info
.strtab
+ sym
->st_name
);
1742 handle_symbol(mod
, &info
, sym
, symname
);
1743 handle_moddevtable(mod
, &info
, sym
, symname
);
1746 check_sec_ref(mod
, &info
);
1748 if (!mod
->is_vmlinux
) {
1749 version
= get_modinfo(&info
, "version");
1750 if (version
|| all_versions
)
1751 get_src_version(mod
->name
, mod
->srcversion
,
1752 sizeof(mod
->srcversion
) - 1);
1755 parse_elf_finish(&info
);
1759 * Our trick to get versioning for module struct etc. - it's
1760 * never passed as an argument to an exported function, so
1761 * the automatic versioning doesn't pick it up, but it's really
1764 sym_add_unresolved("module_layout", mod
, false);
1770 static void read_symbols_from_files(const char *filename
)
1773 char fname
[PATH_MAX
];
1775 in
= fopen(filename
, "r");
1777 fatal("Can't open filenames file %s: %m", filename
);
1779 while (fgets(fname
, PATH_MAX
, in
) != NULL
) {
1780 if (strends(fname
, "\n"))
1781 fname
[strlen(fname
)-1] = '\0';
1782 read_symbols(fname
);
1790 /* We first write the generated file into memory using the
1791 * following helper, then compare to the file on disk and
1792 * only update the later if anything changed */
1794 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1795 const char *fmt
, ...)
1802 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1803 buf_write(buf
, tmp
, len
);
1807 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1809 if (buf
->size
- buf
->pos
< len
) {
1810 buf
->size
+= len
+ SZ
;
1811 buf
->p
= NOFAIL(realloc(buf
->p
, buf
->size
));
1813 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1817 static void check_exports(struct module
*mod
)
1819 struct symbol
*s
, *exp
;
1821 list_for_each_entry(s
, &mod
->unresolved_symbols
, list
) {
1822 const char *basename
;
1823 exp
= find_symbol(s
->name
);
1825 if (!s
->weak
&& nr_unresolved
++ < MAX_UNRESOLVED_REPORTS
)
1826 modpost_log(warn_unresolved
? LOG_WARN
: LOG_ERROR
,
1827 "\"%s\" [%s.ko] undefined!\n",
1828 s
->name
, mod
->name
);
1831 if (exp
->module
== mod
) {
1832 error("\"%s\" [%s.ko] was exported without definition\n",
1833 s
->name
, mod
->name
);
1838 s
->module
= exp
->module
;
1839 s
->crc_valid
= exp
->crc_valid
;
1842 basename
= strrchr(mod
->name
, '/');
1846 basename
= mod
->name
;
1848 if (!contains_namespace(&mod
->imported_namespaces
, exp
->namespace)) {
1849 modpost_log(allow_missing_ns_imports
? LOG_WARN
: LOG_ERROR
,
1850 "module %s uses symbol %s from namespace %s, but does not import it.\n",
1851 basename
, exp
->name
, exp
->namespace);
1852 add_namespace(&mod
->missing_namespaces
, exp
->namespace);
1855 if (!mod
->is_gpl_compatible
&& exp
->is_gpl_only
)
1856 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
1857 basename
, exp
->name
);
1861 static void handle_white_list_exports(const char *white_list
)
1863 char *buf
, *p
, *name
;
1865 buf
= read_text_file(white_list
);
1868 while ((name
= strsep(&p
, "\n"))) {
1869 struct symbol
*sym
= find_symbol(name
);
1878 static void check_modname_len(struct module
*mod
)
1880 const char *mod_name
;
1882 mod_name
= strrchr(mod
->name
, '/');
1883 if (mod_name
== NULL
)
1884 mod_name
= mod
->name
;
1887 if (strlen(mod_name
) >= MODULE_NAME_LEN
)
1888 error("module name is too long [%s.ko]\n", mod
->name
);
1892 * Header for the generated file
1894 static void add_header(struct buffer
*b
, struct module
*mod
)
1896 buf_printf(b
, "#include <linux/module.h>\n");
1898 * Include build-salt.h after module.h in order to
1899 * inherit the definitions.
1901 buf_printf(b
, "#define INCLUDE_VERMAGIC\n");
1902 buf_printf(b
, "#include <linux/build-salt.h>\n");
1903 buf_printf(b
, "#include <linux/elfnote-lto.h>\n");
1904 buf_printf(b
, "#include <linux/export-internal.h>\n");
1905 buf_printf(b
, "#include <linux/vermagic.h>\n");
1906 buf_printf(b
, "#include <linux/compiler.h>\n");
1907 buf_printf(b
, "\n");
1908 buf_printf(b
, "#ifdef CONFIG_UNWINDER_ORC\n");
1909 buf_printf(b
, "#include <asm/orc_header.h>\n");
1910 buf_printf(b
, "ORC_HEADER;\n");
1911 buf_printf(b
, "#endif\n");
1912 buf_printf(b
, "\n");
1913 buf_printf(b
, "BUILD_SALT;\n");
1914 buf_printf(b
, "BUILD_LTO_INFO;\n");
1915 buf_printf(b
, "\n");
1916 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1917 buf_printf(b
, "MODULE_INFO(name, KBUILD_MODNAME);\n");
1918 buf_printf(b
, "\n");
1919 buf_printf(b
, "__visible struct module __this_module\n");
1920 buf_printf(b
, "__section(\".gnu.linkonce.this_module\") = {\n");
1921 buf_printf(b
, "\t.name = KBUILD_MODNAME,\n");
1923 buf_printf(b
, "\t.init = init_module,\n");
1924 if (mod
->has_cleanup
)
1925 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1926 "\t.exit = cleanup_module,\n"
1928 buf_printf(b
, "\t.arch = MODULE_ARCH_INIT,\n");
1929 buf_printf(b
, "};\n");
1931 if (!external_module
)
1932 buf_printf(b
, "\nMODULE_INFO(intree, \"Y\");\n");
1936 "#ifdef CONFIG_RETPOLINE\n"
1937 "MODULE_INFO(retpoline, \"Y\");\n"
1940 if (strstarts(mod
->name
, "drivers/staging"))
1941 buf_printf(b
, "\nMODULE_INFO(staging, \"Y\");\n");
1943 if (strstarts(mod
->name
, "tools/testing"))
1944 buf_printf(b
, "\nMODULE_INFO(test, \"Y\");\n");
1947 static void add_exported_symbols(struct buffer
*buf
, struct module
*mod
)
1951 /* generate struct for exported symbols */
1952 buf_printf(buf
, "\n");
1953 list_for_each_entry(sym
, &mod
->exported_symbols
, list
) {
1954 if (trim_unused_exports
&& !sym
->used
)
1957 buf_printf(buf
, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n",
1958 sym
->is_func
? "FUNC" : "DATA", sym
->name
,
1959 sym
->is_gpl_only
? "_gpl" : "", sym
->namespace);
1965 /* record CRCs for exported symbols */
1966 buf_printf(buf
, "\n");
1967 list_for_each_entry(sym
, &mod
->exported_symbols
, list
) {
1968 if (trim_unused_exports
&& !sym
->used
)
1971 if (!sym
->crc_valid
)
1972 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
1973 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
1974 sym
->name
, mod
->name
, mod
->is_vmlinux
? "" : ".ko",
1977 buf_printf(buf
, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
1978 sym
->name
, sym
->crc
, sym
->is_gpl_only
? "_gpl" : "");
1983 * Record CRCs for unresolved symbols
1985 static void add_versions(struct buffer
*b
, struct module
*mod
)
1992 buf_printf(b
, "\n");
1993 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1994 buf_printf(b
, "__used __section(\"__versions\") = {\n");
1996 list_for_each_entry(s
, &mod
->unresolved_symbols
, list
) {
1999 if (!s
->crc_valid
) {
2000 warn("\"%s\" [%s.ko] has no CRC!\n",
2001 s
->name
, mod
->name
);
2004 if (strlen(s
->name
) >= MODULE_NAME_LEN
) {
2005 error("too long symbol \"%s\" [%s.ko]\n",
2006 s
->name
, mod
->name
);
2009 buf_printf(b
, "\t{ %#8x, \"%s\" },\n",
2013 buf_printf(b
, "};\n");
2016 static void add_depends(struct buffer
*b
, struct module
*mod
)
2021 /* Clear ->seen flag of modules that own symbols needed by this. */
2022 list_for_each_entry(s
, &mod
->unresolved_symbols
, list
) {
2024 s
->module
->seen
= s
->module
->is_vmlinux
;
2027 buf_printf(b
, "\n");
2028 buf_printf(b
, "MODULE_INFO(depends, \"");
2029 list_for_each_entry(s
, &mod
->unresolved_symbols
, list
) {
2034 if (s
->module
->seen
)
2037 s
->module
->seen
= true;
2038 p
= strrchr(s
->module
->name
, '/');
2042 p
= s
->module
->name
;
2043 buf_printf(b
, "%s%s", first
? "" : ",", p
);
2046 buf_printf(b
, "\");\n");
2049 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
2051 if (mod
->srcversion
[0]) {
2052 buf_printf(b
, "\n");
2053 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
2058 static void write_buf(struct buffer
*b
, const char *fname
)
2065 file
= fopen(fname
, "w");
2070 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
2074 if (fclose(file
) != 0) {
2080 static void write_if_changed(struct buffer
*b
, const char *fname
)
2086 file
= fopen(fname
, "r");
2090 if (fstat(fileno(file
), &st
) < 0)
2093 if (st
.st_size
!= b
->pos
)
2096 tmp
= NOFAIL(malloc(b
->pos
));
2097 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
2100 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
2112 write_buf(b
, fname
);
2115 static void write_vmlinux_export_c_file(struct module
*mod
)
2117 struct buffer buf
= { };
2120 "#include <linux/export-internal.h>\n");
2122 add_exported_symbols(&buf
, mod
);
2123 write_if_changed(&buf
, ".vmlinux.export.c");
2127 /* do sanity checks, and generate *.mod.c file */
2128 static void write_mod_c_file(struct module
*mod
)
2130 struct buffer buf
= { };
2131 char fname
[PATH_MAX
];
2134 add_header(&buf
, mod
);
2135 add_exported_symbols(&buf
, mod
);
2136 add_versions(&buf
, mod
);
2137 add_depends(&buf
, mod
);
2138 add_moddevtable(&buf
, mod
);
2139 add_srcversion(&buf
, mod
);
2141 ret
= snprintf(fname
, sizeof(fname
), "%s.mod.c", mod
->name
);
2142 if (ret
>= sizeof(fname
)) {
2143 error("%s: too long path was truncated\n", fname
);
2147 write_if_changed(&buf
, fname
);
2153 /* parse Module.symvers file. line format:
2154 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
2156 static void read_dump(const char *fname
)
2158 char *buf
, *pos
, *line
;
2160 buf
= read_text_file(fname
);
2162 /* No symbol versions, silently ignore */
2167 while ((line
= get_line(&pos
))) {
2168 char *symname
, *namespace, *modname
, *d
, *export
;
2174 if (!(symname
= strchr(line
, '\t')))
2177 if (!(modname
= strchr(symname
, '\t')))
2180 if (!(export
= strchr(modname
, '\t')))
2183 if (!(namespace = strchr(export
, '\t')))
2185 *namespace++ = '\0';
2187 crc
= strtoul(line
, &d
, 16);
2188 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
2191 if (!strcmp(export
, "EXPORT_SYMBOL_GPL")) {
2193 } else if (!strcmp(export
, "EXPORT_SYMBOL")) {
2196 error("%s: unknown license %s. skip", symname
, export
);
2200 mod
= find_module(modname
);
2202 mod
= new_module(modname
, strlen(modname
));
2203 mod
->from_dump
= true;
2205 s
= sym_add_exported(symname
, mod
, gpl_only
, namespace);
2206 sym_set_crc(s
, crc
);
2212 fatal("parse error in symbol dump file\n");
2215 static void write_dump(const char *fname
)
2217 struct buffer buf
= { };
2221 list_for_each_entry(mod
, &modules
, list
) {
2224 list_for_each_entry(sym
, &mod
->exported_symbols
, list
) {
2225 if (trim_unused_exports
&& !sym
->used
)
2228 buf_printf(&buf
, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
2229 sym
->crc
, sym
->name
, mod
->name
,
2230 sym
->is_gpl_only
? "_GPL" : "",
2234 write_buf(&buf
, fname
);
2238 static void write_namespace_deps_files(const char *fname
)
2241 struct namespace_list
*ns
;
2242 struct buffer ns_deps_buf
= {};
2244 list_for_each_entry(mod
, &modules
, list
) {
2246 if (mod
->from_dump
|| list_empty(&mod
->missing_namespaces
))
2249 buf_printf(&ns_deps_buf
, "%s.ko:", mod
->name
);
2251 list_for_each_entry(ns
, &mod
->missing_namespaces
, list
)
2252 buf_printf(&ns_deps_buf
, " %s", ns
->namespace);
2254 buf_printf(&ns_deps_buf
, "\n");
2257 write_if_changed(&ns_deps_buf
, fname
);
2258 free(ns_deps_buf
.p
);
2262 struct list_head list
;
2266 int main(int argc
, char **argv
)
2269 char *missing_namespace_deps
= NULL
;
2270 char *unused_exports_white_list
= NULL
;
2271 char *dump_write
= NULL
, *files_source
= NULL
;
2273 LIST_HEAD(dump_lists
);
2274 struct dump_list
*dl
, *dl2
;
2276 while ((opt
= getopt(argc
, argv
, "ei:MmnT:to:au:WwENd:")) != -1) {
2279 external_module
= true;
2282 dl
= NOFAIL(malloc(sizeof(*dl
)));
2284 list_add_tail(&dl
->list
, &dump_lists
);
2287 module_enabled
= true;
2293 ignore_missing_files
= true;
2296 dump_write
= optarg
;
2299 all_versions
= true;
2302 files_source
= optarg
;
2305 trim_unused_exports
= true;
2308 unused_exports_white_list
= optarg
;
2314 warn_unresolved
= true;
2317 sec_mismatch_warn_only
= false;
2320 allow_missing_ns_imports
= true;
2323 missing_namespace_deps
= optarg
;
2330 list_for_each_entry_safe(dl
, dl2
, &dump_lists
, list
) {
2331 read_dump(dl
->file
);
2332 list_del(&dl
->list
);
2336 while (optind
< argc
)
2337 read_symbols(argv
[optind
++]);
2340 read_symbols_from_files(files_source
);
2342 list_for_each_entry(mod
, &modules
, list
) {
2343 if (mod
->from_dump
|| mod
->is_vmlinux
)
2346 check_modname_len(mod
);
2350 if (unused_exports_white_list
)
2351 handle_white_list_exports(unused_exports_white_list
);
2353 list_for_each_entry(mod
, &modules
, list
) {
2357 if (mod
->is_vmlinux
)
2358 write_vmlinux_export_c_file(mod
);
2360 write_mod_c_file(mod
);
2363 if (missing_namespace_deps
)
2364 write_namespace_deps_files(missing_namespace_deps
);
2367 write_dump(dump_write
);
2368 if (sec_mismatch_count
&& !sec_mismatch_warn_only
)
2369 error("Section mismatches detected.\n"
2370 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2372 if (nr_unresolved
> MAX_UNRESOLVED_REPORTS
)
2373 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2374 nr_unresolved
- MAX_UNRESOLVED_REPORTS
);
2376 return error_occurred
? 1 : 0;