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",
181 if (func->bind == STB_WEAK)
184 if (func->bind == STB_GLOBAL)
185 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
186 if (!strcmp(func->name, global_noreturns[i]))
192 insn = find_insn(file, func->sec, func->offset);
196 func_for_each_insn(file, func, insn) {
199 if (insn->type == INSN_RETURN)
207 * A function can have a sibling call instead of a return. In that
208 * case, the function's dead-end status depends on whether the target
209 * of the sibling call returns.
211 func_for_each_insn(file, func, insn) {
212 if (is_sibling_call(insn)) {
213 struct instruction *dest = insn->jump_dest;
216 /* sibling call to another file */
219 /* local sibling call */
220 if (recursion == 5) {
222 * Infinite recursion: two functions have
223 * sibling calls to each other. This is a very
224 * rare case. It means they aren't dead ends.
229 return __dead_end_function(file, dest->func, recursion+1);
236 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
238 return __dead_end_function(file, func, 0);
241 static void init_cfi_state(struct cfi_state *cfi)
245 for (i = 0; i < CFI_NUM_REGS; i++) {
246 cfi->regs[i].base = CFI_UNDEFINED;
247 cfi->vals[i].base = CFI_UNDEFINED;
249 cfi->cfa.base = CFI_UNDEFINED;
250 cfi->drap_reg = CFI_UNDEFINED;
251 cfi->drap_offset = -1;
254 static void init_insn_state(struct insn_state *state, struct section *sec)
256 memset(state, 0, sizeof(*state));
257 init_cfi_state(&state->cfi);
260 * We need the full vmlinux for noinstr validation, otherwise we can
261 * not correctly determine insn->call_dest->sec (external symbols do
262 * not have a section).
264 if (vmlinux && noinstr && sec)
265 state->noinstr = sec->noinstr;
269 * Call the arch-specific instruction decoder for all the instructions and add
270 * them to the global instruction list.
272 static int decode_instructions(struct objtool_file *file)
276 unsigned long offset;
277 struct instruction *insn;
278 unsigned long nr_insns = 0;
281 for_each_sec(file, sec) {
283 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
286 if (strcmp(sec->name, ".altinstr_replacement") &&
287 strcmp(sec->name, ".altinstr_aux") &&
288 strncmp(sec->name, ".discard.", 9))
291 if (!strcmp(sec->name, ".noinstr.text") ||
292 !strcmp(sec->name, ".entry.text"))
295 for (offset = 0; offset < sec->len; offset += insn->len) {
296 insn = malloc(sizeof(*insn));
298 WARN("malloc failed");
301 memset(insn, 0, sizeof(*insn));
302 INIT_LIST_HEAD(&insn->alts);
303 INIT_LIST_HEAD(&insn->stack_ops);
304 init_cfi_state(&insn->cfi);
307 insn->offset = offset;
309 ret = arch_decode_instruction(file->elf, sec, offset,
311 &insn->len, &insn->type,
317 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
318 list_add_tail(&insn->list, &file->insn_list);
322 list_for_each_entry(func, &sec->symbol_list, list) {
323 if (func->type != STT_FUNC || func->alias != func)
326 if (!find_insn(file, sec, func->offset)) {
327 WARN("%s(): can't find starting instruction",
332 sym_for_each_insn(file, func, insn)
338 printf("nr_insns: %lu\n", nr_insns);
347 static struct instruction *find_last_insn(struct objtool_file *file,
350 struct instruction *insn = NULL;
352 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
354 for (offset = sec->len - 1; offset >= end && !insn; offset--)
355 insn = find_insn(file, sec, offset);
361 * Mark "ud2" instructions and manually annotated dead ends.
363 static int add_dead_ends(struct objtool_file *file)
367 struct instruction *insn;
370 * By default, "ud2" is a dead end unless otherwise annotated, because
371 * GCC 7 inserts it for certain divide-by-zero cases.
373 for_each_insn(file, insn)
374 if (insn->type == INSN_BUG)
375 insn->dead_end = true;
378 * Check for manually annotated dead ends.
380 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
384 list_for_each_entry(reloc, &sec->reloc_list, list) {
385 if (reloc->sym->type != STT_SECTION) {
386 WARN("unexpected relocation symbol type in %s", sec->name);
389 insn = find_insn(file, reloc->sym->sec, reloc->addend);
391 insn = list_prev_entry(insn, list);
392 else if (reloc->addend == reloc->sym->sec->len) {
393 insn = find_last_insn(file, reloc->sym->sec);
395 WARN("can't find unreachable insn at %s+0x%x",
396 reloc->sym->sec->name, reloc->addend);
400 WARN("can't find unreachable insn at %s+0x%x",
401 reloc->sym->sec->name, reloc->addend);
405 insn->dead_end = true;
410 * These manually annotated reachable checks are needed for GCC 4.4,
411 * where the Linux unreachable() macro isn't supported. In that case
412 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
415 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
419 list_for_each_entry(reloc, &sec->reloc_list, list) {
420 if (reloc->sym->type != STT_SECTION) {
421 WARN("unexpected relocation symbol type in %s", sec->name);
424 insn = find_insn(file, reloc->sym->sec, reloc->addend);
426 insn = list_prev_entry(insn, list);
427 else if (reloc->addend == reloc->sym->sec->len) {
428 insn = find_last_insn(file, reloc->sym->sec);
430 WARN("can't find reachable insn at %s+0x%x",
431 reloc->sym->sec->name, reloc->addend);
435 WARN("can't find reachable insn at %s+0x%x",
436 reloc->sym->sec->name, reloc->addend);
440 insn->dead_end = false;
446 static int create_static_call_sections(struct objtool_file *file)
449 struct static_call_site *site;
450 struct instruction *insn;
451 struct symbol *key_sym;
452 char *key_name, *tmp;
455 sec = find_section_by_name(file->elf, ".static_call_sites");
457 INIT_LIST_HEAD(&file->static_call_list);
458 WARN("file already has .static_call_sites section, skipping");
462 if (list_empty(&file->static_call_list))
466 list_for_each_entry(insn, &file->static_call_list, call_node)
469 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
470 sizeof(struct static_call_site), idx);
475 list_for_each_entry(insn, &file->static_call_list, call_node) {
477 site = (struct static_call_site *)sec->data->d_buf + idx;
478 memset(site, 0, sizeof(struct static_call_site));
480 /* populate reloc for 'addr' */
481 if (elf_add_reloc_to_insn(file->elf, sec,
482 idx * sizeof(struct static_call_site),
484 insn->sec, insn->offset))
487 /* find key symbol */
488 key_name = strdup(insn->call_dest->name);
493 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
494 STATIC_CALL_TRAMP_PREFIX_LEN)) {
495 WARN("static_call: trampoline name malformed: %s", key_name);
498 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
499 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
501 key_sym = find_symbol_by_name(file->elf, tmp);
504 WARN("static_call: can't find static_call_key symbol: %s", tmp);
509 * For modules(), the key might not be exported, which
510 * means the module can make static calls but isn't
511 * allowed to change them.
513 * In that case we temporarily set the key to be the
514 * trampoline address. This is fixed up in
515 * static_call_add_module().
517 key_sym = insn->call_dest;
521 /* populate reloc for 'key' */
522 if (elf_add_reloc(file->elf, sec,
523 idx * sizeof(struct static_call_site) + 4,
524 R_X86_64_PC32, key_sym,
525 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
534 static int create_mcount_loc_sections(struct objtool_file *file)
538 struct instruction *insn;
541 sec = find_section_by_name(file->elf, "__mcount_loc");
543 INIT_LIST_HEAD(&file->mcount_loc_list);
544 WARN("file already has __mcount_loc section, skipping");
548 if (list_empty(&file->mcount_loc_list))
552 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
555 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
560 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
562 loc = (unsigned long *)sec->data->d_buf + idx;
563 memset(loc, 0, sizeof(unsigned long));
565 if (elf_add_reloc_to_insn(file->elf, sec,
566 idx * sizeof(unsigned long),
568 insn->sec, insn->offset))
578 * Warnings shouldn't be reported for ignored functions.
580 static void add_ignores(struct objtool_file *file)
582 struct instruction *insn;
587 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
591 list_for_each_entry(reloc, &sec->reloc_list, list) {
592 switch (reloc->sym->type) {
598 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
604 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
608 func_for_each_insn(file, func, insn)
614 * This is a whitelist of functions that is allowed to be called with AC set.
615 * The list is meant to be minimal and only contains compiler instrumentation
616 * ABI and a few functions used to implement *_{to,from}_user() functions.
618 * These functions must not directly change AC, but may PUSHF/POPF.
620 static const char *uaccess_safe_builtin[] = {
624 /* KASAN out-of-line */
625 "__asan_loadN_noabort",
626 "__asan_load1_noabort",
627 "__asan_load2_noabort",
628 "__asan_load4_noabort",
629 "__asan_load8_noabort",
630 "__asan_load16_noabort",
631 "__asan_storeN_noabort",
632 "__asan_store1_noabort",
633 "__asan_store2_noabort",
634 "__asan_store4_noabort",
635 "__asan_store8_noabort",
636 "__asan_store16_noabort",
637 "__kasan_check_read",
638 "__kasan_check_write",
640 "__asan_report_load_n_noabort",
641 "__asan_report_load1_noabort",
642 "__asan_report_load2_noabort",
643 "__asan_report_load4_noabort",
644 "__asan_report_load8_noabort",
645 "__asan_report_load16_noabort",
646 "__asan_report_store_n_noabort",
647 "__asan_report_store1_noabort",
648 "__asan_report_store2_noabort",
649 "__asan_report_store4_noabort",
650 "__asan_report_store8_noabort",
651 "__asan_report_store16_noabort",
653 "__kcsan_check_access",
654 "kcsan_found_watchpoint",
655 "kcsan_setup_watchpoint",
656 "kcsan_check_scoped_accesses",
657 "kcsan_disable_current",
658 "kcsan_enable_current_nowarn",
663 "__tsan_write_range",
674 "__tsan_read_write1",
675 "__tsan_read_write2",
676 "__tsan_read_write4",
677 "__tsan_read_write8",
678 "__tsan_read_write16",
679 "__tsan_atomic8_load",
680 "__tsan_atomic16_load",
681 "__tsan_atomic32_load",
682 "__tsan_atomic64_load",
683 "__tsan_atomic8_store",
684 "__tsan_atomic16_store",
685 "__tsan_atomic32_store",
686 "__tsan_atomic64_store",
687 "__tsan_atomic8_exchange",
688 "__tsan_atomic16_exchange",
689 "__tsan_atomic32_exchange",
690 "__tsan_atomic64_exchange",
691 "__tsan_atomic8_fetch_add",
692 "__tsan_atomic16_fetch_add",
693 "__tsan_atomic32_fetch_add",
694 "__tsan_atomic64_fetch_add",
695 "__tsan_atomic8_fetch_sub",
696 "__tsan_atomic16_fetch_sub",
697 "__tsan_atomic32_fetch_sub",
698 "__tsan_atomic64_fetch_sub",
699 "__tsan_atomic8_fetch_and",
700 "__tsan_atomic16_fetch_and",
701 "__tsan_atomic32_fetch_and",
702 "__tsan_atomic64_fetch_and",
703 "__tsan_atomic8_fetch_or",
704 "__tsan_atomic16_fetch_or",
705 "__tsan_atomic32_fetch_or",
706 "__tsan_atomic64_fetch_or",
707 "__tsan_atomic8_fetch_xor",
708 "__tsan_atomic16_fetch_xor",
709 "__tsan_atomic32_fetch_xor",
710 "__tsan_atomic64_fetch_xor",
711 "__tsan_atomic8_fetch_nand",
712 "__tsan_atomic16_fetch_nand",
713 "__tsan_atomic32_fetch_nand",
714 "__tsan_atomic64_fetch_nand",
715 "__tsan_atomic8_compare_exchange_strong",
716 "__tsan_atomic16_compare_exchange_strong",
717 "__tsan_atomic32_compare_exchange_strong",
718 "__tsan_atomic64_compare_exchange_strong",
719 "__tsan_atomic8_compare_exchange_weak",
720 "__tsan_atomic16_compare_exchange_weak",
721 "__tsan_atomic32_compare_exchange_weak",
722 "__tsan_atomic64_compare_exchange_weak",
723 "__tsan_atomic8_compare_exchange_val",
724 "__tsan_atomic16_compare_exchange_val",
725 "__tsan_atomic32_compare_exchange_val",
726 "__tsan_atomic64_compare_exchange_val",
727 "__tsan_atomic_thread_fence",
728 "__tsan_atomic_signal_fence",
732 "__sanitizer_cov_trace_pc",
733 "__sanitizer_cov_trace_const_cmp1",
734 "__sanitizer_cov_trace_const_cmp2",
735 "__sanitizer_cov_trace_const_cmp4",
736 "__sanitizer_cov_trace_const_cmp8",
737 "__sanitizer_cov_trace_cmp1",
738 "__sanitizer_cov_trace_cmp2",
739 "__sanitizer_cov_trace_cmp4",
740 "__sanitizer_cov_trace_cmp8",
741 "__sanitizer_cov_trace_switch",
743 "ubsan_type_mismatch_common",
744 "__ubsan_handle_type_mismatch",
745 "__ubsan_handle_type_mismatch_v1",
746 "__ubsan_handle_shift_out_of_bounds",
748 "csum_partial_copy_generic",
750 "copy_mc_fragile_handle_tail",
751 "copy_mc_enhanced_fast_string",
752 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
756 static void add_uaccess_safe(struct objtool_file *file)
764 for (name = uaccess_safe_builtin; *name; name++) {
765 func = find_symbol_by_name(file->elf, *name);
769 func->uaccess_safe = true;
774 * FIXME: For now, just ignore any alternatives which add retpolines. This is
775 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
776 * But it at least allows objtool to understand the control flow *around* the
779 static int add_ignore_alternatives(struct objtool_file *file)
783 struct instruction *insn;
785 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
789 list_for_each_entry(reloc, &sec->reloc_list, list) {
790 if (reloc->sym->type != STT_SECTION) {
791 WARN("unexpected relocation symbol type in %s", sec->name);
795 insn = find_insn(file, reloc->sym->sec, reloc->addend);
797 WARN("bad .discard.ignore_alts entry");
801 insn->ignore_alts = true;
807 __weak bool arch_is_retpoline(struct symbol *sym)
812 #define NEGATIVE_RELOC ((void *)-1L)
814 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
816 if (insn->reloc == NEGATIVE_RELOC)
820 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
821 insn->offset, insn->len);
823 insn->reloc = NEGATIVE_RELOC;
832 * Find the destination instructions for all jumps.
834 static int add_jump_destinations(struct objtool_file *file)
836 struct instruction *insn;
838 struct section *dest_sec;
839 unsigned long dest_off;
841 for_each_insn(file, insn) {
842 if (!is_static_jump(insn))
845 reloc = insn_reloc(file, insn);
847 dest_sec = insn->sec;
848 dest_off = arch_jump_destination(insn);
849 } else if (reloc->sym->type == STT_SECTION) {
850 dest_sec = reloc->sym->sec;
851 dest_off = arch_dest_reloc_offset(reloc->addend);
852 } else if (arch_is_retpoline(reloc->sym)) {
854 * Retpoline jumps are really dynamic jumps in
855 * disguise, so convert them accordingly.
857 if (insn->type == INSN_JUMP_UNCONDITIONAL)
858 insn->type = INSN_JUMP_DYNAMIC;
860 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
862 list_add_tail(&insn->call_node,
863 &file->retpoline_call_list);
865 insn->retpoline_safe = true;
867 } else if (insn->func) {
868 /* internal or external sibling call (with reloc) */
869 insn->call_dest = reloc->sym;
870 if (insn->call_dest->static_call_tramp) {
871 list_add_tail(&insn->call_node,
872 &file->static_call_list);
875 } else if (reloc->sym->sec->idx) {
876 dest_sec = reloc->sym->sec;
877 dest_off = reloc->sym->sym.st_value +
878 arch_dest_reloc_offset(reloc->addend);
880 /* non-func asm code jumping to another file */
884 insn->jump_dest = find_insn(file, dest_sec, dest_off);
885 if (!insn->jump_dest) {
888 * This is a special case where an alt instruction
889 * jumps past the end of the section. These are
890 * handled later in handle_group_alt().
892 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
895 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
896 insn->sec, insn->offset, dest_sec->name,
902 * Cross-function jump.
904 if (insn->func && insn->jump_dest->func &&
905 insn->func != insn->jump_dest->func) {
908 * For GCC 8+, create parent/child links for any cold
909 * subfunctions. This is _mostly_ redundant with a
910 * similar initialization in read_symbols().
912 * If a function has aliases, we want the *first* such
913 * function in the symbol table to be the subfunction's
914 * parent. In that case we overwrite the
915 * initialization done in read_symbols().
917 * However this code can't completely replace the
918 * read_symbols() code because this doesn't detect the
919 * case where the parent function's only reference to a
920 * subfunction is through a jump table.
922 if (!strstr(insn->func->name, ".cold") &&
923 strstr(insn->jump_dest->func->name, ".cold")) {
924 insn->func->cfunc = insn->jump_dest->func;
925 insn->jump_dest->func->pfunc = insn->func;
927 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
928 insn->jump_dest->offset == insn->jump_dest->func->offset) {
930 /* internal sibling call (without reloc) */
931 insn->call_dest = insn->jump_dest->func;
932 if (insn->call_dest->static_call_tramp) {
933 list_add_tail(&insn->call_node,
934 &file->static_call_list);
943 static void remove_insn_ops(struct instruction *insn)
945 struct stack_op *op, *tmp;
947 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
953 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
955 struct symbol *call_dest;
957 call_dest = find_func_by_offset(sec, offset);
959 call_dest = find_symbol_by_offset(sec, offset);
965 * Find the destination instructions for all calls.
967 static int add_call_destinations(struct objtool_file *file)
969 struct instruction *insn;
970 unsigned long dest_off;
973 for_each_insn(file, insn) {
974 if (insn->type != INSN_CALL)
977 reloc = insn_reloc(file, insn);
979 dest_off = arch_jump_destination(insn);
980 insn->call_dest = find_call_destination(insn->sec, dest_off);
985 if (!insn->call_dest) {
986 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
990 if (insn->func && insn->call_dest->type != STT_FUNC) {
991 WARN_FUNC("unsupported call to non-function",
992 insn->sec, insn->offset);
996 } else if (reloc->sym->type == STT_SECTION) {
997 dest_off = arch_dest_reloc_offset(reloc->addend);
998 insn->call_dest = find_call_destination(reloc->sym->sec,
1000 if (!insn->call_dest) {
1001 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1002 insn->sec, insn->offset,
1003 reloc->sym->sec->name,
1008 } else if (arch_is_retpoline(reloc->sym)) {
1010 * Retpoline calls are really dynamic calls in
1011 * disguise, so convert them accordingly.
1013 insn->type = INSN_CALL_DYNAMIC;
1014 insn->retpoline_safe = true;
1016 list_add_tail(&insn->call_node,
1017 &file->retpoline_call_list);
1019 remove_insn_ops(insn);
1023 insn->call_dest = reloc->sym;
1025 if (insn->call_dest && insn->call_dest->static_call_tramp) {
1026 list_add_tail(&insn->call_node,
1027 &file->static_call_list);
1031 * Many compilers cannot disable KCOV with a function attribute
1032 * so they need a little help, NOP out any KCOV calls from noinstr
1035 if (insn->sec->noinstr &&
1036 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
1038 reloc->type = R_NONE;
1039 elf_write_reloc(file->elf, reloc);
1042 elf_write_insn(file->elf, insn->sec,
1043 insn->offset, insn->len,
1044 arch_nop_insn(insn->len));
1045 insn->type = INSN_NOP;
1048 if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1050 reloc->type = R_NONE;
1051 elf_write_reloc(file->elf, reloc);
1054 elf_write_insn(file->elf, insn->sec,
1055 insn->offset, insn->len,
1056 arch_nop_insn(insn->len));
1058 insn->type = INSN_NOP;
1060 list_add_tail(&insn->mcount_loc_node,
1061 &file->mcount_loc_list);
1065 * Whatever stack impact regular CALLs have, should be undone
1066 * by the RETURN of the called function.
1068 * Annotated intra-function calls retain the stack_ops but
1069 * are converted to JUMP, see read_intra_function_calls().
1071 remove_insn_ops(insn);
1078 * The .alternatives section requires some extra special care over and above
1079 * other special sections because alternatives are patched in place.
1081 static int handle_group_alt(struct objtool_file *file,
1082 struct special_alt *special_alt,
1083 struct instruction *orig_insn,
1084 struct instruction **new_insn)
1086 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1087 struct alt_group *orig_alt_group, *new_alt_group;
1088 unsigned long dest_off;
1091 orig_alt_group = malloc(sizeof(*orig_alt_group));
1092 if (!orig_alt_group) {
1093 WARN("malloc failed");
1096 orig_alt_group->cfi = calloc(special_alt->orig_len,
1097 sizeof(struct cfi_state *));
1098 if (!orig_alt_group->cfi) {
1099 WARN("calloc failed");
1103 last_orig_insn = NULL;
1105 sec_for_each_insn_from(file, insn) {
1106 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1109 insn->alt_group = orig_alt_group;
1110 last_orig_insn = insn;
1112 orig_alt_group->orig_group = NULL;
1113 orig_alt_group->first_insn = orig_insn;
1114 orig_alt_group->last_insn = last_orig_insn;
1117 new_alt_group = malloc(sizeof(*new_alt_group));
1118 if (!new_alt_group) {
1119 WARN("malloc failed");
1123 if (special_alt->new_len < special_alt->orig_len) {
1125 * Insert a fake nop at the end to make the replacement
1126 * alt_group the same size as the original. This is needed to
1127 * allow propagate_alt_cfi() to do its magic. When the last
1128 * instruction affects the stack, the instruction after it (the
1129 * nop) will propagate the new state to the shared CFI array.
1131 nop = malloc(sizeof(*nop));
1133 WARN("malloc failed");
1136 memset(nop, 0, sizeof(*nop));
1137 INIT_LIST_HEAD(&nop->alts);
1138 INIT_LIST_HEAD(&nop->stack_ops);
1139 init_cfi_state(&nop->cfi);
1141 nop->sec = special_alt->new_sec;
1142 nop->offset = special_alt->new_off + special_alt->new_len;
1143 nop->len = special_alt->orig_len - special_alt->new_len;
1144 nop->type = INSN_NOP;
1145 nop->func = orig_insn->func;
1146 nop->alt_group = new_alt_group;
1147 nop->ignore = orig_insn->ignore_alts;
1150 if (!special_alt->new_len) {
1156 sec_for_each_insn_from(file, insn) {
1157 struct reloc *alt_reloc;
1159 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1162 last_new_insn = insn;
1164 insn->ignore = orig_insn->ignore_alts;
1165 insn->func = orig_insn->func;
1166 insn->alt_group = new_alt_group;
1169 * Since alternative replacement code is copy/pasted by the
1170 * kernel after applying relocations, generally such code can't
1171 * have relative-address relocation references to outside the
1172 * .altinstr_replacement section, unless the arch's
1173 * alternatives code can adjust the relative offsets
1176 alt_reloc = insn_reloc(file, insn);
1178 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1180 WARN_FUNC("unsupported relocation in alternatives section",
1181 insn->sec, insn->offset);
1185 if (!is_static_jump(insn))
1188 if (!insn->immediate)
1191 dest_off = arch_jump_destination(insn);
1192 if (dest_off == special_alt->new_off + special_alt->new_len)
1193 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1195 if (!insn->jump_dest) {
1196 WARN_FUNC("can't find alternative jump destination",
1197 insn->sec, insn->offset);
1202 if (!last_new_insn) {
1203 WARN_FUNC("can't find last new alternative instruction",
1204 special_alt->new_sec, special_alt->new_off);
1209 list_add(&nop->list, &last_new_insn->list);
1211 new_alt_group->orig_group = orig_alt_group;
1212 new_alt_group->first_insn = *new_insn;
1213 new_alt_group->last_insn = nop ? : last_new_insn;
1214 new_alt_group->cfi = orig_alt_group->cfi;
1219 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1220 * If the original instruction is a jump, make the alt entry an effective nop
1221 * by just skipping the original instruction.
1223 static int handle_jump_alt(struct objtool_file *file,
1224 struct special_alt *special_alt,
1225 struct instruction *orig_insn,
1226 struct instruction **new_insn)
1228 if (orig_insn->type == INSN_NOP)
1231 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1232 WARN_FUNC("unsupported instruction at jump label",
1233 orig_insn->sec, orig_insn->offset);
1237 *new_insn = list_next_entry(orig_insn, list);
1242 * Read all the special sections which have alternate instructions which can be
1243 * patched in or redirected to at runtime. Each instruction having alternate
1244 * instruction(s) has them added to its insn->alts list, which will be
1245 * traversed in validate_branch().
1247 static int add_special_section_alts(struct objtool_file *file)
1249 struct list_head special_alts;
1250 struct instruction *orig_insn, *new_insn;
1251 struct special_alt *special_alt, *tmp;
1252 struct alternative *alt;
1255 ret = special_get_alts(file->elf, &special_alts);
1259 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1261 orig_insn = find_insn(file, special_alt->orig_sec,
1262 special_alt->orig_off);
1264 WARN_FUNC("special: can't find orig instruction",
1265 special_alt->orig_sec, special_alt->orig_off);
1271 if (!special_alt->group || special_alt->new_len) {
1272 new_insn = find_insn(file, special_alt->new_sec,
1273 special_alt->new_off);
1275 WARN_FUNC("special: can't find new instruction",
1276 special_alt->new_sec,
1277 special_alt->new_off);
1283 if (special_alt->group) {
1284 if (!special_alt->orig_len) {
1285 WARN_FUNC("empty alternative entry",
1286 orig_insn->sec, orig_insn->offset);
1290 ret = handle_group_alt(file, special_alt, orig_insn,
1294 } else if (special_alt->jump_or_nop) {
1295 ret = handle_jump_alt(file, special_alt, orig_insn,
1301 alt = malloc(sizeof(*alt));
1303 WARN("malloc failed");
1308 alt->insn = new_insn;
1309 alt->skip_orig = special_alt->skip_orig;
1310 orig_insn->ignore_alts |= special_alt->skip_alt;
1311 list_add_tail(&alt->list, &orig_insn->alts);
1313 list_del(&special_alt->list);
1321 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1322 struct reloc *table)
1324 struct reloc *reloc = table;
1325 struct instruction *dest_insn;
1326 struct alternative *alt;
1327 struct symbol *pfunc = insn->func->pfunc;
1328 unsigned int prev_offset = 0;
1331 * Each @reloc is a switch table relocation which points to the target
1334 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1336 /* Check for the end of the table: */
1337 if (reloc != table && reloc->jump_table_start)
1340 /* Make sure the table entries are consecutive: */
1341 if (prev_offset && reloc->offset != prev_offset + 8)
1344 /* Detect function pointers from contiguous objects: */
1345 if (reloc->sym->sec == pfunc->sec &&
1346 reloc->addend == pfunc->offset)
1349 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1353 /* Make sure the destination is in the same function: */
1354 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1357 alt = malloc(sizeof(*alt));
1359 WARN("malloc failed");
1363 alt->insn = dest_insn;
1364 list_add_tail(&alt->list, &insn->alts);
1365 prev_offset = reloc->offset;
1369 WARN_FUNC("can't find switch jump table",
1370 insn->sec, insn->offset);
1378 * find_jump_table() - Given a dynamic jump, find the switch jump table
1379 * associated with it.
1381 static struct reloc *find_jump_table(struct objtool_file *file,
1382 struct symbol *func,
1383 struct instruction *insn)
1385 struct reloc *table_reloc;
1386 struct instruction *dest_insn, *orig_insn = insn;
1389 * Backward search using the @first_jump_src links, these help avoid
1390 * much of the 'in between' code. Which avoids us getting confused by
1394 insn && insn->func && insn->func->pfunc == func;
1395 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1397 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1400 /* allow small jumps within the range */
1401 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1403 (insn->jump_dest->offset <= insn->offset ||
1404 insn->jump_dest->offset > orig_insn->offset))
1407 table_reloc = arch_find_switch_table(file, insn);
1410 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1411 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1421 * First pass: Mark the head of each jump table so that in the next pass,
1422 * we know when a given jump table ends and the next one starts.
1424 static void mark_func_jump_tables(struct objtool_file *file,
1425 struct symbol *func)
1427 struct instruction *insn, *last = NULL;
1428 struct reloc *reloc;
1430 func_for_each_insn(file, func, insn) {
1435 * Store back-pointers for unconditional forward jumps such
1436 * that find_jump_table() can back-track using those and
1437 * avoid some potentially confusing code.
1439 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1440 insn->offset > last->offset &&
1441 insn->jump_dest->offset > insn->offset &&
1442 !insn->jump_dest->first_jump_src) {
1444 insn->jump_dest->first_jump_src = insn;
1445 last = insn->jump_dest;
1448 if (insn->type != INSN_JUMP_DYNAMIC)
1451 reloc = find_jump_table(file, func, insn);
1453 reloc->jump_table_start = true;
1454 insn->jump_table = reloc;
1459 static int add_func_jump_tables(struct objtool_file *file,
1460 struct symbol *func)
1462 struct instruction *insn;
1465 func_for_each_insn(file, func, insn) {
1466 if (!insn->jump_table)
1469 ret = add_jump_table(file, insn, insn->jump_table);
1478 * For some switch statements, gcc generates a jump table in the .rodata
1479 * section which contains a list of addresses within the function to jump to.
1480 * This finds these jump tables and adds them to the insn->alts lists.
1482 static int add_jump_table_alts(struct objtool_file *file)
1484 struct section *sec;
1485 struct symbol *func;
1491 for_each_sec(file, sec) {
1492 list_for_each_entry(func, &sec->symbol_list, list) {
1493 if (func->type != STT_FUNC)
1496 mark_func_jump_tables(file, func);
1497 ret = add_func_jump_tables(file, func);
1506 static void set_func_state(struct cfi_state *state)
1508 state->cfa = initial_func_cfi.cfa;
1509 memcpy(&state->regs, &initial_func_cfi.regs,
1510 CFI_NUM_REGS * sizeof(struct cfi_reg));
1511 state->stack_size = initial_func_cfi.cfa.offset;
1514 static int read_unwind_hints(struct objtool_file *file)
1516 struct section *sec, *relocsec;
1517 struct reloc *reloc;
1518 struct unwind_hint *hint;
1519 struct instruction *insn;
1522 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1526 relocsec = sec->reloc;
1528 WARN("missing .rela.discard.unwind_hints section");
1532 if (sec->len % sizeof(struct unwind_hint)) {
1533 WARN("struct unwind_hint size mismatch");
1539 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1540 hint = (struct unwind_hint *)sec->data->d_buf + i;
1542 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1544 WARN("can't find reloc for unwind_hints[%d]", i);
1548 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1550 WARN("can't find insn for unwind_hints[%d]", i);
1556 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1557 set_func_state(&insn->cfi);
1561 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1562 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1563 insn->sec, insn->offset, hint->sp_reg);
1567 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1568 insn->cfi.type = hint->type;
1569 insn->cfi.end = hint->end;
1575 static int read_retpoline_hints(struct objtool_file *file)
1577 struct section *sec;
1578 struct instruction *insn;
1579 struct reloc *reloc;
1581 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1585 list_for_each_entry(reloc, &sec->reloc_list, list) {
1586 if (reloc->sym->type != STT_SECTION) {
1587 WARN("unexpected relocation symbol type in %s", sec->name);
1591 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1593 WARN("bad .discard.retpoline_safe entry");
1597 if (insn->type != INSN_JUMP_DYNAMIC &&
1598 insn->type != INSN_CALL_DYNAMIC) {
1599 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1600 insn->sec, insn->offset);
1604 insn->retpoline_safe = true;
1610 static int read_instr_hints(struct objtool_file *file)
1612 struct section *sec;
1613 struct instruction *insn;
1614 struct reloc *reloc;
1616 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1620 list_for_each_entry(reloc, &sec->reloc_list, list) {
1621 if (reloc->sym->type != STT_SECTION) {
1622 WARN("unexpected relocation symbol type in %s", sec->name);
1626 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1628 WARN("bad .discard.instr_end entry");
1635 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1639 list_for_each_entry(reloc, &sec->reloc_list, list) {
1640 if (reloc->sym->type != STT_SECTION) {
1641 WARN("unexpected relocation symbol type in %s", sec->name);
1645 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1647 WARN("bad .discard.instr_begin entry");
1657 static int read_intra_function_calls(struct objtool_file *file)
1659 struct instruction *insn;
1660 struct section *sec;
1661 struct reloc *reloc;
1663 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1667 list_for_each_entry(reloc, &sec->reloc_list, list) {
1668 unsigned long dest_off;
1670 if (reloc->sym->type != STT_SECTION) {
1671 WARN("unexpected relocation symbol type in %s",
1676 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1678 WARN("bad .discard.intra_function_call entry");
1682 if (insn->type != INSN_CALL) {
1683 WARN_FUNC("intra_function_call not a direct call",
1684 insn->sec, insn->offset);
1689 * Treat intra-function CALLs as JMPs, but with a stack_op.
1690 * See add_call_destinations(), which strips stack_ops from
1693 insn->type = INSN_JUMP_UNCONDITIONAL;
1695 dest_off = insn->offset + insn->len + insn->immediate;
1696 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1697 if (!insn->jump_dest) {
1698 WARN_FUNC("can't find call dest at %s+0x%lx",
1699 insn->sec, insn->offset,
1700 insn->sec->name, dest_off);
1708 static int read_static_call_tramps(struct objtool_file *file)
1710 struct section *sec;
1711 struct symbol *func;
1713 for_each_sec(file, sec) {
1714 list_for_each_entry(func, &sec->symbol_list, list) {
1715 if (func->bind == STB_GLOBAL &&
1716 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1717 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1718 func->static_call_tramp = true;
1725 static void mark_rodata(struct objtool_file *file)
1727 struct section *sec;
1731 * Search for the following rodata sections, each of which can
1732 * potentially contain jump tables:
1734 * - .rodata: can contain GCC switch tables
1735 * - .rodata.<func>: same, if -fdata-sections is being used
1736 * - .rodata..c_jump_table: contains C annotated jump tables
1738 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1740 for_each_sec(file, sec) {
1741 if (!strncmp(sec->name, ".rodata", 7) &&
1742 !strstr(sec->name, ".str1.")) {
1748 file->rodata = found;
1751 __weak int arch_rewrite_retpolines(struct objtool_file *file)
1756 static int decode_sections(struct objtool_file *file)
1762 ret = decode_instructions(file);
1766 ret = add_dead_ends(file);
1771 add_uaccess_safe(file);
1773 ret = add_ignore_alternatives(file);
1778 * Must be before add_{jump_call}_destination.
1780 ret = read_static_call_tramps(file);
1785 * Must be before add_special_section_alts() as that depends on
1786 * jump_dest being set.
1788 ret = add_jump_destinations(file);
1792 ret = add_special_section_alts(file);
1797 * Must be before add_call_destination(); it changes INSN_CALL to
1800 ret = read_intra_function_calls(file);
1804 ret = add_call_destinations(file);
1808 ret = add_jump_table_alts(file);
1812 ret = read_unwind_hints(file);
1816 ret = read_retpoline_hints(file);
1820 ret = read_instr_hints(file);
1825 * Must be after add_special_section_alts(), since this will emit
1826 * alternatives. Must be after add_{jump,call}_destination(), since
1827 * those create the call insn lists.
1829 ret = arch_rewrite_retpolines(file);
1836 static bool is_fentry_call(struct instruction *insn)
1838 if (insn->type == INSN_CALL && insn->call_dest &&
1839 insn->call_dest->type == STT_NOTYPE &&
1840 !strcmp(insn->call_dest->name, "__fentry__"))
1846 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1848 struct cfi_state *cfi = &state->cfi;
1851 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1854 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1857 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1860 for (i = 0; i < CFI_NUM_REGS; i++) {
1861 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1862 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1869 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1870 int expected_offset)
1872 return reg->base == CFI_CFA &&
1873 reg->offset == expected_offset;
1876 static bool has_valid_stack_frame(struct insn_state *state)
1878 struct cfi_state *cfi = &state->cfi;
1880 if (cfi->cfa.base == CFI_BP &&
1881 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1882 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1885 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1891 static int update_cfi_state_regs(struct instruction *insn,
1892 struct cfi_state *cfi,
1893 struct stack_op *op)
1895 struct cfi_reg *cfa = &cfi->cfa;
1897 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1901 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1905 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1908 /* add immediate to sp */
1909 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1910 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1911 cfa->offset -= op->src.offset;
1916 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1918 if (arch_callee_saved_reg(reg) &&
1919 cfi->regs[reg].base == CFI_UNDEFINED) {
1920 cfi->regs[reg].base = base;
1921 cfi->regs[reg].offset = offset;
1925 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1927 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1928 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1932 * A note about DRAP stack alignment:
1934 * GCC has the concept of a DRAP register, which is used to help keep track of
1935 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1936 * register. The typical DRAP pattern is:
1938 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1939 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1940 * 41 ff 72 f8 pushq -0x8(%r10)
1942 * 48 89 e5 mov %rsp,%rbp
1949 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1952 * There are some variations in the epilogues, like:
1960 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1965 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1966 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1967 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1968 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1970 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1973 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1974 * restored beforehand:
1977 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1978 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1980 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1984 static int update_cfi_state(struct instruction *insn,
1985 struct instruction *next_insn,
1986 struct cfi_state *cfi, struct stack_op *op)
1988 struct cfi_reg *cfa = &cfi->cfa;
1989 struct cfi_reg *regs = cfi->regs;
1991 /* stack operations don't make sense with an undefined CFA */
1992 if (cfa->base == CFI_UNDEFINED) {
1994 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2000 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2001 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2002 return update_cfi_state_regs(insn, cfi, op);
2004 switch (op->dest.type) {
2007 switch (op->src.type) {
2010 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2011 cfa->base == CFI_SP &&
2012 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2014 /* mov %rsp, %rbp */
2015 cfa->base = op->dest.reg;
2016 cfi->bp_scratch = false;
2019 else if (op->src.reg == CFI_SP &&
2020 op->dest.reg == CFI_BP && cfi->drap) {
2022 /* drap: mov %rsp, %rbp */
2023 regs[CFI_BP].base = CFI_BP;
2024 regs[CFI_BP].offset = -cfi->stack_size;
2025 cfi->bp_scratch = false;
2028 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2033 * This is needed for the rare case where GCC
2040 cfi->vals[op->dest.reg].base = CFI_CFA;
2041 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2044 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2045 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2050 * Restore the original stack pointer (Clang).
2052 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2055 else if (op->dest.reg == cfa->base) {
2057 /* mov %reg, %rsp */
2058 if (cfa->base == CFI_SP &&
2059 cfi->vals[op->src.reg].base == CFI_CFA) {
2062 * This is needed for the rare case
2063 * where GCC does something dumb like:
2065 * lea 0x8(%rsp), %rcx
2069 cfa->offset = -cfi->vals[op->src.reg].offset;
2070 cfi->stack_size = cfa->offset;
2072 } else if (cfa->base == CFI_SP &&
2073 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2074 cfi->vals[op->src.reg].offset == cfa->offset) {
2079 * 1: mov %rsp, (%[tos])
2080 * 2: mov %[tos], %rsp
2086 * 1 - places a pointer to the previous
2087 * stack at the Top-of-Stack of the
2090 * 2 - switches to the new stack.
2092 * 3 - pops the Top-of-Stack to restore
2093 * the original stack.
2095 * Note: we set base to SP_INDIRECT
2096 * here and preserve offset. Therefore
2097 * when the unwinder reaches ToS it
2098 * will dereference SP and then add the
2099 * offset to find the next frame, IOW:
2102 cfa->base = CFI_SP_INDIRECT;
2105 cfa->base = CFI_UNDEFINED;
2110 else if (op->dest.reg == CFI_SP &&
2111 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2112 cfi->vals[op->src.reg].offset == cfa->offset) {
2115 * The same stack swizzle case 2) as above. But
2116 * because we can't change cfa->base, case 3)
2117 * will become a regular POP. Pretend we're a
2118 * PUSH so things don't go unbalanced.
2120 cfi->stack_size += 8;
2127 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2130 cfi->stack_size -= op->src.offset;
2131 if (cfa->base == CFI_SP)
2132 cfa->offset -= op->src.offset;
2136 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2138 /* lea disp(%rbp), %rsp */
2139 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2143 if (!cfi->drap && op->src.reg == CFI_SP &&
2144 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2145 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2147 /* lea disp(%rsp), %rbp */
2149 cfa->offset -= op->src.offset;
2150 cfi->bp_scratch = false;
2154 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2156 /* drap: lea disp(%rsp), %drap */
2157 cfi->drap_reg = op->dest.reg;
2160 * lea disp(%rsp), %reg
2162 * This is needed for the rare case where GCC
2163 * does something dumb like:
2165 * lea 0x8(%rsp), %rcx
2169 cfi->vals[op->dest.reg].base = CFI_CFA;
2170 cfi->vals[op->dest.reg].offset = \
2171 -cfi->stack_size + op->src.offset;
2176 if (cfi->drap && op->dest.reg == CFI_SP &&
2177 op->src.reg == cfi->drap_reg) {
2179 /* drap: lea disp(%drap), %rsp */
2181 cfa->offset = cfi->stack_size = -op->src.offset;
2182 cfi->drap_reg = CFI_UNDEFINED;
2187 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2188 WARN_FUNC("unsupported stack register modification",
2189 insn->sec, insn->offset);
2196 if (op->dest.reg != CFI_SP ||
2197 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2198 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2199 WARN_FUNC("unsupported stack pointer realignment",
2200 insn->sec, insn->offset);
2204 if (cfi->drap_reg != CFI_UNDEFINED) {
2205 /* drap: and imm, %rsp */
2206 cfa->base = cfi->drap_reg;
2207 cfa->offset = cfi->stack_size = 0;
2212 * Older versions of GCC (4.8ish) realign the stack
2213 * without DRAP, with a frame pointer.
2220 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2222 /* pop %rsp; # restore from a stack swizzle */
2227 if (!cfi->drap && op->dest.reg == cfa->base) {
2233 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2234 op->dest.reg == cfi->drap_reg &&
2235 cfi->drap_offset == -cfi->stack_size) {
2237 /* drap: pop %drap */
2238 cfa->base = cfi->drap_reg;
2240 cfi->drap_offset = -1;
2242 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2245 restore_reg(cfi, op->dest.reg);
2248 cfi->stack_size -= 8;
2249 if (cfa->base == CFI_SP)
2254 case OP_SRC_REG_INDIRECT:
2255 if (!cfi->drap && op->dest.reg == cfa->base &&
2256 op->dest.reg == CFI_BP) {
2258 /* mov disp(%rsp), %rbp */
2260 cfa->offset = cfi->stack_size;
2263 if (cfi->drap && op->src.reg == CFI_BP &&
2264 op->src.offset == cfi->drap_offset) {
2266 /* drap: mov disp(%rbp), %drap */
2267 cfa->base = cfi->drap_reg;
2269 cfi->drap_offset = -1;
2272 if (cfi->drap && op->src.reg == CFI_BP &&
2273 op->src.offset == regs[op->dest.reg].offset) {
2275 /* drap: mov disp(%rbp), %reg */
2276 restore_reg(cfi, op->dest.reg);
2278 } else if (op->src.reg == cfa->base &&
2279 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2281 /* mov disp(%rbp), %reg */
2282 /* mov disp(%rsp), %reg */
2283 restore_reg(cfi, op->dest.reg);
2285 } else if (op->src.reg == CFI_SP &&
2286 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2288 /* mov disp(%rsp), %reg */
2289 restore_reg(cfi, op->dest.reg);
2295 WARN_FUNC("unknown stack-related instruction",
2296 insn->sec, insn->offset);
2304 cfi->stack_size += 8;
2305 if (cfa->base == CFI_SP)
2308 if (op->src.type != OP_SRC_REG)
2312 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2314 /* drap: push %drap */
2315 cfa->base = CFI_BP_INDIRECT;
2316 cfa->offset = -cfi->stack_size;
2318 /* save drap so we know when to restore it */
2319 cfi->drap_offset = -cfi->stack_size;
2321 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2323 /* drap: push %rbp */
2324 cfi->stack_size = 0;
2328 /* drap: push %reg */
2329 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2335 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2338 /* detect when asm code uses rbp as a scratch register */
2339 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2340 cfa->base != CFI_BP)
2341 cfi->bp_scratch = true;
2344 case OP_DEST_REG_INDIRECT:
2347 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2349 /* drap: mov %drap, disp(%rbp) */
2350 cfa->base = CFI_BP_INDIRECT;
2351 cfa->offset = op->dest.offset;
2353 /* save drap offset so we know when to restore it */
2354 cfi->drap_offset = op->dest.offset;
2357 /* drap: mov reg, disp(%rbp) */
2358 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2361 } else if (op->dest.reg == cfa->base) {
2363 /* mov reg, disp(%rbp) */
2364 /* mov reg, disp(%rsp) */
2365 save_reg(cfi, op->src.reg, CFI_CFA,
2366 op->dest.offset - cfi->cfa.offset);
2368 } else if (op->dest.reg == CFI_SP) {
2370 /* mov reg, disp(%rsp) */
2371 save_reg(cfi, op->src.reg, CFI_CFA,
2372 op->dest.offset - cfi->stack_size);
2374 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2376 /* mov %rsp, (%reg); # setup a stack swizzle. */
2377 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2378 cfi->vals[op->dest.reg].offset = cfa->offset;
2384 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2385 WARN_FUNC("unknown stack-related memory operation",
2386 insn->sec, insn->offset);
2391 cfi->stack_size -= 8;
2392 if (cfa->base == CFI_SP)
2398 WARN_FUNC("unknown stack-related instruction",
2399 insn->sec, insn->offset);
2407 * The stack layouts of alternatives instructions can sometimes diverge when
2408 * they have stack modifications. That's fine as long as the potential stack
2409 * layouts don't conflict at any given potential instruction boundary.
2411 * Flatten the CFIs of the different alternative code streams (both original
2412 * and replacement) into a single shared CFI array which can be used to detect
2413 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2415 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2417 struct cfi_state **alt_cfi;
2420 if (!insn->alt_group)
2423 alt_cfi = insn->alt_group->cfi;
2424 group_off = insn->offset - insn->alt_group->first_insn->offset;
2426 if (!alt_cfi[group_off]) {
2427 alt_cfi[group_off] = &insn->cfi;
2429 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2430 WARN_FUNC("stack layout conflict in alternatives",
2431 insn->sec, insn->offset);
2439 static int handle_insn_ops(struct instruction *insn,
2440 struct instruction *next_insn,
2441 struct insn_state *state)
2443 struct stack_op *op;
2445 list_for_each_entry(op, &insn->stack_ops, list) {
2447 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2450 if (!insn->alt_group)
2453 if (op->dest.type == OP_DEST_PUSHF) {
2454 if (!state->uaccess_stack) {
2455 state->uaccess_stack = 1;
2456 } else if (state->uaccess_stack >> 31) {
2457 WARN_FUNC("PUSHF stack exhausted",
2458 insn->sec, insn->offset);
2461 state->uaccess_stack <<= 1;
2462 state->uaccess_stack |= state->uaccess;
2465 if (op->src.type == OP_SRC_POPF) {
2466 if (state->uaccess_stack) {
2467 state->uaccess = state->uaccess_stack & 1;
2468 state->uaccess_stack >>= 1;
2469 if (state->uaccess_stack == 1)
2470 state->uaccess_stack = 0;
2478 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2480 struct cfi_state *cfi1 = &insn->cfi;
2483 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2485 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2486 insn->sec, insn->offset,
2487 cfi1->cfa.base, cfi1->cfa.offset,
2488 cfi2->cfa.base, cfi2->cfa.offset);
2490 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2491 for (i = 0; i < CFI_NUM_REGS; i++) {
2492 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2493 sizeof(struct cfi_reg)))
2496 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2497 insn->sec, insn->offset,
2498 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2499 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2503 } else if (cfi1->type != cfi2->type) {
2505 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2506 insn->sec, insn->offset, cfi1->type, cfi2->type);
2508 } else if (cfi1->drap != cfi2->drap ||
2509 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2510 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2512 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2513 insn->sec, insn->offset,
2514 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2515 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2523 static inline bool func_uaccess_safe(struct symbol *func)
2526 return func->uaccess_safe;
2531 static inline const char *call_dest_name(struct instruction *insn)
2533 if (insn->call_dest)
2534 return insn->call_dest->name;
2539 static inline bool noinstr_call_dest(struct symbol *func)
2542 * We can't deal with indirect function calls at present;
2543 * assume they're instrumented.
2549 * If the symbol is from a noinstr section; we good.
2551 if (func->sec->noinstr)
2555 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2556 * something 'BAD' happened. At the risk of taking the machine down,
2557 * let them proceed to get the message out.
2559 if (!strncmp(func->name, "__ubsan_handle_", 15))
2565 static int validate_call(struct instruction *insn, struct insn_state *state)
2567 if (state->noinstr && state->instr <= 0 &&
2568 !noinstr_call_dest(insn->call_dest)) {
2569 WARN_FUNC("call to %s() leaves .noinstr.text section",
2570 insn->sec, insn->offset, call_dest_name(insn));
2574 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2575 WARN_FUNC("call to %s() with UACCESS enabled",
2576 insn->sec, insn->offset, call_dest_name(insn));
2581 WARN_FUNC("call to %s() with DF set",
2582 insn->sec, insn->offset, call_dest_name(insn));
2589 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2591 if (has_modified_stack_frame(insn, state)) {
2592 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2593 insn->sec, insn->offset);
2597 return validate_call(insn, state);
2600 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2602 if (state->noinstr && state->instr > 0) {
2603 WARN_FUNC("return with instrumentation enabled",
2604 insn->sec, insn->offset);
2608 if (state->uaccess && !func_uaccess_safe(func)) {
2609 WARN_FUNC("return with UACCESS enabled",
2610 insn->sec, insn->offset);
2614 if (!state->uaccess && func_uaccess_safe(func)) {
2615 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2616 insn->sec, insn->offset);
2621 WARN_FUNC("return with DF set",
2622 insn->sec, insn->offset);
2626 if (func && has_modified_stack_frame(insn, state)) {
2627 WARN_FUNC("return with modified stack frame",
2628 insn->sec, insn->offset);
2632 if (state->cfi.bp_scratch) {
2633 WARN_FUNC("BP used as a scratch register",
2634 insn->sec, insn->offset);
2641 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2642 struct instruction *insn)
2644 struct alt_group *alt_group = insn->alt_group;
2647 * Simulate the fact that alternatives are patched in-place. When the
2648 * end of a replacement alt_group is reached, redirect objtool flow to
2649 * the end of the original alt_group.
2651 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2652 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2654 return next_insn_same_sec(file, insn);
2658 * Follow the branch starting at the given instruction, and recursively follow
2659 * any other branches (jumps). Meanwhile, track the frame pointer state at
2660 * each instruction and validate all the rules described in
2661 * tools/objtool/Documentation/stack-validation.txt.
2663 static int validate_branch(struct objtool_file *file, struct symbol *func,
2664 struct instruction *insn, struct insn_state state)
2666 struct alternative *alt;
2667 struct instruction *next_insn;
2668 struct section *sec;
2675 next_insn = next_insn_to_validate(file, insn);
2677 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2678 WARN("%s() falls through to next function %s()",
2679 func->name, insn->func->name);
2683 if (func && insn->ignore) {
2684 WARN_FUNC("BUG: why am I validating an ignored function?",
2689 visited = 1 << state.uaccess;
2690 if (insn->visited) {
2691 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2694 if (insn->visited & visited)
2699 state.instr += insn->instr;
2702 state.cfi = insn->cfi;
2704 insn->cfi = state.cfi;
2706 insn->visited |= visited;
2708 if (propagate_alt_cfi(file, insn))
2711 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2712 bool skip_orig = false;
2714 list_for_each_entry(alt, &insn->alts, list) {
2718 ret = validate_branch(file, func, alt->insn, state);
2721 BT_FUNC("(alt)", insn);
2730 if (handle_insn_ops(insn, next_insn, &state))
2733 switch (insn->type) {
2736 return validate_return(func, insn, &state);
2739 case INSN_CALL_DYNAMIC:
2740 ret = validate_call(insn, &state);
2744 if (!no_fp && func && !is_fentry_call(insn) &&
2745 !has_valid_stack_frame(&state)) {
2746 WARN_FUNC("call without frame pointer save/setup",
2751 if (dead_end_function(file, insn->call_dest))
2756 case INSN_JUMP_CONDITIONAL:
2757 case INSN_JUMP_UNCONDITIONAL:
2758 if (is_sibling_call(insn)) {
2759 ret = validate_sibling_call(insn, &state);
2763 } else if (insn->jump_dest) {
2764 ret = validate_branch(file, func,
2765 insn->jump_dest, state);
2768 BT_FUNC("(branch)", insn);
2773 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2778 case INSN_JUMP_DYNAMIC:
2779 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2780 if (is_sibling_call(insn)) {
2781 ret = validate_sibling_call(insn, &state);
2786 if (insn->type == INSN_JUMP_DYNAMIC)
2791 case INSN_CONTEXT_SWITCH:
2792 if (func && (!next_insn || !next_insn->hint)) {
2793 WARN_FUNC("unsupported instruction in callable function",
2800 if (state.uaccess) {
2801 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2805 state.uaccess = true;
2809 if (!state.uaccess && func) {
2810 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2814 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2815 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2819 state.uaccess = false;
2824 WARN_FUNC("recursive STD", sec, insn->offset);
2832 if (!state.df && func) {
2833 WARN_FUNC("redundant CLD", sec, insn->offset);
2848 if (state.cfi.cfa.base == CFI_UNDEFINED)
2850 WARN("%s: unexpected end of section", sec->name);
2860 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2862 struct instruction *insn;
2863 struct insn_state state;
2864 int ret, warnings = 0;
2869 init_insn_state(&state, sec);
2872 insn = find_insn(file, sec, 0);
2876 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2879 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2880 if (insn->hint && !insn->visited) {
2881 ret = validate_branch(file, insn->func, insn, state);
2882 if (ret && backtrace)
2883 BT_FUNC("<=== (hint)", insn);
2887 insn = list_next_entry(insn, list);
2893 static int validate_retpoline(struct objtool_file *file)
2895 struct instruction *insn;
2898 for_each_insn(file, insn) {
2899 if (insn->type != INSN_JUMP_DYNAMIC &&
2900 insn->type != INSN_CALL_DYNAMIC)
2903 if (insn->retpoline_safe)
2907 * .init.text code is ran before userspace and thus doesn't
2908 * strictly need retpolines, except for modules which are
2909 * loaded late, they very much do need retpoline in their
2912 if (!strcmp(insn->sec->name, ".init.text") && !module)
2915 WARN_FUNC("indirect %s found in RETPOLINE build",
2916 insn->sec, insn->offset,
2917 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2925 static bool is_kasan_insn(struct instruction *insn)
2927 return (insn->type == INSN_CALL &&
2928 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2931 static bool is_ubsan_insn(struct instruction *insn)
2933 return (insn->type == INSN_CALL &&
2934 !strcmp(insn->call_dest->name,
2935 "__ubsan_handle_builtin_unreachable"));
2938 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
2941 struct instruction *prev_insn;
2943 if (insn->ignore || insn->type == INSN_NOP)
2947 * Ignore any unused exceptions. This can happen when a whitelisted
2948 * function has an exception table entry.
2950 * Also ignore alternative replacement instructions. This can happen
2951 * when a whitelisted function uses one of the ALTERNATIVE macros.
2953 if (!strcmp(insn->sec->name, ".fixup") ||
2954 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2955 !strcmp(insn->sec->name, ".altinstr_aux"))
2962 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2963 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2964 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2965 * (or occasionally a JMP to UD2).
2967 * It may also insert a UD2 after calling a __noreturn function.
2969 prev_insn = list_prev_entry(insn, list);
2970 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
2971 (insn->type == INSN_BUG ||
2972 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2973 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2977 * Check if this (or a subsequent) instruction is related to
2978 * CONFIG_UBSAN or CONFIG_KASAN.
2980 * End the search at 5 instructions to avoid going into the weeds.
2982 for (i = 0; i < 5; i++) {
2984 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2987 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2988 if (insn->jump_dest &&
2989 insn->jump_dest->func == insn->func) {
2990 insn = insn->jump_dest;
2997 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3000 insn = list_next_entry(insn, list);
3006 static int validate_symbol(struct objtool_file *file, struct section *sec,
3007 struct symbol *sym, struct insn_state *state)
3009 struct instruction *insn;
3013 WARN("%s() is missing an ELF size annotation", sym->name);
3017 if (sym->pfunc != sym || sym->alias != sym)
3020 insn = find_insn(file, sec, sym->offset);
3021 if (!insn || insn->ignore || insn->visited)
3024 state->uaccess = sym->uaccess_safe;
3026 ret = validate_branch(file, insn->func, insn, *state);
3027 if (ret && backtrace)
3028 BT_FUNC("<=== (sym)", insn);
3032 static int validate_section(struct objtool_file *file, struct section *sec)
3034 struct insn_state state;
3035 struct symbol *func;
3038 list_for_each_entry(func, &sec->symbol_list, list) {
3039 if (func->type != STT_FUNC)
3042 init_insn_state(&state, sec);
3043 set_func_state(&state.cfi);
3045 warnings += validate_symbol(file, sec, func, &state);
3051 static int validate_vmlinux_functions(struct objtool_file *file)
3053 struct section *sec;
3056 sec = find_section_by_name(file->elf, ".noinstr.text");
3058 warnings += validate_section(file, sec);
3059 warnings += validate_unwind_hints(file, sec);
3062 sec = find_section_by_name(file->elf, ".entry.text");
3064 warnings += validate_section(file, sec);
3065 warnings += validate_unwind_hints(file, sec);
3071 static int validate_functions(struct objtool_file *file)
3073 struct section *sec;
3076 for_each_sec(file, sec) {
3077 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3080 warnings += validate_section(file, sec);
3086 static int validate_reachable_instructions(struct objtool_file *file)
3088 struct instruction *insn;
3090 if (file->ignore_unreachables)
3093 for_each_insn(file, insn) {
3094 if (insn->visited || ignore_unreachable_insn(file, insn))
3097 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3104 int check(struct objtool_file *file)
3106 int ret, warnings = 0;
3108 arch_initial_func_cfi_state(&initial_func_cfi);
3110 ret = decode_sections(file);
3115 if (list_empty(&file->insn_list))
3118 if (vmlinux && !validate_dup) {
3119 ret = validate_vmlinux_functions(file);
3128 ret = validate_retpoline(file);
3134 ret = validate_functions(file);
3139 ret = validate_unwind_hints(file, NULL);
3145 ret = validate_reachable_instructions(file);
3151 ret = create_static_call_sections(file);
3157 ret = create_mcount_loc_sections(file);
3165 * For now, don't fail the kernel build on fatal warnings. These
3166 * errors are still fairly common due to the growing matrix of
3167 * supported toolchains and their recent pace of change.