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