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 if (sym) 701 return elf->strtab + sym->st_name; 702 else 703 return "(unknown)"; 704 } 705 706 /* 707 * Check whether the 'string' argument matches one of the 'patterns', 708 * an array of shell wildcard patterns (glob). 709 * 710 * Return true is there is a match. 711 */ 712 static bool match(const char *string, const char *const patterns[]) 713 { 714 const char *pattern; 715 716 while ((pattern = *patterns++)) { 717 if (!fnmatch(pattern, string, 0)) 718 return true; 719 } 720 721 return false; 722 } 723 724 /* useful to pass patterns to match() directly */ 725 #define PATTERNS(...) \ 726 ({ \ 727 static const char *const patterns[] = {__VA_ARGS__, NULL}; \ 728 patterns; \ 729 }) 730 731 /* sections that we do not want to do full section mismatch check on */ 732 static const char *const section_white_list[] = 733 { 734 ".comment*", 735 ".debug*", 736 ".zdebug*", /* Compressed debug sections. */ 737 ".GCC.command.line", /* record-gcc-switches */ 738 ".mdebug*", /* alpha, score, mips etc. */ 739 ".pdr", /* alpha, score, mips etc. */ 740 ".stab*", 741 ".note*", 742 ".got*", 743 ".toc*", 744 ".xt.prop", /* xtensa */ 745 ".xt.lit", /* xtensa */ 746 ".arcextmap*", /* arc */ 747 ".gnu.linkonce.arcext*", /* arc : modules */ 748 ".cmem*", /* EZchip */ 749 ".fmt_slot*", /* EZchip */ 750 ".gnu.lto*", 751 ".discard.*", 752 ".llvm.call-graph-profile", /* call graph */ 753 NULL 754 }; 755 756 /* 757 * This is used to find sections missing the SHF_ALLOC flag. 758 * The cause of this is often a section specified in assembler 759 * without "ax" / "aw". 760 */ 761 static void check_section(const char *modname, struct elf_info *elf, 762 Elf_Shdr *sechdr) 763 { 764 const char *sec = sech_name(elf, sechdr); 765 766 if (sechdr->sh_type == SHT_PROGBITS && 767 !(sechdr->sh_flags & SHF_ALLOC) && 768 !match(sec, section_white_list)) { 769 warn("%s (%s): unexpected non-allocatable section.\n" 770 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n" 771 "Note that for example <linux/init.h> contains\n" 772 "section definitions for use in .S files.\n\n", 773 modname, sec); 774 } 775 } 776 777 778 779 #define ALL_INIT_DATA_SECTIONS \ 780 ".init.setup", ".init.rodata", ".init.data" 781 782 #define ALL_PCI_INIT_SECTIONS \ 783 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \ 784 ".pci_fixup_enable", ".pci_fixup_resume", \ 785 ".pci_fixup_resume_early", ".pci_fixup_suspend" 786 787 #define ALL_INIT_SECTIONS ".init.*" 788 #define ALL_EXIT_SECTIONS ".exit.*" 789 790 #define DATA_SECTIONS ".data", ".data.rel" 791 #define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \ 792 ".kprobes.text", ".cpuidle.text", ".noinstr.text", \ 793 ".ltext", ".ltext.*" 794 #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ 795 ".fixup", ".entry.text", ".exception.text", \ 796 ".coldtext", ".softirqentry.text" 797 798 #define ALL_TEXT_SECTIONS ".init.text", ".exit.text", \ 799 TEXT_SECTIONS, OTHER_TEXT_SECTIONS 800 801 enum mismatch { 802 TEXTDATA_TO_ANY_INIT_EXIT, 803 XXXINIT_TO_SOME_INIT, 804 ANY_INIT_TO_ANY_EXIT, 805 ANY_EXIT_TO_ANY_INIT, 806 EXTABLE_TO_NON_TEXT, 807 }; 808 809 /** 810 * Describe how to match sections on different criteria: 811 * 812 * @fromsec: Array of sections to be matched. 813 * 814 * @bad_tosec: Relocations applied to a section in @fromsec to a section in 815 * this array is forbidden (black-list). Can be empty. 816 * 817 * @good_tosec: Relocations applied to a section in @fromsec must be 818 * targeting sections in this array (white-list). Can be empty. 819 * 820 * @mismatch: Type of mismatch. 821 */ 822 struct sectioncheck { 823 const char *fromsec[20]; 824 const char *bad_tosec[20]; 825 const char *good_tosec[20]; 826 enum mismatch mismatch; 827 }; 828 829 static const struct sectioncheck sectioncheck[] = { 830 /* Do not reference init/exit code/data from 831 * normal code and data 832 */ 833 { 834 .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL }, 835 .bad_tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }, 836 .mismatch = TEXTDATA_TO_ANY_INIT_EXIT, 837 }, 838 /* Do not use exit code/data from init code */ 839 { 840 .fromsec = { ALL_INIT_SECTIONS, NULL }, 841 .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, 842 .mismatch = ANY_INIT_TO_ANY_EXIT, 843 }, 844 /* Do not use init code/data from exit code */ 845 { 846 .fromsec = { ALL_EXIT_SECTIONS, NULL }, 847 .bad_tosec = { ALL_INIT_SECTIONS, NULL }, 848 .mismatch = ANY_EXIT_TO_ANY_INIT, 849 }, 850 { 851 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL }, 852 .bad_tosec = { ALL_INIT_SECTIONS, NULL }, 853 .mismatch = ANY_INIT_TO_ANY_EXIT, 854 }, 855 { 856 .fromsec = { "__ex_table", NULL }, 857 /* If you're adding any new black-listed sections in here, consider 858 * adding a special 'printer' for them in scripts/check_extable. 859 */ 860 .bad_tosec = { ".altinstr_replacement", NULL }, 861 .good_tosec = {ALL_TEXT_SECTIONS , NULL}, 862 .mismatch = EXTABLE_TO_NON_TEXT, 863 } 864 }; 865 866 static const struct sectioncheck *section_mismatch( 867 const char *fromsec, const char *tosec) 868 { 869 int i; 870 871 /* 872 * The target section could be the SHT_NUL section when we're 873 * handling relocations to un-resolved symbols, trying to match it 874 * doesn't make much sense and causes build failures on parisc 875 * architectures. 876 */ 877 if (*tosec == '\0') 878 return NULL; 879 880 for (i = 0; i < ARRAY_SIZE(sectioncheck); i++) { 881 const struct sectioncheck *check = §ioncheck[i]; 882 883 if (match(fromsec, check->fromsec)) { 884 if (check->bad_tosec[0] && match(tosec, check->bad_tosec)) 885 return check; 886 if (check->good_tosec[0] && !match(tosec, check->good_tosec)) 887 return check; 888 } 889 } 890 return NULL; 891 } 892 893 /** 894 * Whitelist to allow certain references to pass with no warning. 895 * 896 * Pattern 1: 897 * If a module parameter is declared __initdata and permissions=0 898 * then this is legal despite the warning generated. 899 * We cannot see value of permissions here, so just ignore 900 * this pattern. 901 * The pattern is identified by: 902 * tosec = .init.data 903 * fromsec = .data* 904 * atsym =__param* 905 * 906 * Pattern 1a: 907 * module_param_call() ops can refer to __init set function if permissions=0 908 * The pattern is identified by: 909 * tosec = .init.text 910 * fromsec = .data* 911 * atsym = __param_ops_* 912 * 913 * Pattern 3: 914 * Whitelist all references from .head.text to any init section 915 * 916 * Pattern 4: 917 * Some symbols belong to init section but still it is ok to reference 918 * these from non-init sections as these symbols don't have any memory 919 * allocated for them and symbol address and value are same. So even 920 * if init section is freed, its ok to reference those symbols. 921 * For ex. symbols marking the init section boundaries. 922 * This pattern is identified by 923 * refsymname = __init_begin, _sinittext, _einittext 924 * 925 * Pattern 5: 926 * GCC may optimize static inlines when fed constant arg(s) resulting 927 * in functions like cpumask_empty() -- generating an associated symbol 928 * cpumask_empty.constprop.3 that appears in the audit. If the const that 929 * is passed in comes from __init, like say nmi_ipi_mask, we get a 930 * meaningless section warning. May need to add isra symbols too... 931 * This pattern is identified by 932 * tosec = init section 933 * fromsec = text section 934 * refsymname = *.constprop.* 935 * 936 **/ 937 static int secref_whitelist(const char *fromsec, const char *fromsym, 938 const char *tosec, const char *tosym) 939 { 940 /* Check for pattern 1 */ 941 if (match(tosec, PATTERNS(ALL_INIT_DATA_SECTIONS)) && 942 match(fromsec, PATTERNS(DATA_SECTIONS)) && 943 strstarts(fromsym, "__param")) 944 return 0; 945 946 /* Check for pattern 1a */ 947 if (strcmp(tosec, ".init.text") == 0 && 948 match(fromsec, PATTERNS(DATA_SECTIONS)) && 949 strstarts(fromsym, "__param_ops_")) 950 return 0; 951 952 /* symbols in data sections that may refer to any init/exit sections */ 953 if (match(fromsec, PATTERNS(DATA_SECTIONS)) && 954 match(tosec, PATTERNS(ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS)) && 955 match(fromsym, PATTERNS("*_ops", "*_probe", "*_console"))) 956 return 0; 957 958 /* Check for pattern 3 */ 959 if (strstarts(fromsec, ".head.text") && 960 match(tosec, PATTERNS(ALL_INIT_SECTIONS))) 961 return 0; 962 963 /* Check for pattern 4 */ 964 if (match(tosym, PATTERNS("__init_begin", "_sinittext", "_einittext"))) 965 return 0; 966 967 /* Check for pattern 5 */ 968 if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) && 969 match(tosec, PATTERNS(ALL_INIT_SECTIONS)) && 970 match(fromsym, PATTERNS("*.constprop.*"))) 971 return 0; 972 973 return 1; 974 } 975 976 static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr, 977 unsigned int secndx) 978 { 979 return symsearch_find_nearest(elf, addr, secndx, false, ~0); 980 } 981 982 static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym) 983 { 984 Elf_Sym *new_sym; 985 986 /* If the supplied symbol has a valid name, return it */ 987 if (is_valid_name(elf, sym)) 988 return sym; 989 990 /* 991 * Strive to find a better symbol name, but the resulting name may not 992 * match the symbol referenced in the original code. 993 */ 994 new_sym = symsearch_find_nearest(elf, addr, get_secindex(elf, sym), 995 true, 20); 996 return new_sym ? new_sym : sym; 997 } 998 999 static bool is_executable_section(struct elf_info *elf, unsigned int secndx) 1000 { 1001 if (secndx >= elf->num_sections) 1002 return false; 1003 1004 return (elf->sechdrs[secndx].sh_flags & SHF_EXECINSTR) != 0; 1005 } 1006 1007 static void default_mismatch_handler(const char *modname, struct elf_info *elf, 1008 const struct sectioncheck* const mismatch, 1009 Elf_Sym *tsym, 1010 unsigned int fsecndx, const char *fromsec, Elf_Addr faddr, 1011 const char *tosec, Elf_Addr taddr) 1012 { 1013 Elf_Sym *from; 1014 const char *tosym; 1015 const char *fromsym; 1016 1017 from = find_fromsym(elf, faddr, fsecndx); 1018 fromsym = sym_name(elf, from); 1019 1020 tsym = find_tosym(elf, taddr, tsym); 1021 tosym = sym_name(elf, tsym); 1022 1023 /* check whitelist - we may ignore it */ 1024 if (!secref_whitelist(fromsec, fromsym, tosec, tosym)) 1025 return; 1026 1027 sec_mismatch_count++; 1028 1029 warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n", 1030 modname, fromsym, 1031 (unsigned int)(faddr - (from ? from->st_value : 0)), 1032 fromsec, tosym, tosec); 1033 1034 if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) { 1035 if (match(tosec, mismatch->bad_tosec)) 1036 fatal("The relocation at %s+0x%lx references\n" 1037 "section \"%s\" which is black-listed.\n" 1038 "Something is seriously wrong and should be fixed.\n" 1039 "You might get more information about where this is\n" 1040 "coming from by using scripts/check_extable.sh %s\n", 1041 fromsec, (long)faddr, tosec, modname); 1042 else if (is_executable_section(elf, get_secindex(elf, tsym))) 1043 warn("The relocation at %s+0x%lx references\n" 1044 "section \"%s\" which is not in the list of\n" 1045 "authorized sections. If you're adding a new section\n" 1046 "and/or if this reference is valid, add \"%s\" to the\n" 1047 "list of authorized sections to jump to on fault.\n" 1048 "This can be achieved by adding \"%s\" to\n" 1049 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n", 1050 fromsec, (long)faddr, tosec, tosec, tosec); 1051 else 1052 error("%s+0x%lx references non-executable section '%s'\n", 1053 fromsec, (long)faddr, tosec); 1054 } 1055 } 1056 1057 static void check_export_symbol(struct module *mod, struct elf_info *elf, 1058 Elf_Addr faddr, const char *secname, 1059 Elf_Sym *sym) 1060 { 1061 static const char *prefix = "__export_symbol_"; 1062 const char *label_name, *name, *data; 1063 Elf_Sym *label; 1064 struct symbol *s; 1065 bool is_gpl; 1066 1067 label = find_fromsym(elf, faddr, elf->export_symbol_secndx); 1068 label_name = sym_name(elf, label); 1069 1070 if (!strstarts(label_name, prefix)) { 1071 error("%s: .export_symbol section contains strange symbol '%s'\n", 1072 mod->name, label_name); 1073 return; 1074 } 1075 1076 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && 1077 ELF_ST_BIND(sym->st_info) != STB_WEAK) { 1078 error("%s: local symbol '%s' was exported\n", mod->name, 1079 label_name + strlen(prefix)); 1080 return; 1081 } 1082 1083 name = sym_name(elf, sym); 1084 if (strcmp(label_name + strlen(prefix), name)) { 1085 error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n", 1086 mod->name, name); 1087 return; 1088 } 1089 1090 data = sym_get_data(elf, label); /* license */ 1091 if (!strcmp(data, "GPL")) { 1092 is_gpl = true; 1093 } else if (!strcmp(data, "")) { 1094 is_gpl = false; 1095 } else { 1096 error("%s: unknown license '%s' was specified for '%s'\n", 1097 mod->name, data, name); 1098 return; 1099 } 1100 1101 data += strlen(data) + 1; /* namespace */ 1102 s = sym_add_exported(name, mod, is_gpl, data); 1103 1104 /* 1105 * We need to be aware whether we are exporting a function or 1106 * a data on some architectures. 1107 */ 1108 s->is_func = (ELF_ST_TYPE(sym->st_info) == STT_FUNC); 1109 1110 /* 1111 * For parisc64, symbols prefixed $$ from the library have the symbol type 1112 * STT_LOPROC. They should be handled as functions too. 1113 */ 1114 if (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64 && 1115 elf->hdr->e_machine == EM_PARISC && 1116 ELF_ST_TYPE(sym->st_info) == STT_LOPROC) 1117 s->is_func = true; 1118 1119 if (match(secname, PATTERNS(ALL_INIT_SECTIONS))) 1120 warn("%s: %s: EXPORT_SYMBOL used for init symbol. Remove __init or EXPORT_SYMBOL.\n", 1121 mod->name, name); 1122 else if (match(secname, PATTERNS(ALL_EXIT_SECTIONS))) 1123 warn("%s: %s: EXPORT_SYMBOL used for exit symbol. Remove __exit or EXPORT_SYMBOL.\n", 1124 mod->name, name); 1125 } 1126 1127 static void check_section_mismatch(struct module *mod, struct elf_info *elf, 1128 Elf_Sym *sym, 1129 unsigned int fsecndx, const char *fromsec, 1130 Elf_Addr faddr, Elf_Addr taddr) 1131 { 1132 const char *tosec = sec_name(elf, get_secindex(elf, sym)); 1133 const struct sectioncheck *mismatch; 1134 1135 if (module_enabled && elf->export_symbol_secndx == fsecndx) { 1136 check_export_symbol(mod, elf, faddr, tosec, sym); 1137 return; 1138 } 1139 1140 mismatch = section_mismatch(fromsec, tosec); 1141 if (!mismatch) 1142 return; 1143 1144 default_mismatch_handler(mod->name, elf, mismatch, sym, 1145 fsecndx, fromsec, faddr, 1146 tosec, taddr); 1147 } 1148 1149 static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type) 1150 { 1151 switch (r_type) { 1152 case R_386_32: 1153 return TO_NATIVE(*location); 1154 case R_386_PC32: 1155 return TO_NATIVE(*location) + 4; 1156 } 1157 1158 return (Elf_Addr)(-1); 1159 } 1160 1161 static int32_t sign_extend32(int32_t value, int index) 1162 { 1163 uint8_t shift = 31 - index; 1164 1165 return (int32_t)(value << shift) >> shift; 1166 } 1167 1168 static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type) 1169 { 1170 uint32_t inst, upper, lower, sign, j1, j2; 1171 int32_t offset; 1172 1173 switch (r_type) { 1174 case R_ARM_ABS32: 1175 case R_ARM_REL32: 1176 inst = TO_NATIVE(*(uint32_t *)loc); 1177 return inst + sym->st_value; 1178 case R_ARM_MOVW_ABS_NC: 1179 case R_ARM_MOVT_ABS: 1180 inst = TO_NATIVE(*(uint32_t *)loc); 1181 offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff), 1182 15); 1183 return offset + sym->st_value; 1184 case R_ARM_PC24: 1185 case R_ARM_CALL: 1186 case R_ARM_JUMP24: 1187 inst = TO_NATIVE(*(uint32_t *)loc); 1188 offset = sign_extend32((inst & 0x00ffffff) << 2, 25); 1189 return offset + sym->st_value + 8; 1190 case R_ARM_THM_MOVW_ABS_NC: 1191 case R_ARM_THM_MOVT_ABS: 1192 upper = TO_NATIVE(*(uint16_t *)loc); 1193 lower = TO_NATIVE(*((uint16_t *)loc + 1)); 1194 offset = sign_extend32(((upper & 0x000f) << 12) | 1195 ((upper & 0x0400) << 1) | 1196 ((lower & 0x7000) >> 4) | 1197 (lower & 0x00ff), 1198 15); 1199 return offset + sym->st_value; 1200 case R_ARM_THM_JUMP19: 1201 /* 1202 * Encoding T3: 1203 * S = upper[10] 1204 * imm6 = upper[5:0] 1205 * J1 = lower[13] 1206 * J2 = lower[11] 1207 * imm11 = lower[10:0] 1208 * imm32 = SignExtend(S:J2:J1:imm6:imm11:'0') 1209 */ 1210 upper = TO_NATIVE(*(uint16_t *)loc); 1211 lower = TO_NATIVE(*((uint16_t *)loc + 1)); 1212 1213 sign = (upper >> 10) & 1; 1214 j1 = (lower >> 13) & 1; 1215 j2 = (lower >> 11) & 1; 1216 offset = sign_extend32((sign << 20) | (j2 << 19) | (j1 << 18) | 1217 ((upper & 0x03f) << 12) | 1218 ((lower & 0x07ff) << 1), 1219 20); 1220 return offset + sym->st_value + 4; 1221 case R_ARM_THM_PC22: 1222 case R_ARM_THM_JUMP24: 1223 /* 1224 * Encoding T4: 1225 * S = upper[10] 1226 * imm10 = upper[9:0] 1227 * J1 = lower[13] 1228 * J2 = lower[11] 1229 * imm11 = lower[10:0] 1230 * I1 = NOT(J1 XOR S) 1231 * I2 = NOT(J2 XOR S) 1232 * imm32 = SignExtend(S:I1:I2:imm10:imm11:'0') 1233 */ 1234 upper = TO_NATIVE(*(uint16_t *)loc); 1235 lower = TO_NATIVE(*((uint16_t *)loc + 1)); 1236 1237 sign = (upper >> 10) & 1; 1238 j1 = (lower >> 13) & 1; 1239 j2 = (lower >> 11) & 1; 1240 offset = sign_extend32((sign << 24) | 1241 ((~(j1 ^ sign) & 1) << 23) | 1242 ((~(j2 ^ sign) & 1) << 22) | 1243 ((upper & 0x03ff) << 12) | 1244 ((lower & 0x07ff) << 1), 1245 24); 1246 return offset + sym->st_value + 4; 1247 } 1248 1249 return (Elf_Addr)(-1); 1250 } 1251 1252 static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type) 1253 { 1254 uint32_t inst; 1255 1256 inst = TO_NATIVE(*location); 1257 switch (r_type) { 1258 case R_MIPS_LO16: 1259 return inst & 0xffff; 1260 case R_MIPS_26: 1261 return (inst & 0x03ffffff) << 2; 1262 case R_MIPS_32: 1263 return inst; 1264 } 1265 return (Elf_Addr)(-1); 1266 } 1267 1268 #ifndef EM_RISCV 1269 #define EM_RISCV 243 1270 #endif 1271 1272 #ifndef R_RISCV_SUB32 1273 #define R_RISCV_SUB32 39 1274 #endif 1275 1276 #ifndef EM_LOONGARCH 1277 #define EM_LOONGARCH 258 1278 #endif 1279 1280 #ifndef R_LARCH_SUB32 1281 #define R_LARCH_SUB32 55 1282 #endif 1283 1284 #ifndef R_LARCH_RELAX 1285 #define R_LARCH_RELAX 100 1286 #endif 1287 1288 #ifndef R_LARCH_ALIGN 1289 #define R_LARCH_ALIGN 102 1290 #endif 1291 1292 static void get_rel_type_and_sym(struct elf_info *elf, uint64_t r_info, 1293 unsigned int *r_type, unsigned int *r_sym) 1294 { 1295 typedef struct { 1296 Elf64_Word r_sym; /* Symbol index */ 1297 unsigned char r_ssym; /* Special symbol for 2nd relocation */ 1298 unsigned char r_type3; /* 3rd relocation type */ 1299 unsigned char r_type2; /* 2nd relocation type */ 1300 unsigned char r_type; /* 1st relocation type */ 1301 } Elf64_Mips_R_Info; 1302 1303 bool is_64bit = (elf->hdr->e_ident[EI_CLASS] == ELFCLASS64); 1304 1305 if (elf->hdr->e_machine == EM_MIPS && is_64bit) { 1306 Elf64_Mips_R_Info *mips64_r_info = (void *)&r_info; 1307 1308 *r_type = mips64_r_info->r_type; 1309 *r_sym = TO_NATIVE(mips64_r_info->r_sym); 1310 return; 1311 } 1312 1313 if (is_64bit) 1314 r_info = TO_NATIVE((Elf64_Xword)r_info); 1315 else 1316 r_info = TO_NATIVE((Elf32_Word)r_info); 1317 1318 *r_type = ELF_R_TYPE(r_info); 1319 *r_sym = ELF_R_SYM(r_info); 1320 } 1321 1322 static void section_rela(struct module *mod, struct elf_info *elf, 1323 unsigned int fsecndx, const char *fromsec, 1324 const Elf_Rela *start, const Elf_Rela *stop) 1325 { 1326 const Elf_Rela *rela; 1327 1328 for (rela = start; rela < stop; rela++) { 1329 Elf_Sym *tsym; 1330 Elf_Addr taddr, r_offset; 1331 unsigned int r_type, r_sym; 1332 1333 r_offset = TO_NATIVE(rela->r_offset); 1334 get_rel_type_and_sym(elf, rela->r_info, &r_type, &r_sym); 1335 1336 tsym = elf->symtab_start + r_sym; 1337 taddr = tsym->st_value + TO_NATIVE(rela->r_addend); 1338 1339 switch (elf->hdr->e_machine) { 1340 case EM_RISCV: 1341 if (!strcmp("__ex_table", fromsec) && 1342 r_type == R_RISCV_SUB32) 1343 continue; 1344 break; 1345 case EM_LOONGARCH: 1346 switch (r_type) { 1347 case R_LARCH_SUB32: 1348 if (!strcmp("__ex_table", fromsec)) 1349 continue; 1350 break; 1351 case R_LARCH_RELAX: 1352 case R_LARCH_ALIGN: 1353 /* These relocs do not refer to symbols */ 1354 continue; 1355 } 1356 break; 1357 } 1358 1359 check_section_mismatch(mod, elf, tsym, 1360 fsecndx, fromsec, r_offset, taddr); 1361 } 1362 } 1363 1364 static void section_rel(struct module *mod, struct elf_info *elf, 1365 unsigned int fsecndx, const char *fromsec, 1366 const Elf_Rel *start, const Elf_Rel *stop) 1367 { 1368 const Elf_Rel *rel; 1369 1370 for (rel = start; rel < stop; rel++) { 1371 Elf_Sym *tsym; 1372 Elf_Addr taddr, r_offset; 1373 unsigned int r_type, r_sym; 1374 void *loc; 1375 1376 r_offset = TO_NATIVE(rel->r_offset); 1377 get_rel_type_and_sym(elf, rel->r_info, &r_type, &r_sym); 1378 1379 loc = sym_get_data_by_offset(elf, fsecndx, r_offset); 1380 tsym = elf->symtab_start + r_sym; 1381 1382 switch (elf->hdr->e_machine) { 1383 case EM_386: 1384 taddr = addend_386_rel(loc, r_type); 1385 break; 1386 case EM_ARM: 1387 taddr = addend_arm_rel(loc, tsym, r_type); 1388 break; 1389 case EM_MIPS: 1390 taddr = addend_mips_rel(loc, r_type); 1391 break; 1392 default: 1393 fatal("Please add code to calculate addend for this architecture\n"); 1394 } 1395 1396 check_section_mismatch(mod, elf, tsym, 1397 fsecndx, fromsec, r_offset, taddr); 1398 } 1399 } 1400 1401 /** 1402 * A module includes a number of sections that are discarded 1403 * either when loaded or when used as built-in. 1404 * For loaded modules all functions marked __init and all data 1405 * marked __initdata will be discarded when the module has been initialized. 1406 * Likewise for modules used built-in the sections marked __exit 1407 * are discarded because __exit marked function are supposed to be called 1408 * only when a module is unloaded which never happens for built-in modules. 1409 * The check_sec_ref() function traverses all relocation records 1410 * to find all references to a section that reference a section that will 1411 * be discarded and warns about it. 1412 **/ 1413 static void check_sec_ref(struct module *mod, struct elf_info *elf) 1414 { 1415 int i; 1416 1417 /* Walk through all sections */ 1418 for (i = 0; i < elf->num_sections; i++) { 1419 Elf_Shdr *sechdr = &elf->sechdrs[i]; 1420 1421 check_section(mod->name, elf, sechdr); 1422 /* We want to process only relocation sections and not .init */ 1423 if (sechdr->sh_type == SHT_REL || sechdr->sh_type == SHT_RELA) { 1424 /* section to which the relocation applies */ 1425 unsigned int secndx = sechdr->sh_info; 1426 const char *secname = sec_name(elf, secndx); 1427 const void *start, *stop; 1428 1429 /* If the section is known good, skip it */ 1430 if (match(secname, section_white_list)) 1431 continue; 1432 1433 start = sym_get_data_by_offset(elf, i, 0); 1434 stop = start + sechdr->sh_size; 1435 1436 if (sechdr->sh_type == SHT_RELA) 1437 section_rela(mod, elf, secndx, secname, 1438 start, stop); 1439 else 1440 section_rel(mod, elf, secndx, secname, 1441 start, stop); 1442 } 1443 } 1444 } 1445 1446 static char *remove_dot(char *s) 1447 { 1448 size_t n = strcspn(s, "."); 1449 1450 if (n && s[n]) { 1451 size_t m = strspn(s + n + 1, "0123456789"); 1452 if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0)) 1453 s[n] = 0; 1454 } 1455 return s; 1456 } 1457 1458 /* 1459 * The CRCs are recorded in .*.cmd files in the form of: 1460 * #SYMVER <name> <crc> 1461 */ 1462 static void extract_crcs_for_object(const char *object, struct module *mod) 1463 { 1464 char cmd_file[PATH_MAX]; 1465 char *buf, *p; 1466 const char *base; 1467 int dirlen, ret; 1468 1469 base = strrchr(object, '/'); 1470 if (base) { 1471 base++; 1472 dirlen = base - object; 1473 } else { 1474 dirlen = 0; 1475 base = object; 1476 } 1477 1478 ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd", 1479 dirlen, object, base); 1480 if (ret >= sizeof(cmd_file)) { 1481 error("%s: too long path was truncated\n", cmd_file); 1482 return; 1483 } 1484 1485 buf = read_text_file(cmd_file); 1486 p = buf; 1487 1488 while ((p = strstr(p, "\n#SYMVER "))) { 1489 char *name; 1490 size_t namelen; 1491 unsigned int crc; 1492 struct symbol *sym; 1493 1494 name = p + strlen("\n#SYMVER "); 1495 1496 p = strchr(name, ' '); 1497 if (!p) 1498 break; 1499 1500 namelen = p - name; 1501 p++; 1502 1503 if (!isdigit(*p)) 1504 continue; /* skip this line */ 1505 1506 crc = strtoul(p, &p, 0); 1507 if (*p != '\n') 1508 continue; /* skip this line */ 1509 1510 name[namelen] = '\0'; 1511 1512 /* 1513 * sym_find_with_module() may return NULL here. 1514 * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y. 1515 * Since commit e1327a127703, genksyms calculates CRCs of all 1516 * symbols, including trimmed ones. Ignore orphan CRCs. 1517 */ 1518 sym = sym_find_with_module(name, mod); 1519 if (sym) 1520 sym_set_crc(sym, crc); 1521 } 1522 1523 free(buf); 1524 } 1525 1526 /* 1527 * The symbol versions (CRC) are recorded in the .*.cmd files. 1528 * Parse them to retrieve CRCs for the current module. 1529 */ 1530 static void mod_set_crcs(struct module *mod) 1531 { 1532 char objlist[PATH_MAX]; 1533 char *buf, *p, *obj; 1534 int ret; 1535 1536 if (mod->is_vmlinux) { 1537 strcpy(objlist, ".vmlinux.objs"); 1538 } else { 1539 /* objects for a module are listed in the *.mod file. */ 1540 ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name); 1541 if (ret >= sizeof(objlist)) { 1542 error("%s: too long path was truncated\n", objlist); 1543 return; 1544 } 1545 } 1546 1547 buf = read_text_file(objlist); 1548 p = buf; 1549 1550 while ((obj = strsep(&p, "\n")) && obj[0]) 1551 extract_crcs_for_object(obj, mod); 1552 1553 free(buf); 1554 } 1555 1556 static void read_symbols(const char *modname) 1557 { 1558 const char *symname; 1559 char *version; 1560 char *license; 1561 char *namespace; 1562 struct module *mod; 1563 struct elf_info info = { }; 1564 Elf_Sym *sym; 1565 1566 if (!parse_elf(&info, modname)) 1567 return; 1568 1569 if (!strends(modname, ".o")) { 1570 error("%s: filename must be suffixed with .o\n", modname); 1571 return; 1572 } 1573 1574 /* strip trailing .o */ 1575 mod = new_module(modname, strlen(modname) - strlen(".o")); 1576 1577 if (!mod->is_vmlinux) { 1578 license = get_modinfo(&info, "license"); 1579 if (!license) 1580 error("missing MODULE_LICENSE() in %s\n", modname); 1581 while (license) { 1582 if (!license_is_gpl_compatible(license)) { 1583 mod->is_gpl_compatible = false; 1584 break; 1585 } 1586 license = get_next_modinfo(&info, "license", license); 1587 } 1588 1589 namespace = get_modinfo(&info, "import_ns"); 1590 while (namespace) { 1591 add_namespace(&mod->imported_namespaces, namespace); 1592 namespace = get_next_modinfo(&info, "import_ns", 1593 namespace); 1594 } 1595 1596 if (extra_warn && !get_modinfo(&info, "description")) 1597 warn("missing MODULE_DESCRIPTION() in %s\n", modname); 1598 } 1599 1600 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { 1601 symname = remove_dot(info.strtab + sym->st_name); 1602 1603 handle_symbol(mod, &info, sym, symname); 1604 handle_moddevtable(mod, &info, sym, symname); 1605 } 1606 1607 check_sec_ref(mod, &info); 1608 1609 if (!mod->is_vmlinux) { 1610 version = get_modinfo(&info, "version"); 1611 if (version || all_versions) 1612 get_src_version(mod->name, mod->srcversion, 1613 sizeof(mod->srcversion) - 1); 1614 } 1615 1616 parse_elf_finish(&info); 1617 1618 if (modversions) { 1619 /* 1620 * Our trick to get versioning for module struct etc. - it's 1621 * never passed as an argument to an exported function, so 1622 * the automatic versioning doesn't pick it up, but it's really 1623 * important anyhow. 1624 */ 1625 sym_add_unresolved("module_layout", mod, false); 1626 1627 mod_set_crcs(mod); 1628 } 1629 } 1630 1631 static void read_symbols_from_files(const char *filename) 1632 { 1633 FILE *in = stdin; 1634 char fname[PATH_MAX]; 1635 1636 in = fopen(filename, "r"); 1637 if (!in) 1638 fatal("Can't open filenames file %s: %m", filename); 1639 1640 while (fgets(fname, PATH_MAX, in) != NULL) { 1641 if (strends(fname, "\n")) 1642 fname[strlen(fname)-1] = '\0'; 1643 read_symbols(fname); 1644 } 1645 1646 fclose(in); 1647 } 1648 1649 #define SZ 500 1650 1651 /* We first write the generated file into memory using the 1652 * following helper, then compare to the file on disk and 1653 * only update the later if anything changed */ 1654 1655 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, 1656 const char *fmt, ...) 1657 { 1658 char tmp[SZ]; 1659 int len; 1660 va_list ap; 1661 1662 va_start(ap, fmt); 1663 len = vsnprintf(tmp, SZ, fmt, ap); 1664 buf_write(buf, tmp, len); 1665 va_end(ap); 1666 } 1667 1668 void buf_write(struct buffer *buf, const char *s, int len) 1669 { 1670 if (buf->size - buf->pos < len) { 1671 buf->size += len + SZ; 1672 buf->p = xrealloc(buf->p, buf->size); 1673 } 1674 strncpy(buf->p + buf->pos, s, len); 1675 buf->pos += len; 1676 } 1677 1678 static void check_exports(struct module *mod) 1679 { 1680 struct symbol *s, *exp; 1681 1682 list_for_each_entry(s, &mod->unresolved_symbols, list) { 1683 const char *basename; 1684 exp = find_symbol(s->name); 1685 if (!exp) { 1686 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS) 1687 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR, 1688 "\"%s\" [%s.ko] undefined!\n", 1689 s->name, mod->name); 1690 continue; 1691 } 1692 if (exp->module == mod) { 1693 error("\"%s\" [%s.ko] was exported without definition\n", 1694 s->name, mod->name); 1695 continue; 1696 } 1697 1698 exp->used = true; 1699 s->module = exp->module; 1700 s->crc_valid = exp->crc_valid; 1701 s->crc = exp->crc; 1702 1703 basename = strrchr(mod->name, '/'); 1704 if (basename) 1705 basename++; 1706 else 1707 basename = mod->name; 1708 1709 if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { 1710 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR, 1711 "module %s uses symbol %s from namespace %s, but does not import it.\n", 1712 basename, exp->name, exp->namespace); 1713 add_namespace(&mod->missing_namespaces, exp->namespace); 1714 } 1715 1716 if (!mod->is_gpl_compatible && exp->is_gpl_only) 1717 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n", 1718 basename, exp->name); 1719 } 1720 } 1721 1722 static void handle_white_list_exports(const char *white_list) 1723 { 1724 char *buf, *p, *name; 1725 1726 buf = read_text_file(white_list); 1727 p = buf; 1728 1729 while ((name = strsep(&p, "\n"))) { 1730 struct symbol *sym = find_symbol(name); 1731 1732 if (sym) 1733 sym->used = true; 1734 } 1735 1736 free(buf); 1737 } 1738 1739 static void check_modname_len(struct module *mod) 1740 { 1741 const char *mod_name; 1742 1743 mod_name = strrchr(mod->name, '/'); 1744 if (mod_name == NULL) 1745 mod_name = mod->name; 1746 else 1747 mod_name++; 1748 if (strlen(mod_name) >= MODULE_NAME_LEN) 1749 error("module name is too long [%s.ko]\n", mod->name); 1750 } 1751 1752 /** 1753 * Header for the generated file 1754 **/ 1755 static void add_header(struct buffer *b, struct module *mod) 1756 { 1757 buf_printf(b, "#include <linux/module.h>\n"); 1758 /* 1759 * Include build-salt.h after module.h in order to 1760 * inherit the definitions. 1761 */ 1762 buf_printf(b, "#define INCLUDE_VERMAGIC\n"); 1763 buf_printf(b, "#include <linux/build-salt.h>\n"); 1764 buf_printf(b, "#include <linux/elfnote-lto.h>\n"); 1765 buf_printf(b, "#include <linux/export-internal.h>\n"); 1766 buf_printf(b, "#include <linux/vermagic.h>\n"); 1767 buf_printf(b, "#include <linux/compiler.h>\n"); 1768 buf_printf(b, "\n"); 1769 buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n"); 1770 buf_printf(b, "#include <asm/orc_header.h>\n"); 1771 buf_printf(b, "ORC_HEADER;\n"); 1772 buf_printf(b, "#endif\n"); 1773 buf_printf(b, "\n"); 1774 buf_printf(b, "BUILD_SALT;\n"); 1775 buf_printf(b, "BUILD_LTO_INFO;\n"); 1776 buf_printf(b, "\n"); 1777 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); 1778 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); 1779 buf_printf(b, "\n"); 1780 buf_printf(b, "__visible struct module __this_module\n"); 1781 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n"); 1782 buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); 1783 if (mod->has_init) 1784 buf_printf(b, "\t.init = init_module,\n"); 1785 if (mod->has_cleanup) 1786 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" 1787 "\t.exit = cleanup_module,\n" 1788 "#endif\n"); 1789 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n"); 1790 buf_printf(b, "};\n"); 1791 1792 if (!external_module) 1793 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); 1794 1795 buf_printf(b, 1796 "\n" 1797 "#ifdef CONFIG_MITIGATION_RETPOLINE\n" 1798 "MODULE_INFO(retpoline, \"Y\");\n" 1799 "#endif\n"); 1800 1801 if (strstarts(mod->name, "drivers/staging")) 1802 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); 1803 1804 if (strstarts(mod->name, "tools/testing")) 1805 buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n"); 1806 } 1807 1808 static void add_exported_symbols(struct buffer *buf, struct module *mod) 1809 { 1810 struct symbol *sym; 1811 1812 /* generate struct for exported symbols */ 1813 buf_printf(buf, "\n"); 1814 list_for_each_entry(sym, &mod->exported_symbols, list) { 1815 if (trim_unused_exports && !sym->used) 1816 continue; 1817 1818 buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n", 1819 sym->is_func ? "FUNC" : "DATA", sym->name, 1820 sym->is_gpl_only ? "_gpl" : "", sym->namespace); 1821 } 1822 1823 if (!modversions) 1824 return; 1825 1826 /* record CRCs for exported symbols */ 1827 buf_printf(buf, "\n"); 1828 list_for_each_entry(sym, &mod->exported_symbols, list) { 1829 if (trim_unused_exports && !sym->used) 1830 continue; 1831 1832 if (!sym->crc_valid) 1833 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n" 1834 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n", 1835 sym->name, mod->name, mod->is_vmlinux ? "" : ".ko", 1836 sym->name); 1837 1838 buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n", 1839 sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : ""); 1840 } 1841 } 1842 1843 /** 1844 * Record CRCs for unresolved symbols 1845 **/ 1846 static void add_versions(struct buffer *b, struct module *mod) 1847 { 1848 struct symbol *s; 1849 1850 if (!modversions) 1851 return; 1852 1853 buf_printf(b, "\n"); 1854 buf_printf(b, "static const struct modversion_info ____versions[]\n"); 1855 buf_printf(b, "__used __section(\"__versions\") = {\n"); 1856 1857 list_for_each_entry(s, &mod->unresolved_symbols, list) { 1858 if (!s->module) 1859 continue; 1860 if (!s->crc_valid) { 1861 warn("\"%s\" [%s.ko] has no CRC!\n", 1862 s->name, mod->name); 1863 continue; 1864 } 1865 if (strlen(s->name) >= MODULE_NAME_LEN) { 1866 error("too long symbol \"%s\" [%s.ko]\n", 1867 s->name, mod->name); 1868 break; 1869 } 1870 buf_printf(b, "\t{ %#8x, \"%s\" },\n", 1871 s->crc, s->name); 1872 } 1873 1874 buf_printf(b, "};\n"); 1875 } 1876 1877 static void add_depends(struct buffer *b, struct module *mod) 1878 { 1879 struct symbol *s; 1880 int first = 1; 1881 1882 /* Clear ->seen flag of modules that own symbols needed by this. */ 1883 list_for_each_entry(s, &mod->unresolved_symbols, list) { 1884 if (s->module) 1885 s->module->seen = s->module->is_vmlinux; 1886 } 1887 1888 buf_printf(b, "\n"); 1889 buf_printf(b, "MODULE_INFO(depends, \""); 1890 list_for_each_entry(s, &mod->unresolved_symbols, list) { 1891 const char *p; 1892 if (!s->module) 1893 continue; 1894 1895 if (s->module->seen) 1896 continue; 1897 1898 s->module->seen = true; 1899 p = strrchr(s->module->name, '/'); 1900 if (p) 1901 p++; 1902 else 1903 p = s->module->name; 1904 buf_printf(b, "%s%s", first ? "" : ",", p); 1905 first = 0; 1906 } 1907 buf_printf(b, "\");\n"); 1908 } 1909 1910 static void add_srcversion(struct buffer *b, struct module *mod) 1911 { 1912 if (mod->srcversion[0]) { 1913 buf_printf(b, "\n"); 1914 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", 1915 mod->srcversion); 1916 } 1917 } 1918 1919 static void write_buf(struct buffer *b, const char *fname) 1920 { 1921 FILE *file; 1922 1923 if (error_occurred) 1924 return; 1925 1926 file = fopen(fname, "w"); 1927 if (!file) { 1928 perror(fname); 1929 exit(1); 1930 } 1931 if (fwrite(b->p, 1, b->pos, file) != b->pos) { 1932 perror(fname); 1933 exit(1); 1934 } 1935 if (fclose(file) != 0) { 1936 perror(fname); 1937 exit(1); 1938 } 1939 } 1940 1941 static void write_if_changed(struct buffer *b, const char *fname) 1942 { 1943 char *tmp; 1944 FILE *file; 1945 struct stat st; 1946 1947 file = fopen(fname, "r"); 1948 if (!file) 1949 goto write; 1950 1951 if (fstat(fileno(file), &st) < 0) 1952 goto close_write; 1953 1954 if (st.st_size != b->pos) 1955 goto close_write; 1956 1957 tmp = xmalloc(b->pos); 1958 if (fread(tmp, 1, b->pos, file) != b->pos) 1959 goto free_write; 1960 1961 if (memcmp(tmp, b->p, b->pos) != 0) 1962 goto free_write; 1963 1964 free(tmp); 1965 fclose(file); 1966 return; 1967 1968 free_write: 1969 free(tmp); 1970 close_write: 1971 fclose(file); 1972 write: 1973 write_buf(b, fname); 1974 } 1975 1976 static void write_vmlinux_export_c_file(struct module *mod) 1977 { 1978 struct buffer buf = { }; 1979 1980 buf_printf(&buf, 1981 "#include <linux/export-internal.h>\n"); 1982 1983 add_exported_symbols(&buf, mod); 1984 write_if_changed(&buf, ".vmlinux.export.c"); 1985 free(buf.p); 1986 } 1987 1988 /* do sanity checks, and generate *.mod.c file */ 1989 static void write_mod_c_file(struct module *mod) 1990 { 1991 struct buffer buf = { }; 1992 char fname[PATH_MAX]; 1993 int ret; 1994 1995 add_header(&buf, mod); 1996 add_exported_symbols(&buf, mod); 1997 add_versions(&buf, mod); 1998 add_depends(&buf, mod); 1999 add_moddevtable(&buf, mod); 2000 add_srcversion(&buf, mod); 2001 2002 ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name); 2003 if (ret >= sizeof(fname)) { 2004 error("%s: too long path was truncated\n", fname); 2005 goto free; 2006 } 2007 2008 write_if_changed(&buf, fname); 2009 2010 free: 2011 free(buf.p); 2012 } 2013 2014 /* parse Module.symvers file. line format: 2015 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace 2016 **/ 2017 static void read_dump(const char *fname) 2018 { 2019 char *buf, *pos, *line; 2020 2021 buf = read_text_file(fname); 2022 if (!buf) 2023 /* No symbol versions, silently ignore */ 2024 return; 2025 2026 pos = buf; 2027 2028 while ((line = get_line(&pos))) { 2029 char *symname, *namespace, *modname, *d, *export; 2030 unsigned int crc; 2031 struct module *mod; 2032 struct symbol *s; 2033 bool gpl_only; 2034 2035 if (!(symname = strchr(line, '\t'))) 2036 goto fail; 2037 *symname++ = '\0'; 2038 if (!(modname = strchr(symname, '\t'))) 2039 goto fail; 2040 *modname++ = '\0'; 2041 if (!(export = strchr(modname, '\t'))) 2042 goto fail; 2043 *export++ = '\0'; 2044 if (!(namespace = strchr(export, '\t'))) 2045 goto fail; 2046 *namespace++ = '\0'; 2047 2048 crc = strtoul(line, &d, 16); 2049 if (*symname == '\0' || *modname == '\0' || *d != '\0') 2050 goto fail; 2051 2052 if (!strcmp(export, "EXPORT_SYMBOL_GPL")) { 2053 gpl_only = true; 2054 } else if (!strcmp(export, "EXPORT_SYMBOL")) { 2055 gpl_only = false; 2056 } else { 2057 error("%s: unknown license %s. skip", symname, export); 2058 continue; 2059 } 2060 2061 mod = find_module(modname); 2062 if (!mod) { 2063 mod = new_module(modname, strlen(modname)); 2064 mod->from_dump = true; 2065 } 2066 s = sym_add_exported(symname, mod, gpl_only, namespace); 2067 sym_set_crc(s, crc); 2068 } 2069 free(buf); 2070 return; 2071 fail: 2072 free(buf); 2073 fatal("parse error in symbol dump file\n"); 2074 } 2075 2076 static void write_dump(const char *fname) 2077 { 2078 struct buffer buf = { }; 2079 struct module *mod; 2080 struct symbol *sym; 2081 2082 list_for_each_entry(mod, &modules, list) { 2083 if (mod->from_dump) 2084 continue; 2085 list_for_each_entry(sym, &mod->exported_symbols, list) { 2086 if (trim_unused_exports && !sym->used) 2087 continue; 2088 2089 buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n", 2090 sym->crc, sym->name, mod->name, 2091 sym->is_gpl_only ? "_GPL" : "", 2092 sym->namespace); 2093 } 2094 } 2095 write_buf(&buf, fname); 2096 free(buf.p); 2097 } 2098 2099 static void write_namespace_deps_files(const char *fname) 2100 { 2101 struct module *mod; 2102 struct namespace_list *ns; 2103 struct buffer ns_deps_buf = {}; 2104 2105 list_for_each_entry(mod, &modules, list) { 2106 2107 if (mod->from_dump || list_empty(&mod->missing_namespaces)) 2108 continue; 2109 2110 buf_printf(&ns_deps_buf, "%s.ko:", mod->name); 2111 2112 list_for_each_entry(ns, &mod->missing_namespaces, list) 2113 buf_printf(&ns_deps_buf, " %s", ns->namespace); 2114 2115 buf_printf(&ns_deps_buf, "\n"); 2116 } 2117 2118 write_if_changed(&ns_deps_buf, fname); 2119 free(ns_deps_buf.p); 2120 } 2121 2122 struct dump_list { 2123 struct list_head list; 2124 const char *file; 2125 }; 2126 2127 static void check_host_endian(void) 2128 { 2129 static const union { 2130 short s; 2131 char c[2]; 2132 } endian_test = { .c = {0x01, 0x02} }; 2133 2134 switch (endian_test.s) { 2135 case 0x0102: 2136 host_is_big_endian = true; 2137 break; 2138 case 0x0201: 2139 host_is_big_endian = false; 2140 break; 2141 default: 2142 fatal("Unknown host endian\n"); 2143 } 2144 } 2145 2146 int main(int argc, char **argv) 2147 { 2148 struct module *mod; 2149 char *missing_namespace_deps = NULL; 2150 char *unused_exports_white_list = NULL; 2151 char *dump_write = NULL, *files_source = NULL; 2152 int opt; 2153 LIST_HEAD(dump_lists); 2154 struct dump_list *dl, *dl2; 2155 2156 while ((opt = getopt(argc, argv, "ei:MmnT:to:au:WwENd:")) != -1) { 2157 switch (opt) { 2158 case 'e': 2159 external_module = true; 2160 break; 2161 case 'i': 2162 dl = xmalloc(sizeof(*dl)); 2163 dl->file = optarg; 2164 list_add_tail(&dl->list, &dump_lists); 2165 break; 2166 case 'M': 2167 module_enabled = true; 2168 break; 2169 case 'm': 2170 modversions = true; 2171 break; 2172 case 'n': 2173 ignore_missing_files = true; 2174 break; 2175 case 'o': 2176 dump_write = optarg; 2177 break; 2178 case 'a': 2179 all_versions = true; 2180 break; 2181 case 'T': 2182 files_source = optarg; 2183 break; 2184 case 't': 2185 trim_unused_exports = true; 2186 break; 2187 case 'u': 2188 unused_exports_white_list = optarg; 2189 break; 2190 case 'W': 2191 extra_warn = true; 2192 break; 2193 case 'w': 2194 warn_unresolved = true; 2195 break; 2196 case 'E': 2197 sec_mismatch_warn_only = false; 2198 break; 2199 case 'N': 2200 allow_missing_ns_imports = true; 2201 break; 2202 case 'd': 2203 missing_namespace_deps = optarg; 2204 break; 2205 default: 2206 exit(1); 2207 } 2208 } 2209 2210 check_host_endian(); 2211 2212 list_for_each_entry_safe(dl, dl2, &dump_lists, list) { 2213 read_dump(dl->file); 2214 list_del(&dl->list); 2215 free(dl); 2216 } 2217 2218 while (optind < argc) 2219 read_symbols(argv[optind++]); 2220 2221 if (files_source) 2222 read_symbols_from_files(files_source); 2223 2224 list_for_each_entry(mod, &modules, list) { 2225 if (mod->from_dump || mod->is_vmlinux) 2226 continue; 2227 2228 check_modname_len(mod); 2229 check_exports(mod); 2230 } 2231 2232 if (unused_exports_white_list) 2233 handle_white_list_exports(unused_exports_white_list); 2234 2235 list_for_each_entry(mod, &modules, list) { 2236 if (mod->from_dump) 2237 continue; 2238 2239 if (mod->is_vmlinux) 2240 write_vmlinux_export_c_file(mod); 2241 else 2242 write_mod_c_file(mod); 2243 } 2244 2245 if (missing_namespace_deps) 2246 write_namespace_deps_files(missing_namespace_deps); 2247 2248 if (dump_write) 2249 write_dump(dump_write); 2250 if (sec_mismatch_count && !sec_mismatch_warn_only) 2251 error("Section mismatches detected.\n" 2252 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); 2253 2254 if (nr_unresolved > MAX_UNRESOLVED_REPORTS) 2255 warn("suppressed %u unresolved symbol warnings because there were too many)\n", 2256 nr_unresolved - MAX_UNRESOLVED_REPORTS); 2257 2258 return error_occurred ? 1 : 0; 2259 } 2260