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