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>
32 struct list_head list;
33 struct instruction *insn;
37 struct cfi_state initial_func_cfi;
39 struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
42 struct instruction *insn;
44 hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 if (insn->sec == sec && insn->offset == offset)
51 static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 struct instruction *insn)
54 struct instruction *next = list_next_entry(insn, list);
56 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
62 static struct instruction *next_insn_same_func(struct objtool_file *file,
63 struct instruction *insn)
65 struct instruction *next = list_next_entry(insn, list);
66 struct symbol *func = insn->func;
71 if (&next->list != &file->insn_list && next->func == func)
74 /* Check if we're already in the subfunction: */
75 if (func == func->cfunc)
78 /* Move to the subfunction: */
79 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
82 #define func_for_each_insn_all(file, func, insn) \
83 for (insn = find_insn(file, func->sec, func->offset); \
85 insn = next_insn_same_func(file, insn))
87 #define func_for_each_insn(file, func, insn) \
88 for (insn = find_insn(file, func->sec, func->offset); \
89 insn && &insn->list != &file->insn_list && \
90 insn->sec == func->sec && \
91 insn->offset < func->offset + func->len; \
92 insn = list_next_entry(insn, list))
94 #define func_for_each_insn_continue_reverse(file, func, insn) \
95 for (insn = list_prev_entry(insn, list); \
96 &insn->list != &file->insn_list && \
97 insn->sec == func->sec && insn->offset >= func->offset; \
98 insn = list_prev_entry(insn, list))
100 #define sec_for_each_insn_from(file, insn) \
101 for (; insn; insn = next_insn_same_sec(file, insn))
103 #define sec_for_each_insn_continue(file, insn) \
104 for (insn = next_insn_same_sec(file, insn); insn; \
105 insn = next_insn_same_sec(file, insn))
108 * Check if the function has been manually whitelisted with the
109 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
110 * due to its use of a context switching instruction.
112 static bool ignore_func(struct objtool_file *file, struct symbol *func)
116 /* check for STACK_FRAME_NON_STANDARD */
117 if (file->whitelist && file->whitelist->rela)
118 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
119 if (rela->sym->type == STT_SECTION &&
120 rela->sym->sec == func->sec &&
121 rela->addend == func->offset)
123 if (rela->sym->type == STT_FUNC && rela->sym == func)
131 * This checks to see if the given function is a "noreturn" function.
133 * For global functions which are outside the scope of this object file, we
134 * have to keep a manual list of them.
136 * For local functions, we have to detect them manually by simply looking for
137 * the lack of a return instruction.
144 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
148 struct instruction *insn;
152 * Unfortunately these have to be hard coded because the noreturn
153 * attribute isn't provided in ELF data.
155 static const char * const global_noreturns[] = {
160 "__module_put_and_exit",
162 "kvm_spurious_fault",
167 "machine_real_restart",
168 "rewind_stack_do_exit",
171 if (func->bind == STB_WEAK)
174 if (func->bind == STB_GLOBAL)
175 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
176 if (!strcmp(func->name, global_noreturns[i]))
182 insn = find_insn(file, func->sec, func->offset);
186 func_for_each_insn_all(file, func, insn) {
189 if (insn->type == INSN_RETURN)
197 * A function can have a sibling call instead of a return. In that
198 * case, the function's dead-end status depends on whether the target
199 * of the sibling call returns.
201 func_for_each_insn_all(file, func, insn) {
202 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
203 struct instruction *dest = insn->jump_dest;
206 /* sibling call to another file */
209 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
211 /* local sibling call */
212 if (recursion == 5) {
214 * Infinite recursion: two functions
215 * have sibling calls to each other.
216 * This is a very rare case. It means
217 * they aren't dead ends.
222 return __dead_end_function(file, dest->func,
227 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
235 static int dead_end_function(struct objtool_file *file, struct symbol *func)
237 return __dead_end_function(file, func, 0);
240 static void clear_insn_state(struct insn_state *state)
244 memset(state, 0, sizeof(*state));
245 state->cfa.base = CFI_UNDEFINED;
246 for (i = 0; i < CFI_NUM_REGS; i++) {
247 state->regs[i].base = CFI_UNDEFINED;
248 state->vals[i].base = CFI_UNDEFINED;
250 state->drap_reg = CFI_UNDEFINED;
251 state->drap_offset = -1;
255 * Call the arch-specific instruction decoder for all the instructions and add
256 * them to the global instruction list.
258 static int decode_instructions(struct objtool_file *file)
262 unsigned long offset;
263 struct instruction *insn;
266 for_each_sec(file, sec) {
268 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
271 if (strcmp(sec->name, ".altinstr_replacement") &&
272 strcmp(sec->name, ".altinstr_aux") &&
273 strncmp(sec->name, ".discard.", 9))
276 for (offset = 0; offset < sec->len; offset += insn->len) {
277 insn = malloc(sizeof(*insn));
279 WARN("malloc failed");
282 memset(insn, 0, sizeof(*insn));
283 INIT_LIST_HEAD(&insn->alts);
284 clear_insn_state(&insn->state);
287 insn->offset = offset;
289 ret = arch_decode_instruction(file->elf, sec, offset,
291 &insn->len, &insn->type,
297 if (!insn->type || insn->type > INSN_LAST) {
298 WARN_FUNC("invalid instruction type %d",
299 insn->sec, insn->offset, insn->type);
304 hash_add(file->insn_hash, &insn->hash, insn->offset);
305 list_add_tail(&insn->list, &file->insn_list);
308 list_for_each_entry(func, &sec->symbol_list, list) {
309 if (func->type != STT_FUNC)
312 if (!find_insn(file, sec, func->offset)) {
313 WARN("%s(): can't find starting instruction",
318 func_for_each_insn(file, func, insn)
332 * Mark "ud2" instructions and manually annotated dead ends.
334 static int add_dead_ends(struct objtool_file *file)
338 struct instruction *insn;
342 * By default, "ud2" is a dead end unless otherwise annotated, because
343 * GCC 7 inserts it for certain divide-by-zero cases.
345 for_each_insn(file, insn)
346 if (insn->type == INSN_BUG)
347 insn->dead_end = true;
350 * Check for manually annotated dead ends.
352 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
356 list_for_each_entry(rela, &sec->rela_list, list) {
357 if (rela->sym->type != STT_SECTION) {
358 WARN("unexpected relocation symbol type in %s", sec->name);
361 insn = find_insn(file, rela->sym->sec, rela->addend);
363 insn = list_prev_entry(insn, list);
364 else if (rela->addend == rela->sym->sec->len) {
366 list_for_each_entry_reverse(insn, &file->insn_list, list) {
367 if (insn->sec == rela->sym->sec) {
374 WARN("can't find unreachable insn at %s+0x%x",
375 rela->sym->sec->name, rela->addend);
379 WARN("can't find unreachable insn at %s+0x%x",
380 rela->sym->sec->name, rela->addend);
384 insn->dead_end = true;
389 * These manually annotated reachable checks are needed for GCC 4.4,
390 * where the Linux unreachable() macro isn't supported. In that case
391 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
394 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
398 list_for_each_entry(rela, &sec->rela_list, list) {
399 if (rela->sym->type != STT_SECTION) {
400 WARN("unexpected relocation symbol type in %s", sec->name);
403 insn = find_insn(file, rela->sym->sec, rela->addend);
405 insn = list_prev_entry(insn, list);
406 else if (rela->addend == rela->sym->sec->len) {
408 list_for_each_entry_reverse(insn, &file->insn_list, list) {
409 if (insn->sec == rela->sym->sec) {
416 WARN("can't find reachable insn at %s+0x%x",
417 rela->sym->sec->name, rela->addend);
421 WARN("can't find reachable insn at %s+0x%x",
422 rela->sym->sec->name, rela->addend);
426 insn->dead_end = false;
433 * Warnings shouldn't be reported for ignored functions.
435 static void add_ignores(struct objtool_file *file)
437 struct instruction *insn;
441 for_each_sec(file, sec) {
442 list_for_each_entry(func, &sec->symbol_list, list) {
443 if (func->type != STT_FUNC)
446 if (!ignore_func(file, func))
449 func_for_each_insn_all(file, func, insn)
456 * FIXME: For now, just ignore any alternatives which add retpolines. This is
457 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
458 * But it at least allows objtool to understand the control flow *around* the
461 static int add_nospec_ignores(struct objtool_file *file)
465 struct instruction *insn;
467 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
471 list_for_each_entry(rela, &sec->rela_list, list) {
472 if (rela->sym->type != STT_SECTION) {
473 WARN("unexpected relocation symbol type in %s", sec->name);
477 insn = find_insn(file, rela->sym->sec, rela->addend);
479 WARN("bad .discard.nospec entry");
483 insn->ignore_alts = true;
490 * Find the destination instructions for all jumps.
492 static int add_jump_destinations(struct objtool_file *file)
494 struct instruction *insn;
496 struct section *dest_sec;
497 unsigned long dest_off;
499 for_each_insn(file, insn) {
500 if (insn->type != INSN_JUMP_CONDITIONAL &&
501 insn->type != INSN_JUMP_UNCONDITIONAL)
507 rela = find_rela_by_dest_range(insn->sec, insn->offset,
510 dest_sec = insn->sec;
511 dest_off = insn->offset + insn->len + insn->immediate;
512 } else if (rela->sym->type == STT_SECTION) {
513 dest_sec = rela->sym->sec;
514 dest_off = rela->addend + 4;
515 } else if (rela->sym->sec->idx) {
516 dest_sec = rela->sym->sec;
517 dest_off = rela->sym->sym.st_value + rela->addend + 4;
518 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
520 * Retpoline jumps are really dynamic jumps in
521 * disguise, so convert them accordingly.
523 insn->type = INSN_JUMP_DYNAMIC;
524 insn->retpoline_safe = true;
532 insn->jump_dest = find_insn(file, dest_sec, dest_off);
533 if (!insn->jump_dest) {
536 * This is a special case where an alt instruction
537 * jumps past the end of the section. These are
538 * handled later in handle_group_alt().
540 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
543 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
544 insn->sec, insn->offset, dest_sec->name,
550 * For GCC 8+, create parent/child links for any cold
551 * subfunctions. This is _mostly_ redundant with a similar
552 * initialization in read_symbols().
554 * If a function has aliases, we want the *first* such function
555 * in the symbol table to be the subfunction's parent. In that
556 * case we overwrite the initialization done in read_symbols().
558 * However this code can't completely replace the
559 * read_symbols() code because this doesn't detect the case
560 * where the parent function's only reference to a subfunction
561 * is through a switch table.
563 if (insn->func && insn->jump_dest->func &&
564 insn->func != insn->jump_dest->func &&
565 !strstr(insn->func->name, ".cold.") &&
566 strstr(insn->jump_dest->func->name, ".cold.")) {
567 insn->func->cfunc = insn->jump_dest->func;
568 insn->jump_dest->func->pfunc = insn->func;
576 * Find the destination instructions for all calls.
578 static int add_call_destinations(struct objtool_file *file)
580 struct instruction *insn;
581 unsigned long dest_off;
584 for_each_insn(file, insn) {
585 if (insn->type != INSN_CALL)
588 rela = find_rela_by_dest_range(insn->sec, insn->offset,
591 dest_off = insn->offset + insn->len + insn->immediate;
592 insn->call_dest = find_symbol_by_offset(insn->sec,
595 if (!insn->call_dest && !insn->ignore) {
596 WARN_FUNC("unsupported intra-function call",
597 insn->sec, insn->offset);
599 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
603 } else if (rela->sym->type == STT_SECTION) {
604 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
606 if (!insn->call_dest ||
607 insn->call_dest->type != STT_FUNC) {
608 WARN_FUNC("can't find call dest symbol at %s+0x%x",
609 insn->sec, insn->offset,
610 rela->sym->sec->name,
615 insn->call_dest = rela->sym;
622 * The .alternatives section requires some extra special care, over and above
623 * what other special sections require:
625 * 1. Because alternatives are patched in-place, we need to insert a fake jump
626 * instruction at the end so that validate_branch() skips all the original
627 * replaced instructions when validating the new instruction path.
629 * 2. An added wrinkle is that the new instruction length might be zero. In
630 * that case the old instructions are replaced with noops. We simulate that
631 * by creating a fake jump as the only new instruction.
633 * 3. In some cases, the alternative section includes an instruction which
634 * conditionally jumps to the _end_ of the entry. We have to modify these
635 * jumps' destinations to point back to .text rather than the end of the
636 * entry in .altinstr_replacement.
638 * 4. It has been requested that we don't validate the !POPCNT feature path
639 * which is a "very very small percentage of machines".
641 static int handle_group_alt(struct objtool_file *file,
642 struct special_alt *special_alt,
643 struct instruction *orig_insn,
644 struct instruction **new_insn)
646 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
647 unsigned long dest_off;
649 last_orig_insn = NULL;
651 sec_for_each_insn_from(file, insn) {
652 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
655 if (special_alt->skip_orig)
656 insn->type = INSN_NOP;
658 insn->alt_group = true;
659 last_orig_insn = insn;
662 if (next_insn_same_sec(file, last_orig_insn)) {
663 fake_jump = malloc(sizeof(*fake_jump));
665 WARN("malloc failed");
668 memset(fake_jump, 0, sizeof(*fake_jump));
669 INIT_LIST_HEAD(&fake_jump->alts);
670 clear_insn_state(&fake_jump->state);
672 fake_jump->sec = special_alt->new_sec;
673 fake_jump->offset = -1;
674 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
675 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
676 fake_jump->ignore = true;
679 if (!special_alt->new_len) {
681 WARN("%s: empty alternative at end of section",
682 special_alt->orig_sec->name);
686 *new_insn = fake_jump;
690 last_new_insn = NULL;
692 sec_for_each_insn_from(file, insn) {
693 if (insn->offset >= special_alt->new_off + special_alt->new_len)
696 last_new_insn = insn;
698 insn->ignore = orig_insn->ignore_alts;
700 if (insn->type != INSN_JUMP_CONDITIONAL &&
701 insn->type != INSN_JUMP_UNCONDITIONAL)
704 if (!insn->immediate)
707 dest_off = insn->offset + insn->len + insn->immediate;
708 if (dest_off == special_alt->new_off + special_alt->new_len) {
710 WARN("%s: alternative jump to end of section",
711 special_alt->orig_sec->name);
714 insn->jump_dest = fake_jump;
717 if (!insn->jump_dest) {
718 WARN_FUNC("can't find alternative jump destination",
719 insn->sec, insn->offset);
724 if (!last_new_insn) {
725 WARN_FUNC("can't find last new alternative instruction",
726 special_alt->new_sec, special_alt->new_off);
731 list_add(&fake_jump->list, &last_new_insn->list);
737 * A jump table entry can either convert a nop to a jump or a jump to a nop.
738 * If the original instruction is a jump, make the alt entry an effective nop
739 * by just skipping the original instruction.
741 static int handle_jump_alt(struct objtool_file *file,
742 struct special_alt *special_alt,
743 struct instruction *orig_insn,
744 struct instruction **new_insn)
746 if (orig_insn->type == INSN_NOP)
749 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
750 WARN_FUNC("unsupported instruction at jump label",
751 orig_insn->sec, orig_insn->offset);
755 *new_insn = list_next_entry(orig_insn, list);
760 * Read all the special sections which have alternate instructions which can be
761 * patched in or redirected to at runtime. Each instruction having alternate
762 * instruction(s) has them added to its insn->alts list, which will be
763 * traversed in validate_branch().
765 static int add_special_section_alts(struct objtool_file *file)
767 struct list_head special_alts;
768 struct instruction *orig_insn, *new_insn;
769 struct special_alt *special_alt, *tmp;
770 struct alternative *alt;
773 ret = special_get_alts(file->elf, &special_alts);
777 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
779 orig_insn = find_insn(file, special_alt->orig_sec,
780 special_alt->orig_off);
782 WARN_FUNC("special: can't find orig instruction",
783 special_alt->orig_sec, special_alt->orig_off);
789 if (!special_alt->group || special_alt->new_len) {
790 new_insn = find_insn(file, special_alt->new_sec,
791 special_alt->new_off);
793 WARN_FUNC("special: can't find new instruction",
794 special_alt->new_sec,
795 special_alt->new_off);
801 if (special_alt->group) {
802 ret = handle_group_alt(file, special_alt, orig_insn,
806 } else if (special_alt->jump_or_nop) {
807 ret = handle_jump_alt(file, special_alt, orig_insn,
813 alt = malloc(sizeof(*alt));
815 WARN("malloc failed");
820 alt->insn = new_insn;
821 list_add_tail(&alt->list, &orig_insn->alts);
823 list_del(&special_alt->list);
831 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
832 struct rela *table, struct rela *next_table)
834 struct rela *rela = table;
835 struct instruction *alt_insn;
836 struct alternative *alt;
837 struct symbol *pfunc = insn->func->pfunc;
838 unsigned int prev_offset = 0;
840 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
841 if (rela == next_table)
844 /* Make sure the switch table entries are consecutive: */
845 if (prev_offset && rela->offset != prev_offset + 8)
848 /* Detect function pointers from contiguous objects: */
849 if (rela->sym->sec == pfunc->sec &&
850 rela->addend == pfunc->offset)
853 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
857 /* Make sure the jmp dest is in the function or subfunction: */
858 if (alt_insn->func->pfunc != pfunc)
861 alt = malloc(sizeof(*alt));
863 WARN("malloc failed");
867 alt->insn = alt_insn;
868 list_add_tail(&alt->list, &insn->alts);
869 prev_offset = rela->offset;
873 WARN_FUNC("can't find switch jump table",
874 insn->sec, insn->offset);
882 * find_switch_table() - Given a dynamic jump, find the switch jump table in
883 * .rodata associated with it.
885 * There are 3 basic patterns:
887 * 1. jmpq *[rodata addr](,%reg,8)
889 * This is the most common case by far. It jumps to an address in a simple
890 * jump table which is stored in .rodata.
892 * 2. jmpq *[rodata addr](%rip)
894 * This is caused by a rare GCC quirk, currently only seen in three driver
895 * functions in the kernel, only with certain obscure non-distro configs.
897 * As part of an optimization, GCC makes a copy of an existing switch jump
898 * table, modifies it, and then hard-codes the jump (albeit with an indirect
899 * jump) to use a single entry in the table. The rest of the jump table and
900 * some of its jump targets remain as dead code.
902 * In such a case we can just crudely ignore all unreachable instruction
903 * warnings for the entire object file. Ideally we would just ignore them
904 * for the function, but that would require redesigning the code quite a
905 * bit. And honestly that's just not worth doing: unreachable instruction
906 * warnings are of questionable value anyway, and this is such a rare issue.
908 * 3. mov [rodata addr],%reg1
909 * ... some instructions ...
910 * jmpq *(%reg1,%reg2,8)
912 * This is a fairly uncommon pattern which is new for GCC 6. As of this
913 * writing, there are 11 occurrences of it in the allmodconfig kernel.
915 * As of GCC 7 there are quite a few more of these and the 'in between' code
916 * is significant. Esp. with KASAN enabled some of the code between the mov
917 * and jmpq uses .rodata itself, which can confuse things.
919 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
920 * ensure the same register is used in the mov and jump instructions.
922 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
924 static struct rela *find_switch_table(struct objtool_file *file,
926 struct instruction *insn)
928 struct rela *text_rela, *rodata_rela;
929 struct instruction *orig_insn = insn;
930 struct section *rodata_sec;
931 unsigned long table_offset;
934 * Backward search using the @first_jump_src links, these help avoid
935 * much of the 'in between' code. Which avoids us getting confused by
939 &insn->list != &file->insn_list &&
940 insn->sec == func->sec &&
941 insn->offset >= func->offset;
943 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
945 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
948 /* allow small jumps within the range */
949 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
951 (insn->jump_dest->offset <= insn->offset ||
952 insn->jump_dest->offset > orig_insn->offset))
955 /* look for a relocation which references .rodata */
956 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
958 if (!text_rela || text_rela->sym->type != STT_SECTION ||
959 !text_rela->sym->sec->rodata)
962 table_offset = text_rela->addend;
963 rodata_sec = text_rela->sym->sec;
965 if (text_rela->type == R_X86_64_PC32)
969 * Make sure the .rodata address isn't associated with a
970 * symbol. gcc jump tables are anonymous data.
972 if (find_symbol_containing(rodata_sec, table_offset))
975 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
978 * Use of RIP-relative switch jumps is quite rare, and
979 * indicates a rare GCC quirk/bug which can leave dead
982 if (text_rela->type == R_X86_64_PC32)
983 file->ignore_unreachables = true;
993 static int add_func_switch_tables(struct objtool_file *file,
996 struct instruction *insn, *last = NULL, *prev_jump = NULL;
997 struct rela *rela, *prev_rela = NULL;
1000 func_for_each_insn_all(file, func, insn) {
1005 * Store back-pointers for unconditional forward jumps such
1006 * that find_switch_table() can back-track using those and
1007 * avoid some potentially confusing code.
1009 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1010 insn->offset > last->offset &&
1011 insn->jump_dest->offset > insn->offset &&
1012 !insn->jump_dest->first_jump_src) {
1014 insn->jump_dest->first_jump_src = insn;
1015 last = insn->jump_dest;
1018 if (insn->type != INSN_JUMP_DYNAMIC)
1021 rela = find_switch_table(file, func, insn);
1026 * We found a switch table, but we don't know yet how big it
1027 * is. Don't add it until we reach the end of the function or
1028 * the beginning of another switch table in the same function.
1031 ret = add_switch_table(file, prev_jump, prev_rela, rela);
1041 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1050 * For some switch statements, gcc generates a jump table in the .rodata
1051 * section which contains a list of addresses within the function to jump to.
1052 * This finds these jump tables and adds them to the insn->alts lists.
1054 static int add_switch_table_alts(struct objtool_file *file)
1056 struct section *sec;
1057 struct symbol *func;
1063 for_each_sec(file, sec) {
1064 list_for_each_entry(func, &sec->symbol_list, list) {
1065 if (func->type != STT_FUNC)
1068 ret = add_func_switch_tables(file, func);
1077 static int read_unwind_hints(struct objtool_file *file)
1079 struct section *sec, *relasec;
1081 struct unwind_hint *hint;
1082 struct instruction *insn;
1083 struct cfi_reg *cfa;
1086 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1090 relasec = sec->rela;
1092 WARN("missing .rela.discard.unwind_hints section");
1096 if (sec->len % sizeof(struct unwind_hint)) {
1097 WARN("struct unwind_hint size mismatch");
1103 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1104 hint = (struct unwind_hint *)sec->data->d_buf + i;
1106 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1108 WARN("can't find rela for unwind_hints[%d]", i);
1112 insn = find_insn(file, rela->sym->sec, rela->addend);
1114 WARN("can't find insn for unwind_hints[%d]", i);
1118 cfa = &insn->state.cfa;
1120 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1124 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1125 insn->restore = true;
1132 switch (hint->sp_reg) {
1133 case ORC_REG_UNDEFINED:
1134 cfa->base = CFI_UNDEFINED;
1142 case ORC_REG_SP_INDIRECT:
1143 cfa->base = CFI_SP_INDIRECT;
1146 cfa->base = CFI_R10;
1149 cfa->base = CFI_R13;
1158 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1159 insn->sec, insn->offset, hint->sp_reg);
1163 cfa->offset = hint->sp_offset;
1164 insn->state.type = hint->type;
1165 insn->state.end = hint->end;
1171 static int read_retpoline_hints(struct objtool_file *file)
1173 struct section *sec;
1174 struct instruction *insn;
1177 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1181 list_for_each_entry(rela, &sec->rela_list, list) {
1182 if (rela->sym->type != STT_SECTION) {
1183 WARN("unexpected relocation symbol type in %s", sec->name);
1187 insn = find_insn(file, rela->sym->sec, rela->addend);
1189 WARN("bad .discard.retpoline_safe entry");
1193 if (insn->type != INSN_JUMP_DYNAMIC &&
1194 insn->type != INSN_CALL_DYNAMIC) {
1195 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1196 insn->sec, insn->offset);
1200 insn->retpoline_safe = true;
1206 static void mark_rodata(struct objtool_file *file)
1208 struct section *sec;
1212 * This searches for the .rodata section or multiple .rodata.func_name
1213 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1214 * rodata sections are ignored as they don't contain jump tables.
1216 for_each_sec(file, sec) {
1217 if (!strncmp(sec->name, ".rodata", 7) &&
1218 !strstr(sec->name, ".str1.")) {
1224 file->rodata = found;
1227 static int decode_sections(struct objtool_file *file)
1233 ret = decode_instructions(file);
1237 ret = add_dead_ends(file);
1243 ret = add_nospec_ignores(file);
1247 ret = add_jump_destinations(file);
1251 ret = add_special_section_alts(file);
1255 ret = add_call_destinations(file);
1259 ret = add_switch_table_alts(file);
1263 ret = read_unwind_hints(file);
1267 ret = read_retpoline_hints(file);
1274 static bool is_fentry_call(struct instruction *insn)
1276 if (insn->type == INSN_CALL &&
1277 insn->call_dest->type == STT_NOTYPE &&
1278 !strcmp(insn->call_dest->name, "__fentry__"))
1284 static bool has_modified_stack_frame(struct insn_state *state)
1288 if (state->cfa.base != initial_func_cfi.cfa.base ||
1289 state->cfa.offset != initial_func_cfi.cfa.offset ||
1290 state->stack_size != initial_func_cfi.cfa.offset ||
1294 for (i = 0; i < CFI_NUM_REGS; i++)
1295 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1296 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1302 static bool has_valid_stack_frame(struct insn_state *state)
1304 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1305 state->regs[CFI_BP].offset == -16)
1308 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1314 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1316 struct cfi_reg *cfa = &state->cfa;
1317 struct stack_op *op = &insn->stack_op;
1319 if (cfa->base != CFI_SP)
1323 if (op->dest.type == OP_DEST_PUSH)
1327 if (op->src.type == OP_SRC_POP)
1330 /* add immediate to sp */
1331 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1332 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1333 cfa->offset -= op->src.offset;
1338 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1341 if (arch_callee_saved_reg(reg) &&
1342 state->regs[reg].base == CFI_UNDEFINED) {
1343 state->regs[reg].base = base;
1344 state->regs[reg].offset = offset;
1348 static void restore_reg(struct insn_state *state, unsigned char reg)
1350 state->regs[reg].base = CFI_UNDEFINED;
1351 state->regs[reg].offset = 0;
1355 * A note about DRAP stack alignment:
1357 * GCC has the concept of a DRAP register, which is used to help keep track of
1358 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1359 * register. The typical DRAP pattern is:
1361 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1362 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1363 * 41 ff 72 f8 pushq -0x8(%r10)
1365 * 48 89 e5 mov %rsp,%rbp
1372 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1375 * There are some variations in the epilogues, like:
1383 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1388 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1389 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1390 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1391 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1393 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1396 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1397 * restored beforehand:
1400 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1401 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1403 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1407 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1409 struct stack_op *op = &insn->stack_op;
1410 struct cfi_reg *cfa = &state->cfa;
1411 struct cfi_reg *regs = state->regs;
1413 /* stack operations don't make sense with an undefined CFA */
1414 if (cfa->base == CFI_UNDEFINED) {
1416 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1422 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1423 return update_insn_state_regs(insn, state);
1425 switch (op->dest.type) {
1428 switch (op->src.type) {
1431 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1432 cfa->base == CFI_SP &&
1433 regs[CFI_BP].base == CFI_CFA &&
1434 regs[CFI_BP].offset == -cfa->offset) {
1436 /* mov %rsp, %rbp */
1437 cfa->base = op->dest.reg;
1438 state->bp_scratch = false;
1441 else if (op->src.reg == CFI_SP &&
1442 op->dest.reg == CFI_BP && state->drap) {
1444 /* drap: mov %rsp, %rbp */
1445 regs[CFI_BP].base = CFI_BP;
1446 regs[CFI_BP].offset = -state->stack_size;
1447 state->bp_scratch = false;
1450 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1455 * This is needed for the rare case where GCC
1462 state->vals[op->dest.reg].base = CFI_CFA;
1463 state->vals[op->dest.reg].offset = -state->stack_size;
1466 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1467 cfa->base == CFI_BP) {
1472 * Restore the original stack pointer (Clang).
1474 state->stack_size = -state->regs[CFI_BP].offset;
1477 else if (op->dest.reg == cfa->base) {
1479 /* mov %reg, %rsp */
1480 if (cfa->base == CFI_SP &&
1481 state->vals[op->src.reg].base == CFI_CFA) {
1484 * This is needed for the rare case
1485 * where GCC does something dumb like:
1487 * lea 0x8(%rsp), %rcx
1491 cfa->offset = -state->vals[op->src.reg].offset;
1492 state->stack_size = cfa->offset;
1495 cfa->base = CFI_UNDEFINED;
1503 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1506 state->stack_size -= op->src.offset;
1507 if (cfa->base == CFI_SP)
1508 cfa->offset -= op->src.offset;
1512 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1514 /* lea disp(%rbp), %rsp */
1515 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1519 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1521 /* drap: lea disp(%rsp), %drap */
1522 state->drap_reg = op->dest.reg;
1525 * lea disp(%rsp), %reg
1527 * This is needed for the rare case where GCC
1528 * does something dumb like:
1530 * lea 0x8(%rsp), %rcx
1534 state->vals[op->dest.reg].base = CFI_CFA;
1535 state->vals[op->dest.reg].offset = \
1536 -state->stack_size + op->src.offset;
1541 if (state->drap && op->dest.reg == CFI_SP &&
1542 op->src.reg == state->drap_reg) {
1544 /* drap: lea disp(%drap), %rsp */
1546 cfa->offset = state->stack_size = -op->src.offset;
1547 state->drap_reg = CFI_UNDEFINED;
1548 state->drap = false;
1552 if (op->dest.reg == state->cfa.base) {
1553 WARN_FUNC("unsupported stack register modification",
1554 insn->sec, insn->offset);
1561 if (op->dest.reg != CFI_SP ||
1562 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1563 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1564 WARN_FUNC("unsupported stack pointer realignment",
1565 insn->sec, insn->offset);
1569 if (state->drap_reg != CFI_UNDEFINED) {
1570 /* drap: and imm, %rsp */
1571 cfa->base = state->drap_reg;
1572 cfa->offset = state->stack_size = 0;
1577 * Older versions of GCC (4.8ish) realign the stack
1578 * without DRAP, with a frame pointer.
1584 if (!state->drap && op->dest.type == OP_DEST_REG &&
1585 op->dest.reg == cfa->base) {
1591 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1592 op->dest.type == OP_DEST_REG &&
1593 op->dest.reg == state->drap_reg &&
1594 state->drap_offset == -state->stack_size) {
1596 /* drap: pop %drap */
1597 cfa->base = state->drap_reg;
1599 state->drap_offset = -1;
1601 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1604 restore_reg(state, op->dest.reg);
1607 state->stack_size -= 8;
1608 if (cfa->base == CFI_SP)
1613 case OP_SRC_REG_INDIRECT:
1614 if (state->drap && op->src.reg == CFI_BP &&
1615 op->src.offset == state->drap_offset) {
1617 /* drap: mov disp(%rbp), %drap */
1618 cfa->base = state->drap_reg;
1620 state->drap_offset = -1;
1623 if (state->drap && op->src.reg == CFI_BP &&
1624 op->src.offset == regs[op->dest.reg].offset) {
1626 /* drap: mov disp(%rbp), %reg */
1627 restore_reg(state, op->dest.reg);
1629 } else if (op->src.reg == cfa->base &&
1630 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1632 /* mov disp(%rbp), %reg */
1633 /* mov disp(%rsp), %reg */
1634 restore_reg(state, op->dest.reg);
1640 WARN_FUNC("unknown stack-related instruction",
1641 insn->sec, insn->offset);
1648 state->stack_size += 8;
1649 if (cfa->base == CFI_SP)
1652 if (op->src.type != OP_SRC_REG)
1656 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1658 /* drap: push %drap */
1659 cfa->base = CFI_BP_INDIRECT;
1660 cfa->offset = -state->stack_size;
1662 /* save drap so we know when to restore it */
1663 state->drap_offset = -state->stack_size;
1665 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1667 /* drap: push %rbp */
1668 state->stack_size = 0;
1670 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1672 /* drap: push %reg */
1673 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1679 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1682 /* detect when asm code uses rbp as a scratch register */
1683 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1684 cfa->base != CFI_BP)
1685 state->bp_scratch = true;
1688 case OP_DEST_REG_INDIRECT:
1691 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1693 /* drap: mov %drap, disp(%rbp) */
1694 cfa->base = CFI_BP_INDIRECT;
1695 cfa->offset = op->dest.offset;
1697 /* save drap offset so we know when to restore it */
1698 state->drap_offset = op->dest.offset;
1701 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1703 /* drap: mov reg, disp(%rbp) */
1704 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1707 } else if (op->dest.reg == cfa->base) {
1709 /* mov reg, disp(%rbp) */
1710 /* mov reg, disp(%rsp) */
1711 save_reg(state, op->src.reg, CFI_CFA,
1712 op->dest.offset - state->cfa.offset);
1718 if ((!state->drap && cfa->base != CFI_BP) ||
1719 (state->drap && cfa->base != state->drap_reg)) {
1720 WARN_FUNC("leave instruction with modified stack frame",
1721 insn->sec, insn->offset);
1725 /* leave (mov %rbp, %rsp; pop %rbp) */
1727 state->stack_size = -state->regs[CFI_BP].offset - 8;
1728 restore_reg(state, CFI_BP);
1738 if (op->src.type != OP_SRC_POP) {
1739 WARN_FUNC("unknown stack-related memory operation",
1740 insn->sec, insn->offset);
1745 state->stack_size -= 8;
1746 if (cfa->base == CFI_SP)
1752 WARN_FUNC("unknown stack-related instruction",
1753 insn->sec, insn->offset);
1760 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1762 struct insn_state *state1 = &insn->state, *state2 = state;
1765 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1766 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1767 insn->sec, insn->offset,
1768 state1->cfa.base, state1->cfa.offset,
1769 state2->cfa.base, state2->cfa.offset);
1771 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1772 for (i = 0; i < CFI_NUM_REGS; i++) {
1773 if (!memcmp(&state1->regs[i], &state2->regs[i],
1774 sizeof(struct cfi_reg)))
1777 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1778 insn->sec, insn->offset,
1779 i, state1->regs[i].base, state1->regs[i].offset,
1780 i, state2->regs[i].base, state2->regs[i].offset);
1784 } else if (state1->type != state2->type) {
1785 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1786 insn->sec, insn->offset, state1->type, state2->type);
1788 } else if (state1->drap != state2->drap ||
1789 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1790 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1791 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1792 insn->sec, insn->offset,
1793 state1->drap, state1->drap_reg, state1->drap_offset,
1794 state2->drap, state2->drap_reg, state2->drap_offset);
1803 * Follow the branch starting at the given instruction, and recursively follow
1804 * any other branches (jumps). Meanwhile, track the frame pointer state at
1805 * each instruction and validate all the rules described in
1806 * tools/objtool/Documentation/stack-validation.txt.
1808 static int validate_branch(struct objtool_file *file, struct instruction *first,
1809 struct insn_state state)
1811 struct alternative *alt;
1812 struct instruction *insn, *next_insn;
1813 struct section *sec;
1814 struct symbol *func = NULL;
1820 if (insn->alt_group && list_empty(&insn->alts)) {
1821 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1827 next_insn = next_insn_same_sec(file, insn);
1829 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1830 WARN("%s() falls through to next function %s()",
1831 func->name, insn->func->name);
1835 func = insn->func ? insn->func->pfunc : NULL;
1837 if (func && insn->ignore) {
1838 WARN_FUNC("BUG: why am I validating an ignored function?",
1843 if (insn->visited) {
1844 if (!insn->hint && !insn_state_match(insn, &state))
1851 if (insn->restore) {
1852 struct instruction *save_insn, *i;
1856 func_for_each_insn_continue_reverse(file, insn->func, i) {
1864 WARN_FUNC("no corresponding CFI save for CFI restore",
1869 if (!save_insn->visited) {
1871 * Oops, no state to copy yet.
1872 * Hopefully we can reach this
1873 * instruction from another branch
1874 * after the save insn has been
1880 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1885 insn->state = save_insn->state;
1888 state = insn->state;
1891 insn->state = state;
1893 insn->visited = true;
1895 if (!insn->ignore_alts) {
1896 list_for_each_entry(alt, &insn->alts, list) {
1897 ret = validate_branch(file, alt->insn, state);
1903 switch (insn->type) {
1906 if (func && has_modified_stack_frame(&state)) {
1907 WARN_FUNC("return with modified stack frame",
1912 if (state.bp_scratch) {
1913 WARN("%s uses BP as a scratch register",
1921 if (is_fentry_call(insn))
1924 ret = dead_end_function(file, insn->call_dest);
1931 case INSN_CALL_DYNAMIC:
1932 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1933 WARN_FUNC("call without frame pointer save/setup",
1939 case INSN_JUMP_CONDITIONAL:
1940 case INSN_JUMP_UNCONDITIONAL:
1941 if (insn->jump_dest &&
1942 (!func || !insn->jump_dest->func ||
1943 insn->jump_dest->func->pfunc == func)) {
1944 ret = validate_branch(file, insn->jump_dest,
1949 } else if (func && has_modified_stack_frame(&state)) {
1950 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1955 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1960 case INSN_JUMP_DYNAMIC:
1961 if (func && list_empty(&insn->alts) &&
1962 has_modified_stack_frame(&state)) {
1963 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1970 case INSN_CONTEXT_SWITCH:
1971 if (func && (!next_insn || !next_insn->hint)) {
1972 WARN_FUNC("unsupported instruction in callable function",
1979 if (update_insn_state(insn, &state))
1992 if (state.cfa.base == CFI_UNDEFINED)
1994 WARN("%s: unexpected end of section", sec->name);
2004 static int validate_unwind_hints(struct objtool_file *file)
2006 struct instruction *insn;
2007 int ret, warnings = 0;
2008 struct insn_state state;
2013 clear_insn_state(&state);
2015 for_each_insn(file, insn) {
2016 if (insn->hint && !insn->visited) {
2017 ret = validate_branch(file, insn, state);
2025 static int validate_retpoline(struct objtool_file *file)
2027 struct instruction *insn;
2030 for_each_insn(file, insn) {
2031 if (insn->type != INSN_JUMP_DYNAMIC &&
2032 insn->type != INSN_CALL_DYNAMIC)
2035 if (insn->retpoline_safe)
2039 * .init.text code is ran before userspace and thus doesn't
2040 * strictly need retpolines, except for modules which are
2041 * loaded late, they very much do need retpoline in their
2044 if (!strcmp(insn->sec->name, ".init.text") && !module)
2047 WARN_FUNC("indirect %s found in RETPOLINE build",
2048 insn->sec, insn->offset,
2049 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2057 static bool is_kasan_insn(struct instruction *insn)
2059 return (insn->type == INSN_CALL &&
2060 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2063 static bool is_ubsan_insn(struct instruction *insn)
2065 return (insn->type == INSN_CALL &&
2066 !strcmp(insn->call_dest->name,
2067 "__ubsan_handle_builtin_unreachable"));
2070 static bool ignore_unreachable_insn(struct instruction *insn)
2074 if (insn->ignore || insn->type == INSN_NOP)
2078 * Ignore any unused exceptions. This can happen when a whitelisted
2079 * function has an exception table entry.
2081 * Also ignore alternative replacement instructions. This can happen
2082 * when a whitelisted function uses one of the ALTERNATIVE macros.
2084 if (!strcmp(insn->sec->name, ".fixup") ||
2085 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2086 !strcmp(insn->sec->name, ".altinstr_aux"))
2090 * Check if this (or a subsequent) instruction is related to
2091 * CONFIG_UBSAN or CONFIG_KASAN.
2093 * End the search at 5 instructions to avoid going into the weeds.
2097 for (i = 0; i < 5; i++) {
2099 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2102 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2103 if (insn->jump_dest &&
2104 insn->jump_dest->func == insn->func) {
2105 insn = insn->jump_dest;
2112 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2115 insn = list_next_entry(insn, list);
2121 static int validate_functions(struct objtool_file *file)
2123 struct section *sec;
2124 struct symbol *func;
2125 struct instruction *insn;
2126 struct insn_state state;
2127 int ret, warnings = 0;
2129 clear_insn_state(&state);
2131 state.cfa = initial_func_cfi.cfa;
2132 memcpy(&state.regs, &initial_func_cfi.regs,
2133 CFI_NUM_REGS * sizeof(struct cfi_reg));
2134 state.stack_size = initial_func_cfi.cfa.offset;
2136 for_each_sec(file, sec) {
2137 list_for_each_entry(func, &sec->symbol_list, list) {
2138 if (func->type != STT_FUNC || func->pfunc != func)
2141 insn = find_insn(file, sec, func->offset);
2142 if (!insn || insn->ignore)
2145 ret = validate_branch(file, insn, state);
2153 static int validate_reachable_instructions(struct objtool_file *file)
2155 struct instruction *insn;
2157 if (file->ignore_unreachables)
2160 for_each_insn(file, insn) {
2161 if (insn->visited || ignore_unreachable_insn(insn))
2164 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2171 static void cleanup(struct objtool_file *file)
2173 struct instruction *insn, *tmpinsn;
2174 struct alternative *alt, *tmpalt;
2176 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2177 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2178 list_del(&alt->list);
2181 list_del(&insn->list);
2182 hash_del(&insn->hash);
2185 elf_close(file->elf);
2188 static struct objtool_file file;
2190 int check(const char *_objname, bool orc)
2192 int ret, warnings = 0;
2196 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2200 INIT_LIST_HEAD(&file.insn_list);
2201 hash_init(file.insn_hash);
2202 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2203 file.c_file = find_section_by_name(file.elf, ".comment");
2204 file.ignore_unreachables = no_unreachable;
2207 arch_initial_func_cfi_state(&initial_func_cfi);
2209 ret = decode_sections(&file);
2214 if (list_empty(&file.insn_list))
2218 ret = validate_retpoline(&file);
2224 ret = validate_functions(&file);
2229 ret = validate_unwind_hints(&file);
2235 ret = validate_reachable_instructions(&file);
2242 ret = create_orc(&file);
2246 ret = create_orc_sections(&file);
2250 ret = elf_write(file.elf);
2258 /* ignore warnings for now until we get all the code cleaned up */
2259 if (ret || warnings)