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