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 #define func_for_each_insn(file, func, insn) \
77 for (insn = find_insn(file, func->sec, func->offset); \
79 insn = next_insn_same_func(file, insn))
81 #define sym_for_each_insn(file, sym, insn) \
82 for (insn = find_insn(file, sym->sec, sym->offset); \
83 insn && &insn->list != &file->insn_list && \
84 insn->sec == sym->sec && \
85 insn->offset < sym->offset + sym->len; \
86 insn = list_next_entry(insn, list))
88 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
89 for (insn = list_prev_entry(insn, list); \
90 &insn->list != &file->insn_list && \
91 insn->sec == sym->sec && insn->offset >= sym->offset; \
92 insn = list_prev_entry(insn, list))
94 #define sec_for_each_insn_from(file, insn) \
95 for (; insn; insn = next_insn_same_sec(file, insn))
97 #define sec_for_each_insn_continue(file, insn) \
98 for (insn = next_insn_same_sec(file, insn); insn; \
99 insn = next_insn_same_sec(file, insn))
101 static bool is_static_jump(struct instruction *insn)
103 return insn->type == INSN_JUMP_CONDITIONAL ||
104 insn->type == INSN_JUMP_UNCONDITIONAL;
107 static bool is_sibling_call(struct instruction *insn)
109 /* An indirect jump is either a sibling call or a jump to a table. */
110 if (insn->type == INSN_JUMP_DYNAMIC)
111 return list_empty(&insn->alts);
113 if (!is_static_jump(insn))
116 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
117 return !!insn->call_dest;
121 * This checks to see if the given function is a "noreturn" function.
123 * For global functions which are outside the scope of this object file, we
124 * have to keep a manual list of them.
126 * For local functions, we have to detect them manually by simply looking for
127 * the lack of a return instruction.
129 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
133 struct instruction *insn;
137 * Unfortunately these have to be hard coded because the noreturn
138 * attribute isn't provided in ELF data.
140 static const char * const global_noreturns[] = {
145 "__module_put_and_exit",
151 "machine_real_restart",
152 "rewind_stack_do_exit",
153 "kunit_try_catch_throw",
159 if (func->bind == STB_WEAK)
162 if (func->bind == STB_GLOBAL)
163 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
164 if (!strcmp(func->name, global_noreturns[i]))
170 insn = find_insn(file, func->sec, func->offset);
174 func_for_each_insn(file, func, insn) {
177 if (insn->type == INSN_RETURN)
185 * A function can have a sibling call instead of a return. In that
186 * case, the function's dead-end status depends on whether the target
187 * of the sibling call returns.
189 func_for_each_insn(file, func, insn) {
190 if (is_sibling_call(insn)) {
191 struct instruction *dest = insn->jump_dest;
194 /* sibling call to another file */
197 /* local sibling call */
198 if (recursion == 5) {
200 * Infinite recursion: two functions have
201 * sibling calls to each other. This is a very
202 * rare case. It means they aren't dead ends.
207 return __dead_end_function(file, dest->func, recursion+1);
214 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
216 return __dead_end_function(file, func, 0);
219 static void init_cfi_state(struct cfi_state *cfi)
223 for (i = 0; i < CFI_NUM_REGS; i++) {
224 cfi->regs[i].base = CFI_UNDEFINED;
225 cfi->vals[i].base = CFI_UNDEFINED;
227 cfi->cfa.base = CFI_UNDEFINED;
228 cfi->drap_reg = CFI_UNDEFINED;
229 cfi->drap_offset = -1;
232 static void init_insn_state(struct insn_state *state, struct section *sec)
234 memset(state, 0, sizeof(*state));
235 init_cfi_state(&state->cfi);
238 * We need the full vmlinux for noinstr validation, otherwise we can
239 * not correctly determine insn->call_dest->sec (external symbols do
240 * not have a section).
243 state->noinstr = sec->noinstr;
247 * Call the arch-specific instruction decoder for all the instructions and add
248 * them to the global instruction list.
250 static int decode_instructions(struct objtool_file *file)
254 unsigned long offset;
255 struct instruction *insn;
256 unsigned long nr_insns = 0;
259 for_each_sec(file, sec) {
261 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
264 if (strcmp(sec->name, ".altinstr_replacement") &&
265 strcmp(sec->name, ".altinstr_aux") &&
266 strncmp(sec->name, ".discard.", 9))
269 if (!strcmp(sec->name, ".noinstr.text") ||
270 !strcmp(sec->name, ".entry.text"))
273 for (offset = 0; offset < sec->len; offset += insn->len) {
274 insn = malloc(sizeof(*insn));
276 WARN("malloc failed");
279 memset(insn, 0, sizeof(*insn));
280 INIT_LIST_HEAD(&insn->alts);
281 INIT_LIST_HEAD(&insn->stack_ops);
282 init_cfi_state(&insn->cfi);
285 insn->offset = offset;
287 ret = arch_decode_instruction(file->elf, sec, offset,
289 &insn->len, &insn->type,
295 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
296 list_add_tail(&insn->list, &file->insn_list);
300 list_for_each_entry(func, &sec->symbol_list, list) {
301 if (func->type != STT_FUNC || func->alias != func)
304 if (!find_insn(file, sec, func->offset)) {
305 WARN("%s(): can't find starting instruction",
310 sym_for_each_insn(file, func, insn)
316 printf("nr_insns: %lu\n", nr_insns);
326 * Mark "ud2" instructions and manually annotated dead ends.
328 static int add_dead_ends(struct objtool_file *file)
332 struct instruction *insn;
336 * By default, "ud2" is a dead end unless otherwise annotated, because
337 * GCC 7 inserts it for certain divide-by-zero cases.
339 for_each_insn(file, insn)
340 if (insn->type == INSN_BUG)
341 insn->dead_end = true;
344 * Check for manually annotated dead ends.
346 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
350 list_for_each_entry(rela, &sec->rela_list, list) {
351 if (rela->sym->type != STT_SECTION) {
352 WARN("unexpected relocation symbol type in %s", sec->name);
355 insn = find_insn(file, rela->sym->sec, rela->addend);
357 insn = list_prev_entry(insn, list);
358 else if (rela->addend == rela->sym->sec->len) {
360 list_for_each_entry_reverse(insn, &file->insn_list, list) {
361 if (insn->sec == rela->sym->sec) {
368 WARN("can't find unreachable insn at %s+0x%x",
369 rela->sym->sec->name, rela->addend);
373 WARN("can't find unreachable insn at %s+0x%x",
374 rela->sym->sec->name, rela->addend);
378 insn->dead_end = true;
383 * These manually annotated reachable checks are needed for GCC 4.4,
384 * where the Linux unreachable() macro isn't supported. In that case
385 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
388 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
392 list_for_each_entry(rela, &sec->rela_list, list) {
393 if (rela->sym->type != STT_SECTION) {
394 WARN("unexpected relocation symbol type in %s", sec->name);
397 insn = find_insn(file, rela->sym->sec, rela->addend);
399 insn = list_prev_entry(insn, list);
400 else if (rela->addend == rela->sym->sec->len) {
402 list_for_each_entry_reverse(insn, &file->insn_list, list) {
403 if (insn->sec == rela->sym->sec) {
410 WARN("can't find reachable insn at %s+0x%x",
411 rela->sym->sec->name, rela->addend);
415 WARN("can't find reachable insn at %s+0x%x",
416 rela->sym->sec->name, rela->addend);
420 insn->dead_end = false;
427 * Warnings shouldn't be reported for ignored functions.
429 static void add_ignores(struct objtool_file *file)
431 struct instruction *insn;
436 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
440 list_for_each_entry(rela, &sec->rela_list, list) {
441 switch (rela->sym->type) {
447 func = find_func_by_offset(rela->sym->sec, rela->addend);
453 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
457 func_for_each_insn(file, func, insn)
463 * This is a whitelist of functions that is allowed to be called with AC set.
464 * The list is meant to be minimal and only contains compiler instrumentation
465 * ABI and a few functions used to implement *_{to,from}_user() functions.
467 * These functions must not directly change AC, but may PUSHF/POPF.
469 static const char *uaccess_safe_builtin[] = {
472 "check_memory_region",
473 /* KASAN out-of-line */
474 "__asan_loadN_noabort",
475 "__asan_load1_noabort",
476 "__asan_load2_noabort",
477 "__asan_load4_noabort",
478 "__asan_load8_noabort",
479 "__asan_load16_noabort",
480 "__asan_storeN_noabort",
481 "__asan_store1_noabort",
482 "__asan_store2_noabort",
483 "__asan_store4_noabort",
484 "__asan_store8_noabort",
485 "__asan_store16_noabort",
487 "__asan_report_load_n_noabort",
488 "__asan_report_load1_noabort",
489 "__asan_report_load2_noabort",
490 "__asan_report_load4_noabort",
491 "__asan_report_load8_noabort",
492 "__asan_report_load16_noabort",
493 "__asan_report_store_n_noabort",
494 "__asan_report_store1_noabort",
495 "__asan_report_store2_noabort",
496 "__asan_report_store4_noabort",
497 "__asan_report_store8_noabort",
498 "__asan_report_store16_noabort",
501 "__sanitizer_cov_trace_pc",
502 "__sanitizer_cov_trace_const_cmp1",
503 "__sanitizer_cov_trace_const_cmp2",
504 "__sanitizer_cov_trace_const_cmp4",
505 "__sanitizer_cov_trace_const_cmp8",
506 "__sanitizer_cov_trace_cmp1",
507 "__sanitizer_cov_trace_cmp2",
508 "__sanitizer_cov_trace_cmp4",
509 "__sanitizer_cov_trace_cmp8",
510 "__sanitizer_cov_trace_switch",
512 "ubsan_type_mismatch_common",
513 "__ubsan_handle_type_mismatch",
514 "__ubsan_handle_type_mismatch_v1",
515 "__ubsan_handle_shift_out_of_bounds",
517 "csum_partial_copy_generic",
519 "mcsafe_handle_tail",
520 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
524 static void add_uaccess_safe(struct objtool_file *file)
532 for (name = uaccess_safe_builtin; *name; name++) {
533 func = find_symbol_by_name(file->elf, *name);
537 func->uaccess_safe = true;
542 * FIXME: For now, just ignore any alternatives which add retpolines. This is
543 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
544 * But it at least allows objtool to understand the control flow *around* the
547 static int add_ignore_alternatives(struct objtool_file *file)
551 struct instruction *insn;
553 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
557 list_for_each_entry(rela, &sec->rela_list, list) {
558 if (rela->sym->type != STT_SECTION) {
559 WARN("unexpected relocation symbol type in %s", sec->name);
563 insn = find_insn(file, rela->sym->sec, rela->addend);
565 WARN("bad .discard.ignore_alts entry");
569 insn->ignore_alts = true;
576 * Find the destination instructions for all jumps.
578 static int add_jump_destinations(struct objtool_file *file)
580 struct instruction *insn;
582 struct section *dest_sec;
583 unsigned long dest_off;
585 for_each_insn(file, insn) {
586 if (!is_static_jump(insn))
589 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
592 rela = find_rela_by_dest_range(file->elf, insn->sec,
593 insn->offset, insn->len);
595 dest_sec = insn->sec;
596 dest_off = arch_jump_destination(insn);
597 } else if (rela->sym->type == STT_SECTION) {
598 dest_sec = rela->sym->sec;
599 dest_off = arch_dest_rela_offset(rela->addend);
600 } else if (rela->sym->sec->idx) {
601 dest_sec = rela->sym->sec;
602 dest_off = rela->sym->sym.st_value +
603 arch_dest_rela_offset(rela->addend);
604 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
606 * Retpoline jumps are really dynamic jumps in
607 * disguise, so convert them accordingly.
609 if (insn->type == INSN_JUMP_UNCONDITIONAL)
610 insn->type = INSN_JUMP_DYNAMIC;
612 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
614 insn->retpoline_safe = true;
617 /* external sibling call */
618 insn->call_dest = rela->sym;
622 insn->jump_dest = find_insn(file, dest_sec, dest_off);
623 if (!insn->jump_dest) {
626 * This is a special case where an alt instruction
627 * jumps past the end of the section. These are
628 * handled later in handle_group_alt().
630 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
633 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
634 insn->sec, insn->offset, dest_sec->name,
640 * Cross-function jump.
642 if (insn->func && insn->jump_dest->func &&
643 insn->func != insn->jump_dest->func) {
646 * For GCC 8+, create parent/child links for any cold
647 * subfunctions. This is _mostly_ redundant with a
648 * similar initialization in read_symbols().
650 * If a function has aliases, we want the *first* such
651 * function in the symbol table to be the subfunction's
652 * parent. In that case we overwrite the
653 * initialization done in read_symbols().
655 * However this code can't completely replace the
656 * read_symbols() code because this doesn't detect the
657 * case where the parent function's only reference to a
658 * subfunction is through a jump table.
660 if (!strstr(insn->func->name, ".cold.") &&
661 strstr(insn->jump_dest->func->name, ".cold.")) {
662 insn->func->cfunc = insn->jump_dest->func;
663 insn->jump_dest->func->pfunc = insn->func;
665 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
666 insn->jump_dest->offset == insn->jump_dest->func->offset) {
668 /* internal sibling call */
669 insn->call_dest = insn->jump_dest->func;
677 static void remove_insn_ops(struct instruction *insn)
679 struct stack_op *op, *tmp;
681 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
688 * Find the destination instructions for all calls.
690 static int add_call_destinations(struct objtool_file *file)
692 struct instruction *insn;
693 unsigned long dest_off;
696 for_each_insn(file, insn) {
697 if (insn->type != INSN_CALL)
700 rela = find_rela_by_dest_range(file->elf, insn->sec,
701 insn->offset, insn->len);
703 dest_off = arch_jump_destination(insn);
704 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
705 if (!insn->call_dest)
706 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
711 if (!insn->call_dest) {
712 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
716 if (insn->func && insn->call_dest->type != STT_FUNC) {
717 WARN_FUNC("unsupported call to non-function",
718 insn->sec, insn->offset);
722 } else if (rela->sym->type == STT_SECTION) {
723 dest_off = arch_dest_rela_offset(rela->addend);
724 insn->call_dest = find_func_by_offset(rela->sym->sec,
726 if (!insn->call_dest) {
727 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
728 insn->sec, insn->offset,
729 rela->sym->sec->name,
734 insn->call_dest = rela->sym;
737 * Whatever stack impact regular CALLs have, should be undone
738 * by the RETURN of the called function.
740 * Annotated intra-function calls retain the stack_ops but
741 * are converted to JUMP, see read_intra_function_calls().
743 remove_insn_ops(insn);
750 * The .alternatives section requires some extra special care, over and above
751 * what other special sections require:
753 * 1. Because alternatives are patched in-place, we need to insert a fake jump
754 * instruction at the end so that validate_branch() skips all the original
755 * replaced instructions when validating the new instruction path.
757 * 2. An added wrinkle is that the new instruction length might be zero. In
758 * that case the old instructions are replaced with noops. We simulate that
759 * by creating a fake jump as the only new instruction.
761 * 3. In some cases, the alternative section includes an instruction which
762 * conditionally jumps to the _end_ of the entry. We have to modify these
763 * jumps' destinations to point back to .text rather than the end of the
764 * entry in .altinstr_replacement.
766 static int handle_group_alt(struct objtool_file *file,
767 struct special_alt *special_alt,
768 struct instruction *orig_insn,
769 struct instruction **new_insn)
771 static unsigned int alt_group_next_index = 1;
772 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
773 unsigned int alt_group = alt_group_next_index++;
774 unsigned long dest_off;
776 last_orig_insn = NULL;
778 sec_for_each_insn_from(file, insn) {
779 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
782 insn->alt_group = alt_group;
783 last_orig_insn = insn;
786 if (next_insn_same_sec(file, last_orig_insn)) {
787 fake_jump = malloc(sizeof(*fake_jump));
789 WARN("malloc failed");
792 memset(fake_jump, 0, sizeof(*fake_jump));
793 INIT_LIST_HEAD(&fake_jump->alts);
794 INIT_LIST_HEAD(&fake_jump->stack_ops);
795 init_cfi_state(&fake_jump->cfi);
797 fake_jump->sec = special_alt->new_sec;
798 fake_jump->offset = FAKE_JUMP_OFFSET;
799 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
800 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
801 fake_jump->func = orig_insn->func;
804 if (!special_alt->new_len) {
806 WARN("%s: empty alternative at end of section",
807 special_alt->orig_sec->name);
811 *new_insn = fake_jump;
815 last_new_insn = NULL;
816 alt_group = alt_group_next_index++;
818 sec_for_each_insn_from(file, insn) {
819 if (insn->offset >= special_alt->new_off + special_alt->new_len)
822 last_new_insn = insn;
824 insn->ignore = orig_insn->ignore_alts;
825 insn->func = orig_insn->func;
826 insn->alt_group = alt_group;
829 * Since alternative replacement code is copy/pasted by the
830 * kernel after applying relocations, generally such code can't
831 * have relative-address relocation references to outside the
832 * .altinstr_replacement section, unless the arch's
833 * alternatives code can adjust the relative offsets
836 * The x86 alternatives code adjusts the offsets only when it
837 * encounters a branch instruction at the very beginning of the
840 if ((insn->offset != special_alt->new_off ||
841 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
842 find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
844 WARN_FUNC("unsupported relocation in alternatives section",
845 insn->sec, insn->offset);
849 if (!is_static_jump(insn))
852 if (!insn->immediate)
855 dest_off = arch_jump_destination(insn);
856 if (dest_off == special_alt->new_off + special_alt->new_len) {
858 WARN("%s: alternative jump to end of section",
859 special_alt->orig_sec->name);
862 insn->jump_dest = fake_jump;
865 if (!insn->jump_dest) {
866 WARN_FUNC("can't find alternative jump destination",
867 insn->sec, insn->offset);
872 if (!last_new_insn) {
873 WARN_FUNC("can't find last new alternative instruction",
874 special_alt->new_sec, special_alt->new_off);
879 list_add(&fake_jump->list, &last_new_insn->list);
885 * A jump table entry can either convert a nop to a jump or a jump to a nop.
886 * If the original instruction is a jump, make the alt entry an effective nop
887 * by just skipping the original instruction.
889 static int handle_jump_alt(struct objtool_file *file,
890 struct special_alt *special_alt,
891 struct instruction *orig_insn,
892 struct instruction **new_insn)
894 if (orig_insn->type == INSN_NOP)
897 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
898 WARN_FUNC("unsupported instruction at jump label",
899 orig_insn->sec, orig_insn->offset);
903 *new_insn = list_next_entry(orig_insn, list);
908 * Read all the special sections which have alternate instructions which can be
909 * patched in or redirected to at runtime. Each instruction having alternate
910 * instruction(s) has them added to its insn->alts list, which will be
911 * traversed in validate_branch().
913 static int add_special_section_alts(struct objtool_file *file)
915 struct list_head special_alts;
916 struct instruction *orig_insn, *new_insn;
917 struct special_alt *special_alt, *tmp;
918 struct alternative *alt;
921 ret = special_get_alts(file->elf, &special_alts);
925 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
927 orig_insn = find_insn(file, special_alt->orig_sec,
928 special_alt->orig_off);
930 WARN_FUNC("special: can't find orig instruction",
931 special_alt->orig_sec, special_alt->orig_off);
937 if (!special_alt->group || special_alt->new_len) {
938 new_insn = find_insn(file, special_alt->new_sec,
939 special_alt->new_off);
941 WARN_FUNC("special: can't find new instruction",
942 special_alt->new_sec,
943 special_alt->new_off);
949 if (special_alt->group) {
950 if (!special_alt->orig_len) {
951 WARN_FUNC("empty alternative entry",
952 orig_insn->sec, orig_insn->offset);
956 ret = handle_group_alt(file, special_alt, orig_insn,
960 } else if (special_alt->jump_or_nop) {
961 ret = handle_jump_alt(file, special_alt, orig_insn,
967 alt = malloc(sizeof(*alt));
969 WARN("malloc failed");
974 alt->insn = new_insn;
975 alt->skip_orig = special_alt->skip_orig;
976 orig_insn->ignore_alts |= special_alt->skip_alt;
977 list_add_tail(&alt->list, &orig_insn->alts);
979 list_del(&special_alt->list);
987 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
990 struct rela *rela = table;
991 struct instruction *dest_insn;
992 struct alternative *alt;
993 struct symbol *pfunc = insn->func->pfunc;
994 unsigned int prev_offset = 0;
997 * Each @rela is a switch table relocation which points to the target
1000 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
1002 /* Check for the end of the table: */
1003 if (rela != table && rela->jump_table_start)
1006 /* Make sure the table entries are consecutive: */
1007 if (prev_offset && rela->offset != prev_offset + 8)
1010 /* Detect function pointers from contiguous objects: */
1011 if (rela->sym->sec == pfunc->sec &&
1012 rela->addend == pfunc->offset)
1015 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
1019 /* Make sure the destination is in the same function: */
1020 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1023 alt = malloc(sizeof(*alt));
1025 WARN("malloc failed");
1029 alt->insn = dest_insn;
1030 list_add_tail(&alt->list, &insn->alts);
1031 prev_offset = rela->offset;
1035 WARN_FUNC("can't find switch jump table",
1036 insn->sec, insn->offset);
1044 * find_jump_table() - Given a dynamic jump, find the switch jump table in
1045 * .rodata associated with it.
1047 * There are 3 basic patterns:
1049 * 1. jmpq *[rodata addr](,%reg,8)
1051 * This is the most common case by far. It jumps to an address in a simple
1052 * jump table which is stored in .rodata.
1054 * 2. jmpq *[rodata addr](%rip)
1056 * This is caused by a rare GCC quirk, currently only seen in three driver
1057 * functions in the kernel, only with certain obscure non-distro configs.
1059 * As part of an optimization, GCC makes a copy of an existing switch jump
1060 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1061 * jump) to use a single entry in the table. The rest of the jump table and
1062 * some of its jump targets remain as dead code.
1064 * In such a case we can just crudely ignore all unreachable instruction
1065 * warnings for the entire object file. Ideally we would just ignore them
1066 * for the function, but that would require redesigning the code quite a
1067 * bit. And honestly that's just not worth doing: unreachable instruction
1068 * warnings are of questionable value anyway, and this is such a rare issue.
1070 * 3. mov [rodata addr],%reg1
1071 * ... some instructions ...
1072 * jmpq *(%reg1,%reg2,8)
1074 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1075 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1077 * As of GCC 7 there are quite a few more of these and the 'in between' code
1078 * is significant. Esp. with KASAN enabled some of the code between the mov
1079 * and jmpq uses .rodata itself, which can confuse things.
1081 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1082 * ensure the same register is used in the mov and jump instructions.
1084 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1086 static struct rela *find_jump_table(struct objtool_file *file,
1087 struct symbol *func,
1088 struct instruction *insn)
1090 struct rela *text_rela, *table_rela;
1091 struct instruction *dest_insn, *orig_insn = insn;
1092 struct section *table_sec;
1093 unsigned long table_offset;
1096 * Backward search using the @first_jump_src links, these help avoid
1097 * much of the 'in between' code. Which avoids us getting confused by
1101 &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func;
1102 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1104 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1107 /* allow small jumps within the range */
1108 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1110 (insn->jump_dest->offset <= insn->offset ||
1111 insn->jump_dest->offset > orig_insn->offset))
1114 /* look for a relocation which references .rodata */
1115 text_rela = find_rela_by_dest_range(file->elf, insn->sec,
1116 insn->offset, insn->len);
1117 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1118 !text_rela->sym->sec->rodata)
1121 table_offset = text_rela->addend;
1122 table_sec = text_rela->sym->sec;
1124 if (text_rela->type == R_X86_64_PC32)
1128 * Make sure the .rodata address isn't associated with a
1129 * symbol. GCC jump tables are anonymous data.
1131 * Also support C jump tables which are in the same format as
1132 * switch jump tables. For objtool to recognize them, they
1133 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1134 * have symbols associated with them.
1136 if (find_symbol_containing(table_sec, table_offset) &&
1137 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1141 * Each table entry has a rela associated with it. The rela
1142 * should reference text in the same function as the original
1145 table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
1148 dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
1149 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1153 * Use of RIP-relative switch jumps is quite rare, and
1154 * indicates a rare GCC quirk/bug which can leave dead code
1157 if (text_rela->type == R_X86_64_PC32)
1158 file->ignore_unreachables = true;
1167 * First pass: Mark the head of each jump table so that in the next pass,
1168 * we know when a given jump table ends and the next one starts.
1170 static void mark_func_jump_tables(struct objtool_file *file,
1171 struct symbol *func)
1173 struct instruction *insn, *last = NULL;
1176 func_for_each_insn(file, func, insn) {
1181 * Store back-pointers for unconditional forward jumps such
1182 * that find_jump_table() can back-track using those and
1183 * avoid some potentially confusing code.
1185 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1186 insn->offset > last->offset &&
1187 insn->jump_dest->offset > insn->offset &&
1188 !insn->jump_dest->first_jump_src) {
1190 insn->jump_dest->first_jump_src = insn;
1191 last = insn->jump_dest;
1194 if (insn->type != INSN_JUMP_DYNAMIC)
1197 rela = find_jump_table(file, func, insn);
1199 rela->jump_table_start = true;
1200 insn->jump_table = rela;
1205 static int add_func_jump_tables(struct objtool_file *file,
1206 struct symbol *func)
1208 struct instruction *insn;
1211 func_for_each_insn(file, func, insn) {
1212 if (!insn->jump_table)
1215 ret = add_jump_table(file, insn, insn->jump_table);
1224 * For some switch statements, gcc generates a jump table in the .rodata
1225 * section which contains a list of addresses within the function to jump to.
1226 * This finds these jump tables and adds them to the insn->alts lists.
1228 static int add_jump_table_alts(struct objtool_file *file)
1230 struct section *sec;
1231 struct symbol *func;
1237 for_each_sec(file, sec) {
1238 list_for_each_entry(func, &sec->symbol_list, list) {
1239 if (func->type != STT_FUNC)
1242 mark_func_jump_tables(file, func);
1243 ret = add_func_jump_tables(file, func);
1252 static int read_unwind_hints(struct objtool_file *file)
1254 struct section *sec, *relasec;
1256 struct unwind_hint *hint;
1257 struct instruction *insn;
1258 struct cfi_reg *cfa;
1261 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1265 relasec = sec->rela;
1267 WARN("missing .rela.discard.unwind_hints section");
1271 if (sec->len % sizeof(struct unwind_hint)) {
1272 WARN("struct unwind_hint size mismatch");
1278 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1279 hint = (struct unwind_hint *)sec->data->d_buf + i;
1281 rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
1283 WARN("can't find rela for unwind_hints[%d]", i);
1287 insn = find_insn(file, rela->sym->sec, rela->addend);
1289 WARN("can't find insn for unwind_hints[%d]", i);
1293 cfa = &insn->cfi.cfa;
1295 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1296 insn->ret_offset = hint->sp_offset;
1302 switch (hint->sp_reg) {
1303 case ORC_REG_UNDEFINED:
1304 cfa->base = CFI_UNDEFINED;
1312 case ORC_REG_SP_INDIRECT:
1313 cfa->base = CFI_SP_INDIRECT;
1316 cfa->base = CFI_R10;
1319 cfa->base = CFI_R13;
1328 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1329 insn->sec, insn->offset, hint->sp_reg);
1333 cfa->offset = hint->sp_offset;
1334 insn->cfi.type = hint->type;
1335 insn->cfi.end = hint->end;
1341 static int read_retpoline_hints(struct objtool_file *file)
1343 struct section *sec;
1344 struct instruction *insn;
1347 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1351 list_for_each_entry(rela, &sec->rela_list, list) {
1352 if (rela->sym->type != STT_SECTION) {
1353 WARN("unexpected relocation symbol type in %s", sec->name);
1357 insn = find_insn(file, rela->sym->sec, rela->addend);
1359 WARN("bad .discard.retpoline_safe entry");
1363 if (insn->type != INSN_JUMP_DYNAMIC &&
1364 insn->type != INSN_CALL_DYNAMIC) {
1365 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1366 insn->sec, insn->offset);
1370 insn->retpoline_safe = true;
1376 static int read_instr_hints(struct objtool_file *file)
1378 struct section *sec;
1379 struct instruction *insn;
1382 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1386 list_for_each_entry(rela, &sec->rela_list, list) {
1387 if (rela->sym->type != STT_SECTION) {
1388 WARN("unexpected relocation symbol type in %s", sec->name);
1392 insn = find_insn(file, rela->sym->sec, rela->addend);
1394 WARN("bad .discard.instr_end entry");
1401 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1405 list_for_each_entry(rela, &sec->rela_list, list) {
1406 if (rela->sym->type != STT_SECTION) {
1407 WARN("unexpected relocation symbol type in %s", sec->name);
1411 insn = find_insn(file, rela->sym->sec, rela->addend);
1413 WARN("bad .discard.instr_begin entry");
1423 static int read_intra_function_calls(struct objtool_file *file)
1425 struct instruction *insn;
1426 struct section *sec;
1429 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1433 list_for_each_entry(rela, &sec->rela_list, list) {
1434 unsigned long dest_off;
1436 if (rela->sym->type != STT_SECTION) {
1437 WARN("unexpected relocation symbol type in %s",
1442 insn = find_insn(file, rela->sym->sec, rela->addend);
1444 WARN("bad .discard.intra_function_call entry");
1448 if (insn->type != INSN_CALL) {
1449 WARN_FUNC("intra_function_call not a direct call",
1450 insn->sec, insn->offset);
1455 * Treat intra-function CALLs as JMPs, but with a stack_op.
1456 * See add_call_destinations(), which strips stack_ops from
1459 insn->type = INSN_JUMP_UNCONDITIONAL;
1461 dest_off = insn->offset + insn->len + insn->immediate;
1462 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1463 if (!insn->jump_dest) {
1464 WARN_FUNC("can't find call dest at %s+0x%lx",
1465 insn->sec, insn->offset,
1466 insn->sec->name, dest_off);
1474 static void mark_rodata(struct objtool_file *file)
1476 struct section *sec;
1480 * Search for the following rodata sections, each of which can
1481 * potentially contain jump tables:
1483 * - .rodata: can contain GCC switch tables
1484 * - .rodata.<func>: same, if -fdata-sections is being used
1485 * - .rodata..c_jump_table: contains C annotated jump tables
1487 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1489 for_each_sec(file, sec) {
1490 if (!strncmp(sec->name, ".rodata", 7) &&
1491 !strstr(sec->name, ".str1.")) {
1497 file->rodata = found;
1500 static int decode_sections(struct objtool_file *file)
1506 ret = decode_instructions(file);
1510 ret = add_dead_ends(file);
1515 add_uaccess_safe(file);
1517 ret = add_ignore_alternatives(file);
1521 ret = add_jump_destinations(file);
1525 ret = add_special_section_alts(file);
1529 ret = read_intra_function_calls(file);
1533 ret = add_call_destinations(file);
1537 ret = add_jump_table_alts(file);
1541 ret = read_unwind_hints(file);
1545 ret = read_retpoline_hints(file);
1549 ret = read_instr_hints(file);
1556 static bool is_fentry_call(struct instruction *insn)
1558 if (insn->type == INSN_CALL && insn->call_dest &&
1559 insn->call_dest->type == STT_NOTYPE &&
1560 !strcmp(insn->call_dest->name, "__fentry__"))
1566 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1568 u8 ret_offset = insn->ret_offset;
1569 struct cfi_state *cfi = &state->cfi;
1572 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1575 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
1578 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1582 * If there is a ret offset hint then don't check registers
1583 * because a callee-saved register might have been pushed on
1589 for (i = 0; i < CFI_NUM_REGS; i++) {
1590 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1591 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1598 static bool has_valid_stack_frame(struct insn_state *state)
1600 struct cfi_state *cfi = &state->cfi;
1602 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1603 cfi->regs[CFI_BP].offset == -16)
1606 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1612 static int update_cfi_state_regs(struct instruction *insn,
1613 struct cfi_state *cfi,
1614 struct stack_op *op)
1616 struct cfi_reg *cfa = &cfi->cfa;
1618 if (cfa->base != CFI_SP)
1622 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1626 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1629 /* add immediate to sp */
1630 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1631 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1632 cfa->offset -= op->src.offset;
1637 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1639 if (arch_callee_saved_reg(reg) &&
1640 cfi->regs[reg].base == CFI_UNDEFINED) {
1641 cfi->regs[reg].base = base;
1642 cfi->regs[reg].offset = offset;
1646 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1648 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1649 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1653 * A note about DRAP stack alignment:
1655 * GCC has the concept of a DRAP register, which is used to help keep track of
1656 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1657 * register. The typical DRAP pattern is:
1659 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1660 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1661 * 41 ff 72 f8 pushq -0x8(%r10)
1663 * 48 89 e5 mov %rsp,%rbp
1670 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1673 * There are some variations in the epilogues, like:
1681 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1686 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1687 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1688 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1689 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1691 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1694 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1695 * restored beforehand:
1698 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1699 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1701 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1705 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1706 struct stack_op *op)
1708 struct cfi_reg *cfa = &cfi->cfa;
1709 struct cfi_reg *regs = cfi->regs;
1711 /* stack operations don't make sense with an undefined CFA */
1712 if (cfa->base == CFI_UNDEFINED) {
1714 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1720 if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1721 return update_cfi_state_regs(insn, cfi, op);
1723 switch (op->dest.type) {
1726 switch (op->src.type) {
1729 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1730 cfa->base == CFI_SP &&
1731 regs[CFI_BP].base == CFI_CFA &&
1732 regs[CFI_BP].offset == -cfa->offset) {
1734 /* mov %rsp, %rbp */
1735 cfa->base = op->dest.reg;
1736 cfi->bp_scratch = false;
1739 else if (op->src.reg == CFI_SP &&
1740 op->dest.reg == CFI_BP && cfi->drap) {
1742 /* drap: mov %rsp, %rbp */
1743 regs[CFI_BP].base = CFI_BP;
1744 regs[CFI_BP].offset = -cfi->stack_size;
1745 cfi->bp_scratch = false;
1748 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1753 * This is needed for the rare case where GCC
1760 cfi->vals[op->dest.reg].base = CFI_CFA;
1761 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1764 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1765 cfa->base == CFI_BP) {
1770 * Restore the original stack pointer (Clang).
1772 cfi->stack_size = -cfi->regs[CFI_BP].offset;
1775 else if (op->dest.reg == cfa->base) {
1777 /* mov %reg, %rsp */
1778 if (cfa->base == CFI_SP &&
1779 cfi->vals[op->src.reg].base == CFI_CFA) {
1782 * This is needed for the rare case
1783 * where GCC does something dumb like:
1785 * lea 0x8(%rsp), %rcx
1789 cfa->offset = -cfi->vals[op->src.reg].offset;
1790 cfi->stack_size = cfa->offset;
1793 cfa->base = CFI_UNDEFINED;
1801 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1804 cfi->stack_size -= op->src.offset;
1805 if (cfa->base == CFI_SP)
1806 cfa->offset -= op->src.offset;
1810 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1812 /* lea disp(%rbp), %rsp */
1813 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1817 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1819 /* drap: lea disp(%rsp), %drap */
1820 cfi->drap_reg = op->dest.reg;
1823 * lea disp(%rsp), %reg
1825 * This is needed for the rare case where GCC
1826 * does something dumb like:
1828 * lea 0x8(%rsp), %rcx
1832 cfi->vals[op->dest.reg].base = CFI_CFA;
1833 cfi->vals[op->dest.reg].offset = \
1834 -cfi->stack_size + op->src.offset;
1839 if (cfi->drap && op->dest.reg == CFI_SP &&
1840 op->src.reg == cfi->drap_reg) {
1842 /* drap: lea disp(%drap), %rsp */
1844 cfa->offset = cfi->stack_size = -op->src.offset;
1845 cfi->drap_reg = CFI_UNDEFINED;
1850 if (op->dest.reg == cfi->cfa.base) {
1851 WARN_FUNC("unsupported stack register modification",
1852 insn->sec, insn->offset);
1859 if (op->dest.reg != CFI_SP ||
1860 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1861 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1862 WARN_FUNC("unsupported stack pointer realignment",
1863 insn->sec, insn->offset);
1867 if (cfi->drap_reg != CFI_UNDEFINED) {
1868 /* drap: and imm, %rsp */
1869 cfa->base = cfi->drap_reg;
1870 cfa->offset = cfi->stack_size = 0;
1875 * Older versions of GCC (4.8ish) realign the stack
1876 * without DRAP, with a frame pointer.
1883 if (!cfi->drap && op->dest.reg == cfa->base) {
1889 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
1890 op->dest.reg == cfi->drap_reg &&
1891 cfi->drap_offset == -cfi->stack_size) {
1893 /* drap: pop %drap */
1894 cfa->base = cfi->drap_reg;
1896 cfi->drap_offset = -1;
1898 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
1901 restore_reg(cfi, op->dest.reg);
1904 cfi->stack_size -= 8;
1905 if (cfa->base == CFI_SP)
1910 case OP_SRC_REG_INDIRECT:
1911 if (cfi->drap && op->src.reg == CFI_BP &&
1912 op->src.offset == cfi->drap_offset) {
1914 /* drap: mov disp(%rbp), %drap */
1915 cfa->base = cfi->drap_reg;
1917 cfi->drap_offset = -1;
1920 if (cfi->drap && op->src.reg == CFI_BP &&
1921 op->src.offset == regs[op->dest.reg].offset) {
1923 /* drap: mov disp(%rbp), %reg */
1924 restore_reg(cfi, op->dest.reg);
1926 } else if (op->src.reg == cfa->base &&
1927 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1929 /* mov disp(%rbp), %reg */
1930 /* mov disp(%rsp), %reg */
1931 restore_reg(cfi, op->dest.reg);
1937 WARN_FUNC("unknown stack-related instruction",
1938 insn->sec, insn->offset);
1946 cfi->stack_size += 8;
1947 if (cfa->base == CFI_SP)
1950 if (op->src.type != OP_SRC_REG)
1954 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
1956 /* drap: push %drap */
1957 cfa->base = CFI_BP_INDIRECT;
1958 cfa->offset = -cfi->stack_size;
1960 /* save drap so we know when to restore it */
1961 cfi->drap_offset = -cfi->stack_size;
1963 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
1965 /* drap: push %rbp */
1966 cfi->stack_size = 0;
1968 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1970 /* drap: push %reg */
1971 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
1977 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
1980 /* detect when asm code uses rbp as a scratch register */
1981 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1982 cfa->base != CFI_BP)
1983 cfi->bp_scratch = true;
1986 case OP_DEST_REG_INDIRECT:
1989 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
1991 /* drap: mov %drap, disp(%rbp) */
1992 cfa->base = CFI_BP_INDIRECT;
1993 cfa->offset = op->dest.offset;
1995 /* save drap offset so we know when to restore it */
1996 cfi->drap_offset = op->dest.offset;
1999 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
2001 /* drap: mov reg, disp(%rbp) */
2002 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2005 } else if (op->dest.reg == cfa->base) {
2007 /* mov reg, disp(%rbp) */
2008 /* mov reg, disp(%rsp) */
2009 save_reg(cfi, op->src.reg, CFI_CFA,
2010 op->dest.offset - cfi->cfa.offset);
2016 if ((!cfi->drap && cfa->base != CFI_BP) ||
2017 (cfi->drap && cfa->base != cfi->drap_reg)) {
2018 WARN_FUNC("leave instruction with modified stack frame",
2019 insn->sec, insn->offset);
2023 /* leave (mov %rbp, %rsp; pop %rbp) */
2025 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2026 restore_reg(cfi, CFI_BP);
2036 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2037 WARN_FUNC("unknown stack-related memory operation",
2038 insn->sec, insn->offset);
2043 cfi->stack_size -= 8;
2044 if (cfa->base == CFI_SP)
2050 WARN_FUNC("unknown stack-related instruction",
2051 insn->sec, insn->offset);
2058 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2060 struct stack_op *op;
2062 list_for_each_entry(op, &insn->stack_ops, list) {
2063 struct cfi_state old_cfi = state->cfi;
2066 res = update_cfi_state(insn, &state->cfi, op);
2070 if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_state))) {
2071 WARN_FUNC("alternative modifies stack", insn->sec, insn->offset);
2075 if (op->dest.type == OP_DEST_PUSHF) {
2076 if (!state->uaccess_stack) {
2077 state->uaccess_stack = 1;
2078 } else if (state->uaccess_stack >> 31) {
2079 WARN_FUNC("PUSHF stack exhausted",
2080 insn->sec, insn->offset);
2083 state->uaccess_stack <<= 1;
2084 state->uaccess_stack |= state->uaccess;
2087 if (op->src.type == OP_SRC_POPF) {
2088 if (state->uaccess_stack) {
2089 state->uaccess = state->uaccess_stack & 1;
2090 state->uaccess_stack >>= 1;
2091 if (state->uaccess_stack == 1)
2092 state->uaccess_stack = 0;
2100 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2102 struct cfi_state *cfi1 = &insn->cfi;
2105 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2107 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2108 insn->sec, insn->offset,
2109 cfi1->cfa.base, cfi1->cfa.offset,
2110 cfi2->cfa.base, cfi2->cfa.offset);
2112 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2113 for (i = 0; i < CFI_NUM_REGS; i++) {
2114 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2115 sizeof(struct cfi_reg)))
2118 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2119 insn->sec, insn->offset,
2120 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2121 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2125 } else if (cfi1->type != cfi2->type) {
2127 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2128 insn->sec, insn->offset, cfi1->type, cfi2->type);
2130 } else if (cfi1->drap != cfi2->drap ||
2131 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2132 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2134 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2135 insn->sec, insn->offset,
2136 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2137 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2145 static inline bool func_uaccess_safe(struct symbol *func)
2148 return func->uaccess_safe;
2153 static inline const char *call_dest_name(struct instruction *insn)
2155 if (insn->call_dest)
2156 return insn->call_dest->name;
2161 static int validate_call(struct instruction *insn, struct insn_state *state)
2163 if (state->noinstr && state->instr <= 0 &&
2164 (!insn->call_dest || !insn->call_dest->sec->noinstr)) {
2165 WARN_FUNC("call to %s() leaves .noinstr.text section",
2166 insn->sec, insn->offset, call_dest_name(insn));
2170 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2171 WARN_FUNC("call to %s() with UACCESS enabled",
2172 insn->sec, insn->offset, call_dest_name(insn));
2177 WARN_FUNC("call to %s() with DF set",
2178 insn->sec, insn->offset, call_dest_name(insn));
2185 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2187 if (has_modified_stack_frame(insn, state)) {
2188 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2189 insn->sec, insn->offset);
2193 return validate_call(insn, state);
2196 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2198 if (state->noinstr && state->instr > 0) {
2199 WARN_FUNC("return with instrumentation enabled",
2200 insn->sec, insn->offset);
2204 if (state->uaccess && !func_uaccess_safe(func)) {
2205 WARN_FUNC("return with UACCESS enabled",
2206 insn->sec, insn->offset);
2210 if (!state->uaccess && func_uaccess_safe(func)) {
2211 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2212 insn->sec, insn->offset);
2217 WARN_FUNC("return with DF set",
2218 insn->sec, insn->offset);
2222 if (func && has_modified_stack_frame(insn, state)) {
2223 WARN_FUNC("return with modified stack frame",
2224 insn->sec, insn->offset);
2228 if (state->cfi.bp_scratch) {
2229 WARN_FUNC("BP used as a scratch register",
2230 insn->sec, insn->offset);
2238 * Alternatives should not contain any ORC entries, this in turn means they
2239 * should not contain any CFI ops, which implies all instructions should have
2240 * the same same CFI state.
2242 * It is possible to constuct alternatives that have unreachable holes that go
2243 * unreported (because they're NOPs), such holes would result in CFI_UNDEFINED
2244 * states which then results in ORC entries, which we just said we didn't want.
2246 * Avoid them by copying the CFI entry of the first instruction into the whole
2249 static void fill_alternative_cfi(struct objtool_file *file, struct instruction *insn)
2251 struct instruction *first_insn = insn;
2252 int alt_group = insn->alt_group;
2254 sec_for_each_insn_continue(file, insn) {
2255 if (insn->alt_group != alt_group)
2257 insn->cfi = first_insn->cfi;
2262 * Follow the branch starting at the given instruction, and recursively follow
2263 * any other branches (jumps). Meanwhile, track the frame pointer state at
2264 * each instruction and validate all the rules described in
2265 * tools/objtool/Documentation/stack-validation.txt.
2267 static int validate_branch(struct objtool_file *file, struct symbol *func,
2268 struct instruction *insn, struct insn_state state)
2270 struct alternative *alt;
2271 struct instruction *next_insn;
2272 struct section *sec;
2279 next_insn = next_insn_same_sec(file, insn);
2281 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2282 WARN("%s() falls through to next function %s()",
2283 func->name, insn->func->name);
2287 if (func && insn->ignore) {
2288 WARN_FUNC("BUG: why am I validating an ignored function?",
2293 visited = 1 << state.uaccess;
2294 if (insn->visited) {
2295 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2298 if (insn->visited & visited)
2303 state.instr += insn->instr;
2306 state.cfi = insn->cfi;
2308 insn->cfi = state.cfi;
2310 insn->visited |= visited;
2312 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2313 bool skip_orig = false;
2315 list_for_each_entry(alt, &insn->alts, list) {
2319 ret = validate_branch(file, func, alt->insn, state);
2322 BT_FUNC("(alt)", insn);
2327 if (insn->alt_group)
2328 fill_alternative_cfi(file, insn);
2334 if (handle_insn_ops(insn, &state))
2337 switch (insn->type) {
2340 return validate_return(func, insn, &state);
2343 case INSN_CALL_DYNAMIC:
2344 ret = validate_call(insn, &state);
2348 if (!no_fp && func && !is_fentry_call(insn) &&
2349 !has_valid_stack_frame(&state)) {
2350 WARN_FUNC("call without frame pointer save/setup",
2355 if (dead_end_function(file, insn->call_dest))
2360 case INSN_JUMP_CONDITIONAL:
2361 case INSN_JUMP_UNCONDITIONAL:
2362 if (func && is_sibling_call(insn)) {
2363 ret = validate_sibling_call(insn, &state);
2367 } else if (insn->jump_dest) {
2368 ret = validate_branch(file, func,
2369 insn->jump_dest, state);
2372 BT_FUNC("(branch)", insn);
2377 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2382 case INSN_JUMP_DYNAMIC:
2383 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2384 if (func && is_sibling_call(insn)) {
2385 ret = validate_sibling_call(insn, &state);
2390 if (insn->type == INSN_JUMP_DYNAMIC)
2395 case INSN_CONTEXT_SWITCH:
2396 if (func && (!next_insn || !next_insn->hint)) {
2397 WARN_FUNC("unsupported instruction in callable function",
2404 if (state.uaccess) {
2405 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2409 state.uaccess = true;
2413 if (!state.uaccess && func) {
2414 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2418 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2419 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2423 state.uaccess = false;
2428 WARN_FUNC("recursive STD", sec, insn->offset);
2434 if (!state.df && func)
2435 WARN_FUNC("redundant CLD", sec, insn->offset);
2448 if (state.cfi.cfa.base == CFI_UNDEFINED)
2450 WARN("%s: unexpected end of section", sec->name);
2460 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2462 struct instruction *insn;
2463 struct insn_state state;
2464 int ret, warnings = 0;
2469 init_insn_state(&state, sec);
2472 insn = find_insn(file, sec, 0);
2476 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2479 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2480 if (insn->hint && !insn->visited) {
2481 ret = validate_branch(file, insn->func, insn, state);
2482 if (ret && backtrace)
2483 BT_FUNC("<=== (hint)", insn);
2487 insn = list_next_entry(insn, list);
2493 static int validate_retpoline(struct objtool_file *file)
2495 struct instruction *insn;
2498 for_each_insn(file, insn) {
2499 if (insn->type != INSN_JUMP_DYNAMIC &&
2500 insn->type != INSN_CALL_DYNAMIC)
2503 if (insn->retpoline_safe)
2507 * .init.text code is ran before userspace and thus doesn't
2508 * strictly need retpolines, except for modules which are
2509 * loaded late, they very much do need retpoline in their
2512 if (!strcmp(insn->sec->name, ".init.text") && !module)
2515 WARN_FUNC("indirect %s found in RETPOLINE build",
2516 insn->sec, insn->offset,
2517 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2525 static bool is_kasan_insn(struct instruction *insn)
2527 return (insn->type == INSN_CALL &&
2528 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2531 static bool is_ubsan_insn(struct instruction *insn)
2533 return (insn->type == INSN_CALL &&
2534 !strcmp(insn->call_dest->name,
2535 "__ubsan_handle_builtin_unreachable"));
2538 static bool ignore_unreachable_insn(struct instruction *insn)
2542 if (insn->ignore || insn->type == INSN_NOP)
2546 * Ignore any unused exceptions. This can happen when a whitelisted
2547 * function has an exception table entry.
2549 * Also ignore alternative replacement instructions. This can happen
2550 * when a whitelisted function uses one of the ALTERNATIVE macros.
2552 if (!strcmp(insn->sec->name, ".fixup") ||
2553 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2554 !strcmp(insn->sec->name, ".altinstr_aux"))
2561 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2562 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2563 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2564 * (or occasionally a JMP to UD2).
2566 if (list_prev_entry(insn, list)->dead_end &&
2567 (insn->type == INSN_BUG ||
2568 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2569 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2573 * Check if this (or a subsequent) instruction is related to
2574 * CONFIG_UBSAN or CONFIG_KASAN.
2576 * End the search at 5 instructions to avoid going into the weeds.
2578 for (i = 0; i < 5; i++) {
2580 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2583 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2584 if (insn->jump_dest &&
2585 insn->jump_dest->func == insn->func) {
2586 insn = insn->jump_dest;
2593 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2596 insn = list_next_entry(insn, list);
2602 static int validate_symbol(struct objtool_file *file, struct section *sec,
2603 struct symbol *sym, struct insn_state *state)
2605 struct instruction *insn;
2609 WARN("%s() is missing an ELF size annotation", sym->name);
2613 if (sym->pfunc != sym || sym->alias != sym)
2616 insn = find_insn(file, sec, sym->offset);
2617 if (!insn || insn->ignore || insn->visited)
2620 state->uaccess = sym->uaccess_safe;
2622 ret = validate_branch(file, insn->func, insn, *state);
2623 if (ret && backtrace)
2624 BT_FUNC("<=== (sym)", insn);
2628 static int validate_section(struct objtool_file *file, struct section *sec)
2630 struct insn_state state;
2631 struct symbol *func;
2634 list_for_each_entry(func, &sec->symbol_list, list) {
2635 if (func->type != STT_FUNC)
2638 init_insn_state(&state, sec);
2639 state.cfi.cfa = initial_func_cfi.cfa;
2640 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
2641 CFI_NUM_REGS * sizeof(struct cfi_reg));
2642 state.cfi.stack_size = initial_func_cfi.cfa.offset;
2644 warnings += validate_symbol(file, sec, func, &state);
2650 static int validate_vmlinux_functions(struct objtool_file *file)
2652 struct section *sec;
2655 sec = find_section_by_name(file->elf, ".noinstr.text");
2657 warnings += validate_section(file, sec);
2658 warnings += validate_unwind_hints(file, sec);
2661 sec = find_section_by_name(file->elf, ".entry.text");
2663 warnings += validate_section(file, sec);
2664 warnings += validate_unwind_hints(file, sec);
2670 static int validate_functions(struct objtool_file *file)
2672 struct section *sec;
2675 for_each_sec(file, sec) {
2676 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2679 warnings += validate_section(file, sec);
2685 static int validate_reachable_instructions(struct objtool_file *file)
2687 struct instruction *insn;
2689 if (file->ignore_unreachables)
2692 for_each_insn(file, insn) {
2693 if (insn->visited || ignore_unreachable_insn(insn))
2696 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2703 static struct objtool_file file;
2705 int check(const char *_objname, bool orc)
2707 int ret, warnings = 0;
2711 file.elf = elf_open_read(objname, orc ? O_RDWR : O_RDONLY);
2715 INIT_LIST_HEAD(&file.insn_list);
2716 hash_init(file.insn_hash);
2717 file.c_file = find_section_by_name(file.elf, ".comment");
2718 file.ignore_unreachables = no_unreachable;
2721 arch_initial_func_cfi_state(&initial_func_cfi);
2723 ret = decode_sections(&file);
2728 if (list_empty(&file.insn_list))
2731 if (vmlinux && !validate_dup) {
2732 ret = validate_vmlinux_functions(&file);
2741 ret = validate_retpoline(&file);
2747 ret = validate_functions(&file);
2752 ret = validate_unwind_hints(&file, NULL);
2758 ret = validate_reachable_instructions(&file);
2765 ret = create_orc(&file);
2769 ret = create_orc_sections(&file);
2773 ret = elf_write(file.elf);
2781 * Fatal error. The binary is corrupt or otherwise broken in
2782 * some way, or objtool itself is broken. Fail the kernel