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