1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
10 #include <objtool/builtin.h>
11 #include <objtool/cfi.h>
12 #include <objtool/arch.h>
13 #include <objtool/check.h>
14 #include <objtool/special.h>
15 #include <objtool/warn.h>
16 #include <objtool/endianness.h>
18 #include <linux/objtool.h>
19 #include <linux/hashtable.h>
20 #include <linux/kernel.h>
21 #include <linux/static_call_types.h>
24 struct list_head list
;
25 struct instruction
*insn
;
29 struct cfi_init_state initial_func_cfi
;
31 struct instruction
*find_insn(struct objtool_file
*file
,
32 struct section
*sec
, unsigned long offset
)
34 struct instruction
*insn
;
36 hash_for_each_possible(file
->insn_hash
, insn
, hash
, sec_offset_hash(sec
, offset
)) {
37 if (insn
->sec
== sec
&& insn
->offset
== offset
)
44 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
45 struct instruction
*insn
)
47 struct instruction
*next
= list_next_entry(insn
, list
);
49 if (!next
|| &next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
55 static struct instruction
*next_insn_same_func(struct objtool_file
*file
,
56 struct instruction
*insn
)
58 struct instruction
*next
= list_next_entry(insn
, list
);
59 struct symbol
*func
= insn
->func
;
64 if (&next
->list
!= &file
->insn_list
&& next
->func
== func
)
67 /* Check if we're already in the subfunction: */
68 if (func
== func
->cfunc
)
71 /* Move to the subfunction: */
72 return find_insn(file
, func
->cfunc
->sec
, func
->cfunc
->offset
);
75 static struct instruction
*prev_insn_same_sym(struct objtool_file
*file
,
76 struct instruction
*insn
)
78 struct instruction
*prev
= list_prev_entry(insn
, list
);
80 if (&prev
->list
!= &file
->insn_list
&& prev
->func
== insn
->func
)
86 #define func_for_each_insn(file, func, insn) \
87 for (insn = find_insn(file, func->sec, func->offset); \
89 insn = next_insn_same_func(file, insn))
91 #define sym_for_each_insn(file, sym, insn) \
92 for (insn = find_insn(file, sym->sec, sym->offset); \
93 insn && &insn->list != &file->insn_list && \
94 insn->sec == sym->sec && \
95 insn->offset < sym->offset + sym->len; \
96 insn = list_next_entry(insn, list))
98 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
99 for (insn = list_prev_entry(insn, list); \
100 &insn->list != &file->insn_list && \
101 insn->sec == sym->sec && insn->offset >= sym->offset; \
102 insn = list_prev_entry(insn, list))
104 #define sec_for_each_insn_from(file, insn) \
105 for (; insn; insn = next_insn_same_sec(file, insn))
107 #define sec_for_each_insn_continue(file, insn) \
108 for (insn = next_insn_same_sec(file, insn); insn; \
109 insn = next_insn_same_sec(file, insn))
111 static bool is_sibling_call(struct instruction
*insn
)
114 * Assume only ELF functions can make sibling calls. This ensures
115 * sibling call detection consistency between vmlinux.o and individual
121 /* An indirect jump is either a sibling call or a jump to a table. */
122 if (insn
->type
== INSN_JUMP_DYNAMIC
)
123 return list_empty(&insn
->alts
);
125 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
126 return (is_static_jump(insn
) && insn
->call_dest
);
130 * This checks to see if the given function is a "noreturn" function.
132 * For global functions which are outside the scope of this object file, we
133 * have to keep a manual list of them.
135 * For local functions, we have to detect them manually by simply looking for
136 * the lack of a return instruction.
138 static bool __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
142 struct instruction
*insn
;
146 * Unfortunately these have to be hard coded because the noreturn
147 * attribute isn't provided in ELF data.
149 static const char * const global_noreturns
[] = {
154 "__module_put_and_exit",
160 "machine_real_restart",
161 "rewind_stack_do_exit",
162 "kunit_try_catch_throw",
169 if (func
->bind
== STB_WEAK
)
172 if (func
->bind
== STB_GLOBAL
)
173 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
174 if (!strcmp(func
->name
, global_noreturns
[i
]))
180 insn
= find_insn(file
, func
->sec
, func
->offset
);
184 func_for_each_insn(file
, func
, insn
) {
187 if (insn
->type
== INSN_RETURN
)
195 * A function can have a sibling call instead of a return. In that
196 * case, the function's dead-end status depends on whether the target
197 * of the sibling call returns.
199 func_for_each_insn(file
, func
, insn
) {
200 if (is_sibling_call(insn
)) {
201 struct instruction
*dest
= insn
->jump_dest
;
204 /* sibling call to another file */
207 /* local sibling call */
208 if (recursion
== 5) {
210 * Infinite recursion: two functions have
211 * sibling calls to each other. This is a very
212 * rare case. It means they aren't dead ends.
217 return __dead_end_function(file
, dest
->func
, recursion
+1);
224 static bool dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
226 return __dead_end_function(file
, func
, 0);
229 static void init_cfi_state(struct cfi_state
*cfi
)
233 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
234 cfi
->regs
[i
].base
= CFI_UNDEFINED
;
235 cfi
->vals
[i
].base
= CFI_UNDEFINED
;
237 cfi
->cfa
.base
= CFI_UNDEFINED
;
238 cfi
->drap_reg
= CFI_UNDEFINED
;
239 cfi
->drap_offset
= -1;
242 static void init_insn_state(struct insn_state
*state
, struct section
*sec
)
244 memset(state
, 0, sizeof(*state
));
245 init_cfi_state(&state
->cfi
);
248 * We need the full vmlinux for noinstr validation, otherwise we can
249 * not correctly determine insn->call_dest->sec (external symbols do
250 * not have a section).
252 if (vmlinux
&& noinstr
&& sec
)
253 state
->noinstr
= sec
->noinstr
;
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
;
266 unsigned long nr_insns
= 0;
269 for_each_sec(file
, sec
) {
271 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
274 if (strcmp(sec
->name
, ".altinstr_replacement") &&
275 strcmp(sec
->name
, ".altinstr_aux") &&
276 strncmp(sec
->name
, ".discard.", 9))
279 if (!strcmp(sec
->name
, ".noinstr.text") ||
280 !strcmp(sec
->name
, ".entry.text"))
283 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
284 insn
= malloc(sizeof(*insn
));
286 WARN("malloc failed");
289 memset(insn
, 0, sizeof(*insn
));
290 INIT_LIST_HEAD(&insn
->alts
);
291 INIT_LIST_HEAD(&insn
->stack_ops
);
292 init_cfi_state(&insn
->cfi
);
295 insn
->offset
= offset
;
297 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
299 &insn
->len
, &insn
->type
,
305 hash_add(file
->insn_hash
, &insn
->hash
, sec_offset_hash(sec
, insn
->offset
));
306 list_add_tail(&insn
->list
, &file
->insn_list
);
310 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
311 if (func
->type
!= STT_FUNC
|| func
->alias
!= func
)
314 if (!find_insn(file
, sec
, func
->offset
)) {
315 WARN("%s(): can't find starting instruction",
320 sym_for_each_insn(file
, func
, insn
)
326 printf("nr_insns: %lu\n", nr_insns
);
335 static struct instruction
*find_last_insn(struct objtool_file
*file
,
338 struct instruction
*insn
= NULL
;
340 unsigned int end
= (sec
->len
> 10) ? sec
->len
- 10 : 0;
342 for (offset
= sec
->len
- 1; offset
>= end
&& !insn
; offset
--)
343 insn
= find_insn(file
, sec
, offset
);
349 * Mark "ud2" instructions and manually annotated dead ends.
351 static int add_dead_ends(struct objtool_file
*file
)
355 struct instruction
*insn
;
358 * By default, "ud2" is a dead end unless otherwise annotated, because
359 * GCC 7 inserts it for certain divide-by-zero cases.
361 for_each_insn(file
, insn
)
362 if (insn
->type
== INSN_BUG
)
363 insn
->dead_end
= true;
366 * Check for manually annotated dead ends.
368 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
372 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
373 if (reloc
->sym
->type
!= STT_SECTION
) {
374 WARN("unexpected relocation symbol type in %s", sec
->name
);
377 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
379 insn
= list_prev_entry(insn
, list
);
380 else if (reloc
->addend
== reloc
->sym
->sec
->len
) {
381 insn
= find_last_insn(file
, reloc
->sym
->sec
);
383 WARN("can't find unreachable insn at %s+0x%x",
384 reloc
->sym
->sec
->name
, reloc
->addend
);
388 WARN("can't find unreachable insn at %s+0x%x",
389 reloc
->sym
->sec
->name
, reloc
->addend
);
393 insn
->dead_end
= true;
398 * These manually annotated reachable checks are needed for GCC 4.4,
399 * where the Linux unreachable() macro isn't supported. In that case
400 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
403 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
407 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
408 if (reloc
->sym
->type
!= STT_SECTION
) {
409 WARN("unexpected relocation symbol type in %s", sec
->name
);
412 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
414 insn
= list_prev_entry(insn
, list
);
415 else if (reloc
->addend
== reloc
->sym
->sec
->len
) {
416 insn
= find_last_insn(file
, reloc
->sym
->sec
);
418 WARN("can't find reachable insn at %s+0x%x",
419 reloc
->sym
->sec
->name
, reloc
->addend
);
423 WARN("can't find reachable insn at %s+0x%x",
424 reloc
->sym
->sec
->name
, reloc
->addend
);
428 insn
->dead_end
= false;
434 static int create_static_call_sections(struct objtool_file
*file
)
437 struct static_call_site
*site
;
438 struct instruction
*insn
;
439 struct symbol
*key_sym
;
440 char *key_name
, *tmp
;
443 sec
= find_section_by_name(file
->elf
, ".static_call_sites");
445 INIT_LIST_HEAD(&file
->static_call_list
);
446 WARN("file already has .static_call_sites section, skipping");
450 if (list_empty(&file
->static_call_list
))
454 list_for_each_entry(insn
, &file
->static_call_list
, static_call_node
)
457 sec
= elf_create_section(file
->elf
, ".static_call_sites", SHF_WRITE
,
458 sizeof(struct static_call_site
), idx
);
463 list_for_each_entry(insn
, &file
->static_call_list
, static_call_node
) {
465 site
= (struct static_call_site
*)sec
->data
->d_buf
+ idx
;
466 memset(site
, 0, sizeof(struct static_call_site
));
468 /* populate reloc for 'addr' */
469 if (elf_add_reloc_to_insn(file
->elf
, sec
,
470 idx
* sizeof(struct static_call_site
),
472 insn
->sec
, insn
->offset
))
475 /* find key symbol */
476 key_name
= strdup(insn
->call_dest
->name
);
481 if (strncmp(key_name
, STATIC_CALL_TRAMP_PREFIX_STR
,
482 STATIC_CALL_TRAMP_PREFIX_LEN
)) {
483 WARN("static_call: trampoline name malformed: %s", key_name
);
486 tmp
= key_name
+ STATIC_CALL_TRAMP_PREFIX_LEN
- STATIC_CALL_KEY_PREFIX_LEN
;
487 memcpy(tmp
, STATIC_CALL_KEY_PREFIX_STR
, STATIC_CALL_KEY_PREFIX_LEN
);
489 key_sym
= find_symbol_by_name(file
->elf
, tmp
);
492 WARN("static_call: can't find static_call_key symbol: %s", tmp
);
497 * For modules(), the key might not be exported, which
498 * means the module can make static calls but isn't
499 * allowed to change them.
501 * In that case we temporarily set the key to be the
502 * trampoline address. This is fixed up in
503 * static_call_add_module().
505 key_sym
= insn
->call_dest
;
509 /* populate reloc for 'key' */
510 if (elf_add_reloc(file
->elf
, sec
,
511 idx
* sizeof(struct static_call_site
) + 4,
512 R_X86_64_PC32
, key_sym
,
513 is_sibling_call(insn
) * STATIC_CALL_SITE_TAIL
))
522 static int create_mcount_loc_sections(struct objtool_file
*file
)
526 struct instruction
*insn
;
529 sec
= find_section_by_name(file
->elf
, "__mcount_loc");
531 INIT_LIST_HEAD(&file
->mcount_loc_list
);
532 WARN("file already has __mcount_loc section, skipping");
536 if (list_empty(&file
->mcount_loc_list
))
540 list_for_each_entry(insn
, &file
->mcount_loc_list
, mcount_loc_node
)
543 sec
= elf_create_section(file
->elf
, "__mcount_loc", 0, sizeof(unsigned long), idx
);
548 list_for_each_entry(insn
, &file
->mcount_loc_list
, mcount_loc_node
) {
550 loc
= (unsigned long *)sec
->data
->d_buf
+ idx
;
551 memset(loc
, 0, sizeof(unsigned long));
553 if (elf_add_reloc_to_insn(file
->elf
, sec
,
554 idx
* sizeof(unsigned long),
556 insn
->sec
, insn
->offset
))
566 * Warnings shouldn't be reported for ignored functions.
568 static void add_ignores(struct objtool_file
*file
)
570 struct instruction
*insn
;
575 sec
= find_section_by_name(file
->elf
, ".rela.discard.func_stack_frame_non_standard");
579 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
580 switch (reloc
->sym
->type
) {
586 func
= find_func_by_offset(reloc
->sym
->sec
, reloc
->addend
);
592 WARN("unexpected relocation symbol type in %s: %d", sec
->name
, reloc
->sym
->type
);
596 func_for_each_insn(file
, func
, insn
)
602 * This is a whitelist of functions that is allowed to be called with AC set.
603 * The list is meant to be minimal and only contains compiler instrumentation
604 * ABI and a few functions used to implement *_{to,from}_user() functions.
606 * These functions must not directly change AC, but may PUSHF/POPF.
608 static const char *uaccess_safe_builtin
[] = {
612 /* KASAN out-of-line */
613 "__asan_loadN_noabort",
614 "__asan_load1_noabort",
615 "__asan_load2_noabort",
616 "__asan_load4_noabort",
617 "__asan_load8_noabort",
618 "__asan_load16_noabort",
619 "__asan_storeN_noabort",
620 "__asan_store1_noabort",
621 "__asan_store2_noabort",
622 "__asan_store4_noabort",
623 "__asan_store8_noabort",
624 "__asan_store16_noabort",
625 "__kasan_check_read",
626 "__kasan_check_write",
628 "__asan_report_load_n_noabort",
629 "__asan_report_load1_noabort",
630 "__asan_report_load2_noabort",
631 "__asan_report_load4_noabort",
632 "__asan_report_load8_noabort",
633 "__asan_report_load16_noabort",
634 "__asan_report_store_n_noabort",
635 "__asan_report_store1_noabort",
636 "__asan_report_store2_noabort",
637 "__asan_report_store4_noabort",
638 "__asan_report_store8_noabort",
639 "__asan_report_store16_noabort",
641 "__kcsan_check_access",
642 "kcsan_found_watchpoint",
643 "kcsan_setup_watchpoint",
644 "kcsan_check_scoped_accesses",
645 "kcsan_disable_current",
646 "kcsan_enable_current_nowarn",
651 "__tsan_write_range",
662 "__tsan_read_write1",
663 "__tsan_read_write2",
664 "__tsan_read_write4",
665 "__tsan_read_write8",
666 "__tsan_read_write16",
667 "__tsan_atomic8_load",
668 "__tsan_atomic16_load",
669 "__tsan_atomic32_load",
670 "__tsan_atomic64_load",
671 "__tsan_atomic8_store",
672 "__tsan_atomic16_store",
673 "__tsan_atomic32_store",
674 "__tsan_atomic64_store",
675 "__tsan_atomic8_exchange",
676 "__tsan_atomic16_exchange",
677 "__tsan_atomic32_exchange",
678 "__tsan_atomic64_exchange",
679 "__tsan_atomic8_fetch_add",
680 "__tsan_atomic16_fetch_add",
681 "__tsan_atomic32_fetch_add",
682 "__tsan_atomic64_fetch_add",
683 "__tsan_atomic8_fetch_sub",
684 "__tsan_atomic16_fetch_sub",
685 "__tsan_atomic32_fetch_sub",
686 "__tsan_atomic64_fetch_sub",
687 "__tsan_atomic8_fetch_and",
688 "__tsan_atomic16_fetch_and",
689 "__tsan_atomic32_fetch_and",
690 "__tsan_atomic64_fetch_and",
691 "__tsan_atomic8_fetch_or",
692 "__tsan_atomic16_fetch_or",
693 "__tsan_atomic32_fetch_or",
694 "__tsan_atomic64_fetch_or",
695 "__tsan_atomic8_fetch_xor",
696 "__tsan_atomic16_fetch_xor",
697 "__tsan_atomic32_fetch_xor",
698 "__tsan_atomic64_fetch_xor",
699 "__tsan_atomic8_fetch_nand",
700 "__tsan_atomic16_fetch_nand",
701 "__tsan_atomic32_fetch_nand",
702 "__tsan_atomic64_fetch_nand",
703 "__tsan_atomic8_compare_exchange_strong",
704 "__tsan_atomic16_compare_exchange_strong",
705 "__tsan_atomic32_compare_exchange_strong",
706 "__tsan_atomic64_compare_exchange_strong",
707 "__tsan_atomic8_compare_exchange_weak",
708 "__tsan_atomic16_compare_exchange_weak",
709 "__tsan_atomic32_compare_exchange_weak",
710 "__tsan_atomic64_compare_exchange_weak",
711 "__tsan_atomic8_compare_exchange_val",
712 "__tsan_atomic16_compare_exchange_val",
713 "__tsan_atomic32_compare_exchange_val",
714 "__tsan_atomic64_compare_exchange_val",
715 "__tsan_atomic_thread_fence",
716 "__tsan_atomic_signal_fence",
720 "__sanitizer_cov_trace_pc",
721 "__sanitizer_cov_trace_const_cmp1",
722 "__sanitizer_cov_trace_const_cmp2",
723 "__sanitizer_cov_trace_const_cmp4",
724 "__sanitizer_cov_trace_const_cmp8",
725 "__sanitizer_cov_trace_cmp1",
726 "__sanitizer_cov_trace_cmp2",
727 "__sanitizer_cov_trace_cmp4",
728 "__sanitizer_cov_trace_cmp8",
729 "__sanitizer_cov_trace_switch",
731 "ubsan_type_mismatch_common",
732 "__ubsan_handle_type_mismatch",
733 "__ubsan_handle_type_mismatch_v1",
734 "__ubsan_handle_shift_out_of_bounds",
736 "csum_partial_copy_generic",
738 "copy_mc_fragile_handle_tail",
739 "copy_mc_enhanced_fast_string",
740 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
744 static void add_uaccess_safe(struct objtool_file
*file
)
752 for (name
= uaccess_safe_builtin
; *name
; name
++) {
753 func
= find_symbol_by_name(file
->elf
, *name
);
757 func
->uaccess_safe
= true;
762 * FIXME: For now, just ignore any alternatives which add retpolines. This is
763 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
764 * But it at least allows objtool to understand the control flow *around* the
767 static int add_ignore_alternatives(struct objtool_file
*file
)
771 struct instruction
*insn
;
773 sec
= find_section_by_name(file
->elf
, ".rela.discard.ignore_alts");
777 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
778 if (reloc
->sym
->type
!= STT_SECTION
) {
779 WARN("unexpected relocation symbol type in %s", sec
->name
);
783 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
785 WARN("bad .discard.ignore_alts entry");
789 insn
->ignore_alts
= true;
795 __weak
bool arch_is_retpoline(struct symbol
*sym
)
801 * Find the destination instructions for all jumps.
803 static int add_jump_destinations(struct objtool_file
*file
)
805 struct instruction
*insn
;
807 struct section
*dest_sec
;
808 unsigned long dest_off
;
810 for_each_insn(file
, insn
) {
811 if (!is_static_jump(insn
))
814 reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
815 insn
->offset
, insn
->len
);
817 dest_sec
= insn
->sec
;
818 dest_off
= arch_jump_destination(insn
);
819 } else if (reloc
->sym
->type
== STT_SECTION
) {
820 dest_sec
= reloc
->sym
->sec
;
821 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
822 } else if (arch_is_retpoline(reloc
->sym
)) {
824 * Retpoline jumps are really dynamic jumps in
825 * disguise, so convert them accordingly.
827 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
828 insn
->type
= INSN_JUMP_DYNAMIC
;
830 insn
->type
= INSN_JUMP_DYNAMIC_CONDITIONAL
;
832 insn
->retpoline_safe
= true;
834 } else if (insn
->func
) {
835 /* internal or external sibling call (with reloc) */
836 insn
->call_dest
= reloc
->sym
;
837 if (insn
->call_dest
->static_call_tramp
) {
838 list_add_tail(&insn
->static_call_node
,
839 &file
->static_call_list
);
842 } else if (reloc
->sym
->sec
->idx
) {
843 dest_sec
= reloc
->sym
->sec
;
844 dest_off
= reloc
->sym
->sym
.st_value
+
845 arch_dest_reloc_offset(reloc
->addend
);
847 /* non-func asm code jumping to another file */
851 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
852 if (!insn
->jump_dest
) {
855 * This is a special case where an alt instruction
856 * jumps past the end of the section. These are
857 * handled later in handle_group_alt().
859 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
862 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
863 insn
->sec
, insn
->offset
, dest_sec
->name
,
869 * Cross-function jump.
871 if (insn
->func
&& insn
->jump_dest
->func
&&
872 insn
->func
!= insn
->jump_dest
->func
) {
875 * For GCC 8+, create parent/child links for any cold
876 * subfunctions. This is _mostly_ redundant with a
877 * similar initialization in read_symbols().
879 * If a function has aliases, we want the *first* such
880 * function in the symbol table to be the subfunction's
881 * parent. In that case we overwrite the
882 * initialization done in read_symbols().
884 * However this code can't completely replace the
885 * read_symbols() code because this doesn't detect the
886 * case where the parent function's only reference to a
887 * subfunction is through a jump table.
889 if (!strstr(insn
->func
->name
, ".cold") &&
890 strstr(insn
->jump_dest
->func
->name
, ".cold")) {
891 insn
->func
->cfunc
= insn
->jump_dest
->func
;
892 insn
->jump_dest
->func
->pfunc
= insn
->func
;
894 } else if (insn
->jump_dest
->func
->pfunc
!= insn
->func
->pfunc
&&
895 insn
->jump_dest
->offset
== insn
->jump_dest
->func
->offset
) {
897 /* internal sibling call (without reloc) */
898 insn
->call_dest
= insn
->jump_dest
->func
;
899 if (insn
->call_dest
->static_call_tramp
) {
900 list_add_tail(&insn
->static_call_node
,
901 &file
->static_call_list
);
910 static void remove_insn_ops(struct instruction
*insn
)
912 struct stack_op
*op
, *tmp
;
914 list_for_each_entry_safe(op
, tmp
, &insn
->stack_ops
, list
) {
920 static struct symbol
*find_call_destination(struct section
*sec
, unsigned long offset
)
922 struct symbol
*call_dest
;
924 call_dest
= find_func_by_offset(sec
, offset
);
926 call_dest
= find_symbol_by_offset(sec
, offset
);
932 * Find the destination instructions for all calls.
934 static int add_call_destinations(struct objtool_file
*file
)
936 struct instruction
*insn
;
937 unsigned long dest_off
;
940 for_each_insn(file
, insn
) {
941 if (insn
->type
!= INSN_CALL
)
944 reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
945 insn
->offset
, insn
->len
);
947 dest_off
= arch_jump_destination(insn
);
948 insn
->call_dest
= find_call_destination(insn
->sec
, dest_off
);
953 if (!insn
->call_dest
) {
954 WARN_FUNC("unannotated intra-function call", insn
->sec
, insn
->offset
);
958 if (insn
->func
&& insn
->call_dest
->type
!= STT_FUNC
) {
959 WARN_FUNC("unsupported call to non-function",
960 insn
->sec
, insn
->offset
);
964 } else if (reloc
->sym
->type
== STT_SECTION
) {
965 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
966 insn
->call_dest
= find_call_destination(reloc
->sym
->sec
,
968 if (!insn
->call_dest
) {
969 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
970 insn
->sec
, insn
->offset
,
971 reloc
->sym
->sec
->name
,
976 } else if (arch_is_retpoline(reloc
->sym
)) {
978 * Retpoline calls are really dynamic calls in
979 * disguise, so convert them accordingly.
981 insn
->type
= INSN_CALL_DYNAMIC
;
982 insn
->retpoline_safe
= true;
984 remove_insn_ops(insn
);
988 insn
->call_dest
= reloc
->sym
;
990 if (insn
->call_dest
&& insn
->call_dest
->static_call_tramp
) {
991 list_add_tail(&insn
->static_call_node
,
992 &file
->static_call_list
);
996 * Many compilers cannot disable KCOV with a function attribute
997 * so they need a little help, NOP out any KCOV calls from noinstr
1000 if (insn
->sec
->noinstr
&&
1001 !strncmp(insn
->call_dest
->name
, "__sanitizer_cov_", 16)) {
1003 reloc
->type
= R_NONE
;
1004 elf_write_reloc(file
->elf
, reloc
);
1007 elf_write_insn(file
->elf
, insn
->sec
,
1008 insn
->offset
, insn
->len
,
1009 arch_nop_insn(insn
->len
));
1010 insn
->type
= INSN_NOP
;
1013 if (mcount
&& !strcmp(insn
->call_dest
->name
, "__fentry__")) {
1015 reloc
->type
= R_NONE
;
1016 elf_write_reloc(file
->elf
, reloc
);
1019 elf_write_insn(file
->elf
, insn
->sec
,
1020 insn
->offset
, insn
->len
,
1021 arch_nop_insn(insn
->len
));
1023 insn
->type
= INSN_NOP
;
1025 list_add_tail(&insn
->mcount_loc_node
,
1026 &file
->mcount_loc_list
);
1030 * Whatever stack impact regular CALLs have, should be undone
1031 * by the RETURN of the called function.
1033 * Annotated intra-function calls retain the stack_ops but
1034 * are converted to JUMP, see read_intra_function_calls().
1036 remove_insn_ops(insn
);
1043 * The .alternatives section requires some extra special care over and above
1044 * other special sections because alternatives are patched in place.
1046 static int handle_group_alt(struct objtool_file
*file
,
1047 struct special_alt
*special_alt
,
1048 struct instruction
*orig_insn
,
1049 struct instruction
**new_insn
)
1051 struct instruction
*last_orig_insn
, *last_new_insn
= NULL
, *insn
, *nop
= NULL
;
1052 struct alt_group
*orig_alt_group
, *new_alt_group
;
1053 unsigned long dest_off
;
1056 orig_alt_group
= malloc(sizeof(*orig_alt_group
));
1057 if (!orig_alt_group
) {
1058 WARN("malloc failed");
1061 orig_alt_group
->cfi
= calloc(special_alt
->orig_len
,
1062 sizeof(struct cfi_state
*));
1063 if (!orig_alt_group
->cfi
) {
1064 WARN("calloc failed");
1068 last_orig_insn
= NULL
;
1070 sec_for_each_insn_from(file
, insn
) {
1071 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
1074 insn
->alt_group
= orig_alt_group
;
1075 last_orig_insn
= insn
;
1077 orig_alt_group
->orig_group
= NULL
;
1078 orig_alt_group
->first_insn
= orig_insn
;
1079 orig_alt_group
->last_insn
= last_orig_insn
;
1082 new_alt_group
= malloc(sizeof(*new_alt_group
));
1083 if (!new_alt_group
) {
1084 WARN("malloc failed");
1088 if (special_alt
->new_len
< special_alt
->orig_len
) {
1090 * Insert a fake nop at the end to make the replacement
1091 * alt_group the same size as the original. This is needed to
1092 * allow propagate_alt_cfi() to do its magic. When the last
1093 * instruction affects the stack, the instruction after it (the
1094 * nop) will propagate the new state to the shared CFI array.
1096 nop
= malloc(sizeof(*nop
));
1098 WARN("malloc failed");
1101 memset(nop
, 0, sizeof(*nop
));
1102 INIT_LIST_HEAD(&nop
->alts
);
1103 INIT_LIST_HEAD(&nop
->stack_ops
);
1104 init_cfi_state(&nop
->cfi
);
1106 nop
->sec
= special_alt
->new_sec
;
1107 nop
->offset
= special_alt
->new_off
+ special_alt
->new_len
;
1108 nop
->len
= special_alt
->orig_len
- special_alt
->new_len
;
1109 nop
->type
= INSN_NOP
;
1110 nop
->func
= orig_insn
->func
;
1111 nop
->alt_group
= new_alt_group
;
1112 nop
->ignore
= orig_insn
->ignore_alts
;
1115 if (!special_alt
->new_len
) {
1121 sec_for_each_insn_from(file
, insn
) {
1122 struct reloc
*alt_reloc
;
1124 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
1127 last_new_insn
= insn
;
1129 insn
->ignore
= orig_insn
->ignore_alts
;
1130 insn
->func
= orig_insn
->func
;
1131 insn
->alt_group
= new_alt_group
;
1134 * Since alternative replacement code is copy/pasted by the
1135 * kernel after applying relocations, generally such code can't
1136 * have relative-address relocation references to outside the
1137 * .altinstr_replacement section, unless the arch's
1138 * alternatives code can adjust the relative offsets
1141 alt_reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
1142 insn
->offset
, insn
->len
);
1144 !arch_support_alt_relocation(special_alt
, insn
, alt_reloc
)) {
1146 WARN_FUNC("unsupported relocation in alternatives section",
1147 insn
->sec
, insn
->offset
);
1151 if (!is_static_jump(insn
))
1154 if (!insn
->immediate
)
1157 dest_off
= arch_jump_destination(insn
);
1158 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
1159 insn
->jump_dest
= next_insn_same_sec(file
, last_orig_insn
);
1161 if (!insn
->jump_dest
) {
1162 WARN_FUNC("can't find alternative jump destination",
1163 insn
->sec
, insn
->offset
);
1168 if (!last_new_insn
) {
1169 WARN_FUNC("can't find last new alternative instruction",
1170 special_alt
->new_sec
, special_alt
->new_off
);
1175 list_add(&nop
->list
, &last_new_insn
->list
);
1177 new_alt_group
->orig_group
= orig_alt_group
;
1178 new_alt_group
->first_insn
= *new_insn
;
1179 new_alt_group
->last_insn
= nop
? : last_new_insn
;
1180 new_alt_group
->cfi
= orig_alt_group
->cfi
;
1185 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1186 * If the original instruction is a jump, make the alt entry an effective nop
1187 * by just skipping the original instruction.
1189 static int handle_jump_alt(struct objtool_file
*file
,
1190 struct special_alt
*special_alt
,
1191 struct instruction
*orig_insn
,
1192 struct instruction
**new_insn
)
1194 if (orig_insn
->type
== INSN_NOP
)
1197 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
1198 WARN_FUNC("unsupported instruction at jump label",
1199 orig_insn
->sec
, orig_insn
->offset
);
1203 *new_insn
= list_next_entry(orig_insn
, list
);
1208 * Read all the special sections which have alternate instructions which can be
1209 * patched in or redirected to at runtime. Each instruction having alternate
1210 * instruction(s) has them added to its insn->alts list, which will be
1211 * traversed in validate_branch().
1213 static int add_special_section_alts(struct objtool_file
*file
)
1215 struct list_head special_alts
;
1216 struct instruction
*orig_insn
, *new_insn
;
1217 struct special_alt
*special_alt
, *tmp
;
1218 struct alternative
*alt
;
1221 ret
= special_get_alts(file
->elf
, &special_alts
);
1225 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
1227 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
1228 special_alt
->orig_off
);
1230 WARN_FUNC("special: can't find orig instruction",
1231 special_alt
->orig_sec
, special_alt
->orig_off
);
1237 if (!special_alt
->group
|| special_alt
->new_len
) {
1238 new_insn
= find_insn(file
, special_alt
->new_sec
,
1239 special_alt
->new_off
);
1241 WARN_FUNC("special: can't find new instruction",
1242 special_alt
->new_sec
,
1243 special_alt
->new_off
);
1249 if (special_alt
->group
) {
1250 if (!special_alt
->orig_len
) {
1251 WARN_FUNC("empty alternative entry",
1252 orig_insn
->sec
, orig_insn
->offset
);
1256 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
1260 } else if (special_alt
->jump_or_nop
) {
1261 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
1267 alt
= malloc(sizeof(*alt
));
1269 WARN("malloc failed");
1274 alt
->insn
= new_insn
;
1275 alt
->skip_orig
= special_alt
->skip_orig
;
1276 orig_insn
->ignore_alts
|= special_alt
->skip_alt
;
1277 list_add_tail(&alt
->list
, &orig_insn
->alts
);
1279 list_del(&special_alt
->list
);
1287 static int add_jump_table(struct objtool_file
*file
, struct instruction
*insn
,
1288 struct reloc
*table
)
1290 struct reloc
*reloc
= table
;
1291 struct instruction
*dest_insn
;
1292 struct alternative
*alt
;
1293 struct symbol
*pfunc
= insn
->func
->pfunc
;
1294 unsigned int prev_offset
= 0;
1297 * Each @reloc is a switch table relocation which points to the target
1300 list_for_each_entry_from(reloc
, &table
->sec
->reloc_list
, list
) {
1302 /* Check for the end of the table: */
1303 if (reloc
!= table
&& reloc
->jump_table_start
)
1306 /* Make sure the table entries are consecutive: */
1307 if (prev_offset
&& reloc
->offset
!= prev_offset
+ 8)
1310 /* Detect function pointers from contiguous objects: */
1311 if (reloc
->sym
->sec
== pfunc
->sec
&&
1312 reloc
->addend
== pfunc
->offset
)
1315 dest_insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1319 /* Make sure the destination is in the same function: */
1320 if (!dest_insn
->func
|| dest_insn
->func
->pfunc
!= pfunc
)
1323 alt
= malloc(sizeof(*alt
));
1325 WARN("malloc failed");
1329 alt
->insn
= dest_insn
;
1330 list_add_tail(&alt
->list
, &insn
->alts
);
1331 prev_offset
= reloc
->offset
;
1335 WARN_FUNC("can't find switch jump table",
1336 insn
->sec
, insn
->offset
);
1344 * find_jump_table() - Given a dynamic jump, find the switch jump table
1345 * associated with it.
1347 static struct reloc
*find_jump_table(struct objtool_file
*file
,
1348 struct symbol
*func
,
1349 struct instruction
*insn
)
1351 struct reloc
*table_reloc
;
1352 struct instruction
*dest_insn
, *orig_insn
= insn
;
1355 * Backward search using the @first_jump_src links, these help avoid
1356 * much of the 'in between' code. Which avoids us getting confused by
1360 insn
&& insn
->func
&& insn
->func
->pfunc
== func
;
1361 insn
= insn
->first_jump_src
?: prev_insn_same_sym(file
, insn
)) {
1363 if (insn
!= orig_insn
&& insn
->type
== INSN_JUMP_DYNAMIC
)
1366 /* allow small jumps within the range */
1367 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
1369 (insn
->jump_dest
->offset
<= insn
->offset
||
1370 insn
->jump_dest
->offset
> orig_insn
->offset
))
1373 table_reloc
= arch_find_switch_table(file
, insn
);
1376 dest_insn
= find_insn(file
, table_reloc
->sym
->sec
, table_reloc
->addend
);
1377 if (!dest_insn
|| !dest_insn
->func
|| dest_insn
->func
->pfunc
!= func
)
1387 * First pass: Mark the head of each jump table so that in the next pass,
1388 * we know when a given jump table ends and the next one starts.
1390 static void mark_func_jump_tables(struct objtool_file
*file
,
1391 struct symbol
*func
)
1393 struct instruction
*insn
, *last
= NULL
;
1394 struct reloc
*reloc
;
1396 func_for_each_insn(file
, func
, insn
) {
1401 * Store back-pointers for unconditional forward jumps such
1402 * that find_jump_table() can back-track using those and
1403 * avoid some potentially confusing code.
1405 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
&&
1406 insn
->offset
> last
->offset
&&
1407 insn
->jump_dest
->offset
> insn
->offset
&&
1408 !insn
->jump_dest
->first_jump_src
) {
1410 insn
->jump_dest
->first_jump_src
= insn
;
1411 last
= insn
->jump_dest
;
1414 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
1417 reloc
= find_jump_table(file
, func
, insn
);
1419 reloc
->jump_table_start
= true;
1420 insn
->jump_table
= reloc
;
1425 static int add_func_jump_tables(struct objtool_file
*file
,
1426 struct symbol
*func
)
1428 struct instruction
*insn
;
1431 func_for_each_insn(file
, func
, insn
) {
1432 if (!insn
->jump_table
)
1435 ret
= add_jump_table(file
, insn
, insn
->jump_table
);
1444 * For some switch statements, gcc generates a jump table in the .rodata
1445 * section which contains a list of addresses within the function to jump to.
1446 * This finds these jump tables and adds them to the insn->alts lists.
1448 static int add_jump_table_alts(struct objtool_file
*file
)
1450 struct section
*sec
;
1451 struct symbol
*func
;
1457 for_each_sec(file
, sec
) {
1458 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1459 if (func
->type
!= STT_FUNC
)
1462 mark_func_jump_tables(file
, func
);
1463 ret
= add_func_jump_tables(file
, func
);
1472 static void set_func_state(struct cfi_state
*state
)
1474 state
->cfa
= initial_func_cfi
.cfa
;
1475 memcpy(&state
->regs
, &initial_func_cfi
.regs
,
1476 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
1477 state
->stack_size
= initial_func_cfi
.cfa
.offset
;
1480 static int read_unwind_hints(struct objtool_file
*file
)
1482 struct section
*sec
, *relocsec
;
1483 struct reloc
*reloc
;
1484 struct unwind_hint
*hint
;
1485 struct instruction
*insn
;
1488 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
1492 relocsec
= sec
->reloc
;
1494 WARN("missing .rela.discard.unwind_hints section");
1498 if (sec
->len
% sizeof(struct unwind_hint
)) {
1499 WARN("struct unwind_hint size mismatch");
1505 for (i
= 0; i
< sec
->len
/ sizeof(struct unwind_hint
); i
++) {
1506 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1508 reloc
= find_reloc_by_dest(file
->elf
, sec
, i
* sizeof(*hint
));
1510 WARN("can't find reloc for unwind_hints[%d]", i
);
1514 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1516 WARN("can't find insn for unwind_hints[%d]", i
);
1522 if (hint
->type
== UNWIND_HINT_TYPE_FUNC
) {
1523 set_func_state(&insn
->cfi
);
1527 if (arch_decode_hint_reg(insn
, hint
->sp_reg
)) {
1528 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1529 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1533 insn
->cfi
.cfa
.offset
= bswap_if_needed(hint
->sp_offset
);
1534 insn
->cfi
.type
= hint
->type
;
1535 insn
->cfi
.end
= hint
->end
;
1541 static int read_retpoline_hints(struct objtool_file
*file
)
1543 struct section
*sec
;
1544 struct instruction
*insn
;
1545 struct reloc
*reloc
;
1547 sec
= find_section_by_name(file
->elf
, ".rela.discard.retpoline_safe");
1551 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1552 if (reloc
->sym
->type
!= STT_SECTION
) {
1553 WARN("unexpected relocation symbol type in %s", sec
->name
);
1557 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1559 WARN("bad .discard.retpoline_safe entry");
1563 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1564 insn
->type
!= INSN_CALL_DYNAMIC
) {
1565 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1566 insn
->sec
, insn
->offset
);
1570 insn
->retpoline_safe
= true;
1576 static int read_instr_hints(struct objtool_file
*file
)
1578 struct section
*sec
;
1579 struct instruction
*insn
;
1580 struct reloc
*reloc
;
1582 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_end");
1586 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1587 if (reloc
->sym
->type
!= STT_SECTION
) {
1588 WARN("unexpected relocation symbol type in %s", sec
->name
);
1592 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1594 WARN("bad .discard.instr_end entry");
1601 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_begin");
1605 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1606 if (reloc
->sym
->type
!= STT_SECTION
) {
1607 WARN("unexpected relocation symbol type in %s", sec
->name
);
1611 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1613 WARN("bad .discard.instr_begin entry");
1623 static int read_intra_function_calls(struct objtool_file
*file
)
1625 struct instruction
*insn
;
1626 struct section
*sec
;
1627 struct reloc
*reloc
;
1629 sec
= find_section_by_name(file
->elf
, ".rela.discard.intra_function_calls");
1633 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1634 unsigned long dest_off
;
1636 if (reloc
->sym
->type
!= STT_SECTION
) {
1637 WARN("unexpected relocation symbol type in %s",
1642 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1644 WARN("bad .discard.intra_function_call entry");
1648 if (insn
->type
!= INSN_CALL
) {
1649 WARN_FUNC("intra_function_call not a direct call",
1650 insn
->sec
, insn
->offset
);
1655 * Treat intra-function CALLs as JMPs, but with a stack_op.
1656 * See add_call_destinations(), which strips stack_ops from
1659 insn
->type
= INSN_JUMP_UNCONDITIONAL
;
1661 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
1662 insn
->jump_dest
= find_insn(file
, insn
->sec
, dest_off
);
1663 if (!insn
->jump_dest
) {
1664 WARN_FUNC("can't find call dest at %s+0x%lx",
1665 insn
->sec
, insn
->offset
,
1666 insn
->sec
->name
, dest_off
);
1674 static int read_static_call_tramps(struct objtool_file
*file
)
1676 struct section
*sec
;
1677 struct symbol
*func
;
1679 for_each_sec(file
, sec
) {
1680 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1681 if (func
->bind
== STB_GLOBAL
&&
1682 !strncmp(func
->name
, STATIC_CALL_TRAMP_PREFIX_STR
,
1683 strlen(STATIC_CALL_TRAMP_PREFIX_STR
)))
1684 func
->static_call_tramp
= true;
1691 static void mark_rodata(struct objtool_file
*file
)
1693 struct section
*sec
;
1697 * Search for the following rodata sections, each of which can
1698 * potentially contain jump tables:
1700 * - .rodata: can contain GCC switch tables
1701 * - .rodata.<func>: same, if -fdata-sections is being used
1702 * - .rodata..c_jump_table: contains C annotated jump tables
1704 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1706 for_each_sec(file
, sec
) {
1707 if (!strncmp(sec
->name
, ".rodata", 7) &&
1708 !strstr(sec
->name
, ".str1.")) {
1714 file
->rodata
= found
;
1717 static int decode_sections(struct objtool_file
*file
)
1723 ret
= decode_instructions(file
);
1727 ret
= add_dead_ends(file
);
1732 add_uaccess_safe(file
);
1734 ret
= add_ignore_alternatives(file
);
1739 * Must be before add_{jump_call}_destination.
1741 ret
= read_static_call_tramps(file
);
1745 ret
= add_jump_destinations(file
);
1749 ret
= add_special_section_alts(file
);
1754 * Must be before add_call_destination(); it changes INSN_CALL to
1757 ret
= read_intra_function_calls(file
);
1761 ret
= add_call_destinations(file
);
1765 ret
= add_jump_table_alts(file
);
1769 ret
= read_unwind_hints(file
);
1773 ret
= read_retpoline_hints(file
);
1777 ret
= read_instr_hints(file
);
1784 static bool is_fentry_call(struct instruction
*insn
)
1786 if (insn
->type
== INSN_CALL
&& insn
->call_dest
&&
1787 insn
->call_dest
->type
== STT_NOTYPE
&&
1788 !strcmp(insn
->call_dest
->name
, "__fentry__"))
1794 static bool has_modified_stack_frame(struct instruction
*insn
, struct insn_state
*state
)
1796 struct cfi_state
*cfi
= &state
->cfi
;
1799 if (cfi
->cfa
.base
!= initial_func_cfi
.cfa
.base
|| cfi
->drap
)
1802 if (cfi
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
)
1805 if (cfi
->stack_size
!= initial_func_cfi
.cfa
.offset
)
1808 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1809 if (cfi
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1810 cfi
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1817 static bool check_reg_frame_pos(const struct cfi_reg
*reg
,
1818 int expected_offset
)
1820 return reg
->base
== CFI_CFA
&&
1821 reg
->offset
== expected_offset
;
1824 static bool has_valid_stack_frame(struct insn_state
*state
)
1826 struct cfi_state
*cfi
= &state
->cfi
;
1828 if (cfi
->cfa
.base
== CFI_BP
&&
1829 check_reg_frame_pos(&cfi
->regs
[CFI_BP
], -cfi
->cfa
.offset
) &&
1830 check_reg_frame_pos(&cfi
->regs
[CFI_RA
], -cfi
->cfa
.offset
+ 8))
1833 if (cfi
->drap
&& cfi
->regs
[CFI_BP
].base
== CFI_BP
)
1839 static int update_cfi_state_regs(struct instruction
*insn
,
1840 struct cfi_state
*cfi
,
1841 struct stack_op
*op
)
1843 struct cfi_reg
*cfa
= &cfi
->cfa
;
1845 if (cfa
->base
!= CFI_SP
&& cfa
->base
!= CFI_SP_INDIRECT
)
1849 if (op
->dest
.type
== OP_DEST_PUSH
|| op
->dest
.type
== OP_DEST_PUSHF
)
1853 if (op
->src
.type
== OP_SRC_POP
|| op
->src
.type
== OP_SRC_POPF
)
1856 /* add immediate to sp */
1857 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
1858 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
1859 cfa
->offset
-= op
->src
.offset
;
1864 static void save_reg(struct cfi_state
*cfi
, unsigned char reg
, int base
, int offset
)
1866 if (arch_callee_saved_reg(reg
) &&
1867 cfi
->regs
[reg
].base
== CFI_UNDEFINED
) {
1868 cfi
->regs
[reg
].base
= base
;
1869 cfi
->regs
[reg
].offset
= offset
;
1873 static void restore_reg(struct cfi_state
*cfi
, unsigned char reg
)
1875 cfi
->regs
[reg
].base
= initial_func_cfi
.regs
[reg
].base
;
1876 cfi
->regs
[reg
].offset
= initial_func_cfi
.regs
[reg
].offset
;
1880 * A note about DRAP stack alignment:
1882 * GCC has the concept of a DRAP register, which is used to help keep track of
1883 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1884 * register. The typical DRAP pattern is:
1886 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1887 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1888 * 41 ff 72 f8 pushq -0x8(%r10)
1890 * 48 89 e5 mov %rsp,%rbp
1897 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1900 * There are some variations in the epilogues, like:
1908 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1913 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1914 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1915 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1916 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1918 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1921 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1922 * restored beforehand:
1925 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1926 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1928 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1932 static int update_cfi_state(struct instruction
*insn
, struct cfi_state
*cfi
,
1933 struct stack_op
*op
)
1935 struct cfi_reg
*cfa
= &cfi
->cfa
;
1936 struct cfi_reg
*regs
= cfi
->regs
;
1938 /* stack operations don't make sense with an undefined CFA */
1939 if (cfa
->base
== CFI_UNDEFINED
) {
1941 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1947 if (cfi
->type
== UNWIND_HINT_TYPE_REGS
||
1948 cfi
->type
== UNWIND_HINT_TYPE_REGS_PARTIAL
)
1949 return update_cfi_state_regs(insn
, cfi
, op
);
1951 switch (op
->dest
.type
) {
1954 switch (op
->src
.type
) {
1957 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
1958 cfa
->base
== CFI_SP
&&
1959 check_reg_frame_pos(®s
[CFI_BP
], -cfa
->offset
)) {
1961 /* mov %rsp, %rbp */
1962 cfa
->base
= op
->dest
.reg
;
1963 cfi
->bp_scratch
= false;
1966 else if (op
->src
.reg
== CFI_SP
&&
1967 op
->dest
.reg
== CFI_BP
&& cfi
->drap
) {
1969 /* drap: mov %rsp, %rbp */
1970 regs
[CFI_BP
].base
= CFI_BP
;
1971 regs
[CFI_BP
].offset
= -cfi
->stack_size
;
1972 cfi
->bp_scratch
= false;
1975 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1980 * This is needed for the rare case where GCC
1987 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1988 cfi
->vals
[op
->dest
.reg
].offset
= -cfi
->stack_size
;
1991 else if (op
->src
.reg
== CFI_BP
&& op
->dest
.reg
== CFI_SP
&&
1992 cfa
->base
== CFI_BP
) {
1997 * Restore the original stack pointer (Clang).
1999 cfi
->stack_size
= -cfi
->regs
[CFI_BP
].offset
;
2002 else if (op
->dest
.reg
== cfa
->base
) {
2004 /* mov %reg, %rsp */
2005 if (cfa
->base
== CFI_SP
&&
2006 cfi
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
2009 * This is needed for the rare case
2010 * where GCC does something dumb like:
2012 * lea 0x8(%rsp), %rcx
2016 cfa
->offset
= -cfi
->vals
[op
->src
.reg
].offset
;
2017 cfi
->stack_size
= cfa
->offset
;
2019 } else if (cfa
->base
== CFI_SP
&&
2020 cfi
->vals
[op
->src
.reg
].base
== CFI_SP_INDIRECT
&&
2021 cfi
->vals
[op
->src
.reg
].offset
== cfa
->offset
) {
2026 * 1: mov %rsp, (%[tos])
2027 * 2: mov %[tos], %rsp
2033 * 1 - places a pointer to the previous
2034 * stack at the Top-of-Stack of the
2037 * 2 - switches to the new stack.
2039 * 3 - pops the Top-of-Stack to restore
2040 * the original stack.
2042 * Note: we set base to SP_INDIRECT
2043 * here and preserve offset. Therefore
2044 * when the unwinder reaches ToS it
2045 * will dereference SP and then add the
2046 * offset to find the next frame, IOW:
2049 cfa
->base
= CFI_SP_INDIRECT
;
2052 cfa
->base
= CFI_UNDEFINED
;
2057 else if (op
->dest
.reg
== CFI_SP
&&
2058 cfi
->vals
[op
->src
.reg
].base
== CFI_SP_INDIRECT
&&
2059 cfi
->vals
[op
->src
.reg
].offset
== cfa
->offset
) {
2062 * The same stack swizzle case 2) as above. But
2063 * because we can't change cfa->base, case 3)
2064 * will become a regular POP. Pretend we're a
2065 * PUSH so things don't go unbalanced.
2067 cfi
->stack_size
+= 8;
2074 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
2077 cfi
->stack_size
-= op
->src
.offset
;
2078 if (cfa
->base
== CFI_SP
)
2079 cfa
->offset
-= op
->src
.offset
;
2083 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
2085 /* lea disp(%rbp), %rsp */
2086 cfi
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
2090 if (!cfi
->drap
&& op
->src
.reg
== CFI_SP
&&
2091 op
->dest
.reg
== CFI_BP
&& cfa
->base
== CFI_SP
&&
2092 check_reg_frame_pos(®s
[CFI_BP
], -cfa
->offset
+ op
->src
.offset
)) {
2094 /* lea disp(%rsp), %rbp */
2096 cfa
->offset
-= op
->src
.offset
;
2097 cfi
->bp_scratch
= false;
2101 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
2103 /* drap: lea disp(%rsp), %drap */
2104 cfi
->drap_reg
= op
->dest
.reg
;
2107 * lea disp(%rsp), %reg
2109 * This is needed for the rare case where GCC
2110 * does something dumb like:
2112 * lea 0x8(%rsp), %rcx
2116 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
2117 cfi
->vals
[op
->dest
.reg
].offset
= \
2118 -cfi
->stack_size
+ op
->src
.offset
;
2123 if (cfi
->drap
&& op
->dest
.reg
== CFI_SP
&&
2124 op
->src
.reg
== cfi
->drap_reg
) {
2126 /* drap: lea disp(%drap), %rsp */
2128 cfa
->offset
= cfi
->stack_size
= -op
->src
.offset
;
2129 cfi
->drap_reg
= CFI_UNDEFINED
;
2134 if (op
->dest
.reg
== cfi
->cfa
.base
) {
2135 WARN_FUNC("unsupported stack register modification",
2136 insn
->sec
, insn
->offset
);
2143 if (op
->dest
.reg
!= CFI_SP
||
2144 (cfi
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
2145 (cfi
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
2146 WARN_FUNC("unsupported stack pointer realignment",
2147 insn
->sec
, insn
->offset
);
2151 if (cfi
->drap_reg
!= CFI_UNDEFINED
) {
2152 /* drap: and imm, %rsp */
2153 cfa
->base
= cfi
->drap_reg
;
2154 cfa
->offset
= cfi
->stack_size
= 0;
2159 * Older versions of GCC (4.8ish) realign the stack
2160 * without DRAP, with a frame pointer.
2167 if (op
->dest
.reg
== CFI_SP
&& cfa
->base
== CFI_SP_INDIRECT
) {
2169 /* pop %rsp; # restore from a stack swizzle */
2174 if (!cfi
->drap
&& op
->dest
.reg
== cfa
->base
) {
2180 if (cfi
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
2181 op
->dest
.reg
== cfi
->drap_reg
&&
2182 cfi
->drap_offset
== -cfi
->stack_size
) {
2184 /* drap: pop %drap */
2185 cfa
->base
= cfi
->drap_reg
;
2187 cfi
->drap_offset
= -1;
2189 } else if (regs
[op
->dest
.reg
].offset
== -cfi
->stack_size
) {
2192 restore_reg(cfi
, op
->dest
.reg
);
2195 cfi
->stack_size
-= 8;
2196 if (cfa
->base
== CFI_SP
)
2201 case OP_SRC_REG_INDIRECT
:
2202 if (!cfi
->drap
&& op
->dest
.reg
== cfa
->base
&&
2203 op
->dest
.reg
== CFI_BP
) {
2205 /* mov disp(%rsp), %rbp */
2207 cfa
->offset
= cfi
->stack_size
;
2210 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2211 op
->src
.offset
== cfi
->drap_offset
) {
2213 /* drap: mov disp(%rbp), %drap */
2214 cfa
->base
= cfi
->drap_reg
;
2216 cfi
->drap_offset
= -1;
2219 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2220 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
2222 /* drap: mov disp(%rbp), %reg */
2223 restore_reg(cfi
, op
->dest
.reg
);
2225 } else if (op
->src
.reg
== cfa
->base
&&
2226 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
2228 /* mov disp(%rbp), %reg */
2229 /* mov disp(%rsp), %reg */
2230 restore_reg(cfi
, op
->dest
.reg
);
2232 } else if (op
->src
.reg
== CFI_SP
&&
2233 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfi
->stack_size
) {
2235 /* mov disp(%rsp), %reg */
2236 restore_reg(cfi
, op
->dest
.reg
);
2242 WARN_FUNC("unknown stack-related instruction",
2243 insn
->sec
, insn
->offset
);
2251 cfi
->stack_size
+= 8;
2252 if (cfa
->base
== CFI_SP
)
2255 if (op
->src
.type
!= OP_SRC_REG
)
2259 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2261 /* drap: push %drap */
2262 cfa
->base
= CFI_BP_INDIRECT
;
2263 cfa
->offset
= -cfi
->stack_size
;
2265 /* save drap so we know when to restore it */
2266 cfi
->drap_offset
= -cfi
->stack_size
;
2268 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== cfi
->drap_reg
) {
2270 /* drap: push %rbp */
2271 cfi
->stack_size
= 0;
2275 /* drap: push %reg */
2276 save_reg(cfi
, op
->src
.reg
, CFI_BP
, -cfi
->stack_size
);
2282 save_reg(cfi
, op
->src
.reg
, CFI_CFA
, -cfi
->stack_size
);
2285 /* detect when asm code uses rbp as a scratch register */
2286 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
2287 cfa
->base
!= CFI_BP
)
2288 cfi
->bp_scratch
= true;
2291 case OP_DEST_REG_INDIRECT
:
2294 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2296 /* drap: mov %drap, disp(%rbp) */
2297 cfa
->base
= CFI_BP_INDIRECT
;
2298 cfa
->offset
= op
->dest
.offset
;
2300 /* save drap offset so we know when to restore it */
2301 cfi
->drap_offset
= op
->dest
.offset
;
2304 /* drap: mov reg, disp(%rbp) */
2305 save_reg(cfi
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
2308 } else if (op
->dest
.reg
== cfa
->base
) {
2310 /* mov reg, disp(%rbp) */
2311 /* mov reg, disp(%rsp) */
2312 save_reg(cfi
, op
->src
.reg
, CFI_CFA
,
2313 op
->dest
.offset
- cfi
->cfa
.offset
);
2315 } else if (op
->dest
.reg
== CFI_SP
) {
2317 /* mov reg, disp(%rsp) */
2318 save_reg(cfi
, op
->src
.reg
, CFI_CFA
,
2319 op
->dest
.offset
- cfi
->stack_size
);
2321 } else if (op
->src
.reg
== CFI_SP
&& op
->dest
.offset
== 0) {
2323 /* mov %rsp, (%reg); # setup a stack swizzle. */
2324 cfi
->vals
[op
->dest
.reg
].base
= CFI_SP_INDIRECT
;
2325 cfi
->vals
[op
->dest
.reg
].offset
= cfa
->offset
;
2331 if ((!cfi
->drap
&& cfa
->base
!= CFI_BP
) ||
2332 (cfi
->drap
&& cfa
->base
!= cfi
->drap_reg
)) {
2333 WARN_FUNC("leave instruction with modified stack frame",
2334 insn
->sec
, insn
->offset
);
2338 /* leave (mov %rbp, %rsp; pop %rbp) */
2340 cfi
->stack_size
= -cfi
->regs
[CFI_BP
].offset
- 8;
2341 restore_reg(cfi
, CFI_BP
);
2351 if (op
->src
.type
!= OP_SRC_POP
&& op
->src
.type
!= OP_SRC_POPF
) {
2352 WARN_FUNC("unknown stack-related memory operation",
2353 insn
->sec
, insn
->offset
);
2358 cfi
->stack_size
-= 8;
2359 if (cfa
->base
== CFI_SP
)
2365 WARN_FUNC("unknown stack-related instruction",
2366 insn
->sec
, insn
->offset
);
2374 * The stack layouts of alternatives instructions can sometimes diverge when
2375 * they have stack modifications. That's fine as long as the potential stack
2376 * layouts don't conflict at any given potential instruction boundary.
2378 * Flatten the CFIs of the different alternative code streams (both original
2379 * and replacement) into a single shared CFI array which can be used to detect
2380 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2382 static int propagate_alt_cfi(struct objtool_file
*file
, struct instruction
*insn
)
2384 struct cfi_state
**alt_cfi
;
2387 if (!insn
->alt_group
)
2390 alt_cfi
= insn
->alt_group
->cfi
;
2391 group_off
= insn
->offset
- insn
->alt_group
->first_insn
->offset
;
2393 if (!alt_cfi
[group_off
]) {
2394 alt_cfi
[group_off
] = &insn
->cfi
;
2396 if (memcmp(alt_cfi
[group_off
], &insn
->cfi
, sizeof(struct cfi_state
))) {
2397 WARN_FUNC("stack layout conflict in alternatives",
2398 insn
->sec
, insn
->offset
);
2406 static int handle_insn_ops(struct instruction
*insn
, struct insn_state
*state
)
2408 struct stack_op
*op
;
2410 list_for_each_entry(op
, &insn
->stack_ops
, list
) {
2412 if (update_cfi_state(insn
, &state
->cfi
, op
))
2415 if (!insn
->alt_group
)
2418 if (op
->dest
.type
== OP_DEST_PUSHF
) {
2419 if (!state
->uaccess_stack
) {
2420 state
->uaccess_stack
= 1;
2421 } else if (state
->uaccess_stack
>> 31) {
2422 WARN_FUNC("PUSHF stack exhausted",
2423 insn
->sec
, insn
->offset
);
2426 state
->uaccess_stack
<<= 1;
2427 state
->uaccess_stack
|= state
->uaccess
;
2430 if (op
->src
.type
== OP_SRC_POPF
) {
2431 if (state
->uaccess_stack
) {
2432 state
->uaccess
= state
->uaccess_stack
& 1;
2433 state
->uaccess_stack
>>= 1;
2434 if (state
->uaccess_stack
== 1)
2435 state
->uaccess_stack
= 0;
2443 static bool insn_cfi_match(struct instruction
*insn
, struct cfi_state
*cfi2
)
2445 struct cfi_state
*cfi1
= &insn
->cfi
;
2448 if (memcmp(&cfi1
->cfa
, &cfi2
->cfa
, sizeof(cfi1
->cfa
))) {
2450 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2451 insn
->sec
, insn
->offset
,
2452 cfi1
->cfa
.base
, cfi1
->cfa
.offset
,
2453 cfi2
->cfa
.base
, cfi2
->cfa
.offset
);
2455 } else if (memcmp(&cfi1
->regs
, &cfi2
->regs
, sizeof(cfi1
->regs
))) {
2456 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
2457 if (!memcmp(&cfi1
->regs
[i
], &cfi2
->regs
[i
],
2458 sizeof(struct cfi_reg
)))
2461 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2462 insn
->sec
, insn
->offset
,
2463 i
, cfi1
->regs
[i
].base
, cfi1
->regs
[i
].offset
,
2464 i
, cfi2
->regs
[i
].base
, cfi2
->regs
[i
].offset
);
2468 } else if (cfi1
->type
!= cfi2
->type
) {
2470 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2471 insn
->sec
, insn
->offset
, cfi1
->type
, cfi2
->type
);
2473 } else if (cfi1
->drap
!= cfi2
->drap
||
2474 (cfi1
->drap
&& cfi1
->drap_reg
!= cfi2
->drap_reg
) ||
2475 (cfi1
->drap
&& cfi1
->drap_offset
!= cfi2
->drap_offset
)) {
2477 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2478 insn
->sec
, insn
->offset
,
2479 cfi1
->drap
, cfi1
->drap_reg
, cfi1
->drap_offset
,
2480 cfi2
->drap
, cfi2
->drap_reg
, cfi2
->drap_offset
);
2488 static inline bool func_uaccess_safe(struct symbol
*func
)
2491 return func
->uaccess_safe
;
2496 static inline const char *call_dest_name(struct instruction
*insn
)
2498 if (insn
->call_dest
)
2499 return insn
->call_dest
->name
;
2504 static inline bool noinstr_call_dest(struct symbol
*func
)
2507 * We can't deal with indirect function calls at present;
2508 * assume they're instrumented.
2514 * If the symbol is from a noinstr section; we good.
2516 if (func
->sec
->noinstr
)
2520 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2521 * something 'BAD' happened. At the risk of taking the machine down,
2522 * let them proceed to get the message out.
2524 if (!strncmp(func
->name
, "__ubsan_handle_", 15))
2530 static int validate_call(struct instruction
*insn
, struct insn_state
*state
)
2532 if (state
->noinstr
&& state
->instr
<= 0 &&
2533 !noinstr_call_dest(insn
->call_dest
)) {
2534 WARN_FUNC("call to %s() leaves .noinstr.text section",
2535 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2539 if (state
->uaccess
&& !func_uaccess_safe(insn
->call_dest
)) {
2540 WARN_FUNC("call to %s() with UACCESS enabled",
2541 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2546 WARN_FUNC("call to %s() with DF set",
2547 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2554 static int validate_sibling_call(struct instruction
*insn
, struct insn_state
*state
)
2556 if (has_modified_stack_frame(insn
, state
)) {
2557 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2558 insn
->sec
, insn
->offset
);
2562 return validate_call(insn
, state
);
2565 static int validate_return(struct symbol
*func
, struct instruction
*insn
, struct insn_state
*state
)
2567 if (state
->noinstr
&& state
->instr
> 0) {
2568 WARN_FUNC("return with instrumentation enabled",
2569 insn
->sec
, insn
->offset
);
2573 if (state
->uaccess
&& !func_uaccess_safe(func
)) {
2574 WARN_FUNC("return with UACCESS enabled",
2575 insn
->sec
, insn
->offset
);
2579 if (!state
->uaccess
&& func_uaccess_safe(func
)) {
2580 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2581 insn
->sec
, insn
->offset
);
2586 WARN_FUNC("return with DF set",
2587 insn
->sec
, insn
->offset
);
2591 if (func
&& has_modified_stack_frame(insn
, state
)) {
2592 WARN_FUNC("return with modified stack frame",
2593 insn
->sec
, insn
->offset
);
2597 if (state
->cfi
.bp_scratch
) {
2598 WARN_FUNC("BP used as a scratch register",
2599 insn
->sec
, insn
->offset
);
2606 static struct instruction
*next_insn_to_validate(struct objtool_file
*file
,
2607 struct instruction
*insn
)
2609 struct alt_group
*alt_group
= insn
->alt_group
;
2612 * Simulate the fact that alternatives are patched in-place. When the
2613 * end of a replacement alt_group is reached, redirect objtool flow to
2614 * the end of the original alt_group.
2616 if (alt_group
&& insn
== alt_group
->last_insn
&& alt_group
->orig_group
)
2617 return next_insn_same_sec(file
, alt_group
->orig_group
->last_insn
);
2619 return next_insn_same_sec(file
, insn
);
2623 * Follow the branch starting at the given instruction, and recursively follow
2624 * any other branches (jumps). Meanwhile, track the frame pointer state at
2625 * each instruction and validate all the rules described in
2626 * tools/objtool/Documentation/stack-validation.txt.
2628 static int validate_branch(struct objtool_file
*file
, struct symbol
*func
,
2629 struct instruction
*insn
, struct insn_state state
)
2631 struct alternative
*alt
;
2632 struct instruction
*next_insn
;
2633 struct section
*sec
;
2640 next_insn
= next_insn_to_validate(file
, insn
);
2642 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
->pfunc
) {
2643 WARN("%s() falls through to next function %s()",
2644 func
->name
, insn
->func
->name
);
2648 if (func
&& insn
->ignore
) {
2649 WARN_FUNC("BUG: why am I validating an ignored function?",
2654 visited
= 1 << state
.uaccess
;
2655 if (insn
->visited
) {
2656 if (!insn
->hint
&& !insn_cfi_match(insn
, &state
.cfi
))
2659 if (insn
->visited
& visited
)
2664 state
.instr
+= insn
->instr
;
2667 state
.cfi
= insn
->cfi
;
2669 insn
->cfi
= state
.cfi
;
2671 insn
->visited
|= visited
;
2673 if (propagate_alt_cfi(file
, insn
))
2676 if (!insn
->ignore_alts
&& !list_empty(&insn
->alts
)) {
2677 bool skip_orig
= false;
2679 list_for_each_entry(alt
, &insn
->alts
, list
) {
2683 ret
= validate_branch(file
, func
, alt
->insn
, state
);
2686 BT_FUNC("(alt)", insn
);
2695 if (handle_insn_ops(insn
, &state
))
2698 switch (insn
->type
) {
2701 return validate_return(func
, insn
, &state
);
2704 case INSN_CALL_DYNAMIC
:
2705 ret
= validate_call(insn
, &state
);
2709 if (!no_fp
&& func
&& !is_fentry_call(insn
) &&
2710 !has_valid_stack_frame(&state
)) {
2711 WARN_FUNC("call without frame pointer save/setup",
2716 if (dead_end_function(file
, insn
->call_dest
))
2721 case INSN_JUMP_CONDITIONAL
:
2722 case INSN_JUMP_UNCONDITIONAL
:
2723 if (is_sibling_call(insn
)) {
2724 ret
= validate_sibling_call(insn
, &state
);
2728 } else if (insn
->jump_dest
) {
2729 ret
= validate_branch(file
, func
,
2730 insn
->jump_dest
, state
);
2733 BT_FUNC("(branch)", insn
);
2738 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
2743 case INSN_JUMP_DYNAMIC
:
2744 case INSN_JUMP_DYNAMIC_CONDITIONAL
:
2745 if (is_sibling_call(insn
)) {
2746 ret
= validate_sibling_call(insn
, &state
);
2751 if (insn
->type
== INSN_JUMP_DYNAMIC
)
2756 case INSN_CONTEXT_SWITCH
:
2757 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
2758 WARN_FUNC("unsupported instruction in callable function",
2765 if (state
.uaccess
) {
2766 WARN_FUNC("recursive UACCESS enable", sec
, insn
->offset
);
2770 state
.uaccess
= true;
2774 if (!state
.uaccess
&& func
) {
2775 WARN_FUNC("redundant UACCESS disable", sec
, insn
->offset
);
2779 if (func_uaccess_safe(func
) && !state
.uaccess_stack
) {
2780 WARN_FUNC("UACCESS-safe disables UACCESS", sec
, insn
->offset
);
2784 state
.uaccess
= false;
2789 WARN_FUNC("recursive STD", sec
, insn
->offset
);
2797 if (!state
.df
&& func
) {
2798 WARN_FUNC("redundant CLD", sec
, insn
->offset
);
2813 if (state
.cfi
.cfa
.base
== CFI_UNDEFINED
)
2815 WARN("%s: unexpected end of section", sec
->name
);
2825 static int validate_unwind_hints(struct objtool_file
*file
, struct section
*sec
)
2827 struct instruction
*insn
;
2828 struct insn_state state
;
2829 int ret
, warnings
= 0;
2834 init_insn_state(&state
, sec
);
2837 insn
= find_insn(file
, sec
, 0);
2841 insn
= list_first_entry(&file
->insn_list
, typeof(*insn
), list
);
2844 while (&insn
->list
!= &file
->insn_list
&& (!sec
|| insn
->sec
== sec
)) {
2845 if (insn
->hint
&& !insn
->visited
) {
2846 ret
= validate_branch(file
, insn
->func
, insn
, state
);
2847 if (ret
&& backtrace
)
2848 BT_FUNC("<=== (hint)", insn
);
2852 insn
= list_next_entry(insn
, list
);
2858 static int validate_retpoline(struct objtool_file
*file
)
2860 struct instruction
*insn
;
2863 for_each_insn(file
, insn
) {
2864 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
2865 insn
->type
!= INSN_CALL_DYNAMIC
)
2868 if (insn
->retpoline_safe
)
2872 * .init.text code is ran before userspace and thus doesn't
2873 * strictly need retpolines, except for modules which are
2874 * loaded late, they very much do need retpoline in their
2877 if (!strcmp(insn
->sec
->name
, ".init.text") && !module
)
2880 WARN_FUNC("indirect %s found in RETPOLINE build",
2881 insn
->sec
, insn
->offset
,
2882 insn
->type
== INSN_JUMP_DYNAMIC
? "jump" : "call");
2890 static bool is_kasan_insn(struct instruction
*insn
)
2892 return (insn
->type
== INSN_CALL
&&
2893 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
2896 static bool is_ubsan_insn(struct instruction
*insn
)
2898 return (insn
->type
== INSN_CALL
&&
2899 !strcmp(insn
->call_dest
->name
,
2900 "__ubsan_handle_builtin_unreachable"));
2903 static bool ignore_unreachable_insn(struct objtool_file
*file
, struct instruction
*insn
)
2906 struct instruction
*prev_insn
;
2908 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
2912 * Ignore any unused exceptions. This can happen when a whitelisted
2913 * function has an exception table entry.
2915 * Also ignore alternative replacement instructions. This can happen
2916 * when a whitelisted function uses one of the ALTERNATIVE macros.
2918 if (!strcmp(insn
->sec
->name
, ".fixup") ||
2919 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
2920 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
2927 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2928 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2929 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2930 * (or occasionally a JMP to UD2).
2932 * It may also insert a UD2 after calling a __noreturn function.
2934 prev_insn
= list_prev_entry(insn
, list
);
2935 if ((prev_insn
->dead_end
|| dead_end_function(file
, prev_insn
->call_dest
)) &&
2936 (insn
->type
== INSN_BUG
||
2937 (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
2938 insn
->jump_dest
&& insn
->jump_dest
->type
== INSN_BUG
)))
2942 * Check if this (or a subsequent) instruction is related to
2943 * CONFIG_UBSAN or CONFIG_KASAN.
2945 * End the search at 5 instructions to avoid going into the weeds.
2947 for (i
= 0; i
< 5; i
++) {
2949 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
2952 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
2953 if (insn
->jump_dest
&&
2954 insn
->jump_dest
->func
== insn
->func
) {
2955 insn
= insn
->jump_dest
;
2962 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
2965 insn
= list_next_entry(insn
, list
);
2971 static int validate_symbol(struct objtool_file
*file
, struct section
*sec
,
2972 struct symbol
*sym
, struct insn_state
*state
)
2974 struct instruction
*insn
;
2978 WARN("%s() is missing an ELF size annotation", sym
->name
);
2982 if (sym
->pfunc
!= sym
|| sym
->alias
!= sym
)
2985 insn
= find_insn(file
, sec
, sym
->offset
);
2986 if (!insn
|| insn
->ignore
|| insn
->visited
)
2989 state
->uaccess
= sym
->uaccess_safe
;
2991 ret
= validate_branch(file
, insn
->func
, insn
, *state
);
2992 if (ret
&& backtrace
)
2993 BT_FUNC("<=== (sym)", insn
);
2997 static int validate_section(struct objtool_file
*file
, struct section
*sec
)
2999 struct insn_state state
;
3000 struct symbol
*func
;
3003 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
3004 if (func
->type
!= STT_FUNC
)
3007 init_insn_state(&state
, sec
);
3008 set_func_state(&state
.cfi
);
3010 warnings
+= validate_symbol(file
, sec
, func
, &state
);
3016 static int validate_vmlinux_functions(struct objtool_file
*file
)
3018 struct section
*sec
;
3021 sec
= find_section_by_name(file
->elf
, ".noinstr.text");
3023 warnings
+= validate_section(file
, sec
);
3024 warnings
+= validate_unwind_hints(file
, sec
);
3027 sec
= find_section_by_name(file
->elf
, ".entry.text");
3029 warnings
+= validate_section(file
, sec
);
3030 warnings
+= validate_unwind_hints(file
, sec
);
3036 static int validate_functions(struct objtool_file
*file
)
3038 struct section
*sec
;
3041 for_each_sec(file
, sec
) {
3042 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
3045 warnings
+= validate_section(file
, sec
);
3051 static int validate_reachable_instructions(struct objtool_file
*file
)
3053 struct instruction
*insn
;
3055 if (file
->ignore_unreachables
)
3058 for_each_insn(file
, insn
) {
3059 if (insn
->visited
|| ignore_unreachable_insn(file
, insn
))
3062 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
3069 int check(struct objtool_file
*file
)
3071 int ret
, warnings
= 0;
3073 arch_initial_func_cfi_state(&initial_func_cfi
);
3075 ret
= decode_sections(file
);
3080 if (list_empty(&file
->insn_list
))
3083 if (vmlinux
&& !validate_dup
) {
3084 ret
= validate_vmlinux_functions(file
);
3093 ret
= validate_retpoline(file
);
3099 ret
= validate_functions(file
);
3104 ret
= validate_unwind_hints(file
, NULL
);
3110 ret
= validate_reachable_instructions(file
);
3116 ret
= create_static_call_sections(file
);
3122 ret
= create_mcount_loc_sections(file
);
3130 * For now, don't fail the kernel build on fatal warnings. These
3131 * errors are still fairly common due to the growing matrix of
3132 * supported toolchains and their recent pace of change.