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_jump_table_jump(struct instruction
*insn
)
113 struct alt_group
*alt_group
= insn
->alt_group
;
115 if (insn
->jump_table
)
118 /* Retpoline alternative for a jump table? */
119 return alt_group
&& alt_group
->orig_group
&&
120 alt_group
->orig_group
->first_insn
->jump_table
;
123 static bool is_sibling_call(struct instruction
*insn
)
126 * Assume only ELF functions can make sibling calls. This ensures
127 * sibling call detection consistency between vmlinux.o and individual
133 /* An indirect jump is either a sibling call or a jump to a table. */
134 if (insn
->type
== INSN_JUMP_DYNAMIC
)
135 return !is_jump_table_jump(insn
);
137 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
138 return (is_static_jump(insn
) && insn
->call_dest
);
142 * This checks to see if the given function is a "noreturn" function.
144 * For global functions which are outside the scope of this object file, we
145 * have to keep a manual list of them.
147 * For local functions, we have to detect them manually by simply looking for
148 * the lack of a return instruction.
150 static bool __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
154 struct instruction
*insn
;
158 * Unfortunately these have to be hard coded because the noreturn
159 * attribute isn't provided in ELF data.
161 static const char * const global_noreturns
[] = {
166 "__module_put_and_exit",
172 "machine_real_restart",
173 "rewind_stack_do_exit",
174 "kunit_try_catch_throw",
176 "cpu_bringup_and_idle",
182 if (func
->bind
== STB_WEAK
)
185 if (func
->bind
== STB_GLOBAL
)
186 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
187 if (!strcmp(func
->name
, global_noreturns
[i
]))
193 insn
= find_insn(file
, func
->sec
, func
->offset
);
197 func_for_each_insn(file
, func
, insn
) {
200 if (insn
->type
== INSN_RETURN
)
208 * A function can have a sibling call instead of a return. In that
209 * case, the function's dead-end status depends on whether the target
210 * of the sibling call returns.
212 func_for_each_insn(file
, func
, insn
) {
213 if (is_sibling_call(insn
)) {
214 struct instruction
*dest
= insn
->jump_dest
;
217 /* sibling call to another file */
220 /* local sibling call */
221 if (recursion
== 5) {
223 * Infinite recursion: two functions have
224 * sibling calls to each other. This is a very
225 * rare case. It means they aren't dead ends.
230 return __dead_end_function(file
, dest
->func
, recursion
+1);
237 static bool dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
239 return __dead_end_function(file
, func
, 0);
242 static void init_cfi_state(struct cfi_state
*cfi
)
246 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
247 cfi
->regs
[i
].base
= CFI_UNDEFINED
;
248 cfi
->vals
[i
].base
= CFI_UNDEFINED
;
250 cfi
->cfa
.base
= CFI_UNDEFINED
;
251 cfi
->drap_reg
= CFI_UNDEFINED
;
252 cfi
->drap_offset
= -1;
255 static void init_insn_state(struct insn_state
*state
, struct section
*sec
)
257 memset(state
, 0, sizeof(*state
));
258 init_cfi_state(&state
->cfi
);
261 * We need the full vmlinux for noinstr validation, otherwise we can
262 * not correctly determine insn->call_dest->sec (external symbols do
263 * not have a section).
265 if (vmlinux
&& noinstr
&& sec
)
266 state
->noinstr
= sec
->noinstr
;
270 * Call the arch-specific instruction decoder for all the instructions and add
271 * them to the global instruction list.
273 static int decode_instructions(struct objtool_file
*file
)
277 unsigned long offset
;
278 struct instruction
*insn
;
279 unsigned long nr_insns
= 0;
282 for_each_sec(file
, sec
) {
284 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
287 if (strcmp(sec
->name
, ".altinstr_replacement") &&
288 strcmp(sec
->name
, ".altinstr_aux") &&
289 strncmp(sec
->name
, ".discard.", 9))
292 if (!strcmp(sec
->name
, ".noinstr.text") ||
293 !strcmp(sec
->name
, ".entry.text"))
296 for (offset
= 0; offset
< sec
->sh
.sh_size
; offset
+= insn
->len
) {
297 insn
= malloc(sizeof(*insn
));
299 WARN("malloc failed");
302 memset(insn
, 0, sizeof(*insn
));
303 INIT_LIST_HEAD(&insn
->alts
);
304 INIT_LIST_HEAD(&insn
->stack_ops
);
305 init_cfi_state(&insn
->cfi
);
308 insn
->offset
= offset
;
310 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
311 sec
->sh
.sh_size
- offset
,
312 &insn
->len
, &insn
->type
,
318 hash_add(file
->insn_hash
, &insn
->hash
, sec_offset_hash(sec
, insn
->offset
));
319 list_add_tail(&insn
->list
, &file
->insn_list
);
323 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
324 if (func
->type
!= STT_FUNC
|| func
->alias
!= func
)
327 if (!find_insn(file
, sec
, func
->offset
)) {
328 WARN("%s(): can't find starting instruction",
333 sym_for_each_insn(file
, func
, insn
)
339 printf("nr_insns: %lu\n", nr_insns
);
348 static struct instruction
*find_last_insn(struct objtool_file
*file
,
351 struct instruction
*insn
= NULL
;
353 unsigned int end
= (sec
->sh
.sh_size
> 10) ? sec
->sh
.sh_size
- 10 : 0;
355 for (offset
= sec
->sh
.sh_size
- 1; offset
>= end
&& !insn
; offset
--)
356 insn
= find_insn(file
, sec
, offset
);
362 * Mark "ud2" instructions and manually annotated dead ends.
364 static int add_dead_ends(struct objtool_file
*file
)
368 struct instruction
*insn
;
371 * By default, "ud2" is a dead end unless otherwise annotated, because
372 * GCC 7 inserts it for certain divide-by-zero cases.
374 for_each_insn(file
, insn
)
375 if (insn
->type
== INSN_BUG
)
376 insn
->dead_end
= true;
379 * Check for manually annotated dead ends.
381 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
385 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
386 if (reloc
->sym
->type
!= STT_SECTION
) {
387 WARN("unexpected relocation symbol type in %s", sec
->name
);
390 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
392 insn
= list_prev_entry(insn
, list
);
393 else if (reloc
->addend
== reloc
->sym
->sec
->sh
.sh_size
) {
394 insn
= find_last_insn(file
, reloc
->sym
->sec
);
396 WARN("can't find unreachable insn at %s+0x%x",
397 reloc
->sym
->sec
->name
, reloc
->addend
);
401 WARN("can't find unreachable insn at %s+0x%x",
402 reloc
->sym
->sec
->name
, reloc
->addend
);
406 insn
->dead_end
= true;
411 * These manually annotated reachable checks are needed for GCC 4.4,
412 * where the Linux unreachable() macro isn't supported. In that case
413 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
416 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
420 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
421 if (reloc
->sym
->type
!= STT_SECTION
) {
422 WARN("unexpected relocation symbol type in %s", sec
->name
);
425 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
427 insn
= list_prev_entry(insn
, list
);
428 else if (reloc
->addend
== reloc
->sym
->sec
->sh
.sh_size
) {
429 insn
= find_last_insn(file
, reloc
->sym
->sec
);
431 WARN("can't find reachable insn at %s+0x%x",
432 reloc
->sym
->sec
->name
, reloc
->addend
);
436 WARN("can't find reachable insn at %s+0x%x",
437 reloc
->sym
->sec
->name
, reloc
->addend
);
441 insn
->dead_end
= false;
447 static int create_static_call_sections(struct objtool_file
*file
)
450 struct static_call_site
*site
;
451 struct instruction
*insn
;
452 struct symbol
*key_sym
;
453 char *key_name
, *tmp
;
456 sec
= find_section_by_name(file
->elf
, ".static_call_sites");
458 INIT_LIST_HEAD(&file
->static_call_list
);
459 WARN("file already has .static_call_sites section, skipping");
463 if (list_empty(&file
->static_call_list
))
467 list_for_each_entry(insn
, &file
->static_call_list
, call_node
)
470 sec
= elf_create_section(file
->elf
, ".static_call_sites", SHF_WRITE
,
471 sizeof(struct static_call_site
), idx
);
476 list_for_each_entry(insn
, &file
->static_call_list
, call_node
) {
478 site
= (struct static_call_site
*)sec
->data
->d_buf
+ idx
;
479 memset(site
, 0, sizeof(struct static_call_site
));
481 /* populate reloc for 'addr' */
482 if (elf_add_reloc_to_insn(file
->elf
, sec
,
483 idx
* sizeof(struct static_call_site
),
485 insn
->sec
, insn
->offset
))
488 /* find key symbol */
489 key_name
= strdup(insn
->call_dest
->name
);
494 if (strncmp(key_name
, STATIC_CALL_TRAMP_PREFIX_STR
,
495 STATIC_CALL_TRAMP_PREFIX_LEN
)) {
496 WARN("static_call: trampoline name malformed: %s", key_name
);
499 tmp
= key_name
+ STATIC_CALL_TRAMP_PREFIX_LEN
- STATIC_CALL_KEY_PREFIX_LEN
;
500 memcpy(tmp
, STATIC_CALL_KEY_PREFIX_STR
, STATIC_CALL_KEY_PREFIX_LEN
);
502 key_sym
= find_symbol_by_name(file
->elf
, tmp
);
505 WARN("static_call: can't find static_call_key symbol: %s", tmp
);
510 * For modules(), the key might not be exported, which
511 * means the module can make static calls but isn't
512 * allowed to change them.
514 * In that case we temporarily set the key to be the
515 * trampoline address. This is fixed up in
516 * static_call_add_module().
518 key_sym
= insn
->call_dest
;
522 /* populate reloc for 'key' */
523 if (elf_add_reloc(file
->elf
, sec
,
524 idx
* sizeof(struct static_call_site
) + 4,
525 R_X86_64_PC32
, key_sym
,
526 is_sibling_call(insn
) * STATIC_CALL_SITE_TAIL
))
535 static int create_retpoline_sites_sections(struct objtool_file
*file
)
537 struct instruction
*insn
;
541 sec
= find_section_by_name(file
->elf
, ".retpoline_sites");
543 WARN("file already has .retpoline_sites, skipping");
548 list_for_each_entry(insn
, &file
->retpoline_call_list
, call_node
)
554 sec
= elf_create_section(file
->elf
, ".retpoline_sites", 0,
557 WARN("elf_create_section: .retpoline_sites");
562 list_for_each_entry(insn
, &file
->retpoline_call_list
, call_node
) {
564 int *site
= (int *)sec
->data
->d_buf
+ idx
;
567 if (elf_add_reloc_to_insn(file
->elf
, sec
,
570 insn
->sec
, insn
->offset
)) {
571 WARN("elf_add_reloc_to_insn: .retpoline_sites");
581 static int create_mcount_loc_sections(struct objtool_file
*file
)
585 struct instruction
*insn
;
588 sec
= find_section_by_name(file
->elf
, "__mcount_loc");
590 INIT_LIST_HEAD(&file
->mcount_loc_list
);
591 WARN("file already has __mcount_loc section, skipping");
595 if (list_empty(&file
->mcount_loc_list
))
599 list_for_each_entry(insn
, &file
->mcount_loc_list
, call_node
)
602 sec
= elf_create_section(file
->elf
, "__mcount_loc", 0, sizeof(unsigned long), idx
);
607 list_for_each_entry(insn
, &file
->mcount_loc_list
, call_node
) {
609 loc
= (unsigned long *)sec
->data
->d_buf
+ idx
;
610 memset(loc
, 0, sizeof(unsigned long));
612 if (elf_add_reloc_to_insn(file
->elf
, sec
,
613 idx
* sizeof(unsigned long),
615 insn
->sec
, insn
->offset
))
625 * Warnings shouldn't be reported for ignored functions.
627 static void add_ignores(struct objtool_file
*file
)
629 struct instruction
*insn
;
634 sec
= find_section_by_name(file
->elf
, ".rela.discard.func_stack_frame_non_standard");
638 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
639 switch (reloc
->sym
->type
) {
645 func
= find_func_by_offset(reloc
->sym
->sec
, reloc
->addend
);
651 WARN("unexpected relocation symbol type in %s: %d", sec
->name
, reloc
->sym
->type
);
655 func_for_each_insn(file
, func
, insn
)
661 * This is a whitelist of functions that is allowed to be called with AC set.
662 * The list is meant to be minimal and only contains compiler instrumentation
663 * ABI and a few functions used to implement *_{to,from}_user() functions.
665 * These functions must not directly change AC, but may PUSHF/POPF.
667 static const char *uaccess_safe_builtin
[] = {
671 /* KASAN out-of-line */
672 "__asan_loadN_noabort",
673 "__asan_load1_noabort",
674 "__asan_load2_noabort",
675 "__asan_load4_noabort",
676 "__asan_load8_noabort",
677 "__asan_load16_noabort",
678 "__asan_storeN_noabort",
679 "__asan_store1_noabort",
680 "__asan_store2_noabort",
681 "__asan_store4_noabort",
682 "__asan_store8_noabort",
683 "__asan_store16_noabort",
684 "__kasan_check_read",
685 "__kasan_check_write",
687 "__asan_report_load_n_noabort",
688 "__asan_report_load1_noabort",
689 "__asan_report_load2_noabort",
690 "__asan_report_load4_noabort",
691 "__asan_report_load8_noabort",
692 "__asan_report_load16_noabort",
693 "__asan_report_store_n_noabort",
694 "__asan_report_store1_noabort",
695 "__asan_report_store2_noabort",
696 "__asan_report_store4_noabort",
697 "__asan_report_store8_noabort",
698 "__asan_report_store16_noabort",
700 "__kcsan_check_access",
701 "kcsan_found_watchpoint",
702 "kcsan_setup_watchpoint",
703 "kcsan_check_scoped_accesses",
704 "kcsan_disable_current",
705 "kcsan_enable_current_nowarn",
710 "__tsan_write_range",
721 "__tsan_read_write1",
722 "__tsan_read_write2",
723 "__tsan_read_write4",
724 "__tsan_read_write8",
725 "__tsan_read_write16",
726 "__tsan_atomic8_load",
727 "__tsan_atomic16_load",
728 "__tsan_atomic32_load",
729 "__tsan_atomic64_load",
730 "__tsan_atomic8_store",
731 "__tsan_atomic16_store",
732 "__tsan_atomic32_store",
733 "__tsan_atomic64_store",
734 "__tsan_atomic8_exchange",
735 "__tsan_atomic16_exchange",
736 "__tsan_atomic32_exchange",
737 "__tsan_atomic64_exchange",
738 "__tsan_atomic8_fetch_add",
739 "__tsan_atomic16_fetch_add",
740 "__tsan_atomic32_fetch_add",
741 "__tsan_atomic64_fetch_add",
742 "__tsan_atomic8_fetch_sub",
743 "__tsan_atomic16_fetch_sub",
744 "__tsan_atomic32_fetch_sub",
745 "__tsan_atomic64_fetch_sub",
746 "__tsan_atomic8_fetch_and",
747 "__tsan_atomic16_fetch_and",
748 "__tsan_atomic32_fetch_and",
749 "__tsan_atomic64_fetch_and",
750 "__tsan_atomic8_fetch_or",
751 "__tsan_atomic16_fetch_or",
752 "__tsan_atomic32_fetch_or",
753 "__tsan_atomic64_fetch_or",
754 "__tsan_atomic8_fetch_xor",
755 "__tsan_atomic16_fetch_xor",
756 "__tsan_atomic32_fetch_xor",
757 "__tsan_atomic64_fetch_xor",
758 "__tsan_atomic8_fetch_nand",
759 "__tsan_atomic16_fetch_nand",
760 "__tsan_atomic32_fetch_nand",
761 "__tsan_atomic64_fetch_nand",
762 "__tsan_atomic8_compare_exchange_strong",
763 "__tsan_atomic16_compare_exchange_strong",
764 "__tsan_atomic32_compare_exchange_strong",
765 "__tsan_atomic64_compare_exchange_strong",
766 "__tsan_atomic8_compare_exchange_weak",
767 "__tsan_atomic16_compare_exchange_weak",
768 "__tsan_atomic32_compare_exchange_weak",
769 "__tsan_atomic64_compare_exchange_weak",
770 "__tsan_atomic8_compare_exchange_val",
771 "__tsan_atomic16_compare_exchange_val",
772 "__tsan_atomic32_compare_exchange_val",
773 "__tsan_atomic64_compare_exchange_val",
774 "__tsan_atomic_thread_fence",
775 "__tsan_atomic_signal_fence",
779 "__sanitizer_cov_trace_pc",
780 "__sanitizer_cov_trace_const_cmp1",
781 "__sanitizer_cov_trace_const_cmp2",
782 "__sanitizer_cov_trace_const_cmp4",
783 "__sanitizer_cov_trace_const_cmp8",
784 "__sanitizer_cov_trace_cmp1",
785 "__sanitizer_cov_trace_cmp2",
786 "__sanitizer_cov_trace_cmp4",
787 "__sanitizer_cov_trace_cmp8",
788 "__sanitizer_cov_trace_switch",
790 "ubsan_type_mismatch_common",
791 "__ubsan_handle_type_mismatch",
792 "__ubsan_handle_type_mismatch_v1",
793 "__ubsan_handle_shift_out_of_bounds",
795 "csum_partial_copy_generic",
797 "copy_mc_fragile_handle_tail",
798 "copy_mc_enhanced_fast_string",
799 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
803 static void add_uaccess_safe(struct objtool_file
*file
)
811 for (name
= uaccess_safe_builtin
; *name
; name
++) {
812 func
= find_symbol_by_name(file
->elf
, *name
);
816 func
->uaccess_safe
= true;
821 * FIXME: For now, just ignore any alternatives which add retpolines. This is
822 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
823 * But it at least allows objtool to understand the control flow *around* the
826 static int add_ignore_alternatives(struct objtool_file
*file
)
830 struct instruction
*insn
;
832 sec
= find_section_by_name(file
->elf
, ".rela.discard.ignore_alts");
836 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
837 if (reloc
->sym
->type
!= STT_SECTION
) {
838 WARN("unexpected relocation symbol type in %s", sec
->name
);
842 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
844 WARN("bad .discard.ignore_alts entry");
848 insn
->ignore_alts
= true;
854 __weak
bool arch_is_retpoline(struct symbol
*sym
)
859 #define NEGATIVE_RELOC ((void *)-1L)
861 static struct reloc
*insn_reloc(struct objtool_file
*file
, struct instruction
*insn
)
863 if (insn
->reloc
== NEGATIVE_RELOC
)
867 insn
->reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
868 insn
->offset
, insn
->len
);
870 insn
->reloc
= NEGATIVE_RELOC
;
878 static void remove_insn_ops(struct instruction
*insn
)
880 struct stack_op
*op
, *tmp
;
882 list_for_each_entry_safe(op
, tmp
, &insn
->stack_ops
, list
) {
888 static void annotate_call_site(struct objtool_file
*file
,
889 struct instruction
*insn
, bool sibling
)
891 struct reloc
*reloc
= insn_reloc(file
, insn
);
892 struct symbol
*sym
= insn
->call_dest
;
898 * Alternative replacement code is just template code which is
899 * sometimes copied to the original instruction. For now, don't
900 * annotate it. (In the future we might consider annotating the
901 * original instruction if/when it ever makes sense to do so.)
903 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
906 if (sym
->static_call_tramp
) {
907 list_add_tail(&insn
->call_node
, &file
->static_call_list
);
911 if (sym
->retpoline_thunk
) {
912 list_add_tail(&insn
->call_node
, &file
->retpoline_call_list
);
917 * Many compilers cannot disable KCOV with a function attribute
918 * so they need a little help, NOP out any KCOV calls from noinstr
921 if (insn
->sec
->noinstr
&& sym
->kcov
) {
923 reloc
->type
= R_NONE
;
924 elf_write_reloc(file
->elf
, reloc
);
927 elf_write_insn(file
->elf
, insn
->sec
,
928 insn
->offset
, insn
->len
,
929 sibling
? arch_ret_insn(insn
->len
)
930 : arch_nop_insn(insn
->len
));
932 insn
->type
= sibling
? INSN_RETURN
: INSN_NOP
;
936 if (mcount
&& sym
->fentry
) {
938 WARN_FUNC("Tail call to __fentry__ !?!?", insn
->sec
, insn
->offset
);
941 reloc
->type
= R_NONE
;
942 elf_write_reloc(file
->elf
, reloc
);
945 elf_write_insn(file
->elf
, insn
->sec
,
946 insn
->offset
, insn
->len
,
947 arch_nop_insn(insn
->len
));
949 insn
->type
= INSN_NOP
;
951 list_add_tail(&insn
->call_node
, &file
->mcount_loc_list
);
956 static void add_call_dest(struct objtool_file
*file
, struct instruction
*insn
,
957 struct symbol
*dest
, bool sibling
)
959 insn
->call_dest
= dest
;
964 * Whatever stack impact regular CALLs have, should be undone
965 * by the RETURN of the called function.
967 * Annotated intra-function calls retain the stack_ops but
968 * are converted to JUMP, see read_intra_function_calls().
970 remove_insn_ops(insn
);
972 annotate_call_site(file
, insn
, sibling
);
975 static void add_retpoline_call(struct objtool_file
*file
, struct instruction
*insn
)
978 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
979 * so convert them accordingly.
981 switch (insn
->type
) {
983 insn
->type
= INSN_CALL_DYNAMIC
;
985 case INSN_JUMP_UNCONDITIONAL
:
986 insn
->type
= INSN_JUMP_DYNAMIC
;
988 case INSN_JUMP_CONDITIONAL
:
989 insn
->type
= INSN_JUMP_DYNAMIC_CONDITIONAL
;
995 insn
->retpoline_safe
= true;
998 * Whatever stack impact regular CALLs have, should be undone
999 * by the RETURN of the called function.
1001 * Annotated intra-function calls retain the stack_ops but
1002 * are converted to JUMP, see read_intra_function_calls().
1004 remove_insn_ops(insn
);
1006 annotate_call_site(file
, insn
, false);
1009 * Find the destination instructions for all jumps.
1011 static int add_jump_destinations(struct objtool_file
*file
)
1013 struct instruction
*insn
;
1014 struct reloc
*reloc
;
1015 struct section
*dest_sec
;
1016 unsigned long dest_off
;
1018 for_each_insn(file
, insn
) {
1019 if (!is_static_jump(insn
))
1022 reloc
= insn_reloc(file
, insn
);
1024 dest_sec
= insn
->sec
;
1025 dest_off
= arch_jump_destination(insn
);
1026 } else if (reloc
->sym
->type
== STT_SECTION
) {
1027 dest_sec
= reloc
->sym
->sec
;
1028 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
1029 } else if (reloc
->sym
->retpoline_thunk
) {
1030 add_retpoline_call(file
, insn
);
1032 } else if (insn
->func
) {
1033 /* internal or external sibling call (with reloc) */
1034 add_call_dest(file
, insn
, reloc
->sym
, true);
1036 } else if (reloc
->sym
->sec
->idx
) {
1037 dest_sec
= reloc
->sym
->sec
;
1038 dest_off
= reloc
->sym
->sym
.st_value
+
1039 arch_dest_reloc_offset(reloc
->addend
);
1041 /* non-func asm code jumping to another file */
1045 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
1046 if (!insn
->jump_dest
) {
1049 * This is a special case where an alt instruction
1050 * jumps past the end of the section. These are
1051 * handled later in handle_group_alt().
1053 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
1056 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1057 insn
->sec
, insn
->offset
, dest_sec
->name
,
1063 * Cross-function jump.
1065 if (insn
->func
&& insn
->jump_dest
->func
&&
1066 insn
->func
!= insn
->jump_dest
->func
) {
1069 * For GCC 8+, create parent/child links for any cold
1070 * subfunctions. This is _mostly_ redundant with a
1071 * similar initialization in read_symbols().
1073 * If a function has aliases, we want the *first* such
1074 * function in the symbol table to be the subfunction's
1075 * parent. In that case we overwrite the
1076 * initialization done in read_symbols().
1078 * However this code can't completely replace the
1079 * read_symbols() code because this doesn't detect the
1080 * case where the parent function's only reference to a
1081 * subfunction is through a jump table.
1083 if (!strstr(insn
->func
->name
, ".cold") &&
1084 strstr(insn
->jump_dest
->func
->name
, ".cold")) {
1085 insn
->func
->cfunc
= insn
->jump_dest
->func
;
1086 insn
->jump_dest
->func
->pfunc
= insn
->func
;
1088 } else if (insn
->jump_dest
->func
->pfunc
!= insn
->func
->pfunc
&&
1089 insn
->jump_dest
->offset
== insn
->jump_dest
->func
->offset
) {
1090 /* internal sibling call (without reloc) */
1091 add_call_dest(file
, insn
, insn
->jump_dest
->func
, true);
1099 static struct symbol
*find_call_destination(struct section
*sec
, unsigned long offset
)
1101 struct symbol
*call_dest
;
1103 call_dest
= find_func_by_offset(sec
, offset
);
1105 call_dest
= find_symbol_by_offset(sec
, offset
);
1111 * Find the destination instructions for all calls.
1113 static int add_call_destinations(struct objtool_file
*file
)
1115 struct instruction
*insn
;
1116 unsigned long dest_off
;
1117 struct symbol
*dest
;
1118 struct reloc
*reloc
;
1120 for_each_insn(file
, insn
) {
1121 if (insn
->type
!= INSN_CALL
)
1124 reloc
= insn_reloc(file
, insn
);
1126 dest_off
= arch_jump_destination(insn
);
1127 dest
= find_call_destination(insn
->sec
, dest_off
);
1129 add_call_dest(file
, insn
, dest
, false);
1134 if (!insn
->call_dest
) {
1135 WARN_FUNC("unannotated intra-function call", insn
->sec
, insn
->offset
);
1139 if (insn
->func
&& insn
->call_dest
->type
!= STT_FUNC
) {
1140 WARN_FUNC("unsupported call to non-function",
1141 insn
->sec
, insn
->offset
);
1145 } else if (reloc
->sym
->type
== STT_SECTION
) {
1146 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
1147 dest
= find_call_destination(reloc
->sym
->sec
, dest_off
);
1149 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1150 insn
->sec
, insn
->offset
,
1151 reloc
->sym
->sec
->name
,
1156 add_call_dest(file
, insn
, dest
, false);
1158 } else if (reloc
->sym
->retpoline_thunk
) {
1159 add_retpoline_call(file
, insn
);
1162 add_call_dest(file
, insn
, reloc
->sym
, false);
1169 * The .alternatives section requires some extra special care over and above
1170 * other special sections because alternatives are patched in place.
1172 static int handle_group_alt(struct objtool_file
*file
,
1173 struct special_alt
*special_alt
,
1174 struct instruction
*orig_insn
,
1175 struct instruction
**new_insn
)
1177 struct instruction
*last_orig_insn
, *last_new_insn
= NULL
, *insn
, *nop
= NULL
;
1178 struct alt_group
*orig_alt_group
, *new_alt_group
;
1179 unsigned long dest_off
;
1182 orig_alt_group
= malloc(sizeof(*orig_alt_group
));
1183 if (!orig_alt_group
) {
1184 WARN("malloc failed");
1187 orig_alt_group
->cfi
= calloc(special_alt
->orig_len
,
1188 sizeof(struct cfi_state
*));
1189 if (!orig_alt_group
->cfi
) {
1190 WARN("calloc failed");
1194 last_orig_insn
= NULL
;
1196 sec_for_each_insn_from(file
, insn
) {
1197 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
1200 insn
->alt_group
= orig_alt_group
;
1201 last_orig_insn
= insn
;
1203 orig_alt_group
->orig_group
= NULL
;
1204 orig_alt_group
->first_insn
= orig_insn
;
1205 orig_alt_group
->last_insn
= last_orig_insn
;
1208 new_alt_group
= malloc(sizeof(*new_alt_group
));
1209 if (!new_alt_group
) {
1210 WARN("malloc failed");
1214 if (special_alt
->new_len
< special_alt
->orig_len
) {
1216 * Insert a fake nop at the end to make the replacement
1217 * alt_group the same size as the original. This is needed to
1218 * allow propagate_alt_cfi() to do its magic. When the last
1219 * instruction affects the stack, the instruction after it (the
1220 * nop) will propagate the new state to the shared CFI array.
1222 nop
= malloc(sizeof(*nop
));
1224 WARN("malloc failed");
1227 memset(nop
, 0, sizeof(*nop
));
1228 INIT_LIST_HEAD(&nop
->alts
);
1229 INIT_LIST_HEAD(&nop
->stack_ops
);
1230 init_cfi_state(&nop
->cfi
);
1232 nop
->sec
= special_alt
->new_sec
;
1233 nop
->offset
= special_alt
->new_off
+ special_alt
->new_len
;
1234 nop
->len
= special_alt
->orig_len
- special_alt
->new_len
;
1235 nop
->type
= INSN_NOP
;
1236 nop
->func
= orig_insn
->func
;
1237 nop
->alt_group
= new_alt_group
;
1238 nop
->ignore
= orig_insn
->ignore_alts
;
1241 if (!special_alt
->new_len
) {
1247 sec_for_each_insn_from(file
, insn
) {
1248 struct reloc
*alt_reloc
;
1250 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
1253 last_new_insn
= insn
;
1255 insn
->ignore
= orig_insn
->ignore_alts
;
1256 insn
->func
= orig_insn
->func
;
1257 insn
->alt_group
= new_alt_group
;
1260 * Since alternative replacement code is copy/pasted by the
1261 * kernel after applying relocations, generally such code can't
1262 * have relative-address relocation references to outside the
1263 * .altinstr_replacement section, unless the arch's
1264 * alternatives code can adjust the relative offsets
1267 alt_reloc
= insn_reloc(file
, insn
);
1269 !arch_support_alt_relocation(special_alt
, insn
, alt_reloc
)) {
1271 WARN_FUNC("unsupported relocation in alternatives section",
1272 insn
->sec
, insn
->offset
);
1276 if (!is_static_jump(insn
))
1279 if (!insn
->immediate
)
1282 dest_off
= arch_jump_destination(insn
);
1283 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
1284 insn
->jump_dest
= next_insn_same_sec(file
, last_orig_insn
);
1286 if (!insn
->jump_dest
) {
1287 WARN_FUNC("can't find alternative jump destination",
1288 insn
->sec
, insn
->offset
);
1293 if (!last_new_insn
) {
1294 WARN_FUNC("can't find last new alternative instruction",
1295 special_alt
->new_sec
, special_alt
->new_off
);
1300 list_add(&nop
->list
, &last_new_insn
->list
);
1302 new_alt_group
->orig_group
= orig_alt_group
;
1303 new_alt_group
->first_insn
= *new_insn
;
1304 new_alt_group
->last_insn
= nop
? : last_new_insn
;
1305 new_alt_group
->cfi
= orig_alt_group
->cfi
;
1310 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1311 * If the original instruction is a jump, make the alt entry an effective nop
1312 * by just skipping the original instruction.
1314 static int handle_jump_alt(struct objtool_file
*file
,
1315 struct special_alt
*special_alt
,
1316 struct instruction
*orig_insn
,
1317 struct instruction
**new_insn
)
1319 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
&&
1320 orig_insn
->type
!= INSN_NOP
) {
1322 WARN_FUNC("unsupported instruction at jump label",
1323 orig_insn
->sec
, orig_insn
->offset
);
1327 if (special_alt
->key_addend
& 2) {
1328 struct reloc
*reloc
= insn_reloc(file
, orig_insn
);
1331 reloc
->type
= R_NONE
;
1332 elf_write_reloc(file
->elf
, reloc
);
1334 elf_write_insn(file
->elf
, orig_insn
->sec
,
1335 orig_insn
->offset
, orig_insn
->len
,
1336 arch_nop_insn(orig_insn
->len
));
1337 orig_insn
->type
= INSN_NOP
;
1340 if (orig_insn
->type
== INSN_NOP
) {
1341 if (orig_insn
->len
== 2)
1342 file
->jl_nop_short
++;
1344 file
->jl_nop_long
++;
1349 if (orig_insn
->len
== 2)
1354 *new_insn
= list_next_entry(orig_insn
, list
);
1359 * Read all the special sections which have alternate instructions which can be
1360 * patched in or redirected to at runtime. Each instruction having alternate
1361 * instruction(s) has them added to its insn->alts list, which will be
1362 * traversed in validate_branch().
1364 static int add_special_section_alts(struct objtool_file
*file
)
1366 struct list_head special_alts
;
1367 struct instruction
*orig_insn
, *new_insn
;
1368 struct special_alt
*special_alt
, *tmp
;
1369 struct alternative
*alt
;
1372 ret
= special_get_alts(file
->elf
, &special_alts
);
1376 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
1378 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
1379 special_alt
->orig_off
);
1381 WARN_FUNC("special: can't find orig instruction",
1382 special_alt
->orig_sec
, special_alt
->orig_off
);
1388 if (!special_alt
->group
|| special_alt
->new_len
) {
1389 new_insn
= find_insn(file
, special_alt
->new_sec
,
1390 special_alt
->new_off
);
1392 WARN_FUNC("special: can't find new instruction",
1393 special_alt
->new_sec
,
1394 special_alt
->new_off
);
1400 if (special_alt
->group
) {
1401 if (!special_alt
->orig_len
) {
1402 WARN_FUNC("empty alternative entry",
1403 orig_insn
->sec
, orig_insn
->offset
);
1407 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
1411 } else if (special_alt
->jump_or_nop
) {
1412 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
1418 alt
= malloc(sizeof(*alt
));
1420 WARN("malloc failed");
1425 alt
->insn
= new_insn
;
1426 alt
->skip_orig
= special_alt
->skip_orig
;
1427 orig_insn
->ignore_alts
|= special_alt
->skip_alt
;
1428 list_add_tail(&alt
->list
, &orig_insn
->alts
);
1430 list_del(&special_alt
->list
);
1435 printf("jl\\\tNOP\tJMP\n");
1436 printf("short:\t%ld\t%ld\n", file
->jl_nop_short
, file
->jl_short
);
1437 printf("long:\t%ld\t%ld\n", file
->jl_nop_long
, file
->jl_long
);
1444 static int add_jump_table(struct objtool_file
*file
, struct instruction
*insn
,
1445 struct reloc
*table
)
1447 struct reloc
*reloc
= table
;
1448 struct instruction
*dest_insn
;
1449 struct alternative
*alt
;
1450 struct symbol
*pfunc
= insn
->func
->pfunc
;
1451 unsigned int prev_offset
= 0;
1454 * Each @reloc is a switch table relocation which points to the target
1457 list_for_each_entry_from(reloc
, &table
->sec
->reloc_list
, list
) {
1459 /* Check for the end of the table: */
1460 if (reloc
!= table
&& reloc
->jump_table_start
)
1463 /* Make sure the table entries are consecutive: */
1464 if (prev_offset
&& reloc
->offset
!= prev_offset
+ 8)
1467 /* Detect function pointers from contiguous objects: */
1468 if (reloc
->sym
->sec
== pfunc
->sec
&&
1469 reloc
->addend
== pfunc
->offset
)
1472 dest_insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1476 /* Make sure the destination is in the same function: */
1477 if (!dest_insn
->func
|| dest_insn
->func
->pfunc
!= pfunc
)
1480 alt
= malloc(sizeof(*alt
));
1482 WARN("malloc failed");
1486 alt
->insn
= dest_insn
;
1487 list_add_tail(&alt
->list
, &insn
->alts
);
1488 prev_offset
= reloc
->offset
;
1492 WARN_FUNC("can't find switch jump table",
1493 insn
->sec
, insn
->offset
);
1501 * find_jump_table() - Given a dynamic jump, find the switch jump table
1502 * associated with it.
1504 static struct reloc
*find_jump_table(struct objtool_file
*file
,
1505 struct symbol
*func
,
1506 struct instruction
*insn
)
1508 struct reloc
*table_reloc
;
1509 struct instruction
*dest_insn
, *orig_insn
= insn
;
1512 * Backward search using the @first_jump_src links, these help avoid
1513 * much of the 'in between' code. Which avoids us getting confused by
1517 insn
&& insn
->func
&& insn
->func
->pfunc
== func
;
1518 insn
= insn
->first_jump_src
?: prev_insn_same_sym(file
, insn
)) {
1520 if (insn
!= orig_insn
&& insn
->type
== INSN_JUMP_DYNAMIC
)
1523 /* allow small jumps within the range */
1524 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
1526 (insn
->jump_dest
->offset
<= insn
->offset
||
1527 insn
->jump_dest
->offset
> orig_insn
->offset
))
1530 table_reloc
= arch_find_switch_table(file
, insn
);
1533 dest_insn
= find_insn(file
, table_reloc
->sym
->sec
, table_reloc
->addend
);
1534 if (!dest_insn
|| !dest_insn
->func
|| dest_insn
->func
->pfunc
!= func
)
1544 * First pass: Mark the head of each jump table so that in the next pass,
1545 * we know when a given jump table ends and the next one starts.
1547 static void mark_func_jump_tables(struct objtool_file
*file
,
1548 struct symbol
*func
)
1550 struct instruction
*insn
, *last
= NULL
;
1551 struct reloc
*reloc
;
1553 func_for_each_insn(file
, func
, insn
) {
1558 * Store back-pointers for unconditional forward jumps such
1559 * that find_jump_table() can back-track using those and
1560 * avoid some potentially confusing code.
1562 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
&&
1563 insn
->offset
> last
->offset
&&
1564 insn
->jump_dest
->offset
> insn
->offset
&&
1565 !insn
->jump_dest
->first_jump_src
) {
1567 insn
->jump_dest
->first_jump_src
= insn
;
1568 last
= insn
->jump_dest
;
1571 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
1574 reloc
= find_jump_table(file
, func
, insn
);
1576 reloc
->jump_table_start
= true;
1577 insn
->jump_table
= reloc
;
1582 static int add_func_jump_tables(struct objtool_file
*file
,
1583 struct symbol
*func
)
1585 struct instruction
*insn
;
1588 func_for_each_insn(file
, func
, insn
) {
1589 if (!insn
->jump_table
)
1592 ret
= add_jump_table(file
, insn
, insn
->jump_table
);
1601 * For some switch statements, gcc generates a jump table in the .rodata
1602 * section which contains a list of addresses within the function to jump to.
1603 * This finds these jump tables and adds them to the insn->alts lists.
1605 static int add_jump_table_alts(struct objtool_file
*file
)
1607 struct section
*sec
;
1608 struct symbol
*func
;
1614 for_each_sec(file
, sec
) {
1615 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1616 if (func
->type
!= STT_FUNC
)
1619 mark_func_jump_tables(file
, func
);
1620 ret
= add_func_jump_tables(file
, func
);
1629 static void set_func_state(struct cfi_state
*state
)
1631 state
->cfa
= initial_func_cfi
.cfa
;
1632 memcpy(&state
->regs
, &initial_func_cfi
.regs
,
1633 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
1634 state
->stack_size
= initial_func_cfi
.cfa
.offset
;
1637 static int read_unwind_hints(struct objtool_file
*file
)
1639 struct section
*sec
, *relocsec
;
1640 struct reloc
*reloc
;
1641 struct unwind_hint
*hint
;
1642 struct instruction
*insn
;
1645 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
1649 relocsec
= sec
->reloc
;
1651 WARN("missing .rela.discard.unwind_hints section");
1655 if (sec
->sh
.sh_size
% sizeof(struct unwind_hint
)) {
1656 WARN("struct unwind_hint size mismatch");
1662 for (i
= 0; i
< sec
->sh
.sh_size
/ sizeof(struct unwind_hint
); i
++) {
1663 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1665 reloc
= find_reloc_by_dest(file
->elf
, sec
, i
* sizeof(*hint
));
1667 WARN("can't find reloc for unwind_hints[%d]", i
);
1671 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1673 WARN("can't find insn for unwind_hints[%d]", i
);
1679 if (hint
->type
== UNWIND_HINT_TYPE_FUNC
) {
1680 set_func_state(&insn
->cfi
);
1684 if (arch_decode_hint_reg(insn
, hint
->sp_reg
)) {
1685 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1686 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1690 insn
->cfi
.cfa
.offset
= bswap_if_needed(hint
->sp_offset
);
1691 insn
->cfi
.type
= hint
->type
;
1692 insn
->cfi
.end
= hint
->end
;
1698 static int read_retpoline_hints(struct objtool_file
*file
)
1700 struct section
*sec
;
1701 struct instruction
*insn
;
1702 struct reloc
*reloc
;
1704 sec
= find_section_by_name(file
->elf
, ".rela.discard.retpoline_safe");
1708 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1709 if (reloc
->sym
->type
!= STT_SECTION
) {
1710 WARN("unexpected relocation symbol type in %s", sec
->name
);
1714 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1716 WARN("bad .discard.retpoline_safe entry");
1720 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1721 insn
->type
!= INSN_CALL_DYNAMIC
) {
1722 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1723 insn
->sec
, insn
->offset
);
1727 insn
->retpoline_safe
= true;
1733 static int read_instr_hints(struct objtool_file
*file
)
1735 struct section
*sec
;
1736 struct instruction
*insn
;
1737 struct reloc
*reloc
;
1739 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_end");
1743 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1744 if (reloc
->sym
->type
!= STT_SECTION
) {
1745 WARN("unexpected relocation symbol type in %s", sec
->name
);
1749 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1751 WARN("bad .discard.instr_end entry");
1758 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_begin");
1762 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1763 if (reloc
->sym
->type
!= STT_SECTION
) {
1764 WARN("unexpected relocation symbol type in %s", sec
->name
);
1768 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1770 WARN("bad .discard.instr_begin entry");
1780 static int read_intra_function_calls(struct objtool_file
*file
)
1782 struct instruction
*insn
;
1783 struct section
*sec
;
1784 struct reloc
*reloc
;
1786 sec
= find_section_by_name(file
->elf
, ".rela.discard.intra_function_calls");
1790 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1791 unsigned long dest_off
;
1793 if (reloc
->sym
->type
!= STT_SECTION
) {
1794 WARN("unexpected relocation symbol type in %s",
1799 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1801 WARN("bad .discard.intra_function_call entry");
1805 if (insn
->type
!= INSN_CALL
) {
1806 WARN_FUNC("intra_function_call not a direct call",
1807 insn
->sec
, insn
->offset
);
1812 * Treat intra-function CALLs as JMPs, but with a stack_op.
1813 * See add_call_destinations(), which strips stack_ops from
1816 insn
->type
= INSN_JUMP_UNCONDITIONAL
;
1818 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
1819 insn
->jump_dest
= find_insn(file
, insn
->sec
, dest_off
);
1820 if (!insn
->jump_dest
) {
1821 WARN_FUNC("can't find call dest at %s+0x%lx",
1822 insn
->sec
, insn
->offset
,
1823 insn
->sec
->name
, dest_off
);
1831 static int classify_symbols(struct objtool_file
*file
)
1833 struct section
*sec
;
1834 struct symbol
*func
;
1836 for_each_sec(file
, sec
) {
1837 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1838 if (func
->bind
!= STB_GLOBAL
)
1841 if (!strncmp(func
->name
, STATIC_CALL_TRAMP_PREFIX_STR
,
1842 strlen(STATIC_CALL_TRAMP_PREFIX_STR
)))
1843 func
->static_call_tramp
= true;
1845 if (arch_is_retpoline(func
))
1846 func
->retpoline_thunk
= true;
1848 if (!strcmp(func
->name
, "__fentry__"))
1849 func
->fentry
= true;
1851 if (!strncmp(func
->name
, "__sanitizer_cov_", 16))
1859 static void mark_rodata(struct objtool_file
*file
)
1861 struct section
*sec
;
1865 * Search for the following rodata sections, each of which can
1866 * potentially contain jump tables:
1868 * - .rodata: can contain GCC switch tables
1869 * - .rodata.<func>: same, if -fdata-sections is being used
1870 * - .rodata..c_jump_table: contains C annotated jump tables
1872 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1874 for_each_sec(file
, sec
) {
1875 if (!strncmp(sec
->name
, ".rodata", 7) &&
1876 !strstr(sec
->name
, ".str1.")) {
1882 file
->rodata
= found
;
1885 static int decode_sections(struct objtool_file
*file
)
1891 ret
= decode_instructions(file
);
1895 ret
= add_dead_ends(file
);
1900 add_uaccess_safe(file
);
1902 ret
= add_ignore_alternatives(file
);
1907 * Must be before add_{jump_call}_destination.
1909 ret
= classify_symbols(file
);
1914 * Must be before add_special_section_alts() as that depends on
1915 * jump_dest being set.
1917 ret
= add_jump_destinations(file
);
1921 ret
= add_special_section_alts(file
);
1926 * Must be before add_call_destination(); it changes INSN_CALL to
1929 ret
= read_intra_function_calls(file
);
1933 ret
= add_call_destinations(file
);
1937 ret
= add_jump_table_alts(file
);
1941 ret
= read_unwind_hints(file
);
1945 ret
= read_retpoline_hints(file
);
1949 ret
= read_instr_hints(file
);
1956 static bool is_fentry_call(struct instruction
*insn
)
1958 if (insn
->type
== INSN_CALL
&&
1960 insn
->call_dest
->fentry
)
1966 static bool has_modified_stack_frame(struct instruction
*insn
, struct insn_state
*state
)
1968 struct cfi_state
*cfi
= &state
->cfi
;
1971 if (cfi
->cfa
.base
!= initial_func_cfi
.cfa
.base
|| cfi
->drap
)
1974 if (cfi
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
)
1977 if (cfi
->stack_size
!= initial_func_cfi
.cfa
.offset
)
1980 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1981 if (cfi
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1982 cfi
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1989 static bool check_reg_frame_pos(const struct cfi_reg
*reg
,
1990 int expected_offset
)
1992 return reg
->base
== CFI_CFA
&&
1993 reg
->offset
== expected_offset
;
1996 static bool has_valid_stack_frame(struct insn_state
*state
)
1998 struct cfi_state
*cfi
= &state
->cfi
;
2000 if (cfi
->cfa
.base
== CFI_BP
&&
2001 check_reg_frame_pos(&cfi
->regs
[CFI_BP
], -cfi
->cfa
.offset
) &&
2002 check_reg_frame_pos(&cfi
->regs
[CFI_RA
], -cfi
->cfa
.offset
+ 8))
2005 if (cfi
->drap
&& cfi
->regs
[CFI_BP
].base
== CFI_BP
)
2011 static int update_cfi_state_regs(struct instruction
*insn
,
2012 struct cfi_state
*cfi
,
2013 struct stack_op
*op
)
2015 struct cfi_reg
*cfa
= &cfi
->cfa
;
2017 if (cfa
->base
!= CFI_SP
&& cfa
->base
!= CFI_SP_INDIRECT
)
2021 if (op
->dest
.type
== OP_DEST_PUSH
|| op
->dest
.type
== OP_DEST_PUSHF
)
2025 if (op
->src
.type
== OP_SRC_POP
|| op
->src
.type
== OP_SRC_POPF
)
2028 /* add immediate to sp */
2029 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
2030 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
2031 cfa
->offset
-= op
->src
.offset
;
2036 static void save_reg(struct cfi_state
*cfi
, unsigned char reg
, int base
, int offset
)
2038 if (arch_callee_saved_reg(reg
) &&
2039 cfi
->regs
[reg
].base
== CFI_UNDEFINED
) {
2040 cfi
->regs
[reg
].base
= base
;
2041 cfi
->regs
[reg
].offset
= offset
;
2045 static void restore_reg(struct cfi_state
*cfi
, unsigned char reg
)
2047 cfi
->regs
[reg
].base
= initial_func_cfi
.regs
[reg
].base
;
2048 cfi
->regs
[reg
].offset
= initial_func_cfi
.regs
[reg
].offset
;
2052 * A note about DRAP stack alignment:
2054 * GCC has the concept of a DRAP register, which is used to help keep track of
2055 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2056 * register. The typical DRAP pattern is:
2058 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2059 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2060 * 41 ff 72 f8 pushq -0x8(%r10)
2062 * 48 89 e5 mov %rsp,%rbp
2069 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2072 * There are some variations in the epilogues, like:
2080 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2085 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2086 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2087 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2088 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2090 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2093 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2094 * restored beforehand:
2097 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2098 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2100 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2104 static int update_cfi_state(struct instruction
*insn
,
2105 struct instruction
*next_insn
,
2106 struct cfi_state
*cfi
, struct stack_op
*op
)
2108 struct cfi_reg
*cfa
= &cfi
->cfa
;
2109 struct cfi_reg
*regs
= cfi
->regs
;
2111 /* stack operations don't make sense with an undefined CFA */
2112 if (cfa
->base
== CFI_UNDEFINED
) {
2114 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
2120 if (cfi
->type
== UNWIND_HINT_TYPE_REGS
||
2121 cfi
->type
== UNWIND_HINT_TYPE_REGS_PARTIAL
)
2122 return update_cfi_state_regs(insn
, cfi
, op
);
2124 switch (op
->dest
.type
) {
2127 switch (op
->src
.type
) {
2130 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
2131 cfa
->base
== CFI_SP
&&
2132 check_reg_frame_pos(®s
[CFI_BP
], -cfa
->offset
)) {
2134 /* mov %rsp, %rbp */
2135 cfa
->base
= op
->dest
.reg
;
2136 cfi
->bp_scratch
= false;
2139 else if (op
->src
.reg
== CFI_SP
&&
2140 op
->dest
.reg
== CFI_BP
&& cfi
->drap
) {
2142 /* drap: mov %rsp, %rbp */
2143 regs
[CFI_BP
].base
= CFI_BP
;
2144 regs
[CFI_BP
].offset
= -cfi
->stack_size
;
2145 cfi
->bp_scratch
= false;
2148 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
2153 * This is needed for the rare case where GCC
2160 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
2161 cfi
->vals
[op
->dest
.reg
].offset
= -cfi
->stack_size
;
2164 else if (op
->src
.reg
== CFI_BP
&& op
->dest
.reg
== CFI_SP
&&
2165 (cfa
->base
== CFI_BP
|| cfa
->base
== cfi
->drap_reg
)) {
2170 * Restore the original stack pointer (Clang).
2172 cfi
->stack_size
= -cfi
->regs
[CFI_BP
].offset
;
2175 else if (op
->dest
.reg
== cfa
->base
) {
2177 /* mov %reg, %rsp */
2178 if (cfa
->base
== CFI_SP
&&
2179 cfi
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
2182 * This is needed for the rare case
2183 * where GCC does something dumb like:
2185 * lea 0x8(%rsp), %rcx
2189 cfa
->offset
= -cfi
->vals
[op
->src
.reg
].offset
;
2190 cfi
->stack_size
= cfa
->offset
;
2192 } else if (cfa
->base
== CFI_SP
&&
2193 cfi
->vals
[op
->src
.reg
].base
== CFI_SP_INDIRECT
&&
2194 cfi
->vals
[op
->src
.reg
].offset
== cfa
->offset
) {
2199 * 1: mov %rsp, (%[tos])
2200 * 2: mov %[tos], %rsp
2206 * 1 - places a pointer to the previous
2207 * stack at the Top-of-Stack of the
2210 * 2 - switches to the new stack.
2212 * 3 - pops the Top-of-Stack to restore
2213 * the original stack.
2215 * Note: we set base to SP_INDIRECT
2216 * here and preserve offset. Therefore
2217 * when the unwinder reaches ToS it
2218 * will dereference SP and then add the
2219 * offset to find the next frame, IOW:
2222 cfa
->base
= CFI_SP_INDIRECT
;
2225 cfa
->base
= CFI_UNDEFINED
;
2230 else if (op
->dest
.reg
== CFI_SP
&&
2231 cfi
->vals
[op
->src
.reg
].base
== CFI_SP_INDIRECT
&&
2232 cfi
->vals
[op
->src
.reg
].offset
== cfa
->offset
) {
2235 * The same stack swizzle case 2) as above. But
2236 * because we can't change cfa->base, case 3)
2237 * will become a regular POP. Pretend we're a
2238 * PUSH so things don't go unbalanced.
2240 cfi
->stack_size
+= 8;
2247 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
2250 cfi
->stack_size
-= op
->src
.offset
;
2251 if (cfa
->base
== CFI_SP
)
2252 cfa
->offset
-= op
->src
.offset
;
2256 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
2258 /* lea disp(%rbp), %rsp */
2259 cfi
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
2263 if (!cfi
->drap
&& op
->src
.reg
== CFI_SP
&&
2264 op
->dest
.reg
== CFI_BP
&& cfa
->base
== CFI_SP
&&
2265 check_reg_frame_pos(®s
[CFI_BP
], -cfa
->offset
+ op
->src
.offset
)) {
2267 /* lea disp(%rsp), %rbp */
2269 cfa
->offset
-= op
->src
.offset
;
2270 cfi
->bp_scratch
= false;
2274 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
2276 /* drap: lea disp(%rsp), %drap */
2277 cfi
->drap_reg
= op
->dest
.reg
;
2280 * lea disp(%rsp), %reg
2282 * This is needed for the rare case where GCC
2283 * does something dumb like:
2285 * lea 0x8(%rsp), %rcx
2289 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
2290 cfi
->vals
[op
->dest
.reg
].offset
= \
2291 -cfi
->stack_size
+ op
->src
.offset
;
2296 if (cfi
->drap
&& op
->dest
.reg
== CFI_SP
&&
2297 op
->src
.reg
== cfi
->drap_reg
) {
2299 /* drap: lea disp(%drap), %rsp */
2301 cfa
->offset
= cfi
->stack_size
= -op
->src
.offset
;
2302 cfi
->drap_reg
= CFI_UNDEFINED
;
2307 if (op
->dest
.reg
== cfi
->cfa
.base
&& !(next_insn
&& next_insn
->hint
)) {
2308 WARN_FUNC("unsupported stack register modification",
2309 insn
->sec
, insn
->offset
);
2316 if (op
->dest
.reg
!= CFI_SP
||
2317 (cfi
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
2318 (cfi
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
2319 WARN_FUNC("unsupported stack pointer realignment",
2320 insn
->sec
, insn
->offset
);
2324 if (cfi
->drap_reg
!= CFI_UNDEFINED
) {
2325 /* drap: and imm, %rsp */
2326 cfa
->base
= cfi
->drap_reg
;
2327 cfa
->offset
= cfi
->stack_size
= 0;
2332 * Older versions of GCC (4.8ish) realign the stack
2333 * without DRAP, with a frame pointer.
2340 if (op
->dest
.reg
== CFI_SP
&& cfa
->base
== CFI_SP_INDIRECT
) {
2342 /* pop %rsp; # restore from a stack swizzle */
2347 if (!cfi
->drap
&& op
->dest
.reg
== cfa
->base
) {
2353 if (cfi
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
2354 op
->dest
.reg
== cfi
->drap_reg
&&
2355 cfi
->drap_offset
== -cfi
->stack_size
) {
2357 /* drap: pop %drap */
2358 cfa
->base
= cfi
->drap_reg
;
2360 cfi
->drap_offset
= -1;
2362 } else if (cfi
->stack_size
== -regs
[op
->dest
.reg
].offset
) {
2365 restore_reg(cfi
, op
->dest
.reg
);
2368 cfi
->stack_size
-= 8;
2369 if (cfa
->base
== CFI_SP
)
2374 case OP_SRC_REG_INDIRECT
:
2375 if (!cfi
->drap
&& op
->dest
.reg
== cfa
->base
&&
2376 op
->dest
.reg
== CFI_BP
) {
2378 /* mov disp(%rsp), %rbp */
2380 cfa
->offset
= cfi
->stack_size
;
2383 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2384 op
->src
.offset
== cfi
->drap_offset
) {
2386 /* drap: mov disp(%rbp), %drap */
2387 cfa
->base
= cfi
->drap_reg
;
2389 cfi
->drap_offset
= -1;
2392 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2393 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
2395 /* drap: mov disp(%rbp), %reg */
2396 restore_reg(cfi
, op
->dest
.reg
);
2398 } else if (op
->src
.reg
== cfa
->base
&&
2399 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
2401 /* mov disp(%rbp), %reg */
2402 /* mov disp(%rsp), %reg */
2403 restore_reg(cfi
, op
->dest
.reg
);
2405 } else if (op
->src
.reg
== CFI_SP
&&
2406 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfi
->stack_size
) {
2408 /* mov disp(%rsp), %reg */
2409 restore_reg(cfi
, op
->dest
.reg
);
2415 WARN_FUNC("unknown stack-related instruction",
2416 insn
->sec
, insn
->offset
);
2424 cfi
->stack_size
+= 8;
2425 if (cfa
->base
== CFI_SP
)
2428 if (op
->src
.type
!= OP_SRC_REG
)
2432 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2434 /* drap: push %drap */
2435 cfa
->base
= CFI_BP_INDIRECT
;
2436 cfa
->offset
= -cfi
->stack_size
;
2438 /* save drap so we know when to restore it */
2439 cfi
->drap_offset
= -cfi
->stack_size
;
2441 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== cfi
->drap_reg
) {
2443 /* drap: push %rbp */
2444 cfi
->stack_size
= 0;
2448 /* drap: push %reg */
2449 save_reg(cfi
, op
->src
.reg
, CFI_BP
, -cfi
->stack_size
);
2455 save_reg(cfi
, op
->src
.reg
, CFI_CFA
, -cfi
->stack_size
);
2458 /* detect when asm code uses rbp as a scratch register */
2459 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
2460 cfa
->base
!= CFI_BP
)
2461 cfi
->bp_scratch
= true;
2464 case OP_DEST_REG_INDIRECT
:
2467 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2469 /* drap: mov %drap, disp(%rbp) */
2470 cfa
->base
= CFI_BP_INDIRECT
;
2471 cfa
->offset
= op
->dest
.offset
;
2473 /* save drap offset so we know when to restore it */
2474 cfi
->drap_offset
= op
->dest
.offset
;
2477 /* drap: mov reg, disp(%rbp) */
2478 save_reg(cfi
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
2481 } else if (op
->dest
.reg
== cfa
->base
) {
2483 /* mov reg, disp(%rbp) */
2484 /* mov reg, disp(%rsp) */
2485 save_reg(cfi
, op
->src
.reg
, CFI_CFA
,
2486 op
->dest
.offset
- cfi
->cfa
.offset
);
2488 } else if (op
->dest
.reg
== CFI_SP
) {
2490 /* mov reg, disp(%rsp) */
2491 save_reg(cfi
, op
->src
.reg
, CFI_CFA
,
2492 op
->dest
.offset
- cfi
->stack_size
);
2494 } else if (op
->src
.reg
== CFI_SP
&& op
->dest
.offset
== 0) {
2496 /* mov %rsp, (%reg); # setup a stack swizzle. */
2497 cfi
->vals
[op
->dest
.reg
].base
= CFI_SP_INDIRECT
;
2498 cfi
->vals
[op
->dest
.reg
].offset
= cfa
->offset
;
2504 if (op
->src
.type
!= OP_SRC_POP
&& op
->src
.type
!= OP_SRC_POPF
) {
2505 WARN_FUNC("unknown stack-related memory operation",
2506 insn
->sec
, insn
->offset
);
2511 cfi
->stack_size
-= 8;
2512 if (cfa
->base
== CFI_SP
)
2518 WARN_FUNC("unknown stack-related instruction",
2519 insn
->sec
, insn
->offset
);
2527 * The stack layouts of alternatives instructions can sometimes diverge when
2528 * they have stack modifications. That's fine as long as the potential stack
2529 * layouts don't conflict at any given potential instruction boundary.
2531 * Flatten the CFIs of the different alternative code streams (both original
2532 * and replacement) into a single shared CFI array which can be used to detect
2533 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2535 static int propagate_alt_cfi(struct objtool_file
*file
, struct instruction
*insn
)
2537 struct cfi_state
**alt_cfi
;
2540 if (!insn
->alt_group
)
2543 alt_cfi
= insn
->alt_group
->cfi
;
2544 group_off
= insn
->offset
- insn
->alt_group
->first_insn
->offset
;
2546 if (!alt_cfi
[group_off
]) {
2547 alt_cfi
[group_off
] = &insn
->cfi
;
2549 if (memcmp(alt_cfi
[group_off
], &insn
->cfi
, sizeof(struct cfi_state
))) {
2550 WARN_FUNC("stack layout conflict in alternatives",
2551 insn
->sec
, insn
->offset
);
2559 static int handle_insn_ops(struct instruction
*insn
,
2560 struct instruction
*next_insn
,
2561 struct insn_state
*state
)
2563 struct stack_op
*op
;
2565 list_for_each_entry(op
, &insn
->stack_ops
, list
) {
2567 if (update_cfi_state(insn
, next_insn
, &state
->cfi
, op
))
2570 if (!insn
->alt_group
)
2573 if (op
->dest
.type
== OP_DEST_PUSHF
) {
2574 if (!state
->uaccess_stack
) {
2575 state
->uaccess_stack
= 1;
2576 } else if (state
->uaccess_stack
>> 31) {
2577 WARN_FUNC("PUSHF stack exhausted",
2578 insn
->sec
, insn
->offset
);
2581 state
->uaccess_stack
<<= 1;
2582 state
->uaccess_stack
|= state
->uaccess
;
2585 if (op
->src
.type
== OP_SRC_POPF
) {
2586 if (state
->uaccess_stack
) {
2587 state
->uaccess
= state
->uaccess_stack
& 1;
2588 state
->uaccess_stack
>>= 1;
2589 if (state
->uaccess_stack
== 1)
2590 state
->uaccess_stack
= 0;
2598 static bool insn_cfi_match(struct instruction
*insn
, struct cfi_state
*cfi2
)
2600 struct cfi_state
*cfi1
= &insn
->cfi
;
2603 if (memcmp(&cfi1
->cfa
, &cfi2
->cfa
, sizeof(cfi1
->cfa
))) {
2605 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2606 insn
->sec
, insn
->offset
,
2607 cfi1
->cfa
.base
, cfi1
->cfa
.offset
,
2608 cfi2
->cfa
.base
, cfi2
->cfa
.offset
);
2610 } else if (memcmp(&cfi1
->regs
, &cfi2
->regs
, sizeof(cfi1
->regs
))) {
2611 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
2612 if (!memcmp(&cfi1
->regs
[i
], &cfi2
->regs
[i
],
2613 sizeof(struct cfi_reg
)))
2616 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2617 insn
->sec
, insn
->offset
,
2618 i
, cfi1
->regs
[i
].base
, cfi1
->regs
[i
].offset
,
2619 i
, cfi2
->regs
[i
].base
, cfi2
->regs
[i
].offset
);
2623 } else if (cfi1
->type
!= cfi2
->type
) {
2625 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2626 insn
->sec
, insn
->offset
, cfi1
->type
, cfi2
->type
);
2628 } else if (cfi1
->drap
!= cfi2
->drap
||
2629 (cfi1
->drap
&& cfi1
->drap_reg
!= cfi2
->drap_reg
) ||
2630 (cfi1
->drap
&& cfi1
->drap_offset
!= cfi2
->drap_offset
)) {
2632 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2633 insn
->sec
, insn
->offset
,
2634 cfi1
->drap
, cfi1
->drap_reg
, cfi1
->drap_offset
,
2635 cfi2
->drap
, cfi2
->drap_reg
, cfi2
->drap_offset
);
2643 static inline bool func_uaccess_safe(struct symbol
*func
)
2646 return func
->uaccess_safe
;
2651 static inline const char *call_dest_name(struct instruction
*insn
)
2653 if (insn
->call_dest
)
2654 return insn
->call_dest
->name
;
2659 static inline bool noinstr_call_dest(struct symbol
*func
)
2662 * We can't deal with indirect function calls at present;
2663 * assume they're instrumented.
2669 * If the symbol is from a noinstr section; we good.
2671 if (func
->sec
->noinstr
)
2675 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2676 * something 'BAD' happened. At the risk of taking the machine down,
2677 * let them proceed to get the message out.
2679 if (!strncmp(func
->name
, "__ubsan_handle_", 15))
2685 static int validate_call(struct instruction
*insn
, struct insn_state
*state
)
2687 if (state
->noinstr
&& state
->instr
<= 0 &&
2688 !noinstr_call_dest(insn
->call_dest
)) {
2689 WARN_FUNC("call to %s() leaves .noinstr.text section",
2690 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2694 if (state
->uaccess
&& !func_uaccess_safe(insn
->call_dest
)) {
2695 WARN_FUNC("call to %s() with UACCESS enabled",
2696 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2701 WARN_FUNC("call to %s() with DF set",
2702 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2709 static int validate_sibling_call(struct instruction
*insn
, struct insn_state
*state
)
2711 if (has_modified_stack_frame(insn
, state
)) {
2712 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2713 insn
->sec
, insn
->offset
);
2717 return validate_call(insn
, state
);
2720 static int validate_return(struct symbol
*func
, struct instruction
*insn
, struct insn_state
*state
)
2722 if (state
->noinstr
&& state
->instr
> 0) {
2723 WARN_FUNC("return with instrumentation enabled",
2724 insn
->sec
, insn
->offset
);
2728 if (state
->uaccess
&& !func_uaccess_safe(func
)) {
2729 WARN_FUNC("return with UACCESS enabled",
2730 insn
->sec
, insn
->offset
);
2734 if (!state
->uaccess
&& func_uaccess_safe(func
)) {
2735 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2736 insn
->sec
, insn
->offset
);
2741 WARN_FUNC("return with DF set",
2742 insn
->sec
, insn
->offset
);
2746 if (func
&& has_modified_stack_frame(insn
, state
)) {
2747 WARN_FUNC("return with modified stack frame",
2748 insn
->sec
, insn
->offset
);
2752 if (state
->cfi
.bp_scratch
) {
2753 WARN_FUNC("BP used as a scratch register",
2754 insn
->sec
, insn
->offset
);
2761 static struct instruction
*next_insn_to_validate(struct objtool_file
*file
,
2762 struct instruction
*insn
)
2764 struct alt_group
*alt_group
= insn
->alt_group
;
2767 * Simulate the fact that alternatives are patched in-place. When the
2768 * end of a replacement alt_group is reached, redirect objtool flow to
2769 * the end of the original alt_group.
2771 if (alt_group
&& insn
== alt_group
->last_insn
&& alt_group
->orig_group
)
2772 return next_insn_same_sec(file
, alt_group
->orig_group
->last_insn
);
2774 return next_insn_same_sec(file
, insn
);
2778 * Follow the branch starting at the given instruction, and recursively follow
2779 * any other branches (jumps). Meanwhile, track the frame pointer state at
2780 * each instruction and validate all the rules described in
2781 * tools/objtool/Documentation/stack-validation.txt.
2783 static int validate_branch(struct objtool_file
*file
, struct symbol
*func
,
2784 struct instruction
*insn
, struct insn_state state
)
2786 struct alternative
*alt
;
2787 struct instruction
*next_insn
;
2788 struct section
*sec
;
2795 next_insn
= next_insn_to_validate(file
, insn
);
2797 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
->pfunc
) {
2798 WARN("%s() falls through to next function %s()",
2799 func
->name
, insn
->func
->name
);
2803 if (func
&& insn
->ignore
) {
2804 WARN_FUNC("BUG: why am I validating an ignored function?",
2809 visited
= 1 << state
.uaccess
;
2810 if (insn
->visited
) {
2811 if (!insn
->hint
&& !insn_cfi_match(insn
, &state
.cfi
))
2814 if (insn
->visited
& visited
)
2819 state
.instr
+= insn
->instr
;
2822 state
.cfi
= insn
->cfi
;
2824 insn
->cfi
= state
.cfi
;
2826 insn
->visited
|= visited
;
2828 if (propagate_alt_cfi(file
, insn
))
2831 if (!insn
->ignore_alts
&& !list_empty(&insn
->alts
)) {
2832 bool skip_orig
= false;
2834 list_for_each_entry(alt
, &insn
->alts
, list
) {
2838 ret
= validate_branch(file
, func
, alt
->insn
, state
);
2841 BT_FUNC("(alt)", insn
);
2850 if (handle_insn_ops(insn
, next_insn
, &state
))
2853 switch (insn
->type
) {
2856 return validate_return(func
, insn
, &state
);
2859 case INSN_CALL_DYNAMIC
:
2860 ret
= validate_call(insn
, &state
);
2864 if (!no_fp
&& func
&& !is_fentry_call(insn
) &&
2865 !has_valid_stack_frame(&state
)) {
2866 WARN_FUNC("call without frame pointer save/setup",
2871 if (dead_end_function(file
, insn
->call_dest
))
2876 case INSN_JUMP_CONDITIONAL
:
2877 case INSN_JUMP_UNCONDITIONAL
:
2878 if (is_sibling_call(insn
)) {
2879 ret
= validate_sibling_call(insn
, &state
);
2883 } else if (insn
->jump_dest
) {
2884 ret
= validate_branch(file
, func
,
2885 insn
->jump_dest
, state
);
2888 BT_FUNC("(branch)", insn
);
2893 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
2898 case INSN_JUMP_DYNAMIC
:
2899 case INSN_JUMP_DYNAMIC_CONDITIONAL
:
2900 if (is_sibling_call(insn
)) {
2901 ret
= validate_sibling_call(insn
, &state
);
2906 if (insn
->type
== INSN_JUMP_DYNAMIC
)
2911 case INSN_CONTEXT_SWITCH
:
2912 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
2913 WARN_FUNC("unsupported instruction in callable function",
2920 if (state
.uaccess
) {
2921 WARN_FUNC("recursive UACCESS enable", sec
, insn
->offset
);
2925 state
.uaccess
= true;
2929 if (!state
.uaccess
&& func
) {
2930 WARN_FUNC("redundant UACCESS disable", sec
, insn
->offset
);
2934 if (func_uaccess_safe(func
) && !state
.uaccess_stack
) {
2935 WARN_FUNC("UACCESS-safe disables UACCESS", sec
, insn
->offset
);
2939 state
.uaccess
= false;
2944 WARN_FUNC("recursive STD", sec
, insn
->offset
);
2952 if (!state
.df
&& func
) {
2953 WARN_FUNC("redundant CLD", sec
, insn
->offset
);
2968 if (state
.cfi
.cfa
.base
== CFI_UNDEFINED
)
2970 WARN("%s: unexpected end of section", sec
->name
);
2980 static int validate_unwind_hints(struct objtool_file
*file
, struct section
*sec
)
2982 struct instruction
*insn
;
2983 struct insn_state state
;
2984 int ret
, warnings
= 0;
2989 init_insn_state(&state
, sec
);
2992 insn
= find_insn(file
, sec
, 0);
2996 insn
= list_first_entry(&file
->insn_list
, typeof(*insn
), list
);
2999 while (&insn
->list
!= &file
->insn_list
&& (!sec
|| insn
->sec
== sec
)) {
3000 if (insn
->hint
&& !insn
->visited
) {
3001 ret
= validate_branch(file
, insn
->func
, insn
, state
);
3002 if (ret
&& backtrace
)
3003 BT_FUNC("<=== (hint)", insn
);
3007 insn
= list_next_entry(insn
, list
);
3013 static int validate_retpoline(struct objtool_file
*file
)
3015 struct instruction
*insn
;
3018 for_each_insn(file
, insn
) {
3019 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
3020 insn
->type
!= INSN_CALL_DYNAMIC
)
3023 if (insn
->retpoline_safe
)
3027 * .init.text code is ran before userspace and thus doesn't
3028 * strictly need retpolines, except for modules which are
3029 * loaded late, they very much do need retpoline in their
3032 if (!strcmp(insn
->sec
->name
, ".init.text") && !module
)
3035 WARN_FUNC("indirect %s found in RETPOLINE build",
3036 insn
->sec
, insn
->offset
,
3037 insn
->type
== INSN_JUMP_DYNAMIC
? "jump" : "call");
3045 static bool is_kasan_insn(struct instruction
*insn
)
3047 return (insn
->type
== INSN_CALL
&&
3048 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
3051 static bool is_ubsan_insn(struct instruction
*insn
)
3053 return (insn
->type
== INSN_CALL
&&
3054 !strcmp(insn
->call_dest
->name
,
3055 "__ubsan_handle_builtin_unreachable"));
3058 static bool ignore_unreachable_insn(struct objtool_file
*file
, struct instruction
*insn
)
3061 struct instruction
*prev_insn
;
3063 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
3067 * Ignore any unused exceptions. This can happen when a whitelisted
3068 * function has an exception table entry.
3070 * Also ignore alternative replacement instructions. This can happen
3071 * when a whitelisted function uses one of the ALTERNATIVE macros.
3073 if (!strcmp(insn
->sec
->name
, ".fixup") ||
3074 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
3075 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
3082 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3083 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3084 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3085 * (or occasionally a JMP to UD2).
3087 * It may also insert a UD2 after calling a __noreturn function.
3089 prev_insn
= list_prev_entry(insn
, list
);
3090 if ((prev_insn
->dead_end
|| dead_end_function(file
, prev_insn
->call_dest
)) &&
3091 (insn
->type
== INSN_BUG
||
3092 (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
3093 insn
->jump_dest
&& insn
->jump_dest
->type
== INSN_BUG
)))
3097 * Check if this (or a subsequent) instruction is related to
3098 * CONFIG_UBSAN or CONFIG_KASAN.
3100 * End the search at 5 instructions to avoid going into the weeds.
3102 for (i
= 0; i
< 5; i
++) {
3104 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
3107 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
3108 if (insn
->jump_dest
&&
3109 insn
->jump_dest
->func
== insn
->func
) {
3110 insn
= insn
->jump_dest
;
3117 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
3120 insn
= list_next_entry(insn
, list
);
3126 static int validate_symbol(struct objtool_file
*file
, struct section
*sec
,
3127 struct symbol
*sym
, struct insn_state
*state
)
3129 struct instruction
*insn
;
3133 WARN("%s() is missing an ELF size annotation", sym
->name
);
3137 if (sym
->pfunc
!= sym
|| sym
->alias
!= sym
)
3140 insn
= find_insn(file
, sec
, sym
->offset
);
3141 if (!insn
|| insn
->ignore
|| insn
->visited
)
3144 state
->uaccess
= sym
->uaccess_safe
;
3146 ret
= validate_branch(file
, insn
->func
, insn
, *state
);
3147 if (ret
&& backtrace
)
3148 BT_FUNC("<=== (sym)", insn
);
3152 static int validate_section(struct objtool_file
*file
, struct section
*sec
)
3154 struct insn_state state
;
3155 struct symbol
*func
;
3158 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
3159 if (func
->type
!= STT_FUNC
)
3162 init_insn_state(&state
, sec
);
3163 set_func_state(&state
.cfi
);
3165 warnings
+= validate_symbol(file
, sec
, func
, &state
);
3171 static int validate_vmlinux_functions(struct objtool_file
*file
)
3173 struct section
*sec
;
3176 sec
= find_section_by_name(file
->elf
, ".noinstr.text");
3178 warnings
+= validate_section(file
, sec
);
3179 warnings
+= validate_unwind_hints(file
, sec
);
3182 sec
= find_section_by_name(file
->elf
, ".entry.text");
3184 warnings
+= validate_section(file
, sec
);
3185 warnings
+= validate_unwind_hints(file
, sec
);
3191 static int validate_functions(struct objtool_file
*file
)
3193 struct section
*sec
;
3196 for_each_sec(file
, sec
) {
3197 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
3200 warnings
+= validate_section(file
, sec
);
3206 static int validate_reachable_instructions(struct objtool_file
*file
)
3208 struct instruction
*insn
;
3210 if (file
->ignore_unreachables
)
3213 for_each_insn(file
, insn
) {
3214 if (insn
->visited
|| ignore_unreachable_insn(file
, insn
))
3217 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
3224 int check(struct objtool_file
*file
)
3226 int ret
, warnings
= 0;
3228 arch_initial_func_cfi_state(&initial_func_cfi
);
3230 ret
= decode_sections(file
);
3235 if (list_empty(&file
->insn_list
))
3238 if (vmlinux
&& !validate_dup
) {
3239 ret
= validate_vmlinux_functions(file
);
3248 ret
= validate_retpoline(file
);
3254 ret
= validate_functions(file
);
3259 ret
= validate_unwind_hints(file
, NULL
);
3265 ret
= validate_reachable_instructions(file
);
3271 ret
= create_static_call_sections(file
);
3277 ret
= create_retpoline_sites_sections(file
);
3284 ret
= create_mcount_loc_sections(file
);
3292 * For now, don't fail the kernel build on fatal warnings. These
3293 * errors are still fairly common due to the growing matrix of
3294 * supported toolchains and their recent pace of change.