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 ...
17 /* Are we using CONFIG_MODVERSIONS? */
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
21 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
22 static int all_versions
= 0;
23 /* If we are modposting external module set to 1 */
24 static int external_module
= 0;
26 void fatal(const char *fmt
, ...)
30 fprintf(stderr
, "FATAL: ");
32 va_start(arglist
, fmt
);
33 vfprintf(stderr
, fmt
, arglist
);
39 void warn(const char *fmt
, ...)
43 fprintf(stderr
, "WARNING: ");
45 va_start(arglist
, fmt
);
46 vfprintf(stderr
, fmt
, arglist
);
50 static int is_vmlinux(const char *modname
)
54 if ((myname
= strrchr(modname
, '/')))
59 return strcmp(myname
, "vmlinux") == 0;
62 void *do_nofail(void *ptr
, const char *expr
)
65 fatal("modpost: Memory allocation failure: %s.\n", expr
);
70 /* A list of all modules we processed */
72 static struct module
*modules
;
74 static struct module
*find_module(char *modname
)
78 for (mod
= modules
; mod
; mod
= mod
->next
)
79 if (strcmp(mod
->name
, modname
) == 0)
84 static struct module
*new_module(char *modname
)
89 mod
= NOFAIL(malloc(sizeof(*mod
)));
90 memset(mod
, 0, sizeof(*mod
));
91 p
= NOFAIL(strdup(modname
));
93 /* strip trailing .o */
94 if ((s
= strrchr(p
, '.')) != NULL
)
95 if (strcmp(s
, ".o") == 0)
106 /* A hash of all exported symbols,
107 * struct symbol is also used for lists of unresolved symbols */
109 #define SYMBOL_HASH_SIZE 1024
113 struct module
*module
;
117 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
118 unsigned int kernel
:1; /* 1 if symbol is from kernel
119 * (only for external modules) **/
120 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
124 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
126 /* This is based on the hash agorithm from gdbm, via tdb */
127 static inline unsigned int tdb_hash(const char *name
)
129 unsigned value
; /* Used to compute the hash value. */
130 unsigned i
; /* Used to cycle through random values. */
132 /* Set the initial value from the key size. */
133 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
134 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
136 return (1103515243 * value
+ 12345);
140 * Allocate a new symbols for use in the hash of exported symbols or
141 * the list of unresolved symbols per module
143 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
146 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
148 memset(s
, 0, sizeof(*s
));
149 strcpy(s
->name
, name
);
155 /* For the hash of exported symbols */
156 static struct symbol
*new_symbol(const char *name
, struct module
*module
)
161 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
162 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
163 new->module
= module
;
167 static struct symbol
*find_symbol(const char *name
)
171 /* For our purposes, .foo matches foo. PPC64 needs this. */
175 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
176 if (strcmp(s
->name
, name
) == 0)
183 * Add an exported symbol - it may have already been added without a
184 * CRC, in this case just update the CRC
186 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
)
188 struct symbol
*s
= find_symbol(name
);
191 s
= new_symbol(name
, mod
);
194 warn("%s: '%s' exported twice. Previous export "
195 "was in %s%s\n", mod
->name
, name
,
197 is_vmlinux(s
->module
->name
) ?"":".ko");
201 s
->vmlinux
= is_vmlinux(mod
->name
);
206 static void sym_update_crc(const char *name
, struct module
*mod
,
209 struct symbol
*s
= find_symbol(name
);
212 s
= new_symbol(name
, mod
);
217 void *grab_file(const char *filename
, unsigned long *size
)
223 fd
= open(filename
, O_RDONLY
);
224 if (fd
< 0 || fstat(fd
, &st
) != 0)
228 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
231 if (map
== MAP_FAILED
)
237 * Return a copy of the next line in a mmap'ed file.
238 * spaces in the beginning of the line is trimmed away.
239 * Return a pointer to a static buffer.
241 char* get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
243 static char line
[4096];
246 signed char *p
= (signed char *)file
+ *pos
;
249 for (; *pos
< size
; (*pos
)++)
251 if (skip
&& isspace(*p
)) {
256 if (*p
!= '\n' && (*pos
< size
)) {
260 break; /* Too long, stop */
271 void release_file(void *file
, unsigned long size
)
276 static void parse_elf(struct elf_info
*info
, const char *filename
)
279 Elf_Ehdr
*hdr
= info
->hdr
;
283 hdr
= grab_file(filename
, &info
->size
);
289 if (info
->size
< sizeof(*hdr
))
292 /* Fix endianness in ELF header */
293 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
294 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
295 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
296 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
297 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
298 info
->sechdrs
= sechdrs
;
300 /* Fix endianness in section headers */
301 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
302 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
303 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
304 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
305 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
306 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
308 /* Find symbol table. */
309 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
310 const char *secstrings
311 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
313 if (sechdrs
[i
].sh_offset
> info
->size
)
315 if (strcmp(secstrings
+sechdrs
[i
].sh_name
, ".modinfo") == 0) {
316 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
317 info
->modinfo_len
= sechdrs
[i
].sh_size
;
319 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
322 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
323 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
324 + sechdrs
[i
].sh_size
;
325 info
->strtab
= (void *)hdr
+
326 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
328 if (!info
->symtab_start
) {
329 fatal("%s has no symtab?\n", filename
);
331 /* Fix endianness in symbols */
332 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
333 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
334 sym
->st_name
= TO_NATIVE(sym
->st_name
);
335 sym
->st_value
= TO_NATIVE(sym
->st_value
);
336 sym
->st_size
= TO_NATIVE(sym
->st_size
);
341 fatal("%s is truncated.\n", filename
);
344 static void parse_elf_finish(struct elf_info
*info
)
346 release_file(info
->hdr
, info
->size
);
349 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
350 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
352 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
353 Elf_Sym
*sym
, const char *symname
)
357 switch (sym
->st_shndx
) {
359 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
363 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
364 crc
= (unsigned int) sym
->st_value
;
365 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
);
369 /* undefined symbol */
370 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
371 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
373 /* ignore global offset table */
374 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
376 /* ignore __this_module, it will be resolved shortly */
377 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
379 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
380 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
381 /* add compatibility with older glibc */
382 #ifndef STT_SPARC_REGISTER
383 #define STT_SPARC_REGISTER STT_REGISTER
385 if (info
->hdr
->e_machine
== EM_SPARC
||
386 info
->hdr
->e_machine
== EM_SPARCV9
) {
387 /* Ignore register directives. */
388 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
390 if (symname
[0] == '.') {
391 char *munged
= strdup(symname
);
393 munged
[1] = toupper(munged
[1]);
399 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
400 strlen(MODULE_SYMBOL_PREFIX
)) == 0)
401 mod
->unres
= alloc_symbol(symname
+
402 strlen(MODULE_SYMBOL_PREFIX
),
403 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
407 /* All exported symbols */
408 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
409 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
);
411 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
413 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
414 mod
->has_cleanup
= 1;
420 * Parse tag=value strings from .modinfo section
422 static char *next_string(char *string
, unsigned long *secsize
)
424 /* Skip non-zero chars */
427 if ((*secsize
)-- <= 1)
431 /* Skip any zero padding. */
434 if ((*secsize
)-- <= 1)
440 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
444 unsigned int taglen
= strlen(tag
);
445 unsigned long size
= modinfo_len
;
447 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
448 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
449 return p
+ taglen
+ 1;
455 * Test if string s ends in string sub
458 static int strrcmp(const char *s
, const char *sub
)
466 sublen
= strlen(sub
);
468 if ((slen
== 0) || (sublen
== 0))
474 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
478 * Whitelist to allow certain references to pass with no warning.
480 * If a module parameter is declared __initdata and permissions=0
481 * then this is legal despite the warning generated.
482 * We cannot see value of permissions here, so just ignore
484 * The pattern is identified by:
490 * Many drivers utilise a *_driver container with references to
491 * add, remove, probe functions etc.
492 * These functions may often be marked __init and we do not want to
494 * the pattern is identified by:
495 * tosec = .init.text | .exit.text | .init.data
497 * atsym = *_driver, *_template, *_sht, *_ops, *_probe, *probe_one
499 static int secref_whitelist(const char *tosec
, const char *fromsec
,
504 const char *pat2sym
[] = {
506 "_template", /* scsi uses *_template a lot */
507 "_sht", /* scsi also used *_sht to some extent */
514 /* Check for pattern 1 */
515 if (strcmp(tosec
, ".init.data") != 0)
517 if (strncmp(fromsec
, ".data", strlen(".data")) != 0)
519 if (strncmp(atsym
, "__param", strlen("__param")) != 0)
525 /* Check for pattern 2 */
526 if ((strcmp(tosec
, ".init.text") != 0) &&
527 (strcmp(tosec
, ".exit.text") != 0) &&
528 (strcmp(tosec
, ".init.data") != 0))
530 if (strcmp(fromsec
, ".data") != 0)
533 for (s
= pat2sym
; *s
; s
++)
534 if (strrcmp(atsym
, *s
) == 0)
541 * Find symbol based on relocation record info.
542 * In some cases the symbol supplied is a valid symbol so
543 * return refsym. If st_name != 0 we assume this is a valid symbol.
544 * In other cases the symbol needs to be looked up in the symbol table
545 * based on section and address.
547 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf_Addr addr
,
552 if (relsym
->st_name
!= 0)
554 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
555 if (sym
->st_shndx
!= relsym
->st_shndx
)
557 if (sym
->st_value
== addr
)
564 * Find symbols before or equal addr and after addr - in the section sec.
565 * If we find two symbols with equal offset prefer one with a valid name.
566 * The ELF format may have a better way to detect what type of symbol
567 * it is, but this works for now.
569 static void find_symbols_between(struct elf_info
*elf
, Elf_Addr addr
,
571 Elf_Sym
**before
, Elf_Sym
**after
)
574 Elf_Ehdr
*hdr
= elf
->hdr
;
575 Elf_Addr beforediff
= ~0;
576 Elf_Addr afterdiff
= ~0;
577 const char *secstrings
= (void *)hdr
+
578 elf
->sechdrs
[hdr
->e_shstrndx
].sh_offset
;
583 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
586 if (sym
->st_shndx
>= SHN_LORESERVE
)
588 symsec
= secstrings
+ elf
->sechdrs
[sym
->st_shndx
].sh_name
;
589 if (strcmp(symsec
, sec
) != 0)
591 if (sym
->st_value
<= addr
) {
592 if ((addr
- sym
->st_value
) < beforediff
) {
593 beforediff
= addr
- sym
->st_value
;
596 else if ((addr
- sym
->st_value
) == beforediff
) {
597 /* equal offset, valid name? */
598 const char *name
= elf
->strtab
+ sym
->st_name
;
599 if (name
&& strlen(name
))
605 if ((sym
->st_value
- addr
) < afterdiff
) {
606 afterdiff
= sym
->st_value
- addr
;
609 else if ((sym
->st_value
- addr
) == afterdiff
) {
610 /* equal offset, valid name? */
611 const char *name
= elf
->strtab
+ sym
->st_name
;
612 if (name
&& strlen(name
))
620 * Print a warning about a section mismatch.
621 * Try to find symbols near it so user can find it.
622 * Check whitelist before warning - it may be a false positive.
624 static void warn_sec_mismatch(const char *modname
, const char *fromsec
,
625 struct elf_info
*elf
, Elf_Sym
*sym
, Elf_Rela r
)
627 const char *refsymname
= "";
628 Elf_Sym
*before
, *after
;
630 Elf_Ehdr
*hdr
= elf
->hdr
;
631 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
632 const char *secstrings
= (void *)hdr
+
633 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
634 const char *secname
= secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
;
636 find_symbols_between(elf
, r
.r_offset
, fromsec
, &before
, &after
);
638 refsym
= find_elf_symbol(elf
, r
.r_addend
, sym
);
639 if (refsym
&& strlen(elf
->strtab
+ refsym
->st_name
))
640 refsymname
= elf
->strtab
+ refsym
->st_name
;
642 /* check whitelist - we may ignore it */
644 secref_whitelist(secname
, fromsec
, elf
->strtab
+ before
->st_name
))
647 if (before
&& after
) {
648 warn("%s - Section mismatch: reference to %s:%s from %s "
649 "between '%s' (at offset 0x%llx) and '%s'\n",
650 modname
, secname
, refsymname
, fromsec
,
651 elf
->strtab
+ before
->st_name
,
652 (long long)r
.r_offset
,
653 elf
->strtab
+ after
->st_name
);
655 warn("%s - Section mismatch: reference to %s:%s from %s "
656 "after '%s' (at offset 0x%llx)\n",
657 modname
, secname
, refsymname
, fromsec
,
658 elf
->strtab
+ before
->st_name
,
659 (long long)r
.r_offset
);
661 warn("%s - Section mismatch: reference to %s:%s from %s "
662 "before '%s' (at offset -0x%llx)\n",
663 modname
, secname
, refsymname
, fromsec
,
664 elf
->strtab
+ after
->st_name
,
665 (long long)r
.r_offset
);
667 warn("%s - Section mismatch: reference to %s:%s from %s "
669 modname
, secname
, fromsec
, refsymname
,
670 (long long)r
.r_offset
);
675 * A module includes a number of sections that are discarded
676 * either when loaded or when used as built-in.
677 * For loaded modules all functions marked __init and all data
678 * marked __initdata will be discarded when the module has been intialized.
679 * Likewise for modules used built-in the sections marked __exit
680 * are discarded because __exit marked function are supposed to be called
681 * only when a moduel is unloaded which never happes for built-in modules.
682 * The check_sec_ref() function traverses all relocation records
683 * to find all references to a section that reference a section that will
684 * be discarded and warns about it.
686 static void check_sec_ref(struct module
*mod
, const char *modname
,
687 struct elf_info
*elf
,
688 int section(const char*),
689 int section_ref_ok(const char *))
693 Elf_Ehdr
*hdr
= elf
->hdr
;
694 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
695 const char *secstrings
= (void *)hdr
+
696 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
698 /* Walk through all sections */
699 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
701 Elf_Rela
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
702 Elf_Rela
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
703 const char *name
= secstrings
+ sechdrs
[i
].sh_name
+
705 /* We want to process only relocation sections and not .init */
706 if (section_ref_ok(name
) || (sechdrs
[i
].sh_type
!= SHT_RELA
))
709 for (rela
= start
; rela
< stop
; rela
++) {
712 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
713 r
.r_info
= TO_NATIVE(rela
->r_info
);
714 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
715 sym
= elf
->symtab_start
+ ELF_R_SYM(r
.r_info
);
716 /* Skip special sections */
717 if (sym
->st_shndx
>= SHN_LORESERVE
)
720 secname
= secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
;
721 if (section(secname
))
722 warn_sec_mismatch(modname
, name
, elf
, sym
, r
);
728 * Functions used only during module init is marked __init and is stored in
729 * a .init.text section. Likewise data is marked __initdata and stored in
730 * a .init.data section.
731 * If this section is one of these sections return 1
732 * See include/linux/init.h for the details
734 static int init_section(const char *name
)
736 if (strcmp(name
, ".init") == 0)
738 if (strncmp(name
, ".init.", strlen(".init.")) == 0)
744 * Identify sections from which references to a .init section is OK.
746 * Unfortunately references to read only data that referenced .init
747 * sections had to be excluded. Almost all of these are false
748 * positives, they are created by gcc. The downside of excluding rodata
749 * is that there really are some user references from rodata to
750 * init code, e.g. drivers/video/vgacon.c:
752 * const struct consw vga_con = {
753 * con_startup: vgacon_startup,
755 * where vgacon_startup is __init. If you want to wade through the false
756 * positives, take out the check for rodata.
758 static int init_section_ref_ok(const char *name
)
761 /* Absolute section names */
762 const char *namelist1
[] = {
764 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
765 ".toc1", /* used by ppc64 */
769 "__bug_table", /* used by powerpc for BUG() */
776 /* Start of section names */
777 const char *namelist2
[] = {
784 /* part of section name */
785 const char *namelist3
[] = {
786 ".unwind", /* sample: IA_64.unwind.init.text */
790 for (s
= namelist1
; *s
; s
++)
791 if (strcmp(*s
, name
) == 0)
793 for (s
= namelist2
; *s
; s
++)
794 if (strncmp(*s
, name
, strlen(*s
)) == 0)
796 for (s
= namelist3
; *s
; s
++)
797 if (strstr(name
, *s
) != NULL
)
803 * Functions used only during module exit is marked __exit and is stored in
804 * a .exit.text section. Likewise data is marked __exitdata and stored in
805 * a .exit.data section.
806 * If this section is one of these sections return 1
807 * See include/linux/init.h for the details
809 static int exit_section(const char *name
)
811 if (strcmp(name
, ".exit.text") == 0)
813 if (strcmp(name
, ".exit.data") == 0)
820 * Identify sections from which references to a .exit section is OK.
822 * [OPD] Keith Ownes <kaos@sgi.com> commented:
823 * For our future {in}sanity, add a comment that this is the ppc .opd
824 * section, not the ia64 .opd section.
825 * ia64 .opd should not point to discarded sections.
826 * [.rodata] like for .init.text we ignore .rodata references -same reason
828 static int exit_section_ref_ok(const char *name
)
831 /* Absolute section names */
832 const char *namelist1
[] = {
837 ".opd", /* See comment [OPD] */
838 ".toc1", /* used by ppc64 */
841 "__bug_table", /* used by powerpc for BUG() */
847 /* Start of section names */
848 const char *namelist2
[] = {
852 /* part of section name */
853 const char *namelist3
[] = {
854 ".unwind", /* Sample: IA_64.unwind.exit.text */
858 for (s
= namelist1
; *s
; s
++)
859 if (strcmp(*s
, name
) == 0)
861 for (s
= namelist2
; *s
; s
++)
862 if (strncmp(*s
, name
, strlen(*s
)) == 0)
864 for (s
= namelist3
; *s
; s
++)
865 if (strstr(name
, *s
) != NULL
)
870 static void read_symbols(char *modname
)
875 struct elf_info info
= { };
878 parse_elf(&info
, modname
);
880 mod
= new_module(modname
);
882 /* When there's no vmlinux, don't print warnings about
883 * unresolved symbols (since there'll be too many ;) */
884 if (is_vmlinux(modname
)) {
889 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
890 symname
= info
.strtab
+ sym
->st_name
;
892 handle_modversions(mod
, &info
, sym
, symname
);
893 handle_moddevtable(mod
, &info
, sym
, symname
);
895 check_sec_ref(mod
, modname
, &info
, init_section
, init_section_ref_ok
);
896 check_sec_ref(mod
, modname
, &info
, exit_section
, exit_section_ref_ok
);
898 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
900 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
901 version
- (char *)info
.hdr
);
902 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
903 get_src_version(modname
, mod
->srcversion
,
904 sizeof(mod
->srcversion
)-1);
906 parse_elf_finish(&info
);
908 /* Our trick to get versioning for struct_module - it's
909 * never passed as an argument to an exported function, so
910 * the automatic versioning doesn't pick it up, but it's really
911 * important anyhow */
913 mod
->unres
= alloc_symbol("struct_module", 0, mod
->unres
);
918 /* We first write the generated file into memory using the
919 * following helper, then compare to the file on disk and
920 * only update the later if anything changed */
922 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
923 const char *fmt
, ...)
930 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
931 buf_write(buf
, tmp
, len
);
935 void buf_write(struct buffer
*buf
, const char *s
, int len
)
937 if (buf
->size
- buf
->pos
< len
) {
938 buf
->size
+= len
+ SZ
;
939 buf
->p
= realloc(buf
->p
, buf
->size
);
941 strncpy(buf
->p
+ buf
->pos
, s
, len
);
946 * Header for the generated file
948 static void add_header(struct buffer
*b
, struct module
*mod
)
950 buf_printf(b
, "#include <linux/module.h>\n");
951 buf_printf(b
, "#include <linux/vermagic.h>\n");
952 buf_printf(b
, "#include <linux/compiler.h>\n");
954 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
956 buf_printf(b
, "struct module __this_module\n");
957 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
958 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
960 buf_printf(b
, " .init = init_module,\n");
961 if (mod
->has_cleanup
)
962 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
963 " .exit = cleanup_module,\n"
965 buf_printf(b
, "};\n");
969 * Record CRCs for unresolved symbols
971 static void add_versions(struct buffer
*b
, struct module
*mod
)
973 struct symbol
*s
, *exp
;
975 for (s
= mod
->unres
; s
; s
= s
->next
) {
976 exp
= find_symbol(s
->name
);
977 if (!exp
|| exp
->module
== mod
) {
978 if (have_vmlinux
&& !s
->weak
)
979 warn("\"%s\" [%s.ko] undefined!\n",
983 s
->module
= exp
->module
;
984 s
->crc_valid
= exp
->crc_valid
;
992 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
993 buf_printf(b
, "__attribute_used__\n");
994 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
996 for (s
= mod
->unres
; s
; s
= s
->next
) {
1000 if (!s
->crc_valid
) {
1001 warn("\"%s\" [%s.ko] has no CRC!\n",
1002 s
->name
, mod
->name
);
1005 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1008 buf_printf(b
, "};\n");
1011 static void add_depends(struct buffer
*b
, struct module
*mod
,
1012 struct module
*modules
)
1018 for (m
= modules
; m
; m
= m
->next
) {
1019 m
->seen
= is_vmlinux(m
->name
);
1022 buf_printf(b
, "\n");
1023 buf_printf(b
, "static const char __module_depends[]\n");
1024 buf_printf(b
, "__attribute_used__\n");
1025 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1026 buf_printf(b
, "\"depends=");
1027 for (s
= mod
->unres
; s
; s
= s
->next
) {
1031 if (s
->module
->seen
)
1034 s
->module
->seen
= 1;
1035 buf_printf(b
, "%s%s", first
? "" : ",",
1036 strrchr(s
->module
->name
, '/') + 1);
1039 buf_printf(b
, "\";\n");
1042 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1044 if (mod
->srcversion
[0]) {
1045 buf_printf(b
, "\n");
1046 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1051 static void write_if_changed(struct buffer
*b
, const char *fname
)
1057 file
= fopen(fname
, "r");
1061 if (fstat(fileno(file
), &st
) < 0)
1064 if (st
.st_size
!= b
->pos
)
1067 tmp
= NOFAIL(malloc(b
->pos
));
1068 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1071 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1083 file
= fopen(fname
, "w");
1088 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1095 static void read_dump(const char *fname
, unsigned int kernel
)
1097 unsigned long size
, pos
= 0;
1098 void *file
= grab_file(fname
, &size
);
1102 /* No symbol versions, silently ignore */
1105 while ((line
= get_next_line(&pos
, file
, size
))) {
1106 char *symname
, *modname
, *d
;
1111 if (!(symname
= strchr(line
, '\t')))
1114 if (!(modname
= strchr(symname
, '\t')))
1117 if (strchr(modname
, '\t'))
1119 crc
= strtoul(line
, &d
, 16);
1120 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1123 if (!(mod
= find_module(modname
))) {
1124 if (is_vmlinux(modname
)) {
1127 mod
= new_module(NOFAIL(strdup(modname
)));
1130 s
= sym_add_exported(symname
, mod
);
1133 sym_update_crc(symname
, mod
, crc
);
1137 fatal("parse error in symbol dump file\n");
1140 /* For normal builds always dump all symbols.
1141 * For external modules only dump symbols
1142 * that are not read from kernel Module.symvers.
1144 static int dump_sym(struct symbol
*sym
)
1146 if (!external_module
)
1148 if (sym
->vmlinux
|| sym
->kernel
)
1153 static void write_dump(const char *fname
)
1155 struct buffer buf
= { };
1156 struct symbol
*symbol
;
1159 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1160 symbol
= symbolhash
[n
];
1162 if (dump_sym(symbol
))
1163 buf_printf(&buf
, "0x%08x\t%s\t%s\n",
1164 symbol
->crc
, symbol
->name
,
1165 symbol
->module
->name
);
1166 symbol
= symbol
->next
;
1169 write_if_changed(&buf
, fname
);
1172 int main(int argc
, char **argv
)
1175 struct buffer buf
= { };
1177 char *kernel_read
= NULL
, *module_read
= NULL
;
1178 char *dump_write
= NULL
;
1181 while ((opt
= getopt(argc
, argv
, "i:I:mo:a")) != -1) {
1184 kernel_read
= optarg
;
1187 module_read
= optarg
;
1188 external_module
= 1;
1194 dump_write
= optarg
;
1205 read_dump(kernel_read
, 1);
1207 read_dump(module_read
, 0);
1209 while (optind
< argc
) {
1210 read_symbols(argv
[optind
++]);
1213 for (mod
= modules
; mod
; mod
= mod
->next
) {
1219 add_header(&buf
, mod
);
1220 add_versions(&buf
, mod
);
1221 add_depends(&buf
, mod
, modules
);
1222 add_moddevtable(&buf
, mod
);
1223 add_srcversion(&buf
, mod
);
1225 sprintf(fname
, "%s.mod.c", mod
->name
);
1226 write_if_changed(&buf
, fname
);
1230 write_dump(dump_write
);