1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
11 #include <objtool/builtin.h>
12 #include <objtool/cfi.h>
13 #include <objtool/arch.h>
14 #include <objtool/check.h>
15 #include <objtool/special.h>
16 #include <objtool/warn.h>
17 #include <objtool/endianness.h>
19 #include <linux/objtool.h>
20 #include <linux/hashtable.h>
21 #include <linux/kernel.h>
22 #include <linux/static_call_types.h>
25 struct list_head list;
26 struct instruction *insn;
30 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
32 static struct cfi_init_state initial_func_cfi;
33 static struct cfi_state init_cfi;
34 static struct cfi_state func_cfi;
36 struct instruction *find_insn(struct objtool_file *file,
37 struct section *sec, unsigned long offset)
39 struct instruction *insn;
41 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
42 if (insn->sec == sec && insn->offset == offset)
49 static struct instruction *next_insn_same_sec(struct objtool_file *file,
50 struct instruction *insn)
52 struct instruction *next = list_next_entry(insn, list);
54 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
60 static struct instruction *next_insn_same_func(struct objtool_file *file,
61 struct instruction *insn)
63 struct instruction *next = list_next_entry(insn, list);
64 struct symbol *func = insn->func;
69 if (&next->list != &file->insn_list && next->func == func)
72 /* Check if we're already in the subfunction: */
73 if (func == func->cfunc)
76 /* Move to the subfunction: */
77 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
80 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
81 struct instruction *insn)
83 struct instruction *prev = list_prev_entry(insn, list);
85 if (&prev->list != &file->insn_list && prev->func == insn->func)
91 #define func_for_each_insn(file, func, insn) \
92 for (insn = find_insn(file, func->sec, func->offset); \
94 insn = next_insn_same_func(file, insn))
96 #define sym_for_each_insn(file, sym, insn) \
97 for (insn = find_insn(file, sym->sec, sym->offset); \
98 insn && &insn->list != &file->insn_list && \
99 insn->sec == sym->sec && \
100 insn->offset < sym->offset + sym->len; \
101 insn = list_next_entry(insn, list))
103 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
104 for (insn = list_prev_entry(insn, list); \
105 &insn->list != &file->insn_list && \
106 insn->sec == sym->sec && insn->offset >= sym->offset; \
107 insn = list_prev_entry(insn, list))
109 #define sec_for_each_insn_from(file, insn) \
110 for (; insn; insn = next_insn_same_sec(file, insn))
112 #define sec_for_each_insn_continue(file, insn) \
113 for (insn = next_insn_same_sec(file, insn); insn; \
114 insn = next_insn_same_sec(file, insn))
116 static bool is_jump_table_jump(struct instruction *insn)
118 struct alt_group *alt_group = insn->alt_group;
120 if (insn->jump_table)
123 /* Retpoline alternative for a jump table? */
124 return alt_group && alt_group->orig_group &&
125 alt_group->orig_group->first_insn->jump_table;
128 static bool is_sibling_call(struct instruction *insn)
131 * Assume only ELF functions can make sibling calls. This ensures
132 * sibling call detection consistency between vmlinux.o and individual
138 /* An indirect jump is either a sibling call or a jump to a table. */
139 if (insn->type == INSN_JUMP_DYNAMIC)
140 return !is_jump_table_jump(insn);
142 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
143 return (is_static_jump(insn) && insn->call_dest);
147 * This checks to see if the given function is a "noreturn" function.
149 * For global functions which are outside the scope of this object file, we
150 * have to keep a manual list of them.
152 * For local functions, we have to detect them manually by simply looking for
153 * the lack of a return instruction.
155 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
159 struct instruction *insn;
163 * Unfortunately these have to be hard coded because the noreturn
164 * attribute isn't provided in ELF data.
166 static const char * const global_noreturns[] = {
171 "__module_put_and_exit",
177 "machine_real_restart",
178 "rewind_stack_do_exit",
179 "kunit_try_catch_throw",
181 "cpu_bringup_and_idle",
187 if (func->bind == STB_WEAK)
190 if (func->bind == STB_GLOBAL)
191 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
192 if (!strcmp(func->name, global_noreturns[i]))
198 insn = find_insn(file, func->sec, func->offset);
202 func_for_each_insn(file, func, insn) {
205 if (insn->type == INSN_RETURN)
213 * A function can have a sibling call instead of a return. In that
214 * case, the function's dead-end status depends on whether the target
215 * of the sibling call returns.
217 func_for_each_insn(file, func, insn) {
218 if (is_sibling_call(insn)) {
219 struct instruction *dest = insn->jump_dest;
222 /* sibling call to another file */
225 /* local sibling call */
226 if (recursion == 5) {
228 * Infinite recursion: two functions have
229 * sibling calls to each other. This is a very
230 * rare case. It means they aren't dead ends.
235 return __dead_end_function(file, dest->func, recursion+1);
242 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
244 return __dead_end_function(file, func, 0);
247 static void init_cfi_state(struct cfi_state *cfi)
251 for (i = 0; i < CFI_NUM_REGS; i++) {
252 cfi->regs[i].base = CFI_UNDEFINED;
253 cfi->vals[i].base = CFI_UNDEFINED;
255 cfi->cfa.base = CFI_UNDEFINED;
256 cfi->drap_reg = CFI_UNDEFINED;
257 cfi->drap_offset = -1;
260 static void init_insn_state(struct insn_state *state, struct section *sec)
262 memset(state, 0, sizeof(*state));
263 init_cfi_state(&state->cfi);
266 * We need the full vmlinux for noinstr validation, otherwise we can
267 * not correctly determine insn->call_dest->sec (external symbols do
268 * not have a section).
270 if (vmlinux && noinstr && sec)
271 state->noinstr = sec->noinstr;
274 static struct cfi_state *cfi_alloc(void)
276 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
278 WARN("calloc failed");
286 static struct hlist_head *cfi_hash;
288 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
290 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
291 (void *)cfi2 + sizeof(cfi2->hash),
292 sizeof(struct cfi_state) - sizeof(struct hlist_node));
295 static inline u32 cfi_key(struct cfi_state *cfi)
297 return jhash((void *)cfi + sizeof(cfi->hash),
298 sizeof(*cfi) - sizeof(cfi->hash), 0);
301 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
303 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
304 struct cfi_state *obj;
306 hlist_for_each_entry(obj, head, hash) {
307 if (!cficmp(cfi, obj)) {
315 hlist_add_head(&obj->hash, head);
320 static void cfi_hash_add(struct cfi_state *cfi)
322 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
324 hlist_add_head(&cfi->hash, head);
327 static void *cfi_hash_alloc(unsigned long size)
329 cfi_bits = max(10, ilog2(size));
330 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
331 PROT_READ|PROT_WRITE,
332 MAP_PRIVATE|MAP_ANON, -1, 0);
333 if (cfi_hash == (void *)-1L) {
334 WARN("mmap fail cfi_hash");
337 printf("cfi_bits: %d\n", cfi_bits);
343 static unsigned long nr_insns;
344 static unsigned long nr_insns_visited;
347 * Call the arch-specific instruction decoder for all the instructions and add
348 * them to the global instruction list.
350 static int decode_instructions(struct objtool_file *file)
354 unsigned long offset;
355 struct instruction *insn;
358 for_each_sec(file, sec) {
360 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
363 if (strcmp(sec->name, ".altinstr_replacement") &&
364 strcmp(sec->name, ".altinstr_aux") &&
365 strncmp(sec->name, ".discard.", 9))
368 if (!strcmp(sec->name, ".noinstr.text") ||
369 !strcmp(sec->name, ".entry.text"))
372 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
373 insn = malloc(sizeof(*insn));
375 WARN("malloc failed");
378 memset(insn, 0, sizeof(*insn));
379 INIT_LIST_HEAD(&insn->alts);
380 INIT_LIST_HEAD(&insn->stack_ops);
383 insn->offset = offset;
385 ret = arch_decode_instruction(file, sec, offset,
386 sec->sh.sh_size - offset,
387 &insn->len, &insn->type,
393 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
394 list_add_tail(&insn->list, &file->insn_list);
398 list_for_each_entry(func, &sec->symbol_list, list) {
399 if (func->type != STT_FUNC || func->alias != func)
402 if (!find_insn(file, sec, func->offset)) {
403 WARN("%s(): can't find starting instruction",
408 sym_for_each_insn(file, func, insn)
414 printf("nr_insns: %lu\n", nr_insns);
424 * Read the pv_ops[] .data table to find the static initialized values.
426 static int add_pv_ops(struct objtool_file *file, const char *symname)
428 struct symbol *sym, *func;
429 unsigned long off, end;
433 sym = find_symbol_by_name(file->elf, symname);
438 end = off + sym->len;
440 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
445 if (func->type == STT_SECTION)
446 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
448 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
450 objtool_pv_add(file, idx, func);
452 off = rel->offset + 1;
461 * Allocate and initialize file->pv_ops[].
463 static int init_pv_ops(struct objtool_file *file)
465 static const char *pv_ops_tables[] = {
481 sym = find_symbol_by_name(file->elf, "pv_ops");
485 nr = sym->len / sizeof(unsigned long);
486 file->pv_ops = calloc(sizeof(struct pv_state), nr);
490 for (idx = 0; idx < nr; idx++)
491 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
493 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
494 add_pv_ops(file, pv_ops);
499 static struct instruction *find_last_insn(struct objtool_file *file,
502 struct instruction *insn = NULL;
504 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
506 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
507 insn = find_insn(file, sec, offset);
513 * Mark "ud2" instructions and manually annotated dead ends.
515 static int add_dead_ends(struct objtool_file *file)
519 struct instruction *insn;
522 * By default, "ud2" is a dead end unless otherwise annotated, because
523 * GCC 7 inserts it for certain divide-by-zero cases.
525 for_each_insn(file, insn)
526 if (insn->type == INSN_BUG)
527 insn->dead_end = true;
530 * Check for manually annotated dead ends.
532 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
536 list_for_each_entry(reloc, &sec->reloc_list, list) {
537 if (reloc->sym->type != STT_SECTION) {
538 WARN("unexpected relocation symbol type in %s", sec->name);
541 insn = find_insn(file, reloc->sym->sec, reloc->addend);
543 insn = list_prev_entry(insn, list);
544 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
545 insn = find_last_insn(file, reloc->sym->sec);
547 WARN("can't find unreachable insn at %s+0x%x",
548 reloc->sym->sec->name, reloc->addend);
552 WARN("can't find unreachable insn at %s+0x%x",
553 reloc->sym->sec->name, reloc->addend);
557 insn->dead_end = true;
562 * These manually annotated reachable checks are needed for GCC 4.4,
563 * where the Linux unreachable() macro isn't supported. In that case
564 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
567 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
571 list_for_each_entry(reloc, &sec->reloc_list, list) {
572 if (reloc->sym->type != STT_SECTION) {
573 WARN("unexpected relocation symbol type in %s", sec->name);
576 insn = find_insn(file, reloc->sym->sec, reloc->addend);
578 insn = list_prev_entry(insn, list);
579 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
580 insn = find_last_insn(file, reloc->sym->sec);
582 WARN("can't find reachable insn at %s+0x%x",
583 reloc->sym->sec->name, reloc->addend);
587 WARN("can't find reachable insn at %s+0x%x",
588 reloc->sym->sec->name, reloc->addend);
592 insn->dead_end = false;
598 static int create_static_call_sections(struct objtool_file *file)
601 struct static_call_site *site;
602 struct instruction *insn;
603 struct symbol *key_sym;
604 char *key_name, *tmp;
607 sec = find_section_by_name(file->elf, ".static_call_sites");
609 INIT_LIST_HEAD(&file->static_call_list);
610 WARN("file already has .static_call_sites section, skipping");
614 if (list_empty(&file->static_call_list))
618 list_for_each_entry(insn, &file->static_call_list, call_node)
621 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
622 sizeof(struct static_call_site), idx);
627 list_for_each_entry(insn, &file->static_call_list, call_node) {
629 site = (struct static_call_site *)sec->data->d_buf + idx;
630 memset(site, 0, sizeof(struct static_call_site));
632 /* populate reloc for 'addr' */
633 if (elf_add_reloc_to_insn(file->elf, sec,
634 idx * sizeof(struct static_call_site),
636 insn->sec, insn->offset))
639 /* find key symbol */
640 key_name = strdup(insn->call_dest->name);
645 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
646 STATIC_CALL_TRAMP_PREFIX_LEN)) {
647 WARN("static_call: trampoline name malformed: %s", key_name);
650 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
651 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
653 key_sym = find_symbol_by_name(file->elf, tmp);
656 WARN("static_call: can't find static_call_key symbol: %s", tmp);
661 * For modules(), the key might not be exported, which
662 * means the module can make static calls but isn't
663 * allowed to change them.
665 * In that case we temporarily set the key to be the
666 * trampoline address. This is fixed up in
667 * static_call_add_module().
669 key_sym = insn->call_dest;
673 /* populate reloc for 'key' */
674 if (elf_add_reloc(file->elf, sec,
675 idx * sizeof(struct static_call_site) + 4,
676 R_X86_64_PC32, key_sym,
677 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
686 static int create_retpoline_sites_sections(struct objtool_file *file)
688 struct instruction *insn;
692 sec = find_section_by_name(file->elf, ".retpoline_sites");
694 WARN("file already has .retpoline_sites, skipping");
699 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
705 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
708 WARN("elf_create_section: .retpoline_sites");
713 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
715 int *site = (int *)sec->data->d_buf + idx;
718 if (elf_add_reloc_to_insn(file->elf, sec,
721 insn->sec, insn->offset)) {
722 WARN("elf_add_reloc_to_insn: .retpoline_sites");
732 static int create_mcount_loc_sections(struct objtool_file *file)
736 struct instruction *insn;
739 sec = find_section_by_name(file->elf, "__mcount_loc");
741 INIT_LIST_HEAD(&file->mcount_loc_list);
742 WARN("file already has __mcount_loc section, skipping");
746 if (list_empty(&file->mcount_loc_list))
750 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
753 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
758 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
760 loc = (unsigned long *)sec->data->d_buf + idx;
761 memset(loc, 0, sizeof(unsigned long));
763 if (elf_add_reloc_to_insn(file->elf, sec,
764 idx * sizeof(unsigned long),
766 insn->sec, insn->offset))
776 * Warnings shouldn't be reported for ignored functions.
778 static void add_ignores(struct objtool_file *file)
780 struct instruction *insn;
785 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
789 list_for_each_entry(reloc, &sec->reloc_list, list) {
790 switch (reloc->sym->type) {
796 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
802 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
806 func_for_each_insn(file, func, insn)
812 * This is a whitelist of functions that is allowed to be called with AC set.
813 * The list is meant to be minimal and only contains compiler instrumentation
814 * ABI and a few functions used to implement *_{to,from}_user() functions.
816 * These functions must not directly change AC, but may PUSHF/POPF.
818 static const char *uaccess_safe_builtin[] = {
822 /* KASAN out-of-line */
823 "__asan_loadN_noabort",
824 "__asan_load1_noabort",
825 "__asan_load2_noabort",
826 "__asan_load4_noabort",
827 "__asan_load8_noabort",
828 "__asan_load16_noabort",
829 "__asan_storeN_noabort",
830 "__asan_store1_noabort",
831 "__asan_store2_noabort",
832 "__asan_store4_noabort",
833 "__asan_store8_noabort",
834 "__asan_store16_noabort",
835 "__kasan_check_read",
836 "__kasan_check_write",
838 "__asan_report_load_n_noabort",
839 "__asan_report_load1_noabort",
840 "__asan_report_load2_noabort",
841 "__asan_report_load4_noabort",
842 "__asan_report_load8_noabort",
843 "__asan_report_load16_noabort",
844 "__asan_report_store_n_noabort",
845 "__asan_report_store1_noabort",
846 "__asan_report_store2_noabort",
847 "__asan_report_store4_noabort",
848 "__asan_report_store8_noabort",
849 "__asan_report_store16_noabort",
851 "__kcsan_check_access",
852 "kcsan_found_watchpoint",
853 "kcsan_setup_watchpoint",
854 "kcsan_check_scoped_accesses",
855 "kcsan_disable_current",
856 "kcsan_enable_current_nowarn",
861 "__tsan_write_range",
872 "__tsan_read_write1",
873 "__tsan_read_write2",
874 "__tsan_read_write4",
875 "__tsan_read_write8",
876 "__tsan_read_write16",
877 "__tsan_atomic8_load",
878 "__tsan_atomic16_load",
879 "__tsan_atomic32_load",
880 "__tsan_atomic64_load",
881 "__tsan_atomic8_store",
882 "__tsan_atomic16_store",
883 "__tsan_atomic32_store",
884 "__tsan_atomic64_store",
885 "__tsan_atomic8_exchange",
886 "__tsan_atomic16_exchange",
887 "__tsan_atomic32_exchange",
888 "__tsan_atomic64_exchange",
889 "__tsan_atomic8_fetch_add",
890 "__tsan_atomic16_fetch_add",
891 "__tsan_atomic32_fetch_add",
892 "__tsan_atomic64_fetch_add",
893 "__tsan_atomic8_fetch_sub",
894 "__tsan_atomic16_fetch_sub",
895 "__tsan_atomic32_fetch_sub",
896 "__tsan_atomic64_fetch_sub",
897 "__tsan_atomic8_fetch_and",
898 "__tsan_atomic16_fetch_and",
899 "__tsan_atomic32_fetch_and",
900 "__tsan_atomic64_fetch_and",
901 "__tsan_atomic8_fetch_or",
902 "__tsan_atomic16_fetch_or",
903 "__tsan_atomic32_fetch_or",
904 "__tsan_atomic64_fetch_or",
905 "__tsan_atomic8_fetch_xor",
906 "__tsan_atomic16_fetch_xor",
907 "__tsan_atomic32_fetch_xor",
908 "__tsan_atomic64_fetch_xor",
909 "__tsan_atomic8_fetch_nand",
910 "__tsan_atomic16_fetch_nand",
911 "__tsan_atomic32_fetch_nand",
912 "__tsan_atomic64_fetch_nand",
913 "__tsan_atomic8_compare_exchange_strong",
914 "__tsan_atomic16_compare_exchange_strong",
915 "__tsan_atomic32_compare_exchange_strong",
916 "__tsan_atomic64_compare_exchange_strong",
917 "__tsan_atomic8_compare_exchange_weak",
918 "__tsan_atomic16_compare_exchange_weak",
919 "__tsan_atomic32_compare_exchange_weak",
920 "__tsan_atomic64_compare_exchange_weak",
921 "__tsan_atomic8_compare_exchange_val",
922 "__tsan_atomic16_compare_exchange_val",
923 "__tsan_atomic32_compare_exchange_val",
924 "__tsan_atomic64_compare_exchange_val",
925 "__tsan_atomic_thread_fence",
926 "__tsan_atomic_signal_fence",
930 "__sanitizer_cov_trace_pc",
931 "__sanitizer_cov_trace_const_cmp1",
932 "__sanitizer_cov_trace_const_cmp2",
933 "__sanitizer_cov_trace_const_cmp4",
934 "__sanitizer_cov_trace_const_cmp8",
935 "__sanitizer_cov_trace_cmp1",
936 "__sanitizer_cov_trace_cmp2",
937 "__sanitizer_cov_trace_cmp4",
938 "__sanitizer_cov_trace_cmp8",
939 "__sanitizer_cov_trace_switch",
941 "ubsan_type_mismatch_common",
942 "__ubsan_handle_type_mismatch",
943 "__ubsan_handle_type_mismatch_v1",
944 "__ubsan_handle_shift_out_of_bounds",
946 "csum_partial_copy_generic",
948 "copy_mc_fragile_handle_tail",
949 "copy_mc_enhanced_fast_string",
950 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
954 static void add_uaccess_safe(struct objtool_file *file)
962 for (name = uaccess_safe_builtin; *name; name++) {
963 func = find_symbol_by_name(file->elf, *name);
967 func->uaccess_safe = true;
972 * FIXME: For now, just ignore any alternatives which add retpolines. This is
973 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
974 * But it at least allows objtool to understand the control flow *around* the
977 static int add_ignore_alternatives(struct objtool_file *file)
981 struct instruction *insn;
983 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
987 list_for_each_entry(reloc, &sec->reloc_list, list) {
988 if (reloc->sym->type != STT_SECTION) {
989 WARN("unexpected relocation symbol type in %s", sec->name);
993 insn = find_insn(file, reloc->sym->sec, reloc->addend);
995 WARN("bad .discard.ignore_alts entry");
999 insn->ignore_alts = true;
1005 __weak bool arch_is_retpoline(struct symbol *sym)
1010 #define NEGATIVE_RELOC ((void *)-1L)
1012 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1014 if (insn->reloc == NEGATIVE_RELOC)
1021 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1022 insn->offset, insn->len);
1024 insn->reloc = NEGATIVE_RELOC;
1032 static void remove_insn_ops(struct instruction *insn)
1034 struct stack_op *op, *tmp;
1036 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1037 list_del(&op->list);
1042 static void annotate_call_site(struct objtool_file *file,
1043 struct instruction *insn, bool sibling)
1045 struct reloc *reloc = insn_reloc(file, insn);
1046 struct symbol *sym = insn->call_dest;
1052 * Alternative replacement code is just template code which is
1053 * sometimes copied to the original instruction. For now, don't
1054 * annotate it. (In the future we might consider annotating the
1055 * original instruction if/when it ever makes sense to do so.)
1057 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1060 if (sym->static_call_tramp) {
1061 list_add_tail(&insn->call_node, &file->static_call_list);
1065 if (sym->retpoline_thunk) {
1066 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1071 * Many compilers cannot disable KCOV with a function attribute
1072 * so they need a little help, NOP out any KCOV calls from noinstr
1075 if (insn->sec->noinstr && sym->kcov) {
1077 reloc->type = R_NONE;
1078 elf_write_reloc(file->elf, reloc);
1081 elf_write_insn(file->elf, insn->sec,
1082 insn->offset, insn->len,
1083 sibling ? arch_ret_insn(insn->len)
1084 : arch_nop_insn(insn->len));
1086 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1090 if (mcount && sym->fentry) {
1092 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1095 reloc->type = R_NONE;
1096 elf_write_reloc(file->elf, reloc);
1099 elf_write_insn(file->elf, insn->sec,
1100 insn->offset, insn->len,
1101 arch_nop_insn(insn->len));
1103 insn->type = INSN_NOP;
1105 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1110 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1111 struct symbol *dest, bool sibling)
1113 insn->call_dest = dest;
1118 * Whatever stack impact regular CALLs have, should be undone
1119 * by the RETURN of the called function.
1121 * Annotated intra-function calls retain the stack_ops but
1122 * are converted to JUMP, see read_intra_function_calls().
1124 remove_insn_ops(insn);
1126 annotate_call_site(file, insn, sibling);
1129 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1132 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1133 * so convert them accordingly.
1135 switch (insn->type) {
1137 insn->type = INSN_CALL_DYNAMIC;
1139 case INSN_JUMP_UNCONDITIONAL:
1140 insn->type = INSN_JUMP_DYNAMIC;
1142 case INSN_JUMP_CONDITIONAL:
1143 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1149 insn->retpoline_safe = true;
1152 * Whatever stack impact regular CALLs have, should be undone
1153 * by the RETURN of the called function.
1155 * Annotated intra-function calls retain the stack_ops but
1156 * are converted to JUMP, see read_intra_function_calls().
1158 remove_insn_ops(insn);
1160 annotate_call_site(file, insn, false);
1163 * Find the destination instructions for all jumps.
1165 static int add_jump_destinations(struct objtool_file *file)
1167 struct instruction *insn;
1168 struct reloc *reloc;
1169 struct section *dest_sec;
1170 unsigned long dest_off;
1172 for_each_insn(file, insn) {
1173 if (!is_static_jump(insn))
1176 reloc = insn_reloc(file, insn);
1178 dest_sec = insn->sec;
1179 dest_off = arch_jump_destination(insn);
1180 } else if (reloc->sym->type == STT_SECTION) {
1181 dest_sec = reloc->sym->sec;
1182 dest_off = arch_dest_reloc_offset(reloc->addend);
1183 } else if (reloc->sym->retpoline_thunk) {
1184 add_retpoline_call(file, insn);
1186 } else if (insn->func) {
1187 /* internal or external sibling call (with reloc) */
1188 add_call_dest(file, insn, reloc->sym, true);
1190 } else if (reloc->sym->sec->idx) {
1191 dest_sec = reloc->sym->sec;
1192 dest_off = reloc->sym->sym.st_value +
1193 arch_dest_reloc_offset(reloc->addend);
1195 /* non-func asm code jumping to another file */
1199 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1200 if (!insn->jump_dest) {
1203 * This is a special case where an alt instruction
1204 * jumps past the end of the section. These are
1205 * handled later in handle_group_alt().
1207 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1210 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1211 insn->sec, insn->offset, dest_sec->name,
1217 * Cross-function jump.
1219 if (insn->func && insn->jump_dest->func &&
1220 insn->func != insn->jump_dest->func) {
1223 * For GCC 8+, create parent/child links for any cold
1224 * subfunctions. This is _mostly_ redundant with a
1225 * similar initialization in read_symbols().
1227 * If a function has aliases, we want the *first* such
1228 * function in the symbol table to be the subfunction's
1229 * parent. In that case we overwrite the
1230 * initialization done in read_symbols().
1232 * However this code can't completely replace the
1233 * read_symbols() code because this doesn't detect the
1234 * case where the parent function's only reference to a
1235 * subfunction is through a jump table.
1237 if (!strstr(insn->func->name, ".cold") &&
1238 strstr(insn->jump_dest->func->name, ".cold")) {
1239 insn->func->cfunc = insn->jump_dest->func;
1240 insn->jump_dest->func->pfunc = insn->func;
1242 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1243 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1244 /* internal sibling call (without reloc) */
1245 add_call_dest(file, insn, insn->jump_dest->func, true);
1253 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1255 struct symbol *call_dest;
1257 call_dest = find_func_by_offset(sec, offset);
1259 call_dest = find_symbol_by_offset(sec, offset);
1265 * Find the destination instructions for all calls.
1267 static int add_call_destinations(struct objtool_file *file)
1269 struct instruction *insn;
1270 unsigned long dest_off;
1271 struct symbol *dest;
1272 struct reloc *reloc;
1274 for_each_insn(file, insn) {
1275 if (insn->type != INSN_CALL)
1278 reloc = insn_reloc(file, insn);
1280 dest_off = arch_jump_destination(insn);
1281 dest = find_call_destination(insn->sec, dest_off);
1283 add_call_dest(file, insn, dest, false);
1288 if (!insn->call_dest) {
1289 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1293 if (insn->func && insn->call_dest->type != STT_FUNC) {
1294 WARN_FUNC("unsupported call to non-function",
1295 insn->sec, insn->offset);
1299 } else if (reloc->sym->type == STT_SECTION) {
1300 dest_off = arch_dest_reloc_offset(reloc->addend);
1301 dest = find_call_destination(reloc->sym->sec, dest_off);
1303 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1304 insn->sec, insn->offset,
1305 reloc->sym->sec->name,
1310 add_call_dest(file, insn, dest, false);
1312 } else if (reloc->sym->retpoline_thunk) {
1313 add_retpoline_call(file, insn);
1316 add_call_dest(file, insn, reloc->sym, false);
1323 * The .alternatives section requires some extra special care over and above
1324 * other special sections because alternatives are patched in place.
1326 static int handle_group_alt(struct objtool_file *file,
1327 struct special_alt *special_alt,
1328 struct instruction *orig_insn,
1329 struct instruction **new_insn)
1331 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1332 struct alt_group *orig_alt_group, *new_alt_group;
1333 unsigned long dest_off;
1336 orig_alt_group = malloc(sizeof(*orig_alt_group));
1337 if (!orig_alt_group) {
1338 WARN("malloc failed");
1341 orig_alt_group->cfi = calloc(special_alt->orig_len,
1342 sizeof(struct cfi_state *));
1343 if (!orig_alt_group->cfi) {
1344 WARN("calloc failed");
1348 last_orig_insn = NULL;
1350 sec_for_each_insn_from(file, insn) {
1351 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1354 insn->alt_group = orig_alt_group;
1355 last_orig_insn = insn;
1357 orig_alt_group->orig_group = NULL;
1358 orig_alt_group->first_insn = orig_insn;
1359 orig_alt_group->last_insn = last_orig_insn;
1362 new_alt_group = malloc(sizeof(*new_alt_group));
1363 if (!new_alt_group) {
1364 WARN("malloc failed");
1368 if (special_alt->new_len < special_alt->orig_len) {
1370 * Insert a fake nop at the end to make the replacement
1371 * alt_group the same size as the original. This is needed to
1372 * allow propagate_alt_cfi() to do its magic. When the last
1373 * instruction affects the stack, the instruction after it (the
1374 * nop) will propagate the new state to the shared CFI array.
1376 nop = malloc(sizeof(*nop));
1378 WARN("malloc failed");
1381 memset(nop, 0, sizeof(*nop));
1382 INIT_LIST_HEAD(&nop->alts);
1383 INIT_LIST_HEAD(&nop->stack_ops);
1385 nop->sec = special_alt->new_sec;
1386 nop->offset = special_alt->new_off + special_alt->new_len;
1387 nop->len = special_alt->orig_len - special_alt->new_len;
1388 nop->type = INSN_NOP;
1389 nop->func = orig_insn->func;
1390 nop->alt_group = new_alt_group;
1391 nop->ignore = orig_insn->ignore_alts;
1394 if (!special_alt->new_len) {
1400 sec_for_each_insn_from(file, insn) {
1401 struct reloc *alt_reloc;
1403 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1406 last_new_insn = insn;
1408 insn->ignore = orig_insn->ignore_alts;
1409 insn->func = orig_insn->func;
1410 insn->alt_group = new_alt_group;
1413 * Since alternative replacement code is copy/pasted by the
1414 * kernel after applying relocations, generally such code can't
1415 * have relative-address relocation references to outside the
1416 * .altinstr_replacement section, unless the arch's
1417 * alternatives code can adjust the relative offsets
1420 alt_reloc = insn_reloc(file, insn);
1422 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1424 WARN_FUNC("unsupported relocation in alternatives section",
1425 insn->sec, insn->offset);
1429 if (!is_static_jump(insn))
1432 if (!insn->immediate)
1435 dest_off = arch_jump_destination(insn);
1436 if (dest_off == special_alt->new_off + special_alt->new_len)
1437 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1439 if (!insn->jump_dest) {
1440 WARN_FUNC("can't find alternative jump destination",
1441 insn->sec, insn->offset);
1446 if (!last_new_insn) {
1447 WARN_FUNC("can't find last new alternative instruction",
1448 special_alt->new_sec, special_alt->new_off);
1453 list_add(&nop->list, &last_new_insn->list);
1455 new_alt_group->orig_group = orig_alt_group;
1456 new_alt_group->first_insn = *new_insn;
1457 new_alt_group->last_insn = nop ? : last_new_insn;
1458 new_alt_group->cfi = orig_alt_group->cfi;
1463 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1464 * If the original instruction is a jump, make the alt entry an effective nop
1465 * by just skipping the original instruction.
1467 static int handle_jump_alt(struct objtool_file *file,
1468 struct special_alt *special_alt,
1469 struct instruction *orig_insn,
1470 struct instruction **new_insn)
1472 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1473 orig_insn->type != INSN_NOP) {
1475 WARN_FUNC("unsupported instruction at jump label",
1476 orig_insn->sec, orig_insn->offset);
1480 if (special_alt->key_addend & 2) {
1481 struct reloc *reloc = insn_reloc(file, orig_insn);
1484 reloc->type = R_NONE;
1485 elf_write_reloc(file->elf, reloc);
1487 elf_write_insn(file->elf, orig_insn->sec,
1488 orig_insn->offset, orig_insn->len,
1489 arch_nop_insn(orig_insn->len));
1490 orig_insn->type = INSN_NOP;
1493 if (orig_insn->type == INSN_NOP) {
1494 if (orig_insn->len == 2)
1495 file->jl_nop_short++;
1497 file->jl_nop_long++;
1502 if (orig_insn->len == 2)
1507 *new_insn = list_next_entry(orig_insn, list);
1512 * Read all the special sections which have alternate instructions which can be
1513 * patched in or redirected to at runtime. Each instruction having alternate
1514 * instruction(s) has them added to its insn->alts list, which will be
1515 * traversed in validate_branch().
1517 static int add_special_section_alts(struct objtool_file *file)
1519 struct list_head special_alts;
1520 struct instruction *orig_insn, *new_insn;
1521 struct special_alt *special_alt, *tmp;
1522 struct alternative *alt;
1525 ret = special_get_alts(file->elf, &special_alts);
1529 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1531 orig_insn = find_insn(file, special_alt->orig_sec,
1532 special_alt->orig_off);
1534 WARN_FUNC("special: can't find orig instruction",
1535 special_alt->orig_sec, special_alt->orig_off);
1541 if (!special_alt->group || special_alt->new_len) {
1542 new_insn = find_insn(file, special_alt->new_sec,
1543 special_alt->new_off);
1545 WARN_FUNC("special: can't find new instruction",
1546 special_alt->new_sec,
1547 special_alt->new_off);
1553 if (special_alt->group) {
1554 if (!special_alt->orig_len) {
1555 WARN_FUNC("empty alternative entry",
1556 orig_insn->sec, orig_insn->offset);
1560 ret = handle_group_alt(file, special_alt, orig_insn,
1564 } else if (special_alt->jump_or_nop) {
1565 ret = handle_jump_alt(file, special_alt, orig_insn,
1571 alt = malloc(sizeof(*alt));
1573 WARN("malloc failed");
1578 alt->insn = new_insn;
1579 alt->skip_orig = special_alt->skip_orig;
1580 orig_insn->ignore_alts |= special_alt->skip_alt;
1581 list_add_tail(&alt->list, &orig_insn->alts);
1583 list_del(&special_alt->list);
1588 printf("jl\\\tNOP\tJMP\n");
1589 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1590 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1597 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1598 struct reloc *table)
1600 struct reloc *reloc = table;
1601 struct instruction *dest_insn;
1602 struct alternative *alt;
1603 struct symbol *pfunc = insn->func->pfunc;
1604 unsigned int prev_offset = 0;
1607 * Each @reloc is a switch table relocation which points to the target
1610 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1612 /* Check for the end of the table: */
1613 if (reloc != table && reloc->jump_table_start)
1616 /* Make sure the table entries are consecutive: */
1617 if (prev_offset && reloc->offset != prev_offset + 8)
1620 /* Detect function pointers from contiguous objects: */
1621 if (reloc->sym->sec == pfunc->sec &&
1622 reloc->addend == pfunc->offset)
1625 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1629 /* Make sure the destination is in the same function: */
1630 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1633 alt = malloc(sizeof(*alt));
1635 WARN("malloc failed");
1639 alt->insn = dest_insn;
1640 list_add_tail(&alt->list, &insn->alts);
1641 prev_offset = reloc->offset;
1645 WARN_FUNC("can't find switch jump table",
1646 insn->sec, insn->offset);
1654 * find_jump_table() - Given a dynamic jump, find the switch jump table
1655 * associated with it.
1657 static struct reloc *find_jump_table(struct objtool_file *file,
1658 struct symbol *func,
1659 struct instruction *insn)
1661 struct reloc *table_reloc;
1662 struct instruction *dest_insn, *orig_insn = insn;
1665 * Backward search using the @first_jump_src links, these help avoid
1666 * much of the 'in between' code. Which avoids us getting confused by
1670 insn && insn->func && insn->func->pfunc == func;
1671 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1673 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1676 /* allow small jumps within the range */
1677 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1679 (insn->jump_dest->offset <= insn->offset ||
1680 insn->jump_dest->offset > orig_insn->offset))
1683 table_reloc = arch_find_switch_table(file, insn);
1686 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1687 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1697 * First pass: Mark the head of each jump table so that in the next pass,
1698 * we know when a given jump table ends and the next one starts.
1700 static void mark_func_jump_tables(struct objtool_file *file,
1701 struct symbol *func)
1703 struct instruction *insn, *last = NULL;
1704 struct reloc *reloc;
1706 func_for_each_insn(file, func, insn) {
1711 * Store back-pointers for unconditional forward jumps such
1712 * that find_jump_table() can back-track using those and
1713 * avoid some potentially confusing code.
1715 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1716 insn->offset > last->offset &&
1717 insn->jump_dest->offset > insn->offset &&
1718 !insn->jump_dest->first_jump_src) {
1720 insn->jump_dest->first_jump_src = insn;
1721 last = insn->jump_dest;
1724 if (insn->type != INSN_JUMP_DYNAMIC)
1727 reloc = find_jump_table(file, func, insn);
1729 reloc->jump_table_start = true;
1730 insn->jump_table = reloc;
1735 static int add_func_jump_tables(struct objtool_file *file,
1736 struct symbol *func)
1738 struct instruction *insn;
1741 func_for_each_insn(file, func, insn) {
1742 if (!insn->jump_table)
1745 ret = add_jump_table(file, insn, insn->jump_table);
1754 * For some switch statements, gcc generates a jump table in the .rodata
1755 * section which contains a list of addresses within the function to jump to.
1756 * This finds these jump tables and adds them to the insn->alts lists.
1758 static int add_jump_table_alts(struct objtool_file *file)
1760 struct section *sec;
1761 struct symbol *func;
1767 for_each_sec(file, sec) {
1768 list_for_each_entry(func, &sec->symbol_list, list) {
1769 if (func->type != STT_FUNC)
1772 mark_func_jump_tables(file, func);
1773 ret = add_func_jump_tables(file, func);
1782 static void set_func_state(struct cfi_state *state)
1784 state->cfa = initial_func_cfi.cfa;
1785 memcpy(&state->regs, &initial_func_cfi.regs,
1786 CFI_NUM_REGS * sizeof(struct cfi_reg));
1787 state->stack_size = initial_func_cfi.cfa.offset;
1790 static int read_unwind_hints(struct objtool_file *file)
1792 struct cfi_state cfi = init_cfi;
1793 struct section *sec, *relocsec;
1794 struct unwind_hint *hint;
1795 struct instruction *insn;
1796 struct reloc *reloc;
1799 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1803 relocsec = sec->reloc;
1805 WARN("missing .rela.discard.unwind_hints section");
1809 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1810 WARN("struct unwind_hint size mismatch");
1816 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1817 hint = (struct unwind_hint *)sec->data->d_buf + i;
1819 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1821 WARN("can't find reloc for unwind_hints[%d]", i);
1825 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1827 WARN("can't find insn for unwind_hints[%d]", i);
1833 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1834 insn->cfi = &func_cfi;
1841 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1842 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1843 insn->sec, insn->offset, hint->sp_reg);
1847 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1848 cfi.type = hint->type;
1849 cfi.end = hint->end;
1851 insn->cfi = cfi_hash_find_or_add(&cfi);
1857 static int read_retpoline_hints(struct objtool_file *file)
1859 struct section *sec;
1860 struct instruction *insn;
1861 struct reloc *reloc;
1863 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1867 list_for_each_entry(reloc, &sec->reloc_list, list) {
1868 if (reloc->sym->type != STT_SECTION) {
1869 WARN("unexpected relocation symbol type in %s", sec->name);
1873 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1875 WARN("bad .discard.retpoline_safe entry");
1879 if (insn->type != INSN_JUMP_DYNAMIC &&
1880 insn->type != INSN_CALL_DYNAMIC) {
1881 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1882 insn->sec, insn->offset);
1886 insn->retpoline_safe = true;
1892 static int read_instr_hints(struct objtool_file *file)
1894 struct section *sec;
1895 struct instruction *insn;
1896 struct reloc *reloc;
1898 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1902 list_for_each_entry(reloc, &sec->reloc_list, list) {
1903 if (reloc->sym->type != STT_SECTION) {
1904 WARN("unexpected relocation symbol type in %s", sec->name);
1908 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1910 WARN("bad .discard.instr_end entry");
1917 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1921 list_for_each_entry(reloc, &sec->reloc_list, list) {
1922 if (reloc->sym->type != STT_SECTION) {
1923 WARN("unexpected relocation symbol type in %s", sec->name);
1927 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1929 WARN("bad .discard.instr_begin entry");
1939 static int read_intra_function_calls(struct objtool_file *file)
1941 struct instruction *insn;
1942 struct section *sec;
1943 struct reloc *reloc;
1945 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1949 list_for_each_entry(reloc, &sec->reloc_list, list) {
1950 unsigned long dest_off;
1952 if (reloc->sym->type != STT_SECTION) {
1953 WARN("unexpected relocation symbol type in %s",
1958 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1960 WARN("bad .discard.intra_function_call entry");
1964 if (insn->type != INSN_CALL) {
1965 WARN_FUNC("intra_function_call not a direct call",
1966 insn->sec, insn->offset);
1971 * Treat intra-function CALLs as JMPs, but with a stack_op.
1972 * See add_call_destinations(), which strips stack_ops from
1975 insn->type = INSN_JUMP_UNCONDITIONAL;
1977 dest_off = insn->offset + insn->len + insn->immediate;
1978 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1979 if (!insn->jump_dest) {
1980 WARN_FUNC("can't find call dest at %s+0x%lx",
1981 insn->sec, insn->offset,
1982 insn->sec->name, dest_off);
1990 static int classify_symbols(struct objtool_file *file)
1992 struct section *sec;
1993 struct symbol *func;
1995 for_each_sec(file, sec) {
1996 list_for_each_entry(func, &sec->symbol_list, list) {
1997 if (func->bind != STB_GLOBAL)
2000 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2001 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2002 func->static_call_tramp = true;
2004 if (arch_is_retpoline(func))
2005 func->retpoline_thunk = true;
2007 if (!strcmp(func->name, "__fentry__"))
2008 func->fentry = true;
2010 if (!strncmp(func->name, "__sanitizer_cov_", 16))
2018 static void mark_rodata(struct objtool_file *file)
2020 struct section *sec;
2024 * Search for the following rodata sections, each of which can
2025 * potentially contain jump tables:
2027 * - .rodata: can contain GCC switch tables
2028 * - .rodata.<func>: same, if -fdata-sections is being used
2029 * - .rodata..c_jump_table: contains C annotated jump tables
2031 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2033 for_each_sec(file, sec) {
2034 if (!strncmp(sec->name, ".rodata", 7) &&
2035 !strstr(sec->name, ".str1.")) {
2041 file->rodata = found;
2044 static int decode_sections(struct objtool_file *file)
2050 ret = init_pv_ops(file);
2054 ret = decode_instructions(file);
2058 ret = add_dead_ends(file);
2063 add_uaccess_safe(file);
2065 ret = add_ignore_alternatives(file);
2070 * Must be before add_{jump_call}_destination.
2072 ret = classify_symbols(file);
2077 * Must be before add_special_section_alts() as that depends on
2078 * jump_dest being set.
2080 ret = add_jump_destinations(file);
2084 ret = add_special_section_alts(file);
2089 * Must be before add_call_destination(); it changes INSN_CALL to
2092 ret = read_intra_function_calls(file);
2096 ret = add_call_destinations(file);
2100 ret = add_jump_table_alts(file);
2104 ret = read_unwind_hints(file);
2108 ret = read_retpoline_hints(file);
2112 ret = read_instr_hints(file);
2119 static bool is_fentry_call(struct instruction *insn)
2121 if (insn->type == INSN_CALL &&
2123 insn->call_dest->fentry)
2129 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2131 struct cfi_state *cfi = &state->cfi;
2134 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2137 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2140 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2143 for (i = 0; i < CFI_NUM_REGS; i++) {
2144 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2145 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2152 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2153 int expected_offset)
2155 return reg->base == CFI_CFA &&
2156 reg->offset == expected_offset;
2159 static bool has_valid_stack_frame(struct insn_state *state)
2161 struct cfi_state *cfi = &state->cfi;
2163 if (cfi->cfa.base == CFI_BP &&
2164 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2165 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2168 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2174 static int update_cfi_state_regs(struct instruction *insn,
2175 struct cfi_state *cfi,
2176 struct stack_op *op)
2178 struct cfi_reg *cfa = &cfi->cfa;
2180 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2184 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2188 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2191 /* add immediate to sp */
2192 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2193 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2194 cfa->offset -= op->src.offset;
2199 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2201 if (arch_callee_saved_reg(reg) &&
2202 cfi->regs[reg].base == CFI_UNDEFINED) {
2203 cfi->regs[reg].base = base;
2204 cfi->regs[reg].offset = offset;
2208 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2210 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2211 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2215 * A note about DRAP stack alignment:
2217 * GCC has the concept of a DRAP register, which is used to help keep track of
2218 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2219 * register. The typical DRAP pattern is:
2221 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2222 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2223 * 41 ff 72 f8 pushq -0x8(%r10)
2225 * 48 89 e5 mov %rsp,%rbp
2232 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2235 * There are some variations in the epilogues, like:
2243 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2248 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2249 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2250 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2251 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2253 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2256 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2257 * restored beforehand:
2260 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2261 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2263 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2267 static int update_cfi_state(struct instruction *insn,
2268 struct instruction *next_insn,
2269 struct cfi_state *cfi, struct stack_op *op)
2271 struct cfi_reg *cfa = &cfi->cfa;
2272 struct cfi_reg *regs = cfi->regs;
2274 /* stack operations don't make sense with an undefined CFA */
2275 if (cfa->base == CFI_UNDEFINED) {
2277 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2283 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2284 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2285 return update_cfi_state_regs(insn, cfi, op);
2287 switch (op->dest.type) {
2290 switch (op->src.type) {
2293 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2294 cfa->base == CFI_SP &&
2295 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2297 /* mov %rsp, %rbp */
2298 cfa->base = op->dest.reg;
2299 cfi->bp_scratch = false;
2302 else if (op->src.reg == CFI_SP &&
2303 op->dest.reg == CFI_BP && cfi->drap) {
2305 /* drap: mov %rsp, %rbp */
2306 regs[CFI_BP].base = CFI_BP;
2307 regs[CFI_BP].offset = -cfi->stack_size;
2308 cfi->bp_scratch = false;
2311 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2316 * This is needed for the rare case where GCC
2323 cfi->vals[op->dest.reg].base = CFI_CFA;
2324 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2327 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2328 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2333 * Restore the original stack pointer (Clang).
2335 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2338 else if (op->dest.reg == cfa->base) {
2340 /* mov %reg, %rsp */
2341 if (cfa->base == CFI_SP &&
2342 cfi->vals[op->src.reg].base == CFI_CFA) {
2345 * This is needed for the rare case
2346 * where GCC does something dumb like:
2348 * lea 0x8(%rsp), %rcx
2352 cfa->offset = -cfi->vals[op->src.reg].offset;
2353 cfi->stack_size = cfa->offset;
2355 } else if (cfa->base == CFI_SP &&
2356 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2357 cfi->vals[op->src.reg].offset == cfa->offset) {
2362 * 1: mov %rsp, (%[tos])
2363 * 2: mov %[tos], %rsp
2369 * 1 - places a pointer to the previous
2370 * stack at the Top-of-Stack of the
2373 * 2 - switches to the new stack.
2375 * 3 - pops the Top-of-Stack to restore
2376 * the original stack.
2378 * Note: we set base to SP_INDIRECT
2379 * here and preserve offset. Therefore
2380 * when the unwinder reaches ToS it
2381 * will dereference SP and then add the
2382 * offset to find the next frame, IOW:
2385 cfa->base = CFI_SP_INDIRECT;
2388 cfa->base = CFI_UNDEFINED;
2393 else if (op->dest.reg == CFI_SP &&
2394 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2395 cfi->vals[op->src.reg].offset == cfa->offset) {
2398 * The same stack swizzle case 2) as above. But
2399 * because we can't change cfa->base, case 3)
2400 * will become a regular POP. Pretend we're a
2401 * PUSH so things don't go unbalanced.
2403 cfi->stack_size += 8;
2410 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2413 cfi->stack_size -= op->src.offset;
2414 if (cfa->base == CFI_SP)
2415 cfa->offset -= op->src.offset;
2419 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2421 /* lea disp(%rbp), %rsp */
2422 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2426 if (!cfi->drap && op->src.reg == CFI_SP &&
2427 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2428 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2430 /* lea disp(%rsp), %rbp */
2432 cfa->offset -= op->src.offset;
2433 cfi->bp_scratch = false;
2437 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2439 /* drap: lea disp(%rsp), %drap */
2440 cfi->drap_reg = op->dest.reg;
2443 * lea disp(%rsp), %reg
2445 * This is needed for the rare case where GCC
2446 * does something dumb like:
2448 * lea 0x8(%rsp), %rcx
2452 cfi->vals[op->dest.reg].base = CFI_CFA;
2453 cfi->vals[op->dest.reg].offset = \
2454 -cfi->stack_size + op->src.offset;
2459 if (cfi->drap && op->dest.reg == CFI_SP &&
2460 op->src.reg == cfi->drap_reg) {
2462 /* drap: lea disp(%drap), %rsp */
2464 cfa->offset = cfi->stack_size = -op->src.offset;
2465 cfi->drap_reg = CFI_UNDEFINED;
2470 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2471 WARN_FUNC("unsupported stack register modification",
2472 insn->sec, insn->offset);
2479 if (op->dest.reg != CFI_SP ||
2480 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2481 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2482 WARN_FUNC("unsupported stack pointer realignment",
2483 insn->sec, insn->offset);
2487 if (cfi->drap_reg != CFI_UNDEFINED) {
2488 /* drap: and imm, %rsp */
2489 cfa->base = cfi->drap_reg;
2490 cfa->offset = cfi->stack_size = 0;
2495 * Older versions of GCC (4.8ish) realign the stack
2496 * without DRAP, with a frame pointer.
2503 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2505 /* pop %rsp; # restore from a stack swizzle */
2510 if (!cfi->drap && op->dest.reg == cfa->base) {
2516 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2517 op->dest.reg == cfi->drap_reg &&
2518 cfi->drap_offset == -cfi->stack_size) {
2520 /* drap: pop %drap */
2521 cfa->base = cfi->drap_reg;
2523 cfi->drap_offset = -1;
2525 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2528 restore_reg(cfi, op->dest.reg);
2531 cfi->stack_size -= 8;
2532 if (cfa->base == CFI_SP)
2537 case OP_SRC_REG_INDIRECT:
2538 if (!cfi->drap && op->dest.reg == cfa->base &&
2539 op->dest.reg == CFI_BP) {
2541 /* mov disp(%rsp), %rbp */
2543 cfa->offset = cfi->stack_size;
2546 if (cfi->drap && op->src.reg == CFI_BP &&
2547 op->src.offset == cfi->drap_offset) {
2549 /* drap: mov disp(%rbp), %drap */
2550 cfa->base = cfi->drap_reg;
2552 cfi->drap_offset = -1;
2555 if (cfi->drap && op->src.reg == CFI_BP &&
2556 op->src.offset == regs[op->dest.reg].offset) {
2558 /* drap: mov disp(%rbp), %reg */
2559 restore_reg(cfi, op->dest.reg);
2561 } else if (op->src.reg == cfa->base &&
2562 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2564 /* mov disp(%rbp), %reg */
2565 /* mov disp(%rsp), %reg */
2566 restore_reg(cfi, op->dest.reg);
2568 } else if (op->src.reg == CFI_SP &&
2569 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2571 /* mov disp(%rsp), %reg */
2572 restore_reg(cfi, op->dest.reg);
2578 WARN_FUNC("unknown stack-related instruction",
2579 insn->sec, insn->offset);
2587 cfi->stack_size += 8;
2588 if (cfa->base == CFI_SP)
2591 if (op->src.type != OP_SRC_REG)
2595 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2597 /* drap: push %drap */
2598 cfa->base = CFI_BP_INDIRECT;
2599 cfa->offset = -cfi->stack_size;
2601 /* save drap so we know when to restore it */
2602 cfi->drap_offset = -cfi->stack_size;
2604 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2606 /* drap: push %rbp */
2607 cfi->stack_size = 0;
2611 /* drap: push %reg */
2612 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2618 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2621 /* detect when asm code uses rbp as a scratch register */
2622 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2623 cfa->base != CFI_BP)
2624 cfi->bp_scratch = true;
2627 case OP_DEST_REG_INDIRECT:
2630 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2632 /* drap: mov %drap, disp(%rbp) */
2633 cfa->base = CFI_BP_INDIRECT;
2634 cfa->offset = op->dest.offset;
2636 /* save drap offset so we know when to restore it */
2637 cfi->drap_offset = op->dest.offset;
2640 /* drap: mov reg, disp(%rbp) */
2641 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2644 } else if (op->dest.reg == cfa->base) {
2646 /* mov reg, disp(%rbp) */
2647 /* mov reg, disp(%rsp) */
2648 save_reg(cfi, op->src.reg, CFI_CFA,
2649 op->dest.offset - cfi->cfa.offset);
2651 } else if (op->dest.reg == CFI_SP) {
2653 /* mov reg, disp(%rsp) */
2654 save_reg(cfi, op->src.reg, CFI_CFA,
2655 op->dest.offset - cfi->stack_size);
2657 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2659 /* mov %rsp, (%reg); # setup a stack swizzle. */
2660 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2661 cfi->vals[op->dest.reg].offset = cfa->offset;
2667 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2668 WARN_FUNC("unknown stack-related memory operation",
2669 insn->sec, insn->offset);
2674 cfi->stack_size -= 8;
2675 if (cfa->base == CFI_SP)
2681 WARN_FUNC("unknown stack-related instruction",
2682 insn->sec, insn->offset);
2690 * The stack layouts of alternatives instructions can sometimes diverge when
2691 * they have stack modifications. That's fine as long as the potential stack
2692 * layouts don't conflict at any given potential instruction boundary.
2694 * Flatten the CFIs of the different alternative code streams (both original
2695 * and replacement) into a single shared CFI array which can be used to detect
2696 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2698 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2700 struct cfi_state **alt_cfi;
2703 if (!insn->alt_group)
2707 WARN("CFI missing");
2711 alt_cfi = insn->alt_group->cfi;
2712 group_off = insn->offset - insn->alt_group->first_insn->offset;
2714 if (!alt_cfi[group_off]) {
2715 alt_cfi[group_off] = insn->cfi;
2717 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2718 WARN_FUNC("stack layout conflict in alternatives",
2719 insn->sec, insn->offset);
2727 static int handle_insn_ops(struct instruction *insn,
2728 struct instruction *next_insn,
2729 struct insn_state *state)
2731 struct stack_op *op;
2733 list_for_each_entry(op, &insn->stack_ops, list) {
2735 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2738 if (!insn->alt_group)
2741 if (op->dest.type == OP_DEST_PUSHF) {
2742 if (!state->uaccess_stack) {
2743 state->uaccess_stack = 1;
2744 } else if (state->uaccess_stack >> 31) {
2745 WARN_FUNC("PUSHF stack exhausted",
2746 insn->sec, insn->offset);
2749 state->uaccess_stack <<= 1;
2750 state->uaccess_stack |= state->uaccess;
2753 if (op->src.type == OP_SRC_POPF) {
2754 if (state->uaccess_stack) {
2755 state->uaccess = state->uaccess_stack & 1;
2756 state->uaccess_stack >>= 1;
2757 if (state->uaccess_stack == 1)
2758 state->uaccess_stack = 0;
2766 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2768 struct cfi_state *cfi1 = insn->cfi;
2772 WARN("CFI missing");
2776 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2778 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2779 insn->sec, insn->offset,
2780 cfi1->cfa.base, cfi1->cfa.offset,
2781 cfi2->cfa.base, cfi2->cfa.offset);
2783 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2784 for (i = 0; i < CFI_NUM_REGS; i++) {
2785 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2786 sizeof(struct cfi_reg)))
2789 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2790 insn->sec, insn->offset,
2791 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2792 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2796 } else if (cfi1->type != cfi2->type) {
2798 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2799 insn->sec, insn->offset, cfi1->type, cfi2->type);
2801 } else if (cfi1->drap != cfi2->drap ||
2802 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2803 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2805 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2806 insn->sec, insn->offset,
2807 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2808 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2816 static inline bool func_uaccess_safe(struct symbol *func)
2819 return func->uaccess_safe;
2824 static inline const char *call_dest_name(struct instruction *insn)
2826 static char pvname[16];
2830 if (insn->call_dest)
2831 return insn->call_dest->name;
2833 rel = insn_reloc(NULL, insn);
2834 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
2835 idx = (rel->addend / sizeof(void *));
2836 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
2843 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
2845 struct symbol *target;
2849 rel = insn_reloc(file, insn);
2850 if (!rel || strcmp(rel->sym->name, "pv_ops"))
2853 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
2855 if (file->pv_ops[idx].clean)
2858 file->pv_ops[idx].clean = true;
2860 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
2861 if (!target->sec->noinstr) {
2862 WARN("pv_ops[%d]: %s", idx, target->name);
2863 file->pv_ops[idx].clean = false;
2867 return file->pv_ops[idx].clean;
2870 static inline bool noinstr_call_dest(struct objtool_file *file,
2871 struct instruction *insn,
2872 struct symbol *func)
2875 * We can't deal with indirect function calls at present;
2876 * assume they're instrumented.
2880 return pv_call_dest(file, insn);
2886 * If the symbol is from a noinstr section; we good.
2888 if (func->sec->noinstr)
2892 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2893 * something 'BAD' happened. At the risk of taking the machine down,
2894 * let them proceed to get the message out.
2896 if (!strncmp(func->name, "__ubsan_handle_", 15))
2902 static int validate_call(struct objtool_file *file,
2903 struct instruction *insn,
2904 struct insn_state *state)
2906 if (state->noinstr && state->instr <= 0 &&
2907 !noinstr_call_dest(file, insn, insn->call_dest)) {
2908 WARN_FUNC("call to %s() leaves .noinstr.text section",
2909 insn->sec, insn->offset, call_dest_name(insn));
2913 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2914 WARN_FUNC("call to %s() with UACCESS enabled",
2915 insn->sec, insn->offset, call_dest_name(insn));
2920 WARN_FUNC("call to %s() with DF set",
2921 insn->sec, insn->offset, call_dest_name(insn));
2928 static int validate_sibling_call(struct objtool_file *file,
2929 struct instruction *insn,
2930 struct insn_state *state)
2932 if (has_modified_stack_frame(insn, state)) {
2933 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2934 insn->sec, insn->offset);
2938 return validate_call(file, insn, state);
2941 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2943 if (state->noinstr && state->instr > 0) {
2944 WARN_FUNC("return with instrumentation enabled",
2945 insn->sec, insn->offset);
2949 if (state->uaccess && !func_uaccess_safe(func)) {
2950 WARN_FUNC("return with UACCESS enabled",
2951 insn->sec, insn->offset);
2955 if (!state->uaccess && func_uaccess_safe(func)) {
2956 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2957 insn->sec, insn->offset);
2962 WARN_FUNC("return with DF set",
2963 insn->sec, insn->offset);
2967 if (func && has_modified_stack_frame(insn, state)) {
2968 WARN_FUNC("return with modified stack frame",
2969 insn->sec, insn->offset);
2973 if (state->cfi.bp_scratch) {
2974 WARN_FUNC("BP used as a scratch register",
2975 insn->sec, insn->offset);
2982 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2983 struct instruction *insn)
2985 struct alt_group *alt_group = insn->alt_group;
2988 * Simulate the fact that alternatives are patched in-place. When the
2989 * end of a replacement alt_group is reached, redirect objtool flow to
2990 * the end of the original alt_group.
2992 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2993 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2995 return next_insn_same_sec(file, insn);
2999 * Follow the branch starting at the given instruction, and recursively follow
3000 * any other branches (jumps). Meanwhile, track the frame pointer state at
3001 * each instruction and validate all the rules described in
3002 * tools/objtool/Documentation/stack-validation.txt.
3004 static int validate_branch(struct objtool_file *file, struct symbol *func,
3005 struct instruction *insn, struct insn_state state)
3007 struct alternative *alt;
3008 struct instruction *next_insn, *prev_insn = NULL;
3009 struct section *sec;
3016 next_insn = next_insn_to_validate(file, insn);
3018 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
3019 WARN("%s() falls through to next function %s()",
3020 func->name, insn->func->name);
3024 if (func && insn->ignore) {
3025 WARN_FUNC("BUG: why am I validating an ignored function?",
3030 visited = 1 << state.uaccess;
3031 if (insn->visited) {
3032 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3035 if (insn->visited & visited)
3042 state.instr += insn->instr;
3045 state.cfi = *insn->cfi;
3047 /* XXX track if we actually changed state.cfi */
3049 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3050 insn->cfi = prev_insn->cfi;
3053 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3057 insn->visited |= visited;
3059 if (propagate_alt_cfi(file, insn))
3062 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3063 bool skip_orig = false;
3065 list_for_each_entry(alt, &insn->alts, list) {
3069 ret = validate_branch(file, func, alt->insn, state);
3072 BT_FUNC("(alt)", insn);
3081 if (handle_insn_ops(insn, next_insn, &state))
3084 switch (insn->type) {
3087 return validate_return(func, insn, &state);
3090 case INSN_CALL_DYNAMIC:
3091 ret = validate_call(file, insn, &state);
3095 if (!no_fp && func && !is_fentry_call(insn) &&
3096 !has_valid_stack_frame(&state)) {
3097 WARN_FUNC("call without frame pointer save/setup",
3102 if (dead_end_function(file, insn->call_dest))
3107 case INSN_JUMP_CONDITIONAL:
3108 case INSN_JUMP_UNCONDITIONAL:
3109 if (is_sibling_call(insn)) {
3110 ret = validate_sibling_call(file, insn, &state);
3114 } else if (insn->jump_dest) {
3115 ret = validate_branch(file, func,
3116 insn->jump_dest, state);
3119 BT_FUNC("(branch)", insn);
3124 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3129 case INSN_JUMP_DYNAMIC:
3130 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3131 if (is_sibling_call(insn)) {
3132 ret = validate_sibling_call(file, insn, &state);
3137 if (insn->type == INSN_JUMP_DYNAMIC)
3142 case INSN_CONTEXT_SWITCH:
3143 if (func && (!next_insn || !next_insn->hint)) {
3144 WARN_FUNC("unsupported instruction in callable function",
3151 if (state.uaccess) {
3152 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3156 state.uaccess = true;
3160 if (!state.uaccess && func) {
3161 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3165 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3166 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3170 state.uaccess = false;
3175 WARN_FUNC("recursive STD", sec, insn->offset);
3183 if (!state.df && func) {
3184 WARN_FUNC("redundant CLD", sec, insn->offset);
3199 if (state.cfi.cfa.base == CFI_UNDEFINED)
3201 WARN("%s: unexpected end of section", sec->name);
3212 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3214 struct instruction *insn;
3215 struct insn_state state;
3216 int ret, warnings = 0;
3221 init_insn_state(&state, sec);
3224 insn = find_insn(file, sec, 0);
3228 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3231 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3232 if (insn->hint && !insn->visited && !insn->ignore) {
3233 ret = validate_branch(file, insn->func, insn, state);
3234 if (ret && backtrace)
3235 BT_FUNC("<=== (hint)", insn);
3239 insn = list_next_entry(insn, list);
3245 static int validate_retpoline(struct objtool_file *file)
3247 struct instruction *insn;
3250 for_each_insn(file, insn) {
3251 if (insn->type != INSN_JUMP_DYNAMIC &&
3252 insn->type != INSN_CALL_DYNAMIC)
3255 if (insn->retpoline_safe)
3259 * .init.text code is ran before userspace and thus doesn't
3260 * strictly need retpolines, except for modules which are
3261 * loaded late, they very much do need retpoline in their
3264 if (!strcmp(insn->sec->name, ".init.text") && !module)
3267 WARN_FUNC("indirect %s found in RETPOLINE build",
3268 insn->sec, insn->offset,
3269 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3277 static bool is_kasan_insn(struct instruction *insn)
3279 return (insn->type == INSN_CALL &&
3280 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3283 static bool is_ubsan_insn(struct instruction *insn)
3285 return (insn->type == INSN_CALL &&
3286 !strcmp(insn->call_dest->name,
3287 "__ubsan_handle_builtin_unreachable"));
3290 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3293 struct instruction *prev_insn;
3295 if (insn->ignore || insn->type == INSN_NOP)
3299 * Ignore any unused exceptions. This can happen when a whitelisted
3300 * function has an exception table entry.
3302 * Also ignore alternative replacement instructions. This can happen
3303 * when a whitelisted function uses one of the ALTERNATIVE macros.
3305 if (!strcmp(insn->sec->name, ".fixup") ||
3306 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3307 !strcmp(insn->sec->name, ".altinstr_aux"))
3314 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3315 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3316 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3317 * (or occasionally a JMP to UD2).
3319 * It may also insert a UD2 after calling a __noreturn function.
3321 prev_insn = list_prev_entry(insn, list);
3322 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3323 (insn->type == INSN_BUG ||
3324 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3325 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3329 * Check if this (or a subsequent) instruction is related to
3330 * CONFIG_UBSAN or CONFIG_KASAN.
3332 * End the search at 5 instructions to avoid going into the weeds.
3334 for (i = 0; i < 5; i++) {
3336 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3339 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3340 if (insn->jump_dest &&
3341 insn->jump_dest->func == insn->func) {
3342 insn = insn->jump_dest;
3349 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3352 insn = list_next_entry(insn, list);
3358 static int validate_symbol(struct objtool_file *file, struct section *sec,
3359 struct symbol *sym, struct insn_state *state)
3361 struct instruction *insn;
3365 WARN("%s() is missing an ELF size annotation", sym->name);
3369 if (sym->pfunc != sym || sym->alias != sym)
3372 insn = find_insn(file, sec, sym->offset);
3373 if (!insn || insn->ignore || insn->visited)
3376 state->uaccess = sym->uaccess_safe;
3378 ret = validate_branch(file, insn->func, insn, *state);
3379 if (ret && backtrace)
3380 BT_FUNC("<=== (sym)", insn);
3384 static int validate_section(struct objtool_file *file, struct section *sec)
3386 struct insn_state state;
3387 struct symbol *func;
3390 list_for_each_entry(func, &sec->symbol_list, list) {
3391 if (func->type != STT_FUNC)
3394 init_insn_state(&state, sec);
3395 set_func_state(&state.cfi);
3397 warnings += validate_symbol(file, sec, func, &state);
3403 static int validate_vmlinux_functions(struct objtool_file *file)
3405 struct section *sec;
3408 sec = find_section_by_name(file->elf, ".noinstr.text");
3410 warnings += validate_section(file, sec);
3411 warnings += validate_unwind_hints(file, sec);
3414 sec = find_section_by_name(file->elf, ".entry.text");
3416 warnings += validate_section(file, sec);
3417 warnings += validate_unwind_hints(file, sec);
3423 static int validate_functions(struct objtool_file *file)
3425 struct section *sec;
3428 for_each_sec(file, sec) {
3429 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3432 warnings += validate_section(file, sec);
3438 static int validate_reachable_instructions(struct objtool_file *file)
3440 struct instruction *insn;
3442 if (file->ignore_unreachables)
3445 for_each_insn(file, insn) {
3446 if (insn->visited || ignore_unreachable_insn(file, insn))
3449 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3456 int check(struct objtool_file *file)
3458 int ret, warnings = 0;
3460 arch_initial_func_cfi_state(&initial_func_cfi);
3461 init_cfi_state(&init_cfi);
3462 init_cfi_state(&func_cfi);
3463 set_func_state(&func_cfi);
3465 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3468 cfi_hash_add(&init_cfi);
3469 cfi_hash_add(&func_cfi);
3471 ret = decode_sections(file);
3477 if (list_empty(&file->insn_list))
3480 if (vmlinux && !validate_dup) {
3481 ret = validate_vmlinux_functions(file);
3490 ret = validate_retpoline(file);
3496 ret = validate_functions(file);
3501 ret = validate_unwind_hints(file, NULL);
3507 ret = validate_reachable_instructions(file);
3513 ret = create_static_call_sections(file);
3519 ret = create_retpoline_sites_sections(file);
3526 ret = create_mcount_loc_sections(file);
3533 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3534 printf("nr_cfi: %ld\n", nr_cfi);
3535 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3536 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3541 * For now, don't fail the kernel build on fatal warnings. These
3542 * errors are still fairly common due to the growing matrix of
3543 * supported toolchains and their recent pace of change.