1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006-2008 Sam Ravnborg
6 * Based in part on module-init-tools/depmod.c,file2alias
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * Usage: modpost vmlinux module1.o module2.o ...
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 /* Warn about section mismatch in vmlinux if set to 1 */
27 static int vmlinux_section_warnings
= 1;
28 /* Only warn about unresolved symbols */
29 static int warn_unresolved
= 0;
30 /* How a symbol is exported */
32 export_plain
, export_unused
, export_gpl
,
33 export_unused_gpl
, export_gpl_future
, export_unknown
36 #define PRINTF __attribute__ ((format (printf, 1, 2)))
38 PRINTF
void fatal(const char *fmt
, ...)
42 fprintf(stderr
, "FATAL: ");
44 va_start(arglist
, fmt
);
45 vfprintf(stderr
, fmt
, arglist
);
51 PRINTF
void warn(const char *fmt
, ...)
55 fprintf(stderr
, "WARNING: ");
57 va_start(arglist
, fmt
);
58 vfprintf(stderr
, fmt
, arglist
);
62 PRINTF
void merror(const char *fmt
, ...)
66 fprintf(stderr
, "ERROR: ");
68 va_start(arglist
, fmt
);
69 vfprintf(stderr
, fmt
, arglist
);
73 static int is_vmlinux(const char *modname
)
77 myname
= strrchr(modname
, '/');
83 return (strcmp(myname
, "vmlinux") == 0) ||
84 (strcmp(myname
, "vmlinux.o") == 0);
87 void *do_nofail(void *ptr
, const char *expr
)
90 fatal("modpost: Memory allocation failure: %s.\n", expr
);
95 /* A list of all modules we processed */
96 static struct module
*modules
;
98 static struct module
*find_module(char *modname
)
102 for (mod
= modules
; mod
; mod
= mod
->next
)
103 if (strcmp(mod
->name
, modname
) == 0)
108 static struct module
*new_module(char *modname
)
113 mod
= NOFAIL(malloc(sizeof(*mod
)));
114 memset(mod
, 0, sizeof(*mod
));
115 p
= NOFAIL(strdup(modname
));
117 /* strip trailing .o */
120 if (strcmp(s
, ".o") == 0)
125 mod
->gpl_compatible
= -1;
132 /* A hash of all exported symbols,
133 * struct symbol is also used for lists of unresolved symbols */
135 #define SYMBOL_HASH_SIZE 1024
139 struct module
*module
;
143 unsigned int vmlinux
:1; /* 1 if symbol is defined in vmlinux */
144 unsigned int kernel
:1; /* 1 if symbol is from kernel
145 * (only for external modules) **/
146 unsigned int preloaded
:1; /* 1 if symbol from Module.symvers */
147 enum export export
; /* Type of export */
151 static struct symbol
*symbolhash
[SYMBOL_HASH_SIZE
];
153 /* This is based on the hash agorithm from gdbm, via tdb */
154 static inline unsigned int tdb_hash(const char *name
)
156 unsigned value
; /* Used to compute the hash value. */
157 unsigned i
; /* Used to cycle through random values. */
159 /* Set the initial value from the key size. */
160 for (value
= 0x238F13AF * strlen(name
), i
= 0; name
[i
]; i
++)
161 value
= (value
+ (((unsigned char *)name
)[i
] << (i
*5 % 24)));
163 return (1103515243 * value
+ 12345);
167 * Allocate a new symbols for use in the hash of exported symbols or
168 * the list of unresolved symbols per module
170 static struct symbol
*alloc_symbol(const char *name
, unsigned int weak
,
173 struct symbol
*s
= NOFAIL(malloc(sizeof(*s
) + strlen(name
) + 1));
175 memset(s
, 0, sizeof(*s
));
176 strcpy(s
->name
, name
);
182 /* For the hash of exported symbols */
183 static struct symbol
*new_symbol(const char *name
, struct module
*module
,
189 hash
= tdb_hash(name
) % SYMBOL_HASH_SIZE
;
190 new = symbolhash
[hash
] = alloc_symbol(name
, 0, symbolhash
[hash
]);
191 new->module
= module
;
192 new->export
= export
;
196 static struct symbol
*find_symbol(const char *name
)
200 /* For our purposes, .foo matches foo. PPC64 needs this. */
204 for (s
= symbolhash
[tdb_hash(name
) % SYMBOL_HASH_SIZE
]; s
; s
= s
->next
) {
205 if (strcmp(s
->name
, name
) == 0)
215 { .str
= "EXPORT_SYMBOL", .export
= export_plain
},
216 { .str
= "EXPORT_UNUSED_SYMBOL", .export
= export_unused
},
217 { .str
= "EXPORT_SYMBOL_GPL", .export
= export_gpl
},
218 { .str
= "EXPORT_UNUSED_SYMBOL_GPL", .export
= export_unused_gpl
},
219 { .str
= "EXPORT_SYMBOL_GPL_FUTURE", .export
= export_gpl_future
},
220 { .str
= "(unknown)", .export
= export_unknown
},
224 static const char *export_str(enum export ex
)
226 return export_list
[ex
].str
;
229 static enum export
export_no(const char *s
)
234 return export_unknown
;
235 for (i
= 0; export_list
[i
].export
!= export_unknown
; i
++) {
236 if (strcmp(export_list
[i
].str
, s
) == 0)
237 return export_list
[i
].export
;
239 return export_unknown
;
242 static enum export
export_from_sec(struct elf_info
*elf
, Elf_Section sec
)
244 if (sec
== elf
->export_sec
)
246 else if (sec
== elf
->export_unused_sec
)
247 return export_unused
;
248 else if (sec
== elf
->export_gpl_sec
)
250 else if (sec
== elf
->export_unused_gpl_sec
)
251 return export_unused_gpl
;
252 else if (sec
== elf
->export_gpl_future_sec
)
253 return export_gpl_future
;
255 return export_unknown
;
259 * Add an exported symbol - it may have already been added without a
260 * CRC, in this case just update the CRC
262 static struct symbol
*sym_add_exported(const char *name
, struct module
*mod
,
265 struct symbol
*s
= find_symbol(name
);
268 s
= new_symbol(name
, mod
, export
);
271 warn("%s: '%s' exported twice. Previous export "
272 "was in %s%s\n", mod
->name
, name
,
274 is_vmlinux(s
->module
->name
) ?"":".ko");
276 /* In case Modules.symvers was out of date */
281 s
->vmlinux
= is_vmlinux(mod
->name
);
287 static void sym_update_crc(const char *name
, struct module
*mod
,
288 unsigned int crc
, enum export export
)
290 struct symbol
*s
= find_symbol(name
);
293 s
= new_symbol(name
, mod
, export
);
298 void *grab_file(const char *filename
, unsigned long *size
)
304 fd
= open(filename
, O_RDONLY
);
305 if (fd
< 0 || fstat(fd
, &st
) != 0)
309 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
312 if (map
== MAP_FAILED
)
318 * Return a copy of the next line in a mmap'ed file.
319 * spaces in the beginning of the line is trimmed away.
320 * Return a pointer to a static buffer.
322 char *get_next_line(unsigned long *pos
, void *file
, unsigned long size
)
324 static char line
[4096];
327 signed char *p
= (signed char *)file
+ *pos
;
330 for (; *pos
< size
; (*pos
)++) {
331 if (skip
&& isspace(*p
)) {
336 if (*p
!= '\n' && (*pos
< size
)) {
340 break; /* Too long, stop */
351 void release_file(void *file
, unsigned long size
)
356 static int parse_elf(struct elf_info
*info
, const char *filename
)
363 hdr
= grab_file(filename
, &info
->size
);
369 if (info
->size
< sizeof(*hdr
)) {
370 /* file too small, assume this is an empty .o file */
373 /* Is this a valid ELF file? */
374 if ((hdr
->e_ident
[EI_MAG0
] != ELFMAG0
) ||
375 (hdr
->e_ident
[EI_MAG1
] != ELFMAG1
) ||
376 (hdr
->e_ident
[EI_MAG2
] != ELFMAG2
) ||
377 (hdr
->e_ident
[EI_MAG3
] != ELFMAG3
)) {
378 /* Not an ELF file - silently ignore it */
381 /* Fix endianness in ELF header */
382 hdr
->e_shoff
= TO_NATIVE(hdr
->e_shoff
);
383 hdr
->e_shstrndx
= TO_NATIVE(hdr
->e_shstrndx
);
384 hdr
->e_shnum
= TO_NATIVE(hdr
->e_shnum
);
385 hdr
->e_machine
= TO_NATIVE(hdr
->e_machine
);
386 hdr
->e_type
= TO_NATIVE(hdr
->e_type
);
387 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
388 info
->sechdrs
= sechdrs
;
390 /* Check if file offset is correct */
391 if (hdr
->e_shoff
> info
->size
) {
392 fatal("section header offset=%lu in file '%s' is bigger than "
393 "filesize=%lu\n", (unsigned long)hdr
->e_shoff
,
394 filename
, info
->size
);
398 /* Fix endianness in section headers */
399 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
400 sechdrs
[i
].sh_type
= TO_NATIVE(sechdrs
[i
].sh_type
);
401 sechdrs
[i
].sh_offset
= TO_NATIVE(sechdrs
[i
].sh_offset
);
402 sechdrs
[i
].sh_size
= TO_NATIVE(sechdrs
[i
].sh_size
);
403 sechdrs
[i
].sh_link
= TO_NATIVE(sechdrs
[i
].sh_link
);
404 sechdrs
[i
].sh_name
= TO_NATIVE(sechdrs
[i
].sh_name
);
405 sechdrs
[i
].sh_info
= TO_NATIVE(sechdrs
[i
].sh_info
);
406 sechdrs
[i
].sh_addr
= TO_NATIVE(sechdrs
[i
].sh_addr
);
408 /* Find symbol table. */
409 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
410 const char *secstrings
411 = (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
414 if (sechdrs
[i
].sh_offset
> info
->size
) {
415 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
416 "sizeof(*hrd)=%zu\n", filename
,
417 (unsigned long)sechdrs
[i
].sh_offset
,
421 secname
= secstrings
+ sechdrs
[i
].sh_name
;
422 if (strcmp(secname
, ".modinfo") == 0) {
423 info
->modinfo
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
424 info
->modinfo_len
= sechdrs
[i
].sh_size
;
425 } else if (strcmp(secname
, "__ksymtab") == 0)
426 info
->export_sec
= i
;
427 else if (strcmp(secname
, "__ksymtab_unused") == 0)
428 info
->export_unused_sec
= i
;
429 else if (strcmp(secname
, "__ksymtab_gpl") == 0)
430 info
->export_gpl_sec
= i
;
431 else if (strcmp(secname
, "__ksymtab_unused_gpl") == 0)
432 info
->export_unused_gpl_sec
= i
;
433 else if (strcmp(secname
, "__ksymtab_gpl_future") == 0)
434 info
->export_gpl_future_sec
= i
;
436 if (sechdrs
[i
].sh_type
!= SHT_SYMTAB
)
439 info
->symtab_start
= (void *)hdr
+ sechdrs
[i
].sh_offset
;
440 info
->symtab_stop
= (void *)hdr
+ sechdrs
[i
].sh_offset
441 + sechdrs
[i
].sh_size
;
442 info
->strtab
= (void *)hdr
+
443 sechdrs
[sechdrs
[i
].sh_link
].sh_offset
;
445 if (!info
->symtab_start
)
446 fatal("%s has no symtab?\n", filename
);
448 /* Fix endianness in symbols */
449 for (sym
= info
->symtab_start
; sym
< info
->symtab_stop
; sym
++) {
450 sym
->st_shndx
= TO_NATIVE(sym
->st_shndx
);
451 sym
->st_name
= TO_NATIVE(sym
->st_name
);
452 sym
->st_value
= TO_NATIVE(sym
->st_value
);
453 sym
->st_size
= TO_NATIVE(sym
->st_size
);
458 static void parse_elf_finish(struct elf_info
*info
)
460 release_file(info
->hdr
, info
->size
);
463 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
464 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
466 static void handle_modversions(struct module
*mod
, struct elf_info
*info
,
467 Elf_Sym
*sym
, const char *symname
)
470 enum export export
= export_from_sec(info
, sym
->st_shndx
);
472 switch (sym
->st_shndx
) {
474 warn("\"%s\" [%s] is COMMON symbol\n", symname
, mod
->name
);
478 if (memcmp(symname
, CRC_PFX
, strlen(CRC_PFX
)) == 0) {
479 crc
= (unsigned int) sym
->st_value
;
480 sym_update_crc(symname
+ strlen(CRC_PFX
), mod
, crc
,
485 /* undefined symbol */
486 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
487 ELF_ST_BIND(sym
->st_info
) != STB_WEAK
)
489 /* ignore global offset table */
490 if (strcmp(symname
, "_GLOBAL_OFFSET_TABLE_") == 0)
492 /* ignore __this_module, it will be resolved shortly */
493 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"__this_module") == 0)
495 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
496 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
497 /* add compatibility with older glibc */
498 #ifndef STT_SPARC_REGISTER
499 #define STT_SPARC_REGISTER STT_REGISTER
501 if (info
->hdr
->e_machine
== EM_SPARC
||
502 info
->hdr
->e_machine
== EM_SPARCV9
) {
503 /* Ignore register directives. */
504 if (ELF_ST_TYPE(sym
->st_info
) == STT_SPARC_REGISTER
)
506 if (symname
[0] == '.') {
507 char *munged
= strdup(symname
);
509 munged
[1] = toupper(munged
[1]);
515 if (memcmp(symname
, MODULE_SYMBOL_PREFIX
,
516 strlen(MODULE_SYMBOL_PREFIX
)) == 0) {
518 alloc_symbol(symname
+
519 strlen(MODULE_SYMBOL_PREFIX
),
520 ELF_ST_BIND(sym
->st_info
) == STB_WEAK
,
525 /* All exported symbols */
526 if (memcmp(symname
, KSYMTAB_PFX
, strlen(KSYMTAB_PFX
)) == 0) {
527 sym_add_exported(symname
+ strlen(KSYMTAB_PFX
), mod
,
530 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"init_module") == 0)
532 if (strcmp(symname
, MODULE_SYMBOL_PREFIX
"cleanup_module") == 0)
533 mod
->has_cleanup
= 1;
539 * Parse tag=value strings from .modinfo section
541 static char *next_string(char *string
, unsigned long *secsize
)
543 /* Skip non-zero chars */
546 if ((*secsize
)-- <= 1)
550 /* Skip any zero padding. */
553 if ((*secsize
)-- <= 1)
559 static char *get_next_modinfo(void *modinfo
, unsigned long modinfo_len
,
560 const char *tag
, char *info
)
563 unsigned int taglen
= strlen(tag
);
564 unsigned long size
= modinfo_len
;
567 size
-= info
- (char *)modinfo
;
568 modinfo
= next_string(info
, &size
);
571 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
572 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
573 return p
+ taglen
+ 1;
578 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
582 return get_next_modinfo(modinfo
, modinfo_len
, tag
, NULL
);
586 * Test if string s ends in string sub
589 static int strrcmp(const char *s
, const char *sub
)
597 sublen
= strlen(sub
);
599 if ((slen
== 0) || (sublen
== 0))
605 return memcmp(s
+ slen
- sublen
, sub
, sublen
);
608 static const char *sym_name(struct elf_info
*elf
, Elf_Sym
*sym
)
611 return elf
->strtab
+ sym
->st_name
;
616 static const char *sec_name(struct elf_info
*elf
, int shndx
)
618 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
619 return (void *)elf
->hdr
+
620 elf
->sechdrs
[elf
->hdr
->e_shstrndx
].sh_offset
+
621 sechdrs
[shndx
].sh_name
;
624 static const char *sech_name(struct elf_info
*elf
, Elf_Shdr
*sechdr
)
626 return (void *)elf
->hdr
+
627 elf
->sechdrs
[elf
->hdr
->e_shstrndx
].sh_offset
+
631 /* if sym is empty or point to a string
632 * like ".[0-9]+" then return 1.
633 * This is the optional prefix added by ld to some sections
635 static int number_prefix(const char *sym
)
643 if (c
< '0' || c
> '9')
649 /* The pattern is an array of simple patterns.
650 * "foo" will match an exact string equal to "foo"
651 * "*foo" will match a string that ends with "foo"
652 * "foo*" will match a string that begins with "foo"
653 * "foo$" will match a string equal to "foo" or "foo.1"
654 * where the '1' can be any number including several digits.
655 * The $ syntax is for sections where ld append a dot number
656 * to make section name unique.
658 int match(const char *sym
, const char * const pat
[])
663 const char *endp
= p
+ strlen(p
) - 1;
667 if (strrcmp(sym
, p
+ 1) == 0)
671 else if (*endp
== '*') {
672 if (strncmp(sym
, p
, strlen(p
) - 1) == 0)
676 else if (*endp
== '$') {
677 if (strncmp(sym
, p
, strlen(p
) - 1) == 0) {
678 if (number_prefix(sym
+ strlen(p
) - 1))
684 if (strcmp(p
, sym
) == 0)
692 /* sections that we do not want to do full section mismatch check on */
693 static const char *section_white_list
[] =
694 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL
};
696 #define ALL_INIT_DATA_SECTIONS \
697 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
698 #define ALL_EXIT_DATA_SECTIONS \
699 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
701 #define ALL_INIT_TEXT_SECTIONS \
702 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
703 #define ALL_EXIT_TEXT_SECTIONS \
704 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
706 #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
707 #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
709 #define DATA_SECTIONS ".data$", ".data.rel$"
710 #define TEXT_SECTIONS ".text$"
712 #define INIT_SECTIONS ".init.data$", ".init.text$"
713 #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
714 #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
715 #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
717 #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
718 #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
719 #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
720 #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
722 /* init data sections */
723 static const char *init_data_sections
[] = { ALL_INIT_DATA_SECTIONS
, NULL
};
725 /* all init sections */
726 static const char *init_sections
[] = { ALL_INIT_SECTIONS
, NULL
};
728 /* All init and exit sections (code + data) */
729 static const char *init_exit_sections
[] =
730 {ALL_INIT_SECTIONS
, ALL_EXIT_SECTIONS
, NULL
};
733 static const char *data_sections
[] = { DATA_SECTIONS
, NULL
};
735 /* sections that may refer to an init/exit section with no warning */
736 static const char *initref_sections
[] =
745 /* symbols in .data that may refer to init/exit sections */
746 static const char *symbol_white_list
[] =
749 "*_template", /* scsi uses *_template a lot */
750 "*_timer", /* arm uses ops structures named _timer a lot */
751 "*_sht", /* scsi also used *_sht to some extent */
759 static const char *head_sections
[] = { ".head.text*", NULL
};
760 static const char *linker_symbols
[] =
761 { "__init_begin", "_sinittext", "_einittext", NULL
};
763 struct sectioncheck
{
764 const char *fromsec
[20];
765 const char *tosec
[20];
768 const struct sectioncheck sectioncheck
[] = {
769 /* Do not reference init/exit code/data from
770 * normal code and data
773 .fromsec
= { TEXT_SECTIONS
, DATA_SECTIONS
, NULL
},
774 .tosec
= { ALL_INIT_SECTIONS
, ALL_EXIT_SECTIONS
, NULL
}
776 /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
778 .fromsec
= { DEV_INIT_SECTIONS
, CPU_INIT_SECTIONS
, MEM_INIT_SECTIONS
, NULL
},
779 .tosec
= { INIT_SECTIONS
, NULL
}
781 /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
783 .fromsec
= { DEV_EXIT_SECTIONS
, CPU_EXIT_SECTIONS
, MEM_EXIT_SECTIONS
, NULL
},
784 .tosec
= { EXIT_SECTIONS
, NULL
}
786 /* Do not use exit code/data from init code */
788 .fromsec
= { ALL_INIT_SECTIONS
, NULL
},
789 .tosec
= { ALL_EXIT_SECTIONS
, NULL
},
791 /* Do not use init code/data from exit code */
793 .fromsec
= { ALL_EXIT_SECTIONS
, NULL
},
794 .tosec
= { ALL_INIT_SECTIONS
, NULL
}
796 /* Do not export init/exit functions or data */
798 .fromsec
= { "__ksymtab*", NULL
},
799 .tosec
= { ALL_INIT_SECTIONS
, ALL_EXIT_SECTIONS
, NULL
}
803 static int section_mismatch(const char *fromsec
, const char *tosec
)
806 int elems
= sizeof(sectioncheck
) / sizeof(struct sectioncheck
);
807 const struct sectioncheck
*check
= §ioncheck
[0];
809 for (i
= 0; i
< elems
; i
++) {
810 if (match(fromsec
, check
->fromsec
) &&
811 match(tosec
, check
->tosec
))
819 * Whitelist to allow certain references to pass with no warning.
822 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
823 * The pattern is identified by:
824 * fromsec = .text.init.refok* | .data.init.refok*
827 * If a module parameter is declared __initdata and permissions=0
828 * then this is legal despite the warning generated.
829 * We cannot see value of permissions here, so just ignore
831 * The pattern is identified by:
837 * Many drivers utilise a *driver container with references to
838 * add, remove, probe functions etc.
839 * These functions may often be marked __init and we do not want to
841 * the pattern is identified by:
842 * tosec = init or exit section
843 * fromsec = data section
844 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
845 * *probe_one, *_console, *_timer
848 * Whitelist all refereces from .text.head to .init.data
849 * Whitelist all refereces from .text.head to .init.text
852 * Some symbols belong to init section but still it is ok to reference
853 * these from non-init sections as these symbols don't have any memory
854 * allocated for them and symbol address and value are same. So even
855 * if init section is freed, its ok to reference those symbols.
856 * For ex. symbols marking the init section boundaries.
857 * This pattern is identified by
858 * refsymname = __init_begin, _sinittext, _einittext
861 static int secref_whitelist(const char *fromsec
, const char *fromsym
,
862 const char *tosec
, const char *tosym
)
864 /* Check for pattern 0 */
865 if (match(fromsec
, initref_sections
))
868 /* Check for pattern 1 */
869 if (match(tosec
, init_data_sections
) &&
870 match(fromsec
, data_sections
) &&
871 (strncmp(fromsym
, "__param", strlen("__param")) == 0))
874 /* Check for pattern 2 */
875 if (match(tosec
, init_exit_sections
) &&
876 match(fromsec
, data_sections
) &&
877 match(fromsym
, symbol_white_list
))
880 /* Check for pattern 3 */
881 if (match(fromsec
, head_sections
) &&
882 match(tosec
, init_sections
))
885 /* Check for pattern 4 */
886 if (match(tosym
, linker_symbols
))
893 * Find symbol based on relocation record info.
894 * In some cases the symbol supplied is a valid symbol so
895 * return refsym. If st_name != 0 we assume this is a valid symbol.
896 * In other cases the symbol needs to be looked up in the symbol table
897 * based on section and address.
899 static Elf_Sym
*find_elf_symbol(struct elf_info
*elf
, Elf64_Sword addr
,
903 Elf_Sym
*near
= NULL
;
904 Elf64_Sword distance
= 20;
907 if (relsym
->st_name
!= 0)
909 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
910 if (sym
->st_shndx
!= relsym
->st_shndx
)
912 if (ELF_ST_TYPE(sym
->st_info
) == STT_SECTION
)
914 if (sym
->st_value
== addr
)
916 /* Find a symbol nearby - addr are maybe negative */
917 d
= sym
->st_value
- addr
;
919 d
= addr
- sym
->st_value
;
925 /* We need a close match */
932 static inline int is_arm_mapping_symbol(const char *str
)
934 return str
[0] == '$' && strchr("atd", str
[1])
935 && (str
[2] == '\0' || str
[2] == '.');
939 * If there's no name there, ignore it; likewise, ignore it if it's
940 * one of the magic symbols emitted used by current ARM tools.
942 * Otherwise if find_symbols_between() returns those symbols, they'll
943 * fail the whitelist tests and cause lots of false alarms ... fixable
944 * only by merging __exit and __init sections into __text, bloating
945 * the kernel (which is especially evil on embedded platforms).
947 static inline int is_valid_name(struct elf_info
*elf
, Elf_Sym
*sym
)
949 const char *name
= elf
->strtab
+ sym
->st_name
;
951 if (!name
|| !strlen(name
))
953 return !is_arm_mapping_symbol(name
);
957 * Find symbols before or equal addr and after addr - in the section sec.
958 * If we find two symbols with equal offset prefer one with a valid name.
959 * The ELF format may have a better way to detect what type of symbol
960 * it is, but this works for now.
962 static Elf_Sym
*find_elf_symbol2(struct elf_info
*elf
, Elf_Addr addr
,
966 Elf_Sym
*near
= NULL
;
967 Elf_Addr distance
= ~0;
969 for (sym
= elf
->symtab_start
; sym
< elf
->symtab_stop
; sym
++) {
972 if (sym
->st_shndx
>= SHN_LORESERVE
)
974 symsec
= sec_name(elf
, sym
->st_shndx
);
975 if (strcmp(symsec
, sec
) != 0)
977 if (!is_valid_name(elf
, sym
))
979 if (sym
->st_value
<= addr
) {
980 if ((addr
- sym
->st_value
) < distance
) {
981 distance
= addr
- sym
->st_value
;
983 } else if ((addr
- sym
->st_value
) == distance
) {
992 * Print a warning about a section mismatch.
993 * Try to find symbols near it so user can find it.
994 * Check whitelist before warning - it may be a false positive.
996 static void report_sec_mismatch(const char *modname
,
998 unsigned long long fromaddr
,
1000 const char *tosec
, const char *tosym
)
1002 if (strlen(tosym
)) {
1003 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
1005 modname
, fromsec
, fromaddr
,
1006 tosec
, tosym
, fromsym
);
1008 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
1009 modname
, fromsec
, fromaddr
,
1014 static void check_section_mismatch(const char *modname
, struct elf_info
*elf
,
1015 Elf_Rela
*r
, Elf_Sym
*sym
, const char *fromsec
)
1019 tosec
= sec_name(elf
, sym
->st_shndx
);
1020 if (section_mismatch(fromsec
, tosec
)) {
1021 const char *fromsym
;
1024 fromsym
= sym_name(elf
,
1025 find_elf_symbol2(elf
, r
->r_offset
, fromsec
));
1026 tosym
= sym_name(elf
,
1027 find_elf_symbol(elf
, r
->r_addend
, sym
));
1029 /* check whitelist - we may ignore it */
1030 if (secref_whitelist(fromsec
, fromsym
, tosec
, tosym
)) {
1031 report_sec_mismatch(modname
, fromsec
, r
->r_offset
,
1032 fromsym
, tosec
, tosym
);
1037 static unsigned int *reloc_location(struct elf_info
*elf
,
1038 Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1040 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
1041 int section
= sechdr
->sh_info
;
1043 return (void *)elf
->hdr
+ sechdrs
[section
].sh_offset
+
1044 (r
->r_offset
- sechdrs
[section
].sh_addr
);
1047 static int addend_386_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1049 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1050 unsigned int *location
= reloc_location(elf
, sechdr
, r
);
1054 r
->r_addend
= TO_NATIVE(*location
);
1057 r
->r_addend
= TO_NATIVE(*location
) + 4;
1058 /* For CONFIG_RELOCATABLE=y */
1059 if (elf
->hdr
->e_type
== ET_EXEC
)
1060 r
->r_addend
+= r
->r_offset
;
1066 static int addend_arm_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1068 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1072 /* From ARM ABI: (S + A) | T */
1073 r
->r_addend
= (int)(long)
1074 (elf
->symtab_start
+ ELF_R_SYM(r
->r_info
));
1077 /* From ARM ABI: ((S + A) | T) - P */
1078 r
->r_addend
= (int)(long)(elf
->hdr
+
1080 (r
->r_offset
- sechdr
->sh_addr
));
1088 static int addend_mips_rel(struct elf_info
*elf
, Elf_Shdr
*sechdr
, Elf_Rela
*r
)
1090 unsigned int r_typ
= ELF_R_TYPE(r
->r_info
);
1091 unsigned int *location
= reloc_location(elf
, sechdr
, r
);
1094 if (r_typ
== R_MIPS_HI16
)
1095 return 1; /* skip this */
1096 inst
= TO_NATIVE(*location
);
1099 r
->r_addend
= inst
& 0xffff;
1102 r
->r_addend
= (inst
& 0x03ffffff) << 2;
1111 static void section_rela(const char *modname
, struct elf_info
*elf
,
1118 const char *fromsec
;
1120 Elf_Rela
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1121 Elf_Rela
*stop
= (void *)start
+ sechdr
->sh_size
;
1123 fromsec
= sech_name(elf
, sechdr
);
1124 fromsec
+= strlen(".rela");
1125 /* if from section (name) is know good then skip it */
1126 if (match(fromsec
, section_white_list
))
1128 for (rela
= start
; rela
< stop
; rela
++) {
1129 r
.r_offset
= TO_NATIVE(rela
->r_offset
);
1130 #if KERNEL_ELFCLASS == ELFCLASS64
1131 if (elf
->hdr
->e_machine
== EM_MIPS
) {
1133 r_sym
= ELF64_MIPS_R_SYM(rela
->r_info
);
1134 r_sym
= TO_NATIVE(r_sym
);
1135 r_typ
= ELF64_MIPS_R_TYPE(rela
->r_info
);
1136 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
1138 r
.r_info
= TO_NATIVE(rela
->r_info
);
1139 r_sym
= ELF_R_SYM(r
.r_info
);
1142 r
.r_info
= TO_NATIVE(rela
->r_info
);
1143 r_sym
= ELF_R_SYM(r
.r_info
);
1145 r
.r_addend
= TO_NATIVE(rela
->r_addend
);
1146 sym
= elf
->symtab_start
+ r_sym
;
1147 /* Skip special sections */
1148 if (sym
->st_shndx
>= SHN_LORESERVE
)
1150 check_section_mismatch(modname
, elf
, &r
, sym
, fromsec
);
1154 static void section_rel(const char *modname
, struct elf_info
*elf
,
1161 const char *fromsec
;
1163 Elf_Rel
*start
= (void *)elf
->hdr
+ sechdr
->sh_offset
;
1164 Elf_Rel
*stop
= (void *)start
+ sechdr
->sh_size
;
1166 fromsec
= sech_name(elf
, sechdr
);
1167 fromsec
+= strlen(".rel");
1168 /* if from section (name) is know good then skip it */
1169 if (match(fromsec
, section_white_list
))
1172 for (rel
= start
; rel
< stop
; rel
++) {
1173 r
.r_offset
= TO_NATIVE(rel
->r_offset
);
1174 #if KERNEL_ELFCLASS == ELFCLASS64
1175 if (elf
->hdr
->e_machine
== EM_MIPS
) {
1177 r_sym
= ELF64_MIPS_R_SYM(rel
->r_info
);
1178 r_sym
= TO_NATIVE(r_sym
);
1179 r_typ
= ELF64_MIPS_R_TYPE(rel
->r_info
);
1180 r
.r_info
= ELF64_R_INFO(r_sym
, r_typ
);
1182 r
.r_info
= TO_NATIVE(rel
->r_info
);
1183 r_sym
= ELF_R_SYM(r
.r_info
);
1186 r
.r_info
= TO_NATIVE(rel
->r_info
);
1187 r_sym
= ELF_R_SYM(r
.r_info
);
1190 switch (elf
->hdr
->e_machine
) {
1192 if (addend_386_rel(elf
, sechdr
, &r
))
1196 if (addend_arm_rel(elf
, sechdr
, &r
))
1200 if (addend_mips_rel(elf
, sechdr
, &r
))
1204 sym
= elf
->symtab_start
+ r_sym
;
1205 /* Skip special sections */
1206 if (sym
->st_shndx
>= SHN_LORESERVE
)
1208 check_section_mismatch(modname
, elf
, &r
, sym
, fromsec
);
1213 * A module includes a number of sections that are discarded
1214 * either when loaded or when used as built-in.
1215 * For loaded modules all functions marked __init and all data
1216 * marked __initdata will be discarded when the module has been intialized.
1217 * Likewise for modules used built-in the sections marked __exit
1218 * are discarded because __exit marked function are supposed to be called
1219 * only when a moduel is unloaded which never happes for built-in modules.
1220 * The check_sec_ref() function traverses all relocation records
1221 * to find all references to a section that reference a section that will
1222 * be discarded and warns about it.
1224 static void check_sec_ref(struct module
*mod
, const char *modname
,
1225 struct elf_info
*elf
)
1228 Elf_Shdr
*sechdrs
= elf
->sechdrs
;
1230 /* Walk through all sections */
1231 for (i
= 0; i
< elf
->hdr
->e_shnum
; i
++) {
1232 /* We want to process only relocation sections and not .init */
1233 if (sechdrs
[i
].sh_type
== SHT_RELA
)
1234 section_rela(modname
, elf
, &elf
->sechdrs
[i
]);
1235 else if (sechdrs
[i
].sh_type
== SHT_REL
)
1236 section_rel(modname
, elf
, &elf
->sechdrs
[i
]);
1240 static void read_symbols(char *modname
)
1242 const char *symname
;
1246 struct elf_info info
= { };
1249 if (!parse_elf(&info
, modname
))
1252 mod
= new_module(modname
);
1254 /* When there's no vmlinux, don't print warnings about
1255 * unresolved symbols (since there'll be too many ;) */
1256 if (is_vmlinux(modname
)) {
1261 license
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "license");
1263 if (license_is_gpl_compatible(license
))
1264 mod
->gpl_compatible
= 1;
1266 mod
->gpl_compatible
= 0;
1269 license
= get_next_modinfo(info
.modinfo
, info
.modinfo_len
,
1270 "license", license
);
1273 for (sym
= info
.symtab_start
; sym
< info
.symtab_stop
; sym
++) {
1274 symname
= info
.strtab
+ sym
->st_name
;
1276 handle_modversions(mod
, &info
, sym
, symname
);
1277 handle_moddevtable(mod
, &info
, sym
, symname
);
1279 if (!is_vmlinux(modname
) ||
1280 (is_vmlinux(modname
) && vmlinux_section_warnings
))
1281 check_sec_ref(mod
, modname
, &info
);
1283 version
= get_modinfo(info
.modinfo
, info
.modinfo_len
, "version");
1285 maybe_frob_rcs_version(modname
, version
, info
.modinfo
,
1286 version
- (char *)info
.hdr
);
1287 if (version
|| (all_versions
&& !is_vmlinux(modname
)))
1288 get_src_version(modname
, mod
->srcversion
,
1289 sizeof(mod
->srcversion
)-1);
1291 parse_elf_finish(&info
);
1293 /* Our trick to get versioning for struct_module - it's
1294 * never passed as an argument to an exported function, so
1295 * the automatic versioning doesn't pick it up, but it's really
1296 * important anyhow */
1298 mod
->unres
= alloc_symbol("struct_module", 0, mod
->unres
);
1303 /* We first write the generated file into memory using the
1304 * following helper, then compare to the file on disk and
1305 * only update the later if anything changed */
1307 void __attribute__((format(printf
, 2, 3))) buf_printf(struct buffer
*buf
,
1308 const char *fmt
, ...)
1315 len
= vsnprintf(tmp
, SZ
, fmt
, ap
);
1316 buf_write(buf
, tmp
, len
);
1320 void buf_write(struct buffer
*buf
, const char *s
, int len
)
1322 if (buf
->size
- buf
->pos
< len
) {
1323 buf
->size
+= len
+ SZ
;
1324 buf
->p
= realloc(buf
->p
, buf
->size
);
1326 strncpy(buf
->p
+ buf
->pos
, s
, len
);
1330 static void check_for_gpl_usage(enum export exp
, const char *m
, const char *s
)
1332 const char *e
= is_vmlinux(m
) ?"":".ko";
1336 fatal("modpost: GPL-incompatible module %s%s "
1337 "uses GPL-only symbol '%s'\n", m
, e
, s
);
1339 case export_unused_gpl
:
1340 fatal("modpost: GPL-incompatible module %s%s "
1341 "uses GPL-only symbol marked UNUSED '%s'\n", m
, e
, s
);
1343 case export_gpl_future
:
1344 warn("modpost: GPL-incompatible module %s%s "
1345 "uses future GPL-only symbol '%s'\n", m
, e
, s
);
1349 case export_unknown
:
1355 static void check_for_unused(enum export exp
, const char *m
, const char *s
)
1357 const char *e
= is_vmlinux(m
) ?"":".ko";
1361 case export_unused_gpl
:
1362 warn("modpost: module %s%s "
1363 "uses symbol '%s' marked UNUSED\n", m
, e
, s
);
1371 static void check_exports(struct module
*mod
)
1373 struct symbol
*s
, *exp
;
1375 for (s
= mod
->unres
; s
; s
= s
->next
) {
1376 const char *basename
;
1377 exp
= find_symbol(s
->name
);
1378 if (!exp
|| exp
->module
== mod
)
1380 basename
= strrchr(mod
->name
, '/');
1384 basename
= mod
->name
;
1385 if (!mod
->gpl_compatible
)
1386 check_for_gpl_usage(exp
->export
, basename
, exp
->name
);
1387 check_for_unused(exp
->export
, basename
, exp
->name
);
1392 * Header for the generated file
1394 static void add_header(struct buffer
*b
, struct module
*mod
)
1396 buf_printf(b
, "#include <linux/module.h>\n");
1397 buf_printf(b
, "#include <linux/vermagic.h>\n");
1398 buf_printf(b
, "#include <linux/compiler.h>\n");
1399 buf_printf(b
, "\n");
1400 buf_printf(b
, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1401 buf_printf(b
, "\n");
1402 buf_printf(b
, "struct module __this_module\n");
1403 buf_printf(b
, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1404 buf_printf(b
, " .name = KBUILD_MODNAME,\n");
1406 buf_printf(b
, " .init = init_module,\n");
1407 if (mod
->has_cleanup
)
1408 buf_printf(b
, "#ifdef CONFIG_MODULE_UNLOAD\n"
1409 " .exit = cleanup_module,\n"
1411 buf_printf(b
, " .arch = MODULE_ARCH_INIT,\n");
1412 buf_printf(b
, "};\n");
1416 * Record CRCs for unresolved symbols
1418 static int add_versions(struct buffer
*b
, struct module
*mod
)
1420 struct symbol
*s
, *exp
;
1423 for (s
= mod
->unres
; s
; s
= s
->next
) {
1424 exp
= find_symbol(s
->name
);
1425 if (!exp
|| exp
->module
== mod
) {
1426 if (have_vmlinux
&& !s
->weak
) {
1427 if (warn_unresolved
) {
1428 warn("\"%s\" [%s.ko] undefined!\n",
1429 s
->name
, mod
->name
);
1431 merror("\"%s\" [%s.ko] undefined!\n",
1432 s
->name
, mod
->name
);
1438 s
->module
= exp
->module
;
1439 s
->crc_valid
= exp
->crc_valid
;
1446 buf_printf(b
, "\n");
1447 buf_printf(b
, "static const struct modversion_info ____versions[]\n");
1448 buf_printf(b
, "__used\n");
1449 buf_printf(b
, "__attribute__((section(\"__versions\"))) = {\n");
1451 for (s
= mod
->unres
; s
; s
= s
->next
) {
1454 if (!s
->crc_valid
) {
1455 warn("\"%s\" [%s.ko] has no CRC!\n",
1456 s
->name
, mod
->name
);
1459 buf_printf(b
, "\t{ %#8x, \"%s\" },\n", s
->crc
, s
->name
);
1462 buf_printf(b
, "};\n");
1467 static void add_depends(struct buffer
*b
, struct module
*mod
,
1468 struct module
*modules
)
1474 for (m
= modules
; m
; m
= m
->next
)
1475 m
->seen
= is_vmlinux(m
->name
);
1477 buf_printf(b
, "\n");
1478 buf_printf(b
, "static const char __module_depends[]\n");
1479 buf_printf(b
, "__used\n");
1480 buf_printf(b
, "__attribute__((section(\".modinfo\"))) =\n");
1481 buf_printf(b
, "\"depends=");
1482 for (s
= mod
->unres
; s
; s
= s
->next
) {
1487 if (s
->module
->seen
)
1490 s
->module
->seen
= 1;
1491 p
= strrchr(s
->module
->name
, '/');
1495 p
= s
->module
->name
;
1496 buf_printf(b
, "%s%s", first
? "" : ",", p
);
1499 buf_printf(b
, "\";\n");
1502 static void add_srcversion(struct buffer
*b
, struct module
*mod
)
1504 if (mod
->srcversion
[0]) {
1505 buf_printf(b
, "\n");
1506 buf_printf(b
, "MODULE_INFO(srcversion, \"%s\");\n",
1511 static void write_if_changed(struct buffer
*b
, const char *fname
)
1517 file
= fopen(fname
, "r");
1521 if (fstat(fileno(file
), &st
) < 0)
1524 if (st
.st_size
!= b
->pos
)
1527 tmp
= NOFAIL(malloc(b
->pos
));
1528 if (fread(tmp
, 1, b
->pos
, file
) != b
->pos
)
1531 if (memcmp(tmp
, b
->p
, b
->pos
) != 0)
1543 file
= fopen(fname
, "w");
1548 if (fwrite(b
->p
, 1, b
->pos
, file
) != b
->pos
) {
1555 /* parse Module.symvers file. line format:
1556 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1558 static void read_dump(const char *fname
, unsigned int kernel
)
1560 unsigned long size
, pos
= 0;
1561 void *file
= grab_file(fname
, &size
);
1565 /* No symbol versions, silently ignore */
1568 while ((line
= get_next_line(&pos
, file
, size
))) {
1569 char *symname
, *modname
, *d
, *export
, *end
;
1574 if (!(symname
= strchr(line
, '\t')))
1577 if (!(modname
= strchr(symname
, '\t')))
1580 if ((export
= strchr(modname
, '\t')) != NULL
)
1582 if (export
&& ((end
= strchr(export
, '\t')) != NULL
))
1584 crc
= strtoul(line
, &d
, 16);
1585 if (*symname
== '\0' || *modname
== '\0' || *d
!= '\0')
1587 mod
= find_module(modname
);
1589 if (is_vmlinux(modname
))
1591 mod
= new_module(NOFAIL(strdup(modname
)));
1594 s
= sym_add_exported(symname
, mod
, export_no(export
));
1597 sym_update_crc(symname
, mod
, crc
, export_no(export
));
1601 fatal("parse error in symbol dump file\n");
1604 /* For normal builds always dump all symbols.
1605 * For external modules only dump symbols
1606 * that are not read from kernel Module.symvers.
1608 static int dump_sym(struct symbol
*sym
)
1610 if (!external_module
)
1612 if (sym
->vmlinux
|| sym
->kernel
)
1617 static void write_dump(const char *fname
)
1619 struct buffer buf
= { };
1620 struct symbol
*symbol
;
1623 for (n
= 0; n
< SYMBOL_HASH_SIZE
; n
++) {
1624 symbol
= symbolhash
[n
];
1626 if (dump_sym(symbol
))
1627 buf_printf(&buf
, "0x%08x\t%s\t%s\t%s\n",
1628 symbol
->crc
, symbol
->name
,
1629 symbol
->module
->name
,
1630 export_str(symbol
->export
));
1631 symbol
= symbol
->next
;
1634 write_if_changed(&buf
, fname
);
1637 int main(int argc
, char **argv
)
1640 struct buffer buf
= { };
1641 char *kernel_read
= NULL
, *module_read
= NULL
;
1642 char *dump_write
= NULL
;
1646 while ((opt
= getopt(argc
, argv
, "i:I:mso:aw")) != -1) {
1649 kernel_read
= optarg
;
1652 module_read
= optarg
;
1653 external_module
= 1;
1659 dump_write
= optarg
;
1665 vmlinux_section_warnings
= 0;
1668 warn_unresolved
= 1;
1676 read_dump(kernel_read
, 1);
1678 read_dump(module_read
, 0);
1680 while (optind
< argc
)
1681 read_symbols(argv
[optind
++]);
1683 for (mod
= modules
; mod
; mod
= mod
->next
) {
1691 for (mod
= modules
; mod
; mod
= mod
->next
) {
1692 char fname
[strlen(mod
->name
) + 10];
1699 add_header(&buf
, mod
);
1700 err
|= add_versions(&buf
, mod
);
1701 add_depends(&buf
, mod
, modules
);
1702 add_moddevtable(&buf
, mod
);
1703 add_srcversion(&buf
, mod
);
1705 sprintf(fname
, "%s.mod.c", mod
->name
);
1706 write_if_changed(&buf
, fname
);
1710 write_dump(dump_write
);