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