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