1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
11 #include <objtool/builtin.h>
12 #include <objtool/cfi.h>
13 #include <objtool/arch.h>
14 #include <objtool/check.h>
15 #include <objtool/special.h>
16 #include <objtool/warn.h>
17 #include <objtool/endianness.h>
19 #include <linux/objtool.h>
20 #include <linux/hashtable.h>
21 #include <linux/kernel.h>
22 #include <linux/static_call_types.h>
25 struct list_head list;
26 struct instruction *insn;
30 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
32 static struct cfi_init_state initial_func_cfi;
33 static struct cfi_state init_cfi;
34 static struct cfi_state func_cfi;
36 struct instruction *find_insn(struct objtool_file *file,
37 struct section *sec, unsigned long offset)
39 struct instruction *insn;
41 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
42 if (insn->sec == sec && insn->offset == offset)
49 static struct instruction *next_insn_same_sec(struct objtool_file *file,
50 struct instruction *insn)
52 struct instruction *next = list_next_entry(insn, list);
54 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
60 static struct instruction *next_insn_same_func(struct objtool_file *file,
61 struct instruction *insn)
63 struct instruction *next = list_next_entry(insn, list);
64 struct symbol *func = insn->func;
69 if (&next->list != &file->insn_list && next->func == func)
72 /* Check if we're already in the subfunction: */
73 if (func == func->cfunc)
76 /* Move to the subfunction: */
77 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
80 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
81 struct instruction *insn)
83 struct instruction *prev = list_prev_entry(insn, list);
85 if (&prev->list != &file->insn_list && prev->func == insn->func)
91 #define func_for_each_insn(file, func, insn) \
92 for (insn = find_insn(file, func->sec, func->offset); \
94 insn = next_insn_same_func(file, insn))
96 #define sym_for_each_insn(file, sym, insn) \
97 for (insn = find_insn(file, sym->sec, sym->offset); \
98 insn && &insn->list != &file->insn_list && \
99 insn->sec == sym->sec && \
100 insn->offset < sym->offset + sym->len; \
101 insn = list_next_entry(insn, list))
103 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
104 for (insn = list_prev_entry(insn, list); \
105 &insn->list != &file->insn_list && \
106 insn->sec == sym->sec && insn->offset >= sym->offset; \
107 insn = list_prev_entry(insn, list))
109 #define sec_for_each_insn_from(file, insn) \
110 for (; insn; insn = next_insn_same_sec(file, insn))
112 #define sec_for_each_insn_continue(file, insn) \
113 for (insn = next_insn_same_sec(file, insn); insn; \
114 insn = next_insn_same_sec(file, insn))
116 static bool is_jump_table_jump(struct instruction *insn)
118 struct alt_group *alt_group = insn->alt_group;
120 if (insn->jump_table)
123 /* Retpoline alternative for a jump table? */
124 return alt_group && alt_group->orig_group &&
125 alt_group->orig_group->first_insn->jump_table;
128 static bool is_sibling_call(struct instruction *insn)
131 * Assume only ELF functions can make sibling calls. This ensures
132 * sibling call detection consistency between vmlinux.o and individual
138 /* An indirect jump is either a sibling call or a jump to a table. */
139 if (insn->type == INSN_JUMP_DYNAMIC)
140 return !is_jump_table_jump(insn);
142 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
143 return (is_static_jump(insn) && insn->call_dest);
147 * This checks to see if the given function is a "noreturn" function.
149 * For global functions which are outside the scope of this object file, we
150 * have to keep a manual list of them.
152 * For local functions, we have to detect them manually by simply looking for
153 * the lack of a return instruction.
155 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
159 struct instruction *insn;
163 * Unfortunately these have to be hard coded because the noreturn
164 * attribute isn't provided in ELF data.
166 static const char * const global_noreturns[] = {
173 "__module_put_and_kthread_exit",
174 "kthread_complete_and_exit",
179 "machine_real_restart",
180 "rewind_stack_and_make_dead",
181 "kunit_try_catch_throw",
183 "cpu_bringup_and_idle",
193 if (func->bind == STB_WEAK)
196 if (func->bind == STB_GLOBAL)
197 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
198 if (!strcmp(func->name, global_noreturns[i]))
204 insn = find_insn(file, func->sec, func->offset);
208 func_for_each_insn(file, func, insn) {
211 if (insn->type == INSN_RETURN)
219 * A function can have a sibling call instead of a return. In that
220 * case, the function's dead-end status depends on whether the target
221 * of the sibling call returns.
223 func_for_each_insn(file, func, insn) {
224 if (is_sibling_call(insn)) {
225 struct instruction *dest = insn->jump_dest;
228 /* sibling call to another file */
231 /* local sibling call */
232 if (recursion == 5) {
234 * Infinite recursion: two functions have
235 * sibling calls to each other. This is a very
236 * rare case. It means they aren't dead ends.
241 return __dead_end_function(file, dest->func, recursion+1);
248 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
250 return __dead_end_function(file, func, 0);
253 static void init_cfi_state(struct cfi_state *cfi)
257 for (i = 0; i < CFI_NUM_REGS; i++) {
258 cfi->regs[i].base = CFI_UNDEFINED;
259 cfi->vals[i].base = CFI_UNDEFINED;
261 cfi->cfa.base = CFI_UNDEFINED;
262 cfi->drap_reg = CFI_UNDEFINED;
263 cfi->drap_offset = -1;
266 static void init_insn_state(struct insn_state *state, struct section *sec)
268 memset(state, 0, sizeof(*state));
269 init_cfi_state(&state->cfi);
272 * We need the full vmlinux for noinstr validation, otherwise we can
273 * not correctly determine insn->call_dest->sec (external symbols do
274 * not have a section).
276 if (opts.vmlinux && opts.noinstr && sec)
277 state->noinstr = sec->noinstr;
280 static struct cfi_state *cfi_alloc(void)
282 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
284 WARN("calloc failed");
292 static struct hlist_head *cfi_hash;
294 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
296 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
297 (void *)cfi2 + sizeof(cfi2->hash),
298 sizeof(struct cfi_state) - sizeof(struct hlist_node));
301 static inline u32 cfi_key(struct cfi_state *cfi)
303 return jhash((void *)cfi + sizeof(cfi->hash),
304 sizeof(*cfi) - sizeof(cfi->hash), 0);
307 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
309 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
310 struct cfi_state *obj;
312 hlist_for_each_entry(obj, head, hash) {
313 if (!cficmp(cfi, obj)) {
321 hlist_add_head(&obj->hash, head);
326 static void cfi_hash_add(struct cfi_state *cfi)
328 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
330 hlist_add_head(&cfi->hash, head);
333 static void *cfi_hash_alloc(unsigned long size)
335 cfi_bits = max(10, ilog2(size));
336 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
337 PROT_READ|PROT_WRITE,
338 MAP_PRIVATE|MAP_ANON, -1, 0);
339 if (cfi_hash == (void *)-1L) {
340 WARN("mmap fail cfi_hash");
342 } else if (opts.stats) {
343 printf("cfi_bits: %d\n", cfi_bits);
349 static unsigned long nr_insns;
350 static unsigned long nr_insns_visited;
353 * Call the arch-specific instruction decoder for all the instructions and add
354 * them to the global instruction list.
356 static int decode_instructions(struct objtool_file *file)
360 unsigned long offset;
361 struct instruction *insn;
364 for_each_sec(file, sec) {
366 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
369 if (strcmp(sec->name, ".altinstr_replacement") &&
370 strcmp(sec->name, ".altinstr_aux") &&
371 strncmp(sec->name, ".discard.", 9))
374 if (!strcmp(sec->name, ".noinstr.text") ||
375 !strcmp(sec->name, ".entry.text"))
378 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
379 insn = malloc(sizeof(*insn));
381 WARN("malloc failed");
384 memset(insn, 0, sizeof(*insn));
385 INIT_LIST_HEAD(&insn->alts);
386 INIT_LIST_HEAD(&insn->stack_ops);
387 INIT_LIST_HEAD(&insn->call_node);
390 insn->offset = offset;
392 ret = arch_decode_instruction(file, sec, offset,
393 sec->sh.sh_size - offset,
394 &insn->len, &insn->type,
401 * By default, "ud2" is a dead end unless otherwise
402 * annotated, because GCC 7 inserts it for certain
403 * divide-by-zero cases.
405 if (insn->type == INSN_BUG)
406 insn->dead_end = true;
408 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
409 list_add_tail(&insn->list, &file->insn_list);
413 list_for_each_entry(func, &sec->symbol_list, list) {
414 if (func->type != STT_FUNC || func->alias != func)
417 if (!find_insn(file, sec, func->offset)) {
418 WARN("%s(): can't find starting instruction",
423 sym_for_each_insn(file, func, insn) {
425 if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) {
426 if (insn->offset == insn->func->offset) {
427 list_add_tail(&insn->call_node, &file->endbr_list);
430 file->nr_endbr_int++;
438 printf("nr_insns: %lu\n", nr_insns);
448 * Read the pv_ops[] .data table to find the static initialized values.
450 static int add_pv_ops(struct objtool_file *file, const char *symname)
452 struct symbol *sym, *func;
453 unsigned long off, end;
457 sym = find_symbol_by_name(file->elf, symname);
462 end = off + sym->len;
464 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
469 if (func->type == STT_SECTION)
470 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
472 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
474 objtool_pv_add(file, idx, func);
476 off = rel->offset + 1;
485 * Allocate and initialize file->pv_ops[].
487 static int init_pv_ops(struct objtool_file *file)
489 static const char *pv_ops_tables[] = {
505 sym = find_symbol_by_name(file->elf, "pv_ops");
509 nr = sym->len / sizeof(unsigned long);
510 file->pv_ops = calloc(sizeof(struct pv_state), nr);
514 for (idx = 0; idx < nr; idx++)
515 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
517 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
518 add_pv_ops(file, pv_ops);
523 static struct instruction *find_last_insn(struct objtool_file *file,
526 struct instruction *insn = NULL;
528 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
530 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
531 insn = find_insn(file, sec, offset);
537 * Mark "ud2" instructions and manually annotated dead ends.
539 static int add_dead_ends(struct objtool_file *file)
543 struct instruction *insn;
546 * Check for manually annotated dead ends.
548 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
552 list_for_each_entry(reloc, &sec->reloc_list, list) {
553 if (reloc->sym->type != STT_SECTION) {
554 WARN("unexpected relocation symbol type in %s", sec->name);
557 insn = find_insn(file, reloc->sym->sec, reloc->addend);
559 insn = list_prev_entry(insn, list);
560 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
561 insn = find_last_insn(file, reloc->sym->sec);
563 WARN("can't find unreachable insn at %s+0x%lx",
564 reloc->sym->sec->name, reloc->addend);
568 WARN("can't find unreachable insn at %s+0x%lx",
569 reloc->sym->sec->name, reloc->addend);
573 insn->dead_end = true;
578 * These manually annotated reachable checks are needed for GCC 4.4,
579 * where the Linux unreachable() macro isn't supported. In that case
580 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
583 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
587 list_for_each_entry(reloc, &sec->reloc_list, list) {
588 if (reloc->sym->type != STT_SECTION) {
589 WARN("unexpected relocation symbol type in %s", sec->name);
592 insn = find_insn(file, reloc->sym->sec, reloc->addend);
594 insn = list_prev_entry(insn, list);
595 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
596 insn = find_last_insn(file, reloc->sym->sec);
598 WARN("can't find reachable insn at %s+0x%lx",
599 reloc->sym->sec->name, reloc->addend);
603 WARN("can't find reachable insn at %s+0x%lx",
604 reloc->sym->sec->name, reloc->addend);
608 insn->dead_end = false;
614 static int create_static_call_sections(struct objtool_file *file)
617 struct static_call_site *site;
618 struct instruction *insn;
619 struct symbol *key_sym;
620 char *key_name, *tmp;
623 sec = find_section_by_name(file->elf, ".static_call_sites");
625 INIT_LIST_HEAD(&file->static_call_list);
626 WARN("file already has .static_call_sites section, skipping");
630 if (list_empty(&file->static_call_list))
634 list_for_each_entry(insn, &file->static_call_list, call_node)
637 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
638 sizeof(struct static_call_site), idx);
643 list_for_each_entry(insn, &file->static_call_list, call_node) {
645 site = (struct static_call_site *)sec->data->d_buf + idx;
646 memset(site, 0, sizeof(struct static_call_site));
648 /* populate reloc for 'addr' */
649 if (elf_add_reloc_to_insn(file->elf, sec,
650 idx * sizeof(struct static_call_site),
652 insn->sec, insn->offset))
655 /* find key symbol */
656 key_name = strdup(insn->call_dest->name);
661 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
662 STATIC_CALL_TRAMP_PREFIX_LEN)) {
663 WARN("static_call: trampoline name malformed: %s", key_name);
666 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
667 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
669 key_sym = find_symbol_by_name(file->elf, tmp);
672 WARN("static_call: can't find static_call_key symbol: %s", tmp);
677 * For modules(), the key might not be exported, which
678 * means the module can make static calls but isn't
679 * allowed to change them.
681 * In that case we temporarily set the key to be the
682 * trampoline address. This is fixed up in
683 * static_call_add_module().
685 key_sym = insn->call_dest;
689 /* populate reloc for 'key' */
690 if (elf_add_reloc(file->elf, sec,
691 idx * sizeof(struct static_call_site) + 4,
692 R_X86_64_PC32, key_sym,
693 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
702 static int create_retpoline_sites_sections(struct objtool_file *file)
704 struct instruction *insn;
708 sec = find_section_by_name(file->elf, ".retpoline_sites");
710 WARN("file already has .retpoline_sites, skipping");
715 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
721 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
724 WARN("elf_create_section: .retpoline_sites");
729 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
731 int *site = (int *)sec->data->d_buf + idx;
734 if (elf_add_reloc_to_insn(file->elf, sec,
737 insn->sec, insn->offset)) {
738 WARN("elf_add_reloc_to_insn: .retpoline_sites");
748 static int create_ibt_endbr_seal_sections(struct objtool_file *file)
750 struct instruction *insn;
754 sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
756 WARN("file already has .ibt_endbr_seal, skipping");
761 list_for_each_entry(insn, &file->endbr_list, call_node)
765 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
766 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
767 printf("ibt: superfluous ENDBR: %d\n", idx);
773 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0,
776 WARN("elf_create_section: .ibt_endbr_seal");
781 list_for_each_entry(insn, &file->endbr_list, call_node) {
783 int *site = (int *)sec->data->d_buf + idx;
786 if (elf_add_reloc_to_insn(file->elf, sec,
789 insn->sec, insn->offset)) {
790 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal");
800 static int create_mcount_loc_sections(struct objtool_file *file)
804 struct instruction *insn;
807 sec = find_section_by_name(file->elf, "__mcount_loc");
809 INIT_LIST_HEAD(&file->mcount_loc_list);
810 WARN("file already has __mcount_loc section, skipping");
814 if (list_empty(&file->mcount_loc_list))
818 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
821 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
826 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
828 loc = (unsigned long *)sec->data->d_buf + idx;
829 memset(loc, 0, sizeof(unsigned long));
831 if (elf_add_reloc_to_insn(file->elf, sec,
832 idx * sizeof(unsigned long),
834 insn->sec, insn->offset))
844 * Warnings shouldn't be reported for ignored functions.
846 static void add_ignores(struct objtool_file *file)
848 struct instruction *insn;
853 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
857 list_for_each_entry(reloc, &sec->reloc_list, list) {
858 switch (reloc->sym->type) {
864 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
870 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
874 func_for_each_insn(file, func, insn)
880 * This is a whitelist of functions that is allowed to be called with AC set.
881 * The list is meant to be minimal and only contains compiler instrumentation
882 * ABI and a few functions used to implement *_{to,from}_user() functions.
884 * These functions must not directly change AC, but may PUSHF/POPF.
886 static const char *uaccess_safe_builtin[] = {
890 /* KASAN out-of-line */
891 "__asan_loadN_noabort",
892 "__asan_load1_noabort",
893 "__asan_load2_noabort",
894 "__asan_load4_noabort",
895 "__asan_load8_noabort",
896 "__asan_load16_noabort",
897 "__asan_storeN_noabort",
898 "__asan_store1_noabort",
899 "__asan_store2_noabort",
900 "__asan_store4_noabort",
901 "__asan_store8_noabort",
902 "__asan_store16_noabort",
903 "__kasan_check_read",
904 "__kasan_check_write",
906 "__asan_report_load_n_noabort",
907 "__asan_report_load1_noabort",
908 "__asan_report_load2_noabort",
909 "__asan_report_load4_noabort",
910 "__asan_report_load8_noabort",
911 "__asan_report_load16_noabort",
912 "__asan_report_store_n_noabort",
913 "__asan_report_store1_noabort",
914 "__asan_report_store2_noabort",
915 "__asan_report_store4_noabort",
916 "__asan_report_store8_noabort",
917 "__asan_report_store16_noabort",
919 "__kcsan_check_access",
924 "kcsan_found_watchpoint",
925 "kcsan_setup_watchpoint",
926 "kcsan_check_scoped_accesses",
927 "kcsan_disable_current",
928 "kcsan_enable_current_nowarn",
933 "__tsan_write_range",
944 "__tsan_read_write1",
945 "__tsan_read_write2",
946 "__tsan_read_write4",
947 "__tsan_read_write8",
948 "__tsan_read_write16",
949 "__tsan_atomic8_load",
950 "__tsan_atomic16_load",
951 "__tsan_atomic32_load",
952 "__tsan_atomic64_load",
953 "__tsan_atomic8_store",
954 "__tsan_atomic16_store",
955 "__tsan_atomic32_store",
956 "__tsan_atomic64_store",
957 "__tsan_atomic8_exchange",
958 "__tsan_atomic16_exchange",
959 "__tsan_atomic32_exchange",
960 "__tsan_atomic64_exchange",
961 "__tsan_atomic8_fetch_add",
962 "__tsan_atomic16_fetch_add",
963 "__tsan_atomic32_fetch_add",
964 "__tsan_atomic64_fetch_add",
965 "__tsan_atomic8_fetch_sub",
966 "__tsan_atomic16_fetch_sub",
967 "__tsan_atomic32_fetch_sub",
968 "__tsan_atomic64_fetch_sub",
969 "__tsan_atomic8_fetch_and",
970 "__tsan_atomic16_fetch_and",
971 "__tsan_atomic32_fetch_and",
972 "__tsan_atomic64_fetch_and",
973 "__tsan_atomic8_fetch_or",
974 "__tsan_atomic16_fetch_or",
975 "__tsan_atomic32_fetch_or",
976 "__tsan_atomic64_fetch_or",
977 "__tsan_atomic8_fetch_xor",
978 "__tsan_atomic16_fetch_xor",
979 "__tsan_atomic32_fetch_xor",
980 "__tsan_atomic64_fetch_xor",
981 "__tsan_atomic8_fetch_nand",
982 "__tsan_atomic16_fetch_nand",
983 "__tsan_atomic32_fetch_nand",
984 "__tsan_atomic64_fetch_nand",
985 "__tsan_atomic8_compare_exchange_strong",
986 "__tsan_atomic16_compare_exchange_strong",
987 "__tsan_atomic32_compare_exchange_strong",
988 "__tsan_atomic64_compare_exchange_strong",
989 "__tsan_atomic8_compare_exchange_weak",
990 "__tsan_atomic16_compare_exchange_weak",
991 "__tsan_atomic32_compare_exchange_weak",
992 "__tsan_atomic64_compare_exchange_weak",
993 "__tsan_atomic8_compare_exchange_val",
994 "__tsan_atomic16_compare_exchange_val",
995 "__tsan_atomic32_compare_exchange_val",
996 "__tsan_atomic64_compare_exchange_val",
997 "__tsan_atomic_thread_fence",
998 "__tsan_atomic_signal_fence",
1002 "__sanitizer_cov_trace_pc",
1003 "__sanitizer_cov_trace_const_cmp1",
1004 "__sanitizer_cov_trace_const_cmp2",
1005 "__sanitizer_cov_trace_const_cmp4",
1006 "__sanitizer_cov_trace_const_cmp8",
1007 "__sanitizer_cov_trace_cmp1",
1008 "__sanitizer_cov_trace_cmp2",
1009 "__sanitizer_cov_trace_cmp4",
1010 "__sanitizer_cov_trace_cmp8",
1011 "__sanitizer_cov_trace_switch",
1013 "ubsan_type_mismatch_common",
1014 "__ubsan_handle_type_mismatch",
1015 "__ubsan_handle_type_mismatch_v1",
1016 "__ubsan_handle_shift_out_of_bounds",
1018 "csum_partial_copy_generic",
1020 "copy_mc_fragile_handle_tail",
1021 "copy_mc_enhanced_fast_string",
1022 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
1026 static void add_uaccess_safe(struct objtool_file *file)
1028 struct symbol *func;
1034 for (name = uaccess_safe_builtin; *name; name++) {
1035 func = find_symbol_by_name(file->elf, *name);
1039 func->uaccess_safe = true;
1044 * FIXME: For now, just ignore any alternatives which add retpolines. This is
1045 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1046 * But it at least allows objtool to understand the control flow *around* the
1049 static int add_ignore_alternatives(struct objtool_file *file)
1051 struct section *sec;
1052 struct reloc *reloc;
1053 struct instruction *insn;
1055 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
1059 list_for_each_entry(reloc, &sec->reloc_list, list) {
1060 if (reloc->sym->type != STT_SECTION) {
1061 WARN("unexpected relocation symbol type in %s", sec->name);
1065 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1067 WARN("bad .discard.ignore_alts entry");
1071 insn->ignore_alts = true;
1077 __weak bool arch_is_retpoline(struct symbol *sym)
1082 #define NEGATIVE_RELOC ((void *)-1L)
1084 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1086 if (insn->reloc == NEGATIVE_RELOC)
1093 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1094 insn->offset, insn->len);
1096 insn->reloc = NEGATIVE_RELOC;
1104 static void remove_insn_ops(struct instruction *insn)
1106 struct stack_op *op, *tmp;
1108 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1109 list_del(&op->list);
1114 static void annotate_call_site(struct objtool_file *file,
1115 struct instruction *insn, bool sibling)
1117 struct reloc *reloc = insn_reloc(file, insn);
1118 struct symbol *sym = insn->call_dest;
1124 * Alternative replacement code is just template code which is
1125 * sometimes copied to the original instruction. For now, don't
1126 * annotate it. (In the future we might consider annotating the
1127 * original instruction if/when it ever makes sense to do so.)
1129 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1132 if (sym->static_call_tramp) {
1133 list_add_tail(&insn->call_node, &file->static_call_list);
1137 if (sym->retpoline_thunk) {
1138 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1143 * Many compilers cannot disable KCOV or sanitizer calls with a function
1144 * attribute so they need a little help, NOP out any such calls from
1147 if (insn->sec->noinstr && sym->profiling_func) {
1149 reloc->type = R_NONE;
1150 elf_write_reloc(file->elf, reloc);
1153 elf_write_insn(file->elf, insn->sec,
1154 insn->offset, insn->len,
1155 sibling ? arch_ret_insn(insn->len)
1156 : arch_nop_insn(insn->len));
1158 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1162 * We've replaced the tail-call JMP insn by two new
1163 * insn: RET; INT3, except we only have a single struct
1164 * insn here. Mark it retpoline_safe to avoid the SLS
1165 * warning, instead of adding another insn.
1167 insn->retpoline_safe = true;
1173 if (opts.mcount && sym->fentry) {
1175 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1178 reloc->type = R_NONE;
1179 elf_write_reloc(file->elf, reloc);
1182 elf_write_insn(file->elf, insn->sec,
1183 insn->offset, insn->len,
1184 arch_nop_insn(insn->len));
1186 insn->type = INSN_NOP;
1188 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1192 if (!sibling && dead_end_function(file, sym))
1193 insn->dead_end = true;
1196 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1197 struct symbol *dest, bool sibling)
1199 insn->call_dest = dest;
1204 * Whatever stack impact regular CALLs have, should be undone
1205 * by the RETURN of the called function.
1207 * Annotated intra-function calls retain the stack_ops but
1208 * are converted to JUMP, see read_intra_function_calls().
1210 remove_insn_ops(insn);
1212 annotate_call_site(file, insn, sibling);
1215 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1218 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1219 * so convert them accordingly.
1221 switch (insn->type) {
1223 insn->type = INSN_CALL_DYNAMIC;
1225 case INSN_JUMP_UNCONDITIONAL:
1226 insn->type = INSN_JUMP_DYNAMIC;
1228 case INSN_JUMP_CONDITIONAL:
1229 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1235 insn->retpoline_safe = true;
1238 * Whatever stack impact regular CALLs have, should be undone
1239 * by the RETURN of the called function.
1241 * Annotated intra-function calls retain the stack_ops but
1242 * are converted to JUMP, see read_intra_function_calls().
1244 remove_insn_ops(insn);
1246 annotate_call_site(file, insn, false);
1249 static bool same_function(struct instruction *insn1, struct instruction *insn2)
1251 return insn1->func->pfunc == insn2->func->pfunc;
1254 static bool is_first_func_insn(struct objtool_file *file, struct instruction *insn)
1256 if (insn->offset == insn->func->offset)
1260 struct instruction *prev = prev_insn_same_sym(file, insn);
1262 if (prev && prev->type == INSN_ENDBR &&
1263 insn->offset == insn->func->offset + prev->len)
1271 * Find the destination instructions for all jumps.
1273 static int add_jump_destinations(struct objtool_file *file)
1275 struct instruction *insn, *jump_dest;
1276 struct reloc *reloc;
1277 struct section *dest_sec;
1278 unsigned long dest_off;
1280 for_each_insn(file, insn) {
1281 if (insn->jump_dest) {
1283 * handle_group_alt() may have previously set
1284 * 'jump_dest' for some alternatives.
1288 if (!is_static_jump(insn))
1291 reloc = insn_reloc(file, insn);
1293 dest_sec = insn->sec;
1294 dest_off = arch_jump_destination(insn);
1295 } else if (reloc->sym->type == STT_SECTION) {
1296 dest_sec = reloc->sym->sec;
1297 dest_off = arch_dest_reloc_offset(reloc->addend);
1298 } else if (reloc->sym->retpoline_thunk) {
1299 add_retpoline_call(file, insn);
1301 } else if (insn->func) {
1303 * External sibling call or internal sibling call with
1306 add_call_dest(file, insn, reloc->sym, true);
1308 } else if (reloc->sym->sec->idx) {
1309 dest_sec = reloc->sym->sec;
1310 dest_off = reloc->sym->sym.st_value +
1311 arch_dest_reloc_offset(reloc->addend);
1313 /* non-func asm code jumping to another file */
1317 jump_dest = find_insn(file, dest_sec, dest_off);
1319 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1320 insn->sec, insn->offset, dest_sec->name,
1326 * Cross-function jump.
1328 if (insn->func && jump_dest->func &&
1329 insn->func != jump_dest->func) {
1332 * For GCC 8+, create parent/child links for any cold
1333 * subfunctions. This is _mostly_ redundant with a
1334 * similar initialization in read_symbols().
1336 * If a function has aliases, we want the *first* such
1337 * function in the symbol table to be the subfunction's
1338 * parent. In that case we overwrite the
1339 * initialization done in read_symbols().
1341 * However this code can't completely replace the
1342 * read_symbols() code because this doesn't detect the
1343 * case where the parent function's only reference to a
1344 * subfunction is through a jump table.
1346 if (!strstr(insn->func->name, ".cold") &&
1347 strstr(jump_dest->func->name, ".cold")) {
1348 insn->func->cfunc = jump_dest->func;
1349 jump_dest->func->pfunc = insn->func;
1351 } else if (!same_function(insn, jump_dest) &&
1352 is_first_func_insn(file, jump_dest)) {
1354 * Internal sibling call without reloc or with
1355 * STT_SECTION reloc.
1357 add_call_dest(file, insn, jump_dest->func, true);
1362 insn->jump_dest = jump_dest;
1368 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1370 struct symbol *call_dest;
1372 call_dest = find_func_by_offset(sec, offset);
1374 call_dest = find_symbol_by_offset(sec, offset);
1380 * Find the destination instructions for all calls.
1382 static int add_call_destinations(struct objtool_file *file)
1384 struct instruction *insn;
1385 unsigned long dest_off;
1386 struct symbol *dest;
1387 struct reloc *reloc;
1389 for_each_insn(file, insn) {
1390 if (insn->type != INSN_CALL)
1393 reloc = insn_reloc(file, insn);
1395 dest_off = arch_jump_destination(insn);
1396 dest = find_call_destination(insn->sec, dest_off);
1398 add_call_dest(file, insn, dest, false);
1403 if (!insn->call_dest) {
1404 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1408 if (insn->func && insn->call_dest->type != STT_FUNC) {
1409 WARN_FUNC("unsupported call to non-function",
1410 insn->sec, insn->offset);
1414 } else if (reloc->sym->type == STT_SECTION) {
1415 dest_off = arch_dest_reloc_offset(reloc->addend);
1416 dest = find_call_destination(reloc->sym->sec, dest_off);
1418 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1419 insn->sec, insn->offset,
1420 reloc->sym->sec->name,
1425 add_call_dest(file, insn, dest, false);
1427 } else if (reloc->sym->retpoline_thunk) {
1428 add_retpoline_call(file, insn);
1431 add_call_dest(file, insn, reloc->sym, false);
1438 * The .alternatives section requires some extra special care over and above
1439 * other special sections because alternatives are patched in place.
1441 static int handle_group_alt(struct objtool_file *file,
1442 struct special_alt *special_alt,
1443 struct instruction *orig_insn,
1444 struct instruction **new_insn)
1446 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1447 struct alt_group *orig_alt_group, *new_alt_group;
1448 unsigned long dest_off;
1451 orig_alt_group = malloc(sizeof(*orig_alt_group));
1452 if (!orig_alt_group) {
1453 WARN("malloc failed");
1456 orig_alt_group->cfi = calloc(special_alt->orig_len,
1457 sizeof(struct cfi_state *));
1458 if (!orig_alt_group->cfi) {
1459 WARN("calloc failed");
1463 last_orig_insn = NULL;
1465 sec_for_each_insn_from(file, insn) {
1466 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1469 insn->alt_group = orig_alt_group;
1470 last_orig_insn = insn;
1472 orig_alt_group->orig_group = NULL;
1473 orig_alt_group->first_insn = orig_insn;
1474 orig_alt_group->last_insn = last_orig_insn;
1477 new_alt_group = malloc(sizeof(*new_alt_group));
1478 if (!new_alt_group) {
1479 WARN("malloc failed");
1483 if (special_alt->new_len < special_alt->orig_len) {
1485 * Insert a fake nop at the end to make the replacement
1486 * alt_group the same size as the original. This is needed to
1487 * allow propagate_alt_cfi() to do its magic. When the last
1488 * instruction affects the stack, the instruction after it (the
1489 * nop) will propagate the new state to the shared CFI array.
1491 nop = malloc(sizeof(*nop));
1493 WARN("malloc failed");
1496 memset(nop, 0, sizeof(*nop));
1497 INIT_LIST_HEAD(&nop->alts);
1498 INIT_LIST_HEAD(&nop->stack_ops);
1500 nop->sec = special_alt->new_sec;
1501 nop->offset = special_alt->new_off + special_alt->new_len;
1502 nop->len = special_alt->orig_len - special_alt->new_len;
1503 nop->type = INSN_NOP;
1504 nop->func = orig_insn->func;
1505 nop->alt_group = new_alt_group;
1506 nop->ignore = orig_insn->ignore_alts;
1509 if (!special_alt->new_len) {
1515 sec_for_each_insn_from(file, insn) {
1516 struct reloc *alt_reloc;
1518 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1521 last_new_insn = insn;
1523 insn->ignore = orig_insn->ignore_alts;
1524 insn->func = orig_insn->func;
1525 insn->alt_group = new_alt_group;
1528 * Since alternative replacement code is copy/pasted by the
1529 * kernel after applying relocations, generally such code can't
1530 * have relative-address relocation references to outside the
1531 * .altinstr_replacement section, unless the arch's
1532 * alternatives code can adjust the relative offsets
1535 alt_reloc = insn_reloc(file, insn);
1537 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1539 WARN_FUNC("unsupported relocation in alternatives section",
1540 insn->sec, insn->offset);
1544 if (!is_static_jump(insn))
1547 if (!insn->immediate)
1550 dest_off = arch_jump_destination(insn);
1551 if (dest_off == special_alt->new_off + special_alt->new_len) {
1552 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1553 if (!insn->jump_dest) {
1554 WARN_FUNC("can't find alternative jump destination",
1555 insn->sec, insn->offset);
1561 if (!last_new_insn) {
1562 WARN_FUNC("can't find last new alternative instruction",
1563 special_alt->new_sec, special_alt->new_off);
1568 list_add(&nop->list, &last_new_insn->list);
1570 new_alt_group->orig_group = orig_alt_group;
1571 new_alt_group->first_insn = *new_insn;
1572 new_alt_group->last_insn = nop ? : last_new_insn;
1573 new_alt_group->cfi = orig_alt_group->cfi;
1578 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1579 * If the original instruction is a jump, make the alt entry an effective nop
1580 * by just skipping the original instruction.
1582 static int handle_jump_alt(struct objtool_file *file,
1583 struct special_alt *special_alt,
1584 struct instruction *orig_insn,
1585 struct instruction **new_insn)
1587 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1588 orig_insn->type != INSN_NOP) {
1590 WARN_FUNC("unsupported instruction at jump label",
1591 orig_insn->sec, orig_insn->offset);
1595 if (special_alt->key_addend & 2) {
1596 struct reloc *reloc = insn_reloc(file, orig_insn);
1599 reloc->type = R_NONE;
1600 elf_write_reloc(file->elf, reloc);
1602 elf_write_insn(file->elf, orig_insn->sec,
1603 orig_insn->offset, orig_insn->len,
1604 arch_nop_insn(orig_insn->len));
1605 orig_insn->type = INSN_NOP;
1608 if (orig_insn->type == INSN_NOP) {
1609 if (orig_insn->len == 2)
1610 file->jl_nop_short++;
1612 file->jl_nop_long++;
1617 if (orig_insn->len == 2)
1622 *new_insn = list_next_entry(orig_insn, list);
1627 * Read all the special sections which have alternate instructions which can be
1628 * patched in or redirected to at runtime. Each instruction having alternate
1629 * instruction(s) has them added to its insn->alts list, which will be
1630 * traversed in validate_branch().
1632 static int add_special_section_alts(struct objtool_file *file)
1634 struct list_head special_alts;
1635 struct instruction *orig_insn, *new_insn;
1636 struct special_alt *special_alt, *tmp;
1637 struct alternative *alt;
1640 ret = special_get_alts(file->elf, &special_alts);
1644 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1646 orig_insn = find_insn(file, special_alt->orig_sec,
1647 special_alt->orig_off);
1649 WARN_FUNC("special: can't find orig instruction",
1650 special_alt->orig_sec, special_alt->orig_off);
1656 if (!special_alt->group || special_alt->new_len) {
1657 new_insn = find_insn(file, special_alt->new_sec,
1658 special_alt->new_off);
1660 WARN_FUNC("special: can't find new instruction",
1661 special_alt->new_sec,
1662 special_alt->new_off);
1668 if (special_alt->group) {
1669 if (!special_alt->orig_len) {
1670 WARN_FUNC("empty alternative entry",
1671 orig_insn->sec, orig_insn->offset);
1675 ret = handle_group_alt(file, special_alt, orig_insn,
1679 } else if (special_alt->jump_or_nop) {
1680 ret = handle_jump_alt(file, special_alt, orig_insn,
1686 alt = malloc(sizeof(*alt));
1688 WARN("malloc failed");
1693 alt->insn = new_insn;
1694 alt->skip_orig = special_alt->skip_orig;
1695 orig_insn->ignore_alts |= special_alt->skip_alt;
1696 list_add_tail(&alt->list, &orig_insn->alts);
1698 list_del(&special_alt->list);
1703 printf("jl\\\tNOP\tJMP\n");
1704 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1705 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1712 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1713 struct reloc *table)
1715 struct reloc *reloc = table;
1716 struct instruction *dest_insn;
1717 struct alternative *alt;
1718 struct symbol *pfunc = insn->func->pfunc;
1719 unsigned int prev_offset = 0;
1722 * Each @reloc is a switch table relocation which points to the target
1725 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1727 /* Check for the end of the table: */
1728 if (reloc != table && reloc->jump_table_start)
1731 /* Make sure the table entries are consecutive: */
1732 if (prev_offset && reloc->offset != prev_offset + 8)
1735 /* Detect function pointers from contiguous objects: */
1736 if (reloc->sym->sec == pfunc->sec &&
1737 reloc->addend == pfunc->offset)
1740 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1744 /* Make sure the destination is in the same function: */
1745 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1748 alt = malloc(sizeof(*alt));
1750 WARN("malloc failed");
1754 alt->insn = dest_insn;
1755 list_add_tail(&alt->list, &insn->alts);
1756 prev_offset = reloc->offset;
1760 WARN_FUNC("can't find switch jump table",
1761 insn->sec, insn->offset);
1769 * find_jump_table() - Given a dynamic jump, find the switch jump table
1770 * associated with it.
1772 static struct reloc *find_jump_table(struct objtool_file *file,
1773 struct symbol *func,
1774 struct instruction *insn)
1776 struct reloc *table_reloc;
1777 struct instruction *dest_insn, *orig_insn = insn;
1780 * Backward search using the @first_jump_src links, these help avoid
1781 * much of the 'in between' code. Which avoids us getting confused by
1785 insn && insn->func && insn->func->pfunc == func;
1786 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1788 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1791 /* allow small jumps within the range */
1792 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1794 (insn->jump_dest->offset <= insn->offset ||
1795 insn->jump_dest->offset > orig_insn->offset))
1798 table_reloc = arch_find_switch_table(file, insn);
1801 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1802 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1812 * First pass: Mark the head of each jump table so that in the next pass,
1813 * we know when a given jump table ends and the next one starts.
1815 static void mark_func_jump_tables(struct objtool_file *file,
1816 struct symbol *func)
1818 struct instruction *insn, *last = NULL;
1819 struct reloc *reloc;
1821 func_for_each_insn(file, func, insn) {
1826 * Store back-pointers for unconditional forward jumps such
1827 * that find_jump_table() can back-track using those and
1828 * avoid some potentially confusing code.
1830 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1831 insn->offset > last->offset &&
1832 insn->jump_dest->offset > insn->offset &&
1833 !insn->jump_dest->first_jump_src) {
1835 insn->jump_dest->first_jump_src = insn;
1836 last = insn->jump_dest;
1839 if (insn->type != INSN_JUMP_DYNAMIC)
1842 reloc = find_jump_table(file, func, insn);
1844 reloc->jump_table_start = true;
1845 insn->jump_table = reloc;
1850 static int add_func_jump_tables(struct objtool_file *file,
1851 struct symbol *func)
1853 struct instruction *insn;
1856 func_for_each_insn(file, func, insn) {
1857 if (!insn->jump_table)
1860 ret = add_jump_table(file, insn, insn->jump_table);
1869 * For some switch statements, gcc generates a jump table in the .rodata
1870 * section which contains a list of addresses within the function to jump to.
1871 * This finds these jump tables and adds them to the insn->alts lists.
1873 static int add_jump_table_alts(struct objtool_file *file)
1875 struct section *sec;
1876 struct symbol *func;
1882 for_each_sec(file, sec) {
1883 list_for_each_entry(func, &sec->symbol_list, list) {
1884 if (func->type != STT_FUNC)
1887 mark_func_jump_tables(file, func);
1888 ret = add_func_jump_tables(file, func);
1897 static void set_func_state(struct cfi_state *state)
1899 state->cfa = initial_func_cfi.cfa;
1900 memcpy(&state->regs, &initial_func_cfi.regs,
1901 CFI_NUM_REGS * sizeof(struct cfi_reg));
1902 state->stack_size = initial_func_cfi.cfa.offset;
1905 static int read_unwind_hints(struct objtool_file *file)
1907 struct cfi_state cfi = init_cfi;
1908 struct section *sec, *relocsec;
1909 struct unwind_hint *hint;
1910 struct instruction *insn;
1911 struct reloc *reloc;
1914 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1918 relocsec = sec->reloc;
1920 WARN("missing .rela.discard.unwind_hints section");
1924 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1925 WARN("struct unwind_hint size mismatch");
1931 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1932 hint = (struct unwind_hint *)sec->data->d_buf + i;
1934 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1936 WARN("can't find reloc for unwind_hints[%d]", i);
1940 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1942 WARN("can't find insn for unwind_hints[%d]", i);
1948 if (opts.ibt && hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1949 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1951 if (sym && sym->bind == STB_GLOBAL &&
1952 insn->type != INSN_ENDBR && !insn->noendbr) {
1953 WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR",
1954 insn->sec, insn->offset);
1958 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1959 insn->cfi = &func_cfi;
1966 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1967 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1968 insn->sec, insn->offset, hint->sp_reg);
1972 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1973 cfi.type = hint->type;
1974 cfi.end = hint->end;
1976 insn->cfi = cfi_hash_find_or_add(&cfi);
1982 static int read_noendbr_hints(struct objtool_file *file)
1984 struct section *sec;
1985 struct instruction *insn;
1986 struct reloc *reloc;
1988 sec = find_section_by_name(file->elf, ".rela.discard.noendbr");
1992 list_for_each_entry(reloc, &sec->reloc_list, list) {
1993 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
1995 WARN("bad .discard.noendbr entry");
1999 if (insn->type == INSN_ENDBR)
2000 WARN_FUNC("ANNOTATE_NOENDBR on ENDBR", insn->sec, insn->offset);
2008 static int read_retpoline_hints(struct objtool_file *file)
2010 struct section *sec;
2011 struct instruction *insn;
2012 struct reloc *reloc;
2014 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
2018 list_for_each_entry(reloc, &sec->reloc_list, list) {
2019 if (reloc->sym->type != STT_SECTION) {
2020 WARN("unexpected relocation symbol type in %s", sec->name);
2024 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2026 WARN("bad .discard.retpoline_safe entry");
2030 if (insn->type != INSN_JUMP_DYNAMIC &&
2031 insn->type != INSN_CALL_DYNAMIC) {
2032 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
2033 insn->sec, insn->offset);
2037 insn->retpoline_safe = true;
2043 static int read_instr_hints(struct objtool_file *file)
2045 struct section *sec;
2046 struct instruction *insn;
2047 struct reloc *reloc;
2049 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2053 list_for_each_entry(reloc, &sec->reloc_list, list) {
2054 if (reloc->sym->type != STT_SECTION) {
2055 WARN("unexpected relocation symbol type in %s", sec->name);
2059 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2061 WARN("bad .discard.instr_end entry");
2068 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2072 list_for_each_entry(reloc, &sec->reloc_list, list) {
2073 if (reloc->sym->type != STT_SECTION) {
2074 WARN("unexpected relocation symbol type in %s", sec->name);
2078 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2080 WARN("bad .discard.instr_begin entry");
2090 static int read_intra_function_calls(struct objtool_file *file)
2092 struct instruction *insn;
2093 struct section *sec;
2094 struct reloc *reloc;
2096 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2100 list_for_each_entry(reloc, &sec->reloc_list, list) {
2101 unsigned long dest_off;
2103 if (reloc->sym->type != STT_SECTION) {
2104 WARN("unexpected relocation symbol type in %s",
2109 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2111 WARN("bad .discard.intra_function_call entry");
2115 if (insn->type != INSN_CALL) {
2116 WARN_FUNC("intra_function_call not a direct call",
2117 insn->sec, insn->offset);
2122 * Treat intra-function CALLs as JMPs, but with a stack_op.
2123 * See add_call_destinations(), which strips stack_ops from
2126 insn->type = INSN_JUMP_UNCONDITIONAL;
2128 dest_off = insn->offset + insn->len + insn->immediate;
2129 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2130 if (!insn->jump_dest) {
2131 WARN_FUNC("can't find call dest at %s+0x%lx",
2132 insn->sec, insn->offset,
2133 insn->sec->name, dest_off);
2142 * Return true if name matches an instrumentation function, where calls to that
2143 * function from noinstr code can safely be removed, but compilers won't do so.
2145 static bool is_profiling_func(const char *name)
2148 * Many compilers cannot disable KCOV with a function attribute.
2150 if (!strncmp(name, "__sanitizer_cov_", 16))
2154 * Some compilers currently do not remove __tsan_func_entry/exit nor
2155 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2156 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2157 * minimum Clang version is 14.0, this can be removed.
2159 if (!strncmp(name, "__tsan_func_", 12) ||
2160 !strcmp(name, "__tsan_atomic_signal_fence"))
2166 static int classify_symbols(struct objtool_file *file)
2168 struct section *sec;
2169 struct symbol *func;
2171 for_each_sec(file, sec) {
2172 list_for_each_entry(func, &sec->symbol_list, list) {
2173 if (func->bind != STB_GLOBAL)
2176 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2177 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2178 func->static_call_tramp = true;
2180 if (arch_is_retpoline(func))
2181 func->retpoline_thunk = true;
2183 if (!strcmp(func->name, "__fentry__"))
2184 func->fentry = true;
2186 if (is_profiling_func(func->name))
2187 func->profiling_func = true;
2194 static void mark_rodata(struct objtool_file *file)
2196 struct section *sec;
2200 * Search for the following rodata sections, each of which can
2201 * potentially contain jump tables:
2203 * - .rodata: can contain GCC switch tables
2204 * - .rodata.<func>: same, if -fdata-sections is being used
2205 * - .rodata..c_jump_table: contains C annotated jump tables
2207 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2209 for_each_sec(file, sec) {
2210 if (!strncmp(sec->name, ".rodata", 7) &&
2211 !strstr(sec->name, ".str1.")) {
2217 file->rodata = found;
2220 static int decode_sections(struct objtool_file *file)
2226 ret = init_pv_ops(file);
2230 ret = decode_instructions(file);
2235 add_uaccess_safe(file);
2237 ret = add_ignore_alternatives(file);
2242 * Must be before read_unwind_hints() since that needs insn->noendbr.
2244 ret = read_noendbr_hints(file);
2249 * Must be before add_{jump_call}_destination.
2251 ret = classify_symbols(file);
2256 * Must be before add_jump_destinations(), which depends on 'func'
2257 * being set for alternatives, to enable proper sibling call detection.
2259 ret = add_special_section_alts(file);
2263 ret = add_jump_destinations(file);
2268 * Must be before add_call_destination(); it changes INSN_CALL to
2271 ret = read_intra_function_calls(file);
2275 ret = add_call_destinations(file);
2280 * Must be after add_call_destinations() such that it can override
2281 * dead_end_function() marks.
2283 ret = add_dead_ends(file);
2287 ret = add_jump_table_alts(file);
2291 ret = read_unwind_hints(file);
2295 ret = read_retpoline_hints(file);
2299 ret = read_instr_hints(file);
2306 static bool is_fentry_call(struct instruction *insn)
2308 if (insn->type == INSN_CALL &&
2310 insn->call_dest->fentry)
2316 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2318 struct cfi_state *cfi = &state->cfi;
2321 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2324 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2327 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2330 for (i = 0; i < CFI_NUM_REGS; i++) {
2331 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2332 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2339 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2340 int expected_offset)
2342 return reg->base == CFI_CFA &&
2343 reg->offset == expected_offset;
2346 static bool has_valid_stack_frame(struct insn_state *state)
2348 struct cfi_state *cfi = &state->cfi;
2350 if (cfi->cfa.base == CFI_BP &&
2351 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2352 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2355 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2361 static int update_cfi_state_regs(struct instruction *insn,
2362 struct cfi_state *cfi,
2363 struct stack_op *op)
2365 struct cfi_reg *cfa = &cfi->cfa;
2367 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2371 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2375 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2378 /* add immediate to sp */
2379 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2380 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2381 cfa->offset -= op->src.offset;
2386 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2388 if (arch_callee_saved_reg(reg) &&
2389 cfi->regs[reg].base == CFI_UNDEFINED) {
2390 cfi->regs[reg].base = base;
2391 cfi->regs[reg].offset = offset;
2395 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2397 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2398 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2402 * A note about DRAP stack alignment:
2404 * GCC has the concept of a DRAP register, which is used to help keep track of
2405 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2406 * register. The typical DRAP pattern is:
2408 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2409 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2410 * 41 ff 72 f8 pushq -0x8(%r10)
2412 * 48 89 e5 mov %rsp,%rbp
2419 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2422 * There are some variations in the epilogues, like:
2430 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2435 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2436 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2437 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2438 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2440 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2443 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2444 * restored beforehand:
2447 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2448 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2450 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2454 static int update_cfi_state(struct instruction *insn,
2455 struct instruction *next_insn,
2456 struct cfi_state *cfi, struct stack_op *op)
2458 struct cfi_reg *cfa = &cfi->cfa;
2459 struct cfi_reg *regs = cfi->regs;
2461 /* stack operations don't make sense with an undefined CFA */
2462 if (cfa->base == CFI_UNDEFINED) {
2464 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2470 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2471 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2472 return update_cfi_state_regs(insn, cfi, op);
2474 switch (op->dest.type) {
2477 switch (op->src.type) {
2480 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2481 cfa->base == CFI_SP &&
2482 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2484 /* mov %rsp, %rbp */
2485 cfa->base = op->dest.reg;
2486 cfi->bp_scratch = false;
2489 else if (op->src.reg == CFI_SP &&
2490 op->dest.reg == CFI_BP && cfi->drap) {
2492 /* drap: mov %rsp, %rbp */
2493 regs[CFI_BP].base = CFI_BP;
2494 regs[CFI_BP].offset = -cfi->stack_size;
2495 cfi->bp_scratch = false;
2498 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2503 * This is needed for the rare case where GCC
2510 cfi->vals[op->dest.reg].base = CFI_CFA;
2511 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2514 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2515 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2520 * Restore the original stack pointer (Clang).
2522 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2525 else if (op->dest.reg == cfa->base) {
2527 /* mov %reg, %rsp */
2528 if (cfa->base == CFI_SP &&
2529 cfi->vals[op->src.reg].base == CFI_CFA) {
2532 * This is needed for the rare case
2533 * where GCC does something dumb like:
2535 * lea 0x8(%rsp), %rcx
2539 cfa->offset = -cfi->vals[op->src.reg].offset;
2540 cfi->stack_size = cfa->offset;
2542 } else if (cfa->base == CFI_SP &&
2543 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2544 cfi->vals[op->src.reg].offset == cfa->offset) {
2549 * 1: mov %rsp, (%[tos])
2550 * 2: mov %[tos], %rsp
2556 * 1 - places a pointer to the previous
2557 * stack at the Top-of-Stack of the
2560 * 2 - switches to the new stack.
2562 * 3 - pops the Top-of-Stack to restore
2563 * the original stack.
2565 * Note: we set base to SP_INDIRECT
2566 * here and preserve offset. Therefore
2567 * when the unwinder reaches ToS it
2568 * will dereference SP and then add the
2569 * offset to find the next frame, IOW:
2572 cfa->base = CFI_SP_INDIRECT;
2575 cfa->base = CFI_UNDEFINED;
2580 else if (op->dest.reg == CFI_SP &&
2581 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2582 cfi->vals[op->src.reg].offset == cfa->offset) {
2585 * The same stack swizzle case 2) as above. But
2586 * because we can't change cfa->base, case 3)
2587 * will become a regular POP. Pretend we're a
2588 * PUSH so things don't go unbalanced.
2590 cfi->stack_size += 8;
2597 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2600 cfi->stack_size -= op->src.offset;
2601 if (cfa->base == CFI_SP)
2602 cfa->offset -= op->src.offset;
2606 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2608 /* lea disp(%rbp), %rsp */
2609 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2613 if (!cfi->drap && op->src.reg == CFI_SP &&
2614 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2615 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2617 /* lea disp(%rsp), %rbp */
2619 cfa->offset -= op->src.offset;
2620 cfi->bp_scratch = false;
2624 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2626 /* drap: lea disp(%rsp), %drap */
2627 cfi->drap_reg = op->dest.reg;
2630 * lea disp(%rsp), %reg
2632 * This is needed for the rare case where GCC
2633 * does something dumb like:
2635 * lea 0x8(%rsp), %rcx
2639 cfi->vals[op->dest.reg].base = CFI_CFA;
2640 cfi->vals[op->dest.reg].offset = \
2641 -cfi->stack_size + op->src.offset;
2646 if (cfi->drap && op->dest.reg == CFI_SP &&
2647 op->src.reg == cfi->drap_reg) {
2649 /* drap: lea disp(%drap), %rsp */
2651 cfa->offset = cfi->stack_size = -op->src.offset;
2652 cfi->drap_reg = CFI_UNDEFINED;
2657 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2658 WARN_FUNC("unsupported stack register modification",
2659 insn->sec, insn->offset);
2666 if (op->dest.reg != CFI_SP ||
2667 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2668 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2669 WARN_FUNC("unsupported stack pointer realignment",
2670 insn->sec, insn->offset);
2674 if (cfi->drap_reg != CFI_UNDEFINED) {
2675 /* drap: and imm, %rsp */
2676 cfa->base = cfi->drap_reg;
2677 cfa->offset = cfi->stack_size = 0;
2682 * Older versions of GCC (4.8ish) realign the stack
2683 * without DRAP, with a frame pointer.
2690 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2692 /* pop %rsp; # restore from a stack swizzle */
2697 if (!cfi->drap && op->dest.reg == cfa->base) {
2703 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2704 op->dest.reg == cfi->drap_reg &&
2705 cfi->drap_offset == -cfi->stack_size) {
2707 /* drap: pop %drap */
2708 cfa->base = cfi->drap_reg;
2710 cfi->drap_offset = -1;
2712 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2715 restore_reg(cfi, op->dest.reg);
2718 cfi->stack_size -= 8;
2719 if (cfa->base == CFI_SP)
2724 case OP_SRC_REG_INDIRECT:
2725 if (!cfi->drap && op->dest.reg == cfa->base &&
2726 op->dest.reg == CFI_BP) {
2728 /* mov disp(%rsp), %rbp */
2730 cfa->offset = cfi->stack_size;
2733 if (cfi->drap && op->src.reg == CFI_BP &&
2734 op->src.offset == cfi->drap_offset) {
2736 /* drap: mov disp(%rbp), %drap */
2737 cfa->base = cfi->drap_reg;
2739 cfi->drap_offset = -1;
2742 if (cfi->drap && op->src.reg == CFI_BP &&
2743 op->src.offset == regs[op->dest.reg].offset) {
2745 /* drap: mov disp(%rbp), %reg */
2746 restore_reg(cfi, op->dest.reg);
2748 } else if (op->src.reg == cfa->base &&
2749 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2751 /* mov disp(%rbp), %reg */
2752 /* mov disp(%rsp), %reg */
2753 restore_reg(cfi, op->dest.reg);
2755 } else if (op->src.reg == CFI_SP &&
2756 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2758 /* mov disp(%rsp), %reg */
2759 restore_reg(cfi, op->dest.reg);
2765 WARN_FUNC("unknown stack-related instruction",
2766 insn->sec, insn->offset);
2774 cfi->stack_size += 8;
2775 if (cfa->base == CFI_SP)
2778 if (op->src.type != OP_SRC_REG)
2782 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2784 /* drap: push %drap */
2785 cfa->base = CFI_BP_INDIRECT;
2786 cfa->offset = -cfi->stack_size;
2788 /* save drap so we know when to restore it */
2789 cfi->drap_offset = -cfi->stack_size;
2791 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2793 /* drap: push %rbp */
2794 cfi->stack_size = 0;
2798 /* drap: push %reg */
2799 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2805 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2808 /* detect when asm code uses rbp as a scratch register */
2809 if (!opts.no_fp && insn->func && op->src.reg == CFI_BP &&
2810 cfa->base != CFI_BP)
2811 cfi->bp_scratch = true;
2814 case OP_DEST_REG_INDIRECT:
2817 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2819 /* drap: mov %drap, disp(%rbp) */
2820 cfa->base = CFI_BP_INDIRECT;
2821 cfa->offset = op->dest.offset;
2823 /* save drap offset so we know when to restore it */
2824 cfi->drap_offset = op->dest.offset;
2827 /* drap: mov reg, disp(%rbp) */
2828 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2831 } else if (op->dest.reg == cfa->base) {
2833 /* mov reg, disp(%rbp) */
2834 /* mov reg, disp(%rsp) */
2835 save_reg(cfi, op->src.reg, CFI_CFA,
2836 op->dest.offset - cfi->cfa.offset);
2838 } else if (op->dest.reg == CFI_SP) {
2840 /* mov reg, disp(%rsp) */
2841 save_reg(cfi, op->src.reg, CFI_CFA,
2842 op->dest.offset - cfi->stack_size);
2844 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2846 /* mov %rsp, (%reg); # setup a stack swizzle. */
2847 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2848 cfi->vals[op->dest.reg].offset = cfa->offset;
2854 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2855 WARN_FUNC("unknown stack-related memory operation",
2856 insn->sec, insn->offset);
2861 cfi->stack_size -= 8;
2862 if (cfa->base == CFI_SP)
2868 WARN_FUNC("unknown stack-related instruction",
2869 insn->sec, insn->offset);
2877 * The stack layouts of alternatives instructions can sometimes diverge when
2878 * they have stack modifications. That's fine as long as the potential stack
2879 * layouts don't conflict at any given potential instruction boundary.
2881 * Flatten the CFIs of the different alternative code streams (both original
2882 * and replacement) into a single shared CFI array which can be used to detect
2883 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2885 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2887 struct cfi_state **alt_cfi;
2890 if (!insn->alt_group)
2894 WARN("CFI missing");
2898 alt_cfi = insn->alt_group->cfi;
2899 group_off = insn->offset - insn->alt_group->first_insn->offset;
2901 if (!alt_cfi[group_off]) {
2902 alt_cfi[group_off] = insn->cfi;
2904 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2905 WARN_FUNC("stack layout conflict in alternatives",
2906 insn->sec, insn->offset);
2914 static int handle_insn_ops(struct instruction *insn,
2915 struct instruction *next_insn,
2916 struct insn_state *state)
2918 struct stack_op *op;
2920 list_for_each_entry(op, &insn->stack_ops, list) {
2922 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2925 if (!insn->alt_group)
2928 if (op->dest.type == OP_DEST_PUSHF) {
2929 if (!state->uaccess_stack) {
2930 state->uaccess_stack = 1;
2931 } else if (state->uaccess_stack >> 31) {
2932 WARN_FUNC("PUSHF stack exhausted",
2933 insn->sec, insn->offset);
2936 state->uaccess_stack <<= 1;
2937 state->uaccess_stack |= state->uaccess;
2940 if (op->src.type == OP_SRC_POPF) {
2941 if (state->uaccess_stack) {
2942 state->uaccess = state->uaccess_stack & 1;
2943 state->uaccess_stack >>= 1;
2944 if (state->uaccess_stack == 1)
2945 state->uaccess_stack = 0;
2953 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2955 struct cfi_state *cfi1 = insn->cfi;
2959 WARN("CFI missing");
2963 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2965 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2966 insn->sec, insn->offset,
2967 cfi1->cfa.base, cfi1->cfa.offset,
2968 cfi2->cfa.base, cfi2->cfa.offset);
2970 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2971 for (i = 0; i < CFI_NUM_REGS; i++) {
2972 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2973 sizeof(struct cfi_reg)))
2976 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2977 insn->sec, insn->offset,
2978 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2979 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2983 } else if (cfi1->type != cfi2->type) {
2985 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2986 insn->sec, insn->offset, cfi1->type, cfi2->type);
2988 } else if (cfi1->drap != cfi2->drap ||
2989 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2990 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2992 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2993 insn->sec, insn->offset,
2994 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2995 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
3003 static inline bool func_uaccess_safe(struct symbol *func)
3006 return func->uaccess_safe;
3011 static inline const char *call_dest_name(struct instruction *insn)
3013 static char pvname[19];
3017 if (insn->call_dest)
3018 return insn->call_dest->name;
3020 rel = insn_reloc(NULL, insn);
3021 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
3022 idx = (rel->addend / sizeof(void *));
3023 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3030 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3032 struct symbol *target;
3036 rel = insn_reloc(file, insn);
3037 if (!rel || strcmp(rel->sym->name, "pv_ops"))
3040 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
3042 if (file->pv_ops[idx].clean)
3045 file->pv_ops[idx].clean = true;
3047 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3048 if (!target->sec->noinstr) {
3049 WARN("pv_ops[%d]: %s", idx, target->name);
3050 file->pv_ops[idx].clean = false;
3054 return file->pv_ops[idx].clean;
3057 static inline bool noinstr_call_dest(struct objtool_file *file,
3058 struct instruction *insn,
3059 struct symbol *func)
3062 * We can't deal with indirect function calls at present;
3063 * assume they're instrumented.
3067 return pv_call_dest(file, insn);
3073 * If the symbol is from a noinstr section; we good.
3075 if (func->sec->noinstr)
3079 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3080 * something 'BAD' happened. At the risk of taking the machine down,
3081 * let them proceed to get the message out.
3083 if (!strncmp(func->name, "__ubsan_handle_", 15))
3089 static int validate_call(struct objtool_file *file,
3090 struct instruction *insn,
3091 struct insn_state *state)
3093 if (state->noinstr && state->instr <= 0 &&
3094 !noinstr_call_dest(file, insn, insn->call_dest)) {
3095 WARN_FUNC("call to %s() leaves .noinstr.text section",
3096 insn->sec, insn->offset, call_dest_name(insn));
3100 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
3101 WARN_FUNC("call to %s() with UACCESS enabled",
3102 insn->sec, insn->offset, call_dest_name(insn));
3107 WARN_FUNC("call to %s() with DF set",
3108 insn->sec, insn->offset, call_dest_name(insn));
3115 static int validate_sibling_call(struct objtool_file *file,
3116 struct instruction *insn,
3117 struct insn_state *state)
3119 if (has_modified_stack_frame(insn, state)) {
3120 WARN_FUNC("sibling call from callable instruction with modified stack frame",
3121 insn->sec, insn->offset);
3125 return validate_call(file, insn, state);
3128 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3130 if (state->noinstr && state->instr > 0) {
3131 WARN_FUNC("return with instrumentation enabled",
3132 insn->sec, insn->offset);
3136 if (state->uaccess && !func_uaccess_safe(func)) {
3137 WARN_FUNC("return with UACCESS enabled",
3138 insn->sec, insn->offset);
3142 if (!state->uaccess && func_uaccess_safe(func)) {
3143 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
3144 insn->sec, insn->offset);
3149 WARN_FUNC("return with DF set",
3150 insn->sec, insn->offset);
3154 if (func && has_modified_stack_frame(insn, state)) {
3155 WARN_FUNC("return with modified stack frame",
3156 insn->sec, insn->offset);
3160 if (state->cfi.bp_scratch) {
3161 WARN_FUNC("BP used as a scratch register",
3162 insn->sec, insn->offset);
3169 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3170 struct instruction *insn)
3172 struct alt_group *alt_group = insn->alt_group;
3175 * Simulate the fact that alternatives are patched in-place. When the
3176 * end of a replacement alt_group is reached, redirect objtool flow to
3177 * the end of the original alt_group.
3179 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3180 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3182 return next_insn_same_sec(file, insn);
3186 * Follow the branch starting at the given instruction, and recursively follow
3187 * any other branches (jumps). Meanwhile, track the frame pointer state at
3188 * each instruction and validate all the rules described in
3189 * tools/objtool/Documentation/stack-validation.txt.
3191 static int validate_branch(struct objtool_file *file, struct symbol *func,
3192 struct instruction *insn, struct insn_state state)
3194 struct alternative *alt;
3195 struct instruction *next_insn, *prev_insn = NULL;
3196 struct section *sec;
3203 next_insn = next_insn_to_validate(file, insn);
3205 if (func && insn->func && func != insn->func->pfunc) {
3206 WARN("%s() falls through to next function %s()",
3207 func->name, insn->func->name);
3211 if (func && insn->ignore) {
3212 WARN_FUNC("BUG: why am I validating an ignored function?",
3217 visited = 1 << state.uaccess;
3218 if (insn->visited) {
3219 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3222 if (insn->visited & visited)
3229 state.instr += insn->instr;
3232 state.cfi = *insn->cfi;
3234 /* XXX track if we actually changed state.cfi */
3236 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3237 insn->cfi = prev_insn->cfi;
3240 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3244 insn->visited |= visited;
3246 if (propagate_alt_cfi(file, insn))
3249 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3250 bool skip_orig = false;
3252 list_for_each_entry(alt, &insn->alts, list) {
3256 ret = validate_branch(file, func, alt->insn, state);
3259 BT_FUNC("(alt)", insn);
3268 if (handle_insn_ops(insn, next_insn, &state))
3271 switch (insn->type) {
3274 return validate_return(func, insn, &state);
3277 case INSN_CALL_DYNAMIC:
3278 ret = validate_call(file, insn, &state);
3282 if (!opts.no_fp && func && !is_fentry_call(insn) &&
3283 !has_valid_stack_frame(&state)) {
3284 WARN_FUNC("call without frame pointer save/setup",
3294 case INSN_JUMP_CONDITIONAL:
3295 case INSN_JUMP_UNCONDITIONAL:
3296 if (is_sibling_call(insn)) {
3297 ret = validate_sibling_call(file, insn, &state);
3301 } else if (insn->jump_dest) {
3302 ret = validate_branch(file, func,
3303 insn->jump_dest, state);
3306 BT_FUNC("(branch)", insn);
3311 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3316 case INSN_JUMP_DYNAMIC:
3317 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3318 if (is_sibling_call(insn)) {
3319 ret = validate_sibling_call(file, insn, &state);
3324 if (insn->type == INSN_JUMP_DYNAMIC)
3329 case INSN_CONTEXT_SWITCH:
3330 if (func && (!next_insn || !next_insn->hint)) {
3331 WARN_FUNC("unsupported instruction in callable function",
3338 if (state.uaccess) {
3339 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3343 state.uaccess = true;
3347 if (!state.uaccess && func) {
3348 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3352 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3353 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3357 state.uaccess = false;
3362 WARN_FUNC("recursive STD", sec, insn->offset);
3370 if (!state.df && func) {
3371 WARN_FUNC("redundant CLD", sec, insn->offset);
3386 if (state.cfi.cfa.base == CFI_UNDEFINED)
3388 WARN("%s: unexpected end of section", sec->name);
3399 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3401 struct instruction *insn;
3402 struct insn_state state;
3403 int ret, warnings = 0;
3408 init_insn_state(&state, sec);
3411 insn = find_insn(file, sec, 0);
3415 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3418 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3419 if (insn->hint && !insn->visited && !insn->ignore) {
3420 ret = validate_branch(file, insn->func, insn, state);
3421 if (ret && opts.backtrace)
3422 BT_FUNC("<=== (hint)", insn);
3426 insn = list_next_entry(insn, list);
3432 static int validate_retpoline(struct objtool_file *file)
3434 struct instruction *insn;
3437 for_each_insn(file, insn) {
3438 if (insn->type != INSN_JUMP_DYNAMIC &&
3439 insn->type != INSN_CALL_DYNAMIC)
3442 if (insn->retpoline_safe)
3446 * .init.text code is ran before userspace and thus doesn't
3447 * strictly need retpolines, except for modules which are
3448 * loaded late, they very much do need retpoline in their
3451 if (!strcmp(insn->sec->name, ".init.text") && !opts.module)
3454 WARN_FUNC("indirect %s found in RETPOLINE build",
3455 insn->sec, insn->offset,
3456 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3464 static bool is_kasan_insn(struct instruction *insn)
3466 return (insn->type == INSN_CALL &&
3467 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3470 static bool is_ubsan_insn(struct instruction *insn)
3472 return (insn->type == INSN_CALL &&
3473 !strcmp(insn->call_dest->name,
3474 "__ubsan_handle_builtin_unreachable"));
3477 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3480 struct instruction *prev_insn;
3482 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3486 * Ignore alternative replacement instructions. This can happen
3487 * when a whitelisted function uses one of the ALTERNATIVE macros.
3489 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
3490 !strcmp(insn->sec->name, ".altinstr_aux"))
3494 * Whole archive runs might encounder dead code from weak symbols.
3495 * This is where the linker will have dropped the weak symbol in
3496 * favour of a regular symbol, but leaves the code in place.
3498 * In this case we'll find a piece of code (whole function) that is not
3499 * covered by a !section symbol. Ignore them.
3501 if (!insn->func && opts.lto) {
3502 int size = find_symbol_hole_containing(insn->sec, insn->offset);
3503 unsigned long end = insn->offset + size;
3505 if (!size) /* not a hole */
3508 if (size < 0) /* hole until the end */
3511 sec_for_each_insn_continue(file, insn) {
3513 * If we reach a visited instruction at or before the
3514 * end of the hole, ignore the unreachable.
3519 if (insn->offset >= end)
3523 * If this hole jumps to a .cold function, mark it ignore too.
3525 if (insn->jump_dest && insn->jump_dest->func &&
3526 strstr(insn->jump_dest->func->name, ".cold")) {
3527 struct instruction *dest = insn->jump_dest;
3528 func_for_each_insn(file, dest->func, dest)
3529 dest->ignore = true;
3539 if (insn->func->static_call_tramp)
3543 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3544 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3545 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3546 * (or occasionally a JMP to UD2).
3548 * It may also insert a UD2 after calling a __noreturn function.
3550 prev_insn = list_prev_entry(insn, list);
3551 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3552 (insn->type == INSN_BUG ||
3553 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3554 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3558 * Check if this (or a subsequent) instruction is related to
3559 * CONFIG_UBSAN or CONFIG_KASAN.
3561 * End the search at 5 instructions to avoid going into the weeds.
3563 for (i = 0; i < 5; i++) {
3565 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3568 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3569 if (insn->jump_dest &&
3570 insn->jump_dest->func == insn->func) {
3571 insn = insn->jump_dest;
3578 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3581 insn = list_next_entry(insn, list);
3587 static int validate_symbol(struct objtool_file *file, struct section *sec,
3588 struct symbol *sym, struct insn_state *state)
3590 struct instruction *insn;
3594 WARN("%s() is missing an ELF size annotation", sym->name);
3598 if (sym->pfunc != sym || sym->alias != sym)
3601 insn = find_insn(file, sec, sym->offset);
3602 if (!insn || insn->ignore || insn->visited)
3605 state->uaccess = sym->uaccess_safe;
3607 ret = validate_branch(file, insn->func, insn, *state);
3608 if (ret && opts.backtrace)
3609 BT_FUNC("<=== (sym)", insn);
3613 static int validate_section(struct objtool_file *file, struct section *sec)
3615 struct insn_state state;
3616 struct symbol *func;
3619 list_for_each_entry(func, &sec->symbol_list, list) {
3620 if (func->type != STT_FUNC)
3623 init_insn_state(&state, sec);
3624 set_func_state(&state.cfi);
3626 warnings += validate_symbol(file, sec, func, &state);
3632 static int validate_vmlinux_functions(struct objtool_file *file)
3634 struct section *sec;
3637 sec = find_section_by_name(file->elf, ".noinstr.text");
3639 warnings += validate_section(file, sec);
3640 warnings += validate_unwind_hints(file, sec);
3643 sec = find_section_by_name(file->elf, ".entry.text");
3645 warnings += validate_section(file, sec);
3646 warnings += validate_unwind_hints(file, sec);
3652 static int validate_functions(struct objtool_file *file)
3654 struct section *sec;
3657 for_each_sec(file, sec) {
3658 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3661 warnings += validate_section(file, sec);
3667 static void mark_endbr_used(struct instruction *insn)
3669 if (!list_empty(&insn->call_node))
3670 list_del_init(&insn->call_node);
3673 static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
3675 struct instruction *dest;
3676 struct reloc *reloc;
3681 * Looking for function pointer load relocations. Ignore
3682 * direct/indirect branches:
3684 switch (insn->type) {
3686 case INSN_CALL_DYNAMIC:
3687 case INSN_JUMP_CONDITIONAL:
3688 case INSN_JUMP_UNCONDITIONAL:
3689 case INSN_JUMP_DYNAMIC:
3690 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3698 for (reloc = insn_reloc(file, insn);
3700 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
3702 (insn->offset + insn->len) - (reloc->offset + 1))) {
3705 * static_call_update() references the trampoline, which
3706 * doesn't have (or need) ENDBR. Skip warning in that case.
3708 if (reloc->sym->static_call_tramp)
3711 off = reloc->sym->offset;
3712 if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)
3713 off += arch_dest_reloc_offset(reloc->addend);
3715 off += reloc->addend;
3717 dest = find_insn(file, reloc->sym->sec, off);
3721 if (dest->type == INSN_ENDBR) {
3722 mark_endbr_used(dest);
3726 if (dest->func && dest->func == insn->func) {
3728 * Anything from->to self is either _THIS_IP_ or
3731 * There is no sane way to annotate _THIS_IP_ since the
3732 * compiler treats the relocation as a constant and is
3733 * happy to fold in offsets, skewing any annotation we
3734 * do, leading to vast amounts of false-positives.
3736 * There's also compiler generated _THIS_IP_ through
3737 * KCOV and such which we have no hope of annotating.
3739 * As such, blanket accept self-references without
3748 WARN_FUNC("relocation to !ENDBR: %s",
3749 insn->sec, insn->offset,
3750 offstr(dest->sec, dest->offset));
3758 static int validate_ibt_data_reloc(struct objtool_file *file,
3759 struct reloc *reloc)
3761 struct instruction *dest;
3763 dest = find_insn(file, reloc->sym->sec,
3764 reloc->sym->offset + reloc->addend);
3768 if (dest->type == INSN_ENDBR) {
3769 mark_endbr_used(dest);
3776 WARN_FUNC("data relocation to !ENDBR: %s",
3777 reloc->sec->base, reloc->offset,
3778 offstr(dest->sec, dest->offset));
3784 * Validate IBT rules and remove used ENDBR instructions from the seal list.
3785 * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
3786 * NOPs) later, in create_ibt_endbr_seal_sections().
3788 static int validate_ibt(struct objtool_file *file)
3790 struct section *sec;
3791 struct reloc *reloc;
3792 struct instruction *insn;
3795 for_each_insn(file, insn)
3796 warnings += validate_ibt_insn(file, insn);
3798 for_each_sec(file, sec) {
3800 /* Already done by validate_ibt_insn() */
3801 if (sec->sh.sh_flags & SHF_EXECINSTR)
3808 * These sections can reference text addresses, but not with
3809 * the intent to indirect branch to them.
3811 if (!strncmp(sec->name, ".discard", 8) ||
3812 !strncmp(sec->name, ".debug", 6) ||
3813 !strcmp(sec->name, ".altinstructions") ||
3814 !strcmp(sec->name, ".ibt_endbr_seal") ||
3815 !strcmp(sec->name, ".orc_unwind_ip") ||
3816 !strcmp(sec->name, ".parainstructions") ||
3817 !strcmp(sec->name, ".retpoline_sites") ||
3818 !strcmp(sec->name, ".smp_locks") ||
3819 !strcmp(sec->name, ".static_call_sites") ||
3820 !strcmp(sec->name, "_error_injection_whitelist") ||
3821 !strcmp(sec->name, "_kprobe_blacklist") ||
3822 !strcmp(sec->name, "__bug_table") ||
3823 !strcmp(sec->name, "__ex_table") ||
3824 !strcmp(sec->name, "__jump_table") ||
3825 !strcmp(sec->name, "__mcount_loc") ||
3826 !strcmp(sec->name, "__tracepoints"))
3829 list_for_each_entry(reloc, &sec->reloc->reloc_list, list)
3830 warnings += validate_ibt_data_reloc(file, reloc);
3836 static int validate_sls(struct objtool_file *file)
3838 struct instruction *insn, *next_insn;
3841 for_each_insn(file, insn) {
3842 next_insn = next_insn_same_sec(file, insn);
3844 if (insn->retpoline_safe)
3847 switch (insn->type) {
3849 if (!next_insn || next_insn->type != INSN_TRAP) {
3850 WARN_FUNC("missing int3 after ret",
3851 insn->sec, insn->offset);
3856 case INSN_JUMP_DYNAMIC:
3857 if (!next_insn || next_insn->type != INSN_TRAP) {
3858 WARN_FUNC("missing int3 after indirect jump",
3859 insn->sec, insn->offset);
3871 static int validate_reachable_instructions(struct objtool_file *file)
3873 struct instruction *insn;
3875 if (file->ignore_unreachables)
3878 for_each_insn(file, insn) {
3879 if (insn->visited || ignore_unreachable_insn(file, insn))
3882 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3889 int check(struct objtool_file *file)
3891 int ret, warnings = 0;
3893 if (opts.lto && !(opts.vmlinux || opts.module)) {
3894 fprintf(stderr, "--lto requires: --vmlinux or --module\n");
3898 if (opts.ibt && !opts.lto) {
3899 fprintf(stderr, "--ibt requires: --lto\n");
3903 arch_initial_func_cfi_state(&initial_func_cfi);
3904 init_cfi_state(&init_cfi);
3905 init_cfi_state(&func_cfi);
3906 set_func_state(&func_cfi);
3908 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3911 cfi_hash_add(&init_cfi);
3912 cfi_hash_add(&func_cfi);
3914 ret = decode_sections(file);
3920 if (list_empty(&file->insn_list))
3923 if (opts.vmlinux && !opts.lto) {
3924 ret = validate_vmlinux_functions(file);
3932 if (opts.retpoline) {
3933 ret = validate_retpoline(file);
3939 if (opts.stackval || opts.orc || opts.uaccess) {
3940 ret = validate_functions(file);
3945 ret = validate_unwind_hints(file, NULL);
3951 ret = validate_reachable_instructions(file);
3959 ret = validate_ibt(file);
3966 ret = validate_sls(file);
3972 ret = create_static_call_sections(file);
3977 if (opts.retpoline) {
3978 ret = create_retpoline_sites_sections(file);
3985 ret = create_mcount_loc_sections(file);
3992 ret = create_ibt_endbr_seal_sections(file);
3998 if (opts.orc && !list_empty(&file->insn_list)) {
3999 ret = orc_create(file);
4007 printf("nr_insns_visited: %ld\n", nr_insns_visited);
4008 printf("nr_cfi: %ld\n", nr_cfi);
4009 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4010 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4015 * For now, don't fail the kernel build on fatal warnings. These
4016 * errors are still fairly common due to the growing matrix of
4017 * supported toolchains and their recent pace of change.