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"))
272 for (offset = 0; offset < sec->len; offset += insn->len) {
273 insn = malloc(sizeof(*insn));
275 WARN("malloc failed");
278 memset(insn, 0, sizeof(*insn));
279 INIT_LIST_HEAD(&insn->alts);
280 INIT_LIST_HEAD(&insn->stack_ops);
281 init_cfi_state(&insn->cfi);
284 insn->offset = offset;
286 ret = arch_decode_instruction(file->elf, sec, offset,
288 &insn->len, &insn->type,
294 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
295 list_add_tail(&insn->list, &file->insn_list);
299 list_for_each_entry(func, &sec->symbol_list, list) {
300 if (func->type != STT_FUNC || func->alias != func)
303 if (!find_insn(file, sec, func->offset)) {
304 WARN("%s(): can't find starting instruction",
309 sym_for_each_insn(file, func, insn)
315 printf("nr_insns: %lu\n", nr_insns);
325 * Mark "ud2" instructions and manually annotated dead ends.
327 static int add_dead_ends(struct objtool_file *file)
331 struct instruction *insn;
335 * By default, "ud2" is a dead end unless otherwise annotated, because
336 * GCC 7 inserts it for certain divide-by-zero cases.
338 for_each_insn(file, insn)
339 if (insn->type == INSN_BUG)
340 insn->dead_end = true;
343 * Check for manually annotated dead ends.
345 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
349 list_for_each_entry(rela, &sec->rela_list, list) {
350 if (rela->sym->type != STT_SECTION) {
351 WARN("unexpected relocation symbol type in %s", sec->name);
354 insn = find_insn(file, rela->sym->sec, rela->addend);
356 insn = list_prev_entry(insn, list);
357 else if (rela->addend == rela->sym->sec->len) {
359 list_for_each_entry_reverse(insn, &file->insn_list, list) {
360 if (insn->sec == rela->sym->sec) {
367 WARN("can't find unreachable insn at %s+0x%x",
368 rela->sym->sec->name, rela->addend);
372 WARN("can't find unreachable insn at %s+0x%x",
373 rela->sym->sec->name, rela->addend);
377 insn->dead_end = true;
382 * These manually annotated reachable checks are needed for GCC 4.4,
383 * where the Linux unreachable() macro isn't supported. In that case
384 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
387 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
391 list_for_each_entry(rela, &sec->rela_list, list) {
392 if (rela->sym->type != STT_SECTION) {
393 WARN("unexpected relocation symbol type in %s", sec->name);
396 insn = find_insn(file, rela->sym->sec, rela->addend);
398 insn = list_prev_entry(insn, list);
399 else if (rela->addend == rela->sym->sec->len) {
401 list_for_each_entry_reverse(insn, &file->insn_list, list) {
402 if (insn->sec == rela->sym->sec) {
409 WARN("can't find reachable insn at %s+0x%x",
410 rela->sym->sec->name, rela->addend);
414 WARN("can't find reachable insn at %s+0x%x",
415 rela->sym->sec->name, rela->addend);
419 insn->dead_end = false;
426 * Warnings shouldn't be reported for ignored functions.
428 static void add_ignores(struct objtool_file *file)
430 struct instruction *insn;
435 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
439 list_for_each_entry(rela, &sec->rela_list, list) {
440 switch (rela->sym->type) {
446 func = find_func_by_offset(rela->sym->sec, rela->addend);
452 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
456 func_for_each_insn(file, func, insn)
462 * This is a whitelist of functions that is allowed to be called with AC set.
463 * The list is meant to be minimal and only contains compiler instrumentation
464 * ABI and a few functions used to implement *_{to,from}_user() functions.
466 * These functions must not directly change AC, but may PUSHF/POPF.
468 static const char *uaccess_safe_builtin[] = {
471 "check_memory_region",
472 /* KASAN out-of-line */
473 "__asan_loadN_noabort",
474 "__asan_load1_noabort",
475 "__asan_load2_noabort",
476 "__asan_load4_noabort",
477 "__asan_load8_noabort",
478 "__asan_load16_noabort",
479 "__asan_storeN_noabort",
480 "__asan_store1_noabort",
481 "__asan_store2_noabort",
482 "__asan_store4_noabort",
483 "__asan_store8_noabort",
484 "__asan_store16_noabort",
486 "__asan_report_load_n_noabort",
487 "__asan_report_load1_noabort",
488 "__asan_report_load2_noabort",
489 "__asan_report_load4_noabort",
490 "__asan_report_load8_noabort",
491 "__asan_report_load16_noabort",
492 "__asan_report_store_n_noabort",
493 "__asan_report_store1_noabort",
494 "__asan_report_store2_noabort",
495 "__asan_report_store4_noabort",
496 "__asan_report_store8_noabort",
497 "__asan_report_store16_noabort",
500 "__sanitizer_cov_trace_pc",
501 "__sanitizer_cov_trace_const_cmp1",
502 "__sanitizer_cov_trace_const_cmp2",
503 "__sanitizer_cov_trace_const_cmp4",
504 "__sanitizer_cov_trace_const_cmp8",
505 "__sanitizer_cov_trace_cmp1",
506 "__sanitizer_cov_trace_cmp2",
507 "__sanitizer_cov_trace_cmp4",
508 "__sanitizer_cov_trace_cmp8",
509 "__sanitizer_cov_trace_switch",
511 "ubsan_type_mismatch_common",
512 "__ubsan_handle_type_mismatch",
513 "__ubsan_handle_type_mismatch_v1",
514 "__ubsan_handle_shift_out_of_bounds",
516 "csum_partial_copy_generic",
518 "mcsafe_handle_tail",
519 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
523 static void add_uaccess_safe(struct objtool_file *file)
531 for (name = uaccess_safe_builtin; *name; name++) {
532 func = find_symbol_by_name(file->elf, *name);
536 func->uaccess_safe = true;
541 * FIXME: For now, just ignore any alternatives which add retpolines. This is
542 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
543 * But it at least allows objtool to understand the control flow *around* the
546 static int add_ignore_alternatives(struct objtool_file *file)
550 struct instruction *insn;
552 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
556 list_for_each_entry(rela, &sec->rela_list, list) {
557 if (rela->sym->type != STT_SECTION) {
558 WARN("unexpected relocation symbol type in %s", sec->name);
562 insn = find_insn(file, rela->sym->sec, rela->addend);
564 WARN("bad .discard.ignore_alts entry");
568 insn->ignore_alts = true;
575 * Find the destination instructions for all jumps.
577 static int add_jump_destinations(struct objtool_file *file)
579 struct instruction *insn;
581 struct section *dest_sec;
582 unsigned long dest_off;
584 for_each_insn(file, insn) {
585 if (!is_static_jump(insn))
588 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
591 rela = find_rela_by_dest_range(file->elf, insn->sec,
592 insn->offset, insn->len);
594 dest_sec = insn->sec;
595 dest_off = arch_jump_destination(insn);
596 } else if (rela->sym->type == STT_SECTION) {
597 dest_sec = rela->sym->sec;
598 dest_off = arch_dest_rela_offset(rela->addend);
599 } else if (rela->sym->sec->idx) {
600 dest_sec = rela->sym->sec;
601 dest_off = rela->sym->sym.st_value +
602 arch_dest_rela_offset(rela->addend);
603 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
605 * Retpoline jumps are really dynamic jumps in
606 * disguise, so convert them accordingly.
608 if (insn->type == INSN_JUMP_UNCONDITIONAL)
609 insn->type = INSN_JUMP_DYNAMIC;
611 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
613 insn->retpoline_safe = true;
616 /* external sibling call */
617 insn->call_dest = rela->sym;
621 insn->jump_dest = find_insn(file, dest_sec, dest_off);
622 if (!insn->jump_dest) {
625 * This is a special case where an alt instruction
626 * jumps past the end of the section. These are
627 * handled later in handle_group_alt().
629 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
632 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
633 insn->sec, insn->offset, dest_sec->name,
639 * Cross-function jump.
641 if (insn->func && insn->jump_dest->func &&
642 insn->func != insn->jump_dest->func) {
645 * For GCC 8+, create parent/child links for any cold
646 * subfunctions. This is _mostly_ redundant with a
647 * similar initialization in read_symbols().
649 * If a function has aliases, we want the *first* such
650 * function in the symbol table to be the subfunction's
651 * parent. In that case we overwrite the
652 * initialization done in read_symbols().
654 * However this code can't completely replace the
655 * read_symbols() code because this doesn't detect the
656 * case where the parent function's only reference to a
657 * subfunction is through a jump table.
659 if (!strstr(insn->func->name, ".cold.") &&
660 strstr(insn->jump_dest->func->name, ".cold.")) {
661 insn->func->cfunc = insn->jump_dest->func;
662 insn->jump_dest->func->pfunc = insn->func;
664 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
665 insn->jump_dest->offset == insn->jump_dest->func->offset) {
667 /* internal sibling call */
668 insn->call_dest = insn->jump_dest->func;
677 * Find the destination instructions for all calls.
679 static int add_call_destinations(struct objtool_file *file)
681 struct instruction *insn;
682 unsigned long dest_off;
685 for_each_insn(file, insn) {
686 if (insn->type != INSN_CALL)
689 rela = find_rela_by_dest_range(file->elf, insn->sec,
690 insn->offset, insn->len);
692 dest_off = arch_jump_destination(insn);
693 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
694 if (!insn->call_dest)
695 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
700 if (!insn->call_dest) {
701 WARN_FUNC("unsupported intra-function call",
702 insn->sec, insn->offset);
704 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
708 if (insn->func && insn->call_dest->type != STT_FUNC) {
709 WARN_FUNC("unsupported call to non-function",
710 insn->sec, insn->offset);
714 } else if (rela->sym->type == STT_SECTION) {
715 dest_off = arch_dest_rela_offset(rela->addend);
716 insn->call_dest = find_func_by_offset(rela->sym->sec,
718 if (!insn->call_dest) {
719 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
720 insn->sec, insn->offset,
721 rela->sym->sec->name,
726 insn->call_dest = rela->sym;
733 * The .alternatives section requires some extra special care, over and above
734 * what other special sections require:
736 * 1. Because alternatives are patched in-place, we need to insert a fake jump
737 * instruction at the end so that validate_branch() skips all the original
738 * replaced instructions when validating the new instruction path.
740 * 2. An added wrinkle is that the new instruction length might be zero. In
741 * that case the old instructions are replaced with noops. We simulate that
742 * by creating a fake jump as the only new instruction.
744 * 3. In some cases, the alternative section includes an instruction which
745 * conditionally jumps to the _end_ of the entry. We have to modify these
746 * jumps' destinations to point back to .text rather than the end of the
747 * entry in .altinstr_replacement.
749 static int handle_group_alt(struct objtool_file *file,
750 struct special_alt *special_alt,
751 struct instruction *orig_insn,
752 struct instruction **new_insn)
754 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
755 unsigned long dest_off;
757 last_orig_insn = NULL;
759 sec_for_each_insn_from(file, insn) {
760 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
763 insn->alt_group = true;
764 last_orig_insn = insn;
767 if (next_insn_same_sec(file, last_orig_insn)) {
768 fake_jump = malloc(sizeof(*fake_jump));
770 WARN("malloc failed");
773 memset(fake_jump, 0, sizeof(*fake_jump));
774 INIT_LIST_HEAD(&fake_jump->alts);
775 INIT_LIST_HEAD(&fake_jump->stack_ops);
776 init_cfi_state(&fake_jump->cfi);
778 fake_jump->sec = special_alt->new_sec;
779 fake_jump->offset = FAKE_JUMP_OFFSET;
780 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
781 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
782 fake_jump->func = orig_insn->func;
785 if (!special_alt->new_len) {
787 WARN("%s: empty alternative at end of section",
788 special_alt->orig_sec->name);
792 *new_insn = fake_jump;
796 last_new_insn = NULL;
798 sec_for_each_insn_from(file, insn) {
799 if (insn->offset >= special_alt->new_off + special_alt->new_len)
802 last_new_insn = insn;
804 insn->ignore = orig_insn->ignore_alts;
805 insn->func = orig_insn->func;
808 * Since alternative replacement code is copy/pasted by the
809 * kernel after applying relocations, generally such code can't
810 * have relative-address relocation references to outside the
811 * .altinstr_replacement section, unless the arch's
812 * alternatives code can adjust the relative offsets
815 * The x86 alternatives code adjusts the offsets only when it
816 * encounters a branch instruction at the very beginning of the
819 if ((insn->offset != special_alt->new_off ||
820 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
821 find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
823 WARN_FUNC("unsupported relocation in alternatives section",
824 insn->sec, insn->offset);
828 if (!is_static_jump(insn))
831 if (!insn->immediate)
834 dest_off = arch_jump_destination(insn);
835 if (dest_off == special_alt->new_off + special_alt->new_len) {
837 WARN("%s: alternative jump to end of section",
838 special_alt->orig_sec->name);
841 insn->jump_dest = fake_jump;
844 if (!insn->jump_dest) {
845 WARN_FUNC("can't find alternative jump destination",
846 insn->sec, insn->offset);
851 if (!last_new_insn) {
852 WARN_FUNC("can't find last new alternative instruction",
853 special_alt->new_sec, special_alt->new_off);
858 list_add(&fake_jump->list, &last_new_insn->list);
864 * A jump table entry can either convert a nop to a jump or a jump to a nop.
865 * If the original instruction is a jump, make the alt entry an effective nop
866 * by just skipping the original instruction.
868 static int handle_jump_alt(struct objtool_file *file,
869 struct special_alt *special_alt,
870 struct instruction *orig_insn,
871 struct instruction **new_insn)
873 if (orig_insn->type == INSN_NOP)
876 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
877 WARN_FUNC("unsupported instruction at jump label",
878 orig_insn->sec, orig_insn->offset);
882 *new_insn = list_next_entry(orig_insn, list);
887 * Read all the special sections which have alternate instructions which can be
888 * patched in or redirected to at runtime. Each instruction having alternate
889 * instruction(s) has them added to its insn->alts list, which will be
890 * traversed in validate_branch().
892 static int add_special_section_alts(struct objtool_file *file)
894 struct list_head special_alts;
895 struct instruction *orig_insn, *new_insn;
896 struct special_alt *special_alt, *tmp;
897 struct alternative *alt;
900 ret = special_get_alts(file->elf, &special_alts);
904 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
906 orig_insn = find_insn(file, special_alt->orig_sec,
907 special_alt->orig_off);
909 WARN_FUNC("special: can't find orig instruction",
910 special_alt->orig_sec, special_alt->orig_off);
916 if (!special_alt->group || special_alt->new_len) {
917 new_insn = find_insn(file, special_alt->new_sec,
918 special_alt->new_off);
920 WARN_FUNC("special: can't find new instruction",
921 special_alt->new_sec,
922 special_alt->new_off);
928 if (special_alt->group) {
929 if (!special_alt->orig_len) {
930 WARN_FUNC("empty alternative entry",
931 orig_insn->sec, orig_insn->offset);
935 ret = handle_group_alt(file, special_alt, orig_insn,
939 } else if (special_alt->jump_or_nop) {
940 ret = handle_jump_alt(file, special_alt, orig_insn,
946 alt = malloc(sizeof(*alt));
948 WARN("malloc failed");
953 alt->insn = new_insn;
954 alt->skip_orig = special_alt->skip_orig;
955 orig_insn->ignore_alts |= special_alt->skip_alt;
956 list_add_tail(&alt->list, &orig_insn->alts);
958 list_del(&special_alt->list);
966 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
969 struct rela *rela = table;
970 struct instruction *dest_insn;
971 struct alternative *alt;
972 struct symbol *pfunc = insn->func->pfunc;
973 unsigned int prev_offset = 0;
976 * Each @rela is a switch table relocation which points to the target
979 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
981 /* Check for the end of the table: */
982 if (rela != table && rela->jump_table_start)
985 /* Make sure the table entries are consecutive: */
986 if (prev_offset && rela->offset != prev_offset + 8)
989 /* Detect function pointers from contiguous objects: */
990 if (rela->sym->sec == pfunc->sec &&
991 rela->addend == pfunc->offset)
994 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
998 /* Make sure the destination is in the same function: */
999 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1002 alt = malloc(sizeof(*alt));
1004 WARN("malloc failed");
1008 alt->insn = dest_insn;
1009 list_add_tail(&alt->list, &insn->alts);
1010 prev_offset = rela->offset;
1014 WARN_FUNC("can't find switch jump table",
1015 insn->sec, insn->offset);
1023 * find_jump_table() - Given a dynamic jump, find the switch jump table in
1024 * .rodata associated with it.
1026 * There are 3 basic patterns:
1028 * 1. jmpq *[rodata addr](,%reg,8)
1030 * This is the most common case by far. It jumps to an address in a simple
1031 * jump table which is stored in .rodata.
1033 * 2. jmpq *[rodata addr](%rip)
1035 * This is caused by a rare GCC quirk, currently only seen in three driver
1036 * functions in the kernel, only with certain obscure non-distro configs.
1038 * As part of an optimization, GCC makes a copy of an existing switch jump
1039 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1040 * jump) to use a single entry in the table. The rest of the jump table and
1041 * some of its jump targets remain as dead code.
1043 * In such a case we can just crudely ignore all unreachable instruction
1044 * warnings for the entire object file. Ideally we would just ignore them
1045 * for the function, but that would require redesigning the code quite a
1046 * bit. And honestly that's just not worth doing: unreachable instruction
1047 * warnings are of questionable value anyway, and this is such a rare issue.
1049 * 3. mov [rodata addr],%reg1
1050 * ... some instructions ...
1051 * jmpq *(%reg1,%reg2,8)
1053 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1054 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1056 * As of GCC 7 there are quite a few more of these and the 'in between' code
1057 * is significant. Esp. with KASAN enabled some of the code between the mov
1058 * and jmpq uses .rodata itself, which can confuse things.
1060 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1061 * ensure the same register is used in the mov and jump instructions.
1063 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1065 static struct rela *find_jump_table(struct objtool_file *file,
1066 struct symbol *func,
1067 struct instruction *insn)
1069 struct rela *text_rela, *table_rela;
1070 struct instruction *dest_insn, *orig_insn = insn;
1071 struct section *table_sec;
1072 unsigned long table_offset;
1075 * Backward search using the @first_jump_src links, these help avoid
1076 * much of the 'in between' code. Which avoids us getting confused by
1080 &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func;
1081 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1083 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1086 /* allow small jumps within the range */
1087 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1089 (insn->jump_dest->offset <= insn->offset ||
1090 insn->jump_dest->offset > orig_insn->offset))
1093 /* look for a relocation which references .rodata */
1094 text_rela = find_rela_by_dest_range(file->elf, insn->sec,
1095 insn->offset, insn->len);
1096 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1097 !text_rela->sym->sec->rodata)
1100 table_offset = text_rela->addend;
1101 table_sec = text_rela->sym->sec;
1103 if (text_rela->type == R_X86_64_PC32)
1107 * Make sure the .rodata address isn't associated with a
1108 * symbol. GCC jump tables are anonymous data.
1110 * Also support C jump tables which are in the same format as
1111 * switch jump tables. For objtool to recognize them, they
1112 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1113 * have symbols associated with them.
1115 if (find_symbol_containing(table_sec, table_offset) &&
1116 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1120 * Each table entry has a rela associated with it. The rela
1121 * should reference text in the same function as the original
1124 table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
1127 dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
1128 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1132 * Use of RIP-relative switch jumps is quite rare, and
1133 * indicates a rare GCC quirk/bug which can leave dead code
1136 if (text_rela->type == R_X86_64_PC32)
1137 file->ignore_unreachables = true;
1146 * First pass: Mark the head of each jump table so that in the next pass,
1147 * we know when a given jump table ends and the next one starts.
1149 static void mark_func_jump_tables(struct objtool_file *file,
1150 struct symbol *func)
1152 struct instruction *insn, *last = NULL;
1155 func_for_each_insn(file, func, insn) {
1160 * Store back-pointers for unconditional forward jumps such
1161 * that find_jump_table() can back-track using those and
1162 * avoid some potentially confusing code.
1164 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1165 insn->offset > last->offset &&
1166 insn->jump_dest->offset > insn->offset &&
1167 !insn->jump_dest->first_jump_src) {
1169 insn->jump_dest->first_jump_src = insn;
1170 last = insn->jump_dest;
1173 if (insn->type != INSN_JUMP_DYNAMIC)
1176 rela = find_jump_table(file, func, insn);
1178 rela->jump_table_start = true;
1179 insn->jump_table = rela;
1184 static int add_func_jump_tables(struct objtool_file *file,
1185 struct symbol *func)
1187 struct instruction *insn;
1190 func_for_each_insn(file, func, insn) {
1191 if (!insn->jump_table)
1194 ret = add_jump_table(file, insn, insn->jump_table);
1203 * For some switch statements, gcc generates a jump table in the .rodata
1204 * section which contains a list of addresses within the function to jump to.
1205 * This finds these jump tables and adds them to the insn->alts lists.
1207 static int add_jump_table_alts(struct objtool_file *file)
1209 struct section *sec;
1210 struct symbol *func;
1216 for_each_sec(file, sec) {
1217 list_for_each_entry(func, &sec->symbol_list, list) {
1218 if (func->type != STT_FUNC)
1221 mark_func_jump_tables(file, func);
1222 ret = add_func_jump_tables(file, func);
1231 static int read_unwind_hints(struct objtool_file *file)
1233 struct section *sec, *relasec;
1235 struct unwind_hint *hint;
1236 struct instruction *insn;
1237 struct cfi_reg *cfa;
1240 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1244 relasec = sec->rela;
1246 WARN("missing .rela.discard.unwind_hints section");
1250 if (sec->len % sizeof(struct unwind_hint)) {
1251 WARN("struct unwind_hint size mismatch");
1257 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1258 hint = (struct unwind_hint *)sec->data->d_buf + i;
1260 rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
1262 WARN("can't find rela for unwind_hints[%d]", i);
1266 insn = find_insn(file, rela->sym->sec, rela->addend);
1268 WARN("can't find insn for unwind_hints[%d]", i);
1272 cfa = &insn->cfi.cfa;
1274 if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1275 insn->ret_offset = hint->sp_offset;
1281 switch (hint->sp_reg) {
1282 case ORC_REG_UNDEFINED:
1283 cfa->base = CFI_UNDEFINED;
1291 case ORC_REG_SP_INDIRECT:
1292 cfa->base = CFI_SP_INDIRECT;
1295 cfa->base = CFI_R10;
1298 cfa->base = CFI_R13;
1307 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1308 insn->sec, insn->offset, hint->sp_reg);
1312 cfa->offset = hint->sp_offset;
1313 insn->cfi.type = hint->type;
1314 insn->cfi.end = hint->end;
1320 static int read_retpoline_hints(struct objtool_file *file)
1322 struct section *sec;
1323 struct instruction *insn;
1326 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1330 list_for_each_entry(rela, &sec->rela_list, list) {
1331 if (rela->sym->type != STT_SECTION) {
1332 WARN("unexpected relocation symbol type in %s", sec->name);
1336 insn = find_insn(file, rela->sym->sec, rela->addend);
1338 WARN("bad .discard.retpoline_safe entry");
1342 if (insn->type != INSN_JUMP_DYNAMIC &&
1343 insn->type != INSN_CALL_DYNAMIC) {
1344 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1345 insn->sec, insn->offset);
1349 insn->retpoline_safe = true;
1355 static int read_instr_hints(struct objtool_file *file)
1357 struct section *sec;
1358 struct instruction *insn;
1361 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1365 list_for_each_entry(rela, &sec->rela_list, list) {
1366 if (rela->sym->type != STT_SECTION) {
1367 WARN("unexpected relocation symbol type in %s", sec->name);
1371 insn = find_insn(file, rela->sym->sec, rela->addend);
1373 WARN("bad .discard.instr_end entry");
1380 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1384 list_for_each_entry(rela, &sec->rela_list, list) {
1385 if (rela->sym->type != STT_SECTION) {
1386 WARN("unexpected relocation symbol type in %s", sec->name);
1390 insn = find_insn(file, rela->sym->sec, rela->addend);
1392 WARN("bad .discard.instr_begin entry");
1402 static void mark_rodata(struct objtool_file *file)
1404 struct section *sec;
1408 * Search for the following rodata sections, each of which can
1409 * potentially contain jump tables:
1411 * - .rodata: can contain GCC switch tables
1412 * - .rodata.<func>: same, if -fdata-sections is being used
1413 * - .rodata..c_jump_table: contains C annotated jump tables
1415 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1417 for_each_sec(file, sec) {
1418 if (!strncmp(sec->name, ".rodata", 7) &&
1419 !strstr(sec->name, ".str1.")) {
1425 file->rodata = found;
1428 static int decode_sections(struct objtool_file *file)
1434 ret = decode_instructions(file);
1438 ret = add_dead_ends(file);
1443 add_uaccess_safe(file);
1445 ret = add_ignore_alternatives(file);
1449 ret = add_jump_destinations(file);
1453 ret = add_special_section_alts(file);
1457 ret = add_call_destinations(file);
1461 ret = add_jump_table_alts(file);
1465 ret = read_unwind_hints(file);
1469 ret = read_retpoline_hints(file);
1473 ret = read_instr_hints(file);
1480 static bool is_fentry_call(struct instruction *insn)
1482 if (insn->type == INSN_CALL &&
1483 insn->call_dest->type == STT_NOTYPE &&
1484 !strcmp(insn->call_dest->name, "__fentry__"))
1490 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1492 u8 ret_offset = insn->ret_offset;
1493 struct cfi_state *cfi = &state->cfi;
1496 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1499 if (cfi->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
1502 if (cfi->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1505 for (i = 0; i < CFI_NUM_REGS; i++) {
1506 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1507 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1514 static bool has_valid_stack_frame(struct insn_state *state)
1516 struct cfi_state *cfi = &state->cfi;
1518 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
1519 cfi->regs[CFI_BP].offset == -16)
1522 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
1528 static int update_cfi_state_regs(struct instruction *insn,
1529 struct cfi_state *cfi,
1530 struct stack_op *op)
1532 struct cfi_reg *cfa = &cfi->cfa;
1534 if (cfa->base != CFI_SP)
1538 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1542 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1545 /* add immediate to sp */
1546 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1547 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1548 cfa->offset -= op->src.offset;
1553 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
1555 if (arch_callee_saved_reg(reg) &&
1556 cfi->regs[reg].base == CFI_UNDEFINED) {
1557 cfi->regs[reg].base = base;
1558 cfi->regs[reg].offset = offset;
1562 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
1564 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
1565 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
1569 * A note about DRAP stack alignment:
1571 * GCC has the concept of a DRAP register, which is used to help keep track of
1572 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1573 * register. The typical DRAP pattern is:
1575 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1576 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1577 * 41 ff 72 f8 pushq -0x8(%r10)
1579 * 48 89 e5 mov %rsp,%rbp
1586 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1589 * There are some variations in the epilogues, like:
1597 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1602 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1603 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1604 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1605 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1607 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1610 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1611 * restored beforehand:
1614 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1615 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1617 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1621 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
1622 struct stack_op *op)
1624 struct cfi_reg *cfa = &cfi->cfa;
1625 struct cfi_reg *regs = cfi->regs;
1627 /* stack operations don't make sense with an undefined CFA */
1628 if (cfa->base == CFI_UNDEFINED) {
1630 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1636 if (cfi->type == ORC_TYPE_REGS || cfi->type == ORC_TYPE_REGS_IRET)
1637 return update_cfi_state_regs(insn, cfi, op);
1639 switch (op->dest.type) {
1642 switch (op->src.type) {
1645 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1646 cfa->base == CFI_SP &&
1647 regs[CFI_BP].base == CFI_CFA &&
1648 regs[CFI_BP].offset == -cfa->offset) {
1650 /* mov %rsp, %rbp */
1651 cfa->base = op->dest.reg;
1652 cfi->bp_scratch = false;
1655 else if (op->src.reg == CFI_SP &&
1656 op->dest.reg == CFI_BP && cfi->drap) {
1658 /* drap: mov %rsp, %rbp */
1659 regs[CFI_BP].base = CFI_BP;
1660 regs[CFI_BP].offset = -cfi->stack_size;
1661 cfi->bp_scratch = false;
1664 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1669 * This is needed for the rare case where GCC
1676 cfi->vals[op->dest.reg].base = CFI_CFA;
1677 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
1680 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1681 cfa->base == CFI_BP) {
1686 * Restore the original stack pointer (Clang).
1688 cfi->stack_size = -cfi->regs[CFI_BP].offset;
1691 else if (op->dest.reg == cfa->base) {
1693 /* mov %reg, %rsp */
1694 if (cfa->base == CFI_SP &&
1695 cfi->vals[op->src.reg].base == CFI_CFA) {
1698 * This is needed for the rare case
1699 * where GCC does something dumb like:
1701 * lea 0x8(%rsp), %rcx
1705 cfa->offset = -cfi->vals[op->src.reg].offset;
1706 cfi->stack_size = cfa->offset;
1709 cfa->base = CFI_UNDEFINED;
1717 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1720 cfi->stack_size -= op->src.offset;
1721 if (cfa->base == CFI_SP)
1722 cfa->offset -= op->src.offset;
1726 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1728 /* lea disp(%rbp), %rsp */
1729 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1733 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1735 /* drap: lea disp(%rsp), %drap */
1736 cfi->drap_reg = op->dest.reg;
1739 * lea disp(%rsp), %reg
1741 * This is needed for the rare case where GCC
1742 * does something dumb like:
1744 * lea 0x8(%rsp), %rcx
1748 cfi->vals[op->dest.reg].base = CFI_CFA;
1749 cfi->vals[op->dest.reg].offset = \
1750 -cfi->stack_size + op->src.offset;
1755 if (cfi->drap && op->dest.reg == CFI_SP &&
1756 op->src.reg == cfi->drap_reg) {
1758 /* drap: lea disp(%drap), %rsp */
1760 cfa->offset = cfi->stack_size = -op->src.offset;
1761 cfi->drap_reg = CFI_UNDEFINED;
1766 if (op->dest.reg == cfi->cfa.base) {
1767 WARN_FUNC("unsupported stack register modification",
1768 insn->sec, insn->offset);
1775 if (op->dest.reg != CFI_SP ||
1776 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1777 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1778 WARN_FUNC("unsupported stack pointer realignment",
1779 insn->sec, insn->offset);
1783 if (cfi->drap_reg != CFI_UNDEFINED) {
1784 /* drap: and imm, %rsp */
1785 cfa->base = cfi->drap_reg;
1786 cfa->offset = cfi->stack_size = 0;
1791 * Older versions of GCC (4.8ish) realign the stack
1792 * without DRAP, with a frame pointer.
1799 if (!cfi->drap && op->dest.reg == cfa->base) {
1805 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
1806 op->dest.reg == cfi->drap_reg &&
1807 cfi->drap_offset == -cfi->stack_size) {
1809 /* drap: pop %drap */
1810 cfa->base = cfi->drap_reg;
1812 cfi->drap_offset = -1;
1814 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
1817 restore_reg(cfi, op->dest.reg);
1820 cfi->stack_size -= 8;
1821 if (cfa->base == CFI_SP)
1826 case OP_SRC_REG_INDIRECT:
1827 if (cfi->drap && op->src.reg == CFI_BP &&
1828 op->src.offset == cfi->drap_offset) {
1830 /* drap: mov disp(%rbp), %drap */
1831 cfa->base = cfi->drap_reg;
1833 cfi->drap_offset = -1;
1836 if (cfi->drap && op->src.reg == CFI_BP &&
1837 op->src.offset == regs[op->dest.reg].offset) {
1839 /* drap: mov disp(%rbp), %reg */
1840 restore_reg(cfi, op->dest.reg);
1842 } else if (op->src.reg == cfa->base &&
1843 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1845 /* mov disp(%rbp), %reg */
1846 /* mov disp(%rsp), %reg */
1847 restore_reg(cfi, op->dest.reg);
1853 WARN_FUNC("unknown stack-related instruction",
1854 insn->sec, insn->offset);
1862 cfi->stack_size += 8;
1863 if (cfa->base == CFI_SP)
1866 if (op->src.type != OP_SRC_REG)
1870 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
1872 /* drap: push %drap */
1873 cfa->base = CFI_BP_INDIRECT;
1874 cfa->offset = -cfi->stack_size;
1876 /* save drap so we know when to restore it */
1877 cfi->drap_offset = -cfi->stack_size;
1879 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
1881 /* drap: push %rbp */
1882 cfi->stack_size = 0;
1884 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1886 /* drap: push %reg */
1887 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
1893 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
1896 /* detect when asm code uses rbp as a scratch register */
1897 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1898 cfa->base != CFI_BP)
1899 cfi->bp_scratch = true;
1902 case OP_DEST_REG_INDIRECT:
1905 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
1907 /* drap: mov %drap, disp(%rbp) */
1908 cfa->base = CFI_BP_INDIRECT;
1909 cfa->offset = op->dest.offset;
1911 /* save drap offset so we know when to restore it */
1912 cfi->drap_offset = op->dest.offset;
1915 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1917 /* drap: mov reg, disp(%rbp) */
1918 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
1921 } else if (op->dest.reg == cfa->base) {
1923 /* mov reg, disp(%rbp) */
1924 /* mov reg, disp(%rsp) */
1925 save_reg(cfi, op->src.reg, CFI_CFA,
1926 op->dest.offset - cfi->cfa.offset);
1932 if ((!cfi->drap && cfa->base != CFI_BP) ||
1933 (cfi->drap && cfa->base != cfi->drap_reg)) {
1934 WARN_FUNC("leave instruction with modified stack frame",
1935 insn->sec, insn->offset);
1939 /* leave (mov %rbp, %rsp; pop %rbp) */
1941 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
1942 restore_reg(cfi, CFI_BP);
1952 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
1953 WARN_FUNC("unknown stack-related memory operation",
1954 insn->sec, insn->offset);
1959 cfi->stack_size -= 8;
1960 if (cfa->base == CFI_SP)
1966 WARN_FUNC("unknown stack-related instruction",
1967 insn->sec, insn->offset);
1974 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
1976 struct stack_op *op;
1978 list_for_each_entry(op, &insn->stack_ops, list) {
1981 res = update_cfi_state(insn, &state->cfi, op);
1985 if (op->dest.type == OP_DEST_PUSHF) {
1986 if (!state->uaccess_stack) {
1987 state->uaccess_stack = 1;
1988 } else if (state->uaccess_stack >> 31) {
1989 WARN_FUNC("PUSHF stack exhausted",
1990 insn->sec, insn->offset);
1993 state->uaccess_stack <<= 1;
1994 state->uaccess_stack |= state->uaccess;
1997 if (op->src.type == OP_SRC_POPF) {
1998 if (state->uaccess_stack) {
1999 state->uaccess = state->uaccess_stack & 1;
2000 state->uaccess_stack >>= 1;
2001 if (state->uaccess_stack == 1)
2002 state->uaccess_stack = 0;
2010 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2012 struct cfi_state *cfi1 = &insn->cfi;
2015 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2017 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2018 insn->sec, insn->offset,
2019 cfi1->cfa.base, cfi1->cfa.offset,
2020 cfi2->cfa.base, cfi2->cfa.offset);
2022 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2023 for (i = 0; i < CFI_NUM_REGS; i++) {
2024 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2025 sizeof(struct cfi_reg)))
2028 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2029 insn->sec, insn->offset,
2030 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2031 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2035 } else if (cfi1->type != cfi2->type) {
2037 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2038 insn->sec, insn->offset, cfi1->type, cfi2->type);
2040 } else if (cfi1->drap != cfi2->drap ||
2041 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2042 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2044 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2045 insn->sec, insn->offset,
2046 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2047 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2055 static inline bool func_uaccess_safe(struct symbol *func)
2058 return func->uaccess_safe;
2063 static inline const char *call_dest_name(struct instruction *insn)
2065 if (insn->call_dest)
2066 return insn->call_dest->name;
2071 static int validate_call(struct instruction *insn, struct insn_state *state)
2073 if (state->noinstr && state->instr <= 0 &&
2074 (!insn->call_dest || insn->call_dest->sec != insn->sec)) {
2075 WARN_FUNC("call to %s() leaves .noinstr.text section",
2076 insn->sec, insn->offset, call_dest_name(insn));
2080 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2081 WARN_FUNC("call to %s() with UACCESS enabled",
2082 insn->sec, insn->offset, call_dest_name(insn));
2087 WARN_FUNC("call to %s() with DF set",
2088 insn->sec, insn->offset, call_dest_name(insn));
2095 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2097 if (has_modified_stack_frame(insn, state)) {
2098 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2099 insn->sec, insn->offset);
2103 return validate_call(insn, state);
2106 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2108 if (state->noinstr && state->instr > 0) {
2109 WARN_FUNC("return with instrumentation enabled",
2110 insn->sec, insn->offset);
2114 if (state->uaccess && !func_uaccess_safe(func)) {
2115 WARN_FUNC("return with UACCESS enabled",
2116 insn->sec, insn->offset);
2120 if (!state->uaccess && func_uaccess_safe(func)) {
2121 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2122 insn->sec, insn->offset);
2127 WARN_FUNC("return with DF set",
2128 insn->sec, insn->offset);
2132 if (func && has_modified_stack_frame(insn, state)) {
2133 WARN_FUNC("return with modified stack frame",
2134 insn->sec, insn->offset);
2138 if (state->cfi.bp_scratch) {
2139 WARN_FUNC("BP used as a scratch register",
2140 insn->sec, insn->offset);
2148 * Follow the branch starting at the given instruction, and recursively follow
2149 * any other branches (jumps). Meanwhile, track the frame pointer state at
2150 * each instruction and validate all the rules described in
2151 * tools/objtool/Documentation/stack-validation.txt.
2153 static int validate_branch(struct objtool_file *file, struct symbol *func,
2154 struct instruction *insn, struct insn_state state)
2156 struct alternative *alt;
2157 struct instruction *next_insn;
2158 struct section *sec;
2164 if (insn->alt_group && list_empty(&insn->alts)) {
2165 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
2171 next_insn = next_insn_same_sec(file, insn);
2173 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2174 WARN("%s() falls through to next function %s()",
2175 func->name, insn->func->name);
2179 if (func && insn->ignore) {
2180 WARN_FUNC("BUG: why am I validating an ignored function?",
2185 visited = 1 << state.uaccess;
2186 if (insn->visited) {
2187 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2190 if (insn->visited & visited)
2195 state.instr += insn->instr;
2198 state.cfi = insn->cfi;
2200 insn->cfi = state.cfi;
2202 insn->visited |= visited;
2204 if (!insn->ignore_alts) {
2205 bool skip_orig = false;
2207 list_for_each_entry(alt, &insn->alts, list) {
2211 ret = validate_branch(file, func, alt->insn, state);
2214 BT_FUNC("(alt)", insn);
2223 switch (insn->type) {
2226 return validate_return(func, insn, &state);
2229 case INSN_CALL_DYNAMIC:
2230 ret = validate_call(insn, &state);
2234 if (!no_fp && func && !is_fentry_call(insn) &&
2235 !has_valid_stack_frame(&state)) {
2236 WARN_FUNC("call without frame pointer save/setup",
2241 if (dead_end_function(file, insn->call_dest))
2246 case INSN_JUMP_CONDITIONAL:
2247 case INSN_JUMP_UNCONDITIONAL:
2248 if (func && is_sibling_call(insn)) {
2249 ret = validate_sibling_call(insn, &state);
2253 } else if (insn->jump_dest) {
2254 ret = validate_branch(file, func,
2255 insn->jump_dest, state);
2258 BT_FUNC("(branch)", insn);
2263 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2268 case INSN_JUMP_DYNAMIC:
2269 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2270 if (func && is_sibling_call(insn)) {
2271 ret = validate_sibling_call(insn, &state);
2276 if (insn->type == INSN_JUMP_DYNAMIC)
2281 case INSN_EXCEPTION_RETURN:
2282 if (handle_insn_ops(insn, &state))
2286 * This handles x86's sync_core() case, where we use an
2287 * IRET to self. All 'normal' IRET instructions are in
2288 * STT_NOTYPE entry symbols.
2295 case INSN_CONTEXT_SWITCH:
2296 if (func && (!next_insn || !next_insn->hint)) {
2297 WARN_FUNC("unsupported instruction in callable function",
2304 if (handle_insn_ops(insn, &state))
2309 if (state.uaccess) {
2310 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2314 state.uaccess = true;
2318 if (!state.uaccess && func) {
2319 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2323 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2324 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2328 state.uaccess = false;
2333 WARN_FUNC("recursive STD", sec, insn->offset);
2339 if (!state.df && func)
2340 WARN_FUNC("redundant CLD", sec, insn->offset);
2353 if (state.cfi.cfa.base == CFI_UNDEFINED)
2355 WARN("%s: unexpected end of section", sec->name);
2365 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2367 struct instruction *insn;
2368 struct insn_state state;
2369 int ret, warnings = 0;
2374 init_insn_state(&state, sec);
2377 insn = find_insn(file, sec, 0);
2381 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2384 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
2385 if (insn->hint && !insn->visited) {
2386 ret = validate_branch(file, insn->func, insn, state);
2387 if (ret && backtrace)
2388 BT_FUNC("<=== (hint)", insn);
2392 insn = list_next_entry(insn, list);
2398 static int validate_retpoline(struct objtool_file *file)
2400 struct instruction *insn;
2403 for_each_insn(file, insn) {
2404 if (insn->type != INSN_JUMP_DYNAMIC &&
2405 insn->type != INSN_CALL_DYNAMIC)
2408 if (insn->retpoline_safe)
2412 * .init.text code is ran before userspace and thus doesn't
2413 * strictly need retpolines, except for modules which are
2414 * loaded late, they very much do need retpoline in their
2417 if (!strcmp(insn->sec->name, ".init.text") && !module)
2420 WARN_FUNC("indirect %s found in RETPOLINE build",
2421 insn->sec, insn->offset,
2422 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2430 static bool is_kasan_insn(struct instruction *insn)
2432 return (insn->type == INSN_CALL &&
2433 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2436 static bool is_ubsan_insn(struct instruction *insn)
2438 return (insn->type == INSN_CALL &&
2439 !strcmp(insn->call_dest->name,
2440 "__ubsan_handle_builtin_unreachable"));
2443 static bool ignore_unreachable_insn(struct instruction *insn)
2447 if (insn->ignore || insn->type == INSN_NOP)
2451 * Ignore any unused exceptions. This can happen when a whitelisted
2452 * function has an exception table entry.
2454 * Also ignore alternative replacement instructions. This can happen
2455 * when a whitelisted function uses one of the ALTERNATIVE macros.
2457 if (!strcmp(insn->sec->name, ".fixup") ||
2458 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2459 !strcmp(insn->sec->name, ".altinstr_aux"))
2466 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2467 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2468 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2469 * (or occasionally a JMP to UD2).
2471 if (list_prev_entry(insn, list)->dead_end &&
2472 (insn->type == INSN_BUG ||
2473 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2474 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2478 * Check if this (or a subsequent) instruction is related to
2479 * CONFIG_UBSAN or CONFIG_KASAN.
2481 * End the search at 5 instructions to avoid going into the weeds.
2483 for (i = 0; i < 5; i++) {
2485 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2488 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2489 if (insn->jump_dest &&
2490 insn->jump_dest->func == insn->func) {
2491 insn = insn->jump_dest;
2498 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2501 insn = list_next_entry(insn, list);
2507 static int validate_symbol(struct objtool_file *file, struct section *sec,
2508 struct symbol *sym, struct insn_state *state)
2510 struct instruction *insn;
2514 WARN("%s() is missing an ELF size annotation", sym->name);
2518 if (sym->pfunc != sym || sym->alias != sym)
2521 insn = find_insn(file, sec, sym->offset);
2522 if (!insn || insn->ignore || insn->visited)
2525 state->uaccess = sym->uaccess_safe;
2527 ret = validate_branch(file, insn->func, insn, *state);
2528 if (ret && backtrace)
2529 BT_FUNC("<=== (sym)", insn);
2533 static int validate_section(struct objtool_file *file, struct section *sec)
2535 struct insn_state state;
2536 struct symbol *func;
2539 list_for_each_entry(func, &sec->symbol_list, list) {
2540 if (func->type != STT_FUNC)
2543 init_insn_state(&state, sec);
2544 state.cfi.cfa = initial_func_cfi.cfa;
2545 memcpy(&state.cfi.regs, &initial_func_cfi.regs,
2546 CFI_NUM_REGS * sizeof(struct cfi_reg));
2547 state.cfi.stack_size = initial_func_cfi.cfa.offset;
2549 warnings += validate_symbol(file, sec, func, &state);
2555 static int validate_vmlinux_functions(struct objtool_file *file)
2557 struct section *sec;
2560 sec = find_section_by_name(file->elf, ".noinstr.text");
2564 warnings += validate_section(file, sec);
2565 warnings += validate_unwind_hints(file, sec);
2570 static int validate_functions(struct objtool_file *file)
2572 struct section *sec;
2575 for_each_sec(file, sec) {
2576 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
2579 warnings += validate_section(file, sec);
2585 static int validate_reachable_instructions(struct objtool_file *file)
2587 struct instruction *insn;
2589 if (file->ignore_unreachables)
2592 for_each_insn(file, insn) {
2593 if (insn->visited || ignore_unreachable_insn(insn))
2596 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2603 static struct objtool_file file;
2605 int check(const char *_objname, bool orc)
2607 int ret, warnings = 0;
2611 file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
2615 INIT_LIST_HEAD(&file.insn_list);
2616 hash_init(file.insn_hash);
2617 file.c_file = find_section_by_name(file.elf, ".comment");
2618 file.ignore_unreachables = no_unreachable;
2621 arch_initial_func_cfi_state(&initial_func_cfi);
2623 ret = decode_sections(&file);
2628 if (list_empty(&file.insn_list))
2631 if (vmlinux && !validate_dup) {
2632 ret = validate_vmlinux_functions(&file);
2641 ret = validate_retpoline(&file);
2647 ret = validate_functions(&file);
2652 ret = validate_unwind_hints(&file, NULL);
2658 ret = validate_reachable_instructions(&file);
2665 ret = create_orc(&file);
2669 ret = create_orc_sections(&file);
2673 ret = elf_write(file.elf);
2681 * Fatal error. The binary is corrupt or otherwise broken in
2682 * some way, or objtool itself is broken. Fail the kernel