vserver 1.9.3
[linux-2.6.git] / scripts / mod / modpost.c
1 /* Postprocess module symbol versions
2  *
3  * Copyright 2003       Kai Germaschewski
4  *           2002-2003  Rusty Russell, IBM Corporation
5  *
6  * Based in part on module-init-tools/depmod.c,file2alias
7  *
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  * Usage: modpost vmlinux module1.o module2.o ...
12  */
13
14 #include <ctype.h>
15 #include "modpost.h"
16
17 /* Are we using CONFIG_MODVERSIONS? */
18 int modversions = 0;
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
20 int have_vmlinux = 0;
21
22 void
23 fatal(const char *fmt, ...)
24 {
25         va_list arglist;
26
27         fprintf(stderr, "FATAL: ");
28
29         va_start(arglist, fmt);
30         vfprintf(stderr, fmt, arglist);
31         va_end(arglist);
32
33         exit(1);
34 }
35
36 void
37 warn(const char *fmt, ...)
38 {
39         va_list arglist;
40
41         fprintf(stderr, "WARNING: ");
42
43         va_start(arglist, fmt);
44         vfprintf(stderr, fmt, arglist);
45         va_end(arglist);
46 }
47
48 void *do_nofail(void *ptr, const char *file, int line, const char *expr)
49 {
50         if (!ptr) {
51                 fatal("Memory allocation failure %s line %d: %s.\n",
52                       file, line, expr);
53         }
54         return ptr;
55 }
56
57 /* A list of all modules we processed */
58
59 static struct module *modules;
60
61 struct module *
62 find_module(char *modname)
63 {
64         struct module *mod;
65
66         for (mod = modules; mod; mod = mod->next)
67                 if (strcmp(mod->name, modname) == 0)
68                         break;
69         return mod;
70 }
71
72 struct module *
73 new_module(char *modname)
74 {
75         struct module *mod;
76         char *p, *s;
77         
78         mod = NOFAIL(malloc(sizeof(*mod)));
79         memset(mod, 0, sizeof(*mod));
80         p = NOFAIL(strdup(modname));
81
82         /* strip trailing .o */
83         if ((s = strrchr(p, '.')) != NULL)
84                 if (strcmp(s, ".o") == 0)
85                         *s = '\0';
86
87         /* add to list */
88         mod->name = p;
89         mod->next = modules;
90         modules = mod;
91
92         return mod;
93 }
94
95 /* A hash of all exported symbols,
96  * struct symbol is also used for lists of unresolved symbols */
97
98 #define SYMBOL_HASH_SIZE 1024
99
100 struct symbol {
101         struct symbol *next;
102         struct module *module;
103         unsigned int crc;
104         int crc_valid;
105         char name[0];
106 };
107
108 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
109
110 /* This is based on the hash agorithm from gdbm, via tdb */
111 static inline unsigned int tdb_hash(const char *name)
112 {
113         unsigned value; /* Used to compute the hash value.  */
114         unsigned   i;   /* Used to cycle through random values. */
115
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)));
119
120         return (1103515243 * value + 12345);
121 }
122
123 /* Allocate a new symbols for use in the hash of exported symbols or
124  * the list of unresolved symbols per module */
125
126 struct symbol *
127 alloc_symbol(const char *name, struct symbol *next)
128 {
129         struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
130
131         memset(s, 0, sizeof(*s));
132         strcpy(s->name, name);
133         s->next = next;
134         return s;
135 }
136
137 /* For the hash of exported symbols */
138
139 void
140 new_symbol(const char *name, struct module *module, unsigned int *crc)
141 {
142         unsigned int hash;
143         struct symbol *new;
144
145         hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
146         new = symbolhash[hash] = alloc_symbol(name, symbolhash[hash]);
147         new->module = module;
148         if (crc) {
149                 new->crc = *crc;
150                 new->crc_valid = 1;
151         }
152 }
153
154 struct symbol *
155 find_symbol(const char *name)
156 {
157         struct symbol *s;
158
159         /* For our purposes, .foo matches foo.  PPC64 needs this. */
160         if (name[0] == '.')
161                 name++;
162
163         for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
164                 if (strcmp(s->name, name) == 0)
165                         return s;
166         }
167         return NULL;
168 }
169
170 /* Add an exported symbol - it may have already been added without a
171  * CRC, in this case just update the CRC */
172 void
173 add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
174 {
175         struct symbol *s = find_symbol(name);
176
177         if (!s) {
178                 new_symbol(name, module, crc);
179                 return;
180         }
181         if (crc) {
182                 s->crc = *crc;
183                 s->crc_valid = 1;
184         }
185 }
186
187 void *
188 grab_file(const char *filename, unsigned long *size)
189 {
190         struct stat st;
191         void *map;
192         int fd;
193
194         fd = open(filename, O_RDONLY);
195         if (fd < 0 || fstat(fd, &st) != 0)
196                 return NULL;
197
198         *size = st.st_size;
199         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
200         close(fd);
201
202         if (map == MAP_FAILED)
203                 return NULL;
204         return map;
205 }
206
207 /*
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.
211 */
212 char*
213 get_next_line(unsigned long *pos, void *file, unsigned long size)
214 {
215         static char line[4096];
216         int skip = 1;
217         size_t len = 0;
218         char *p = (char *)file + *pos;
219         char *s = line;
220
221         for (; *pos < size ; (*pos)++)
222         {
223                 if (skip && isspace(*p)) {
224                         p++;
225                         continue;
226                 }
227                 skip = 0;
228                 if (*p != '\n' && (*pos < size)) {
229                         len++;
230                         *s++ = *p++;
231                         if (len > 4095)
232                                 break; /* Too long, stop */
233                 } else {
234                         /* End of string */
235                         *s = '\0';
236                         return line;
237                 }
238         }
239         /* End of buffer */
240         return NULL;
241 }
242
243 void
244 release_file(void *file, unsigned long size)
245 {
246         munmap(file, size);
247 }
248
249 void
250 parse_elf(struct elf_info *info, const char *filename)
251 {
252         unsigned int i;
253         Elf_Ehdr *hdr = info->hdr;
254         Elf_Shdr *sechdrs;
255         Elf_Sym  *sym;
256
257         hdr = grab_file(filename, &info->size);
258         if (!hdr) {
259                 perror(filename);
260                 abort();
261         }
262         info->hdr = hdr;
263         if (info->size < sizeof(*hdr))
264                 goto truncated;
265
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;
273
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);
281         }
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;
286
287                 if (sechdrs[i].sh_offset > info->size)
288                         goto truncated;
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;
292                 }
293                 if (sechdrs[i].sh_type != SHT_SYMTAB)
294                         continue;
295
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;
301         }
302         if (!info->symtab_start) {
303                 fprintf(stderr, "modpost: %s no symtab?\n", filename);
304                 abort();
305         }
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);
312         }
313         return;
314
315  truncated:
316         fprintf(stderr, "modpost: %s is truncated.\n", filename);
317         abort();
318 }
319
320 void
321 parse_elf_finish(struct elf_info *info)
322 {
323         release_file(info->hdr, info->size);
324 }
325
326 #define CRC_PFX     MODULE_SYMBOL_PREFIX "__crc_"
327 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
328
329 void
330 handle_modversions(struct module *mod, struct elf_info *info,
331                    Elf_Sym *sym, const char *symname)
332 {
333         unsigned int crc;
334
335         switch (sym->st_shndx) {
336         case SHN_COMMON:
337                 fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n",
338                         symname, mod->name);
339                 break;
340         case SHN_ABS:
341                 /* CRC'd symbol */
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),
345                                             mod, &crc);
346                 }
347                 break;
348         case SHN_UNDEF:
349                 /* undefined symbol */
350                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL)
351                         break;
352                 /* ignore global offset table */
353                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
354                         break;
355                 /* ignore __this_module, it will be resolved shortly */
356                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
357                         break;
358 #ifdef STT_REGISTER
359                 if (info->hdr->e_machine == EM_SPARC ||
360                     info->hdr->e_machine == EM_SPARCV9) {
361                         /* Ignore register directives. */
362                         if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
363                                 break;
364                 }
365 #endif
366                 
367                 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
368                            strlen(MODULE_SYMBOL_PREFIX)) == 0)
369                         mod->unres = alloc_symbol(symname +
370                                                   strlen(MODULE_SYMBOL_PREFIX),
371                                                   mod->unres);
372                 break;
373         default:
374                 /* All exported symbols */
375                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
376                         add_exported_symbol(symname + strlen(KSYMTAB_PFX),
377                                             mod, NULL);
378                 }
379                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
380                         mod->has_init = 1;
381                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
382                         mod->has_cleanup = 1;
383                 break;
384         }
385 }
386
387 int
388 is_vmlinux(const char *modname)
389 {
390         const char *myname;
391
392         if ((myname = strrchr(modname, '/')))
393                 myname++;
394         else
395                 myname = modname;
396
397         return strcmp(myname, "vmlinux") == 0;
398 }
399
400 void
401 read_symbols(char *modname)
402 {
403         const char *symname;
404         struct module *mod;
405         struct elf_info info = { };
406         Elf_Sym *sym;
407
408         parse_elf(&info, modname);
409
410         mod = new_module(modname);
411
412         /* When there's no vmlinux, don't print warnings about
413          * unresolved symbols (since there'll be too many ;) */
414         if (is_vmlinux(modname)) {
415                 unsigned int fake_crc = 0;
416                 have_vmlinux = 1;
417                 add_exported_symbol("struct_module", mod, &fake_crc);
418                 mod->skip = 1;
419         }
420
421         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
422                 symname = info.strtab + sym->st_name;
423
424                 handle_modversions(mod, &info, sym, symname);
425                 handle_moddevtable(mod, &info, sym, symname);
426         }
427         maybe_frob_version(modname, info.modinfo, info.modinfo_len,
428                            (void *)info.modinfo - (void *)info.hdr);
429         parse_elf_finish(&info);
430
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 */
435         if (modversions)
436                 mod->unres = alloc_symbol("struct_module", mod->unres);
437 }
438
439 #define SZ 500
440
441 /* We first write the generated file into memory using the
442  * following helper, then compare to the file on disk and
443  * only update the later if anything changed */
444
445 void __attribute__((format(printf, 2, 3)))
446 buf_printf(struct buffer *buf, const char *fmt, ...)
447 {
448         char tmp[SZ];
449         int len;
450         va_list ap;
451         
452         va_start(ap, fmt);
453         len = vsnprintf(tmp, SZ, fmt, ap);
454         if (buf->size - buf->pos < len + 1) {
455                 buf->size += 128;
456                 buf->p = realloc(buf->p, buf->size);
457         }
458         strncpy(buf->p + buf->pos, tmp, len + 1);
459         buf->pos += len;
460         va_end(ap);
461 }
462
463 void
464 buf_write(struct buffer *buf, const char *s, int len)
465 {
466         if (buf->size - buf->pos < len) {
467                 buf->size += len;
468                 buf->p = realloc(buf->p, buf->size);
469         }
470         strncpy(buf->p + buf->pos, s, len);
471         buf->pos += len;
472 }
473
474 /* Header for the generated file */
475
476 void
477 add_header(struct buffer *b, struct module *mod)
478 {
479         buf_printf(b, "#include <linux/module.h>\n");
480         buf_printf(b, "#include <linux/vermagic.h>\n");
481         buf_printf(b, "#include <linux/compiler.h>\n");
482         buf_printf(b, "\n");
483         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
484         buf_printf(b, "\n");
485         buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
486         buf_printf(b, "struct module __this_module\n");
487         buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
488         buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
489         if (mod->has_init)
490                 buf_printf(b, " .init = init_module,\n");
491         if (mod->has_cleanup)
492                 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
493                               " .exit = cleanup_module,\n"
494                               "#endif\n");
495         buf_printf(b, "};\n");
496 }
497
498 /* Record CRCs for unresolved symbols */
499
500 void
501 add_versions(struct buffer *b, struct module *mod)
502 {
503         struct symbol *s, *exp;
504
505         for (s = mod->unres; s; s = s->next) {
506                 exp = find_symbol(s->name);
507                 if (!exp || exp->module == mod) {
508                         if (have_vmlinux)
509                                 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
510                                 "undefined!\n", s->name, mod->name);
511                         continue;
512                 }
513                 s->module = exp->module;
514                 s->crc_valid = exp->crc_valid;
515                 s->crc = exp->crc;
516         }
517
518         if (!modversions)
519                 return;
520
521         buf_printf(b, "\n");
522         buf_printf(b, "static const struct modversion_info ____versions[]\n");
523         buf_printf(b, "__attribute_used__\n");
524         buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
525
526         for (s = mod->unres; s; s = s->next) {
527                 if (!s->module) {
528                         continue;
529                 }
530                 if (!s->crc_valid) {
531                         fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
532                                 "has no CRC!\n",
533                                 s->name, mod->name);
534                         continue;
535                 }
536                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
537         }
538
539         buf_printf(b, "};\n");
540 }
541
542 void
543 add_depends(struct buffer *b, struct module *mod, struct module *modules)
544 {
545         struct symbol *s;
546         struct module *m;
547         int first = 1;
548
549         for (m = modules; m; m = m->next) {
550                 m->seen = is_vmlinux(m->name);
551         }
552
553         buf_printf(b, "\n");
554         buf_printf(b, "static const char __module_depends[]\n");
555         buf_printf(b, "__attribute_used__\n");
556         buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
557         buf_printf(b, "\"depends=");
558         for (s = mod->unres; s; s = s->next) {
559                 if (!s->module)
560                         continue;
561
562                 if (s->module->seen)
563                         continue;
564
565                 s->module->seen = 1;
566                 buf_printf(b, "%s%s", first ? "" : ",",
567                            strrchr(s->module->name, '/') + 1);
568                 first = 0;
569         }
570         buf_printf(b, "\";\n");
571 }
572
573 void
574 write_if_changed(struct buffer *b, const char *fname)
575 {
576         char *tmp;
577         FILE *file;
578         struct stat st;
579
580         file = fopen(fname, "r");
581         if (!file)
582                 goto write;
583
584         if (fstat(fileno(file), &st) < 0)
585                 goto close_write;
586
587         if (st.st_size != b->pos)
588                 goto close_write;
589
590         tmp = NOFAIL(malloc(b->pos));
591         if (fread(tmp, 1, b->pos, file) != b->pos)
592                 goto free_write;
593
594         if (memcmp(tmp, b->p, b->pos) != 0)
595                 goto free_write;
596
597         free(tmp);
598         fclose(file);
599         return;
600
601  free_write:
602         free(tmp);
603  close_write:
604         fclose(file);
605  write:
606         file = fopen(fname, "w");
607         if (!file) {
608                 perror(fname);
609                 exit(1);
610         }
611         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
612                 perror(fname);
613                 exit(1);
614         }
615         fclose(file);
616 }
617
618 void
619 read_dump(const char *fname)
620 {
621         unsigned long size, pos = 0;
622         void *file = grab_file(fname, &size);
623         char *line;
624
625         if (!file)
626                 /* No symbol versions, silently ignore */
627                 return;
628
629         while ((line = get_next_line(&pos, file, size))) {
630                 char *symname, *modname, *d;
631                 unsigned int crc;
632                 struct module *mod;
633
634                 if (!(symname = strchr(line, '\t')))
635                         goto fail;
636                 *symname++ = '\0';
637                 if (!(modname = strchr(symname, '\t')))
638                         goto fail;
639                 *modname++ = '\0';
640                 if (strchr(modname, '\t'))
641                         goto fail;
642                 crc = strtoul(line, &d, 16);
643                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
644                         goto fail;
645
646                 if (!(mod = find_module(modname))) {
647                         if (is_vmlinux(modname)) {
648                                 have_vmlinux = 1;
649                         }
650                         mod = new_module(NOFAIL(strdup(modname)));
651                         mod->skip = 1;
652                 }
653                 add_exported_symbol(symname, mod, &crc);
654         }
655         return;
656 fail:
657         fatal("parse error in symbol dump file\n");
658 }
659
660 void
661 write_dump(const char *fname)
662 {
663         struct buffer buf = { };
664         struct symbol *symbol;
665         int n;
666
667         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
668                 symbol = symbolhash[n];
669                 while (symbol) {
670                         symbol = symbol->next;
671                 }
672         }
673
674         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
675                 symbol = symbolhash[n];
676                 while (symbol) {
677                         buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
678                                 symbol->name, symbol->module->name);
679                         symbol = symbol->next;
680                 }
681         }
682         write_if_changed(&buf, fname);
683 }
684
685 int
686 main(int argc, char **argv)
687 {
688         struct module *mod;
689         struct buffer buf = { };
690         char fname[SZ];
691         char *dump_read = NULL, *dump_write = NULL;
692         int opt;
693
694         while ((opt = getopt(argc, argv, "i:mo:")) != -1) {
695                 switch(opt) {
696                         case 'i':
697                                 dump_read = optarg;
698                                 break;
699                         case 'm':
700                                 modversions = 1;
701                                 break;
702                         case 'o':
703                                 dump_write = optarg;
704                                 break;
705                         default:
706                                 exit(1);
707                 }
708         }
709
710         if (dump_read)
711                 read_dump(dump_read);
712
713         while (optind < argc) {
714                 read_symbols(argv[optind++]);
715         }
716
717         for (mod = modules; mod; mod = mod->next) {
718                 if (mod->skip)
719                         continue;
720
721                 buf.pos = 0;
722
723                 add_header(&buf, mod);
724                 add_versions(&buf, mod);
725                 add_depends(&buf, mod, modules);
726                 add_moddevtable(&buf, mod);
727
728                 sprintf(fname, "%s.mod.c", mod->name);
729                 write_if_changed(&buf, fname);
730         }
731
732         if (dump_write)
733                 write_dump(dump_write);
734
735         return 0;
736 }
737