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