kbuild: improved modversioning support for external modules
[pandora-kernel.git] / scripts / mod / modpost.c
1 /* Postprocess module symbol versions
2  *
3  * Copyright 2003       Kai Germaschewski
4  * Copyright 2002-2004  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 /* 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;
25
26 void fatal(const char *fmt, ...)
27 {
28         va_list arglist;
29
30         fprintf(stderr, "FATAL: ");
31
32         va_start(arglist, fmt);
33         vfprintf(stderr, fmt, arglist);
34         va_end(arglist);
35
36         exit(1);
37 }
38
39 void warn(const char *fmt, ...)
40 {
41         va_list arglist;
42
43         fprintf(stderr, "WARNING: ");
44
45         va_start(arglist, fmt);
46         vfprintf(stderr, fmt, arglist);
47         va_end(arglist);
48 }
49
50 static int is_vmlinux(const char *modname)
51 {
52         const char *myname;
53
54         if ((myname = strrchr(modname, '/')))
55                 myname++;
56         else
57                 myname = modname;
58
59         return strcmp(myname, "vmlinux") == 0;
60 }
61
62 void *do_nofail(void *ptr, const char *expr)
63 {
64         if (!ptr) {
65                 fatal("modpost: Memory allocation failure: %s.\n", expr);
66         }
67         return ptr;
68 }
69
70 /* A list of all modules we processed */
71
72 static struct module *modules;
73
74 static struct module *find_module(char *modname)
75 {
76         struct module *mod;
77
78         for (mod = modules; mod; mod = mod->next)
79                 if (strcmp(mod->name, modname) == 0)
80                         break;
81         return mod;
82 }
83
84 static struct module *new_module(char *modname)
85 {
86         struct module *mod;
87         char *p, *s;
88         
89         mod = NOFAIL(malloc(sizeof(*mod)));
90         memset(mod, 0, sizeof(*mod));
91         p = NOFAIL(strdup(modname));
92
93         /* strip trailing .o */
94         if ((s = strrchr(p, '.')) != NULL)
95                 if (strcmp(s, ".o") == 0)
96                         *s = '\0';
97
98         /* add to list */
99         mod->name = p;
100         mod->next = modules;
101         modules = mod;
102
103         return mod;
104 }
105
106 /* A hash of all exported symbols,
107  * struct symbol is also used for lists of unresolved symbols */
108
109 #define SYMBOL_HASH_SIZE 1024
110
111 struct symbol {
112         struct symbol *next;
113         struct module *module;
114         unsigned int crc;
115         int crc_valid;
116         unsigned int weak:1;
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         char name[0];
121 };
122
123 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
124
125 /* This is based on the hash agorithm from gdbm, via tdb */
126 static inline unsigned int tdb_hash(const char *name)
127 {
128         unsigned value; /* Used to compute the hash value.  */
129         unsigned   i;   /* Used to cycle through random values. */
130
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)));
134
135         return (1103515243 * value + 12345);
136 }
137
138 /**
139  * Allocate a new symbols for use in the hash of exported symbols or
140  * the list of unresolved symbols per module
141  **/
142 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
143                                    struct symbol *next)
144 {
145         struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
146
147         memset(s, 0, sizeof(*s));
148         strcpy(s->name, name);
149         s->weak = weak;
150         s->next = next;
151         return s;
152 }
153
154 /* For the hash of exported symbols */
155 static struct symbol *new_symbol(const char *name, struct module *module)
156 {
157         unsigned int hash;
158         struct symbol *new;
159
160         hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
161         new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
162         new->module = module;
163         return new;
164 }
165
166 static struct symbol *find_symbol(const char *name)
167 {
168         struct symbol *s;
169
170         /* For our purposes, .foo matches foo.  PPC64 needs this. */
171         if (name[0] == '.')
172                 name++;
173
174         for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
175                 if (strcmp(s->name, name) == 0)
176                         return s;
177         }
178         return NULL;
179 }
180
181 /**
182  * Add an exported symbol - it may have already been added without a
183  * CRC, in this case just update the CRC
184  **/
185 static struct symbol *sym_add_exported(const char *name, struct module *mod)
186 {
187         struct symbol *s = find_symbol(name);
188
189         if (!s)
190                 s = new_symbol(name, mod);
191
192         s->vmlinux   = is_vmlinux(mod->name);
193         s->kernel    = 0;
194         return s;
195 }
196
197 static void sym_update_crc(const char *name, struct module *mod,
198                            unsigned int crc)
199 {
200         struct symbol *s = find_symbol(name);
201
202         if (!s)
203                 s = new_symbol(name, mod);
204         s->crc = crc;
205         s->crc_valid = 1;
206 }
207
208 void *grab_file(const char *filename, unsigned long *size)
209 {
210         struct stat st;
211         void *map;
212         int fd;
213
214         fd = open(filename, O_RDONLY);
215         if (fd < 0 || fstat(fd, &st) != 0)
216                 return NULL;
217
218         *size = st.st_size;
219         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
220         close(fd);
221
222         if (map == MAP_FAILED)
223                 return NULL;
224         return map;
225 }
226
227 /**
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.
231   **/
232 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
233 {
234         static char line[4096];
235         int skip = 1;
236         size_t len = 0;
237         signed char *p = (signed char *)file + *pos;
238         char *s = line;
239
240         for (; *pos < size ; (*pos)++)
241         {
242                 if (skip && isspace(*p)) {
243                         p++;
244                         continue;
245                 }
246                 skip = 0;
247                 if (*p != '\n' && (*pos < size)) {
248                         len++;
249                         *s++ = *p++;
250                         if (len > 4095)
251                                 break; /* Too long, stop */
252                 } else {
253                         /* End of string */
254                         *s = '\0';
255                         return line;
256                 }
257         }
258         /* End of buffer */
259         return NULL;
260 }
261
262 void release_file(void *file, unsigned long size)
263 {
264         munmap(file, size);
265 }
266
267 static void parse_elf(struct elf_info *info, const char *filename)
268 {
269         unsigned int i;
270         Elf_Ehdr *hdr = info->hdr;
271         Elf_Shdr *sechdrs;
272         Elf_Sym  *sym;
273
274         hdr = grab_file(filename, &info->size);
275         if (!hdr) {
276                 perror(filename);
277                 abort();
278         }
279         info->hdr = hdr;
280         if (info->size < sizeof(*hdr))
281                 goto truncated;
282
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;
290
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);
298         }
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;
303
304                 if (sechdrs[i].sh_offset > info->size)
305                         goto truncated;
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;
309                 }
310                 if (sechdrs[i].sh_type != SHT_SYMTAB)
311                         continue;
312
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;
318         }
319         if (!info->symtab_start) {
320                 fatal("%s has no symtab?\n", filename);
321         }
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);
328         }
329         return;
330
331  truncated:
332         fatal("%s is truncated.\n", filename);
333 }
334
335 static void parse_elf_finish(struct elf_info *info)
336 {
337         release_file(info->hdr, info->size);
338 }
339
340 #define CRC_PFX     "__crc_"
341 #define KSYMTAB_PFX "__ksymtab_"
342
343 static void handle_modversions(struct module *mod, struct elf_info *info,
344                                Elf_Sym *sym, const char *symname)
345 {
346         unsigned int crc;
347
348         switch (sym->st_shndx) {
349         case SHN_COMMON:
350                 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
351                 break;
352         case SHN_ABS:
353                 /* CRC'd symbol */
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);
357                 }
358                 break;
359         case SHN_UNDEF:
360                 /* undefined symbol */
361                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
362                     ELF_ST_BIND(sym->st_info) != STB_WEAK)
363                         break;
364                 /* ignore global offset table */
365                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
366                         break;
367                 /* ignore __this_module, it will be resolved shortly */
368                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
369                         break;
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
375 #endif
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)
380                                 break;
381                         if (symname[0] == '.') {
382                                 char *munged = strdup(symname);
383                                 munged[0] = '_';
384                                 munged[1] = toupper(munged[1]);
385                                 symname = munged;
386                         }
387                 }
388 #endif
389                 
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,
395                                                   mod->unres);
396                 break;
397         default:
398                 /* All exported symbols */
399                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
400                         sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
401                 }
402                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
403                         mod->has_init = 1;
404                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
405                         mod->has_cleanup = 1;
406                 break;
407         }
408 }
409
410 /**
411  * Parse tag=value strings from .modinfo section
412  **/
413 static char *next_string(char *string, unsigned long *secsize)
414 {
415         /* Skip non-zero chars */
416         while (string[0]) {
417                 string++;
418                 if ((*secsize)-- <= 1)
419                         return NULL;
420         }
421
422         /* Skip any zero padding. */
423         while (!string[0]) {
424                 string++;
425                 if ((*secsize)-- <= 1)
426                         return NULL;
427         }
428         return string;
429 }
430
431 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
432                          const char *tag)
433 {
434         char *p;
435         unsigned int taglen = strlen(tag);
436         unsigned long size = modinfo_len;
437
438         for (p = modinfo; p; p = next_string(p, &size)) {
439                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
440                         return p + taglen + 1;
441         }
442         return NULL;
443 }
444
445 static void read_symbols(char *modname)
446 {
447         const char *symname;
448         char *version;
449         struct module *mod;
450         struct elf_info info = { };
451         Elf_Sym *sym;
452
453         parse_elf(&info, modname);
454
455         mod = new_module(modname);
456
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)) {
460                 have_vmlinux = 1;
461                 mod->skip = 1;
462         }
463
464         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
465                 symname = info.strtab + sym->st_name;
466
467                 handle_modversions(mod, &info, sym, symname);
468                 handle_moddevtable(mod, &info, sym, symname);
469         }
470
471         version = get_modinfo(info.modinfo, info.modinfo_len, "version");
472         if (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);
478
479         parse_elf_finish(&info);
480
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 */
485         if (modversions)
486                 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
487 }
488
489 #define SZ 500
490
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 */
494
495 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
496                                                       const char *fmt, ...)
497 {
498         char tmp[SZ];
499         int len;
500         va_list ap;
501         
502         va_start(ap, fmt);
503         len = vsnprintf(tmp, SZ, fmt, ap);
504         if (buf->size - buf->pos < len + 1) {
505                 buf->size += 128;
506                 buf->p = realloc(buf->p, buf->size);
507         }
508         strncpy(buf->p + buf->pos, tmp, len + 1);
509         buf->pos += len;
510         va_end(ap);
511 }
512
513 void buf_write(struct buffer *buf, const char *s, int len)
514 {
515         if (buf->size - buf->pos < len) {
516                 buf->size += len;
517                 buf->p = realloc(buf->p, buf->size);
518         }
519         strncpy(buf->p + buf->pos, s, len);
520         buf->pos += len;
521 }
522
523 /**
524  * Header for the generated file
525  **/
526 static void add_header(struct buffer *b, struct module *mod)
527 {
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");
531         buf_printf(b, "\n");
532         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
533         buf_printf(b, "\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");
537         if (mod->has_init)
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"
542                               "#endif\n");
543         buf_printf(b, "};\n");
544 }
545
546 /**
547  * Record CRCs for unresolved symbols
548  **/
549 static void add_versions(struct buffer *b, struct module *mod)
550 {
551         struct symbol *s, *exp;
552
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",
558                                      s->name, mod->name);
559                         continue;
560                 }
561                 s->module = exp->module;
562                 s->crc_valid = exp->crc_valid;
563                 s->crc = exp->crc;
564         }
565
566         if (!modversions)
567                 return;
568
569         buf_printf(b, "\n");
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");
573
574         for (s = mod->unres; s; s = s->next) {
575                 if (!s->module) {
576                         continue;
577                 }
578                 if (!s->crc_valid) {
579                         warn("\"%s\" [%s.ko] has no CRC!\n",
580                                 s->name, mod->name);
581                         continue;
582                 }
583                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
584         }
585
586         buf_printf(b, "};\n");
587 }
588
589 static void add_depends(struct buffer *b, struct module *mod,
590                         struct module *modules)
591 {
592         struct symbol *s;
593         struct module *m;
594         int first = 1;
595
596         for (m = modules; m; m = m->next) {
597                 m->seen = is_vmlinux(m->name);
598         }
599
600         buf_printf(b, "\n");
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) {
606                 if (!s->module)
607                         continue;
608
609                 if (s->module->seen)
610                         continue;
611
612                 s->module->seen = 1;
613                 buf_printf(b, "%s%s", first ? "" : ",",
614                            strrchr(s->module->name, '/') + 1);
615                 first = 0;
616         }
617         buf_printf(b, "\";\n");
618 }
619
620 static void add_srcversion(struct buffer *b, struct module *mod)
621 {
622         if (mod->srcversion[0]) {
623                 buf_printf(b, "\n");
624                 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
625                            mod->srcversion);
626         }
627 }
628
629 static void write_if_changed(struct buffer *b, const char *fname)
630 {
631         char *tmp;
632         FILE *file;
633         struct stat st;
634
635         file = fopen(fname, "r");
636         if (!file)
637                 goto write;
638
639         if (fstat(fileno(file), &st) < 0)
640                 goto close_write;
641
642         if (st.st_size != b->pos)
643                 goto close_write;
644
645         tmp = NOFAIL(malloc(b->pos));
646         if (fread(tmp, 1, b->pos, file) != b->pos)
647                 goto free_write;
648
649         if (memcmp(tmp, b->p, b->pos) != 0)
650                 goto free_write;
651
652         free(tmp);
653         fclose(file);
654         return;
655
656  free_write:
657         free(tmp);
658  close_write:
659         fclose(file);
660  write:
661         file = fopen(fname, "w");
662         if (!file) {
663                 perror(fname);
664                 exit(1);
665         }
666         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
667                 perror(fname);
668                 exit(1);
669         }
670         fclose(file);
671 }
672
673 static void read_dump(const char *fname, unsigned int kernel)
674 {
675         unsigned long size, pos = 0;
676         void *file = grab_file(fname, &size);
677         char *line;
678
679         if (!file)
680                 /* No symbol versions, silently ignore */
681                 return;
682
683         while ((line = get_next_line(&pos, file, size))) {
684                 char *symname, *modname, *d;
685                 unsigned int crc;
686                 struct module *mod;
687                 struct symbol *s;
688
689                 if (!(symname = strchr(line, '\t')))
690                         goto fail;
691                 *symname++ = '\0';
692                 if (!(modname = strchr(symname, '\t')))
693                         goto fail;
694                 *modname++ = '\0';
695                 if (strchr(modname, '\t'))
696                         goto fail;
697                 crc = strtoul(line, &d, 16);
698                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
699                         goto fail;
700
701                 if (!(mod = find_module(modname))) {
702                         if (is_vmlinux(modname)) {
703                                 have_vmlinux = 1;
704                         }
705                         mod = new_module(NOFAIL(strdup(modname)));
706                         mod->skip = 1;
707                 }
708                 s = sym_add_exported(symname, mod);
709                 s->kernel = kernel;
710                 sym_update_crc(symname, mod, crc);
711         }
712         return;
713 fail:
714         fatal("parse error in symbol dump file\n");
715 }
716
717 /* For normal builds always dump all symbols.
718  * For external modules only dump symbols
719  * that are not read from kernel Module.symvers.
720  **/
721 static int dump_sym(struct symbol *sym)
722 {
723         if (!external_module)
724                 return 1;
725         if (sym->vmlinux || sym->kernel)
726                 return 0;
727         return 1;
728 }
729                 
730 static void write_dump(const char *fname)
731 {
732         struct buffer buf = { };
733         struct symbol *symbol;
734         int n;
735
736         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
737                 symbol = symbolhash[n];
738                 while (symbol) {
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;
744                 }
745         }
746         write_if_changed(&buf, fname);
747 }
748
749 int main(int argc, char **argv)
750 {
751         struct module *mod;
752         struct buffer buf = { };
753         char fname[SZ];
754         char *kernel_read = NULL, *module_read = NULL;
755         char *dump_write = NULL;
756         int opt;
757
758         while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
759                 switch(opt) {
760                         case 'i':
761                                 kernel_read = optarg;
762                                 break;
763                         case 'I':
764                                 module_read = optarg;
765                                 external_module = 1;
766                                 break;
767                         case 'm':
768                                 modversions = 1;
769                                 break;
770                         case 'o':
771                                 dump_write = optarg;
772                                 break;
773                         case 'a':
774                                 all_versions = 1;
775                                 break;
776                         default:
777                                 exit(1);
778                 }
779         }
780
781         if (kernel_read)
782                 read_dump(kernel_read, 1);
783         if (module_read)
784                 read_dump(module_read, 0);
785
786         while (optind < argc) {
787                 read_symbols(argv[optind++]);
788         }
789
790         for (mod = modules; mod; mod = mod->next) {
791                 if (mod->skip)
792                         continue;
793
794                 buf.pos = 0;
795
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);
801
802                 sprintf(fname, "%s.mod.c", mod->name);
803                 write_if_changed(&buf, fname);
804         }
805
806         if (dump_write)
807                 write_dump(dump_write);
808
809         return 0;
810 }