0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #define _GNU_SOURCE
0015 #include <elf.h>
0016 #include <fnmatch.h>
0017 #include <stdio.h>
0018 #include <ctype.h>
0019 #include <string.h>
0020 #include <limits.h>
0021 #include <stdbool.h>
0022 #include <errno.h>
0023 #include "modpost.h"
0024 #include "../../include/linux/license.h"
0025
0026
0027 static bool modversions;
0028
0029 static bool all_versions;
0030
0031 static bool external_module;
0032
0033 static bool warn_unresolved;
0034
0035 static int sec_mismatch_count;
0036 static bool sec_mismatch_warn_only = true;
0037
0038 static bool ignore_missing_files;
0039
0040 static bool allow_missing_ns_imports;
0041
0042 static bool error_occurred;
0043
0044
0045
0046
0047
0048 #define MAX_UNRESOLVED_REPORTS 10
0049 static unsigned int nr_unresolved;
0050
0051
0052
0053
0054
0055 #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
0056
0057 void __attribute__((format(printf, 2, 3)))
0058 modpost_log(enum loglevel loglevel, const char *fmt, ...)
0059 {
0060 va_list arglist;
0061
0062 switch (loglevel) {
0063 case LOG_WARN:
0064 fprintf(stderr, "WARNING: ");
0065 break;
0066 case LOG_ERROR:
0067 fprintf(stderr, "ERROR: ");
0068 break;
0069 case LOG_FATAL:
0070 fprintf(stderr, "FATAL: ");
0071 break;
0072 default:
0073 break;
0074 }
0075
0076 fprintf(stderr, "modpost: ");
0077
0078 va_start(arglist, fmt);
0079 vfprintf(stderr, fmt, arglist);
0080 va_end(arglist);
0081
0082 if (loglevel == LOG_FATAL)
0083 exit(1);
0084 if (loglevel == LOG_ERROR)
0085 error_occurred = true;
0086 }
0087
0088 static inline bool strends(const char *str, const char *postfix)
0089 {
0090 if (strlen(str) < strlen(postfix))
0091 return false;
0092
0093 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
0094 }
0095
0096 void *do_nofail(void *ptr, const char *expr)
0097 {
0098 if (!ptr)
0099 fatal("Memory allocation failure: %s.\n", expr);
0100
0101 return ptr;
0102 }
0103
0104 char *read_text_file(const char *filename)
0105 {
0106 struct stat st;
0107 size_t nbytes;
0108 int fd;
0109 char *buf;
0110
0111 fd = open(filename, O_RDONLY);
0112 if (fd < 0) {
0113 perror(filename);
0114 exit(1);
0115 }
0116
0117 if (fstat(fd, &st) < 0) {
0118 perror(filename);
0119 exit(1);
0120 }
0121
0122 buf = NOFAIL(malloc(st.st_size + 1));
0123
0124 nbytes = st.st_size;
0125
0126 while (nbytes) {
0127 ssize_t bytes_read;
0128
0129 bytes_read = read(fd, buf, nbytes);
0130 if (bytes_read < 0) {
0131 perror(filename);
0132 exit(1);
0133 }
0134
0135 nbytes -= bytes_read;
0136 }
0137 buf[st.st_size] = '\0';
0138
0139 close(fd);
0140
0141 return buf;
0142 }
0143
0144 char *get_line(char **stringp)
0145 {
0146 char *orig = *stringp, *next;
0147
0148
0149 if (!orig || *orig == '\0')
0150 return NULL;
0151
0152
0153 next = strchr(orig, '\n');
0154 if (next)
0155 *next++ = '\0';
0156
0157 *stringp = next;
0158
0159 return orig;
0160 }
0161
0162
0163 LIST_HEAD(modules);
0164
0165 static struct module *find_module(const char *modname)
0166 {
0167 struct module *mod;
0168
0169 list_for_each_entry(mod, &modules, list) {
0170 if (strcmp(mod->name, modname) == 0)
0171 return mod;
0172 }
0173 return NULL;
0174 }
0175
0176 static struct module *new_module(const char *name, size_t namelen)
0177 {
0178 struct module *mod;
0179
0180 mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
0181 memset(mod, 0, sizeof(*mod));
0182
0183 INIT_LIST_HEAD(&mod->exported_symbols);
0184 INIT_LIST_HEAD(&mod->unresolved_symbols);
0185 INIT_LIST_HEAD(&mod->missing_namespaces);
0186 INIT_LIST_HEAD(&mod->imported_namespaces);
0187
0188 memcpy(mod->name, name, namelen);
0189 mod->name[namelen] = '\0';
0190 mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
0191
0192
0193
0194
0195
0196
0197 mod->is_gpl_compatible = true;
0198
0199 list_add_tail(&mod->list, &modules);
0200
0201 return mod;
0202 }
0203
0204
0205
0206
0207 #define SYMBOL_HASH_SIZE 1024
0208
0209 struct symbol {
0210 struct symbol *next;
0211 struct list_head list;
0212 struct module *module;
0213 char *namespace;
0214 unsigned int crc;
0215 bool crc_valid;
0216 bool weak;
0217 bool is_gpl_only;
0218 char name[];
0219 };
0220
0221 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
0222
0223
0224 static inline unsigned int tdb_hash(const char *name)
0225 {
0226 unsigned value;
0227 unsigned i;
0228
0229
0230 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
0231 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
0232
0233 return (1103515243 * value + 12345);
0234 }
0235
0236
0237
0238
0239
0240 static struct symbol *alloc_symbol(const char *name)
0241 {
0242 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
0243
0244 memset(s, 0, sizeof(*s));
0245 strcpy(s->name, name);
0246
0247 return s;
0248 }
0249
0250
0251 static void hash_add_symbol(struct symbol *sym)
0252 {
0253 unsigned int hash;
0254
0255 hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
0256 sym->next = symbolhash[hash];
0257 symbolhash[hash] = sym;
0258 }
0259
0260 static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
0261 {
0262 struct symbol *sym;
0263
0264 sym = alloc_symbol(name);
0265 sym->weak = weak;
0266
0267 list_add_tail(&sym->list, &mod->unresolved_symbols);
0268 }
0269
0270 static struct symbol *sym_find_with_module(const char *name, struct module *mod)
0271 {
0272 struct symbol *s;
0273
0274
0275 if (name[0] == '.')
0276 name++;
0277
0278 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
0279 if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
0280 return s;
0281 }
0282 return NULL;
0283 }
0284
0285 static struct symbol *find_symbol(const char *name)
0286 {
0287 return sym_find_with_module(name, NULL);
0288 }
0289
0290 struct namespace_list {
0291 struct list_head list;
0292 char namespace[];
0293 };
0294
0295 static bool contains_namespace(struct list_head *head, const char *namespace)
0296 {
0297 struct namespace_list *list;
0298
0299 list_for_each_entry(list, head, list) {
0300 if (!strcmp(list->namespace, namespace))
0301 return true;
0302 }
0303
0304 return false;
0305 }
0306
0307 static void add_namespace(struct list_head *head, const char *namespace)
0308 {
0309 struct namespace_list *ns_entry;
0310
0311 if (!contains_namespace(head, namespace)) {
0312 ns_entry = NOFAIL(malloc(sizeof(*ns_entry) +
0313 strlen(namespace) + 1));
0314 strcpy(ns_entry->namespace, namespace);
0315 list_add_tail(&ns_entry->list, head);
0316 }
0317 }
0318
0319 static void *sym_get_data_by_offset(const struct elf_info *info,
0320 unsigned int secindex, unsigned long offset)
0321 {
0322 Elf_Shdr *sechdr = &info->sechdrs[secindex];
0323
0324 return (void *)info->hdr + sechdr->sh_offset + offset;
0325 }
0326
0327 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
0328 {
0329 return sym_get_data_by_offset(info, get_secindex(info, sym),
0330 sym->st_value);
0331 }
0332
0333 static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
0334 {
0335 return sym_get_data_by_offset(info, info->secindex_strings,
0336 sechdr->sh_name);
0337 }
0338
0339 static const char *sec_name(const struct elf_info *info, unsigned int secindex)
0340 {
0341
0342
0343
0344
0345
0346 if (secindex >= info->num_sections)
0347 return "";
0348
0349 return sech_name(info, &info->sechdrs[secindex]);
0350 }
0351
0352 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
0353
0354 static void sym_update_namespace(const char *symname, const char *namespace)
0355 {
0356 struct symbol *s = find_symbol(symname);
0357
0358
0359
0360
0361
0362 if (!s) {
0363 error("Could not update namespace(%s) for symbol %s\n",
0364 namespace, symname);
0365 return;
0366 }
0367
0368 free(s->namespace);
0369 s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
0370 }
0371
0372 static struct symbol *sym_add_exported(const char *name, struct module *mod,
0373 bool gpl_only)
0374 {
0375 struct symbol *s = find_symbol(name);
0376
0377 if (s && (!external_module || s->module->is_vmlinux || s->module == mod)) {
0378 error("%s: '%s' exported twice. Previous export was in %s%s\n",
0379 mod->name, name, s->module->name,
0380 s->module->is_vmlinux ? "" : ".ko");
0381 }
0382
0383 s = alloc_symbol(name);
0384 s->module = mod;
0385 s->is_gpl_only = gpl_only;
0386 list_add_tail(&s->list, &mod->exported_symbols);
0387 hash_add_symbol(s);
0388
0389 return s;
0390 }
0391
0392 static void sym_set_crc(struct symbol *sym, unsigned int crc)
0393 {
0394 sym->crc = crc;
0395 sym->crc_valid = true;
0396 }
0397
0398 static void *grab_file(const char *filename, size_t *size)
0399 {
0400 struct stat st;
0401 void *map = MAP_FAILED;
0402 int fd;
0403
0404 fd = open(filename, O_RDONLY);
0405 if (fd < 0)
0406 return NULL;
0407 if (fstat(fd, &st))
0408 goto failed;
0409
0410 *size = st.st_size;
0411 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
0412
0413 failed:
0414 close(fd);
0415 if (map == MAP_FAILED)
0416 return NULL;
0417 return map;
0418 }
0419
0420 static void release_file(void *file, size_t size)
0421 {
0422 munmap(file, size);
0423 }
0424
0425 static int parse_elf(struct elf_info *info, const char *filename)
0426 {
0427 unsigned int i;
0428 Elf_Ehdr *hdr;
0429 Elf_Shdr *sechdrs;
0430 Elf_Sym *sym;
0431 const char *secstrings;
0432 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
0433
0434 hdr = grab_file(filename, &info->size);
0435 if (!hdr) {
0436 if (ignore_missing_files) {
0437 fprintf(stderr, "%s: %s (ignored)\n", filename,
0438 strerror(errno));
0439 return 0;
0440 }
0441 perror(filename);
0442 exit(1);
0443 }
0444 info->hdr = hdr;
0445 if (info->size < sizeof(*hdr)) {
0446
0447 return 0;
0448 }
0449
0450 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
0451 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
0452 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
0453 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
0454
0455 return 0;
0456 }
0457
0458 hdr->e_type = TO_NATIVE(hdr->e_type);
0459 hdr->e_machine = TO_NATIVE(hdr->e_machine);
0460 hdr->e_version = TO_NATIVE(hdr->e_version);
0461 hdr->e_entry = TO_NATIVE(hdr->e_entry);
0462 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
0463 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
0464 hdr->e_flags = TO_NATIVE(hdr->e_flags);
0465 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
0466 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
0467 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
0468 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
0469 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
0470 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
0471 sechdrs = (void *)hdr + hdr->e_shoff;
0472 info->sechdrs = sechdrs;
0473
0474
0475 if (hdr->e_type != ET_REL)
0476 fatal("%s: not relocatable object.", filename);
0477
0478
0479 if (hdr->e_shoff > info->size) {
0480 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
0481 (unsigned long)hdr->e_shoff, filename, info->size);
0482 return 0;
0483 }
0484
0485 if (hdr->e_shnum == SHN_UNDEF) {
0486
0487
0488
0489
0490 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
0491 }
0492 else {
0493 info->num_sections = hdr->e_shnum;
0494 }
0495 if (hdr->e_shstrndx == SHN_XINDEX) {
0496 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
0497 }
0498 else {
0499 info->secindex_strings = hdr->e_shstrndx;
0500 }
0501
0502
0503 for (i = 0; i < info->num_sections; i++) {
0504 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
0505 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
0506 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
0507 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
0508 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
0509 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
0510 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
0511 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
0512 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
0513 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
0514 }
0515
0516 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
0517 for (i = 1; i < info->num_sections; i++) {
0518 const char *secname;
0519 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
0520
0521 if (!nobits && sechdrs[i].sh_offset > info->size) {
0522 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
0523 "sizeof(*hrd)=%zu\n", filename,
0524 (unsigned long)sechdrs[i].sh_offset,
0525 sizeof(*hdr));
0526 return 0;
0527 }
0528 secname = secstrings + sechdrs[i].sh_name;
0529 if (strcmp(secname, ".modinfo") == 0) {
0530 if (nobits)
0531 fatal("%s has NOBITS .modinfo\n", filename);
0532 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
0533 info->modinfo_len = sechdrs[i].sh_size;
0534 }
0535
0536 if (sechdrs[i].sh_type == SHT_SYMTAB) {
0537 unsigned int sh_link_idx;
0538 symtab_idx = i;
0539 info->symtab_start = (void *)hdr +
0540 sechdrs[i].sh_offset;
0541 info->symtab_stop = (void *)hdr +
0542 sechdrs[i].sh_offset + sechdrs[i].sh_size;
0543 sh_link_idx = sechdrs[i].sh_link;
0544 info->strtab = (void *)hdr +
0545 sechdrs[sh_link_idx].sh_offset;
0546 }
0547
0548
0549 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
0550 symtab_shndx_idx = i;
0551 info->symtab_shndx_start = (void *)hdr +
0552 sechdrs[i].sh_offset;
0553 info->symtab_shndx_stop = (void *)hdr +
0554 sechdrs[i].sh_offset + sechdrs[i].sh_size;
0555 }
0556 }
0557 if (!info->symtab_start)
0558 fatal("%s has no symtab?\n", filename);
0559
0560
0561 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
0562 sym->st_shndx = TO_NATIVE(sym->st_shndx);
0563 sym->st_name = TO_NATIVE(sym->st_name);
0564 sym->st_value = TO_NATIVE(sym->st_value);
0565 sym->st_size = TO_NATIVE(sym->st_size);
0566 }
0567
0568 if (symtab_shndx_idx != ~0U) {
0569 Elf32_Word *p;
0570 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
0571 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
0572 filename, sechdrs[symtab_shndx_idx].sh_link,
0573 symtab_idx);
0574
0575 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
0576 p++)
0577 *p = TO_NATIVE(*p);
0578 }
0579
0580 return 1;
0581 }
0582
0583 static void parse_elf_finish(struct elf_info *info)
0584 {
0585 release_file(info->hdr, info->size);
0586 }
0587
0588 static int ignore_undef_symbol(struct elf_info *info, const char *symname)
0589 {
0590
0591 if (strcmp(symname, "__this_module") == 0)
0592 return 1;
0593
0594 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
0595 return 1;
0596 if (info->hdr->e_machine == EM_PPC)
0597
0598 if (strstarts(symname, "_restgpr_") ||
0599 strstarts(symname, "_savegpr_") ||
0600 strstarts(symname, "_rest32gpr_") ||
0601 strstarts(symname, "_save32gpr_") ||
0602 strstarts(symname, "_restvr_") ||
0603 strstarts(symname, "_savevr_"))
0604 return 1;
0605 if (info->hdr->e_machine == EM_PPC64)
0606
0607 if (strstarts(symname, "_restgpr0_") ||
0608 strstarts(symname, "_savegpr0_") ||
0609 strstarts(symname, "_restvr_") ||
0610 strstarts(symname, "_savevr_") ||
0611 strcmp(symname, ".TOC.") == 0)
0612 return 1;
0613
0614 if (info->hdr->e_machine == EM_S390)
0615
0616 if (strstarts(symname, "__s390_indirect_jump_r"))
0617 return 1;
0618
0619 return 0;
0620 }
0621
0622 static void handle_symbol(struct module *mod, struct elf_info *info,
0623 const Elf_Sym *sym, const char *symname)
0624 {
0625 switch (sym->st_shndx) {
0626 case SHN_COMMON:
0627 if (strstarts(symname, "__gnu_lto_")) {
0628
0629 } else
0630 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
0631 break;
0632 case SHN_UNDEF:
0633
0634 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
0635 ELF_ST_BIND(sym->st_info) != STB_WEAK)
0636 break;
0637 if (ignore_undef_symbol(info, symname))
0638 break;
0639 if (info->hdr->e_machine == EM_SPARC ||
0640 info->hdr->e_machine == EM_SPARCV9) {
0641
0642 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
0643 break;
0644 if (symname[0] == '.') {
0645 char *munged = NOFAIL(strdup(symname));
0646 munged[0] = '_';
0647 munged[1] = toupper(munged[1]);
0648 symname = munged;
0649 }
0650 }
0651
0652 sym_add_unresolved(symname, mod,
0653 ELF_ST_BIND(sym->st_info) == STB_WEAK);
0654 break;
0655 default:
0656
0657 if (strstarts(symname, "__ksymtab_")) {
0658 const char *name, *secname;
0659
0660 name = symname + strlen("__ksymtab_");
0661 secname = sec_name(info, get_secindex(info, sym));
0662
0663 if (strstarts(secname, "___ksymtab_gpl+"))
0664 sym_add_exported(name, mod, true);
0665 else if (strstarts(secname, "___ksymtab+"))
0666 sym_add_exported(name, mod, false);
0667 }
0668 if (strcmp(symname, "init_module") == 0)
0669 mod->has_init = true;
0670 if (strcmp(symname, "cleanup_module") == 0)
0671 mod->has_cleanup = true;
0672 break;
0673 }
0674 }
0675
0676
0677
0678
0679 static char *next_string(char *string, unsigned long *secsize)
0680 {
0681
0682 while (string[0]) {
0683 string++;
0684 if ((*secsize)-- <= 1)
0685 return NULL;
0686 }
0687
0688
0689 while (!string[0]) {
0690 string++;
0691 if ((*secsize)-- <= 1)
0692 return NULL;
0693 }
0694 return string;
0695 }
0696
0697 static char *get_next_modinfo(struct elf_info *info, const char *tag,
0698 char *prev)
0699 {
0700 char *p;
0701 unsigned int taglen = strlen(tag);
0702 char *modinfo = info->modinfo;
0703 unsigned long size = info->modinfo_len;
0704
0705 if (prev) {
0706 size -= prev - modinfo;
0707 modinfo = next_string(prev, &size);
0708 }
0709
0710 for (p = modinfo; p; p = next_string(p, &size)) {
0711 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
0712 return p + taglen + 1;
0713 }
0714 return NULL;
0715 }
0716
0717 static char *get_modinfo(struct elf_info *info, const char *tag)
0718
0719 {
0720 return get_next_modinfo(info, tag, NULL);
0721 }
0722
0723 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
0724 {
0725 if (sym)
0726 return elf->strtab + sym->st_name;
0727 else
0728 return "(unknown)";
0729 }
0730
0731
0732
0733
0734
0735
0736
0737 static bool match(const char *string, const char *const patterns[])
0738 {
0739 const char *pattern;
0740
0741 while ((pattern = *patterns++)) {
0742 if (!fnmatch(pattern, string, 0))
0743 return true;
0744 }
0745
0746 return false;
0747 }
0748
0749
0750 #define PATTERNS(...) \
0751 ({ \
0752 static const char *const patterns[] = {__VA_ARGS__, NULL}; \
0753 patterns; \
0754 })
0755
0756
0757 static const char *const section_white_list[] =
0758 {
0759 ".comment*",
0760 ".debug*",
0761 ".zdebug*",
0762 ".GCC.command.line",
0763 ".mdebug*",
0764 ".pdr",
0765 ".stab*",
0766 ".note*",
0767 ".got*",
0768 ".toc*",
0769 ".xt.prop",
0770 ".xt.lit",
0771 ".arcextmap*",
0772 ".gnu.linkonce.arcext*",
0773 ".cmem*",
0774 ".fmt_slot*",
0775 ".gnu.lto*",
0776 ".discard.*",
0777 NULL
0778 };
0779
0780
0781
0782
0783
0784
0785 static void check_section(const char *modname, struct elf_info *elf,
0786 Elf_Shdr *sechdr)
0787 {
0788 const char *sec = sech_name(elf, sechdr);
0789
0790 if (sechdr->sh_type == SHT_PROGBITS &&
0791 !(sechdr->sh_flags & SHF_ALLOC) &&
0792 !match(sec, section_white_list)) {
0793 warn("%s (%s): unexpected non-allocatable section.\n"
0794 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
0795 "Note that for example <linux/init.h> contains\n"
0796 "section definitions for use in .S files.\n\n",
0797 modname, sec);
0798 }
0799 }
0800
0801
0802
0803 #define ALL_INIT_DATA_SECTIONS \
0804 ".init.setup", ".init.rodata", ".meminit.rodata", \
0805 ".init.data", ".meminit.data"
0806 #define ALL_EXIT_DATA_SECTIONS \
0807 ".exit.data", ".memexit.data"
0808
0809 #define ALL_INIT_TEXT_SECTIONS \
0810 ".init.text", ".meminit.text"
0811 #define ALL_EXIT_TEXT_SECTIONS \
0812 ".exit.text", ".memexit.text"
0813
0814 #define ALL_PCI_INIT_SECTIONS \
0815 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
0816 ".pci_fixup_enable", ".pci_fixup_resume", \
0817 ".pci_fixup_resume_early", ".pci_fixup_suspend"
0818
0819 #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
0820 #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
0821
0822 #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
0823 #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
0824
0825 #define DATA_SECTIONS ".data", ".data.rel"
0826 #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
0827 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
0828 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
0829 ".fixup", ".entry.text", ".exception.text", ".text.*", \
0830 ".coldtext", ".softirqentry.text"
0831
0832 #define INIT_SECTIONS ".init.*"
0833 #define MEM_INIT_SECTIONS ".meminit.*"
0834
0835 #define EXIT_SECTIONS ".exit.*"
0836 #define MEM_EXIT_SECTIONS ".memexit.*"
0837
0838 #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
0839 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
0840
0841
0842 static const char *const init_data_sections[] =
0843 { ALL_INIT_DATA_SECTIONS, NULL };
0844
0845
0846 static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
0847
0848
0849 static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
0850
0851
0852 static const char *const data_sections[] = { DATA_SECTIONS, NULL };
0853
0854 static const char *const head_sections[] = { ".head.text*", NULL };
0855 static const char *const linker_symbols[] =
0856 { "__init_begin", "_sinittext", "_einittext", NULL };
0857 static const char *const optim_symbols[] = { "*.constprop.*", NULL };
0858
0859 enum mismatch {
0860 TEXT_TO_ANY_INIT,
0861 DATA_TO_ANY_INIT,
0862 TEXT_TO_ANY_EXIT,
0863 DATA_TO_ANY_EXIT,
0864 XXXINIT_TO_SOME_INIT,
0865 XXXEXIT_TO_SOME_EXIT,
0866 ANY_INIT_TO_ANY_EXIT,
0867 ANY_EXIT_TO_ANY_INIT,
0868 EXPORT_TO_INIT_EXIT,
0869 EXTABLE_TO_NON_TEXT,
0870 };
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889 struct sectioncheck {
0890 const char *fromsec[20];
0891 const char *bad_tosec[20];
0892 const char *good_tosec[20];
0893 enum mismatch mismatch;
0894 void (*handler)(const char *modname, struct elf_info *elf,
0895 const struct sectioncheck* const mismatch,
0896 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
0897
0898 };
0899
0900 static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
0901 const struct sectioncheck* const mismatch,
0902 Elf_Rela *r, Elf_Sym *sym,
0903 const char *fromsec);
0904
0905 static const struct sectioncheck sectioncheck[] = {
0906
0907
0908
0909 {
0910 .fromsec = { TEXT_SECTIONS, NULL },
0911 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
0912 .mismatch = TEXT_TO_ANY_INIT,
0913 },
0914 {
0915 .fromsec = { DATA_SECTIONS, NULL },
0916 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
0917 .mismatch = DATA_TO_ANY_INIT,
0918 },
0919 {
0920 .fromsec = { DATA_SECTIONS, NULL },
0921 .bad_tosec = { INIT_SECTIONS, NULL },
0922 .mismatch = DATA_TO_ANY_INIT,
0923 },
0924 {
0925 .fromsec = { TEXT_SECTIONS, NULL },
0926 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
0927 .mismatch = TEXT_TO_ANY_EXIT,
0928 },
0929 {
0930 .fromsec = { DATA_SECTIONS, NULL },
0931 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
0932 .mismatch = DATA_TO_ANY_EXIT,
0933 },
0934
0935 {
0936 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
0937 .bad_tosec = { INIT_SECTIONS, NULL },
0938 .mismatch = XXXINIT_TO_SOME_INIT,
0939 },
0940
0941 {
0942 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
0943 .bad_tosec = { EXIT_SECTIONS, NULL },
0944 .mismatch = XXXEXIT_TO_SOME_EXIT,
0945 },
0946
0947 {
0948 .fromsec = { ALL_INIT_SECTIONS, NULL },
0949 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
0950 .mismatch = ANY_INIT_TO_ANY_EXIT,
0951 },
0952
0953 {
0954 .fromsec = { ALL_EXIT_SECTIONS, NULL },
0955 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
0956 .mismatch = ANY_EXIT_TO_ANY_INIT,
0957 },
0958 {
0959 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
0960 .bad_tosec = { INIT_SECTIONS, NULL },
0961 .mismatch = ANY_INIT_TO_ANY_EXIT,
0962 },
0963
0964 {
0965 .fromsec = { "___ksymtab*", NULL },
0966 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
0967 .mismatch = EXPORT_TO_INIT_EXIT,
0968 },
0969 {
0970 .fromsec = { "__ex_table", NULL },
0971
0972
0973
0974 .bad_tosec = { ".altinstr_replacement", NULL },
0975 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
0976 .mismatch = EXTABLE_TO_NON_TEXT,
0977 .handler = extable_mismatch_handler,
0978 }
0979 };
0980
0981 static const struct sectioncheck *section_mismatch(
0982 const char *fromsec, const char *tosec)
0983 {
0984 int i;
0985
0986
0987
0988
0989
0990
0991
0992 if (*tosec == '\0')
0993 return NULL;
0994
0995 for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) {
0996 const struct sectioncheck *check = §ioncheck[i];
0997
0998 if (match(fromsec, check->fromsec)) {
0999 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1000 return check;
1001 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1002 return check;
1003 }
1004 }
1005 return NULL;
1006 }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 static int secref_whitelist(const struct sectioncheck *mismatch,
1061 const char *fromsec, const char *fromsym,
1062 const char *tosec, const char *tosym)
1063 {
1064
1065 if (match(tosec, init_data_sections) &&
1066 match(fromsec, data_sections) &&
1067 strstarts(fromsym, "__param"))
1068 return 0;
1069
1070
1071 if (strcmp(tosec, ".init.text") == 0 &&
1072 match(fromsec, data_sections) &&
1073 strstarts(fromsym, "__param_ops_"))
1074 return 0;
1075
1076
1077 if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
1078 match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
1079 match(fromsym, PATTERNS("*_template",
1080 "*_timer",
1081 "*_sht",
1082 "*_ops",
1083 "*_probe",
1084 "*_probe_one",
1085 "*_console")))
1086 return 0;
1087
1088
1089 if (match(fromsec, PATTERNS(DATA_SECTIONS)) &&
1090 match(tosec, PATTERNS(ALL_XXXINIT_SECTIONS, ALL_EXIT_SECTIONS)) &&
1091 match(fromsym, PATTERNS("*driver")))
1092 return 0;
1093
1094
1095 if (match(fromsec, head_sections) &&
1096 match(tosec, init_sections))
1097 return 0;
1098
1099
1100 if (match(tosym, linker_symbols))
1101 return 0;
1102
1103
1104 if (match(fromsec, text_sections) &&
1105 match(tosec, init_sections) &&
1106 match(fromsym, optim_symbols))
1107 return 0;
1108
1109
1110 if (strstarts(fromsym, ".L"))
1111 return 0;
1112
1113 return 1;
1114 }
1115
1116 static inline int is_arm_mapping_symbol(const char *str)
1117 {
1118 return str[0] == '$' &&
1119 (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x')
1120 && (str[2] == '\0' || str[2] == '.');
1121 }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1133 {
1134 const char *name = elf->strtab + sym->st_name;
1135
1136 if (!name || !strlen(name))
1137 return 0;
1138 return !is_arm_mapping_symbol(name);
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1149 Elf_Sym *relsym)
1150 {
1151 Elf_Sym *sym;
1152 Elf_Sym *near = NULL;
1153 Elf64_Sword distance = 20;
1154 Elf64_Sword d;
1155 unsigned int relsym_secindex;
1156
1157 if (relsym->st_name != 0)
1158 return relsym;
1159
1160 relsym_secindex = get_secindex(elf, relsym);
1161 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1162 if (get_secindex(elf, sym) != relsym_secindex)
1163 continue;
1164 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1165 continue;
1166 if (!is_valid_name(elf, sym))
1167 continue;
1168 if (sym->st_value == addr)
1169 return sym;
1170
1171 d = sym->st_value - addr;
1172 if (d < 0)
1173 d = addr - sym->st_value;
1174 if (d < distance) {
1175 distance = d;
1176 near = sym;
1177 }
1178 }
1179
1180 if (distance < 20)
1181 return near;
1182 else
1183 return NULL;
1184 }
1185
1186
1187
1188
1189
1190
1191
1192 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1193 const char *sec)
1194 {
1195 Elf_Sym *sym;
1196 Elf_Sym *near = NULL;
1197 Elf_Addr distance = ~0;
1198
1199 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1200 const char *symsec;
1201
1202 if (is_shndx_special(sym->st_shndx))
1203 continue;
1204 symsec = sec_name(elf, get_secindex(elf, sym));
1205 if (strcmp(symsec, sec) != 0)
1206 continue;
1207 if (!is_valid_name(elf, sym))
1208 continue;
1209 if (sym->st_value <= addr && addr - sym->st_value <= distance) {
1210 distance = addr - sym->st_value;
1211 near = sym;
1212 }
1213 }
1214 return near;
1215 }
1216
1217 static int is_function(Elf_Sym *sym)
1218 {
1219 if (sym)
1220 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1221 else
1222 return -1;
1223 }
1224
1225 static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1226 {
1227 switch (is_func) {
1228 case 0: *name = "variable"; *name_p = ""; break;
1229 case 1: *name = "function"; *name_p = "()"; break;
1230 default: *name = "(unknown reference)"; *name_p = ""; break;
1231 }
1232 }
1233
1234
1235
1236
1237
1238
1239 static void report_sec_mismatch(const char *modname,
1240 const struct sectioncheck *mismatch,
1241 const char *fromsec,
1242 const char *fromsym,
1243 const char *tosec, const char *tosym)
1244 {
1245 sec_mismatch_count++;
1246
1247 switch (mismatch->mismatch) {
1248 case TEXT_TO_ANY_INIT:
1249 case DATA_TO_ANY_INIT:
1250 case TEXT_TO_ANY_EXIT:
1251 case DATA_TO_ANY_EXIT:
1252 case XXXINIT_TO_SOME_INIT:
1253 case XXXEXIT_TO_SOME_EXIT:
1254 case ANY_INIT_TO_ANY_EXIT:
1255 case ANY_EXIT_TO_ANY_INIT:
1256 warn("%s: section mismatch in reference: %s (section: %s) -> %s (section: %s)\n",
1257 modname, fromsym, fromsec, tosym, tosec);
1258 break;
1259 case EXPORT_TO_INIT_EXIT:
1260 warn("%s: EXPORT_SYMBOL used for init/exit symbol: %s (section: %s)\n",
1261 modname, tosym, tosec);
1262 break;
1263 case EXTABLE_TO_NON_TEXT:
1264 fatal("There's a special handler for this mismatch type, we should never get here.\n");
1265 break;
1266 }
1267 }
1268
1269 static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1270 const struct sectioncheck* const mismatch,
1271 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1272 {
1273 const char *tosec;
1274 Elf_Sym *to;
1275 Elf_Sym *from;
1276 const char *tosym;
1277 const char *fromsym;
1278
1279 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1280 fromsym = sym_name(elf, from);
1281
1282 tosec = sec_name(elf, get_secindex(elf, sym));
1283 to = find_elf_symbol(elf, r->r_addend, sym);
1284 tosym = sym_name(elf, to);
1285
1286
1287 if (secref_whitelist(mismatch,
1288 fromsec, fromsym, tosec, tosym)) {
1289 report_sec_mismatch(modname, mismatch,
1290 fromsec, fromsym, tosec, tosym);
1291 }
1292 }
1293
1294 static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1295 {
1296 if (section_index > elf->num_sections)
1297 fatal("section_index is outside elf->num_sections!\n");
1298
1299 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1300 }
1301
1302
1303
1304
1305
1306 static unsigned int extable_entry_size = 0;
1307 static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
1308 {
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318 if (!extable_entry_size)
1319 extable_entry_size = r->r_offset * 2;
1320 }
1321
1322 static inline bool is_extable_fault_address(Elf_Rela *r)
1323 {
1324
1325
1326
1327
1328
1329 if (r->r_offset && extable_entry_size == 0)
1330 fatal("extable_entry size hasn't been discovered!\n");
1331
1332 return ((r->r_offset == 0) ||
1333 (r->r_offset % extable_entry_size == 0));
1334 }
1335
1336 #define is_second_extable_reloc(Start, Cur, Sec) \
1337 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1338
1339 static void report_extable_warnings(const char* modname, struct elf_info* elf,
1340 const struct sectioncheck* const mismatch,
1341 Elf_Rela* r, Elf_Sym* sym,
1342 const char* fromsec, const char* tosec)
1343 {
1344 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1345 const char* fromsym_name = sym_name(elf, fromsym);
1346 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1347 const char* tosym_name = sym_name(elf, tosym);
1348 const char* from_pretty_name;
1349 const char* from_pretty_name_p;
1350 const char* to_pretty_name;
1351 const char* to_pretty_name_p;
1352
1353 get_pretty_name(is_function(fromsym),
1354 &from_pretty_name, &from_pretty_name_p);
1355 get_pretty_name(is_function(tosym),
1356 &to_pretty_name, &to_pretty_name_p);
1357
1358 warn("%s(%s+0x%lx): Section mismatch in reference"
1359 " from the %s %s%s to the %s %s:%s%s\n",
1360 modname, fromsec, (long)r->r_offset, from_pretty_name,
1361 fromsym_name, from_pretty_name_p,
1362 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1363
1364 if (!match(tosec, mismatch->bad_tosec) &&
1365 is_executable_section(elf, get_secindex(elf, sym)))
1366 fprintf(stderr,
1367 "The relocation at %s+0x%lx references\n"
1368 "section \"%s\" which is not in the list of\n"
1369 "authorized sections. If you're adding a new section\n"
1370 "and/or if this reference is valid, add \"%s\" to the\n"
1371 "list of authorized sections to jump to on fault.\n"
1372 "This can be achieved by adding \"%s\" to \n"
1373 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1374 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1375 }
1376
1377 static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1378 const struct sectioncheck* const mismatch,
1379 Elf_Rela* r, Elf_Sym* sym,
1380 const char *fromsec)
1381 {
1382 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1383
1384 sec_mismatch_count++;
1385
1386 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
1387
1388 if (match(tosec, mismatch->bad_tosec))
1389 fatal("The relocation at %s+0x%lx references\n"
1390 "section \"%s\" which is black-listed.\n"
1391 "Something is seriously wrong and should be fixed.\n"
1392 "You might get more information about where this is\n"
1393 "coming from by using scripts/check_extable.sh %s\n",
1394 fromsec, (long)r->r_offset, tosec, modname);
1395 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1396 if (is_extable_fault_address(r))
1397 fatal("The relocation at %s+0x%lx references\n"
1398 "section \"%s\" which is not executable, IOW\n"
1399 "it is not possible for the kernel to fault\n"
1400 "at that address. Something is seriously wrong\n"
1401 "and should be fixed.\n",
1402 fromsec, (long)r->r_offset, tosec);
1403 else
1404 fatal("The relocation at %s+0x%lx references\n"
1405 "section \"%s\" which is not executable, IOW\n"
1406 "the kernel will fault if it ever tries to\n"
1407 "jump to it. Something is seriously wrong\n"
1408 "and should be fixed.\n",
1409 fromsec, (long)r->r_offset, tosec);
1410 }
1411 }
1412
1413 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1414 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1415 {
1416 const char *tosec = sec_name(elf, get_secindex(elf, sym));
1417 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
1418
1419 if (mismatch) {
1420 if (mismatch->handler)
1421 mismatch->handler(modname, elf, mismatch,
1422 r, sym, fromsec);
1423 else
1424 default_mismatch_handler(modname, elf, mismatch,
1425 r, sym, fromsec);
1426 }
1427 }
1428
1429 static unsigned int *reloc_location(struct elf_info *elf,
1430 Elf_Shdr *sechdr, Elf_Rela *r)
1431 {
1432 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
1433 }
1434
1435 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1436 {
1437 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1438 unsigned int *location = reloc_location(elf, sechdr, r);
1439
1440 switch (r_typ) {
1441 case R_386_32:
1442 r->r_addend = TO_NATIVE(*location);
1443 break;
1444 case R_386_PC32:
1445 r->r_addend = TO_NATIVE(*location) + 4;
1446 break;
1447 }
1448 return 0;
1449 }
1450
1451 #ifndef R_ARM_CALL
1452 #define R_ARM_CALL 28
1453 #endif
1454 #ifndef R_ARM_JUMP24
1455 #define R_ARM_JUMP24 29
1456 #endif
1457
1458 #ifndef R_ARM_THM_CALL
1459 #define R_ARM_THM_CALL 10
1460 #endif
1461 #ifndef R_ARM_THM_JUMP24
1462 #define R_ARM_THM_JUMP24 30
1463 #endif
1464 #ifndef R_ARM_THM_JUMP19
1465 #define R_ARM_THM_JUMP19 51
1466 #endif
1467
1468 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1469 {
1470 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1471
1472 switch (r_typ) {
1473 case R_ARM_ABS32:
1474
1475 r->r_addend = (int)(long)
1476 (elf->symtab_start + ELF_R_SYM(r->r_info));
1477 break;
1478 case R_ARM_PC24:
1479 case R_ARM_CALL:
1480 case R_ARM_JUMP24:
1481 case R_ARM_THM_CALL:
1482 case R_ARM_THM_JUMP24:
1483 case R_ARM_THM_JUMP19:
1484
1485 r->r_addend = (int)(long)(elf->hdr +
1486 sechdr->sh_offset +
1487 (r->r_offset - sechdr->sh_addr));
1488 break;
1489 default:
1490 return 1;
1491 }
1492 return 0;
1493 }
1494
1495 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1496 {
1497 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1498 unsigned int *location = reloc_location(elf, sechdr, r);
1499 unsigned int inst;
1500
1501 if (r_typ == R_MIPS_HI16)
1502 return 1;
1503 inst = TO_NATIVE(*location);
1504 switch (r_typ) {
1505 case R_MIPS_LO16:
1506 r->r_addend = inst & 0xffff;
1507 break;
1508 case R_MIPS_26:
1509 r->r_addend = (inst & 0x03ffffff) << 2;
1510 break;
1511 case R_MIPS_32:
1512 r->r_addend = inst;
1513 break;
1514 }
1515 return 0;
1516 }
1517
1518 #ifndef EM_RISCV
1519 #define EM_RISCV 243
1520 #endif
1521
1522 #ifndef R_RISCV_SUB32
1523 #define R_RISCV_SUB32 39
1524 #endif
1525
1526 static void section_rela(const char *modname, struct elf_info *elf,
1527 Elf_Shdr *sechdr)
1528 {
1529 Elf_Sym *sym;
1530 Elf_Rela *rela;
1531 Elf_Rela r;
1532 unsigned int r_sym;
1533 const char *fromsec;
1534
1535 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1536 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1537
1538 fromsec = sec_name(elf, sechdr->sh_info);
1539
1540 if (match(fromsec, section_white_list))
1541 return;
1542
1543 for (rela = start; rela < stop; rela++) {
1544 r.r_offset = TO_NATIVE(rela->r_offset);
1545 #if KERNEL_ELFCLASS == ELFCLASS64
1546 if (elf->hdr->e_machine == EM_MIPS) {
1547 unsigned int r_typ;
1548 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1549 r_sym = TO_NATIVE(r_sym);
1550 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1551 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1552 } else {
1553 r.r_info = TO_NATIVE(rela->r_info);
1554 r_sym = ELF_R_SYM(r.r_info);
1555 }
1556 #else
1557 r.r_info = TO_NATIVE(rela->r_info);
1558 r_sym = ELF_R_SYM(r.r_info);
1559 #endif
1560 r.r_addend = TO_NATIVE(rela->r_addend);
1561 switch (elf->hdr->e_machine) {
1562 case EM_RISCV:
1563 if (!strcmp("__ex_table", fromsec) &&
1564 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1565 continue;
1566 break;
1567 }
1568 sym = elf->symtab_start + r_sym;
1569
1570 if (is_shndx_special(sym->st_shndx))
1571 continue;
1572 if (is_second_extable_reloc(start, rela, fromsec))
1573 find_extable_entry_size(fromsec, &r);
1574 check_section_mismatch(modname, elf, &r, sym, fromsec);
1575 }
1576 }
1577
1578 static void section_rel(const char *modname, struct elf_info *elf,
1579 Elf_Shdr *sechdr)
1580 {
1581 Elf_Sym *sym;
1582 Elf_Rel *rel;
1583 Elf_Rela r;
1584 unsigned int r_sym;
1585 const char *fromsec;
1586
1587 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1588 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1589
1590 fromsec = sec_name(elf, sechdr->sh_info);
1591
1592 if (match(fromsec, section_white_list))
1593 return;
1594
1595 for (rel = start; rel < stop; rel++) {
1596 r.r_offset = TO_NATIVE(rel->r_offset);
1597 #if KERNEL_ELFCLASS == ELFCLASS64
1598 if (elf->hdr->e_machine == EM_MIPS) {
1599 unsigned int r_typ;
1600 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1601 r_sym = TO_NATIVE(r_sym);
1602 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1603 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1604 } else {
1605 r.r_info = TO_NATIVE(rel->r_info);
1606 r_sym = ELF_R_SYM(r.r_info);
1607 }
1608 #else
1609 r.r_info = TO_NATIVE(rel->r_info);
1610 r_sym = ELF_R_SYM(r.r_info);
1611 #endif
1612 r.r_addend = 0;
1613 switch (elf->hdr->e_machine) {
1614 case EM_386:
1615 if (addend_386_rel(elf, sechdr, &r))
1616 continue;
1617 break;
1618 case EM_ARM:
1619 if (addend_arm_rel(elf, sechdr, &r))
1620 continue;
1621 break;
1622 case EM_MIPS:
1623 if (addend_mips_rel(elf, sechdr, &r))
1624 continue;
1625 break;
1626 }
1627 sym = elf->symtab_start + r_sym;
1628
1629 if (is_shndx_special(sym->st_shndx))
1630 continue;
1631 if (is_second_extable_reloc(start, rel, fromsec))
1632 find_extable_entry_size(fromsec, &r);
1633 check_section_mismatch(modname, elf, &r, sym, fromsec);
1634 }
1635 }
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649 static void check_sec_ref(const char *modname, struct elf_info *elf)
1650 {
1651 int i;
1652 Elf_Shdr *sechdrs = elf->sechdrs;
1653
1654
1655 for (i = 0; i < elf->num_sections; i++) {
1656 check_section(modname, elf, &elf->sechdrs[i]);
1657
1658 if (sechdrs[i].sh_type == SHT_RELA)
1659 section_rela(modname, elf, &elf->sechdrs[i]);
1660 else if (sechdrs[i].sh_type == SHT_REL)
1661 section_rel(modname, elf, &elf->sechdrs[i]);
1662 }
1663 }
1664
1665 static char *remove_dot(char *s)
1666 {
1667 size_t n = strcspn(s, ".");
1668
1669 if (n && s[n]) {
1670 size_t m = strspn(s + n + 1, "0123456789");
1671 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0))
1672 s[n] = 0;
1673 }
1674 return s;
1675 }
1676
1677
1678
1679
1680
1681 static void extract_crcs_for_object(const char *object, struct module *mod)
1682 {
1683 char cmd_file[PATH_MAX];
1684 char *buf, *p;
1685 const char *base;
1686 int dirlen, ret;
1687
1688 base = strrchr(object, '/');
1689 if (base) {
1690 base++;
1691 dirlen = base - object;
1692 } else {
1693 dirlen = 0;
1694 base = object;
1695 }
1696
1697 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
1698 dirlen, object, base);
1699 if (ret >= sizeof(cmd_file)) {
1700 error("%s: too long path was truncated\n", cmd_file);
1701 return;
1702 }
1703
1704 buf = read_text_file(cmd_file);
1705 p = buf;
1706
1707 while ((p = strstr(p, "\n#SYMVER "))) {
1708 char *name;
1709 size_t namelen;
1710 unsigned int crc;
1711 struct symbol *sym;
1712
1713 name = p + strlen("\n#SYMVER ");
1714
1715 p = strchr(name, ' ');
1716 if (!p)
1717 break;
1718
1719 namelen = p - name;
1720 p++;
1721
1722 if (!isdigit(*p))
1723 continue;
1724
1725 crc = strtol(p, &p, 0);
1726 if (*p != '\n')
1727 continue;
1728
1729 name[namelen] = '\0';
1730
1731
1732
1733
1734
1735
1736
1737 sym = sym_find_with_module(name, mod);
1738 if (sym)
1739 sym_set_crc(sym, crc);
1740 }
1741
1742 free(buf);
1743 }
1744
1745
1746
1747
1748
1749 static void mod_set_crcs(struct module *mod)
1750 {
1751 char objlist[PATH_MAX];
1752 char *buf, *p, *obj;
1753 int ret;
1754
1755 if (mod->is_vmlinux) {
1756 strcpy(objlist, ".vmlinux.objs");
1757 } else {
1758
1759 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
1760 if (ret >= sizeof(objlist)) {
1761 error("%s: too long path was truncated\n", objlist);
1762 return;
1763 }
1764 }
1765
1766 buf = read_text_file(objlist);
1767 p = buf;
1768
1769 while ((obj = strsep(&p, "\n")) && obj[0])
1770 extract_crcs_for_object(obj, mod);
1771
1772 free(buf);
1773 }
1774
1775 static void read_symbols(const char *modname)
1776 {
1777 const char *symname;
1778 char *version;
1779 char *license;
1780 char *namespace;
1781 struct module *mod;
1782 struct elf_info info = { };
1783 Elf_Sym *sym;
1784
1785 if (!parse_elf(&info, modname))
1786 return;
1787
1788 if (!strends(modname, ".o")) {
1789 error("%s: filename must be suffixed with .o\n", modname);
1790 return;
1791 }
1792
1793
1794 mod = new_module(modname, strlen(modname) - strlen(".o"));
1795
1796 if (!mod->is_vmlinux) {
1797 license = get_modinfo(&info, "license");
1798 if (!license)
1799 error("missing MODULE_LICENSE() in %s\n", modname);
1800 while (license) {
1801 if (!license_is_gpl_compatible(license)) {
1802 mod->is_gpl_compatible = false;
1803 break;
1804 }
1805 license = get_next_modinfo(&info, "license", license);
1806 }
1807
1808 namespace = get_modinfo(&info, "import_ns");
1809 while (namespace) {
1810 add_namespace(&mod->imported_namespaces, namespace);
1811 namespace = get_next_modinfo(&info, "import_ns",
1812 namespace);
1813 }
1814 }
1815
1816 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1817 symname = remove_dot(info.strtab + sym->st_name);
1818
1819 handle_symbol(mod, &info, sym, symname);
1820 handle_moddevtable(mod, &info, sym, symname);
1821 }
1822
1823 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1824 symname = remove_dot(info.strtab + sym->st_name);
1825
1826
1827 if (strstarts(symname, "__kstrtabns_"))
1828 sym_update_namespace(symname + strlen("__kstrtabns_"),
1829 sym_get_data(&info, sym));
1830 }
1831
1832 check_sec_ref(modname, &info);
1833
1834 if (!mod->is_vmlinux) {
1835 version = get_modinfo(&info, "version");
1836 if (version || all_versions)
1837 get_src_version(mod->name, mod->srcversion,
1838 sizeof(mod->srcversion) - 1);
1839 }
1840
1841 parse_elf_finish(&info);
1842
1843 if (modversions) {
1844
1845
1846
1847
1848
1849
1850 sym_add_unresolved("module_layout", mod, false);
1851
1852 mod_set_crcs(mod);
1853 }
1854 }
1855
1856 static void read_symbols_from_files(const char *filename)
1857 {
1858 FILE *in = stdin;
1859 char fname[PATH_MAX];
1860
1861 if (strcmp(filename, "-") != 0) {
1862 in = fopen(filename, "r");
1863 if (!in)
1864 fatal("Can't open filenames file %s: %m", filename);
1865 }
1866
1867 while (fgets(fname, PATH_MAX, in) != NULL) {
1868 if (strends(fname, "\n"))
1869 fname[strlen(fname)-1] = '\0';
1870 read_symbols(fname);
1871 }
1872
1873 if (in != stdin)
1874 fclose(in);
1875 }
1876
1877 #define SZ 500
1878
1879
1880
1881
1882
1883 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1884 const char *fmt, ...)
1885 {
1886 char tmp[SZ];
1887 int len;
1888 va_list ap;
1889
1890 va_start(ap, fmt);
1891 len = vsnprintf(tmp, SZ, fmt, ap);
1892 buf_write(buf, tmp, len);
1893 va_end(ap);
1894 }
1895
1896 void buf_write(struct buffer *buf, const char *s, int len)
1897 {
1898 if (buf->size - buf->pos < len) {
1899 buf->size += len + SZ;
1900 buf->p = NOFAIL(realloc(buf->p, buf->size));
1901 }
1902 strncpy(buf->p + buf->pos, s, len);
1903 buf->pos += len;
1904 }
1905
1906 static void check_exports(struct module *mod)
1907 {
1908 struct symbol *s, *exp;
1909
1910 list_for_each_entry(s, &mod->unresolved_symbols, list) {
1911 const char *basename;
1912 exp = find_symbol(s->name);
1913 if (!exp) {
1914 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
1915 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
1916 "\"%s\" [%s.ko] undefined!\n",
1917 s->name, mod->name);
1918 continue;
1919 }
1920 if (exp->module == mod) {
1921 error("\"%s\" [%s.ko] was exported without definition\n",
1922 s->name, mod->name);
1923 continue;
1924 }
1925
1926 s->module = exp->module;
1927 s->crc_valid = exp->crc_valid;
1928 s->crc = exp->crc;
1929
1930 basename = strrchr(mod->name, '/');
1931 if (basename)
1932 basename++;
1933 else
1934 basename = mod->name;
1935
1936 if (exp->namespace &&
1937 !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
1938 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
1939 "module %s uses symbol %s from namespace %s, but does not import it.\n",
1940 basename, exp->name, exp->namespace);
1941 add_namespace(&mod->missing_namespaces, exp->namespace);
1942 }
1943
1944 if (!mod->is_gpl_compatible && exp->is_gpl_only)
1945 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
1946 basename, exp->name);
1947 }
1948 }
1949
1950 static void check_modname_len(struct module *mod)
1951 {
1952 const char *mod_name;
1953
1954 mod_name = strrchr(mod->name, '/');
1955 if (mod_name == NULL)
1956 mod_name = mod->name;
1957 else
1958 mod_name++;
1959 if (strlen(mod_name) >= MODULE_NAME_LEN)
1960 error("module name is too long [%s.ko]\n", mod->name);
1961 }
1962
1963
1964
1965
1966 static void add_header(struct buffer *b, struct module *mod)
1967 {
1968 buf_printf(b, "#include <linux/module.h>\n");
1969
1970
1971
1972
1973 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
1974 buf_printf(b, "#include <linux/build-salt.h>\n");
1975 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
1976 buf_printf(b, "#include <linux/export-internal.h>\n");
1977 buf_printf(b, "#include <linux/vermagic.h>\n");
1978 buf_printf(b, "#include <linux/compiler.h>\n");
1979 buf_printf(b, "\n");
1980 buf_printf(b, "BUILD_SALT;\n");
1981 buf_printf(b, "BUILD_LTO_INFO;\n");
1982 buf_printf(b, "\n");
1983 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1984 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
1985 buf_printf(b, "\n");
1986 buf_printf(b, "__visible struct module __this_module\n");
1987 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
1988 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
1989 if (mod->has_init)
1990 buf_printf(b, "\t.init = init_module,\n");
1991 if (mod->has_cleanup)
1992 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1993 "\t.exit = cleanup_module,\n"
1994 "#endif\n");
1995 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
1996 buf_printf(b, "};\n");
1997
1998 if (!external_module)
1999 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2000
2001 buf_printf(b,
2002 "\n"
2003 "#ifdef CONFIG_RETPOLINE\n"
2004 "MODULE_INFO(retpoline, \"Y\");\n"
2005 "#endif\n");
2006
2007 if (strstarts(mod->name, "drivers/staging"))
2008 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2009
2010 if (strstarts(mod->name, "tools/testing"))
2011 buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
2012 }
2013
2014 static void add_exported_symbols(struct buffer *buf, struct module *mod)
2015 {
2016 struct symbol *sym;
2017
2018 if (!modversions)
2019 return;
2020
2021
2022 buf_printf(buf, "\n");
2023 list_for_each_entry(sym, &mod->exported_symbols, list) {
2024 if (!sym->crc_valid)
2025 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
2026 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
2027 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
2028 sym->name);
2029
2030 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
2031 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
2032 }
2033 }
2034
2035
2036
2037
2038 static void add_versions(struct buffer *b, struct module *mod)
2039 {
2040 struct symbol *s;
2041
2042 if (!modversions)
2043 return;
2044
2045 buf_printf(b, "\n");
2046 buf_printf(b, "static const struct modversion_info ____versions[]\n");
2047 buf_printf(b, "__used __section(\"__versions\") = {\n");
2048
2049 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2050 if (!s->module)
2051 continue;
2052 if (!s->crc_valid) {
2053 warn("\"%s\" [%s.ko] has no CRC!\n",
2054 s->name, mod->name);
2055 continue;
2056 }
2057 if (strlen(s->name) >= MODULE_NAME_LEN) {
2058 error("too long symbol \"%s\" [%s.ko]\n",
2059 s->name, mod->name);
2060 break;
2061 }
2062 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
2063 s->crc, s->name);
2064 }
2065
2066 buf_printf(b, "};\n");
2067 }
2068
2069 static void add_depends(struct buffer *b, struct module *mod)
2070 {
2071 struct symbol *s;
2072 int first = 1;
2073
2074
2075 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2076 if (s->module)
2077 s->module->seen = s->module->is_vmlinux;
2078 }
2079
2080 buf_printf(b, "\n");
2081 buf_printf(b, "MODULE_INFO(depends, \"");
2082 list_for_each_entry(s, &mod->unresolved_symbols, list) {
2083 const char *p;
2084 if (!s->module)
2085 continue;
2086
2087 if (s->module->seen)
2088 continue;
2089
2090 s->module->seen = true;
2091 p = strrchr(s->module->name, '/');
2092 if (p)
2093 p++;
2094 else
2095 p = s->module->name;
2096 buf_printf(b, "%s%s", first ? "" : ",", p);
2097 first = 0;
2098 }
2099 buf_printf(b, "\");\n");
2100 }
2101
2102 static void add_srcversion(struct buffer *b, struct module *mod)
2103 {
2104 if (mod->srcversion[0]) {
2105 buf_printf(b, "\n");
2106 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2107 mod->srcversion);
2108 }
2109 }
2110
2111 static void write_buf(struct buffer *b, const char *fname)
2112 {
2113 FILE *file;
2114
2115 if (error_occurred)
2116 return;
2117
2118 file = fopen(fname, "w");
2119 if (!file) {
2120 perror(fname);
2121 exit(1);
2122 }
2123 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2124 perror(fname);
2125 exit(1);
2126 }
2127 if (fclose(file) != 0) {
2128 perror(fname);
2129 exit(1);
2130 }
2131 }
2132
2133 static void write_if_changed(struct buffer *b, const char *fname)
2134 {
2135 char *tmp;
2136 FILE *file;
2137 struct stat st;
2138
2139 file = fopen(fname, "r");
2140 if (!file)
2141 goto write;
2142
2143 if (fstat(fileno(file), &st) < 0)
2144 goto close_write;
2145
2146 if (st.st_size != b->pos)
2147 goto close_write;
2148
2149 tmp = NOFAIL(malloc(b->pos));
2150 if (fread(tmp, 1, b->pos, file) != b->pos)
2151 goto free_write;
2152
2153 if (memcmp(tmp, b->p, b->pos) != 0)
2154 goto free_write;
2155
2156 free(tmp);
2157 fclose(file);
2158 return;
2159
2160 free_write:
2161 free(tmp);
2162 close_write:
2163 fclose(file);
2164 write:
2165 write_buf(b, fname);
2166 }
2167
2168 static void write_vmlinux_export_c_file(struct module *mod)
2169 {
2170 struct buffer buf = { };
2171
2172 buf_printf(&buf,
2173 "#include <linux/export-internal.h>\n");
2174
2175 add_exported_symbols(&buf, mod);
2176 write_if_changed(&buf, ".vmlinux.export.c");
2177 free(buf.p);
2178 }
2179
2180
2181 static void write_mod_c_file(struct module *mod)
2182 {
2183 struct buffer buf = { };
2184 char fname[PATH_MAX];
2185 int ret;
2186
2187 check_modname_len(mod);
2188 check_exports(mod);
2189
2190 add_header(&buf, mod);
2191 add_exported_symbols(&buf, mod);
2192 add_versions(&buf, mod);
2193 add_depends(&buf, mod);
2194 add_moddevtable(&buf, mod);
2195 add_srcversion(&buf, mod);
2196
2197 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
2198 if (ret >= sizeof(fname)) {
2199 error("%s: too long path was truncated\n", fname);
2200 goto free;
2201 }
2202
2203 write_if_changed(&buf, fname);
2204
2205 free:
2206 free(buf.p);
2207 }
2208
2209
2210
2211
2212 static void read_dump(const char *fname)
2213 {
2214 char *buf, *pos, *line;
2215
2216 buf = read_text_file(fname);
2217 if (!buf)
2218
2219 return;
2220
2221 pos = buf;
2222
2223 while ((line = get_line(&pos))) {
2224 char *symname, *namespace, *modname, *d, *export;
2225 unsigned int crc;
2226 struct module *mod;
2227 struct symbol *s;
2228 bool gpl_only;
2229
2230 if (!(symname = strchr(line, '\t')))
2231 goto fail;
2232 *symname++ = '\0';
2233 if (!(modname = strchr(symname, '\t')))
2234 goto fail;
2235 *modname++ = '\0';
2236 if (!(export = strchr(modname, '\t')))
2237 goto fail;
2238 *export++ = '\0';
2239 if (!(namespace = strchr(export, '\t')))
2240 goto fail;
2241 *namespace++ = '\0';
2242
2243 crc = strtoul(line, &d, 16);
2244 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2245 goto fail;
2246
2247 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2248 gpl_only = true;
2249 } else if (!strcmp(export, "EXPORT_SYMBOL")) {
2250 gpl_only = false;
2251 } else {
2252 error("%s: unknown license %s. skip", symname, export);
2253 continue;
2254 }
2255
2256 mod = find_module(modname);
2257 if (!mod) {
2258 mod = new_module(modname, strlen(modname));
2259 mod->from_dump = true;
2260 }
2261 s = sym_add_exported(symname, mod, gpl_only);
2262 sym_set_crc(s, crc);
2263 sym_update_namespace(symname, namespace);
2264 }
2265 free(buf);
2266 return;
2267 fail:
2268 free(buf);
2269 fatal("parse error in symbol dump file\n");
2270 }
2271
2272 static void write_dump(const char *fname)
2273 {
2274 struct buffer buf = { };
2275 struct module *mod;
2276 struct symbol *sym;
2277
2278 list_for_each_entry(mod, &modules, list) {
2279 if (mod->from_dump)
2280 continue;
2281 list_for_each_entry(sym, &mod->exported_symbols, list) {
2282 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
2283 sym->crc, sym->name, mod->name,
2284 sym->is_gpl_only ? "_GPL" : "",
2285 sym->namespace ?: "");
2286 }
2287 }
2288 write_buf(&buf, fname);
2289 free(buf.p);
2290 }
2291
2292 static void write_namespace_deps_files(const char *fname)
2293 {
2294 struct module *mod;
2295 struct namespace_list *ns;
2296 struct buffer ns_deps_buf = {};
2297
2298 list_for_each_entry(mod, &modules, list) {
2299
2300 if (mod->from_dump || list_empty(&mod->missing_namespaces))
2301 continue;
2302
2303 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
2304
2305 list_for_each_entry(ns, &mod->missing_namespaces, list)
2306 buf_printf(&ns_deps_buf, " %s", ns->namespace);
2307
2308 buf_printf(&ns_deps_buf, "\n");
2309 }
2310
2311 write_if_changed(&ns_deps_buf, fname);
2312 free(ns_deps_buf.p);
2313 }
2314
2315 struct dump_list {
2316 struct list_head list;
2317 const char *file;
2318 };
2319
2320 int main(int argc, char **argv)
2321 {
2322 struct module *mod;
2323 char *missing_namespace_deps = NULL;
2324 char *dump_write = NULL, *files_source = NULL;
2325 int opt;
2326 LIST_HEAD(dump_lists);
2327 struct dump_list *dl, *dl2;
2328
2329 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
2330 switch (opt) {
2331 case 'e':
2332 external_module = true;
2333 break;
2334 case 'i':
2335 dl = NOFAIL(malloc(sizeof(*dl)));
2336 dl->file = optarg;
2337 list_add_tail(&dl->list, &dump_lists);
2338 break;
2339 case 'm':
2340 modversions = true;
2341 break;
2342 case 'n':
2343 ignore_missing_files = true;
2344 break;
2345 case 'o':
2346 dump_write = optarg;
2347 break;
2348 case 'a':
2349 all_versions = true;
2350 break;
2351 case 'T':
2352 files_source = optarg;
2353 break;
2354 case 'w':
2355 warn_unresolved = true;
2356 break;
2357 case 'E':
2358 sec_mismatch_warn_only = false;
2359 break;
2360 case 'N':
2361 allow_missing_ns_imports = true;
2362 break;
2363 case 'd':
2364 missing_namespace_deps = optarg;
2365 break;
2366 default:
2367 exit(1);
2368 }
2369 }
2370
2371 list_for_each_entry_safe(dl, dl2, &dump_lists, list) {
2372 read_dump(dl->file);
2373 list_del(&dl->list);
2374 free(dl);
2375 }
2376
2377 while (optind < argc)
2378 read_symbols(argv[optind++]);
2379
2380 if (files_source)
2381 read_symbols_from_files(files_source);
2382
2383 list_for_each_entry(mod, &modules, list) {
2384 if (mod->from_dump)
2385 continue;
2386
2387 if (mod->is_vmlinux)
2388 write_vmlinux_export_c_file(mod);
2389 else
2390 write_mod_c_file(mod);
2391 }
2392
2393 if (missing_namespace_deps)
2394 write_namespace_deps_files(missing_namespace_deps);
2395
2396 if (dump_write)
2397 write_dump(dump_write);
2398 if (sec_mismatch_count && !sec_mismatch_warn_only)
2399 error("Section mismatches detected.\n"
2400 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2401
2402 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2403 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2404 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2405
2406 return error_occurred ? 1 : 0;
2407 }