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