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