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