1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
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[] = {
172 "__module_put_and_exit",
178 "machine_real_restart",
179 "rewind_stack_and_make_dead",
180 "kunit_try_catch_throw",
182 "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);
201 if (!insn || !insn->func)
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).
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(void)
331 cfi_bits = vmlinux ? ELF_HASH_BITS - 3 : 13;
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") ||
372 !strncmp(sec->name, ".text..__x86.", 13))
375 for (offset = 0; offset < sec->len; offset += insn->len) {
376 insn = malloc(sizeof(*insn));
378 WARN("malloc failed");
381 memset(insn, 0, sizeof(*insn));
382 INIT_LIST_HEAD(&insn->alts);
383 INIT_LIST_HEAD(&insn->stack_ops);
386 insn->offset = offset;
388 ret = arch_decode_instruction(file->elf, sec, offset,
390 &insn->len, &insn->type,
396 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
397 list_add_tail(&insn->list, &file->insn_list);
401 list_for_each_entry(func, &sec->symbol_list, list) {
402 if (func->type != STT_FUNC || func->alias != func)
405 if (!find_insn(file, sec, func->offset)) {
406 WARN("%s(): can't find starting instruction",
411 sym_for_each_insn(file, func, insn)
417 printf("nr_insns: %lu\n", nr_insns);
426 static struct instruction *find_last_insn(struct objtool_file *file,
429 struct instruction *insn = NULL;
431 unsigned int end = (sec->len > 10) ? sec->len - 10 : 0;
433 for (offset = sec->len - 1; offset >= end && !insn; offset--)
434 insn = find_insn(file, sec, offset);
440 * Mark "ud2" instructions and manually annotated dead ends.
442 static int add_dead_ends(struct objtool_file *file)
446 struct instruction *insn;
449 * By default, "ud2" is a dead end unless otherwise annotated, because
450 * GCC 7 inserts it for certain divide-by-zero cases.
452 for_each_insn(file, insn)
453 if (insn->type == INSN_BUG)
454 insn->dead_end = true;
457 * Check for manually annotated dead ends.
459 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
463 list_for_each_entry(reloc, &sec->reloc_list, list) {
464 if (reloc->sym->type != STT_SECTION) {
465 WARN("unexpected relocation symbol type in %s", sec->name);
468 insn = find_insn(file, reloc->sym->sec, reloc->addend);
470 insn = list_prev_entry(insn, list);
471 else if (reloc->addend == reloc->sym->sec->len) {
472 insn = find_last_insn(file, reloc->sym->sec);
474 WARN("can't find unreachable insn at %s+0x%" PRIx64,
475 reloc->sym->sec->name, reloc->addend);
479 WARN("can't find unreachable insn at %s+0x%" PRIx64,
480 reloc->sym->sec->name, reloc->addend);
484 insn->dead_end = true;
489 * These manually annotated reachable checks are needed for GCC 4.4,
490 * where the Linux unreachable() macro isn't supported. In that case
491 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
494 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
498 list_for_each_entry(reloc, &sec->reloc_list, list) {
499 if (reloc->sym->type != STT_SECTION) {
500 WARN("unexpected relocation symbol type in %s", sec->name);
503 insn = find_insn(file, reloc->sym->sec, reloc->addend);
505 insn = list_prev_entry(insn, list);
506 else if (reloc->addend == reloc->sym->sec->len) {
507 insn = find_last_insn(file, reloc->sym->sec);
509 WARN("can't find reachable insn at %s+0x%" PRIx64,
510 reloc->sym->sec->name, reloc->addend);
514 WARN("can't find reachable insn at %s+0x%" PRIx64,
515 reloc->sym->sec->name, reloc->addend);
519 insn->dead_end = false;
525 static int create_static_call_sections(struct objtool_file *file)
528 struct static_call_site *site;
529 struct instruction *insn;
530 struct symbol *key_sym;
531 char *key_name, *tmp;
534 sec = find_section_by_name(file->elf, ".static_call_sites");
536 INIT_LIST_HEAD(&file->static_call_list);
537 WARN("file already has .static_call_sites section, skipping");
541 if (list_empty(&file->static_call_list))
545 list_for_each_entry(insn, &file->static_call_list, call_node)
548 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
549 sizeof(struct static_call_site), idx);
554 list_for_each_entry(insn, &file->static_call_list, call_node) {
556 site = (struct static_call_site *)sec->data->d_buf + idx;
557 memset(site, 0, sizeof(struct static_call_site));
559 /* populate reloc for 'addr' */
560 if (elf_add_reloc_to_insn(file->elf, sec,
561 idx * sizeof(struct static_call_site),
563 insn->sec, insn->offset))
566 /* find key symbol */
567 key_name = strdup(insn->call_dest->name);
572 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
573 STATIC_CALL_TRAMP_PREFIX_LEN)) {
574 WARN("static_call: trampoline name malformed: %s", key_name);
578 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
579 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
581 key_sym = find_symbol_by_name(file->elf, tmp);
584 WARN("static_call: can't find static_call_key symbol: %s", tmp);
590 * For modules(), the key might not be exported, which
591 * means the module can make static calls but isn't
592 * allowed to change them.
594 * In that case we temporarily set the key to be the
595 * trampoline address. This is fixed up in
596 * static_call_add_module().
598 key_sym = insn->call_dest;
602 /* populate reloc for 'key' */
603 if (elf_add_reloc(file->elf, sec,
604 idx * sizeof(struct static_call_site) + 4,
605 R_X86_64_PC32, key_sym,
606 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
615 static int create_retpoline_sites_sections(struct objtool_file *file)
617 struct instruction *insn;
621 sec = find_section_by_name(file->elf, ".retpoline_sites");
623 WARN("file already has .retpoline_sites, skipping");
628 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
634 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
637 WARN("elf_create_section: .retpoline_sites");
642 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
644 int *site = (int *)sec->data->d_buf + idx;
647 if (elf_add_reloc_to_insn(file->elf, sec,
650 insn->sec, insn->offset)) {
651 WARN("elf_add_reloc_to_insn: .retpoline_sites");
661 static int create_return_sites_sections(struct objtool_file *file)
663 struct instruction *insn;
667 sec = find_section_by_name(file->elf, ".return_sites");
669 WARN("file already has .return_sites, skipping");
674 list_for_each_entry(insn, &file->return_thunk_list, call_node)
680 sec = elf_create_section(file->elf, ".return_sites", 0,
683 WARN("elf_create_section: .return_sites");
688 list_for_each_entry(insn, &file->return_thunk_list, call_node) {
690 int *site = (int *)sec->data->d_buf + idx;
693 if (elf_add_reloc_to_insn(file->elf, sec,
696 insn->sec, insn->offset)) {
697 WARN("elf_add_reloc_to_insn: .return_sites");
708 * Warnings shouldn't be reported for ignored functions.
710 static void add_ignores(struct objtool_file *file)
712 struct instruction *insn;
717 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
721 list_for_each_entry(reloc, &sec->reloc_list, list) {
722 switch (reloc->sym->type) {
728 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
734 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
738 func_for_each_insn(file, func, insn)
744 * This is a whitelist of functions that is allowed to be called with AC set.
745 * The list is meant to be minimal and only contains compiler instrumentation
746 * ABI and a few functions used to implement *_{to,from}_user() functions.
748 * These functions must not directly change AC, but may PUSHF/POPF.
750 static const char *uaccess_safe_builtin[] = {
753 "check_memory_region",
754 /* KASAN out-of-line */
755 "__asan_loadN_noabort",
756 "__asan_load1_noabort",
757 "__asan_load2_noabort",
758 "__asan_load4_noabort",
759 "__asan_load8_noabort",
760 "__asan_load16_noabort",
761 "__asan_storeN_noabort",
762 "__asan_store1_noabort",
763 "__asan_store2_noabort",
764 "__asan_store4_noabort",
765 "__asan_store8_noabort",
766 "__asan_store16_noabort",
767 "__kasan_check_read",
768 "__kasan_check_write",
770 "__asan_report_load_n_noabort",
771 "__asan_report_load1_noabort",
772 "__asan_report_load2_noabort",
773 "__asan_report_load4_noabort",
774 "__asan_report_load8_noabort",
775 "__asan_report_load16_noabort",
776 "__asan_report_store_n_noabort",
777 "__asan_report_store1_noabort",
778 "__asan_report_store2_noabort",
779 "__asan_report_store4_noabort",
780 "__asan_report_store8_noabort",
781 "__asan_report_store16_noabort",
783 "__kcsan_check_access",
784 "kcsan_found_watchpoint",
785 "kcsan_setup_watchpoint",
786 "kcsan_check_scoped_accesses",
787 "kcsan_disable_current",
788 "kcsan_enable_current_nowarn",
793 "__tsan_write_range",
804 "__tsan_read_write1",
805 "__tsan_read_write2",
806 "__tsan_read_write4",
807 "__tsan_read_write8",
808 "__tsan_read_write16",
809 "__tsan_volatile_read1",
810 "__tsan_volatile_read2",
811 "__tsan_volatile_read4",
812 "__tsan_volatile_read8",
813 "__tsan_volatile_read16",
814 "__tsan_volatile_write1",
815 "__tsan_volatile_write2",
816 "__tsan_volatile_write4",
817 "__tsan_volatile_write8",
818 "__tsan_volatile_write16",
819 "__tsan_atomic8_load",
820 "__tsan_atomic16_load",
821 "__tsan_atomic32_load",
822 "__tsan_atomic64_load",
823 "__tsan_atomic8_store",
824 "__tsan_atomic16_store",
825 "__tsan_atomic32_store",
826 "__tsan_atomic64_store",
827 "__tsan_atomic8_exchange",
828 "__tsan_atomic16_exchange",
829 "__tsan_atomic32_exchange",
830 "__tsan_atomic64_exchange",
831 "__tsan_atomic8_fetch_add",
832 "__tsan_atomic16_fetch_add",
833 "__tsan_atomic32_fetch_add",
834 "__tsan_atomic64_fetch_add",
835 "__tsan_atomic8_fetch_sub",
836 "__tsan_atomic16_fetch_sub",
837 "__tsan_atomic32_fetch_sub",
838 "__tsan_atomic64_fetch_sub",
839 "__tsan_atomic8_fetch_and",
840 "__tsan_atomic16_fetch_and",
841 "__tsan_atomic32_fetch_and",
842 "__tsan_atomic64_fetch_and",
843 "__tsan_atomic8_fetch_or",
844 "__tsan_atomic16_fetch_or",
845 "__tsan_atomic32_fetch_or",
846 "__tsan_atomic64_fetch_or",
847 "__tsan_atomic8_fetch_xor",
848 "__tsan_atomic16_fetch_xor",
849 "__tsan_atomic32_fetch_xor",
850 "__tsan_atomic64_fetch_xor",
851 "__tsan_atomic8_fetch_nand",
852 "__tsan_atomic16_fetch_nand",
853 "__tsan_atomic32_fetch_nand",
854 "__tsan_atomic64_fetch_nand",
855 "__tsan_atomic8_compare_exchange_strong",
856 "__tsan_atomic16_compare_exchange_strong",
857 "__tsan_atomic32_compare_exchange_strong",
858 "__tsan_atomic64_compare_exchange_strong",
859 "__tsan_atomic8_compare_exchange_weak",
860 "__tsan_atomic16_compare_exchange_weak",
861 "__tsan_atomic32_compare_exchange_weak",
862 "__tsan_atomic64_compare_exchange_weak",
863 "__tsan_atomic8_compare_exchange_val",
864 "__tsan_atomic16_compare_exchange_val",
865 "__tsan_atomic32_compare_exchange_val",
866 "__tsan_atomic64_compare_exchange_val",
867 "__tsan_atomic_thread_fence",
868 "__tsan_atomic_signal_fence",
869 "__tsan_unaligned_read16",
870 "__tsan_unaligned_write16",
874 "__sanitizer_cov_trace_pc",
875 "__sanitizer_cov_trace_const_cmp1",
876 "__sanitizer_cov_trace_const_cmp2",
877 "__sanitizer_cov_trace_const_cmp4",
878 "__sanitizer_cov_trace_const_cmp8",
879 "__sanitizer_cov_trace_cmp1",
880 "__sanitizer_cov_trace_cmp2",
881 "__sanitizer_cov_trace_cmp4",
882 "__sanitizer_cov_trace_cmp8",
883 "__sanitizer_cov_trace_switch",
885 "ubsan_type_mismatch_common",
886 "__ubsan_handle_type_mismatch",
887 "__ubsan_handle_type_mismatch_v1",
888 "__ubsan_handle_shift_out_of_bounds",
890 "csum_partial_copy_generic",
892 "copy_mc_fragile_handle_tail",
893 "copy_mc_enhanced_fast_string",
894 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
898 static void add_uaccess_safe(struct objtool_file *file)
906 for (name = uaccess_safe_builtin; *name; name++) {
907 func = find_symbol_by_name(file->elf, *name);
911 func->uaccess_safe = true;
916 * FIXME: For now, just ignore any alternatives which add retpolines. This is
917 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
918 * But it at least allows objtool to understand the control flow *around* the
921 static int add_ignore_alternatives(struct objtool_file *file)
925 struct instruction *insn;
927 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
931 list_for_each_entry(reloc, &sec->reloc_list, list) {
932 if (reloc->sym->type != STT_SECTION) {
933 WARN("unexpected relocation symbol type in %s", sec->name);
937 insn = find_insn(file, reloc->sym->sec, reloc->addend);
939 WARN("bad .discard.ignore_alts entry");
943 insn->ignore_alts = true;
950 * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol
951 * will be added to the .retpoline_sites section.
953 __weak bool arch_is_retpoline(struct symbol *sym)
959 * Symbols that replace INSN_RETURN, every (tail) call to such a symbol
960 * will be added to the .return_sites section.
962 __weak bool arch_is_rethunk(struct symbol *sym)
968 * Symbols that are embedded inside other instructions, because sometimes crazy
969 * code exists. These are mostly ignored for validation purposes.
971 __weak bool arch_is_embedded_insn(struct symbol *sym)
976 #define NEGATIVE_RELOC ((void *)-1L)
978 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
980 if (insn->reloc == NEGATIVE_RELOC)
984 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
985 insn->offset, insn->len);
987 insn->reloc = NEGATIVE_RELOC;
995 static void remove_insn_ops(struct instruction *insn)
997 struct stack_op *op, *tmp;
999 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1000 list_del(&op->list);
1005 static void annotate_call_site(struct objtool_file *file,
1006 struct instruction *insn, bool sibling)
1008 struct reloc *reloc = insn_reloc(file, insn);
1009 struct symbol *sym = insn->call_dest;
1015 * Alternative replacement code is just template code which is
1016 * sometimes copied to the original instruction. For now, don't
1017 * annotate it. (In the future we might consider annotating the
1018 * original instruction if/when it ever makes sense to do so.)
1020 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1023 if (sym->static_call_tramp) {
1024 list_add_tail(&insn->call_node, &file->static_call_list);
1028 if (sym->retpoline_thunk) {
1029 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1034 * Many compilers cannot disable KCOV with a function attribute
1035 * so they need a little help, NOP out any KCOV calls from noinstr
1038 if (insn->sec->noinstr && sym->kcov) {
1040 reloc->type = R_NONE;
1041 elf_write_reloc(file->elf, reloc);
1044 elf_write_insn(file->elf, insn->sec,
1045 insn->offset, insn->len,
1046 sibling ? arch_ret_insn(insn->len)
1047 : arch_nop_insn(insn->len));
1049 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1053 * We've replaced the tail-call JMP insn by two new
1054 * insn: RET; INT3, except we only have a single struct
1055 * insn here. Mark it retpoline_safe to avoid the SLS
1056 * warning, instead of adding another insn.
1058 insn->retpoline_safe = true;
1065 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1066 struct symbol *dest, bool sibling)
1068 insn->call_dest = dest;
1073 * Whatever stack impact regular CALLs have, should be undone
1074 * by the RETURN of the called function.
1076 * Annotated intra-function calls retain the stack_ops but
1077 * are converted to JUMP, see read_intra_function_calls().
1079 remove_insn_ops(insn);
1081 annotate_call_site(file, insn, sibling);
1084 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1087 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1088 * so convert them accordingly.
1090 switch (insn->type) {
1092 insn->type = INSN_CALL_DYNAMIC;
1094 case INSN_JUMP_UNCONDITIONAL:
1095 insn->type = INSN_JUMP_DYNAMIC;
1097 case INSN_JUMP_CONDITIONAL:
1098 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1104 insn->retpoline_safe = true;
1107 * Whatever stack impact regular CALLs have, should be undone
1108 * by the RETURN of the called function.
1110 * Annotated intra-function calls retain the stack_ops but
1111 * are converted to JUMP, see read_intra_function_calls().
1113 remove_insn_ops(insn);
1115 annotate_call_site(file, insn, false);
1118 static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
1121 * Return thunk tail calls are really just returns in disguise,
1122 * so convert them accordingly.
1124 insn->type = INSN_RETURN;
1125 insn->retpoline_safe = true;
1127 /* Skip the non-text sections, specially .discard ones */
1128 if (add && insn->sec->text)
1129 list_add_tail(&insn->call_node, &file->return_thunk_list);
1133 * Find the destination instructions for all jumps.
1135 static int add_jump_destinations(struct objtool_file *file)
1137 struct instruction *insn;
1138 struct reloc *reloc;
1139 struct section *dest_sec;
1140 unsigned long dest_off;
1142 for_each_insn(file, insn) {
1143 if (!is_static_jump(insn))
1146 reloc = insn_reloc(file, insn);
1148 dest_sec = insn->sec;
1149 dest_off = arch_jump_destination(insn);
1150 } else if (reloc->sym->type == STT_SECTION) {
1151 dest_sec = reloc->sym->sec;
1152 dest_off = arch_dest_reloc_offset(reloc->addend);
1153 } else if (reloc->sym->retpoline_thunk) {
1154 add_retpoline_call(file, insn);
1156 } else if (reloc->sym->return_thunk) {
1157 add_return_call(file, insn, true);
1159 } else if (insn->func) {
1160 /* internal or external sibling call (with reloc) */
1161 add_call_dest(file, insn, reloc->sym, true);
1163 } else if (reloc->sym->sec->idx) {
1164 dest_sec = reloc->sym->sec;
1165 dest_off = reloc->sym->sym.st_value +
1166 arch_dest_reloc_offset(reloc->addend);
1168 /* non-func asm code jumping to another file */
1172 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1173 if (!insn->jump_dest) {
1174 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1177 * This is a special case where an alt instruction
1178 * jumps past the end of the section. These are
1179 * handled later in handle_group_alt().
1181 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1185 * This is a special case for retbleed_untrain_ret().
1186 * It jumps to __x86_return_thunk(), but objtool
1187 * can't find the thunk's starting RET
1188 * instruction, because the RET is also in the
1189 * middle of another instruction. Objtool only
1190 * knows about the outer instruction.
1192 if (sym && sym->embedded_insn) {
1193 add_return_call(file, insn, false);
1197 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1198 insn->sec, insn->offset, dest_sec->name,
1204 * Cross-function jump.
1206 if (insn->func && insn->jump_dest->func &&
1207 insn->func != insn->jump_dest->func) {
1210 * For GCC 8+, create parent/child links for any cold
1211 * subfunctions. This is _mostly_ redundant with a
1212 * similar initialization in read_symbols().
1214 * If a function has aliases, we want the *first* such
1215 * function in the symbol table to be the subfunction's
1216 * parent. In that case we overwrite the
1217 * initialization done in read_symbols().
1219 * However this code can't completely replace the
1220 * read_symbols() code because this doesn't detect the
1221 * case where the parent function's only reference to a
1222 * subfunction is through a jump table.
1224 if (!strstr(insn->func->name, ".cold") &&
1225 strstr(insn->jump_dest->func->name, ".cold")) {
1226 insn->func->cfunc = insn->jump_dest->func;
1227 insn->jump_dest->func->pfunc = insn->func;
1229 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1230 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1231 /* internal sibling call (without reloc) */
1232 add_call_dest(file, insn, insn->jump_dest->func, true);
1240 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1242 struct symbol *call_dest;
1244 call_dest = find_func_by_offset(sec, offset);
1246 call_dest = find_symbol_by_offset(sec, offset);
1252 * Find the destination instructions for all calls.
1254 static int add_call_destinations(struct objtool_file *file)
1256 struct instruction *insn;
1257 unsigned long dest_off;
1258 struct symbol *dest;
1259 struct reloc *reloc;
1261 for_each_insn(file, insn) {
1262 if (insn->type != INSN_CALL)
1265 reloc = insn_reloc(file, insn);
1267 dest_off = arch_jump_destination(insn);
1268 dest = find_call_destination(insn->sec, dest_off);
1270 add_call_dest(file, insn, dest, false);
1275 if (!insn->call_dest) {
1276 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1280 if (insn->func && insn->call_dest->type != STT_FUNC) {
1281 WARN_FUNC("unsupported call to non-function",
1282 insn->sec, insn->offset);
1286 } else if (reloc->sym->type == STT_SECTION) {
1287 dest_off = arch_dest_reloc_offset(reloc->addend);
1288 dest = find_call_destination(reloc->sym->sec, dest_off);
1290 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1291 insn->sec, insn->offset,
1292 reloc->sym->sec->name,
1297 add_call_dest(file, insn, dest, false);
1299 } else if (reloc->sym->retpoline_thunk) {
1300 add_retpoline_call(file, insn);
1303 add_call_dest(file, insn, reloc->sym, false);
1310 * The .alternatives section requires some extra special care over and above
1311 * other special sections because alternatives are patched in place.
1313 static int handle_group_alt(struct objtool_file *file,
1314 struct special_alt *special_alt,
1315 struct instruction *orig_insn,
1316 struct instruction **new_insn)
1318 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1319 struct alt_group *orig_alt_group, *new_alt_group;
1320 unsigned long dest_off;
1323 orig_alt_group = malloc(sizeof(*orig_alt_group));
1324 if (!orig_alt_group) {
1325 WARN("malloc failed");
1328 orig_alt_group->cfi = calloc(special_alt->orig_len,
1329 sizeof(struct cfi_state *));
1330 if (!orig_alt_group->cfi) {
1331 WARN("calloc failed");
1335 last_orig_insn = NULL;
1337 sec_for_each_insn_from(file, insn) {
1338 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1341 insn->alt_group = orig_alt_group;
1342 last_orig_insn = insn;
1344 orig_alt_group->orig_group = NULL;
1345 orig_alt_group->first_insn = orig_insn;
1346 orig_alt_group->last_insn = last_orig_insn;
1349 new_alt_group = malloc(sizeof(*new_alt_group));
1350 if (!new_alt_group) {
1351 WARN("malloc failed");
1355 if (special_alt->new_len < special_alt->orig_len) {
1357 * Insert a fake nop at the end to make the replacement
1358 * alt_group the same size as the original. This is needed to
1359 * allow propagate_alt_cfi() to do its magic. When the last
1360 * instruction affects the stack, the instruction after it (the
1361 * nop) will propagate the new state to the shared CFI array.
1363 nop = malloc(sizeof(*nop));
1365 WARN("malloc failed");
1368 memset(nop, 0, sizeof(*nop));
1369 INIT_LIST_HEAD(&nop->alts);
1370 INIT_LIST_HEAD(&nop->stack_ops);
1372 nop->sec = special_alt->new_sec;
1373 nop->offset = special_alt->new_off + special_alt->new_len;
1374 nop->len = special_alt->orig_len - special_alt->new_len;
1375 nop->type = INSN_NOP;
1376 nop->func = orig_insn->func;
1377 nop->alt_group = new_alt_group;
1378 nop->ignore = orig_insn->ignore_alts;
1381 if (!special_alt->new_len) {
1387 sec_for_each_insn_from(file, insn) {
1388 struct reloc *alt_reloc;
1390 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1393 last_new_insn = insn;
1395 insn->ignore = orig_insn->ignore_alts;
1396 insn->func = orig_insn->func;
1397 insn->alt_group = new_alt_group;
1400 * Since alternative replacement code is copy/pasted by the
1401 * kernel after applying relocations, generally such code can't
1402 * have relative-address relocation references to outside the
1403 * .altinstr_replacement section, unless the arch's
1404 * alternatives code can adjust the relative offsets
1407 alt_reloc = insn_reloc(file, insn);
1409 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1411 WARN_FUNC("unsupported relocation in alternatives section",
1412 insn->sec, insn->offset);
1416 if (!is_static_jump(insn))
1419 if (!insn->immediate)
1422 dest_off = arch_jump_destination(insn);
1423 if (dest_off == special_alt->new_off + special_alt->new_len)
1424 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1426 if (!insn->jump_dest) {
1427 WARN_FUNC("can't find alternative jump destination",
1428 insn->sec, insn->offset);
1433 if (!last_new_insn) {
1434 WARN_FUNC("can't find last new alternative instruction",
1435 special_alt->new_sec, special_alt->new_off);
1440 list_add(&nop->list, &last_new_insn->list);
1442 new_alt_group->orig_group = orig_alt_group;
1443 new_alt_group->first_insn = *new_insn;
1444 new_alt_group->last_insn = nop ? : last_new_insn;
1445 new_alt_group->cfi = orig_alt_group->cfi;
1450 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1451 * If the original instruction is a jump, make the alt entry an effective nop
1452 * by just skipping the original instruction.
1454 static int handle_jump_alt(struct objtool_file *file,
1455 struct special_alt *special_alt,
1456 struct instruction *orig_insn,
1457 struct instruction **new_insn)
1459 if (orig_insn->type == INSN_NOP)
1462 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
1463 WARN_FUNC("unsupported instruction at jump label",
1464 orig_insn->sec, orig_insn->offset);
1468 *new_insn = list_next_entry(orig_insn, list);
1473 * Read all the special sections which have alternate instructions which can be
1474 * patched in or redirected to at runtime. Each instruction having alternate
1475 * instruction(s) has them added to its insn->alts list, which will be
1476 * traversed in validate_branch().
1478 static int add_special_section_alts(struct objtool_file *file)
1480 struct list_head special_alts;
1481 struct instruction *orig_insn, *new_insn;
1482 struct special_alt *special_alt, *tmp;
1483 struct alternative *alt;
1486 ret = special_get_alts(file->elf, &special_alts);
1490 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1492 orig_insn = find_insn(file, special_alt->orig_sec,
1493 special_alt->orig_off);
1495 WARN_FUNC("special: can't find orig instruction",
1496 special_alt->orig_sec, special_alt->orig_off);
1502 if (!special_alt->group || special_alt->new_len) {
1503 new_insn = find_insn(file, special_alt->new_sec,
1504 special_alt->new_off);
1506 WARN_FUNC("special: can't find new instruction",
1507 special_alt->new_sec,
1508 special_alt->new_off);
1514 if (special_alt->group) {
1515 if (!special_alt->orig_len) {
1516 WARN_FUNC("empty alternative entry",
1517 orig_insn->sec, orig_insn->offset);
1521 ret = handle_group_alt(file, special_alt, orig_insn,
1525 } else if (special_alt->jump_or_nop) {
1526 ret = handle_jump_alt(file, special_alt, orig_insn,
1532 alt = malloc(sizeof(*alt));
1534 WARN("malloc failed");
1539 alt->insn = new_insn;
1540 alt->skip_orig = special_alt->skip_orig;
1541 orig_insn->ignore_alts |= special_alt->skip_alt;
1542 list_add_tail(&alt->list, &orig_insn->alts);
1544 list_del(&special_alt->list);
1552 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1553 struct reloc *table)
1555 struct reloc *reloc = table;
1556 struct instruction *dest_insn;
1557 struct alternative *alt;
1558 struct symbol *pfunc = insn->func->pfunc;
1559 unsigned int prev_offset = 0;
1562 * Each @reloc is a switch table relocation which points to the target
1565 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1567 /* Check for the end of the table: */
1568 if (reloc != table && reloc->jump_table_start)
1571 /* Make sure the table entries are consecutive: */
1572 if (prev_offset && reloc->offset != prev_offset + 8)
1575 /* Detect function pointers from contiguous objects: */
1576 if (reloc->sym->sec == pfunc->sec &&
1577 reloc->addend == pfunc->offset)
1580 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1584 /* Make sure the destination is in the same function: */
1585 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1588 alt = malloc(sizeof(*alt));
1590 WARN("malloc failed");
1594 alt->insn = dest_insn;
1595 list_add_tail(&alt->list, &insn->alts);
1596 prev_offset = reloc->offset;
1600 WARN_FUNC("can't find switch jump table",
1601 insn->sec, insn->offset);
1609 * find_jump_table() - Given a dynamic jump, find the switch jump table
1610 * associated with it.
1612 static struct reloc *find_jump_table(struct objtool_file *file,
1613 struct symbol *func,
1614 struct instruction *insn)
1616 struct reloc *table_reloc;
1617 struct instruction *dest_insn, *orig_insn = insn;
1620 * Backward search using the @first_jump_src links, these help avoid
1621 * much of the 'in between' code. Which avoids us getting confused by
1625 insn && insn->func && insn->func->pfunc == func;
1626 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1628 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1631 /* allow small jumps within the range */
1632 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1634 (insn->jump_dest->offset <= insn->offset ||
1635 insn->jump_dest->offset > orig_insn->offset))
1638 table_reloc = arch_find_switch_table(file, insn);
1641 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1642 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1652 * First pass: Mark the head of each jump table so that in the next pass,
1653 * we know when a given jump table ends and the next one starts.
1655 static void mark_func_jump_tables(struct objtool_file *file,
1656 struct symbol *func)
1658 struct instruction *insn, *last = NULL;
1659 struct reloc *reloc;
1661 func_for_each_insn(file, func, insn) {
1666 * Store back-pointers for unconditional forward jumps such
1667 * that find_jump_table() can back-track using those and
1668 * avoid some potentially confusing code.
1670 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1671 insn->offset > last->offset &&
1672 insn->jump_dest->offset > insn->offset &&
1673 !insn->jump_dest->first_jump_src) {
1675 insn->jump_dest->first_jump_src = insn;
1676 last = insn->jump_dest;
1679 if (insn->type != INSN_JUMP_DYNAMIC)
1682 reloc = find_jump_table(file, func, insn);
1684 reloc->jump_table_start = true;
1685 insn->jump_table = reloc;
1690 static int add_func_jump_tables(struct objtool_file *file,
1691 struct symbol *func)
1693 struct instruction *insn;
1696 func_for_each_insn(file, func, insn) {
1697 if (!insn->jump_table)
1700 ret = add_jump_table(file, insn, insn->jump_table);
1709 * For some switch statements, gcc generates a jump table in the .rodata
1710 * section which contains a list of addresses within the function to jump to.
1711 * This finds these jump tables and adds them to the insn->alts lists.
1713 static int add_jump_table_alts(struct objtool_file *file)
1715 struct section *sec;
1716 struct symbol *func;
1722 for_each_sec(file, sec) {
1723 list_for_each_entry(func, &sec->symbol_list, list) {
1724 if (func->type != STT_FUNC)
1727 mark_func_jump_tables(file, func);
1728 ret = add_func_jump_tables(file, func);
1737 static void set_func_state(struct cfi_state *state)
1739 state->cfa = initial_func_cfi.cfa;
1740 memcpy(&state->regs, &initial_func_cfi.regs,
1741 CFI_NUM_REGS * sizeof(struct cfi_reg));
1742 state->stack_size = initial_func_cfi.cfa.offset;
1745 static int read_unwind_hints(struct objtool_file *file)
1747 struct cfi_state cfi = init_cfi;
1748 struct section *sec, *relocsec;
1749 struct unwind_hint *hint;
1750 struct instruction *insn;
1751 struct reloc *reloc;
1754 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1758 relocsec = sec->reloc;
1760 WARN("missing .rela.discard.unwind_hints section");
1764 if (sec->len % sizeof(struct unwind_hint)) {
1765 WARN("struct unwind_hint size mismatch");
1771 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1772 hint = (struct unwind_hint *)sec->data->d_buf + i;
1774 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1776 WARN("can't find reloc for unwind_hints[%d]", i);
1780 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1782 WARN("can't find insn for unwind_hints[%d]", i);
1788 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1794 if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1795 insn->restore = true;
1799 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1800 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1802 if (sym && sym->bind == STB_GLOBAL) {
1807 if (hint->type == UNWIND_HINT_TYPE_ENTRY) {
1808 hint->type = UNWIND_HINT_TYPE_CALL;
1812 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1813 insn->cfi = &func_cfi;
1820 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1821 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1822 insn->sec, insn->offset, hint->sp_reg);
1826 cfi.cfa.offset = hint->sp_offset;
1827 cfi.type = hint->type;
1828 cfi.end = hint->end;
1830 insn->cfi = cfi_hash_find_or_add(&cfi);
1836 static int read_retpoline_hints(struct objtool_file *file)
1838 struct section *sec;
1839 struct instruction *insn;
1840 struct reloc *reloc;
1842 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1846 list_for_each_entry(reloc, &sec->reloc_list, list) {
1847 if (reloc->sym->type != STT_SECTION) {
1848 WARN("unexpected relocation symbol type in %s", sec->name);
1852 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1854 WARN("bad .discard.retpoline_safe entry");
1858 if (insn->type != INSN_JUMP_DYNAMIC &&
1859 insn->type != INSN_CALL_DYNAMIC &&
1860 insn->type != INSN_RETURN &&
1861 insn->type != INSN_NOP) {
1862 WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop",
1863 insn->sec, insn->offset);
1867 insn->retpoline_safe = true;
1873 static int read_instr_hints(struct objtool_file *file)
1875 struct section *sec;
1876 struct instruction *insn;
1877 struct reloc *reloc;
1879 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1883 list_for_each_entry(reloc, &sec->reloc_list, list) {
1884 if (reloc->sym->type != STT_SECTION) {
1885 WARN("unexpected relocation symbol type in %s", sec->name);
1889 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1891 WARN("bad .discard.instr_end entry");
1898 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1902 list_for_each_entry(reloc, &sec->reloc_list, list) {
1903 if (reloc->sym->type != STT_SECTION) {
1904 WARN("unexpected relocation symbol type in %s", sec->name);
1908 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1910 WARN("bad .discard.instr_begin entry");
1920 static int read_intra_function_calls(struct objtool_file *file)
1922 struct instruction *insn;
1923 struct section *sec;
1924 struct reloc *reloc;
1926 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1930 list_for_each_entry(reloc, &sec->reloc_list, list) {
1931 unsigned long dest_off;
1933 if (reloc->sym->type != STT_SECTION) {
1934 WARN("unexpected relocation symbol type in %s",
1939 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1941 WARN("bad .discard.intra_function_call entry");
1945 if (insn->type != INSN_CALL) {
1946 WARN_FUNC("intra_function_call not a direct call",
1947 insn->sec, insn->offset);
1952 * Treat intra-function CALLs as JMPs, but with a stack_op.
1953 * See add_call_destinations(), which strips stack_ops from
1956 insn->type = INSN_JUMP_UNCONDITIONAL;
1958 dest_off = insn->offset + insn->len + insn->immediate;
1959 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1960 if (!insn->jump_dest) {
1961 WARN_FUNC("can't find call dest at %s+0x%lx",
1962 insn->sec, insn->offset,
1963 insn->sec->name, dest_off);
1971 static int classify_symbols(struct objtool_file *file)
1973 struct section *sec;
1974 struct symbol *func;
1976 for_each_sec(file, sec) {
1977 list_for_each_entry(func, &sec->symbol_list, list) {
1978 if (func->bind != STB_GLOBAL)
1981 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1982 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1983 func->static_call_tramp = true;
1985 if (arch_is_retpoline(func))
1986 func->retpoline_thunk = true;
1988 if (arch_is_rethunk(func))
1989 func->return_thunk = true;
1991 if (arch_is_embedded_insn(func))
1992 func->embedded_insn = true;
1994 if (!strcmp(func->name, "__fentry__"))
1995 func->fentry = true;
1997 if (!strncmp(func->name, "__sanitizer_cov_", 16))
2005 static void mark_rodata(struct objtool_file *file)
2007 struct section *sec;
2011 * Search for the following rodata sections, each of which can
2012 * potentially contain jump tables:
2014 * - .rodata: can contain GCC switch tables
2015 * - .rodata.<func>: same, if -fdata-sections is being used
2016 * - .rodata..c_jump_table: contains C annotated jump tables
2018 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2020 for_each_sec(file, sec) {
2021 if (!strncmp(sec->name, ".rodata", 7) &&
2022 !strstr(sec->name, ".str1.")) {
2028 file->rodata = found;
2031 static int decode_sections(struct objtool_file *file)
2037 ret = decode_instructions(file);
2041 ret = add_dead_ends(file);
2046 add_uaccess_safe(file);
2048 ret = add_ignore_alternatives(file);
2053 * Must be before add_{jump_call}_destination.
2055 ret = classify_symbols(file);
2060 * Must be before add_special_section_alts() as that depends on
2061 * jump_dest being set.
2063 ret = add_jump_destinations(file);
2067 ret = add_special_section_alts(file);
2072 * Must be before add_call_destination(); it changes INSN_CALL to
2075 ret = read_intra_function_calls(file);
2079 ret = add_call_destinations(file);
2083 ret = add_jump_table_alts(file);
2087 ret = read_unwind_hints(file);
2091 ret = read_retpoline_hints(file);
2095 ret = read_instr_hints(file);
2102 static bool is_special_call(struct instruction *insn)
2104 if (insn->type == INSN_CALL) {
2105 struct symbol *dest = insn->call_dest;
2110 if (dest->fentry || dest->embedded_insn)
2117 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2119 struct cfi_state *cfi = &state->cfi;
2122 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2125 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2128 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2131 for (i = 0; i < CFI_NUM_REGS; i++) {
2132 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2133 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2140 static bool has_valid_stack_frame(struct insn_state *state)
2142 struct cfi_state *cfi = &state->cfi;
2144 if (cfi->cfa.base == CFI_BP && cfi->regs[CFI_BP].base == CFI_CFA &&
2145 cfi->regs[CFI_BP].offset == -16)
2148 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2154 static int update_cfi_state_regs(struct instruction *insn,
2155 struct cfi_state *cfi,
2156 struct stack_op *op)
2158 struct cfi_reg *cfa = &cfi->cfa;
2160 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2164 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2168 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2171 /* add immediate to sp */
2172 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2173 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2174 cfa->offset -= op->src.offset;
2179 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2181 if (arch_callee_saved_reg(reg) &&
2182 cfi->regs[reg].base == CFI_UNDEFINED) {
2183 cfi->regs[reg].base = base;
2184 cfi->regs[reg].offset = offset;
2188 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2190 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2191 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2195 * A note about DRAP stack alignment:
2197 * GCC has the concept of a DRAP register, which is used to help keep track of
2198 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2199 * register. The typical DRAP pattern is:
2201 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2202 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2203 * 41 ff 72 f8 pushq -0x8(%r10)
2205 * 48 89 e5 mov %rsp,%rbp
2212 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2215 * There are some variations in the epilogues, like:
2223 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2228 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2229 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2230 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2231 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2233 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2236 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2237 * restored beforehand:
2240 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2241 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2243 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2247 static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
2248 struct stack_op *op)
2250 struct cfi_reg *cfa = &cfi->cfa;
2251 struct cfi_reg *regs = cfi->regs;
2253 /* stack operations don't make sense with an undefined CFA */
2254 if (cfa->base == CFI_UNDEFINED) {
2256 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2262 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2263 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2264 return update_cfi_state_regs(insn, cfi, op);
2266 switch (op->dest.type) {
2269 switch (op->src.type) {
2272 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2273 cfa->base == CFI_SP &&
2274 regs[CFI_BP].base == CFI_CFA &&
2275 regs[CFI_BP].offset == -cfa->offset) {
2277 /* mov %rsp, %rbp */
2278 cfa->base = op->dest.reg;
2279 cfi->bp_scratch = false;
2282 else if (op->src.reg == CFI_SP &&
2283 op->dest.reg == CFI_BP && cfi->drap) {
2285 /* drap: mov %rsp, %rbp */
2286 regs[CFI_BP].base = CFI_BP;
2287 regs[CFI_BP].offset = -cfi->stack_size;
2288 cfi->bp_scratch = false;
2291 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2296 * This is needed for the rare case where GCC
2303 cfi->vals[op->dest.reg].base = CFI_CFA;
2304 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2307 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2308 cfa->base == CFI_BP) {
2313 * Restore the original stack pointer (Clang).
2315 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2318 else if (op->dest.reg == cfa->base) {
2320 /* mov %reg, %rsp */
2321 if (cfa->base == CFI_SP &&
2322 cfi->vals[op->src.reg].base == CFI_CFA) {
2325 * This is needed for the rare case
2326 * where GCC does something dumb like:
2328 * lea 0x8(%rsp), %rcx
2332 cfa->offset = -cfi->vals[op->src.reg].offset;
2333 cfi->stack_size = cfa->offset;
2336 cfa->base = CFI_UNDEFINED;
2344 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2347 cfi->stack_size -= op->src.offset;
2348 if (cfa->base == CFI_SP)
2349 cfa->offset -= op->src.offset;
2353 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2355 /* lea disp(%rbp), %rsp */
2356 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2360 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2362 /* drap: lea disp(%rsp), %drap */
2363 cfi->drap_reg = op->dest.reg;
2366 * lea disp(%rsp), %reg
2368 * This is needed for the rare case where GCC
2369 * does something dumb like:
2371 * lea 0x8(%rsp), %rcx
2375 cfi->vals[op->dest.reg].base = CFI_CFA;
2376 cfi->vals[op->dest.reg].offset = \
2377 -cfi->stack_size + op->src.offset;
2382 if (cfi->drap && op->dest.reg == CFI_SP &&
2383 op->src.reg == cfi->drap_reg) {
2385 /* drap: lea disp(%drap), %rsp */
2387 cfa->offset = cfi->stack_size = -op->src.offset;
2388 cfi->drap_reg = CFI_UNDEFINED;
2393 if (op->dest.reg == cfi->cfa.base) {
2394 WARN_FUNC("unsupported stack register modification",
2395 insn->sec, insn->offset);
2402 if (op->dest.reg != CFI_SP ||
2403 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2404 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2405 WARN_FUNC("unsupported stack pointer realignment",
2406 insn->sec, insn->offset);
2410 if (cfi->drap_reg != CFI_UNDEFINED) {
2411 /* drap: and imm, %rsp */
2412 cfa->base = cfi->drap_reg;
2413 cfa->offset = cfi->stack_size = 0;
2418 * Older versions of GCC (4.8ish) realign the stack
2419 * without DRAP, with a frame pointer.
2426 if (!cfi->drap && op->dest.reg == cfa->base) {
2432 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2433 op->dest.reg == cfi->drap_reg &&
2434 cfi->drap_offset == -cfi->stack_size) {
2436 /* drap: pop %drap */
2437 cfa->base = cfi->drap_reg;
2439 cfi->drap_offset = -1;
2441 } else if (regs[op->dest.reg].offset == -cfi->stack_size) {
2444 restore_reg(cfi, op->dest.reg);
2447 cfi->stack_size -= 8;
2448 if (cfa->base == CFI_SP)
2453 case OP_SRC_REG_INDIRECT:
2454 if (cfi->drap && op->src.reg == CFI_BP &&
2455 op->src.offset == cfi->drap_offset) {
2457 /* drap: mov disp(%rbp), %drap */
2458 cfa->base = cfi->drap_reg;
2460 cfi->drap_offset = -1;
2463 if (cfi->drap && op->src.reg == CFI_BP &&
2464 op->src.offset == regs[op->dest.reg].offset) {
2466 /* drap: mov disp(%rbp), %reg */
2467 restore_reg(cfi, op->dest.reg);
2469 } else if (op->src.reg == cfa->base &&
2470 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2472 /* mov disp(%rbp), %reg */
2473 /* mov disp(%rsp), %reg */
2474 restore_reg(cfi, op->dest.reg);
2480 WARN_FUNC("unknown stack-related instruction",
2481 insn->sec, insn->offset);
2489 cfi->stack_size += 8;
2490 if (cfa->base == CFI_SP)
2493 if (op->src.type != OP_SRC_REG)
2497 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2499 /* drap: push %drap */
2500 cfa->base = CFI_BP_INDIRECT;
2501 cfa->offset = -cfi->stack_size;
2503 /* save drap so we know when to restore it */
2504 cfi->drap_offset = -cfi->stack_size;
2506 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2508 /* drap: push %rbp */
2509 cfi->stack_size = 0;
2513 /* drap: push %reg */
2514 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2520 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2523 /* detect when asm code uses rbp as a scratch register */
2524 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2525 cfa->base != CFI_BP)
2526 cfi->bp_scratch = true;
2529 case OP_DEST_REG_INDIRECT:
2532 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2534 /* drap: mov %drap, disp(%rbp) */
2535 cfa->base = CFI_BP_INDIRECT;
2536 cfa->offset = op->dest.offset;
2538 /* save drap offset so we know when to restore it */
2539 cfi->drap_offset = op->dest.offset;
2542 /* drap: mov reg, disp(%rbp) */
2543 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2546 } else if (op->dest.reg == cfa->base) {
2548 /* mov reg, disp(%rbp) */
2549 /* mov reg, disp(%rsp) */
2550 save_reg(cfi, op->src.reg, CFI_CFA,
2551 op->dest.offset - cfi->cfa.offset);
2557 if ((!cfi->drap && cfa->base != CFI_BP) ||
2558 (cfi->drap && cfa->base != cfi->drap_reg)) {
2559 WARN_FUNC("leave instruction with modified stack frame",
2560 insn->sec, insn->offset);
2564 /* leave (mov %rbp, %rsp; pop %rbp) */
2566 cfi->stack_size = -cfi->regs[CFI_BP].offset - 8;
2567 restore_reg(cfi, CFI_BP);
2577 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2578 WARN_FUNC("unknown stack-related memory operation",
2579 insn->sec, insn->offset);
2584 cfi->stack_size -= 8;
2585 if (cfa->base == CFI_SP)
2591 WARN_FUNC("unknown stack-related instruction",
2592 insn->sec, insn->offset);
2600 * The stack layouts of alternatives instructions can sometimes diverge when
2601 * they have stack modifications. That's fine as long as the potential stack
2602 * layouts don't conflict at any given potential instruction boundary.
2604 * Flatten the CFIs of the different alternative code streams (both original
2605 * and replacement) into a single shared CFI array which can be used to detect
2606 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2608 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2610 struct cfi_state **alt_cfi;
2613 if (!insn->alt_group)
2617 WARN("CFI missing");
2621 alt_cfi = insn->alt_group->cfi;
2622 group_off = insn->offset - insn->alt_group->first_insn->offset;
2624 if (!alt_cfi[group_off]) {
2625 alt_cfi[group_off] = insn->cfi;
2627 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2628 WARN_FUNC("stack layout conflict in alternatives",
2629 insn->sec, insn->offset);
2637 static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
2639 struct stack_op *op;
2641 list_for_each_entry(op, &insn->stack_ops, list) {
2643 if (update_cfi_state(insn, &state->cfi, op))
2646 if (op->dest.type == OP_DEST_PUSHF) {
2647 if (!state->uaccess_stack) {
2648 state->uaccess_stack = 1;
2649 } else if (state->uaccess_stack >> 31) {
2650 WARN_FUNC("PUSHF stack exhausted",
2651 insn->sec, insn->offset);
2654 state->uaccess_stack <<= 1;
2655 state->uaccess_stack |= state->uaccess;
2658 if (op->src.type == OP_SRC_POPF) {
2659 if (state->uaccess_stack) {
2660 state->uaccess = state->uaccess_stack & 1;
2661 state->uaccess_stack >>= 1;
2662 if (state->uaccess_stack == 1)
2663 state->uaccess_stack = 0;
2671 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2673 struct cfi_state *cfi1 = insn->cfi;
2677 WARN("CFI missing");
2681 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2683 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2684 insn->sec, insn->offset,
2685 cfi1->cfa.base, cfi1->cfa.offset,
2686 cfi2->cfa.base, cfi2->cfa.offset);
2688 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2689 for (i = 0; i < CFI_NUM_REGS; i++) {
2690 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2691 sizeof(struct cfi_reg)))
2694 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2695 insn->sec, insn->offset,
2696 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2697 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2701 } else if (cfi1->type != cfi2->type) {
2703 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2704 insn->sec, insn->offset, cfi1->type, cfi2->type);
2706 } else if (cfi1->drap != cfi2->drap ||
2707 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2708 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2710 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2711 insn->sec, insn->offset,
2712 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2713 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2721 static inline bool func_uaccess_safe(struct symbol *func)
2724 return func->uaccess_safe;
2729 static inline const char *call_dest_name(struct instruction *insn)
2731 if (insn->call_dest)
2732 return insn->call_dest->name;
2737 static inline bool noinstr_call_dest(struct symbol *func)
2740 * We can't deal with indirect function calls at present;
2741 * assume they're instrumented.
2747 * If the symbol is from a noinstr section; we good.
2749 if (func->sec->noinstr)
2753 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2754 * something 'BAD' happened. At the risk of taking the machine down,
2755 * let them proceed to get the message out.
2757 if (!strncmp(func->name, "__ubsan_handle_", 15))
2763 static int validate_call(struct instruction *insn, struct insn_state *state)
2765 if (state->noinstr && state->instr <= 0 &&
2766 !noinstr_call_dest(insn->call_dest)) {
2767 WARN_FUNC("call to %s() leaves .noinstr.text section",
2768 insn->sec, insn->offset, call_dest_name(insn));
2772 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2773 WARN_FUNC("call to %s() with UACCESS enabled",
2774 insn->sec, insn->offset, call_dest_name(insn));
2779 WARN_FUNC("call to %s() with DF set",
2780 insn->sec, insn->offset, call_dest_name(insn));
2787 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2789 if (has_modified_stack_frame(insn, state)) {
2790 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2791 insn->sec, insn->offset);
2795 return validate_call(insn, state);
2798 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2800 if (state->noinstr && state->instr > 0) {
2801 WARN_FUNC("return with instrumentation enabled",
2802 insn->sec, insn->offset);
2806 if (state->uaccess && !func_uaccess_safe(func)) {
2807 WARN_FUNC("return with UACCESS enabled",
2808 insn->sec, insn->offset);
2812 if (!state->uaccess && func_uaccess_safe(func)) {
2813 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2814 insn->sec, insn->offset);
2819 WARN_FUNC("return with DF set",
2820 insn->sec, insn->offset);
2824 if (func && has_modified_stack_frame(insn, state)) {
2825 WARN_FUNC("return with modified stack frame",
2826 insn->sec, insn->offset);
2830 if (state->cfi.bp_scratch) {
2831 WARN_FUNC("BP used as a scratch register",
2832 insn->sec, insn->offset);
2839 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2840 struct instruction *insn)
2842 struct alt_group *alt_group = insn->alt_group;
2845 * Simulate the fact that alternatives are patched in-place. When the
2846 * end of a replacement alt_group is reached, redirect objtool flow to
2847 * the end of the original alt_group.
2849 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2850 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2852 return next_insn_same_sec(file, insn);
2856 * Follow the branch starting at the given instruction, and recursively follow
2857 * any other branches (jumps). Meanwhile, track the frame pointer state at
2858 * each instruction and validate all the rules described in
2859 * tools/objtool/Documentation/stack-validation.txt.
2861 static int validate_branch(struct objtool_file *file, struct symbol *func,
2862 struct instruction *insn, struct insn_state state)
2864 struct alternative *alt;
2865 struct instruction *next_insn, *prev_insn = NULL;
2866 struct section *sec;
2873 next_insn = next_insn_to_validate(file, insn);
2875 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2876 WARN("%s() falls through to next function %s()",
2877 func->name, insn->func->name);
2881 if (func && insn->ignore) {
2882 WARN_FUNC("BUG: why am I validating an ignored function?",
2887 visited = VISITED_BRANCH << state.uaccess;
2888 if (insn->visited & VISITED_BRANCH_MASK) {
2889 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2892 if (insn->visited & visited)
2899 state.instr += insn->instr;
2902 if (insn->restore) {
2903 struct instruction *save_insn, *i;
2908 sym_for_each_insn_continue_reverse(file, func, i) {
2916 WARN_FUNC("no corresponding CFI save for CFI restore",
2921 if (!save_insn->visited) {
2922 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2927 insn->cfi = save_insn->cfi;
2931 state.cfi = *insn->cfi;
2933 /* XXX track if we actually changed state.cfi */
2935 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
2936 insn->cfi = prev_insn->cfi;
2939 insn->cfi = cfi_hash_find_or_add(&state.cfi);
2943 insn->visited |= visited;
2945 if (propagate_alt_cfi(file, insn))
2948 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2949 bool skip_orig = false;
2951 list_for_each_entry(alt, &insn->alts, list) {
2955 ret = validate_branch(file, func, alt->insn, state);
2958 BT_FUNC("(alt)", insn);
2967 if (handle_insn_ops(insn, &state))
2970 switch (insn->type) {
2973 if (sls && !insn->retpoline_safe &&
2974 next_insn && next_insn->type != INSN_TRAP) {
2975 WARN_FUNC("missing int3 after ret",
2976 insn->sec, insn->offset);
2978 return validate_return(func, insn, &state);
2981 case INSN_CALL_DYNAMIC:
2982 ret = validate_call(insn, &state);
2986 if (!no_fp && func && !is_special_call(insn) &&
2987 !has_valid_stack_frame(&state)) {
2988 WARN_FUNC("call without frame pointer save/setup",
2993 if (dead_end_function(file, insn->call_dest))
2998 case INSN_JUMP_CONDITIONAL:
2999 case INSN_JUMP_UNCONDITIONAL:
3000 if (is_sibling_call(insn)) {
3001 ret = validate_sibling_call(insn, &state);
3005 } else if (insn->jump_dest) {
3006 ret = validate_branch(file, func,
3007 insn->jump_dest, state);
3010 BT_FUNC("(branch)", insn);
3015 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3020 case INSN_JUMP_DYNAMIC:
3021 if (sls && !insn->retpoline_safe &&
3022 next_insn && next_insn->type != INSN_TRAP) {
3023 WARN_FUNC("missing int3 after indirect jump",
3024 insn->sec, insn->offset);
3028 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3029 if (is_sibling_call(insn)) {
3030 ret = validate_sibling_call(insn, &state);
3035 if (insn->type == INSN_JUMP_DYNAMIC)
3040 case INSN_CONTEXT_SWITCH:
3041 if (func && (!next_insn || !next_insn->hint)) {
3042 WARN_FUNC("unsupported instruction in callable function",
3049 if (state.uaccess) {
3050 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3054 state.uaccess = true;
3058 if (!state.uaccess && func) {
3059 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3063 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3064 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3068 state.uaccess = false;
3073 WARN_FUNC("recursive STD", sec, insn->offset);
3081 if (!state.df && func) {
3082 WARN_FUNC("redundant CLD", sec, insn->offset);
3097 if (state.cfi.cfa.base == CFI_UNDEFINED)
3099 WARN("%s: unexpected end of section", sec->name);
3110 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3112 struct instruction *insn;
3113 struct insn_state state;
3114 int ret, warnings = 0;
3119 init_insn_state(&state, sec);
3122 insn = find_insn(file, sec, 0);
3126 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3129 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3130 if (insn->hint && !insn->visited) {
3131 ret = validate_branch(file, insn->func, insn, state);
3132 if (ret && backtrace)
3133 BT_FUNC("<=== (hint)", insn);
3137 insn = list_next_entry(insn, list);
3144 * Validate rethunk entry constraint: must untrain RET before the first RET.
3146 * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes
3147 * before an actual RET instruction.
3149 static int validate_entry(struct objtool_file *file, struct instruction *insn)
3151 struct instruction *next, *dest;
3152 int ret, warnings = 0;
3155 next = next_insn_to_validate(file, insn);
3157 if (insn->visited & VISITED_ENTRY)
3160 insn->visited |= VISITED_ENTRY;
3162 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3163 struct alternative *alt;
3164 bool skip_orig = false;
3166 list_for_each_entry(alt, &insn->alts, list) {
3170 ret = validate_entry(file, alt->insn);
3173 BT_FUNC("(alt)", insn);
3182 switch (insn->type) {
3184 case INSN_CALL_DYNAMIC:
3185 case INSN_JUMP_DYNAMIC:
3186 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3187 WARN_FUNC("early indirect call", insn->sec, insn->offset);
3190 case INSN_JUMP_UNCONDITIONAL:
3191 case INSN_JUMP_CONDITIONAL:
3192 if (!is_sibling_call(insn)) {
3193 if (!insn->jump_dest) {
3194 WARN_FUNC("unresolved jump target after linking?!?",
3195 insn->sec, insn->offset);
3198 ret = validate_entry(file, insn->jump_dest);
3201 BT_FUNC("(branch%s)", insn,
3202 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
3207 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3215 dest = find_insn(file, insn->call_dest->sec,
3216 insn->call_dest->offset);
3218 WARN("Unresolved function after linking!?: %s",
3219 insn->call_dest->name);
3223 ret = validate_entry(file, dest);
3226 BT_FUNC("(call)", insn);
3230 * If a call returns without error, it must have seen UNTRAIN_RET.
3231 * Therefore any non-error return is a success.
3236 WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset);
3240 if (insn->retpoline_safe)
3249 WARN_FUNC("teh end!", insn->sec, insn->offset);
3259 * Validate that all branches starting at 'insn->entry' encounter UNRET_END
3262 static int validate_unret(struct objtool_file *file)
3264 struct instruction *insn;
3265 int ret, warnings = 0;
3267 for_each_insn(file, insn) {
3271 ret = validate_entry(file, insn);
3273 WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset);
3282 static int validate_retpoline(struct objtool_file *file)
3284 struct instruction *insn;
3287 for_each_insn(file, insn) {
3288 if (insn->type != INSN_JUMP_DYNAMIC &&
3289 insn->type != INSN_CALL_DYNAMIC &&
3290 insn->type != INSN_RETURN)
3293 if (insn->retpoline_safe)
3297 * .init.text code is ran before userspace and thus doesn't
3298 * strictly need retpolines, except for modules which are
3299 * loaded late, they very much do need retpoline in their
3302 if (!strcmp(insn->sec->name, ".init.text") && !module)
3305 if (insn->type == INSN_RETURN) {
3307 WARN_FUNC("'naked' return found in RETHUNK build",
3308 insn->sec, insn->offset);
3312 WARN_FUNC("indirect %s found in RETPOLINE build",
3313 insn->sec, insn->offset,
3314 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3323 static bool is_kasan_insn(struct instruction *insn)
3325 return (insn->type == INSN_CALL &&
3326 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3329 static bool is_ubsan_insn(struct instruction *insn)
3331 return (insn->type == INSN_CALL &&
3332 !strcmp(insn->call_dest->name,
3333 "__ubsan_handle_builtin_unreachable"));
3336 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3339 struct instruction *prev_insn;
3341 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3345 * Ignore any unused exceptions. This can happen when a whitelisted
3346 * function has an exception table entry.
3348 * Also ignore alternative replacement instructions. This can happen
3349 * when a whitelisted function uses one of the ALTERNATIVE macros.
3351 if (!strcmp(insn->sec->name, ".fixup") ||
3352 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3353 !strcmp(insn->sec->name, ".altinstr_aux"))
3360 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3361 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3362 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3363 * (or occasionally a JMP to UD2).
3365 * It may also insert a UD2 after calling a __noreturn function.
3367 prev_insn = list_prev_entry(insn, list);
3368 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3369 (insn->type == INSN_BUG ||
3370 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3371 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3375 * Check if this (or a subsequent) instruction is related to
3376 * CONFIG_UBSAN or CONFIG_KASAN.
3378 * End the search at 5 instructions to avoid going into the weeds.
3380 for (i = 0; i < 5; i++) {
3382 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3385 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3386 if (insn->jump_dest &&
3387 insn->jump_dest->func == insn->func) {
3388 insn = insn->jump_dest;
3395 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3398 insn = list_next_entry(insn, list);
3404 static int validate_symbol(struct objtool_file *file, struct section *sec,
3405 struct symbol *sym, struct insn_state *state)
3407 struct instruction *insn;
3411 WARN("%s() is missing an ELF size annotation", sym->name);
3415 if (sym->pfunc != sym || sym->alias != sym)
3418 insn = find_insn(file, sec, sym->offset);
3419 if (!insn || insn->ignore || insn->visited)
3422 state->uaccess = sym->uaccess_safe;
3424 ret = validate_branch(file, insn->func, insn, *state);
3425 if (ret && backtrace)
3426 BT_FUNC("<=== (sym)", insn);
3430 static int validate_section(struct objtool_file *file, struct section *sec)
3432 struct insn_state state;
3433 struct symbol *func;
3436 list_for_each_entry(func, &sec->symbol_list, list) {
3437 if (func->type != STT_FUNC)
3440 init_insn_state(&state, sec);
3441 set_func_state(&state.cfi);
3443 warnings += validate_symbol(file, sec, func, &state);
3449 static int validate_vmlinux_functions(struct objtool_file *file)
3451 struct section *sec;
3454 sec = find_section_by_name(file->elf, ".noinstr.text");
3456 warnings += validate_section(file, sec);
3457 warnings += validate_unwind_hints(file, sec);
3460 sec = find_section_by_name(file->elf, ".entry.text");
3462 warnings += validate_section(file, sec);
3463 warnings += validate_unwind_hints(file, sec);
3469 static int validate_functions(struct objtool_file *file)
3471 struct section *sec;
3474 for_each_sec(file, sec) {
3475 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3478 warnings += validate_section(file, sec);
3484 static int validate_reachable_instructions(struct objtool_file *file)
3486 struct instruction *insn;
3488 if (file->ignore_unreachables)
3491 for_each_insn(file, insn) {
3492 if (insn->visited || ignore_unreachable_insn(file, insn))
3495 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3502 int check(struct objtool_file *file)
3504 int ret, warnings = 0;
3506 arch_initial_func_cfi_state(&initial_func_cfi);
3507 init_cfi_state(&init_cfi);
3508 init_cfi_state(&func_cfi);
3509 set_func_state(&func_cfi);
3511 if (!cfi_hash_alloc())
3514 cfi_hash_add(&init_cfi);
3515 cfi_hash_add(&func_cfi);
3517 ret = decode_sections(file);
3523 if (list_empty(&file->insn_list))
3526 if (vmlinux && !validate_dup) {
3527 ret = validate_vmlinux_functions(file);
3536 ret = validate_retpoline(file);
3542 ret = validate_functions(file);
3547 ret = validate_unwind_hints(file, NULL);
3554 * Must be after validate_branch() and friends, it plays
3555 * further games with insn->visited.
3557 ret = validate_unret(file);
3564 ret = validate_reachable_instructions(file);
3570 ret = create_static_call_sections(file);
3576 ret = create_retpoline_sites_sections(file);
3583 ret = create_return_sites_sections(file);
3590 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3591 printf("nr_cfi: %ld\n", nr_cfi);
3592 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3593 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3598 * For now, don't fail the kernel build on fatal warnings. These
3599 * errors are still fairly common due to the growing matrix of
3600 * supported toolchains and their recent pace of change.