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