1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
12 #include <objtool/builtin.h>
13 #include <objtool/cfi.h>
14 #include <objtool/arch.h>
15 #include <objtool/check.h>
16 #include <objtool/special.h>
17 #include <objtool/warn.h>
18 #include <objtool/endianness.h>
20 #include <linux/objtool.h>
21 #include <linux/hashtable.h>
22 #include <linux/kernel.h>
23 #include <linux/static_call_types.h>
26 struct list_head list;
27 struct instruction *insn;
31 static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
33 static struct cfi_init_state initial_func_cfi;
34 static struct cfi_state init_cfi;
35 static struct cfi_state func_cfi;
37 struct instruction *find_insn(struct objtool_file *file,
38 struct section *sec, unsigned long offset)
40 struct instruction *insn;
42 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
43 if (insn->sec == sec && insn->offset == offset)
50 static struct instruction *next_insn_same_sec(struct objtool_file *file,
51 struct instruction *insn)
53 struct instruction *next = list_next_entry(insn, list);
55 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
61 static struct instruction *next_insn_same_func(struct objtool_file *file,
62 struct instruction *insn)
64 struct instruction *next = list_next_entry(insn, list);
65 struct symbol *func = insn->func;
70 if (&next->list != &file->insn_list && next->func == func)
73 /* Check if we're already in the subfunction: */
74 if (func == func->cfunc)
77 /* Move to the subfunction: */
78 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
81 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
82 struct instruction *insn)
84 struct instruction *prev = list_prev_entry(insn, list);
86 if (&prev->list != &file->insn_list && prev->func == insn->func)
92 #define func_for_each_insn(file, func, insn) \
93 for (insn = find_insn(file, func->sec, func->offset); \
95 insn = next_insn_same_func(file, insn))
97 #define sym_for_each_insn(file, sym, insn) \
98 for (insn = find_insn(file, sym->sec, sym->offset); \
99 insn && &insn->list != &file->insn_list && \
100 insn->sec == sym->sec && \
101 insn->offset < sym->offset + sym->len; \
102 insn = list_next_entry(insn, list))
104 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
105 for (insn = list_prev_entry(insn, list); \
106 &insn->list != &file->insn_list && \
107 insn->sec == sym->sec && insn->offset >= sym->offset; \
108 insn = list_prev_entry(insn, list))
110 #define sec_for_each_insn_from(file, insn) \
111 for (; insn; insn = next_insn_same_sec(file, insn))
113 #define sec_for_each_insn_continue(file, insn) \
114 for (insn = next_insn_same_sec(file, insn); insn; \
115 insn = next_insn_same_sec(file, insn))
117 static bool is_jump_table_jump(struct instruction *insn)
119 struct alt_group *alt_group = insn->alt_group;
121 if (insn->jump_table)
124 /* Retpoline alternative for a jump table? */
125 return alt_group && alt_group->orig_group &&
126 alt_group->orig_group->first_insn->jump_table;
129 static bool is_sibling_call(struct instruction *insn)
132 * Assume only ELF functions can make sibling calls. This ensures
133 * sibling call detection consistency between vmlinux.o and individual
139 /* An indirect jump is either a sibling call or a jump to a table. */
140 if (insn->type == INSN_JUMP_DYNAMIC)
141 return !is_jump_table_jump(insn);
143 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
144 return (is_static_jump(insn) && insn->call_dest);
148 * This checks to see if the given function is a "noreturn" function.
150 * For global functions which are outside the scope of this object file, we
151 * have to keep a manual list of them.
153 * For local functions, we have to detect them manually by simply looking for
154 * the lack of a return instruction.
156 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
160 struct instruction *insn;
164 * Unfortunately these have to be hard coded because the noreturn
165 * attribute isn't provided in ELF data.
167 static const char * const global_noreturns[] = {
174 "__module_put_and_kthread_exit",
175 "kthread_complete_and_exit",
180 "machine_real_restart",
181 "rewind_stack_and_make_dead",
182 "kunit_try_catch_throw",
184 "cpu_bringup_and_idle",
189 "__ubsan_handle_builtin_unreachable",
190 "ex_handler_msr_mce",
196 if (func->bind == STB_WEAK)
199 if (func->bind == STB_GLOBAL)
200 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
201 if (!strcmp(func->name, global_noreturns[i]))
207 insn = find_insn(file, func->sec, func->offset);
211 func_for_each_insn(file, func, insn) {
214 if (insn->type == INSN_RETURN)
222 * A function can have a sibling call instead of a return. In that
223 * case, the function's dead-end status depends on whether the target
224 * of the sibling call returns.
226 func_for_each_insn(file, func, insn) {
227 if (is_sibling_call(insn)) {
228 struct instruction *dest = insn->jump_dest;
231 /* sibling call to another file */
234 /* local sibling call */
235 if (recursion == 5) {
237 * Infinite recursion: two functions have
238 * sibling calls to each other. This is a very
239 * rare case. It means they aren't dead ends.
244 return __dead_end_function(file, dest->func, recursion+1);
251 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
253 return __dead_end_function(file, func, 0);
256 static void init_cfi_state(struct cfi_state *cfi)
260 for (i = 0; i < CFI_NUM_REGS; i++) {
261 cfi->regs[i].base = CFI_UNDEFINED;
262 cfi->vals[i].base = CFI_UNDEFINED;
264 cfi->cfa.base = CFI_UNDEFINED;
265 cfi->drap_reg = CFI_UNDEFINED;
266 cfi->drap_offset = -1;
269 static void init_insn_state(struct objtool_file *file, struct insn_state *state,
272 memset(state, 0, sizeof(*state));
273 init_cfi_state(&state->cfi);
276 * We need the full vmlinux for noinstr validation, otherwise we can
277 * not correctly determine insn->call_dest->sec (external symbols do
278 * not have a section).
280 if (opts.link && opts.noinstr && sec)
281 state->noinstr = sec->noinstr;
284 static struct cfi_state *cfi_alloc(void)
286 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
288 WARN("calloc failed");
296 static struct hlist_head *cfi_hash;
298 static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
300 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
301 (void *)cfi2 + sizeof(cfi2->hash),
302 sizeof(struct cfi_state) - sizeof(struct hlist_node));
305 static inline u32 cfi_key(struct cfi_state *cfi)
307 return jhash((void *)cfi + sizeof(cfi->hash),
308 sizeof(*cfi) - sizeof(cfi->hash), 0);
311 static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
313 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
314 struct cfi_state *obj;
316 hlist_for_each_entry(obj, head, hash) {
317 if (!cficmp(cfi, obj)) {
325 hlist_add_head(&obj->hash, head);
330 static void cfi_hash_add(struct cfi_state *cfi)
332 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
334 hlist_add_head(&cfi->hash, head);
337 static void *cfi_hash_alloc(unsigned long size)
339 cfi_bits = max(10, ilog2(size));
340 cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
341 PROT_READ|PROT_WRITE,
342 MAP_PRIVATE|MAP_ANON, -1, 0);
343 if (cfi_hash == (void *)-1L) {
344 WARN("mmap fail cfi_hash");
346 } else if (opts.stats) {
347 printf("cfi_bits: %d\n", cfi_bits);
353 static unsigned long nr_insns;
354 static unsigned long nr_insns_visited;
357 * Call the arch-specific instruction decoder for all the instructions and add
358 * them to the global instruction list.
360 static int decode_instructions(struct objtool_file *file)
364 unsigned long offset;
365 struct instruction *insn;
368 for_each_sec(file, sec) {
370 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
373 if (strcmp(sec->name, ".altinstr_replacement") &&
374 strcmp(sec->name, ".altinstr_aux") &&
375 strncmp(sec->name, ".discard.", 9))
378 if (!strcmp(sec->name, ".noinstr.text") ||
379 !strcmp(sec->name, ".entry.text"))
382 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
383 insn = malloc(sizeof(*insn));
385 WARN("malloc failed");
388 memset(insn, 0, sizeof(*insn));
389 INIT_LIST_HEAD(&insn->alts);
390 INIT_LIST_HEAD(&insn->stack_ops);
391 INIT_LIST_HEAD(&insn->call_node);
394 insn->offset = offset;
396 ret = arch_decode_instruction(file, sec, offset,
397 sec->sh.sh_size - offset,
398 &insn->len, &insn->type,
405 * By default, "ud2" is a dead end unless otherwise
406 * annotated, because GCC 7 inserts it for certain
407 * divide-by-zero cases.
409 if (insn->type == INSN_BUG)
410 insn->dead_end = true;
412 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
413 list_add_tail(&insn->list, &file->insn_list);
417 list_for_each_entry(func, &sec->symbol_list, list) {
418 if (func->type != STT_FUNC || func->alias != func)
421 if (!find_insn(file, sec, func->offset)) {
422 WARN("%s(): can't find starting instruction",
427 sym_for_each_insn(file, func, insn) {
429 if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) {
430 if (insn->offset == insn->func->offset) {
431 list_add_tail(&insn->call_node, &file->endbr_list);
434 file->nr_endbr_int++;
442 printf("nr_insns: %lu\n", nr_insns);
452 * Read the pv_ops[] .data table to find the static initialized values.
454 static int add_pv_ops(struct objtool_file *file, const char *symname)
456 struct symbol *sym, *func;
457 unsigned long off, end;
461 sym = find_symbol_by_name(file->elf, symname);
466 end = off + sym->len;
468 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
473 if (func->type == STT_SECTION)
474 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
476 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
478 objtool_pv_add(file, idx, func);
480 off = rel->offset + 1;
489 * Allocate and initialize file->pv_ops[].
491 static int init_pv_ops(struct objtool_file *file)
493 static const char *pv_ops_tables[] = {
509 sym = find_symbol_by_name(file->elf, "pv_ops");
513 nr = sym->len / sizeof(unsigned long);
514 file->pv_ops = calloc(sizeof(struct pv_state), nr);
518 for (idx = 0; idx < nr; idx++)
519 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
521 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
522 add_pv_ops(file, pv_ops);
527 static struct instruction *find_last_insn(struct objtool_file *file,
530 struct instruction *insn = NULL;
532 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
534 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
535 insn = find_insn(file, sec, offset);
541 * Mark "ud2" instructions and manually annotated dead ends.
543 static int add_dead_ends(struct objtool_file *file)
547 struct instruction *insn;
550 * Check for manually annotated dead ends.
552 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
556 list_for_each_entry(reloc, &sec->reloc_list, list) {
557 if (reloc->sym->type != STT_SECTION) {
558 WARN("unexpected relocation symbol type in %s", sec->name);
561 insn = find_insn(file, reloc->sym->sec, reloc->addend);
563 insn = list_prev_entry(insn, list);
564 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
565 insn = find_last_insn(file, reloc->sym->sec);
567 WARN("can't find unreachable insn at %s+0x%" PRIx64,
568 reloc->sym->sec->name, reloc->addend);
572 WARN("can't find unreachable insn at %s+0x%" PRIx64,
573 reloc->sym->sec->name, reloc->addend);
577 insn->dead_end = true;
582 * These manually annotated reachable checks are needed for GCC 4.4,
583 * where the Linux unreachable() macro isn't supported. In that case
584 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
587 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
591 list_for_each_entry(reloc, &sec->reloc_list, list) {
592 if (reloc->sym->type != STT_SECTION) {
593 WARN("unexpected relocation symbol type in %s", sec->name);
596 insn = find_insn(file, reloc->sym->sec, reloc->addend);
598 insn = list_prev_entry(insn, list);
599 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
600 insn = find_last_insn(file, reloc->sym->sec);
602 WARN("can't find reachable insn at %s+0x%" PRIx64,
603 reloc->sym->sec->name, reloc->addend);
607 WARN("can't find reachable insn at %s+0x%" PRIx64,
608 reloc->sym->sec->name, reloc->addend);
612 insn->dead_end = false;
618 static int create_static_call_sections(struct objtool_file *file)
621 struct static_call_site *site;
622 struct instruction *insn;
623 struct symbol *key_sym;
624 char *key_name, *tmp;
627 sec = find_section_by_name(file->elf, ".static_call_sites");
629 INIT_LIST_HEAD(&file->static_call_list);
630 WARN("file already has .static_call_sites section, skipping");
634 if (list_empty(&file->static_call_list))
638 list_for_each_entry(insn, &file->static_call_list, call_node)
641 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
642 sizeof(struct static_call_site), idx);
647 list_for_each_entry(insn, &file->static_call_list, call_node) {
649 site = (struct static_call_site *)sec->data->d_buf + idx;
650 memset(site, 0, sizeof(struct static_call_site));
652 /* populate reloc for 'addr' */
653 if (elf_add_reloc_to_insn(file->elf, sec,
654 idx * sizeof(struct static_call_site),
656 insn->sec, insn->offset))
659 /* find key symbol */
660 key_name = strdup(insn->call_dest->name);
665 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
666 STATIC_CALL_TRAMP_PREFIX_LEN)) {
667 WARN("static_call: trampoline name malformed: %s", key_name);
670 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
671 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
673 key_sym = find_symbol_by_name(file->elf, tmp);
676 WARN("static_call: can't find static_call_key symbol: %s", tmp);
681 * For modules(), the key might not be exported, which
682 * means the module can make static calls but isn't
683 * allowed to change them.
685 * In that case we temporarily set the key to be the
686 * trampoline address. This is fixed up in
687 * static_call_add_module().
689 key_sym = insn->call_dest;
693 /* populate reloc for 'key' */
694 if (elf_add_reloc(file->elf, sec,
695 idx * sizeof(struct static_call_site) + 4,
696 R_X86_64_PC32, key_sym,
697 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
706 static int create_retpoline_sites_sections(struct objtool_file *file)
708 struct instruction *insn;
712 sec = find_section_by_name(file->elf, ".retpoline_sites");
714 WARN("file already has .retpoline_sites, skipping");
719 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
725 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
728 WARN("elf_create_section: .retpoline_sites");
733 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
735 int *site = (int *)sec->data->d_buf + idx;
738 if (elf_add_reloc_to_insn(file->elf, sec,
741 insn->sec, insn->offset)) {
742 WARN("elf_add_reloc_to_insn: .retpoline_sites");
752 static int create_ibt_endbr_seal_sections(struct objtool_file *file)
754 struct instruction *insn;
758 sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
760 WARN("file already has .ibt_endbr_seal, skipping");
765 list_for_each_entry(insn, &file->endbr_list, call_node)
769 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
770 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
771 printf("ibt: superfluous ENDBR: %d\n", idx);
777 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0,
780 WARN("elf_create_section: .ibt_endbr_seal");
785 list_for_each_entry(insn, &file->endbr_list, call_node) {
787 int *site = (int *)sec->data->d_buf + idx;
790 if (elf_add_reloc_to_insn(file->elf, sec,
793 insn->sec, insn->offset)) {
794 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal");
804 static int create_mcount_loc_sections(struct objtool_file *file)
808 struct instruction *insn;
811 sec = find_section_by_name(file->elf, "__mcount_loc");
813 INIT_LIST_HEAD(&file->mcount_loc_list);
814 WARN("file already has __mcount_loc section, skipping");
818 if (list_empty(&file->mcount_loc_list))
822 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
825 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
830 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
832 loc = (unsigned long *)sec->data->d_buf + idx;
833 memset(loc, 0, sizeof(unsigned long));
835 if (elf_add_reloc_to_insn(file->elf, sec,
836 idx * sizeof(unsigned long),
838 insn->sec, insn->offset))
848 * Warnings shouldn't be reported for ignored functions.
850 static void add_ignores(struct objtool_file *file)
852 struct instruction *insn;
857 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
861 list_for_each_entry(reloc, &sec->reloc_list, list) {
862 switch (reloc->sym->type) {
868 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
874 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
878 func_for_each_insn(file, func, insn)
884 * This is a whitelist of functions that is allowed to be called with AC set.
885 * The list is meant to be minimal and only contains compiler instrumentation
886 * ABI and a few functions used to implement *_{to,from}_user() functions.
888 * These functions must not directly change AC, but may PUSHF/POPF.
890 static const char *uaccess_safe_builtin[] = {
894 /* KASAN out-of-line */
895 "__asan_loadN_noabort",
896 "__asan_load1_noabort",
897 "__asan_load2_noabort",
898 "__asan_load4_noabort",
899 "__asan_load8_noabort",
900 "__asan_load16_noabort",
901 "__asan_storeN_noabort",
902 "__asan_store1_noabort",
903 "__asan_store2_noabort",
904 "__asan_store4_noabort",
905 "__asan_store8_noabort",
906 "__asan_store16_noabort",
907 "__kasan_check_read",
908 "__kasan_check_write",
910 "__asan_report_load_n_noabort",
911 "__asan_report_load1_noabort",
912 "__asan_report_load2_noabort",
913 "__asan_report_load4_noabort",
914 "__asan_report_load8_noabort",
915 "__asan_report_load16_noabort",
916 "__asan_report_store_n_noabort",
917 "__asan_report_store1_noabort",
918 "__asan_report_store2_noabort",
919 "__asan_report_store4_noabort",
920 "__asan_report_store8_noabort",
921 "__asan_report_store16_noabort",
923 "__kcsan_check_access",
928 "kcsan_found_watchpoint",
929 "kcsan_setup_watchpoint",
930 "kcsan_check_scoped_accesses",
931 "kcsan_disable_current",
932 "kcsan_enable_current_nowarn",
937 "__tsan_write_range",
948 "__tsan_read_write1",
949 "__tsan_read_write2",
950 "__tsan_read_write4",
951 "__tsan_read_write8",
952 "__tsan_read_write16",
953 "__tsan_atomic8_load",
954 "__tsan_atomic16_load",
955 "__tsan_atomic32_load",
956 "__tsan_atomic64_load",
957 "__tsan_atomic8_store",
958 "__tsan_atomic16_store",
959 "__tsan_atomic32_store",
960 "__tsan_atomic64_store",
961 "__tsan_atomic8_exchange",
962 "__tsan_atomic16_exchange",
963 "__tsan_atomic32_exchange",
964 "__tsan_atomic64_exchange",
965 "__tsan_atomic8_fetch_add",
966 "__tsan_atomic16_fetch_add",
967 "__tsan_atomic32_fetch_add",
968 "__tsan_atomic64_fetch_add",
969 "__tsan_atomic8_fetch_sub",
970 "__tsan_atomic16_fetch_sub",
971 "__tsan_atomic32_fetch_sub",
972 "__tsan_atomic64_fetch_sub",
973 "__tsan_atomic8_fetch_and",
974 "__tsan_atomic16_fetch_and",
975 "__tsan_atomic32_fetch_and",
976 "__tsan_atomic64_fetch_and",
977 "__tsan_atomic8_fetch_or",
978 "__tsan_atomic16_fetch_or",
979 "__tsan_atomic32_fetch_or",
980 "__tsan_atomic64_fetch_or",
981 "__tsan_atomic8_fetch_xor",
982 "__tsan_atomic16_fetch_xor",
983 "__tsan_atomic32_fetch_xor",
984 "__tsan_atomic64_fetch_xor",
985 "__tsan_atomic8_fetch_nand",
986 "__tsan_atomic16_fetch_nand",
987 "__tsan_atomic32_fetch_nand",
988 "__tsan_atomic64_fetch_nand",
989 "__tsan_atomic8_compare_exchange_strong",
990 "__tsan_atomic16_compare_exchange_strong",
991 "__tsan_atomic32_compare_exchange_strong",
992 "__tsan_atomic64_compare_exchange_strong",
993 "__tsan_atomic8_compare_exchange_weak",
994 "__tsan_atomic16_compare_exchange_weak",
995 "__tsan_atomic32_compare_exchange_weak",
996 "__tsan_atomic64_compare_exchange_weak",
997 "__tsan_atomic8_compare_exchange_val",
998 "__tsan_atomic16_compare_exchange_val",
999 "__tsan_atomic32_compare_exchange_val",
1000 "__tsan_atomic64_compare_exchange_val",
1001 "__tsan_atomic_thread_fence",
1002 "__tsan_atomic_signal_fence",
1006 "__sanitizer_cov_trace_pc",
1007 "__sanitizer_cov_trace_const_cmp1",
1008 "__sanitizer_cov_trace_const_cmp2",
1009 "__sanitizer_cov_trace_const_cmp4",
1010 "__sanitizer_cov_trace_const_cmp8",
1011 "__sanitizer_cov_trace_cmp1",
1012 "__sanitizer_cov_trace_cmp2",
1013 "__sanitizer_cov_trace_cmp4",
1014 "__sanitizer_cov_trace_cmp8",
1015 "__sanitizer_cov_trace_switch",
1017 "ubsan_type_mismatch_common",
1018 "__ubsan_handle_type_mismatch",
1019 "__ubsan_handle_type_mismatch_v1",
1020 "__ubsan_handle_shift_out_of_bounds",
1022 "csum_partial_copy_generic",
1024 "copy_mc_fragile_handle_tail",
1025 "copy_mc_enhanced_fast_string",
1026 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
1030 static void add_uaccess_safe(struct objtool_file *file)
1032 struct symbol *func;
1038 for (name = uaccess_safe_builtin; *name; name++) {
1039 func = find_symbol_by_name(file->elf, *name);
1043 func->uaccess_safe = true;
1048 * FIXME: For now, just ignore any alternatives which add retpolines. This is
1049 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1050 * But it at least allows objtool to understand the control flow *around* the
1053 static int add_ignore_alternatives(struct objtool_file *file)
1055 struct section *sec;
1056 struct reloc *reloc;
1057 struct instruction *insn;
1059 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
1063 list_for_each_entry(reloc, &sec->reloc_list, list) {
1064 if (reloc->sym->type != STT_SECTION) {
1065 WARN("unexpected relocation symbol type in %s", sec->name);
1069 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1071 WARN("bad .discard.ignore_alts entry");
1075 insn->ignore_alts = true;
1081 __weak bool arch_is_retpoline(struct symbol *sym)
1086 #define NEGATIVE_RELOC ((void *)-1L)
1088 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1090 if (insn->reloc == NEGATIVE_RELOC)
1097 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1098 insn->offset, insn->len);
1100 insn->reloc = NEGATIVE_RELOC;
1108 static void remove_insn_ops(struct instruction *insn)
1110 struct stack_op *op, *tmp;
1112 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1113 list_del(&op->list);
1118 static void annotate_call_site(struct objtool_file *file,
1119 struct instruction *insn, bool sibling)
1121 struct reloc *reloc = insn_reloc(file, insn);
1122 struct symbol *sym = insn->call_dest;
1128 * Alternative replacement code is just template code which is
1129 * sometimes copied to the original instruction. For now, don't
1130 * annotate it. (In the future we might consider annotating the
1131 * original instruction if/when it ever makes sense to do so.)
1133 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1136 if (sym->static_call_tramp) {
1137 list_add_tail(&insn->call_node, &file->static_call_list);
1141 if (sym->retpoline_thunk) {
1142 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1147 * Many compilers cannot disable KCOV or sanitizer calls with a function
1148 * attribute so they need a little help, NOP out any such calls from
1151 if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
1153 reloc->type = R_NONE;
1154 elf_write_reloc(file->elf, reloc);
1157 elf_write_insn(file->elf, insn->sec,
1158 insn->offset, insn->len,
1159 sibling ? arch_ret_insn(insn->len)
1160 : arch_nop_insn(insn->len));
1162 insn->type = sibling ? INSN_RETURN : INSN_NOP;
1166 * We've replaced the tail-call JMP insn by two new
1167 * insn: RET; INT3, except we only have a single struct
1168 * insn here. Mark it retpoline_safe to avoid the SLS
1169 * warning, instead of adding another insn.
1171 insn->retpoline_safe = true;
1177 if (opts.mcount && sym->fentry) {
1179 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1182 reloc->type = R_NONE;
1183 elf_write_reloc(file->elf, reloc);
1186 elf_write_insn(file->elf, insn->sec,
1187 insn->offset, insn->len,
1188 arch_nop_insn(insn->len));
1190 insn->type = INSN_NOP;
1192 list_add_tail(&insn->call_node, &file->mcount_loc_list);
1196 if (!sibling && dead_end_function(file, sym))
1197 insn->dead_end = true;
1200 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1201 struct symbol *dest, bool sibling)
1203 insn->call_dest = dest;
1208 * Whatever stack impact regular CALLs have, should be undone
1209 * by the RETURN of the called function.
1211 * Annotated intra-function calls retain the stack_ops but
1212 * are converted to JUMP, see read_intra_function_calls().
1214 remove_insn_ops(insn);
1216 annotate_call_site(file, insn, sibling);
1219 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1222 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1223 * so convert them accordingly.
1225 switch (insn->type) {
1227 insn->type = INSN_CALL_DYNAMIC;
1229 case INSN_JUMP_UNCONDITIONAL:
1230 insn->type = INSN_JUMP_DYNAMIC;
1232 case INSN_JUMP_CONDITIONAL:
1233 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1239 insn->retpoline_safe = true;
1242 * Whatever stack impact regular CALLs have, should be undone
1243 * by the RETURN of the called function.
1245 * Annotated intra-function calls retain the stack_ops but
1246 * are converted to JUMP, see read_intra_function_calls().
1248 remove_insn_ops(insn);
1250 annotate_call_site(file, insn, false);
1253 static bool same_function(struct instruction *insn1, struct instruction *insn2)
1255 return insn1->func->pfunc == insn2->func->pfunc;
1258 static bool is_first_func_insn(struct objtool_file *file, struct instruction *insn)
1260 if (insn->offset == insn->func->offset)
1264 struct instruction *prev = prev_insn_same_sym(file, insn);
1266 if (prev && prev->type == INSN_ENDBR &&
1267 insn->offset == insn->func->offset + prev->len)
1275 * Find the destination instructions for all jumps.
1277 static int add_jump_destinations(struct objtool_file *file)
1279 struct instruction *insn, *jump_dest;
1280 struct reloc *reloc;
1281 struct section *dest_sec;
1282 unsigned long dest_off;
1284 for_each_insn(file, insn) {
1285 if (insn->jump_dest) {
1287 * handle_group_alt() may have previously set
1288 * 'jump_dest' for some alternatives.
1292 if (!is_static_jump(insn))
1295 reloc = insn_reloc(file, insn);
1297 dest_sec = insn->sec;
1298 dest_off = arch_jump_destination(insn);
1299 } else if (reloc->sym->type == STT_SECTION) {
1300 dest_sec = reloc->sym->sec;
1301 dest_off = arch_dest_reloc_offset(reloc->addend);
1302 } else if (reloc->sym->retpoline_thunk) {
1303 add_retpoline_call(file, insn);
1305 } else if (insn->func) {
1307 * External sibling call or internal sibling call with
1310 add_call_dest(file, insn, reloc->sym, true);
1312 } else if (reloc->sym->sec->idx) {
1313 dest_sec = reloc->sym->sec;
1314 dest_off = reloc->sym->sym.st_value +
1315 arch_dest_reloc_offset(reloc->addend);
1317 /* non-func asm code jumping to another file */
1321 jump_dest = find_insn(file, dest_sec, dest_off);
1323 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1324 insn->sec, insn->offset, dest_sec->name,
1330 * Cross-function jump.
1332 if (insn->func && jump_dest->func &&
1333 insn->func != jump_dest->func) {
1336 * For GCC 8+, create parent/child links for any cold
1337 * subfunctions. This is _mostly_ redundant with a
1338 * similar initialization in read_symbols().
1340 * If a function has aliases, we want the *first* such
1341 * function in the symbol table to be the subfunction's
1342 * parent. In that case we overwrite the
1343 * initialization done in read_symbols().
1345 * However this code can't completely replace the
1346 * read_symbols() code because this doesn't detect the
1347 * case where the parent function's only reference to a
1348 * subfunction is through a jump table.
1350 if (!strstr(insn->func->name, ".cold") &&
1351 strstr(jump_dest->func->name, ".cold")) {
1352 insn->func->cfunc = jump_dest->func;
1353 jump_dest->func->pfunc = insn->func;
1355 } else if (!same_function(insn, jump_dest) &&
1356 is_first_func_insn(file, jump_dest)) {
1358 * Internal sibling call without reloc or with
1359 * STT_SECTION reloc.
1361 add_call_dest(file, insn, jump_dest->func, true);
1366 insn->jump_dest = jump_dest;
1372 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1374 struct symbol *call_dest;
1376 call_dest = find_func_by_offset(sec, offset);
1378 call_dest = find_symbol_by_offset(sec, offset);
1384 * Find the destination instructions for all calls.
1386 static int add_call_destinations(struct objtool_file *file)
1388 struct instruction *insn;
1389 unsigned long dest_off;
1390 struct symbol *dest;
1391 struct reloc *reloc;
1393 for_each_insn(file, insn) {
1394 if (insn->type != INSN_CALL)
1397 reloc = insn_reloc(file, insn);
1399 dest_off = arch_jump_destination(insn);
1400 dest = find_call_destination(insn->sec, dest_off);
1402 add_call_dest(file, insn, dest, false);
1407 if (!insn->call_dest) {
1408 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1412 if (insn->func && insn->call_dest->type != STT_FUNC) {
1413 WARN_FUNC("unsupported call to non-function",
1414 insn->sec, insn->offset);
1418 } else if (reloc->sym->type == STT_SECTION) {
1419 dest_off = arch_dest_reloc_offset(reloc->addend);
1420 dest = find_call_destination(reloc->sym->sec, dest_off);
1422 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1423 insn->sec, insn->offset,
1424 reloc->sym->sec->name,
1429 add_call_dest(file, insn, dest, false);
1431 } else if (reloc->sym->retpoline_thunk) {
1432 add_retpoline_call(file, insn);
1435 add_call_dest(file, insn, reloc->sym, false);
1442 * The .alternatives section requires some extra special care over and above
1443 * other special sections because alternatives are patched in place.
1445 static int handle_group_alt(struct objtool_file *file,
1446 struct special_alt *special_alt,
1447 struct instruction *orig_insn,
1448 struct instruction **new_insn)
1450 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1451 struct alt_group *orig_alt_group, *new_alt_group;
1452 unsigned long dest_off;
1455 orig_alt_group = malloc(sizeof(*orig_alt_group));
1456 if (!orig_alt_group) {
1457 WARN("malloc failed");
1460 orig_alt_group->cfi = calloc(special_alt->orig_len,
1461 sizeof(struct cfi_state *));
1462 if (!orig_alt_group->cfi) {
1463 WARN("calloc failed");
1467 last_orig_insn = NULL;
1469 sec_for_each_insn_from(file, insn) {
1470 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1473 insn->alt_group = orig_alt_group;
1474 last_orig_insn = insn;
1476 orig_alt_group->orig_group = NULL;
1477 orig_alt_group->first_insn = orig_insn;
1478 orig_alt_group->last_insn = last_orig_insn;
1481 new_alt_group = malloc(sizeof(*new_alt_group));
1482 if (!new_alt_group) {
1483 WARN("malloc failed");
1487 if (special_alt->new_len < special_alt->orig_len) {
1489 * Insert a fake nop at the end to make the replacement
1490 * alt_group the same size as the original. This is needed to
1491 * allow propagate_alt_cfi() to do its magic. When the last
1492 * instruction affects the stack, the instruction after it (the
1493 * nop) will propagate the new state to the shared CFI array.
1495 nop = malloc(sizeof(*nop));
1497 WARN("malloc failed");
1500 memset(nop, 0, sizeof(*nop));
1501 INIT_LIST_HEAD(&nop->alts);
1502 INIT_LIST_HEAD(&nop->stack_ops);
1504 nop->sec = special_alt->new_sec;
1505 nop->offset = special_alt->new_off + special_alt->new_len;
1506 nop->len = special_alt->orig_len - special_alt->new_len;
1507 nop->type = INSN_NOP;
1508 nop->func = orig_insn->func;
1509 nop->alt_group = new_alt_group;
1510 nop->ignore = orig_insn->ignore_alts;
1513 if (!special_alt->new_len) {
1519 sec_for_each_insn_from(file, insn) {
1520 struct reloc *alt_reloc;
1522 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1525 last_new_insn = insn;
1527 insn->ignore = orig_insn->ignore_alts;
1528 insn->func = orig_insn->func;
1529 insn->alt_group = new_alt_group;
1532 * Since alternative replacement code is copy/pasted by the
1533 * kernel after applying relocations, generally such code can't
1534 * have relative-address relocation references to outside the
1535 * .altinstr_replacement section, unless the arch's
1536 * alternatives code can adjust the relative offsets
1539 alt_reloc = insn_reloc(file, insn);
1541 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1543 WARN_FUNC("unsupported relocation in alternatives section",
1544 insn->sec, insn->offset);
1548 if (!is_static_jump(insn))
1551 if (!insn->immediate)
1554 dest_off = arch_jump_destination(insn);
1555 if (dest_off == special_alt->new_off + special_alt->new_len) {
1556 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1557 if (!insn->jump_dest) {
1558 WARN_FUNC("can't find alternative jump destination",
1559 insn->sec, insn->offset);
1565 if (!last_new_insn) {
1566 WARN_FUNC("can't find last new alternative instruction",
1567 special_alt->new_sec, special_alt->new_off);
1572 list_add(&nop->list, &last_new_insn->list);
1574 new_alt_group->orig_group = orig_alt_group;
1575 new_alt_group->first_insn = *new_insn;
1576 new_alt_group->last_insn = nop ? : last_new_insn;
1577 new_alt_group->cfi = orig_alt_group->cfi;
1582 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1583 * If the original instruction is a jump, make the alt entry an effective nop
1584 * by just skipping the original instruction.
1586 static int handle_jump_alt(struct objtool_file *file,
1587 struct special_alt *special_alt,
1588 struct instruction *orig_insn,
1589 struct instruction **new_insn)
1591 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1592 orig_insn->type != INSN_NOP) {
1594 WARN_FUNC("unsupported instruction at jump label",
1595 orig_insn->sec, orig_insn->offset);
1599 if (opts.hack_jump_label && special_alt->key_addend & 2) {
1600 struct reloc *reloc = insn_reloc(file, orig_insn);
1603 reloc->type = R_NONE;
1604 elf_write_reloc(file->elf, reloc);
1606 elf_write_insn(file->elf, orig_insn->sec,
1607 orig_insn->offset, orig_insn->len,
1608 arch_nop_insn(orig_insn->len));
1609 orig_insn->type = INSN_NOP;
1612 if (orig_insn->type == INSN_NOP) {
1613 if (orig_insn->len == 2)
1614 file->jl_nop_short++;
1616 file->jl_nop_long++;
1621 if (orig_insn->len == 2)
1626 *new_insn = list_next_entry(orig_insn, list);
1631 * Read all the special sections which have alternate instructions which can be
1632 * patched in or redirected to at runtime. Each instruction having alternate
1633 * instruction(s) has them added to its insn->alts list, which will be
1634 * traversed in validate_branch().
1636 static int add_special_section_alts(struct objtool_file *file)
1638 struct list_head special_alts;
1639 struct instruction *orig_insn, *new_insn;
1640 struct special_alt *special_alt, *tmp;
1641 struct alternative *alt;
1644 ret = special_get_alts(file->elf, &special_alts);
1648 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1650 orig_insn = find_insn(file, special_alt->orig_sec,
1651 special_alt->orig_off);
1653 WARN_FUNC("special: can't find orig instruction",
1654 special_alt->orig_sec, special_alt->orig_off);
1660 if (!special_alt->group || special_alt->new_len) {
1661 new_insn = find_insn(file, special_alt->new_sec,
1662 special_alt->new_off);
1664 WARN_FUNC("special: can't find new instruction",
1665 special_alt->new_sec,
1666 special_alt->new_off);
1672 if (special_alt->group) {
1673 if (!special_alt->orig_len) {
1674 WARN_FUNC("empty alternative entry",
1675 orig_insn->sec, orig_insn->offset);
1679 ret = handle_group_alt(file, special_alt, orig_insn,
1683 } else if (special_alt->jump_or_nop) {
1684 ret = handle_jump_alt(file, special_alt, orig_insn,
1690 alt = malloc(sizeof(*alt));
1692 WARN("malloc failed");
1697 alt->insn = new_insn;
1698 alt->skip_orig = special_alt->skip_orig;
1699 orig_insn->ignore_alts |= special_alt->skip_alt;
1700 list_add_tail(&alt->list, &orig_insn->alts);
1702 list_del(&special_alt->list);
1707 printf("jl\\\tNOP\tJMP\n");
1708 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1709 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1716 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1717 struct reloc *table)
1719 struct reloc *reloc = table;
1720 struct instruction *dest_insn;
1721 struct alternative *alt;
1722 struct symbol *pfunc = insn->func->pfunc;
1723 unsigned int prev_offset = 0;
1726 * Each @reloc is a switch table relocation which points to the target
1729 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1731 /* Check for the end of the table: */
1732 if (reloc != table && reloc->jump_table_start)
1735 /* Make sure the table entries are consecutive: */
1736 if (prev_offset && reloc->offset != prev_offset + 8)
1739 /* Detect function pointers from contiguous objects: */
1740 if (reloc->sym->sec == pfunc->sec &&
1741 reloc->addend == pfunc->offset)
1744 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1748 /* Make sure the destination is in the same function: */
1749 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1752 alt = malloc(sizeof(*alt));
1754 WARN("malloc failed");
1758 alt->insn = dest_insn;
1759 list_add_tail(&alt->list, &insn->alts);
1760 prev_offset = reloc->offset;
1764 WARN_FUNC("can't find switch jump table",
1765 insn->sec, insn->offset);
1773 * find_jump_table() - Given a dynamic jump, find the switch jump table
1774 * associated with it.
1776 static struct reloc *find_jump_table(struct objtool_file *file,
1777 struct symbol *func,
1778 struct instruction *insn)
1780 struct reloc *table_reloc;
1781 struct instruction *dest_insn, *orig_insn = insn;
1784 * Backward search using the @first_jump_src links, these help avoid
1785 * much of the 'in between' code. Which avoids us getting confused by
1789 insn && insn->func && insn->func->pfunc == func;
1790 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1792 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1795 /* allow small jumps within the range */
1796 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1798 (insn->jump_dest->offset <= insn->offset ||
1799 insn->jump_dest->offset > orig_insn->offset))
1802 table_reloc = arch_find_switch_table(file, insn);
1805 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1806 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1816 * First pass: Mark the head of each jump table so that in the next pass,
1817 * we know when a given jump table ends and the next one starts.
1819 static void mark_func_jump_tables(struct objtool_file *file,
1820 struct symbol *func)
1822 struct instruction *insn, *last = NULL;
1823 struct reloc *reloc;
1825 func_for_each_insn(file, func, insn) {
1830 * Store back-pointers for unconditional forward jumps such
1831 * that find_jump_table() can back-track using those and
1832 * avoid some potentially confusing code.
1834 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1835 insn->offset > last->offset &&
1836 insn->jump_dest->offset > insn->offset &&
1837 !insn->jump_dest->first_jump_src) {
1839 insn->jump_dest->first_jump_src = insn;
1840 last = insn->jump_dest;
1843 if (insn->type != INSN_JUMP_DYNAMIC)
1846 reloc = find_jump_table(file, func, insn);
1848 reloc->jump_table_start = true;
1849 insn->jump_table = reloc;
1854 static int add_func_jump_tables(struct objtool_file *file,
1855 struct symbol *func)
1857 struct instruction *insn;
1860 func_for_each_insn(file, func, insn) {
1861 if (!insn->jump_table)
1864 ret = add_jump_table(file, insn, insn->jump_table);
1873 * For some switch statements, gcc generates a jump table in the .rodata
1874 * section which contains a list of addresses within the function to jump to.
1875 * This finds these jump tables and adds them to the insn->alts lists.
1877 static int add_jump_table_alts(struct objtool_file *file)
1879 struct section *sec;
1880 struct symbol *func;
1886 for_each_sec(file, sec) {
1887 list_for_each_entry(func, &sec->symbol_list, list) {
1888 if (func->type != STT_FUNC)
1891 mark_func_jump_tables(file, func);
1892 ret = add_func_jump_tables(file, func);
1901 static void set_func_state(struct cfi_state *state)
1903 state->cfa = initial_func_cfi.cfa;
1904 memcpy(&state->regs, &initial_func_cfi.regs,
1905 CFI_NUM_REGS * sizeof(struct cfi_reg));
1906 state->stack_size = initial_func_cfi.cfa.offset;
1909 static int read_unwind_hints(struct objtool_file *file)
1911 struct cfi_state cfi = init_cfi;
1912 struct section *sec, *relocsec;
1913 struct unwind_hint *hint;
1914 struct instruction *insn;
1915 struct reloc *reloc;
1918 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1922 relocsec = sec->reloc;
1924 WARN("missing .rela.discard.unwind_hints section");
1928 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1929 WARN("struct unwind_hint size mismatch");
1935 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1936 hint = (struct unwind_hint *)sec->data->d_buf + i;
1938 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1940 WARN("can't find reloc for unwind_hints[%d]", i);
1944 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1946 WARN("can't find insn for unwind_hints[%d]", i);
1952 if (opts.ibt && hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
1953 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
1955 if (sym && sym->bind == STB_GLOBAL &&
1956 insn->type != INSN_ENDBR && !insn->noendbr) {
1957 WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR",
1958 insn->sec, insn->offset);
1962 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1963 insn->cfi = &func_cfi;
1970 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
1971 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1972 insn->sec, insn->offset, hint->sp_reg);
1976 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1977 cfi.type = hint->type;
1978 cfi.end = hint->end;
1980 insn->cfi = cfi_hash_find_or_add(&cfi);
1986 static int read_noendbr_hints(struct objtool_file *file)
1988 struct section *sec;
1989 struct instruction *insn;
1990 struct reloc *reloc;
1992 sec = find_section_by_name(file->elf, ".rela.discard.noendbr");
1996 list_for_each_entry(reloc, &sec->reloc_list, list) {
1997 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
1999 WARN("bad .discard.noendbr entry");
2003 if (insn->type == INSN_ENDBR)
2004 WARN_FUNC("ANNOTATE_NOENDBR on ENDBR", insn->sec, insn->offset);
2012 static int read_retpoline_hints(struct objtool_file *file)
2014 struct section *sec;
2015 struct instruction *insn;
2016 struct reloc *reloc;
2018 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
2022 list_for_each_entry(reloc, &sec->reloc_list, list) {
2023 if (reloc->sym->type != STT_SECTION) {
2024 WARN("unexpected relocation symbol type in %s", sec->name);
2028 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2030 WARN("bad .discard.retpoline_safe entry");
2034 if (insn->type != INSN_JUMP_DYNAMIC &&
2035 insn->type != INSN_CALL_DYNAMIC) {
2036 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
2037 insn->sec, insn->offset);
2041 insn->retpoline_safe = true;
2047 static int read_instr_hints(struct objtool_file *file)
2049 struct section *sec;
2050 struct instruction *insn;
2051 struct reloc *reloc;
2053 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2057 list_for_each_entry(reloc, &sec->reloc_list, list) {
2058 if (reloc->sym->type != STT_SECTION) {
2059 WARN("unexpected relocation symbol type in %s", sec->name);
2063 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2065 WARN("bad .discard.instr_end entry");
2072 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2076 list_for_each_entry(reloc, &sec->reloc_list, list) {
2077 if (reloc->sym->type != STT_SECTION) {
2078 WARN("unexpected relocation symbol type in %s", sec->name);
2082 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2084 WARN("bad .discard.instr_begin entry");
2094 static int read_intra_function_calls(struct objtool_file *file)
2096 struct instruction *insn;
2097 struct section *sec;
2098 struct reloc *reloc;
2100 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2104 list_for_each_entry(reloc, &sec->reloc_list, list) {
2105 unsigned long dest_off;
2107 if (reloc->sym->type != STT_SECTION) {
2108 WARN("unexpected relocation symbol type in %s",
2113 insn = find_insn(file, reloc->sym->sec, reloc->addend);
2115 WARN("bad .discard.intra_function_call entry");
2119 if (insn->type != INSN_CALL) {
2120 WARN_FUNC("intra_function_call not a direct call",
2121 insn->sec, insn->offset);
2126 * Treat intra-function CALLs as JMPs, but with a stack_op.
2127 * See add_call_destinations(), which strips stack_ops from
2130 insn->type = INSN_JUMP_UNCONDITIONAL;
2132 dest_off = insn->offset + insn->len + insn->immediate;
2133 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2134 if (!insn->jump_dest) {
2135 WARN_FUNC("can't find call dest at %s+0x%lx",
2136 insn->sec, insn->offset,
2137 insn->sec->name, dest_off);
2146 * Return true if name matches an instrumentation function, where calls to that
2147 * function from noinstr code can safely be removed, but compilers won't do so.
2149 static bool is_profiling_func(const char *name)
2152 * Many compilers cannot disable KCOV with a function attribute.
2154 if (!strncmp(name, "__sanitizer_cov_", 16))
2158 * Some compilers currently do not remove __tsan_func_entry/exit nor
2159 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2160 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2161 * minimum Clang version is 14.0, this can be removed.
2163 if (!strncmp(name, "__tsan_func_", 12) ||
2164 !strcmp(name, "__tsan_atomic_signal_fence"))
2170 static int classify_symbols(struct objtool_file *file)
2172 struct section *sec;
2173 struct symbol *func;
2175 for_each_sec(file, sec) {
2176 list_for_each_entry(func, &sec->symbol_list, list) {
2177 if (func->bind != STB_GLOBAL)
2180 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2181 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2182 func->static_call_tramp = true;
2184 if (arch_is_retpoline(func))
2185 func->retpoline_thunk = true;
2187 if (!strcmp(func->name, "__fentry__"))
2188 func->fentry = true;
2190 if (is_profiling_func(func->name))
2191 func->profiling_func = true;
2198 static void mark_rodata(struct objtool_file *file)
2200 struct section *sec;
2204 * Search for the following rodata sections, each of which can
2205 * potentially contain jump tables:
2207 * - .rodata: can contain GCC switch tables
2208 * - .rodata.<func>: same, if -fdata-sections is being used
2209 * - .rodata..c_jump_table: contains C annotated jump tables
2211 * .rodata.str1.* sections are ignored; they don't contain jump tables.
2213 for_each_sec(file, sec) {
2214 if (!strncmp(sec->name, ".rodata", 7) &&
2215 !strstr(sec->name, ".str1.")) {
2221 file->rodata = found;
2224 static int decode_sections(struct objtool_file *file)
2230 ret = init_pv_ops(file);
2234 ret = decode_instructions(file);
2239 add_uaccess_safe(file);
2241 ret = add_ignore_alternatives(file);
2246 * Must be before read_unwind_hints() since that needs insn->noendbr.
2248 ret = read_noendbr_hints(file);
2253 * Must be before add_{jump_call}_destination.
2255 ret = classify_symbols(file);
2260 * Must be before add_jump_destinations(), which depends on 'func'
2261 * being set for alternatives, to enable proper sibling call detection.
2263 ret = add_special_section_alts(file);
2267 ret = add_jump_destinations(file);
2272 * Must be before add_call_destination(); it changes INSN_CALL to
2275 ret = read_intra_function_calls(file);
2279 ret = add_call_destinations(file);
2284 * Must be after add_call_destinations() such that it can override
2285 * dead_end_function() marks.
2287 ret = add_dead_ends(file);
2291 ret = add_jump_table_alts(file);
2295 ret = read_unwind_hints(file);
2299 ret = read_retpoline_hints(file);
2303 ret = read_instr_hints(file);
2310 static bool is_fentry_call(struct instruction *insn)
2312 if (insn->type == INSN_CALL &&
2314 insn->call_dest->fentry)
2320 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
2322 struct cfi_state *cfi = &state->cfi;
2325 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
2328 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
2331 if (cfi->stack_size != initial_func_cfi.cfa.offset)
2334 for (i = 0; i < CFI_NUM_REGS; i++) {
2335 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2336 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
2343 static bool check_reg_frame_pos(const struct cfi_reg *reg,
2344 int expected_offset)
2346 return reg->base == CFI_CFA &&
2347 reg->offset == expected_offset;
2350 static bool has_valid_stack_frame(struct insn_state *state)
2352 struct cfi_state *cfi = &state->cfi;
2354 if (cfi->cfa.base == CFI_BP &&
2355 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2356 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2359 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2365 static int update_cfi_state_regs(struct instruction *insn,
2366 struct cfi_state *cfi,
2367 struct stack_op *op)
2369 struct cfi_reg *cfa = &cfi->cfa;
2371 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2375 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2379 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2382 /* add immediate to sp */
2383 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2384 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2385 cfa->offset -= op->src.offset;
2390 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2392 if (arch_callee_saved_reg(reg) &&
2393 cfi->regs[reg].base == CFI_UNDEFINED) {
2394 cfi->regs[reg].base = base;
2395 cfi->regs[reg].offset = offset;
2399 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2401 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2402 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2406 * A note about DRAP stack alignment:
2408 * GCC has the concept of a DRAP register, which is used to help keep track of
2409 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2410 * register. The typical DRAP pattern is:
2412 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2413 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2414 * 41 ff 72 f8 pushq -0x8(%r10)
2416 * 48 89 e5 mov %rsp,%rbp
2423 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2426 * There are some variations in the epilogues, like:
2434 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2439 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2440 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2441 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2442 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2444 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2447 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2448 * restored beforehand:
2451 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2452 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2454 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2458 static int update_cfi_state(struct instruction *insn,
2459 struct instruction *next_insn,
2460 struct cfi_state *cfi, struct stack_op *op)
2462 struct cfi_reg *cfa = &cfi->cfa;
2463 struct cfi_reg *regs = cfi->regs;
2465 /* stack operations don't make sense with an undefined CFA */
2466 if (cfa->base == CFI_UNDEFINED) {
2468 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2474 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2475 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2476 return update_cfi_state_regs(insn, cfi, op);
2478 switch (op->dest.type) {
2481 switch (op->src.type) {
2484 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2485 cfa->base == CFI_SP &&
2486 check_reg_frame_pos(®s[CFI_BP], -cfa->offset)) {
2488 /* mov %rsp, %rbp */
2489 cfa->base = op->dest.reg;
2490 cfi->bp_scratch = false;
2493 else if (op->src.reg == CFI_SP &&
2494 op->dest.reg == CFI_BP && cfi->drap) {
2496 /* drap: mov %rsp, %rbp */
2497 regs[CFI_BP].base = CFI_BP;
2498 regs[CFI_BP].offset = -cfi->stack_size;
2499 cfi->bp_scratch = false;
2502 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2507 * This is needed for the rare case where GCC
2514 cfi->vals[op->dest.reg].base = CFI_CFA;
2515 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2518 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2519 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2524 * Restore the original stack pointer (Clang).
2526 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2529 else if (op->dest.reg == cfa->base) {
2531 /* mov %reg, %rsp */
2532 if (cfa->base == CFI_SP &&
2533 cfi->vals[op->src.reg].base == CFI_CFA) {
2536 * This is needed for the rare case
2537 * where GCC does something dumb like:
2539 * lea 0x8(%rsp), %rcx
2543 cfa->offset = -cfi->vals[op->src.reg].offset;
2544 cfi->stack_size = cfa->offset;
2546 } else if (cfa->base == CFI_SP &&
2547 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2548 cfi->vals[op->src.reg].offset == cfa->offset) {
2553 * 1: mov %rsp, (%[tos])
2554 * 2: mov %[tos], %rsp
2560 * 1 - places a pointer to the previous
2561 * stack at the Top-of-Stack of the
2564 * 2 - switches to the new stack.
2566 * 3 - pops the Top-of-Stack to restore
2567 * the original stack.
2569 * Note: we set base to SP_INDIRECT
2570 * here and preserve offset. Therefore
2571 * when the unwinder reaches ToS it
2572 * will dereference SP and then add the
2573 * offset to find the next frame, IOW:
2576 cfa->base = CFI_SP_INDIRECT;
2579 cfa->base = CFI_UNDEFINED;
2584 else if (op->dest.reg == CFI_SP &&
2585 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2586 cfi->vals[op->src.reg].offset == cfa->offset) {
2589 * The same stack swizzle case 2) as above. But
2590 * because we can't change cfa->base, case 3)
2591 * will become a regular POP. Pretend we're a
2592 * PUSH so things don't go unbalanced.
2594 cfi->stack_size += 8;
2601 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2604 cfi->stack_size -= op->src.offset;
2605 if (cfa->base == CFI_SP)
2606 cfa->offset -= op->src.offset;
2610 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2612 /* lea disp(%rbp), %rsp */
2613 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2617 if (!cfi->drap && op->src.reg == CFI_SP &&
2618 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2619 check_reg_frame_pos(®s[CFI_BP], -cfa->offset + op->src.offset)) {
2621 /* lea disp(%rsp), %rbp */
2623 cfa->offset -= op->src.offset;
2624 cfi->bp_scratch = false;
2628 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2630 /* drap: lea disp(%rsp), %drap */
2631 cfi->drap_reg = op->dest.reg;
2634 * lea disp(%rsp), %reg
2636 * This is needed for the rare case where GCC
2637 * does something dumb like:
2639 * lea 0x8(%rsp), %rcx
2643 cfi->vals[op->dest.reg].base = CFI_CFA;
2644 cfi->vals[op->dest.reg].offset = \
2645 -cfi->stack_size + op->src.offset;
2650 if (cfi->drap && op->dest.reg == CFI_SP &&
2651 op->src.reg == cfi->drap_reg) {
2653 /* drap: lea disp(%drap), %rsp */
2655 cfa->offset = cfi->stack_size = -op->src.offset;
2656 cfi->drap_reg = CFI_UNDEFINED;
2661 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2662 WARN_FUNC("unsupported stack register modification",
2663 insn->sec, insn->offset);
2670 if (op->dest.reg != CFI_SP ||
2671 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2672 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2673 WARN_FUNC("unsupported stack pointer realignment",
2674 insn->sec, insn->offset);
2678 if (cfi->drap_reg != CFI_UNDEFINED) {
2679 /* drap: and imm, %rsp */
2680 cfa->base = cfi->drap_reg;
2681 cfa->offset = cfi->stack_size = 0;
2686 * Older versions of GCC (4.8ish) realign the stack
2687 * without DRAP, with a frame pointer.
2694 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2696 /* pop %rsp; # restore from a stack swizzle */
2701 if (!cfi->drap && op->dest.reg == cfa->base) {
2707 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2708 op->dest.reg == cfi->drap_reg &&
2709 cfi->drap_offset == -cfi->stack_size) {
2711 /* drap: pop %drap */
2712 cfa->base = cfi->drap_reg;
2714 cfi->drap_offset = -1;
2716 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2719 restore_reg(cfi, op->dest.reg);
2722 cfi->stack_size -= 8;
2723 if (cfa->base == CFI_SP)
2728 case OP_SRC_REG_INDIRECT:
2729 if (!cfi->drap && op->dest.reg == cfa->base &&
2730 op->dest.reg == CFI_BP) {
2732 /* mov disp(%rsp), %rbp */
2734 cfa->offset = cfi->stack_size;
2737 if (cfi->drap && op->src.reg == CFI_BP &&
2738 op->src.offset == cfi->drap_offset) {
2740 /* drap: mov disp(%rbp), %drap */
2741 cfa->base = cfi->drap_reg;
2743 cfi->drap_offset = -1;
2746 if (cfi->drap && op->src.reg == CFI_BP &&
2747 op->src.offset == regs[op->dest.reg].offset) {
2749 /* drap: mov disp(%rbp), %reg */
2750 restore_reg(cfi, op->dest.reg);
2752 } else if (op->src.reg == cfa->base &&
2753 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2755 /* mov disp(%rbp), %reg */
2756 /* mov disp(%rsp), %reg */
2757 restore_reg(cfi, op->dest.reg);
2759 } else if (op->src.reg == CFI_SP &&
2760 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2762 /* mov disp(%rsp), %reg */
2763 restore_reg(cfi, op->dest.reg);
2769 WARN_FUNC("unknown stack-related instruction",
2770 insn->sec, insn->offset);
2778 cfi->stack_size += 8;
2779 if (cfa->base == CFI_SP)
2782 if (op->src.type != OP_SRC_REG)
2786 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2788 /* drap: push %drap */
2789 cfa->base = CFI_BP_INDIRECT;
2790 cfa->offset = -cfi->stack_size;
2792 /* save drap so we know when to restore it */
2793 cfi->drap_offset = -cfi->stack_size;
2795 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2797 /* drap: push %rbp */
2798 cfi->stack_size = 0;
2802 /* drap: push %reg */
2803 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2809 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2812 /* detect when asm code uses rbp as a scratch register */
2813 if (opts.stackval && insn->func && op->src.reg == CFI_BP &&
2814 cfa->base != CFI_BP)
2815 cfi->bp_scratch = true;
2818 case OP_DEST_REG_INDIRECT:
2821 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2823 /* drap: mov %drap, disp(%rbp) */
2824 cfa->base = CFI_BP_INDIRECT;
2825 cfa->offset = op->dest.offset;
2827 /* save drap offset so we know when to restore it */
2828 cfi->drap_offset = op->dest.offset;
2831 /* drap: mov reg, disp(%rbp) */
2832 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2835 } else if (op->dest.reg == cfa->base) {
2837 /* mov reg, disp(%rbp) */
2838 /* mov reg, disp(%rsp) */
2839 save_reg(cfi, op->src.reg, CFI_CFA,
2840 op->dest.offset - cfi->cfa.offset);
2842 } else if (op->dest.reg == CFI_SP) {
2844 /* mov reg, disp(%rsp) */
2845 save_reg(cfi, op->src.reg, CFI_CFA,
2846 op->dest.offset - cfi->stack_size);
2848 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2850 /* mov %rsp, (%reg); # setup a stack swizzle. */
2851 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2852 cfi->vals[op->dest.reg].offset = cfa->offset;
2858 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2859 WARN_FUNC("unknown stack-related memory operation",
2860 insn->sec, insn->offset);
2865 cfi->stack_size -= 8;
2866 if (cfa->base == CFI_SP)
2872 WARN_FUNC("unknown stack-related instruction",
2873 insn->sec, insn->offset);
2881 * The stack layouts of alternatives instructions can sometimes diverge when
2882 * they have stack modifications. That's fine as long as the potential stack
2883 * layouts don't conflict at any given potential instruction boundary.
2885 * Flatten the CFIs of the different alternative code streams (both original
2886 * and replacement) into a single shared CFI array which can be used to detect
2887 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2889 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2891 struct cfi_state **alt_cfi;
2894 if (!insn->alt_group)
2898 WARN("CFI missing");
2902 alt_cfi = insn->alt_group->cfi;
2903 group_off = insn->offset - insn->alt_group->first_insn->offset;
2905 if (!alt_cfi[group_off]) {
2906 alt_cfi[group_off] = insn->cfi;
2908 if (cficmp(alt_cfi[group_off], insn->cfi)) {
2909 WARN_FUNC("stack layout conflict in alternatives",
2910 insn->sec, insn->offset);
2918 static int handle_insn_ops(struct instruction *insn,
2919 struct instruction *next_insn,
2920 struct insn_state *state)
2922 struct stack_op *op;
2924 list_for_each_entry(op, &insn->stack_ops, list) {
2926 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2929 if (!insn->alt_group)
2932 if (op->dest.type == OP_DEST_PUSHF) {
2933 if (!state->uaccess_stack) {
2934 state->uaccess_stack = 1;
2935 } else if (state->uaccess_stack >> 31) {
2936 WARN_FUNC("PUSHF stack exhausted",
2937 insn->sec, insn->offset);
2940 state->uaccess_stack <<= 1;
2941 state->uaccess_stack |= state->uaccess;
2944 if (op->src.type == OP_SRC_POPF) {
2945 if (state->uaccess_stack) {
2946 state->uaccess = state->uaccess_stack & 1;
2947 state->uaccess_stack >>= 1;
2948 if (state->uaccess_stack == 1)
2949 state->uaccess_stack = 0;
2957 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2959 struct cfi_state *cfi1 = insn->cfi;
2963 WARN("CFI missing");
2967 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2969 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2970 insn->sec, insn->offset,
2971 cfi1->cfa.base, cfi1->cfa.offset,
2972 cfi2->cfa.base, cfi2->cfa.offset);
2974 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2975 for (i = 0; i < CFI_NUM_REGS; i++) {
2976 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2977 sizeof(struct cfi_reg)))
2980 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2981 insn->sec, insn->offset,
2982 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2983 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2987 } else if (cfi1->type != cfi2->type) {
2989 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2990 insn->sec, insn->offset, cfi1->type, cfi2->type);
2992 } else if (cfi1->drap != cfi2->drap ||
2993 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2994 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2996 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2997 insn->sec, insn->offset,
2998 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2999 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
3007 static inline bool func_uaccess_safe(struct symbol *func)
3010 return func->uaccess_safe;
3015 static inline const char *call_dest_name(struct instruction *insn)
3017 static char pvname[19];
3021 if (insn->call_dest)
3022 return insn->call_dest->name;
3024 rel = insn_reloc(NULL, insn);
3025 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
3026 idx = (rel->addend / sizeof(void *));
3027 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3034 static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3036 struct symbol *target;
3040 rel = insn_reloc(file, insn);
3041 if (!rel || strcmp(rel->sym->name, "pv_ops"))
3044 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
3046 if (file->pv_ops[idx].clean)
3049 file->pv_ops[idx].clean = true;
3051 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3052 if (!target->sec->noinstr) {
3053 WARN("pv_ops[%d]: %s", idx, target->name);
3054 file->pv_ops[idx].clean = false;
3058 return file->pv_ops[idx].clean;
3061 static inline bool noinstr_call_dest(struct objtool_file *file,
3062 struct instruction *insn,
3063 struct symbol *func)
3066 * We can't deal with indirect function calls at present;
3067 * assume they're instrumented.
3071 return pv_call_dest(file, insn);
3077 * If the symbol is from a noinstr section; we good.
3079 if (func->sec->noinstr)
3083 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3084 * something 'BAD' happened. At the risk of taking the machine down,
3085 * let them proceed to get the message out.
3087 if (!strncmp(func->name, "__ubsan_handle_", 15))
3093 static int validate_call(struct objtool_file *file,
3094 struct instruction *insn,
3095 struct insn_state *state)
3097 if (state->noinstr && state->instr <= 0 &&
3098 !noinstr_call_dest(file, insn, insn->call_dest)) {
3099 WARN_FUNC("call to %s() leaves .noinstr.text section",
3100 insn->sec, insn->offset, call_dest_name(insn));
3104 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
3105 WARN_FUNC("call to %s() with UACCESS enabled",
3106 insn->sec, insn->offset, call_dest_name(insn));
3111 WARN_FUNC("call to %s() with DF set",
3112 insn->sec, insn->offset, call_dest_name(insn));
3119 static int validate_sibling_call(struct objtool_file *file,
3120 struct instruction *insn,
3121 struct insn_state *state)
3123 if (has_modified_stack_frame(insn, state)) {
3124 WARN_FUNC("sibling call from callable instruction with modified stack frame",
3125 insn->sec, insn->offset);
3129 return validate_call(file, insn, state);
3132 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3134 if (state->noinstr && state->instr > 0) {
3135 WARN_FUNC("return with instrumentation enabled",
3136 insn->sec, insn->offset);
3140 if (state->uaccess && !func_uaccess_safe(func)) {
3141 WARN_FUNC("return with UACCESS enabled",
3142 insn->sec, insn->offset);
3146 if (!state->uaccess && func_uaccess_safe(func)) {
3147 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
3148 insn->sec, insn->offset);
3153 WARN_FUNC("return with DF set",
3154 insn->sec, insn->offset);
3158 if (func && has_modified_stack_frame(insn, state)) {
3159 WARN_FUNC("return with modified stack frame",
3160 insn->sec, insn->offset);
3164 if (state->cfi.bp_scratch) {
3165 WARN_FUNC("BP used as a scratch register",
3166 insn->sec, insn->offset);
3173 static struct instruction *next_insn_to_validate(struct objtool_file *file,
3174 struct instruction *insn)
3176 struct alt_group *alt_group = insn->alt_group;
3179 * Simulate the fact that alternatives are patched in-place. When the
3180 * end of a replacement alt_group is reached, redirect objtool flow to
3181 * the end of the original alt_group.
3183 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3184 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3186 return next_insn_same_sec(file, insn);
3190 * Follow the branch starting at the given instruction, and recursively follow
3191 * any other branches (jumps). Meanwhile, track the frame pointer state at
3192 * each instruction and validate all the rules described in
3193 * tools/objtool/Documentation/stack-validation.txt.
3195 static int validate_branch(struct objtool_file *file, struct symbol *func,
3196 struct instruction *insn, struct insn_state state)
3198 struct alternative *alt;
3199 struct instruction *next_insn, *prev_insn = NULL;
3200 struct section *sec;
3207 next_insn = next_insn_to_validate(file, insn);
3209 if (func && insn->func && func != insn->func->pfunc) {
3210 WARN("%s() falls through to next function %s()",
3211 func->name, insn->func->name);
3215 if (func && insn->ignore) {
3216 WARN_FUNC("BUG: why am I validating an ignored function?",
3221 visited = 1 << state.uaccess;
3222 if (insn->visited) {
3223 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
3226 if (insn->visited & visited)
3233 state.instr += insn->instr;
3236 state.cfi = *insn->cfi;
3238 /* XXX track if we actually changed state.cfi */
3240 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3241 insn->cfi = prev_insn->cfi;
3244 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3248 insn->visited |= visited;
3250 if (propagate_alt_cfi(file, insn))
3253 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3254 bool skip_orig = false;
3256 list_for_each_entry(alt, &insn->alts, list) {
3260 ret = validate_branch(file, func, alt->insn, state);
3263 BT_FUNC("(alt)", insn);
3272 if (handle_insn_ops(insn, next_insn, &state))
3275 switch (insn->type) {
3278 return validate_return(func, insn, &state);
3281 case INSN_CALL_DYNAMIC:
3282 ret = validate_call(file, insn, &state);
3286 if (opts.stackval && func && !is_fentry_call(insn) &&
3287 !has_valid_stack_frame(&state)) {
3288 WARN_FUNC("call without frame pointer save/setup",
3298 case INSN_JUMP_CONDITIONAL:
3299 case INSN_JUMP_UNCONDITIONAL:
3300 if (is_sibling_call(insn)) {
3301 ret = validate_sibling_call(file, insn, &state);
3305 } else if (insn->jump_dest) {
3306 ret = validate_branch(file, func,
3307 insn->jump_dest, state);
3310 BT_FUNC("(branch)", insn);
3315 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3320 case INSN_JUMP_DYNAMIC:
3321 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3322 if (is_sibling_call(insn)) {
3323 ret = validate_sibling_call(file, insn, &state);
3328 if (insn->type == INSN_JUMP_DYNAMIC)
3333 case INSN_CONTEXT_SWITCH:
3334 if (func && (!next_insn || !next_insn->hint)) {
3335 WARN_FUNC("unsupported instruction in callable function",
3342 if (state.uaccess) {
3343 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3347 state.uaccess = true;
3351 if (!state.uaccess && func) {
3352 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3356 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3357 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3361 state.uaccess = false;
3366 WARN_FUNC("recursive STD", sec, insn->offset);
3374 if (!state.df && func) {
3375 WARN_FUNC("redundant CLD", sec, insn->offset);
3390 if (state.cfi.cfa.base == CFI_UNDEFINED)
3392 WARN("%s: unexpected end of section", sec->name);
3403 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
3405 struct instruction *insn;
3406 struct insn_state state;
3407 int ret, warnings = 0;
3412 init_insn_state(file, &state, sec);
3415 insn = find_insn(file, sec, 0);
3419 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3422 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3423 if (insn->hint && !insn->visited && !insn->ignore) {
3424 ret = validate_branch(file, insn->func, insn, state);
3425 if (ret && opts.backtrace)
3426 BT_FUNC("<=== (hint)", insn);
3430 insn = list_next_entry(insn, list);
3436 static int validate_retpoline(struct objtool_file *file)
3438 struct instruction *insn;
3441 for_each_insn(file, insn) {
3442 if (insn->type != INSN_JUMP_DYNAMIC &&
3443 insn->type != INSN_CALL_DYNAMIC)
3446 if (insn->retpoline_safe)
3450 * .init.text code is ran before userspace and thus doesn't
3451 * strictly need retpolines, except for modules which are
3452 * loaded late, they very much do need retpoline in their
3455 if (!strcmp(insn->sec->name, ".init.text") && !opts.module)
3458 WARN_FUNC("indirect %s found in RETPOLINE build",
3459 insn->sec, insn->offset,
3460 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3468 static bool is_kasan_insn(struct instruction *insn)
3470 return (insn->type == INSN_CALL &&
3471 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3474 static bool is_ubsan_insn(struct instruction *insn)
3476 return (insn->type == INSN_CALL &&
3477 !strcmp(insn->call_dest->name,
3478 "__ubsan_handle_builtin_unreachable"));
3481 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3484 struct instruction *prev_insn;
3486 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
3490 * Ignore alternative replacement instructions. This can happen
3491 * when a whitelisted function uses one of the ALTERNATIVE macros.
3493 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
3494 !strcmp(insn->sec->name, ".altinstr_aux"))
3498 * Whole archive runs might encounter dead code from weak symbols.
3499 * This is where the linker will have dropped the weak symbol in
3500 * favour of a regular symbol, but leaves the code in place.
3502 * In this case we'll find a piece of code (whole function) that is not
3503 * covered by a !section symbol. Ignore them.
3505 if (opts.link && !insn->func) {
3506 int size = find_symbol_hole_containing(insn->sec, insn->offset);
3507 unsigned long end = insn->offset + size;
3509 if (!size) /* not a hole */
3512 if (size < 0) /* hole until the end */
3515 sec_for_each_insn_continue(file, insn) {
3517 * If we reach a visited instruction at or before the
3518 * end of the hole, ignore the unreachable.
3523 if (insn->offset >= end)
3527 * If this hole jumps to a .cold function, mark it ignore too.
3529 if (insn->jump_dest && insn->jump_dest->func &&
3530 strstr(insn->jump_dest->func->name, ".cold")) {
3531 struct instruction *dest = insn->jump_dest;
3532 func_for_each_insn(file, dest->func, dest)
3533 dest->ignore = true;
3543 if (insn->func->static_call_tramp)
3547 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3548 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3549 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3550 * (or occasionally a JMP to UD2).
3552 * It may also insert a UD2 after calling a __noreturn function.
3554 prev_insn = list_prev_entry(insn, list);
3555 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3556 (insn->type == INSN_BUG ||
3557 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3558 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3562 * Check if this (or a subsequent) instruction is related to
3563 * CONFIG_UBSAN or CONFIG_KASAN.
3565 * End the search at 5 instructions to avoid going into the weeds.
3567 for (i = 0; i < 5; i++) {
3569 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3572 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3573 if (insn->jump_dest &&
3574 insn->jump_dest->func == insn->func) {
3575 insn = insn->jump_dest;
3582 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3585 insn = list_next_entry(insn, list);
3591 static int validate_symbol(struct objtool_file *file, struct section *sec,
3592 struct symbol *sym, struct insn_state *state)
3594 struct instruction *insn;
3598 WARN("%s() is missing an ELF size annotation", sym->name);
3602 if (sym->pfunc != sym || sym->alias != sym)
3605 insn = find_insn(file, sec, sym->offset);
3606 if (!insn || insn->ignore || insn->visited)
3609 state->uaccess = sym->uaccess_safe;
3611 ret = validate_branch(file, insn->func, insn, *state);
3612 if (ret && opts.backtrace)
3613 BT_FUNC("<=== (sym)", insn);
3617 static int validate_section(struct objtool_file *file, struct section *sec)
3619 struct insn_state state;
3620 struct symbol *func;
3623 list_for_each_entry(func, &sec->symbol_list, list) {
3624 if (func->type != STT_FUNC)
3627 init_insn_state(file, &state, sec);
3628 set_func_state(&state.cfi);
3630 warnings += validate_symbol(file, sec, func, &state);
3636 static int validate_noinstr_sections(struct objtool_file *file)
3638 struct section *sec;
3641 sec = find_section_by_name(file->elf, ".noinstr.text");
3643 warnings += validate_section(file, sec);
3644 warnings += validate_unwind_hints(file, sec);
3647 sec = find_section_by_name(file->elf, ".entry.text");
3649 warnings += validate_section(file, sec);
3650 warnings += validate_unwind_hints(file, sec);
3656 static int validate_functions(struct objtool_file *file)
3658 struct section *sec;
3661 for_each_sec(file, sec) {
3662 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3665 warnings += validate_section(file, sec);
3671 static void mark_endbr_used(struct instruction *insn)
3673 if (!list_empty(&insn->call_node))
3674 list_del_init(&insn->call_node);
3677 static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
3679 struct instruction *dest;
3680 struct reloc *reloc;
3685 * Looking for function pointer load relocations. Ignore
3686 * direct/indirect branches:
3688 switch (insn->type) {
3690 case INSN_CALL_DYNAMIC:
3691 case INSN_JUMP_CONDITIONAL:
3692 case INSN_JUMP_UNCONDITIONAL:
3693 case INSN_JUMP_DYNAMIC:
3694 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3702 for (reloc = insn_reloc(file, insn);
3704 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
3706 (insn->offset + insn->len) - (reloc->offset + 1))) {
3709 * static_call_update() references the trampoline, which
3710 * doesn't have (or need) ENDBR. Skip warning in that case.
3712 if (reloc->sym->static_call_tramp)
3715 off = reloc->sym->offset;
3716 if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)
3717 off += arch_dest_reloc_offset(reloc->addend);
3719 off += reloc->addend;
3721 dest = find_insn(file, reloc->sym->sec, off);
3725 if (dest->type == INSN_ENDBR) {
3726 mark_endbr_used(dest);
3730 if (dest->func && dest->func == insn->func) {
3732 * Anything from->to self is either _THIS_IP_ or
3735 * There is no sane way to annotate _THIS_IP_ since the
3736 * compiler treats the relocation as a constant and is
3737 * happy to fold in offsets, skewing any annotation we
3738 * do, leading to vast amounts of false-positives.
3740 * There's also compiler generated _THIS_IP_ through
3741 * KCOV and such which we have no hope of annotating.
3743 * As such, blanket accept self-references without
3752 WARN_FUNC("relocation to !ENDBR: %s",
3753 insn->sec, insn->offset,
3754 offstr(dest->sec, dest->offset));
3762 static int validate_ibt_data_reloc(struct objtool_file *file,
3763 struct reloc *reloc)
3765 struct instruction *dest;
3767 dest = find_insn(file, reloc->sym->sec,
3768 reloc->sym->offset + reloc->addend);
3772 if (dest->type == INSN_ENDBR) {
3773 mark_endbr_used(dest);
3780 WARN_FUNC("data relocation to !ENDBR: %s",
3781 reloc->sec->base, reloc->offset,
3782 offstr(dest->sec, dest->offset));
3788 * Validate IBT rules and remove used ENDBR instructions from the seal list.
3789 * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
3790 * NOPs) later, in create_ibt_endbr_seal_sections().
3792 static int validate_ibt(struct objtool_file *file)
3794 struct section *sec;
3795 struct reloc *reloc;
3796 struct instruction *insn;
3799 for_each_insn(file, insn)
3800 warnings += validate_ibt_insn(file, insn);
3802 for_each_sec(file, sec) {
3804 /* Already done by validate_ibt_insn() */
3805 if (sec->sh.sh_flags & SHF_EXECINSTR)
3812 * These sections can reference text addresses, but not with
3813 * the intent to indirect branch to them.
3815 if (!strncmp(sec->name, ".discard", 8) ||
3816 !strncmp(sec->name, ".debug", 6) ||
3817 !strcmp(sec->name, ".altinstructions") ||
3818 !strcmp(sec->name, ".ibt_endbr_seal") ||
3819 !strcmp(sec->name, ".orc_unwind_ip") ||
3820 !strcmp(sec->name, ".parainstructions") ||
3821 !strcmp(sec->name, ".retpoline_sites") ||
3822 !strcmp(sec->name, ".smp_locks") ||
3823 !strcmp(sec->name, ".static_call_sites") ||
3824 !strcmp(sec->name, "_error_injection_whitelist") ||
3825 !strcmp(sec->name, "_kprobe_blacklist") ||
3826 !strcmp(sec->name, "__bug_table") ||
3827 !strcmp(sec->name, "__ex_table") ||
3828 !strcmp(sec->name, "__jump_table") ||
3829 !strcmp(sec->name, "__mcount_loc") ||
3830 !strcmp(sec->name, "__tracepoints"))
3833 list_for_each_entry(reloc, &sec->reloc->reloc_list, list)
3834 warnings += validate_ibt_data_reloc(file, reloc);
3840 static int validate_sls(struct objtool_file *file)
3842 struct instruction *insn, *next_insn;
3845 for_each_insn(file, insn) {
3846 next_insn = next_insn_same_sec(file, insn);
3848 if (insn->retpoline_safe)
3851 switch (insn->type) {
3853 if (!next_insn || next_insn->type != INSN_TRAP) {
3854 WARN_FUNC("missing int3 after ret",
3855 insn->sec, insn->offset);
3860 case INSN_JUMP_DYNAMIC:
3861 if (!next_insn || next_insn->type != INSN_TRAP) {
3862 WARN_FUNC("missing int3 after indirect jump",
3863 insn->sec, insn->offset);
3875 static int validate_reachable_instructions(struct objtool_file *file)
3877 struct instruction *insn;
3879 if (file->ignore_unreachables)
3882 for_each_insn(file, insn) {
3883 if (insn->visited || ignore_unreachable_insn(file, insn))
3886 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3893 int check(struct objtool_file *file)
3895 int ret, warnings = 0;
3897 arch_initial_func_cfi_state(&initial_func_cfi);
3898 init_cfi_state(&init_cfi);
3899 init_cfi_state(&func_cfi);
3900 set_func_state(&func_cfi);
3902 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
3905 cfi_hash_add(&init_cfi);
3906 cfi_hash_add(&func_cfi);
3908 ret = decode_sections(file);
3914 if (list_empty(&file->insn_list))
3917 if (opts.retpoline) {
3918 ret = validate_retpoline(file);
3924 if (opts.stackval || opts.orc || opts.uaccess) {
3925 ret = validate_functions(file);
3930 ret = validate_unwind_hints(file, NULL);
3936 ret = validate_reachable_instructions(file);
3942 } else if (opts.noinstr) {
3943 ret = validate_noinstr_sections(file);
3950 ret = validate_ibt(file);
3957 ret = validate_sls(file);
3963 if (opts.static_call) {
3964 ret = create_static_call_sections(file);
3970 if (opts.retpoline) {
3971 ret = create_retpoline_sites_sections(file);
3978 ret = create_mcount_loc_sections(file);
3985 ret = create_ibt_endbr_seal_sections(file);
3991 if (opts.orc && !list_empty(&file->insn_list)) {
3992 ret = orc_create(file);
4000 printf("nr_insns_visited: %ld\n", nr_insns_visited);
4001 printf("nr_cfi: %ld\n", nr_cfi);
4002 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4003 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4008 * For now, don't fail the kernel build on fatal warnings. These
4009 * errors are still fairly common due to the growing matrix of
4010 * supported toolchains and their recent pace of change.