2 * Copyright (C) 2015-2017 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/>.
28 #include <linux/hashtable.h>
29 #include <linux/kernel.h>
31 #define FAKE_JUMP_OFFSET -1
34 struct list_head list;
35 struct instruction *insn;
39 struct cfi_state initial_func_cfi;
41 struct instruction *find_insn(struct objtool_file *file,
42 struct section *sec, unsigned long offset)
44 struct instruction *insn;
46 hash_for_each_possible(file->insn_hash, insn, hash, offset)
47 if (insn->sec == sec && insn->offset == offset)
53 static struct instruction *next_insn_same_sec(struct objtool_file *file,
54 struct instruction *insn)
56 struct instruction *next = list_next_entry(insn, list);
58 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
64 static struct instruction *next_insn_same_func(struct objtool_file *file,
65 struct instruction *insn)
67 struct instruction *next = list_next_entry(insn, list);
68 struct symbol *func = insn->func;
73 if (&next->list != &file->insn_list && next->func == func)
76 /* Check if we're already in the subfunction: */
77 if (func == func->cfunc)
80 /* Move to the subfunction: */
81 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
84 #define func_for_each_insn_all(file, func, insn) \
85 for (insn = find_insn(file, func->sec, func->offset); \
87 insn = next_insn_same_func(file, insn))
89 #define func_for_each_insn(file, func, insn) \
90 for (insn = find_insn(file, func->sec, func->offset); \
91 insn && &insn->list != &file->insn_list && \
92 insn->sec == func->sec && \
93 insn->offset < func->offset + func->len; \
94 insn = list_next_entry(insn, list))
96 #define func_for_each_insn_continue_reverse(file, func, insn) \
97 for (insn = list_prev_entry(insn, list); \
98 &insn->list != &file->insn_list && \
99 insn->sec == func->sec && insn->offset >= func->offset; \
100 insn = list_prev_entry(insn, list))
102 #define sec_for_each_insn_from(file, insn) \
103 for (; insn; insn = next_insn_same_sec(file, insn))
105 #define sec_for_each_insn_continue(file, insn) \
106 for (insn = next_insn_same_sec(file, insn); insn; \
107 insn = next_insn_same_sec(file, insn))
110 * Check if the function has been manually whitelisted with the
111 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
112 * due to its use of a context switching instruction.
114 static bool ignore_func(struct objtool_file *file, struct symbol *func)
118 /* check for STACK_FRAME_NON_STANDARD */
119 if (file->whitelist && file->whitelist->rela)
120 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
121 if (rela->sym->type == STT_SECTION &&
122 rela->sym->sec == func->sec &&
123 rela->addend == func->offset)
125 if (rela->sym->type == STT_FUNC && rela->sym == func)
133 * This checks to see if the given function is a "noreturn" function.
135 * For global functions which are outside the scope of this object file, we
136 * have to keep a manual list of them.
138 * For local functions, we have to detect them manually by simply looking for
139 * the lack of a return instruction.
146 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
150 struct instruction *insn;
154 * Unfortunately these have to be hard coded because the noreturn
155 * attribute isn't provided in ELF data.
157 static const char * const global_noreturns[] = {
162 "__module_put_and_exit",
164 "kvm_spurious_fault",
169 "machine_real_restart",
170 "rewind_stack_do_exit",
173 if (func->bind == STB_WEAK)
176 if (func->bind == STB_GLOBAL)
177 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
178 if (!strcmp(func->name, global_noreturns[i]))
184 insn = find_insn(file, func->sec, func->offset);
188 func_for_each_insn_all(file, func, insn) {
191 if (insn->type == INSN_RETURN)
199 * A function can have a sibling call instead of a return. In that
200 * case, the function's dead-end status depends on whether the target
201 * of the sibling call returns.
203 func_for_each_insn_all(file, func, insn) {
204 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
205 struct instruction *dest = insn->jump_dest;
208 /* sibling call to another file */
211 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
213 /* local sibling call */
214 if (recursion == 5) {
216 * Infinite recursion: two functions
217 * have sibling calls to each other.
218 * This is a very rare case. It means
219 * they aren't dead ends.
224 return __dead_end_function(file, dest->func,
229 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
237 static int dead_end_function(struct objtool_file *file, struct symbol *func)
239 return __dead_end_function(file, func, 0);
242 static void clear_insn_state(struct insn_state *state)
246 memset(state, 0, sizeof(*state));
247 state->cfa.base = CFI_UNDEFINED;
248 for (i = 0; i < CFI_NUM_REGS; i++) {
249 state->regs[i].base = CFI_UNDEFINED;
250 state->vals[i].base = CFI_UNDEFINED;
252 state->drap_reg = CFI_UNDEFINED;
253 state->drap_offset = -1;
257 * Call the arch-specific instruction decoder for all the instructions and add
258 * them to the global instruction list.
260 static int decode_instructions(struct objtool_file *file)
264 unsigned long offset;
265 struct instruction *insn;
268 for_each_sec(file, sec) {
270 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
273 if (strcmp(sec->name, ".altinstr_replacement") &&
274 strcmp(sec->name, ".altinstr_aux") &&
275 strncmp(sec->name, ".discard.", 9))
278 for (offset = 0; offset < sec->len; offset += insn->len) {
279 insn = malloc(sizeof(*insn));
281 WARN("malloc failed");
284 memset(insn, 0, sizeof(*insn));
285 INIT_LIST_HEAD(&insn->alts);
286 clear_insn_state(&insn->state);
289 insn->offset = offset;
291 ret = arch_decode_instruction(file->elf, sec, offset,
293 &insn->len, &insn->type,
299 if (!insn->type || insn->type > INSN_LAST) {
300 WARN_FUNC("invalid instruction type %d",
301 insn->sec, insn->offset, insn->type);
306 hash_add(file->insn_hash, &insn->hash, insn->offset);
307 list_add_tail(&insn->list, &file->insn_list);
310 list_for_each_entry(func, &sec->symbol_list, list) {
311 if (func->type != STT_FUNC)
314 if (!find_insn(file, sec, func->offset)) {
315 WARN("%s(): can't find starting instruction",
320 func_for_each_insn(file, func, insn)
334 * Mark "ud2" instructions and manually annotated dead ends.
336 static int add_dead_ends(struct objtool_file *file)
340 struct instruction *insn;
344 * By default, "ud2" is a dead end unless otherwise annotated, because
345 * GCC 7 inserts it for certain divide-by-zero cases.
347 for_each_insn(file, insn)
348 if (insn->type == INSN_BUG)
349 insn->dead_end = true;
352 * Check for manually annotated dead ends.
354 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
358 list_for_each_entry(rela, &sec->rela_list, list) {
359 if (rela->sym->type != STT_SECTION) {
360 WARN("unexpected relocation symbol type in %s", sec->name);
363 insn = find_insn(file, rela->sym->sec, rela->addend);
365 insn = list_prev_entry(insn, list);
366 else if (rela->addend == rela->sym->sec->len) {
368 list_for_each_entry_reverse(insn, &file->insn_list, list) {
369 if (insn->sec == rela->sym->sec) {
376 WARN("can't find unreachable insn at %s+0x%x",
377 rela->sym->sec->name, rela->addend);
381 WARN("can't find unreachable insn at %s+0x%x",
382 rela->sym->sec->name, rela->addend);
386 insn->dead_end = true;
391 * These manually annotated reachable checks are needed for GCC 4.4,
392 * where the Linux unreachable() macro isn't supported. In that case
393 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
396 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
400 list_for_each_entry(rela, &sec->rela_list, list) {
401 if (rela->sym->type != STT_SECTION) {
402 WARN("unexpected relocation symbol type in %s", sec->name);
405 insn = find_insn(file, rela->sym->sec, rela->addend);
407 insn = list_prev_entry(insn, list);
408 else if (rela->addend == rela->sym->sec->len) {
410 list_for_each_entry_reverse(insn, &file->insn_list, list) {
411 if (insn->sec == rela->sym->sec) {
418 WARN("can't find reachable insn at %s+0x%x",
419 rela->sym->sec->name, rela->addend);
423 WARN("can't find reachable insn at %s+0x%x",
424 rela->sym->sec->name, rela->addend);
428 insn->dead_end = false;
435 * Warnings shouldn't be reported for ignored functions.
437 static void add_ignores(struct objtool_file *file)
439 struct instruction *insn;
443 for_each_sec(file, sec) {
444 list_for_each_entry(func, &sec->symbol_list, list) {
445 if (func->type != STT_FUNC)
448 if (!ignore_func(file, func))
451 func_for_each_insn_all(file, func, insn)
458 * FIXME: For now, just ignore any alternatives which add retpolines. This is
459 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
460 * But it at least allows objtool to understand the control flow *around* the
463 static int add_nospec_ignores(struct objtool_file *file)
467 struct instruction *insn;
469 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
473 list_for_each_entry(rela, &sec->rela_list, list) {
474 if (rela->sym->type != STT_SECTION) {
475 WARN("unexpected relocation symbol type in %s", sec->name);
479 insn = find_insn(file, rela->sym->sec, rela->addend);
481 WARN("bad .discard.nospec entry");
485 insn->ignore_alts = true;
492 * Find the destination instructions for all jumps.
494 static int add_jump_destinations(struct objtool_file *file)
496 struct instruction *insn;
498 struct section *dest_sec;
499 unsigned long dest_off;
501 for_each_insn(file, insn) {
502 if (insn->type != INSN_JUMP_CONDITIONAL &&
503 insn->type != INSN_JUMP_UNCONDITIONAL)
506 if (insn->offset == FAKE_JUMP_OFFSET)
509 rela = find_rela_by_dest_range(insn->sec, insn->offset,
512 dest_sec = insn->sec;
513 dest_off = insn->offset + insn->len + insn->immediate;
514 } else if (rela->sym->type == STT_SECTION) {
515 dest_sec = rela->sym->sec;
516 dest_off = rela->addend + 4;
517 } else if (rela->sym->sec->idx) {
518 dest_sec = rela->sym->sec;
519 dest_off = rela->sym->sym.st_value + rela->addend + 4;
520 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
522 * Retpoline jumps are really dynamic jumps in
523 * disguise, so convert them accordingly.
525 insn->type = INSN_JUMP_DYNAMIC;
526 insn->retpoline_safe = true;
534 insn->jump_dest = find_insn(file, dest_sec, dest_off);
535 if (!insn->jump_dest) {
538 * This is a special case where an alt instruction
539 * jumps past the end of the section. These are
540 * handled later in handle_group_alt().
542 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
545 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
546 insn->sec, insn->offset, dest_sec->name,
552 * For GCC 8+, create parent/child links for any cold
553 * subfunctions. This is _mostly_ redundant with a similar
554 * initialization in read_symbols().
556 * If a function has aliases, we want the *first* such function
557 * in the symbol table to be the subfunction's parent. In that
558 * case we overwrite the initialization done in read_symbols().
560 * However this code can't completely replace the
561 * read_symbols() code because this doesn't detect the case
562 * where the parent function's only reference to a subfunction
563 * is through a switch table.
565 if (insn->func && insn->jump_dest->func &&
566 insn->func != insn->jump_dest->func &&
567 !strstr(insn->func->name, ".cold.") &&
568 strstr(insn->jump_dest->func->name, ".cold.")) {
569 insn->func->cfunc = insn->jump_dest->func;
570 insn->jump_dest->func->pfunc = insn->func;
578 * Find the destination instructions for all calls.
580 static int add_call_destinations(struct objtool_file *file)
582 struct instruction *insn;
583 unsigned long dest_off;
586 for_each_insn(file, insn) {
587 if (insn->type != INSN_CALL)
590 rela = find_rela_by_dest_range(insn->sec, insn->offset,
593 dest_off = insn->offset + insn->len + insn->immediate;
594 insn->call_dest = find_symbol_by_offset(insn->sec,
597 if (!insn->call_dest && !insn->ignore) {
598 WARN_FUNC("unsupported intra-function call",
599 insn->sec, insn->offset);
601 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
605 } else if (rela->sym->type == STT_SECTION) {
606 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
608 if (!insn->call_dest ||
609 insn->call_dest->type != STT_FUNC) {
610 WARN_FUNC("can't find call dest symbol at %s+0x%x",
611 insn->sec, insn->offset,
612 rela->sym->sec->name,
617 insn->call_dest = rela->sym;
624 * The .alternatives section requires some extra special care, over and above
625 * what other special sections require:
627 * 1. Because alternatives are patched in-place, we need to insert a fake jump
628 * instruction at the end so that validate_branch() skips all the original
629 * replaced instructions when validating the new instruction path.
631 * 2. An added wrinkle is that the new instruction length might be zero. In
632 * that case the old instructions are replaced with noops. We simulate that
633 * by creating a fake jump as the only new instruction.
635 * 3. In some cases, the alternative section includes an instruction which
636 * conditionally jumps to the _end_ of the entry. We have to modify these
637 * jumps' destinations to point back to .text rather than the end of the
638 * entry in .altinstr_replacement.
640 * 4. It has been requested that we don't validate the !POPCNT feature path
641 * which is a "very very small percentage of machines".
643 static int handle_group_alt(struct objtool_file *file,
644 struct special_alt *special_alt,
645 struct instruction *orig_insn,
646 struct instruction **new_insn)
648 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
649 unsigned long dest_off;
651 last_orig_insn = NULL;
653 sec_for_each_insn_from(file, insn) {
654 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
657 if (special_alt->skip_orig)
658 insn->type = INSN_NOP;
660 insn->alt_group = true;
661 last_orig_insn = insn;
664 if (next_insn_same_sec(file, last_orig_insn)) {
665 fake_jump = malloc(sizeof(*fake_jump));
667 WARN("malloc failed");
670 memset(fake_jump, 0, sizeof(*fake_jump));
671 INIT_LIST_HEAD(&fake_jump->alts);
672 clear_insn_state(&fake_jump->state);
674 fake_jump->sec = special_alt->new_sec;
675 fake_jump->offset = FAKE_JUMP_OFFSET;
676 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
677 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
678 fake_jump->func = orig_insn->func;
681 if (!special_alt->new_len) {
683 WARN("%s: empty alternative at end of section",
684 special_alt->orig_sec->name);
688 *new_insn = fake_jump;
692 last_new_insn = NULL;
694 sec_for_each_insn_from(file, insn) {
695 if (insn->offset >= special_alt->new_off + special_alt->new_len)
698 last_new_insn = insn;
700 insn->ignore = orig_insn->ignore_alts;
702 if (insn->type != INSN_JUMP_CONDITIONAL &&
703 insn->type != INSN_JUMP_UNCONDITIONAL)
706 if (!insn->immediate)
709 dest_off = insn->offset + insn->len + insn->immediate;
710 if (dest_off == special_alt->new_off + special_alt->new_len) {
712 WARN("%s: alternative jump to end of section",
713 special_alt->orig_sec->name);
716 insn->jump_dest = fake_jump;
719 if (!insn->jump_dest) {
720 WARN_FUNC("can't find alternative jump destination",
721 insn->sec, insn->offset);
726 if (!last_new_insn) {
727 WARN_FUNC("can't find last new alternative instruction",
728 special_alt->new_sec, special_alt->new_off);
733 list_add(&fake_jump->list, &last_new_insn->list);
739 * A jump table entry can either convert a nop to a jump or a jump to a nop.
740 * If the original instruction is a jump, make the alt entry an effective nop
741 * by just skipping the original instruction.
743 static int handle_jump_alt(struct objtool_file *file,
744 struct special_alt *special_alt,
745 struct instruction *orig_insn,
746 struct instruction **new_insn)
748 if (orig_insn->type == INSN_NOP)
751 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
752 WARN_FUNC("unsupported instruction at jump label",
753 orig_insn->sec, orig_insn->offset);
757 *new_insn = list_next_entry(orig_insn, list);
762 * Read all the special sections which have alternate instructions which can be
763 * patched in or redirected to at runtime. Each instruction having alternate
764 * instruction(s) has them added to its insn->alts list, which will be
765 * traversed in validate_branch().
767 static int add_special_section_alts(struct objtool_file *file)
769 struct list_head special_alts;
770 struct instruction *orig_insn, *new_insn;
771 struct special_alt *special_alt, *tmp;
772 struct alternative *alt;
775 ret = special_get_alts(file->elf, &special_alts);
779 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
781 orig_insn = find_insn(file, special_alt->orig_sec,
782 special_alt->orig_off);
784 WARN_FUNC("special: can't find orig instruction",
785 special_alt->orig_sec, special_alt->orig_off);
791 if (!special_alt->group || special_alt->new_len) {
792 new_insn = find_insn(file, special_alt->new_sec,
793 special_alt->new_off);
795 WARN_FUNC("special: can't find new instruction",
796 special_alt->new_sec,
797 special_alt->new_off);
803 if (special_alt->group) {
804 if (!special_alt->orig_len) {
805 WARN_FUNC("empty alternative entry",
806 orig_insn->sec, orig_insn->offset);
810 ret = handle_group_alt(file, special_alt, orig_insn,
814 } else if (special_alt->jump_or_nop) {
815 ret = handle_jump_alt(file, special_alt, orig_insn,
821 alt = malloc(sizeof(*alt));
823 WARN("malloc failed");
828 alt->insn = new_insn;
829 list_add_tail(&alt->list, &orig_insn->alts);
831 list_del(&special_alt->list);
839 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
840 struct rela *table, struct rela *next_table)
842 struct rela *rela = table;
843 struct instruction *alt_insn;
844 struct alternative *alt;
845 struct symbol *pfunc = insn->func->pfunc;
846 unsigned int prev_offset = 0;
848 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
849 if (rela == next_table)
852 /* Make sure the switch table entries are consecutive: */
853 if (prev_offset && rela->offset != prev_offset + 8)
856 /* Detect function pointers from contiguous objects: */
857 if (rela->sym->sec == pfunc->sec &&
858 rela->addend == pfunc->offset)
861 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
865 /* Make sure the jmp dest is in the function or subfunction: */
866 if (alt_insn->func->pfunc != pfunc)
869 alt = malloc(sizeof(*alt));
871 WARN("malloc failed");
875 alt->insn = alt_insn;
876 list_add_tail(&alt->list, &insn->alts);
877 prev_offset = rela->offset;
881 WARN_FUNC("can't find switch jump table",
882 insn->sec, insn->offset);
890 * find_switch_table() - Given a dynamic jump, find the switch jump table in
891 * .rodata associated with it.
893 * There are 3 basic patterns:
895 * 1. jmpq *[rodata addr](,%reg,8)
897 * This is the most common case by far. It jumps to an address in a simple
898 * jump table which is stored in .rodata.
900 * 2. jmpq *[rodata addr](%rip)
902 * This is caused by a rare GCC quirk, currently only seen in three driver
903 * functions in the kernel, only with certain obscure non-distro configs.
905 * As part of an optimization, GCC makes a copy of an existing switch jump
906 * table, modifies it, and then hard-codes the jump (albeit with an indirect
907 * jump) to use a single entry in the table. The rest of the jump table and
908 * some of its jump targets remain as dead code.
910 * In such a case we can just crudely ignore all unreachable instruction
911 * warnings for the entire object file. Ideally we would just ignore them
912 * for the function, but that would require redesigning the code quite a
913 * bit. And honestly that's just not worth doing: unreachable instruction
914 * warnings are of questionable value anyway, and this is such a rare issue.
916 * 3. mov [rodata addr],%reg1
917 * ... some instructions ...
918 * jmpq *(%reg1,%reg2,8)
920 * This is a fairly uncommon pattern which is new for GCC 6. As of this
921 * writing, there are 11 occurrences of it in the allmodconfig kernel.
923 * As of GCC 7 there are quite a few more of these and the 'in between' code
924 * is significant. Esp. with KASAN enabled some of the code between the mov
925 * and jmpq uses .rodata itself, which can confuse things.
927 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
928 * ensure the same register is used in the mov and jump instructions.
930 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
932 static struct rela *find_switch_table(struct objtool_file *file,
934 struct instruction *insn)
936 struct rela *text_rela, *rodata_rela;
937 struct instruction *orig_insn = insn;
938 struct section *rodata_sec;
939 unsigned long table_offset;
942 * Backward search using the @first_jump_src links, these help avoid
943 * much of the 'in between' code. Which avoids us getting confused by
947 &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func;
948 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
950 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
953 /* allow small jumps within the range */
954 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
956 (insn->jump_dest->offset <= insn->offset ||
957 insn->jump_dest->offset > orig_insn->offset))
960 /* look for a relocation which references .rodata */
961 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
963 if (!text_rela || text_rela->sym->type != STT_SECTION ||
964 !text_rela->sym->sec->rodata)
967 table_offset = text_rela->addend;
968 rodata_sec = text_rela->sym->sec;
970 if (text_rela->type == R_X86_64_PC32)
974 * Make sure the .rodata address isn't associated with a
975 * symbol. gcc jump tables are anonymous data.
977 if (find_symbol_containing(rodata_sec, table_offset))
980 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
983 * Use of RIP-relative switch jumps is quite rare, and
984 * indicates a rare GCC quirk/bug which can leave dead
987 if (text_rela->type == R_X86_64_PC32)
988 file->ignore_unreachables = true;
998 static int add_func_switch_tables(struct objtool_file *file,
1001 struct instruction *insn, *last = NULL, *prev_jump = NULL;
1002 struct rela *rela, *prev_rela = NULL;
1005 func_for_each_insn_all(file, func, insn) {
1010 * Store back-pointers for unconditional forward jumps such
1011 * that find_switch_table() can back-track using those and
1012 * avoid some potentially confusing code.
1014 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1015 insn->offset > last->offset &&
1016 insn->jump_dest->offset > insn->offset &&
1017 !insn->jump_dest->first_jump_src) {
1019 insn->jump_dest->first_jump_src = insn;
1020 last = insn->jump_dest;
1023 if (insn->type != INSN_JUMP_DYNAMIC)
1026 rela = find_switch_table(file, func, insn);
1031 * We found a switch table, but we don't know yet how big it
1032 * is. Don't add it until we reach the end of the function or
1033 * the beginning of another switch table in the same function.
1036 ret = add_switch_table(file, prev_jump, prev_rela, rela);
1046 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1055 * For some switch statements, gcc generates a jump table in the .rodata
1056 * section which contains a list of addresses within the function to jump to.
1057 * This finds these jump tables and adds them to the insn->alts lists.
1059 static int add_switch_table_alts(struct objtool_file *file)
1061 struct section *sec;
1062 struct symbol *func;
1068 for_each_sec(file, sec) {
1069 list_for_each_entry(func, &sec->symbol_list, list) {
1070 if (func->type != STT_FUNC)
1073 ret = add_func_switch_tables(file, func);
1082 static int read_unwind_hints(struct objtool_file *file)
1084 struct section *sec, *relasec;
1086 struct unwind_hint *hint;
1087 struct instruction *insn;
1088 struct cfi_reg *cfa;
1091 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1095 relasec = sec->rela;
1097 WARN("missing .rela.discard.unwind_hints section");
1101 if (sec->len % sizeof(struct unwind_hint)) {
1102 WARN("struct unwind_hint size mismatch");
1108 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1109 hint = (struct unwind_hint *)sec->data->d_buf + i;
1111 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1113 WARN("can't find rela for unwind_hints[%d]", i);
1117 insn = find_insn(file, rela->sym->sec, rela->addend);
1119 WARN("can't find insn for unwind_hints[%d]", i);
1123 cfa = &insn->state.cfa;
1125 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1129 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1130 insn->restore = true;
1137 switch (hint->sp_reg) {
1138 case ORC_REG_UNDEFINED:
1139 cfa->base = CFI_UNDEFINED;
1147 case ORC_REG_SP_INDIRECT:
1148 cfa->base = CFI_SP_INDIRECT;
1151 cfa->base = CFI_R10;
1154 cfa->base = CFI_R13;
1163 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1164 insn->sec, insn->offset, hint->sp_reg);
1168 cfa->offset = hint->sp_offset;
1169 insn->state.type = hint->type;
1170 insn->state.end = hint->end;
1176 static int read_retpoline_hints(struct objtool_file *file)
1178 struct section *sec;
1179 struct instruction *insn;
1182 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1186 list_for_each_entry(rela, &sec->rela_list, list) {
1187 if (rela->sym->type != STT_SECTION) {
1188 WARN("unexpected relocation symbol type in %s", sec->name);
1192 insn = find_insn(file, rela->sym->sec, rela->addend);
1194 WARN("bad .discard.retpoline_safe entry");
1198 if (insn->type != INSN_JUMP_DYNAMIC &&
1199 insn->type != INSN_CALL_DYNAMIC) {
1200 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1201 insn->sec, insn->offset);
1205 insn->retpoline_safe = true;
1211 static void mark_rodata(struct objtool_file *file)
1213 struct section *sec;
1217 * This searches for the .rodata section or multiple .rodata.func_name
1218 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1219 * rodata sections are ignored as they don't contain jump tables.
1221 for_each_sec(file, sec) {
1222 if (!strncmp(sec->name, ".rodata", 7) &&
1223 !strstr(sec->name, ".str1.")) {
1229 file->rodata = found;
1232 static int decode_sections(struct objtool_file *file)
1238 ret = decode_instructions(file);
1242 ret = add_dead_ends(file);
1248 ret = add_nospec_ignores(file);
1252 ret = add_jump_destinations(file);
1256 ret = add_special_section_alts(file);
1260 ret = add_call_destinations(file);
1264 ret = add_switch_table_alts(file);
1268 ret = read_unwind_hints(file);
1272 ret = read_retpoline_hints(file);
1279 static bool is_fentry_call(struct instruction *insn)
1281 if (insn->type == INSN_CALL &&
1282 insn->call_dest->type == STT_NOTYPE &&
1283 !strcmp(insn->call_dest->name, "__fentry__"))
1289 static bool has_modified_stack_frame(struct insn_state *state)
1293 if (state->cfa.base != initial_func_cfi.cfa.base ||
1294 state->cfa.offset != initial_func_cfi.cfa.offset ||
1295 state->stack_size != initial_func_cfi.cfa.offset ||
1299 for (i = 0; i < CFI_NUM_REGS; i++)
1300 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1301 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1307 static bool has_valid_stack_frame(struct insn_state *state)
1309 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1310 state->regs[CFI_BP].offset == -16)
1313 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1319 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1321 struct cfi_reg *cfa = &state->cfa;
1322 struct stack_op *op = &insn->stack_op;
1324 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1328 if (op->dest.type == OP_DEST_PUSH)
1332 if (op->src.type == OP_SRC_POP)
1335 /* add immediate to sp */
1336 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1337 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1338 cfa->offset -= op->src.offset;
1343 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1346 if (arch_callee_saved_reg(reg) &&
1347 state->regs[reg].base == CFI_UNDEFINED) {
1348 state->regs[reg].base = base;
1349 state->regs[reg].offset = offset;
1353 static void restore_reg(struct insn_state *state, unsigned char reg)
1355 state->regs[reg].base = CFI_UNDEFINED;
1356 state->regs[reg].offset = 0;
1360 * A note about DRAP stack alignment:
1362 * GCC has the concept of a DRAP register, which is used to help keep track of
1363 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1364 * register. The typical DRAP pattern is:
1366 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1367 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1368 * 41 ff 72 f8 pushq -0x8(%r10)
1370 * 48 89 e5 mov %rsp,%rbp
1377 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1380 * There are some variations in the epilogues, like:
1388 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1393 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1394 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1395 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1396 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1398 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1401 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1402 * restored beforehand:
1405 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1406 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1408 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1412 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1414 struct stack_op *op = &insn->stack_op;
1415 struct cfi_reg *cfa = &state->cfa;
1416 struct cfi_reg *regs = state->regs;
1418 /* stack operations don't make sense with an undefined CFA */
1419 if (cfa->base == CFI_UNDEFINED) {
1421 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1427 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1428 return update_insn_state_regs(insn, state);
1430 switch (op->dest.type) {
1433 switch (op->src.type) {
1436 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1437 cfa->base == CFI_SP &&
1438 regs[CFI_BP].base == CFI_CFA &&
1439 regs[CFI_BP].offset == -cfa->offset) {
1441 /* mov %rsp, %rbp */
1442 cfa->base = op->dest.reg;
1443 state->bp_scratch = false;
1446 else if (op->src.reg == CFI_SP &&
1447 op->dest.reg == CFI_BP && state->drap) {
1449 /* drap: mov %rsp, %rbp */
1450 regs[CFI_BP].base = CFI_BP;
1451 regs[CFI_BP].offset = -state->stack_size;
1452 state->bp_scratch = false;
1455 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1460 * This is needed for the rare case where GCC
1467 state->vals[op->dest.reg].base = CFI_CFA;
1468 state->vals[op->dest.reg].offset = -state->stack_size;
1471 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1472 cfa->base == CFI_BP) {
1477 * Restore the original stack pointer (Clang).
1479 state->stack_size = -state->regs[CFI_BP].offset;
1482 else if (op->dest.reg == cfa->base) {
1484 /* mov %reg, %rsp */
1485 if (cfa->base == CFI_SP &&
1486 state->vals[op->src.reg].base == CFI_CFA) {
1489 * This is needed for the rare case
1490 * where GCC does something dumb like:
1492 * lea 0x8(%rsp), %rcx
1496 cfa->offset = -state->vals[op->src.reg].offset;
1497 state->stack_size = cfa->offset;
1500 cfa->base = CFI_UNDEFINED;
1508 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1511 state->stack_size -= op->src.offset;
1512 if (cfa->base == CFI_SP)
1513 cfa->offset -= op->src.offset;
1517 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1519 /* lea disp(%rbp), %rsp */
1520 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1524 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1526 /* drap: lea disp(%rsp), %drap */
1527 state->drap_reg = op->dest.reg;
1530 * lea disp(%rsp), %reg
1532 * This is needed for the rare case where GCC
1533 * does something dumb like:
1535 * lea 0x8(%rsp), %rcx
1539 state->vals[op->dest.reg].base = CFI_CFA;
1540 state->vals[op->dest.reg].offset = \
1541 -state->stack_size + op->src.offset;
1546 if (state->drap && op->dest.reg == CFI_SP &&
1547 op->src.reg == state->drap_reg) {
1549 /* drap: lea disp(%drap), %rsp */
1551 cfa->offset = state->stack_size = -op->src.offset;
1552 state->drap_reg = CFI_UNDEFINED;
1553 state->drap = false;
1557 if (op->dest.reg == state->cfa.base) {
1558 WARN_FUNC("unsupported stack register modification",
1559 insn->sec, insn->offset);
1566 if (op->dest.reg != CFI_SP ||
1567 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1568 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1569 WARN_FUNC("unsupported stack pointer realignment",
1570 insn->sec, insn->offset);
1574 if (state->drap_reg != CFI_UNDEFINED) {
1575 /* drap: and imm, %rsp */
1576 cfa->base = state->drap_reg;
1577 cfa->offset = state->stack_size = 0;
1582 * Older versions of GCC (4.8ish) realign the stack
1583 * without DRAP, with a frame pointer.
1589 if (!state->drap && op->dest.type == OP_DEST_REG &&
1590 op->dest.reg == cfa->base) {
1596 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1597 op->dest.type == OP_DEST_REG &&
1598 op->dest.reg == state->drap_reg &&
1599 state->drap_offset == -state->stack_size) {
1601 /* drap: pop %drap */
1602 cfa->base = state->drap_reg;
1604 state->drap_offset = -1;
1606 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1609 restore_reg(state, op->dest.reg);
1612 state->stack_size -= 8;
1613 if (cfa->base == CFI_SP)
1618 case OP_SRC_REG_INDIRECT:
1619 if (state->drap && op->src.reg == CFI_BP &&
1620 op->src.offset == state->drap_offset) {
1622 /* drap: mov disp(%rbp), %drap */
1623 cfa->base = state->drap_reg;
1625 state->drap_offset = -1;
1628 if (state->drap && op->src.reg == CFI_BP &&
1629 op->src.offset == regs[op->dest.reg].offset) {
1631 /* drap: mov disp(%rbp), %reg */
1632 restore_reg(state, op->dest.reg);
1634 } else if (op->src.reg == cfa->base &&
1635 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1637 /* mov disp(%rbp), %reg */
1638 /* mov disp(%rsp), %reg */
1639 restore_reg(state, op->dest.reg);
1645 WARN_FUNC("unknown stack-related instruction",
1646 insn->sec, insn->offset);
1653 state->stack_size += 8;
1654 if (cfa->base == CFI_SP)
1657 if (op->src.type != OP_SRC_REG)
1661 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1663 /* drap: push %drap */
1664 cfa->base = CFI_BP_INDIRECT;
1665 cfa->offset = -state->stack_size;
1667 /* save drap so we know when to restore it */
1668 state->drap_offset = -state->stack_size;
1670 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1672 /* drap: push %rbp */
1673 state->stack_size = 0;
1675 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1677 /* drap: push %reg */
1678 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1684 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1687 /* detect when asm code uses rbp as a scratch register */
1688 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1689 cfa->base != CFI_BP)
1690 state->bp_scratch = true;
1693 case OP_DEST_REG_INDIRECT:
1696 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1698 /* drap: mov %drap, disp(%rbp) */
1699 cfa->base = CFI_BP_INDIRECT;
1700 cfa->offset = op->dest.offset;
1702 /* save drap offset so we know when to restore it */
1703 state->drap_offset = op->dest.offset;
1706 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1708 /* drap: mov reg, disp(%rbp) */
1709 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1712 } else if (op->dest.reg == cfa->base) {
1714 /* mov reg, disp(%rbp) */
1715 /* mov reg, disp(%rsp) */
1716 save_reg(state, op->src.reg, CFI_CFA,
1717 op->dest.offset - state->cfa.offset);
1723 if ((!state->drap && cfa->base != CFI_BP) ||
1724 (state->drap && cfa->base != state->drap_reg)) {
1725 WARN_FUNC("leave instruction with modified stack frame",
1726 insn->sec, insn->offset);
1730 /* leave (mov %rbp, %rsp; pop %rbp) */
1732 state->stack_size = -state->regs[CFI_BP].offset - 8;
1733 restore_reg(state, CFI_BP);
1743 if (op->src.type != OP_SRC_POP) {
1744 WARN_FUNC("unknown stack-related memory operation",
1745 insn->sec, insn->offset);
1750 state->stack_size -= 8;
1751 if (cfa->base == CFI_SP)
1757 WARN_FUNC("unknown stack-related instruction",
1758 insn->sec, insn->offset);
1765 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1767 struct insn_state *state1 = &insn->state, *state2 = state;
1770 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1771 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1772 insn->sec, insn->offset,
1773 state1->cfa.base, state1->cfa.offset,
1774 state2->cfa.base, state2->cfa.offset);
1776 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1777 for (i = 0; i < CFI_NUM_REGS; i++) {
1778 if (!memcmp(&state1->regs[i], &state2->regs[i],
1779 sizeof(struct cfi_reg)))
1782 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1783 insn->sec, insn->offset,
1784 i, state1->regs[i].base, state1->regs[i].offset,
1785 i, state2->regs[i].base, state2->regs[i].offset);
1789 } else if (state1->type != state2->type) {
1790 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1791 insn->sec, insn->offset, state1->type, state2->type);
1793 } else if (state1->drap != state2->drap ||
1794 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1795 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1796 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1797 insn->sec, insn->offset,
1798 state1->drap, state1->drap_reg, state1->drap_offset,
1799 state2->drap, state2->drap_reg, state2->drap_offset);
1808 * Follow the branch starting at the given instruction, and recursively follow
1809 * any other branches (jumps). Meanwhile, track the frame pointer state at
1810 * each instruction and validate all the rules described in
1811 * tools/objtool/Documentation/stack-validation.txt.
1813 static int validate_branch(struct objtool_file *file, struct instruction *first,
1814 struct insn_state state)
1816 struct alternative *alt;
1817 struct instruction *insn, *next_insn;
1818 struct section *sec;
1819 struct symbol *func = NULL;
1825 if (insn->alt_group && list_empty(&insn->alts)) {
1826 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1832 next_insn = next_insn_same_sec(file, insn);
1834 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1835 WARN("%s() falls through to next function %s()",
1836 func->name, insn->func->name);
1841 func = insn->func->pfunc;
1843 if (func && insn->ignore) {
1844 WARN_FUNC("BUG: why am I validating an ignored function?",
1849 if (insn->visited) {
1850 if (!insn->hint && !insn_state_match(insn, &state))
1857 if (insn->restore) {
1858 struct instruction *save_insn, *i;
1862 func_for_each_insn_continue_reverse(file, insn->func, i) {
1870 WARN_FUNC("no corresponding CFI save for CFI restore",
1875 if (!save_insn->visited) {
1877 * Oops, no state to copy yet.
1878 * Hopefully we can reach this
1879 * instruction from another branch
1880 * after the save insn has been
1886 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1891 insn->state = save_insn->state;
1894 state = insn->state;
1897 insn->state = state;
1899 insn->visited = true;
1901 if (!insn->ignore_alts) {
1902 list_for_each_entry(alt, &insn->alts, list) {
1903 ret = validate_branch(file, alt->insn, state);
1909 switch (insn->type) {
1912 if (func && has_modified_stack_frame(&state)) {
1913 WARN_FUNC("return with modified stack frame",
1918 if (state.bp_scratch) {
1919 WARN("%s uses BP as a scratch register",
1927 if (is_fentry_call(insn))
1930 ret = dead_end_function(file, insn->call_dest);
1937 case INSN_CALL_DYNAMIC:
1938 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1939 WARN_FUNC("call without frame pointer save/setup",
1945 case INSN_JUMP_CONDITIONAL:
1946 case INSN_JUMP_UNCONDITIONAL:
1947 if (insn->jump_dest &&
1948 (!func || !insn->jump_dest->func ||
1949 insn->jump_dest->func->pfunc == func)) {
1950 ret = validate_branch(file, insn->jump_dest,
1955 } else if (func && has_modified_stack_frame(&state)) {
1956 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1961 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1966 case INSN_JUMP_DYNAMIC:
1967 if (func && list_empty(&insn->alts) &&
1968 has_modified_stack_frame(&state)) {
1969 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1976 case INSN_CONTEXT_SWITCH:
1977 if (func && (!next_insn || !next_insn->hint)) {
1978 WARN_FUNC("unsupported instruction in callable function",
1985 if (update_insn_state(insn, &state))
1998 if (state.cfa.base == CFI_UNDEFINED)
2000 WARN("%s: unexpected end of section", sec->name);
2010 static int validate_unwind_hints(struct objtool_file *file)
2012 struct instruction *insn;
2013 int ret, warnings = 0;
2014 struct insn_state state;
2019 clear_insn_state(&state);
2021 for_each_insn(file, insn) {
2022 if (insn->hint && !insn->visited) {
2023 ret = validate_branch(file, insn, state);
2031 static int validate_retpoline(struct objtool_file *file)
2033 struct instruction *insn;
2036 for_each_insn(file, insn) {
2037 if (insn->type != INSN_JUMP_DYNAMIC &&
2038 insn->type != INSN_CALL_DYNAMIC)
2041 if (insn->retpoline_safe)
2045 * .init.text code is ran before userspace and thus doesn't
2046 * strictly need retpolines, except for modules which are
2047 * loaded late, they very much do need retpoline in their
2050 if (!strcmp(insn->sec->name, ".init.text") && !module)
2053 WARN_FUNC("indirect %s found in RETPOLINE build",
2054 insn->sec, insn->offset,
2055 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2063 static bool is_kasan_insn(struct instruction *insn)
2065 return (insn->type == INSN_CALL &&
2066 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2069 static bool is_ubsan_insn(struct instruction *insn)
2071 return (insn->type == INSN_CALL &&
2072 !strcmp(insn->call_dest->name,
2073 "__ubsan_handle_builtin_unreachable"));
2076 static bool ignore_unreachable_insn(struct instruction *insn)
2080 if (insn->ignore || insn->type == INSN_NOP)
2084 * Ignore any unused exceptions. This can happen when a whitelisted
2085 * function has an exception table entry.
2087 * Also ignore alternative replacement instructions. This can happen
2088 * when a whitelisted function uses one of the ALTERNATIVE macros.
2090 if (!strcmp(insn->sec->name, ".fixup") ||
2091 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2092 !strcmp(insn->sec->name, ".altinstr_aux"))
2099 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2100 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2101 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2102 * (or occasionally a JMP to UD2).
2104 if (list_prev_entry(insn, list)->dead_end &&
2105 (insn->type == INSN_BUG ||
2106 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2107 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2111 * Check if this (or a subsequent) instruction is related to
2112 * CONFIG_UBSAN or CONFIG_KASAN.
2114 * End the search at 5 instructions to avoid going into the weeds.
2116 for (i = 0; i < 5; i++) {
2118 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2121 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2122 if (insn->jump_dest &&
2123 insn->jump_dest->func == insn->func) {
2124 insn = insn->jump_dest;
2131 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2134 insn = list_next_entry(insn, list);
2140 static int validate_functions(struct objtool_file *file)
2142 struct section *sec;
2143 struct symbol *func;
2144 struct instruction *insn;
2145 struct insn_state state;
2146 int ret, warnings = 0;
2148 clear_insn_state(&state);
2150 state.cfa = initial_func_cfi.cfa;
2151 memcpy(&state.regs, &initial_func_cfi.regs,
2152 CFI_NUM_REGS * sizeof(struct cfi_reg));
2153 state.stack_size = initial_func_cfi.cfa.offset;
2155 for_each_sec(file, sec) {
2156 list_for_each_entry(func, &sec->symbol_list, list) {
2157 if (func->type != STT_FUNC || func->pfunc != func)
2160 insn = find_insn(file, sec, func->offset);
2161 if (!insn || insn->ignore)
2164 ret = validate_branch(file, insn, state);
2172 static int validate_reachable_instructions(struct objtool_file *file)
2174 struct instruction *insn;
2176 if (file->ignore_unreachables)
2179 for_each_insn(file, insn) {
2180 if (insn->visited || ignore_unreachable_insn(insn))
2183 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2190 static void cleanup(struct objtool_file *file)
2192 struct instruction *insn, *tmpinsn;
2193 struct alternative *alt, *tmpalt;
2195 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2196 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2197 list_del(&alt->list);
2200 list_del(&insn->list);
2201 hash_del(&insn->hash);
2204 elf_close(file->elf);
2207 static struct objtool_file file;
2209 int check(const char *_objname, bool orc)
2211 int ret, warnings = 0;
2215 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2219 INIT_LIST_HEAD(&file.insn_list);
2220 hash_init(file.insn_hash);
2221 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2222 file.c_file = find_section_by_name(file.elf, ".comment");
2223 file.ignore_unreachables = no_unreachable;
2226 arch_initial_func_cfi_state(&initial_func_cfi);
2228 ret = decode_sections(&file);
2233 if (list_empty(&file.insn_list))
2237 ret = validate_retpoline(&file);
2243 ret = validate_functions(&file);
2248 ret = validate_unwind_hints(&file);
2254 ret = validate_reachable_instructions(&file);
2261 ret = create_orc(&file);
2265 ret = create_orc_sections(&file);
2269 ret = elf_write(file.elf);
2277 /* ignore warnings for now until we get all the code cleaned up */
2278 if (ret || warnings)