2 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
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.
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.
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/>.
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.
25 * For more information, see tools/objtool/Documentation/stack-validation.txt.
30 #include <subcmd/parse-options.h>
38 #include <linux/hashtable.h>
39 #include <linux/kernel.h>
41 #define STATE_FP_SAVED 0x1
42 #define STATE_FP_SETUP 0x2
43 #define STATE_FENTRY 0x4
46 struct list_head list;
47 struct hlist_node hash;
50 unsigned int len, state;
52 unsigned long immediate;
53 bool alt_group, visited, dead_end;
54 struct symbol *call_dest;
55 struct instruction *jump_dest;
56 struct list_head alts;
61 struct list_head list;
62 struct instruction *insn;
67 struct list_head insn_list;
68 DECLARE_HASHTABLE(insn_hash, 16);
69 struct section *rodata, *whitelist;
70 bool ignore_unreachables, c_file;
76 static struct instruction *find_insn(struct objtool_file *file,
77 struct section *sec, unsigned long offset)
79 struct instruction *insn;
81 hash_for_each_possible(file->insn_hash, insn, hash, offset)
82 if (insn->sec == sec && insn->offset == offset)
88 static struct instruction *next_insn_same_sec(struct objtool_file *file,
89 struct instruction *insn)
91 struct instruction *next = list_next_entry(insn, list);
93 if (&next->list == &file->insn_list || next->sec != insn->sec)
99 static bool gcov_enabled(struct objtool_file *file)
104 list_for_each_entry(sec, &file->elf->sections, list)
105 list_for_each_entry(sym, &sec->symbol_list, list)
106 if (!strncmp(sym->name, "__gcov_.", 8))
112 #define for_each_insn(file, insn) \
113 list_for_each_entry(insn, &file->insn_list, list)
115 #define func_for_each_insn(file, func, insn) \
116 for (insn = find_insn(file, func->sec, func->offset); \
117 insn && &insn->list != &file->insn_list && \
118 insn->sec == func->sec && \
119 insn->offset < func->offset + func->len; \
120 insn = list_next_entry(insn, list))
122 #define func_for_each_insn_continue_reverse(file, func, insn) \
123 for (insn = list_prev_entry(insn, list); \
124 &insn->list != &file->insn_list && \
125 insn->sec == func->sec && insn->offset >= func->offset; \
126 insn = list_prev_entry(insn, list))
128 #define sec_for_each_insn_from(file, insn) \
129 for (; insn; insn = next_insn_same_sec(file, insn))
133 * Check if the function has been manually whitelisted with the
134 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
135 * due to its use of a context switching instruction.
137 static bool ignore_func(struct objtool_file *file, struct symbol *func)
140 struct instruction *insn;
142 /* check for STACK_FRAME_NON_STANDARD */
143 if (file->whitelist && file->whitelist->rela)
144 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
145 if (rela->sym->type == STT_SECTION &&
146 rela->sym->sec == func->sec &&
147 rela->addend == func->offset)
149 if (rela->sym->type == STT_FUNC && rela->sym == func)
153 /* check if it has a context switching instruction */
154 func_for_each_insn(file, func, insn)
155 if (insn->type == INSN_CONTEXT_SWITCH)
162 * This checks to see if the given function is a "noreturn" function.
164 * For global functions which are outside the scope of this object file, we
165 * have to keep a manual list of them.
167 * For local functions, we have to detect them manually by simply looking for
168 * the lack of a return instruction.
175 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
179 struct instruction *insn;
183 * Unfortunately these have to be hard coded because the noreturn
184 * attribute isn't provided in ELF data.
186 static const char * const global_noreturns[] = {
191 "__module_put_and_exit",
193 "kvm_spurious_fault",
198 if (func->bind == STB_WEAK)
201 if (func->bind == STB_GLOBAL)
202 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
203 if (!strcmp(func->name, global_noreturns[i]))
209 func_for_each_insn(file, func, insn) {
212 if (insn->type == INSN_RETURN)
220 * A function can have a sibling call instead of a return. In that
221 * case, the function's dead-end status depends on whether the target
222 * of the sibling call returns.
224 func_for_each_insn(file, func, insn) {
225 if (insn->sec != func->sec ||
226 insn->offset >= func->offset + func->len)
229 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
230 struct instruction *dest = insn->jump_dest;
231 struct symbol *dest_func;
234 /* sibling call to another file */
237 if (dest->sec != func->sec ||
238 dest->offset < func->offset ||
239 dest->offset >= func->offset + func->len) {
240 /* local sibling call */
241 dest_func = find_symbol_by_offset(dest->sec,
246 if (recursion == 5) {
247 WARN_FUNC("infinite recursion (objtool bug!)",
248 dest->sec, dest->offset);
252 return __dead_end_function(file, dest_func,
257 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
265 static int dead_end_function(struct objtool_file *file, struct symbol *func)
267 return __dead_end_function(file, func, 0);
271 * Call the arch-specific instruction decoder for all the instructions and add
272 * them to the global instruction list.
274 static int decode_instructions(struct objtool_file *file)
278 unsigned long offset;
279 struct instruction *insn;
282 list_for_each_entry(sec, &file->elf->sections, list) {
284 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
287 for (offset = 0; offset < sec->len; offset += insn->len) {
288 insn = malloc(sizeof(*insn));
289 memset(insn, 0, sizeof(*insn));
291 INIT_LIST_HEAD(&insn->alts);
293 insn->offset = offset;
295 ret = arch_decode_instruction(file->elf, sec, offset,
297 &insn->len, &insn->type,
302 if (!insn->type || insn->type > INSN_LAST) {
303 WARN_FUNC("invalid instruction type %d",
304 insn->sec, insn->offset, insn->type);
308 hash_add(file->insn_hash, &insn->hash, insn->offset);
309 list_add_tail(&insn->list, &file->insn_list);
312 list_for_each_entry(func, &sec->symbol_list, list) {
313 if (func->type != STT_FUNC)
316 if (!find_insn(file, sec, func->offset)) {
317 WARN("%s(): can't find starting instruction",
322 func_for_each_insn(file, func, insn)
332 * Find all uses of the unreachable() macro, which are code path dead ends.
334 static int add_dead_ends(struct objtool_file *file)
338 struct instruction *insn;
341 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
345 list_for_each_entry(rela, &sec->rela_list, list) {
346 if (rela->sym->type != STT_SECTION) {
347 WARN("unexpected relocation symbol type in %s", sec->name);
350 insn = find_insn(file, rela->sym->sec, rela->addend);
352 insn = list_prev_entry(insn, list);
353 else if (rela->addend == rela->sym->sec->len) {
355 list_for_each_entry_reverse(insn, &file->insn_list, list) {
356 if (insn->sec == rela->sym->sec) {
363 WARN("can't find unreachable insn at %s+0x%x",
364 rela->sym->sec->name, rela->addend);
368 WARN("can't find unreachable insn at %s+0x%x",
369 rela->sym->sec->name, rela->addend);
373 insn->dead_end = true;
380 * Warnings shouldn't be reported for ignored functions.
382 static void add_ignores(struct objtool_file *file)
384 struct instruction *insn;
388 list_for_each_entry(sec, &file->elf->sections, list) {
389 list_for_each_entry(func, &sec->symbol_list, list) {
390 if (func->type != STT_FUNC)
393 if (!ignore_func(file, func))
396 func_for_each_insn(file, func, insn)
397 insn->visited = true;
403 * Find the destination instructions for all jumps.
405 static int add_jump_destinations(struct objtool_file *file)
407 struct instruction *insn;
409 struct section *dest_sec;
410 unsigned long dest_off;
412 for_each_insn(file, insn) {
413 if (insn->type != INSN_JUMP_CONDITIONAL &&
414 insn->type != INSN_JUMP_UNCONDITIONAL)
421 rela = find_rela_by_dest_range(insn->sec, insn->offset,
424 dest_sec = insn->sec;
425 dest_off = insn->offset + insn->len + insn->immediate;
426 } else if (rela->sym->type == STT_SECTION) {
427 dest_sec = rela->sym->sec;
428 dest_off = rela->addend + 4;
429 } else if (rela->sym->sec->idx) {
430 dest_sec = rela->sym->sec;
431 dest_off = rela->sym->sym.st_value + rela->addend + 4;
438 insn->jump_dest = find_insn(file, dest_sec, dest_off);
439 if (!insn->jump_dest) {
442 * This is a special case where an alt instruction
443 * jumps past the end of the section. These are
444 * handled later in handle_group_alt().
446 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
449 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
450 insn->sec, insn->offset, dest_sec->name,
460 * Find the destination instructions for all calls.
462 static int add_call_destinations(struct objtool_file *file)
464 struct instruction *insn;
465 unsigned long dest_off;
468 for_each_insn(file, insn) {
469 if (insn->type != INSN_CALL)
472 rela = find_rela_by_dest_range(insn->sec, insn->offset,
475 dest_off = insn->offset + insn->len + insn->immediate;
476 insn->call_dest = find_symbol_by_offset(insn->sec,
478 if (!insn->call_dest) {
479 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
480 insn->sec, insn->offset, dest_off);
483 } else if (rela->sym->type == STT_SECTION) {
484 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
486 if (!insn->call_dest ||
487 insn->call_dest->type != STT_FUNC) {
488 WARN_FUNC("can't find call dest symbol at %s+0x%x",
489 insn->sec, insn->offset,
490 rela->sym->sec->name,
495 insn->call_dest = rela->sym;
502 * The .alternatives section requires some extra special care, over and above
503 * what other special sections require:
505 * 1. Because alternatives are patched in-place, we need to insert a fake jump
506 * instruction at the end so that validate_branch() skips all the original
507 * replaced instructions when validating the new instruction path.
509 * 2. An added wrinkle is that the new instruction length might be zero. In
510 * that case the old instructions are replaced with noops. We simulate that
511 * by creating a fake jump as the only new instruction.
513 * 3. In some cases, the alternative section includes an instruction which
514 * conditionally jumps to the _end_ of the entry. We have to modify these
515 * jumps' destinations to point back to .text rather than the end of the
516 * entry in .altinstr_replacement.
518 * 4. It has been requested that we don't validate the !POPCNT feature path
519 * which is a "very very small percentage of machines".
521 static int handle_group_alt(struct objtool_file *file,
522 struct special_alt *special_alt,
523 struct instruction *orig_insn,
524 struct instruction **new_insn)
526 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
527 unsigned long dest_off;
529 last_orig_insn = NULL;
531 sec_for_each_insn_from(file, insn) {
532 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
535 if (special_alt->skip_orig)
536 insn->type = INSN_NOP;
538 insn->alt_group = true;
539 last_orig_insn = insn;
542 if (!next_insn_same_sec(file, last_orig_insn)) {
543 WARN("%s: don't know how to handle alternatives at end of section",
544 special_alt->orig_sec->name);
548 fake_jump = malloc(sizeof(*fake_jump));
550 WARN("malloc failed");
553 memset(fake_jump, 0, sizeof(*fake_jump));
554 INIT_LIST_HEAD(&fake_jump->alts);
555 fake_jump->sec = special_alt->new_sec;
556 fake_jump->offset = -1;
557 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
558 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
560 if (!special_alt->new_len) {
561 *new_insn = fake_jump;
565 last_new_insn = NULL;
567 sec_for_each_insn_from(file, insn) {
568 if (insn->offset >= special_alt->new_off + special_alt->new_len)
571 last_new_insn = insn;
573 if (insn->type != INSN_JUMP_CONDITIONAL &&
574 insn->type != INSN_JUMP_UNCONDITIONAL)
577 if (!insn->immediate)
580 dest_off = insn->offset + insn->len + insn->immediate;
581 if (dest_off == special_alt->new_off + special_alt->new_len)
582 insn->jump_dest = fake_jump;
584 if (!insn->jump_dest) {
585 WARN_FUNC("can't find alternative jump destination",
586 insn->sec, insn->offset);
591 if (!last_new_insn) {
592 WARN_FUNC("can't find last new alternative instruction",
593 special_alt->new_sec, special_alt->new_off);
597 list_add(&fake_jump->list, &last_new_insn->list);
603 * A jump table entry can either convert a nop to a jump or a jump to a nop.
604 * If the original instruction is a jump, make the alt entry an effective nop
605 * by just skipping the original instruction.
607 static int handle_jump_alt(struct objtool_file *file,
608 struct special_alt *special_alt,
609 struct instruction *orig_insn,
610 struct instruction **new_insn)
612 if (orig_insn->type == INSN_NOP)
615 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
616 WARN_FUNC("unsupported instruction at jump label",
617 orig_insn->sec, orig_insn->offset);
621 *new_insn = list_next_entry(orig_insn, list);
626 * Read all the special sections which have alternate instructions which can be
627 * patched in or redirected to at runtime. Each instruction having alternate
628 * instruction(s) has them added to its insn->alts list, which will be
629 * traversed in validate_branch().
631 static int add_special_section_alts(struct objtool_file *file)
633 struct list_head special_alts;
634 struct instruction *orig_insn, *new_insn;
635 struct special_alt *special_alt, *tmp;
636 struct alternative *alt;
639 ret = special_get_alts(file->elf, &special_alts);
643 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
644 alt = malloc(sizeof(*alt));
646 WARN("malloc failed");
651 orig_insn = find_insn(file, special_alt->orig_sec,
652 special_alt->orig_off);
654 WARN_FUNC("special: can't find orig instruction",
655 special_alt->orig_sec, special_alt->orig_off);
661 if (!special_alt->group || special_alt->new_len) {
662 new_insn = find_insn(file, special_alt->new_sec,
663 special_alt->new_off);
665 WARN_FUNC("special: can't find new instruction",
666 special_alt->new_sec,
667 special_alt->new_off);
673 if (special_alt->group) {
674 ret = handle_group_alt(file, special_alt, orig_insn,
678 } else if (special_alt->jump_or_nop) {
679 ret = handle_jump_alt(file, special_alt, orig_insn,
685 alt->insn = new_insn;
686 list_add_tail(&alt->list, &orig_insn->alts);
688 list_del(&special_alt->list);
696 static int add_switch_table(struct objtool_file *file, struct symbol *func,
697 struct instruction *insn, struct rela *table,
698 struct rela *next_table)
700 struct rela *rela = table;
701 struct instruction *alt_insn;
702 struct alternative *alt;
704 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
705 if (rela == next_table)
708 if (rela->sym->sec != insn->sec ||
709 rela->addend <= func->offset ||
710 rela->addend >= func->offset + func->len)
713 alt_insn = find_insn(file, insn->sec, rela->addend);
715 WARN("%s: can't find instruction at %s+0x%x",
716 file->rodata->rela->name, insn->sec->name,
721 alt = malloc(sizeof(*alt));
723 WARN("malloc failed");
727 alt->insn = alt_insn;
728 list_add_tail(&alt->list, &insn->alts);
735 * find_switch_table() - Given a dynamic jump, find the switch jump table in
736 * .rodata associated with it.
738 * There are 3 basic patterns:
740 * 1. jmpq *[rodata addr](,%reg,8)
742 * This is the most common case by far. It jumps to an address in a simple
743 * jump table which is stored in .rodata.
745 * 2. jmpq *[rodata addr](%rip)
747 * This is caused by a rare GCC quirk, currently only seen in three driver
748 * functions in the kernel, only with certain obscure non-distro configs.
750 * As part of an optimization, GCC makes a copy of an existing switch jump
751 * table, modifies it, and then hard-codes the jump (albeit with an indirect
752 * jump) to use a single entry in the table. The rest of the jump table and
753 * some of its jump targets remain as dead code.
755 * In such a case we can just crudely ignore all unreachable instruction
756 * warnings for the entire object file. Ideally we would just ignore them
757 * for the function, but that would require redesigning the code quite a
758 * bit. And honestly that's just not worth doing: unreachable instruction
759 * warnings are of questionable value anyway, and this is such a rare issue.
761 * 3. mov [rodata addr],%reg1
762 * ... some instructions ...
763 * jmpq *(%reg1,%reg2,8)
765 * This is a fairly uncommon pattern which is new for GCC 6. As of this
766 * writing, there are 11 occurrences of it in the allmodconfig kernel.
768 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
769 * ensure the same register is used in the mov and jump instructions.
771 static struct rela *find_switch_table(struct objtool_file *file,
773 struct instruction *insn)
775 struct rela *text_rela, *rodata_rela;
776 struct instruction *orig_insn = insn;
778 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
779 if (text_rela && text_rela->sym == file->rodata->sym) {
781 rodata_rela = find_rela_by_dest(file->rodata,
787 rodata_rela = find_rela_by_dest(file->rodata,
788 text_rela->addend + 4);
791 file->ignore_unreachables = true;
796 func_for_each_insn_continue_reverse(file, func, insn) {
797 if (insn->type == INSN_JUMP_DYNAMIC)
800 /* allow small jumps within the range */
801 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
803 (insn->jump_dest->offset <= insn->offset ||
804 insn->jump_dest->offset > orig_insn->offset))
807 /* look for a relocation which references .rodata */
808 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
810 if (!text_rela || text_rela->sym != file->rodata->sym)
814 * Make sure the .rodata address isn't associated with a
815 * symbol. gcc jump tables are anonymous data.
817 if (find_symbol_containing(file->rodata, text_rela->addend))
820 return find_rela_by_dest(file->rodata, text_rela->addend);
826 static int add_func_switch_tables(struct objtool_file *file,
829 struct instruction *insn, *prev_jump = NULL;
830 struct rela *rela, *prev_rela = NULL;
833 func_for_each_insn(file, func, insn) {
834 if (insn->type != INSN_JUMP_DYNAMIC)
837 rela = find_switch_table(file, func, insn);
842 * We found a switch table, but we don't know yet how big it
843 * is. Don't add it until we reach the end of the function or
844 * the beginning of another switch table in the same function.
847 ret = add_switch_table(file, func, prev_jump, prev_rela,
858 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
867 * For some switch statements, gcc generates a jump table in the .rodata
868 * section which contains a list of addresses within the function to jump to.
869 * This finds these jump tables and adds them to the insn->alts lists.
871 static int add_switch_table_alts(struct objtool_file *file)
877 if (!file->rodata || !file->rodata->rela)
880 list_for_each_entry(sec, &file->elf->sections, list) {
881 list_for_each_entry(func, &sec->symbol_list, list) {
882 if (func->type != STT_FUNC)
885 ret = add_func_switch_tables(file, func);
894 static int decode_sections(struct objtool_file *file)
898 ret = decode_instructions(file);
902 ret = add_dead_ends(file);
908 ret = add_jump_destinations(file);
912 ret = add_call_destinations(file);
916 ret = add_special_section_alts(file);
920 ret = add_switch_table_alts(file);
927 static bool is_fentry_call(struct instruction *insn)
929 if (insn->type == INSN_CALL &&
930 insn->call_dest->type == STT_NOTYPE &&
931 !strcmp(insn->call_dest->name, "__fentry__"))
937 static bool has_modified_stack_frame(struct instruction *insn)
939 return (insn->state & STATE_FP_SAVED) ||
940 (insn->state & STATE_FP_SETUP);
943 static bool has_valid_stack_frame(struct instruction *insn)
945 return (insn->state & STATE_FP_SAVED) &&
946 (insn->state & STATE_FP_SETUP);
949 static unsigned int frame_state(unsigned long state)
951 return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
955 * Follow the branch starting at the given instruction, and recursively follow
956 * any other branches (jumps). Meanwhile, track the frame pointer state at
957 * each instruction and validate all the rules described in
958 * tools/objtool/Documentation/stack-validation.txt.
960 static int validate_branch(struct objtool_file *file,
961 struct instruction *first, unsigned char first_state)
963 struct alternative *alt;
964 struct instruction *insn;
966 struct symbol *func = NULL;
974 if (insn->alt_group && list_empty(&insn->alts)) {
975 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
981 if (file->c_file && insn->func) {
982 if (func && func != insn->func) {
983 WARN("%s() falls through to next function %s()",
984 func->name, insn->func->name);
992 if (frame_state(insn->state) != frame_state(state)) {
993 WARN_FUNC("frame pointer state mismatch",
1001 insn->visited = true;
1002 insn->state = state;
1004 list_for_each_entry(alt, &insn->alts, list) {
1005 ret = validate_branch(file, alt->insn, state);
1010 switch (insn->type) {
1014 if (state & STATE_FP_SAVED) {
1015 WARN_FUNC("duplicate frame pointer save",
1019 state |= STATE_FP_SAVED;
1025 if (state & STATE_FP_SETUP) {
1026 WARN_FUNC("duplicate frame pointer setup",
1030 state |= STATE_FP_SETUP;
1034 case INSN_FP_RESTORE:
1036 if (has_valid_stack_frame(insn))
1037 state &= ~STATE_FP_SETUP;
1039 state &= ~STATE_FP_SAVED;
1044 if (!nofp && has_modified_stack_frame(insn)) {
1045 WARN_FUNC("return without frame pointer restore",
1052 if (is_fentry_call(insn)) {
1053 state |= STATE_FENTRY;
1057 ret = dead_end_function(file, insn->call_dest);
1064 case INSN_CALL_DYNAMIC:
1065 if (!nofp && !has_valid_stack_frame(insn)) {
1066 WARN_FUNC("call without frame pointer save/setup",
1072 case INSN_JUMP_CONDITIONAL:
1073 case INSN_JUMP_UNCONDITIONAL:
1074 if (insn->jump_dest) {
1075 ret = validate_branch(file, insn->jump_dest,
1079 } else if (has_modified_stack_frame(insn)) {
1080 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1083 } /* else it's a sibling call */
1085 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1090 case INSN_JUMP_DYNAMIC:
1091 if (list_empty(&insn->alts) &&
1092 has_modified_stack_frame(insn)) {
1093 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
1107 insn = next_insn_same_sec(file, insn);
1109 WARN("%s: unexpected end of section", sec->name);
1117 static bool is_kasan_insn(struct instruction *insn)
1119 return (insn->type == INSN_CALL &&
1120 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1123 static bool is_ubsan_insn(struct instruction *insn)
1125 return (insn->type == INSN_CALL &&
1126 !strcmp(insn->call_dest->name,
1127 "__ubsan_handle_builtin_unreachable"));
1130 static bool ignore_unreachable_insn(struct symbol *func,
1131 struct instruction *insn)
1135 if (insn->type == INSN_NOP)
1139 * Check if this (or a subsequent) instruction is related to
1140 * CONFIG_UBSAN or CONFIG_KASAN.
1142 * End the search at 5 instructions to avoid going into the weeds.
1144 for (i = 0; i < 5; i++) {
1146 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1149 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1150 insn = insn->jump_dest;
1154 if (insn->offset + insn->len >= func->offset + func->len)
1156 insn = list_next_entry(insn, list);
1162 static int validate_functions(struct objtool_file *file)
1164 struct section *sec;
1165 struct symbol *func;
1166 struct instruction *insn;
1167 int ret, warnings = 0;
1169 list_for_each_entry(sec, &file->elf->sections, list) {
1170 list_for_each_entry(func, &sec->symbol_list, list) {
1171 if (func->type != STT_FUNC)
1174 insn = find_insn(file, sec, func->offset);
1178 ret = validate_branch(file, insn, 0);
1183 list_for_each_entry(sec, &file->elf->sections, list) {
1184 list_for_each_entry(func, &sec->symbol_list, list) {
1185 if (func->type != STT_FUNC)
1188 func_for_each_insn(file, func, insn) {
1192 insn->visited = true;
1194 if (file->ignore_unreachables || warnings ||
1195 ignore_unreachable_insn(func, insn))
1199 * gcov produces a lot of unreachable
1200 * instructions. If we get an unreachable
1201 * warning and the file has gcov enabled, just
1202 * ignore it, and all other such warnings for
1205 if (!file->ignore_unreachables &&
1206 gcov_enabled(file)) {
1207 file->ignore_unreachables = true;
1211 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1220 static int validate_uncallable_instructions(struct objtool_file *file)
1222 struct instruction *insn;
1225 for_each_insn(file, insn) {
1226 if (!insn->visited && insn->type == INSN_RETURN) {
1227 WARN_FUNC("return instruction outside of a callable function",
1228 insn->sec, insn->offset);
1236 static void cleanup(struct objtool_file *file)
1238 struct instruction *insn, *tmpinsn;
1239 struct alternative *alt, *tmpalt;
1241 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
1242 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1243 list_del(&alt->list);
1246 list_del(&insn->list);
1247 hash_del(&insn->hash);
1250 elf_close(file->elf);
1253 const char * const check_usage[] = {
1254 "objtool check [<options>] file.o",
1258 int cmd_check(int argc, const char **argv)
1260 struct objtool_file file;
1261 int ret, warnings = 0;
1263 const struct option options[] = {
1264 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1268 argc = parse_options(argc, argv, options, check_usage, 0);
1271 usage_with_options(check_usage, options);
1275 file.elf = elf_open(objname);
1277 fprintf(stderr, "error reading elf file %s\n", objname);
1281 INIT_LIST_HEAD(&file.insn_list);
1282 hash_init(file.insn_hash);
1283 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
1284 file.rodata = find_section_by_name(file.elf, ".rodata");
1285 file.ignore_unreachables = false;
1286 file.c_file = find_section_by_name(file.elf, ".comment");
1288 ret = decode_sections(&file);
1293 ret = validate_functions(&file);
1298 ret = validate_uncallable_instructions(&file);
1306 /* ignore warnings for now until we get all the code cleaned up */
1307 if (ret || warnings)