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[] = {
163 "__module_put_and_exit",
165 "kvm_spurious_fault",
169 "machine_real_restart",
170 "rewind_stack_and_make_dead",
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,
556 * Find the destination instructions for all calls.
558 static int add_call_destinations(struct objtool_file *file)
560 struct instruction *insn;
561 unsigned long dest_off;
564 for_each_insn(file, insn) {
565 if (insn->type != INSN_CALL)
568 rela = find_rela_by_dest_range(insn->sec, insn->offset,
571 dest_off = insn->offset + insn->len + insn->immediate;
572 insn->call_dest = find_symbol_by_offset(insn->sec,
575 if (!insn->call_dest && !insn->ignore) {
576 WARN_FUNC("unsupported intra-function call",
577 insn->sec, insn->offset);
579 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
583 } else if (rela->sym->type == STT_SECTION) {
584 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
586 if (!insn->call_dest ||
587 insn->call_dest->type != STT_FUNC) {
588 WARN_FUNC("can't find call dest symbol at %s+0x%x",
589 insn->sec, insn->offset,
590 rela->sym->sec->name,
595 insn->call_dest = rela->sym;
602 * The .alternatives section requires some extra special care, over and above
603 * what other special sections require:
605 * 1. Because alternatives are patched in-place, we need to insert a fake jump
606 * instruction at the end so that validate_branch() skips all the original
607 * replaced instructions when validating the new instruction path.
609 * 2. An added wrinkle is that the new instruction length might be zero. In
610 * that case the old instructions are replaced with noops. We simulate that
611 * by creating a fake jump as the only new instruction.
613 * 3. In some cases, the alternative section includes an instruction which
614 * conditionally jumps to the _end_ of the entry. We have to modify these
615 * jumps' destinations to point back to .text rather than the end of the
616 * entry in .altinstr_replacement.
618 * 4. It has been requested that we don't validate the !POPCNT feature path
619 * which is a "very very small percentage of machines".
621 static int handle_group_alt(struct objtool_file *file,
622 struct special_alt *special_alt,
623 struct instruction *orig_insn,
624 struct instruction **new_insn)
626 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
627 unsigned long dest_off;
629 last_orig_insn = NULL;
631 sec_for_each_insn_from(file, insn) {
632 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
635 if (special_alt->skip_orig)
636 insn->type = INSN_NOP;
638 insn->alt_group = true;
639 last_orig_insn = insn;
642 if (next_insn_same_sec(file, last_orig_insn)) {
643 fake_jump = malloc(sizeof(*fake_jump));
645 WARN("malloc failed");
648 memset(fake_jump, 0, sizeof(*fake_jump));
649 INIT_LIST_HEAD(&fake_jump->alts);
650 clear_insn_state(&fake_jump->state);
652 fake_jump->sec = special_alt->new_sec;
653 fake_jump->offset = FAKE_JUMP_OFFSET;
654 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
655 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
656 fake_jump->func = orig_insn->func;
659 if (!special_alt->new_len) {
661 WARN("%s: empty alternative at end of section",
662 special_alt->orig_sec->name);
666 *new_insn = fake_jump;
670 last_new_insn = NULL;
672 sec_for_each_insn_from(file, insn) {
673 if (insn->offset >= special_alt->new_off + special_alt->new_len)
676 last_new_insn = insn;
678 insn->ignore = orig_insn->ignore_alts;
680 if (insn->type != INSN_JUMP_CONDITIONAL &&
681 insn->type != INSN_JUMP_UNCONDITIONAL)
684 if (!insn->immediate)
687 dest_off = insn->offset + insn->len + insn->immediate;
688 if (dest_off == special_alt->new_off + special_alt->new_len) {
690 WARN("%s: alternative jump to end of section",
691 special_alt->orig_sec->name);
694 insn->jump_dest = fake_jump;
697 if (!insn->jump_dest) {
698 WARN_FUNC("can't find alternative jump destination",
699 insn->sec, insn->offset);
704 if (!last_new_insn) {
705 WARN_FUNC("can't find last new alternative instruction",
706 special_alt->new_sec, special_alt->new_off);
711 list_add(&fake_jump->list, &last_new_insn->list);
717 * A jump table entry can either convert a nop to a jump or a jump to a nop.
718 * If the original instruction is a jump, make the alt entry an effective nop
719 * by just skipping the original instruction.
721 static int handle_jump_alt(struct objtool_file *file,
722 struct special_alt *special_alt,
723 struct instruction *orig_insn,
724 struct instruction **new_insn)
726 if (orig_insn->type == INSN_NOP)
729 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
730 WARN_FUNC("unsupported instruction at jump label",
731 orig_insn->sec, orig_insn->offset);
735 *new_insn = list_next_entry(orig_insn, list);
740 * Read all the special sections which have alternate instructions which can be
741 * patched in or redirected to at runtime. Each instruction having alternate
742 * instruction(s) has them added to its insn->alts list, which will be
743 * traversed in validate_branch().
745 static int add_special_section_alts(struct objtool_file *file)
747 struct list_head special_alts;
748 struct instruction *orig_insn, *new_insn;
749 struct special_alt *special_alt, *tmp;
750 struct alternative *alt;
753 ret = special_get_alts(file->elf, &special_alts);
757 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
759 orig_insn = find_insn(file, special_alt->orig_sec,
760 special_alt->orig_off);
762 WARN_FUNC("special: can't find orig instruction",
763 special_alt->orig_sec, special_alt->orig_off);
769 if (!special_alt->group || special_alt->new_len) {
770 new_insn = find_insn(file, special_alt->new_sec,
771 special_alt->new_off);
773 WARN_FUNC("special: can't find new instruction",
774 special_alt->new_sec,
775 special_alt->new_off);
781 if (special_alt->group) {
782 if (!special_alt->orig_len) {
783 WARN_FUNC("empty alternative entry",
784 orig_insn->sec, orig_insn->offset);
788 ret = handle_group_alt(file, special_alt, orig_insn,
792 } else if (special_alt->jump_or_nop) {
793 ret = handle_jump_alt(file, special_alt, orig_insn,
799 alt = malloc(sizeof(*alt));
801 WARN("malloc failed");
806 alt->insn = new_insn;
807 list_add_tail(&alt->list, &orig_insn->alts);
809 list_del(&special_alt->list);
817 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
818 struct rela *table, struct rela *next_table)
820 struct rela *rela = table;
821 struct instruction *alt_insn;
822 struct alternative *alt;
823 struct symbol *pfunc = insn->func->pfunc;
824 unsigned int prev_offset = 0;
826 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
827 if (rela == next_table)
830 /* Make sure the switch table entries are consecutive: */
831 if (prev_offset && rela->offset != prev_offset + 8)
834 /* Detect function pointers from contiguous objects: */
835 if (rela->sym->sec == pfunc->sec &&
836 rela->addend == pfunc->offset)
839 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
843 /* Make sure the jmp dest is in the function or subfunction: */
844 if (alt_insn->func->pfunc != pfunc)
847 alt = malloc(sizeof(*alt));
849 WARN("malloc failed");
853 alt->insn = alt_insn;
854 list_add_tail(&alt->list, &insn->alts);
855 prev_offset = rela->offset;
859 WARN_FUNC("can't find switch jump table",
860 insn->sec, insn->offset);
868 * find_switch_table() - Given a dynamic jump, find the switch jump table in
869 * .rodata associated with it.
871 * There are 3 basic patterns:
873 * 1. jmpq *[rodata addr](,%reg,8)
875 * This is the most common case by far. It jumps to an address in a simple
876 * jump table which is stored in .rodata.
878 * 2. jmpq *[rodata addr](%rip)
880 * This is caused by a rare GCC quirk, currently only seen in three driver
881 * functions in the kernel, only with certain obscure non-distro configs.
883 * As part of an optimization, GCC makes a copy of an existing switch jump
884 * table, modifies it, and then hard-codes the jump (albeit with an indirect
885 * jump) to use a single entry in the table. The rest of the jump table and
886 * some of its jump targets remain as dead code.
888 * In such a case we can just crudely ignore all unreachable instruction
889 * warnings for the entire object file. Ideally we would just ignore them
890 * for the function, but that would require redesigning the code quite a
891 * bit. And honestly that's just not worth doing: unreachable instruction
892 * warnings are of questionable value anyway, and this is such a rare issue.
894 * 3. mov [rodata addr],%reg1
895 * ... some instructions ...
896 * jmpq *(%reg1,%reg2,8)
898 * This is a fairly uncommon pattern which is new for GCC 6. As of this
899 * writing, there are 11 occurrences of it in the allmodconfig kernel.
901 * As of GCC 7 there are quite a few more of these and the 'in between' code
902 * is significant. Esp. with KASAN enabled some of the code between the mov
903 * and jmpq uses .rodata itself, which can confuse things.
905 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
906 * ensure the same register is used in the mov and jump instructions.
908 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
910 static struct rela *find_switch_table(struct objtool_file *file,
912 struct instruction *insn)
914 struct rela *text_rela, *rodata_rela;
915 struct instruction *orig_insn = insn;
916 struct section *rodata_sec;
917 unsigned long table_offset;
920 * Backward search using the @first_jump_src links, these help avoid
921 * much of the 'in between' code. Which avoids us getting confused by
925 &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func;
926 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
928 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
931 /* allow small jumps within the range */
932 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
934 (insn->jump_dest->offset <= insn->offset ||
935 insn->jump_dest->offset > orig_insn->offset))
938 /* look for a relocation which references .rodata */
939 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
941 if (!text_rela || text_rela->sym->type != STT_SECTION ||
942 !text_rela->sym->sec->rodata)
945 table_offset = text_rela->addend;
946 rodata_sec = text_rela->sym->sec;
948 if (text_rela->type == R_X86_64_PC32)
952 * Make sure the .rodata address isn't associated with a
953 * symbol. gcc jump tables are anonymous data.
955 if (find_symbol_containing(rodata_sec, table_offset))
958 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
961 * Use of RIP-relative switch jumps is quite rare, and
962 * indicates a rare GCC quirk/bug which can leave dead
965 if (text_rela->type == R_X86_64_PC32)
966 file->ignore_unreachables = true;
976 static int add_func_switch_tables(struct objtool_file *file,
979 struct instruction *insn, *last = NULL, *prev_jump = NULL;
980 struct rela *rela, *prev_rela = NULL;
983 func_for_each_insn_all(file, func, insn) {
988 * Store back-pointers for unconditional forward jumps such
989 * that find_switch_table() can back-track using those and
990 * avoid some potentially confusing code.
992 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
993 insn->offset > last->offset &&
994 insn->jump_dest->offset > insn->offset &&
995 !insn->jump_dest->first_jump_src) {
997 insn->jump_dest->first_jump_src = insn;
998 last = insn->jump_dest;
1001 if (insn->type != INSN_JUMP_DYNAMIC)
1004 rela = find_switch_table(file, func, insn);
1009 * We found a switch table, but we don't know yet how big it
1010 * is. Don't add it until we reach the end of the function or
1011 * the beginning of another switch table in the same function.
1014 ret = add_switch_table(file, prev_jump, prev_rela, rela);
1024 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1033 * For some switch statements, gcc generates a jump table in the .rodata
1034 * section which contains a list of addresses within the function to jump to.
1035 * This finds these jump tables and adds them to the insn->alts lists.
1037 static int add_switch_table_alts(struct objtool_file *file)
1039 struct section *sec;
1040 struct symbol *func;
1046 for_each_sec(file, sec) {
1047 list_for_each_entry(func, &sec->symbol_list, list) {
1048 if (func->type != STT_FUNC)
1051 ret = add_func_switch_tables(file, func);
1060 static int read_unwind_hints(struct objtool_file *file)
1062 struct section *sec, *relasec;
1064 struct unwind_hint *hint;
1065 struct instruction *insn;
1066 struct cfi_reg *cfa;
1069 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1073 relasec = sec->rela;
1075 WARN("missing .rela.discard.unwind_hints section");
1079 if (sec->len % sizeof(struct unwind_hint)) {
1080 WARN("struct unwind_hint size mismatch");
1086 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1087 hint = (struct unwind_hint *)sec->data->d_buf + i;
1089 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1091 WARN("can't find rela for unwind_hints[%d]", i);
1095 insn = find_insn(file, rela->sym->sec, rela->addend);
1097 WARN("can't find insn for unwind_hints[%d]", i);
1101 cfa = &insn->state.cfa;
1103 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1107 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1108 insn->restore = true;
1115 switch (hint->sp_reg) {
1116 case ORC_REG_UNDEFINED:
1117 cfa->base = CFI_UNDEFINED;
1125 case ORC_REG_SP_INDIRECT:
1126 cfa->base = CFI_SP_INDIRECT;
1129 cfa->base = CFI_R10;
1132 cfa->base = CFI_R13;
1141 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1142 insn->sec, insn->offset, hint->sp_reg);
1146 cfa->offset = hint->sp_offset;
1147 insn->state.type = hint->type;
1153 static int read_retpoline_hints(struct objtool_file *file)
1155 struct section *sec;
1156 struct instruction *insn;
1159 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1163 list_for_each_entry(rela, &sec->rela_list, list) {
1164 if (rela->sym->type != STT_SECTION) {
1165 WARN("unexpected relocation symbol type in %s", sec->name);
1169 insn = find_insn(file, rela->sym->sec, rela->addend);
1171 WARN("bad .discard.retpoline_safe entry");
1175 if (insn->type != INSN_JUMP_DYNAMIC &&
1176 insn->type != INSN_CALL_DYNAMIC) {
1177 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1178 insn->sec, insn->offset);
1182 insn->retpoline_safe = true;
1188 static void mark_rodata(struct objtool_file *file)
1190 struct section *sec;
1194 * This searches for the .rodata section or multiple .rodata.func_name
1195 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1196 * rodata sections are ignored as they don't contain jump tables.
1198 for_each_sec(file, sec) {
1199 if (!strncmp(sec->name, ".rodata", 7) &&
1200 !strstr(sec->name, ".str1.")) {
1206 file->rodata = found;
1209 static int decode_sections(struct objtool_file *file)
1215 ret = decode_instructions(file);
1219 ret = add_dead_ends(file);
1225 ret = add_nospec_ignores(file);
1229 ret = add_jump_destinations(file);
1233 ret = add_special_section_alts(file);
1237 ret = add_call_destinations(file);
1241 ret = add_switch_table_alts(file);
1245 ret = read_unwind_hints(file);
1249 ret = read_retpoline_hints(file);
1256 static bool is_fentry_call(struct instruction *insn)
1258 if (insn->type == INSN_CALL &&
1259 insn->call_dest->type == STT_NOTYPE &&
1260 !strcmp(insn->call_dest->name, "__fentry__"))
1266 static bool has_modified_stack_frame(struct insn_state *state)
1270 if (state->cfa.base != initial_func_cfi.cfa.base ||
1271 state->cfa.offset != initial_func_cfi.cfa.offset ||
1272 state->stack_size != initial_func_cfi.cfa.offset ||
1276 for (i = 0; i < CFI_NUM_REGS; i++)
1277 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1278 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1284 static bool has_valid_stack_frame(struct insn_state *state)
1286 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1287 state->regs[CFI_BP].offset == -16)
1290 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1296 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1298 struct cfi_reg *cfa = &state->cfa;
1299 struct stack_op *op = &insn->stack_op;
1301 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1305 if (op->dest.type == OP_DEST_PUSH)
1309 if (op->src.type == OP_SRC_POP)
1312 /* add immediate to sp */
1313 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1314 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1315 cfa->offset -= op->src.offset;
1320 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1323 if (arch_callee_saved_reg(reg) &&
1324 state->regs[reg].base == CFI_UNDEFINED) {
1325 state->regs[reg].base = base;
1326 state->regs[reg].offset = offset;
1330 static void restore_reg(struct insn_state *state, unsigned char reg)
1332 state->regs[reg].base = CFI_UNDEFINED;
1333 state->regs[reg].offset = 0;
1337 * A note about DRAP stack alignment:
1339 * GCC has the concept of a DRAP register, which is used to help keep track of
1340 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1341 * register. The typical DRAP pattern is:
1343 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1344 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1345 * 41 ff 72 f8 pushq -0x8(%r10)
1347 * 48 89 e5 mov %rsp,%rbp
1354 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1357 * There are some variations in the epilogues, like:
1365 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1370 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1371 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1372 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1373 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1375 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1378 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1379 * restored beforehand:
1382 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1383 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1385 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1389 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1391 struct stack_op *op = &insn->stack_op;
1392 struct cfi_reg *cfa = &state->cfa;
1393 struct cfi_reg *regs = state->regs;
1395 /* stack operations don't make sense with an undefined CFA */
1396 if (cfa->base == CFI_UNDEFINED) {
1398 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1404 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1405 return update_insn_state_regs(insn, state);
1407 switch (op->dest.type) {
1410 switch (op->src.type) {
1413 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1414 cfa->base == CFI_SP &&
1415 regs[CFI_BP].base == CFI_CFA &&
1416 regs[CFI_BP].offset == -cfa->offset) {
1418 /* mov %rsp, %rbp */
1419 cfa->base = op->dest.reg;
1420 state->bp_scratch = false;
1423 else if (op->src.reg == CFI_SP &&
1424 op->dest.reg == CFI_BP && state->drap) {
1426 /* drap: mov %rsp, %rbp */
1427 regs[CFI_BP].base = CFI_BP;
1428 regs[CFI_BP].offset = -state->stack_size;
1429 state->bp_scratch = false;
1432 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1437 * This is needed for the rare case where GCC
1444 state->vals[op->dest.reg].base = CFI_CFA;
1445 state->vals[op->dest.reg].offset = -state->stack_size;
1448 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1449 cfa->base == CFI_BP) {
1454 * Restore the original stack pointer (Clang).
1456 state->stack_size = -state->regs[CFI_BP].offset;
1459 else if (op->dest.reg == cfa->base) {
1461 /* mov %reg, %rsp */
1462 if (cfa->base == CFI_SP &&
1463 state->vals[op->src.reg].base == CFI_CFA) {
1466 * This is needed for the rare case
1467 * where GCC does something dumb like:
1469 * lea 0x8(%rsp), %rcx
1473 cfa->offset = -state->vals[op->src.reg].offset;
1474 state->stack_size = cfa->offset;
1477 cfa->base = CFI_UNDEFINED;
1485 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1488 state->stack_size -= op->src.offset;
1489 if (cfa->base == CFI_SP)
1490 cfa->offset -= op->src.offset;
1494 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1496 /* lea disp(%rbp), %rsp */
1497 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1501 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1503 /* drap: lea disp(%rsp), %drap */
1504 state->drap_reg = op->dest.reg;
1507 * lea disp(%rsp), %reg
1509 * This is needed for the rare case where GCC
1510 * does something dumb like:
1512 * lea 0x8(%rsp), %rcx
1516 state->vals[op->dest.reg].base = CFI_CFA;
1517 state->vals[op->dest.reg].offset = \
1518 -state->stack_size + op->src.offset;
1523 if (state->drap && op->dest.reg == CFI_SP &&
1524 op->src.reg == state->drap_reg) {
1526 /* drap: lea disp(%drap), %rsp */
1528 cfa->offset = state->stack_size = -op->src.offset;
1529 state->drap_reg = CFI_UNDEFINED;
1530 state->drap = false;
1534 if (op->dest.reg == state->cfa.base) {
1535 WARN_FUNC("unsupported stack register modification",
1536 insn->sec, insn->offset);
1543 if (op->dest.reg != CFI_SP ||
1544 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1545 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1546 WARN_FUNC("unsupported stack pointer realignment",
1547 insn->sec, insn->offset);
1551 if (state->drap_reg != CFI_UNDEFINED) {
1552 /* drap: and imm, %rsp */
1553 cfa->base = state->drap_reg;
1554 cfa->offset = state->stack_size = 0;
1559 * Older versions of GCC (4.8ish) realign the stack
1560 * without DRAP, with a frame pointer.
1566 if (!state->drap && op->dest.type == OP_DEST_REG &&
1567 op->dest.reg == cfa->base) {
1573 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1574 op->dest.type == OP_DEST_REG &&
1575 op->dest.reg == state->drap_reg &&
1576 state->drap_offset == -state->stack_size) {
1578 /* drap: pop %drap */
1579 cfa->base = state->drap_reg;
1581 state->drap_offset = -1;
1583 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1586 restore_reg(state, op->dest.reg);
1589 state->stack_size -= 8;
1590 if (cfa->base == CFI_SP)
1595 case OP_SRC_REG_INDIRECT:
1596 if (state->drap && op->src.reg == CFI_BP &&
1597 op->src.offset == state->drap_offset) {
1599 /* drap: mov disp(%rbp), %drap */
1600 cfa->base = state->drap_reg;
1602 state->drap_offset = -1;
1605 if (state->drap && op->src.reg == CFI_BP &&
1606 op->src.offset == regs[op->dest.reg].offset) {
1608 /* drap: mov disp(%rbp), %reg */
1609 restore_reg(state, op->dest.reg);
1611 } else if (op->src.reg == cfa->base &&
1612 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1614 /* mov disp(%rbp), %reg */
1615 /* mov disp(%rsp), %reg */
1616 restore_reg(state, op->dest.reg);
1622 WARN_FUNC("unknown stack-related instruction",
1623 insn->sec, insn->offset);
1630 state->stack_size += 8;
1631 if (cfa->base == CFI_SP)
1634 if (op->src.type != OP_SRC_REG)
1638 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1640 /* drap: push %drap */
1641 cfa->base = CFI_BP_INDIRECT;
1642 cfa->offset = -state->stack_size;
1644 /* save drap so we know when to restore it */
1645 state->drap_offset = -state->stack_size;
1647 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1649 /* drap: push %rbp */
1650 state->stack_size = 0;
1652 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1654 /* drap: push %reg */
1655 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1661 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1664 /* detect when asm code uses rbp as a scratch register */
1665 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1666 cfa->base != CFI_BP)
1667 state->bp_scratch = true;
1670 case OP_DEST_REG_INDIRECT:
1673 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1675 /* drap: mov %drap, disp(%rbp) */
1676 cfa->base = CFI_BP_INDIRECT;
1677 cfa->offset = op->dest.offset;
1679 /* save drap offset so we know when to restore it */
1680 state->drap_offset = op->dest.offset;
1683 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1685 /* drap: mov reg, disp(%rbp) */
1686 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1689 } else if (op->dest.reg == cfa->base) {
1691 /* mov reg, disp(%rbp) */
1692 /* mov reg, disp(%rsp) */
1693 save_reg(state, op->src.reg, CFI_CFA,
1694 op->dest.offset - state->cfa.offset);
1700 if ((!state->drap && cfa->base != CFI_BP) ||
1701 (state->drap && cfa->base != state->drap_reg)) {
1702 WARN_FUNC("leave instruction with modified stack frame",
1703 insn->sec, insn->offset);
1707 /* leave (mov %rbp, %rsp; pop %rbp) */
1709 state->stack_size = -state->regs[CFI_BP].offset - 8;
1710 restore_reg(state, CFI_BP);
1720 if (op->src.type != OP_SRC_POP) {
1721 WARN_FUNC("unknown stack-related memory operation",
1722 insn->sec, insn->offset);
1727 state->stack_size -= 8;
1728 if (cfa->base == CFI_SP)
1734 WARN_FUNC("unknown stack-related instruction",
1735 insn->sec, insn->offset);
1742 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1744 struct insn_state *state1 = &insn->state, *state2 = state;
1747 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1748 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1749 insn->sec, insn->offset,
1750 state1->cfa.base, state1->cfa.offset,
1751 state2->cfa.base, state2->cfa.offset);
1753 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1754 for (i = 0; i < CFI_NUM_REGS; i++) {
1755 if (!memcmp(&state1->regs[i], &state2->regs[i],
1756 sizeof(struct cfi_reg)))
1759 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1760 insn->sec, insn->offset,
1761 i, state1->regs[i].base, state1->regs[i].offset,
1762 i, state2->regs[i].base, state2->regs[i].offset);
1766 } else if (state1->type != state2->type) {
1767 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1768 insn->sec, insn->offset, state1->type, state2->type);
1770 } else if (state1->drap != state2->drap ||
1771 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1772 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1773 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1774 insn->sec, insn->offset,
1775 state1->drap, state1->drap_reg, state1->drap_offset,
1776 state2->drap, state2->drap_reg, state2->drap_offset);
1785 * Follow the branch starting at the given instruction, and recursively follow
1786 * any other branches (jumps). Meanwhile, track the frame pointer state at
1787 * each instruction and validate all the rules described in
1788 * tools/objtool/Documentation/stack-validation.txt.
1790 static int validate_branch(struct objtool_file *file, struct instruction *first,
1791 struct insn_state state)
1793 struct alternative *alt;
1794 struct instruction *insn, *next_insn;
1795 struct section *sec;
1796 struct symbol *func = NULL;
1802 if (insn->alt_group && list_empty(&insn->alts)) {
1803 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1809 next_insn = next_insn_same_sec(file, insn);
1811 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1812 WARN("%s() falls through to next function %s()",
1813 func->name, insn->func->name);
1818 func = insn->func->pfunc;
1820 if (func && insn->ignore) {
1821 WARN_FUNC("BUG: why am I validating an ignored function?",
1826 if (insn->visited) {
1827 if (!insn->hint && !insn_state_match(insn, &state))
1834 if (insn->restore) {
1835 struct instruction *save_insn, *i;
1839 func_for_each_insn_continue_reverse(file, insn->func, i) {
1847 WARN_FUNC("no corresponding CFI save for CFI restore",
1852 if (!save_insn->visited) {
1854 * Oops, no state to copy yet.
1855 * Hopefully we can reach this
1856 * instruction from another branch
1857 * after the save insn has been
1863 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1868 insn->state = save_insn->state;
1871 state = insn->state;
1874 insn->state = state;
1876 insn->visited = true;
1878 if (!insn->ignore_alts) {
1879 list_for_each_entry(alt, &insn->alts, list) {
1880 ret = validate_branch(file, alt->insn, state);
1886 switch (insn->type) {
1889 if (func && has_modified_stack_frame(&state)) {
1890 WARN_FUNC("return with modified stack frame",
1895 if (state.bp_scratch) {
1896 WARN("%s uses BP as a scratch register",
1904 if (is_fentry_call(insn))
1907 ret = dead_end_function(file, insn->call_dest);
1914 case INSN_CALL_DYNAMIC:
1915 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1916 WARN_FUNC("call without frame pointer save/setup",
1922 case INSN_JUMP_CONDITIONAL:
1923 case INSN_JUMP_UNCONDITIONAL:
1924 if (insn->jump_dest &&
1925 (!func || !insn->jump_dest->func ||
1926 insn->jump_dest->func->pfunc == func)) {
1927 ret = validate_branch(file, insn->jump_dest,
1932 } else if (func && has_modified_stack_frame(&state)) {
1933 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1938 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1943 case INSN_JUMP_DYNAMIC:
1944 if (func && list_empty(&insn->alts) &&
1945 has_modified_stack_frame(&state)) {
1946 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1953 case INSN_CONTEXT_SWITCH:
1954 if (func && (!next_insn || !next_insn->hint)) {
1955 WARN_FUNC("unsupported instruction in callable function",
1962 if (update_insn_state(insn, &state))
1975 if (state.cfa.base == CFI_UNDEFINED)
1977 WARN("%s: unexpected end of section", sec->name);
1987 static int validate_unwind_hints(struct objtool_file *file)
1989 struct instruction *insn;
1990 int ret, warnings = 0;
1991 struct insn_state state;
1996 clear_insn_state(&state);
1998 for_each_insn(file, insn) {
1999 if (insn->hint && !insn->visited) {
2000 ret = validate_branch(file, insn, state);
2008 static int validate_retpoline(struct objtool_file *file)
2010 struct instruction *insn;
2013 for_each_insn(file, insn) {
2014 if (insn->type != INSN_JUMP_DYNAMIC &&
2015 insn->type != INSN_CALL_DYNAMIC)
2018 if (insn->retpoline_safe)
2022 * .init.text code is ran before userspace and thus doesn't
2023 * strictly need retpolines, except for modules which are
2024 * loaded late, they very much do need retpoline in their
2027 if (!strcmp(insn->sec->name, ".init.text") && !module)
2030 WARN_FUNC("indirect %s found in RETPOLINE build",
2031 insn->sec, insn->offset,
2032 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2040 static bool is_kasan_insn(struct instruction *insn)
2042 return (insn->type == INSN_CALL &&
2043 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2046 static bool is_ubsan_insn(struct instruction *insn)
2048 return (insn->type == INSN_CALL &&
2049 !strcmp(insn->call_dest->name,
2050 "__ubsan_handle_builtin_unreachable"));
2053 static bool ignore_unreachable_insn(struct instruction *insn)
2057 if (insn->ignore || insn->type == INSN_NOP)
2061 * Ignore any unused exceptions. This can happen when a whitelisted
2062 * function has an exception table entry.
2064 * Also ignore alternative replacement instructions. This can happen
2065 * when a whitelisted function uses one of the ALTERNATIVE macros.
2067 if (!strcmp(insn->sec->name, ".fixup") ||
2068 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2069 !strcmp(insn->sec->name, ".altinstr_aux"))
2076 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2077 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2078 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2079 * (or occasionally a JMP to UD2).
2081 if (list_prev_entry(insn, list)->dead_end &&
2082 (insn->type == INSN_BUG ||
2083 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2084 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2088 * Check if this (or a subsequent) instruction is related to
2089 * CONFIG_UBSAN or CONFIG_KASAN.
2091 * End the search at 5 instructions to avoid going into the weeds.
2093 for (i = 0; i < 5; i++) {
2095 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2098 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2099 if (insn->jump_dest &&
2100 insn->jump_dest->func == insn->func) {
2101 insn = insn->jump_dest;
2108 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2111 insn = list_next_entry(insn, list);
2117 static int validate_functions(struct objtool_file *file)
2119 struct section *sec;
2120 struct symbol *func;
2121 struct instruction *insn;
2122 struct insn_state state;
2123 int ret, warnings = 0;
2125 clear_insn_state(&state);
2127 state.cfa = initial_func_cfi.cfa;
2128 memcpy(&state.regs, &initial_func_cfi.regs,
2129 CFI_NUM_REGS * sizeof(struct cfi_reg));
2130 state.stack_size = initial_func_cfi.cfa.offset;
2132 for_each_sec(file, sec) {
2133 list_for_each_entry(func, &sec->symbol_list, list) {
2134 if (func->type != STT_FUNC || func->pfunc != func)
2137 insn = find_insn(file, sec, func->offset);
2138 if (!insn || insn->ignore)
2141 ret = validate_branch(file, insn, state);
2149 static int validate_reachable_instructions(struct objtool_file *file)
2151 struct instruction *insn;
2153 if (file->ignore_unreachables)
2156 for_each_insn(file, insn) {
2157 if (insn->visited || ignore_unreachable_insn(insn))
2160 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2167 static void cleanup(struct objtool_file *file)
2169 struct instruction *insn, *tmpinsn;
2170 struct alternative *alt, *tmpalt;
2172 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2173 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2174 list_del(&alt->list);
2177 list_del(&insn->list);
2178 hash_del(&insn->hash);
2181 elf_close(file->elf);
2184 static struct objtool_file file;
2186 int check(const char *_objname, bool orc)
2188 int ret, warnings = 0;
2192 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2196 INIT_LIST_HEAD(&file.insn_list);
2197 hash_init(file.insn_hash);
2198 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2199 file.c_file = find_section_by_name(file.elf, ".comment");
2200 file.ignore_unreachables = no_unreachable;
2203 arch_initial_func_cfi_state(&initial_func_cfi);
2205 ret = decode_sections(&file);
2210 if (list_empty(&file.insn_list))
2214 ret = validate_retpoline(&file);
2220 ret = validate_functions(&file);
2225 ret = validate_unwind_hints(&file);
2231 ret = validate_reachable_instructions(&file);
2238 ret = create_orc(&file);
2242 ret = create_orc_sections(&file);
2246 ret = elf_write(file.elf);
2254 /* ignore warnings for now until we get all the code cleaned up */
2255 if (ret || warnings)