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).
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);
510 WARN("static_call: can't find static_call_key symbol: %s", tmp);
515 /* populate reloc for 'key' */
516 reloc = malloc(sizeof(*reloc));
521 memset(reloc, 0, sizeof(*reloc));
522 reloc->sym = key_sym;
523 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
524 reloc->type = R_X86_64_PC32;
525 reloc->offset = idx * sizeof(struct static_call_site) + 4;
526 reloc->sec = reloc_sec;
527 elf_add_reloc(file->elf, reloc);
532 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
539 * Warnings shouldn't be reported for ignored functions.
541 static void add_ignores(struct objtool_file *file)
543 struct instruction *insn;
548 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
552 list_for_each_entry(reloc, &sec->reloc_list, list) {
553 switch (reloc->sym->type) {
559 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
565 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
569 func_for_each_insn(file, func, insn)
575 * This is a whitelist of functions that is allowed to be called with AC set.
576 * The list is meant to be minimal and only contains compiler instrumentation
577 * ABI and a few functions used to implement *_{to,from}_user() functions.
579 * These functions must not directly change AC, but may PUSHF/POPF.
581 static const char *uaccess_safe_builtin[] = {
584 "check_memory_region",
585 /* KASAN out-of-line */
586 "__asan_loadN_noabort",
587 "__asan_load1_noabort",
588 "__asan_load2_noabort",
589 "__asan_load4_noabort",
590 "__asan_load8_noabort",
591 "__asan_load16_noabort",
592 "__asan_storeN_noabort",
593 "__asan_store1_noabort",
594 "__asan_store2_noabort",
595 "__asan_store4_noabort",
596 "__asan_store8_noabort",
597 "__asan_store16_noabort",
598 "__kasan_check_read",
599 "__kasan_check_write",
601 "__asan_report_load_n_noabort",
602 "__asan_report_load1_noabort",
603 "__asan_report_load2_noabort",
604 "__asan_report_load4_noabort",
605 "__asan_report_load8_noabort",
606 "__asan_report_load16_noabort",
607 "__asan_report_store_n_noabort",
608 "__asan_report_store1_noabort",
609 "__asan_report_store2_noabort",
610 "__asan_report_store4_noabort",
611 "__asan_report_store8_noabort",
612 "__asan_report_store16_noabort",
614 "__kcsan_check_access",
615 "kcsan_found_watchpoint",
616 "kcsan_setup_watchpoint",
617 "kcsan_check_scoped_accesses",
618 "kcsan_disable_current",
619 "kcsan_enable_current_nowarn",
624 "__tsan_write_range",
635 "__tsan_read_write1",
636 "__tsan_read_write2",
637 "__tsan_read_write4",
638 "__tsan_read_write8",
639 "__tsan_read_write16",
640 "__tsan_atomic8_load",
641 "__tsan_atomic16_load",
642 "__tsan_atomic32_load",
643 "__tsan_atomic64_load",
644 "__tsan_atomic8_store",
645 "__tsan_atomic16_store",
646 "__tsan_atomic32_store",
647 "__tsan_atomic64_store",
648 "__tsan_atomic8_exchange",
649 "__tsan_atomic16_exchange",
650 "__tsan_atomic32_exchange",
651 "__tsan_atomic64_exchange",
652 "__tsan_atomic8_fetch_add",
653 "__tsan_atomic16_fetch_add",
654 "__tsan_atomic32_fetch_add",
655 "__tsan_atomic64_fetch_add",
656 "__tsan_atomic8_fetch_sub",
657 "__tsan_atomic16_fetch_sub",
658 "__tsan_atomic32_fetch_sub",
659 "__tsan_atomic64_fetch_sub",
660 "__tsan_atomic8_fetch_and",
661 "__tsan_atomic16_fetch_and",
662 "__tsan_atomic32_fetch_and",
663 "__tsan_atomic64_fetch_and",
664 "__tsan_atomic8_fetch_or",
665 "__tsan_atomic16_fetch_or",
666 "__tsan_atomic32_fetch_or",
667 "__tsan_atomic64_fetch_or",
668 "__tsan_atomic8_fetch_xor",
669 "__tsan_atomic16_fetch_xor",
670 "__tsan_atomic32_fetch_xor",
671 "__tsan_atomic64_fetch_xor",
672 "__tsan_atomic8_fetch_nand",
673 "__tsan_atomic16_fetch_nand",
674 "__tsan_atomic32_fetch_nand",
675 "__tsan_atomic64_fetch_nand",
676 "__tsan_atomic8_compare_exchange_strong",
677 "__tsan_atomic16_compare_exchange_strong",
678 "__tsan_atomic32_compare_exchange_strong",
679 "__tsan_atomic64_compare_exchange_strong",
680 "__tsan_atomic8_compare_exchange_weak",
681 "__tsan_atomic16_compare_exchange_weak",
682 "__tsan_atomic32_compare_exchange_weak",
683 "__tsan_atomic64_compare_exchange_weak",
684 "__tsan_atomic8_compare_exchange_val",
685 "__tsan_atomic16_compare_exchange_val",
686 "__tsan_atomic32_compare_exchange_val",
687 "__tsan_atomic64_compare_exchange_val",
688 "__tsan_atomic_thread_fence",
689 "__tsan_atomic_signal_fence",
693 "__sanitizer_cov_trace_pc",
694 "__sanitizer_cov_trace_const_cmp1",
695 "__sanitizer_cov_trace_const_cmp2",
696 "__sanitizer_cov_trace_const_cmp4",
697 "__sanitizer_cov_trace_const_cmp8",
698 "__sanitizer_cov_trace_cmp1",
699 "__sanitizer_cov_trace_cmp2",
700 "__sanitizer_cov_trace_cmp4",
701 "__sanitizer_cov_trace_cmp8",
702 "__sanitizer_cov_trace_switch",
704 "ubsan_type_mismatch_common",
705 "__ubsan_handle_type_mismatch",
706 "__ubsan_handle_type_mismatch_v1",
707 "__ubsan_handle_shift_out_of_bounds",
709 "csum_partial_copy_generic",
711 "copy_mc_fragile_handle_tail",
712 "copy_mc_enhanced_fast_string",
713 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
717 static void add_uaccess_safe(struct objtool_file *file)
725 for (name = uaccess_safe_builtin; *name; name++) {
726 func = find_symbol_by_name(file->elf, *name);
730 func->uaccess_safe = true;
735 * FIXME: For now, just ignore any alternatives which add retpolines. This is
736 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
737 * But it at least allows objtool to understand the control flow *around* the
740 static int add_ignore_alternatives(struct objtool_file *file)
744 struct instruction *insn;
746 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
750 list_for_each_entry(reloc, &sec->reloc_list, list) {
751 if (reloc->sym->type != STT_SECTION) {
752 WARN("unexpected relocation symbol type in %s", sec->name);
756 insn = find_insn(file, reloc->sym->sec, reloc->addend);
758 WARN("bad .discard.ignore_alts entry");
762 insn->ignore_alts = true;
769 * Find the destination instructions for all jumps.
771 static int add_jump_destinations(struct objtool_file *file)
773 struct instruction *insn;
775 struct section *dest_sec;
776 unsigned long dest_off;
778 for_each_insn(file, insn) {
779 if (!is_static_jump(insn))
782 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
783 insn->offset, insn->len);
785 dest_sec = insn->sec;
786 dest_off = arch_jump_destination(insn);
787 } else if (reloc->sym->type == STT_SECTION) {
788 dest_sec = reloc->sym->sec;
789 dest_off = arch_dest_reloc_offset(reloc->addend);
790 } else if (!strncmp(reloc->sym->name, "__x86_indirect_thunk_", 21) ||
791 !strncmp(reloc->sym->name, "__x86_retpoline_", 16)) {
793 * Retpoline jumps are really dynamic jumps in
794 * disguise, so convert them accordingly.
796 if (insn->type == INSN_JUMP_UNCONDITIONAL)
797 insn->type = INSN_JUMP_DYNAMIC;
799 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
801 insn->retpoline_safe = true;
803 } else if (insn->func) {
804 /* internal or external sibling call (with reloc) */
805 insn->call_dest = reloc->sym;
806 if (insn->call_dest->static_call_tramp) {
807 list_add_tail(&insn->static_call_node,
808 &file->static_call_list);
811 } else if (reloc->sym->sec->idx) {
812 dest_sec = reloc->sym->sec;
813 dest_off = reloc->sym->sym.st_value +
814 arch_dest_reloc_offset(reloc->addend);
816 /* non-func asm code jumping to another file */
820 insn->jump_dest = find_insn(file, dest_sec, dest_off);
821 if (!insn->jump_dest) {
824 * This is a special case where an alt instruction
825 * jumps past the end of the section. These are
826 * handled later in handle_group_alt().
828 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
831 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
832 insn->sec, insn->offset, dest_sec->name,
838 * Cross-function jump.
840 if (insn->func && insn->jump_dest->func &&
841 insn->func != insn->jump_dest->func) {
844 * For GCC 8+, create parent/child links for any cold
845 * subfunctions. This is _mostly_ redundant with a
846 * similar initialization in read_symbols().
848 * If a function has aliases, we want the *first* such
849 * function in the symbol table to be the subfunction's
850 * parent. In that case we overwrite the
851 * initialization done in read_symbols().
853 * However this code can't completely replace the
854 * read_symbols() code because this doesn't detect the
855 * case where the parent function's only reference to a
856 * subfunction is through a jump table.
858 if (!strstr(insn->func->name, ".cold") &&
859 strstr(insn->jump_dest->func->name, ".cold")) {
860 insn->func->cfunc = insn->jump_dest->func;
861 insn->jump_dest->func->pfunc = insn->func;
863 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
864 insn->jump_dest->offset == insn->jump_dest->func->offset) {
866 /* internal sibling call (without reloc) */
867 insn->call_dest = insn->jump_dest->func;
868 if (insn->call_dest->static_call_tramp) {
869 list_add_tail(&insn->static_call_node,
870 &file->static_call_list);
879 static void remove_insn_ops(struct instruction *insn)
881 struct stack_op *op, *tmp;
883 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
889 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
891 struct symbol *call_dest;
893 call_dest = find_func_by_offset(sec, offset);
895 call_dest = find_symbol_by_offset(sec, offset);
901 * Find the destination instructions for all calls.
903 static int add_call_destinations(struct objtool_file *file)
905 struct instruction *insn;
906 unsigned long dest_off;
909 for_each_insn(file, insn) {
910 if (insn->type != INSN_CALL)
913 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
914 insn->offset, insn->len);
916 dest_off = arch_jump_destination(insn);
917 insn->call_dest = find_call_destination(insn->sec, dest_off);
922 if (!insn->call_dest) {
923 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
927 if (insn->func && insn->call_dest->type != STT_FUNC) {
928 WARN_FUNC("unsupported call to non-function",
929 insn->sec, insn->offset);
933 } else if (reloc->sym->type == STT_SECTION) {
934 dest_off = arch_dest_reloc_offset(reloc->addend);
935 insn->call_dest = find_call_destination(reloc->sym->sec,
937 if (!insn->call_dest) {
938 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
939 insn->sec, insn->offset,
940 reloc->sym->sec->name,
945 insn->call_dest = reloc->sym;
948 * Many compilers cannot disable KCOV with a function attribute
949 * so they need a little help, NOP out any KCOV calls from noinstr
952 if (insn->sec->noinstr &&
953 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
955 reloc->type = R_NONE;
956 elf_write_reloc(file->elf, reloc);
959 elf_write_insn(file->elf, insn->sec,
960 insn->offset, insn->len,
961 arch_nop_insn(insn->len));
962 insn->type = INSN_NOP;
966 * Whatever stack impact regular CALLs have, should be undone
967 * by the RETURN of the called function.
969 * Annotated intra-function calls retain the stack_ops but
970 * are converted to JUMP, see read_intra_function_calls().
972 remove_insn_ops(insn);
979 * The .alternatives section requires some extra special care over and above
980 * other special sections because alternatives are patched in place.
982 static int handle_group_alt(struct objtool_file *file,
983 struct special_alt *special_alt,
984 struct instruction *orig_insn,
985 struct instruction **new_insn)
987 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
988 struct alt_group *orig_alt_group, *new_alt_group;
989 unsigned long dest_off;
992 orig_alt_group = malloc(sizeof(*orig_alt_group));
993 if (!orig_alt_group) {
994 WARN("malloc failed");
997 orig_alt_group->cfi = calloc(special_alt->orig_len,
998 sizeof(struct cfi_state *));
999 if (!orig_alt_group->cfi) {
1000 WARN("calloc failed");
1004 last_orig_insn = NULL;
1006 sec_for_each_insn_from(file, insn) {
1007 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1010 insn->alt_group = orig_alt_group;
1011 last_orig_insn = insn;
1013 orig_alt_group->orig_group = NULL;
1014 orig_alt_group->first_insn = orig_insn;
1015 orig_alt_group->last_insn = last_orig_insn;
1018 new_alt_group = malloc(sizeof(*new_alt_group));
1019 if (!new_alt_group) {
1020 WARN("malloc failed");
1024 if (special_alt->new_len < special_alt->orig_len) {
1026 * Insert a fake nop at the end to make the replacement
1027 * alt_group the same size as the original. This is needed to
1028 * allow propagate_alt_cfi() to do its magic. When the last
1029 * instruction affects the stack, the instruction after it (the
1030 * nop) will propagate the new state to the shared CFI array.
1032 nop = malloc(sizeof(*nop));
1034 WARN("malloc failed");
1037 memset(nop, 0, sizeof(*nop));
1038 INIT_LIST_HEAD(&nop->alts);
1039 INIT_LIST_HEAD(&nop->stack_ops);
1040 init_cfi_state(&nop->cfi);
1042 nop->sec = special_alt->new_sec;
1043 nop->offset = special_alt->new_off + special_alt->new_len;
1044 nop->len = special_alt->orig_len - special_alt->new_len;
1045 nop->type = INSN_NOP;
1046 nop->func = orig_insn->func;
1047 nop->alt_group = new_alt_group;
1048 nop->ignore = orig_insn->ignore_alts;
1051 if (!special_alt->new_len) {
1057 sec_for_each_insn_from(file, insn) {
1058 struct reloc *alt_reloc;
1060 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1063 last_new_insn = insn;
1065 insn->ignore = orig_insn->ignore_alts;
1066 insn->func = orig_insn->func;
1067 insn->alt_group = new_alt_group;
1070 * Since alternative replacement code is copy/pasted by the
1071 * kernel after applying relocations, generally such code can't
1072 * have relative-address relocation references to outside the
1073 * .altinstr_replacement section, unless the arch's
1074 * alternatives code can adjust the relative offsets
1077 alt_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1078 insn->offset, insn->len);
1080 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1082 WARN_FUNC("unsupported relocation in alternatives section",
1083 insn->sec, insn->offset);
1087 if (!is_static_jump(insn))
1090 if (!insn->immediate)
1093 dest_off = arch_jump_destination(insn);
1094 if (dest_off == special_alt->new_off + special_alt->new_len)
1095 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1097 if (!insn->jump_dest) {
1098 WARN_FUNC("can't find alternative jump destination",
1099 insn->sec, insn->offset);
1104 if (!last_new_insn) {
1105 WARN_FUNC("can't find last new alternative instruction",
1106 special_alt->new_sec, special_alt->new_off);
1111 list_add(&nop->list, &last_new_insn->list);
1113 new_alt_group->orig_group = orig_alt_group;
1114 new_alt_group->first_insn = *new_insn;
1115 new_alt_group->last_insn = nop ? : last_new_insn;
1116 new_alt_group->cfi = orig_alt_group->cfi;
1121 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1122 * If the original instruction is a jump, make the alt entry an effective nop
1123 * by just skipping the original instruction.
1125 static int handle_jump_alt(struct objtool_file *file,
1126 struct special_alt *special_alt,
1127 struct instruction *orig_insn,
1128 struct instruction **new_insn)
1130 if (orig_insn->type == INSN_NOP)
1133 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1134 WARN_FUNC("unsupported instruction at jump label",
1135 orig_insn->sec, orig_insn->offset);
1139 *new_insn = list_next_entry(orig_insn, list);
1144 * Read all the special sections which have alternate instructions which can be
1145 * patched in or redirected to at runtime. Each instruction having alternate
1146 * instruction(s) has them added to its insn->alts list, which will be
1147 * traversed in validate_branch().
1149 static int add_special_section_alts(struct objtool_file *file)
1151 struct list_head special_alts;
1152 struct instruction *orig_insn, *new_insn;
1153 struct special_alt *special_alt, *tmp;
1154 struct alternative *alt;
1157 ret = special_get_alts(file->elf, &special_alts);
1161 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1163 orig_insn = find_insn(file, special_alt->orig_sec,
1164 special_alt->orig_off);
1166 WARN_FUNC("special: can't find orig instruction",
1167 special_alt->orig_sec, special_alt->orig_off);
1173 if (!special_alt->group || special_alt->new_len) {
1174 new_insn = find_insn(file, special_alt->new_sec,
1175 special_alt->new_off);
1177 WARN_FUNC("special: can't find new instruction",
1178 special_alt->new_sec,
1179 special_alt->new_off);
1185 if (special_alt->group) {
1186 if (!special_alt->orig_len) {
1187 WARN_FUNC("empty alternative entry",
1188 orig_insn->sec, orig_insn->offset);
1192 ret = handle_group_alt(file, special_alt, orig_insn,
1196 } else if (special_alt->jump_or_nop) {
1197 ret = handle_jump_alt(file, special_alt, orig_insn,
1203 alt = malloc(sizeof(*alt));
1205 WARN("malloc failed");
1210 alt->insn = new_insn;
1211 alt->skip_orig = special_alt->skip_orig;
1212 orig_insn->ignore_alts |= special_alt->skip_alt;
1213 list_add_tail(&alt->list, &orig_insn->alts);
1215 list_del(&special_alt->list);
1223 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1224 struct reloc *table)
1226 struct reloc *reloc = table;
1227 struct instruction *dest_insn;
1228 struct alternative *alt;
1229 struct symbol *pfunc = insn->func->pfunc;
1230 unsigned int prev_offset = 0;
1233 * Each @reloc is a switch table relocation which points to the target
1236 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1238 /* Check for the end of the table: */
1239 if (reloc != table && reloc->jump_table_start)
1242 /* Make sure the table entries are consecutive: */
1243 if (prev_offset && reloc->offset != prev_offset + 8)
1246 /* Detect function pointers from contiguous objects: */
1247 if (reloc->sym->sec == pfunc->sec &&
1248 reloc->addend == pfunc->offset)
1251 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1255 /* Make sure the destination is in the same function: */
1256 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1259 alt = malloc(sizeof(*alt));
1261 WARN("malloc failed");
1265 alt->insn = dest_insn;
1266 list_add_tail(&alt->list, &insn->alts);
1267 prev_offset = reloc->offset;
1271 WARN_FUNC("can't find switch jump table",
1272 insn->sec, insn->offset);
1280 * find_jump_table() - Given a dynamic jump, find the switch jump table
1281 * associated with it.
1283 static struct reloc *find_jump_table(struct objtool_file *file,
1284 struct symbol *func,
1285 struct instruction *insn)
1287 struct reloc *table_reloc;
1288 struct instruction *dest_insn, *orig_insn = insn;
1291 * Backward search using the @first_jump_src links, these help avoid
1292 * much of the 'in between' code. Which avoids us getting confused by
1296 insn && insn->func && insn->func->pfunc == func;
1297 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1299 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1302 /* allow small jumps within the range */
1303 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1305 (insn->jump_dest->offset <= insn->offset ||
1306 insn->jump_dest->offset > orig_insn->offset))
1309 table_reloc = arch_find_switch_table(file, insn);
1312 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1313 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1323 * First pass: Mark the head of each jump table so that in the next pass,
1324 * we know when a given jump table ends and the next one starts.
1326 static void mark_func_jump_tables(struct objtool_file *file,
1327 struct symbol *func)
1329 struct instruction *insn, *last = NULL;
1330 struct reloc *reloc;
1332 func_for_each_insn(file, func, insn) {
1337 * Store back-pointers for unconditional forward jumps such
1338 * that find_jump_table() can back-track using those and
1339 * avoid some potentially confusing code.
1341 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1342 insn->offset > last->offset &&
1343 insn->jump_dest->offset > insn->offset &&
1344 !insn->jump_dest->first_jump_src) {
1346 insn->jump_dest->first_jump_src = insn;
1347 last = insn->jump_dest;
1350 if (insn->type != INSN_JUMP_DYNAMIC)
1353 reloc = find_jump_table(file, func, insn);
1355 reloc->jump_table_start = true;
1356 insn->jump_table = reloc;
1361 static int add_func_jump_tables(struct objtool_file *file,
1362 struct symbol *func)
1364 struct instruction *insn;
1367 func_for_each_insn(file, func, insn) {
1368 if (!insn->jump_table)
1371 ret = add_jump_table(file, insn, insn->jump_table);
1380 * For some switch statements, gcc generates a jump table in the .rodata
1381 * section which contains a list of addresses within the function to jump to.
1382 * This finds these jump tables and adds them to the insn->alts lists.
1384 static int add_jump_table_alts(struct objtool_file *file)
1386 struct section *sec;
1387 struct symbol *func;
1393 for_each_sec(file, sec) {
1394 list_for_each_entry(func, &sec->symbol_list, list) {
1395 if (func->type != STT_FUNC)
1398 mark_func_jump_tables(file, func);
1399 ret = add_func_jump_tables(file, func);
1408 static void set_func_state(struct cfi_state *state)
1410 state->cfa = initial_func_cfi.cfa;
1411 memcpy(&state->regs, &initial_func_cfi.regs,
1412 CFI_NUM_REGS * sizeof(struct cfi_reg));
1413 state->stack_size = initial_func_cfi.cfa.offset;
1416 static int read_unwind_hints(struct objtool_file *file)
1418 struct section *sec, *relocsec;
1419 struct reloc *reloc;
1420 struct unwind_hint *hint;
1421 struct instruction *insn;
1424 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1428 relocsec = sec->reloc;
1430 WARN("missing .rela.discard.unwind_hints section");
1434 if (sec->len % sizeof(struct unwind_hint)) {
1435 WARN("struct unwind_hint size mismatch");
1441 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1442 hint = (struct unwind_hint *)sec->data->d_buf + i;
1444 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1446 WARN("can't find reloc for unwind_hints[%d]", i);
1450 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1452 WARN("can't find insn for unwind_hints[%d]", i);
1458 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1459 set_func_state(&insn->cfi);
1463 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1464 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1465 insn->sec, insn->offset, hint->sp_reg);
1469 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1470 insn->cfi.type = hint->type;
1471 insn->cfi.end = hint->end;
1477 static int read_retpoline_hints(struct objtool_file *file)
1479 struct section *sec;
1480 struct instruction *insn;
1481 struct reloc *reloc;
1483 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1487 list_for_each_entry(reloc, &sec->reloc_list, list) {
1488 if (reloc->sym->type != STT_SECTION) {
1489 WARN("unexpected relocation symbol type in %s", sec->name);
1493 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1495 WARN("bad .discard.retpoline_safe entry");
1499 if (insn->type != INSN_JUMP_DYNAMIC &&
1500 insn->type != INSN_CALL_DYNAMIC) {
1501 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1502 insn->sec, insn->offset);
1506 insn->retpoline_safe = true;
1512 static int read_instr_hints(struct objtool_file *file)
1514 struct section *sec;
1515 struct instruction *insn;
1516 struct reloc *reloc;
1518 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1522 list_for_each_entry(reloc, &sec->reloc_list, list) {
1523 if (reloc->sym->type != STT_SECTION) {
1524 WARN("unexpected relocation symbol type in %s", sec->name);
1528 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1530 WARN("bad .discard.instr_end entry");
1537 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1541 list_for_each_entry(reloc, &sec->reloc_list, list) {
1542 if (reloc->sym->type != STT_SECTION) {
1543 WARN("unexpected relocation symbol type in %s", sec->name);
1547 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1549 WARN("bad .discard.instr_begin entry");
1559 static int read_intra_function_calls(struct objtool_file *file)
1561 struct instruction *insn;
1562 struct section *sec;
1563 struct reloc *reloc;
1565 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1569 list_for_each_entry(reloc, &sec->reloc_list, list) {
1570 unsigned long dest_off;
1572 if (reloc->sym->type != STT_SECTION) {
1573 WARN("unexpected relocation symbol type in %s",
1578 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1580 WARN("bad .discard.intra_function_call entry");
1584 if (insn->type != INSN_CALL) {
1585 WARN_FUNC("intra_function_call not a direct call",
1586 insn->sec, insn->offset);
1591 * Treat intra-function CALLs as JMPs, but with a stack_op.
1592 * See add_call_destinations(), which strips stack_ops from
1595 insn->type = INSN_JUMP_UNCONDITIONAL;
1597 dest_off = insn->offset + insn->len + insn->immediate;
1598 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1599 if (!insn->jump_dest) {
1600 WARN_FUNC("can't find call dest at %s+0x%lx",
1601 insn->sec, insn->offset,
1602 insn->sec->name, dest_off);
1610 static int read_static_call_tramps(struct objtool_file *file)
1612 struct section *sec;
1613 struct symbol *func;
1615 for_each_sec(file, sec) {
1616 list_for_each_entry(func, &sec->symbol_list, list) {
1617 if (func->bind == STB_GLOBAL &&
1618 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1619 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1620 func->static_call_tramp = true;
1627 static void mark_rodata(struct objtool_file *file)
1629 struct section *sec;
1633 * Search for the following rodata sections, each of which can
1634 * potentially contain jump tables:
1636 * - .rodata: can contain GCC switch tables
1637 * - .rodata.<func>: same, if -fdata-sections is being used
1638 * - .rodata..c_jump_table: contains C annotated jump tables
1640 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1642 for_each_sec(file, sec) {
1643 if (!strncmp(sec->name, ".rodata", 7) &&
1644 !strstr(sec->name, ".str1.")) {
1650 file->rodata = found;
1653 static int decode_sections(struct objtool_file *file)
1659 ret = decode_instructions(file);
1663 ret = add_dead_ends(file);
1668 add_uaccess_safe(file);
1670 ret = add_ignore_alternatives(file);
1674 ret = read_static_call_tramps(file);
1678 ret = add_jump_destinations(file);
1682 ret = add_special_section_alts(file);
1686 ret = read_intra_function_calls(file);
1690 ret = add_call_destinations(file);
1694 ret = add_jump_table_alts(file);
1698 ret = read_unwind_hints(file);
1702 ret = read_retpoline_hints(file);
1706 ret = read_instr_hints(file);
1713 static bool is_fentry_call(struct instruction *insn)
1715 if (insn->type == INSN_CALL && insn->call_dest &&
1716 insn->call_dest->type == STT_NOTYPE &&
1717 !strcmp(insn->call_dest->name, "__fentry__"))
1723 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1725 struct cfi_state *cfi = &state->cfi;
1728 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1731 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1734 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1737 for (i = 0; i < CFI_NUM_REGS; i++) {
1738 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1739 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1746 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1747 int expected_offset)
1749 return reg->base == CFI_CFA &&
1750 reg->offset == expected_offset;
1753 static bool has_valid_stack_frame(struct insn_state *state)
1755 struct cfi_state *cfi = &state->cfi;
1757 if (cfi->cfa.base == CFI_BP &&
1758 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
1759 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
1762 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1768 static int update_cfi_state_regs(struct instruction *insn,
1769 struct cfi_state *cfi,
1770 struct stack_op *op)
1772 struct cfi_reg *cfa = &cfi->cfa;
1774 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1778 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1782 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1785 /* add immediate to sp */
1786 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1787 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1788 cfa->offset -= op->src.offset;
1793 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1795 if (arch_callee_saved_reg(reg) &&
1796 cfi->regs[reg].base == CFI_UNDEFINED) {
1797 cfi->regs[reg].base = base;
1798 cfi->regs[reg].offset = offset;
1802 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1804 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1805 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1809 * A note about DRAP stack alignment:
1811 * GCC has the concept of a DRAP register, which is used to help keep track of
1812 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1813 * register. The typical DRAP pattern is:
1815 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1816 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1817 * 41 ff 72 f8 pushq -0x8(%r10)
1819 * 48 89 e5 mov %rsp,%rbp
1826 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1829 * There are some variations in the epilogues, like:
1837 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1842 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1843 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1844 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1845 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1847 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1850 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1851 * restored beforehand:
1854 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1855 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1857 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1861 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1862 struct stack_op *op)
1864 struct cfi_reg *cfa = &cfi->cfa;
1865 struct cfi_reg *regs = cfi->regs;
1867 /* stack operations don't make sense with an undefined CFA */
1868 if (cfa->base == CFI_UNDEFINED) {
1870 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1876 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
1877 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
1878 return update_cfi_state_regs(insn, cfi, op);
1880 switch (op->dest.type) {
1883 switch (op->src.type) {
1886 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1887 cfa->base == CFI_SP &&
1888 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
1890 /* mov %rsp, %rbp */
1891 cfa->base = op->dest.reg;
1892 cfi->bp_scratch = false;
1895 else if (op->src.reg == CFI_SP &&
1896 op->dest.reg == CFI_BP && cfi->drap) {
1898 /* drap: mov %rsp, %rbp */
1899 regs[CFI_BP].base = CFI_BP;
1900 regs[CFI_BP].offset = -cfi->stack_size;
1901 cfi->bp_scratch = false;
1904 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1909 * This is needed for the rare case where GCC
1916 cfi->vals[op->dest.reg].base = CFI_CFA;
1917 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1920 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1921 cfa->base == CFI_BP) {
1926 * Restore the original stack pointer (Clang).
1928 cfi->stack_size = -cfi->regs[CFI_BP].offset;
1931 else if (op->dest.reg == cfa->base) {
1933 /* mov %reg, %rsp */
1934 if (cfa->base == CFI_SP &&
1935 cfi->vals[op->src.reg].base == CFI_CFA) {
1938 * This is needed for the rare case
1939 * where GCC does something dumb like:
1941 * lea 0x8(%rsp), %rcx
1945 cfa->offset = -cfi->vals[op->src.reg].offset;
1946 cfi->stack_size = cfa->offset;
1948 } else if (cfa->base == CFI_SP &&
1949 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
1950 cfi->vals[op->src.reg].offset == cfa->offset) {
1955 * 1: mov %rsp, (%[tos])
1956 * 2: mov %[tos], %rsp
1962 * 1 - places a pointer to the previous
1963 * stack at the Top-of-Stack of the
1966 * 2 - switches to the new stack.
1968 * 3 - pops the Top-of-Stack to restore
1969 * the original stack.
1971 * Note: we set base to SP_INDIRECT
1972 * here and preserve offset. Therefore
1973 * when the unwinder reaches ToS it
1974 * will dereference SP and then add the
1975 * offset to find the next frame, IOW:
1978 cfa->base = CFI_SP_INDIRECT;
1981 cfa->base = CFI_UNDEFINED;
1986 else if (op->dest.reg == CFI_SP &&
1987 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
1988 cfi->vals[op->src.reg].offset == cfa->offset) {
1991 * The same stack swizzle case 2) as above. But
1992 * because we can't change cfa->base, case 3)
1993 * will become a regular POP. Pretend we're a
1994 * PUSH so things don't go unbalanced.
1996 cfi->stack_size += 8;
2003 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2006 cfi->stack_size -= op->src.offset;
2007 if (cfa->base == CFI_SP)
2008 cfa->offset -= op->src.offset;
2012 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2014 /* lea disp(%rbp), %rsp */
2015 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2019 if (!cfi->drap && op->src.reg == CFI_SP &&
2020 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2021 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2023 /* lea disp(%rsp), %rbp */
2025 cfa->offset -= op->src.offset;
2026 cfi->bp_scratch = false;
2030 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2032 /* drap: lea disp(%rsp), %drap */
2033 cfi->drap_reg = op->dest.reg;
2036 * lea disp(%rsp), %reg
2038 * This is needed for the rare case where GCC
2039 * does something dumb like:
2041 * lea 0x8(%rsp), %rcx
2045 cfi->vals[op->dest.reg].base = CFI_CFA;
2046 cfi->vals[op->dest.reg].offset = \
2047 -cfi->stack_size + op->src.offset;
2052 if (cfi->drap && op->dest.reg == CFI_SP &&
2053 op->src.reg == cfi->drap_reg) {
2055 /* drap: lea disp(%drap), %rsp */
2057 cfa->offset = cfi->stack_size = -op->src.offset;
2058 cfi->drap_reg = CFI_UNDEFINED;
2063 if (op->dest.reg == cfi->cfa.base) {
2064 WARN_FUNC("unsupported stack register modification",
2065 insn->sec, insn->offset);
2072 if (op->dest.reg != CFI_SP ||
2073 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2074 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2075 WARN_FUNC("unsupported stack pointer realignment",
2076 insn->sec, insn->offset);
2080 if (cfi->drap_reg != CFI_UNDEFINED) {
2081 /* drap: and imm, %rsp */
2082 cfa->base = cfi->drap_reg;
2083 cfa->offset = cfi->stack_size = 0;
2088 * Older versions of GCC (4.8ish) realign the stack
2089 * without DRAP, with a frame pointer.
2096 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2098 /* pop %rsp; # restore from a stack swizzle */
2103 if (!cfi->drap && op->dest.reg == cfa->base) {
2109 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2110 op->dest.reg == cfi->drap_reg &&
2111 cfi->drap_offset == -cfi->stack_size) {
2113 /* drap: pop %drap */
2114 cfa->base = cfi->drap_reg;
2116 cfi->drap_offset = -1;
2118 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2121 restore_reg(cfi, op->dest.reg);
2124 cfi->stack_size -= 8;
2125 if (cfa->base == CFI_SP)
2130 case OP_SRC_REG_INDIRECT:
2131 if (!cfi->drap && op->dest.reg == cfa->base &&
2132 op->dest.reg == CFI_BP) {
2134 /* mov disp(%rsp), %rbp */
2136 cfa->offset = cfi->stack_size;
2139 if (cfi->drap && op->src.reg == CFI_BP &&
2140 op->src.offset == cfi->drap_offset) {
2142 /* drap: mov disp(%rbp), %drap */
2143 cfa->base = cfi->drap_reg;
2145 cfi->drap_offset = -1;
2148 if (cfi->drap && op->src.reg == CFI_BP &&
2149 op->src.offset == regs[op->dest.reg].offset) {
2151 /* drap: mov disp(%rbp), %reg */
2152 restore_reg(cfi, op->dest.reg);
2154 } else if (op->src.reg == cfa->base &&
2155 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2157 /* mov disp(%rbp), %reg */
2158 /* mov disp(%rsp), %reg */
2159 restore_reg(cfi, op->dest.reg);
2161 } else if (op->src.reg == CFI_SP &&
2162 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2164 /* mov disp(%rsp), %reg */
2165 restore_reg(cfi, op->dest.reg);
2171 WARN_FUNC("unknown stack-related instruction",
2172 insn->sec, insn->offset);
2180 cfi->stack_size += 8;
2181 if (cfa->base == CFI_SP)
2184 if (op->src.type != OP_SRC_REG)
2188 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2190 /* drap: push %drap */
2191 cfa->base = CFI_BP_INDIRECT;
2192 cfa->offset = -cfi->stack_size;
2194 /* save drap so we know when to restore it */
2195 cfi->drap_offset = -cfi->stack_size;
2197 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2199 /* drap: push %rbp */
2200 cfi->stack_size = 0;
2204 /* drap: push %reg */
2205 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2211 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2214 /* detect when asm code uses rbp as a scratch register */
2215 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2216 cfa->base != CFI_BP)
2217 cfi->bp_scratch = true;
2220 case OP_DEST_REG_INDIRECT:
2223 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2225 /* drap: mov %drap, disp(%rbp) */
2226 cfa->base = CFI_BP_INDIRECT;
2227 cfa->offset = op->dest.offset;
2229 /* save drap offset so we know when to restore it */
2230 cfi->drap_offset = op->dest.offset;
2233 /* drap: mov reg, disp(%rbp) */
2234 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2237 } else if (op->dest.reg == cfa->base) {
2239 /* mov reg, disp(%rbp) */
2240 /* mov reg, disp(%rsp) */
2241 save_reg(cfi, op->src.reg, CFI_CFA,
2242 op->dest.offset - cfi->cfa.offset);
2244 } else if (op->dest.reg == CFI_SP) {
2246 /* mov reg, disp(%rsp) */
2247 save_reg(cfi, op->src.reg, CFI_CFA,
2248 op->dest.offset - cfi->stack_size);
2250 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2252 /* mov %rsp, (%reg); # setup a stack swizzle. */
2253 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2254 cfi->vals[op->dest.reg].offset = cfa->offset;
2260 if ((!cfi->drap && cfa->base != CFI_BP) ||
2261 (cfi->drap && cfa->base != cfi->drap_reg)) {
2262 WARN_FUNC("leave instruction with modified stack frame",
2263 insn->sec, insn->offset);
2267 /* leave (mov %rbp, %rsp; pop %rbp) */
2269 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2270 restore_reg(cfi, CFI_BP);
2280 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2281 WARN_FUNC("unknown stack-related memory operation",
2282 insn->sec, insn->offset);
2287 cfi->stack_size -= 8;
2288 if (cfa->base == CFI_SP)
2294 WARN_FUNC("unknown stack-related instruction",
2295 insn->sec, insn->offset);
2303 * The stack layouts of alternatives instructions can sometimes diverge when
2304 * they have stack modifications. That's fine as long as the potential stack
2305 * layouts don't conflict at any given potential instruction boundary.
2307 * Flatten the CFIs of the different alternative code streams (both original
2308 * and replacement) into a single shared CFI array which can be used to detect
2309 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2311 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2313 struct cfi_state **alt_cfi;
2316 if (!insn->alt_group)
2319 alt_cfi = insn->alt_group->cfi;
2320 group_off = insn->offset - insn->alt_group->first_insn->offset;
2322 if (!alt_cfi[group_off]) {
2323 alt_cfi[group_off] = &insn->cfi;
2325 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2326 WARN_FUNC("stack layout conflict in alternatives",
2327 insn->sec, insn->offset);
2335 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2337 struct stack_op *op;
2339 list_for_each_entry(op, &insn->stack_ops, list) {
2341 if (update_cfi_state(insn, &state->cfi, op))
2344 if (op->dest.type == OP_DEST_PUSHF) {
2345 if (!state->uaccess_stack) {
2346 state->uaccess_stack = 1;
2347 } else if (state->uaccess_stack >> 31) {
2348 WARN_FUNC("PUSHF stack exhausted",
2349 insn->sec, insn->offset);
2352 state->uaccess_stack <<= 1;
2353 state->uaccess_stack |= state->uaccess;
2356 if (op->src.type == OP_SRC_POPF) {
2357 if (state->uaccess_stack) {
2358 state->uaccess = state->uaccess_stack & 1;
2359 state->uaccess_stack >>= 1;
2360 if (state->uaccess_stack == 1)
2361 state->uaccess_stack = 0;
2369 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2371 struct cfi_state *cfi1 = &insn->cfi;
2374 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2376 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2377 insn->sec, insn->offset,
2378 cfi1->cfa.base, cfi1->cfa.offset,
2379 cfi2->cfa.base, cfi2->cfa.offset);
2381 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2382 for (i = 0; i < CFI_NUM_REGS; i++) {
2383 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2384 sizeof(struct cfi_reg)))
2387 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2388 insn->sec, insn->offset,
2389 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2390 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2394 } else if (cfi1->type != cfi2->type) {
2396 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2397 insn->sec, insn->offset, cfi1->type, cfi2->type);
2399 } else if (cfi1->drap != cfi2->drap ||
2400 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2401 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2403 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2404 insn->sec, insn->offset,
2405 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2406 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2414 static inline bool func_uaccess_safe(struct symbol *func)
2417 return func->uaccess_safe;
2422 static inline const char *call_dest_name(struct instruction *insn)
2424 if (insn->call_dest)
2425 return insn->call_dest->name;
2430 static inline bool noinstr_call_dest(struct symbol *func)
2433 * We can't deal with indirect function calls at present;
2434 * assume they're instrumented.
2440 * If the symbol is from a noinstr section; we good.
2442 if (func->sec->noinstr)
2446 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2447 * something 'BAD' happened. At the risk of taking the machine down,
2448 * let them proceed to get the message out.
2450 if (!strncmp(func->name, "__ubsan_handle_", 15))
2456 static int validate_call(struct instruction *insn, struct insn_state *state)
2458 if (state->noinstr && state->instr <= 0 &&
2459 !noinstr_call_dest(insn->call_dest)) {
2460 WARN_FUNC("call to %s() leaves .noinstr.text section",
2461 insn->sec, insn->offset, call_dest_name(insn));
2465 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2466 WARN_FUNC("call to %s() with UACCESS enabled",
2467 insn->sec, insn->offset, call_dest_name(insn));
2472 WARN_FUNC("call to %s() with DF set",
2473 insn->sec, insn->offset, call_dest_name(insn));
2480 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2482 if (has_modified_stack_frame(insn, state)) {
2483 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2484 insn->sec, insn->offset);
2488 return validate_call(insn, state);
2491 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2493 if (state->noinstr && state->instr > 0) {
2494 WARN_FUNC("return with instrumentation enabled",
2495 insn->sec, insn->offset);
2499 if (state->uaccess && !func_uaccess_safe(func)) {
2500 WARN_FUNC("return with UACCESS enabled",
2501 insn->sec, insn->offset);
2505 if (!state->uaccess && func_uaccess_safe(func)) {
2506 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2507 insn->sec, insn->offset);
2512 WARN_FUNC("return with DF set",
2513 insn->sec, insn->offset);
2517 if (func && has_modified_stack_frame(insn, state)) {
2518 WARN_FUNC("return with modified stack frame",
2519 insn->sec, insn->offset);
2523 if (state->cfi.bp_scratch) {
2524 WARN_FUNC("BP used as a scratch register",
2525 insn->sec, insn->offset);
2532 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2533 struct instruction *insn)
2535 struct alt_group *alt_group = insn->alt_group;
2538 * Simulate the fact that alternatives are patched in-place. When the
2539 * end of a replacement alt_group is reached, redirect objtool flow to
2540 * the end of the original alt_group.
2542 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2543 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2545 return next_insn_same_sec(file, insn);
2549 * Follow the branch starting at the given instruction, and recursively follow
2550 * any other branches (jumps). Meanwhile, track the frame pointer state at
2551 * each instruction and validate all the rules described in
2552 * tools/objtool/Documentation/stack-validation.txt.
2554 static int validate_branch(struct objtool_file *file, struct symbol *func,
2555 struct instruction *insn, struct insn_state state)
2557 struct alternative *alt;
2558 struct instruction *next_insn;
2559 struct section *sec;
2566 next_insn = next_insn_to_validate(file, insn);
2568 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2569 WARN("%s() falls through to next function %s()",
2570 func->name, insn->func->name);
2574 if (func && insn->ignore) {
2575 WARN_FUNC("BUG: why am I validating an ignored function?",
2580 visited = 1 << state.uaccess;
2581 if (insn->visited) {
2582 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2585 if (insn->visited & visited)
2590 state.instr += insn->instr;
2593 state.cfi = insn->cfi;
2595 insn->cfi = state.cfi;
2597 insn->visited |= visited;
2599 if (propagate_alt_cfi(file, insn))
2602 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2603 bool skip_orig = false;
2605 list_for_each_entry(alt, &insn->alts, list) {
2609 ret = validate_branch(file, func, alt->insn, state);
2612 BT_FUNC("(alt)", insn);
2621 if (handle_insn_ops(insn, &state))
2624 switch (insn->type) {
2627 return validate_return(func, insn, &state);
2630 case INSN_CALL_DYNAMIC:
2631 ret = validate_call(insn, &state);
2635 if (!no_fp && func && !is_fentry_call(insn) &&
2636 !has_valid_stack_frame(&state)) {
2637 WARN_FUNC("call without frame pointer save/setup",
2642 if (dead_end_function(file, insn->call_dest))
2645 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2646 list_add_tail(&insn->static_call_node,
2647 &file->static_call_list);
2652 case INSN_JUMP_CONDITIONAL:
2653 case INSN_JUMP_UNCONDITIONAL:
2654 if (is_sibling_call(insn)) {
2655 ret = validate_sibling_call(insn, &state);
2659 } else if (insn->jump_dest) {
2660 ret = validate_branch(file, func,
2661 insn->jump_dest, state);
2664 BT_FUNC("(branch)", insn);
2669 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2674 case INSN_JUMP_DYNAMIC:
2675 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2676 if (is_sibling_call(insn)) {
2677 ret = validate_sibling_call(insn, &state);
2682 if (insn->type == INSN_JUMP_DYNAMIC)
2687 case INSN_CONTEXT_SWITCH:
2688 if (func && (!next_insn || !next_insn->hint)) {
2689 WARN_FUNC("unsupported instruction in callable function",
2696 if (state.uaccess) {
2697 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2701 state.uaccess = true;
2705 if (!state.uaccess && func) {
2706 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2710 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2711 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2715 state.uaccess = false;
2720 WARN_FUNC("recursive STD", sec, insn->offset);
2728 if (!state.df && func) {
2729 WARN_FUNC("redundant CLD", sec, insn->offset);
2744 if (state.cfi.cfa.base == CFI_UNDEFINED)
2746 WARN("%s: unexpected end of section", sec->name);
2756 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2758 struct instruction *insn;
2759 struct insn_state state;
2760 int ret, warnings = 0;
2765 init_insn_state(&state, sec);
2768 insn = find_insn(file, sec, 0);
2772 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2775 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2776 if (insn->hint && !insn->visited) {
2777 ret = validate_branch(file, insn->func, insn, state);
2778 if (ret && backtrace)
2779 BT_FUNC("<=== (hint)", insn);
2783 insn = list_next_entry(insn, list);
2789 static int validate_retpoline(struct objtool_file *file)
2791 struct instruction *insn;
2794 for_each_insn(file, insn) {
2795 if (insn->type != INSN_JUMP_DYNAMIC &&
2796 insn->type != INSN_CALL_DYNAMIC)
2799 if (insn->retpoline_safe)
2803 * .init.text code is ran before userspace and thus doesn't
2804 * strictly need retpolines, except for modules which are
2805 * loaded late, they very much do need retpoline in their
2808 if (!strcmp(insn->sec->name, ".init.text") && !module)
2811 WARN_FUNC("indirect %s found in RETPOLINE build",
2812 insn->sec, insn->offset,
2813 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2821 static bool is_kasan_insn(struct instruction *insn)
2823 return (insn->type == INSN_CALL &&
2824 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2827 static bool is_ubsan_insn(struct instruction *insn)
2829 return (insn->type == INSN_CALL &&
2830 !strcmp(insn->call_dest->name,
2831 "__ubsan_handle_builtin_unreachable"));
2834 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
2837 struct instruction *prev_insn;
2839 if (insn->ignore || insn->type == INSN_NOP)
2843 * Ignore any unused exceptions. This can happen when a whitelisted
2844 * function has an exception table entry.
2846 * Also ignore alternative replacement instructions. This can happen
2847 * when a whitelisted function uses one of the ALTERNATIVE macros.
2849 if (!strcmp(insn->sec->name, ".fixup") ||
2850 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2851 !strcmp(insn->sec->name, ".altinstr_aux"))
2858 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2859 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2860 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2861 * (or occasionally a JMP to UD2).
2863 * It may also insert a UD2 after calling a __noreturn function.
2865 prev_insn = list_prev_entry(insn, list);
2866 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
2867 (insn->type == INSN_BUG ||
2868 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2869 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2873 * Check if this (or a subsequent) instruction is related to
2874 * CONFIG_UBSAN or CONFIG_KASAN.
2876 * End the search at 5 instructions to avoid going into the weeds.
2878 for (i = 0; i < 5; i++) {
2880 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2883 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2884 if (insn->jump_dest &&
2885 insn->jump_dest->func == insn->func) {
2886 insn = insn->jump_dest;
2893 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2896 insn = list_next_entry(insn, list);
2902 static int validate_symbol(struct objtool_file *file, struct section *sec,
2903 struct symbol *sym, struct insn_state *state)
2905 struct instruction *insn;
2909 WARN("%s() is missing an ELF size annotation", sym->name);
2913 if (sym->pfunc != sym || sym->alias != sym)
2916 insn = find_insn(file, sec, sym->offset);
2917 if (!insn || insn->ignore || insn->visited)
2920 state->uaccess = sym->uaccess_safe;
2922 ret = validate_branch(file, insn->func, insn, *state);
2923 if (ret && backtrace)
2924 BT_FUNC("<=== (sym)", insn);
2928 static int validate_section(struct objtool_file *file, struct section *sec)
2930 struct insn_state state;
2931 struct symbol *func;
2934 list_for_each_entry(func, &sec->symbol_list, list) {
2935 if (func->type != STT_FUNC)
2938 init_insn_state(&state, sec);
2939 set_func_state(&state.cfi);
2941 warnings += validate_symbol(file, sec, func, &state);
2947 static int validate_vmlinux_functions(struct objtool_file *file)
2949 struct section *sec;
2952 sec = find_section_by_name(file->elf, ".noinstr.text");
2954 warnings += validate_section(file, sec);
2955 warnings += validate_unwind_hints(file, sec);
2958 sec = find_section_by_name(file->elf, ".entry.text");
2960 warnings += validate_section(file, sec);
2961 warnings += validate_unwind_hints(file, sec);
2967 static int validate_functions(struct objtool_file *file)
2969 struct section *sec;
2972 for_each_sec(file, sec) {
2973 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2976 warnings += validate_section(file, sec);
2982 static int validate_reachable_instructions(struct objtool_file *file)
2984 struct instruction *insn;
2986 if (file->ignore_unreachables)
2989 for_each_insn(file, insn) {
2990 if (insn->visited || ignore_unreachable_insn(file, insn))
2993 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3000 int check(struct objtool_file *file)
3002 int ret, warnings = 0;
3004 arch_initial_func_cfi_state(&initial_func_cfi);
3006 ret = decode_sections(file);
3011 if (list_empty(&file->insn_list))
3014 if (vmlinux && !validate_dup) {
3015 ret = validate_vmlinux_functions(file);
3024 ret = validate_retpoline(file);
3030 ret = validate_functions(file);
3035 ret = validate_unwind_hints(file, NULL);
3041 ret = validate_reachable_instructions(file);
3047 ret = create_static_call_sections(file);
3054 * For now, don't fail the kernel build on fatal warnings. These
3055 * errors are still fairly common due to the growing matrix of
3056 * supported toolchains and their recent pace of change.