2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <linux/hashtable.h>
29 #include <linux/kernel.h>
32 struct list_head list
;
33 struct instruction
*insn
;
37 struct cfi_state initial_func_cfi
;
39 struct instruction
*find_insn(struct objtool_file
*file
,
40 struct section
*sec
, unsigned long offset
)
42 struct instruction
*insn
;
44 hash_for_each_possible(file
->insn_hash
, insn
, hash
, offset
)
45 if (insn
->sec
== sec
&& insn
->offset
== offset
)
51 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
52 struct instruction
*insn
)
54 struct instruction
*next
= list_next_entry(insn
, list
);
56 if (!next
|| &next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
62 static struct instruction
*next_insn_same_func(struct objtool_file
*file
,
63 struct instruction
*insn
)
65 struct instruction
*next
= list_next_entry(insn
, list
);
66 struct symbol
*func
= insn
->func
;
71 if (&next
->list
!= &file
->insn_list
&& next
->func
== func
)
74 /* Check if we're already in the subfunction: */
75 if (func
== func
->cfunc
)
78 /* Move to the subfunction: */
79 return find_insn(file
, func
->cfunc
->sec
, func
->cfunc
->offset
);
82 #define func_for_each_insn_all(file, func, insn) \
83 for (insn = find_insn(file, func->sec, func->offset); \
85 insn = next_insn_same_func(file, insn))
87 #define func_for_each_insn(file, func, insn) \
88 for (insn = find_insn(file, func->sec, func->offset); \
89 insn && &insn->list != &file->insn_list && \
90 insn->sec == func->sec && \
91 insn->offset < func->offset + func->len; \
92 insn = list_next_entry(insn, list))
94 #define func_for_each_insn_continue_reverse(file, func, insn) \
95 for (insn = list_prev_entry(insn, list); \
96 &insn->list != &file->insn_list && \
97 insn->sec == func->sec && insn->offset >= func->offset; \
98 insn = list_prev_entry(insn, list))
100 #define sec_for_each_insn_from(file, insn) \
101 for (; insn; insn = next_insn_same_sec(file, insn))
103 #define sec_for_each_insn_continue(file, insn) \
104 for (insn = next_insn_same_sec(file, insn); insn; \
105 insn = next_insn_same_sec(file, insn))
108 * Check if the function has been manually whitelisted with the
109 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
110 * due to its use of a context switching instruction.
112 static bool ignore_func(struct objtool_file
*file
, struct symbol
*func
)
116 /* check for STACK_FRAME_NON_STANDARD */
117 if (file
->whitelist
&& file
->whitelist
->rela
)
118 list_for_each_entry(rela
, &file
->whitelist
->rela
->rela_list
, list
) {
119 if (rela
->sym
->type
== STT_SECTION
&&
120 rela
->sym
->sec
== func
->sec
&&
121 rela
->addend
== func
->offset
)
123 if (rela
->sym
->type
== STT_FUNC
&& rela
->sym
== func
)
131 * This checks to see if the given function is a "noreturn" function.
133 * For global functions which are outside the scope of this object file, we
134 * have to keep a manual list of them.
136 * For local functions, we have to detect them manually by simply looking for
137 * the lack of a return instruction.
144 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
148 struct instruction
*insn
;
152 * Unfortunately these have to be hard coded because the noreturn
153 * attribute isn't provided in ELF data.
155 static const char * const global_noreturns
[] = {
160 "__module_put_and_exit",
162 "kvm_spurious_fault",
168 if (func
->bind
== STB_WEAK
)
171 if (func
->bind
== STB_GLOBAL
)
172 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
173 if (!strcmp(func
->name
, global_noreturns
[i
]))
179 insn
= find_insn(file
, func
->sec
, func
->offset
);
183 func_for_each_insn_all(file
, func
, insn
) {
186 if (insn
->type
== INSN_RETURN
)
194 * A function can have a sibling call instead of a return. In that
195 * case, the function's dead-end status depends on whether the target
196 * of the sibling call returns.
198 func_for_each_insn_all(file
, func
, insn
) {
199 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
200 struct instruction
*dest
= insn
->jump_dest
;
203 /* sibling call to another file */
206 if (dest
->func
&& dest
->func
->pfunc
!= insn
->func
->pfunc
) {
208 /* local sibling call */
209 if (recursion
== 5) {
211 * Infinite recursion: two functions
212 * have sibling calls to each other.
213 * This is a very rare case. It means
214 * they aren't dead ends.
219 return __dead_end_function(file
, dest
->func
,
224 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
232 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
234 return __dead_end_function(file
, func
, 0);
237 static void clear_insn_state(struct insn_state
*state
)
241 memset(state
, 0, sizeof(*state
));
242 state
->cfa
.base
= CFI_UNDEFINED
;
243 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
244 state
->regs
[i
].base
= CFI_UNDEFINED
;
245 state
->vals
[i
].base
= CFI_UNDEFINED
;
247 state
->drap_reg
= CFI_UNDEFINED
;
248 state
->drap_offset
= -1;
252 * Call the arch-specific instruction decoder for all the instructions and add
253 * them to the global instruction list.
255 static int decode_instructions(struct objtool_file
*file
)
259 unsigned long offset
;
260 struct instruction
*insn
;
263 for_each_sec(file
, sec
) {
265 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
268 if (strcmp(sec
->name
, ".altinstr_replacement") &&
269 strcmp(sec
->name
, ".altinstr_aux") &&
270 strncmp(sec
->name
, ".discard.", 9))
273 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
274 insn
= malloc(sizeof(*insn
));
276 WARN("malloc failed");
279 memset(insn
, 0, sizeof(*insn
));
280 INIT_LIST_HEAD(&insn
->alts
);
281 clear_insn_state(&insn
->state
);
284 insn
->offset
= offset
;
286 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
288 &insn
->len
, &insn
->type
,
294 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
295 WARN_FUNC("invalid instruction type %d",
296 insn
->sec
, insn
->offset
, insn
->type
);
301 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
302 list_add_tail(&insn
->list
, &file
->insn_list
);
305 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
306 if (func
->type
!= STT_FUNC
)
309 if (!find_insn(file
, sec
, func
->offset
)) {
310 WARN("%s(): can't find starting instruction",
315 func_for_each_insn(file
, func
, insn
)
329 * Mark "ud2" instructions and manually annotated dead ends.
331 static int add_dead_ends(struct objtool_file
*file
)
335 struct instruction
*insn
;
339 * By default, "ud2" is a dead end unless otherwise annotated, because
340 * GCC 7 inserts it for certain divide-by-zero cases.
342 for_each_insn(file
, insn
)
343 if (insn
->type
== INSN_BUG
)
344 insn
->dead_end
= true;
347 * Check for manually annotated dead ends.
349 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
353 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
354 if (rela
->sym
->type
!= STT_SECTION
) {
355 WARN("unexpected relocation symbol type in %s", sec
->name
);
358 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
360 insn
= list_prev_entry(insn
, list
);
361 else if (rela
->addend
== rela
->sym
->sec
->len
) {
363 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
364 if (insn
->sec
== rela
->sym
->sec
) {
371 WARN("can't find unreachable insn at %s+0x%x",
372 rela
->sym
->sec
->name
, rela
->addend
);
376 WARN("can't find unreachable insn at %s+0x%x",
377 rela
->sym
->sec
->name
, rela
->addend
);
381 insn
->dead_end
= true;
386 * These manually annotated reachable checks are needed for GCC 4.4,
387 * where the Linux unreachable() macro isn't supported. In that case
388 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
391 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
395 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
396 if (rela
->sym
->type
!= STT_SECTION
) {
397 WARN("unexpected relocation symbol type in %s", sec
->name
);
400 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
402 insn
= list_prev_entry(insn
, list
);
403 else if (rela
->addend
== rela
->sym
->sec
->len
) {
405 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
406 if (insn
->sec
== rela
->sym
->sec
) {
413 WARN("can't find reachable insn at %s+0x%x",
414 rela
->sym
->sec
->name
, rela
->addend
);
418 WARN("can't find reachable insn at %s+0x%x",
419 rela
->sym
->sec
->name
, rela
->addend
);
423 insn
->dead_end
= false;
430 * Warnings shouldn't be reported for ignored functions.
432 static void add_ignores(struct objtool_file
*file
)
434 struct instruction
*insn
;
438 for_each_sec(file
, sec
) {
439 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
440 if (func
->type
!= STT_FUNC
)
443 if (!ignore_func(file
, func
))
446 func_for_each_insn_all(file
, func
, insn
)
453 * FIXME: For now, just ignore any alternatives which add retpolines. This is
454 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
455 * But it at least allows objtool to understand the control flow *around* the
458 static int add_nospec_ignores(struct objtool_file
*file
)
462 struct instruction
*insn
;
464 sec
= find_section_by_name(file
->elf
, ".rela.discard.nospec");
468 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
469 if (rela
->sym
->type
!= STT_SECTION
) {
470 WARN("unexpected relocation symbol type in %s", sec
->name
);
474 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
476 WARN("bad .discard.nospec entry");
480 insn
->ignore_alts
= true;
487 * Find the destination instructions for all jumps.
489 static int add_jump_destinations(struct objtool_file
*file
)
491 struct instruction
*insn
;
493 struct section
*dest_sec
;
494 unsigned long dest_off
;
496 for_each_insn(file
, insn
) {
497 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
498 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
504 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
507 dest_sec
= insn
->sec
;
508 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
509 } else if (rela
->sym
->type
== STT_SECTION
) {
510 dest_sec
= rela
->sym
->sec
;
511 dest_off
= rela
->addend
+ 4;
512 } else if (rela
->sym
->sec
->idx
) {
513 dest_sec
= rela
->sym
->sec
;
514 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
515 } else if (strstr(rela
->sym
->name
, "_indirect_thunk_")) {
517 * Retpoline jumps are really dynamic jumps in
518 * disguise, so convert them accordingly.
520 insn
->type
= INSN_JUMP_DYNAMIC
;
521 insn
->retpoline_safe
= true;
529 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
530 if (!insn
->jump_dest
) {
533 * This is a special case where an alt instruction
534 * jumps past the end of the section. These are
535 * handled later in handle_group_alt().
537 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
540 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
541 insn
->sec
, insn
->offset
, dest_sec
->name
,
551 * Find the destination instructions for all calls.
553 static int add_call_destinations(struct objtool_file
*file
)
555 struct instruction
*insn
;
556 unsigned long dest_off
;
559 for_each_insn(file
, insn
) {
560 if (insn
->type
!= INSN_CALL
)
563 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
566 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
567 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
570 if (!insn
->call_dest
&& !insn
->ignore
) {
571 WARN_FUNC("unsupported intra-function call",
572 insn
->sec
, insn
->offset
);
574 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
578 } else if (rela
->sym
->type
== STT_SECTION
) {
579 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
581 if (!insn
->call_dest
||
582 insn
->call_dest
->type
!= STT_FUNC
) {
583 WARN_FUNC("can't find call dest symbol at %s+0x%x",
584 insn
->sec
, insn
->offset
,
585 rela
->sym
->sec
->name
,
590 insn
->call_dest
= rela
->sym
;
597 * The .alternatives section requires some extra special care, over and above
598 * what other special sections require:
600 * 1. Because alternatives are patched in-place, we need to insert a fake jump
601 * instruction at the end so that validate_branch() skips all the original
602 * replaced instructions when validating the new instruction path.
604 * 2. An added wrinkle is that the new instruction length might be zero. In
605 * that case the old instructions are replaced with noops. We simulate that
606 * by creating a fake jump as the only new instruction.
608 * 3. In some cases, the alternative section includes an instruction which
609 * conditionally jumps to the _end_ of the entry. We have to modify these
610 * jumps' destinations to point back to .text rather than the end of the
611 * entry in .altinstr_replacement.
613 * 4. It has been requested that we don't validate the !POPCNT feature path
614 * which is a "very very small percentage of machines".
616 static int handle_group_alt(struct objtool_file
*file
,
617 struct special_alt
*special_alt
,
618 struct instruction
*orig_insn
,
619 struct instruction
**new_insn
)
621 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
= NULL
;
622 unsigned long dest_off
;
624 last_orig_insn
= NULL
;
626 sec_for_each_insn_from(file
, insn
) {
627 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
630 if (special_alt
->skip_orig
)
631 insn
->type
= INSN_NOP
;
633 insn
->alt_group
= true;
634 last_orig_insn
= insn
;
637 if (next_insn_same_sec(file
, last_orig_insn
)) {
638 fake_jump
= malloc(sizeof(*fake_jump
));
640 WARN("malloc failed");
643 memset(fake_jump
, 0, sizeof(*fake_jump
));
644 INIT_LIST_HEAD(&fake_jump
->alts
);
645 clear_insn_state(&fake_jump
->state
);
647 fake_jump
->sec
= special_alt
->new_sec
;
648 fake_jump
->offset
= -1;
649 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
650 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
651 fake_jump
->ignore
= true;
654 if (!special_alt
->new_len
) {
656 WARN("%s: empty alternative at end of section",
657 special_alt
->orig_sec
->name
);
661 *new_insn
= fake_jump
;
665 last_new_insn
= NULL
;
667 sec_for_each_insn_from(file
, insn
) {
668 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
671 last_new_insn
= insn
;
673 insn
->ignore
= orig_insn
->ignore_alts
;
675 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
676 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
679 if (!insn
->immediate
)
682 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
683 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
) {
685 WARN("%s: alternative jump to end of section",
686 special_alt
->orig_sec
->name
);
689 insn
->jump_dest
= fake_jump
;
692 if (!insn
->jump_dest
) {
693 WARN_FUNC("can't find alternative jump destination",
694 insn
->sec
, insn
->offset
);
699 if (!last_new_insn
) {
700 WARN_FUNC("can't find last new alternative instruction",
701 special_alt
->new_sec
, special_alt
->new_off
);
706 list_add(&fake_jump
->list
, &last_new_insn
->list
);
712 * A jump table entry can either convert a nop to a jump or a jump to a nop.
713 * If the original instruction is a jump, make the alt entry an effective nop
714 * by just skipping the original instruction.
716 static int handle_jump_alt(struct objtool_file
*file
,
717 struct special_alt
*special_alt
,
718 struct instruction
*orig_insn
,
719 struct instruction
**new_insn
)
721 if (orig_insn
->type
== INSN_NOP
)
724 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
725 WARN_FUNC("unsupported instruction at jump label",
726 orig_insn
->sec
, orig_insn
->offset
);
730 *new_insn
= list_next_entry(orig_insn
, list
);
735 * Read all the special sections which have alternate instructions which can be
736 * patched in or redirected to at runtime. Each instruction having alternate
737 * instruction(s) has them added to its insn->alts list, which will be
738 * traversed in validate_branch().
740 static int add_special_section_alts(struct objtool_file
*file
)
742 struct list_head special_alts
;
743 struct instruction
*orig_insn
, *new_insn
;
744 struct special_alt
*special_alt
, *tmp
;
745 struct alternative
*alt
;
748 ret
= special_get_alts(file
->elf
, &special_alts
);
752 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
754 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
755 special_alt
->orig_off
);
757 WARN_FUNC("special: can't find orig instruction",
758 special_alt
->orig_sec
, special_alt
->orig_off
);
764 if (!special_alt
->group
|| special_alt
->new_len
) {
765 new_insn
= find_insn(file
, special_alt
->new_sec
,
766 special_alt
->new_off
);
768 WARN_FUNC("special: can't find new instruction",
769 special_alt
->new_sec
,
770 special_alt
->new_off
);
776 if (special_alt
->group
) {
777 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
781 } else if (special_alt
->jump_or_nop
) {
782 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
788 alt
= malloc(sizeof(*alt
));
790 WARN("malloc failed");
795 alt
->insn
= new_insn
;
796 list_add_tail(&alt
->list
, &orig_insn
->alts
);
798 list_del(&special_alt
->list
);
806 static int add_switch_table(struct objtool_file
*file
, struct instruction
*insn
,
807 struct rela
*table
, struct rela
*next_table
)
809 struct rela
*rela
= table
;
810 struct instruction
*alt_insn
;
811 struct alternative
*alt
;
812 struct symbol
*pfunc
= insn
->func
->pfunc
;
813 unsigned int prev_offset
= 0;
815 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
816 if (rela
== next_table
)
819 /* Make sure the switch table entries are consecutive: */
820 if (prev_offset
&& rela
->offset
!= prev_offset
+ 8)
823 /* Detect function pointers from contiguous objects: */
824 if (rela
->sym
->sec
== pfunc
->sec
&&
825 rela
->addend
== pfunc
->offset
)
828 alt_insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
832 /* Make sure the jmp dest is in the function or subfunction: */
833 if (alt_insn
->func
->pfunc
!= pfunc
)
836 alt
= malloc(sizeof(*alt
));
838 WARN("malloc failed");
842 alt
->insn
= alt_insn
;
843 list_add_tail(&alt
->list
, &insn
->alts
);
844 prev_offset
= rela
->offset
;
848 WARN_FUNC("can't find switch jump table",
849 insn
->sec
, insn
->offset
);
857 * find_switch_table() - Given a dynamic jump, find the switch jump table in
858 * .rodata associated with it.
860 * There are 3 basic patterns:
862 * 1. jmpq *[rodata addr](,%reg,8)
864 * This is the most common case by far. It jumps to an address in a simple
865 * jump table which is stored in .rodata.
867 * 2. jmpq *[rodata addr](%rip)
869 * This is caused by a rare GCC quirk, currently only seen in three driver
870 * functions in the kernel, only with certain obscure non-distro configs.
872 * As part of an optimization, GCC makes a copy of an existing switch jump
873 * table, modifies it, and then hard-codes the jump (albeit with an indirect
874 * jump) to use a single entry in the table. The rest of the jump table and
875 * some of its jump targets remain as dead code.
877 * In such a case we can just crudely ignore all unreachable instruction
878 * warnings for the entire object file. Ideally we would just ignore them
879 * for the function, but that would require redesigning the code quite a
880 * bit. And honestly that's just not worth doing: unreachable instruction
881 * warnings are of questionable value anyway, and this is such a rare issue.
883 * 3. mov [rodata addr],%reg1
884 * ... some instructions ...
885 * jmpq *(%reg1,%reg2,8)
887 * This is a fairly uncommon pattern which is new for GCC 6. As of this
888 * writing, there are 11 occurrences of it in the allmodconfig kernel.
890 * As of GCC 7 there are quite a few more of these and the 'in between' code
891 * is significant. Esp. with KASAN enabled some of the code between the mov
892 * and jmpq uses .rodata itself, which can confuse things.
894 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
895 * ensure the same register is used in the mov and jump instructions.
897 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
899 static struct rela
*find_switch_table(struct objtool_file
*file
,
901 struct instruction
*insn
)
903 struct rela
*text_rela
, *rodata_rela
;
904 struct instruction
*orig_insn
= insn
;
905 unsigned long table_offset
;
908 * Backward search using the @first_jump_src links, these help avoid
909 * much of the 'in between' code. Which avoids us getting confused by
913 &insn
->list
!= &file
->insn_list
&&
914 insn
->sec
== func
->sec
&&
915 insn
->offset
>= func
->offset
;
917 insn
= insn
->first_jump_src
?: list_prev_entry(insn
, list
)) {
919 if (insn
!= orig_insn
&& insn
->type
== INSN_JUMP_DYNAMIC
)
922 /* allow small jumps within the range */
923 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
925 (insn
->jump_dest
->offset
<= insn
->offset
||
926 insn
->jump_dest
->offset
> orig_insn
->offset
))
929 /* look for a relocation which references .rodata */
930 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
932 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
935 table_offset
= text_rela
->addend
;
936 if (text_rela
->type
== R_X86_64_PC32
)
940 * Make sure the .rodata address isn't associated with a
941 * symbol. gcc jump tables are anonymous data.
943 if (find_symbol_containing(file
->rodata
, table_offset
))
946 rodata_rela
= find_rela_by_dest(file
->rodata
, table_offset
);
949 * Use of RIP-relative switch jumps is quite rare, and
950 * indicates a rare GCC quirk/bug which can leave dead
953 if (text_rela
->type
== R_X86_64_PC32
)
954 file
->ignore_unreachables
= true;
964 static int add_func_switch_tables(struct objtool_file
*file
,
967 struct instruction
*insn
, *last
= NULL
, *prev_jump
= NULL
;
968 struct rela
*rela
, *prev_rela
= NULL
;
971 func_for_each_insn_all(file
, func
, insn
) {
976 * Store back-pointers for unconditional forward jumps such
977 * that find_switch_table() can back-track using those and
978 * avoid some potentially confusing code.
980 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
&&
981 insn
->offset
> last
->offset
&&
982 insn
->jump_dest
->offset
> insn
->offset
&&
983 !insn
->jump_dest
->first_jump_src
) {
985 insn
->jump_dest
->first_jump_src
= insn
;
986 last
= insn
->jump_dest
;
989 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
992 rela
= find_switch_table(file
, func
, insn
);
997 * We found a switch table, but we don't know yet how big it
998 * is. Don't add it until we reach the end of the function or
999 * the beginning of another switch table in the same function.
1002 ret
= add_switch_table(file
, prev_jump
, prev_rela
, rela
);
1012 ret
= add_switch_table(file
, prev_jump
, prev_rela
, NULL
);
1021 * For some switch statements, gcc generates a jump table in the .rodata
1022 * section which contains a list of addresses within the function to jump to.
1023 * This finds these jump tables and adds them to the insn->alts lists.
1025 static int add_switch_table_alts(struct objtool_file
*file
)
1027 struct section
*sec
;
1028 struct symbol
*func
;
1031 if (!file
->rodata
|| !file
->rodata
->rela
)
1034 for_each_sec(file
, sec
) {
1035 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1036 if (func
->type
!= STT_FUNC
)
1039 ret
= add_func_switch_tables(file
, func
);
1048 static int read_unwind_hints(struct objtool_file
*file
)
1050 struct section
*sec
, *relasec
;
1052 struct unwind_hint
*hint
;
1053 struct instruction
*insn
;
1054 struct cfi_reg
*cfa
;
1057 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
1061 relasec
= sec
->rela
;
1063 WARN("missing .rela.discard.unwind_hints section");
1067 if (sec
->len
% sizeof(struct unwind_hint
)) {
1068 WARN("struct unwind_hint size mismatch");
1074 for (i
= 0; i
< sec
->len
/ sizeof(struct unwind_hint
); i
++) {
1075 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1077 rela
= find_rela_by_dest(sec
, i
* sizeof(*hint
));
1079 WARN("can't find rela for unwind_hints[%d]", i
);
1083 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
1085 WARN("can't find insn for unwind_hints[%d]", i
);
1089 cfa
= &insn
->state
.cfa
;
1091 if (hint
->type
== UNWIND_HINT_TYPE_SAVE
) {
1095 } else if (hint
->type
== UNWIND_HINT_TYPE_RESTORE
) {
1096 insn
->restore
= true;
1103 switch (hint
->sp_reg
) {
1104 case ORC_REG_UNDEFINED
:
1105 cfa
->base
= CFI_UNDEFINED
;
1113 case ORC_REG_SP_INDIRECT
:
1114 cfa
->base
= CFI_SP_INDIRECT
;
1117 cfa
->base
= CFI_R10
;
1120 cfa
->base
= CFI_R13
;
1129 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1130 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1134 cfa
->offset
= hint
->sp_offset
;
1135 insn
->state
.type
= hint
->type
;
1141 static int read_retpoline_hints(struct objtool_file
*file
)
1143 struct section
*sec
;
1144 struct instruction
*insn
;
1147 sec
= find_section_by_name(file
->elf
, ".rela.discard.retpoline_safe");
1151 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
1152 if (rela
->sym
->type
!= STT_SECTION
) {
1153 WARN("unexpected relocation symbol type in %s", sec
->name
);
1157 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
1159 WARN("bad .discard.retpoline_safe entry");
1163 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1164 insn
->type
!= INSN_CALL_DYNAMIC
) {
1165 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1166 insn
->sec
, insn
->offset
);
1170 insn
->retpoline_safe
= true;
1176 static int decode_sections(struct objtool_file
*file
)
1180 ret
= decode_instructions(file
);
1184 ret
= add_dead_ends(file
);
1190 ret
= add_nospec_ignores(file
);
1194 ret
= add_jump_destinations(file
);
1198 ret
= add_special_section_alts(file
);
1202 ret
= add_call_destinations(file
);
1206 ret
= add_switch_table_alts(file
);
1210 ret
= read_unwind_hints(file
);
1214 ret
= read_retpoline_hints(file
);
1221 static bool is_fentry_call(struct instruction
*insn
)
1223 if (insn
->type
== INSN_CALL
&&
1224 insn
->call_dest
->type
== STT_NOTYPE
&&
1225 !strcmp(insn
->call_dest
->name
, "__fentry__"))
1231 static bool has_modified_stack_frame(struct insn_state
*state
)
1235 if (state
->cfa
.base
!= initial_func_cfi
.cfa
.base
||
1236 state
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
||
1237 state
->stack_size
!= initial_func_cfi
.cfa
.offset
||
1241 for (i
= 0; i
< CFI_NUM_REGS
; i
++)
1242 if (state
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1243 state
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1249 static bool has_valid_stack_frame(struct insn_state
*state
)
1251 if (state
->cfa
.base
== CFI_BP
&& state
->regs
[CFI_BP
].base
== CFI_CFA
&&
1252 state
->regs
[CFI_BP
].offset
== -16)
1255 if (state
->drap
&& state
->regs
[CFI_BP
].base
== CFI_BP
)
1261 static int update_insn_state_regs(struct instruction
*insn
, struct insn_state
*state
)
1263 struct cfi_reg
*cfa
= &state
->cfa
;
1264 struct stack_op
*op
= &insn
->stack_op
;
1266 if (cfa
->base
!= CFI_SP
)
1270 if (op
->dest
.type
== OP_DEST_PUSH
)
1274 if (op
->src
.type
== OP_SRC_POP
)
1277 /* add immediate to sp */
1278 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
1279 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
1280 cfa
->offset
-= op
->src
.offset
;
1285 static void save_reg(struct insn_state
*state
, unsigned char reg
, int base
,
1288 if (arch_callee_saved_reg(reg
) &&
1289 state
->regs
[reg
].base
== CFI_UNDEFINED
) {
1290 state
->regs
[reg
].base
= base
;
1291 state
->regs
[reg
].offset
= offset
;
1295 static void restore_reg(struct insn_state
*state
, unsigned char reg
)
1297 state
->regs
[reg
].base
= CFI_UNDEFINED
;
1298 state
->regs
[reg
].offset
= 0;
1302 * A note about DRAP stack alignment:
1304 * GCC has the concept of a DRAP register, which is used to help keep track of
1305 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1306 * register. The typical DRAP pattern is:
1308 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1309 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1310 * 41 ff 72 f8 pushq -0x8(%r10)
1312 * 48 89 e5 mov %rsp,%rbp
1319 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1322 * There are some variations in the epilogues, like:
1330 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1335 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1336 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1337 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1338 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1340 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1343 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1344 * restored beforehand:
1347 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1348 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1350 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1354 static int update_insn_state(struct instruction
*insn
, struct insn_state
*state
)
1356 struct stack_op
*op
= &insn
->stack_op
;
1357 struct cfi_reg
*cfa
= &state
->cfa
;
1358 struct cfi_reg
*regs
= state
->regs
;
1360 /* stack operations don't make sense with an undefined CFA */
1361 if (cfa
->base
== CFI_UNDEFINED
) {
1363 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1369 if (state
->type
== ORC_TYPE_REGS
|| state
->type
== ORC_TYPE_REGS_IRET
)
1370 return update_insn_state_regs(insn
, state
);
1372 switch (op
->dest
.type
) {
1375 switch (op
->src
.type
) {
1378 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
1379 cfa
->base
== CFI_SP
&&
1380 regs
[CFI_BP
].base
== CFI_CFA
&&
1381 regs
[CFI_BP
].offset
== -cfa
->offset
) {
1383 /* mov %rsp, %rbp */
1384 cfa
->base
= op
->dest
.reg
;
1385 state
->bp_scratch
= false;
1388 else if (op
->src
.reg
== CFI_SP
&&
1389 op
->dest
.reg
== CFI_BP
&& state
->drap
) {
1391 /* drap: mov %rsp, %rbp */
1392 regs
[CFI_BP
].base
= CFI_BP
;
1393 regs
[CFI_BP
].offset
= -state
->stack_size
;
1394 state
->bp_scratch
= false;
1397 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1402 * This is needed for the rare case where GCC
1409 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1410 state
->vals
[op
->dest
.reg
].offset
= -state
->stack_size
;
1413 else if (op
->dest
.reg
== cfa
->base
) {
1415 /* mov %reg, %rsp */
1416 if (cfa
->base
== CFI_SP
&&
1417 state
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
1420 * This is needed for the rare case
1421 * where GCC does something dumb like:
1423 * lea 0x8(%rsp), %rcx
1427 cfa
->offset
= -state
->vals
[op
->src
.reg
].offset
;
1428 state
->stack_size
= cfa
->offset
;
1431 cfa
->base
= CFI_UNDEFINED
;
1439 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
1442 state
->stack_size
-= op
->src
.offset
;
1443 if (cfa
->base
== CFI_SP
)
1444 cfa
->offset
-= op
->src
.offset
;
1448 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
1450 /* lea disp(%rbp), %rsp */
1451 state
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
1455 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1457 /* drap: lea disp(%rsp), %drap */
1458 state
->drap_reg
= op
->dest
.reg
;
1461 * lea disp(%rsp), %reg
1463 * This is needed for the rare case where GCC
1464 * does something dumb like:
1466 * lea 0x8(%rsp), %rcx
1470 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1471 state
->vals
[op
->dest
.reg
].offset
= \
1472 -state
->stack_size
+ op
->src
.offset
;
1477 if (state
->drap
&& op
->dest
.reg
== CFI_SP
&&
1478 op
->src
.reg
== state
->drap_reg
) {
1480 /* drap: lea disp(%drap), %rsp */
1482 cfa
->offset
= state
->stack_size
= -op
->src
.offset
;
1483 state
->drap_reg
= CFI_UNDEFINED
;
1484 state
->drap
= false;
1488 if (op
->dest
.reg
== state
->cfa
.base
) {
1489 WARN_FUNC("unsupported stack register modification",
1490 insn
->sec
, insn
->offset
);
1497 if (op
->dest
.reg
!= CFI_SP
||
1498 (state
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
1499 (state
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
1500 WARN_FUNC("unsupported stack pointer realignment",
1501 insn
->sec
, insn
->offset
);
1505 if (state
->drap_reg
!= CFI_UNDEFINED
) {
1506 /* drap: and imm, %rsp */
1507 cfa
->base
= state
->drap_reg
;
1508 cfa
->offset
= state
->stack_size
= 0;
1513 * Older versions of GCC (4.8ish) realign the stack
1514 * without DRAP, with a frame pointer.
1520 if (!state
->drap
&& op
->dest
.type
== OP_DEST_REG
&&
1521 op
->dest
.reg
== cfa
->base
) {
1527 if (state
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
1528 op
->dest
.type
== OP_DEST_REG
&&
1529 op
->dest
.reg
== state
->drap_reg
&&
1530 state
->drap_offset
== -state
->stack_size
) {
1532 /* drap: pop %drap */
1533 cfa
->base
= state
->drap_reg
;
1535 state
->drap_offset
= -1;
1537 } else if (regs
[op
->dest
.reg
].offset
== -state
->stack_size
) {
1540 restore_reg(state
, op
->dest
.reg
);
1543 state
->stack_size
-= 8;
1544 if (cfa
->base
== CFI_SP
)
1549 case OP_SRC_REG_INDIRECT
:
1550 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1551 op
->src
.offset
== state
->drap_offset
) {
1553 /* drap: mov disp(%rbp), %drap */
1554 cfa
->base
= state
->drap_reg
;
1556 state
->drap_offset
= -1;
1559 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1560 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
1562 /* drap: mov disp(%rbp), %reg */
1563 restore_reg(state
, op
->dest
.reg
);
1565 } else if (op
->src
.reg
== cfa
->base
&&
1566 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
1568 /* mov disp(%rbp), %reg */
1569 /* mov disp(%rsp), %reg */
1570 restore_reg(state
, op
->dest
.reg
);
1576 WARN_FUNC("unknown stack-related instruction",
1577 insn
->sec
, insn
->offset
);
1584 state
->stack_size
+= 8;
1585 if (cfa
->base
== CFI_SP
)
1588 if (op
->src
.type
!= OP_SRC_REG
)
1592 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1594 /* drap: push %drap */
1595 cfa
->base
= CFI_BP_INDIRECT
;
1596 cfa
->offset
= -state
->stack_size
;
1598 /* save drap so we know when to restore it */
1599 state
->drap_offset
= -state
->stack_size
;
1601 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== state
->drap_reg
) {
1603 /* drap: push %rbp */
1604 state
->stack_size
= 0;
1606 } else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1608 /* drap: push %reg */
1609 save_reg(state
, op
->src
.reg
, CFI_BP
, -state
->stack_size
);
1615 save_reg(state
, op
->src
.reg
, CFI_CFA
, -state
->stack_size
);
1618 /* detect when asm code uses rbp as a scratch register */
1619 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
1620 cfa
->base
!= CFI_BP
)
1621 state
->bp_scratch
= true;
1624 case OP_DEST_REG_INDIRECT
:
1627 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1629 /* drap: mov %drap, disp(%rbp) */
1630 cfa
->base
= CFI_BP_INDIRECT
;
1631 cfa
->offset
= op
->dest
.offset
;
1633 /* save drap offset so we know when to restore it */
1634 state
->drap_offset
= op
->dest
.offset
;
1637 else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1639 /* drap: mov reg, disp(%rbp) */
1640 save_reg(state
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
1643 } else if (op
->dest
.reg
== cfa
->base
) {
1645 /* mov reg, disp(%rbp) */
1646 /* mov reg, disp(%rsp) */
1647 save_reg(state
, op
->src
.reg
, CFI_CFA
,
1648 op
->dest
.offset
- state
->cfa
.offset
);
1654 if ((!state
->drap
&& cfa
->base
!= CFI_BP
) ||
1655 (state
->drap
&& cfa
->base
!= state
->drap_reg
)) {
1656 WARN_FUNC("leave instruction with modified stack frame",
1657 insn
->sec
, insn
->offset
);
1661 /* leave (mov %rbp, %rsp; pop %rbp) */
1663 state
->stack_size
= -state
->regs
[CFI_BP
].offset
- 8;
1664 restore_reg(state
, CFI_BP
);
1674 if (op
->src
.type
!= OP_SRC_POP
) {
1675 WARN_FUNC("unknown stack-related memory operation",
1676 insn
->sec
, insn
->offset
);
1681 state
->stack_size
-= 8;
1682 if (cfa
->base
== CFI_SP
)
1688 WARN_FUNC("unknown stack-related instruction",
1689 insn
->sec
, insn
->offset
);
1696 static bool insn_state_match(struct instruction
*insn
, struct insn_state
*state
)
1698 struct insn_state
*state1
= &insn
->state
, *state2
= state
;
1701 if (memcmp(&state1
->cfa
, &state2
->cfa
, sizeof(state1
->cfa
))) {
1702 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1703 insn
->sec
, insn
->offset
,
1704 state1
->cfa
.base
, state1
->cfa
.offset
,
1705 state2
->cfa
.base
, state2
->cfa
.offset
);
1707 } else if (memcmp(&state1
->regs
, &state2
->regs
, sizeof(state1
->regs
))) {
1708 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1709 if (!memcmp(&state1
->regs
[i
], &state2
->regs
[i
],
1710 sizeof(struct cfi_reg
)))
1713 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1714 insn
->sec
, insn
->offset
,
1715 i
, state1
->regs
[i
].base
, state1
->regs
[i
].offset
,
1716 i
, state2
->regs
[i
].base
, state2
->regs
[i
].offset
);
1720 } else if (state1
->type
!= state2
->type
) {
1721 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1722 insn
->sec
, insn
->offset
, state1
->type
, state2
->type
);
1724 } else if (state1
->drap
!= state2
->drap
||
1725 (state1
->drap
&& state1
->drap_reg
!= state2
->drap_reg
) ||
1726 (state1
->drap
&& state1
->drap_offset
!= state2
->drap_offset
)) {
1727 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1728 insn
->sec
, insn
->offset
,
1729 state1
->drap
, state1
->drap_reg
, state1
->drap_offset
,
1730 state2
->drap
, state2
->drap_reg
, state2
->drap_offset
);
1739 * Follow the branch starting at the given instruction, and recursively follow
1740 * any other branches (jumps). Meanwhile, track the frame pointer state at
1741 * each instruction and validate all the rules described in
1742 * tools/objtool/Documentation/stack-validation.txt.
1744 static int validate_branch(struct objtool_file
*file
, struct instruction
*first
,
1745 struct insn_state state
)
1747 struct alternative
*alt
;
1748 struct instruction
*insn
, *next_insn
;
1749 struct section
*sec
;
1750 struct symbol
*func
= NULL
;
1756 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
1757 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1763 next_insn
= next_insn_same_sec(file
, insn
);
1765 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
->pfunc
) {
1766 WARN("%s() falls through to next function %s()",
1767 func
->name
, insn
->func
->name
);
1771 func
= insn
->func
? insn
->func
->pfunc
: NULL
;
1773 if (func
&& insn
->ignore
) {
1774 WARN_FUNC("BUG: why am I validating an ignored function?",
1779 if (insn
->visited
) {
1780 if (!insn
->hint
&& !insn_state_match(insn
, &state
))
1787 if (insn
->restore
) {
1788 struct instruction
*save_insn
, *i
;
1792 func_for_each_insn_continue_reverse(file
, insn
->func
, i
) {
1800 WARN_FUNC("no corresponding CFI save for CFI restore",
1805 if (!save_insn
->visited
) {
1807 * Oops, no state to copy yet.
1808 * Hopefully we can reach this
1809 * instruction from another branch
1810 * after the save insn has been
1816 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1821 insn
->state
= save_insn
->state
;
1824 state
= insn
->state
;
1827 insn
->state
= state
;
1829 insn
->visited
= true;
1831 if (!insn
->ignore_alts
) {
1832 list_for_each_entry(alt
, &insn
->alts
, list
) {
1833 ret
= validate_branch(file
, alt
->insn
, state
);
1839 switch (insn
->type
) {
1842 if (func
&& has_modified_stack_frame(&state
)) {
1843 WARN_FUNC("return with modified stack frame",
1848 if (state
.bp_scratch
) {
1849 WARN("%s uses BP as a scratch register",
1857 if (is_fentry_call(insn
))
1860 ret
= dead_end_function(file
, insn
->call_dest
);
1867 case INSN_CALL_DYNAMIC
:
1868 if (!no_fp
&& func
&& !has_valid_stack_frame(&state
)) {
1869 WARN_FUNC("call without frame pointer save/setup",
1875 case INSN_JUMP_CONDITIONAL
:
1876 case INSN_JUMP_UNCONDITIONAL
:
1877 if (insn
->jump_dest
&&
1878 (!func
|| !insn
->jump_dest
->func
||
1879 insn
->jump_dest
->func
->pfunc
== func
)) {
1880 ret
= validate_branch(file
, insn
->jump_dest
,
1885 } else if (func
&& has_modified_stack_frame(&state
)) {
1886 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1891 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
1896 case INSN_JUMP_DYNAMIC
:
1897 if (func
&& list_empty(&insn
->alts
) &&
1898 has_modified_stack_frame(&state
)) {
1899 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1906 case INSN_CONTEXT_SWITCH
:
1907 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
1908 WARN_FUNC("unsupported instruction in callable function",
1915 if (update_insn_state(insn
, &state
))
1928 if (state
.cfa
.base
== CFI_UNDEFINED
)
1930 WARN("%s: unexpected end of section", sec
->name
);
1940 static int validate_unwind_hints(struct objtool_file
*file
)
1942 struct instruction
*insn
;
1943 int ret
, warnings
= 0;
1944 struct insn_state state
;
1949 clear_insn_state(&state
);
1951 for_each_insn(file
, insn
) {
1952 if (insn
->hint
&& !insn
->visited
) {
1953 ret
= validate_branch(file
, insn
, state
);
1961 static int validate_retpoline(struct objtool_file
*file
)
1963 struct instruction
*insn
;
1966 for_each_insn(file
, insn
) {
1967 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1968 insn
->type
!= INSN_CALL_DYNAMIC
)
1971 if (insn
->retpoline_safe
)
1975 * .init.text code is ran before userspace and thus doesn't
1976 * strictly need retpolines, except for modules which are
1977 * loaded late, they very much do need retpoline in their
1980 if (!strcmp(insn
->sec
->name
, ".init.text") && !module
)
1983 WARN_FUNC("indirect %s found in RETPOLINE build",
1984 insn
->sec
, insn
->offset
,
1985 insn
->type
== INSN_JUMP_DYNAMIC
? "jump" : "call");
1993 static bool is_kasan_insn(struct instruction
*insn
)
1995 return (insn
->type
== INSN_CALL
&&
1996 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1999 static bool is_ubsan_insn(struct instruction
*insn
)
2001 return (insn
->type
== INSN_CALL
&&
2002 !strcmp(insn
->call_dest
->name
,
2003 "__ubsan_handle_builtin_unreachable"));
2006 static bool ignore_unreachable_insn(struct instruction
*insn
)
2010 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
2014 * Ignore any unused exceptions. This can happen when a whitelisted
2015 * function has an exception table entry.
2017 * Also ignore alternative replacement instructions. This can happen
2018 * when a whitelisted function uses one of the ALTERNATIVE macros.
2020 if (!strcmp(insn
->sec
->name
, ".fixup") ||
2021 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
2022 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
2026 * Check if this (or a subsequent) instruction is related to
2027 * CONFIG_UBSAN or CONFIG_KASAN.
2029 * End the search at 5 instructions to avoid going into the weeds.
2033 for (i
= 0; i
< 5; i
++) {
2035 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
2038 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
2039 if (insn
->jump_dest
&&
2040 insn
->jump_dest
->func
== insn
->func
) {
2041 insn
= insn
->jump_dest
;
2048 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
2051 insn
= list_next_entry(insn
, list
);
2057 static int validate_functions(struct objtool_file
*file
)
2059 struct section
*sec
;
2060 struct symbol
*func
;
2061 struct instruction
*insn
;
2062 struct insn_state state
;
2063 int ret
, warnings
= 0;
2065 clear_insn_state(&state
);
2067 state
.cfa
= initial_func_cfi
.cfa
;
2068 memcpy(&state
.regs
, &initial_func_cfi
.regs
,
2069 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
2070 state
.stack_size
= initial_func_cfi
.cfa
.offset
;
2072 for_each_sec(file
, sec
) {
2073 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
2074 if (func
->type
!= STT_FUNC
|| func
->pfunc
!= func
)
2077 insn
= find_insn(file
, sec
, func
->offset
);
2078 if (!insn
|| insn
->ignore
)
2081 ret
= validate_branch(file
, insn
, state
);
2089 static int validate_reachable_instructions(struct objtool_file
*file
)
2091 struct instruction
*insn
;
2093 if (file
->ignore_unreachables
)
2096 for_each_insn(file
, insn
) {
2097 if (insn
->visited
|| ignore_unreachable_insn(insn
))
2100 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
2107 static void cleanup(struct objtool_file
*file
)
2109 struct instruction
*insn
, *tmpinsn
;
2110 struct alternative
*alt
, *tmpalt
;
2112 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
2113 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
2114 list_del(&alt
->list
);
2117 list_del(&insn
->list
);
2118 hash_del(&insn
->hash
);
2121 elf_close(file
->elf
);
2124 int check(const char *_objname
, bool orc
)
2126 struct objtool_file file
;
2127 int ret
, warnings
= 0;
2131 file
.elf
= elf_open(objname
, orc
? O_RDWR
: O_RDONLY
);
2135 INIT_LIST_HEAD(&file
.insn_list
);
2136 hash_init(file
.insn_hash
);
2137 file
.whitelist
= find_section_by_name(file
.elf
, ".discard.func_stack_frame_non_standard");
2138 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
2139 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
2140 file
.ignore_unreachables
= no_unreachable
;
2143 arch_initial_func_cfi_state(&initial_func_cfi
);
2145 ret
= decode_sections(&file
);
2150 if (list_empty(&file
.insn_list
))
2154 ret
= validate_retpoline(&file
);
2160 ret
= validate_functions(&file
);
2165 ret
= validate_unwind_hints(&file
);
2171 ret
= validate_reachable_instructions(&file
);
2178 ret
= create_orc(&file
);
2182 ret
= create_orc_sections(&file
);
2186 ret
= elf_write(file
.elf
);
2194 /* ignore warnings for now until we get all the code cleaned up */
2195 if (ret
|| warnings
)