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);
545 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
551 static int create_mcount_loc_sections(struct objtool_file *file)
553 struct section *sec, *reloc_sec;
556 struct instruction *insn;
559 sec = find_section_by_name(file->elf, "__mcount_loc");
561 INIT_LIST_HEAD(&file->mcount_loc_list);
562 WARN("file already has __mcount_loc section, skipping");
566 if (list_empty(&file->mcount_loc_list))
570 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
573 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
577 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
582 list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
584 loc = (unsigned long *)sec->data->d_buf + idx;
585 memset(loc, 0, sizeof(unsigned long));
587 reloc = malloc(sizeof(*reloc));
592 memset(reloc, 0, sizeof(*reloc));
594 if (insn->sec->sym) {
595 reloc->sym = insn->sec->sym;
596 reloc->addend = insn->offset;
598 reloc->sym = find_symbol_containing(insn->sec, insn->offset);
601 WARN("missing symbol for insn at offset 0x%lx\n",
606 reloc->addend = insn->offset - reloc->sym->offset;
609 reloc->type = R_X86_64_64;
610 reloc->offset = idx * sizeof(unsigned long);
611 reloc->sec = reloc_sec;
612 elf_add_reloc(file->elf, reloc);
617 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
624 * Warnings shouldn't be reported for ignored functions.
626 static void add_ignores(struct objtool_file *file)
628 struct instruction *insn;
633 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
637 list_for_each_entry(reloc, &sec->reloc_list, list) {
638 switch (reloc->sym->type) {
644 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
650 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
654 func_for_each_insn(file, func, insn)
660 * This is a whitelist of functions that is allowed to be called with AC set.
661 * The list is meant to be minimal and only contains compiler instrumentation
662 * ABI and a few functions used to implement *_{to,from}_user() functions.
664 * These functions must not directly change AC, but may PUSHF/POPF.
666 static const char *uaccess_safe_builtin[] = {
670 /* KASAN out-of-line */
671 "__asan_loadN_noabort",
672 "__asan_load1_noabort",
673 "__asan_load2_noabort",
674 "__asan_load4_noabort",
675 "__asan_load8_noabort",
676 "__asan_load16_noabort",
677 "__asan_storeN_noabort",
678 "__asan_store1_noabort",
679 "__asan_store2_noabort",
680 "__asan_store4_noabort",
681 "__asan_store8_noabort",
682 "__asan_store16_noabort",
683 "__kasan_check_read",
684 "__kasan_check_write",
686 "__asan_report_load_n_noabort",
687 "__asan_report_load1_noabort",
688 "__asan_report_load2_noabort",
689 "__asan_report_load4_noabort",
690 "__asan_report_load8_noabort",
691 "__asan_report_load16_noabort",
692 "__asan_report_store_n_noabort",
693 "__asan_report_store1_noabort",
694 "__asan_report_store2_noabort",
695 "__asan_report_store4_noabort",
696 "__asan_report_store8_noabort",
697 "__asan_report_store16_noabort",
699 "__kcsan_check_access",
700 "kcsan_found_watchpoint",
701 "kcsan_setup_watchpoint",
702 "kcsan_check_scoped_accesses",
703 "kcsan_disable_current",
704 "kcsan_enable_current_nowarn",
709 "__tsan_write_range",
720 "__tsan_read_write1",
721 "__tsan_read_write2",
722 "__tsan_read_write4",
723 "__tsan_read_write8",
724 "__tsan_read_write16",
725 "__tsan_atomic8_load",
726 "__tsan_atomic16_load",
727 "__tsan_atomic32_load",
728 "__tsan_atomic64_load",
729 "__tsan_atomic8_store",
730 "__tsan_atomic16_store",
731 "__tsan_atomic32_store",
732 "__tsan_atomic64_store",
733 "__tsan_atomic8_exchange",
734 "__tsan_atomic16_exchange",
735 "__tsan_atomic32_exchange",
736 "__tsan_atomic64_exchange",
737 "__tsan_atomic8_fetch_add",
738 "__tsan_atomic16_fetch_add",
739 "__tsan_atomic32_fetch_add",
740 "__tsan_atomic64_fetch_add",
741 "__tsan_atomic8_fetch_sub",
742 "__tsan_atomic16_fetch_sub",
743 "__tsan_atomic32_fetch_sub",
744 "__tsan_atomic64_fetch_sub",
745 "__tsan_atomic8_fetch_and",
746 "__tsan_atomic16_fetch_and",
747 "__tsan_atomic32_fetch_and",
748 "__tsan_atomic64_fetch_and",
749 "__tsan_atomic8_fetch_or",
750 "__tsan_atomic16_fetch_or",
751 "__tsan_atomic32_fetch_or",
752 "__tsan_atomic64_fetch_or",
753 "__tsan_atomic8_fetch_xor",
754 "__tsan_atomic16_fetch_xor",
755 "__tsan_atomic32_fetch_xor",
756 "__tsan_atomic64_fetch_xor",
757 "__tsan_atomic8_fetch_nand",
758 "__tsan_atomic16_fetch_nand",
759 "__tsan_atomic32_fetch_nand",
760 "__tsan_atomic64_fetch_nand",
761 "__tsan_atomic8_compare_exchange_strong",
762 "__tsan_atomic16_compare_exchange_strong",
763 "__tsan_atomic32_compare_exchange_strong",
764 "__tsan_atomic64_compare_exchange_strong",
765 "__tsan_atomic8_compare_exchange_weak",
766 "__tsan_atomic16_compare_exchange_weak",
767 "__tsan_atomic32_compare_exchange_weak",
768 "__tsan_atomic64_compare_exchange_weak",
769 "__tsan_atomic8_compare_exchange_val",
770 "__tsan_atomic16_compare_exchange_val",
771 "__tsan_atomic32_compare_exchange_val",
772 "__tsan_atomic64_compare_exchange_val",
773 "__tsan_atomic_thread_fence",
774 "__tsan_atomic_signal_fence",
778 "__sanitizer_cov_trace_pc",
779 "__sanitizer_cov_trace_const_cmp1",
780 "__sanitizer_cov_trace_const_cmp2",
781 "__sanitizer_cov_trace_const_cmp4",
782 "__sanitizer_cov_trace_const_cmp8",
783 "__sanitizer_cov_trace_cmp1",
784 "__sanitizer_cov_trace_cmp2",
785 "__sanitizer_cov_trace_cmp4",
786 "__sanitizer_cov_trace_cmp8",
787 "__sanitizer_cov_trace_switch",
789 "ubsan_type_mismatch_common",
790 "__ubsan_handle_type_mismatch",
791 "__ubsan_handle_type_mismatch_v1",
792 "__ubsan_handle_shift_out_of_bounds",
794 "csum_partial_copy_generic",
796 "copy_mc_fragile_handle_tail",
797 "copy_mc_enhanced_fast_string",
798 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
802 static void add_uaccess_safe(struct objtool_file *file)
810 for (name = uaccess_safe_builtin; *name; name++) {
811 func = find_symbol_by_name(file->elf, *name);
815 func->uaccess_safe = true;
820 * FIXME: For now, just ignore any alternatives which add retpolines. This is
821 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
822 * But it at least allows objtool to understand the control flow *around* the
825 static int add_ignore_alternatives(struct objtool_file *file)
829 struct instruction *insn;
831 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
835 list_for_each_entry(reloc, &sec->reloc_list, list) {
836 if (reloc->sym->type != STT_SECTION) {
837 WARN("unexpected relocation symbol type in %s", sec->name);
841 insn = find_insn(file, reloc->sym->sec, reloc->addend);
843 WARN("bad .discard.ignore_alts entry");
847 insn->ignore_alts = true;
854 * Find the destination instructions for all jumps.
856 static int add_jump_destinations(struct objtool_file *file)
858 struct instruction *insn;
860 struct section *dest_sec;
861 unsigned long dest_off;
863 for_each_insn(file, insn) {
864 if (!is_static_jump(insn))
867 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
868 insn->offset, insn->len);
870 dest_sec = insn->sec;
871 dest_off = arch_jump_destination(insn);
872 } else if (reloc->sym->type == STT_SECTION) {
873 dest_sec = reloc->sym->sec;
874 dest_off = arch_dest_reloc_offset(reloc->addend);
875 } else if (!strncmp(reloc->sym->name, "__x86_indirect_thunk_", 21) ||
876 !strncmp(reloc->sym->name, "__x86_retpoline_", 16)) {
878 * Retpoline jumps are really dynamic jumps in
879 * disguise, so convert them accordingly.
881 if (insn->type == INSN_JUMP_UNCONDITIONAL)
882 insn->type = INSN_JUMP_DYNAMIC;
884 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
886 insn->retpoline_safe = true;
888 } else if (insn->func) {
889 /* internal or external sibling call (with reloc) */
890 insn->call_dest = reloc->sym;
891 if (insn->call_dest->static_call_tramp) {
892 list_add_tail(&insn->static_call_node,
893 &file->static_call_list);
896 } else if (reloc->sym->sec->idx) {
897 dest_sec = reloc->sym->sec;
898 dest_off = reloc->sym->sym.st_value +
899 arch_dest_reloc_offset(reloc->addend);
901 /* non-func asm code jumping to another file */
905 insn->jump_dest = find_insn(file, dest_sec, dest_off);
906 if (!insn->jump_dest) {
909 * This is a special case where an alt instruction
910 * jumps past the end of the section. These are
911 * handled later in handle_group_alt().
913 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
916 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
917 insn->sec, insn->offset, dest_sec->name,
923 * Cross-function jump.
925 if (insn->func && insn->jump_dest->func &&
926 insn->func != insn->jump_dest->func) {
929 * For GCC 8+, create parent/child links for any cold
930 * subfunctions. This is _mostly_ redundant with a
931 * similar initialization in read_symbols().
933 * If a function has aliases, we want the *first* such
934 * function in the symbol table to be the subfunction's
935 * parent. In that case we overwrite the
936 * initialization done in read_symbols().
938 * However this code can't completely replace the
939 * read_symbols() code because this doesn't detect the
940 * case where the parent function's only reference to a
941 * subfunction is through a jump table.
943 if (!strstr(insn->func->name, ".cold") &&
944 strstr(insn->jump_dest->func->name, ".cold")) {
945 insn->func->cfunc = insn->jump_dest->func;
946 insn->jump_dest->func->pfunc = insn->func;
948 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
949 insn->jump_dest->offset == insn->jump_dest->func->offset) {
951 /* internal sibling call (without reloc) */
952 insn->call_dest = insn->jump_dest->func;
953 if (insn->call_dest->static_call_tramp) {
954 list_add_tail(&insn->static_call_node,
955 &file->static_call_list);
964 static void remove_insn_ops(struct instruction *insn)
966 struct stack_op *op, *tmp;
968 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
974 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
976 struct symbol *call_dest;
978 call_dest = find_func_by_offset(sec, offset);
980 call_dest = find_symbol_by_offset(sec, offset);
986 * Find the destination instructions for all calls.
988 static int add_call_destinations(struct objtool_file *file)
990 struct instruction *insn;
991 unsigned long dest_off;
994 for_each_insn(file, insn) {
995 if (insn->type != INSN_CALL)
998 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
999 insn->offset, insn->len);
1001 dest_off = arch_jump_destination(insn);
1002 insn->call_dest = find_call_destination(insn->sec, dest_off);
1007 if (!insn->call_dest) {
1008 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1012 if (insn->func && insn->call_dest->type != STT_FUNC) {
1013 WARN_FUNC("unsupported call to non-function",
1014 insn->sec, insn->offset);
1018 } else if (reloc->sym->type == STT_SECTION) {
1019 dest_off = arch_dest_reloc_offset(reloc->addend);
1020 insn->call_dest = find_call_destination(reloc->sym->sec,
1022 if (!insn->call_dest) {
1023 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1024 insn->sec, insn->offset,
1025 reloc->sym->sec->name,
1030 insn->call_dest = reloc->sym;
1033 * Many compilers cannot disable KCOV with a function attribute
1034 * so they need a little help, NOP out any KCOV calls from noinstr
1037 if (insn->sec->noinstr &&
1038 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
1040 reloc->type = R_NONE;
1041 elf_write_reloc(file->elf, reloc);
1044 elf_write_insn(file->elf, insn->sec,
1045 insn->offset, insn->len,
1046 arch_nop_insn(insn->len));
1047 insn->type = INSN_NOP;
1050 if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1052 reloc->type = R_NONE;
1053 elf_write_reloc(file->elf, reloc);
1056 elf_write_insn(file->elf, insn->sec,
1057 insn->offset, insn->len,
1058 arch_nop_insn(insn->len));
1060 insn->type = INSN_NOP;
1062 list_add_tail(&insn->mcount_loc_node,
1063 &file->mcount_loc_list);
1067 * Whatever stack impact regular CALLs have, should be undone
1068 * by the RETURN of the called function.
1070 * Annotated intra-function calls retain the stack_ops but
1071 * are converted to JUMP, see read_intra_function_calls().
1073 remove_insn_ops(insn);
1080 * The .alternatives section requires some extra special care over and above
1081 * other special sections because alternatives are patched in place.
1083 static int handle_group_alt(struct objtool_file *file,
1084 struct special_alt *special_alt,
1085 struct instruction *orig_insn,
1086 struct instruction **new_insn)
1088 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1089 struct alt_group *orig_alt_group, *new_alt_group;
1090 unsigned long dest_off;
1093 orig_alt_group = malloc(sizeof(*orig_alt_group));
1094 if (!orig_alt_group) {
1095 WARN("malloc failed");
1098 orig_alt_group->cfi = calloc(special_alt->orig_len,
1099 sizeof(struct cfi_state *));
1100 if (!orig_alt_group->cfi) {
1101 WARN("calloc failed");
1105 last_orig_insn = NULL;
1107 sec_for_each_insn_from(file, insn) {
1108 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1111 insn->alt_group = orig_alt_group;
1112 last_orig_insn = insn;
1114 orig_alt_group->orig_group = NULL;
1115 orig_alt_group->first_insn = orig_insn;
1116 orig_alt_group->last_insn = last_orig_insn;
1119 new_alt_group = malloc(sizeof(*new_alt_group));
1120 if (!new_alt_group) {
1121 WARN("malloc failed");
1125 if (special_alt->new_len < special_alt->orig_len) {
1127 * Insert a fake nop at the end to make the replacement
1128 * alt_group the same size as the original. This is needed to
1129 * allow propagate_alt_cfi() to do its magic. When the last
1130 * instruction affects the stack, the instruction after it (the
1131 * nop) will propagate the new state to the shared CFI array.
1133 nop = malloc(sizeof(*nop));
1135 WARN("malloc failed");
1138 memset(nop, 0, sizeof(*nop));
1139 INIT_LIST_HEAD(&nop->alts);
1140 INIT_LIST_HEAD(&nop->stack_ops);
1141 init_cfi_state(&nop->cfi);
1143 nop->sec = special_alt->new_sec;
1144 nop->offset = special_alt->new_off + special_alt->new_len;
1145 nop->len = special_alt->orig_len - special_alt->new_len;
1146 nop->type = INSN_NOP;
1147 nop->func = orig_insn->func;
1148 nop->alt_group = new_alt_group;
1149 nop->ignore = orig_insn->ignore_alts;
1152 if (!special_alt->new_len) {
1158 sec_for_each_insn_from(file, insn) {
1159 struct reloc *alt_reloc;
1161 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1164 last_new_insn = insn;
1166 insn->ignore = orig_insn->ignore_alts;
1167 insn->func = orig_insn->func;
1168 insn->alt_group = new_alt_group;
1171 * Since alternative replacement code is copy/pasted by the
1172 * kernel after applying relocations, generally such code can't
1173 * have relative-address relocation references to outside the
1174 * .altinstr_replacement section, unless the arch's
1175 * alternatives code can adjust the relative offsets
1178 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1179 insn->offset, insn->len);
1181 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1183 WARN_FUNC("unsupported relocation in alternatives section",
1184 insn->sec, insn->offset);
1188 if (!is_static_jump(insn))
1191 if (!insn->immediate)
1194 dest_off = arch_jump_destination(insn);
1195 if (dest_off == special_alt->new_off + special_alt->new_len)
1196 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1198 if (!insn->jump_dest) {
1199 WARN_FUNC("can't find alternative jump destination",
1200 insn->sec, insn->offset);
1205 if (!last_new_insn) {
1206 WARN_FUNC("can't find last new alternative instruction",
1207 special_alt->new_sec, special_alt->new_off);
1212 list_add(&nop->list, &last_new_insn->list);
1214 new_alt_group->orig_group = orig_alt_group;
1215 new_alt_group->first_insn = *new_insn;
1216 new_alt_group->last_insn = nop ? : last_new_insn;
1217 new_alt_group->cfi = orig_alt_group->cfi;
1222 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1223 * If the original instruction is a jump, make the alt entry an effective nop
1224 * by just skipping the original instruction.
1226 static int handle_jump_alt(struct objtool_file *file,
1227 struct special_alt *special_alt,
1228 struct instruction *orig_insn,
1229 struct instruction **new_insn)
1231 if (orig_insn->type == INSN_NOP)
1234 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1235 WARN_FUNC("unsupported instruction at jump label",
1236 orig_insn->sec, orig_insn->offset);
1240 *new_insn = list_next_entry(orig_insn, list);
1245 * Read all the special sections which have alternate instructions which can be
1246 * patched in or redirected to at runtime. Each instruction having alternate
1247 * instruction(s) has them added to its insn->alts list, which will be
1248 * traversed in validate_branch().
1250 static int add_special_section_alts(struct objtool_file *file)
1252 struct list_head special_alts;
1253 struct instruction *orig_insn, *new_insn;
1254 struct special_alt *special_alt, *tmp;
1255 struct alternative *alt;
1258 ret = special_get_alts(file->elf, &special_alts);
1262 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1264 orig_insn = find_insn(file, special_alt->orig_sec,
1265 special_alt->orig_off);
1267 WARN_FUNC("special: can't find orig instruction",
1268 special_alt->orig_sec, special_alt->orig_off);
1274 if (!special_alt->group || special_alt->new_len) {
1275 new_insn = find_insn(file, special_alt->new_sec,
1276 special_alt->new_off);
1278 WARN_FUNC("special: can't find new instruction",
1279 special_alt->new_sec,
1280 special_alt->new_off);
1286 if (special_alt->group) {
1287 if (!special_alt->orig_len) {
1288 WARN_FUNC("empty alternative entry",
1289 orig_insn->sec, orig_insn->offset);
1293 ret = handle_group_alt(file, special_alt, orig_insn,
1297 } else if (special_alt->jump_or_nop) {
1298 ret = handle_jump_alt(file, special_alt, orig_insn,
1304 alt = malloc(sizeof(*alt));
1306 WARN("malloc failed");
1311 alt->insn = new_insn;
1312 alt->skip_orig = special_alt->skip_orig;
1313 orig_insn->ignore_alts |= special_alt->skip_alt;
1314 list_add_tail(&alt->list, &orig_insn->alts);
1316 list_del(&special_alt->list);
1324 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1325 struct reloc *table)
1327 struct reloc *reloc = table;
1328 struct instruction *dest_insn;
1329 struct alternative *alt;
1330 struct symbol *pfunc = insn->func->pfunc;
1331 unsigned int prev_offset = 0;
1334 * Each @reloc is a switch table relocation which points to the target
1337 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1339 /* Check for the end of the table: */
1340 if (reloc != table && reloc->jump_table_start)
1343 /* Make sure the table entries are consecutive: */
1344 if (prev_offset && reloc->offset != prev_offset + 8)
1347 /* Detect function pointers from contiguous objects: */
1348 if (reloc->sym->sec == pfunc->sec &&
1349 reloc->addend == pfunc->offset)
1352 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1356 /* Make sure the destination is in the same function: */
1357 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1360 alt = malloc(sizeof(*alt));
1362 WARN("malloc failed");
1366 alt->insn = dest_insn;
1367 list_add_tail(&alt->list, &insn->alts);
1368 prev_offset = reloc->offset;
1372 WARN_FUNC("can't find switch jump table",
1373 insn->sec, insn->offset);
1381 * find_jump_table() - Given a dynamic jump, find the switch jump table
1382 * associated with it.
1384 static struct reloc *find_jump_table(struct objtool_file *file,
1385 struct symbol *func,
1386 struct instruction *insn)
1388 struct reloc *table_reloc;
1389 struct instruction *dest_insn, *orig_insn = insn;
1392 * Backward search using the @first_jump_src links, these help avoid
1393 * much of the 'in between' code. Which avoids us getting confused by
1397 insn && insn->func && insn->func->pfunc == func;
1398 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1400 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1403 /* allow small jumps within the range */
1404 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1406 (insn->jump_dest->offset <= insn->offset ||
1407 insn->jump_dest->offset > orig_insn->offset))
1410 table_reloc = arch_find_switch_table(file, insn);
1413 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1414 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1424 * First pass: Mark the head of each jump table so that in the next pass,
1425 * we know when a given jump table ends and the next one starts.
1427 static void mark_func_jump_tables(struct objtool_file *file,
1428 struct symbol *func)
1430 struct instruction *insn, *last = NULL;
1431 struct reloc *reloc;
1433 func_for_each_insn(file, func, insn) {
1438 * Store back-pointers for unconditional forward jumps such
1439 * that find_jump_table() can back-track using those and
1440 * avoid some potentially confusing code.
1442 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1443 insn->offset > last->offset &&
1444 insn->jump_dest->offset > insn->offset &&
1445 !insn->jump_dest->first_jump_src) {
1447 insn->jump_dest->first_jump_src = insn;
1448 last = insn->jump_dest;
1451 if (insn->type != INSN_JUMP_DYNAMIC)
1454 reloc = find_jump_table(file, func, insn);
1456 reloc->jump_table_start = true;
1457 insn->jump_table = reloc;
1462 static int add_func_jump_tables(struct objtool_file *file,
1463 struct symbol *func)
1465 struct instruction *insn;
1468 func_for_each_insn(file, func, insn) {
1469 if (!insn->jump_table)
1472 ret = add_jump_table(file, insn, insn->jump_table);
1481 * For some switch statements, gcc generates a jump table in the .rodata
1482 * section which contains a list of addresses within the function to jump to.
1483 * This finds these jump tables and adds them to the insn->alts lists.
1485 static int add_jump_table_alts(struct objtool_file *file)
1487 struct section *sec;
1488 struct symbol *func;
1494 for_each_sec(file, sec) {
1495 list_for_each_entry(func, &sec->symbol_list, list) {
1496 if (func->type != STT_FUNC)
1499 mark_func_jump_tables(file, func);
1500 ret = add_func_jump_tables(file, func);
1509 static void set_func_state(struct cfi_state *state)
1511 state->cfa = initial_func_cfi.cfa;
1512 memcpy(&state->regs, &initial_func_cfi.regs,
1513 CFI_NUM_REGS * sizeof(struct cfi_reg));
1514 state->stack_size = initial_func_cfi.cfa.offset;
1517 static int read_unwind_hints(struct objtool_file *file)
1519 struct section *sec, *relocsec;
1520 struct reloc *reloc;
1521 struct unwind_hint *hint;
1522 struct instruction *insn;
1525 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1529 relocsec = sec->reloc;
1531 WARN("missing .rela.discard.unwind_hints section");
1535 if (sec->len % sizeof(struct unwind_hint)) {
1536 WARN("struct unwind_hint size mismatch");
1542 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1543 hint = (struct unwind_hint *)sec->data->d_buf + i;
1545 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1547 WARN("can't find reloc for unwind_hints[%d]", i);
1551 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1553 WARN("can't find insn for unwind_hints[%d]", i);
1559 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1560 set_func_state(&insn->cfi);
1564 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1565 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1566 insn->sec, insn->offset, hint->sp_reg);
1570 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1571 insn->cfi.type = hint->type;
1572 insn->cfi.end = hint->end;
1578 static int read_retpoline_hints(struct objtool_file *file)
1580 struct section *sec;
1581 struct instruction *insn;
1582 struct reloc *reloc;
1584 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1588 list_for_each_entry(reloc, &sec->reloc_list, list) {
1589 if (reloc->sym->type != STT_SECTION) {
1590 WARN("unexpected relocation symbol type in %s", sec->name);
1594 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1596 WARN("bad .discard.retpoline_safe entry");
1600 if (insn->type != INSN_JUMP_DYNAMIC &&
1601 insn->type != INSN_CALL_DYNAMIC) {
1602 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1603 insn->sec, insn->offset);
1607 insn->retpoline_safe = true;
1613 static int read_instr_hints(struct objtool_file *file)
1615 struct section *sec;
1616 struct instruction *insn;
1617 struct reloc *reloc;
1619 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1623 list_for_each_entry(reloc, &sec->reloc_list, list) {
1624 if (reloc->sym->type != STT_SECTION) {
1625 WARN("unexpected relocation symbol type in %s", sec->name);
1629 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1631 WARN("bad .discard.instr_end entry");
1638 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1642 list_for_each_entry(reloc, &sec->reloc_list, list) {
1643 if (reloc->sym->type != STT_SECTION) {
1644 WARN("unexpected relocation symbol type in %s", sec->name);
1648 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1650 WARN("bad .discard.instr_begin entry");
1660 static int read_intra_function_calls(struct objtool_file *file)
1662 struct instruction *insn;
1663 struct section *sec;
1664 struct reloc *reloc;
1666 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1670 list_for_each_entry(reloc, &sec->reloc_list, list) {
1671 unsigned long dest_off;
1673 if (reloc->sym->type != STT_SECTION) {
1674 WARN("unexpected relocation symbol type in %s",
1679 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1681 WARN("bad .discard.intra_function_call entry");
1685 if (insn->type != INSN_CALL) {
1686 WARN_FUNC("intra_function_call not a direct call",
1687 insn->sec, insn->offset);
1692 * Treat intra-function CALLs as JMPs, but with a stack_op.
1693 * See add_call_destinations(), which strips stack_ops from
1696 insn->type = INSN_JUMP_UNCONDITIONAL;
1698 dest_off = insn->offset + insn->len + insn->immediate;
1699 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1700 if (!insn->jump_dest) {
1701 WARN_FUNC("can't find call dest at %s+0x%lx",
1702 insn->sec, insn->offset,
1703 insn->sec->name, dest_off);
1711 static int read_static_call_tramps(struct objtool_file *file)
1713 struct section *sec;
1714 struct symbol *func;
1716 for_each_sec(file, sec) {
1717 list_for_each_entry(func, &sec->symbol_list, list) {
1718 if (func->bind == STB_GLOBAL &&
1719 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1720 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1721 func->static_call_tramp = true;
1728 static void mark_rodata(struct objtool_file *file)
1730 struct section *sec;
1734 * Search for the following rodata sections, each of which can
1735 * potentially contain jump tables:
1737 * - .rodata: can contain GCC switch tables
1738 * - .rodata.<func>: same, if -fdata-sections is being used
1739 * - .rodata..c_jump_table: contains C annotated jump tables
1741 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1743 for_each_sec(file, sec) {
1744 if (!strncmp(sec->name, ".rodata", 7) &&
1745 !strstr(sec->name, ".str1.")) {
1751 file->rodata = found;
1754 static int decode_sections(struct objtool_file *file)
1760 ret = decode_instructions(file);
1764 ret = add_dead_ends(file);
1769 add_uaccess_safe(file);
1771 ret = add_ignore_alternatives(file);
1775 ret = read_static_call_tramps(file);
1779 ret = add_jump_destinations(file);
1783 ret = add_special_section_alts(file);
1787 ret = read_intra_function_calls(file);
1791 ret = add_call_destinations(file);
1795 ret = add_jump_table_alts(file);
1799 ret = read_unwind_hints(file);
1803 ret = read_retpoline_hints(file);
1807 ret = read_instr_hints(file);
1814 static bool is_fentry_call(struct instruction *insn)
1816 if (insn->type == INSN_CALL && insn->call_dest &&
1817 insn->call_dest->type == STT_NOTYPE &&
1818 !strcmp(insn->call_dest->name, "__fentry__"))
1824 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1826 struct cfi_state *cfi = &state->cfi;
1829 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1832 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1835 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1838 for (i = 0; i < CFI_NUM_REGS; i++) {
1839 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1840 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1847 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1848 int expected_offset)
1850 return reg->base == CFI_CFA &&
1851 reg->offset == expected_offset;
1854 static bool has_valid_stack_frame(struct insn_state *state)
1856 struct cfi_state *cfi = &state->cfi;
1858 if (cfi->cfa.base == CFI_BP &&
1859 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1860 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1863 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1869 static int update_cfi_state_regs(struct instruction *insn,
1870 struct cfi_state *cfi,
1871 struct stack_op *op)
1873 struct cfi_reg *cfa = &cfi->cfa;
1875 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1879 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1883 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1886 /* add immediate to sp */
1887 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1888 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1889 cfa->offset -= op->src.offset;
1894 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1896 if (arch_callee_saved_reg(reg) &&
1897 cfi->regs[reg].base == CFI_UNDEFINED) {
1898 cfi->regs[reg].base = base;
1899 cfi->regs[reg].offset = offset;
1903 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1905 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1906 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1910 * A note about DRAP stack alignment:
1912 * GCC has the concept of a DRAP register, which is used to help keep track of
1913 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1914 * register. The typical DRAP pattern is:
1916 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1917 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1918 * 41 ff 72 f8 pushq -0x8(%r10)
1920 * 48 89 e5 mov %rsp,%rbp
1927 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1930 * There are some variations in the epilogues, like:
1938 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1943 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1944 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1945 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1946 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1948 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1951 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1952 * restored beforehand:
1955 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1956 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1958 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1962 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1963 struct stack_op *op)
1965 struct cfi_reg *cfa = &cfi->cfa;
1966 struct cfi_reg *regs = cfi->regs;
1968 /* stack operations don't make sense with an undefined CFA */
1969 if (cfa->base == CFI_UNDEFINED) {
1971 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1977 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1978 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
1979 return update_cfi_state_regs(insn, cfi, op);
1981 switch (op->dest.type) {
1984 switch (op->src.type) {
1987 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1988 cfa->base == CFI_SP &&
1989 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
1991 /* mov %rsp, %rbp */
1992 cfa->base = op->dest.reg;
1993 cfi->bp_scratch = false;
1996 else if (op->src.reg == CFI_SP &&
1997 op->dest.reg == CFI_BP && cfi->drap) {
1999 /* drap: mov %rsp, %rbp */
2000 regs[CFI_BP].base = CFI_BP;
2001 regs[CFI_BP].offset = -cfi->stack_size;
2002 cfi->bp_scratch = false;
2005 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2010 * This is needed for the rare case where GCC
2017 cfi->vals[op->dest.reg].base = CFI_CFA;
2018 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2021 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2022 cfa->base == CFI_BP) {
2027 * Restore the original stack pointer (Clang).
2029 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2032 else if (op->dest.reg == cfa->base) {
2034 /* mov %reg, %rsp */
2035 if (cfa->base == CFI_SP &&
2036 cfi->vals[op->src.reg].base == CFI_CFA) {
2039 * This is needed for the rare case
2040 * where GCC does something dumb like:
2042 * lea 0x8(%rsp), %rcx
2046 cfa->offset = -cfi->vals[op->src.reg].offset;
2047 cfi->stack_size = cfa->offset;
2049 } else if (cfa->base == CFI_SP &&
2050 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2051 cfi->vals[op->src.reg].offset == cfa->offset) {
2056 * 1: mov %rsp, (%[tos])
2057 * 2: mov %[tos], %rsp
2063 * 1 - places a pointer to the previous
2064 * stack at the Top-of-Stack of the
2067 * 2 - switches to the new stack.
2069 * 3 - pops the Top-of-Stack to restore
2070 * the original stack.
2072 * Note: we set base to SP_INDIRECT
2073 * here and preserve offset. Therefore
2074 * when the unwinder reaches ToS it
2075 * will dereference SP and then add the
2076 * offset to find the next frame, IOW:
2079 cfa->base = CFI_SP_INDIRECT;
2082 cfa->base = CFI_UNDEFINED;
2087 else if (op->dest.reg == CFI_SP &&
2088 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2089 cfi->vals[op->src.reg].offset == cfa->offset) {
2092 * The same stack swizzle case 2) as above. But
2093 * because we can't change cfa->base, case 3)
2094 * will become a regular POP. Pretend we're a
2095 * PUSH so things don't go unbalanced.
2097 cfi->stack_size += 8;
2104 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2107 cfi->stack_size -= op->src.offset;
2108 if (cfa->base == CFI_SP)
2109 cfa->offset -= op->src.offset;
2113 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2115 /* lea disp(%rbp), %rsp */
2116 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2120 if (!cfi->drap && op->src.reg == CFI_SP &&
2121 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2122 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2124 /* lea disp(%rsp), %rbp */
2126 cfa->offset -= op->src.offset;
2127 cfi->bp_scratch = false;
2131 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2133 /* drap: lea disp(%rsp), %drap */
2134 cfi->drap_reg = op->dest.reg;
2137 * lea disp(%rsp), %reg
2139 * This is needed for the rare case where GCC
2140 * does something dumb like:
2142 * lea 0x8(%rsp), %rcx
2146 cfi->vals[op->dest.reg].base = CFI_CFA;
2147 cfi->vals[op->dest.reg].offset = \
2148 -cfi->stack_size + op->src.offset;
2153 if (cfi->drap && op->dest.reg == CFI_SP &&
2154 op->src.reg == cfi->drap_reg) {
2156 /* drap: lea disp(%drap), %rsp */
2158 cfa->offset = cfi->stack_size = -op->src.offset;
2159 cfi->drap_reg = CFI_UNDEFINED;
2164 if (op->dest.reg == cfi->cfa.base) {
2165 WARN_FUNC("unsupported stack register modification",
2166 insn->sec, insn->offset);
2173 if (op->dest.reg != CFI_SP ||
2174 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2175 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2176 WARN_FUNC("unsupported stack pointer realignment",
2177 insn->sec, insn->offset);
2181 if (cfi->drap_reg != CFI_UNDEFINED) {
2182 /* drap: and imm, %rsp */
2183 cfa->base = cfi->drap_reg;
2184 cfa->offset = cfi->stack_size = 0;
2189 * Older versions of GCC (4.8ish) realign the stack
2190 * without DRAP, with a frame pointer.
2197 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2199 /* pop %rsp; # restore from a stack swizzle */
2204 if (!cfi->drap && op->dest.reg == cfa->base) {
2210 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2211 op->dest.reg == cfi->drap_reg &&
2212 cfi->drap_offset == -cfi->stack_size) {
2214 /* drap: pop %drap */
2215 cfa->base = cfi->drap_reg;
2217 cfi->drap_offset = -1;
2219 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2222 restore_reg(cfi, op->dest.reg);
2225 cfi->stack_size -= 8;
2226 if (cfa->base == CFI_SP)
2231 case OP_SRC_REG_INDIRECT:
2232 if (!cfi->drap && op->dest.reg == cfa->base &&
2233 op->dest.reg == CFI_BP) {
2235 /* mov disp(%rsp), %rbp */
2237 cfa->offset = cfi->stack_size;
2240 if (cfi->drap && op->src.reg == CFI_BP &&
2241 op->src.offset == cfi->drap_offset) {
2243 /* drap: mov disp(%rbp), %drap */
2244 cfa->base = cfi->drap_reg;
2246 cfi->drap_offset = -1;
2249 if (cfi->drap && op->src.reg == CFI_BP &&
2250 op->src.offset == regs[op->dest.reg].offset) {
2252 /* drap: mov disp(%rbp), %reg */
2253 restore_reg(cfi, op->dest.reg);
2255 } else if (op->src.reg == cfa->base &&
2256 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2258 /* mov disp(%rbp), %reg */
2259 /* mov disp(%rsp), %reg */
2260 restore_reg(cfi, op->dest.reg);
2262 } else if (op->src.reg == CFI_SP &&
2263 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2265 /* mov disp(%rsp), %reg */
2266 restore_reg(cfi, op->dest.reg);
2272 WARN_FUNC("unknown stack-related instruction",
2273 insn->sec, insn->offset);
2281 cfi->stack_size += 8;
2282 if (cfa->base == CFI_SP)
2285 if (op->src.type != OP_SRC_REG)
2289 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2291 /* drap: push %drap */
2292 cfa->base = CFI_BP_INDIRECT;
2293 cfa->offset = -cfi->stack_size;
2295 /* save drap so we know when to restore it */
2296 cfi->drap_offset = -cfi->stack_size;
2298 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2300 /* drap: push %rbp */
2301 cfi->stack_size = 0;
2305 /* drap: push %reg */
2306 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2312 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2315 /* detect when asm code uses rbp as a scratch register */
2316 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2317 cfa->base != CFI_BP)
2318 cfi->bp_scratch = true;
2321 case OP_DEST_REG_INDIRECT:
2324 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2326 /* drap: mov %drap, disp(%rbp) */
2327 cfa->base = CFI_BP_INDIRECT;
2328 cfa->offset = op->dest.offset;
2330 /* save drap offset so we know when to restore it */
2331 cfi->drap_offset = op->dest.offset;
2334 /* drap: mov reg, disp(%rbp) */
2335 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2338 } else if (op->dest.reg == cfa->base) {
2340 /* mov reg, disp(%rbp) */
2341 /* mov reg, disp(%rsp) */
2342 save_reg(cfi, op->src.reg, CFI_CFA,
2343 op->dest.offset - cfi->cfa.offset);
2345 } else if (op->dest.reg == CFI_SP) {
2347 /* mov reg, disp(%rsp) */
2348 save_reg(cfi, op->src.reg, CFI_CFA,
2349 op->dest.offset - cfi->stack_size);
2351 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2353 /* mov %rsp, (%reg); # setup a stack swizzle. */
2354 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2355 cfi->vals[op->dest.reg].offset = cfa->offset;
2361 if ((!cfi->drap && cfa->base != CFI_BP) ||
2362 (cfi->drap && cfa->base != cfi->drap_reg)) {
2363 WARN_FUNC("leave instruction with modified stack frame",
2364 insn->sec, insn->offset);
2368 /* leave (mov %rbp, %rsp; pop %rbp) */
2370 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2371 restore_reg(cfi, CFI_BP);
2381 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2382 WARN_FUNC("unknown stack-related memory operation",
2383 insn->sec, insn->offset);
2388 cfi->stack_size -= 8;
2389 if (cfa->base == CFI_SP)
2395 WARN_FUNC("unknown stack-related instruction",
2396 insn->sec, insn->offset);
2404 * The stack layouts of alternatives instructions can sometimes diverge when
2405 * they have stack modifications. That's fine as long as the potential stack
2406 * layouts don't conflict at any given potential instruction boundary.
2408 * Flatten the CFIs of the different alternative code streams (both original
2409 * and replacement) into a single shared CFI array which can be used to detect
2410 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2412 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2414 struct cfi_state **alt_cfi;
2417 if (!insn->alt_group)
2420 alt_cfi = insn->alt_group->cfi;
2421 group_off = insn->offset - insn->alt_group->first_insn->offset;
2423 if (!alt_cfi[group_off]) {
2424 alt_cfi[group_off] = &insn->cfi;
2426 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2427 WARN_FUNC("stack layout conflict in alternatives",
2428 insn->sec, insn->offset);
2436 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2438 struct stack_op *op;
2440 list_for_each_entry(op, &insn->stack_ops, list) {
2442 if (update_cfi_state(insn, &state->cfi, op))
2445 if (!insn->alt_group)
2448 if (op->dest.type == OP_DEST_PUSHF) {
2449 if (!state->uaccess_stack) {
2450 state->uaccess_stack = 1;
2451 } else if (state->uaccess_stack >> 31) {
2452 WARN_FUNC("PUSHF stack exhausted",
2453 insn->sec, insn->offset);
2456 state->uaccess_stack <<= 1;
2457 state->uaccess_stack |= state->uaccess;
2460 if (op->src.type == OP_SRC_POPF) {
2461 if (state->uaccess_stack) {
2462 state->uaccess = state->uaccess_stack & 1;
2463 state->uaccess_stack >>= 1;
2464 if (state->uaccess_stack == 1)
2465 state->uaccess_stack = 0;
2473 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2475 struct cfi_state *cfi1 = &insn->cfi;
2478 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2480 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2481 insn->sec, insn->offset,
2482 cfi1->cfa.base, cfi1->cfa.offset,
2483 cfi2->cfa.base, cfi2->cfa.offset);
2485 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2486 for (i = 0; i < CFI_NUM_REGS; i++) {
2487 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2488 sizeof(struct cfi_reg)))
2491 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2492 insn->sec, insn->offset,
2493 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2494 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2498 } else if (cfi1->type != cfi2->type) {
2500 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2501 insn->sec, insn->offset, cfi1->type, cfi2->type);
2503 } else if (cfi1->drap != cfi2->drap ||
2504 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2505 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2507 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2508 insn->sec, insn->offset,
2509 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2510 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2518 static inline bool func_uaccess_safe(struct symbol *func)
2521 return func->uaccess_safe;
2526 static inline const char *call_dest_name(struct instruction *insn)
2528 if (insn->call_dest)
2529 return insn->call_dest->name;
2534 static inline bool noinstr_call_dest(struct symbol *func)
2537 * We can't deal with indirect function calls at present;
2538 * assume they're instrumented.
2544 * If the symbol is from a noinstr section; we good.
2546 if (func->sec->noinstr)
2550 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2551 * something 'BAD' happened. At the risk of taking the machine down,
2552 * let them proceed to get the message out.
2554 if (!strncmp(func->name, "__ubsan_handle_", 15))
2560 static int validate_call(struct instruction *insn, struct insn_state *state)
2562 if (state->noinstr && state->instr <= 0 &&
2563 !noinstr_call_dest(insn->call_dest)) {
2564 WARN_FUNC("call to %s() leaves .noinstr.text section",
2565 insn->sec, insn->offset, call_dest_name(insn));
2569 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2570 WARN_FUNC("call to %s() with UACCESS enabled",
2571 insn->sec, insn->offset, call_dest_name(insn));
2576 WARN_FUNC("call to %s() with DF set",
2577 insn->sec, insn->offset, call_dest_name(insn));
2584 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2586 if (has_modified_stack_frame(insn, state)) {
2587 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2588 insn->sec, insn->offset);
2592 return validate_call(insn, state);
2595 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2597 if (state->noinstr && state->instr > 0) {
2598 WARN_FUNC("return with instrumentation enabled",
2599 insn->sec, insn->offset);
2603 if (state->uaccess && !func_uaccess_safe(func)) {
2604 WARN_FUNC("return with UACCESS enabled",
2605 insn->sec, insn->offset);
2609 if (!state->uaccess && func_uaccess_safe(func)) {
2610 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2611 insn->sec, insn->offset);
2616 WARN_FUNC("return with DF set",
2617 insn->sec, insn->offset);
2621 if (func && has_modified_stack_frame(insn, state)) {
2622 WARN_FUNC("return with modified stack frame",
2623 insn->sec, insn->offset);
2627 if (state->cfi.bp_scratch) {
2628 WARN_FUNC("BP used as a scratch register",
2629 insn->sec, insn->offset);
2636 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2637 struct instruction *insn)
2639 struct alt_group *alt_group = insn->alt_group;
2642 * Simulate the fact that alternatives are patched in-place. When the
2643 * end of a replacement alt_group is reached, redirect objtool flow to
2644 * the end of the original alt_group.
2646 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2647 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2649 return next_insn_same_sec(file, insn);
2653 * Follow the branch starting at the given instruction, and recursively follow
2654 * any other branches (jumps). Meanwhile, track the frame pointer state at
2655 * each instruction and validate all the rules described in
2656 * tools/objtool/Documentation/stack-validation.txt.
2658 static int validate_branch(struct objtool_file *file, struct symbol *func,
2659 struct instruction *insn, struct insn_state state)
2661 struct alternative *alt;
2662 struct instruction *next_insn;
2663 struct section *sec;
2670 next_insn = next_insn_to_validate(file, insn);
2672 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2673 WARN("%s() falls through to next function %s()",
2674 func->name, insn->func->name);
2678 if (func && insn->ignore) {
2679 WARN_FUNC("BUG: why am I validating an ignored function?",
2684 visited = 1 << state.uaccess;
2685 if (insn->visited) {
2686 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2689 if (insn->visited & visited)
2694 state.instr += insn->instr;
2697 state.cfi = insn->cfi;
2699 insn->cfi = state.cfi;
2701 insn->visited |= visited;
2703 if (propagate_alt_cfi(file, insn))
2706 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2707 bool skip_orig = false;
2709 list_for_each_entry(alt, &insn->alts, list) {
2713 ret = validate_branch(file, func, alt->insn, state);
2716 BT_FUNC("(alt)", insn);
2725 if (handle_insn_ops(insn, &state))
2728 switch (insn->type) {
2731 return validate_return(func, insn, &state);
2734 case INSN_CALL_DYNAMIC:
2735 ret = validate_call(insn, &state);
2739 if (!no_fp && func && !is_fentry_call(insn) &&
2740 !has_valid_stack_frame(&state)) {
2741 WARN_FUNC("call without frame pointer save/setup",
2746 if (dead_end_function(file, insn->call_dest))
2749 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2750 list_add_tail(&insn->static_call_node,
2751 &file->static_call_list);
2756 case INSN_JUMP_CONDITIONAL:
2757 case INSN_JUMP_UNCONDITIONAL:
2758 if (is_sibling_call(insn)) {
2759 ret = validate_sibling_call(insn, &state);
2763 } else if (insn->jump_dest) {
2764 ret = validate_branch(file, func,
2765 insn->jump_dest, state);
2768 BT_FUNC("(branch)", insn);
2773 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2778 case INSN_JUMP_DYNAMIC:
2779 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2780 if (is_sibling_call(insn)) {
2781 ret = validate_sibling_call(insn, &state);
2786 if (insn->type == INSN_JUMP_DYNAMIC)
2791 case INSN_CONTEXT_SWITCH:
2792 if (func && (!next_insn || !next_insn->hint)) {
2793 WARN_FUNC("unsupported instruction in callable function",
2800 if (state.uaccess) {
2801 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2805 state.uaccess = true;
2809 if (!state.uaccess && func) {
2810 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2814 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2815 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2819 state.uaccess = false;
2824 WARN_FUNC("recursive STD", sec, insn->offset);
2832 if (!state.df && func) {
2833 WARN_FUNC("redundant CLD", sec, insn->offset);
2848 if (state.cfi.cfa.base == CFI_UNDEFINED)
2850 WARN("%s: unexpected end of section", sec->name);
2860 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2862 struct instruction *insn;
2863 struct insn_state state;
2864 int ret, warnings = 0;
2869 init_insn_state(&state, sec);
2872 insn = find_insn(file, sec, 0);
2876 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2879 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2880 if (insn->hint && !insn->visited) {
2881 ret = validate_branch(file, insn->func, insn, state);
2882 if (ret && backtrace)
2883 BT_FUNC("<=== (hint)", insn);
2887 insn = list_next_entry(insn, list);
2893 static int validate_retpoline(struct objtool_file *file)
2895 struct instruction *insn;
2898 for_each_insn(file, insn) {
2899 if (insn->type != INSN_JUMP_DYNAMIC &&
2900 insn->type != INSN_CALL_DYNAMIC)
2903 if (insn->retpoline_safe)
2907 * .init.text code is ran before userspace and thus doesn't
2908 * strictly need retpolines, except for modules which are
2909 * loaded late, they very much do need retpoline in their
2912 if (!strcmp(insn->sec->name, ".init.text") && !module)
2915 WARN_FUNC("indirect %s found in RETPOLINE build",
2916 insn->sec, insn->offset,
2917 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2925 static bool is_kasan_insn(struct instruction *insn)
2927 return (insn->type == INSN_CALL &&
2928 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2931 static bool is_ubsan_insn(struct instruction *insn)
2933 return (insn->type == INSN_CALL &&
2934 !strcmp(insn->call_dest->name,
2935 "__ubsan_handle_builtin_unreachable"));
2938 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
2941 struct instruction *prev_insn;
2943 if (insn->ignore || insn->type == INSN_NOP)
2947 * Ignore any unused exceptions. This can happen when a whitelisted
2948 * function has an exception table entry.
2950 * Also ignore alternative replacement instructions. This can happen
2951 * when a whitelisted function uses one of the ALTERNATIVE macros.
2953 if (!strcmp(insn->sec->name, ".fixup") ||
2954 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2955 !strcmp(insn->sec->name, ".altinstr_aux"))
2962 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2963 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2964 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2965 * (or occasionally a JMP to UD2).
2967 * It may also insert a UD2 after calling a __noreturn function.
2969 prev_insn = list_prev_entry(insn, list);
2970 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
2971 (insn->type == INSN_BUG ||
2972 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2973 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2977 * Check if this (or a subsequent) instruction is related to
2978 * CONFIG_UBSAN or CONFIG_KASAN.
2980 * End the search at 5 instructions to avoid going into the weeds.
2982 for (i = 0; i < 5; i++) {
2984 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2987 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2988 if (insn->jump_dest &&
2989 insn->jump_dest->func == insn->func) {
2990 insn = insn->jump_dest;
2997 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3000 insn = list_next_entry(insn, list);
3006 static int validate_symbol(struct objtool_file *file, struct section *sec,
3007 struct symbol *sym, struct insn_state *state)
3009 struct instruction *insn;
3013 WARN("%s() is missing an ELF size annotation", sym->name);
3017 if (sym->pfunc != sym || sym->alias != sym)
3020 insn = find_insn(file, sec, sym->offset);
3021 if (!insn || insn->ignore || insn->visited)
3024 state->uaccess = sym->uaccess_safe;
3026 ret = validate_branch(file, insn->func, insn, *state);
3027 if (ret && backtrace)
3028 BT_FUNC("<=== (sym)", insn);
3032 static int validate_section(struct objtool_file *file, struct section *sec)
3034 struct insn_state state;
3035 struct symbol *func;
3038 list_for_each_entry(func, &sec->symbol_list, list) {
3039 if (func->type != STT_FUNC)
3042 init_insn_state(&state, sec);
3043 set_func_state(&state.cfi);
3045 warnings += validate_symbol(file, sec, func, &state);
3051 static int validate_vmlinux_functions(struct objtool_file *file)
3053 struct section *sec;
3056 sec = find_section_by_name(file->elf, ".noinstr.text");
3058 warnings += validate_section(file, sec);
3059 warnings += validate_unwind_hints(file, sec);
3062 sec = find_section_by_name(file->elf, ".entry.text");
3064 warnings += validate_section(file, sec);
3065 warnings += validate_unwind_hints(file, sec);
3071 static int validate_functions(struct objtool_file *file)
3073 struct section *sec;
3076 for_each_sec(file, sec) {
3077 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3080 warnings += validate_section(file, sec);
3086 static int validate_reachable_instructions(struct objtool_file *file)
3088 struct instruction *insn;
3090 if (file->ignore_unreachables)
3093 for_each_insn(file, insn) {
3094 if (insn->visited || ignore_unreachable_insn(file, insn))
3097 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3104 int check(struct objtool_file *file)
3106 int ret, warnings = 0;
3108 arch_initial_func_cfi_state(&initial_func_cfi);
3110 ret = decode_sections(file);
3115 if (list_empty(&file->insn_list))
3118 if (vmlinux && !validate_dup) {
3119 ret = validate_vmlinux_functions(file);
3128 ret = validate_retpoline(file);
3134 ret = validate_functions(file);
3139 ret = validate_unwind_hints(file, NULL);
3145 ret = validate_reachable_instructions(file);
3151 ret = create_static_call_sections(file);
3157 ret = create_mcount_loc_sections(file);
3165 * For now, don't fail the kernel build on fatal warnings. These
3166 * errors are still fairly common due to the growing matrix of
3167 * supported toolchains and their recent pace of change.