Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / scripts / mod / modpost.c
blob9b9f94c915d234365c14444872c5ea5e1163bd6d
1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
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"
17 /* Are we using CONFIG_MODVERSIONS? */
18 int modversions = 0;
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
20 int have_vmlinux = 0;
21 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
22 static int all_versions = 0;
24 void
25 fatal(const char *fmt, ...)
27 va_list arglist;
29 fprintf(stderr, "FATAL: ");
31 va_start(arglist, fmt);
32 vfprintf(stderr, fmt, arglist);
33 va_end(arglist);
35 exit(1);
38 void
39 warn(const char *fmt, ...)
41 va_list arglist;
43 fprintf(stderr, "WARNING: ");
45 va_start(arglist, fmt);
46 vfprintf(stderr, fmt, arglist);
47 va_end(arglist);
50 void *do_nofail(void *ptr, const char *expr)
52 if (!ptr) {
53 fatal("modpost: Memory allocation failure: %s.\n", expr);
55 return ptr;
58 /* A list of all modules we processed */
60 static struct module *modules;
62 struct module *
63 find_module(char *modname)
65 struct module *mod;
67 for (mod = modules; mod; mod = mod->next)
68 if (strcmp(mod->name, modname) == 0)
69 break;
70 return mod;
73 struct module *
74 new_module(char *modname)
76 struct module *mod;
77 char *p, *s;
79 mod = NOFAIL(malloc(sizeof(*mod)));
80 memset(mod, 0, sizeof(*mod));
81 p = NOFAIL(strdup(modname));
83 /* strip trailing .o */
84 if ((s = strrchr(p, '.')) != NULL)
85 if (strcmp(s, ".o") == 0)
86 *s = '\0';
88 /* add to list */
89 mod->name = p;
90 mod->next = modules;
91 modules = mod;
93 return mod;
96 /* A hash of all exported symbols,
97 * struct symbol is also used for lists of unresolved symbols */
99 #define SYMBOL_HASH_SIZE 1024
101 struct symbol {
102 struct symbol *next;
103 struct module *module;
104 unsigned int crc;
105 int crc_valid;
106 unsigned int weak:1;
107 char name[0];
110 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
112 /* This is based on the hash agorithm from gdbm, via tdb */
113 static inline unsigned int tdb_hash(const char *name)
115 unsigned value; /* Used to compute the hash value. */
116 unsigned i; /* Used to cycle through random values. */
118 /* Set the initial value from the key size. */
119 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
120 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
122 return (1103515243 * value + 12345);
125 /* Allocate a new symbols for use in the hash of exported symbols or
126 * the list of unresolved symbols per module */
128 struct symbol *
129 alloc_symbol(const char *name, unsigned int weak, struct symbol *next)
131 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
133 memset(s, 0, sizeof(*s));
134 strcpy(s->name, name);
135 s->weak = weak;
136 s->next = next;
137 return s;
140 /* For the hash of exported symbols */
142 void
143 new_symbol(const char *name, struct module *module, unsigned int *crc)
145 unsigned int hash;
146 struct symbol *new;
148 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
149 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
150 new->module = module;
151 if (crc) {
152 new->crc = *crc;
153 new->crc_valid = 1;
157 struct symbol *
158 find_symbol(const char *name)
160 struct symbol *s;
162 /* For our purposes, .foo matches foo. PPC64 needs this. */
163 if (name[0] == '.')
164 name++;
166 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
167 if (strcmp(s->name, name) == 0)
168 return s;
170 return NULL;
173 /* Add an exported symbol - it may have already been added without a
174 * CRC, in this case just update the CRC */
175 void
176 add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
178 struct symbol *s = find_symbol(name);
180 if (!s) {
181 new_symbol(name, module, crc);
182 return;
184 if (crc) {
185 s->crc = *crc;
186 s->crc_valid = 1;
190 void *
191 grab_file(const char *filename, unsigned long *size)
193 struct stat st;
194 void *map;
195 int fd;
197 fd = open(filename, O_RDONLY);
198 if (fd < 0 || fstat(fd, &st) != 0)
199 return NULL;
201 *size = st.st_size;
202 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
203 close(fd);
205 if (map == MAP_FAILED)
206 return NULL;
207 return map;
211 Return a copy of the next line in a mmap'ed file.
212 spaces in the beginning of the line is trimmed away.
213 Return a pointer to a static buffer.
215 char*
216 get_next_line(unsigned long *pos, void *file, unsigned long size)
218 static char line[4096];
219 int skip = 1;
220 size_t len = 0;
221 signed char *p = (signed char *)file + *pos;
222 char *s = line;
224 for (; *pos < size ; (*pos)++)
226 if (skip && isspace(*p)) {
227 p++;
228 continue;
230 skip = 0;
231 if (*p != '\n' && (*pos < size)) {
232 len++;
233 *s++ = *p++;
234 if (len > 4095)
235 break; /* Too long, stop */
236 } else {
237 /* End of string */
238 *s = '\0';
239 return line;
242 /* End of buffer */
243 return NULL;
246 void
247 release_file(void *file, unsigned long size)
249 munmap(file, size);
252 void
253 parse_elf(struct elf_info *info, const char *filename)
255 unsigned int i;
256 Elf_Ehdr *hdr = info->hdr;
257 Elf_Shdr *sechdrs;
258 Elf_Sym *sym;
260 hdr = grab_file(filename, &info->size);
261 if (!hdr) {
262 perror(filename);
263 abort();
265 info->hdr = hdr;
266 if (info->size < sizeof(*hdr))
267 goto truncated;
269 /* Fix endianness in ELF header */
270 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
271 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
272 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
273 hdr->e_machine = TO_NATIVE(hdr->e_machine);
274 sechdrs = (void *)hdr + hdr->e_shoff;
275 info->sechdrs = sechdrs;
277 /* Fix endianness in section headers */
278 for (i = 0; i < hdr->e_shnum; i++) {
279 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
280 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
281 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
282 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
283 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
285 /* Find symbol table. */
286 for (i = 1; i < hdr->e_shnum; i++) {
287 const char *secstrings
288 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
290 if (sechdrs[i].sh_offset > info->size)
291 goto truncated;
292 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
293 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
294 info->modinfo_len = sechdrs[i].sh_size;
296 if (sechdrs[i].sh_type != SHT_SYMTAB)
297 continue;
299 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
300 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
301 + sechdrs[i].sh_size;
302 info->strtab = (void *)hdr +
303 sechdrs[sechdrs[i].sh_link].sh_offset;
305 if (!info->symtab_start) {
306 fprintf(stderr, "modpost: %s no symtab?\n", filename);
307 abort();
309 /* Fix endianness in symbols */
310 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
311 sym->st_shndx = TO_NATIVE(sym->st_shndx);
312 sym->st_name = TO_NATIVE(sym->st_name);
313 sym->st_value = TO_NATIVE(sym->st_value);
314 sym->st_size = TO_NATIVE(sym->st_size);
316 return;
318 truncated:
319 fprintf(stderr, "modpost: %s is truncated.\n", filename);
320 abort();
323 void
324 parse_elf_finish(struct elf_info *info)
326 release_file(info->hdr, info->size);
329 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
330 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
332 void
333 handle_modversions(struct module *mod, struct elf_info *info,
334 Elf_Sym *sym, const char *symname)
336 unsigned int crc;
338 switch (sym->st_shndx) {
339 case SHN_COMMON:
340 fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n",
341 symname, mod->name);
342 break;
343 case SHN_ABS:
344 /* CRC'd symbol */
345 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
346 crc = (unsigned int) sym->st_value;
347 add_exported_symbol(symname + strlen(CRC_PFX),
348 mod, &crc);
350 break;
351 case SHN_UNDEF:
352 /* undefined symbol */
353 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
354 ELF_ST_BIND(sym->st_info) != STB_WEAK)
355 break;
356 /* ignore global offset table */
357 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
358 break;
359 /* ignore __this_module, it will be resolved shortly */
360 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
361 break;
362 #ifdef STT_REGISTER
363 if (info->hdr->e_machine == EM_SPARC ||
364 info->hdr->e_machine == EM_SPARCV9) {
365 /* Ignore register directives. */
366 if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
367 break;
369 #endif
371 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
372 strlen(MODULE_SYMBOL_PREFIX)) == 0)
373 mod->unres = alloc_symbol(symname +
374 strlen(MODULE_SYMBOL_PREFIX),
375 ELF_ST_BIND(sym->st_info) == STB_WEAK,
376 mod->unres);
377 break;
378 default:
379 /* All exported symbols */
380 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
381 add_exported_symbol(symname + strlen(KSYMTAB_PFX),
382 mod, NULL);
384 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
385 mod->has_init = 1;
386 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
387 mod->has_cleanup = 1;
388 break;
393 is_vmlinux(const char *modname)
395 const char *myname;
397 if ((myname = strrchr(modname, '/')))
398 myname++;
399 else
400 myname = modname;
402 return strcmp(myname, "vmlinux") == 0;
405 /* Parse tag=value strings from .modinfo section */
406 static char *next_string(char *string, unsigned long *secsize)
408 /* Skip non-zero chars */
409 while (string[0]) {
410 string++;
411 if ((*secsize)-- <= 1)
412 return NULL;
415 /* Skip any zero padding. */
416 while (!string[0]) {
417 string++;
418 if ((*secsize)-- <= 1)
419 return NULL;
421 return string;
424 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
425 const char *tag)
427 char *p;
428 unsigned int taglen = strlen(tag);
429 unsigned long size = modinfo_len;
431 for (p = modinfo; p; p = next_string(p, &size)) {
432 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
433 return p + taglen + 1;
435 return NULL;
438 void
439 read_symbols(char *modname)
441 const char *symname;
442 char *version;
443 struct module *mod;
444 struct elf_info info = { };
445 Elf_Sym *sym;
447 parse_elf(&info, modname);
449 mod = new_module(modname);
451 /* When there's no vmlinux, don't print warnings about
452 * unresolved symbols (since there'll be too many ;) */
453 if (is_vmlinux(modname)) {
454 unsigned int fake_crc = 0;
455 have_vmlinux = 1;
456 add_exported_symbol("struct_module", mod, &fake_crc);
457 mod->skip = 1;
460 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
461 symname = info.strtab + sym->st_name;
463 handle_modversions(mod, &info, sym, symname);
464 handle_moddevtable(mod, &info, sym, symname);
467 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
468 if (version)
469 maybe_frob_rcs_version(modname, version, info.modinfo,
470 version - (char *)info.hdr);
471 if (version || (all_versions && !is_vmlinux(modname)))
472 get_src_version(modname, mod->srcversion,
473 sizeof(mod->srcversion)-1);
475 parse_elf_finish(&info);
477 /* Our trick to get versioning for struct_module - it's
478 * never passed as an argument to an exported function, so
479 * the automatic versioning doesn't pick it up, but it's really
480 * important anyhow */
481 if (modversions)
482 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
485 #define SZ 500
487 /* We first write the generated file into memory using the
488 * following helper, then compare to the file on disk and
489 * only update the later if anything changed */
491 void __attribute__((format(printf, 2, 3)))
492 buf_printf(struct buffer *buf, const char *fmt, ...)
494 char tmp[SZ];
495 int len;
496 va_list ap;
498 va_start(ap, fmt);
499 len = vsnprintf(tmp, SZ, fmt, ap);
500 if (buf->size - buf->pos < len + 1) {
501 buf->size += 128;
502 buf->p = realloc(buf->p, buf->size);
504 strncpy(buf->p + buf->pos, tmp, len + 1);
505 buf->pos += len;
506 va_end(ap);
509 void
510 buf_write(struct buffer *buf, const char *s, int len)
512 if (buf->size - buf->pos < len) {
513 buf->size += len;
514 buf->p = realloc(buf->p, buf->size);
516 strncpy(buf->p + buf->pos, s, len);
517 buf->pos += len;
520 /* Header for the generated file */
522 void
523 add_header(struct buffer *b, struct module *mod)
525 buf_printf(b, "#include <linux/module.h>\n");
526 buf_printf(b, "#include <linux/vermagic.h>\n");
527 buf_printf(b, "#include <linux/compiler.h>\n");
528 buf_printf(b, "\n");
529 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
530 buf_printf(b, "\n");
531 buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
532 buf_printf(b, "struct module __this_module\n");
533 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
534 buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
535 if (mod->has_init)
536 buf_printf(b, " .init = init_module,\n");
537 if (mod->has_cleanup)
538 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
539 " .exit = cleanup_module,\n"
540 "#endif\n");
541 buf_printf(b, "};\n");
544 /* Record CRCs for unresolved symbols */
546 void
547 add_versions(struct buffer *b, struct module *mod)
549 struct symbol *s, *exp;
551 for (s = mod->unres; s; s = s->next) {
552 exp = find_symbol(s->name);
553 if (!exp || exp->module == mod) {
554 if (have_vmlinux && !s->weak)
555 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
556 "undefined!\n", s->name, mod->name);
557 continue;
559 s->module = exp->module;
560 s->crc_valid = exp->crc_valid;
561 s->crc = exp->crc;
564 if (!modversions)
565 return;
567 buf_printf(b, "\n");
568 buf_printf(b, "static const struct modversion_info ____versions[]\n");
569 buf_printf(b, "__attribute_used__\n");
570 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
572 for (s = mod->unres; s; s = s->next) {
573 if (!s->module) {
574 continue;
576 if (!s->crc_valid) {
577 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
578 "has no CRC!\n",
579 s->name, mod->name);
580 continue;
582 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
585 buf_printf(b, "};\n");
588 void
589 add_depends(struct buffer *b, struct module *mod, struct module *modules)
591 struct symbol *s;
592 struct module *m;
593 int first = 1;
595 for (m = modules; m; m = m->next) {
596 m->seen = is_vmlinux(m->name);
599 buf_printf(b, "\n");
600 buf_printf(b, "static const char __module_depends[]\n");
601 buf_printf(b, "__attribute_used__\n");
602 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
603 buf_printf(b, "\"depends=");
604 for (s = mod->unres; s; s = s->next) {
605 if (!s->module)
606 continue;
608 if (s->module->seen)
609 continue;
611 s->module->seen = 1;
612 buf_printf(b, "%s%s", first ? "" : ",",
613 strrchr(s->module->name, '/') + 1);
614 first = 0;
616 buf_printf(b, "\";\n");
619 void
620 add_srcversion(struct buffer *b, struct module *mod)
622 if (mod->srcversion[0]) {
623 buf_printf(b, "\n");
624 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
625 mod->srcversion);
629 void
630 write_if_changed(struct buffer *b, const char *fname)
632 char *tmp;
633 FILE *file;
634 struct stat st;
636 file = fopen(fname, "r");
637 if (!file)
638 goto write;
640 if (fstat(fileno(file), &st) < 0)
641 goto close_write;
643 if (st.st_size != b->pos)
644 goto close_write;
646 tmp = NOFAIL(malloc(b->pos));
647 if (fread(tmp, 1, b->pos, file) != b->pos)
648 goto free_write;
650 if (memcmp(tmp, b->p, b->pos) != 0)
651 goto free_write;
653 free(tmp);
654 fclose(file);
655 return;
657 free_write:
658 free(tmp);
659 close_write:
660 fclose(file);
661 write:
662 file = fopen(fname, "w");
663 if (!file) {
664 perror(fname);
665 exit(1);
667 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
668 perror(fname);
669 exit(1);
671 fclose(file);
674 void
675 read_dump(const char *fname)
677 unsigned long size, pos = 0;
678 void *file = grab_file(fname, &size);
679 char *line;
681 if (!file)
682 /* No symbol versions, silently ignore */
683 return;
685 while ((line = get_next_line(&pos, file, size))) {
686 char *symname, *modname, *d;
687 unsigned int crc;
688 struct module *mod;
690 if (!(symname = strchr(line, '\t')))
691 goto fail;
692 *symname++ = '\0';
693 if (!(modname = strchr(symname, '\t')))
694 goto fail;
695 *modname++ = '\0';
696 if (strchr(modname, '\t'))
697 goto fail;
698 crc = strtoul(line, &d, 16);
699 if (*symname == '\0' || *modname == '\0' || *d != '\0')
700 goto fail;
702 if (!(mod = find_module(modname))) {
703 if (is_vmlinux(modname)) {
704 have_vmlinux = 1;
706 mod = new_module(NOFAIL(strdup(modname)));
707 mod->skip = 1;
709 add_exported_symbol(symname, mod, &crc);
711 return;
712 fail:
713 fatal("parse error in symbol dump file\n");
716 void
717 write_dump(const char *fname)
719 struct buffer buf = { };
720 struct symbol *symbol;
721 int n;
723 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
724 symbol = symbolhash[n];
725 while (symbol) {
726 symbol = symbol->next;
730 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
731 symbol = symbolhash[n];
732 while (symbol) {
733 buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
734 symbol->name, symbol->module->name);
735 symbol = symbol->next;
738 write_if_changed(&buf, fname);
742 main(int argc, char **argv)
744 struct module *mod;
745 struct buffer buf = { };
746 char fname[SZ];
747 char *dump_read = NULL, *dump_write = NULL;
748 int opt;
750 while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
751 switch(opt) {
752 case 'i':
753 dump_read = optarg;
754 break;
755 case 'm':
756 modversions = 1;
757 break;
758 case 'o':
759 dump_write = optarg;
760 break;
761 case 'a':
762 all_versions = 1;
763 break;
764 default:
765 exit(1);
769 if (dump_read)
770 read_dump(dump_read);
772 while (optind < argc) {
773 read_symbols(argv[optind++]);
776 for (mod = modules; mod; mod = mod->next) {
777 if (mod->skip)
778 continue;
780 buf.pos = 0;
782 add_header(&buf, mod);
783 add_versions(&buf, mod);
784 add_depends(&buf, mod, modules);
785 add_moddevtable(&buf, mod);
786 add_srcversion(&buf, mod);
788 sprintf(fname, "%s.mod.c", mod->name);
789 write_if_changed(&buf, fname);
792 if (dump_write)
793 write_dump(dump_write);
795 return 0;