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;
24 void fatal(const char *fmt, ...)
28 fprintf(stderr, "FATAL: ");
30 va_start(arglist, fmt);
31 vfprintf(stderr, fmt, arglist);
37 void 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 *expr)
51 fatal("modpost: Memory allocation failure: %s.\n", expr);
56 /* A list of all modules we processed */
58 static struct module *modules;
60 static struct module *find_module(char *modname)
64 for (mod = modules; mod; mod = mod->next)
65 if (strcmp(mod->name, modname) == 0)
70 static struct module *new_module(char *modname)
75 mod = NOFAIL(malloc(sizeof(*mod)));
76 memset(mod, 0, sizeof(*mod));
77 p = NOFAIL(strdup(modname));
79 /* strip trailing .o */
80 if ((s = strrchr(p, '.')) != NULL)
81 if (strcmp(s, ".o") == 0)
92 /* A hash of all exported symbols,
93 * struct symbol is also used for lists of unresolved symbols */
95 #define SYMBOL_HASH_SIZE 1024
99 struct module *module;
106 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
108 /* This is based on the hash agorithm from gdbm, via tdb */
109 static inline unsigned int tdb_hash(const char *name)
111 unsigned value; /* Used to compute the hash value. */
112 unsigned i; /* Used to cycle through random values. */
114 /* Set the initial value from the key size. */
115 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
116 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
118 return (1103515243 * value + 12345);
122 * Allocate a new symbols for use in the hash of exported symbols or
123 * the list of unresolved symbols per module
125 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
128 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
130 memset(s, 0, sizeof(*s));
131 strcpy(s->name, name);
137 /* For the hash of exported symbols */
138 static void new_symbol(const char *name, struct module *module,
144 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
145 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
146 new->module = module;
153 static struct symbol *find_symbol(const char *name)
157 /* For our purposes, .foo matches foo. PPC64 needs this. */
161 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
162 if (strcmp(s->name, name) == 0)
169 * Add an exported symbol - it may have already been added without a
170 * CRC, in this case just update the CRC
172 static void add_exported_symbol(const char *name, struct module *module,
175 struct symbol *s = find_symbol(name);
178 new_symbol(name, module, crc);
187 void *grab_file(const char *filename, unsigned long *size)
193 fd = open(filename, O_RDONLY);
194 if (fd < 0 || fstat(fd, &st) != 0)
198 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
201 if (map == MAP_FAILED)
207 * Return a copy of the next line in a mmap'ed file.
208 * spaces in the beginning of the line is trimmed away.
209 * Return a pointer to a static buffer.
211 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
213 static char line[4096];
216 signed char *p = (signed char *)file + *pos;
219 for (; *pos < size ; (*pos)++)
221 if (skip && isspace(*p)) {
226 if (*p != '\n' && (*pos < size)) {
230 break; /* Too long, stop */
241 void release_file(void *file, unsigned long size)
246 static void parse_elf(struct elf_info *info, const char *filename)
249 Elf_Ehdr *hdr = info->hdr;
253 hdr = grab_file(filename, &info->size);
259 if (info->size < sizeof(*hdr))
262 /* Fix endianness in ELF header */
263 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
264 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
265 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
266 hdr->e_machine = TO_NATIVE(hdr->e_machine);
267 sechdrs = (void *)hdr + hdr->e_shoff;
268 info->sechdrs = sechdrs;
270 /* Fix endianness in section headers */
271 for (i = 0; i < hdr->e_shnum; i++) {
272 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
273 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
274 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
275 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
276 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
278 /* Find symbol table. */
279 for (i = 1; i < hdr->e_shnum; i++) {
280 const char *secstrings
281 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
283 if (sechdrs[i].sh_offset > info->size)
285 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
286 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
287 info->modinfo_len = sechdrs[i].sh_size;
289 if (sechdrs[i].sh_type != SHT_SYMTAB)
292 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
293 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
294 + sechdrs[i].sh_size;
295 info->strtab = (void *)hdr +
296 sechdrs[sechdrs[i].sh_link].sh_offset;
298 if (!info->symtab_start) {
299 fatal("%s has no symtab?\n", filename);
301 /* Fix endianness in symbols */
302 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
303 sym->st_shndx = TO_NATIVE(sym->st_shndx);
304 sym->st_name = TO_NATIVE(sym->st_name);
305 sym->st_value = TO_NATIVE(sym->st_value);
306 sym->st_size = TO_NATIVE(sym->st_size);
311 fatal("%s is truncated.\n", filename);
314 static void parse_elf_finish(struct elf_info *info)
316 release_file(info->hdr, info->size);
319 #define CRC_PFX "__crc_"
320 #define KSYMTAB_PFX "__ksymtab_"
322 static void handle_modversions(struct module *mod, struct elf_info *info,
323 Elf_Sym *sym, const char *symname)
327 switch (sym->st_shndx) {
329 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
333 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
334 crc = (unsigned int) sym->st_value;
335 add_exported_symbol(symname + strlen(CRC_PFX),
340 /* undefined symbol */
341 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
342 ELF_ST_BIND(sym->st_info) != STB_WEAK)
344 /* ignore global offset table */
345 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
347 /* ignore __this_module, it will be resolved shortly */
348 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
350 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
351 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
352 /* add compatibility with older glibc */
353 #ifndef STT_SPARC_REGISTER
354 #define STT_SPARC_REGISTER STT_REGISTER
356 if (info->hdr->e_machine == EM_SPARC ||
357 info->hdr->e_machine == EM_SPARCV9) {
358 /* Ignore register directives. */
359 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
361 if (symname[0] == '.') {
362 char *munged = strdup(symname);
364 munged[1] = toupper(munged[1]);
370 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
371 strlen(MODULE_SYMBOL_PREFIX)) == 0)
372 mod->unres = alloc_symbol(symname +
373 strlen(MODULE_SYMBOL_PREFIX),
374 ELF_ST_BIND(sym->st_info) == STB_WEAK,
378 /* All exported symbols */
379 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
380 add_exported_symbol(symname + strlen(KSYMTAB_PFX),
383 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
385 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
386 mod->has_cleanup = 1;
391 static int is_vmlinux(const char *modname)
395 if ((myname = strrchr(modname, '/')))
400 return strcmp(myname, "vmlinux") == 0;
404 * Parse tag=value strings from .modinfo section
406 static char *next_string(char *string, unsigned long *secsize)
408 /* Skip non-zero chars */
411 if ((*secsize)-- <= 1)
415 /* Skip any zero padding. */
418 if ((*secsize)-- <= 1)
424 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
428 unsigned int taglen = strlen(tag);
429 unsigned long size = modinfo_len;
431 for (p = modinfo; p; p = next_string(p, &size)) {
432 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
433 return p + taglen + 1;
438 static void read_symbols(char *modname)
443 struct elf_info info = { };
446 parse_elf(&info, modname);
448 mod = new_module(modname);
450 /* When there's no vmlinux, don't print warnings about
451 * unresolved symbols (since there'll be too many ;) */
452 if (is_vmlinux(modname)) {
453 unsigned int fake_crc = 0;
455 add_exported_symbol("struct_module", mod, &fake_crc);
459 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
460 symname = info.strtab + sym->st_name;
462 handle_modversions(mod, &info, sym, symname);
463 handle_moddevtable(mod, &info, sym, symname);
466 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
468 maybe_frob_rcs_version(modname, version, info.modinfo,
469 version - (char *)info.hdr);
470 if (version || (all_versions && !is_vmlinux(modname)))
471 get_src_version(modname, mod->srcversion,
472 sizeof(mod->srcversion)-1);
474 parse_elf_finish(&info);
476 /* Our trick to get versioning for struct_module - it's
477 * never passed as an argument to an exported function, so
478 * the automatic versioning doesn't pick it up, but it's really
479 * important anyhow */
481 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
486 /* We first write the generated file into memory using the
487 * following helper, then compare to the file on disk and
488 * only update the later if anything changed */
490 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
491 const char *fmt, ...)
498 len = vsnprintf(tmp, SZ, fmt, ap);
499 if (buf->size - buf->pos < len + 1) {
501 buf->p = realloc(buf->p, buf->size);
503 strncpy(buf->p + buf->pos, tmp, len + 1);
508 void buf_write(struct buffer *buf, const char *s, int len)
510 if (buf->size - buf->pos < len) {
512 buf->p = realloc(buf->p, buf->size);
514 strncpy(buf->p + buf->pos, s, len);
519 * Header for the generated file
521 static void add_header(struct buffer *b, struct module *mod)
523 buf_printf(b, "#include <linux/module.h>\n");
524 buf_printf(b, "#include <linux/vermagic.h>\n");
525 buf_printf(b, "#include <linux/compiler.h>\n");
527 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
529 buf_printf(b, "struct module __this_module\n");
530 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
531 buf_printf(b, " .name = KBUILD_MODNAME,\n");
533 buf_printf(b, " .init = init_module,\n");
534 if (mod->has_cleanup)
535 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
536 " .exit = cleanup_module,\n"
538 buf_printf(b, "};\n");
542 * Record CRCs for unresolved symbols
544 static void add_versions(struct buffer *b, struct module *mod)
546 struct symbol *s, *exp;
548 for (s = mod->unres; s; s = s->next) {
549 exp = find_symbol(s->name);
550 if (!exp || exp->module == mod) {
551 if (have_vmlinux && !s->weak)
552 warn("\"%s\" [%s.ko] undefined!\n",
556 s->module = exp->module;
557 s->crc_valid = exp->crc_valid;
565 buf_printf(b, "static const struct modversion_info ____versions[]\n");
566 buf_printf(b, "__attribute_used__\n");
567 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
569 for (s = mod->unres; s; s = s->next) {
574 warn("\"%s\" [%s.ko] has no CRC!\n",
578 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
581 buf_printf(b, "};\n");
584 static void add_depends(struct buffer *b, struct module *mod,
585 struct module *modules)
591 for (m = modules; m; m = m->next) {
592 m->seen = is_vmlinux(m->name);
596 buf_printf(b, "static const char __module_depends[]\n");
597 buf_printf(b, "__attribute_used__\n");
598 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
599 buf_printf(b, "\"depends=");
600 for (s = mod->unres; s; s = s->next) {
608 buf_printf(b, "%s%s", first ? "" : ",",
609 strrchr(s->module->name, '/') + 1);
612 buf_printf(b, "\";\n");
615 static void add_srcversion(struct buffer *b, struct module *mod)
617 if (mod->srcversion[0]) {
619 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
624 static void write_if_changed(struct buffer *b, const char *fname)
630 file = fopen(fname, "r");
634 if (fstat(fileno(file), &st) < 0)
637 if (st.st_size != b->pos)
640 tmp = NOFAIL(malloc(b->pos));
641 if (fread(tmp, 1, b->pos, file) != b->pos)
644 if (memcmp(tmp, b->p, b->pos) != 0)
656 file = fopen(fname, "w");
661 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
668 static void read_dump(const char *fname)
670 unsigned long size, pos = 0;
671 void *file = grab_file(fname, &size);
675 /* No symbol versions, silently ignore */
678 while ((line = get_next_line(&pos, file, size))) {
679 char *symname, *modname, *d;
683 if (!(symname = strchr(line, '\t')))
686 if (!(modname = strchr(symname, '\t')))
689 if (strchr(modname, '\t'))
691 crc = strtoul(line, &d, 16);
692 if (*symname == '\0' || *modname == '\0' || *d != '\0')
695 if (!(mod = find_module(modname))) {
696 if (is_vmlinux(modname)) {
699 mod = new_module(NOFAIL(strdup(modname)));
702 add_exported_symbol(symname, mod, &crc);
706 fatal("parse error in symbol dump file\n");
709 static void write_dump(const char *fname)
711 struct buffer buf = { };
712 struct symbol *symbol;
715 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
716 symbol = symbolhash[n];
718 symbol = symbol->next;
722 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
723 symbol = symbolhash[n];
725 buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
726 symbol->name, symbol->module->name);
727 symbol = symbol->next;
730 write_if_changed(&buf, fname);
733 int main(int argc, char **argv)
736 struct buffer buf = { };
738 char *dump_read = NULL, *dump_write = NULL;
741 while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
761 read_dump(dump_read);
763 while (optind < argc) {
764 read_symbols(argv[optind++]);
767 for (mod = modules; mod; mod = mod->next) {
773 add_header(&buf, mod);
774 add_versions(&buf, mod);
775 add_depends(&buf, mod, modules);
776 add_moddevtable(&buf, mod);
777 add_srcversion(&buf, mod);
779 sprintf(fname, "%s.mod.c", mod->name);
780 write_if_changed(&buf, fname);
784 write_dump(dump_write);