1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> 4 */ 5 6 #define _GNU_SOURCE /* memmem() */ 7 #include <fnmatch.h> 8 #include <string.h> 9 #include <stdlib.h> 10 #include <inttypes.h> 11 #include <sys/mman.h> 12 13 #include <objtool/builtin.h> 14 #include <objtool/cfi.h> 15 #include <objtool/arch.h> 16 #include <objtool/disas.h> 17 #include <objtool/check.h> 18 #include <objtool/special.h> 19 #include <objtool/trace.h> 20 #include <objtool/warn.h> 21 #include <objtool/checksum.h> 22 #include <objtool/util.h> 23 24 #include <linux/objtool_types.h> 25 #include <linux/hashtable.h> 26 #include <linux/kernel.h> 27 #include <linux/static_call_types.h> 28 #include <linux/string.h> 29 30 struct alternative { 31 struct alternative *next; 32 struct instruction *insn; 33 }; 34 35 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache; 36 37 static struct cfi_init_state initial_func_cfi; 38 static struct cfi_state init_cfi; 39 static struct cfi_state func_cfi; 40 static struct cfi_state force_undefined_cfi; 41 42 struct disas_context *objtool_disas_ctx; 43 44 size_t sym_name_max_len; 45 46 struct instruction *find_insn(struct objtool_file *file, 47 struct section *sec, unsigned long offset) 48 { 49 struct instruction *insn; 50 51 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { 52 if (insn->sec == sec && insn->offset == offset) 53 return insn; 54 } 55 56 return NULL; 57 } 58 59 struct instruction *next_insn_same_sec(struct objtool_file *file, 60 struct instruction *insn) 61 { 62 if (insn->idx == INSN_CHUNK_MAX) 63 return find_insn(file, insn->sec, insn->offset + insn->len); 64 65 insn++; 66 if (!insn->len) 67 return NULL; 68 69 return insn; 70 } 71 72 static struct instruction *next_insn_same_func(struct objtool_file *file, 73 struct instruction *insn) 74 { 75 struct instruction *next = next_insn_same_sec(file, insn); 76 struct symbol *func = insn_func(insn); 77 78 if (!func) 79 return NULL; 80 81 if (next && insn_func(next) == func) 82 return next; 83 84 /* Check if we're already in the subfunction: */ 85 if (func == func->cfunc) 86 return NULL; 87 88 /* Move to the subfunction: */ 89 return find_insn(file, func->cfunc->sec, func->cfunc->offset); 90 } 91 92 static struct instruction *prev_insn_same_sec(struct objtool_file *file, 93 struct instruction *insn) 94 { 95 if (insn->idx == 0) { 96 if (insn->prev_len) 97 return find_insn(file, insn->sec, insn->offset - insn->prev_len); 98 return NULL; 99 } 100 101 return insn - 1; 102 } 103 104 static struct instruction *prev_insn_same_sym(struct objtool_file *file, 105 struct instruction *insn) 106 { 107 struct instruction *prev = prev_insn_same_sec(file, insn); 108 109 if (prev && insn_func(prev) == insn_func(insn)) 110 return prev; 111 112 return NULL; 113 } 114 115 #define for_each_insn(file, insn) \ 116 for (struct section *__sec, *__fake = (struct section *)1; \ 117 __fake; __fake = NULL) \ 118 for_each_sec(file->elf, __sec) \ 119 sec_for_each_insn(file, __sec, insn) 120 121 #define func_for_each_insn(file, func, insn) \ 122 for (insn = find_insn(file, func->sec, func->offset); \ 123 insn; \ 124 insn = next_insn_same_func(file, insn)) 125 126 #define sym_for_each_insn(file, sym, insn) \ 127 for (insn = find_insn(file, sym->sec, sym->offset); \ 128 insn && insn->offset < sym->offset + sym->len; \ 129 insn = next_insn_same_sec(file, insn)) 130 131 #define sym_for_each_insn_continue_reverse(file, sym, insn) \ 132 for (insn = prev_insn_same_sec(file, insn); \ 133 insn && insn->offset >= sym->offset; \ 134 insn = prev_insn_same_sec(file, insn)) 135 136 #define sec_for_each_insn_from(file, insn) \ 137 for (; insn; insn = next_insn_same_sec(file, insn)) 138 139 #define sec_for_each_insn_continue(file, insn) \ 140 for (insn = next_insn_same_sec(file, insn); insn; \ 141 insn = next_insn_same_sec(file, insn)) 142 143 static inline struct reloc *insn_jump_table(struct instruction *insn) 144 { 145 if (insn->type == INSN_JUMP_DYNAMIC || 146 insn->type == INSN_CALL_DYNAMIC) 147 return insn->_jump_table; 148 149 return NULL; 150 } 151 152 static inline unsigned long insn_jump_table_size(struct instruction *insn) 153 { 154 if (insn->type == INSN_JUMP_DYNAMIC || 155 insn->type == INSN_CALL_DYNAMIC) 156 return insn->_jump_table_size; 157 158 return 0; 159 } 160 161 static bool is_jump_table_jump(struct instruction *insn) 162 { 163 struct alt_group *alt_group = insn->alt_group; 164 165 if (insn_jump_table(insn)) 166 return true; 167 168 /* Retpoline alternative for a jump table? */ 169 return alt_group && alt_group->orig_group && 170 insn_jump_table(alt_group->orig_group->first_insn); 171 } 172 173 static bool is_sibling_call(struct instruction *insn) 174 { 175 /* 176 * Assume only STT_FUNC calls have jump-tables. 177 */ 178 if (insn_func(insn)) { 179 /* An indirect jump is either a sibling call or a jump to a table. */ 180 if (insn->type == INSN_JUMP_DYNAMIC) 181 return !is_jump_table_jump(insn); 182 } 183 184 /* add_jump_destinations() sets insn_call_dest(insn) for sibling calls. */ 185 return (is_static_jump(insn) && insn_call_dest(insn)); 186 } 187 188 /* 189 * Checks if a function is a Rust "noreturn" one. 190 */ 191 static bool is_rust_noreturn(const struct symbol *func) 192 { 193 /* 194 * If it does not start with "_R", then it is not a Rust symbol. 195 */ 196 if (strncmp(func->name, "_R", 2)) 197 return false; 198 199 /* 200 * These are just heuristics -- we do not control the precise symbol 201 * name, due to the crate disambiguators (which depend on the compiler) 202 * as well as changes to the source code itself between versions (since 203 * these come from the Rust standard library). 204 */ 205 return str_ends_with(func->name, "_4core5sliceSp15copy_from_slice17len_mismatch_fail") || 206 str_ends_with(func->name, "_4core6option13expect_failed") || 207 str_ends_with(func->name, "_4core6option13unwrap_failed") || 208 str_ends_with(func->name, "_4core6result13unwrap_failed") || 209 str_ends_with(func->name, "_4core9panicking5panic") || 210 str_ends_with(func->name, "_4core9panicking9panic_fmt") || 211 str_ends_with(func->name, "_4core9panicking14panic_explicit") || 212 str_ends_with(func->name, "_4core9panicking14panic_nounwind") || 213 str_ends_with(func->name, "_4core9panicking18panic_bounds_check") || 214 str_ends_with(func->name, "_4core9panicking18panic_nounwind_fmt") || 215 str_ends_with(func->name, "_4core9panicking19assert_failed_inner") || 216 str_ends_with(func->name, "_4core9panicking30panic_null_pointer_dereference") || 217 str_ends_with(func->name, "_4core9panicking36panic_misaligned_pointer_dereference") || 218 str_ends_with(func->name, "_7___rustc17rust_begin_unwind") || 219 strstr(func->name, "_4core9panicking13assert_failed") || 220 strstr(func->name, "_4core9panicking11panic_const24panic_const_") || 221 (strstr(func->name, "_4core5slice5index") && 222 strstr(func->name, "slice_") && 223 str_ends_with(func->name, "_fail")); 224 } 225 226 /* 227 * This checks to see if the given function is a "noreturn" function. 228 * 229 * For global functions which are outside the scope of this object file, we 230 * have to keep a manual list of them. 231 * 232 * For local functions, we have to detect them manually by simply looking for 233 * the lack of a return instruction. 234 */ 235 static bool __dead_end_function(struct objtool_file *file, struct symbol *func, 236 int recursion) 237 { 238 int i; 239 struct instruction *insn; 240 bool empty = true; 241 242 #define NORETURN(func) __stringify(func), 243 static const char * const global_noreturns[] = { 244 #include "noreturns.h" 245 }; 246 #undef NORETURN 247 248 if (!func) 249 return false; 250 251 if (!is_local_sym(func)) { 252 if (is_rust_noreturn(func)) 253 return true; 254 255 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) 256 if (!strcmp(func->name, global_noreturns[i])) 257 return true; 258 } 259 260 if (is_weak_sym(func)) 261 return false; 262 263 if (!func->len) 264 return false; 265 266 insn = find_insn(file, func->sec, func->offset); 267 if (!insn || !insn_func(insn)) 268 return false; 269 270 func_for_each_insn(file, func, insn) { 271 empty = false; 272 273 if (insn->type == INSN_RETURN) 274 return false; 275 } 276 277 if (empty) 278 return false; 279 280 /* 281 * A function can have a sibling call instead of a return. In that 282 * case, the function's dead-end status depends on whether the target 283 * of the sibling call returns. 284 */ 285 func_for_each_insn(file, func, insn) { 286 if (is_sibling_call(insn)) { 287 struct instruction *dest = insn->jump_dest; 288 289 if (!dest) 290 /* sibling call to another file */ 291 return false; 292 293 /* local sibling call */ 294 if (recursion == 5) { 295 /* 296 * Infinite recursion: two functions have 297 * sibling calls to each other. This is a very 298 * rare case. It means they aren't dead ends. 299 */ 300 return false; 301 } 302 303 return __dead_end_function(file, insn_func(dest), recursion+1); 304 } 305 } 306 307 return true; 308 } 309 310 static bool dead_end_function(struct objtool_file *file, struct symbol *func) 311 { 312 return __dead_end_function(file, func, 0); 313 } 314 315 static void init_cfi_state(struct cfi_state *cfi) 316 { 317 int i; 318 319 for (i = 0; i < CFI_NUM_REGS; i++) { 320 cfi->regs[i].base = CFI_UNDEFINED; 321 cfi->vals[i].base = CFI_UNDEFINED; 322 } 323 cfi->cfa.base = CFI_UNDEFINED; 324 cfi->drap_reg = CFI_UNDEFINED; 325 cfi->drap_offset = -1; 326 } 327 328 static void init_insn_state(struct objtool_file *file, struct insn_state *state, 329 struct section *sec) 330 { 331 memset(state, 0, sizeof(*state)); 332 init_cfi_state(&state->cfi); 333 334 if (opts.noinstr && sec) 335 state->noinstr = sec->noinstr; 336 } 337 338 static struct cfi_state *cfi_alloc(void) 339 { 340 struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state)); 341 if (!cfi) { 342 ERROR_GLIBC("calloc"); 343 exit(1); 344 } 345 nr_cfi++; 346 return cfi; 347 } 348 349 static int cfi_bits; 350 static struct hlist_head *cfi_hash; 351 352 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2) 353 { 354 return memcmp((void *)cfi1 + sizeof(cfi1->hash), 355 (void *)cfi2 + sizeof(cfi2->hash), 356 sizeof(struct cfi_state) - sizeof(struct hlist_node)); 357 } 358 359 static inline u32 cfi_key(struct cfi_state *cfi) 360 { 361 return jhash((void *)cfi + sizeof(cfi->hash), 362 sizeof(*cfi) - sizeof(cfi->hash), 0); 363 } 364 365 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi) 366 { 367 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; 368 struct cfi_state *obj; 369 370 hlist_for_each_entry(obj, head, hash) { 371 if (!cficmp(cfi, obj)) { 372 nr_cfi_cache++; 373 return obj; 374 } 375 } 376 377 obj = cfi_alloc(); 378 *obj = *cfi; 379 hlist_add_head(&obj->hash, head); 380 381 return obj; 382 } 383 384 static void cfi_hash_add(struct cfi_state *cfi) 385 { 386 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)]; 387 388 hlist_add_head(&cfi->hash, head); 389 } 390 391 static void *cfi_hash_alloc(unsigned long size) 392 { 393 cfi_bits = max(10, ilog2(size)); 394 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits, 395 PROT_READ|PROT_WRITE, 396 MAP_PRIVATE|MAP_ANON, -1, 0); 397 if (cfi_hash == (void *)-1L) { 398 ERROR_GLIBC("mmap fail cfi_hash"); 399 cfi_hash = NULL; 400 } else if (opts.stats) { 401 printf("cfi_bits: %d\n", cfi_bits); 402 } 403 404 return cfi_hash; 405 } 406 407 static unsigned long nr_insns; 408 static unsigned long nr_insns_visited; 409 410 /* 411 * Call the arch-specific instruction decoder for all the instructions and add 412 * them to the global instruction list. 413 */ 414 static int decode_instructions(struct objtool_file *file) 415 { 416 struct section *sec; 417 struct symbol *func; 418 unsigned long offset; 419 struct instruction *insn; 420 421 for_each_sec(file->elf, sec) { 422 struct instruction *insns = NULL; 423 u8 prev_len = 0; 424 u8 idx = 0; 425 426 if (!is_text_sec(sec)) 427 continue; 428 429 if (strcmp(sec->name, ".altinstr_replacement") && 430 strcmp(sec->name, ".altinstr_aux") && 431 strncmp(sec->name, ".discard.", 9)) 432 sec->text = true; 433 434 if (!strcmp(sec->name, ".noinstr.text") || 435 !strcmp(sec->name, ".entry.text") || 436 !strcmp(sec->name, ".cpuidle.text") || 437 !strncmp(sec->name, ".text..__x86.", 13)) 438 sec->noinstr = true; 439 440 /* 441 * .init.text code is ran before userspace and thus doesn't 442 * strictly need retpolines, except for modules which are 443 * loaded late, they very much do need retpoline in their 444 * .init.text 445 */ 446 if (!strcmp(sec->name, ".init.text") && !opts.module) 447 sec->init = true; 448 449 for (offset = 0; offset < sec_size(sec); offset += insn->len) { 450 if (!insns || idx == INSN_CHUNK_MAX) { 451 insns = calloc(INSN_CHUNK_SIZE, sizeof(*insn)); 452 if (!insns) { 453 ERROR_GLIBC("calloc"); 454 return -1; 455 } 456 idx = 0; 457 } else { 458 idx++; 459 } 460 insn = &insns[idx]; 461 insn->idx = idx; 462 463 INIT_LIST_HEAD(&insn->call_node); 464 insn->sec = sec; 465 insn->offset = offset; 466 insn->prev_len = prev_len; 467 468 if (arch_decode_instruction(file, sec, offset, sec_size(sec) - offset, insn)) 469 return -1; 470 471 prev_len = insn->len; 472 473 /* 474 * By default, "ud2" is a dead end unless otherwise 475 * annotated, because GCC 7 inserts it for certain 476 * divide-by-zero cases. 477 */ 478 if (insn->type == INSN_BUG) 479 insn->dead_end = true; 480 481 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); 482 nr_insns++; 483 } 484 485 sec_for_each_sym(sec, func) { 486 if (!is_notype_sym(func) && !is_func_sym(func)) 487 continue; 488 489 if (func->offset == sec_size(sec)) { 490 /* Heuristic: likely an "end" symbol */ 491 if (is_notype_sym(func)) 492 continue; 493 ERROR("%s(): STT_FUNC at end of section", func->name); 494 return -1; 495 } 496 497 if (func->embedded_insn || func->alias != func) 498 continue; 499 500 if (!find_insn(file, sec, func->offset)) { 501 ERROR("%s(): can't find starting instruction", func->name); 502 return -1; 503 } 504 505 sym_for_each_insn(file, func, insn) { 506 insn->sym = func; 507 if (is_func_sym(func) && 508 insn->type == INSN_ENDBR && 509 list_empty(&insn->call_node)) { 510 if (insn->offset == func->offset) { 511 list_add_tail(&insn->call_node, &file->endbr_list); 512 file->nr_endbr++; 513 } else { 514 file->nr_endbr_int++; 515 } 516 } 517 } 518 } 519 } 520 521 if (opts.stats) 522 printf("nr_insns: %lu\n", nr_insns); 523 524 return 0; 525 } 526 527 /* 528 * Read the pv_ops[] .data table to find the static initialized values. 529 */ 530 static int add_pv_ops(struct objtool_file *file, const char *symname) 531 { 532 struct symbol *sym, *func; 533 unsigned long off, end; 534 struct reloc *reloc; 535 int idx; 536 537 sym = find_symbol_by_name(file->elf, symname); 538 if (!sym) 539 return 0; 540 541 off = sym->offset; 542 end = off + sym->len; 543 for (;;) { 544 reloc = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); 545 if (!reloc) 546 break; 547 548 idx = (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long); 549 550 func = reloc->sym; 551 if (is_sec_sym(func)) 552 func = find_symbol_by_offset(reloc->sym->sec, 553 reloc_addend(reloc)); 554 if (!func) { 555 ERROR_FUNC(reloc->sym->sec, reloc_addend(reloc), 556 "can't find func at %s[%d]", symname, idx); 557 return -1; 558 } 559 560 if (objtool_pv_add(file, idx, func)) 561 return -1; 562 563 off = reloc_offset(reloc) + 1; 564 if (off > end) 565 break; 566 } 567 568 return 0; 569 } 570 571 /* 572 * Allocate and initialize file->pv_ops[]. 573 */ 574 static int init_pv_ops(struct objtool_file *file) 575 { 576 static const char *pv_ops_tables[] = { 577 "pv_ops", 578 "xen_cpu_ops", 579 "xen_irq_ops", 580 "xen_mmu_ops", 581 NULL, 582 }; 583 const char *pv_ops; 584 struct symbol *sym; 585 int idx, nr; 586 587 if (!opts.noinstr) 588 return 0; 589 590 file->pv_ops = NULL; 591 592 sym = find_symbol_by_name(file->elf, "pv_ops"); 593 if (!sym) 594 return 0; 595 596 nr = sym->len / sizeof(unsigned long); 597 file->pv_ops = calloc(nr, sizeof(struct pv_state)); 598 if (!file->pv_ops) { 599 ERROR_GLIBC("calloc"); 600 return -1; 601 } 602 603 for (idx = 0; idx < nr; idx++) 604 INIT_LIST_HEAD(&file->pv_ops[idx].targets); 605 606 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++) { 607 if (add_pv_ops(file, pv_ops)) 608 return -1; 609 } 610 611 return 0; 612 } 613 614 static bool is_livepatch_module(struct objtool_file *file) 615 { 616 struct section *sec; 617 618 if (!opts.module) 619 return false; 620 621 sec = find_section_by_name(file->elf, ".modinfo"); 622 if (!sec) 623 return false; 624 625 return memmem(sec->data->d_buf, sec_size(sec), "\0livepatch=Y", 12); 626 } 627 628 static int create_static_call_sections(struct objtool_file *file) 629 { 630 struct static_call_site *site; 631 struct section *sec; 632 struct instruction *insn; 633 struct symbol *key_sym; 634 char *key_name, *tmp; 635 int idx; 636 637 sec = find_section_by_name(file->elf, ".static_call_sites"); 638 if (sec) { 639 /* 640 * Livepatch modules may have already extracted the static call 641 * site entries to take advantage of vmlinux static call 642 * privileges. 643 */ 644 if (!file->klp) 645 WARN("file already has .static_call_sites section, skipping"); 646 647 return 0; 648 } 649 650 if (list_empty(&file->static_call_list)) 651 return 0; 652 653 idx = 0; 654 list_for_each_entry(insn, &file->static_call_list, call_node) 655 idx++; 656 657 sec = elf_create_section_pair(file->elf, ".static_call_sites", 658 sizeof(*site), idx, idx * 2); 659 if (!sec) 660 return -1; 661 662 /* Allow modules to modify the low bits of static_call_site::key */ 663 sec->sh.sh_flags |= SHF_WRITE; 664 665 idx = 0; 666 list_for_each_entry(insn, &file->static_call_list, call_node) { 667 668 /* populate reloc for 'addr' */ 669 if (!elf_init_reloc_text_sym(file->elf, sec, 670 idx * sizeof(*site), idx * 2, 671 insn->sec, insn->offset)) 672 return -1; 673 674 /* find key symbol */ 675 key_name = strdup(insn_call_dest(insn)->name); 676 if (!key_name) { 677 ERROR_GLIBC("strdup"); 678 return -1; 679 } 680 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR, 681 STATIC_CALL_TRAMP_PREFIX_LEN)) { 682 ERROR("static_call: trampoline name malformed: %s", key_name); 683 return -1; 684 } 685 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN; 686 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN); 687 688 key_sym = find_symbol_by_name(file->elf, tmp); 689 if (!key_sym) { 690 if (!opts.module || file->klp) { 691 ERROR("static_call: can't find static_call_key symbol: %s", tmp); 692 return -1; 693 } 694 695 /* 696 * For modules(), the key might not be exported, which 697 * means the module can make static calls but isn't 698 * allowed to change them. 699 * 700 * In that case we temporarily set the key to be the 701 * trampoline address. This is fixed up in 702 * static_call_add_module(). 703 */ 704 key_sym = insn_call_dest(insn); 705 } 706 707 /* populate reloc for 'key' */ 708 if (!elf_init_reloc_data_sym(file->elf, sec, 709 idx * sizeof(*site) + 4, 710 (idx * 2) + 1, key_sym, 711 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) 712 return -1; 713 714 idx++; 715 } 716 717 return 0; 718 } 719 720 static int create_retpoline_sites_sections(struct objtool_file *file) 721 { 722 struct instruction *insn; 723 struct section *sec; 724 int idx; 725 726 sec = find_section_by_name(file->elf, ".retpoline_sites"); 727 if (sec) { 728 WARN("file already has .retpoline_sites, skipping"); 729 return 0; 730 } 731 732 idx = 0; 733 list_for_each_entry(insn, &file->retpoline_call_list, call_node) 734 idx++; 735 736 if (!idx) 737 return 0; 738 739 sec = elf_create_section_pair(file->elf, ".retpoline_sites", 740 sizeof(int), idx, idx); 741 if (!sec) 742 return -1; 743 744 idx = 0; 745 list_for_each_entry(insn, &file->retpoline_call_list, call_node) { 746 747 if (!elf_init_reloc_text_sym(file->elf, sec, 748 idx * sizeof(int), idx, 749 insn->sec, insn->offset)) 750 return -1; 751 752 idx++; 753 } 754 755 return 0; 756 } 757 758 static int create_return_sites_sections(struct objtool_file *file) 759 { 760 struct instruction *insn; 761 struct section *sec; 762 int idx; 763 764 sec = find_section_by_name(file->elf, ".return_sites"); 765 if (sec) { 766 WARN("file already has .return_sites, skipping"); 767 return 0; 768 } 769 770 idx = 0; 771 list_for_each_entry(insn, &file->return_thunk_list, call_node) 772 idx++; 773 774 if (!idx) 775 return 0; 776 777 sec = elf_create_section_pair(file->elf, ".return_sites", 778 sizeof(int), idx, idx); 779 if (!sec) 780 return -1; 781 782 idx = 0; 783 list_for_each_entry(insn, &file->return_thunk_list, call_node) { 784 785 if (!elf_init_reloc_text_sym(file->elf, sec, 786 idx * sizeof(int), idx, 787 insn->sec, insn->offset)) 788 return -1; 789 790 idx++; 791 } 792 793 return 0; 794 } 795 796 static int create_ibt_endbr_seal_sections(struct objtool_file *file) 797 { 798 struct instruction *insn; 799 struct section *sec; 800 int idx; 801 802 sec = find_section_by_name(file->elf, ".ibt_endbr_seal"); 803 if (sec) { 804 WARN("file already has .ibt_endbr_seal, skipping"); 805 return 0; 806 } 807 808 idx = 0; 809 list_for_each_entry(insn, &file->endbr_list, call_node) 810 idx++; 811 812 if (opts.stats) { 813 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr); 814 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int); 815 printf("ibt: superfluous ENDBR: %d\n", idx); 816 } 817 818 if (!idx) 819 return 0; 820 821 sec = elf_create_section_pair(file->elf, ".ibt_endbr_seal", 822 sizeof(int), idx, idx); 823 if (!sec) 824 return -1; 825 826 idx = 0; 827 list_for_each_entry(insn, &file->endbr_list, call_node) { 828 829 int *site = (int *)sec->data->d_buf + idx; 830 struct symbol *sym = insn->sym; 831 *site = 0; 832 833 if (opts.module && sym && is_func_sym(sym) && 834 insn->offset == sym->offset && 835 (!strcmp(sym->name, "init_module") || 836 !strcmp(sym->name, "cleanup_module"))) { 837 ERROR("%s(): Magic init_module() function name is deprecated, use module_init(fn) instead", 838 sym->name); 839 return -1; 840 } 841 842 if (!elf_init_reloc_text_sym(file->elf, sec, 843 idx * sizeof(int), idx, 844 insn->sec, insn->offset)) 845 return -1; 846 847 idx++; 848 } 849 850 return 0; 851 } 852 853 static int create_cfi_sections(struct objtool_file *file) 854 { 855 struct section *sec; 856 struct symbol *sym; 857 int idx; 858 859 sec = find_section_by_name(file->elf, ".cfi_sites"); 860 if (sec) { 861 WARN("file already has .cfi_sites section, skipping"); 862 return 0; 863 } 864 865 idx = 0; 866 for_each_sym(file->elf, sym) { 867 if (!is_func_sym(sym)) 868 continue; 869 870 if (strncmp(sym->name, "__cfi_", 6)) 871 continue; 872 873 idx++; 874 } 875 876 sec = elf_create_section_pair(file->elf, ".cfi_sites", 877 sizeof(unsigned int), idx, idx); 878 if (!sec) 879 return -1; 880 881 idx = 0; 882 for_each_sym(file->elf, sym) { 883 if (!is_func_sym(sym)) 884 continue; 885 886 if (strncmp(sym->name, "__cfi_", 6)) 887 continue; 888 889 if (!elf_init_reloc_text_sym(file->elf, sec, 890 idx * sizeof(unsigned int), idx, 891 sym->sec, sym->offset)) 892 return -1; 893 894 idx++; 895 } 896 897 return 0; 898 } 899 900 static int create_mcount_loc_sections(struct objtool_file *file) 901 { 902 size_t addr_size = elf_addr_size(file->elf); 903 struct instruction *insn; 904 struct section *sec; 905 int idx; 906 907 sec = find_section_by_name(file->elf, "__mcount_loc"); 908 if (sec) { 909 /* 910 * Livepatch modules have already extracted their __mcount_loc 911 * entries to cover the !CONFIG_FTRACE_MCOUNT_USE_OBJTOOL case. 912 */ 913 if (!file->klp) 914 WARN("file already has __mcount_loc section, skipping"); 915 916 return 0; 917 } 918 919 if (list_empty(&file->mcount_loc_list)) 920 return 0; 921 922 idx = 0; 923 list_for_each_entry(insn, &file->mcount_loc_list, call_node) 924 idx++; 925 926 sec = elf_create_section_pair(file->elf, "__mcount_loc", addr_size, 927 idx, idx); 928 if (!sec) 929 return -1; 930 931 sec->sh.sh_addralign = addr_size; 932 933 idx = 0; 934 list_for_each_entry(insn, &file->mcount_loc_list, call_node) { 935 936 struct reloc *reloc; 937 938 reloc = elf_init_reloc_text_sym(file->elf, sec, idx * addr_size, idx, 939 insn->sec, insn->offset); 940 if (!reloc) 941 return -1; 942 943 set_reloc_type(file->elf, reloc, addr_size == 8 ? R_ABS64 : R_ABS32); 944 945 idx++; 946 } 947 948 return 0; 949 } 950 951 static int create_direct_call_sections(struct objtool_file *file) 952 { 953 struct instruction *insn; 954 struct section *sec; 955 int idx; 956 957 sec = find_section_by_name(file->elf, ".call_sites"); 958 if (sec) { 959 WARN("file already has .call_sites section, skipping"); 960 return 0; 961 } 962 963 if (list_empty(&file->call_list)) 964 return 0; 965 966 idx = 0; 967 list_for_each_entry(insn, &file->call_list, call_node) 968 idx++; 969 970 sec = elf_create_section_pair(file->elf, ".call_sites", 971 sizeof(unsigned int), idx, idx); 972 if (!sec) 973 return -1; 974 975 idx = 0; 976 list_for_each_entry(insn, &file->call_list, call_node) { 977 978 if (!elf_init_reloc_text_sym(file->elf, sec, 979 idx * sizeof(unsigned int), idx, 980 insn->sec, insn->offset)) 981 return -1; 982 983 idx++; 984 } 985 986 return 0; 987 } 988 989 #ifdef BUILD_KLP 990 static int create_sym_checksum_section(struct objtool_file *file) 991 { 992 struct section *sec; 993 struct symbol *sym; 994 unsigned int idx = 0; 995 struct sym_checksum *checksum; 996 size_t entsize = sizeof(struct sym_checksum); 997 998 sec = find_section_by_name(file->elf, ".discard.sym_checksum"); 999 if (sec) { 1000 if (!opts.dryrun) 1001 WARN("file already has .discard.sym_checksum section, skipping"); 1002 1003 return 0; 1004 } 1005 1006 for_each_sym(file->elf, sym) 1007 if (sym->csum.checksum) 1008 idx++; 1009 1010 if (!idx) 1011 return 0; 1012 1013 sec = elf_create_section_pair(file->elf, ".discard.sym_checksum", entsize, 1014 idx, idx); 1015 if (!sec) 1016 return -1; 1017 1018 idx = 0; 1019 for_each_sym(file->elf, sym) { 1020 if (!sym->csum.checksum) 1021 continue; 1022 1023 if (!elf_init_reloc(file->elf, sec->rsec, idx, idx * entsize, 1024 sym, 0, R_TEXT64)) 1025 return -1; 1026 1027 checksum = (struct sym_checksum *)sec->data->d_buf + idx; 1028 checksum->addr = 0; /* reloc */ 1029 checksum->checksum = sym->csum.checksum; 1030 1031 mark_sec_changed(file->elf, sec, true); 1032 1033 idx++; 1034 } 1035 1036 return 0; 1037 } 1038 #else 1039 static int create_sym_checksum_section(struct objtool_file *file) { return -EINVAL; } 1040 #endif 1041 1042 /* 1043 * Warnings shouldn't be reported for ignored functions. 1044 */ 1045 static int add_ignores(struct objtool_file *file) 1046 { 1047 struct section *rsec; 1048 struct symbol *func; 1049 struct reloc *reloc; 1050 1051 rsec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); 1052 if (!rsec) 1053 return 0; 1054 1055 for_each_reloc(rsec, reloc) { 1056 switch (reloc->sym->type) { 1057 case STT_FUNC: 1058 func = reloc->sym; 1059 break; 1060 1061 case STT_SECTION: 1062 func = find_func_by_offset(reloc->sym->sec, reloc_addend(reloc)); 1063 if (!func) 1064 continue; 1065 break; 1066 1067 default: 1068 ERROR("unexpected relocation symbol type in %s: %d", 1069 rsec->name, reloc->sym->type); 1070 return -1; 1071 } 1072 1073 func->ignore = true; 1074 if (func->cfunc) 1075 func->cfunc->ignore = true; 1076 } 1077 1078 return 0; 1079 } 1080 1081 /* 1082 * This is a whitelist of functions that is allowed to be called with AC set. 1083 * The list is meant to be minimal and only contains compiler instrumentation 1084 * ABI and a few functions used to implement *_{to,from}_user() functions. 1085 * 1086 * These functions must not directly change AC, but may PUSHF/POPF. 1087 */ 1088 static const char *uaccess_safe_builtin[] = { 1089 /* KASAN */ 1090 "kasan_report", 1091 "kasan_check_range", 1092 /* KASAN out-of-line */ 1093 "__asan_loadN_noabort", 1094 "__asan_load1_noabort", 1095 "__asan_load2_noabort", 1096 "__asan_load4_noabort", 1097 "__asan_load8_noabort", 1098 "__asan_load16_noabort", 1099 "__asan_storeN_noabort", 1100 "__asan_store1_noabort", 1101 "__asan_store2_noabort", 1102 "__asan_store4_noabort", 1103 "__asan_store8_noabort", 1104 "__asan_store16_noabort", 1105 "__kasan_check_read", 1106 "__kasan_check_write", 1107 /* KASAN in-line */ 1108 "__asan_report_load_n_noabort", 1109 "__asan_report_load1_noabort", 1110 "__asan_report_load2_noabort", 1111 "__asan_report_load4_noabort", 1112 "__asan_report_load8_noabort", 1113 "__asan_report_load16_noabort", 1114 "__asan_report_store_n_noabort", 1115 "__asan_report_store1_noabort", 1116 "__asan_report_store2_noabort", 1117 "__asan_report_store4_noabort", 1118 "__asan_report_store8_noabort", 1119 "__asan_report_store16_noabort", 1120 /* KCSAN */ 1121 "__kcsan_check_access", 1122 "__kcsan_mb", 1123 "__kcsan_wmb", 1124 "__kcsan_rmb", 1125 "__kcsan_release", 1126 "kcsan_found_watchpoint", 1127 "kcsan_setup_watchpoint", 1128 "kcsan_check_scoped_accesses", 1129 "kcsan_disable_current", 1130 "kcsan_enable_current_nowarn", 1131 /* KCSAN/TSAN */ 1132 "__tsan_func_entry", 1133 "__tsan_func_exit", 1134 "__tsan_read_range", 1135 "__tsan_write_range", 1136 "__tsan_read1", 1137 "__tsan_read2", 1138 "__tsan_read4", 1139 "__tsan_read8", 1140 "__tsan_read16", 1141 "__tsan_write1", 1142 "__tsan_write2", 1143 "__tsan_write4", 1144 "__tsan_write8", 1145 "__tsan_write16", 1146 "__tsan_read_write1", 1147 "__tsan_read_write2", 1148 "__tsan_read_write4", 1149 "__tsan_read_write8", 1150 "__tsan_read_write16", 1151 "__tsan_volatile_read1", 1152 "__tsan_volatile_read2", 1153 "__tsan_volatile_read4", 1154 "__tsan_volatile_read8", 1155 "__tsan_volatile_read16", 1156 "__tsan_volatile_write1", 1157 "__tsan_volatile_write2", 1158 "__tsan_volatile_write4", 1159 "__tsan_volatile_write8", 1160 "__tsan_volatile_write16", 1161 "__tsan_atomic8_load", 1162 "__tsan_atomic16_load", 1163 "__tsan_atomic32_load", 1164 "__tsan_atomic64_load", 1165 "__tsan_atomic8_store", 1166 "__tsan_atomic16_store", 1167 "__tsan_atomic32_store", 1168 "__tsan_atomic64_store", 1169 "__tsan_atomic8_exchange", 1170 "__tsan_atomic16_exchange", 1171 "__tsan_atomic32_exchange", 1172 "__tsan_atomic64_exchange", 1173 "__tsan_atomic8_fetch_add", 1174 "__tsan_atomic16_fetch_add", 1175 "__tsan_atomic32_fetch_add", 1176 "__tsan_atomic64_fetch_add", 1177 "__tsan_atomic8_fetch_sub", 1178 "__tsan_atomic16_fetch_sub", 1179 "__tsan_atomic32_fetch_sub", 1180 "__tsan_atomic64_fetch_sub", 1181 "__tsan_atomic8_fetch_and", 1182 "__tsan_atomic16_fetch_and", 1183 "__tsan_atomic32_fetch_and", 1184 "__tsan_atomic64_fetch_and", 1185 "__tsan_atomic8_fetch_or", 1186 "__tsan_atomic16_fetch_or", 1187 "__tsan_atomic32_fetch_or", 1188 "__tsan_atomic64_fetch_or", 1189 "__tsan_atomic8_fetch_xor", 1190 "__tsan_atomic16_fetch_xor", 1191 "__tsan_atomic32_fetch_xor", 1192 "__tsan_atomic64_fetch_xor", 1193 "__tsan_atomic8_fetch_nand", 1194 "__tsan_atomic16_fetch_nand", 1195 "__tsan_atomic32_fetch_nand", 1196 "__tsan_atomic64_fetch_nand", 1197 "__tsan_atomic8_compare_exchange_strong", 1198 "__tsan_atomic16_compare_exchange_strong", 1199 "__tsan_atomic32_compare_exchange_strong", 1200 "__tsan_atomic64_compare_exchange_strong", 1201 "__tsan_atomic8_compare_exchange_weak", 1202 "__tsan_atomic16_compare_exchange_weak", 1203 "__tsan_atomic32_compare_exchange_weak", 1204 "__tsan_atomic64_compare_exchange_weak", 1205 "__tsan_atomic8_compare_exchange_val", 1206 "__tsan_atomic16_compare_exchange_val", 1207 "__tsan_atomic32_compare_exchange_val", 1208 "__tsan_atomic64_compare_exchange_val", 1209 "__tsan_atomic_thread_fence", 1210 "__tsan_atomic_signal_fence", 1211 "__tsan_unaligned_read16", 1212 "__tsan_unaligned_write16", 1213 /* KCOV */ 1214 "write_comp_data", 1215 "check_kcov_mode", 1216 "__sanitizer_cov_trace_pc", 1217 "__sanitizer_cov_trace_const_cmp1", 1218 "__sanitizer_cov_trace_const_cmp2", 1219 "__sanitizer_cov_trace_const_cmp4", 1220 "__sanitizer_cov_trace_const_cmp8", 1221 "__sanitizer_cov_trace_cmp1", 1222 "__sanitizer_cov_trace_cmp2", 1223 "__sanitizer_cov_trace_cmp4", 1224 "__sanitizer_cov_trace_cmp8", 1225 "__sanitizer_cov_trace_switch", 1226 /* KMSAN */ 1227 "kmsan_copy_to_user", 1228 "kmsan_disable_current", 1229 "kmsan_enable_current", 1230 "kmsan_report", 1231 "kmsan_unpoison_entry_regs", 1232 "kmsan_unpoison_memory", 1233 "__msan_chain_origin", 1234 "__msan_get_context_state", 1235 "__msan_instrument_asm_store", 1236 "__msan_metadata_ptr_for_load_1", 1237 "__msan_metadata_ptr_for_load_2", 1238 "__msan_metadata_ptr_for_load_4", 1239 "__msan_metadata_ptr_for_load_8", 1240 "__msan_metadata_ptr_for_load_n", 1241 "__msan_metadata_ptr_for_store_1", 1242 "__msan_metadata_ptr_for_store_2", 1243 "__msan_metadata_ptr_for_store_4", 1244 "__msan_metadata_ptr_for_store_8", 1245 "__msan_metadata_ptr_for_store_n", 1246 "__msan_poison_alloca", 1247 "__msan_warning", 1248 /* UBSAN */ 1249 "ubsan_type_mismatch_common", 1250 "__ubsan_handle_type_mismatch", 1251 "__ubsan_handle_type_mismatch_v1", 1252 "__ubsan_handle_shift_out_of_bounds", 1253 "__ubsan_handle_load_invalid_value", 1254 /* KSTACK_ERASE */ 1255 "__sanitizer_cov_stack_depth", 1256 /* TRACE_BRANCH_PROFILING */ 1257 "ftrace_likely_update", 1258 /* STACKPROTECTOR */ 1259 "__stack_chk_fail", 1260 /* misc */ 1261 "csum_partial_copy_generic", 1262 "copy_mc_fragile", 1263 "copy_mc_fragile_handle_tail", 1264 "copy_mc_enhanced_fast_string", 1265 "rep_stos_alternative", 1266 "rep_movs_alternative", 1267 "__copy_user_nocache", 1268 NULL 1269 }; 1270 1271 static void add_uaccess_safe(struct objtool_file *file) 1272 { 1273 struct symbol *func; 1274 const char **name; 1275 1276 if (!opts.uaccess) 1277 return; 1278 1279 for (name = uaccess_safe_builtin; *name; name++) { 1280 func = find_symbol_by_name(file->elf, *name); 1281 if (!func) 1282 continue; 1283 1284 func->uaccess_safe = true; 1285 } 1286 } 1287 1288 /* 1289 * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol 1290 * will be added to the .retpoline_sites section. 1291 */ 1292 __weak bool arch_is_retpoline(struct symbol *sym) 1293 { 1294 return false; 1295 } 1296 1297 /* 1298 * Symbols that replace INSN_RETURN, every (tail) call to such a symbol 1299 * will be added to the .return_sites section. 1300 */ 1301 __weak bool arch_is_rethunk(struct symbol *sym) 1302 { 1303 return false; 1304 } 1305 1306 /* 1307 * Symbols that are embedded inside other instructions, because sometimes crazy 1308 * code exists. These are mostly ignored for validation purposes. 1309 */ 1310 __weak bool arch_is_embedded_insn(struct symbol *sym) 1311 { 1312 return false; 1313 } 1314 1315 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) 1316 { 1317 struct reloc *reloc; 1318 1319 if (insn->no_reloc) 1320 return NULL; 1321 1322 if (!file) 1323 return NULL; 1324 1325 reloc = find_reloc_by_dest_range(file->elf, insn->sec, 1326 insn->offset, insn->len); 1327 if (!reloc) { 1328 insn->no_reloc = 1; 1329 return NULL; 1330 } 1331 1332 return reloc; 1333 } 1334 1335 static void remove_insn_ops(struct instruction *insn) 1336 { 1337 struct stack_op *op, *next; 1338 1339 for (op = insn->stack_ops; op; op = next) { 1340 next = op->next; 1341 free(op); 1342 } 1343 insn->stack_ops = NULL; 1344 } 1345 1346 static int annotate_call_site(struct objtool_file *file, 1347 struct instruction *insn, bool sibling) 1348 { 1349 struct reloc *reloc = insn_reloc(file, insn); 1350 struct symbol *sym = insn_call_dest(insn); 1351 1352 if (!sym) 1353 sym = reloc->sym; 1354 1355 if (sym->static_call_tramp) { 1356 list_add_tail(&insn->call_node, &file->static_call_list); 1357 return 0; 1358 } 1359 1360 if (sym->retpoline_thunk) { 1361 list_add_tail(&insn->call_node, &file->retpoline_call_list); 1362 return 0; 1363 } 1364 1365 /* 1366 * Many compilers cannot disable KCOV or sanitizer calls with a function 1367 * attribute so they need a little help, NOP out any such calls from 1368 * noinstr text. 1369 */ 1370 if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) { 1371 if (reloc) 1372 set_reloc_type(file->elf, reloc, R_NONE); 1373 1374 if (elf_write_insn(file->elf, insn->sec, 1375 insn->offset, insn->len, 1376 sibling ? arch_ret_insn(insn->len) 1377 : arch_nop_insn(insn->len))) { 1378 return -1; 1379 } 1380 1381 insn->type = sibling ? INSN_RETURN : INSN_NOP; 1382 1383 if (sibling) { 1384 /* 1385 * We've replaced the tail-call JMP insn by two new 1386 * insn: RET; INT3, except we only have a single struct 1387 * insn here. Mark it retpoline_safe to avoid the SLS 1388 * warning, instead of adding another insn. 1389 */ 1390 insn->retpoline_safe = true; 1391 } 1392 1393 return 0; 1394 } 1395 1396 if (opts.mcount && sym->fentry) { 1397 if (sibling) 1398 WARN_INSN(insn, "tail call to __fentry__ !?!?"); 1399 if (opts.mnop) { 1400 if (reloc) 1401 set_reloc_type(file->elf, reloc, R_NONE); 1402 1403 if (elf_write_insn(file->elf, insn->sec, 1404 insn->offset, insn->len, 1405 arch_nop_insn(insn->len))) { 1406 return -1; 1407 } 1408 1409 insn->type = INSN_NOP; 1410 } 1411 1412 list_add_tail(&insn->call_node, &file->mcount_loc_list); 1413 return 0; 1414 } 1415 1416 if (insn->type == INSN_CALL && !insn->sec->init && 1417 !insn->_call_dest->embedded_insn) 1418 list_add_tail(&insn->call_node, &file->call_list); 1419 1420 if (!sibling && dead_end_function(file, sym)) 1421 insn->dead_end = true; 1422 1423 return 0; 1424 } 1425 1426 static int add_call_dest(struct objtool_file *file, struct instruction *insn, 1427 struct symbol *dest, bool sibling) 1428 { 1429 insn->_call_dest = dest; 1430 if (!dest) 1431 return 0; 1432 1433 /* 1434 * Whatever stack impact regular CALLs have, should be undone 1435 * by the RETURN of the called function. 1436 * 1437 * Annotated intra-function calls retain the stack_ops but 1438 * are converted to JUMP, see read_intra_function_calls(). 1439 */ 1440 remove_insn_ops(insn); 1441 1442 return annotate_call_site(file, insn, sibling); 1443 } 1444 1445 static int add_retpoline_call(struct objtool_file *file, struct instruction *insn) 1446 { 1447 /* 1448 * Retpoline calls/jumps are really dynamic calls/jumps in disguise, 1449 * so convert them accordingly. 1450 */ 1451 switch (insn->type) { 1452 case INSN_CALL: 1453 insn->type = INSN_CALL_DYNAMIC; 1454 break; 1455 case INSN_JUMP_UNCONDITIONAL: 1456 insn->type = INSN_JUMP_DYNAMIC; 1457 break; 1458 case INSN_JUMP_CONDITIONAL: 1459 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL; 1460 break; 1461 default: 1462 return 0; 1463 } 1464 1465 insn->retpoline_safe = true; 1466 1467 /* 1468 * Whatever stack impact regular CALLs have, should be undone 1469 * by the RETURN of the called function. 1470 * 1471 * Annotated intra-function calls retain the stack_ops but 1472 * are converted to JUMP, see read_intra_function_calls(). 1473 */ 1474 remove_insn_ops(insn); 1475 1476 return annotate_call_site(file, insn, false); 1477 } 1478 1479 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add) 1480 { 1481 /* 1482 * Return thunk tail calls are really just returns in disguise, 1483 * so convert them accordingly. 1484 */ 1485 insn->type = INSN_RETURN; 1486 insn->retpoline_safe = true; 1487 1488 if (add) 1489 list_add_tail(&insn->call_node, &file->return_thunk_list); 1490 } 1491 1492 static bool is_first_func_insn(struct objtool_file *file, 1493 struct instruction *insn) 1494 { 1495 struct symbol *func = insn_func(insn); 1496 1497 if (!func) 1498 return false; 1499 1500 if (insn->offset == func->offset) 1501 return true; 1502 1503 /* Allow direct CALL/JMP past ENDBR */ 1504 if (opts.ibt) { 1505 struct instruction *prev = prev_insn_same_sym(file, insn); 1506 1507 if (prev && prev->type == INSN_ENDBR && 1508 insn->offset == func->offset + prev->len) 1509 return true; 1510 } 1511 1512 return false; 1513 } 1514 1515 /* 1516 * Find the destination instructions for all jumps. 1517 */ 1518 static int add_jump_destinations(struct objtool_file *file) 1519 { 1520 struct instruction *insn; 1521 struct reloc *reloc; 1522 1523 for_each_insn(file, insn) { 1524 struct symbol *func = insn_func(insn); 1525 struct instruction *dest_insn; 1526 struct section *dest_sec; 1527 struct symbol *dest_sym; 1528 unsigned long dest_off; 1529 1530 if (!is_static_jump(insn)) 1531 continue; 1532 1533 if (insn->jump_dest) { 1534 /* 1535 * handle_group_alt() may have previously set 1536 * 'jump_dest' for some alternatives. 1537 */ 1538 continue; 1539 } 1540 1541 reloc = insn_reloc(file, insn); 1542 if (!reloc) { 1543 dest_sec = insn->sec; 1544 dest_off = arch_jump_destination(insn); 1545 dest_sym = dest_sec->sym; 1546 } else { 1547 dest_sym = reloc->sym; 1548 if (is_undef_sym(dest_sym)) { 1549 if (dest_sym->retpoline_thunk) { 1550 if (add_retpoline_call(file, insn)) 1551 return -1; 1552 continue; 1553 } 1554 1555 if (dest_sym->return_thunk) { 1556 add_return_call(file, insn, true); 1557 continue; 1558 } 1559 1560 /* External symbol */ 1561 if (func) { 1562 /* External sibling call */ 1563 if (add_call_dest(file, insn, dest_sym, true)) 1564 return -1; 1565 continue; 1566 } 1567 1568 /* Non-func asm code jumping to external symbol */ 1569 continue; 1570 } 1571 1572 dest_sec = dest_sym->sec; 1573 dest_off = dest_sym->offset + arch_insn_adjusted_addend(insn, reloc); 1574 } 1575 1576 dest_insn = find_insn(file, dest_sec, dest_off); 1577 if (!dest_insn) { 1578 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off); 1579 1580 /* 1581 * retbleed_untrain_ret() jumps to 1582 * __x86_return_thunk(), but objtool can't find 1583 * the thunk's starting RET instruction, 1584 * because the RET is also in the middle of 1585 * another instruction. Objtool only knows 1586 * about the outer instruction. 1587 */ 1588 if (sym && sym->embedded_insn) { 1589 add_return_call(file, insn, false); 1590 continue; 1591 } 1592 1593 /* 1594 * GCOV/KCOV dead code can jump to the end of 1595 * the function/section. 1596 */ 1597 if (file->ignore_unreachables && func && 1598 dest_sec == insn->sec && 1599 dest_off == func->offset + func->len) 1600 continue; 1601 1602 ERROR_INSN(insn, "can't find jump dest instruction at %s", 1603 offstr(dest_sec, dest_off)); 1604 return -1; 1605 } 1606 1607 if (!dest_sym || is_sec_sym(dest_sym)) { 1608 dest_sym = dest_insn->sym; 1609 if (!dest_sym) 1610 goto set_jump_dest; 1611 } 1612 1613 if (dest_sym->retpoline_thunk && dest_insn->offset == dest_sym->offset) { 1614 if (add_retpoline_call(file, insn)) 1615 return -1; 1616 continue; 1617 } 1618 1619 if (dest_sym->return_thunk && dest_insn->offset == dest_sym->offset) { 1620 add_return_call(file, insn, true); 1621 continue; 1622 } 1623 1624 if (!insn->sym || insn->sym->pfunc == dest_sym->pfunc) 1625 goto set_jump_dest; 1626 1627 /* 1628 * Internal cross-function jump. 1629 */ 1630 1631 if (is_first_func_insn(file, dest_insn)) { 1632 /* Internal sibling call */ 1633 if (add_call_dest(file, insn, dest_sym, true)) 1634 return -1; 1635 continue; 1636 } 1637 1638 set_jump_dest: 1639 insn->jump_dest = dest_insn; 1640 } 1641 1642 return 0; 1643 } 1644 1645 static struct symbol *find_call_destination(struct section *sec, unsigned long offset) 1646 { 1647 struct symbol *call_dest; 1648 1649 call_dest = find_func_by_offset(sec, offset); 1650 if (!call_dest) 1651 call_dest = find_symbol_by_offset(sec, offset); 1652 1653 return call_dest; 1654 } 1655 1656 /* 1657 * Find the destination instructions for all calls. 1658 */ 1659 static int add_call_destinations(struct objtool_file *file) 1660 { 1661 struct instruction *insn; 1662 unsigned long dest_off; 1663 struct symbol *dest; 1664 struct reloc *reloc; 1665 1666 for_each_insn(file, insn) { 1667 struct symbol *func = insn_func(insn); 1668 if (insn->type != INSN_CALL) 1669 continue; 1670 1671 reloc = insn_reloc(file, insn); 1672 if (!reloc) { 1673 dest_off = arch_jump_destination(insn); 1674 dest = find_call_destination(insn->sec, dest_off); 1675 1676 if (add_call_dest(file, insn, dest, false)) 1677 return -1; 1678 1679 if (func && func->ignore) 1680 continue; 1681 1682 if (!insn_call_dest(insn)) { 1683 ERROR_INSN(insn, "unannotated intra-function call"); 1684 return -1; 1685 } 1686 1687 if (func && !is_func_sym(insn_call_dest(insn))) { 1688 ERROR_INSN(insn, "unsupported call to non-function"); 1689 return -1; 1690 } 1691 1692 } else if (is_sec_sym(reloc->sym)) { 1693 dest_off = arch_insn_adjusted_addend(insn, reloc); 1694 dest = find_call_destination(reloc->sym->sec, dest_off); 1695 if (!dest) { 1696 ERROR_INSN(insn, "can't find call dest symbol at %s+0x%lx", 1697 reloc->sym->sec->name, dest_off); 1698 return -1; 1699 } 1700 1701 if (add_call_dest(file, insn, dest, false)) 1702 return -1; 1703 1704 } else if (reloc->sym->retpoline_thunk) { 1705 if (add_retpoline_call(file, insn)) 1706 return -1; 1707 1708 } else { 1709 if (add_call_dest(file, insn, reloc->sym, false)) 1710 return -1; 1711 } 1712 } 1713 1714 return 0; 1715 } 1716 1717 /* 1718 * The .alternatives section requires some extra special care over and above 1719 * other special sections because alternatives are patched in place. 1720 */ 1721 static int handle_group_alt(struct objtool_file *file, 1722 struct special_alt *special_alt, 1723 struct instruction *orig_insn, 1724 struct instruction **new_insn) 1725 { 1726 struct instruction *last_new_insn = NULL, *insn, *nop = NULL; 1727 struct alt_group *orig_alt_group, *new_alt_group; 1728 unsigned long dest_off; 1729 1730 orig_alt_group = orig_insn->alt_group; 1731 if (!orig_alt_group) { 1732 struct instruction *last_orig_insn = NULL; 1733 1734 orig_alt_group = calloc(1, sizeof(*orig_alt_group)); 1735 if (!orig_alt_group) { 1736 ERROR_GLIBC("calloc"); 1737 return -1; 1738 } 1739 orig_alt_group->cfi = calloc(special_alt->orig_len, 1740 sizeof(struct cfi_state *)); 1741 if (!orig_alt_group->cfi) { 1742 ERROR_GLIBC("calloc"); 1743 return -1; 1744 } 1745 1746 insn = orig_insn; 1747 sec_for_each_insn_from(file, insn) { 1748 if (insn->offset >= special_alt->orig_off + special_alt->orig_len) 1749 break; 1750 1751 insn->alt_group = orig_alt_group; 1752 last_orig_insn = insn; 1753 } 1754 orig_alt_group->orig_group = NULL; 1755 orig_alt_group->first_insn = orig_insn; 1756 orig_alt_group->last_insn = last_orig_insn; 1757 orig_alt_group->nop = NULL; 1758 orig_alt_group->ignore = orig_insn->ignore_alts; 1759 } else { 1760 if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len - 1761 orig_alt_group->first_insn->offset != special_alt->orig_len) { 1762 ERROR_INSN(orig_insn, "weirdly overlapping alternative! %ld != %d", 1763 orig_alt_group->last_insn->offset + 1764 orig_alt_group->last_insn->len - 1765 orig_alt_group->first_insn->offset, 1766 special_alt->orig_len); 1767 return -1; 1768 } 1769 } 1770 1771 new_alt_group = calloc(1, sizeof(*new_alt_group)); 1772 if (!new_alt_group) { 1773 ERROR_GLIBC("calloc"); 1774 return -1; 1775 } 1776 1777 if (special_alt->new_len < special_alt->orig_len) { 1778 /* 1779 * Insert a fake nop at the end to make the replacement 1780 * alt_group the same size as the original. This is needed to 1781 * allow propagate_alt_cfi() to do its magic. When the last 1782 * instruction affects the stack, the instruction after it (the 1783 * nop) will propagate the new state to the shared CFI array. 1784 */ 1785 nop = calloc(1, sizeof(*nop)); 1786 if (!nop) { 1787 ERROR_GLIBC("calloc"); 1788 return -1; 1789 } 1790 memset(nop, 0, sizeof(*nop)); 1791 1792 nop->sec = special_alt->new_sec; 1793 nop->offset = special_alt->new_off + special_alt->new_len; 1794 nop->len = special_alt->orig_len - special_alt->new_len; 1795 nop->type = INSN_NOP; 1796 nop->sym = orig_insn->sym; 1797 nop->alt_group = new_alt_group; 1798 nop->fake = 1; 1799 } 1800 1801 if (!special_alt->new_len) { 1802 *new_insn = nop; 1803 goto end; 1804 } 1805 1806 insn = *new_insn; 1807 sec_for_each_insn_from(file, insn) { 1808 struct reloc *alt_reloc; 1809 1810 if (insn->offset >= special_alt->new_off + special_alt->new_len) 1811 break; 1812 1813 last_new_insn = insn; 1814 1815 insn->sym = orig_insn->sym; 1816 insn->alt_group = new_alt_group; 1817 1818 /* 1819 * Since alternative replacement code is copy/pasted by the 1820 * kernel after applying relocations, generally such code can't 1821 * have relative-address relocation references to outside the 1822 * .altinstr_replacement section, unless the arch's 1823 * alternatives code can adjust the relative offsets 1824 * accordingly. 1825 */ 1826 alt_reloc = insn_reloc(file, insn); 1827 if (alt_reloc && arch_pc_relative_reloc(alt_reloc) && 1828 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) { 1829 1830 ERROR_INSN(insn, "unsupported relocation in alternatives section"); 1831 return -1; 1832 } 1833 1834 if (!is_static_jump(insn)) 1835 continue; 1836 1837 if (!insn->immediate) 1838 continue; 1839 1840 dest_off = arch_jump_destination(insn); 1841 if (dest_off == special_alt->new_off + special_alt->new_len) { 1842 insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn); 1843 if (!insn->jump_dest) { 1844 ERROR_INSN(insn, "can't find alternative jump destination"); 1845 return -1; 1846 } 1847 } 1848 } 1849 1850 if (!last_new_insn) { 1851 ERROR_FUNC(special_alt->new_sec, special_alt->new_off, 1852 "can't find last new alternative instruction"); 1853 return -1; 1854 } 1855 1856 end: 1857 new_alt_group->orig_group = orig_alt_group; 1858 new_alt_group->first_insn = *new_insn; 1859 new_alt_group->last_insn = last_new_insn; 1860 new_alt_group->nop = nop; 1861 new_alt_group->ignore = (*new_insn)->ignore_alts; 1862 new_alt_group->cfi = orig_alt_group->cfi; 1863 return 0; 1864 } 1865 1866 /* 1867 * A jump table entry can either convert a nop to a jump or a jump to a nop. 1868 * If the original instruction is a jump, make the alt entry an effective nop 1869 * by just skipping the original instruction. 1870 */ 1871 static int handle_jump_alt(struct objtool_file *file, 1872 struct special_alt *special_alt, 1873 struct instruction *orig_insn, 1874 struct instruction **new_insn) 1875 { 1876 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL && 1877 orig_insn->type != INSN_NOP) { 1878 1879 ERROR_INSN(orig_insn, "unsupported instruction at jump label"); 1880 return -1; 1881 } 1882 1883 if (opts.hack_jump_label && special_alt->key_addend & 2) { 1884 struct reloc *reloc = insn_reloc(file, orig_insn); 1885 1886 if (reloc) 1887 set_reloc_type(file->elf, reloc, R_NONE); 1888 1889 if (elf_write_insn(file->elf, orig_insn->sec, 1890 orig_insn->offset, orig_insn->len, 1891 arch_nop_insn(orig_insn->len))) { 1892 return -1; 1893 } 1894 1895 orig_insn->type = INSN_NOP; 1896 } 1897 1898 if (orig_insn->type == INSN_NOP) { 1899 if (orig_insn->len == 2) 1900 file->jl_nop_short++; 1901 else 1902 file->jl_nop_long++; 1903 1904 return 0; 1905 } 1906 1907 if (orig_insn->len == 2) 1908 file->jl_short++; 1909 else 1910 file->jl_long++; 1911 1912 *new_insn = next_insn_same_sec(file, orig_insn); 1913 return 0; 1914 } 1915 1916 /* 1917 * Read all the special sections which have alternate instructions which can be 1918 * patched in or redirected to at runtime. Each instruction having alternate 1919 * instruction(s) has them added to its insn->alts list, which will be 1920 * traversed in validate_branch(). 1921 */ 1922 static int add_special_section_alts(struct objtool_file *file) 1923 { 1924 struct list_head special_alts; 1925 struct instruction *orig_insn, *new_insn; 1926 struct special_alt *special_alt, *tmp; 1927 struct alternative *alt; 1928 1929 if (special_get_alts(file->elf, &special_alts)) 1930 return -1; 1931 1932 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { 1933 1934 orig_insn = find_insn(file, special_alt->orig_sec, 1935 special_alt->orig_off); 1936 if (!orig_insn) { 1937 ERROR_FUNC(special_alt->orig_sec, special_alt->orig_off, 1938 "special: can't find orig instruction"); 1939 return -1; 1940 } 1941 1942 new_insn = NULL; 1943 if (!special_alt->group || special_alt->new_len) { 1944 new_insn = find_insn(file, special_alt->new_sec, 1945 special_alt->new_off); 1946 if (!new_insn) { 1947 ERROR_FUNC(special_alt->new_sec, special_alt->new_off, 1948 "special: can't find new instruction"); 1949 return -1; 1950 } 1951 } 1952 1953 if (special_alt->group) { 1954 if (!special_alt->orig_len) { 1955 ERROR_INSN(orig_insn, "empty alternative entry"); 1956 continue; 1957 } 1958 1959 if (handle_group_alt(file, special_alt, orig_insn, &new_insn)) 1960 return -1; 1961 1962 } else if (special_alt->jump_or_nop) { 1963 if (handle_jump_alt(file, special_alt, orig_insn, &new_insn)) 1964 return -1; 1965 } 1966 1967 alt = calloc(1, sizeof(*alt)); 1968 if (!alt) { 1969 ERROR_GLIBC("calloc"); 1970 return -1; 1971 } 1972 1973 alt->insn = new_insn; 1974 alt->next = orig_insn->alts; 1975 orig_insn->alts = alt; 1976 1977 list_del(&special_alt->list); 1978 free(special_alt); 1979 } 1980 1981 if (opts.stats) { 1982 printf("jl\\\tNOP\tJMP\n"); 1983 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short); 1984 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long); 1985 } 1986 1987 return 0; 1988 } 1989 1990 __weak unsigned long arch_jump_table_sym_offset(struct reloc *reloc, struct reloc *table) 1991 { 1992 return reloc->sym->offset + reloc_addend(reloc); 1993 } 1994 1995 static int add_jump_table(struct objtool_file *file, struct instruction *insn) 1996 { 1997 unsigned long table_size = insn_jump_table_size(insn); 1998 struct symbol *pfunc = insn_func(insn)->pfunc; 1999 struct reloc *table = insn_jump_table(insn); 2000 struct instruction *dest_insn; 2001 unsigned int prev_offset = 0; 2002 struct reloc *reloc = table; 2003 struct alternative *alt; 2004 unsigned long sym_offset; 2005 2006 /* 2007 * Each @reloc is a switch table relocation which points to the target 2008 * instruction. 2009 */ 2010 for_each_reloc_from(table->sec, reloc) { 2011 2012 /* Check for the end of the table: */ 2013 if (table_size && reloc_offset(reloc) - reloc_offset(table) >= table_size) 2014 break; 2015 if (reloc != table && is_jump_table(reloc)) 2016 break; 2017 2018 /* Make sure the table entries are consecutive: */ 2019 if (prev_offset && reloc_offset(reloc) != prev_offset + arch_reloc_size(reloc)) 2020 break; 2021 2022 sym_offset = arch_jump_table_sym_offset(reloc, table); 2023 2024 /* Detect function pointers from contiguous objects: */ 2025 if (reloc->sym->sec == pfunc->sec && sym_offset == pfunc->offset) 2026 break; 2027 2028 /* 2029 * Clang sometimes leaves dangling unused jump table entries 2030 * which point to the end of the function. Ignore them. 2031 */ 2032 if (reloc->sym->sec == pfunc->sec && 2033 sym_offset == pfunc->offset + pfunc->len) 2034 goto next; 2035 2036 dest_insn = find_insn(file, reloc->sym->sec, sym_offset); 2037 if (!dest_insn) 2038 break; 2039 2040 /* Make sure the destination is in the same function: */ 2041 if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc) 2042 break; 2043 2044 alt = calloc(1, sizeof(*alt)); 2045 if (!alt) { 2046 ERROR_GLIBC("calloc"); 2047 return -1; 2048 } 2049 2050 alt->insn = dest_insn; 2051 alt->next = insn->alts; 2052 insn->alts = alt; 2053 next: 2054 prev_offset = reloc_offset(reloc); 2055 } 2056 2057 if (!prev_offset) { 2058 ERROR_INSN(insn, "can't find switch jump table"); 2059 return -1; 2060 } 2061 2062 return 0; 2063 } 2064 2065 /* 2066 * find_jump_table() - Given a dynamic jump, find the switch jump table 2067 * associated with it. 2068 */ 2069 static void find_jump_table(struct objtool_file *file, struct symbol *func, 2070 struct instruction *insn) 2071 { 2072 struct reloc *table_reloc; 2073 struct instruction *dest_insn, *orig_insn = insn; 2074 unsigned long table_size; 2075 unsigned long sym_offset; 2076 2077 /* 2078 * Backward search using the @first_jump_src links, these help avoid 2079 * much of the 'in between' code. Which avoids us getting confused by 2080 * it. 2081 */ 2082 for (; 2083 insn && insn_func(insn) && insn_func(insn)->pfunc == func; 2084 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) { 2085 2086 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC) 2087 break; 2088 2089 /* allow small jumps within the range */ 2090 if (insn->type == INSN_JUMP_UNCONDITIONAL && 2091 insn->jump_dest && 2092 (insn->jump_dest->offset <= insn->offset || 2093 insn->jump_dest->offset > orig_insn->offset)) 2094 break; 2095 2096 table_reloc = arch_find_switch_table(file, insn, &table_size); 2097 if (!table_reloc) 2098 continue; 2099 2100 sym_offset = table_reloc->sym->offset + reloc_addend(table_reloc); 2101 2102 dest_insn = find_insn(file, table_reloc->sym->sec, sym_offset); 2103 if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) 2104 continue; 2105 2106 set_jump_table(table_reloc); 2107 orig_insn->_jump_table = table_reloc; 2108 orig_insn->_jump_table_size = table_size; 2109 2110 break; 2111 } 2112 } 2113 2114 /* 2115 * First pass: Mark the head of each jump table so that in the next pass, 2116 * we know when a given jump table ends and the next one starts. 2117 */ 2118 static void mark_func_jump_tables(struct objtool_file *file, 2119 struct symbol *func) 2120 { 2121 struct instruction *insn, *last = NULL; 2122 2123 func_for_each_insn(file, func, insn) { 2124 if (!last) 2125 last = insn; 2126 2127 /* 2128 * Store back-pointers for unconditional forward jumps such 2129 * that find_jump_table() can back-track using those and 2130 * avoid some potentially confusing code. 2131 */ 2132 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest && 2133 insn->offset > last->offset && 2134 insn->jump_dest->offset > insn->offset && 2135 !insn->jump_dest->first_jump_src) { 2136 2137 insn->jump_dest->first_jump_src = insn; 2138 last = insn->jump_dest; 2139 } 2140 2141 if (insn->type != INSN_JUMP_DYNAMIC) 2142 continue; 2143 2144 find_jump_table(file, func, insn); 2145 } 2146 } 2147 2148 static int add_func_jump_tables(struct objtool_file *file, 2149 struct symbol *func) 2150 { 2151 struct instruction *insn; 2152 2153 func_for_each_insn(file, func, insn) { 2154 if (!insn_jump_table(insn)) 2155 continue; 2156 2157 if (add_jump_table(file, insn)) 2158 return -1; 2159 } 2160 2161 return 0; 2162 } 2163 2164 /* 2165 * For some switch statements, gcc generates a jump table in the .rodata 2166 * section which contains a list of addresses within the function to jump to. 2167 * This finds these jump tables and adds them to the insn->alts lists. 2168 */ 2169 static int add_jump_table_alts(struct objtool_file *file) 2170 { 2171 struct symbol *func; 2172 2173 if (!file->rodata) 2174 return 0; 2175 2176 for_each_sym(file->elf, func) { 2177 if (!is_func_sym(func) || func->alias != func) 2178 continue; 2179 2180 mark_func_jump_tables(file, func); 2181 if (add_func_jump_tables(file, func)) 2182 return -1; 2183 } 2184 2185 return 0; 2186 } 2187 2188 static void set_func_state(struct cfi_state *state) 2189 { 2190 state->cfa = initial_func_cfi.cfa; 2191 memcpy(&state->regs, &initial_func_cfi.regs, 2192 CFI_NUM_REGS * sizeof(struct cfi_reg)); 2193 state->stack_size = initial_func_cfi.cfa.offset; 2194 state->type = UNWIND_HINT_TYPE_CALL; 2195 } 2196 2197 static int read_unwind_hints(struct objtool_file *file) 2198 { 2199 struct cfi_state cfi = init_cfi; 2200 struct section *sec; 2201 struct unwind_hint *hint; 2202 struct instruction *insn; 2203 struct reloc *reloc; 2204 unsigned long offset; 2205 int i; 2206 2207 sec = find_section_by_name(file->elf, ".discard.unwind_hints"); 2208 if (!sec) 2209 return 0; 2210 2211 if (!sec->rsec) { 2212 ERROR("missing .rela.discard.unwind_hints section"); 2213 return -1; 2214 } 2215 2216 if (sec_size(sec) % sizeof(struct unwind_hint)) { 2217 ERROR("struct unwind_hint size mismatch"); 2218 return -1; 2219 } 2220 2221 file->hints = true; 2222 2223 for (i = 0; i < sec_size(sec) / sizeof(struct unwind_hint); i++) { 2224 hint = (struct unwind_hint *)sec->data->d_buf + i; 2225 2226 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint)); 2227 if (!reloc) { 2228 ERROR("can't find reloc for unwind_hints[%d]", i); 2229 return -1; 2230 } 2231 2232 offset = reloc->sym->offset + reloc_addend(reloc); 2233 2234 insn = find_insn(file, reloc->sym->sec, offset); 2235 if (!insn) { 2236 ERROR("can't find insn for unwind_hints[%d]", i); 2237 return -1; 2238 } 2239 2240 insn->hint = true; 2241 2242 if (hint->type == UNWIND_HINT_TYPE_UNDEFINED) { 2243 insn->cfi = &force_undefined_cfi; 2244 continue; 2245 } 2246 2247 if (hint->type == UNWIND_HINT_TYPE_SAVE) { 2248 insn->hint = false; 2249 insn->save = true; 2250 continue; 2251 } 2252 2253 if (hint->type == UNWIND_HINT_TYPE_RESTORE) { 2254 insn->restore = true; 2255 continue; 2256 } 2257 2258 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) { 2259 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset); 2260 2261 if (sym && is_global_sym(sym)) { 2262 if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) { 2263 ERROR_INSN(insn, "UNWIND_HINT_IRET_REGS without ENDBR"); 2264 return -1; 2265 } 2266 } 2267 } 2268 2269 if (hint->type == UNWIND_HINT_TYPE_FUNC) { 2270 insn->cfi = &func_cfi; 2271 continue; 2272 } 2273 2274 if (insn->cfi) 2275 cfi = *(insn->cfi); 2276 2277 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) { 2278 ERROR_INSN(insn, "unsupported unwind_hint sp base reg %d", hint->sp_reg); 2279 return -1; 2280 } 2281 2282 cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset); 2283 cfi.type = hint->type; 2284 cfi.signal = hint->signal; 2285 2286 insn->cfi = cfi_hash_find_or_add(&cfi); 2287 } 2288 2289 return 0; 2290 } 2291 2292 static int read_annotate(struct objtool_file *file, 2293 int (*func)(struct objtool_file *file, int type, struct instruction *insn)) 2294 { 2295 struct section *sec; 2296 struct instruction *insn; 2297 struct reloc *reloc; 2298 uint64_t offset; 2299 int type; 2300 2301 sec = find_section_by_name(file->elf, ".discard.annotate_insn"); 2302 if (!sec) 2303 return 0; 2304 2305 if (!sec->rsec) 2306 return 0; 2307 2308 if (sec->sh.sh_entsize != 8) { 2309 static bool warned = false; 2310 if (!warned && opts.verbose) { 2311 WARN("%s: dodgy linker, sh_entsize != 8", sec->name); 2312 warned = true; 2313 } 2314 sec->sh.sh_entsize = 8; 2315 } 2316 2317 if (sec_num_entries(sec) != sec_num_entries(sec->rsec)) { 2318 ERROR("bad .discard.annotate_insn section: missing relocs"); 2319 return -1; 2320 } 2321 2322 for_each_reloc(sec->rsec, reloc) { 2323 type = annotype(file->elf, sec, reloc); 2324 offset = reloc->sym->offset + reloc_addend(reloc); 2325 insn = find_insn(file, reloc->sym->sec, offset); 2326 2327 if (!insn) { 2328 ERROR("bad .discard.annotate_insn entry: %d of type %d", reloc_idx(reloc), type); 2329 return -1; 2330 } 2331 2332 if (func(file, type, insn)) 2333 return -1; 2334 } 2335 2336 return 0; 2337 } 2338 2339 static int __annotate_early(struct objtool_file *file, int type, struct instruction *insn) 2340 { 2341 switch (type) { 2342 2343 /* Must be before add_special_section_alts() */ 2344 case ANNOTYPE_IGNORE_ALTS: 2345 insn->ignore_alts = true; 2346 break; 2347 2348 /* 2349 * Must be before read_unwind_hints() since that needs insn->noendbr. 2350 */ 2351 case ANNOTYPE_NOENDBR: 2352 insn->noendbr = 1; 2353 break; 2354 2355 default: 2356 break; 2357 } 2358 2359 return 0; 2360 } 2361 2362 static int __annotate_ifc(struct objtool_file *file, int type, struct instruction *insn) 2363 { 2364 unsigned long dest_off; 2365 2366 if (type != ANNOTYPE_INTRA_FUNCTION_CALL) 2367 return 0; 2368 2369 if (insn->type != INSN_CALL) { 2370 ERROR_INSN(insn, "intra_function_call not a direct call"); 2371 return -1; 2372 } 2373 2374 /* 2375 * Treat intra-function CALLs as JMPs, but with a stack_op. 2376 * See add_call_destinations(), which strips stack_ops from 2377 * normal CALLs. 2378 */ 2379 insn->type = INSN_JUMP_UNCONDITIONAL; 2380 2381 dest_off = arch_jump_destination(insn); 2382 insn->jump_dest = find_insn(file, insn->sec, dest_off); 2383 if (!insn->jump_dest) { 2384 ERROR_INSN(insn, "can't find call dest at %s+0x%lx", 2385 insn->sec->name, dest_off); 2386 return -1; 2387 } 2388 2389 return 0; 2390 } 2391 2392 static int __annotate_late(struct objtool_file *file, int type, struct instruction *insn) 2393 { 2394 struct symbol *sym; 2395 2396 switch (type) { 2397 case ANNOTYPE_NOENDBR: 2398 /* early */ 2399 break; 2400 2401 case ANNOTYPE_RETPOLINE_SAFE: 2402 if (insn->type != INSN_JUMP_DYNAMIC && 2403 insn->type != INSN_CALL_DYNAMIC && 2404 insn->type != INSN_RETURN && 2405 insn->type != INSN_NOP) { 2406 ERROR_INSN(insn, "retpoline_safe hint not an indirect jump/call/ret/nop"); 2407 return -1; 2408 } 2409 2410 insn->retpoline_safe = true; 2411 break; 2412 2413 case ANNOTYPE_INSTR_BEGIN: 2414 insn->instr++; 2415 break; 2416 2417 case ANNOTYPE_INSTR_END: 2418 insn->instr--; 2419 break; 2420 2421 case ANNOTYPE_UNRET_BEGIN: 2422 insn->unret = 1; 2423 break; 2424 2425 case ANNOTYPE_IGNORE_ALTS: 2426 /* early */ 2427 break; 2428 2429 case ANNOTYPE_INTRA_FUNCTION_CALL: 2430 /* ifc */ 2431 break; 2432 2433 case ANNOTYPE_REACHABLE: 2434 insn->dead_end = false; 2435 break; 2436 2437 case ANNOTYPE_NOCFI: 2438 sym = insn->sym; 2439 if (!sym) { 2440 ERROR_INSN(insn, "dodgy NOCFI annotation"); 2441 return -1; 2442 } 2443 insn->sym->nocfi = 1; 2444 break; 2445 2446 default: 2447 ERROR_INSN(insn, "Unknown annotation type: %d", type); 2448 return -1; 2449 } 2450 2451 return 0; 2452 } 2453 2454 /* 2455 * Return true if name matches an instrumentation function, where calls to that 2456 * function from noinstr code can safely be removed, but compilers won't do so. 2457 */ 2458 static bool is_profiling_func(const char *name) 2459 { 2460 /* 2461 * Many compilers cannot disable KCOV with a function attribute. 2462 */ 2463 if (!strncmp(name, "__sanitizer_cov_", 16)) 2464 return true; 2465 2466 return false; 2467 } 2468 2469 static int classify_symbols(struct objtool_file *file) 2470 { 2471 struct symbol *func; 2472 size_t len; 2473 2474 for_each_sym(file->elf, func) { 2475 if (is_notype_sym(func) && strstarts(func->name, ".L")) 2476 func->local_label = true; 2477 2478 if (!is_global_sym(func)) 2479 continue; 2480 2481 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR, 2482 strlen(STATIC_CALL_TRAMP_PREFIX_STR))) 2483 func->static_call_tramp = true; 2484 2485 if (arch_is_retpoline(func)) 2486 func->retpoline_thunk = true; 2487 2488 if (arch_is_rethunk(func)) 2489 func->return_thunk = true; 2490 2491 if (arch_is_embedded_insn(func)) 2492 func->embedded_insn = true; 2493 2494 if (arch_ftrace_match(func->name)) 2495 func->fentry = true; 2496 2497 if (is_profiling_func(func->name)) 2498 func->profiling_func = true; 2499 2500 len = strlen(func->name); 2501 if (len > sym_name_max_len) 2502 sym_name_max_len = len; 2503 } 2504 2505 return 0; 2506 } 2507 2508 static void mark_rodata(struct objtool_file *file) 2509 { 2510 struct section *sec; 2511 bool found = false; 2512 2513 /* 2514 * Search for the following rodata sections, each of which can 2515 * potentially contain jump tables: 2516 * 2517 * - .rodata: can contain GCC switch tables 2518 * - .rodata.<func>: same, if -fdata-sections is being used 2519 * - .data.rel.ro.c_jump_table: contains C annotated jump tables 2520 * 2521 * .rodata.str1.* sections are ignored; they don't contain jump tables. 2522 */ 2523 for_each_sec(file->elf, sec) { 2524 if ((!strncmp(sec->name, ".rodata", 7) && 2525 !strstr(sec->name, ".str1.")) || 2526 !strncmp(sec->name, ".data.rel.ro", 12)) { 2527 sec->rodata = true; 2528 found = true; 2529 } 2530 } 2531 2532 file->rodata = found; 2533 } 2534 2535 static void mark_holes(struct objtool_file *file) 2536 { 2537 struct instruction *insn; 2538 bool in_hole = false; 2539 2540 if (!opts.link) 2541 return; 2542 2543 /* 2544 * Whole archive runs might encounter dead code from weak symbols. 2545 * This is where the linker will have dropped the weak symbol in 2546 * favour of a regular symbol, but leaves the code in place. 2547 */ 2548 for_each_insn(file, insn) { 2549 if (insn->sym || !find_symbol_hole_containing(insn->sec, insn->offset)) { 2550 in_hole = false; 2551 continue; 2552 } 2553 2554 /* Skip function padding and pfx code */ 2555 if (!in_hole && insn->type == INSN_NOP) 2556 continue; 2557 2558 in_hole = true; 2559 insn->hole = 1; 2560 2561 /* 2562 * If this hole jumps to a .cold function, mark it ignore. 2563 */ 2564 if (insn->jump_dest) { 2565 struct symbol *dest_func = insn_func(insn->jump_dest); 2566 2567 if (dest_func && dest_func->cold) 2568 dest_func->ignore = true; 2569 } 2570 } 2571 } 2572 2573 static bool validate_branch_enabled(void) 2574 { 2575 return opts.stackval || 2576 opts.orc || 2577 opts.uaccess || 2578 opts.checksum; 2579 } 2580 2581 static int decode_sections(struct objtool_file *file) 2582 { 2583 file->klp = is_livepatch_module(file); 2584 2585 mark_rodata(file); 2586 2587 if (init_pv_ops(file)) 2588 return -1; 2589 2590 /* 2591 * Must be before add_{jump_call}_destination. 2592 */ 2593 if (classify_symbols(file)) 2594 return -1; 2595 2596 if (decode_instructions(file)) 2597 return -1; 2598 2599 if (add_ignores(file)) 2600 return -1; 2601 2602 add_uaccess_safe(file); 2603 2604 if (read_annotate(file, __annotate_early)) 2605 return -1; 2606 2607 /* 2608 * Must be before add_jump_destinations(), which depends on 'func' 2609 * being set for alternatives, to enable proper sibling call detection. 2610 */ 2611 if (validate_branch_enabled() || opts.noinstr || opts.hack_jump_label) { 2612 if (add_special_section_alts(file)) 2613 return -1; 2614 } 2615 2616 if (add_jump_destinations(file)) 2617 return -1; 2618 2619 /* 2620 * Must be before add_call_destination(); it changes INSN_CALL to 2621 * INSN_JUMP. 2622 */ 2623 if (read_annotate(file, __annotate_ifc)) 2624 return -1; 2625 2626 if (add_call_destinations(file)) 2627 return -1; 2628 2629 if (add_jump_table_alts(file)) 2630 return -1; 2631 2632 if (read_unwind_hints(file)) 2633 return -1; 2634 2635 /* Must be after add_jump_destinations() */ 2636 mark_holes(file); 2637 2638 /* 2639 * Must be after add_call_destinations() such that it can override 2640 * dead_end_function() marks. 2641 */ 2642 if (read_annotate(file, __annotate_late)) 2643 return -1; 2644 2645 return 0; 2646 } 2647 2648 static bool is_special_call(struct instruction *insn) 2649 { 2650 if (insn->type == INSN_CALL) { 2651 struct symbol *dest = insn_call_dest(insn); 2652 2653 if (!dest) 2654 return false; 2655 2656 if (dest->fentry || dest->embedded_insn) 2657 return true; 2658 } 2659 2660 return false; 2661 } 2662 2663 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state) 2664 { 2665 struct cfi_state *cfi = &state->cfi; 2666 int i; 2667 2668 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap) 2669 return true; 2670 2671 if (cfi->cfa.offset != initial_func_cfi.cfa.offset) 2672 return true; 2673 2674 if (cfi->stack_size != initial_func_cfi.cfa.offset) 2675 return true; 2676 2677 for (i = 0; i < CFI_NUM_REGS; i++) { 2678 if (cfi->regs[i].base != initial_func_cfi.regs[i].base || 2679 cfi->regs[i].offset != initial_func_cfi.regs[i].offset) 2680 return true; 2681 } 2682 2683 return false; 2684 } 2685 2686 static bool check_reg_frame_pos(const struct cfi_reg *reg, 2687 int expected_offset) 2688 { 2689 return reg->base == CFI_CFA && 2690 reg->offset == expected_offset; 2691 } 2692 2693 static bool has_valid_stack_frame(struct insn_state *state) 2694 { 2695 struct cfi_state *cfi = &state->cfi; 2696 2697 if (cfi->cfa.base == CFI_BP && 2698 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) && 2699 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8)) 2700 return true; 2701 2702 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP) 2703 return true; 2704 2705 return false; 2706 } 2707 2708 static int update_cfi_state_regs(struct instruction *insn, 2709 struct cfi_state *cfi, 2710 struct stack_op *op) 2711 { 2712 struct cfi_reg *cfa = &cfi->cfa; 2713 2714 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT) 2715 return 0; 2716 2717 /* push */ 2718 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF) 2719 cfa->offset += 8; 2720 2721 /* pop */ 2722 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF) 2723 cfa->offset -= 8; 2724 2725 /* add immediate to sp */ 2726 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD && 2727 op->dest.reg == CFI_SP && op->src.reg == CFI_SP) 2728 cfa->offset -= op->src.offset; 2729 2730 return 0; 2731 } 2732 2733 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset) 2734 { 2735 if (arch_callee_saved_reg(reg) && 2736 cfi->regs[reg].base == CFI_UNDEFINED) { 2737 cfi->regs[reg].base = base; 2738 cfi->regs[reg].offset = offset; 2739 } 2740 } 2741 2742 static void restore_reg(struct cfi_state *cfi, unsigned char reg) 2743 { 2744 cfi->regs[reg].base = initial_func_cfi.regs[reg].base; 2745 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset; 2746 } 2747 2748 /* 2749 * A note about DRAP stack alignment: 2750 * 2751 * GCC has the concept of a DRAP register, which is used to help keep track of 2752 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP 2753 * register. The typical DRAP pattern is: 2754 * 2755 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 2756 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp 2757 * 41 ff 72 f8 pushq -0x8(%r10) 2758 * 55 push %rbp 2759 * 48 89 e5 mov %rsp,%rbp 2760 * (more pushes) 2761 * 41 52 push %r10 2762 * ... 2763 * 41 5a pop %r10 2764 * (more pops) 2765 * 5d pop %rbp 2766 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2767 * c3 retq 2768 * 2769 * There are some variations in the epilogues, like: 2770 * 2771 * 5b pop %rbx 2772 * 41 5a pop %r10 2773 * 41 5c pop %r12 2774 * 41 5d pop %r13 2775 * 41 5e pop %r14 2776 * c9 leaveq 2777 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2778 * c3 retq 2779 * 2780 * and: 2781 * 2782 * 4c 8b 55 e8 mov -0x18(%rbp),%r10 2783 * 48 8b 5d e0 mov -0x20(%rbp),%rbx 2784 * 4c 8b 65 f0 mov -0x10(%rbp),%r12 2785 * 4c 8b 6d f8 mov -0x8(%rbp),%r13 2786 * c9 leaveq 2787 * 49 8d 62 f8 lea -0x8(%r10),%rsp 2788 * c3 retq 2789 * 2790 * Sometimes r13 is used as the DRAP register, in which case it's saved and 2791 * restored beforehand: 2792 * 2793 * 41 55 push %r13 2794 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 2795 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 2796 * ... 2797 * 49 8d 65 f0 lea -0x10(%r13),%rsp 2798 * 41 5d pop %r13 2799 * c3 retq 2800 */ 2801 static int update_cfi_state(struct instruction *insn, 2802 struct instruction *next_insn, 2803 struct cfi_state *cfi, struct stack_op *op) 2804 { 2805 struct cfi_reg *cfa = &cfi->cfa; 2806 struct cfi_reg *regs = cfi->regs; 2807 2808 /* ignore UNWIND_HINT_UNDEFINED regions */ 2809 if (cfi->force_undefined) 2810 return 0; 2811 2812 /* stack operations don't make sense with an undefined CFA */ 2813 if (cfa->base == CFI_UNDEFINED) { 2814 if (insn_func(insn)) { 2815 WARN_INSN(insn, "undefined stack state"); 2816 return 1; 2817 } 2818 return 0; 2819 } 2820 2821 if (cfi->type == UNWIND_HINT_TYPE_REGS || 2822 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL) 2823 return update_cfi_state_regs(insn, cfi, op); 2824 2825 switch (op->dest.type) { 2826 2827 case OP_DEST_REG: 2828 switch (op->src.type) { 2829 2830 case OP_SRC_REG: 2831 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP && 2832 cfa->base == CFI_SP && 2833 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) { 2834 2835 /* mov %rsp, %rbp */ 2836 cfa->base = op->dest.reg; 2837 cfi->bp_scratch = false; 2838 } 2839 2840 else if (op->src.reg == CFI_SP && 2841 op->dest.reg == CFI_BP && cfi->drap) { 2842 2843 /* drap: mov %rsp, %rbp */ 2844 regs[CFI_BP].base = CFI_BP; 2845 regs[CFI_BP].offset = -cfi->stack_size; 2846 cfi->bp_scratch = false; 2847 } 2848 2849 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { 2850 2851 /* 2852 * mov %rsp, %reg 2853 * 2854 * This is needed for the rare case where GCC 2855 * does: 2856 * 2857 * mov %rsp, %rax 2858 * ... 2859 * mov %rax, %rsp 2860 */ 2861 cfi->vals[op->dest.reg].base = CFI_CFA; 2862 cfi->vals[op->dest.reg].offset = -cfi->stack_size; 2863 } 2864 2865 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP && 2866 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) { 2867 2868 /* 2869 * mov %rbp, %rsp 2870 * 2871 * Restore the original stack pointer (Clang). 2872 */ 2873 cfi->stack_size = -cfi->regs[CFI_BP].offset; 2874 } 2875 2876 else if (op->dest.reg == cfa->base) { 2877 2878 /* mov %reg, %rsp */ 2879 if (cfa->base == CFI_SP && 2880 cfi->vals[op->src.reg].base == CFI_CFA) { 2881 2882 /* 2883 * This is needed for the rare case 2884 * where GCC does something dumb like: 2885 * 2886 * lea 0x8(%rsp), %rcx 2887 * ... 2888 * mov %rcx, %rsp 2889 */ 2890 cfa->offset = -cfi->vals[op->src.reg].offset; 2891 cfi->stack_size = cfa->offset; 2892 2893 } else if (cfa->base == CFI_SP && 2894 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && 2895 cfi->vals[op->src.reg].offset == cfa->offset) { 2896 2897 /* 2898 * Stack swizzle: 2899 * 2900 * 1: mov %rsp, (%[tos]) 2901 * 2: mov %[tos], %rsp 2902 * ... 2903 * 3: pop %rsp 2904 * 2905 * Where: 2906 * 2907 * 1 - places a pointer to the previous 2908 * stack at the Top-of-Stack of the 2909 * new stack. 2910 * 2911 * 2 - switches to the new stack. 2912 * 2913 * 3 - pops the Top-of-Stack to restore 2914 * the original stack. 2915 * 2916 * Note: we set base to SP_INDIRECT 2917 * here and preserve offset. Therefore 2918 * when the unwinder reaches ToS it 2919 * will dereference SP and then add the 2920 * offset to find the next frame, IOW: 2921 * (%rsp) + offset. 2922 */ 2923 cfa->base = CFI_SP_INDIRECT; 2924 2925 } else { 2926 cfa->base = CFI_UNDEFINED; 2927 cfa->offset = 0; 2928 } 2929 } 2930 2931 else if (op->dest.reg == CFI_SP && 2932 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT && 2933 cfi->vals[op->src.reg].offset == cfa->offset) { 2934 2935 /* 2936 * The same stack swizzle case 2) as above. But 2937 * because we can't change cfa->base, case 3) 2938 * will become a regular POP. Pretend we're a 2939 * PUSH so things don't go unbalanced. 2940 */ 2941 cfi->stack_size += 8; 2942 } 2943 2944 2945 break; 2946 2947 case OP_SRC_ADD: 2948 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { 2949 2950 /* add imm, %rsp */ 2951 cfi->stack_size -= op->src.offset; 2952 if (cfa->base == CFI_SP) 2953 cfa->offset -= op->src.offset; 2954 break; 2955 } 2956 2957 if (op->dest.reg == CFI_BP && op->src.reg == CFI_SP && 2958 insn->sym->frame_pointer) { 2959 /* addi.d fp,sp,imm on LoongArch */ 2960 if (cfa->base == CFI_SP && cfa->offset == op->src.offset) { 2961 cfa->base = CFI_BP; 2962 cfa->offset = 0; 2963 } 2964 break; 2965 } 2966 2967 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { 2968 /* addi.d sp,fp,imm on LoongArch */ 2969 if (cfa->base == CFI_BP && cfa->offset == 0) { 2970 if (insn->sym->frame_pointer) { 2971 cfa->base = CFI_SP; 2972 cfa->offset = -op->src.offset; 2973 } 2974 } else { 2975 /* lea disp(%rbp), %rsp */ 2976 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset); 2977 } 2978 break; 2979 } 2980 2981 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) { 2982 2983 /* drap: lea disp(%rsp), %drap */ 2984 cfi->drap_reg = op->dest.reg; 2985 2986 /* 2987 * lea disp(%rsp), %reg 2988 * 2989 * This is needed for the rare case where GCC 2990 * does something dumb like: 2991 * 2992 * lea 0x8(%rsp), %rcx 2993 * ... 2994 * mov %rcx, %rsp 2995 */ 2996 cfi->vals[op->dest.reg].base = CFI_CFA; 2997 cfi->vals[op->dest.reg].offset = \ 2998 -cfi->stack_size + op->src.offset; 2999 3000 break; 3001 } 3002 3003 if (cfi->drap && op->dest.reg == CFI_SP && 3004 op->src.reg == cfi->drap_reg) { 3005 3006 /* drap: lea disp(%drap), %rsp */ 3007 cfa->base = CFI_SP; 3008 cfa->offset = cfi->stack_size = -op->src.offset; 3009 cfi->drap_reg = CFI_UNDEFINED; 3010 cfi->drap = false; 3011 break; 3012 } 3013 3014 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) { 3015 WARN_INSN(insn, "unsupported stack register modification"); 3016 return -1; 3017 } 3018 3019 break; 3020 3021 case OP_SRC_AND: 3022 if (op->dest.reg != CFI_SP || 3023 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || 3024 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { 3025 WARN_INSN(insn, "unsupported stack pointer realignment"); 3026 return -1; 3027 } 3028 3029 if (cfi->drap_reg != CFI_UNDEFINED) { 3030 /* drap: and imm, %rsp */ 3031 cfa->base = cfi->drap_reg; 3032 cfa->offset = cfi->stack_size = 0; 3033 cfi->drap = true; 3034 } 3035 3036 /* 3037 * Older versions of GCC (4.8ish) realign the stack 3038 * without DRAP, with a frame pointer. 3039 */ 3040 3041 break; 3042 3043 case OP_SRC_POP: 3044 case OP_SRC_POPF: 3045 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) { 3046 3047 /* pop %rsp; # restore from a stack swizzle */ 3048 cfa->base = CFI_SP; 3049 break; 3050 } 3051 3052 if (!cfi->drap && op->dest.reg == cfa->base) { 3053 3054 /* pop %rbp */ 3055 cfa->base = CFI_SP; 3056 } 3057 3058 if (cfi->drap && cfa->base == CFI_BP_INDIRECT && 3059 op->dest.reg == cfi->drap_reg && 3060 cfi->drap_offset == -cfi->stack_size) { 3061 3062 /* drap: pop %drap */ 3063 cfa->base = cfi->drap_reg; 3064 cfa->offset = 0; 3065 cfi->drap_offset = -1; 3066 3067 } else if (cfi->stack_size == -regs[op->dest.reg].offset) { 3068 3069 /* pop %reg */ 3070 restore_reg(cfi, op->dest.reg); 3071 } 3072 3073 cfi->stack_size -= 8; 3074 if (cfa->base == CFI_SP) 3075 cfa->offset -= 8; 3076 3077 break; 3078 3079 case OP_SRC_REG_INDIRECT: 3080 if (!cfi->drap && op->dest.reg == cfa->base && 3081 op->dest.reg == CFI_BP) { 3082 3083 /* mov disp(%rsp), %rbp */ 3084 cfa->base = CFI_SP; 3085 cfa->offset = cfi->stack_size; 3086 } 3087 3088 if (cfi->drap && op->src.reg == CFI_BP && 3089 op->src.offset == cfi->drap_offset) { 3090 3091 /* drap: mov disp(%rbp), %drap */ 3092 cfa->base = cfi->drap_reg; 3093 cfa->offset = 0; 3094 cfi->drap_offset = -1; 3095 } 3096 3097 if (cfi->drap && op->src.reg == CFI_BP && 3098 op->src.offset == regs[op->dest.reg].offset) { 3099 3100 /* drap: mov disp(%rbp), %reg */ 3101 restore_reg(cfi, op->dest.reg); 3102 3103 } else if (op->src.reg == cfa->base && 3104 op->src.offset == regs[op->dest.reg].offset + cfa->offset) { 3105 3106 /* mov disp(%rbp), %reg */ 3107 /* mov disp(%rsp), %reg */ 3108 restore_reg(cfi, op->dest.reg); 3109 3110 } else if (op->src.reg == CFI_SP && 3111 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { 3112 3113 /* mov disp(%rsp), %reg */ 3114 restore_reg(cfi, op->dest.reg); 3115 } 3116 3117 break; 3118 3119 default: 3120 WARN_INSN(insn, "unknown stack-related instruction"); 3121 return -1; 3122 } 3123 3124 break; 3125 3126 case OP_DEST_PUSH: 3127 case OP_DEST_PUSHF: 3128 cfi->stack_size += 8; 3129 if (cfa->base == CFI_SP) 3130 cfa->offset += 8; 3131 3132 if (op->src.type != OP_SRC_REG) 3133 break; 3134 3135 if (cfi->drap) { 3136 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { 3137 3138 /* drap: push %drap */ 3139 cfa->base = CFI_BP_INDIRECT; 3140 cfa->offset = -cfi->stack_size; 3141 3142 /* save drap so we know when to restore it */ 3143 cfi->drap_offset = -cfi->stack_size; 3144 3145 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) { 3146 3147 /* drap: push %rbp */ 3148 cfi->stack_size = 0; 3149 3150 } else { 3151 3152 /* drap: push %reg */ 3153 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size); 3154 } 3155 3156 } else { 3157 3158 /* push %reg */ 3159 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size); 3160 } 3161 3162 /* detect when asm code uses rbp as a scratch register */ 3163 if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP && 3164 cfa->base != CFI_BP) 3165 cfi->bp_scratch = true; 3166 break; 3167 3168 case OP_DEST_REG_INDIRECT: 3169 3170 if (cfi->drap) { 3171 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) { 3172 3173 /* drap: mov %drap, disp(%rbp) */ 3174 cfa->base = CFI_BP_INDIRECT; 3175 cfa->offset = op->dest.offset; 3176 3177 /* save drap offset so we know when to restore it */ 3178 cfi->drap_offset = op->dest.offset; 3179 } else { 3180 3181 /* drap: mov reg, disp(%rbp) */ 3182 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset); 3183 } 3184 3185 } else if (op->dest.reg == cfa->base) { 3186 3187 /* mov reg, disp(%rbp) */ 3188 /* mov reg, disp(%rsp) */ 3189 save_reg(cfi, op->src.reg, CFI_CFA, 3190 op->dest.offset - cfi->cfa.offset); 3191 3192 } else if (op->dest.reg == CFI_SP) { 3193 3194 /* mov reg, disp(%rsp) */ 3195 save_reg(cfi, op->src.reg, CFI_CFA, 3196 op->dest.offset - cfi->stack_size); 3197 3198 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) { 3199 3200 /* mov %rsp, (%reg); # setup a stack swizzle. */ 3201 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT; 3202 cfi->vals[op->dest.reg].offset = cfa->offset; 3203 } 3204 3205 break; 3206 3207 case OP_DEST_MEM: 3208 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) { 3209 WARN_INSN(insn, "unknown stack-related memory operation"); 3210 return -1; 3211 } 3212 3213 /* pop mem */ 3214 cfi->stack_size -= 8; 3215 if (cfa->base == CFI_SP) 3216 cfa->offset -= 8; 3217 3218 break; 3219 3220 default: 3221 WARN_INSN(insn, "unknown stack-related instruction"); 3222 return -1; 3223 } 3224 3225 return 0; 3226 } 3227 3228 /* 3229 * The stack layouts of alternatives instructions can sometimes diverge when 3230 * they have stack modifications. That's fine as long as the potential stack 3231 * layouts don't conflict at any given potential instruction boundary. 3232 * 3233 * Flatten the CFIs of the different alternative code streams (both original 3234 * and replacement) into a single shared CFI array which can be used to detect 3235 * conflicts and nicely feed a linear array of ORC entries to the unwinder. 3236 */ 3237 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn) 3238 { 3239 struct cfi_state **alt_cfi; 3240 int group_off; 3241 3242 if (!insn->alt_group) 3243 return 0; 3244 3245 if (!insn->cfi) { 3246 WARN("CFI missing"); 3247 return -1; 3248 } 3249 3250 alt_cfi = insn->alt_group->cfi; 3251 group_off = insn->offset - insn->alt_group->first_insn->offset; 3252 3253 if (!alt_cfi[group_off]) { 3254 alt_cfi[group_off] = insn->cfi; 3255 } else { 3256 if (cficmp(alt_cfi[group_off], insn->cfi)) { 3257 struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group; 3258 struct instruction *orig = orig_group->first_insn; 3259 WARN_INSN(orig, "stack layout conflict in alternatives: %s", 3260 offstr(insn->sec, insn->offset)); 3261 return -1; 3262 } 3263 } 3264 3265 return 0; 3266 } 3267 3268 static int handle_insn_ops(struct instruction *insn, 3269 struct instruction *next_insn, 3270 struct insn_state *state) 3271 { 3272 struct stack_op *op; 3273 int ret; 3274 3275 for (op = insn->stack_ops; op; op = op->next) { 3276 3277 ret = update_cfi_state(insn, next_insn, &state->cfi, op); 3278 if (ret) 3279 return ret; 3280 3281 if (!opts.uaccess || !insn->alt_group) 3282 continue; 3283 3284 if (op->dest.type == OP_DEST_PUSHF) { 3285 if (!state->uaccess_stack) { 3286 state->uaccess_stack = 1; 3287 } else if (state->uaccess_stack >> 31) { 3288 WARN_INSN(insn, "PUSHF stack exhausted"); 3289 return 1; 3290 } 3291 state->uaccess_stack <<= 1; 3292 state->uaccess_stack |= state->uaccess; 3293 } 3294 3295 if (op->src.type == OP_SRC_POPF) { 3296 if (state->uaccess_stack) { 3297 state->uaccess = state->uaccess_stack & 1; 3298 state->uaccess_stack >>= 1; 3299 if (state->uaccess_stack == 1) 3300 state->uaccess_stack = 0; 3301 } 3302 } 3303 } 3304 3305 return 0; 3306 } 3307 3308 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2) 3309 { 3310 struct cfi_state *cfi1 = insn->cfi; 3311 int i; 3312 3313 if (!cfi1) { 3314 WARN("CFI missing"); 3315 return false; 3316 } 3317 3318 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) { 3319 3320 WARN_INSN(insn, "stack state mismatch: cfa1=%d%+d cfa2=%d%+d", 3321 cfi1->cfa.base, cfi1->cfa.offset, 3322 cfi2->cfa.base, cfi2->cfa.offset); 3323 return false; 3324 3325 } 3326 3327 if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) { 3328 for (i = 0; i < CFI_NUM_REGS; i++) { 3329 3330 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i], sizeof(struct cfi_reg))) 3331 continue; 3332 3333 WARN_INSN(insn, "stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", 3334 i, cfi1->regs[i].base, cfi1->regs[i].offset, 3335 i, cfi2->regs[i].base, cfi2->regs[i].offset); 3336 } 3337 return false; 3338 } 3339 3340 if (cfi1->type != cfi2->type) { 3341 3342 WARN_INSN(insn, "stack state mismatch: type1=%d type2=%d", 3343 cfi1->type, cfi2->type); 3344 return false; 3345 } 3346 3347 if (cfi1->drap != cfi2->drap || 3348 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) || 3349 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) { 3350 3351 WARN_INSN(insn, "stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)", 3352 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset, 3353 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset); 3354 return false; 3355 } 3356 3357 return true; 3358 } 3359 3360 static inline bool func_uaccess_safe(struct symbol *func) 3361 { 3362 if (func) 3363 return func->uaccess_safe; 3364 3365 return false; 3366 } 3367 3368 static inline const char *call_dest_name(struct instruction *insn) 3369 { 3370 static char pvname[19]; 3371 struct reloc *reloc; 3372 int idx; 3373 3374 if (insn_call_dest(insn)) 3375 return insn_call_dest(insn)->name; 3376 3377 reloc = insn_reloc(NULL, insn); 3378 if (reloc && !strcmp(reloc->sym->name, "pv_ops")) { 3379 idx = (reloc_addend(reloc) / sizeof(void *)); 3380 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx); 3381 return pvname; 3382 } 3383 3384 return "{dynamic}"; 3385 } 3386 3387 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) 3388 { 3389 struct symbol *target; 3390 struct reloc *reloc; 3391 int idx; 3392 3393 reloc = insn_reloc(file, insn); 3394 if (!reloc || strcmp(reloc->sym->name, "pv_ops")) 3395 return false; 3396 3397 idx = arch_insn_adjusted_addend(insn, reloc) / sizeof(void *); 3398 3399 if (file->pv_ops[idx].clean) 3400 return true; 3401 3402 file->pv_ops[idx].clean = true; 3403 3404 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) { 3405 if (!target->sec->noinstr) { 3406 WARN("pv_ops[%d]: %s", idx, target->name); 3407 file->pv_ops[idx].clean = false; 3408 } 3409 } 3410 3411 return file->pv_ops[idx].clean; 3412 } 3413 3414 static inline bool noinstr_call_dest(struct objtool_file *file, 3415 struct instruction *insn, 3416 struct symbol *func) 3417 { 3418 /* 3419 * We can't deal with indirect function calls at present; 3420 * assume they're instrumented. 3421 */ 3422 if (!func) { 3423 if (file->pv_ops) 3424 return pv_call_dest(file, insn); 3425 3426 return false; 3427 } 3428 3429 /* 3430 * If the symbol is from a noinstr section; we good. 3431 */ 3432 if (func->sec->noinstr) 3433 return true; 3434 3435 /* 3436 * If the symbol is a static_call trampoline, we can't tell. 3437 */ 3438 if (func->static_call_tramp) 3439 return true; 3440 3441 /* 3442 * The __ubsan_handle_*() calls are like WARN(), they only happen when 3443 * something 'BAD' happened. At the risk of taking the machine down, 3444 * let them proceed to get the message out. 3445 */ 3446 if (!strncmp(func->name, "__ubsan_handle_", 15)) 3447 return true; 3448 3449 return false; 3450 } 3451 3452 static int validate_call(struct objtool_file *file, 3453 struct instruction *insn, 3454 struct insn_state *state) 3455 { 3456 if (state->noinstr && state->instr <= 0 && 3457 !noinstr_call_dest(file, insn, insn_call_dest(insn))) { 3458 WARN_INSN(insn, "call to %s() leaves .noinstr.text section", call_dest_name(insn)); 3459 return 1; 3460 } 3461 3462 if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) { 3463 WARN_INSN(insn, "call to %s() with UACCESS enabled", call_dest_name(insn)); 3464 return 1; 3465 } 3466 3467 if (state->df) { 3468 WARN_INSN(insn, "call to %s() with DF set", call_dest_name(insn)); 3469 return 1; 3470 } 3471 3472 return 0; 3473 } 3474 3475 static int validate_sibling_call(struct objtool_file *file, 3476 struct instruction *insn, 3477 struct insn_state *state) 3478 { 3479 if (insn_func(insn) && has_modified_stack_frame(insn, state)) { 3480 WARN_INSN(insn, "sibling call from callable instruction with modified stack frame"); 3481 return 1; 3482 } 3483 3484 return validate_call(file, insn, state); 3485 } 3486 3487 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state) 3488 { 3489 if (state->noinstr && state->instr > 0) { 3490 WARN_INSN(insn, "return with instrumentation enabled"); 3491 return 1; 3492 } 3493 3494 if (state->uaccess && !func_uaccess_safe(func)) { 3495 WARN_INSN(insn, "return with UACCESS enabled"); 3496 return 1; 3497 } 3498 3499 if (!state->uaccess && func_uaccess_safe(func)) { 3500 WARN_INSN(insn, "return with UACCESS disabled from a UACCESS-safe function"); 3501 return 1; 3502 } 3503 3504 if (state->df) { 3505 WARN_INSN(insn, "return with DF set"); 3506 return 1; 3507 } 3508 3509 if (func && has_modified_stack_frame(insn, state)) { 3510 WARN_INSN(insn, "return with modified stack frame"); 3511 return 1; 3512 } 3513 3514 if (state->cfi.bp_scratch) { 3515 WARN_INSN(insn, "BP used as a scratch register"); 3516 return 1; 3517 } 3518 3519 return 0; 3520 } 3521 3522 static struct instruction *next_insn_to_validate(struct objtool_file *file, 3523 struct instruction *insn) 3524 { 3525 struct alt_group *alt_group = insn->alt_group; 3526 3527 /* 3528 * Simulate the fact that alternatives are patched in-place. When the 3529 * end of a replacement alt_group is reached, redirect objtool flow to 3530 * the end of the original alt_group. 3531 * 3532 * insn->alts->insn -> alt_group->first_insn 3533 * ... 3534 * alt_group->last_insn 3535 * [alt_group->nop] -> next(orig_group->last_insn) 3536 */ 3537 if (alt_group) { 3538 if (alt_group->nop) { 3539 /* ->nop implies ->orig_group */ 3540 if (insn == alt_group->last_insn) 3541 return alt_group->nop; 3542 if (insn == alt_group->nop) 3543 goto next_orig; 3544 } 3545 if (insn == alt_group->last_insn && alt_group->orig_group) 3546 goto next_orig; 3547 } 3548 3549 return next_insn_same_sec(file, insn); 3550 3551 next_orig: 3552 return next_insn_same_sec(file, alt_group->orig_group->last_insn); 3553 } 3554 3555 static bool skip_alt_group(struct instruction *insn) 3556 { 3557 struct instruction *alt_insn = insn->alts ? insn->alts->insn : NULL; 3558 3559 if (!insn->alt_group) 3560 return false; 3561 3562 /* ANNOTATE_IGNORE_ALTERNATIVE */ 3563 if (insn->alt_group->ignore) { 3564 TRACE_INSN(insn, "alt group ignored"); 3565 return true; 3566 } 3567 3568 /* 3569 * For NOP patched with CLAC/STAC, only follow the latter to avoid 3570 * impossible code paths combining patched CLAC with unpatched STAC 3571 * or vice versa. 3572 * 3573 * ANNOTATE_IGNORE_ALTERNATIVE could have been used here, but Linus 3574 * requested not to do that to avoid hurting .s file readability 3575 * around CLAC/STAC alternative sites. 3576 */ 3577 3578 if (!alt_insn) 3579 return false; 3580 3581 /* Don't override ASM_{CLAC,STAC}_UNSAFE */ 3582 if (alt_insn->alt_group && alt_insn->alt_group->ignore) 3583 return false; 3584 3585 return alt_insn->type == INSN_CLAC || alt_insn->type == INSN_STAC; 3586 } 3587 3588 static int checksum_debug_init(struct objtool_file *file) 3589 { 3590 char *dup, *s; 3591 3592 if (!opts.debug_checksum) 3593 return 0; 3594 3595 dup = strdup(opts.debug_checksum); 3596 if (!dup) { 3597 ERROR_GLIBC("strdup"); 3598 return -1; 3599 } 3600 3601 s = dup; 3602 while (*s) { 3603 struct symbol *func; 3604 char *comma; 3605 3606 comma = strchr(s, ','); 3607 if (comma) 3608 *comma = '\0'; 3609 3610 func = find_symbol_by_name(file->elf, s); 3611 if (!func || !is_func_sym(func)) 3612 WARN("--debug-checksum: can't find '%s'", s); 3613 else 3614 func->debug_checksum = 1; 3615 3616 if (!comma) 3617 break; 3618 3619 s = comma + 1; 3620 } 3621 3622 free(dup); 3623 return 0; 3624 } 3625 3626 static void checksum_update_insn(struct objtool_file *file, struct symbol *func, 3627 struct instruction *insn) 3628 { 3629 struct reloc *reloc = insn_reloc(file, insn); 3630 unsigned long offset; 3631 struct symbol *sym; 3632 3633 if (insn->fake) 3634 return; 3635 3636 checksum_update(func, insn, insn->sec->data->d_buf + insn->offset, insn->len); 3637 3638 if (!reloc) { 3639 struct symbol *call_dest = insn_call_dest(insn); 3640 3641 if (call_dest) 3642 checksum_update(func, insn, call_dest->demangled_name, 3643 strlen(call_dest->demangled_name)); 3644 return; 3645 } 3646 3647 sym = reloc->sym; 3648 offset = arch_insn_adjusted_addend(insn, reloc); 3649 3650 if (is_string_sec(sym->sec)) { 3651 char *str; 3652 3653 str = sym->sec->data->d_buf + sym->offset + offset; 3654 checksum_update(func, insn, str, strlen(str)); 3655 return; 3656 } 3657 3658 if (is_sec_sym(sym)) { 3659 sym = find_symbol_containing(reloc->sym->sec, offset); 3660 if (!sym) 3661 return; 3662 3663 offset -= sym->offset; 3664 } 3665 3666 checksum_update(func, insn, sym->demangled_name, strlen(sym->demangled_name)); 3667 checksum_update(func, insn, &offset, sizeof(offset)); 3668 } 3669 3670 static int validate_branch(struct objtool_file *file, struct symbol *func, 3671 struct instruction *insn, struct insn_state state); 3672 static int do_validate_branch(struct objtool_file *file, struct symbol *func, 3673 struct instruction *insn, struct insn_state state); 3674 3675 static int validate_insn(struct objtool_file *file, struct symbol *func, 3676 struct instruction *insn, struct insn_state *statep, 3677 struct instruction *prev_insn, struct instruction *next_insn, 3678 bool *dead_end) 3679 { 3680 /* prev_state is not used if there is no disassembly support */ 3681 struct insn_state prev_state __maybe_unused; 3682 struct alternative *alt; 3683 u8 visited; 3684 int ret; 3685 3686 /* 3687 * Any returns before the end of this function are effectively dead 3688 * ends, i.e. validate_branch() has reached the end of the branch. 3689 */ 3690 *dead_end = true; 3691 3692 visited = VISITED_BRANCH << statep->uaccess; 3693 if (insn->visited & VISITED_BRANCH_MASK) { 3694 if (!insn->hint && !insn_cfi_match(insn, &statep->cfi)) 3695 return 1; 3696 3697 if (insn->visited & visited) { 3698 TRACE_INSN(insn, "already visited"); 3699 return 0; 3700 } 3701 } else { 3702 nr_insns_visited++; 3703 } 3704 3705 if (statep->noinstr) 3706 statep->instr += insn->instr; 3707 3708 if (insn->hint) { 3709 if (insn->restore) { 3710 struct instruction *save_insn, *i; 3711 3712 i = insn; 3713 save_insn = NULL; 3714 3715 sym_for_each_insn_continue_reverse(file, func, i) { 3716 if (i->save) { 3717 save_insn = i; 3718 break; 3719 } 3720 } 3721 3722 if (!save_insn) { 3723 WARN_INSN(insn, "no corresponding CFI save for CFI restore"); 3724 return 1; 3725 } 3726 3727 if (!save_insn->visited) { 3728 /* 3729 * If the restore hint insn is at the 3730 * beginning of a basic block and was 3731 * branched to from elsewhere, and the 3732 * save insn hasn't been visited yet, 3733 * defer following this branch for now. 3734 * It will be seen later via the 3735 * straight-line path. 3736 */ 3737 if (!prev_insn) { 3738 TRACE_INSN(insn, "defer restore"); 3739 return 0; 3740 } 3741 3742 WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/restore combo"); 3743 return 1; 3744 } 3745 3746 insn->cfi = save_insn->cfi; 3747 nr_cfi_reused++; 3748 } 3749 3750 statep->cfi = *insn->cfi; 3751 } else { 3752 /* XXX track if we actually changed statep->cfi */ 3753 3754 if (prev_insn && !cficmp(prev_insn->cfi, &statep->cfi)) { 3755 insn->cfi = prev_insn->cfi; 3756 nr_cfi_reused++; 3757 } else { 3758 insn->cfi = cfi_hash_find_or_add(&statep->cfi); 3759 } 3760 } 3761 3762 insn->visited |= visited; 3763 3764 if (propagate_alt_cfi(file, insn)) 3765 return 1; 3766 3767 if (insn->alts) { 3768 int i, num_alts; 3769 3770 num_alts = 0; 3771 for (alt = insn->alts; alt; alt = alt->next) 3772 num_alts++; 3773 3774 i = 1; 3775 for (alt = insn->alts; alt; alt = alt->next) { 3776 TRACE_INSN(insn, "alternative %d/%d", i, num_alts); 3777 ret = validate_branch(file, func, alt->insn, *statep); 3778 if (ret) { 3779 BT_INSN(insn, "(alt)"); 3780 return ret; 3781 } 3782 i++; 3783 } 3784 TRACE_INSN(insn, "alternative DEFAULT"); 3785 } 3786 3787 if (skip_alt_group(insn)) 3788 return 0; 3789 3790 prev_state = *statep; 3791 ret = handle_insn_ops(insn, next_insn, statep); 3792 TRACE_INSN_STATE(insn, &prev_state, statep); 3793 3794 if (ret) 3795 return 1; 3796 3797 switch (insn->type) { 3798 3799 case INSN_RETURN: 3800 TRACE_INSN(insn, "return"); 3801 return validate_return(func, insn, statep); 3802 3803 case INSN_CALL: 3804 case INSN_CALL_DYNAMIC: 3805 if (insn->type == INSN_CALL) 3806 TRACE_INSN(insn, "call"); 3807 else 3808 TRACE_INSN(insn, "indirect call"); 3809 3810 ret = validate_call(file, insn, statep); 3811 if (ret) 3812 return ret; 3813 3814 if (opts.stackval && func && !is_special_call(insn) && 3815 !has_valid_stack_frame(statep)) { 3816 WARN_INSN(insn, "call without frame pointer save/setup"); 3817 return 1; 3818 } 3819 3820 break; 3821 3822 case INSN_JUMP_CONDITIONAL: 3823 case INSN_JUMP_UNCONDITIONAL: 3824 if (is_sibling_call(insn)) { 3825 TRACE_INSN(insn, "sibling call"); 3826 ret = validate_sibling_call(file, insn, statep); 3827 if (ret) 3828 return ret; 3829 3830 } else if (insn->jump_dest) { 3831 if (insn->type == INSN_JUMP_UNCONDITIONAL) 3832 TRACE_INSN(insn, "unconditional jump"); 3833 else 3834 TRACE_INSN(insn, "jump taken"); 3835 3836 ret = validate_branch(file, func, insn->jump_dest, *statep); 3837 if (ret) { 3838 BT_INSN(insn, "(branch)"); 3839 return ret; 3840 } 3841 } 3842 3843 if (insn->type == INSN_JUMP_UNCONDITIONAL) 3844 return 0; 3845 3846 TRACE_INSN(insn, "jump not taken"); 3847 break; 3848 3849 case INSN_JUMP_DYNAMIC: 3850 case INSN_JUMP_DYNAMIC_CONDITIONAL: 3851 TRACE_INSN(insn, "indirect jump"); 3852 if (is_sibling_call(insn)) { 3853 ret = validate_sibling_call(file, insn, statep); 3854 if (ret) 3855 return ret; 3856 } 3857 3858 if (insn->type == INSN_JUMP_DYNAMIC) 3859 return 0; 3860 3861 break; 3862 3863 case INSN_SYSCALL: 3864 TRACE_INSN(insn, "syscall"); 3865 if (func && (!next_insn || !next_insn->hint)) { 3866 WARN_INSN(insn, "unsupported instruction in callable function"); 3867 return 1; 3868 } 3869 3870 break; 3871 3872 case INSN_SYSRET: 3873 TRACE_INSN(insn, "sysret"); 3874 if (func && (!next_insn || !next_insn->hint)) { 3875 WARN_INSN(insn, "unsupported instruction in callable function"); 3876 return 1; 3877 } 3878 3879 return 0; 3880 3881 case INSN_STAC: 3882 TRACE_INSN(insn, "stac"); 3883 if (!opts.uaccess) 3884 break; 3885 3886 if (statep->uaccess) { 3887 WARN_INSN(insn, "recursive UACCESS enable"); 3888 return 1; 3889 } 3890 3891 statep->uaccess = true; 3892 break; 3893 3894 case INSN_CLAC: 3895 TRACE_INSN(insn, "clac"); 3896 if (!opts.uaccess) 3897 break; 3898 3899 if (!statep->uaccess && func) { 3900 WARN_INSN(insn, "redundant UACCESS disable"); 3901 return 1; 3902 } 3903 3904 if (func_uaccess_safe(func) && !statep->uaccess_stack) { 3905 WARN_INSN(insn, "UACCESS-safe disables UACCESS"); 3906 return 1; 3907 } 3908 3909 statep->uaccess = false; 3910 break; 3911 3912 case INSN_STD: 3913 TRACE_INSN(insn, "std"); 3914 if (statep->df) { 3915 WARN_INSN(insn, "recursive STD"); 3916 return 1; 3917 } 3918 3919 statep->df = true; 3920 break; 3921 3922 case INSN_CLD: 3923 TRACE_INSN(insn, "cld"); 3924 if (!statep->df && func) { 3925 WARN_INSN(insn, "redundant CLD"); 3926 return 1; 3927 } 3928 3929 statep->df = false; 3930 break; 3931 3932 default: 3933 break; 3934 } 3935 3936 if (insn->dead_end) 3937 TRACE_INSN(insn, "dead end"); 3938 3939 *dead_end = insn->dead_end; 3940 return 0; 3941 } 3942 3943 /* 3944 * Follow the branch starting at the given instruction, and recursively follow 3945 * any other branches (jumps). Meanwhile, track the frame pointer state at 3946 * each instruction and validate all the rules described in 3947 * tools/objtool/Documentation/objtool.txt. 3948 */ 3949 static int do_validate_branch(struct objtool_file *file, struct symbol *func, 3950 struct instruction *insn, struct insn_state state) 3951 { 3952 struct instruction *next_insn, *prev_insn = NULL; 3953 bool dead_end; 3954 int ret; 3955 3956 if (func && func->ignore) 3957 return 0; 3958 3959 do { 3960 insn->trace = 0; 3961 next_insn = next_insn_to_validate(file, insn); 3962 3963 if (opts.checksum && func && insn->sec) 3964 checksum_update_insn(file, func, insn); 3965 3966 if (func && insn_func(insn) && func != insn_func(insn)->pfunc) { 3967 /* Ignore KCFI type preambles, which always fall through */ 3968 if (is_prefix_func(func)) 3969 return 0; 3970 3971 if (file->ignore_unreachables) 3972 return 0; 3973 3974 WARN("%s() falls through to next function %s()", 3975 func->name, insn_func(insn)->name); 3976 func->warned = 1; 3977 3978 return 1; 3979 } 3980 3981 ret = validate_insn(file, func, insn, &state, prev_insn, next_insn, 3982 &dead_end); 3983 3984 if (!insn->trace) { 3985 if (ret) 3986 TRACE_INSN(insn, "warning (%d)", ret); 3987 else 3988 TRACE_INSN(insn, NULL); 3989 } 3990 3991 if (!dead_end && !next_insn) { 3992 if (state.cfi.cfa.base == CFI_UNDEFINED) 3993 return 0; 3994 if (file->ignore_unreachables) 3995 return 0; 3996 3997 WARN("%s%sunexpected end of section %s", 3998 func ? func->name : "", func ? "(): " : "", 3999 insn->sec->name); 4000 return 1; 4001 } 4002 4003 prev_insn = insn; 4004 insn = next_insn; 4005 4006 } while (!dead_end); 4007 4008 return ret; 4009 } 4010 4011 static int validate_branch(struct objtool_file *file, struct symbol *func, 4012 struct instruction *insn, struct insn_state state) 4013 { 4014 int ret; 4015 4016 trace_depth_inc(); 4017 ret = do_validate_branch(file, func, insn, state); 4018 trace_depth_dec(); 4019 4020 return ret; 4021 } 4022 4023 static int validate_unwind_hint(struct objtool_file *file, 4024 struct instruction *insn, 4025 struct insn_state *state) 4026 { 4027 if (insn->hint && !insn->visited) { 4028 struct symbol *func = insn_func(insn); 4029 int ret; 4030 4031 if (opts.checksum) 4032 checksum_init(func); 4033 4034 ret = validate_branch(file, func, insn, *state); 4035 if (ret) 4036 BT_INSN(insn, "<=== (hint)"); 4037 return ret; 4038 } 4039 4040 return 0; 4041 } 4042 4043 static int validate_unwind_hints(struct objtool_file *file, struct section *sec) 4044 { 4045 struct instruction *insn; 4046 struct insn_state state; 4047 int warnings = 0; 4048 4049 if (!file->hints) 4050 return 0; 4051 4052 init_insn_state(file, &state, sec); 4053 4054 if (sec) { 4055 sec_for_each_insn(file, sec, insn) 4056 warnings += validate_unwind_hint(file, insn, &state); 4057 } else { 4058 for_each_insn(file, insn) 4059 warnings += validate_unwind_hint(file, insn, &state); 4060 } 4061 4062 return warnings; 4063 } 4064 4065 /* 4066 * Validate rethunk entry constraint: must untrain RET before the first RET. 4067 * 4068 * Follow every branch (intra-function) and ensure VALIDATE_UNRET_END comes 4069 * before an actual RET instruction. 4070 */ 4071 static int validate_unret(struct objtool_file *file, struct instruction *insn) 4072 { 4073 struct instruction *next, *dest; 4074 int ret; 4075 4076 for (;;) { 4077 next = next_insn_to_validate(file, insn); 4078 4079 if (insn->visited & VISITED_UNRET) 4080 return 0; 4081 4082 insn->visited |= VISITED_UNRET; 4083 4084 if (insn->alts) { 4085 struct alternative *alt; 4086 for (alt = insn->alts; alt; alt = alt->next) { 4087 ret = validate_unret(file, alt->insn); 4088 if (ret) { 4089 BT_INSN(insn, "(alt)"); 4090 return ret; 4091 } 4092 } 4093 } 4094 4095 switch (insn->type) { 4096 4097 case INSN_CALL_DYNAMIC: 4098 case INSN_JUMP_DYNAMIC: 4099 case INSN_JUMP_DYNAMIC_CONDITIONAL: 4100 WARN_INSN(insn, "early indirect call"); 4101 return 1; 4102 4103 case INSN_JUMP_UNCONDITIONAL: 4104 case INSN_JUMP_CONDITIONAL: 4105 if (!is_sibling_call(insn)) { 4106 if (!insn->jump_dest) { 4107 WARN_INSN(insn, "unresolved jump target after linking?!?"); 4108 return 1; 4109 } 4110 ret = validate_unret(file, insn->jump_dest); 4111 if (ret) { 4112 BT_INSN(insn, "(branch%s)", 4113 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : ""); 4114 return ret; 4115 } 4116 4117 if (insn->type == INSN_JUMP_UNCONDITIONAL) 4118 return 0; 4119 4120 break; 4121 } 4122 4123 /* fallthrough */ 4124 case INSN_CALL: 4125 dest = find_insn(file, insn_call_dest(insn)->sec, 4126 insn_call_dest(insn)->offset); 4127 if (!dest) { 4128 WARN("Unresolved function after linking!?: %s", 4129 insn_call_dest(insn)->name); 4130 return 1; 4131 } 4132 4133 ret = validate_unret(file, dest); 4134 if (ret) { 4135 BT_INSN(insn, "(call)"); 4136 return ret; 4137 } 4138 /* 4139 * If a call returns without error, it must have seen UNTRAIN_RET. 4140 * Therefore any non-error return is a success. 4141 */ 4142 return 0; 4143 4144 case INSN_RETURN: 4145 WARN_INSN(insn, "RET before UNTRAIN"); 4146 return 1; 4147 4148 case INSN_SYSCALL: 4149 break; 4150 4151 case INSN_SYSRET: 4152 return 0; 4153 4154 case INSN_NOP: 4155 if (insn->retpoline_safe) 4156 return 0; 4157 break; 4158 4159 default: 4160 break; 4161 } 4162 4163 if (insn->dead_end) 4164 return 0; 4165 4166 if (!next) { 4167 WARN_INSN(insn, "teh end!"); 4168 return 1; 4169 } 4170 insn = next; 4171 } 4172 4173 return 0; 4174 } 4175 4176 /* 4177 * Validate that all branches starting at VALIDATE_UNRET_BEGIN encounter 4178 * VALIDATE_UNRET_END before RET. 4179 */ 4180 static int validate_unrets(struct objtool_file *file) 4181 { 4182 struct instruction *insn; 4183 int warnings = 0; 4184 4185 for_each_insn(file, insn) { 4186 if (!insn->unret) 4187 continue; 4188 4189 warnings += validate_unret(file, insn); 4190 } 4191 4192 return warnings; 4193 } 4194 4195 static int validate_retpoline(struct objtool_file *file) 4196 { 4197 struct instruction *insn; 4198 int warnings = 0; 4199 4200 for_each_insn(file, insn) { 4201 if (insn->type != INSN_JUMP_DYNAMIC && 4202 insn->type != INSN_CALL_DYNAMIC && 4203 insn->type != INSN_RETURN) 4204 continue; 4205 4206 if (insn->retpoline_safe) 4207 continue; 4208 4209 if (insn->sec->init) 4210 continue; 4211 4212 if (insn->type == INSN_RETURN) { 4213 if (opts.rethunk) { 4214 WARN_INSN(insn, "'naked' return found in MITIGATION_RETHUNK build"); 4215 warnings++; 4216 } 4217 continue; 4218 } 4219 4220 WARN_INSN(insn, "indirect %s found in MITIGATION_RETPOLINE build", 4221 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call"); 4222 warnings++; 4223 } 4224 4225 if (!opts.cfi) 4226 return warnings; 4227 4228 /* 4229 * kCFI call sites look like: 4230 * 4231 * movl $(-0x12345678), %r10d 4232 * addl -4(%r11), %r10d 4233 * jz 1f 4234 * ud2 4235 * 1: cs call __x86_indirect_thunk_r11 4236 * 4237 * Verify all indirect calls are kCFI adorned by checking for the 4238 * UD2. Notably, doing __nocfi calls to regular (cfi) functions is 4239 * broken. 4240 */ 4241 list_for_each_entry(insn, &file->retpoline_call_list, call_node) { 4242 struct symbol *sym = insn->sym; 4243 4244 if (sym && (sym->type == STT_NOTYPE || 4245 sym->type == STT_FUNC) && !sym->nocfi) { 4246 struct instruction *prev = 4247 prev_insn_same_sym(file, insn); 4248 4249 if (!prev || prev->type != INSN_BUG) { 4250 WARN_INSN(insn, "no-cfi indirect call!"); 4251 warnings++; 4252 } 4253 } 4254 } 4255 4256 return warnings; 4257 } 4258 4259 static bool is_kasan_insn(struct instruction *insn) 4260 { 4261 return (insn->type == INSN_CALL && 4262 !strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return")); 4263 } 4264 4265 static bool is_ubsan_insn(struct instruction *insn) 4266 { 4267 return (insn->type == INSN_CALL && 4268 !strcmp(insn_call_dest(insn)->name, 4269 "__ubsan_handle_builtin_unreachable")); 4270 } 4271 4272 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn) 4273 { 4274 struct symbol *func = insn_func(insn); 4275 struct instruction *prev_insn; 4276 int i; 4277 4278 if (insn->type == INSN_NOP || insn->type == INSN_TRAP || 4279 insn->hole || (func && func->ignore)) 4280 return true; 4281 4282 /* 4283 * Ignore alternative replacement instructions. This can happen 4284 * when a whitelisted function uses one of the ALTERNATIVE macros. 4285 */ 4286 if (!strcmp(insn->sec->name, ".altinstr_replacement") || 4287 !strcmp(insn->sec->name, ".altinstr_aux")) 4288 return true; 4289 4290 if (!func) 4291 return false; 4292 4293 if (func->static_call_tramp) 4294 return true; 4295 4296 /* 4297 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees 4298 * __builtin_unreachable(). The BUG() macro has an unreachable() after 4299 * the UD2, which causes GCC's undefined trap logic to emit another UD2 4300 * (or occasionally a JMP to UD2). 4301 * 4302 * It may also insert a UD2 after calling a __noreturn function. 4303 */ 4304 prev_insn = prev_insn_same_sec(file, insn); 4305 if (prev_insn && prev_insn->dead_end && 4306 (insn->type == INSN_BUG || 4307 (insn->type == INSN_JUMP_UNCONDITIONAL && 4308 insn->jump_dest && insn->jump_dest->type == INSN_BUG))) 4309 return true; 4310 4311 /* 4312 * Check if this (or a subsequent) instruction is related to 4313 * CONFIG_UBSAN or CONFIG_KASAN. 4314 * 4315 * End the search at 5 instructions to avoid going into the weeds. 4316 */ 4317 for (i = 0; i < 5; i++) { 4318 4319 if (is_kasan_insn(insn) || is_ubsan_insn(insn)) 4320 return true; 4321 4322 if (insn->type == INSN_JUMP_UNCONDITIONAL) { 4323 if (insn->jump_dest && 4324 insn_func(insn->jump_dest) == func) { 4325 insn = insn->jump_dest; 4326 continue; 4327 } 4328 4329 break; 4330 } 4331 4332 if (insn->offset + insn->len >= func->offset + func->len) 4333 break; 4334 4335 insn = next_insn_same_sec(file, insn); 4336 } 4337 4338 return false; 4339 } 4340 4341 /* 4342 * For FineIBT or kCFI, a certain number of bytes preceding the function may be 4343 * NOPs. Those NOPs may be rewritten at runtime and executed, so give them a 4344 * proper function name: __pfx_<func>. 4345 * 4346 * The NOPs may not exist for the following cases: 4347 * 4348 * - compiler cloned functions (*.cold, *.part0, etc) 4349 * - asm functions created with inline asm or without SYM_FUNC_START() 4350 * 4351 * Also, the function may already have a prefix from a previous objtool run 4352 * (livepatch extracted functions, or manually running objtool multiple times). 4353 * 4354 * So return 0 if the NOPs are missing or the function already has a prefix 4355 * symbol. 4356 */ 4357 static int create_prefix_symbol(struct objtool_file *file, struct symbol *func) 4358 { 4359 struct instruction *insn, *prev; 4360 char name[SYM_NAME_LEN]; 4361 struct cfi_state *cfi; 4362 4363 if (!is_func_sym(func) || is_prefix_func(func) || 4364 func->cold || func->static_call_tramp) 4365 return 0; 4366 4367 if ((strlen(func->name) + sizeof("__pfx_") > SYM_NAME_LEN)) { 4368 WARN("%s: symbol name too long, can't create __pfx_ symbol", 4369 func->name); 4370 return 0; 4371 } 4372 4373 if (snprintf_check(name, SYM_NAME_LEN, "__pfx_%s", func->name)) 4374 return -1; 4375 4376 if (file->klp) { 4377 struct symbol *pfx; 4378 4379 pfx = find_symbol_by_offset(func->sec, func->offset - opts.prefix); 4380 if (pfx && is_prefix_func(pfx) && !strcmp(pfx->name, name)) 4381 return 0; 4382 } 4383 4384 insn = find_insn(file, func->sec, func->offset); 4385 if (!insn) { 4386 WARN("%s: can't find starting instruction", func->name); 4387 return -1; 4388 } 4389 4390 for (prev = prev_insn_same_sec(file, insn); 4391 prev; 4392 prev = prev_insn_same_sec(file, prev)) { 4393 u64 offset; 4394 4395 if (prev->type != INSN_NOP) 4396 return 0; 4397 4398 offset = func->offset - prev->offset; 4399 4400 if (offset > opts.prefix) 4401 return 0; 4402 4403 if (offset < opts.prefix) 4404 continue; 4405 4406 if (!elf_create_symbol(file->elf, name, func->sec, 4407 GELF_ST_BIND(func->sym.st_info), 4408 GELF_ST_TYPE(func->sym.st_info), 4409 prev->offset, opts.prefix)) 4410 return -1; 4411 4412 break; 4413 } 4414 4415 if (!prev) 4416 return 0; 4417 4418 if (!insn->cfi) { 4419 /* 4420 * This can happen if stack validation isn't enabled or the 4421 * function is annotated with STACK_FRAME_NON_STANDARD. 4422 */ 4423 return 0; 4424 } 4425 4426 /* Propagate insn->cfi to the prefix code */ 4427 cfi = cfi_hash_find_or_add(insn->cfi); 4428 for (; prev != insn; prev = next_insn_same_sec(file, prev)) 4429 prev->cfi = cfi; 4430 4431 return 0; 4432 } 4433 4434 static int create_prefix_symbols(struct objtool_file *file) 4435 { 4436 struct section *sec; 4437 struct symbol *func; 4438 4439 for_each_sec(file->elf, sec) { 4440 if (!is_text_sec(sec)) 4441 continue; 4442 4443 sec_for_each_sym(sec, func) { 4444 if (create_prefix_symbol(file, func)) 4445 return -1; 4446 } 4447 } 4448 4449 return 0; 4450 } 4451 4452 static int validate_symbol(struct objtool_file *file, struct section *sec, 4453 struct symbol *sym, struct insn_state *state) 4454 { 4455 struct instruction *insn; 4456 struct symbol *func; 4457 int ret; 4458 4459 if (!sym->len) { 4460 WARN("%s() is missing an ELF size annotation", sym->name); 4461 return 1; 4462 } 4463 4464 if (sym->pfunc != sym || sym->alias != sym) 4465 return 0; 4466 4467 insn = find_insn(file, sec, sym->offset); 4468 if (!insn || insn->visited) 4469 return 0; 4470 4471 if (opts.uaccess) 4472 state->uaccess = sym->uaccess_safe; 4473 4474 func = insn_func(insn); 4475 4476 if (opts.checksum) 4477 checksum_init(func); 4478 4479 if (opts.trace && !fnmatch(opts.trace, sym->name, 0)) { 4480 trace_enable(); 4481 TRACE("%s: validation begin\n", sym->name); 4482 } 4483 4484 ret = validate_branch(file, func, insn, *state); 4485 if (ret) 4486 BT_INSN(insn, "<=== (sym)"); 4487 4488 TRACE("%s: validation %s\n\n", sym->name, ret ? "failed" : "end"); 4489 trace_disable(); 4490 4491 if (opts.checksum) 4492 checksum_finish(func); 4493 4494 return ret; 4495 } 4496 4497 static int validate_section(struct objtool_file *file, struct section *sec) 4498 { 4499 struct insn_state state; 4500 struct symbol *func; 4501 int warnings = 0; 4502 4503 sec_for_each_sym(sec, func) { 4504 if (!is_func_sym(func)) 4505 continue; 4506 4507 init_insn_state(file, &state, sec); 4508 set_func_state(&state.cfi); 4509 4510 warnings += validate_symbol(file, sec, func, &state); 4511 } 4512 4513 return warnings; 4514 } 4515 4516 static int validate_noinstr_sections(struct objtool_file *file) 4517 { 4518 struct section *sec; 4519 int warnings = 0; 4520 4521 sec = find_section_by_name(file->elf, ".noinstr.text"); 4522 if (sec) { 4523 warnings += validate_section(file, sec); 4524 warnings += validate_unwind_hints(file, sec); 4525 } 4526 4527 sec = find_section_by_name(file->elf, ".entry.text"); 4528 if (sec) { 4529 warnings += validate_section(file, sec); 4530 warnings += validate_unwind_hints(file, sec); 4531 } 4532 4533 sec = find_section_by_name(file->elf, ".cpuidle.text"); 4534 if (sec) { 4535 warnings += validate_section(file, sec); 4536 warnings += validate_unwind_hints(file, sec); 4537 } 4538 4539 return warnings; 4540 } 4541 4542 static int validate_functions(struct objtool_file *file) 4543 { 4544 struct section *sec; 4545 int warnings = 0; 4546 4547 for_each_sec(file->elf, sec) { 4548 if (!is_text_sec(sec)) 4549 continue; 4550 4551 warnings += validate_section(file, sec); 4552 } 4553 4554 return warnings; 4555 } 4556 4557 static void mark_endbr_used(struct instruction *insn) 4558 { 4559 if (!list_empty(&insn->call_node)) 4560 list_del_init(&insn->call_node); 4561 } 4562 4563 static bool noendbr_range(struct objtool_file *file, struct instruction *insn) 4564 { 4565 struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1); 4566 struct instruction *first; 4567 4568 if (!sym) 4569 return false; 4570 4571 first = find_insn(file, sym->sec, sym->offset); 4572 if (!first) 4573 return false; 4574 4575 if (first->type != INSN_ENDBR && !first->noendbr) 4576 return false; 4577 4578 return insn->offset == sym->offset + sym->len; 4579 } 4580 4581 static int __validate_ibt_insn(struct objtool_file *file, struct instruction *insn, 4582 struct instruction *dest) 4583 { 4584 if (dest->type == INSN_ENDBR) { 4585 mark_endbr_used(dest); 4586 return 0; 4587 } 4588 4589 if (insn_func(dest) && insn_func(insn) && 4590 insn_func(dest)->pfunc == insn_func(insn)->pfunc) { 4591 /* 4592 * Anything from->to self is either _THIS_IP_ or 4593 * IRET-to-self. 4594 * 4595 * There is no sane way to annotate _THIS_IP_ since the 4596 * compiler treats the relocation as a constant and is 4597 * happy to fold in offsets, skewing any annotation we 4598 * do, leading to vast amounts of false-positives. 4599 * 4600 * There's also compiler generated _THIS_IP_ through 4601 * KCOV and such which we have no hope of annotating. 4602 * 4603 * As such, blanket accept self-references without 4604 * issue. 4605 */ 4606 return 0; 4607 } 4608 4609 /* 4610 * Accept anything ANNOTATE_NOENDBR. 4611 */ 4612 if (dest->noendbr) 4613 return 0; 4614 4615 /* 4616 * Accept if this is the instruction after a symbol 4617 * that is (no)endbr -- typical code-range usage. 4618 */ 4619 if (noendbr_range(file, dest)) 4620 return 0; 4621 4622 WARN_INSN(insn, "relocation to !ENDBR: %s", offstr(dest->sec, dest->offset)); 4623 return 1; 4624 } 4625 4626 static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn) 4627 { 4628 struct instruction *dest; 4629 struct reloc *reloc; 4630 unsigned long off; 4631 int warnings = 0; 4632 4633 /* 4634 * Looking for function pointer load relocations. Ignore 4635 * direct/indirect branches: 4636 */ 4637 switch (insn->type) { 4638 4639 case INSN_CALL: 4640 case INSN_CALL_DYNAMIC: 4641 case INSN_JUMP_CONDITIONAL: 4642 case INSN_JUMP_UNCONDITIONAL: 4643 case INSN_JUMP_DYNAMIC: 4644 case INSN_JUMP_DYNAMIC_CONDITIONAL: 4645 case INSN_RETURN: 4646 case INSN_NOP: 4647 return 0; 4648 4649 case INSN_LEA_RIP: 4650 if (!insn_reloc(file, insn)) { 4651 /* local function pointer reference without reloc */ 4652 4653 off = arch_jump_destination(insn); 4654 4655 dest = find_insn(file, insn->sec, off); 4656 if (!dest) { 4657 WARN_INSN(insn, "corrupt function pointer reference"); 4658 return 1; 4659 } 4660 4661 return __validate_ibt_insn(file, insn, dest); 4662 } 4663 break; 4664 4665 default: 4666 break; 4667 } 4668 4669 for (reloc = insn_reloc(file, insn); 4670 reloc; 4671 reloc = find_reloc_by_dest_range(file->elf, insn->sec, 4672 reloc_offset(reloc) + 1, 4673 (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) { 4674 4675 off = reloc->sym->offset + arch_insn_adjusted_addend(insn, reloc); 4676 4677 dest = find_insn(file, reloc->sym->sec, off); 4678 if (!dest) 4679 continue; 4680 4681 warnings += __validate_ibt_insn(file, insn, dest); 4682 } 4683 4684 return warnings; 4685 } 4686 4687 static int validate_ibt_data_reloc(struct objtool_file *file, 4688 struct reloc *reloc) 4689 { 4690 struct instruction *dest; 4691 4692 dest = find_insn(file, reloc->sym->sec, 4693 reloc->sym->offset + reloc_addend(reloc)); 4694 if (!dest) 4695 return 0; 4696 4697 if (dest->type == INSN_ENDBR) { 4698 mark_endbr_used(dest); 4699 return 0; 4700 } 4701 4702 if (dest->noendbr) 4703 return 0; 4704 4705 WARN_FUNC(reloc->sec->base, reloc_offset(reloc), 4706 "data relocation to !ENDBR: %s", offstr(dest->sec, dest->offset)); 4707 4708 return 1; 4709 } 4710 4711 /* 4712 * Validate IBT rules and remove used ENDBR instructions from the seal list. 4713 * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with 4714 * NOPs) later, in create_ibt_endbr_seal_sections(). 4715 */ 4716 static int validate_ibt(struct objtool_file *file) 4717 { 4718 struct section *sec; 4719 struct reloc *reloc; 4720 struct instruction *insn; 4721 int warnings = 0; 4722 4723 for_each_insn(file, insn) 4724 warnings += validate_ibt_insn(file, insn); 4725 4726 for_each_sec(file->elf, sec) { 4727 4728 /* Already done by validate_ibt_insn() */ 4729 if (is_text_sec(sec)) 4730 continue; 4731 4732 if (!sec->rsec) 4733 continue; 4734 4735 /* 4736 * These sections can reference text addresses, but not with 4737 * the intent to indirect branch to them. 4738 */ 4739 if ((!strncmp(sec->name, ".discard", 8) && 4740 strcmp(sec->name, ".discard.ibt_endbr_noseal")) || 4741 !strncmp(sec->name, ".debug", 6) || 4742 !strcmp(sec->name, ".altinstructions") || 4743 !strcmp(sec->name, ".ibt_endbr_seal") || 4744 !strcmp(sec->name, ".kcfi_traps") || 4745 !strcmp(sec->name, ".orc_unwind_ip") || 4746 !strcmp(sec->name, ".retpoline_sites") || 4747 !strcmp(sec->name, ".smp_locks") || 4748 !strcmp(sec->name, ".static_call_sites") || 4749 !strcmp(sec->name, "_error_injection_whitelist") || 4750 !strcmp(sec->name, "_kprobe_blacklist") || 4751 !strcmp(sec->name, "__bug_table") || 4752 !strcmp(sec->name, "__ex_table") || 4753 !strcmp(sec->name, "__jump_table") || 4754 !strcmp(sec->name, "__klp_funcs") || 4755 !strcmp(sec->name, "__mcount_loc") || 4756 !strcmp(sec->name, ".llvm.call-graph-profile") || 4757 !strcmp(sec->name, ".llvm_bb_addr_map") || 4758 !strcmp(sec->name, "__tracepoints") || 4759 !strcmp(sec->name, "__patchable_function_entries")) 4760 continue; 4761 4762 for_each_reloc(sec->rsec, reloc) 4763 warnings += validate_ibt_data_reloc(file, reloc); 4764 } 4765 4766 return warnings; 4767 } 4768 4769 static int validate_sls(struct objtool_file *file) 4770 { 4771 struct instruction *insn, *next_insn; 4772 int warnings = 0; 4773 4774 for_each_insn(file, insn) { 4775 next_insn = next_insn_same_sec(file, insn); 4776 4777 if (insn->retpoline_safe) 4778 continue; 4779 4780 switch (insn->type) { 4781 case INSN_RETURN: 4782 if (!next_insn || next_insn->type != INSN_TRAP) { 4783 WARN_INSN(insn, "missing int3 after ret"); 4784 warnings++; 4785 } 4786 4787 break; 4788 case INSN_JUMP_DYNAMIC: 4789 if (!next_insn || next_insn->type != INSN_TRAP) { 4790 WARN_INSN(insn, "missing int3 after indirect jump"); 4791 warnings++; 4792 } 4793 break; 4794 default: 4795 break; 4796 } 4797 } 4798 4799 return warnings; 4800 } 4801 4802 static int validate_reachable_instructions(struct objtool_file *file) 4803 { 4804 struct instruction *insn, *prev_insn; 4805 struct symbol *call_dest; 4806 int warnings = 0; 4807 4808 if (file->ignore_unreachables) 4809 return 0; 4810 4811 for_each_insn(file, insn) { 4812 if (insn->visited || ignore_unreachable_insn(file, insn)) 4813 continue; 4814 4815 prev_insn = prev_insn_same_sec(file, insn); 4816 if (prev_insn && prev_insn->dead_end) { 4817 call_dest = insn_call_dest(prev_insn); 4818 if (call_dest) { 4819 WARN_INSN(insn, "%s() missing __noreturn in .c/.h or NORETURN() in noreturns.h", 4820 call_dest->name); 4821 warnings++; 4822 continue; 4823 } 4824 } 4825 4826 WARN_INSN(insn, "unreachable instruction"); 4827 warnings++; 4828 } 4829 4830 return warnings; 4831 } 4832 4833 __weak bool arch_absolute_reloc(struct elf *elf, struct reloc *reloc) 4834 { 4835 unsigned int type = reloc_type(reloc); 4836 size_t sz = elf_addr_size(elf); 4837 4838 return (sz == 8) ? (type == R_ABS64) : (type == R_ABS32); 4839 } 4840 4841 static int check_abs_references(struct objtool_file *file) 4842 { 4843 struct section *sec; 4844 struct reloc *reloc; 4845 int ret = 0; 4846 4847 for_each_sec(file->elf, sec) { 4848 /* absolute references in non-loadable sections are fine */ 4849 if (!(sec->sh.sh_flags & SHF_ALLOC)) 4850 continue; 4851 4852 /* section must have an associated .rela section */ 4853 if (!sec->rsec) 4854 continue; 4855 4856 /* 4857 * Special case for compiler generated metadata that is not 4858 * consumed until after boot. 4859 */ 4860 if (!strcmp(sec->name, "__patchable_function_entries")) 4861 continue; 4862 4863 for_each_reloc(sec->rsec, reloc) { 4864 if (arch_absolute_reloc(file->elf, reloc)) { 4865 WARN("section %s has absolute relocation at offset 0x%llx", 4866 sec->name, (unsigned long long)reloc_offset(reloc)); 4867 ret++; 4868 } 4869 } 4870 } 4871 return ret; 4872 } 4873 4874 struct insn_chunk { 4875 void *addr; 4876 struct insn_chunk *next; 4877 }; 4878 4879 /* 4880 * Reduce peak RSS usage by freeing insns memory before writing the ELF file, 4881 * which can trigger more allocations for .debug_* sections whose data hasn't 4882 * been read yet. 4883 */ 4884 static void free_insns(struct objtool_file *file) 4885 { 4886 struct instruction *insn; 4887 struct insn_chunk *chunks = NULL, *chunk; 4888 4889 for_each_insn(file, insn) { 4890 if (!insn->idx) { 4891 chunk = malloc(sizeof(*chunk)); 4892 chunk->addr = insn; 4893 chunk->next = chunks; 4894 chunks = chunk; 4895 } 4896 } 4897 4898 for (chunk = chunks; chunk; chunk = chunk->next) 4899 free(chunk->addr); 4900 } 4901 4902 const char *objtool_disas_insn(struct instruction *insn) 4903 { 4904 struct disas_context *dctx = objtool_disas_ctx; 4905 4906 if (!dctx) 4907 return ""; 4908 4909 disas_insn(dctx, insn); 4910 return disas_result(dctx); 4911 } 4912 4913 int check(struct objtool_file *file) 4914 { 4915 struct disas_context *disas_ctx = NULL; 4916 int ret = 0, warnings = 0; 4917 4918 /* 4919 * If the verbose or backtrace option is used then we need a 4920 * disassembly context to disassemble instruction or function 4921 * on warning or backtrace. 4922 */ 4923 if (opts.verbose || opts.backtrace || opts.trace) { 4924 disas_ctx = disas_context_create(file); 4925 if (!disas_ctx) 4926 opts.trace = false; 4927 objtool_disas_ctx = disas_ctx; 4928 } 4929 4930 arch_initial_func_cfi_state(&initial_func_cfi); 4931 init_cfi_state(&init_cfi); 4932 init_cfi_state(&func_cfi); 4933 set_func_state(&func_cfi); 4934 init_cfi_state(&force_undefined_cfi); 4935 force_undefined_cfi.force_undefined = true; 4936 4937 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) { 4938 ret = -1; 4939 goto out; 4940 } 4941 4942 cfi_hash_add(&init_cfi); 4943 cfi_hash_add(&func_cfi); 4944 4945 ret = checksum_debug_init(file); 4946 if (ret) 4947 goto out; 4948 4949 ret = decode_sections(file); 4950 if (ret) 4951 goto out; 4952 4953 if (!nr_insns) 4954 goto out; 4955 4956 if (opts.retpoline) 4957 warnings += validate_retpoline(file); 4958 4959 if (validate_branch_enabled()) { 4960 int w = 0; 4961 4962 w += validate_functions(file); 4963 w += validate_unwind_hints(file, NULL); 4964 if (!w) 4965 w += validate_reachable_instructions(file); 4966 4967 warnings += w; 4968 4969 } else if (opts.noinstr) { 4970 warnings += validate_noinstr_sections(file); 4971 } 4972 4973 if (opts.unret) { 4974 /* 4975 * Must be after validate_branch() and friends, it plays 4976 * further games with insn->visited. 4977 */ 4978 warnings += validate_unrets(file); 4979 } 4980 4981 if (opts.ibt) 4982 warnings += validate_ibt(file); 4983 4984 if (opts.sls) 4985 warnings += validate_sls(file); 4986 4987 if (opts.static_call) { 4988 ret = create_static_call_sections(file); 4989 if (ret) 4990 goto out; 4991 } 4992 4993 if (opts.retpoline) { 4994 ret = create_retpoline_sites_sections(file); 4995 if (ret) 4996 goto out; 4997 } 4998 4999 if (opts.cfi) { 5000 ret = create_cfi_sections(file); 5001 if (ret) 5002 goto out; 5003 } 5004 5005 if (opts.rethunk) { 5006 ret = create_return_sites_sections(file); 5007 if (ret) 5008 goto out; 5009 5010 if (opts.hack_skylake) { 5011 ret = create_direct_call_sections(file); 5012 if (ret) 5013 goto out; 5014 } 5015 } 5016 5017 if (opts.mcount) { 5018 ret = create_mcount_loc_sections(file); 5019 if (ret) 5020 goto out; 5021 } 5022 5023 if (opts.prefix) { 5024 ret = create_prefix_symbols(file); 5025 if (ret) 5026 goto out; 5027 } 5028 5029 if (opts.ibt) { 5030 ret = create_ibt_endbr_seal_sections(file); 5031 if (ret) 5032 goto out; 5033 } 5034 5035 if (opts.noabs) 5036 warnings += check_abs_references(file); 5037 5038 if (opts.checksum) { 5039 ret = create_sym_checksum_section(file); 5040 if (ret) 5041 goto out; 5042 } 5043 5044 if (opts.orc && nr_insns) { 5045 ret = orc_create(file); 5046 if (ret) 5047 goto out; 5048 } 5049 5050 if (opts.stats) { 5051 printf("nr_insns_visited: %ld\n", nr_insns_visited); 5052 printf("nr_cfi: %ld\n", nr_cfi); 5053 printf("nr_cfi_reused: %ld\n", nr_cfi_reused); 5054 printf("nr_cfi_cache: %ld\n", nr_cfi_cache); 5055 } 5056 5057 out: 5058 if (!ret && !warnings) { 5059 free_insns(file); 5060 return 0; 5061 } 5062 5063 if (opts.werror && warnings) 5064 ret = 1; 5065 5066 if (opts.verbose) { 5067 if (opts.werror && warnings) 5068 WARN("%d warning(s) upgraded to errors", warnings); 5069 disas_warned_funcs(disas_ctx); 5070 } 5071 5072 if (disas_ctx) { 5073 disas_context_destroy(disas_ctx); 5074 objtool_disas_ctx = NULL; 5075 } 5076 5077 free_insns(file); 5078 5079 if (opts.backup && make_backup()) 5080 return 1; 5081 5082 return ret; 5083 } 5084