1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
10 #include <objtool/builtin.h>
11 #include <objtool/cfi.h>
12 #include <objtool/arch.h>
13 #include <objtool/check.h>
14 #include <objtool/special.h>
15 #include <objtool/warn.h>
16 #include <objtool/endianness.h>
18 #include <linux/objtool.h>
19 #include <linux/hashtable.h>
20 #include <linux/kernel.h>
21 #include <linux/static_call_types.h>
24 struct list_head list;
25 struct instruction *insn;
29 struct cfi_init_state initial_func_cfi;
31 struct instruction *find_insn(struct objtool_file *file,
32 struct section *sec, unsigned long offset)
34 struct instruction *insn;
36 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
37 if (insn->sec == sec && insn->offset == offset)
44 static struct instruction *next_insn_same_sec(struct objtool_file *file,
45 struct instruction *insn)
47 struct instruction *next = list_next_entry(insn, list);
49 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
55 static struct instruction *next_insn_same_func(struct objtool_file *file,
56 struct instruction *insn)
58 struct instruction *next = list_next_entry(insn, list);
59 struct symbol *func = insn->func;
64 if (&next->list != &file->insn_list && next->func == func)
67 /* Check if we're already in the subfunction: */
68 if (func == func->cfunc)
71 /* Move to the subfunction: */
72 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
75 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
76 struct instruction *insn)
78 struct instruction *prev = list_prev_entry(insn, list);
80 if (&prev->list != &file->insn_list && prev->func == insn->func)
86 #define func_for_each_insn(file, func, insn) \
87 for (insn = find_insn(file, func->sec, func->offset); \
89 insn = next_insn_same_func(file, insn))
91 #define sym_for_each_insn(file, sym, insn) \
92 for (insn = find_insn(file, sym->sec, sym->offset); \
93 insn && &insn->list != &file->insn_list && \
94 insn->sec == sym->sec && \
95 insn->offset < sym->offset + sym->len; \
96 insn = list_next_entry(insn, list))
98 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
99 for (insn = list_prev_entry(insn, list); \
100 &insn->list != &file->insn_list && \
101 insn->sec == sym->sec && insn->offset >= sym->offset; \
102 insn = list_prev_entry(insn, list))
104 #define sec_for_each_insn_from(file, insn) \
105 for (; insn; insn = next_insn_same_sec(file, insn))
107 #define sec_for_each_insn_continue(file, insn) \
108 for (insn = next_insn_same_sec(file, insn); insn; \
109 insn = next_insn_same_sec(file, insn))
111 static bool is_jump_table_jump(struct instruction *insn)
113 struct alt_group *alt_group = insn->alt_group;
115 if (insn->jump_table)
118 /* Retpoline alternative for a jump table? */
119 return alt_group && alt_group->orig_group &&
120 alt_group->orig_group->first_insn->jump_table;
123 static bool is_sibling_call(struct instruction *insn)
126 * Assume only ELF functions can make sibling calls. This ensures
127 * sibling call detection consistency between vmlinux.o and individual
133 /* An indirect jump is either a sibling call or a jump to a table. */
134 if (insn->type == INSN_JUMP_DYNAMIC)
135 return !is_jump_table_jump(insn);
137 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
138 return (is_static_jump(insn) && insn->call_dest);
142 * This checks to see if the given function is a "noreturn" function.
144 * For global functions which are outside the scope of this object file, we
145 * have to keep a manual list of them.
147 * For local functions, we have to detect them manually by simply looking for
148 * the lack of a return instruction.
150 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
154 struct instruction *insn;
158 * Unfortunately these have to be hard coded because the noreturn
159 * attribute isn't provided in ELF data.
161 static const char * const global_noreturns[] = {
166 "__module_put_and_exit",
172 "machine_real_restart",
173 "rewind_stack_do_exit",
174 "kunit_try_catch_throw",
176 "cpu_bringup_and_idle",
182 if (func->bind == STB_WEAK)
185 if (func->bind == STB_GLOBAL)
186 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
187 if (!strcmp(func->name, global_noreturns[i]))
193 insn = find_insn(file, func->sec, func->offset);
197 func_for_each_insn(file, func, insn) {
200 if (insn->type == INSN_RETURN)
208 * A function can have a sibling call instead of a return. In that
209 * case, the function's dead-end status depends on whether the target
210 * of the sibling call returns.
212 func_for_each_insn(file, func, insn) {
213 if (is_sibling_call(insn)) {
214 struct instruction *dest = insn->jump_dest;
217 /* sibling call to another file */
220 /* local sibling call */
221 if (recursion == 5) {
223 * Infinite recursion: two functions have
224 * sibling calls to each other. This is a very
225 * rare case. It means they aren't dead ends.
230 return __dead_end_function(file, dest->func, recursion+1);
237 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
239 return __dead_end_function(file, func, 0);
242 static void init_cfi_state(struct cfi_state *cfi)
246 for (i = 0; i < CFI_NUM_REGS; i++) {
247 cfi->regs[i].base = CFI_UNDEFINED;
248 cfi->vals[i].base = CFI_UNDEFINED;
250 cfi->cfa.base = CFI_UNDEFINED;
251 cfi->drap_reg = CFI_UNDEFINED;
252 cfi->drap_offset = -1;
255 static void init_insn_state(struct insn_state *state, struct section *sec)
257 memset(state, 0, sizeof(*state));
258 init_cfi_state(&state->cfi);
261 * We need the full vmlinux for noinstr validation, otherwise we can
262 * not correctly determine insn->call_dest->sec (external symbols do
263 * not have a section).
265 if (vmlinux && noinstr && sec)
266 state->noinstr = sec->noinstr;
270 * Call the arch-specific instruction decoder for all the instructions and add
271 * them to the global instruction list.
273 static int decode_instructions(struct objtool_file *file)
277 unsigned long offset;
278 struct instruction *insn;
279 unsigned long nr_insns = 0;
282 for_each_sec(file, sec) {
284 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
287 if (strcmp(sec->name, ".altinstr_replacement") &&
288 strcmp(sec->name, ".altinstr_aux") &&
289 strncmp(sec->name, ".discard.", 9))
292 if (!strcmp(sec->name, ".noinstr.text") ||
293 !strcmp(sec->name, ".entry.text"))
296 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
297 insn = malloc(sizeof(*insn));
299 WARN("malloc failed");
302 memset(insn, 0, sizeof(*insn));
303 INIT_LIST_HEAD(&insn->alts);
304 INIT_LIST_HEAD(&insn->stack_ops);
305 init_cfi_state(&insn->cfi);
308 insn->offset = offset;
310 ret = arch_decode_instruction(file->elf, sec, offset,
311 sec->sh.sh_size - offset,
312 &insn->len, &insn->type,
318 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
319 list_add_tail(&insn->list, &file->insn_list);
323 list_for_each_entry(func, &sec->symbol_list, list) {
324 if (func->type != STT_FUNC || func->alias != func)
327 if (!find_insn(file, sec, func->offset)) {
328 WARN("%s(): can't find starting instruction",
333 sym_for_each_insn(file, func, insn)
339 printf("nr_insns: %lu\n", nr_insns);
348 static struct instruction *find_last_insn(struct objtool_file *file,
351 struct instruction *insn = NULL;
353 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
355 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
356 insn = find_insn(file, sec, offset);
362 * Mark "ud2" instructions and manually annotated dead ends.
364 static int add_dead_ends(struct objtool_file *file)
368 struct instruction *insn;
371 * By default, "ud2" is a dead end unless otherwise annotated, because
372 * GCC 7 inserts it for certain divide-by-zero cases.
374 for_each_insn(file, insn)
375 if (insn->type == INSN_BUG)
376 insn->dead_end = true;
379 * Check for manually annotated dead ends.
381 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
385 list_for_each_entry(reloc, &sec->reloc_list, list) {
386 if (reloc->sym->type != STT_SECTION) {
387 WARN("unexpected relocation symbol type in %s", sec->name);
390 insn = find_insn(file, reloc->sym->sec, reloc->addend);
392 insn = list_prev_entry(insn, list);
393 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
394 insn = find_last_insn(file, reloc->sym->sec);
396 WARN("can't find unreachable insn at %s+0x%x",
397 reloc->sym->sec->name, reloc->addend);
401 WARN("can't find unreachable insn at %s+0x%x",
402 reloc->sym->sec->name, reloc->addend);
406 insn->dead_end = true;
411 * These manually annotated reachable checks are needed for GCC 4.4,
412 * where the Linux unreachable() macro isn't supported. In that case
413 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
416 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
420 list_for_each_entry(reloc, &sec->reloc_list, list) {
421 if (reloc->sym->type != STT_SECTION) {
422 WARN("unexpected relocation symbol type in %s", sec->name);
425 insn = find_insn(file, reloc->sym->sec, reloc->addend);
427 insn = list_prev_entry(insn, list);
428 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
429 insn = find_last_insn(file, reloc->sym->sec);
431 WARN("can't find reachable insn at %s+0x%x",
432 reloc->sym->sec->name, reloc->addend);
436 WARN("can't find reachable insn at %s+0x%x",
437 reloc->sym->sec->name, reloc->addend);
441 insn->dead_end = false;
447 static int create_static_call_sections(struct objtool_file *file)
450 struct static_call_site *site;
451 struct instruction *insn;
452 struct symbol *key_sym;
453 char *key_name, *tmp;
456 sec = find_section_by_name(file->elf, ".static_call_sites");
458 INIT_LIST_HEAD(&file->static_call_list);
459 WARN("file already has .static_call_sites section, skipping");
463 if (list_empty(&file->static_call_list))
467 list_for_each_entry(insn, &file->static_call_list, call_node)
470 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
471 sizeof(struct static_call_site), idx);
476 list_for_each_entry(insn, &file->static_call_list, call_node) {
478 site = (struct static_call_site *)sec->data->d_buf + idx;
479 memset(site, 0, sizeof(struct static_call_site));
481 /* populate reloc for 'addr' */
482 if (elf_add_reloc_to_insn(file->elf, sec,
483 idx * sizeof(struct static_call_site),
485 insn->sec, insn->offset))
488 /* find key symbol */
489 key_name = strdup(insn->call_dest->name);
494 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
495 STATIC_CALL_TRAMP_PREFIX_LEN)) {
496 WARN("static_call: trampoline name malformed: %s", key_name);
499 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
500 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
502 key_sym = find_symbol_by_name(file->elf, tmp);
505 WARN("static_call: can't find static_call_key symbol: %s", tmp);
510 * For modules(), the key might not be exported, which
511 * means the module can make static calls but isn't
512 * allowed to change them.
514 * In that case we temporarily set the key to be the
515 * trampoline address. This is fixed up in
516 * static_call_add_module().
518 key_sym = insn->call_dest;
522 /* populate reloc for 'key' */
523 if (elf_add_reloc(file->elf, sec,
524 idx * sizeof(struct static_call_site) + 4,
525 R_X86_64_PC32, key_sym,
526 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
535 static int create_mcount_loc_sections(struct objtool_file *file)
539 struct instruction *insn;
542 sec = find_section_by_name(file->elf, "__mcount_loc");
544 INIT_LIST_HEAD(&file->mcount_loc_list);
545 WARN("file already has __mcount_loc section, skipping");
549 if (list_empty(&file->mcount_loc_list))
553 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
556 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
561 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
563 loc = (unsigned long *)sec->data->d_buf + idx;
564 memset(loc, 0, sizeof(unsigned long));
566 if (elf_add_reloc_to_insn(file->elf, sec,
567 idx * sizeof(unsigned long),
569 insn->sec, insn->offset))
579 * Warnings shouldn't be reported for ignored functions.
581 static void add_ignores(struct objtool_file *file)
583 struct instruction *insn;
588 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
592 list_for_each_entry(reloc, &sec->reloc_list, list) {
593 switch (reloc->sym->type) {
599 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
605 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
609 func_for_each_insn(file, func, insn)
615 * This is a whitelist of functions that is allowed to be called with AC set.
616 * The list is meant to be minimal and only contains compiler instrumentation
617 * ABI and a few functions used to implement *_{to,from}_user() functions.
619 * These functions must not directly change AC, but may PUSHF/POPF.
621 static const char *uaccess_safe_builtin[] = {
625 /* KASAN out-of-line */
626 "__asan_loadN_noabort",
627 "__asan_load1_noabort",
628 "__asan_load2_noabort",
629 "__asan_load4_noabort",
630 "__asan_load8_noabort",
631 "__asan_load16_noabort",
632 "__asan_storeN_noabort",
633 "__asan_store1_noabort",
634 "__asan_store2_noabort",
635 "__asan_store4_noabort",
636 "__asan_store8_noabort",
637 "__asan_store16_noabort",
638 "__kasan_check_read",
639 "__kasan_check_write",
641 "__asan_report_load_n_noabort",
642 "__asan_report_load1_noabort",
643 "__asan_report_load2_noabort",
644 "__asan_report_load4_noabort",
645 "__asan_report_load8_noabort",
646 "__asan_report_load16_noabort",
647 "__asan_report_store_n_noabort",
648 "__asan_report_store1_noabort",
649 "__asan_report_store2_noabort",
650 "__asan_report_store4_noabort",
651 "__asan_report_store8_noabort",
652 "__asan_report_store16_noabort",
654 "__kcsan_check_access",
655 "kcsan_found_watchpoint",
656 "kcsan_setup_watchpoint",
657 "kcsan_check_scoped_accesses",
658 "kcsan_disable_current",
659 "kcsan_enable_current_nowarn",
664 "__tsan_write_range",
675 "__tsan_read_write1",
676 "__tsan_read_write2",
677 "__tsan_read_write4",
678 "__tsan_read_write8",
679 "__tsan_read_write16",
680 "__tsan_atomic8_load",
681 "__tsan_atomic16_load",
682 "__tsan_atomic32_load",
683 "__tsan_atomic64_load",
684 "__tsan_atomic8_store",
685 "__tsan_atomic16_store",
686 "__tsan_atomic32_store",
687 "__tsan_atomic64_store",
688 "__tsan_atomic8_exchange",
689 "__tsan_atomic16_exchange",
690 "__tsan_atomic32_exchange",
691 "__tsan_atomic64_exchange",
692 "__tsan_atomic8_fetch_add",
693 "__tsan_atomic16_fetch_add",
694 "__tsan_atomic32_fetch_add",
695 "__tsan_atomic64_fetch_add",
696 "__tsan_atomic8_fetch_sub",
697 "__tsan_atomic16_fetch_sub",
698 "__tsan_atomic32_fetch_sub",
699 "__tsan_atomic64_fetch_sub",
700 "__tsan_atomic8_fetch_and",
701 "__tsan_atomic16_fetch_and",
702 "__tsan_atomic32_fetch_and",
703 "__tsan_atomic64_fetch_and",
704 "__tsan_atomic8_fetch_or",
705 "__tsan_atomic16_fetch_or",
706 "__tsan_atomic32_fetch_or",
707 "__tsan_atomic64_fetch_or",
708 "__tsan_atomic8_fetch_xor",
709 "__tsan_atomic16_fetch_xor",
710 "__tsan_atomic32_fetch_xor",
711 "__tsan_atomic64_fetch_xor",
712 "__tsan_atomic8_fetch_nand",
713 "__tsan_atomic16_fetch_nand",
714 "__tsan_atomic32_fetch_nand",
715 "__tsan_atomic64_fetch_nand",
716 "__tsan_atomic8_compare_exchange_strong",
717 "__tsan_atomic16_compare_exchange_strong",
718 "__tsan_atomic32_compare_exchange_strong",
719 "__tsan_atomic64_compare_exchange_strong",
720 "__tsan_atomic8_compare_exchange_weak",
721 "__tsan_atomic16_compare_exchange_weak",
722 "__tsan_atomic32_compare_exchange_weak",
723 "__tsan_atomic64_compare_exchange_weak",
724 "__tsan_atomic8_compare_exchange_val",
725 "__tsan_atomic16_compare_exchange_val",
726 "__tsan_atomic32_compare_exchange_val",
727 "__tsan_atomic64_compare_exchange_val",
728 "__tsan_atomic_thread_fence",
729 "__tsan_atomic_signal_fence",
733 "__sanitizer_cov_trace_pc",
734 "__sanitizer_cov_trace_const_cmp1",
735 "__sanitizer_cov_trace_const_cmp2",
736 "__sanitizer_cov_trace_const_cmp4",
737 "__sanitizer_cov_trace_const_cmp8",
738 "__sanitizer_cov_trace_cmp1",
739 "__sanitizer_cov_trace_cmp2",
740 "__sanitizer_cov_trace_cmp4",
741 "__sanitizer_cov_trace_cmp8",
742 "__sanitizer_cov_trace_switch",
744 "ubsan_type_mismatch_common",
745 "__ubsan_handle_type_mismatch",
746 "__ubsan_handle_type_mismatch_v1",
747 "__ubsan_handle_shift_out_of_bounds",
749 "csum_partial_copy_generic",
751 "copy_mc_fragile_handle_tail",
752 "copy_mc_enhanced_fast_string",
753 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
757 static void add_uaccess_safe(struct objtool_file *file)
765 for (name = uaccess_safe_builtin; *name; name++) {
766 func = find_symbol_by_name(file->elf, *name);
770 func->uaccess_safe = true;
775 * FIXME: For now, just ignore any alternatives which add retpolines. This is
776 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
777 * But it at least allows objtool to understand the control flow *around* the
780 static int add_ignore_alternatives(struct objtool_file *file)
784 struct instruction *insn;
786 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
790 list_for_each_entry(reloc, &sec->reloc_list, list) {
791 if (reloc->sym->type != STT_SECTION) {
792 WARN("unexpected relocation symbol type in %s", sec->name);
796 insn = find_insn(file, reloc->sym->sec, reloc->addend);
798 WARN("bad .discard.ignore_alts entry");
802 insn->ignore_alts = true;
808 __weak bool arch_is_retpoline(struct symbol *sym)
813 #define NEGATIVE_RELOC ((void *)-1L)
815 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
817 if (insn->reloc == NEGATIVE_RELOC)
821 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
822 insn->offset, insn->len);
824 insn->reloc = NEGATIVE_RELOC;
832 static void remove_insn_ops(struct instruction *insn)
834 struct stack_op *op, *tmp;
836 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
842 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
843 struct symbol *dest, bool sibling)
845 struct reloc *reloc = insn_reloc(file, insn);
847 insn->call_dest = dest;
851 if (insn->call_dest->static_call_tramp) {
852 list_add_tail(&insn->call_node,
853 &file->static_call_list);
857 * Many compilers cannot disable KCOV with a function attribute
858 * so they need a little help, NOP out any KCOV calls from noinstr
861 if (insn->sec->noinstr &&
862 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
864 reloc->type = R_NONE;
865 elf_write_reloc(file->elf, reloc);
868 elf_write_insn(file->elf, insn->sec,
869 insn->offset, insn->len,
870 sibling ? arch_ret_insn(insn->len)
871 : arch_nop_insn(insn->len));
873 insn->type = sibling ? INSN_RETURN : INSN_NOP;
876 if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
878 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
881 reloc->type = R_NONE;
882 elf_write_reloc(file->elf, reloc);
885 elf_write_insn(file->elf, insn->sec,
886 insn->offset, insn->len,
887 arch_nop_insn(insn->len));
889 insn->type = INSN_NOP;
891 list_add_tail(&insn->mcount_loc_node,
892 &file->mcount_loc_list);
896 * Whatever stack impact regular CALLs have, should be undone
897 * by the RETURN of the called function.
899 * Annotated intra-function calls retain the stack_ops but
900 * are converted to JUMP, see read_intra_function_calls().
902 remove_insn_ops(insn);
906 * Find the destination instructions for all jumps.
908 static int add_jump_destinations(struct objtool_file *file)
910 struct instruction *insn;
912 struct section *dest_sec;
913 unsigned long dest_off;
915 for_each_insn(file, insn) {
916 if (!is_static_jump(insn))
919 reloc = insn_reloc(file, insn);
921 dest_sec = insn->sec;
922 dest_off = arch_jump_destination(insn);
923 } else if (reloc->sym->type == STT_SECTION) {
924 dest_sec = reloc->sym->sec;
925 dest_off = arch_dest_reloc_offset(reloc->addend);
926 } else if (arch_is_retpoline(reloc->sym)) {
928 * Retpoline jumps are really dynamic jumps in
929 * disguise, so convert them accordingly.
931 if (insn->type == INSN_JUMP_UNCONDITIONAL)
932 insn->type = INSN_JUMP_DYNAMIC;
934 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
936 list_add_tail(&insn->call_node,
937 &file->retpoline_call_list);
939 insn->retpoline_safe = true;
941 } else if (insn->func) {
942 /* internal or external sibling call (with reloc) */
943 add_call_dest(file, insn, reloc->sym, true);
945 } else if (reloc->sym->sec->idx) {
946 dest_sec = reloc->sym->sec;
947 dest_off = reloc->sym->sym.st_value +
948 arch_dest_reloc_offset(reloc->addend);
950 /* non-func asm code jumping to another file */
954 insn->jump_dest = find_insn(file, dest_sec, dest_off);
955 if (!insn->jump_dest) {
958 * This is a special case where an alt instruction
959 * jumps past the end of the section. These are
960 * handled later in handle_group_alt().
962 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
965 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
966 insn->sec, insn->offset, dest_sec->name,
972 * Cross-function jump.
974 if (insn->func && insn->jump_dest->func &&
975 insn->func != insn->jump_dest->func) {
978 * For GCC 8+, create parent/child links for any cold
979 * subfunctions. This is _mostly_ redundant with a
980 * similar initialization in read_symbols().
982 * If a function has aliases, we want the *first* such
983 * function in the symbol table to be the subfunction's
984 * parent. In that case we overwrite the
985 * initialization done in read_symbols().
987 * However this code can't completely replace the
988 * read_symbols() code because this doesn't detect the
989 * case where the parent function's only reference to a
990 * subfunction is through a jump table.
992 if (!strstr(insn->func->name, ".cold") &&
993 strstr(insn->jump_dest->func->name, ".cold")) {
994 insn->func->cfunc = insn->jump_dest->func;
995 insn->jump_dest->func->pfunc = insn->func;
997 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
998 insn->jump_dest->offset == insn->jump_dest->func->offset) {
999 /* internal sibling call (without reloc) */
1000 add_call_dest(file, insn, insn->jump_dest->func, true);
1008 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1010 struct symbol *call_dest;
1012 call_dest = find_func_by_offset(sec, offset);
1014 call_dest = find_symbol_by_offset(sec, offset);
1020 * Find the destination instructions for all calls.
1022 static int add_call_destinations(struct objtool_file *file)
1024 struct instruction *insn;
1025 unsigned long dest_off;
1026 struct symbol *dest;
1027 struct reloc *reloc;
1029 for_each_insn(file, insn) {
1030 if (insn->type != INSN_CALL)
1033 reloc = insn_reloc(file, insn);
1035 dest_off = arch_jump_destination(insn);
1036 dest = find_call_destination(insn->sec, dest_off);
1038 add_call_dest(file, insn, dest, false);
1043 if (!insn->call_dest) {
1044 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1048 if (insn->func && insn->call_dest->type != STT_FUNC) {
1049 WARN_FUNC("unsupported call to non-function",
1050 insn->sec, insn->offset);
1054 } else if (reloc->sym->type == STT_SECTION) {
1055 dest_off = arch_dest_reloc_offset(reloc->addend);
1056 dest = find_call_destination(reloc->sym->sec, dest_off);
1058 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1059 insn->sec, insn->offset,
1060 reloc->sym->sec->name,
1065 add_call_dest(file, insn, dest, false);
1067 } else if (arch_is_retpoline(reloc->sym)) {
1069 * Retpoline calls are really dynamic calls in
1070 * disguise, so convert them accordingly.
1072 insn->type = INSN_CALL_DYNAMIC;
1073 insn->retpoline_safe = true;
1075 list_add_tail(&insn->call_node,
1076 &file->retpoline_call_list);
1078 remove_insn_ops(insn);
1082 add_call_dest(file, insn, reloc->sym, false);
1089 * The .alternatives section requires some extra special care over and above
1090 * other special sections because alternatives are patched in place.
1092 static int handle_group_alt(struct objtool_file *file,
1093 struct special_alt *special_alt,
1094 struct instruction *orig_insn,
1095 struct instruction **new_insn)
1097 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1098 struct alt_group *orig_alt_group, *new_alt_group;
1099 unsigned long dest_off;
1102 orig_alt_group = malloc(sizeof(*orig_alt_group));
1103 if (!orig_alt_group) {
1104 WARN("malloc failed");
1107 orig_alt_group->cfi = calloc(special_alt->orig_len,
1108 sizeof(struct cfi_state *));
1109 if (!orig_alt_group->cfi) {
1110 WARN("calloc failed");
1114 last_orig_insn = NULL;
1116 sec_for_each_insn_from(file, insn) {
1117 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1120 insn->alt_group = orig_alt_group;
1121 last_orig_insn = insn;
1123 orig_alt_group->orig_group = NULL;
1124 orig_alt_group->first_insn = orig_insn;
1125 orig_alt_group->last_insn = last_orig_insn;
1128 new_alt_group = malloc(sizeof(*new_alt_group));
1129 if (!new_alt_group) {
1130 WARN("malloc failed");
1134 if (special_alt->new_len < special_alt->orig_len) {
1136 * Insert a fake nop at the end to make the replacement
1137 * alt_group the same size as the original. This is needed to
1138 * allow propagate_alt_cfi() to do its magic. When the last
1139 * instruction affects the stack, the instruction after it (the
1140 * nop) will propagate the new state to the shared CFI array.
1142 nop = malloc(sizeof(*nop));
1144 WARN("malloc failed");
1147 memset(nop, 0, sizeof(*nop));
1148 INIT_LIST_HEAD(&nop->alts);
1149 INIT_LIST_HEAD(&nop->stack_ops);
1150 init_cfi_state(&nop->cfi);
1152 nop->sec = special_alt->new_sec;
1153 nop->offset = special_alt->new_off + special_alt->new_len;
1154 nop->len = special_alt->orig_len - special_alt->new_len;
1155 nop->type = INSN_NOP;
1156 nop->func = orig_insn->func;
1157 nop->alt_group = new_alt_group;
1158 nop->ignore = orig_insn->ignore_alts;
1161 if (!special_alt->new_len) {
1167 sec_for_each_insn_from(file, insn) {
1168 struct reloc *alt_reloc;
1170 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1173 last_new_insn = insn;
1175 insn->ignore = orig_insn->ignore_alts;
1176 insn->func = orig_insn->func;
1177 insn->alt_group = new_alt_group;
1180 * Since alternative replacement code is copy/pasted by the
1181 * kernel after applying relocations, generally such code can't
1182 * have relative-address relocation references to outside the
1183 * .altinstr_replacement section, unless the arch's
1184 * alternatives code can adjust the relative offsets
1187 alt_reloc = insn_reloc(file, insn);
1189 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1191 WARN_FUNC("unsupported relocation in alternatives section",
1192 insn->sec, insn->offset);
1196 if (!is_static_jump(insn))
1199 if (!insn->immediate)
1202 dest_off = arch_jump_destination(insn);
1203 if (dest_off == special_alt->new_off + special_alt->new_len)
1204 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1206 if (!insn->jump_dest) {
1207 WARN_FUNC("can't find alternative jump destination",
1208 insn->sec, insn->offset);
1213 if (!last_new_insn) {
1214 WARN_FUNC("can't find last new alternative instruction",
1215 special_alt->new_sec, special_alt->new_off);
1220 list_add(&nop->list, &last_new_insn->list);
1222 new_alt_group->orig_group = orig_alt_group;
1223 new_alt_group->first_insn = *new_insn;
1224 new_alt_group->last_insn = nop ? : last_new_insn;
1225 new_alt_group->cfi = orig_alt_group->cfi;
1230 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1231 * If the original instruction is a jump, make the alt entry an effective nop
1232 * by just skipping the original instruction.
1234 static int handle_jump_alt(struct objtool_file *file,
1235 struct special_alt *special_alt,
1236 struct instruction *orig_insn,
1237 struct instruction **new_insn)
1239 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1240 orig_insn->type != INSN_NOP) {
1242 WARN_FUNC("unsupported instruction at jump label",
1243 orig_insn->sec, orig_insn->offset);
1247 if (special_alt->key_addend & 2) {
1248 struct reloc *reloc = insn_reloc(file, orig_insn);
1251 reloc->type = R_NONE;
1252 elf_write_reloc(file->elf, reloc);
1254 elf_write_insn(file->elf, orig_insn->sec,
1255 orig_insn->offset, orig_insn->len,
1256 arch_nop_insn(orig_insn->len));
1257 orig_insn->type = INSN_NOP;
1260 if (orig_insn->type == INSN_NOP) {
1261 if (orig_insn->len == 2)
1262 file->jl_nop_short++;
1264 file->jl_nop_long++;
1269 if (orig_insn->len == 2)
1274 *new_insn = list_next_entry(orig_insn, list);
1279 * Read all the special sections which have alternate instructions which can be
1280 * patched in or redirected to at runtime. Each instruction having alternate
1281 * instruction(s) has them added to its insn->alts list, which will be
1282 * traversed in validate_branch().
1284 static int add_special_section_alts(struct objtool_file *file)
1286 struct list_head special_alts;
1287 struct instruction *orig_insn, *new_insn;
1288 struct special_alt *special_alt, *tmp;
1289 struct alternative *alt;
1292 ret = special_get_alts(file->elf, &special_alts);
1296 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1298 orig_insn = find_insn(file, special_alt->orig_sec,
1299 special_alt->orig_off);
1301 WARN_FUNC("special: can't find orig instruction",
1302 special_alt->orig_sec, special_alt->orig_off);
1308 if (!special_alt->group || special_alt->new_len) {
1309 new_insn = find_insn(file, special_alt->new_sec,
1310 special_alt->new_off);
1312 WARN_FUNC("special: can't find new instruction",
1313 special_alt->new_sec,
1314 special_alt->new_off);
1320 if (special_alt->group) {
1321 if (!special_alt->orig_len) {
1322 WARN_FUNC("empty alternative entry",
1323 orig_insn->sec, orig_insn->offset);
1327 ret = handle_group_alt(file, special_alt, orig_insn,
1331 } else if (special_alt->jump_or_nop) {
1332 ret = handle_jump_alt(file, special_alt, orig_insn,
1338 alt = malloc(sizeof(*alt));
1340 WARN("malloc failed");
1345 alt->insn = new_insn;
1346 alt->skip_orig = special_alt->skip_orig;
1347 orig_insn->ignore_alts |= special_alt->skip_alt;
1348 list_add_tail(&alt->list, &orig_insn->alts);
1350 list_del(&special_alt->list);
1355 printf("jl\\\tNOP\tJMP\n");
1356 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1357 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1364 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1365 struct reloc *table)
1367 struct reloc *reloc = table;
1368 struct instruction *dest_insn;
1369 struct alternative *alt;
1370 struct symbol *pfunc = insn->func->pfunc;
1371 unsigned int prev_offset = 0;
1374 * Each @reloc is a switch table relocation which points to the target
1377 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1379 /* Check for the end of the table: */
1380 if (reloc != table && reloc->jump_table_start)
1383 /* Make sure the table entries are consecutive: */
1384 if (prev_offset && reloc->offset != prev_offset + 8)
1387 /* Detect function pointers from contiguous objects: */
1388 if (reloc->sym->sec == pfunc->sec &&
1389 reloc->addend == pfunc->offset)
1392 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1396 /* Make sure the destination is in the same function: */
1397 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1400 alt = malloc(sizeof(*alt));
1402 WARN("malloc failed");
1406 alt->insn = dest_insn;
1407 list_add_tail(&alt->list, &insn->alts);
1408 prev_offset = reloc->offset;
1412 WARN_FUNC("can't find switch jump table",
1413 insn->sec, insn->offset);
1421 * find_jump_table() - Given a dynamic jump, find the switch jump table
1422 * associated with it.
1424 static struct reloc *find_jump_table(struct objtool_file *file,
1425 struct symbol *func,
1426 struct instruction *insn)
1428 struct reloc *table_reloc;
1429 struct instruction *dest_insn, *orig_insn = insn;
1432 * Backward search using the @first_jump_src links, these help avoid
1433 * much of the 'in between' code. Which avoids us getting confused by
1437 insn && insn->func && insn->func->pfunc == func;
1438 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1440 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1443 /* allow small jumps within the range */
1444 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1446 (insn->jump_dest->offset <= insn->offset ||
1447 insn->jump_dest->offset > orig_insn->offset))
1450 table_reloc = arch_find_switch_table(file, insn);
1453 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1454 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1464 * First pass: Mark the head of each jump table so that in the next pass,
1465 * we know when a given jump table ends and the next one starts.
1467 static void mark_func_jump_tables(struct objtool_file *file,
1468 struct symbol *func)
1470 struct instruction *insn, *last = NULL;
1471 struct reloc *reloc;
1473 func_for_each_insn(file, func, insn) {
1478 * Store back-pointers for unconditional forward jumps such
1479 * that find_jump_table() can back-track using those and
1480 * avoid some potentially confusing code.
1482 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1483 insn->offset > last->offset &&
1484 insn->jump_dest->offset > insn->offset &&
1485 !insn->jump_dest->first_jump_src) {
1487 insn->jump_dest->first_jump_src = insn;
1488 last = insn->jump_dest;
1491 if (insn->type != INSN_JUMP_DYNAMIC)
1494 reloc = find_jump_table(file, func, insn);
1496 reloc->jump_table_start = true;
1497 insn->jump_table = reloc;
1502 static int add_func_jump_tables(struct objtool_file *file,
1503 struct symbol *func)
1505 struct instruction *insn;
1508 func_for_each_insn(file, func, insn) {
1509 if (!insn->jump_table)
1512 ret = add_jump_table(file, insn, insn->jump_table);
1521 * For some switch statements, gcc generates a jump table in the .rodata
1522 * section which contains a list of addresses within the function to jump to.
1523 * This finds these jump tables and adds them to the insn->alts lists.
1525 static int add_jump_table_alts(struct objtool_file *file)
1527 struct section *sec;
1528 struct symbol *func;
1534 for_each_sec(file, sec) {
1535 list_for_each_entry(func, &sec->symbol_list, list) {
1536 if (func->type != STT_FUNC)
1539 mark_func_jump_tables(file, func);
1540 ret = add_func_jump_tables(file, func);
1549 static void set_func_state(struct cfi_state *state)
1551 state->cfa = initial_func_cfi.cfa;
1552 memcpy(&state->regs, &initial_func_cfi.regs,
1553 CFI_NUM_REGS * sizeof(struct cfi_reg));
1554 state->stack_size = initial_func_cfi.cfa.offset;
1557 static int read_unwind_hints(struct objtool_file *file)
1559 struct section *sec, *relocsec;
1560 struct reloc *reloc;
1561 struct unwind_hint *hint;
1562 struct instruction *insn;
1565 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1569 relocsec = sec->reloc;
1571 WARN("missing .rela.discard.unwind_hints section");
1575 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1576 WARN("struct unwind_hint size mismatch");
1582 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1583 hint = (struct unwind_hint *)sec->data->d_buf + i;
1585 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1587 WARN("can't find reloc for unwind_hints[%d]", i);
1591 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1593 WARN("can't find insn for unwind_hints[%d]", i);
1599 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1600 set_func_state(&insn->cfi);
1604 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1605 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1606 insn->sec, insn->offset, hint->sp_reg);
1610 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1611 insn->cfi.type = hint->type;
1612 insn->cfi.end = hint->end;
1618 static int read_retpoline_hints(struct objtool_file *file)
1620 struct section *sec;
1621 struct instruction *insn;
1622 struct reloc *reloc;
1624 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1628 list_for_each_entry(reloc, &sec->reloc_list, list) {
1629 if (reloc->sym->type != STT_SECTION) {
1630 WARN("unexpected relocation symbol type in %s", sec->name);
1634 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1636 WARN("bad .discard.retpoline_safe entry");
1640 if (insn->type != INSN_JUMP_DYNAMIC &&
1641 insn->type != INSN_CALL_DYNAMIC) {
1642 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1643 insn->sec, insn->offset);
1647 insn->retpoline_safe = true;
1653 static int read_instr_hints(struct objtool_file *file)
1655 struct section *sec;
1656 struct instruction *insn;
1657 struct reloc *reloc;
1659 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1663 list_for_each_entry(reloc, &sec->reloc_list, list) {
1664 if (reloc->sym->type != STT_SECTION) {
1665 WARN("unexpected relocation symbol type in %s", sec->name);
1669 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1671 WARN("bad .discard.instr_end entry");
1678 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1682 list_for_each_entry(reloc, &sec->reloc_list, list) {
1683 if (reloc->sym->type != STT_SECTION) {
1684 WARN("unexpected relocation symbol type in %s", sec->name);
1688 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1690 WARN("bad .discard.instr_begin entry");
1700 static int read_intra_function_calls(struct objtool_file *file)
1702 struct instruction *insn;
1703 struct section *sec;
1704 struct reloc *reloc;
1706 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1710 list_for_each_entry(reloc, &sec->reloc_list, list) {
1711 unsigned long dest_off;
1713 if (reloc->sym->type != STT_SECTION) {
1714 WARN("unexpected relocation symbol type in %s",
1719 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1721 WARN("bad .discard.intra_function_call entry");
1725 if (insn->type != INSN_CALL) {
1726 WARN_FUNC("intra_function_call not a direct call",
1727 insn->sec, insn->offset);
1732 * Treat intra-function CALLs as JMPs, but with a stack_op.
1733 * See add_call_destinations(), which strips stack_ops from
1736 insn->type = INSN_JUMP_UNCONDITIONAL;
1738 dest_off = insn->offset + insn->len + insn->immediate;
1739 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1740 if (!insn->jump_dest) {
1741 WARN_FUNC("can't find call dest at %s+0x%lx",
1742 insn->sec, insn->offset,
1743 insn->sec->name, dest_off);
1751 static int read_static_call_tramps(struct objtool_file *file)
1753 struct section *sec;
1754 struct symbol *func;
1756 for_each_sec(file, sec) {
1757 list_for_each_entry(func, &sec->symbol_list, list) {
1758 if (func->bind == STB_GLOBAL &&
1759 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1760 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1761 func->static_call_tramp = true;
1768 static void mark_rodata(struct objtool_file *file)
1770 struct section *sec;
1774 * Search for the following rodata sections, each of which can
1775 * potentially contain jump tables:
1777 * - .rodata: can contain GCC switch tables
1778 * - .rodata.<func>: same, if -fdata-sections is being used
1779 * - .rodata..c_jump_table: contains C annotated jump tables
1781 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1783 for_each_sec(file, sec) {
1784 if (!strncmp(sec->name, ".rodata", 7) &&
1785 !strstr(sec->name, ".str1.")) {
1791 file->rodata = found;
1794 __weak int arch_rewrite_retpolines(struct objtool_file *file)
1799 static int decode_sections(struct objtool_file *file)
1805 ret = decode_instructions(file);
1809 ret = add_dead_ends(file);
1814 add_uaccess_safe(file);
1816 ret = add_ignore_alternatives(file);
1821 * Must be before add_{jump_call}_destination.
1823 ret = read_static_call_tramps(file);
1828 * Must be before add_special_section_alts() as that depends on
1829 * jump_dest being set.
1831 ret = add_jump_destinations(file);
1835 ret = add_special_section_alts(file);
1840 * Must be before add_call_destination(); it changes INSN_CALL to
1843 ret = read_intra_function_calls(file);
1847 ret = add_call_destinations(file);
1851 ret = add_jump_table_alts(file);
1855 ret = read_unwind_hints(file);
1859 ret = read_retpoline_hints(file);
1863 ret = read_instr_hints(file);
1868 * Must be after add_special_section_alts(), since this will emit
1869 * alternatives. Must be after add_{jump,call}_destination(), since
1870 * those create the call insn lists.
1872 ret = arch_rewrite_retpolines(file);
1879 static bool is_fentry_call(struct instruction *insn)
1881 if (insn->type == INSN_CALL && insn->call_dest &&
1882 insn->call_dest->type == STT_NOTYPE &&
1883 !strcmp(insn->call_dest->name, "__fentry__"))
1889 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1891 struct cfi_state *cfi = &state->cfi;
1894 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1897 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1900 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1903 for (i = 0; i < CFI_NUM_REGS; i++) {
1904 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1905 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1912 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1913 int expected_offset)
1915 return reg->base == CFI_CFA &&
1916 reg->offset == expected_offset;
1919 static bool has_valid_stack_frame(struct insn_state *state)
1921 struct cfi_state *cfi = &state->cfi;
1923 if (cfi->cfa.base == CFI_BP &&
1924 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1925 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1928 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1934 static int update_cfi_state_regs(struct instruction *insn,
1935 struct cfi_state *cfi,
1936 struct stack_op *op)
1938 struct cfi_reg *cfa = &cfi->cfa;
1940 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1944 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1948 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1951 /* add immediate to sp */
1952 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1953 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1954 cfa->offset -= op->src.offset;
1959 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1961 if (arch_callee_saved_reg(reg) &&
1962 cfi->regs[reg].base == CFI_UNDEFINED) {
1963 cfi->regs[reg].base = base;
1964 cfi->regs[reg].offset = offset;
1968 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1970 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1971 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1975 * A note about DRAP stack alignment:
1977 * GCC has the concept of a DRAP register, which is used to help keep track of
1978 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1979 * register. The typical DRAP pattern is:
1981 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1982 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1983 * 41 ff 72 f8 pushq -0x8(%r10)
1985 * 48 89 e5 mov %rsp,%rbp
1992 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1995 * There are some variations in the epilogues, like:
2003 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2008 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2009 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2010 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2011 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2013 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2016 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2017 * restored beforehand:
2020 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2021 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2023 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2027 static int update_cfi_state(struct instruction *insn,
2028 struct instruction *next_insn,
2029 struct cfi_state *cfi, struct stack_op *op)
2031 struct cfi_reg *cfa = &cfi->cfa;
2032 struct cfi_reg *regs = cfi->regs;
2034 /* stack operations don't make sense with an undefined CFA */
2035 if (cfa->base == CFI_UNDEFINED) {
2037 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2043 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2044 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2045 return update_cfi_state_regs(insn, cfi, op);
2047 switch (op->dest.type) {
2050 switch (op->src.type) {
2053 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2054 cfa->base == CFI_SP &&
2055 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2057 /* mov %rsp, %rbp */
2058 cfa->base = op->dest.reg;
2059 cfi->bp_scratch = false;
2062 else if (op->src.reg == CFI_SP &&
2063 op->dest.reg == CFI_BP && cfi->drap) {
2065 /* drap: mov %rsp, %rbp */
2066 regs[CFI_BP].base = CFI_BP;
2067 regs[CFI_BP].offset = -cfi->stack_size;
2068 cfi->bp_scratch = false;
2071 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2076 * This is needed for the rare case where GCC
2083 cfi->vals[op->dest.reg].base = CFI_CFA;
2084 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2087 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2088 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2093 * Restore the original stack pointer (Clang).
2095 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2098 else if (op->dest.reg == cfa->base) {
2100 /* mov %reg, %rsp */
2101 if (cfa->base == CFI_SP &&
2102 cfi->vals[op->src.reg].base == CFI_CFA) {
2105 * This is needed for the rare case
2106 * where GCC does something dumb like:
2108 * lea 0x8(%rsp), %rcx
2112 cfa->offset = -cfi->vals[op->src.reg].offset;
2113 cfi->stack_size = cfa->offset;
2115 } else if (cfa->base == CFI_SP &&
2116 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2117 cfi->vals[op->src.reg].offset == cfa->offset) {
2122 * 1: mov %rsp, (%[tos])
2123 * 2: mov %[tos], %rsp
2129 * 1 - places a pointer to the previous
2130 * stack at the Top-of-Stack of the
2133 * 2 - switches to the new stack.
2135 * 3 - pops the Top-of-Stack to restore
2136 * the original stack.
2138 * Note: we set base to SP_INDIRECT
2139 * here and preserve offset. Therefore
2140 * when the unwinder reaches ToS it
2141 * will dereference SP and then add the
2142 * offset to find the next frame, IOW:
2145 cfa->base = CFI_SP_INDIRECT;
2148 cfa->base = CFI_UNDEFINED;
2153 else if (op->dest.reg == CFI_SP &&
2154 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2155 cfi->vals[op->src.reg].offset == cfa->offset) {
2158 * The same stack swizzle case 2) as above. But
2159 * because we can't change cfa->base, case 3)
2160 * will become a regular POP. Pretend we're a
2161 * PUSH so things don't go unbalanced.
2163 cfi->stack_size += 8;
2170 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2173 cfi->stack_size -= op->src.offset;
2174 if (cfa->base == CFI_SP)
2175 cfa->offset -= op->src.offset;
2179 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2181 /* lea disp(%rbp), %rsp */
2182 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2186 if (!cfi->drap && op->src.reg == CFI_SP &&
2187 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2188 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2190 /* lea disp(%rsp), %rbp */
2192 cfa->offset -= op->src.offset;
2193 cfi->bp_scratch = false;
2197 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2199 /* drap: lea disp(%rsp), %drap */
2200 cfi->drap_reg = op->dest.reg;
2203 * lea disp(%rsp), %reg
2205 * This is needed for the rare case where GCC
2206 * does something dumb like:
2208 * lea 0x8(%rsp), %rcx
2212 cfi->vals[op->dest.reg].base = CFI_CFA;
2213 cfi->vals[op->dest.reg].offset = \
2214 -cfi->stack_size + op->src.offset;
2219 if (cfi->drap && op->dest.reg == CFI_SP &&
2220 op->src.reg == cfi->drap_reg) {
2222 /* drap: lea disp(%drap), %rsp */
2224 cfa->offset = cfi->stack_size = -op->src.offset;
2225 cfi->drap_reg = CFI_UNDEFINED;
2230 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2231 WARN_FUNC("unsupported stack register modification",
2232 insn->sec, insn->offset);
2239 if (op->dest.reg != CFI_SP ||
2240 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2241 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2242 WARN_FUNC("unsupported stack pointer realignment",
2243 insn->sec, insn->offset);
2247 if (cfi->drap_reg != CFI_UNDEFINED) {
2248 /* drap: and imm, %rsp */
2249 cfa->base = cfi->drap_reg;
2250 cfa->offset = cfi->stack_size = 0;
2255 * Older versions of GCC (4.8ish) realign the stack
2256 * without DRAP, with a frame pointer.
2263 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2265 /* pop %rsp; # restore from a stack swizzle */
2270 if (!cfi->drap && op->dest.reg == cfa->base) {
2276 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2277 op->dest.reg == cfi->drap_reg &&
2278 cfi->drap_offset == -cfi->stack_size) {
2280 /* drap: pop %drap */
2281 cfa->base = cfi->drap_reg;
2283 cfi->drap_offset = -1;
2285 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2288 restore_reg(cfi, op->dest.reg);
2291 cfi->stack_size -= 8;
2292 if (cfa->base == CFI_SP)
2297 case OP_SRC_REG_INDIRECT:
2298 if (!cfi->drap && op->dest.reg == cfa->base &&
2299 op->dest.reg == CFI_BP) {
2301 /* mov disp(%rsp), %rbp */
2303 cfa->offset = cfi->stack_size;
2306 if (cfi->drap && op->src.reg == CFI_BP &&
2307 op->src.offset == cfi->drap_offset) {
2309 /* drap: mov disp(%rbp), %drap */
2310 cfa->base = cfi->drap_reg;
2312 cfi->drap_offset = -1;
2315 if (cfi->drap && op->src.reg == CFI_BP &&
2316 op->src.offset == regs[op->dest.reg].offset) {
2318 /* drap: mov disp(%rbp), %reg */
2319 restore_reg(cfi, op->dest.reg);
2321 } else if (op->src.reg == cfa->base &&
2322 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2324 /* mov disp(%rbp), %reg */
2325 /* mov disp(%rsp), %reg */
2326 restore_reg(cfi, op->dest.reg);
2328 } else if (op->src.reg == CFI_SP &&
2329 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2331 /* mov disp(%rsp), %reg */
2332 restore_reg(cfi, op->dest.reg);
2338 WARN_FUNC("unknown stack-related instruction",
2339 insn->sec, insn->offset);
2347 cfi->stack_size += 8;
2348 if (cfa->base == CFI_SP)
2351 if (op->src.type != OP_SRC_REG)
2355 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2357 /* drap: push %drap */
2358 cfa->base = CFI_BP_INDIRECT;
2359 cfa->offset = -cfi->stack_size;
2361 /* save drap so we know when to restore it */
2362 cfi->drap_offset = -cfi->stack_size;
2364 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2366 /* drap: push %rbp */
2367 cfi->stack_size = 0;
2371 /* drap: push %reg */
2372 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2378 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2381 /* detect when asm code uses rbp as a scratch register */
2382 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2383 cfa->base != CFI_BP)
2384 cfi->bp_scratch = true;
2387 case OP_DEST_REG_INDIRECT:
2390 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2392 /* drap: mov %drap, disp(%rbp) */
2393 cfa->base = CFI_BP_INDIRECT;
2394 cfa->offset = op->dest.offset;
2396 /* save drap offset so we know when to restore it */
2397 cfi->drap_offset = op->dest.offset;
2400 /* drap: mov reg, disp(%rbp) */
2401 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2404 } else if (op->dest.reg == cfa->base) {
2406 /* mov reg, disp(%rbp) */
2407 /* mov reg, disp(%rsp) */
2408 save_reg(cfi, op->src.reg, CFI_CFA,
2409 op->dest.offset - cfi->cfa.offset);
2411 } else if (op->dest.reg == CFI_SP) {
2413 /* mov reg, disp(%rsp) */
2414 save_reg(cfi, op->src.reg, CFI_CFA,
2415 op->dest.offset - cfi->stack_size);
2417 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2419 /* mov %rsp, (%reg); # setup a stack swizzle. */
2420 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2421 cfi->vals[op->dest.reg].offset = cfa->offset;
2427 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2428 WARN_FUNC("unknown stack-related memory operation",
2429 insn->sec, insn->offset);
2434 cfi->stack_size -= 8;
2435 if (cfa->base == CFI_SP)
2441 WARN_FUNC("unknown stack-related instruction",
2442 insn->sec, insn->offset);
2450 * The stack layouts of alternatives instructions can sometimes diverge when
2451 * they have stack modifications. That's fine as long as the potential stack
2452 * layouts don't conflict at any given potential instruction boundary.
2454 * Flatten the CFIs of the different alternative code streams (both original
2455 * and replacement) into a single shared CFI array which can be used to detect
2456 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2458 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2460 struct cfi_state **alt_cfi;
2463 if (!insn->alt_group)
2466 alt_cfi = insn->alt_group->cfi;
2467 group_off = insn->offset - insn->alt_group->first_insn->offset;
2469 if (!alt_cfi[group_off]) {
2470 alt_cfi[group_off] = &insn->cfi;
2472 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2473 WARN_FUNC("stack layout conflict in alternatives",
2474 insn->sec, insn->offset);
2482 static int handle_insn_ops(struct instruction *insn,
2483 struct instruction *next_insn,
2484 struct insn_state *state)
2486 struct stack_op *op;
2488 list_for_each_entry(op, &insn->stack_ops, list) {
2490 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2493 if (!insn->alt_group)
2496 if (op->dest.type == OP_DEST_PUSHF) {
2497 if (!state->uaccess_stack) {
2498 state->uaccess_stack = 1;
2499 } else if (state->uaccess_stack >> 31) {
2500 WARN_FUNC("PUSHF stack exhausted",
2501 insn->sec, insn->offset);
2504 state->uaccess_stack <<= 1;
2505 state->uaccess_stack |= state->uaccess;
2508 if (op->src.type == OP_SRC_POPF) {
2509 if (state->uaccess_stack) {
2510 state->uaccess = state->uaccess_stack & 1;
2511 state->uaccess_stack >>= 1;
2512 if (state->uaccess_stack == 1)
2513 state->uaccess_stack = 0;
2521 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2523 struct cfi_state *cfi1 = &insn->cfi;
2526 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2528 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2529 insn->sec, insn->offset,
2530 cfi1->cfa.base, cfi1->cfa.offset,
2531 cfi2->cfa.base, cfi2->cfa.offset);
2533 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2534 for (i = 0; i < CFI_NUM_REGS; i++) {
2535 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2536 sizeof(struct cfi_reg)))
2539 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2540 insn->sec, insn->offset,
2541 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2542 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2546 } else if (cfi1->type != cfi2->type) {
2548 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2549 insn->sec, insn->offset, cfi1->type, cfi2->type);
2551 } else if (cfi1->drap != cfi2->drap ||
2552 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2553 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2555 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2556 insn->sec, insn->offset,
2557 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2558 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2566 static inline bool func_uaccess_safe(struct symbol *func)
2569 return func->uaccess_safe;
2574 static inline const char *call_dest_name(struct instruction *insn)
2576 if (insn->call_dest)
2577 return insn->call_dest->name;
2582 static inline bool noinstr_call_dest(struct symbol *func)
2585 * We can't deal with indirect function calls at present;
2586 * assume they're instrumented.
2592 * If the symbol is from a noinstr section; we good.
2594 if (func->sec->noinstr)
2598 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2599 * something 'BAD' happened. At the risk of taking the machine down,
2600 * let them proceed to get the message out.
2602 if (!strncmp(func->name, "__ubsan_handle_", 15))
2608 static int validate_call(struct instruction *insn, struct insn_state *state)
2610 if (state->noinstr && state->instr <= 0 &&
2611 !noinstr_call_dest(insn->call_dest)) {
2612 WARN_FUNC("call to %s() leaves .noinstr.text section",
2613 insn->sec, insn->offset, call_dest_name(insn));
2617 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2618 WARN_FUNC("call to %s() with UACCESS enabled",
2619 insn->sec, insn->offset, call_dest_name(insn));
2624 WARN_FUNC("call to %s() with DF set",
2625 insn->sec, insn->offset, call_dest_name(insn));
2632 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2634 if (has_modified_stack_frame(insn, state)) {
2635 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2636 insn->sec, insn->offset);
2640 return validate_call(insn, state);
2643 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2645 if (state->noinstr && state->instr > 0) {
2646 WARN_FUNC("return with instrumentation enabled",
2647 insn->sec, insn->offset);
2651 if (state->uaccess && !func_uaccess_safe(func)) {
2652 WARN_FUNC("return with UACCESS enabled",
2653 insn->sec, insn->offset);
2657 if (!state->uaccess && func_uaccess_safe(func)) {
2658 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2659 insn->sec, insn->offset);
2664 WARN_FUNC("return with DF set",
2665 insn->sec, insn->offset);
2669 if (func && has_modified_stack_frame(insn, state)) {
2670 WARN_FUNC("return with modified stack frame",
2671 insn->sec, insn->offset);
2675 if (state->cfi.bp_scratch) {
2676 WARN_FUNC("BP used as a scratch register",
2677 insn->sec, insn->offset);
2684 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2685 struct instruction *insn)
2687 struct alt_group *alt_group = insn->alt_group;
2690 * Simulate the fact that alternatives are patched in-place. When the
2691 * end of a replacement alt_group is reached, redirect objtool flow to
2692 * the end of the original alt_group.
2694 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2695 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2697 return next_insn_same_sec(file, insn);
2701 * Follow the branch starting at the given instruction, and recursively follow
2702 * any other branches (jumps). Meanwhile, track the frame pointer state at
2703 * each instruction and validate all the rules described in
2704 * tools/objtool/Documentation/stack-validation.txt.
2706 static int validate_branch(struct objtool_file *file, struct symbol *func,
2707 struct instruction *insn, struct insn_state state)
2709 struct alternative *alt;
2710 struct instruction *next_insn;
2711 struct section *sec;
2718 next_insn = next_insn_to_validate(file, insn);
2720 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2721 WARN("%s() falls through to next function %s()",
2722 func->name, insn->func->name);
2726 if (func && insn->ignore) {
2727 WARN_FUNC("BUG: why am I validating an ignored function?",
2732 visited = 1 << state.uaccess;
2733 if (insn->visited) {
2734 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2737 if (insn->visited & visited)
2742 state.instr += insn->instr;
2745 state.cfi = insn->cfi;
2747 insn->cfi = state.cfi;
2749 insn->visited |= visited;
2751 if (propagate_alt_cfi(file, insn))
2754 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2755 bool skip_orig = false;
2757 list_for_each_entry(alt, &insn->alts, list) {
2761 ret = validate_branch(file, func, alt->insn, state);
2764 BT_FUNC("(alt)", insn);
2773 if (handle_insn_ops(insn, next_insn, &state))
2776 switch (insn->type) {
2779 return validate_return(func, insn, &state);
2782 case INSN_CALL_DYNAMIC:
2783 ret = validate_call(insn, &state);
2787 if (!no_fp && func && !is_fentry_call(insn) &&
2788 !has_valid_stack_frame(&state)) {
2789 WARN_FUNC("call without frame pointer save/setup",
2794 if (dead_end_function(file, insn->call_dest))
2799 case INSN_JUMP_CONDITIONAL:
2800 case INSN_JUMP_UNCONDITIONAL:
2801 if (is_sibling_call(insn)) {
2802 ret = validate_sibling_call(insn, &state);
2806 } else if (insn->jump_dest) {
2807 ret = validate_branch(file, func,
2808 insn->jump_dest, state);
2811 BT_FUNC("(branch)", insn);
2816 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2821 case INSN_JUMP_DYNAMIC:
2822 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2823 if (is_sibling_call(insn)) {
2824 ret = validate_sibling_call(insn, &state);
2829 if (insn->type == INSN_JUMP_DYNAMIC)
2834 case INSN_CONTEXT_SWITCH:
2835 if (func && (!next_insn || !next_insn->hint)) {
2836 WARN_FUNC("unsupported instruction in callable function",
2843 if (state.uaccess) {
2844 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2848 state.uaccess = true;
2852 if (!state.uaccess && func) {
2853 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2857 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2858 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2862 state.uaccess = false;
2867 WARN_FUNC("recursive STD", sec, insn->offset);
2875 if (!state.df && func) {
2876 WARN_FUNC("redundant CLD", sec, insn->offset);
2891 if (state.cfi.cfa.base == CFI_UNDEFINED)
2893 WARN("%s: unexpected end of section", sec->name);
2903 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2905 struct instruction *insn;
2906 struct insn_state state;
2907 int ret, warnings = 0;
2912 init_insn_state(&state, sec);
2915 insn = find_insn(file, sec, 0);
2919 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2922 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2923 if (insn->hint && !insn->visited) {
2924 ret = validate_branch(file, insn->func, insn, state);
2925 if (ret && backtrace)
2926 BT_FUNC("<=== (hint)", insn);
2930 insn = list_next_entry(insn, list);
2936 static int validate_retpoline(struct objtool_file *file)
2938 struct instruction *insn;
2941 for_each_insn(file, insn) {
2942 if (insn->type != INSN_JUMP_DYNAMIC &&
2943 insn->type != INSN_CALL_DYNAMIC)
2946 if (insn->retpoline_safe)
2950 * .init.text code is ran before userspace and thus doesn't
2951 * strictly need retpolines, except for modules which are
2952 * loaded late, they very much do need retpoline in their
2955 if (!strcmp(insn->sec->name, ".init.text") && !module)
2958 WARN_FUNC("indirect %s found in RETPOLINE build",
2959 insn->sec, insn->offset,
2960 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2968 static bool is_kasan_insn(struct instruction *insn)
2970 return (insn->type == INSN_CALL &&
2971 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2974 static bool is_ubsan_insn(struct instruction *insn)
2976 return (insn->type == INSN_CALL &&
2977 !strcmp(insn->call_dest->name,
2978 "__ubsan_handle_builtin_unreachable"));
2981 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
2984 struct instruction *prev_insn;
2986 if (insn->ignore || insn->type == INSN_NOP)
2990 * Ignore any unused exceptions. This can happen when a whitelisted
2991 * function has an exception table entry.
2993 * Also ignore alternative replacement instructions. This can happen
2994 * when a whitelisted function uses one of the ALTERNATIVE macros.
2996 if (!strcmp(insn->sec->name, ".fixup") ||
2997 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2998 !strcmp(insn->sec->name, ".altinstr_aux"))
3005 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3006 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3007 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3008 * (or occasionally a JMP to UD2).
3010 * It may also insert a UD2 after calling a __noreturn function.
3012 prev_insn = list_prev_entry(insn, list);
3013 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3014 (insn->type == INSN_BUG ||
3015 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3016 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3020 * Check if this (or a subsequent) instruction is related to
3021 * CONFIG_UBSAN or CONFIG_KASAN.
3023 * End the search at 5 instructions to avoid going into the weeds.
3025 for (i = 0; i < 5; i++) {
3027 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3030 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3031 if (insn->jump_dest &&
3032 insn->jump_dest->func == insn->func) {
3033 insn = insn->jump_dest;
3040 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3043 insn = list_next_entry(insn, list);
3049 static int validate_symbol(struct objtool_file *file, struct section *sec,
3050 struct symbol *sym, struct insn_state *state)
3052 struct instruction *insn;
3056 WARN("%s() is missing an ELF size annotation", sym->name);
3060 if (sym->pfunc != sym || sym->alias != sym)
3063 insn = find_insn(file, sec, sym->offset);
3064 if (!insn || insn->ignore || insn->visited)
3067 state->uaccess = sym->uaccess_safe;
3069 ret = validate_branch(file, insn->func, insn, *state);
3070 if (ret && backtrace)
3071 BT_FUNC("<=== (sym)", insn);
3075 static int validate_section(struct objtool_file *file, struct section *sec)
3077 struct insn_state state;
3078 struct symbol *func;
3081 list_for_each_entry(func, &sec->symbol_list, list) {
3082 if (func->type != STT_FUNC)
3085 init_insn_state(&state, sec);
3086 set_func_state(&state.cfi);
3088 warnings += validate_symbol(file, sec, func, &state);
3094 static int validate_vmlinux_functions(struct objtool_file *file)
3096 struct section *sec;
3099 sec = find_section_by_name(file->elf, ".noinstr.text");
3101 warnings += validate_section(file, sec);
3102 warnings += validate_unwind_hints(file, sec);
3105 sec = find_section_by_name(file->elf, ".entry.text");
3107 warnings += validate_section(file, sec);
3108 warnings += validate_unwind_hints(file, sec);
3114 static int validate_functions(struct objtool_file *file)
3116 struct section *sec;
3119 for_each_sec(file, sec) {
3120 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3123 warnings += validate_section(file, sec);
3129 static int validate_reachable_instructions(struct objtool_file *file)
3131 struct instruction *insn;
3133 if (file->ignore_unreachables)
3136 for_each_insn(file, insn) {
3137 if (insn->visited || ignore_unreachable_insn(file, insn))
3140 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3147 int check(struct objtool_file *file)
3149 int ret, warnings = 0;
3151 arch_initial_func_cfi_state(&initial_func_cfi);
3153 ret = decode_sections(file);
3158 if (list_empty(&file->insn_list))
3161 if (vmlinux && !validate_dup) {
3162 ret = validate_vmlinux_functions(file);
3171 ret = validate_retpoline(file);
3177 ret = validate_functions(file);
3182 ret = validate_unwind_hints(file, NULL);
3188 ret = validate_reachable_instructions(file);
3194 ret = create_static_call_sections(file);
3200 ret = create_mcount_loc_sections(file);
3208 * For now, don't fail the kernel build on fatal warnings. These
3209 * errors are still fairly common due to the growing matrix of
3210 * supported toolchains and their recent pace of change.