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",
192 if (func->bind == STB_WEAK)
195 if (func->bind == STB_GLOBAL)
196 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
197 if (!strcmp(func->name, global_noreturns[i]))
203 insn = find_insn(file, func->sec, func->offset);
207 func_for_each_insn(file, func, insn) {
210 if (insn->type == INSN_RETURN)
218 * A function can have a sibling call instead of a return. In that
219 * case, the function's dead-end status depends on whether the target
220 * of the sibling call returns.
222 func_for_each_insn(file, func, insn) {
223 if (is_sibling_call(insn)) {
224 struct instruction *dest = insn->jump_dest;
227 /* sibling call to another file */
230 /* local sibling call */
231 if (recursion == 5) {
233 * Infinite recursion: two functions have
234 * sibling calls to each other. This is a very
235 * rare case. It means they aren't dead ends.
240 return __dead_end_function(file, dest->func, recursion+1);
247 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
249 return __dead_end_function(file, func, 0);
252 static void init_cfi_state(struct cfi_state *cfi)
256 for (i = 0; i < CFI_NUM_REGS; i++) {
257 cfi->regs[i].base = CFI_UNDEFINED;
258 cfi->vals[i].base = CFI_UNDEFINED;
260 cfi->cfa.base = CFI_UNDEFINED;
261 cfi->drap_reg = CFI_UNDEFINED;
262 cfi->drap_offset = -1;
265 static void init_insn_state(struct insn_state *state, struct section *sec)
267 memset(state, 0, sizeof(*state));
268 init_cfi_state(&state->cfi);
271 * We need the full vmlinux for noinstr validation, otherwise we can
272 * not correctly determine insn->call_dest->sec (external symbols do
273 * not have a section).
275 if (vmlinux && noinstr && sec)
276 state->noinstr = sec->noinstr;
279 static struct cfi_state *cfi_alloc(void)
281 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
283 WARN("calloc failed");
291 static struct hlist_head *cfi_hash;
293 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
295 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
296 (void *)cfi2 + sizeof(cfi2->hash),
297 sizeof(struct cfi_state) - sizeof(struct hlist_node));
300 static inline u32 cfi_key(struct cfi_state *cfi)
302 return jhash((void *)cfi + sizeof(cfi->hash),
303 sizeof(*cfi) - sizeof(cfi->hash), 0);
306 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
308 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
309 struct cfi_state *obj;
311 hlist_for_each_entry(obj, head, hash) {
312 if (!cficmp(cfi, obj)) {
320 hlist_add_head(&obj->hash, head);
325 static void cfi_hash_add(struct cfi_state *cfi)
327 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
329 hlist_add_head(&cfi->hash, head);
332 static void *cfi_hash_alloc(unsigned long size)
334 cfi_bits = max(10, ilog2(size));
335 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
336 PROT_READ|PROT_WRITE,
337 MAP_PRIVATE|MAP_ANON, -1, 0);
338 if (cfi_hash == (void *)-1L) {
339 WARN("mmap fail cfi_hash");
342 printf("cfi_bits: %d\n", cfi_bits);
348 static unsigned long nr_insns;
349 static unsigned long nr_insns_visited;
352 * Call the arch-specific instruction decoder for all the instructions and add
353 * them to the global instruction list.
355 static int decode_instructions(struct objtool_file *file)
359 unsigned long offset;
360 struct instruction *insn;
363 for_each_sec(file, sec) {
365 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
368 if (strcmp(sec->name, ".altinstr_replacement") &&
369 strcmp(sec->name, ".altinstr_aux") &&
370 strncmp(sec->name, ".discard.", 9))
373 if (!strcmp(sec->name, ".noinstr.text") ||
374 !strcmp(sec->name, ".entry.text"))
377 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
378 insn = malloc(sizeof(*insn));
380 WARN("malloc failed");
383 memset(insn, 0, sizeof(*insn));
384 INIT_LIST_HEAD(&insn->alts);
385 INIT_LIST_HEAD(&insn->stack_ops);
386 INIT_LIST_HEAD(&insn->call_node);
389 insn->offset = offset;
391 ret = arch_decode_instruction(file, sec, offset,
392 sec->sh.sh_size - offset,
393 &insn->len, &insn->type,
400 * By default, "ud2" is a dead end unless otherwise
401 * annotated, because GCC 7 inserts it for certain
402 * divide-by-zero cases.
404 if (insn->type == INSN_BUG)
405 insn->dead_end = true;
407 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
408 list_add_tail(&insn->list, &file->insn_list);
412 list_for_each_entry(func, &sec->symbol_list, list) {
413 if (func->type != STT_FUNC || func->alias != func)
416 if (!find_insn(file, sec, func->offset)) {
417 WARN("%s(): can't find starting instruction",
422 sym_for_each_insn(file, func, insn) {
424 if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) {
425 if (insn->offset == insn->func->offset) {
426 list_add_tail(&insn->call_node, &file->endbr_list);
429 file->nr_endbr_int++;
437 printf("nr_insns: %lu\n", nr_insns);
447 * Read the pv_ops[] .data table to find the static initialized values.
449 static int add_pv_ops(struct objtool_file *file, const char *symname)
451 struct symbol *sym, *func;
452 unsigned long off, end;
456 sym = find_symbol_by_name(file->elf, symname);
461 end = off + sym->len;
463 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
468 if (func->type == STT_SECTION)
469 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
471 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
473 objtool_pv_add(file, idx, func);
475 off = rel->offset + 1;
484 * Allocate and initialize file->pv_ops[].
486 static int init_pv_ops(struct objtool_file *file)
488 static const char *pv_ops_tables[] = {
504 sym = find_symbol_by_name(file->elf, "pv_ops");
508 nr = sym->len / sizeof(unsigned long);
509 file->pv_ops = calloc(sizeof(struct pv_state), nr);
513 for (idx = 0; idx < nr; idx++)
514 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
516 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
517 add_pv_ops(file, pv_ops);
522 static struct instruction *find_last_insn(struct objtool_file *file,
525 struct instruction *insn = NULL;
527 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
529 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
530 insn = find_insn(file, sec, offset);
536 * Mark "ud2" instructions and manually annotated dead ends.
538 static int add_dead_ends(struct objtool_file *file)
542 struct instruction *insn;
545 * Check for manually annotated dead ends.
547 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
551 list_for_each_entry(reloc, &sec->reloc_list, list) {
552 if (reloc->sym->type != STT_SECTION) {
553 WARN("unexpected relocation symbol type in %s", sec->name);
556 insn = find_insn(file, reloc->sym->sec, reloc->addend);
558 insn = list_prev_entry(insn, list);
559 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
560 insn = find_last_insn(file, reloc->sym->sec);
562 WARN("can't find unreachable insn at %s+0x%x",
563 reloc->sym->sec->name, reloc->addend);
567 WARN("can't find unreachable insn at %s+0x%x",
568 reloc->sym->sec->name, reloc->addend);
572 insn->dead_end = true;
577 * These manually annotated reachable checks are needed for GCC 4.4,
578 * where the Linux unreachable() macro isn't supported. In that case
579 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
582 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
586 list_for_each_entry(reloc, &sec->reloc_list, list) {
587 if (reloc->sym->type != STT_SECTION) {
588 WARN("unexpected relocation symbol type in %s", sec->name);
591 insn = find_insn(file, reloc->sym->sec, reloc->addend);
593 insn = list_prev_entry(insn, list);
594 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
595 insn = find_last_insn(file, reloc->sym->sec);
597 WARN("can't find reachable insn at %s+0x%x",
598 reloc->sym->sec->name, reloc->addend);
602 WARN("can't find reachable insn at %s+0x%x",
603 reloc->sym->sec->name, reloc->addend);
607 insn->dead_end = false;
613 static int create_static_call_sections(struct objtool_file *file)
616 struct static_call_site *site;
617 struct instruction *insn;
618 struct symbol *key_sym;
619 char *key_name, *tmp;
622 sec = find_section_by_name(file->elf, ".static_call_sites");
624 INIT_LIST_HEAD(&file->static_call_list);
625 WARN("file already has .static_call_sites section, skipping");
629 if (list_empty(&file->static_call_list))
633 list_for_each_entry(insn, &file->static_call_list, call_node)
636 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
637 sizeof(struct static_call_site), idx);
642 list_for_each_entry(insn, &file->static_call_list, call_node) {
644 site = (struct static_call_site *)sec->data->d_buf + idx;
645 memset(site, 0, sizeof(struct static_call_site));
647 /* populate reloc for 'addr' */
648 if (elf_add_reloc_to_insn(file->elf, sec,
649 idx * sizeof(struct static_call_site),
651 insn->sec, insn->offset))
654 /* find key symbol */
655 key_name = strdup(insn->call_dest->name);
660 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
661 STATIC_CALL_TRAMP_PREFIX_LEN)) {
662 WARN("static_call: trampoline name malformed: %s", key_name);
665 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
666 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
668 key_sym = find_symbol_by_name(file->elf, tmp);
671 WARN("static_call: can't find static_call_key symbol: %s", tmp);
676 * For modules(), the key might not be exported, which
677 * means the module can make static calls but isn't
678 * allowed to change them.
680 * In that case we temporarily set the key to be the
681 * trampoline address. This is fixed up in
682 * static_call_add_module().
684 key_sym = insn->call_dest;
688 /* populate reloc for 'key' */
689 if (elf_add_reloc(file->elf, sec,
690 idx * sizeof(struct static_call_site) + 4,
691 R_X86_64_PC32, key_sym,
692 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
701 static int create_retpoline_sites_sections(struct objtool_file *file)
703 struct instruction *insn;
707 sec = find_section_by_name(file->elf, ".retpoline_sites");
709 WARN("file already has .retpoline_sites, skipping");
714 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
720 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
723 WARN("elf_create_section: .retpoline_sites");
728 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
730 int *site = (int *)sec->data->d_buf + idx;
733 if (elf_add_reloc_to_insn(file->elf, sec,
736 insn->sec, insn->offset)) {
737 WARN("elf_add_reloc_to_insn: .retpoline_sites");
747 static int create_ibt_endbr_seal_sections(struct objtool_file *file)
749 struct instruction *insn;
753 sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
755 WARN("file already has .ibt_endbr_seal, skipping");
760 list_for_each_entry(insn, &file->endbr_list, call_node)
764 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
765 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
766 printf("ibt: superfluous ENDBR: %d\n", idx);
772 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0,
775 WARN("elf_create_section: .ibt_endbr_seal");
780 list_for_each_entry(insn, &file->endbr_list, call_node) {
782 int *site = (int *)sec->data->d_buf + idx;
785 if (elf_add_reloc_to_insn(file->elf, sec,
788 insn->sec, insn->offset)) {
789 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal");
799 static int create_mcount_loc_sections(struct objtool_file *file)
803 struct instruction *insn;
806 sec = find_section_by_name(file->elf, "__mcount_loc");
808 INIT_LIST_HEAD(&file->mcount_loc_list);
809 WARN("file already has __mcount_loc section, skipping");
813 if (list_empty(&file->mcount_loc_list))
817 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
820 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
825 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
827 loc = (unsigned long *)sec->data->d_buf + idx;
828 memset(loc, 0, sizeof(unsigned long));
830 if (elf_add_reloc_to_insn(file->elf, sec,
831 idx * sizeof(unsigned long),
833 insn->sec, insn->offset))
843 * Warnings shouldn't be reported for ignored functions.
845 static void add_ignores(struct objtool_file *file)
847 struct instruction *insn;
852 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
856 list_for_each_entry(reloc, &sec->reloc_list, list) {
857 switch (reloc->sym->type) {
863 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
869 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
873 func_for_each_insn(file, func, insn)
879 * This is a whitelist of functions that is allowed to be called with AC set.
880 * The list is meant to be minimal and only contains compiler instrumentation
881 * ABI and a few functions used to implement *_{to,from}_user() functions.
883 * These functions must not directly change AC, but may PUSHF/POPF.
885 static const char *uaccess_safe_builtin[] = {
889 /* KASAN out-of-line */
890 "__asan_loadN_noabort",
891 "__asan_load1_noabort",
892 "__asan_load2_noabort",
893 "__asan_load4_noabort",
894 "__asan_load8_noabort",
895 "__asan_load16_noabort",
896 "__asan_storeN_noabort",
897 "__asan_store1_noabort",
898 "__asan_store2_noabort",
899 "__asan_store4_noabort",
900 "__asan_store8_noabort",
901 "__asan_store16_noabort",
902 "__kasan_check_read",
903 "__kasan_check_write",
905 "__asan_report_load_n_noabort",
906 "__asan_report_load1_noabort",
907 "__asan_report_load2_noabort",
908 "__asan_report_load4_noabort",
909 "__asan_report_load8_noabort",
910 "__asan_report_load16_noabort",
911 "__asan_report_store_n_noabort",
912 "__asan_report_store1_noabort",
913 "__asan_report_store2_noabort",
914 "__asan_report_store4_noabort",
915 "__asan_report_store8_noabort",
916 "__asan_report_store16_noabort",
918 "__kcsan_check_access",
923 "kcsan_found_watchpoint",
924 "kcsan_setup_watchpoint",
925 "kcsan_check_scoped_accesses",
926 "kcsan_disable_current",
927 "kcsan_enable_current_nowarn",
932 "__tsan_write_range",
943 "__tsan_read_write1",
944 "__tsan_read_write2",
945 "__tsan_read_write4",
946 "__tsan_read_write8",
947 "__tsan_read_write16",
948 "__tsan_atomic8_load",
949 "__tsan_atomic16_load",
950 "__tsan_atomic32_load",
951 "__tsan_atomic64_load",
952 "__tsan_atomic8_store",
953 "__tsan_atomic16_store",
954 "__tsan_atomic32_store",
955 "__tsan_atomic64_store",
956 "__tsan_atomic8_exchange",
957 "__tsan_atomic16_exchange",
958 "__tsan_atomic32_exchange",
959 "__tsan_atomic64_exchange",
960 "__tsan_atomic8_fetch_add",
961 "__tsan_atomic16_fetch_add",
962 "__tsan_atomic32_fetch_add",
963 "__tsan_atomic64_fetch_add",
964 "__tsan_atomic8_fetch_sub",
965 "__tsan_atomic16_fetch_sub",
966 "__tsan_atomic32_fetch_sub",
967 "__tsan_atomic64_fetch_sub",
968 "__tsan_atomic8_fetch_and",
969 "__tsan_atomic16_fetch_and",
970 "__tsan_atomic32_fetch_and",
971 "__tsan_atomic64_fetch_and",
972 "__tsan_atomic8_fetch_or",
973 "__tsan_atomic16_fetch_or",
974 "__tsan_atomic32_fetch_or",
975 "__tsan_atomic64_fetch_or",
976 "__tsan_atomic8_fetch_xor",
977 "__tsan_atomic16_fetch_xor",
978 "__tsan_atomic32_fetch_xor",
979 "__tsan_atomic64_fetch_xor",
980 "__tsan_atomic8_fetch_nand",
981 "__tsan_atomic16_fetch_nand",
982 "__tsan_atomic32_fetch_nand",
983 "__tsan_atomic64_fetch_nand",
984 "__tsan_atomic8_compare_exchange_strong",
985 "__tsan_atomic16_compare_exchange_strong",
986 "__tsan_atomic32_compare_exchange_strong",
987 "__tsan_atomic64_compare_exchange_strong",
988 "__tsan_atomic8_compare_exchange_weak",
989 "__tsan_atomic16_compare_exchange_weak",
990 "__tsan_atomic32_compare_exchange_weak",
991 "__tsan_atomic64_compare_exchange_weak",
992 "__tsan_atomic8_compare_exchange_val",
993 "__tsan_atomic16_compare_exchange_val",
994 "__tsan_atomic32_compare_exchange_val",
995 "__tsan_atomic64_compare_exchange_val",
996 "__tsan_atomic_thread_fence",
997 "__tsan_atomic_signal_fence",
1001 "__sanitizer_cov_trace_pc",
1002 "__sanitizer_cov_trace_const_cmp1",
1003 "__sanitizer_cov_trace_const_cmp2",
1004 "__sanitizer_cov_trace_const_cmp4",
1005 "__sanitizer_cov_trace_const_cmp8",
1006 "__sanitizer_cov_trace_cmp1",
1007 "__sanitizer_cov_trace_cmp2",
1008 "__sanitizer_cov_trace_cmp4",
1009 "__sanitizer_cov_trace_cmp8",
1010 "__sanitizer_cov_trace_switch",
1012 "ubsan_type_mismatch_common",
1013 "__ubsan_handle_type_mismatch",
1014 "__ubsan_handle_type_mismatch_v1",
1015 "__ubsan_handle_shift_out_of_bounds",
1017 "csum_partial_copy_generic",
1019 "copy_mc_fragile_handle_tail",
1020 "copy_mc_enhanced_fast_string",
1021 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
1025 static void add_uaccess_safe(struct objtool_file *file)
1027 struct symbol *func;
1033 for (name = uaccess_safe_builtin; *name; name++) {
1034 func = find_symbol_by_name(file->elf, *name);
1038 func->uaccess_safe = true;
1043 * FIXME: For now, just ignore any alternatives which add retpolines. This is
1044 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1045 * But it at least allows objtool to understand the control flow *around* the
1048 static int add_ignore_alternatives(struct objtool_file *file)
1050 struct section *sec;
1051 struct reloc *reloc;
1052 struct instruction *insn;
1054 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
1058 list_for_each_entry(reloc, &sec->reloc_list, list) {
1059 if (reloc->sym->type != STT_SECTION) {
1060 WARN("unexpected relocation symbol type in %s", sec->name);
1064 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1066 WARN("bad .discard.ignore_alts entry");
1070 insn->ignore_alts = true;
1076 __weak bool arch_is_retpoline(struct symbol *sym)
1081 #define NEGATIVE_RELOC ((void *)-1L)
1083 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1085 if (insn->reloc == NEGATIVE_RELOC)
1092 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1093 insn->offset, insn->len);
1095 insn->reloc = NEGATIVE_RELOC;
1103 static void remove_insn_ops(struct instruction *insn)
1105 struct stack_op *op, *tmp;
1107 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1108 list_del(&op->list);
1113 static void annotate_call_site(struct objtool_file *file,
1114 struct instruction *insn, bool sibling)
1116 struct reloc *reloc = insn_reloc(file, insn);
1117 struct symbol *sym = insn->call_dest;
1123 * Alternative replacement code is just template code which is
1124 * sometimes copied to the original instruction. For now, don't
1125 * annotate it. (In the future we might consider annotating the
1126 * original instruction if/when it ever makes sense to do so.)
1128 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1131 if (sym->static_call_tramp) {
1132 list_add_tail(&insn->call_node, &file->static_call_list);
1136 if (sym->retpoline_thunk) {
1137 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1142 * Many compilers cannot disable KCOV or sanitizer calls with a function
1143 * attribute so they need a little help, NOP out any such calls from
1146 if (insn->sec->noinstr && sym->profiling_func) {
1148 reloc->type = R_NONE;
1149 elf_write_reloc(file->elf, reloc);
1152 elf_write_insn(file->elf, insn->sec,
1153 insn->offset, insn->len,
1154 sibling ? arch_ret_insn(insn->len)
1155 : arch_nop_insn(insn->len));
1157 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1161 * We've replaced the tail-call JMP insn by two new
1162 * insn: RET; INT3, except we only have a single struct
1163 * insn here. Mark it retpoline_safe to avoid the SLS
1164 * warning, instead of adding another insn.
1166 insn->retpoline_safe = true;
1172 if (mcount && sym->fentry) {
1174 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1177 reloc->type = R_NONE;
1178 elf_write_reloc(file->elf, reloc);
1181 elf_write_insn(file->elf, insn->sec,
1182 insn->offset, insn->len,
1183 arch_nop_insn(insn->len));
1185 insn->type = INSN_NOP;
1187 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1191 if (!sibling && dead_end_function(file, sym))
1192 insn->dead_end = true;
1195 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1196 struct symbol *dest, bool sibling)
1198 insn->call_dest = dest;
1203 * Whatever stack impact regular CALLs have, should be undone
1204 * by the RETURN of the called function.
1206 * Annotated intra-function calls retain the stack_ops but
1207 * are converted to JUMP, see read_intra_function_calls().
1209 remove_insn_ops(insn);
1211 annotate_call_site(file, insn, sibling);
1214 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1217 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1218 * so convert them accordingly.
1220 switch (insn->type) {
1222 insn->type = INSN_CALL_DYNAMIC;
1224 case INSN_JUMP_UNCONDITIONAL:
1225 insn->type = INSN_JUMP_DYNAMIC;
1227 case INSN_JUMP_CONDITIONAL:
1228 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1234 insn->retpoline_safe = true;
1237 * Whatever stack impact regular CALLs have, should be undone
1238 * by the RETURN of the called function.
1240 * Annotated intra-function calls retain the stack_ops but
1241 * are converted to JUMP, see read_intra_function_calls().
1243 remove_insn_ops(insn);
1245 annotate_call_site(file, insn, false);
1248 static bool same_function(struct instruction *insn1, struct instruction *insn2)
1250 return insn1->func->pfunc == insn2->func->pfunc;
1253 static bool is_first_func_insn(struct objtool_file *file, struct instruction *insn)
1255 if (insn->offset == insn->func->offset)
1259 struct instruction *prev = prev_insn_same_sym(file, insn);
1261 if (prev && prev->type == INSN_ENDBR &&
1262 insn->offset == insn->func->offset + prev->len)
1270 * Find the destination instructions for all jumps.
1272 static int add_jump_destinations(struct objtool_file *file)
1274 struct instruction *insn, *jump_dest;
1275 struct reloc *reloc;
1276 struct section *dest_sec;
1277 unsigned long dest_off;
1279 for_each_insn(file, insn) {
1280 if (insn->jump_dest) {
1282 * handle_group_alt() may have previously set
1283 * 'jump_dest' for some alternatives.
1287 if (!is_static_jump(insn))
1290 reloc = insn_reloc(file, insn);
1292 dest_sec = insn->sec;
1293 dest_off = arch_jump_destination(insn);
1294 } else if (reloc->sym->type == STT_SECTION) {
1295 dest_sec = reloc->sym->sec;
1296 dest_off = arch_dest_reloc_offset(reloc->addend);
1297 } else if (reloc->sym->retpoline_thunk) {
1298 add_retpoline_call(file, insn);
1300 } else if (insn->func) {
1302 * External sibling call or internal sibling call with
1305 add_call_dest(file, insn, reloc->sym, true);
1307 } else if (reloc->sym->sec->idx) {
1308 dest_sec = reloc->sym->sec;
1309 dest_off = reloc->sym->sym.st_value +
1310 arch_dest_reloc_offset(reloc->addend);
1312 /* non-func asm code jumping to another file */
1316 jump_dest = find_insn(file, dest_sec, dest_off);
1318 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1319 insn->sec, insn->offset, dest_sec->name,
1325 * Cross-function jump.
1327 if (insn->func && jump_dest->func &&
1328 insn->func != jump_dest->func) {
1331 * For GCC 8+, create parent/child links for any cold
1332 * subfunctions. This is _mostly_ redundant with a
1333 * similar initialization in read_symbols().
1335 * If a function has aliases, we want the *first* such
1336 * function in the symbol table to be the subfunction's
1337 * parent. In that case we overwrite the
1338 * initialization done in read_symbols().
1340 * However this code can't completely replace the
1341 * read_symbols() code because this doesn't detect the
1342 * case where the parent function's only reference to a
1343 * subfunction is through a jump table.
1345 if (!strstr(insn->func->name, ".cold") &&
1346 strstr(jump_dest->func->name, ".cold")) {
1347 insn->func->cfunc = jump_dest->func;
1348 jump_dest->func->pfunc = insn->func;
1350 } else if (!same_function(insn, jump_dest) &&
1351 is_first_func_insn(file, jump_dest)) {
1353 * Internal sibling call without reloc or with
1354 * STT_SECTION reloc.
1356 add_call_dest(file, insn, jump_dest->func, true);
1361 insn->jump_dest = jump_dest;
1367 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1369 struct symbol *call_dest;
1371 call_dest = find_func_by_offset(sec, offset);
1373 call_dest = find_symbol_by_offset(sec, offset);
1379 * Find the destination instructions for all calls.
1381 static int add_call_destinations(struct objtool_file *file)
1383 struct instruction *insn;
1384 unsigned long dest_off;
1385 struct symbol *dest;
1386 struct reloc *reloc;
1388 for_each_insn(file, insn) {
1389 if (insn->type != INSN_CALL)
1392 reloc = insn_reloc(file, insn);
1394 dest_off = arch_jump_destination(insn);
1395 dest = find_call_destination(insn->sec, dest_off);
1397 add_call_dest(file, insn, dest, false);
1402 if (!insn->call_dest) {
1403 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1407 if (insn->func && insn->call_dest->type != STT_FUNC) {
1408 WARN_FUNC("unsupported call to non-function",
1409 insn->sec, insn->offset);
1413 } else if (reloc->sym->type == STT_SECTION) {
1414 dest_off = arch_dest_reloc_offset(reloc->addend);
1415 dest = find_call_destination(reloc->sym->sec, dest_off);
1417 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1418 insn->sec, insn->offset,
1419 reloc->sym->sec->name,
1424 add_call_dest(file, insn, dest, false);
1426 } else if (reloc->sym->retpoline_thunk) {
1427 add_retpoline_call(file, insn);
1430 add_call_dest(file, insn, reloc->sym, false);
1437 * The .alternatives section requires some extra special care over and above
1438 * other special sections because alternatives are patched in place.
1440 static int handle_group_alt(struct objtool_file *file,
1441 struct special_alt *special_alt,
1442 struct instruction *orig_insn,
1443 struct instruction **new_insn)
1445 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1446 struct alt_group *orig_alt_group, *new_alt_group;
1447 unsigned long dest_off;
1450 orig_alt_group = malloc(sizeof(*orig_alt_group));
1451 if (!orig_alt_group) {
1452 WARN("malloc failed");
1455 orig_alt_group->cfi = calloc(special_alt->orig_len,
1456 sizeof(struct cfi_state *));
1457 if (!orig_alt_group->cfi) {
1458 WARN("calloc failed");
1462 last_orig_insn = NULL;
1464 sec_for_each_insn_from(file, insn) {
1465 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1468 insn->alt_group = orig_alt_group;
1469 last_orig_insn = insn;
1471 orig_alt_group->orig_group = NULL;
1472 orig_alt_group->first_insn = orig_insn;
1473 orig_alt_group->last_insn = last_orig_insn;
1476 new_alt_group = malloc(sizeof(*new_alt_group));
1477 if (!new_alt_group) {
1478 WARN("malloc failed");
1482 if (special_alt->new_len < special_alt->orig_len) {
1484 * Insert a fake nop at the end to make the replacement
1485 * alt_group the same size as the original. This is needed to
1486 * allow propagate_alt_cfi() to do its magic. When the last
1487 * instruction affects the stack, the instruction after it (the
1488 * nop) will propagate the new state to the shared CFI array.
1490 nop = malloc(sizeof(*nop));
1492 WARN("malloc failed");
1495 memset(nop, 0, sizeof(*nop));
1496 INIT_LIST_HEAD(&nop->alts);
1497 INIT_LIST_HEAD(&nop->stack_ops);
1499 nop->sec = special_alt->new_sec;
1500 nop->offset = special_alt->new_off + special_alt->new_len;
1501 nop->len = special_alt->orig_len - special_alt->new_len;
1502 nop->type = INSN_NOP;
1503 nop->func = orig_insn->func;
1504 nop->alt_group = new_alt_group;
1505 nop->ignore = orig_insn->ignore_alts;
1508 if (!special_alt->new_len) {
1514 sec_for_each_insn_from(file, insn) {
1515 struct reloc *alt_reloc;
1517 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1520 last_new_insn = insn;
1522 insn->ignore = orig_insn->ignore_alts;
1523 insn->func = orig_insn->func;
1524 insn->alt_group = new_alt_group;
1527 * Since alternative replacement code is copy/pasted by the
1528 * kernel after applying relocations, generally such code can't
1529 * have relative-address relocation references to outside the
1530 * .altinstr_replacement section, unless the arch's
1531 * alternatives code can adjust the relative offsets
1534 alt_reloc = insn_reloc(file, insn);
1536 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1538 WARN_FUNC("unsupported relocation in alternatives section",
1539 insn->sec, insn->offset);
1543 if (!is_static_jump(insn))
1546 if (!insn->immediate)
1549 dest_off = arch_jump_destination(insn);
1550 if (dest_off == special_alt->new_off + special_alt->new_len) {
1551 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1552 if (!insn->jump_dest) {
1553 WARN_FUNC("can't find alternative jump destination",
1554 insn->sec, insn->offset);
1560 if (!last_new_insn) {
1561 WARN_FUNC("can't find last new alternative instruction",
1562 special_alt->new_sec, special_alt->new_off);
1567 list_add(&nop->list, &last_new_insn->list);
1569 new_alt_group->orig_group = orig_alt_group;
1570 new_alt_group->first_insn = *new_insn;
1571 new_alt_group->last_insn = nop ? : last_new_insn;
1572 new_alt_group->cfi = orig_alt_group->cfi;
1577 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1578 * If the original instruction is a jump, make the alt entry an effective nop
1579 * by just skipping the original instruction.
1581 static int handle_jump_alt(struct objtool_file *file,
1582 struct special_alt *special_alt,
1583 struct instruction *orig_insn,
1584 struct instruction **new_insn)
1586 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1587 orig_insn->type != INSN_NOP) {
1589 WARN_FUNC("unsupported instruction at jump label",
1590 orig_insn->sec, orig_insn->offset);
1594 if (special_alt->key_addend & 2) {
1595 struct reloc *reloc = insn_reloc(file, orig_insn);
1598 reloc->type = R_NONE;
1599 elf_write_reloc(file->elf, reloc);
1601 elf_write_insn(file->elf, orig_insn->sec,
1602 orig_insn->offset, orig_insn->len,
1603 arch_nop_insn(orig_insn->len));
1604 orig_insn->type = INSN_NOP;
1607 if (orig_insn->type == INSN_NOP) {
1608 if (orig_insn->len == 2)
1609 file->jl_nop_short++;
1611 file->jl_nop_long++;
1616 if (orig_insn->len == 2)
1621 *new_insn = list_next_entry(orig_insn, list);
1626 * Read all the special sections which have alternate instructions which can be
1627 * patched in or redirected to at runtime. Each instruction having alternate
1628 * instruction(s) has them added to its insn->alts list, which will be
1629 * traversed in validate_branch().
1631 static int add_special_section_alts(struct objtool_file *file)
1633 struct list_head special_alts;
1634 struct instruction *orig_insn, *new_insn;
1635 struct special_alt *special_alt, *tmp;
1636 struct alternative *alt;
1639 ret = special_get_alts(file->elf, &special_alts);
1643 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1645 orig_insn = find_insn(file, special_alt->orig_sec,
1646 special_alt->orig_off);
1648 WARN_FUNC("special: can't find orig instruction",
1649 special_alt->orig_sec, special_alt->orig_off);
1655 if (!special_alt->group || special_alt->new_len) {
1656 new_insn = find_insn(file, special_alt->new_sec,
1657 special_alt->new_off);
1659 WARN_FUNC("special: can't find new instruction",
1660 special_alt->new_sec,
1661 special_alt->new_off);
1667 if (special_alt->group) {
1668 if (!special_alt->orig_len) {
1669 WARN_FUNC("empty alternative entry",
1670 orig_insn->sec, orig_insn->offset);
1674 ret = handle_group_alt(file, special_alt, orig_insn,
1678 } else if (special_alt->jump_or_nop) {
1679 ret = handle_jump_alt(file, special_alt, orig_insn,
1685 alt = malloc(sizeof(*alt));
1687 WARN("malloc failed");
1692 alt->insn = new_insn;
1693 alt->skip_orig = special_alt->skip_orig;
1694 orig_insn->ignore_alts |= special_alt->skip_alt;
1695 list_add_tail(&alt->list, &orig_insn->alts);
1697 list_del(&special_alt->list);
1702 printf("jl\\\tNOP\tJMP\n");
1703 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1704 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1711 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1712 struct reloc *table)
1714 struct reloc *reloc = table;
1715 struct instruction *dest_insn;
1716 struct alternative *alt;
1717 struct symbol *pfunc = insn->func->pfunc;
1718 unsigned int prev_offset = 0;
1721 * Each @reloc is a switch table relocation which points to the target
1724 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1726 /* Check for the end of the table: */
1727 if (reloc != table && reloc->jump_table_start)
1730 /* Make sure the table entries are consecutive: */
1731 if (prev_offset && reloc->offset != prev_offset + 8)
1734 /* Detect function pointers from contiguous objects: */
1735 if (reloc->sym->sec == pfunc->sec &&
1736 reloc->addend == pfunc->offset)
1739 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1743 /* Make sure the destination is in the same function: */
1744 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1747 alt = malloc(sizeof(*alt));
1749 WARN("malloc failed");
1753 alt->insn = dest_insn;
1754 list_add_tail(&alt->list, &insn->alts);
1755 prev_offset = reloc->offset;
1759 WARN_FUNC("can't find switch jump table",
1760 insn->sec, insn->offset);
1768 * find_jump_table() - Given a dynamic jump, find the switch jump table
1769 * associated with it.
1771 static struct reloc *find_jump_table(struct objtool_file *file,
1772 struct symbol *func,
1773 struct instruction *insn)
1775 struct reloc *table_reloc;
1776 struct instruction *dest_insn, *orig_insn = insn;
1779 * Backward search using the @first_jump_src links, these help avoid
1780 * much of the 'in between' code. Which avoids us getting confused by
1784 insn && insn->func && insn->func->pfunc == func;
1785 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1787 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1790 /* allow small jumps within the range */
1791 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1793 (insn->jump_dest->offset <= insn->offset ||
1794 insn->jump_dest->offset > orig_insn->offset))
1797 table_reloc = arch_find_switch_table(file, insn);
1800 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1801 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1811 * First pass: Mark the head of each jump table so that in the next pass,
1812 * we know when a given jump table ends and the next one starts.
1814 static void mark_func_jump_tables(struct objtool_file *file,
1815 struct symbol *func)
1817 struct instruction *insn, *last = NULL;
1818 struct reloc *reloc;
1820 func_for_each_insn(file, func, insn) {
1825 * Store back-pointers for unconditional forward jumps such
1826 * that find_jump_table() can back-track using those and
1827 * avoid some potentially confusing code.
1829 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1830 insn->offset > last->offset &&
1831 insn->jump_dest->offset > insn->offset &&
1832 !insn->jump_dest->first_jump_src) {
1834 insn->jump_dest->first_jump_src = insn;
1835 last = insn->jump_dest;
1838 if (insn->type != INSN_JUMP_DYNAMIC)
1841 reloc = find_jump_table(file, func, insn);
1843 reloc->jump_table_start = true;
1844 insn->jump_table = reloc;
1849 static int add_func_jump_tables(struct objtool_file *file,
1850 struct symbol *func)
1852 struct instruction *insn;
1855 func_for_each_insn(file, func, insn) {
1856 if (!insn->jump_table)
1859 ret = add_jump_table(file, insn, insn->jump_table);
1868 * For some switch statements, gcc generates a jump table in the .rodata
1869 * section which contains a list of addresses within the function to jump to.
1870 * This finds these jump tables and adds them to the insn->alts lists.
1872 static int add_jump_table_alts(struct objtool_file *file)
1874 struct section *sec;
1875 struct symbol *func;
1881 for_each_sec(file, sec) {
1882 list_for_each_entry(func, &sec->symbol_list, list) {
1883 if (func->type != STT_FUNC)
1886 mark_func_jump_tables(file, func);
1887 ret = add_func_jump_tables(file, func);
1896 static void set_func_state(struct cfi_state *state)
1898 state->cfa = initial_func_cfi.cfa;
1899 memcpy(&state->regs, &initial_func_cfi.regs,
1900 CFI_NUM_REGS * sizeof(struct cfi_reg));
1901 state->stack_size = initial_func_cfi.cfa.offset;
1904 static int read_unwind_hints(struct objtool_file *file)
1906 struct cfi_state cfi = init_cfi;
1907 struct section *sec, *relocsec;
1908 struct unwind_hint *hint;
1909 struct instruction *insn;
1910 struct reloc *reloc;
1913 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1917 relocsec = sec->reloc;
1919 WARN("missing .rela.discard.unwind_hints section");
1923 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1924 WARN("struct unwind_hint size mismatch");
1930 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1931 hint = (struct unwind_hint *)sec->data->d_buf + i;
1933 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1935 WARN("can't find reloc for unwind_hints[%d]", i);
1939 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1941 WARN("can't find insn for unwind_hints[%d]", i);
1947 if (ibt && hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1948 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1950 if (sym && sym->bind == STB_GLOBAL &&
1951 insn->type != INSN_ENDBR && !insn->noendbr) {
1952 WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR",
1953 insn->sec, insn->offset);
1957 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1958 insn->cfi = &func_cfi;
1965 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1966 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1967 insn->sec, insn->offset, hint->sp_reg);
1971 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1972 cfi.type = hint->type;
1973 cfi.end = hint->end;
1975 insn->cfi = cfi_hash_find_or_add(&cfi);
1981 static int read_noendbr_hints(struct objtool_file *file)
1983 struct section *sec;
1984 struct instruction *insn;
1985 struct reloc *reloc;
1987 sec = find_section_by_name(file->elf, ".rela.discard.noendbr");
1991 list_for_each_entry(reloc, &sec->reloc_list, list) {
1992 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
1994 WARN("bad .discard.noendbr entry");
1998 if (insn->type == INSN_ENDBR)
1999 WARN_FUNC("ANNOTATE_NOENDBR on ENDBR", insn->sec, insn->offset);
2007 static int read_retpoline_hints(struct objtool_file *file)
2009 struct section *sec;
2010 struct instruction *insn;
2011 struct reloc *reloc;
2013 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
2017 list_for_each_entry(reloc, &sec->reloc_list, list) {
2018 if (reloc->sym->type != STT_SECTION) {
2019 WARN("unexpected relocation symbol type in %s", sec->name);
2023 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2025 WARN("bad .discard.retpoline_safe entry");
2029 if (insn->type != INSN_JUMP_DYNAMIC &&
2030 insn->type != INSN_CALL_DYNAMIC) {
2031 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
2032 insn->sec, insn->offset);
2036 insn->retpoline_safe = true;
2042 static int read_instr_hints(struct objtool_file *file)
2044 struct section *sec;
2045 struct instruction *insn;
2046 struct reloc *reloc;
2048 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2052 list_for_each_entry(reloc, &sec->reloc_list, list) {
2053 if (reloc->sym->type != STT_SECTION) {
2054 WARN("unexpected relocation symbol type in %s", sec->name);
2058 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2060 WARN("bad .discard.instr_end entry");
2067 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2071 list_for_each_entry(reloc, &sec->reloc_list, list) {
2072 if (reloc->sym->type != STT_SECTION) {
2073 WARN("unexpected relocation symbol type in %s", sec->name);
2077 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2079 WARN("bad .discard.instr_begin entry");
2089 static int read_intra_function_calls(struct objtool_file *file)
2091 struct instruction *insn;
2092 struct section *sec;
2093 struct reloc *reloc;
2095 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2099 list_for_each_entry(reloc, &sec->reloc_list, list) {
2100 unsigned long dest_off;
2102 if (reloc->sym->type != STT_SECTION) {
2103 WARN("unexpected relocation symbol type in %s",
2108 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2110 WARN("bad .discard.intra_function_call entry");
2114 if (insn->type != INSN_CALL) {
2115 WARN_FUNC("intra_function_call not a direct call",
2116 insn->sec, insn->offset);
2121 * Treat intra-function CALLs as JMPs, but with a stack_op.
2122 * See add_call_destinations(), which strips stack_ops from
2125 insn->type = INSN_JUMP_UNCONDITIONAL;
2127 dest_off = insn->offset + insn->len + insn->immediate;
2128 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2129 if (!insn->jump_dest) {
2130 WARN_FUNC("can't find call dest at %s+0x%lx",
2131 insn->sec, insn->offset,
2132 insn->sec->name, dest_off);
2141 * Return true if name matches an instrumentation function, where calls to that
2142 * function from noinstr code can safely be removed, but compilers won't do so.
2144 static bool is_profiling_func(const char *name)
2147 * Many compilers cannot disable KCOV with a function attribute.
2149 if (!strncmp(name, "__sanitizer_cov_", 16))
2153 * Some compilers currently do not remove __tsan_func_entry/exit nor
2154 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2155 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2156 * minimum Clang version is 14.0, this can be removed.
2158 if (!strncmp(name, "__tsan_func_", 12) ||
2159 !strcmp(name, "__tsan_atomic_signal_fence"))
2165 static int classify_symbols(struct objtool_file *file)
2167 struct section *sec;
2168 struct symbol *func;
2170 for_each_sec(file, sec) {
2171 list_for_each_entry(func, &sec->symbol_list, list) {
2172 if (func->bind != STB_GLOBAL)
2175 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2176 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2177 func->static_call_tramp = true;
2179 if (arch_is_retpoline(func))
2180 func->retpoline_thunk = true;
2182 if (!strcmp(func->name, "__fentry__"))
2183 func->fentry = true;
2185 if (is_profiling_func(func->name))
2186 func->profiling_func = true;
2193 static void mark_rodata(struct objtool_file *file)
2195 struct section *sec;
2199 * Search for the following rodata sections, each of which can
2200 * potentially contain jump tables:
2202 * - .rodata: can contain GCC switch tables
2203 * - .rodata.<func>: same, if -fdata-sections is being used
2204 * - .rodata..c_jump_table: contains C annotated jump tables
2206 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2208 for_each_sec(file, sec) {
2209 if (!strncmp(sec->name, ".rodata", 7) &&
2210 !strstr(sec->name, ".str1.")) {
2216 file->rodata = found;
2219 static int decode_sections(struct objtool_file *file)
2225 ret = init_pv_ops(file);
2229 ret = decode_instructions(file);
2234 add_uaccess_safe(file);
2236 ret = add_ignore_alternatives(file);
2241 * Must be before read_unwind_hints() since that needs insn->noendbr.
2243 ret = read_noendbr_hints(file);
2248 * Must be before add_{jump_call}_destination.
2250 ret = classify_symbols(file);
2255 * Must be before add_jump_destinations(), which depends on 'func'
2256 * being set for alternatives, to enable proper sibling call detection.
2258 ret = add_special_section_alts(file);
2262 ret = add_jump_destinations(file);
2267 * Must be before add_call_destination(); it changes INSN_CALL to
2270 ret = read_intra_function_calls(file);
2274 ret = add_call_destinations(file);
2279 * Must be after add_call_destinations() such that it can override
2280 * dead_end_function() marks.
2282 ret = add_dead_ends(file);
2286 ret = add_jump_table_alts(file);
2290 ret = read_unwind_hints(file);
2294 ret = read_retpoline_hints(file);
2298 ret = read_instr_hints(file);
2305 static bool is_fentry_call(struct instruction *insn)
2307 if (insn->type == INSN_CALL &&
2309 insn->call_dest->fentry)
2315 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2317 struct cfi_state *cfi = &state->cfi;
2320 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2323 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2326 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2329 for (i = 0; i < CFI_NUM_REGS; i++) {
2330 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2331 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2338 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2339 int expected_offset)
2341 return reg->base == CFI_CFA &&
2342 reg->offset == expected_offset;
2345 static bool has_valid_stack_frame(struct insn_state *state)
2347 struct cfi_state *cfi = &state->cfi;
2349 if (cfi->cfa.base == CFI_BP &&
2350 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2351 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2354 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2360 static int update_cfi_state_regs(struct instruction *insn,
2361 struct cfi_state *cfi,
2362 struct stack_op *op)
2364 struct cfi_reg *cfa = &cfi->cfa;
2366 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2370 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2374 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2377 /* add immediate to sp */
2378 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2379 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2380 cfa->offset -= op->src.offset;
2385 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2387 if (arch_callee_saved_reg(reg) &&
2388 cfi->regs[reg].base == CFI_UNDEFINED) {
2389 cfi->regs[reg].base = base;
2390 cfi->regs[reg].offset = offset;
2394 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2396 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2397 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2401 * A note about DRAP stack alignment:
2403 * GCC has the concept of a DRAP register, which is used to help keep track of
2404 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2405 * register. The typical DRAP pattern is:
2407 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2408 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2409 * 41 ff 72 f8 pushq -0x8(%r10)
2411 * 48 89 e5 mov %rsp,%rbp
2418 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2421 * There are some variations in the epilogues, like:
2429 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2434 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2435 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2436 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2437 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2439 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2442 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2443 * restored beforehand:
2446 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2447 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2449 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2453 static int update_cfi_state(struct instruction *insn,
2454 struct instruction *next_insn,
2455 struct cfi_state *cfi, struct stack_op *op)
2457 struct cfi_reg *cfa = &cfi->cfa;
2458 struct cfi_reg *regs = cfi->regs;
2460 /* stack operations don't make sense with an undefined CFA */
2461 if (cfa->base == CFI_UNDEFINED) {
2463 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2469 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2470 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2471 return update_cfi_state_regs(insn, cfi, op);
2473 switch (op->dest.type) {
2476 switch (op->src.type) {
2479 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2480 cfa->base == CFI_SP &&
2481 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2483 /* mov %rsp, %rbp */
2484 cfa->base = op->dest.reg;
2485 cfi->bp_scratch = false;
2488 else if (op->src.reg == CFI_SP &&
2489 op->dest.reg == CFI_BP && cfi->drap) {
2491 /* drap: mov %rsp, %rbp */
2492 regs[CFI_BP].base = CFI_BP;
2493 regs[CFI_BP].offset = -cfi->stack_size;
2494 cfi->bp_scratch = false;
2497 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2502 * This is needed for the rare case where GCC
2509 cfi->vals[op->dest.reg].base = CFI_CFA;
2510 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2513 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2514 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2519 * Restore the original stack pointer (Clang).
2521 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2524 else if (op->dest.reg == cfa->base) {
2526 /* mov %reg, %rsp */
2527 if (cfa->base == CFI_SP &&
2528 cfi->vals[op->src.reg].base == CFI_CFA) {
2531 * This is needed for the rare case
2532 * where GCC does something dumb like:
2534 * lea 0x8(%rsp), %rcx
2538 cfa->offset = -cfi->vals[op->src.reg].offset;
2539 cfi->stack_size = cfa->offset;
2541 } else if (cfa->base == CFI_SP &&
2542 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2543 cfi->vals[op->src.reg].offset == cfa->offset) {
2548 * 1: mov %rsp, (%[tos])
2549 * 2: mov %[tos], %rsp
2555 * 1 - places a pointer to the previous
2556 * stack at the Top-of-Stack of the
2559 * 2 - switches to the new stack.
2561 * 3 - pops the Top-of-Stack to restore
2562 * the original stack.
2564 * Note: we set base to SP_INDIRECT
2565 * here and preserve offset. Therefore
2566 * when the unwinder reaches ToS it
2567 * will dereference SP and then add the
2568 * offset to find the next frame, IOW:
2571 cfa->base = CFI_SP_INDIRECT;
2574 cfa->base = CFI_UNDEFINED;
2579 else if (op->dest.reg == CFI_SP &&
2580 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2581 cfi->vals[op->src.reg].offset == cfa->offset) {
2584 * The same stack swizzle case 2) as above. But
2585 * because we can't change cfa->base, case 3)
2586 * will become a regular POP. Pretend we're a
2587 * PUSH so things don't go unbalanced.
2589 cfi->stack_size += 8;
2596 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2599 cfi->stack_size -= op->src.offset;
2600 if (cfa->base == CFI_SP)
2601 cfa->offset -= op->src.offset;
2605 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2607 /* lea disp(%rbp), %rsp */
2608 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2612 if (!cfi->drap && op->src.reg == CFI_SP &&
2613 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2614 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2616 /* lea disp(%rsp), %rbp */
2618 cfa->offset -= op->src.offset;
2619 cfi->bp_scratch = false;
2623 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2625 /* drap: lea disp(%rsp), %drap */
2626 cfi->drap_reg = op->dest.reg;
2629 * lea disp(%rsp), %reg
2631 * This is needed for the rare case where GCC
2632 * does something dumb like:
2634 * lea 0x8(%rsp), %rcx
2638 cfi->vals[op->dest.reg].base = CFI_CFA;
2639 cfi->vals[op->dest.reg].offset = \
2640 -cfi->stack_size + op->src.offset;
2645 if (cfi->drap && op->dest.reg == CFI_SP &&
2646 op->src.reg == cfi->drap_reg) {
2648 /* drap: lea disp(%drap), %rsp */
2650 cfa->offset = cfi->stack_size = -op->src.offset;
2651 cfi->drap_reg = CFI_UNDEFINED;
2656 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2657 WARN_FUNC("unsupported stack register modification",
2658 insn->sec, insn->offset);
2665 if (op->dest.reg != CFI_SP ||
2666 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2667 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2668 WARN_FUNC("unsupported stack pointer realignment",
2669 insn->sec, insn->offset);
2673 if (cfi->drap_reg != CFI_UNDEFINED) {
2674 /* drap: and imm, %rsp */
2675 cfa->base = cfi->drap_reg;
2676 cfa->offset = cfi->stack_size = 0;
2681 * Older versions of GCC (4.8ish) realign the stack
2682 * without DRAP, with a frame pointer.
2689 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2691 /* pop %rsp; # restore from a stack swizzle */
2696 if (!cfi->drap && op->dest.reg == cfa->base) {
2702 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2703 op->dest.reg == cfi->drap_reg &&
2704 cfi->drap_offset == -cfi->stack_size) {
2706 /* drap: pop %drap */
2707 cfa->base = cfi->drap_reg;
2709 cfi->drap_offset = -1;
2711 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2714 restore_reg(cfi, op->dest.reg);
2717 cfi->stack_size -= 8;
2718 if (cfa->base == CFI_SP)
2723 case OP_SRC_REG_INDIRECT:
2724 if (!cfi->drap && op->dest.reg == cfa->base &&
2725 op->dest.reg == CFI_BP) {
2727 /* mov disp(%rsp), %rbp */
2729 cfa->offset = cfi->stack_size;
2732 if (cfi->drap && op->src.reg == CFI_BP &&
2733 op->src.offset == cfi->drap_offset) {
2735 /* drap: mov disp(%rbp), %drap */
2736 cfa->base = cfi->drap_reg;
2738 cfi->drap_offset = -1;
2741 if (cfi->drap && op->src.reg == CFI_BP &&
2742 op->src.offset == regs[op->dest.reg].offset) {
2744 /* drap: mov disp(%rbp), %reg */
2745 restore_reg(cfi, op->dest.reg);
2747 } else if (op->src.reg == cfa->base &&
2748 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2750 /* mov disp(%rbp), %reg */
2751 /* mov disp(%rsp), %reg */
2752 restore_reg(cfi, op->dest.reg);
2754 } else if (op->src.reg == CFI_SP &&
2755 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2757 /* mov disp(%rsp), %reg */
2758 restore_reg(cfi, op->dest.reg);
2764 WARN_FUNC("unknown stack-related instruction",
2765 insn->sec, insn->offset);
2773 cfi->stack_size += 8;
2774 if (cfa->base == CFI_SP)
2777 if (op->src.type != OP_SRC_REG)
2781 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2783 /* drap: push %drap */
2784 cfa->base = CFI_BP_INDIRECT;
2785 cfa->offset = -cfi->stack_size;
2787 /* save drap so we know when to restore it */
2788 cfi->drap_offset = -cfi->stack_size;
2790 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2792 /* drap: push %rbp */
2793 cfi->stack_size = 0;
2797 /* drap: push %reg */
2798 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2804 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2807 /* detect when asm code uses rbp as a scratch register */
2808 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2809 cfa->base != CFI_BP)
2810 cfi->bp_scratch = true;
2813 case OP_DEST_REG_INDIRECT:
2816 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2818 /* drap: mov %drap, disp(%rbp) */
2819 cfa->base = CFI_BP_INDIRECT;
2820 cfa->offset = op->dest.offset;
2822 /* save drap offset so we know when to restore it */
2823 cfi->drap_offset = op->dest.offset;
2826 /* drap: mov reg, disp(%rbp) */
2827 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2830 } else if (op->dest.reg == cfa->base) {
2832 /* mov reg, disp(%rbp) */
2833 /* mov reg, disp(%rsp) */
2834 save_reg(cfi, op->src.reg, CFI_CFA,
2835 op->dest.offset - cfi->cfa.offset);
2837 } else if (op->dest.reg == CFI_SP) {
2839 /* mov reg, disp(%rsp) */
2840 save_reg(cfi, op->src.reg, CFI_CFA,
2841 op->dest.offset - cfi->stack_size);
2843 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2845 /* mov %rsp, (%reg); # setup a stack swizzle. */
2846 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2847 cfi->vals[op->dest.reg].offset = cfa->offset;
2853 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2854 WARN_FUNC("unknown stack-related memory operation",
2855 insn->sec, insn->offset);
2860 cfi->stack_size -= 8;
2861 if (cfa->base == CFI_SP)
2867 WARN_FUNC("unknown stack-related instruction",
2868 insn->sec, insn->offset);
2876 * The stack layouts of alternatives instructions can sometimes diverge when
2877 * they have stack modifications. That's fine as long as the potential stack
2878 * layouts don't conflict at any given potential instruction boundary.
2880 * Flatten the CFIs of the different alternative code streams (both original
2881 * and replacement) into a single shared CFI array which can be used to detect
2882 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2884 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2886 struct cfi_state **alt_cfi;
2889 if (!insn->alt_group)
2893 WARN("CFI missing");
2897 alt_cfi = insn->alt_group->cfi;
2898 group_off = insn->offset - insn->alt_group->first_insn->offset;
2900 if (!alt_cfi[group_off]) {
2901 alt_cfi[group_off] = insn->cfi;
2903 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2904 WARN_FUNC("stack layout conflict in alternatives",
2905 insn->sec, insn->offset);
2913 static int handle_insn_ops(struct instruction *insn,
2914 struct instruction *next_insn,
2915 struct insn_state *state)
2917 struct stack_op *op;
2919 list_for_each_entry(op, &insn->stack_ops, list) {
2921 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2924 if (!insn->alt_group)
2927 if (op->dest.type == OP_DEST_PUSHF) {
2928 if (!state->uaccess_stack) {
2929 state->uaccess_stack = 1;
2930 } else if (state->uaccess_stack >> 31) {
2931 WARN_FUNC("PUSHF stack exhausted",
2932 insn->sec, insn->offset);
2935 state->uaccess_stack <<= 1;
2936 state->uaccess_stack |= state->uaccess;
2939 if (op->src.type == OP_SRC_POPF) {
2940 if (state->uaccess_stack) {
2941 state->uaccess = state->uaccess_stack & 1;
2942 state->uaccess_stack >>= 1;
2943 if (state->uaccess_stack == 1)
2944 state->uaccess_stack = 0;
2952 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2954 struct cfi_state *cfi1 = insn->cfi;
2958 WARN("CFI missing");
2962 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2964 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2965 insn->sec, insn->offset,
2966 cfi1->cfa.base, cfi1->cfa.offset,
2967 cfi2->cfa.base, cfi2->cfa.offset);
2969 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2970 for (i = 0; i < CFI_NUM_REGS; i++) {
2971 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2972 sizeof(struct cfi_reg)))
2975 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2976 insn->sec, insn->offset,
2977 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2978 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2982 } else if (cfi1->type != cfi2->type) {
2984 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2985 insn->sec, insn->offset, cfi1->type, cfi2->type);
2987 } else if (cfi1->drap != cfi2->drap ||
2988 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2989 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2991 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2992 insn->sec, insn->offset,
2993 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2994 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
3002 static inline bool func_uaccess_safe(struct symbol *func)
3005 return func->uaccess_safe;
3010 static inline const char *call_dest_name(struct instruction *insn)
3012 static char pvname[19];
3016 if (insn->call_dest)
3017 return insn->call_dest->name;
3019 rel = insn_reloc(NULL, insn);
3020 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
3021 idx = (rel->addend / sizeof(void *));
3022 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3029 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3031 struct symbol *target;
3035 rel = insn_reloc(file, insn);
3036 if (!rel || strcmp(rel->sym->name, "pv_ops"))
3039 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
3041 if (file->pv_ops[idx].clean)
3044 file->pv_ops[idx].clean = true;
3046 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3047 if (!target->sec->noinstr) {
3048 WARN("pv_ops[%d]: %s", idx, target->name);
3049 file->pv_ops[idx].clean = false;
3053 return file->pv_ops[idx].clean;
3056 static inline bool noinstr_call_dest(struct objtool_file *file,
3057 struct instruction *insn,
3058 struct symbol *func)
3061 * We can't deal with indirect function calls at present;
3062 * assume they're instrumented.
3066 return pv_call_dest(file, insn);
3072 * If the symbol is from a noinstr section; we good.
3074 if (func->sec->noinstr)
3078 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3079 * something 'BAD' happened. At the risk of taking the machine down,
3080 * let them proceed to get the message out.
3082 if (!strncmp(func->name, "__ubsan_handle_", 15))
3088 static int validate_call(struct objtool_file *file,
3089 struct instruction *insn,
3090 struct insn_state *state)
3092 if (state->noinstr && state->instr <= 0 &&
3093 !noinstr_call_dest(file, insn, insn->call_dest)) {
3094 WARN_FUNC("call to %s() leaves .noinstr.text section",
3095 insn->sec, insn->offset, call_dest_name(insn));
3099 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
3100 WARN_FUNC("call to %s() with UACCESS enabled",
3101 insn->sec, insn->offset, call_dest_name(insn));
3106 WARN_FUNC("call to %s() with DF set",
3107 insn->sec, insn->offset, call_dest_name(insn));
3114 static int validate_sibling_call(struct objtool_file *file,
3115 struct instruction *insn,
3116 struct insn_state *state)
3118 if (has_modified_stack_frame(insn, state)) {
3119 WARN_FUNC("sibling call from callable instruction with modified stack frame",
3120 insn->sec, insn->offset);
3124 return validate_call(file, insn, state);
3127 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3129 if (state->noinstr && state->instr > 0) {
3130 WARN_FUNC("return with instrumentation enabled",
3131 insn->sec, insn->offset);
3135 if (state->uaccess && !func_uaccess_safe(func)) {
3136 WARN_FUNC("return with UACCESS enabled",
3137 insn->sec, insn->offset);
3141 if (!state->uaccess && func_uaccess_safe(func)) {
3142 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
3143 insn->sec, insn->offset);
3148 WARN_FUNC("return with DF set",
3149 insn->sec, insn->offset);
3153 if (func && has_modified_stack_frame(insn, state)) {
3154 WARN_FUNC("return with modified stack frame",
3155 insn->sec, insn->offset);
3159 if (state->cfi.bp_scratch) {
3160 WARN_FUNC("BP used as a scratch register",
3161 insn->sec, insn->offset);
3168 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3169 struct instruction *insn)
3171 struct alt_group *alt_group = insn->alt_group;
3174 * Simulate the fact that alternatives are patched in-place. When the
3175 * end of a replacement alt_group is reached, redirect objtool flow to
3176 * the end of the original alt_group.
3178 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3179 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3181 return next_insn_same_sec(file, insn);
3184 static struct instruction *
3185 validate_ibt_reloc(struct objtool_file *file, struct reloc *reloc)
3187 struct instruction *dest;
3188 struct section *sec;
3191 sec = reloc->sym->sec;
3192 off = reloc->sym->offset;
3194 if ((reloc->sec->base->sh.sh_flags & SHF_EXECINSTR) &&
3195 (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32))
3196 off += arch_dest_reloc_offset(reloc->addend);
3198 off += reloc->addend;
3200 dest = find_insn(file, sec, off);
3204 if (dest->type == INSN_ENDBR) {
3205 if (!list_empty(&dest->call_node))
3206 list_del_init(&dest->call_node);
3211 if (reloc->sym->static_call_tramp)
3217 static void warn_noendbr(const char *msg, struct section *sec, unsigned long offset,
3218 struct instruction *dest)
3220 WARN_FUNC("%srelocation to !ENDBR: %s+0x%lx", sec, offset, msg,
3221 dest->func ? dest->func->name : dest->sec->name,
3222 dest->func ? dest->offset - dest->func->offset : dest->offset);
3225 static void validate_ibt_dest(struct objtool_file *file, struct instruction *insn,
3226 struct instruction *dest)
3228 if (dest->func && dest->func == insn->func) {
3230 * Anything from->to self is either _THIS_IP_ or IRET-to-self.
3232 * There is no sane way to annotate _THIS_IP_ since the compiler treats the
3233 * relocation as a constant and is happy to fold in offsets, skewing any
3234 * annotation we do, leading to vast amounts of false-positives.
3236 * There's also compiler generated _THIS_IP_ through KCOV and
3237 * such which we have no hope of annotating.
3239 * As such, blanket accept self-references without issue.
3247 warn_noendbr("", insn->sec, insn->offset, dest);
3250 static void validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
3252 struct instruction *dest;
3253 struct reloc *reloc;
3255 switch (insn->type) {
3257 case INSN_CALL_DYNAMIC:
3258 case INSN_JUMP_CONDITIONAL:
3259 case INSN_JUMP_UNCONDITIONAL:
3260 case INSN_JUMP_DYNAMIC:
3261 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3264 * We're looking for code references setting up indirect code
3265 * flow. As such, ignore direct code flow and the actual
3272 * handle_group_alt() will create INSN_NOP instruction that
3273 * don't belong to any section, ignore all NOP since they won't
3274 * carry a (useful) relocation anyway.
3282 for (reloc = insn_reloc(file, insn);
3284 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
3286 (insn->offset + insn->len) - (reloc->offset + 1))) {
3287 dest = validate_ibt_reloc(file, reloc);
3289 validate_ibt_dest(file, insn, dest);
3294 * Follow the branch starting at the given instruction, and recursively follow
3295 * any other branches (jumps). Meanwhile, track the frame pointer state at
3296 * each instruction and validate all the rules described in
3297 * tools/objtool/Documentation/stack-validation.txt.
3299 static int validate_branch(struct objtool_file *file, struct symbol *func,
3300 struct instruction *insn, struct insn_state state)
3302 struct alternative *alt;
3303 struct instruction *next_insn, *prev_insn = NULL;
3304 struct section *sec;
3311 next_insn = next_insn_to_validate(file, insn);
3313 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
3314 WARN("%s() falls through to next function %s()",
3315 func->name, insn->func->name);
3319 if (func && insn->ignore) {
3320 WARN_FUNC("BUG: why am I validating an ignored function?",
3325 visited = 1 << state.uaccess;
3326 if (insn->visited) {
3327 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3330 if (insn->visited & visited)
3337 state.instr += insn->instr;
3340 state.cfi = *insn->cfi;
3342 /* XXX track if we actually changed state.cfi */
3344 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3345 insn->cfi = prev_insn->cfi;
3348 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3352 insn->visited |= visited;
3354 if (propagate_alt_cfi(file, insn))
3357 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3358 bool skip_orig = false;
3360 list_for_each_entry(alt, &insn->alts, list) {
3364 ret = validate_branch(file, func, alt->insn, state);
3367 BT_FUNC("(alt)", insn);
3376 if (handle_insn_ops(insn, next_insn, &state))
3379 switch (insn->type) {
3382 if (sls && !insn->retpoline_safe &&
3383 next_insn && next_insn->type != INSN_TRAP) {
3384 WARN_FUNC("missing int3 after ret",
3385 insn->sec, insn->offset);
3387 return validate_return(func, insn, &state);
3390 case INSN_CALL_DYNAMIC:
3391 ret = validate_call(file, insn, &state);
3395 if (!no_fp && func && !is_fentry_call(insn) &&
3396 !has_valid_stack_frame(&state)) {
3397 WARN_FUNC("call without frame pointer save/setup",
3407 case INSN_JUMP_CONDITIONAL:
3408 case INSN_JUMP_UNCONDITIONAL:
3409 if (is_sibling_call(insn)) {
3410 ret = validate_sibling_call(file, insn, &state);
3414 } else if (insn->jump_dest) {
3415 ret = validate_branch(file, func,
3416 insn->jump_dest, state);
3419 BT_FUNC("(branch)", insn);
3424 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3429 case INSN_JUMP_DYNAMIC:
3430 if (sls && !insn->retpoline_safe &&
3431 next_insn && next_insn->type != INSN_TRAP) {
3432 WARN_FUNC("missing int3 after indirect jump",
3433 insn->sec, insn->offset);
3437 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3438 if (is_sibling_call(insn)) {
3439 ret = validate_sibling_call(file, insn, &state);
3444 if (insn->type == INSN_JUMP_DYNAMIC)
3449 case INSN_CONTEXT_SWITCH:
3450 if (func && (!next_insn || !next_insn->hint)) {
3451 WARN_FUNC("unsupported instruction in callable function",
3458 if (state.uaccess) {
3459 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3463 state.uaccess = true;
3467 if (!state.uaccess && func) {
3468 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3472 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3473 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3477 state.uaccess = false;
3482 WARN_FUNC("recursive STD", sec, insn->offset);
3490 if (!state.df && func) {
3491 WARN_FUNC("redundant CLD", sec, insn->offset);
3503 validate_ibt_insn(file, insn);
3509 if (state.cfi.cfa.base == CFI_UNDEFINED)
3511 WARN("%s: unexpected end of section", sec->name);
3522 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3524 struct instruction *insn;
3525 struct insn_state state;
3526 int ret, warnings = 0;
3531 init_insn_state(&state, sec);
3534 insn = find_insn(file, sec, 0);
3538 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3541 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3542 if (insn->hint && !insn->visited && !insn->ignore) {
3543 ret = validate_branch(file, insn->func, insn, state);
3544 if (ret && backtrace)
3545 BT_FUNC("<=== (hint)", insn);
3549 insn = list_next_entry(insn, list);
3555 static int validate_retpoline(struct objtool_file *file)
3557 struct instruction *insn;
3560 for_each_insn(file, insn) {
3561 if (insn->type != INSN_JUMP_DYNAMIC &&
3562 insn->type != INSN_CALL_DYNAMIC)
3565 if (insn->retpoline_safe)
3569 * .init.text code is ran before userspace and thus doesn't
3570 * strictly need retpolines, except for modules which are
3571 * loaded late, they very much do need retpoline in their
3574 if (!strcmp(insn->sec->name, ".init.text") && !module)
3577 WARN_FUNC("indirect %s found in RETPOLINE build",
3578 insn->sec, insn->offset,
3579 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3587 static bool is_kasan_insn(struct instruction *insn)
3589 return (insn->type == INSN_CALL &&
3590 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3593 static bool is_ubsan_insn(struct instruction *insn)
3595 return (insn->type == INSN_CALL &&
3596 !strcmp(insn->call_dest->name,
3597 "__ubsan_handle_builtin_unreachable"));
3600 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3603 struct instruction *prev_insn;
3605 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3609 * Ignore alternative replacement instructions. This can happen
3610 * when a whitelisted function uses one of the ALTERNATIVE macros.
3612 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
3613 !strcmp(insn->sec->name, ".altinstr_aux"))
3617 * Whole archive runs might encounder dead code from weak symbols.
3618 * This is where the linker will have dropped the weak symbol in
3619 * favour of a regular symbol, but leaves the code in place.
3621 * In this case we'll find a piece of code (whole function) that is not
3622 * covered by a !section symbol. Ignore them.
3624 if (!insn->func && lto) {
3625 int size = find_symbol_hole_containing(insn->sec, insn->offset);
3626 unsigned long end = insn->offset + size;
3628 if (!size) /* not a hole */
3631 if (size < 0) /* hole until the end */
3634 sec_for_each_insn_continue(file, insn) {
3636 * If we reach a visited instruction at or before the
3637 * end of the hole, ignore the unreachable.
3642 if (insn->offset >= end)
3646 * If this hole jumps to a .cold function, mark it ignore too.
3648 if (insn->jump_dest && insn->jump_dest->func &&
3649 strstr(insn->jump_dest->func->name, ".cold")) {
3650 struct instruction *dest = insn->jump_dest;
3651 func_for_each_insn(file, dest->func, dest)
3652 dest->ignore = true;
3662 if (insn->func->static_call_tramp)
3666 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3667 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3668 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3669 * (or occasionally a JMP to UD2).
3671 * It may also insert a UD2 after calling a __noreturn function.
3673 prev_insn = list_prev_entry(insn, list);
3674 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3675 (insn->type == INSN_BUG ||
3676 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3677 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3681 * Check if this (or a subsequent) instruction is related to
3682 * CONFIG_UBSAN or CONFIG_KASAN.
3684 * End the search at 5 instructions to avoid going into the weeds.
3686 for (i = 0; i < 5; i++) {
3688 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3691 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3692 if (insn->jump_dest &&
3693 insn->jump_dest->func == insn->func) {
3694 insn = insn->jump_dest;
3701 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3704 insn = list_next_entry(insn, list);
3710 static int validate_symbol(struct objtool_file *file, struct section *sec,
3711 struct symbol *sym, struct insn_state *state)
3713 struct instruction *insn;
3717 WARN("%s() is missing an ELF size annotation", sym->name);
3721 if (sym->pfunc != sym || sym->alias != sym)
3724 insn = find_insn(file, sec, sym->offset);
3725 if (!insn || insn->ignore || insn->visited)
3728 state->uaccess = sym->uaccess_safe;
3730 ret = validate_branch(file, insn->func, insn, *state);
3731 if (ret && backtrace)
3732 BT_FUNC("<=== (sym)", insn);
3736 static int validate_section(struct objtool_file *file, struct section *sec)
3738 struct insn_state state;
3739 struct symbol *func;
3742 list_for_each_entry(func, &sec->symbol_list, list) {
3743 if (func->type != STT_FUNC)
3746 init_insn_state(&state, sec);
3747 set_func_state(&state.cfi);
3749 warnings += validate_symbol(file, sec, func, &state);
3755 static int validate_vmlinux_functions(struct objtool_file *file)
3757 struct section *sec;
3760 sec = find_section_by_name(file->elf, ".noinstr.text");
3762 warnings += validate_section(file, sec);
3763 warnings += validate_unwind_hints(file, sec);
3766 sec = find_section_by_name(file->elf, ".entry.text");
3768 warnings += validate_section(file, sec);
3769 warnings += validate_unwind_hints(file, sec);
3775 static int validate_functions(struct objtool_file *file)
3777 struct section *sec;
3780 for_each_sec(file, sec) {
3781 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3784 warnings += validate_section(file, sec);
3790 static int validate_ibt(struct objtool_file *file)
3792 struct section *sec;
3793 struct reloc *reloc;
3795 for_each_sec(file, sec) {
3798 /* already done in validate_branch() */
3799 if (sec->sh.sh_flags & SHF_EXECINSTR)
3805 if (!strncmp(sec->name, ".orc", 4))
3808 if (!strncmp(sec->name, ".discard", 8))
3811 if (!strncmp(sec->name, ".debug", 6))
3814 if (!strcmp(sec->name, "_error_injection_whitelist"))
3817 if (!strcmp(sec->name, "_kprobe_blacklist"))
3820 is_data = strstr(sec->name, ".data") || strstr(sec->name, ".rodata");
3822 list_for_each_entry(reloc, &sec->reloc->reloc_list, list) {
3823 struct instruction *dest;
3825 dest = validate_ibt_reloc(file, reloc);
3826 if (is_data && dest && !dest->noendbr) {
3827 warn_noendbr("data ", reloc->sym->sec,
3828 reloc->sym->offset + reloc->addend,
3837 static int validate_reachable_instructions(struct objtool_file *file)
3839 struct instruction *insn;
3841 if (file->ignore_unreachables)
3844 for_each_insn(file, insn) {
3845 if (insn->visited || ignore_unreachable_insn(file, insn))
3848 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3855 int check(struct objtool_file *file)
3857 int ret, warnings = 0;
3859 if (lto && !(vmlinux || module)) {
3860 fprintf(stderr, "--lto requires: --vmlinux or --module\n");
3865 fprintf(stderr, "--ibt requires: --lto\n");
3869 arch_initial_func_cfi_state(&initial_func_cfi);
3870 init_cfi_state(&init_cfi);
3871 init_cfi_state(&func_cfi);
3872 set_func_state(&func_cfi);
3874 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3877 cfi_hash_add(&init_cfi);
3878 cfi_hash_add(&func_cfi);
3880 ret = decode_sections(file);
3886 if (list_empty(&file->insn_list))
3889 if (vmlinux && !lto) {
3890 ret = validate_vmlinux_functions(file);
3899 ret = validate_retpoline(file);
3905 ret = validate_functions(file);
3910 ret = validate_unwind_hints(file, NULL);
3916 ret = validate_ibt(file);
3923 ret = validate_reachable_instructions(file);
3929 ret = create_static_call_sections(file);
3935 ret = create_retpoline_sites_sections(file);
3942 ret = create_mcount_loc_sections(file);
3949 ret = create_ibt_endbr_seal_sections(file);
3956 printf("nr_insns_visited: %ld\n", nr_insns_visited);
3957 printf("nr_cfi: %ld\n", nr_cfi);
3958 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
3959 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
3964 * For now, don't fail the kernel build on fatal warnings. These
3965 * errors are still fairly common due to the growing matrix of
3966 * supported toolchains and their recent pace of change.