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 ...
18 #include "../../include/linux/license.h"
20 /* Are we using CONFIG_MODVERSIONS? */
22 /* Warn about undefined symbols? (do so if we have vmlinux) */
24 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
25 static int all_versions
= 0;
26 /* If we are modposting external module set to 1 */
27 static int external_module
= 0;
28 /* Warn about section mismatch in vmlinux if set to 1 */
29 static int vmlinux_section_warnings
= 1;
30 /* Only warn about unresolved symbols */
31 static int warn_unresolved
= 0;
32 /* How a symbol is exported */
33 static int sec_mismatch_count
= 0;
34 static int sec_mismatch_verbose
= 1;
37 export_plain
, export_unused
, export_gpl
,
38 export_unused_gpl
, export_gpl_future
, export_unknown
41 #define PRINTF __attribute__ ((format (printf, 1, 2)))
43 PRINTF
void fatal(const char *fmt
, ...)
47 fprintf(stderr
, "FATAL: ");
49 va_start(arglist
, fmt
);
50 vfprintf(stderr
, fmt
, arglist
);
56 PRINTF
void warn(const char *fmt
, ...)
60 fprintf(stderr
, "WARNING: ");
62 va_start(arglist
, fmt
);
63 vfprintf(stderr
, fmt
, arglist
);
67 PRINTF
void merror(const char *fmt
, ...)
71 fprintf(stderr
, "ERROR: ");
73 va_start(arglist
, fmt
);
74 vfprintf(stderr
, fmt
, arglist
);
78 static int is_vmlinux(const char *modname
)
82 myname
= strrchr(modname
, '/');
88 return (strcmp(myname
, "vmlinux") == 0) ||
89 (strcmp(myname
, "vmlinux.o") == 0);
92 void *do_nofail(void *ptr
, const char *expr
)
95 fatal("modpost: Memory allocation failure: %s.\n", expr
);
100 /* A list of all modules we processed */
101 static struct module
*modules
;
103 static struct module
*find_module(char *modname
)
107 for (mod
= modules
; mod
; mod
= mod
->next
)
108 if (strcmp(mod
->name
, modname
) == 0)
113 static struct module
*new_module(char *modname
)
118 mod
= NOFAIL(malloc(sizeof(*mod
)));
119 memset(mod
, 0, sizeof(*mod
));
120 p
= NOFAIL(strdup(modname
));
122 /* strip trailing .o */
125 if (strcmp(s
, ".o") == 0)
130 mod
->gpl_compatible
= -1;
137 /* A hash of all exported symbols,
138 * struct symbol is also used for lists of unresolved symbols */
140 #define SYMBOL_HASH_SIZE 1024
144 struct module
*module
;
148 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
149 unsigned int kernel
:1; /* 1 if symbol is from kernel
150 * (only for external modules) **/
151 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
152 enum export export
; /* Type of export */
156 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
158 /* This is based on the hash agorithm from gdbm, via tdb */
159 static inline unsigned int tdb_hash(const char *name
)
161 unsigned value
; /* Used to compute the hash value. */
162 unsigned i
; /* Used to cycle through random values. */
164 /* Set the initial value from the key size. */
165 for (value
= 0x238F13AF * strlen(name
), i
= 0; name
[i
]; i
++)
166 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
168 return (1103515243 * value
+ 12345);
172 * Allocate a new symbols for use in the hash of exported symbols or
173 * the list of unresolved symbols per module
175 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
178 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
180 memset(s
, 0, sizeof(*s
));
181 strcpy(s
->name
, name
);
187 /* For the hash of exported symbols */
188 static struct symbol
*new_symbol(const char *name
, struct module
*module
,
194 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
195 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
196 new->module
= module
;
197 new->export
= export
;
201 static struct symbol
*find_symbol(const char *name
)
205 /* For our purposes, .foo matches foo. PPC64 needs this. */
209 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
= s
->next
) {
210 if (strcmp(s
->name
, name
) == 0)
220 { .str
= "EXPORT_SYMBOL", .export
= export_plain
},
221 { .str
= "EXPORT_UNUSED_SYMBOL", .export
= export_unused
},
222 { .str
= "EXPORT_SYMBOL_GPL", .export
= export_gpl
},
223 { .str
= "EXPORT_UNUSED_SYMBOL_GPL", .export
= export_unused_gpl
},
224 { .str
= "EXPORT_SYMBOL_GPL_FUTURE", .export
= export_gpl_future
},
225 { .str
= "(unknown)", .export
= export_unknown
},
229 static const char *export_str(enum export ex
)
231 return export_list
[ex
].str
;
234 static enum export
export_no(const char *s
)
239 return export_unknown
;
240 for (i
= 0; export_list
[i
].export
!= export_unknown
; i
++) {
241 if (strcmp(export_list
[i
].str
, s
) == 0)
242 return export_list
[i
].export
;
244 return export_unknown
;
247 static enum export
export_from_sec(struct elf_info
*elf
, Elf_Section sec
)
249 if (sec
== elf
->export_sec
)
251 else if (sec
== elf
->export_unused_sec
)
252 return export_unused
;
253 else if (sec
== elf
->export_gpl_sec
)
255 else if (sec
== elf
->export_unused_gpl_sec
)
256 return export_unused_gpl
;
257 else if (sec
== elf
->export_gpl_future_sec
)
258 return export_gpl_future
;
260 return export_unknown
;
264 * Add an exported symbol - it may have already been added without a
265 * CRC, in this case just update the CRC
267 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
270 struct symbol
*s
= find_symbol(name
);
273 s
= new_symbol(name
, mod
, export
);
276 warn("%s: '%s' exported twice. Previous export "
277 "was in %s%s\n", mod
->name
, name
,
279 is_vmlinux(s
->module
->name
) ?"":".ko");
281 /* In case Modules.symvers was out of date */
286 s
->vmlinux
= is_vmlinux(mod
->name
);
292 static void sym_update_crc(const char *name
, struct module
*mod
,
293 unsigned int crc
, enum export export
)
295 struct symbol
*s
= find_symbol(name
);
298 s
= new_symbol(name
, mod
, export
);
303 void *grab_file(const char *filename
, unsigned long *size
)
309 fd
= open(filename
, O_RDONLY
);
310 if (fd
< 0 || fstat(fd
, &st
) != 0)
314 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
317 if (map
== MAP_FAILED
)
323 * Return a copy of the next line in a mmap'ed file.
324 * spaces in the beginning of the line is trimmed away.
325 * Return a pointer to a static buffer.
327 char *get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
329 static char line
[4096];
332 signed char *p
= (signed char *)file
+ *pos
;
335 for (; *pos
< size
; (*pos
)++) {
336 if (skip
&& isspace(*p
)) {
341 if (*p
!= '\n' && (*pos
< size
)) {
345 break; /* Too long, stop */
356 void release_file(void *file
, unsigned long size
)
361 static int parse_elf(struct elf_info
*info
, const char *filename
)
368 hdr
= grab_file(filename
, &info
->size
);
374 if (info
->size
< sizeof(*hdr
)) {
375 /* file too small, assume this is an empty .o file */
378 /* Is this a valid ELF file? */
379 if ((hdr
->e_ident
[EI_MAG0
] != ELFMAG0
) ||
380 (hdr
->e_ident
[EI_MAG1
] != ELFMAG1
) ||
381 (hdr
->e_ident
[EI_MAG2
] != ELFMAG2
) ||
382 (hdr
->e_ident
[EI_MAG3
] != ELFMAG3
)) {
383 /* Not an ELF file - silently ignore it */
386 /* Fix endianness in ELF header */
387 hdr
->e_type
= TO_NATIVE(hdr
->e_type
);
388 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
389 hdr
->e_version
= TO_NATIVE(hdr
->e_version
);
390 hdr
->e_entry
= TO_NATIVE(hdr
->e_entry
);
391 hdr
->e_phoff
= TO_NATIVE(hdr
->e_phoff
);
392 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
393 hdr
->e_flags
= TO_NATIVE(hdr
->e_flags
);
394 hdr
->e_ehsize
= TO_NATIVE(hdr
->e_ehsize
);
395 hdr
->e_phentsize
= TO_NATIVE(hdr
->e_phentsize
);
396 hdr
->e_phnum
= TO_NATIVE(hdr
->e_phnum
);
397 hdr
->e_shentsize
= TO_NATIVE(hdr
->e_shentsize
);
398 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
399 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
400 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
401 info
->sechdrs
= sechdrs
;
403 /* Check if file offset is correct */
404 if (hdr
->e_shoff
> info
->size
) {
405 fatal("section header offset=%lu in file '%s' is bigger than "
406 "filesize=%lu\n", (unsigned long)hdr
->e_shoff
,
407 filename
, info
->size
);
411 /* Fix endianness in section headers */
412 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
413 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
414 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
415 sechdrs
[i
].sh_flags
= TO_NATIVE(sechdrs
[i
].sh_flags
);
416 sechdrs
[i
].sh_addr
= TO_NATIVE(sechdrs
[i
].sh_addr
);
417 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
418 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
419 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
420 sechdrs
[i
].sh_info
= TO_NATIVE(sechdrs
[i
].sh_info
);
421 sechdrs
[i
].sh_addralign
= TO_NATIVE(sechdrs
[i
].sh_addralign
);
422 sechdrs
[i
].sh_entsize
= TO_NATIVE(sechdrs
[i
].sh_entsize
);
424 /* Find symbol table. */
425 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
426 const char *secstrings
427 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
429 int nobits
= sechdrs
[i
].sh_type
== SHT_NOBITS
;
431 if (!nobits
&& sechdrs
[i
].sh_offset
> info
->size
) {
432 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
433 "sizeof(*hrd)=%zu\n", filename
,
434 (unsigned long)sechdrs
[i
].sh_offset
,
438 secname
= secstrings
+ sechdrs
[i
].sh_name
;
439 if (strcmp(secname
, ".modinfo") == 0) {
441 fatal("%s has NOBITS .modinfo\n", filename
);
442 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
443 info
->modinfo_len
= sechdrs
[i
].sh_size
;
444 } else if (strcmp(secname
, "__ksymtab") == 0)
445 info
->export_sec
= i
;
446 else if (strcmp(secname
, "__ksymtab_unused") == 0)
447 info
->export_unused_sec
= i
;
448 else if (strcmp(secname
, "__ksymtab_gpl") == 0)
449 info
->export_gpl_sec
= i
;
450 else if (strcmp(secname
, "__ksymtab_unused_gpl") == 0)
451 info
->export_unused_gpl_sec
= i
;
452 else if (strcmp(secname
, "__ksymtab_gpl_future") == 0)
453 info
->export_gpl_future_sec
= i
;
454 else if (strcmp(secname
, "__markers_strings") == 0)
455 info
->markers_strings_sec
= i
;
457 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
460 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
461 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
462 + sechdrs
[i
].sh_size
;
463 info
->strtab
= (void *)hdr
+
464 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
466 if (!info
->symtab_start
)
467 fatal("%s has no symtab?\n", filename
);
469 /* Fix endianness in symbols */
470 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
471 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
472 sym
->st_name
= TO_NATIVE(sym
->st_name
);
473 sym
->st_value
= TO_NATIVE(sym
->st_value
);
474 sym
->st_size
= TO_NATIVE(sym
->st_size
);
479 static void parse_elf_finish(struct elf_info
*info
)
481 release_file(info
->hdr
, info
->size
);
484 static int ignore_undef_symbol(struct elf_info
*info
, const char *symname
)
486 /* ignore __this_module, it will be resolved shortly */
487 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
489 /* ignore global offset table */
490 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
492 if (info
->hdr
->e_machine
== EM_PPC
)
493 /* Special register function linked on all modules during final link of .ko */
494 if (strncmp(symname
, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
495 strncmp(symname
, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
496 strncmp(symname
, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
497 strncmp(symname
, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
499 /* Do not ignore this symbol */
503 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
504 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
506 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
507 Elf_Sym
*sym
, const char *symname
)
510 enum export export
= export_from_sec(info
, sym
->st_shndx
);
512 switch (sym
->st_shndx
) {
514 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
518 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
519 crc
= (unsigned int) sym
->st_value
;
520 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
,
525 /* undefined symbol */
526 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
527 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
529 if (ignore_undef_symbol(info
, symname
))
531 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
532 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
533 /* add compatibility with older glibc */
534 #ifndef STT_SPARC_REGISTER
535 #define STT_SPARC_REGISTER STT_REGISTER
537 if (info
->hdr
->e_machine
== EM_SPARC
||
538 info
->hdr
->e_machine
== EM_SPARCV9
) {
539 /* Ignore register directives. */
540 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
542 if (symname
[0] == '.') {
543 char *munged
= strdup(symname
);
545 munged
[1] = toupper(munged
[1]);
551 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
552 strlen(MODULE_SYMBOL_PREFIX
)) == 0) {
554 alloc_symbol(symname
+
555 strlen(MODULE_SYMBOL_PREFIX
),
556 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
561 /* All exported symbols */
562 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
563 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
,
566 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
568 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
569 mod
->has_cleanup
= 1;
575 * Parse tag=value strings from .modinfo section
577 static char *next_string(char *string
, unsigned long *secsize
)
579 /* Skip non-zero chars */
582 if ((*secsize
)-- <= 1)
586 /* Skip any zero padding. */
589 if ((*secsize
)-- <= 1)
595 static char *get_next_modinfo(void *modinfo
, unsigned long modinfo_len
,
596 const char *tag
, char *info
)
599 unsigned int taglen
= strlen(tag
);
600 unsigned long size
= modinfo_len
;
603 size
-= info
- (char *)modinfo
;
604 modinfo
= next_string(info
, &size
);
607 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
608 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
609 return p
+ taglen
+ 1;
614 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
618 return get_next_modinfo(modinfo
, modinfo_len
, tag
, NULL
);
622 * Test if string s ends in string sub
625 static int strrcmp(const char *s
, const char *sub
)
633 sublen
= strlen(sub
);
635 if ((slen
== 0) || (sublen
== 0))
641 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
644 static const char *sym_name(struct elf_info
*elf
, Elf_Sym
*sym
)
647 return elf
->strtab
+ sym
->st_name
;
652 static const char *sec_name(struct elf_info
*elf
, int shndx
)
654 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
655 return (void *)elf
->hdr
+
656 elf
->sechdrs
[elf
->hdr
->e_shstrndx
].sh_offset
+
657 sechdrs
[shndx
].sh_name
;
660 static const char *sech_name(struct elf_info
*elf
, Elf_Shdr
*sechdr
)
662 return (void *)elf
->hdr
+
663 elf
->sechdrs
[elf
->hdr
->e_shstrndx
].sh_offset
+
667 /* if sym is empty or point to a string
668 * like ".[0-9]+" then return 1.
669 * This is the optional prefix added by ld to some sections
671 static int number_prefix(const char *sym
)
679 if (c
< '0' || c
> '9')
685 /* The pattern is an array of simple patterns.
686 * "foo" will match an exact string equal to "foo"
687 * "*foo" will match a string that ends with "foo"
688 * "foo*" will match a string that begins with "foo"
689 * "foo$" will match a string equal to "foo" or "foo.1"
690 * where the '1' can be any number including several digits.
691 * The $ syntax is for sections where ld append a dot number
692 * to make section name unique.
694 static int match(const char *sym
, const char * const pat
[])
699 const char *endp
= p
+ strlen(p
) - 1;
703 if (strrcmp(sym
, p
+ 1) == 0)
707 else if (*endp
== '*') {
708 if (strncmp(sym
, p
, strlen(p
) - 1) == 0)
712 else if (*endp
== '$') {
713 if (strncmp(sym
, p
, strlen(p
) - 1) == 0) {
714 if (number_prefix(sym
+ strlen(p
) - 1))
720 if (strcmp(p
, sym
) == 0)
728 /* sections that we do not want to do full section mismatch check on */
729 static const char *section_white_list
[] =
733 ".mdebug*", /* alpha, score, mips etc. */
734 ".pdr", /* alpha, score, mips etc. */
743 * This is used to find sections missing the SHF_ALLOC flag.
744 * The cause of this is often a section specified in assembler
745 * without "ax" / "aw".
747 static void check_section(const char *modname
, struct elf_info
*elf
,
750 const char *sec
= sech_name(elf
, sechdr
);
752 if (sechdr
->sh_type
== SHT_PROGBITS
&&
753 !(sechdr
->sh_flags
& SHF_ALLOC
) &&
754 !match(sec
, section_white_list
)) {
755 warn("%s (%s): unexpected non-allocatable section.\n"
756 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
757 "Note that for example <linux/init.h> contains\n"
758 "section definitions for use in .S files.\n\n",
765 #define ALL_INIT_DATA_SECTIONS \
766 ".init.setup$", ".init.rodata$", \
767 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$" \
768 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
769 #define ALL_EXIT_DATA_SECTIONS \
770 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
772 #define ALL_INIT_TEXT_SECTIONS \
773 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
774 #define ALL_EXIT_TEXT_SECTIONS \
775 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
777 #define ALL_INIT_SECTIONS INIT_SECTIONS, DEV_INIT_SECTIONS, \
778 CPU_INIT_SECTIONS, MEM_INIT_SECTIONS
779 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, DEV_EXIT_SECTIONS, \
780 CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS
782 #define DATA_SECTIONS ".data$", ".data.rel$"
783 #define TEXT_SECTIONS ".text$"
785 #define INIT_SECTIONS ".init.*"
786 #define DEV_INIT_SECTIONS ".devinit.*"
787 #define CPU_INIT_SECTIONS ".cpuinit.*"
788 #define MEM_INIT_SECTIONS ".meminit.*"
790 #define EXIT_SECTIONS ".exit.*"
791 #define DEV_EXIT_SECTIONS ".devexit.*"
792 #define CPU_EXIT_SECTIONS ".cpuexit.*"
793 #define MEM_EXIT_SECTIONS ".memexit.*"
795 /* init data sections */
796 static const char *init_data_sections
[] = { ALL_INIT_DATA_SECTIONS
, NULL
};
798 /* all init sections */
799 static const char *init_sections
[] = { ALL_INIT_SECTIONS
, NULL
};
801 /* All init and exit sections (code + data) */
802 static const char *init_exit_sections
[] =
803 {ALL_INIT_SECTIONS
, ALL_EXIT_SECTIONS
, NULL
};
806 static const char *data_sections
[] = { DATA_SECTIONS
, NULL
};
809 /* symbols in .data that may refer to init/exit sections */
810 static const char *symbol_white_list
[] =
813 "*_template", /* scsi uses *_template a lot */
814 "*_timer", /* arm uses ops structures named _timer a lot */
815 "*_sht", /* scsi also used *_sht to some extent */
823 static const char *head_sections
[] = { ".head.text*", NULL
};
824 static const char *linker_symbols
[] =
825 { "__init_begin", "_sinittext", "_einittext", NULL
};
840 struct sectioncheck
{
841 const char *fromsec
[20];
842 const char *tosec
[20];
843 enum mismatch mismatch
;
846 const struct sectioncheck sectioncheck
[] = {
847 /* Do not reference init/exit code/data from
848 * normal code and data
851 .fromsec
= { TEXT_SECTIONS
, NULL
},
852 .tosec
= { ALL_INIT_SECTIONS
, NULL
},
853 .mismatch
= TEXT_TO_INIT
,
856 .fromsec
= { DATA_SECTIONS
, NULL
},
857 .tosec
= { ALL_INIT_SECTIONS
, NULL
},
858 .mismatch
= DATA_TO_INIT
,
861 .fromsec
= { TEXT_SECTIONS
, NULL
},
862 .tosec
= { ALL_EXIT_SECTIONS
, NULL
},
863 .mismatch
= TEXT_TO_EXIT
,
866 .fromsec
= { DATA_SECTIONS
, NULL
},
867 .tosec
= { ALL_EXIT_SECTIONS
, NULL
},
868 .mismatch
= DATA_TO_EXIT
,
870 /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
872 .fromsec
= { DEV_INIT_SECTIONS
, CPU_INIT_SECTIONS
, MEM_INIT_SECTIONS
, NULL
},
873 .tosec
= { INIT_SECTIONS
, NULL
},
874 .mismatch
= XXXINIT_TO_INIT
,
876 /* Do not reference cpuinit code/data from meminit code/data */
878 .fromsec
= { MEM_INIT_SECTIONS
, NULL
},
879 .tosec
= { CPU_INIT_SECTIONS
, NULL
},
880 .mismatch
= XXXINIT_TO_INIT
,
882 /* Do not reference meminit code/data from cpuinit code/data */
884 .fromsec
= { CPU_INIT_SECTIONS
, NULL
},
885 .tosec
= { MEM_INIT_SECTIONS
, NULL
},
886 .mismatch
= XXXINIT_TO_INIT
,
888 /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
890 .fromsec
= { DEV_EXIT_SECTIONS
, CPU_EXIT_SECTIONS
, MEM_EXIT_SECTIONS
, NULL
},
891 .tosec
= { EXIT_SECTIONS
, NULL
},
892 .mismatch
= XXXEXIT_TO_EXIT
,
894 /* Do not reference cpuexit code/data from memexit code/data */
896 .fromsec
= { MEM_EXIT_SECTIONS
, NULL
},
897 .tosec
= { CPU_EXIT_SECTIONS
, NULL
},
898 .mismatch
= XXXEXIT_TO_EXIT
,
900 /* Do not reference memexit code/data from cpuexit code/data */
902 .fromsec
= { CPU_EXIT_SECTIONS
, NULL
},
903 .tosec
= { MEM_EXIT_SECTIONS
, NULL
},
904 .mismatch
= XXXEXIT_TO_EXIT
,
906 /* Do not use exit code/data from init code */
908 .fromsec
= { ALL_INIT_SECTIONS
, NULL
},
909 .tosec
= { ALL_EXIT_SECTIONS
, NULL
},
910 .mismatch
= INIT_TO_EXIT
,
912 /* Do not use init code/data from exit code */
914 .fromsec
= { ALL_EXIT_SECTIONS
, NULL
},
915 .tosec
= { ALL_INIT_SECTIONS
, NULL
},
916 .mismatch
= EXIT_TO_INIT
,
918 /* Do not export init/exit functions or data */
920 .fromsec
= { "__ksymtab*", NULL
},
921 .tosec
= { INIT_SECTIONS
, EXIT_SECTIONS
, NULL
},
922 .mismatch
= EXPORT_TO_INIT_EXIT
926 static int section_mismatch(const char *fromsec
, const char *tosec
)
929 int elems
= sizeof(sectioncheck
) / sizeof(struct sectioncheck
);
930 const struct sectioncheck
*check
= §ioncheck
[0];
932 for (i
= 0; i
< elems
; i
++) {
933 if (match(fromsec
, check
->fromsec
) &&
934 match(tosec
, check
->tosec
))
935 return check
->mismatch
;
942 * Whitelist to allow certain references to pass with no warning.
945 * If a module parameter is declared __initdata and permissions=0
946 * then this is legal despite the warning generated.
947 * We cannot see value of permissions here, so just ignore
949 * The pattern is identified by:
955 * Many drivers utilise a *driver container with references to
956 * add, remove, probe functions etc.
957 * These functions may often be marked __init and we do not want to
959 * the pattern is identified by:
960 * tosec = init or exit section
961 * fromsec = data section
962 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
963 * *probe_one, *_console, *_timer
966 * Whitelist all references from .head.text to any init section
969 * Some symbols belong to init section but still it is ok to reference
970 * these from non-init sections as these symbols don't have any memory
971 * allocated for them and symbol address and value are same. So even
972 * if init section is freed, its ok to reference those symbols.
973 * For ex. symbols marking the init section boundaries.
974 * This pattern is identified by
975 * refsymname = __init_begin, _sinittext, _einittext
978 static int secref_whitelist(const char *fromsec
, const char *fromsym
,
979 const char *tosec
, const char *tosym
)
981 /* Check for pattern 1 */
982 if (match(tosec
, init_data_sections
) &&
983 match(fromsec
, data_sections
) &&
984 (strncmp(fromsym
, "__param", strlen("__param")) == 0))
987 /* Check for pattern 2 */
988 if (match(tosec
, init_exit_sections
) &&
989 match(fromsec
, data_sections
) &&
990 match(fromsym
, symbol_white_list
))
993 /* Check for pattern 3 */
994 if (match(fromsec
, head_sections
) &&
995 match(tosec
, init_sections
))
998 /* Check for pattern 4 */
999 if (match(tosym
, linker_symbols
))
1006 * Find symbol based on relocation record info.
1007 * In some cases the symbol supplied is a valid symbol so
1008 * return refsym. If st_name != 0 we assume this is a valid symbol.
1009 * In other cases the symbol needs to be looked up in the symbol table
1010 * based on section and address.
1012 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf64_Sword addr
,
1016 Elf_Sym
*near
= NULL
;
1017 Elf64_Sword distance
= 20;
1020 if (relsym
->st_name
!= 0)
1022 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
1023 if (sym
->st_shndx
!= relsym
->st_shndx
)
1025 if (ELF_ST_TYPE(sym
->st_info
) == STT_SECTION
)
1027 if (sym
->st_value
== addr
)
1029 /* Find a symbol nearby - addr are maybe negative */
1030 d
= sym
->st_value
- addr
;
1032 d
= addr
- sym
->st_value
;
1038 /* We need a close match */
1045 static inline int is_arm_mapping_symbol(const char *str
)
1047 return str
[0] == '$' && strchr("atd", str
[1])
1048 && (str
[2] == '\0' || str
[2] == '.');
1052 * If there's no name there, ignore it; likewise, ignore it if it's
1053 * one of the magic symbols emitted used by current ARM tools.
1055 * Otherwise if find_symbols_between() returns those symbols, they'll
1056 * fail the whitelist tests and cause lots of false alarms ... fixable
1057 * only by merging __exit and __init sections into __text, bloating
1058 * the kernel (which is especially evil on embedded platforms).
1060 static inline int is_valid_name(struct elf_info
*elf
, Elf_Sym
*sym
)
1062 const char *name
= elf
->strtab
+ sym
->st_name
;
1064 if (!name
|| !strlen(name
))
1066 return !is_arm_mapping_symbol(name
);
1070 * Find symbols before or equal addr and after addr - in the section sec.
1071 * If we find two symbols with equal offset prefer one with a valid name.
1072 * The ELF format may have a better way to detect what type of symbol
1073 * it is, but this works for now.
1075 static Elf_Sym
*find_elf_symbol2(struct elf_info
*elf
, Elf_Addr addr
,
1079 Elf_Sym
*near
= NULL
;
1080 Elf_Addr distance
= ~0;
1082 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
1085 if (sym
->st_shndx
>= SHN_LORESERVE
)
1087 symsec
= sec_name(elf
, sym
->st_shndx
);
1088 if (strcmp(symsec
, sec
) != 0)
1090 if (!is_valid_name(elf
, sym
))
1092 if (sym
->st_value
<= addr
) {
1093 if ((addr
- sym
->st_value
) < distance
) {
1094 distance
= addr
- sym
->st_value
;
1096 } else if ((addr
- sym
->st_value
) == distance
) {
1105 * Convert a section name to the function/data attribute
1106 * .init.text => __init
1107 * .cpuinit.data => __cpudata
1108 * .memexitconst => __memconst
1111 static char *sec2annotation(const char *s
)
1113 if (match(s
, init_exit_sections
)) {
1114 char *p
= malloc(20);
1121 while (*s
&& *s
!= '.')
1126 if (strstr(s
, "rodata") != NULL
)
1127 strcat(p
, "const ");
1128 else if (strstr(s
, "data") != NULL
)
1132 return r
; /* we leak her but we do not care */
1138 static int is_function(Elf_Sym
*sym
)
1141 return ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
;
1147 * Print a warning about a section mismatch.
1148 * Try to find symbols near it so user can find it.
1149 * Check whitelist before warning - it may be a false positive.
1151 static void report_sec_mismatch(const char *modname
, enum mismatch mismatch
,
1152 const char *fromsec
,
1153 unsigned long long fromaddr
,
1154 const char *fromsym
,
1156 const char *tosec
, const char *tosym
,
1159 const char *from
, *from_p
;
1160 const char *to
, *to_p
;
1162 switch (from_is_func
) {
1163 case 0: from
= "variable"; from_p
= ""; break;
1164 case 1: from
= "function"; from_p
= "()"; break;
1165 default: from
= "(unknown reference)"; from_p
= ""; break;
1167 switch (to_is_func
) {
1168 case 0: to
= "variable"; to_p
= ""; break;
1169 case 1: to
= "function"; to_p
= "()"; break;
1170 default: to
= "(unknown reference)"; to_p
= ""; break;
1173 sec_mismatch_count
++;
1174 if (!sec_mismatch_verbose
)
1177 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1178 "to the %s %s:%s%s\n",
1179 modname
, fromsec
, fromaddr
, from
, fromsym
, from_p
, to
, tosec
,
1185 "The function %s%s() references\n"
1187 "This is often because %s lacks a %s\n"
1188 "annotation or the annotation of %s is wrong.\n",
1189 sec2annotation(fromsec
), fromsym
,
1190 to
, sec2annotation(tosec
), tosym
, to_p
,
1191 fromsym
, sec2annotation(tosec
), tosym
);
1193 case DATA_TO_INIT
: {
1194 const char **s
= symbol_white_list
;
1196 "The variable %s references\n"
1198 "If the reference is valid then annotate the\n"
1199 "variable with __init* or __refdata (see linux/init.h) "
1200 "or name the variable:\n",
1201 fromsym
, to
, sec2annotation(tosec
), tosym
, to_p
);
1203 fprintf(stderr
, "%s, ", *s
++);
1204 fprintf(stderr
, "\n");
1209 "The function %s() references a %s in an exit section.\n"
1210 "Often the %s %s%s has valid usage outside the exit section\n"
1211 "and the fix is to remove the %sannotation of %s.\n",
1212 fromsym
, to
, to
, tosym
, to_p
, sec2annotation(tosec
), tosym
);
1214 case DATA_TO_EXIT
: {
1215 const char **s
= symbol_white_list
;
1217 "The variable %s references\n"
1219 "If the reference is valid then annotate the\n"
1220 "variable with __exit* (see linux/init.h) or "
1221 "name the variable:\n",
1222 fromsym
, to
, sec2annotation(tosec
), tosym
, to_p
);
1224 fprintf(stderr
, "%s, ", *s
++);
1225 fprintf(stderr
, "\n");
1228 case XXXINIT_TO_INIT
:
1229 case XXXEXIT_TO_EXIT
:
1231 "The %s %s%s%s references\n"
1233 "If %s is only used by %s then\n"
1234 "annotate %s with a matching annotation.\n",
1235 from
, sec2annotation(fromsec
), fromsym
, from_p
,
1236 to
, sec2annotation(tosec
), tosym
, to_p
,
1237 tosym
, fromsym
, tosym
);
1241 "The %s %s%s%s references\n"
1243 "This is often seen when error handling "
1244 "in the init function\n"
1245 "uses functionality in the exit path.\n"
1246 "The fix is often to remove the %sannotation of\n"
1247 "%s%s so it may be used outside an exit section.\n",
1248 from
, sec2annotation(fromsec
), fromsym
, from_p
,
1249 to
, sec2annotation(tosec
), tosym
, to_p
,
1250 sec2annotation(tosec
), tosym
, to_p
);
1254 "The %s %s%s%s references\n"
1256 "This is often seen when error handling "
1257 "in the exit function\n"
1258 "uses functionality in the init path.\n"
1259 "The fix is often to remove the %sannotation of\n"
1260 "%s%s so it may be used outside an init section.\n",
1261 from
, sec2annotation(fromsec
), fromsym
, from_p
,
1262 to
, sec2annotation(tosec
), tosym
, to_p
,
1263 sec2annotation(tosec
), tosym
, to_p
);
1265 case EXPORT_TO_INIT_EXIT
:
1267 "The symbol %s is exported and annotated %s\n"
1268 "Fix this by removing the %sannotation of %s "
1269 "or drop the export.\n",
1270 tosym
, sec2annotation(tosec
), sec2annotation(tosec
), tosym
);
1272 /* To get warnings on missing members */
1275 fprintf(stderr
, "\n");
1278 static void check_section_mismatch(const char *modname
, struct elf_info
*elf
,
1279 Elf_Rela
*r
, Elf_Sym
*sym
, const char *fromsec
)
1282 enum mismatch mismatch
;
1284 tosec
= sec_name(elf
, sym
->st_shndx
);
1285 mismatch
= section_mismatch(fromsec
, tosec
);
1286 if (mismatch
!= NO_MISMATCH
) {
1290 const char *fromsym
;
1292 from
= find_elf_symbol2(elf
, r
->r_offset
, fromsec
);
1293 fromsym
= sym_name(elf
, from
);
1294 to
= find_elf_symbol(elf
, r
->r_addend
, sym
);
1295 tosym
= sym_name(elf
, to
);
1297 /* check whitelist - we may ignore it */
1298 if (secref_whitelist(fromsec
, fromsym
, tosec
, tosym
)) {
1299 report_sec_mismatch(modname
, mismatch
,
1300 fromsec
, r
->r_offset
, fromsym
,
1301 is_function(from
), tosec
, tosym
,
1307 static unsigned int *reloc_location(struct elf_info
*elf
,
1308 Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1310 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
1311 int section
= sechdr
->sh_info
;
1313 return (void *)elf
->hdr
+ sechdrs
[section
].sh_offset
+
1314 r
->r_offset
- sechdrs
[section
].sh_addr
;
1317 static int addend_386_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1319 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1320 unsigned int *location
= reloc_location(elf
, sechdr
, r
);
1324 r
->r_addend
= TO_NATIVE(*location
);
1327 r
->r_addend
= TO_NATIVE(*location
) + 4;
1328 /* For CONFIG_RELOCATABLE=y */
1329 if (elf
->hdr
->e_type
== ET_EXEC
)
1330 r
->r_addend
+= r
->r_offset
;
1336 static int addend_arm_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1338 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1342 /* From ARM ABI: (S + A) | T */
1343 r
->r_addend
= (int)(long)
1344 (elf
->symtab_start
+ ELF_R_SYM(r
->r_info
));
1347 /* From ARM ABI: ((S + A) | T) - P */
1348 r
->r_addend
= (int)(long)(elf
->hdr
+
1350 (r
->r_offset
- sechdr
->sh_addr
));
1358 static int addend_mips_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1360 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1361 unsigned int *location
= reloc_location(elf
, sechdr
, r
);
1364 if (r_typ
== R_MIPS_HI16
)
1365 return 1; /* skip this */
1366 inst
= TO_NATIVE(*location
);
1369 r
->r_addend
= inst
& 0xffff;
1372 r
->r_addend
= (inst
& 0x03ffffff) << 2;
1381 static void section_rela(const char *modname
, struct elf_info
*elf
,
1388 const char *fromsec
;
1390 Elf_Rela
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1391 Elf_Rela
*stop
= (void *)start
+ sechdr
->sh_size
;
1393 fromsec
= sech_name(elf
, sechdr
);
1394 fromsec
+= strlen(".rela");
1395 /* if from section (name) is know good then skip it */
1396 if (match(fromsec
, section_white_list
))
1399 for (rela
= start
; rela
< stop
; rela
++) {
1400 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
1401 #if KERNEL_ELFCLASS == ELFCLASS64
1402 if (elf
->hdr
->e_machine
== EM_MIPS
) {
1404 r_sym
= ELF64_MIPS_R_SYM(rela
->r_info
);
1405 r_sym
= TO_NATIVE(r_sym
);
1406 r_typ
= ELF64_MIPS_R_TYPE(rela
->r_info
);
1407 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
1409 r
.r_info
= TO_NATIVE(rela
->r_info
);
1410 r_sym
= ELF_R_SYM(r
.r_info
);
1413 r
.r_info
= TO_NATIVE(rela
->r_info
);
1414 r_sym
= ELF_R_SYM(r
.r_info
);
1416 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
1417 sym
= elf
->symtab_start
+ r_sym
;
1418 /* Skip special sections */
1419 if (sym
->st_shndx
>= SHN_LORESERVE
)
1421 check_section_mismatch(modname
, elf
, &r
, sym
, fromsec
);
1425 static void section_rel(const char *modname
, struct elf_info
*elf
,
1432 const char *fromsec
;
1434 Elf_Rel
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1435 Elf_Rel
*stop
= (void *)start
+ sechdr
->sh_size
;
1437 fromsec
= sech_name(elf
, sechdr
);
1438 fromsec
+= strlen(".rel");
1439 /* if from section (name) is know good then skip it */
1440 if (match(fromsec
, section_white_list
))
1443 for (rel
= start
; rel
< stop
; rel
++) {
1444 r
.r_offset
= TO_NATIVE(rel
->r_offset
);
1445 #if KERNEL_ELFCLASS == ELFCLASS64
1446 if (elf
->hdr
->e_machine
== EM_MIPS
) {
1448 r_sym
= ELF64_MIPS_R_SYM(rel
->r_info
);
1449 r_sym
= TO_NATIVE(r_sym
);
1450 r_typ
= ELF64_MIPS_R_TYPE(rel
->r_info
);
1451 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
1453 r
.r_info
= TO_NATIVE(rel
->r_info
);
1454 r_sym
= ELF_R_SYM(r
.r_info
);
1457 r
.r_info
= TO_NATIVE(rel
->r_info
);
1458 r_sym
= ELF_R_SYM(r
.r_info
);
1461 switch (elf
->hdr
->e_machine
) {
1463 if (addend_386_rel(elf
, sechdr
, &r
))
1467 if (addend_arm_rel(elf
, sechdr
, &r
))
1471 if (addend_mips_rel(elf
, sechdr
, &r
))
1475 sym
= elf
->symtab_start
+ r_sym
;
1476 /* Skip special sections */
1477 if (sym
->st_shndx
>= SHN_LORESERVE
)
1479 check_section_mismatch(modname
, elf
, &r
, sym
, fromsec
);
1484 * A module includes a number of sections that are discarded
1485 * either when loaded or when used as built-in.
1486 * For loaded modules all functions marked __init and all data
1487 * marked __initdata will be discarded when the module has been intialized.
1488 * Likewise for modules used built-in the sections marked __exit
1489 * are discarded because __exit marked function are supposed to be called
1490 * only when a module is unloaded which never happens for built-in modules.
1491 * The check_sec_ref() function traverses all relocation records
1492 * to find all references to a section that reference a section that will
1493 * be discarded and warns about it.
1495 static void check_sec_ref(struct module
*mod
, const char *modname
,
1496 struct elf_info
*elf
)
1499 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
1501 /* Walk through all sections */
1502 for (i
= 0; i
< elf
->hdr
->e_shnum
; i
++) {
1503 check_section(modname
, elf
, &elf
->sechdrs
[i
]);
1504 /* We want to process only relocation sections and not .init */
1505 if (sechdrs
[i
].sh_type
== SHT_RELA
)
1506 section_rela(modname
, elf
, &elf
->sechdrs
[i
]);
1507 else if (sechdrs
[i
].sh_type
== SHT_REL
)
1508 section_rel(modname
, elf
, &elf
->sechdrs
[i
]);
1512 static void get_markers(struct elf_info
*info
, struct module
*mod
)
1514 const Elf_Shdr
*sh
= &info
->sechdrs
[info
->markers_strings_sec
];
1515 const char *strings
= (const char *) info
->hdr
+ sh
->sh_offset
;
1516 const Elf_Sym
*sym
, *first_sym
, *last_sym
;
1519 if (!info
->markers_strings_sec
)
1523 * First count the strings. We look for all the symbols defined
1524 * in the __markers_strings section named __mstrtab_*. For
1525 * these local names, the compiler puts a random .NNN suffix on,
1526 * so the names don't correspond exactly.
1528 first_sym
= last_sym
= NULL
;
1530 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++)
1531 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
&&
1532 sym
->st_shndx
== info
->markers_strings_sec
&&
1533 !strncmp(info
->strtab
+ sym
->st_name
,
1534 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1535 if (first_sym
== NULL
)
1545 * Now collect each name and format into a line for the output.
1547 * marker_name vmlinux marker %s format %d
1548 * The format string after the second \t can use whitespace.
1550 mod
->markers
= NOFAIL(malloc(sizeof mod
->markers
[0] * n
));
1554 for (sym
= first_sym
; sym
<= last_sym
; sym
++)
1555 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
&&
1556 sym
->st_shndx
== info
->markers_strings_sec
&&
1557 !strncmp(info
->strtab
+ sym
->st_name
,
1558 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1559 const char *name
= strings
+ sym
->st_value
;
1560 const char *fmt
= strchr(name
, '\0') + 1;
1562 asprintf(&line
, "%s\t%s\t%s\n", name
, mod
->name
, fmt
);
1564 mod
->markers
[n
++] = line
;
1568 static void read_symbols(char *modname
)
1570 const char *symname
;
1574 struct elf_info info
= { };
1577 if (!parse_elf(&info
, modname
))
1580 mod
= new_module(modname
);
1582 /* When there's no vmlinux, don't print warnings about
1583 * unresolved symbols (since there'll be too many ;) */
1584 if (is_vmlinux(modname
)) {
1589 license
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "license");
1590 if (info
.modinfo
&& !license
&& !is_vmlinux(modname
))
1591 warn("modpost: missing MODULE_LICENSE() in %s\n"
1592 "see include/linux/module.h for "
1593 "more information\n", modname
);
1595 if (license_is_gpl_compatible(license
))
1596 mod
->gpl_compatible
= 1;
1598 mod
->gpl_compatible
= 0;
1601 license
= get_next_modinfo(info
.modinfo
, info
.modinfo_len
,
1602 "license", license
);
1605 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1606 symname
= info
.strtab
+ sym
->st_name
;
1608 handle_modversions(mod
, &info
, sym
, symname
);
1609 handle_moddevtable(mod
, &info
, sym
, symname
);
1611 if (!is_vmlinux(modname
) ||
1612 (is_vmlinux(modname
) && vmlinux_section_warnings
))
1613 check_sec_ref(mod
, modname
, &info
);
1615 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
1617 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
1618 version
- (char *)info
.hdr
);
1619 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
1620 get_src_version(modname
, mod
->srcversion
,
1621 sizeof(mod
->srcversion
)-1);
1623 get_markers(&info
, mod
);
1625 parse_elf_finish(&info
);
1627 /* Our trick to get versioning for module struct etc. - it's
1628 * never passed as an argument to an exported function, so
1629 * the automatic versioning doesn't pick it up, but it's really
1630 * important anyhow */
1632 mod
->unres
= alloc_symbol("module_layout", 0, mod
->unres
);
1637 /* We first write the generated file into memory using the
1638 * following helper, then compare to the file on disk and
1639 * only update the later if anything changed */
1641 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1642 const char *fmt
, ...)
1649 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1650 buf_write(buf
, tmp
, len
);
1654 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1656 if (buf
->size
- buf
->pos
< len
) {
1657 buf
->size
+= len
+ SZ
;
1658 buf
->p
= realloc(buf
->p
, buf
->size
);
1660 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1664 static void check_for_gpl_usage(enum export exp
, const char *m
, const char *s
)
1666 const char *e
= is_vmlinux(m
) ?"":".ko";
1670 fatal("modpost: GPL-incompatible module %s%s "
1671 "uses GPL-only symbol '%s'\n", m
, e
, s
);
1673 case export_unused_gpl
:
1674 fatal("modpost: GPL-incompatible module %s%s "
1675 "uses GPL-only symbol marked UNUSED '%s'\n", m
, e
, s
);
1677 case export_gpl_future
:
1678 warn("modpost: GPL-incompatible module %s%s "
1679 "uses future GPL-only symbol '%s'\n", m
, e
, s
);
1683 case export_unknown
:
1689 static void check_for_unused(enum export exp
, const char *m
, const char *s
)
1691 const char *e
= is_vmlinux(m
) ?"":".ko";
1695 case export_unused_gpl
:
1696 warn("modpost: module %s%s "
1697 "uses symbol '%s' marked UNUSED\n", m
, e
, s
);
1705 static void check_exports(struct module
*mod
)
1707 struct symbol
*s
, *exp
;
1709 for (s
= mod
->unres
; s
; s
= s
->next
) {
1710 const char *basename
;
1711 exp
= find_symbol(s
->name
);
1712 if (!exp
|| exp
->module
== mod
)
1714 basename
= strrchr(mod
->name
, '/');
1718 basename
= mod
->name
;
1719 if (!mod
->gpl_compatible
)
1720 check_for_gpl_usage(exp
->export
, basename
, exp
->name
);
1721 check_for_unused(exp
->export
, basename
, exp
->name
);
1726 * Header for the generated file
1728 static void add_header(struct buffer
*b
, struct module
*mod
)
1730 buf_printf(b
, "#include <linux/module.h>\n");
1731 buf_printf(b
, "#include <linux/vermagic.h>\n");
1732 buf_printf(b
, "#include <linux/compiler.h>\n");
1733 buf_printf(b
, "\n");
1734 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1735 buf_printf(b
, "\n");
1736 buf_printf(b
, "struct module __this_module\n");
1737 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1738 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
1740 buf_printf(b
, " .init = init_module,\n");
1741 if (mod
->has_cleanup
)
1742 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1743 " .exit = cleanup_module,\n"
1745 buf_printf(b
, " .arch = MODULE_ARCH_INIT,\n");
1746 buf_printf(b
, "};\n");
1749 static void add_staging_flag(struct buffer
*b
, const char *name
)
1751 static const char *staging_dir
= "drivers/staging";
1753 if (strncmp(staging_dir
, name
, strlen(staging_dir
)) == 0)
1754 buf_printf(b
, "\nMODULE_INFO(staging, \"Y\");\n");
1758 * Record CRCs for unresolved symbols
1760 static int add_versions(struct buffer
*b
, struct module
*mod
)
1762 struct symbol
*s
, *exp
;
1765 for (s
= mod
->unres
; s
; s
= s
->next
) {
1766 exp
= find_symbol(s
->name
);
1767 if (!exp
|| exp
->module
== mod
) {
1768 if (have_vmlinux
&& !s
->weak
) {
1769 if (warn_unresolved
) {
1770 warn("\"%s\" [%s.ko] undefined!\n",
1771 s
->name
, mod
->name
);
1773 merror("\"%s\" [%s.ko] undefined!\n",
1774 s
->name
, mod
->name
);
1780 s
->module
= exp
->module
;
1781 s
->crc_valid
= exp
->crc_valid
;
1788 buf_printf(b
, "\n");
1789 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1790 buf_printf(b
, "__used\n");
1791 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
1793 for (s
= mod
->unres
; s
; s
= s
->next
) {
1796 if (!s
->crc_valid
) {
1797 warn("\"%s\" [%s.ko] has no CRC!\n",
1798 s
->name
, mod
->name
);
1801 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1804 buf_printf(b
, "};\n");
1809 static void add_depends(struct buffer
*b
, struct module
*mod
,
1810 struct module
*modules
)
1816 for (m
= modules
; m
; m
= m
->next
)
1817 m
->seen
= is_vmlinux(m
->name
);
1819 buf_printf(b
, "\n");
1820 buf_printf(b
, "static const char __module_depends[]\n");
1821 buf_printf(b
, "__used\n");
1822 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1823 buf_printf(b
, "\"depends=");
1824 for (s
= mod
->unres
; s
; s
= s
->next
) {
1829 if (s
->module
->seen
)
1832 s
->module
->seen
= 1;
1833 p
= strrchr(s
->module
->name
, '/');
1837 p
= s
->module
->name
;
1838 buf_printf(b
, "%s%s", first
? "" : ",", p
);
1841 buf_printf(b
, "\";\n");
1844 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1846 if (mod
->srcversion
[0]) {
1847 buf_printf(b
, "\n");
1848 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1853 static void write_if_changed(struct buffer
*b
, const char *fname
)
1859 file
= fopen(fname
, "r");
1863 if (fstat(fileno(file
), &st
) < 0)
1866 if (st
.st_size
!= b
->pos
)
1869 tmp
= NOFAIL(malloc(b
->pos
));
1870 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1873 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1885 file
= fopen(fname
, "w");
1890 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1897 /* parse Module.symvers file. line format:
1898 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1900 static void read_dump(const char *fname
, unsigned int kernel
)
1902 unsigned long size
, pos
= 0;
1903 void *file
= grab_file(fname
, &size
);
1907 /* No symbol versions, silently ignore */
1910 while ((line
= get_next_line(&pos
, file
, size
))) {
1911 char *symname
, *modname
, *d
, *export
, *end
;
1916 if (!(symname
= strchr(line
, '\t')))
1919 if (!(modname
= strchr(symname
, '\t')))
1922 if ((export
= strchr(modname
, '\t')) != NULL
)
1924 if (export
&& ((end
= strchr(export
, '\t')) != NULL
))
1926 crc
= strtoul(line
, &d
, 16);
1927 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1929 mod
= find_module(modname
);
1931 if (is_vmlinux(modname
))
1933 mod
= new_module(modname
);
1936 s
= sym_add_exported(symname
, mod
, export_no(export
));
1939 sym_update_crc(symname
, mod
, crc
, export_no(export
));
1943 fatal("parse error in symbol dump file\n");
1946 /* For normal builds always dump all symbols.
1947 * For external modules only dump symbols
1948 * that are not read from kernel Module.symvers.
1950 static int dump_sym(struct symbol
*sym
)
1952 if (!external_module
)
1954 if (sym
->vmlinux
|| sym
->kernel
)
1959 static void write_dump(const char *fname
)
1961 struct buffer buf
= { };
1962 struct symbol
*symbol
;
1965 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1966 symbol
= symbolhash
[n
];
1968 if (dump_sym(symbol
))
1969 buf_printf(&buf
, "0x%08x\t%s\t%s\t%s\n",
1970 symbol
->crc
, symbol
->name
,
1971 symbol
->module
->name
,
1972 export_str(symbol
->export
));
1973 symbol
= symbol
->next
;
1976 write_if_changed(&buf
, fname
);
1979 static void add_marker(struct module
*mod
, const char *name
, const char *fmt
)
1982 asprintf(&line
, "%s\t%s\t%s\n", name
, mod
->name
, fmt
);
1985 mod
->markers
= NOFAIL(realloc(mod
->markers
, ((mod
->nmarkers
+ 1) *
1986 sizeof mod
->markers
[0])));
1987 mod
->markers
[mod
->nmarkers
++] = line
;
1990 static void read_markers(const char *fname
)
1992 unsigned long size
, pos
= 0;
1993 void *file
= grab_file(fname
, &size
);
1996 if (!file
) /* No old markers, silently ignore */
1999 while ((line
= get_next_line(&pos
, file
, size
))) {
2000 char *marker
, *modname
, *fmt
;
2004 modname
= strchr(marker
, '\t');
2008 fmt
= strchr(modname
, '\t');
2012 if (*marker
== '\0' || *modname
== '\0')
2015 mod
= find_module(modname
);
2017 mod
= new_module(modname
);
2020 if (is_vmlinux(modname
)) {
2026 add_marker(mod
, marker
, fmt
);
2028 release_file(file
, size
);
2031 fatal("parse error in markers list file\n");
2034 static int compare_strings(const void *a
, const void *b
)
2036 return strcmp(*(const char **) a
, *(const char **) b
);
2039 static void write_markers(const char *fname
)
2041 struct buffer buf
= { };
2045 for (mod
= modules
; mod
; mod
= mod
->next
)
2046 if ((!external_module
|| !mod
->skip
) && mod
->markers
!= NULL
) {
2048 * Sort the strings so we can skip duplicates when
2049 * we write them out.
2051 qsort(mod
->markers
, mod
->nmarkers
,
2052 sizeof mod
->markers
[0], &compare_strings
);
2053 for (i
= 0; i
< mod
->nmarkers
; ++i
) {
2054 char *line
= mod
->markers
[i
];
2055 buf_write(&buf
, line
, strlen(line
));
2056 while (i
+ 1 < mod
->nmarkers
&&
2057 !strcmp(mod
->markers
[i
],
2058 mod
->markers
[i
+ 1]))
2059 free(mod
->markers
[i
++]);
2060 free(mod
->markers
[i
]);
2063 mod
->markers
= NULL
;
2066 write_if_changed(&buf
, fname
);
2069 struct ext_sym_list
{
2070 struct ext_sym_list
*next
;
2074 int main(int argc
, char **argv
)
2077 struct buffer buf
= { };
2078 char *kernel_read
= NULL
, *module_read
= NULL
;
2079 char *dump_write
= NULL
;
2080 char *markers_read
= NULL
;
2081 char *markers_write
= NULL
;
2084 struct ext_sym_list
*extsym_iter
;
2085 struct ext_sym_list
*extsym_start
= NULL
;
2087 while ((opt
= getopt(argc
, argv
, "i:I:e:cmsSo:awM:K:")) != -1) {
2090 kernel_read
= optarg
;
2093 module_read
= optarg
;
2094 external_module
= 1;
2100 external_module
= 1;
2102 NOFAIL(malloc(sizeof(*extsym_iter
)));
2103 extsym_iter
->next
= extsym_start
;
2104 extsym_iter
->file
= optarg
;
2105 extsym_start
= extsym_iter
;
2111 dump_write
= optarg
;
2117 vmlinux_section_warnings
= 0;
2120 sec_mismatch_verbose
= 0;
2123 warn_unresolved
= 1;
2126 markers_write
= optarg
;
2129 markers_read
= optarg
;
2137 read_dump(kernel_read
, 1);
2139 read_dump(module_read
, 0);
2140 while (extsym_start
) {
2141 read_dump(extsym_start
->file
, 0);
2142 extsym_iter
= extsym_start
->next
;
2144 extsym_start
= extsym_iter
;
2147 while (optind
< argc
)
2148 read_symbols(argv
[optind
++]);
2150 for (mod
= modules
; mod
; mod
= mod
->next
) {
2158 for (mod
= modules
; mod
; mod
= mod
->next
) {
2159 char fname
[strlen(mod
->name
) + 10];
2166 add_header(&buf
, mod
);
2167 add_staging_flag(&buf
, mod
->name
);
2168 err
|= add_versions(&buf
, mod
);
2169 add_depends(&buf
, mod
, modules
);
2170 add_moddevtable(&buf
, mod
);
2171 add_srcversion(&buf
, mod
);
2173 sprintf(fname
, "%s.mod.c", mod
->name
);
2174 write_if_changed(&buf
, fname
);
2178 write_dump(dump_write
);
2179 if (sec_mismatch_count
&& !sec_mismatch_verbose
)
2180 warn("modpost: Found %d section mismatch(es).\n"
2181 "To see full details build your kernel with:\n"
2182 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2183 sec_mismatch_count
);
2186 read_markers(markers_read
);
2189 write_markers(markers_write
);