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 */
28 export_plain, export_unused, export_gpl,
29 export_unused_gpl, export_gpl_future, export_unknown
32 void fatal(const char *fmt, ...)
36 fprintf(stderr, "FATAL: ");
38 va_start(arglist, fmt);
39 vfprintf(stderr, fmt, arglist);
45 void warn(const char *fmt, ...)
49 fprintf(stderr, "WARNING: ");
51 va_start(arglist, fmt);
52 vfprintf(stderr, fmt, arglist);
56 static int is_vmlinux(const char *modname)
60 if ((myname = strrchr(modname, '/')))
65 return strcmp(myname, "vmlinux") == 0;
68 void *do_nofail(void *ptr, const char *expr)
71 fatal("modpost: Memory allocation failure: %s.\n", expr);
76 /* A list of all modules we processed */
78 static struct module *modules;
80 static struct module *find_module(char *modname)
84 for (mod = modules; mod; mod = mod->next)
85 if (strcmp(mod->name, modname) == 0)
90 static struct module *new_module(char *modname)
95 mod = NOFAIL(malloc(sizeof(*mod)));
96 memset(mod, 0, sizeof(*mod));
97 p = NOFAIL(strdup(modname));
99 /* strip trailing .o */
100 if ((s = strrchr(p, '.')) != NULL)
101 if (strcmp(s, ".o") == 0)
106 mod->gpl_compatible = -1;
113 /* A hash of all exported symbols,
114 * struct symbol is also used for lists of unresolved symbols */
116 #define SYMBOL_HASH_SIZE 1024
120 struct module *module;
124 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
125 unsigned int kernel:1; /* 1 if symbol is from kernel
126 * (only for external modules) **/
127 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
128 enum export export; /* Type of export */
132 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
134 /* This is based on the hash agorithm from gdbm, via tdb */
135 static inline unsigned int tdb_hash(const char *name)
137 unsigned value; /* Used to compute the hash value. */
138 unsigned i; /* Used to cycle through random values. */
140 /* Set the initial value from the key size. */
141 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
142 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
144 return (1103515243 * value + 12345);
148 * Allocate a new symbols for use in the hash of exported symbols or
149 * the list of unresolved symbols per module
151 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
154 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
156 memset(s, 0, sizeof(*s));
157 strcpy(s->name, name);
163 /* For the hash of exported symbols */
164 static struct symbol *new_symbol(const char *name, struct module *module,
170 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
171 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
172 new->module = module;
173 new->export = export;
177 static struct symbol *find_symbol(const char *name)
181 /* For our purposes, .foo matches foo. PPC64 needs this. */
185 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
186 if (strcmp(s->name, name) == 0)
196 { .str = "EXPORT_SYMBOL", .export = export_plain },
197 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
198 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
199 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
200 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
201 { .str = "(unknown)", .export = export_unknown },
205 static const char *export_str(enum export ex)
207 return export_list[ex].str;
210 static enum export export_no(const char * s)
214 return export_unknown;
215 for (i = 0; export_list[i].export != export_unknown; i++) {
216 if (strcmp(export_list[i].str, s) == 0)
217 return export_list[i].export;
219 return export_unknown;
222 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
224 if (sec == elf->export_sec)
226 else if (sec == elf->export_unused_sec)
227 return export_unused;
228 else if (sec == elf->export_gpl_sec)
230 else if (sec == elf->export_unused_gpl_sec)
231 return export_unused_gpl;
232 else if (sec == elf->export_gpl_future_sec)
233 return export_gpl_future;
235 return export_unknown;
239 * Add an exported symbol - it may have already been added without a
240 * CRC, in this case just update the CRC
242 static struct symbol *sym_add_exported(const char *name, struct module *mod,
245 struct symbol *s = find_symbol(name);
248 s = new_symbol(name, mod, export);
251 warn("%s: '%s' exported twice. Previous export "
252 "was in %s%s\n", mod->name, name,
254 is_vmlinux(s->module->name) ?"":".ko");
258 s->vmlinux = is_vmlinux(mod->name);
264 static void sym_update_crc(const char *name, struct module *mod,
265 unsigned int crc, enum export export)
267 struct symbol *s = find_symbol(name);
270 s = new_symbol(name, mod, export);
275 void *grab_file(const char *filename, unsigned long *size)
281 fd = open(filename, O_RDONLY);
282 if (fd < 0 || fstat(fd, &st) != 0)
286 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
289 if (map == MAP_FAILED)
295 * Return a copy of the next line in a mmap'ed file.
296 * spaces in the beginning of the line is trimmed away.
297 * Return a pointer to a static buffer.
299 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
301 static char line[4096];
304 signed char *p = (signed char *)file + *pos;
307 for (; *pos < size ; (*pos)++)
309 if (skip && isspace(*p)) {
314 if (*p != '\n' && (*pos < size)) {
318 break; /* Too long, stop */
329 void release_file(void *file, unsigned long size)
334 static void parse_elf(struct elf_info *info, const char *filename)
337 Elf_Ehdr *hdr = info->hdr;
341 hdr = grab_file(filename, &info->size);
347 if (info->size < sizeof(*hdr))
350 /* Fix endianness in ELF header */
351 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
352 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
353 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
354 hdr->e_machine = TO_NATIVE(hdr->e_machine);
355 sechdrs = (void *)hdr + hdr->e_shoff;
356 info->sechdrs = sechdrs;
358 /* Fix endianness in section headers */
359 for (i = 0; i < hdr->e_shnum; i++) {
360 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
361 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
362 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
363 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
364 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
366 /* Find symbol table. */
367 for (i = 1; i < hdr->e_shnum; i++) {
368 const char *secstrings
369 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
372 if (sechdrs[i].sh_offset > info->size)
374 secname = secstrings + sechdrs[i].sh_name;
375 if (strcmp(secname, ".modinfo") == 0) {
376 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
377 info->modinfo_len = sechdrs[i].sh_size;
378 } else if (strcmp(secname, "__ksymtab") == 0)
379 info->export_sec = i;
380 else if (strcmp(secname, "__ksymtab_unused") == 0)
381 info->export_unused_sec = i;
382 else if (strcmp(secname, "__ksymtab_gpl") == 0)
383 info->export_gpl_sec = i;
384 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
385 info->export_unused_gpl_sec = i;
386 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
387 info->export_gpl_future_sec = i;
389 if (sechdrs[i].sh_type != SHT_SYMTAB)
392 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
393 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
394 + sechdrs[i].sh_size;
395 info->strtab = (void *)hdr +
396 sechdrs[sechdrs[i].sh_link].sh_offset;
398 if (!info->symtab_start) {
399 fatal("%s has no symtab?\n", filename);
401 /* Fix endianness in symbols */
402 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
403 sym->st_shndx = TO_NATIVE(sym->st_shndx);
404 sym->st_name = TO_NATIVE(sym->st_name);
405 sym->st_value = TO_NATIVE(sym->st_value);
406 sym->st_size = TO_NATIVE(sym->st_size);
411 fatal("%s is truncated.\n", filename);
414 static void parse_elf_finish(struct elf_info *info)
416 release_file(info->hdr, info->size);
419 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
420 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
422 static void handle_modversions(struct module *mod, struct elf_info *info,
423 Elf_Sym *sym, const char *symname)
426 enum export export = export_from_sec(info, sym->st_shndx);
428 switch (sym->st_shndx) {
430 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
434 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
435 crc = (unsigned int) sym->st_value;
436 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
441 /* undefined symbol */
442 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
443 ELF_ST_BIND(sym->st_info) != STB_WEAK)
445 /* ignore global offset table */
446 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
448 /* ignore __this_module, it will be resolved shortly */
449 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
451 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
452 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
453 /* add compatibility with older glibc */
454 #ifndef STT_SPARC_REGISTER
455 #define STT_SPARC_REGISTER STT_REGISTER
457 if (info->hdr->e_machine == EM_SPARC ||
458 info->hdr->e_machine == EM_SPARCV9) {
459 /* Ignore register directives. */
460 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
462 if (symname[0] == '.') {
463 char *munged = strdup(symname);
465 munged[1] = toupper(munged[1]);
471 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
472 strlen(MODULE_SYMBOL_PREFIX)) == 0)
473 mod->unres = alloc_symbol(symname +
474 strlen(MODULE_SYMBOL_PREFIX),
475 ELF_ST_BIND(sym->st_info) == STB_WEAK,
479 /* All exported symbols */
480 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
481 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
484 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
486 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
487 mod->has_cleanup = 1;
493 * Parse tag=value strings from .modinfo section
495 static char *next_string(char *string, unsigned long *secsize)
497 /* Skip non-zero chars */
500 if ((*secsize)-- <= 1)
504 /* Skip any zero padding. */
507 if ((*secsize)-- <= 1)
513 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
514 const char *tag, char *info)
517 unsigned int taglen = strlen(tag);
518 unsigned long size = modinfo_len;
521 size -= info - (char *)modinfo;
522 modinfo = next_string(info, &size);
525 for (p = modinfo; p; p = next_string(p, &size)) {
526 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
527 return p + taglen + 1;
532 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
536 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
540 * Test if string s ends in string sub
543 static int strrcmp(const char *s, const char *sub)
551 sublen = strlen(sub);
553 if ((slen == 0) || (sublen == 0))
559 return memcmp(s + slen - sublen, sub, sublen);
563 * Whitelist to allow certain references to pass with no warning.
565 * If a module parameter is declared __initdata and permissions=0
566 * then this is legal despite the warning generated.
567 * We cannot see value of permissions here, so just ignore
569 * The pattern is identified by:
575 * Many drivers utilise a *driver container with references to
576 * add, remove, probe functions etc.
577 * These functions may often be marked __init and we do not want to
579 * the pattern is identified by:
580 * tosec = .init.text | .exit.text | .init.data
582 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
584 static int secref_whitelist(const char *tosec, const char *fromsec,
589 const char *pat2sym[] = {
591 "_template", /* scsi uses *_template a lot */
592 "_sht", /* scsi also used *_sht to some extent */
599 /* Check for pattern 1 */
600 if (strcmp(tosec, ".init.data") != 0)
602 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
604 if (strncmp(atsym, "__param", strlen("__param")) != 0)
610 /* Check for pattern 2 */
611 if ((strcmp(tosec, ".init.text") != 0) &&
612 (strcmp(tosec, ".exit.text") != 0) &&
613 (strcmp(tosec, ".init.data") != 0))
615 if (strcmp(fromsec, ".data") != 0)
618 for (s = pat2sym; *s; s++)
619 if (strrcmp(atsym, *s) == 0)
626 * Find symbol based on relocation record info.
627 * In some cases the symbol supplied is a valid symbol so
628 * return refsym. If st_name != 0 we assume this is a valid symbol.
629 * In other cases the symbol needs to be looked up in the symbol table
630 * based on section and address.
632 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
637 if (relsym->st_name != 0)
639 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
640 if (sym->st_shndx != relsym->st_shndx)
642 if (sym->st_value == addr)
649 * Find symbols before or equal addr and after addr - in the section sec.
650 * If we find two symbols with equal offset prefer one with a valid name.
651 * The ELF format may have a better way to detect what type of symbol
652 * it is, but this works for now.
654 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
656 Elf_Sym **before, Elf_Sym **after)
659 Elf_Ehdr *hdr = elf->hdr;
660 Elf_Addr beforediff = ~0;
661 Elf_Addr afterdiff = ~0;
662 const char *secstrings = (void *)hdr +
663 elf->sechdrs[hdr->e_shstrndx].sh_offset;
668 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
671 if (sym->st_shndx >= SHN_LORESERVE)
673 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
674 if (strcmp(symsec, sec) != 0)
676 if (sym->st_value <= addr) {
677 if ((addr - sym->st_value) < beforediff) {
678 beforediff = addr - sym->st_value;
681 else if ((addr - sym->st_value) == beforediff) {
682 /* equal offset, valid name? */
683 const char *name = elf->strtab + sym->st_name;
684 if (name && strlen(name))
690 if ((sym->st_value - addr) < afterdiff) {
691 afterdiff = sym->st_value - addr;
694 else if ((sym->st_value - addr) == afterdiff) {
695 /* equal offset, valid name? */
696 const char *name = elf->strtab + sym->st_name;
697 if (name && strlen(name))
705 * Print a warning about a section mismatch.
706 * Try to find symbols near it so user can find it.
707 * Check whitelist before warning - it may be a false positive.
709 static void warn_sec_mismatch(const char *modname, const char *fromsec,
710 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
712 const char *refsymname = "";
713 Elf_Sym *before, *after;
715 Elf_Ehdr *hdr = elf->hdr;
716 Elf_Shdr *sechdrs = elf->sechdrs;
717 const char *secstrings = (void *)hdr +
718 sechdrs[hdr->e_shstrndx].sh_offset;
719 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
721 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
723 refsym = find_elf_symbol(elf, r.r_addend, sym);
724 if (refsym && strlen(elf->strtab + refsym->st_name))
725 refsymname = elf->strtab + refsym->st_name;
727 /* check whitelist - we may ignore it */
729 secref_whitelist(secname, fromsec, elf->strtab + before->st_name))
732 if (before && after) {
733 warn("%s - Section mismatch: reference to %s:%s from %s "
734 "between '%s' (at offset 0x%llx) and '%s'\n",
735 modname, secname, refsymname, fromsec,
736 elf->strtab + before->st_name,
737 (long long)r.r_offset,
738 elf->strtab + after->st_name);
740 warn("%s - Section mismatch: reference to %s:%s from %s "
741 "after '%s' (at offset 0x%llx)\n",
742 modname, secname, refsymname, fromsec,
743 elf->strtab + before->st_name,
744 (long long)r.r_offset);
746 warn("%s - Section mismatch: reference to %s:%s from %s "
747 "before '%s' (at offset -0x%llx)\n",
748 modname, secname, refsymname, fromsec,
749 elf->strtab + after->st_name,
750 (long long)r.r_offset);
752 warn("%s - Section mismatch: reference to %s:%s from %s "
754 modname, secname, fromsec, refsymname,
755 (long long)r.r_offset);
760 * A module includes a number of sections that are discarded
761 * either when loaded or when used as built-in.
762 * For loaded modules all functions marked __init and all data
763 * marked __initdata will be discarded when the module has been intialized.
764 * Likewise for modules used built-in the sections marked __exit
765 * are discarded because __exit marked function are supposed to be called
766 * only when a moduel is unloaded which never happes for built-in modules.
767 * The check_sec_ref() function traverses all relocation records
768 * to find all references to a section that reference a section that will
769 * be discarded and warns about it.
771 static void check_sec_ref(struct module *mod, const char *modname,
772 struct elf_info *elf,
773 int section(const char*),
774 int section_ref_ok(const char *))
778 Elf_Ehdr *hdr = elf->hdr;
779 Elf_Shdr *sechdrs = elf->sechdrs;
780 const char *secstrings = (void *)hdr +
781 sechdrs[hdr->e_shstrndx].sh_offset;
783 /* Walk through all sections */
784 for (i = 0; i < hdr->e_shnum; i++) {
785 const char *name = secstrings + sechdrs[i].sh_name;
789 /* We want to process only relocation sections and not .init */
790 if (sechdrs[i].sh_type == SHT_RELA) {
792 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
793 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
794 name += strlen(".rela");
795 if (section_ref_ok(name))
798 for (rela = start; rela < stop; rela++) {
799 r.r_offset = TO_NATIVE(rela->r_offset);
800 #if KERNEL_ELFCLASS == ELFCLASS64
801 if (hdr->e_machine == EM_MIPS) {
802 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
803 r_sym = TO_NATIVE(r_sym);
805 r.r_info = TO_NATIVE(rela->r_info);
806 r_sym = ELF_R_SYM(r.r_info);
809 r.r_info = TO_NATIVE(rela->r_info);
810 r_sym = ELF_R_SYM(r.r_info);
812 r.r_addend = TO_NATIVE(rela->r_addend);
813 sym = elf->symtab_start + r_sym;
814 /* Skip special sections */
815 if (sym->st_shndx >= SHN_LORESERVE)
818 secname = secstrings +
819 sechdrs[sym->st_shndx].sh_name;
820 if (section(secname))
821 warn_sec_mismatch(modname, name,
824 } else if (sechdrs[i].sh_type == SHT_REL) {
826 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
827 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
828 name += strlen(".rel");
829 if (section_ref_ok(name))
832 for (rel = start; rel < stop; rel++) {
833 r.r_offset = TO_NATIVE(rel->r_offset);
834 #if KERNEL_ELFCLASS == ELFCLASS64
835 if (hdr->e_machine == EM_MIPS) {
836 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
837 r_sym = TO_NATIVE(r_sym);
839 r.r_info = TO_NATIVE(rel->r_info);
840 r_sym = ELF_R_SYM(r.r_info);
843 r.r_info = TO_NATIVE(rel->r_info);
844 r_sym = ELF_R_SYM(r.r_info);
847 sym = elf->symtab_start + r_sym;
848 /* Skip special sections */
849 if (sym->st_shndx >= SHN_LORESERVE)
852 secname = secstrings +
853 sechdrs[sym->st_shndx].sh_name;
854 if (section(secname))
855 warn_sec_mismatch(modname, name,
863 * Functions used only during module init is marked __init and is stored in
864 * a .init.text section. Likewise data is marked __initdata and stored in
865 * a .init.data section.
866 * If this section is one of these sections return 1
867 * See include/linux/init.h for the details
869 static int init_section(const char *name)
871 if (strcmp(name, ".init") == 0)
873 if (strncmp(name, ".init.", strlen(".init.")) == 0)
879 * Identify sections from which references to a .init section is OK.
881 * Unfortunately references to read only data that referenced .init
882 * sections had to be excluded. Almost all of these are false
883 * positives, they are created by gcc. The downside of excluding rodata
884 * is that there really are some user references from rodata to
885 * init code, e.g. drivers/video/vgacon.c:
887 * const struct consw vga_con = {
888 * con_startup: vgacon_startup,
890 * where vgacon_startup is __init. If you want to wade through the false
891 * positives, take out the check for rodata.
893 static int init_section_ref_ok(const char *name)
896 /* Absolute section names */
897 const char *namelist1[] = {
899 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
900 ".toc1", /* used by ppc64 */
904 "__bug_table", /* used by powerpc for BUG() */
912 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
915 /* Start of section names */
916 const char *namelist2[] = {
923 /* part of section name */
924 const char *namelist3 [] = {
925 ".unwind", /* sample: IA_64.unwind.init.text */
929 for (s = namelist1; *s; s++)
930 if (strcmp(*s, name) == 0)
932 for (s = namelist2; *s; s++)
933 if (strncmp(*s, name, strlen(*s)) == 0)
935 for (s = namelist3; *s; s++)
936 if (strstr(name, *s) != NULL)
938 if (strrcmp(name, ".init") == 0)
944 * Functions used only during module exit is marked __exit and is stored in
945 * a .exit.text section. Likewise data is marked __exitdata and stored in
946 * a .exit.data section.
947 * If this section is one of these sections return 1
948 * See include/linux/init.h for the details
950 static int exit_section(const char *name)
952 if (strcmp(name, ".exit.text") == 0)
954 if (strcmp(name, ".exit.data") == 0)
961 * Identify sections from which references to a .exit section is OK.
963 * [OPD] Keith Ownes <kaos@sgi.com> commented:
964 * For our future {in}sanity, add a comment that this is the ppc .opd
965 * section, not the ia64 .opd section.
966 * ia64 .opd should not point to discarded sections.
967 * [.rodata] like for .init.text we ignore .rodata references -same reason
969 static int exit_section_ref_ok(const char *name)
972 /* Absolute section names */
973 const char *namelist1[] = {
978 ".opd", /* See comment [OPD] */
979 ".toc1", /* used by ppc64 */
982 "__bug_table", /* used by powerpc for BUG() */
989 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
992 /* Start of section names */
993 const char *namelist2[] = {
997 /* part of section name */
998 const char *namelist3 [] = {
999 ".unwind", /* Sample: IA_64.unwind.exit.text */
1003 for (s = namelist1; *s; s++)
1004 if (strcmp(*s, name) == 0)
1006 for (s = namelist2; *s; s++)
1007 if (strncmp(*s, name, strlen(*s)) == 0)
1009 for (s = namelist3; *s; s++)
1010 if (strstr(name, *s) != NULL)
1015 static void read_symbols(char *modname)
1017 const char *symname;
1021 struct elf_info info = { };
1024 parse_elf(&info, modname);
1026 mod = new_module(modname);
1028 /* When there's no vmlinux, don't print warnings about
1029 * unresolved symbols (since there'll be too many ;) */
1030 if (is_vmlinux(modname)) {
1035 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1037 if (license_is_gpl_compatible(license))
1038 mod->gpl_compatible = 1;
1040 mod->gpl_compatible = 0;
1043 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1044 "license", license);
1047 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1048 symname = info.strtab + sym->st_name;
1050 handle_modversions(mod, &info, sym, symname);
1051 handle_moddevtable(mod, &info, sym, symname);
1053 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1054 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1056 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1058 maybe_frob_rcs_version(modname, version, info.modinfo,
1059 version - (char *)info.hdr);
1060 if (version || (all_versions && !is_vmlinux(modname)))
1061 get_src_version(modname, mod->srcversion,
1062 sizeof(mod->srcversion)-1);
1064 parse_elf_finish(&info);
1066 /* Our trick to get versioning for struct_module - it's
1067 * never passed as an argument to an exported function, so
1068 * the automatic versioning doesn't pick it up, but it's really
1069 * important anyhow */
1071 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1076 /* We first write the generated file into memory using the
1077 * following helper, then compare to the file on disk and
1078 * only update the later if anything changed */
1080 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1081 const char *fmt, ...)
1088 len = vsnprintf(tmp, SZ, fmt, ap);
1089 buf_write(buf, tmp, len);
1093 void buf_write(struct buffer *buf, const char *s, int len)
1095 if (buf->size - buf->pos < len) {
1096 buf->size += len + SZ;
1097 buf->p = realloc(buf->p, buf->size);
1099 strncpy(buf->p + buf->pos, s, len);
1103 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1105 const char *e = is_vmlinux(m) ?"":".ko";
1109 fatal("modpost: GPL-incompatible module %s%s "
1110 "uses GPL-only symbol '%s'\n", m, e, s);
1112 case export_unused_gpl:
1113 fatal("modpost: GPL-incompatible module %s%s "
1114 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1116 case export_gpl_future:
1117 warn("modpost: GPL-incompatible module %s%s "
1118 "uses future GPL-only symbol '%s'\n", m, e, s);
1122 case export_unknown:
1128 static void check_for_unused(enum export exp, const char* m, const char* s)
1130 const char *e = is_vmlinux(m) ?"":".ko";
1134 case export_unused_gpl:
1135 warn("modpost: module %s%s "
1136 "uses symbol '%s' marked UNUSED\n", m, e, s);
1144 static void check_exports(struct module *mod)
1146 struct symbol *s, *exp;
1148 for (s = mod->unres; s; s = s->next) {
1149 const char *basename;
1150 exp = find_symbol(s->name);
1151 if (!exp || exp->module == mod)
1153 basename = strrchr(mod->name, '/');
1157 basename = mod->name;
1158 if (!mod->gpl_compatible)
1159 check_for_gpl_usage(exp->export, basename, exp->name);
1160 check_for_unused(exp->export, basename, exp->name);
1165 * Header for the generated file
1167 static void add_header(struct buffer *b, struct module *mod)
1169 buf_printf(b, "#include <linux/module.h>\n");
1170 buf_printf(b, "#include <linux/vermagic.h>\n");
1171 buf_printf(b, "#include <linux/compiler.h>\n");
1172 buf_printf(b, "\n");
1173 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1174 buf_printf(b, "\n");
1175 buf_printf(b, "struct module __this_module\n");
1176 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1177 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1179 buf_printf(b, " .init = init_module,\n");
1180 if (mod->has_cleanup)
1181 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1182 " .exit = cleanup_module,\n"
1184 buf_printf(b, "};\n");
1188 * Record CRCs for unresolved symbols
1190 static void add_versions(struct buffer *b, struct module *mod)
1192 struct symbol *s, *exp;
1194 for (s = mod->unres; s; s = s->next) {
1195 exp = find_symbol(s->name);
1196 if (!exp || exp->module == mod) {
1197 if (have_vmlinux && !s->weak)
1198 warn("\"%s\" [%s.ko] undefined!\n",
1199 s->name, mod->name);
1202 s->module = exp->module;
1203 s->crc_valid = exp->crc_valid;
1210 buf_printf(b, "\n");
1211 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1212 buf_printf(b, "__attribute_used__\n");
1213 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1215 for (s = mod->unres; s; s = s->next) {
1219 if (!s->crc_valid) {
1220 warn("\"%s\" [%s.ko] has no CRC!\n",
1221 s->name, mod->name);
1224 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1227 buf_printf(b, "};\n");
1230 static void add_depends(struct buffer *b, struct module *mod,
1231 struct module *modules)
1237 for (m = modules; m; m = m->next) {
1238 m->seen = is_vmlinux(m->name);
1241 buf_printf(b, "\n");
1242 buf_printf(b, "static const char __module_depends[]\n");
1243 buf_printf(b, "__attribute_used__\n");
1244 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1245 buf_printf(b, "\"depends=");
1246 for (s = mod->unres; s; s = s->next) {
1250 if (s->module->seen)
1253 s->module->seen = 1;
1254 buf_printf(b, "%s%s", first ? "" : ",",
1255 strrchr(s->module->name, '/') + 1);
1258 buf_printf(b, "\";\n");
1261 static void add_srcversion(struct buffer *b, struct module *mod)
1263 if (mod->srcversion[0]) {
1264 buf_printf(b, "\n");
1265 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1270 static void write_if_changed(struct buffer *b, const char *fname)
1276 file = fopen(fname, "r");
1280 if (fstat(fileno(file), &st) < 0)
1283 if (st.st_size != b->pos)
1286 tmp = NOFAIL(malloc(b->pos));
1287 if (fread(tmp, 1, b->pos, file) != b->pos)
1290 if (memcmp(tmp, b->p, b->pos) != 0)
1302 file = fopen(fname, "w");
1307 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1314 /* parse Module.symvers file. line format:
1315 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1317 static void read_dump(const char *fname, unsigned int kernel)
1319 unsigned long size, pos = 0;
1320 void *file = grab_file(fname, &size);
1324 /* No symbol versions, silently ignore */
1327 while ((line = get_next_line(&pos, file, size))) {
1328 char *symname, *modname, *d, *export, *end;
1333 if (!(symname = strchr(line, '\t')))
1336 if (!(modname = strchr(symname, '\t')))
1339 if ((export = strchr(modname, '\t')) != NULL)
1341 if (export && ((end = strchr(export, '\t')) != NULL))
1343 crc = strtoul(line, &d, 16);
1344 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1347 if (!(mod = find_module(modname))) {
1348 if (is_vmlinux(modname)) {
1351 mod = new_module(NOFAIL(strdup(modname)));
1354 s = sym_add_exported(symname, mod, export_no(export));
1357 sym_update_crc(symname, mod, crc, export_no(export));
1361 fatal("parse error in symbol dump file\n");
1364 /* For normal builds always dump all symbols.
1365 * For external modules only dump symbols
1366 * that are not read from kernel Module.symvers.
1368 static int dump_sym(struct symbol *sym)
1370 if (!external_module)
1372 if (sym->vmlinux || sym->kernel)
1377 static void write_dump(const char *fname)
1379 struct buffer buf = { };
1380 struct symbol *symbol;
1383 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1384 symbol = symbolhash[n];
1386 if (dump_sym(symbol))
1387 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1388 symbol->crc, symbol->name,
1389 symbol->module->name,
1390 export_str(symbol->export));
1391 symbol = symbol->next;
1394 write_if_changed(&buf, fname);
1397 int main(int argc, char **argv)
1400 struct buffer buf = { };
1402 char *kernel_read = NULL, *module_read = NULL;
1403 char *dump_write = NULL;
1406 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
1409 kernel_read = optarg;
1412 module_read = optarg;
1413 external_module = 1;
1419 dump_write = optarg;
1430 read_dump(kernel_read, 1);
1432 read_dump(module_read, 0);
1434 while (optind < argc) {
1435 read_symbols(argv[optind++]);
1438 for (mod = modules; mod; mod = mod->next) {
1444 for (mod = modules; mod; mod = mod->next) {
1450 add_header(&buf, mod);
1451 add_versions(&buf, mod);
1452 add_depends(&buf, mod, modules);
1453 add_moddevtable(&buf, mod);
1454 add_srcversion(&buf, mod);
1456 sprintf(fname, "%s.mod.c", mod->name);
1457 write_if_changed(&buf, fname);
1461 write_dump(dump_write);