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;
23 /* If we are modposting external module set to 1 */
24 static int external_module = 0;
26 void fatal(const char *fmt, ...)
30 fprintf(stderr, "FATAL: ");
32 va_start(arglist, fmt);
33 vfprintf(stderr, fmt, arglist);
39 void warn(const char *fmt, ...)
43 fprintf(stderr, "WARNING: ");
45 va_start(arglist, fmt);
46 vfprintf(stderr, fmt, arglist);
50 static int is_vmlinux(const char *modname)
54 if ((myname = strrchr(modname, '/')))
59 return strcmp(myname, "vmlinux") == 0;
62 void *do_nofail(void *ptr, const char *expr)
65 fatal("modpost: Memory allocation failure: %s.\n", expr);
70 /* A list of all modules we processed */
72 static struct module *modules;
74 static struct module *find_module(char *modname)
78 for (mod = modules; mod; mod = mod->next)
79 if (strcmp(mod->name, modname) == 0)
84 static struct module *new_module(char *modname)
89 mod = NOFAIL(malloc(sizeof(*mod)));
90 memset(mod, 0, sizeof(*mod));
91 p = NOFAIL(strdup(modname));
93 /* strip trailing .o */
94 if ((s = strrchr(p, '.')) != NULL)
95 if (strcmp(s, ".o") == 0)
106 /* A hash of all exported symbols,
107 * struct symbol is also used for lists of unresolved symbols */
109 #define SYMBOL_HASH_SIZE 1024
113 struct module *module;
117 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
118 unsigned int kernel:1; /* 1 if symbol is from kernel
119 * (only for external modules) **/
120 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
124 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
126 /* This is based on the hash agorithm from gdbm, via tdb */
127 static inline unsigned int tdb_hash(const char *name)
129 unsigned value; /* Used to compute the hash value. */
130 unsigned i; /* Used to cycle through random values. */
132 /* Set the initial value from the key size. */
133 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
134 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
136 return (1103515243 * value + 12345);
140 * Allocate a new symbols for use in the hash of exported symbols or
141 * the list of unresolved symbols per module
143 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
146 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
148 memset(s, 0, sizeof(*s));
149 strcpy(s->name, name);
155 /* For the hash of exported symbols */
156 static struct symbol *new_symbol(const char *name, struct module *module)
161 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
162 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
163 new->module = module;
167 static struct symbol *find_symbol(const char *name)
171 /* For our purposes, .foo matches foo. PPC64 needs this. */
175 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
176 if (strcmp(s->name, name) == 0)
183 * Add an exported symbol - it may have already been added without a
184 * CRC, in this case just update the CRC
186 static struct symbol *sym_add_exported(const char *name, struct module *mod)
188 struct symbol *s = find_symbol(name);
191 s = new_symbol(name, mod);
194 warn("%s: duplicate symbol '%s' previous definition "
195 "was in %s%s\n", mod->name, name,
197 is_vmlinux(s->module->name) ?"":".ko");
201 s->vmlinux = is_vmlinux(mod->name);
206 static void sym_update_crc(const char *name, struct module *mod,
209 struct symbol *s = find_symbol(name);
212 s = new_symbol(name, mod);
217 void *grab_file(const char *filename, unsigned long *size)
223 fd = open(filename, O_RDONLY);
224 if (fd < 0 || fstat(fd, &st) != 0)
228 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
231 if (map == MAP_FAILED)
237 * Return a copy of the next line in a mmap'ed file.
238 * spaces in the beginning of the line is trimmed away.
239 * Return a pointer to a static buffer.
241 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
243 static char line[4096];
246 signed char *p = (signed char *)file + *pos;
249 for (; *pos < size ; (*pos)++)
251 if (skip && isspace(*p)) {
256 if (*p != '\n' && (*pos < size)) {
260 break; /* Too long, stop */
271 void release_file(void *file, unsigned long size)
276 static void parse_elf(struct elf_info *info, const char *filename)
279 Elf_Ehdr *hdr = info->hdr;
283 hdr = grab_file(filename, &info->size);
289 if (info->size < sizeof(*hdr))
292 /* Fix endianness in ELF header */
293 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
294 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
295 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
296 hdr->e_machine = TO_NATIVE(hdr->e_machine);
297 sechdrs = (void *)hdr + hdr->e_shoff;
298 info->sechdrs = sechdrs;
300 /* Fix endianness in section headers */
301 for (i = 0; i < hdr->e_shnum; i++) {
302 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
303 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
304 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
305 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
306 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
308 /* Find symbol table. */
309 for (i = 1; i < hdr->e_shnum; i++) {
310 const char *secstrings
311 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
313 if (sechdrs[i].sh_offset > info->size)
315 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
316 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
317 info->modinfo_len = sechdrs[i].sh_size;
319 if (sechdrs[i].sh_type != SHT_SYMTAB)
322 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
323 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
324 + sechdrs[i].sh_size;
325 info->strtab = (void *)hdr +
326 sechdrs[sechdrs[i].sh_link].sh_offset;
328 if (!info->symtab_start) {
329 fatal("%s has no symtab?\n", filename);
331 /* Fix endianness in symbols */
332 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
333 sym->st_shndx = TO_NATIVE(sym->st_shndx);
334 sym->st_name = TO_NATIVE(sym->st_name);
335 sym->st_value = TO_NATIVE(sym->st_value);
336 sym->st_size = TO_NATIVE(sym->st_size);
341 fatal("%s is truncated.\n", filename);
344 static void parse_elf_finish(struct elf_info *info)
346 release_file(info->hdr, info->size);
349 #define CRC_PFX "__crc_"
350 #define KSYMTAB_PFX "__ksymtab_"
352 static void handle_modversions(struct module *mod, struct elf_info *info,
353 Elf_Sym *sym, const char *symname)
357 switch (sym->st_shndx) {
359 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
363 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
364 crc = (unsigned int) sym->st_value;
365 sym_update_crc(symname + strlen(CRC_PFX), mod, crc);
369 /* undefined symbol */
370 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
371 ELF_ST_BIND(sym->st_info) != STB_WEAK)
373 /* ignore global offset table */
374 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
376 /* ignore __this_module, it will be resolved shortly */
377 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
379 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
380 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
381 /* add compatibility with older glibc */
382 #ifndef STT_SPARC_REGISTER
383 #define STT_SPARC_REGISTER STT_REGISTER
385 if (info->hdr->e_machine == EM_SPARC ||
386 info->hdr->e_machine == EM_SPARCV9) {
387 /* Ignore register directives. */
388 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
390 if (symname[0] == '.') {
391 char *munged = strdup(symname);
393 munged[1] = toupper(munged[1]);
399 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
400 strlen(MODULE_SYMBOL_PREFIX)) == 0)
401 mod->unres = alloc_symbol(symname +
402 strlen(MODULE_SYMBOL_PREFIX),
403 ELF_ST_BIND(sym->st_info) == STB_WEAK,
407 /* All exported symbols */
408 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
409 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
411 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
413 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
414 mod->has_cleanup = 1;
420 * Parse tag=value strings from .modinfo section
422 static char *next_string(char *string, unsigned long *secsize)
424 /* Skip non-zero chars */
427 if ((*secsize)-- <= 1)
431 /* Skip any zero padding. */
434 if ((*secsize)-- <= 1)
440 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
444 unsigned int taglen = strlen(tag);
445 unsigned long size = modinfo_len;
447 for (p = modinfo; p; p = next_string(p, &size)) {
448 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
449 return p + taglen + 1;
454 static void read_symbols(char *modname)
459 struct elf_info info = { };
462 parse_elf(&info, modname);
464 mod = new_module(modname);
466 /* When there's no vmlinux, don't print warnings about
467 * unresolved symbols (since there'll be too many ;) */
468 if (is_vmlinux(modname)) {
473 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
474 symname = info.strtab + sym->st_name;
476 handle_modversions(mod, &info, sym, symname);
477 handle_moddevtable(mod, &info, sym, symname);
480 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
482 maybe_frob_rcs_version(modname, version, info.modinfo,
483 version - (char *)info.hdr);
484 if (version || (all_versions && !is_vmlinux(modname)))
485 get_src_version(modname, mod->srcversion,
486 sizeof(mod->srcversion)-1);
488 parse_elf_finish(&info);
490 /* Our trick to get versioning for struct_module - it's
491 * never passed as an argument to an exported function, so
492 * the automatic versioning doesn't pick it up, but it's really
493 * important anyhow */
495 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
500 /* We first write the generated file into memory using the
501 * following helper, then compare to the file on disk and
502 * only update the later if anything changed */
504 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
505 const char *fmt, ...)
512 len = vsnprintf(tmp, SZ, fmt, ap);
513 if (buf->size - buf->pos < len + 1) {
515 buf->p = realloc(buf->p, buf->size);
517 strncpy(buf->p + buf->pos, tmp, len + 1);
522 void buf_write(struct buffer *buf, const char *s, int len)
524 if (buf->size - buf->pos < len) {
526 buf->p = realloc(buf->p, buf->size);
528 strncpy(buf->p + buf->pos, s, len);
533 * Header for the generated file
535 static void add_header(struct buffer *b, struct module *mod)
537 buf_printf(b, "#include <linux/module.h>\n");
538 buf_printf(b, "#include <linux/vermagic.h>\n");
539 buf_printf(b, "#include <linux/compiler.h>\n");
541 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
543 buf_printf(b, "struct module __this_module\n");
544 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
545 buf_printf(b, " .name = KBUILD_MODNAME,\n");
547 buf_printf(b, " .init = init_module,\n");
548 if (mod->has_cleanup)
549 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
550 " .exit = cleanup_module,\n"
552 buf_printf(b, "};\n");
556 * Record CRCs for unresolved symbols
558 static void add_versions(struct buffer *b, struct module *mod)
560 struct symbol *s, *exp;
562 for (s = mod->unres; s; s = s->next) {
563 exp = find_symbol(s->name);
564 if (!exp || exp->module == mod) {
565 if (have_vmlinux && !s->weak)
566 warn("\"%s\" [%s.ko] undefined!\n",
570 s->module = exp->module;
571 s->crc_valid = exp->crc_valid;
579 buf_printf(b, "static const struct modversion_info ____versions[]\n");
580 buf_printf(b, "__attribute_used__\n");
581 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
583 for (s = mod->unres; s; s = s->next) {
588 warn("\"%s\" [%s.ko] has no CRC!\n",
592 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
595 buf_printf(b, "};\n");
598 static void add_depends(struct buffer *b, struct module *mod,
599 struct module *modules)
605 for (m = modules; m; m = m->next) {
606 m->seen = is_vmlinux(m->name);
610 buf_printf(b, "static const char __module_depends[]\n");
611 buf_printf(b, "__attribute_used__\n");
612 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
613 buf_printf(b, "\"depends=");
614 for (s = mod->unres; s; s = s->next) {
622 buf_printf(b, "%s%s", first ? "" : ",",
623 strrchr(s->module->name, '/') + 1);
626 buf_printf(b, "\";\n");
629 static void add_srcversion(struct buffer *b, struct module *mod)
631 if (mod->srcversion[0]) {
633 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
638 static void write_if_changed(struct buffer *b, const char *fname)
644 file = fopen(fname, "r");
648 if (fstat(fileno(file), &st) < 0)
651 if (st.st_size != b->pos)
654 tmp = NOFAIL(malloc(b->pos));
655 if (fread(tmp, 1, b->pos, file) != b->pos)
658 if (memcmp(tmp, b->p, b->pos) != 0)
670 file = fopen(fname, "w");
675 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
682 static void read_dump(const char *fname, unsigned int kernel)
684 unsigned long size, pos = 0;
685 void *file = grab_file(fname, &size);
689 /* No symbol versions, silently ignore */
692 while ((line = get_next_line(&pos, file, size))) {
693 char *symname, *modname, *d;
698 if (!(symname = strchr(line, '\t')))
701 if (!(modname = strchr(symname, '\t')))
704 if (strchr(modname, '\t'))
706 crc = strtoul(line, &d, 16);
707 if (*symname == '\0' || *modname == '\0' || *d != '\0')
710 if (!(mod = find_module(modname))) {
711 if (is_vmlinux(modname)) {
714 mod = new_module(NOFAIL(strdup(modname)));
717 s = sym_add_exported(symname, mod);
720 sym_update_crc(symname, mod, crc);
724 fatal("parse error in symbol dump file\n");
727 /* For normal builds always dump all symbols.
728 * For external modules only dump symbols
729 * that are not read from kernel Module.symvers.
731 static int dump_sym(struct symbol *sym)
733 if (!external_module)
735 if (sym->vmlinux || sym->kernel)
740 static void write_dump(const char *fname)
742 struct buffer buf = { };
743 struct symbol *symbol;
746 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
747 symbol = symbolhash[n];
749 if (dump_sym(symbol))
750 buf_printf(&buf, "0x%08x\t%s\t%s\n",
751 symbol->crc, symbol->name,
752 symbol->module->name);
753 symbol = symbol->next;
756 write_if_changed(&buf, fname);
759 int main(int argc, char **argv)
762 struct buffer buf = { };
764 char *kernel_read = NULL, *module_read = NULL;
765 char *dump_write = NULL;
768 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
771 kernel_read = optarg;
774 module_read = optarg;
792 read_dump(kernel_read, 1);
794 read_dump(module_read, 0);
796 while (optind < argc) {
797 read_symbols(argv[optind++]);
800 for (mod = modules; mod; mod = mod->next) {
806 add_header(&buf, mod);
807 add_versions(&buf, mod);
808 add_depends(&buf, mod, modules);
809 add_moddevtable(&buf, mod);
810 add_srcversion(&buf, mod);
812 sprintf(fname, "%s.mod.c", mod->name);
813 write_if_changed(&buf, fname);
817 write_dump(dump_write);