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