Merge git://git.infradead.org/~dwmw2/khdrs-2.6
[linux-drm-fsl-dcu.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  * Copyright 2006       Sam Ravnborg
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 #include "../../include/linux/license.h"
17
18 /* Are we using CONFIG_MODVERSIONS? */
19 int modversions = 0;
20 /* Warn about undefined symbols? (do so if we have vmlinux) */
21 int have_vmlinux = 0;
22 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
23 static int all_versions = 0;
24 /* If we are modposting external module set to 1 */
25 static int external_module = 0;
26 /* Only warn about unresolved symbols */
27 static int warn_unresolved = 0;
28 /* How a symbol is exported */
29 enum export {
30         export_plain,      export_unused,     export_gpl,
31         export_unused_gpl, export_gpl_future, export_unknown
32 };
33
34 void fatal(const char *fmt, ...)
35 {
36         va_list arglist;
37
38         fprintf(stderr, "FATAL: ");
39
40         va_start(arglist, fmt);
41         vfprintf(stderr, fmt, arglist);
42         va_end(arglist);
43
44         exit(1);
45 }
46
47 void warn(const char *fmt, ...)
48 {
49         va_list arglist;
50
51         fprintf(stderr, "WARNING: ");
52
53         va_start(arglist, fmt);
54         vfprintf(stderr, fmt, arglist);
55         va_end(arglist);
56 }
57
58 static int is_vmlinux(const char *modname)
59 {
60         const char *myname;
61
62         if ((myname = strrchr(modname, '/')))
63                 myname++;
64         else
65                 myname = modname;
66
67         return strcmp(myname, "vmlinux") == 0;
68 }
69
70 void *do_nofail(void *ptr, const char *expr)
71 {
72         if (!ptr) {
73                 fatal("modpost: Memory allocation failure: %s.\n", expr);
74         }
75         return ptr;
76 }
77
78 /* A list of all modules we processed */
79
80 static struct module *modules;
81
82 static struct module *find_module(char *modname)
83 {
84         struct module *mod;
85
86         for (mod = modules; mod; mod = mod->next)
87                 if (strcmp(mod->name, modname) == 0)
88                         break;
89         return mod;
90 }
91
92 static struct module *new_module(char *modname)
93 {
94         struct module *mod;
95         char *p, *s;
96
97         mod = NOFAIL(malloc(sizeof(*mod)));
98         memset(mod, 0, sizeof(*mod));
99         p = NOFAIL(strdup(modname));
100
101         /* strip trailing .o */
102         if ((s = strrchr(p, '.')) != NULL)
103                 if (strcmp(s, ".o") == 0)
104                         *s = '\0';
105
106         /* add to list */
107         mod->name = p;
108         mod->gpl_compatible = -1;
109         mod->next = modules;
110         modules = mod;
111
112         return mod;
113 }
114
115 /* A hash of all exported symbols,
116  * struct symbol is also used for lists of unresolved symbols */
117
118 #define SYMBOL_HASH_SIZE 1024
119
120 struct symbol {
121         struct symbol *next;
122         struct module *module;
123         unsigned int crc;
124         int crc_valid;
125         unsigned int weak:1;
126         unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
127         unsigned int kernel:1;     /* 1 if symbol is from kernel
128                                     *  (only for external modules) **/
129         unsigned int preloaded:1;  /* 1 if symbol from Module.symvers */
130         enum export  export;       /* Type of export */
131         char name[0];
132 };
133
134 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
135
136 /* This is based on the hash agorithm from gdbm, via tdb */
137 static inline unsigned int tdb_hash(const char *name)
138 {
139         unsigned value; /* Used to compute the hash value.  */
140         unsigned   i;   /* Used to cycle through random values. */
141
142         /* Set the initial value from the key size. */
143         for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
144                 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
145
146         return (1103515243 * value + 12345);
147 }
148
149 /**
150  * Allocate a new symbols for use in the hash of exported symbols or
151  * the list of unresolved symbols per module
152  **/
153 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
154                                    struct symbol *next)
155 {
156         struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
157
158         memset(s, 0, sizeof(*s));
159         strcpy(s->name, name);
160         s->weak = weak;
161         s->next = next;
162         return s;
163 }
164
165 /* For the hash of exported symbols */
166 static struct symbol *new_symbol(const char *name, struct module *module,
167                                  enum export export)
168 {
169         unsigned int hash;
170         struct symbol *new;
171
172         hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
173         new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
174         new->module = module;
175         new->export = export;
176         return new;
177 }
178
179 static struct symbol *find_symbol(const char *name)
180 {
181         struct symbol *s;
182
183         /* For our purposes, .foo matches foo.  PPC64 needs this. */
184         if (name[0] == '.')
185                 name++;
186
187         for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
188                 if (strcmp(s->name, name) == 0)
189                         return s;
190         }
191         return NULL;
192 }
193
194 static struct {
195         const char *str;
196         enum export export;
197 } export_list[] = {
198         { .str = "EXPORT_SYMBOL",            .export = export_plain },
199         { .str = "EXPORT_UNUSED_SYMBOL",     .export = export_unused },
200         { .str = "EXPORT_SYMBOL_GPL",        .export = export_gpl },
201         { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
202         { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
203         { .str = "(unknown)",                .export = export_unknown },
204 };
205
206
207 static const char *export_str(enum export ex)
208 {
209         return export_list[ex].str;
210 }
211
212 static enum export export_no(const char * s)
213 {
214         int i;
215         if (!s)
216                 return export_unknown;
217         for (i = 0; export_list[i].export != export_unknown; i++) {
218                 if (strcmp(export_list[i].str, s) == 0)
219                         return export_list[i].export;
220         }
221         return export_unknown;
222 }
223
224 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
225 {
226         if (sec == elf->export_sec)
227                 return export_plain;
228         else if (sec == elf->export_unused_sec)
229                 return export_unused;
230         else if (sec == elf->export_gpl_sec)
231                 return export_gpl;
232         else if (sec == elf->export_unused_gpl_sec)
233                 return export_unused_gpl;
234         else if (sec == elf->export_gpl_future_sec)
235                 return export_gpl_future;
236         else
237                 return export_unknown;
238 }
239
240 /**
241  * Add an exported symbol - it may have already been added without a
242  * CRC, in this case just update the CRC
243  **/
244 static struct symbol *sym_add_exported(const char *name, struct module *mod,
245                                        enum export export)
246 {
247         struct symbol *s = find_symbol(name);
248
249         if (!s) {
250                 s = new_symbol(name, mod, export);
251         } else {
252                 if (!s->preloaded) {
253                         warn("%s: '%s' exported twice. Previous export "
254                              "was in %s%s\n", mod->name, name,
255                              s->module->name,
256                              is_vmlinux(s->module->name) ?"":".ko");
257                 }
258         }
259         s->preloaded = 0;
260         s->vmlinux   = is_vmlinux(mod->name);
261         s->kernel    = 0;
262         s->export    = export;
263         return s;
264 }
265
266 static void sym_update_crc(const char *name, struct module *mod,
267                            unsigned int crc, enum export export)
268 {
269         struct symbol *s = find_symbol(name);
270
271         if (!s)
272                 s = new_symbol(name, mod, export);
273         s->crc = crc;
274         s->crc_valid = 1;
275 }
276
277 void *grab_file(const char *filename, unsigned long *size)
278 {
279         struct stat st;
280         void *map;
281         int fd;
282
283         fd = open(filename, O_RDONLY);
284         if (fd < 0 || fstat(fd, &st) != 0)
285                 return NULL;
286
287         *size = st.st_size;
288         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
289         close(fd);
290
291         if (map == MAP_FAILED)
292                 return NULL;
293         return map;
294 }
295
296 /**
297   * Return a copy of the next line in a mmap'ed file.
298   * spaces in the beginning of the line is trimmed away.
299   * Return a pointer to a static buffer.
300   **/
301 char* get_next_line(unsigned long *pos, void *file, unsigned long size)
302 {
303         static char line[4096];
304         int skip = 1;
305         size_t len = 0;
306         signed char *p = (signed char *)file + *pos;
307         char *s = line;
308
309         for (; *pos < size ; (*pos)++)
310         {
311                 if (skip && isspace(*p)) {
312                         p++;
313                         continue;
314                 }
315                 skip = 0;
316                 if (*p != '\n' && (*pos < size)) {
317                         len++;
318                         *s++ = *p++;
319                         if (len > 4095)
320                                 break; /* Too long, stop */
321                 } else {
322                         /* End of string */
323                         *s = '\0';
324                         return line;
325                 }
326         }
327         /* End of buffer */
328         return NULL;
329 }
330
331 void release_file(void *file, unsigned long size)
332 {
333         munmap(file, size);
334 }
335
336 static void parse_elf(struct elf_info *info, const char *filename)
337 {
338         unsigned int i;
339         Elf_Ehdr *hdr = info->hdr;
340         Elf_Shdr *sechdrs;
341         Elf_Sym  *sym;
342
343         hdr = grab_file(filename, &info->size);
344         if (!hdr) {
345                 perror(filename);
346                 exit(1);
347         }
348         info->hdr = hdr;
349         if (info->size < sizeof(*hdr))
350                 goto truncated;
351
352         /* Fix endianness in ELF header */
353         hdr->e_shoff    = TO_NATIVE(hdr->e_shoff);
354         hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
355         hdr->e_shnum    = TO_NATIVE(hdr->e_shnum);
356         hdr->e_machine  = TO_NATIVE(hdr->e_machine);
357         sechdrs = (void *)hdr + hdr->e_shoff;
358         info->sechdrs = sechdrs;
359
360         /* Fix endianness in section headers */
361         for (i = 0; i < hdr->e_shnum; i++) {
362                 sechdrs[i].sh_type   = TO_NATIVE(sechdrs[i].sh_type);
363                 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
364                 sechdrs[i].sh_size   = TO_NATIVE(sechdrs[i].sh_size);
365                 sechdrs[i].sh_link   = TO_NATIVE(sechdrs[i].sh_link);
366                 sechdrs[i].sh_name   = TO_NATIVE(sechdrs[i].sh_name);
367         }
368         /* Find symbol table. */
369         for (i = 1; i < hdr->e_shnum; i++) {
370                 const char *secstrings
371                         = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
372                 const char *secname;
373
374                 if (sechdrs[i].sh_offset > info->size)
375                         goto truncated;
376                 secname = secstrings + sechdrs[i].sh_name;
377                 if (strcmp(secname, ".modinfo") == 0) {
378                         info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
379                         info->modinfo_len = sechdrs[i].sh_size;
380                 } else if (strcmp(secname, "__ksymtab") == 0)
381                         info->export_sec = i;
382                 else if (strcmp(secname, "__ksymtab_unused") == 0)
383                         info->export_unused_sec = i;
384                 else if (strcmp(secname, "__ksymtab_gpl") == 0)
385                         info->export_gpl_sec = i;
386                 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
387                         info->export_unused_gpl_sec = i;
388                 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
389                         info->export_gpl_future_sec = i;
390
391                 if (sechdrs[i].sh_type != SHT_SYMTAB)
392                         continue;
393
394                 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
395                 info->symtab_stop  = (void *)hdr + sechdrs[i].sh_offset
396                                                  + sechdrs[i].sh_size;
397                 info->strtab       = (void *)hdr +
398                                      sechdrs[sechdrs[i].sh_link].sh_offset;
399         }
400         if (!info->symtab_start) {
401                 fatal("%s has no symtab?\n", filename);
402         }
403         /* Fix endianness in symbols */
404         for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
405                 sym->st_shndx = TO_NATIVE(sym->st_shndx);
406                 sym->st_name  = TO_NATIVE(sym->st_name);
407                 sym->st_value = TO_NATIVE(sym->st_value);
408                 sym->st_size  = TO_NATIVE(sym->st_size);
409         }
410         return;
411
412  truncated:
413         fatal("%s is truncated.\n", filename);
414 }
415
416 static void parse_elf_finish(struct elf_info *info)
417 {
418         release_file(info->hdr, info->size);
419 }
420
421 #define CRC_PFX     MODULE_SYMBOL_PREFIX "__crc_"
422 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
423
424 static void handle_modversions(struct module *mod, struct elf_info *info,
425                                Elf_Sym *sym, const char *symname)
426 {
427         unsigned int crc;
428         enum export export = export_from_sec(info, sym->st_shndx);
429
430         switch (sym->st_shndx) {
431         case SHN_COMMON:
432                 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
433                 break;
434         case SHN_ABS:
435                 /* CRC'd symbol */
436                 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
437                         crc = (unsigned int) sym->st_value;
438                         sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
439                                         export);
440                 }
441                 break;
442         case SHN_UNDEF:
443                 /* undefined symbol */
444                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
445                     ELF_ST_BIND(sym->st_info) != STB_WEAK)
446                         break;
447                 /* ignore global offset table */
448                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
449                         break;
450                 /* ignore __this_module, it will be resolved shortly */
451                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
452                         break;
453 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
454 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
455 /* add compatibility with older glibc */
456 #ifndef STT_SPARC_REGISTER
457 #define STT_SPARC_REGISTER STT_REGISTER
458 #endif
459                 if (info->hdr->e_machine == EM_SPARC ||
460                     info->hdr->e_machine == EM_SPARCV9) {
461                         /* Ignore register directives. */
462                         if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
463                                 break;
464                         if (symname[0] == '.') {
465                                 char *munged = strdup(symname);
466                                 munged[0] = '_';
467                                 munged[1] = toupper(munged[1]);
468                                 symname = munged;
469                         }
470                 }
471 #endif
472
473                 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
474                            strlen(MODULE_SYMBOL_PREFIX)) == 0)
475                         mod->unres = alloc_symbol(symname +
476                                                   strlen(MODULE_SYMBOL_PREFIX),
477                                                   ELF_ST_BIND(sym->st_info) == STB_WEAK,
478                                                   mod->unres);
479                 break;
480         default:
481                 /* All exported symbols */
482                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
483                         sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
484                                         export);
485                 }
486                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
487                         mod->has_init = 1;
488                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
489                         mod->has_cleanup = 1;
490                 break;
491         }
492 }
493
494 /**
495  * Parse tag=value strings from .modinfo section
496  **/
497 static char *next_string(char *string, unsigned long *secsize)
498 {
499         /* Skip non-zero chars */
500         while (string[0]) {
501                 string++;
502                 if ((*secsize)-- <= 1)
503                         return NULL;
504         }
505
506         /* Skip any zero padding. */
507         while (!string[0]) {
508                 string++;
509                 if ((*secsize)-- <= 1)
510                         return NULL;
511         }
512         return string;
513 }
514
515 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
516                               const char *tag, char *info)
517 {
518         char *p;
519         unsigned int taglen = strlen(tag);
520         unsigned long size = modinfo_len;
521
522         if (info) {
523                 size -= info - (char *)modinfo;
524                 modinfo = next_string(info, &size);
525         }
526
527         for (p = modinfo; p; p = next_string(p, &size)) {
528                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
529                         return p + taglen + 1;
530         }
531         return NULL;
532 }
533
534 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
535                          const char *tag)
536
537 {
538         return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
539 }
540
541 /**
542  * Test if string s ends in string sub
543  * return 0 if match
544  **/
545 static int strrcmp(const char *s, const char *sub)
546 {
547         int slen, sublen;
548
549         if (!s || !sub)
550                 return 1;
551
552         slen = strlen(s);
553         sublen = strlen(sub);
554
555         if ((slen == 0) || (sublen == 0))
556                 return 1;
557
558         if (sublen > slen)
559                 return 1;
560
561         return memcmp(s + slen - sublen, sub, sublen);
562 }
563
564 /**
565  * Whitelist to allow certain references to pass with no warning.
566  * Pattern 1:
567  *   If a module parameter is declared __initdata and permissions=0
568  *   then this is legal despite the warning generated.
569  *   We cannot see value of permissions here, so just ignore
570  *   this pattern.
571  *   The pattern is identified by:
572  *   tosec   = .init.data
573  *   fromsec = .data*
574  *   atsym   =__param*
575  *
576  * Pattern 2:
577  *   Many drivers utilise a *driver container with references to
578  *   add, remove, probe functions etc.
579  *   These functions may often be marked __init and we do not want to
580  *   warn here.
581  *   the pattern is identified by:
582  *   tosec   = .init.text | .exit.text | .init.data
583  *   fromsec = .data
584  *   atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
585  **/
586 static int secref_whitelist(const char *modname, const char *tosec,
587                             const char *fromsec, const char *atsym)
588 {
589         int f1 = 1, f2 = 1;
590         const char **s;
591         const char *pat2sym[] = {
592                 "driver",
593                 "_template", /* scsi uses *_template a lot */
594                 "_sht",      /* scsi also used *_sht to some extent */
595                 "_ops",
596                 "_probe",
597                 "_probe_one",
598                 NULL
599         };
600
601         /* Check for pattern 1 */
602         if (strcmp(tosec, ".init.data") != 0)
603                 f1 = 0;
604         if (strncmp(fromsec, ".data", strlen(".data")) != 0)
605                 f1 = 0;
606         if (strncmp(atsym, "__param", strlen("__param")) != 0)
607                 f1 = 0;
608
609         if (f1)
610                 return f1;
611
612         /* Check for pattern 2 */
613         if ((strcmp(tosec, ".init.text") != 0) &&
614             (strcmp(tosec, ".exit.text") != 0) &&
615             (strcmp(tosec, ".init.data") != 0))
616                 f2 = 0;
617         if (strcmp(fromsec, ".data") != 0)
618                 f2 = 0;
619
620         for (s = pat2sym; *s; s++)
621                 if (strrcmp(atsym, *s) == 0)
622                         f1 = 1;
623         if (f1 && f2)
624                 return 1;
625
626         /* Whitelist all references from .pci_fixup section if vmlinux */
627         if (is_vmlinux(modname)) {
628                 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
629                     (strcmp(tosec, ".init.text") == 0))
630                 return 1;
631         }
632         return 0;
633 }
634
635 /**
636  * Find symbol based on relocation record info.
637  * In some cases the symbol supplied is a valid symbol so
638  * return refsym. If st_name != 0 we assume this is a valid symbol.
639  * In other cases the symbol needs to be looked up in the symbol table
640  * based on section and address.
641  *  **/
642 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
643                                 Elf_Sym *relsym)
644 {
645         Elf_Sym *sym;
646
647         if (relsym->st_name != 0)
648                 return relsym;
649         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
650                 if (sym->st_shndx != relsym->st_shndx)
651                         continue;
652                 if (sym->st_value == addr)
653                         return sym;
654         }
655         return NULL;
656 }
657
658 /*
659  * Find symbols before or equal addr and after addr - in the section sec.
660  * If we find two symbols with equal offset prefer one with a valid name.
661  * The ELF format may have a better way to detect what type of symbol
662  * it is, but this works for now.
663  **/
664 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
665                                  const char *sec,
666                                  Elf_Sym **before, Elf_Sym **after)
667 {
668         Elf_Sym *sym;
669         Elf_Ehdr *hdr = elf->hdr;
670         Elf_Addr beforediff = ~0;
671         Elf_Addr afterdiff = ~0;
672         const char *secstrings = (void *)hdr +
673                                  elf->sechdrs[hdr->e_shstrndx].sh_offset;
674
675         *before = NULL;
676         *after = NULL;
677
678         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
679                 const char *symsec;
680
681                 if (sym->st_shndx >= SHN_LORESERVE)
682                         continue;
683                 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
684                 if (strcmp(symsec, sec) != 0)
685                         continue;
686                 if (sym->st_value <= addr) {
687                         if ((addr - sym->st_value) < beforediff) {
688                                 beforediff = addr - sym->st_value;
689                                 *before = sym;
690                         }
691                         else if ((addr - sym->st_value) == beforediff) {
692                                 /* equal offset, valid name? */
693                                 const char *name = elf->strtab + sym->st_name;
694                                 if (name && strlen(name))
695                                         *before = sym;
696                         }
697                 }
698                 else
699                 {
700                         if ((sym->st_value - addr) < afterdiff) {
701                                 afterdiff = sym->st_value - addr;
702                                 *after = sym;
703                         }
704                         else if ((sym->st_value - addr) == afterdiff) {
705                                 /* equal offset, valid name? */
706                                 const char *name = elf->strtab + sym->st_name;
707                                 if (name && strlen(name))
708                                         *after = sym;
709                         }
710                 }
711         }
712 }
713
714 /**
715  * Print a warning about a section mismatch.
716  * Try to find symbols near it so user can find it.
717  * Check whitelist before warning - it may be a false positive.
718  **/
719 static void warn_sec_mismatch(const char *modname, const char *fromsec,
720                               struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
721 {
722         const char *refsymname = "";
723         Elf_Sym *before, *after;
724         Elf_Sym *refsym;
725         Elf_Ehdr *hdr = elf->hdr;
726         Elf_Shdr *sechdrs = elf->sechdrs;
727         const char *secstrings = (void *)hdr +
728                                  sechdrs[hdr->e_shstrndx].sh_offset;
729         const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
730
731         find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
732
733         refsym = find_elf_symbol(elf, r.r_addend, sym);
734         if (refsym && strlen(elf->strtab + refsym->st_name))
735                 refsymname = elf->strtab + refsym->st_name;
736
737         /* check whitelist - we may ignore it */
738         if (before &&
739             secref_whitelist(modname, secname, fromsec,
740                              elf->strtab + before->st_name))
741                 return;
742
743         if (before && after) {
744                 warn("%s - Section mismatch: reference to %s:%s from %s "
745                      "between '%s' (at offset 0x%llx) and '%s'\n",
746                      modname, secname, refsymname, fromsec,
747                      elf->strtab + before->st_name,
748                      (long long)r.r_offset,
749                      elf->strtab + after->st_name);
750         } else if (before) {
751                 warn("%s - Section mismatch: reference to %s:%s from %s "
752                      "after '%s' (at offset 0x%llx)\n",
753                      modname, secname, refsymname, fromsec,
754                      elf->strtab + before->st_name,
755                      (long long)r.r_offset);
756         } else if (after) {
757                 warn("%s - Section mismatch: reference to %s:%s from %s "
758                      "before '%s' (at offset -0x%llx)\n",
759                      modname, secname, refsymname, fromsec,
760                      elf->strtab + after->st_name,
761                      (long long)r.r_offset);
762         } else {
763                 warn("%s - Section mismatch: reference to %s:%s from %s "
764                      "(offset 0x%llx)\n",
765                      modname, secname, fromsec, refsymname,
766                      (long long)r.r_offset);
767         }
768 }
769
770 /**
771  * A module includes a number of sections that are discarded
772  * either when loaded or when used as built-in.
773  * For loaded modules all functions marked __init and all data
774  * marked __initdata will be discarded when the module has been intialized.
775  * Likewise for modules used built-in the sections marked __exit
776  * are discarded because __exit marked function are supposed to be called
777  * only when a moduel is unloaded which never happes for built-in modules.
778  * The check_sec_ref() function traverses all relocation records
779  * to find all references to a section that reference a section that will
780  * be discarded and warns about it.
781  **/
782 static void check_sec_ref(struct module *mod, const char *modname,
783                           struct elf_info *elf,
784                           int section(const char*),
785                           int section_ref_ok(const char *))
786 {
787         int i;
788         Elf_Sym  *sym;
789         Elf_Ehdr *hdr = elf->hdr;
790         Elf_Shdr *sechdrs = elf->sechdrs;
791         const char *secstrings = (void *)hdr +
792                                  sechdrs[hdr->e_shstrndx].sh_offset;
793
794         /* Walk through all sections */
795         for (i = 0; i < hdr->e_shnum; i++) {
796                 const char *name = secstrings + sechdrs[i].sh_name;
797                 const char *secname;
798                 Elf_Rela r;
799                 unsigned int r_sym;
800                 /* We want to process only relocation sections and not .init */
801                 if (sechdrs[i].sh_type == SHT_RELA) {
802                         Elf_Rela *rela;
803                         Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
804                         Elf_Rela *stop  = (void*)start + sechdrs[i].sh_size;
805                         name += strlen(".rela");
806                         if (section_ref_ok(name))
807                                 continue;
808
809                         for (rela = start; rela < stop; rela++) {
810                                 r.r_offset = TO_NATIVE(rela->r_offset);
811 #if KERNEL_ELFCLASS == ELFCLASS64
812                                 if (hdr->e_machine == EM_MIPS) {
813                                         r_sym = ELF64_MIPS_R_SYM(rela->r_info);
814                                         r_sym = TO_NATIVE(r_sym);
815                                 } else {
816                                         r.r_info = TO_NATIVE(rela->r_info);
817                                         r_sym = ELF_R_SYM(r.r_info);
818                                 }
819 #else
820                                 r.r_info = TO_NATIVE(rela->r_info);
821                                 r_sym = ELF_R_SYM(r.r_info);
822 #endif
823                                 r.r_addend = TO_NATIVE(rela->r_addend);
824                                 sym = elf->symtab_start + r_sym;
825                                 /* Skip special sections */
826                                 if (sym->st_shndx >= SHN_LORESERVE)
827                                         continue;
828
829                                 secname = secstrings +
830                                         sechdrs[sym->st_shndx].sh_name;
831                                 if (section(secname))
832                                         warn_sec_mismatch(modname, name,
833                                                           elf, sym, r);
834                         }
835                 } else if (sechdrs[i].sh_type == SHT_REL) {
836                         Elf_Rel *rel;
837                         Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
838                         Elf_Rel *stop  = (void*)start + sechdrs[i].sh_size;
839                         name += strlen(".rel");
840                         if (section_ref_ok(name))
841                                 continue;
842
843                         for (rel = start; rel < stop; rel++) {
844                                 r.r_offset = TO_NATIVE(rel->r_offset);
845 #if KERNEL_ELFCLASS == ELFCLASS64
846                                 if (hdr->e_machine == EM_MIPS) {
847                                         r_sym = ELF64_MIPS_R_SYM(rel->r_info);
848                                         r_sym = TO_NATIVE(r_sym);
849                                 } else {
850                                         r.r_info = TO_NATIVE(rel->r_info);
851                                         r_sym = ELF_R_SYM(r.r_info);
852                                 }
853 #else
854                                 r.r_info = TO_NATIVE(rel->r_info);
855                                 r_sym = ELF_R_SYM(r.r_info);
856 #endif
857                                 r.r_addend = 0;
858                                 sym = elf->symtab_start + r_sym;
859                                 /* Skip special sections */
860                                 if (sym->st_shndx >= SHN_LORESERVE)
861                                         continue;
862
863                                 secname = secstrings +
864                                         sechdrs[sym->st_shndx].sh_name;
865                                 if (section(secname))
866                                         warn_sec_mismatch(modname, name,
867                                                           elf, sym, r);
868                         }
869                 }
870         }
871 }
872
873 /**
874  * Functions used only during module init is marked __init and is stored in
875  * a .init.text section. Likewise data is marked __initdata and stored in
876  * a .init.data section.
877  * If this section is one of these sections return 1
878  * See include/linux/init.h for the details
879  **/
880 static int init_section(const char *name)
881 {
882         if (strcmp(name, ".init") == 0)
883                 return 1;
884         if (strncmp(name, ".init.", strlen(".init.")) == 0)
885                 return 1;
886         return 0;
887 }
888
889 /**
890  * Identify sections from which references to a .init section is OK.
891  *
892  * Unfortunately references to read only data that referenced .init
893  * sections had to be excluded. Almost all of these are false
894  * positives, they are created by gcc. The downside of excluding rodata
895  * is that there really are some user references from rodata to
896  * init code, e.g. drivers/video/vgacon.c:
897  *
898  * const struct consw vga_con = {
899  *        con_startup:            vgacon_startup,
900  *
901  * where vgacon_startup is __init.  If you want to wade through the false
902  * positives, take out the check for rodata.
903  **/
904 static int init_section_ref_ok(const char *name)
905 {
906         const char **s;
907         /* Absolute section names */
908         const char *namelist1[] = {
909                 ".init",
910                 ".opd",   /* see comment [OPD] at exit_section_ref_ok() */
911                 ".toc1",  /* used by ppc64 */
912                 ".stab",
913                 ".rodata",
914                 ".text.lock",
915                 "__bug_table", /* used by powerpc for BUG() */
916                 ".pci_fixup_header",
917                 ".pci_fixup_final",
918                 ".pdr",
919                 "__param",
920                 "__ex_table",
921                 ".fixup",
922                 ".smp_locks",
923                 ".plt",  /* seen on ARCH=um build on x86_64. Harmless */
924                 NULL
925         };
926         /* Start of section names */
927         const char *namelist2[] = {
928                 ".init.",
929                 ".altinstructions",
930                 ".eh_frame",
931                 ".debug",
932                 NULL
933         };
934         /* part of section name */
935         const char *namelist3 [] = {
936                 ".unwind",  /* sample: IA_64.unwind.init.text */
937                 NULL
938         };
939
940         for (s = namelist1; *s; s++)
941                 if (strcmp(*s, name) == 0)
942                         return 1;
943         for (s = namelist2; *s; s++)
944                 if (strncmp(*s, name, strlen(*s)) == 0)
945                         return 1;
946         for (s = namelist3; *s; s++)
947                 if (strstr(name, *s) != NULL)
948                         return 1;
949         if (strrcmp(name, ".init") == 0)
950                 return 1;
951         return 0;
952 }
953
954 /*
955  * Functions used only during module exit is marked __exit and is stored in
956  * a .exit.text section. Likewise data is marked __exitdata and stored in
957  * a .exit.data section.
958  * If this section is one of these sections return 1
959  * See include/linux/init.h for the details
960  **/
961 static int exit_section(const char *name)
962 {
963         if (strcmp(name, ".exit.text") == 0)
964                 return 1;
965         if (strcmp(name, ".exit.data") == 0)
966                 return 1;
967         return 0;
968
969 }
970
971 /*
972  * Identify sections from which references to a .exit section is OK.
973  *
974  * [OPD] Keith Ownes <kaos@sgi.com> commented:
975  * For our future {in}sanity, add a comment that this is the ppc .opd
976  * section, not the ia64 .opd section.
977  * ia64 .opd should not point to discarded sections.
978  * [.rodata] like for .init.text we ignore .rodata references -same reason
979  **/
980 static int exit_section_ref_ok(const char *name)
981 {
982         const char **s;
983         /* Absolute section names */
984         const char *namelist1[] = {
985                 ".exit.text",
986                 ".exit.data",
987                 ".init.text",
988                 ".rodata",
989                 ".opd", /* See comment [OPD] */
990                 ".toc1",  /* used by ppc64 */
991                 ".altinstructions",
992                 ".pdr",
993                 "__bug_table", /* used by powerpc for BUG() */
994                 ".exitcall.exit",
995                 ".eh_frame",
996                 ".stab",
997                 "__ex_table",
998                 ".fixup",
999                 ".smp_locks",
1000                 ".plt",  /* seen on ARCH=um build on x86_64. Harmless */
1001                 NULL
1002         };
1003         /* Start of section names */
1004         const char *namelist2[] = {
1005                 ".debug",
1006                 NULL
1007         };
1008         /* part of section name */
1009         const char *namelist3 [] = {
1010                 ".unwind",  /* Sample: IA_64.unwind.exit.text */
1011                 NULL
1012         };
1013
1014         for (s = namelist1; *s; s++)
1015                 if (strcmp(*s, name) == 0)
1016                         return 1;
1017         for (s = namelist2; *s; s++)
1018                 if (strncmp(*s, name, strlen(*s)) == 0)
1019                         return 1;
1020         for (s = namelist3; *s; s++)
1021                 if (strstr(name, *s) != NULL)
1022                         return 1;
1023         return 0;
1024 }
1025
1026 static void read_symbols(char *modname)
1027 {
1028         const char *symname;
1029         char *version;
1030         char *license;
1031         struct module *mod;
1032         struct elf_info info = { };
1033         Elf_Sym *sym;
1034
1035         parse_elf(&info, modname);
1036
1037         mod = new_module(modname);
1038
1039         /* When there's no vmlinux, don't print warnings about
1040          * unresolved symbols (since there'll be too many ;) */
1041         if (is_vmlinux(modname)) {
1042                 have_vmlinux = 1;
1043                 mod->skip = 1;
1044         }
1045
1046         license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1047         while (license) {
1048                 if (license_is_gpl_compatible(license))
1049                         mod->gpl_compatible = 1;
1050                 else {
1051                         mod->gpl_compatible = 0;
1052                         break;
1053                 }
1054                 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1055                                            "license", license);
1056         }
1057
1058         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1059                 symname = info.strtab + sym->st_name;
1060
1061                 handle_modversions(mod, &info, sym, symname);
1062                 handle_moddevtable(mod, &info, sym, symname);
1063         }
1064         check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1065         check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1066
1067         version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1068         if (version)
1069                 maybe_frob_rcs_version(modname, version, info.modinfo,
1070                                        version - (char *)info.hdr);
1071         if (version || (all_versions && !is_vmlinux(modname)))
1072                 get_src_version(modname, mod->srcversion,
1073                                 sizeof(mod->srcversion)-1);
1074
1075         parse_elf_finish(&info);
1076
1077         /* Our trick to get versioning for struct_module - it's
1078          * never passed as an argument to an exported function, so
1079          * the automatic versioning doesn't pick it up, but it's really
1080          * important anyhow */
1081         if (modversions)
1082                 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1083 }
1084
1085 #define SZ 500
1086
1087 /* We first write the generated file into memory using the
1088  * following helper, then compare to the file on disk and
1089  * only update the later if anything changed */
1090
1091 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1092                                                       const char *fmt, ...)
1093 {
1094         char tmp[SZ];
1095         int len;
1096         va_list ap;
1097
1098         va_start(ap, fmt);
1099         len = vsnprintf(tmp, SZ, fmt, ap);
1100         buf_write(buf, tmp, len);
1101         va_end(ap);
1102 }
1103
1104 void buf_write(struct buffer *buf, const char *s, int len)
1105 {
1106         if (buf->size - buf->pos < len) {
1107                 buf->size += len + SZ;
1108                 buf->p = realloc(buf->p, buf->size);
1109         }
1110         strncpy(buf->p + buf->pos, s, len);
1111         buf->pos += len;
1112 }
1113
1114 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1115 {
1116         const char *e = is_vmlinux(m) ?"":".ko";
1117
1118         switch (exp) {
1119         case export_gpl:
1120                 fatal("modpost: GPL-incompatible module %s%s "
1121                       "uses GPL-only symbol '%s'\n", m, e, s);
1122                 break;
1123         case export_unused_gpl:
1124                 fatal("modpost: GPL-incompatible module %s%s "
1125                       "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1126                 break;
1127         case export_gpl_future:
1128                 warn("modpost: GPL-incompatible module %s%s "
1129                       "uses future GPL-only symbol '%s'\n", m, e, s);
1130                 break;
1131         case export_plain:
1132         case export_unused:
1133         case export_unknown:
1134                 /* ignore */
1135                 break;
1136         }
1137 }
1138
1139 static void check_for_unused(enum export exp, const char* m, const char* s)
1140 {
1141         const char *e = is_vmlinux(m) ?"":".ko";
1142
1143         switch (exp) {
1144         case export_unused:
1145         case export_unused_gpl:
1146                 warn("modpost: module %s%s "
1147                       "uses symbol '%s' marked UNUSED\n", m, e, s);
1148                 break;
1149         default:
1150                 /* ignore */
1151                 break;
1152         }
1153 }
1154
1155 static void check_exports(struct module *mod)
1156 {
1157         struct symbol *s, *exp;
1158
1159         for (s = mod->unres; s; s = s->next) {
1160                 const char *basename;
1161                 exp = find_symbol(s->name);
1162                 if (!exp || exp->module == mod)
1163                         continue;
1164                 basename = strrchr(mod->name, '/');
1165                 if (basename)
1166                         basename++;
1167                 else
1168                         basename = mod->name;
1169                 if (!mod->gpl_compatible)
1170                         check_for_gpl_usage(exp->export, basename, exp->name);
1171                 check_for_unused(exp->export, basename, exp->name);
1172         }
1173 }
1174
1175 /**
1176  * Header for the generated file
1177  **/
1178 static void add_header(struct buffer *b, struct module *mod)
1179 {
1180         buf_printf(b, "#include <linux/module.h>\n");
1181         buf_printf(b, "#include <linux/vermagic.h>\n");
1182         buf_printf(b, "#include <linux/compiler.h>\n");
1183         buf_printf(b, "\n");
1184         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1185         buf_printf(b, "\n");
1186         buf_printf(b, "struct module __this_module\n");
1187         buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1188         buf_printf(b, " .name = KBUILD_MODNAME,\n");
1189         if (mod->has_init)
1190                 buf_printf(b, " .init = init_module,\n");
1191         if (mod->has_cleanup)
1192                 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1193                               " .exit = cleanup_module,\n"
1194                               "#endif\n");
1195         buf_printf(b, "};\n");
1196 }
1197
1198 /**
1199  * Record CRCs for unresolved symbols
1200  **/
1201 static int add_versions(struct buffer *b, struct module *mod)
1202 {
1203         struct symbol *s, *exp;
1204         int err = 0;
1205
1206         for (s = mod->unres; s; s = s->next) {
1207                 exp = find_symbol(s->name);
1208                 if (!exp || exp->module == mod) {
1209                         if (have_vmlinux && !s->weak) {
1210                                 warn("\"%s\" [%s.ko] undefined!\n",
1211                                      s->name, mod->name);
1212                                 err = warn_unresolved ? 0 : 1;
1213                         }
1214                         continue;
1215                 }
1216                 s->module = exp->module;
1217                 s->crc_valid = exp->crc_valid;
1218                 s->crc = exp->crc;
1219         }
1220
1221         if (!modversions)
1222                 return err;
1223
1224         buf_printf(b, "\n");
1225         buf_printf(b, "static const struct modversion_info ____versions[]\n");
1226         buf_printf(b, "__attribute_used__\n");
1227         buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1228
1229         for (s = mod->unres; s; s = s->next) {
1230                 if (!s->module) {
1231                         continue;
1232                 }
1233                 if (!s->crc_valid) {
1234                         warn("\"%s\" [%s.ko] has no CRC!\n",
1235                                 s->name, mod->name);
1236                         continue;
1237                 }
1238                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1239         }
1240
1241         buf_printf(b, "};\n");
1242
1243         return err;
1244 }
1245
1246 static void add_depends(struct buffer *b, struct module *mod,
1247                         struct module *modules)
1248 {
1249         struct symbol *s;
1250         struct module *m;
1251         int first = 1;
1252
1253         for (m = modules; m; m = m->next) {
1254                 m->seen = is_vmlinux(m->name);
1255         }
1256
1257         buf_printf(b, "\n");
1258         buf_printf(b, "static const char __module_depends[]\n");
1259         buf_printf(b, "__attribute_used__\n");
1260         buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1261         buf_printf(b, "\"depends=");
1262         for (s = mod->unres; s; s = s->next) {
1263                 if (!s->module)
1264                         continue;
1265
1266                 if (s->module->seen)
1267                         continue;
1268
1269                 s->module->seen = 1;
1270                 buf_printf(b, "%s%s", first ? "" : ",",
1271                            strrchr(s->module->name, '/') + 1);
1272                 first = 0;
1273         }
1274         buf_printf(b, "\";\n");
1275 }
1276
1277 static void add_srcversion(struct buffer *b, struct module *mod)
1278 {
1279         if (mod->srcversion[0]) {
1280                 buf_printf(b, "\n");
1281                 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1282                            mod->srcversion);
1283         }
1284 }
1285
1286 static void write_if_changed(struct buffer *b, const char *fname)
1287 {
1288         char *tmp;
1289         FILE *file;
1290         struct stat st;
1291
1292         file = fopen(fname, "r");
1293         if (!file)
1294                 goto write;
1295
1296         if (fstat(fileno(file), &st) < 0)
1297                 goto close_write;
1298
1299         if (st.st_size != b->pos)
1300                 goto close_write;
1301
1302         tmp = NOFAIL(malloc(b->pos));
1303         if (fread(tmp, 1, b->pos, file) != b->pos)
1304                 goto free_write;
1305
1306         if (memcmp(tmp, b->p, b->pos) != 0)
1307                 goto free_write;
1308
1309         free(tmp);
1310         fclose(file);
1311         return;
1312
1313  free_write:
1314         free(tmp);
1315  close_write:
1316         fclose(file);
1317  write:
1318         file = fopen(fname, "w");
1319         if (!file) {
1320                 perror(fname);
1321                 exit(1);
1322         }
1323         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1324                 perror(fname);
1325                 exit(1);
1326         }
1327         fclose(file);
1328 }
1329
1330 /* parse Module.symvers file. line format:
1331  * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1332  **/
1333 static void read_dump(const char *fname, unsigned int kernel)
1334 {
1335         unsigned long size, pos = 0;
1336         void *file = grab_file(fname, &size);
1337         char *line;
1338
1339         if (!file)
1340                 /* No symbol versions, silently ignore */
1341                 return;
1342
1343         while ((line = get_next_line(&pos, file, size))) {
1344                 char *symname, *modname, *d, *export, *end;
1345                 unsigned int crc;
1346                 struct module *mod;
1347                 struct symbol *s;
1348
1349                 if (!(symname = strchr(line, '\t')))
1350                         goto fail;
1351                 *symname++ = '\0';
1352                 if (!(modname = strchr(symname, '\t')))
1353                         goto fail;
1354                 *modname++ = '\0';
1355                 if ((export = strchr(modname, '\t')) != NULL)
1356                         *export++ = '\0';
1357                 if (export && ((end = strchr(export, '\t')) != NULL))
1358                         *end = '\0';
1359                 crc = strtoul(line, &d, 16);
1360                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1361                         goto fail;
1362
1363                 if (!(mod = find_module(modname))) {
1364                         if (is_vmlinux(modname)) {
1365                                 have_vmlinux = 1;
1366                         }
1367                         mod = new_module(NOFAIL(strdup(modname)));
1368                         mod->skip = 1;
1369                 }
1370                 s = sym_add_exported(symname, mod, export_no(export));
1371                 s->kernel    = kernel;
1372                 s->preloaded = 1;
1373                 sym_update_crc(symname, mod, crc, export_no(export));
1374         }
1375         return;
1376 fail:
1377         fatal("parse error in symbol dump file\n");
1378 }
1379
1380 /* For normal builds always dump all symbols.
1381  * For external modules only dump symbols
1382  * that are not read from kernel Module.symvers.
1383  **/
1384 static int dump_sym(struct symbol *sym)
1385 {
1386         if (!external_module)
1387                 return 1;
1388         if (sym->vmlinux || sym->kernel)
1389                 return 0;
1390         return 1;
1391 }
1392
1393 static void write_dump(const char *fname)
1394 {
1395         struct buffer buf = { };
1396         struct symbol *symbol;
1397         int n;
1398
1399         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1400                 symbol = symbolhash[n];
1401                 while (symbol) {
1402                         if (dump_sym(symbol))
1403                                 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1404                                         symbol->crc, symbol->name,
1405                                         symbol->module->name,
1406                                         export_str(symbol->export));
1407                         symbol = symbol->next;
1408                 }
1409         }
1410         write_if_changed(&buf, fname);
1411 }
1412
1413 int main(int argc, char **argv)
1414 {
1415         struct module *mod;
1416         struct buffer buf = { };
1417         char fname[SZ];
1418         char *kernel_read = NULL, *module_read = NULL;
1419         char *dump_write = NULL;
1420         int opt;
1421         int err;
1422
1423         while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
1424                 switch(opt) {
1425                         case 'i':
1426                                 kernel_read = optarg;
1427                                 break;
1428                         case 'I':
1429                                 module_read = optarg;
1430                                 external_module = 1;
1431                                 break;
1432                         case 'm':
1433                                 modversions = 1;
1434                                 break;
1435                         case 'o':
1436                                 dump_write = optarg;
1437                                 break;
1438                         case 'a':
1439                                 all_versions = 1;
1440                                 break;
1441                         case 'w':
1442                                 warn_unresolved = 1;
1443                                 break;
1444                         default:
1445                                 exit(1);
1446                 }
1447         }
1448
1449         if (kernel_read)
1450                 read_dump(kernel_read, 1);
1451         if (module_read)
1452                 read_dump(module_read, 0);
1453
1454         while (optind < argc) {
1455                 read_symbols(argv[optind++]);
1456         }
1457
1458         for (mod = modules; mod; mod = mod->next) {
1459                 if (mod->skip)
1460                         continue;
1461                 check_exports(mod);
1462         }
1463
1464         err = 0;
1465
1466         for (mod = modules; mod; mod = mod->next) {
1467                 if (mod->skip)
1468                         continue;
1469
1470                 buf.pos = 0;
1471
1472                 add_header(&buf, mod);
1473                 err |= add_versions(&buf, mod);
1474                 add_depends(&buf, mod, modules);
1475                 add_moddevtable(&buf, mod);
1476                 add_srcversion(&buf, mod);
1477
1478                 sprintf(fname, "%s.mod.c", mod->name);
1479                 write_if_changed(&buf, fname);
1480         }
1481
1482         if (dump_write)
1483                 write_dump(dump_write);
1484
1485         return err;
1486 }