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