1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
17 #include <linux/objtool.h>
18 #include <linux/hashtable.h>
19 #include <linux/kernel.h>
20 #include <linux/static_call_types.h>
22 #define FAKE_JUMP_OFFSET -1
25 struct list_head list
;
26 struct instruction
*insn
;
30 struct cfi_init_state initial_func_cfi
;
32 struct instruction
*find_insn(struct objtool_file
*file
,
33 struct section
*sec
, unsigned long offset
)
35 struct instruction
*insn
;
37 hash_for_each_possible(file
->insn_hash
, insn
, hash
, sec_offset_hash(sec
, offset
)) {
38 if (insn
->sec
== sec
&& insn
->offset
== offset
)
45 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
46 struct instruction
*insn
)
48 struct instruction
*next
= list_next_entry(insn
, list
);
50 if (!next
|| &next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
56 static struct instruction
*next_insn_same_func(struct objtool_file
*file
,
57 struct instruction
*insn
)
59 struct instruction
*next
= list_next_entry(insn
, list
);
60 struct symbol
*func
= insn
->func
;
65 if (&next
->list
!= &file
->insn_list
&& next
->func
== func
)
68 /* Check if we're already in the subfunction: */
69 if (func
== func
->cfunc
)
72 /* Move to the subfunction: */
73 return find_insn(file
, func
->cfunc
->sec
, func
->cfunc
->offset
);
76 static struct instruction
*prev_insn_same_sym(struct objtool_file
*file
,
77 struct instruction
*insn
)
79 struct instruction
*prev
= list_prev_entry(insn
, list
);
81 if (&prev
->list
!= &file
->insn_list
&& prev
->func
== insn
->func
)
87 #define func_for_each_insn(file, func, insn) \
88 for (insn = find_insn(file, func->sec, func->offset); \
90 insn = next_insn_same_func(file, insn))
92 #define sym_for_each_insn(file, sym, insn) \
93 for (insn = find_insn(file, sym->sec, sym->offset); \
94 insn && &insn->list != &file->insn_list && \
95 insn->sec == sym->sec && \
96 insn->offset < sym->offset + sym->len; \
97 insn = list_next_entry(insn, list))
99 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
100 for (insn = list_prev_entry(insn, list); \
101 &insn->list != &file->insn_list && \
102 insn->sec == sym->sec && insn->offset >= sym->offset; \
103 insn = list_prev_entry(insn, list))
105 #define sec_for_each_insn_from(file, insn) \
106 for (; insn; insn = next_insn_same_sec(file, insn))
108 #define sec_for_each_insn_continue(file, insn) \
109 for (insn = next_insn_same_sec(file, insn); insn; \
110 insn = next_insn_same_sec(file, insn))
112 static bool is_sibling_call(struct instruction
*insn
)
114 /* An indirect jump is either a sibling call or a jump to a table. */
115 if (insn
->type
== INSN_JUMP_DYNAMIC
)
116 return list_empty(&insn
->alts
);
118 if (!is_static_jump(insn
))
121 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
122 return !!insn
->call_dest
;
126 * This checks to see if the given function is a "noreturn" function.
128 * For global functions which are outside the scope of this object file, we
129 * have to keep a manual list of them.
131 * For local functions, we have to detect them manually by simply looking for
132 * the lack of a return instruction.
134 static bool __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
138 struct instruction
*insn
;
142 * Unfortunately these have to be hard coded because the noreturn
143 * attribute isn't provided in ELF data.
145 static const char * const global_noreturns
[] = {
150 "__module_put_and_exit",
156 "machine_real_restart",
157 "rewind_stack_do_exit",
158 "kunit_try_catch_throw",
164 if (func
->bind
== STB_WEAK
)
167 if (func
->bind
== STB_GLOBAL
)
168 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
169 if (!strcmp(func
->name
, global_noreturns
[i
]))
175 insn
= find_insn(file
, func
->sec
, func
->offset
);
179 func_for_each_insn(file
, func
, insn
) {
182 if (insn
->type
== INSN_RETURN
)
190 * A function can have a sibling call instead of a return. In that
191 * case, the function's dead-end status depends on whether the target
192 * of the sibling call returns.
194 func_for_each_insn(file
, func
, insn
) {
195 if (is_sibling_call(insn
)) {
196 struct instruction
*dest
= insn
->jump_dest
;
199 /* sibling call to another file */
202 /* local sibling call */
203 if (recursion
== 5) {
205 * Infinite recursion: two functions have
206 * sibling calls to each other. This is a very
207 * rare case. It means they aren't dead ends.
212 return __dead_end_function(file
, dest
->func
, recursion
+1);
219 static bool dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
221 return __dead_end_function(file
, func
, 0);
224 static void init_cfi_state(struct cfi_state
*cfi
)
228 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
229 cfi
->regs
[i
].base
= CFI_UNDEFINED
;
230 cfi
->vals
[i
].base
= CFI_UNDEFINED
;
232 cfi
->cfa
.base
= CFI_UNDEFINED
;
233 cfi
->drap_reg
= CFI_UNDEFINED
;
234 cfi
->drap_offset
= -1;
237 static void init_insn_state(struct insn_state
*state
, struct section
*sec
)
239 memset(state
, 0, sizeof(*state
));
240 init_cfi_state(&state
->cfi
);
243 * We need the full vmlinux for noinstr validation, otherwise we can
244 * not correctly determine insn->call_dest->sec (external symbols do
245 * not have a section).
248 state
->noinstr
= sec
->noinstr
;
252 * Call the arch-specific instruction decoder for all the instructions and add
253 * them to the global instruction list.
255 static int decode_instructions(struct objtool_file
*file
)
259 unsigned long offset
;
260 struct instruction
*insn
;
261 unsigned long nr_insns
= 0;
264 for_each_sec(file
, sec
) {
266 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
269 if (strcmp(sec
->name
, ".altinstr_replacement") &&
270 strcmp(sec
->name
, ".altinstr_aux") &&
271 strncmp(sec
->name
, ".discard.", 9))
274 if (!strcmp(sec
->name
, ".noinstr.text") ||
275 !strcmp(sec
->name
, ".entry.text"))
278 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
279 insn
= malloc(sizeof(*insn
));
281 WARN("malloc failed");
284 memset(insn
, 0, sizeof(*insn
));
285 INIT_LIST_HEAD(&insn
->alts
);
286 INIT_LIST_HEAD(&insn
->stack_ops
);
287 init_cfi_state(&insn
->cfi
);
290 insn
->offset
= offset
;
292 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
294 &insn
->len
, &insn
->type
,
300 hash_add(file
->insn_hash
, &insn
->hash
, sec_offset_hash(sec
, insn
->offset
));
301 list_add_tail(&insn
->list
, &file
->insn_list
);
305 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
306 if (func
->type
!= STT_FUNC
|| func
->alias
!= func
)
309 if (!find_insn(file
, sec
, func
->offset
)) {
310 WARN("%s(): can't find starting instruction",
315 sym_for_each_insn(file
, func
, insn
)
321 printf("nr_insns: %lu\n", nr_insns
);
330 static struct instruction
*find_last_insn(struct objtool_file
*file
,
333 struct instruction
*insn
= NULL
;
335 unsigned int end
= (sec
->len
> 10) ? sec
->len
- 10 : 0;
337 for (offset
= sec
->len
- 1; offset
>= end
&& !insn
; offset
--)
338 insn
= find_insn(file
, sec
, offset
);
344 * Mark "ud2" instructions and manually annotated dead ends.
346 static int add_dead_ends(struct objtool_file
*file
)
350 struct instruction
*insn
;
353 * By default, "ud2" is a dead end unless otherwise annotated, because
354 * GCC 7 inserts it for certain divide-by-zero cases.
356 for_each_insn(file
, insn
)
357 if (insn
->type
== INSN_BUG
)
358 insn
->dead_end
= true;
361 * Check for manually annotated dead ends.
363 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
367 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
368 if (reloc
->sym
->type
!= STT_SECTION
) {
369 WARN("unexpected relocation symbol type in %s", sec
->name
);
372 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
374 insn
= list_prev_entry(insn
, list
);
375 else if (reloc
->addend
== reloc
->sym
->sec
->len
) {
376 insn
= find_last_insn(file
, reloc
->sym
->sec
);
378 WARN("can't find unreachable insn at %s+0x%x",
379 reloc
->sym
->sec
->name
, reloc
->addend
);
383 WARN("can't find unreachable insn at %s+0x%x",
384 reloc
->sym
->sec
->name
, reloc
->addend
);
388 insn
->dead_end
= true;
393 * These manually annotated reachable checks are needed for GCC 4.4,
394 * where the Linux unreachable() macro isn't supported. In that case
395 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
398 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
402 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
403 if (reloc
->sym
->type
!= STT_SECTION
) {
404 WARN("unexpected relocation symbol type in %s", sec
->name
);
407 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
409 insn
= list_prev_entry(insn
, list
);
410 else if (reloc
->addend
== reloc
->sym
->sec
->len
) {
411 insn
= find_last_insn(file
, reloc
->sym
->sec
);
413 WARN("can't find reachable insn at %s+0x%x",
414 reloc
->sym
->sec
->name
, reloc
->addend
);
418 WARN("can't find reachable insn at %s+0x%x",
419 reloc
->sym
->sec
->name
, reloc
->addend
);
423 insn
->dead_end
= false;
429 static int create_static_call_sections(struct objtool_file
*file
)
431 struct section
*sec
, *reloc_sec
;
433 struct static_call_site
*site
;
434 struct instruction
*insn
;
435 struct symbol
*key_sym
;
436 char *key_name
, *tmp
;
439 sec
= find_section_by_name(file
->elf
, ".static_call_sites");
441 INIT_LIST_HEAD(&file
->static_call_list
);
442 WARN("file already has .static_call_sites section, skipping");
446 if (list_empty(&file
->static_call_list
))
450 list_for_each_entry(insn
, &file
->static_call_list
, static_call_node
)
453 sec
= elf_create_section(file
->elf
, ".static_call_sites", SHF_WRITE
,
454 sizeof(struct static_call_site
), idx
);
458 reloc_sec
= elf_create_reloc_section(file
->elf
, sec
, SHT_RELA
);
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 reloc
= malloc(sizeof(*reloc
));
474 memset(reloc
, 0, sizeof(*reloc
));
475 reloc
->sym
= insn
->sec
->sym
;
476 reloc
->addend
= insn
->offset
;
477 reloc
->type
= R_X86_64_PC32
;
478 reloc
->offset
= idx
* sizeof(struct static_call_site
);
479 reloc
->sec
= reloc_sec
;
480 elf_add_reloc(file
->elf
, reloc
);
482 /* find key symbol */
483 key_name
= strdup(insn
->call_dest
->name
);
488 if (strncmp(key_name
, STATIC_CALL_TRAMP_PREFIX_STR
,
489 STATIC_CALL_TRAMP_PREFIX_LEN
)) {
490 WARN("static_call: trampoline name malformed: %s", key_name
);
493 tmp
= key_name
+ STATIC_CALL_TRAMP_PREFIX_LEN
- STATIC_CALL_KEY_PREFIX_LEN
;
494 memcpy(tmp
, STATIC_CALL_KEY_PREFIX_STR
, STATIC_CALL_KEY_PREFIX_LEN
);
496 key_sym
= find_symbol_by_name(file
->elf
, tmp
);
498 WARN("static_call: can't find static_call_key symbol: %s", tmp
);
503 /* populate reloc for 'key' */
504 reloc
= malloc(sizeof(*reloc
));
509 memset(reloc
, 0, sizeof(*reloc
));
510 reloc
->sym
= key_sym
;
511 reloc
->addend
= is_sibling_call(insn
) ? STATIC_CALL_SITE_TAIL
: 0;
512 reloc
->type
= R_X86_64_PC32
;
513 reloc
->offset
= idx
* sizeof(struct static_call_site
) + 4;
514 reloc
->sec
= reloc_sec
;
515 elf_add_reloc(file
->elf
, reloc
);
520 if (elf_rebuild_reloc_section(file
->elf
, reloc_sec
))
527 * Warnings shouldn't be reported for ignored functions.
529 static void add_ignores(struct objtool_file
*file
)
531 struct instruction
*insn
;
536 sec
= find_section_by_name(file
->elf
, ".rela.discard.func_stack_frame_non_standard");
540 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
541 switch (reloc
->sym
->type
) {
547 func
= find_func_by_offset(reloc
->sym
->sec
, reloc
->addend
);
553 WARN("unexpected relocation symbol type in %s: %d", sec
->name
, reloc
->sym
->type
);
557 func_for_each_insn(file
, func
, insn
)
563 * This is a whitelist of functions that is allowed to be called with AC set.
564 * The list is meant to be minimal and only contains compiler instrumentation
565 * ABI and a few functions used to implement *_{to,from}_user() functions.
567 * These functions must not directly change AC, but may PUSHF/POPF.
569 static const char *uaccess_safe_builtin
[] = {
572 "check_memory_region",
573 /* KASAN out-of-line */
574 "__asan_loadN_noabort",
575 "__asan_load1_noabort",
576 "__asan_load2_noabort",
577 "__asan_load4_noabort",
578 "__asan_load8_noabort",
579 "__asan_load16_noabort",
580 "__asan_storeN_noabort",
581 "__asan_store1_noabort",
582 "__asan_store2_noabort",
583 "__asan_store4_noabort",
584 "__asan_store8_noabort",
585 "__asan_store16_noabort",
586 "__kasan_check_read",
587 "__kasan_check_write",
589 "__asan_report_load_n_noabort",
590 "__asan_report_load1_noabort",
591 "__asan_report_load2_noabort",
592 "__asan_report_load4_noabort",
593 "__asan_report_load8_noabort",
594 "__asan_report_load16_noabort",
595 "__asan_report_store_n_noabort",
596 "__asan_report_store1_noabort",
597 "__asan_report_store2_noabort",
598 "__asan_report_store4_noabort",
599 "__asan_report_store8_noabort",
600 "__asan_report_store16_noabort",
602 "__kcsan_check_access",
603 "kcsan_found_watchpoint",
604 "kcsan_setup_watchpoint",
605 "kcsan_check_scoped_accesses",
606 "kcsan_disable_current",
607 "kcsan_enable_current_nowarn",
612 "__tsan_write_range",
623 "__tsan_read_write1",
624 "__tsan_read_write2",
625 "__tsan_read_write4",
626 "__tsan_read_write8",
627 "__tsan_read_write16",
628 "__tsan_atomic8_load",
629 "__tsan_atomic16_load",
630 "__tsan_atomic32_load",
631 "__tsan_atomic64_load",
632 "__tsan_atomic8_store",
633 "__tsan_atomic16_store",
634 "__tsan_atomic32_store",
635 "__tsan_atomic64_store",
636 "__tsan_atomic8_exchange",
637 "__tsan_atomic16_exchange",
638 "__tsan_atomic32_exchange",
639 "__tsan_atomic64_exchange",
640 "__tsan_atomic8_fetch_add",
641 "__tsan_atomic16_fetch_add",
642 "__tsan_atomic32_fetch_add",
643 "__tsan_atomic64_fetch_add",
644 "__tsan_atomic8_fetch_sub",
645 "__tsan_atomic16_fetch_sub",
646 "__tsan_atomic32_fetch_sub",
647 "__tsan_atomic64_fetch_sub",
648 "__tsan_atomic8_fetch_and",
649 "__tsan_atomic16_fetch_and",
650 "__tsan_atomic32_fetch_and",
651 "__tsan_atomic64_fetch_and",
652 "__tsan_atomic8_fetch_or",
653 "__tsan_atomic16_fetch_or",
654 "__tsan_atomic32_fetch_or",
655 "__tsan_atomic64_fetch_or",
656 "__tsan_atomic8_fetch_xor",
657 "__tsan_atomic16_fetch_xor",
658 "__tsan_atomic32_fetch_xor",
659 "__tsan_atomic64_fetch_xor",
660 "__tsan_atomic8_fetch_nand",
661 "__tsan_atomic16_fetch_nand",
662 "__tsan_atomic32_fetch_nand",
663 "__tsan_atomic64_fetch_nand",
664 "__tsan_atomic8_compare_exchange_strong",
665 "__tsan_atomic16_compare_exchange_strong",
666 "__tsan_atomic32_compare_exchange_strong",
667 "__tsan_atomic64_compare_exchange_strong",
668 "__tsan_atomic8_compare_exchange_weak",
669 "__tsan_atomic16_compare_exchange_weak",
670 "__tsan_atomic32_compare_exchange_weak",
671 "__tsan_atomic64_compare_exchange_weak",
672 "__tsan_atomic8_compare_exchange_val",
673 "__tsan_atomic16_compare_exchange_val",
674 "__tsan_atomic32_compare_exchange_val",
675 "__tsan_atomic64_compare_exchange_val",
676 "__tsan_atomic_thread_fence",
677 "__tsan_atomic_signal_fence",
681 "__sanitizer_cov_trace_pc",
682 "__sanitizer_cov_trace_const_cmp1",
683 "__sanitizer_cov_trace_const_cmp2",
684 "__sanitizer_cov_trace_const_cmp4",
685 "__sanitizer_cov_trace_const_cmp8",
686 "__sanitizer_cov_trace_cmp1",
687 "__sanitizer_cov_trace_cmp2",
688 "__sanitizer_cov_trace_cmp4",
689 "__sanitizer_cov_trace_cmp8",
690 "__sanitizer_cov_trace_switch",
692 "ubsan_type_mismatch_common",
693 "__ubsan_handle_type_mismatch",
694 "__ubsan_handle_type_mismatch_v1",
695 "__ubsan_handle_shift_out_of_bounds",
697 "csum_partial_copy_generic",
699 "copy_mc_fragile_handle_tail",
700 "copy_mc_enhanced_fast_string",
701 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
705 static void add_uaccess_safe(struct objtool_file
*file
)
713 for (name
= uaccess_safe_builtin
; *name
; name
++) {
714 func
= find_symbol_by_name(file
->elf
, *name
);
718 func
->uaccess_safe
= true;
723 * FIXME: For now, just ignore any alternatives which add retpolines. This is
724 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
725 * But it at least allows objtool to understand the control flow *around* the
728 static int add_ignore_alternatives(struct objtool_file
*file
)
732 struct instruction
*insn
;
734 sec
= find_section_by_name(file
->elf
, ".rela.discard.ignore_alts");
738 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
739 if (reloc
->sym
->type
!= STT_SECTION
) {
740 WARN("unexpected relocation symbol type in %s", sec
->name
);
744 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
746 WARN("bad .discard.ignore_alts entry");
750 insn
->ignore_alts
= true;
757 * Find the destination instructions for all jumps.
759 static int add_jump_destinations(struct objtool_file
*file
)
761 struct instruction
*insn
;
763 struct section
*dest_sec
;
764 unsigned long dest_off
;
766 for_each_insn(file
, insn
) {
767 if (!is_static_jump(insn
))
770 if (insn
->offset
== FAKE_JUMP_OFFSET
)
773 reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
774 insn
->offset
, insn
->len
);
776 dest_sec
= insn
->sec
;
777 dest_off
= arch_jump_destination(insn
);
778 } else if (reloc
->sym
->type
== STT_SECTION
) {
779 dest_sec
= reloc
->sym
->sec
;
780 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
781 } else if (reloc
->sym
->sec
->idx
) {
782 dest_sec
= reloc
->sym
->sec
;
783 dest_off
= reloc
->sym
->sym
.st_value
+
784 arch_dest_reloc_offset(reloc
->addend
);
785 } else if (strstr(reloc
->sym
->name
, "_indirect_thunk_")) {
787 * Retpoline jumps are really dynamic jumps in
788 * disguise, so convert them accordingly.
790 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
791 insn
->type
= INSN_JUMP_DYNAMIC
;
793 insn
->type
= INSN_JUMP_DYNAMIC_CONDITIONAL
;
795 insn
->retpoline_safe
= true;
798 /* external sibling call */
799 insn
->call_dest
= reloc
->sym
;
800 if (insn
->call_dest
->static_call_tramp
) {
801 list_add_tail(&insn
->static_call_node
,
802 &file
->static_call_list
);
807 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
808 if (!insn
->jump_dest
) {
811 * This is a special case where an alt instruction
812 * jumps past the end of the section. These are
813 * handled later in handle_group_alt().
815 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
818 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
819 insn
->sec
, insn
->offset
, dest_sec
->name
,
825 * Cross-function jump.
827 if (insn
->func
&& insn
->jump_dest
->func
&&
828 insn
->func
!= insn
->jump_dest
->func
) {
831 * For GCC 8+, create parent/child links for any cold
832 * subfunctions. This is _mostly_ redundant with a
833 * similar initialization in read_symbols().
835 * If a function has aliases, we want the *first* such
836 * function in the symbol table to be the subfunction's
837 * parent. In that case we overwrite the
838 * initialization done in read_symbols().
840 * However this code can't completely replace the
841 * read_symbols() code because this doesn't detect the
842 * case where the parent function's only reference to a
843 * subfunction is through a jump table.
845 if (!strstr(insn
->func
->name
, ".cold.") &&
846 strstr(insn
->jump_dest
->func
->name
, ".cold.")) {
847 insn
->func
->cfunc
= insn
->jump_dest
->func
;
848 insn
->jump_dest
->func
->pfunc
= insn
->func
;
850 } else if (insn
->jump_dest
->func
->pfunc
!= insn
->func
->pfunc
&&
851 insn
->jump_dest
->offset
== insn
->jump_dest
->func
->offset
) {
853 /* internal sibling call */
854 insn
->call_dest
= insn
->jump_dest
->func
;
855 if (insn
->call_dest
->static_call_tramp
) {
856 list_add_tail(&insn
->static_call_node
,
857 &file
->static_call_list
);
866 static void remove_insn_ops(struct instruction
*insn
)
868 struct stack_op
*op
, *tmp
;
870 list_for_each_entry_safe(op
, tmp
, &insn
->stack_ops
, list
) {
876 static struct symbol
*find_call_destination(struct section
*sec
, unsigned long offset
)
878 struct symbol
*call_dest
;
880 call_dest
= find_func_by_offset(sec
, offset
);
882 call_dest
= find_symbol_by_offset(sec
, offset
);
888 * Find the destination instructions for all calls.
890 static int add_call_destinations(struct objtool_file
*file
)
892 struct instruction
*insn
;
893 unsigned long dest_off
;
896 for_each_insn(file
, insn
) {
897 if (insn
->type
!= INSN_CALL
)
900 reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
901 insn
->offset
, insn
->len
);
903 dest_off
= arch_jump_destination(insn
);
904 insn
->call_dest
= find_call_destination(insn
->sec
, dest_off
);
909 if (!insn
->call_dest
) {
910 WARN_FUNC("unannotated intra-function call", insn
->sec
, insn
->offset
);
914 if (insn
->func
&& insn
->call_dest
->type
!= STT_FUNC
) {
915 WARN_FUNC("unsupported call to non-function",
916 insn
->sec
, insn
->offset
);
920 } else if (reloc
->sym
->type
== STT_SECTION
) {
921 dest_off
= arch_dest_reloc_offset(reloc
->addend
);
922 insn
->call_dest
= find_call_destination(reloc
->sym
->sec
,
924 if (!insn
->call_dest
) {
925 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
926 insn
->sec
, insn
->offset
,
927 reloc
->sym
->sec
->name
,
932 insn
->call_dest
= reloc
->sym
;
935 * Many compilers cannot disable KCOV with a function attribute
936 * so they need a little help, NOP out any KCOV calls from noinstr
939 if (insn
->sec
->noinstr
&&
940 !strncmp(insn
->call_dest
->name
, "__sanitizer_cov_", 16)) {
942 reloc
->type
= R_NONE
;
943 elf_write_reloc(file
->elf
, reloc
);
946 elf_write_insn(file
->elf
, insn
->sec
,
947 insn
->offset
, insn
->len
,
948 arch_nop_insn(insn
->len
));
949 insn
->type
= INSN_NOP
;
953 * Whatever stack impact regular CALLs have, should be undone
954 * by the RETURN of the called function.
956 * Annotated intra-function calls retain the stack_ops but
957 * are converted to JUMP, see read_intra_function_calls().
959 remove_insn_ops(insn
);
966 * The .alternatives section requires some extra special care, over and above
967 * what other special sections require:
969 * 1. Because alternatives are patched in-place, we need to insert a fake jump
970 * instruction at the end so that validate_branch() skips all the original
971 * replaced instructions when validating the new instruction path.
973 * 2. An added wrinkle is that the new instruction length might be zero. In
974 * that case the old instructions are replaced with noops. We simulate that
975 * by creating a fake jump as the only new instruction.
977 * 3. In some cases, the alternative section includes an instruction which
978 * conditionally jumps to the _end_ of the entry. We have to modify these
979 * jumps' destinations to point back to .text rather than the end of the
980 * entry in .altinstr_replacement.
982 static int handle_group_alt(struct objtool_file
*file
,
983 struct special_alt
*special_alt
,
984 struct instruction
*orig_insn
,
985 struct instruction
**new_insn
)
987 static unsigned int alt_group_next_index
= 1;
988 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
= NULL
;
989 unsigned int alt_group
= alt_group_next_index
++;
990 unsigned long dest_off
;
992 last_orig_insn
= NULL
;
994 sec_for_each_insn_from(file
, insn
) {
995 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
998 insn
->alt_group
= alt_group
;
999 last_orig_insn
= insn
;
1002 if (next_insn_same_sec(file
, last_orig_insn
)) {
1003 fake_jump
= malloc(sizeof(*fake_jump
));
1005 WARN("malloc failed");
1008 memset(fake_jump
, 0, sizeof(*fake_jump
));
1009 INIT_LIST_HEAD(&fake_jump
->alts
);
1010 INIT_LIST_HEAD(&fake_jump
->stack_ops
);
1011 init_cfi_state(&fake_jump
->cfi
);
1013 fake_jump
->sec
= special_alt
->new_sec
;
1014 fake_jump
->offset
= FAKE_JUMP_OFFSET
;
1015 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
1016 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
1017 fake_jump
->func
= orig_insn
->func
;
1020 if (!special_alt
->new_len
) {
1022 WARN("%s: empty alternative at end of section",
1023 special_alt
->orig_sec
->name
);
1027 *new_insn
= fake_jump
;
1031 last_new_insn
= NULL
;
1032 alt_group
= alt_group_next_index
++;
1034 sec_for_each_insn_from(file
, insn
) {
1035 struct reloc
*alt_reloc
;
1037 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
1040 last_new_insn
= insn
;
1042 insn
->ignore
= orig_insn
->ignore_alts
;
1043 insn
->func
= orig_insn
->func
;
1044 insn
->alt_group
= alt_group
;
1047 * Since alternative replacement code is copy/pasted by the
1048 * kernel after applying relocations, generally such code can't
1049 * have relative-address relocation references to outside the
1050 * .altinstr_replacement section, unless the arch's
1051 * alternatives code can adjust the relative offsets
1054 alt_reloc
= find_reloc_by_dest_range(file
->elf
, insn
->sec
,
1055 insn
->offset
, insn
->len
);
1057 !arch_support_alt_relocation(special_alt
, insn
, alt_reloc
)) {
1059 WARN_FUNC("unsupported relocation in alternatives section",
1060 insn
->sec
, insn
->offset
);
1064 if (!is_static_jump(insn
))
1067 if (!insn
->immediate
)
1070 dest_off
= arch_jump_destination(insn
);
1071 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
) {
1073 WARN("%s: alternative jump to end of section",
1074 special_alt
->orig_sec
->name
);
1077 insn
->jump_dest
= fake_jump
;
1080 if (!insn
->jump_dest
) {
1081 WARN_FUNC("can't find alternative jump destination",
1082 insn
->sec
, insn
->offset
);
1087 if (!last_new_insn
) {
1088 WARN_FUNC("can't find last new alternative instruction",
1089 special_alt
->new_sec
, special_alt
->new_off
);
1094 list_add(&fake_jump
->list
, &last_new_insn
->list
);
1100 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1101 * If the original instruction is a jump, make the alt entry an effective nop
1102 * by just skipping the original instruction.
1104 static int handle_jump_alt(struct objtool_file
*file
,
1105 struct special_alt
*special_alt
,
1106 struct instruction
*orig_insn
,
1107 struct instruction
**new_insn
)
1109 if (orig_insn
->type
== INSN_NOP
)
1112 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
1113 WARN_FUNC("unsupported instruction at jump label",
1114 orig_insn
->sec
, orig_insn
->offset
);
1118 *new_insn
= list_next_entry(orig_insn
, list
);
1123 * Read all the special sections which have alternate instructions which can be
1124 * patched in or redirected to at runtime. Each instruction having alternate
1125 * instruction(s) has them added to its insn->alts list, which will be
1126 * traversed in validate_branch().
1128 static int add_special_section_alts(struct objtool_file
*file
)
1130 struct list_head special_alts
;
1131 struct instruction
*orig_insn
, *new_insn
;
1132 struct special_alt
*special_alt
, *tmp
;
1133 struct alternative
*alt
;
1136 ret
= special_get_alts(file
->elf
, &special_alts
);
1140 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
1142 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
1143 special_alt
->orig_off
);
1145 WARN_FUNC("special: can't find orig instruction",
1146 special_alt
->orig_sec
, special_alt
->orig_off
);
1152 if (!special_alt
->group
|| special_alt
->new_len
) {
1153 new_insn
= find_insn(file
, special_alt
->new_sec
,
1154 special_alt
->new_off
);
1156 WARN_FUNC("special: can't find new instruction",
1157 special_alt
->new_sec
,
1158 special_alt
->new_off
);
1164 if (special_alt
->group
) {
1165 if (!special_alt
->orig_len
) {
1166 WARN_FUNC("empty alternative entry",
1167 orig_insn
->sec
, orig_insn
->offset
);
1171 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
1175 } else if (special_alt
->jump_or_nop
) {
1176 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
1182 alt
= malloc(sizeof(*alt
));
1184 WARN("malloc failed");
1189 alt
->insn
= new_insn
;
1190 alt
->skip_orig
= special_alt
->skip_orig
;
1191 orig_insn
->ignore_alts
|= special_alt
->skip_alt
;
1192 list_add_tail(&alt
->list
, &orig_insn
->alts
);
1194 list_del(&special_alt
->list
);
1202 static int add_jump_table(struct objtool_file
*file
, struct instruction
*insn
,
1203 struct reloc
*table
)
1205 struct reloc
*reloc
= table
;
1206 struct instruction
*dest_insn
;
1207 struct alternative
*alt
;
1208 struct symbol
*pfunc
= insn
->func
->pfunc
;
1209 unsigned int prev_offset
= 0;
1212 * Each @reloc is a switch table relocation which points to the target
1215 list_for_each_entry_from(reloc
, &table
->sec
->reloc_list
, list
) {
1217 /* Check for the end of the table: */
1218 if (reloc
!= table
&& reloc
->jump_table_start
)
1221 /* Make sure the table entries are consecutive: */
1222 if (prev_offset
&& reloc
->offset
!= prev_offset
+ 8)
1225 /* Detect function pointers from contiguous objects: */
1226 if (reloc
->sym
->sec
== pfunc
->sec
&&
1227 reloc
->addend
== pfunc
->offset
)
1230 dest_insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1234 /* Make sure the destination is in the same function: */
1235 if (!dest_insn
->func
|| dest_insn
->func
->pfunc
!= pfunc
)
1238 alt
= malloc(sizeof(*alt
));
1240 WARN("malloc failed");
1244 alt
->insn
= dest_insn
;
1245 list_add_tail(&alt
->list
, &insn
->alts
);
1246 prev_offset
= reloc
->offset
;
1250 WARN_FUNC("can't find switch jump table",
1251 insn
->sec
, insn
->offset
);
1259 * find_jump_table() - Given a dynamic jump, find the switch jump table
1260 * associated with it.
1262 static struct reloc
*find_jump_table(struct objtool_file
*file
,
1263 struct symbol
*func
,
1264 struct instruction
*insn
)
1266 struct reloc
*table_reloc
;
1267 struct instruction
*dest_insn
, *orig_insn
= insn
;
1270 * Backward search using the @first_jump_src links, these help avoid
1271 * much of the 'in between' code. Which avoids us getting confused by
1275 insn
&& insn
->func
&& insn
->func
->pfunc
== func
;
1276 insn
= insn
->first_jump_src
?: prev_insn_same_sym(file
, insn
)) {
1278 if (insn
!= orig_insn
&& insn
->type
== INSN_JUMP_DYNAMIC
)
1281 /* allow small jumps within the range */
1282 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
1284 (insn
->jump_dest
->offset
<= insn
->offset
||
1285 insn
->jump_dest
->offset
> orig_insn
->offset
))
1288 table_reloc
= arch_find_switch_table(file
, insn
);
1291 dest_insn
= find_insn(file
, table_reloc
->sym
->sec
, table_reloc
->addend
);
1292 if (!dest_insn
|| !dest_insn
->func
|| dest_insn
->func
->pfunc
!= func
)
1302 * First pass: Mark the head of each jump table so that in the next pass,
1303 * we know when a given jump table ends and the next one starts.
1305 static void mark_func_jump_tables(struct objtool_file
*file
,
1306 struct symbol
*func
)
1308 struct instruction
*insn
, *last
= NULL
;
1309 struct reloc
*reloc
;
1311 func_for_each_insn(file
, func
, insn
) {
1316 * Store back-pointers for unconditional forward jumps such
1317 * that find_jump_table() can back-track using those and
1318 * avoid some potentially confusing code.
1320 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
&&
1321 insn
->offset
> last
->offset
&&
1322 insn
->jump_dest
->offset
> insn
->offset
&&
1323 !insn
->jump_dest
->first_jump_src
) {
1325 insn
->jump_dest
->first_jump_src
= insn
;
1326 last
= insn
->jump_dest
;
1329 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
1332 reloc
= find_jump_table(file
, func
, insn
);
1334 reloc
->jump_table_start
= true;
1335 insn
->jump_table
= reloc
;
1340 static int add_func_jump_tables(struct objtool_file
*file
,
1341 struct symbol
*func
)
1343 struct instruction
*insn
;
1346 func_for_each_insn(file
, func
, insn
) {
1347 if (!insn
->jump_table
)
1350 ret
= add_jump_table(file
, insn
, insn
->jump_table
);
1359 * For some switch statements, gcc generates a jump table in the .rodata
1360 * section which contains a list of addresses within the function to jump to.
1361 * This finds these jump tables and adds them to the insn->alts lists.
1363 static int add_jump_table_alts(struct objtool_file
*file
)
1365 struct section
*sec
;
1366 struct symbol
*func
;
1372 for_each_sec(file
, sec
) {
1373 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1374 if (func
->type
!= STT_FUNC
)
1377 mark_func_jump_tables(file
, func
);
1378 ret
= add_func_jump_tables(file
, func
);
1387 static int read_unwind_hints(struct objtool_file
*file
)
1389 struct section
*sec
, *relocsec
;
1390 struct reloc
*reloc
;
1391 struct unwind_hint
*hint
;
1392 struct instruction
*insn
;
1393 struct cfi_reg
*cfa
;
1396 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
1400 relocsec
= sec
->reloc
;
1402 WARN("missing .rela.discard.unwind_hints section");
1406 if (sec
->len
% sizeof(struct unwind_hint
)) {
1407 WARN("struct unwind_hint size mismatch");
1413 for (i
= 0; i
< sec
->len
/ sizeof(struct unwind_hint
); i
++) {
1414 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1416 reloc
= find_reloc_by_dest(file
->elf
, sec
, i
* sizeof(*hint
));
1418 WARN("can't find reloc for unwind_hints[%d]", i
);
1422 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1424 WARN("can't find insn for unwind_hints[%d]", i
);
1428 cfa
= &insn
->cfi
.cfa
;
1430 if (hint
->type
== UNWIND_HINT_TYPE_RET_OFFSET
) {
1431 insn
->ret_offset
= hint
->sp_offset
;
1437 if (arch_decode_hint_reg(insn
, hint
->sp_reg
)) {
1438 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1439 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1443 cfa
->offset
= hint
->sp_offset
;
1444 insn
->cfi
.type
= hint
->type
;
1445 insn
->cfi
.end
= hint
->end
;
1451 static int read_retpoline_hints(struct objtool_file
*file
)
1453 struct section
*sec
;
1454 struct instruction
*insn
;
1455 struct reloc
*reloc
;
1457 sec
= find_section_by_name(file
->elf
, ".rela.discard.retpoline_safe");
1461 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1462 if (reloc
->sym
->type
!= STT_SECTION
) {
1463 WARN("unexpected relocation symbol type in %s", sec
->name
);
1467 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1469 WARN("bad .discard.retpoline_safe entry");
1473 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1474 insn
->type
!= INSN_CALL_DYNAMIC
) {
1475 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1476 insn
->sec
, insn
->offset
);
1480 insn
->retpoline_safe
= true;
1486 static int read_instr_hints(struct objtool_file
*file
)
1488 struct section
*sec
;
1489 struct instruction
*insn
;
1490 struct reloc
*reloc
;
1492 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_end");
1496 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1497 if (reloc
->sym
->type
!= STT_SECTION
) {
1498 WARN("unexpected relocation symbol type in %s", sec
->name
);
1502 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1504 WARN("bad .discard.instr_end entry");
1511 sec
= find_section_by_name(file
->elf
, ".rela.discard.instr_begin");
1515 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1516 if (reloc
->sym
->type
!= STT_SECTION
) {
1517 WARN("unexpected relocation symbol type in %s", sec
->name
);
1521 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1523 WARN("bad .discard.instr_begin entry");
1533 static int read_intra_function_calls(struct objtool_file
*file
)
1535 struct instruction
*insn
;
1536 struct section
*sec
;
1537 struct reloc
*reloc
;
1539 sec
= find_section_by_name(file
->elf
, ".rela.discard.intra_function_calls");
1543 list_for_each_entry(reloc
, &sec
->reloc_list
, list
) {
1544 unsigned long dest_off
;
1546 if (reloc
->sym
->type
!= STT_SECTION
) {
1547 WARN("unexpected relocation symbol type in %s",
1552 insn
= find_insn(file
, reloc
->sym
->sec
, reloc
->addend
);
1554 WARN("bad .discard.intra_function_call entry");
1558 if (insn
->type
!= INSN_CALL
) {
1559 WARN_FUNC("intra_function_call not a direct call",
1560 insn
->sec
, insn
->offset
);
1565 * Treat intra-function CALLs as JMPs, but with a stack_op.
1566 * See add_call_destinations(), which strips stack_ops from
1569 insn
->type
= INSN_JUMP_UNCONDITIONAL
;
1571 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
1572 insn
->jump_dest
= find_insn(file
, insn
->sec
, dest_off
);
1573 if (!insn
->jump_dest
) {
1574 WARN_FUNC("can't find call dest at %s+0x%lx",
1575 insn
->sec
, insn
->offset
,
1576 insn
->sec
->name
, dest_off
);
1584 static int read_static_call_tramps(struct objtool_file
*file
)
1586 struct section
*sec
;
1587 struct symbol
*func
;
1589 for_each_sec(file
, sec
) {
1590 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1591 if (func
->bind
== STB_GLOBAL
&&
1592 !strncmp(func
->name
, STATIC_CALL_TRAMP_PREFIX_STR
,
1593 strlen(STATIC_CALL_TRAMP_PREFIX_STR
)))
1594 func
->static_call_tramp
= true;
1601 static void mark_rodata(struct objtool_file
*file
)
1603 struct section
*sec
;
1607 * Search for the following rodata sections, each of which can
1608 * potentially contain jump tables:
1610 * - .rodata: can contain GCC switch tables
1611 * - .rodata.<func>: same, if -fdata-sections is being used
1612 * - .rodata..c_jump_table: contains C annotated jump tables
1614 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1616 for_each_sec(file
, sec
) {
1617 if (!strncmp(sec
->name
, ".rodata", 7) &&
1618 !strstr(sec
->name
, ".str1.")) {
1624 file
->rodata
= found
;
1627 static int decode_sections(struct objtool_file
*file
)
1633 ret
= decode_instructions(file
);
1637 ret
= add_dead_ends(file
);
1642 add_uaccess_safe(file
);
1644 ret
= add_ignore_alternatives(file
);
1648 ret
= read_static_call_tramps(file
);
1652 ret
= add_jump_destinations(file
);
1656 ret
= add_special_section_alts(file
);
1660 ret
= read_intra_function_calls(file
);
1664 ret
= add_call_destinations(file
);
1668 ret
= add_jump_table_alts(file
);
1672 ret
= read_unwind_hints(file
);
1676 ret
= read_retpoline_hints(file
);
1680 ret
= read_instr_hints(file
);
1687 static bool is_fentry_call(struct instruction
*insn
)
1689 if (insn
->type
== INSN_CALL
&& insn
->call_dest
&&
1690 insn
->call_dest
->type
== STT_NOTYPE
&&
1691 !strcmp(insn
->call_dest
->name
, "__fentry__"))
1697 static bool has_modified_stack_frame(struct instruction
*insn
, struct insn_state
*state
)
1699 u8 ret_offset
= insn
->ret_offset
;
1700 struct cfi_state
*cfi
= &state
->cfi
;
1703 if (cfi
->cfa
.base
!= initial_func_cfi
.cfa
.base
|| cfi
->drap
)
1706 if (cfi
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
+ ret_offset
)
1709 if (cfi
->stack_size
!= initial_func_cfi
.cfa
.offset
+ ret_offset
)
1713 * If there is a ret offset hint then don't check registers
1714 * because a callee-saved register might have been pushed on
1720 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1721 if (cfi
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1722 cfi
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1729 static bool has_valid_stack_frame(struct insn_state
*state
)
1731 struct cfi_state
*cfi
= &state
->cfi
;
1733 if (cfi
->cfa
.base
== CFI_BP
&& cfi
->regs
[CFI_BP
].base
== CFI_CFA
&&
1734 cfi
->regs
[CFI_BP
].offset
== -16)
1737 if (cfi
->drap
&& cfi
->regs
[CFI_BP
].base
== CFI_BP
)
1743 static int update_cfi_state_regs(struct instruction
*insn
,
1744 struct cfi_state
*cfi
,
1745 struct stack_op
*op
)
1747 struct cfi_reg
*cfa
= &cfi
->cfa
;
1749 if (cfa
->base
!= CFI_SP
&& cfa
->base
!= CFI_SP_INDIRECT
)
1753 if (op
->dest
.type
== OP_DEST_PUSH
|| op
->dest
.type
== OP_DEST_PUSHF
)
1757 if (op
->src
.type
== OP_SRC_POP
|| op
->src
.type
== OP_SRC_POPF
)
1760 /* add immediate to sp */
1761 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
1762 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
1763 cfa
->offset
-= op
->src
.offset
;
1768 static void save_reg(struct cfi_state
*cfi
, unsigned char reg
, int base
, int offset
)
1770 if (arch_callee_saved_reg(reg
) &&
1771 cfi
->regs
[reg
].base
== CFI_UNDEFINED
) {
1772 cfi
->regs
[reg
].base
= base
;
1773 cfi
->regs
[reg
].offset
= offset
;
1777 static void restore_reg(struct cfi_state
*cfi
, unsigned char reg
)
1779 cfi
->regs
[reg
].base
= initial_func_cfi
.regs
[reg
].base
;
1780 cfi
->regs
[reg
].offset
= initial_func_cfi
.regs
[reg
].offset
;
1784 * A note about DRAP stack alignment:
1786 * GCC has the concept of a DRAP register, which is used to help keep track of
1787 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1788 * register. The typical DRAP pattern is:
1790 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1791 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1792 * 41 ff 72 f8 pushq -0x8(%r10)
1794 * 48 89 e5 mov %rsp,%rbp
1801 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1804 * There are some variations in the epilogues, like:
1812 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1817 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1818 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1819 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1820 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1822 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1825 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1826 * restored beforehand:
1829 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1830 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1832 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1836 static int update_cfi_state(struct instruction
*insn
, struct cfi_state
*cfi
,
1837 struct stack_op
*op
)
1839 struct cfi_reg
*cfa
= &cfi
->cfa
;
1840 struct cfi_reg
*regs
= cfi
->regs
;
1842 /* stack operations don't make sense with an undefined CFA */
1843 if (cfa
->base
== CFI_UNDEFINED
) {
1845 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1851 if (cfi
->type
== UNWIND_HINT_TYPE_REGS
||
1852 cfi
->type
== UNWIND_HINT_TYPE_REGS_PARTIAL
)
1853 return update_cfi_state_regs(insn
, cfi
, op
);
1855 switch (op
->dest
.type
) {
1858 switch (op
->src
.type
) {
1861 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
1862 cfa
->base
== CFI_SP
&&
1863 regs
[CFI_BP
].base
== CFI_CFA
&&
1864 regs
[CFI_BP
].offset
== -cfa
->offset
) {
1866 /* mov %rsp, %rbp */
1867 cfa
->base
= op
->dest
.reg
;
1868 cfi
->bp_scratch
= false;
1871 else if (op
->src
.reg
== CFI_SP
&&
1872 op
->dest
.reg
== CFI_BP
&& cfi
->drap
) {
1874 /* drap: mov %rsp, %rbp */
1875 regs
[CFI_BP
].base
= CFI_BP
;
1876 regs
[CFI_BP
].offset
= -cfi
->stack_size
;
1877 cfi
->bp_scratch
= false;
1880 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1885 * This is needed for the rare case where GCC
1892 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1893 cfi
->vals
[op
->dest
.reg
].offset
= -cfi
->stack_size
;
1896 else if (op
->src
.reg
== CFI_BP
&& op
->dest
.reg
== CFI_SP
&&
1897 cfa
->base
== CFI_BP
) {
1902 * Restore the original stack pointer (Clang).
1904 cfi
->stack_size
= -cfi
->regs
[CFI_BP
].offset
;
1907 else if (op
->dest
.reg
== cfa
->base
) {
1909 /* mov %reg, %rsp */
1910 if (cfa
->base
== CFI_SP
&&
1911 cfi
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
1914 * This is needed for the rare case
1915 * where GCC does something dumb like:
1917 * lea 0x8(%rsp), %rcx
1921 cfa
->offset
= -cfi
->vals
[op
->src
.reg
].offset
;
1922 cfi
->stack_size
= cfa
->offset
;
1925 cfa
->base
= CFI_UNDEFINED
;
1933 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
1936 cfi
->stack_size
-= op
->src
.offset
;
1937 if (cfa
->base
== CFI_SP
)
1938 cfa
->offset
-= op
->src
.offset
;
1942 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
1944 /* lea disp(%rbp), %rsp */
1945 cfi
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
1949 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1951 /* drap: lea disp(%rsp), %drap */
1952 cfi
->drap_reg
= op
->dest
.reg
;
1955 * lea disp(%rsp), %reg
1957 * This is needed for the rare case where GCC
1958 * does something dumb like:
1960 * lea 0x8(%rsp), %rcx
1964 cfi
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1965 cfi
->vals
[op
->dest
.reg
].offset
= \
1966 -cfi
->stack_size
+ op
->src
.offset
;
1971 if (cfi
->drap
&& op
->dest
.reg
== CFI_SP
&&
1972 op
->src
.reg
== cfi
->drap_reg
) {
1974 /* drap: lea disp(%drap), %rsp */
1976 cfa
->offset
= cfi
->stack_size
= -op
->src
.offset
;
1977 cfi
->drap_reg
= CFI_UNDEFINED
;
1982 if (op
->dest
.reg
== cfi
->cfa
.base
) {
1983 WARN_FUNC("unsupported stack register modification",
1984 insn
->sec
, insn
->offset
);
1991 if (op
->dest
.reg
!= CFI_SP
||
1992 (cfi
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
1993 (cfi
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
1994 WARN_FUNC("unsupported stack pointer realignment",
1995 insn
->sec
, insn
->offset
);
1999 if (cfi
->drap_reg
!= CFI_UNDEFINED
) {
2000 /* drap: and imm, %rsp */
2001 cfa
->base
= cfi
->drap_reg
;
2002 cfa
->offset
= cfi
->stack_size
= 0;
2007 * Older versions of GCC (4.8ish) realign the stack
2008 * without DRAP, with a frame pointer.
2015 if (!cfi
->drap
&& op
->dest
.reg
== cfa
->base
) {
2021 if (cfi
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
2022 op
->dest
.reg
== cfi
->drap_reg
&&
2023 cfi
->drap_offset
== -cfi
->stack_size
) {
2025 /* drap: pop %drap */
2026 cfa
->base
= cfi
->drap_reg
;
2028 cfi
->drap_offset
= -1;
2030 } else if (regs
[op
->dest
.reg
].offset
== -cfi
->stack_size
) {
2033 restore_reg(cfi
, op
->dest
.reg
);
2036 cfi
->stack_size
-= 8;
2037 if (cfa
->base
== CFI_SP
)
2042 case OP_SRC_REG_INDIRECT
:
2043 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2044 op
->src
.offset
== cfi
->drap_offset
) {
2046 /* drap: mov disp(%rbp), %drap */
2047 cfa
->base
= cfi
->drap_reg
;
2049 cfi
->drap_offset
= -1;
2052 if (cfi
->drap
&& op
->src
.reg
== CFI_BP
&&
2053 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
2055 /* drap: mov disp(%rbp), %reg */
2056 restore_reg(cfi
, op
->dest
.reg
);
2058 } else if (op
->src
.reg
== cfa
->base
&&
2059 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
2061 /* mov disp(%rbp), %reg */
2062 /* mov disp(%rsp), %reg */
2063 restore_reg(cfi
, op
->dest
.reg
);
2069 WARN_FUNC("unknown stack-related instruction",
2070 insn
->sec
, insn
->offset
);
2078 cfi
->stack_size
+= 8;
2079 if (cfa
->base
== CFI_SP
)
2082 if (op
->src
.type
!= OP_SRC_REG
)
2086 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2088 /* drap: push %drap */
2089 cfa
->base
= CFI_BP_INDIRECT
;
2090 cfa
->offset
= -cfi
->stack_size
;
2092 /* save drap so we know when to restore it */
2093 cfi
->drap_offset
= -cfi
->stack_size
;
2095 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== cfi
->drap_reg
) {
2097 /* drap: push %rbp */
2098 cfi
->stack_size
= 0;
2102 /* drap: push %reg */
2103 save_reg(cfi
, op
->src
.reg
, CFI_BP
, -cfi
->stack_size
);
2109 save_reg(cfi
, op
->src
.reg
, CFI_CFA
, -cfi
->stack_size
);
2112 /* detect when asm code uses rbp as a scratch register */
2113 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
2114 cfa
->base
!= CFI_BP
)
2115 cfi
->bp_scratch
= true;
2118 case OP_DEST_REG_INDIRECT
:
2121 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== cfi
->drap_reg
) {
2123 /* drap: mov %drap, disp(%rbp) */
2124 cfa
->base
= CFI_BP_INDIRECT
;
2125 cfa
->offset
= op
->dest
.offset
;
2127 /* save drap offset so we know when to restore it */
2128 cfi
->drap_offset
= op
->dest
.offset
;
2131 /* drap: mov reg, disp(%rbp) */
2132 save_reg(cfi
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
2135 } else if (op
->dest
.reg
== cfa
->base
) {
2137 /* mov reg, disp(%rbp) */
2138 /* mov reg, disp(%rsp) */
2139 save_reg(cfi
, op
->src
.reg
, CFI_CFA
,
2140 op
->dest
.offset
- cfi
->cfa
.offset
);
2146 if ((!cfi
->drap
&& cfa
->base
!= CFI_BP
) ||
2147 (cfi
->drap
&& cfa
->base
!= cfi
->drap_reg
)) {
2148 WARN_FUNC("leave instruction with modified stack frame",
2149 insn
->sec
, insn
->offset
);
2153 /* leave (mov %rbp, %rsp; pop %rbp) */
2155 cfi
->stack_size
= -cfi
->regs
[CFI_BP
].offset
- 8;
2156 restore_reg(cfi
, CFI_BP
);
2166 if (op
->src
.type
!= OP_SRC_POP
&& op
->src
.type
!= OP_SRC_POPF
) {
2167 WARN_FUNC("unknown stack-related memory operation",
2168 insn
->sec
, insn
->offset
);
2173 cfi
->stack_size
-= 8;
2174 if (cfa
->base
== CFI_SP
)
2180 WARN_FUNC("unknown stack-related instruction",
2181 insn
->sec
, insn
->offset
);
2188 static int handle_insn_ops(struct instruction
*insn
, struct insn_state
*state
)
2190 struct stack_op
*op
;
2192 list_for_each_entry(op
, &insn
->stack_ops
, list
) {
2193 struct cfi_state old_cfi
= state
->cfi
;
2196 res
= update_cfi_state(insn
, &state
->cfi
, op
);
2200 if (insn
->alt_group
&& memcmp(&state
->cfi
, &old_cfi
, sizeof(struct cfi_state
))) {
2201 WARN_FUNC("alternative modifies stack", insn
->sec
, insn
->offset
);
2205 if (op
->dest
.type
== OP_DEST_PUSHF
) {
2206 if (!state
->uaccess_stack
) {
2207 state
->uaccess_stack
= 1;
2208 } else if (state
->uaccess_stack
>> 31) {
2209 WARN_FUNC("PUSHF stack exhausted",
2210 insn
->sec
, insn
->offset
);
2213 state
->uaccess_stack
<<= 1;
2214 state
->uaccess_stack
|= state
->uaccess
;
2217 if (op
->src
.type
== OP_SRC_POPF
) {
2218 if (state
->uaccess_stack
) {
2219 state
->uaccess
= state
->uaccess_stack
& 1;
2220 state
->uaccess_stack
>>= 1;
2221 if (state
->uaccess_stack
== 1)
2222 state
->uaccess_stack
= 0;
2230 static bool insn_cfi_match(struct instruction
*insn
, struct cfi_state
*cfi2
)
2232 struct cfi_state
*cfi1
= &insn
->cfi
;
2235 if (memcmp(&cfi1
->cfa
, &cfi2
->cfa
, sizeof(cfi1
->cfa
))) {
2237 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2238 insn
->sec
, insn
->offset
,
2239 cfi1
->cfa
.base
, cfi1
->cfa
.offset
,
2240 cfi2
->cfa
.base
, cfi2
->cfa
.offset
);
2242 } else if (memcmp(&cfi1
->regs
, &cfi2
->regs
, sizeof(cfi1
->regs
))) {
2243 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
2244 if (!memcmp(&cfi1
->regs
[i
], &cfi2
->regs
[i
],
2245 sizeof(struct cfi_reg
)))
2248 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2249 insn
->sec
, insn
->offset
,
2250 i
, cfi1
->regs
[i
].base
, cfi1
->regs
[i
].offset
,
2251 i
, cfi2
->regs
[i
].base
, cfi2
->regs
[i
].offset
);
2255 } else if (cfi1
->type
!= cfi2
->type
) {
2257 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2258 insn
->sec
, insn
->offset
, cfi1
->type
, cfi2
->type
);
2260 } else if (cfi1
->drap
!= cfi2
->drap
||
2261 (cfi1
->drap
&& cfi1
->drap_reg
!= cfi2
->drap_reg
) ||
2262 (cfi1
->drap
&& cfi1
->drap_offset
!= cfi2
->drap_offset
)) {
2264 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2265 insn
->sec
, insn
->offset
,
2266 cfi1
->drap
, cfi1
->drap_reg
, cfi1
->drap_offset
,
2267 cfi2
->drap
, cfi2
->drap_reg
, cfi2
->drap_offset
);
2275 static inline bool func_uaccess_safe(struct symbol
*func
)
2278 return func
->uaccess_safe
;
2283 static inline const char *call_dest_name(struct instruction
*insn
)
2285 if (insn
->call_dest
)
2286 return insn
->call_dest
->name
;
2291 static inline bool noinstr_call_dest(struct symbol
*func
)
2294 * We can't deal with indirect function calls at present;
2295 * assume they're instrumented.
2301 * If the symbol is from a noinstr section; we good.
2303 if (func
->sec
->noinstr
)
2307 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2308 * something 'BAD' happened. At the risk of taking the machine down,
2309 * let them proceed to get the message out.
2311 if (!strncmp(func
->name
, "__ubsan_handle_", 15))
2317 static int validate_call(struct instruction
*insn
, struct insn_state
*state
)
2319 if (state
->noinstr
&& state
->instr
<= 0 &&
2320 !noinstr_call_dest(insn
->call_dest
)) {
2321 WARN_FUNC("call to %s() leaves .noinstr.text section",
2322 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2326 if (state
->uaccess
&& !func_uaccess_safe(insn
->call_dest
)) {
2327 WARN_FUNC("call to %s() with UACCESS enabled",
2328 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2333 WARN_FUNC("call to %s() with DF set",
2334 insn
->sec
, insn
->offset
, call_dest_name(insn
));
2341 static int validate_sibling_call(struct instruction
*insn
, struct insn_state
*state
)
2343 if (has_modified_stack_frame(insn
, state
)) {
2344 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2345 insn
->sec
, insn
->offset
);
2349 return validate_call(insn
, state
);
2352 static int validate_return(struct symbol
*func
, struct instruction
*insn
, struct insn_state
*state
)
2354 if (state
->noinstr
&& state
->instr
> 0) {
2355 WARN_FUNC("return with instrumentation enabled",
2356 insn
->sec
, insn
->offset
);
2360 if (state
->uaccess
&& !func_uaccess_safe(func
)) {
2361 WARN_FUNC("return with UACCESS enabled",
2362 insn
->sec
, insn
->offset
);
2366 if (!state
->uaccess
&& func_uaccess_safe(func
)) {
2367 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2368 insn
->sec
, insn
->offset
);
2373 WARN_FUNC("return with DF set",
2374 insn
->sec
, insn
->offset
);
2378 if (func
&& has_modified_stack_frame(insn
, state
)) {
2379 WARN_FUNC("return with modified stack frame",
2380 insn
->sec
, insn
->offset
);
2384 if (state
->cfi
.bp_scratch
) {
2385 WARN_FUNC("BP used as a scratch register",
2386 insn
->sec
, insn
->offset
);
2394 * Alternatives should not contain any ORC entries, this in turn means they
2395 * should not contain any CFI ops, which implies all instructions should have
2396 * the same same CFI state.
2398 * It is possible to constuct alternatives that have unreachable holes that go
2399 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2400 * states which then results in ORC entries, which we just said we didn't want.
2402 * Avoid them by copying the CFI entry of the first instruction into the whole
2405 static void fill_alternative_cfi(struct objtool_file
*file
, struct instruction
*insn
)
2407 struct instruction
*first_insn
= insn
;
2408 int alt_group
= insn
->alt_group
;
2410 sec_for_each_insn_continue(file
, insn
) {
2411 if (insn
->alt_group
!= alt_group
)
2413 insn
->cfi
= first_insn
->cfi
;
2418 * Follow the branch starting at the given instruction, and recursively follow
2419 * any other branches (jumps). Meanwhile, track the frame pointer state at
2420 * each instruction and validate all the rules described in
2421 * tools/objtool/Documentation/stack-validation.txt.
2423 static int validate_branch(struct objtool_file
*file
, struct symbol
*func
,
2424 struct instruction
*insn
, struct insn_state state
)
2426 struct alternative
*alt
;
2427 struct instruction
*next_insn
;
2428 struct section
*sec
;
2435 next_insn
= next_insn_same_sec(file
, insn
);
2437 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
->pfunc
) {
2438 WARN("%s() falls through to next function %s()",
2439 func
->name
, insn
->func
->name
);
2443 if (func
&& insn
->ignore
) {
2444 WARN_FUNC("BUG: why am I validating an ignored function?",
2449 visited
= 1 << state
.uaccess
;
2450 if (insn
->visited
) {
2451 if (!insn
->hint
&& !insn_cfi_match(insn
, &state
.cfi
))
2454 if (insn
->visited
& visited
)
2459 state
.instr
+= insn
->instr
;
2462 state
.cfi
= insn
->cfi
;
2464 insn
->cfi
= state
.cfi
;
2466 insn
->visited
|= visited
;
2468 if (!insn
->ignore_alts
&& !list_empty(&insn
->alts
)) {
2469 bool skip_orig
= false;
2471 list_for_each_entry(alt
, &insn
->alts
, list
) {
2475 ret
= validate_branch(file
, func
, alt
->insn
, state
);
2478 BT_FUNC("(alt)", insn
);
2483 if (insn
->alt_group
)
2484 fill_alternative_cfi(file
, insn
);
2490 if (handle_insn_ops(insn
, &state
))
2493 switch (insn
->type
) {
2496 return validate_return(func
, insn
, &state
);
2499 case INSN_CALL_DYNAMIC
:
2500 ret
= validate_call(insn
, &state
);
2504 if (!no_fp
&& func
&& !is_fentry_call(insn
) &&
2505 !has_valid_stack_frame(&state
)) {
2506 WARN_FUNC("call without frame pointer save/setup",
2511 if (dead_end_function(file
, insn
->call_dest
))
2514 if (insn
->type
== INSN_CALL
&& insn
->call_dest
->static_call_tramp
) {
2515 list_add_tail(&insn
->static_call_node
,
2516 &file
->static_call_list
);
2521 case INSN_JUMP_CONDITIONAL
:
2522 case INSN_JUMP_UNCONDITIONAL
:
2523 if (func
&& is_sibling_call(insn
)) {
2524 ret
= validate_sibling_call(insn
, &state
);
2528 } else if (insn
->jump_dest
) {
2529 ret
= validate_branch(file
, func
,
2530 insn
->jump_dest
, state
);
2533 BT_FUNC("(branch)", insn
);
2538 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
2543 case INSN_JUMP_DYNAMIC
:
2544 case INSN_JUMP_DYNAMIC_CONDITIONAL
:
2545 if (func
&& is_sibling_call(insn
)) {
2546 ret
= validate_sibling_call(insn
, &state
);
2551 if (insn
->type
== INSN_JUMP_DYNAMIC
)
2556 case INSN_CONTEXT_SWITCH
:
2557 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
2558 WARN_FUNC("unsupported instruction in callable function",
2565 if (state
.uaccess
) {
2566 WARN_FUNC("recursive UACCESS enable", sec
, insn
->offset
);
2570 state
.uaccess
= true;
2574 if (!state
.uaccess
&& func
) {
2575 WARN_FUNC("redundant UACCESS disable", sec
, insn
->offset
);
2579 if (func_uaccess_safe(func
) && !state
.uaccess_stack
) {
2580 WARN_FUNC("UACCESS-safe disables UACCESS", sec
, insn
->offset
);
2584 state
.uaccess
= false;
2589 WARN_FUNC("recursive STD", sec
, insn
->offset
);
2595 if (!state
.df
&& func
)
2596 WARN_FUNC("redundant CLD", sec
, insn
->offset
);
2609 if (state
.cfi
.cfa
.base
== CFI_UNDEFINED
)
2611 WARN("%s: unexpected end of section", sec
->name
);
2621 static int validate_unwind_hints(struct objtool_file
*file
, struct section
*sec
)
2623 struct instruction
*insn
;
2624 struct insn_state state
;
2625 int ret
, warnings
= 0;
2630 init_insn_state(&state
, sec
);
2633 insn
= find_insn(file
, sec
, 0);
2637 insn
= list_first_entry(&file
->insn_list
, typeof(*insn
), list
);
2640 while (&insn
->list
!= &file
->insn_list
&& (!sec
|| insn
->sec
== sec
)) {
2641 if (insn
->hint
&& !insn
->visited
) {
2642 ret
= validate_branch(file
, insn
->func
, insn
, state
);
2643 if (ret
&& backtrace
)
2644 BT_FUNC("<=== (hint)", insn
);
2648 insn
= list_next_entry(insn
, list
);
2654 static int validate_retpoline(struct objtool_file
*file
)
2656 struct instruction
*insn
;
2659 for_each_insn(file
, insn
) {
2660 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
2661 insn
->type
!= INSN_CALL_DYNAMIC
)
2664 if (insn
->retpoline_safe
)
2668 * .init.text code is ran before userspace and thus doesn't
2669 * strictly need retpolines, except for modules which are
2670 * loaded late, they very much do need retpoline in their
2673 if (!strcmp(insn
->sec
->name
, ".init.text") && !module
)
2676 WARN_FUNC("indirect %s found in RETPOLINE build",
2677 insn
->sec
, insn
->offset
,
2678 insn
->type
== INSN_JUMP_DYNAMIC
? "jump" : "call");
2686 static bool is_kasan_insn(struct instruction
*insn
)
2688 return (insn
->type
== INSN_CALL
&&
2689 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
2692 static bool is_ubsan_insn(struct instruction
*insn
)
2694 return (insn
->type
== INSN_CALL
&&
2695 !strcmp(insn
->call_dest
->name
,
2696 "__ubsan_handle_builtin_unreachable"));
2699 static bool ignore_unreachable_insn(struct objtool_file
*file
, struct instruction
*insn
)
2702 struct instruction
*prev_insn
;
2704 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
2708 * Ignore any unused exceptions. This can happen when a whitelisted
2709 * function has an exception table entry.
2711 * Also ignore alternative replacement instructions. This can happen
2712 * when a whitelisted function uses one of the ALTERNATIVE macros.
2714 if (!strcmp(insn
->sec
->name
, ".fixup") ||
2715 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
2716 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
2719 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->offset
== FAKE_JUMP_OFFSET
)
2726 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2727 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2728 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2729 * (or occasionally a JMP to UD2).
2731 * It may also insert a UD2 after calling a __noreturn function.
2733 prev_insn
= list_prev_entry(insn
, list
);
2734 if ((prev_insn
->dead_end
|| dead_end_function(file
, prev_insn
->call_dest
)) &&
2735 (insn
->type
== INSN_BUG
||
2736 (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
2737 insn
->jump_dest
&& insn
->jump_dest
->type
== INSN_BUG
)))
2741 * Check if this (or a subsequent) instruction is related to
2742 * CONFIG_UBSAN or CONFIG_KASAN.
2744 * End the search at 5 instructions to avoid going into the weeds.
2746 for (i
= 0; i
< 5; i
++) {
2748 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
2751 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
2752 if (insn
->jump_dest
&&
2753 insn
->jump_dest
->func
== insn
->func
) {
2754 insn
= insn
->jump_dest
;
2761 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
2764 insn
= list_next_entry(insn
, list
);
2770 static int validate_symbol(struct objtool_file
*file
, struct section
*sec
,
2771 struct symbol
*sym
, struct insn_state
*state
)
2773 struct instruction
*insn
;
2777 WARN("%s() is missing an ELF size annotation", sym
->name
);
2781 if (sym
->pfunc
!= sym
|| sym
->alias
!= sym
)
2784 insn
= find_insn(file
, sec
, sym
->offset
);
2785 if (!insn
|| insn
->ignore
|| insn
->visited
)
2788 state
->uaccess
= sym
->uaccess_safe
;
2790 ret
= validate_branch(file
, insn
->func
, insn
, *state
);
2791 if (ret
&& backtrace
)
2792 BT_FUNC("<=== (sym)", insn
);
2796 static int validate_section(struct objtool_file
*file
, struct section
*sec
)
2798 struct insn_state state
;
2799 struct symbol
*func
;
2802 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
2803 if (func
->type
!= STT_FUNC
)
2806 init_insn_state(&state
, sec
);
2807 state
.cfi
.cfa
= initial_func_cfi
.cfa
;
2808 memcpy(&state
.cfi
.regs
, &initial_func_cfi
.regs
,
2809 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
2810 state
.cfi
.stack_size
= initial_func_cfi
.cfa
.offset
;
2812 warnings
+= validate_symbol(file
, sec
, func
, &state
);
2818 static int validate_vmlinux_functions(struct objtool_file
*file
)
2820 struct section
*sec
;
2823 sec
= find_section_by_name(file
->elf
, ".noinstr.text");
2825 warnings
+= validate_section(file
, sec
);
2826 warnings
+= validate_unwind_hints(file
, sec
);
2829 sec
= find_section_by_name(file
->elf
, ".entry.text");
2831 warnings
+= validate_section(file
, sec
);
2832 warnings
+= validate_unwind_hints(file
, sec
);
2838 static int validate_functions(struct objtool_file
*file
)
2840 struct section
*sec
;
2843 for_each_sec(file
, sec
) {
2844 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
2847 warnings
+= validate_section(file
, sec
);
2853 static int validate_reachable_instructions(struct objtool_file
*file
)
2855 struct instruction
*insn
;
2857 if (file
->ignore_unreachables
)
2860 for_each_insn(file
, insn
) {
2861 if (insn
->visited
|| ignore_unreachable_insn(file
, insn
))
2864 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
2871 int check(struct objtool_file
*file
)
2873 int ret
, warnings
= 0;
2875 arch_initial_func_cfi_state(&initial_func_cfi
);
2877 ret
= decode_sections(file
);
2882 if (list_empty(&file
->insn_list
))
2885 if (vmlinux
&& !validate_dup
) {
2886 ret
= validate_vmlinux_functions(file
);
2895 ret
= validate_retpoline(file
);
2901 ret
= validate_functions(file
);
2906 ret
= validate_unwind_hints(file
, NULL
);
2912 ret
= validate_reachable_instructions(file
);
2918 ret
= create_static_call_sections(file
);
2926 * Fatal error. The binary is corrupt or otherwise broken in
2927 * some way, or objtool itself is broken. Fail the kernel