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