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