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),
350 /* undefined symbol */
351 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL)
353 /* ignore global offset table */
354 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
356 /* ignore __this_module, it will be resolved shortly */
357 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
360 if (info->hdr->e_machine == EM_SPARC ||
361 info->hdr->e_machine == EM_SPARCV9) {
362 /* Ignore register directives. */
363 if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
368 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
369 strlen(MODULE_SYMBOL_PREFIX)) == 0)
370 mod->unres = alloc_symbol(symname +
371 strlen(MODULE_SYMBOL_PREFIX),
375 /* All exported symbols */
376 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
377 add_exported_symbol(symname + strlen(KSYMTAB_PFX),
385 is_vmlinux(const char *modname)
389 if ((myname = strrchr(modname, '/')))
394 return strcmp(myname, "vmlinux") == 0;
398 read_symbols(char *modname)
402 struct elf_info info = { };
405 parse_elf(&info, modname);
407 mod = new_module(modname);
409 /* When there's no vmlinux, don't print warnings about
410 * unresolved symbols (since there'll be too many ;) */
411 if (is_vmlinux(modname)) {
412 unsigned int fake_crc = 0;
414 /* May not have this if !CONFIG_MODULE_UNLOAD: fake it.
415 If it appears, we'll get the real CRC. */
416 add_exported_symbol("cleanup_module", mod, &fake_crc);
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);
438 /* Always version init_module and cleanup_module, in
439 * case module doesn't have its own. */
440 mod->unres = alloc_symbol("init_module", mod->unres);
441 mod->unres = alloc_symbol("cleanup_module", mod->unres);
447 /* We first write the generated file into memory using the
448 * following helper, then compare to the file on disk and
449 * only update the later if anything changed */
451 void __attribute__((format(printf, 2, 3)))
452 buf_printf(struct buffer *buf, const char *fmt, ...)
459 len = vsnprintf(tmp, SZ, fmt, ap);
460 if (buf->size - buf->pos < len + 1) {
462 buf->p = realloc(buf->p, buf->size);
464 strncpy(buf->p + buf->pos, tmp, len + 1);
470 buf_write(struct buffer *buf, const char *s, int len)
472 if (buf->size - buf->pos < len) {
474 buf->p = realloc(buf->p, buf->size);
476 strncpy(buf->p + buf->pos, s, len);
480 /* Header for the generated file */
483 add_header(struct buffer *b)
485 buf_printf(b, "#include <linux/module.h>\n");
486 buf_printf(b, "#include <linux/vermagic.h>\n");
487 buf_printf(b, "#include <linux/compiler.h>\n");
489 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
491 buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
492 buf_printf(b, "struct module __this_module\n");
493 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
494 buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
495 buf_printf(b, " .init = init_module,\n");
496 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n");
497 buf_printf(b, " .exit = cleanup_module,\n");
498 buf_printf(b, "#endif\n");
499 buf_printf(b, "};\n");
502 /* Record CRCs for unresolved symbols */
505 add_versions(struct buffer *b, struct module *mod)
507 struct symbol *s, *exp;
509 for (s = mod->unres; s; s = s->next) {
510 exp = find_symbol(s->name);
511 if (!exp || exp->module == mod) {
513 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
514 "undefined!\n", s->name, mod->name);
517 s->module = exp->module;
518 s->crc_valid = exp->crc_valid;
526 buf_printf(b, "static const struct modversion_info ____versions[]\n");
527 buf_printf(b, "__attribute_used__\n");
528 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
530 for (s = mod->unres; s; s = s->next) {
535 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
540 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
543 buf_printf(b, "};\n");
547 add_depends(struct buffer *b, struct module *mod, struct module *modules)
553 for (m = modules; m; m = m->next) {
554 m->seen = is_vmlinux(m->name);
558 buf_printf(b, "static const char __module_depends[]\n");
559 buf_printf(b, "__attribute_used__\n");
560 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
561 buf_printf(b, "\"depends=");
562 for (s = mod->unres; s; s = s->next) {
570 buf_printf(b, "%s%s", first ? "" : ",",
571 strrchr(s->module->name, '/') + 1);
574 buf_printf(b, "\";\n");
578 write_if_changed(struct buffer *b, const char *fname)
584 file = fopen(fname, "r");
588 if (fstat(fileno(file), &st) < 0)
591 if (st.st_size != b->pos)
594 tmp = NOFAIL(malloc(b->pos));
595 if (fread(tmp, 1, b->pos, file) != b->pos)
598 if (memcmp(tmp, b->p, b->pos) != 0)
610 file = fopen(fname, "w");
615 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
623 read_dump(const char *fname)
625 unsigned long size, pos = 0;
626 void *file = grab_file(fname, &size);
630 /* No symbol versions, silently ignore */
633 while ((line = get_next_line(&pos, file, size))) {
634 char *symname, *modname, *d;
638 if (!(symname = strchr(line, '\t')))
641 if (!(modname = strchr(symname, '\t')))
644 if (strchr(modname, '\t'))
646 crc = strtoul(line, &d, 16);
647 if (*symname == '\0' || *modname == '\0' || *d != '\0')
650 if (!(mod = find_module(modname))) {
651 if (is_vmlinux(modname)) {
655 mod = new_module(NOFAIL(strdup(modname)));
658 add_exported_symbol(symname, mod, &crc);
662 fatal("parse error in symbol dump file\n");
666 write_dump(const char *fname)
668 struct buffer buf = { };
669 struct symbol *symbol;
672 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
673 symbol = symbolhash[n];
675 symbol = symbol->next;
679 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
680 symbol = symbolhash[n];
682 buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
683 symbol->name, symbol->module->name);
684 symbol = symbol->next;
687 write_if_changed(&buf, fname);
691 main(int argc, char **argv)
694 struct buffer buf = { };
696 char *dump_read = NULL, *dump_write = NULL;
699 while ((opt = getopt(argc, argv, "i:o:")) != -1) {
713 read_dump(dump_read);
715 while (optind < argc) {
716 read_symbols(argv[optind++]);
719 for (mod = modules; mod; mod = mod->next) {
726 add_versions(&buf, mod);
727 add_depends(&buf, mod, modules);
728 add_moddevtable(&buf, mod);
730 sprintf(fname, "%s.mod.c", mod->name);
731 write_if_changed(&buf, fname);
735 write_dump(dump_write);