1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> 4 */ 5 6 #include <string.h> 7 #include <stdlib.h> 8 #include <sys/mman.h> 9 10 #include <arch/elf.h> 11 #include <objtool/builtin.h> 12 #include <objtool/cfi.h> 13 #include <objtool/arch.h> 14 #include <objtool/check.h> 15 #include <objtool/special.h> 16 #include <objtool/warn.h> 17 #include <objtool/endianness.h> 18 19 #include <linux/objtool.h> 20 #include <linux/hashtable.h> 21 #include <linux/kernel.h> 22 #include <linux/static_call_types.h> 23 24 struct alternative { 25 struct list_head list; 26 struct instruction *insn; 27 bool skip_orig; 28 }; 29 30 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; 31 32 static struct cfi_init_state initial_func_cfi; 33 static struct cfi_state init_cfi; 34 static struct cfi_state func_cfi; 35 36 struct instruction *find_insn(struct objtool_file *file, 37 struct section *sec, unsigned long offset) 38 { 39 struct instruction *insn; 40 41 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { 42 if (insn->sec == sec && insn->offset == offset) 43 return insn; 44 } 45 46 return NULL; 47 } 48 49 static struct instruction *next_insn_same_sec(struct objtool_file *file, 50 struct instruction *insn) 51 { 52 struct instruction *next = list_next_entry(insn, list); 53 54 if (!next || &next->list == &file->insn_list || next->sec != insn->sec) 55 return NULL; 56 57 return next; 58 } 59 60 static struct instruction *next_insn_same_func(struct objtool_file *file, 61 struct instruction *insn) 62 { 63 struct instruction *next = list_next_entry(insn, list); 64 struct symbol *func = insn->func; 65 66 if (!func) 67 return NULL; 68 69 if (&next->list != &file->insn_list && next->func == func) 70 return next; 71 72 /* Check if we're already in the subfunction: */ 73 if (func == func->cfunc) 74 return NULL; 75 76 /* Move to the subfunction: */ 77 return find_insn(file, func->cfunc->sec, func->cfunc->offset); 78 } 79 80 static struct instruction *prev_insn_same_sym(struct objtool_file *file, 81 struct instruction *insn) 82 { 83 struct instruction *prev = list_prev_entry(insn, list); 84 85 if (&prev->list != &file->insn_list && prev->func == insn->func) 86 return prev; 87 88 return NULL; 89 } 90 91 #define func_for_each_insn(file, func, insn) \ 92 for (insn = find_insn(file, func->sec, func->offset); \ 93 insn; \ 94 insn = next_insn_same_func(file, insn)) 95 96 #define sym_for_each_insn(file, sym, insn) \ 97 for (insn = find_insn(file, sym->sec, sym->offset); \ 98 insn && &insn->list != &file->insn_list && \ 99 insn->sec == sym->sec && \ 100 insn->offset < sym->offset + sym->len; \ 101 insn = list_next_entry(insn, list)) 102 103 #define sym_for_each_insn_continue_reverse(file, sym, insn) \ 104 for (insn = list_prev_entry(insn, list); \ 105 &insn->list != &file->insn_list && \ 106 insn->sec == sym->sec && insn->offset >= sym->offset; \ 107 insn = list_prev_entry(insn, list)) 108 109 #define sec_for_each_insn_from(file, insn) \ 110 for (; insn; insn = next_insn_same_sec(file, insn)) 111 112 #define sec_for_each_insn_continue(file, insn) \ 113 for (insn = next_insn_same_sec(file, insn); insn; \ 114 insn = next_insn_same_sec(file, insn)) 115 116 static bool is_jump_table_jump(struct instruction *insn) 117 { 118 struct alt_group *alt_group = insn->alt_group; 119 120 if (insn->jump_table) 121 return true; 122 123 /* Retpoline alternative for a jump table? */ 124 return alt_group && alt_group->orig_group && 125 alt_group->orig_group->first_insn->jump_table; 126 } 127 128 static bool is_sibling_call(struct instruction *insn) 129 { 130 /* 131 * Assume only ELF functions can make sibling calls. This ensures 132 * sibling call detection consistency between vmlinux.o and individual 133 * objects. 134 */ 135 if (!insn->func) 136 return false; 137 138 /* An indirect jump is either a sibling call or a jump to a table. */ 139 if (insn->type == INSN_JUMP_DYNAMIC) 140 return !is_jump_table_jump(insn); 141 142 /* add_jump_destinations() sets insn->call_dest for sibling calls. */ 143 return (is_static_jump(insn) && insn->call_dest); 144 } 145 146 /* 147 * This checks to see if the given function is a "noreturn" function. 148 * 149 * For global functions which are outside the scope of this object file, we 150 * have to keep a manual list of them. 151 * 152 * For local functions, we have to detect them manually by simply looking for 153 * the lack of a return instruction. 154 */ 155 static bool __dead_end_function(struct objtool_file *file, struct symbol *func, 156 int recursion) 157 { 158 int i; 159 struct instruction *insn; 160 bool empty = true; 161 162 /* 163 * Unfortunately these have to be hard coded because the noreturn 164 * attribute isn't provided in ELF data. 165 */ 166 static const char * const global_noreturns[] = { 167 "__stack_chk_fail", 168 "panic", 169 "do_exit", 170 "do_task_dead", 171 "kthread_exit", 172 "make_task_dead", 173 "__module_put_and_kthread_exit", 174 "kthread_complete_and_exit", 175 "__reiserfs_panic", 176 "lbug_with_loc", 177 "fortify_panic", 178 "usercopy_abort", 179 "machine_real_restart", 180 "rewind_stack_and_make_dead", 181 "kunit_try_catch_throw", 182 "xen_start_kernel", 183 "cpu_bringup_and_idle", 184 "do_group_exit", 185 "stop_this_cpu", 186 "__invalid_creds", 187 }; 188 189 if (!func) 190 return false; 191 192 if (func->bind == STB_WEAK) 193 return false; 194 195 if (func->bind == STB_GLOBAL) 196 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) 197 if (!strcmp(func->name, global_noreturns[i])) 198 return true; 199 200 if (!func->len) 201 return false; 202 203 insn = find_insn(file, func->sec, func->offset); 204 if (!insn->func) 205 return false; 206 207 func_for_each_insn(file, func, insn) { 208 empty = false; 209 210 if (insn->type == INSN_RETURN) 211 return false; 212 } 213 214 if (empty) 215 return false; 216 217 /* 218 * A function can have a sibling call instead of a return. In that 219 * case, the function's dead-end status depends on whether the target 220 * of the sibling call returns. 221 */ 222 func_for_each_insn(file, func, insn) { 223 if (is_sibling_call(insn)) { 224 struct instruction *dest = insn->jump_dest; 225 226 if (!dest) 227 /* sibling call to another file */ 228 return false; 229 230 /* local sibling call */ 231 if (recursion == 5) { 232 /* 233 * Infinite recursion: two functions have 234 * sibling calls to each other. This is a very 235 * rare case. It means they aren't dead ends. 236 */ 237 return false; 238 } 239 240 return __dead_end_function(file, dest->func, recursion+1); 241 } 242 } 243 244 return true; 245 } 246 247 static bool dead_end_function(struct objtool_file *file, struct symbol *func) 248 { 249 return __dead_end_function(file, func, 0); 250 } 251 252 static void init_cfi_state(struct cfi_state *cfi) 253 { 254 int i; 255 256 for (i = 0; i < CFI_NUM_REGS; i++) { 257 cfi->regs[i].base = CFI_UNDEFINED; 258 cfi->vals[i].base = CFI_UNDEFINED; 259 } 260 cfi->cfa.base = CFI_UNDEFINED; 261 cfi->drap_reg = CFI_UNDEFINED; 262 cfi->drap_offset = -1; 263 } 264 265 static void init_insn_state(struct insn_state *state, struct section *sec) 266 { 267 memset(state, 0, sizeof(*state)); 268 init_cfi_state(&state->cfi); 269 270 /* 271 * We need the full vmlinux for noinstr validation, otherwise we can 272 * not correctly determine insn->call_dest->sec (external symbols do 273 * not have a section). 274 */ 275 if (vmlinux && noinstr && sec) 276 state->noinstr = sec->noinstr; 277 } 278 279 static struct cfi_state *cfi_alloc(void) 280 { 281 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1); 282 if (!cfi) { 283 WARN("calloc failed"); 284 exit(1); 285 } 286 nr_cfi++; 287 return cfi; 288 } 289 290 static int cfi_bits; 291 static struct hlist_head *cfi_hash; 292 293 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) 294 { 295 return memcmp((void *)cfi1 + sizeof(cfi1->hash), 296 (void *)cfi2 + sizeof(cfi2->hash), 297 sizeof(struct cfi_state) - sizeof(struct hlist_node)); 298 } 299 300 static inline u32 cfi_key(struct cfi_state *cfi) 301 { 302 return jhash((void *)cfi + sizeof(cfi->hash), 303 sizeof(*cfi) - sizeof(cfi->hash), 0); 304 } 305 306 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) 307 { 308 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; 309 struct cfi_state *obj; 310 311 hlist_for_each_entry(obj, head, hash) { 312 if (!cficmp(cfi, obj)) { 313 nr_cfi_cache++; 314 return obj; 315 } 316 } 317 318 obj = cfi_alloc(); 319 *obj = *cfi; 320 hlist_add_head(&obj->hash, head); 321 322 return obj; 323 } 324 325 static void cfi_hash_add(struct cfi_state *cfi) 326 { 327 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; 328 329 hlist_add_head(&cfi->hash, head); 330 } 331 332 static void *cfi_hash_alloc(unsigned long size) 333 { 334 cfi_bits = max(10, ilog2(size)); 335 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, 336 PROT_READ|PROT_WRITE, 337 MAP_PRIVATE|MAP_ANON, -1, 0); 338 if (cfi_hash == (void *)-1L) { 339 WARN("mmap fail cfi_hash"); 340 cfi_hash = NULL; 341 } else if (stats) { 342 printf("cfi_bits: %d\n", cfi_bits); 343 } 344 345 return cfi_hash; 346 } 347 348 static unsigned long nr_insns; 349 static unsigned long nr_insns_visited; 350 351 /* 352 * Call the arch-specific instruction decoder for all the instructions and add 353 * them to the global instruction list. 354 */ 355 static int decode_instructions(struct objtool_file *file) 356 { 357 struct section *sec; 358 struct symbol *func; 359 unsigned long offset; 360 struct instruction *insn; 361 int ret; 362 363 for_each_sec(file, sec) { 364 365 if (!(sec->sh.sh_flags & SHF_EXECINSTR)) 366 continue; 367 368 if (strcmp(sec->name, ".altinstr_replacement") && 369 strcmp(sec->name, ".altinstr_aux") && 370 strncmp(sec->name, ".discard.", 9)) 371 sec->text = true; 372 373 if (!strcmp(sec->name, ".noinstr.text") || 374 !strcmp(sec->name, ".entry.text")) 375 sec->noinstr = true; 376 377 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { 378 insn = malloc(sizeof(*insn)); 379 if (!insn) { 380 WARN("malloc failed"); 381 return -1; 382 } 383 memset(insn, 0, sizeof(*insn)); 384 INIT_LIST_HEAD(&insn->alts); 385 INIT_LIST_HEAD(&insn->stack_ops); 386 INIT_LIST_HEAD(&insn->call_node); 387 388 insn->sec = sec; 389 insn->offset = offset; 390 391 ret = arch_decode_instruction(file, sec, offset, 392 sec->sh.sh_size - offset, 393 &insn->len, &insn->type, 394 &insn->immediate, 395 &insn->stack_ops); 396 if (ret) 397 goto err; 398 399 /* 400 * By default, "ud2" is a dead end unless otherwise 401 * annotated, because GCC 7 inserts it for certain 402 * divide-by-zero cases. 403 */ 404 if (insn->type == INSN_BUG) 405 insn->dead_end = true; 406 407 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); 408 list_add_tail(&insn->list, &file->insn_list); 409 nr_insns++; 410 } 411 412 list_for_each_entry(func, &sec->symbol_list, list) { 413 if (func->type != STT_FUNC || func->alias != func) 414 continue; 415 416 if (!find_insn(file, sec, func->offset)) { 417 WARN("%s(): can't find starting instruction", 418 func->name); 419 return -1; 420 } 421 422 sym_for_each_insn(file, func, insn) { 423 insn->func = func; 424 if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) { 425 if (insn->offset == insn->func->offset) { 426 list_add_tail(&insn->call_node, &file->endbr_list); 427 file->nr_endbr++; 428 } else { 429 file->nr_endbr_int++; 430 } 431 } 432 } 433 } 434 } 435 436 if (stats) 437 printf("nr_insns: %lu\n", nr_insns); 438 439 return 0; 440 441 err: 442 free(insn); 443 return ret; 444 } 445 446 /* 447 * Read the pv_ops[] .data table to find the static initialized values. 448 */ 449 static int add_pv_ops(struct objtool_file *file, const char *symname) 450 { 451 struct symbol *sym, *func; 452 unsigned long off, end; 453 struct reloc *rel; 454 int idx; 455 456 sym = find_symbol_by_name(file->elf, symname); 457 if (!sym) 458 return 0; 459 460 off = sym->offset; 461 end = off + sym->len; 462 for (;;) { 463 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); 464 if (!rel) 465 break; 466 467 func = rel->sym; 468 if (func->type == STT_SECTION) 469 func = find_symbol_by_offset(rel->sym->sec, rel->addend); 470 471 idx = (rel->offset - sym->offset) / sizeof(unsigned long); 472 473 objtool_pv_add(file, idx, func); 474 475 off = rel->offset + 1; 476 if (off > end) 477 break; 478 } 479 480 return 0; 481 } 482 483 /* 484 * Allocate and initialize file->pv_ops[]. 485 */ 486 static int init_pv_ops(struct objtool_file *file) 487 { 488 static const char *pv_ops_tables[] = { 489 "pv_ops", 490 "xen_cpu_ops", 491 "xen_irq_ops", 492 "xen_mmu_ops", 493 NULL, 494 }; 495 const char *pv_ops; 496 struct symbol *sym; 497 int idx, nr; 498 499 if (!noinstr) 500 return 0; 501 502 file->pv_ops = NULL; 503 504 sym = find_symbol_by_name(file->elf, "pv_ops"); 505 if (!sym) 506 return 0; 507 508 nr = sym->len / sizeof(unsigned long); 509 file->pv_ops = calloc(sizeof(struct pv_state), nr); 510 if (!file->pv_ops) 511 return -1; 512 513 for (idx = 0; idx < nr; idx++) 514 INIT_LIST_HEAD(&file->pv_ops[idx].targets); 515 516 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++) 517 add_pv_ops(file, pv_ops); 518 519 return 0; 520 } 521 522 static struct instruction *find_last_insn(struct objtool_file *file, 523 struct section *sec) 524 { 525 struct instruction *insn = NULL; 526 unsigned int offset; 527 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0; 528 529 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--) 530 insn = find_insn(file, sec, offset); 531 532 return insn; 533 } 534 535 /* 536 * Mark "ud2" instructions and manually annotated dead ends. 537 */ 538 static int add_dead_ends(struct objtool_file *file) 539 { 540 struct section *sec; 541 struct reloc *reloc; 542 struct instruction *insn; 543 544 /* 545 * Check for manually annotated dead ends. 546 */ 547 sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); 548 if (!sec) 549 goto reachable; 550 551 list_for_each_entry(reloc, &sec->reloc_list, list) { 552 if (reloc->sym->type != STT_SECTION) { 553 WARN("unexpected relocation symbol type in %s", sec->name); 554 return -1; 555 } 556 insn = find_insn(file, reloc->sym->sec, reloc->addend); 557 if (insn) 558 insn = list_prev_entry(insn, list); 559 else if (reloc->addend == reloc->sym->sec->sh.sh_size) { 560 insn = find_last_insn(file, reloc->sym->sec); 561 if (!insn) { 562 WARN("can't find unreachable insn at %s+0x%x", 563 reloc->sym->sec->name, reloc->addend); 564 return -1; 565 } 566 } else { 567 WARN("can't find unreachable insn at %s+0x%x", 568 reloc->sym->sec->name, reloc->addend); 569 return -1; 570 } 571 572 insn->dead_end = true; 573 } 574 575 reachable: 576 /* 577 * These manually annotated reachable checks are needed for GCC 4.4, 578 * where the Linux unreachable() macro isn't supported. In that case 579 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's 580 * not a dead end. 581 */ 582 sec = find_section_by_name(file->elf, ".rela.discard.reachable"); 583 if (!sec) 584 return 0; 585 586 list_for_each_entry(reloc, &sec->reloc_list, list) { 587 if (reloc->sym->type != STT_SECTION) { 588 WARN("unexpected relocation symbol type in %s", sec->name); 589 return -1; 590 } 591 insn = find_insn(file, reloc->sym->sec, reloc->addend); 592 if (insn) 593 insn = list_prev_entry(insn, list); 594 else if (reloc->addend == reloc->sym->sec->sh.sh_size) { 595 insn = find_last_insn(file, reloc->sym->sec); 596 if (!insn) { 597 WARN("can't find reachable insn at %s+0x%x", 598 reloc->sym->sec->name, reloc->addend); 599 return -1; 600 } 601 } else { 602 WARN("can't find reachable insn at %s+0x%x", 603 reloc->sym->sec->name, reloc->addend); 604 return -1; 605 } 606 607 insn->dead_end = false; 608 } 609 610 return 0; 611 } 612 613 static int create_static_call_sections(struct objtool_file *file) 614 { 615 struct section *sec; 616 struct static_call_site *site; 617 struct instruction *insn; 618 struct symbol *key_sym; 619 char *key_name, *tmp; 620 int idx; 621 622 sec = find_section_by_name(file->elf, ".static_call_sites"); 623 if (sec) { 624 INIT_LIST_HEAD(&file->static_call_list); 625 WARN("file already has .static_call_sites section, skipping"); 626 return 0; 627 } 628 629 if (list_empty(&file->static_call_list)) 630 return 0; 631 632 idx = 0; 633 list_for_each_entry(insn, &file->static_call_list, call_node) 634 idx++; 635 636 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, 637 sizeof(struct static_call_site), idx); 638 if (!sec) 639 return -1; 640 641 idx = 0; 642 list_for_each_entry(insn, &file->static_call_list, call_node) { 643 644 site = (struct static_call_site *)sec->data->d_buf + idx; 645 memset(site, 0, sizeof(struct static_call_site)); 646 647 /* populate reloc for 'addr' */ 648 if (elf_add_reloc_to_insn(file->elf, sec, 649 idx * sizeof(struct static_call_site), 650 R_X86_64_PC32, 651 insn->sec, insn->offset)) 652 return -1; 653 654 /* find key symbol */ 655 key_name = strdup(insn->call_dest->name); 656 if (!key_name) { 657 perror("strdup"); 658 return -1; 659 } 660 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR, 661 STATIC_CALL_TRAMP_PREFIX_LEN)) { 662 WARN("static_call: trampoline name malformed: %s", key_name); 663 return -1; 664 } 665 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN; 666 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN); 667 668 key_sym = find_symbol_by_name(file->elf, tmp); 669 if (!key_sym) { 670 if (!module) { 671 WARN("static_call: can't find static_call_key symbol: %s", tmp); 672 return -1; 673 } 674 675 /* 676 * For modules(), the key might not be exported, which 677 * means the module can make static calls but isn't 678 * allowed to change them. 679 * 680 * In that case we temporarily set the key to be the 681 * trampoline address. This is fixed up in 682 * static_call_add_module(). 683 */ 684 key_sym = insn->call_dest; 685 } 686 free(key_name); 687 688 /* populate reloc for 'key' */ 689 if (elf_add_reloc(file->elf, sec, 690 idx * sizeof(struct static_call_site) + 4, 691 R_X86_64_PC32, key_sym, 692 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) 693 return -1; 694 695 idx++; 696 } 697 698 return 0; 699 } 700 701 static int create_retpoline_sites_sections(struct objtool_file *file) 702 { 703 struct instruction *insn; 704 struct section *sec; 705 int idx; 706 707 sec = find_section_by_name(file->elf, ".retpoline_sites"); 708 if (sec) { 709 WARN("file already has .retpoline_sites, skipping"); 710 return 0; 711 } 712 713 idx = 0; 714 list_for_each_entry(insn, &file->retpoline_call_list, call_node) 715 idx++; 716 717 if (!idx) 718 return 0; 719 720 sec = elf_create_section(file->elf, ".retpoline_sites", 0, 721 sizeof(int), idx); 722 if (!sec) { 723 WARN("elf_create_section: .retpoline_sites"); 724 return -1; 725 } 726 727 idx = 0; 728 list_for_each_entry(insn, &file->retpoline_call_list, call_node) { 729 730 int *site = (int *)sec->data->d_buf + idx; 731 *site = 0; 732 733 if (elf_add_reloc_to_insn(file->elf, sec, 734 idx * sizeof(int), 735 R_X86_64_PC32, 736 insn->sec, insn->offset)) { 737 WARN("elf_add_reloc_to_insn: .retpoline_sites"); 738 return -1; 739 } 740 741 idx++; 742 } 743 744 return 0; 745 } 746 747 static int create_ibt_endbr_seal_sections(struct objtool_file *file) 748 { 749 struct instruction *insn; 750 struct section *sec; 751 int idx; 752 753 sec = find_section_by_name(file->elf, ".ibt_endbr_seal"); 754 if (sec) { 755 WARN("file already has .ibt_endbr_seal, skipping"); 756 return 0; 757 } 758 759 idx = 0; 760 list_for_each_entry(insn, &file->endbr_list, call_node) 761 idx++; 762 763 if (stats) { 764 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr); 765 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int); 766 printf("ibt: superfluous ENDBR: %d\n", idx); 767 } 768 769 if (!idx) 770 return 0; 771 772 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0, 773 sizeof(int), idx); 774 if (!sec) { 775 WARN("elf_create_section: .ibt_endbr_seal"); 776 return -1; 777 } 778 779 idx = 0; 780 list_for_each_entry(insn, &file->endbr_list, call_node) { 781 782 int *site = (int *)sec->data->d_buf + idx; 783 *site = 0; 784 785 if (elf_add_reloc_to_insn(file->elf, sec, 786 idx * sizeof(int), 787 R_X86_64_PC32, 788 insn->sec, insn->offset)) { 789 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal"); 790 return -1; 791 } 792 793 idx++; 794 } 795 796 return 0; 797 } 798 799 static int create_mcount_loc_sections(struct objtool_file *file) 800 { 801 struct section *sec; 802 unsigned long *loc; 803 struct instruction *insn; 804 int idx; 805 806 sec = find_section_by_name(file->elf, "__mcount_loc"); 807 if (sec) { 808 INIT_LIST_HEAD(&file->mcount_loc_list); 809 WARN("file already has __mcount_loc section, skipping"); 810 return 0; 811 } 812 813 if (list_empty(&file->mcount_loc_list)) 814 return 0; 815 816 idx = 0; 817 list_for_each_entry(insn, &file->mcount_loc_list, call_node) 818 idx++; 819 820 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx); 821 if (!sec) 822 return -1; 823 824 idx = 0; 825 list_for_each_entry(insn, &file->mcount_loc_list, call_node) { 826 827 loc = (unsigned long *)sec->data->d_buf + idx; 828 memset(loc, 0, sizeof(unsigned long)); 829 830 if (elf_add_reloc_to_insn(file->elf, sec, 831 idx * sizeof(unsigned long), 832 R_X86_64_64, 833 insn->sec, insn->offset)) 834 return -1; 835 836 idx++; 837 } 838 839 return 0; 840 } 841 842 /* 843 * Warnings shouldn't be reported for ignored functions. 844 */ 845 static void add_ignores(struct objtool_file *file) 846 { 847 struct instruction *insn; 848 struct section *sec; 849 struct symbol *func; 850 struct reloc *reloc; 851 852 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); 853 if (!sec) 854 return; 855 856 list_for_each_entry(reloc, &sec->reloc_list, list) { 857 switch (reloc->sym->type) { 858 case STT_FUNC: 859 func = reloc->sym; 860 break; 861 862 case STT_SECTION: 863 func = find_func_by_offset(reloc->sym->sec, reloc->addend); 864 if (!func) 865 continue; 866 break; 867 868 default: 869 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); 870 continue; 871 } 872 873 func_for_each_insn(file, func, insn) 874 insn->ignore = true; 875 } 876 } 877 878 /* 879 * This is a whitelist of functions that is allowed to be called with AC set. 880 * The list is meant to be minimal and only contains compiler instrumentation 881 * ABI and a few functions used to implement *_{to,from}_user() functions. 882 * 883 * These functions must not directly change AC, but may PUSHF/POPF. 884 */ 885 static const char *uaccess_safe_builtin[] = { 886 /* KASAN */ 887 "kasan_report", 888 "kasan_check_range", 889 /* KASAN out-of-line */ 890 "__asan_loadN_noabort", 891 "__asan_load1_noabort", 892 "__asan_load2_noabort", 893 "__asan_load4_noabort", 894 "__asan_load8_noabort", 895 "__asan_load16_noabort", 896 "__asan_storeN_noabort", 897 "__asan_store1_noabort", 898 "__asan_store2_noabort", 899 "__asan_store4_noabort", 900 "__asan_store8_noabort", 901 "__asan_store16_noabort", 902 "__kasan_check_read", 903 "__kasan_check_write", 904 /* KASAN in-line */ 905 "__asan_report_load_n_noabort", 906 "__asan_report_load1_noabort", 907 "__asan_report_load2_noabort", 908 "__asan_report_load4_noabort", 909 "__asan_report_load8_noabort", 910 "__asan_report_load16_noabort", 911 "__asan_report_store_n_noabort", 912 "__asan_report_store1_noabort", 913 "__asan_report_store2_noabort", 914 "__asan_report_store4_noabort", 915 "__asan_report_store8_noabort", 916 "__asan_report_store16_noabort", 917 /* KCSAN */ 918 "__kcsan_check_access", 919 "__kcsan_mb", 920 "__kcsan_wmb", 921 "__kcsan_rmb", 922 "__kcsan_release", 923 "kcsan_found_watchpoint", 924 "kcsan_setup_watchpoint", 925 "kcsan_check_scoped_accesses", 926 "kcsan_disable_current", 927 "kcsan_enable_current_nowarn", 928 /* KCSAN/TSAN */ 929 "__tsan_func_entry", 930 "__tsan_func_exit", 931 "__tsan_read_range", 932 "__tsan_write_range", 933 "__tsan_read1", 934 "__tsan_read2", 935 "__tsan_read4", 936 "__tsan_read8", 937 "__tsan_read16", 938 "__tsan_write1", 939 "__tsan_write2", 940 "__tsan_write4", 941 "__tsan_write8", 942 "__tsan_write16", 943 "__tsan_read_write1", 944 "__tsan_read_write2", 945 "__tsan_read_write4", 946 "__tsan_read_write8", 947 "__tsan_read_write16", 948 "__tsan_atomic8_load", 949 "__tsan_atomic16_load", 950 "__tsan_atomic32_load", 951 "__tsan_atomic64_load", 952 "__tsan_atomic8_store", 953 "__tsan_atomic16_store", 954 "__tsan_atomic32_store", 955 "__tsan_atomic64_store", 956 "__tsan_atomic8_exchange", 957 "__tsan_atomic16_exchange", 958 "__tsan_atomic32_exchange", 959 "__tsan_atomic64_exchange", 960 "__tsan_atomic8_fetch_add", 961 "__tsan_atomic16_fetch_add", 962 "__tsan_atomic32_fetch_add", 963 "__tsan_atomic64_fetch_add", 964 "__tsan_atomic8_fetch_sub", 965 "__tsan_atomic16_fetch_sub", 966 "__tsan_atomic32_fetch_sub", 967 "__tsan_atomic64_fetch_sub", 968 "__tsan_atomic8_fetch_and", 969 "__tsan_atomic16_fetch_and", 970 "__tsan_atomic32_fetch_and", 971 "__tsan_atomic64_fetch_and", 972 "__tsan_atomic8_fetch_or", 973 "__tsan_atomic16_fetch_or", 974 "__tsan_atomic32_fetch_or", 975 "__tsan_atomic64_fetch_or", 976 "__tsan_atomic8_fetch_xor", 977 "__tsan_atomic16_fetch_xor", 978 "__tsan_atomic32_fetch_xor", 979 "__tsan_atomic64_fetch_xor", 980 "__tsan_atomic8_fetch_nand", 981 "__tsan_atomic16_fetch_nand", 982 "__tsan_atomic32_fetch_nand", 983 "__tsan_atomic64_fetch_nand", 984 "__tsan_atomic8_compare_exchange_strong", 985 "__tsan_atomic16_compare_exchange_strong", 986 "__tsan_atomic32_compare_exchange_strong", 987 "__tsan_atomic64_compare_exchange_strong", 988 "__tsan_atomic8_compare_exchange_weak", 989 "__tsan_atomic16_compare_exchange_weak", 990 "__tsan_atomic32_compare_exchange_weak", 991 "__tsan_atomic64_compare_exchange_weak", 992 "__tsan_atomic8_compare_exchange_val", 993 "__tsan_atomic16_compare_exchange_val", 994 "__tsan_atomic32_compare_exchange_val", 995 "__tsan_atomic64_compare_exchange_val", 996 "__tsan_atomic_thread_fence", 997 "__tsan_atomic_signal_fence", 998 /* KCOV */ 999 "write_comp_data", 1000 "check_kcov_mode", 1001 "__sanitizer_cov_trace_pc", 1002 "__sanitizer_cov_trace_const_cmp1", 1003 "__sanitizer_cov_trace_const_cmp2", 1004 "__sanitizer_cov_trace_const_cmp4", 1005 "__sanitizer_cov_trace_const_cmp8", 1006 "__sanitizer_cov_trace_cmp1", 1007 "__sanitizer_cov_trace_cmp2", 1008 "__sanitizer_cov_trace_cmp4", 1009 "__sanitizer_cov_trace_cmp8", 1010 "__sanitizer_cov_trace_switch", 1011 /* UBSAN */ 1012 "ubsan_type_mismatch_common", 1013 "__ubsan_handle_type_mismatch", 1014 "__ubsan_handle_type_mismatch_v1", 1015 "__ubsan_handle_shift_out_of_bounds", 1016 /* misc */ 1017 "csum_partial_copy_generic", 1018 "copy_mc_fragile", 1019 "copy_mc_fragile_handle_tail", 1020 "copy_mc_enhanced_fast_string", 1021 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */ 1022 NULL 1023 }; 1024 1025 static void add_uaccess_safe(struct objtool_file *file) 1026 { 1027 struct symbol *func; 1028 const char **name; 1029 1030 if (!uaccess) 1031 return; 1032 1033 for (name = uaccess_safe_builtin; *name; name++) { 1034 func = find_symbol_by_name(file->elf, *name); 1035 if (!func) 1036 continue; 1037 1038 func->uaccess_safe = true; 1039 } 1040 } 1041 1042 /* 1043 * FIXME: For now, just ignore any alternatives which add retpolines. This is 1044 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline. 1045 * But it at least allows objtool to understand the control flow *around* the 1046 * retpoline. 1047 */ 1048 static int add_ignore_alternatives(struct objtool_file *file) 1049 { 1050 struct section *sec; 1051 struct reloc *reloc; 1052 struct instruction *insn; 1053 1054 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); 1055 if (!sec) 1056 return 0; 1057 1058 list_for_each_entry(reloc, &sec->reloc_list, list) { 1059 if (reloc->sym->type != STT_SECTION) { 1060 WARN("unexpected relocation symbol type in %s", sec->name); 1061 return -1; 1062 } 1063 1064 insn = find_insn(file, reloc->sym->sec, reloc->addend); 1065 if (!insn) { 1066 WARN("bad .discard.ignore_alts entry"); 1067 return -1; 1068 } 1069 1070 insn->ignore_alts = true; 1071 } 1072 1073 return 0; 1074 } 1075 1076 __weak bool arch_is_retpoline(struct symbol *sym) 1077 { 1078 return false; 1079 } 1080 1081 #define NEGATIVE_RELOC ((void *)-1L) 1082 1083 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) 1084 { 1085 if (insn->reloc == NEGATIVE_RELOC) 1086 return NULL; 1087 1088 if (!insn->reloc) { 1089 if (!file) 1090 return NULL; 1091 1092 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec, 1093 insn->offset, insn->len); 1094 if (!insn->reloc) { 1095 insn->reloc = NEGATIVE_RELOC; 1096 return NULL; 1097 } 1098 } 1099 1100 return insn->reloc; 1101 } 1102 1103 static void remove_insn_ops(struct instruction *insn) 1104 { 1105 struct stack_op *op, *tmp; 1106 1107 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { 1108 list_del(&op->list); 1109 free(op); 1110 } 1111 } 1112 1113 static void annotate_call_site(struct objtool_file *file, 1114 struct instruction *insn, bool sibling) 1115 { 1116 struct reloc *reloc = insn_reloc(file, insn); 1117 struct symbol *sym = insn->call_dest; 1118 1119 if (!sym) 1120 sym = reloc->sym; 1121 1122 /* 1123 * Alternative replacement code is just template code which is 1124 * sometimes copied to the original instruction. For now, don't 1125 * annotate it. (In the future we might consider annotating the 1126 * original instruction if/when it ever makes sense to do so.) 1127 */ 1128 if (!strcmp(insn->sec->name, ".altinstr_replacement")) 1129 return; 1130 1131 if (sym->static_call_tramp) { 1132 list_add_tail(&insn->call_node, &file->static_call_list); 1133 return; 1134 } 1135 1136 if (sym->retpoline_thunk) { 1137 list_add_tail(&insn->call_node, &file->retpoline_call_list); 1138 return; 1139 } 1140 1141 /* 1142 * Many compilers cannot disable KCOV or sanitizer calls with a function 1143 * attribute so they need a little help, NOP out any such calls from 1144 * noinstr text. 1145 */ 1146 if (insn->sec->noinstr && sym->profiling_func) { 1147 if (reloc) { 1148 reloc->type = R_NONE; 1149 elf_write_reloc(file->elf, reloc); 1150 } 1151 1152 elf_write_insn(file->elf, insn->sec, 1153 insn->offset, insn->len, 1154 sibling ? arch_ret_insn(insn->len) 1155 : arch_nop_insn(insn->len)); 1156 1157 insn->type = sibling ? INSN_RETURN : INSN_NOP; 1158 1159 if (sibling) { 1160 /* 1161 * We've replaced the tail-call JMP insn by two new 1162 * insn: RET; INT3, except we only have a single struct 1163 * insn here. Mark it retpoline_safe to avoid the SLS 1164 * warning, instead of adding another insn. 1165 */ 1166 insn->retpoline_safe = true; 1167 } 1168 1169 return; 1170 } 1171 1172 if (mcount && sym->fentry) { 1173 if (sibling) 1174 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset); 1175 1176 if (reloc) { 1177 reloc->type = R_NONE; 1178 elf_write_reloc(file->elf, reloc); 1179 } 1180 1181 elf_write_insn(file->elf, insn->sec, 1182 insn->offset, insn->len, 1183 arch_nop_insn(insn->len)); 1184 1185 insn->type = INSN_NOP; 1186 1187 list_add_tail(&insn->call_node, &file->mcount_loc_list); 1188 return; 1189 } 1190 1191 if (!sibling && dead_end_function(file, sym)) 1192 insn->dead_end = true; 1193 } 1194 1195 static void add_call_dest(struct objtool_file *file, struct instruction *insn, 1196 struct symbol *dest, bool sibling) 1197 { 1198 insn->call_dest = dest; 1199 if (!dest) 1200 return; 1201 1202 /* 1203 * Whatever stack impact regular CALLs have, should be undone 1204 * by the RETURN of the called function. 1205 * 1206 * Annotated intra-function calls retain the stack_ops but 1207 * are converted to JUMP, see read_intra_function_calls(). 1208 */ 1209 remove_insn_ops(insn); 1210 1211 annotate_call_site(file, insn, sibling); 1212 } 1213 1214 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn) 1215 { 1216 /* 1217 * Retpoline calls/jumps are really dynamic calls/jumps in disguise, 1218 * so convert them accordingly. 1219 */ 1220 switch (insn->type) { 1221 case INSN_CALL: 1222 insn->type = INSN_CALL_DYNAMIC; 1223 break; 1224 case INSN_JUMP_UNCONDITIONAL: 1225 insn->type = INSN_JUMP_DYNAMIC; 1226 break; 1227 case INSN_JUMP_CONDITIONAL: 1228 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; 1229 break; 1230 default: 1231 return; 1232 } 1233 1234 insn->retpoline_safe = true; 1235 1236 /* 1237 * Whatever stack impact regular CALLs have, should be undone 1238 * by the RETURN of the called function. 1239 * 1240 * Annotated intra-function calls retain the stack_ops but 1241 * are converted to JUMP, see read_intra_function_calls(). 1242 */ 1243 remove_insn_ops(insn); 1244 1245 annotate_call_site(file, insn, false); 1246 } 1247 1248 static bool same_function(struct instruction *insn1, struct instruction *insn2) 1249 { 1250 return insn1->func->pfunc == insn2->func->pfunc; 1251 } 1252 1253 static bool is_first_func_insn(struct objtool_file *file, struct instruction *insn) 1254 { 1255 if (insn->offset == insn->func->offset) 1256 return true; 1257 1258 if (ibt) { 1259 struct instruction *prev = prev_insn_same_sym(file, insn); 1260 1261 if (prev && prev->type == INSN_ENDBR && 1262 insn->offset == insn->func->offset + prev->len) 1263 return true; 1264 } 1265 1266 return false; 1267 } 1268 1269 /* 1270 * Find the destination instructions for all jumps. 1271 */ 1272 static int add_jump_destinations(struct objtool_file *file) 1273 { 1274 struct instruction *insn; 1275 struct reloc *reloc; 1276 struct section *dest_sec; 1277 unsigned long dest_off; 1278 1279 for_each_insn(file, insn) { 1280 if (!is_static_jump(insn)) 1281 continue; 1282 1283 reloc = insn_reloc(file, insn); 1284 if (!reloc) { 1285 dest_sec = insn->sec; 1286 dest_off = arch_jump_destination(insn); 1287 } else if (reloc->sym->type == STT_SECTION) { 1288 dest_sec = reloc->sym->sec; 1289 dest_off = arch_dest_reloc_offset(reloc->addend); 1290 } else if (reloc->sym->retpoline_thunk) { 1291 add_retpoline_call(file, insn); 1292 continue; 1293 } else if (insn->func) { 1294 /* internal or external sibling call (with reloc) */ 1295 add_call_dest(file, insn, reloc->sym, true); 1296 continue; 1297 } else if (reloc->sym->sec->idx) { 1298 dest_sec = reloc->sym->sec; 1299 dest_off = reloc->sym->sym.st_value + 1300 arch_dest_reloc_offset(reloc->addend); 1301 } else { 1302 /* non-func asm code jumping to another file */ 1303 continue; 1304 } 1305 1306 insn->jump_dest = find_insn(file, dest_sec, dest_off); 1307 if (!insn->jump_dest) { 1308 1309 /* 1310 * This is a special case where an alt instruction 1311 * jumps past the end of the section. These are 1312 * handled later in handle_group_alt(). 1313 */ 1314 if (!strcmp(insn->sec->name, ".altinstr_replacement")) 1315 continue; 1316 1317 WARN_FUNC("can't find jump dest instruction at %s+0x%lx", 1318 insn->sec, insn->offset, dest_sec->name, 1319 dest_off); 1320 return -1; 1321 } 1322 1323 /* 1324 * Cross-function jump. 1325 */ 1326 if (insn->func && insn->jump_dest->func && 1327 insn->func != insn->jump_dest->func) { 1328 1329 /* 1330 * For GCC 8+, create parent/child links for any cold 1331 * subfunctions. This is _mostly_ redundant with a 1332 * similar initialization in read_symbols(). 1333 * 1334 * If a function has aliases, we want the *first* such 1335 * function in the symbol table to be the subfunction's 1336 * parent. In that case we overwrite the 1337 * initialization done in read_symbols(). 1338 * 1339 * However this code can't completely replace the 1340 * read_symbols() code because this doesn't detect the 1341 * case where the parent function's only reference to a 1342 * subfunction is through a jump table. 1343 */ 1344 if (!strstr(insn->func->name, ".cold") && 1345 strstr(insn->jump_dest->func->name, ".cold")) { 1346 insn->func->cfunc = insn->jump_dest->func; 1347 insn->jump_dest->func->pfunc = insn->func; 1348 1349 } else if (!same_function(insn, insn->jump_dest) && 1350 is_first_func_insn(file, insn->jump_dest)) { 1351 /* internal sibling call (without reloc) */ 1352 add_call_dest(file, insn, insn->jump_dest->func, true); 1353 } 1354 } 1355 } 1356 1357 return 0; 1358 } 1359 1360 static struct symbol *find_call_destination(struct section *sec, unsigned long offset) 1361 { 1362 struct symbol *call_dest; 1363 1364 call_dest = find_func_by_offset(sec, offset); 1365 if (!call_dest) 1366 call_dest = find_symbol_by_offset(sec, offset); 1367 1368 return call_dest; 1369 } 1370 1371 /* 1372 * Find the destination instructions for all calls. 1373 */ 1374 static int add_call_destinations(struct objtool_file *file) 1375 { 1376 struct instruction *insn; 1377 unsigned long dest_off; 1378 struct symbol *dest; 1379 struct reloc *reloc; 1380 1381 for_each_insn(file, insn) { 1382 if (insn->type != INSN_CALL) 1383 continue; 1384 1385 reloc = insn_reloc(file, insn); 1386 if (!reloc) { 1387 dest_off = arch_jump_destination(insn); 1388 dest = find_call_destination(insn->sec, dest_off); 1389 1390 add_call_dest(file, insn, dest, false); 1391 1392 if (insn->ignore) 1393 continue; 1394 1395 if (!insn->call_dest) { 1396 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); 1397 return -1; 1398 } 1399 1400 if (insn->func && insn->call_dest->type != STT_FUNC) { 1401 WARN_FUNC("unsupported call to non-function", 1402 insn->sec, insn->offset); 1403 return -1; 1404 } 1405 1406 } else if (reloc->sym->type == STT_SECTION) { 1407 dest_off = arch_dest_reloc_offset(reloc->addend); 1408 dest = find_call_destination(reloc->sym->sec, dest_off); 1409 if (!dest) { 1410 WARN_FUNC("can't find call dest symbol at %s+0x%lx", 1411 insn->sec, insn->offset, 1412 reloc->sym->sec->name, 1413 dest_off); 1414 return -1; 1415 } 1416 1417 add_call_dest(file, insn, dest, false); 1418 1419 } else if (reloc->sym->retpoline_thunk) { 1420 add_retpoline_call(file, insn); 1421 1422 } else 1423 add_call_dest(file, insn, reloc->sym, false); 1424 } 1425 1426 return 0; 1427 } 1428 1429 /* 1430 * The .alternatives section requires some extra special care over and above 1431 * other special sections because alternatives are patched in place. 1432 */ 1433 static int handle_group_alt(struct objtool_file *file, 1434 struct special_alt *special_alt, 1435 struct instruction *orig_insn, 1436 struct instruction **new_insn) 1437 { 1438 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL; 1439 struct alt_group *orig_alt_group, *new_alt_group; 1440 unsigned long dest_off; 1441 1442 1443 orig_alt_group = malloc(sizeof(*orig_alt_group)); 1444 if (!orig_alt_group) { 1445 WARN("malloc failed"); 1446 return -1; 1447 } 1448 orig_alt_group->cfi = calloc(special_alt->orig_len, 1449 sizeof(struct cfi_state *)); 1450 if (!orig_alt_group->cfi) { 1451 WARN("calloc failed"); 1452 return -1; 1453 } 1454 1455 last_orig_insn = NULL; 1456 insn = orig_insn; 1457 sec_for_each_insn_from(file, insn) { 1458 if (insn->offset >= special_alt->orig_off + special_alt->orig_len) 1459 break; 1460 1461 insn->alt_group = orig_alt_group; 1462 last_orig_insn = insn; 1463 } 1464 orig_alt_group->orig_group = NULL; 1465 orig_alt_group->first_insn = orig_insn; 1466 orig_alt_group->last_insn = last_orig_insn; 1467 1468 1469 new_alt_group = malloc(sizeof(*new_alt_group)); 1470 if (!new_alt_group) { 1471 WARN("malloc failed"); 1472 return -1; 1473 } 1474 1475 if (special_alt->new_len < special_alt->orig_len) { 1476 /* 1477 * Insert a fake nop at the end to make the replacement 1478 * alt_group the same size as the original. This is needed to 1479 * allow propagate_alt_cfi() to do its magic. When the last 1480 * instruction affects the stack, the instruction after it (the 1481 * nop) will propagate the new state to the shared CFI array. 1482 */ 1483 nop = malloc(sizeof(*nop)); 1484 if (!nop) { 1485 WARN("malloc failed"); 1486 return -1; 1487 } 1488 memset(nop, 0, sizeof(*nop)); 1489 INIT_LIST_HEAD(&nop->alts); 1490 INIT_LIST_HEAD(&nop->stack_ops); 1491 1492 nop->sec = special_alt->new_sec; 1493 nop->offset = special_alt->new_off + special_alt->new_len; 1494 nop->len = special_alt->orig_len - special_alt->new_len; 1495 nop->type = INSN_NOP; 1496 nop->func = orig_insn->func; 1497 nop->alt_group = new_alt_group; 1498 nop->ignore = orig_insn->ignore_alts; 1499 } 1500 1501 if (!special_alt->new_len) { 1502 *new_insn = nop; 1503 goto end; 1504 } 1505 1506 insn = *new_insn; 1507 sec_for_each_insn_from(file, insn) { 1508 struct reloc *alt_reloc; 1509 1510 if (insn->offset >= special_alt->new_off + special_alt->new_len) 1511 break; 1512 1513 last_new_insn = insn; 1514 1515 insn->ignore = orig_insn->ignore_alts; 1516 insn->func = orig_insn->func; 1517 insn->alt_group = new_alt_group; 1518 1519 /* 1520 * Since alternative replacement code is copy/pasted by the 1521 * kernel after applying relocations, generally such code can't 1522 * have relative-address relocation references to outside the 1523 * .altinstr_replacement section, unless the arch's 1524 * alternatives code can adjust the relative offsets 1525 * accordingly. 1526 */ 1527 alt_reloc = insn_reloc(file, insn); 1528 if (alt_reloc && 1529 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) { 1530 1531 WARN_FUNC("unsupported relocation in alternatives section", 1532 insn->sec, insn->offset); 1533 return -1; 1534 } 1535 1536 if (!is_static_jump(insn)) 1537 continue; 1538 1539 if (!insn->immediate) 1540 continue; 1541 1542 dest_off = arch_jump_destination(insn); 1543 if (dest_off == special_alt->new_off + special_alt->new_len) 1544 insn->jump_dest = next_insn_same_sec(file, last_orig_insn); 1545 1546 if (!insn->jump_dest) { 1547 WARN_FUNC("can't find alternative jump destination", 1548 insn->sec, insn->offset); 1549 return -1; 1550 } 1551 } 1552 1553 if (!last_new_insn) { 1554 WARN_FUNC("can't find last new alternative instruction", 1555 special_alt->new_sec, special_alt->new_off); 1556 return -1; 1557 } 1558 1559 if (nop) 1560 list_add(&nop->list, &last_new_insn->list); 1561 end: 1562 new_alt_group->orig_group = orig_alt_group; 1563 new_alt_group->first_insn = *new_insn; 1564 new_alt_group->last_insn = nop ? : last_new_insn; 1565 new_alt_group->cfi = orig_alt_group->cfi; 1566 return 0; 1567 } 1568 1569 /* 1570 * A jump table entry can either convert a nop to a jump or a jump to a nop. 1571 * If the original instruction is a jump, make the alt entry an effective nop 1572 * by just skipping the original instruction. 1573 */ 1574 static int handle_jump_alt(struct objtool_file *file, 1575 struct special_alt *special_alt, 1576 struct instruction *orig_insn, 1577 struct instruction **new_insn) 1578 { 1579 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL && 1580 orig_insn->type != INSN_NOP) { 1581 1582 WARN_FUNC("unsupported instruction at jump label", 1583 orig_insn->sec, orig_insn->offset); 1584 return -1; 1585 } 1586 1587 if (special_alt->key_addend & 2) { 1588 struct reloc *reloc = insn_reloc(file, orig_insn); 1589 1590 if (reloc) { 1591 reloc->type = R_NONE; 1592 elf_write_reloc(file->elf, reloc); 1593 } 1594 elf_write_insn(file->elf, orig_insn->sec, 1595 orig_insn->offset, orig_insn->len, 1596 arch_nop_insn(orig_insn->len)); 1597 orig_insn->type = INSN_NOP; 1598 } 1599 1600 if (orig_insn->type == INSN_NOP) { 1601 if (orig_insn->len == 2) 1602 file->jl_nop_short++; 1603 else 1604 file->jl_nop_long++; 1605 1606 return 0; 1607 } 1608 1609 if (orig_insn->len == 2) 1610 file->jl_short++; 1611 else 1612 file->jl_long++; 1613 1614 *new_insn = list_next_entry(orig_insn, list); 1615 return 0; 1616 } 1617 1618 /* 1619 * Read all the special sections which have alternate instructions which can be 1620 * patched in or redirected to at runtime. Each instruction having alternate 1621 * instruction(s) has them added to its insn->alts list, which will be 1622 * traversed in validate_branch(). 1623 */ 1624 static int add_special_section_alts(struct objtool_file *file) 1625 { 1626 struct list_head special_alts; 1627 struct instruction *orig_insn, *new_insn; 1628 struct special_alt *special_alt, *tmp; 1629 struct alternative *alt; 1630 int ret; 1631 1632 ret = special_get_alts(file->elf, &special_alts); 1633 if (ret) 1634 return ret; 1635 1636 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { 1637 1638 orig_insn = find_insn(file, special_alt->orig_sec, 1639 special_alt->orig_off); 1640 if (!orig_insn) { 1641 WARN_FUNC("special: can't find orig instruction", 1642 special_alt->orig_sec, special_alt->orig_off); 1643 ret = -1; 1644 goto out; 1645 } 1646 1647 new_insn = NULL; 1648 if (!special_alt->group || special_alt->new_len) { 1649 new_insn = find_insn(file, special_alt->new_sec, 1650 special_alt->new_off); 1651 if (!new_insn) { 1652 WARN_FUNC("special: can't find new instruction", 1653 special_alt->new_sec, 1654 special_alt->new_off); 1655 ret = -1; 1656 goto out; 1657 } 1658 } 1659 1660 if (special_alt->group) { 1661 if (!special_alt->orig_len) { 1662 WARN_FUNC("empty alternative entry", 1663 orig_insn->sec, orig_insn->offset); 1664 continue; 1665 } 1666 1667 ret = handle_group_alt(file, special_alt, orig_insn, 1668 &new_insn); 1669 if (ret) 1670 goto out; 1671 } else if (special_alt->jump_or_nop) { 1672 ret = handle_jump_alt(file, special_alt, orig_insn, 1673 &new_insn); 1674 if (ret) 1675 goto out; 1676 } 1677 1678 alt = malloc(sizeof(*alt)); 1679 if (!alt) { 1680 WARN("malloc failed"); 1681 ret = -1; 1682 goto out; 1683 } 1684 1685 alt->insn = new_insn; 1686 alt->skip_orig = special_alt->skip_orig; 1687 orig_insn->ignore_alts |= special_alt->skip_alt; 1688 list_add_tail(&alt->list, &orig_insn->alts); 1689 1690 list_del(&special_alt->list); 1691 free(special_alt); 1692 } 1693 1694 if (stats) { 1695 printf("jl\\\tNOP\tJMP\n"); 1696 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short); 1697 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long); 1698 } 1699 1700 out: 1701 return ret; 1702 } 1703 1704 static int add_jump_table(struct objtool_file *file, struct instruction *insn, 1705 struct reloc *table) 1706 { 1707 struct reloc *reloc = table; 1708 struct instruction *dest_insn; 1709 struct alternative *alt; 1710 struct symbol *pfunc = insn->func->pfunc; 1711 unsigned int prev_offset = 0; 1712 1713 /* 1714 * Each @reloc is a switch table relocation which points to the target 1715 * instruction. 1716 */ 1717 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { 1718 1719 /* Check for the end of the table: */ 1720 if (reloc != table && reloc->jump_table_start) 1721 break; 1722 1723 /* Make sure the table entries are consecutive: */ 1724 if (prev_offset && reloc->offset != prev_offset + 8) 1725 break; 1726 1727 /* Detect function pointers from contiguous objects: */ 1728 if (reloc->sym->sec == pfunc->sec && 1729 reloc->addend == pfunc->offset) 1730 break; 1731 1732 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); 1733 if (!dest_insn) 1734 break; 1735 1736 /* Make sure the destination is in the same function: */ 1737 if (!dest_insn->func || dest_insn->func->pfunc != pfunc) 1738 break; 1739 1740 alt = malloc(sizeof(*alt)); 1741 if (!alt) { 1742 WARN("malloc failed"); 1743 return -1; 1744 } 1745 1746 alt->insn = dest_insn; 1747 list_add_tail(&alt->list, &insn->alts); 1748 prev_offset = reloc->offset; 1749 } 1750 1751 if (!prev_offset) { 1752 WARN_FUNC("can't find switch jump table", 1753 insn->sec, insn->offset); 1754 return -1; 1755 } 1756 1757 return 0; 1758 } 1759 1760 /* 1761 * find_jump_table() - Given a dynamic jump, find the switch jump table 1762 * associated with it. 1763 */ 1764 static struct reloc *find_jump_table(struct objtool_file *file, 1765 struct symbol *func, 1766 struct instruction *insn) 1767 { 1768 struct reloc *table_reloc; 1769 struct instruction *dest_insn, *orig_insn = insn; 1770 1771 /* 1772 * Backward search using the @first_jump_src links, these help avoid 1773 * much of the 'in between' code. Which avoids us getting confused by 1774 * it. 1775 */ 1776 for (; 1777 insn && insn->func && insn->func->pfunc == func; 1778 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { 1779 1780 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) 1781 break; 1782 1783 /* allow small jumps within the range */ 1784 if (insn->type == INSN_JUMP_UNCONDITIONAL && 1785 insn->jump_dest && 1786 (insn->jump_dest->offset <= insn->offset || 1787 insn->jump_dest->offset > orig_insn->offset)) 1788 break; 1789 1790 table_reloc = arch_find_switch_table(file, insn); 1791 if (!table_reloc) 1792 continue; 1793 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); 1794 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func) 1795 continue; 1796 1797 return table_reloc; 1798 } 1799 1800 return NULL; 1801 } 1802 1803 /* 1804 * First pass: Mark the head of each jump table so that in the next pass, 1805 * we know when a given jump table ends and the next one starts. 1806 */ 1807 static void mark_func_jump_tables(struct objtool_file *file, 1808 struct symbol *func) 1809 { 1810 struct instruction *insn, *last = NULL; 1811 struct reloc *reloc; 1812 1813 func_for_each_insn(file, func, insn) { 1814 if (!last) 1815 last = insn; 1816 1817 /* 1818 * Store back-pointers for unconditional forward jumps such 1819 * that find_jump_table() can back-track using those and 1820 * avoid some potentially confusing code. 1821 */ 1822 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && 1823 insn->offset > last->offset && 1824 insn->jump_dest->offset > insn->offset && 1825 !insn->jump_dest->first_jump_src) { 1826 1827 insn->jump_dest->first_jump_src = insn; 1828 last = insn->jump_dest; 1829 } 1830 1831 if (insn->type != INSN_JUMP_DYNAMIC) 1832 continue; 1833 1834 reloc = find_jump_table(file, func, insn); 1835 if (reloc) { 1836 reloc->jump_table_start = true; 1837 insn->jump_table = reloc; 1838 } 1839 } 1840 } 1841 1842 static int add_func_jump_tables(struct objtool_file *file, 1843 struct symbol *func) 1844 { 1845 struct instruction *insn; 1846 int ret; 1847 1848 func_for_each_insn(file, func, insn) { 1849 if (!insn->jump_table) 1850 continue; 1851 1852 ret = add_jump_table(file, insn, insn->jump_table); 1853 if (ret) 1854 return ret; 1855 } 1856 1857 return 0; 1858 } 1859 1860 /* 1861 * For some switch statements, gcc generates a jump table in the .rodata 1862 * section which contains a list of addresses within the function to jump to. 1863 * This finds these jump tables and adds them to the insn->alts lists. 1864 */ 1865 static int add_jump_table_alts(struct objtool_file *file) 1866 { 1867 struct section *sec; 1868 struct symbol *func; 1869 int ret; 1870 1871 if (!file->rodata) 1872 return 0; 1873 1874 for_each_sec(file, sec) { 1875 list_for_each_entry(func, &sec->symbol_list, list) { 1876 if (func->type != STT_FUNC) 1877 continue; 1878 1879 mark_func_jump_tables(file, func); 1880 ret = add_func_jump_tables(file, func); 1881 if (ret) 1882 return ret; 1883 } 1884 } 1885 1886 return 0; 1887 } 1888 1889 static void set_func_state(struct cfi_state *state) 1890 { 1891 state->cfa = initial_func_cfi.cfa; 1892 memcpy(&state->regs, &initial_func_cfi.regs, 1893 CFI_NUM_REGS * sizeof(struct cfi_reg)); 1894 state->stack_size = initial_func_cfi.cfa.offset; 1895 } 1896 1897 static int read_unwind_hints(struct objtool_file *file) 1898 { 1899 struct cfi_state cfi = init_cfi; 1900 struct section *sec, *relocsec; 1901 struct unwind_hint *hint; 1902 struct instruction *insn; 1903 struct reloc *reloc; 1904 int i; 1905 1906 sec = find_section_by_name(file->elf, ".discard.unwind_hints"); 1907 if (!sec) 1908 return 0; 1909 1910 relocsec = sec->reloc; 1911 if (!relocsec) { 1912 WARN("missing .rela.discard.unwind_hints section"); 1913 return -1; 1914 } 1915 1916 if (sec->sh.sh_size % sizeof(struct unwind_hint)) { 1917 WARN("struct unwind_hint size mismatch"); 1918 return -1; 1919 } 1920 1921 file->hints = true; 1922 1923 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) { 1924 hint = (struct unwind_hint *)sec->data->d_buf + i; 1925 1926 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); 1927 if (!reloc) { 1928 WARN("can't find reloc for unwind_hints[%d]", i); 1929 return -1; 1930 } 1931 1932 insn = find_insn(file, reloc->sym->sec, reloc->addend); 1933 if (!insn) { 1934 WARN("can't find insn for unwind_hints[%d]", i); 1935 return -1; 1936 } 1937 1938 insn->hint = true; 1939 1940 if (ibt && hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) { 1941 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset); 1942 1943 if (sym && sym->bind == STB_GLOBAL && 1944 insn->type != INSN_ENDBR && !insn->noendbr) { 1945 WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR", 1946 insn->sec, insn->offset); 1947 } 1948 } 1949 1950 if (hint->type == UNWIND_HINT_TYPE_FUNC) { 1951 insn->cfi = &func_cfi; 1952 continue; 1953 } 1954 1955 if (insn->cfi) 1956 cfi = *(insn->cfi); 1957 1958 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) { 1959 WARN_FUNC("unsupported unwind_hint sp base reg %d", 1960 insn->sec, insn->offset, hint->sp_reg); 1961 return -1; 1962 } 1963 1964 cfi.cfa.offset = bswap_if_needed(hint->sp_offset); 1965 cfi.type = hint->type; 1966 cfi.end = hint->end; 1967 1968 insn->cfi = cfi_hash_find_or_add(&cfi); 1969 } 1970 1971 return 0; 1972 } 1973 1974 static int read_noendbr_hints(struct objtool_file *file) 1975 { 1976 struct section *sec; 1977 struct instruction *insn; 1978 struct reloc *reloc; 1979 1980 sec = find_section_by_name(file->elf, ".rela.discard.noendbr"); 1981 if (!sec) 1982 return 0; 1983 1984 list_for_each_entry(reloc, &sec->reloc_list, list) { 1985 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend); 1986 if (!insn) { 1987 WARN("bad .discard.noendbr entry"); 1988 return -1; 1989 } 1990 1991 if (insn->type == INSN_ENDBR) 1992 WARN_FUNC("ANNOTATE_NOENDBR on ENDBR", insn->sec, insn->offset); 1993 1994 insn->noendbr = 1; 1995 } 1996 1997 return 0; 1998 } 1999 2000 static int read_retpoline_hints(struct objtool_file *file) 2001 { 2002 struct section *sec; 2003 struct instruction *insn; 2004 struct reloc *reloc; 2005 2006 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); 2007 if (!sec) 2008 return 0; 2009 2010 list_for_each_entry(reloc, &sec->reloc_list, list) { 2011 if (reloc->sym->type != STT_SECTION) { 2012 WARN("unexpected relocation symbol type in %s", sec->name); 2013 return -1; 2014 } 2015 2016 insn = find_insn(file, reloc->sym->sec, reloc->addend); 2017 if (!insn) { 2018 WARN("bad .discard.retpoline_safe entry"); 2019 return -1; 2020 } 2021 2022 if (insn->type != INSN_JUMP_DYNAMIC && 2023 insn->type != INSN_CALL_DYNAMIC) { 2024 WARN_FUNC("retpoline_safe hint not an indirect jump/call", 2025 insn->sec, insn->offset); 2026 return -1; 2027 } 2028 2029 insn->retpoline_safe = true; 2030 } 2031 2032 return 0; 2033 } 2034 2035 static int read_instr_hints(struct objtool_file *file) 2036 { 2037 struct section *sec; 2038 struct instruction *insn; 2039 struct reloc *reloc; 2040 2041 sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); 2042 if (!sec) 2043 return 0; 2044 2045 list_for_each_entry(reloc, &sec->reloc_list, list) { 2046 if (reloc->sym->type != STT_SECTION) { 2047 WARN("unexpected relocation symbol type in %s", sec->name); 2048 return -1; 2049 } 2050 2051 insn = find_insn(file, reloc->sym->sec, reloc->addend); 2052 if (!insn) { 2053 WARN("bad .discard.instr_end entry"); 2054 return -1; 2055 } 2056 2057 insn->instr--; 2058 } 2059 2060 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); 2061 if (!sec) 2062 return 0; 2063 2064 list_for_each_entry(reloc, &sec->reloc_list, list) { 2065 if (reloc->sym->type != STT_SECTION) { 2066 WARN("unexpected relocation symbol type in %s", sec->name); 2067 return -1; 2068 } 2069 2070 insn = find_insn(file, reloc->sym->sec, reloc->addend); 2071 if (!insn) { 2072 WARN("bad .discard.instr_begin entry"); 2073 return -1; 2074 } 2075 2076 insn->instr++; 2077 } 2078 2079 return 0; 2080 } 2081 2082 static int read_intra_function_calls(struct objtool_file *file) 2083 { 2084 struct instruction *insn; 2085 struct section *sec; 2086 struct reloc *reloc; 2087 2088 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); 2089 if (!sec) 2090 return 0; 2091 2092 list_for_each_entry(reloc, &sec->reloc_list, list) { 2093 unsigned long dest_off; 2094 2095 if (reloc->sym->type != STT_SECTION) { 2096 WARN("unexpected relocation symbol type in %s", 2097 sec->name); 2098 return -1; 2099 } 2100 2101 insn = find_insn(file, reloc->sym->sec, reloc->addend); 2102 if (!insn) { 2103 WARN("bad .discard.intra_function_call entry"); 2104 return -1; 2105 } 2106 2107 if (insn->type != INSN_CALL) { 2108 WARN_FUNC("intra_function_call not a direct call", 2109 insn->sec, insn->offset); 2110 return -1; 2111 } 2112 2113 /* 2114 * Treat intra-function CALLs as JMPs, but with a stack_op. 2115 * See add_call_destinations(), which strips stack_ops from 2116 * normal CALLs. 2117 */ 2118 insn->type = INSN_JUMP_UNCONDITIONAL; 2119 2120 dest_off = insn->offset + insn->len + insn->immediate; 2121 insn->jump_dest = find_insn(file, insn->sec, dest_off); 2122 if (!insn->jump_dest) { 2123 WARN_FUNC("can't find call dest at %s+0x%lx", 2124 insn->sec, insn->offset, 2125 insn->sec->name, dest_off); 2126 return -1; 2127 } 2128 } 2129 2130 return 0; 2131 } 2132 2133 /* 2134 * Return true if name matches an instrumentation function, where calls to that 2135 * function from noinstr code can safely be removed, but compilers won't do so. 2136 */ 2137 static bool is_profiling_func(const char *name) 2138 { 2139 /* 2140 * Many compilers cannot disable KCOV with a function attribute. 2141 */ 2142 if (!strncmp(name, "__sanitizer_cov_", 16)) 2143 return true; 2144 2145 /* 2146 * Some compilers currently do not remove __tsan_func_entry/exit nor 2147 * __tsan_atomic_signal_fence (used for barrier instrumentation) with 2148 * the __no_sanitize_thread attribute, remove them. Once the kernel's 2149 * minimum Clang version is 14.0, this can be removed. 2150 */ 2151 if (!strncmp(name, "__tsan_func_", 12) || 2152 !strcmp(name, "__tsan_atomic_signal_fence")) 2153 return true; 2154 2155 return false; 2156 } 2157 2158 static int classify_symbols(struct objtool_file *file) 2159 { 2160 struct section *sec; 2161 struct symbol *func; 2162 2163 for_each_sec(file, sec) { 2164 list_for_each_entry(func, &sec->symbol_list, list) { 2165 if (func->bind != STB_GLOBAL) 2166 continue; 2167 2168 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR, 2169 strlen(STATIC_CALL_TRAMP_PREFIX_STR))) 2170 func->static_call_tramp = true; 2171 2172 if (arch_is_retpoline(func)) 2173 func->retpoline_thunk = true; 2174 2175 if (!strcmp(func->name, "__fentry__")) 2176 func->fentry = true; 2177 2178 if (is_profiling_func(func->name)) 2179 func->profiling_func = true; 2180 } 2181 } 2182 2183 return 0; 2184 } 2185 2186 static void mark_rodata(struct objtool_file *file) 2187 { 2188 struct section *sec; 2189 bool found = false; 2190 2191 /* 2192 * Search for the following rodata sections, each of which can 2193 * potentially contain jump tables: 2194 * 2195 * - .rodata: can contain GCC switch tables 2196 * - .rodata.<func>: same, if -fdata-sections is being used 2197 * - .rodata..c_jump_table: contains C annotated jump tables 2198 * 2199 * .rodata.str1.* sections are ignored; they don't contain jump tables. 2200 */ 2201 for_each_sec(file, sec) { 2202 if (!strncmp(sec->name, ".rodata", 7) && 2203 !strstr(sec->name, ".str1.")) { 2204 sec->rodata = true; 2205 found = true; 2206 } 2207 } 2208 2209 file->rodata = found; 2210 } 2211 2212 static int decode_sections(struct objtool_file *file) 2213 { 2214 int ret; 2215 2216 mark_rodata(file); 2217 2218 ret = init_pv_ops(file); 2219 if (ret) 2220 return ret; 2221 2222 ret = decode_instructions(file); 2223 if (ret) 2224 return ret; 2225 2226 add_ignores(file); 2227 add_uaccess_safe(file); 2228 2229 ret = add_ignore_alternatives(file); 2230 if (ret) 2231 return ret; 2232 2233 /* 2234 * Must be before read_unwind_hints() since that needs insn->noendbr. 2235 */ 2236 ret = read_noendbr_hints(file); 2237 if (ret) 2238 return ret; 2239 2240 /* 2241 * Must be before add_{jump_call}_destination. 2242 */ 2243 ret = classify_symbols(file); 2244 if (ret) 2245 return ret; 2246 2247 /* 2248 * Must be before add_special_section_alts() as that depends on 2249 * jump_dest being set. 2250 */ 2251 ret = add_jump_destinations(file); 2252 if (ret) 2253 return ret; 2254 2255 ret = add_special_section_alts(file); 2256 if (ret) 2257 return ret; 2258 2259 /* 2260 * Must be before add_call_destination(); it changes INSN_CALL to 2261 * INSN_JUMP. 2262 */ 2263 ret = read_intra_function_calls(file); 2264 if (ret) 2265 return ret; 2266 2267 ret = add_call_destinations(file); 2268 if (ret) 2269 return ret; 2270 2271 /* 2272 * Must be after add_call_destinations() such that it can override 2273 * dead_end_function() marks. 2274 */ 2275 ret = add_dead_ends(file); 2276 if (ret) 2277 return ret; 2278 2279 ret = add_jump_table_alts(file); 2280 if (ret) 2281 return ret; 2282 2283 ret = read_unwind_hints(file); 2284 if (ret) 2285 return ret; 2286 2287 ret = read_retpoline_hints(file); 2288 if (ret) 2289 return ret; 2290 2291 ret = read_instr_hints(file); 2292 if (ret) 2293 return ret; 2294 2295 return 0; 2296 } 2297 2298 static bool is_fentry_call(struct instruction *insn) 2299 { 2300 if (insn->type == INSN_CALL && 2301 insn->call_dest && 2302 insn->call_dest->fentry) 2303 return true; 2304 2305 return false; 2306 } 2307 2308 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) 2309 { 2310 struct cfi_state *cfi = &state->cfi; 2311 int i; 2312 2313 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) 2314 return true; 2315 2316 if (cfi->cfa.offset != initial_func_cfi.cfa.offset) 2317 return true; 2318 2319 if (cfi->stack_size != initial_func_cfi.cfa.offset) 2320 return true; 2321 2322 for (i = 0; i < CFI_NUM_REGS; i++) { 2323 if (cfi->regs[i].base != initial_func_cfi.regs[i].base || 2324 cfi->regs[i].offset != initial_func_cfi.regs[i].offset) 2325 return true; 2326 } 2327 2328 return false; 2329 } 2330 2331 static bool check_reg_frame_pos(const struct cfi_reg *reg, 2332 int expected_offset) 2333 { 2334 return reg->base == CFI_CFA && 2335 reg->offset == expected_offset; 2336 } 2337 2338 static bool has_valid_stack_frame(struct insn_state *state) 2339 { 2340 struct cfi_state *cfi = &state->cfi; 2341 2342 if (cfi->cfa.base == CFI_BP && 2343 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) && 2344 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8)) 2345 return true; 2346 2347 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) 2348 return true; 2349 2350 return false; 2351 } 2352 2353 static int update_cfi_state_regs(struct instruction *insn, 2354 struct cfi_state *cfi, 2355 struct stack_op *op) 2356 { 2357 struct cfi_reg *cfa = &cfi->cfa; 2358 2359 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) 2360 return 0; 2361 2362 /* push */ 2363 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) 2364 cfa->offset += 8; 2365 2366 /* pop */ 2367 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) 2368 cfa->offset -= 8; 2369 2370 /* add immediate to sp */ 2371 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && 2372 op->dest.reg == CFI_SP && op->src.reg == CFI_SP) 2373 cfa->offset -= op->src.offset; 2374 2375 return 0; 2376 } 2377 2378 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) 2379 { 2380 if (arch_callee_saved_reg(reg) && 2381 cfi->regs[reg].base == CFI_UNDEFINED) { 2382 cfi->regs[reg].base = base; 2383 cfi->regs[reg].offset = offset; 2384 } 2385 } 2386 2387 static void restore_reg(struct cfi_state *cfi, unsigned char reg) 2388 { 2389 cfi->regs[reg].base = initial_func_cfi.regs[reg].base; 2390 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; 2391 } 2392 2393 /* 2394 * A note about DRAP stack alignment: 2395 * 2396 * GCC has the concept of a DRAP register, which is used to help keep track of 2397 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP 2398 * register. The typical DRAP pattern is: 2399 * 2400 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 2401 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp 2402 * 41 ff 72 f8 pushq -0x8(%r10) 2403 * 55 push %rbp 2404 * 48 89 e5 mov %rsp,%rbp 2405 * (more pushes) 2406 * 41 52 push %r10 2407 * ... 2408 * 41 5a pop %r10 2409 * (more pops) 2410 * 5d pop %rbp 2411 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2412 * c3 retq 2413 * 2414 * There are some variations in the epilogues, like: 2415 * 2416 * 5b pop %rbx 2417 * 41 5a pop %r10 2418 * 41 5c pop %r12 2419 * 41 5d pop %r13 2420 * 41 5e pop %r14 2421 * c9 leaveq 2422 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2423 * c3 retq 2424 * 2425 * and: 2426 * 2427 * 4c 8b 55 e8 mov -0x18(%rbp),%r10 2428 * 48 8b 5d e0 mov -0x20(%rbp),%rbx 2429 * 4c 8b 65 f0 mov -0x10(%rbp),%r12 2430 * 4c 8b 6d f8 mov -0x8(%rbp),%r13 2431 * c9 leaveq 2432 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2433 * c3 retq 2434 * 2435 * Sometimes r13 is used as the DRAP register, in which case it's saved and 2436 * restored beforehand: 2437 * 2438 * 41 55 push %r13 2439 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 2440 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 2441 * ... 2442 * 49 8d 65 f0 lea -0x10(%r13),%rsp 2443 * 41 5d pop %r13 2444 * c3 retq 2445 */ 2446 static int update_cfi_state(struct instruction *insn, 2447 struct instruction *next_insn, 2448 struct cfi_state *cfi, struct stack_op *op) 2449 { 2450 struct cfi_reg *cfa = &cfi->cfa; 2451 struct cfi_reg *regs = cfi->regs; 2452 2453 /* stack operations don't make sense with an undefined CFA */ 2454 if (cfa->base == CFI_UNDEFINED) { 2455 if (insn->func) { 2456 WARN_FUNC("undefined stack state", insn->sec, insn->offset); 2457 return -1; 2458 } 2459 return 0; 2460 } 2461 2462 if (cfi->type == UNWIND_HINT_TYPE_REGS || 2463 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL) 2464 return update_cfi_state_regs(insn, cfi, op); 2465 2466 switch (op->dest.type) { 2467 2468 case OP_DEST_REG: 2469 switch (op->src.type) { 2470 2471 case OP_SRC_REG: 2472 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && 2473 cfa->base == CFI_SP && 2474 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) { 2475 2476 /* mov %rsp, %rbp */ 2477 cfa->base = op->dest.reg; 2478 cfi->bp_scratch = false; 2479 } 2480 2481 else if (op->src.reg == CFI_SP && 2482 op->dest.reg == CFI_BP && cfi->drap) { 2483 2484 /* drap: mov %rsp, %rbp */ 2485 regs[CFI_BP].base = CFI_BP; 2486 regs[CFI_BP].offset = -cfi->stack_size; 2487 cfi->bp_scratch = false; 2488 } 2489 2490 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { 2491 2492 /* 2493 * mov %rsp, %reg 2494 * 2495 * This is needed for the rare case where GCC 2496 * does: 2497 * 2498 * mov %rsp, %rax 2499 * ... 2500 * mov %rax, %rsp 2501 */ 2502 cfi->vals[op->dest.reg].base = CFI_CFA; 2503 cfi->vals[op->dest.reg].offset = -cfi->stack_size; 2504 } 2505 2506 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && 2507 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) { 2508 2509 /* 2510 * mov %rbp, %rsp 2511 * 2512 * Restore the original stack pointer (Clang). 2513 */ 2514 cfi->stack_size = -cfi->regs[CFI_BP].offset; 2515 } 2516 2517 else if (op->dest.reg == cfa->base) { 2518 2519 /* mov %reg, %rsp */ 2520 if (cfa->base == CFI_SP && 2521 cfi->vals[op->src.reg].base == CFI_CFA) { 2522 2523 /* 2524 * This is needed for the rare case 2525 * where GCC does something dumb like: 2526 * 2527 * lea 0x8(%rsp), %rcx 2528 * ... 2529 * mov %rcx, %rsp 2530 */ 2531 cfa->offset = -cfi->vals[op->src.reg].offset; 2532 cfi->stack_size = cfa->offset; 2533 2534 } else if (cfa->base == CFI_SP && 2535 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && 2536 cfi->vals[op->src.reg].offset == cfa->offset) { 2537 2538 /* 2539 * Stack swizzle: 2540 * 2541 * 1: mov %rsp, (%[tos]) 2542 * 2: mov %[tos], %rsp 2543 * ... 2544 * 3: pop %rsp 2545 * 2546 * Where: 2547 * 2548 * 1 - places a pointer to the previous 2549 * stack at the Top-of-Stack of the 2550 * new stack. 2551 * 2552 * 2 - switches to the new stack. 2553 * 2554 * 3 - pops the Top-of-Stack to restore 2555 * the original stack. 2556 * 2557 * Note: we set base to SP_INDIRECT 2558 * here and preserve offset. Therefore 2559 * when the unwinder reaches ToS it 2560 * will dereference SP and then add the 2561 * offset to find the next frame, IOW: 2562 * (%rsp) + offset. 2563 */ 2564 cfa->base = CFI_SP_INDIRECT; 2565 2566 } else { 2567 cfa->base = CFI_UNDEFINED; 2568 cfa->offset = 0; 2569 } 2570 } 2571 2572 else if (op->dest.reg == CFI_SP && 2573 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && 2574 cfi->vals[op->src.reg].offset == cfa->offset) { 2575 2576 /* 2577 * The same stack swizzle case 2) as above. But 2578 * because we can't change cfa->base, case 3) 2579 * will become a regular POP. Pretend we're a 2580 * PUSH so things don't go unbalanced. 2581 */ 2582 cfi->stack_size += 8; 2583 } 2584 2585 2586 break; 2587 2588 case OP_SRC_ADD: 2589 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { 2590 2591 /* add imm, %rsp */ 2592 cfi->stack_size -= op->src.offset; 2593 if (cfa->base == CFI_SP) 2594 cfa->offset -= op->src.offset; 2595 break; 2596 } 2597 2598 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { 2599 2600 /* lea disp(%rbp), %rsp */ 2601 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); 2602 break; 2603 } 2604 2605 if (!cfi->drap && op->src.reg == CFI_SP && 2606 op->dest.reg == CFI_BP && cfa->base == CFI_SP && 2607 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) { 2608 2609 /* lea disp(%rsp), %rbp */ 2610 cfa->base = CFI_BP; 2611 cfa->offset -= op->src.offset; 2612 cfi->bp_scratch = false; 2613 break; 2614 } 2615 2616 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { 2617 2618 /* drap: lea disp(%rsp), %drap */ 2619 cfi->drap_reg = op->dest.reg; 2620 2621 /* 2622 * lea disp(%rsp), %reg 2623 * 2624 * This is needed for the rare case where GCC 2625 * does something dumb like: 2626 * 2627 * lea 0x8(%rsp), %rcx 2628 * ... 2629 * mov %rcx, %rsp 2630 */ 2631 cfi->vals[op->dest.reg].base = CFI_CFA; 2632 cfi->vals[op->dest.reg].offset = \ 2633 -cfi->stack_size + op->src.offset; 2634 2635 break; 2636 } 2637 2638 if (cfi->drap && op->dest.reg == CFI_SP && 2639 op->src.reg == cfi->drap_reg) { 2640 2641 /* drap: lea disp(%drap), %rsp */ 2642 cfa->base = CFI_SP; 2643 cfa->offset = cfi->stack_size = -op->src.offset; 2644 cfi->drap_reg = CFI_UNDEFINED; 2645 cfi->drap = false; 2646 break; 2647 } 2648 2649 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) { 2650 WARN_FUNC("unsupported stack register modification", 2651 insn->sec, insn->offset); 2652 return -1; 2653 } 2654 2655 break; 2656 2657 case OP_SRC_AND: 2658 if (op->dest.reg != CFI_SP || 2659 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || 2660 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { 2661 WARN_FUNC("unsupported stack pointer realignment", 2662 insn->sec, insn->offset); 2663 return -1; 2664 } 2665 2666 if (cfi->drap_reg != CFI_UNDEFINED) { 2667 /* drap: and imm, %rsp */ 2668 cfa->base = cfi->drap_reg; 2669 cfa->offset = cfi->stack_size = 0; 2670 cfi->drap = true; 2671 } 2672 2673 /* 2674 * Older versions of GCC (4.8ish) realign the stack 2675 * without DRAP, with a frame pointer. 2676 */ 2677 2678 break; 2679 2680 case OP_SRC_POP: 2681 case OP_SRC_POPF: 2682 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) { 2683 2684 /* pop %rsp; # restore from a stack swizzle */ 2685 cfa->base = CFI_SP; 2686 break; 2687 } 2688 2689 if (!cfi->drap && op->dest.reg == cfa->base) { 2690 2691 /* pop %rbp */ 2692 cfa->base = CFI_SP; 2693 } 2694 2695 if (cfi->drap && cfa->base == CFI_BP_INDIRECT && 2696 op->dest.reg == cfi->drap_reg && 2697 cfi->drap_offset == -cfi->stack_size) { 2698 2699 /* drap: pop %drap */ 2700 cfa->base = cfi->drap_reg; 2701 cfa->offset = 0; 2702 cfi->drap_offset = -1; 2703 2704 } else if (cfi->stack_size == -regs[op->dest.reg].offset) { 2705 2706 /* pop %reg */ 2707 restore_reg(cfi, op->dest.reg); 2708 } 2709 2710 cfi->stack_size -= 8; 2711 if (cfa->base == CFI_SP) 2712 cfa->offset -= 8; 2713 2714 break; 2715 2716 case OP_SRC_REG_INDIRECT: 2717 if (!cfi->drap && op->dest.reg == cfa->base && 2718 op->dest.reg == CFI_BP) { 2719 2720 /* mov disp(%rsp), %rbp */ 2721 cfa->base = CFI_SP; 2722 cfa->offset = cfi->stack_size; 2723 } 2724 2725 if (cfi->drap && op->src.reg == CFI_BP && 2726 op->src.offset == cfi->drap_offset) { 2727 2728 /* drap: mov disp(%rbp), %drap */ 2729 cfa->base = cfi->drap_reg; 2730 cfa->offset = 0; 2731 cfi->drap_offset = -1; 2732 } 2733 2734 if (cfi->drap && op->src.reg == CFI_BP && 2735 op->src.offset == regs[op->dest.reg].offset) { 2736 2737 /* drap: mov disp(%rbp), %reg */ 2738 restore_reg(cfi, op->dest.reg); 2739 2740 } else if (op->src.reg == cfa->base && 2741 op->src.offset == regs[op->dest.reg].offset + cfa->offset) { 2742 2743 /* mov disp(%rbp), %reg */ 2744 /* mov disp(%rsp), %reg */ 2745 restore_reg(cfi, op->dest.reg); 2746 2747 } else if (op->src.reg == CFI_SP && 2748 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { 2749 2750 /* mov disp(%rsp), %reg */ 2751 restore_reg(cfi, op->dest.reg); 2752 } 2753 2754 break; 2755 2756 default: 2757 WARN_FUNC("unknown stack-related instruction", 2758 insn->sec, insn->offset); 2759 return -1; 2760 } 2761 2762 break; 2763 2764 case OP_DEST_PUSH: 2765 case OP_DEST_PUSHF: 2766 cfi->stack_size += 8; 2767 if (cfa->base == CFI_SP) 2768 cfa->offset += 8; 2769 2770 if (op->src.type != OP_SRC_REG) 2771 break; 2772 2773 if (cfi->drap) { 2774 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { 2775 2776 /* drap: push %drap */ 2777 cfa->base = CFI_BP_INDIRECT; 2778 cfa->offset = -cfi->stack_size; 2779 2780 /* save drap so we know when to restore it */ 2781 cfi->drap_offset = -cfi->stack_size; 2782 2783 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { 2784 2785 /* drap: push %rbp */ 2786 cfi->stack_size = 0; 2787 2788 } else { 2789 2790 /* drap: push %reg */ 2791 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); 2792 } 2793 2794 } else { 2795 2796 /* push %reg */ 2797 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); 2798 } 2799 2800 /* detect when asm code uses rbp as a scratch register */ 2801 if (!no_fp && insn->func && op->src.reg == CFI_BP && 2802 cfa->base != CFI_BP) 2803 cfi->bp_scratch = true; 2804 break; 2805 2806 case OP_DEST_REG_INDIRECT: 2807 2808 if (cfi->drap) { 2809 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { 2810 2811 /* drap: mov %drap, disp(%rbp) */ 2812 cfa->base = CFI_BP_INDIRECT; 2813 cfa->offset = op->dest.offset; 2814 2815 /* save drap offset so we know when to restore it */ 2816 cfi->drap_offset = op->dest.offset; 2817 } else { 2818 2819 /* drap: mov reg, disp(%rbp) */ 2820 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); 2821 } 2822 2823 } else if (op->dest.reg == cfa->base) { 2824 2825 /* mov reg, disp(%rbp) */ 2826 /* mov reg, disp(%rsp) */ 2827 save_reg(cfi, op->src.reg, CFI_CFA, 2828 op->dest.offset - cfi->cfa.offset); 2829 2830 } else if (op->dest.reg == CFI_SP) { 2831 2832 /* mov reg, disp(%rsp) */ 2833 save_reg(cfi, op->src.reg, CFI_CFA, 2834 op->dest.offset - cfi->stack_size); 2835 2836 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) { 2837 2838 /* mov %rsp, (%reg); # setup a stack swizzle. */ 2839 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT; 2840 cfi->vals[op->dest.reg].offset = cfa->offset; 2841 } 2842 2843 break; 2844 2845 case OP_DEST_MEM: 2846 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { 2847 WARN_FUNC("unknown stack-related memory operation", 2848 insn->sec, insn->offset); 2849 return -1; 2850 } 2851 2852 /* pop mem */ 2853 cfi->stack_size -= 8; 2854 if (cfa->base == CFI_SP) 2855 cfa->offset -= 8; 2856 2857 break; 2858 2859 default: 2860 WARN_FUNC("unknown stack-related instruction", 2861 insn->sec, insn->offset); 2862 return -1; 2863 } 2864 2865 return 0; 2866 } 2867 2868 /* 2869 * The stack layouts of alternatives instructions can sometimes diverge when 2870 * they have stack modifications. That's fine as long as the potential stack 2871 * layouts don't conflict at any given potential instruction boundary. 2872 * 2873 * Flatten the CFIs of the different alternative code streams (both original 2874 * and replacement) into a single shared CFI array which can be used to detect 2875 * conflicts and nicely feed a linear array of ORC entries to the unwinder. 2876 */ 2877 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn) 2878 { 2879 struct cfi_state **alt_cfi; 2880 int group_off; 2881 2882 if (!insn->alt_group) 2883 return 0; 2884 2885 if (!insn->cfi) { 2886 WARN("CFI missing"); 2887 return -1; 2888 } 2889 2890 alt_cfi = insn->alt_group->cfi; 2891 group_off = insn->offset - insn->alt_group->first_insn->offset; 2892 2893 if (!alt_cfi[group_off]) { 2894 alt_cfi[group_off] = insn->cfi; 2895 } else { 2896 if (cficmp(alt_cfi[group_off], insn->cfi)) { 2897 WARN_FUNC("stack layout conflict in alternatives", 2898 insn->sec, insn->offset); 2899 return -1; 2900 } 2901 } 2902 2903 return 0; 2904 } 2905 2906 static int handle_insn_ops(struct instruction *insn, 2907 struct instruction *next_insn, 2908 struct insn_state *state) 2909 { 2910 struct stack_op *op; 2911 2912 list_for_each_entry(op, &insn->stack_ops, list) { 2913 2914 if (update_cfi_state(insn, next_insn, &state->cfi, op)) 2915 return 1; 2916 2917 if (!insn->alt_group) 2918 continue; 2919 2920 if (op->dest.type == OP_DEST_PUSHF) { 2921 if (!state->uaccess_stack) { 2922 state->uaccess_stack = 1; 2923 } else if (state->uaccess_stack >> 31) { 2924 WARN_FUNC("PUSHF stack exhausted", 2925 insn->sec, insn->offset); 2926 return 1; 2927 } 2928 state->uaccess_stack <<= 1; 2929 state->uaccess_stack |= state->uaccess; 2930 } 2931 2932 if (op->src.type == OP_SRC_POPF) { 2933 if (state->uaccess_stack) { 2934 state->uaccess = state->uaccess_stack & 1; 2935 state->uaccess_stack >>= 1; 2936 if (state->uaccess_stack == 1) 2937 state->uaccess_stack = 0; 2938 } 2939 } 2940 } 2941 2942 return 0; 2943 } 2944 2945 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) 2946 { 2947 struct cfi_state *cfi1 = insn->cfi; 2948 int i; 2949 2950 if (!cfi1) { 2951 WARN("CFI missing"); 2952 return false; 2953 } 2954 2955 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { 2956 2957 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", 2958 insn->sec, insn->offset, 2959 cfi1->cfa.base, cfi1->cfa.offset, 2960 cfi2->cfa.base, cfi2->cfa.offset); 2961 2962 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { 2963 for (i = 0; i < CFI_NUM_REGS; i++) { 2964 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], 2965 sizeof(struct cfi_reg))) 2966 continue; 2967 2968 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", 2969 insn->sec, insn->offset, 2970 i, cfi1->regs[i].base, cfi1->regs[i].offset, 2971 i, cfi2->regs[i].base, cfi2->regs[i].offset); 2972 break; 2973 } 2974 2975 } else if (cfi1->type != cfi2->type) { 2976 2977 WARN_FUNC("stack state mismatch: type1=%d type2=%d", 2978 insn->sec, insn->offset, cfi1->type, cfi2->type); 2979 2980 } else if (cfi1->drap != cfi2->drap || 2981 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || 2982 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { 2983 2984 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", 2985 insn->sec, insn->offset, 2986 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, 2987 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); 2988 2989 } else 2990 return true; 2991 2992 return false; 2993 } 2994 2995 static inline bool func_uaccess_safe(struct symbol *func) 2996 { 2997 if (func) 2998 return func->uaccess_safe; 2999 3000 return false; 3001 } 3002 3003 static inline const char *call_dest_name(struct instruction *insn) 3004 { 3005 static char pvname[19]; 3006 struct reloc *rel; 3007 int idx; 3008 3009 if (insn->call_dest) 3010 return insn->call_dest->name; 3011 3012 rel = insn_reloc(NULL, insn); 3013 if (rel && !strcmp(rel->sym->name, "pv_ops")) { 3014 idx = (rel->addend / sizeof(void *)); 3015 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx); 3016 return pvname; 3017 } 3018 3019 return "{dynamic}"; 3020 } 3021 3022 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) 3023 { 3024 struct symbol *target; 3025 struct reloc *rel; 3026 int idx; 3027 3028 rel = insn_reloc(file, insn); 3029 if (!rel || strcmp(rel->sym->name, "pv_ops")) 3030 return false; 3031 3032 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *)); 3033 3034 if (file->pv_ops[idx].clean) 3035 return true; 3036 3037 file->pv_ops[idx].clean = true; 3038 3039 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) { 3040 if (!target->sec->noinstr) { 3041 WARN("pv_ops[%d]: %s", idx, target->name); 3042 file->pv_ops[idx].clean = false; 3043 } 3044 } 3045 3046 return file->pv_ops[idx].clean; 3047 } 3048 3049 static inline bool noinstr_call_dest(struct objtool_file *file, 3050 struct instruction *insn, 3051 struct symbol *func) 3052 { 3053 /* 3054 * We can't deal with indirect function calls at present; 3055 * assume they're instrumented. 3056 */ 3057 if (!func) { 3058 if (file->pv_ops) 3059 return pv_call_dest(file, insn); 3060 3061 return false; 3062 } 3063 3064 /* 3065 * If the symbol is from a noinstr section; we good. 3066 */ 3067 if (func->sec->noinstr) 3068 return true; 3069 3070 /* 3071 * The __ubsan_handle_*() calls are like WARN(), they only happen when 3072 * something 'BAD' happened. At the risk of taking the machine down, 3073 * let them proceed to get the message out. 3074 */ 3075 if (!strncmp(func->name, "__ubsan_handle_", 15)) 3076 return true; 3077 3078 return false; 3079 } 3080 3081 static int validate_call(struct objtool_file *file, 3082 struct instruction *insn, 3083 struct insn_state *state) 3084 { 3085 if (state->noinstr && state->instr <= 0 && 3086 !noinstr_call_dest(file, insn, insn->call_dest)) { 3087 WARN_FUNC("call to %s() leaves .noinstr.text section", 3088 insn->sec, insn->offset, call_dest_name(insn)); 3089 return 1; 3090 } 3091 3092 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) { 3093 WARN_FUNC("call to %s() with UACCESS enabled", 3094 insn->sec, insn->offset, call_dest_name(insn)); 3095 return 1; 3096 } 3097 3098 if (state->df) { 3099 WARN_FUNC("call to %s() with DF set", 3100 insn->sec, insn->offset, call_dest_name(insn)); 3101 return 1; 3102 } 3103 3104 return 0; 3105 } 3106 3107 static int validate_sibling_call(struct objtool_file *file, 3108 struct instruction *insn, 3109 struct insn_state *state) 3110 { 3111 if (has_modified_stack_frame(insn, state)) { 3112 WARN_FUNC("sibling call from callable instruction with modified stack frame", 3113 insn->sec, insn->offset); 3114 return 1; 3115 } 3116 3117 return validate_call(file, insn, state); 3118 } 3119 3120 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) 3121 { 3122 if (state->noinstr && state->instr > 0) { 3123 WARN_FUNC("return with instrumentation enabled", 3124 insn->sec, insn->offset); 3125 return 1; 3126 } 3127 3128 if (state->uaccess && !func_uaccess_safe(func)) { 3129 WARN_FUNC("return with UACCESS enabled", 3130 insn->sec, insn->offset); 3131 return 1; 3132 } 3133 3134 if (!state->uaccess && func_uaccess_safe(func)) { 3135 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", 3136 insn->sec, insn->offset); 3137 return 1; 3138 } 3139 3140 if (state->df) { 3141 WARN_FUNC("return with DF set", 3142 insn->sec, insn->offset); 3143 return 1; 3144 } 3145 3146 if (func && has_modified_stack_frame(insn, state)) { 3147 WARN_FUNC("return with modified stack frame", 3148 insn->sec, insn->offset); 3149 return 1; 3150 } 3151 3152 if (state->cfi.bp_scratch) { 3153 WARN_FUNC("BP used as a scratch register", 3154 insn->sec, insn->offset); 3155 return 1; 3156 } 3157 3158 return 0; 3159 } 3160 3161 static struct instruction *next_insn_to_validate(struct objtool_file *file, 3162 struct instruction *insn) 3163 { 3164 struct alt_group *alt_group = insn->alt_group; 3165 3166 /* 3167 * Simulate the fact that alternatives are patched in-place. When the 3168 * end of a replacement alt_group is reached, redirect objtool flow to 3169 * the end of the original alt_group. 3170 */ 3171 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group) 3172 return next_insn_same_sec(file, alt_group->orig_group->last_insn); 3173 3174 return next_insn_same_sec(file, insn); 3175 } 3176 3177 static struct instruction * 3178 validate_ibt_reloc(struct objtool_file *file, struct reloc *reloc) 3179 { 3180 struct instruction *dest; 3181 struct section *sec; 3182 unsigned long off; 3183 3184 sec = reloc->sym->sec; 3185 off = reloc->sym->offset; 3186 3187 if ((reloc->sec->base->sh.sh_flags & SHF_EXECINSTR) && 3188 (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)) 3189 off += arch_dest_reloc_offset(reloc->addend); 3190 else 3191 off += reloc->addend; 3192 3193 dest = find_insn(file, sec, off); 3194 if (!dest) 3195 return NULL; 3196 3197 if (dest->type == INSN_ENDBR) { 3198 if (!list_empty(&dest->call_node)) 3199 list_del_init(&dest->call_node); 3200 3201 return NULL; 3202 } 3203 3204 if (reloc->sym->static_call_tramp) 3205 return NULL; 3206 3207 return dest; 3208 } 3209 3210 static void warn_noendbr(const char *msg, struct section *sec, unsigned long offset, 3211 struct instruction *dest) 3212 { 3213 WARN_FUNC("%srelocation to !ENDBR: %s+0x%lx", sec, offset, msg, 3214 dest->func ? dest->func->name : dest->sec->name, 3215 dest->func ? dest->offset - dest->func->offset : dest->offset); 3216 } 3217 3218 static void validate_ibt_dest(struct objtool_file *file, struct instruction *insn, 3219 struct instruction *dest) 3220 { 3221 if (dest->func && dest->func == insn->func) { 3222 /* 3223 * Anything from->to self is either _THIS_IP_ or IRET-to-self. 3224 * 3225 * There is no sane way to annotate _THIS_IP_ since the compiler treats the 3226 * relocation as a constant and is happy to fold in offsets, skewing any 3227 * annotation we do, leading to vast amounts of false-positives. 3228 * 3229 * There's also compiler generated _THIS_IP_ through KCOV and 3230 * such which we have no hope of annotating. 3231 * 3232 * As such, blanket accept self-references without issue. 3233 */ 3234 return; 3235 } 3236 3237 if (dest->noendbr) 3238 return; 3239 3240 warn_noendbr("", insn->sec, insn->offset, dest); 3241 } 3242 3243 static void validate_ibt_insn(struct objtool_file *file, struct instruction *insn) 3244 { 3245 struct instruction *dest; 3246 struct reloc *reloc; 3247 3248 switch (insn->type) { 3249 case INSN_CALL: 3250 case INSN_CALL_DYNAMIC: 3251 case INSN_JUMP_CONDITIONAL: 3252 case INSN_JUMP_UNCONDITIONAL: 3253 case INSN_JUMP_DYNAMIC: 3254 case INSN_JUMP_DYNAMIC_CONDITIONAL: 3255 case INSN_RETURN: 3256 /* 3257 * We're looking for code references setting up indirect code 3258 * flow. As such, ignore direct code flow and the actual 3259 * dynamic branches. 3260 */ 3261 return; 3262 3263 case INSN_NOP: 3264 /* 3265 * handle_group_alt() will create INSN_NOP instruction that 3266 * don't belong to any section, ignore all NOP since they won't 3267 * carry a (useful) relocation anyway. 3268 */ 3269 return; 3270 3271 default: 3272 break; 3273 } 3274 3275 for (reloc = insn_reloc(file, insn); 3276 reloc; 3277 reloc = find_reloc_by_dest_range(file->elf, insn->sec, 3278 reloc->offset + 1, 3279 (insn->offset + insn->len) - (reloc->offset + 1))) { 3280 dest = validate_ibt_reloc(file, reloc); 3281 if (dest) 3282 validate_ibt_dest(file, insn, dest); 3283 } 3284 } 3285 3286 /* 3287 * Follow the branch starting at the given instruction, and recursively follow 3288 * any other branches (jumps). Meanwhile, track the frame pointer state at 3289 * each instruction and validate all the rules described in 3290 * tools/objtool/Documentation/stack-validation.txt. 3291 */ 3292 static int validate_branch(struct objtool_file *file, struct symbol *func, 3293 struct instruction *insn, struct insn_state state) 3294 { 3295 struct alternative *alt; 3296 struct instruction *next_insn, *prev_insn = NULL; 3297 struct section *sec; 3298 u8 visited; 3299 int ret; 3300 3301 sec = insn->sec; 3302 3303 while (1) { 3304 next_insn = next_insn_to_validate(file, insn); 3305 3306 if (file->c_file && func && insn->func && func != insn->func->pfunc) { 3307 WARN("%s() falls through to next function %s()", 3308 func->name, insn->func->name); 3309 return 1; 3310 } 3311 3312 if (func && insn->ignore) { 3313 WARN_FUNC("BUG: why am I validating an ignored function?", 3314 sec, insn->offset); 3315 return 1; 3316 } 3317 3318 visited = 1 << state.uaccess; 3319 if (insn->visited) { 3320 if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) 3321 return 1; 3322 3323 if (insn->visited & visited) 3324 return 0; 3325 } else { 3326 nr_insns_visited++; 3327 } 3328 3329 if (state.noinstr) 3330 state.instr += insn->instr; 3331 3332 if (insn->hint) { 3333 state.cfi = *insn->cfi; 3334 } else { 3335 /* XXX track if we actually changed state.cfi */ 3336 3337 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) { 3338 insn->cfi = prev_insn->cfi; 3339 nr_cfi_reused++; 3340 } else { 3341 insn->cfi = cfi_hash_find_or_add(&state.cfi); 3342 } 3343 } 3344 3345 insn->visited |= visited; 3346 3347 if (propagate_alt_cfi(file, insn)) 3348 return 1; 3349 3350 if (!insn->ignore_alts && !list_empty(&insn->alts)) { 3351 bool skip_orig = false; 3352 3353 list_for_each_entry(alt, &insn->alts, list) { 3354 if (alt->skip_orig) 3355 skip_orig = true; 3356 3357 ret = validate_branch(file, func, alt->insn, state); 3358 if (ret) { 3359 if (backtrace) 3360 BT_FUNC("(alt)", insn); 3361 return ret; 3362 } 3363 } 3364 3365 if (skip_orig) 3366 return 0; 3367 } 3368 3369 if (handle_insn_ops(insn, next_insn, &state)) 3370 return 1; 3371 3372 switch (insn->type) { 3373 3374 case INSN_RETURN: 3375 if (sls && !insn->retpoline_safe && 3376 next_insn && next_insn->type != INSN_TRAP) { 3377 WARN_FUNC("missing int3 after ret", 3378 insn->sec, insn->offset); 3379 } 3380 return validate_return(func, insn, &state); 3381 3382 case INSN_CALL: 3383 case INSN_CALL_DYNAMIC: 3384 ret = validate_call(file, insn, &state); 3385 if (ret) 3386 return ret; 3387 3388 if (!no_fp && func && !is_fentry_call(insn) && 3389 !has_valid_stack_frame(&state)) { 3390 WARN_FUNC("call without frame pointer save/setup", 3391 sec, insn->offset); 3392 return 1; 3393 } 3394 3395 if (insn->dead_end) 3396 return 0; 3397 3398 break; 3399 3400 case INSN_JUMP_CONDITIONAL: 3401 case INSN_JUMP_UNCONDITIONAL: 3402 if (is_sibling_call(insn)) { 3403 ret = validate_sibling_call(file, insn, &state); 3404 if (ret) 3405 return ret; 3406 3407 } else if (insn->jump_dest) { 3408 ret = validate_branch(file, func, 3409 insn->jump_dest, state); 3410 if (ret) { 3411 if (backtrace) 3412 BT_FUNC("(branch)", insn); 3413 return ret; 3414 } 3415 } 3416 3417 if (insn->type == INSN_JUMP_UNCONDITIONAL) 3418 return 0; 3419 3420 break; 3421 3422 case INSN_JUMP_DYNAMIC: 3423 if (sls && !insn->retpoline_safe && 3424 next_insn && next_insn->type != INSN_TRAP) { 3425 WARN_FUNC("missing int3 after indirect jump", 3426 insn->sec, insn->offset); 3427 } 3428 3429 /* fallthrough */ 3430 case INSN_JUMP_DYNAMIC_CONDITIONAL: 3431 if (is_sibling_call(insn)) { 3432 ret = validate_sibling_call(file, insn, &state); 3433 if (ret) 3434 return ret; 3435 } 3436 3437 if (insn->type == INSN_JUMP_DYNAMIC) 3438 return 0; 3439 3440 break; 3441 3442 case INSN_CONTEXT_SWITCH: 3443 if (func && (!next_insn || !next_insn->hint)) { 3444 WARN_FUNC("unsupported instruction in callable function", 3445 sec, insn->offset); 3446 return 1; 3447 } 3448 return 0; 3449 3450 case INSN_STAC: 3451 if (state.uaccess) { 3452 WARN_FUNC("recursive UACCESS enable", sec, insn->offset); 3453 return 1; 3454 } 3455 3456 state.uaccess = true; 3457 break; 3458 3459 case INSN_CLAC: 3460 if (!state.uaccess && func) { 3461 WARN_FUNC("redundant UACCESS disable", sec, insn->offset); 3462 return 1; 3463 } 3464 3465 if (func_uaccess_safe(func) && !state.uaccess_stack) { 3466 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset); 3467 return 1; 3468 } 3469 3470 state.uaccess = false; 3471 break; 3472 3473 case INSN_STD: 3474 if (state.df) { 3475 WARN_FUNC("recursive STD", sec, insn->offset); 3476 return 1; 3477 } 3478 3479 state.df = true; 3480 break; 3481 3482 case INSN_CLD: 3483 if (!state.df && func) { 3484 WARN_FUNC("redundant CLD", sec, insn->offset); 3485 return 1; 3486 } 3487 3488 state.df = false; 3489 break; 3490 3491 default: 3492 break; 3493 } 3494 3495 if (ibt) 3496 validate_ibt_insn(file, insn); 3497 3498 if (insn->dead_end) 3499 return 0; 3500 3501 if (!next_insn) { 3502 if (state.cfi.cfa.base == CFI_UNDEFINED) 3503 return 0; 3504 WARN("%s: unexpected end of section", sec->name); 3505 return 1; 3506 } 3507 3508 prev_insn = insn; 3509 insn = next_insn; 3510 } 3511 3512 return 0; 3513 } 3514 3515 static int validate_unwind_hints(struct objtool_file *file, struct section *sec) 3516 { 3517 struct instruction *insn; 3518 struct insn_state state; 3519 int ret, warnings = 0; 3520 3521 if (!file->hints) 3522 return 0; 3523 3524 init_insn_state(&state, sec); 3525 3526 if (sec) { 3527 insn = find_insn(file, sec, 0); 3528 if (!insn) 3529 return 0; 3530 } else { 3531 insn = list_first_entry(&file->insn_list, typeof(*insn), list); 3532 } 3533 3534 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) { 3535 if (insn->hint && !insn->visited && !insn->ignore) { 3536 ret = validate_branch(file, insn->func, insn, state); 3537 if (ret && backtrace) 3538 BT_FUNC("<=== (hint)", insn); 3539 warnings += ret; 3540 } 3541 3542 insn = list_next_entry(insn, list); 3543 } 3544 3545 return warnings; 3546 } 3547 3548 static int validate_retpoline(struct objtool_file *file) 3549 { 3550 struct instruction *insn; 3551 int warnings = 0; 3552 3553 for_each_insn(file, insn) { 3554 if (insn->type != INSN_JUMP_DYNAMIC && 3555 insn->type != INSN_CALL_DYNAMIC) 3556 continue; 3557 3558 if (insn->retpoline_safe) 3559 continue; 3560 3561 /* 3562 * .init.text code is ran before userspace and thus doesn't 3563 * strictly need retpolines, except for modules which are 3564 * loaded late, they very much do need retpoline in their 3565 * .init.text 3566 */ 3567 if (!strcmp(insn->sec->name, ".init.text") && !module) 3568 continue; 3569 3570 WARN_FUNC("indirect %s found in RETPOLINE build", 3571 insn->sec, insn->offset, 3572 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); 3573 3574 warnings++; 3575 } 3576 3577 return warnings; 3578 } 3579 3580 static bool is_kasan_insn(struct instruction *insn) 3581 { 3582 return (insn->type == INSN_CALL && 3583 !strcmp(insn->call_dest->name, "__asan_handle_no_return")); 3584 } 3585 3586 static bool is_ubsan_insn(struct instruction *insn) 3587 { 3588 return (insn->type == INSN_CALL && 3589 !strcmp(insn->call_dest->name, 3590 "__ubsan_handle_builtin_unreachable")); 3591 } 3592 3593 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn) 3594 { 3595 int i; 3596 struct instruction *prev_insn; 3597 3598 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP) 3599 return true; 3600 3601 /* 3602 * Ignore alternative replacement instructions. This can happen 3603 * when a whitelisted function uses one of the ALTERNATIVE macros. 3604 */ 3605 if (!strcmp(insn->sec->name, ".altinstr_replacement") || 3606 !strcmp(insn->sec->name, ".altinstr_aux")) 3607 return true; 3608 3609 /* 3610 * Whole archive runs might encounder dead code from weak symbols. 3611 * This is where the linker will have dropped the weak symbol in 3612 * favour of a regular symbol, but leaves the code in place. 3613 * 3614 * In this case we'll find a piece of code (whole function) that is not 3615 * covered by a !section symbol. Ignore them. 3616 */ 3617 if (!insn->func && lto) { 3618 int size = find_symbol_hole_containing(insn->sec, insn->offset); 3619 unsigned long end = insn->offset + size; 3620 3621 if (!size) /* not a hole */ 3622 return false; 3623 3624 if (size < 0) /* hole until the end */ 3625 return true; 3626 3627 sec_for_each_insn_continue(file, insn) { 3628 /* 3629 * If we reach a visited instruction at or before the 3630 * end of the hole, ignore the unreachable. 3631 */ 3632 if (insn->visited) 3633 return true; 3634 3635 if (insn->offset >= end) 3636 break; 3637 3638 /* 3639 * If this hole jumps to a .cold function, mark it ignore too. 3640 */ 3641 if (insn->jump_dest && insn->jump_dest->func && 3642 strstr(insn->jump_dest->func->name, ".cold")) { 3643 struct instruction *dest = insn->jump_dest; 3644 func_for_each_insn(file, dest->func, dest) 3645 dest->ignore = true; 3646 } 3647 } 3648 3649 return false; 3650 } 3651 3652 if (!insn->func) 3653 return false; 3654 3655 if (insn->func->static_call_tramp) 3656 return true; 3657 3658 /* 3659 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees 3660 * __builtin_unreachable(). The BUG() macro has an unreachable() after 3661 * the UD2, which causes GCC's undefined trap logic to emit another UD2 3662 * (or occasionally a JMP to UD2). 3663 * 3664 * It may also insert a UD2 after calling a __noreturn function. 3665 */ 3666 prev_insn = list_prev_entry(insn, list); 3667 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) && 3668 (insn->type == INSN_BUG || 3669 (insn->type == INSN_JUMP_UNCONDITIONAL && 3670 insn->jump_dest && insn->jump_dest->type == INSN_BUG))) 3671 return true; 3672 3673 /* 3674 * Check if this (or a subsequent) instruction is related to 3675 * CONFIG_UBSAN or CONFIG_KASAN. 3676 * 3677 * End the search at 5 instructions to avoid going into the weeds. 3678 */ 3679 for (i = 0; i < 5; i++) { 3680 3681 if (is_kasan_insn(insn) || is_ubsan_insn(insn)) 3682 return true; 3683 3684 if (insn->type == INSN_JUMP_UNCONDITIONAL) { 3685 if (insn->jump_dest && 3686 insn->jump_dest->func == insn->func) { 3687 insn = insn->jump_dest; 3688 continue; 3689 } 3690 3691 break; 3692 } 3693 3694 if (insn->offset + insn->len >= insn->func->offset + insn->func->len) 3695 break; 3696 3697 insn = list_next_entry(insn, list); 3698 } 3699 3700 return false; 3701 } 3702 3703 static int validate_symbol(struct objtool_file *file, struct section *sec, 3704 struct symbol *sym, struct insn_state *state) 3705 { 3706 struct instruction *insn; 3707 int ret; 3708 3709 if (!sym->len) { 3710 WARN("%s() is missing an ELF size annotation", sym->name); 3711 return 1; 3712 } 3713 3714 if (sym->pfunc != sym || sym->alias != sym) 3715 return 0; 3716 3717 insn = find_insn(file, sec, sym->offset); 3718 if (!insn || insn->ignore || insn->visited) 3719 return 0; 3720 3721 state->uaccess = sym->uaccess_safe; 3722 3723 ret = validate_branch(file, insn->func, insn, *state); 3724 if (ret && backtrace) 3725 BT_FUNC("<=== (sym)", insn); 3726 return ret; 3727 } 3728 3729 static int validate_section(struct objtool_file *file, struct section *sec) 3730 { 3731 struct insn_state state; 3732 struct symbol *func; 3733 int warnings = 0; 3734 3735 list_for_each_entry(func, &sec->symbol_list, list) { 3736 if (func->type != STT_FUNC) 3737 continue; 3738 3739 init_insn_state(&state, sec); 3740 set_func_state(&state.cfi); 3741 3742 warnings += validate_symbol(file, sec, func, &state); 3743 } 3744 3745 return warnings; 3746 } 3747 3748 static int validate_vmlinux_functions(struct objtool_file *file) 3749 { 3750 struct section *sec; 3751 int warnings = 0; 3752 3753 sec = find_section_by_name(file->elf, ".noinstr.text"); 3754 if (sec) { 3755 warnings += validate_section(file, sec); 3756 warnings += validate_unwind_hints(file, sec); 3757 } 3758 3759 sec = find_section_by_name(file->elf, ".entry.text"); 3760 if (sec) { 3761 warnings += validate_section(file, sec); 3762 warnings += validate_unwind_hints(file, sec); 3763 } 3764 3765 return warnings; 3766 } 3767 3768 static int validate_functions(struct objtool_file *file) 3769 { 3770 struct section *sec; 3771 int warnings = 0; 3772 3773 for_each_sec(file, sec) { 3774 if (!(sec->sh.sh_flags & SHF_EXECINSTR)) 3775 continue; 3776 3777 warnings += validate_section(file, sec); 3778 } 3779 3780 return warnings; 3781 } 3782 3783 static int validate_ibt(struct objtool_file *file) 3784 { 3785 struct section *sec; 3786 struct reloc *reloc; 3787 3788 for_each_sec(file, sec) { 3789 bool is_data; 3790 3791 /* already done in validate_branch() */ 3792 if (sec->sh.sh_flags & SHF_EXECINSTR) 3793 continue; 3794 3795 if (!sec->reloc) 3796 continue; 3797 3798 if (!strncmp(sec->name, ".orc", 4)) 3799 continue; 3800 3801 if (!strncmp(sec->name, ".discard", 8)) 3802 continue; 3803 3804 if (!strncmp(sec->name, ".debug", 6)) 3805 continue; 3806 3807 if (!strcmp(sec->name, "_error_injection_whitelist")) 3808 continue; 3809 3810 if (!strcmp(sec->name, "_kprobe_blacklist")) 3811 continue; 3812 3813 is_data = strstr(sec->name, ".data") || strstr(sec->name, ".rodata"); 3814 3815 list_for_each_entry(reloc, &sec->reloc->reloc_list, list) { 3816 struct instruction *dest; 3817 3818 dest = validate_ibt_reloc(file, reloc); 3819 if (is_data && dest && !dest->noendbr) { 3820 warn_noendbr("data ", reloc->sym->sec, 3821 reloc->sym->offset + reloc->addend, 3822 dest); 3823 } 3824 } 3825 } 3826 3827 return 0; 3828 } 3829 3830 static int validate_reachable_instructions(struct objtool_file *file) 3831 { 3832 struct instruction *insn; 3833 3834 if (file->ignore_unreachables) 3835 return 0; 3836 3837 for_each_insn(file, insn) { 3838 if (insn->visited || ignore_unreachable_insn(file, insn)) 3839 continue; 3840 3841 WARN_FUNC("unreachable instruction", insn->sec, insn->offset); 3842 return 1; 3843 } 3844 3845 return 0; 3846 } 3847 3848 int check(struct objtool_file *file) 3849 { 3850 int ret, warnings = 0; 3851 3852 if (lto && !(vmlinux || module)) { 3853 fprintf(stderr, "--lto requires: --vmlinux or --module\n"); 3854 return 1; 3855 } 3856 3857 if (ibt && !lto) { 3858 fprintf(stderr, "--ibt requires: --lto\n"); 3859 return 1; 3860 } 3861 3862 arch_initial_func_cfi_state(&initial_func_cfi); 3863 init_cfi_state(&init_cfi); 3864 init_cfi_state(&func_cfi); 3865 set_func_state(&func_cfi); 3866 3867 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) 3868 goto out; 3869 3870 cfi_hash_add(&init_cfi); 3871 cfi_hash_add(&func_cfi); 3872 3873 ret = decode_sections(file); 3874 if (ret < 0) 3875 goto out; 3876 3877 warnings += ret; 3878 3879 if (list_empty(&file->insn_list)) 3880 goto out; 3881 3882 if (vmlinux && !lto) { 3883 ret = validate_vmlinux_functions(file); 3884 if (ret < 0) 3885 goto out; 3886 3887 warnings += ret; 3888 goto out; 3889 } 3890 3891 if (retpoline) { 3892 ret = validate_retpoline(file); 3893 if (ret < 0) 3894 return ret; 3895 warnings += ret; 3896 } 3897 3898 ret = validate_functions(file); 3899 if (ret < 0) 3900 goto out; 3901 warnings += ret; 3902 3903 ret = validate_unwind_hints(file, NULL); 3904 if (ret < 0) 3905 goto out; 3906 warnings += ret; 3907 3908 if (ibt) { 3909 ret = validate_ibt(file); 3910 if (ret < 0) 3911 goto out; 3912 warnings += ret; 3913 } 3914 3915 if (!warnings) { 3916 ret = validate_reachable_instructions(file); 3917 if (ret < 0) 3918 goto out; 3919 warnings += ret; 3920 } 3921 3922 ret = create_static_call_sections(file); 3923 if (ret < 0) 3924 goto out; 3925 warnings += ret; 3926 3927 if (retpoline) { 3928 ret = create_retpoline_sites_sections(file); 3929 if (ret < 0) 3930 goto out; 3931 warnings += ret; 3932 } 3933 3934 if (mcount) { 3935 ret = create_mcount_loc_sections(file); 3936 if (ret < 0) 3937 goto out; 3938 warnings += ret; 3939 } 3940 3941 if (ibt) { 3942 ret = create_ibt_endbr_seal_sections(file); 3943 if (ret < 0) 3944 goto out; 3945 warnings += ret; 3946 } 3947 3948 if (stats) { 3949 printf("nr_insns_visited: %ld\n", nr_insns_visited); 3950 printf("nr_cfi: %ld\n", nr_cfi); 3951 printf("nr_cfi_reused: %ld\n", nr_cfi_reused); 3952 printf("nr_cfi_cache: %ld\n", nr_cfi_cache); 3953 } 3954 3955 out: 3956 /* 3957 * For now, don't fail the kernel build on fatal warnings. These 3958 * errors are still fairly common due to the growing matrix of 3959 * supported toolchains and their recent pace of change. 3960 */ 3961 return 0; 3962 } 3963