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