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) **/
123 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
125 /* This is based on the hash agorithm from gdbm, via tdb */
126 static inline unsigned int tdb_hash(const char *name)
128 unsigned value; /* Used to compute the hash value. */
129 unsigned i; /* Used to cycle through random values. */
131 /* Set the initial value from the key size. */
132 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
133 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
135 return (1103515243 * value + 12345);
139 * Allocate a new symbols for use in the hash of exported symbols or
140 * the list of unresolved symbols per module
142 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
145 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
147 memset(s, 0, sizeof(*s));
148 strcpy(s->name, name);
154 /* For the hash of exported symbols */
155 static struct symbol *new_symbol(const char *name, struct module *module)
160 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
161 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
162 new->module = module;
166 static struct symbol *find_symbol(const char *name)
170 /* For our purposes, .foo matches foo. PPC64 needs this. */
174 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
175 if (strcmp(s->name, name) == 0)
182 * Add an exported symbol - it may have already been added without a
183 * CRC, in this case just update the CRC
185 static struct symbol *sym_add_exported(const char *name, struct module *mod)
187 struct symbol *s = find_symbol(name);
190 s = new_symbol(name, mod);
192 s->vmlinux = is_vmlinux(mod->name);
197 static void sym_update_crc(const char *name, struct module *mod,
200 struct symbol *s = find_symbol(name);
203 s = new_symbol(name, mod);
208 void *grab_file(const char *filename, unsigned long *size)
214 fd = open(filename, O_RDONLY);
215 if (fd < 0 || fstat(fd, &st) != 0)
219 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
222 if (map == MAP_FAILED)
228 * Return a copy of the next line in a mmap'ed file.
229 * spaces in the beginning of the line is trimmed away.
230 * Return a pointer to a static buffer.
232 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
234 static char line[4096];
237 signed char *p = (signed char *)file + *pos;
240 for (; *pos < size ; (*pos)++)
242 if (skip && isspace(*p)) {
247 if (*p != '\n' && (*pos < size)) {
251 break; /* Too long, stop */
262 void release_file(void *file, unsigned long size)
267 static void parse_elf(struct elf_info *info, const char *filename)
270 Elf_Ehdr *hdr = info->hdr;
274 hdr = grab_file(filename, &info->size);
280 if (info->size < sizeof(*hdr))
283 /* Fix endianness in ELF header */
284 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
285 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
286 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
287 hdr->e_machine = TO_NATIVE(hdr->e_machine);
288 sechdrs = (void *)hdr + hdr->e_shoff;
289 info->sechdrs = sechdrs;
291 /* Fix endianness in section headers */
292 for (i = 0; i < hdr->e_shnum; i++) {
293 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
294 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
295 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
296 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
297 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
299 /* Find symbol table. */
300 for (i = 1; i < hdr->e_shnum; i++) {
301 const char *secstrings
302 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
304 if (sechdrs[i].sh_offset > info->size)
306 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
307 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
308 info->modinfo_len = sechdrs[i].sh_size;
310 if (sechdrs[i].sh_type != SHT_SYMTAB)
313 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
314 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
315 + sechdrs[i].sh_size;
316 info->strtab = (void *)hdr +
317 sechdrs[sechdrs[i].sh_link].sh_offset;
319 if (!info->symtab_start) {
320 fatal("%s has no symtab?\n", filename);
322 /* Fix endianness in symbols */
323 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
324 sym->st_shndx = TO_NATIVE(sym->st_shndx);
325 sym->st_name = TO_NATIVE(sym->st_name);
326 sym->st_value = TO_NATIVE(sym->st_value);
327 sym->st_size = TO_NATIVE(sym->st_size);
332 fatal("%s is truncated.\n", filename);
335 static void parse_elf_finish(struct elf_info *info)
337 release_file(info->hdr, info->size);
340 #define CRC_PFX "__crc_"
341 #define KSYMTAB_PFX "__ksymtab_"
343 static void handle_modversions(struct module *mod, struct elf_info *info,
344 Elf_Sym *sym, const char *symname)
348 switch (sym->st_shndx) {
350 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
354 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
355 crc = (unsigned int) sym->st_value;
356 sym_update_crc(symname + strlen(CRC_PFX), mod, crc);
360 /* undefined symbol */
361 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
362 ELF_ST_BIND(sym->st_info) != STB_WEAK)
364 /* ignore global offset table */
365 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
367 /* ignore __this_module, it will be resolved shortly */
368 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
370 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
371 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
372 /* add compatibility with older glibc */
373 #ifndef STT_SPARC_REGISTER
374 #define STT_SPARC_REGISTER STT_REGISTER
376 if (info->hdr->e_machine == EM_SPARC ||
377 info->hdr->e_machine == EM_SPARCV9) {
378 /* Ignore register directives. */
379 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
381 if (symname[0] == '.') {
382 char *munged = strdup(symname);
384 munged[1] = toupper(munged[1]);
390 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
391 strlen(MODULE_SYMBOL_PREFIX)) == 0)
392 mod->unres = alloc_symbol(symname +
393 strlen(MODULE_SYMBOL_PREFIX),
394 ELF_ST_BIND(sym->st_info) == STB_WEAK,
398 /* All exported symbols */
399 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
400 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
402 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
404 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
405 mod->has_cleanup = 1;
411 * Parse tag=value strings from .modinfo section
413 static char *next_string(char *string, unsigned long *secsize)
415 /* Skip non-zero chars */
418 if ((*secsize)-- <= 1)
422 /* Skip any zero padding. */
425 if ((*secsize)-- <= 1)
431 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
435 unsigned int taglen = strlen(tag);
436 unsigned long size = modinfo_len;
438 for (p = modinfo; p; p = next_string(p, &size)) {
439 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
440 return p + taglen + 1;
445 static void read_symbols(char *modname)
450 struct elf_info info = { };
453 parse_elf(&info, modname);
455 mod = new_module(modname);
457 /* When there's no vmlinux, don't print warnings about
458 * unresolved symbols (since there'll be too many ;) */
459 if (is_vmlinux(modname)) {
464 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
465 symname = info.strtab + sym->st_name;
467 handle_modversions(mod, &info, sym, symname);
468 handle_moddevtable(mod, &info, sym, symname);
471 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
473 maybe_frob_rcs_version(modname, version, info.modinfo,
474 version - (char *)info.hdr);
475 if (version || (all_versions && !is_vmlinux(modname)))
476 get_src_version(modname, mod->srcversion,
477 sizeof(mod->srcversion)-1);
479 parse_elf_finish(&info);
481 /* Our trick to get versioning for struct_module - it's
482 * never passed as an argument to an exported function, so
483 * the automatic versioning doesn't pick it up, but it's really
484 * important anyhow */
486 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
491 /* We first write the generated file into memory using the
492 * following helper, then compare to the file on disk and
493 * only update the later if anything changed */
495 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
496 const char *fmt, ...)
503 len = vsnprintf(tmp, SZ, fmt, ap);
504 if (buf->size - buf->pos < len + 1) {
506 buf->p = realloc(buf->p, buf->size);
508 strncpy(buf->p + buf->pos, tmp, len + 1);
513 void buf_write(struct buffer *buf, const char *s, int len)
515 if (buf->size - buf->pos < len) {
517 buf->p = realloc(buf->p, buf->size);
519 strncpy(buf->p + buf->pos, s, len);
524 * Header for the generated file
526 static void add_header(struct buffer *b, struct module *mod)
528 buf_printf(b, "#include <linux/module.h>\n");
529 buf_printf(b, "#include <linux/vermagic.h>\n");
530 buf_printf(b, "#include <linux/compiler.h>\n");
532 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
534 buf_printf(b, "struct module __this_module\n");
535 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
536 buf_printf(b, " .name = KBUILD_MODNAME,\n");
538 buf_printf(b, " .init = init_module,\n");
539 if (mod->has_cleanup)
540 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
541 " .exit = cleanup_module,\n"
543 buf_printf(b, "};\n");
547 * Record CRCs for unresolved symbols
549 static void add_versions(struct buffer *b, struct module *mod)
551 struct symbol *s, *exp;
553 for (s = mod->unres; s; s = s->next) {
554 exp = find_symbol(s->name);
555 if (!exp || exp->module == mod) {
556 if (have_vmlinux && !s->weak)
557 warn("\"%s\" [%s.ko] undefined!\n",
561 s->module = exp->module;
562 s->crc_valid = exp->crc_valid;
570 buf_printf(b, "static const struct modversion_info ____versions[]\n");
571 buf_printf(b, "__attribute_used__\n");
572 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
574 for (s = mod->unres; s; s = s->next) {
579 warn("\"%s\" [%s.ko] has no CRC!\n",
583 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
586 buf_printf(b, "};\n");
589 static void add_depends(struct buffer *b, struct module *mod,
590 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");
620 static void add_srcversion(struct buffer *b, struct module *mod)
622 if (mod->srcversion[0]) {
624 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
629 static void write_if_changed(struct buffer *b, const char *fname)
635 file = fopen(fname, "r");
639 if (fstat(fileno(file), &st) < 0)
642 if (st.st_size != b->pos)
645 tmp = NOFAIL(malloc(b->pos));
646 if (fread(tmp, 1, b->pos, file) != b->pos)
649 if (memcmp(tmp, b->p, b->pos) != 0)
661 file = fopen(fname, "w");
666 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
673 static void read_dump(const char *fname, unsigned int kernel)
675 unsigned long size, pos = 0;
676 void *file = grab_file(fname, &size);
680 /* No symbol versions, silently ignore */
683 while ((line = get_next_line(&pos, file, size))) {
684 char *symname, *modname, *d;
689 if (!(symname = strchr(line, '\t')))
692 if (!(modname = strchr(symname, '\t')))
695 if (strchr(modname, '\t'))
697 crc = strtoul(line, &d, 16);
698 if (*symname == '\0' || *modname == '\0' || *d != '\0')
701 if (!(mod = find_module(modname))) {
702 if (is_vmlinux(modname)) {
705 mod = new_module(NOFAIL(strdup(modname)));
708 s = sym_add_exported(symname, mod);
710 sym_update_crc(symname, mod, crc);
714 fatal("parse error in symbol dump file\n");
717 /* For normal builds always dump all symbols.
718 * For external modules only dump symbols
719 * that are not read from kernel Module.symvers.
721 static int dump_sym(struct symbol *sym)
723 if (!external_module)
725 if (sym->vmlinux || sym->kernel)
730 static void write_dump(const char *fname)
732 struct buffer buf = { };
733 struct symbol *symbol;
736 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
737 symbol = symbolhash[n];
739 if (dump_sym(symbol))
740 buf_printf(&buf, "0x%08x\t%s\t%s\n",
741 symbol->crc, symbol->name,
742 symbol->module->name);
743 symbol = symbol->next;
746 write_if_changed(&buf, fname);
749 int main(int argc, char **argv)
752 struct buffer buf = { };
754 char *kernel_read = NULL, *module_read = NULL;
755 char *dump_write = NULL;
758 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
761 kernel_read = optarg;
764 module_read = optarg;
782 read_dump(kernel_read, 1);
784 read_dump(module_read, 0);
786 while (optind < argc) {
787 read_symbols(argv[optind++]);
790 for (mod = modules; mod; mod = mod->next) {
796 add_header(&buf, mod);
797 add_versions(&buf, mod);
798 add_depends(&buf, mod, modules);
799 add_moddevtable(&buf, mod);
800 add_srcversion(&buf, mod);
802 sprintf(fname, "%s.mod.c", mod->name);
803 write_if_changed(&buf, fname);
807 write_dump(dump_write);