patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / scripts / 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                         modversions = 1;
347                 }
348                 break;
349         case SHN_UNDEF:
350                 /* undefined symbol */
351                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL)
352                         break;
353                 /* ignore global offset table */
354                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
355                         break;
356                 /* ignore __this_module, it will be resolved shortly */
357                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
358                         break;
359 #ifdef STT_REGISTER
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)
364                                 break;
365                 }
366 #endif
367                 
368                 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
369                            strlen(MODULE_SYMBOL_PREFIX)) == 0)
370                         mod->unres = alloc_symbol(symname +
371                                                   strlen(MODULE_SYMBOL_PREFIX),
372                                                   mod->unres);
373                 break;
374         default:
375                 /* All exported symbols */
376                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
377                         add_exported_symbol(symname + strlen(KSYMTAB_PFX),
378                                             mod, NULL);
379                 }
380                 break;
381         }
382 }
383
384 int
385 is_vmlinux(const char *modname)
386 {
387         const char *myname;
388
389         if ((myname = strrchr(modname, '/')))
390                 myname++;
391         else
392                 myname = modname;
393
394         return strcmp(myname, "vmlinux") == 0;
395 }
396
397 void
398 read_symbols(char *modname)
399 {
400         const char *symname;
401         struct module *mod;
402         struct elf_info info = { };
403         Elf_Sym *sym;
404
405         parse_elf(&info, modname);
406
407         mod = new_module(modname);
408
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;
413                 have_vmlinux = 1;
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);
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                 /* 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);
442         }
443 }
444
445 #define SZ 500
446
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 */
450
451 void __attribute__((format(printf, 2, 3)))
452 buf_printf(struct buffer *buf, const char *fmt, ...)
453 {
454         char tmp[SZ];
455         int len;
456         va_list ap;
457         
458         va_start(ap, fmt);
459         len = vsnprintf(tmp, SZ, fmt, ap);
460         if (buf->size - buf->pos < len + 1) {
461                 buf->size += 128;
462                 buf->p = realloc(buf->p, buf->size);
463         }
464         strncpy(buf->p + buf->pos, tmp, len + 1);
465         buf->pos += len;
466         va_end(ap);
467 }
468
469 void
470 buf_write(struct buffer *buf, const char *s, int len)
471 {
472         if (buf->size - buf->pos < len) {
473                 buf->size += len;
474                 buf->p = realloc(buf->p, buf->size);
475         }
476         strncpy(buf->p + buf->pos, s, len);
477         buf->pos += len;
478 }
479
480 /* Header for the generated file */
481
482 void
483 add_header(struct buffer *b)
484 {
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");
488         buf_printf(b, "\n");
489         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
490         buf_printf(b, "\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");
500 }
501
502 /* Record CRCs for unresolved symbols */
503
504 void
505 add_versions(struct buffer *b, struct module *mod)
506 {
507         struct symbol *s, *exp;
508
509         for (s = mod->unres; s; s = s->next) {
510                 exp = find_symbol(s->name);
511                 if (!exp || exp->module == mod) {
512                         if (have_vmlinux)
513                                 fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
514                                 "undefined!\n", s->name, mod->name);
515                         continue;
516                 }
517                 s->module = exp->module;
518                 s->crc_valid = exp->crc_valid;
519                 s->crc = exp->crc;
520         }
521
522         if (!modversions)
523                 return;
524
525         buf_printf(b, "\n");
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");
529
530         for (s = mod->unres; s; s = s->next) {
531                 if (!s->module) {
532                         continue;
533                 }
534                 if (!s->crc_valid) {
535                         fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
536                                 "has no CRC!\n",
537                                 s->name, mod->name);
538                         continue;
539                 }
540                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
541         }
542
543         buf_printf(b, "};\n");
544 }
545
546 void
547 add_depends(struct buffer *b, struct module *mod, struct module *modules)
548 {
549         struct symbol *s;
550         struct module *m;
551         int first = 1;
552
553         for (m = modules; m; m = m->next) {
554                 m->seen = is_vmlinux(m->name);
555         }
556
557         buf_printf(b, "\n");
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) {
563                 if (!s->module)
564                         continue;
565
566                 if (s->module->seen)
567                         continue;
568
569                 s->module->seen = 1;
570                 buf_printf(b, "%s%s", first ? "" : ",",
571                            strrchr(s->module->name, '/') + 1);
572                 first = 0;
573         }
574         buf_printf(b, "\";\n");
575 }
576
577 void
578 write_if_changed(struct buffer *b, const char *fname)
579 {
580         char *tmp;
581         FILE *file;
582         struct stat st;
583
584         file = fopen(fname, "r");
585         if (!file)
586                 goto write;
587
588         if (fstat(fileno(file), &st) < 0)
589                 goto close_write;
590
591         if (st.st_size != b->pos)
592                 goto close_write;
593
594         tmp = NOFAIL(malloc(b->pos));
595         if (fread(tmp, 1, b->pos, file) != b->pos)
596                 goto free_write;
597
598         if (memcmp(tmp, b->p, b->pos) != 0)
599                 goto free_write;
600
601         free(tmp);
602         fclose(file);
603         return;
604
605  free_write:
606         free(tmp);
607  close_write:
608         fclose(file);
609  write:
610         file = fopen(fname, "w");
611         if (!file) {
612                 perror(fname);
613                 exit(1);
614         }
615         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
616                 perror(fname);
617                 exit(1);
618         }
619         fclose(file);
620 }
621
622 void
623 read_dump(const char *fname)
624 {
625         unsigned long size, pos = 0;
626         void *file = grab_file(fname, &size);
627         char *line;
628
629         if (!file)
630                 /* No symbol versions, silently ignore */
631                 return;
632
633         while ((line = get_next_line(&pos, file, size))) {
634                 char *symname, *modname, *d;
635                 unsigned int crc;
636                 struct module *mod;
637
638                 if (!(symname = strchr(line, '\t')))
639                         goto fail;
640                 *symname++ = '\0';
641                 if (!(modname = strchr(symname, '\t')))
642                         goto fail;
643                 *modname++ = '\0';
644                 if (strchr(modname, '\t'))
645                         goto fail;
646                 crc = strtoul(line, &d, 16);
647                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
648                         goto fail;
649
650                 if (!(mod = find_module(modname))) {
651                         if (is_vmlinux(modname)) {
652                                 modversions = 1;
653                                 have_vmlinux = 1;
654                         }
655                         mod = new_module(NOFAIL(strdup(modname)));
656                         mod->skip = 1;
657                 }
658                 add_exported_symbol(symname, mod, &crc);
659         }
660         return;
661 fail:
662         fatal("parse error in symbol dump file\n");
663 }
664
665 void
666 write_dump(const char *fname)
667 {
668         struct buffer buf = { };
669         struct symbol *symbol;
670         int n;
671
672         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
673                 symbol = symbolhash[n];
674                 while (symbol) {
675                         symbol = symbol->next;
676                 }
677         }
678
679         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
680                 symbol = symbolhash[n];
681                 while (symbol) {
682                         buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
683                                 symbol->name, symbol->module->name);
684                         symbol = symbol->next;
685                 }
686         }
687         write_if_changed(&buf, fname);
688 }
689
690 int
691 main(int argc, char **argv)
692 {
693         struct module *mod;
694         struct buffer buf = { };
695         char fname[SZ];
696         char *dump_read = NULL, *dump_write = NULL;
697         int opt;
698
699         while ((opt = getopt(argc, argv, "i:o:")) != -1) {
700                 switch(opt) {
701                         case 'i':
702                                 dump_read = optarg;
703                                 break;
704                         case 'o':
705                                 dump_write = optarg;
706                                 break;
707                         default:
708                                 exit(1);
709                 }
710         }
711
712         if (dump_read)
713                 read_dump(dump_read);
714
715         while (optind < argc) {
716                 read_symbols(argv[optind++]);
717         }
718
719         for (mod = modules; mod; mod = mod->next) {
720                 if (mod->skip)
721                         continue;
722
723                 buf.pos = 0;
724
725                 add_header(&buf);
726                 add_versions(&buf, mod);
727                 add_depends(&buf, mod, modules);
728                 add_moddevtable(&buf, mod);
729
730                 sprintf(fname, "%s.mod.c", mod->name);
731                 write_if_changed(&buf, fname);
732         }
733
734         if (dump_write)
735                 write_dump(dump_write);
736
737         return 0;
738 }
739