]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - scripts/mod/modpost.c
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 /* How a symbol is exported */
27 enum export
{export_plain
, export_gpl
, export_gpl_future
, export_unknown
};
29 void fatal(const char *fmt
, ...)
33 fprintf(stderr
, "FATAL: ");
35 va_start(arglist
, fmt
);
36 vfprintf(stderr
, fmt
, arglist
);
42 void warn(const char *fmt
, ...)
46 fprintf(stderr
, "WARNING: ");
48 va_start(arglist
, fmt
);
49 vfprintf(stderr
, fmt
, arglist
);
53 static int is_vmlinux(const char *modname
)
57 if ((myname
= strrchr(modname
, '/')))
62 return strcmp(myname
, "vmlinux") == 0;
65 void *do_nofail(void *ptr
, const char *expr
)
68 fatal("modpost: Memory allocation failure: %s.\n", expr
);
73 /* A list of all modules we processed */
75 static struct module
*modules
;
77 static struct module
*find_module(char *modname
)
81 for (mod
= modules
; mod
; mod
= mod
->next
)
82 if (strcmp(mod
->name
, modname
) == 0)
87 static struct module
*new_module(char *modname
)
92 mod
= NOFAIL(malloc(sizeof(*mod
)));
93 memset(mod
, 0, sizeof(*mod
));
94 p
= NOFAIL(strdup(modname
));
96 /* strip trailing .o */
97 if ((s
= strrchr(p
, '.')) != NULL
)
98 if (strcmp(s
, ".o") == 0)
103 mod
->gpl_compatible
= -1;
110 /* A hash of all exported symbols,
111 * struct symbol is also used for lists of unresolved symbols */
113 #define SYMBOL_HASH_SIZE 1024
117 struct module
*module
;
121 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
122 unsigned int kernel
:1; /* 1 if symbol is from kernel
123 * (only for external modules) **/
124 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
125 enum export export
; /* Type of export */
129 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
131 /* This is based on the hash agorithm from gdbm, via tdb */
132 static inline unsigned int tdb_hash(const char *name
)
134 unsigned value
; /* Used to compute the hash value. */
135 unsigned i
; /* Used to cycle through random values. */
137 /* Set the initial value from the key size. */
138 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
139 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
141 return (1103515243 * value
+ 12345);
145 * Allocate a new symbols for use in the hash of exported symbols or
146 * the list of unresolved symbols per module
148 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
151 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
153 memset(s
, 0, sizeof(*s
));
154 strcpy(s
->name
, name
);
160 /* For the hash of exported symbols */
161 static struct symbol
*new_symbol(const char *name
, struct module
*module
,
167 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
168 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
169 new->module
= module
;
170 new->export
= export
;
174 static struct symbol
*find_symbol(const char *name
)
178 /* For our purposes, .foo matches foo. PPC64 needs this. */
182 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
=s
->next
) {
183 if (strcmp(s
->name
, name
) == 0)
193 { .str
= "EXPORT_SYMBOL", .export
= export_plain
},
194 { .str
= "EXPORT_SYMBOL_GPL", .export
= export_gpl
},
195 { .str
= "EXPORT_SYMBOL_GPL_FUTURE", .export
= export_gpl_future
},
196 { .str
= "(unknown)", .export
= export_unknown
},
200 static const char *export_str(enum export ex
)
202 return export_list
[ex
].str
;
205 static enum export
export_no(const char * s
)
208 for (i
= 0; export_list
[i
].export
!= export_unknown
; i
++) {
209 if (strcmp(export_list
[i
].str
, s
) == 0)
210 return export_list
[i
].export
;
212 return export_unknown
;
215 static enum export
export_from_sec(struct elf_info
*elf
, Elf_Section sec
)
217 if (sec
== elf
->export_sec
)
219 else if (sec
== elf
->export_gpl_sec
)
221 else if (sec
== elf
->export_gpl_future_sec
)
222 return export_gpl_future
;
224 return export_unknown
;
228 * Add an exported symbol - it may have already been added without a
229 * CRC, in this case just update the CRC
231 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
234 struct symbol
*s
= find_symbol(name
);
237 s
= new_symbol(name
, mod
, export
);
240 warn("%s: '%s' exported twice. Previous export "
241 "was in %s%s\n", mod
->name
, name
,
243 is_vmlinux(s
->module
->name
) ?"":".ko");
247 s
->vmlinux
= is_vmlinux(mod
->name
);
253 static void sym_update_crc(const char *name
, struct module
*mod
,
254 unsigned int crc
, enum export export
)
256 struct symbol
*s
= find_symbol(name
);
259 s
= new_symbol(name
, mod
, export
);
264 void *grab_file(const char *filename
, unsigned long *size
)
270 fd
= open(filename
, O_RDONLY
);
271 if (fd
< 0 || fstat(fd
, &st
) != 0)
275 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
278 if (map
== MAP_FAILED
)
284 * Return a copy of the next line in a mmap'ed file.
285 * spaces in the beginning of the line is trimmed away.
286 * Return a pointer to a static buffer.
288 char* get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
290 static char line
[4096];
293 signed char *p
= (signed char *)file
+ *pos
;
296 for (; *pos
< size
; (*pos
)++)
298 if (skip
&& isspace(*p
)) {
303 if (*p
!= '\n' && (*pos
< size
)) {
307 break; /* Too long, stop */
318 void release_file(void *file
, unsigned long size
)
323 static void parse_elf(struct elf_info
*info
, const char *filename
)
326 Elf_Ehdr
*hdr
= info
->hdr
;
330 hdr
= grab_file(filename
, &info
->size
);
336 if (info
->size
< sizeof(*hdr
))
339 /* Fix endianness in ELF header */
340 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
341 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
342 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
343 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
344 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
345 info
->sechdrs
= sechdrs
;
347 /* Fix endianness in section headers */
348 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
349 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
350 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
351 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
352 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
353 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
355 /* Find symbol table. */
356 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
357 const char *secstrings
358 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
361 if (sechdrs
[i
].sh_offset
> info
->size
)
363 secname
= secstrings
+ sechdrs
[i
].sh_name
;
364 if (strcmp(secname
, ".modinfo") == 0) {
365 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
366 info
->modinfo_len
= sechdrs
[i
].sh_size
;
367 } else if (strcmp(secname
, "__ksymtab") == 0)
368 info
->export_sec
= i
;
369 else if (strcmp(secname
, "__ksymtab_gpl") == 0)
370 info
->export_gpl_sec
= i
;
371 else if (strcmp(secname
, "__ksymtab_gpl_future") == 0)
372 info
->export_gpl_future_sec
= i
;
374 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
377 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
378 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
379 + sechdrs
[i
].sh_size
;
380 info
->strtab
= (void *)hdr
+
381 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
383 if (!info
->symtab_start
) {
384 fatal("%s has no symtab?\n", filename
);
386 /* Fix endianness in symbols */
387 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
388 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
389 sym
->st_name
= TO_NATIVE(sym
->st_name
);
390 sym
->st_value
= TO_NATIVE(sym
->st_value
);
391 sym
->st_size
= TO_NATIVE(sym
->st_size
);
396 fatal("%s is truncated.\n", filename
);
399 static void parse_elf_finish(struct elf_info
*info
)
401 release_file(info
->hdr
, info
->size
);
404 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
405 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
407 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
408 Elf_Sym
*sym
, const char *symname
)
411 enum export export
= export_from_sec(info
, sym
->st_shndx
);
413 switch (sym
->st_shndx
) {
415 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
419 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
420 crc
= (unsigned int) sym
->st_value
;
421 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
,
426 /* undefined symbol */
427 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
428 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
430 /* ignore global offset table */
431 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
433 /* ignore __this_module, it will be resolved shortly */
434 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
436 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
437 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
438 /* add compatibility with older glibc */
439 #ifndef STT_SPARC_REGISTER
440 #define STT_SPARC_REGISTER STT_REGISTER
442 if (info
->hdr
->e_machine
== EM_SPARC
||
443 info
->hdr
->e_machine
== EM_SPARCV9
) {
444 /* Ignore register directives. */
445 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
447 if (symname
[0] == '.') {
448 char *munged
= strdup(symname
);
450 munged
[1] = toupper(munged
[1]);
456 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
457 strlen(MODULE_SYMBOL_PREFIX
)) == 0)
458 mod
->unres
= alloc_symbol(symname
+
459 strlen(MODULE_SYMBOL_PREFIX
),
460 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
464 /* All exported symbols */
465 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
466 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
,
469 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
471 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
472 mod
->has_cleanup
= 1;
478 * Parse tag=value strings from .modinfo section
480 static char *next_string(char *string
, unsigned long *secsize
)
482 /* Skip non-zero chars */
485 if ((*secsize
)-- <= 1)
489 /* Skip any zero padding. */
492 if ((*secsize
)-- <= 1)
498 static char *get_next_modinfo(void *modinfo
, unsigned long modinfo_len
,
499 const char *tag
, char *info
)
502 unsigned int taglen
= strlen(tag
);
503 unsigned long size
= modinfo_len
;
506 size
-= info
- (char *)modinfo
;
507 modinfo
= next_string(info
, &size
);
510 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
511 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
512 return p
+ taglen
+ 1;
517 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
521 return get_next_modinfo(modinfo
, modinfo_len
, tag
, NULL
);
525 * Test if string s ends in string sub
528 static int strrcmp(const char *s
, const char *sub
)
536 sublen
= strlen(sub
);
538 if ((slen
== 0) || (sublen
== 0))
544 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
548 * Whitelist to allow certain references to pass with no warning.
550 * If a module parameter is declared __initdata and permissions=0
551 * then this is legal despite the warning generated.
552 * We cannot see value of permissions here, so just ignore
554 * The pattern is identified by:
560 * Many drivers utilise a *driver container with references to
561 * add, remove, probe functions etc.
562 * These functions may often be marked __init and we do not want to
564 * the pattern is identified by:
565 * tosec = .init.text | .exit.text | .init.data
567 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
569 static int secref_whitelist(const char *tosec
, const char *fromsec
,
574 const char *pat2sym
[] = {
576 "_template", /* scsi uses *_template a lot */
577 "_sht", /* scsi also used *_sht to some extent */
584 /* Check for pattern 1 */
585 if (strcmp(tosec
, ".init.data") != 0)
587 if (strncmp(fromsec
, ".data", strlen(".data")) != 0)
589 if (strncmp(atsym
, "__param", strlen("__param")) != 0)
595 /* Check for pattern 2 */
596 if ((strcmp(tosec
, ".init.text") != 0) &&
597 (strcmp(tosec
, ".exit.text") != 0) &&
598 (strcmp(tosec
, ".init.data") != 0))
600 if (strcmp(fromsec
, ".data") != 0)
603 for (s
= pat2sym
; *s
; s
++)
604 if (strrcmp(atsym
, *s
) == 0)
611 * Find symbol based on relocation record info.
612 * In some cases the symbol supplied is a valid symbol so
613 * return refsym. If st_name != 0 we assume this is a valid symbol.
614 * In other cases the symbol needs to be looked up in the symbol table
615 * based on section and address.
617 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf_Addr addr
,
622 if (relsym
->st_name
!= 0)
624 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
625 if (sym
->st_shndx
!= relsym
->st_shndx
)
627 if (sym
->st_value
== addr
)
634 * Find symbols before or equal addr and after addr - in the section sec.
635 * If we find two symbols with equal offset prefer one with a valid name.
636 * The ELF format may have a better way to detect what type of symbol
637 * it is, but this works for now.
639 static void find_symbols_between(struct elf_info
*elf
, Elf_Addr addr
,
641 Elf_Sym
**before
, Elf_Sym
**after
)
644 Elf_Ehdr
*hdr
= elf
->hdr
;
645 Elf_Addr beforediff
= ~0;
646 Elf_Addr afterdiff
= ~0;
647 const char *secstrings
= (void *)hdr
+
648 elf
->sechdrs
[hdr
->e_shstrndx
].sh_offset
;
653 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
656 if (sym
->st_shndx
>= SHN_LORESERVE
)
658 symsec
= secstrings
+ elf
->sechdrs
[sym
->st_shndx
].sh_name
;
659 if (strcmp(symsec
, sec
) != 0)
661 if (sym
->st_value
<= addr
) {
662 if ((addr
- sym
->st_value
) < beforediff
) {
663 beforediff
= addr
- sym
->st_value
;
666 else if ((addr
- sym
->st_value
) == beforediff
) {
667 /* equal offset, valid name? */
668 const char *name
= elf
->strtab
+ sym
->st_name
;
669 if (name
&& strlen(name
))
675 if ((sym
->st_value
- addr
) < afterdiff
) {
676 afterdiff
= sym
->st_value
- addr
;
679 else if ((sym
->st_value
- addr
) == afterdiff
) {
680 /* equal offset, valid name? */
681 const char *name
= elf
->strtab
+ sym
->st_name
;
682 if (name
&& strlen(name
))
690 * Print a warning about a section mismatch.
691 * Try to find symbols near it so user can find it.
692 * Check whitelist before warning - it may be a false positive.
694 static void warn_sec_mismatch(const char *modname
, const char *fromsec
,
695 struct elf_info
*elf
, Elf_Sym
*sym
, Elf_Rela r
)
697 const char *refsymname
= "";
698 Elf_Sym
*before
, *after
;
700 Elf_Ehdr
*hdr
= elf
->hdr
;
701 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
702 const char *secstrings
= (void *)hdr
+
703 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
704 const char *secname
= secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
;
706 find_symbols_between(elf
, r
.r_offset
, fromsec
, &before
, &after
);
708 refsym
= find_elf_symbol(elf
, r
.r_addend
, sym
);
709 if (refsym
&& strlen(elf
->strtab
+ refsym
->st_name
))
710 refsymname
= elf
->strtab
+ refsym
->st_name
;
712 /* check whitelist - we may ignore it */
714 secref_whitelist(secname
, fromsec
, elf
->strtab
+ before
->st_name
))
717 if (before
&& after
) {
718 warn("%s - Section mismatch: reference to %s:%s from %s "
719 "between '%s' (at offset 0x%llx) and '%s'\n",
720 modname
, secname
, refsymname
, fromsec
,
721 elf
->strtab
+ before
->st_name
,
722 (long long)r
.r_offset
,
723 elf
->strtab
+ after
->st_name
);
725 warn("%s - Section mismatch: reference to %s:%s from %s "
726 "after '%s' (at offset 0x%llx)\n",
727 modname
, secname
, refsymname
, fromsec
,
728 elf
->strtab
+ before
->st_name
,
729 (long long)r
.r_offset
);
731 warn("%s - Section mismatch: reference to %s:%s from %s "
732 "before '%s' (at offset -0x%llx)\n",
733 modname
, secname
, refsymname
, fromsec
,
734 elf
->strtab
+ after
->st_name
,
735 (long long)r
.r_offset
);
737 warn("%s - Section mismatch: reference to %s:%s from %s "
739 modname
, secname
, fromsec
, refsymname
,
740 (long long)r
.r_offset
);
745 * A module includes a number of sections that are discarded
746 * either when loaded or when used as built-in.
747 * For loaded modules all functions marked __init and all data
748 * marked __initdata will be discarded when the module has been intialized.
749 * Likewise for modules used built-in the sections marked __exit
750 * are discarded because __exit marked function are supposed to be called
751 * only when a moduel is unloaded which never happes for built-in modules.
752 * The check_sec_ref() function traverses all relocation records
753 * to find all references to a section that reference a section that will
754 * be discarded and warns about it.
756 static void check_sec_ref(struct module
*mod
, const char *modname
,
757 struct elf_info
*elf
,
758 int section(const char*),
759 int section_ref_ok(const char *))
763 Elf_Ehdr
*hdr
= elf
->hdr
;
764 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
765 const char *secstrings
= (void *)hdr
+
766 sechdrs
[hdr
->e_shstrndx
].sh_offset
;
768 /* Walk through all sections */
769 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
770 const char *name
= secstrings
+ sechdrs
[i
].sh_name
;
774 /* We want to process only relocation sections and not .init */
775 if (sechdrs
[i
].sh_type
== SHT_RELA
) {
777 Elf_Rela
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
778 Elf_Rela
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
779 name
+= strlen(".rela");
780 if (section_ref_ok(name
))
783 for (rela
= start
; rela
< stop
; rela
++) {
784 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
785 #if KERNEL_ELFCLASS == ELFCLASS64
786 if (hdr
->e_machine
== EM_MIPS
) {
787 r_sym
= ELF64_MIPS_R_SYM(rela
->r_info
);
788 r_sym
= TO_NATIVE(r_sym
);
790 r
.r_info
= TO_NATIVE(rela
->r_info
);
791 r_sym
= ELF_R_SYM(r
.r_info
);
794 r
.r_info
= TO_NATIVE(rela
->r_info
);
795 r_sym
= ELF_R_SYM(r
.r_info
);
797 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
798 sym
= elf
->symtab_start
+ r_sym
;
799 /* Skip special sections */
800 if (sym
->st_shndx
>= SHN_LORESERVE
)
803 secname
= secstrings
+
804 sechdrs
[sym
->st_shndx
].sh_name
;
805 if (section(secname
))
806 warn_sec_mismatch(modname
, name
,
809 } else if (sechdrs
[i
].sh_type
== SHT_REL
) {
811 Elf_Rel
*start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
812 Elf_Rel
*stop
= (void*)start
+ sechdrs
[i
].sh_size
;
813 name
+= strlen(".rel");
814 if (section_ref_ok(name
))
817 for (rel
= start
; rel
< stop
; rel
++) {
818 r
.r_offset
= TO_NATIVE(rel
->r_offset
);
819 #if KERNEL_ELFCLASS == ELFCLASS64
820 if (hdr
->e_machine
== EM_MIPS
) {
821 r_sym
= ELF64_MIPS_R_SYM(rel
->r_info
);
822 r_sym
= TO_NATIVE(r_sym
);
824 r
.r_info
= TO_NATIVE(rel
->r_info
);
825 r_sym
= ELF_R_SYM(r
.r_info
);
828 r
.r_info
= TO_NATIVE(rel
->r_info
);
829 r_sym
= ELF_R_SYM(r
.r_info
);
832 sym
= elf
->symtab_start
+ r_sym
;
833 /* Skip special sections */
834 if (sym
->st_shndx
>= SHN_LORESERVE
)
837 secname
= secstrings
+
838 sechdrs
[sym
->st_shndx
].sh_name
;
839 if (section(secname
))
840 warn_sec_mismatch(modname
, name
,
848 * Functions used only during module init is marked __init and is stored in
849 * a .init.text section. Likewise data is marked __initdata and stored in
850 * a .init.data section.
851 * If this section is one of these sections return 1
852 * See include/linux/init.h for the details
854 static int init_section(const char *name
)
856 if (strcmp(name
, ".init") == 0)
858 if (strncmp(name
, ".init.", strlen(".init.")) == 0)
864 * Identify sections from which references to a .init section is OK.
866 * Unfortunately references to read only data that referenced .init
867 * sections had to be excluded. Almost all of these are false
868 * positives, they are created by gcc. The downside of excluding rodata
869 * is that there really are some user references from rodata to
870 * init code, e.g. drivers/video/vgacon.c:
872 * const struct consw vga_con = {
873 * con_startup: vgacon_startup,
875 * where vgacon_startup is __init. If you want to wade through the false
876 * positives, take out the check for rodata.
878 static int init_section_ref_ok(const char *name
)
881 /* Absolute section names */
882 const char *namelist1
[] = {
884 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
885 ".toc1", /* used by ppc64 */
889 "__bug_table", /* used by powerpc for BUG() */
895 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
898 /* Start of section names */
899 const char *namelist2
[] = {
906 /* part of section name */
907 const char *namelist3
[] = {
908 ".unwind", /* sample: IA_64.unwind.init.text */
912 for (s
= namelist1
; *s
; s
++)
913 if (strcmp(*s
, name
) == 0)
915 for (s
= namelist2
; *s
; s
++)
916 if (strncmp(*s
, name
, strlen(*s
)) == 0)
918 for (s
= namelist3
; *s
; s
++)
919 if (strstr(name
, *s
) != NULL
)
925 * Functions used only during module exit is marked __exit and is stored in
926 * a .exit.text section. Likewise data is marked __exitdata and stored in
927 * a .exit.data section.
928 * If this section is one of these sections return 1
929 * See include/linux/init.h for the details
931 static int exit_section(const char *name
)
933 if (strcmp(name
, ".exit.text") == 0)
935 if (strcmp(name
, ".exit.data") == 0)
942 * Identify sections from which references to a .exit section is OK.
944 * [OPD] Keith Ownes <kaos@sgi.com> commented:
945 * For our future {in}sanity, add a comment that this is the ppc .opd
946 * section, not the ia64 .opd section.
947 * ia64 .opd should not point to discarded sections.
948 * [.rodata] like for .init.text we ignore .rodata references -same reason
950 static int exit_section_ref_ok(const char *name
)
953 /* Absolute section names */
954 const char *namelist1
[] = {
959 ".opd", /* See comment [OPD] */
960 ".toc1", /* used by ppc64 */
963 "__bug_table", /* used by powerpc for BUG() */
968 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
971 /* Start of section names */
972 const char *namelist2
[] = {
976 /* part of section name */
977 const char *namelist3
[] = {
978 ".unwind", /* Sample: IA_64.unwind.exit.text */
982 for (s
= namelist1
; *s
; s
++)
983 if (strcmp(*s
, name
) == 0)
985 for (s
= namelist2
; *s
; s
++)
986 if (strncmp(*s
, name
, strlen(*s
)) == 0)
988 for (s
= namelist3
; *s
; s
++)
989 if (strstr(name
, *s
) != NULL
)
994 static void read_symbols(char *modname
)
1000 struct elf_info info
= { };
1003 parse_elf(&info
, modname
);
1005 mod
= new_module(modname
);
1007 /* When there's no vmlinux, don't print warnings about
1008 * unresolved symbols (since there'll be too many ;) */
1009 if (is_vmlinux(modname
)) {
1014 license
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "license");
1016 if (license_is_gpl_compatible(license
))
1017 mod
->gpl_compatible
= 1;
1019 mod
->gpl_compatible
= 0;
1022 license
= get_next_modinfo(info
.modinfo
, info
.modinfo_len
,
1023 "license", license
);
1026 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1027 symname
= info
.strtab
+ sym
->st_name
;
1029 handle_modversions(mod
, &info
, sym
, symname
);
1030 handle_moddevtable(mod
, &info
, sym
, symname
);
1032 check_sec_ref(mod
, modname
, &info
, init_section
, init_section_ref_ok
);
1033 check_sec_ref(mod
, modname
, &info
, exit_section
, exit_section_ref_ok
);
1035 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
1037 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
1038 version
- (char *)info
.hdr
);
1039 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
1040 get_src_version(modname
, mod
->srcversion
,
1041 sizeof(mod
->srcversion
)-1);
1043 parse_elf_finish(&info
);
1045 /* Our trick to get versioning for struct_module - it's
1046 * never passed as an argument to an exported function, so
1047 * the automatic versioning doesn't pick it up, but it's really
1048 * important anyhow */
1050 mod
->unres
= alloc_symbol("struct_module", 0, mod
->unres
);
1055 /* We first write the generated file into memory using the
1056 * following helper, then compare to the file on disk and
1057 * only update the later if anything changed */
1059 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1060 const char *fmt
, ...)
1067 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1068 buf_write(buf
, tmp
, len
);
1072 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1074 if (buf
->size
- buf
->pos
< len
) {
1075 buf
->size
+= len
+ SZ
;
1076 buf
->p
= realloc(buf
->p
, buf
->size
);
1078 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1082 void check_license(struct module
*mod
)
1084 struct symbol
*s
, *exp
;
1086 for (s
= mod
->unres
; s
; s
= s
->next
) {
1087 if (mod
->gpl_compatible
== 1) {
1088 /* GPL-compatible modules may use all symbols */
1091 exp
= find_symbol(s
->name
);
1092 if (!exp
|| exp
->module
== mod
)
1094 const char *basename
= strrchr(mod
->name
, '/');
1097 switch (exp
->export
) {
1099 fatal("modpost: GPL-incompatible module %s "
1100 "uses GPL-only symbol '%s'\n",
1101 basename
? basename
: mod
->name
,
1104 case export_gpl_future
:
1105 warn("modpost: GPL-incompatible module %s "
1106 "uses future GPL-only symbol '%s'\n",
1107 basename
? basename
: mod
->name
,
1110 case export_plain
: /* ignore */ break;
1111 case export_unknown
: /* ignore */ break;
1117 * Header for the generated file
1119 static void add_header(struct buffer
*b
, struct module
*mod
)
1121 buf_printf(b
, "#include <linux/module.h>\n");
1122 buf_printf(b
, "#include <linux/vermagic.h>\n");
1123 buf_printf(b
, "#include <linux/compiler.h>\n");
1124 buf_printf(b
, "\n");
1125 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1126 buf_printf(b
, "\n");
1127 buf_printf(b
, "struct module __this_module\n");
1128 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1129 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
1131 buf_printf(b
, " .init = init_module,\n");
1132 if (mod
->has_cleanup
)
1133 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1134 " .exit = cleanup_module,\n"
1136 buf_printf(b
, "};\n");
1140 * Record CRCs for unresolved symbols
1142 static void add_versions(struct buffer
*b
, struct module
*mod
)
1144 struct symbol
*s
, *exp
;
1146 for (s
= mod
->unres
; s
; s
= s
->next
) {
1147 exp
= find_symbol(s
->name
);
1148 if (!exp
|| exp
->module
== mod
) {
1149 if (have_vmlinux
&& !s
->weak
)
1150 warn("\"%s\" [%s.ko] undefined!\n",
1151 s
->name
, mod
->name
);
1154 s
->module
= exp
->module
;
1155 s
->crc_valid
= exp
->crc_valid
;
1162 buf_printf(b
, "\n");
1163 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1164 buf_printf(b
, "__attribute_used__\n");
1165 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
1167 for (s
= mod
->unres
; s
; s
= s
->next
) {
1171 if (!s
->crc_valid
) {
1172 warn("\"%s\" [%s.ko] has no CRC!\n",
1173 s
->name
, mod
->name
);
1176 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1179 buf_printf(b
, "};\n");
1182 static void add_depends(struct buffer
*b
, struct module
*mod
,
1183 struct module
*modules
)
1189 for (m
= modules
; m
; m
= m
->next
) {
1190 m
->seen
= is_vmlinux(m
->name
);
1193 buf_printf(b
, "\n");
1194 buf_printf(b
, "static const char __module_depends[]\n");
1195 buf_printf(b
, "__attribute_used__\n");
1196 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1197 buf_printf(b
, "\"depends=");
1198 for (s
= mod
->unres
; s
; s
= s
->next
) {
1202 if (s
->module
->seen
)
1205 s
->module
->seen
= 1;
1206 buf_printf(b
, "%s%s", first
? "" : ",",
1207 strrchr(s
->module
->name
, '/') + 1);
1210 buf_printf(b
, "\";\n");
1213 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1215 if (mod
->srcversion
[0]) {
1216 buf_printf(b
, "\n");
1217 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1222 static void write_if_changed(struct buffer
*b
, const char *fname
)
1228 file
= fopen(fname
, "r");
1232 if (fstat(fileno(file
), &st
) < 0)
1235 if (st
.st_size
!= b
->pos
)
1238 tmp
= NOFAIL(malloc(b
->pos
));
1239 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1242 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1254 file
= fopen(fname
, "w");
1259 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1266 /* parse Module.symvers file. line format:
1267 * 0x12345678<tab>symbol<tab>module[<tab>export]
1269 static void read_dump(const char *fname
, unsigned int kernel
)
1271 unsigned long size
, pos
= 0;
1272 void *file
= grab_file(fname
, &size
);
1276 /* No symbol versions, silently ignore */
1279 while ((line
= get_next_line(&pos
, file
, size
))) {
1280 char *symname
, *modname
, *d
, *export
;
1285 if (!(symname
= strchr(line
, '\t')))
1288 if (!(modname
= strchr(symname
, '\t')))
1291 if (!(export
= strchr(modname
, '\t')))
1294 crc
= strtoul(line
, &d
, 16);
1295 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1298 if (!(mod
= find_module(modname
))) {
1299 if (is_vmlinux(modname
)) {
1302 mod
= new_module(NOFAIL(strdup(modname
)));
1305 s
= sym_add_exported(symname
, mod
, export_no(export
));
1308 sym_update_crc(symname
, mod
, crc
, export_no(export
));
1312 fatal("parse error in symbol dump file\n");
1315 /* For normal builds always dump all symbols.
1316 * For external modules only dump symbols
1317 * that are not read from kernel Module.symvers.
1319 static int dump_sym(struct symbol
*sym
)
1321 if (!external_module
)
1323 if (sym
->vmlinux
|| sym
->kernel
)
1328 static void write_dump(const char *fname
)
1330 struct buffer buf
= { };
1331 struct symbol
*symbol
;
1334 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1335 symbol
= symbolhash
[n
];
1337 if (dump_sym(symbol
))
1338 buf_printf(&buf
, "0x%08x\t%s\t%s\t%s\n",
1339 symbol
->crc
, symbol
->name
,
1340 symbol
->module
->name
,
1341 export_str(symbol
->export
));
1342 symbol
= symbol
->next
;
1345 write_if_changed(&buf
, fname
);
1348 int main(int argc
, char **argv
)
1351 struct buffer buf
= { };
1353 char *kernel_read
= NULL
, *module_read
= NULL
;
1354 char *dump_write
= NULL
;
1357 while ((opt
= getopt(argc
, argv
, "i:I:mo:a")) != -1) {
1360 kernel_read
= optarg
;
1363 module_read
= optarg
;
1364 external_module
= 1;
1370 dump_write
= optarg
;
1381 read_dump(kernel_read
, 1);
1383 read_dump(module_read
, 0);
1385 while (optind
< argc
) {
1386 read_symbols(argv
[optind
++]);
1389 for (mod
= modules
; mod
; mod
= mod
->next
) {
1395 for (mod
= modules
; mod
; mod
= mod
->next
) {
1401 add_header(&buf
, mod
);
1402 add_versions(&buf
, mod
);
1403 add_depends(&buf
, mod
, modules
);
1404 add_moddevtable(&buf
, mod
);
1405 add_srcversion(&buf
, mod
);
1407 sprintf(fname
, "%s.mod.c", mod
->name
);
1408 write_if_changed(&buf
, fname
);
1412 write_dump(dump_write
);