1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
12 #include <objtool/builtin.h>
13 #include <objtool/cfi.h>
14 #include <objtool/arch.h>
15 #include <objtool/check.h>
16 #include <objtool/special.h>
17 #include <objtool/warn.h>
18 #include <objtool/endianness.h>
20 #include <linux/objtool.h>
21 #include <linux/hashtable.h>
22 #include <linux/kernel.h>
23 #include <linux/static_call_types.h>
26 struct list_head list;
27 struct instruction *insn;
31 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
33 static struct cfi_init_state initial_func_cfi;
34 static struct cfi_state init_cfi;
35 static struct cfi_state func_cfi;
37 struct instruction *find_insn(struct objtool_file *file,
38 struct section *sec, unsigned long offset)
40 struct instruction *insn;
42 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
43 if (insn->sec == sec && insn->offset == offset)
50 static struct instruction *next_insn_same_sec(struct objtool_file *file,
51 struct instruction *insn)
53 struct instruction *next = list_next_entry(insn, list);
55 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
61 static struct instruction *next_insn_same_func(struct objtool_file *file,
62 struct instruction *insn)
64 struct instruction *next = list_next_entry(insn, list);
65 struct symbol *func = insn->func;
70 if (&next->list != &file->insn_list && next->func == func)
73 /* Check if we're already in the subfunction: */
74 if (func == func->cfunc)
77 /* Move to the subfunction: */
78 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
81 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
82 struct instruction *insn)
84 struct instruction *prev = list_prev_entry(insn, list);
86 if (&prev->list != &file->insn_list && prev->func == insn->func)
92 #define func_for_each_insn(file, func, insn) \
93 for (insn = find_insn(file, func->sec, func->offset); \
95 insn = next_insn_same_func(file, insn))
97 #define sym_for_each_insn(file, sym, insn) \
98 for (insn = find_insn(file, sym->sec, sym->offset); \
99 insn && &insn->list != &file->insn_list && \
100 insn->sec == sym->sec && \
101 insn->offset < sym->offset + sym->len; \
102 insn = list_next_entry(insn, list))
104 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
105 for (insn = list_prev_entry(insn, list); \
106 &insn->list != &file->insn_list && \
107 insn->sec == sym->sec && insn->offset >= sym->offset; \
108 insn = list_prev_entry(insn, list))
110 #define sec_for_each_insn_from(file, insn) \
111 for (; insn; insn = next_insn_same_sec(file, insn))
113 #define sec_for_each_insn_continue(file, insn) \
114 for (insn = next_insn_same_sec(file, insn); insn; \
115 insn = next_insn_same_sec(file, insn))
117 static bool is_jump_table_jump(struct instruction *insn)
119 struct alt_group *alt_group = insn->alt_group;
121 if (insn->jump_table)
124 /* Retpoline alternative for a jump table? */
125 return alt_group && alt_group->orig_group &&
126 alt_group->orig_group->first_insn->jump_table;
129 static bool is_sibling_call(struct instruction *insn)
132 * Assume only ELF functions can make sibling calls. This ensures
133 * sibling call detection consistency between vmlinux.o and individual
139 /* An indirect jump is either a sibling call or a jump to a table. */
140 if (insn->type == INSN_JUMP_DYNAMIC)
141 return !is_jump_table_jump(insn);
143 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
144 return (is_static_jump(insn) && insn->call_dest);
148 * This checks to see if the given function is a "noreturn" function.
150 * For global functions which are outside the scope of this object file, we
151 * have to keep a manual list of them.
153 * For local functions, we have to detect them manually by simply looking for
154 * the lack of a return instruction.
156 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
160 struct instruction *insn;
164 * Unfortunately these have to be hard coded because the noreturn
165 * attribute isn't provided in ELF data.
167 static const char * const global_noreturns[] = {
172 "__module_put_and_exit",
178 "machine_real_restart",
179 "rewind_stack_do_exit",
180 "kunit_try_catch_throw",
182 "cpu_bringup_and_idle",
188 if (func->bind == STB_WEAK)
191 if (func->bind == STB_GLOBAL)
192 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
193 if (!strcmp(func->name, global_noreturns[i]))
199 insn = find_insn(file, func->sec, func->offset);
203 func_for_each_insn(file, func, insn) {
206 if (insn->type == INSN_RETURN)
214 * A function can have a sibling call instead of a return. In that
215 * case, the function's dead-end status depends on whether the target
216 * of the sibling call returns.
218 func_for_each_insn(file, func, insn) {
219 if (is_sibling_call(insn)) {
220 struct instruction *dest = insn->jump_dest;
223 /* sibling call to another file */
226 /* local sibling call */
227 if (recursion == 5) {
229 * Infinite recursion: two functions have
230 * sibling calls to each other. This is a very
231 * rare case. It means they aren't dead ends.
236 return __dead_end_function(file, dest->func, recursion+1);
243 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
245 return __dead_end_function(file, func, 0);
248 static void init_cfi_state(struct cfi_state *cfi)
252 for (i = 0; i < CFI_NUM_REGS; i++) {
253 cfi->regs[i].base = CFI_UNDEFINED;
254 cfi->vals[i].base = CFI_UNDEFINED;
256 cfi->cfa.base = CFI_UNDEFINED;
257 cfi->drap_reg = CFI_UNDEFINED;
258 cfi->drap_offset = -1;
261 static void init_insn_state(struct insn_state *state, struct section *sec)
263 memset(state, 0, sizeof(*state));
264 init_cfi_state(&state->cfi);
267 * We need the full vmlinux for noinstr validation, otherwise we can
268 * not correctly determine insn->call_dest->sec (external symbols do
269 * not have a section).
271 if (vmlinux && noinstr && sec)
272 state->noinstr = sec->noinstr;
275 static struct cfi_state *cfi_alloc(void)
277 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
279 WARN("calloc failed");
287 static struct hlist_head *cfi_hash;
289 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
291 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
292 (void *)cfi2 + sizeof(cfi2->hash),
293 sizeof(struct cfi_state) - sizeof(struct hlist_node));
296 static inline u32 cfi_key(struct cfi_state *cfi)
298 return jhash((void *)cfi + sizeof(cfi->hash),
299 sizeof(*cfi) - sizeof(cfi->hash), 0);
302 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
304 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
305 struct cfi_state *obj;
307 hlist_for_each_entry(obj, head, hash) {
308 if (!cficmp(cfi, obj)) {
316 hlist_add_head(&obj->hash, head);
321 static void cfi_hash_add(struct cfi_state *cfi)
323 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
325 hlist_add_head(&cfi->hash, head);
328 static void *cfi_hash_alloc(unsigned long size)
330 cfi_bits = max(10, ilog2(size));
331 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
332 PROT_READ|PROT_WRITE,
333 MAP_PRIVATE|MAP_ANON, -1, 0);
334 if (cfi_hash == (void *)-1L) {
335 WARN("mmap fail cfi_hash");
338 printf("cfi_bits: %d\n", cfi_bits);
344 static unsigned long nr_insns;
345 static unsigned long nr_insns_visited;
348 * Call the arch-specific instruction decoder for all the instructions and add
349 * them to the global instruction list.
351 static int decode_instructions(struct objtool_file *file)
355 unsigned long offset;
356 struct instruction *insn;
359 for_each_sec(file, sec) {
361 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
364 if (strcmp(sec->name, ".altinstr_replacement") &&
365 strcmp(sec->name, ".altinstr_aux") &&
366 strncmp(sec->name, ".discard.", 9))
369 if (!strcmp(sec->name, ".noinstr.text") ||
370 !strcmp(sec->name, ".entry.text") ||
371 !strncmp(sec->name, ".text.__x86.", 12))
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->elf, 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);
425 static struct instruction *find_last_insn(struct objtool_file *file,
428 struct instruction *insn = NULL;
430 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
432 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
433 insn = find_insn(file, sec, offset);
439 * Mark "ud2" instructions and manually annotated dead ends.
441 static int add_dead_ends(struct objtool_file *file)
445 struct instruction *insn;
448 * By default, "ud2" is a dead end unless otherwise annotated, because
449 * GCC 7 inserts it for certain divide-by-zero cases.
451 for_each_insn(file, insn)
452 if (insn->type == INSN_BUG)
453 insn->dead_end = true;
456 * Check for manually annotated dead ends.
458 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
462 list_for_each_entry(reloc, &sec->reloc_list, list) {
463 if (reloc->sym->type != STT_SECTION) {
464 WARN("unexpected relocation symbol type in %s", sec->name);
467 insn = find_insn(file, reloc->sym->sec, reloc->addend);
469 insn = list_prev_entry(insn, list);
470 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
471 insn = find_last_insn(file, reloc->sym->sec);
473 WARN("can't find unreachable insn at %s+0x%" PRIx64,
474 reloc->sym->sec->name, reloc->addend);
478 WARN("can't find unreachable insn at %s+0x%" PRIx64,
479 reloc->sym->sec->name, reloc->addend);
483 insn->dead_end = true;
488 * These manually annotated reachable checks are needed for GCC 4.4,
489 * where the Linux unreachable() macro isn't supported. In that case
490 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
493 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
497 list_for_each_entry(reloc, &sec->reloc_list, list) {
498 if (reloc->sym->type != STT_SECTION) {
499 WARN("unexpected relocation symbol type in %s", sec->name);
502 insn = find_insn(file, reloc->sym->sec, reloc->addend);
504 insn = list_prev_entry(insn, list);
505 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
506 insn = find_last_insn(file, reloc->sym->sec);
508 WARN("can't find reachable insn at %s+0x%" PRIx64,
509 reloc->sym->sec->name, reloc->addend);
513 WARN("can't find reachable insn at %s+0x%" PRIx64,
514 reloc->sym->sec->name, reloc->addend);
518 insn->dead_end = false;
524 static int create_static_call_sections(struct objtool_file *file)
527 struct static_call_site *site;
528 struct instruction *insn;
529 struct symbol *key_sym;
530 char *key_name, *tmp;
533 sec = find_section_by_name(file->elf, ".static_call_sites");
535 INIT_LIST_HEAD(&file->static_call_list);
536 WARN("file already has .static_call_sites section, skipping");
540 if (list_empty(&file->static_call_list))
544 list_for_each_entry(insn, &file->static_call_list, call_node)
547 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
548 sizeof(struct static_call_site), idx);
553 list_for_each_entry(insn, &file->static_call_list, call_node) {
555 site = (struct static_call_site *)sec->data->d_buf + idx;
556 memset(site, 0, sizeof(struct static_call_site));
558 /* populate reloc for 'addr' */
559 if (elf_add_reloc_to_insn(file->elf, sec,
560 idx * sizeof(struct static_call_site),
562 insn->sec, insn->offset))
565 /* find key symbol */
566 key_name = strdup(insn->call_dest->name);
571 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
572 STATIC_CALL_TRAMP_PREFIX_LEN)) {
573 WARN("static_call: trampoline name malformed: %s", key_name);
576 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
577 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
579 key_sym = find_symbol_by_name(file->elf, tmp);
582 WARN("static_call: can't find static_call_key symbol: %s", tmp);
587 * For modules(), the key might not be exported, which
588 * means the module can make static calls but isn't
589 * allowed to change them.
591 * In that case we temporarily set the key to be the
592 * trampoline address. This is fixed up in
593 * static_call_add_module().
595 key_sym = insn->call_dest;
599 /* populate reloc for 'key' */
600 if (elf_add_reloc(file->elf, sec,
601 idx * sizeof(struct static_call_site) + 4,
602 R_X86_64_PC32, key_sym,
603 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
612 static int create_retpoline_sites_sections(struct objtool_file *file)
614 struct instruction *insn;
618 sec = find_section_by_name(file->elf, ".retpoline_sites");
620 WARN("file already has .retpoline_sites, skipping");
625 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
631 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
634 WARN("elf_create_section: .retpoline_sites");
639 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
641 int *site = (int *)sec->data->d_buf + idx;
644 if (elf_add_reloc_to_insn(file->elf, sec,
647 insn->sec, insn->offset)) {
648 WARN("elf_add_reloc_to_insn: .retpoline_sites");
658 static int create_return_sites_sections(struct objtool_file *file)
660 struct instruction *insn;
664 sec = find_section_by_name(file->elf, ".return_sites");
666 WARN("file already has .return_sites, skipping");
671 list_for_each_entry(insn, &file->return_thunk_list, call_node)
677 sec = elf_create_section(file->elf, ".return_sites", 0,
680 WARN("elf_create_section: .return_sites");
685 list_for_each_entry(insn, &file->return_thunk_list, call_node) {
687 int *site = (int *)sec->data->d_buf + idx;
690 if (elf_add_reloc_to_insn(file->elf, sec,
693 insn->sec, insn->offset)) {
694 WARN("elf_add_reloc_to_insn: .return_sites");
704 static int create_mcount_loc_sections(struct objtool_file *file)
708 struct instruction *insn;
711 sec = find_section_by_name(file->elf, "__mcount_loc");
713 INIT_LIST_HEAD(&file->mcount_loc_list);
714 WARN("file already has __mcount_loc section, skipping");
718 if (list_empty(&file->mcount_loc_list))
722 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
725 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
730 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
732 loc = (unsigned long *)sec->data->d_buf + idx;
733 memset(loc, 0, sizeof(unsigned long));
735 if (elf_add_reloc_to_insn(file->elf, sec,
736 idx * sizeof(unsigned long),
738 insn->sec, insn->offset))
748 * Warnings shouldn't be reported for ignored functions.
750 static void add_ignores(struct objtool_file *file)
752 struct instruction *insn;
757 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
761 list_for_each_entry(reloc, &sec->reloc_list, list) {
762 switch (reloc->sym->type) {
768 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
774 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
778 func_for_each_insn(file, func, insn)
784 * This is a whitelist of functions that is allowed to be called with AC set.
785 * The list is meant to be minimal and only contains compiler instrumentation
786 * ABI and a few functions used to implement *_{to,from}_user() functions.
788 * These functions must not directly change AC, but may PUSHF/POPF.
790 static const char *uaccess_safe_builtin[] = {
794 /* KASAN out-of-line */
795 "__asan_loadN_noabort",
796 "__asan_load1_noabort",
797 "__asan_load2_noabort",
798 "__asan_load4_noabort",
799 "__asan_load8_noabort",
800 "__asan_load16_noabort",
801 "__asan_storeN_noabort",
802 "__asan_store1_noabort",
803 "__asan_store2_noabort",
804 "__asan_store4_noabort",
805 "__asan_store8_noabort",
806 "__asan_store16_noabort",
807 "__kasan_check_read",
808 "__kasan_check_write",
810 "__asan_report_load_n_noabort",
811 "__asan_report_load1_noabort",
812 "__asan_report_load2_noabort",
813 "__asan_report_load4_noabort",
814 "__asan_report_load8_noabort",
815 "__asan_report_load16_noabort",
816 "__asan_report_store_n_noabort",
817 "__asan_report_store1_noabort",
818 "__asan_report_store2_noabort",
819 "__asan_report_store4_noabort",
820 "__asan_report_store8_noabort",
821 "__asan_report_store16_noabort",
823 "__kcsan_check_access",
824 "kcsan_found_watchpoint",
825 "kcsan_setup_watchpoint",
826 "kcsan_check_scoped_accesses",
827 "kcsan_disable_current",
828 "kcsan_enable_current_nowarn",
833 "__tsan_write_range",
844 "__tsan_read_write1",
845 "__tsan_read_write2",
846 "__tsan_read_write4",
847 "__tsan_read_write8",
848 "__tsan_read_write16",
849 "__tsan_atomic8_load",
850 "__tsan_atomic16_load",
851 "__tsan_atomic32_load",
852 "__tsan_atomic64_load",
853 "__tsan_atomic8_store",
854 "__tsan_atomic16_store",
855 "__tsan_atomic32_store",
856 "__tsan_atomic64_store",
857 "__tsan_atomic8_exchange",
858 "__tsan_atomic16_exchange",
859 "__tsan_atomic32_exchange",
860 "__tsan_atomic64_exchange",
861 "__tsan_atomic8_fetch_add",
862 "__tsan_atomic16_fetch_add",
863 "__tsan_atomic32_fetch_add",
864 "__tsan_atomic64_fetch_add",
865 "__tsan_atomic8_fetch_sub",
866 "__tsan_atomic16_fetch_sub",
867 "__tsan_atomic32_fetch_sub",
868 "__tsan_atomic64_fetch_sub",
869 "__tsan_atomic8_fetch_and",
870 "__tsan_atomic16_fetch_and",
871 "__tsan_atomic32_fetch_and",
872 "__tsan_atomic64_fetch_and",
873 "__tsan_atomic8_fetch_or",
874 "__tsan_atomic16_fetch_or",
875 "__tsan_atomic32_fetch_or",
876 "__tsan_atomic64_fetch_or",
877 "__tsan_atomic8_fetch_xor",
878 "__tsan_atomic16_fetch_xor",
879 "__tsan_atomic32_fetch_xor",
880 "__tsan_atomic64_fetch_xor",
881 "__tsan_atomic8_fetch_nand",
882 "__tsan_atomic16_fetch_nand",
883 "__tsan_atomic32_fetch_nand",
884 "__tsan_atomic64_fetch_nand",
885 "__tsan_atomic8_compare_exchange_strong",
886 "__tsan_atomic16_compare_exchange_strong",
887 "__tsan_atomic32_compare_exchange_strong",
888 "__tsan_atomic64_compare_exchange_strong",
889 "__tsan_atomic8_compare_exchange_weak",
890 "__tsan_atomic16_compare_exchange_weak",
891 "__tsan_atomic32_compare_exchange_weak",
892 "__tsan_atomic64_compare_exchange_weak",
893 "__tsan_atomic8_compare_exchange_val",
894 "__tsan_atomic16_compare_exchange_val",
895 "__tsan_atomic32_compare_exchange_val",
896 "__tsan_atomic64_compare_exchange_val",
897 "__tsan_atomic_thread_fence",
898 "__tsan_atomic_signal_fence",
902 "__sanitizer_cov_trace_pc",
903 "__sanitizer_cov_trace_const_cmp1",
904 "__sanitizer_cov_trace_const_cmp2",
905 "__sanitizer_cov_trace_const_cmp4",
906 "__sanitizer_cov_trace_const_cmp8",
907 "__sanitizer_cov_trace_cmp1",
908 "__sanitizer_cov_trace_cmp2",
909 "__sanitizer_cov_trace_cmp4",
910 "__sanitizer_cov_trace_cmp8",
911 "__sanitizer_cov_trace_switch",
913 "ubsan_type_mismatch_common",
914 "__ubsan_handle_type_mismatch",
915 "__ubsan_handle_type_mismatch_v1",
916 "__ubsan_handle_shift_out_of_bounds",
918 "csum_partial_copy_generic",
920 "copy_mc_fragile_handle_tail",
921 "copy_mc_enhanced_fast_string",
922 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
926 static void add_uaccess_safe(struct objtool_file *file)
934 for (name = uaccess_safe_builtin; *name; name++) {
935 func = find_symbol_by_name(file->elf, *name);
939 func->uaccess_safe = true;
944 * FIXME: For now, just ignore any alternatives which add retpolines. This is
945 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
946 * But it at least allows objtool to understand the control flow *around* the
949 static int add_ignore_alternatives(struct objtool_file *file)
953 struct instruction *insn;
955 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
959 list_for_each_entry(reloc, &sec->reloc_list, list) {
960 if (reloc->sym->type != STT_SECTION) {
961 WARN("unexpected relocation symbol type in %s", sec->name);
965 insn = find_insn(file, reloc->sym->sec, reloc->addend);
967 WARN("bad .discard.ignore_alts entry");
971 insn->ignore_alts = true;
977 __weak bool arch_is_retpoline(struct symbol *sym)
982 __weak bool arch_is_rethunk(struct symbol *sym)
987 #define NEGATIVE_RELOC ((void *)-1L)
989 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
991 if (insn->reloc == NEGATIVE_RELOC)
995 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
996 insn->offset, insn->len);
998 insn->reloc = NEGATIVE_RELOC;
1006 static void remove_insn_ops(struct instruction *insn)
1008 struct stack_op *op, *tmp;
1010 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1011 list_del(&op->list);
1016 static void annotate_call_site(struct objtool_file *file,
1017 struct instruction *insn, bool sibling)
1019 struct reloc *reloc = insn_reloc(file, insn);
1020 struct symbol *sym = insn->call_dest;
1026 * Alternative replacement code is just template code which is
1027 * sometimes copied to the original instruction. For now, don't
1028 * annotate it. (In the future we might consider annotating the
1029 * original instruction if/when it ever makes sense to do so.)
1031 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1034 if (sym->static_call_tramp) {
1035 list_add_tail(&insn->call_node, &file->static_call_list);
1039 if (sym->retpoline_thunk) {
1040 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1045 * Many compilers cannot disable KCOV with a function attribute
1046 * so they need a little help, NOP out any KCOV calls from noinstr
1049 if (insn->sec->noinstr && sym->kcov) {
1051 reloc->type = R_NONE;
1052 elf_write_reloc(file->elf, reloc);
1055 elf_write_insn(file->elf, insn->sec,
1056 insn->offset, insn->len,
1057 sibling ? arch_ret_insn(insn->len)
1058 : arch_nop_insn(insn->len));
1060 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1064 * We've replaced the tail-call JMP insn by two new
1065 * insn: RET; INT3, except we only have a single struct
1066 * insn here. Mark it retpoline_safe to avoid the SLS
1067 * warning, instead of adding another insn.
1069 insn->retpoline_safe = true;
1075 if (mcount && sym->fentry) {
1077 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1080 reloc->type = R_NONE;
1081 elf_write_reloc(file->elf, reloc);
1084 elf_write_insn(file->elf, insn->sec,
1085 insn->offset, insn->len,
1086 arch_nop_insn(insn->len));
1088 insn->type = INSN_NOP;
1090 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1095 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1096 struct symbol *dest, bool sibling)
1098 insn->call_dest = dest;
1103 * Whatever stack impact regular CALLs have, should be undone
1104 * by the RETURN of the called function.
1106 * Annotated intra-function calls retain the stack_ops but
1107 * are converted to JUMP, see read_intra_function_calls().
1109 remove_insn_ops(insn);
1111 annotate_call_site(file, insn, sibling);
1114 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1117 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1118 * so convert them accordingly.
1120 switch (insn->type) {
1122 insn->type = INSN_CALL_DYNAMIC;
1124 case INSN_JUMP_UNCONDITIONAL:
1125 insn->type = INSN_JUMP_DYNAMIC;
1127 case INSN_JUMP_CONDITIONAL:
1128 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1134 insn->retpoline_safe = true;
1137 * Whatever stack impact regular CALLs have, should be undone
1138 * by the RETURN of the called function.
1140 * Annotated intra-function calls retain the stack_ops but
1141 * are converted to JUMP, see read_intra_function_calls().
1143 remove_insn_ops(insn);
1145 annotate_call_site(file, insn, false);
1148 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
1151 * Return thunk tail calls are really just returns in disguise,
1152 * so convert them accordingly.
1154 insn->type = INSN_RETURN;
1155 insn->retpoline_safe = true;
1157 /* Skip the non-text sections, specially .discard ones */
1158 if (add && insn->sec->text)
1159 list_add_tail(&insn->call_node, &file->return_thunk_list);
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 (reloc->sym->return_thunk) {
1187 add_return_call(file, insn, true);
1189 } else if (insn->func) {
1190 /* internal or external sibling call (with reloc) */
1191 add_call_dest(file, insn, reloc->sym, true);
1193 } else if (reloc->sym->sec->idx) {
1194 dest_sec = reloc->sym->sec;
1195 dest_off = reloc->sym->sym.st_value +
1196 arch_dest_reloc_offset(reloc->addend);
1198 /* non-func asm code jumping to another file */
1202 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1203 if (!insn->jump_dest) {
1204 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1207 * This is a special case where an alt instruction
1208 * jumps past the end of the section. These are
1209 * handled later in handle_group_alt().
1211 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1215 * This is a special case for zen_untrain_ret().
1216 * It jumps to __x86_return_thunk(), but objtool
1217 * can't find the thunk's starting RET
1218 * instruction, because the RET is also in the
1219 * middle of another instruction. Objtool only
1220 * knows about the outer instruction.
1222 if (sym && sym->return_thunk) {
1223 add_return_call(file, insn, false);
1227 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1228 insn->sec, insn->offset, dest_sec->name,
1234 * Cross-function jump.
1236 if (insn->func && insn->jump_dest->func &&
1237 insn->func != insn->jump_dest->func) {
1240 * For GCC 8+, create parent/child links for any cold
1241 * subfunctions. This is _mostly_ redundant with a
1242 * similar initialization in read_symbols().
1244 * If a function has aliases, we want the *first* such
1245 * function in the symbol table to be the subfunction's
1246 * parent. In that case we overwrite the
1247 * initialization done in read_symbols().
1249 * However this code can't completely replace the
1250 * read_symbols() code because this doesn't detect the
1251 * case where the parent function's only reference to a
1252 * subfunction is through a jump table.
1254 if (!strstr(insn->func->name, ".cold") &&
1255 strstr(insn->jump_dest->func->name, ".cold")) {
1256 insn->func->cfunc = insn->jump_dest->func;
1257 insn->jump_dest->func->pfunc = insn->func;
1259 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1260 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1261 /* internal sibling call (without reloc) */
1262 add_call_dest(file, insn, insn->jump_dest->func, true);
1270 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1272 struct symbol *call_dest;
1274 call_dest = find_func_by_offset(sec, offset);
1276 call_dest = find_symbol_by_offset(sec, offset);
1282 * Find the destination instructions for all calls.
1284 static int add_call_destinations(struct objtool_file *file)
1286 struct instruction *insn;
1287 unsigned long dest_off;
1288 struct symbol *dest;
1289 struct reloc *reloc;
1291 for_each_insn(file, insn) {
1292 if (insn->type != INSN_CALL)
1295 reloc = insn_reloc(file, insn);
1297 dest_off = arch_jump_destination(insn);
1298 dest = find_call_destination(insn->sec, dest_off);
1300 add_call_dest(file, insn, dest, false);
1305 if (!insn->call_dest) {
1306 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1310 if (insn->func && insn->call_dest->type != STT_FUNC) {
1311 WARN_FUNC("unsupported call to non-function",
1312 insn->sec, insn->offset);
1316 } else if (reloc->sym->type == STT_SECTION) {
1317 dest_off = arch_dest_reloc_offset(reloc->addend);
1318 dest = find_call_destination(reloc->sym->sec, dest_off);
1320 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1321 insn->sec, insn->offset,
1322 reloc->sym->sec->name,
1327 add_call_dest(file, insn, dest, false);
1329 } else if (reloc->sym->retpoline_thunk) {
1330 add_retpoline_call(file, insn);
1333 add_call_dest(file, insn, reloc->sym, false);
1340 * The .alternatives section requires some extra special care over and above
1341 * other special sections because alternatives are patched in place.
1343 static int handle_group_alt(struct objtool_file *file,
1344 struct special_alt *special_alt,
1345 struct instruction *orig_insn,
1346 struct instruction **new_insn)
1348 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1349 struct alt_group *orig_alt_group, *new_alt_group;
1350 unsigned long dest_off;
1353 orig_alt_group = malloc(sizeof(*orig_alt_group));
1354 if (!orig_alt_group) {
1355 WARN("malloc failed");
1358 orig_alt_group->cfi = calloc(special_alt->orig_len,
1359 sizeof(struct cfi_state *));
1360 if (!orig_alt_group->cfi) {
1361 WARN("calloc failed");
1365 last_orig_insn = NULL;
1367 sec_for_each_insn_from(file, insn) {
1368 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1371 insn->alt_group = orig_alt_group;
1372 last_orig_insn = insn;
1374 orig_alt_group->orig_group = NULL;
1375 orig_alt_group->first_insn = orig_insn;
1376 orig_alt_group->last_insn = last_orig_insn;
1379 new_alt_group = malloc(sizeof(*new_alt_group));
1380 if (!new_alt_group) {
1381 WARN("malloc failed");
1385 if (special_alt->new_len < special_alt->orig_len) {
1387 * Insert a fake nop at the end to make the replacement
1388 * alt_group the same size as the original. This is needed to
1389 * allow propagate_alt_cfi() to do its magic. When the last
1390 * instruction affects the stack, the instruction after it (the
1391 * nop) will propagate the new state to the shared CFI array.
1393 nop = malloc(sizeof(*nop));
1395 WARN("malloc failed");
1398 memset(nop, 0, sizeof(*nop));
1399 INIT_LIST_HEAD(&nop->alts);
1400 INIT_LIST_HEAD(&nop->stack_ops);
1402 nop->sec = special_alt->new_sec;
1403 nop->offset = special_alt->new_off + special_alt->new_len;
1404 nop->len = special_alt->orig_len - special_alt->new_len;
1405 nop->type = INSN_NOP;
1406 nop->func = orig_insn->func;
1407 nop->alt_group = new_alt_group;
1408 nop->ignore = orig_insn->ignore_alts;
1411 if (!special_alt->new_len) {
1417 sec_for_each_insn_from(file, insn) {
1418 struct reloc *alt_reloc;
1420 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1423 last_new_insn = insn;
1425 insn->ignore = orig_insn->ignore_alts;
1426 insn->func = orig_insn->func;
1427 insn->alt_group = new_alt_group;
1430 * Since alternative replacement code is copy/pasted by the
1431 * kernel after applying relocations, generally such code can't
1432 * have relative-address relocation references to outside the
1433 * .altinstr_replacement section, unless the arch's
1434 * alternatives code can adjust the relative offsets
1437 alt_reloc = insn_reloc(file, insn);
1439 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1441 WARN_FUNC("unsupported relocation in alternatives section",
1442 insn->sec, insn->offset);
1446 if (!is_static_jump(insn))
1449 if (!insn->immediate)
1452 dest_off = arch_jump_destination(insn);
1453 if (dest_off == special_alt->new_off + special_alt->new_len)
1454 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1456 if (!insn->jump_dest) {
1457 WARN_FUNC("can't find alternative jump destination",
1458 insn->sec, insn->offset);
1463 if (!last_new_insn) {
1464 WARN_FUNC("can't find last new alternative instruction",
1465 special_alt->new_sec, special_alt->new_off);
1470 list_add(&nop->list, &last_new_insn->list);
1472 new_alt_group->orig_group = orig_alt_group;
1473 new_alt_group->first_insn = *new_insn;
1474 new_alt_group->last_insn = nop ? : last_new_insn;
1475 new_alt_group->cfi = orig_alt_group->cfi;
1480 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1481 * If the original instruction is a jump, make the alt entry an effective nop
1482 * by just skipping the original instruction.
1484 static int handle_jump_alt(struct objtool_file *file,
1485 struct special_alt *special_alt,
1486 struct instruction *orig_insn,
1487 struct instruction **new_insn)
1489 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1490 orig_insn->type != INSN_NOP) {
1492 WARN_FUNC("unsupported instruction at jump label",
1493 orig_insn->sec, orig_insn->offset);
1497 if (special_alt->key_addend & 2) {
1498 struct reloc *reloc = insn_reloc(file, orig_insn);
1501 reloc->type = R_NONE;
1502 elf_write_reloc(file->elf, reloc);
1504 elf_write_insn(file->elf, orig_insn->sec,
1505 orig_insn->offset, orig_insn->len,
1506 arch_nop_insn(orig_insn->len));
1507 orig_insn->type = INSN_NOP;
1510 if (orig_insn->type == INSN_NOP) {
1511 if (orig_insn->len == 2)
1512 file->jl_nop_short++;
1514 file->jl_nop_long++;
1519 if (orig_insn->len == 2)
1524 *new_insn = list_next_entry(orig_insn, list);
1529 * Read all the special sections which have alternate instructions which can be
1530 * patched in or redirected to at runtime. Each instruction having alternate
1531 * instruction(s) has them added to its insn->alts list, which will be
1532 * traversed in validate_branch().
1534 static int add_special_section_alts(struct objtool_file *file)
1536 struct list_head special_alts;
1537 struct instruction *orig_insn, *new_insn;
1538 struct special_alt *special_alt, *tmp;
1539 struct alternative *alt;
1542 ret = special_get_alts(file->elf, &special_alts);
1546 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1548 orig_insn = find_insn(file, special_alt->orig_sec,
1549 special_alt->orig_off);
1551 WARN_FUNC("special: can't find orig instruction",
1552 special_alt->orig_sec, special_alt->orig_off);
1558 if (!special_alt->group || special_alt->new_len) {
1559 new_insn = find_insn(file, special_alt->new_sec,
1560 special_alt->new_off);
1562 WARN_FUNC("special: can't find new instruction",
1563 special_alt->new_sec,
1564 special_alt->new_off);
1570 if (special_alt->group) {
1571 if (!special_alt->orig_len) {
1572 WARN_FUNC("empty alternative entry",
1573 orig_insn->sec, orig_insn->offset);
1577 ret = handle_group_alt(file, special_alt, orig_insn,
1581 } else if (special_alt->jump_or_nop) {
1582 ret = handle_jump_alt(file, special_alt, orig_insn,
1588 alt = malloc(sizeof(*alt));
1590 WARN("malloc failed");
1595 alt->insn = new_insn;
1596 alt->skip_orig = special_alt->skip_orig;
1597 orig_insn->ignore_alts |= special_alt->skip_alt;
1598 list_add_tail(&alt->list, &orig_insn->alts);
1600 list_del(&special_alt->list);
1605 printf("jl\\\tNOP\tJMP\n");
1606 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1607 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1614 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1615 struct reloc *table)
1617 struct reloc *reloc = table;
1618 struct instruction *dest_insn;
1619 struct alternative *alt;
1620 struct symbol *pfunc = insn->func->pfunc;
1621 unsigned int prev_offset = 0;
1624 * Each @reloc is a switch table relocation which points to the target
1627 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1629 /* Check for the end of the table: */
1630 if (reloc != table && reloc->jump_table_start)
1633 /* Make sure the table entries are consecutive: */
1634 if (prev_offset && reloc->offset != prev_offset + 8)
1637 /* Detect function pointers from contiguous objects: */
1638 if (reloc->sym->sec == pfunc->sec &&
1639 reloc->addend == pfunc->offset)
1642 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1646 /* Make sure the destination is in the same function: */
1647 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1650 alt = malloc(sizeof(*alt));
1652 WARN("malloc failed");
1656 alt->insn = dest_insn;
1657 list_add_tail(&alt->list, &insn->alts);
1658 prev_offset = reloc->offset;
1662 WARN_FUNC("can't find switch jump table",
1663 insn->sec, insn->offset);
1671 * find_jump_table() - Given a dynamic jump, find the switch jump table
1672 * associated with it.
1674 static struct reloc *find_jump_table(struct objtool_file *file,
1675 struct symbol *func,
1676 struct instruction *insn)
1678 struct reloc *table_reloc;
1679 struct instruction *dest_insn, *orig_insn = insn;
1682 * Backward search using the @first_jump_src links, these help avoid
1683 * much of the 'in between' code. Which avoids us getting confused by
1687 insn && insn->func && insn->func->pfunc == func;
1688 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1690 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1693 /* allow small jumps within the range */
1694 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1696 (insn->jump_dest->offset <= insn->offset ||
1697 insn->jump_dest->offset > orig_insn->offset))
1700 table_reloc = arch_find_switch_table(file, insn);
1703 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1704 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1714 * First pass: Mark the head of each jump table so that in the next pass,
1715 * we know when a given jump table ends and the next one starts.
1717 static void mark_func_jump_tables(struct objtool_file *file,
1718 struct symbol *func)
1720 struct instruction *insn, *last = NULL;
1721 struct reloc *reloc;
1723 func_for_each_insn(file, func, insn) {
1728 * Store back-pointers for unconditional forward jumps such
1729 * that find_jump_table() can back-track using those and
1730 * avoid some potentially confusing code.
1732 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1733 insn->offset > last->offset &&
1734 insn->jump_dest->offset > insn->offset &&
1735 !insn->jump_dest->first_jump_src) {
1737 insn->jump_dest->first_jump_src = insn;
1738 last = insn->jump_dest;
1741 if (insn->type != INSN_JUMP_DYNAMIC)
1744 reloc = find_jump_table(file, func, insn);
1746 reloc->jump_table_start = true;
1747 insn->jump_table = reloc;
1752 static int add_func_jump_tables(struct objtool_file *file,
1753 struct symbol *func)
1755 struct instruction *insn;
1758 func_for_each_insn(file, func, insn) {
1759 if (!insn->jump_table)
1762 ret = add_jump_table(file, insn, insn->jump_table);
1771 * For some switch statements, gcc generates a jump table in the .rodata
1772 * section which contains a list of addresses within the function to jump to.
1773 * This finds these jump tables and adds them to the insn->alts lists.
1775 static int add_jump_table_alts(struct objtool_file *file)
1777 struct section *sec;
1778 struct symbol *func;
1784 for_each_sec(file, sec) {
1785 list_for_each_entry(func, &sec->symbol_list, list) {
1786 if (func->type != STT_FUNC)
1789 mark_func_jump_tables(file, func);
1790 ret = add_func_jump_tables(file, func);
1799 static void set_func_state(struct cfi_state *state)
1801 state->cfa = initial_func_cfi.cfa;
1802 memcpy(&state->regs, &initial_func_cfi.regs,
1803 CFI_NUM_REGS * sizeof(struct cfi_reg));
1804 state->stack_size = initial_func_cfi.cfa.offset;
1807 static int read_unwind_hints(struct objtool_file *file)
1809 struct cfi_state cfi = init_cfi;
1810 struct section *sec, *relocsec;
1811 struct unwind_hint *hint;
1812 struct instruction *insn;
1813 struct reloc *reloc;
1816 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1820 relocsec = sec->reloc;
1822 WARN("missing .rela.discard.unwind_hints section");
1826 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1827 WARN("struct unwind_hint size mismatch");
1833 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1834 hint = (struct unwind_hint *)sec->data->d_buf + i;
1836 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1838 WARN("can't find reloc for unwind_hints[%d]", i);
1842 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1844 WARN("can't find insn for unwind_hints[%d]", i);
1850 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1856 if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1857 insn->restore = true;
1861 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1862 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1864 if (sym && sym->bind == STB_GLOBAL) {
1869 if (hint->type == UNWIND_HINT_TYPE_ENTRY) {
1870 hint->type = UNWIND_HINT_TYPE_CALL;
1874 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1875 insn->cfi = &func_cfi;
1882 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1883 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1884 insn->sec, insn->offset, hint->sp_reg);
1888 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1889 cfi.type = hint->type;
1890 cfi.end = hint->end;
1892 insn->cfi = cfi_hash_find_or_add(&cfi);
1898 static int read_retpoline_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.retpoline_safe");
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.retpoline_safe entry");
1920 if (insn->type != INSN_JUMP_DYNAMIC &&
1921 insn->type != INSN_CALL_DYNAMIC &&
1922 insn->type != INSN_RETURN &&
1923 insn->type != INSN_NOP) {
1924 WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop",
1925 insn->sec, insn->offset);
1929 insn->retpoline_safe = true;
1935 static int read_instr_hints(struct objtool_file *file)
1937 struct section *sec;
1938 struct instruction *insn;
1939 struct reloc *reloc;
1941 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1945 list_for_each_entry(reloc, &sec->reloc_list, list) {
1946 if (reloc->sym->type != STT_SECTION) {
1947 WARN("unexpected relocation symbol type in %s", sec->name);
1951 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1953 WARN("bad .discard.instr_end entry");
1960 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1964 list_for_each_entry(reloc, &sec->reloc_list, list) {
1965 if (reloc->sym->type != STT_SECTION) {
1966 WARN("unexpected relocation symbol type in %s", sec->name);
1970 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1972 WARN("bad .discard.instr_begin entry");
1982 static int read_intra_function_calls(struct objtool_file *file)
1984 struct instruction *insn;
1985 struct section *sec;
1986 struct reloc *reloc;
1988 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1992 list_for_each_entry(reloc, &sec->reloc_list, list) {
1993 unsigned long dest_off;
1995 if (reloc->sym->type != STT_SECTION) {
1996 WARN("unexpected relocation symbol type in %s",
2001 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2003 WARN("bad .discard.intra_function_call entry");
2007 if (insn->type != INSN_CALL) {
2008 WARN_FUNC("intra_function_call not a direct call",
2009 insn->sec, insn->offset);
2014 * Treat intra-function CALLs as JMPs, but with a stack_op.
2015 * See add_call_destinations(), which strips stack_ops from
2018 insn->type = INSN_JUMP_UNCONDITIONAL;
2020 dest_off = insn->offset + insn->len + insn->immediate;
2021 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2022 if (!insn->jump_dest) {
2023 WARN_FUNC("can't find call dest at %s+0x%lx",
2024 insn->sec, insn->offset,
2025 insn->sec->name, dest_off);
2033 static int classify_symbols(struct objtool_file *file)
2035 struct section *sec;
2036 struct symbol *func;
2038 for_each_sec(file, sec) {
2039 list_for_each_entry(func, &sec->symbol_list, list) {
2040 if (func->bind != STB_GLOBAL)
2043 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2044 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2045 func->static_call_tramp = true;
2047 if (arch_is_retpoline(func))
2048 func->retpoline_thunk = true;
2050 if (arch_is_rethunk(func))
2051 func->return_thunk = true;
2053 if (!strcmp(func->name, "__fentry__"))
2054 func->fentry = true;
2056 if (!strncmp(func->name, "__sanitizer_cov_", 16))
2064 static void mark_rodata(struct objtool_file *file)
2066 struct section *sec;
2070 * Search for the following rodata sections, each of which can
2071 * potentially contain jump tables:
2073 * - .rodata: can contain GCC switch tables
2074 * - .rodata.<func>: same, if -fdata-sections is being used
2075 * - .rodata..c_jump_table: contains C annotated jump tables
2077 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2079 for_each_sec(file, sec) {
2080 if (!strncmp(sec->name, ".rodata", 7) &&
2081 !strstr(sec->name, ".str1.")) {
2087 file->rodata = found;
2090 static int decode_sections(struct objtool_file *file)
2096 ret = decode_instructions(file);
2100 ret = add_dead_ends(file);
2105 add_uaccess_safe(file);
2107 ret = add_ignore_alternatives(file);
2112 * Must be before add_{jump_call}_destination.
2114 ret = classify_symbols(file);
2119 * Must be before add_special_section_alts() as that depends on
2120 * jump_dest being set.
2122 ret = add_jump_destinations(file);
2126 ret = add_special_section_alts(file);
2131 * Must be before add_call_destination(); it changes INSN_CALL to
2134 ret = read_intra_function_calls(file);
2138 ret = add_call_destinations(file);
2142 ret = add_jump_table_alts(file);
2146 ret = read_unwind_hints(file);
2150 ret = read_retpoline_hints(file);
2154 ret = read_instr_hints(file);
2161 static bool is_fentry_call(struct instruction *insn)
2163 if (insn->type == INSN_CALL &&
2165 insn->call_dest->fentry)
2171 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2173 struct cfi_state *cfi = &state->cfi;
2176 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2179 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2182 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2185 for (i = 0; i < CFI_NUM_REGS; i++) {
2186 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2187 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2194 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2195 int expected_offset)
2197 return reg->base == CFI_CFA &&
2198 reg->offset == expected_offset;
2201 static bool has_valid_stack_frame(struct insn_state *state)
2203 struct cfi_state *cfi = &state->cfi;
2205 if (cfi->cfa.base == CFI_BP &&
2206 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2207 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2210 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2216 static int update_cfi_state_regs(struct instruction *insn,
2217 struct cfi_state *cfi,
2218 struct stack_op *op)
2220 struct cfi_reg *cfa = &cfi->cfa;
2222 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2226 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2230 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2233 /* add immediate to sp */
2234 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2235 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2236 cfa->offset -= op->src.offset;
2241 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2243 if (arch_callee_saved_reg(reg) &&
2244 cfi->regs[reg].base == CFI_UNDEFINED) {
2245 cfi->regs[reg].base = base;
2246 cfi->regs[reg].offset = offset;
2250 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2252 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2253 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2257 * A note about DRAP stack alignment:
2259 * GCC has the concept of a DRAP register, which is used to help keep track of
2260 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2261 * register. The typical DRAP pattern is:
2263 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2264 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2265 * 41 ff 72 f8 pushq -0x8(%r10)
2267 * 48 89 e5 mov %rsp,%rbp
2274 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2277 * There are some variations in the epilogues, like:
2285 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2290 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2291 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2292 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2293 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2295 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2298 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2299 * restored beforehand:
2302 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2303 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2305 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2309 static int update_cfi_state(struct instruction *insn,
2310 struct instruction *next_insn,
2311 struct cfi_state *cfi, struct stack_op *op)
2313 struct cfi_reg *cfa = &cfi->cfa;
2314 struct cfi_reg *regs = cfi->regs;
2316 /* stack operations don't make sense with an undefined CFA */
2317 if (cfa->base == CFI_UNDEFINED) {
2319 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2325 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2326 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2327 return update_cfi_state_regs(insn, cfi, op);
2329 switch (op->dest.type) {
2332 switch (op->src.type) {
2335 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2336 cfa->base == CFI_SP &&
2337 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2339 /* mov %rsp, %rbp */
2340 cfa->base = op->dest.reg;
2341 cfi->bp_scratch = false;
2344 else if (op->src.reg == CFI_SP &&
2345 op->dest.reg == CFI_BP && cfi->drap) {
2347 /* drap: mov %rsp, %rbp */
2348 regs[CFI_BP].base = CFI_BP;
2349 regs[CFI_BP].offset = -cfi->stack_size;
2350 cfi->bp_scratch = false;
2353 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2358 * This is needed for the rare case where GCC
2365 cfi->vals[op->dest.reg].base = CFI_CFA;
2366 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2369 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2370 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2375 * Restore the original stack pointer (Clang).
2377 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2380 else if (op->dest.reg == cfa->base) {
2382 /* mov %reg, %rsp */
2383 if (cfa->base == CFI_SP &&
2384 cfi->vals[op->src.reg].base == CFI_CFA) {
2387 * This is needed for the rare case
2388 * where GCC does something dumb like:
2390 * lea 0x8(%rsp), %rcx
2394 cfa->offset = -cfi->vals[op->src.reg].offset;
2395 cfi->stack_size = cfa->offset;
2397 } else if (cfa->base == CFI_SP &&
2398 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2399 cfi->vals[op->src.reg].offset == cfa->offset) {
2404 * 1: mov %rsp, (%[tos])
2405 * 2: mov %[tos], %rsp
2411 * 1 - places a pointer to the previous
2412 * stack at the Top-of-Stack of the
2415 * 2 - switches to the new stack.
2417 * 3 - pops the Top-of-Stack to restore
2418 * the original stack.
2420 * Note: we set base to SP_INDIRECT
2421 * here and preserve offset. Therefore
2422 * when the unwinder reaches ToS it
2423 * will dereference SP and then add the
2424 * offset to find the next frame, IOW:
2427 cfa->base = CFI_SP_INDIRECT;
2430 cfa->base = CFI_UNDEFINED;
2435 else if (op->dest.reg == CFI_SP &&
2436 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2437 cfi->vals[op->src.reg].offset == cfa->offset) {
2440 * The same stack swizzle case 2) as above. But
2441 * because we can't change cfa->base, case 3)
2442 * will become a regular POP. Pretend we're a
2443 * PUSH so things don't go unbalanced.
2445 cfi->stack_size += 8;
2452 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2455 cfi->stack_size -= op->src.offset;
2456 if (cfa->base == CFI_SP)
2457 cfa->offset -= op->src.offset;
2461 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2463 /* lea disp(%rbp), %rsp */
2464 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2468 if (!cfi->drap && op->src.reg == CFI_SP &&
2469 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2470 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2472 /* lea disp(%rsp), %rbp */
2474 cfa->offset -= op->src.offset;
2475 cfi->bp_scratch = false;
2479 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2481 /* drap: lea disp(%rsp), %drap */
2482 cfi->drap_reg = op->dest.reg;
2485 * lea disp(%rsp), %reg
2487 * This is needed for the rare case where GCC
2488 * does something dumb like:
2490 * lea 0x8(%rsp), %rcx
2494 cfi->vals[op->dest.reg].base = CFI_CFA;
2495 cfi->vals[op->dest.reg].offset = \
2496 -cfi->stack_size + op->src.offset;
2501 if (cfi->drap && op->dest.reg == CFI_SP &&
2502 op->src.reg == cfi->drap_reg) {
2504 /* drap: lea disp(%drap), %rsp */
2506 cfa->offset = cfi->stack_size = -op->src.offset;
2507 cfi->drap_reg = CFI_UNDEFINED;
2512 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2513 WARN_FUNC("unsupported stack register modification",
2514 insn->sec, insn->offset);
2521 if (op->dest.reg != CFI_SP ||
2522 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2523 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2524 WARN_FUNC("unsupported stack pointer realignment",
2525 insn->sec, insn->offset);
2529 if (cfi->drap_reg != CFI_UNDEFINED) {
2530 /* drap: and imm, %rsp */
2531 cfa->base = cfi->drap_reg;
2532 cfa->offset = cfi->stack_size = 0;
2537 * Older versions of GCC (4.8ish) realign the stack
2538 * without DRAP, with a frame pointer.
2545 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2547 /* pop %rsp; # restore from a stack swizzle */
2552 if (!cfi->drap && op->dest.reg == cfa->base) {
2558 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2559 op->dest.reg == cfi->drap_reg &&
2560 cfi->drap_offset == -cfi->stack_size) {
2562 /* drap: pop %drap */
2563 cfa->base = cfi->drap_reg;
2565 cfi->drap_offset = -1;
2567 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2570 restore_reg(cfi, op->dest.reg);
2573 cfi->stack_size -= 8;
2574 if (cfa->base == CFI_SP)
2579 case OP_SRC_REG_INDIRECT:
2580 if (!cfi->drap && op->dest.reg == cfa->base &&
2581 op->dest.reg == CFI_BP) {
2583 /* mov disp(%rsp), %rbp */
2585 cfa->offset = cfi->stack_size;
2588 if (cfi->drap && op->src.reg == CFI_BP &&
2589 op->src.offset == cfi->drap_offset) {
2591 /* drap: mov disp(%rbp), %drap */
2592 cfa->base = cfi->drap_reg;
2594 cfi->drap_offset = -1;
2597 if (cfi->drap && op->src.reg == CFI_BP &&
2598 op->src.offset == regs[op->dest.reg].offset) {
2600 /* drap: mov disp(%rbp), %reg */
2601 restore_reg(cfi, op->dest.reg);
2603 } else if (op->src.reg == cfa->base &&
2604 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2606 /* mov disp(%rbp), %reg */
2607 /* mov disp(%rsp), %reg */
2608 restore_reg(cfi, op->dest.reg);
2610 } else if (op->src.reg == CFI_SP &&
2611 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2613 /* mov disp(%rsp), %reg */
2614 restore_reg(cfi, op->dest.reg);
2620 WARN_FUNC("unknown stack-related instruction",
2621 insn->sec, insn->offset);
2629 cfi->stack_size += 8;
2630 if (cfa->base == CFI_SP)
2633 if (op->src.type != OP_SRC_REG)
2637 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2639 /* drap: push %drap */
2640 cfa->base = CFI_BP_INDIRECT;
2641 cfa->offset = -cfi->stack_size;
2643 /* save drap so we know when to restore it */
2644 cfi->drap_offset = -cfi->stack_size;
2646 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2648 /* drap: push %rbp */
2649 cfi->stack_size = 0;
2653 /* drap: push %reg */
2654 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2660 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2663 /* detect when asm code uses rbp as a scratch register */
2664 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2665 cfa->base != CFI_BP)
2666 cfi->bp_scratch = true;
2669 case OP_DEST_REG_INDIRECT:
2672 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2674 /* drap: mov %drap, disp(%rbp) */
2675 cfa->base = CFI_BP_INDIRECT;
2676 cfa->offset = op->dest.offset;
2678 /* save drap offset so we know when to restore it */
2679 cfi->drap_offset = op->dest.offset;
2682 /* drap: mov reg, disp(%rbp) */
2683 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2686 } else if (op->dest.reg == cfa->base) {
2688 /* mov reg, disp(%rbp) */
2689 /* mov reg, disp(%rsp) */
2690 save_reg(cfi, op->src.reg, CFI_CFA,
2691 op->dest.offset - cfi->cfa.offset);
2693 } else if (op->dest.reg == CFI_SP) {
2695 /* mov reg, disp(%rsp) */
2696 save_reg(cfi, op->src.reg, CFI_CFA,
2697 op->dest.offset - cfi->stack_size);
2699 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2701 /* mov %rsp, (%reg); # setup a stack swizzle. */
2702 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2703 cfi->vals[op->dest.reg].offset = cfa->offset;
2709 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2710 WARN_FUNC("unknown stack-related memory operation",
2711 insn->sec, insn->offset);
2716 cfi->stack_size -= 8;
2717 if (cfa->base == CFI_SP)
2723 WARN_FUNC("unknown stack-related instruction",
2724 insn->sec, insn->offset);
2732 * The stack layouts of alternatives instructions can sometimes diverge when
2733 * they have stack modifications. That's fine as long as the potential stack
2734 * layouts don't conflict at any given potential instruction boundary.
2736 * Flatten the CFIs of the different alternative code streams (both original
2737 * and replacement) into a single shared CFI array which can be used to detect
2738 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2740 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2742 struct cfi_state **alt_cfi;
2745 if (!insn->alt_group)
2749 WARN("CFI missing");
2753 alt_cfi = insn->alt_group->cfi;
2754 group_off = insn->offset - insn->alt_group->first_insn->offset;
2756 if (!alt_cfi[group_off]) {
2757 alt_cfi[group_off] = insn->cfi;
2759 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2760 WARN_FUNC("stack layout conflict in alternatives",
2761 insn->sec, insn->offset);
2769 static int handle_insn_ops(struct instruction *insn,
2770 struct instruction *next_insn,
2771 struct insn_state *state)
2773 struct stack_op *op;
2775 list_for_each_entry(op, &insn->stack_ops, list) {
2777 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2780 if (!insn->alt_group)
2783 if (op->dest.type == OP_DEST_PUSHF) {
2784 if (!state->uaccess_stack) {
2785 state->uaccess_stack = 1;
2786 } else if (state->uaccess_stack >> 31) {
2787 WARN_FUNC("PUSHF stack exhausted",
2788 insn->sec, insn->offset);
2791 state->uaccess_stack <<= 1;
2792 state->uaccess_stack |= state->uaccess;
2795 if (op->src.type == OP_SRC_POPF) {
2796 if (state->uaccess_stack) {
2797 state->uaccess = state->uaccess_stack & 1;
2798 state->uaccess_stack >>= 1;
2799 if (state->uaccess_stack == 1)
2800 state->uaccess_stack = 0;
2808 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2810 struct cfi_state *cfi1 = insn->cfi;
2814 WARN("CFI missing");
2818 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2820 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2821 insn->sec, insn->offset,
2822 cfi1->cfa.base, cfi1->cfa.offset,
2823 cfi2->cfa.base, cfi2->cfa.offset);
2825 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2826 for (i = 0; i < CFI_NUM_REGS; i++) {
2827 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2828 sizeof(struct cfi_reg)))
2831 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2832 insn->sec, insn->offset,
2833 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2834 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2838 } else if (cfi1->type != cfi2->type) {
2840 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2841 insn->sec, insn->offset, cfi1->type, cfi2->type);
2843 } else if (cfi1->drap != cfi2->drap ||
2844 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2845 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2847 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2848 insn->sec, insn->offset,
2849 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2850 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2858 static inline bool func_uaccess_safe(struct symbol *func)
2861 return func->uaccess_safe;
2866 static inline const char *call_dest_name(struct instruction *insn)
2868 if (insn->call_dest)
2869 return insn->call_dest->name;
2874 static inline bool noinstr_call_dest(struct symbol *func)
2877 * We can't deal with indirect function calls at present;
2878 * assume they're instrumented.
2884 * If the symbol is from a noinstr section; we good.
2886 if (func->sec->noinstr)
2890 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2891 * something 'BAD' happened. At the risk of taking the machine down,
2892 * let them proceed to get the message out.
2894 if (!strncmp(func->name, "__ubsan_handle_", 15))
2900 static int validate_call(struct instruction *insn, struct insn_state *state)
2902 if (state->noinstr && state->instr <= 0 &&
2903 !noinstr_call_dest(insn->call_dest)) {
2904 WARN_FUNC("call to %s() leaves .noinstr.text section",
2905 insn->sec, insn->offset, call_dest_name(insn));
2909 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2910 WARN_FUNC("call to %s() with UACCESS enabled",
2911 insn->sec, insn->offset, call_dest_name(insn));
2916 WARN_FUNC("call to %s() with DF set",
2917 insn->sec, insn->offset, call_dest_name(insn));
2924 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2926 if (has_modified_stack_frame(insn, state)) {
2927 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2928 insn->sec, insn->offset);
2932 return validate_call(insn, state);
2935 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2937 if (state->noinstr && state->instr > 0) {
2938 WARN_FUNC("return with instrumentation enabled",
2939 insn->sec, insn->offset);
2943 if (state->uaccess && !func_uaccess_safe(func)) {
2944 WARN_FUNC("return with UACCESS enabled",
2945 insn->sec, insn->offset);
2949 if (!state->uaccess && func_uaccess_safe(func)) {
2950 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2951 insn->sec, insn->offset);
2956 WARN_FUNC("return with DF set",
2957 insn->sec, insn->offset);
2961 if (func && has_modified_stack_frame(insn, state)) {
2962 WARN_FUNC("return with modified stack frame",
2963 insn->sec, insn->offset);
2967 if (state->cfi.bp_scratch) {
2968 WARN_FUNC("BP used as a scratch register",
2969 insn->sec, insn->offset);
2976 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2977 struct instruction *insn)
2979 struct alt_group *alt_group = insn->alt_group;
2982 * Simulate the fact that alternatives are patched in-place. When the
2983 * end of a replacement alt_group is reached, redirect objtool flow to
2984 * the end of the original alt_group.
2986 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2987 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2989 return next_insn_same_sec(file, insn);
2993 * Follow the branch starting at the given instruction, and recursively follow
2994 * any other branches (jumps). Meanwhile, track the frame pointer state at
2995 * each instruction and validate all the rules described in
2996 * tools/objtool/Documentation/stack-validation.txt.
2998 static int validate_branch(struct objtool_file *file, struct symbol *func,
2999 struct instruction *insn, struct insn_state state)
3001 struct alternative *alt;
3002 struct instruction *next_insn, *prev_insn = NULL;
3003 struct section *sec;
3010 next_insn = next_insn_to_validate(file, insn);
3012 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
3013 WARN("%s() falls through to next function %s()",
3014 func->name, insn->func->name);
3018 if (func && insn->ignore) {
3019 WARN_FUNC("BUG: why am I validating an ignored function?",
3024 visited = VISITED_BRANCH << state.uaccess;
3025 if (insn->visited & VISITED_BRANCH_MASK) {
3026 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3029 if (insn->visited & visited)
3036 state.instr += insn->instr;
3039 if (insn->restore) {
3040 struct instruction *save_insn, *i;
3045 sym_for_each_insn_continue_reverse(file, func, i) {
3053 WARN_FUNC("no corresponding CFI save for CFI restore",
3058 if (!save_insn->visited) {
3059 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
3064 insn->cfi = save_insn->cfi;
3068 state.cfi = *insn->cfi;
3070 /* XXX track if we actually changed state.cfi */
3072 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3073 insn->cfi = prev_insn->cfi;
3076 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3080 insn->visited |= visited;
3082 if (propagate_alt_cfi(file, insn))
3085 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3086 bool skip_orig = false;
3088 list_for_each_entry(alt, &insn->alts, list) {
3092 ret = validate_branch(file, func, alt->insn, state);
3095 BT_FUNC("(alt)", insn);
3104 if (handle_insn_ops(insn, next_insn, &state))
3107 switch (insn->type) {
3110 if (sls && !insn->retpoline_safe &&
3111 next_insn && next_insn->type != INSN_TRAP) {
3112 WARN_FUNC("missing int3 after ret",
3113 insn->sec, insn->offset);
3115 return validate_return(func, insn, &state);
3118 case INSN_CALL_DYNAMIC:
3119 ret = validate_call(insn, &state);
3123 if (!no_fp && func && !is_fentry_call(insn) &&
3124 !has_valid_stack_frame(&state)) {
3125 WARN_FUNC("call without frame pointer save/setup",
3130 if (dead_end_function(file, insn->call_dest))
3135 case INSN_JUMP_CONDITIONAL:
3136 case INSN_JUMP_UNCONDITIONAL:
3137 if (is_sibling_call(insn)) {
3138 ret = validate_sibling_call(insn, &state);
3142 } else if (insn->jump_dest) {
3143 ret = validate_branch(file, func,
3144 insn->jump_dest, state);
3147 BT_FUNC("(branch)", insn);
3152 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3157 case INSN_JUMP_DYNAMIC:
3158 if (sls && !insn->retpoline_safe &&
3159 next_insn && next_insn->type != INSN_TRAP) {
3160 WARN_FUNC("missing int3 after indirect jump",
3161 insn->sec, insn->offset);
3165 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3166 if (is_sibling_call(insn)) {
3167 ret = validate_sibling_call(insn, &state);
3172 if (insn->type == INSN_JUMP_DYNAMIC)
3177 case INSN_CONTEXT_SWITCH:
3178 if (func && (!next_insn || !next_insn->hint)) {
3179 WARN_FUNC("unsupported instruction in callable function",
3186 if (state.uaccess) {
3187 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3191 state.uaccess = true;
3195 if (!state.uaccess && func) {
3196 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3200 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3201 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3205 state.uaccess = false;
3210 WARN_FUNC("recursive STD", sec, insn->offset);
3218 if (!state.df && func) {
3219 WARN_FUNC("redundant CLD", sec, insn->offset);
3234 if (state.cfi.cfa.base == CFI_UNDEFINED)
3236 WARN("%s: unexpected end of section", sec->name);
3247 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3249 struct instruction *insn;
3250 struct insn_state state;
3251 int ret, warnings = 0;
3256 init_insn_state(&state, sec);
3259 insn = find_insn(file, sec, 0);
3263 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3266 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3267 if (insn->hint && !insn->visited) {
3268 ret = validate_branch(file, insn->func, insn, state);
3269 if (ret && backtrace)
3270 BT_FUNC("<=== (hint)", insn);
3274 insn = list_next_entry(insn, list);
3281 * Validate rethunk entry constraint: must untrain RET before the first RET.
3283 * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes
3284 * before an actual RET instruction.
3286 static int validate_entry(struct objtool_file *file, struct instruction *insn)
3288 struct instruction *next, *dest;
3289 int ret, warnings = 0;
3292 next = next_insn_to_validate(file, insn);
3294 if (insn->visited & VISITED_ENTRY)
3297 insn->visited |= VISITED_ENTRY;
3299 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3300 struct alternative *alt;
3301 bool skip_orig = false;
3303 list_for_each_entry(alt, &insn->alts, list) {
3307 ret = validate_entry(file, alt->insn);
3310 BT_FUNC("(alt)", insn);
3319 switch (insn->type) {
3321 case INSN_CALL_DYNAMIC:
3322 case INSN_JUMP_DYNAMIC:
3323 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3324 WARN_FUNC("early indirect call", insn->sec, insn->offset);
3327 case INSN_JUMP_UNCONDITIONAL:
3328 case INSN_JUMP_CONDITIONAL:
3329 if (!is_sibling_call(insn)) {
3330 if (!insn->jump_dest) {
3331 WARN_FUNC("unresolved jump target after linking?!?",
3332 insn->sec, insn->offset);
3335 ret = validate_entry(file, insn->jump_dest);
3338 BT_FUNC("(branch%s)", insn,
3339 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
3344 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3352 dest = find_insn(file, insn->call_dest->sec,
3353 insn->call_dest->offset);
3355 WARN("Unresolved function after linking!?: %s",
3356 insn->call_dest->name);
3360 ret = validate_entry(file, dest);
3363 BT_FUNC("(call)", insn);
3367 * If a call returns without error, it must have seen UNTRAIN_RET.
3368 * Therefore any non-error return is a success.
3373 WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset);
3377 if (insn->retpoline_safe)
3386 WARN_FUNC("teh end!", insn->sec, insn->offset);
3396 * Validate that all branches starting at 'insn->entry' encounter UNRET_END
3399 static int validate_unret(struct objtool_file *file)
3401 struct instruction *insn;
3402 int ret, warnings = 0;
3404 for_each_insn(file, insn) {
3408 ret = validate_entry(file, insn);
3410 WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset);
3419 static int validate_retpoline(struct objtool_file *file)
3421 struct instruction *insn;
3424 for_each_insn(file, insn) {
3425 if (insn->type != INSN_JUMP_DYNAMIC &&
3426 insn->type != INSN_CALL_DYNAMIC &&
3427 insn->type != INSN_RETURN)
3430 if (insn->retpoline_safe)
3434 * .init.text code is ran before userspace and thus doesn't
3435 * strictly need retpolines, except for modules which are
3436 * loaded late, they very much do need retpoline in their
3439 if (!strcmp(insn->sec->name, ".init.text") && !module)
3442 if (insn->type == INSN_RETURN) {
3444 WARN_FUNC("'naked' return found in RETHUNK build",
3445 insn->sec, insn->offset);
3449 WARN_FUNC("indirect %s found in RETPOLINE build",
3450 insn->sec, insn->offset,
3451 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3460 static bool is_kasan_insn(struct instruction *insn)
3462 return (insn->type == INSN_CALL &&
3463 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3466 static bool is_ubsan_insn(struct instruction *insn)
3468 return (insn->type == INSN_CALL &&
3469 !strcmp(insn->call_dest->name,
3470 "__ubsan_handle_builtin_unreachable"));
3473 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3476 struct instruction *prev_insn;
3478 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3482 * Ignore any unused exceptions. This can happen when a whitelisted
3483 * function has an exception table entry.
3485 * Also ignore alternative replacement instructions. This can happen
3486 * when a whitelisted function uses one of the ALTERNATIVE macros.
3488 if (!strcmp(insn->sec->name, ".fixup") ||
3489 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3490 !strcmp(insn->sec->name, ".altinstr_aux"))
3497 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3498 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3499 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3500 * (or occasionally a JMP to UD2).
3502 * It may also insert a UD2 after calling a __noreturn function.
3504 prev_insn = list_prev_entry(insn, list);
3505 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3506 (insn->type == INSN_BUG ||
3507 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3508 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3512 * Check if this (or a subsequent) instruction is related to
3513 * CONFIG_UBSAN or CONFIG_KASAN.
3515 * End the search at 5 instructions to avoid going into the weeds.
3517 for (i = 0; i < 5; i++) {
3519 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3522 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3523 if (insn->jump_dest &&
3524 insn->jump_dest->func == insn->func) {
3525 insn = insn->jump_dest;
3532 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3535 insn = list_next_entry(insn, list);
3541 static int validate_symbol(struct objtool_file *file, struct section *sec,
3542 struct symbol *sym, struct insn_state *state)
3544 struct instruction *insn;
3548 WARN("%s() is missing an ELF size annotation", sym->name);
3552 if (sym->pfunc != sym || sym->alias != sym)
3555 insn = find_insn(file, sec, sym->offset);
3556 if (!insn || insn->ignore || insn->visited)
3559 state->uaccess = sym->uaccess_safe;
3561 ret = validate_branch(file, insn->func, insn, *state);
3562 if (ret && backtrace)
3563 BT_FUNC("<=== (sym)", insn);
3567 static int validate_section(struct objtool_file *file, struct section *sec)
3569 struct insn_state state;
3570 struct symbol *func;
3573 list_for_each_entry(func, &sec->symbol_list, list) {
3574 if (func->type != STT_FUNC)
3577 init_insn_state(&state, sec);
3578 set_func_state(&state.cfi);
3580 warnings += validate_symbol(file, sec, func, &state);
3586 static int validate_vmlinux_functions(struct objtool_file *file)
3588 struct section *sec;
3591 sec = find_section_by_name(file->elf, ".noinstr.text");
3593 warnings += validate_section(file, sec);
3594 warnings += validate_unwind_hints(file, sec);
3597 sec = find_section_by_name(file->elf, ".entry.text");
3599 warnings += validate_section(file, sec);
3600 warnings += validate_unwind_hints(file, sec);
3606 static int validate_functions(struct objtool_file *file)
3608 struct section *sec;
3611 for_each_sec(file, sec) {
3612 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3615 warnings += validate_section(file, sec);
3621 static int validate_reachable_instructions(struct objtool_file *file)
3623 struct instruction *insn;
3625 if (file->ignore_unreachables)
3628 for_each_insn(file, insn) {
3629 if (insn->visited || ignore_unreachable_insn(file, insn))
3632 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3639 int check(struct objtool_file *file)
3641 int ret, warnings = 0;
3643 arch_initial_func_cfi_state(&initial_func_cfi);
3644 init_cfi_state(&init_cfi);
3645 init_cfi_state(&func_cfi);
3646 set_func_state(&func_cfi);
3648 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3651 cfi_hash_add(&init_cfi);
3652 cfi_hash_add(&func_cfi);
3654 ret = decode_sections(file);
3660 if (list_empty(&file->insn_list))
3663 if (vmlinux && !validate_dup) {
3664 ret = validate_vmlinux_functions(file);
3673 ret = validate_retpoline(file);
3679 ret = validate_functions(file);
3684 ret = validate_unwind_hints(file, NULL);
3691 * Must be after validate_branch() and friends, it plays
3692 * further games with insn->visited.
3694 ret = validate_unret(file);
3701 ret = validate_reachable_instructions(file);
3707 ret = create_static_call_sections(file);
3713 ret = create_retpoline_sites_sections(file);
3720 ret = create_return_sites_sections(file);
3727 ret = create_mcount_loc_sections(file);
3734 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3735 printf("nr_cfi: %ld\n", nr_cfi);
3736 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3737 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3742 * For now, don't fail the kernel build on fatal warnings. These
3743 * errors are still fairly common due to the growing matrix of
3744 * supported toolchains and their recent pace of change.