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/>.
27 #include <linux/hashtable.h>
28 #include <linux/kernel.h>
31 struct list_head list
;
32 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 #define func_for_each_insn(file, func, insn) \
63 for (insn = find_insn(file, func->sec, func->offset); \
64 insn && &insn->list != &file->insn_list && \
65 insn->sec == func->sec && \
66 insn->offset < func->offset + func->len; \
67 insn = list_next_entry(insn, list))
69 #define func_for_each_insn_continue_reverse(file, func, insn) \
70 for (insn = list_prev_entry(insn, list); \
71 &insn->list != &file->insn_list && \
72 insn->sec == func->sec && insn->offset >= func->offset; \
73 insn = list_prev_entry(insn, list))
75 #define sec_for_each_insn_from(file, insn) \
76 for (; insn; insn = next_insn_same_sec(file, insn))
78 #define sec_for_each_insn_continue(file, insn) \
79 for (insn = next_insn_same_sec(file, insn); insn; \
80 insn = next_insn_same_sec(file, insn))
83 * Check if the function has been manually whitelisted with the
84 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
85 * due to its use of a context switching instruction.
87 static bool ignore_func(struct objtool_file
*file
, struct symbol
*func
)
91 /* check for STACK_FRAME_NON_STANDARD */
92 if (file
->whitelist
&& file
->whitelist
->rela
)
93 list_for_each_entry(rela
, &file
->whitelist
->rela
->rela_list
, list
) {
94 if (rela
->sym
->type
== STT_SECTION
&&
95 rela
->sym
->sec
== func
->sec
&&
96 rela
->addend
== func
->offset
)
98 if (rela
->sym
->type
== STT_FUNC
&& rela
->sym
== func
)
106 * This checks to see if the given function is a "noreturn" function.
108 * For global functions which are outside the scope of this object file, we
109 * have to keep a manual list of them.
111 * For local functions, we have to detect them manually by simply looking for
112 * the lack of a return instruction.
119 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
123 struct instruction
*insn
;
127 * Unfortunately these have to be hard coded because the noreturn
128 * attribute isn't provided in ELF data.
130 static const char * const global_noreturns
[] = {
135 "__module_put_and_exit",
137 "kvm_spurious_fault",
144 if (func
->bind
== STB_WEAK
)
147 if (func
->bind
== STB_GLOBAL
)
148 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
149 if (!strcmp(func
->name
, global_noreturns
[i
]))
155 func_for_each_insn(file
, func
, insn
) {
158 if (insn
->type
== INSN_RETURN
)
166 * A function can have a sibling call instead of a return. In that
167 * case, the function's dead-end status depends on whether the target
168 * of the sibling call returns.
170 func_for_each_insn(file
, func
, insn
) {
171 if (insn
->sec
!= func
->sec
||
172 insn
->offset
>= func
->offset
+ func
->len
)
175 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
176 struct instruction
*dest
= insn
->jump_dest
;
177 struct symbol
*dest_func
;
180 /* sibling call to another file */
183 if (dest
->sec
!= func
->sec
||
184 dest
->offset
< func
->offset
||
185 dest
->offset
>= func
->offset
+ func
->len
) {
186 /* local sibling call */
187 dest_func
= find_symbol_by_offset(dest
->sec
,
192 if (recursion
== 5) {
193 WARN_FUNC("infinite recursion (objtool bug!)",
194 dest
->sec
, dest
->offset
);
198 return __dead_end_function(file
, dest_func
,
203 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
211 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
213 return __dead_end_function(file
, func
, 0);
216 static void clear_insn_state(struct insn_state
*state
)
220 memset(state
, 0, sizeof(*state
));
221 state
->cfa
.base
= CFI_UNDEFINED
;
222 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
223 state
->regs
[i
].base
= CFI_UNDEFINED
;
224 state
->vals
[i
].base
= CFI_UNDEFINED
;
226 state
->drap_reg
= CFI_UNDEFINED
;
227 state
->drap_offset
= -1;
231 * Call the arch-specific instruction decoder for all the instructions and add
232 * them to the global instruction list.
234 static int decode_instructions(struct objtool_file
*file
)
238 unsigned long offset
;
239 struct instruction
*insn
;
242 for_each_sec(file
, sec
) {
244 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
247 if (strcmp(sec
->name
, ".altinstr_replacement") &&
248 strcmp(sec
->name
, ".altinstr_aux") &&
249 strncmp(sec
->name
, ".discard.", 9))
252 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
253 insn
= malloc(sizeof(*insn
));
255 WARN("malloc failed");
258 memset(insn
, 0, sizeof(*insn
));
259 INIT_LIST_HEAD(&insn
->alts
);
260 clear_insn_state(&insn
->state
);
263 insn
->offset
= offset
;
265 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
267 &insn
->len
, &insn
->type
,
273 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
274 WARN_FUNC("invalid instruction type %d",
275 insn
->sec
, insn
->offset
, insn
->type
);
280 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
281 list_add_tail(&insn
->list
, &file
->insn_list
);
284 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
285 if (func
->type
!= STT_FUNC
)
288 if (!find_insn(file
, sec
, func
->offset
)) {
289 WARN("%s(): can't find starting instruction",
294 func_for_each_insn(file
, func
, insn
)
308 * Mark "ud2" instructions and manually annotated dead ends.
310 static int add_dead_ends(struct objtool_file
*file
)
314 struct instruction
*insn
;
318 * By default, "ud2" is a dead end unless otherwise annotated, because
319 * GCC 7 inserts it for certain divide-by-zero cases.
321 for_each_insn(file
, insn
)
322 if (insn
->type
== INSN_BUG
)
323 insn
->dead_end
= true;
326 * Check for manually annotated dead ends.
328 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
332 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
333 if (rela
->sym
->type
!= STT_SECTION
) {
334 WARN("unexpected relocation symbol type in %s", sec
->name
);
337 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
339 insn
= list_prev_entry(insn
, list
);
340 else if (rela
->addend
== rela
->sym
->sec
->len
) {
342 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
343 if (insn
->sec
== rela
->sym
->sec
) {
350 WARN("can't find unreachable insn at %s+0x%x",
351 rela
->sym
->sec
->name
, rela
->addend
);
355 WARN("can't find unreachable insn at %s+0x%x",
356 rela
->sym
->sec
->name
, rela
->addend
);
360 insn
->dead_end
= true;
365 * These manually annotated reachable checks are needed for GCC 4.4,
366 * where the Linux unreachable() macro isn't supported. In that case
367 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
370 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
374 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
375 if (rela
->sym
->type
!= STT_SECTION
) {
376 WARN("unexpected relocation symbol type in %s", sec
->name
);
379 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
381 insn
= list_prev_entry(insn
, list
);
382 else if (rela
->addend
== rela
->sym
->sec
->len
) {
384 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
385 if (insn
->sec
== rela
->sym
->sec
) {
392 WARN("can't find reachable insn at %s+0x%x",
393 rela
->sym
->sec
->name
, rela
->addend
);
397 WARN("can't find reachable insn at %s+0x%x",
398 rela
->sym
->sec
->name
, rela
->addend
);
402 insn
->dead_end
= false;
409 * Warnings shouldn't be reported for ignored functions.
411 static void add_ignores(struct objtool_file
*file
)
413 struct instruction
*insn
;
417 for_each_sec(file
, sec
) {
418 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
419 if (func
->type
!= STT_FUNC
)
422 if (!ignore_func(file
, func
))
425 func_for_each_insn(file
, func
, insn
)
432 * FIXME: For now, just ignore any alternatives which add retpolines. This is
433 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
434 * But it at least allows objtool to understand the control flow *around* the
437 static int add_nospec_ignores(struct objtool_file
*file
)
441 struct instruction
*insn
;
443 sec
= find_section_by_name(file
->elf
, ".rela.discard.nospec");
447 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
448 if (rela
->sym
->type
!= STT_SECTION
) {
449 WARN("unexpected relocation symbol type in %s", sec
->name
);
453 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
455 WARN("bad .discard.nospec entry");
459 insn
->ignore_alts
= true;
466 * Find the destination instructions for all jumps.
468 static int add_jump_destinations(struct objtool_file
*file
)
470 struct instruction
*insn
;
472 struct section
*dest_sec
;
473 unsigned long dest_off
;
475 for_each_insn(file
, insn
) {
476 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
477 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
483 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
486 dest_sec
= insn
->sec
;
487 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
488 } else if (rela
->sym
->type
== STT_SECTION
) {
489 dest_sec
= rela
->sym
->sec
;
490 dest_off
= rela
->addend
+ 4;
491 } else if (rela
->sym
->sec
->idx
) {
492 dest_sec
= rela
->sym
->sec
;
493 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
494 } else if (strstr(rela
->sym
->name
, "_indirect_thunk_")) {
496 * Retpoline jumps are really dynamic jumps in
497 * disguise, so convert them accordingly.
499 insn
->type
= INSN_JUMP_DYNAMIC
;
507 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
508 if (!insn
->jump_dest
) {
511 * This is a special case where an alt instruction
512 * jumps past the end of the section. These are
513 * handled later in handle_group_alt().
515 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
518 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
519 insn
->sec
, insn
->offset
, dest_sec
->name
,
529 * Find the destination instructions for all calls.
531 static int add_call_destinations(struct objtool_file
*file
)
533 struct instruction
*insn
;
534 unsigned long dest_off
;
537 for_each_insn(file
, insn
) {
538 if (insn
->type
!= INSN_CALL
)
541 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
544 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
545 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
548 if (!insn
->call_dest
&& !insn
->ignore
) {
549 WARN_FUNC("unsupported intra-function call",
550 insn
->sec
, insn
->offset
);
551 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
555 } else if (rela
->sym
->type
== STT_SECTION
) {
556 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
558 if (!insn
->call_dest
||
559 insn
->call_dest
->type
!= STT_FUNC
) {
560 WARN_FUNC("can't find call dest symbol at %s+0x%x",
561 insn
->sec
, insn
->offset
,
562 rela
->sym
->sec
->name
,
567 insn
->call_dest
= rela
->sym
;
574 * The .alternatives section requires some extra special care, over and above
575 * what other special sections require:
577 * 1. Because alternatives are patched in-place, we need to insert a fake jump
578 * instruction at the end so that validate_branch() skips all the original
579 * replaced instructions when validating the new instruction path.
581 * 2. An added wrinkle is that the new instruction length might be zero. In
582 * that case the old instructions are replaced with noops. We simulate that
583 * by creating a fake jump as the only new instruction.
585 * 3. In some cases, the alternative section includes an instruction which
586 * conditionally jumps to the _end_ of the entry. We have to modify these
587 * jumps' destinations to point back to .text rather than the end of the
588 * entry in .altinstr_replacement.
590 * 4. It has been requested that we don't validate the !POPCNT feature path
591 * which is a "very very small percentage of machines".
593 static int handle_group_alt(struct objtool_file
*file
,
594 struct special_alt
*special_alt
,
595 struct instruction
*orig_insn
,
596 struct instruction
**new_insn
)
598 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
= NULL
;
599 unsigned long dest_off
;
601 last_orig_insn
= NULL
;
603 sec_for_each_insn_from(file
, insn
) {
604 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
607 if (special_alt
->skip_orig
)
608 insn
->type
= INSN_NOP
;
610 insn
->alt_group
= true;
611 last_orig_insn
= insn
;
614 if (next_insn_same_sec(file
, last_orig_insn
)) {
615 fake_jump
= malloc(sizeof(*fake_jump
));
617 WARN("malloc failed");
620 memset(fake_jump
, 0, sizeof(*fake_jump
));
621 INIT_LIST_HEAD(&fake_jump
->alts
);
622 clear_insn_state(&fake_jump
->state
);
624 fake_jump
->sec
= special_alt
->new_sec
;
625 fake_jump
->offset
= -1;
626 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
627 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
628 fake_jump
->ignore
= true;
631 if (!special_alt
->new_len
) {
633 WARN("%s: empty alternative at end of section",
634 special_alt
->orig_sec
->name
);
638 *new_insn
= fake_jump
;
642 last_new_insn
= NULL
;
644 sec_for_each_insn_from(file
, insn
) {
645 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
648 last_new_insn
= insn
;
650 insn
->ignore
= orig_insn
->ignore_alts
;
652 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
653 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
656 if (!insn
->immediate
)
659 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
660 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
) {
662 WARN("%s: alternative jump to end of section",
663 special_alt
->orig_sec
->name
);
666 insn
->jump_dest
= fake_jump
;
669 if (!insn
->jump_dest
) {
670 WARN_FUNC("can't find alternative jump destination",
671 insn
->sec
, insn
->offset
);
676 if (!last_new_insn
) {
677 WARN_FUNC("can't find last new alternative instruction",
678 special_alt
->new_sec
, special_alt
->new_off
);
683 list_add(&fake_jump
->list
, &last_new_insn
->list
);
689 * A jump table entry can either convert a nop to a jump or a jump to a nop.
690 * If the original instruction is a jump, make the alt entry an effective nop
691 * by just skipping the original instruction.
693 static int handle_jump_alt(struct objtool_file
*file
,
694 struct special_alt
*special_alt
,
695 struct instruction
*orig_insn
,
696 struct instruction
**new_insn
)
698 if (orig_insn
->type
== INSN_NOP
)
701 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
702 WARN_FUNC("unsupported instruction at jump label",
703 orig_insn
->sec
, orig_insn
->offset
);
707 *new_insn
= list_next_entry(orig_insn
, list
);
712 * Read all the special sections which have alternate instructions which can be
713 * patched in or redirected to at runtime. Each instruction having alternate
714 * instruction(s) has them added to its insn->alts list, which will be
715 * traversed in validate_branch().
717 static int add_special_section_alts(struct objtool_file
*file
)
719 struct list_head special_alts
;
720 struct instruction
*orig_insn
, *new_insn
;
721 struct special_alt
*special_alt
, *tmp
;
722 struct alternative
*alt
;
725 ret
= special_get_alts(file
->elf
, &special_alts
);
729 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
731 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
732 special_alt
->orig_off
);
734 WARN_FUNC("special: can't find orig instruction",
735 special_alt
->orig_sec
, special_alt
->orig_off
);
741 if (!special_alt
->group
|| special_alt
->new_len
) {
742 new_insn
= find_insn(file
, special_alt
->new_sec
,
743 special_alt
->new_off
);
745 WARN_FUNC("special: can't find new instruction",
746 special_alt
->new_sec
,
747 special_alt
->new_off
);
753 if (special_alt
->group
) {
754 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
758 } else if (special_alt
->jump_or_nop
) {
759 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
765 alt
= malloc(sizeof(*alt
));
767 WARN("malloc failed");
772 alt
->insn
= new_insn
;
773 list_add_tail(&alt
->list
, &orig_insn
->alts
);
775 list_del(&special_alt
->list
);
783 static int add_switch_table(struct objtool_file
*file
, struct symbol
*func
,
784 struct instruction
*insn
, struct rela
*table
,
785 struct rela
*next_table
)
787 struct rela
*rela
= table
;
788 struct instruction
*alt_insn
;
789 struct alternative
*alt
;
791 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
792 if (rela
== next_table
)
795 if (rela
->sym
->sec
!= insn
->sec
||
796 rela
->addend
<= func
->offset
||
797 rela
->addend
>= func
->offset
+ func
->len
)
800 alt_insn
= find_insn(file
, insn
->sec
, rela
->addend
);
802 WARN("%s: can't find instruction at %s+0x%x",
803 file
->rodata
->rela
->name
, insn
->sec
->name
,
808 alt
= malloc(sizeof(*alt
));
810 WARN("malloc failed");
814 alt
->insn
= alt_insn
;
815 list_add_tail(&alt
->list
, &insn
->alts
);
822 * find_switch_table() - Given a dynamic jump, find the switch jump table in
823 * .rodata associated with it.
825 * There are 3 basic patterns:
827 * 1. jmpq *[rodata addr](,%reg,8)
829 * This is the most common case by far. It jumps to an address in a simple
830 * jump table which is stored in .rodata.
832 * 2. jmpq *[rodata addr](%rip)
834 * This is caused by a rare GCC quirk, currently only seen in three driver
835 * functions in the kernel, only with certain obscure non-distro configs.
837 * As part of an optimization, GCC makes a copy of an existing switch jump
838 * table, modifies it, and then hard-codes the jump (albeit with an indirect
839 * jump) to use a single entry in the table. The rest of the jump table and
840 * some of its jump targets remain as dead code.
842 * In such a case we can just crudely ignore all unreachable instruction
843 * warnings for the entire object file. Ideally we would just ignore them
844 * for the function, but that would require redesigning the code quite a
845 * bit. And honestly that's just not worth doing: unreachable instruction
846 * warnings are of questionable value anyway, and this is such a rare issue.
848 * 3. mov [rodata addr],%reg1
849 * ... some instructions ...
850 * jmpq *(%reg1,%reg2,8)
852 * This is a fairly uncommon pattern which is new for GCC 6. As of this
853 * writing, there are 11 occurrences of it in the allmodconfig kernel.
855 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
856 * ensure the same register is used in the mov and jump instructions.
858 static struct rela
*find_switch_table(struct objtool_file
*file
,
860 struct instruction
*insn
)
862 struct rela
*text_rela
, *rodata_rela
;
863 struct instruction
*orig_insn
= insn
;
865 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
, insn
->len
);
866 if (text_rela
&& text_rela
->sym
== file
->rodata
->sym
) {
868 rodata_rela
= find_rela_by_dest(file
->rodata
,
874 rodata_rela
= find_rela_by_dest(file
->rodata
,
875 text_rela
->addend
+ 4);
878 file
->ignore_unreachables
= true;
883 func_for_each_insn_continue_reverse(file
, func
, insn
) {
884 if (insn
->type
== INSN_JUMP_DYNAMIC
)
887 /* allow small jumps within the range */
888 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
890 (insn
->jump_dest
->offset
<= insn
->offset
||
891 insn
->jump_dest
->offset
> orig_insn
->offset
))
894 /* look for a relocation which references .rodata */
895 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
897 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
901 * Make sure the .rodata address isn't associated with a
902 * symbol. gcc jump tables are anonymous data.
904 if (find_symbol_containing(file
->rodata
, text_rela
->addend
))
907 return find_rela_by_dest(file
->rodata
, text_rela
->addend
);
913 static int add_func_switch_tables(struct objtool_file
*file
,
916 struct instruction
*insn
, *prev_jump
= NULL
;
917 struct rela
*rela
, *prev_rela
= NULL
;
920 func_for_each_insn(file
, func
, insn
) {
921 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
924 rela
= find_switch_table(file
, func
, insn
);
929 * We found a switch table, but we don't know yet how big it
930 * is. Don't add it until we reach the end of the function or
931 * the beginning of another switch table in the same function.
934 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
,
945 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
, NULL
);
954 * For some switch statements, gcc generates a jump table in the .rodata
955 * section which contains a list of addresses within the function to jump to.
956 * This finds these jump tables and adds them to the insn->alts lists.
958 static int add_switch_table_alts(struct objtool_file
*file
)
964 if (!file
->rodata
|| !file
->rodata
->rela
)
967 for_each_sec(file
, sec
) {
968 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
969 if (func
->type
!= STT_FUNC
)
972 ret
= add_func_switch_tables(file
, func
);
981 static int read_unwind_hints(struct objtool_file
*file
)
983 struct section
*sec
, *relasec
;
985 struct unwind_hint
*hint
;
986 struct instruction
*insn
;
990 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
996 WARN("missing .rela.discard.unwind_hints section");
1000 if (sec
->len
% sizeof(struct unwind_hint
)) {
1001 WARN("struct unwind_hint size mismatch");
1007 for (i
= 0; i
< sec
->len
/ sizeof(struct unwind_hint
); i
++) {
1008 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1010 rela
= find_rela_by_dest(sec
, i
* sizeof(*hint
));
1012 WARN("can't find rela for unwind_hints[%d]", i
);
1016 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
1018 WARN("can't find insn for unwind_hints[%d]", i
);
1022 cfa
= &insn
->state
.cfa
;
1024 if (hint
->type
== UNWIND_HINT_TYPE_SAVE
) {
1028 } else if (hint
->type
== UNWIND_HINT_TYPE_RESTORE
) {
1029 insn
->restore
= true;
1036 switch (hint
->sp_reg
) {
1037 case ORC_REG_UNDEFINED
:
1038 cfa
->base
= CFI_UNDEFINED
;
1046 case ORC_REG_SP_INDIRECT
:
1047 cfa
->base
= CFI_SP_INDIRECT
;
1050 cfa
->base
= CFI_R10
;
1053 cfa
->base
= CFI_R13
;
1062 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1063 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1067 cfa
->offset
= hint
->sp_offset
;
1068 insn
->state
.type
= hint
->type
;
1074 static int decode_sections(struct objtool_file
*file
)
1078 ret
= decode_instructions(file
);
1082 ret
= add_dead_ends(file
);
1088 ret
= add_nospec_ignores(file
);
1092 ret
= add_jump_destinations(file
);
1096 ret
= add_special_section_alts(file
);
1100 ret
= add_call_destinations(file
);
1104 ret
= add_switch_table_alts(file
);
1108 ret
= read_unwind_hints(file
);
1115 static bool is_fentry_call(struct instruction
*insn
)
1117 if (insn
->type
== INSN_CALL
&&
1118 insn
->call_dest
->type
== STT_NOTYPE
&&
1119 !strcmp(insn
->call_dest
->name
, "__fentry__"))
1125 static bool has_modified_stack_frame(struct insn_state
*state
)
1129 if (state
->cfa
.base
!= initial_func_cfi
.cfa
.base
||
1130 state
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
||
1131 state
->stack_size
!= initial_func_cfi
.cfa
.offset
||
1135 for (i
= 0; i
< CFI_NUM_REGS
; i
++)
1136 if (state
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1137 state
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1143 static bool has_valid_stack_frame(struct insn_state
*state
)
1145 if (state
->cfa
.base
== CFI_BP
&& state
->regs
[CFI_BP
].base
== CFI_CFA
&&
1146 state
->regs
[CFI_BP
].offset
== -16)
1149 if (state
->drap
&& state
->regs
[CFI_BP
].base
== CFI_BP
)
1155 static int update_insn_state_regs(struct instruction
*insn
, struct insn_state
*state
)
1157 struct cfi_reg
*cfa
= &state
->cfa
;
1158 struct stack_op
*op
= &insn
->stack_op
;
1160 if (cfa
->base
!= CFI_SP
)
1164 if (op
->dest
.type
== OP_DEST_PUSH
)
1168 if (op
->src
.type
== OP_SRC_POP
)
1171 /* add immediate to sp */
1172 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
1173 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
1174 cfa
->offset
-= op
->src
.offset
;
1179 static void save_reg(struct insn_state
*state
, unsigned char reg
, int base
,
1182 if (arch_callee_saved_reg(reg
) &&
1183 state
->regs
[reg
].base
== CFI_UNDEFINED
) {
1184 state
->regs
[reg
].base
= base
;
1185 state
->regs
[reg
].offset
= offset
;
1189 static void restore_reg(struct insn_state
*state
, unsigned char reg
)
1191 state
->regs
[reg
].base
= CFI_UNDEFINED
;
1192 state
->regs
[reg
].offset
= 0;
1196 * A note about DRAP stack alignment:
1198 * GCC has the concept of a DRAP register, which is used to help keep track of
1199 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1200 * register. The typical DRAP pattern is:
1202 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1203 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1204 * 41 ff 72 f8 pushq -0x8(%r10)
1206 * 48 89 e5 mov %rsp,%rbp
1213 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1216 * There are some variations in the epilogues, like:
1224 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1229 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1230 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1231 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1232 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1234 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1237 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1238 * restored beforehand:
1241 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1242 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1244 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1248 static int update_insn_state(struct instruction
*insn
, struct insn_state
*state
)
1250 struct stack_op
*op
= &insn
->stack_op
;
1251 struct cfi_reg
*cfa
= &state
->cfa
;
1252 struct cfi_reg
*regs
= state
->regs
;
1254 /* stack operations don't make sense with an undefined CFA */
1255 if (cfa
->base
== CFI_UNDEFINED
) {
1257 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1263 if (state
->type
== ORC_TYPE_REGS
|| state
->type
== ORC_TYPE_REGS_IRET
)
1264 return update_insn_state_regs(insn
, state
);
1266 switch (op
->dest
.type
) {
1269 switch (op
->src
.type
) {
1272 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
1273 cfa
->base
== CFI_SP
&&
1274 regs
[CFI_BP
].base
== CFI_CFA
&&
1275 regs
[CFI_BP
].offset
== -cfa
->offset
) {
1277 /* mov %rsp, %rbp */
1278 cfa
->base
= op
->dest
.reg
;
1279 state
->bp_scratch
= false;
1282 else if (op
->src
.reg
== CFI_SP
&&
1283 op
->dest
.reg
== CFI_BP
&& state
->drap
) {
1285 /* drap: mov %rsp, %rbp */
1286 regs
[CFI_BP
].base
= CFI_BP
;
1287 regs
[CFI_BP
].offset
= -state
->stack_size
;
1288 state
->bp_scratch
= false;
1291 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1296 * This is needed for the rare case where GCC
1303 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1304 state
->vals
[op
->dest
.reg
].offset
= -state
->stack_size
;
1307 else if (op
->dest
.reg
== cfa
->base
) {
1309 /* mov %reg, %rsp */
1310 if (cfa
->base
== CFI_SP
&&
1311 state
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
1314 * This is needed for the rare case
1315 * where GCC does something dumb like:
1317 * lea 0x8(%rsp), %rcx
1321 cfa
->offset
= -state
->vals
[op
->src
.reg
].offset
;
1322 state
->stack_size
= cfa
->offset
;
1325 cfa
->base
= CFI_UNDEFINED
;
1333 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
1336 state
->stack_size
-= op
->src
.offset
;
1337 if (cfa
->base
== CFI_SP
)
1338 cfa
->offset
-= op
->src
.offset
;
1342 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
1344 /* lea disp(%rbp), %rsp */
1345 state
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
1349 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1351 /* drap: lea disp(%rsp), %drap */
1352 state
->drap_reg
= op
->dest
.reg
;
1355 * lea disp(%rsp), %reg
1357 * This is needed for the rare case where GCC
1358 * does something dumb like:
1360 * lea 0x8(%rsp), %rcx
1364 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1365 state
->vals
[op
->dest
.reg
].offset
= \
1366 -state
->stack_size
+ op
->src
.offset
;
1371 if (state
->drap
&& op
->dest
.reg
== CFI_SP
&&
1372 op
->src
.reg
== state
->drap_reg
) {
1374 /* drap: lea disp(%drap), %rsp */
1376 cfa
->offset
= state
->stack_size
= -op
->src
.offset
;
1377 state
->drap_reg
= CFI_UNDEFINED
;
1378 state
->drap
= false;
1382 if (op
->dest
.reg
== state
->cfa
.base
) {
1383 WARN_FUNC("unsupported stack register modification",
1384 insn
->sec
, insn
->offset
);
1391 if (op
->dest
.reg
!= CFI_SP
||
1392 (state
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
1393 (state
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
1394 WARN_FUNC("unsupported stack pointer realignment",
1395 insn
->sec
, insn
->offset
);
1399 if (state
->drap_reg
!= CFI_UNDEFINED
) {
1400 /* drap: and imm, %rsp */
1401 cfa
->base
= state
->drap_reg
;
1402 cfa
->offset
= state
->stack_size
= 0;
1407 * Older versions of GCC (4.8ish) realign the stack
1408 * without DRAP, with a frame pointer.
1414 if (!state
->drap
&& op
->dest
.type
== OP_DEST_REG
&&
1415 op
->dest
.reg
== cfa
->base
) {
1421 if (state
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
1422 op
->dest
.type
== OP_DEST_REG
&&
1423 op
->dest
.reg
== state
->drap_reg
&&
1424 state
->drap_offset
== -state
->stack_size
) {
1426 /* drap: pop %drap */
1427 cfa
->base
= state
->drap_reg
;
1429 state
->drap_offset
= -1;
1431 } else if (regs
[op
->dest
.reg
].offset
== -state
->stack_size
) {
1434 restore_reg(state
, op
->dest
.reg
);
1437 state
->stack_size
-= 8;
1438 if (cfa
->base
== CFI_SP
)
1443 case OP_SRC_REG_INDIRECT
:
1444 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1445 op
->src
.offset
== state
->drap_offset
) {
1447 /* drap: mov disp(%rbp), %drap */
1448 cfa
->base
= state
->drap_reg
;
1450 state
->drap_offset
= -1;
1453 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1454 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
1456 /* drap: mov disp(%rbp), %reg */
1457 restore_reg(state
, op
->dest
.reg
);
1459 } else if (op
->src
.reg
== cfa
->base
&&
1460 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
1462 /* mov disp(%rbp), %reg */
1463 /* mov disp(%rsp), %reg */
1464 restore_reg(state
, op
->dest
.reg
);
1470 WARN_FUNC("unknown stack-related instruction",
1471 insn
->sec
, insn
->offset
);
1478 state
->stack_size
+= 8;
1479 if (cfa
->base
== CFI_SP
)
1482 if (op
->src
.type
!= OP_SRC_REG
)
1486 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1488 /* drap: push %drap */
1489 cfa
->base
= CFI_BP_INDIRECT
;
1490 cfa
->offset
= -state
->stack_size
;
1492 /* save drap so we know when to restore it */
1493 state
->drap_offset
= -state
->stack_size
;
1495 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== state
->drap_reg
) {
1497 /* drap: push %rbp */
1498 state
->stack_size
= 0;
1500 } else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1502 /* drap: push %reg */
1503 save_reg(state
, op
->src
.reg
, CFI_BP
, -state
->stack_size
);
1509 save_reg(state
, op
->src
.reg
, CFI_CFA
, -state
->stack_size
);
1512 /* detect when asm code uses rbp as a scratch register */
1513 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
1514 cfa
->base
!= CFI_BP
)
1515 state
->bp_scratch
= true;
1518 case OP_DEST_REG_INDIRECT
:
1521 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1523 /* drap: mov %drap, disp(%rbp) */
1524 cfa
->base
= CFI_BP_INDIRECT
;
1525 cfa
->offset
= op
->dest
.offset
;
1527 /* save drap offset so we know when to restore it */
1528 state
->drap_offset
= op
->dest
.offset
;
1531 else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1533 /* drap: mov reg, disp(%rbp) */
1534 save_reg(state
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
1537 } else if (op
->dest
.reg
== cfa
->base
) {
1539 /* mov reg, disp(%rbp) */
1540 /* mov reg, disp(%rsp) */
1541 save_reg(state
, op
->src
.reg
, CFI_CFA
,
1542 op
->dest
.offset
- state
->cfa
.offset
);
1548 if ((!state
->drap
&& cfa
->base
!= CFI_BP
) ||
1549 (state
->drap
&& cfa
->base
!= state
->drap_reg
)) {
1550 WARN_FUNC("leave instruction with modified stack frame",
1551 insn
->sec
, insn
->offset
);
1555 /* leave (mov %rbp, %rsp; pop %rbp) */
1557 state
->stack_size
= -state
->regs
[CFI_BP
].offset
- 8;
1558 restore_reg(state
, CFI_BP
);
1568 if (op
->src
.type
!= OP_SRC_POP
) {
1569 WARN_FUNC("unknown stack-related memory operation",
1570 insn
->sec
, insn
->offset
);
1575 state
->stack_size
-= 8;
1576 if (cfa
->base
== CFI_SP
)
1582 WARN_FUNC("unknown stack-related instruction",
1583 insn
->sec
, insn
->offset
);
1590 static bool insn_state_match(struct instruction
*insn
, struct insn_state
*state
)
1592 struct insn_state
*state1
= &insn
->state
, *state2
= state
;
1595 if (memcmp(&state1
->cfa
, &state2
->cfa
, sizeof(state1
->cfa
))) {
1596 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1597 insn
->sec
, insn
->offset
,
1598 state1
->cfa
.base
, state1
->cfa
.offset
,
1599 state2
->cfa
.base
, state2
->cfa
.offset
);
1601 } else if (memcmp(&state1
->regs
, &state2
->regs
, sizeof(state1
->regs
))) {
1602 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1603 if (!memcmp(&state1
->regs
[i
], &state2
->regs
[i
],
1604 sizeof(struct cfi_reg
)))
1607 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1608 insn
->sec
, insn
->offset
,
1609 i
, state1
->regs
[i
].base
, state1
->regs
[i
].offset
,
1610 i
, state2
->regs
[i
].base
, state2
->regs
[i
].offset
);
1614 } else if (state1
->type
!= state2
->type
) {
1615 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1616 insn
->sec
, insn
->offset
, state1
->type
, state2
->type
);
1618 } else if (state1
->drap
!= state2
->drap
||
1619 (state1
->drap
&& state1
->drap_reg
!= state2
->drap_reg
) ||
1620 (state1
->drap
&& state1
->drap_offset
!= state2
->drap_offset
)) {
1621 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1622 insn
->sec
, insn
->offset
,
1623 state1
->drap
, state1
->drap_reg
, state1
->drap_offset
,
1624 state2
->drap
, state2
->drap_reg
, state2
->drap_offset
);
1633 * Follow the branch starting at the given instruction, and recursively follow
1634 * any other branches (jumps). Meanwhile, track the frame pointer state at
1635 * each instruction and validate all the rules described in
1636 * tools/objtool/Documentation/stack-validation.txt.
1638 static int validate_branch(struct objtool_file
*file
, struct instruction
*first
,
1639 struct insn_state state
)
1641 struct alternative
*alt
;
1642 struct instruction
*insn
, *next_insn
;
1643 struct section
*sec
;
1644 struct symbol
*func
= NULL
;
1650 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
1651 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1657 next_insn
= next_insn_same_sec(file
, insn
);
1660 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
) {
1661 WARN("%s() falls through to next function %s()",
1662 func
->name
, insn
->func
->name
);
1669 if (func
&& insn
->ignore
) {
1670 WARN_FUNC("BUG: why am I validating an ignored function?",
1675 if (insn
->visited
) {
1676 if (!insn
->hint
&& !insn_state_match(insn
, &state
))
1683 if (insn
->restore
) {
1684 struct instruction
*save_insn
, *i
;
1688 func_for_each_insn_continue_reverse(file
, func
, i
) {
1696 WARN_FUNC("no corresponding CFI save for CFI restore",
1701 if (!save_insn
->visited
) {
1703 * Oops, no state to copy yet.
1704 * Hopefully we can reach this
1705 * instruction from another branch
1706 * after the save insn has been
1712 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1717 insn
->state
= save_insn
->state
;
1720 state
= insn
->state
;
1723 insn
->state
= state
;
1725 insn
->visited
= true;
1727 if (!insn
->ignore_alts
) {
1728 list_for_each_entry(alt
, &insn
->alts
, list
) {
1729 ret
= validate_branch(file
, alt
->insn
, state
);
1735 switch (insn
->type
) {
1738 if (func
&& has_modified_stack_frame(&state
)) {
1739 WARN_FUNC("return with modified stack frame",
1744 if (state
.bp_scratch
) {
1745 WARN("%s uses BP as a scratch register",
1753 if (is_fentry_call(insn
))
1756 ret
= dead_end_function(file
, insn
->call_dest
);
1763 case INSN_CALL_DYNAMIC
:
1764 if (!no_fp
&& func
&& !has_valid_stack_frame(&state
)) {
1765 WARN_FUNC("call without frame pointer save/setup",
1771 case INSN_JUMP_CONDITIONAL
:
1772 case INSN_JUMP_UNCONDITIONAL
:
1773 if (insn
->jump_dest
&&
1774 (!func
|| !insn
->jump_dest
->func
||
1775 func
== insn
->jump_dest
->func
)) {
1776 ret
= validate_branch(file
, insn
->jump_dest
,
1781 } else if (func
&& has_modified_stack_frame(&state
)) {
1782 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1787 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
1792 case INSN_JUMP_DYNAMIC
:
1793 if (func
&& list_empty(&insn
->alts
) &&
1794 has_modified_stack_frame(&state
)) {
1795 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1802 case INSN_CONTEXT_SWITCH
:
1803 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
1804 WARN_FUNC("unsupported instruction in callable function",
1811 if (update_insn_state(insn
, &state
))
1824 if (state
.cfa
.base
== CFI_UNDEFINED
)
1826 WARN("%s: unexpected end of section", sec
->name
);
1836 static int validate_unwind_hints(struct objtool_file
*file
)
1838 struct instruction
*insn
;
1839 int ret
, warnings
= 0;
1840 struct insn_state state
;
1845 clear_insn_state(&state
);
1847 for_each_insn(file
, insn
) {
1848 if (insn
->hint
&& !insn
->visited
) {
1849 ret
= validate_branch(file
, insn
, state
);
1857 static bool is_kasan_insn(struct instruction
*insn
)
1859 return (insn
->type
== INSN_CALL
&&
1860 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1863 static bool is_ubsan_insn(struct instruction
*insn
)
1865 return (insn
->type
== INSN_CALL
&&
1866 !strcmp(insn
->call_dest
->name
,
1867 "__ubsan_handle_builtin_unreachable"));
1870 static bool ignore_unreachable_insn(struct instruction
*insn
)
1874 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
1878 * Ignore any unused exceptions. This can happen when a whitelisted
1879 * function has an exception table entry.
1881 * Also ignore alternative replacement instructions. This can happen
1882 * when a whitelisted function uses one of the ALTERNATIVE macros.
1884 if (!strcmp(insn
->sec
->name
, ".fixup") ||
1885 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
1886 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
1890 * Check if this (or a subsequent) instruction is related to
1891 * CONFIG_UBSAN or CONFIG_KASAN.
1893 * End the search at 5 instructions to avoid going into the weeds.
1897 for (i
= 0; i
< 5; i
++) {
1899 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
1902 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
) {
1903 insn
= insn
->jump_dest
;
1907 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
1909 insn
= list_next_entry(insn
, list
);
1915 static int validate_functions(struct objtool_file
*file
)
1917 struct section
*sec
;
1918 struct symbol
*func
;
1919 struct instruction
*insn
;
1920 struct insn_state state
;
1921 int ret
, warnings
= 0;
1923 clear_insn_state(&state
);
1925 state
.cfa
= initial_func_cfi
.cfa
;
1926 memcpy(&state
.regs
, &initial_func_cfi
.regs
,
1927 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
1928 state
.stack_size
= initial_func_cfi
.cfa
.offset
;
1930 for_each_sec(file
, sec
) {
1931 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1932 if (func
->type
!= STT_FUNC
)
1935 insn
= find_insn(file
, sec
, func
->offset
);
1936 if (!insn
|| insn
->ignore
)
1939 ret
= validate_branch(file
, insn
, state
);
1947 static int validate_reachable_instructions(struct objtool_file
*file
)
1949 struct instruction
*insn
;
1951 if (file
->ignore_unreachables
)
1954 for_each_insn(file
, insn
) {
1955 if (insn
->visited
|| ignore_unreachable_insn(insn
))
1958 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
1965 static void cleanup(struct objtool_file
*file
)
1967 struct instruction
*insn
, *tmpinsn
;
1968 struct alternative
*alt
, *tmpalt
;
1970 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
1971 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
1972 list_del(&alt
->list
);
1975 list_del(&insn
->list
);
1976 hash_del(&insn
->hash
);
1979 elf_close(file
->elf
);
1982 int check(const char *_objname
, bool _no_fp
, bool no_unreachable
, bool orc
)
1984 struct objtool_file file
;
1985 int ret
, warnings
= 0;
1990 file
.elf
= elf_open(objname
, orc
? O_RDWR
: O_RDONLY
);
1994 INIT_LIST_HEAD(&file
.insn_list
);
1995 hash_init(file
.insn_hash
);
1996 file
.whitelist
= find_section_by_name(file
.elf
, ".discard.func_stack_frame_non_standard");
1997 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
1998 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
1999 file
.ignore_unreachables
= no_unreachable
;
2002 arch_initial_func_cfi_state(&initial_func_cfi
);
2004 ret
= decode_sections(&file
);
2009 if (list_empty(&file
.insn_list
))
2012 ret
= validate_functions(&file
);
2017 ret
= validate_unwind_hints(&file
);
2023 ret
= validate_reachable_instructions(&file
);
2030 ret
= create_orc(&file
);
2034 ret
= create_orc_sections(&file
);
2038 ret
= elf_write(file
.elf
);
2046 /* ignore warnings for now until we get all the code cleaned up */
2047 if (ret
|| warnings
)