1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
17 #include <linux/hashtable.h>
18 #include <linux/kernel.h>
19 #include <linux/static_call_types.h>
21 #define FAKE_JUMP_OFFSET -1
23 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
26 struct list_head list;
27 struct instruction *insn;
32 struct cfi_init_state initial_func_cfi;
34 struct instruction *find_insn(struct objtool_file *file,
35 struct section *sec, unsigned long offset)
37 struct instruction *insn;
39 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
40 if (insn->sec == sec && insn->offset == offset)
47 static struct instruction *next_insn_same_sec(struct objtool_file *file,
48 struct instruction *insn)
50 struct instruction *next = list_next_entry(insn, list);
52 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
58 static struct instruction *next_insn_same_func(struct objtool_file *file,
59 struct instruction *insn)
61 struct instruction *next = list_next_entry(insn, list);
62 struct symbol *func = insn->func;
67 if (&next->list != &file->insn_list && next->func == func)
70 /* Check if we're already in the subfunction: */
71 if (func == func->cfunc)
74 /* Move to the subfunction: */
75 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
78 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
79 struct instruction *insn)
81 struct instruction *prev = list_prev_entry(insn, list);
83 if (&prev->list != &file->insn_list && prev->func == insn->func)
89 #define func_for_each_insn(file, func, insn) \
90 for (insn = find_insn(file, func->sec, func->offset); \
92 insn = next_insn_same_func(file, insn))
94 #define sym_for_each_insn(file, sym, insn) \
95 for (insn = find_insn(file, sym->sec, sym->offset); \
96 insn && &insn->list != &file->insn_list && \
97 insn->sec == sym->sec && \
98 insn->offset < sym->offset + sym->len; \
99 insn = list_next_entry(insn, list))
101 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
102 for (insn = list_prev_entry(insn, list); \
103 &insn->list != &file->insn_list && \
104 insn->sec == sym->sec && insn->offset >= sym->offset; \
105 insn = list_prev_entry(insn, list))
107 #define sec_for_each_insn_from(file, insn) \
108 for (; insn; insn = next_insn_same_sec(file, insn))
110 #define sec_for_each_insn_continue(file, insn) \
111 for (insn = next_insn_same_sec(file, insn); insn; \
112 insn = next_insn_same_sec(file, insn))
114 static bool is_static_jump(struct instruction *insn)
116 return insn->type == INSN_JUMP_CONDITIONAL ||
117 insn->type == INSN_JUMP_UNCONDITIONAL;
120 static bool is_sibling_call(struct instruction *insn)
122 /* An indirect jump is either a sibling call or a jump to a table. */
123 if (insn->type == INSN_JUMP_DYNAMIC)
124 return list_empty(&insn->alts);
126 if (!is_static_jump(insn))
129 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
130 return !!insn->call_dest;
134 * This checks to see if the given function is a "noreturn" function.
136 * For global functions which are outside the scope of this object file, we
137 * have to keep a manual list of them.
139 * For local functions, we have to detect them manually by simply looking for
140 * the lack of a return instruction.
142 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
146 struct instruction *insn;
150 * Unfortunately these have to be hard coded because the noreturn
151 * attribute isn't provided in ELF data.
153 static const char * const global_noreturns[] = {
158 "__module_put_and_exit",
164 "machine_real_restart",
165 "rewind_stack_do_exit",
166 "kunit_try_catch_throw",
172 if (func->bind == STB_WEAK)
175 if (func->bind == STB_GLOBAL)
176 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
177 if (!strcmp(func->name, global_noreturns[i]))
183 insn = find_insn(file, func->sec, func->offset);
187 func_for_each_insn(file, func, insn) {
190 if (insn->type == INSN_RETURN)
198 * A function can have a sibling call instead of a return. In that
199 * case, the function's dead-end status depends on whether the target
200 * of the sibling call returns.
202 func_for_each_insn(file, func, insn) {
203 if (is_sibling_call(insn)) {
204 struct instruction *dest = insn->jump_dest;
207 /* sibling call to another file */
210 /* local sibling call */
211 if (recursion == 5) {
213 * Infinite recursion: two functions have
214 * sibling calls to each other. This is a very
215 * rare case. It means they aren't dead ends.
220 return __dead_end_function(file, dest->func, recursion+1);
227 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
229 return __dead_end_function(file, func, 0);
232 static void init_cfi_state(struct cfi_state *cfi)
236 for (i = 0; i < CFI_NUM_REGS; i++) {
237 cfi->regs[i].base = CFI_UNDEFINED;
238 cfi->vals[i].base = CFI_UNDEFINED;
240 cfi->cfa.base = CFI_UNDEFINED;
241 cfi->drap_reg = CFI_UNDEFINED;
242 cfi->drap_offset = -1;
245 static void init_insn_state(struct insn_state *state, struct section *sec)
247 memset(state, 0, sizeof(*state));
248 init_cfi_state(&state->cfi);
251 * We need the full vmlinux for noinstr validation, otherwise we can
252 * not correctly determine insn->call_dest->sec (external symbols do
253 * not have a section).
256 state->noinstr = sec->noinstr;
260 * Call the arch-specific instruction decoder for all the instructions and add
261 * them to the global instruction list.
263 static int decode_instructions(struct objtool_file *file)
267 unsigned long offset;
268 struct instruction *insn;
269 unsigned long nr_insns = 0;
272 for_each_sec(file, sec) {
274 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
277 if (strcmp(sec->name, ".altinstr_replacement") &&
278 strcmp(sec->name, ".altinstr_aux") &&
279 strncmp(sec->name, ".discard.", 9))
282 if (!strcmp(sec->name, ".noinstr.text") ||
283 !strcmp(sec->name, ".entry.text"))
286 for (offset = 0; offset < sec->len; offset += insn->len) {
287 insn = malloc(sizeof(*insn));
289 WARN("malloc failed");
292 memset(insn, 0, sizeof(*insn));
293 INIT_LIST_HEAD(&insn->alts);
294 INIT_LIST_HEAD(&insn->stack_ops);
295 init_cfi_state(&insn->cfi);
298 insn->offset = offset;
300 ret = arch_decode_instruction(file->elf, sec, offset,
302 &insn->len, &insn->type,
308 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
309 list_add_tail(&insn->list, &file->insn_list);
313 list_for_each_entry(func, &sec->symbol_list, list) {
314 if (func->type != STT_FUNC || func->alias != func)
317 if (!find_insn(file, sec, func->offset)) {
318 WARN("%s(): can't find starting instruction",
323 sym_for_each_insn(file, func, insn)
329 printf("nr_insns: %lu\n", nr_insns);
338 static struct instruction *find_last_insn(struct objtool_file *file,
341 struct instruction *insn = NULL;
343 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
345 for (offset = sec->len - 1; offset >= end && !insn; offset--)
346 insn = find_insn(file, sec, offset);
352 * Mark "ud2" instructions and manually annotated dead ends.
354 static int add_dead_ends(struct objtool_file *file)
358 struct instruction *insn;
361 * By default, "ud2" is a dead end unless otherwise annotated, because
362 * GCC 7 inserts it for certain divide-by-zero cases.
364 for_each_insn(file, insn)
365 if (insn->type == INSN_BUG)
366 insn->dead_end = true;
369 * Check for manually annotated dead ends.
371 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
375 list_for_each_entry(reloc, &sec->reloc_list, list) {
376 if (reloc->sym->type != STT_SECTION) {
377 WARN("unexpected relocation symbol type in %s", sec->name);
380 insn = find_insn(file, reloc->sym->sec, reloc->addend);
382 insn = list_prev_entry(insn, list);
383 else if (reloc->addend == reloc->sym->sec->len) {
384 insn = find_last_insn(file, reloc->sym->sec);
386 WARN("can't find unreachable insn at %s+0x%x",
387 reloc->sym->sec->name, reloc->addend);
391 WARN("can't find unreachable insn at %s+0x%x",
392 reloc->sym->sec->name, reloc->addend);
396 insn->dead_end = true;
401 * These manually annotated reachable checks are needed for GCC 4.4,
402 * where the Linux unreachable() macro isn't supported. In that case
403 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
406 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
410 list_for_each_entry(reloc, &sec->reloc_list, list) {
411 if (reloc->sym->type != STT_SECTION) {
412 WARN("unexpected relocation symbol type in %s", sec->name);
415 insn = find_insn(file, reloc->sym->sec, reloc->addend);
417 insn = list_prev_entry(insn, list);
418 else if (reloc->addend == reloc->sym->sec->len) {
419 insn = find_last_insn(file, reloc->sym->sec);
421 WARN("can't find reachable insn at %s+0x%x",
422 reloc->sym->sec->name, reloc->addend);
426 WARN("can't find reachable insn at %s+0x%x",
427 reloc->sym->sec->name, reloc->addend);
431 insn->dead_end = false;
437 static int create_static_call_sections(struct objtool_file *file)
439 struct section *sec, *reloc_sec;
441 struct static_call_site *site;
442 struct instruction *insn;
443 struct symbol *key_sym;
444 char *key_name, *tmp;
447 sec = find_section_by_name(file->elf, ".static_call_sites");
449 INIT_LIST_HEAD(&file->static_call_list);
450 WARN("file already has .static_call_sites section, skipping");
454 if (list_empty(&file->static_call_list))
458 list_for_each_entry(insn, &file->static_call_list, static_call_node)
461 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
462 sizeof(struct static_call_site), idx);
466 reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
471 list_for_each_entry(insn, &file->static_call_list, static_call_node) {
473 site = (struct static_call_site *)sec->data->d_buf + idx;
474 memset(site, 0, sizeof(struct static_call_site));
476 /* populate reloc for 'addr' */
477 reloc = malloc(sizeof(*reloc));
482 memset(reloc, 0, sizeof(*reloc));
483 reloc->sym = insn->sec->sym;
484 reloc->addend = insn->offset;
485 reloc->type = R_X86_64_PC32;
486 reloc->offset = idx * sizeof(struct static_call_site);
487 reloc->sec = reloc_sec;
488 elf_add_reloc(file->elf, reloc);
490 /* find key symbol */
491 key_name = strdup(insn->call_dest->name);
496 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
497 STATIC_CALL_TRAMP_PREFIX_LEN)) {
498 WARN("static_call: trampoline name malformed: %s", key_name);
501 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
502 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
504 key_sym = find_symbol_by_name(file->elf, tmp);
506 WARN("static_call: can't find static_call_key symbol: %s", tmp);
511 /* populate reloc for 'key' */
512 reloc = malloc(sizeof(*reloc));
517 memset(reloc, 0, sizeof(*reloc));
518 reloc->sym = key_sym;
519 reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
520 reloc->type = R_X86_64_PC32;
521 reloc->offset = idx * sizeof(struct static_call_site) + 4;
522 reloc->sec = reloc_sec;
523 elf_add_reloc(file->elf, reloc);
528 if (elf_rebuild_reloc_section(file->elf, reloc_sec))
535 * Warnings shouldn't be reported for ignored functions.
537 static void add_ignores(struct objtool_file *file)
539 struct instruction *insn;
544 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
548 list_for_each_entry(reloc, &sec->reloc_list, list) {
549 switch (reloc->sym->type) {
555 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
561 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
565 func_for_each_insn(file, func, insn)
571 * This is a whitelist of functions that is allowed to be called with AC set.
572 * The list is meant to be minimal and only contains compiler instrumentation
573 * ABI and a few functions used to implement *_{to,from}_user() functions.
575 * These functions must not directly change AC, but may PUSHF/POPF.
577 static const char *uaccess_safe_builtin[] = {
580 "check_memory_region",
581 /* KASAN out-of-line */
582 "__asan_loadN_noabort",
583 "__asan_load1_noabort",
584 "__asan_load2_noabort",
585 "__asan_load4_noabort",
586 "__asan_load8_noabort",
587 "__asan_load16_noabort",
588 "__asan_storeN_noabort",
589 "__asan_store1_noabort",
590 "__asan_store2_noabort",
591 "__asan_store4_noabort",
592 "__asan_store8_noabort",
593 "__asan_store16_noabort",
595 "__asan_report_load_n_noabort",
596 "__asan_report_load1_noabort",
597 "__asan_report_load2_noabort",
598 "__asan_report_load4_noabort",
599 "__asan_report_load8_noabort",
600 "__asan_report_load16_noabort",
601 "__asan_report_store_n_noabort",
602 "__asan_report_store1_noabort",
603 "__asan_report_store2_noabort",
604 "__asan_report_store4_noabort",
605 "__asan_report_store8_noabort",
606 "__asan_report_store16_noabort",
608 "__kcsan_check_access",
609 "kcsan_found_watchpoint",
610 "kcsan_setup_watchpoint",
611 "kcsan_check_scoped_accesses",
612 "kcsan_disable_current",
613 "kcsan_enable_current_nowarn",
618 "__tsan_write_range",
629 "__tsan_read_write1",
630 "__tsan_read_write2",
631 "__tsan_read_write4",
632 "__tsan_read_write8",
633 "__tsan_read_write16",
634 "__tsan_atomic8_load",
635 "__tsan_atomic16_load",
636 "__tsan_atomic32_load",
637 "__tsan_atomic64_load",
638 "__tsan_atomic8_store",
639 "__tsan_atomic16_store",
640 "__tsan_atomic32_store",
641 "__tsan_atomic64_store",
642 "__tsan_atomic8_exchange",
643 "__tsan_atomic16_exchange",
644 "__tsan_atomic32_exchange",
645 "__tsan_atomic64_exchange",
646 "__tsan_atomic8_fetch_add",
647 "__tsan_atomic16_fetch_add",
648 "__tsan_atomic32_fetch_add",
649 "__tsan_atomic64_fetch_add",
650 "__tsan_atomic8_fetch_sub",
651 "__tsan_atomic16_fetch_sub",
652 "__tsan_atomic32_fetch_sub",
653 "__tsan_atomic64_fetch_sub",
654 "__tsan_atomic8_fetch_and",
655 "__tsan_atomic16_fetch_and",
656 "__tsan_atomic32_fetch_and",
657 "__tsan_atomic64_fetch_and",
658 "__tsan_atomic8_fetch_or",
659 "__tsan_atomic16_fetch_or",
660 "__tsan_atomic32_fetch_or",
661 "__tsan_atomic64_fetch_or",
662 "__tsan_atomic8_fetch_xor",
663 "__tsan_atomic16_fetch_xor",
664 "__tsan_atomic32_fetch_xor",
665 "__tsan_atomic64_fetch_xor",
666 "__tsan_atomic8_fetch_nand",
667 "__tsan_atomic16_fetch_nand",
668 "__tsan_atomic32_fetch_nand",
669 "__tsan_atomic64_fetch_nand",
670 "__tsan_atomic8_compare_exchange_strong",
671 "__tsan_atomic16_compare_exchange_strong",
672 "__tsan_atomic32_compare_exchange_strong",
673 "__tsan_atomic64_compare_exchange_strong",
674 "__tsan_atomic8_compare_exchange_weak",
675 "__tsan_atomic16_compare_exchange_weak",
676 "__tsan_atomic32_compare_exchange_weak",
677 "__tsan_atomic64_compare_exchange_weak",
678 "__tsan_atomic8_compare_exchange_val",
679 "__tsan_atomic16_compare_exchange_val",
680 "__tsan_atomic32_compare_exchange_val",
681 "__tsan_atomic64_compare_exchange_val",
682 "__tsan_atomic_thread_fence",
683 "__tsan_atomic_signal_fence",
687 "__sanitizer_cov_trace_pc",
688 "__sanitizer_cov_trace_const_cmp1",
689 "__sanitizer_cov_trace_const_cmp2",
690 "__sanitizer_cov_trace_const_cmp4",
691 "__sanitizer_cov_trace_const_cmp8",
692 "__sanitizer_cov_trace_cmp1",
693 "__sanitizer_cov_trace_cmp2",
694 "__sanitizer_cov_trace_cmp4",
695 "__sanitizer_cov_trace_cmp8",
696 "__sanitizer_cov_trace_switch",
698 "ubsan_type_mismatch_common",
699 "__ubsan_handle_type_mismatch",
700 "__ubsan_handle_type_mismatch_v1",
701 "__ubsan_handle_shift_out_of_bounds",
703 "csum_partial_copy_generic",
705 "copy_mc_fragile_handle_tail",
706 "copy_mc_enhanced_fast_string",
707 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
711 static void add_uaccess_safe(struct objtool_file *file)
719 for (name = uaccess_safe_builtin; *name; name++) {
720 func = find_symbol_by_name(file->elf, *name);
724 func->uaccess_safe = true;
729 * FIXME: For now, just ignore any alternatives which add retpolines. This is
730 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
731 * But it at least allows objtool to understand the control flow *around* the
734 static int add_ignore_alternatives(struct objtool_file *file)
738 struct instruction *insn;
740 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
744 list_for_each_entry(reloc, &sec->reloc_list, list) {
745 if (reloc->sym->type != STT_SECTION) {
746 WARN("unexpected relocation symbol type in %s", sec->name);
750 insn = find_insn(file, reloc->sym->sec, reloc->addend);
752 WARN("bad .discard.ignore_alts entry");
756 insn->ignore_alts = true;
763 * Find the destination instructions for all jumps.
765 static int add_jump_destinations(struct objtool_file *file)
767 struct instruction *insn;
769 struct section *dest_sec;
770 unsigned long dest_off;
772 for_each_insn(file, insn) {
773 if (!is_static_jump(insn))
776 if (insn->offset == FAKE_JUMP_OFFSET)
779 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
780 insn->offset, insn->len);
782 dest_sec = insn->sec;
783 dest_off = arch_jump_destination(insn);
784 } else if (reloc->sym->type == STT_SECTION) {
785 dest_sec = reloc->sym->sec;
786 dest_off = arch_dest_reloc_offset(reloc->addend);
787 } else if (reloc->sym->sec->idx) {
788 dest_sec = reloc->sym->sec;
789 dest_off = reloc->sym->sym.st_value +
790 arch_dest_reloc_offset(reloc->addend);
791 } else if (strstr(reloc->sym->name, "_indirect_thunk_")) {
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;
804 /* external sibling call */
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);
813 insn->jump_dest = find_insn(file, dest_sec, dest_off);
814 if (!insn->jump_dest) {
817 * This is a special case where an alt instruction
818 * jumps past the end of the section. These are
819 * handled later in handle_group_alt().
821 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
824 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
825 insn->sec, insn->offset, dest_sec->name,
831 * Cross-function jump.
833 if (insn->func && insn->jump_dest->func &&
834 insn->func != insn->jump_dest->func) {
837 * For GCC 8+, create parent/child links for any cold
838 * subfunctions. This is _mostly_ redundant with a
839 * similar initialization in read_symbols().
841 * If a function has aliases, we want the *first* such
842 * function in the symbol table to be the subfunction's
843 * parent. In that case we overwrite the
844 * initialization done in read_symbols().
846 * However this code can't completely replace the
847 * read_symbols() code because this doesn't detect the
848 * case where the parent function's only reference to a
849 * subfunction is through a jump table.
851 if (!strstr(insn->func->name, ".cold.") &&
852 strstr(insn->jump_dest->func->name, ".cold.")) {
853 insn->func->cfunc = insn->jump_dest->func;
854 insn->jump_dest->func->pfunc = insn->func;
856 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
857 insn->jump_dest->offset == insn->jump_dest->func->offset) {
859 /* internal sibling call */
860 insn->call_dest = insn->jump_dest->func;
861 if (insn->call_dest->static_call_tramp) {
862 list_add_tail(&insn->static_call_node,
863 &file->static_call_list);
872 static void remove_insn_ops(struct instruction *insn)
874 struct stack_op *op, *tmp;
876 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
883 * Find the destination instructions for all calls.
885 static int add_call_destinations(struct objtool_file *file)
887 struct instruction *insn;
888 unsigned long dest_off;
891 for_each_insn(file, insn) {
892 if (insn->type != INSN_CALL)
895 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
896 insn->offset, insn->len);
898 dest_off = arch_jump_destination(insn);
899 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
900 if (!insn->call_dest)
901 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
906 if (!insn->call_dest) {
907 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
911 if (insn->func && insn->call_dest->type != STT_FUNC) {
912 WARN_FUNC("unsupported call to non-function",
913 insn->sec, insn->offset);
917 } else if (reloc->sym->type == STT_SECTION) {
918 dest_off = arch_dest_reloc_offset(reloc->addend);
919 insn->call_dest = find_func_by_offset(reloc->sym->sec,
921 if (!insn->call_dest) {
922 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
923 insn->sec, insn->offset,
924 reloc->sym->sec->name,
929 insn->call_dest = reloc->sym;
932 * Many compilers cannot disable KCOV with a function attribute
933 * so they need a little help, NOP out any KCOV calls from noinstr
936 if (insn->sec->noinstr &&
937 !strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
939 reloc->type = R_NONE;
940 elf_write_reloc(file->elf, reloc);
943 elf_write_insn(file->elf, insn->sec,
944 insn->offset, insn->len,
945 arch_nop_insn(insn->len));
946 insn->type = INSN_NOP;
950 * Whatever stack impact regular CALLs have, should be undone
951 * by the RETURN of the called function.
953 * Annotated intra-function calls retain the stack_ops but
954 * are converted to JUMP, see read_intra_function_calls().
956 remove_insn_ops(insn);
963 * The .alternatives section requires some extra special care, over and above
964 * what other special sections require:
966 * 1. Because alternatives are patched in-place, we need to insert a fake jump
967 * instruction at the end so that validate_branch() skips all the original
968 * replaced instructions when validating the new instruction path.
970 * 2. An added wrinkle is that the new instruction length might be zero. In
971 * that case the old instructions are replaced with noops. We simulate that
972 * by creating a fake jump as the only new instruction.
974 * 3. In some cases, the alternative section includes an instruction which
975 * conditionally jumps to the _end_ of the entry. We have to modify these
976 * jumps' destinations to point back to .text rather than the end of the
977 * entry in .altinstr_replacement.
979 static int handle_group_alt(struct objtool_file *file,
980 struct special_alt *special_alt,
981 struct instruction *orig_insn,
982 struct instruction **new_insn)
984 static unsigned int alt_group_next_index = 1;
985 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
986 unsigned int alt_group = alt_group_next_index++;
987 unsigned long dest_off;
989 last_orig_insn = NULL;
991 sec_for_each_insn_from(file, insn) {
992 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
995 insn->alt_group = alt_group;
996 last_orig_insn = insn;
999 if (next_insn_same_sec(file, last_orig_insn)) {
1000 fake_jump = malloc(sizeof(*fake_jump));
1002 WARN("malloc failed");
1005 memset(fake_jump, 0, sizeof(*fake_jump));
1006 INIT_LIST_HEAD(&fake_jump->alts);
1007 INIT_LIST_HEAD(&fake_jump->stack_ops);
1008 init_cfi_state(&fake_jump->cfi);
1010 fake_jump->sec = special_alt->new_sec;
1011 fake_jump->offset = FAKE_JUMP_OFFSET;
1012 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
1013 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
1014 fake_jump->func = orig_insn->func;
1017 if (!special_alt->new_len) {
1019 WARN("%s: empty alternative at end of section",
1020 special_alt->orig_sec->name);
1024 *new_insn = fake_jump;
1028 last_new_insn = NULL;
1029 alt_group = alt_group_next_index++;
1031 sec_for_each_insn_from(file, insn) {
1032 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1035 last_new_insn = insn;
1037 insn->ignore = orig_insn->ignore_alts;
1038 insn->func = orig_insn->func;
1039 insn->alt_group = alt_group;
1042 * Since alternative replacement code is copy/pasted by the
1043 * kernel after applying relocations, generally such code can't
1044 * have relative-address relocation references to outside the
1045 * .altinstr_replacement section, unless the arch's
1046 * alternatives code can adjust the relative offsets
1049 * The x86 alternatives code adjusts the offsets only when it
1050 * encounters a branch instruction at the very beginning of the
1051 * replacement group.
1053 if ((insn->offset != special_alt->new_off ||
1054 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
1055 find_reloc_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
1057 WARN_FUNC("unsupported relocation in alternatives section",
1058 insn->sec, insn->offset);
1062 if (!is_static_jump(insn))
1065 if (!insn->immediate)
1068 dest_off = arch_jump_destination(insn);
1069 if (dest_off == special_alt->new_off + special_alt->new_len) {
1071 WARN("%s: alternative jump to end of section",
1072 special_alt->orig_sec->name);
1075 insn->jump_dest = fake_jump;
1078 if (!insn->jump_dest) {
1079 WARN_FUNC("can't find alternative jump destination",
1080 insn->sec, insn->offset);
1085 if (!last_new_insn) {
1086 WARN_FUNC("can't find last new alternative instruction",
1087 special_alt->new_sec, special_alt->new_off);
1092 list_add(&fake_jump->list, &last_new_insn->list);
1098 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1099 * If the original instruction is a jump, make the alt entry an effective nop
1100 * by just skipping the original instruction.
1102 static int handle_jump_alt(struct objtool_file *file,
1103 struct special_alt *special_alt,
1104 struct instruction *orig_insn,
1105 struct instruction **new_insn)
1107 if (orig_insn->type == INSN_NOP)
1110 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1111 WARN_FUNC("unsupported instruction at jump label",
1112 orig_insn->sec, orig_insn->offset);
1116 *new_insn = list_next_entry(orig_insn, list);
1121 * Read all the special sections which have alternate instructions which can be
1122 * patched in or redirected to at runtime. Each instruction having alternate
1123 * instruction(s) has them added to its insn->alts list, which will be
1124 * traversed in validate_branch().
1126 static int add_special_section_alts(struct objtool_file *file)
1128 struct list_head special_alts;
1129 struct instruction *orig_insn, *new_insn;
1130 struct special_alt *special_alt, *tmp;
1131 struct alternative *alt;
1134 ret = special_get_alts(file->elf, &special_alts);
1138 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1140 orig_insn = find_insn(file, special_alt->orig_sec,
1141 special_alt->orig_off);
1143 WARN_FUNC("special: can't find orig instruction",
1144 special_alt->orig_sec, special_alt->orig_off);
1150 if (!special_alt->group || special_alt->new_len) {
1151 new_insn = find_insn(file, special_alt->new_sec,
1152 special_alt->new_off);
1154 WARN_FUNC("special: can't find new instruction",
1155 special_alt->new_sec,
1156 special_alt->new_off);
1162 if (special_alt->group) {
1163 if (!special_alt->orig_len) {
1164 WARN_FUNC("empty alternative entry",
1165 orig_insn->sec, orig_insn->offset);
1169 ret = handle_group_alt(file, special_alt, orig_insn,
1173 } else if (special_alt->jump_or_nop) {
1174 ret = handle_jump_alt(file, special_alt, orig_insn,
1180 alt = malloc(sizeof(*alt));
1182 WARN("malloc failed");
1187 alt->insn = new_insn;
1188 alt->skip_orig = special_alt->skip_orig;
1189 orig_insn->ignore_alts |= special_alt->skip_alt;
1190 list_add_tail(&alt->list, &orig_insn->alts);
1192 list_del(&special_alt->list);
1200 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1201 struct reloc *table)
1203 struct reloc *reloc = table;
1204 struct instruction *dest_insn;
1205 struct alternative *alt;
1206 struct symbol *pfunc = insn->func->pfunc;
1207 unsigned int prev_offset = 0;
1210 * Each @reloc is a switch table relocation which points to the target
1213 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1215 /* Check for the end of the table: */
1216 if (reloc != table && reloc->jump_table_start)
1219 /* Make sure the table entries are consecutive: */
1220 if (prev_offset && reloc->offset != prev_offset + 8)
1223 /* Detect function pointers from contiguous objects: */
1224 if (reloc->sym->sec == pfunc->sec &&
1225 reloc->addend == pfunc->offset)
1228 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1232 /* Make sure the destination is in the same function: */
1233 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1236 alt = malloc(sizeof(*alt));
1238 WARN("malloc failed");
1242 alt->insn = dest_insn;
1243 list_add_tail(&alt->list, &insn->alts);
1244 prev_offset = reloc->offset;
1248 WARN_FUNC("can't find switch jump table",
1249 insn->sec, insn->offset);
1257 * find_jump_table() - Given a dynamic jump, find the switch jump table in
1258 * .rodata associated with it.
1260 * There are 3 basic patterns:
1262 * 1. jmpq *[rodata addr](,%reg,8)
1264 * This is the most common case by far. It jumps to an address in a simple
1265 * jump table which is stored in .rodata.
1267 * 2. jmpq *[rodata addr](%rip)
1269 * This is caused by a rare GCC quirk, currently only seen in three driver
1270 * functions in the kernel, only with certain obscure non-distro configs.
1272 * As part of an optimization, GCC makes a copy of an existing switch jump
1273 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1274 * jump) to use a single entry in the table. The rest of the jump table and
1275 * some of its jump targets remain as dead code.
1277 * In such a case we can just crudely ignore all unreachable instruction
1278 * warnings for the entire object file. Ideally we would just ignore them
1279 * for the function, but that would require redesigning the code quite a
1280 * bit. And honestly that's just not worth doing: unreachable instruction
1281 * warnings are of questionable value anyway, and this is such a rare issue.
1283 * 3. mov [rodata addr],%reg1
1284 * ... some instructions ...
1285 * jmpq *(%reg1,%reg2,8)
1287 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1288 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1290 * As of GCC 7 there are quite a few more of these and the 'in between' code
1291 * is significant. Esp. with KASAN enabled some of the code between the mov
1292 * and jmpq uses .rodata itself, which can confuse things.
1294 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1295 * ensure the same register is used in the mov and jump instructions.
1297 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1299 static struct reloc *find_jump_table(struct objtool_file *file,
1300 struct symbol *func,
1301 struct instruction *insn)
1303 struct reloc *text_reloc, *table_reloc;
1304 struct instruction *dest_insn, *orig_insn = insn;
1305 struct section *table_sec;
1306 unsigned long table_offset;
1309 * Backward search using the @first_jump_src links, these help avoid
1310 * much of the 'in between' code. Which avoids us getting confused by
1314 insn && insn->func && insn->func->pfunc == func;
1315 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1317 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1320 /* allow small jumps within the range */
1321 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1323 (insn->jump_dest->offset <= insn->offset ||
1324 insn->jump_dest->offset > orig_insn->offset))
1327 /* look for a relocation which references .rodata */
1328 text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1329 insn->offset, insn->len);
1330 if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
1331 !text_reloc->sym->sec->rodata)
1334 table_offset = text_reloc->addend;
1335 table_sec = text_reloc->sym->sec;
1337 if (text_reloc->type == R_X86_64_PC32)
1341 * Make sure the .rodata address isn't associated with a
1342 * symbol. GCC jump tables are anonymous data.
1344 * Also support C jump tables which are in the same format as
1345 * switch jump tables. For objtool to recognize them, they
1346 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1347 * have symbols associated with them.
1349 if (find_symbol_containing(table_sec, table_offset) &&
1350 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1354 * Each table entry has a reloc associated with it. The reloc
1355 * should reference text in the same function as the original
1358 table_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
1361 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1362 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1366 * Use of RIP-relative switch jumps is quite rare, and
1367 * indicates a rare GCC quirk/bug which can leave dead code
1370 if (text_reloc->type == R_X86_64_PC32)
1371 file->ignore_unreachables = true;
1380 * First pass: Mark the head of each jump table so that in the next pass,
1381 * we know when a given jump table ends and the next one starts.
1383 static void mark_func_jump_tables(struct objtool_file *file,
1384 struct symbol *func)
1386 struct instruction *insn, *last = NULL;
1387 struct reloc *reloc;
1389 func_for_each_insn(file, func, insn) {
1394 * Store back-pointers for unconditional forward jumps such
1395 * that find_jump_table() can back-track using those and
1396 * avoid some potentially confusing code.
1398 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1399 insn->offset > last->offset &&
1400 insn->jump_dest->offset > insn->offset &&
1401 !insn->jump_dest->first_jump_src) {
1403 insn->jump_dest->first_jump_src = insn;
1404 last = insn->jump_dest;
1407 if (insn->type != INSN_JUMP_DYNAMIC)
1410 reloc = find_jump_table(file, func, insn);
1412 reloc->jump_table_start = true;
1413 insn->jump_table = reloc;
1418 static int add_func_jump_tables(struct objtool_file *file,
1419 struct symbol *func)
1421 struct instruction *insn;
1424 func_for_each_insn(file, func, insn) {
1425 if (!insn->jump_table)
1428 ret = add_jump_table(file, insn, insn->jump_table);
1437 * For some switch statements, gcc generates a jump table in the .rodata
1438 * section which contains a list of addresses within the function to jump to.
1439 * This finds these jump tables and adds them to the insn->alts lists.
1441 static int add_jump_table_alts(struct objtool_file *file)
1443 struct section *sec;
1444 struct symbol *func;
1450 for_each_sec(file, sec) {
1451 list_for_each_entry(func, &sec->symbol_list, list) {
1452 if (func->type != STT_FUNC)
1455 mark_func_jump_tables(file, func);
1456 ret = add_func_jump_tables(file, func);
1465 static int read_unwind_hints(struct objtool_file *file)
1467 struct section *sec, *relocsec;
1468 struct reloc *reloc;
1469 struct unwind_hint *hint;
1470 struct instruction *insn;
1471 struct cfi_reg *cfa;
1474 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1478 relocsec = sec->reloc;
1480 WARN("missing .rela.discard.unwind_hints section");
1484 if (sec->len % sizeof(struct unwind_hint)) {
1485 WARN("struct unwind_hint size mismatch");
1491 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1492 hint = (struct unwind_hint *)sec->data->d_buf + i;
1494 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1496 WARN("can't find reloc for unwind_hints[%d]", i);
1500 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1502 WARN("can't find insn for unwind_hints[%d]", i);
1506 cfa = &insn->cfi.cfa;
1508 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1509 insn->ret_offset = hint->sp_offset;
1515 switch (hint->sp_reg) {
1516 case ORC_REG_UNDEFINED:
1517 cfa->base = CFI_UNDEFINED;
1525 case ORC_REG_SP_INDIRECT:
1526 cfa->base = CFI_SP_INDIRECT;
1529 cfa->base = CFI_R10;
1532 cfa->base = CFI_R13;
1541 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1542 insn->sec, insn->offset, hint->sp_reg);
1546 cfa->offset = hint->sp_offset;
1547 insn->cfi.type = hint->type;
1548 insn->cfi.end = hint->end;
1554 static int read_retpoline_hints(struct objtool_file *file)
1556 struct section *sec;
1557 struct instruction *insn;
1558 struct reloc *reloc;
1560 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1564 list_for_each_entry(reloc, &sec->reloc_list, list) {
1565 if (reloc->sym->type != STT_SECTION) {
1566 WARN("unexpected relocation symbol type in %s", sec->name);
1570 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1572 WARN("bad .discard.retpoline_safe entry");
1576 if (insn->type != INSN_JUMP_DYNAMIC &&
1577 insn->type != INSN_CALL_DYNAMIC) {
1578 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1579 insn->sec, insn->offset);
1583 insn->retpoline_safe = true;
1589 static int read_instr_hints(struct objtool_file *file)
1591 struct section *sec;
1592 struct instruction *insn;
1593 struct reloc *reloc;
1595 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1599 list_for_each_entry(reloc, &sec->reloc_list, list) {
1600 if (reloc->sym->type != STT_SECTION) {
1601 WARN("unexpected relocation symbol type in %s", sec->name);
1605 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1607 WARN("bad .discard.instr_end entry");
1614 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1618 list_for_each_entry(reloc, &sec->reloc_list, list) {
1619 if (reloc->sym->type != STT_SECTION) {
1620 WARN("unexpected relocation symbol type in %s", sec->name);
1624 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1626 WARN("bad .discard.instr_begin entry");
1636 static int read_intra_function_calls(struct objtool_file *file)
1638 struct instruction *insn;
1639 struct section *sec;
1640 struct reloc *reloc;
1642 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1646 list_for_each_entry(reloc, &sec->reloc_list, list) {
1647 unsigned long dest_off;
1649 if (reloc->sym->type != STT_SECTION) {
1650 WARN("unexpected relocation symbol type in %s",
1655 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1657 WARN("bad .discard.intra_function_call entry");
1661 if (insn->type != INSN_CALL) {
1662 WARN_FUNC("intra_function_call not a direct call",
1663 insn->sec, insn->offset);
1668 * Treat intra-function CALLs as JMPs, but with a stack_op.
1669 * See add_call_destinations(), which strips stack_ops from
1672 insn->type = INSN_JUMP_UNCONDITIONAL;
1674 dest_off = insn->offset + insn->len + insn->immediate;
1675 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1676 if (!insn->jump_dest) {
1677 WARN_FUNC("can't find call dest at %s+0x%lx",
1678 insn->sec, insn->offset,
1679 insn->sec->name, dest_off);
1687 static int read_static_call_tramps(struct objtool_file *file)
1689 struct section *sec;
1690 struct symbol *func;
1692 for_each_sec(file, sec) {
1693 list_for_each_entry(func, &sec->symbol_list, list) {
1694 if (func->bind == STB_GLOBAL &&
1695 !strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1696 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1697 func->static_call_tramp = true;
1704 static void mark_rodata(struct objtool_file *file)
1706 struct section *sec;
1710 * Search for the following rodata sections, each of which can
1711 * potentially contain jump tables:
1713 * - .rodata: can contain GCC switch tables
1714 * - .rodata.<func>: same, if -fdata-sections is being used
1715 * - .rodata..c_jump_table: contains C annotated jump tables
1717 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1719 for_each_sec(file, sec) {
1720 if (!strncmp(sec->name, ".rodata", 7) &&
1721 !strstr(sec->name, ".str1.")) {
1727 file->rodata = found;
1730 static int decode_sections(struct objtool_file *file)
1736 ret = decode_instructions(file);
1740 ret = add_dead_ends(file);
1745 add_uaccess_safe(file);
1747 ret = add_ignore_alternatives(file);
1751 ret = read_static_call_tramps(file);
1755 ret = add_jump_destinations(file);
1759 ret = add_special_section_alts(file);
1763 ret = read_intra_function_calls(file);
1767 ret = add_call_destinations(file);
1771 ret = add_jump_table_alts(file);
1775 ret = read_unwind_hints(file);
1779 ret = read_retpoline_hints(file);
1783 ret = read_instr_hints(file);
1790 static bool is_fentry_call(struct instruction *insn)
1792 if (insn->type == INSN_CALL && insn->call_dest &&
1793 insn->call_dest->type == STT_NOTYPE &&
1794 !strcmp(insn->call_dest->name, "__fentry__"))
1800 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1802 u8 ret_offset = insn->ret_offset;
1803 struct cfi_state *cfi = &state->cfi;
1806 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1809 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
1812 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1816 * If there is a ret offset hint then don't check registers
1817 * because a callee-saved register might have been pushed on
1823 for (i = 0; i < CFI_NUM_REGS; i++) {
1824 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1825 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1832 static bool has_valid_stack_frame(struct insn_state *state)
1834 struct cfi_state *cfi = &state->cfi;
1836 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1837 cfi->regs[CFI_BP].offset == -16)
1840 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1846 static int update_cfi_state_regs(struct instruction *insn,
1847 struct cfi_state *cfi,
1848 struct stack_op *op)
1850 struct cfi_reg *cfa = &cfi->cfa;
1852 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1856 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1860 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1863 /* add immediate to sp */
1864 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1865 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1866 cfa->offset -= op->src.offset;
1871 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1873 if (arch_callee_saved_reg(reg) &&
1874 cfi->regs[reg].base == CFI_UNDEFINED) {
1875 cfi->regs[reg].base = base;
1876 cfi->regs[reg].offset = offset;
1880 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1882 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1883 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1887 * A note about DRAP stack alignment:
1889 * GCC has the concept of a DRAP register, which is used to help keep track of
1890 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1891 * register. The typical DRAP pattern is:
1893 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1894 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1895 * 41 ff 72 f8 pushq -0x8(%r10)
1897 * 48 89 e5 mov %rsp,%rbp
1904 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1907 * There are some variations in the epilogues, like:
1915 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1920 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1921 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1922 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1923 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1925 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1928 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1929 * restored beforehand:
1932 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1933 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1935 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1939 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1940 struct stack_op *op)
1942 struct cfi_reg *cfa = &cfi->cfa;
1943 struct cfi_reg *regs = cfi->regs;
1945 /* stack operations don't make sense with an undefined CFA */
1946 if (cfa->base == CFI_UNDEFINED) {
1948 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1954 if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1955 return update_cfi_state_regs(insn, cfi, op);
1957 switch (op->dest.type) {
1960 switch (op->src.type) {
1963 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1964 cfa->base == CFI_SP &&
1965 regs[CFI_BP].base == CFI_CFA &&
1966 regs[CFI_BP].offset == -cfa->offset) {
1968 /* mov %rsp, %rbp */
1969 cfa->base = op->dest.reg;
1970 cfi->bp_scratch = false;
1973 else if (op->src.reg == CFI_SP &&
1974 op->dest.reg == CFI_BP && cfi->drap) {
1976 /* drap: mov %rsp, %rbp */
1977 regs[CFI_BP].base = CFI_BP;
1978 regs[CFI_BP].offset = -cfi->stack_size;
1979 cfi->bp_scratch = false;
1982 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1987 * This is needed for the rare case where GCC
1994 cfi->vals[op->dest.reg].base = CFI_CFA;
1995 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1998 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1999 cfa->base == CFI_BP) {
2004 * Restore the original stack pointer (Clang).
2006 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2009 else if (op->dest.reg == cfa->base) {
2011 /* mov %reg, %rsp */
2012 if (cfa->base == CFI_SP &&
2013 cfi->vals[op->src.reg].base == CFI_CFA) {
2016 * This is needed for the rare case
2017 * where GCC does something dumb like:
2019 * lea 0x8(%rsp), %rcx
2023 cfa->offset = -cfi->vals[op->src.reg].offset;
2024 cfi->stack_size = cfa->offset;
2027 cfa->base = CFI_UNDEFINED;
2035 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2038 cfi->stack_size -= op->src.offset;
2039 if (cfa->base == CFI_SP)
2040 cfa->offset -= op->src.offset;
2044 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2046 /* lea disp(%rbp), %rsp */
2047 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2051 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2053 /* drap: lea disp(%rsp), %drap */
2054 cfi->drap_reg = op->dest.reg;
2057 * lea disp(%rsp), %reg
2059 * This is needed for the rare case where GCC
2060 * does something dumb like:
2062 * lea 0x8(%rsp), %rcx
2066 cfi->vals[op->dest.reg].base = CFI_CFA;
2067 cfi->vals[op->dest.reg].offset = \
2068 -cfi->stack_size + op->src.offset;
2073 if (cfi->drap && op->dest.reg == CFI_SP &&
2074 op->src.reg == cfi->drap_reg) {
2076 /* drap: lea disp(%drap), %rsp */
2078 cfa->offset = cfi->stack_size = -op->src.offset;
2079 cfi->drap_reg = CFI_UNDEFINED;
2084 if (op->dest.reg == cfi->cfa.base) {
2085 WARN_FUNC("unsupported stack register modification",
2086 insn->sec, insn->offset);
2093 if (op->dest.reg != CFI_SP ||
2094 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2095 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2096 WARN_FUNC("unsupported stack pointer realignment",
2097 insn->sec, insn->offset);
2101 if (cfi->drap_reg != CFI_UNDEFINED) {
2102 /* drap: and imm, %rsp */
2103 cfa->base = cfi->drap_reg;
2104 cfa->offset = cfi->stack_size = 0;
2109 * Older versions of GCC (4.8ish) realign the stack
2110 * without DRAP, with a frame pointer.
2117 if (!cfi->drap && op->dest.reg == cfa->base) {
2123 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2124 op->dest.reg == cfi->drap_reg &&
2125 cfi->drap_offset == -cfi->stack_size) {
2127 /* drap: pop %drap */
2128 cfa->base = cfi->drap_reg;
2130 cfi->drap_offset = -1;
2132 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2135 restore_reg(cfi, op->dest.reg);
2138 cfi->stack_size -= 8;
2139 if (cfa->base == CFI_SP)
2144 case OP_SRC_REG_INDIRECT:
2145 if (cfi->drap && op->src.reg == CFI_BP &&
2146 op->src.offset == cfi->drap_offset) {
2148 /* drap: mov disp(%rbp), %drap */
2149 cfa->base = cfi->drap_reg;
2151 cfi->drap_offset = -1;
2154 if (cfi->drap && op->src.reg == CFI_BP &&
2155 op->src.offset == regs[op->dest.reg].offset) {
2157 /* drap: mov disp(%rbp), %reg */
2158 restore_reg(cfi, op->dest.reg);
2160 } else if (op->src.reg == cfa->base &&
2161 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2163 /* mov disp(%rbp), %reg */
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;
2202 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
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 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2235 /* drap: mov reg, disp(%rbp) */
2236 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2239 } else if (op->dest.reg == cfa->base) {
2241 /* mov reg, disp(%rbp) */
2242 /* mov reg, disp(%rsp) */
2243 save_reg(cfi, op->src.reg, CFI_CFA,
2244 op->dest.offset - cfi->cfa.offset);
2250 if ((!cfi->drap && cfa->base != CFI_BP) ||
2251 (cfi->drap && cfa->base != cfi->drap_reg)) {
2252 WARN_FUNC("leave instruction with modified stack frame",
2253 insn->sec, insn->offset);
2257 /* leave (mov %rbp, %rsp; pop %rbp) */
2259 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2260 restore_reg(cfi, CFI_BP);
2270 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2271 WARN_FUNC("unknown stack-related memory operation",
2272 insn->sec, insn->offset);
2277 cfi->stack_size -= 8;
2278 if (cfa->base == CFI_SP)
2284 WARN_FUNC("unknown stack-related instruction",
2285 insn->sec, insn->offset);
2292 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2294 struct stack_op *op;
2296 list_for_each_entry(op, &insn->stack_ops, list) {
2297 struct cfi_state old_cfi = state->cfi;
2300 res = update_cfi_state(insn, &state->cfi, op);
2304 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2305 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2309 if (op->dest.type == OP_DEST_PUSHF) {
2310 if (!state->uaccess_stack) {
2311 state->uaccess_stack = 1;
2312 } else if (state->uaccess_stack >> 31) {
2313 WARN_FUNC("PUSHF stack exhausted",
2314 insn->sec, insn->offset);
2317 state->uaccess_stack <<= 1;
2318 state->uaccess_stack |= state->uaccess;
2321 if (op->src.type == OP_SRC_POPF) {
2322 if (state->uaccess_stack) {
2323 state->uaccess = state->uaccess_stack & 1;
2324 state->uaccess_stack >>= 1;
2325 if (state->uaccess_stack == 1)
2326 state->uaccess_stack = 0;
2334 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2336 struct cfi_state *cfi1 = &insn->cfi;
2339 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2341 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2342 insn->sec, insn->offset,
2343 cfi1->cfa.base, cfi1->cfa.offset,
2344 cfi2->cfa.base, cfi2->cfa.offset);
2346 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2347 for (i = 0; i < CFI_NUM_REGS; i++) {
2348 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2349 sizeof(struct cfi_reg)))
2352 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2353 insn->sec, insn->offset,
2354 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2355 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2359 } else if (cfi1->type != cfi2->type) {
2361 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2362 insn->sec, insn->offset, cfi1->type, cfi2->type);
2364 } else if (cfi1->drap != cfi2->drap ||
2365 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2366 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2368 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2369 insn->sec, insn->offset,
2370 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2371 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2379 static inline bool func_uaccess_safe(struct symbol *func)
2382 return func->uaccess_safe;
2387 static inline const char *call_dest_name(struct instruction *insn)
2389 if (insn->call_dest)
2390 return insn->call_dest->name;
2395 static inline bool noinstr_call_dest(struct symbol *func)
2398 * We can't deal with indirect function calls at present;
2399 * assume they're instrumented.
2405 * If the symbol is from a noinstr section; we good.
2407 if (func->sec->noinstr)
2411 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2412 * something 'BAD' happened. At the risk of taking the machine down,
2413 * let them proceed to get the message out.
2415 if (!strncmp(func->name, "__ubsan_handle_", 15))
2421 static int validate_call(struct instruction *insn, struct insn_state *state)
2423 if (state->noinstr && state->instr <= 0 &&
2424 !noinstr_call_dest(insn->call_dest)) {
2425 WARN_FUNC("call to %s() leaves .noinstr.text section",
2426 insn->sec, insn->offset, call_dest_name(insn));
2430 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2431 WARN_FUNC("call to %s() with UACCESS enabled",
2432 insn->sec, insn->offset, call_dest_name(insn));
2437 WARN_FUNC("call to %s() with DF set",
2438 insn->sec, insn->offset, call_dest_name(insn));
2445 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2447 if (has_modified_stack_frame(insn, state)) {
2448 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2449 insn->sec, insn->offset);
2453 return validate_call(insn, state);
2456 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2458 if (state->noinstr && state->instr > 0) {
2459 WARN_FUNC("return with instrumentation enabled",
2460 insn->sec, insn->offset);
2464 if (state->uaccess && !func_uaccess_safe(func)) {
2465 WARN_FUNC("return with UACCESS enabled",
2466 insn->sec, insn->offset);
2470 if (!state->uaccess && func_uaccess_safe(func)) {
2471 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2472 insn->sec, insn->offset);
2477 WARN_FUNC("return with DF set",
2478 insn->sec, insn->offset);
2482 if (func && has_modified_stack_frame(insn, state)) {
2483 WARN_FUNC("return with modified stack frame",
2484 insn->sec, insn->offset);
2488 if (state->cfi.bp_scratch) {
2489 WARN_FUNC("BP used as a scratch register",
2490 insn->sec, insn->offset);
2498 * Alternatives should not contain any ORC entries, this in turn means they
2499 * should not contain any CFI ops, which implies all instructions should have
2500 * the same same CFI state.
2502 * It is possible to constuct alternatives that have unreachable holes that go
2503 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2504 * states which then results in ORC entries, which we just said we didn't want.
2506 * Avoid them by copying the CFI entry of the first instruction into the whole
2509 static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2511 struct instruction *first_insn = insn;
2512 int alt_group = insn->alt_group;
2514 sec_for_each_insn_continue(file, insn) {
2515 if (insn->alt_group != alt_group)
2517 insn->cfi = first_insn->cfi;
2522 * Follow the branch starting at the given instruction, and recursively follow
2523 * any other branches (jumps). Meanwhile, track the frame pointer state at
2524 * each instruction and validate all the rules described in
2525 * tools/objtool/Documentation/stack-validation.txt.
2527 static int validate_branch(struct objtool_file *file, struct symbol *func,
2528 struct instruction *insn, struct insn_state state)
2530 struct alternative *alt;
2531 struct instruction *next_insn;
2532 struct section *sec;
2539 next_insn = next_insn_same_sec(file, insn);
2541 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2542 WARN("%s() falls through to next function %s()",
2543 func->name, insn->func->name);
2547 if (func && insn->ignore) {
2548 WARN_FUNC("BUG: why am I validating an ignored function?",
2553 visited = 1 << state.uaccess;
2554 if (insn->visited) {
2555 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2558 if (insn->visited & visited)
2563 state.instr += insn->instr;
2566 state.cfi = insn->cfi;
2568 insn->cfi = state.cfi;
2570 insn->visited |= visited;
2572 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2573 bool skip_orig = false;
2575 list_for_each_entry(alt, &insn->alts, list) {
2579 ret = validate_branch(file, func, alt->insn, state);
2582 BT_FUNC("(alt)", insn);
2587 if (insn->alt_group)
2588 fill_alternative_cfi(file, insn);
2594 if (handle_insn_ops(insn, &state))
2597 switch (insn->type) {
2600 return validate_return(func, insn, &state);
2603 case INSN_CALL_DYNAMIC:
2604 ret = validate_call(insn, &state);
2608 if (!no_fp && func && !is_fentry_call(insn) &&
2609 !has_valid_stack_frame(&state)) {
2610 WARN_FUNC("call without frame pointer save/setup",
2615 if (dead_end_function(file, insn->call_dest))
2618 if (insn->type == INSN_CALL && insn->call_dest->static_call_tramp) {
2619 list_add_tail(&insn->static_call_node,
2620 &file->static_call_list);
2625 case INSN_JUMP_CONDITIONAL:
2626 case INSN_JUMP_UNCONDITIONAL:
2627 if (func && is_sibling_call(insn)) {
2628 ret = validate_sibling_call(insn, &state);
2632 } else if (insn->jump_dest) {
2633 ret = validate_branch(file, func,
2634 insn->jump_dest, state);
2637 BT_FUNC("(branch)", insn);
2642 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2647 case INSN_JUMP_DYNAMIC:
2648 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2649 if (func && is_sibling_call(insn)) {
2650 ret = validate_sibling_call(insn, &state);
2655 if (insn->type == INSN_JUMP_DYNAMIC)
2660 case INSN_CONTEXT_SWITCH:
2661 if (func && (!next_insn || !next_insn->hint)) {
2662 WARN_FUNC("unsupported instruction in callable function",
2669 if (state.uaccess) {
2670 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2674 state.uaccess = true;
2678 if (!state.uaccess && func) {
2679 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2683 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2684 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2688 state.uaccess = false;
2693 WARN_FUNC("recursive STD", sec, insn->offset);
2699 if (!state.df && func)
2700 WARN_FUNC("redundant CLD", sec, insn->offset);
2713 if (state.cfi.cfa.base == CFI_UNDEFINED)
2715 WARN("%s: unexpected end of section", sec->name);
2725 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2727 struct instruction *insn;
2728 struct insn_state state;
2729 int ret, warnings = 0;
2734 init_insn_state(&state, sec);
2737 insn = find_insn(file, sec, 0);
2741 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2744 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2745 if (insn->hint && !insn->visited) {
2746 ret = validate_branch(file, insn->func, insn, state);
2747 if (ret && backtrace)
2748 BT_FUNC("<=== (hint)", insn);
2752 insn = list_next_entry(insn, list);
2758 static int validate_retpoline(struct objtool_file *file)
2760 struct instruction *insn;
2763 for_each_insn(file, insn) {
2764 if (insn->type != INSN_JUMP_DYNAMIC &&
2765 insn->type != INSN_CALL_DYNAMIC)
2768 if (insn->retpoline_safe)
2772 * .init.text code is ran before userspace and thus doesn't
2773 * strictly need retpolines, except for modules which are
2774 * loaded late, they very much do need retpoline in their
2777 if (!strcmp(insn->sec->name, ".init.text") && !module)
2780 WARN_FUNC("indirect %s found in RETPOLINE build",
2781 insn->sec, insn->offset,
2782 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2790 static bool is_kasan_insn(struct instruction *insn)
2792 return (insn->type == INSN_CALL &&
2793 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2796 static bool is_ubsan_insn(struct instruction *insn)
2798 return (insn->type == INSN_CALL &&
2799 !strcmp(insn->call_dest->name,
2800 "__ubsan_handle_builtin_unreachable"));
2803 static bool ignore_unreachable_insn(struct instruction *insn)
2807 if (insn->ignore || insn->type == INSN_NOP)
2811 * Ignore any unused exceptions. This can happen when a whitelisted
2812 * function has an exception table entry.
2814 * Also ignore alternative replacement instructions. This can happen
2815 * when a whitelisted function uses one of the ALTERNATIVE macros.
2817 if (!strcmp(insn->sec->name, ".fixup") ||
2818 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2819 !strcmp(insn->sec->name, ".altinstr_aux"))
2826 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2827 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2828 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2829 * (or occasionally a JMP to UD2).
2831 if (list_prev_entry(insn, list)->dead_end &&
2832 (insn->type == INSN_BUG ||
2833 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2834 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2838 * Check if this (or a subsequent) instruction is related to
2839 * CONFIG_UBSAN or CONFIG_KASAN.
2841 * End the search at 5 instructions to avoid going into the weeds.
2843 for (i = 0; i < 5; i++) {
2845 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2848 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2849 if (insn->jump_dest &&
2850 insn->jump_dest->func == insn->func) {
2851 insn = insn->jump_dest;
2858 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2861 insn = list_next_entry(insn, list);
2867 static int validate_symbol(struct objtool_file *file, struct section *sec,
2868 struct symbol *sym, struct insn_state *state)
2870 struct instruction *insn;
2874 WARN("%s() is missing an ELF size annotation", sym->name);
2878 if (sym->pfunc != sym || sym->alias != sym)
2881 insn = find_insn(file, sec, sym->offset);
2882 if (!insn || insn->ignore || insn->visited)
2885 state->uaccess = sym->uaccess_safe;
2887 ret = validate_branch(file, insn->func, insn, *state);
2888 if (ret && backtrace)
2889 BT_FUNC("<=== (sym)", insn);
2893 static int validate_section(struct objtool_file *file, struct section *sec)
2895 struct insn_state state;
2896 struct symbol *func;
2899 list_for_each_entry(func, &sec->symbol_list, list) {
2900 if (func->type != STT_FUNC)
2903 init_insn_state(&state, sec);
2904 state.cfi.cfa = initial_func_cfi.cfa;
2905 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
2906 CFI_NUM_REGS * sizeof(struct cfi_reg));
2907 state.cfi.stack_size = initial_func_cfi.cfa.offset;
2909 warnings += validate_symbol(file, sec, func, &state);
2915 static int validate_vmlinux_functions(struct objtool_file *file)
2917 struct section *sec;
2920 sec = find_section_by_name(file->elf, ".noinstr.text");
2922 warnings += validate_section(file, sec);
2923 warnings += validate_unwind_hints(file, sec);
2926 sec = find_section_by_name(file->elf, ".entry.text");
2928 warnings += validate_section(file, sec);
2929 warnings += validate_unwind_hints(file, sec);
2935 static int validate_functions(struct objtool_file *file)
2937 struct section *sec;
2940 for_each_sec(file, sec) {
2941 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2944 warnings += validate_section(file, sec);
2950 static int validate_reachable_instructions(struct objtool_file *file)
2952 struct instruction *insn;
2954 if (file->ignore_unreachables)
2957 for_each_insn(file, insn) {
2958 if (insn->visited || ignore_unreachable_insn(insn))
2961 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2968 static struct objtool_file file;
2970 int check(const char *_objname, bool orc)
2972 int ret, warnings = 0;
2976 file.elf = elf_open_read(objname, O_RDWR);
2980 INIT_LIST_HEAD(&file.insn_list);
2981 hash_init(file.insn_hash);
2982 INIT_LIST_HEAD(&file.static_call_list);
2983 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
2984 file.ignore_unreachables = no_unreachable;
2987 arch_initial_func_cfi_state(&initial_func_cfi);
2989 ret = decode_sections(&file);
2994 if (list_empty(&file.insn_list))
2997 if (vmlinux && !validate_dup) {
2998 ret = validate_vmlinux_functions(&file);
3007 ret = validate_retpoline(&file);
3013 ret = validate_functions(&file);
3018 ret = validate_unwind_hints(&file, NULL);
3024 ret = validate_reachable_instructions(&file);
3030 ret = create_static_call_sections(&file);
3036 ret = create_orc(&file);
3040 ret = create_orc_sections(&file);
3045 if (file.elf->changed) {
3046 ret = elf_write(file.elf);
3054 * Fatal error. The binary is corrupt or otherwise broken in
3055 * some way, or objtool itself is broken. Fail the kernel