]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/objtool/check.c
UBUNTU: Ubuntu-5.15.0-39.42
[mirror_ubuntu-jammy-kernel.git] / tools / objtool / check.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6 #include <string.h>
7 #include <stdlib.h>
8
9 #include <arch/elf.h>
10 #include <objtool/builtin.h>
11 #include <objtool/cfi.h>
12 #include <objtool/arch.h>
13 #include <objtool/check.h>
14 #include <objtool/special.h>
15 #include <objtool/warn.h>
16 #include <objtool/endianness.h>
17
18 #include <linux/objtool.h>
19 #include <linux/hashtable.h>
20 #include <linux/kernel.h>
21 #include <linux/static_call_types.h>
22
23 struct alternative {
24 struct list_head list;
25 struct instruction *insn;
26 bool skip_orig;
27 };
28
29 struct cfi_init_state initial_func_cfi;
30
31 struct instruction *find_insn(struct objtool_file *file,
32 struct section *sec, unsigned long offset)
33 {
34 struct instruction *insn;
35
36 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
37 if (insn->sec == sec && insn->offset == offset)
38 return insn;
39 }
40
41 return NULL;
42 }
43
44 static struct instruction *next_insn_same_sec(struct objtool_file *file,
45 struct instruction *insn)
46 {
47 struct instruction *next = list_next_entry(insn, list);
48
49 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
50 return NULL;
51
52 return next;
53 }
54
55 static struct instruction *next_insn_same_func(struct objtool_file *file,
56 struct instruction *insn)
57 {
58 struct instruction *next = list_next_entry(insn, list);
59 struct symbol *func = insn->func;
60
61 if (!func)
62 return NULL;
63
64 if (&next->list != &file->insn_list && next->func == func)
65 return next;
66
67 /* Check if we're already in the subfunction: */
68 if (func == func->cfunc)
69 return NULL;
70
71 /* Move to the subfunction: */
72 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
73 }
74
75 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
76 struct instruction *insn)
77 {
78 struct instruction *prev = list_prev_entry(insn, list);
79
80 if (&prev->list != &file->insn_list && prev->func == insn->func)
81 return prev;
82
83 return NULL;
84 }
85
86 #define func_for_each_insn(file, func, insn) \
87 for (insn = find_insn(file, func->sec, func->offset); \
88 insn; \
89 insn = next_insn_same_func(file, insn))
90
91 #define sym_for_each_insn(file, sym, insn) \
92 for (insn = find_insn(file, sym->sec, sym->offset); \
93 insn && &insn->list != &file->insn_list && \
94 insn->sec == sym->sec && \
95 insn->offset < sym->offset + sym->len; \
96 insn = list_next_entry(insn, list))
97
98 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
99 for (insn = list_prev_entry(insn, list); \
100 &insn->list != &file->insn_list && \
101 insn->sec == sym->sec && insn->offset >= sym->offset; \
102 insn = list_prev_entry(insn, list))
103
104 #define sec_for_each_insn_from(file, insn) \
105 for (; insn; insn = next_insn_same_sec(file, insn))
106
107 #define sec_for_each_insn_continue(file, insn) \
108 for (insn = next_insn_same_sec(file, insn); insn; \
109 insn = next_insn_same_sec(file, insn))
110
111 static bool is_jump_table_jump(struct instruction *insn)
112 {
113 struct alt_group *alt_group = insn->alt_group;
114
115 if (insn->jump_table)
116 return true;
117
118 /* Retpoline alternative for a jump table? */
119 return alt_group && alt_group->orig_group &&
120 alt_group->orig_group->first_insn->jump_table;
121 }
122
123 static bool is_sibling_call(struct instruction *insn)
124 {
125 /*
126 * Assume only ELF functions can make sibling calls. This ensures
127 * sibling call detection consistency between vmlinux.o and individual
128 * objects.
129 */
130 if (!insn->func)
131 return false;
132
133 /* An indirect jump is either a sibling call or a jump to a table. */
134 if (insn->type == INSN_JUMP_DYNAMIC)
135 return !is_jump_table_jump(insn);
136
137 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
138 return (is_static_jump(insn) && insn->call_dest);
139 }
140
141 /*
142 * This checks to see if the given function is a "noreturn" function.
143 *
144 * For global functions which are outside the scope of this object file, we
145 * have to keep a manual list of them.
146 *
147 * For local functions, we have to detect them manually by simply looking for
148 * the lack of a return instruction.
149 */
150 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
151 int recursion)
152 {
153 int i;
154 struct instruction *insn;
155 bool empty = true;
156
157 /*
158 * Unfortunately these have to be hard coded because the noreturn
159 * attribute isn't provided in ELF data.
160 */
161 static const char * const global_noreturns[] = {
162 "__stack_chk_fail",
163 "panic",
164 "do_exit",
165 "do_task_dead",
166 "__module_put_and_exit",
167 "complete_and_exit",
168 "__reiserfs_panic",
169 "lbug_with_loc",
170 "fortify_panic",
171 "usercopy_abort",
172 "machine_real_restart",
173 "rewind_stack_do_exit",
174 "kunit_try_catch_throw",
175 "xen_start_kernel",
176 "cpu_bringup_and_idle",
177 };
178
179 if (!func)
180 return false;
181
182 if (func->bind == STB_WEAK)
183 return false;
184
185 if (func->bind == STB_GLOBAL)
186 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
187 if (!strcmp(func->name, global_noreturns[i]))
188 return true;
189
190 if (!func->len)
191 return false;
192
193 insn = find_insn(file, func->sec, func->offset);
194 if (!insn->func)
195 return false;
196
197 func_for_each_insn(file, func, insn) {
198 empty = false;
199
200 if (insn->type == INSN_RETURN)
201 return false;
202 }
203
204 if (empty)
205 return false;
206
207 /*
208 * A function can have a sibling call instead of a return. In that
209 * case, the function's dead-end status depends on whether the target
210 * of the sibling call returns.
211 */
212 func_for_each_insn(file, func, insn) {
213 if (is_sibling_call(insn)) {
214 struct instruction *dest = insn->jump_dest;
215
216 if (!dest)
217 /* sibling call to another file */
218 return false;
219
220 /* local sibling call */
221 if (recursion == 5) {
222 /*
223 * Infinite recursion: two functions have
224 * sibling calls to each other. This is a very
225 * rare case. It means they aren't dead ends.
226 */
227 return false;
228 }
229
230 return __dead_end_function(file, dest->func, recursion+1);
231 }
232 }
233
234 return true;
235 }
236
237 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
238 {
239 return __dead_end_function(file, func, 0);
240 }
241
242 static void init_cfi_state(struct cfi_state *cfi)
243 {
244 int i;
245
246 for (i = 0; i < CFI_NUM_REGS; i++) {
247 cfi->regs[i].base = CFI_UNDEFINED;
248 cfi->vals[i].base = CFI_UNDEFINED;
249 }
250 cfi->cfa.base = CFI_UNDEFINED;
251 cfi->drap_reg = CFI_UNDEFINED;
252 cfi->drap_offset = -1;
253 }
254
255 static void init_insn_state(struct insn_state *state, struct section *sec)
256 {
257 memset(state, 0, sizeof(*state));
258 init_cfi_state(&state->cfi);
259
260 /*
261 * We need the full vmlinux for noinstr validation, otherwise we can
262 * not correctly determine insn->call_dest->sec (external symbols do
263 * not have a section).
264 */
265 if (vmlinux && noinstr && sec)
266 state->noinstr = sec->noinstr;
267 }
268
269 /*
270 * Call the arch-specific instruction decoder for all the instructions and add
271 * them to the global instruction list.
272 */
273 static int decode_instructions(struct objtool_file *file)
274 {
275 struct section *sec;
276 struct symbol *func;
277 unsigned long offset;
278 struct instruction *insn;
279 unsigned long nr_insns = 0;
280 int ret;
281
282 for_each_sec(file, sec) {
283
284 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
285 continue;
286
287 if (strcmp(sec->name, ".altinstr_replacement") &&
288 strcmp(sec->name, ".altinstr_aux") &&
289 strncmp(sec->name, ".discard.", 9))
290 sec->text = true;
291
292 if (!strcmp(sec->name, ".noinstr.text") ||
293 !strcmp(sec->name, ".entry.text"))
294 sec->noinstr = true;
295
296 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
297 insn = malloc(sizeof(*insn));
298 if (!insn) {
299 WARN("malloc failed");
300 return -1;
301 }
302 memset(insn, 0, sizeof(*insn));
303 INIT_LIST_HEAD(&insn->alts);
304 INIT_LIST_HEAD(&insn->stack_ops);
305 init_cfi_state(&insn->cfi);
306
307 insn->sec = sec;
308 insn->offset = offset;
309
310 ret = arch_decode_instruction(file->elf, sec, offset,
311 sec->sh.sh_size - offset,
312 &insn->len, &insn->type,
313 &insn->immediate,
314 &insn->stack_ops);
315 if (ret)
316 goto err;
317
318 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
319 list_add_tail(&insn->list, &file->insn_list);
320 nr_insns++;
321 }
322
323 list_for_each_entry(func, &sec->symbol_list, list) {
324 if (func->type != STT_FUNC || func->alias != func)
325 continue;
326
327 if (!find_insn(file, sec, func->offset)) {
328 WARN("%s(): can't find starting instruction",
329 func->name);
330 return -1;
331 }
332
333 sym_for_each_insn(file, func, insn)
334 insn->func = func;
335 }
336 }
337
338 if (stats)
339 printf("nr_insns: %lu\n", nr_insns);
340
341 return 0;
342
343 err:
344 free(insn);
345 return ret;
346 }
347
348 static struct instruction *find_last_insn(struct objtool_file *file,
349 struct section *sec)
350 {
351 struct instruction *insn = NULL;
352 unsigned int offset;
353 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
354
355 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
356 insn = find_insn(file, sec, offset);
357
358 return insn;
359 }
360
361 /*
362 * Mark "ud2" instructions and manually annotated dead ends.
363 */
364 static int add_dead_ends(struct objtool_file *file)
365 {
366 struct section *sec;
367 struct reloc *reloc;
368 struct instruction *insn;
369
370 /*
371 * By default, "ud2" is a dead end unless otherwise annotated, because
372 * GCC 7 inserts it for certain divide-by-zero cases.
373 */
374 for_each_insn(file, insn)
375 if (insn->type == INSN_BUG)
376 insn->dead_end = true;
377
378 /*
379 * Check for manually annotated dead ends.
380 */
381 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
382 if (!sec)
383 goto reachable;
384
385 list_for_each_entry(reloc, &sec->reloc_list, list) {
386 if (reloc->sym->type != STT_SECTION) {
387 WARN("unexpected relocation symbol type in %s", sec->name);
388 return -1;
389 }
390 insn = find_insn(file, reloc->sym->sec, reloc->addend);
391 if (insn)
392 insn = list_prev_entry(insn, list);
393 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
394 insn = find_last_insn(file, reloc->sym->sec);
395 if (!insn) {
396 WARN("can't find unreachable insn at %s+0x%x",
397 reloc->sym->sec->name, reloc->addend);
398 return -1;
399 }
400 } else {
401 WARN("can't find unreachable insn at %s+0x%x",
402 reloc->sym->sec->name, reloc->addend);
403 return -1;
404 }
405
406 insn->dead_end = true;
407 }
408
409 reachable:
410 /*
411 * These manually annotated reachable checks are needed for GCC 4.4,
412 * where the Linux unreachable() macro isn't supported. In that case
413 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
414 * not a dead end.
415 */
416 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
417 if (!sec)
418 return 0;
419
420 list_for_each_entry(reloc, &sec->reloc_list, list) {
421 if (reloc->sym->type != STT_SECTION) {
422 WARN("unexpected relocation symbol type in %s", sec->name);
423 return -1;
424 }
425 insn = find_insn(file, reloc->sym->sec, reloc->addend);
426 if (insn)
427 insn = list_prev_entry(insn, list);
428 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
429 insn = find_last_insn(file, reloc->sym->sec);
430 if (!insn) {
431 WARN("can't find reachable insn at %s+0x%x",
432 reloc->sym->sec->name, reloc->addend);
433 return -1;
434 }
435 } else {
436 WARN("can't find reachable insn at %s+0x%x",
437 reloc->sym->sec->name, reloc->addend);
438 return -1;
439 }
440
441 insn->dead_end = false;
442 }
443
444 return 0;
445 }
446
447 static int create_static_call_sections(struct objtool_file *file)
448 {
449 struct section *sec;
450 struct static_call_site *site;
451 struct instruction *insn;
452 struct symbol *key_sym;
453 char *key_name, *tmp;
454 int idx;
455
456 sec = find_section_by_name(file->elf, ".static_call_sites");
457 if (sec) {
458 INIT_LIST_HEAD(&file->static_call_list);
459 WARN("file already has .static_call_sites section, skipping");
460 return 0;
461 }
462
463 if (list_empty(&file->static_call_list))
464 return 0;
465
466 idx = 0;
467 list_for_each_entry(insn, &file->static_call_list, call_node)
468 idx++;
469
470 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
471 sizeof(struct static_call_site), idx);
472 if (!sec)
473 return -1;
474
475 idx = 0;
476 list_for_each_entry(insn, &file->static_call_list, call_node) {
477
478 site = (struct static_call_site *)sec->data->d_buf + idx;
479 memset(site, 0, sizeof(struct static_call_site));
480
481 /* populate reloc for 'addr' */
482 if (elf_add_reloc_to_insn(file->elf, sec,
483 idx * sizeof(struct static_call_site),
484 R_X86_64_PC32,
485 insn->sec, insn->offset))
486 return -1;
487
488 /* find key symbol */
489 key_name = strdup(insn->call_dest->name);
490 if (!key_name) {
491 perror("strdup");
492 return -1;
493 }
494 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
495 STATIC_CALL_TRAMP_PREFIX_LEN)) {
496 WARN("static_call: trampoline name malformed: %s", key_name);
497 return -1;
498 }
499 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
500 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
501
502 key_sym = find_symbol_by_name(file->elf, tmp);
503 if (!key_sym) {
504 if (!module) {
505 WARN("static_call: can't find static_call_key symbol: %s", tmp);
506 return -1;
507 }
508
509 /*
510 * For modules(), the key might not be exported, which
511 * means the module can make static calls but isn't
512 * allowed to change them.
513 *
514 * In that case we temporarily set the key to be the
515 * trampoline address. This is fixed up in
516 * static_call_add_module().
517 */
518 key_sym = insn->call_dest;
519 }
520 free(key_name);
521
522 /* populate reloc for 'key' */
523 if (elf_add_reloc(file->elf, sec,
524 idx * sizeof(struct static_call_site) + 4,
525 R_X86_64_PC32, key_sym,
526 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
527 return -1;
528
529 idx++;
530 }
531
532 return 0;
533 }
534
535 static int create_retpoline_sites_sections(struct objtool_file *file)
536 {
537 struct instruction *insn;
538 struct section *sec;
539 int idx;
540
541 sec = find_section_by_name(file->elf, ".retpoline_sites");
542 if (sec) {
543 WARN("file already has .retpoline_sites, skipping");
544 return 0;
545 }
546
547 idx = 0;
548 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
549 idx++;
550
551 if (!idx)
552 return 0;
553
554 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
555 sizeof(int), idx);
556 if (!sec) {
557 WARN("elf_create_section: .retpoline_sites");
558 return -1;
559 }
560
561 idx = 0;
562 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
563
564 int *site = (int *)sec->data->d_buf + idx;
565 *site = 0;
566
567 if (elf_add_reloc_to_insn(file->elf, sec,
568 idx * sizeof(int),
569 R_X86_64_PC32,
570 insn->sec, insn->offset)) {
571 WARN("elf_add_reloc_to_insn: .retpoline_sites");
572 return -1;
573 }
574
575 idx++;
576 }
577
578 return 0;
579 }
580
581 static int create_mcount_loc_sections(struct objtool_file *file)
582 {
583 struct section *sec;
584 unsigned long *loc;
585 struct instruction *insn;
586 int idx;
587
588 sec = find_section_by_name(file->elf, "__mcount_loc");
589 if (sec) {
590 INIT_LIST_HEAD(&file->mcount_loc_list);
591 WARN("file already has __mcount_loc section, skipping");
592 return 0;
593 }
594
595 if (list_empty(&file->mcount_loc_list))
596 return 0;
597
598 idx = 0;
599 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
600 idx++;
601
602 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
603 if (!sec)
604 return -1;
605
606 idx = 0;
607 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
608
609 loc = (unsigned long *)sec->data->d_buf + idx;
610 memset(loc, 0, sizeof(unsigned long));
611
612 if (elf_add_reloc_to_insn(file->elf, sec,
613 idx * sizeof(unsigned long),
614 R_X86_64_64,
615 insn->sec, insn->offset))
616 return -1;
617
618 idx++;
619 }
620
621 return 0;
622 }
623
624 /*
625 * Warnings shouldn't be reported for ignored functions.
626 */
627 static void add_ignores(struct objtool_file *file)
628 {
629 struct instruction *insn;
630 struct section *sec;
631 struct symbol *func;
632 struct reloc *reloc;
633
634 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
635 if (!sec)
636 return;
637
638 list_for_each_entry(reloc, &sec->reloc_list, list) {
639 switch (reloc->sym->type) {
640 case STT_FUNC:
641 func = reloc->sym;
642 break;
643
644 case STT_SECTION:
645 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
646 if (!func)
647 continue;
648 break;
649
650 default:
651 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
652 continue;
653 }
654
655 func_for_each_insn(file, func, insn)
656 insn->ignore = true;
657 }
658 }
659
660 /*
661 * This is a whitelist of functions that is allowed to be called with AC set.
662 * The list is meant to be minimal and only contains compiler instrumentation
663 * ABI and a few functions used to implement *_{to,from}_user() functions.
664 *
665 * These functions must not directly change AC, but may PUSHF/POPF.
666 */
667 static const char *uaccess_safe_builtin[] = {
668 /* KASAN */
669 "kasan_report",
670 "kasan_check_range",
671 /* KASAN out-of-line */
672 "__asan_loadN_noabort",
673 "__asan_load1_noabort",
674 "__asan_load2_noabort",
675 "__asan_load4_noabort",
676 "__asan_load8_noabort",
677 "__asan_load16_noabort",
678 "__asan_storeN_noabort",
679 "__asan_store1_noabort",
680 "__asan_store2_noabort",
681 "__asan_store4_noabort",
682 "__asan_store8_noabort",
683 "__asan_store16_noabort",
684 "__kasan_check_read",
685 "__kasan_check_write",
686 /* KASAN in-line */
687 "__asan_report_load_n_noabort",
688 "__asan_report_load1_noabort",
689 "__asan_report_load2_noabort",
690 "__asan_report_load4_noabort",
691 "__asan_report_load8_noabort",
692 "__asan_report_load16_noabort",
693 "__asan_report_store_n_noabort",
694 "__asan_report_store1_noabort",
695 "__asan_report_store2_noabort",
696 "__asan_report_store4_noabort",
697 "__asan_report_store8_noabort",
698 "__asan_report_store16_noabort",
699 /* KCSAN */
700 "__kcsan_check_access",
701 "kcsan_found_watchpoint",
702 "kcsan_setup_watchpoint",
703 "kcsan_check_scoped_accesses",
704 "kcsan_disable_current",
705 "kcsan_enable_current_nowarn",
706 /* KCSAN/TSAN */
707 "__tsan_func_entry",
708 "__tsan_func_exit",
709 "__tsan_read_range",
710 "__tsan_write_range",
711 "__tsan_read1",
712 "__tsan_read2",
713 "__tsan_read4",
714 "__tsan_read8",
715 "__tsan_read16",
716 "__tsan_write1",
717 "__tsan_write2",
718 "__tsan_write4",
719 "__tsan_write8",
720 "__tsan_write16",
721 "__tsan_read_write1",
722 "__tsan_read_write2",
723 "__tsan_read_write4",
724 "__tsan_read_write8",
725 "__tsan_read_write16",
726 "__tsan_atomic8_load",
727 "__tsan_atomic16_load",
728 "__tsan_atomic32_load",
729 "__tsan_atomic64_load",
730 "__tsan_atomic8_store",
731 "__tsan_atomic16_store",
732 "__tsan_atomic32_store",
733 "__tsan_atomic64_store",
734 "__tsan_atomic8_exchange",
735 "__tsan_atomic16_exchange",
736 "__tsan_atomic32_exchange",
737 "__tsan_atomic64_exchange",
738 "__tsan_atomic8_fetch_add",
739 "__tsan_atomic16_fetch_add",
740 "__tsan_atomic32_fetch_add",
741 "__tsan_atomic64_fetch_add",
742 "__tsan_atomic8_fetch_sub",
743 "__tsan_atomic16_fetch_sub",
744 "__tsan_atomic32_fetch_sub",
745 "__tsan_atomic64_fetch_sub",
746 "__tsan_atomic8_fetch_and",
747 "__tsan_atomic16_fetch_and",
748 "__tsan_atomic32_fetch_and",
749 "__tsan_atomic64_fetch_and",
750 "__tsan_atomic8_fetch_or",
751 "__tsan_atomic16_fetch_or",
752 "__tsan_atomic32_fetch_or",
753 "__tsan_atomic64_fetch_or",
754 "__tsan_atomic8_fetch_xor",
755 "__tsan_atomic16_fetch_xor",
756 "__tsan_atomic32_fetch_xor",
757 "__tsan_atomic64_fetch_xor",
758 "__tsan_atomic8_fetch_nand",
759 "__tsan_atomic16_fetch_nand",
760 "__tsan_atomic32_fetch_nand",
761 "__tsan_atomic64_fetch_nand",
762 "__tsan_atomic8_compare_exchange_strong",
763 "__tsan_atomic16_compare_exchange_strong",
764 "__tsan_atomic32_compare_exchange_strong",
765 "__tsan_atomic64_compare_exchange_strong",
766 "__tsan_atomic8_compare_exchange_weak",
767 "__tsan_atomic16_compare_exchange_weak",
768 "__tsan_atomic32_compare_exchange_weak",
769 "__tsan_atomic64_compare_exchange_weak",
770 "__tsan_atomic8_compare_exchange_val",
771 "__tsan_atomic16_compare_exchange_val",
772 "__tsan_atomic32_compare_exchange_val",
773 "__tsan_atomic64_compare_exchange_val",
774 "__tsan_atomic_thread_fence",
775 "__tsan_atomic_signal_fence",
776 /* KCOV */
777 "write_comp_data",
778 "check_kcov_mode",
779 "__sanitizer_cov_trace_pc",
780 "__sanitizer_cov_trace_const_cmp1",
781 "__sanitizer_cov_trace_const_cmp2",
782 "__sanitizer_cov_trace_const_cmp4",
783 "__sanitizer_cov_trace_const_cmp8",
784 "__sanitizer_cov_trace_cmp1",
785 "__sanitizer_cov_trace_cmp2",
786 "__sanitizer_cov_trace_cmp4",
787 "__sanitizer_cov_trace_cmp8",
788 "__sanitizer_cov_trace_switch",
789 /* UBSAN */
790 "ubsan_type_mismatch_common",
791 "__ubsan_handle_type_mismatch",
792 "__ubsan_handle_type_mismatch_v1",
793 "__ubsan_handle_shift_out_of_bounds",
794 /* misc */
795 "csum_partial_copy_generic",
796 "copy_mc_fragile",
797 "copy_mc_fragile_handle_tail",
798 "copy_mc_enhanced_fast_string",
799 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
800 NULL
801 };
802
803 static void add_uaccess_safe(struct objtool_file *file)
804 {
805 struct symbol *func;
806 const char **name;
807
808 if (!uaccess)
809 return;
810
811 for (name = uaccess_safe_builtin; *name; name++) {
812 func = find_symbol_by_name(file->elf, *name);
813 if (!func)
814 continue;
815
816 func->uaccess_safe = true;
817 }
818 }
819
820 /*
821 * FIXME: For now, just ignore any alternatives which add retpolines. This is
822 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
823 * But it at least allows objtool to understand the control flow *around* the
824 * retpoline.
825 */
826 static int add_ignore_alternatives(struct objtool_file *file)
827 {
828 struct section *sec;
829 struct reloc *reloc;
830 struct instruction *insn;
831
832 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
833 if (!sec)
834 return 0;
835
836 list_for_each_entry(reloc, &sec->reloc_list, list) {
837 if (reloc->sym->type != STT_SECTION) {
838 WARN("unexpected relocation symbol type in %s", sec->name);
839 return -1;
840 }
841
842 insn = find_insn(file, reloc->sym->sec, reloc->addend);
843 if (!insn) {
844 WARN("bad .discard.ignore_alts entry");
845 return -1;
846 }
847
848 insn->ignore_alts = true;
849 }
850
851 return 0;
852 }
853
854 __weak bool arch_is_retpoline(struct symbol *sym)
855 {
856 return false;
857 }
858
859 #define NEGATIVE_RELOC ((void *)-1L)
860
861 static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
862 {
863 if (insn->reloc == NEGATIVE_RELOC)
864 return NULL;
865
866 if (!insn->reloc) {
867 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
868 insn->offset, insn->len);
869 if (!insn->reloc) {
870 insn->reloc = NEGATIVE_RELOC;
871 return NULL;
872 }
873 }
874
875 return insn->reloc;
876 }
877
878 static void remove_insn_ops(struct instruction *insn)
879 {
880 struct stack_op *op, *tmp;
881
882 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
883 list_del(&op->list);
884 free(op);
885 }
886 }
887
888 static void annotate_call_site(struct objtool_file *file,
889 struct instruction *insn, bool sibling)
890 {
891 struct reloc *reloc = insn_reloc(file, insn);
892 struct symbol *sym = insn->call_dest;
893
894 if (!sym)
895 sym = reloc->sym;
896
897 /*
898 * Alternative replacement code is just template code which is
899 * sometimes copied to the original instruction. For now, don't
900 * annotate it. (In the future we might consider annotating the
901 * original instruction if/when it ever makes sense to do so.)
902 */
903 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
904 return;
905
906 if (sym->static_call_tramp) {
907 list_add_tail(&insn->call_node, &file->static_call_list);
908 return;
909 }
910
911 if (sym->retpoline_thunk) {
912 list_add_tail(&insn->call_node, &file->retpoline_call_list);
913 return;
914 }
915
916 /*
917 * Many compilers cannot disable KCOV with a function attribute
918 * so they need a little help, NOP out any KCOV calls from noinstr
919 * text.
920 */
921 if (insn->sec->noinstr && sym->kcov) {
922 if (reloc) {
923 reloc->type = R_NONE;
924 elf_write_reloc(file->elf, reloc);
925 }
926
927 elf_write_insn(file->elf, insn->sec,
928 insn->offset, insn->len,
929 sibling ? arch_ret_insn(insn->len)
930 : arch_nop_insn(insn->len));
931
932 insn->type = sibling ? INSN_RETURN : INSN_NOP;
933 return;
934 }
935
936 if (mcount && sym->fentry) {
937 if (sibling)
938 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
939
940 if (reloc) {
941 reloc->type = R_NONE;
942 elf_write_reloc(file->elf, reloc);
943 }
944
945 elf_write_insn(file->elf, insn->sec,
946 insn->offset, insn->len,
947 arch_nop_insn(insn->len));
948
949 insn->type = INSN_NOP;
950
951 list_add_tail(&insn->call_node, &file->mcount_loc_list);
952 return;
953 }
954 }
955
956 static void add_call_dest(struct objtool_file *file, struct instruction *insn,
957 struct symbol *dest, bool sibling)
958 {
959 insn->call_dest = dest;
960 if (!dest)
961 return;
962
963 /*
964 * Whatever stack impact regular CALLs have, should be undone
965 * by the RETURN of the called function.
966 *
967 * Annotated intra-function calls retain the stack_ops but
968 * are converted to JUMP, see read_intra_function_calls().
969 */
970 remove_insn_ops(insn);
971
972 annotate_call_site(file, insn, sibling);
973 }
974
975 static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
976 {
977 /*
978 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
979 * so convert them accordingly.
980 */
981 switch (insn->type) {
982 case INSN_CALL:
983 insn->type = INSN_CALL_DYNAMIC;
984 break;
985 case INSN_JUMP_UNCONDITIONAL:
986 insn->type = INSN_JUMP_DYNAMIC;
987 break;
988 case INSN_JUMP_CONDITIONAL:
989 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
990 break;
991 default:
992 return;
993 }
994
995 insn->retpoline_safe = true;
996
997 /*
998 * Whatever stack impact regular CALLs have, should be undone
999 * by the RETURN of the called function.
1000 *
1001 * Annotated intra-function calls retain the stack_ops but
1002 * are converted to JUMP, see read_intra_function_calls().
1003 */
1004 remove_insn_ops(insn);
1005
1006 annotate_call_site(file, insn, false);
1007 }
1008 /*
1009 * Find the destination instructions for all jumps.
1010 */
1011 static int add_jump_destinations(struct objtool_file *file)
1012 {
1013 struct instruction *insn;
1014 struct reloc *reloc;
1015 struct section *dest_sec;
1016 unsigned long dest_off;
1017
1018 for_each_insn(file, insn) {
1019 if (!is_static_jump(insn))
1020 continue;
1021
1022 reloc = insn_reloc(file, insn);
1023 if (!reloc) {
1024 dest_sec = insn->sec;
1025 dest_off = arch_jump_destination(insn);
1026 } else if (reloc->sym->type == STT_SECTION) {
1027 dest_sec = reloc->sym->sec;
1028 dest_off = arch_dest_reloc_offset(reloc->addend);
1029 } else if (reloc->sym->retpoline_thunk) {
1030 add_retpoline_call(file, insn);
1031 continue;
1032 } else if (insn->func) {
1033 /* internal or external sibling call (with reloc) */
1034 add_call_dest(file, insn, reloc->sym, true);
1035 continue;
1036 } else if (reloc->sym->sec->idx) {
1037 dest_sec = reloc->sym->sec;
1038 dest_off = reloc->sym->sym.st_value +
1039 arch_dest_reloc_offset(reloc->addend);
1040 } else {
1041 /* non-func asm code jumping to another file */
1042 continue;
1043 }
1044
1045 insn->jump_dest = find_insn(file, dest_sec, dest_off);
1046 if (!insn->jump_dest) {
1047
1048 /*
1049 * This is a special case where an alt instruction
1050 * jumps past the end of the section. These are
1051 * handled later in handle_group_alt().
1052 */
1053 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
1054 continue;
1055
1056 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1057 insn->sec, insn->offset, dest_sec->name,
1058 dest_off);
1059 return -1;
1060 }
1061
1062 /*
1063 * Cross-function jump.
1064 */
1065 if (insn->func && insn->jump_dest->func &&
1066 insn->func != insn->jump_dest->func) {
1067
1068 /*
1069 * For GCC 8+, create parent/child links for any cold
1070 * subfunctions. This is _mostly_ redundant with a
1071 * similar initialization in read_symbols().
1072 *
1073 * If a function has aliases, we want the *first* such
1074 * function in the symbol table to be the subfunction's
1075 * parent. In that case we overwrite the
1076 * initialization done in read_symbols().
1077 *
1078 * However this code can't completely replace the
1079 * read_symbols() code because this doesn't detect the
1080 * case where the parent function's only reference to a
1081 * subfunction is through a jump table.
1082 */
1083 if (!strstr(insn->func->name, ".cold") &&
1084 strstr(insn->jump_dest->func->name, ".cold")) {
1085 insn->func->cfunc = insn->jump_dest->func;
1086 insn->jump_dest->func->pfunc = insn->func;
1087
1088 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
1089 insn->jump_dest->offset == insn->jump_dest->func->offset) {
1090 /* internal sibling call (without reloc) */
1091 add_call_dest(file, insn, insn->jump_dest->func, true);
1092 }
1093 }
1094 }
1095
1096 return 0;
1097 }
1098
1099 static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1100 {
1101 struct symbol *call_dest;
1102
1103 call_dest = find_func_by_offset(sec, offset);
1104 if (!call_dest)
1105 call_dest = find_symbol_by_offset(sec, offset);
1106
1107 return call_dest;
1108 }
1109
1110 /*
1111 * Find the destination instructions for all calls.
1112 */
1113 static int add_call_destinations(struct objtool_file *file)
1114 {
1115 struct instruction *insn;
1116 unsigned long dest_off;
1117 struct symbol *dest;
1118 struct reloc *reloc;
1119
1120 for_each_insn(file, insn) {
1121 if (insn->type != INSN_CALL)
1122 continue;
1123
1124 reloc = insn_reloc(file, insn);
1125 if (!reloc) {
1126 dest_off = arch_jump_destination(insn);
1127 dest = find_call_destination(insn->sec, dest_off);
1128
1129 add_call_dest(file, insn, dest, false);
1130
1131 if (insn->ignore)
1132 continue;
1133
1134 if (!insn->call_dest) {
1135 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
1136 return -1;
1137 }
1138
1139 if (insn->func && insn->call_dest->type != STT_FUNC) {
1140 WARN_FUNC("unsupported call to non-function",
1141 insn->sec, insn->offset);
1142 return -1;
1143 }
1144
1145 } else if (reloc->sym->type == STT_SECTION) {
1146 dest_off = arch_dest_reloc_offset(reloc->addend);
1147 dest = find_call_destination(reloc->sym->sec, dest_off);
1148 if (!dest) {
1149 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
1150 insn->sec, insn->offset,
1151 reloc->sym->sec->name,
1152 dest_off);
1153 return -1;
1154 }
1155
1156 add_call_dest(file, insn, dest, false);
1157
1158 } else if (reloc->sym->retpoline_thunk) {
1159 add_retpoline_call(file, insn);
1160
1161 } else
1162 add_call_dest(file, insn, reloc->sym, false);
1163 }
1164
1165 return 0;
1166 }
1167
1168 /*
1169 * The .alternatives section requires some extra special care over and above
1170 * other special sections because alternatives are patched in place.
1171 */
1172 static int handle_group_alt(struct objtool_file *file,
1173 struct special_alt *special_alt,
1174 struct instruction *orig_insn,
1175 struct instruction **new_insn)
1176 {
1177 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
1178 struct alt_group *orig_alt_group, *new_alt_group;
1179 unsigned long dest_off;
1180
1181
1182 orig_alt_group = malloc(sizeof(*orig_alt_group));
1183 if (!orig_alt_group) {
1184 WARN("malloc failed");
1185 return -1;
1186 }
1187 orig_alt_group->cfi = calloc(special_alt->orig_len,
1188 sizeof(struct cfi_state *));
1189 if (!orig_alt_group->cfi) {
1190 WARN("calloc failed");
1191 return -1;
1192 }
1193
1194 last_orig_insn = NULL;
1195 insn = orig_insn;
1196 sec_for_each_insn_from(file, insn) {
1197 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1198 break;
1199
1200 insn->alt_group = orig_alt_group;
1201 last_orig_insn = insn;
1202 }
1203 orig_alt_group->orig_group = NULL;
1204 orig_alt_group->first_insn = orig_insn;
1205 orig_alt_group->last_insn = last_orig_insn;
1206
1207
1208 new_alt_group = malloc(sizeof(*new_alt_group));
1209 if (!new_alt_group) {
1210 WARN("malloc failed");
1211 return -1;
1212 }
1213
1214 if (special_alt->new_len < special_alt->orig_len) {
1215 /*
1216 * Insert a fake nop at the end to make the replacement
1217 * alt_group the same size as the original. This is needed to
1218 * allow propagate_alt_cfi() to do its magic. When the last
1219 * instruction affects the stack, the instruction after it (the
1220 * nop) will propagate the new state to the shared CFI array.
1221 */
1222 nop = malloc(sizeof(*nop));
1223 if (!nop) {
1224 WARN("malloc failed");
1225 return -1;
1226 }
1227 memset(nop, 0, sizeof(*nop));
1228 INIT_LIST_HEAD(&nop->alts);
1229 INIT_LIST_HEAD(&nop->stack_ops);
1230 init_cfi_state(&nop->cfi);
1231
1232 nop->sec = special_alt->new_sec;
1233 nop->offset = special_alt->new_off + special_alt->new_len;
1234 nop->len = special_alt->orig_len - special_alt->new_len;
1235 nop->type = INSN_NOP;
1236 nop->func = orig_insn->func;
1237 nop->alt_group = new_alt_group;
1238 nop->ignore = orig_insn->ignore_alts;
1239 }
1240
1241 if (!special_alt->new_len) {
1242 *new_insn = nop;
1243 goto end;
1244 }
1245
1246 insn = *new_insn;
1247 sec_for_each_insn_from(file, insn) {
1248 struct reloc *alt_reloc;
1249
1250 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1251 break;
1252
1253 last_new_insn = insn;
1254
1255 insn->ignore = orig_insn->ignore_alts;
1256 insn->func = orig_insn->func;
1257 insn->alt_group = new_alt_group;
1258
1259 /*
1260 * Since alternative replacement code is copy/pasted by the
1261 * kernel after applying relocations, generally such code can't
1262 * have relative-address relocation references to outside the
1263 * .altinstr_replacement section, unless the arch's
1264 * alternatives code can adjust the relative offsets
1265 * accordingly.
1266 */
1267 alt_reloc = insn_reloc(file, insn);
1268 if (alt_reloc &&
1269 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
1270
1271 WARN_FUNC("unsupported relocation in alternatives section",
1272 insn->sec, insn->offset);
1273 return -1;
1274 }
1275
1276 if (!is_static_jump(insn))
1277 continue;
1278
1279 if (!insn->immediate)
1280 continue;
1281
1282 dest_off = arch_jump_destination(insn);
1283 if (dest_off == special_alt->new_off + special_alt->new_len)
1284 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
1285
1286 if (!insn->jump_dest) {
1287 WARN_FUNC("can't find alternative jump destination",
1288 insn->sec, insn->offset);
1289 return -1;
1290 }
1291 }
1292
1293 if (!last_new_insn) {
1294 WARN_FUNC("can't find last new alternative instruction",
1295 special_alt->new_sec, special_alt->new_off);
1296 return -1;
1297 }
1298
1299 if (nop)
1300 list_add(&nop->list, &last_new_insn->list);
1301 end:
1302 new_alt_group->orig_group = orig_alt_group;
1303 new_alt_group->first_insn = *new_insn;
1304 new_alt_group->last_insn = nop ? : last_new_insn;
1305 new_alt_group->cfi = orig_alt_group->cfi;
1306 return 0;
1307 }
1308
1309 /*
1310 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1311 * If the original instruction is a jump, make the alt entry an effective nop
1312 * by just skipping the original instruction.
1313 */
1314 static int handle_jump_alt(struct objtool_file *file,
1315 struct special_alt *special_alt,
1316 struct instruction *orig_insn,
1317 struct instruction **new_insn)
1318 {
1319 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1320 orig_insn->type != INSN_NOP) {
1321
1322 WARN_FUNC("unsupported instruction at jump label",
1323 orig_insn->sec, orig_insn->offset);
1324 return -1;
1325 }
1326
1327 if (special_alt->key_addend & 2) {
1328 struct reloc *reloc = insn_reloc(file, orig_insn);
1329
1330 if (reloc) {
1331 reloc->type = R_NONE;
1332 elf_write_reloc(file->elf, reloc);
1333 }
1334 elf_write_insn(file->elf, orig_insn->sec,
1335 orig_insn->offset, orig_insn->len,
1336 arch_nop_insn(orig_insn->len));
1337 orig_insn->type = INSN_NOP;
1338 }
1339
1340 if (orig_insn->type == INSN_NOP) {
1341 if (orig_insn->len == 2)
1342 file->jl_nop_short++;
1343 else
1344 file->jl_nop_long++;
1345
1346 return 0;
1347 }
1348
1349 if (orig_insn->len == 2)
1350 file->jl_short++;
1351 else
1352 file->jl_long++;
1353
1354 *new_insn = list_next_entry(orig_insn, list);
1355 return 0;
1356 }
1357
1358 /*
1359 * Read all the special sections which have alternate instructions which can be
1360 * patched in or redirected to at runtime. Each instruction having alternate
1361 * instruction(s) has them added to its insn->alts list, which will be
1362 * traversed in validate_branch().
1363 */
1364 static int add_special_section_alts(struct objtool_file *file)
1365 {
1366 struct list_head special_alts;
1367 struct instruction *orig_insn, *new_insn;
1368 struct special_alt *special_alt, *tmp;
1369 struct alternative *alt;
1370 int ret;
1371
1372 ret = special_get_alts(file->elf, &special_alts);
1373 if (ret)
1374 return ret;
1375
1376 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
1377
1378 orig_insn = find_insn(file, special_alt->orig_sec,
1379 special_alt->orig_off);
1380 if (!orig_insn) {
1381 WARN_FUNC("special: can't find orig instruction",
1382 special_alt->orig_sec, special_alt->orig_off);
1383 ret = -1;
1384 goto out;
1385 }
1386
1387 new_insn = NULL;
1388 if (!special_alt->group || special_alt->new_len) {
1389 new_insn = find_insn(file, special_alt->new_sec,
1390 special_alt->new_off);
1391 if (!new_insn) {
1392 WARN_FUNC("special: can't find new instruction",
1393 special_alt->new_sec,
1394 special_alt->new_off);
1395 ret = -1;
1396 goto out;
1397 }
1398 }
1399
1400 if (special_alt->group) {
1401 if (!special_alt->orig_len) {
1402 WARN_FUNC("empty alternative entry",
1403 orig_insn->sec, orig_insn->offset);
1404 continue;
1405 }
1406
1407 ret = handle_group_alt(file, special_alt, orig_insn,
1408 &new_insn);
1409 if (ret)
1410 goto out;
1411 } else if (special_alt->jump_or_nop) {
1412 ret = handle_jump_alt(file, special_alt, orig_insn,
1413 &new_insn);
1414 if (ret)
1415 goto out;
1416 }
1417
1418 alt = malloc(sizeof(*alt));
1419 if (!alt) {
1420 WARN("malloc failed");
1421 ret = -1;
1422 goto out;
1423 }
1424
1425 alt->insn = new_insn;
1426 alt->skip_orig = special_alt->skip_orig;
1427 orig_insn->ignore_alts |= special_alt->skip_alt;
1428 list_add_tail(&alt->list, &orig_insn->alts);
1429
1430 list_del(&special_alt->list);
1431 free(special_alt);
1432 }
1433
1434 if (stats) {
1435 printf("jl\\\tNOP\tJMP\n");
1436 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1437 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1438 }
1439
1440 out:
1441 return ret;
1442 }
1443
1444 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
1445 struct reloc *table)
1446 {
1447 struct reloc *reloc = table;
1448 struct instruction *dest_insn;
1449 struct alternative *alt;
1450 struct symbol *pfunc = insn->func->pfunc;
1451 unsigned int prev_offset = 0;
1452
1453 /*
1454 * Each @reloc is a switch table relocation which points to the target
1455 * instruction.
1456 */
1457 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
1458
1459 /* Check for the end of the table: */
1460 if (reloc != table && reloc->jump_table_start)
1461 break;
1462
1463 /* Make sure the table entries are consecutive: */
1464 if (prev_offset && reloc->offset != prev_offset + 8)
1465 break;
1466
1467 /* Detect function pointers from contiguous objects: */
1468 if (reloc->sym->sec == pfunc->sec &&
1469 reloc->addend == pfunc->offset)
1470 break;
1471
1472 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
1473 if (!dest_insn)
1474 break;
1475
1476 /* Make sure the destination is in the same function: */
1477 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
1478 break;
1479
1480 alt = malloc(sizeof(*alt));
1481 if (!alt) {
1482 WARN("malloc failed");
1483 return -1;
1484 }
1485
1486 alt->insn = dest_insn;
1487 list_add_tail(&alt->list, &insn->alts);
1488 prev_offset = reloc->offset;
1489 }
1490
1491 if (!prev_offset) {
1492 WARN_FUNC("can't find switch jump table",
1493 insn->sec, insn->offset);
1494 return -1;
1495 }
1496
1497 return 0;
1498 }
1499
1500 /*
1501 * find_jump_table() - Given a dynamic jump, find the switch jump table
1502 * associated with it.
1503 */
1504 static struct reloc *find_jump_table(struct objtool_file *file,
1505 struct symbol *func,
1506 struct instruction *insn)
1507 {
1508 struct reloc *table_reloc;
1509 struct instruction *dest_insn, *orig_insn = insn;
1510
1511 /*
1512 * Backward search using the @first_jump_src links, these help avoid
1513 * much of the 'in between' code. Which avoids us getting confused by
1514 * it.
1515 */
1516 for (;
1517 insn && insn->func && insn->func->pfunc == func;
1518 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1519
1520 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1521 break;
1522
1523 /* allow small jumps within the range */
1524 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1525 insn->jump_dest &&
1526 (insn->jump_dest->offset <= insn->offset ||
1527 insn->jump_dest->offset > orig_insn->offset))
1528 break;
1529
1530 table_reloc = arch_find_switch_table(file, insn);
1531 if (!table_reloc)
1532 continue;
1533 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
1534 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1535 continue;
1536
1537 return table_reloc;
1538 }
1539
1540 return NULL;
1541 }
1542
1543 /*
1544 * First pass: Mark the head of each jump table so that in the next pass,
1545 * we know when a given jump table ends and the next one starts.
1546 */
1547 static void mark_func_jump_tables(struct objtool_file *file,
1548 struct symbol *func)
1549 {
1550 struct instruction *insn, *last = NULL;
1551 struct reloc *reloc;
1552
1553 func_for_each_insn(file, func, insn) {
1554 if (!last)
1555 last = insn;
1556
1557 /*
1558 * Store back-pointers for unconditional forward jumps such
1559 * that find_jump_table() can back-track using those and
1560 * avoid some potentially confusing code.
1561 */
1562 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1563 insn->offset > last->offset &&
1564 insn->jump_dest->offset > insn->offset &&
1565 !insn->jump_dest->first_jump_src) {
1566
1567 insn->jump_dest->first_jump_src = insn;
1568 last = insn->jump_dest;
1569 }
1570
1571 if (insn->type != INSN_JUMP_DYNAMIC)
1572 continue;
1573
1574 reloc = find_jump_table(file, func, insn);
1575 if (reloc) {
1576 reloc->jump_table_start = true;
1577 insn->jump_table = reloc;
1578 }
1579 }
1580 }
1581
1582 static int add_func_jump_tables(struct objtool_file *file,
1583 struct symbol *func)
1584 {
1585 struct instruction *insn;
1586 int ret;
1587
1588 func_for_each_insn(file, func, insn) {
1589 if (!insn->jump_table)
1590 continue;
1591
1592 ret = add_jump_table(file, insn, insn->jump_table);
1593 if (ret)
1594 return ret;
1595 }
1596
1597 return 0;
1598 }
1599
1600 /*
1601 * For some switch statements, gcc generates a jump table in the .rodata
1602 * section which contains a list of addresses within the function to jump to.
1603 * This finds these jump tables and adds them to the insn->alts lists.
1604 */
1605 static int add_jump_table_alts(struct objtool_file *file)
1606 {
1607 struct section *sec;
1608 struct symbol *func;
1609 int ret;
1610
1611 if (!file->rodata)
1612 return 0;
1613
1614 for_each_sec(file, sec) {
1615 list_for_each_entry(func, &sec->symbol_list, list) {
1616 if (func->type != STT_FUNC)
1617 continue;
1618
1619 mark_func_jump_tables(file, func);
1620 ret = add_func_jump_tables(file, func);
1621 if (ret)
1622 return ret;
1623 }
1624 }
1625
1626 return 0;
1627 }
1628
1629 static void set_func_state(struct cfi_state *state)
1630 {
1631 state->cfa = initial_func_cfi.cfa;
1632 memcpy(&state->regs, &initial_func_cfi.regs,
1633 CFI_NUM_REGS * sizeof(struct cfi_reg));
1634 state->stack_size = initial_func_cfi.cfa.offset;
1635 }
1636
1637 static int read_unwind_hints(struct objtool_file *file)
1638 {
1639 struct section *sec, *relocsec;
1640 struct reloc *reloc;
1641 struct unwind_hint *hint;
1642 struct instruction *insn;
1643 int i;
1644
1645 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1646 if (!sec)
1647 return 0;
1648
1649 relocsec = sec->reloc;
1650 if (!relocsec) {
1651 WARN("missing .rela.discard.unwind_hints section");
1652 return -1;
1653 }
1654
1655 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
1656 WARN("struct unwind_hint size mismatch");
1657 return -1;
1658 }
1659
1660 file->hints = true;
1661
1662 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
1663 hint = (struct unwind_hint *)sec->data->d_buf + i;
1664
1665 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
1666 if (!reloc) {
1667 WARN("can't find reloc for unwind_hints[%d]", i);
1668 return -1;
1669 }
1670
1671 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1672 if (!insn) {
1673 WARN("can't find insn for unwind_hints[%d]", i);
1674 return -1;
1675 }
1676
1677 insn->hint = true;
1678
1679 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
1680 set_func_state(&insn->cfi);
1681 continue;
1682 }
1683
1684 if (arch_decode_hint_reg(insn, hint->sp_reg)) {
1685 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1686 insn->sec, insn->offset, hint->sp_reg);
1687 return -1;
1688 }
1689
1690 insn->cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
1691 insn->cfi.type = hint->type;
1692 insn->cfi.end = hint->end;
1693 }
1694
1695 return 0;
1696 }
1697
1698 static int read_retpoline_hints(struct objtool_file *file)
1699 {
1700 struct section *sec;
1701 struct instruction *insn;
1702 struct reloc *reloc;
1703
1704 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1705 if (!sec)
1706 return 0;
1707
1708 list_for_each_entry(reloc, &sec->reloc_list, list) {
1709 if (reloc->sym->type != STT_SECTION) {
1710 WARN("unexpected relocation symbol type in %s", sec->name);
1711 return -1;
1712 }
1713
1714 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1715 if (!insn) {
1716 WARN("bad .discard.retpoline_safe entry");
1717 return -1;
1718 }
1719
1720 if (insn->type != INSN_JUMP_DYNAMIC &&
1721 insn->type != INSN_CALL_DYNAMIC) {
1722 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1723 insn->sec, insn->offset);
1724 return -1;
1725 }
1726
1727 insn->retpoline_safe = true;
1728 }
1729
1730 return 0;
1731 }
1732
1733 static int read_instr_hints(struct objtool_file *file)
1734 {
1735 struct section *sec;
1736 struct instruction *insn;
1737 struct reloc *reloc;
1738
1739 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
1740 if (!sec)
1741 return 0;
1742
1743 list_for_each_entry(reloc, &sec->reloc_list, list) {
1744 if (reloc->sym->type != STT_SECTION) {
1745 WARN("unexpected relocation symbol type in %s", sec->name);
1746 return -1;
1747 }
1748
1749 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1750 if (!insn) {
1751 WARN("bad .discard.instr_end entry");
1752 return -1;
1753 }
1754
1755 insn->instr--;
1756 }
1757
1758 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
1759 if (!sec)
1760 return 0;
1761
1762 list_for_each_entry(reloc, &sec->reloc_list, list) {
1763 if (reloc->sym->type != STT_SECTION) {
1764 WARN("unexpected relocation symbol type in %s", sec->name);
1765 return -1;
1766 }
1767
1768 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1769 if (!insn) {
1770 WARN("bad .discard.instr_begin entry");
1771 return -1;
1772 }
1773
1774 insn->instr++;
1775 }
1776
1777 return 0;
1778 }
1779
1780 static int read_intra_function_calls(struct objtool_file *file)
1781 {
1782 struct instruction *insn;
1783 struct section *sec;
1784 struct reloc *reloc;
1785
1786 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
1787 if (!sec)
1788 return 0;
1789
1790 list_for_each_entry(reloc, &sec->reloc_list, list) {
1791 unsigned long dest_off;
1792
1793 if (reloc->sym->type != STT_SECTION) {
1794 WARN("unexpected relocation symbol type in %s",
1795 sec->name);
1796 return -1;
1797 }
1798
1799 insn = find_insn(file, reloc->sym->sec, reloc->addend);
1800 if (!insn) {
1801 WARN("bad .discard.intra_function_call entry");
1802 return -1;
1803 }
1804
1805 if (insn->type != INSN_CALL) {
1806 WARN_FUNC("intra_function_call not a direct call",
1807 insn->sec, insn->offset);
1808 return -1;
1809 }
1810
1811 /*
1812 * Treat intra-function CALLs as JMPs, but with a stack_op.
1813 * See add_call_destinations(), which strips stack_ops from
1814 * normal CALLs.
1815 */
1816 insn->type = INSN_JUMP_UNCONDITIONAL;
1817
1818 dest_off = insn->offset + insn->len + insn->immediate;
1819 insn->jump_dest = find_insn(file, insn->sec, dest_off);
1820 if (!insn->jump_dest) {
1821 WARN_FUNC("can't find call dest at %s+0x%lx",
1822 insn->sec, insn->offset,
1823 insn->sec->name, dest_off);
1824 return -1;
1825 }
1826 }
1827
1828 return 0;
1829 }
1830
1831 static int classify_symbols(struct objtool_file *file)
1832 {
1833 struct section *sec;
1834 struct symbol *func;
1835
1836 for_each_sec(file, sec) {
1837 list_for_each_entry(func, &sec->symbol_list, list) {
1838 if (func->bind != STB_GLOBAL)
1839 continue;
1840
1841 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1842 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
1843 func->static_call_tramp = true;
1844
1845 if (arch_is_retpoline(func))
1846 func->retpoline_thunk = true;
1847
1848 if (!strcmp(func->name, "__fentry__"))
1849 func->fentry = true;
1850
1851 if (!strncmp(func->name, "__sanitizer_cov_", 16))
1852 func->kcov = true;
1853 }
1854 }
1855
1856 return 0;
1857 }
1858
1859 static void mark_rodata(struct objtool_file *file)
1860 {
1861 struct section *sec;
1862 bool found = false;
1863
1864 /*
1865 * Search for the following rodata sections, each of which can
1866 * potentially contain jump tables:
1867 *
1868 * - .rodata: can contain GCC switch tables
1869 * - .rodata.<func>: same, if -fdata-sections is being used
1870 * - .rodata..c_jump_table: contains C annotated jump tables
1871 *
1872 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1873 */
1874 for_each_sec(file, sec) {
1875 if (!strncmp(sec->name, ".rodata", 7) &&
1876 !strstr(sec->name, ".str1.")) {
1877 sec->rodata = true;
1878 found = true;
1879 }
1880 }
1881
1882 file->rodata = found;
1883 }
1884
1885 static int decode_sections(struct objtool_file *file)
1886 {
1887 int ret;
1888
1889 mark_rodata(file);
1890
1891 ret = decode_instructions(file);
1892 if (ret)
1893 return ret;
1894
1895 ret = add_dead_ends(file);
1896 if (ret)
1897 return ret;
1898
1899 add_ignores(file);
1900 add_uaccess_safe(file);
1901
1902 ret = add_ignore_alternatives(file);
1903 if (ret)
1904 return ret;
1905
1906 /*
1907 * Must be before add_{jump_call}_destination.
1908 */
1909 ret = classify_symbols(file);
1910 if (ret)
1911 return ret;
1912
1913 /*
1914 * Must be before add_special_section_alts() as that depends on
1915 * jump_dest being set.
1916 */
1917 ret = add_jump_destinations(file);
1918 if (ret)
1919 return ret;
1920
1921 ret = add_special_section_alts(file);
1922 if (ret)
1923 return ret;
1924
1925 /*
1926 * Must be before add_call_destination(); it changes INSN_CALL to
1927 * INSN_JUMP.
1928 */
1929 ret = read_intra_function_calls(file);
1930 if (ret)
1931 return ret;
1932
1933 ret = add_call_destinations(file);
1934 if (ret)
1935 return ret;
1936
1937 ret = add_jump_table_alts(file);
1938 if (ret)
1939 return ret;
1940
1941 ret = read_unwind_hints(file);
1942 if (ret)
1943 return ret;
1944
1945 ret = read_retpoline_hints(file);
1946 if (ret)
1947 return ret;
1948
1949 ret = read_instr_hints(file);
1950 if (ret)
1951 return ret;
1952
1953 return 0;
1954 }
1955
1956 static bool is_fentry_call(struct instruction *insn)
1957 {
1958 if (insn->type == INSN_CALL &&
1959 insn->call_dest &&
1960 insn->call_dest->fentry)
1961 return true;
1962
1963 return false;
1964 }
1965
1966 static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
1967 {
1968 struct cfi_state *cfi = &state->cfi;
1969 int i;
1970
1971 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
1972 return true;
1973
1974 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
1975 return true;
1976
1977 if (cfi->stack_size != initial_func_cfi.cfa.offset)
1978 return true;
1979
1980 for (i = 0; i < CFI_NUM_REGS; i++) {
1981 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
1982 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
1983 return true;
1984 }
1985
1986 return false;
1987 }
1988
1989 static bool check_reg_frame_pos(const struct cfi_reg *reg,
1990 int expected_offset)
1991 {
1992 return reg->base == CFI_CFA &&
1993 reg->offset == expected_offset;
1994 }
1995
1996 static bool has_valid_stack_frame(struct insn_state *state)
1997 {
1998 struct cfi_state *cfi = &state->cfi;
1999
2000 if (cfi->cfa.base == CFI_BP &&
2001 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2002 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
2003 return true;
2004
2005 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
2006 return true;
2007
2008 return false;
2009 }
2010
2011 static int update_cfi_state_regs(struct instruction *insn,
2012 struct cfi_state *cfi,
2013 struct stack_op *op)
2014 {
2015 struct cfi_reg *cfa = &cfi->cfa;
2016
2017 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
2018 return 0;
2019
2020 /* push */
2021 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
2022 cfa->offset += 8;
2023
2024 /* pop */
2025 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
2026 cfa->offset -= 8;
2027
2028 /* add immediate to sp */
2029 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2030 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2031 cfa->offset -= op->src.offset;
2032
2033 return 0;
2034 }
2035
2036 static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
2037 {
2038 if (arch_callee_saved_reg(reg) &&
2039 cfi->regs[reg].base == CFI_UNDEFINED) {
2040 cfi->regs[reg].base = base;
2041 cfi->regs[reg].offset = offset;
2042 }
2043 }
2044
2045 static void restore_reg(struct cfi_state *cfi, unsigned char reg)
2046 {
2047 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2048 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
2049 }
2050
2051 /*
2052 * A note about DRAP stack alignment:
2053 *
2054 * GCC has the concept of a DRAP register, which is used to help keep track of
2055 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2056 * register. The typical DRAP pattern is:
2057 *
2058 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2059 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2060 * 41 ff 72 f8 pushq -0x8(%r10)
2061 * 55 push %rbp
2062 * 48 89 e5 mov %rsp,%rbp
2063 * (more pushes)
2064 * 41 52 push %r10
2065 * ...
2066 * 41 5a pop %r10
2067 * (more pops)
2068 * 5d pop %rbp
2069 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2070 * c3 retq
2071 *
2072 * There are some variations in the epilogues, like:
2073 *
2074 * 5b pop %rbx
2075 * 41 5a pop %r10
2076 * 41 5c pop %r12
2077 * 41 5d pop %r13
2078 * 41 5e pop %r14
2079 * c9 leaveq
2080 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2081 * c3 retq
2082 *
2083 * and:
2084 *
2085 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2086 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2087 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2088 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2089 * c9 leaveq
2090 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2091 * c3 retq
2092 *
2093 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2094 * restored beforehand:
2095 *
2096 * 41 55 push %r13
2097 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2098 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2099 * ...
2100 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2101 * 41 5d pop %r13
2102 * c3 retq
2103 */
2104 static int update_cfi_state(struct instruction *insn,
2105 struct instruction *next_insn,
2106 struct cfi_state *cfi, struct stack_op *op)
2107 {
2108 struct cfi_reg *cfa = &cfi->cfa;
2109 struct cfi_reg *regs = cfi->regs;
2110
2111 /* stack operations don't make sense with an undefined CFA */
2112 if (cfa->base == CFI_UNDEFINED) {
2113 if (insn->func) {
2114 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2115 return -1;
2116 }
2117 return 0;
2118 }
2119
2120 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2121 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
2122 return update_cfi_state_regs(insn, cfi, op);
2123
2124 switch (op->dest.type) {
2125
2126 case OP_DEST_REG:
2127 switch (op->src.type) {
2128
2129 case OP_SRC_REG:
2130 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2131 cfa->base == CFI_SP &&
2132 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
2133
2134 /* mov %rsp, %rbp */
2135 cfa->base = op->dest.reg;
2136 cfi->bp_scratch = false;
2137 }
2138
2139 else if (op->src.reg == CFI_SP &&
2140 op->dest.reg == CFI_BP && cfi->drap) {
2141
2142 /* drap: mov %rsp, %rbp */
2143 regs[CFI_BP].base = CFI_BP;
2144 regs[CFI_BP].offset = -cfi->stack_size;
2145 cfi->bp_scratch = false;
2146 }
2147
2148 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2149
2150 /*
2151 * mov %rsp, %reg
2152 *
2153 * This is needed for the rare case where GCC
2154 * does:
2155 *
2156 * mov %rsp, %rax
2157 * ...
2158 * mov %rax, %rsp
2159 */
2160 cfi->vals[op->dest.reg].base = CFI_CFA;
2161 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
2162 }
2163
2164 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
2165 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
2166
2167 /*
2168 * mov %rbp, %rsp
2169 *
2170 * Restore the original stack pointer (Clang).
2171 */
2172 cfi->stack_size = -cfi->regs[CFI_BP].offset;
2173 }
2174
2175 else if (op->dest.reg == cfa->base) {
2176
2177 /* mov %reg, %rsp */
2178 if (cfa->base == CFI_SP &&
2179 cfi->vals[op->src.reg].base == CFI_CFA) {
2180
2181 /*
2182 * This is needed for the rare case
2183 * where GCC does something dumb like:
2184 *
2185 * lea 0x8(%rsp), %rcx
2186 * ...
2187 * mov %rcx, %rsp
2188 */
2189 cfa->offset = -cfi->vals[op->src.reg].offset;
2190 cfi->stack_size = cfa->offset;
2191
2192 } else if (cfa->base == CFI_SP &&
2193 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2194 cfi->vals[op->src.reg].offset == cfa->offset) {
2195
2196 /*
2197 * Stack swizzle:
2198 *
2199 * 1: mov %rsp, (%[tos])
2200 * 2: mov %[tos], %rsp
2201 * ...
2202 * 3: pop %rsp
2203 *
2204 * Where:
2205 *
2206 * 1 - places a pointer to the previous
2207 * stack at the Top-of-Stack of the
2208 * new stack.
2209 *
2210 * 2 - switches to the new stack.
2211 *
2212 * 3 - pops the Top-of-Stack to restore
2213 * the original stack.
2214 *
2215 * Note: we set base to SP_INDIRECT
2216 * here and preserve offset. Therefore
2217 * when the unwinder reaches ToS it
2218 * will dereference SP and then add the
2219 * offset to find the next frame, IOW:
2220 * (%rsp) + offset.
2221 */
2222 cfa->base = CFI_SP_INDIRECT;
2223
2224 } else {
2225 cfa->base = CFI_UNDEFINED;
2226 cfa->offset = 0;
2227 }
2228 }
2229
2230 else if (op->dest.reg == CFI_SP &&
2231 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2232 cfi->vals[op->src.reg].offset == cfa->offset) {
2233
2234 /*
2235 * The same stack swizzle case 2) as above. But
2236 * because we can't change cfa->base, case 3)
2237 * will become a regular POP. Pretend we're a
2238 * PUSH so things don't go unbalanced.
2239 */
2240 cfi->stack_size += 8;
2241 }
2242
2243
2244 break;
2245
2246 case OP_SRC_ADD:
2247 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2248
2249 /* add imm, %rsp */
2250 cfi->stack_size -= op->src.offset;
2251 if (cfa->base == CFI_SP)
2252 cfa->offset -= op->src.offset;
2253 break;
2254 }
2255
2256 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2257
2258 /* lea disp(%rbp), %rsp */
2259 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
2260 break;
2261 }
2262
2263 if (!cfi->drap && op->src.reg == CFI_SP &&
2264 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2265 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
2266
2267 /* lea disp(%rsp), %rbp */
2268 cfa->base = CFI_BP;
2269 cfa->offset -= op->src.offset;
2270 cfi->bp_scratch = false;
2271 break;
2272 }
2273
2274 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2275
2276 /* drap: lea disp(%rsp), %drap */
2277 cfi->drap_reg = op->dest.reg;
2278
2279 /*
2280 * lea disp(%rsp), %reg
2281 *
2282 * This is needed for the rare case where GCC
2283 * does something dumb like:
2284 *
2285 * lea 0x8(%rsp), %rcx
2286 * ...
2287 * mov %rcx, %rsp
2288 */
2289 cfi->vals[op->dest.reg].base = CFI_CFA;
2290 cfi->vals[op->dest.reg].offset = \
2291 -cfi->stack_size + op->src.offset;
2292
2293 break;
2294 }
2295
2296 if (cfi->drap && op->dest.reg == CFI_SP &&
2297 op->src.reg == cfi->drap_reg) {
2298
2299 /* drap: lea disp(%drap), %rsp */
2300 cfa->base = CFI_SP;
2301 cfa->offset = cfi->stack_size = -op->src.offset;
2302 cfi->drap_reg = CFI_UNDEFINED;
2303 cfi->drap = false;
2304 break;
2305 }
2306
2307 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
2308 WARN_FUNC("unsupported stack register modification",
2309 insn->sec, insn->offset);
2310 return -1;
2311 }
2312
2313 break;
2314
2315 case OP_SRC_AND:
2316 if (op->dest.reg != CFI_SP ||
2317 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2318 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
2319 WARN_FUNC("unsupported stack pointer realignment",
2320 insn->sec, insn->offset);
2321 return -1;
2322 }
2323
2324 if (cfi->drap_reg != CFI_UNDEFINED) {
2325 /* drap: and imm, %rsp */
2326 cfa->base = cfi->drap_reg;
2327 cfa->offset = cfi->stack_size = 0;
2328 cfi->drap = true;
2329 }
2330
2331 /*
2332 * Older versions of GCC (4.8ish) realign the stack
2333 * without DRAP, with a frame pointer.
2334 */
2335
2336 break;
2337
2338 case OP_SRC_POP:
2339 case OP_SRC_POPF:
2340 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2341
2342 /* pop %rsp; # restore from a stack swizzle */
2343 cfa->base = CFI_SP;
2344 break;
2345 }
2346
2347 if (!cfi->drap && op->dest.reg == cfa->base) {
2348
2349 /* pop %rbp */
2350 cfa->base = CFI_SP;
2351 }
2352
2353 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2354 op->dest.reg == cfi->drap_reg &&
2355 cfi->drap_offset == -cfi->stack_size) {
2356
2357 /* drap: pop %drap */
2358 cfa->base = cfi->drap_reg;
2359 cfa->offset = 0;
2360 cfi->drap_offset = -1;
2361
2362 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
2363
2364 /* pop %reg */
2365 restore_reg(cfi, op->dest.reg);
2366 }
2367
2368 cfi->stack_size -= 8;
2369 if (cfa->base == CFI_SP)
2370 cfa->offset -= 8;
2371
2372 break;
2373
2374 case OP_SRC_REG_INDIRECT:
2375 if (!cfi->drap && op->dest.reg == cfa->base &&
2376 op->dest.reg == CFI_BP) {
2377
2378 /* mov disp(%rsp), %rbp */
2379 cfa->base = CFI_SP;
2380 cfa->offset = cfi->stack_size;
2381 }
2382
2383 if (cfi->drap && op->src.reg == CFI_BP &&
2384 op->src.offset == cfi->drap_offset) {
2385
2386 /* drap: mov disp(%rbp), %drap */
2387 cfa->base = cfi->drap_reg;
2388 cfa->offset = 0;
2389 cfi->drap_offset = -1;
2390 }
2391
2392 if (cfi->drap && op->src.reg == CFI_BP &&
2393 op->src.offset == regs[op->dest.reg].offset) {
2394
2395 /* drap: mov disp(%rbp), %reg */
2396 restore_reg(cfi, op->dest.reg);
2397
2398 } else if (op->src.reg == cfa->base &&
2399 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2400
2401 /* mov disp(%rbp), %reg */
2402 /* mov disp(%rsp), %reg */
2403 restore_reg(cfi, op->dest.reg);
2404
2405 } else if (op->src.reg == CFI_SP &&
2406 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2407
2408 /* mov disp(%rsp), %reg */
2409 restore_reg(cfi, op->dest.reg);
2410 }
2411
2412 break;
2413
2414 default:
2415 WARN_FUNC("unknown stack-related instruction",
2416 insn->sec, insn->offset);
2417 return -1;
2418 }
2419
2420 break;
2421
2422 case OP_DEST_PUSH:
2423 case OP_DEST_PUSHF:
2424 cfi->stack_size += 8;
2425 if (cfa->base == CFI_SP)
2426 cfa->offset += 8;
2427
2428 if (op->src.type != OP_SRC_REG)
2429 break;
2430
2431 if (cfi->drap) {
2432 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2433
2434 /* drap: push %drap */
2435 cfa->base = CFI_BP_INDIRECT;
2436 cfa->offset = -cfi->stack_size;
2437
2438 /* save drap so we know when to restore it */
2439 cfi->drap_offset = -cfi->stack_size;
2440
2441 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
2442
2443 /* drap: push %rbp */
2444 cfi->stack_size = 0;
2445
2446 } else {
2447
2448 /* drap: push %reg */
2449 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
2450 }
2451
2452 } else {
2453
2454 /* push %reg */
2455 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
2456 }
2457
2458 /* detect when asm code uses rbp as a scratch register */
2459 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
2460 cfa->base != CFI_BP)
2461 cfi->bp_scratch = true;
2462 break;
2463
2464 case OP_DEST_REG_INDIRECT:
2465
2466 if (cfi->drap) {
2467 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
2468
2469 /* drap: mov %drap, disp(%rbp) */
2470 cfa->base = CFI_BP_INDIRECT;
2471 cfa->offset = op->dest.offset;
2472
2473 /* save drap offset so we know when to restore it */
2474 cfi->drap_offset = op->dest.offset;
2475 } else {
2476
2477 /* drap: mov reg, disp(%rbp) */
2478 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
2479 }
2480
2481 } else if (op->dest.reg == cfa->base) {
2482
2483 /* mov reg, disp(%rbp) */
2484 /* mov reg, disp(%rsp) */
2485 save_reg(cfi, op->src.reg, CFI_CFA,
2486 op->dest.offset - cfi->cfa.offset);
2487
2488 } else if (op->dest.reg == CFI_SP) {
2489
2490 /* mov reg, disp(%rsp) */
2491 save_reg(cfi, op->src.reg, CFI_CFA,
2492 op->dest.offset - cfi->stack_size);
2493
2494 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2495
2496 /* mov %rsp, (%reg); # setup a stack swizzle. */
2497 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2498 cfi->vals[op->dest.reg].offset = cfa->offset;
2499 }
2500
2501 break;
2502
2503 case OP_DEST_MEM:
2504 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
2505 WARN_FUNC("unknown stack-related memory operation",
2506 insn->sec, insn->offset);
2507 return -1;
2508 }
2509
2510 /* pop mem */
2511 cfi->stack_size -= 8;
2512 if (cfa->base == CFI_SP)
2513 cfa->offset -= 8;
2514
2515 break;
2516
2517 default:
2518 WARN_FUNC("unknown stack-related instruction",
2519 insn->sec, insn->offset);
2520 return -1;
2521 }
2522
2523 return 0;
2524 }
2525
2526 /*
2527 * The stack layouts of alternatives instructions can sometimes diverge when
2528 * they have stack modifications. That's fine as long as the potential stack
2529 * layouts don't conflict at any given potential instruction boundary.
2530 *
2531 * Flatten the CFIs of the different alternative code streams (both original
2532 * and replacement) into a single shared CFI array which can be used to detect
2533 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2534 */
2535 static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
2536 {
2537 struct cfi_state **alt_cfi;
2538 int group_off;
2539
2540 if (!insn->alt_group)
2541 return 0;
2542
2543 alt_cfi = insn->alt_group->cfi;
2544 group_off = insn->offset - insn->alt_group->first_insn->offset;
2545
2546 if (!alt_cfi[group_off]) {
2547 alt_cfi[group_off] = &insn->cfi;
2548 } else {
2549 if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) {
2550 WARN_FUNC("stack layout conflict in alternatives",
2551 insn->sec, insn->offset);
2552 return -1;
2553 }
2554 }
2555
2556 return 0;
2557 }
2558
2559 static int handle_insn_ops(struct instruction *insn,
2560 struct instruction *next_insn,
2561 struct insn_state *state)
2562 {
2563 struct stack_op *op;
2564
2565 list_for_each_entry(op, &insn->stack_ops, list) {
2566
2567 if (update_cfi_state(insn, next_insn, &state->cfi, op))
2568 return 1;
2569
2570 if (!insn->alt_group)
2571 continue;
2572
2573 if (op->dest.type == OP_DEST_PUSHF) {
2574 if (!state->uaccess_stack) {
2575 state->uaccess_stack = 1;
2576 } else if (state->uaccess_stack >> 31) {
2577 WARN_FUNC("PUSHF stack exhausted",
2578 insn->sec, insn->offset);
2579 return 1;
2580 }
2581 state->uaccess_stack <<= 1;
2582 state->uaccess_stack |= state->uaccess;
2583 }
2584
2585 if (op->src.type == OP_SRC_POPF) {
2586 if (state->uaccess_stack) {
2587 state->uaccess = state->uaccess_stack & 1;
2588 state->uaccess_stack >>= 1;
2589 if (state->uaccess_stack == 1)
2590 state->uaccess_stack = 0;
2591 }
2592 }
2593 }
2594
2595 return 0;
2596 }
2597
2598 static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
2599 {
2600 struct cfi_state *cfi1 = &insn->cfi;
2601 int i;
2602
2603 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
2604
2605 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
2606 insn->sec, insn->offset,
2607 cfi1->cfa.base, cfi1->cfa.offset,
2608 cfi2->cfa.base, cfi2->cfa.offset);
2609
2610 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
2611 for (i = 0; i < CFI_NUM_REGS; i++) {
2612 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
2613 sizeof(struct cfi_reg)))
2614 continue;
2615
2616 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
2617 insn->sec, insn->offset,
2618 i, cfi1->regs[i].base, cfi1->regs[i].offset,
2619 i, cfi2->regs[i].base, cfi2->regs[i].offset);
2620 break;
2621 }
2622
2623 } else if (cfi1->type != cfi2->type) {
2624
2625 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
2626 insn->sec, insn->offset, cfi1->type, cfi2->type);
2627
2628 } else if (cfi1->drap != cfi2->drap ||
2629 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
2630 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
2631
2632 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
2633 insn->sec, insn->offset,
2634 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
2635 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
2636
2637 } else
2638 return true;
2639
2640 return false;
2641 }
2642
2643 static inline bool func_uaccess_safe(struct symbol *func)
2644 {
2645 if (func)
2646 return func->uaccess_safe;
2647
2648 return false;
2649 }
2650
2651 static inline const char *call_dest_name(struct instruction *insn)
2652 {
2653 if (insn->call_dest)
2654 return insn->call_dest->name;
2655
2656 return "{dynamic}";
2657 }
2658
2659 static inline bool noinstr_call_dest(struct symbol *func)
2660 {
2661 /*
2662 * We can't deal with indirect function calls at present;
2663 * assume they're instrumented.
2664 */
2665 if (!func)
2666 return false;
2667
2668 /*
2669 * If the symbol is from a noinstr section; we good.
2670 */
2671 if (func->sec->noinstr)
2672 return true;
2673
2674 /*
2675 * The __ubsan_handle_*() calls are like WARN(), they only happen when
2676 * something 'BAD' happened. At the risk of taking the machine down,
2677 * let them proceed to get the message out.
2678 */
2679 if (!strncmp(func->name, "__ubsan_handle_", 15))
2680 return true;
2681
2682 return false;
2683 }
2684
2685 static int validate_call(struct instruction *insn, struct insn_state *state)
2686 {
2687 if (state->noinstr && state->instr <= 0 &&
2688 !noinstr_call_dest(insn->call_dest)) {
2689 WARN_FUNC("call to %s() leaves .noinstr.text section",
2690 insn->sec, insn->offset, call_dest_name(insn));
2691 return 1;
2692 }
2693
2694 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
2695 WARN_FUNC("call to %s() with UACCESS enabled",
2696 insn->sec, insn->offset, call_dest_name(insn));
2697 return 1;
2698 }
2699
2700 if (state->df) {
2701 WARN_FUNC("call to %s() with DF set",
2702 insn->sec, insn->offset, call_dest_name(insn));
2703 return 1;
2704 }
2705
2706 return 0;
2707 }
2708
2709 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
2710 {
2711 if (has_modified_stack_frame(insn, state)) {
2712 WARN_FUNC("sibling call from callable instruction with modified stack frame",
2713 insn->sec, insn->offset);
2714 return 1;
2715 }
2716
2717 return validate_call(insn, state);
2718 }
2719
2720 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2721 {
2722 if (state->noinstr && state->instr > 0) {
2723 WARN_FUNC("return with instrumentation enabled",
2724 insn->sec, insn->offset);
2725 return 1;
2726 }
2727
2728 if (state->uaccess && !func_uaccess_safe(func)) {
2729 WARN_FUNC("return with UACCESS enabled",
2730 insn->sec, insn->offset);
2731 return 1;
2732 }
2733
2734 if (!state->uaccess && func_uaccess_safe(func)) {
2735 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2736 insn->sec, insn->offset);
2737 return 1;
2738 }
2739
2740 if (state->df) {
2741 WARN_FUNC("return with DF set",
2742 insn->sec, insn->offset);
2743 return 1;
2744 }
2745
2746 if (func && has_modified_stack_frame(insn, state)) {
2747 WARN_FUNC("return with modified stack frame",
2748 insn->sec, insn->offset);
2749 return 1;
2750 }
2751
2752 if (state->cfi.bp_scratch) {
2753 WARN_FUNC("BP used as a scratch register",
2754 insn->sec, insn->offset);
2755 return 1;
2756 }
2757
2758 return 0;
2759 }
2760
2761 static struct instruction *next_insn_to_validate(struct objtool_file *file,
2762 struct instruction *insn)
2763 {
2764 struct alt_group *alt_group = insn->alt_group;
2765
2766 /*
2767 * Simulate the fact that alternatives are patched in-place. When the
2768 * end of a replacement alt_group is reached, redirect objtool flow to
2769 * the end of the original alt_group.
2770 */
2771 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
2772 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
2773
2774 return next_insn_same_sec(file, insn);
2775 }
2776
2777 /*
2778 * Follow the branch starting at the given instruction, and recursively follow
2779 * any other branches (jumps). Meanwhile, track the frame pointer state at
2780 * each instruction and validate all the rules described in
2781 * tools/objtool/Documentation/stack-validation.txt.
2782 */
2783 static int validate_branch(struct objtool_file *file, struct symbol *func,
2784 struct instruction *insn, struct insn_state state)
2785 {
2786 struct alternative *alt;
2787 struct instruction *next_insn;
2788 struct section *sec;
2789 u8 visited;
2790 int ret;
2791
2792 sec = insn->sec;
2793
2794 while (1) {
2795 next_insn = next_insn_to_validate(file, insn);
2796
2797 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2798 WARN("%s() falls through to next function %s()",
2799 func->name, insn->func->name);
2800 return 1;
2801 }
2802
2803 if (func && insn->ignore) {
2804 WARN_FUNC("BUG: why am I validating an ignored function?",
2805 sec, insn->offset);
2806 return 1;
2807 }
2808
2809 visited = 1 << state.uaccess;
2810 if (insn->visited) {
2811 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
2812 return 1;
2813
2814 if (insn->visited & visited)
2815 return 0;
2816 }
2817
2818 if (state.noinstr)
2819 state.instr += insn->instr;
2820
2821 if (insn->hint)
2822 state.cfi = insn->cfi;
2823 else
2824 insn->cfi = state.cfi;
2825
2826 insn->visited |= visited;
2827
2828 if (propagate_alt_cfi(file, insn))
2829 return 1;
2830
2831 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
2832 bool skip_orig = false;
2833
2834 list_for_each_entry(alt, &insn->alts, list) {
2835 if (alt->skip_orig)
2836 skip_orig = true;
2837
2838 ret = validate_branch(file, func, alt->insn, state);
2839 if (ret) {
2840 if (backtrace)
2841 BT_FUNC("(alt)", insn);
2842 return ret;
2843 }
2844 }
2845
2846 if (skip_orig)
2847 return 0;
2848 }
2849
2850 if (handle_insn_ops(insn, next_insn, &state))
2851 return 1;
2852
2853 switch (insn->type) {
2854
2855 case INSN_RETURN:
2856 return validate_return(func, insn, &state);
2857
2858 case INSN_CALL:
2859 case INSN_CALL_DYNAMIC:
2860 ret = validate_call(insn, &state);
2861 if (ret)
2862 return ret;
2863
2864 if (!no_fp && func && !is_fentry_call(insn) &&
2865 !has_valid_stack_frame(&state)) {
2866 WARN_FUNC("call without frame pointer save/setup",
2867 sec, insn->offset);
2868 return 1;
2869 }
2870
2871 if (dead_end_function(file, insn->call_dest))
2872 return 0;
2873
2874 break;
2875
2876 case INSN_JUMP_CONDITIONAL:
2877 case INSN_JUMP_UNCONDITIONAL:
2878 if (is_sibling_call(insn)) {
2879 ret = validate_sibling_call(insn, &state);
2880 if (ret)
2881 return ret;
2882
2883 } else if (insn->jump_dest) {
2884 ret = validate_branch(file, func,
2885 insn->jump_dest, state);
2886 if (ret) {
2887 if (backtrace)
2888 BT_FUNC("(branch)", insn);
2889 return ret;
2890 }
2891 }
2892
2893 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2894 return 0;
2895
2896 break;
2897
2898 case INSN_JUMP_DYNAMIC:
2899 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2900 if (is_sibling_call(insn)) {
2901 ret = validate_sibling_call(insn, &state);
2902 if (ret)
2903 return ret;
2904 }
2905
2906 if (insn->type == INSN_JUMP_DYNAMIC)
2907 return 0;
2908
2909 break;
2910
2911 case INSN_CONTEXT_SWITCH:
2912 if (func && (!next_insn || !next_insn->hint)) {
2913 WARN_FUNC("unsupported instruction in callable function",
2914 sec, insn->offset);
2915 return 1;
2916 }
2917 return 0;
2918
2919 case INSN_STAC:
2920 if (state.uaccess) {
2921 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2922 return 1;
2923 }
2924
2925 state.uaccess = true;
2926 break;
2927
2928 case INSN_CLAC:
2929 if (!state.uaccess && func) {
2930 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2931 return 1;
2932 }
2933
2934 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2935 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2936 return 1;
2937 }
2938
2939 state.uaccess = false;
2940 break;
2941
2942 case INSN_STD:
2943 if (state.df) {
2944 WARN_FUNC("recursive STD", sec, insn->offset);
2945 return 1;
2946 }
2947
2948 state.df = true;
2949 break;
2950
2951 case INSN_CLD:
2952 if (!state.df && func) {
2953 WARN_FUNC("redundant CLD", sec, insn->offset);
2954 return 1;
2955 }
2956
2957 state.df = false;
2958 break;
2959
2960 default:
2961 break;
2962 }
2963
2964 if (insn->dead_end)
2965 return 0;
2966
2967 if (!next_insn) {
2968 if (state.cfi.cfa.base == CFI_UNDEFINED)
2969 return 0;
2970 WARN("%s: unexpected end of section", sec->name);
2971 return 1;
2972 }
2973
2974 insn = next_insn;
2975 }
2976
2977 return 0;
2978 }
2979
2980 static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
2981 {
2982 struct instruction *insn;
2983 struct insn_state state;
2984 int ret, warnings = 0;
2985
2986 if (!file->hints)
2987 return 0;
2988
2989 init_insn_state(&state, sec);
2990
2991 if (sec) {
2992 insn = find_insn(file, sec, 0);
2993 if (!insn)
2994 return 0;
2995 } else {
2996 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
2997 }
2998
2999 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
3000 if (insn->hint && !insn->visited) {
3001 ret = validate_branch(file, insn->func, insn, state);
3002 if (ret && backtrace)
3003 BT_FUNC("<=== (hint)", insn);
3004 warnings += ret;
3005 }
3006
3007 insn = list_next_entry(insn, list);
3008 }
3009
3010 return warnings;
3011 }
3012
3013 static int validate_retpoline(struct objtool_file *file)
3014 {
3015 struct instruction *insn;
3016 int warnings = 0;
3017
3018 for_each_insn(file, insn) {
3019 if (insn->type != INSN_JUMP_DYNAMIC &&
3020 insn->type != INSN_CALL_DYNAMIC)
3021 continue;
3022
3023 if (insn->retpoline_safe)
3024 continue;
3025
3026 /*
3027 * .init.text code is ran before userspace and thus doesn't
3028 * strictly need retpolines, except for modules which are
3029 * loaded late, they very much do need retpoline in their
3030 * .init.text
3031 */
3032 if (!strcmp(insn->sec->name, ".init.text") && !module)
3033 continue;
3034
3035 WARN_FUNC("indirect %s found in RETPOLINE build",
3036 insn->sec, insn->offset,
3037 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3038
3039 warnings++;
3040 }
3041
3042 return warnings;
3043 }
3044
3045 static bool is_kasan_insn(struct instruction *insn)
3046 {
3047 return (insn->type == INSN_CALL &&
3048 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3049 }
3050
3051 static bool is_ubsan_insn(struct instruction *insn)
3052 {
3053 return (insn->type == INSN_CALL &&
3054 !strcmp(insn->call_dest->name,
3055 "__ubsan_handle_builtin_unreachable"));
3056 }
3057
3058 static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
3059 {
3060 int i;
3061 struct instruction *prev_insn;
3062
3063 if (insn->ignore || insn->type == INSN_NOP)
3064 return true;
3065
3066 /*
3067 * Ignore any unused exceptions. This can happen when a whitelisted
3068 * function has an exception table entry.
3069 *
3070 * Also ignore alternative replacement instructions. This can happen
3071 * when a whitelisted function uses one of the ALTERNATIVE macros.
3072 */
3073 if (!strcmp(insn->sec->name, ".fixup") ||
3074 !strcmp(insn->sec->name, ".altinstr_replacement") ||
3075 !strcmp(insn->sec->name, ".altinstr_aux"))
3076 return true;
3077
3078 if (!insn->func)
3079 return false;
3080
3081 /*
3082 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3083 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3084 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3085 * (or occasionally a JMP to UD2).
3086 *
3087 * It may also insert a UD2 after calling a __noreturn function.
3088 */
3089 prev_insn = list_prev_entry(insn, list);
3090 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
3091 (insn->type == INSN_BUG ||
3092 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3093 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3094 return true;
3095
3096 /*
3097 * Check if this (or a subsequent) instruction is related to
3098 * CONFIG_UBSAN or CONFIG_KASAN.
3099 *
3100 * End the search at 5 instructions to avoid going into the weeds.
3101 */
3102 for (i = 0; i < 5; i++) {
3103
3104 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3105 return true;
3106
3107 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3108 if (insn->jump_dest &&
3109 insn->jump_dest->func == insn->func) {
3110 insn = insn->jump_dest;
3111 continue;
3112 }
3113
3114 break;
3115 }
3116
3117 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
3118 break;
3119
3120 insn = list_next_entry(insn, list);
3121 }
3122
3123 return false;
3124 }
3125
3126 static int validate_symbol(struct objtool_file *file, struct section *sec,
3127 struct symbol *sym, struct insn_state *state)
3128 {
3129 struct instruction *insn;
3130 int ret;
3131
3132 if (!sym->len) {
3133 WARN("%s() is missing an ELF size annotation", sym->name);
3134 return 1;
3135 }
3136
3137 if (sym->pfunc != sym || sym->alias != sym)
3138 return 0;
3139
3140 insn = find_insn(file, sec, sym->offset);
3141 if (!insn || insn->ignore || insn->visited)
3142 return 0;
3143
3144 state->uaccess = sym->uaccess_safe;
3145
3146 ret = validate_branch(file, insn->func, insn, *state);
3147 if (ret && backtrace)
3148 BT_FUNC("<=== (sym)", insn);
3149 return ret;
3150 }
3151
3152 static int validate_section(struct objtool_file *file, struct section *sec)
3153 {
3154 struct insn_state state;
3155 struct symbol *func;
3156 int warnings = 0;
3157
3158 list_for_each_entry(func, &sec->symbol_list, list) {
3159 if (func->type != STT_FUNC)
3160 continue;
3161
3162 init_insn_state(&state, sec);
3163 set_func_state(&state.cfi);
3164
3165 warnings += validate_symbol(file, sec, func, &state);
3166 }
3167
3168 return warnings;
3169 }
3170
3171 static int validate_vmlinux_functions(struct objtool_file *file)
3172 {
3173 struct section *sec;
3174 int warnings = 0;
3175
3176 sec = find_section_by_name(file->elf, ".noinstr.text");
3177 if (sec) {
3178 warnings += validate_section(file, sec);
3179 warnings += validate_unwind_hints(file, sec);
3180 }
3181
3182 sec = find_section_by_name(file->elf, ".entry.text");
3183 if (sec) {
3184 warnings += validate_section(file, sec);
3185 warnings += validate_unwind_hints(file, sec);
3186 }
3187
3188 return warnings;
3189 }
3190
3191 static int validate_functions(struct objtool_file *file)
3192 {
3193 struct section *sec;
3194 int warnings = 0;
3195
3196 for_each_sec(file, sec) {
3197 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3198 continue;
3199
3200 warnings += validate_section(file, sec);
3201 }
3202
3203 return warnings;
3204 }
3205
3206 static int validate_reachable_instructions(struct objtool_file *file)
3207 {
3208 struct instruction *insn;
3209
3210 if (file->ignore_unreachables)
3211 return 0;
3212
3213 for_each_insn(file, insn) {
3214 if (insn->visited || ignore_unreachable_insn(file, insn))
3215 continue;
3216
3217 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
3218 return 1;
3219 }
3220
3221 return 0;
3222 }
3223
3224 int check(struct objtool_file *file)
3225 {
3226 int ret, warnings = 0;
3227
3228 arch_initial_func_cfi_state(&initial_func_cfi);
3229
3230 ret = decode_sections(file);
3231 if (ret < 0)
3232 goto out;
3233 warnings += ret;
3234
3235 if (list_empty(&file->insn_list))
3236 goto out;
3237
3238 if (vmlinux && !validate_dup) {
3239 ret = validate_vmlinux_functions(file);
3240 if (ret < 0)
3241 goto out;
3242
3243 warnings += ret;
3244 goto out;
3245 }
3246
3247 if (retpoline) {
3248 ret = validate_retpoline(file);
3249 if (ret < 0)
3250 return ret;
3251 warnings += ret;
3252 }
3253
3254 ret = validate_functions(file);
3255 if (ret < 0)
3256 goto out;
3257 warnings += ret;
3258
3259 ret = validate_unwind_hints(file, NULL);
3260 if (ret < 0)
3261 goto out;
3262 warnings += ret;
3263
3264 if (!warnings) {
3265 ret = validate_reachable_instructions(file);
3266 if (ret < 0)
3267 goto out;
3268 warnings += ret;
3269 }
3270
3271 ret = create_static_call_sections(file);
3272 if (ret < 0)
3273 goto out;
3274 warnings += ret;
3275
3276 if (retpoline) {
3277 ret = create_retpoline_sites_sections(file);
3278 if (ret < 0)
3279 goto out;
3280 warnings += ret;
3281 }
3282
3283 if (mcount) {
3284 ret = create_mcount_loc_sections(file);
3285 if (ret < 0)
3286 goto out;
3287 warnings += ret;
3288 }
3289
3290 out:
3291 /*
3292 * For now, don't fail the kernel build on fatal warnings. These
3293 * errors are still fairly common due to the growing matrix of
3294 * supported toolchains and their recent pace of change.
3295 */
3296 return 0;
3297 }