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