Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[linux-2.6-block.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-2008  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 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <stdbool.h>
20 #include <errno.h>
21 #include "modpost.h"
22 #include "../../include/linux/license.h"
23
24 /* Are we using CONFIG_MODVERSIONS? */
25 static int modversions = 0;
26 /* Warn about undefined symbols? (do so if we have vmlinux) */
27 static int have_vmlinux = 0;
28 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29 static int all_versions = 0;
30 /* If we are modposting external module set to 1 */
31 static int external_module = 0;
32 /* Warn about section mismatch in vmlinux if set to 1 */
33 static int vmlinux_section_warnings = 1;
34 /* Only warn about unresolved symbols */
35 static int warn_unresolved = 0;
36 /* How a symbol is exported */
37 static int sec_mismatch_count = 0;
38 static int sec_mismatch_fatal = 0;
39 /* ignore missing files */
40 static int ignore_missing_files;
41
42 enum export {
43         export_plain,      export_unused,     export_gpl,
44         export_unused_gpl, export_gpl_future, export_unknown
45 };
46
47 /* In kernel, this size is defined in linux/module.h;
48  * here we use Elf_Addr instead of long for covering cross-compile
49  */
50
51 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
53 #define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55 PRINTF void fatal(const char *fmt, ...)
56 {
57         va_list arglist;
58
59         fprintf(stderr, "FATAL: ");
60
61         va_start(arglist, fmt);
62         vfprintf(stderr, fmt, arglist);
63         va_end(arglist);
64
65         exit(1);
66 }
67
68 PRINTF void warn(const char *fmt, ...)
69 {
70         va_list arglist;
71
72         fprintf(stderr, "WARNING: ");
73
74         va_start(arglist, fmt);
75         vfprintf(stderr, fmt, arglist);
76         va_end(arglist);
77 }
78
79 PRINTF void merror(const char *fmt, ...)
80 {
81         va_list arglist;
82
83         fprintf(stderr, "ERROR: ");
84
85         va_start(arglist, fmt);
86         vfprintf(stderr, fmt, arglist);
87         va_end(arglist);
88 }
89
90 static inline bool strends(const char *str, const char *postfix)
91 {
92         if (strlen(str) < strlen(postfix))
93                 return false;
94
95         return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96 }
97
98 static int is_vmlinux(const char *modname)
99 {
100         const char *myname;
101
102         myname = strrchr(modname, '/');
103         if (myname)
104                 myname++;
105         else
106                 myname = modname;
107
108         return (strcmp(myname, "vmlinux") == 0) ||
109                (strcmp(myname, "vmlinux.o") == 0);
110 }
111
112 void *do_nofail(void *ptr, const char *expr)
113 {
114         if (!ptr)
115                 fatal("modpost: Memory allocation failure: %s.\n", expr);
116
117         return ptr;
118 }
119
120 /* A list of all modules we processed */
121 static struct module *modules;
122
123 static struct module *find_module(const char *modname)
124 {
125         struct module *mod;
126
127         for (mod = modules; mod; mod = mod->next)
128                 if (strcmp(mod->name, modname) == 0)
129                         break;
130         return mod;
131 }
132
133 static struct module *new_module(const char *modname)
134 {
135         struct module *mod;
136         char *p;
137
138         mod = NOFAIL(malloc(sizeof(*mod)));
139         memset(mod, 0, sizeof(*mod));
140         p = NOFAIL(strdup(modname));
141
142         /* strip trailing .o */
143         if (strends(p, ".o")) {
144                 p[strlen(p) - 2] = '\0';
145                 mod->is_dot_o = 1;
146         }
147
148         /* add to list */
149         mod->name = p;
150         mod->gpl_compatible = -1;
151         mod->next = modules;
152         modules = mod;
153
154         return mod;
155 }
156
157 /* A hash of all exported symbols,
158  * struct symbol is also used for lists of unresolved symbols */
159
160 #define SYMBOL_HASH_SIZE 1024
161
162 struct symbol {
163         struct symbol *next;
164         struct module *module;
165         unsigned int crc;
166         int crc_valid;
167         unsigned int weak:1;
168         unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
169         unsigned int kernel:1;     /* 1 if symbol is from kernel
170                                     *  (only for external modules) **/
171         unsigned int preloaded:1;  /* 1 if symbol from Module.symvers, or crc */
172         unsigned int is_static:1;  /* 1 if symbol is not global */
173         enum export  export;       /* Type of export */
174         char name[0];
175 };
176
177 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
178
179 /* This is based on the hash agorithm from gdbm, via tdb */
180 static inline unsigned int tdb_hash(const char *name)
181 {
182         unsigned value; /* Used to compute the hash value.  */
183         unsigned   i;   /* Used to cycle through random values. */
184
185         /* Set the initial value from the key size. */
186         for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
187                 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
188
189         return (1103515243 * value + 12345);
190 }
191
192 /**
193  * Allocate a new symbols for use in the hash of exported symbols or
194  * the list of unresolved symbols per module
195  **/
196 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
197                                    struct symbol *next)
198 {
199         struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
200
201         memset(s, 0, sizeof(*s));
202         strcpy(s->name, name);
203         s->weak = weak;
204         s->next = next;
205         s->is_static = 1;
206         return s;
207 }
208
209 /* For the hash of exported symbols */
210 static struct symbol *new_symbol(const char *name, struct module *module,
211                                  enum export export)
212 {
213         unsigned int hash;
214         struct symbol *new;
215
216         hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
217         new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
218         new->module = module;
219         new->export = export;
220         return new;
221 }
222
223 static struct symbol *find_symbol(const char *name)
224 {
225         struct symbol *s;
226
227         /* For our purposes, .foo matches foo.  PPC64 needs this. */
228         if (name[0] == '.')
229                 name++;
230
231         for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
232                 if (strcmp(s->name, name) == 0)
233                         return s;
234         }
235         return NULL;
236 }
237
238 static const struct {
239         const char *str;
240         enum export export;
241 } export_list[] = {
242         { .str = "EXPORT_SYMBOL",            .export = export_plain },
243         { .str = "EXPORT_UNUSED_SYMBOL",     .export = export_unused },
244         { .str = "EXPORT_SYMBOL_GPL",        .export = export_gpl },
245         { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
246         { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
247         { .str = "(unknown)",                .export = export_unknown },
248 };
249
250
251 static const char *export_str(enum export ex)
252 {
253         return export_list[ex].str;
254 }
255
256 static enum export export_no(const char *s)
257 {
258         int i;
259
260         if (!s)
261                 return export_unknown;
262         for (i = 0; export_list[i].export != export_unknown; i++) {
263                 if (strcmp(export_list[i].str, s) == 0)
264                         return export_list[i].export;
265         }
266         return export_unknown;
267 }
268
269 static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
270 {
271         return (void *)elf->hdr +
272                 elf->sechdrs[elf->secindex_strings].sh_offset +
273                 sechdr->sh_name;
274 }
275
276 static const char *sec_name(struct elf_info *elf, int secindex)
277 {
278         return sech_name(elf, &elf->sechdrs[secindex]);
279 }
280
281 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
282
283 static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
284 {
285         const char *secname = sec_name(elf, sec);
286
287         if (strstarts(secname, "___ksymtab+"))
288                 return export_plain;
289         else if (strstarts(secname, "___ksymtab_unused+"))
290                 return export_unused;
291         else if (strstarts(secname, "___ksymtab_gpl+"))
292                 return export_gpl;
293         else if (strstarts(secname, "___ksymtab_unused_gpl+"))
294                 return export_unused_gpl;
295         else if (strstarts(secname, "___ksymtab_gpl_future+"))
296                 return export_gpl_future;
297         else
298                 return export_unknown;
299 }
300
301 static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
302 {
303         if (sec == elf->export_sec)
304                 return export_plain;
305         else if (sec == elf->export_unused_sec)
306                 return export_unused;
307         else if (sec == elf->export_gpl_sec)
308                 return export_gpl;
309         else if (sec == elf->export_unused_gpl_sec)
310                 return export_unused_gpl;
311         else if (sec == elf->export_gpl_future_sec)
312                 return export_gpl_future;
313         else
314                 return export_unknown;
315 }
316
317 /**
318  * Add an exported symbol - it may have already been added without a
319  * CRC, in this case just update the CRC
320  **/
321 static struct symbol *sym_add_exported(const char *name, struct module *mod,
322                                        enum export export)
323 {
324         struct symbol *s = find_symbol(name);
325
326         if (!s) {
327                 s = new_symbol(name, mod, export);
328         } else {
329                 if (!s->preloaded) {
330                         warn("%s: '%s' exported twice. Previous export "
331                              "was in %s%s\n", mod->name, name,
332                              s->module->name,
333                              is_vmlinux(s->module->name) ?"":".ko");
334                 } else {
335                         /* In case Module.symvers was out of date */
336                         s->module = mod;
337                 }
338         }
339         s->preloaded = 0;
340         s->vmlinux   = is_vmlinux(mod->name);
341         s->kernel    = 0;
342         s->export    = export;
343         return s;
344 }
345
346 static void sym_update_crc(const char *name, struct module *mod,
347                            unsigned int crc, enum export export)
348 {
349         struct symbol *s = find_symbol(name);
350
351         if (!s) {
352                 s = new_symbol(name, mod, export);
353                 /* Don't complain when we find it later. */
354                 s->preloaded = 1;
355         }
356         s->crc = crc;
357         s->crc_valid = 1;
358 }
359
360 void *grab_file(const char *filename, unsigned long *size)
361 {
362         struct stat st;
363         void *map = MAP_FAILED;
364         int fd;
365
366         fd = open(filename, O_RDONLY);
367         if (fd < 0)
368                 return NULL;
369         if (fstat(fd, &st))
370                 goto failed;
371
372         *size = st.st_size;
373         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
374
375 failed:
376         close(fd);
377         if (map == MAP_FAILED)
378                 return NULL;
379         return map;
380 }
381
382 /**
383   * Return a copy of the next line in a mmap'ed file.
384   * spaces in the beginning of the line is trimmed away.
385   * Return a pointer to a static buffer.
386   **/
387 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
388 {
389         static char line[4096];
390         int skip = 1;
391         size_t len = 0;
392         signed char *p = (signed char *)file + *pos;
393         char *s = line;
394
395         for (; *pos < size ; (*pos)++) {
396                 if (skip && isspace(*p)) {
397                         p++;
398                         continue;
399                 }
400                 skip = 0;
401                 if (*p != '\n' && (*pos < size)) {
402                         len++;
403                         *s++ = *p++;
404                         if (len > 4095)
405                                 break; /* Too long, stop */
406                 } else {
407                         /* End of string */
408                         *s = '\0';
409                         return line;
410                 }
411         }
412         /* End of buffer */
413         return NULL;
414 }
415
416 void release_file(void *file, unsigned long size)
417 {
418         munmap(file, size);
419 }
420
421 static int parse_elf(struct elf_info *info, const char *filename)
422 {
423         unsigned int i;
424         Elf_Ehdr *hdr;
425         Elf_Shdr *sechdrs;
426         Elf_Sym  *sym;
427         const char *secstrings;
428         unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
429
430         hdr = grab_file(filename, &info->size);
431         if (!hdr) {
432                 if (ignore_missing_files) {
433                         fprintf(stderr, "%s: %s (ignored)\n", filename,
434                                 strerror(errno));
435                         return 0;
436                 }
437                 perror(filename);
438                 exit(1);
439         }
440         info->hdr = hdr;
441         if (info->size < sizeof(*hdr)) {
442                 /* file too small, assume this is an empty .o file */
443                 return 0;
444         }
445         /* Is this a valid ELF file? */
446         if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
447             (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
448             (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
449             (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
450                 /* Not an ELF file - silently ignore it */
451                 return 0;
452         }
453         /* Fix endianness in ELF header */
454         hdr->e_type      = TO_NATIVE(hdr->e_type);
455         hdr->e_machine   = TO_NATIVE(hdr->e_machine);
456         hdr->e_version   = TO_NATIVE(hdr->e_version);
457         hdr->e_entry     = TO_NATIVE(hdr->e_entry);
458         hdr->e_phoff     = TO_NATIVE(hdr->e_phoff);
459         hdr->e_shoff     = TO_NATIVE(hdr->e_shoff);
460         hdr->e_flags     = TO_NATIVE(hdr->e_flags);
461         hdr->e_ehsize    = TO_NATIVE(hdr->e_ehsize);
462         hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
463         hdr->e_phnum     = TO_NATIVE(hdr->e_phnum);
464         hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
465         hdr->e_shnum     = TO_NATIVE(hdr->e_shnum);
466         hdr->e_shstrndx  = TO_NATIVE(hdr->e_shstrndx);
467         sechdrs = (void *)hdr + hdr->e_shoff;
468         info->sechdrs = sechdrs;
469
470         /* Check if file offset is correct */
471         if (hdr->e_shoff > info->size) {
472                 fatal("section header offset=%lu in file '%s' is bigger than "
473                       "filesize=%lu\n", (unsigned long)hdr->e_shoff,
474                       filename, info->size);
475                 return 0;
476         }
477
478         if (hdr->e_shnum == SHN_UNDEF) {
479                 /*
480                  * There are more than 64k sections,
481                  * read count from .sh_size.
482                  */
483                 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
484         }
485         else {
486                 info->num_sections = hdr->e_shnum;
487         }
488         if (hdr->e_shstrndx == SHN_XINDEX) {
489                 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
490         }
491         else {
492                 info->secindex_strings = hdr->e_shstrndx;
493         }
494
495         /* Fix endianness in section headers */
496         for (i = 0; i < info->num_sections; i++) {
497                 sechdrs[i].sh_name      = TO_NATIVE(sechdrs[i].sh_name);
498                 sechdrs[i].sh_type      = TO_NATIVE(sechdrs[i].sh_type);
499                 sechdrs[i].sh_flags     = TO_NATIVE(sechdrs[i].sh_flags);
500                 sechdrs[i].sh_addr      = TO_NATIVE(sechdrs[i].sh_addr);
501                 sechdrs[i].sh_offset    = TO_NATIVE(sechdrs[i].sh_offset);
502                 sechdrs[i].sh_size      = TO_NATIVE(sechdrs[i].sh_size);
503                 sechdrs[i].sh_link      = TO_NATIVE(sechdrs[i].sh_link);
504                 sechdrs[i].sh_info      = TO_NATIVE(sechdrs[i].sh_info);
505                 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
506                 sechdrs[i].sh_entsize   = TO_NATIVE(sechdrs[i].sh_entsize);
507         }
508         /* Find symbol table. */
509         secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
510         for (i = 1; i < info->num_sections; i++) {
511                 const char *secname;
512                 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
513
514                 if (!nobits && sechdrs[i].sh_offset > info->size) {
515                         fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
516                               "sizeof(*hrd)=%zu\n", filename,
517                               (unsigned long)sechdrs[i].sh_offset,
518                               sizeof(*hdr));
519                         return 0;
520                 }
521                 secname = secstrings + sechdrs[i].sh_name;
522                 if (strcmp(secname, ".modinfo") == 0) {
523                         if (nobits)
524                                 fatal("%s has NOBITS .modinfo\n", filename);
525                         info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
526                         info->modinfo_len = sechdrs[i].sh_size;
527                 } else if (strcmp(secname, "__ksymtab") == 0)
528                         info->export_sec = i;
529                 else if (strcmp(secname, "__ksymtab_unused") == 0)
530                         info->export_unused_sec = i;
531                 else if (strcmp(secname, "__ksymtab_gpl") == 0)
532                         info->export_gpl_sec = i;
533                 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
534                         info->export_unused_gpl_sec = i;
535                 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
536                         info->export_gpl_future_sec = i;
537
538                 if (sechdrs[i].sh_type == SHT_SYMTAB) {
539                         unsigned int sh_link_idx;
540                         symtab_idx = i;
541                         info->symtab_start = (void *)hdr +
542                             sechdrs[i].sh_offset;
543                         info->symtab_stop  = (void *)hdr +
544                             sechdrs[i].sh_offset + sechdrs[i].sh_size;
545                         sh_link_idx = sechdrs[i].sh_link;
546                         info->strtab       = (void *)hdr +
547                             sechdrs[sh_link_idx].sh_offset;
548                 }
549
550                 /* 32bit section no. table? ("more than 64k sections") */
551                 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
552                         symtab_shndx_idx = i;
553                         info->symtab_shndx_start = (void *)hdr +
554                             sechdrs[i].sh_offset;
555                         info->symtab_shndx_stop  = (void *)hdr +
556                             sechdrs[i].sh_offset + sechdrs[i].sh_size;
557                 }
558         }
559         if (!info->symtab_start)
560                 fatal("%s has no symtab?\n", filename);
561
562         /* Fix endianness in symbols */
563         for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
564                 sym->st_shndx = TO_NATIVE(sym->st_shndx);
565                 sym->st_name  = TO_NATIVE(sym->st_name);
566                 sym->st_value = TO_NATIVE(sym->st_value);
567                 sym->st_size  = TO_NATIVE(sym->st_size);
568         }
569
570         if (symtab_shndx_idx != ~0U) {
571                 Elf32_Word *p;
572                 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
573                         fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
574                               filename, sechdrs[symtab_shndx_idx].sh_link,
575                               symtab_idx);
576                 /* Fix endianness */
577                 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
578                      p++)
579                         *p = TO_NATIVE(*p);
580         }
581
582         return 1;
583 }
584
585 static void parse_elf_finish(struct elf_info *info)
586 {
587         release_file(info->hdr, info->size);
588 }
589
590 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
591 {
592         /* ignore __this_module, it will be resolved shortly */
593         if (strcmp(symname, "__this_module") == 0)
594                 return 1;
595         /* ignore global offset table */
596         if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
597                 return 1;
598         if (info->hdr->e_machine == EM_PPC)
599                 /* Special register function linked on all modules during final link of .ko */
600                 if (strstarts(symname, "_restgpr_") ||
601                     strstarts(symname, "_savegpr_") ||
602                     strstarts(symname, "_rest32gpr_") ||
603                     strstarts(symname, "_save32gpr_") ||
604                     strstarts(symname, "_restvr_") ||
605                     strstarts(symname, "_savevr_"))
606                         return 1;
607         if (info->hdr->e_machine == EM_PPC64)
608                 /* Special register function linked on all modules during final link of .ko */
609                 if (strstarts(symname, "_restgpr0_") ||
610                     strstarts(symname, "_savegpr0_") ||
611                     strstarts(symname, "_restvr_") ||
612                     strstarts(symname, "_savevr_") ||
613                     strcmp(symname, ".TOC.") == 0)
614                         return 1;
615         /* Do not ignore this symbol */
616         return 0;
617 }
618
619 static void handle_modversions(struct module *mod, struct elf_info *info,
620                                Elf_Sym *sym, const char *symname)
621 {
622         unsigned int crc;
623         enum export export;
624         bool is_crc = false;
625
626         if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
627             strstarts(symname, "__ksymtab"))
628                 export = export_from_secname(info, get_secindex(info, sym));
629         else
630                 export = export_from_sec(info, get_secindex(info, sym));
631
632         /* CRC'd symbol */
633         if (strstarts(symname, "__crc_")) {
634                 is_crc = true;
635                 crc = (unsigned int) sym->st_value;
636                 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
637                         unsigned int *crcp;
638
639                         /* symbol points to the CRC in the ELF object */
640                         crcp = (void *)info->hdr + sym->st_value +
641                                info->sechdrs[sym->st_shndx].sh_offset -
642                                (info->hdr->e_type != ET_REL ?
643                                 info->sechdrs[sym->st_shndx].sh_addr : 0);
644                         crc = TO_NATIVE(*crcp);
645                 }
646                 sym_update_crc(symname + strlen("__crc_"), mod, crc,
647                                 export);
648         }
649
650         switch (sym->st_shndx) {
651         case SHN_COMMON:
652                 if (strstarts(symname, "__gnu_lto_")) {
653                         /* Should warn here, but modpost runs before the linker */
654                 } else
655                         warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
656                 break;
657         case SHN_UNDEF:
658                 /* undefined symbol */
659                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
660                     ELF_ST_BIND(sym->st_info) != STB_WEAK)
661                         break;
662                 if (ignore_undef_symbol(info, symname))
663                         break;
664 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
665 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
666 /* add compatibility with older glibc */
667 #ifndef STT_SPARC_REGISTER
668 #define STT_SPARC_REGISTER STT_REGISTER
669 #endif
670                 if (info->hdr->e_machine == EM_SPARC ||
671                     info->hdr->e_machine == EM_SPARCV9) {
672                         /* Ignore register directives. */
673                         if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
674                                 break;
675                         if (symname[0] == '.') {
676                                 char *munged = NOFAIL(strdup(symname));
677                                 munged[0] = '_';
678                                 munged[1] = toupper(munged[1]);
679                                 symname = munged;
680                         }
681                 }
682 #endif
683
684                 if (is_crc) {
685                         const char *e = is_vmlinux(mod->name) ?"":".ko";
686                         warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
687                              symname + strlen("__crc_"), mod->name, e);
688                 }
689                 mod->unres = alloc_symbol(symname,
690                                           ELF_ST_BIND(sym->st_info) == STB_WEAK,
691                                           mod->unres);
692                 break;
693         default:
694                 /* All exported symbols */
695                 if (strstarts(symname, "__ksymtab_")) {
696                         sym_add_exported(symname + strlen("__ksymtab_"), mod,
697                                         export);
698                 }
699                 if (strcmp(symname, "init_module") == 0)
700                         mod->has_init = 1;
701                 if (strcmp(symname, "cleanup_module") == 0)
702                         mod->has_cleanup = 1;
703                 break;
704         }
705 }
706
707 /**
708  * Parse tag=value strings from .modinfo section
709  **/
710 static char *next_string(char *string, unsigned long *secsize)
711 {
712         /* Skip non-zero chars */
713         while (string[0]) {
714                 string++;
715                 if ((*secsize)-- <= 1)
716                         return NULL;
717         }
718
719         /* Skip any zero padding. */
720         while (!string[0]) {
721                 string++;
722                 if ((*secsize)-- <= 1)
723                         return NULL;
724         }
725         return string;
726 }
727
728 static char *get_next_modinfo(struct elf_info *info, const char *tag,
729                               char *prev)
730 {
731         char *p;
732         unsigned int taglen = strlen(tag);
733         char *modinfo = info->modinfo;
734         unsigned long size = info->modinfo_len;
735
736         if (prev) {
737                 size -= prev - modinfo;
738                 modinfo = next_string(prev, &size);
739         }
740
741         for (p = modinfo; p; p = next_string(p, &size)) {
742                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
743                         return p + taglen + 1;
744         }
745         return NULL;
746 }
747
748 static char *get_modinfo(struct elf_info *info, const char *tag)
749
750 {
751         return get_next_modinfo(info, tag, NULL);
752 }
753
754 /**
755  * Test if string s ends in string sub
756  * return 0 if match
757  **/
758 static int strrcmp(const char *s, const char *sub)
759 {
760         int slen, sublen;
761
762         if (!s || !sub)
763                 return 1;
764
765         slen = strlen(s);
766         sublen = strlen(sub);
767
768         if ((slen == 0) || (sublen == 0))
769                 return 1;
770
771         if (sublen > slen)
772                 return 1;
773
774         return memcmp(s + slen - sublen, sub, sublen);
775 }
776
777 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
778 {
779         if (sym)
780                 return elf->strtab + sym->st_name;
781         else
782                 return "(unknown)";
783 }
784
785 /* The pattern is an array of simple patterns.
786  * "foo" will match an exact string equal to "foo"
787  * "*foo" will match a string that ends with "foo"
788  * "foo*" will match a string that begins with "foo"
789  * "*foo*" will match a string that contains "foo"
790  */
791 static int match(const char *sym, const char * const pat[])
792 {
793         const char *p;
794         while (*pat) {
795                 p = *pat++;
796                 const char *endp = p + strlen(p) - 1;
797
798                 /* "*foo*" */
799                 if (*p == '*' && *endp == '*') {
800                         char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
801                         char *here = strstr(sym, bare);
802
803                         free(bare);
804                         if (here != NULL)
805                                 return 1;
806                 }
807                 /* "*foo" */
808                 else if (*p == '*') {
809                         if (strrcmp(sym, p + 1) == 0)
810                                 return 1;
811                 }
812                 /* "foo*" */
813                 else if (*endp == '*') {
814                         if (strncmp(sym, p, strlen(p) - 1) == 0)
815                                 return 1;
816                 }
817                 /* no wildcards */
818                 else {
819                         if (strcmp(p, sym) == 0)
820                                 return 1;
821                 }
822         }
823         /* no match */
824         return 0;
825 }
826
827 /* sections that we do not want to do full section mismatch check on */
828 static const char *const section_white_list[] =
829 {
830         ".comment*",
831         ".debug*",
832         ".cranges",             /* sh64 */
833         ".zdebug*",             /* Compressed debug sections. */
834         ".GCC.command.line",    /* record-gcc-switches */
835         ".mdebug*",        /* alpha, score, mips etc. */
836         ".pdr",            /* alpha, score, mips etc. */
837         ".stab*",
838         ".note*",
839         ".got*",
840         ".toc*",
841         ".xt.prop",                              /* xtensa */
842         ".xt.lit",         /* xtensa */
843         ".arcextmap*",                  /* arc */
844         ".gnu.linkonce.arcext*",        /* arc : modules */
845         ".cmem*",                       /* EZchip */
846         ".fmt_slot*",                   /* EZchip */
847         ".gnu.lto*",
848         ".discard.*",
849         NULL
850 };
851
852 /*
853  * This is used to find sections missing the SHF_ALLOC flag.
854  * The cause of this is often a section specified in assembler
855  * without "ax" / "aw".
856  */
857 static void check_section(const char *modname, struct elf_info *elf,
858                           Elf_Shdr *sechdr)
859 {
860         const char *sec = sech_name(elf, sechdr);
861
862         if (sechdr->sh_type == SHT_PROGBITS &&
863             !(sechdr->sh_flags & SHF_ALLOC) &&
864             !match(sec, section_white_list)) {
865                 warn("%s (%s): unexpected non-allocatable section.\n"
866                      "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
867                      "Note that for example <linux/init.h> contains\n"
868                      "section definitions for use in .S files.\n\n",
869                      modname, sec);
870         }
871 }
872
873
874
875 #define ALL_INIT_DATA_SECTIONS \
876         ".init.setup", ".init.rodata", ".meminit.rodata", \
877         ".init.data", ".meminit.data"
878 #define ALL_EXIT_DATA_SECTIONS \
879         ".exit.data", ".memexit.data"
880
881 #define ALL_INIT_TEXT_SECTIONS \
882         ".init.text", ".meminit.text"
883 #define ALL_EXIT_TEXT_SECTIONS \
884         ".exit.text", ".memexit.text"
885
886 #define ALL_PCI_INIT_SECTIONS   \
887         ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
888         ".pci_fixup_enable", ".pci_fixup_resume", \
889         ".pci_fixup_resume_early", ".pci_fixup_suspend"
890
891 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
892 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
893
894 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
895 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
896
897 #define DATA_SECTIONS ".data", ".data.rel"
898 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
899                 ".kprobes.text", ".cpuidle.text"
900 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
901                 ".fixup", ".entry.text", ".exception.text", ".text.*", \
902                 ".coldtext"
903
904 #define INIT_SECTIONS      ".init.*"
905 #define MEM_INIT_SECTIONS  ".meminit.*"
906
907 #define EXIT_SECTIONS      ".exit.*"
908 #define MEM_EXIT_SECTIONS  ".memexit.*"
909
910 #define ALL_TEXT_SECTIONS  ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
911                 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
912
913 /* init data sections */
914 static const char *const init_data_sections[] =
915         { ALL_INIT_DATA_SECTIONS, NULL };
916
917 /* all init sections */
918 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
919
920 /* All init and exit sections (code + data) */
921 static const char *const init_exit_sections[] =
922         {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
923
924 /* all text sections */
925 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
926
927 /* data section */
928 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
929
930
931 /* symbols in .data that may refer to init/exit sections */
932 #define DEFAULT_SYMBOL_WHITE_LIST                                       \
933         "*driver",                                                      \
934         "*_template", /* scsi uses *_template a lot */                  \
935         "*_timer",    /* arm uses ops structures named _timer a lot */  \
936         "*_sht",      /* scsi also used *_sht to some extent */         \
937         "*_ops",                                                        \
938         "*_probe",                                                      \
939         "*_probe_one",                                                  \
940         "*_console"
941
942 static const char *const head_sections[] = { ".head.text*", NULL };
943 static const char *const linker_symbols[] =
944         { "__init_begin", "_sinittext", "_einittext", NULL };
945 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
946
947 enum mismatch {
948         TEXT_TO_ANY_INIT,
949         DATA_TO_ANY_INIT,
950         TEXT_TO_ANY_EXIT,
951         DATA_TO_ANY_EXIT,
952         XXXINIT_TO_SOME_INIT,
953         XXXEXIT_TO_SOME_EXIT,
954         ANY_INIT_TO_ANY_EXIT,
955         ANY_EXIT_TO_ANY_INIT,
956         EXPORT_TO_INIT_EXIT,
957         EXTABLE_TO_NON_TEXT,
958 };
959
960 /**
961  * Describe how to match sections on different criterias:
962  *
963  * @fromsec: Array of sections to be matched.
964  *
965  * @bad_tosec: Relocations applied to a section in @fromsec to a section in
966  * this array is forbidden (black-list).  Can be empty.
967  *
968  * @good_tosec: Relocations applied to a section in @fromsec must be
969  * targetting sections in this array (white-list).  Can be empty.
970  *
971  * @mismatch: Type of mismatch.
972  *
973  * @symbol_white_list: Do not match a relocation to a symbol in this list
974  * even if it is targetting a section in @bad_to_sec.
975  *
976  * @handler: Specific handler to call when a match is found.  If NULL,
977  * default_mismatch_handler() will be called.
978  *
979  */
980 struct sectioncheck {
981         const char *fromsec[20];
982         const char *bad_tosec[20];
983         const char *good_tosec[20];
984         enum mismatch mismatch;
985         const char *symbol_white_list[20];
986         void (*handler)(const char *modname, struct elf_info *elf,
987                         const struct sectioncheck* const mismatch,
988                         Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
989
990 };
991
992 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
993                                      const struct sectioncheck* const mismatch,
994                                      Elf_Rela *r, Elf_Sym *sym,
995                                      const char *fromsec);
996
997 static const struct sectioncheck sectioncheck[] = {
998 /* Do not reference init/exit code/data from
999  * normal code and data
1000  */
1001 {
1002         .fromsec = { TEXT_SECTIONS, NULL },
1003         .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1004         .mismatch = TEXT_TO_ANY_INIT,
1005         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1006 },
1007 {
1008         .fromsec = { DATA_SECTIONS, NULL },
1009         .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
1010         .mismatch = DATA_TO_ANY_INIT,
1011         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1012 },
1013 {
1014         .fromsec = { DATA_SECTIONS, NULL },
1015         .bad_tosec = { INIT_SECTIONS, NULL },
1016         .mismatch = DATA_TO_ANY_INIT,
1017         .symbol_white_list = {
1018                 "*_template", "*_timer", "*_sht", "*_ops",
1019                 "*_probe", "*_probe_one", "*_console", NULL
1020         },
1021 },
1022 {
1023         .fromsec = { TEXT_SECTIONS, NULL },
1024         .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1025         .mismatch = TEXT_TO_ANY_EXIT,
1026         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1027 },
1028 {
1029         .fromsec = { DATA_SECTIONS, NULL },
1030         .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1031         .mismatch = DATA_TO_ANY_EXIT,
1032         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1033 },
1034 /* Do not reference init code/data from meminit code/data */
1035 {
1036         .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
1037         .bad_tosec = { INIT_SECTIONS, NULL },
1038         .mismatch = XXXINIT_TO_SOME_INIT,
1039         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1040 },
1041 /* Do not reference exit code/data from memexit code/data */
1042 {
1043         .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
1044         .bad_tosec = { EXIT_SECTIONS, NULL },
1045         .mismatch = XXXEXIT_TO_SOME_EXIT,
1046         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1047 },
1048 /* Do not use exit code/data from init code */
1049 {
1050         .fromsec = { ALL_INIT_SECTIONS, NULL },
1051         .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
1052         .mismatch = ANY_INIT_TO_ANY_EXIT,
1053         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1054 },
1055 /* Do not use init code/data from exit code */
1056 {
1057         .fromsec = { ALL_EXIT_SECTIONS, NULL },
1058         .bad_tosec = { ALL_INIT_SECTIONS, NULL },
1059         .mismatch = ANY_EXIT_TO_ANY_INIT,
1060         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1061 },
1062 {
1063         .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1064         .bad_tosec = { INIT_SECTIONS, NULL },
1065         .mismatch = ANY_INIT_TO_ANY_EXIT,
1066         .symbol_white_list = { NULL },
1067 },
1068 /* Do not export init/exit functions or data */
1069 {
1070         .fromsec = { "__ksymtab*", NULL },
1071         .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
1072         .mismatch = EXPORT_TO_INIT_EXIT,
1073         .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
1074 },
1075 {
1076         .fromsec = { "__ex_table", NULL },
1077         /* If you're adding any new black-listed sections in here, consider
1078          * adding a special 'printer' for them in scripts/check_extable.
1079          */
1080         .bad_tosec = { ".altinstr_replacement", NULL },
1081         .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1082         .mismatch = EXTABLE_TO_NON_TEXT,
1083         .handler = extable_mismatch_handler,
1084 }
1085 };
1086
1087 static const struct sectioncheck *section_mismatch(
1088                 const char *fromsec, const char *tosec)
1089 {
1090         int i;
1091         int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1092         const struct sectioncheck *check = &sectioncheck[0];
1093
1094         /*
1095          * The target section could be the SHT_NUL section when we're
1096          * handling relocations to un-resolved symbols, trying to match it
1097          * doesn't make much sense and causes build failures on parisc
1098          * architectures.
1099          */
1100         if (*tosec == '\0')
1101                 return NULL;
1102
1103         for (i = 0; i < elems; i++) {
1104                 if (match(fromsec, check->fromsec)) {
1105                         if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1106                                 return check;
1107                         if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1108                                 return check;
1109                 }
1110                 check++;
1111         }
1112         return NULL;
1113 }
1114
1115 /**
1116  * Whitelist to allow certain references to pass with no warning.
1117  *
1118  * Pattern 1:
1119  *   If a module parameter is declared __initdata and permissions=0
1120  *   then this is legal despite the warning generated.
1121  *   We cannot see value of permissions here, so just ignore
1122  *   this pattern.
1123  *   The pattern is identified by:
1124  *   tosec   = .init.data
1125  *   fromsec = .data*
1126  *   atsym   =__param*
1127  *
1128  * Pattern 1a:
1129  *   module_param_call() ops can refer to __init set function if permissions=0
1130  *   The pattern is identified by:
1131  *   tosec   = .init.text
1132  *   fromsec = .data*
1133  *   atsym   = __param_ops_*
1134  *
1135  * Pattern 2:
1136  *   Many drivers utilise a *driver container with references to
1137  *   add, remove, probe functions etc.
1138  *   the pattern is identified by:
1139  *   tosec   = init or exit section
1140  *   fromsec = data section
1141  *   atsym = *driver, *_template, *_sht, *_ops, *_probe,
1142  *           *probe_one, *_console, *_timer
1143  *
1144  * Pattern 3:
1145  *   Whitelist all references from .head.text to any init section
1146  *
1147  * Pattern 4:
1148  *   Some symbols belong to init section but still it is ok to reference
1149  *   these from non-init sections as these symbols don't have any memory
1150  *   allocated for them and symbol address and value are same. So even
1151  *   if init section is freed, its ok to reference those symbols.
1152  *   For ex. symbols marking the init section boundaries.
1153  *   This pattern is identified by
1154  *   refsymname = __init_begin, _sinittext, _einittext
1155  *
1156  * Pattern 5:
1157  *   GCC may optimize static inlines when fed constant arg(s) resulting
1158  *   in functions like cpumask_empty() -- generating an associated symbol
1159  *   cpumask_empty.constprop.3 that appears in the audit.  If the const that
1160  *   is passed in comes from __init, like say nmi_ipi_mask, we get a
1161  *   meaningless section warning.  May need to add isra symbols too...
1162  *   This pattern is identified by
1163  *   tosec   = init section
1164  *   fromsec = text section
1165  *   refsymname = *.constprop.*
1166  *
1167  * Pattern 6:
1168  *   Hide section mismatch warnings for ELF local symbols.  The goal
1169  *   is to eliminate false positive modpost warnings caused by
1170  *   compiler-generated ELF local symbol names such as ".LANCHOR1".
1171  *   Autogenerated symbol names bypass modpost's "Pattern 2"
1172  *   whitelisting, which relies on pattern-matching against symbol
1173  *   names to work.  (One situation where gcc can autogenerate ELF
1174  *   local symbols is when "-fsection-anchors" is used.)
1175  **/
1176 static int secref_whitelist(const struct sectioncheck *mismatch,
1177                             const char *fromsec, const char *fromsym,
1178                             const char *tosec, const char *tosym)
1179 {
1180         /* Check for pattern 1 */
1181         if (match(tosec, init_data_sections) &&
1182             match(fromsec, data_sections) &&
1183             strstarts(fromsym, "__param"))
1184                 return 0;
1185
1186         /* Check for pattern 1a */
1187         if (strcmp(tosec, ".init.text") == 0 &&
1188             match(fromsec, data_sections) &&
1189             strstarts(fromsym, "__param_ops_"))
1190                 return 0;
1191
1192         /* Check for pattern 2 */
1193         if (match(tosec, init_exit_sections) &&
1194             match(fromsec, data_sections) &&
1195             match(fromsym, mismatch->symbol_white_list))
1196                 return 0;
1197
1198         /* Check for pattern 3 */
1199         if (match(fromsec, head_sections) &&
1200             match(tosec, init_sections))
1201                 return 0;
1202
1203         /* Check for pattern 4 */
1204         if (match(tosym, linker_symbols))
1205                 return 0;
1206
1207         /* Check for pattern 5 */
1208         if (match(fromsec, text_sections) &&
1209             match(tosec, init_sections) &&
1210             match(fromsym, optim_symbols))
1211                 return 0;
1212
1213         /* Check for pattern 6 */
1214         if (strstarts(fromsym, ".L"))
1215                 return 0;
1216
1217         return 1;
1218 }
1219
1220 static inline int is_arm_mapping_symbol(const char *str)
1221 {
1222         return str[0] == '$' && strchr("axtd", str[1])
1223                && (str[2] == '\0' || str[2] == '.');
1224 }
1225
1226 /*
1227  * If there's no name there, ignore it; likewise, ignore it if it's
1228  * one of the magic symbols emitted used by current ARM tools.
1229  *
1230  * Otherwise if find_symbols_between() returns those symbols, they'll
1231  * fail the whitelist tests and cause lots of false alarms ... fixable
1232  * only by merging __exit and __init sections into __text, bloating
1233  * the kernel (which is especially evil on embedded platforms).
1234  */
1235 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1236 {
1237         const char *name = elf->strtab + sym->st_name;
1238
1239         if (!name || !strlen(name))
1240                 return 0;
1241         return !is_arm_mapping_symbol(name);
1242 }
1243
1244 /**
1245  * Find symbol based on relocation record info.
1246  * In some cases the symbol supplied is a valid symbol so
1247  * return refsym. If st_name != 0 we assume this is a valid symbol.
1248  * In other cases the symbol needs to be looked up in the symbol table
1249  * based on section and address.
1250  *  **/
1251 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1252                                 Elf_Sym *relsym)
1253 {
1254         Elf_Sym *sym;
1255         Elf_Sym *near = NULL;
1256         Elf64_Sword distance = 20;
1257         Elf64_Sword d;
1258         unsigned int relsym_secindex;
1259
1260         if (relsym->st_name != 0)
1261                 return relsym;
1262
1263         relsym_secindex = get_secindex(elf, relsym);
1264         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1265                 if (get_secindex(elf, sym) != relsym_secindex)
1266                         continue;
1267                 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1268                         continue;
1269                 if (!is_valid_name(elf, sym))
1270                         continue;
1271                 if (sym->st_value == addr)
1272                         return sym;
1273                 /* Find a symbol nearby - addr are maybe negative */
1274                 d = sym->st_value - addr;
1275                 if (d < 0)
1276                         d = addr - sym->st_value;
1277                 if (d < distance) {
1278                         distance = d;
1279                         near = sym;
1280                 }
1281         }
1282         /* We need a close match */
1283         if (distance < 20)
1284                 return near;
1285         else
1286                 return NULL;
1287 }
1288
1289 /*
1290  * Find symbols before or equal addr and after addr - in the section sec.
1291  * If we find two symbols with equal offset prefer one with a valid name.
1292  * The ELF format may have a better way to detect what type of symbol
1293  * it is, but this works for now.
1294  **/
1295 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1296                                  const char *sec)
1297 {
1298         Elf_Sym *sym;
1299         Elf_Sym *near = NULL;
1300         Elf_Addr distance = ~0;
1301
1302         for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1303                 const char *symsec;
1304
1305                 if (is_shndx_special(sym->st_shndx))
1306                         continue;
1307                 symsec = sec_name(elf, get_secindex(elf, sym));
1308                 if (strcmp(symsec, sec) != 0)
1309                         continue;
1310                 if (!is_valid_name(elf, sym))
1311                         continue;
1312                 if (sym->st_value <= addr) {
1313                         if ((addr - sym->st_value) < distance) {
1314                                 distance = addr - sym->st_value;
1315                                 near = sym;
1316                         } else if ((addr - sym->st_value) == distance) {
1317                                 near = sym;
1318                         }
1319                 }
1320         }
1321         return near;
1322 }
1323
1324 /*
1325  * Convert a section name to the function/data attribute
1326  * .init.text => __init
1327  * .memexitconst => __memconst
1328  * etc.
1329  *
1330  * The memory of returned value has been allocated on a heap. The user of this
1331  * method should free it after usage.
1332 */
1333 static char *sec2annotation(const char *s)
1334 {
1335         if (match(s, init_exit_sections)) {
1336                 char *p = NOFAIL(malloc(20));
1337                 char *r = p;
1338
1339                 *p++ = '_';
1340                 *p++ = '_';
1341                 if (*s == '.')
1342                         s++;
1343                 while (*s && *s != '.')
1344                         *p++ = *s++;
1345                 *p = '\0';
1346                 if (*s == '.')
1347                         s++;
1348                 if (strstr(s, "rodata") != NULL)
1349                         strcat(p, "const ");
1350                 else if (strstr(s, "data") != NULL)
1351                         strcat(p, "data ");
1352                 else
1353                         strcat(p, " ");
1354                 return r;
1355         } else {
1356                 return NOFAIL(strdup(""));
1357         }
1358 }
1359
1360 static int is_function(Elf_Sym *sym)
1361 {
1362         if (sym)
1363                 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1364         else
1365                 return -1;
1366 }
1367
1368 static void print_section_list(const char * const list[20])
1369 {
1370         const char *const *s = list;
1371
1372         while (*s) {
1373                 fprintf(stderr, "%s", *s);
1374                 s++;
1375                 if (*s)
1376                         fprintf(stderr, ", ");
1377         }
1378         fprintf(stderr, "\n");
1379 }
1380
1381 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1382 {
1383         switch (is_func) {
1384         case 0: *name = "variable"; *name_p = ""; break;
1385         case 1: *name = "function"; *name_p = "()"; break;
1386         default: *name = "(unknown reference)"; *name_p = ""; break;
1387         }
1388 }
1389
1390 /*
1391  * Print a warning about a section mismatch.
1392  * Try to find symbols near it so user can find it.
1393  * Check whitelist before warning - it may be a false positive.
1394  */
1395 static void report_sec_mismatch(const char *modname,
1396                                 const struct sectioncheck *mismatch,
1397                                 const char *fromsec,
1398                                 unsigned long long fromaddr,
1399                                 const char *fromsym,
1400                                 int from_is_func,
1401                                 const char *tosec, const char *tosym,
1402                                 int to_is_func)
1403 {
1404         const char *from, *from_p;
1405         const char *to, *to_p;
1406         char *prl_from;
1407         char *prl_to;
1408
1409         sec_mismatch_count++;
1410
1411         get_pretty_name(from_is_func, &from, &from_p);
1412         get_pretty_name(to_is_func, &to, &to_p);
1413
1414         warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1415              "to the %s %s:%s%s\n",
1416              modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1417              tosym, to_p);
1418
1419         switch (mismatch->mismatch) {
1420         case TEXT_TO_ANY_INIT:
1421                 prl_from = sec2annotation(fromsec);
1422                 prl_to = sec2annotation(tosec);
1423                 fprintf(stderr,
1424                 "The function %s%s() references\n"
1425                 "the %s %s%s%s.\n"
1426                 "This is often because %s lacks a %s\n"
1427                 "annotation or the annotation of %s is wrong.\n",
1428                 prl_from, fromsym,
1429                 to, prl_to, tosym, to_p,
1430                 fromsym, prl_to, tosym);
1431                 free(prl_from);
1432                 free(prl_to);
1433                 break;
1434         case DATA_TO_ANY_INIT: {
1435                 prl_to = sec2annotation(tosec);
1436                 fprintf(stderr,
1437                 "The variable %s references\n"
1438                 "the %s %s%s%s\n"
1439                 "If the reference is valid then annotate the\n"
1440                 "variable with __init* or __refdata (see linux/init.h) "
1441                 "or name the variable:\n",
1442                 fromsym, to, prl_to, tosym, to_p);
1443                 print_section_list(mismatch->symbol_white_list);
1444                 free(prl_to);
1445                 break;
1446         }
1447         case TEXT_TO_ANY_EXIT:
1448                 prl_to = sec2annotation(tosec);
1449                 fprintf(stderr,
1450                 "The function %s() references a %s in an exit section.\n"
1451                 "Often the %s %s%s has valid usage outside the exit section\n"
1452                 "and the fix is to remove the %sannotation of %s.\n",
1453                 fromsym, to, to, tosym, to_p, prl_to, tosym);
1454                 free(prl_to);
1455                 break;
1456         case DATA_TO_ANY_EXIT: {
1457                 prl_to = sec2annotation(tosec);
1458                 fprintf(stderr,
1459                 "The variable %s references\n"
1460                 "the %s %s%s%s\n"
1461                 "If the reference is valid then annotate the\n"
1462                 "variable with __exit* (see linux/init.h) or "
1463                 "name the variable:\n",
1464                 fromsym, to, prl_to, tosym, to_p);
1465                 print_section_list(mismatch->symbol_white_list);
1466                 free(prl_to);
1467                 break;
1468         }
1469         case XXXINIT_TO_SOME_INIT:
1470         case XXXEXIT_TO_SOME_EXIT:
1471                 prl_from = sec2annotation(fromsec);
1472                 prl_to = sec2annotation(tosec);
1473                 fprintf(stderr,
1474                 "The %s %s%s%s references\n"
1475                 "a %s %s%s%s.\n"
1476                 "If %s is only used by %s then\n"
1477                 "annotate %s with a matching annotation.\n",
1478                 from, prl_from, fromsym, from_p,
1479                 to, prl_to, tosym, to_p,
1480                 tosym, fromsym, tosym);
1481                 free(prl_from);
1482                 free(prl_to);
1483                 break;
1484         case ANY_INIT_TO_ANY_EXIT:
1485                 prl_from = sec2annotation(fromsec);
1486                 prl_to = sec2annotation(tosec);
1487                 fprintf(stderr,
1488                 "The %s %s%s%s references\n"
1489                 "a %s %s%s%s.\n"
1490                 "This is often seen when error handling "
1491                 "in the init function\n"
1492                 "uses functionality in the exit path.\n"
1493                 "The fix is often to remove the %sannotation of\n"
1494                 "%s%s so it may be used outside an exit section.\n",
1495                 from, prl_from, fromsym, from_p,
1496                 to, prl_to, tosym, to_p,
1497                 prl_to, tosym, to_p);
1498                 free(prl_from);
1499                 free(prl_to);
1500                 break;
1501         case ANY_EXIT_TO_ANY_INIT:
1502                 prl_from = sec2annotation(fromsec);
1503                 prl_to = sec2annotation(tosec);
1504                 fprintf(stderr,
1505                 "The %s %s%s%s references\n"
1506                 "a %s %s%s%s.\n"
1507                 "This is often seen when error handling "
1508                 "in the exit function\n"
1509                 "uses functionality in the init path.\n"
1510                 "The fix is often to remove the %sannotation of\n"
1511                 "%s%s so it may be used outside an init section.\n",
1512                 from, prl_from, fromsym, from_p,
1513                 to, prl_to, tosym, to_p,
1514                 prl_to, tosym, to_p);
1515                 free(prl_from);
1516                 free(prl_to);
1517                 break;
1518         case EXPORT_TO_INIT_EXIT:
1519                 prl_to = sec2annotation(tosec);
1520                 fprintf(stderr,
1521                 "The symbol %s is exported and annotated %s\n"
1522                 "Fix this by removing the %sannotation of %s "
1523                 "or drop the export.\n",
1524                 tosym, prl_to, prl_to, tosym);
1525                 free(prl_to);
1526                 break;
1527         case EXTABLE_TO_NON_TEXT:
1528                 fatal("There's a special handler for this mismatch type, "
1529                       "we should never get here.");
1530                 break;
1531         }
1532         fprintf(stderr, "\n");
1533 }
1534
1535 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1536                                      const struct sectioncheck* const mismatch,
1537                                      Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1538 {
1539         const char *tosec;
1540         Elf_Sym *to;
1541         Elf_Sym *from;
1542         const char *tosym;
1543         const char *fromsym;
1544
1545         from = find_elf_symbol2(elf, r->r_offset, fromsec);
1546         fromsym = sym_name(elf, from);
1547
1548         if (strstarts(fromsym, "reference___initcall"))
1549                 return;
1550
1551         tosec = sec_name(elf, get_secindex(elf, sym));
1552         to = find_elf_symbol(elf, r->r_addend, sym);
1553         tosym = sym_name(elf, to);
1554
1555         /* check whitelist - we may ignore it */
1556         if (secref_whitelist(mismatch,
1557                              fromsec, fromsym, tosec, tosym)) {
1558                 report_sec_mismatch(modname, mismatch,
1559                                     fromsec, r->r_offset, fromsym,
1560                                     is_function(from), tosec, tosym,
1561                                     is_function(to));
1562         }
1563 }
1564
1565 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1566 {
1567         if (section_index > elf->num_sections)
1568                 fatal("section_index is outside elf->num_sections!\n");
1569
1570         return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1571 }
1572
1573 /*
1574  * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1575  * to know the sizeof(struct exception_table_entry) for the target architecture.
1576  */
1577 static unsigned int extable_entry_size = 0;
1578 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1579 {
1580         /*
1581          * If we're currently checking the second relocation within __ex_table,
1582          * that relocation offset tells us the offsetof(struct
1583          * exception_table_entry, fixup) which is equal to sizeof(struct
1584          * exception_table_entry) divided by two.  We use that to our advantage
1585          * since there's no portable way to get that size as every architecture
1586          * seems to go with different sized types.  Not pretty but better than
1587          * hard-coding the size for every architecture..
1588          */
1589         if (!extable_entry_size)
1590                 extable_entry_size = r->r_offset * 2;
1591 }
1592
1593 static inline bool is_extable_fault_address(Elf_Rela *r)
1594 {
1595         /*
1596          * extable_entry_size is only discovered after we've handled the
1597          * _second_ relocation in __ex_table, so only abort when we're not
1598          * handling the first reloc and extable_entry_size is zero.
1599          */
1600         if (r->r_offset && extable_entry_size == 0)
1601                 fatal("extable_entry size hasn't been discovered!\n");
1602
1603         return ((r->r_offset == 0) ||
1604                 (r->r_offset % extable_entry_size == 0));
1605 }
1606
1607 #define is_second_extable_reloc(Start, Cur, Sec)                        \
1608         (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1609
1610 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1611                                     const struct sectioncheck* const mismatch,
1612                                     Elf_Rela* r, Elf_Sym* sym,
1613                                     const char* fromsec, const char* tosec)
1614 {
1615         Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1616         const char* fromsym_name = sym_name(elf, fromsym);
1617         Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1618         const char* tosym_name = sym_name(elf, tosym);
1619         const char* from_pretty_name;
1620         const char* from_pretty_name_p;
1621         const char* to_pretty_name;
1622         const char* to_pretty_name_p;
1623
1624         get_pretty_name(is_function(fromsym),
1625                         &from_pretty_name, &from_pretty_name_p);
1626         get_pretty_name(is_function(tosym),
1627                         &to_pretty_name, &to_pretty_name_p);
1628
1629         warn("%s(%s+0x%lx): Section mismatch in reference"
1630              " from the %s %s%s to the %s %s:%s%s\n",
1631              modname, fromsec, (long)r->r_offset, from_pretty_name,
1632              fromsym_name, from_pretty_name_p,
1633              to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1634
1635         if (!match(tosec, mismatch->bad_tosec) &&
1636             is_executable_section(elf, get_secindex(elf, sym)))
1637                 fprintf(stderr,
1638                         "The relocation at %s+0x%lx references\n"
1639                         "section \"%s\" which is not in the list of\n"
1640                         "authorized sections.  If you're adding a new section\n"
1641                         "and/or if this reference is valid, add \"%s\" to the\n"
1642                         "list of authorized sections to jump to on fault.\n"
1643                         "This can be achieved by adding \"%s\" to \n"
1644                         "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1645                         fromsec, (long)r->r_offset, tosec, tosec, tosec);
1646 }
1647
1648 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1649                                      const struct sectioncheck* const mismatch,
1650                                      Elf_Rela* r, Elf_Sym* sym,
1651                                      const char *fromsec)
1652 {
1653         const char* tosec = sec_name(elf, get_secindex(elf, sym));
1654
1655         sec_mismatch_count++;
1656
1657         report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1658
1659         if (match(tosec, mismatch->bad_tosec))
1660                 fatal("The relocation at %s+0x%lx references\n"
1661                       "section \"%s\" which is black-listed.\n"
1662                       "Something is seriously wrong and should be fixed.\n"
1663                       "You might get more information about where this is\n"
1664                       "coming from by using scripts/check_extable.sh %s\n",
1665                       fromsec, (long)r->r_offset, tosec, modname);
1666         else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1667                 if (is_extable_fault_address(r))
1668                         fatal("The relocation at %s+0x%lx references\n"
1669                               "section \"%s\" which is not executable, IOW\n"
1670                               "it is not possible for the kernel to fault\n"
1671                               "at that address.  Something is seriously wrong\n"
1672                               "and should be fixed.\n",
1673                               fromsec, (long)r->r_offset, tosec);
1674                 else
1675                         fatal("The relocation at %s+0x%lx references\n"
1676                               "section \"%s\" which is not executable, IOW\n"
1677                               "the kernel will fault if it ever tries to\n"
1678                               "jump to it.  Something is seriously wrong\n"
1679                               "and should be fixed.\n",
1680                               fromsec, (long)r->r_offset, tosec);
1681         }
1682 }
1683
1684 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1685                                    Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1686 {
1687         const char *tosec = sec_name(elf, get_secindex(elf, sym));
1688         const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1689
1690         if (mismatch) {
1691                 if (mismatch->handler)
1692                         mismatch->handler(modname, elf,  mismatch,
1693                                           r, sym, fromsec);
1694                 else
1695                         default_mismatch_handler(modname, elf, mismatch,
1696                                                  r, sym, fromsec);
1697         }
1698 }
1699
1700 static unsigned int *reloc_location(struct elf_info *elf,
1701                                     Elf_Shdr *sechdr, Elf_Rela *r)
1702 {
1703         Elf_Shdr *sechdrs = elf->sechdrs;
1704         int section = sechdr->sh_info;
1705
1706         return (void *)elf->hdr + sechdrs[section].sh_offset +
1707                 r->r_offset;
1708 }
1709
1710 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1711 {
1712         unsigned int r_typ = ELF_R_TYPE(r->r_info);
1713         unsigned int *location = reloc_location(elf, sechdr, r);
1714
1715         switch (r_typ) {
1716         case R_386_32:
1717                 r->r_addend = TO_NATIVE(*location);
1718                 break;
1719         case R_386_PC32:
1720                 r->r_addend = TO_NATIVE(*location) + 4;
1721                 /* For CONFIG_RELOCATABLE=y */
1722                 if (elf->hdr->e_type == ET_EXEC)
1723                         r->r_addend += r->r_offset;
1724                 break;
1725         }
1726         return 0;
1727 }
1728
1729 #ifndef R_ARM_CALL
1730 #define R_ARM_CALL      28
1731 #endif
1732 #ifndef R_ARM_JUMP24
1733 #define R_ARM_JUMP24    29
1734 #endif
1735
1736 #ifndef R_ARM_THM_CALL
1737 #define R_ARM_THM_CALL          10
1738 #endif
1739 #ifndef R_ARM_THM_JUMP24
1740 #define R_ARM_THM_JUMP24        30
1741 #endif
1742 #ifndef R_ARM_THM_JUMP19
1743 #define R_ARM_THM_JUMP19        51
1744 #endif
1745
1746 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1747 {
1748         unsigned int r_typ = ELF_R_TYPE(r->r_info);
1749
1750         switch (r_typ) {
1751         case R_ARM_ABS32:
1752                 /* From ARM ABI: (S + A) | T */
1753                 r->r_addend = (int)(long)
1754                               (elf->symtab_start + ELF_R_SYM(r->r_info));
1755                 break;
1756         case R_ARM_PC24:
1757         case R_ARM_CALL:
1758         case R_ARM_JUMP24:
1759         case R_ARM_THM_CALL:
1760         case R_ARM_THM_JUMP24:
1761         case R_ARM_THM_JUMP19:
1762                 /* From ARM ABI: ((S + A) | T) - P */
1763                 r->r_addend = (int)(long)(elf->hdr +
1764                               sechdr->sh_offset +
1765                               (r->r_offset - sechdr->sh_addr));
1766                 break;
1767         default:
1768                 return 1;
1769         }
1770         return 0;
1771 }
1772
1773 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1774 {
1775         unsigned int r_typ = ELF_R_TYPE(r->r_info);
1776         unsigned int *location = reloc_location(elf, sechdr, r);
1777         unsigned int inst;
1778
1779         if (r_typ == R_MIPS_HI16)
1780                 return 1;       /* skip this */
1781         inst = TO_NATIVE(*location);
1782         switch (r_typ) {
1783         case R_MIPS_LO16:
1784                 r->r_addend = inst & 0xffff;
1785                 break;
1786         case R_MIPS_26:
1787                 r->r_addend = (inst & 0x03ffffff) << 2;
1788                 break;
1789         case R_MIPS_32:
1790                 r->r_addend = inst;
1791                 break;
1792         }
1793         return 0;
1794 }
1795
1796 static void section_rela(const char *modname, struct elf_info *elf,
1797                          Elf_Shdr *sechdr)
1798 {
1799         Elf_Sym  *sym;
1800         Elf_Rela *rela;
1801         Elf_Rela r;
1802         unsigned int r_sym;
1803         const char *fromsec;
1804
1805         Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1806         Elf_Rela *stop  = (void *)start + sechdr->sh_size;
1807
1808         fromsec = sech_name(elf, sechdr);
1809         fromsec += strlen(".rela");
1810         /* if from section (name) is know good then skip it */
1811         if (match(fromsec, section_white_list))
1812                 return;
1813
1814         for (rela = start; rela < stop; rela++) {
1815                 r.r_offset = TO_NATIVE(rela->r_offset);
1816 #if KERNEL_ELFCLASS == ELFCLASS64
1817                 if (elf->hdr->e_machine == EM_MIPS) {
1818                         unsigned int r_typ;
1819                         r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1820                         r_sym = TO_NATIVE(r_sym);
1821                         r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1822                         r.r_info = ELF64_R_INFO(r_sym, r_typ);
1823                 } else {
1824                         r.r_info = TO_NATIVE(rela->r_info);
1825                         r_sym = ELF_R_SYM(r.r_info);
1826                 }
1827 #else
1828                 r.r_info = TO_NATIVE(rela->r_info);
1829                 r_sym = ELF_R_SYM(r.r_info);
1830 #endif
1831                 r.r_addend = TO_NATIVE(rela->r_addend);
1832                 sym = elf->symtab_start + r_sym;
1833                 /* Skip special sections */
1834                 if (is_shndx_special(sym->st_shndx))
1835                         continue;
1836                 if (is_second_extable_reloc(start, rela, fromsec))
1837                         find_extable_entry_size(fromsec, &r);
1838                 check_section_mismatch(modname, elf, &r, sym, fromsec);
1839         }
1840 }
1841
1842 static void section_rel(const char *modname, struct elf_info *elf,
1843                         Elf_Shdr *sechdr)
1844 {
1845         Elf_Sym *sym;
1846         Elf_Rel *rel;
1847         Elf_Rela r;
1848         unsigned int r_sym;
1849         const char *fromsec;
1850
1851         Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1852         Elf_Rel *stop  = (void *)start + sechdr->sh_size;
1853
1854         fromsec = sech_name(elf, sechdr);
1855         fromsec += strlen(".rel");
1856         /* if from section (name) is know good then skip it */
1857         if (match(fromsec, section_white_list))
1858                 return;
1859
1860         for (rel = start; rel < stop; rel++) {
1861                 r.r_offset = TO_NATIVE(rel->r_offset);
1862 #if KERNEL_ELFCLASS == ELFCLASS64
1863                 if (elf->hdr->e_machine == EM_MIPS) {
1864                         unsigned int r_typ;
1865                         r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1866                         r_sym = TO_NATIVE(r_sym);
1867                         r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1868                         r.r_info = ELF64_R_INFO(r_sym, r_typ);
1869                 } else {
1870                         r.r_info = TO_NATIVE(rel->r_info);
1871                         r_sym = ELF_R_SYM(r.r_info);
1872                 }
1873 #else
1874                 r.r_info = TO_NATIVE(rel->r_info);
1875                 r_sym = ELF_R_SYM(r.r_info);
1876 #endif
1877                 r.r_addend = 0;
1878                 switch (elf->hdr->e_machine) {
1879                 case EM_386:
1880                         if (addend_386_rel(elf, sechdr, &r))
1881                                 continue;
1882                         break;
1883                 case EM_ARM:
1884                         if (addend_arm_rel(elf, sechdr, &r))
1885                                 continue;
1886                         break;
1887                 case EM_MIPS:
1888                         if (addend_mips_rel(elf, sechdr, &r))
1889                                 continue;
1890                         break;
1891                 }
1892                 sym = elf->symtab_start + r_sym;
1893                 /* Skip special sections */
1894                 if (is_shndx_special(sym->st_shndx))
1895                         continue;
1896                 if (is_second_extable_reloc(start, rel, fromsec))
1897                         find_extable_entry_size(fromsec, &r);
1898                 check_section_mismatch(modname, elf, &r, sym, fromsec);
1899         }
1900 }
1901
1902 /**
1903  * A module includes a number of sections that are discarded
1904  * either when loaded or when used as built-in.
1905  * For loaded modules all functions marked __init and all data
1906  * marked __initdata will be discarded when the module has been initialized.
1907  * Likewise for modules used built-in the sections marked __exit
1908  * are discarded because __exit marked function are supposed to be called
1909  * only when a module is unloaded which never happens for built-in modules.
1910  * The check_sec_ref() function traverses all relocation records
1911  * to find all references to a section that reference a section that will
1912  * be discarded and warns about it.
1913  **/
1914 static void check_sec_ref(struct module *mod, const char *modname,
1915                           struct elf_info *elf)
1916 {
1917         int i;
1918         Elf_Shdr *sechdrs = elf->sechdrs;
1919
1920         /* Walk through all sections */
1921         for (i = 0; i < elf->num_sections; i++) {
1922                 check_section(modname, elf, &elf->sechdrs[i]);
1923                 /* We want to process only relocation sections and not .init */
1924                 if (sechdrs[i].sh_type == SHT_RELA)
1925                         section_rela(modname, elf, &elf->sechdrs[i]);
1926                 else if (sechdrs[i].sh_type == SHT_REL)
1927                         section_rel(modname, elf, &elf->sechdrs[i]);
1928         }
1929 }
1930
1931 static char *remove_dot(char *s)
1932 {
1933         size_t n = strcspn(s, ".");
1934
1935         if (n && s[n]) {
1936                 size_t m = strspn(s + n + 1, "0123456789");
1937                 if (m && (s[n + m] == '.' || s[n + m] == 0))
1938                         s[n] = 0;
1939         }
1940         return s;
1941 }
1942
1943 static void read_symbols(const char *modname)
1944 {
1945         const char *symname;
1946         char *version;
1947         char *license;
1948         struct module *mod;
1949         struct elf_info info = { };
1950         Elf_Sym *sym;
1951
1952         if (!parse_elf(&info, modname))
1953                 return;
1954
1955         mod = new_module(modname);
1956
1957         /* When there's no vmlinux, don't print warnings about
1958          * unresolved symbols (since there'll be too many ;) */
1959         if (is_vmlinux(modname)) {
1960                 have_vmlinux = 1;
1961                 mod->skip = 1;
1962         }
1963
1964         license = get_modinfo(&info, "license");
1965         if (!license && !is_vmlinux(modname))
1966                 warn("modpost: missing MODULE_LICENSE() in %s\n"
1967                      "see include/linux/module.h for "
1968                      "more information\n", modname);
1969         while (license) {
1970                 if (license_is_gpl_compatible(license))
1971                         mod->gpl_compatible = 1;
1972                 else {
1973                         mod->gpl_compatible = 0;
1974                         break;
1975                 }
1976                 license = get_next_modinfo(&info, "license", license);
1977         }
1978
1979         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1980                 symname = remove_dot(info.strtab + sym->st_name);
1981
1982                 handle_modversions(mod, &info, sym, symname);
1983                 handle_moddevtable(mod, &info, sym, symname);
1984         }
1985
1986         // check for static EXPORT_SYMBOL_* functions && global vars
1987         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1988                 unsigned char bind = ELF_ST_BIND(sym->st_info);
1989
1990                 if (bind == STB_GLOBAL || bind == STB_WEAK) {
1991                         struct symbol *s =
1992                                 find_symbol(remove_dot(info.strtab +
1993                                                        sym->st_name));
1994
1995                         if (s)
1996                                 s->is_static = 0;
1997                 }
1998         }
1999
2000         if (!is_vmlinux(modname) || vmlinux_section_warnings)
2001                 check_sec_ref(mod, modname, &info);
2002
2003         version = get_modinfo(&info, "version");
2004         if (version)
2005                 maybe_frob_rcs_version(modname, version, info.modinfo,
2006                                        version - (char *)info.hdr);
2007         if (version || (all_versions && !is_vmlinux(modname)))
2008                 get_src_version(modname, mod->srcversion,
2009                                 sizeof(mod->srcversion)-1);
2010
2011         parse_elf_finish(&info);
2012
2013         /* Our trick to get versioning for module struct etc. - it's
2014          * never passed as an argument to an exported function, so
2015          * the automatic versioning doesn't pick it up, but it's really
2016          * important anyhow */
2017         if (modversions)
2018                 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
2019 }
2020
2021 static void read_symbols_from_files(const char *filename)
2022 {
2023         FILE *in = stdin;
2024         char fname[PATH_MAX];
2025
2026         if (strcmp(filename, "-") != 0) {
2027                 in = fopen(filename, "r");
2028                 if (!in)
2029                         fatal("Can't open filenames file %s: %m", filename);
2030         }
2031
2032         while (fgets(fname, PATH_MAX, in) != NULL) {
2033                 if (strends(fname, "\n"))
2034                         fname[strlen(fname)-1] = '\0';
2035                 read_symbols(fname);
2036         }
2037
2038         if (in != stdin)
2039                 fclose(in);
2040 }
2041
2042 #define SZ 500
2043
2044 /* We first write the generated file into memory using the
2045  * following helper, then compare to the file on disk and
2046  * only update the later if anything changed */
2047
2048 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2049                                                       const char *fmt, ...)
2050 {
2051         char tmp[SZ];
2052         int len;
2053         va_list ap;
2054
2055         va_start(ap, fmt);
2056         len = vsnprintf(tmp, SZ, fmt, ap);
2057         buf_write(buf, tmp, len);
2058         va_end(ap);
2059 }
2060
2061 void buf_write(struct buffer *buf, const char *s, int len)
2062 {
2063         if (buf->size - buf->pos < len) {
2064                 buf->size += len + SZ;
2065                 buf->p = NOFAIL(realloc(buf->p, buf->size));
2066         }
2067         strncpy(buf->p + buf->pos, s, len);
2068         buf->pos += len;
2069 }
2070
2071 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2072 {
2073         const char *e = is_vmlinux(m) ?"":".ko";
2074
2075         switch (exp) {
2076         case export_gpl:
2077                 fatal("modpost: GPL-incompatible module %s%s "
2078                       "uses GPL-only symbol '%s'\n", m, e, s);
2079                 break;
2080         case export_unused_gpl:
2081                 fatal("modpost: GPL-incompatible module %s%s "
2082                       "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2083                 break;
2084         case export_gpl_future:
2085                 warn("modpost: GPL-incompatible module %s%s "
2086                       "uses future GPL-only symbol '%s'\n", m, e, s);
2087                 break;
2088         case export_plain:
2089         case export_unused:
2090         case export_unknown:
2091                 /* ignore */
2092                 break;
2093         }
2094 }
2095
2096 static void check_for_unused(enum export exp, const char *m, const char *s)
2097 {
2098         const char *e = is_vmlinux(m) ?"":".ko";
2099
2100         switch (exp) {
2101         case export_unused:
2102         case export_unused_gpl:
2103                 warn("modpost: module %s%s "
2104                       "uses symbol '%s' marked UNUSED\n", m, e, s);
2105                 break;
2106         default:
2107                 /* ignore */
2108                 break;
2109         }
2110 }
2111
2112 static int check_exports(struct module *mod)
2113 {
2114         struct symbol *s, *exp;
2115         int err = 0;
2116
2117         for (s = mod->unres; s; s = s->next) {
2118                 const char *basename;
2119                 exp = find_symbol(s->name);
2120                 if (!exp || exp->module == mod) {
2121                         if (have_vmlinux && !s->weak) {
2122                                 if (warn_unresolved) {
2123                                         warn("\"%s\" [%s.ko] undefined!\n",
2124                                              s->name, mod->name);
2125                                 } else {
2126                                         merror("\"%s\" [%s.ko] undefined!\n",
2127                                                s->name, mod->name);
2128                                         err = 1;
2129                                 }
2130                         }
2131                         continue;
2132                 }
2133                 basename = strrchr(mod->name, '/');
2134                 if (basename)
2135                         basename++;
2136                 else
2137                         basename = mod->name;
2138                 if (!mod->gpl_compatible)
2139                         check_for_gpl_usage(exp->export, basename, exp->name);
2140                 check_for_unused(exp->export, basename, exp->name);
2141         }
2142
2143         return err;
2144 }
2145
2146 static int check_modname_len(struct module *mod)
2147 {
2148         const char *mod_name;
2149
2150         mod_name = strrchr(mod->name, '/');
2151         if (mod_name == NULL)
2152                 mod_name = mod->name;
2153         else
2154                 mod_name++;
2155         if (strlen(mod_name) >= MODULE_NAME_LEN) {
2156                 merror("module name is too long [%s.ko]\n", mod->name);
2157                 return 1;
2158         }
2159
2160         return 0;
2161 }
2162
2163 /**
2164  * Header for the generated file
2165  **/
2166 static void add_header(struct buffer *b, struct module *mod)
2167 {
2168         buf_printf(b, "#include <linux/build-salt.h>\n");
2169         buf_printf(b, "#include <linux/module.h>\n");
2170         buf_printf(b, "#include <linux/vermagic.h>\n");
2171         buf_printf(b, "#include <linux/compiler.h>\n");
2172         buf_printf(b, "\n");
2173         buf_printf(b, "BUILD_SALT;\n");
2174         buf_printf(b, "\n");
2175         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
2176         buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
2177         buf_printf(b, "\n");
2178         buf_printf(b, "__visible struct module __this_module\n");
2179         buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
2180         buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
2181         if (mod->has_init)
2182                 buf_printf(b, "\t.init = init_module,\n");
2183         if (mod->has_cleanup)
2184                 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
2185                               "\t.exit = cleanup_module,\n"
2186                               "#endif\n");
2187         buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
2188         buf_printf(b, "};\n");
2189 }
2190
2191 static void add_intree_flag(struct buffer *b, int is_intree)
2192 {
2193         if (is_intree)
2194                 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2195 }
2196
2197 /* Cannot check for assembler */
2198 static void add_retpoline(struct buffer *b)
2199 {
2200         buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
2201         buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2202         buf_printf(b, "#endif\n");
2203 }
2204
2205 static void add_staging_flag(struct buffer *b, const char *name)
2206 {
2207         if (strstarts(name, "drivers/staging"))
2208                 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2209 }
2210
2211 /**
2212  * Record CRCs for unresolved symbols
2213  **/
2214 static int add_versions(struct buffer *b, struct module *mod)
2215 {
2216         struct symbol *s, *exp;
2217         int err = 0;
2218
2219         for (s = mod->unres; s; s = s->next) {
2220                 exp = find_symbol(s->name);
2221                 if (!exp || exp->module == mod)
2222                         continue;
2223                 s->module = exp->module;
2224                 s->crc_valid = exp->crc_valid;
2225                 s->crc = exp->crc;
2226         }
2227
2228         if (!modversions)
2229                 return err;
2230
2231         buf_printf(b, "\n");
2232         buf_printf(b, "static const struct modversion_info ____versions[]\n");
2233         buf_printf(b, "__used __section(__versions) = {\n");
2234
2235         for (s = mod->unres; s; s = s->next) {
2236                 if (!s->module)
2237                         continue;
2238                 if (!s->crc_valid) {
2239                         warn("\"%s\" [%s.ko] has no CRC!\n",
2240                                 s->name, mod->name);
2241                         continue;
2242                 }
2243                 if (strlen(s->name) >= MODULE_NAME_LEN) {
2244                         merror("too long symbol \"%s\" [%s.ko]\n",
2245                                s->name, mod->name);
2246                         err = 1;
2247                         break;
2248                 }
2249                 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2250                            s->crc, s->name);
2251         }
2252
2253         buf_printf(b, "};\n");
2254
2255         return err;
2256 }
2257
2258 static void add_depends(struct buffer *b, struct module *mod)
2259 {
2260         struct symbol *s;
2261         int first = 1;
2262
2263         /* Clear ->seen flag of modules that own symbols needed by this. */
2264         for (s = mod->unres; s; s = s->next)
2265                 if (s->module)
2266                         s->module->seen = is_vmlinux(s->module->name);
2267
2268         buf_printf(b, "\n");
2269         buf_printf(b, "MODULE_INFO(depends, \"");
2270         for (s = mod->unres; s; s = s->next) {
2271                 const char *p;
2272                 if (!s->module)
2273                         continue;
2274
2275                 if (s->module->seen)
2276                         continue;
2277
2278                 s->module->seen = 1;
2279                 p = strrchr(s->module->name, '/');
2280                 if (p)
2281                         p++;
2282                 else
2283                         p = s->module->name;
2284                 buf_printf(b, "%s%s", first ? "" : ",", p);
2285                 first = 0;
2286         }
2287         buf_printf(b, "\");\n");
2288 }
2289
2290 static void add_srcversion(struct buffer *b, struct module *mod)
2291 {
2292         if (mod->srcversion[0]) {
2293                 buf_printf(b, "\n");
2294                 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2295                            mod->srcversion);
2296         }
2297 }
2298
2299 static void write_if_changed(struct buffer *b, const char *fname)
2300 {
2301         char *tmp;
2302         FILE *file;
2303         struct stat st;
2304
2305         file = fopen(fname, "r");
2306         if (!file)
2307                 goto write;
2308
2309         if (fstat(fileno(file), &st) < 0)
2310                 goto close_write;
2311
2312         if (st.st_size != b->pos)
2313                 goto close_write;
2314
2315         tmp = NOFAIL(malloc(b->pos));
2316         if (fread(tmp, 1, b->pos, file) != b->pos)
2317                 goto free_write;
2318
2319         if (memcmp(tmp, b->p, b->pos) != 0)
2320                 goto free_write;
2321
2322         free(tmp);
2323         fclose(file);
2324         return;
2325
2326  free_write:
2327         free(tmp);
2328  close_write:
2329         fclose(file);
2330  write:
2331         file = fopen(fname, "w");
2332         if (!file) {
2333                 perror(fname);
2334                 exit(1);
2335         }
2336         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2337                 perror(fname);
2338                 exit(1);
2339         }
2340         fclose(file);
2341 }
2342
2343 /* parse Module.symvers file. line format:
2344  * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
2345  **/
2346 static void read_dump(const char *fname, unsigned int kernel)
2347 {
2348         unsigned long size, pos = 0;
2349         void *file = grab_file(fname, &size);
2350         char *line;
2351
2352         if (!file)
2353                 /* No symbol versions, silently ignore */
2354                 return;
2355
2356         while ((line = get_next_line(&pos, file, size))) {
2357                 char *symname, *modname, *d, *export, *end;
2358                 unsigned int crc;
2359                 struct module *mod;
2360                 struct symbol *s;
2361
2362                 if (!(symname = strchr(line, '\t')))
2363                         goto fail;
2364                 *symname++ = '\0';
2365                 if (!(modname = strchr(symname, '\t')))
2366                         goto fail;
2367                 *modname++ = '\0';
2368                 if ((export = strchr(modname, '\t')) != NULL)
2369                         *export++ = '\0';
2370                 if (export && ((end = strchr(export, '\t')) != NULL))
2371                         *end = '\0';
2372                 crc = strtoul(line, &d, 16);
2373                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2374                         goto fail;
2375                 mod = find_module(modname);
2376                 if (!mod) {
2377                         if (is_vmlinux(modname))
2378                                 have_vmlinux = 1;
2379                         mod = new_module(modname);
2380                         mod->skip = 1;
2381                 }
2382                 s = sym_add_exported(symname, mod, export_no(export));
2383                 s->kernel    = kernel;
2384                 s->preloaded = 1;
2385                 s->is_static = 0;
2386                 sym_update_crc(symname, mod, crc, export_no(export));
2387         }
2388         release_file(file, size);
2389         return;
2390 fail:
2391         release_file(file, size);
2392         fatal("parse error in symbol dump file\n");
2393 }
2394
2395 /* For normal builds always dump all symbols.
2396  * For external modules only dump symbols
2397  * that are not read from kernel Module.symvers.
2398  **/
2399 static int dump_sym(struct symbol *sym)
2400 {
2401         if (!external_module)
2402                 return 1;
2403         if (sym->vmlinux || sym->kernel)
2404                 return 0;
2405         return 1;
2406 }
2407
2408 static void write_dump(const char *fname)
2409 {
2410         struct buffer buf = { };
2411         struct symbol *symbol;
2412         int n;
2413
2414         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2415                 symbol = symbolhash[n];
2416                 while (symbol) {
2417                         if (dump_sym(symbol))
2418                                 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
2419                                         symbol->crc, symbol->name,
2420                                         symbol->module->name,
2421                                         export_str(symbol->export));
2422                         symbol = symbol->next;
2423                 }
2424         }
2425         write_if_changed(&buf, fname);
2426         free(buf.p);
2427 }
2428
2429 struct ext_sym_list {
2430         struct ext_sym_list *next;
2431         const char *file;
2432 };
2433
2434 int main(int argc, char **argv)
2435 {
2436         struct module *mod;
2437         struct buffer buf = { };
2438         char *kernel_read = NULL, *module_read = NULL;
2439         char *dump_write = NULL, *files_source = NULL;
2440         int opt;
2441         int err;
2442         int n;
2443         struct ext_sym_list *extsym_iter;
2444         struct ext_sym_list *extsym_start = NULL;
2445
2446         while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
2447                 switch (opt) {
2448                 case 'i':
2449                         kernel_read = optarg;
2450                         break;
2451                 case 'I':
2452                         module_read = optarg;
2453                         external_module = 1;
2454                         break;
2455                 case 'e':
2456                         external_module = 1;
2457                         extsym_iter =
2458                            NOFAIL(malloc(sizeof(*extsym_iter)));
2459                         extsym_iter->next = extsym_start;
2460                         extsym_iter->file = optarg;
2461                         extsym_start = extsym_iter;
2462                         break;
2463                 case 'm':
2464                         modversions = 1;
2465                         break;
2466                 case 'n':
2467                         ignore_missing_files = 1;
2468                         break;
2469                 case 'o':
2470                         dump_write = optarg;
2471                         break;
2472                 case 'a':
2473                         all_versions = 1;
2474                         break;
2475                 case 's':
2476                         vmlinux_section_warnings = 0;
2477                         break;
2478                 case 'T':
2479                         files_source = optarg;
2480                         break;
2481                 case 'w':
2482                         warn_unresolved = 1;
2483                         break;
2484                 case 'E':
2485                         sec_mismatch_fatal = 1;
2486                         break;
2487                 default:
2488                         exit(1);
2489                 }
2490         }
2491
2492         if (kernel_read)
2493                 read_dump(kernel_read, 1);
2494         if (module_read)
2495                 read_dump(module_read, 0);
2496         while (extsym_start) {
2497                 read_dump(extsym_start->file, 0);
2498                 extsym_iter = extsym_start->next;
2499                 free(extsym_start);
2500                 extsym_start = extsym_iter;
2501         }
2502
2503         while (optind < argc)
2504                 read_symbols(argv[optind++]);
2505
2506         if (files_source)
2507                 read_symbols_from_files(files_source);
2508
2509         err = 0;
2510
2511         for (mod = modules; mod; mod = mod->next) {
2512                 char fname[PATH_MAX];
2513
2514                 if (mod->skip)
2515                         continue;
2516
2517                 buf.pos = 0;
2518
2519                 err |= check_modname_len(mod);
2520                 err |= check_exports(mod);
2521                 add_header(&buf, mod);
2522                 add_intree_flag(&buf, !external_module);
2523                 add_retpoline(&buf);
2524                 add_staging_flag(&buf, mod->name);
2525                 err |= add_versions(&buf, mod);
2526                 add_depends(&buf, mod);
2527                 add_moddevtable(&buf, mod);
2528                 add_srcversion(&buf, mod);
2529
2530                 sprintf(fname, "%s.mod.c", mod->name);
2531                 write_if_changed(&buf, fname);
2532         }
2533         if (dump_write)
2534                 write_dump(dump_write);
2535         if (sec_mismatch_count && sec_mismatch_fatal)
2536                 fatal("modpost: Section mismatches detected.\n"
2537                       "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2538         for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2539                 struct symbol *s = symbolhash[n];
2540
2541                 while (s) {
2542                         if (s->is_static)
2543                                 warn("\"%s\" [%s] is a static %s\n",
2544                                      s->name, s->module->name,
2545                                      export_str(s->export));
2546
2547                         s = s->next;
2548                 }
2549         }
2550
2551         free(buf.p);
2552
2553         return err;
2554 }