1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
16 #include <linux/hashtable.h>
17 #include <linux/kernel.h>
19 #define FAKE_JUMP_OFFSET -1
21 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
24 struct list_head list;
25 struct instruction *insn;
30 struct cfi_init_state initial_func_cfi;
32 struct instruction *find_insn(struct objtool_file *file,
33 struct section *sec, unsigned long offset)
35 struct instruction *insn;
37 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
38 if (insn->sec == sec && insn->offset == offset)
45 static struct instruction *next_insn_same_sec(struct objtool_file *file,
46 struct instruction *insn)
48 struct instruction *next = list_next_entry(insn, list);
50 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
56 static struct instruction *next_insn_same_func(struct objtool_file *file,
57 struct instruction *insn)
59 struct instruction *next = list_next_entry(insn, list);
60 struct symbol *func = insn->func;
65 if (&next->list != &file->insn_list && next->func == func)
68 /* Check if we're already in the subfunction: */
69 if (func == func->cfunc)
72 /* Move to the subfunction: */
73 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
76 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
77 struct instruction *insn)
79 struct instruction *prev = list_prev_entry(insn, list);
81 if (&prev->list != &file->insn_list && prev->func == insn->func)
87 #define func_for_each_insn(file, func, insn) \
88 for (insn = find_insn(file, func->sec, func->offset); \
90 insn = next_insn_same_func(file, insn))
92 #define sym_for_each_insn(file, sym, insn) \
93 for (insn = find_insn(file, sym->sec, sym->offset); \
94 insn && &insn->list != &file->insn_list && \
95 insn->sec == sym->sec && \
96 insn->offset < sym->offset + sym->len; \
97 insn = list_next_entry(insn, list))
99 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
100 for (insn = list_prev_entry(insn, list); \
101 &insn->list != &file->insn_list && \
102 insn->sec == sym->sec && insn->offset >= sym->offset; \
103 insn = list_prev_entry(insn, list))
105 #define sec_for_each_insn_from(file, insn) \
106 for (; insn; insn = next_insn_same_sec(file, insn))
108 #define sec_for_each_insn_continue(file, insn) \
109 for (insn = next_insn_same_sec(file, insn); insn; \
110 insn = next_insn_same_sec(file, insn))
112 static bool is_static_jump(struct instruction *insn)
114 return insn->type == INSN_JUMP_CONDITIONAL ||
115 insn->type == INSN_JUMP_UNCONDITIONAL;
118 static bool is_sibling_call(struct instruction *insn)
120 /* An indirect jump is either a sibling call or a jump to a table. */
121 if (insn->type == INSN_JUMP_DYNAMIC)
122 return list_empty(&insn->alts);
124 if (!is_static_jump(insn))
127 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
128 return !!insn->call_dest;
132 * This checks to see if the given function is a "noreturn" function.
134 * For global functions which are outside the scope of this object file, we
135 * have to keep a manual list of them.
137 * For local functions, we have to detect them manually by simply looking for
138 * the lack of a return instruction.
140 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
144 struct instruction *insn;
148 * Unfortunately these have to be hard coded because the noreturn
149 * attribute isn't provided in ELF data.
151 static const char * const global_noreturns[] = {
156 "__module_put_and_exit",
162 "machine_real_restart",
163 "rewind_stack_do_exit",
164 "kunit_try_catch_throw",
170 if (func->bind == STB_WEAK)
173 if (func->bind == STB_GLOBAL)
174 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
175 if (!strcmp(func->name, global_noreturns[i]))
181 insn = find_insn(file, func->sec, func->offset);
185 func_for_each_insn(file, func, insn) {
188 if (insn->type == INSN_RETURN)
196 * A function can have a sibling call instead of a return. In that
197 * case, the function's dead-end status depends on whether the target
198 * of the sibling call returns.
200 func_for_each_insn(file, func, insn) {
201 if (is_sibling_call(insn)) {
202 struct instruction *dest = insn->jump_dest;
205 /* sibling call to another file */
208 /* local sibling call */
209 if (recursion == 5) {
211 * Infinite recursion: two functions have
212 * sibling calls to each other. This is a very
213 * rare case. It means they aren't dead ends.
218 return __dead_end_function(file, dest->func, recursion+1);
225 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
227 return __dead_end_function(file, func, 0);
230 static void init_cfi_state(struct cfi_state *cfi)
234 for (i = 0; i < CFI_NUM_REGS; i++) {
235 cfi->regs[i].base = CFI_UNDEFINED;
236 cfi->vals[i].base = CFI_UNDEFINED;
238 cfi->cfa.base = CFI_UNDEFINED;
239 cfi->drap_reg = CFI_UNDEFINED;
240 cfi->drap_offset = -1;
243 static void init_insn_state(struct insn_state *state, struct section *sec)
245 memset(state, 0, sizeof(*state));
246 init_cfi_state(&state->cfi);
249 * We need the full vmlinux for noinstr validation, otherwise we can
250 * not correctly determine insn->call_dest->sec (external symbols do
251 * not have a section).
254 state->noinstr = sec->noinstr;
258 * Call the arch-specific instruction decoder for all the instructions and add
259 * them to the global instruction list.
261 static int decode_instructions(struct objtool_file *file)
265 unsigned long offset;
266 struct instruction *insn;
267 unsigned long nr_insns = 0;
270 for_each_sec(file, sec) {
272 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
275 if (strcmp(sec->name, ".altinstr_replacement") &&
276 strcmp(sec->name, ".altinstr_aux") &&
277 strncmp(sec->name, ".discard.", 9))
280 if (!strcmp(sec->name, ".noinstr.text") ||
281 !strcmp(sec->name, ".entry.text"))
284 for (offset = 0; offset < sec->len; offset += insn->len) {
285 insn = malloc(sizeof(*insn));
287 WARN("malloc failed");
290 memset(insn, 0, sizeof(*insn));
291 INIT_LIST_HEAD(&insn->alts);
292 INIT_LIST_HEAD(&insn->stack_ops);
293 init_cfi_state(&insn->cfi);
296 insn->offset = offset;
298 ret = arch_decode_instruction(file->elf, sec, offset,
300 &insn->len, &insn->type,
306 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
307 list_add_tail(&insn->list, &file->insn_list);
311 list_for_each_entry(func, &sec->symbol_list, list) {
312 if (func->type != STT_FUNC || func->alias != func)
315 if (!find_insn(file, sec, func->offset)) {
316 WARN("%s(): can't find starting instruction",
321 sym_for_each_insn(file, func, insn)
327 printf("nr_insns: %lu\n", nr_insns);
336 static struct instruction *find_last_insn(struct objtool_file *file,
339 struct instruction *insn = NULL;
341 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
343 for (offset = sec->len - 1; offset >= end && !insn; offset--)
344 insn = find_insn(file, sec, offset);
350 * Mark "ud2" instructions and manually annotated dead ends.
352 static int add_dead_ends(struct objtool_file *file)
356 struct instruction *insn;
359 * By default, "ud2" is a dead end unless otherwise annotated, because
360 * GCC 7 inserts it for certain divide-by-zero cases.
362 for_each_insn(file, insn)
363 if (insn->type == INSN_BUG)
364 insn->dead_end = true;
367 * Check for manually annotated dead ends.
369 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
373 list_for_each_entry(rela, &sec->rela_list, list) {
374 if (rela->sym->type != STT_SECTION) {
375 WARN("unexpected relocation symbol type in %s", sec->name);
378 insn = find_insn(file, rela->sym->sec, rela->addend);
380 insn = list_prev_entry(insn, list);
381 else if (rela->addend == rela->sym->sec->len) {
382 insn = find_last_insn(file, rela->sym->sec);
384 WARN("can't find unreachable insn at %s+0x%x",
385 rela->sym->sec->name, rela->addend);
389 WARN("can't find unreachable insn at %s+0x%x",
390 rela->sym->sec->name, rela->addend);
394 insn->dead_end = true;
399 * These manually annotated reachable checks are needed for GCC 4.4,
400 * where the Linux unreachable() macro isn't supported. In that case
401 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
404 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
408 list_for_each_entry(rela, &sec->rela_list, list) {
409 if (rela->sym->type != STT_SECTION) {
410 WARN("unexpected relocation symbol type in %s", sec->name);
413 insn = find_insn(file, rela->sym->sec, rela->addend);
415 insn = list_prev_entry(insn, list);
416 else if (rela->addend == rela->sym->sec->len) {
417 insn = find_last_insn(file, rela->sym->sec);
419 WARN("can't find reachable insn at %s+0x%x",
420 rela->sym->sec->name, rela->addend);
424 WARN("can't find reachable insn at %s+0x%x",
425 rela->sym->sec->name, rela->addend);
429 insn->dead_end = false;
436 * Warnings shouldn't be reported for ignored functions.
438 static void add_ignores(struct objtool_file *file)
440 struct instruction *insn;
445 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
449 list_for_each_entry(rela, &sec->rela_list, list) {
450 switch (rela->sym->type) {
456 func = find_func_by_offset(rela->sym->sec, rela->addend);
462 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
466 func_for_each_insn(file, func, insn)
472 * This is a whitelist of functions that is allowed to be called with AC set.
473 * The list is meant to be minimal and only contains compiler instrumentation
474 * ABI and a few functions used to implement *_{to,from}_user() functions.
476 * These functions must not directly change AC, but may PUSHF/POPF.
478 static const char *uaccess_safe_builtin[] = {
481 "check_memory_region",
482 /* KASAN out-of-line */
483 "__asan_loadN_noabort",
484 "__asan_load1_noabort",
485 "__asan_load2_noabort",
486 "__asan_load4_noabort",
487 "__asan_load8_noabort",
488 "__asan_load16_noabort",
489 "__asan_storeN_noabort",
490 "__asan_store1_noabort",
491 "__asan_store2_noabort",
492 "__asan_store4_noabort",
493 "__asan_store8_noabort",
494 "__asan_store16_noabort",
496 "__asan_report_load_n_noabort",
497 "__asan_report_load1_noabort",
498 "__asan_report_load2_noabort",
499 "__asan_report_load4_noabort",
500 "__asan_report_load8_noabort",
501 "__asan_report_load16_noabort",
502 "__asan_report_store_n_noabort",
503 "__asan_report_store1_noabort",
504 "__asan_report_store2_noabort",
505 "__asan_report_store4_noabort",
506 "__asan_report_store8_noabort",
507 "__asan_report_store16_noabort",
511 "__sanitizer_cov_trace_pc",
512 "__sanitizer_cov_trace_const_cmp1",
513 "__sanitizer_cov_trace_const_cmp2",
514 "__sanitizer_cov_trace_const_cmp4",
515 "__sanitizer_cov_trace_const_cmp8",
516 "__sanitizer_cov_trace_cmp1",
517 "__sanitizer_cov_trace_cmp2",
518 "__sanitizer_cov_trace_cmp4",
519 "__sanitizer_cov_trace_cmp8",
520 "__sanitizer_cov_trace_switch",
522 "ubsan_type_mismatch_common",
523 "__ubsan_handle_type_mismatch",
524 "__ubsan_handle_type_mismatch_v1",
525 "__ubsan_handle_shift_out_of_bounds",
527 "csum_partial_copy_generic",
529 "mcsafe_handle_tail",
530 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
534 static void add_uaccess_safe(struct objtool_file *file)
542 for (name = uaccess_safe_builtin; *name; name++) {
543 func = find_symbol_by_name(file->elf, *name);
547 func->uaccess_safe = true;
552 * FIXME: For now, just ignore any alternatives which add retpolines. This is
553 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
554 * But it at least allows objtool to understand the control flow *around* the
557 static int add_ignore_alternatives(struct objtool_file *file)
561 struct instruction *insn;
563 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
567 list_for_each_entry(rela, &sec->rela_list, list) {
568 if (rela->sym->type != STT_SECTION) {
569 WARN("unexpected relocation symbol type in %s", sec->name);
573 insn = find_insn(file, rela->sym->sec, rela->addend);
575 WARN("bad .discard.ignore_alts entry");
579 insn->ignore_alts = true;
586 * Find the destination instructions for all jumps.
588 static int add_jump_destinations(struct objtool_file *file)
590 struct instruction *insn;
592 struct section *dest_sec;
593 unsigned long dest_off;
595 for_each_insn(file, insn) {
596 if (!is_static_jump(insn))
599 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
602 rela = find_rela_by_dest_range(file->elf, insn->sec,
603 insn->offset, insn->len);
605 dest_sec = insn->sec;
606 dest_off = arch_jump_destination(insn);
607 } else if (rela->sym->type == STT_SECTION) {
608 dest_sec = rela->sym->sec;
609 dest_off = arch_dest_rela_offset(rela->addend);
610 } else if (rela->sym->sec->idx) {
611 dest_sec = rela->sym->sec;
612 dest_off = rela->sym->sym.st_value +
613 arch_dest_rela_offset(rela->addend);
614 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
616 * Retpoline jumps are really dynamic jumps in
617 * disguise, so convert them accordingly.
619 if (insn->type == INSN_JUMP_UNCONDITIONAL)
620 insn->type = INSN_JUMP_DYNAMIC;
622 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
624 insn->retpoline_safe = true;
627 /* external sibling call */
628 insn->call_dest = rela->sym;
632 insn->jump_dest = find_insn(file, dest_sec, dest_off);
633 if (!insn->jump_dest) {
636 * This is a special case where an alt instruction
637 * jumps past the end of the section. These are
638 * handled later in handle_group_alt().
640 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
643 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
644 insn->sec, insn->offset, dest_sec->name,
650 * Cross-function jump.
652 if (insn->func && insn->jump_dest->func &&
653 insn->func != insn->jump_dest->func) {
656 * For GCC 8+, create parent/child links for any cold
657 * subfunctions. This is _mostly_ redundant with a
658 * similar initialization in read_symbols().
660 * If a function has aliases, we want the *first* such
661 * function in the symbol table to be the subfunction's
662 * parent. In that case we overwrite the
663 * initialization done in read_symbols().
665 * However this code can't completely replace the
666 * read_symbols() code because this doesn't detect the
667 * case where the parent function's only reference to a
668 * subfunction is through a jump table.
670 if (!strstr(insn->func->name, ".cold.") &&
671 strstr(insn->jump_dest->func->name, ".cold.")) {
672 insn->func->cfunc = insn->jump_dest->func;
673 insn->jump_dest->func->pfunc = insn->func;
675 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
676 insn->jump_dest->offset == insn->jump_dest->func->offset) {
678 /* internal sibling call */
679 insn->call_dest = insn->jump_dest->func;
687 static void remove_insn_ops(struct instruction *insn)
689 struct stack_op *op, *tmp;
691 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
698 * Find the destination instructions for all calls.
700 static int add_call_destinations(struct objtool_file *file)
702 struct instruction *insn;
703 unsigned long dest_off;
706 for_each_insn(file, insn) {
707 if (insn->type != INSN_CALL)
710 rela = find_rela_by_dest_range(file->elf, insn->sec,
711 insn->offset, insn->len);
713 dest_off = arch_jump_destination(insn);
714 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
715 if (!insn->call_dest)
716 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
721 if (!insn->call_dest) {
722 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
726 if (insn->func && insn->call_dest->type != STT_FUNC) {
727 WARN_FUNC("unsupported call to non-function",
728 insn->sec, insn->offset);
732 } else if (rela->sym->type == STT_SECTION) {
733 dest_off = arch_dest_rela_offset(rela->addend);
734 insn->call_dest = find_func_by_offset(rela->sym->sec,
736 if (!insn->call_dest) {
737 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
738 insn->sec, insn->offset,
739 rela->sym->sec->name,
744 insn->call_dest = rela->sym;
747 * Whatever stack impact regular CALLs have, should be undone
748 * by the RETURN of the called function.
750 * Annotated intra-function calls retain the stack_ops but
751 * are converted to JUMP, see read_intra_function_calls().
753 remove_insn_ops(insn);
760 * The .alternatives section requires some extra special care, over and above
761 * what other special sections require:
763 * 1. Because alternatives are patched in-place, we need to insert a fake jump
764 * instruction at the end so that validate_branch() skips all the original
765 * replaced instructions when validating the new instruction path.
767 * 2. An added wrinkle is that the new instruction length might be zero. In
768 * that case the old instructions are replaced with noops. We simulate that
769 * by creating a fake jump as the only new instruction.
771 * 3. In some cases, the alternative section includes an instruction which
772 * conditionally jumps to the _end_ of the entry. We have to modify these
773 * jumps' destinations to point back to .text rather than the end of the
774 * entry in .altinstr_replacement.
776 static int handle_group_alt(struct objtool_file *file,
777 struct special_alt *special_alt,
778 struct instruction *orig_insn,
779 struct instruction **new_insn)
781 static unsigned int alt_group_next_index = 1;
782 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
783 unsigned int alt_group = alt_group_next_index++;
784 unsigned long dest_off;
786 last_orig_insn = NULL;
788 sec_for_each_insn_from(file, insn) {
789 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
792 insn->alt_group = alt_group;
793 last_orig_insn = insn;
796 if (next_insn_same_sec(file, last_orig_insn)) {
797 fake_jump = malloc(sizeof(*fake_jump));
799 WARN("malloc failed");
802 memset(fake_jump, 0, sizeof(*fake_jump));
803 INIT_LIST_HEAD(&fake_jump->alts);
804 INIT_LIST_HEAD(&fake_jump->stack_ops);
805 init_cfi_state(&fake_jump->cfi);
807 fake_jump->sec = special_alt->new_sec;
808 fake_jump->offset = FAKE_JUMP_OFFSET;
809 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
810 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
811 fake_jump->func = orig_insn->func;
814 if (!special_alt->new_len) {
816 WARN("%s: empty alternative at end of section",
817 special_alt->orig_sec->name);
821 *new_insn = fake_jump;
825 last_new_insn = NULL;
826 alt_group = alt_group_next_index++;
828 sec_for_each_insn_from(file, insn) {
829 if (insn->offset >= special_alt->new_off + special_alt->new_len)
832 last_new_insn = insn;
834 insn->ignore = orig_insn->ignore_alts;
835 insn->func = orig_insn->func;
836 insn->alt_group = alt_group;
839 * Since alternative replacement code is copy/pasted by the
840 * kernel after applying relocations, generally such code can't
841 * have relative-address relocation references to outside the
842 * .altinstr_replacement section, unless the arch's
843 * alternatives code can adjust the relative offsets
846 * The x86 alternatives code adjusts the offsets only when it
847 * encounters a branch instruction at the very beginning of the
850 if ((insn->offset != special_alt->new_off ||
851 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
852 find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
854 WARN_FUNC("unsupported relocation in alternatives section",
855 insn->sec, insn->offset);
859 if (!is_static_jump(insn))
862 if (!insn->immediate)
865 dest_off = arch_jump_destination(insn);
866 if (dest_off == special_alt->new_off + special_alt->new_len) {
868 WARN("%s: alternative jump to end of section",
869 special_alt->orig_sec->name);
872 insn->jump_dest = fake_jump;
875 if (!insn->jump_dest) {
876 WARN_FUNC("can't find alternative jump destination",
877 insn->sec, insn->offset);
882 if (!last_new_insn) {
883 WARN_FUNC("can't find last new alternative instruction",
884 special_alt->new_sec, special_alt->new_off);
889 list_add(&fake_jump->list, &last_new_insn->list);
895 * A jump table entry can either convert a nop to a jump or a jump to a nop.
896 * If the original instruction is a jump, make the alt entry an effective nop
897 * by just skipping the original instruction.
899 static int handle_jump_alt(struct objtool_file *file,
900 struct special_alt *special_alt,
901 struct instruction *orig_insn,
902 struct instruction **new_insn)
904 if (orig_insn->type == INSN_NOP)
907 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
908 WARN_FUNC("unsupported instruction at jump label",
909 orig_insn->sec, orig_insn->offset);
913 *new_insn = list_next_entry(orig_insn, list);
918 * Read all the special sections which have alternate instructions which can be
919 * patched in or redirected to at runtime. Each instruction having alternate
920 * instruction(s) has them added to its insn->alts list, which will be
921 * traversed in validate_branch().
923 static int add_special_section_alts(struct objtool_file *file)
925 struct list_head special_alts;
926 struct instruction *orig_insn, *new_insn;
927 struct special_alt *special_alt, *tmp;
928 struct alternative *alt;
931 ret = special_get_alts(file->elf, &special_alts);
935 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
937 orig_insn = find_insn(file, special_alt->orig_sec,
938 special_alt->orig_off);
940 WARN_FUNC("special: can't find orig instruction",
941 special_alt->orig_sec, special_alt->orig_off);
947 if (!special_alt->group || special_alt->new_len) {
948 new_insn = find_insn(file, special_alt->new_sec,
949 special_alt->new_off);
951 WARN_FUNC("special: can't find new instruction",
952 special_alt->new_sec,
953 special_alt->new_off);
959 if (special_alt->group) {
960 if (!special_alt->orig_len) {
961 WARN_FUNC("empty alternative entry",
962 orig_insn->sec, orig_insn->offset);
966 ret = handle_group_alt(file, special_alt, orig_insn,
970 } else if (special_alt->jump_or_nop) {
971 ret = handle_jump_alt(file, special_alt, orig_insn,
977 alt = malloc(sizeof(*alt));
979 WARN("malloc failed");
984 alt->insn = new_insn;
985 alt->skip_orig = special_alt->skip_orig;
986 orig_insn->ignore_alts |= special_alt->skip_alt;
987 list_add_tail(&alt->list, &orig_insn->alts);
989 list_del(&special_alt->list);
997 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1000 struct rela *rela = table;
1001 struct instruction *dest_insn;
1002 struct alternative *alt;
1003 struct symbol *pfunc = insn->func->pfunc;
1004 unsigned int prev_offset = 0;
1007 * Each @rela is a switch table relocation which points to the target
1010 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
1012 /* Check for the end of the table: */
1013 if (rela != table && rela->jump_table_start)
1016 /* Make sure the table entries are consecutive: */
1017 if (prev_offset && rela->offset != prev_offset + 8)
1020 /* Detect function pointers from contiguous objects: */
1021 if (rela->sym->sec == pfunc->sec &&
1022 rela->addend == pfunc->offset)
1025 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
1029 /* Make sure the destination is in the same function: */
1030 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1033 alt = malloc(sizeof(*alt));
1035 WARN("malloc failed");
1039 alt->insn = dest_insn;
1040 list_add_tail(&alt->list, &insn->alts);
1041 prev_offset = rela->offset;
1045 WARN_FUNC("can't find switch jump table",
1046 insn->sec, insn->offset);
1054 * find_jump_table() - Given a dynamic jump, find the switch jump table in
1055 * .rodata associated with it.
1057 * There are 3 basic patterns:
1059 * 1. jmpq *[rodata addr](,%reg,8)
1061 * This is the most common case by far. It jumps to an address in a simple
1062 * jump table which is stored in .rodata.
1064 * 2. jmpq *[rodata addr](%rip)
1066 * This is caused by a rare GCC quirk, currently only seen in three driver
1067 * functions in the kernel, only with certain obscure non-distro configs.
1069 * As part of an optimization, GCC makes a copy of an existing switch jump
1070 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1071 * jump) to use a single entry in the table. The rest of the jump table and
1072 * some of its jump targets remain as dead code.
1074 * In such a case we can just crudely ignore all unreachable instruction
1075 * warnings for the entire object file. Ideally we would just ignore them
1076 * for the function, but that would require redesigning the code quite a
1077 * bit. And honestly that's just not worth doing: unreachable instruction
1078 * warnings are of questionable value anyway, and this is such a rare issue.
1080 * 3. mov [rodata addr],%reg1
1081 * ... some instructions ...
1082 * jmpq *(%reg1,%reg2,8)
1084 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1085 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1087 * As of GCC 7 there are quite a few more of these and the 'in between' code
1088 * is significant. Esp. with KASAN enabled some of the code between the mov
1089 * and jmpq uses .rodata itself, which can confuse things.
1091 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1092 * ensure the same register is used in the mov and jump instructions.
1094 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1096 static struct rela *find_jump_table(struct objtool_file *file,
1097 struct symbol *func,
1098 struct instruction *insn)
1100 struct rela *text_rela, *table_rela;
1101 struct instruction *dest_insn, *orig_insn = insn;
1102 struct section *table_sec;
1103 unsigned long table_offset;
1106 * Backward search using the @first_jump_src links, these help avoid
1107 * much of the 'in between' code. Which avoids us getting confused by
1111 insn && insn->func && insn->func->pfunc == func;
1112 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1114 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1117 /* allow small jumps within the range */
1118 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1120 (insn->jump_dest->offset <= insn->offset ||
1121 insn->jump_dest->offset > orig_insn->offset))
1124 /* look for a relocation which references .rodata */
1125 text_rela = find_rela_by_dest_range(file->elf, insn->sec,
1126 insn->offset, insn->len);
1127 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1128 !text_rela->sym->sec->rodata)
1131 table_offset = text_rela->addend;
1132 table_sec = text_rela->sym->sec;
1134 if (text_rela->type == R_X86_64_PC32)
1138 * Make sure the .rodata address isn't associated with a
1139 * symbol. GCC jump tables are anonymous data.
1141 * Also support C jump tables which are in the same format as
1142 * switch jump tables. For objtool to recognize them, they
1143 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1144 * have symbols associated with them.
1146 if (find_symbol_containing(table_sec, table_offset) &&
1147 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1151 * Each table entry has a rela associated with it. The rela
1152 * should reference text in the same function as the original
1155 table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
1158 dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
1159 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1163 * Use of RIP-relative switch jumps is quite rare, and
1164 * indicates a rare GCC quirk/bug which can leave dead code
1167 if (text_rela->type == R_X86_64_PC32)
1168 file->ignore_unreachables = true;
1177 * First pass: Mark the head of each jump table so that in the next pass,
1178 * we know when a given jump table ends and the next one starts.
1180 static void mark_func_jump_tables(struct objtool_file *file,
1181 struct symbol *func)
1183 struct instruction *insn, *last = NULL;
1186 func_for_each_insn(file, func, insn) {
1191 * Store back-pointers for unconditional forward jumps such
1192 * that find_jump_table() can back-track using those and
1193 * avoid some potentially confusing code.
1195 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1196 insn->offset > last->offset &&
1197 insn->jump_dest->offset > insn->offset &&
1198 !insn->jump_dest->first_jump_src) {
1200 insn->jump_dest->first_jump_src = insn;
1201 last = insn->jump_dest;
1204 if (insn->type != INSN_JUMP_DYNAMIC)
1207 rela = find_jump_table(file, func, insn);
1209 rela->jump_table_start = true;
1210 insn->jump_table = rela;
1215 static int add_func_jump_tables(struct objtool_file *file,
1216 struct symbol *func)
1218 struct instruction *insn;
1221 func_for_each_insn(file, func, insn) {
1222 if (!insn->jump_table)
1225 ret = add_jump_table(file, insn, insn->jump_table);
1234 * For some switch statements, gcc generates a jump table in the .rodata
1235 * section which contains a list of addresses within the function to jump to.
1236 * This finds these jump tables and adds them to the insn->alts lists.
1238 static int add_jump_table_alts(struct objtool_file *file)
1240 struct section *sec;
1241 struct symbol *func;
1247 for_each_sec(file, sec) {
1248 list_for_each_entry(func, &sec->symbol_list, list) {
1249 if (func->type != STT_FUNC)
1252 mark_func_jump_tables(file, func);
1253 ret = add_func_jump_tables(file, func);
1262 static int read_unwind_hints(struct objtool_file *file)
1264 struct section *sec, *relasec;
1266 struct unwind_hint *hint;
1267 struct instruction *insn;
1268 struct cfi_reg *cfa;
1271 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1275 relasec = sec->rela;
1277 WARN("missing .rela.discard.unwind_hints section");
1281 if (sec->len % sizeof(struct unwind_hint)) {
1282 WARN("struct unwind_hint size mismatch");
1288 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1289 hint = (struct unwind_hint *)sec->data->d_buf + i;
1291 rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
1293 WARN("can't find rela for unwind_hints[%d]", i);
1297 insn = find_insn(file, rela->sym->sec, rela->addend);
1299 WARN("can't find insn for unwind_hints[%d]", i);
1303 cfa = &insn->cfi.cfa;
1305 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1306 insn->ret_offset = hint->sp_offset;
1312 switch (hint->sp_reg) {
1313 case ORC_REG_UNDEFINED:
1314 cfa->base = CFI_UNDEFINED;
1322 case ORC_REG_SP_INDIRECT:
1323 cfa->base = CFI_SP_INDIRECT;
1326 cfa->base = CFI_R10;
1329 cfa->base = CFI_R13;
1338 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1339 insn->sec, insn->offset, hint->sp_reg);
1343 cfa->offset = hint->sp_offset;
1344 insn->cfi.type = hint->type;
1345 insn->cfi.end = hint->end;
1351 static int read_retpoline_hints(struct objtool_file *file)
1353 struct section *sec;
1354 struct instruction *insn;
1357 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1361 list_for_each_entry(rela, &sec->rela_list, list) {
1362 if (rela->sym->type != STT_SECTION) {
1363 WARN("unexpected relocation symbol type in %s", sec->name);
1367 insn = find_insn(file, rela->sym->sec, rela->addend);
1369 WARN("bad .discard.retpoline_safe entry");
1373 if (insn->type != INSN_JUMP_DYNAMIC &&
1374 insn->type != INSN_CALL_DYNAMIC) {
1375 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1376 insn->sec, insn->offset);
1380 insn->retpoline_safe = true;
1386 static int read_instr_hints(struct objtool_file *file)
1388 struct section *sec;
1389 struct instruction *insn;
1392 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1396 list_for_each_entry(rela, &sec->rela_list, list) {
1397 if (rela->sym->type != STT_SECTION) {
1398 WARN("unexpected relocation symbol type in %s", sec->name);
1402 insn = find_insn(file, rela->sym->sec, rela->addend);
1404 WARN("bad .discard.instr_end entry");
1411 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1415 list_for_each_entry(rela, &sec->rela_list, list) {
1416 if (rela->sym->type != STT_SECTION) {
1417 WARN("unexpected relocation symbol type in %s", sec->name);
1421 insn = find_insn(file, rela->sym->sec, rela->addend);
1423 WARN("bad .discard.instr_begin entry");
1433 static int read_intra_function_calls(struct objtool_file *file)
1435 struct instruction *insn;
1436 struct section *sec;
1439 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1443 list_for_each_entry(rela, &sec->rela_list, list) {
1444 unsigned long dest_off;
1446 if (rela->sym->type != STT_SECTION) {
1447 WARN("unexpected relocation symbol type in %s",
1452 insn = find_insn(file, rela->sym->sec, rela->addend);
1454 WARN("bad .discard.intra_function_call entry");
1458 if (insn->type != INSN_CALL) {
1459 WARN_FUNC("intra_function_call not a direct call",
1460 insn->sec, insn->offset);
1465 * Treat intra-function CALLs as JMPs, but with a stack_op.
1466 * See add_call_destinations(), which strips stack_ops from
1469 insn->type = INSN_JUMP_UNCONDITIONAL;
1471 dest_off = insn->offset + insn->len + insn->immediate;
1472 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1473 if (!insn->jump_dest) {
1474 WARN_FUNC("can't find call dest at %s+0x%lx",
1475 insn->sec, insn->offset,
1476 insn->sec->name, dest_off);
1484 static void mark_rodata(struct objtool_file *file)
1486 struct section *sec;
1490 * Search for the following rodata sections, each of which can
1491 * potentially contain jump tables:
1493 * - .rodata: can contain GCC switch tables
1494 * - .rodata.<func>: same, if -fdata-sections is being used
1495 * - .rodata..c_jump_table: contains C annotated jump tables
1497 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1499 for_each_sec(file, sec) {
1500 if (!strncmp(sec->name, ".rodata", 7) &&
1501 !strstr(sec->name, ".str1.")) {
1507 file->rodata = found;
1510 static int decode_sections(struct objtool_file *file)
1516 ret = decode_instructions(file);
1520 ret = add_dead_ends(file);
1525 add_uaccess_safe(file);
1527 ret = add_ignore_alternatives(file);
1531 ret = add_jump_destinations(file);
1535 ret = add_special_section_alts(file);
1539 ret = read_intra_function_calls(file);
1543 ret = add_call_destinations(file);
1547 ret = add_jump_table_alts(file);
1551 ret = read_unwind_hints(file);
1555 ret = read_retpoline_hints(file);
1559 ret = read_instr_hints(file);
1566 static bool is_fentry_call(struct instruction *insn)
1568 if (insn->type == INSN_CALL && insn->call_dest &&
1569 insn->call_dest->type == STT_NOTYPE &&
1570 !strcmp(insn->call_dest->name, "__fentry__"))
1576 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1578 u8 ret_offset = insn->ret_offset;
1579 struct cfi_state *cfi = &state->cfi;
1582 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1585 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
1588 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1592 * If there is a ret offset hint then don't check registers
1593 * because a callee-saved register might have been pushed on
1599 for (i = 0; i < CFI_NUM_REGS; i++) {
1600 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1601 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1608 static bool has_valid_stack_frame(struct insn_state *state)
1610 struct cfi_state *cfi = &state->cfi;
1612 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1613 cfi->regs[CFI_BP].offset == -16)
1616 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1622 static int update_cfi_state_regs(struct instruction *insn,
1623 struct cfi_state *cfi,
1624 struct stack_op *op)
1626 struct cfi_reg *cfa = &cfi->cfa;
1628 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1632 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1636 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1639 /* add immediate to sp */
1640 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1641 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1642 cfa->offset -= op->src.offset;
1647 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1649 if (arch_callee_saved_reg(reg) &&
1650 cfi->regs[reg].base == CFI_UNDEFINED) {
1651 cfi->regs[reg].base = base;
1652 cfi->regs[reg].offset = offset;
1656 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1658 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1659 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1663 * A note about DRAP stack alignment:
1665 * GCC has the concept of a DRAP register, which is used to help keep track of
1666 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1667 * register. The typical DRAP pattern is:
1669 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1670 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1671 * 41 ff 72 f8 pushq -0x8(%r10)
1673 * 48 89 e5 mov %rsp,%rbp
1680 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1683 * There are some variations in the epilogues, like:
1691 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1696 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1697 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1698 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1699 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1701 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1704 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1705 * restored beforehand:
1708 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1709 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1711 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1715 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1716 struct stack_op *op)
1718 struct cfi_reg *cfa = &cfi->cfa;
1719 struct cfi_reg *regs = cfi->regs;
1721 /* stack operations don't make sense with an undefined CFA */
1722 if (cfa->base == CFI_UNDEFINED) {
1724 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1730 if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1731 return update_cfi_state_regs(insn, cfi, op);
1733 switch (op->dest.type) {
1736 switch (op->src.type) {
1739 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1740 cfa->base == CFI_SP &&
1741 regs[CFI_BP].base == CFI_CFA &&
1742 regs[CFI_BP].offset == -cfa->offset) {
1744 /* mov %rsp, %rbp */
1745 cfa->base = op->dest.reg;
1746 cfi->bp_scratch = false;
1749 else if (op->src.reg == CFI_SP &&
1750 op->dest.reg == CFI_BP && cfi->drap) {
1752 /* drap: mov %rsp, %rbp */
1753 regs[CFI_BP].base = CFI_BP;
1754 regs[CFI_BP].offset = -cfi->stack_size;
1755 cfi->bp_scratch = false;
1758 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1763 * This is needed for the rare case where GCC
1770 cfi->vals[op->dest.reg].base = CFI_CFA;
1771 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1774 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1775 cfa->base == CFI_BP) {
1780 * Restore the original stack pointer (Clang).
1782 cfi->stack_size = -cfi->regs[CFI_BP].offset;
1785 else if (op->dest.reg == cfa->base) {
1787 /* mov %reg, %rsp */
1788 if (cfa->base == CFI_SP &&
1789 cfi->vals[op->src.reg].base == CFI_CFA) {
1792 * This is needed for the rare case
1793 * where GCC does something dumb like:
1795 * lea 0x8(%rsp), %rcx
1799 cfa->offset = -cfi->vals[op->src.reg].offset;
1800 cfi->stack_size = cfa->offset;
1803 cfa->base = CFI_UNDEFINED;
1811 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1814 cfi->stack_size -= op->src.offset;
1815 if (cfa->base == CFI_SP)
1816 cfa->offset -= op->src.offset;
1820 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1822 /* lea disp(%rbp), %rsp */
1823 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1827 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1829 /* drap: lea disp(%rsp), %drap */
1830 cfi->drap_reg = op->dest.reg;
1833 * lea disp(%rsp), %reg
1835 * This is needed for the rare case where GCC
1836 * does something dumb like:
1838 * lea 0x8(%rsp), %rcx
1842 cfi->vals[op->dest.reg].base = CFI_CFA;
1843 cfi->vals[op->dest.reg].offset = \
1844 -cfi->stack_size + op->src.offset;
1849 if (cfi->drap && op->dest.reg == CFI_SP &&
1850 op->src.reg == cfi->drap_reg) {
1852 /* drap: lea disp(%drap), %rsp */
1854 cfa->offset = cfi->stack_size = -op->src.offset;
1855 cfi->drap_reg = CFI_UNDEFINED;
1860 if (op->dest.reg == cfi->cfa.base) {
1861 WARN_FUNC("unsupported stack register modification",
1862 insn->sec, insn->offset);
1869 if (op->dest.reg != CFI_SP ||
1870 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1871 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1872 WARN_FUNC("unsupported stack pointer realignment",
1873 insn->sec, insn->offset);
1877 if (cfi->drap_reg != CFI_UNDEFINED) {
1878 /* drap: and imm, %rsp */
1879 cfa->base = cfi->drap_reg;
1880 cfa->offset = cfi->stack_size = 0;
1885 * Older versions of GCC (4.8ish) realign the stack
1886 * without DRAP, with a frame pointer.
1893 if (!cfi->drap && op->dest.reg == cfa->base) {
1899 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
1900 op->dest.reg == cfi->drap_reg &&
1901 cfi->drap_offset == -cfi->stack_size) {
1903 /* drap: pop %drap */
1904 cfa->base = cfi->drap_reg;
1906 cfi->drap_offset = -1;
1908 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
1911 restore_reg(cfi, op->dest.reg);
1914 cfi->stack_size -= 8;
1915 if (cfa->base == CFI_SP)
1920 case OP_SRC_REG_INDIRECT:
1921 if (cfi->drap && op->src.reg == CFI_BP &&
1922 op->src.offset == cfi->drap_offset) {
1924 /* drap: mov disp(%rbp), %drap */
1925 cfa->base = cfi->drap_reg;
1927 cfi->drap_offset = -1;
1930 if (cfi->drap && op->src.reg == CFI_BP &&
1931 op->src.offset == regs[op->dest.reg].offset) {
1933 /* drap: mov disp(%rbp), %reg */
1934 restore_reg(cfi, op->dest.reg);
1936 } else if (op->src.reg == cfa->base &&
1937 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1939 /* mov disp(%rbp), %reg */
1940 /* mov disp(%rsp), %reg */
1941 restore_reg(cfi, op->dest.reg);
1947 WARN_FUNC("unknown stack-related instruction",
1948 insn->sec, insn->offset);
1956 cfi->stack_size += 8;
1957 if (cfa->base == CFI_SP)
1960 if (op->src.type != OP_SRC_REG)
1964 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
1966 /* drap: push %drap */
1967 cfa->base = CFI_BP_INDIRECT;
1968 cfa->offset = -cfi->stack_size;
1970 /* save drap so we know when to restore it */
1971 cfi->drap_offset = -cfi->stack_size;
1973 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
1975 /* drap: push %rbp */
1976 cfi->stack_size = 0;
1978 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1980 /* drap: push %reg */
1981 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
1987 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
1990 /* detect when asm code uses rbp as a scratch register */
1991 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1992 cfa->base != CFI_BP)
1993 cfi->bp_scratch = true;
1996 case OP_DEST_REG_INDIRECT:
1999 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2001 /* drap: mov %drap, disp(%rbp) */
2002 cfa->base = CFI_BP_INDIRECT;
2003 cfa->offset = op->dest.offset;
2005 /* save drap offset so we know when to restore it */
2006 cfi->drap_offset = op->dest.offset;
2009 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2011 /* drap: mov reg, disp(%rbp) */
2012 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2015 } else if (op->dest.reg == cfa->base) {
2017 /* mov reg, disp(%rbp) */
2018 /* mov reg, disp(%rsp) */
2019 save_reg(cfi, op->src.reg, CFI_CFA,
2020 op->dest.offset - cfi->cfa.offset);
2026 if ((!cfi->drap && cfa->base != CFI_BP) ||
2027 (cfi->drap && cfa->base != cfi->drap_reg)) {
2028 WARN_FUNC("leave instruction with modified stack frame",
2029 insn->sec, insn->offset);
2033 /* leave (mov %rbp, %rsp; pop %rbp) */
2035 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2036 restore_reg(cfi, CFI_BP);
2046 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2047 WARN_FUNC("unknown stack-related memory operation",
2048 insn->sec, insn->offset);
2053 cfi->stack_size -= 8;
2054 if (cfa->base == CFI_SP)
2060 WARN_FUNC("unknown stack-related instruction",
2061 insn->sec, insn->offset);
2068 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2070 struct stack_op *op;
2072 list_for_each_entry(op, &insn->stack_ops, list) {
2073 struct cfi_state old_cfi = state->cfi;
2076 res = update_cfi_state(insn, &state->cfi, op);
2080 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2081 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2085 if (op->dest.type == OP_DEST_PUSHF) {
2086 if (!state->uaccess_stack) {
2087 state->uaccess_stack = 1;
2088 } else if (state->uaccess_stack >> 31) {
2089 WARN_FUNC("PUSHF stack exhausted",
2090 insn->sec, insn->offset);
2093 state->uaccess_stack <<= 1;
2094 state->uaccess_stack |= state->uaccess;
2097 if (op->src.type == OP_SRC_POPF) {
2098 if (state->uaccess_stack) {
2099 state->uaccess = state->uaccess_stack & 1;
2100 state->uaccess_stack >>= 1;
2101 if (state->uaccess_stack == 1)
2102 state->uaccess_stack = 0;
2110 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2112 struct cfi_state *cfi1 = &insn->cfi;
2115 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2117 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2118 insn->sec, insn->offset,
2119 cfi1->cfa.base, cfi1->cfa.offset,
2120 cfi2->cfa.base, cfi2->cfa.offset);
2122 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2123 for (i = 0; i < CFI_NUM_REGS; i++) {
2124 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2125 sizeof(struct cfi_reg)))
2128 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2129 insn->sec, insn->offset,
2130 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2131 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2135 } else if (cfi1->type != cfi2->type) {
2137 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2138 insn->sec, insn->offset, cfi1->type, cfi2->type);
2140 } else if (cfi1->drap != cfi2->drap ||
2141 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2142 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2144 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2145 insn->sec, insn->offset,
2146 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2147 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2155 static inline bool func_uaccess_safe(struct symbol *func)
2158 return func->uaccess_safe;
2163 static inline const char *call_dest_name(struct instruction *insn)
2165 if (insn->call_dest)
2166 return insn->call_dest->name;
2171 static int validate_call(struct instruction *insn, struct insn_state *state)
2173 if (state->noinstr && state->instr <= 0 &&
2174 (!insn->call_dest || !insn->call_dest->sec->noinstr)) {
2175 WARN_FUNC("call to %s() leaves .noinstr.text section",
2176 insn->sec, insn->offset, call_dest_name(insn));
2180 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2181 WARN_FUNC("call to %s() with UACCESS enabled",
2182 insn->sec, insn->offset, call_dest_name(insn));
2187 WARN_FUNC("call to %s() with DF set",
2188 insn->sec, insn->offset, call_dest_name(insn));
2195 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2197 if (has_modified_stack_frame(insn, state)) {
2198 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2199 insn->sec, insn->offset);
2203 return validate_call(insn, state);
2206 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2208 if (state->noinstr && state->instr > 0) {
2209 WARN_FUNC("return with instrumentation enabled",
2210 insn->sec, insn->offset);
2214 if (state->uaccess && !func_uaccess_safe(func)) {
2215 WARN_FUNC("return with UACCESS enabled",
2216 insn->sec, insn->offset);
2220 if (!state->uaccess && func_uaccess_safe(func)) {
2221 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2222 insn->sec, insn->offset);
2227 WARN_FUNC("return with DF set",
2228 insn->sec, insn->offset);
2232 if (func && has_modified_stack_frame(insn, state)) {
2233 WARN_FUNC("return with modified stack frame",
2234 insn->sec, insn->offset);
2238 if (state->cfi.bp_scratch) {
2239 WARN_FUNC("BP used as a scratch register",
2240 insn->sec, insn->offset);
2248 * Alternatives should not contain any ORC entries, this in turn means they
2249 * should not contain any CFI ops, which implies all instructions should have
2250 * the same same CFI state.
2252 * It is possible to constuct alternatives that have unreachable holes that go
2253 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2254 * states which then results in ORC entries, which we just said we didn't want.
2256 * Avoid them by copying the CFI entry of the first instruction into the whole
2259 static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2261 struct instruction *first_insn = insn;
2262 int alt_group = insn->alt_group;
2264 sec_for_each_insn_continue(file, insn) {
2265 if (insn->alt_group != alt_group)
2267 insn->cfi = first_insn->cfi;
2272 * Follow the branch starting at the given instruction, and recursively follow
2273 * any other branches (jumps). Meanwhile, track the frame pointer state at
2274 * each instruction and validate all the rules described in
2275 * tools/objtool/Documentation/stack-validation.txt.
2277 static int validate_branch(struct objtool_file *file, struct symbol *func,
2278 struct instruction *insn, struct insn_state state)
2280 struct alternative *alt;
2281 struct instruction *next_insn;
2282 struct section *sec;
2289 next_insn = next_insn_same_sec(file, insn);
2291 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2292 WARN("%s() falls through to next function %s()",
2293 func->name, insn->func->name);
2297 if (func && insn->ignore) {
2298 WARN_FUNC("BUG: why am I validating an ignored function?",
2303 visited = 1 << state.uaccess;
2304 if (insn->visited) {
2305 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2308 if (insn->visited & visited)
2313 state.instr += insn->instr;
2316 state.cfi = insn->cfi;
2318 insn->cfi = state.cfi;
2320 insn->visited |= visited;
2322 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2323 bool skip_orig = false;
2325 list_for_each_entry(alt, &insn->alts, list) {
2329 ret = validate_branch(file, func, alt->insn, state);
2332 BT_FUNC("(alt)", insn);
2337 if (insn->alt_group)
2338 fill_alternative_cfi(file, insn);
2344 if (handle_insn_ops(insn, &state))
2347 switch (insn->type) {
2350 return validate_return(func, insn, &state);
2353 case INSN_CALL_DYNAMIC:
2354 ret = validate_call(insn, &state);
2358 if (!no_fp && func && !is_fentry_call(insn) &&
2359 !has_valid_stack_frame(&state)) {
2360 WARN_FUNC("call without frame pointer save/setup",
2365 if (dead_end_function(file, insn->call_dest))
2370 case INSN_JUMP_CONDITIONAL:
2371 case INSN_JUMP_UNCONDITIONAL:
2372 if (func && is_sibling_call(insn)) {
2373 ret = validate_sibling_call(insn, &state);
2377 } else if (insn->jump_dest) {
2378 ret = validate_branch(file, func,
2379 insn->jump_dest, state);
2382 BT_FUNC("(branch)", insn);
2387 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2392 case INSN_JUMP_DYNAMIC:
2393 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2394 if (func && is_sibling_call(insn)) {
2395 ret = validate_sibling_call(insn, &state);
2400 if (insn->type == INSN_JUMP_DYNAMIC)
2405 case INSN_CONTEXT_SWITCH:
2406 if (func && (!next_insn || !next_insn->hint)) {
2407 WARN_FUNC("unsupported instruction in callable function",
2414 if (state.uaccess) {
2415 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2419 state.uaccess = true;
2423 if (!state.uaccess && func) {
2424 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2428 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2429 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2433 state.uaccess = false;
2438 WARN_FUNC("recursive STD", sec, insn->offset);
2444 if (!state.df && func)
2445 WARN_FUNC("redundant CLD", sec, insn->offset);
2458 if (state.cfi.cfa.base == CFI_UNDEFINED)
2460 WARN("%s: unexpected end of section", sec->name);
2470 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2472 struct instruction *insn;
2473 struct insn_state state;
2474 int ret, warnings = 0;
2479 init_insn_state(&state, sec);
2482 insn = find_insn(file, sec, 0);
2486 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2489 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2490 if (insn->hint && !insn->visited) {
2491 ret = validate_branch(file, insn->func, insn, state);
2492 if (ret && backtrace)
2493 BT_FUNC("<=== (hint)", insn);
2497 insn = list_next_entry(insn, list);
2503 static int validate_retpoline(struct objtool_file *file)
2505 struct instruction *insn;
2508 for_each_insn(file, insn) {
2509 if (insn->type != INSN_JUMP_DYNAMIC &&
2510 insn->type != INSN_CALL_DYNAMIC)
2513 if (insn->retpoline_safe)
2517 * .init.text code is ran before userspace and thus doesn't
2518 * strictly need retpolines, except for modules which are
2519 * loaded late, they very much do need retpoline in their
2522 if (!strcmp(insn->sec->name, ".init.text") && !module)
2525 WARN_FUNC("indirect %s found in RETPOLINE build",
2526 insn->sec, insn->offset,
2527 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2535 static bool is_kasan_insn(struct instruction *insn)
2537 return (insn->type == INSN_CALL &&
2538 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2541 static bool is_ubsan_insn(struct instruction *insn)
2543 return (insn->type == INSN_CALL &&
2544 !strcmp(insn->call_dest->name,
2545 "__ubsan_handle_builtin_unreachable"));
2548 static bool ignore_unreachable_insn(struct instruction *insn)
2552 if (insn->ignore || insn->type == INSN_NOP)
2556 * Ignore any unused exceptions. This can happen when a whitelisted
2557 * function has an exception table entry.
2559 * Also ignore alternative replacement instructions. This can happen
2560 * when a whitelisted function uses one of the ALTERNATIVE macros.
2562 if (!strcmp(insn->sec->name, ".fixup") ||
2563 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2564 !strcmp(insn->sec->name, ".altinstr_aux"))
2571 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2572 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2573 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2574 * (or occasionally a JMP to UD2).
2576 if (list_prev_entry(insn, list)->dead_end &&
2577 (insn->type == INSN_BUG ||
2578 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2579 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2583 * Check if this (or a subsequent) instruction is related to
2584 * CONFIG_UBSAN or CONFIG_KASAN.
2586 * End the search at 5 instructions to avoid going into the weeds.
2588 for (i = 0; i < 5; i++) {
2590 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2593 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2594 if (insn->jump_dest &&
2595 insn->jump_dest->func == insn->func) {
2596 insn = insn->jump_dest;
2603 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2606 insn = list_next_entry(insn, list);
2612 static int validate_symbol(struct objtool_file *file, struct section *sec,
2613 struct symbol *sym, struct insn_state *state)
2615 struct instruction *insn;
2619 WARN("%s() is missing an ELF size annotation", sym->name);
2623 if (sym->pfunc != sym || sym->alias != sym)
2626 insn = find_insn(file, sec, sym->offset);
2627 if (!insn || insn->ignore || insn->visited)
2630 state->uaccess = sym->uaccess_safe;
2632 ret = validate_branch(file, insn->func, insn, *state);
2633 if (ret && backtrace)
2634 BT_FUNC("<=== (sym)", insn);
2638 static int validate_section(struct objtool_file *file, struct section *sec)
2640 struct insn_state state;
2641 struct symbol *func;
2644 list_for_each_entry(func, &sec->symbol_list, list) {
2645 if (func->type != STT_FUNC)
2648 init_insn_state(&state, sec);
2649 state.cfi.cfa = initial_func_cfi.cfa;
2650 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
2651 CFI_NUM_REGS * sizeof(struct cfi_reg));
2652 state.cfi.stack_size = initial_func_cfi.cfa.offset;
2654 warnings += validate_symbol(file, sec, func, &state);
2660 static int validate_vmlinux_functions(struct objtool_file *file)
2662 struct section *sec;
2665 sec = find_section_by_name(file->elf, ".noinstr.text");
2667 warnings += validate_section(file, sec);
2668 warnings += validate_unwind_hints(file, sec);
2671 sec = find_section_by_name(file->elf, ".entry.text");
2673 warnings += validate_section(file, sec);
2674 warnings += validate_unwind_hints(file, sec);
2680 static int validate_functions(struct objtool_file *file)
2682 struct section *sec;
2685 for_each_sec(file, sec) {
2686 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2689 warnings += validate_section(file, sec);
2695 static int validate_reachable_instructions(struct objtool_file *file)
2697 struct instruction *insn;
2699 if (file->ignore_unreachables)
2702 for_each_insn(file, insn) {
2703 if (insn->visited || ignore_unreachable_insn(insn))
2706 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2713 static struct objtool_file file;
2715 int check(const char *_objname, bool orc)
2717 int ret, warnings = 0;
2721 file.elf = elf_open_read(objname, orc ? O_RDWR : O_RDONLY);
2725 INIT_LIST_HEAD(&file.insn_list);
2726 hash_init(file.insn_hash);
2727 file.c_file = find_section_by_name(file.elf, ".comment");
2728 file.ignore_unreachables = no_unreachable;
2731 arch_initial_func_cfi_state(&initial_func_cfi);
2733 ret = decode_sections(&file);
2738 if (list_empty(&file.insn_list))
2741 if (vmlinux && !validate_dup) {
2742 ret = validate_vmlinux_functions(&file);
2751 ret = validate_retpoline(&file);
2757 ret = validate_functions(&file);
2762 ret = validate_unwind_hints(&file, NULL);
2768 ret = validate_reachable_instructions(&file);
2775 ret = create_orc(&file);
2779 ret = create_orc_sections(&file);
2783 ret = elf_write(file.elf);
2791 * Fatal error. The binary is corrupt or otherwise broken in
2792 * some way, or objtool itself is broken. Fail the kernel