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