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 ...
17 /* Are we using CONFIG_MODVERSIONS? */
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
21 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
22 static int all_versions = 0;
25 fatal(const char *fmt, ...)
29 fprintf(stderr, "FATAL: ");
31 va_start(arglist, fmt);
32 vfprintf(stderr, fmt, arglist);
39 warn(const char *fmt, ...)
43 fprintf(stderr, "WARNING: ");
45 va_start(arglist, fmt);
46 vfprintf(stderr, fmt, arglist);
50 void *do_nofail(void *ptr, const char *file, int line, const char *expr)
53 fatal("Memory allocation failure %s line %d: %s.\n",
59 /* A list of all modules we processed */
61 static struct module *modules;
64 find_module(char *modname)
68 for (mod = modules; mod; mod = mod->next)
69 if (strcmp(mod->name, modname) == 0)
75 new_module(char *modname)
80 mod = NOFAIL(malloc(sizeof(*mod)));
81 memset(mod, 0, sizeof(*mod));
82 p = NOFAIL(strdup(modname));
84 /* strip trailing .o */
85 if ((s = strrchr(p, '.')) != NULL)
86 if (strcmp(s, ".o") == 0)
97 /* A hash of all exported symbols,
98 * struct symbol is also used for lists of unresolved symbols */
100 #define SYMBOL_HASH_SIZE 1024
104 struct module *module;
111 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
113 /* This is based on the hash agorithm from gdbm, via tdb */
114 static inline unsigned int tdb_hash(const char *name)
116 unsigned value; /* Used to compute the hash value. */
117 unsigned i; /* Used to cycle through random values. */
119 /* Set the initial value from the key size. */
120 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
121 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
123 return (1103515243 * value + 12345);
126 /* Allocate a new symbols for use in the hash of exported symbols or
127 * the list of unresolved symbols per module */
130 alloc_symbol(const char *name, unsigned int weak, struct symbol *next)
132 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
134 memset(s, 0, sizeof(*s));
135 strcpy(s->name, name);
141 /* For the hash of exported symbols */
144 new_symbol(const char *name, struct module *module, unsigned int *crc)
149 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
150 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
151 new->module = module;
159 find_symbol(const char *name)
163 /* For our purposes, .foo matches foo. PPC64 needs this. */
167 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
168 if (strcmp(s->name, name) == 0)
174 /* Add an exported symbol - it may have already been added without a
175 * CRC, in this case just update the CRC */
177 add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
179 struct symbol *s = find_symbol(name);
182 new_symbol(name, module, crc);
192 grab_file(const char *filename, unsigned long *size)
198 fd = open(filename, O_RDONLY);
199 if (fd < 0 || fstat(fd, &st) != 0)
203 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
206 if (map == MAP_FAILED)
212 Return a copy of the next line in a mmap'ed file.
213 spaces in the beginning of the line is trimmed away.
214 Return a pointer to a static buffer.
217 get_next_line(unsigned long *pos, void *file, unsigned long size)
219 static char line[4096];
222 signed char *p = (signed char *)file + *pos;
225 for (; *pos < size ; (*pos)++)
227 if (skip && isspace(*p)) {
232 if (*p != '\n' && (*pos < size)) {
236 break; /* Too long, stop */
248 release_file(void *file, unsigned long size)
254 parse_elf(struct elf_info *info, const char *filename)
257 Elf_Ehdr *hdr = info->hdr;
261 hdr = grab_file(filename, &info->size);
267 if (info->size < sizeof(*hdr))
270 /* Fix endianness in ELF header */
271 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
272 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
273 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
274 hdr->e_machine = TO_NATIVE(hdr->e_machine);
275 sechdrs = (void *)hdr + hdr->e_shoff;
276 info->sechdrs = sechdrs;
278 /* Fix endianness in section headers */
279 for (i = 0; i < hdr->e_shnum; i++) {
280 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
281 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
282 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
283 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
284 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
286 /* Find symbol table. */
287 for (i = 1; i < hdr->e_shnum; i++) {
288 const char *secstrings
289 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
291 if (sechdrs[i].sh_offset > info->size)
293 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
294 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
295 info->modinfo_len = sechdrs[i].sh_size;
297 if (sechdrs[i].sh_type != SHT_SYMTAB)
300 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
301 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
302 + sechdrs[i].sh_size;
303 info->strtab = (void *)hdr +
304 sechdrs[sechdrs[i].sh_link].sh_offset;
306 if (!info->symtab_start) {
307 fprintf(stderr, "modpost: %s no symtab?\n", filename);
310 /* Fix endianness in symbols */
311 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
312 sym->st_shndx = TO_NATIVE(sym->st_shndx);
313 sym->st_name = TO_NATIVE(sym->st_name);
314 sym->st_value = TO_NATIVE(sym->st_value);
315 sym->st_size = TO_NATIVE(sym->st_size);
320 fprintf(stderr, "modpost: %s is truncated.\n", filename);
325 parse_elf_finish(struct elf_info *info)
327 release_file(info->hdr, info->size);
330 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
331 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
334 handle_modversions(struct module *mod, struct elf_info *info,
335 Elf_Sym *sym, const char *symname)
339 switch (sym->st_shndx) {
341 fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n",
346 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
347 crc = (unsigned int) sym->st_value;
348 add_exported_symbol(symname + strlen(CRC_PFX),
353 /* undefined symbol */
354 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
355 ELF_ST_BIND(sym->st_info) != STB_WEAK)
357 /* ignore global offset table */
358 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
360 /* ignore __this_module, it will be resolved shortly */
361 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
364 if (info->hdr->e_machine == EM_SPARC ||
365 info->hdr->e_machine == EM_SPARCV9) {
366 /* Ignore register directives. */
367 if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
372 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
373 strlen(MODULE_SYMBOL_PREFIX)) == 0)
374 mod->unres = alloc_symbol(symname +
375 strlen(MODULE_SYMBOL_PREFIX),
376 ELF_ST_BIND(sym->st_info) == STB_WEAK,
380 /* All exported symbols */
381 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
382 add_exported_symbol(symname + strlen(KSYMTAB_PFX),
385 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
387 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
388 mod->has_cleanup = 1;
394 is_vmlinux(const char *modname)
398 if ((myname = strrchr(modname, '/')))
403 return strcmp(myname, "vmlinux") == 0;
406 /* Parse tag=value strings from .modinfo section */
407 static char *next_string(char *string, unsigned long *secsize)
409 /* Skip non-zero chars */
412 if ((*secsize)-- <= 1)
416 /* Skip any zero padding. */
419 if ((*secsize)-- <= 1)
425 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
429 unsigned int taglen = strlen(tag);
430 unsigned long size = modinfo_len;
432 for (p = modinfo; p; p = next_string(p, &size)) {
433 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
434 return p + taglen + 1;
440 read_symbols(char *modname)
445 struct elf_info info = { };
448 parse_elf(&info, modname);
450 mod = new_module(modname);
452 /* When there's no vmlinux, don't print warnings about
453 * unresolved symbols (since there'll be too many ;) */
454 if (is_vmlinux(modname)) {
455 unsigned int fake_crc = 0;
457 add_exported_symbol("struct_module", mod, &fake_crc);
461 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
462 symname = info.strtab + sym->st_name;
464 handle_modversions(mod, &info, sym, symname);
465 handle_moddevtable(mod, &info, sym, symname);
468 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
470 maybe_frob_rcs_version(modname, version, info.modinfo,
471 version - (char *)info.hdr);
472 if (version || (all_versions && !is_vmlinux(modname)))
473 get_src_version(modname, mod->srcversion,
474 sizeof(mod->srcversion)-1);
476 parse_elf_finish(&info);
478 /* Our trick to get versioning for struct_module - it's
479 * never passed as an argument to an exported function, so
480 * the automatic versioning doesn't pick it up, but it's really
481 * important anyhow */
483 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
488 /* We first write the generated file into memory using the
489 * following helper, then compare to the file on disk and
490 * only update the later if anything changed */
492 void __attribute__((format(printf, 2, 3)))
493 buf_printf(struct buffer *buf, const char *fmt, ...)
500 len = vsnprintf(tmp, SZ, fmt, ap);
501 if (buf->size - buf->pos < len + 1) {
503 buf->p = realloc(buf->p, buf->size);
505 strncpy(buf->p + buf->pos, tmp, len + 1);
511 buf_write(struct buffer *buf, const char *s, int len)
513 if (buf->size - buf->pos < len) {
515 buf->p = realloc(buf->p, buf->size);
517 strncpy(buf->p + buf->pos, s, len);
521 /* Header for the generated file */
524 add_header(struct buffer *b, struct module *mod)
526 buf_printf(b, "#include <linux/module.h>\n");
527 buf_printf(b, "#include <linux/vermagic.h>\n");
528 buf_printf(b, "#include <linux/compiler.h>\n");
530 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
532 buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
533 buf_printf(b, "struct module __this_module\n");
534 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
535 buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
537 buf_printf(b, " .init = init_module,\n");
538 if (mod->has_cleanup)
539 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
540 " .exit = cleanup_module,\n"
542 buf_printf(b, "};\n");
545 /* Record CRCs for unresolved symbols */
548 add_versions(struct buffer *b, struct module *mod)
550 struct symbol *s, *exp;
552 for (s = mod->unres; s; s = s->next) {
553 exp = find_symbol(s->name);
554 if (!exp || exp->module == mod) {
555 if (have_vmlinux && !s->weak)
556 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
557 "undefined!\n", s->name, mod->name);
560 s->module = exp->module;
561 s->crc_valid = exp->crc_valid;
569 buf_printf(b, "static const struct modversion_info ____versions[]\n");
570 buf_printf(b, "__attribute_used__\n");
571 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
573 for (s = mod->unres; s; s = s->next) {
578 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
583 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
586 buf_printf(b, "};\n");
590 add_depends(struct buffer *b, struct module *mod, struct module *modules)
596 for (m = modules; m; m = m->next) {
597 m->seen = is_vmlinux(m->name);
601 buf_printf(b, "static const char __module_depends[]\n");
602 buf_printf(b, "__attribute_used__\n");
603 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
604 buf_printf(b, "\"depends=");
605 for (s = mod->unres; s; s = s->next) {
613 buf_printf(b, "%s%s", first ? "" : ",",
614 strrchr(s->module->name, '/') + 1);
617 buf_printf(b, "\";\n");
621 add_srcversion(struct buffer *b, struct module *mod)
623 if (mod->srcversion[0]) {
625 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
631 write_if_changed(struct buffer *b, const char *fname)
637 file = fopen(fname, "r");
641 if (fstat(fileno(file), &st) < 0)
644 if (st.st_size != b->pos)
647 tmp = NOFAIL(malloc(b->pos));
648 if (fread(tmp, 1, b->pos, file) != b->pos)
651 if (memcmp(tmp, b->p, b->pos) != 0)
663 file = fopen(fname, "w");
668 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
676 read_dump(const char *fname)
678 unsigned long size, pos = 0;
679 void *file = grab_file(fname, &size);
683 /* No symbol versions, silently ignore */
686 while ((line = get_next_line(&pos, file, size))) {
687 char *symname, *modname, *d;
691 if (!(symname = strchr(line, '\t')))
694 if (!(modname = strchr(symname, '\t')))
697 if (strchr(modname, '\t'))
699 crc = strtoul(line, &d, 16);
700 if (*symname == '\0' || *modname == '\0' || *d != '\0')
703 if (!(mod = find_module(modname))) {
704 if (is_vmlinux(modname)) {
707 mod = new_module(NOFAIL(strdup(modname)));
710 add_exported_symbol(symname, mod, &crc);
714 fatal("parse error in symbol dump file\n");
718 write_dump(const char *fname)
720 struct buffer buf = { };
721 struct symbol *symbol;
724 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
725 symbol = symbolhash[n];
727 symbol = symbol->next;
731 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
732 symbol = symbolhash[n];
734 buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
735 symbol->name, symbol->module->name);
736 symbol = symbol->next;
739 write_if_changed(&buf, fname);
743 main(int argc, char **argv)
746 struct buffer buf = { };
748 char *dump_read = NULL, *dump_write = NULL;
751 while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
771 read_dump(dump_read);
773 while (optind < argc) {
774 read_symbols(argv[optind++]);
777 for (mod = modules; mod; mod = mod->next) {
783 add_header(&buf, mod);
784 add_versions(&buf, mod);
785 add_depends(&buf, mod, modules);
786 add_moddevtable(&buf, mod);
787 add_srcversion(&buf, mod);
789 sprintf(fname, "%s.mod.c", mod->name);
790 write_if_changed(&buf, fname);
794 write_dump(dump_write);