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