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",
170 if (func->bind == STB_WEAK)
173 if (func->bind == STB_GLOBAL)
174 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
175 if (!strcmp(func->name, global_noreturns[i]))
181 insn = find_insn(file, func->sec, func->offset);
185 func_for_each_insn_all(file, func, insn) {
188 if (insn->type == INSN_RETURN)
196 * A function can have a sibling call instead of a return. In that
197 * case, the function's dead-end status depends on whether the target
198 * of the sibling call returns.
200 func_for_each_insn_all(file, func, insn) {
201 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
202 struct instruction *dest = insn->jump_dest;
205 /* sibling call to another file */
208 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
210 /* local sibling call */
211 if (recursion == 5) {
213 * Infinite recursion: two functions
214 * have sibling calls to each other.
215 * This is a very rare case. It means
216 * they aren't dead ends.
221 return __dead_end_function(file, dest->func,
226 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
234 static int dead_end_function(struct objtool_file *file, struct symbol *func)
236 return __dead_end_function(file, func, 0);
239 static void clear_insn_state(struct insn_state *state)
243 memset(state, 0, sizeof(*state));
244 state->cfa.base = CFI_UNDEFINED;
245 for (i = 0; i < CFI_NUM_REGS; i++) {
246 state->regs[i].base = CFI_UNDEFINED;
247 state->vals[i].base = CFI_UNDEFINED;
249 state->drap_reg = CFI_UNDEFINED;
250 state->drap_offset = -1;
254 * Call the arch-specific instruction decoder for all the instructions and add
255 * them to the global instruction list.
257 static int decode_instructions(struct objtool_file *file)
261 unsigned long offset;
262 struct instruction *insn;
265 for_each_sec(file, sec) {
267 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
270 if (strcmp(sec->name, ".altinstr_replacement") &&
271 strcmp(sec->name, ".altinstr_aux") &&
272 strncmp(sec->name, ".discard.", 9))
275 for (offset = 0; offset < sec->len; offset += insn->len) {
276 insn = malloc(sizeof(*insn));
278 WARN("malloc failed");
281 memset(insn, 0, sizeof(*insn));
282 INIT_LIST_HEAD(&insn->alts);
283 clear_insn_state(&insn->state);
286 insn->offset = offset;
288 ret = arch_decode_instruction(file->elf, sec, offset,
290 &insn->len, &insn->type,
296 if (!insn->type || insn->type > INSN_LAST) {
297 WARN_FUNC("invalid instruction type %d",
298 insn->sec, insn->offset, insn->type);
303 hash_add(file->insn_hash, &insn->hash, insn->offset);
304 list_add_tail(&insn->list, &file->insn_list);
307 list_for_each_entry(func, &sec->symbol_list, list) {
308 if (func->type != STT_FUNC)
311 if (!find_insn(file, sec, func->offset)) {
312 WARN("%s(): can't find starting instruction",
317 func_for_each_insn(file, func, insn)
331 * Mark "ud2" instructions and manually annotated dead ends.
333 static int add_dead_ends(struct objtool_file *file)
337 struct instruction *insn;
341 * By default, "ud2" is a dead end unless otherwise annotated, because
342 * GCC 7 inserts it for certain divide-by-zero cases.
344 for_each_insn(file, insn)
345 if (insn->type == INSN_BUG)
346 insn->dead_end = true;
349 * Check for manually annotated dead ends.
351 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
355 list_for_each_entry(rela, &sec->rela_list, list) {
356 if (rela->sym->type != STT_SECTION) {
357 WARN("unexpected relocation symbol type in %s", sec->name);
360 insn = find_insn(file, rela->sym->sec, rela->addend);
362 insn = list_prev_entry(insn, list);
363 else if (rela->addend == rela->sym->sec->len) {
365 list_for_each_entry_reverse(insn, &file->insn_list, list) {
366 if (insn->sec == rela->sym->sec) {
373 WARN("can't find unreachable insn at %s+0x%x",
374 rela->sym->sec->name, rela->addend);
378 WARN("can't find unreachable insn at %s+0x%x",
379 rela->sym->sec->name, rela->addend);
383 insn->dead_end = true;
388 * These manually annotated reachable checks are needed for GCC 4.4,
389 * where the Linux unreachable() macro isn't supported. In that case
390 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
393 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
397 list_for_each_entry(rela, &sec->rela_list, list) {
398 if (rela->sym->type != STT_SECTION) {
399 WARN("unexpected relocation symbol type in %s", sec->name);
402 insn = find_insn(file, rela->sym->sec, rela->addend);
404 insn = list_prev_entry(insn, list);
405 else if (rela->addend == rela->sym->sec->len) {
407 list_for_each_entry_reverse(insn, &file->insn_list, list) {
408 if (insn->sec == rela->sym->sec) {
415 WARN("can't find reachable insn at %s+0x%x",
416 rela->sym->sec->name, rela->addend);
420 WARN("can't find reachable insn at %s+0x%x",
421 rela->sym->sec->name, rela->addend);
425 insn->dead_end = false;
432 * Warnings shouldn't be reported for ignored functions.
434 static void add_ignores(struct objtool_file *file)
436 struct instruction *insn;
440 for_each_sec(file, sec) {
441 list_for_each_entry(func, &sec->symbol_list, list) {
442 if (func->type != STT_FUNC)
445 if (!ignore_func(file, func))
448 func_for_each_insn_all(file, func, insn)
455 * FIXME: For now, just ignore any alternatives which add retpolines. This is
456 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
457 * But it at least allows objtool to understand the control flow *around* the
460 static int add_nospec_ignores(struct objtool_file *file)
464 struct instruction *insn;
466 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
470 list_for_each_entry(rela, &sec->rela_list, list) {
471 if (rela->sym->type != STT_SECTION) {
472 WARN("unexpected relocation symbol type in %s", sec->name);
476 insn = find_insn(file, rela->sym->sec, rela->addend);
478 WARN("bad .discard.nospec entry");
482 insn->ignore_alts = true;
489 * Find the destination instructions for all jumps.
491 static int add_jump_destinations(struct objtool_file *file)
493 struct instruction *insn;
495 struct section *dest_sec;
496 unsigned long dest_off;
498 for_each_insn(file, insn) {
499 if (insn->type != INSN_JUMP_CONDITIONAL &&
500 insn->type != INSN_JUMP_UNCONDITIONAL)
506 rela = find_rela_by_dest_range(insn->sec, insn->offset,
509 dest_sec = insn->sec;
510 dest_off = insn->offset + insn->len + insn->immediate;
511 } else if (rela->sym->type == STT_SECTION) {
512 dest_sec = rela->sym->sec;
513 dest_off = rela->addend + 4;
514 } else if (rela->sym->sec->idx) {
515 dest_sec = rela->sym->sec;
516 dest_off = rela->sym->sym.st_value + rela->addend + 4;
517 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
519 * Retpoline jumps are really dynamic jumps in
520 * disguise, so convert them accordingly.
522 insn->type = INSN_JUMP_DYNAMIC;
523 insn->retpoline_safe = true;
531 insn->jump_dest = find_insn(file, dest_sec, dest_off);
532 if (!insn->jump_dest) {
535 * This is a special case where an alt instruction
536 * jumps past the end of the section. These are
537 * handled later in handle_group_alt().
539 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
542 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
543 insn->sec, insn->offset, dest_sec->name,
549 * For GCC 8+, create parent/child links for any cold
550 * subfunctions. This is _mostly_ redundant with a similar
551 * initialization in read_symbols().
553 * If a function has aliases, we want the *first* such function
554 * in the symbol table to be the subfunction's parent. In that
555 * case we overwrite the initialization done in read_symbols().
557 * However this code can't completely replace the
558 * read_symbols() code because this doesn't detect the case
559 * where the parent function's only reference to a subfunction
560 * is through a switch table.
562 if (insn->func && insn->jump_dest->func &&
563 insn->func != insn->jump_dest->func &&
564 !strstr(insn->func->name, ".cold.") &&
565 strstr(insn->jump_dest->func->name, ".cold.")) {
566 insn->func->cfunc = insn->jump_dest->func;
567 insn->jump_dest->func->pfunc = insn->func;
575 * Find the destination instructions for all calls.
577 static int add_call_destinations(struct objtool_file *file)
579 struct instruction *insn;
580 unsigned long dest_off;
583 for_each_insn(file, insn) {
584 if (insn->type != INSN_CALL)
587 rela = find_rela_by_dest_range(insn->sec, insn->offset,
590 dest_off = insn->offset + insn->len + insn->immediate;
591 insn->call_dest = find_symbol_by_offset(insn->sec,
594 if (!insn->call_dest && !insn->ignore) {
595 WARN_FUNC("unsupported intra-function call",
596 insn->sec, insn->offset);
598 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
602 } else if (rela->sym->type == STT_SECTION) {
603 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
605 if (!insn->call_dest ||
606 insn->call_dest->type != STT_FUNC) {
607 WARN_FUNC("can't find call dest symbol at %s+0x%x",
608 insn->sec, insn->offset,
609 rela->sym->sec->name,
614 insn->call_dest = rela->sym;
621 * The .alternatives section requires some extra special care, over and above
622 * what other special sections require:
624 * 1. Because alternatives are patched in-place, we need to insert a fake jump
625 * instruction at the end so that validate_branch() skips all the original
626 * replaced instructions when validating the new instruction path.
628 * 2. An added wrinkle is that the new instruction length might be zero. In
629 * that case the old instructions are replaced with noops. We simulate that
630 * by creating a fake jump as the only new instruction.
632 * 3. In some cases, the alternative section includes an instruction which
633 * conditionally jumps to the _end_ of the entry. We have to modify these
634 * jumps' destinations to point back to .text rather than the end of the
635 * entry in .altinstr_replacement.
637 * 4. It has been requested that we don't validate the !POPCNT feature path
638 * which is a "very very small percentage of machines".
640 static int handle_group_alt(struct objtool_file *file,
641 struct special_alt *special_alt,
642 struct instruction *orig_insn,
643 struct instruction **new_insn)
645 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
646 unsigned long dest_off;
648 last_orig_insn = NULL;
650 sec_for_each_insn_from(file, insn) {
651 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
654 if (special_alt->skip_orig)
655 insn->type = INSN_NOP;
657 insn->alt_group = true;
658 last_orig_insn = insn;
661 if (next_insn_same_sec(file, last_orig_insn)) {
662 fake_jump = malloc(sizeof(*fake_jump));
664 WARN("malloc failed");
667 memset(fake_jump, 0, sizeof(*fake_jump));
668 INIT_LIST_HEAD(&fake_jump->alts);
669 clear_insn_state(&fake_jump->state);
671 fake_jump->sec = special_alt->new_sec;
672 fake_jump->offset = -1;
673 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
674 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
675 fake_jump->ignore = true;
678 if (!special_alt->new_len) {
680 WARN("%s: empty alternative at end of section",
681 special_alt->orig_sec->name);
685 *new_insn = fake_jump;
689 last_new_insn = NULL;
691 sec_for_each_insn_from(file, insn) {
692 if (insn->offset >= special_alt->new_off + special_alt->new_len)
695 last_new_insn = insn;
697 insn->ignore = orig_insn->ignore_alts;
699 if (insn->type != INSN_JUMP_CONDITIONAL &&
700 insn->type != INSN_JUMP_UNCONDITIONAL)
703 if (!insn->immediate)
706 dest_off = insn->offset + insn->len + insn->immediate;
707 if (dest_off == special_alt->new_off + special_alt->new_len) {
709 WARN("%s: alternative jump to end of section",
710 special_alt->orig_sec->name);
713 insn->jump_dest = fake_jump;
716 if (!insn->jump_dest) {
717 WARN_FUNC("can't find alternative jump destination",
718 insn->sec, insn->offset);
723 if (!last_new_insn) {
724 WARN_FUNC("can't find last new alternative instruction",
725 special_alt->new_sec, special_alt->new_off);
730 list_add(&fake_jump->list, &last_new_insn->list);
736 * A jump table entry can either convert a nop to a jump or a jump to a nop.
737 * If the original instruction is a jump, make the alt entry an effective nop
738 * by just skipping the original instruction.
740 static int handle_jump_alt(struct objtool_file *file,
741 struct special_alt *special_alt,
742 struct instruction *orig_insn,
743 struct instruction **new_insn)
745 if (orig_insn->type == INSN_NOP)
748 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
749 WARN_FUNC("unsupported instruction at jump label",
750 orig_insn->sec, orig_insn->offset);
754 *new_insn = list_next_entry(orig_insn, list);
759 * Read all the special sections which have alternate instructions which can be
760 * patched in or redirected to at runtime. Each instruction having alternate
761 * instruction(s) has them added to its insn->alts list, which will be
762 * traversed in validate_branch().
764 static int add_special_section_alts(struct objtool_file *file)
766 struct list_head special_alts;
767 struct instruction *orig_insn, *new_insn;
768 struct special_alt *special_alt, *tmp;
769 struct alternative *alt;
772 ret = special_get_alts(file->elf, &special_alts);
776 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
778 orig_insn = find_insn(file, special_alt->orig_sec,
779 special_alt->orig_off);
781 WARN_FUNC("special: can't find orig instruction",
782 special_alt->orig_sec, special_alt->orig_off);
788 if (!special_alt->group || special_alt->new_len) {
789 new_insn = find_insn(file, special_alt->new_sec,
790 special_alt->new_off);
792 WARN_FUNC("special: can't find new instruction",
793 special_alt->new_sec,
794 special_alt->new_off);
800 if (special_alt->group) {
801 ret = handle_group_alt(file, special_alt, orig_insn,
805 } else if (special_alt->jump_or_nop) {
806 ret = handle_jump_alt(file, special_alt, orig_insn,
812 alt = malloc(sizeof(*alt));
814 WARN("malloc failed");
819 alt->insn = new_insn;
820 list_add_tail(&alt->list, &orig_insn->alts);
822 list_del(&special_alt->list);
830 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
831 struct rela *table, struct rela *next_table)
833 struct rela *rela = table;
834 struct instruction *alt_insn;
835 struct alternative *alt;
836 struct symbol *pfunc = insn->func->pfunc;
837 unsigned int prev_offset = 0;
839 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
840 if (rela == next_table)
843 /* Make sure the switch table entries are consecutive: */
844 if (prev_offset && rela->offset != prev_offset + 8)
847 /* Detect function pointers from contiguous objects: */
848 if (rela->sym->sec == pfunc->sec &&
849 rela->addend == pfunc->offset)
852 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
856 /* Make sure the jmp dest is in the function or subfunction: */
857 if (alt_insn->func->pfunc != pfunc)
860 alt = malloc(sizeof(*alt));
862 WARN("malloc failed");
866 alt->insn = alt_insn;
867 list_add_tail(&alt->list, &insn->alts);
868 prev_offset = rela->offset;
872 WARN_FUNC("can't find switch jump table",
873 insn->sec, insn->offset);
881 * find_switch_table() - Given a dynamic jump, find the switch jump table in
882 * .rodata associated with it.
884 * There are 3 basic patterns:
886 * 1. jmpq *[rodata addr](,%reg,8)
888 * This is the most common case by far. It jumps to an address in a simple
889 * jump table which is stored in .rodata.
891 * 2. jmpq *[rodata addr](%rip)
893 * This is caused by a rare GCC quirk, currently only seen in three driver
894 * functions in the kernel, only with certain obscure non-distro configs.
896 * As part of an optimization, GCC makes a copy of an existing switch jump
897 * table, modifies it, and then hard-codes the jump (albeit with an indirect
898 * jump) to use a single entry in the table. The rest of the jump table and
899 * some of its jump targets remain as dead code.
901 * In such a case we can just crudely ignore all unreachable instruction
902 * warnings for the entire object file. Ideally we would just ignore them
903 * for the function, but that would require redesigning the code quite a
904 * bit. And honestly that's just not worth doing: unreachable instruction
905 * warnings are of questionable value anyway, and this is such a rare issue.
907 * 3. mov [rodata addr],%reg1
908 * ... some instructions ...
909 * jmpq *(%reg1,%reg2,8)
911 * This is a fairly uncommon pattern which is new for GCC 6. As of this
912 * writing, there are 11 occurrences of it in the allmodconfig kernel.
914 * As of GCC 7 there are quite a few more of these and the 'in between' code
915 * is significant. Esp. with KASAN enabled some of the code between the mov
916 * and jmpq uses .rodata itself, which can confuse things.
918 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
919 * ensure the same register is used in the mov and jump instructions.
921 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
923 static struct rela *find_switch_table(struct objtool_file *file,
925 struct instruction *insn)
927 struct rela *text_rela, *rodata_rela;
928 struct instruction *orig_insn = insn;
929 struct section *rodata_sec;
930 unsigned long table_offset;
933 * Backward search using the @first_jump_src links, these help avoid
934 * much of the 'in between' code. Which avoids us getting confused by
938 &insn->list != &file->insn_list &&
939 insn->sec == func->sec &&
940 insn->offset >= func->offset;
942 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
944 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
947 /* allow small jumps within the range */
948 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
950 (insn->jump_dest->offset <= insn->offset ||
951 insn->jump_dest->offset > orig_insn->offset))
954 /* look for a relocation which references .rodata */
955 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
957 if (!text_rela || text_rela->sym->type != STT_SECTION ||
958 !text_rela->sym->sec->rodata)
961 table_offset = text_rela->addend;
962 rodata_sec = text_rela->sym->sec;
964 if (text_rela->type == R_X86_64_PC32)
968 * Make sure the .rodata address isn't associated with a
969 * symbol. gcc jump tables are anonymous data.
971 if (find_symbol_containing(rodata_sec, table_offset))
974 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
977 * Use of RIP-relative switch jumps is quite rare, and
978 * indicates a rare GCC quirk/bug which can leave dead
981 if (text_rela->type == R_X86_64_PC32)
982 file->ignore_unreachables = true;
992 static int add_func_switch_tables(struct objtool_file *file,
995 struct instruction *insn, *last = NULL, *prev_jump = NULL;
996 struct rela *rela, *prev_rela = NULL;
999 func_for_each_insn_all(file, func, insn) {
1004 * Store back-pointers for unconditional forward jumps such
1005 * that find_switch_table() can back-track using those and
1006 * avoid some potentially confusing code.
1008 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1009 insn->offset > last->offset &&
1010 insn->jump_dest->offset > insn->offset &&
1011 !insn->jump_dest->first_jump_src) {
1013 insn->jump_dest->first_jump_src = insn;
1014 last = insn->jump_dest;
1017 if (insn->type != INSN_JUMP_DYNAMIC)
1020 rela = find_switch_table(file, func, insn);
1025 * We found a switch table, but we don't know yet how big it
1026 * is. Don't add it until we reach the end of the function or
1027 * the beginning of another switch table in the same function.
1030 ret = add_switch_table(file, prev_jump, prev_rela, rela);
1040 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1049 * For some switch statements, gcc generates a jump table in the .rodata
1050 * section which contains a list of addresses within the function to jump to.
1051 * This finds these jump tables and adds them to the insn->alts lists.
1053 static int add_switch_table_alts(struct objtool_file *file)
1055 struct section *sec;
1056 struct symbol *func;
1062 for_each_sec(file, sec) {
1063 list_for_each_entry(func, &sec->symbol_list, list) {
1064 if (func->type != STT_FUNC)
1067 ret = add_func_switch_tables(file, func);
1076 static int read_unwind_hints(struct objtool_file *file)
1078 struct section *sec, *relasec;
1080 struct unwind_hint *hint;
1081 struct instruction *insn;
1082 struct cfi_reg *cfa;
1085 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1089 relasec = sec->rela;
1091 WARN("missing .rela.discard.unwind_hints section");
1095 if (sec->len % sizeof(struct unwind_hint)) {
1096 WARN("struct unwind_hint size mismatch");
1102 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1103 hint = (struct unwind_hint *)sec->data->d_buf + i;
1105 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1107 WARN("can't find rela for unwind_hints[%d]", i);
1111 insn = find_insn(file, rela->sym->sec, rela->addend);
1113 WARN("can't find insn for unwind_hints[%d]", i);
1117 cfa = &insn->state.cfa;
1119 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1123 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1124 insn->restore = true;
1131 switch (hint->sp_reg) {
1132 case ORC_REG_UNDEFINED:
1133 cfa->base = CFI_UNDEFINED;
1141 case ORC_REG_SP_INDIRECT:
1142 cfa->base = CFI_SP_INDIRECT;
1145 cfa->base = CFI_R10;
1148 cfa->base = CFI_R13;
1157 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1158 insn->sec, insn->offset, hint->sp_reg);
1162 cfa->offset = hint->sp_offset;
1163 insn->state.type = hint->type;
1164 insn->state.end = hint->end;
1170 static int read_retpoline_hints(struct objtool_file *file)
1172 struct section *sec;
1173 struct instruction *insn;
1176 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1180 list_for_each_entry(rela, &sec->rela_list, list) {
1181 if (rela->sym->type != STT_SECTION) {
1182 WARN("unexpected relocation symbol type in %s", sec->name);
1186 insn = find_insn(file, rela->sym->sec, rela->addend);
1188 WARN("bad .discard.retpoline_safe entry");
1192 if (insn->type != INSN_JUMP_DYNAMIC &&
1193 insn->type != INSN_CALL_DYNAMIC) {
1194 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1195 insn->sec, insn->offset);
1199 insn->retpoline_safe = true;
1205 static void mark_rodata(struct objtool_file *file)
1207 struct section *sec;
1211 * This searches for the .rodata section or multiple .rodata.func_name
1212 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1213 * rodata sections are ignored as they don't contain jump tables.
1215 for_each_sec(file, sec) {
1216 if (!strncmp(sec->name, ".rodata", 7) &&
1217 !strstr(sec->name, ".str1.")) {
1223 file->rodata = found;
1226 static int decode_sections(struct objtool_file *file)
1232 ret = decode_instructions(file);
1236 ret = add_dead_ends(file);
1242 ret = add_nospec_ignores(file);
1246 ret = add_jump_destinations(file);
1250 ret = add_special_section_alts(file);
1254 ret = add_call_destinations(file);
1258 ret = add_switch_table_alts(file);
1262 ret = read_unwind_hints(file);
1266 ret = read_retpoline_hints(file);
1273 static bool is_fentry_call(struct instruction *insn)
1275 if (insn->type == INSN_CALL &&
1276 insn->call_dest->type == STT_NOTYPE &&
1277 !strcmp(insn->call_dest->name, "__fentry__"))
1283 static bool has_modified_stack_frame(struct insn_state *state)
1287 if (state->cfa.base != initial_func_cfi.cfa.base ||
1288 state->cfa.offset != initial_func_cfi.cfa.offset ||
1289 state->stack_size != initial_func_cfi.cfa.offset ||
1293 for (i = 0; i < CFI_NUM_REGS; i++)
1294 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1295 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1301 static bool has_valid_stack_frame(struct insn_state *state)
1303 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1304 state->regs[CFI_BP].offset == -16)
1307 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1313 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1315 struct cfi_reg *cfa = &state->cfa;
1316 struct stack_op *op = &insn->stack_op;
1318 if (cfa->base != CFI_SP)
1322 if (op->dest.type == OP_DEST_PUSH)
1326 if (op->src.type == OP_SRC_POP)
1329 /* add immediate to sp */
1330 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1331 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1332 cfa->offset -= op->src.offset;
1337 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1340 if (arch_callee_saved_reg(reg) &&
1341 state->regs[reg].base == CFI_UNDEFINED) {
1342 state->regs[reg].base = base;
1343 state->regs[reg].offset = offset;
1347 static void restore_reg(struct insn_state *state, unsigned char reg)
1349 state->regs[reg].base = CFI_UNDEFINED;
1350 state->regs[reg].offset = 0;
1354 * A note about DRAP stack alignment:
1356 * GCC has the concept of a DRAP register, which is used to help keep track of
1357 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1358 * register. The typical DRAP pattern is:
1360 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1361 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1362 * 41 ff 72 f8 pushq -0x8(%r10)
1364 * 48 89 e5 mov %rsp,%rbp
1371 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1374 * There are some variations in the epilogues, like:
1382 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1387 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1388 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1389 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1390 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1392 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1395 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1396 * restored beforehand:
1399 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1400 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1402 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1406 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1408 struct stack_op *op = &insn->stack_op;
1409 struct cfi_reg *cfa = &state->cfa;
1410 struct cfi_reg *regs = state->regs;
1412 /* stack operations don't make sense with an undefined CFA */
1413 if (cfa->base == CFI_UNDEFINED) {
1415 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1421 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1422 return update_insn_state_regs(insn, state);
1424 switch (op->dest.type) {
1427 switch (op->src.type) {
1430 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1431 cfa->base == CFI_SP &&
1432 regs[CFI_BP].base == CFI_CFA &&
1433 regs[CFI_BP].offset == -cfa->offset) {
1435 /* mov %rsp, %rbp */
1436 cfa->base = op->dest.reg;
1437 state->bp_scratch = false;
1440 else if (op->src.reg == CFI_SP &&
1441 op->dest.reg == CFI_BP && state->drap) {
1443 /* drap: mov %rsp, %rbp */
1444 regs[CFI_BP].base = CFI_BP;
1445 regs[CFI_BP].offset = -state->stack_size;
1446 state->bp_scratch = false;
1449 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1454 * This is needed for the rare case where GCC
1461 state->vals[op->dest.reg].base = CFI_CFA;
1462 state->vals[op->dest.reg].offset = -state->stack_size;
1465 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1466 cfa->base == CFI_BP) {
1471 * Restore the original stack pointer (Clang).
1473 state->stack_size = -state->regs[CFI_BP].offset;
1476 else if (op->dest.reg == cfa->base) {
1478 /* mov %reg, %rsp */
1479 if (cfa->base == CFI_SP &&
1480 state->vals[op->src.reg].base == CFI_CFA) {
1483 * This is needed for the rare case
1484 * where GCC does something dumb like:
1486 * lea 0x8(%rsp), %rcx
1490 cfa->offset = -state->vals[op->src.reg].offset;
1491 state->stack_size = cfa->offset;
1494 cfa->base = CFI_UNDEFINED;
1502 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1505 state->stack_size -= op->src.offset;
1506 if (cfa->base == CFI_SP)
1507 cfa->offset -= op->src.offset;
1511 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1513 /* lea disp(%rbp), %rsp */
1514 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1518 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1520 /* drap: lea disp(%rsp), %drap */
1521 state->drap_reg = op->dest.reg;
1524 * lea disp(%rsp), %reg
1526 * This is needed for the rare case where GCC
1527 * does something dumb like:
1529 * lea 0x8(%rsp), %rcx
1533 state->vals[op->dest.reg].base = CFI_CFA;
1534 state->vals[op->dest.reg].offset = \
1535 -state->stack_size + op->src.offset;
1540 if (state->drap && op->dest.reg == CFI_SP &&
1541 op->src.reg == state->drap_reg) {
1543 /* drap: lea disp(%drap), %rsp */
1545 cfa->offset = state->stack_size = -op->src.offset;
1546 state->drap_reg = CFI_UNDEFINED;
1547 state->drap = false;
1551 if (op->dest.reg == state->cfa.base) {
1552 WARN_FUNC("unsupported stack register modification",
1553 insn->sec, insn->offset);
1560 if (op->dest.reg != CFI_SP ||
1561 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1562 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1563 WARN_FUNC("unsupported stack pointer realignment",
1564 insn->sec, insn->offset);
1568 if (state->drap_reg != CFI_UNDEFINED) {
1569 /* drap: and imm, %rsp */
1570 cfa->base = state->drap_reg;
1571 cfa->offset = state->stack_size = 0;
1576 * Older versions of GCC (4.8ish) realign the stack
1577 * without DRAP, with a frame pointer.
1583 if (!state->drap && op->dest.type == OP_DEST_REG &&
1584 op->dest.reg == cfa->base) {
1590 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1591 op->dest.type == OP_DEST_REG &&
1592 op->dest.reg == state->drap_reg &&
1593 state->drap_offset == -state->stack_size) {
1595 /* drap: pop %drap */
1596 cfa->base = state->drap_reg;
1598 state->drap_offset = -1;
1600 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1603 restore_reg(state, op->dest.reg);
1606 state->stack_size -= 8;
1607 if (cfa->base == CFI_SP)
1612 case OP_SRC_REG_INDIRECT:
1613 if (state->drap && op->src.reg == CFI_BP &&
1614 op->src.offset == state->drap_offset) {
1616 /* drap: mov disp(%rbp), %drap */
1617 cfa->base = state->drap_reg;
1619 state->drap_offset = -1;
1622 if (state->drap && op->src.reg == CFI_BP &&
1623 op->src.offset == regs[op->dest.reg].offset) {
1625 /* drap: mov disp(%rbp), %reg */
1626 restore_reg(state, op->dest.reg);
1628 } else if (op->src.reg == cfa->base &&
1629 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1631 /* mov disp(%rbp), %reg */
1632 /* mov disp(%rsp), %reg */
1633 restore_reg(state, op->dest.reg);
1639 WARN_FUNC("unknown stack-related instruction",
1640 insn->sec, insn->offset);
1647 state->stack_size += 8;
1648 if (cfa->base == CFI_SP)
1651 if (op->src.type != OP_SRC_REG)
1655 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1657 /* drap: push %drap */
1658 cfa->base = CFI_BP_INDIRECT;
1659 cfa->offset = -state->stack_size;
1661 /* save drap so we know when to restore it */
1662 state->drap_offset = -state->stack_size;
1664 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1666 /* drap: push %rbp */
1667 state->stack_size = 0;
1669 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1671 /* drap: push %reg */
1672 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1678 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1681 /* detect when asm code uses rbp as a scratch register */
1682 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1683 cfa->base != CFI_BP)
1684 state->bp_scratch = true;
1687 case OP_DEST_REG_INDIRECT:
1690 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1692 /* drap: mov %drap, disp(%rbp) */
1693 cfa->base = CFI_BP_INDIRECT;
1694 cfa->offset = op->dest.offset;
1696 /* save drap offset so we know when to restore it */
1697 state->drap_offset = op->dest.offset;
1700 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1702 /* drap: mov reg, disp(%rbp) */
1703 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1706 } else if (op->dest.reg == cfa->base) {
1708 /* mov reg, disp(%rbp) */
1709 /* mov reg, disp(%rsp) */
1710 save_reg(state, op->src.reg, CFI_CFA,
1711 op->dest.offset - state->cfa.offset);
1717 if ((!state->drap && cfa->base != CFI_BP) ||
1718 (state->drap && cfa->base != state->drap_reg)) {
1719 WARN_FUNC("leave instruction with modified stack frame",
1720 insn->sec, insn->offset);
1724 /* leave (mov %rbp, %rsp; pop %rbp) */
1726 state->stack_size = -state->regs[CFI_BP].offset - 8;
1727 restore_reg(state, CFI_BP);
1737 if (op->src.type != OP_SRC_POP) {
1738 WARN_FUNC("unknown stack-related memory operation",
1739 insn->sec, insn->offset);
1744 state->stack_size -= 8;
1745 if (cfa->base == CFI_SP)
1751 WARN_FUNC("unknown stack-related instruction",
1752 insn->sec, insn->offset);
1759 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1761 struct insn_state *state1 = &insn->state, *state2 = state;
1764 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1765 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1766 insn->sec, insn->offset,
1767 state1->cfa.base, state1->cfa.offset,
1768 state2->cfa.base, state2->cfa.offset);
1770 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1771 for (i = 0; i < CFI_NUM_REGS; i++) {
1772 if (!memcmp(&state1->regs[i], &state2->regs[i],
1773 sizeof(struct cfi_reg)))
1776 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1777 insn->sec, insn->offset,
1778 i, state1->regs[i].base, state1->regs[i].offset,
1779 i, state2->regs[i].base, state2->regs[i].offset);
1783 } else if (state1->type != state2->type) {
1784 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1785 insn->sec, insn->offset, state1->type, state2->type);
1787 } else if (state1->drap != state2->drap ||
1788 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1789 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1790 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1791 insn->sec, insn->offset,
1792 state1->drap, state1->drap_reg, state1->drap_offset,
1793 state2->drap, state2->drap_reg, state2->drap_offset);
1802 * Follow the branch starting at the given instruction, and recursively follow
1803 * any other branches (jumps). Meanwhile, track the frame pointer state at
1804 * each instruction and validate all the rules described in
1805 * tools/objtool/Documentation/stack-validation.txt.
1807 static int validate_branch(struct objtool_file *file, struct instruction *first,
1808 struct insn_state state)
1810 struct alternative *alt;
1811 struct instruction *insn, *next_insn;
1812 struct section *sec;
1813 struct symbol *func = NULL;
1819 if (insn->alt_group && list_empty(&insn->alts)) {
1820 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1826 next_insn = next_insn_same_sec(file, insn);
1828 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1829 WARN("%s() falls through to next function %s()",
1830 func->name, insn->func->name);
1834 func = insn->func ? insn->func->pfunc : NULL;
1836 if (func && insn->ignore) {
1837 WARN_FUNC("BUG: why am I validating an ignored function?",
1842 if (insn->visited) {
1843 if (!insn->hint && !insn_state_match(insn, &state))
1850 if (insn->restore) {
1851 struct instruction *save_insn, *i;
1855 func_for_each_insn_continue_reverse(file, insn->func, i) {
1863 WARN_FUNC("no corresponding CFI save for CFI restore",
1868 if (!save_insn->visited) {
1870 * Oops, no state to copy yet.
1871 * Hopefully we can reach this
1872 * instruction from another branch
1873 * after the save insn has been
1879 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1884 insn->state = save_insn->state;
1887 state = insn->state;
1890 insn->state = state;
1892 insn->visited = true;
1894 if (!insn->ignore_alts) {
1895 list_for_each_entry(alt, &insn->alts, list) {
1896 ret = validate_branch(file, alt->insn, state);
1902 switch (insn->type) {
1905 if (func && has_modified_stack_frame(&state)) {
1906 WARN_FUNC("return with modified stack frame",
1911 if (state.bp_scratch) {
1912 WARN("%s uses BP as a scratch register",
1920 if (is_fentry_call(insn))
1923 ret = dead_end_function(file, insn->call_dest);
1930 case INSN_CALL_DYNAMIC:
1931 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1932 WARN_FUNC("call without frame pointer save/setup",
1938 case INSN_JUMP_CONDITIONAL:
1939 case INSN_JUMP_UNCONDITIONAL:
1940 if (insn->jump_dest &&
1941 (!func || !insn->jump_dest->func ||
1942 insn->jump_dest->func->pfunc == func)) {
1943 ret = validate_branch(file, insn->jump_dest,
1948 } else if (func && has_modified_stack_frame(&state)) {
1949 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1954 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1959 case INSN_JUMP_DYNAMIC:
1960 if (func && list_empty(&insn->alts) &&
1961 has_modified_stack_frame(&state)) {
1962 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1969 case INSN_CONTEXT_SWITCH:
1970 if (func && (!next_insn || !next_insn->hint)) {
1971 WARN_FUNC("unsupported instruction in callable function",
1978 if (update_insn_state(insn, &state))
1991 if (state.cfa.base == CFI_UNDEFINED)
1993 WARN("%s: unexpected end of section", sec->name);
2003 static int validate_unwind_hints(struct objtool_file *file)
2005 struct instruction *insn;
2006 int ret, warnings = 0;
2007 struct insn_state state;
2012 clear_insn_state(&state);
2014 for_each_insn(file, insn) {
2015 if (insn->hint && !insn->visited) {
2016 ret = validate_branch(file, insn, state);
2024 static int validate_retpoline(struct objtool_file *file)
2026 struct instruction *insn;
2029 for_each_insn(file, insn) {
2030 if (insn->type != INSN_JUMP_DYNAMIC &&
2031 insn->type != INSN_CALL_DYNAMIC)
2034 if (insn->retpoline_safe)
2038 * .init.text code is ran before userspace and thus doesn't
2039 * strictly need retpolines, except for modules which are
2040 * loaded late, they very much do need retpoline in their
2043 if (!strcmp(insn->sec->name, ".init.text") && !module)
2046 WARN_FUNC("indirect %s found in RETPOLINE build",
2047 insn->sec, insn->offset,
2048 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2056 static bool is_kasan_insn(struct instruction *insn)
2058 return (insn->type == INSN_CALL &&
2059 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2062 static bool is_ubsan_insn(struct instruction *insn)
2064 return (insn->type == INSN_CALL &&
2065 !strcmp(insn->call_dest->name,
2066 "__ubsan_handle_builtin_unreachable"));
2069 static bool ignore_unreachable_insn(struct instruction *insn)
2073 if (insn->ignore || insn->type == INSN_NOP)
2077 * Ignore any unused exceptions. This can happen when a whitelisted
2078 * function has an exception table entry.
2080 * Also ignore alternative replacement instructions. This can happen
2081 * when a whitelisted function uses one of the ALTERNATIVE macros.
2083 if (!strcmp(insn->sec->name, ".fixup") ||
2084 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2085 !strcmp(insn->sec->name, ".altinstr_aux"))
2089 * Check if this (or a subsequent) instruction is related to
2090 * CONFIG_UBSAN or CONFIG_KASAN.
2092 * End the search at 5 instructions to avoid going into the weeds.
2096 for (i = 0; i < 5; i++) {
2098 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2101 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2102 if (insn->jump_dest &&
2103 insn->jump_dest->func == insn->func) {
2104 insn = insn->jump_dest;
2111 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2114 insn = list_next_entry(insn, list);
2120 static int validate_functions(struct objtool_file *file)
2122 struct section *sec;
2123 struct symbol *func;
2124 struct instruction *insn;
2125 struct insn_state state;
2126 int ret, warnings = 0;
2128 clear_insn_state(&state);
2130 state.cfa = initial_func_cfi.cfa;
2131 memcpy(&state.regs, &initial_func_cfi.regs,
2132 CFI_NUM_REGS * sizeof(struct cfi_reg));
2133 state.stack_size = initial_func_cfi.cfa.offset;
2135 for_each_sec(file, sec) {
2136 list_for_each_entry(func, &sec->symbol_list, list) {
2137 if (func->type != STT_FUNC || func->pfunc != func)
2140 insn = find_insn(file, sec, func->offset);
2141 if (!insn || insn->ignore)
2144 ret = validate_branch(file, insn, state);
2152 static int validate_reachable_instructions(struct objtool_file *file)
2154 struct instruction *insn;
2156 if (file->ignore_unreachables)
2159 for_each_insn(file, insn) {
2160 if (insn->visited || ignore_unreachable_insn(insn))
2163 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2170 static void cleanup(struct objtool_file *file)
2172 struct instruction *insn, *tmpinsn;
2173 struct alternative *alt, *tmpalt;
2175 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2176 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2177 list_del(&alt->list);
2180 list_del(&insn->list);
2181 hash_del(&insn->hash);
2184 elf_close(file->elf);
2187 int check(const char *_objname, bool orc)
2189 struct objtool_file file;
2190 int ret, warnings = 0;
2194 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2198 INIT_LIST_HEAD(&file.insn_list);
2199 hash_init(file.insn_hash);
2200 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2201 file.c_file = find_section_by_name(file.elf, ".comment");
2202 file.ignore_unreachables = no_unreachable;
2205 arch_initial_func_cfi_state(&initial_func_cfi);
2207 ret = decode_sections(&file);
2212 if (list_empty(&file.insn_list))
2216 ret = validate_retpoline(&file);
2222 ret = validate_functions(&file);
2227 ret = validate_unwind_hints(&file);
2233 ret = validate_reachable_instructions(&file);
2240 ret = create_orc(&file);
2244 ret = create_orc_sections(&file);
2248 ret = elf_write(file.elf);
2256 /* ignore warnings for now until we get all the code cleaned up */
2257 if (ret || warnings)