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_sibling_call(struct instruction *insn)
114 * Assume only ELF functions can make sibling calls. This ensures
115 * sibling call detection consistency between vmlinux.o and individual
121 /* An indirect jump is either a sibling call or a jump to a table. */
122 if (insn->type == INSN_JUMP_DYNAMIC)
123 return list_empty(&insn->alts);
125 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
126 return (is_static_jump(insn) && insn->call_dest);
130 * This checks to see if the given function is a "noreturn" function.
132 * For global functions which are outside the scope of this object file, we
133 * have to keep a manual list of them.
135 * For local functions, we have to detect them manually by simply looking for
136 * the lack of a return instruction.
138 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
142 struct instruction *insn;
146 * Unfortunately these have to be hard coded because the noreturn
147 * attribute isn't provided in ELF data.
149 static const char * const global_noreturns[] = {
154 "__module_put_and_exit",
160 "machine_real_restart",
161 "rewind_stack_do_exit",
162 "kunit_try_catch_throw",
169 if (func->bind == STB_WEAK)
172 if (func->bind == STB_GLOBAL)
173 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
174 if (!strcmp(func->name, global_noreturns[i]))
180 insn = find_insn(file, func->sec, func->offset);
184 func_for_each_insn(file, func, insn) {
187 if (insn->type == INSN_RETURN)
195 * A function can have a sibling call instead of a return. In that
196 * case, the function's dead-end status depends on whether the target
197 * of the sibling call returns.
199 func_for_each_insn(file, func, insn) {
200 if (is_sibling_call(insn)) {
201 struct instruction *dest = insn->jump_dest;
204 /* sibling call to another file */
207 /* local sibling call */
208 if (recursion == 5) {
210 * Infinite recursion: two functions have
211 * sibling calls to each other. This is a very
212 * rare case. It means they aren't dead ends.
217 return __dead_end_function(file, dest->func, recursion+1);
224 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
226 return __dead_end_function(file, func, 0);
229 static void init_cfi_state(struct cfi_state *cfi)
233 for (i = 0; i < CFI_NUM_REGS; i++) {
234 cfi->regs[i].base = CFI_UNDEFINED;
235 cfi->vals[i].base = CFI_UNDEFINED;
237 cfi->cfa.base = CFI_UNDEFINED;
238 cfi->drap_reg = CFI_UNDEFINED;
239 cfi->drap_offset = -1;
242 static void init_insn_state(struct insn_state *state, struct section *sec)
244 memset(state, 0, sizeof(*state));
245 init_cfi_state(&state->cfi);
248 * We need the full vmlinux for noinstr validation, otherwise we can
249 * not correctly determine insn->call_dest->sec (external symbols do
250 * not have a section).
252 if (vmlinux && noinstr && sec)
253 state->noinstr = sec->noinstr;
257 * Call the arch-specific instruction decoder for all the instructions and add
258 * them to the global instruction list.
260 static int decode_instructions(struct objtool_file *file)
264 unsigned long offset;
265 struct instruction *insn;
266 unsigned long nr_insns = 0;
269 for_each_sec(file, sec) {
271 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
274 if (strcmp(sec->name, ".altinstr_replacement") &&
275 strcmp(sec->name, ".altinstr_aux") &&
276 strncmp(sec->name, ".discard.", 9))
279 if (!strcmp(sec->name, ".noinstr.text") ||
280 !strcmp(sec->name, ".entry.text"))
283 for (offset = 0; offset < sec->len; offset += insn->len) {
284 insn = malloc(sizeof(*insn));
286 WARN("malloc failed");
289 memset(insn, 0, sizeof(*insn));
290 INIT_LIST_HEAD(&insn->alts);
291 INIT_LIST_HEAD(&insn->stack_ops);
292 init_cfi_state(&insn->cfi);
295 insn->offset = offset;
297 ret = arch_decode_instruction(file->elf, sec, offset,
299 &insn->len, &insn->type,
305 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
306 list_add_tail(&insn->list, &file->insn_list);
310 list_for_each_entry(func, &sec->symbol_list, list) {
311 if (func->type != STT_FUNC || func->alias != func)
314 if (!find_insn(file, sec, func->offset)) {
315 WARN("%s(): can't find starting instruction",
320 sym_for_each_insn(file, func, insn)
326 printf("nr_insns: %lu\n", nr_insns);
335 static struct instruction *find_last_insn(struct objtool_file *file,
338 struct instruction *insn = NULL;
340 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
342 for (offset = sec->len - 1; offset >= end && !insn; offset--)
343 insn = find_insn(file, sec, offset);
349 * Mark "ud2" instructions and manually annotated dead ends.
351 static int add_dead_ends(struct objtool_file *file)
355 struct instruction *insn;
358 * By default, "ud2" is a dead end unless otherwise annotated, because
359 * GCC 7 inserts it for certain divide-by-zero cases.
361 for_each_insn(file, insn)
362 if (insn->type == INSN_BUG)
363 insn->dead_end = true;
366 * Check for manually annotated dead ends.
368 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
372 list_for_each_entry(reloc, &sec->reloc_list, list) {
373 if (reloc->sym->type != STT_SECTION) {
374 WARN("unexpected relocation symbol type in %s", sec->name);
377 insn = find_insn(file, reloc->sym->sec, reloc->addend);
379 insn = list_prev_entry(insn, list);
380 else if (reloc->addend == reloc->sym->sec->len) {
381 insn = find_last_insn(file, reloc->sym->sec);
383 WARN("can't find unreachable insn at %s+0x%x",
384 reloc->sym->sec->name, reloc->addend);
388 WARN("can't find unreachable insn at %s+0x%x",
389 reloc->sym->sec->name, reloc->addend);
393 insn->dead_end = true;
398 * These manually annotated reachable checks are needed for GCC 4.4,
399 * where the Linux unreachable() macro isn't supported. In that case
400 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
403 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
407 list_for_each_entry(reloc, &sec->reloc_list, list) {
408 if (reloc->sym->type != STT_SECTION) {
409 WARN("unexpected relocation symbol type in %s", sec->name);
412 insn = find_insn(file, reloc->sym->sec, reloc->addend);
414 insn = list_prev_entry(insn, list);
415 else if (reloc->addend == reloc->sym->sec->len) {
416 insn = find_last_insn(file, reloc->sym->sec);
418 WARN("can't find reachable insn at %s+0x%x",
419 reloc->sym->sec->name, reloc->addend);
423 WARN("can't find reachable insn at %s+0x%x",
424 reloc->sym->sec->name, reloc->addend);
428 insn->dead_end = false;
434 static int create_static_call_sections(struct objtool_file *file)
436 struct section *sec, *reloc_sec;
438 struct static_call_site *site;
439 struct instruction *insn;
440 struct symbol *key_sym;
441 char *key_name, *tmp;
444 sec = find_section_by_name(file->elf, ".static_call_sites");
446 INIT_LIST_HEAD(&file->static_call_list);
447 WARN("file already has .static_call_sites section, skipping");
451 if (list_empty(&file->static_call_list))
455 list_for_each_entry(insn, &file->static_call_list, static_call_node)
458 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
459 sizeof(struct static_call_site), idx);
463 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
468 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
470 site = (struct static_call_site *)sec->data->d_buf + idx;
471 memset(site, 0, sizeof(struct static_call_site));
473 /* populate reloc for 'addr' */
474 reloc = malloc(sizeof(*reloc));
480 memset(reloc, 0, sizeof(*reloc));
482 insn_to_reloc_sym_addend(insn->sec, insn->offset, reloc);
484 WARN_FUNC("static call tramp: missing containing symbol",
485 insn->sec, insn->offset);
489 reloc->type = R_X86_64_PC32;
490 reloc->offset = idx * sizeof(struct static_call_site);
491 reloc->sec = reloc_sec;
492 elf_add_reloc(file->elf, reloc);
494 /* find key symbol */
495 key_name = strdup(insn->call_dest->name);
500 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
501 STATIC_CALL_TRAMP_PREFIX_LEN)) {
502 WARN("static_call: trampoline name malformed: %s", key_name);
505 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
506 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
508 key_sym = find_symbol_by_name(file->elf, tmp);
511 WARN("static_call: can't find static_call_key symbol: %s", tmp);
516 * For modules(), the key might not be exported, which
517 * means the module can make static calls but isn't
518 * allowed to change them.
520 * In that case we temporarily set the key to be the
521 * trampoline address. This is fixed up in
522 * static_call_add_module().
524 key_sym = insn->call_dest;
528 /* populate reloc for 'key' */
529 reloc = malloc(sizeof(*reloc));
534 memset(reloc, 0, sizeof(*reloc));
535 reloc->sym = key_sym;
536 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
537 reloc->type = R_X86_64_PC32;
538 reloc->offset = idx * sizeof(struct static_call_site) + 4;
539 reloc->sec = reloc_sec;
540 elf_add_reloc(file->elf, reloc);
548 static int create_mcount_loc_sections(struct objtool_file *file)
550 struct section *sec, *reloc_sec;
553 struct instruction *insn;
556 sec = find_section_by_name(file->elf, "__mcount_loc");
558 INIT_LIST_HEAD(&file->mcount_loc_list);
559 WARN("file already has __mcount_loc section, skipping");
563 if (list_empty(&file->mcount_loc_list))
567 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
570 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
574 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
579 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
581 loc = (unsigned long *)sec->data->d_buf + idx;
582 memset(loc, 0, sizeof(unsigned long));
584 reloc = malloc(sizeof(*reloc));
589 memset(reloc, 0, sizeof(*reloc));
591 if (insn->sec->sym) {
592 reloc->sym = insn->sec->sym;
593 reloc->addend = insn->offset;
595 reloc->sym = find_symbol_containing(insn->sec, insn->offset);
598 WARN("missing symbol for insn at offset 0x%lx\n",
603 reloc->addend = insn->offset - reloc->sym->offset;
606 reloc->type = R_X86_64_64;
607 reloc->offset = idx * sizeof(unsigned long);
608 reloc->sec = reloc_sec;
609 elf_add_reloc(file->elf, reloc);
618 * Warnings shouldn't be reported for ignored functions.
620 static void add_ignores(struct objtool_file *file)
622 struct instruction *insn;
627 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
631 list_for_each_entry(reloc, &sec->reloc_list, list) {
632 switch (reloc->sym->type) {
638 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
644 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
648 func_for_each_insn(file, func, insn)
654 * This is a whitelist of functions that is allowed to be called with AC set.
655 * The list is meant to be minimal and only contains compiler instrumentation
656 * ABI and a few functions used to implement *_{to,from}_user() functions.
658 * These functions must not directly change AC, but may PUSHF/POPF.
660 static const char *uaccess_safe_builtin[] = {
664 /* KASAN out-of-line */
665 "__asan_loadN_noabort",
666 "__asan_load1_noabort",
667 "__asan_load2_noabort",
668 "__asan_load4_noabort",
669 "__asan_load8_noabort",
670 "__asan_load16_noabort",
671 "__asan_storeN_noabort",
672 "__asan_store1_noabort",
673 "__asan_store2_noabort",
674 "__asan_store4_noabort",
675 "__asan_store8_noabort",
676 "__asan_store16_noabort",
677 "__kasan_check_read",
678 "__kasan_check_write",
680 "__asan_report_load_n_noabort",
681 "__asan_report_load1_noabort",
682 "__asan_report_load2_noabort",
683 "__asan_report_load4_noabort",
684 "__asan_report_load8_noabort",
685 "__asan_report_load16_noabort",
686 "__asan_report_store_n_noabort",
687 "__asan_report_store1_noabort",
688 "__asan_report_store2_noabort",
689 "__asan_report_store4_noabort",
690 "__asan_report_store8_noabort",
691 "__asan_report_store16_noabort",
693 "__kcsan_check_access",
694 "kcsan_found_watchpoint",
695 "kcsan_setup_watchpoint",
696 "kcsan_check_scoped_accesses",
697 "kcsan_disable_current",
698 "kcsan_enable_current_nowarn",
703 "__tsan_write_range",
714 "__tsan_read_write1",
715 "__tsan_read_write2",
716 "__tsan_read_write4",
717 "__tsan_read_write8",
718 "__tsan_read_write16",
719 "__tsan_atomic8_load",
720 "__tsan_atomic16_load",
721 "__tsan_atomic32_load",
722 "__tsan_atomic64_load",
723 "__tsan_atomic8_store",
724 "__tsan_atomic16_store",
725 "__tsan_atomic32_store",
726 "__tsan_atomic64_store",
727 "__tsan_atomic8_exchange",
728 "__tsan_atomic16_exchange",
729 "__tsan_atomic32_exchange",
730 "__tsan_atomic64_exchange",
731 "__tsan_atomic8_fetch_add",
732 "__tsan_atomic16_fetch_add",
733 "__tsan_atomic32_fetch_add",
734 "__tsan_atomic64_fetch_add",
735 "__tsan_atomic8_fetch_sub",
736 "__tsan_atomic16_fetch_sub",
737 "__tsan_atomic32_fetch_sub",
738 "__tsan_atomic64_fetch_sub",
739 "__tsan_atomic8_fetch_and",
740 "__tsan_atomic16_fetch_and",
741 "__tsan_atomic32_fetch_and",
742 "__tsan_atomic64_fetch_and",
743 "__tsan_atomic8_fetch_or",
744 "__tsan_atomic16_fetch_or",
745 "__tsan_atomic32_fetch_or",
746 "__tsan_atomic64_fetch_or",
747 "__tsan_atomic8_fetch_xor",
748 "__tsan_atomic16_fetch_xor",
749 "__tsan_atomic32_fetch_xor",
750 "__tsan_atomic64_fetch_xor",
751 "__tsan_atomic8_fetch_nand",
752 "__tsan_atomic16_fetch_nand",
753 "__tsan_atomic32_fetch_nand",
754 "__tsan_atomic64_fetch_nand",
755 "__tsan_atomic8_compare_exchange_strong",
756 "__tsan_atomic16_compare_exchange_strong",
757 "__tsan_atomic32_compare_exchange_strong",
758 "__tsan_atomic64_compare_exchange_strong",
759 "__tsan_atomic8_compare_exchange_weak",
760 "__tsan_atomic16_compare_exchange_weak",
761 "__tsan_atomic32_compare_exchange_weak",
762 "__tsan_atomic64_compare_exchange_weak",
763 "__tsan_atomic8_compare_exchange_val",
764 "__tsan_atomic16_compare_exchange_val",
765 "__tsan_atomic32_compare_exchange_val",
766 "__tsan_atomic64_compare_exchange_val",
767 "__tsan_atomic_thread_fence",
768 "__tsan_atomic_signal_fence",
772 "__sanitizer_cov_trace_pc",
773 "__sanitizer_cov_trace_const_cmp1",
774 "__sanitizer_cov_trace_const_cmp2",
775 "__sanitizer_cov_trace_const_cmp4",
776 "__sanitizer_cov_trace_const_cmp8",
777 "__sanitizer_cov_trace_cmp1",
778 "__sanitizer_cov_trace_cmp2",
779 "__sanitizer_cov_trace_cmp4",
780 "__sanitizer_cov_trace_cmp8",
781 "__sanitizer_cov_trace_switch",
783 "ubsan_type_mismatch_common",
784 "__ubsan_handle_type_mismatch",
785 "__ubsan_handle_type_mismatch_v1",
786 "__ubsan_handle_shift_out_of_bounds",
788 "csum_partial_copy_generic",
790 "copy_mc_fragile_handle_tail",
791 "copy_mc_enhanced_fast_string",
792 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
796 static void add_uaccess_safe(struct objtool_file *file)
804 for (name = uaccess_safe_builtin; *name; name++) {
805 func = find_symbol_by_name(file->elf, *name);
809 func->uaccess_safe = true;
814 * FIXME: For now, just ignore any alternatives which add retpolines. This is
815 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
816 * But it at least allows objtool to understand the control flow *around* the
819 static int add_ignore_alternatives(struct objtool_file *file)
823 struct instruction *insn;
825 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
829 list_for_each_entry(reloc, &sec->reloc_list, list) {
830 if (reloc->sym->type != STT_SECTION) {
831 WARN("unexpected relocation symbol type in %s", sec->name);
835 insn = find_insn(file, reloc->sym->sec, reloc->addend);
837 WARN("bad .discard.ignore_alts entry");
841 insn->ignore_alts = true;
847 __weak bool arch_is_retpoline(struct symbol *sym)
853 * Find the destination instructions for all jumps.
855 static int add_jump_destinations(struct objtool_file *file)
857 struct instruction *insn;
859 struct section *dest_sec;
860 unsigned long dest_off;
862 for_each_insn(file, insn) {
863 if (!is_static_jump(insn))
866 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
867 insn->offset, insn->len);
869 dest_sec = insn->sec;
870 dest_off = arch_jump_destination(insn);
871 } else if (reloc->sym->type == STT_SECTION) {
872 dest_sec = reloc->sym->sec;
873 dest_off = arch_dest_reloc_offset(reloc->addend);
874 } else if (arch_is_retpoline(reloc->sym)) {
876 * Retpoline jumps are really dynamic jumps in
877 * disguise, so convert them accordingly.
879 if (insn->type == INSN_JUMP_UNCONDITIONAL)
880 insn->type = INSN_JUMP_DYNAMIC;
882 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
884 insn->retpoline_safe = true;
886 } else if (insn->func) {
887 /* internal or external sibling call (with reloc) */
888 insn->call_dest = reloc->sym;
889 if (insn->call_dest->static_call_tramp) {
890 list_add_tail(&insn->static_call_node,
891 &file->static_call_list);
894 } else if (reloc->sym->sec->idx) {
895 dest_sec = reloc->sym->sec;
896 dest_off = reloc->sym->sym.st_value +
897 arch_dest_reloc_offset(reloc->addend);
899 /* non-func asm code jumping to another file */
903 insn->jump_dest = find_insn(file, dest_sec, dest_off);
904 if (!insn->jump_dest) {
907 * This is a special case where an alt instruction
908 * jumps past the end of the section. These are
909 * handled later in handle_group_alt().
911 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
914 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
915 insn->sec, insn->offset, dest_sec->name,
921 * Cross-function jump.
923 if (insn->func && insn->jump_dest->func &&
924 insn->func != insn->jump_dest->func) {
927 * For GCC 8+, create parent/child links for any cold
928 * subfunctions. This is _mostly_ redundant with a
929 * similar initialization in read_symbols().
931 * If a function has aliases, we want the *first* such
932 * function in the symbol table to be the subfunction's
933 * parent. In that case we overwrite the
934 * initialization done in read_symbols().
936 * However this code can't completely replace the
937 * read_symbols() code because this doesn't detect the
938 * case where the parent function's only reference to a
939 * subfunction is through a jump table.
941 if (!strstr(insn->func->name, ".cold") &&
942 strstr(insn->jump_dest->func->name, ".cold")) {
943 insn->func->cfunc = insn->jump_dest->func;
944 insn->jump_dest->func->pfunc = insn->func;
946 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
947 insn->jump_dest->offset == insn->jump_dest->func->offset) {
949 /* internal sibling call (without reloc) */
950 insn->call_dest = insn->jump_dest->func;
951 if (insn->call_dest->static_call_tramp) {
952 list_add_tail(&insn->static_call_node,
953 &file->static_call_list);
962 static void remove_insn_ops(struct instruction *insn)
964 struct stack_op *op, *tmp;
966 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
972 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
974 struct symbol *call_dest;
976 call_dest = find_func_by_offset(sec, offset);
978 call_dest = find_symbol_by_offset(sec, offset);
984 * Find the destination instructions for all calls.
986 static int add_call_destinations(struct objtool_file *file)
988 struct instruction *insn;
989 unsigned long dest_off;
992 for_each_insn(file, insn) {
993 if (insn->type != INSN_CALL)
996 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
997 insn->offset, insn->len);
999 dest_off = arch_jump_destination(insn);
1000 insn->call_dest = find_call_destination(insn->sec, dest_off);
1005 if (!insn->call_dest) {
1006 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1010 if (insn->func && insn->call_dest->type != STT_FUNC) {
1011 WARN_FUNC("unsupported call to non-function",
1012 insn->sec, insn->offset);
1016 } else if (reloc->sym->type == STT_SECTION) {
1017 dest_off = arch_dest_reloc_offset(reloc->addend);
1018 insn->call_dest = find_call_destination(reloc->sym->sec,
1020 if (!insn->call_dest) {
1021 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1022 insn->sec, insn->offset,
1023 reloc->sym->sec->name,
1028 } else if (arch_is_retpoline(reloc->sym)) {
1030 * Retpoline calls are really dynamic calls in
1031 * disguise, so convert them accordingly.
1033 insn->type = INSN_CALL_DYNAMIC;
1034 insn->retpoline_safe = true;
1036 remove_insn_ops(insn);
1040 insn->call_dest = reloc->sym;
1042 if (insn->call_dest && insn->call_dest->static_call_tramp) {
1043 list_add_tail(&insn->static_call_node,
1044 &file->static_call_list);
1048 * Many compilers cannot disable KCOV with a function attribute
1049 * so they need a little help, NOP out any KCOV calls from noinstr
1052 if (insn->sec->noinstr &&
1053 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
1055 reloc->type = R_NONE;
1056 elf_write_reloc(file->elf, reloc);
1059 elf_write_insn(file->elf, insn->sec,
1060 insn->offset, insn->len,
1061 arch_nop_insn(insn->len));
1062 insn->type = INSN_NOP;
1065 if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1067 reloc->type = R_NONE;
1068 elf_write_reloc(file->elf, reloc);
1071 elf_write_insn(file->elf, insn->sec,
1072 insn->offset, insn->len,
1073 arch_nop_insn(insn->len));
1075 insn->type = INSN_NOP;
1077 list_add_tail(&insn->mcount_loc_node,
1078 &file->mcount_loc_list);
1082 * Whatever stack impact regular CALLs have, should be undone
1083 * by the RETURN of the called function.
1085 * Annotated intra-function calls retain the stack_ops but
1086 * are converted to JUMP, see read_intra_function_calls().
1088 remove_insn_ops(insn);
1095 * The .alternatives section requires some extra special care over and above
1096 * other special sections because alternatives are patched in place.
1098 static int handle_group_alt(struct objtool_file *file,
1099 struct special_alt *special_alt,
1100 struct instruction *orig_insn,
1101 struct instruction **new_insn)
1103 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1104 struct alt_group *orig_alt_group, *new_alt_group;
1105 unsigned long dest_off;
1108 orig_alt_group = malloc(sizeof(*orig_alt_group));
1109 if (!orig_alt_group) {
1110 WARN("malloc failed");
1113 orig_alt_group->cfi = calloc(special_alt->orig_len,
1114 sizeof(struct cfi_state *));
1115 if (!orig_alt_group->cfi) {
1116 WARN("calloc failed");
1120 last_orig_insn = NULL;
1122 sec_for_each_insn_from(file, insn) {
1123 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1126 insn->alt_group = orig_alt_group;
1127 last_orig_insn = insn;
1129 orig_alt_group->orig_group = NULL;
1130 orig_alt_group->first_insn = orig_insn;
1131 orig_alt_group->last_insn = last_orig_insn;
1134 new_alt_group = malloc(sizeof(*new_alt_group));
1135 if (!new_alt_group) {
1136 WARN("malloc failed");
1140 if (special_alt->new_len < special_alt->orig_len) {
1142 * Insert a fake nop at the end to make the replacement
1143 * alt_group the same size as the original. This is needed to
1144 * allow propagate_alt_cfi() to do its magic. When the last
1145 * instruction affects the stack, the instruction after it (the
1146 * nop) will propagate the new state to the shared CFI array.
1148 nop = malloc(sizeof(*nop));
1150 WARN("malloc failed");
1153 memset(nop, 0, sizeof(*nop));
1154 INIT_LIST_HEAD(&nop->alts);
1155 INIT_LIST_HEAD(&nop->stack_ops);
1156 init_cfi_state(&nop->cfi);
1158 nop->sec = special_alt->new_sec;
1159 nop->offset = special_alt->new_off + special_alt->new_len;
1160 nop->len = special_alt->orig_len - special_alt->new_len;
1161 nop->type = INSN_NOP;
1162 nop->func = orig_insn->func;
1163 nop->alt_group = new_alt_group;
1164 nop->ignore = orig_insn->ignore_alts;
1167 if (!special_alt->new_len) {
1173 sec_for_each_insn_from(file, insn) {
1174 struct reloc *alt_reloc;
1176 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1179 last_new_insn = insn;
1181 insn->ignore = orig_insn->ignore_alts;
1182 insn->func = orig_insn->func;
1183 insn->alt_group = new_alt_group;
1186 * Since alternative replacement code is copy/pasted by the
1187 * kernel after applying relocations, generally such code can't
1188 * have relative-address relocation references to outside the
1189 * .altinstr_replacement section, unless the arch's
1190 * alternatives code can adjust the relative offsets
1193 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1194 insn->offset, insn->len);
1196 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1198 WARN_FUNC("unsupported relocation in alternatives section",
1199 insn->sec, insn->offset);
1203 if (!is_static_jump(insn))
1206 if (!insn->immediate)
1209 dest_off = arch_jump_destination(insn);
1210 if (dest_off == special_alt->new_off + special_alt->new_len)
1211 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1213 if (!insn->jump_dest) {
1214 WARN_FUNC("can't find alternative jump destination",
1215 insn->sec, insn->offset);
1220 if (!last_new_insn) {
1221 WARN_FUNC("can't find last new alternative instruction",
1222 special_alt->new_sec, special_alt->new_off);
1227 list_add(&nop->list, &last_new_insn->list);
1229 new_alt_group->orig_group = orig_alt_group;
1230 new_alt_group->first_insn = *new_insn;
1231 new_alt_group->last_insn = nop ? : last_new_insn;
1232 new_alt_group->cfi = orig_alt_group->cfi;
1237 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1238 * If the original instruction is a jump, make the alt entry an effective nop
1239 * by just skipping the original instruction.
1241 static int handle_jump_alt(struct objtool_file *file,
1242 struct special_alt *special_alt,
1243 struct instruction *orig_insn,
1244 struct instruction **new_insn)
1246 if (orig_insn->type == INSN_NOP)
1249 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1250 WARN_FUNC("unsupported instruction at jump label",
1251 orig_insn->sec, orig_insn->offset);
1255 *new_insn = list_next_entry(orig_insn, list);
1260 * Read all the special sections which have alternate instructions which can be
1261 * patched in or redirected to at runtime. Each instruction having alternate
1262 * instruction(s) has them added to its insn->alts list, which will be
1263 * traversed in validate_branch().
1265 static int add_special_section_alts(struct objtool_file *file)
1267 struct list_head special_alts;
1268 struct instruction *orig_insn, *new_insn;
1269 struct special_alt *special_alt, *tmp;
1270 struct alternative *alt;
1273 ret = special_get_alts(file->elf, &special_alts);
1277 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1279 orig_insn = find_insn(file, special_alt->orig_sec,
1280 special_alt->orig_off);
1282 WARN_FUNC("special: can't find orig instruction",
1283 special_alt->orig_sec, special_alt->orig_off);
1289 if (!special_alt->group || special_alt->new_len) {
1290 new_insn = find_insn(file, special_alt->new_sec,
1291 special_alt->new_off);
1293 WARN_FUNC("special: can't find new instruction",
1294 special_alt->new_sec,
1295 special_alt->new_off);
1301 if (special_alt->group) {
1302 if (!special_alt->orig_len) {
1303 WARN_FUNC("empty alternative entry",
1304 orig_insn->sec, orig_insn->offset);
1308 ret = handle_group_alt(file, special_alt, orig_insn,
1312 } else if (special_alt->jump_or_nop) {
1313 ret = handle_jump_alt(file, special_alt, orig_insn,
1319 alt = malloc(sizeof(*alt));
1321 WARN("malloc failed");
1326 alt->insn = new_insn;
1327 alt->skip_orig = special_alt->skip_orig;
1328 orig_insn->ignore_alts |= special_alt->skip_alt;
1329 list_add_tail(&alt->list, &orig_insn->alts);
1331 list_del(&special_alt->list);
1339 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1340 struct reloc *table)
1342 struct reloc *reloc = table;
1343 struct instruction *dest_insn;
1344 struct alternative *alt;
1345 struct symbol *pfunc = insn->func->pfunc;
1346 unsigned int prev_offset = 0;
1349 * Each @reloc is a switch table relocation which points to the target
1352 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1354 /* Check for the end of the table: */
1355 if (reloc != table && reloc->jump_table_start)
1358 /* Make sure the table entries are consecutive: */
1359 if (prev_offset && reloc->offset != prev_offset + 8)
1362 /* Detect function pointers from contiguous objects: */
1363 if (reloc->sym->sec == pfunc->sec &&
1364 reloc->addend == pfunc->offset)
1367 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1371 /* Make sure the destination is in the same function: */
1372 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1375 alt = malloc(sizeof(*alt));
1377 WARN("malloc failed");
1381 alt->insn = dest_insn;
1382 list_add_tail(&alt->list, &insn->alts);
1383 prev_offset = reloc->offset;
1387 WARN_FUNC("can't find switch jump table",
1388 insn->sec, insn->offset);
1396 * find_jump_table() - Given a dynamic jump, find the switch jump table
1397 * associated with it.
1399 static struct reloc *find_jump_table(struct objtool_file *file,
1400 struct symbol *func,
1401 struct instruction *insn)
1403 struct reloc *table_reloc;
1404 struct instruction *dest_insn, *orig_insn = insn;
1407 * Backward search using the @first_jump_src links, these help avoid
1408 * much of the 'in between' code. Which avoids us getting confused by
1412 insn && insn->func && insn->func->pfunc == func;
1413 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1415 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1418 /* allow small jumps within the range */
1419 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1421 (insn->jump_dest->offset <= insn->offset ||
1422 insn->jump_dest->offset > orig_insn->offset))
1425 table_reloc = arch_find_switch_table(file, insn);
1428 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1429 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1439 * First pass: Mark the head of each jump table so that in the next pass,
1440 * we know when a given jump table ends and the next one starts.
1442 static void mark_func_jump_tables(struct objtool_file *file,
1443 struct symbol *func)
1445 struct instruction *insn, *last = NULL;
1446 struct reloc *reloc;
1448 func_for_each_insn(file, func, insn) {
1453 * Store back-pointers for unconditional forward jumps such
1454 * that find_jump_table() can back-track using those and
1455 * avoid some potentially confusing code.
1457 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1458 insn->offset > last->offset &&
1459 insn->jump_dest->offset > insn->offset &&
1460 !insn->jump_dest->first_jump_src) {
1462 insn->jump_dest->first_jump_src = insn;
1463 last = insn->jump_dest;
1466 if (insn->type != INSN_JUMP_DYNAMIC)
1469 reloc = find_jump_table(file, func, insn);
1471 reloc->jump_table_start = true;
1472 insn->jump_table = reloc;
1477 static int add_func_jump_tables(struct objtool_file *file,
1478 struct symbol *func)
1480 struct instruction *insn;
1483 func_for_each_insn(file, func, insn) {
1484 if (!insn->jump_table)
1487 ret = add_jump_table(file, insn, insn->jump_table);
1496 * For some switch statements, gcc generates a jump table in the .rodata
1497 * section which contains a list of addresses within the function to jump to.
1498 * This finds these jump tables and adds them to the insn->alts lists.
1500 static int add_jump_table_alts(struct objtool_file *file)
1502 struct section *sec;
1503 struct symbol *func;
1509 for_each_sec(file, sec) {
1510 list_for_each_entry(func, &sec->symbol_list, list) {
1511 if (func->type != STT_FUNC)
1514 mark_func_jump_tables(file, func);
1515 ret = add_func_jump_tables(file, func);
1524 static void set_func_state(struct cfi_state *state)
1526 state->cfa = initial_func_cfi.cfa;
1527 memcpy(&state->regs, &initial_func_cfi.regs,
1528 CFI_NUM_REGS * sizeof(struct cfi_reg));
1529 state->stack_size = initial_func_cfi.cfa.offset;
1532 static int read_unwind_hints(struct objtool_file *file)
1534 struct section *sec, *relocsec;
1535 struct reloc *reloc;
1536 struct unwind_hint *hint;
1537 struct instruction *insn;
1540 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1544 relocsec = sec->reloc;
1546 WARN("missing .rela.discard.unwind_hints section");
1550 if (sec->len % sizeof(struct unwind_hint)) {
1551 WARN("struct unwind_hint size mismatch");
1557 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1558 hint = (struct unwind_hint *)sec->data->d_buf + i;
1560 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1562 WARN("can't find reloc for unwind_hints[%d]", i);
1566 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1568 WARN("can't find insn for unwind_hints[%d]", i);
1574 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1575 set_func_state(&insn->cfi);
1579 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1580 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1581 insn->sec, insn->offset, hint->sp_reg);
1585 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1586 insn->cfi.type = hint->type;
1587 insn->cfi.end = hint->end;
1593 static int read_retpoline_hints(struct objtool_file *file)
1595 struct section *sec;
1596 struct instruction *insn;
1597 struct reloc *reloc;
1599 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1603 list_for_each_entry(reloc, &sec->reloc_list, list) {
1604 if (reloc->sym->type != STT_SECTION) {
1605 WARN("unexpected relocation symbol type in %s", sec->name);
1609 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1611 WARN("bad .discard.retpoline_safe entry");
1615 if (insn->type != INSN_JUMP_DYNAMIC &&
1616 insn->type != INSN_CALL_DYNAMIC) {
1617 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1618 insn->sec, insn->offset);
1622 insn->retpoline_safe = true;
1628 static int read_instr_hints(struct objtool_file *file)
1630 struct section *sec;
1631 struct instruction *insn;
1632 struct reloc *reloc;
1634 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1638 list_for_each_entry(reloc, &sec->reloc_list, list) {
1639 if (reloc->sym->type != STT_SECTION) {
1640 WARN("unexpected relocation symbol type in %s", sec->name);
1644 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1646 WARN("bad .discard.instr_end entry");
1653 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1657 list_for_each_entry(reloc, &sec->reloc_list, list) {
1658 if (reloc->sym->type != STT_SECTION) {
1659 WARN("unexpected relocation symbol type in %s", sec->name);
1663 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1665 WARN("bad .discard.instr_begin entry");
1675 static int read_intra_function_calls(struct objtool_file *file)
1677 struct instruction *insn;
1678 struct section *sec;
1679 struct reloc *reloc;
1681 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1685 list_for_each_entry(reloc, &sec->reloc_list, list) {
1686 unsigned long dest_off;
1688 if (reloc->sym->type != STT_SECTION) {
1689 WARN("unexpected relocation symbol type in %s",
1694 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1696 WARN("bad .discard.intra_function_call entry");
1700 if (insn->type != INSN_CALL) {
1701 WARN_FUNC("intra_function_call not a direct call",
1702 insn->sec, insn->offset);
1707 * Treat intra-function CALLs as JMPs, but with a stack_op.
1708 * See add_call_destinations(), which strips stack_ops from
1711 insn->type = INSN_JUMP_UNCONDITIONAL;
1713 dest_off = insn->offset + insn->len + insn->immediate;
1714 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1715 if (!insn->jump_dest) {
1716 WARN_FUNC("can't find call dest at %s+0x%lx",
1717 insn->sec, insn->offset,
1718 insn->sec->name, dest_off);
1726 static int read_static_call_tramps(struct objtool_file *file)
1728 struct section *sec;
1729 struct symbol *func;
1731 for_each_sec(file, sec) {
1732 list_for_each_entry(func, &sec->symbol_list, list) {
1733 if (func->bind == STB_GLOBAL &&
1734 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1735 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1736 func->static_call_tramp = true;
1743 static void mark_rodata(struct objtool_file *file)
1745 struct section *sec;
1749 * Search for the following rodata sections, each of which can
1750 * potentially contain jump tables:
1752 * - .rodata: can contain GCC switch tables
1753 * - .rodata.<func>: same, if -fdata-sections is being used
1754 * - .rodata..c_jump_table: contains C annotated jump tables
1756 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1758 for_each_sec(file, sec) {
1759 if (!strncmp(sec->name, ".rodata", 7) &&
1760 !strstr(sec->name, ".str1.")) {
1766 file->rodata = found;
1769 static int decode_sections(struct objtool_file *file)
1775 ret = decode_instructions(file);
1779 ret = add_dead_ends(file);
1784 add_uaccess_safe(file);
1786 ret = add_ignore_alternatives(file);
1791 * Must be before add_{jump_call}_destination.
1793 ret = read_static_call_tramps(file);
1797 ret = add_jump_destinations(file);
1801 ret = add_special_section_alts(file);
1806 * Must be before add_call_destination(); it changes INSN_CALL to
1809 ret = read_intra_function_calls(file);
1813 ret = add_call_destinations(file);
1817 ret = add_jump_table_alts(file);
1821 ret = read_unwind_hints(file);
1825 ret = read_retpoline_hints(file);
1829 ret = read_instr_hints(file);
1836 static bool is_fentry_call(struct instruction *insn)
1838 if (insn->type == INSN_CALL && insn->call_dest &&
1839 insn->call_dest->type == STT_NOTYPE &&
1840 !strcmp(insn->call_dest->name, "__fentry__"))
1846 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1848 struct cfi_state *cfi = &state->cfi;
1851 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1854 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1857 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1860 for (i = 0; i < CFI_NUM_REGS; i++) {
1861 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1862 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1869 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1870 int expected_offset)
1872 return reg->base == CFI_CFA &&
1873 reg->offset == expected_offset;
1876 static bool has_valid_stack_frame(struct insn_state *state)
1878 struct cfi_state *cfi = &state->cfi;
1880 if (cfi->cfa.base == CFI_BP &&
1881 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1882 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1885 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1891 static int update_cfi_state_regs(struct instruction *insn,
1892 struct cfi_state *cfi,
1893 struct stack_op *op)
1895 struct cfi_reg *cfa = &cfi->cfa;
1897 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1901 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1905 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1908 /* add immediate to sp */
1909 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1910 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1911 cfa->offset -= op->src.offset;
1916 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1918 if (arch_callee_saved_reg(reg) &&
1919 cfi->regs[reg].base == CFI_UNDEFINED) {
1920 cfi->regs[reg].base = base;
1921 cfi->regs[reg].offset = offset;
1925 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1927 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1928 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1932 * A note about DRAP stack alignment:
1934 * GCC has the concept of a DRAP register, which is used to help keep track of
1935 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1936 * register. The typical DRAP pattern is:
1938 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1939 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1940 * 41 ff 72 f8 pushq -0x8(%r10)
1942 * 48 89 e5 mov %rsp,%rbp
1949 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1952 * There are some variations in the epilogues, like:
1960 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1965 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1966 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1967 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1968 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1970 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1973 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1974 * restored beforehand:
1977 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1978 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1980 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1984 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1985 struct stack_op *op)
1987 struct cfi_reg *cfa = &cfi->cfa;
1988 struct cfi_reg *regs = cfi->regs;
1990 /* stack operations don't make sense with an undefined CFA */
1991 if (cfa->base == CFI_UNDEFINED) {
1993 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1999 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2000 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2001 return update_cfi_state_regs(insn, cfi, op);
2003 switch (op->dest.type) {
2006 switch (op->src.type) {
2009 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2010 cfa->base == CFI_SP &&
2011 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2013 /* mov %rsp, %rbp */
2014 cfa->base = op->dest.reg;
2015 cfi->bp_scratch = false;
2018 else if (op->src.reg == CFI_SP &&
2019 op->dest.reg == CFI_BP && cfi->drap) {
2021 /* drap: mov %rsp, %rbp */
2022 regs[CFI_BP].base = CFI_BP;
2023 regs[CFI_BP].offset = -cfi->stack_size;
2024 cfi->bp_scratch = false;
2027 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2032 * This is needed for the rare case where GCC
2039 cfi->vals[op->dest.reg].base = CFI_CFA;
2040 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2043 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2044 cfa->base == CFI_BP) {
2049 * Restore the original stack pointer (Clang).
2051 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2054 else if (op->dest.reg == cfa->base) {
2056 /* mov %reg, %rsp */
2057 if (cfa->base == CFI_SP &&
2058 cfi->vals[op->src.reg].base == CFI_CFA) {
2061 * This is needed for the rare case
2062 * where GCC does something dumb like:
2064 * lea 0x8(%rsp), %rcx
2068 cfa->offset = -cfi->vals[op->src.reg].offset;
2069 cfi->stack_size = cfa->offset;
2071 } else if (cfa->base == CFI_SP &&
2072 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2073 cfi->vals[op->src.reg].offset == cfa->offset) {
2078 * 1: mov %rsp, (%[tos])
2079 * 2: mov %[tos], %rsp
2085 * 1 - places a pointer to the previous
2086 * stack at the Top-of-Stack of the
2089 * 2 - switches to the new stack.
2091 * 3 - pops the Top-of-Stack to restore
2092 * the original stack.
2094 * Note: we set base to SP_INDIRECT
2095 * here and preserve offset. Therefore
2096 * when the unwinder reaches ToS it
2097 * will dereference SP and then add the
2098 * offset to find the next frame, IOW:
2101 cfa->base = CFI_SP_INDIRECT;
2104 cfa->base = CFI_UNDEFINED;
2109 else if (op->dest.reg == CFI_SP &&
2110 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2111 cfi->vals[op->src.reg].offset == cfa->offset) {
2114 * The same stack swizzle case 2) as above. But
2115 * because we can't change cfa->base, case 3)
2116 * will become a regular POP. Pretend we're a
2117 * PUSH so things don't go unbalanced.
2119 cfi->stack_size += 8;
2126 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2129 cfi->stack_size -= op->src.offset;
2130 if (cfa->base == CFI_SP)
2131 cfa->offset -= op->src.offset;
2135 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2137 /* lea disp(%rbp), %rsp */
2138 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2142 if (!cfi->drap && op->src.reg == CFI_SP &&
2143 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2144 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2146 /* lea disp(%rsp), %rbp */
2148 cfa->offset -= op->src.offset;
2149 cfi->bp_scratch = false;
2153 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2155 /* drap: lea disp(%rsp), %drap */
2156 cfi->drap_reg = op->dest.reg;
2159 * lea disp(%rsp), %reg
2161 * This is needed for the rare case where GCC
2162 * does something dumb like:
2164 * lea 0x8(%rsp), %rcx
2168 cfi->vals[op->dest.reg].base = CFI_CFA;
2169 cfi->vals[op->dest.reg].offset = \
2170 -cfi->stack_size + op->src.offset;
2175 if (cfi->drap && op->dest.reg == CFI_SP &&
2176 op->src.reg == cfi->drap_reg) {
2178 /* drap: lea disp(%drap), %rsp */
2180 cfa->offset = cfi->stack_size = -op->src.offset;
2181 cfi->drap_reg = CFI_UNDEFINED;
2186 if (op->dest.reg == cfi->cfa.base) {
2187 WARN_FUNC("unsupported stack register modification",
2188 insn->sec, insn->offset);
2195 if (op->dest.reg != CFI_SP ||
2196 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2197 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2198 WARN_FUNC("unsupported stack pointer realignment",
2199 insn->sec, insn->offset);
2203 if (cfi->drap_reg != CFI_UNDEFINED) {
2204 /* drap: and imm, %rsp */
2205 cfa->base = cfi->drap_reg;
2206 cfa->offset = cfi->stack_size = 0;
2211 * Older versions of GCC (4.8ish) realign the stack
2212 * without DRAP, with a frame pointer.
2219 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2221 /* pop %rsp; # restore from a stack swizzle */
2226 if (!cfi->drap && op->dest.reg == cfa->base) {
2232 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2233 op->dest.reg == cfi->drap_reg &&
2234 cfi->drap_offset == -cfi->stack_size) {
2236 /* drap: pop %drap */
2237 cfa->base = cfi->drap_reg;
2239 cfi->drap_offset = -1;
2241 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2244 restore_reg(cfi, op->dest.reg);
2247 cfi->stack_size -= 8;
2248 if (cfa->base == CFI_SP)
2253 case OP_SRC_REG_INDIRECT:
2254 if (!cfi->drap && op->dest.reg == cfa->base &&
2255 op->dest.reg == CFI_BP) {
2257 /* mov disp(%rsp), %rbp */
2259 cfa->offset = cfi->stack_size;
2262 if (cfi->drap && op->src.reg == CFI_BP &&
2263 op->src.offset == cfi->drap_offset) {
2265 /* drap: mov disp(%rbp), %drap */
2266 cfa->base = cfi->drap_reg;
2268 cfi->drap_offset = -1;
2271 if (cfi->drap && op->src.reg == CFI_BP &&
2272 op->src.offset == regs[op->dest.reg].offset) {
2274 /* drap: mov disp(%rbp), %reg */
2275 restore_reg(cfi, op->dest.reg);
2277 } else if (op->src.reg == cfa->base &&
2278 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2280 /* mov disp(%rbp), %reg */
2281 /* mov disp(%rsp), %reg */
2282 restore_reg(cfi, op->dest.reg);
2284 } else if (op->src.reg == CFI_SP &&
2285 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2287 /* mov disp(%rsp), %reg */
2288 restore_reg(cfi, op->dest.reg);
2294 WARN_FUNC("unknown stack-related instruction",
2295 insn->sec, insn->offset);
2303 cfi->stack_size += 8;
2304 if (cfa->base == CFI_SP)
2307 if (op->src.type != OP_SRC_REG)
2311 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2313 /* drap: push %drap */
2314 cfa->base = CFI_BP_INDIRECT;
2315 cfa->offset = -cfi->stack_size;
2317 /* save drap so we know when to restore it */
2318 cfi->drap_offset = -cfi->stack_size;
2320 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2322 /* drap: push %rbp */
2323 cfi->stack_size = 0;
2327 /* drap: push %reg */
2328 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2334 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2337 /* detect when asm code uses rbp as a scratch register */
2338 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2339 cfa->base != CFI_BP)
2340 cfi->bp_scratch = true;
2343 case OP_DEST_REG_INDIRECT:
2346 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2348 /* drap: mov %drap, disp(%rbp) */
2349 cfa->base = CFI_BP_INDIRECT;
2350 cfa->offset = op->dest.offset;
2352 /* save drap offset so we know when to restore it */
2353 cfi->drap_offset = op->dest.offset;
2356 /* drap: mov reg, disp(%rbp) */
2357 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2360 } else if (op->dest.reg == cfa->base) {
2362 /* mov reg, disp(%rbp) */
2363 /* mov reg, disp(%rsp) */
2364 save_reg(cfi, op->src.reg, CFI_CFA,
2365 op->dest.offset - cfi->cfa.offset);
2367 } else if (op->dest.reg == CFI_SP) {
2369 /* mov reg, disp(%rsp) */
2370 save_reg(cfi, op->src.reg, CFI_CFA,
2371 op->dest.offset - cfi->stack_size);
2373 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2375 /* mov %rsp, (%reg); # setup a stack swizzle. */
2376 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2377 cfi->vals[op->dest.reg].offset = cfa->offset;
2383 if ((!cfi->drap && cfa->base != CFI_BP) ||
2384 (cfi->drap && cfa->base != cfi->drap_reg)) {
2385 WARN_FUNC("leave instruction with modified stack frame",
2386 insn->sec, insn->offset);
2390 /* leave (mov %rbp, %rsp; pop %rbp) */
2392 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2393 restore_reg(cfi, CFI_BP);
2403 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2404 WARN_FUNC("unknown stack-related memory operation",
2405 insn->sec, insn->offset);
2410 cfi->stack_size -= 8;
2411 if (cfa->base == CFI_SP)
2417 WARN_FUNC("unknown stack-related instruction",
2418 insn->sec, insn->offset);
2426 * The stack layouts of alternatives instructions can sometimes diverge when
2427 * they have stack modifications. That's fine as long as the potential stack
2428 * layouts don't conflict at any given potential instruction boundary.
2430 * Flatten the CFIs of the different alternative code streams (both original
2431 * and replacement) into a single shared CFI array which can be used to detect
2432 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2434 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2436 struct cfi_state **alt_cfi;
2439 if (!insn->alt_group)
2442 alt_cfi = insn->alt_group->cfi;
2443 group_off = insn->offset - insn->alt_group->first_insn->offset;
2445 if (!alt_cfi[group_off]) {
2446 alt_cfi[group_off] = &insn->cfi;
2448 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2449 WARN_FUNC("stack layout conflict in alternatives",
2450 insn->sec, insn->offset);
2458 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2460 struct stack_op *op;
2462 list_for_each_entry(op, &insn->stack_ops, list) {
2464 if (update_cfi_state(insn, &state->cfi, op))
2467 if (!insn->alt_group)
2470 if (op->dest.type == OP_DEST_PUSHF) {
2471 if (!state->uaccess_stack) {
2472 state->uaccess_stack = 1;
2473 } else if (state->uaccess_stack >> 31) {
2474 WARN_FUNC("PUSHF stack exhausted",
2475 insn->sec, insn->offset);
2478 state->uaccess_stack <<= 1;
2479 state->uaccess_stack |= state->uaccess;
2482 if (op->src.type == OP_SRC_POPF) {
2483 if (state->uaccess_stack) {
2484 state->uaccess = state->uaccess_stack & 1;
2485 state->uaccess_stack >>= 1;
2486 if (state->uaccess_stack == 1)
2487 state->uaccess_stack = 0;
2495 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2497 struct cfi_state *cfi1 = &insn->cfi;
2500 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2502 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2503 insn->sec, insn->offset,
2504 cfi1->cfa.base, cfi1->cfa.offset,
2505 cfi2->cfa.base, cfi2->cfa.offset);
2507 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2508 for (i = 0; i < CFI_NUM_REGS; i++) {
2509 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2510 sizeof(struct cfi_reg)))
2513 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2514 insn->sec, insn->offset,
2515 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2516 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2520 } else if (cfi1->type != cfi2->type) {
2522 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2523 insn->sec, insn->offset, cfi1->type, cfi2->type);
2525 } else if (cfi1->drap != cfi2->drap ||
2526 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2527 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2529 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2530 insn->sec, insn->offset,
2531 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2532 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2540 static inline bool func_uaccess_safe(struct symbol *func)
2543 return func->uaccess_safe;
2548 static inline const char *call_dest_name(struct instruction *insn)
2550 if (insn->call_dest)
2551 return insn->call_dest->name;
2556 static inline bool noinstr_call_dest(struct symbol *func)
2559 * We can't deal with indirect function calls at present;
2560 * assume they're instrumented.
2566 * If the symbol is from a noinstr section; we good.
2568 if (func->sec->noinstr)
2572 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2573 * something 'BAD' happened. At the risk of taking the machine down,
2574 * let them proceed to get the message out.
2576 if (!strncmp(func->name, "__ubsan_handle_", 15))
2582 static int validate_call(struct instruction *insn, struct insn_state *state)
2584 if (state->noinstr && state->instr <= 0 &&
2585 !noinstr_call_dest(insn->call_dest)) {
2586 WARN_FUNC("call to %s() leaves .noinstr.text section",
2587 insn->sec, insn->offset, call_dest_name(insn));
2591 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2592 WARN_FUNC("call to %s() with UACCESS enabled",
2593 insn->sec, insn->offset, call_dest_name(insn));
2598 WARN_FUNC("call to %s() with DF set",
2599 insn->sec, insn->offset, call_dest_name(insn));
2606 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2608 if (has_modified_stack_frame(insn, state)) {
2609 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2610 insn->sec, insn->offset);
2614 return validate_call(insn, state);
2617 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2619 if (state->noinstr && state->instr > 0) {
2620 WARN_FUNC("return with instrumentation enabled",
2621 insn->sec, insn->offset);
2625 if (state->uaccess && !func_uaccess_safe(func)) {
2626 WARN_FUNC("return with UACCESS enabled",
2627 insn->sec, insn->offset);
2631 if (!state->uaccess && func_uaccess_safe(func)) {
2632 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2633 insn->sec, insn->offset);
2638 WARN_FUNC("return with DF set",
2639 insn->sec, insn->offset);
2643 if (func && has_modified_stack_frame(insn, state)) {
2644 WARN_FUNC("return with modified stack frame",
2645 insn->sec, insn->offset);
2649 if (state->cfi.bp_scratch) {
2650 WARN_FUNC("BP used as a scratch register",
2651 insn->sec, insn->offset);
2658 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2659 struct instruction *insn)
2661 struct alt_group *alt_group = insn->alt_group;
2664 * Simulate the fact that alternatives are patched in-place. When the
2665 * end of a replacement alt_group is reached, redirect objtool flow to
2666 * the end of the original alt_group.
2668 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2669 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2671 return next_insn_same_sec(file, insn);
2675 * Follow the branch starting at the given instruction, and recursively follow
2676 * any other branches (jumps). Meanwhile, track the frame pointer state at
2677 * each instruction and validate all the rules described in
2678 * tools/objtool/Documentation/stack-validation.txt.
2680 static int validate_branch(struct objtool_file *file, struct symbol *func,
2681 struct instruction *insn, struct insn_state state)
2683 struct alternative *alt;
2684 struct instruction *next_insn;
2685 struct section *sec;
2692 next_insn = next_insn_to_validate(file, insn);
2694 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2695 WARN("%s() falls through to next function %s()",
2696 func->name, insn->func->name);
2700 if (func && insn->ignore) {
2701 WARN_FUNC("BUG: why am I validating an ignored function?",
2706 visited = 1 << state.uaccess;
2707 if (insn->visited) {
2708 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2711 if (insn->visited & visited)
2716 state.instr += insn->instr;
2719 state.cfi = insn->cfi;
2721 insn->cfi = state.cfi;
2723 insn->visited |= visited;
2725 if (propagate_alt_cfi(file, insn))
2728 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2729 bool skip_orig = false;
2731 list_for_each_entry(alt, &insn->alts, list) {
2735 ret = validate_branch(file, func, alt->insn, state);
2738 BT_FUNC("(alt)", insn);
2747 if (handle_insn_ops(insn, &state))
2750 switch (insn->type) {
2753 return validate_return(func, insn, &state);
2756 case INSN_CALL_DYNAMIC:
2757 ret = validate_call(insn, &state);
2761 if (!no_fp && func && !is_fentry_call(insn) &&
2762 !has_valid_stack_frame(&state)) {
2763 WARN_FUNC("call without frame pointer save/setup",
2768 if (dead_end_function(file, insn->call_dest))
2773 case INSN_JUMP_CONDITIONAL:
2774 case INSN_JUMP_UNCONDITIONAL:
2775 if (is_sibling_call(insn)) {
2776 ret = validate_sibling_call(insn, &state);
2780 } else if (insn->jump_dest) {
2781 ret = validate_branch(file, func,
2782 insn->jump_dest, state);
2785 BT_FUNC("(branch)", insn);
2790 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2795 case INSN_JUMP_DYNAMIC:
2796 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2797 if (is_sibling_call(insn)) {
2798 ret = validate_sibling_call(insn, &state);
2803 if (insn->type == INSN_JUMP_DYNAMIC)
2808 case INSN_CONTEXT_SWITCH:
2809 if (func && (!next_insn || !next_insn->hint)) {
2810 WARN_FUNC("unsupported instruction in callable function",
2817 if (state.uaccess) {
2818 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2822 state.uaccess = true;
2826 if (!state.uaccess && func) {
2827 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2831 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2832 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2836 state.uaccess = false;
2841 WARN_FUNC("recursive STD", sec, insn->offset);
2849 if (!state.df && func) {
2850 WARN_FUNC("redundant CLD", sec, insn->offset);
2865 if (state.cfi.cfa.base == CFI_UNDEFINED)
2867 WARN("%s: unexpected end of section", sec->name);
2877 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2879 struct instruction *insn;
2880 struct insn_state state;
2881 int ret, warnings = 0;
2886 init_insn_state(&state, sec);
2889 insn = find_insn(file, sec, 0);
2893 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2896 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2897 if (insn->hint && !insn->visited) {
2898 ret = validate_branch(file, insn->func, insn, state);
2899 if (ret && backtrace)
2900 BT_FUNC("<=== (hint)", insn);
2904 insn = list_next_entry(insn, list);
2910 static int validate_retpoline(struct objtool_file *file)
2912 struct instruction *insn;
2915 for_each_insn(file, insn) {
2916 if (insn->type != INSN_JUMP_DYNAMIC &&
2917 insn->type != INSN_CALL_DYNAMIC)
2920 if (insn->retpoline_safe)
2924 * .init.text code is ran before userspace and thus doesn't
2925 * strictly need retpolines, except for modules which are
2926 * loaded late, they very much do need retpoline in their
2929 if (!strcmp(insn->sec->name, ".init.text") && !module)
2932 WARN_FUNC("indirect %s found in RETPOLINE build",
2933 insn->sec, insn->offset,
2934 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2942 static bool is_kasan_insn(struct instruction *insn)
2944 return (insn->type == INSN_CALL &&
2945 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2948 static bool is_ubsan_insn(struct instruction *insn)
2950 return (insn->type == INSN_CALL &&
2951 !strcmp(insn->call_dest->name,
2952 "__ubsan_handle_builtin_unreachable"));
2955 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
2958 struct instruction *prev_insn;
2960 if (insn->ignore || insn->type == INSN_NOP)
2964 * Ignore any unused exceptions. This can happen when a whitelisted
2965 * function has an exception table entry.
2967 * Also ignore alternative replacement instructions. This can happen
2968 * when a whitelisted function uses one of the ALTERNATIVE macros.
2970 if (!strcmp(insn->sec->name, ".fixup") ||
2971 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2972 !strcmp(insn->sec->name, ".altinstr_aux"))
2979 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2980 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2981 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2982 * (or occasionally a JMP to UD2).
2984 * It may also insert a UD2 after calling a __noreturn function.
2986 prev_insn = list_prev_entry(insn, list);
2987 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
2988 (insn->type == INSN_BUG ||
2989 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2990 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2994 * Check if this (or a subsequent) instruction is related to
2995 * CONFIG_UBSAN or CONFIG_KASAN.
2997 * End the search at 5 instructions to avoid going into the weeds.
2999 for (i = 0; i < 5; i++) {
3001 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3004 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3005 if (insn->jump_dest &&
3006 insn->jump_dest->func == insn->func) {
3007 insn = insn->jump_dest;
3014 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3017 insn = list_next_entry(insn, list);
3023 static int validate_symbol(struct objtool_file *file, struct section *sec,
3024 struct symbol *sym, struct insn_state *state)
3026 struct instruction *insn;
3030 WARN("%s() is missing an ELF size annotation", sym->name);
3034 if (sym->pfunc != sym || sym->alias != sym)
3037 insn = find_insn(file, sec, sym->offset);
3038 if (!insn || insn->ignore || insn->visited)
3041 state->uaccess = sym->uaccess_safe;
3043 ret = validate_branch(file, insn->func, insn, *state);
3044 if (ret && backtrace)
3045 BT_FUNC("<=== (sym)", insn);
3049 static int validate_section(struct objtool_file *file, struct section *sec)
3051 struct insn_state state;
3052 struct symbol *func;
3055 list_for_each_entry(func, &sec->symbol_list, list) {
3056 if (func->type != STT_FUNC)
3059 init_insn_state(&state, sec);
3060 set_func_state(&state.cfi);
3062 warnings += validate_symbol(file, sec, func, &state);
3068 static int validate_vmlinux_functions(struct objtool_file *file)
3070 struct section *sec;
3073 sec = find_section_by_name(file->elf, ".noinstr.text");
3075 warnings += validate_section(file, sec);
3076 warnings += validate_unwind_hints(file, sec);
3079 sec = find_section_by_name(file->elf, ".entry.text");
3081 warnings += validate_section(file, sec);
3082 warnings += validate_unwind_hints(file, sec);
3088 static int validate_functions(struct objtool_file *file)
3090 struct section *sec;
3093 for_each_sec(file, sec) {
3094 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3097 warnings += validate_section(file, sec);
3103 static int validate_reachable_instructions(struct objtool_file *file)
3105 struct instruction *insn;
3107 if (file->ignore_unreachables)
3110 for_each_insn(file, insn) {
3111 if (insn->visited || ignore_unreachable_insn(file, insn))
3114 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3121 int check(struct objtool_file *file)
3123 int ret, warnings = 0;
3125 arch_initial_func_cfi_state(&initial_func_cfi);
3127 ret = decode_sections(file);
3132 if (list_empty(&file->insn_list))
3135 if (vmlinux && !validate_dup) {
3136 ret = validate_vmlinux_functions(file);
3145 ret = validate_retpoline(file);
3151 ret = validate_functions(file);
3156 ret = validate_unwind_hints(file, NULL);
3162 ret = validate_reachable_instructions(file);
3168 ret = create_static_call_sections(file);
3174 ret = create_mcount_loc_sections(file);
3182 * For now, don't fail the kernel build on fatal warnings. These
3183 * errors are still fairly common due to the growing matrix of
3184 * supported toolchains and their recent pace of change.