kbuild: use simpler section mismatch warnings in modpost
[linux-2.6/verdex.git] / scripts / mod / modpost.c
blob7dfd0395c5b038bfea10b1d384f7b319f67cc90a
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 ...
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 /* Warn about section mismatch in vmlinux if set to 1 */
27 static int vmlinux_section_warnings = 1;
28 /* Only warn about unresolved symbols */
29 static int warn_unresolved = 0;
30 /* How a symbol is exported */
31 enum export {
32 export_plain, export_unused, export_gpl,
33 export_unused_gpl, export_gpl_future, export_unknown
36 #define PRINTF __attribute__ ((format (printf, 1, 2)))
38 PRINTF void fatal(const char *fmt, ...)
40 va_list arglist;
42 fprintf(stderr, "FATAL: ");
44 va_start(arglist, fmt);
45 vfprintf(stderr, fmt, arglist);
46 va_end(arglist);
48 exit(1);
51 PRINTF void warn(const char *fmt, ...)
53 va_list arglist;
55 fprintf(stderr, "WARNING: ");
57 va_start(arglist, fmt);
58 vfprintf(stderr, fmt, arglist);
59 va_end(arglist);
62 PRINTF void merror(const char *fmt, ...)
64 va_list arglist;
66 fprintf(stderr, "ERROR: ");
68 va_start(arglist, fmt);
69 vfprintf(stderr, fmt, arglist);
70 va_end(arglist);
73 static int is_vmlinux(const char *modname)
75 const char *myname;
77 myname = strrchr(modname, '/');
78 if (myname)
79 myname++;
80 else
81 myname = modname;
83 return (strcmp(myname, "vmlinux") == 0) ||
84 (strcmp(myname, "vmlinux.o") == 0);
87 void *do_nofail(void *ptr, const char *expr)
89 if (!ptr)
90 fatal("modpost: Memory allocation failure: %s.\n", expr);
92 return ptr;
95 /* A list of all modules we processed */
96 static struct module *modules;
98 static struct module *find_module(char *modname)
100 struct module *mod;
102 for (mod = modules; mod; mod = mod->next)
103 if (strcmp(mod->name, modname) == 0)
104 break;
105 return mod;
108 static struct module *new_module(char *modname)
110 struct module *mod;
111 char *p, *s;
113 mod = NOFAIL(malloc(sizeof(*mod)));
114 memset(mod, 0, sizeof(*mod));
115 p = NOFAIL(strdup(modname));
117 /* strip trailing .o */
118 s = strrchr(p, '.');
119 if (s != NULL)
120 if (strcmp(s, ".o") == 0)
121 *s = '\0';
123 /* add to list */
124 mod->name = p;
125 mod->gpl_compatible = -1;
126 mod->next = modules;
127 modules = mod;
129 return mod;
132 /* A hash of all exported symbols,
133 * struct symbol is also used for lists of unresolved symbols */
135 #define SYMBOL_HASH_SIZE 1024
137 struct symbol {
138 struct symbol *next;
139 struct module *module;
140 unsigned int crc;
141 int crc_valid;
142 unsigned int weak:1;
143 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
144 unsigned int kernel:1; /* 1 if symbol is from kernel
145 * (only for external modules) **/
146 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
147 enum export export; /* Type of export */
148 char name[0];
151 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
153 /* This is based on the hash agorithm from gdbm, via tdb */
154 static inline unsigned int tdb_hash(const char *name)
156 unsigned value; /* Used to compute the hash value. */
157 unsigned i; /* Used to cycle through random values. */
159 /* Set the initial value from the key size. */
160 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
161 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
163 return (1103515243 * value + 12345);
167 * Allocate a new symbols for use in the hash of exported symbols or
168 * the list of unresolved symbols per module
170 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
171 struct symbol *next)
173 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
175 memset(s, 0, sizeof(*s));
176 strcpy(s->name, name);
177 s->weak = weak;
178 s->next = next;
179 return s;
182 /* For the hash of exported symbols */
183 static struct symbol *new_symbol(const char *name, struct module *module,
184 enum export export)
186 unsigned int hash;
187 struct symbol *new;
189 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
190 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
191 new->module = module;
192 new->export = export;
193 return new;
196 static struct symbol *find_symbol(const char *name)
198 struct symbol *s;
200 /* For our purposes, .foo matches foo. PPC64 needs this. */
201 if (name[0] == '.')
202 name++;
204 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
205 if (strcmp(s->name, name) == 0)
206 return s;
208 return NULL;
211 static struct {
212 const char *str;
213 enum export export;
214 } export_list[] = {
215 { .str = "EXPORT_SYMBOL", .export = export_plain },
216 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
217 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
218 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
219 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
220 { .str = "(unknown)", .export = export_unknown },
224 static const char *export_str(enum export ex)
226 return export_list[ex].str;
229 static enum export export_no(const char *s)
231 int i;
233 if (!s)
234 return export_unknown;
235 for (i = 0; export_list[i].export != export_unknown; i++) {
236 if (strcmp(export_list[i].str, s) == 0)
237 return export_list[i].export;
239 return export_unknown;
242 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
244 if (sec == elf->export_sec)
245 return export_plain;
246 else if (sec == elf->export_unused_sec)
247 return export_unused;
248 else if (sec == elf->export_gpl_sec)
249 return export_gpl;
250 else if (sec == elf->export_unused_gpl_sec)
251 return export_unused_gpl;
252 else if (sec == elf->export_gpl_future_sec)
253 return export_gpl_future;
254 else
255 return export_unknown;
259 * Add an exported symbol - it may have already been added without a
260 * CRC, in this case just update the CRC
262 static struct symbol *sym_add_exported(const char *name, struct module *mod,
263 enum export export)
265 struct symbol *s = find_symbol(name);
267 if (!s) {
268 s = new_symbol(name, mod, export);
269 } else {
270 if (!s->preloaded) {
271 warn("%s: '%s' exported twice. Previous export "
272 "was in %s%s\n", mod->name, name,
273 s->module->name,
274 is_vmlinux(s->module->name) ?"":".ko");
275 } else {
276 /* In case Modules.symvers was out of date */
277 s->module = mod;
280 s->preloaded = 0;
281 s->vmlinux = is_vmlinux(mod->name);
282 s->kernel = 0;
283 s->export = export;
284 return s;
287 static void sym_update_crc(const char *name, struct module *mod,
288 unsigned int crc, enum export export)
290 struct symbol *s = find_symbol(name);
292 if (!s)
293 s = new_symbol(name, mod, export);
294 s->crc = crc;
295 s->crc_valid = 1;
298 void *grab_file(const char *filename, unsigned long *size)
300 struct stat st;
301 void *map;
302 int fd;
304 fd = open(filename, O_RDONLY);
305 if (fd < 0 || fstat(fd, &st) != 0)
306 return NULL;
308 *size = st.st_size;
309 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
310 close(fd);
312 if (map == MAP_FAILED)
313 return NULL;
314 return map;
318 * Return a copy of the next line in a mmap'ed file.
319 * spaces in the beginning of the line is trimmed away.
320 * Return a pointer to a static buffer.
322 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
324 static char line[4096];
325 int skip = 1;
326 size_t len = 0;
327 signed char *p = (signed char *)file + *pos;
328 char *s = line;
330 for (; *pos < size ; (*pos)++) {
331 if (skip && isspace(*p)) {
332 p++;
333 continue;
335 skip = 0;
336 if (*p != '\n' && (*pos < size)) {
337 len++;
338 *s++ = *p++;
339 if (len > 4095)
340 break; /* Too long, stop */
341 } else {
342 /* End of string */
343 *s = '\0';
344 return line;
347 /* End of buffer */
348 return NULL;
351 void release_file(void *file, unsigned long size)
353 munmap(file, size);
356 static int parse_elf(struct elf_info *info, const char *filename)
358 unsigned int i;
359 Elf_Ehdr *hdr;
360 Elf_Shdr *sechdrs;
361 Elf_Sym *sym;
363 hdr = grab_file(filename, &info->size);
364 if (!hdr) {
365 perror(filename);
366 exit(1);
368 info->hdr = hdr;
369 if (info->size < sizeof(*hdr)) {
370 /* file too small, assume this is an empty .o file */
371 return 0;
373 /* Is this a valid ELF file? */
374 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
375 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
376 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
377 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
378 /* Not an ELF file - silently ignore it */
379 return 0;
381 /* Fix endianness in ELF header */
382 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
383 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
384 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
385 hdr->e_machine = TO_NATIVE(hdr->e_machine);
386 hdr->e_type = TO_NATIVE(hdr->e_type);
387 sechdrs = (void *)hdr + hdr->e_shoff;
388 info->sechdrs = sechdrs;
390 /* Check if file offset is correct */
391 if (hdr->e_shoff > info->size) {
392 fatal("section header offset=%lu in file '%s' is bigger than "
393 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
394 filename, info->size);
395 return 0;
398 /* Fix endianness in section headers */
399 for (i = 0; i < hdr->e_shnum; i++) {
400 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
401 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
402 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
403 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
404 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
405 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
406 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
408 /* Find symbol table. */
409 for (i = 1; i < hdr->e_shnum; i++) {
410 const char *secstrings
411 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
412 const char *secname;
414 if (sechdrs[i].sh_offset > info->size) {
415 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
416 "sizeof(*hrd)=%zu\n", filename,
417 (unsigned long)sechdrs[i].sh_offset,
418 sizeof(*hdr));
419 return 0;
421 secname = secstrings + sechdrs[i].sh_name;
422 if (strcmp(secname, ".modinfo") == 0) {
423 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
424 info->modinfo_len = sechdrs[i].sh_size;
425 } else if (strcmp(secname, "__ksymtab") == 0)
426 info->export_sec = i;
427 else if (strcmp(secname, "__ksymtab_unused") == 0)
428 info->export_unused_sec = i;
429 else if (strcmp(secname, "__ksymtab_gpl") == 0)
430 info->export_gpl_sec = i;
431 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
432 info->export_unused_gpl_sec = i;
433 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
434 info->export_gpl_future_sec = i;
436 if (sechdrs[i].sh_type != SHT_SYMTAB)
437 continue;
439 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
440 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
441 + sechdrs[i].sh_size;
442 info->strtab = (void *)hdr +
443 sechdrs[sechdrs[i].sh_link].sh_offset;
445 if (!info->symtab_start)
446 fatal("%s has no symtab?\n", filename);
448 /* Fix endianness in symbols */
449 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
450 sym->st_shndx = TO_NATIVE(sym->st_shndx);
451 sym->st_name = TO_NATIVE(sym->st_name);
452 sym->st_value = TO_NATIVE(sym->st_value);
453 sym->st_size = TO_NATIVE(sym->st_size);
455 return 1;
458 static void parse_elf_finish(struct elf_info *info)
460 release_file(info->hdr, info->size);
463 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
464 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
466 static void handle_modversions(struct module *mod, struct elf_info *info,
467 Elf_Sym *sym, const char *symname)
469 unsigned int crc;
470 enum export export = export_from_sec(info, sym->st_shndx);
472 switch (sym->st_shndx) {
473 case SHN_COMMON:
474 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
475 break;
476 case SHN_ABS:
477 /* CRC'd symbol */
478 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
479 crc = (unsigned int) sym->st_value;
480 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
481 export);
483 break;
484 case SHN_UNDEF:
485 /* undefined symbol */
486 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
487 ELF_ST_BIND(sym->st_info) != STB_WEAK)
488 break;
489 /* ignore global offset table */
490 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
491 break;
492 /* ignore __this_module, it will be resolved shortly */
493 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
494 break;
495 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
496 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
497 /* add compatibility with older glibc */
498 #ifndef STT_SPARC_REGISTER
499 #define STT_SPARC_REGISTER STT_REGISTER
500 #endif
501 if (info->hdr->e_machine == EM_SPARC ||
502 info->hdr->e_machine == EM_SPARCV9) {
503 /* Ignore register directives. */
504 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
505 break;
506 if (symname[0] == '.') {
507 char *munged = strdup(symname);
508 munged[0] = '_';
509 munged[1] = toupper(munged[1]);
510 symname = munged;
513 #endif
515 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
516 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
517 mod->unres =
518 alloc_symbol(symname +
519 strlen(MODULE_SYMBOL_PREFIX),
520 ELF_ST_BIND(sym->st_info) == STB_WEAK,
521 mod->unres);
523 break;
524 default:
525 /* All exported symbols */
526 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
527 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
528 export);
530 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
531 mod->has_init = 1;
532 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
533 mod->has_cleanup = 1;
534 break;
539 * Parse tag=value strings from .modinfo section
541 static char *next_string(char *string, unsigned long *secsize)
543 /* Skip non-zero chars */
544 while (string[0]) {
545 string++;
546 if ((*secsize)-- <= 1)
547 return NULL;
550 /* Skip any zero padding. */
551 while (!string[0]) {
552 string++;
553 if ((*secsize)-- <= 1)
554 return NULL;
556 return string;
559 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
560 const char *tag, char *info)
562 char *p;
563 unsigned int taglen = strlen(tag);
564 unsigned long size = modinfo_len;
566 if (info) {
567 size -= info - (char *)modinfo;
568 modinfo = next_string(info, &size);
571 for (p = modinfo; p; p = next_string(p, &size)) {
572 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
573 return p + taglen + 1;
575 return NULL;
578 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
579 const char *tag)
582 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
586 * Test if string s ends in string sub
587 * return 0 if match
589 static int strrcmp(const char *s, const char *sub)
591 int slen, sublen;
593 if (!s || !sub)
594 return 1;
596 slen = strlen(s);
597 sublen = strlen(sub);
599 if ((slen == 0) || (sublen == 0))
600 return 1;
602 if (sublen > slen)
603 return 1;
605 return memcmp(s + slen - sublen, sub, sublen);
608 /* if sym is empty or point to a string
609 * like ".[0-9]+" then return 1.
610 * This is the optional prefix added by ld to some sections
612 static int number_prefix(const char *sym)
614 if (*sym++ == '\0')
615 return 1;
616 if (*sym != '.')
617 return 0;
618 do {
619 char c = *sym++;
620 if (c < '0' || c > '9')
621 return 0;
622 } while (*sym);
623 return 1;
626 /* The pattern is an array of simple patterns.
627 * "foo" will match an exact string equal to "foo"
628 * "*foo" will match a string that ends with "foo"
629 * "foo*" will match a string that begins with "foo"
630 * "foo$" will match a string equal to "foo" or "foo.1"
631 * where the '1' can be any number including several digits.
632 * The $ syntax is for sections where ld append a dot number
633 * to make section name unique.
635 int match(const char *sym, const char * const pat[])
637 const char *p;
638 while (*pat) {
639 p = *pat++;
640 const char *endp = p + strlen(p) - 1;
642 /* "*foo" */
643 if (*p == '*') {
644 if (strrcmp(sym, p + 1) == 0)
645 return 1;
647 /* "foo*" */
648 else if (*endp == '*') {
649 if (strncmp(sym, p, strlen(p) - 1) == 0)
650 return 1;
652 /* "foo$" */
653 else if (*endp == '$') {
654 if (strncmp(sym, p, strlen(p) - 1) == 0) {
655 if (number_prefix(sym + strlen(p) - 1))
656 return 1;
659 /* no wildcards */
660 else {
661 if (strcmp(p, sym) == 0)
662 return 1;
665 /* no match */
666 return 0;
669 /* sections that we do not want to do full section mismatch check on */
670 static const char *section_white_list[] =
671 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
673 #define ALL_INIT_DATA_SECTIONS \
674 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
675 #define ALL_EXIT_DATA_SECTIONS \
676 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
678 #define ALL_INIT_TEXT_SECTIONS \
679 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
680 #define ALL_EXIT_TEXT_SECTIONS \
681 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
683 #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
684 #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
686 #define DATA_SECTIONS ".data$", ".data.rel$"
687 #define TEXT_SECTIONS ".text$"
689 #define INIT_SECTIONS ".init.data$", ".init.text$"
690 #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
691 #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
692 #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
694 #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
695 #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
696 #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
697 #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
699 /* init data sections */
700 static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
702 /* all init sections */
703 static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
705 /* All init and exit sections (code + data) */
706 static const char *init_exit_sections[] =
707 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
709 /* data section */
710 static const char *data_sections[] = { DATA_SECTIONS, NULL };
712 /* sections that may refer to an init/exit section with no warning */
713 static const char *initref_sections[] =
715 ".text.init.refok*",
716 ".exit.text.refok*",
717 ".data.init.refok*",
718 NULL
722 /* symbols in .data that may refer to init/exit sections */
723 static const char *symbol_white_list[] =
725 "*driver",
726 "*_template", /* scsi uses *_template a lot */
727 "*_timer", /* arm uses ops structures named _timer a lot */
728 "*_sht", /* scsi also used *_sht to some extent */
729 "*_ops",
730 "*_probe",
731 "*_probe_one",
732 "*_console",
733 NULL
736 static const char *head_sections[] = { ".head.text*", NULL };
737 static const char *linker_symbols[] =
738 { "__init_begin", "_sinittext", "_einittext", NULL };
740 struct sectioncheck {
741 const char *fromsec[20];
742 const char *tosec[20];
745 const struct sectioncheck sectioncheck[] = {
746 /* Do not reference init/exit code/data from
747 * normal code and data
750 .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
751 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
753 /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
755 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
756 .tosec = { INIT_SECTIONS, NULL }
758 /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
760 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
761 .tosec = { EXIT_SECTIONS, NULL }
763 /* Do not use exit code/data from init code */
765 .fromsec = { ALL_INIT_SECTIONS, NULL },
766 .tosec = { ALL_EXIT_SECTIONS, NULL },
768 /* Do not use init code/data from exit code */
770 .fromsec = { ALL_EXIT_SECTIONS, NULL },
771 .tosec = { ALL_INIT_SECTIONS, NULL }
773 /* Do not export init/exit functions or data */
775 .fromsec = { "__ksymtab*", NULL },
776 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
780 static int section_mismatch(const char *fromsec, const char *tosec)
782 int i;
783 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
784 const struct sectioncheck *check = &sectioncheck[0];
786 for (i = 0; i < elems; i++) {
787 if (match(fromsec, check->fromsec) &&
788 match(tosec, check->tosec))
789 return 1;
790 check++;
792 return 0;
797 * Whitelist to allow certain references to pass with no warning.
799 * Pattern 0:
800 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
801 * The pattern is identified by:
802 * fromsec = .text.init.refok* | .data.init.refok*
804 * Pattern 1:
805 * If a module parameter is declared __initdata and permissions=0
806 * then this is legal despite the warning generated.
807 * We cannot see value of permissions here, so just ignore
808 * this pattern.
809 * The pattern is identified by:
810 * tosec = .init.data
811 * fromsec = .data*
812 * atsym =__param*
814 * Pattern 2:
815 * Many drivers utilise a *driver container with references to
816 * add, remove, probe functions etc.
817 * These functions may often be marked __init and we do not want to
818 * warn here.
819 * the pattern is identified by:
820 * tosec = init or exit section
821 * fromsec = data section
822 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
823 * *probe_one, *_console, *_timer
825 * Pattern 3:
826 * Whitelist all refereces from .text.head to .init.data
827 * Whitelist all refereces from .text.head to .init.text
829 * Pattern 4:
830 * Some symbols belong to init section but still it is ok to reference
831 * these from non-init sections as these symbols don't have any memory
832 * allocated for them and symbol address and value are same. So even
833 * if init section is freed, its ok to reference those symbols.
834 * For ex. symbols marking the init section boundaries.
835 * This pattern is identified by
836 * refsymname = __init_begin, _sinittext, _einittext
839 static int secref_whitelist(const char *modname, const char *tosec,
840 const char *fromsec, const char *atsym,
841 const char *refsymname)
843 /* Check for pattern 0 */
844 if (match(fromsec, initref_sections))
845 return 1;
847 /* Check for pattern 1 */
848 if (match(tosec, init_data_sections) &&
849 match(fromsec, data_sections) &&
850 (strncmp(atsym, "__param", strlen("__param")) == 0))
851 return 1;
853 /* Check for pattern 2 */
854 if (match(tosec, init_exit_sections) &&
855 match(fromsec, data_sections) &&
856 match(atsym, symbol_white_list))
857 return 1;
859 /* Check for pattern 3 */
860 if (match(fromsec, head_sections) &&
861 match(tosec, init_sections))
862 return 1;
864 /* Check for pattern 4 */
865 if (match(refsymname, linker_symbols))
866 return 1;
868 return 0;
872 * Find symbol based on relocation record info.
873 * In some cases the symbol supplied is a valid symbol so
874 * return refsym. If st_name != 0 we assume this is a valid symbol.
875 * In other cases the symbol needs to be looked up in the symbol table
876 * based on section and address.
877 * **/
878 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
879 Elf_Sym *relsym)
881 Elf_Sym *sym;
882 Elf_Sym *near = NULL;
883 Elf64_Sword distance = 20;
884 Elf64_Sword d;
886 if (relsym->st_name != 0)
887 return relsym;
888 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
889 if (sym->st_shndx != relsym->st_shndx)
890 continue;
891 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
892 continue;
893 if (sym->st_value == addr)
894 return sym;
895 /* Find a symbol nearby - addr are maybe negative */
896 d = sym->st_value - addr;
897 if (d < 0)
898 d = addr - sym->st_value;
899 if (d < distance) {
900 distance = d;
901 near = sym;
904 /* We need a close match */
905 if (distance < 20)
906 return near;
907 else
908 return NULL;
911 static inline int is_arm_mapping_symbol(const char *str)
913 return str[0] == '$' && strchr("atd", str[1])
914 && (str[2] == '\0' || str[2] == '.');
918 * If there's no name there, ignore it; likewise, ignore it if it's
919 * one of the magic symbols emitted used by current ARM tools.
921 * Otherwise if find_symbols_between() returns those symbols, they'll
922 * fail the whitelist tests and cause lots of false alarms ... fixable
923 * only by merging __exit and __init sections into __text, bloating
924 * the kernel (which is especially evil on embedded platforms).
926 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
928 const char *name = elf->strtab + sym->st_name;
930 if (!name || !strlen(name))
931 return 0;
932 return !is_arm_mapping_symbol(name);
936 * Find symbols before or equal addr and after addr - in the section sec.
937 * If we find two symbols with equal offset prefer one with a valid name.
938 * The ELF format may have a better way to detect what type of symbol
939 * it is, but this works for now.
941 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
942 const char *sec)
944 Elf_Sym *sym;
945 Elf_Sym *near = NULL;
946 Elf_Ehdr *hdr = elf->hdr;
947 Elf_Addr distance = ~0;
948 const char *secstrings = (void *)hdr +
949 elf->sechdrs[hdr->e_shstrndx].sh_offset;
951 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
952 const char *symsec;
954 if (sym->st_shndx >= SHN_LORESERVE)
955 continue;
956 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
957 if (strcmp(symsec, sec) != 0)
958 continue;
959 if (!is_valid_name(elf, sym))
960 continue;
961 if (sym->st_value <= addr) {
962 if ((addr - sym->st_value) < distance) {
963 distance = addr - sym->st_value;
964 near = sym;
965 } else if ((addr - sym->st_value) == distance) {
966 near = sym;
970 return near;
974 * Print a warning about a section mismatch.
975 * Try to find symbols near it so user can find it.
976 * Check whitelist before warning - it may be a false positive.
978 static void warn_sec_mismatch(const char *modname, const char *fromsec,
979 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
981 const char *refsymname = "";
982 Elf_Sym *where;
983 Elf_Sym *refsym;
984 Elf_Ehdr *hdr = elf->hdr;
985 Elf_Shdr *sechdrs = elf->sechdrs;
986 const char *secstrings = (void *)hdr +
987 sechdrs[hdr->e_shstrndx].sh_offset;
988 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
990 where = find_elf_symbol2(elf, r.r_offset, fromsec);
992 refsym = find_elf_symbol(elf, r.r_addend, sym);
993 if (refsym && strlen(elf->strtab + refsym->st_name))
994 refsymname = elf->strtab + refsym->st_name;
996 /* check whitelist - we may ignore it */
997 if (secref_whitelist(modname, secname, fromsec,
998 where ? elf->strtab + where->st_name : "",
999 refsymname))
1000 return;
1002 if (where) {
1003 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
1004 "in '%s'\n",
1005 modname, fromsec, (unsigned long long)r.r_offset,
1006 secname, refsymname, elf->strtab + where->st_name);
1007 } else {
1008 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
1009 modname, fromsec, (unsigned long long)r.r_offset,
1010 secname, refsymname);
1014 static unsigned int *reloc_location(struct elf_info *elf,
1015 Elf_Shdr *sechdr, Elf_Rela *r)
1017 Elf_Shdr *sechdrs = elf->sechdrs;
1018 int section = sechdr->sh_info;
1020 return (void *)elf->hdr + sechdrs[section].sh_offset +
1021 (r->r_offset - sechdrs[section].sh_addr);
1024 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1026 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1027 unsigned int *location = reloc_location(elf, sechdr, r);
1029 switch (r_typ) {
1030 case R_386_32:
1031 r->r_addend = TO_NATIVE(*location);
1032 break;
1033 case R_386_PC32:
1034 r->r_addend = TO_NATIVE(*location) + 4;
1035 /* For CONFIG_RELOCATABLE=y */
1036 if (elf->hdr->e_type == ET_EXEC)
1037 r->r_addend += r->r_offset;
1038 break;
1040 return 0;
1043 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1045 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1047 switch (r_typ) {
1048 case R_ARM_ABS32:
1049 /* From ARM ABI: (S + A) | T */
1050 r->r_addend = (int)(long)
1051 (elf->symtab_start + ELF_R_SYM(r->r_info));
1052 break;
1053 case R_ARM_PC24:
1054 /* From ARM ABI: ((S + A) | T) - P */
1055 r->r_addend = (int)(long)(elf->hdr +
1056 sechdr->sh_offset +
1057 (r->r_offset - sechdr->sh_addr));
1058 break;
1059 default:
1060 return 1;
1062 return 0;
1065 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1067 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1068 unsigned int *location = reloc_location(elf, sechdr, r);
1069 unsigned int inst;
1071 if (r_typ == R_MIPS_HI16)
1072 return 1; /* skip this */
1073 inst = TO_NATIVE(*location);
1074 switch (r_typ) {
1075 case R_MIPS_LO16:
1076 r->r_addend = inst & 0xffff;
1077 break;
1078 case R_MIPS_26:
1079 r->r_addend = (inst & 0x03ffffff) << 2;
1080 break;
1081 case R_MIPS_32:
1082 r->r_addend = inst;
1083 break;
1085 return 0;
1088 static void section_rela(const char *modname, struct elf_info *elf,
1089 Elf_Shdr *sechdr)
1091 Elf_Sym *sym;
1092 Elf_Rela *rela;
1093 Elf_Rela r;
1094 unsigned int r_sym;
1095 const char *fromsec;
1096 const char * tosec;
1098 Elf_Ehdr *hdr = elf->hdr;
1099 Elf_Rela *start = (void *)hdr + sechdr->sh_offset;
1100 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1102 const char *secstrings = (void *)hdr +
1103 elf->sechdrs[hdr->e_shstrndx].sh_offset;
1105 fromsec = secstrings + sechdr->sh_name;
1106 fromsec += strlen(".rela");
1107 /* if from section (name) is know good then skip it */
1108 if (match(fromsec, section_white_list))
1109 return;
1111 for (rela = start; rela < stop; rela++) {
1112 r.r_offset = TO_NATIVE(rela->r_offset);
1113 #if KERNEL_ELFCLASS == ELFCLASS64
1114 if (hdr->e_machine == EM_MIPS) {
1115 unsigned int r_typ;
1116 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1117 r_sym = TO_NATIVE(r_sym);
1118 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1119 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1120 } else {
1121 r.r_info = TO_NATIVE(rela->r_info);
1122 r_sym = ELF_R_SYM(r.r_info);
1124 #else
1125 r.r_info = TO_NATIVE(rela->r_info);
1126 r_sym = ELF_R_SYM(r.r_info);
1127 #endif
1128 r.r_addend = TO_NATIVE(rela->r_addend);
1129 sym = elf->symtab_start + r_sym;
1130 /* Skip special sections */
1131 if (sym->st_shndx >= SHN_LORESERVE)
1132 continue;
1134 tosec = secstrings +
1135 elf->sechdrs[sym->st_shndx].sh_name;
1136 if (section_mismatch(fromsec, tosec))
1137 warn_sec_mismatch(modname, fromsec, elf, sym, r);
1141 static void section_rel(const char *modname, struct elf_info *elf,
1142 Elf_Shdr *sechdr)
1144 Elf_Sym *sym;
1145 Elf_Rel *rel;
1146 Elf_Rela r;
1147 unsigned int r_sym;
1148 const char *fromsec;
1149 const char * tosec;
1151 Elf_Ehdr *hdr = elf->hdr;
1152 Elf_Rel *start = (void *)hdr + sechdr->sh_offset;
1153 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1155 const char *secstrings = (void *)hdr +
1156 elf->sechdrs[hdr->e_shstrndx].sh_offset;
1158 fromsec = secstrings + sechdr->sh_name;
1159 fromsec += strlen(".rel");
1160 /* if from section (name) is know good then skip it */
1161 if (match(fromsec, section_white_list))
1162 return;
1164 for (rel = start; rel < stop; rel++) {
1165 r.r_offset = TO_NATIVE(rel->r_offset);
1166 #if KERNEL_ELFCLASS == ELFCLASS64
1167 if (hdr->e_machine == EM_MIPS) {
1168 unsigned int r_typ;
1169 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1170 r_sym = TO_NATIVE(r_sym);
1171 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1172 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1173 } else {
1174 r.r_info = TO_NATIVE(rel->r_info);
1175 r_sym = ELF_R_SYM(r.r_info);
1177 #else
1178 r.r_info = TO_NATIVE(rel->r_info);
1179 r_sym = ELF_R_SYM(r.r_info);
1180 #endif
1181 r.r_addend = 0;
1182 switch (hdr->e_machine) {
1183 case EM_386:
1184 if (addend_386_rel(elf, sechdr, &r))
1185 continue;
1186 break;
1187 case EM_ARM:
1188 if (addend_arm_rel(elf, sechdr, &r))
1189 continue;
1190 break;
1191 case EM_MIPS:
1192 if (addend_mips_rel(elf, sechdr, &r))
1193 continue;
1194 break;
1196 sym = elf->symtab_start + r_sym;
1197 /* Skip special sections */
1198 if (sym->st_shndx >= SHN_LORESERVE)
1199 continue;
1201 tosec = secstrings +
1202 elf->sechdrs[sym->st_shndx].sh_name;
1203 if (section_mismatch(fromsec, tosec))
1204 warn_sec_mismatch(modname, fromsec, elf, sym, r);
1209 * A module includes a number of sections that are discarded
1210 * either when loaded or when used as built-in.
1211 * For loaded modules all functions marked __init and all data
1212 * marked __initdata will be discarded when the module has been intialized.
1213 * Likewise for modules used built-in the sections marked __exit
1214 * are discarded because __exit marked function are supposed to be called
1215 * only when a moduel is unloaded which never happes for built-in modules.
1216 * The check_sec_ref() function traverses all relocation records
1217 * to find all references to a section that reference a section that will
1218 * be discarded and warns about it.
1220 static void check_sec_ref(struct module *mod, const char *modname,
1221 struct elf_info *elf)
1223 int i;
1224 Elf_Ehdr *hdr = elf->hdr;
1225 Elf_Shdr *sechdrs = elf->sechdrs;
1227 /* Walk through all sections */
1228 for (i = 0; i < hdr->e_shnum; i++) {
1229 /* We want to process only relocation sections and not .init */
1230 if (sechdrs[i].sh_type == SHT_RELA)
1231 section_rela(modname, elf, &elf->sechdrs[i]);
1232 else if (sechdrs[i].sh_type == SHT_REL)
1233 section_rel(modname, elf, &elf->sechdrs[i]);
1237 static void read_symbols(char *modname)
1239 const char *symname;
1240 char *version;
1241 char *license;
1242 struct module *mod;
1243 struct elf_info info = { };
1244 Elf_Sym *sym;
1246 if (!parse_elf(&info, modname))
1247 return;
1249 mod = new_module(modname);
1251 /* When there's no vmlinux, don't print warnings about
1252 * unresolved symbols (since there'll be too many ;) */
1253 if (is_vmlinux(modname)) {
1254 have_vmlinux = 1;
1255 mod->skip = 1;
1258 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1259 while (license) {
1260 if (license_is_gpl_compatible(license))
1261 mod->gpl_compatible = 1;
1262 else {
1263 mod->gpl_compatible = 0;
1264 break;
1266 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1267 "license", license);
1270 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1271 symname = info.strtab + sym->st_name;
1273 handle_modversions(mod, &info, sym, symname);
1274 handle_moddevtable(mod, &info, sym, symname);
1276 if (!is_vmlinux(modname) ||
1277 (is_vmlinux(modname) && vmlinux_section_warnings))
1278 check_sec_ref(mod, modname, &info);
1280 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1281 if (version)
1282 maybe_frob_rcs_version(modname, version, info.modinfo,
1283 version - (char *)info.hdr);
1284 if (version || (all_versions && !is_vmlinux(modname)))
1285 get_src_version(modname, mod->srcversion,
1286 sizeof(mod->srcversion)-1);
1288 parse_elf_finish(&info);
1290 /* Our trick to get versioning for struct_module - it's
1291 * never passed as an argument to an exported function, so
1292 * the automatic versioning doesn't pick it up, but it's really
1293 * important anyhow */
1294 if (modversions)
1295 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1298 #define SZ 500
1300 /* We first write the generated file into memory using the
1301 * following helper, then compare to the file on disk and
1302 * only update the later if anything changed */
1304 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1305 const char *fmt, ...)
1307 char tmp[SZ];
1308 int len;
1309 va_list ap;
1311 va_start(ap, fmt);
1312 len = vsnprintf(tmp, SZ, fmt, ap);
1313 buf_write(buf, tmp, len);
1314 va_end(ap);
1317 void buf_write(struct buffer *buf, const char *s, int len)
1319 if (buf->size - buf->pos < len) {
1320 buf->size += len + SZ;
1321 buf->p = realloc(buf->p, buf->size);
1323 strncpy(buf->p + buf->pos, s, len);
1324 buf->pos += len;
1327 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1329 const char *e = is_vmlinux(m) ?"":".ko";
1331 switch (exp) {
1332 case export_gpl:
1333 fatal("modpost: GPL-incompatible module %s%s "
1334 "uses GPL-only symbol '%s'\n", m, e, s);
1335 break;
1336 case export_unused_gpl:
1337 fatal("modpost: GPL-incompatible module %s%s "
1338 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1339 break;
1340 case export_gpl_future:
1341 warn("modpost: GPL-incompatible module %s%s "
1342 "uses future GPL-only symbol '%s'\n", m, e, s);
1343 break;
1344 case export_plain:
1345 case export_unused:
1346 case export_unknown:
1347 /* ignore */
1348 break;
1352 static void check_for_unused(enum export exp, const char *m, const char *s)
1354 const char *e = is_vmlinux(m) ?"":".ko";
1356 switch (exp) {
1357 case export_unused:
1358 case export_unused_gpl:
1359 warn("modpost: module %s%s "
1360 "uses symbol '%s' marked UNUSED\n", m, e, s);
1361 break;
1362 default:
1363 /* ignore */
1364 break;
1368 static void check_exports(struct module *mod)
1370 struct symbol *s, *exp;
1372 for (s = mod->unres; s; s = s->next) {
1373 const char *basename;
1374 exp = find_symbol(s->name);
1375 if (!exp || exp->module == mod)
1376 continue;
1377 basename = strrchr(mod->name, '/');
1378 if (basename)
1379 basename++;
1380 else
1381 basename = mod->name;
1382 if (!mod->gpl_compatible)
1383 check_for_gpl_usage(exp->export, basename, exp->name);
1384 check_for_unused(exp->export, basename, exp->name);
1389 * Header for the generated file
1391 static void add_header(struct buffer *b, struct module *mod)
1393 buf_printf(b, "#include <linux/module.h>\n");
1394 buf_printf(b, "#include <linux/vermagic.h>\n");
1395 buf_printf(b, "#include <linux/compiler.h>\n");
1396 buf_printf(b, "\n");
1397 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1398 buf_printf(b, "\n");
1399 buf_printf(b, "struct module __this_module\n");
1400 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1401 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1402 if (mod->has_init)
1403 buf_printf(b, " .init = init_module,\n");
1404 if (mod->has_cleanup)
1405 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1406 " .exit = cleanup_module,\n"
1407 "#endif\n");
1408 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1409 buf_printf(b, "};\n");
1413 * Record CRCs for unresolved symbols
1415 static int add_versions(struct buffer *b, struct module *mod)
1417 struct symbol *s, *exp;
1418 int err = 0;
1420 for (s = mod->unres; s; s = s->next) {
1421 exp = find_symbol(s->name);
1422 if (!exp || exp->module == mod) {
1423 if (have_vmlinux && !s->weak) {
1424 if (warn_unresolved) {
1425 warn("\"%s\" [%s.ko] undefined!\n",
1426 s->name, mod->name);
1427 } else {
1428 merror("\"%s\" [%s.ko] undefined!\n",
1429 s->name, mod->name);
1430 err = 1;
1433 continue;
1435 s->module = exp->module;
1436 s->crc_valid = exp->crc_valid;
1437 s->crc = exp->crc;
1440 if (!modversions)
1441 return err;
1443 buf_printf(b, "\n");
1444 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1445 buf_printf(b, "__attribute_used__\n");
1446 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1448 for (s = mod->unres; s; s = s->next) {
1449 if (!s->module)
1450 continue;
1451 if (!s->crc_valid) {
1452 warn("\"%s\" [%s.ko] has no CRC!\n",
1453 s->name, mod->name);
1454 continue;
1456 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1459 buf_printf(b, "};\n");
1461 return err;
1464 static void add_depends(struct buffer *b, struct module *mod,
1465 struct module *modules)
1467 struct symbol *s;
1468 struct module *m;
1469 int first = 1;
1471 for (m = modules; m; m = m->next)
1472 m->seen = is_vmlinux(m->name);
1474 buf_printf(b, "\n");
1475 buf_printf(b, "static const char __module_depends[]\n");
1476 buf_printf(b, "__attribute_used__\n");
1477 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1478 buf_printf(b, "\"depends=");
1479 for (s = mod->unres; s; s = s->next) {
1480 const char *p;
1481 if (!s->module)
1482 continue;
1484 if (s->module->seen)
1485 continue;
1487 s->module->seen = 1;
1488 p = strrchr(s->module->name, '/');
1489 if (p)
1490 p++;
1491 else
1492 p = s->module->name;
1493 buf_printf(b, "%s%s", first ? "" : ",", p);
1494 first = 0;
1496 buf_printf(b, "\";\n");
1499 static void add_srcversion(struct buffer *b, struct module *mod)
1501 if (mod->srcversion[0]) {
1502 buf_printf(b, "\n");
1503 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1504 mod->srcversion);
1508 static void write_if_changed(struct buffer *b, const char *fname)
1510 char *tmp;
1511 FILE *file;
1512 struct stat st;
1514 file = fopen(fname, "r");
1515 if (!file)
1516 goto write;
1518 if (fstat(fileno(file), &st) < 0)
1519 goto close_write;
1521 if (st.st_size != b->pos)
1522 goto close_write;
1524 tmp = NOFAIL(malloc(b->pos));
1525 if (fread(tmp, 1, b->pos, file) != b->pos)
1526 goto free_write;
1528 if (memcmp(tmp, b->p, b->pos) != 0)
1529 goto free_write;
1531 free(tmp);
1532 fclose(file);
1533 return;
1535 free_write:
1536 free(tmp);
1537 close_write:
1538 fclose(file);
1539 write:
1540 file = fopen(fname, "w");
1541 if (!file) {
1542 perror(fname);
1543 exit(1);
1545 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1546 perror(fname);
1547 exit(1);
1549 fclose(file);
1552 /* parse Module.symvers file. line format:
1553 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1555 static void read_dump(const char *fname, unsigned int kernel)
1557 unsigned long size, pos = 0;
1558 void *file = grab_file(fname, &size);
1559 char *line;
1561 if (!file)
1562 /* No symbol versions, silently ignore */
1563 return;
1565 while ((line = get_next_line(&pos, file, size))) {
1566 char *symname, *modname, *d, *export, *end;
1567 unsigned int crc;
1568 struct module *mod;
1569 struct symbol *s;
1571 if (!(symname = strchr(line, '\t')))
1572 goto fail;
1573 *symname++ = '\0';
1574 if (!(modname = strchr(symname, '\t')))
1575 goto fail;
1576 *modname++ = '\0';
1577 if ((export = strchr(modname, '\t')) != NULL)
1578 *export++ = '\0';
1579 if (export && ((end = strchr(export, '\t')) != NULL))
1580 *end = '\0';
1581 crc = strtoul(line, &d, 16);
1582 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1583 goto fail;
1584 mod = find_module(modname);
1585 if (!mod) {
1586 if (is_vmlinux(modname))
1587 have_vmlinux = 1;
1588 mod = new_module(NOFAIL(strdup(modname)));
1589 mod->skip = 1;
1591 s = sym_add_exported(symname, mod, export_no(export));
1592 s->kernel = kernel;
1593 s->preloaded = 1;
1594 sym_update_crc(symname, mod, crc, export_no(export));
1596 return;
1597 fail:
1598 fatal("parse error in symbol dump file\n");
1601 /* For normal builds always dump all symbols.
1602 * For external modules only dump symbols
1603 * that are not read from kernel Module.symvers.
1605 static int dump_sym(struct symbol *sym)
1607 if (!external_module)
1608 return 1;
1609 if (sym->vmlinux || sym->kernel)
1610 return 0;
1611 return 1;
1614 static void write_dump(const char *fname)
1616 struct buffer buf = { };
1617 struct symbol *symbol;
1618 int n;
1620 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1621 symbol = symbolhash[n];
1622 while (symbol) {
1623 if (dump_sym(symbol))
1624 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1625 symbol->crc, symbol->name,
1626 symbol->module->name,
1627 export_str(symbol->export));
1628 symbol = symbol->next;
1631 write_if_changed(&buf, fname);
1634 int main(int argc, char **argv)
1636 struct module *mod;
1637 struct buffer buf = { };
1638 char *kernel_read = NULL, *module_read = NULL;
1639 char *dump_write = NULL;
1640 int opt;
1641 int err;
1643 while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
1644 switch (opt) {
1645 case 'i':
1646 kernel_read = optarg;
1647 break;
1648 case 'I':
1649 module_read = optarg;
1650 external_module = 1;
1651 break;
1652 case 'm':
1653 modversions = 1;
1654 break;
1655 case 'o':
1656 dump_write = optarg;
1657 break;
1658 case 'a':
1659 all_versions = 1;
1660 break;
1661 case 's':
1662 vmlinux_section_warnings = 0;
1663 break;
1664 case 'w':
1665 warn_unresolved = 1;
1666 break;
1667 default:
1668 exit(1);
1672 if (kernel_read)
1673 read_dump(kernel_read, 1);
1674 if (module_read)
1675 read_dump(module_read, 0);
1677 while (optind < argc)
1678 read_symbols(argv[optind++]);
1680 for (mod = modules; mod; mod = mod->next) {
1681 if (mod->skip)
1682 continue;
1683 check_exports(mod);
1686 err = 0;
1688 for (mod = modules; mod; mod = mod->next) {
1689 char fname[strlen(mod->name) + 10];
1691 if (mod->skip)
1692 continue;
1694 buf.pos = 0;
1696 add_header(&buf, mod);
1697 err |= add_versions(&buf, mod);
1698 add_depends(&buf, mod, modules);
1699 add_moddevtable(&buf, mod);
1700 add_srcversion(&buf, mod);
1702 sprintf(fname, "%s.mod.c", mod->name);
1703 write_if_changed(&buf, fname);
1706 if (dump_write)
1707 write_dump(dump_write);
1709 return err;