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 void merror(const char *fmt
, ...)
62 fprintf(stderr
, "ERROR: ");
64 va_start(arglist
, fmt
);
65 vfprintf(stderr
, fmt
, arglist
);
69 static int is_vmlinux(const char *modname
)
73 if ((myname
= strrchr(modname
, '/')))
78 return (strcmp(myname
, "vmlinux") == 0) ||
79 (strcmp(myname
, "vmlinux.o") == 0);
82 void *do_nofail(void *ptr
, const char *expr
)
85 fatal("modpost: Memory allocation failure: %s.\n", expr
);
90 /* A list of all modules we processed */
92 static struct module
*modules
;
94 static struct module
*find_module(char *modname
)
98 for (mod
= modules
; mod
; mod
= mod
->next
)
99 if (strcmp(mod
->name
, modname
) == 0)
104 static struct module
*new_module(char *modname
)
109 mod
= NOFAIL(malloc(sizeof(*mod
)));
110 memset(mod
, 0, sizeof(*mod
));
111 p
= NOFAIL(strdup(modname
));
113 /* strip trailing .o */
114 if ((s
= strrchr(p
, '.')) != NULL
)
115 if (strcmp(s
, ".o") == 0)
120 mod
->gpl_compatible
= -1;
127 /* A hash of all exported symbols,
128 * struct symbol is also used for lists of unresolved symbols */
130 #define SYMBOL_HASH_SIZE 1024
134 struct module
*module
;
138 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
139 unsigned int kernel
:1; /* 1 if symbol is from kernel
140 * (only for external modules) **/
141 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
142 enum export export
; /* Type of export */
146 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
148 /* This is based on the hash agorithm from gdbm, via tdb */
149 static inline unsigned int tdb_hash(const char *name
)
151 unsigned value
; /* Used to compute the hash value. */
152 unsigned i
; /* Used to cycle through random values. */
154 /* Set the initial value from the key size. */
155 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
156 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
158 return (1103515243 * value
+ 12345);
162 * Allocate a new symbols for use in the hash of exported symbols or
163 * the list of unresolved symbols per module
165 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
168 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
170 memset(s
, 0, sizeof(*s
));
171 strcpy(s
->name
, name
);
177 /* For the hash of exported symbols */
178 static struct symbol
*new_symbol(const char *name
, struct module
*module
,
184 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
185 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
186 new->module
= module
;
187 new->export
= export
;
191 static struct symbol
*find_symbol(const char *name
)
195 /* For our purposes, .foo matches foo. PPC64 needs this. */
199 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
200 if (strcmp(s
->name
, name
) == 0)
210 { .str
= "EXPORT_SYMBOL", .export
= export_plain
},
211 { .str
= "EXPORT_UNUSED_SYMBOL", .export
= export_unused
},
212 { .str
= "EXPORT_SYMBOL_GPL", .export
= export_gpl
},
213 { .str
= "EXPORT_UNUSED_SYMBOL_GPL", .export
= export_unused_gpl
},
214 { .str
= "EXPORT_SYMBOL_GPL_FUTURE", .export
= export_gpl_future
},
215 { .str
= "(unknown)", .export
= export_unknown
},
219 static const char *export_str(enum export ex
)
221 return export_list
[ex
].str
;
224 static enum export
export_no(const char * s
)
228 return export_unknown
;
229 for (i
= 0; export_list
[i
].export
!= export_unknown
; i
++) {
230 if (strcmp(export_list
[i
].str
, s
) == 0)
231 return export_list
[i
].export
;
233 return export_unknown
;
236 static enum export
export_from_sec(struct elf_info
*elf
, Elf_Section sec
)
238 if (sec
== elf
->export_sec
)
240 else if (sec
== elf
->export_unused_sec
)
241 return export_unused
;
242 else if (sec
== elf
->export_gpl_sec
)
244 else if (sec
== elf
->export_unused_gpl_sec
)
245 return export_unused_gpl
;
246 else if (sec
== elf
->export_gpl_future_sec
)
247 return export_gpl_future
;
249 return export_unknown
;
253 * Add an exported symbol - it may have already been added without a
254 * CRC, in this case just update the CRC
256 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
259 struct symbol
*s
= find_symbol(name
);
262 s
= new_symbol(name
, mod
, export
);
265 warn("%s: '%s' exported twice. Previous export "
266 "was in %s%s\n", mod
->name
, name
,
268 is_vmlinux(s
->module
->name
) ?"":".ko");
272 s
->vmlinux
= is_vmlinux(mod
->name
);
278 static void sym_update_crc(const char *name
, struct module
*mod
,
279 unsigned int crc
, enum export export
)
281 struct symbol
*s
= find_symbol(name
);
284 s
= new_symbol(name
, mod
, export
);
289 void *grab_file(const char *filename
, unsigned long *size
)
295 fd
= open(filename
, O_RDONLY
);
296 if (fd
< 0 || fstat(fd
, &st
) != 0)
300 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
303 if (map
== MAP_FAILED
)
309 * Return a copy of the next line in a mmap'ed file.
310 * spaces in the beginning of the line is trimmed away.
311 * Return a pointer to a static buffer.
313 char* get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
315 static char line
[4096];
318 signed char *p
= (signed char *)file
+ *pos
;
321 for (; *pos
< size
; (*pos
)++)
323 if (skip
&& isspace(*p
)) {
328 if (*p
!= '\n' && (*pos
< size
)) {
332 break; /* Too long, stop */
343 void release_file(void *file
, unsigned long size
)
348 static int parse_elf(struct elf_info
*info
, const char *filename
)
355 hdr
= grab_file(filename
, &info
->size
);
361 if (info
->size
< sizeof(*hdr
)) {
362 /* file too small, assume this is an empty .o file */
365 /* Is this a valid ELF file? */
366 if ((hdr
->e_ident
[EI_MAG0
] != ELFMAG0
) ||
367 (hdr
->e_ident
[EI_MAG1
] != ELFMAG1
) ||
368 (hdr
->e_ident
[EI_MAG2
] != ELFMAG2
) ||
369 (hdr
->e_ident
[EI_MAG3
] != ELFMAG3
)) {
370 /* Not an ELF file - silently ignore it */
373 /* Fix endianness in ELF header */
374 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
375 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
376 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
377 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
378 hdr
->e_type
= TO_NATIVE(hdr
->e_type
);
379 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
380 info
->sechdrs
= sechdrs
;
382 /* Fix endianness in section headers */
383 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
384 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
385 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
386 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
387 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
388 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
389 sechdrs
[i
].sh_info
= TO_NATIVE(sechdrs
[i
].sh_info
);
390 sechdrs
[i
].sh_addr
= TO_NATIVE(sechdrs
[i
].sh_addr
);
392 /* Find symbol table. */
393 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
394 const char *secstrings
395 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
398 if (sechdrs
[i
].sh_offset
> info
->size
) {
399 fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename
, (unsigned int)sechdrs
[i
].sh_offset
, sizeof(*hdr
));
402 secname
= secstrings
+ sechdrs
[i
].sh_name
;
403 if (strcmp(secname
, ".modinfo") == 0) {
404 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
405 info
->modinfo_len
= sechdrs
[i
].sh_size
;
406 } else if (strcmp(secname
, "__ksymtab") == 0)
407 info
->export_sec
= i
;
408 else if (strcmp(secname
, "__ksymtab_unused") == 0)
409 info
->export_unused_sec
= i
;
410 else if (strcmp(secname
, "__ksymtab_gpl") == 0)
411 info
->export_gpl_sec
= i
;
412 else if (strcmp(secname
, "__ksymtab_unused_gpl") == 0)
413 info
->export_unused_gpl_sec
= i
;
414 else if (strcmp(secname
, "__ksymtab_gpl_future") == 0)
415 info
->export_gpl_future_sec
= i
;
417 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
420 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
421 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
422 + sechdrs
[i
].sh_size
;
423 info
->strtab
= (void *)hdr
+
424 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
426 if (!info
->symtab_start
) {
427 fatal("%s has no symtab?\n", filename
);
429 /* Fix endianness in symbols */
430 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
431 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
432 sym
->st_name
= TO_NATIVE(sym
->st_name
);
433 sym
->st_value
= TO_NATIVE(sym
->st_value
);
434 sym
->st_size
= TO_NATIVE(sym
->st_size
);
439 static void parse_elf_finish(struct elf_info
*info
)
441 release_file(info
->hdr
, info
->size
);
444 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
445 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
447 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
448 Elf_Sym
*sym
, const char *symname
)
451 enum export export
= export_from_sec(info
, sym
->st_shndx
);
453 switch (sym
->st_shndx
) {
455 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
459 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
460 crc
= (unsigned int) sym
->st_value
;
461 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
,
466 /* undefined symbol */
467 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
468 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
470 /* ignore global offset table */
471 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
473 /* ignore __this_module, it will be resolved shortly */
474 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
476 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
477 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
478 /* add compatibility with older glibc */
479 #ifndef STT_SPARC_REGISTER
480 #define STT_SPARC_REGISTER STT_REGISTER
482 if (info
->hdr
->e_machine
== EM_SPARC
||
483 info
->hdr
->e_machine
== EM_SPARCV9
) {
484 /* Ignore register directives. */
485 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
487 if (symname
[0] == '.') {
488 char *munged
= strdup(symname
);
490 munged
[1] = toupper(munged
[1]);
496 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
497 strlen(MODULE_SYMBOL_PREFIX
)) == 0)
498 mod
->unres
= alloc_symbol(symname
+
499 strlen(MODULE_SYMBOL_PREFIX
),
500 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
504 /* All exported symbols */
505 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
506 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
,
509 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
511 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
512 mod
->has_cleanup
= 1;
518 * Parse tag=value strings from .modinfo section
520 static char *next_string(char *string
, unsigned long *secsize
)
522 /* Skip non-zero chars */
525 if ((*secsize
)-- <= 1)
529 /* Skip any zero padding. */
532 if ((*secsize
)-- <= 1)
538 static char *get_next_modinfo(void *modinfo
, unsigned long modinfo_len
,
539 const char *tag
, char *info
)
542 unsigned int taglen
= strlen(tag
);
543 unsigned long size
= modinfo_len
;
546 size
-= info
- (char *)modinfo
;
547 modinfo
= next_string(info
, &size
);
550 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
551 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
552 return p
+ taglen
+ 1;
557 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
561 return get_next_modinfo(modinfo
, modinfo_len
, tag
, NULL
);
565 * Test if string s ends in string sub
568 static int strrcmp(const char *s
, const char *sub
)
576 sublen
= strlen(sub
);
578 if ((slen
== 0) || (sublen
== 0))
584 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
588 * Whitelist to allow certain references to pass with no warning.
591 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
592 * The pattern is identified by:
593 * fromsec = .text.init.refok | .data.init.refok
596 * If a module parameter is declared __initdata and permissions=0
597 * then this is legal despite the warning generated.
598 * We cannot see value of permissions here, so just ignore
600 * The pattern is identified by:
606 * Many drivers utilise a *driver container with references to
607 * add, remove, probe functions etc.
608 * These functions may often be marked __init and we do not want to
610 * the pattern is identified by:
611 * tosec = .init.text | .exit.text | .init.data
612 * fromsec = .data | .data.rel | .data.rel.*
613 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer
616 * Whitelist all refereces from .text.head to .init.data
617 * Whitelist all refereces from .text.head to .init.text
620 * Some symbols belong to init section but still it is ok to reference
621 * these from non-init sections as these symbols don't have any memory
622 * allocated for them and symbol address and value are same. So even
623 * if init section is freed, its ok to reference those symbols.
624 * For ex. symbols marking the init section boundaries.
625 * This pattern is identified by
626 * refsymname = __init_begin, _sinittext, _einittext
629 static int secref_whitelist(const char *modname
, const char *tosec
,
630 const char *fromsec
, const char *atsym
,
631 const char *refsymname
)
635 const char *pat2sym
[] = {
637 "_template", /* scsi uses *_template a lot */
638 "_timer", /* arm uses ops structures named _timer a lot */
639 "_sht", /* scsi also used *_sht to some extent */
647 const char *pat3refsym
[] = {
654 /* Check for pattern 0 */
655 if ((strcmp(fromsec
, ".text.init.refok") == 0) ||
656 (strcmp(fromsec
, ".data.init.refok") == 0))
659 /* Check for pattern 1 */
660 if (strcmp(tosec
, ".init.data") != 0)
662 if (strncmp(fromsec
, ".data", strlen(".data")) != 0)
664 if (strncmp(atsym
, "__param", strlen("__param")) != 0)
670 /* Check for pattern 2 */
671 if ((strcmp(tosec
, ".init.text") != 0) &&
672 (strcmp(tosec
, ".exit.text") != 0) &&
673 (strcmp(tosec
, ".init.data") != 0))
675 if ((strcmp(fromsec
, ".data") != 0) &&
676 (strcmp(fromsec
, ".data.rel") != 0) &&
677 (strncmp(fromsec
, ".data.rel.", strlen(".data.rel.")) != 0))
680 for (s
= pat2sym
; *s
; s
++)
681 if (strrcmp(atsym
, *s
) == 0)
686 /* Check for pattern 3 */
687 if ((strcmp(fromsec
, ".text.head") == 0) &&
688 ((strcmp(tosec
, ".init.data") == 0) ||
689 (strcmp(tosec
, ".init.text") == 0)))
692 /* Check for pattern 4 */
693 for (s
= pat3refsym
; *s
; s
++)
694 if (strcmp(refsymname
, *s
) == 0)
701 * Find symbol based on relocation record info.
702 * In some cases the symbol supplied is a valid symbol so
703 * return refsym. If st_name != 0 we assume this is a valid symbol.
704 * In other cases the symbol needs to be looked up in the symbol table
705 * based on section and address.
707 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf_Addr addr
,
712 if (relsym
->st_name
!= 0)
714 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
715 if (sym
->st_shndx
!= relsym
->st_shndx
)
717 if (ELF_ST_TYPE(sym
->st_info
) == STT_SECTION
)
719 if (sym
->st_value
== addr
)
725 static inline int is_arm_mapping_symbol(const char *str
)
727 return str
[0] == '$' && strchr("atd", str
[1])
728 && (str
[2] == '\0' || str
[2] == '.');
732 * If there's no name there, ignore it; likewise, ignore it if it's
733 * one of the magic symbols emitted used by current ARM tools.
735 * Otherwise if find_symbols_between() returns those symbols, they'll
736 * fail the whitelist tests and cause lots of false alarms ... fixable
737 * only by merging __exit and __init sections into __text, bloating
738 * the kernel (which is especially evil on embedded platforms).
740 static inline int is_valid_name(struct elf_info
*elf
, Elf_Sym
*sym
)
742 const char *name
= elf
->strtab
+ sym
->st_name
;
744 if (!name
|| !strlen(name
))
746 return !is_arm_mapping_symbol(name
);
750 * Find symbols before or equal addr and after addr - in the section sec.
751 * If we find two symbols with equal offset prefer one with a valid name.
752 * The ELF format may have a better way to detect what type of symbol
753 * it is, but this works for now.
755 static void find_symbols_between(struct elf_info
*elf
, Elf_Addr addr
,
757 Elf_Sym
**before
, Elf_Sym
**after
)
760 Elf_Ehdr
*hdr
= elf
->hdr
;
761 Elf_Addr beforediff
= ~0;
762 Elf_Addr afterdiff
= ~0;
763 const char *secstrings
= (void *)hdr
+
764 elf
->sechdrs
[hdr
->e_shstrndx
].sh_offset
;
769 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
772 if (sym
->st_shndx
>= SHN_LORESERVE
)
774 symsec
= secstrings
+ elf
->sechdrs
[sym
->st_shndx
].sh_name
;
775 if (strcmp(symsec
, sec
) != 0)
777 if (!is_valid_name(elf
, sym
))
779 if (sym
->st_value
<= addr
) {
780 if ((addr
- sym
->st_value
) < beforediff
) {
781 beforediff
= addr
- sym
->st_value
;
784 else if ((addr
- sym
->st_value
) == beforediff
) {
790 if ((sym
->st_value
- addr
) < afterdiff
) {
791 afterdiff
= sym
->st_value
- addr
;
794 else if ((sym
->st_value
- addr
) == afterdiff
) {
802 * Print a warning about a section mismatch.
803 * Try to find symbols near it so user can find it.
804 * Check whitelist before warning - it may be a false positive.
806 static void warn_sec_mismatch(const char *modname
, const char *fromsec
,
807 struct elf_info
*elf
, Elf_Sym
*sym
, Elf_Rela r
)
809 const char *refsymname
= "";
810 Elf_Sym
*before
, *after
;
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
;
816 const char *secname
= secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
;
818 find_symbols_between(elf
, r
.r_offset
, fromsec
, &before
, &after
);
820 refsym
= find_elf_symbol(elf
, r
.r_addend
, sym
);
821 if (refsym
&& strlen(elf
->strtab
+ refsym
->st_name
))
822 refsymname
= elf
->strtab
+ refsym
->st_name
;
824 /* check whitelist - we may ignore it */
826 secref_whitelist(modname
, secname
, fromsec
,
827 elf
->strtab
+ before
->st_name
, refsymname
))
830 if (before
&& after
) {
831 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
832 "(between '%s' and '%s')\n",
833 modname
, fromsec
, (unsigned long long)r
.r_offset
,
835 elf
->strtab
+ before
->st_name
,
836 elf
->strtab
+ after
->st_name
);
838 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
840 modname
, fromsec
, (unsigned long long)r
.r_offset
,
842 elf
->strtab
+ before
->st_name
);
844 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
845 "before '%s' (at offset -0x%llx)\n",
846 modname
, fromsec
, (unsigned long long)r
.r_offset
,
848 elf
->strtab
+ after
->st_name
);
850 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
851 modname
, fromsec
, (unsigned long long)r
.r_offset
,
852 secname
, refsymname
);
856 static unsigned int *reloc_location(struct elf_info
*elf
,
857 int rsection
, Elf_Rela
*r
)
859 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
860 int section
= sechdrs
[rsection
].sh_info
;
862 return (void *)elf
->hdr
+ sechdrs
[section
].sh_offset
+
863 (r
->r_offset
- sechdrs
[section
].sh_addr
);
866 static int addend_386_rel(struct elf_info
*elf
, int rsection
, Elf_Rela
*r
)
868 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
869 unsigned int *location
= reloc_location(elf
, rsection
, r
);
873 r
->r_addend
= TO_NATIVE(*location
);
876 r
->r_addend
= TO_NATIVE(*location
) + 4;
877 /* For CONFIG_RELOCATABLE=y */
878 if (elf
->hdr
->e_type
== ET_EXEC
)
879 r
->r_addend
+= r
->r_offset
;
885 static int addend_arm_rel(struct elf_info
*elf
, int rsection
, Elf_Rela
*r
)
887 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
891 /* From ARM ABI: (S + A) | T */
892 r
->r_addend
= (int)(long)(elf
->symtab_start
+ ELF_R_SYM(r
->r_info
));
895 /* From ARM ABI: ((S + A) | T) - P */
896 r
->r_addend
= (int)(long)(elf
->hdr
+ elf
->sechdrs
[rsection
].sh_offset
+
897 (r
->r_offset
- elf
->sechdrs
[rsection
].sh_addr
));
905 static int addend_mips_rel(struct elf_info
*elf
, int rsection
, Elf_Rela
*r
)
907 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
908 unsigned int *location
= reloc_location(elf
, rsection
, r
);
911 if (r_typ
== R_MIPS_HI16
)
912 return 1; /* skip this */
913 inst
= TO_NATIVE(*location
);
916 r
->r_addend
= inst
& 0xffff;
919 r
->r_addend
= (inst
& 0x03ffffff) << 2;
929 * A module includes a number of sections that are discarded
930 * either when loaded or when used as built-in.
931 * For loaded modules all functions marked __init and all data
932 * marked __initdata will be discarded when the module has been intialized.
933 * Likewise for modules used built-in the sections marked __exit
934 * are discarded because __exit marked function are supposed to be called
935 * only when a moduel is unloaded which never happes for built-in modules.
936 * The check_sec_ref() function traverses all relocation records
937 * to find all references to a section that reference a section that will
938 * be discarded and warns about it.
940 static void check_sec_ref(struct module
*mod
, const char *modname
,
941 struct elf_info
*elf
,
942 int section(const char*),
943 int section_ref_ok(const char *))
947 Elf_Ehdr
*hdr
= elf
->hdr
;
948 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
949 const char *secstrings
= (void *)hdr
+
950 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
952 /* Walk through all sections */
953 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
954 const char *name
= secstrings
+ sechdrs
[i
].sh_name
;
958 /* We want to process only relocation sections and not .init */
959 if (sechdrs
[i
].sh_type
== SHT_RELA
) {
961 Elf_Rela
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
962 Elf_Rela
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
963 name
+= strlen(".rela");
964 if (section_ref_ok(name
))
967 for (rela
= start
; rela
< stop
; rela
++) {
968 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
969 #if KERNEL_ELFCLASS == ELFCLASS64
970 if (hdr
->e_machine
== EM_MIPS
) {
972 r_sym
= ELF64_MIPS_R_SYM(rela
->r_info
);
973 r_sym
= TO_NATIVE(r_sym
);
974 r_typ
= ELF64_MIPS_R_TYPE(rela
->r_info
);
975 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
977 r
.r_info
= TO_NATIVE(rela
->r_info
);
978 r_sym
= ELF_R_SYM(r
.r_info
);
981 r
.r_info
= TO_NATIVE(rela
->r_info
);
982 r_sym
= ELF_R_SYM(r
.r_info
);
984 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
985 sym
= elf
->symtab_start
+ r_sym
;
986 /* Skip special sections */
987 if (sym
->st_shndx
>= SHN_LORESERVE
)
990 secname
= secstrings
+
991 sechdrs
[sym
->st_shndx
].sh_name
;
992 if (section(secname
))
993 warn_sec_mismatch(modname
, name
,
996 } else if (sechdrs
[i
].sh_type
== SHT_REL
) {
998 Elf_Rel
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
999 Elf_Rel
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
1000 name
+= strlen(".rel");
1001 if (section_ref_ok(name
))
1004 for (rel
= start
; rel
< stop
; rel
++) {
1005 r
.r_offset
= TO_NATIVE(rel
->r_offset
);
1006 #if KERNEL_ELFCLASS == ELFCLASS64
1007 if (hdr
->e_machine
== EM_MIPS
) {
1009 r_sym
= ELF64_MIPS_R_SYM(rel
->r_info
);
1010 r_sym
= TO_NATIVE(r_sym
);
1011 r_typ
= ELF64_MIPS_R_TYPE(rel
->r_info
);
1012 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
1014 r
.r_info
= TO_NATIVE(rel
->r_info
);
1015 r_sym
= ELF_R_SYM(r
.r_info
);
1018 r
.r_info
= TO_NATIVE(rel
->r_info
);
1019 r_sym
= ELF_R_SYM(r
.r_info
);
1022 switch (hdr
->e_machine
) {
1024 if (addend_386_rel(elf
, i
, &r
))
1028 if(addend_arm_rel(elf
, i
, &r
))
1032 if (addend_mips_rel(elf
, i
, &r
))
1036 sym
= elf
->symtab_start
+ r_sym
;
1037 /* Skip special sections */
1038 if (sym
->st_shndx
>= SHN_LORESERVE
)
1041 secname
= secstrings
+
1042 sechdrs
[sym
->st_shndx
].sh_name
;
1043 if (section(secname
))
1044 warn_sec_mismatch(modname
, name
,
1052 * Identify sections from which references to either a
1053 * .init or a .exit section is OK.
1055 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1056 * For our future {in}sanity, add a comment that this is the ppc .opd
1057 * section, not the ia64 .opd section.
1058 * ia64 .opd should not point to discarded sections.
1059 * [.rodata] like for .init.text we ignore .rodata references -same reason
1061 static int initexit_section_ref_ok(const char *name
)
1064 /* Absolute section names */
1065 const char *namelist1
[] = {
1066 "__bug_table", /* used by powerpc for BUG() */
1069 ".cranges", /* used by sh64 */
1071 ".machvec", /* ia64 + powerpc uses these */
1073 ".opd", /* See comment [OPD] */
1074 ".parainstructions",
1076 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
1082 /* Start of section names */
1083 const char *namelist2
[] = {
1086 ".note", /* ignore ELF notes - may contain anything */
1087 ".got", /* powerpc - global offset table */
1088 ".toc", /* powerpc - table of contents */
1091 /* part of section name */
1092 const char *namelist3
[] = {
1093 ".unwind", /* Sample: IA_64.unwind.exit.text */
1097 for (s
= namelist1
; *s
; s
++)
1098 if (strcmp(*s
, name
) == 0)
1100 for (s
= namelist2
; *s
; s
++)
1101 if (strncmp(*s
, name
, strlen(*s
)) == 0)
1103 for (s
= namelist3
; *s
; s
++)
1104 if (strstr(name
, *s
) != NULL
)
1110 * Functions used only during module init is marked __init and is stored in
1111 * a .init.text section. Likewise data is marked __initdata and stored in
1112 * a .init.data section.
1113 * If this section is one of these sections return 1
1114 * See include/linux/init.h for the details
1116 static int init_section(const char *name
)
1118 if (strcmp(name
, ".init") == 0)
1120 if (strncmp(name
, ".init.", strlen(".init.")) == 0)
1126 * Identify sections from which references to a .init section is OK.
1128 * Unfortunately references to read only data that referenced .init
1129 * sections had to be excluded. Almost all of these are false
1130 * positives, they are created by gcc. The downside of excluding rodata
1131 * is that there really are some user references from rodata to
1132 * init code, e.g. drivers/video/vgacon.c:
1134 * const struct consw vga_con = {
1135 * con_startup: vgacon_startup,
1137 * where vgacon_startup is __init. If you want to wade through the false
1138 * positives, take out the check for rodata.
1140 static int init_section_ref_ok(const char *name
)
1143 /* Absolute section names */
1144 const char *namelist1
[] = {
1145 "__dbe_table", /* MIPS generate these */
1146 "__ftr_fixup", /* powerpc cpu feature fixup */
1147 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
1149 ".data.rel.ro", /* used by parisc64 */
1154 /* Start of section names */
1155 const char *namelist2
[] = {
1162 if (initexit_section_ref_ok(name
))
1165 for (s
= namelist1
; *s
; s
++)
1166 if (strcmp(*s
, name
) == 0)
1168 for (s
= namelist2
; *s
; s
++)
1169 if (strncmp(*s
, name
, strlen(*s
)) == 0)
1172 /* If section name ends with ".init" we allow references
1173 * as is the case with .initcallN.init, .early_param.init, .taglist.init etc
1175 if (strrcmp(name
, ".init") == 0)
1181 * Functions used only during module exit is marked __exit and is stored in
1182 * a .exit.text section. Likewise data is marked __exitdata and stored in
1183 * a .exit.data section.
1184 * If this section is one of these sections return 1
1185 * See include/linux/init.h for the details
1187 static int exit_section(const char *name
)
1189 if (strcmp(name
, ".exit.text") == 0)
1191 if (strcmp(name
, ".exit.data") == 0)
1198 * Identify sections from which references to a .exit section is OK.
1200 static int exit_section_ref_ok(const char *name
)
1203 /* Absolute section names */
1204 const char *namelist1
[] = {
1212 if (initexit_section_ref_ok(name
))
1215 for (s
= namelist1
; *s
; s
++)
1216 if (strcmp(*s
, name
) == 0)
1221 static void read_symbols(char *modname
)
1223 const char *symname
;
1227 struct elf_info info
= { };
1230 if (!parse_elf(&info
, modname
))
1233 mod
= new_module(modname
);
1235 /* When there's no vmlinux, don't print warnings about
1236 * unresolved symbols (since there'll be too many ;) */
1237 if (is_vmlinux(modname
)) {
1242 license
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "license");
1244 if (license_is_gpl_compatible(license
))
1245 mod
->gpl_compatible
= 1;
1247 mod
->gpl_compatible
= 0;
1250 license
= get_next_modinfo(info
.modinfo
, info
.modinfo_len
,
1251 "license", license
);
1254 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1255 symname
= info
.strtab
+ sym
->st_name
;
1257 handle_modversions(mod
, &info
, sym
, symname
);
1258 handle_moddevtable(mod
, &info
, sym
, symname
);
1260 check_sec_ref(mod
, modname
, &info
, init_section
, init_section_ref_ok
);
1261 check_sec_ref(mod
, modname
, &info
, exit_section
, exit_section_ref_ok
);
1263 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
1265 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
1266 version
- (char *)info
.hdr
);
1267 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
1268 get_src_version(modname
, mod
->srcversion
,
1269 sizeof(mod
->srcversion
)-1);
1271 parse_elf_finish(&info
);
1273 /* Our trick to get versioning for struct_module - it's
1274 * never passed as an argument to an exported function, so
1275 * the automatic versioning doesn't pick it up, but it's really
1276 * important anyhow */
1278 mod
->unres
= alloc_symbol("struct_module", 0, mod
->unres
);
1283 /* We first write the generated file into memory using the
1284 * following helper, then compare to the file on disk and
1285 * only update the later if anything changed */
1287 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1288 const char *fmt
, ...)
1295 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1296 buf_write(buf
, tmp
, len
);
1300 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1302 if (buf
->size
- buf
->pos
< len
) {
1303 buf
->size
+= len
+ SZ
;
1304 buf
->p
= realloc(buf
->p
, buf
->size
);
1306 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1310 static void check_for_gpl_usage(enum export exp
, const char *m
, const char *s
)
1312 const char *e
= is_vmlinux(m
) ?"":".ko";
1316 fatal("modpost: GPL-incompatible module %s%s "
1317 "uses GPL-only symbol '%s'\n", m
, e
, s
);
1319 case export_unused_gpl
:
1320 fatal("modpost: GPL-incompatible module %s%s "
1321 "uses GPL-only symbol marked UNUSED '%s'\n", m
, e
, s
);
1323 case export_gpl_future
:
1324 warn("modpost: GPL-incompatible module %s%s "
1325 "uses future GPL-only symbol '%s'\n", m
, e
, s
);
1329 case export_unknown
:
1335 static void check_for_unused(enum export exp
, const char* m
, const char* s
)
1337 const char *e
= is_vmlinux(m
) ?"":".ko";
1341 case export_unused_gpl
:
1342 warn("modpost: module %s%s "
1343 "uses symbol '%s' marked UNUSED\n", m
, e
, s
);
1351 static void check_exports(struct module
*mod
)
1353 struct symbol
*s
, *exp
;
1355 for (s
= mod
->unres
; s
; s
= s
->next
) {
1356 const char *basename
;
1357 exp
= find_symbol(s
->name
);
1358 if (!exp
|| exp
->module
== mod
)
1360 basename
= strrchr(mod
->name
, '/');
1364 basename
= mod
->name
;
1365 if (!mod
->gpl_compatible
)
1366 check_for_gpl_usage(exp
->export
, basename
, exp
->name
);
1367 check_for_unused(exp
->export
, basename
, exp
->name
);
1372 * Header for the generated file
1374 static void add_header(struct buffer
*b
, struct module
*mod
)
1376 buf_printf(b
, "#include <linux/module.h>\n");
1377 buf_printf(b
, "#include <linux/vermagic.h>\n");
1378 buf_printf(b
, "#include <linux/compiler.h>\n");
1379 buf_printf(b
, "\n");
1380 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1381 buf_printf(b
, "\n");
1382 buf_printf(b
, "struct module __this_module\n");
1383 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1384 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
1386 buf_printf(b
, " .init = init_module,\n");
1387 if (mod
->has_cleanup
)
1388 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1389 " .exit = cleanup_module,\n"
1391 buf_printf(b
, " .arch = MODULE_ARCH_INIT,\n");
1392 buf_printf(b
, "};\n");
1396 * Record CRCs for unresolved symbols
1398 static int add_versions(struct buffer
*b
, struct module
*mod
)
1400 struct symbol
*s
, *exp
;
1403 for (s
= mod
->unres
; s
; s
= s
->next
) {
1404 exp
= find_symbol(s
->name
);
1405 if (!exp
|| exp
->module
== mod
) {
1406 if (have_vmlinux
&& !s
->weak
) {
1407 if (warn_unresolved
) {
1408 warn("\"%s\" [%s.ko] undefined!\n",
1409 s
->name
, mod
->name
);
1411 merror("\"%s\" [%s.ko] undefined!\n",
1412 s
->name
, mod
->name
);
1418 s
->module
= exp
->module
;
1419 s
->crc_valid
= exp
->crc_valid
;
1426 buf_printf(b
, "\n");
1427 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1428 buf_printf(b
, "__attribute_used__\n");
1429 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
1431 for (s
= mod
->unres
; s
; s
= s
->next
) {
1435 if (!s
->crc_valid
) {
1436 warn("\"%s\" [%s.ko] has no CRC!\n",
1437 s
->name
, mod
->name
);
1440 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1443 buf_printf(b
, "};\n");
1448 static void add_depends(struct buffer
*b
, struct module
*mod
,
1449 struct module
*modules
)
1455 for (m
= modules
; m
; m
= m
->next
) {
1456 m
->seen
= is_vmlinux(m
->name
);
1459 buf_printf(b
, "\n");
1460 buf_printf(b
, "static const char __module_depends[]\n");
1461 buf_printf(b
, "__attribute_used__\n");
1462 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1463 buf_printf(b
, "\"depends=");
1464 for (s
= mod
->unres
; s
; s
= s
->next
) {
1469 if (s
->module
->seen
)
1472 s
->module
->seen
= 1;
1473 if ((p
= strrchr(s
->module
->name
, '/')) != NULL
)
1476 p
= s
->module
->name
;
1477 buf_printf(b
, "%s%s", first
? "" : ",", p
);
1480 buf_printf(b
, "\";\n");
1483 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1485 if (mod
->srcversion
[0]) {
1486 buf_printf(b
, "\n");
1487 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1492 static void write_if_changed(struct buffer
*b
, const char *fname
)
1498 file
= fopen(fname
, "r");
1502 if (fstat(fileno(file
), &st
) < 0)
1505 if (st
.st_size
!= b
->pos
)
1508 tmp
= NOFAIL(malloc(b
->pos
));
1509 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1512 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1524 file
= fopen(fname
, "w");
1529 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1536 /* parse Module.symvers file. line format:
1537 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1539 static void read_dump(const char *fname
, unsigned int kernel
)
1541 unsigned long size
, pos
= 0;
1542 void *file
= grab_file(fname
, &size
);
1546 /* No symbol versions, silently ignore */
1549 while ((line
= get_next_line(&pos
, file
, size
))) {
1550 char *symname
, *modname
, *d
, *export
, *end
;
1555 if (!(symname
= strchr(line
, '\t')))
1558 if (!(modname
= strchr(symname
, '\t')))
1561 if ((export
= strchr(modname
, '\t')) != NULL
)
1563 if (export
&& ((end
= strchr(export
, '\t')) != NULL
))
1565 crc
= strtoul(line
, &d
, 16);
1566 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1569 if (!(mod
= find_module(modname
))) {
1570 if (is_vmlinux(modname
)) {
1573 mod
= new_module(NOFAIL(strdup(modname
)));
1576 s
= sym_add_exported(symname
, mod
, export_no(export
));
1579 sym_update_crc(symname
, mod
, crc
, export_no(export
));
1583 fatal("parse error in symbol dump file\n");
1586 /* For normal builds always dump all symbols.
1587 * For external modules only dump symbols
1588 * that are not read from kernel Module.symvers.
1590 static int dump_sym(struct symbol
*sym
)
1592 if (!external_module
)
1594 if (sym
->vmlinux
|| sym
->kernel
)
1599 static void write_dump(const char *fname
)
1601 struct buffer buf
= { };
1602 struct symbol
*symbol
;
1605 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1606 symbol
= symbolhash
[n
];
1608 if (dump_sym(symbol
))
1609 buf_printf(&buf
, "0x%08x\t%s\t%s\t%s\n",
1610 symbol
->crc
, symbol
->name
,
1611 symbol
->module
->name
,
1612 export_str(symbol
->export
));
1613 symbol
= symbol
->next
;
1616 write_if_changed(&buf
, fname
);
1619 int main(int argc
, char **argv
)
1622 struct buffer buf
= { };
1624 char *kernel_read
= NULL
, *module_read
= NULL
;
1625 char *dump_write
= NULL
;
1629 while ((opt
= getopt(argc
, argv
, "i:I:mo:aw")) != -1) {
1632 kernel_read
= optarg
;
1635 module_read
= optarg
;
1636 external_module
= 1;
1642 dump_write
= optarg
;
1648 warn_unresolved
= 1;
1656 read_dump(kernel_read
, 1);
1658 read_dump(module_read
, 0);
1660 while (optind
< argc
) {
1661 read_symbols(argv
[optind
++]);
1664 for (mod
= modules
; mod
; mod
= mod
->next
) {
1672 for (mod
= modules
; mod
; mod
= mod
->next
) {
1678 add_header(&buf
, mod
);
1679 err
|= add_versions(&buf
, mod
);
1680 add_depends(&buf
, mod
, modules
);
1681 add_moddevtable(&buf
, mod
);
1682 add_srcversion(&buf
, mod
);
1684 sprintf(fname
, "%s.mod.c", mod
->name
);
1685 write_if_changed(&buf
, fname
);
1689 write_dump(dump_write
);