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