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