1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006 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 ...
16 #include "../../include/linux/license.h"
18 /* Are we using CONFIG_MODVERSIONS? */
20 /* Warn about undefined symbols? (do so if we have vmlinux) */
22 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
23 static int all_versions
= 0;
24 /* If we are modposting external module set to 1 */
25 static int external_module
= 0;
26 /* Only warn about unresolved symbols */
27 static int warn_unresolved
= 0;
28 /* How a symbol is exported */
30 export_plain
, export_unused
, export_gpl
,
31 export_unused_gpl
, export_gpl_future
, export_unknown
34 void fatal(const char *fmt
, ...)
38 fprintf(stderr
, "FATAL: ");
40 va_start(arglist
, fmt
);
41 vfprintf(stderr
, fmt
, arglist
);
47 void warn(const char *fmt
, ...)
51 fprintf(stderr
, "WARNING: ");
53 va_start(arglist
, fmt
);
54 vfprintf(stderr
, fmt
, arglist
);
58 static int is_vmlinux(const char *modname
)
62 if ((myname
= strrchr(modname
, '/')))
67 return strcmp(myname
, "vmlinux") == 0;
70 void *do_nofail(void *ptr
, const char *expr
)
73 fatal("modpost: Memory allocation failure: %s.\n", expr
);
78 /* A list of all modules we processed */
80 static struct module
*modules
;
82 static struct module
*find_module(char *modname
)
86 for (mod
= modules
; mod
; mod
= mod
->next
)
87 if (strcmp(mod
->name
, modname
) == 0)
92 static struct module
*new_module(char *modname
)
97 mod
= NOFAIL(malloc(sizeof(*mod
)));
98 memset(mod
, 0, sizeof(*mod
));
99 p
= NOFAIL(strdup(modname
));
101 /* strip trailing .o */
102 if ((s
= strrchr(p
, '.')) != NULL
)
103 if (strcmp(s
, ".o") == 0)
108 mod
->gpl_compatible
= -1;
115 /* A hash of all exported symbols,
116 * struct symbol is also used for lists of unresolved symbols */
118 #define SYMBOL_HASH_SIZE 1024
122 struct module
*module
;
126 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
127 unsigned int kernel
:1; /* 1 if symbol is from kernel
128 * (only for external modules) **/
129 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
130 enum export export
; /* Type of export */
134 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
136 /* This is based on the hash agorithm from gdbm, via tdb */
137 static inline unsigned int tdb_hash(const char *name
)
139 unsigned value
; /* Used to compute the hash value. */
140 unsigned i
; /* Used to cycle through random values. */
142 /* Set the initial value from the key size. */
143 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
144 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
146 return (1103515243 * value
+ 12345);
150 * Allocate a new symbols for use in the hash of exported symbols or
151 * the list of unresolved symbols per module
153 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
156 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
158 memset(s
, 0, sizeof(*s
));
159 strcpy(s
->name
, name
);
165 /* For the hash of exported symbols */
166 static struct symbol
*new_symbol(const char *name
, struct module
*module
,
172 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
173 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
174 new->module
= module
;
175 new->export
= export
;
179 static struct symbol
*find_symbol(const char *name
)
183 /* For our purposes, .foo matches foo. PPC64 needs this. */
187 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
188 if (strcmp(s
->name
, name
) == 0)
198 { .str
= "EXPORT_SYMBOL", .export
= export_plain
},
199 { .str
= "EXPORT_UNUSED_SYMBOL", .export
= export_unused
},
200 { .str
= "EXPORT_SYMBOL_GPL", .export
= export_gpl
},
201 { .str
= "EXPORT_UNUSED_SYMBOL_GPL", .export
= export_unused_gpl
},
202 { .str
= "EXPORT_SYMBOL_GPL_FUTURE", .export
= export_gpl_future
},
203 { .str
= "(unknown)", .export
= export_unknown
},
207 static const char *export_str(enum export ex
)
209 return export_list
[ex
].str
;
212 static enum export
export_no(const char * s
)
216 return export_unknown
;
217 for (i
= 0; export_list
[i
].export
!= export_unknown
; i
++) {
218 if (strcmp(export_list
[i
].str
, s
) == 0)
219 return export_list
[i
].export
;
221 return export_unknown
;
224 static enum export
export_from_sec(struct elf_info
*elf
, Elf_Section sec
)
226 if (sec
== elf
->export_sec
)
228 else if (sec
== elf
->export_unused_sec
)
229 return export_unused
;
230 else if (sec
== elf
->export_gpl_sec
)
232 else if (sec
== elf
->export_unused_gpl_sec
)
233 return export_unused_gpl
;
234 else if (sec
== elf
->export_gpl_future_sec
)
235 return export_gpl_future
;
237 return export_unknown
;
241 * Add an exported symbol - it may have already been added without a
242 * CRC, in this case just update the CRC
244 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
247 struct symbol
*s
= find_symbol(name
);
250 s
= new_symbol(name
, mod
, export
);
253 warn("%s: '%s' exported twice. Previous export "
254 "was in %s%s\n", mod
->name
, name
,
256 is_vmlinux(s
->module
->name
) ?"":".ko");
260 s
->vmlinux
= is_vmlinux(mod
->name
);
266 static void sym_update_crc(const char *name
, struct module
*mod
,
267 unsigned int crc
, enum export export
)
269 struct symbol
*s
= find_symbol(name
);
272 s
= new_symbol(name
, mod
, export
);
277 void *grab_file(const char *filename
, unsigned long *size
)
283 fd
= open(filename
, O_RDONLY
);
284 if (fd
< 0 || fstat(fd
, &st
) != 0)
288 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
291 if (map
== MAP_FAILED
)
297 * Return a copy of the next line in a mmap'ed file.
298 * spaces in the beginning of the line is trimmed away.
299 * Return a pointer to a static buffer.
301 char* get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
303 static char line
[4096];
306 signed char *p
= (signed char *)file
+ *pos
;
309 for (; *pos
< size
; (*pos
)++)
311 if (skip
&& isspace(*p
)) {
316 if (*p
!= '\n' && (*pos
< size
)) {
320 break; /* Too long, stop */
331 void release_file(void *file
, unsigned long size
)
336 static void parse_elf(struct elf_info
*info
, const char *filename
)
339 Elf_Ehdr
*hdr
= info
->hdr
;
343 hdr
= grab_file(filename
, &info
->size
);
349 if (info
->size
< sizeof(*hdr
))
352 /* Fix endianness in ELF header */
353 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
354 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
355 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
356 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
357 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
358 info
->sechdrs
= sechdrs
;
360 /* Fix endianness in section headers */
361 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
362 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
363 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
364 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
365 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
366 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
368 /* Find symbol table. */
369 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
370 const char *secstrings
371 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
374 if (sechdrs
[i
].sh_offset
> info
->size
)
376 secname
= secstrings
+ sechdrs
[i
].sh_name
;
377 if (strcmp(secname
, ".modinfo") == 0) {
378 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
379 info
->modinfo_len
= sechdrs
[i
].sh_size
;
380 } else if (strcmp(secname
, "__ksymtab") == 0)
381 info
->export_sec
= i
;
382 else if (strcmp(secname
, "__ksymtab_unused") == 0)
383 info
->export_unused_sec
= i
;
384 else if (strcmp(secname
, "__ksymtab_gpl") == 0)
385 info
->export_gpl_sec
= i
;
386 else if (strcmp(secname
, "__ksymtab_unused_gpl") == 0)
387 info
->export_unused_gpl_sec
= i
;
388 else if (strcmp(secname
, "__ksymtab_gpl_future") == 0)
389 info
->export_gpl_future_sec
= i
;
391 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
394 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
395 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
396 + sechdrs
[i
].sh_size
;
397 info
->strtab
= (void *)hdr
+
398 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
400 if (!info
->symtab_start
) {
401 fatal("%s has no symtab?\n", filename
);
403 /* Fix endianness in symbols */
404 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
405 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
406 sym
->st_name
= TO_NATIVE(sym
->st_name
);
407 sym
->st_value
= TO_NATIVE(sym
->st_value
);
408 sym
->st_size
= TO_NATIVE(sym
->st_size
);
413 fatal("%s is truncated.\n", filename
);
416 static void parse_elf_finish(struct elf_info
*info
)
418 release_file(info
->hdr
, info
->size
);
421 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
422 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
424 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
425 Elf_Sym
*sym
, const char *symname
)
428 enum export export
= export_from_sec(info
, sym
->st_shndx
);
430 switch (sym
->st_shndx
) {
432 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
436 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
437 crc
= (unsigned int) sym
->st_value
;
438 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
,
443 /* undefined symbol */
444 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
445 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
447 /* ignore global offset table */
448 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
450 /* ignore __this_module, it will be resolved shortly */
451 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
453 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
454 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
455 /* add compatibility with older glibc */
456 #ifndef STT_SPARC_REGISTER
457 #define STT_SPARC_REGISTER STT_REGISTER
459 if (info
->hdr
->e_machine
== EM_SPARC
||
460 info
->hdr
->e_machine
== EM_SPARCV9
) {
461 /* Ignore register directives. */
462 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
464 if (symname
[0] == '.') {
465 char *munged
= strdup(symname
);
467 munged
[1] = toupper(munged
[1]);
473 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
474 strlen(MODULE_SYMBOL_PREFIX
)) == 0)
475 mod
->unres
= alloc_symbol(symname
+
476 strlen(MODULE_SYMBOL_PREFIX
),
477 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
481 /* All exported symbols */
482 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
483 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
,
486 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
488 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
489 mod
->has_cleanup
= 1;
495 * Parse tag=value strings from .modinfo section
497 static char *next_string(char *string
, unsigned long *secsize
)
499 /* Skip non-zero chars */
502 if ((*secsize
)-- <= 1)
506 /* Skip any zero padding. */
509 if ((*secsize
)-- <= 1)
515 static char *get_next_modinfo(void *modinfo
, unsigned long modinfo_len
,
516 const char *tag
, char *info
)
519 unsigned int taglen
= strlen(tag
);
520 unsigned long size
= modinfo_len
;
523 size
-= info
- (char *)modinfo
;
524 modinfo
= next_string(info
, &size
);
527 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
528 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
529 return p
+ taglen
+ 1;
534 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
538 return get_next_modinfo(modinfo
, modinfo_len
, tag
, NULL
);
542 * Test if string s ends in string sub
545 static int strrcmp(const char *s
, const char *sub
)
553 sublen
= strlen(sub
);
555 if ((slen
== 0) || (sublen
== 0))
561 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
565 * Whitelist to allow certain references to pass with no warning.
567 * If a module parameter is declared __initdata and permissions=0
568 * then this is legal despite the warning generated.
569 * We cannot see value of permissions here, so just ignore
571 * The pattern is identified by:
577 * Many drivers utilise a *driver container with references to
578 * add, remove, probe functions etc.
579 * These functions may often be marked __init and we do not want to
581 * the pattern is identified by:
582 * tosec = .init.text | .exit.text | .init.data
584 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
587 * Some symbols belong to init section but still it is ok to reference
588 * these from non-init sections as these symbols don't have any memory
589 * allocated for them and symbol address and value are same. So even
590 * if init section is freed, its ok to reference those symbols.
591 * For ex. symbols marking the init section boundaries.
592 * This pattern is identified by
593 * refsymname = __init_begin, _sinittext, _einittext
595 static int secref_whitelist(const char *modname
, const char *tosec
,
596 const char *fromsec
, const char *atsym
,
597 const char *refsymname
)
601 const char *pat2sym
[] = {
603 "_template", /* scsi uses *_template a lot */
604 "_sht", /* scsi also used *_sht to some extent */
612 const char *pat3refsym
[] = {
619 /* Check for pattern 1 */
620 if (strcmp(tosec
, ".init.data") != 0)
622 if (strncmp(fromsec
, ".data", strlen(".data")) != 0)
624 if (strncmp(atsym
, "__param", strlen("__param")) != 0)
630 /* Check for pattern 2 */
631 if ((strcmp(tosec
, ".init.text") != 0) &&
632 (strcmp(tosec
, ".exit.text") != 0) &&
633 (strcmp(tosec
, ".init.data") != 0))
635 if (strcmp(fromsec
, ".data") != 0)
638 for (s
= pat2sym
; *s
; s
++)
639 if (strrcmp(atsym
, *s
) == 0)
644 /* Whitelist all references from .pci_fixup section if vmlinux */
645 if (is_vmlinux(modname
)) {
646 if ((strcmp(fromsec
, ".pci_fixup") == 0) &&
647 (strcmp(tosec
, ".init.text") == 0))
650 /* Check for pattern 3 */
651 for (s
= pat3refsym
; *s
; s
++)
652 if (strcmp(refsymname
, *s
) == 0)
659 * Find symbol based on relocation record info.
660 * In some cases the symbol supplied is a valid symbol so
661 * return refsym. If st_name != 0 we assume this is a valid symbol.
662 * In other cases the symbol needs to be looked up in the symbol table
663 * based on section and address.
665 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf_Addr addr
,
670 if (relsym
->st_name
!= 0)
672 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
673 if (sym
->st_shndx
!= relsym
->st_shndx
)
675 if (sym
->st_value
== addr
)
682 * Find symbols before or equal addr and after addr - in the section sec.
683 * If we find two symbols with equal offset prefer one with a valid name.
684 * The ELF format may have a better way to detect what type of symbol
685 * it is, but this works for now.
687 static void find_symbols_between(struct elf_info
*elf
, Elf_Addr addr
,
689 Elf_Sym
**before
, Elf_Sym
**after
)
692 Elf_Ehdr
*hdr
= elf
->hdr
;
693 Elf_Addr beforediff
= ~0;
694 Elf_Addr afterdiff
= ~0;
695 const char *secstrings
= (void *)hdr
+
696 elf
->sechdrs
[hdr
->e_shstrndx
].sh_offset
;
701 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
704 if (sym
->st_shndx
>= SHN_LORESERVE
)
706 symsec
= secstrings
+ elf
->sechdrs
[sym
->st_shndx
].sh_name
;
707 if (strcmp(symsec
, sec
) != 0)
709 if (sym
->st_value
<= addr
) {
710 if ((addr
- sym
->st_value
) < beforediff
) {
711 beforediff
= addr
- sym
->st_value
;
714 else if ((addr
- sym
->st_value
) == beforediff
) {
715 /* equal offset, valid name? */
716 const char *name
= elf
->strtab
+ sym
->st_name
;
717 if (name
&& strlen(name
))
723 if ((sym
->st_value
- addr
) < afterdiff
) {
724 afterdiff
= sym
->st_value
- addr
;
727 else if ((sym
->st_value
- addr
) == afterdiff
) {
728 /* equal offset, valid name? */
729 const char *name
= elf
->strtab
+ sym
->st_name
;
730 if (name
&& strlen(name
))
738 * Print a warning about a section mismatch.
739 * Try to find symbols near it so user can find it.
740 * Check whitelist before warning - it may be a false positive.
742 static void warn_sec_mismatch(const char *modname
, const char *fromsec
,
743 struct elf_info
*elf
, Elf_Sym
*sym
, Elf_Rela r
)
745 const char *refsymname
= "";
746 Elf_Sym
*before
, *after
;
748 Elf_Ehdr
*hdr
= elf
->hdr
;
749 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
750 const char *secstrings
= (void *)hdr
+
751 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
752 const char *secname
= secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
;
754 find_symbols_between(elf
, r
.r_offset
, fromsec
, &before
, &after
);
756 refsym
= find_elf_symbol(elf
, r
.r_addend
, sym
);
757 if (refsym
&& strlen(elf
->strtab
+ refsym
->st_name
))
758 refsymname
= elf
->strtab
+ refsym
->st_name
;
760 /* check whitelist - we may ignore it */
762 secref_whitelist(modname
, secname
, fromsec
,
763 elf
->strtab
+ before
->st_name
, refsymname
))
766 if (before
&& after
) {
767 warn("%s - Section mismatch: reference to %s:%s from %s "
768 "between '%s' (at offset 0x%llx) and '%s'\n",
769 modname
, secname
, refsymname
, fromsec
,
770 elf
->strtab
+ before
->st_name
,
771 (long long)r
.r_offset
,
772 elf
->strtab
+ after
->st_name
);
774 warn("%s - Section mismatch: reference to %s:%s from %s "
775 "after '%s' (at offset 0x%llx)\n",
776 modname
, secname
, refsymname
, fromsec
,
777 elf
->strtab
+ before
->st_name
,
778 (long long)r
.r_offset
);
780 warn("%s - Section mismatch: reference to %s:%s from %s "
781 "before '%s' (at offset -0x%llx)\n",
782 modname
, secname
, refsymname
, fromsec
,
783 elf
->strtab
+ after
->st_name
,
784 (long long)r
.r_offset
);
786 warn("%s - Section mismatch: reference to %s:%s from %s "
788 modname
, secname
, fromsec
, refsymname
,
789 (long long)r
.r_offset
);
794 * A module includes a number of sections that are discarded
795 * either when loaded or when used as built-in.
796 * For loaded modules all functions marked __init and all data
797 * marked __initdata will be discarded when the module has been intialized.
798 * Likewise for modules used built-in the sections marked __exit
799 * are discarded because __exit marked function are supposed to be called
800 * only when a moduel is unloaded which never happes for built-in modules.
801 * The check_sec_ref() function traverses all relocation records
802 * to find all references to a section that reference a section that will
803 * be discarded and warns about it.
805 static void check_sec_ref(struct module
*mod
, const char *modname
,
806 struct elf_info
*elf
,
807 int section(const char*),
808 int section_ref_ok(const char *))
812 Elf_Ehdr
*hdr
= elf
->hdr
;
813 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
814 const char *secstrings
= (void *)hdr
+
815 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
817 /* Walk through all sections */
818 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
819 const char *name
= secstrings
+ sechdrs
[i
].sh_name
;
823 /* We want to process only relocation sections and not .init */
824 if (sechdrs
[i
].sh_type
== SHT_RELA
) {
826 Elf_Rela
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
827 Elf_Rela
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
828 name
+= strlen(".rela");
829 if (section_ref_ok(name
))
832 for (rela
= start
; rela
< stop
; rela
++) {
833 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
834 #if KERNEL_ELFCLASS == ELFCLASS64
835 if (hdr
->e_machine
== EM_MIPS
) {
836 r_sym
= ELF64_MIPS_R_SYM(rela
->r_info
);
837 r_sym
= TO_NATIVE(r_sym
);
839 r
.r_info
= TO_NATIVE(rela
->r_info
);
840 r_sym
= ELF_R_SYM(r
.r_info
);
843 r
.r_info
= TO_NATIVE(rela
->r_info
);
844 r_sym
= ELF_R_SYM(r
.r_info
);
846 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
847 sym
= elf
->symtab_start
+ r_sym
;
848 /* Skip special sections */
849 if (sym
->st_shndx
>= SHN_LORESERVE
)
852 secname
= secstrings
+
853 sechdrs
[sym
->st_shndx
].sh_name
;
854 if (section(secname
))
855 warn_sec_mismatch(modname
, name
,
858 } else if (sechdrs
[i
].sh_type
== SHT_REL
) {
860 Elf_Rel
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
861 Elf_Rel
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
862 name
+= strlen(".rel");
863 if (section_ref_ok(name
))
866 for (rel
= start
; rel
< stop
; rel
++) {
867 r
.r_offset
= TO_NATIVE(rel
->r_offset
);
868 #if KERNEL_ELFCLASS == ELFCLASS64
869 if (hdr
->e_machine
== EM_MIPS
) {
870 r_sym
= ELF64_MIPS_R_SYM(rel
->r_info
);
871 r_sym
= TO_NATIVE(r_sym
);
873 r
.r_info
= TO_NATIVE(rel
->r_info
);
874 r_sym
= ELF_R_SYM(r
.r_info
);
877 r
.r_info
= TO_NATIVE(rel
->r_info
);
878 r_sym
= ELF_R_SYM(r
.r_info
);
881 sym
= elf
->symtab_start
+ r_sym
;
882 /* Skip special sections */
883 if (sym
->st_shndx
>= SHN_LORESERVE
)
886 secname
= secstrings
+
887 sechdrs
[sym
->st_shndx
].sh_name
;
888 if (section(secname
))
889 warn_sec_mismatch(modname
, name
,
897 * Functions used only during module init is marked __init and is stored in
898 * a .init.text section. Likewise data is marked __initdata and stored in
899 * a .init.data section.
900 * If this section is one of these sections return 1
901 * See include/linux/init.h for the details
903 static int init_section(const char *name
)
905 if (strcmp(name
, ".init") == 0)
907 if (strncmp(name
, ".init.", strlen(".init.")) == 0)
913 * Identify sections from which references to a .init section is OK.
915 * Unfortunately references to read only data that referenced .init
916 * sections had to be excluded. Almost all of these are false
917 * positives, they are created by gcc. The downside of excluding rodata
918 * is that there really are some user references from rodata to
919 * init code, e.g. drivers/video/vgacon.c:
921 * const struct consw vga_con = {
922 * con_startup: vgacon_startup,
924 * where vgacon_startup is __init. If you want to wade through the false
925 * positives, take out the check for rodata.
927 static int init_section_ref_ok(const char *name
)
930 /* Absolute section names */
931 const char *namelist1
[] = {
933 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
934 ".toc1", /* used by ppc64 */
936 ".data.rel.ro", /* used by parisc64 */
939 "__bug_table", /* used by powerpc for BUG() */
947 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
948 "__ftr_fixup", /* powerpc cpu feature fixup */
949 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
952 /* Start of section names */
953 const char *namelist2
[] = {
962 /* part of section name */
963 const char *namelist3
[] = {
964 ".unwind", /* sample: IA_64.unwind.init.text */
968 for (s
= namelist1
; *s
; s
++)
969 if (strcmp(*s
, name
) == 0)
971 for (s
= namelist2
; *s
; s
++)
972 if (strncmp(*s
, name
, strlen(*s
)) == 0)
974 for (s
= namelist3
; *s
; s
++)
975 if (strstr(name
, *s
) != NULL
)
977 if (strrcmp(name
, ".init") == 0)
983 * Functions used only during module exit is marked __exit and is stored in
984 * a .exit.text section. Likewise data is marked __exitdata and stored in
985 * a .exit.data section.
986 * If this section is one of these sections return 1
987 * See include/linux/init.h for the details
989 static int exit_section(const char *name
)
991 if (strcmp(name
, ".exit.text") == 0)
993 if (strcmp(name
, ".exit.data") == 0)
1000 * Identify sections from which references to a .exit section is OK.
1002 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1003 * For our future {in}sanity, add a comment that this is the ppc .opd
1004 * section, not the ia64 .opd section.
1005 * ia64 .opd should not point to discarded sections.
1006 * [.rodata] like for .init.text we ignore .rodata references -same reason
1008 static int exit_section_ref_ok(const char *name
)
1011 /* Absolute section names */
1012 const char *namelist1
[] = {
1017 ".opd", /* See comment [OPD] */
1018 ".toc1", /* used by ppc64 */
1021 "__bug_table", /* used by powerpc for BUG() */
1024 ".parainstructions",
1029 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
1032 /* Start of section names */
1033 const char *namelist2
[] = {
1037 /* part of section name */
1038 const char *namelist3
[] = {
1039 ".unwind", /* Sample: IA_64.unwind.exit.text */
1043 for (s
= namelist1
; *s
; s
++)
1044 if (strcmp(*s
, name
) == 0)
1046 for (s
= namelist2
; *s
; s
++)
1047 if (strncmp(*s
, name
, strlen(*s
)) == 0)
1049 for (s
= namelist3
; *s
; s
++)
1050 if (strstr(name
, *s
) != NULL
)
1055 static void read_symbols(char *modname
)
1057 const char *symname
;
1061 struct elf_info info
= { };
1064 parse_elf(&info
, modname
);
1066 mod
= new_module(modname
);
1068 /* When there's no vmlinux, don't print warnings about
1069 * unresolved symbols (since there'll be too many ;) */
1070 if (is_vmlinux(modname
)) {
1075 license
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "license");
1077 if (license_is_gpl_compatible(license
))
1078 mod
->gpl_compatible
= 1;
1080 mod
->gpl_compatible
= 0;
1083 license
= get_next_modinfo(info
.modinfo
, info
.modinfo_len
,
1084 "license", license
);
1087 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1088 symname
= info
.strtab
+ sym
->st_name
;
1090 handle_modversions(mod
, &info
, sym
, symname
);
1091 handle_moddevtable(mod
, &info
, sym
, symname
);
1093 check_sec_ref(mod
, modname
, &info
, init_section
, init_section_ref_ok
);
1094 check_sec_ref(mod
, modname
, &info
, exit_section
, exit_section_ref_ok
);
1096 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
1098 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
1099 version
- (char *)info
.hdr
);
1100 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
1101 get_src_version(modname
, mod
->srcversion
,
1102 sizeof(mod
->srcversion
)-1);
1104 parse_elf_finish(&info
);
1106 /* Our trick to get versioning for struct_module - it's
1107 * never passed as an argument to an exported function, so
1108 * the automatic versioning doesn't pick it up, but it's really
1109 * important anyhow */
1111 mod
->unres
= alloc_symbol("struct_module", 0, mod
->unres
);
1116 /* We first write the generated file into memory using the
1117 * following helper, then compare to the file on disk and
1118 * only update the later if anything changed */
1120 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1121 const char *fmt
, ...)
1128 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1129 buf_write(buf
, tmp
, len
);
1133 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1135 if (buf
->size
- buf
->pos
< len
) {
1136 buf
->size
+= len
+ SZ
;
1137 buf
->p
= realloc(buf
->p
, buf
->size
);
1139 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1143 static void check_for_gpl_usage(enum export exp
, const char *m
, const char *s
)
1145 const char *e
= is_vmlinux(m
) ?"":".ko";
1149 fatal("modpost: GPL-incompatible module %s%s "
1150 "uses GPL-only symbol '%s'\n", m
, e
, s
);
1152 case export_unused_gpl
:
1153 fatal("modpost: GPL-incompatible module %s%s "
1154 "uses GPL-only symbol marked UNUSED '%s'\n", m
, e
, s
);
1156 case export_gpl_future
:
1157 warn("modpost: GPL-incompatible module %s%s "
1158 "uses future GPL-only symbol '%s'\n", m
, e
, s
);
1162 case export_unknown
:
1168 static void check_for_unused(enum export exp
, const char* m
, const char* s
)
1170 const char *e
= is_vmlinux(m
) ?"":".ko";
1174 case export_unused_gpl
:
1175 warn("modpost: module %s%s "
1176 "uses symbol '%s' marked UNUSED\n", m
, e
, s
);
1184 static void check_exports(struct module
*mod
)
1186 struct symbol
*s
, *exp
;
1188 for (s
= mod
->unres
; s
; s
= s
->next
) {
1189 const char *basename
;
1190 exp
= find_symbol(s
->name
);
1191 if (!exp
|| exp
->module
== mod
)
1193 basename
= strrchr(mod
->name
, '/');
1197 basename
= mod
->name
;
1198 if (!mod
->gpl_compatible
)
1199 check_for_gpl_usage(exp
->export
, basename
, exp
->name
);
1200 check_for_unused(exp
->export
, basename
, exp
->name
);
1205 * Header for the generated file
1207 static void add_header(struct buffer
*b
, struct module
*mod
)
1209 buf_printf(b
, "#include <linux/module.h>\n");
1210 buf_printf(b
, "#include <linux/vermagic.h>\n");
1211 buf_printf(b
, "#include <linux/compiler.h>\n");
1212 buf_printf(b
, "\n");
1213 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1214 buf_printf(b
, "\n");
1215 buf_printf(b
, "struct module __this_module\n");
1216 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1217 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
1219 buf_printf(b
, " .init = init_module,\n");
1220 if (mod
->has_cleanup
)
1221 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1222 " .exit = cleanup_module,\n"
1224 buf_printf(b
, "};\n");
1228 * Record CRCs for unresolved symbols
1230 static int add_versions(struct buffer
*b
, struct module
*mod
)
1232 struct symbol
*s
, *exp
;
1235 for (s
= mod
->unres
; s
; s
= s
->next
) {
1236 exp
= find_symbol(s
->name
);
1237 if (!exp
|| exp
->module
== mod
) {
1238 if (have_vmlinux
&& !s
->weak
) {
1239 warn("\"%s\" [%s.ko] undefined!\n",
1240 s
->name
, mod
->name
);
1241 err
= warn_unresolved
? 0 : 1;
1245 s
->module
= exp
->module
;
1246 s
->crc_valid
= exp
->crc_valid
;
1253 buf_printf(b
, "\n");
1254 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1255 buf_printf(b
, "__attribute_used__\n");
1256 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
1258 for (s
= mod
->unres
; s
; s
= s
->next
) {
1262 if (!s
->crc_valid
) {
1263 warn("\"%s\" [%s.ko] has no CRC!\n",
1264 s
->name
, mod
->name
);
1267 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1270 buf_printf(b
, "};\n");
1275 static void add_depends(struct buffer
*b
, struct module
*mod
,
1276 struct module
*modules
)
1282 for (m
= modules
; m
; m
= m
->next
) {
1283 m
->seen
= is_vmlinux(m
->name
);
1286 buf_printf(b
, "\n");
1287 buf_printf(b
, "static const char __module_depends[]\n");
1288 buf_printf(b
, "__attribute_used__\n");
1289 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1290 buf_printf(b
, "\"depends=");
1291 for (s
= mod
->unres
; s
; s
= s
->next
) {
1295 if (s
->module
->seen
)
1298 s
->module
->seen
= 1;
1299 buf_printf(b
, "%s%s", first
? "" : ",",
1300 strrchr(s
->module
->name
, '/') + 1);
1303 buf_printf(b
, "\";\n");
1306 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1308 if (mod
->srcversion
[0]) {
1309 buf_printf(b
, "\n");
1310 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1315 static void write_if_changed(struct buffer
*b
, const char *fname
)
1321 file
= fopen(fname
, "r");
1325 if (fstat(fileno(file
), &st
) < 0)
1328 if (st
.st_size
!= b
->pos
)
1331 tmp
= NOFAIL(malloc(b
->pos
));
1332 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1335 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1347 file
= fopen(fname
, "w");
1352 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1359 /* parse Module.symvers file. line format:
1360 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1362 static void read_dump(const char *fname
, unsigned int kernel
)
1364 unsigned long size
, pos
= 0;
1365 void *file
= grab_file(fname
, &size
);
1369 /* No symbol versions, silently ignore */
1372 while ((line
= get_next_line(&pos
, file
, size
))) {
1373 char *symname
, *modname
, *d
, *export
, *end
;
1378 if (!(symname
= strchr(line
, '\t')))
1381 if (!(modname
= strchr(symname
, '\t')))
1384 if ((export
= strchr(modname
, '\t')) != NULL
)
1386 if (export
&& ((end
= strchr(export
, '\t')) != NULL
))
1388 crc
= strtoul(line
, &d
, 16);
1389 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1392 if (!(mod
= find_module(modname
))) {
1393 if (is_vmlinux(modname
)) {
1396 mod
= new_module(NOFAIL(strdup(modname
)));
1399 s
= sym_add_exported(symname
, mod
, export_no(export
));
1402 sym_update_crc(symname
, mod
, crc
, export_no(export
));
1406 fatal("parse error in symbol dump file\n");
1409 /* For normal builds always dump all symbols.
1410 * For external modules only dump symbols
1411 * that are not read from kernel Module.symvers.
1413 static int dump_sym(struct symbol
*sym
)
1415 if (!external_module
)
1417 if (sym
->vmlinux
|| sym
->kernel
)
1422 static void write_dump(const char *fname
)
1424 struct buffer buf
= { };
1425 struct symbol
*symbol
;
1428 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1429 symbol
= symbolhash
[n
];
1431 if (dump_sym(symbol
))
1432 buf_printf(&buf
, "0x%08x\t%s\t%s\t%s\n",
1433 symbol
->crc
, symbol
->name
,
1434 symbol
->module
->name
,
1435 export_str(symbol
->export
));
1436 symbol
= symbol
->next
;
1439 write_if_changed(&buf
, fname
);
1442 int main(int argc
, char **argv
)
1445 struct buffer buf
= { };
1447 char *kernel_read
= NULL
, *module_read
= NULL
;
1448 char *dump_write
= NULL
;
1452 while ((opt
= getopt(argc
, argv
, "i:I:mo:aw")) != -1) {
1455 kernel_read
= optarg
;
1458 module_read
= optarg
;
1459 external_module
= 1;
1465 dump_write
= optarg
;
1471 warn_unresolved
= 1;
1479 read_dump(kernel_read
, 1);
1481 read_dump(module_read
, 0);
1483 while (optind
< argc
) {
1484 read_symbols(argv
[optind
++]);
1487 for (mod
= modules
; mod
; mod
= mod
->next
) {
1495 for (mod
= modules
; mod
; mod
= mod
->next
) {
1501 add_header(&buf
, mod
);
1502 err
|= add_versions(&buf
, mod
);
1503 add_depends(&buf
, mod
, modules
);
1504 add_moddevtable(&buf
, mod
);
1505 add_srcversion(&buf
, mod
);
1507 sprintf(fname
, "%s.mod.c", mod
->name
);
1508 write_if_changed(&buf
, fname
);
1512 write_dump(dump_write
);