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