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