2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <linux/hashtable.h>
29 #include <linux/kernel.h>
32 struct list_head list
;
33 struct instruction
*insn
;
38 struct cfi_state initial_func_cfi
;
40 struct instruction
*find_insn(struct objtool_file
*file
,
41 struct section
*sec
, unsigned long offset
)
43 struct instruction
*insn
;
45 hash_for_each_possible(file
->insn_hash
, insn
, hash
, offset
)
46 if (insn
->sec
== sec
&& insn
->offset
== offset
)
52 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
53 struct instruction
*insn
)
55 struct instruction
*next
= list_next_entry(insn
, list
);
57 if (!next
|| &next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
63 static struct instruction
*next_insn_same_func(struct objtool_file
*file
,
64 struct instruction
*insn
)
66 struct instruction
*next
= list_next_entry(insn
, list
);
67 struct symbol
*func
= insn
->func
;
72 if (&next
->list
!= &file
->insn_list
&& next
->func
== func
)
75 /* Check if we're already in the subfunction: */
76 if (func
== func
->cfunc
)
79 /* Move to the subfunction: */
80 return find_insn(file
, func
->cfunc
->sec
, func
->cfunc
->offset
);
83 #define func_for_each_insn_all(file, func, insn) \
84 for (insn = find_insn(file, func->sec, func->offset); \
86 insn = next_insn_same_func(file, insn))
88 #define func_for_each_insn(file, func, insn) \
89 for (insn = find_insn(file, func->sec, func->offset); \
90 insn && &insn->list != &file->insn_list && \
91 insn->sec == func->sec && \
92 insn->offset < func->offset + func->len; \
93 insn = list_next_entry(insn, list))
95 #define func_for_each_insn_continue_reverse(file, func, insn) \
96 for (insn = list_prev_entry(insn, list); \
97 &insn->list != &file->insn_list && \
98 insn->sec == func->sec && insn->offset >= func->offset; \
99 insn = list_prev_entry(insn, list))
101 #define sec_for_each_insn_from(file, insn) \
102 for (; insn; insn = next_insn_same_sec(file, insn))
104 #define sec_for_each_insn_continue(file, insn) \
105 for (insn = next_insn_same_sec(file, insn); insn; \
106 insn = next_insn_same_sec(file, insn))
109 * This checks to see if the given function is a "noreturn" function.
111 * For global functions which are outside the scope of this object file, we
112 * have to keep a manual list of them.
114 * For local functions, we have to detect them manually by simply looking for
115 * the lack of a return instruction.
122 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
126 struct instruction
*insn
;
130 * Unfortunately these have to be hard coded because the noreturn
131 * attribute isn't provided in ELF data.
133 static const char * const global_noreturns
[] = {
138 "__module_put_and_exit",
140 "kvm_spurious_fault",
145 "machine_real_restart",
148 if (func
->bind
== STB_WEAK
)
151 if (func
->bind
== STB_GLOBAL
)
152 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
153 if (!strcmp(func
->name
, global_noreturns
[i
]))
159 insn
= find_insn(file
, func
->sec
, func
->offset
);
163 func_for_each_insn_all(file
, func
, insn
) {
166 if (insn
->type
== INSN_RETURN
)
174 * A function can have a sibling call instead of a return. In that
175 * case, the function's dead-end status depends on whether the target
176 * of the sibling call returns.
178 func_for_each_insn_all(file
, func
, insn
) {
179 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
180 struct instruction
*dest
= insn
->jump_dest
;
183 /* sibling call to another file */
186 if (dest
->func
&& dest
->func
->pfunc
!= insn
->func
->pfunc
) {
188 /* local sibling call */
189 if (recursion
== 5) {
191 * Infinite recursion: two functions
192 * have sibling calls to each other.
193 * This is a very rare case. It means
194 * they aren't dead ends.
199 return __dead_end_function(file
, dest
->func
,
204 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
212 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
214 return __dead_end_function(file
, func
, 0);
217 static void clear_insn_state(struct insn_state
*state
)
221 memset(state
, 0, sizeof(*state
));
222 state
->cfa
.base
= CFI_UNDEFINED
;
223 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
224 state
->regs
[i
].base
= CFI_UNDEFINED
;
225 state
->vals
[i
].base
= CFI_UNDEFINED
;
227 state
->drap_reg
= CFI_UNDEFINED
;
228 state
->drap_offset
= -1;
232 * Call the arch-specific instruction decoder for all the instructions and add
233 * them to the global instruction list.
235 static int decode_instructions(struct objtool_file
*file
)
239 unsigned long offset
;
240 struct instruction
*insn
;
243 for_each_sec(file
, sec
) {
245 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
248 if (strcmp(sec
->name
, ".altinstr_replacement") &&
249 strcmp(sec
->name
, ".altinstr_aux") &&
250 strncmp(sec
->name
, ".discard.", 9))
253 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
254 insn
= malloc(sizeof(*insn
));
256 WARN("malloc failed");
259 memset(insn
, 0, sizeof(*insn
));
260 INIT_LIST_HEAD(&insn
->alts
);
261 clear_insn_state(&insn
->state
);
264 insn
->offset
= offset
;
266 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
268 &insn
->len
, &insn
->type
,
274 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
275 WARN_FUNC("invalid instruction type %d",
276 insn
->sec
, insn
->offset
, insn
->type
);
281 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
282 list_add_tail(&insn
->list
, &file
->insn_list
);
285 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
286 if (func
->type
!= STT_FUNC
)
289 if (!find_insn(file
, sec
, func
->offset
)) {
290 WARN("%s(): can't find starting instruction",
295 func_for_each_insn(file
, func
, insn
)
309 * Mark "ud2" instructions and manually annotated dead ends.
311 static int add_dead_ends(struct objtool_file
*file
)
315 struct instruction
*insn
;
319 * By default, "ud2" is a dead end unless otherwise annotated, because
320 * GCC 7 inserts it for certain divide-by-zero cases.
322 for_each_insn(file
, insn
)
323 if (insn
->type
== INSN_BUG
)
324 insn
->dead_end
= true;
327 * Check for manually annotated dead ends.
329 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
333 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
334 if (rela
->sym
->type
!= STT_SECTION
) {
335 WARN("unexpected relocation symbol type in %s", sec
->name
);
338 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
340 insn
= list_prev_entry(insn
, list
);
341 else if (rela
->addend
== rela
->sym
->sec
->len
) {
343 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
344 if (insn
->sec
== rela
->sym
->sec
) {
351 WARN("can't find unreachable insn at %s+0x%x",
352 rela
->sym
->sec
->name
, rela
->addend
);
356 WARN("can't find unreachable insn at %s+0x%x",
357 rela
->sym
->sec
->name
, rela
->addend
);
361 insn
->dead_end
= true;
366 * These manually annotated reachable checks are needed for GCC 4.4,
367 * where the Linux unreachable() macro isn't supported. In that case
368 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
371 sec
= find_section_by_name(file
->elf
, ".rela.discard.reachable");
375 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
376 if (rela
->sym
->type
!= STT_SECTION
) {
377 WARN("unexpected relocation symbol type in %s", sec
->name
);
380 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
382 insn
= list_prev_entry(insn
, list
);
383 else if (rela
->addend
== rela
->sym
->sec
->len
) {
385 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
386 if (insn
->sec
== rela
->sym
->sec
) {
393 WARN("can't find reachable insn at %s+0x%x",
394 rela
->sym
->sec
->name
, rela
->addend
);
398 WARN("can't find reachable insn at %s+0x%x",
399 rela
->sym
->sec
->name
, rela
->addend
);
403 insn
->dead_end
= false;
410 * Warnings shouldn't be reported for ignored functions.
412 static void add_ignores(struct objtool_file
*file
)
414 struct instruction
*insn
;
419 sec
= find_section_by_name(file
->elf
, ".rela.discard.func_stack_frame_non_standard");
423 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
424 switch (rela
->sym
->type
) {
430 func
= find_symbol_by_offset(rela
->sym
->sec
, rela
->addend
);
431 if (!func
|| func
->type
!= STT_FUNC
)
436 WARN("unexpected relocation symbol type in %s: %d", sec
->name
, rela
->sym
->type
);
440 func_for_each_insn_all(file
, func
, insn
)
446 * This is a whitelist of functions that is allowed to be called with AC set.
447 * The list is meant to be minimal and only contains compiler instrumentation
448 * ABI and a few functions used to implement *_{to,from}_user() functions.
450 * These functions must not directly change AC, but may PUSHF/POPF.
452 static const char *uaccess_safe_builtin
[] = {
455 "check_memory_region",
456 /* KASAN out-of-line */
457 "__asan_loadN_noabort",
458 "__asan_load1_noabort",
459 "__asan_load2_noabort",
460 "__asan_load4_noabort",
461 "__asan_load8_noabort",
462 "__asan_load16_noabort",
463 "__asan_storeN_noabort",
464 "__asan_store1_noabort",
465 "__asan_store2_noabort",
466 "__asan_store4_noabort",
467 "__asan_store8_noabort",
468 "__asan_store16_noabort",
470 "__asan_report_load_n_noabort",
471 "__asan_report_load1_noabort",
472 "__asan_report_load2_noabort",
473 "__asan_report_load4_noabort",
474 "__asan_report_load8_noabort",
475 "__asan_report_load16_noabort",
476 "__asan_report_store_n_noabort",
477 "__asan_report_store1_noabort",
478 "__asan_report_store2_noabort",
479 "__asan_report_store4_noabort",
480 "__asan_report_store8_noabort",
481 "__asan_report_store16_noabort",
484 "__sanitizer_cov_trace_pc",
485 "__sanitizer_cov_trace_const_cmp1",
486 "__sanitizer_cov_trace_const_cmp2",
487 "__sanitizer_cov_trace_const_cmp4",
488 "__sanitizer_cov_trace_const_cmp8",
489 "__sanitizer_cov_trace_cmp1",
490 "__sanitizer_cov_trace_cmp2",
491 "__sanitizer_cov_trace_cmp4",
492 "__sanitizer_cov_trace_cmp8",
494 "ubsan_type_mismatch_common",
495 "__ubsan_handle_type_mismatch",
496 "__ubsan_handle_type_mismatch_v1",
498 "csum_partial_copy_generic",
500 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
504 static void add_uaccess_safe(struct objtool_file
*file
)
512 for (name
= uaccess_safe_builtin
; *name
; name
++) {
513 func
= find_symbol_by_name(file
->elf
, *name
);
517 func
->alias
->uaccess_safe
= true;
522 * FIXME: For now, just ignore any alternatives which add retpolines. This is
523 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
524 * But it at least allows objtool to understand the control flow *around* the
527 static int add_ignore_alternatives(struct objtool_file
*file
)
531 struct instruction
*insn
;
533 sec
= find_section_by_name(file
->elf
, ".rela.discard.ignore_alts");
537 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
538 if (rela
->sym
->type
!= STT_SECTION
) {
539 WARN("unexpected relocation symbol type in %s", sec
->name
);
543 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
545 WARN("bad .discard.ignore_alts entry");
549 insn
->ignore_alts
= true;
556 * Find the destination instructions for all jumps.
558 static int add_jump_destinations(struct objtool_file
*file
)
560 struct instruction
*insn
;
562 struct section
*dest_sec
;
563 unsigned long dest_off
;
565 for_each_insn(file
, insn
) {
566 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
567 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
573 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
576 dest_sec
= insn
->sec
;
577 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
578 } else if (rela
->sym
->type
== STT_SECTION
) {
579 dest_sec
= rela
->sym
->sec
;
580 dest_off
= rela
->addend
+ 4;
581 } else if (rela
->sym
->sec
->idx
) {
582 dest_sec
= rela
->sym
->sec
;
583 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
584 } else if (strstr(rela
->sym
->name
, "_indirect_thunk_")) {
586 * Retpoline jumps are really dynamic jumps in
587 * disguise, so convert them accordingly.
589 insn
->type
= INSN_JUMP_DYNAMIC
;
590 insn
->retpoline_safe
= true;
594 insn
->call_dest
= rela
->sym
;
595 insn
->jump_dest
= NULL
;
599 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
600 if (!insn
->jump_dest
) {
603 * This is a special case where an alt instruction
604 * jumps past the end of the section. These are
605 * handled later in handle_group_alt().
607 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
610 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
611 insn
->sec
, insn
->offset
, dest_sec
->name
,
617 * Cross-function jump.
619 if (insn
->func
&& insn
->jump_dest
->func
&&
620 insn
->func
!= insn
->jump_dest
->func
) {
623 * For GCC 8+, create parent/child links for any cold
624 * subfunctions. This is _mostly_ redundant with a
625 * similar initialization in read_symbols().
627 * If a function has aliases, we want the *first* such
628 * function in the symbol table to be the subfunction's
629 * parent. In that case we overwrite the
630 * initialization done in read_symbols().
632 * However this code can't completely replace the
633 * read_symbols() code because this doesn't detect the
634 * case where the parent function's only reference to a
635 * subfunction is through a switch table.
637 if (!strstr(insn
->func
->name
, ".cold.") &&
638 strstr(insn
->jump_dest
->func
->name
, ".cold.")) {
639 insn
->func
->cfunc
= insn
->jump_dest
->func
;
640 insn
->jump_dest
->func
->pfunc
= insn
->func
;
642 } else if (insn
->jump_dest
->func
->pfunc
!= insn
->func
->pfunc
&&
643 insn
->jump_dest
->offset
== insn
->jump_dest
->func
->offset
) {
646 insn
->call_dest
= insn
->jump_dest
->func
;
647 insn
->jump_dest
= NULL
;
656 * Find the destination instructions for all calls.
658 static int add_call_destinations(struct objtool_file
*file
)
660 struct instruction
*insn
;
661 unsigned long dest_off
;
664 for_each_insn(file
, insn
) {
665 if (insn
->type
!= INSN_CALL
)
668 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
671 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
672 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
675 if (!insn
->call_dest
&& !insn
->ignore
) {
676 WARN_FUNC("unsupported intra-function call",
677 insn
->sec
, insn
->offset
);
679 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
683 } else if (rela
->sym
->type
== STT_SECTION
) {
684 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
686 if (!insn
->call_dest
||
687 insn
->call_dest
->type
!= STT_FUNC
) {
688 WARN_FUNC("can't find call dest symbol at %s+0x%x",
689 insn
->sec
, insn
->offset
,
690 rela
->sym
->sec
->name
,
695 insn
->call_dest
= rela
->sym
;
702 * The .alternatives section requires some extra special care, over and above
703 * what other special sections require:
705 * 1. Because alternatives are patched in-place, we need to insert a fake jump
706 * instruction at the end so that validate_branch() skips all the original
707 * replaced instructions when validating the new instruction path.
709 * 2. An added wrinkle is that the new instruction length might be zero. In
710 * that case the old instructions are replaced with noops. We simulate that
711 * by creating a fake jump as the only new instruction.
713 * 3. In some cases, the alternative section includes an instruction which
714 * conditionally jumps to the _end_ of the entry. We have to modify these
715 * jumps' destinations to point back to .text rather than the end of the
716 * entry in .altinstr_replacement.
718 static int handle_group_alt(struct objtool_file
*file
,
719 struct special_alt
*special_alt
,
720 struct instruction
*orig_insn
,
721 struct instruction
**new_insn
)
723 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
= NULL
;
724 unsigned long dest_off
;
726 last_orig_insn
= NULL
;
728 sec_for_each_insn_from(file
, insn
) {
729 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
732 insn
->alt_group
= true;
733 last_orig_insn
= insn
;
736 if (next_insn_same_sec(file
, last_orig_insn
)) {
737 fake_jump
= malloc(sizeof(*fake_jump
));
739 WARN("malloc failed");
742 memset(fake_jump
, 0, sizeof(*fake_jump
));
743 INIT_LIST_HEAD(&fake_jump
->alts
);
744 clear_insn_state(&fake_jump
->state
);
746 fake_jump
->sec
= special_alt
->new_sec
;
747 fake_jump
->offset
= -1;
748 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
749 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
750 fake_jump
->ignore
= true;
753 if (!special_alt
->new_len
) {
755 WARN("%s: empty alternative at end of section",
756 special_alt
->orig_sec
->name
);
760 *new_insn
= fake_jump
;
764 last_new_insn
= NULL
;
766 sec_for_each_insn_from(file
, insn
) {
767 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
770 last_new_insn
= insn
;
772 insn
->ignore
= orig_insn
->ignore_alts
;
773 insn
->func
= orig_insn
->func
;
775 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
776 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
779 if (!insn
->immediate
)
782 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
783 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
) {
785 WARN("%s: alternative jump to end of section",
786 special_alt
->orig_sec
->name
);
789 insn
->jump_dest
= fake_jump
;
792 if (!insn
->jump_dest
) {
793 WARN_FUNC("can't find alternative jump destination",
794 insn
->sec
, insn
->offset
);
799 if (!last_new_insn
) {
800 WARN_FUNC("can't find last new alternative instruction",
801 special_alt
->new_sec
, special_alt
->new_off
);
806 list_add(&fake_jump
->list
, &last_new_insn
->list
);
812 * A jump table entry can either convert a nop to a jump or a jump to a nop.
813 * If the original instruction is a jump, make the alt entry an effective nop
814 * by just skipping the original instruction.
816 static int handle_jump_alt(struct objtool_file
*file
,
817 struct special_alt
*special_alt
,
818 struct instruction
*orig_insn
,
819 struct instruction
**new_insn
)
821 if (orig_insn
->type
== INSN_NOP
)
824 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
825 WARN_FUNC("unsupported instruction at jump label",
826 orig_insn
->sec
, orig_insn
->offset
);
830 *new_insn
= list_next_entry(orig_insn
, list
);
835 * Read all the special sections which have alternate instructions which can be
836 * patched in or redirected to at runtime. Each instruction having alternate
837 * instruction(s) has them added to its insn->alts list, which will be
838 * traversed in validate_branch().
840 static int add_special_section_alts(struct objtool_file
*file
)
842 struct list_head special_alts
;
843 struct instruction
*orig_insn
, *new_insn
;
844 struct special_alt
*special_alt
, *tmp
;
845 struct alternative
*alt
;
848 ret
= special_get_alts(file
->elf
, &special_alts
);
852 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
854 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
855 special_alt
->orig_off
);
857 WARN_FUNC("special: can't find orig instruction",
858 special_alt
->orig_sec
, special_alt
->orig_off
);
864 if (!special_alt
->group
|| special_alt
->new_len
) {
865 new_insn
= find_insn(file
, special_alt
->new_sec
,
866 special_alt
->new_off
);
868 WARN_FUNC("special: can't find new instruction",
869 special_alt
->new_sec
,
870 special_alt
->new_off
);
876 if (special_alt
->group
) {
877 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
881 } else if (special_alt
->jump_or_nop
) {
882 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
888 alt
= malloc(sizeof(*alt
));
890 WARN("malloc failed");
895 alt
->insn
= new_insn
;
896 alt
->skip_orig
= special_alt
->skip_orig
;
897 orig_insn
->ignore_alts
|= special_alt
->skip_alt
;
898 list_add_tail(&alt
->list
, &orig_insn
->alts
);
900 list_del(&special_alt
->list
);
908 static int add_switch_table(struct objtool_file
*file
, struct instruction
*insn
,
909 struct rela
*table
, struct rela
*next_table
)
911 struct rela
*rela
= table
;
912 struct instruction
*alt_insn
;
913 struct alternative
*alt
;
914 struct symbol
*pfunc
= insn
->func
->pfunc
;
915 unsigned int prev_offset
= 0;
917 list_for_each_entry_from(rela
, &table
->rela_sec
->rela_list
, list
) {
918 if (rela
== next_table
)
921 /* Make sure the switch table entries are consecutive: */
922 if (prev_offset
&& rela
->offset
!= prev_offset
+ 8)
925 /* Detect function pointers from contiguous objects: */
926 if (rela
->sym
->sec
== pfunc
->sec
&&
927 rela
->addend
== pfunc
->offset
)
930 alt_insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
934 /* Make sure the jmp dest is in the function or subfunction: */
935 if (alt_insn
->func
->pfunc
!= pfunc
)
938 alt
= malloc(sizeof(*alt
));
940 WARN("malloc failed");
944 alt
->insn
= alt_insn
;
945 list_add_tail(&alt
->list
, &insn
->alts
);
946 prev_offset
= rela
->offset
;
950 WARN_FUNC("can't find switch jump table",
951 insn
->sec
, insn
->offset
);
959 * find_switch_table() - Given a dynamic jump, find the switch jump table in
960 * .rodata associated with it.
962 * There are 3 basic patterns:
964 * 1. jmpq *[rodata addr](,%reg,8)
966 * This is the most common case by far. It jumps to an address in a simple
967 * jump table which is stored in .rodata.
969 * 2. jmpq *[rodata addr](%rip)
971 * This is caused by a rare GCC quirk, currently only seen in three driver
972 * functions in the kernel, only with certain obscure non-distro configs.
974 * As part of an optimization, GCC makes a copy of an existing switch jump
975 * table, modifies it, and then hard-codes the jump (albeit with an indirect
976 * jump) to use a single entry in the table. The rest of the jump table and
977 * some of its jump targets remain as dead code.
979 * In such a case we can just crudely ignore all unreachable instruction
980 * warnings for the entire object file. Ideally we would just ignore them
981 * for the function, but that would require redesigning the code quite a
982 * bit. And honestly that's just not worth doing: unreachable instruction
983 * warnings are of questionable value anyway, and this is such a rare issue.
985 * 3. mov [rodata addr],%reg1
986 * ... some instructions ...
987 * jmpq *(%reg1,%reg2,8)
989 * This is a fairly uncommon pattern which is new for GCC 6. As of this
990 * writing, there are 11 occurrences of it in the allmodconfig kernel.
992 * As of GCC 7 there are quite a few more of these and the 'in between' code
993 * is significant. Esp. with KASAN enabled some of the code between the mov
994 * and jmpq uses .rodata itself, which can confuse things.
996 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
997 * ensure the same register is used in the mov and jump instructions.
999 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1001 static struct rela
*find_switch_table(struct objtool_file
*file
,
1002 struct symbol
*func
,
1003 struct instruction
*insn
)
1005 struct rela
*text_rela
, *rodata_rela
;
1006 struct instruction
*orig_insn
= insn
;
1007 struct section
*rodata_sec
;
1008 unsigned long table_offset
;
1011 * Backward search using the @first_jump_src links, these help avoid
1012 * much of the 'in between' code. Which avoids us getting confused by
1016 &insn
->list
!= &file
->insn_list
&&
1017 insn
->sec
== func
->sec
&&
1018 insn
->offset
>= func
->offset
;
1020 insn
= insn
->first_jump_src
?: list_prev_entry(insn
, list
)) {
1022 if (insn
!= orig_insn
&& insn
->type
== INSN_JUMP_DYNAMIC
)
1025 /* allow small jumps within the range */
1026 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
1028 (insn
->jump_dest
->offset
<= insn
->offset
||
1029 insn
->jump_dest
->offset
> orig_insn
->offset
))
1032 /* look for a relocation which references .rodata */
1033 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
1035 if (!text_rela
|| text_rela
->sym
->type
!= STT_SECTION
||
1036 !text_rela
->sym
->sec
->rodata
)
1039 table_offset
= text_rela
->addend
;
1040 rodata_sec
= text_rela
->sym
->sec
;
1042 if (text_rela
->type
== R_X86_64_PC32
)
1046 * Make sure the .rodata address isn't associated with a
1047 * symbol. gcc jump tables are anonymous data.
1049 if (find_symbol_containing(rodata_sec
, table_offset
))
1052 rodata_rela
= find_rela_by_dest(rodata_sec
, table_offset
);
1055 * Use of RIP-relative switch jumps is quite rare, and
1056 * indicates a rare GCC quirk/bug which can leave dead
1059 if (text_rela
->type
== R_X86_64_PC32
)
1060 file
->ignore_unreachables
= true;
1070 static int add_func_switch_tables(struct objtool_file
*file
,
1071 struct symbol
*func
)
1073 struct instruction
*insn
, *last
= NULL
, *prev_jump
= NULL
;
1074 struct rela
*rela
, *prev_rela
= NULL
;
1077 func_for_each_insn_all(file
, func
, insn
) {
1082 * Store back-pointers for unconditional forward jumps such
1083 * that find_switch_table() can back-track using those and
1084 * avoid some potentially confusing code.
1086 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
&&
1087 insn
->offset
> last
->offset
&&
1088 insn
->jump_dest
->offset
> insn
->offset
&&
1089 !insn
->jump_dest
->first_jump_src
) {
1091 insn
->jump_dest
->first_jump_src
= insn
;
1092 last
= insn
->jump_dest
;
1095 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
1098 rela
= find_switch_table(file
, func
, insn
);
1103 * We found a switch table, but we don't know yet how big it
1104 * is. Don't add it until we reach the end of the function or
1105 * the beginning of another switch table in the same function.
1108 ret
= add_switch_table(file
, prev_jump
, prev_rela
, rela
);
1118 ret
= add_switch_table(file
, prev_jump
, prev_rela
, NULL
);
1127 * For some switch statements, gcc generates a jump table in the .rodata
1128 * section which contains a list of addresses within the function to jump to.
1129 * This finds these jump tables and adds them to the insn->alts lists.
1131 static int add_switch_table_alts(struct objtool_file
*file
)
1133 struct section
*sec
;
1134 struct symbol
*func
;
1140 for_each_sec(file
, sec
) {
1141 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1142 if (func
->type
!= STT_FUNC
)
1145 ret
= add_func_switch_tables(file
, func
);
1154 static int read_unwind_hints(struct objtool_file
*file
)
1156 struct section
*sec
, *relasec
;
1158 struct unwind_hint
*hint
;
1159 struct instruction
*insn
;
1160 struct cfi_reg
*cfa
;
1163 sec
= find_section_by_name(file
->elf
, ".discard.unwind_hints");
1167 relasec
= sec
->rela
;
1169 WARN("missing .rela.discard.unwind_hints section");
1173 if (sec
->len
% sizeof(struct unwind_hint
)) {
1174 WARN("struct unwind_hint size mismatch");
1180 for (i
= 0; i
< sec
->len
/ sizeof(struct unwind_hint
); i
++) {
1181 hint
= (struct unwind_hint
*)sec
->data
->d_buf
+ i
;
1183 rela
= find_rela_by_dest(sec
, i
* sizeof(*hint
));
1185 WARN("can't find rela for unwind_hints[%d]", i
);
1189 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
1191 WARN("can't find insn for unwind_hints[%d]", i
);
1195 cfa
= &insn
->state
.cfa
;
1197 if (hint
->type
== UNWIND_HINT_TYPE_SAVE
) {
1201 } else if (hint
->type
== UNWIND_HINT_TYPE_RESTORE
) {
1202 insn
->restore
= true;
1209 switch (hint
->sp_reg
) {
1210 case ORC_REG_UNDEFINED
:
1211 cfa
->base
= CFI_UNDEFINED
;
1219 case ORC_REG_SP_INDIRECT
:
1220 cfa
->base
= CFI_SP_INDIRECT
;
1223 cfa
->base
= CFI_R10
;
1226 cfa
->base
= CFI_R13
;
1235 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1236 insn
->sec
, insn
->offset
, hint
->sp_reg
);
1240 cfa
->offset
= hint
->sp_offset
;
1241 insn
->state
.type
= hint
->type
;
1242 insn
->state
.end
= hint
->end
;
1248 static int read_retpoline_hints(struct objtool_file
*file
)
1250 struct section
*sec
;
1251 struct instruction
*insn
;
1254 sec
= find_section_by_name(file
->elf
, ".rela.discard.retpoline_safe");
1258 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
1259 if (rela
->sym
->type
!= STT_SECTION
) {
1260 WARN("unexpected relocation symbol type in %s", sec
->name
);
1264 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
1266 WARN("bad .discard.retpoline_safe entry");
1270 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
1271 insn
->type
!= INSN_CALL_DYNAMIC
) {
1272 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1273 insn
->sec
, insn
->offset
);
1277 insn
->retpoline_safe
= true;
1283 static void mark_rodata(struct objtool_file
*file
)
1285 struct section
*sec
;
1289 * This searches for the .rodata section or multiple .rodata.func_name
1290 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1291 * rodata sections are ignored as they don't contain jump tables.
1293 for_each_sec(file
, sec
) {
1294 if (!strncmp(sec
->name
, ".rodata", 7) &&
1295 !strstr(sec
->name
, ".str1.")) {
1301 file
->rodata
= found
;
1304 static int decode_sections(struct objtool_file
*file
)
1310 ret
= decode_instructions(file
);
1314 ret
= add_dead_ends(file
);
1319 add_uaccess_safe(file
);
1321 ret
= add_ignore_alternatives(file
);
1325 ret
= add_jump_destinations(file
);
1329 ret
= add_special_section_alts(file
);
1333 ret
= add_call_destinations(file
);
1337 ret
= add_switch_table_alts(file
);
1341 ret
= read_unwind_hints(file
);
1345 ret
= read_retpoline_hints(file
);
1352 static bool is_fentry_call(struct instruction
*insn
)
1354 if (insn
->type
== INSN_CALL
&&
1355 insn
->call_dest
->type
== STT_NOTYPE
&&
1356 !strcmp(insn
->call_dest
->name
, "__fentry__"))
1362 static bool has_modified_stack_frame(struct insn_state
*state
)
1366 if (state
->cfa
.base
!= initial_func_cfi
.cfa
.base
||
1367 state
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
||
1368 state
->stack_size
!= initial_func_cfi
.cfa
.offset
||
1372 for (i
= 0; i
< CFI_NUM_REGS
; i
++)
1373 if (state
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
1374 state
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
1380 static bool has_valid_stack_frame(struct insn_state
*state
)
1382 if (state
->cfa
.base
== CFI_BP
&& state
->regs
[CFI_BP
].base
== CFI_CFA
&&
1383 state
->regs
[CFI_BP
].offset
== -16)
1386 if (state
->drap
&& state
->regs
[CFI_BP
].base
== CFI_BP
)
1392 static int update_insn_state_regs(struct instruction
*insn
, struct insn_state
*state
)
1394 struct cfi_reg
*cfa
= &state
->cfa
;
1395 struct stack_op
*op
= &insn
->stack_op
;
1397 if (cfa
->base
!= CFI_SP
)
1401 if (op
->dest
.type
== OP_DEST_PUSH
|| op
->dest
.type
== OP_DEST_PUSHF
)
1405 if (op
->src
.type
== OP_SRC_POP
|| op
->src
.type
== OP_SRC_POPF
)
1408 /* add immediate to sp */
1409 if (op
->dest
.type
== OP_DEST_REG
&& op
->src
.type
== OP_SRC_ADD
&&
1410 op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
)
1411 cfa
->offset
-= op
->src
.offset
;
1416 static void save_reg(struct insn_state
*state
, unsigned char reg
, int base
,
1419 if (arch_callee_saved_reg(reg
) &&
1420 state
->regs
[reg
].base
== CFI_UNDEFINED
) {
1421 state
->regs
[reg
].base
= base
;
1422 state
->regs
[reg
].offset
= offset
;
1426 static void restore_reg(struct insn_state
*state
, unsigned char reg
)
1428 state
->regs
[reg
].base
= CFI_UNDEFINED
;
1429 state
->regs
[reg
].offset
= 0;
1433 * A note about DRAP stack alignment:
1435 * GCC has the concept of a DRAP register, which is used to help keep track of
1436 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1437 * register. The typical DRAP pattern is:
1439 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1440 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1441 * 41 ff 72 f8 pushq -0x8(%r10)
1443 * 48 89 e5 mov %rsp,%rbp
1450 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1453 * There are some variations in the epilogues, like:
1461 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1466 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1467 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1468 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1469 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1471 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1474 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1475 * restored beforehand:
1478 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1479 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1481 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1485 static int update_insn_state(struct instruction
*insn
, struct insn_state
*state
)
1487 struct stack_op
*op
= &insn
->stack_op
;
1488 struct cfi_reg
*cfa
= &state
->cfa
;
1489 struct cfi_reg
*regs
= state
->regs
;
1491 /* stack operations don't make sense with an undefined CFA */
1492 if (cfa
->base
== CFI_UNDEFINED
) {
1494 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1500 if (state
->type
== ORC_TYPE_REGS
|| state
->type
== ORC_TYPE_REGS_IRET
)
1501 return update_insn_state_regs(insn
, state
);
1503 switch (op
->dest
.type
) {
1506 switch (op
->src
.type
) {
1509 if (op
->src
.reg
== CFI_SP
&& op
->dest
.reg
== CFI_BP
&&
1510 cfa
->base
== CFI_SP
&&
1511 regs
[CFI_BP
].base
== CFI_CFA
&&
1512 regs
[CFI_BP
].offset
== -cfa
->offset
) {
1514 /* mov %rsp, %rbp */
1515 cfa
->base
= op
->dest
.reg
;
1516 state
->bp_scratch
= false;
1519 else if (op
->src
.reg
== CFI_SP
&&
1520 op
->dest
.reg
== CFI_BP
&& state
->drap
) {
1522 /* drap: mov %rsp, %rbp */
1523 regs
[CFI_BP
].base
= CFI_BP
;
1524 regs
[CFI_BP
].offset
= -state
->stack_size
;
1525 state
->bp_scratch
= false;
1528 else if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1533 * This is needed for the rare case where GCC
1540 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1541 state
->vals
[op
->dest
.reg
].offset
= -state
->stack_size
;
1544 else if (op
->src
.reg
== CFI_BP
&& op
->dest
.reg
== CFI_SP
&&
1545 cfa
->base
== CFI_BP
) {
1550 * Restore the original stack pointer (Clang).
1552 state
->stack_size
= -state
->regs
[CFI_BP
].offset
;
1555 else if (op
->dest
.reg
== cfa
->base
) {
1557 /* mov %reg, %rsp */
1558 if (cfa
->base
== CFI_SP
&&
1559 state
->vals
[op
->src
.reg
].base
== CFI_CFA
) {
1562 * This is needed for the rare case
1563 * where GCC does something dumb like:
1565 * lea 0x8(%rsp), %rcx
1569 cfa
->offset
= -state
->vals
[op
->src
.reg
].offset
;
1570 state
->stack_size
= cfa
->offset
;
1573 cfa
->base
= CFI_UNDEFINED
;
1581 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
1584 state
->stack_size
-= op
->src
.offset
;
1585 if (cfa
->base
== CFI_SP
)
1586 cfa
->offset
-= op
->src
.offset
;
1590 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
1592 /* lea disp(%rbp), %rsp */
1593 state
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
1597 if (op
->src
.reg
== CFI_SP
&& cfa
->base
== CFI_SP
) {
1599 /* drap: lea disp(%rsp), %drap */
1600 state
->drap_reg
= op
->dest
.reg
;
1603 * lea disp(%rsp), %reg
1605 * This is needed for the rare case where GCC
1606 * does something dumb like:
1608 * lea 0x8(%rsp), %rcx
1612 state
->vals
[op
->dest
.reg
].base
= CFI_CFA
;
1613 state
->vals
[op
->dest
.reg
].offset
= \
1614 -state
->stack_size
+ op
->src
.offset
;
1619 if (state
->drap
&& op
->dest
.reg
== CFI_SP
&&
1620 op
->src
.reg
== state
->drap_reg
) {
1622 /* drap: lea disp(%drap), %rsp */
1624 cfa
->offset
= state
->stack_size
= -op
->src
.offset
;
1625 state
->drap_reg
= CFI_UNDEFINED
;
1626 state
->drap
= false;
1630 if (op
->dest
.reg
== state
->cfa
.base
) {
1631 WARN_FUNC("unsupported stack register modification",
1632 insn
->sec
, insn
->offset
);
1639 if (op
->dest
.reg
!= CFI_SP
||
1640 (state
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
1641 (state
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
1642 WARN_FUNC("unsupported stack pointer realignment",
1643 insn
->sec
, insn
->offset
);
1647 if (state
->drap_reg
!= CFI_UNDEFINED
) {
1648 /* drap: and imm, %rsp */
1649 cfa
->base
= state
->drap_reg
;
1650 cfa
->offset
= state
->stack_size
= 0;
1655 * Older versions of GCC (4.8ish) realign the stack
1656 * without DRAP, with a frame pointer.
1663 if (!state
->drap
&& op
->dest
.type
== OP_DEST_REG
&&
1664 op
->dest
.reg
== cfa
->base
) {
1670 if (state
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
1671 op
->dest
.type
== OP_DEST_REG
&&
1672 op
->dest
.reg
== state
->drap_reg
&&
1673 state
->drap_offset
== -state
->stack_size
) {
1675 /* drap: pop %drap */
1676 cfa
->base
= state
->drap_reg
;
1678 state
->drap_offset
= -1;
1680 } else if (regs
[op
->dest
.reg
].offset
== -state
->stack_size
) {
1683 restore_reg(state
, op
->dest
.reg
);
1686 state
->stack_size
-= 8;
1687 if (cfa
->base
== CFI_SP
)
1692 case OP_SRC_REG_INDIRECT
:
1693 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1694 op
->src
.offset
== state
->drap_offset
) {
1696 /* drap: mov disp(%rbp), %drap */
1697 cfa
->base
= state
->drap_reg
;
1699 state
->drap_offset
= -1;
1702 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1703 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
1705 /* drap: mov disp(%rbp), %reg */
1706 restore_reg(state
, op
->dest
.reg
);
1708 } else if (op
->src
.reg
== cfa
->base
&&
1709 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
1711 /* mov disp(%rbp), %reg */
1712 /* mov disp(%rsp), %reg */
1713 restore_reg(state
, op
->dest
.reg
);
1719 WARN_FUNC("unknown stack-related instruction",
1720 insn
->sec
, insn
->offset
);
1728 state
->stack_size
+= 8;
1729 if (cfa
->base
== CFI_SP
)
1732 if (op
->src
.type
!= OP_SRC_REG
)
1736 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1738 /* drap: push %drap */
1739 cfa
->base
= CFI_BP_INDIRECT
;
1740 cfa
->offset
= -state
->stack_size
;
1742 /* save drap so we know when to restore it */
1743 state
->drap_offset
= -state
->stack_size
;
1745 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== state
->drap_reg
) {
1747 /* drap: push %rbp */
1748 state
->stack_size
= 0;
1750 } else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1752 /* drap: push %reg */
1753 save_reg(state
, op
->src
.reg
, CFI_BP
, -state
->stack_size
);
1759 save_reg(state
, op
->src
.reg
, CFI_CFA
, -state
->stack_size
);
1762 /* detect when asm code uses rbp as a scratch register */
1763 if (!no_fp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
1764 cfa
->base
!= CFI_BP
)
1765 state
->bp_scratch
= true;
1768 case OP_DEST_REG_INDIRECT
:
1771 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1773 /* drap: mov %drap, disp(%rbp) */
1774 cfa
->base
= CFI_BP_INDIRECT
;
1775 cfa
->offset
= op
->dest
.offset
;
1777 /* save drap offset so we know when to restore it */
1778 state
->drap_offset
= op
->dest
.offset
;
1781 else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1783 /* drap: mov reg, disp(%rbp) */
1784 save_reg(state
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
1787 } else if (op
->dest
.reg
== cfa
->base
) {
1789 /* mov reg, disp(%rbp) */
1790 /* mov reg, disp(%rsp) */
1791 save_reg(state
, op
->src
.reg
, CFI_CFA
,
1792 op
->dest
.offset
- state
->cfa
.offset
);
1798 if ((!state
->drap
&& cfa
->base
!= CFI_BP
) ||
1799 (state
->drap
&& cfa
->base
!= state
->drap_reg
)) {
1800 WARN_FUNC("leave instruction with modified stack frame",
1801 insn
->sec
, insn
->offset
);
1805 /* leave (mov %rbp, %rsp; pop %rbp) */
1807 state
->stack_size
= -state
->regs
[CFI_BP
].offset
- 8;
1808 restore_reg(state
, CFI_BP
);
1818 if (op
->src
.type
!= OP_SRC_POP
&& op
->src
.type
!= OP_SRC_POPF
) {
1819 WARN_FUNC("unknown stack-related memory operation",
1820 insn
->sec
, insn
->offset
);
1825 state
->stack_size
-= 8;
1826 if (cfa
->base
== CFI_SP
)
1832 WARN_FUNC("unknown stack-related instruction",
1833 insn
->sec
, insn
->offset
);
1840 static bool insn_state_match(struct instruction
*insn
, struct insn_state
*state
)
1842 struct insn_state
*state1
= &insn
->state
, *state2
= state
;
1845 if (memcmp(&state1
->cfa
, &state2
->cfa
, sizeof(state1
->cfa
))) {
1846 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1847 insn
->sec
, insn
->offset
,
1848 state1
->cfa
.base
, state1
->cfa
.offset
,
1849 state2
->cfa
.base
, state2
->cfa
.offset
);
1851 } else if (memcmp(&state1
->regs
, &state2
->regs
, sizeof(state1
->regs
))) {
1852 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1853 if (!memcmp(&state1
->regs
[i
], &state2
->regs
[i
],
1854 sizeof(struct cfi_reg
)))
1857 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1858 insn
->sec
, insn
->offset
,
1859 i
, state1
->regs
[i
].base
, state1
->regs
[i
].offset
,
1860 i
, state2
->regs
[i
].base
, state2
->regs
[i
].offset
);
1864 } else if (state1
->type
!= state2
->type
) {
1865 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1866 insn
->sec
, insn
->offset
, state1
->type
, state2
->type
);
1868 } else if (state1
->drap
!= state2
->drap
||
1869 (state1
->drap
&& state1
->drap_reg
!= state2
->drap_reg
) ||
1870 (state1
->drap
&& state1
->drap_offset
!= state2
->drap_offset
)) {
1871 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1872 insn
->sec
, insn
->offset
,
1873 state1
->drap
, state1
->drap_reg
, state1
->drap_offset
,
1874 state2
->drap
, state2
->drap_reg
, state2
->drap_offset
);
1882 static inline bool func_uaccess_safe(struct symbol
*func
)
1885 return func
->alias
->uaccess_safe
;
1890 static inline const char *insn_dest_name(struct instruction
*insn
)
1892 if (insn
->call_dest
)
1893 return insn
->call_dest
->name
;
1898 static int validate_call(struct instruction
*insn
, struct insn_state
*state
)
1900 if (state
->uaccess
&& !func_uaccess_safe(insn
->call_dest
)) {
1901 WARN_FUNC("call to %s() with UACCESS enabled",
1902 insn
->sec
, insn
->offset
, insn_dest_name(insn
));
1909 static int validate_sibling_call(struct instruction
*insn
, struct insn_state
*state
)
1911 if (has_modified_stack_frame(state
)) {
1912 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1913 insn
->sec
, insn
->offset
);
1917 return validate_call(insn
, state
);
1921 * Follow the branch starting at the given instruction, and recursively follow
1922 * any other branches (jumps). Meanwhile, track the frame pointer state at
1923 * each instruction and validate all the rules described in
1924 * tools/objtool/Documentation/stack-validation.txt.
1926 static int validate_branch(struct objtool_file
*file
, struct instruction
*first
,
1927 struct insn_state state
)
1929 struct alternative
*alt
;
1930 struct instruction
*insn
, *next_insn
;
1931 struct section
*sec
;
1932 struct symbol
*func
= NULL
;
1938 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
1939 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1945 next_insn
= next_insn_same_sec(file
, insn
);
1947 if (file
->c_file
&& func
&& insn
->func
&& func
!= insn
->func
->pfunc
) {
1948 WARN("%s() falls through to next function %s()",
1949 func
->name
, insn
->func
->name
);
1953 func
= insn
->func
? insn
->func
->pfunc
: NULL
;
1955 if (func
&& insn
->ignore
) {
1956 WARN_FUNC("BUG: why am I validating an ignored function?",
1961 if (insn
->visited
) {
1962 if (!insn
->hint
&& !insn_state_match(insn
, &state
))
1965 /* If we were here with AC=0, but now have AC=1, go again */
1966 if (insn
->state
.uaccess
|| !state
.uaccess
)
1971 if (insn
->restore
) {
1972 struct instruction
*save_insn
, *i
;
1976 func_for_each_insn_continue_reverse(file
, insn
->func
, i
) {
1984 WARN_FUNC("no corresponding CFI save for CFI restore",
1989 if (!save_insn
->visited
) {
1991 * Oops, no state to copy yet.
1992 * Hopefully we can reach this
1993 * instruction from another branch
1994 * after the save insn has been
2000 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2005 insn
->state
= save_insn
->state
;
2008 state
= insn
->state
;
2011 insn
->state
= state
;
2013 insn
->visited
= true;
2015 if (!insn
->ignore_alts
) {
2016 bool skip_orig
= false;
2018 list_for_each_entry(alt
, &insn
->alts
, list
) {
2022 ret
= validate_branch(file
, alt
->insn
, state
);
2025 BT_FUNC("(alt)", insn
);
2034 switch (insn
->type
) {
2037 if (state
.uaccess
&& !func_uaccess_safe(func
)) {
2038 WARN_FUNC("return with UACCESS enabled", sec
, insn
->offset
);
2042 if (!state
.uaccess
&& func_uaccess_safe(func
)) {
2043 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec
, insn
->offset
);
2047 if (func
&& has_modified_stack_frame(&state
)) {
2048 WARN_FUNC("return with modified stack frame",
2053 if (state
.bp_scratch
) {
2054 WARN("%s uses BP as a scratch register",
2062 case INSN_CALL_DYNAMIC
:
2063 ret
= validate_call(insn
, &state
);
2067 if (insn
->type
== INSN_CALL
) {
2068 if (is_fentry_call(insn
))
2071 ret
= dead_end_function(file
, insn
->call_dest
);
2078 if (!no_fp
&& func
&& !has_valid_stack_frame(&state
)) {
2079 WARN_FUNC("call without frame pointer save/setup",
2085 case INSN_JUMP_CONDITIONAL
:
2086 case INSN_JUMP_UNCONDITIONAL
:
2087 if (func
&& !insn
->jump_dest
) {
2088 ret
= validate_sibling_call(insn
, &state
);
2092 } else if (insn
->jump_dest
&&
2093 (!func
|| !insn
->jump_dest
->func
||
2094 insn
->jump_dest
->func
->pfunc
== func
)) {
2095 ret
= validate_branch(file
, insn
->jump_dest
,
2099 BT_FUNC("(branch)", insn
);
2104 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
2109 case INSN_JUMP_DYNAMIC
:
2110 if (func
&& list_empty(&insn
->alts
)) {
2111 ret
= validate_sibling_call(insn
, &state
);
2118 case INSN_CONTEXT_SWITCH
:
2119 if (func
&& (!next_insn
|| !next_insn
->hint
)) {
2120 WARN_FUNC("unsupported instruction in callable function",
2127 if (update_insn_state(insn
, &state
))
2130 if (insn
->stack_op
.dest
.type
== OP_DEST_PUSHF
) {
2131 if (!state
.uaccess_stack
) {
2132 state
.uaccess_stack
= 1;
2133 } else if (state
.uaccess_stack
>> 31) {
2134 WARN_FUNC("PUSHF stack exhausted", sec
, insn
->offset
);
2137 state
.uaccess_stack
<<= 1;
2138 state
.uaccess_stack
|= state
.uaccess
;
2141 if (insn
->stack_op
.src
.type
== OP_SRC_POPF
) {
2142 if (state
.uaccess_stack
) {
2143 state
.uaccess
= state
.uaccess_stack
& 1;
2144 state
.uaccess_stack
>>= 1;
2145 if (state
.uaccess_stack
== 1)
2146 state
.uaccess_stack
= 0;
2153 if (state
.uaccess
) {
2154 WARN_FUNC("recursive UACCESS enable", sec
, insn
->offset
);
2158 state
.uaccess
= true;
2162 if (!state
.uaccess
&& insn
->func
) {
2163 WARN_FUNC("redundant UACCESS disable", sec
, insn
->offset
);
2167 if (func_uaccess_safe(func
) && !state
.uaccess_stack
) {
2168 WARN_FUNC("UACCESS-safe disables UACCESS", sec
, insn
->offset
);
2172 state
.uaccess
= false;
2183 if (state
.cfa
.base
== CFI_UNDEFINED
)
2185 WARN("%s: unexpected end of section", sec
->name
);
2195 static int validate_unwind_hints(struct objtool_file
*file
)
2197 struct instruction
*insn
;
2198 int ret
, warnings
= 0;
2199 struct insn_state state
;
2204 clear_insn_state(&state
);
2206 for_each_insn(file
, insn
) {
2207 if (insn
->hint
&& !insn
->visited
) {
2208 ret
= validate_branch(file
, insn
, state
);
2209 if (ret
&& backtrace
)
2210 BT_FUNC("<=== (hint)", insn
);
2218 static int validate_retpoline(struct objtool_file
*file
)
2220 struct instruction
*insn
;
2223 for_each_insn(file
, insn
) {
2224 if (insn
->type
!= INSN_JUMP_DYNAMIC
&&
2225 insn
->type
!= INSN_CALL_DYNAMIC
)
2228 if (insn
->retpoline_safe
)
2232 * .init.text code is ran before userspace and thus doesn't
2233 * strictly need retpolines, except for modules which are
2234 * loaded late, they very much do need retpoline in their
2237 if (!strcmp(insn
->sec
->name
, ".init.text") && !module
)
2240 WARN_FUNC("indirect %s found in RETPOLINE build",
2241 insn
->sec
, insn
->offset
,
2242 insn
->type
== INSN_JUMP_DYNAMIC
? "jump" : "call");
2250 static bool is_kasan_insn(struct instruction
*insn
)
2252 return (insn
->type
== INSN_CALL
&&
2253 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
2256 static bool is_ubsan_insn(struct instruction
*insn
)
2258 return (insn
->type
== INSN_CALL
&&
2259 !strcmp(insn
->call_dest
->name
,
2260 "__ubsan_handle_builtin_unreachable"));
2263 static bool ignore_unreachable_insn(struct instruction
*insn
)
2267 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
2271 * Ignore any unused exceptions. This can happen when a whitelisted
2272 * function has an exception table entry.
2274 * Also ignore alternative replacement instructions. This can happen
2275 * when a whitelisted function uses one of the ALTERNATIVE macros.
2277 if (!strcmp(insn
->sec
->name
, ".fixup") ||
2278 !strcmp(insn
->sec
->name
, ".altinstr_replacement") ||
2279 !strcmp(insn
->sec
->name
, ".altinstr_aux"))
2283 * Check if this (or a subsequent) instruction is related to
2284 * CONFIG_UBSAN or CONFIG_KASAN.
2286 * End the search at 5 instructions to avoid going into the weeds.
2290 for (i
= 0; i
< 5; i
++) {
2292 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
2295 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
2296 if (insn
->jump_dest
&&
2297 insn
->jump_dest
->func
== insn
->func
) {
2298 insn
= insn
->jump_dest
;
2305 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
2308 insn
= list_next_entry(insn
, list
);
2314 static int validate_functions(struct objtool_file
*file
)
2316 struct section
*sec
;
2317 struct symbol
*func
;
2318 struct instruction
*insn
;
2319 struct insn_state state
;
2320 int ret
, warnings
= 0;
2322 clear_insn_state(&state
);
2324 state
.cfa
= initial_func_cfi
.cfa
;
2325 memcpy(&state
.regs
, &initial_func_cfi
.regs
,
2326 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
2327 state
.stack_size
= initial_func_cfi
.cfa
.offset
;
2329 for_each_sec(file
, sec
) {
2330 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
2331 if (func
->type
!= STT_FUNC
|| func
->pfunc
!= func
)
2334 insn
= find_insn(file
, sec
, func
->offset
);
2335 if (!insn
|| insn
->ignore
)
2338 state
.uaccess
= func
->alias
->uaccess_safe
;
2340 ret
= validate_branch(file
, insn
, state
);
2341 if (ret
&& backtrace
)
2342 BT_FUNC("<=== (func)", insn
);
2350 static int validate_reachable_instructions(struct objtool_file
*file
)
2352 struct instruction
*insn
;
2354 if (file
->ignore_unreachables
)
2357 for_each_insn(file
, insn
) {
2358 if (insn
->visited
|| ignore_unreachable_insn(insn
))
2361 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
2368 static void cleanup(struct objtool_file
*file
)
2370 struct instruction
*insn
, *tmpinsn
;
2371 struct alternative
*alt
, *tmpalt
;
2373 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
2374 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
2375 list_del(&alt
->list
);
2378 list_del(&insn
->list
);
2379 hash_del(&insn
->hash
);
2382 elf_close(file
->elf
);
2385 static struct objtool_file file
;
2387 int check(const char *_objname
, bool orc
)
2389 int ret
, warnings
= 0;
2393 file
.elf
= elf_open(objname
, orc
? O_RDWR
: O_RDONLY
);
2397 INIT_LIST_HEAD(&file
.insn_list
);
2398 hash_init(file
.insn_hash
);
2399 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
2400 file
.ignore_unreachables
= no_unreachable
;
2403 arch_initial_func_cfi_state(&initial_func_cfi
);
2405 ret
= decode_sections(&file
);
2410 if (list_empty(&file
.insn_list
))
2414 ret
= validate_retpoline(&file
);
2420 ret
= validate_functions(&file
);
2425 ret
= validate_unwind_hints(&file
);
2431 ret
= validate_reachable_instructions(&file
);
2438 ret
= create_orc(&file
);
2442 ret
= create_orc_sections(&file
);
2446 ret
= elf_write(file
.elf
);
2454 /* ignore warnings for now until we get all the code cleaned up */
2455 if (ret
|| warnings
)