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