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[] = {
173 "__module_put_and_kthread_exit",
174 "kthread_complete_and_exit",
179 "machine_real_restart",
180 "rewind_stack_and_make_dead",
181 "kunit_try_catch_throw",
183 "cpu_bringup_and_idle",
189 if (func->bind == STB_WEAK)
192 if (func->bind == STB_GLOBAL)
193 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
194 if (!strcmp(func->name, global_noreturns[i]))
200 insn = find_insn(file, func->sec, func->offset);
204 func_for_each_insn(file, func, insn) {
207 if (insn->type == INSN_RETURN)
215 * A function can have a sibling call instead of a return. In that
216 * case, the function's dead-end status depends on whether the target
217 * of the sibling call returns.
219 func_for_each_insn(file, func, insn) {
220 if (is_sibling_call(insn)) {
221 struct instruction *dest = insn->jump_dest;
224 /* sibling call to another file */
227 /* local sibling call */
228 if (recursion == 5) {
230 * Infinite recursion: two functions have
231 * sibling calls to each other. This is a very
232 * rare case. It means they aren't dead ends.
237 return __dead_end_function(file, dest->func, recursion+1);
244 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
246 return __dead_end_function(file, func, 0);
249 static void init_cfi_state(struct cfi_state *cfi)
253 for (i = 0; i < CFI_NUM_REGS; i++) {
254 cfi->regs[i].base = CFI_UNDEFINED;
255 cfi->vals[i].base = CFI_UNDEFINED;
257 cfi->cfa.base = CFI_UNDEFINED;
258 cfi->drap_reg = CFI_UNDEFINED;
259 cfi->drap_offset = -1;
262 static void init_insn_state(struct insn_state *state, struct section *sec)
264 memset(state, 0, sizeof(*state));
265 init_cfi_state(&state->cfi);
268 * We need the full vmlinux for noinstr validation, otherwise we can
269 * not correctly determine insn->call_dest->sec (external symbols do
270 * not have a section).
272 if (vmlinux && noinstr && sec)
273 state->noinstr = sec->noinstr;
276 static struct cfi_state *cfi_alloc(void)
278 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
280 WARN("calloc failed");
288 static struct hlist_head *cfi_hash;
290 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
292 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
293 (void *)cfi2 + sizeof(cfi2->hash),
294 sizeof(struct cfi_state) - sizeof(struct hlist_node));
297 static inline u32 cfi_key(struct cfi_state *cfi)
299 return jhash((void *)cfi + sizeof(cfi->hash),
300 sizeof(*cfi) - sizeof(cfi->hash), 0);
303 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
305 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
306 struct cfi_state *obj;
308 hlist_for_each_entry(obj, head, hash) {
309 if (!cficmp(cfi, obj)) {
317 hlist_add_head(&obj->hash, head);
322 static void cfi_hash_add(struct cfi_state *cfi)
324 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
326 hlist_add_head(&cfi->hash, head);
329 static void *cfi_hash_alloc(unsigned long size)
331 cfi_bits = max(10, ilog2(size));
332 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
333 PROT_READ|PROT_WRITE,
334 MAP_PRIVATE|MAP_ANON, -1, 0);
335 if (cfi_hash == (void *)-1L) {
336 WARN("mmap fail cfi_hash");
339 printf("cfi_bits: %d\n", cfi_bits);
345 static unsigned long nr_insns;
346 static unsigned long nr_insns_visited;
349 * Call the arch-specific instruction decoder for all the instructions and add
350 * them to the global instruction list.
352 static int decode_instructions(struct objtool_file *file)
356 unsigned long offset;
357 struct instruction *insn;
360 for_each_sec(file, sec) {
362 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
365 if (strcmp(sec->name, ".altinstr_replacement") &&
366 strcmp(sec->name, ".altinstr_aux") &&
367 strncmp(sec->name, ".discard.", 9))
370 if (!strcmp(sec->name, ".noinstr.text") ||
371 !strcmp(sec->name, ".entry.text"))
374 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
375 insn = malloc(sizeof(*insn));
377 WARN("malloc failed");
380 memset(insn, 0, sizeof(*insn));
381 INIT_LIST_HEAD(&insn->alts);
382 INIT_LIST_HEAD(&insn->stack_ops);
385 insn->offset = offset;
387 ret = arch_decode_instruction(file, sec, offset,
388 sec->sh.sh_size - offset,
389 &insn->len, &insn->type,
395 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
396 list_add_tail(&insn->list, &file->insn_list);
400 list_for_each_entry(func, &sec->symbol_list, list) {
401 if (func->type != STT_FUNC || func->alias != func)
404 if (!find_insn(file, sec, func->offset)) {
405 WARN("%s(): can't find starting instruction",
410 sym_for_each_insn(file, func, insn)
416 printf("nr_insns: %lu\n", nr_insns);
426 * Read the pv_ops[] .data table to find the static initialized values.
428 static int add_pv_ops(struct objtool_file *file, const char *symname)
430 struct symbol *sym, *func;
431 unsigned long off, end;
435 sym = find_symbol_by_name(file->elf, symname);
440 end = off + sym->len;
442 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
447 if (func->type == STT_SECTION)
448 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
450 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
452 objtool_pv_add(file, idx, func);
454 off = rel->offset + 1;
463 * Allocate and initialize file->pv_ops[].
465 static int init_pv_ops(struct objtool_file *file)
467 static const char *pv_ops_tables[] = {
483 sym = find_symbol_by_name(file->elf, "pv_ops");
487 nr = sym->len / sizeof(unsigned long);
488 file->pv_ops = calloc(sizeof(struct pv_state), nr);
492 for (idx = 0; idx < nr; idx++)
493 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
495 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
496 add_pv_ops(file, pv_ops);
501 static struct instruction *find_last_insn(struct objtool_file *file,
504 struct instruction *insn = NULL;
506 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
508 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
509 insn = find_insn(file, sec, offset);
515 * Mark "ud2" instructions and manually annotated dead ends.
517 static int add_dead_ends(struct objtool_file *file)
521 struct instruction *insn;
524 * By default, "ud2" is a dead end unless otherwise annotated, because
525 * GCC 7 inserts it for certain divide-by-zero cases.
527 for_each_insn(file, insn)
528 if (insn->type == INSN_BUG)
529 insn->dead_end = true;
532 * Check for manually annotated dead ends.
534 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
538 list_for_each_entry(reloc, &sec->reloc_list, list) {
539 if (reloc->sym->type != STT_SECTION) {
540 WARN("unexpected relocation symbol type in %s", sec->name);
543 insn = find_insn(file, reloc->sym->sec, reloc->addend);
545 insn = list_prev_entry(insn, list);
546 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
547 insn = find_last_insn(file, reloc->sym->sec);
549 WARN("can't find unreachable insn at %s+0x%x",
550 reloc->sym->sec->name, reloc->addend);
554 WARN("can't find unreachable insn at %s+0x%x",
555 reloc->sym->sec->name, reloc->addend);
559 insn->dead_end = true;
564 * These manually annotated reachable checks are needed for GCC 4.4,
565 * where the Linux unreachable() macro isn't supported. In that case
566 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
569 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
573 list_for_each_entry(reloc, &sec->reloc_list, list) {
574 if (reloc->sym->type != STT_SECTION) {
575 WARN("unexpected relocation symbol type in %s", sec->name);
578 insn = find_insn(file, reloc->sym->sec, reloc->addend);
580 insn = list_prev_entry(insn, list);
581 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
582 insn = find_last_insn(file, reloc->sym->sec);
584 WARN("can't find reachable insn at %s+0x%x",
585 reloc->sym->sec->name, reloc->addend);
589 WARN("can't find reachable insn at %s+0x%x",
590 reloc->sym->sec->name, reloc->addend);
594 insn->dead_end = false;
600 static int create_static_call_sections(struct objtool_file *file)
603 struct static_call_site *site;
604 struct instruction *insn;
605 struct symbol *key_sym;
606 char *key_name, *tmp;
609 sec = find_section_by_name(file->elf, ".static_call_sites");
611 INIT_LIST_HEAD(&file->static_call_list);
612 WARN("file already has .static_call_sites section, skipping");
616 if (list_empty(&file->static_call_list))
620 list_for_each_entry(insn, &file->static_call_list, call_node)
623 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
624 sizeof(struct static_call_site), idx);
629 list_for_each_entry(insn, &file->static_call_list, call_node) {
631 site = (struct static_call_site *)sec->data->d_buf + idx;
632 memset(site, 0, sizeof(struct static_call_site));
634 /* populate reloc for 'addr' */
635 if (elf_add_reloc_to_insn(file->elf, sec,
636 idx * sizeof(struct static_call_site),
638 insn->sec, insn->offset))
641 /* find key symbol */
642 key_name = strdup(insn->call_dest->name);
647 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
648 STATIC_CALL_TRAMP_PREFIX_LEN)) {
649 WARN("static_call: trampoline name malformed: %s", key_name);
652 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
653 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
655 key_sym = find_symbol_by_name(file->elf, tmp);
658 WARN("static_call: can't find static_call_key symbol: %s", tmp);
663 * For modules(), the key might not be exported, which
664 * means the module can make static calls but isn't
665 * allowed to change them.
667 * In that case we temporarily set the key to be the
668 * trampoline address. This is fixed up in
669 * static_call_add_module().
671 key_sym = insn->call_dest;
675 /* populate reloc for 'key' */
676 if (elf_add_reloc(file->elf, sec,
677 idx * sizeof(struct static_call_site) + 4,
678 R_X86_64_PC32, key_sym,
679 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
688 static int create_retpoline_sites_sections(struct objtool_file *file)
690 struct instruction *insn;
694 sec = find_section_by_name(file->elf, ".retpoline_sites");
696 WARN("file already has .retpoline_sites, skipping");
701 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
707 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
710 WARN("elf_create_section: .retpoline_sites");
715 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
717 int *site = (int *)sec->data->d_buf + idx;
720 if (elf_add_reloc_to_insn(file->elf, sec,
723 insn->sec, insn->offset)) {
724 WARN("elf_add_reloc_to_insn: .retpoline_sites");
734 static int create_mcount_loc_sections(struct objtool_file *file)
738 struct instruction *insn;
741 sec = find_section_by_name(file->elf, "__mcount_loc");
743 INIT_LIST_HEAD(&file->mcount_loc_list);
744 WARN("file already has __mcount_loc section, skipping");
748 if (list_empty(&file->mcount_loc_list))
752 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
755 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
760 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
762 loc = (unsigned long *)sec->data->d_buf + idx;
763 memset(loc, 0, sizeof(unsigned long));
765 if (elf_add_reloc_to_insn(file->elf, sec,
766 idx * sizeof(unsigned long),
768 insn->sec, insn->offset))
778 * Warnings shouldn't be reported for ignored functions.
780 static void add_ignores(struct objtool_file *file)
782 struct instruction *insn;
787 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
791 list_for_each_entry(reloc, &sec->reloc_list, list) {
792 switch (reloc->sym->type) {
798 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
804 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
808 func_for_each_insn(file, func, insn)
814 * This is a whitelist of functions that is allowed to be called with AC set.
815 * The list is meant to be minimal and only contains compiler instrumentation
816 * ABI and a few functions used to implement *_{to,from}_user() functions.
818 * These functions must not directly change AC, but may PUSHF/POPF.
820 static const char *uaccess_safe_builtin[] = {
824 /* KASAN out-of-line */
825 "__asan_loadN_noabort",
826 "__asan_load1_noabort",
827 "__asan_load2_noabort",
828 "__asan_load4_noabort",
829 "__asan_load8_noabort",
830 "__asan_load16_noabort",
831 "__asan_storeN_noabort",
832 "__asan_store1_noabort",
833 "__asan_store2_noabort",
834 "__asan_store4_noabort",
835 "__asan_store8_noabort",
836 "__asan_store16_noabort",
837 "__kasan_check_read",
838 "__kasan_check_write",
840 "__asan_report_load_n_noabort",
841 "__asan_report_load1_noabort",
842 "__asan_report_load2_noabort",
843 "__asan_report_load4_noabort",
844 "__asan_report_load8_noabort",
845 "__asan_report_load16_noabort",
846 "__asan_report_store_n_noabort",
847 "__asan_report_store1_noabort",
848 "__asan_report_store2_noabort",
849 "__asan_report_store4_noabort",
850 "__asan_report_store8_noabort",
851 "__asan_report_store16_noabort",
853 "__kcsan_check_access",
858 "kcsan_found_watchpoint",
859 "kcsan_setup_watchpoint",
860 "kcsan_check_scoped_accesses",
861 "kcsan_disable_current",
862 "kcsan_enable_current_nowarn",
867 "__tsan_write_range",
878 "__tsan_read_write1",
879 "__tsan_read_write2",
880 "__tsan_read_write4",
881 "__tsan_read_write8",
882 "__tsan_read_write16",
883 "__tsan_atomic8_load",
884 "__tsan_atomic16_load",
885 "__tsan_atomic32_load",
886 "__tsan_atomic64_load",
887 "__tsan_atomic8_store",
888 "__tsan_atomic16_store",
889 "__tsan_atomic32_store",
890 "__tsan_atomic64_store",
891 "__tsan_atomic8_exchange",
892 "__tsan_atomic16_exchange",
893 "__tsan_atomic32_exchange",
894 "__tsan_atomic64_exchange",
895 "__tsan_atomic8_fetch_add",
896 "__tsan_atomic16_fetch_add",
897 "__tsan_atomic32_fetch_add",
898 "__tsan_atomic64_fetch_add",
899 "__tsan_atomic8_fetch_sub",
900 "__tsan_atomic16_fetch_sub",
901 "__tsan_atomic32_fetch_sub",
902 "__tsan_atomic64_fetch_sub",
903 "__tsan_atomic8_fetch_and",
904 "__tsan_atomic16_fetch_and",
905 "__tsan_atomic32_fetch_and",
906 "__tsan_atomic64_fetch_and",
907 "__tsan_atomic8_fetch_or",
908 "__tsan_atomic16_fetch_or",
909 "__tsan_atomic32_fetch_or",
910 "__tsan_atomic64_fetch_or",
911 "__tsan_atomic8_fetch_xor",
912 "__tsan_atomic16_fetch_xor",
913 "__tsan_atomic32_fetch_xor",
914 "__tsan_atomic64_fetch_xor",
915 "__tsan_atomic8_fetch_nand",
916 "__tsan_atomic16_fetch_nand",
917 "__tsan_atomic32_fetch_nand",
918 "__tsan_atomic64_fetch_nand",
919 "__tsan_atomic8_compare_exchange_strong",
920 "__tsan_atomic16_compare_exchange_strong",
921 "__tsan_atomic32_compare_exchange_strong",
922 "__tsan_atomic64_compare_exchange_strong",
923 "__tsan_atomic8_compare_exchange_weak",
924 "__tsan_atomic16_compare_exchange_weak",
925 "__tsan_atomic32_compare_exchange_weak",
926 "__tsan_atomic64_compare_exchange_weak",
927 "__tsan_atomic8_compare_exchange_val",
928 "__tsan_atomic16_compare_exchange_val",
929 "__tsan_atomic32_compare_exchange_val",
930 "__tsan_atomic64_compare_exchange_val",
931 "__tsan_atomic_thread_fence",
932 "__tsan_atomic_signal_fence",
936 "__sanitizer_cov_trace_pc",
937 "__sanitizer_cov_trace_const_cmp1",
938 "__sanitizer_cov_trace_const_cmp2",
939 "__sanitizer_cov_trace_const_cmp4",
940 "__sanitizer_cov_trace_const_cmp8",
941 "__sanitizer_cov_trace_cmp1",
942 "__sanitizer_cov_trace_cmp2",
943 "__sanitizer_cov_trace_cmp4",
944 "__sanitizer_cov_trace_cmp8",
945 "__sanitizer_cov_trace_switch",
947 "ubsan_type_mismatch_common",
948 "__ubsan_handle_type_mismatch",
949 "__ubsan_handle_type_mismatch_v1",
950 "__ubsan_handle_shift_out_of_bounds",
952 "csum_partial_copy_generic",
954 "copy_mc_fragile_handle_tail",
955 "copy_mc_enhanced_fast_string",
956 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
960 static void add_uaccess_safe(struct objtool_file *file)
968 for (name = uaccess_safe_builtin; *name; name++) {
969 func = find_symbol_by_name(file->elf, *name);
973 func->uaccess_safe = true;
978 * FIXME: For now, just ignore any alternatives which add retpolines. This is
979 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
980 * But it at least allows objtool to understand the control flow *around* the
983 static int add_ignore_alternatives(struct objtool_file *file)
987 struct instruction *insn;
989 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
993 list_for_each_entry(reloc, &sec->reloc_list, list) {
994 if (reloc->sym->type != STT_SECTION) {
995 WARN("unexpected relocation symbol type in %s", sec->name);
999 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1001 WARN("bad .discard.ignore_alts entry");
1005 insn->ignore_alts = true;
1011 __weak bool arch_is_retpoline(struct symbol *sym)
1016 #define NEGATIVE_RELOC ((void *)-1L)
1018 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1020 if (insn->reloc == NEGATIVE_RELOC)
1027 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1028 insn->offset, insn->len);
1030 insn->reloc = NEGATIVE_RELOC;
1038 static void remove_insn_ops(struct instruction *insn)
1040 struct stack_op *op, *tmp;
1042 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1043 list_del(&op->list);
1048 static void annotate_call_site(struct objtool_file *file,
1049 struct instruction *insn, bool sibling)
1051 struct reloc *reloc = insn_reloc(file, insn);
1052 struct symbol *sym = insn->call_dest;
1058 * Alternative replacement code is just template code which is
1059 * sometimes copied to the original instruction. For now, don't
1060 * annotate it. (In the future we might consider annotating the
1061 * original instruction if/when it ever makes sense to do so.)
1063 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1066 if (sym->static_call_tramp) {
1067 list_add_tail(&insn->call_node, &file->static_call_list);
1071 if (sym->retpoline_thunk) {
1072 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1077 * Many compilers cannot disable KCOV or sanitizer calls with a function
1078 * attribute so they need a little help, NOP out any such calls from
1081 if (insn->sec->noinstr && sym->profiling_func) {
1083 reloc->type = R_NONE;
1084 elf_write_reloc(file->elf, reloc);
1087 elf_write_insn(file->elf, insn->sec,
1088 insn->offset, insn->len,
1089 sibling ? arch_ret_insn(insn->len)
1090 : arch_nop_insn(insn->len));
1092 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1096 if (mcount && sym->fentry) {
1098 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1101 reloc->type = R_NONE;
1102 elf_write_reloc(file->elf, reloc);
1105 elf_write_insn(file->elf, insn->sec,
1106 insn->offset, insn->len,
1107 arch_nop_insn(insn->len));
1109 insn->type = INSN_NOP;
1111 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1116 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1117 struct symbol *dest, bool sibling)
1119 insn->call_dest = dest;
1124 * Whatever stack impact regular CALLs have, should be undone
1125 * by the RETURN of the called function.
1127 * Annotated intra-function calls retain the stack_ops but
1128 * are converted to JUMP, see read_intra_function_calls().
1130 remove_insn_ops(insn);
1132 annotate_call_site(file, insn, sibling);
1135 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1138 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1139 * so convert them accordingly.
1141 switch (insn->type) {
1143 insn->type = INSN_CALL_DYNAMIC;
1145 case INSN_JUMP_UNCONDITIONAL:
1146 insn->type = INSN_JUMP_DYNAMIC;
1148 case INSN_JUMP_CONDITIONAL:
1149 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1155 insn->retpoline_safe = true;
1158 * Whatever stack impact regular CALLs have, should be undone
1159 * by the RETURN of the called function.
1161 * Annotated intra-function calls retain the stack_ops but
1162 * are converted to JUMP, see read_intra_function_calls().
1164 remove_insn_ops(insn);
1166 annotate_call_site(file, insn, false);
1169 * Find the destination instructions for all jumps.
1171 static int add_jump_destinations(struct objtool_file *file)
1173 struct instruction *insn;
1174 struct reloc *reloc;
1175 struct section *dest_sec;
1176 unsigned long dest_off;
1178 for_each_insn(file, insn) {
1179 if (!is_static_jump(insn))
1182 reloc = insn_reloc(file, insn);
1184 dest_sec = insn->sec;
1185 dest_off = arch_jump_destination(insn);
1186 } else if (reloc->sym->type == STT_SECTION) {
1187 dest_sec = reloc->sym->sec;
1188 dest_off = arch_dest_reloc_offset(reloc->addend);
1189 } else if (reloc->sym->retpoline_thunk) {
1190 add_retpoline_call(file, insn);
1192 } else if (insn->func) {
1193 /* internal or external sibling call (with reloc) */
1194 add_call_dest(file, insn, reloc->sym, true);
1196 } else if (reloc->sym->sec->idx) {
1197 dest_sec = reloc->sym->sec;
1198 dest_off = reloc->sym->sym.st_value +
1199 arch_dest_reloc_offset(reloc->addend);
1201 /* non-func asm code jumping to another file */
1205 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1206 if (!insn->jump_dest) {
1209 * This is a special case where an alt instruction
1210 * jumps past the end of the section. These are
1211 * handled later in handle_group_alt().
1213 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1216 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1217 insn->sec, insn->offset, dest_sec->name,
1223 * Cross-function jump.
1225 if (insn->func && insn->jump_dest->func &&
1226 insn->func != insn->jump_dest->func) {
1229 * For GCC 8+, create parent/child links for any cold
1230 * subfunctions. This is _mostly_ redundant with a
1231 * similar initialization in read_symbols().
1233 * If a function has aliases, we want the *first* such
1234 * function in the symbol table to be the subfunction's
1235 * parent. In that case we overwrite the
1236 * initialization done in read_symbols().
1238 * However this code can't completely replace the
1239 * read_symbols() code because this doesn't detect the
1240 * case where the parent function's only reference to a
1241 * subfunction is through a jump table.
1243 if (!strstr(insn->func->name, ".cold") &&
1244 strstr(insn->jump_dest->func->name, ".cold")) {
1245 insn->func->cfunc = insn->jump_dest->func;
1246 insn->jump_dest->func->pfunc = insn->func;
1248 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1249 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1250 /* internal sibling call (without reloc) */
1251 add_call_dest(file, insn, insn->jump_dest->func, true);
1259 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1261 struct symbol *call_dest;
1263 call_dest = find_func_by_offset(sec, offset);
1265 call_dest = find_symbol_by_offset(sec, offset);
1271 * Find the destination instructions for all calls.
1273 static int add_call_destinations(struct objtool_file *file)
1275 struct instruction *insn;
1276 unsigned long dest_off;
1277 struct symbol *dest;
1278 struct reloc *reloc;
1280 for_each_insn(file, insn) {
1281 if (insn->type != INSN_CALL)
1284 reloc = insn_reloc(file, insn);
1286 dest_off = arch_jump_destination(insn);
1287 dest = find_call_destination(insn->sec, dest_off);
1289 add_call_dest(file, insn, dest, false);
1294 if (!insn->call_dest) {
1295 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1299 if (insn->func && insn->call_dest->type != STT_FUNC) {
1300 WARN_FUNC("unsupported call to non-function",
1301 insn->sec, insn->offset);
1305 } else if (reloc->sym->type == STT_SECTION) {
1306 dest_off = arch_dest_reloc_offset(reloc->addend);
1307 dest = find_call_destination(reloc->sym->sec, dest_off);
1309 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1310 insn->sec, insn->offset,
1311 reloc->sym->sec->name,
1316 add_call_dest(file, insn, dest, false);
1318 } else if (reloc->sym->retpoline_thunk) {
1319 add_retpoline_call(file, insn);
1322 add_call_dest(file, insn, reloc->sym, false);
1329 * The .alternatives section requires some extra special care over and above
1330 * other special sections because alternatives are patched in place.
1332 static int handle_group_alt(struct objtool_file *file,
1333 struct special_alt *special_alt,
1334 struct instruction *orig_insn,
1335 struct instruction **new_insn)
1337 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1338 struct alt_group *orig_alt_group, *new_alt_group;
1339 unsigned long dest_off;
1342 orig_alt_group = malloc(sizeof(*orig_alt_group));
1343 if (!orig_alt_group) {
1344 WARN("malloc failed");
1347 orig_alt_group->cfi = calloc(special_alt->orig_len,
1348 sizeof(struct cfi_state *));
1349 if (!orig_alt_group->cfi) {
1350 WARN("calloc failed");
1354 last_orig_insn = NULL;
1356 sec_for_each_insn_from(file, insn) {
1357 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1360 insn->alt_group = orig_alt_group;
1361 last_orig_insn = insn;
1363 orig_alt_group->orig_group = NULL;
1364 orig_alt_group->first_insn = orig_insn;
1365 orig_alt_group->last_insn = last_orig_insn;
1368 new_alt_group = malloc(sizeof(*new_alt_group));
1369 if (!new_alt_group) {
1370 WARN("malloc failed");
1374 if (special_alt->new_len < special_alt->orig_len) {
1376 * Insert a fake nop at the end to make the replacement
1377 * alt_group the same size as the original. This is needed to
1378 * allow propagate_alt_cfi() to do its magic. When the last
1379 * instruction affects the stack, the instruction after it (the
1380 * nop) will propagate the new state to the shared CFI array.
1382 nop = malloc(sizeof(*nop));
1384 WARN("malloc failed");
1387 memset(nop, 0, sizeof(*nop));
1388 INIT_LIST_HEAD(&nop->alts);
1389 INIT_LIST_HEAD(&nop->stack_ops);
1391 nop->sec = special_alt->new_sec;
1392 nop->offset = special_alt->new_off + special_alt->new_len;
1393 nop->len = special_alt->orig_len - special_alt->new_len;
1394 nop->type = INSN_NOP;
1395 nop->func = orig_insn->func;
1396 nop->alt_group = new_alt_group;
1397 nop->ignore = orig_insn->ignore_alts;
1400 if (!special_alt->new_len) {
1406 sec_for_each_insn_from(file, insn) {
1407 struct reloc *alt_reloc;
1409 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1412 last_new_insn = insn;
1414 insn->ignore = orig_insn->ignore_alts;
1415 insn->func = orig_insn->func;
1416 insn->alt_group = new_alt_group;
1419 * Since alternative replacement code is copy/pasted by the
1420 * kernel after applying relocations, generally such code can't
1421 * have relative-address relocation references to outside the
1422 * .altinstr_replacement section, unless the arch's
1423 * alternatives code can adjust the relative offsets
1426 alt_reloc = insn_reloc(file, insn);
1428 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1430 WARN_FUNC("unsupported relocation in alternatives section",
1431 insn->sec, insn->offset);
1435 if (!is_static_jump(insn))
1438 if (!insn->immediate)
1441 dest_off = arch_jump_destination(insn);
1442 if (dest_off == special_alt->new_off + special_alt->new_len)
1443 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1445 if (!insn->jump_dest) {
1446 WARN_FUNC("can't find alternative jump destination",
1447 insn->sec, insn->offset);
1452 if (!last_new_insn) {
1453 WARN_FUNC("can't find last new alternative instruction",
1454 special_alt->new_sec, special_alt->new_off);
1459 list_add(&nop->list, &last_new_insn->list);
1461 new_alt_group->orig_group = orig_alt_group;
1462 new_alt_group->first_insn = *new_insn;
1463 new_alt_group->last_insn = nop ? : last_new_insn;
1464 new_alt_group->cfi = orig_alt_group->cfi;
1469 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1470 * If the original instruction is a jump, make the alt entry an effective nop
1471 * by just skipping the original instruction.
1473 static int handle_jump_alt(struct objtool_file *file,
1474 struct special_alt *special_alt,
1475 struct instruction *orig_insn,
1476 struct instruction **new_insn)
1478 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1479 orig_insn->type != INSN_NOP) {
1481 WARN_FUNC("unsupported instruction at jump label",
1482 orig_insn->sec, orig_insn->offset);
1486 if (special_alt->key_addend & 2) {
1487 struct reloc *reloc = insn_reloc(file, orig_insn);
1490 reloc->type = R_NONE;
1491 elf_write_reloc(file->elf, reloc);
1493 elf_write_insn(file->elf, orig_insn->sec,
1494 orig_insn->offset, orig_insn->len,
1495 arch_nop_insn(orig_insn->len));
1496 orig_insn->type = INSN_NOP;
1499 if (orig_insn->type == INSN_NOP) {
1500 if (orig_insn->len == 2)
1501 file->jl_nop_short++;
1503 file->jl_nop_long++;
1508 if (orig_insn->len == 2)
1513 *new_insn = list_next_entry(orig_insn, list);
1518 * Read all the special sections which have alternate instructions which can be
1519 * patched in or redirected to at runtime. Each instruction having alternate
1520 * instruction(s) has them added to its insn->alts list, which will be
1521 * traversed in validate_branch().
1523 static int add_special_section_alts(struct objtool_file *file)
1525 struct list_head special_alts;
1526 struct instruction *orig_insn, *new_insn;
1527 struct special_alt *special_alt, *tmp;
1528 struct alternative *alt;
1531 ret = special_get_alts(file->elf, &special_alts);
1535 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1537 orig_insn = find_insn(file, special_alt->orig_sec,
1538 special_alt->orig_off);
1540 WARN_FUNC("special: can't find orig instruction",
1541 special_alt->orig_sec, special_alt->orig_off);
1547 if (!special_alt->group || special_alt->new_len) {
1548 new_insn = find_insn(file, special_alt->new_sec,
1549 special_alt->new_off);
1551 WARN_FUNC("special: can't find new instruction",
1552 special_alt->new_sec,
1553 special_alt->new_off);
1559 if (special_alt->group) {
1560 if (!special_alt->orig_len) {
1561 WARN_FUNC("empty alternative entry",
1562 orig_insn->sec, orig_insn->offset);
1566 ret = handle_group_alt(file, special_alt, orig_insn,
1570 } else if (special_alt->jump_or_nop) {
1571 ret = handle_jump_alt(file, special_alt, orig_insn,
1577 alt = malloc(sizeof(*alt));
1579 WARN("malloc failed");
1584 alt->insn = new_insn;
1585 alt->skip_orig = special_alt->skip_orig;
1586 orig_insn->ignore_alts |= special_alt->skip_alt;
1587 list_add_tail(&alt->list, &orig_insn->alts);
1589 list_del(&special_alt->list);
1594 printf("jl\\\tNOP\tJMP\n");
1595 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1596 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1603 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1604 struct reloc *table)
1606 struct reloc *reloc = table;
1607 struct instruction *dest_insn;
1608 struct alternative *alt;
1609 struct symbol *pfunc = insn->func->pfunc;
1610 unsigned int prev_offset = 0;
1613 * Each @reloc is a switch table relocation which points to the target
1616 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1618 /* Check for the end of the table: */
1619 if (reloc != table && reloc->jump_table_start)
1622 /* Make sure the table entries are consecutive: */
1623 if (prev_offset && reloc->offset != prev_offset + 8)
1626 /* Detect function pointers from contiguous objects: */
1627 if (reloc->sym->sec == pfunc->sec &&
1628 reloc->addend == pfunc->offset)
1631 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1635 /* Make sure the destination is in the same function: */
1636 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1639 alt = malloc(sizeof(*alt));
1641 WARN("malloc failed");
1645 alt->insn = dest_insn;
1646 list_add_tail(&alt->list, &insn->alts);
1647 prev_offset = reloc->offset;
1651 WARN_FUNC("can't find switch jump table",
1652 insn->sec, insn->offset);
1660 * find_jump_table() - Given a dynamic jump, find the switch jump table
1661 * associated with it.
1663 static struct reloc *find_jump_table(struct objtool_file *file,
1664 struct symbol *func,
1665 struct instruction *insn)
1667 struct reloc *table_reloc;
1668 struct instruction *dest_insn, *orig_insn = insn;
1671 * Backward search using the @first_jump_src links, these help avoid
1672 * much of the 'in between' code. Which avoids us getting confused by
1676 insn && insn->func && insn->func->pfunc == func;
1677 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1679 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1682 /* allow small jumps within the range */
1683 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1685 (insn->jump_dest->offset <= insn->offset ||
1686 insn->jump_dest->offset > orig_insn->offset))
1689 table_reloc = arch_find_switch_table(file, insn);
1692 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1693 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1703 * First pass: Mark the head of each jump table so that in the next pass,
1704 * we know when a given jump table ends and the next one starts.
1706 static void mark_func_jump_tables(struct objtool_file *file,
1707 struct symbol *func)
1709 struct instruction *insn, *last = NULL;
1710 struct reloc *reloc;
1712 func_for_each_insn(file, func, insn) {
1717 * Store back-pointers for unconditional forward jumps such
1718 * that find_jump_table() can back-track using those and
1719 * avoid some potentially confusing code.
1721 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1722 insn->offset > last->offset &&
1723 insn->jump_dest->offset > insn->offset &&
1724 !insn->jump_dest->first_jump_src) {
1726 insn->jump_dest->first_jump_src = insn;
1727 last = insn->jump_dest;
1730 if (insn->type != INSN_JUMP_DYNAMIC)
1733 reloc = find_jump_table(file, func, insn);
1735 reloc->jump_table_start = true;
1736 insn->jump_table = reloc;
1741 static int add_func_jump_tables(struct objtool_file *file,
1742 struct symbol *func)
1744 struct instruction *insn;
1747 func_for_each_insn(file, func, insn) {
1748 if (!insn->jump_table)
1751 ret = add_jump_table(file, insn, insn->jump_table);
1760 * For some switch statements, gcc generates a jump table in the .rodata
1761 * section which contains a list of addresses within the function to jump to.
1762 * This finds these jump tables and adds them to the insn->alts lists.
1764 static int add_jump_table_alts(struct objtool_file *file)
1766 struct section *sec;
1767 struct symbol *func;
1773 for_each_sec(file, sec) {
1774 list_for_each_entry(func, &sec->symbol_list, list) {
1775 if (func->type != STT_FUNC)
1778 mark_func_jump_tables(file, func);
1779 ret = add_func_jump_tables(file, func);
1788 static void set_func_state(struct cfi_state *state)
1790 state->cfa = initial_func_cfi.cfa;
1791 memcpy(&state->regs, &initial_func_cfi.regs,
1792 CFI_NUM_REGS * sizeof(struct cfi_reg));
1793 state->stack_size = initial_func_cfi.cfa.offset;
1796 static int read_unwind_hints(struct objtool_file *file)
1798 struct cfi_state cfi = init_cfi;
1799 struct section *sec, *relocsec;
1800 struct unwind_hint *hint;
1801 struct instruction *insn;
1802 struct reloc *reloc;
1805 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1809 relocsec = sec->reloc;
1811 WARN("missing .rela.discard.unwind_hints section");
1815 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1816 WARN("struct unwind_hint size mismatch");
1822 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1823 hint = (struct unwind_hint *)sec->data->d_buf + i;
1825 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1827 WARN("can't find reloc for unwind_hints[%d]", i);
1831 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1833 WARN("can't find insn for unwind_hints[%d]", i);
1839 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1840 insn->cfi = &func_cfi;
1847 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1848 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1849 insn->sec, insn->offset, hint->sp_reg);
1853 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1854 cfi.type = hint->type;
1855 cfi.end = hint->end;
1857 insn->cfi = cfi_hash_find_or_add(&cfi);
1863 static int read_retpoline_hints(struct objtool_file *file)
1865 struct section *sec;
1866 struct instruction *insn;
1867 struct reloc *reloc;
1869 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1873 list_for_each_entry(reloc, &sec->reloc_list, list) {
1874 if (reloc->sym->type != STT_SECTION) {
1875 WARN("unexpected relocation symbol type in %s", sec->name);
1879 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1881 WARN("bad .discard.retpoline_safe entry");
1885 if (insn->type != INSN_JUMP_DYNAMIC &&
1886 insn->type != INSN_CALL_DYNAMIC) {
1887 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1888 insn->sec, insn->offset);
1892 insn->retpoline_safe = true;
1898 static int read_instr_hints(struct objtool_file *file)
1900 struct section *sec;
1901 struct instruction *insn;
1902 struct reloc *reloc;
1904 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1908 list_for_each_entry(reloc, &sec->reloc_list, list) {
1909 if (reloc->sym->type != STT_SECTION) {
1910 WARN("unexpected relocation symbol type in %s", sec->name);
1914 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1916 WARN("bad .discard.instr_end entry");
1923 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1927 list_for_each_entry(reloc, &sec->reloc_list, list) {
1928 if (reloc->sym->type != STT_SECTION) {
1929 WARN("unexpected relocation symbol type in %s", sec->name);
1933 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1935 WARN("bad .discard.instr_begin entry");
1945 static int read_intra_function_calls(struct objtool_file *file)
1947 struct instruction *insn;
1948 struct section *sec;
1949 struct reloc *reloc;
1951 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1955 list_for_each_entry(reloc, &sec->reloc_list, list) {
1956 unsigned long dest_off;
1958 if (reloc->sym->type != STT_SECTION) {
1959 WARN("unexpected relocation symbol type in %s",
1964 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1966 WARN("bad .discard.intra_function_call entry");
1970 if (insn->type != INSN_CALL) {
1971 WARN_FUNC("intra_function_call not a direct call",
1972 insn->sec, insn->offset);
1977 * Treat intra-function CALLs as JMPs, but with a stack_op.
1978 * See add_call_destinations(), which strips stack_ops from
1981 insn->type = INSN_JUMP_UNCONDITIONAL;
1983 dest_off = insn->offset + insn->len + insn->immediate;
1984 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1985 if (!insn->jump_dest) {
1986 WARN_FUNC("can't find call dest at %s+0x%lx",
1987 insn->sec, insn->offset,
1988 insn->sec->name, dest_off);
1997 * Return true if name matches an instrumentation function, where calls to that
1998 * function from noinstr code can safely be removed, but compilers won't do so.
2000 static bool is_profiling_func(const char *name)
2003 * Many compilers cannot disable KCOV with a function attribute.
2005 if (!strncmp(name, "__sanitizer_cov_", 16))
2009 * Some compilers currently do not remove __tsan_func_entry/exit nor
2010 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2011 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2012 * minimum Clang version is 14.0, this can be removed.
2014 if (!strncmp(name, "__tsan_func_", 12) ||
2015 !strcmp(name, "__tsan_atomic_signal_fence"))
2021 static int classify_symbols(struct objtool_file *file)
2023 struct section *sec;
2024 struct symbol *func;
2026 for_each_sec(file, sec) {
2027 list_for_each_entry(func, &sec->symbol_list, list) {
2028 if (func->bind != STB_GLOBAL)
2031 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2032 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2033 func->static_call_tramp = true;
2035 if (arch_is_retpoline(func))
2036 func->retpoline_thunk = true;
2038 if (!strcmp(func->name, "__fentry__"))
2039 func->fentry = true;
2041 if (is_profiling_func(func->name))
2042 func->profiling_func = true;
2049 static void mark_rodata(struct objtool_file *file)
2051 struct section *sec;
2055 * Search for the following rodata sections, each of which can
2056 * potentially contain jump tables:
2058 * - .rodata: can contain GCC switch tables
2059 * - .rodata.<func>: same, if -fdata-sections is being used
2060 * - .rodata..c_jump_table: contains C annotated jump tables
2062 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2064 for_each_sec(file, sec) {
2065 if (!strncmp(sec->name, ".rodata", 7) &&
2066 !strstr(sec->name, ".str1.")) {
2072 file->rodata = found;
2075 static int decode_sections(struct objtool_file *file)
2081 ret = init_pv_ops(file);
2085 ret = decode_instructions(file);
2089 ret = add_dead_ends(file);
2094 add_uaccess_safe(file);
2096 ret = add_ignore_alternatives(file);
2101 * Must be before add_{jump_call}_destination.
2103 ret = classify_symbols(file);
2108 * Must be before add_special_section_alts() as that depends on
2109 * jump_dest being set.
2111 ret = add_jump_destinations(file);
2115 ret = add_special_section_alts(file);
2120 * Must be before add_call_destination(); it changes INSN_CALL to
2123 ret = read_intra_function_calls(file);
2127 ret = add_call_destinations(file);
2131 ret = add_jump_table_alts(file);
2135 ret = read_unwind_hints(file);
2139 ret = read_retpoline_hints(file);
2143 ret = read_instr_hints(file);
2150 static bool is_fentry_call(struct instruction *insn)
2152 if (insn->type == INSN_CALL &&
2154 insn->call_dest->fentry)
2160 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2162 struct cfi_state *cfi = &state->cfi;
2165 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2168 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2171 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2174 for (i = 0; i < CFI_NUM_REGS; i++) {
2175 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2176 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2183 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2184 int expected_offset)
2186 return reg->base == CFI_CFA &&
2187 reg->offset == expected_offset;
2190 static bool has_valid_stack_frame(struct insn_state *state)
2192 struct cfi_state *cfi = &state->cfi;
2194 if (cfi->cfa.base == CFI_BP &&
2195 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2196 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2199 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2205 static int update_cfi_state_regs(struct instruction *insn,
2206 struct cfi_state *cfi,
2207 struct stack_op *op)
2209 struct cfi_reg *cfa = &cfi->cfa;
2211 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2215 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2219 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2222 /* add immediate to sp */
2223 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2224 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2225 cfa->offset -= op->src.offset;
2230 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2232 if (arch_callee_saved_reg(reg) &&
2233 cfi->regs[reg].base == CFI_UNDEFINED) {
2234 cfi->regs[reg].base = base;
2235 cfi->regs[reg].offset = offset;
2239 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2241 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2242 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2246 * A note about DRAP stack alignment:
2248 * GCC has the concept of a DRAP register, which is used to help keep track of
2249 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2250 * register. The typical DRAP pattern is:
2252 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2253 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2254 * 41 ff 72 f8 pushq -0x8(%r10)
2256 * 48 89 e5 mov %rsp,%rbp
2263 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2266 * There are some variations in the epilogues, like:
2274 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2279 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2280 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2281 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2282 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2284 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2287 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2288 * restored beforehand:
2291 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2292 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2294 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2298 static int update_cfi_state(struct instruction *insn,
2299 struct instruction *next_insn,
2300 struct cfi_state *cfi, struct stack_op *op)
2302 struct cfi_reg *cfa = &cfi->cfa;
2303 struct cfi_reg *regs = cfi->regs;
2305 /* stack operations don't make sense with an undefined CFA */
2306 if (cfa->base == CFI_UNDEFINED) {
2308 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2314 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2315 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2316 return update_cfi_state_regs(insn, cfi, op);
2318 switch (op->dest.type) {
2321 switch (op->src.type) {
2324 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2325 cfa->base == CFI_SP &&
2326 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2328 /* mov %rsp, %rbp */
2329 cfa->base = op->dest.reg;
2330 cfi->bp_scratch = false;
2333 else if (op->src.reg == CFI_SP &&
2334 op->dest.reg == CFI_BP && cfi->drap) {
2336 /* drap: mov %rsp, %rbp */
2337 regs[CFI_BP].base = CFI_BP;
2338 regs[CFI_BP].offset = -cfi->stack_size;
2339 cfi->bp_scratch = false;
2342 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2347 * This is needed for the rare case where GCC
2354 cfi->vals[op->dest.reg].base = CFI_CFA;
2355 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2358 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2359 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2364 * Restore the original stack pointer (Clang).
2366 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2369 else if (op->dest.reg == cfa->base) {
2371 /* mov %reg, %rsp */
2372 if (cfa->base == CFI_SP &&
2373 cfi->vals[op->src.reg].base == CFI_CFA) {
2376 * This is needed for the rare case
2377 * where GCC does something dumb like:
2379 * lea 0x8(%rsp), %rcx
2383 cfa->offset = -cfi->vals[op->src.reg].offset;
2384 cfi->stack_size = cfa->offset;
2386 } else if (cfa->base == CFI_SP &&
2387 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2388 cfi->vals[op->src.reg].offset == cfa->offset) {
2393 * 1: mov %rsp, (%[tos])
2394 * 2: mov %[tos], %rsp
2400 * 1 - places a pointer to the previous
2401 * stack at the Top-of-Stack of the
2404 * 2 - switches to the new stack.
2406 * 3 - pops the Top-of-Stack to restore
2407 * the original stack.
2409 * Note: we set base to SP_INDIRECT
2410 * here and preserve offset. Therefore
2411 * when the unwinder reaches ToS it
2412 * will dereference SP and then add the
2413 * offset to find the next frame, IOW:
2416 cfa->base = CFI_SP_INDIRECT;
2419 cfa->base = CFI_UNDEFINED;
2424 else if (op->dest.reg == CFI_SP &&
2425 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2426 cfi->vals[op->src.reg].offset == cfa->offset) {
2429 * The same stack swizzle case 2) as above. But
2430 * because we can't change cfa->base, case 3)
2431 * will become a regular POP. Pretend we're a
2432 * PUSH so things don't go unbalanced.
2434 cfi->stack_size += 8;
2441 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2444 cfi->stack_size -= op->src.offset;
2445 if (cfa->base == CFI_SP)
2446 cfa->offset -= op->src.offset;
2450 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2452 /* lea disp(%rbp), %rsp */
2453 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2457 if (!cfi->drap && op->src.reg == CFI_SP &&
2458 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2459 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2461 /* lea disp(%rsp), %rbp */
2463 cfa->offset -= op->src.offset;
2464 cfi->bp_scratch = false;
2468 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2470 /* drap: lea disp(%rsp), %drap */
2471 cfi->drap_reg = op->dest.reg;
2474 * lea disp(%rsp), %reg
2476 * This is needed for the rare case where GCC
2477 * does something dumb like:
2479 * lea 0x8(%rsp), %rcx
2483 cfi->vals[op->dest.reg].base = CFI_CFA;
2484 cfi->vals[op->dest.reg].offset = \
2485 -cfi->stack_size + op->src.offset;
2490 if (cfi->drap && op->dest.reg == CFI_SP &&
2491 op->src.reg == cfi->drap_reg) {
2493 /* drap: lea disp(%drap), %rsp */
2495 cfa->offset = cfi->stack_size = -op->src.offset;
2496 cfi->drap_reg = CFI_UNDEFINED;
2501 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2502 WARN_FUNC("unsupported stack register modification",
2503 insn->sec, insn->offset);
2510 if (op->dest.reg != CFI_SP ||
2511 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2512 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2513 WARN_FUNC("unsupported stack pointer realignment",
2514 insn->sec, insn->offset);
2518 if (cfi->drap_reg != CFI_UNDEFINED) {
2519 /* drap: and imm, %rsp */
2520 cfa->base = cfi->drap_reg;
2521 cfa->offset = cfi->stack_size = 0;
2526 * Older versions of GCC (4.8ish) realign the stack
2527 * without DRAP, with a frame pointer.
2534 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2536 /* pop %rsp; # restore from a stack swizzle */
2541 if (!cfi->drap && op->dest.reg == cfa->base) {
2547 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2548 op->dest.reg == cfi->drap_reg &&
2549 cfi->drap_offset == -cfi->stack_size) {
2551 /* drap: pop %drap */
2552 cfa->base = cfi->drap_reg;
2554 cfi->drap_offset = -1;
2556 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2559 restore_reg(cfi, op->dest.reg);
2562 cfi->stack_size -= 8;
2563 if (cfa->base == CFI_SP)
2568 case OP_SRC_REG_INDIRECT:
2569 if (!cfi->drap && op->dest.reg == cfa->base &&
2570 op->dest.reg == CFI_BP) {
2572 /* mov disp(%rsp), %rbp */
2574 cfa->offset = cfi->stack_size;
2577 if (cfi->drap && op->src.reg == CFI_BP &&
2578 op->src.offset == cfi->drap_offset) {
2580 /* drap: mov disp(%rbp), %drap */
2581 cfa->base = cfi->drap_reg;
2583 cfi->drap_offset = -1;
2586 if (cfi->drap && op->src.reg == CFI_BP &&
2587 op->src.offset == regs[op->dest.reg].offset) {
2589 /* drap: mov disp(%rbp), %reg */
2590 restore_reg(cfi, op->dest.reg);
2592 } else if (op->src.reg == cfa->base &&
2593 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2595 /* mov disp(%rbp), %reg */
2596 /* mov disp(%rsp), %reg */
2597 restore_reg(cfi, op->dest.reg);
2599 } else if (op->src.reg == CFI_SP &&
2600 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2602 /* mov disp(%rsp), %reg */
2603 restore_reg(cfi, op->dest.reg);
2609 WARN_FUNC("unknown stack-related instruction",
2610 insn->sec, insn->offset);
2618 cfi->stack_size += 8;
2619 if (cfa->base == CFI_SP)
2622 if (op->src.type != OP_SRC_REG)
2626 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2628 /* drap: push %drap */
2629 cfa->base = CFI_BP_INDIRECT;
2630 cfa->offset = -cfi->stack_size;
2632 /* save drap so we know when to restore it */
2633 cfi->drap_offset = -cfi->stack_size;
2635 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2637 /* drap: push %rbp */
2638 cfi->stack_size = 0;
2642 /* drap: push %reg */
2643 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2649 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2652 /* detect when asm code uses rbp as a scratch register */
2653 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2654 cfa->base != CFI_BP)
2655 cfi->bp_scratch = true;
2658 case OP_DEST_REG_INDIRECT:
2661 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2663 /* drap: mov %drap, disp(%rbp) */
2664 cfa->base = CFI_BP_INDIRECT;
2665 cfa->offset = op->dest.offset;
2667 /* save drap offset so we know when to restore it */
2668 cfi->drap_offset = op->dest.offset;
2671 /* drap: mov reg, disp(%rbp) */
2672 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2675 } else if (op->dest.reg == cfa->base) {
2677 /* mov reg, disp(%rbp) */
2678 /* mov reg, disp(%rsp) */
2679 save_reg(cfi, op->src.reg, CFI_CFA,
2680 op->dest.offset - cfi->cfa.offset);
2682 } else if (op->dest.reg == CFI_SP) {
2684 /* mov reg, disp(%rsp) */
2685 save_reg(cfi, op->src.reg, CFI_CFA,
2686 op->dest.offset - cfi->stack_size);
2688 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2690 /* mov %rsp, (%reg); # setup a stack swizzle. */
2691 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2692 cfi->vals[op->dest.reg].offset = cfa->offset;
2698 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2699 WARN_FUNC("unknown stack-related memory operation",
2700 insn->sec, insn->offset);
2705 cfi->stack_size -= 8;
2706 if (cfa->base == CFI_SP)
2712 WARN_FUNC("unknown stack-related instruction",
2713 insn->sec, insn->offset);
2721 * The stack layouts of alternatives instructions can sometimes diverge when
2722 * they have stack modifications. That's fine as long as the potential stack
2723 * layouts don't conflict at any given potential instruction boundary.
2725 * Flatten the CFIs of the different alternative code streams (both original
2726 * and replacement) into a single shared CFI array which can be used to detect
2727 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2729 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2731 struct cfi_state **alt_cfi;
2734 if (!insn->alt_group)
2738 WARN("CFI missing");
2742 alt_cfi = insn->alt_group->cfi;
2743 group_off = insn->offset - insn->alt_group->first_insn->offset;
2745 if (!alt_cfi[group_off]) {
2746 alt_cfi[group_off] = insn->cfi;
2748 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2749 WARN_FUNC("stack layout conflict in alternatives",
2750 insn->sec, insn->offset);
2758 static int handle_insn_ops(struct instruction *insn,
2759 struct instruction *next_insn,
2760 struct insn_state *state)
2762 struct stack_op *op;
2764 list_for_each_entry(op, &insn->stack_ops, list) {
2766 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2769 if (!insn->alt_group)
2772 if (op->dest.type == OP_DEST_PUSHF) {
2773 if (!state->uaccess_stack) {
2774 state->uaccess_stack = 1;
2775 } else if (state->uaccess_stack >> 31) {
2776 WARN_FUNC("PUSHF stack exhausted",
2777 insn->sec, insn->offset);
2780 state->uaccess_stack <<= 1;
2781 state->uaccess_stack |= state->uaccess;
2784 if (op->src.type == OP_SRC_POPF) {
2785 if (state->uaccess_stack) {
2786 state->uaccess = state->uaccess_stack & 1;
2787 state->uaccess_stack >>= 1;
2788 if (state->uaccess_stack == 1)
2789 state->uaccess_stack = 0;
2797 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2799 struct cfi_state *cfi1 = insn->cfi;
2803 WARN("CFI missing");
2807 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2809 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2810 insn->sec, insn->offset,
2811 cfi1->cfa.base, cfi1->cfa.offset,
2812 cfi2->cfa.base, cfi2->cfa.offset);
2814 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2815 for (i = 0; i < CFI_NUM_REGS; i++) {
2816 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2817 sizeof(struct cfi_reg)))
2820 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2821 insn->sec, insn->offset,
2822 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2823 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2827 } else if (cfi1->type != cfi2->type) {
2829 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2830 insn->sec, insn->offset, cfi1->type, cfi2->type);
2832 } else if (cfi1->drap != cfi2->drap ||
2833 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2834 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2836 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2837 insn->sec, insn->offset,
2838 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2839 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2847 static inline bool func_uaccess_safe(struct symbol *func)
2850 return func->uaccess_safe;
2855 static inline const char *call_dest_name(struct instruction *insn)
2857 static char pvname[19];
2861 if (insn->call_dest)
2862 return insn->call_dest->name;
2864 rel = insn_reloc(NULL, insn);
2865 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
2866 idx = (rel->addend / sizeof(void *));
2867 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
2874 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
2876 struct symbol *target;
2880 rel = insn_reloc(file, insn);
2881 if (!rel || strcmp(rel->sym->name, "pv_ops"))
2884 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
2886 if (file->pv_ops[idx].clean)
2889 file->pv_ops[idx].clean = true;
2891 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
2892 if (!target->sec->noinstr) {
2893 WARN("pv_ops[%d]: %s", idx, target->name);
2894 file->pv_ops[idx].clean = false;
2898 return file->pv_ops[idx].clean;
2901 static inline bool noinstr_call_dest(struct objtool_file *file,
2902 struct instruction *insn,
2903 struct symbol *func)
2906 * We can't deal with indirect function calls at present;
2907 * assume they're instrumented.
2911 return pv_call_dest(file, insn);
2917 * If the symbol is from a noinstr section; we good.
2919 if (func->sec->noinstr)
2923 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2924 * something 'BAD' happened. At the risk of taking the machine down,
2925 * let them proceed to get the message out.
2927 if (!strncmp(func->name, "__ubsan_handle_", 15))
2933 static int validate_call(struct objtool_file *file,
2934 struct instruction *insn,
2935 struct insn_state *state)
2937 if (state->noinstr && state->instr <= 0 &&
2938 !noinstr_call_dest(file, insn, insn->call_dest)) {
2939 WARN_FUNC("call to %s() leaves .noinstr.text section",
2940 insn->sec, insn->offset, call_dest_name(insn));
2944 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2945 WARN_FUNC("call to %s() with UACCESS enabled",
2946 insn->sec, insn->offset, call_dest_name(insn));
2951 WARN_FUNC("call to %s() with DF set",
2952 insn->sec, insn->offset, call_dest_name(insn));
2959 static int validate_sibling_call(struct objtool_file *file,
2960 struct instruction *insn,
2961 struct insn_state *state)
2963 if (has_modified_stack_frame(insn, state)) {
2964 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2965 insn->sec, insn->offset);
2969 return validate_call(file, insn, state);
2972 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2974 if (state->noinstr && state->instr > 0) {
2975 WARN_FUNC("return with instrumentation enabled",
2976 insn->sec, insn->offset);
2980 if (state->uaccess && !func_uaccess_safe(func)) {
2981 WARN_FUNC("return with UACCESS enabled",
2982 insn->sec, insn->offset);
2986 if (!state->uaccess && func_uaccess_safe(func)) {
2987 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2988 insn->sec, insn->offset);
2993 WARN_FUNC("return with DF set",
2994 insn->sec, insn->offset);
2998 if (func && has_modified_stack_frame(insn, state)) {
2999 WARN_FUNC("return with modified stack frame",
3000 insn->sec, insn->offset);
3004 if (state->cfi.bp_scratch) {
3005 WARN_FUNC("BP used as a scratch register",
3006 insn->sec, insn->offset);
3013 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3014 struct instruction *insn)
3016 struct alt_group *alt_group = insn->alt_group;
3019 * Simulate the fact that alternatives are patched in-place. When the
3020 * end of a replacement alt_group is reached, redirect objtool flow to
3021 * the end of the original alt_group.
3023 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3024 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3026 return next_insn_same_sec(file, insn);
3030 * Follow the branch starting at the given instruction, and recursively follow
3031 * any other branches (jumps). Meanwhile, track the frame pointer state at
3032 * each instruction and validate all the rules described in
3033 * tools/objtool/Documentation/stack-validation.txt.
3035 static int validate_branch(struct objtool_file *file, struct symbol *func,
3036 struct instruction *insn, struct insn_state state)
3038 struct alternative *alt;
3039 struct instruction *next_insn, *prev_insn = NULL;
3040 struct section *sec;
3047 next_insn = next_insn_to_validate(file, insn);
3049 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
3050 WARN("%s() falls through to next function %s()",
3051 func->name, insn->func->name);
3055 if (func && insn->ignore) {
3056 WARN_FUNC("BUG: why am I validating an ignored function?",
3061 visited = 1 << state.uaccess;
3062 if (insn->visited) {
3063 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3066 if (insn->visited & visited)
3073 state.instr += insn->instr;
3076 state.cfi = *insn->cfi;
3078 /* XXX track if we actually changed state.cfi */
3080 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3081 insn->cfi = prev_insn->cfi;
3084 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3088 insn->visited |= visited;
3090 if (propagate_alt_cfi(file, insn))
3093 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3094 bool skip_orig = false;
3096 list_for_each_entry(alt, &insn->alts, list) {
3100 ret = validate_branch(file, func, alt->insn, state);
3103 BT_FUNC("(alt)", insn);
3112 if (handle_insn_ops(insn, next_insn, &state))
3115 switch (insn->type) {
3118 if (next_insn && next_insn->type == INSN_TRAP) {
3119 next_insn->ignore = true;
3120 } else if (sls && !insn->retpoline_safe) {
3121 WARN_FUNC("missing int3 after ret",
3122 insn->sec, insn->offset);
3124 return validate_return(func, insn, &state);
3127 case INSN_CALL_DYNAMIC:
3128 ret = validate_call(file, insn, &state);
3132 if (!no_fp && func && !is_fentry_call(insn) &&
3133 !has_valid_stack_frame(&state)) {
3134 WARN_FUNC("call without frame pointer save/setup",
3139 if (dead_end_function(file, insn->call_dest))
3144 case INSN_JUMP_CONDITIONAL:
3145 case INSN_JUMP_UNCONDITIONAL:
3146 if (is_sibling_call(insn)) {
3147 ret = validate_sibling_call(file, insn, &state);
3151 } else if (insn->jump_dest) {
3152 ret = validate_branch(file, func,
3153 insn->jump_dest, state);
3156 BT_FUNC("(branch)", insn);
3161 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3166 case INSN_JUMP_DYNAMIC:
3167 if (next_insn && next_insn->type == INSN_TRAP) {
3168 next_insn->ignore = true;
3169 } else if (sls && !insn->retpoline_safe) {
3170 WARN_FUNC("missing int3 after indirect jump",
3171 insn->sec, insn->offset);
3175 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3176 if (is_sibling_call(insn)) {
3177 ret = validate_sibling_call(file, insn, &state);
3182 if (insn->type == INSN_JUMP_DYNAMIC)
3187 case INSN_CONTEXT_SWITCH:
3188 if (func && (!next_insn || !next_insn->hint)) {
3189 WARN_FUNC("unsupported instruction in callable function",
3196 if (state.uaccess) {
3197 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3201 state.uaccess = true;
3205 if (!state.uaccess && func) {
3206 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3210 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3211 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3215 state.uaccess = false;
3220 WARN_FUNC("recursive STD", sec, insn->offset);
3228 if (!state.df && func) {
3229 WARN_FUNC("redundant CLD", sec, insn->offset);
3244 if (state.cfi.cfa.base == CFI_UNDEFINED)
3246 WARN("%s: unexpected end of section", sec->name);
3257 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3259 struct instruction *insn;
3260 struct insn_state state;
3261 int ret, warnings = 0;
3266 init_insn_state(&state, sec);
3269 insn = find_insn(file, sec, 0);
3273 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3276 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3277 if (insn->hint && !insn->visited && !insn->ignore) {
3278 ret = validate_branch(file, insn->func, insn, state);
3279 if (ret && backtrace)
3280 BT_FUNC("<=== (hint)", insn);
3284 insn = list_next_entry(insn, list);
3290 static int validate_retpoline(struct objtool_file *file)
3292 struct instruction *insn;
3295 for_each_insn(file, insn) {
3296 if (insn->type != INSN_JUMP_DYNAMIC &&
3297 insn->type != INSN_CALL_DYNAMIC)
3300 if (insn->retpoline_safe)
3304 * .init.text code is ran before userspace and thus doesn't
3305 * strictly need retpolines, except for modules which are
3306 * loaded late, they very much do need retpoline in their
3309 if (!strcmp(insn->sec->name, ".init.text") && !module)
3312 WARN_FUNC("indirect %s found in RETPOLINE build",
3313 insn->sec, insn->offset,
3314 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3322 static bool is_kasan_insn(struct instruction *insn)
3324 return (insn->type == INSN_CALL &&
3325 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3328 static bool is_ubsan_insn(struct instruction *insn)
3330 return (insn->type == INSN_CALL &&
3331 !strcmp(insn->call_dest->name,
3332 "__ubsan_handle_builtin_unreachable"));
3335 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3338 struct instruction *prev_insn;
3340 if (insn->ignore || insn->type == INSN_NOP)
3344 * Ignore alternative replacement instructions. This can happen
3345 * when a whitelisted function uses one of the ALTERNATIVE macros.
3347 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
3348 !strcmp(insn->sec->name, ".altinstr_aux"))
3354 if (insn->func->static_call_tramp)
3358 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3359 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3360 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3361 * (or occasionally a JMP to UD2).
3363 * It may also insert a UD2 after calling a __noreturn function.
3365 prev_insn = list_prev_entry(insn, list);
3366 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3367 (insn->type == INSN_BUG ||
3368 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3369 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3373 * Check if this (or a subsequent) instruction is related to
3374 * CONFIG_UBSAN or CONFIG_KASAN.
3376 * End the search at 5 instructions to avoid going into the weeds.
3378 for (i = 0; i < 5; i++) {
3380 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3383 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3384 if (insn->jump_dest &&
3385 insn->jump_dest->func == insn->func) {
3386 insn = insn->jump_dest;
3393 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3396 insn = list_next_entry(insn, list);
3402 static int validate_symbol(struct objtool_file *file, struct section *sec,
3403 struct symbol *sym, struct insn_state *state)
3405 struct instruction *insn;
3409 WARN("%s() is missing an ELF size annotation", sym->name);
3413 if (sym->pfunc != sym || sym->alias != sym)
3416 insn = find_insn(file, sec, sym->offset);
3417 if (!insn || insn->ignore || insn->visited)
3420 state->uaccess = sym->uaccess_safe;
3422 ret = validate_branch(file, insn->func, insn, *state);
3423 if (ret && backtrace)
3424 BT_FUNC("<=== (sym)", insn);
3428 static int validate_section(struct objtool_file *file, struct section *sec)
3430 struct insn_state state;
3431 struct symbol *func;
3434 list_for_each_entry(func, &sec->symbol_list, list) {
3435 if (func->type != STT_FUNC)
3438 init_insn_state(&state, sec);
3439 set_func_state(&state.cfi);
3441 warnings += validate_symbol(file, sec, func, &state);
3447 static int validate_vmlinux_functions(struct objtool_file *file)
3449 struct section *sec;
3452 sec = find_section_by_name(file->elf, ".noinstr.text");
3454 warnings += validate_section(file, sec);
3455 warnings += validate_unwind_hints(file, sec);
3458 sec = find_section_by_name(file->elf, ".entry.text");
3460 warnings += validate_section(file, sec);
3461 warnings += validate_unwind_hints(file, sec);
3467 static int validate_functions(struct objtool_file *file)
3469 struct section *sec;
3472 for_each_sec(file, sec) {
3473 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3476 warnings += validate_section(file, sec);
3482 static int validate_reachable_instructions(struct objtool_file *file)
3484 struct instruction *insn;
3486 if (file->ignore_unreachables)
3489 for_each_insn(file, insn) {
3490 if (insn->visited || ignore_unreachable_insn(file, insn))
3493 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3500 int check(struct objtool_file *file)
3502 int ret, warnings = 0;
3504 arch_initial_func_cfi_state(&initial_func_cfi);
3505 init_cfi_state(&init_cfi);
3506 init_cfi_state(&func_cfi);
3507 set_func_state(&func_cfi);
3509 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3512 cfi_hash_add(&init_cfi);
3513 cfi_hash_add(&func_cfi);
3515 ret = decode_sections(file);
3521 if (list_empty(&file->insn_list))
3524 if (vmlinux && !validate_dup) {
3525 ret = validate_vmlinux_functions(file);
3534 ret = validate_retpoline(file);
3540 ret = validate_functions(file);
3545 ret = validate_unwind_hints(file, NULL);
3551 ret = validate_reachable_instructions(file);
3557 ret = create_static_call_sections(file);
3563 ret = create_retpoline_sites_sections(file);
3570 ret = create_mcount_loc_sections(file);
3577 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3578 printf("nr_cfi: %ld\n", nr_cfi);
3579 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3580 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3585 * For now, don't fail the kernel build on fatal warnings. These
3586 * errors are still fairly common due to the growing matrix of
3587 * supported toolchains and their recent pace of change.