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