1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * 2002-2003 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 ...
17 /* Are we using CONFIG_MODVERSIONS? */
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
23 fatal(const char *fmt, ...)
27 fprintf(stderr, "FATAL: ");
29 va_start(arglist, fmt);
30 vfprintf(stderr, fmt, arglist);
37 warn(const char *fmt, ...)
41 fprintf(stderr, "WARNING: ");
43 va_start(arglist, fmt);
44 vfprintf(stderr, fmt, arglist);
48 void *do_nofail(void *ptr, const char *file, int line, const char *expr)
51 fatal("Memory allocation failure %s line %d: %s.\n",
57 /* A list of all modules we processed */
59 static struct module *modules;
62 find_module(char *modname)
66 for (mod = modules; mod; mod = mod->next)
67 if (strcmp(mod->name, modname) == 0)
73 new_module(char *modname)
78 mod = NOFAIL(malloc(sizeof(*mod)));
79 memset(mod, 0, sizeof(*mod));
80 p = NOFAIL(strdup(modname));
82 /* strip trailing .o */
83 if ((s = strrchr(p, '.')) != NULL)
84 if (strcmp(s, ".o") == 0)
95 /* A hash of all exported symbols,
96 * struct symbol is also used for lists of unresolved symbols */
98 #define SYMBOL_HASH_SIZE 1024
102 struct module *module;
108 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
110 /* This is based on the hash agorithm from gdbm, via tdb */
111 static inline unsigned int tdb_hash(const char *name)
113 unsigned value; /* Used to compute the hash value. */
114 unsigned i; /* Used to cycle through random values. */
116 /* Set the initial value from the key size. */
117 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
118 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
120 return (1103515243 * value + 12345);
123 /* Allocate a new symbols for use in the hash of exported symbols or
124 * the list of unresolved symbols per module */
127 alloc_symbol(const char *name, struct symbol *next)
129 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
131 memset(s, 0, sizeof(*s));
132 strcpy(s->name, name);
137 /* For the hash of exported symbols */
140 new_symbol(const char *name, struct module *module, unsigned int *crc)
145 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
146 new = symbolhash[hash] = alloc_symbol(name, symbolhash[hash]);
147 new->module = module;
155 find_symbol(const char *name)
159 /* For our purposes, .foo matches foo. PPC64 needs this. */
163 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
164 if (strcmp(s->name, name) == 0)
170 /* Add an exported symbol - it may have already been added without a
171 * CRC, in this case just update the CRC */
173 add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
175 struct symbol *s = find_symbol(name);
178 new_symbol(name, module, crc);
188 grab_file(const char *filename, unsigned long *size)
194 fd = open(filename, O_RDONLY);
195 if (fd < 0 || fstat(fd, &st) != 0)
199 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
202 if (map == MAP_FAILED)
208 Return a copy of the next line in a mmap'ed file.
209 spaces in the beginning of the line is trimmed away.
210 Return a pointer to a static buffer.
213 get_next_line(unsigned long *pos, void *file, unsigned long size)
215 static char line[4096];
218 char *p = (char *)file + *pos;
221 for (; *pos < size ; (*pos)++)
223 if (skip && isspace(*p)) {
228 if (*p != '\n' && (*pos < size)) {
232 break; /* Too long, stop */
244 release_file(void *file, unsigned long size)
250 parse_elf(struct elf_info *info, const char *filename)
253 Elf_Ehdr *hdr = info->hdr;
257 hdr = grab_file(filename, &info->size);
263 if (info->size < sizeof(*hdr))
266 /* Fix endianness in ELF header */
267 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
268 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
269 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
270 hdr->e_machine = TO_NATIVE(hdr->e_machine);
271 sechdrs = (void *)hdr + hdr->e_shoff;
272 info->sechdrs = sechdrs;
274 /* Fix endianness in section headers */
275 for (i = 0; i < hdr->e_shnum; i++) {
276 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
277 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
278 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
279 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
280 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
282 /* Find symbol table. */
283 for (i = 1; i < hdr->e_shnum; i++) {
284 const char *secstrings
285 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
287 if (sechdrs[i].sh_offset > info->size)
289 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
290 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
291 info->modinfo_len = sechdrs[i].sh_size;
293 if (sechdrs[i].sh_type != SHT_SYMTAB)
296 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
297 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
298 + sechdrs[i].sh_size;
299 info->strtab = (void *)hdr +
300 sechdrs[sechdrs[i].sh_link].sh_offset;
302 if (!info->symtab_start) {
303 fprintf(stderr, "modpost: %s no symtab?\n", filename);
306 /* Fix endianness in symbols */
307 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
308 sym->st_shndx = TO_NATIVE(sym->st_shndx);
309 sym->st_name = TO_NATIVE(sym->st_name);
310 sym->st_value = TO_NATIVE(sym->st_value);
311 sym->st_size = TO_NATIVE(sym->st_size);
316 fprintf(stderr, "modpost: %s is truncated.\n", filename);
321 parse_elf_finish(struct elf_info *info)
323 release_file(info->hdr, info->size);
326 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
327 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
330 handle_modversions(struct module *mod, struct elf_info *info,
331 Elf_Sym *sym, const char *symname)
335 switch (sym->st_shndx) {
337 fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n",
342 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
343 crc = (unsigned int) sym->st_value;
344 add_exported_symbol(symname + strlen(CRC_PFX),
349 /* undefined symbol */
350 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL)
352 /* ignore global offset table */
353 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
355 /* ignore __this_module, it will be resolved shortly */
356 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
359 if (info->hdr->e_machine == EM_SPARC ||
360 info->hdr->e_machine == EM_SPARCV9) {
361 /* Ignore register directives. */
362 if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
367 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
368 strlen(MODULE_SYMBOL_PREFIX)) == 0)
369 mod->unres = alloc_symbol(symname +
370 strlen(MODULE_SYMBOL_PREFIX),
374 /* All exported symbols */
375 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
376 add_exported_symbol(symname + strlen(KSYMTAB_PFX),
379 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
381 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
382 mod->has_cleanup = 1;
388 is_vmlinux(const char *modname)
392 if ((myname = strrchr(modname, '/')))
397 return strcmp(myname, "vmlinux") == 0;
401 read_symbols(char *modname)
405 struct elf_info info = { };
408 parse_elf(&info, modname);
410 mod = new_module(modname);
412 /* When there's no vmlinux, don't print warnings about
413 * unresolved symbols (since there'll be too many ;) */
414 if (is_vmlinux(modname)) {
415 unsigned int fake_crc = 0;
417 add_exported_symbol("struct_module", mod, &fake_crc);
421 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
422 symname = info.strtab + sym->st_name;
424 handle_modversions(mod, &info, sym, symname);
425 handle_moddevtable(mod, &info, sym, symname);
427 maybe_frob_version(modname, info.modinfo, info.modinfo_len,
428 (void *)info.modinfo - (void *)info.hdr);
429 parse_elf_finish(&info);
431 /* Our trick to get versioning for struct_module - it's
432 * never passed as an argument to an exported function, so
433 * the automatic versioning doesn't pick it up, but it's really
434 * important anyhow */
436 mod->unres = alloc_symbol("struct_module", mod->unres);
441 /* We first write the generated file into memory using the
442 * following helper, then compare to the file on disk and
443 * only update the later if anything changed */
445 void __attribute__((format(printf, 2, 3)))
446 buf_printf(struct buffer *buf, const char *fmt, ...)
453 len = vsnprintf(tmp, SZ, fmt, ap);
454 if (buf->size - buf->pos < len + 1) {
456 buf->p = realloc(buf->p, buf->size);
458 strncpy(buf->p + buf->pos, tmp, len + 1);
464 buf_write(struct buffer *buf, const char *s, int len)
466 if (buf->size - buf->pos < len) {
468 buf->p = realloc(buf->p, buf->size);
470 strncpy(buf->p + buf->pos, s, len);
474 /* Header for the generated file */
477 add_header(struct buffer *b, struct module *mod)
479 buf_printf(b, "#include <linux/module.h>\n");
480 buf_printf(b, "#include <linux/vermagic.h>\n");
481 buf_printf(b, "#include <linux/compiler.h>\n");
483 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
485 buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
486 buf_printf(b, "struct module __this_module\n");
487 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
488 buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
490 buf_printf(b, " .init = init_module,\n");
491 if (mod->has_cleanup)
492 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
493 " .exit = cleanup_module,\n"
495 buf_printf(b, "};\n");
498 /* Record CRCs for unresolved symbols */
501 add_versions(struct buffer *b, struct module *mod)
503 struct symbol *s, *exp;
505 for (s = mod->unres; s; s = s->next) {
506 exp = find_symbol(s->name);
507 if (!exp || exp->module == mod) {
509 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
510 "undefined!\n", s->name, mod->name);
513 s->module = exp->module;
514 s->crc_valid = exp->crc_valid;
522 buf_printf(b, "static const struct modversion_info ____versions[]\n");
523 buf_printf(b, "__attribute_used__\n");
524 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
526 for (s = mod->unres; s; s = s->next) {
531 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
536 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
539 buf_printf(b, "};\n");
543 add_depends(struct buffer *b, struct module *mod, struct module *modules)
549 for (m = modules; m; m = m->next) {
550 m->seen = is_vmlinux(m->name);
554 buf_printf(b, "static const char __module_depends[]\n");
555 buf_printf(b, "__attribute_used__\n");
556 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
557 buf_printf(b, "\"depends=");
558 for (s = mod->unres; s; s = s->next) {
566 buf_printf(b, "%s%s", first ? "" : ",",
567 strrchr(s->module->name, '/') + 1);
570 buf_printf(b, "\";\n");
574 write_if_changed(struct buffer *b, const char *fname)
580 file = fopen(fname, "r");
584 if (fstat(fileno(file), &st) < 0)
587 if (st.st_size != b->pos)
590 tmp = NOFAIL(malloc(b->pos));
591 if (fread(tmp, 1, b->pos, file) != b->pos)
594 if (memcmp(tmp, b->p, b->pos) != 0)
606 file = fopen(fname, "w");
611 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
619 read_dump(const char *fname)
621 unsigned long size, pos = 0;
622 void *file = grab_file(fname, &size);
626 /* No symbol versions, silently ignore */
629 while ((line = get_next_line(&pos, file, size))) {
630 char *symname, *modname, *d;
634 if (!(symname = strchr(line, '\t')))
637 if (!(modname = strchr(symname, '\t')))
640 if (strchr(modname, '\t'))
642 crc = strtoul(line, &d, 16);
643 if (*symname == '\0' || *modname == '\0' || *d != '\0')
646 if (!(mod = find_module(modname))) {
647 if (is_vmlinux(modname)) {
650 mod = new_module(NOFAIL(strdup(modname)));
653 add_exported_symbol(symname, mod, &crc);
657 fatal("parse error in symbol dump file\n");
661 write_dump(const char *fname)
663 struct buffer buf = { };
664 struct symbol *symbol;
667 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
668 symbol = symbolhash[n];
670 symbol = symbol->next;
674 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
675 symbol = symbolhash[n];
677 buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
678 symbol->name, symbol->module->name);
679 symbol = symbol->next;
682 write_if_changed(&buf, fname);
686 main(int argc, char **argv)
689 struct buffer buf = { };
691 char *dump_read = NULL, *dump_write = NULL;
694 while ((opt = getopt(argc, argv, "i:mo:")) != -1) {
711 read_dump(dump_read);
713 while (optind < argc) {
714 read_symbols(argv[optind++]);
717 for (mod = modules; mod; mod = mod->next) {
723 add_header(&buf, mod);
724 add_versions(&buf, mod);
725 add_depends(&buf, mod, modules);
726 add_moddevtable(&buf, mod);
728 sprintf(fname, "%s.mod.c", mod->name);
729 write_if_changed(&buf, fname);
733 write_dump(dump_write);