1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
11 #include <objtool/builtin.h>
12 #include <objtool/cfi.h>
13 #include <objtool/arch.h>
14 #include <objtool/check.h>
15 #include <objtool/special.h>
16 #include <objtool/warn.h>
17 #include <objtool/endianness.h>
19 #include <linux/objtool.h>
20 #include <linux/hashtable.h>
21 #include <linux/kernel.h>
22 #include <linux/static_call_types.h>
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_jump_table_jump(struct instruction *insn)
114 struct alt_group *alt_group = insn->alt_group;
116 if (insn->jump_table)
119 /* Retpoline alternative for a jump table? */
120 return alt_group && alt_group->orig_group &&
121 alt_group->orig_group->first_insn->jump_table;
124 static bool is_sibling_call(struct instruction *insn)
127 * Assume only ELF functions can make sibling calls. This ensures
128 * sibling call detection consistency between vmlinux.o and individual
134 /* An indirect jump is either a sibling call or a jump to a table. */
135 if (insn->type == INSN_JUMP_DYNAMIC)
136 return !is_jump_table_jump(insn);
138 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
139 return (is_static_jump(insn) && insn->call_dest);
143 * This checks to see if the given function is a "noreturn" function.
145 * For global functions which are outside the scope of this object file, we
146 * have to keep a manual list of them.
148 * For local functions, we have to detect them manually by simply looking for
149 * the lack of a return instruction.
151 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
155 struct instruction *insn;
159 * Unfortunately these have to be hard coded because the noreturn
160 * attribute isn't provided in ELF data.
162 static const char * const global_noreturns[] = {
167 "__module_put_and_exit",
173 "machine_real_restart",
174 "rewind_stack_do_exit",
175 "kunit_try_catch_throw",
177 "cpu_bringup_and_idle",
183 if (func->bind == STB_WEAK)
186 if (func->bind == STB_GLOBAL)
187 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
188 if (!strcmp(func->name, global_noreturns[i]))
194 insn = find_insn(file, func->sec, func->offset);
198 func_for_each_insn(file, func, insn) {
201 if (insn->type == INSN_RETURN)
209 * A function can have a sibling call instead of a return. In that
210 * case, the function's dead-end status depends on whether the target
211 * of the sibling call returns.
213 func_for_each_insn(file, func, insn) {
214 if (is_sibling_call(insn)) {
215 struct instruction *dest = insn->jump_dest;
218 /* sibling call to another file */
221 /* local sibling call */
222 if (recursion == 5) {
224 * Infinite recursion: two functions have
225 * sibling calls to each other. This is a very
226 * rare case. It means they aren't dead ends.
231 return __dead_end_function(file, dest->func, recursion+1);
238 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
240 return __dead_end_function(file, func, 0);
243 static void init_cfi_state(struct cfi_state *cfi)
247 for (i = 0; i < CFI_NUM_REGS; i++) {
248 cfi->regs[i].base = CFI_UNDEFINED;
249 cfi->vals[i].base = CFI_UNDEFINED;
251 cfi->cfa.base = CFI_UNDEFINED;
252 cfi->drap_reg = CFI_UNDEFINED;
253 cfi->drap_offset = -1;
256 static void init_insn_state(struct insn_state *state, struct section *sec)
258 memset(state, 0, sizeof(*state));
259 init_cfi_state(&state->cfi);
262 * We need the full vmlinux for noinstr validation, otherwise we can
263 * not correctly determine insn->call_dest->sec (external symbols do
264 * not have a section).
266 if (vmlinux && noinstr && sec)
267 state->noinstr = sec->noinstr;
271 * Call the arch-specific instruction decoder for all the instructions and add
272 * them to the global instruction list.
274 static int decode_instructions(struct objtool_file *file)
278 unsigned long offset;
279 struct instruction *insn;
280 unsigned long nr_insns = 0;
283 for_each_sec(file, sec) {
285 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
288 if (strcmp(sec->name, ".altinstr_replacement") &&
289 strcmp(sec->name, ".altinstr_aux") &&
290 strncmp(sec->name, ".discard.", 9))
293 if (!strcmp(sec->name, ".noinstr.text") ||
294 !strcmp(sec->name, ".entry.text"))
297 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
298 insn = malloc(sizeof(*insn));
300 WARN("malloc failed");
303 memset(insn, 0, sizeof(*insn));
304 INIT_LIST_HEAD(&insn->alts);
305 INIT_LIST_HEAD(&insn->stack_ops);
306 init_cfi_state(&insn->cfi);
309 insn->offset = offset;
311 ret = arch_decode_instruction(file->elf, sec, offset,
312 sec->sh.sh_size - offset,
313 &insn->len, &insn->type,
319 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
320 list_add_tail(&insn->list, &file->insn_list);
324 list_for_each_entry(func, &sec->symbol_list, list) {
325 if (func->type != STT_FUNC || func->alias != func)
328 if (!find_insn(file, sec, func->offset)) {
329 WARN("%s(): can't find starting instruction",
334 sym_for_each_insn(file, func, insn)
340 printf("nr_insns: %lu\n", nr_insns);
349 static struct instruction *find_last_insn(struct objtool_file *file,
352 struct instruction *insn = NULL;
354 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
356 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
357 insn = find_insn(file, sec, offset);
363 * Mark "ud2" instructions and manually annotated dead ends.
365 static int add_dead_ends(struct objtool_file *file)
369 struct instruction *insn;
372 * By default, "ud2" is a dead end unless otherwise annotated, because
373 * GCC 7 inserts it for certain divide-by-zero cases.
375 for_each_insn(file, insn)
376 if (insn->type == INSN_BUG)
377 insn->dead_end = true;
380 * Check for manually annotated dead ends.
382 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
386 list_for_each_entry(reloc, &sec->reloc_list, list) {
387 if (reloc->sym->type != STT_SECTION) {
388 WARN("unexpected relocation symbol type in %s", sec->name);
391 insn = find_insn(file, reloc->sym->sec, reloc->addend);
393 insn = list_prev_entry(insn, list);
394 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
395 insn = find_last_insn(file, reloc->sym->sec);
397 WARN("can't find unreachable insn at %s+0x%" PRIx64,
398 reloc->sym->sec->name, reloc->addend);
402 WARN("can't find unreachable insn at %s+0x%" PRIx64,
403 reloc->sym->sec->name, reloc->addend);
407 insn->dead_end = true;
412 * These manually annotated reachable checks are needed for GCC 4.4,
413 * where the Linux unreachable() macro isn't supported. In that case
414 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
417 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
421 list_for_each_entry(reloc, &sec->reloc_list, list) {
422 if (reloc->sym->type != STT_SECTION) {
423 WARN("unexpected relocation symbol type in %s", sec->name);
426 insn = find_insn(file, reloc->sym->sec, reloc->addend);
428 insn = list_prev_entry(insn, list);
429 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
430 insn = find_last_insn(file, reloc->sym->sec);
432 WARN("can't find reachable insn at %s+0x%" PRIx64,
433 reloc->sym->sec->name, reloc->addend);
437 WARN("can't find reachable insn at %s+0x%" PRIx64,
438 reloc->sym->sec->name, reloc->addend);
442 insn->dead_end = false;
448 static int create_static_call_sections(struct objtool_file *file)
451 struct static_call_site *site;
452 struct instruction *insn;
453 struct symbol *key_sym;
454 char *key_name, *tmp;
457 sec = find_section_by_name(file->elf, ".static_call_sites");
459 INIT_LIST_HEAD(&file->static_call_list);
460 WARN("file already has .static_call_sites section, skipping");
464 if (list_empty(&file->static_call_list))
468 list_for_each_entry(insn, &file->static_call_list, call_node)
471 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
472 sizeof(struct static_call_site), idx);
477 list_for_each_entry(insn, &file->static_call_list, call_node) {
479 site = (struct static_call_site *)sec->data->d_buf + idx;
480 memset(site, 0, sizeof(struct static_call_site));
482 /* populate reloc for 'addr' */
483 if (elf_add_reloc_to_insn(file->elf, sec,
484 idx * sizeof(struct static_call_site),
486 insn->sec, insn->offset))
489 /* find key symbol */
490 key_name = strdup(insn->call_dest->name);
495 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
496 STATIC_CALL_TRAMP_PREFIX_LEN)) {
497 WARN("static_call: trampoline name malformed: %s", key_name);
500 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
501 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
503 key_sym = find_symbol_by_name(file->elf, tmp);
506 WARN("static_call: can't find static_call_key symbol: %s", tmp);
511 * For modules(), the key might not be exported, which
512 * means the module can make static calls but isn't
513 * allowed to change them.
515 * In that case we temporarily set the key to be the
516 * trampoline address. This is fixed up in
517 * static_call_add_module().
519 key_sym = insn->call_dest;
523 /* populate reloc for 'key' */
524 if (elf_add_reloc(file->elf, sec,
525 idx * sizeof(struct static_call_site) + 4,
526 R_X86_64_PC32, key_sym,
527 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
536 static int create_mcount_loc_sections(struct objtool_file *file)
540 struct instruction *insn;
543 sec = find_section_by_name(file->elf, "__mcount_loc");
545 INIT_LIST_HEAD(&file->mcount_loc_list);
546 WARN("file already has __mcount_loc section, skipping");
550 if (list_empty(&file->mcount_loc_list))
554 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
557 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
562 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
564 loc = (unsigned long *)sec->data->d_buf + idx;
565 memset(loc, 0, sizeof(unsigned long));
567 if (elf_add_reloc_to_insn(file->elf, sec,
568 idx * sizeof(unsigned long),
570 insn->sec, insn->offset))
580 * Warnings shouldn't be reported for ignored functions.
582 static void add_ignores(struct objtool_file *file)
584 struct instruction *insn;
589 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
593 list_for_each_entry(reloc, &sec->reloc_list, list) {
594 switch (reloc->sym->type) {
600 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
606 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
610 func_for_each_insn(file, func, insn)
616 * This is a whitelist of functions that is allowed to be called with AC set.
617 * The list is meant to be minimal and only contains compiler instrumentation
618 * ABI and a few functions used to implement *_{to,from}_user() functions.
620 * These functions must not directly change AC, but may PUSHF/POPF.
622 static const char *uaccess_safe_builtin[] = {
626 /* KASAN out-of-line */
627 "__asan_loadN_noabort",
628 "__asan_load1_noabort",
629 "__asan_load2_noabort",
630 "__asan_load4_noabort",
631 "__asan_load8_noabort",
632 "__asan_load16_noabort",
633 "__asan_storeN_noabort",
634 "__asan_store1_noabort",
635 "__asan_store2_noabort",
636 "__asan_store4_noabort",
637 "__asan_store8_noabort",
638 "__asan_store16_noabort",
639 "__kasan_check_read",
640 "__kasan_check_write",
642 "__asan_report_load_n_noabort",
643 "__asan_report_load1_noabort",
644 "__asan_report_load2_noabort",
645 "__asan_report_load4_noabort",
646 "__asan_report_load8_noabort",
647 "__asan_report_load16_noabort",
648 "__asan_report_store_n_noabort",
649 "__asan_report_store1_noabort",
650 "__asan_report_store2_noabort",
651 "__asan_report_store4_noabort",
652 "__asan_report_store8_noabort",
653 "__asan_report_store16_noabort",
655 "__kcsan_check_access",
656 "kcsan_found_watchpoint",
657 "kcsan_setup_watchpoint",
658 "kcsan_check_scoped_accesses",
659 "kcsan_disable_current",
660 "kcsan_enable_current_nowarn",
665 "__tsan_write_range",
676 "__tsan_read_write1",
677 "__tsan_read_write2",
678 "__tsan_read_write4",
679 "__tsan_read_write8",
680 "__tsan_read_write16",
681 "__tsan_atomic8_load",
682 "__tsan_atomic16_load",
683 "__tsan_atomic32_load",
684 "__tsan_atomic64_load",
685 "__tsan_atomic8_store",
686 "__tsan_atomic16_store",
687 "__tsan_atomic32_store",
688 "__tsan_atomic64_store",
689 "__tsan_atomic8_exchange",
690 "__tsan_atomic16_exchange",
691 "__tsan_atomic32_exchange",
692 "__tsan_atomic64_exchange",
693 "__tsan_atomic8_fetch_add",
694 "__tsan_atomic16_fetch_add",
695 "__tsan_atomic32_fetch_add",
696 "__tsan_atomic64_fetch_add",
697 "__tsan_atomic8_fetch_sub",
698 "__tsan_atomic16_fetch_sub",
699 "__tsan_atomic32_fetch_sub",
700 "__tsan_atomic64_fetch_sub",
701 "__tsan_atomic8_fetch_and",
702 "__tsan_atomic16_fetch_and",
703 "__tsan_atomic32_fetch_and",
704 "__tsan_atomic64_fetch_and",
705 "__tsan_atomic8_fetch_or",
706 "__tsan_atomic16_fetch_or",
707 "__tsan_atomic32_fetch_or",
708 "__tsan_atomic64_fetch_or",
709 "__tsan_atomic8_fetch_xor",
710 "__tsan_atomic16_fetch_xor",
711 "__tsan_atomic32_fetch_xor",
712 "__tsan_atomic64_fetch_xor",
713 "__tsan_atomic8_fetch_nand",
714 "__tsan_atomic16_fetch_nand",
715 "__tsan_atomic32_fetch_nand",
716 "__tsan_atomic64_fetch_nand",
717 "__tsan_atomic8_compare_exchange_strong",
718 "__tsan_atomic16_compare_exchange_strong",
719 "__tsan_atomic32_compare_exchange_strong",
720 "__tsan_atomic64_compare_exchange_strong",
721 "__tsan_atomic8_compare_exchange_weak",
722 "__tsan_atomic16_compare_exchange_weak",
723 "__tsan_atomic32_compare_exchange_weak",
724 "__tsan_atomic64_compare_exchange_weak",
725 "__tsan_atomic8_compare_exchange_val",
726 "__tsan_atomic16_compare_exchange_val",
727 "__tsan_atomic32_compare_exchange_val",
728 "__tsan_atomic64_compare_exchange_val",
729 "__tsan_atomic_thread_fence",
730 "__tsan_atomic_signal_fence",
734 "__sanitizer_cov_trace_pc",
735 "__sanitizer_cov_trace_const_cmp1",
736 "__sanitizer_cov_trace_const_cmp2",
737 "__sanitizer_cov_trace_const_cmp4",
738 "__sanitizer_cov_trace_const_cmp8",
739 "__sanitizer_cov_trace_cmp1",
740 "__sanitizer_cov_trace_cmp2",
741 "__sanitizer_cov_trace_cmp4",
742 "__sanitizer_cov_trace_cmp8",
743 "__sanitizer_cov_trace_switch",
745 "ubsan_type_mismatch_common",
746 "__ubsan_handle_type_mismatch",
747 "__ubsan_handle_type_mismatch_v1",
748 "__ubsan_handle_shift_out_of_bounds",
750 "csum_partial_copy_generic",
752 "copy_mc_fragile_handle_tail",
753 "copy_mc_enhanced_fast_string",
754 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
758 static void add_uaccess_safe(struct objtool_file *file)
766 for (name = uaccess_safe_builtin; *name; name++) {
767 func = find_symbol_by_name(file->elf, *name);
771 func->uaccess_safe = true;
776 * FIXME: For now, just ignore any alternatives which add retpolines. This is
777 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
778 * But it at least allows objtool to understand the control flow *around* the
781 static int add_ignore_alternatives(struct objtool_file *file)
785 struct instruction *insn;
787 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
791 list_for_each_entry(reloc, &sec->reloc_list, list) {
792 if (reloc->sym->type != STT_SECTION) {
793 WARN("unexpected relocation symbol type in %s", sec->name);
797 insn = find_insn(file, reloc->sym->sec, reloc->addend);
799 WARN("bad .discard.ignore_alts entry");
803 insn->ignore_alts = true;
809 __weak bool arch_is_retpoline(struct symbol *sym)
814 #define NEGATIVE_RELOC ((void *)-1L)
816 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
818 if (insn->reloc == NEGATIVE_RELOC)
822 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
823 insn->offset, insn->len);
825 insn->reloc = NEGATIVE_RELOC;
833 static void remove_insn_ops(struct instruction *insn)
835 struct stack_op *op, *tmp;
837 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
843 static void annotate_call_site(struct objtool_file *file,
844 struct instruction *insn, bool sibling)
846 struct reloc *reloc = insn_reloc(file, insn);
847 struct symbol *sym = insn->call_dest;
853 * Alternative replacement code is just template code which is
854 * sometimes copied to the original instruction. For now, don't
855 * annotate it. (In the future we might consider annotating the
856 * original instruction if/when it ever makes sense to do so.)
858 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
861 if (sym->static_call_tramp) {
862 list_add_tail(&insn->call_node, &file->static_call_list);
867 * Many compilers cannot disable KCOV with a function attribute
868 * so they need a little help, NOP out any KCOV calls from noinstr
871 if (insn->sec->noinstr && sym->kcov) {
873 reloc->type = R_NONE;
874 elf_write_reloc(file->elf, reloc);
877 elf_write_insn(file->elf, insn->sec,
878 insn->offset, insn->len,
879 sibling ? arch_ret_insn(insn->len)
880 : arch_nop_insn(insn->len));
882 insn->type = sibling ? INSN_RETURN : INSN_NOP;
886 * We've replaced the tail-call JMP insn by two new
887 * insn: RET; INT3, except we only have a single struct
888 * insn here. Mark it retpoline_safe to avoid the SLS
889 * warning, instead of adding another insn.
891 insn->retpoline_safe = true;
897 if (mcount && sym->fentry) {
899 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
902 reloc->type = R_NONE;
903 elf_write_reloc(file->elf, reloc);
906 elf_write_insn(file->elf, insn->sec,
907 insn->offset, insn->len,
908 arch_nop_insn(insn->len));
910 insn->type = INSN_NOP;
912 list_add_tail(&insn->mcount_loc_node, &file->mcount_loc_list);
917 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
918 struct symbol *dest, bool sibling)
920 insn->call_dest = dest;
925 * Whatever stack impact regular CALLs have, should be undone
926 * by the RETURN of the called function.
928 * Annotated intra-function calls retain the stack_ops but
929 * are converted to JUMP, see read_intra_function_calls().
931 remove_insn_ops(insn);
933 annotate_call_site(file, insn, sibling);
937 * Find the destination instructions for all jumps.
939 static int add_jump_destinations(struct objtool_file *file)
941 struct instruction *insn;
943 struct section *dest_sec;
944 unsigned long dest_off;
946 for_each_insn(file, insn) {
947 if (!is_static_jump(insn))
950 reloc = insn_reloc(file, insn);
952 dest_sec = insn->sec;
953 dest_off = arch_jump_destination(insn);
954 } else if (reloc->sym->type == STT_SECTION) {
955 dest_sec = reloc->sym->sec;
956 dest_off = arch_dest_reloc_offset(reloc->addend);
957 } else if (reloc->sym->retpoline_thunk) {
959 * Retpoline jumps are really dynamic jumps in
960 * disguise, so convert them accordingly.
962 if (insn->type == INSN_JUMP_UNCONDITIONAL)
963 insn->type = INSN_JUMP_DYNAMIC;
965 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
967 list_add_tail(&insn->call_node,
968 &file->retpoline_call_list);
970 insn->retpoline_safe = true;
972 } else if (insn->func) {
973 /* internal or external sibling call (with reloc) */
974 add_call_dest(file, insn, reloc->sym, true);
976 } else if (reloc->sym->sec->idx) {
977 dest_sec = reloc->sym->sec;
978 dest_off = reloc->sym->sym.st_value +
979 arch_dest_reloc_offset(reloc->addend);
981 /* non-func asm code jumping to another file */
985 insn->jump_dest = find_insn(file, dest_sec, dest_off);
986 if (!insn->jump_dest) {
989 * This is a special case where an alt instruction
990 * jumps past the end of the section. These are
991 * handled later in handle_group_alt().
993 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
996 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
997 insn->sec, insn->offset, dest_sec->name,
1003 * Cross-function jump.
1005 if (insn->func && insn->jump_dest->func &&
1006 insn->func != insn->jump_dest->func) {
1009 * For GCC 8+, create parent/child links for any cold
1010 * subfunctions. This is _mostly_ redundant with a
1011 * similar initialization in read_symbols().
1013 * If a function has aliases, we want the *first* such
1014 * function in the symbol table to be the subfunction's
1015 * parent. In that case we overwrite the
1016 * initialization done in read_symbols().
1018 * However this code can't completely replace the
1019 * read_symbols() code because this doesn't detect the
1020 * case where the parent function's only reference to a
1021 * subfunction is through a jump table.
1023 if (!strstr(insn->func->name, ".cold") &&
1024 strstr(insn->jump_dest->func->name, ".cold")) {
1025 insn->func->cfunc = insn->jump_dest->func;
1026 insn->jump_dest->func->pfunc = insn->func;
1028 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1029 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1030 /* internal sibling call (without reloc) */
1031 add_call_dest(file, insn, insn->jump_dest->func, true);
1039 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1041 struct symbol *call_dest;
1043 call_dest = find_func_by_offset(sec, offset);
1045 call_dest = find_symbol_by_offset(sec, offset);
1051 * Find the destination instructions for all calls.
1053 static int add_call_destinations(struct objtool_file *file)
1055 struct instruction *insn;
1056 unsigned long dest_off;
1057 struct symbol *dest;
1058 struct reloc *reloc;
1060 for_each_insn(file, insn) {
1061 if (insn->type != INSN_CALL)
1064 reloc = insn_reloc(file, insn);
1066 dest_off = arch_jump_destination(insn);
1067 dest = find_call_destination(insn->sec, dest_off);
1069 add_call_dest(file, insn, dest, false);
1074 if (!insn->call_dest) {
1075 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1079 if (insn->func && insn->call_dest->type != STT_FUNC) {
1080 WARN_FUNC("unsupported call to non-function",
1081 insn->sec, insn->offset);
1085 } else if (reloc->sym->type == STT_SECTION) {
1086 dest_off = arch_dest_reloc_offset(reloc->addend);
1087 dest = find_call_destination(reloc->sym->sec, dest_off);
1089 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1090 insn->sec, insn->offset,
1091 reloc->sym->sec->name,
1096 add_call_dest(file, insn, dest, false);
1098 } else if (reloc->sym->retpoline_thunk) {
1100 * Retpoline calls are really dynamic calls in
1101 * disguise, so convert them accordingly.
1103 insn->type = INSN_CALL_DYNAMIC;
1104 insn->retpoline_safe = true;
1106 list_add_tail(&insn->call_node,
1107 &file->retpoline_call_list);
1109 remove_insn_ops(insn);
1113 add_call_dest(file, insn, reloc->sym, false);
1120 * The .alternatives section requires some extra special care over and above
1121 * other special sections because alternatives are patched in place.
1123 static int handle_group_alt(struct objtool_file *file,
1124 struct special_alt *special_alt,
1125 struct instruction *orig_insn,
1126 struct instruction **new_insn)
1128 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1129 struct alt_group *orig_alt_group, *new_alt_group;
1130 unsigned long dest_off;
1133 orig_alt_group = malloc(sizeof(*orig_alt_group));
1134 if (!orig_alt_group) {
1135 WARN("malloc failed");
1138 orig_alt_group->cfi = calloc(special_alt->orig_len,
1139 sizeof(struct cfi_state *));
1140 if (!orig_alt_group->cfi) {
1141 WARN("calloc failed");
1145 last_orig_insn = NULL;
1147 sec_for_each_insn_from(file, insn) {
1148 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1151 insn->alt_group = orig_alt_group;
1152 last_orig_insn = insn;
1154 orig_alt_group->orig_group = NULL;
1155 orig_alt_group->first_insn = orig_insn;
1156 orig_alt_group->last_insn = last_orig_insn;
1159 new_alt_group = malloc(sizeof(*new_alt_group));
1160 if (!new_alt_group) {
1161 WARN("malloc failed");
1165 if (special_alt->new_len < special_alt->orig_len) {
1167 * Insert a fake nop at the end to make the replacement
1168 * alt_group the same size as the original. This is needed to
1169 * allow propagate_alt_cfi() to do its magic. When the last
1170 * instruction affects the stack, the instruction after it (the
1171 * nop) will propagate the new state to the shared CFI array.
1173 nop = malloc(sizeof(*nop));
1175 WARN("malloc failed");
1178 memset(nop, 0, sizeof(*nop));
1179 INIT_LIST_HEAD(&nop->alts);
1180 INIT_LIST_HEAD(&nop->stack_ops);
1181 init_cfi_state(&nop->cfi);
1183 nop->sec = special_alt->new_sec;
1184 nop->offset = special_alt->new_off + special_alt->new_len;
1185 nop->len = special_alt->orig_len - special_alt->new_len;
1186 nop->type = INSN_NOP;
1187 nop->func = orig_insn->func;
1188 nop->alt_group = new_alt_group;
1189 nop->ignore = orig_insn->ignore_alts;
1192 if (!special_alt->new_len) {
1198 sec_for_each_insn_from(file, insn) {
1199 struct reloc *alt_reloc;
1201 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1204 last_new_insn = insn;
1206 insn->ignore = orig_insn->ignore_alts;
1207 insn->func = orig_insn->func;
1208 insn->alt_group = new_alt_group;
1211 * Since alternative replacement code is copy/pasted by the
1212 * kernel after applying relocations, generally such code can't
1213 * have relative-address relocation references to outside the
1214 * .altinstr_replacement section, unless the arch's
1215 * alternatives code can adjust the relative offsets
1218 alt_reloc = insn_reloc(file, insn);
1220 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1222 WARN_FUNC("unsupported relocation in alternatives section",
1223 insn->sec, insn->offset);
1227 if (!is_static_jump(insn))
1230 if (!insn->immediate)
1233 dest_off = arch_jump_destination(insn);
1234 if (dest_off == special_alt->new_off + special_alt->new_len)
1235 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1237 if (!insn->jump_dest) {
1238 WARN_FUNC("can't find alternative jump destination",
1239 insn->sec, insn->offset);
1244 if (!last_new_insn) {
1245 WARN_FUNC("can't find last new alternative instruction",
1246 special_alt->new_sec, special_alt->new_off);
1251 list_add(&nop->list, &last_new_insn->list);
1253 new_alt_group->orig_group = orig_alt_group;
1254 new_alt_group->first_insn = *new_insn;
1255 new_alt_group->last_insn = nop ? : last_new_insn;
1256 new_alt_group->cfi = orig_alt_group->cfi;
1261 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1262 * If the original instruction is a jump, make the alt entry an effective nop
1263 * by just skipping the original instruction.
1265 static int handle_jump_alt(struct objtool_file *file,
1266 struct special_alt *special_alt,
1267 struct instruction *orig_insn,
1268 struct instruction **new_insn)
1270 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1271 orig_insn->type != INSN_NOP) {
1273 WARN_FUNC("unsupported instruction at jump label",
1274 orig_insn->sec, orig_insn->offset);
1278 if (special_alt->key_addend & 2) {
1279 struct reloc *reloc = insn_reloc(file, orig_insn);
1282 reloc->type = R_NONE;
1283 elf_write_reloc(file->elf, reloc);
1285 elf_write_insn(file->elf, orig_insn->sec,
1286 orig_insn->offset, orig_insn->len,
1287 arch_nop_insn(orig_insn->len));
1288 orig_insn->type = INSN_NOP;
1291 if (orig_insn->type == INSN_NOP) {
1292 if (orig_insn->len == 2)
1293 file->jl_nop_short++;
1295 file->jl_nop_long++;
1300 if (orig_insn->len == 2)
1305 *new_insn = list_next_entry(orig_insn, list);
1310 * Read all the special sections which have alternate instructions which can be
1311 * patched in or redirected to at runtime. Each instruction having alternate
1312 * instruction(s) has them added to its insn->alts list, which will be
1313 * traversed in validate_branch().
1315 static int add_special_section_alts(struct objtool_file *file)
1317 struct list_head special_alts;
1318 struct instruction *orig_insn, *new_insn;
1319 struct special_alt *special_alt, *tmp;
1320 struct alternative *alt;
1323 ret = special_get_alts(file->elf, &special_alts);
1327 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1329 orig_insn = find_insn(file, special_alt->orig_sec,
1330 special_alt->orig_off);
1332 WARN_FUNC("special: can't find orig instruction",
1333 special_alt->orig_sec, special_alt->orig_off);
1339 if (!special_alt->group || special_alt->new_len) {
1340 new_insn = find_insn(file, special_alt->new_sec,
1341 special_alt->new_off);
1343 WARN_FUNC("special: can't find new instruction",
1344 special_alt->new_sec,
1345 special_alt->new_off);
1351 if (special_alt->group) {
1352 if (!special_alt->orig_len) {
1353 WARN_FUNC("empty alternative entry",
1354 orig_insn->sec, orig_insn->offset);
1358 ret = handle_group_alt(file, special_alt, orig_insn,
1362 } else if (special_alt->jump_or_nop) {
1363 ret = handle_jump_alt(file, special_alt, orig_insn,
1369 alt = malloc(sizeof(*alt));
1371 WARN("malloc failed");
1376 alt->insn = new_insn;
1377 alt->skip_orig = special_alt->skip_orig;
1378 orig_insn->ignore_alts |= special_alt->skip_alt;
1379 list_add_tail(&alt->list, &orig_insn->alts);
1381 list_del(&special_alt->list);
1386 printf("jl\\\tNOP\tJMP\n");
1387 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1388 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1395 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1396 struct reloc *table)
1398 struct reloc *reloc = table;
1399 struct instruction *dest_insn;
1400 struct alternative *alt;
1401 struct symbol *pfunc = insn->func->pfunc;
1402 unsigned int prev_offset = 0;
1405 * Each @reloc is a switch table relocation which points to the target
1408 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1410 /* Check for the end of the table: */
1411 if (reloc != table && reloc->jump_table_start)
1414 /* Make sure the table entries are consecutive: */
1415 if (prev_offset && reloc->offset != prev_offset + 8)
1418 /* Detect function pointers from contiguous objects: */
1419 if (reloc->sym->sec == pfunc->sec &&
1420 reloc->addend == pfunc->offset)
1423 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1427 /* Make sure the destination is in the same function: */
1428 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1431 alt = malloc(sizeof(*alt));
1433 WARN("malloc failed");
1437 alt->insn = dest_insn;
1438 list_add_tail(&alt->list, &insn->alts);
1439 prev_offset = reloc->offset;
1443 WARN_FUNC("can't find switch jump table",
1444 insn->sec, insn->offset);
1452 * find_jump_table() - Given a dynamic jump, find the switch jump table
1453 * associated with it.
1455 static struct reloc *find_jump_table(struct objtool_file *file,
1456 struct symbol *func,
1457 struct instruction *insn)
1459 struct reloc *table_reloc;
1460 struct instruction *dest_insn, *orig_insn = insn;
1463 * Backward search using the @first_jump_src links, these help avoid
1464 * much of the 'in between' code. Which avoids us getting confused by
1468 insn && insn->func && insn->func->pfunc == func;
1469 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1471 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1474 /* allow small jumps within the range */
1475 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1477 (insn->jump_dest->offset <= insn->offset ||
1478 insn->jump_dest->offset > orig_insn->offset))
1481 table_reloc = arch_find_switch_table(file, insn);
1484 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1485 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1495 * First pass: Mark the head of each jump table so that in the next pass,
1496 * we know when a given jump table ends and the next one starts.
1498 static void mark_func_jump_tables(struct objtool_file *file,
1499 struct symbol *func)
1501 struct instruction *insn, *last = NULL;
1502 struct reloc *reloc;
1504 func_for_each_insn(file, func, insn) {
1509 * Store back-pointers for unconditional forward jumps such
1510 * that find_jump_table() can back-track using those and
1511 * avoid some potentially confusing code.
1513 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1514 insn->offset > last->offset &&
1515 insn->jump_dest->offset > insn->offset &&
1516 !insn->jump_dest->first_jump_src) {
1518 insn->jump_dest->first_jump_src = insn;
1519 last = insn->jump_dest;
1522 if (insn->type != INSN_JUMP_DYNAMIC)
1525 reloc = find_jump_table(file, func, insn);
1527 reloc->jump_table_start = true;
1528 insn->jump_table = reloc;
1533 static int add_func_jump_tables(struct objtool_file *file,
1534 struct symbol *func)
1536 struct instruction *insn;
1539 func_for_each_insn(file, func, insn) {
1540 if (!insn->jump_table)
1543 ret = add_jump_table(file, insn, insn->jump_table);
1552 * For some switch statements, gcc generates a jump table in the .rodata
1553 * section which contains a list of addresses within the function to jump to.
1554 * This finds these jump tables and adds them to the insn->alts lists.
1556 static int add_jump_table_alts(struct objtool_file *file)
1558 struct section *sec;
1559 struct symbol *func;
1565 for_each_sec(file, sec) {
1566 list_for_each_entry(func, &sec->symbol_list, list) {
1567 if (func->type != STT_FUNC)
1570 mark_func_jump_tables(file, func);
1571 ret = add_func_jump_tables(file, func);
1580 static void set_func_state(struct cfi_state *state)
1582 state->cfa = initial_func_cfi.cfa;
1583 memcpy(&state->regs, &initial_func_cfi.regs,
1584 CFI_NUM_REGS * sizeof(struct cfi_reg));
1585 state->stack_size = initial_func_cfi.cfa.offset;
1588 static int read_unwind_hints(struct objtool_file *file)
1590 struct section *sec, *relocsec;
1591 struct reloc *reloc;
1592 struct unwind_hint *hint;
1593 struct instruction *insn;
1596 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1600 relocsec = sec->reloc;
1602 WARN("missing .rela.discard.unwind_hints section");
1606 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1607 WARN("struct unwind_hint size mismatch");
1613 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1614 hint = (struct unwind_hint *)sec->data->d_buf + i;
1616 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1618 WARN("can't find reloc for unwind_hints[%d]", i);
1622 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1624 WARN("can't find insn for unwind_hints[%d]", i);
1630 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1631 set_func_state(&insn->cfi);
1635 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1636 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1637 insn->sec, insn->offset, hint->sp_reg);
1641 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1642 insn->cfi.type = hint->type;
1643 insn->cfi.end = hint->end;
1649 static int read_retpoline_hints(struct objtool_file *file)
1651 struct section *sec;
1652 struct instruction *insn;
1653 struct reloc *reloc;
1655 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1659 list_for_each_entry(reloc, &sec->reloc_list, list) {
1660 if (reloc->sym->type != STT_SECTION) {
1661 WARN("unexpected relocation symbol type in %s", sec->name);
1665 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1667 WARN("bad .discard.retpoline_safe entry");
1671 if (insn->type != INSN_JUMP_DYNAMIC &&
1672 insn->type != INSN_CALL_DYNAMIC) {
1673 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1674 insn->sec, insn->offset);
1678 insn->retpoline_safe = true;
1684 static int read_instr_hints(struct objtool_file *file)
1686 struct section *sec;
1687 struct instruction *insn;
1688 struct reloc *reloc;
1690 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1694 list_for_each_entry(reloc, &sec->reloc_list, list) {
1695 if (reloc->sym->type != STT_SECTION) {
1696 WARN("unexpected relocation symbol type in %s", sec->name);
1700 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1702 WARN("bad .discard.instr_end entry");
1709 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1713 list_for_each_entry(reloc, &sec->reloc_list, list) {
1714 if (reloc->sym->type != STT_SECTION) {
1715 WARN("unexpected relocation symbol type in %s", sec->name);
1719 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1721 WARN("bad .discard.instr_begin entry");
1731 static int read_intra_function_calls(struct objtool_file *file)
1733 struct instruction *insn;
1734 struct section *sec;
1735 struct reloc *reloc;
1737 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1741 list_for_each_entry(reloc, &sec->reloc_list, list) {
1742 unsigned long dest_off;
1744 if (reloc->sym->type != STT_SECTION) {
1745 WARN("unexpected relocation symbol type in %s",
1750 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1752 WARN("bad .discard.intra_function_call entry");
1756 if (insn->type != INSN_CALL) {
1757 WARN_FUNC("intra_function_call not a direct call",
1758 insn->sec, insn->offset);
1763 * Treat intra-function CALLs as JMPs, but with a stack_op.
1764 * See add_call_destinations(), which strips stack_ops from
1767 insn->type = INSN_JUMP_UNCONDITIONAL;
1769 dest_off = insn->offset + insn->len + insn->immediate;
1770 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1771 if (!insn->jump_dest) {
1772 WARN_FUNC("can't find call dest at %s+0x%lx",
1773 insn->sec, insn->offset,
1774 insn->sec->name, dest_off);
1782 static int classify_symbols(struct objtool_file *file)
1784 struct section *sec;
1785 struct symbol *func;
1787 for_each_sec(file, sec) {
1788 list_for_each_entry(func, &sec->symbol_list, list) {
1789 if (func->bind != STB_GLOBAL)
1792 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1793 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1794 func->static_call_tramp = true;
1796 if (arch_is_retpoline(func))
1797 func->retpoline_thunk = true;
1799 if (!strcmp(func->name, "__fentry__"))
1800 func->fentry = true;
1802 if (!strncmp(func->name, "__sanitizer_cov_", 16))
1810 static void mark_rodata(struct objtool_file *file)
1812 struct section *sec;
1816 * Search for the following rodata sections, each of which can
1817 * potentially contain jump tables:
1819 * - .rodata: can contain GCC switch tables
1820 * - .rodata.<func>: same, if -fdata-sections is being used
1821 * - .rodata..c_jump_table: contains C annotated jump tables
1823 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1825 for_each_sec(file, sec) {
1826 if (!strncmp(sec->name, ".rodata", 7) &&
1827 !strstr(sec->name, ".str1.")) {
1833 file->rodata = found;
1836 __weak int arch_rewrite_retpolines(struct objtool_file *file)
1841 static int decode_sections(struct objtool_file *file)
1847 ret = decode_instructions(file);
1851 ret = add_dead_ends(file);
1856 add_uaccess_safe(file);
1858 ret = add_ignore_alternatives(file);
1863 * Must be before add_{jump_call}_destination.
1865 ret = classify_symbols(file);
1870 * Must be before add_special_section_alts() as that depends on
1871 * jump_dest being set.
1873 ret = add_jump_destinations(file);
1877 ret = add_special_section_alts(file);
1882 * Must be before add_call_destination(); it changes INSN_CALL to
1885 ret = read_intra_function_calls(file);
1889 ret = add_call_destinations(file);
1893 ret = add_jump_table_alts(file);
1897 ret = read_unwind_hints(file);
1901 ret = read_retpoline_hints(file);
1905 ret = read_instr_hints(file);
1910 * Must be after add_special_section_alts(), since this will emit
1911 * alternatives. Must be after add_{jump,call}_destination(), since
1912 * those create the call insn lists.
1914 ret = arch_rewrite_retpolines(file);
1921 static bool is_fentry_call(struct instruction *insn)
1923 if (insn->type == INSN_CALL &&
1925 insn->call_dest->fentry)
1931 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1933 struct cfi_state *cfi = &state->cfi;
1936 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1939 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1942 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1945 for (i = 0; i < CFI_NUM_REGS; i++) {
1946 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1947 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1954 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1955 int expected_offset)
1957 return reg->base == CFI_CFA &&
1958 reg->offset == expected_offset;
1961 static bool has_valid_stack_frame(struct insn_state *state)
1963 struct cfi_state *cfi = &state->cfi;
1965 if (cfi->cfa.base == CFI_BP &&
1966 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1967 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1970 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1976 static int update_cfi_state_regs(struct instruction *insn,
1977 struct cfi_state *cfi,
1978 struct stack_op *op)
1980 struct cfi_reg *cfa = &cfi->cfa;
1982 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1986 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1990 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1993 /* add immediate to sp */
1994 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1995 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1996 cfa->offset -= op->src.offset;
2001 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2003 if (arch_callee_saved_reg(reg) &&
2004 cfi->regs[reg].base == CFI_UNDEFINED) {
2005 cfi->regs[reg].base = base;
2006 cfi->regs[reg].offset = offset;
2010 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2012 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2013 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2017 * A note about DRAP stack alignment:
2019 * GCC has the concept of a DRAP register, which is used to help keep track of
2020 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2021 * register. The typical DRAP pattern is:
2023 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2024 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2025 * 41 ff 72 f8 pushq -0x8(%r10)
2027 * 48 89 e5 mov %rsp,%rbp
2034 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2037 * There are some variations in the epilogues, like:
2045 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2050 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2051 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2052 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2053 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2055 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2058 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2059 * restored beforehand:
2062 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2063 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2065 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2069 static int update_cfi_state(struct instruction *insn,
2070 struct instruction *next_insn,
2071 struct cfi_state *cfi, struct stack_op *op)
2073 struct cfi_reg *cfa = &cfi->cfa;
2074 struct cfi_reg *regs = cfi->regs;
2076 /* stack operations don't make sense with an undefined CFA */
2077 if (cfa->base == CFI_UNDEFINED) {
2079 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2085 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2086 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2087 return update_cfi_state_regs(insn, cfi, op);
2089 switch (op->dest.type) {
2092 switch (op->src.type) {
2095 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2096 cfa->base == CFI_SP &&
2097 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2099 /* mov %rsp, %rbp */
2100 cfa->base = op->dest.reg;
2101 cfi->bp_scratch = false;
2104 else if (op->src.reg == CFI_SP &&
2105 op->dest.reg == CFI_BP && cfi->drap) {
2107 /* drap: mov %rsp, %rbp */
2108 regs[CFI_BP].base = CFI_BP;
2109 regs[CFI_BP].offset = -cfi->stack_size;
2110 cfi->bp_scratch = false;
2113 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2118 * This is needed for the rare case where GCC
2125 cfi->vals[op->dest.reg].base = CFI_CFA;
2126 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2129 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2130 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2135 * Restore the original stack pointer (Clang).
2137 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2140 else if (op->dest.reg == cfa->base) {
2142 /* mov %reg, %rsp */
2143 if (cfa->base == CFI_SP &&
2144 cfi->vals[op->src.reg].base == CFI_CFA) {
2147 * This is needed for the rare case
2148 * where GCC does something dumb like:
2150 * lea 0x8(%rsp), %rcx
2154 cfa->offset = -cfi->vals[op->src.reg].offset;
2155 cfi->stack_size = cfa->offset;
2157 } else if (cfa->base == CFI_SP &&
2158 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2159 cfi->vals[op->src.reg].offset == cfa->offset) {
2164 * 1: mov %rsp, (%[tos])
2165 * 2: mov %[tos], %rsp
2171 * 1 - places a pointer to the previous
2172 * stack at the Top-of-Stack of the
2175 * 2 - switches to the new stack.
2177 * 3 - pops the Top-of-Stack to restore
2178 * the original stack.
2180 * Note: we set base to SP_INDIRECT
2181 * here and preserve offset. Therefore
2182 * when the unwinder reaches ToS it
2183 * will dereference SP and then add the
2184 * offset to find the next frame, IOW:
2187 cfa->base = CFI_SP_INDIRECT;
2190 cfa->base = CFI_UNDEFINED;
2195 else if (op->dest.reg == CFI_SP &&
2196 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2197 cfi->vals[op->src.reg].offset == cfa->offset) {
2200 * The same stack swizzle case 2) as above. But
2201 * because we can't change cfa->base, case 3)
2202 * will become a regular POP. Pretend we're a
2203 * PUSH so things don't go unbalanced.
2205 cfi->stack_size += 8;
2212 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2215 cfi->stack_size -= op->src.offset;
2216 if (cfa->base == CFI_SP)
2217 cfa->offset -= op->src.offset;
2221 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2223 /* lea disp(%rbp), %rsp */
2224 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2228 if (!cfi->drap && op->src.reg == CFI_SP &&
2229 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2230 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2232 /* lea disp(%rsp), %rbp */
2234 cfa->offset -= op->src.offset;
2235 cfi->bp_scratch = false;
2239 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2241 /* drap: lea disp(%rsp), %drap */
2242 cfi->drap_reg = op->dest.reg;
2245 * lea disp(%rsp), %reg
2247 * This is needed for the rare case where GCC
2248 * does something dumb like:
2250 * lea 0x8(%rsp), %rcx
2254 cfi->vals[op->dest.reg].base = CFI_CFA;
2255 cfi->vals[op->dest.reg].offset = \
2256 -cfi->stack_size + op->src.offset;
2261 if (cfi->drap && op->dest.reg == CFI_SP &&
2262 op->src.reg == cfi->drap_reg) {
2264 /* drap: lea disp(%drap), %rsp */
2266 cfa->offset = cfi->stack_size = -op->src.offset;
2267 cfi->drap_reg = CFI_UNDEFINED;
2272 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2273 WARN_FUNC("unsupported stack register modification",
2274 insn->sec, insn->offset);
2281 if (op->dest.reg != CFI_SP ||
2282 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2283 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2284 WARN_FUNC("unsupported stack pointer realignment",
2285 insn->sec, insn->offset);
2289 if (cfi->drap_reg != CFI_UNDEFINED) {
2290 /* drap: and imm, %rsp */
2291 cfa->base = cfi->drap_reg;
2292 cfa->offset = cfi->stack_size = 0;
2297 * Older versions of GCC (4.8ish) realign the stack
2298 * without DRAP, with a frame pointer.
2305 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2307 /* pop %rsp; # restore from a stack swizzle */
2312 if (!cfi->drap && op->dest.reg == cfa->base) {
2318 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2319 op->dest.reg == cfi->drap_reg &&
2320 cfi->drap_offset == -cfi->stack_size) {
2322 /* drap: pop %drap */
2323 cfa->base = cfi->drap_reg;
2325 cfi->drap_offset = -1;
2327 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2330 restore_reg(cfi, op->dest.reg);
2333 cfi->stack_size -= 8;
2334 if (cfa->base == CFI_SP)
2339 case OP_SRC_REG_INDIRECT:
2340 if (!cfi->drap && op->dest.reg == cfa->base &&
2341 op->dest.reg == CFI_BP) {
2343 /* mov disp(%rsp), %rbp */
2345 cfa->offset = cfi->stack_size;
2348 if (cfi->drap && op->src.reg == CFI_BP &&
2349 op->src.offset == cfi->drap_offset) {
2351 /* drap: mov disp(%rbp), %drap */
2352 cfa->base = cfi->drap_reg;
2354 cfi->drap_offset = -1;
2357 if (cfi->drap && op->src.reg == CFI_BP &&
2358 op->src.offset == regs[op->dest.reg].offset) {
2360 /* drap: mov disp(%rbp), %reg */
2361 restore_reg(cfi, op->dest.reg);
2363 } else if (op->src.reg == cfa->base &&
2364 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2366 /* mov disp(%rbp), %reg */
2367 /* mov disp(%rsp), %reg */
2368 restore_reg(cfi, op->dest.reg);
2370 } else if (op->src.reg == CFI_SP &&
2371 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2373 /* mov disp(%rsp), %reg */
2374 restore_reg(cfi, op->dest.reg);
2380 WARN_FUNC("unknown stack-related instruction",
2381 insn->sec, insn->offset);
2389 cfi->stack_size += 8;
2390 if (cfa->base == CFI_SP)
2393 if (op->src.type != OP_SRC_REG)
2397 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2399 /* drap: push %drap */
2400 cfa->base = CFI_BP_INDIRECT;
2401 cfa->offset = -cfi->stack_size;
2403 /* save drap so we know when to restore it */
2404 cfi->drap_offset = -cfi->stack_size;
2406 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2408 /* drap: push %rbp */
2409 cfi->stack_size = 0;
2413 /* drap: push %reg */
2414 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2420 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2423 /* detect when asm code uses rbp as a scratch register */
2424 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2425 cfa->base != CFI_BP)
2426 cfi->bp_scratch = true;
2429 case OP_DEST_REG_INDIRECT:
2432 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2434 /* drap: mov %drap, disp(%rbp) */
2435 cfa->base = CFI_BP_INDIRECT;
2436 cfa->offset = op->dest.offset;
2438 /* save drap offset so we know when to restore it */
2439 cfi->drap_offset = op->dest.offset;
2442 /* drap: mov reg, disp(%rbp) */
2443 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2446 } else if (op->dest.reg == cfa->base) {
2448 /* mov reg, disp(%rbp) */
2449 /* mov reg, disp(%rsp) */
2450 save_reg(cfi, op->src.reg, CFI_CFA,
2451 op->dest.offset - cfi->cfa.offset);
2453 } else if (op->dest.reg == CFI_SP) {
2455 /* mov reg, disp(%rsp) */
2456 save_reg(cfi, op->src.reg, CFI_CFA,
2457 op->dest.offset - cfi->stack_size);
2459 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2461 /* mov %rsp, (%reg); # setup a stack swizzle. */
2462 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2463 cfi->vals[op->dest.reg].offset = cfa->offset;
2469 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2470 WARN_FUNC("unknown stack-related memory operation",
2471 insn->sec, insn->offset);
2476 cfi->stack_size -= 8;
2477 if (cfa->base == CFI_SP)
2483 WARN_FUNC("unknown stack-related instruction",
2484 insn->sec, insn->offset);
2492 * The stack layouts of alternatives instructions can sometimes diverge when
2493 * they have stack modifications. That's fine as long as the potential stack
2494 * layouts don't conflict at any given potential instruction boundary.
2496 * Flatten the CFIs of the different alternative code streams (both original
2497 * and replacement) into a single shared CFI array which can be used to detect
2498 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2500 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2502 struct cfi_state **alt_cfi;
2505 if (!insn->alt_group)
2508 alt_cfi = insn->alt_group->cfi;
2509 group_off = insn->offset - insn->alt_group->first_insn->offset;
2511 if (!alt_cfi[group_off]) {
2512 alt_cfi[group_off] = &insn->cfi;
2514 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2515 WARN_FUNC("stack layout conflict in alternatives",
2516 insn->sec, insn->offset);
2524 static int handle_insn_ops(struct instruction *insn,
2525 struct instruction *next_insn,
2526 struct insn_state *state)
2528 struct stack_op *op;
2530 list_for_each_entry(op, &insn->stack_ops, list) {
2532 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2535 if (!insn->alt_group)
2538 if (op->dest.type == OP_DEST_PUSHF) {
2539 if (!state->uaccess_stack) {
2540 state->uaccess_stack = 1;
2541 } else if (state->uaccess_stack >> 31) {
2542 WARN_FUNC("PUSHF stack exhausted",
2543 insn->sec, insn->offset);
2546 state->uaccess_stack <<= 1;
2547 state->uaccess_stack |= state->uaccess;
2550 if (op->src.type == OP_SRC_POPF) {
2551 if (state->uaccess_stack) {
2552 state->uaccess = state->uaccess_stack & 1;
2553 state->uaccess_stack >>= 1;
2554 if (state->uaccess_stack == 1)
2555 state->uaccess_stack = 0;
2563 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2565 struct cfi_state *cfi1 = &insn->cfi;
2568 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2570 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2571 insn->sec, insn->offset,
2572 cfi1->cfa.base, cfi1->cfa.offset,
2573 cfi2->cfa.base, cfi2->cfa.offset);
2575 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2576 for (i = 0; i < CFI_NUM_REGS; i++) {
2577 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2578 sizeof(struct cfi_reg)))
2581 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2582 insn->sec, insn->offset,
2583 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2584 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2588 } else if (cfi1->type != cfi2->type) {
2590 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2591 insn->sec, insn->offset, cfi1->type, cfi2->type);
2593 } else if (cfi1->drap != cfi2->drap ||
2594 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2595 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2597 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2598 insn->sec, insn->offset,
2599 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2600 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2608 static inline bool func_uaccess_safe(struct symbol *func)
2611 return func->uaccess_safe;
2616 static inline const char *call_dest_name(struct instruction *insn)
2618 if (insn->call_dest)
2619 return insn->call_dest->name;
2624 static inline bool noinstr_call_dest(struct symbol *func)
2627 * We can't deal with indirect function calls at present;
2628 * assume they're instrumented.
2634 * If the symbol is from a noinstr section; we good.
2636 if (func->sec->noinstr)
2640 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2641 * something 'BAD' happened. At the risk of taking the machine down,
2642 * let them proceed to get the message out.
2644 if (!strncmp(func->name, "__ubsan_handle_", 15))
2650 static int validate_call(struct instruction *insn, struct insn_state *state)
2652 if (state->noinstr && state->instr <= 0 &&
2653 !noinstr_call_dest(insn->call_dest)) {
2654 WARN_FUNC("call to %s() leaves .noinstr.text section",
2655 insn->sec, insn->offset, call_dest_name(insn));
2659 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2660 WARN_FUNC("call to %s() with UACCESS enabled",
2661 insn->sec, insn->offset, call_dest_name(insn));
2666 WARN_FUNC("call to %s() with DF set",
2667 insn->sec, insn->offset, call_dest_name(insn));
2674 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2676 if (has_modified_stack_frame(insn, state)) {
2677 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2678 insn->sec, insn->offset);
2682 return validate_call(insn, state);
2685 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2687 if (state->noinstr && state->instr > 0) {
2688 WARN_FUNC("return with instrumentation enabled",
2689 insn->sec, insn->offset);
2693 if (state->uaccess && !func_uaccess_safe(func)) {
2694 WARN_FUNC("return with UACCESS enabled",
2695 insn->sec, insn->offset);
2699 if (!state->uaccess && func_uaccess_safe(func)) {
2700 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2701 insn->sec, insn->offset);
2706 WARN_FUNC("return with DF set",
2707 insn->sec, insn->offset);
2711 if (func && has_modified_stack_frame(insn, state)) {
2712 WARN_FUNC("return with modified stack frame",
2713 insn->sec, insn->offset);
2717 if (state->cfi.bp_scratch) {
2718 WARN_FUNC("BP used as a scratch register",
2719 insn->sec, insn->offset);
2726 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2727 struct instruction *insn)
2729 struct alt_group *alt_group = insn->alt_group;
2732 * Simulate the fact that alternatives are patched in-place. When the
2733 * end of a replacement alt_group is reached, redirect objtool flow to
2734 * the end of the original alt_group.
2736 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2737 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2739 return next_insn_same_sec(file, insn);
2743 * Follow the branch starting at the given instruction, and recursively follow
2744 * any other branches (jumps). Meanwhile, track the frame pointer state at
2745 * each instruction and validate all the rules described in
2746 * tools/objtool/Documentation/stack-validation.txt.
2748 static int validate_branch(struct objtool_file *file, struct symbol *func,
2749 struct instruction *insn, struct insn_state state)
2751 struct alternative *alt;
2752 struct instruction *next_insn;
2753 struct section *sec;
2760 next_insn = next_insn_to_validate(file, insn);
2762 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2763 WARN("%s() falls through to next function %s()",
2764 func->name, insn->func->name);
2768 if (func && insn->ignore) {
2769 WARN_FUNC("BUG: why am I validating an ignored function?",
2774 visited = 1 << state.uaccess;
2775 if (insn->visited) {
2776 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2779 if (insn->visited & visited)
2784 state.instr += insn->instr;
2787 state.cfi = insn->cfi;
2789 insn->cfi = state.cfi;
2791 insn->visited |= visited;
2793 if (propagate_alt_cfi(file, insn))
2796 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2797 bool skip_orig = false;
2799 list_for_each_entry(alt, &insn->alts, list) {
2803 ret = validate_branch(file, func, alt->insn, state);
2806 BT_FUNC("(alt)", insn);
2815 if (handle_insn_ops(insn, next_insn, &state))
2818 switch (insn->type) {
2821 if (next_insn && next_insn->type == INSN_TRAP) {
2822 next_insn->ignore = true;
2823 } else if (sls && !insn->retpoline_safe) {
2824 WARN_FUNC("missing int3 after ret",
2825 insn->sec, insn->offset);
2827 return validate_return(func, insn, &state);
2830 case INSN_CALL_DYNAMIC:
2831 ret = validate_call(insn, &state);
2835 if (!no_fp && func && !is_fentry_call(insn) &&
2836 !has_valid_stack_frame(&state)) {
2837 WARN_FUNC("call without frame pointer save/setup",
2842 if (dead_end_function(file, insn->call_dest))
2847 case INSN_JUMP_CONDITIONAL:
2848 case INSN_JUMP_UNCONDITIONAL:
2849 if (is_sibling_call(insn)) {
2850 ret = validate_sibling_call(insn, &state);
2854 } else if (insn->jump_dest) {
2855 ret = validate_branch(file, func,
2856 insn->jump_dest, state);
2859 BT_FUNC("(branch)", insn);
2864 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2869 case INSN_JUMP_DYNAMIC:
2870 if (next_insn && next_insn->type == INSN_TRAP) {
2871 next_insn->ignore = true;
2872 } else if (sls && !insn->retpoline_safe) {
2873 WARN_FUNC("missing int3 after indirect jump",
2874 insn->sec, insn->offset);
2878 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2879 if (is_sibling_call(insn)) {
2880 ret = validate_sibling_call(insn, &state);
2885 if (insn->type == INSN_JUMP_DYNAMIC)
2890 case INSN_CONTEXT_SWITCH:
2891 if (func && (!next_insn || !next_insn->hint)) {
2892 WARN_FUNC("unsupported instruction in callable function",
2899 if (state.uaccess) {
2900 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2904 state.uaccess = true;
2908 if (!state.uaccess && func) {
2909 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2913 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2914 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2918 state.uaccess = false;
2923 WARN_FUNC("recursive STD", sec, insn->offset);
2931 if (!state.df && func) {
2932 WARN_FUNC("redundant CLD", sec, insn->offset);
2947 if (state.cfi.cfa.base == CFI_UNDEFINED)
2949 WARN("%s: unexpected end of section", sec->name);
2959 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2961 struct instruction *insn;
2962 struct insn_state state;
2963 int ret, warnings = 0;
2968 init_insn_state(&state, sec);
2971 insn = find_insn(file, sec, 0);
2975 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2978 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2979 if (insn->hint && !insn->visited) {
2980 ret = validate_branch(file, insn->func, insn, state);
2981 if (ret && backtrace)
2982 BT_FUNC("<=== (hint)", insn);
2986 insn = list_next_entry(insn, list);
2992 static int validate_retpoline(struct objtool_file *file)
2994 struct instruction *insn;
2997 for_each_insn(file, insn) {
2998 if (insn->type != INSN_JUMP_DYNAMIC &&
2999 insn->type != INSN_CALL_DYNAMIC)
3002 if (insn->retpoline_safe)
3006 * .init.text code is ran before userspace and thus doesn't
3007 * strictly need retpolines, except for modules which are
3008 * loaded late, they very much do need retpoline in their
3011 if (!strcmp(insn->sec->name, ".init.text") && !module)
3014 WARN_FUNC("indirect %s found in RETPOLINE build",
3015 insn->sec, insn->offset,
3016 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3024 static bool is_kasan_insn(struct instruction *insn)
3026 return (insn->type == INSN_CALL &&
3027 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3030 static bool is_ubsan_insn(struct instruction *insn)
3032 return (insn->type == INSN_CALL &&
3033 !strcmp(insn->call_dest->name,
3034 "__ubsan_handle_builtin_unreachable"));
3037 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3040 struct instruction *prev_insn;
3042 if (insn->ignore || insn->type == INSN_NOP)
3046 * Ignore any unused exceptions. This can happen when a whitelisted
3047 * function has an exception table entry.
3049 * Also ignore alternative replacement instructions. This can happen
3050 * when a whitelisted function uses one of the ALTERNATIVE macros.
3052 if (!strcmp(insn->sec->name, ".fixup") ||
3053 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3054 !strcmp(insn->sec->name, ".altinstr_aux"))
3061 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3062 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3063 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3064 * (or occasionally a JMP to UD2).
3066 * It may also insert a UD2 after calling a __noreturn function.
3068 prev_insn = list_prev_entry(insn, list);
3069 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3070 (insn->type == INSN_BUG ||
3071 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3072 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3076 * Check if this (or a subsequent) instruction is related to
3077 * CONFIG_UBSAN or CONFIG_KASAN.
3079 * End the search at 5 instructions to avoid going into the weeds.
3081 for (i = 0; i < 5; i++) {
3083 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3086 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3087 if (insn->jump_dest &&
3088 insn->jump_dest->func == insn->func) {
3089 insn = insn->jump_dest;
3096 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3099 insn = list_next_entry(insn, list);
3105 static int validate_symbol(struct objtool_file *file, struct section *sec,
3106 struct symbol *sym, struct insn_state *state)
3108 struct instruction *insn;
3112 WARN("%s() is missing an ELF size annotation", sym->name);
3116 if (sym->pfunc != sym || sym->alias != sym)
3119 insn = find_insn(file, sec, sym->offset);
3120 if (!insn || insn->ignore || insn->visited)
3123 state->uaccess = sym->uaccess_safe;
3125 ret = validate_branch(file, insn->func, insn, *state);
3126 if (ret && backtrace)
3127 BT_FUNC("<=== (sym)", insn);
3131 static int validate_section(struct objtool_file *file, struct section *sec)
3133 struct insn_state state;
3134 struct symbol *func;
3137 list_for_each_entry(func, &sec->symbol_list, list) {
3138 if (func->type != STT_FUNC)
3141 init_insn_state(&state, sec);
3142 set_func_state(&state.cfi);
3144 warnings += validate_symbol(file, sec, func, &state);
3150 static int validate_vmlinux_functions(struct objtool_file *file)
3152 struct section *sec;
3155 sec = find_section_by_name(file->elf, ".noinstr.text");
3157 warnings += validate_section(file, sec);
3158 warnings += validate_unwind_hints(file, sec);
3161 sec = find_section_by_name(file->elf, ".entry.text");
3163 warnings += validate_section(file, sec);
3164 warnings += validate_unwind_hints(file, sec);
3170 static int validate_functions(struct objtool_file *file)
3172 struct section *sec;
3175 for_each_sec(file, sec) {
3176 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3179 warnings += validate_section(file, sec);
3185 static int validate_reachable_instructions(struct objtool_file *file)
3187 struct instruction *insn;
3189 if (file->ignore_unreachables)
3192 for_each_insn(file, insn) {
3193 if (insn->visited || ignore_unreachable_insn(file, insn))
3196 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3203 int check(struct objtool_file *file)
3205 int ret, warnings = 0;
3207 arch_initial_func_cfi_state(&initial_func_cfi);
3209 ret = decode_sections(file);
3214 if (list_empty(&file->insn_list))
3217 if (vmlinux && !validate_dup) {
3218 ret = validate_vmlinux_functions(file);
3227 ret = validate_retpoline(file);
3233 ret = validate_functions(file);
3238 ret = validate_unwind_hints(file, NULL);
3244 ret = validate_reachable_instructions(file);
3250 ret = create_static_call_sections(file);
3256 ret = create_mcount_loc_sections(file);
3264 * For now, don't fail the kernel build on fatal warnings. These
3265 * errors are still fairly common due to the growing matrix of
3266 * supported toolchains and their recent pace of change.