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 ...
18 #include "../../include/linux/license.h"
20 /* Are we using CONFIG_MODVERSIONS? */
22 /* Warn about undefined symbols? (do so if we have vmlinux) */
24 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
25 static int all_versions = 0;
26 /* If we are modposting external module set to 1 */
27 static int external_module = 0;
28 /* Warn about section mismatch in vmlinux if set to 1 */
29 static int vmlinux_section_warnings = 1;
30 /* Only warn about unresolved symbols */
31 static int warn_unresolved = 0;
32 /* How a symbol is exported */
33 static int sec_mismatch_count = 0;
34 static int sec_mismatch_verbose = 1;
37 export_plain, export_unused, export_gpl,
38 export_unused_gpl, export_gpl_future, export_unknown
41 #define PRINTF __attribute__ ((format (printf, 1, 2)))
43 PRINTF void fatal(const char *fmt, ...)
47 fprintf(stderr, "FATAL: ");
49 va_start(arglist, fmt);
50 vfprintf(stderr, fmt, arglist);
56 PRINTF void warn(const char *fmt, ...)
60 fprintf(stderr, "WARNING: ");
62 va_start(arglist, fmt);
63 vfprintf(stderr, fmt, arglist);
67 PRINTF void merror(const char *fmt, ...)
71 fprintf(stderr, "ERROR: ");
73 va_start(arglist, fmt);
74 vfprintf(stderr, fmt, arglist);
78 static int is_vmlinux(const char *modname)
82 myname = strrchr(modname, '/');
88 return (strcmp(myname, "vmlinux") == 0) ||
89 (strcmp(myname, "vmlinux.o") == 0);
92 void *do_nofail(void *ptr, const char *expr)
95 fatal("modpost: Memory allocation failure: %s.\n", expr);
100 /* A list of all modules we processed */
101 static struct module *modules;
103 static struct module *find_module(char *modname)
107 for (mod = modules; mod; mod = mod->next)
108 if (strcmp(mod->name, modname) == 0)
113 static struct module *new_module(char *modname)
118 mod = NOFAIL(malloc(sizeof(*mod)));
119 memset(mod, 0, sizeof(*mod));
120 p = NOFAIL(strdup(modname));
122 /* strip trailing .o */
125 if (strcmp(s, ".o") == 0)
130 mod->gpl_compatible = -1;
137 /* A hash of all exported symbols,
138 * struct symbol is also used for lists of unresolved symbols */
140 #define SYMBOL_HASH_SIZE 1024
144 struct module *module;
148 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
149 unsigned int kernel:1; /* 1 if symbol is from kernel
150 * (only for external modules) **/
151 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
152 enum export export; /* Type of export */
156 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
158 /* This is based on the hash agorithm from gdbm, via tdb */
159 static inline unsigned int tdb_hash(const char *name)
161 unsigned value; /* Used to compute the hash value. */
162 unsigned i; /* Used to cycle through random values. */
164 /* Set the initial value from the key size. */
165 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
166 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
168 return (1103515243 * value + 12345);
172 * Allocate a new symbols for use in the hash of exported symbols or
173 * the list of unresolved symbols per module
175 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
178 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
180 memset(s, 0, sizeof(*s));
181 strcpy(s->name, name);
187 /* For the hash of exported symbols */
188 static struct symbol *new_symbol(const char *name, struct module *module,
194 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
195 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
196 new->module = module;
197 new->export = export;
201 static struct symbol *find_symbol(const char *name)
205 /* For our purposes, .foo matches foo. PPC64 needs this. */
209 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
210 if (strcmp(s->name, name) == 0)
220 { .str = "EXPORT_SYMBOL", .export = export_plain },
221 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
222 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
223 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
224 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
225 { .str = "(unknown)", .export = export_unknown },
229 static const char *export_str(enum export ex)
231 return export_list[ex].str;
234 static enum export export_no(const char *s)
239 return export_unknown;
240 for (i = 0; export_list[i].export != export_unknown; i++) {
241 if (strcmp(export_list[i].str, s) == 0)
242 return export_list[i].export;
244 return export_unknown;
247 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
249 if (sec == elf->export_sec)
251 else if (sec == elf->export_unused_sec)
252 return export_unused;
253 else if (sec == elf->export_gpl_sec)
255 else if (sec == elf->export_unused_gpl_sec)
256 return export_unused_gpl;
257 else if (sec == elf->export_gpl_future_sec)
258 return export_gpl_future;
260 return export_unknown;
264 * Add an exported symbol - it may have already been added without a
265 * CRC, in this case just update the CRC
267 static struct symbol *sym_add_exported(const char *name, struct module *mod,
270 struct symbol *s = find_symbol(name);
273 s = new_symbol(name, mod, export);
276 warn("%s: '%s' exported twice. Previous export "
277 "was in %s%s\n", mod->name, name,
279 is_vmlinux(s->module->name) ?"":".ko");
281 /* In case Modules.symvers was out of date */
286 s->vmlinux = is_vmlinux(mod->name);
292 static void sym_update_crc(const char *name, struct module *mod,
293 unsigned int crc, enum export export)
295 struct symbol *s = find_symbol(name);
298 s = new_symbol(name, mod, export);
303 void *grab_file(const char *filename, unsigned long *size)
309 fd = open(filename, O_RDONLY);
310 if (fd < 0 || fstat(fd, &st) != 0)
314 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
317 if (map == MAP_FAILED)
323 * Return a copy of the next line in a mmap'ed file.
324 * spaces in the beginning of the line is trimmed away.
325 * Return a pointer to a static buffer.
327 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
329 static char line[4096];
332 signed char *p = (signed char *)file + *pos;
335 for (; *pos < size ; (*pos)++) {
336 if (skip && isspace(*p)) {
341 if (*p != '\n' && (*pos < size)) {
345 break; /* Too long, stop */
356 void release_file(void *file, unsigned long size)
361 static int parse_elf(struct elf_info *info, const char *filename)
368 hdr = grab_file(filename, &info->size);
374 if (info->size < sizeof(*hdr)) {
375 /* file too small, assume this is an empty .o file */
378 /* Is this a valid ELF file? */
379 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
380 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
381 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
382 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
383 /* Not an ELF file - silently ignore it */
386 /* Fix endianness in ELF header */
387 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
388 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
389 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
390 hdr->e_machine = TO_NATIVE(hdr->e_machine);
391 hdr->e_type = TO_NATIVE(hdr->e_type);
392 sechdrs = (void *)hdr + hdr->e_shoff;
393 info->sechdrs = sechdrs;
395 /* Check if file offset is correct */
396 if (hdr->e_shoff > info->size) {
397 fatal("section header offset=%lu in file '%s' is bigger than "
398 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
399 filename, info->size);
403 /* Fix endianness in section headers */
404 for (i = 0; i < hdr->e_shnum; i++) {
405 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
406 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
407 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
408 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
409 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
410 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
411 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
413 /* Find symbol table. */
414 for (i = 1; i < hdr->e_shnum; i++) {
415 const char *secstrings
416 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
418 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
420 if (!nobits && sechdrs[i].sh_offset > info->size) {
421 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
422 "sizeof(*hrd)=%zu\n", filename,
423 (unsigned long)sechdrs[i].sh_offset,
427 secname = secstrings + sechdrs[i].sh_name;
428 if (strcmp(secname, ".modinfo") == 0) {
430 fatal("%s has NOBITS .modinfo\n", filename);
431 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
432 info->modinfo_len = sechdrs[i].sh_size;
433 } else if (strcmp(secname, "__ksymtab") == 0)
434 info->export_sec = i;
435 else if (strcmp(secname, "__ksymtab_unused") == 0)
436 info->export_unused_sec = i;
437 else if (strcmp(secname, "__ksymtab_gpl") == 0)
438 info->export_gpl_sec = i;
439 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
440 info->export_unused_gpl_sec = i;
441 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
442 info->export_gpl_future_sec = i;
443 else if (strcmp(secname, "__markers_strings") == 0)
444 info->markers_strings_sec = i;
446 if (sechdrs[i].sh_type != SHT_SYMTAB)
449 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
450 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
451 + sechdrs[i].sh_size;
452 info->strtab = (void *)hdr +
453 sechdrs[sechdrs[i].sh_link].sh_offset;
455 if (!info->symtab_start)
456 fatal("%s has no symtab?\n", filename);
458 /* Fix endianness in symbols */
459 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
460 sym->st_shndx = TO_NATIVE(sym->st_shndx);
461 sym->st_name = TO_NATIVE(sym->st_name);
462 sym->st_value = TO_NATIVE(sym->st_value);
463 sym->st_size = TO_NATIVE(sym->st_size);
468 static void parse_elf_finish(struct elf_info *info)
470 release_file(info->hdr, info->size);
473 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
475 /* ignore __this_module, it will be resolved shortly */
476 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
478 /* ignore global offset table */
479 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
481 if (info->hdr->e_machine == EM_PPC)
482 /* Special register function linked on all modules during final link of .ko */
483 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
484 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
485 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
486 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
488 /* Do not ignore this symbol */
492 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
493 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
495 static void handle_modversions(struct module *mod, struct elf_info *info,
496 Elf_Sym *sym, const char *symname)
499 enum export export = export_from_sec(info, sym->st_shndx);
501 switch (sym->st_shndx) {
503 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
507 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
508 crc = (unsigned int) sym->st_value;
509 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
514 /* undefined symbol */
515 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
516 ELF_ST_BIND(sym->st_info) != STB_WEAK)
518 if (ignore_undef_symbol(info, symname))
520 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
521 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
522 /* add compatibility with older glibc */
523 #ifndef STT_SPARC_REGISTER
524 #define STT_SPARC_REGISTER STT_REGISTER
526 if (info->hdr->e_machine == EM_SPARC ||
527 info->hdr->e_machine == EM_SPARCV9) {
528 /* Ignore register directives. */
529 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
531 if (symname[0] == '.') {
532 char *munged = strdup(symname);
534 munged[1] = toupper(munged[1]);
540 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
541 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
543 alloc_symbol(symname +
544 strlen(MODULE_SYMBOL_PREFIX),
545 ELF_ST_BIND(sym->st_info) == STB_WEAK,
550 /* All exported symbols */
551 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
552 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
555 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
557 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
558 mod->has_cleanup = 1;
564 * Parse tag=value strings from .modinfo section
566 static char *next_string(char *string, unsigned long *secsize)
568 /* Skip non-zero chars */
571 if ((*secsize)-- <= 1)
575 /* Skip any zero padding. */
578 if ((*secsize)-- <= 1)
584 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
585 const char *tag, char *info)
588 unsigned int taglen = strlen(tag);
589 unsigned long size = modinfo_len;
592 size -= info - (char *)modinfo;
593 modinfo = next_string(info, &size);
596 for (p = modinfo; p; p = next_string(p, &size)) {
597 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
598 return p + taglen + 1;
603 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
607 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
611 * Test if string s ends in string sub
614 static int strrcmp(const char *s, const char *sub)
622 sublen = strlen(sub);
624 if ((slen == 0) || (sublen == 0))
630 return memcmp(s + slen - sublen, sub, sublen);
633 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
636 return elf->strtab + sym->st_name;
641 static const char *sec_name(struct elf_info *elf, int shndx)
643 Elf_Shdr *sechdrs = elf->sechdrs;
644 return (void *)elf->hdr +
645 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
646 sechdrs[shndx].sh_name;
649 static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
651 return (void *)elf->hdr +
652 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
656 /* if sym is empty or point to a string
657 * like ".[0-9]+" then return 1.
658 * This is the optional prefix added by ld to some sections
660 static int number_prefix(const char *sym)
668 if (c < '0' || c > '9')
674 /* The pattern is an array of simple patterns.
675 * "foo" will match an exact string equal to "foo"
676 * "*foo" will match a string that ends with "foo"
677 * "foo*" will match a string that begins with "foo"
678 * "foo$" will match a string equal to "foo" or "foo.1"
679 * where the '1' can be any number including several digits.
680 * The $ syntax is for sections where ld append a dot number
681 * to make section name unique.
683 int match(const char *sym, const char * const pat[])
688 const char *endp = p + strlen(p) - 1;
692 if (strrcmp(sym, p + 1) == 0)
696 else if (*endp == '*') {
697 if (strncmp(sym, p, strlen(p) - 1) == 0)
701 else if (*endp == '$') {
702 if (strncmp(sym, p, strlen(p) - 1) == 0) {
703 if (number_prefix(sym + strlen(p) - 1))
709 if (strcmp(p, sym) == 0)
717 /* sections that we do not want to do full section mismatch check on */
718 static const char *section_white_list[] =
719 { ".comment", ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
722 * This is used to find sections missing the SHF_ALLOC flag.
723 * The cause of this is often a section specified in assembler
724 * without "ax" / "aw".
726 static void check_section(const char *modname, struct elf_info *elf,
729 const char *sec = sech_name(elf, sechdr);
731 if (sechdr->sh_type == SHT_PROGBITS &&
732 !(sechdr->sh_flags & SHF_ALLOC) &&
733 !match(sec, section_white_list)) {
734 warn("%s (%s): unexpected non-allocatable section.\n"
735 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
736 "Note that for example <linux/init.h> contains\n"
737 "section definitions for use in .S files.\n\n",
744 #define ALL_INIT_DATA_SECTIONS \
745 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
746 #define ALL_EXIT_DATA_SECTIONS \
747 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
749 #define ALL_INIT_TEXT_SECTIONS \
750 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
751 #define ALL_EXIT_TEXT_SECTIONS \
752 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
754 #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
755 #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
757 #define DATA_SECTIONS ".data$", ".data.rel$"
758 #define TEXT_SECTIONS ".text$"
760 #define INIT_SECTIONS ".init.data$", ".init.text$"
761 #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
762 #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
763 #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
765 #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
766 #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
767 #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
768 #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
770 /* init data sections */
771 static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
773 /* all init sections */
774 static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
776 /* All init and exit sections (code + data) */
777 static const char *init_exit_sections[] =
778 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
781 static const char *data_sections[] = { DATA_SECTIONS, NULL };
784 /* symbols in .data that may refer to init/exit sections */
785 static const char *symbol_white_list[] =
788 "*_template", /* scsi uses *_template a lot */
789 "*_timer", /* arm uses ops structures named _timer a lot */
790 "*_sht", /* scsi also used *_sht to some extent */
798 static const char *head_sections[] = { ".head.text*", NULL };
799 static const char *linker_symbols[] =
800 { "__init_begin", "_sinittext", "_einittext", NULL };
815 struct sectioncheck {
816 const char *fromsec[20];
817 const char *tosec[20];
818 enum mismatch mismatch;
821 const struct sectioncheck sectioncheck[] = {
822 /* Do not reference init/exit code/data from
823 * normal code and data
826 .fromsec = { TEXT_SECTIONS, NULL },
827 .tosec = { ALL_INIT_SECTIONS, NULL },
828 .mismatch = TEXT_TO_INIT,
831 .fromsec = { DATA_SECTIONS, NULL },
832 .tosec = { ALL_INIT_SECTIONS, NULL },
833 .mismatch = DATA_TO_INIT,
836 .fromsec = { TEXT_SECTIONS, NULL },
837 .tosec = { ALL_EXIT_SECTIONS, NULL },
838 .mismatch = TEXT_TO_EXIT,
841 .fromsec = { DATA_SECTIONS, NULL },
842 .tosec = { ALL_EXIT_SECTIONS, NULL },
843 .mismatch = DATA_TO_EXIT,
845 /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
847 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
848 .tosec = { INIT_SECTIONS, NULL },
849 .mismatch = XXXINIT_TO_INIT,
851 /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
853 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
854 .tosec = { EXIT_SECTIONS, NULL },
855 .mismatch = XXXEXIT_TO_EXIT,
857 /* Do not use exit code/data from init code */
859 .fromsec = { ALL_INIT_SECTIONS, NULL },
860 .tosec = { ALL_EXIT_SECTIONS, NULL },
861 .mismatch = INIT_TO_EXIT,
863 /* Do not use init code/data from exit code */
865 .fromsec = { ALL_EXIT_SECTIONS, NULL },
866 .tosec = { ALL_INIT_SECTIONS, NULL },
867 .mismatch = EXIT_TO_INIT,
869 /* Do not export init/exit functions or data */
871 .fromsec = { "__ksymtab*", NULL },
872 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
873 .mismatch = EXPORT_TO_INIT_EXIT
877 static int section_mismatch(const char *fromsec, const char *tosec)
880 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
881 const struct sectioncheck *check = §ioncheck[0];
883 for (i = 0; i < elems; i++) {
884 if (match(fromsec, check->fromsec) &&
885 match(tosec, check->tosec))
886 return check->mismatch;
893 * Whitelist to allow certain references to pass with no warning.
896 * If a module parameter is declared __initdata and permissions=0
897 * then this is legal despite the warning generated.
898 * We cannot see value of permissions here, so just ignore
900 * The pattern is identified by:
906 * Many drivers utilise a *driver container with references to
907 * add, remove, probe functions etc.
908 * These functions may often be marked __init and we do not want to
910 * the pattern is identified by:
911 * tosec = init or exit section
912 * fromsec = data section
913 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
914 * *probe_one, *_console, *_timer
917 * Whitelist all references from .head.text to any init section
920 * Some symbols belong to init section but still it is ok to reference
921 * these from non-init sections as these symbols don't have any memory
922 * allocated for them and symbol address and value are same. So even
923 * if init section is freed, its ok to reference those symbols.
924 * For ex. symbols marking the init section boundaries.
925 * This pattern is identified by
926 * refsymname = __init_begin, _sinittext, _einittext
929 static int secref_whitelist(const char *fromsec, const char *fromsym,
930 const char *tosec, const char *tosym)
932 /* Check for pattern 1 */
933 if (match(tosec, init_data_sections) &&
934 match(fromsec, data_sections) &&
935 (strncmp(fromsym, "__param", strlen("__param")) == 0))
938 /* Check for pattern 2 */
939 if (match(tosec, init_exit_sections) &&
940 match(fromsec, data_sections) &&
941 match(fromsym, symbol_white_list))
944 /* Check for pattern 3 */
945 if (match(fromsec, head_sections) &&
946 match(tosec, init_sections))
949 /* Check for pattern 4 */
950 if (match(tosym, linker_symbols))
957 * Find symbol based on relocation record info.
958 * In some cases the symbol supplied is a valid symbol so
959 * return refsym. If st_name != 0 we assume this is a valid symbol.
960 * In other cases the symbol needs to be looked up in the symbol table
961 * based on section and address.
963 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
967 Elf_Sym *near = NULL;
968 Elf64_Sword distance = 20;
971 if (relsym->st_name != 0)
973 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
974 if (sym->st_shndx != relsym->st_shndx)
976 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
978 if (sym->st_value == addr)
980 /* Find a symbol nearby - addr are maybe negative */
981 d = sym->st_value - addr;
983 d = addr - sym->st_value;
989 /* We need a close match */
996 static inline int is_arm_mapping_symbol(const char *str)
998 return str[0] == '$' && strchr("atd", str[1])
999 && (str[2] == '\0' || str[2] == '.');
1003 * If there's no name there, ignore it; likewise, ignore it if it's
1004 * one of the magic symbols emitted used by current ARM tools.
1006 * Otherwise if find_symbols_between() returns those symbols, they'll
1007 * fail the whitelist tests and cause lots of false alarms ... fixable
1008 * only by merging __exit and __init sections into __text, bloating
1009 * the kernel (which is especially evil on embedded platforms).
1011 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1013 const char *name = elf->strtab + sym->st_name;
1015 if (!name || !strlen(name))
1017 return !is_arm_mapping_symbol(name);
1021 * Find symbols before or equal addr and after addr - in the section sec.
1022 * If we find two symbols with equal offset prefer one with a valid name.
1023 * The ELF format may have a better way to detect what type of symbol
1024 * it is, but this works for now.
1026 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1030 Elf_Sym *near = NULL;
1031 Elf_Addr distance = ~0;
1033 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1036 if (sym->st_shndx >= SHN_LORESERVE)
1038 symsec = sec_name(elf, sym->st_shndx);
1039 if (strcmp(symsec, sec) != 0)
1041 if (!is_valid_name(elf, sym))
1043 if (sym->st_value <= addr) {
1044 if ((addr - sym->st_value) < distance) {
1045 distance = addr - sym->st_value;
1047 } else if ((addr - sym->st_value) == distance) {
1056 * Convert a section name to the function/data attribute
1057 * .init.text => __init
1058 * .cpuinit.data => __cpudata
1059 * .memexitconst => __memconst
1062 static char *sec2annotation(const char *s)
1064 if (match(s, init_exit_sections)) {
1065 char *p = malloc(20);
1072 while (*s && *s != '.')
1077 if (strstr(s, "rodata") != NULL)
1078 strcat(p, "const ");
1079 else if (strstr(s, "data") != NULL)
1083 return r; /* we leak her but we do not care */
1089 static int is_function(Elf_Sym *sym)
1092 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1098 * Print a warning about a section mismatch.
1099 * Try to find symbols near it so user can find it.
1100 * Check whitelist before warning - it may be a false positive.
1102 static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
1103 const char *fromsec,
1104 unsigned long long fromaddr,
1105 const char *fromsym,
1107 const char *tosec, const char *tosym,
1110 const char *from, *from_p;
1111 const char *to, *to_p;
1113 switch (from_is_func) {
1114 case 0: from = "variable"; from_p = ""; break;
1115 case 1: from = "function"; from_p = "()"; break;
1116 default: from = "(unknown reference)"; from_p = ""; break;
1118 switch (to_is_func) {
1119 case 0: to = "variable"; to_p = ""; break;
1120 case 1: to = "function"; to_p = "()"; break;
1121 default: to = "(unknown reference)"; to_p = ""; break;
1124 sec_mismatch_count++;
1125 if (!sec_mismatch_verbose)
1128 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1129 "to the %s %s:%s%s\n",
1130 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1136 "The function %s%s() references\n"
1138 "This is often because %s lacks a %s\n"
1139 "annotation or the annotation of %s is wrong.\n",
1140 sec2annotation(fromsec), fromsym,
1141 to, sec2annotation(tosec), tosym, to_p,
1142 fromsym, sec2annotation(tosec), tosym);
1144 case DATA_TO_INIT: {
1145 const char **s = symbol_white_list;
1147 "The variable %s references\n"
1149 "If the reference is valid then annotate the\n"
1150 "variable with __init* (see linux/init.h) "
1151 "or name the variable:\n",
1152 fromsym, to, sec2annotation(tosec), tosym, to_p);
1154 fprintf(stderr, "%s, ", *s++);
1155 fprintf(stderr, "\n");
1160 "The function %s() references a %s in an exit section.\n"
1161 "Often the %s %s%s has valid usage outside the exit section\n"
1162 "and the fix is to remove the %sannotation of %s.\n",
1163 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1165 case DATA_TO_EXIT: {
1166 const char **s = symbol_white_list;
1168 "The variable %s references\n"
1170 "If the reference is valid then annotate the\n"
1171 "variable with __exit* (see linux/init.h) or "
1172 "name the variable:\n",
1173 fromsym, to, sec2annotation(tosec), tosym, to_p);
1175 fprintf(stderr, "%s, ", *s++);
1176 fprintf(stderr, "\n");
1179 case XXXINIT_TO_INIT:
1180 case XXXEXIT_TO_EXIT:
1182 "The %s %s%s%s references\n"
1184 "If %s is only used by %s then\n"
1185 "annotate %s with a matching annotation.\n",
1186 from, sec2annotation(fromsec), fromsym, from_p,
1187 to, sec2annotation(tosec), tosym, to_p,
1188 tosym, fromsym, tosym);
1192 "The %s %s%s%s references\n"
1194 "This is often seen when error handling "
1195 "in the init function\n"
1196 "uses functionality in the exit path.\n"
1197 "The fix is often to remove the %sannotation of\n"
1198 "%s%s so it may be used outside an exit section.\n",
1199 from, sec2annotation(fromsec), fromsym, from_p,
1200 to, sec2annotation(tosec), tosym, to_p,
1201 sec2annotation(tosec), tosym, to_p);
1205 "The %s %s%s%s references\n"
1207 "This is often seen when error handling "
1208 "in the exit function\n"
1209 "uses functionality in the init path.\n"
1210 "The fix is often to remove the %sannotation of\n"
1211 "%s%s so it may be used outside an init section.\n",
1212 from, sec2annotation(fromsec), fromsym, from_p,
1213 to, sec2annotation(tosec), tosym, to_p,
1214 sec2annotation(tosec), tosym, to_p);
1216 case EXPORT_TO_INIT_EXIT:
1218 "The symbol %s is exported and annotated %s\n"
1219 "Fix this by removing the %sannotation of %s "
1220 "or drop the export.\n",
1221 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1223 /* To get warnings on missing members */
1226 fprintf(stderr, "\n");
1229 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1230 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1233 enum mismatch mismatch;
1235 tosec = sec_name(elf, sym->st_shndx);
1236 mismatch = section_mismatch(fromsec, tosec);
1237 if (mismatch != NO_MISMATCH) {
1241 const char *fromsym;
1243 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1244 fromsym = sym_name(elf, from);
1245 to = find_elf_symbol(elf, r->r_addend, sym);
1246 tosym = sym_name(elf, to);
1248 /* check whitelist - we may ignore it */
1249 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
1250 report_sec_mismatch(modname, mismatch,
1251 fromsec, r->r_offset, fromsym,
1252 is_function(from), tosec, tosym,
1258 static unsigned int *reloc_location(struct elf_info *elf,
1259 Elf_Shdr *sechdr, Elf_Rela *r)
1261 Elf_Shdr *sechdrs = elf->sechdrs;
1262 int section = sechdr->sh_info;
1264 return (void *)elf->hdr + sechdrs[section].sh_offset +
1265 (r->r_offset - sechdrs[section].sh_addr);
1268 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1270 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1271 unsigned int *location = reloc_location(elf, sechdr, r);
1275 r->r_addend = TO_NATIVE(*location);
1278 r->r_addend = TO_NATIVE(*location) + 4;
1279 /* For CONFIG_RELOCATABLE=y */
1280 if (elf->hdr->e_type == ET_EXEC)
1281 r->r_addend += r->r_offset;
1287 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1289 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1293 /* From ARM ABI: (S + A) | T */
1294 r->r_addend = (int)(long)
1295 (elf->symtab_start + ELF_R_SYM(r->r_info));
1298 /* From ARM ABI: ((S + A) | T) - P */
1299 r->r_addend = (int)(long)(elf->hdr +
1301 (r->r_offset - sechdr->sh_addr));
1309 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1311 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1312 unsigned int *location = reloc_location(elf, sechdr, r);
1315 if (r_typ == R_MIPS_HI16)
1316 return 1; /* skip this */
1317 inst = TO_NATIVE(*location);
1320 r->r_addend = inst & 0xffff;
1323 r->r_addend = (inst & 0x03ffffff) << 2;
1332 static void section_rela(const char *modname, struct elf_info *elf,
1339 const char *fromsec;
1341 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1342 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1344 fromsec = sech_name(elf, sechdr);
1345 fromsec += strlen(".rela");
1346 /* if from section (name) is know good then skip it */
1347 if (match(fromsec, section_white_list))
1350 for (rela = start; rela < stop; rela++) {
1351 r.r_offset = TO_NATIVE(rela->r_offset);
1352 #if KERNEL_ELFCLASS == ELFCLASS64
1353 if (elf->hdr->e_machine == EM_MIPS) {
1355 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1356 r_sym = TO_NATIVE(r_sym);
1357 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1358 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1360 r.r_info = TO_NATIVE(rela->r_info);
1361 r_sym = ELF_R_SYM(r.r_info);
1364 r.r_info = TO_NATIVE(rela->r_info);
1365 r_sym = ELF_R_SYM(r.r_info);
1367 r.r_addend = TO_NATIVE(rela->r_addend);
1368 sym = elf->symtab_start + r_sym;
1369 /* Skip special sections */
1370 if (sym->st_shndx >= SHN_LORESERVE)
1372 check_section_mismatch(modname, elf, &r, sym, fromsec);
1376 static void section_rel(const char *modname, struct elf_info *elf,
1383 const char *fromsec;
1385 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1386 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1388 fromsec = sech_name(elf, sechdr);
1389 fromsec += strlen(".rel");
1390 /* if from section (name) is know good then skip it */
1391 if (match(fromsec, section_white_list))
1394 for (rel = start; rel < stop; rel++) {
1395 r.r_offset = TO_NATIVE(rel->r_offset);
1396 #if KERNEL_ELFCLASS == ELFCLASS64
1397 if (elf->hdr->e_machine == EM_MIPS) {
1399 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1400 r_sym = TO_NATIVE(r_sym);
1401 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1402 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1404 r.r_info = TO_NATIVE(rel->r_info);
1405 r_sym = ELF_R_SYM(r.r_info);
1408 r.r_info = TO_NATIVE(rel->r_info);
1409 r_sym = ELF_R_SYM(r.r_info);
1412 switch (elf->hdr->e_machine) {
1414 if (addend_386_rel(elf, sechdr, &r))
1418 if (addend_arm_rel(elf, sechdr, &r))
1422 if (addend_mips_rel(elf, sechdr, &r))
1426 sym = elf->symtab_start + r_sym;
1427 /* Skip special sections */
1428 if (sym->st_shndx >= SHN_LORESERVE)
1430 check_section_mismatch(modname, elf, &r, sym, fromsec);
1435 * A module includes a number of sections that are discarded
1436 * either when loaded or when used as built-in.
1437 * For loaded modules all functions marked __init and all data
1438 * marked __initdata will be discarded when the module has been intialized.
1439 * Likewise for modules used built-in the sections marked __exit
1440 * are discarded because __exit marked function are supposed to be called
1441 * only when a module is unloaded which never happens for built-in modules.
1442 * The check_sec_ref() function traverses all relocation records
1443 * to find all references to a section that reference a section that will
1444 * be discarded and warns about it.
1446 static void check_sec_ref(struct module *mod, const char *modname,
1447 struct elf_info *elf)
1450 Elf_Shdr *sechdrs = elf->sechdrs;
1452 /* Walk through all sections */
1453 for (i = 0; i < elf->hdr->e_shnum; i++) {
1454 check_section(modname, elf, &elf->sechdrs[i]);
1455 /* We want to process only relocation sections and not .init */
1456 if (sechdrs[i].sh_type == SHT_RELA)
1457 section_rela(modname, elf, &elf->sechdrs[i]);
1458 else if (sechdrs[i].sh_type == SHT_REL)
1459 section_rel(modname, elf, &elf->sechdrs[i]);
1463 static void get_markers(struct elf_info *info, struct module *mod)
1465 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1466 const char *strings = (const char *) info->hdr + sh->sh_offset;
1467 const Elf_Sym *sym, *first_sym, *last_sym;
1470 if (!info->markers_strings_sec)
1474 * First count the strings. We look for all the symbols defined
1475 * in the __markers_strings section named __mstrtab_*. For
1476 * these local names, the compiler puts a random .NNN suffix on,
1477 * so the names don't correspond exactly.
1479 first_sym = last_sym = NULL;
1481 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1482 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1483 sym->st_shndx == info->markers_strings_sec &&
1484 !strncmp(info->strtab + sym->st_name,
1485 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1486 if (first_sym == NULL)
1496 * Now collect each name and format into a line for the output.
1498 * marker_name vmlinux marker %s format %d
1499 * The format string after the second \t can use whitespace.
1501 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1505 for (sym = first_sym; sym <= last_sym; sym++)
1506 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1507 sym->st_shndx == info->markers_strings_sec &&
1508 !strncmp(info->strtab + sym->st_name,
1509 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1510 const char *name = strings + sym->st_value;
1511 const char *fmt = strchr(name, '\0') + 1;
1513 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1515 mod->markers[n++] = line;
1519 static void read_symbols(char *modname)
1521 const char *symname;
1525 struct elf_info info = { };
1528 if (!parse_elf(&info, modname))
1531 mod = new_module(modname);
1533 /* When there's no vmlinux, don't print warnings about
1534 * unresolved symbols (since there'll be too many ;) */
1535 if (is_vmlinux(modname)) {
1540 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1541 if (info.modinfo && !license && !is_vmlinux(modname))
1542 warn("modpost: missing MODULE_LICENSE() in %s\n"
1543 "see include/linux/module.h for "
1544 "more information\n", modname);
1546 if (license_is_gpl_compatible(license))
1547 mod->gpl_compatible = 1;
1549 mod->gpl_compatible = 0;
1552 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1553 "license", license);
1556 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1557 symname = info.strtab + sym->st_name;
1559 handle_modversions(mod, &info, sym, symname);
1560 handle_moddevtable(mod, &info, sym, symname);
1562 if (!is_vmlinux(modname) ||
1563 (is_vmlinux(modname) && vmlinux_section_warnings))
1564 check_sec_ref(mod, modname, &info);
1566 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1568 maybe_frob_rcs_version(modname, version, info.modinfo,
1569 version - (char *)info.hdr);
1570 if (version || (all_versions && !is_vmlinux(modname)))
1571 get_src_version(modname, mod->srcversion,
1572 sizeof(mod->srcversion)-1);
1574 get_markers(&info, mod);
1576 parse_elf_finish(&info);
1578 /* Our trick to get versioning for module struct etc. - it's
1579 * never passed as an argument to an exported function, so
1580 * the automatic versioning doesn't pick it up, but it's really
1581 * important anyhow */
1583 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
1588 /* We first write the generated file into memory using the
1589 * following helper, then compare to the file on disk and
1590 * only update the later if anything changed */
1592 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1593 const char *fmt, ...)
1600 len = vsnprintf(tmp, SZ, fmt, ap);
1601 buf_write(buf, tmp, len);
1605 void buf_write(struct buffer *buf, const char *s, int len)
1607 if (buf->size - buf->pos < len) {
1608 buf->size += len + SZ;
1609 buf->p = realloc(buf->p, buf->size);
1611 strncpy(buf->p + buf->pos, s, len);
1615 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1617 const char *e = is_vmlinux(m) ?"":".ko";
1621 fatal("modpost: GPL-incompatible module %s%s "
1622 "uses GPL-only symbol '%s'\n", m, e, s);
1624 case export_unused_gpl:
1625 fatal("modpost: GPL-incompatible module %s%s "
1626 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1628 case export_gpl_future:
1629 warn("modpost: GPL-incompatible module %s%s "
1630 "uses future GPL-only symbol '%s'\n", m, e, s);
1634 case export_unknown:
1640 static void check_for_unused(enum export exp, const char *m, const char *s)
1642 const char *e = is_vmlinux(m) ?"":".ko";
1646 case export_unused_gpl:
1647 warn("modpost: module %s%s "
1648 "uses symbol '%s' marked UNUSED\n", m, e, s);
1656 static void check_exports(struct module *mod)
1658 struct symbol *s, *exp;
1660 for (s = mod->unres; s; s = s->next) {
1661 const char *basename;
1662 exp = find_symbol(s->name);
1663 if (!exp || exp->module == mod)
1665 basename = strrchr(mod->name, '/');
1669 basename = mod->name;
1670 if (!mod->gpl_compatible)
1671 check_for_gpl_usage(exp->export, basename, exp->name);
1672 check_for_unused(exp->export, basename, exp->name);
1677 * Header for the generated file
1679 static void add_header(struct buffer *b, struct module *mod)
1681 buf_printf(b, "#include <linux/module.h>\n");
1682 buf_printf(b, "#include <linux/vermagic.h>\n");
1683 buf_printf(b, "#include <linux/compiler.h>\n");
1684 buf_printf(b, "\n");
1685 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1686 buf_printf(b, "\n");
1687 buf_printf(b, "struct module __this_module\n");
1688 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1689 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1691 buf_printf(b, " .init = init_module,\n");
1692 if (mod->has_cleanup)
1693 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1694 " .exit = cleanup_module,\n"
1696 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1697 buf_printf(b, "};\n");
1700 void add_staging_flag(struct buffer *b, const char *name)
1702 static const char *staging_dir = "drivers/staging";
1704 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1705 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1709 * Record CRCs for unresolved symbols
1711 static int add_versions(struct buffer *b, struct module *mod)
1713 struct symbol *s, *exp;
1716 for (s = mod->unres; s; s = s->next) {
1717 exp = find_symbol(s->name);
1718 if (!exp || exp->module == mod) {
1719 if (have_vmlinux && !s->weak) {
1720 if (warn_unresolved) {
1721 warn("\"%s\" [%s.ko] undefined!\n",
1722 s->name, mod->name);
1724 merror("\"%s\" [%s.ko] undefined!\n",
1725 s->name, mod->name);
1731 s->module = exp->module;
1732 s->crc_valid = exp->crc_valid;
1739 buf_printf(b, "\n");
1740 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1741 buf_printf(b, "__used\n");
1742 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1744 for (s = mod->unres; s; s = s->next) {
1747 if (!s->crc_valid) {
1748 warn("\"%s\" [%s.ko] has no CRC!\n",
1749 s->name, mod->name);
1752 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1755 buf_printf(b, "};\n");
1760 static void add_depends(struct buffer *b, struct module *mod,
1761 struct module *modules)
1767 for (m = modules; m; m = m->next)
1768 m->seen = is_vmlinux(m->name);
1770 buf_printf(b, "\n");
1771 buf_printf(b, "static const char __module_depends[]\n");
1772 buf_printf(b, "__used\n");
1773 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1774 buf_printf(b, "\"depends=");
1775 for (s = mod->unres; s; s = s->next) {
1780 if (s->module->seen)
1783 s->module->seen = 1;
1784 p = strrchr(s->module->name, '/');
1788 p = s->module->name;
1789 buf_printf(b, "%s%s", first ? "" : ",", p);
1792 buf_printf(b, "\";\n");
1795 static void add_srcversion(struct buffer *b, struct module *mod)
1797 if (mod->srcversion[0]) {
1798 buf_printf(b, "\n");
1799 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1804 static void write_if_changed(struct buffer *b, const char *fname)
1810 file = fopen(fname, "r");
1814 if (fstat(fileno(file), &st) < 0)
1817 if (st.st_size != b->pos)
1820 tmp = NOFAIL(malloc(b->pos));
1821 if (fread(tmp, 1, b->pos, file) != b->pos)
1824 if (memcmp(tmp, b->p, b->pos) != 0)
1836 file = fopen(fname, "w");
1841 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1848 /* parse Module.symvers file. line format:
1849 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1851 static void read_dump(const char *fname, unsigned int kernel)
1853 unsigned long size, pos = 0;
1854 void *file = grab_file(fname, &size);
1858 /* No symbol versions, silently ignore */
1861 while ((line = get_next_line(&pos, file, size))) {
1862 char *symname, *modname, *d, *export, *end;
1867 if (!(symname = strchr(line, '\t')))
1870 if (!(modname = strchr(symname, '\t')))
1873 if ((export = strchr(modname, '\t')) != NULL)
1875 if (export && ((end = strchr(export, '\t')) != NULL))
1877 crc = strtoul(line, &d, 16);
1878 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1880 mod = find_module(modname);
1882 if (is_vmlinux(modname))
1884 mod = new_module(modname);
1887 s = sym_add_exported(symname, mod, export_no(export));
1890 sym_update_crc(symname, mod, crc, export_no(export));
1894 fatal("parse error in symbol dump file\n");
1897 /* For normal builds always dump all symbols.
1898 * For external modules only dump symbols
1899 * that are not read from kernel Module.symvers.
1901 static int dump_sym(struct symbol *sym)
1903 if (!external_module)
1905 if (sym->vmlinux || sym->kernel)
1910 static void write_dump(const char *fname)
1912 struct buffer buf = { };
1913 struct symbol *symbol;
1916 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1917 symbol = symbolhash[n];
1919 if (dump_sym(symbol))
1920 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1921 symbol->crc, symbol->name,
1922 symbol->module->name,
1923 export_str(symbol->export));
1924 symbol = symbol->next;
1927 write_if_changed(&buf, fname);
1930 static void add_marker(struct module *mod, const char *name, const char *fmt)
1933 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1936 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1937 sizeof mod->markers[0])));
1938 mod->markers[mod->nmarkers++] = line;
1941 static void read_markers(const char *fname)
1943 unsigned long size, pos = 0;
1944 void *file = grab_file(fname, &size);
1947 if (!file) /* No old markers, silently ignore */
1950 while ((line = get_next_line(&pos, file, size))) {
1951 char *marker, *modname, *fmt;
1955 modname = strchr(marker, '\t');
1959 fmt = strchr(modname, '\t');
1963 if (*marker == '\0' || *modname == '\0')
1966 mod = find_module(modname);
1968 mod = new_module(modname);
1971 if (is_vmlinux(modname)) {
1977 add_marker(mod, marker, fmt);
1979 release_file(file, size);
1982 fatal("parse error in markers list file\n");
1985 static int compare_strings(const void *a, const void *b)
1987 return strcmp(*(const char **) a, *(const char **) b);
1990 static void write_markers(const char *fname)
1992 struct buffer buf = { };
1996 for (mod = modules; mod; mod = mod->next)
1997 if ((!external_module || !mod->skip) && mod->markers != NULL) {
1999 * Sort the strings so we can skip duplicates when
2000 * we write them out.
2002 qsort(mod->markers, mod->nmarkers,
2003 sizeof mod->markers[0], &compare_strings);
2004 for (i = 0; i < mod->nmarkers; ++i) {
2005 char *line = mod->markers[i];
2006 buf_write(&buf, line, strlen(line));
2007 while (i + 1 < mod->nmarkers &&
2008 !strcmp(mod->markers[i],
2009 mod->markers[i + 1]))
2010 free(mod->markers[i++]);
2011 free(mod->markers[i]);
2014 mod->markers = NULL;
2017 write_if_changed(&buf, fname);
2020 struct ext_sym_list {
2021 struct ext_sym_list *next;
2025 int main(int argc, char **argv)
2028 struct buffer buf = { };
2029 char *kernel_read = NULL, *module_read = NULL;
2030 char *dump_write = NULL;
2031 char *markers_read = NULL;
2032 char *markers_write = NULL;
2035 struct ext_sym_list *extsym_iter;
2036 struct ext_sym_list *extsym_start = NULL;
2038 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
2041 kernel_read = optarg;
2044 module_read = optarg;
2045 external_module = 1;
2051 external_module = 1;
2053 NOFAIL(malloc(sizeof(*extsym_iter)));
2054 extsym_iter->next = extsym_start;
2055 extsym_iter->file = optarg;
2056 extsym_start = extsym_iter;
2062 dump_write = optarg;
2068 vmlinux_section_warnings = 0;
2071 sec_mismatch_verbose = 0;
2074 warn_unresolved = 1;
2077 markers_write = optarg;
2080 markers_read = optarg;
2088 read_dump(kernel_read, 1);
2090 read_dump(module_read, 0);
2091 while (extsym_start) {
2092 read_dump(extsym_start->file, 0);
2093 extsym_iter = extsym_start->next;
2095 extsym_start = extsym_iter;
2098 while (optind < argc)
2099 read_symbols(argv[optind++]);
2101 for (mod = modules; mod; mod = mod->next) {
2109 for (mod = modules; mod; mod = mod->next) {
2110 char fname[strlen(mod->name) + 10];
2117 add_header(&buf, mod);
2118 add_staging_flag(&buf, mod->name);
2119 err |= add_versions(&buf, mod);
2120 add_depends(&buf, mod, modules);
2121 add_moddevtable(&buf, mod);
2122 add_srcversion(&buf, mod);
2124 sprintf(fname, "%s.mod.c", mod->name);
2125 write_if_changed(&buf, fname);
2129 write_dump(dump_write);
2130 if (sec_mismatch_count && !sec_mismatch_verbose)
2131 warn("modpost: Found %d section mismatch(es).\n"
2132 "To see full details build your kernel with:\n"
2133 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2134 sec_mismatch_count);
2137 read_markers(markers_read);
2140 write_markers(markers_write);