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