kbuild: fix section mismatch check for vmlinux
[safe/jmp/linux-2.6] / 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 int parse_elf(struct elf_info *info, const char *filename)
337 {
338         unsigned int i;
339         Elf_Ehdr *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                 /* file too small, assume this is an empty .o file */
351                 return 0;
352         }
353         /* Is this a valid ELF file? */
354         if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
355             (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
356             (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
357             (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
358                 /* Not an ELF file - silently ignore it */
359                 return 0;
360         }
361         /* Fix endianness in ELF header */
362         hdr->e_shoff    = TO_NATIVE(hdr->e_shoff);
363         hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
364         hdr->e_shnum    = TO_NATIVE(hdr->e_shnum);
365         hdr->e_machine  = TO_NATIVE(hdr->e_machine);
366         sechdrs = (void *)hdr + hdr->e_shoff;
367         info->sechdrs = sechdrs;
368
369         /* Fix endianness in section headers */
370         for (i = 0; i < hdr->e_shnum; i++) {
371                 sechdrs[i].sh_type   = TO_NATIVE(sechdrs[i].sh_type);
372                 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
373                 sechdrs[i].sh_size   = TO_NATIVE(sechdrs[i].sh_size);
374                 sechdrs[i].sh_link   = TO_NATIVE(sechdrs[i].sh_link);
375                 sechdrs[i].sh_name   = TO_NATIVE(sechdrs[i].sh_name);
376         }
377         /* Find symbol table. */
378         for (i = 1; i < hdr->e_shnum; i++) {
379                 const char *secstrings
380                         = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
381                 const char *secname;
382
383                 if (sechdrs[i].sh_offset > info->size) {
384                         fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
385                         return 0;
386                 }
387                 secname = secstrings + sechdrs[i].sh_name;
388                 if (strcmp(secname, ".modinfo") == 0) {
389                         info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
390                         info->modinfo_len = sechdrs[i].sh_size;
391                 } else if (strcmp(secname, "__ksymtab") == 0)
392                         info->export_sec = i;
393                 else if (strcmp(secname, "__ksymtab_unused") == 0)
394                         info->export_unused_sec = i;
395                 else if (strcmp(secname, "__ksymtab_gpl") == 0)
396                         info->export_gpl_sec = i;
397                 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
398                         info->export_unused_gpl_sec = i;
399                 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
400                         info->export_gpl_future_sec = i;
401
402                 if (sechdrs[i].sh_type != SHT_SYMTAB)
403                         continue;
404
405                 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
406                 info->symtab_stop  = (void *)hdr + sechdrs[i].sh_offset
407                                                  + sechdrs[i].sh_size;
408                 info->strtab       = (void *)hdr +
409                                      sechdrs[sechdrs[i].sh_link].sh_offset;
410         }
411         if (!info->symtab_start) {
412                 fatal("%s has no symtab?\n", filename);
413         }
414         /* Fix endianness in symbols */
415         for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
416                 sym->st_shndx = TO_NATIVE(sym->st_shndx);
417                 sym->st_name  = TO_NATIVE(sym->st_name);
418                 sym->st_value = TO_NATIVE(sym->st_value);
419                 sym->st_size  = TO_NATIVE(sym->st_size);
420         }
421         return 1;
422 }
423
424 static void parse_elf_finish(struct elf_info *info)
425 {
426         release_file(info->hdr, info->size);
427 }
428
429 #define CRC_PFX     MODULE_SYMBOL_PREFIX "__crc_"
430 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
431
432 static void handle_modversions(struct module *mod, struct elf_info *info,
433                                Elf_Sym *sym, const char *symname)
434 {
435         unsigned int crc;
436         enum export export = export_from_sec(info, sym->st_shndx);
437
438         switch (sym->st_shndx) {
439         case SHN_COMMON:
440                 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
441                 break;
442         case SHN_ABS:
443                 /* CRC'd symbol */
444                 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
445                         crc = (unsigned int) sym->st_value;
446                         sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
447                                         export);
448                 }
449                 break;
450         case SHN_UNDEF:
451                 /* undefined symbol */
452                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
453                     ELF_ST_BIND(sym->st_info) != STB_WEAK)
454                         break;
455                 /* ignore global offset table */
456                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
457                         break;
458                 /* ignore __this_module, it will be resolved shortly */
459                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
460                         break;
461 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
462 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
463 /* add compatibility with older glibc */
464 #ifndef STT_SPARC_REGISTER
465 #define STT_SPARC_REGISTER STT_REGISTER
466 #endif
467                 if (info->hdr->e_machine == EM_SPARC ||
468                     info->hdr->e_machine == EM_SPARCV9) {
469                         /* Ignore register directives. */
470                         if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
471                                 break;
472                         if (symname[0] == '.') {
473                                 char *munged = strdup(symname);
474                                 munged[0] = '_';
475                                 munged[1] = toupper(munged[1]);
476                                 symname = munged;
477                         }
478                 }
479 #endif
480
481                 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
482                            strlen(MODULE_SYMBOL_PREFIX)) == 0)
483                         mod->unres = alloc_symbol(symname +
484                                                   strlen(MODULE_SYMBOL_PREFIX),
485                                                   ELF_ST_BIND(sym->st_info) == STB_WEAK,
486                                                   mod->unres);
487                 break;
488         default:
489                 /* All exported symbols */
490                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
491                         sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
492                                         export);
493                 }
494                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
495                         mod->has_init = 1;
496                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
497                         mod->has_cleanup = 1;
498                 break;
499         }
500 }
501
502 /**
503  * Parse tag=value strings from .modinfo section
504  **/
505 static char *next_string(char *string, unsigned long *secsize)
506 {
507         /* Skip non-zero chars */
508         while (string[0]) {
509                 string++;
510                 if ((*secsize)-- <= 1)
511                         return NULL;
512         }
513
514         /* Skip any zero padding. */
515         while (!string[0]) {
516                 string++;
517                 if ((*secsize)-- <= 1)
518                         return NULL;
519         }
520         return string;
521 }
522
523 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
524                               const char *tag, char *info)
525 {
526         char *p;
527         unsigned int taglen = strlen(tag);
528         unsigned long size = modinfo_len;
529
530         if (info) {
531                 size -= info - (char *)modinfo;
532                 modinfo = next_string(info, &size);
533         }
534
535         for (p = modinfo; p; p = next_string(p, &size)) {
536                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
537                         return p + taglen + 1;
538         }
539         return NULL;
540 }
541
542 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
543                          const char *tag)
544
545 {
546         return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
547 }
548
549 /**
550  * Test if string s ends in string sub
551  * return 0 if match
552  **/
553 static int strrcmp(const char *s, const char *sub)
554 {
555         int slen, sublen;
556
557         if (!s || !sub)
558                 return 1;
559
560         slen = strlen(s);
561         sublen = strlen(sub);
562
563         if ((slen == 0) || (sublen == 0))
564                 return 1;
565
566         if (sublen > slen)
567                 return 1;
568
569         return memcmp(s + slen - sublen, sub, sublen);
570 }
571
572 /**
573  * Whitelist to allow certain references to pass with no warning.
574  * Pattern 1:
575  *   If a module parameter is declared __initdata and permissions=0
576  *   then this is legal despite the warning generated.
577  *   We cannot see value of permissions here, so just ignore
578  *   this pattern.
579  *   The pattern is identified by:
580  *   tosec   = .init.data
581  *   fromsec = .data*
582  *   atsym   =__param*
583  *
584  * Pattern 2:
585  *   Many drivers utilise a *driver container with references to
586  *   add, remove, probe functions etc.
587  *   These functions may often be marked __init and we do not want to
588  *   warn here.
589  *   the pattern is identified by:
590  *   tosec   = .init.text | .exit.text | .init.data
591  *   fromsec = .data
592  *   atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
593  *
594  * Pattern 3:
595  *   Some symbols belong to init section but still it is ok to reference
596  *   these from non-init sections as these symbols don't have any memory
597  *   allocated for them and symbol address and value are same. So even
598  *   if init section is freed, its ok to reference those symbols.
599  *   For ex. symbols marking the init section boundaries.
600  *   This pattern is identified by
601  *   refsymname = __init_begin, _sinittext, _einittext
602  **/
603 static int secref_whitelist(const char *modname, const char *tosec,
604                             const char *fromsec, const char *atsym,
605                             const char *refsymname)
606 {
607         int f1 = 1, f2 = 1;
608         const char **s;
609         const char *pat2sym[] = {
610                 "driver",
611                 "_template", /* scsi uses *_template a lot */
612                 "_sht",      /* scsi also used *_sht to some extent */
613                 "_ops",
614                 "_probe",
615                 "_probe_one",
616                 "_console",
617                 NULL
618         };
619
620         const char *pat3refsym[] = {
621                 "__init_begin",
622                 "_sinittext",
623                 "_einittext",
624                 NULL
625         };
626
627         /* Check for pattern 1 */
628         if (strcmp(tosec, ".init.data") != 0)
629                 f1 = 0;
630         if (strncmp(fromsec, ".data", strlen(".data")) != 0)
631                 f1 = 0;
632         if (strncmp(atsym, "__param", strlen("__param")) != 0)
633                 f1 = 0;
634
635         if (f1)
636                 return f1;
637
638         /* Check for pattern 2 */
639         if ((strcmp(tosec, ".init.text") != 0) &&
640             (strcmp(tosec, ".exit.text") != 0) &&
641             (strcmp(tosec, ".init.data") != 0))
642                 f2 = 0;
643         if (strcmp(fromsec, ".data") != 0)
644                 f2 = 0;
645
646         for (s = pat2sym; *s; s++)
647                 if (strrcmp(atsym, *s) == 0)
648                         f1 = 1;
649         if (f1 && f2)
650                 return 1;
651
652         /* Whitelist all references from .pci_fixup section if vmlinux
653          * Whitelist all refereces from .text.head to .init.data if vmlinux
654          * Whitelist all refereces from .text.head to .init.text if vmlinux
655          */
656         if (is_vmlinux(modname)) {
657                 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
658                     (strcmp(tosec, ".init.text") == 0))
659                 return 1;
660
661                 if ((strcmp(fromsec, ".text.head") == 0) &&
662                         ((strcmp(tosec, ".init.data") == 0) ||
663                         (strcmp(tosec, ".init.text") == 0)))
664                 return 1;
665
666                 /* Check for pattern 3 */
667                 for (s = pat3refsym; *s; s++)
668                         if (strcmp(refsymname, *s) == 0)
669                                 return 1;
670         }
671         return 0;
672 }
673
674 /**
675  * Find symbol based on relocation record info.
676  * In some cases the symbol supplied is a valid symbol so
677  * return refsym. If st_name != 0 we assume this is a valid symbol.
678  * In other cases the symbol needs to be looked up in the symbol table
679  * based on section and address.
680  *  **/
681 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
682                                 Elf_Sym *relsym)
683 {
684         Elf_Sym *sym;
685
686         if (relsym->st_name != 0)
687                 return relsym;
688         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
689                 if (sym->st_shndx != relsym->st_shndx)
690                         continue;
691                 if (sym->st_value == addr)
692                         return sym;
693         }
694         return NULL;
695 }
696
697 static inline int is_arm_mapping_symbol(const char *str)
698 {
699         return str[0] == '$' && strchr("atd", str[1])
700                && (str[2] == '\0' || str[2] == '.');
701 }
702
703 /*
704  * If there's no name there, ignore it; likewise, ignore it if it's
705  * one of the magic symbols emitted used by current ARM tools.
706  *
707  * Otherwise if find_symbols_between() returns those symbols, they'll
708  * fail the whitelist tests and cause lots of false alarms ... fixable
709  * only by merging __exit and __init sections into __text, bloating
710  * the kernel (which is especially evil on embedded platforms).
711  */
712 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
713 {
714         const char *name = elf->strtab + sym->st_name;
715
716         if (!name || !strlen(name))
717                 return 0;
718         return !is_arm_mapping_symbol(name);
719 }
720
721 /*
722  * Find symbols before or equal addr and after addr - in the section sec.
723  * If we find two symbols with equal offset prefer one with a valid name.
724  * The ELF format may have a better way to detect what type of symbol
725  * it is, but this works for now.
726  **/
727 static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
728                                  const char *sec,
729                                  Elf_Sym **before, Elf_Sym **after)
730 {
731         Elf_Sym *sym;
732         Elf_Ehdr *hdr = elf->hdr;
733         Elf_Addr beforediff = ~0;
734         Elf_Addr afterdiff = ~0;
735         const char *secstrings = (void *)hdr +
736                                  elf->sechdrs[hdr->e_shstrndx].sh_offset;
737
738         *before = NULL;
739         *after = NULL;
740
741         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
742                 const char *symsec;
743
744                 if (sym->st_shndx >= SHN_LORESERVE)
745                         continue;
746                 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
747                 if (strcmp(symsec, sec) != 0)
748                         continue;
749                 if (!is_valid_name(elf, sym))
750                         continue;
751                 if (sym->st_value <= addr) {
752                         if ((addr - sym->st_value) < beforediff) {
753                                 beforediff = addr - sym->st_value;
754                                 *before = sym;
755                         }
756                         else if ((addr - sym->st_value) == beforediff) {
757                                 *before = sym;
758                         }
759                 }
760                 else
761                 {
762                         if ((sym->st_value - addr) < afterdiff) {
763                                 afterdiff = sym->st_value - addr;
764                                 *after = sym;
765                         }
766                         else if ((sym->st_value - addr) == afterdiff) {
767                                 *after = sym;
768                         }
769                 }
770         }
771 }
772
773 /**
774  * Print a warning about a section mismatch.
775  * Try to find symbols near it so user can find it.
776  * Check whitelist before warning - it may be a false positive.
777  **/
778 static void warn_sec_mismatch(const char *modname, const char *fromsec,
779                               struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
780 {
781         const char *refsymname = "";
782         Elf_Sym *before, *after;
783         Elf_Sym *refsym;
784         Elf_Ehdr *hdr = elf->hdr;
785         Elf_Shdr *sechdrs = elf->sechdrs;
786         const char *secstrings = (void *)hdr +
787                                  sechdrs[hdr->e_shstrndx].sh_offset;
788         const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
789
790         find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
791
792         refsym = find_elf_symbol(elf, r.r_addend, sym);
793         if (refsym && strlen(elf->strtab + refsym->st_name))
794                 refsymname = elf->strtab + refsym->st_name;
795
796         /* check whitelist - we may ignore it */
797         if (before &&
798             secref_whitelist(modname, secname, fromsec,
799                              elf->strtab + before->st_name, refsymname))
800                 return;
801
802         if (before && after) {
803                 warn("%s - Section mismatch: reference to %s:%s from %s "
804                      "between '%s' (at offset 0x%llx) and '%s'\n",
805                      modname, secname, refsymname, fromsec,
806                      elf->strtab + before->st_name,
807                      (long long)r.r_offset,
808                      elf->strtab + after->st_name);
809         } else if (before) {
810                 warn("%s - Section mismatch: reference to %s:%s from %s "
811                      "after '%s' (at offset 0x%llx)\n",
812                      modname, secname, refsymname, fromsec,
813                      elf->strtab + before->st_name,
814                      (long long)r.r_offset);
815         } else if (after) {
816                 warn("%s - Section mismatch: reference to %s:%s from %s "
817                      "before '%s' (at offset -0x%llx)\n",
818                      modname, secname, refsymname, fromsec,
819                      elf->strtab + after->st_name,
820                      (long long)r.r_offset);
821         } else {
822                 warn("%s - Section mismatch: reference to %s:%s from %s "
823                      "(offset 0x%llx)\n",
824                      modname, secname, fromsec, refsymname,
825                      (long long)r.r_offset);
826         }
827 }
828
829 /**
830  * A module includes a number of sections that are discarded
831  * either when loaded or when used as built-in.
832  * For loaded modules all functions marked __init and all data
833  * marked __initdata will be discarded when the module has been intialized.
834  * Likewise for modules used built-in the sections marked __exit
835  * are discarded because __exit marked function are supposed to be called
836  * only when a moduel is unloaded which never happes for built-in modules.
837  * The check_sec_ref() function traverses all relocation records
838  * to find all references to a section that reference a section that will
839  * be discarded and warns about it.
840  **/
841 static void check_sec_ref(struct module *mod, const char *modname,
842                           struct elf_info *elf,
843                           int section(const char*),
844                           int section_ref_ok(const char *))
845 {
846         int i;
847         Elf_Sym  *sym;
848         Elf_Ehdr *hdr = elf->hdr;
849         Elf_Shdr *sechdrs = elf->sechdrs;
850         const char *secstrings = (void *)hdr +
851                                  sechdrs[hdr->e_shstrndx].sh_offset;
852
853         /* Walk through all sections */
854         for (i = 0; i < hdr->e_shnum; i++) {
855                 const char *name = secstrings + sechdrs[i].sh_name;
856                 const char *secname;
857                 Elf_Rela r;
858                 unsigned int r_sym;
859                 /* We want to process only relocation sections and not .init */
860                 if (sechdrs[i].sh_type == SHT_RELA) {
861                         Elf_Rela *rela;
862                         Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
863                         Elf_Rela *stop  = (void*)start + sechdrs[i].sh_size;
864                         name += strlen(".rela");
865                         if (section_ref_ok(name))
866                                 continue;
867
868                         for (rela = start; rela < stop; rela++) {
869                                 r.r_offset = TO_NATIVE(rela->r_offset);
870 #if KERNEL_ELFCLASS == ELFCLASS64
871                                 if (hdr->e_machine == EM_MIPS) {
872                                         r_sym = ELF64_MIPS_R_SYM(rela->r_info);
873                                         r_sym = TO_NATIVE(r_sym);
874                                 } else {
875                                         r.r_info = TO_NATIVE(rela->r_info);
876                                         r_sym = ELF_R_SYM(r.r_info);
877                                 }
878 #else
879                                 r.r_info = TO_NATIVE(rela->r_info);
880                                 r_sym = ELF_R_SYM(r.r_info);
881 #endif
882                                 r.r_addend = TO_NATIVE(rela->r_addend);
883                                 sym = elf->symtab_start + r_sym;
884                                 /* Skip special sections */
885                                 if (sym->st_shndx >= SHN_LORESERVE)
886                                         continue;
887
888                                 secname = secstrings +
889                                         sechdrs[sym->st_shndx].sh_name;
890                                 if (section(secname))
891                                         warn_sec_mismatch(modname, name,
892                                                           elf, sym, r);
893                         }
894                 } else if (sechdrs[i].sh_type == SHT_REL) {
895                         Elf_Rel *rel;
896                         Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
897                         Elf_Rel *stop  = (void*)start + sechdrs[i].sh_size;
898                         name += strlen(".rel");
899                         if (section_ref_ok(name))
900                                 continue;
901
902                         for (rel = start; rel < stop; rel++) {
903                                 r.r_offset = TO_NATIVE(rel->r_offset);
904 #if KERNEL_ELFCLASS == ELFCLASS64
905                                 if (hdr->e_machine == EM_MIPS) {
906                                         r_sym = ELF64_MIPS_R_SYM(rel->r_info);
907                                         r_sym = TO_NATIVE(r_sym);
908                                 } else {
909                                         r.r_info = TO_NATIVE(rel->r_info);
910                                         r_sym = ELF_R_SYM(r.r_info);
911                                 }
912 #else
913                                 r.r_info = TO_NATIVE(rel->r_info);
914                                 r_sym = ELF_R_SYM(r.r_info);
915 #endif
916                                 r.r_addend = 0;
917                                 sym = elf->symtab_start + r_sym;
918                                 /* Skip special sections */
919                                 if (sym->st_shndx >= SHN_LORESERVE)
920                                         continue;
921
922                                 secname = secstrings +
923                                         sechdrs[sym->st_shndx].sh_name;
924                                 if (section(secname))
925                                         warn_sec_mismatch(modname, name,
926                                                           elf, sym, r);
927                         }
928                 }
929         }
930 }
931
932 /**
933  * Functions used only during module init is marked __init and is stored in
934  * a .init.text section. Likewise data is marked __initdata and stored in
935  * a .init.data section.
936  * If this section is one of these sections return 1
937  * See include/linux/init.h for the details
938  **/
939 static int init_section(const char *name)
940 {
941         if (strcmp(name, ".init") == 0)
942                 return 1;
943         if (strncmp(name, ".init.", strlen(".init.")) == 0)
944                 return 1;
945         return 0;
946 }
947
948 /**
949  * Identify sections from which references to a .init section is OK.
950  *
951  * Unfortunately references to read only data that referenced .init
952  * sections had to be excluded. Almost all of these are false
953  * positives, they are created by gcc. The downside of excluding rodata
954  * is that there really are some user references from rodata to
955  * init code, e.g. drivers/video/vgacon.c:
956  *
957  * const struct consw vga_con = {
958  *        con_startup:            vgacon_startup,
959  *
960  * where vgacon_startup is __init.  If you want to wade through the false
961  * positives, take out the check for rodata.
962  **/
963 static int init_section_ref_ok(const char *name)
964 {
965         const char **s;
966         /* Absolute section names */
967         const char *namelist1[] = {
968                 ".init",
969                 ".opd",   /* see comment [OPD] at exit_section_ref_ok() */
970                 ".toc1",  /* used by ppc64 */
971                 ".stab",
972                 ".data.rel.ro", /* used by parisc64 */
973                 ".parainstructions",
974                 ".text.lock",
975                 "__bug_table", /* used by powerpc for BUG() */
976                 ".pci_fixup_header",
977                 ".pci_fixup_final",
978                 ".pdr",
979                 "__param",
980                 "__ex_table",
981                 ".fixup",
982                 ".smp_locks",
983                 ".plt",  /* seen on ARCH=um build on x86_64. Harmless */
984                 "__ftr_fixup",          /* powerpc cpu feature fixup */
985                 "__fw_ftr_fixup",       /* powerpc firmware feature fixup */
986                 NULL
987         };
988         /* Start of section names */
989         const char *namelist2[] = {
990                 ".init.",
991                 ".altinstructions",
992                 ".eh_frame",
993                 ".debug",
994                 ".parainstructions",
995                 ".rodata",
996                 NULL
997         };
998         /* part of section name */
999         const char *namelist3 [] = {
1000                 ".unwind",  /* sample: IA_64.unwind.init.text */
1001                 NULL
1002         };
1003
1004         for (s = namelist1; *s; s++)
1005                 if (strcmp(*s, name) == 0)
1006                         return 1;
1007         for (s = namelist2; *s; s++)
1008                 if (strncmp(*s, name, strlen(*s)) == 0)
1009                         return 1;
1010         for (s = namelist3; *s; s++)
1011                 if (strstr(name, *s) != NULL)
1012                         return 1;
1013         if (strrcmp(name, ".init") == 0)
1014                 return 1;
1015         return 0;
1016 }
1017
1018 /*
1019  * Functions used only during module exit is marked __exit and is stored in
1020  * a .exit.text section. Likewise data is marked __exitdata and stored in
1021  * a .exit.data section.
1022  * If this section is one of these sections return 1
1023  * See include/linux/init.h for the details
1024  **/
1025 static int exit_section(const char *name)
1026 {
1027         if (strcmp(name, ".exit.text") == 0)
1028                 return 1;
1029         if (strcmp(name, ".exit.data") == 0)
1030                 return 1;
1031         return 0;
1032
1033 }
1034
1035 /*
1036  * Identify sections from which references to a .exit section is OK.
1037  *
1038  * [OPD] Keith Ownes <kaos@sgi.com> commented:
1039  * For our future {in}sanity, add a comment that this is the ppc .opd
1040  * section, not the ia64 .opd section.
1041  * ia64 .opd should not point to discarded sections.
1042  * [.rodata] like for .init.text we ignore .rodata references -same reason
1043  **/
1044 static int exit_section_ref_ok(const char *name)
1045 {
1046         const char **s;
1047         /* Absolute section names */
1048         const char *namelist1[] = {
1049                 ".exit.text",
1050                 ".exit.data",
1051                 ".init.text",
1052                 ".rodata",
1053                 ".opd", /* See comment [OPD] */
1054                 ".toc1",  /* used by ppc64 */
1055                 ".altinstructions",
1056                 ".pdr",
1057                 "__bug_table", /* used by powerpc for BUG() */
1058                 ".exitcall.exit",
1059                 ".eh_frame",
1060                 ".parainstructions",
1061                 ".stab",
1062                 "__ex_table",
1063                 ".fixup",
1064                 ".smp_locks",
1065                 ".plt",  /* seen on ARCH=um build on x86_64. Harmless */
1066                 NULL
1067         };
1068         /* Start of section names */
1069         const char *namelist2[] = {
1070                 ".debug",
1071                 NULL
1072         };
1073         /* part of section name */
1074         const char *namelist3 [] = {
1075                 ".unwind",  /* Sample: IA_64.unwind.exit.text */
1076                 NULL
1077         };
1078
1079         for (s = namelist1; *s; s++)
1080                 if (strcmp(*s, name) == 0)
1081                         return 1;
1082         for (s = namelist2; *s; s++)
1083                 if (strncmp(*s, name, strlen(*s)) == 0)
1084                         return 1;
1085         for (s = namelist3; *s; s++)
1086                 if (strstr(name, *s) != NULL)
1087                         return 1;
1088         return 0;
1089 }
1090
1091 static void read_symbols(char *modname)
1092 {
1093         const char *symname;
1094         char *version;
1095         char *license;
1096         struct module *mod;
1097         struct elf_info info = { };
1098         Elf_Sym *sym;
1099
1100         if (!parse_elf(&info, modname))
1101                 return;
1102
1103         mod = new_module(modname);
1104
1105         /* When there's no vmlinux, don't print warnings about
1106          * unresolved symbols (since there'll be too many ;) */
1107         if (is_vmlinux(modname)) {
1108                 have_vmlinux = 1;
1109                 mod->skip = 1;
1110         }
1111
1112         license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1113         while (license) {
1114                 if (license_is_gpl_compatible(license))
1115                         mod->gpl_compatible = 1;
1116                 else {
1117                         mod->gpl_compatible = 0;
1118                         break;
1119                 }
1120                 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1121                                            "license", license);
1122         }
1123
1124         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1125                 symname = info.strtab + sym->st_name;
1126
1127                 handle_modversions(mod, &info, sym, symname);
1128                 handle_moddevtable(mod, &info, sym, symname);
1129         }
1130         check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1131         check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1132
1133         version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1134         if (version)
1135                 maybe_frob_rcs_version(modname, version, info.modinfo,
1136                                        version - (char *)info.hdr);
1137         if (version || (all_versions && !is_vmlinux(modname)))
1138                 get_src_version(modname, mod->srcversion,
1139                                 sizeof(mod->srcversion)-1);
1140
1141         parse_elf_finish(&info);
1142
1143         /* Our trick to get versioning for struct_module - it's
1144          * never passed as an argument to an exported function, so
1145          * the automatic versioning doesn't pick it up, but it's really
1146          * important anyhow */
1147         if (modversions)
1148                 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1149 }
1150
1151 #define SZ 500
1152
1153 /* We first write the generated file into memory using the
1154  * following helper, then compare to the file on disk and
1155  * only update the later if anything changed */
1156
1157 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1158                                                       const char *fmt, ...)
1159 {
1160         char tmp[SZ];
1161         int len;
1162         va_list ap;
1163
1164         va_start(ap, fmt);
1165         len = vsnprintf(tmp, SZ, fmt, ap);
1166         buf_write(buf, tmp, len);
1167         va_end(ap);
1168 }
1169
1170 void buf_write(struct buffer *buf, const char *s, int len)
1171 {
1172         if (buf->size - buf->pos < len) {
1173                 buf->size += len + SZ;
1174                 buf->p = realloc(buf->p, buf->size);
1175         }
1176         strncpy(buf->p + buf->pos, s, len);
1177         buf->pos += len;
1178 }
1179
1180 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1181 {
1182         const char *e = is_vmlinux(m) ?"":".ko";
1183
1184         switch (exp) {
1185         case export_gpl:
1186                 fatal("modpost: GPL-incompatible module %s%s "
1187                       "uses GPL-only symbol '%s'\n", m, e, s);
1188                 break;
1189         case export_unused_gpl:
1190                 fatal("modpost: GPL-incompatible module %s%s "
1191                       "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1192                 break;
1193         case export_gpl_future:
1194                 warn("modpost: GPL-incompatible module %s%s "
1195                       "uses future GPL-only symbol '%s'\n", m, e, s);
1196                 break;
1197         case export_plain:
1198         case export_unused:
1199         case export_unknown:
1200                 /* ignore */
1201                 break;
1202         }
1203 }
1204
1205 static void check_for_unused(enum export exp, const char* m, const char* s)
1206 {
1207         const char *e = is_vmlinux(m) ?"":".ko";
1208
1209         switch (exp) {
1210         case export_unused:
1211         case export_unused_gpl:
1212                 warn("modpost: module %s%s "
1213                       "uses symbol '%s' marked UNUSED\n", m, e, s);
1214                 break;
1215         default:
1216                 /* ignore */
1217                 break;
1218         }
1219 }
1220
1221 static void check_exports(struct module *mod)
1222 {
1223         struct symbol *s, *exp;
1224
1225         for (s = mod->unres; s; s = s->next) {
1226                 const char *basename;
1227                 exp = find_symbol(s->name);
1228                 if (!exp || exp->module == mod)
1229                         continue;
1230                 basename = strrchr(mod->name, '/');
1231                 if (basename)
1232                         basename++;
1233                 else
1234                         basename = mod->name;
1235                 if (!mod->gpl_compatible)
1236                         check_for_gpl_usage(exp->export, basename, exp->name);
1237                 check_for_unused(exp->export, basename, exp->name);
1238         }
1239 }
1240
1241 /**
1242  * Header for the generated file
1243  **/
1244 static void add_header(struct buffer *b, struct module *mod)
1245 {
1246         buf_printf(b, "#include <linux/module.h>\n");
1247         buf_printf(b, "#include <linux/vermagic.h>\n");
1248         buf_printf(b, "#include <linux/compiler.h>\n");
1249         buf_printf(b, "\n");
1250         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1251         buf_printf(b, "\n");
1252         buf_printf(b, "struct module __this_module\n");
1253         buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1254         buf_printf(b, " .name = KBUILD_MODNAME,\n");
1255         if (mod->has_init)
1256                 buf_printf(b, " .init = init_module,\n");
1257         if (mod->has_cleanup)
1258                 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1259                               " .exit = cleanup_module,\n"
1260                               "#endif\n");
1261         buf_printf(b, "};\n");
1262 }
1263
1264 /**
1265  * Record CRCs for unresolved symbols
1266  **/
1267 static int add_versions(struct buffer *b, struct module *mod)
1268 {
1269         struct symbol *s, *exp;
1270         int err = 0;
1271
1272         for (s = mod->unres; s; s = s->next) {
1273                 exp = find_symbol(s->name);
1274                 if (!exp || exp->module == mod) {
1275                         if (have_vmlinux && !s->weak) {
1276                                 warn("\"%s\" [%s.ko] undefined!\n",
1277                                      s->name, mod->name);
1278                                 err = warn_unresolved ? 0 : 1;
1279                         }
1280                         continue;
1281                 }
1282                 s->module = exp->module;
1283                 s->crc_valid = exp->crc_valid;
1284                 s->crc = exp->crc;
1285         }
1286
1287         if (!modversions)
1288                 return err;
1289
1290         buf_printf(b, "\n");
1291         buf_printf(b, "static const struct modversion_info ____versions[]\n");
1292         buf_printf(b, "__attribute_used__\n");
1293         buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1294
1295         for (s = mod->unres; s; s = s->next) {
1296                 if (!s->module) {
1297                         continue;
1298                 }
1299                 if (!s->crc_valid) {
1300                         warn("\"%s\" [%s.ko] has no CRC!\n",
1301                                 s->name, mod->name);
1302                         continue;
1303                 }
1304                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1305         }
1306
1307         buf_printf(b, "};\n");
1308
1309         return err;
1310 }
1311
1312 static void add_depends(struct buffer *b, struct module *mod,
1313                         struct module *modules)
1314 {
1315         struct symbol *s;
1316         struct module *m;
1317         int first = 1;
1318
1319         for (m = modules; m; m = m->next) {
1320                 m->seen = is_vmlinux(m->name);
1321         }
1322
1323         buf_printf(b, "\n");
1324         buf_printf(b, "static const char __module_depends[]\n");
1325         buf_printf(b, "__attribute_used__\n");
1326         buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1327         buf_printf(b, "\"depends=");
1328         for (s = mod->unres; s; s = s->next) {
1329                 if (!s->module)
1330                         continue;
1331
1332                 if (s->module->seen)
1333                         continue;
1334
1335                 s->module->seen = 1;
1336                 buf_printf(b, "%s%s", first ? "" : ",",
1337                            strrchr(s->module->name, '/') + 1);
1338                 first = 0;
1339         }
1340         buf_printf(b, "\";\n");
1341 }
1342
1343 static void add_srcversion(struct buffer *b, struct module *mod)
1344 {
1345         if (mod->srcversion[0]) {
1346                 buf_printf(b, "\n");
1347                 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1348                            mod->srcversion);
1349         }
1350 }
1351
1352 static void write_if_changed(struct buffer *b, const char *fname)
1353 {
1354         char *tmp;
1355         FILE *file;
1356         struct stat st;
1357
1358         file = fopen(fname, "r");
1359         if (!file)
1360                 goto write;
1361
1362         if (fstat(fileno(file), &st) < 0)
1363                 goto close_write;
1364
1365         if (st.st_size != b->pos)
1366                 goto close_write;
1367
1368         tmp = NOFAIL(malloc(b->pos));
1369         if (fread(tmp, 1, b->pos, file) != b->pos)
1370                 goto free_write;
1371
1372         if (memcmp(tmp, b->p, b->pos) != 0)
1373                 goto free_write;
1374
1375         free(tmp);
1376         fclose(file);
1377         return;
1378
1379  free_write:
1380         free(tmp);
1381  close_write:
1382         fclose(file);
1383  write:
1384         file = fopen(fname, "w");
1385         if (!file) {
1386                 perror(fname);
1387                 exit(1);
1388         }
1389         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1390                 perror(fname);
1391                 exit(1);
1392         }
1393         fclose(file);
1394 }
1395
1396 /* parse Module.symvers file. line format:
1397  * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1398  **/
1399 static void read_dump(const char *fname, unsigned int kernel)
1400 {
1401         unsigned long size, pos = 0;
1402         void *file = grab_file(fname, &size);
1403         char *line;
1404
1405         if (!file)
1406                 /* No symbol versions, silently ignore */
1407                 return;
1408
1409         while ((line = get_next_line(&pos, file, size))) {
1410                 char *symname, *modname, *d, *export, *end;
1411                 unsigned int crc;
1412                 struct module *mod;
1413                 struct symbol *s;
1414
1415                 if (!(symname = strchr(line, '\t')))
1416                         goto fail;
1417                 *symname++ = '\0';
1418                 if (!(modname = strchr(symname, '\t')))
1419                         goto fail;
1420                 *modname++ = '\0';
1421                 if ((export = strchr(modname, '\t')) != NULL)
1422                         *export++ = '\0';
1423                 if (export && ((end = strchr(export, '\t')) != NULL))
1424                         *end = '\0';
1425                 crc = strtoul(line, &d, 16);
1426                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1427                         goto fail;
1428
1429                 if (!(mod = find_module(modname))) {
1430                         if (is_vmlinux(modname)) {
1431                                 have_vmlinux = 1;
1432                         }
1433                         mod = new_module(NOFAIL(strdup(modname)));
1434                         mod->skip = 1;
1435                 }
1436                 s = sym_add_exported(symname, mod, export_no(export));
1437                 s->kernel    = kernel;
1438                 s->preloaded = 1;
1439                 sym_update_crc(symname, mod, crc, export_no(export));
1440         }
1441         return;
1442 fail:
1443         fatal("parse error in symbol dump file\n");
1444 }
1445
1446 /* For normal builds always dump all symbols.
1447  * For external modules only dump symbols
1448  * that are not read from kernel Module.symvers.
1449  **/
1450 static int dump_sym(struct symbol *sym)
1451 {
1452         if (!external_module)
1453                 return 1;
1454         if (sym->vmlinux || sym->kernel)
1455                 return 0;
1456         return 1;
1457 }
1458
1459 static void write_dump(const char *fname)
1460 {
1461         struct buffer buf = { };
1462         struct symbol *symbol;
1463         int n;
1464
1465         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1466                 symbol = symbolhash[n];
1467                 while (symbol) {
1468                         if (dump_sym(symbol))
1469                                 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1470                                         symbol->crc, symbol->name,
1471                                         symbol->module->name,
1472                                         export_str(symbol->export));
1473                         symbol = symbol->next;
1474                 }
1475         }
1476         write_if_changed(&buf, fname);
1477 }
1478
1479 int main(int argc, char **argv)
1480 {
1481         struct module *mod;
1482         struct buffer buf = { };
1483         char fname[SZ];
1484         char *kernel_read = NULL, *module_read = NULL;
1485         char *dump_write = NULL;
1486         int opt;
1487         int err;
1488
1489         while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
1490                 switch(opt) {
1491                         case 'i':
1492                                 kernel_read = optarg;
1493                                 break;
1494                         case 'I':
1495                                 module_read = optarg;
1496                                 external_module = 1;
1497                                 break;
1498                         case 'm':
1499                                 modversions = 1;
1500                                 break;
1501                         case 'o':
1502                                 dump_write = optarg;
1503                                 break;
1504                         case 'a':
1505                                 all_versions = 1;
1506                                 break;
1507                         case 'w':
1508                                 warn_unresolved = 1;
1509                                 break;
1510                         default:
1511                                 exit(1);
1512                 }
1513         }
1514
1515         if (kernel_read)
1516                 read_dump(kernel_read, 1);
1517         if (module_read)
1518                 read_dump(module_read, 0);
1519
1520         while (optind < argc) {
1521                 read_symbols(argv[optind++]);
1522         }
1523
1524         for (mod = modules; mod; mod = mod->next) {
1525                 if (mod->skip)
1526                         continue;
1527                 check_exports(mod);
1528         }
1529
1530         err = 0;
1531
1532         for (mod = modules; mod; mod = mod->next) {
1533                 if (mod->skip)
1534                         continue;
1535
1536                 buf.pos = 0;
1537
1538                 add_header(&buf, mod);
1539                 err |= add_versions(&buf, mod);
1540                 add_depends(&buf, mod, modules);
1541                 add_moddevtable(&buf, mod);
1542                 add_srcversion(&buf, mod);
1543
1544                 sprintf(fname, "%s.mod.c", mod->name);
1545                 write_if_changed(&buf, fname);
1546         }
1547
1548         if (dump_write)
1549                 write_dump(dump_write);
1550
1551         return err;
1552 }