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