kbuild: ignore references from ".pci_fixup" to ".init.text"
[linux-2.6/linux-mips.git] / scripts / mod / modpost.c
blob5028d46a8f3572a4e3f5296d051601c3724a78b3
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 ...
14 #include <ctype.h>
15 #include "modpost.h"
16 #include "../../include/linux/license.h"
18 /* Are we using CONFIG_MODVERSIONS? */
19 int modversions = 0;
20 /* Warn about undefined symbols? (do so if we have vmlinux) */
21 int have_vmlinux = 0;
22 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
23 static int all_versions = 0;
24 /* If we are modposting external module set to 1 */
25 static int external_module = 0;
26 /* How a symbol is exported */
27 enum export {
28 export_plain, export_unused, export_gpl,
29 export_unused_gpl, export_gpl_future, export_unknown
32 void fatal(const char *fmt, ...)
34 va_list arglist;
36 fprintf(stderr, "FATAL: ");
38 va_start(arglist, fmt);
39 vfprintf(stderr, fmt, arglist);
40 va_end(arglist);
42 exit(1);
45 void warn(const char *fmt, ...)
47 va_list arglist;
49 fprintf(stderr, "WARNING: ");
51 va_start(arglist, fmt);
52 vfprintf(stderr, fmt, arglist);
53 va_end(arglist);
56 static int is_vmlinux(const char *modname)
58 const char *myname;
60 if ((myname = strrchr(modname, '/')))
61 myname++;
62 else
63 myname = modname;
65 return strcmp(myname, "vmlinux") == 0;
68 void *do_nofail(void *ptr, const char *expr)
70 if (!ptr) {
71 fatal("modpost: Memory allocation failure: %s.\n", expr);
73 return ptr;
76 /* A list of all modules we processed */
78 static struct module *modules;
80 static struct module *find_module(char *modname)
82 struct module *mod;
84 for (mod = modules; mod; mod = mod->next)
85 if (strcmp(mod->name, modname) == 0)
86 break;
87 return mod;
90 static struct module *new_module(char *modname)
92 struct module *mod;
93 char *p, *s;
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)
102 *s = '\0';
104 /* add to list */
105 mod->name = p;
106 mod->gpl_compatible = -1;
107 mod->next = modules;
108 modules = mod;
110 return mod;
113 /* A hash of all exported symbols,
114 * struct symbol is also used for lists of unresolved symbols */
116 #define SYMBOL_HASH_SIZE 1024
118 struct symbol {
119 struct symbol *next;
120 struct module *module;
121 unsigned int crc;
122 int crc_valid;
123 unsigned int weak:1;
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 */
129 char name[0];
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,
152 struct symbol *next)
154 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
156 memset(s, 0, sizeof(*s));
157 strcpy(s->name, name);
158 s->weak = weak;
159 s->next = next;
160 return s;
163 /* For the hash of exported symbols */
164 static struct symbol *new_symbol(const char *name, struct module *module,
165 enum export export)
167 unsigned int hash;
168 struct symbol *new;
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;
174 return new;
177 static struct symbol *find_symbol(const char *name)
179 struct symbol *s;
181 /* For our purposes, .foo matches foo. PPC64 needs this. */
182 if (name[0] == '.')
183 name++;
185 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
186 if (strcmp(s->name, name) == 0)
187 return s;
189 return NULL;
192 static struct {
193 const char *str;
194 enum export export;
195 } export_list[] = {
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)
212 int i;
213 if (!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)
225 return export_plain;
226 else if (sec == elf->export_unused_sec)
227 return export_unused;
228 else if (sec == elf->export_gpl_sec)
229 return export_gpl;
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;
234 else
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,
243 enum export export)
245 struct symbol *s = find_symbol(name);
247 if (!s) {
248 s = new_symbol(name, mod, export);
249 } else {
250 if (!s->preloaded) {
251 warn("%s: '%s' exported twice. Previous export "
252 "was in %s%s\n", mod->name, name,
253 s->module->name,
254 is_vmlinux(s->module->name) ?"":".ko");
257 s->preloaded = 0;
258 s->vmlinux = is_vmlinux(mod->name);
259 s->kernel = 0;
260 s->export = export;
261 return s;
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);
269 if (!s)
270 s = new_symbol(name, mod, export);
271 s->crc = crc;
272 s->crc_valid = 1;
275 void *grab_file(const char *filename, unsigned long *size)
277 struct stat st;
278 void *map;
279 int fd;
281 fd = open(filename, O_RDONLY);
282 if (fd < 0 || fstat(fd, &st) != 0)
283 return NULL;
285 *size = st.st_size;
286 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
287 close(fd);
289 if (map == MAP_FAILED)
290 return NULL;
291 return map;
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];
302 int skip = 1;
303 size_t len = 0;
304 signed char *p = (signed char *)file + *pos;
305 char *s = line;
307 for (; *pos < size ; (*pos)++)
309 if (skip && isspace(*p)) {
310 p++;
311 continue;
313 skip = 0;
314 if (*p != '\n' && (*pos < size)) {
315 len++;
316 *s++ = *p++;
317 if (len > 4095)
318 break; /* Too long, stop */
319 } else {
320 /* End of string */
321 *s = '\0';
322 return line;
325 /* End of buffer */
326 return NULL;
329 void release_file(void *file, unsigned long size)
331 munmap(file, size);
334 static void parse_elf(struct elf_info *info, const char *filename)
336 unsigned int i;
337 Elf_Ehdr *hdr = info->hdr;
338 Elf_Shdr *sechdrs;
339 Elf_Sym *sym;
341 hdr = grab_file(filename, &info->size);
342 if (!hdr) {
343 perror(filename);
344 exit(1);
346 info->hdr = hdr;
347 if (info->size < sizeof(*hdr))
348 goto truncated;
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;
370 const char *secname;
372 if (sechdrs[i].sh_offset > info->size)
373 goto truncated;
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)
390 continue;
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);
408 return;
410 truncated:
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)
425 unsigned int crc;
426 enum export export = export_from_sec(info, sym->st_shndx);
428 switch (sym->st_shndx) {
429 case SHN_COMMON:
430 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
431 break;
432 case SHN_ABS:
433 /* CRC'd symbol */
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,
437 export);
439 break;
440 case SHN_UNDEF:
441 /* undefined symbol */
442 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
443 ELF_ST_BIND(sym->st_info) != STB_WEAK)
444 break;
445 /* ignore global offset table */
446 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
447 break;
448 /* ignore __this_module, it will be resolved shortly */
449 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
450 break;
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
456 #endif
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)
461 break;
462 if (symname[0] == '.') {
463 char *munged = strdup(symname);
464 munged[0] = '_';
465 munged[1] = toupper(munged[1]);
466 symname = munged;
469 #endif
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,
476 mod->unres);
477 break;
478 default:
479 /* All exported symbols */
480 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
481 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
482 export);
484 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
485 mod->has_init = 1;
486 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
487 mod->has_cleanup = 1;
488 break;
493 * Parse tag=value strings from .modinfo section
495 static char *next_string(char *string, unsigned long *secsize)
497 /* Skip non-zero chars */
498 while (string[0]) {
499 string++;
500 if ((*secsize)-- <= 1)
501 return NULL;
504 /* Skip any zero padding. */
505 while (!string[0]) {
506 string++;
507 if ((*secsize)-- <= 1)
508 return NULL;
510 return string;
513 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
514 const char *tag, char *info)
516 char *p;
517 unsigned int taglen = strlen(tag);
518 unsigned long size = modinfo_len;
520 if (info) {
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;
529 return NULL;
532 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
533 const char *tag)
536 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
540 * Test if string s ends in string sub
541 * return 0 if match
543 static int strrcmp(const char *s, const char *sub)
545 int slen, sublen;
547 if (!s || !sub)
548 return 1;
550 slen = strlen(s);
551 sublen = strlen(sub);
553 if ((slen == 0) || (sublen == 0))
554 return 1;
556 if (sublen > slen)
557 return 1;
559 return memcmp(s + slen - sublen, sub, sublen);
563 * Whitelist to allow certain references to pass with no warning.
564 * Pattern 1:
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
568 * this pattern.
569 * The pattern is identified by:
570 * tosec = .init.data
571 * fromsec = .data*
572 * atsym =__param*
574 * Pattern 2:
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
578 * warn here.
579 * the pattern is identified by:
580 * tosec = .init.text | .exit.text | .init.data
581 * fromsec = .data
582 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
584 static int secref_whitelist(const char *modname, const char *tosec,
585 const char *fromsec, const char *atsym)
587 int f1 = 1, f2 = 1;
588 const char **s;
589 const char *pat2sym[] = {
590 "driver",
591 "_template", /* scsi uses *_template a lot */
592 "_sht", /* scsi also used *_sht to some extent */
593 "_ops",
594 "_probe",
595 "_probe_one",
596 NULL
599 /* Check for pattern 1 */
600 if (strcmp(tosec, ".init.data") != 0)
601 f1 = 0;
602 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
603 f1 = 0;
604 if (strncmp(atsym, "__param", strlen("__param")) != 0)
605 f1 = 0;
607 if (f1)
608 return f1;
610 /* Check for pattern 2 */
611 if ((strcmp(tosec, ".init.text") != 0) &&
612 (strcmp(tosec, ".exit.text") != 0) &&
613 (strcmp(tosec, ".init.data") != 0))
614 f2 = 0;
615 if (strcmp(fromsec, ".data") != 0)
616 f2 = 0;
618 for (s = pat2sym; *s; s++)
619 if (strrcmp(atsym, *s) == 0)
620 f1 = 1;
621 if (f1 && f2)
622 return 1;
624 /* Whitelist all references from .pci_fixup section if vmlinux */
625 if (is_vmlinux(modname)) {
626 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
627 (strcmp(tosec, ".init.text") == 0))
628 return 1;
633 * Find symbol based on relocation record info.
634 * In some cases the symbol supplied is a valid symbol so
635 * return refsym. If st_name != 0 we assume this is a valid symbol.
636 * In other cases the symbol needs to be looked up in the symbol table
637 * based on section and address.
638 * **/
639 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
640 Elf_Sym *relsym)
642 Elf_Sym *sym;
644 if (relsym->st_name != 0)
645 return relsym;
646 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
647 if (sym->st_shndx != relsym->st_shndx)
648 continue;
649 if (sym->st_value == addr)
650 return sym;
652 return NULL;
656 * Find symbols before or equal addr and after addr - in the section sec.
657 * If we find two symbols with equal offset prefer one with a valid name.
658 * The ELF format may have a better way to detect what type of symbol
659 * it is, but this works for now.
661 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
662 const char *sec,
663 Elf_Sym **before, Elf_Sym **after)
665 Elf_Sym *sym;
666 Elf_Ehdr *hdr = elf->hdr;
667 Elf_Addr beforediff = ~0;
668 Elf_Addr afterdiff = ~0;
669 const char *secstrings = (void *)hdr +
670 elf->sechdrs[hdr->e_shstrndx].sh_offset;
672 *before = NULL;
673 *after = NULL;
675 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
676 const char *symsec;
678 if (sym->st_shndx >= SHN_LORESERVE)
679 continue;
680 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
681 if (strcmp(symsec, sec) != 0)
682 continue;
683 if (sym->st_value <= addr) {
684 if ((addr - sym->st_value) < beforediff) {
685 beforediff = addr - sym->st_value;
686 *before = sym;
688 else if ((addr - sym->st_value) == beforediff) {
689 /* equal offset, valid name? */
690 const char *name = elf->strtab + sym->st_name;
691 if (name && strlen(name))
692 *before = sym;
695 else
697 if ((sym->st_value - addr) < afterdiff) {
698 afterdiff = sym->st_value - addr;
699 *after = sym;
701 else if ((sym->st_value - addr) == afterdiff) {
702 /* equal offset, valid name? */
703 const char *name = elf->strtab + sym->st_name;
704 if (name && strlen(name))
705 *after = sym;
712 * Print a warning about a section mismatch.
713 * Try to find symbols near it so user can find it.
714 * Check whitelist before warning - it may be a false positive.
716 static void warn_sec_mismatch(const char *modname, const char *fromsec,
717 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
719 const char *refsymname = "";
720 Elf_Sym *before, *after;
721 Elf_Sym *refsym;
722 Elf_Ehdr *hdr = elf->hdr;
723 Elf_Shdr *sechdrs = elf->sechdrs;
724 const char *secstrings = (void *)hdr +
725 sechdrs[hdr->e_shstrndx].sh_offset;
726 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
728 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
730 refsym = find_elf_symbol(elf, r.r_addend, sym);
731 if (refsym && strlen(elf->strtab + refsym->st_name))
732 refsymname = elf->strtab + refsym->st_name;
734 /* check whitelist - we may ignore it */
735 if (before &&
736 secref_whitelist(modname, secname, fromsec,
737 elf->strtab + before->st_name))
738 return;
740 if (before && after) {
741 warn("%s - Section mismatch: reference to %s:%s from %s "
742 "between '%s' (at offset 0x%llx) and '%s'\n",
743 modname, secname, refsymname, fromsec,
744 elf->strtab + before->st_name,
745 (long long)r.r_offset,
746 elf->strtab + after->st_name);
747 } else if (before) {
748 warn("%s - Section mismatch: reference to %s:%s from %s "
749 "after '%s' (at offset 0x%llx)\n",
750 modname, secname, refsymname, fromsec,
751 elf->strtab + before->st_name,
752 (long long)r.r_offset);
753 } else if (after) {
754 warn("%s - Section mismatch: reference to %s:%s from %s "
755 "before '%s' (at offset -0x%llx)\n",
756 modname, secname, refsymname, fromsec,
757 elf->strtab + after->st_name,
758 (long long)r.r_offset);
759 } else {
760 warn("%s - Section mismatch: reference to %s:%s from %s "
761 "(offset 0x%llx)\n",
762 modname, secname, fromsec, refsymname,
763 (long long)r.r_offset);
768 * A module includes a number of sections that are discarded
769 * either when loaded or when used as built-in.
770 * For loaded modules all functions marked __init and all data
771 * marked __initdata will be discarded when the module has been intialized.
772 * Likewise for modules used built-in the sections marked __exit
773 * are discarded because __exit marked function are supposed to be called
774 * only when a moduel is unloaded which never happes for built-in modules.
775 * The check_sec_ref() function traverses all relocation records
776 * to find all references to a section that reference a section that will
777 * be discarded and warns about it.
779 static void check_sec_ref(struct module *mod, const char *modname,
780 struct elf_info *elf,
781 int section(const char*),
782 int section_ref_ok(const char *))
784 int i;
785 Elf_Sym *sym;
786 Elf_Ehdr *hdr = elf->hdr;
787 Elf_Shdr *sechdrs = elf->sechdrs;
788 const char *secstrings = (void *)hdr +
789 sechdrs[hdr->e_shstrndx].sh_offset;
791 /* Walk through all sections */
792 for (i = 0; i < hdr->e_shnum; i++) {
793 const char *name = secstrings + sechdrs[i].sh_name;
794 const char *secname;
795 Elf_Rela r;
796 unsigned int r_sym;
797 /* We want to process only relocation sections and not .init */
798 if (sechdrs[i].sh_type == SHT_RELA) {
799 Elf_Rela *rela;
800 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
801 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
802 name += strlen(".rela");
803 if (section_ref_ok(name))
804 continue;
806 for (rela = start; rela < stop; rela++) {
807 r.r_offset = TO_NATIVE(rela->r_offset);
808 #if KERNEL_ELFCLASS == ELFCLASS64
809 if (hdr->e_machine == EM_MIPS) {
810 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
811 r_sym = TO_NATIVE(r_sym);
812 } else {
813 r.r_info = TO_NATIVE(rela->r_info);
814 r_sym = ELF_R_SYM(r.r_info);
816 #else
817 r.r_info = TO_NATIVE(rela->r_info);
818 r_sym = ELF_R_SYM(r.r_info);
819 #endif
820 r.r_addend = TO_NATIVE(rela->r_addend);
821 sym = elf->symtab_start + r_sym;
822 /* Skip special sections */
823 if (sym->st_shndx >= SHN_LORESERVE)
824 continue;
826 secname = secstrings +
827 sechdrs[sym->st_shndx].sh_name;
828 if (section(secname))
829 warn_sec_mismatch(modname, name,
830 elf, sym, r);
832 } else if (sechdrs[i].sh_type == SHT_REL) {
833 Elf_Rel *rel;
834 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
835 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
836 name += strlen(".rel");
837 if (section_ref_ok(name))
838 continue;
840 for (rel = start; rel < stop; rel++) {
841 r.r_offset = TO_NATIVE(rel->r_offset);
842 #if KERNEL_ELFCLASS == ELFCLASS64
843 if (hdr->e_machine == EM_MIPS) {
844 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
845 r_sym = TO_NATIVE(r_sym);
846 } else {
847 r.r_info = TO_NATIVE(rel->r_info);
848 r_sym = ELF_R_SYM(r.r_info);
850 #else
851 r.r_info = TO_NATIVE(rel->r_info);
852 r_sym = ELF_R_SYM(r.r_info);
853 #endif
854 r.r_addend = 0;
855 sym = elf->symtab_start + r_sym;
856 /* Skip special sections */
857 if (sym->st_shndx >= SHN_LORESERVE)
858 continue;
860 secname = secstrings +
861 sechdrs[sym->st_shndx].sh_name;
862 if (section(secname))
863 warn_sec_mismatch(modname, name,
864 elf, sym, r);
871 * Functions used only during module init is marked __init and is stored in
872 * a .init.text section. Likewise data is marked __initdata and stored in
873 * a .init.data section.
874 * If this section is one of these sections return 1
875 * See include/linux/init.h for the details
877 static int init_section(const char *name)
879 if (strcmp(name, ".init") == 0)
880 return 1;
881 if (strncmp(name, ".init.", strlen(".init.")) == 0)
882 return 1;
883 return 0;
887 * Identify sections from which references to a .init section is OK.
889 * Unfortunately references to read only data that referenced .init
890 * sections had to be excluded. Almost all of these are false
891 * positives, they are created by gcc. The downside of excluding rodata
892 * is that there really are some user references from rodata to
893 * init code, e.g. drivers/video/vgacon.c:
895 * const struct consw vga_con = {
896 * con_startup: vgacon_startup,
898 * where vgacon_startup is __init. If you want to wade through the false
899 * positives, take out the check for rodata.
901 static int init_section_ref_ok(const char *name)
903 const char **s;
904 /* Absolute section names */
905 const char *namelist1[] = {
906 ".init",
907 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
908 ".toc1", /* used by ppc64 */
909 ".stab",
910 ".rodata",
911 ".text.lock",
912 "__bug_table", /* used by powerpc for BUG() */
913 ".pci_fixup_header",
914 ".pci_fixup_final",
915 ".pdr",
916 "__param",
917 "__ex_table",
918 ".fixup",
919 ".smp_locks",
920 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
921 NULL
923 /* Start of section names */
924 const char *namelist2[] = {
925 ".init.",
926 ".altinstructions",
927 ".eh_frame",
928 ".debug",
929 NULL
931 /* part of section name */
932 const char *namelist3 [] = {
933 ".unwind", /* sample: IA_64.unwind.init.text */
934 NULL
937 for (s = namelist1; *s; s++)
938 if (strcmp(*s, name) == 0)
939 return 1;
940 for (s = namelist2; *s; s++)
941 if (strncmp(*s, name, strlen(*s)) == 0)
942 return 1;
943 for (s = namelist3; *s; s++)
944 if (strstr(name, *s) != NULL)
945 return 1;
946 if (strrcmp(name, ".init") == 0)
947 return 1;
948 return 0;
952 * Functions used only during module exit is marked __exit and is stored in
953 * a .exit.text section. Likewise data is marked __exitdata and stored in
954 * a .exit.data section.
955 * If this section is one of these sections return 1
956 * See include/linux/init.h for the details
958 static int exit_section(const char *name)
960 if (strcmp(name, ".exit.text") == 0)
961 return 1;
962 if (strcmp(name, ".exit.data") == 0)
963 return 1;
964 return 0;
969 * Identify sections from which references to a .exit section is OK.
971 * [OPD] Keith Ownes <kaos@sgi.com> commented:
972 * For our future {in}sanity, add a comment that this is the ppc .opd
973 * section, not the ia64 .opd section.
974 * ia64 .opd should not point to discarded sections.
975 * [.rodata] like for .init.text we ignore .rodata references -same reason
977 static int exit_section_ref_ok(const char *name)
979 const char **s;
980 /* Absolute section names */
981 const char *namelist1[] = {
982 ".exit.text",
983 ".exit.data",
984 ".init.text",
985 ".rodata",
986 ".opd", /* See comment [OPD] */
987 ".toc1", /* used by ppc64 */
988 ".altinstructions",
989 ".pdr",
990 "__bug_table", /* used by powerpc for BUG() */
991 ".exitcall.exit",
992 ".eh_frame",
993 ".stab",
994 "__ex_table",
995 ".fixup",
996 ".smp_locks",
997 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
998 NULL
1000 /* Start of section names */
1001 const char *namelist2[] = {
1002 ".debug",
1003 NULL
1005 /* part of section name */
1006 const char *namelist3 [] = {
1007 ".unwind", /* Sample: IA_64.unwind.exit.text */
1008 NULL
1011 for (s = namelist1; *s; s++)
1012 if (strcmp(*s, name) == 0)
1013 return 1;
1014 for (s = namelist2; *s; s++)
1015 if (strncmp(*s, name, strlen(*s)) == 0)
1016 return 1;
1017 for (s = namelist3; *s; s++)
1018 if (strstr(name, *s) != NULL)
1019 return 1;
1020 return 0;
1023 static void read_symbols(char *modname)
1025 const char *symname;
1026 char *version;
1027 char *license;
1028 struct module *mod;
1029 struct elf_info info = { };
1030 Elf_Sym *sym;
1032 parse_elf(&info, modname);
1034 mod = new_module(modname);
1036 /* When there's no vmlinux, don't print warnings about
1037 * unresolved symbols (since there'll be too many ;) */
1038 if (is_vmlinux(modname)) {
1039 have_vmlinux = 1;
1040 mod->skip = 1;
1043 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1044 while (license) {
1045 if (license_is_gpl_compatible(license))
1046 mod->gpl_compatible = 1;
1047 else {
1048 mod->gpl_compatible = 0;
1049 break;
1051 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1052 "license", license);
1055 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1056 symname = info.strtab + sym->st_name;
1058 handle_modversions(mod, &info, sym, symname);
1059 handle_moddevtable(mod, &info, sym, symname);
1061 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1062 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1064 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1065 if (version)
1066 maybe_frob_rcs_version(modname, version, info.modinfo,
1067 version - (char *)info.hdr);
1068 if (version || (all_versions && !is_vmlinux(modname)))
1069 get_src_version(modname, mod->srcversion,
1070 sizeof(mod->srcversion)-1);
1072 parse_elf_finish(&info);
1074 /* Our trick to get versioning for struct_module - it's
1075 * never passed as an argument to an exported function, so
1076 * the automatic versioning doesn't pick it up, but it's really
1077 * important anyhow */
1078 if (modversions)
1079 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1082 #define SZ 500
1084 /* We first write the generated file into memory using the
1085 * following helper, then compare to the file on disk and
1086 * only update the later if anything changed */
1088 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1089 const char *fmt, ...)
1091 char tmp[SZ];
1092 int len;
1093 va_list ap;
1095 va_start(ap, fmt);
1096 len = vsnprintf(tmp, SZ, fmt, ap);
1097 buf_write(buf, tmp, len);
1098 va_end(ap);
1101 void buf_write(struct buffer *buf, const char *s, int len)
1103 if (buf->size - buf->pos < len) {
1104 buf->size += len + SZ;
1105 buf->p = realloc(buf->p, buf->size);
1107 strncpy(buf->p + buf->pos, s, len);
1108 buf->pos += len;
1111 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1113 const char *e = is_vmlinux(m) ?"":".ko";
1115 switch (exp) {
1116 case export_gpl:
1117 fatal("modpost: GPL-incompatible module %s%s "
1118 "uses GPL-only symbol '%s'\n", m, e, s);
1119 break;
1120 case export_unused_gpl:
1121 fatal("modpost: GPL-incompatible module %s%s "
1122 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1123 break;
1124 case export_gpl_future:
1125 warn("modpost: GPL-incompatible module %s%s "
1126 "uses future GPL-only symbol '%s'\n", m, e, s);
1127 break;
1128 case export_plain:
1129 case export_unused:
1130 case export_unknown:
1131 /* ignore */
1132 break;
1136 static void check_for_unused(enum export exp, const char* m, const char* s)
1138 const char *e = is_vmlinux(m) ?"":".ko";
1140 switch (exp) {
1141 case export_unused:
1142 case export_unused_gpl:
1143 warn("modpost: module %s%s "
1144 "uses symbol '%s' marked UNUSED\n", m, e, s);
1145 break;
1146 default:
1147 /* ignore */
1148 break;
1152 static void check_exports(struct module *mod)
1154 struct symbol *s, *exp;
1156 for (s = mod->unres; s; s = s->next) {
1157 const char *basename;
1158 exp = find_symbol(s->name);
1159 if (!exp || exp->module == mod)
1160 continue;
1161 basename = strrchr(mod->name, '/');
1162 if (basename)
1163 basename++;
1164 else
1165 basename = mod->name;
1166 if (!mod->gpl_compatible)
1167 check_for_gpl_usage(exp->export, basename, exp->name);
1168 check_for_unused(exp->export, basename, exp->name);
1173 * Header for the generated file
1175 static void add_header(struct buffer *b, struct module *mod)
1177 buf_printf(b, "#include <linux/module.h>\n");
1178 buf_printf(b, "#include <linux/vermagic.h>\n");
1179 buf_printf(b, "#include <linux/compiler.h>\n");
1180 buf_printf(b, "\n");
1181 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1182 buf_printf(b, "\n");
1183 buf_printf(b, "struct module __this_module\n");
1184 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1185 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1186 if (mod->has_init)
1187 buf_printf(b, " .init = init_module,\n");
1188 if (mod->has_cleanup)
1189 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1190 " .exit = cleanup_module,\n"
1191 "#endif\n");
1192 buf_printf(b, "};\n");
1196 * Record CRCs for unresolved symbols
1198 static void add_versions(struct buffer *b, struct module *mod)
1200 struct symbol *s, *exp;
1202 for (s = mod->unres; s; s = s->next) {
1203 exp = find_symbol(s->name);
1204 if (!exp || exp->module == mod) {
1205 if (have_vmlinux && !s->weak)
1206 warn("\"%s\" [%s.ko] undefined!\n",
1207 s->name, mod->name);
1208 continue;
1210 s->module = exp->module;
1211 s->crc_valid = exp->crc_valid;
1212 s->crc = exp->crc;
1215 if (!modversions)
1216 return;
1218 buf_printf(b, "\n");
1219 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1220 buf_printf(b, "__attribute_used__\n");
1221 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1223 for (s = mod->unres; s; s = s->next) {
1224 if (!s->module) {
1225 continue;
1227 if (!s->crc_valid) {
1228 warn("\"%s\" [%s.ko] has no CRC!\n",
1229 s->name, mod->name);
1230 continue;
1232 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1235 buf_printf(b, "};\n");
1238 static void add_depends(struct buffer *b, struct module *mod,
1239 struct module *modules)
1241 struct symbol *s;
1242 struct module *m;
1243 int first = 1;
1245 for (m = modules; m; m = m->next) {
1246 m->seen = is_vmlinux(m->name);
1249 buf_printf(b, "\n");
1250 buf_printf(b, "static const char __module_depends[]\n");
1251 buf_printf(b, "__attribute_used__\n");
1252 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1253 buf_printf(b, "\"depends=");
1254 for (s = mod->unres; s; s = s->next) {
1255 if (!s->module)
1256 continue;
1258 if (s->module->seen)
1259 continue;
1261 s->module->seen = 1;
1262 buf_printf(b, "%s%s", first ? "" : ",",
1263 strrchr(s->module->name, '/') + 1);
1264 first = 0;
1266 buf_printf(b, "\";\n");
1269 static void add_srcversion(struct buffer *b, struct module *mod)
1271 if (mod->srcversion[0]) {
1272 buf_printf(b, "\n");
1273 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1274 mod->srcversion);
1278 static void write_if_changed(struct buffer *b, const char *fname)
1280 char *tmp;
1281 FILE *file;
1282 struct stat st;
1284 file = fopen(fname, "r");
1285 if (!file)
1286 goto write;
1288 if (fstat(fileno(file), &st) < 0)
1289 goto close_write;
1291 if (st.st_size != b->pos)
1292 goto close_write;
1294 tmp = NOFAIL(malloc(b->pos));
1295 if (fread(tmp, 1, b->pos, file) != b->pos)
1296 goto free_write;
1298 if (memcmp(tmp, b->p, b->pos) != 0)
1299 goto free_write;
1301 free(tmp);
1302 fclose(file);
1303 return;
1305 free_write:
1306 free(tmp);
1307 close_write:
1308 fclose(file);
1309 write:
1310 file = fopen(fname, "w");
1311 if (!file) {
1312 perror(fname);
1313 exit(1);
1315 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1316 perror(fname);
1317 exit(1);
1319 fclose(file);
1322 /* parse Module.symvers file. line format:
1323 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1325 static void read_dump(const char *fname, unsigned int kernel)
1327 unsigned long size, pos = 0;
1328 void *file = grab_file(fname, &size);
1329 char *line;
1331 if (!file)
1332 /* No symbol versions, silently ignore */
1333 return;
1335 while ((line = get_next_line(&pos, file, size))) {
1336 char *symname, *modname, *d, *export, *end;
1337 unsigned int crc;
1338 struct module *mod;
1339 struct symbol *s;
1341 if (!(symname = strchr(line, '\t')))
1342 goto fail;
1343 *symname++ = '\0';
1344 if (!(modname = strchr(symname, '\t')))
1345 goto fail;
1346 *modname++ = '\0';
1347 if ((export = strchr(modname, '\t')) != NULL)
1348 *export++ = '\0';
1349 if (export && ((end = strchr(export, '\t')) != NULL))
1350 *end = '\0';
1351 crc = strtoul(line, &d, 16);
1352 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1353 goto fail;
1355 if (!(mod = find_module(modname))) {
1356 if (is_vmlinux(modname)) {
1357 have_vmlinux = 1;
1359 mod = new_module(NOFAIL(strdup(modname)));
1360 mod->skip = 1;
1362 s = sym_add_exported(symname, mod, export_no(export));
1363 s->kernel = kernel;
1364 s->preloaded = 1;
1365 sym_update_crc(symname, mod, crc, export_no(export));
1367 return;
1368 fail:
1369 fatal("parse error in symbol dump file\n");
1372 /* For normal builds always dump all symbols.
1373 * For external modules only dump symbols
1374 * that are not read from kernel Module.symvers.
1376 static int dump_sym(struct symbol *sym)
1378 if (!external_module)
1379 return 1;
1380 if (sym->vmlinux || sym->kernel)
1381 return 0;
1382 return 1;
1385 static void write_dump(const char *fname)
1387 struct buffer buf = { };
1388 struct symbol *symbol;
1389 int n;
1391 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1392 symbol = symbolhash[n];
1393 while (symbol) {
1394 if (dump_sym(symbol))
1395 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1396 symbol->crc, symbol->name,
1397 symbol->module->name,
1398 export_str(symbol->export));
1399 symbol = symbol->next;
1402 write_if_changed(&buf, fname);
1405 int main(int argc, char **argv)
1407 struct module *mod;
1408 struct buffer buf = { };
1409 char fname[SZ];
1410 char *kernel_read = NULL, *module_read = NULL;
1411 char *dump_write = NULL;
1412 int opt;
1414 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
1415 switch(opt) {
1416 case 'i':
1417 kernel_read = optarg;
1418 break;
1419 case 'I':
1420 module_read = optarg;
1421 external_module = 1;
1422 break;
1423 case 'm':
1424 modversions = 1;
1425 break;
1426 case 'o':
1427 dump_write = optarg;
1428 break;
1429 case 'a':
1430 all_versions = 1;
1431 break;
1432 default:
1433 exit(1);
1437 if (kernel_read)
1438 read_dump(kernel_read, 1);
1439 if (module_read)
1440 read_dump(module_read, 0);
1442 while (optind < argc) {
1443 read_symbols(argv[optind++]);
1446 for (mod = modules; mod; mod = mod->next) {
1447 if (mod->skip)
1448 continue;
1449 check_exports(mod);
1452 for (mod = modules; mod; mod = mod->next) {
1453 if (mod->skip)
1454 continue;
1456 buf.pos = 0;
1458 add_header(&buf, mod);
1459 add_versions(&buf, mod);
1460 add_depends(&buf, mod, modules);
1461 add_moddevtable(&buf, mod);
1462 add_srcversion(&buf, mod);
1464 sprintf(fname, "%s.mod.c", mod->name);
1465 write_if_changed(&buf, fname);
1468 if (dump_write)
1469 write_dump(dump_write);
1471 return 0;