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