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