1 /* 2 * Copyright (C) 2015 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 /* 19 * objtool check: 20 * 21 * This command analyzes every .o file and ensures the validity of its stack 22 * trace metadata. It enforces a set of rules on asm code and C inline 23 * assembly code so that stack traces can be reliable. 24 * 25 * For more information, see tools/objtool/Documentation/stack-validation.txt. 26 */ 27 28 #include <string.h> 29 #include <stdlib.h> 30 #include <subcmd/parse-options.h> 31 32 #include "builtin.h" 33 #include "elf.h" 34 #include "special.h" 35 #include "arch.h" 36 #include "warn.h" 37 38 #include <linux/hashtable.h> 39 40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 41 42 #define STATE_FP_SAVED 0x1 43 #define STATE_FP_SETUP 0x2 44 #define STATE_FENTRY 0x4 45 46 struct instruction { 47 struct list_head list; 48 struct hlist_node hash; 49 struct section *sec; 50 unsigned long offset; 51 unsigned int len, state; 52 unsigned char type; 53 unsigned long immediate; 54 bool alt_group, visited, dead_end; 55 struct symbol *call_dest; 56 struct instruction *jump_dest; 57 struct list_head alts; 58 struct symbol *func; 59 }; 60 61 struct alternative { 62 struct list_head list; 63 struct instruction *insn; 64 }; 65 66 struct objtool_file { 67 struct elf *elf; 68 struct list_head insn_list; 69 DECLARE_HASHTABLE(insn_hash, 16); 70 struct section *rodata, *whitelist; 71 bool ignore_unreachables, c_file; 72 }; 73 74 const char *objname; 75 static bool nofp; 76 77 static struct instruction *find_insn(struct objtool_file *file, 78 struct section *sec, unsigned long offset) 79 { 80 struct instruction *insn; 81 82 hash_for_each_possible(file->insn_hash, insn, hash, offset) 83 if (insn->sec == sec && insn->offset == offset) 84 return insn; 85 86 return NULL; 87 } 88 89 static struct instruction *next_insn_same_sec(struct objtool_file *file, 90 struct instruction *insn) 91 { 92 struct instruction *next = list_next_entry(insn, list); 93 94 if (&next->list == &file->insn_list || next->sec != insn->sec) 95 return NULL; 96 97 return next; 98 } 99 100 static bool gcov_enabled(struct objtool_file *file) 101 { 102 struct section *sec; 103 struct symbol *sym; 104 105 list_for_each_entry(sec, &file->elf->sections, list) 106 list_for_each_entry(sym, &sec->symbol_list, list) 107 if (!strncmp(sym->name, "__gcov_.", 8)) 108 return true; 109 110 return false; 111 } 112 113 #define for_each_insn(file, insn) \ 114 list_for_each_entry(insn, &file->insn_list, list) 115 116 #define func_for_each_insn(file, func, insn) \ 117 for (insn = find_insn(file, func->sec, func->offset); \ 118 insn && &insn->list != &file->insn_list && \ 119 insn->sec == func->sec && \ 120 insn->offset < func->offset + func->len; \ 121 insn = list_next_entry(insn, list)) 122 123 #define func_for_each_insn_continue_reverse(file, func, insn) \ 124 for (insn = list_prev_entry(insn, list); \ 125 &insn->list != &file->insn_list && \ 126 insn->sec == func->sec && insn->offset >= func->offset; \ 127 insn = list_prev_entry(insn, list)) 128 129 #define sec_for_each_insn_from(file, insn) \ 130 for (; insn; insn = next_insn_same_sec(file, insn)) 131 132 133 /* 134 * Check if the function has been manually whitelisted with the 135 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted 136 * due to its use of a context switching instruction. 137 */ 138 static bool ignore_func(struct objtool_file *file, struct symbol *func) 139 { 140 struct rela *rela; 141 struct instruction *insn; 142 143 /* check for STACK_FRAME_NON_STANDARD */ 144 if (file->whitelist && file->whitelist->rela) 145 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) { 146 if (rela->sym->type == STT_SECTION && 147 rela->sym->sec == func->sec && 148 rela->addend == func->offset) 149 return true; 150 if (rela->sym->type == STT_FUNC && rela->sym == func) 151 return true; 152 } 153 154 /* check if it has a context switching instruction */ 155 func_for_each_insn(file, func, insn) 156 if (insn->type == INSN_CONTEXT_SWITCH) 157 return true; 158 159 return false; 160 } 161 162 /* 163 * This checks to see if the given function is a "noreturn" function. 164 * 165 * For global functions which are outside the scope of this object file, we 166 * have to keep a manual list of them. 167 * 168 * For local functions, we have to detect them manually by simply looking for 169 * the lack of a return instruction. 170 * 171 * Returns: 172 * -1: error 173 * 0: no dead end 174 * 1: dead end 175 */ 176 static int __dead_end_function(struct objtool_file *file, struct symbol *func, 177 int recursion) 178 { 179 int i; 180 struct instruction *insn; 181 bool empty = true; 182 183 /* 184 * Unfortunately these have to be hard coded because the noreturn 185 * attribute isn't provided in ELF data. 186 */ 187 static const char * const global_noreturns[] = { 188 "__stack_chk_fail", 189 "panic", 190 "do_exit", 191 "do_task_dead", 192 "__module_put_and_exit", 193 "complete_and_exit", 194 "kvm_spurious_fault", 195 "__reiserfs_panic", 196 "lbug_with_loc" 197 }; 198 199 if (func->bind == STB_WEAK) 200 return 0; 201 202 if (func->bind == STB_GLOBAL) 203 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++) 204 if (!strcmp(func->name, global_noreturns[i])) 205 return 1; 206 207 if (!func->sec) 208 return 0; 209 210 func_for_each_insn(file, func, insn) { 211 empty = false; 212 213 if (insn->type == INSN_RETURN) 214 return 0; 215 } 216 217 if (empty) 218 return 0; 219 220 /* 221 * A function can have a sibling call instead of a return. In that 222 * case, the function's dead-end status depends on whether the target 223 * of the sibling call returns. 224 */ 225 func_for_each_insn(file, func, insn) { 226 if (insn->sec != func->sec || 227 insn->offset >= func->offset + func->len) 228 break; 229 230 if (insn->type == INSN_JUMP_UNCONDITIONAL) { 231 struct instruction *dest = insn->jump_dest; 232 struct symbol *dest_func; 233 234 if (!dest) 235 /* sibling call to another file */ 236 return 0; 237 238 if (dest->sec != func->sec || 239 dest->offset < func->offset || 240 dest->offset >= func->offset + func->len) { 241 /* local sibling call */ 242 dest_func = find_symbol_by_offset(dest->sec, 243 dest->offset); 244 if (!dest_func) 245 continue; 246 247 if (recursion == 5) { 248 WARN_FUNC("infinite recursion (objtool bug!)", 249 dest->sec, dest->offset); 250 return -1; 251 } 252 253 return __dead_end_function(file, dest_func, 254 recursion + 1); 255 } 256 } 257 258 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts)) 259 /* sibling call */ 260 return 0; 261 } 262 263 return 1; 264 } 265 266 static int dead_end_function(struct objtool_file *file, struct symbol *func) 267 { 268 return __dead_end_function(file, func, 0); 269 } 270 271 /* 272 * Call the arch-specific instruction decoder for all the instructions and add 273 * them to the global instruction list. 274 */ 275 static int decode_instructions(struct objtool_file *file) 276 { 277 struct section *sec; 278 struct symbol *func; 279 unsigned long offset; 280 struct instruction *insn; 281 int ret; 282 283 list_for_each_entry(sec, &file->elf->sections, list) { 284 285 if (!(sec->sh.sh_flags & SHF_EXECINSTR)) 286 continue; 287 288 for (offset = 0; offset < sec->len; offset += insn->len) { 289 insn = malloc(sizeof(*insn)); 290 memset(insn, 0, sizeof(*insn)); 291 292 INIT_LIST_HEAD(&insn->alts); 293 insn->sec = sec; 294 insn->offset = offset; 295 296 ret = arch_decode_instruction(file->elf, sec, offset, 297 sec->len - offset, 298 &insn->len, &insn->type, 299 &insn->immediate); 300 if (ret) 301 return ret; 302 303 if (!insn->type || insn->type > INSN_LAST) { 304 WARN_FUNC("invalid instruction type %d", 305 insn->sec, insn->offset, insn->type); 306 return -1; 307 } 308 309 hash_add(file->insn_hash, &insn->hash, insn->offset); 310 list_add_tail(&insn->list, &file->insn_list); 311 } 312 313 list_for_each_entry(func, &sec->symbol_list, list) { 314 if (func->type != STT_FUNC) 315 continue; 316 317 if (!find_insn(file, sec, func->offset)) { 318 WARN("%s(): can't find starting instruction", 319 func->name); 320 return -1; 321 } 322 323 func_for_each_insn(file, func, insn) 324 if (!insn->func) 325 insn->func = func; 326 } 327 } 328 329 return 0; 330 } 331 332 /* 333 * Find all uses of the unreachable() macro, which are code path dead ends. 334 */ 335 static int add_dead_ends(struct objtool_file *file) 336 { 337 struct section *sec; 338 struct rela *rela; 339 struct instruction *insn; 340 bool found; 341 342 sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); 343 if (!sec) 344 return 0; 345 346 list_for_each_entry(rela, &sec->rela_list, list) { 347 if (rela->sym->type != STT_SECTION) { 348 WARN("unexpected relocation symbol type in %s", sec->name); 349 return -1; 350 } 351 insn = find_insn(file, rela->sym->sec, rela->addend); 352 if (insn) 353 insn = list_prev_entry(insn, list); 354 else if (rela->addend == rela->sym->sec->len) { 355 found = false; 356 list_for_each_entry_reverse(insn, &file->insn_list, list) { 357 if (insn->sec == rela->sym->sec) { 358 found = true; 359 break; 360 } 361 } 362 363 if (!found) { 364 WARN("can't find unreachable insn at %s+0x%x", 365 rela->sym->sec->name, rela->addend); 366 return -1; 367 } 368 } else { 369 WARN("can't find unreachable insn at %s+0x%x", 370 rela->sym->sec->name, rela->addend); 371 return -1; 372 } 373 374 insn->dead_end = true; 375 } 376 377 return 0; 378 } 379 380 /* 381 * Warnings shouldn't be reported for ignored functions. 382 */ 383 static void add_ignores(struct objtool_file *file) 384 { 385 struct instruction *insn; 386 struct section *sec; 387 struct symbol *func; 388 389 list_for_each_entry(sec, &file->elf->sections, list) { 390 list_for_each_entry(func, &sec->symbol_list, list) { 391 if (func->type != STT_FUNC) 392 continue; 393 394 if (!ignore_func(file, func)) 395 continue; 396 397 func_for_each_insn(file, func, insn) 398 insn->visited = true; 399 } 400 } 401 } 402 403 /* 404 * Find the destination instructions for all jumps. 405 */ 406 static int add_jump_destinations(struct objtool_file *file) 407 { 408 struct instruction *insn; 409 struct rela *rela; 410 struct section *dest_sec; 411 unsigned long dest_off; 412 413 for_each_insn(file, insn) { 414 if (insn->type != INSN_JUMP_CONDITIONAL && 415 insn->type != INSN_JUMP_UNCONDITIONAL) 416 continue; 417 418 /* skip ignores */ 419 if (insn->visited) 420 continue; 421 422 rela = find_rela_by_dest_range(insn->sec, insn->offset, 423 insn->len); 424 if (!rela) { 425 dest_sec = insn->sec; 426 dest_off = insn->offset + insn->len + insn->immediate; 427 } else if (rela->sym->type == STT_SECTION) { 428 dest_sec = rela->sym->sec; 429 dest_off = rela->addend + 4; 430 } else if (rela->sym->sec->idx) { 431 dest_sec = rela->sym->sec; 432 dest_off = rela->sym->sym.st_value + rela->addend + 4; 433 } else { 434 /* sibling call */ 435 insn->jump_dest = 0; 436 continue; 437 } 438 439 insn->jump_dest = find_insn(file, dest_sec, dest_off); 440 if (!insn->jump_dest) { 441 442 /* 443 * This is a special case where an alt instruction 444 * jumps past the end of the section. These are 445 * handled later in handle_group_alt(). 446 */ 447 if (!strcmp(insn->sec->name, ".altinstr_replacement")) 448 continue; 449 450 WARN_FUNC("can't find jump dest instruction at %s+0x%lx", 451 insn->sec, insn->offset, dest_sec->name, 452 dest_off); 453 return -1; 454 } 455 } 456 457 return 0; 458 } 459 460 /* 461 * Find the destination instructions for all calls. 462 */ 463 static int add_call_destinations(struct objtool_file *file) 464 { 465 struct instruction *insn; 466 unsigned long dest_off; 467 struct rela *rela; 468 469 for_each_insn(file, insn) { 470 if (insn->type != INSN_CALL) 471 continue; 472 473 rela = find_rela_by_dest_range(insn->sec, insn->offset, 474 insn->len); 475 if (!rela) { 476 dest_off = insn->offset + insn->len + insn->immediate; 477 insn->call_dest = find_symbol_by_offset(insn->sec, 478 dest_off); 479 if (!insn->call_dest) { 480 WARN_FUNC("can't find call dest symbol at offset 0x%lx", 481 insn->sec, insn->offset, dest_off); 482 return -1; 483 } 484 } else if (rela->sym->type == STT_SECTION) { 485 insn->call_dest = find_symbol_by_offset(rela->sym->sec, 486 rela->addend+4); 487 if (!insn->call_dest || 488 insn->call_dest->type != STT_FUNC) { 489 WARN_FUNC("can't find call dest symbol at %s+0x%x", 490 insn->sec, insn->offset, 491 rela->sym->sec->name, 492 rela->addend + 4); 493 return -1; 494 } 495 } else 496 insn->call_dest = rela->sym; 497 } 498 499 return 0; 500 } 501 502 /* 503 * The .alternatives section requires some extra special care, over and above 504 * what other special sections require: 505 * 506 * 1. Because alternatives are patched in-place, we need to insert a fake jump 507 * instruction at the end so that validate_branch() skips all the original 508 * replaced instructions when validating the new instruction path. 509 * 510 * 2. An added wrinkle is that the new instruction length might be zero. In 511 * that case the old instructions are replaced with noops. We simulate that 512 * by creating a fake jump as the only new instruction. 513 * 514 * 3. In some cases, the alternative section includes an instruction which 515 * conditionally jumps to the _end_ of the entry. We have to modify these 516 * jumps' destinations to point back to .text rather than the end of the 517 * entry in .altinstr_replacement. 518 * 519 * 4. It has been requested that we don't validate the !POPCNT feature path 520 * which is a "very very small percentage of machines". 521 */ 522 static int handle_group_alt(struct objtool_file *file, 523 struct special_alt *special_alt, 524 struct instruction *orig_insn, 525 struct instruction **new_insn) 526 { 527 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump; 528 unsigned long dest_off; 529 530 last_orig_insn = NULL; 531 insn = orig_insn; 532 sec_for_each_insn_from(file, insn) { 533 if (insn->offset >= special_alt->orig_off + special_alt->orig_len) 534 break; 535 536 if (special_alt->skip_orig) 537 insn->type = INSN_NOP; 538 539 insn->alt_group = true; 540 last_orig_insn = insn; 541 } 542 543 if (!next_insn_same_sec(file, last_orig_insn)) { 544 WARN("%s: don't know how to handle alternatives at end of section", 545 special_alt->orig_sec->name); 546 return -1; 547 } 548 549 fake_jump = malloc(sizeof(*fake_jump)); 550 if (!fake_jump) { 551 WARN("malloc failed"); 552 return -1; 553 } 554 memset(fake_jump, 0, sizeof(*fake_jump)); 555 INIT_LIST_HEAD(&fake_jump->alts); 556 fake_jump->sec = special_alt->new_sec; 557 fake_jump->offset = -1; 558 fake_jump->type = INSN_JUMP_UNCONDITIONAL; 559 fake_jump->jump_dest = list_next_entry(last_orig_insn, list); 560 561 if (!special_alt->new_len) { 562 *new_insn = fake_jump; 563 return 0; 564 } 565 566 last_new_insn = NULL; 567 insn = *new_insn; 568 sec_for_each_insn_from(file, insn) { 569 if (insn->offset >= special_alt->new_off + special_alt->new_len) 570 break; 571 572 last_new_insn = insn; 573 574 if (insn->type != INSN_JUMP_CONDITIONAL && 575 insn->type != INSN_JUMP_UNCONDITIONAL) 576 continue; 577 578 if (!insn->immediate) 579 continue; 580 581 dest_off = insn->offset + insn->len + insn->immediate; 582 if (dest_off == special_alt->new_off + special_alt->new_len) 583 insn->jump_dest = fake_jump; 584 585 if (!insn->jump_dest) { 586 WARN_FUNC("can't find alternative jump destination", 587 insn->sec, insn->offset); 588 return -1; 589 } 590 } 591 592 if (!last_new_insn) { 593 WARN_FUNC("can't find last new alternative instruction", 594 special_alt->new_sec, special_alt->new_off); 595 return -1; 596 } 597 598 list_add(&fake_jump->list, &last_new_insn->list); 599 600 return 0; 601 } 602 603 /* 604 * A jump table entry can either convert a nop to a jump or a jump to a nop. 605 * If the original instruction is a jump, make the alt entry an effective nop 606 * by just skipping the original instruction. 607 */ 608 static int handle_jump_alt(struct objtool_file *file, 609 struct special_alt *special_alt, 610 struct instruction *orig_insn, 611 struct instruction **new_insn) 612 { 613 if (orig_insn->type == INSN_NOP) 614 return 0; 615 616 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) { 617 WARN_FUNC("unsupported instruction at jump label", 618 orig_insn->sec, orig_insn->offset); 619 return -1; 620 } 621 622 *new_insn = list_next_entry(orig_insn, list); 623 return 0; 624 } 625 626 /* 627 * Read all the special sections which have alternate instructions which can be 628 * patched in or redirected to at runtime. Each instruction having alternate 629 * instruction(s) has them added to its insn->alts list, which will be 630 * traversed in validate_branch(). 631 */ 632 static int add_special_section_alts(struct objtool_file *file) 633 { 634 struct list_head special_alts; 635 struct instruction *orig_insn, *new_insn; 636 struct special_alt *special_alt, *tmp; 637 struct alternative *alt; 638 int ret; 639 640 ret = special_get_alts(file->elf, &special_alts); 641 if (ret) 642 return ret; 643 644 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) { 645 alt = malloc(sizeof(*alt)); 646 if (!alt) { 647 WARN("malloc failed"); 648 ret = -1; 649 goto out; 650 } 651 652 orig_insn = find_insn(file, special_alt->orig_sec, 653 special_alt->orig_off); 654 if (!orig_insn) { 655 WARN_FUNC("special: can't find orig instruction", 656 special_alt->orig_sec, special_alt->orig_off); 657 ret = -1; 658 goto out; 659 } 660 661 new_insn = NULL; 662 if (!special_alt->group || special_alt->new_len) { 663 new_insn = find_insn(file, special_alt->new_sec, 664 special_alt->new_off); 665 if (!new_insn) { 666 WARN_FUNC("special: can't find new instruction", 667 special_alt->new_sec, 668 special_alt->new_off); 669 ret = -1; 670 goto out; 671 } 672 } 673 674 if (special_alt->group) { 675 ret = handle_group_alt(file, special_alt, orig_insn, 676 &new_insn); 677 if (ret) 678 goto out; 679 } else if (special_alt->jump_or_nop) { 680 ret = handle_jump_alt(file, special_alt, orig_insn, 681 &new_insn); 682 if (ret) 683 goto out; 684 } 685 686 alt->insn = new_insn; 687 list_add_tail(&alt->list, &orig_insn->alts); 688 689 list_del(&special_alt->list); 690 free(special_alt); 691 } 692 693 out: 694 return ret; 695 } 696 697 static int add_switch_table(struct objtool_file *file, struct symbol *func, 698 struct instruction *insn, struct rela *table, 699 struct rela *next_table) 700 { 701 struct rela *rela = table; 702 struct instruction *alt_insn; 703 struct alternative *alt; 704 705 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) { 706 if (rela == next_table) 707 break; 708 709 if (rela->sym->sec != insn->sec || 710 rela->addend <= func->offset || 711 rela->addend >= func->offset + func->len) 712 break; 713 714 alt_insn = find_insn(file, insn->sec, rela->addend); 715 if (!alt_insn) { 716 WARN("%s: can't find instruction at %s+0x%x", 717 file->rodata->rela->name, insn->sec->name, 718 rela->addend); 719 return -1; 720 } 721 722 alt = malloc(sizeof(*alt)); 723 if (!alt) { 724 WARN("malloc failed"); 725 return -1; 726 } 727 728 alt->insn = alt_insn; 729 list_add_tail(&alt->list, &insn->alts); 730 } 731 732 return 0; 733 } 734 735 /* 736 * find_switch_table() - Given a dynamic jump, find the switch jump table in 737 * .rodata associated with it. 738 * 739 * There are 3 basic patterns: 740 * 741 * 1. jmpq *[rodata addr](,%reg,8) 742 * 743 * This is the most common case by far. It jumps to an address in a simple 744 * jump table which is stored in .rodata. 745 * 746 * 2. jmpq *[rodata addr](%rip) 747 * 748 * This is caused by a rare GCC quirk, currently only seen in three driver 749 * functions in the kernel, only with certain obscure non-distro configs. 750 * 751 * As part of an optimization, GCC makes a copy of an existing switch jump 752 * table, modifies it, and then hard-codes the jump (albeit with an indirect 753 * jump) to use a single entry in the table. The rest of the jump table and 754 * some of its jump targets remain as dead code. 755 * 756 * In such a case we can just crudely ignore all unreachable instruction 757 * warnings for the entire object file. Ideally we would just ignore them 758 * for the function, but that would require redesigning the code quite a 759 * bit. And honestly that's just not worth doing: unreachable instruction 760 * warnings are of questionable value anyway, and this is such a rare issue. 761 * 762 * 3. mov [rodata addr],%reg1 763 * ... some instructions ... 764 * jmpq *(%reg1,%reg2,8) 765 * 766 * This is a fairly uncommon pattern which is new for GCC 6. As of this 767 * writing, there are 11 occurrences of it in the allmodconfig kernel. 768 * 769 * TODO: Once we have DWARF CFI and smarter instruction decoding logic, 770 * ensure the same register is used in the mov and jump instructions. 771 */ 772 static struct rela *find_switch_table(struct objtool_file *file, 773 struct symbol *func, 774 struct instruction *insn) 775 { 776 struct rela *text_rela, *rodata_rela; 777 struct instruction *orig_insn = insn; 778 779 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len); 780 if (text_rela && text_rela->sym == file->rodata->sym) { 781 /* case 1 */ 782 rodata_rela = find_rela_by_dest(file->rodata, 783 text_rela->addend); 784 if (rodata_rela) 785 return rodata_rela; 786 787 /* case 2 */ 788 rodata_rela = find_rela_by_dest(file->rodata, 789 text_rela->addend + 4); 790 if (!rodata_rela) 791 return NULL; 792 file->ignore_unreachables = true; 793 return rodata_rela; 794 } 795 796 /* case 3 */ 797 func_for_each_insn_continue_reverse(file, func, insn) { 798 if (insn->type == INSN_JUMP_DYNAMIC) 799 break; 800 801 /* allow small jumps within the range */ 802 if (insn->type == INSN_JUMP_UNCONDITIONAL && 803 insn->jump_dest && 804 (insn->jump_dest->offset <= insn->offset || 805 insn->jump_dest->offset > orig_insn->offset)) 806 break; 807 808 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, 809 insn->len); 810 if (text_rela && text_rela->sym == file->rodata->sym) 811 return find_rela_by_dest(file->rodata, 812 text_rela->addend); 813 } 814 815 return NULL; 816 } 817 818 static int add_func_switch_tables(struct objtool_file *file, 819 struct symbol *func) 820 { 821 struct instruction *insn, *prev_jump = NULL; 822 struct rela *rela, *prev_rela = NULL; 823 int ret; 824 825 func_for_each_insn(file, func, insn) { 826 if (insn->type != INSN_JUMP_DYNAMIC) 827 continue; 828 829 rela = find_switch_table(file, func, insn); 830 if (!rela) 831 continue; 832 833 /* 834 * We found a switch table, but we don't know yet how big it 835 * is. Don't add it until we reach the end of the function or 836 * the beginning of another switch table in the same function. 837 */ 838 if (prev_jump) { 839 ret = add_switch_table(file, func, prev_jump, prev_rela, 840 rela); 841 if (ret) 842 return ret; 843 } 844 845 prev_jump = insn; 846 prev_rela = rela; 847 } 848 849 if (prev_jump) { 850 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL); 851 if (ret) 852 return ret; 853 } 854 855 return 0; 856 } 857 858 /* 859 * For some switch statements, gcc generates a jump table in the .rodata 860 * section which contains a list of addresses within the function to jump to. 861 * This finds these jump tables and adds them to the insn->alts lists. 862 */ 863 static int add_switch_table_alts(struct objtool_file *file) 864 { 865 struct section *sec; 866 struct symbol *func; 867 int ret; 868 869 if (!file->rodata || !file->rodata->rela) 870 return 0; 871 872 list_for_each_entry(sec, &file->elf->sections, list) { 873 list_for_each_entry(func, &sec->symbol_list, list) { 874 if (func->type != STT_FUNC) 875 continue; 876 877 ret = add_func_switch_tables(file, func); 878 if (ret) 879 return ret; 880 } 881 } 882 883 return 0; 884 } 885 886 static int decode_sections(struct objtool_file *file) 887 { 888 int ret; 889 890 ret = decode_instructions(file); 891 if (ret) 892 return ret; 893 894 ret = add_dead_ends(file); 895 if (ret) 896 return ret; 897 898 add_ignores(file); 899 900 ret = add_jump_destinations(file); 901 if (ret) 902 return ret; 903 904 ret = add_call_destinations(file); 905 if (ret) 906 return ret; 907 908 ret = add_special_section_alts(file); 909 if (ret) 910 return ret; 911 912 ret = add_switch_table_alts(file); 913 if (ret) 914 return ret; 915 916 return 0; 917 } 918 919 static bool is_fentry_call(struct instruction *insn) 920 { 921 if (insn->type == INSN_CALL && 922 insn->call_dest->type == STT_NOTYPE && 923 !strcmp(insn->call_dest->name, "__fentry__")) 924 return true; 925 926 return false; 927 } 928 929 static bool has_modified_stack_frame(struct instruction *insn) 930 { 931 return (insn->state & STATE_FP_SAVED) || 932 (insn->state & STATE_FP_SETUP); 933 } 934 935 static bool has_valid_stack_frame(struct instruction *insn) 936 { 937 return (insn->state & STATE_FP_SAVED) && 938 (insn->state & STATE_FP_SETUP); 939 } 940 941 static unsigned int frame_state(unsigned long state) 942 { 943 return (state & (STATE_FP_SAVED | STATE_FP_SETUP)); 944 } 945 946 /* 947 * Follow the branch starting at the given instruction, and recursively follow 948 * any other branches (jumps). Meanwhile, track the frame pointer state at 949 * each instruction and validate all the rules described in 950 * tools/objtool/Documentation/stack-validation.txt. 951 */ 952 static int validate_branch(struct objtool_file *file, 953 struct instruction *first, unsigned char first_state) 954 { 955 struct alternative *alt; 956 struct instruction *insn; 957 struct section *sec; 958 struct symbol *func = NULL; 959 unsigned char state; 960 int ret; 961 962 insn = first; 963 sec = insn->sec; 964 state = first_state; 965 966 if (insn->alt_group && list_empty(&insn->alts)) { 967 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group", 968 sec, insn->offset); 969 return 1; 970 } 971 972 while (1) { 973 if (file->c_file && insn->func) { 974 if (func && func != insn->func) { 975 WARN("%s() falls through to next function %s()", 976 func->name, insn->func->name); 977 return 1; 978 } 979 980 func = insn->func; 981 } 982 983 if (insn->visited) { 984 if (frame_state(insn->state) != frame_state(state)) { 985 WARN_FUNC("frame pointer state mismatch", 986 sec, insn->offset); 987 return 1; 988 } 989 990 return 0; 991 } 992 993 insn->visited = true; 994 insn->state = state; 995 996 list_for_each_entry(alt, &insn->alts, list) { 997 ret = validate_branch(file, alt->insn, state); 998 if (ret) 999 return 1; 1000 } 1001 1002 switch (insn->type) { 1003 1004 case INSN_FP_SAVE: 1005 if (!nofp) { 1006 if (state & STATE_FP_SAVED) { 1007 WARN_FUNC("duplicate frame pointer save", 1008 sec, insn->offset); 1009 return 1; 1010 } 1011 state |= STATE_FP_SAVED; 1012 } 1013 break; 1014 1015 case INSN_FP_SETUP: 1016 if (!nofp) { 1017 if (state & STATE_FP_SETUP) { 1018 WARN_FUNC("duplicate frame pointer setup", 1019 sec, insn->offset); 1020 return 1; 1021 } 1022 state |= STATE_FP_SETUP; 1023 } 1024 break; 1025 1026 case INSN_FP_RESTORE: 1027 if (!nofp) { 1028 if (has_valid_stack_frame(insn)) 1029 state &= ~STATE_FP_SETUP; 1030 1031 state &= ~STATE_FP_SAVED; 1032 } 1033 break; 1034 1035 case INSN_RETURN: 1036 if (!nofp && has_modified_stack_frame(insn)) { 1037 WARN_FUNC("return without frame pointer restore", 1038 sec, insn->offset); 1039 return 1; 1040 } 1041 return 0; 1042 1043 case INSN_CALL: 1044 if (is_fentry_call(insn)) { 1045 state |= STATE_FENTRY; 1046 break; 1047 } 1048 1049 ret = dead_end_function(file, insn->call_dest); 1050 if (ret == 1) 1051 return 0; 1052 if (ret == -1) 1053 return 1; 1054 1055 /* fallthrough */ 1056 case INSN_CALL_DYNAMIC: 1057 if (!nofp && !has_valid_stack_frame(insn)) { 1058 WARN_FUNC("call without frame pointer save/setup", 1059 sec, insn->offset); 1060 return 1; 1061 } 1062 break; 1063 1064 case INSN_JUMP_CONDITIONAL: 1065 case INSN_JUMP_UNCONDITIONAL: 1066 if (insn->jump_dest) { 1067 ret = validate_branch(file, insn->jump_dest, 1068 state); 1069 if (ret) 1070 return 1; 1071 } else if (has_modified_stack_frame(insn)) { 1072 WARN_FUNC("sibling call from callable instruction with changed frame pointer", 1073 sec, insn->offset); 1074 return 1; 1075 } /* else it's a sibling call */ 1076 1077 if (insn->type == INSN_JUMP_UNCONDITIONAL) 1078 return 0; 1079 1080 break; 1081 1082 case INSN_JUMP_DYNAMIC: 1083 if (list_empty(&insn->alts) && 1084 has_modified_stack_frame(insn)) { 1085 WARN_FUNC("sibling call from callable instruction with changed frame pointer", 1086 sec, insn->offset); 1087 return 1; 1088 } 1089 1090 return 0; 1091 1092 default: 1093 break; 1094 } 1095 1096 if (insn->dead_end) 1097 return 0; 1098 1099 insn = next_insn_same_sec(file, insn); 1100 if (!insn) { 1101 WARN("%s: unexpected end of section", sec->name); 1102 return 1; 1103 } 1104 } 1105 1106 return 0; 1107 } 1108 1109 static bool is_kasan_insn(struct instruction *insn) 1110 { 1111 return (insn->type == INSN_CALL && 1112 !strcmp(insn->call_dest->name, "__asan_handle_no_return")); 1113 } 1114 1115 static bool is_ubsan_insn(struct instruction *insn) 1116 { 1117 return (insn->type == INSN_CALL && 1118 !strcmp(insn->call_dest->name, 1119 "__ubsan_handle_builtin_unreachable")); 1120 } 1121 1122 static bool ignore_unreachable_insn(struct symbol *func, 1123 struct instruction *insn) 1124 { 1125 int i; 1126 1127 if (insn->type == INSN_NOP) 1128 return true; 1129 1130 /* 1131 * Check if this (or a subsequent) instruction is related to 1132 * CONFIG_UBSAN or CONFIG_KASAN. 1133 * 1134 * End the search at 5 instructions to avoid going into the weeds. 1135 */ 1136 for (i = 0; i < 5; i++) { 1137 1138 if (is_kasan_insn(insn) || is_ubsan_insn(insn)) 1139 return true; 1140 1141 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) { 1142 insn = insn->jump_dest; 1143 continue; 1144 } 1145 1146 if (insn->offset + insn->len >= func->offset + func->len) 1147 break; 1148 insn = list_next_entry(insn, list); 1149 } 1150 1151 return false; 1152 } 1153 1154 static int validate_functions(struct objtool_file *file) 1155 { 1156 struct section *sec; 1157 struct symbol *func; 1158 struct instruction *insn; 1159 int ret, warnings = 0; 1160 1161 list_for_each_entry(sec, &file->elf->sections, list) { 1162 list_for_each_entry(func, &sec->symbol_list, list) { 1163 if (func->type != STT_FUNC) 1164 continue; 1165 1166 insn = find_insn(file, sec, func->offset); 1167 if (!insn) 1168 continue; 1169 1170 ret = validate_branch(file, insn, 0); 1171 warnings += ret; 1172 } 1173 } 1174 1175 list_for_each_entry(sec, &file->elf->sections, list) { 1176 list_for_each_entry(func, &sec->symbol_list, list) { 1177 if (func->type != STT_FUNC) 1178 continue; 1179 1180 func_for_each_insn(file, func, insn) { 1181 if (insn->visited) 1182 continue; 1183 1184 insn->visited = true; 1185 1186 if (file->ignore_unreachables || warnings || 1187 ignore_unreachable_insn(func, insn)) 1188 continue; 1189 1190 /* 1191 * gcov produces a lot of unreachable 1192 * instructions. If we get an unreachable 1193 * warning and the file has gcov enabled, just 1194 * ignore it, and all other such warnings for 1195 * the file. 1196 */ 1197 if (!file->ignore_unreachables && 1198 gcov_enabled(file)) { 1199 file->ignore_unreachables = true; 1200 continue; 1201 } 1202 1203 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset); 1204 warnings++; 1205 } 1206 } 1207 } 1208 1209 return warnings; 1210 } 1211 1212 static int validate_uncallable_instructions(struct objtool_file *file) 1213 { 1214 struct instruction *insn; 1215 int warnings = 0; 1216 1217 for_each_insn(file, insn) { 1218 if (!insn->visited && insn->type == INSN_RETURN) { 1219 WARN_FUNC("return instruction outside of a callable function", 1220 insn->sec, insn->offset); 1221 warnings++; 1222 } 1223 } 1224 1225 return warnings; 1226 } 1227 1228 static void cleanup(struct objtool_file *file) 1229 { 1230 struct instruction *insn, *tmpinsn; 1231 struct alternative *alt, *tmpalt; 1232 1233 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) { 1234 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) { 1235 list_del(&alt->list); 1236 free(alt); 1237 } 1238 list_del(&insn->list); 1239 hash_del(&insn->hash); 1240 free(insn); 1241 } 1242 elf_close(file->elf); 1243 } 1244 1245 const char * const check_usage[] = { 1246 "objtool check [<options>] file.o", 1247 NULL, 1248 }; 1249 1250 int cmd_check(int argc, const char **argv) 1251 { 1252 struct objtool_file file; 1253 int ret, warnings = 0; 1254 1255 const struct option options[] = { 1256 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"), 1257 OPT_END(), 1258 }; 1259 1260 argc = parse_options(argc, argv, options, check_usage, 0); 1261 1262 if (argc != 1) 1263 usage_with_options(check_usage, options); 1264 1265 objname = argv[0]; 1266 1267 file.elf = elf_open(objname); 1268 if (!file.elf) { 1269 fprintf(stderr, "error reading elf file %s\n", objname); 1270 return 1; 1271 } 1272 1273 INIT_LIST_HEAD(&file.insn_list); 1274 hash_init(file.insn_hash); 1275 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard"); 1276 file.rodata = find_section_by_name(file.elf, ".rodata"); 1277 file.ignore_unreachables = false; 1278 file.c_file = find_section_by_name(file.elf, ".comment"); 1279 1280 ret = decode_sections(&file); 1281 if (ret < 0) 1282 goto out; 1283 warnings += ret; 1284 1285 ret = validate_functions(&file); 1286 if (ret < 0) 1287 goto out; 1288 warnings += ret; 1289 1290 ret = validate_uncallable_instructions(&file); 1291 if (ret < 0) 1292 goto out; 1293 warnings += ret; 1294 1295 out: 1296 cleanup(&file); 1297 1298 /* ignore warnings for now until we get all the code cleaned up */ 1299 if (ret || warnings) 1300 return 0; 1301 return 0; 1302 } 1303