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