1 /* 2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <string.h> 19 #include <stdlib.h> 20 21 #include "check.h" 22 #include "elf.h" 23 #include "special.h" 24 #include "arch.h" 25 #include "warn.h" 26 27 #include <linux/hashtable.h> 28 #include <linux/kernel.h> 29 30 struct alternative { 31 struct list_head list; 32 struct instruction *insn; 33 }; 34 35 const char *objname; 36 static bool nofp; 37 struct cfi_state initial_func_cfi; 38 39 static struct instruction *find_insn(struct objtool_file *file, 40 struct section *sec, unsigned long offset) 41 { 42 struct instruction *insn; 43 44 hash_for_each_possible(file->insn_hash, insn, hash, offset) 45 if (insn->sec == sec && insn->offset == offset) 46 return insn; 47 48 return NULL; 49 } 50 51 static struct instruction *next_insn_same_sec(struct objtool_file *file, 52 struct instruction *insn) 53 { 54 struct instruction *next = list_next_entry(insn, list); 55 56 if (!next || &next->list == &file->insn_list || next->sec != insn->sec) 57 return NULL; 58 59 return next; 60 } 61 62 static bool gcov_enabled(struct objtool_file *file) 63 { 64 struct section *sec; 65 struct symbol *sym; 66 67 for_each_sec(file, sec) 68 list_for_each_entry(sym, &sec->symbol_list, list) 69 if (!strncmp(sym->name, "__gcov_.", 8)) 70 return true; 71 72 return false; 73 } 74 75 #define func_for_each_insn(file, func, insn) \ 76 for (insn = find_insn(file, func->sec, func->offset); \ 77 insn && &insn->list != &file->insn_list && \ 78 insn->sec == func->sec && \ 79 insn->offset < func->offset + func->len; \ 80 insn = list_next_entry(insn, list)) 81 82 #define func_for_each_insn_continue_reverse(file, func, insn) \ 83 for (insn = list_prev_entry(insn, list); \ 84 &insn->list != &file->insn_list && \ 85 insn->sec == func->sec && insn->offset >= func->offset; \ 86 insn = list_prev_entry(insn, list)) 87 88 #define sec_for_each_insn_from(file, insn) \ 89 for (; insn; insn = next_insn_same_sec(file, insn)) 90 91 #define sec_for_each_insn_continue(file, insn) \ 92 for (insn = next_insn_same_sec(file, insn); insn; \ 93 insn = next_insn_same_sec(file, insn)) 94 95 /* 96 * Check if the function has been manually whitelisted with the 97 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted 98 * due to its use of a context switching instruction. 99 */ 100 static bool ignore_func(struct objtool_file *file, struct symbol *func) 101 { 102 struct rela *rela; 103 104 /* check for STACK_FRAME_NON_STANDARD */ 105 if (file->whitelist && file->whitelist->rela) 106 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { 107 if (rela->sym->type == STT_SECTION && 108 rela->sym->sec == func->sec && 109 rela->addend == func->offset) 110 return true; 111 if (rela->sym->type == STT_FUNC && rela->sym == func) 112 return true; 113 } 114 115 return false; 116 } 117 118 /* 119 * This checks to see if the given function is a "noreturn" function. 120 * 121 * For global functions which are outside the scope of this object file, we 122 * have to keep a manual list of them. 123 * 124 * For local functions, we have to detect them manually by simply looking for 125 * the lack of a return instruction. 126 * 127 * Returns: 128 * -1: error 129 * 0: no dead end 130 * 1: dead end 131 */ 132 static int __dead_end_function(struct objtool_file *file, struct symbol *func, 133 int recursion) 134 { 135 int i; 136 struct instruction *insn; 137 bool empty = true; 138 139 /* 140 * Unfortunately these have to be hard coded because the noreturn 141 * attribute isn't provided in ELF data. 142 */ 143 static const char * const global_noreturns[] = { 144 "__stack_chk_fail", 145 "panic", 146 "do_exit", 147 "do_task_dead", 148 "__module_put_and_exit", 149 "complete_and_exit", 150 "kvm_spurious_fault", 151 "__reiserfs_panic", 152 "lbug_with_loc", 153 "fortify_panic", 154 }; 155 156 if (func->bind == STB_WEAK) 157 return 0; 158 159 if (func->bind == STB_GLOBAL) 160 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) 161 if (!strcmp(func->name, global_noreturns[i])) 162 return 1; 163 164 if (!func->sec) 165 return 0; 166 167 func_for_each_insn(file, func, insn) { 168 empty = false; 169 170 if (insn->type == INSN_RETURN) 171 return 0; 172 } 173 174 if (empty) 175 return 0; 176 177 /* 178 * A function can have a sibling call instead of a return. In that 179 * case, the function's dead-end status depends on whether the target 180 * of the sibling call returns. 181 */ 182 func_for_each_insn(file, func, insn) { 183 if (insn->sec != func->sec || 184 insn->offset >= func->offset + func->len) 185 break; 186 187 if (insn->type == INSN_JUMP_UNCONDITIONAL) { 188 struct instruction *dest = insn->jump_dest; 189 struct symbol *dest_func; 190 191 if (!dest) 192 /* sibling call to another file */ 193 return 0; 194 195 if (dest->sec != func->sec || 196 dest->offset < func->offset || 197 dest->offset >= func->offset + func->len) { 198 /* local sibling call */ 199 dest_func = find_symbol_by_offset(dest->sec, 200 dest->offset); 201 if (!dest_func) 202 continue; 203 204 if (recursion == 5) { 205 WARN_FUNC("infinite recursion (objtool bug!)", 206 dest->sec, dest->offset); 207 return -1; 208 } 209 210 return __dead_end_function(file, dest_func, 211 recursion + 1); 212 } 213 } 214 215 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) 216 /* sibling call */ 217 return 0; 218 } 219 220 return 1; 221 } 222 223 static int dead_end_function(struct objtool_file *file, struct symbol *func) 224 { 225 return __dead_end_function(file, func, 0); 226 } 227 228 static void clear_insn_state(struct insn_state *state) 229 { 230 int i; 231 232 memset(state, 0, sizeof(*state)); 233 state->cfa.base = CFI_UNDEFINED; 234 for (i = 0; i < CFI_NUM_REGS; i++) 235 state->regs[i].base = CFI_UNDEFINED; 236 state->drap_reg = CFI_UNDEFINED; 237 } 238 239 /* 240 * Call the arch-specific instruction decoder for all the instructions and add 241 * them to the global instruction list. 242 */ 243 static int decode_instructions(struct objtool_file *file) 244 { 245 struct section *sec; 246 struct symbol *func; 247 unsigned long offset; 248 struct instruction *insn; 249 int ret; 250 251 for_each_sec(file, sec) { 252 253 if (!(sec->sh.sh_flags & SHF_EXECINSTR)) 254 continue; 255 256 for (offset = 0; offset < sec->len; offset += insn->len) { 257 insn = malloc(sizeof(*insn)); 258 if (!insn) { 259 WARN("malloc failed"); 260 return -1; 261 } 262 memset(insn, 0, sizeof(*insn)); 263 INIT_LIST_HEAD(&insn->alts); 264 clear_insn_state(&insn->state); 265 266 insn->sec = sec; 267 insn->offset = offset; 268 269 ret = arch_decode_instruction(file->elf, sec, offset, 270 sec->len - offset, 271 &insn->len, &insn->type, 272 &insn->immediate, 273 &insn->stack_op); 274 if (ret) 275 return ret; 276 277 if (!insn->type || insn->type > INSN_LAST) { 278 WARN_FUNC("invalid instruction type %d", 279 insn->sec, insn->offset, insn->type); 280 return -1; 281 } 282 283 hash_add(file->insn_hash, &insn->hash, insn->offset); 284 list_add_tail(&insn->list, &file->insn_list); 285 } 286 287 list_for_each_entry(func, &sec->symbol_list, list) { 288 if (func->type != STT_FUNC) 289 continue; 290 291 if (!find_insn(file, sec, func->offset)) { 292 WARN("%s(): can't find starting instruction", 293 func->name); 294 return -1; 295 } 296 297 func_for_each_insn(file, func, insn) 298 if (!insn->func) 299 insn->func = func; 300 } 301 } 302 303 return 0; 304 } 305 306 /* 307 * Find all uses of the unreachable() macro, which are code path dead ends. 308 */ 309 static int add_dead_ends(struct objtool_file *file) 310 { 311 struct section *sec; 312 struct rela *rela; 313 struct instruction *insn; 314 bool found; 315 316 sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); 317 if (!sec) 318 return 0; 319 320 list_for_each_entry(rela, &sec->rela_list, list) { 321 if (rela->sym->type != STT_SECTION) { 322 WARN("unexpected relocation symbol type in %s", sec->name); 323 return -1; 324 } 325 insn = find_insn(file, rela->sym->sec, rela->addend); 326 if (insn) 327 insn = list_prev_entry(insn, list); 328 else if (rela->addend == rela->sym->sec->len) { 329 found = false; 330 list_for_each_entry_reverse(insn, &file->insn_list, list) { 331 if (insn->sec == rela->sym->sec) { 332 found = true; 333 break; 334 } 335 } 336 337 if (!found) { 338 WARN("can't find unreachable insn at %s+0x%x", 339 rela->sym->sec->name, rela->addend); 340 return -1; 341 } 342 } else { 343 WARN("can't find unreachable insn at %s+0x%x", 344 rela->sym->sec->name, rela->addend); 345 return -1; 346 } 347 348 insn->dead_end = true; 349 } 350 351 return 0; 352 } 353 354 /* 355 * Warnings shouldn't be reported for ignored functions. 356 */ 357 static void add_ignores(struct objtool_file *file) 358 { 359 struct instruction *insn; 360 struct section *sec; 361 struct symbol *func; 362 363 for_each_sec(file, sec) { 364 list_for_each_entry(func, &sec->symbol_list, list) { 365 if (func->type != STT_FUNC) 366 continue; 367 368 if (!ignore_func(file, func)) 369 continue; 370 371 func_for_each_insn(file, func, insn) 372 insn->ignore = true; 373 } 374 } 375 } 376 377 /* 378 * Find the destination instructions for all jumps. 379 */ 380 static int add_jump_destinations(struct objtool_file *file) 381 { 382 struct instruction *insn; 383 struct rela *rela; 384 struct section *dest_sec; 385 unsigned long dest_off; 386 387 for_each_insn(file, insn) { 388 if (insn->type != INSN_JUMP_CONDITIONAL && 389 insn->type != INSN_JUMP_UNCONDITIONAL) 390 continue; 391 392 if (insn->ignore) 393 continue; 394 395 rela = find_rela_by_dest_range(insn->sec, insn->offset, 396 insn->len); 397 if (!rela) { 398 dest_sec = insn->sec; 399 dest_off = insn->offset + insn->len + insn->immediate; 400 } else if (rela->sym->type == STT_SECTION) { 401 dest_sec = rela->sym->sec; 402 dest_off = rela->addend + 4; 403 } else if (rela->sym->sec->idx) { 404 dest_sec = rela->sym->sec; 405 dest_off = rela->sym->sym.st_value + rela->addend + 4; 406 } else { 407 /* sibling call */ 408 insn->jump_dest = 0; 409 continue; 410 } 411 412 insn->jump_dest = find_insn(file, dest_sec, dest_off); 413 if (!insn->jump_dest) { 414 415 /* 416 * This is a special case where an alt instruction 417 * jumps past the end of the section. These are 418 * handled later in handle_group_alt(). 419 */ 420 if (!strcmp(insn->sec->name, ".altinstr_replacement")) 421 continue; 422 423 WARN_FUNC("can't find jump dest instruction at %s+0x%lx", 424 insn->sec, insn->offset, dest_sec->name, 425 dest_off); 426 return -1; 427 } 428 } 429 430 return 0; 431 } 432 433 /* 434 * Find the destination instructions for all calls. 435 */ 436 static int add_call_destinations(struct objtool_file *file) 437 { 438 struct instruction *insn; 439 unsigned long dest_off; 440 struct rela *rela; 441 442 for_each_insn(file, insn) { 443 if (insn->type != INSN_CALL) 444 continue; 445 446 rela = find_rela_by_dest_range(insn->sec, insn->offset, 447 insn->len); 448 if (!rela) { 449 dest_off = insn->offset + insn->len + insn->immediate; 450 insn->call_dest = find_symbol_by_offset(insn->sec, 451 dest_off); 452 if (!insn->call_dest) { 453 WARN_FUNC("can't find call dest symbol at offset 0x%lx", 454 insn->sec, insn->offset, dest_off); 455 return -1; 456 } 457 } else if (rela->sym->type == STT_SECTION) { 458 insn->call_dest = find_symbol_by_offset(rela->sym->sec, 459 rela->addend+4); 460 if (!insn->call_dest || 461 insn->call_dest->type != STT_FUNC) { 462 WARN_FUNC("can't find call dest symbol at %s+0x%x", 463 insn->sec, insn->offset, 464 rela->sym->sec->name, 465 rela->addend + 4); 466 return -1; 467 } 468 } else 469 insn->call_dest = rela->sym; 470 } 471 472 return 0; 473 } 474 475 /* 476 * The .alternatives section requires some extra special care, over and above 477 * what other special sections require: 478 * 479 * 1. Because alternatives are patched in-place, we need to insert a fake jump 480 * instruction at the end so that validate_branch() skips all the original 481 * replaced instructions when validating the new instruction path. 482 * 483 * 2. An added wrinkle is that the new instruction length might be zero. In 484 * that case the old instructions are replaced with noops. We simulate that 485 * by creating a fake jump as the only new instruction. 486 * 487 * 3. In some cases, the alternative section includes an instruction which 488 * conditionally jumps to the _end_ of the entry. We have to modify these 489 * jumps' destinations to point back to .text rather than the end of the 490 * entry in .altinstr_replacement. 491 * 492 * 4. It has been requested that we don't validate the !POPCNT feature path 493 * which is a "very very small percentage of machines". 494 */ 495 static int handle_group_alt(struct objtool_file *file, 496 struct special_alt *special_alt, 497 struct instruction *orig_insn, 498 struct instruction **new_insn) 499 { 500 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; 501 unsigned long dest_off; 502 503 last_orig_insn = NULL; 504 insn = orig_insn; 505 sec_for_each_insn_from(file, insn) { 506 if (insn->offset >= special_alt->orig_off + special_alt->orig_len) 507 break; 508 509 if (special_alt->skip_orig) 510 insn->type = INSN_NOP; 511 512 insn->alt_group = true; 513 last_orig_insn = insn; 514 } 515 516 if (!next_insn_same_sec(file, last_orig_insn)) { 517 WARN("%s: don't know how to handle alternatives at end of section", 518 special_alt->orig_sec->name); 519 return -1; 520 } 521 522 fake_jump = malloc(sizeof(*fake_jump)); 523 if (!fake_jump) { 524 WARN("malloc failed"); 525 return -1; 526 } 527 memset(fake_jump, 0, sizeof(*fake_jump)); 528 INIT_LIST_HEAD(&fake_jump->alts); 529 clear_insn_state(&fake_jump->state); 530 531 fake_jump->sec = special_alt->new_sec; 532 fake_jump->offset = -1; 533 fake_jump->type = INSN_JUMP_UNCONDITIONAL; 534 fake_jump->jump_dest = list_next_entry(last_orig_insn, list); 535 fake_jump->ignore = true; 536 537 if (!special_alt->new_len) { 538 *new_insn = fake_jump; 539 return 0; 540 } 541 542 last_new_insn = NULL; 543 insn = *new_insn; 544 sec_for_each_insn_from(file, insn) { 545 if (insn->offset >= special_alt->new_off + special_alt->new_len) 546 break; 547 548 last_new_insn = insn; 549 550 if (insn->type != INSN_JUMP_CONDITIONAL && 551 insn->type != INSN_JUMP_UNCONDITIONAL) 552 continue; 553 554 if (!insn->immediate) 555 continue; 556 557 dest_off = insn->offset + insn->len + insn->immediate; 558 if (dest_off == special_alt->new_off + special_alt->new_len) 559 insn->jump_dest = fake_jump; 560 561 if (!insn->jump_dest) { 562 WARN_FUNC("can't find alternative jump destination", 563 insn->sec, insn->offset); 564 return -1; 565 } 566 } 567 568 if (!last_new_insn) { 569 WARN_FUNC("can't find last new alternative instruction", 570 special_alt->new_sec, special_alt->new_off); 571 return -1; 572 } 573 574 list_add(&fake_jump->list, &last_new_insn->list); 575 576 return 0; 577 } 578 579 /* 580 * A jump table entry can either convert a nop to a jump or a jump to a nop. 581 * If the original instruction is a jump, make the alt entry an effective nop 582 * by just skipping the original instruction. 583 */ 584 static int handle_jump_alt(struct objtool_file *file, 585 struct special_alt *special_alt, 586 struct instruction *orig_insn, 587 struct instruction **new_insn) 588 { 589 if (orig_insn->type == INSN_NOP) 590 return 0; 591 592 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { 593 WARN_FUNC("unsupported instruction at jump label", 594 orig_insn->sec, orig_insn->offset); 595 return -1; 596 } 597 598 *new_insn = list_next_entry(orig_insn, list); 599 return 0; 600 } 601 602 /* 603 * Read all the special sections which have alternate instructions which can be 604 * patched in or redirected to at runtime. Each instruction having alternate 605 * instruction(s) has them added to its insn->alts list, which will be 606 * traversed in validate_branch(). 607 */ 608 static int add_special_section_alts(struct objtool_file *file) 609 { 610 struct list_head special_alts; 611 struct instruction *orig_insn, *new_insn; 612 struct special_alt *special_alt, *tmp; 613 struct alternative *alt; 614 int ret; 615 616 ret = special_get_alts(file->elf, &special_alts); 617 if (ret) 618 return ret; 619 620 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { 621 alt = malloc(sizeof(*alt)); 622 if (!alt) { 623 WARN("malloc failed"); 624 ret = -1; 625 goto out; 626 } 627 628 orig_insn = find_insn(file, special_alt->orig_sec, 629 special_alt->orig_off); 630 if (!orig_insn) { 631 WARN_FUNC("special: can't find orig instruction", 632 special_alt->orig_sec, special_alt->orig_off); 633 ret = -1; 634 goto out; 635 } 636 637 new_insn = NULL; 638 if (!special_alt->group || special_alt->new_len) { 639 new_insn = find_insn(file, special_alt->new_sec, 640 special_alt->new_off); 641 if (!new_insn) { 642 WARN_FUNC("special: can't find new instruction", 643 special_alt->new_sec, 644 special_alt->new_off); 645 ret = -1; 646 goto out; 647 } 648 } 649 650 if (special_alt->group) { 651 ret = handle_group_alt(file, special_alt, orig_insn, 652 &new_insn); 653 if (ret) 654 goto out; 655 } else if (special_alt->jump_or_nop) { 656 ret = handle_jump_alt(file, special_alt, orig_insn, 657 &new_insn); 658 if (ret) 659 goto out; 660 } 661 662 alt->insn = new_insn; 663 list_add_tail(&alt->list, &orig_insn->alts); 664 665 list_del(&special_alt->list); 666 free(special_alt); 667 } 668 669 out: 670 return ret; 671 } 672 673 static int add_switch_table(struct objtool_file *file, struct symbol *func, 674 struct instruction *insn, struct rela *table, 675 struct rela *next_table) 676 { 677 struct rela *rela = table; 678 struct instruction *alt_insn; 679 struct alternative *alt; 680 681 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { 682 if (rela == next_table) 683 break; 684 685 if (rela->sym->sec != insn->sec || 686 rela->addend <= func->offset || 687 rela->addend >= func->offset + func->len) 688 break; 689 690 alt_insn = find_insn(file, insn->sec, rela->addend); 691 if (!alt_insn) { 692 WARN("%s: can't find instruction at %s+0x%x", 693 file->rodata->rela->name, insn->sec->name, 694 rela->addend); 695 return -1; 696 } 697 698 alt = malloc(sizeof(*alt)); 699 if (!alt) { 700 WARN("malloc failed"); 701 return -1; 702 } 703 704 alt->insn = alt_insn; 705 list_add_tail(&alt->list, &insn->alts); 706 } 707 708 return 0; 709 } 710 711 /* 712 * find_switch_table() - Given a dynamic jump, find the switch jump table in 713 * .rodata associated with it. 714 * 715 * There are 3 basic patterns: 716 * 717 * 1. jmpq *[rodata addr](,%reg,8) 718 * 719 * This is the most common case by far. It jumps to an address in a simple 720 * jump table which is stored in .rodata. 721 * 722 * 2. jmpq *[rodata addr](%rip) 723 * 724 * This is caused by a rare GCC quirk, currently only seen in three driver 725 * functions in the kernel, only with certain obscure non-distro configs. 726 * 727 * As part of an optimization, GCC makes a copy of an existing switch jump 728 * table, modifies it, and then hard-codes the jump (albeit with an indirect 729 * jump) to use a single entry in the table. The rest of the jump table and 730 * some of its jump targets remain as dead code. 731 * 732 * In such a case we can just crudely ignore all unreachable instruction 733 * warnings for the entire object file. Ideally we would just ignore them 734 * for the function, but that would require redesigning the code quite a 735 * bit. And honestly that's just not worth doing: unreachable instruction 736 * warnings are of questionable value anyway, and this is such a rare issue. 737 * 738 * 3. mov [rodata addr],%reg1 739 * ... some instructions ... 740 * jmpq *(%reg1,%reg2,8) 741 * 742 * This is a fairly uncommon pattern which is new for GCC 6. As of this 743 * writing, there are 11 occurrences of it in the allmodconfig kernel. 744 * 745 * TODO: Once we have DWARF CFI and smarter instruction decoding logic, 746 * ensure the same register is used in the mov and jump instructions. 747 */ 748 static struct rela *find_switch_table(struct objtool_file *file, 749 struct symbol *func, 750 struct instruction *insn) 751 { 752 struct rela *text_rela, *rodata_rela; 753 struct instruction *orig_insn = insn; 754 755 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); 756 if (text_rela && text_rela->sym == file->rodata->sym) { 757 /* case 1 */ 758 rodata_rela = find_rela_by_dest(file->rodata, 759 text_rela->addend); 760 if (rodata_rela) 761 return rodata_rela; 762 763 /* case 2 */ 764 rodata_rela = find_rela_by_dest(file->rodata, 765 text_rela->addend + 4); 766 if (!rodata_rela) 767 return NULL; 768 file->ignore_unreachables = true; 769 return rodata_rela; 770 } 771 772 /* case 3 */ 773 func_for_each_insn_continue_reverse(file, func, insn) { 774 if (insn->type == INSN_JUMP_DYNAMIC) 775 break; 776 777 /* allow small jumps within the range */ 778 if (insn->type == INSN_JUMP_UNCONDITIONAL && 779 insn->jump_dest && 780 (insn->jump_dest->offset <= insn->offset || 781 insn->jump_dest->offset > orig_insn->offset)) 782 break; 783 784 /* look for a relocation which references .rodata */ 785 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, 786 insn->len); 787 if (!text_rela || text_rela->sym != file->rodata->sym) 788 continue; 789 790 /* 791 * Make sure the .rodata address isn't associated with a 792 * symbol. gcc jump tables are anonymous data. 793 */ 794 if (find_symbol_containing(file->rodata, text_rela->addend)) 795 continue; 796 797 return find_rela_by_dest(file->rodata, text_rela->addend); 798 } 799 800 return NULL; 801 } 802 803 static int add_func_switch_tables(struct objtool_file *file, 804 struct symbol *func) 805 { 806 struct instruction *insn, *prev_jump = NULL; 807 struct rela *rela, *prev_rela = NULL; 808 int ret; 809 810 func_for_each_insn(file, func, insn) { 811 if (insn->type != INSN_JUMP_DYNAMIC) 812 continue; 813 814 rela = find_switch_table(file, func, insn); 815 if (!rela) 816 continue; 817 818 /* 819 * We found a switch table, but we don't know yet how big it 820 * is. Don't add it until we reach the end of the function or 821 * the beginning of another switch table in the same function. 822 */ 823 if (prev_jump) { 824 ret = add_switch_table(file, func, prev_jump, prev_rela, 825 rela); 826 if (ret) 827 return ret; 828 } 829 830 prev_jump = insn; 831 prev_rela = rela; 832 } 833 834 if (prev_jump) { 835 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL); 836 if (ret) 837 return ret; 838 } 839 840 return 0; 841 } 842 843 /* 844 * For some switch statements, gcc generates a jump table in the .rodata 845 * section which contains a list of addresses within the function to jump to. 846 * This finds these jump tables and adds them to the insn->alts lists. 847 */ 848 static int add_switch_table_alts(struct objtool_file *file) 849 { 850 struct section *sec; 851 struct symbol *func; 852 int ret; 853 854 if (!file->rodata || !file->rodata->rela) 855 return 0; 856 857 for_each_sec(file, sec) { 858 list_for_each_entry(func, &sec->symbol_list, list) { 859 if (func->type != STT_FUNC) 860 continue; 861 862 ret = add_func_switch_tables(file, func); 863 if (ret) 864 return ret; 865 } 866 } 867 868 return 0; 869 } 870 871 static int decode_sections(struct objtool_file *file) 872 { 873 int ret; 874 875 ret = decode_instructions(file); 876 if (ret) 877 return ret; 878 879 ret = add_dead_ends(file); 880 if (ret) 881 return ret; 882 883 add_ignores(file); 884 885 ret = add_jump_destinations(file); 886 if (ret) 887 return ret; 888 889 ret = add_call_destinations(file); 890 if (ret) 891 return ret; 892 893 ret = add_special_section_alts(file); 894 if (ret) 895 return ret; 896 897 ret = add_switch_table_alts(file); 898 if (ret) 899 return ret; 900 901 return 0; 902 } 903 904 static bool is_fentry_call(struct instruction *insn) 905 { 906 if (insn->type == INSN_CALL && 907 insn->call_dest->type == STT_NOTYPE && 908 !strcmp(insn->call_dest->name, "__fentry__")) 909 return true; 910 911 return false; 912 } 913 914 static bool has_modified_stack_frame(struct insn_state *state) 915 { 916 int i; 917 918 if (state->cfa.base != initial_func_cfi.cfa.base || 919 state->cfa.offset != initial_func_cfi.cfa.offset || 920 state->stack_size != initial_func_cfi.cfa.offset || 921 state->drap) 922 return true; 923 924 for (i = 0; i < CFI_NUM_REGS; i++) 925 if (state->regs[i].base != initial_func_cfi.regs[i].base || 926 state->regs[i].offset != initial_func_cfi.regs[i].offset) 927 return true; 928 929 return false; 930 } 931 932 static bool has_valid_stack_frame(struct insn_state *state) 933 { 934 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA && 935 state->regs[CFI_BP].offset == -16) 936 return true; 937 938 if (state->drap && state->regs[CFI_BP].base == CFI_BP) 939 return true; 940 941 return false; 942 } 943 944 static void save_reg(struct insn_state *state, unsigned char reg, int base, 945 int offset) 946 { 947 if ((arch_callee_saved_reg(reg) || 948 (state->drap && reg == state->drap_reg)) && 949 state->regs[reg].base == CFI_UNDEFINED) { 950 state->regs[reg].base = base; 951 state->regs[reg].offset = offset; 952 } 953 } 954 955 static void restore_reg(struct insn_state *state, unsigned char reg) 956 { 957 state->regs[reg].base = CFI_UNDEFINED; 958 state->regs[reg].offset = 0; 959 } 960 961 /* 962 * A note about DRAP stack alignment: 963 * 964 * GCC has the concept of a DRAP register, which is used to help keep track of 965 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP 966 * register. The typical DRAP pattern is: 967 * 968 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10 969 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp 970 * 41 ff 72 f8 pushq -0x8(%r10) 971 * 55 push %rbp 972 * 48 89 e5 mov %rsp,%rbp 973 * (more pushes) 974 * 41 52 push %r10 975 * ... 976 * 41 5a pop %r10 977 * (more pops) 978 * 5d pop %rbp 979 * 49 8d 62 f8 lea -0x8(%r10),%rsp 980 * c3 retq 981 * 982 * There are some variations in the epilogues, like: 983 * 984 * 5b pop %rbx 985 * 41 5a pop %r10 986 * 41 5c pop %r12 987 * 41 5d pop %r13 988 * 41 5e pop %r14 989 * c9 leaveq 990 * 49 8d 62 f8 lea -0x8(%r10),%rsp 991 * c3 retq 992 * 993 * and: 994 * 995 * 4c 8b 55 e8 mov -0x18(%rbp),%r10 996 * 48 8b 5d e0 mov -0x20(%rbp),%rbx 997 * 4c 8b 65 f0 mov -0x10(%rbp),%r12 998 * 4c 8b 6d f8 mov -0x8(%rbp),%r13 999 * c9 leaveq 1000 * 49 8d 62 f8 lea -0x8(%r10),%rsp 1001 * c3 retq 1002 * 1003 * Sometimes r13 is used as the DRAP register, in which case it's saved and 1004 * restored beforehand: 1005 * 1006 * 41 55 push %r13 1007 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13 1008 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 1009 * ... 1010 * 49 8d 65 f0 lea -0x10(%r13),%rsp 1011 * 41 5d pop %r13 1012 * c3 retq 1013 */ 1014 static int update_insn_state(struct instruction *insn, struct insn_state *state) 1015 { 1016 struct stack_op *op = &insn->stack_op; 1017 struct cfi_reg *cfa = &state->cfa; 1018 struct cfi_reg *regs = state->regs; 1019 1020 /* stack operations don't make sense with an undefined CFA */ 1021 if (cfa->base == CFI_UNDEFINED) { 1022 if (insn->func) { 1023 WARN_FUNC("undefined stack state", insn->sec, insn->offset); 1024 return -1; 1025 } 1026 return 0; 1027 } 1028 1029 switch (op->dest.type) { 1030 1031 case OP_DEST_REG: 1032 switch (op->src.type) { 1033 1034 case OP_SRC_REG: 1035 if (cfa->base == op->src.reg && cfa->base == CFI_SP && 1036 op->dest.reg == CFI_BP && regs[CFI_BP].base == CFI_CFA && 1037 regs[CFI_BP].offset == -cfa->offset) { 1038 1039 /* mov %rsp, %rbp */ 1040 cfa->base = op->dest.reg; 1041 state->bp_scratch = false; 1042 } else if (state->drap) { 1043 1044 /* drap: mov %rsp, %rbp */ 1045 regs[CFI_BP].base = CFI_BP; 1046 regs[CFI_BP].offset = -state->stack_size; 1047 state->bp_scratch = false; 1048 } else if (!nofp) { 1049 1050 WARN_FUNC("unknown stack-related register move", 1051 insn->sec, insn->offset); 1052 return -1; 1053 } 1054 1055 break; 1056 1057 case OP_SRC_ADD: 1058 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) { 1059 1060 /* add imm, %rsp */ 1061 state->stack_size -= op->src.offset; 1062 if (cfa->base == CFI_SP) 1063 cfa->offset -= op->src.offset; 1064 break; 1065 } 1066 1067 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) { 1068 1069 /* lea disp(%rbp), %rsp */ 1070 state->stack_size = -(op->src.offset + regs[CFI_BP].offset); 1071 break; 1072 } 1073 1074 if (op->dest.reg != CFI_BP && op->src.reg == CFI_SP && 1075 cfa->base == CFI_SP) { 1076 1077 /* drap: lea disp(%rsp), %drap */ 1078 state->drap_reg = op->dest.reg; 1079 break; 1080 } 1081 1082 if (state->drap && op->dest.reg == CFI_SP && 1083 op->src.reg == state->drap_reg) { 1084 1085 /* drap: lea disp(%drap), %rsp */ 1086 cfa->base = CFI_SP; 1087 cfa->offset = state->stack_size = -op->src.offset; 1088 state->drap_reg = CFI_UNDEFINED; 1089 state->drap = false; 1090 break; 1091 } 1092 1093 if (op->dest.reg == state->cfa.base) { 1094 WARN_FUNC("unsupported stack register modification", 1095 insn->sec, insn->offset); 1096 return -1; 1097 } 1098 1099 break; 1100 1101 case OP_SRC_AND: 1102 if (op->dest.reg != CFI_SP || 1103 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) || 1104 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) { 1105 WARN_FUNC("unsupported stack pointer realignment", 1106 insn->sec, insn->offset); 1107 return -1; 1108 } 1109 1110 if (state->drap_reg != CFI_UNDEFINED) { 1111 /* drap: and imm, %rsp */ 1112 cfa->base = state->drap_reg; 1113 cfa->offset = state->stack_size = 0; 1114 state->drap = true; 1115 1116 } 1117 1118 /* 1119 * Older versions of GCC (4.8ish) realign the stack 1120 * without DRAP, with a frame pointer. 1121 */ 1122 1123 break; 1124 1125 case OP_SRC_POP: 1126 if (!state->drap && op->dest.type == OP_DEST_REG && 1127 op->dest.reg == cfa->base) { 1128 1129 /* pop %rbp */ 1130 cfa->base = CFI_SP; 1131 } 1132 1133 if (regs[op->dest.reg].offset == -state->stack_size) { 1134 1135 if (state->drap && cfa->base == CFI_BP_INDIRECT && 1136 op->dest.type == OP_DEST_REG && 1137 op->dest.reg == state->drap_reg) { 1138 1139 /* drap: pop %drap */ 1140 cfa->base = state->drap_reg; 1141 cfa->offset = 0; 1142 } 1143 1144 restore_reg(state, op->dest.reg); 1145 } 1146 1147 state->stack_size -= 8; 1148 if (cfa->base == CFI_SP) 1149 cfa->offset -= 8; 1150 1151 break; 1152 1153 case OP_SRC_REG_INDIRECT: 1154 if (state->drap && op->src.reg == CFI_BP && 1155 op->src.offset == regs[op->dest.reg].offset) { 1156 1157 /* drap: mov disp(%rbp), %reg */ 1158 if (op->dest.reg == state->drap_reg) { 1159 cfa->base = state->drap_reg; 1160 cfa->offset = 0; 1161 } 1162 1163 restore_reg(state, op->dest.reg); 1164 1165 } else if (op->src.reg == cfa->base && 1166 op->src.offset == regs[op->dest.reg].offset + cfa->offset) { 1167 1168 /* mov disp(%rbp), %reg */ 1169 /* mov disp(%rsp), %reg */ 1170 restore_reg(state, op->dest.reg); 1171 } 1172 1173 break; 1174 1175 default: 1176 WARN_FUNC("unknown stack-related instruction", 1177 insn->sec, insn->offset); 1178 return -1; 1179 } 1180 1181 break; 1182 1183 case OP_DEST_PUSH: 1184 state->stack_size += 8; 1185 if (cfa->base == CFI_SP) 1186 cfa->offset += 8; 1187 1188 if (op->src.type != OP_SRC_REG) 1189 break; 1190 1191 if (state->drap) { 1192 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { 1193 1194 /* drap: push %drap */ 1195 cfa->base = CFI_BP_INDIRECT; 1196 cfa->offset = -state->stack_size; 1197 1198 /* save drap so we know when to undefine it */ 1199 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size); 1200 1201 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) { 1202 1203 /* drap: push %rbp */ 1204 state->stack_size = 0; 1205 1206 } else if (regs[op->src.reg].base == CFI_UNDEFINED) { 1207 1208 /* drap: push %reg */ 1209 save_reg(state, op->src.reg, CFI_BP, -state->stack_size); 1210 } 1211 1212 } else { 1213 1214 /* push %reg */ 1215 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size); 1216 } 1217 1218 /* detect when asm code uses rbp as a scratch register */ 1219 if (!nofp && insn->func && op->src.reg == CFI_BP && 1220 cfa->base != CFI_BP) 1221 state->bp_scratch = true; 1222 break; 1223 1224 case OP_DEST_REG_INDIRECT: 1225 1226 if (state->drap) { 1227 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) { 1228 1229 /* drap: mov %drap, disp(%rbp) */ 1230 cfa->base = CFI_BP_INDIRECT; 1231 cfa->offset = op->dest.offset; 1232 1233 /* save drap so we know when to undefine it */ 1234 save_reg(state, op->src.reg, CFI_CFA, op->dest.offset); 1235 } 1236 1237 else if (regs[op->src.reg].base == CFI_UNDEFINED) { 1238 1239 /* drap: mov reg, disp(%rbp) */ 1240 save_reg(state, op->src.reg, CFI_BP, op->dest.offset); 1241 } 1242 1243 } else if (op->dest.reg == cfa->base) { 1244 1245 /* mov reg, disp(%rbp) */ 1246 /* mov reg, disp(%rsp) */ 1247 save_reg(state, op->src.reg, CFI_CFA, 1248 op->dest.offset - state->cfa.offset); 1249 } 1250 1251 break; 1252 1253 case OP_DEST_LEAVE: 1254 if ((!state->drap && cfa->base != CFI_BP) || 1255 (state->drap && cfa->base != state->drap_reg)) { 1256 WARN_FUNC("leave instruction with modified stack frame", 1257 insn->sec, insn->offset); 1258 return -1; 1259 } 1260 1261 /* leave (mov %rbp, %rsp; pop %rbp) */ 1262 1263 state->stack_size = -state->regs[CFI_BP].offset - 8; 1264 restore_reg(state, CFI_BP); 1265 1266 if (!state->drap) { 1267 cfa->base = CFI_SP; 1268 cfa->offset -= 8; 1269 } 1270 1271 break; 1272 1273 case OP_DEST_MEM: 1274 if (op->src.type != OP_SRC_POP) { 1275 WARN_FUNC("unknown stack-related memory operation", 1276 insn->sec, insn->offset); 1277 return -1; 1278 } 1279 1280 /* pop mem */ 1281 state->stack_size -= 8; 1282 if (cfa->base == CFI_SP) 1283 cfa->offset -= 8; 1284 1285 break; 1286 1287 default: 1288 WARN_FUNC("unknown stack-related instruction", 1289 insn->sec, insn->offset); 1290 return -1; 1291 } 1292 1293 return 0; 1294 } 1295 1296 static bool insn_state_match(struct instruction *insn, struct insn_state *state) 1297 { 1298 struct insn_state *state1 = &insn->state, *state2 = state; 1299 int i; 1300 1301 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) { 1302 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d", 1303 insn->sec, insn->offset, 1304 state1->cfa.base, state1->cfa.offset, 1305 state2->cfa.base, state2->cfa.offset); 1306 1307 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) { 1308 for (i = 0; i < CFI_NUM_REGS; i++) { 1309 if (!memcmp(&state1->regs[i], &state2->regs[i], 1310 sizeof(struct cfi_reg))) 1311 continue; 1312 1313 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d", 1314 insn->sec, insn->offset, 1315 i, state1->regs[i].base, state1->regs[i].offset, 1316 i, state2->regs[i].base, state2->regs[i].offset); 1317 break; 1318 } 1319 1320 } else if (state1->drap != state2->drap || 1321 (state1->drap && state1->drap_reg != state2->drap_reg)) { 1322 WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)", 1323 insn->sec, insn->offset, 1324 state1->drap, state1->drap_reg, 1325 state2->drap, state2->drap_reg); 1326 1327 } else 1328 return true; 1329 1330 return false; 1331 } 1332 1333 /* 1334 * Follow the branch starting at the given instruction, and recursively follow 1335 * any other branches (jumps). Meanwhile, track the frame pointer state at 1336 * each instruction and validate all the rules described in 1337 * tools/objtool/Documentation/stack-validation.txt. 1338 */ 1339 static int validate_branch(struct objtool_file *file, struct instruction *first, 1340 struct insn_state state) 1341 { 1342 struct alternative *alt; 1343 struct instruction *insn; 1344 struct section *sec; 1345 struct symbol *func = NULL; 1346 int ret; 1347 1348 insn = first; 1349 sec = insn->sec; 1350 1351 if (insn->alt_group && list_empty(&insn->alts)) { 1352 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", 1353 sec, insn->offset); 1354 return -1; 1355 } 1356 1357 while (1) { 1358 if (file->c_file && insn->func) { 1359 if (func && func != insn->func) { 1360 WARN("%s() falls through to next function %s()", 1361 func->name, insn->func->name); 1362 return 1; 1363 } 1364 } 1365 1366 func = insn->func; 1367 1368 if (insn->visited) { 1369 if (!!insn_state_match(insn, &state)) 1370 return 1; 1371 1372 return 0; 1373 } 1374 1375 insn->state = state; 1376 1377 insn->visited = true; 1378 1379 list_for_each_entry(alt, &insn->alts, list) { 1380 ret = validate_branch(file, alt->insn, state); 1381 if (ret) 1382 return 1; 1383 } 1384 1385 switch (insn->type) { 1386 1387 case INSN_RETURN: 1388 if (func && has_modified_stack_frame(&state)) { 1389 WARN_FUNC("return with modified stack frame", 1390 sec, insn->offset); 1391 return 1; 1392 } 1393 1394 if (state.bp_scratch) { 1395 WARN("%s uses BP as a scratch register", 1396 insn->func->name); 1397 return 1; 1398 } 1399 1400 return 0; 1401 1402 case INSN_CALL: 1403 if (is_fentry_call(insn)) 1404 break; 1405 1406 ret = dead_end_function(file, insn->call_dest); 1407 if (ret == 1) 1408 return 0; 1409 if (ret == -1) 1410 return 1; 1411 1412 /* fallthrough */ 1413 case INSN_CALL_DYNAMIC: 1414 if (!nofp && func && !has_valid_stack_frame(&state)) { 1415 WARN_FUNC("call without frame pointer save/setup", 1416 sec, insn->offset); 1417 return 1; 1418 } 1419 break; 1420 1421 case INSN_JUMP_CONDITIONAL: 1422 case INSN_JUMP_UNCONDITIONAL: 1423 if (insn->jump_dest) { 1424 ret = validate_branch(file, insn->jump_dest, 1425 state); 1426 if (ret) 1427 return 1; 1428 } else if (func && has_modified_stack_frame(&state)) { 1429 WARN_FUNC("sibling call from callable instruction with modified stack frame", 1430 sec, insn->offset); 1431 return 1; 1432 } /* else it's a sibling call */ 1433 1434 if (insn->type == INSN_JUMP_UNCONDITIONAL) 1435 return 0; 1436 1437 break; 1438 1439 case INSN_JUMP_DYNAMIC: 1440 if (func && list_empty(&insn->alts) && 1441 has_modified_stack_frame(&state)) { 1442 WARN_FUNC("sibling call from callable instruction with modified stack frame", 1443 sec, insn->offset); 1444 return 1; 1445 } 1446 1447 return 0; 1448 1449 case INSN_CONTEXT_SWITCH: 1450 if (func) { 1451 WARN_FUNC("unsupported instruction in callable function", 1452 sec, insn->offset); 1453 return 1; 1454 } 1455 return 0; 1456 1457 case INSN_STACK: 1458 if (update_insn_state(insn, &state)) 1459 return -1; 1460 1461 break; 1462 1463 default: 1464 break; 1465 } 1466 1467 if (insn->dead_end) 1468 return 0; 1469 1470 insn = next_insn_same_sec(file, insn); 1471 if (!insn) { 1472 WARN("%s: unexpected end of section", sec->name); 1473 return 1; 1474 } 1475 } 1476 1477 return 0; 1478 } 1479 1480 static bool is_kasan_insn(struct instruction *insn) 1481 { 1482 return (insn->type == INSN_CALL && 1483 !strcmp(insn->call_dest->name, "__asan_handle_no_return")); 1484 } 1485 1486 static bool is_ubsan_insn(struct instruction *insn) 1487 { 1488 return (insn->type == INSN_CALL && 1489 !strcmp(insn->call_dest->name, 1490 "__ubsan_handle_builtin_unreachable")); 1491 } 1492 1493 static bool ignore_unreachable_insn(struct instruction *insn) 1494 { 1495 int i; 1496 1497 if (insn->ignore || insn->type == INSN_NOP) 1498 return true; 1499 1500 /* 1501 * Ignore any unused exceptions. This can happen when a whitelisted 1502 * function has an exception table entry. 1503 */ 1504 if (!strcmp(insn->sec->name, ".fixup")) 1505 return true; 1506 1507 /* 1508 * Check if this (or a subsequent) instruction is related to 1509 * CONFIG_UBSAN or CONFIG_KASAN. 1510 * 1511 * End the search at 5 instructions to avoid going into the weeds. 1512 */ 1513 if (!insn->func) 1514 return false; 1515 for (i = 0; i < 5; i++) { 1516 1517 if (is_kasan_insn(insn) || is_ubsan_insn(insn)) 1518 return true; 1519 1520 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { 1521 insn = insn->jump_dest; 1522 continue; 1523 } 1524 1525 if (insn->offset + insn->len >= insn->func->offset + insn->func->len) 1526 break; 1527 insn = list_next_entry(insn, list); 1528 } 1529 1530 return false; 1531 } 1532 1533 static int validate_functions(struct objtool_file *file) 1534 { 1535 struct section *sec; 1536 struct symbol *func; 1537 struct instruction *insn; 1538 struct insn_state state; 1539 int ret, warnings = 0; 1540 1541 clear_insn_state(&state); 1542 1543 state.cfa = initial_func_cfi.cfa; 1544 memcpy(&state.regs, &initial_func_cfi.regs, 1545 CFI_NUM_REGS * sizeof(struct cfi_reg)); 1546 state.stack_size = initial_func_cfi.cfa.offset; 1547 1548 for_each_sec(file, sec) { 1549 list_for_each_entry(func, &sec->symbol_list, list) { 1550 if (func->type != STT_FUNC) 1551 continue; 1552 1553 insn = find_insn(file, sec, func->offset); 1554 if (!insn || insn->ignore) 1555 continue; 1556 1557 ret = validate_branch(file, insn, state); 1558 warnings += ret; 1559 } 1560 } 1561 1562 return warnings; 1563 } 1564 1565 static int validate_reachable_instructions(struct objtool_file *file) 1566 { 1567 struct instruction *insn; 1568 1569 if (file->ignore_unreachables) 1570 return 0; 1571 1572 for_each_insn(file, insn) { 1573 if (insn->visited || ignore_unreachable_insn(insn)) 1574 continue; 1575 1576 /* 1577 * gcov produces a lot of unreachable instructions. If we get 1578 * an unreachable warning and the file has gcov enabled, just 1579 * ignore it, and all other such warnings for the file. Do 1580 * this here because this is an expensive function. 1581 */ 1582 if (gcov_enabled(file)) 1583 return 0; 1584 1585 WARN_FUNC("unreachable instruction", insn->sec, insn->offset); 1586 return 1; 1587 } 1588 1589 return 0; 1590 } 1591 1592 static void cleanup(struct objtool_file *file) 1593 { 1594 struct instruction *insn, *tmpinsn; 1595 struct alternative *alt, *tmpalt; 1596 1597 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { 1598 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { 1599 list_del(&alt->list); 1600 free(alt); 1601 } 1602 list_del(&insn->list); 1603 hash_del(&insn->hash); 1604 free(insn); 1605 } 1606 elf_close(file->elf); 1607 } 1608 1609 int check(const char *_objname, bool _nofp) 1610 { 1611 struct objtool_file file; 1612 int ret, warnings = 0; 1613 1614 objname = _objname; 1615 nofp = _nofp; 1616 1617 file.elf = elf_open(objname); 1618 if (!file.elf) 1619 return 1; 1620 1621 INIT_LIST_HEAD(&file.insn_list); 1622 hash_init(file.insn_hash); 1623 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); 1624 file.rodata = find_section_by_name(file.elf, ".rodata"); 1625 file.ignore_unreachables = false; 1626 file.c_file = find_section_by_name(file.elf, ".comment"); 1627 1628 arch_initial_func_cfi_state(&initial_func_cfi); 1629 1630 ret = decode_sections(&file); 1631 if (ret < 0) 1632 goto out; 1633 warnings += ret; 1634 1635 if (list_empty(&file.insn_list)) 1636 goto out; 1637 1638 ret = validate_functions(&file); 1639 if (ret < 0) 1640 goto out; 1641 warnings += ret; 1642 1643 if (!warnings) { 1644 ret = validate_reachable_instructions(&file); 1645 if (ret < 0) 1646 goto out; 1647 warnings += ret; 1648 } 1649 1650 out: 1651 cleanup(&file); 1652 1653 /* ignore warnings for now until we get all the code cleaned up */ 1654 if (ret || warnings) 1655 return 0; 1656 return 0; 1657 } 1658