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