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