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