]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/objtool/check.c
x86/speculation: Remove x86_spec_ctrl_mask
[thirdparty/linux.git] / tools / objtool / check.c
CommitLineData
1ccea77e 1// SPDX-License-Identifier: GPL-2.0-or-later
dcc914f4
JP
2/*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
dcc914f4
JP
4 */
5
6#include <string.h>
7#include <stdlib.h>
22682a07 8#include <inttypes.h>
8b946cc3 9#include <sys/mman.h>
dcc914f4 10
7786032e
VG
11#include <arch/elf.h>
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>
dcc914f4 19
ee819aed 20#include <linux/objtool.h>
dcc914f4
JP
21#include <linux/hashtable.h>
22#include <linux/kernel.h>
1e7e4788 23#include <linux/static_call_types.h>
dcc914f4 24
dcc914f4
JP
25struct alternative {
26 struct list_head list;
27 struct instruction *insn;
764eef4b 28 bool skip_orig;
dcc914f4
JP
29};
30
8b946cc3
PZ
31static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
32
33static struct cfi_init_state initial_func_cfi;
34static struct cfi_state init_cfi;
35static struct cfi_state func_cfi;
dcc914f4 36
627fce14
JP
37struct instruction *find_insn(struct objtool_file *file,
38 struct section *sec, unsigned long offset)
dcc914f4
JP
39{
40 struct instruction *insn;
41
87ecb582 42 hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
dcc914f4
JP
43 if (insn->sec == sec && insn->offset == offset)
44 return insn;
87ecb582 45 }
dcc914f4
JP
46
47 return NULL;
48}
49
50static struct instruction *next_insn_same_sec(struct objtool_file *file,
51 struct instruction *insn)
52{
53 struct instruction *next = list_next_entry(insn, list);
54
baa41469 55 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
dcc914f4
JP
56 return NULL;
57
58 return next;
59}
60
13810435
JP
61static struct instruction *next_insn_same_func(struct objtool_file *file,
62 struct instruction *insn)
63{
64 struct instruction *next = list_next_entry(insn, list);
65 struct symbol *func = insn->func;
66
67 if (!func)
68 return NULL;
69
70 if (&next->list != &file->insn_list && next->func == func)
71 return next;
72
73 /* Check if we're already in the subfunction: */
74 if (func == func->cfunc)
75 return NULL;
76
77 /* Move to the subfunction: */
78 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
79}
80
1119d265
JP
81static struct instruction *prev_insn_same_sym(struct objtool_file *file,
82 struct instruction *insn)
83{
84 struct instruction *prev = list_prev_entry(insn, list);
85
86 if (&prev->list != &file->insn_list && prev->func == insn->func)
87 return prev;
88
89 return NULL;
90}
91
f0f70adb 92#define func_for_each_insn(file, func, insn) \
13810435
JP
93 for (insn = find_insn(file, func->sec, func->offset); \
94 insn; \
95 insn = next_insn_same_func(file, insn))
96
dbf4aeb0
PZ
97#define sym_for_each_insn(file, sym, insn) \
98 for (insn = find_insn(file, sym->sec, sym->offset); \
dcc914f4 99 insn && &insn->list != &file->insn_list && \
dbf4aeb0
PZ
100 insn->sec == sym->sec && \
101 insn->offset < sym->offset + sym->len; \
dcc914f4
JP
102 insn = list_next_entry(insn, list))
103
dbf4aeb0 104#define sym_for_each_insn_continue_reverse(file, sym, insn) \
dcc914f4
JP
105 for (insn = list_prev_entry(insn, list); \
106 &insn->list != &file->insn_list && \
dbf4aeb0 107 insn->sec == sym->sec && insn->offset >= sym->offset; \
dcc914f4
JP
108 insn = list_prev_entry(insn, list))
109
110#define sec_for_each_insn_from(file, insn) \
111 for (; insn; insn = next_insn_same_sec(file, insn))
112
baa41469
JP
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))
dcc914f4 116
99033461
JP
117static bool is_jump_table_jump(struct instruction *insn)
118{
119 struct alt_group *alt_group = insn->alt_group;
120
121 if (insn->jump_table)
122 return true;
123
124 /* Retpoline alternative for a jump table? */
125 return alt_group && alt_group->orig_group &&
126 alt_group->orig_group->first_insn->jump_table;
127}
128
0c1ddd33
JP
129static bool is_sibling_call(struct instruction *insn)
130{
ecf11ba4
JP
131 /*
132 * Assume only ELF functions can make sibling calls. This ensures
133 * sibling call detection consistency between vmlinux.o and individual
134 * objects.
135 */
136 if (!insn->func)
137 return false;
138
0c1ddd33
JP
139 /* An indirect jump is either a sibling call or a jump to a table. */
140 if (insn->type == INSN_JUMP_DYNAMIC)
99033461 141 return !is_jump_table_jump(insn);
0c1ddd33 142
0c1ddd33 143 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
ecf11ba4 144 return (is_static_jump(insn) && insn->call_dest);
0c1ddd33
JP
145}
146
dcc914f4
JP
147/*
148 * This checks to see if the given function is a "noreturn" function.
149 *
150 * For global functions which are outside the scope of this object file, we
151 * have to keep a manual list of them.
152 *
153 * For local functions, we have to detect them manually by simply looking for
154 * the lack of a return instruction.
dcc914f4 155 */
8e25c9f8
JP
156static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
157 int recursion)
dcc914f4
JP
158{
159 int i;
160 struct instruction *insn;
161 bool empty = true;
162
163 /*
164 * Unfortunately these have to be hard coded because the noreturn
165 * attribute isn't provided in ELF data.
166 */
167 static const char * const global_noreturns[] = {
168 "__stack_chk_fail",
169 "panic",
170 "do_exit",
171 "do_task_dead",
bbda86e9 172 "kthread_exit",
0e25498f 173 "make_task_dead",
ca3574bd 174 "__module_put_and_kthread_exit",
cead1855 175 "kthread_complete_and_exit",
dcc914f4
JP
176 "__reiserfs_panic",
177 "lbug_with_loc",
178 "fortify_panic",
b394d468 179 "usercopy_abort",
684fb246 180 "machine_real_restart",
1fb466df 181 "rewind_stack_and_make_dead",
33adf80f 182 "kunit_try_catch_throw",
c26acfbb 183 "xen_start_kernel",
9af9dcf1 184 "cpu_bringup_and_idle",
eae654f1 185 "do_group_exit",
f9cdf7ca 186 "stop_this_cpu",
105cd685 187 "__invalid_creds",
385bd430
PZ
188 "cpu_startup_entry",
189 "__ubsan_handle_builtin_unreachable",
2028a255 190 "ex_handler_msr_mce",
dcc914f4
JP
191 };
192
c9bab22b
JP
193 if (!func)
194 return false;
195
dcc914f4 196 if (func->bind == STB_WEAK)
8e25c9f8 197 return false;
dcc914f4
JP
198
199 if (func->bind == STB_GLOBAL)
200 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
201 if (!strcmp(func->name, global_noreturns[i]))
8e25c9f8 202 return true;
dcc914f4 203
13810435 204 if (!func->len)
8e25c9f8 205 return false;
dcc914f4 206
13810435
JP
207 insn = find_insn(file, func->sec, func->offset);
208 if (!insn->func)
8e25c9f8 209 return false;
13810435 210
f0f70adb 211 func_for_each_insn(file, func, insn) {
dcc914f4
JP
212 empty = false;
213
214 if (insn->type == INSN_RETURN)
8e25c9f8 215 return false;
dcc914f4
JP
216 }
217
218 if (empty)
8e25c9f8 219 return false;
dcc914f4
JP
220
221 /*
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.
225 */
f0f70adb 226 func_for_each_insn(file, func, insn) {
0c1ddd33 227 if (is_sibling_call(insn)) {
dcc914f4 228 struct instruction *dest = insn->jump_dest;
dcc914f4
JP
229
230 if (!dest)
231 /* sibling call to another file */
8e25c9f8 232 return false;
dcc914f4 233
0c1ddd33
JP
234 /* local sibling call */
235 if (recursion == 5) {
236 /*
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.
240 */
241 return false;
dcc914f4 242 }
dcc914f4 243
0c1ddd33
JP
244 return __dead_end_function(file, dest->func, recursion+1);
245 }
dcc914f4
JP
246 }
247
8e25c9f8 248 return true;
dcc914f4
JP
249}
250
8e25c9f8 251static bool dead_end_function(struct objtool_file *file, struct symbol *func)
dcc914f4
JP
252{
253 return __dead_end_function(file, func, 0);
254}
255
e7c0219b 256static void init_cfi_state(struct cfi_state *cfi)
baa41469
JP
257{
258 int i;
259
dd88a0a0 260 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b
PZ
261 cfi->regs[i].base = CFI_UNDEFINED;
262 cfi->vals[i].base = CFI_UNDEFINED;
dd88a0a0 263 }
e7c0219b
PZ
264 cfi->cfa.base = CFI_UNDEFINED;
265 cfi->drap_reg = CFI_UNDEFINED;
266 cfi->drap_offset = -1;
267}
268
753da417
JP
269static void init_insn_state(struct objtool_file *file, struct insn_state *state,
270 struct section *sec)
e7c0219b
PZ
271{
272 memset(state, 0, sizeof(*state));
273 init_cfi_state(&state->cfi);
932f8e98
PZ
274
275 /*
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).
279 */
753da417 280 if (opts.link && opts.noinstr && sec)
932f8e98 281 state->noinstr = sec->noinstr;
baa41469
JP
282}
283
8b946cc3
PZ
284static struct cfi_state *cfi_alloc(void)
285{
286 struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
287 if (!cfi) {
288 WARN("calloc failed");
289 exit(1);
290 }
291 nr_cfi++;
292 return cfi;
293}
294
295static int cfi_bits;
296static struct hlist_head *cfi_hash;
297
298static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
299{
300 return memcmp((void *)cfi1 + sizeof(cfi1->hash),
301 (void *)cfi2 + sizeof(cfi2->hash),
302 sizeof(struct cfi_state) - sizeof(struct hlist_node));
303}
304
305static inline u32 cfi_key(struct cfi_state *cfi)
306{
307 return jhash((void *)cfi + sizeof(cfi->hash),
308 sizeof(*cfi) - sizeof(cfi->hash), 0);
309}
310
311static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
312{
313 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
314 struct cfi_state *obj;
315
316 hlist_for_each_entry(obj, head, hash) {
317 if (!cficmp(cfi, obj)) {
318 nr_cfi_cache++;
319 return obj;
320 }
321 }
322
323 obj = cfi_alloc();
324 *obj = *cfi;
325 hlist_add_head(&obj->hash, head);
326
327 return obj;
328}
329
330static void cfi_hash_add(struct cfi_state *cfi)
331{
332 struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
333
334 hlist_add_head(&cfi->hash, head);
335}
336
337static void *cfi_hash_alloc(unsigned long size)
338{
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");
345 cfi_hash = NULL;
2daf7fab 346 } else if (opts.stats) {
8b946cc3
PZ
347 printf("cfi_bits: %d\n", cfi_bits);
348 }
349
350 return cfi_hash;
351}
352
353static unsigned long nr_insns;
354static unsigned long nr_insns_visited;
355
dcc914f4
JP
356/*
357 * Call the arch-specific instruction decoder for all the instructions and add
358 * them to the global instruction list.
359 */
360static int decode_instructions(struct objtool_file *file)
361{
362 struct section *sec;
363 struct symbol *func;
364 unsigned long offset;
365 struct instruction *insn;
366 int ret;
367
baa41469 368 for_each_sec(file, sec) {
dcc914f4
JP
369
370 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
371 continue;
372
627fce14
JP
373 if (strcmp(sec->name, ".altinstr_replacement") &&
374 strcmp(sec->name, ".altinstr_aux") &&
375 strncmp(sec->name, ".discard.", 9))
376 sec->text = true;
377
0cc9ac8d 378 if (!strcmp(sec->name, ".noinstr.text") ||
951ddecf
PZ
379 !strcmp(sec->name, ".entry.text") ||
380 !strncmp(sec->name, ".text.__x86.", 12))
c4a33939
PZ
381 sec->noinstr = true;
382
fe255fe6 383 for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
dcc914f4 384 insn = malloc(sizeof(*insn));
baa41469
JP
385 if (!insn) {
386 WARN("malloc failed");
387 return -1;
388 }
dcc914f4 389 memset(insn, 0, sizeof(*insn));
dcc914f4 390 INIT_LIST_HEAD(&insn->alts);
65ea47dc 391 INIT_LIST_HEAD(&insn->stack_ops);
89bc853e 392 INIT_LIST_HEAD(&insn->call_node);
baa41469 393
dcc914f4
JP
394 insn->sec = sec;
395 insn->offset = offset;
396
db2b0c5d 397 ret = arch_decode_instruction(file, sec, offset,
fe255fe6 398 sec->sh.sh_size - offset,
dcc914f4 399 &insn->len, &insn->type,
baa41469 400 &insn->immediate,
65ea47dc 401 &insn->stack_ops);
dcc914f4 402 if (ret)
b7037983 403 goto err;
dcc914f4 404
0e5b613b
PZ
405 /*
406 * By default, "ud2" is a dead end unless otherwise
407 * annotated, because GCC 7 inserts it for certain
408 * divide-by-zero cases.
409 */
410 if (insn->type == INSN_BUG)
411 insn->dead_end = true;
412
87ecb582 413 hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
dcc914f4 414 list_add_tail(&insn->list, &file->insn_list);
1e11f3fd 415 nr_insns++;
dcc914f4
JP
416 }
417
418 list_for_each_entry(func, &sec->symbol_list, list) {
e10cd8fe 419 if (func->type != STT_FUNC || func->alias != func)
dcc914f4
JP
420 continue;
421
422 if (!find_insn(file, sec, func->offset)) {
423 WARN("%s(): can't find starting instruction",
424 func->name);
425 return -1;
426 }
427
08f87a93 428 sym_for_each_insn(file, func, insn) {
e10cd8fe 429 insn->func = func;
89bc853e 430 if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) {
08f87a93 431 if (insn->offset == insn->func->offset) {
89bc853e 432 list_add_tail(&insn->call_node, &file->endbr_list);
08f87a93
PZ
433 file->nr_endbr++;
434 } else {
435 file->nr_endbr_int++;
436 }
437 }
438 }
dcc914f4
JP
439 }
440 }
441
2daf7fab 442 if (opts.stats)
1e11f3fd
PZ
443 printf("nr_insns: %lu\n", nr_insns);
444
dcc914f4 445 return 0;
b7037983
KB
446
447err:
448 free(insn);
449 return ret;
dcc914f4
JP
450}
451
db2b0c5d
PZ
452/*
453 * Read the pv_ops[] .data table to find the static initialized values.
454 */
455static int add_pv_ops(struct objtool_file *file, const char *symname)
456{
457 struct symbol *sym, *func;
458 unsigned long off, end;
459 struct reloc *rel;
460 int idx;
461
462 sym = find_symbol_by_name(file->elf, symname);
463 if (!sym)
464 return 0;
465
466 off = sym->offset;
467 end = off + sym->len;
468 for (;;) {
469 rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
470 if (!rel)
471 break;
472
473 func = rel->sym;
474 if (func->type == STT_SECTION)
475 func = find_symbol_by_offset(rel->sym->sec, rel->addend);
476
477 idx = (rel->offset - sym->offset) / sizeof(unsigned long);
478
479 objtool_pv_add(file, idx, func);
480
481 off = rel->offset + 1;
482 if (off > end)
483 break;
484 }
485
486 return 0;
487}
488
489/*
490 * Allocate and initialize file->pv_ops[].
491 */
492static int init_pv_ops(struct objtool_file *file)
493{
494 static const char *pv_ops_tables[] = {
495 "pv_ops",
496 "xen_cpu_ops",
497 "xen_irq_ops",
498 "xen_mmu_ops",
499 NULL,
500 };
501 const char *pv_ops;
502 struct symbol *sym;
503 int idx, nr;
504
2daf7fab 505 if (!opts.noinstr)
db2b0c5d
PZ
506 return 0;
507
508 file->pv_ops = NULL;
509
510 sym = find_symbol_by_name(file->elf, "pv_ops");
511 if (!sym)
512 return 0;
513
514 nr = sym->len / sizeof(unsigned long);
515 file->pv_ops = calloc(sizeof(struct pv_state), nr);
516 if (!file->pv_ops)
517 return -1;
518
519 for (idx = 0; idx < nr; idx++)
520 INIT_LIST_HEAD(&file->pv_ops[idx].targets);
521
522 for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
523 add_pv_ops(file, pv_ops);
524
525 return 0;
526}
527
6b5dd716
ST
528static struct instruction *find_last_insn(struct objtool_file *file,
529 struct section *sec)
530{
531 struct instruction *insn = NULL;
532 unsigned int offset;
fe255fe6 533 unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
6b5dd716 534
fe255fe6 535 for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
6b5dd716
ST
536 insn = find_insn(file, sec, offset);
537
538 return insn;
539}
540
dcc914f4 541/*
649ea4d5 542 * Mark "ud2" instructions and manually annotated dead ends.
dcc914f4
JP
543 */
544static int add_dead_ends(struct objtool_file *file)
545{
546 struct section *sec;
f1974222 547 struct reloc *reloc;
dcc914f4 548 struct instruction *insn;
dcc914f4 549
649ea4d5
JP
550 /*
551 * Check for manually annotated dead ends.
552 */
dcc914f4
JP
553 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
554 if (!sec)
649ea4d5 555 goto reachable;
dcc914f4 556
f1974222
MH
557 list_for_each_entry(reloc, &sec->reloc_list, list) {
558 if (reloc->sym->type != STT_SECTION) {
dcc914f4
JP
559 WARN("unexpected relocation symbol type in %s", sec->name);
560 return -1;
561 }
f1974222 562 insn = find_insn(file, reloc->sym->sec, reloc->addend);
dcc914f4
JP
563 if (insn)
564 insn = list_prev_entry(insn, list);
fe255fe6 565 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
f1974222 566 insn = find_last_insn(file, reloc->sym->sec);
6b5dd716 567 if (!insn) {
22682a07 568 WARN("can't find unreachable insn at %s+0x%" PRIx64,
f1974222 569 reloc->sym->sec->name, reloc->addend);
dcc914f4
JP
570 return -1;
571 }
572 } else {
22682a07 573 WARN("can't find unreachable insn at %s+0x%" PRIx64,
f1974222 574 reloc->sym->sec->name, reloc->addend);
dcc914f4
JP
575 return -1;
576 }
577
578 insn->dead_end = true;
579 }
580
649ea4d5
JP
581reachable:
582 /*
583 * These manually annotated reachable checks are needed for GCC 4.4,
584 * where the Linux unreachable() macro isn't supported. In that case
585 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
586 * not a dead end.
587 */
588 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
589 if (!sec)
590 return 0;
591
f1974222
MH
592 list_for_each_entry(reloc, &sec->reloc_list, list) {
593 if (reloc->sym->type != STT_SECTION) {
649ea4d5
JP
594 WARN("unexpected relocation symbol type in %s", sec->name);
595 return -1;
596 }
f1974222 597 insn = find_insn(file, reloc->sym->sec, reloc->addend);
649ea4d5
JP
598 if (insn)
599 insn = list_prev_entry(insn, list);
fe255fe6 600 else if (reloc->addend == reloc->sym->sec->sh.sh_size) {
f1974222 601 insn = find_last_insn(file, reloc->sym->sec);
6b5dd716 602 if (!insn) {
22682a07 603 WARN("can't find reachable insn at %s+0x%" PRIx64,
f1974222 604 reloc->sym->sec->name, reloc->addend);
649ea4d5
JP
605 return -1;
606 }
607 } else {
22682a07 608 WARN("can't find reachable insn at %s+0x%" PRIx64,
f1974222 609 reloc->sym->sec->name, reloc->addend);
649ea4d5
JP
610 return -1;
611 }
612
613 insn->dead_end = false;
614 }
615
dcc914f4
JP
616 return 0;
617}
618
1e7e4788
JP
619static int create_static_call_sections(struct objtool_file *file)
620{
ef47cc01 621 struct section *sec;
1e7e4788
JP
622 struct static_call_site *site;
623 struct instruction *insn;
624 struct symbol *key_sym;
625 char *key_name, *tmp;
626 int idx;
627
628 sec = find_section_by_name(file->elf, ".static_call_sites");
629 if (sec) {
630 INIT_LIST_HEAD(&file->static_call_list);
631 WARN("file already has .static_call_sites section, skipping");
632 return 0;
633 }
634
635 if (list_empty(&file->static_call_list))
636 return 0;
637
638 idx = 0;
43d5430a 639 list_for_each_entry(insn, &file->static_call_list, call_node)
1e7e4788
JP
640 idx++;
641
642 sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE,
643 sizeof(struct static_call_site), idx);
644 if (!sec)
645 return -1;
646
1e7e4788 647 idx = 0;
43d5430a 648 list_for_each_entry(insn, &file->static_call_list, call_node) {
1e7e4788
JP
649
650 site = (struct static_call_site *)sec->data->d_buf + idx;
651 memset(site, 0, sizeof(struct static_call_site));
652
653 /* populate reloc for 'addr' */
ef47cc01
PZ
654 if (elf_add_reloc_to_insn(file->elf, sec,
655 idx * sizeof(struct static_call_site),
656 R_X86_64_PC32,
657 insn->sec, insn->offset))
44f6a7c0 658 return -1;
1e7e4788
JP
659
660 /* find key symbol */
661 key_name = strdup(insn->call_dest->name);
662 if (!key_name) {
663 perror("strdup");
664 return -1;
665 }
666 if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
667 STATIC_CALL_TRAMP_PREFIX_LEN)) {
668 WARN("static_call: trampoline name malformed: %s", key_name);
669 return -1;
670 }
671 tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
672 memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
673
674 key_sym = find_symbol_by_name(file->elf, tmp);
675 if (!key_sym) {
2daf7fab 676 if (!opts.module) {
73f44fe1
JP
677 WARN("static_call: can't find static_call_key symbol: %s", tmp);
678 return -1;
679 }
680
681 /*
682 * For modules(), the key might not be exported, which
683 * means the module can make static calls but isn't
684 * allowed to change them.
685 *
686 * In that case we temporarily set the key to be the
687 * trampoline address. This is fixed up in
688 * static_call_add_module().
689 */
690 key_sym = insn->call_dest;
1e7e4788
JP
691 }
692 free(key_name);
693
694 /* populate reloc for 'key' */
ef47cc01
PZ
695 if (elf_add_reloc(file->elf, sec,
696 idx * sizeof(struct static_call_site) + 4,
697 R_X86_64_PC32, key_sym,
698 is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
1e7e4788 699 return -1;
1e7e4788
JP
700
701 idx++;
702 }
703
1e7e4788
JP
704 return 0;
705}
706
134ab5bd
PZ
707static int create_retpoline_sites_sections(struct objtool_file *file)
708{
709 struct instruction *insn;
710 struct section *sec;
711 int idx;
712
713 sec = find_section_by_name(file->elf, ".retpoline_sites");
714 if (sec) {
715 WARN("file already has .retpoline_sites, skipping");
716 return 0;
717 }
718
719 idx = 0;
720 list_for_each_entry(insn, &file->retpoline_call_list, call_node)
721 idx++;
722
723 if (!idx)
724 return 0;
725
726 sec = elf_create_section(file->elf, ".retpoline_sites", 0,
727 sizeof(int), idx);
728 if (!sec) {
729 WARN("elf_create_section: .retpoline_sites");
730 return -1;
731 }
732
733 idx = 0;
734 list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
735
736 int *site = (int *)sec->data->d_buf + idx;
737 *site = 0;
738
739 if (elf_add_reloc_to_insn(file->elf, sec,
740 idx * sizeof(int),
741 R_X86_64_PC32,
742 insn->sec, insn->offset)) {
743 WARN("elf_add_reloc_to_insn: .retpoline_sites");
744 return -1;
745 }
746
747 idx++;
748 }
749
750 return 0;
751}
752
d9e9d230
PZ
753static int create_return_sites_sections(struct objtool_file *file)
754{
755 struct instruction *insn;
756 struct section *sec;
757 int idx;
758
759 sec = find_section_by_name(file->elf, ".return_sites");
760 if (sec) {
761 WARN("file already has .return_sites, skipping");
762 return 0;
763 }
764
765 idx = 0;
766 list_for_each_entry(insn, &file->return_thunk_list, call_node)
767 idx++;
768
769 if (!idx)
770 return 0;
771
772 sec = elf_create_section(file->elf, ".return_sites", 0,
773 sizeof(int), idx);
774 if (!sec) {
775 WARN("elf_create_section: .return_sites");
776 return -1;
777 }
778
779 idx = 0;
780 list_for_each_entry(insn, &file->return_thunk_list, call_node) {
781
782 int *site = (int *)sec->data->d_buf + idx;
783 *site = 0;
784
785 if (elf_add_reloc_to_insn(file->elf, sec,
786 idx * sizeof(int),
787 R_X86_64_PC32,
788 insn->sec, insn->offset)) {
789 WARN("elf_add_reloc_to_insn: .return_sites");
790 return -1;
791 }
792
793 idx++;
794 }
795
796 return 0;
797}
798
89bc853e
PZ
799static int create_ibt_endbr_seal_sections(struct objtool_file *file)
800{
801 struct instruction *insn;
802 struct section *sec;
803 int idx;
804
805 sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
806 if (sec) {
807 WARN("file already has .ibt_endbr_seal, skipping");
808 return 0;
809 }
810
811 idx = 0;
812 list_for_each_entry(insn, &file->endbr_list, call_node)
813 idx++;
814
2daf7fab 815 if (opts.stats) {
89bc853e
PZ
816 printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
817 printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
818 printf("ibt: superfluous ENDBR: %d\n", idx);
819 }
820
821 if (!idx)
822 return 0;
823
824 sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0,
825 sizeof(int), idx);
826 if (!sec) {
827 WARN("elf_create_section: .ibt_endbr_seal");
828 return -1;
829 }
830
831 idx = 0;
832 list_for_each_entry(insn, &file->endbr_list, call_node) {
833
834 int *site = (int *)sec->data->d_buf + idx;
835 *site = 0;
836
837 if (elf_add_reloc_to_insn(file->elf, sec,
838 idx * sizeof(int),
839 R_X86_64_PC32,
840 insn->sec, insn->offset)) {
841 WARN("elf_add_reloc_to_insn: .ibt_endbr_seal");
842 return -1;
843 }
844
845 idx++;
846 }
847
848 return 0;
849}
850
99d00215
PZ
851static int create_mcount_loc_sections(struct objtool_file *file)
852{
ef47cc01 853 struct section *sec;
99d00215
PZ
854 unsigned long *loc;
855 struct instruction *insn;
856 int idx;
857
858 sec = find_section_by_name(file->elf, "__mcount_loc");
859 if (sec) {
860 INIT_LIST_HEAD(&file->mcount_loc_list);
861 WARN("file already has __mcount_loc section, skipping");
862 return 0;
863 }
864
865 if (list_empty(&file->mcount_loc_list))
866 return 0;
867
868 idx = 0;
c509331b 869 list_for_each_entry(insn, &file->mcount_loc_list, call_node)
99d00215
PZ
870 idx++;
871
872 sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
873 if (!sec)
874 return -1;
875
99d00215 876 idx = 0;
c509331b 877 list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
99d00215
PZ
878
879 loc = (unsigned long *)sec->data->d_buf + idx;
880 memset(loc, 0, sizeof(unsigned long));
881
ef47cc01
PZ
882 if (elf_add_reloc_to_insn(file->elf, sec,
883 idx * sizeof(unsigned long),
884 R_X86_64_64,
885 insn->sec, insn->offset))
99d00215 886 return -1;
99d00215
PZ
887
888 idx++;
889 }
890
99d00215
PZ
891 return 0;
892}
893
dcc914f4
JP
894/*
895 * Warnings shouldn't be reported for ignored functions.
896 */
897static void add_ignores(struct objtool_file *file)
898{
899 struct instruction *insn;
900 struct section *sec;
901 struct symbol *func;
f1974222 902 struct reloc *reloc;
dcc914f4 903
aaf5c623
PZ
904 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
905 if (!sec)
906 return;
dcc914f4 907
f1974222
MH
908 list_for_each_entry(reloc, &sec->reloc_list, list) {
909 switch (reloc->sym->type) {
aaf5c623 910 case STT_FUNC:
f1974222 911 func = reloc->sym;
aaf5c623
PZ
912 break;
913
914 case STT_SECTION:
f1974222 915 func = find_func_by_offset(reloc->sym->sec, reloc->addend);
7acfe531 916 if (!func)
dcc914f4 917 continue;
aaf5c623 918 break;
dcc914f4 919
aaf5c623 920 default:
f1974222 921 WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type);
aaf5c623 922 continue;
dcc914f4 923 }
aaf5c623 924
f0f70adb 925 func_for_each_insn(file, func, insn)
aaf5c623 926 insn->ignore = true;
dcc914f4
JP
927 }
928}
929
ea24213d
PZ
930/*
931 * This is a whitelist of functions that is allowed to be called with AC set.
932 * The list is meant to be minimal and only contains compiler instrumentation
933 * ABI and a few functions used to implement *_{to,from}_user() functions.
934 *
935 * These functions must not directly change AC, but may PUSHF/POPF.
936 */
937static const char *uaccess_safe_builtin[] = {
938 /* KASAN */
939 "kasan_report",
f00748bf 940 "kasan_check_range",
ea24213d
PZ
941 /* KASAN out-of-line */
942 "__asan_loadN_noabort",
943 "__asan_load1_noabort",
944 "__asan_load2_noabort",
945 "__asan_load4_noabort",
946 "__asan_load8_noabort",
947 "__asan_load16_noabort",
948 "__asan_storeN_noabort",
949 "__asan_store1_noabort",
950 "__asan_store2_noabort",
951 "__asan_store4_noabort",
952 "__asan_store8_noabort",
953 "__asan_store16_noabort",
b0b8e56b
JH
954 "__kasan_check_read",
955 "__kasan_check_write",
ea24213d
PZ
956 /* KASAN in-line */
957 "__asan_report_load_n_noabort",
958 "__asan_report_load1_noabort",
959 "__asan_report_load2_noabort",
960 "__asan_report_load4_noabort",
961 "__asan_report_load8_noabort",
962 "__asan_report_load16_noabort",
963 "__asan_report_store_n_noabort",
964 "__asan_report_store1_noabort",
965 "__asan_report_store2_noabort",
966 "__asan_report_store4_noabort",
967 "__asan_report_store8_noabort",
968 "__asan_report_store16_noabort",
5f5c9712 969 /* KCSAN */
9967683c 970 "__kcsan_check_access",
0525bd82
ME
971 "__kcsan_mb",
972 "__kcsan_wmb",
973 "__kcsan_rmb",
974 "__kcsan_release",
5f5c9712
ME
975 "kcsan_found_watchpoint",
976 "kcsan_setup_watchpoint",
9967683c 977 "kcsan_check_scoped_accesses",
50a19ad4
ME
978 "kcsan_disable_current",
979 "kcsan_enable_current_nowarn",
5f5c9712
ME
980 /* KCSAN/TSAN */
981 "__tsan_func_entry",
982 "__tsan_func_exit",
983 "__tsan_read_range",
984 "__tsan_write_range",
985 "__tsan_read1",
986 "__tsan_read2",
987 "__tsan_read4",
988 "__tsan_read8",
989 "__tsan_read16",
990 "__tsan_write1",
991 "__tsan_write2",
992 "__tsan_write4",
993 "__tsan_write8",
994 "__tsan_write16",
a81b3759
ME
995 "__tsan_read_write1",
996 "__tsan_read_write2",
997 "__tsan_read_write4",
998 "__tsan_read_write8",
999 "__tsan_read_write16",
883957b1
ME
1000 "__tsan_atomic8_load",
1001 "__tsan_atomic16_load",
1002 "__tsan_atomic32_load",
1003 "__tsan_atomic64_load",
1004 "__tsan_atomic8_store",
1005 "__tsan_atomic16_store",
1006 "__tsan_atomic32_store",
1007 "__tsan_atomic64_store",
1008 "__tsan_atomic8_exchange",
1009 "__tsan_atomic16_exchange",
1010 "__tsan_atomic32_exchange",
1011 "__tsan_atomic64_exchange",
1012 "__tsan_atomic8_fetch_add",
1013 "__tsan_atomic16_fetch_add",
1014 "__tsan_atomic32_fetch_add",
1015 "__tsan_atomic64_fetch_add",
1016 "__tsan_atomic8_fetch_sub",
1017 "__tsan_atomic16_fetch_sub",
1018 "__tsan_atomic32_fetch_sub",
1019 "__tsan_atomic64_fetch_sub",
1020 "__tsan_atomic8_fetch_and",
1021 "__tsan_atomic16_fetch_and",
1022 "__tsan_atomic32_fetch_and",
1023 "__tsan_atomic64_fetch_and",
1024 "__tsan_atomic8_fetch_or",
1025 "__tsan_atomic16_fetch_or",
1026 "__tsan_atomic32_fetch_or",
1027 "__tsan_atomic64_fetch_or",
1028 "__tsan_atomic8_fetch_xor",
1029 "__tsan_atomic16_fetch_xor",
1030 "__tsan_atomic32_fetch_xor",
1031 "__tsan_atomic64_fetch_xor",
1032 "__tsan_atomic8_fetch_nand",
1033 "__tsan_atomic16_fetch_nand",
1034 "__tsan_atomic32_fetch_nand",
1035 "__tsan_atomic64_fetch_nand",
1036 "__tsan_atomic8_compare_exchange_strong",
1037 "__tsan_atomic16_compare_exchange_strong",
1038 "__tsan_atomic32_compare_exchange_strong",
1039 "__tsan_atomic64_compare_exchange_strong",
1040 "__tsan_atomic8_compare_exchange_weak",
1041 "__tsan_atomic16_compare_exchange_weak",
1042 "__tsan_atomic32_compare_exchange_weak",
1043 "__tsan_atomic64_compare_exchange_weak",
1044 "__tsan_atomic8_compare_exchange_val",
1045 "__tsan_atomic16_compare_exchange_val",
1046 "__tsan_atomic32_compare_exchange_val",
1047 "__tsan_atomic64_compare_exchange_val",
1048 "__tsan_atomic_thread_fence",
1049 "__tsan_atomic_signal_fence",
ea24213d
PZ
1050 /* KCOV */
1051 "write_comp_data",
ae033f08 1052 "check_kcov_mode",
ea24213d
PZ
1053 "__sanitizer_cov_trace_pc",
1054 "__sanitizer_cov_trace_const_cmp1",
1055 "__sanitizer_cov_trace_const_cmp2",
1056 "__sanitizer_cov_trace_const_cmp4",
1057 "__sanitizer_cov_trace_const_cmp8",
1058 "__sanitizer_cov_trace_cmp1",
1059 "__sanitizer_cov_trace_cmp2",
1060 "__sanitizer_cov_trace_cmp4",
1061 "__sanitizer_cov_trace_cmp8",
36b1c700 1062 "__sanitizer_cov_trace_switch",
ea24213d
PZ
1063 /* UBSAN */
1064 "ubsan_type_mismatch_common",
1065 "__ubsan_handle_type_mismatch",
1066 "__ubsan_handle_type_mismatch_v1",
9a50dcaf 1067 "__ubsan_handle_shift_out_of_bounds",
ea24213d
PZ
1068 /* misc */
1069 "csum_partial_copy_generic",
ec6347bb
DW
1070 "copy_mc_fragile",
1071 "copy_mc_fragile_handle_tail",
5da8e4a6 1072 "copy_mc_enhanced_fast_string",
ea24213d
PZ
1073 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
1074 NULL
1075};
1076
1077static void add_uaccess_safe(struct objtool_file *file)
1078{
1079 struct symbol *func;
1080 const char **name;
1081
2daf7fab 1082 if (!opts.uaccess)
ea24213d
PZ
1083 return;
1084
1085 for (name = uaccess_safe_builtin; *name; name++) {
1086 func = find_symbol_by_name(file->elf, *name);
1087 if (!func)
1088 continue;
1089
e10cd8fe 1090 func->uaccess_safe = true;
dcc914f4
JP
1091 }
1092}
1093
258c7605
JP
1094/*
1095 * FIXME: For now, just ignore any alternatives which add retpolines. This is
1096 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
1097 * But it at least allows objtool to understand the control flow *around* the
1098 * retpoline.
1099 */
ff05ab23 1100static int add_ignore_alternatives(struct objtool_file *file)
258c7605
JP
1101{
1102 struct section *sec;
f1974222 1103 struct reloc *reloc;
258c7605
JP
1104 struct instruction *insn;
1105
ff05ab23 1106 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
258c7605
JP
1107 if (!sec)
1108 return 0;
1109
f1974222
MH
1110 list_for_each_entry(reloc, &sec->reloc_list, list) {
1111 if (reloc->sym->type != STT_SECTION) {
258c7605
JP
1112 WARN("unexpected relocation symbol type in %s", sec->name);
1113 return -1;
1114 }
1115
f1974222 1116 insn = find_insn(file, reloc->sym->sec, reloc->addend);
258c7605 1117 if (!insn) {
ff05ab23 1118 WARN("bad .discard.ignore_alts entry");
258c7605
JP
1119 return -1;
1120 }
1121
1122 insn->ignore_alts = true;
1123 }
1124
1125 return 0;
1126}
1127
530b4ddd
PZ
1128__weak bool arch_is_retpoline(struct symbol *sym)
1129{
1130 return false;
1131}
1132
d9e9d230
PZ
1133__weak bool arch_is_rethunk(struct symbol *sym)
1134{
1135 return false;
1136}
1137
7bd2a600
PZ
1138#define NEGATIVE_RELOC ((void *)-1L)
1139
1140static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
1141{
1142 if (insn->reloc == NEGATIVE_RELOC)
1143 return NULL;
1144
1145 if (!insn->reloc) {
db2b0c5d
PZ
1146 if (!file)
1147 return NULL;
1148
7bd2a600
PZ
1149 insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec,
1150 insn->offset, insn->len);
1151 if (!insn->reloc) {
1152 insn->reloc = NEGATIVE_RELOC;
1153 return NULL;
1154 }
1155 }
1156
1157 return insn->reloc;
1158}
1159
f56dae88
PZ
1160static void remove_insn_ops(struct instruction *insn)
1161{
1162 struct stack_op *op, *tmp;
1163
1164 list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1165 list_del(&op->list);
1166 free(op);
1167 }
1168}
1169
dd003ede
PZ
1170static void annotate_call_site(struct objtool_file *file,
1171 struct instruction *insn, bool sibling)
f56dae88
PZ
1172{
1173 struct reloc *reloc = insn_reloc(file, insn);
dd003ede 1174 struct symbol *sym = insn->call_dest;
f56dae88 1175
dd003ede
PZ
1176 if (!sym)
1177 sym = reloc->sym;
1178
1179 /*
1180 * Alternative replacement code is just template code which is
1181 * sometimes copied to the original instruction. For now, don't
1182 * annotate it. (In the future we might consider annotating the
1183 * original instruction if/when it ever makes sense to do so.)
1184 */
1185 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
f56dae88
PZ
1186 return;
1187
dd003ede
PZ
1188 if (sym->static_call_tramp) {
1189 list_add_tail(&insn->call_node, &file->static_call_list);
1190 return;
f56dae88
PZ
1191 }
1192
134ab5bd
PZ
1193 if (sym->retpoline_thunk) {
1194 list_add_tail(&insn->call_node, &file->retpoline_call_list);
1195 return;
1196 }
1197
f56dae88 1198 /*
05098119
ME
1199 * Many compilers cannot disable KCOV or sanitizer calls with a function
1200 * attribute so they need a little help, NOP out any such calls from
1201 * noinstr text.
f56dae88 1202 */
22102f45 1203 if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
f56dae88
PZ
1204 if (reloc) {
1205 reloc->type = R_NONE;
1206 elf_write_reloc(file->elf, reloc);
1207 }
1208
1209 elf_write_insn(file->elf, insn->sec,
1210 insn->offset, insn->len,
1211 sibling ? arch_ret_insn(insn->len)
1212 : arch_nop_insn(insn->len));
1213
1214 insn->type = sibling ? INSN_RETURN : INSN_NOP;
7a53f408
PZ
1215
1216 if (sibling) {
1217 /*
1218 * We've replaced the tail-call JMP insn by two new
1219 * insn: RET; INT3, except we only have a single struct
1220 * insn here. Mark it retpoline_safe to avoid the SLS
1221 * warning, instead of adding another insn.
1222 */
1223 insn->retpoline_safe = true;
1224 }
1225
dd003ede 1226 return;
f56dae88
PZ
1227 }
1228
2daf7fab 1229 if (opts.mcount && sym->fentry) {
f56dae88
PZ
1230 if (sibling)
1231 WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
1232
1233 if (reloc) {
1234 reloc->type = R_NONE;
1235 elf_write_reloc(file->elf, reloc);
1236 }
1237
1238 elf_write_insn(file->elf, insn->sec,
1239 insn->offset, insn->len,
1240 arch_nop_insn(insn->len));
1241
1242 insn->type = INSN_NOP;
1243
c509331b 1244 list_add_tail(&insn->call_node, &file->mcount_loc_list);
dd003ede 1245 return;
f56dae88 1246 }
0e5b613b
PZ
1247
1248 if (!sibling && dead_end_function(file, sym))
1249 insn->dead_end = true;
dd003ede
PZ
1250}
1251
1252static void add_call_dest(struct objtool_file *file, struct instruction *insn,
1253 struct symbol *dest, bool sibling)
1254{
1255 insn->call_dest = dest;
1256 if (!dest)
1257 return;
f56dae88
PZ
1258
1259 /*
1260 * Whatever stack impact regular CALLs have, should be undone
1261 * by the RETURN of the called function.
1262 *
1263 * Annotated intra-function calls retain the stack_ops but
1264 * are converted to JUMP, see read_intra_function_calls().
1265 */
1266 remove_insn_ops(insn);
dd003ede
PZ
1267
1268 annotate_call_site(file, insn, sibling);
f56dae88
PZ
1269}
1270
134ab5bd
PZ
1271static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
1272{
1273 /*
1274 * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
1275 * so convert them accordingly.
1276 */
1277 switch (insn->type) {
1278 case INSN_CALL:
1279 insn->type = INSN_CALL_DYNAMIC;
1280 break;
1281 case INSN_JUMP_UNCONDITIONAL:
1282 insn->type = INSN_JUMP_DYNAMIC;
1283 break;
1284 case INSN_JUMP_CONDITIONAL:
1285 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
1286 break;
1287 default:
1288 return;
1289 }
1290
1291 insn->retpoline_safe = true;
1292
1293 /*
1294 * Whatever stack impact regular CALLs have, should be undone
1295 * by the RETURN of the called function.
1296 *
1297 * Annotated intra-function calls retain the stack_ops but
1298 * are converted to JUMP, see read_intra_function_calls().
1299 */
1300 remove_insn_ops(insn);
1301
1302 annotate_call_site(file, insn, false);
1303}
08f87a93 1304
a149180f 1305static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
d9e9d230
PZ
1306{
1307 /*
1308 * Return thunk tail calls are really just returns in disguise,
1309 * so convert them accordingly.
1310 */
1311 insn->type = INSN_RETURN;
1312 insn->retpoline_safe = true;
1313
a149180f
PZ
1314 if (add)
1315 list_add_tail(&insn->call_node, &file->return_thunk_list);
d9e9d230
PZ
1316}
1317
08f87a93
PZ
1318static bool same_function(struct instruction *insn1, struct instruction *insn2)
1319{
1320 return insn1->func->pfunc == insn2->func->pfunc;
1321}
1322
d139bca4 1323static bool is_first_func_insn(struct objtool_file *file, struct instruction *insn)
08f87a93 1324{
d139bca4
PZ
1325 if (insn->offset == insn->func->offset)
1326 return true;
1327
2daf7fab 1328 if (opts.ibt) {
d139bca4
PZ
1329 struct instruction *prev = prev_insn_same_sym(file, insn);
1330
1331 if (prev && prev->type == INSN_ENDBR &&
1332 insn->offset == insn->func->offset + prev->len)
1333 return true;
1334 }
1335
1336 return false;
08f87a93
PZ
1337}
1338
dcc914f4
JP
1339/*
1340 * Find the destination instructions for all jumps.
1341 */
1342static int add_jump_destinations(struct objtool_file *file)
1343{
26ff6041 1344 struct instruction *insn, *jump_dest;
f1974222 1345 struct reloc *reloc;
dcc914f4
JP
1346 struct section *dest_sec;
1347 unsigned long dest_off;
1348
1349 for_each_insn(file, insn) {
34c861e8
JP
1350 if (insn->jump_dest) {
1351 /*
1352 * handle_group_alt() may have previously set
1353 * 'jump_dest' for some alternatives.
1354 */
1355 continue;
1356 }
a2296140 1357 if (!is_static_jump(insn))
dcc914f4
JP
1358 continue;
1359
7bd2a600 1360 reloc = insn_reloc(file, insn);
f1974222 1361 if (!reloc) {
dcc914f4 1362 dest_sec = insn->sec;
bfb08f22 1363 dest_off = arch_jump_destination(insn);
f1974222
MH
1364 } else if (reloc->sym->type == STT_SECTION) {
1365 dest_sec = reloc->sym->sec;
1366 dest_off = arch_dest_reloc_offset(reloc->addend);
1739c66e 1367 } else if (reloc->sym->retpoline_thunk) {
134ab5bd 1368 add_retpoline_call(file, insn);
39b73533 1369 continue;
d9e9d230 1370 } else if (reloc->sym->return_thunk) {
a149180f 1371 add_return_call(file, insn, true);
d9e9d230 1372 continue;
ecf11ba4 1373 } else if (insn->func) {
26ff6041
JP
1374 /*
1375 * External sibling call or internal sibling call with
1376 * STT_FUNC reloc.
1377 */
f56dae88 1378 add_call_dest(file, insn, reloc->sym, true);
dcc914f4 1379 continue;
ecf11ba4
JP
1380 } else if (reloc->sym->sec->idx) {
1381 dest_sec = reloc->sym->sec;
1382 dest_off = reloc->sym->sym.st_value +
1383 arch_dest_reloc_offset(reloc->addend);
1384 } else {
1385 /* non-func asm code jumping to another file */
1386 continue;
dcc914f4
JP
1387 }
1388
26ff6041
JP
1389 jump_dest = find_insn(file, dest_sec, dest_off);
1390 if (!jump_dest) {
a149180f
PZ
1391 struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1392
1393 /*
1394 * This is a special case for zen_untrain_ret().
1395 * It jumps to __x86_return_thunk(), but objtool
1396 * can't find the thunk's starting RET
1397 * instruction, because the RET is also in the
1398 * middle of another instruction. Objtool only
1399 * knows about the outer instruction.
1400 */
1401 if (sym && sym->return_thunk) {
1402 add_return_call(file, insn, false);
1403 continue;
1404 }
1405
dcc914f4
JP
1406 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
1407 insn->sec, insn->offset, dest_sec->name,
1408 dest_off);
1409 return -1;
1410 }
cd77849a
JP
1411
1412 /*
54262aa2 1413 * Cross-function jump.
cd77849a 1414 */
26ff6041
JP
1415 if (insn->func && jump_dest->func &&
1416 insn->func != jump_dest->func) {
54262aa2
PZ
1417
1418 /*
1419 * For GCC 8+, create parent/child links for any cold
1420 * subfunctions. This is _mostly_ redundant with a
1421 * similar initialization in read_symbols().
1422 *
1423 * If a function has aliases, we want the *first* such
1424 * function in the symbol table to be the subfunction's
1425 * parent. In that case we overwrite the
1426 * initialization done in read_symbols().
1427 *
1428 * However this code can't completely replace the
1429 * read_symbols() code because this doesn't detect the
1430 * case where the parent function's only reference to a
e7c2bc37 1431 * subfunction is through a jump table.
54262aa2 1432 */
34ca59e1 1433 if (!strstr(insn->func->name, ".cold") &&
26ff6041
JP
1434 strstr(jump_dest->func->name, ".cold")) {
1435 insn->func->cfunc = jump_dest->func;
1436 jump_dest->func->pfunc = insn->func;
1437
1438 } else if (!same_function(insn, jump_dest) &&
1439 is_first_func_insn(file, jump_dest)) {
1440 /*
1441 * Internal sibling call without reloc or with
1442 * STT_SECTION reloc.
1443 */
1444 add_call_dest(file, insn, jump_dest->func, true);
1445 continue;
54262aa2 1446 }
cd77849a 1447 }
26ff6041
JP
1448
1449 insn->jump_dest = jump_dest;
dcc914f4
JP
1450 }
1451
1452 return 0;
1453}
1454
2b232a22
JT
1455static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
1456{
1457 struct symbol *call_dest;
1458
1459 call_dest = find_func_by_offset(sec, offset);
1460 if (!call_dest)
1461 call_dest = find_symbol_by_offset(sec, offset);
1462
1463 return call_dest;
1464}
1465
dcc914f4
JP
1466/*
1467 * Find the destination instructions for all calls.
1468 */
1469static int add_call_destinations(struct objtool_file *file)
1470{
1471 struct instruction *insn;
1472 unsigned long dest_off;
f56dae88 1473 struct symbol *dest;
f1974222 1474 struct reloc *reloc;
dcc914f4
JP
1475
1476 for_each_insn(file, insn) {
1477 if (insn->type != INSN_CALL)
1478 continue;
1479
7bd2a600 1480 reloc = insn_reloc(file, insn);
f1974222 1481 if (!reloc) {
bfb08f22 1482 dest_off = arch_jump_destination(insn);
f56dae88
PZ
1483 dest = find_call_destination(insn->sec, dest_off);
1484
1485 add_call_dest(file, insn, dest, false);
a845c7cf 1486
7acfe531
JP
1487 if (insn->ignore)
1488 continue;
1489
1490 if (!insn->call_dest) {
8aa8eb2a 1491 WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset);
dcc914f4
JP
1492 return -1;
1493 }
a845c7cf 1494
7acfe531
JP
1495 if (insn->func && insn->call_dest->type != STT_FUNC) {
1496 WARN_FUNC("unsupported call to non-function",
1497 insn->sec, insn->offset);
1498 return -1;
1499 }
1500
f1974222
MH
1501 } else if (reloc->sym->type == STT_SECTION) {
1502 dest_off = arch_dest_reloc_offset(reloc->addend);
f56dae88
PZ
1503 dest = find_call_destination(reloc->sym->sec, dest_off);
1504 if (!dest) {
bfb08f22 1505 WARN_FUNC("can't find call dest symbol at %s+0x%lx",
dcc914f4 1506 insn->sec, insn->offset,
f1974222 1507 reloc->sym->sec->name,
bfb08f22 1508 dest_off);
dcc914f4
JP
1509 return -1;
1510 }
bcb1b6ff 1511
f56dae88
PZ
1512 add_call_dest(file, insn, dest, false);
1513
1739c66e 1514 } else if (reloc->sym->retpoline_thunk) {
134ab5bd 1515 add_retpoline_call(file, insn);
bcb1b6ff 1516
dcc914f4 1517 } else
f56dae88 1518 add_call_dest(file, insn, reloc->sym, false);
dcc914f4
JP
1519 }
1520
1521 return 0;
1522}
1523
1524/*
c9c324dc
JP
1525 * The .alternatives section requires some extra special care over and above
1526 * other special sections because alternatives are patched in place.
dcc914f4
JP
1527 */
1528static int handle_group_alt(struct objtool_file *file,
1529 struct special_alt *special_alt,
1530 struct instruction *orig_insn,
1531 struct instruction **new_insn)
1532{
c9c324dc 1533 struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
b23cc71c 1534 struct alt_group *orig_alt_group, *new_alt_group;
dcc914f4
JP
1535 unsigned long dest_off;
1536
b23cc71c
JP
1537
1538 orig_alt_group = malloc(sizeof(*orig_alt_group));
1539 if (!orig_alt_group) {
1540 WARN("malloc failed");
1541 return -1;
1542 }
c9c324dc
JP
1543 orig_alt_group->cfi = calloc(special_alt->orig_len,
1544 sizeof(struct cfi_state *));
1545 if (!orig_alt_group->cfi) {
1546 WARN("calloc failed");
1547 return -1;
1548 }
1549
dcc914f4
JP
1550 last_orig_insn = NULL;
1551 insn = orig_insn;
1552 sec_for_each_insn_from(file, insn) {
1553 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
1554 break;
1555
b23cc71c 1556 insn->alt_group = orig_alt_group;
dcc914f4
JP
1557 last_orig_insn = insn;
1558 }
b23cc71c
JP
1559 orig_alt_group->orig_group = NULL;
1560 orig_alt_group->first_insn = orig_insn;
1561 orig_alt_group->last_insn = last_orig_insn;
dcc914f4 1562
17bc3391 1563
c9c324dc
JP
1564 new_alt_group = malloc(sizeof(*new_alt_group));
1565 if (!new_alt_group) {
1566 WARN("malloc failed");
1567 return -1;
dcc914f4 1568 }
dcc914f4 1569
c9c324dc
JP
1570 if (special_alt->new_len < special_alt->orig_len) {
1571 /*
1572 * Insert a fake nop at the end to make the replacement
1573 * alt_group the same size as the original. This is needed to
1574 * allow propagate_alt_cfi() to do its magic. When the last
1575 * instruction affects the stack, the instruction after it (the
1576 * nop) will propagate the new state to the shared CFI array.
1577 */
1578 nop = malloc(sizeof(*nop));
1579 if (!nop) {
1580 WARN("malloc failed");
17bc3391
JP
1581 return -1;
1582 }
c9c324dc
JP
1583 memset(nop, 0, sizeof(*nop));
1584 INIT_LIST_HEAD(&nop->alts);
1585 INIT_LIST_HEAD(&nop->stack_ops);
c9c324dc
JP
1586
1587 nop->sec = special_alt->new_sec;
1588 nop->offset = special_alt->new_off + special_alt->new_len;
1589 nop->len = special_alt->orig_len - special_alt->new_len;
1590 nop->type = INSN_NOP;
1591 nop->func = orig_insn->func;
1592 nop->alt_group = new_alt_group;
1593 nop->ignore = orig_insn->ignore_alts;
dcc914f4 1594 }
17bc3391 1595
c9c324dc
JP
1596 if (!special_alt->new_len) {
1597 *new_insn = nop;
1598 goto end;
dcc914f4
JP
1599 }
1600
dcc914f4
JP
1601 insn = *new_insn;
1602 sec_for_each_insn_from(file, insn) {
45245f51
JT
1603 struct reloc *alt_reloc;
1604
dcc914f4
JP
1605 if (insn->offset >= special_alt->new_off + special_alt->new_len)
1606 break;
1607
1608 last_new_insn = insn;
1609
a845c7cf 1610 insn->ignore = orig_insn->ignore_alts;
a4d09dde 1611 insn->func = orig_insn->func;
b23cc71c 1612 insn->alt_group = new_alt_group;
a845c7cf 1613
dc419723
JP
1614 /*
1615 * Since alternative replacement code is copy/pasted by the
1616 * kernel after applying relocations, generally such code can't
1617 * have relative-address relocation references to outside the
1618 * .altinstr_replacement section, unless the arch's
1619 * alternatives code can adjust the relative offsets
1620 * accordingly.
dc419723 1621 */
7bd2a600 1622 alt_reloc = insn_reloc(file, insn);
45245f51
JT
1623 if (alt_reloc &&
1624 !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
dc419723
JP
1625
1626 WARN_FUNC("unsupported relocation in alternatives section",
1627 insn->sec, insn->offset);
1628 return -1;
1629 }
1630
a2296140 1631 if (!is_static_jump(insn))
dcc914f4
JP
1632 continue;
1633
1634 if (!insn->immediate)
1635 continue;
1636
bfb08f22 1637 dest_off = arch_jump_destination(insn);
34c861e8 1638 if (dest_off == special_alt->new_off + special_alt->new_len) {
c9c324dc 1639 insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
34c861e8
JP
1640 if (!insn->jump_dest) {
1641 WARN_FUNC("can't find alternative jump destination",
1642 insn->sec, insn->offset);
1643 return -1;
1644 }
dcc914f4
JP
1645 }
1646 }
1647
1648 if (!last_new_insn) {
1649 WARN_FUNC("can't find last new alternative instruction",
1650 special_alt->new_sec, special_alt->new_off);
1651 return -1;
1652 }
1653
c9c324dc
JP
1654 if (nop)
1655 list_add(&nop->list, &last_new_insn->list);
1656end:
b23cc71c
JP
1657 new_alt_group->orig_group = orig_alt_group;
1658 new_alt_group->first_insn = *new_insn;
c9c324dc
JP
1659 new_alt_group->last_insn = nop ? : last_new_insn;
1660 new_alt_group->cfi = orig_alt_group->cfi;
dcc914f4
JP
1661 return 0;
1662}
1663
1664/*
1665 * A jump table entry can either convert a nop to a jump or a jump to a nop.
1666 * If the original instruction is a jump, make the alt entry an effective nop
1667 * by just skipping the original instruction.
1668 */
1669static int handle_jump_alt(struct objtool_file *file,
1670 struct special_alt *special_alt,
1671 struct instruction *orig_insn,
1672 struct instruction **new_insn)
1673{
48001d26
PZ
1674 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
1675 orig_insn->type != INSN_NOP) {
e2d9494b 1676
dcc914f4
JP
1677 WARN_FUNC("unsupported instruction at jump label",
1678 orig_insn->sec, orig_insn->offset);
1679 return -1;
1680 }
1681
4ab7674f 1682 if (opts.hack_jump_label && special_alt->key_addend & 2) {
6d37b83c
PZ
1683 struct reloc *reloc = insn_reloc(file, orig_insn);
1684
1685 if (reloc) {
1686 reloc->type = R_NONE;
1687 elf_write_reloc(file->elf, reloc);
1688 }
1689 elf_write_insn(file->elf, orig_insn->sec,
1690 orig_insn->offset, orig_insn->len,
1691 arch_nop_insn(orig_insn->len));
1692 orig_insn->type = INSN_NOP;
48001d26
PZ
1693 }
1694
1695 if (orig_insn->type == INSN_NOP) {
1696 if (orig_insn->len == 2)
1697 file->jl_nop_short++;
1698 else
1699 file->jl_nop_long++;
1700
1701 return 0;
6d37b83c
PZ
1702 }
1703
e2d9494b
PZ
1704 if (orig_insn->len == 2)
1705 file->jl_short++;
1706 else
1707 file->jl_long++;
1708
dcc914f4
JP
1709 *new_insn = list_next_entry(orig_insn, list);
1710 return 0;
1711}
1712
1713/*
1714 * Read all the special sections which have alternate instructions which can be
1715 * patched in or redirected to at runtime. Each instruction having alternate
1716 * instruction(s) has them added to its insn->alts list, which will be
1717 * traversed in validate_branch().
1718 */
1719static int add_special_section_alts(struct objtool_file *file)
1720{
1721 struct list_head special_alts;
1722 struct instruction *orig_insn, *new_insn;
1723 struct special_alt *special_alt, *tmp;
1724 struct alternative *alt;
1725 int ret;
1726
1727 ret = special_get_alts(file->elf, &special_alts);
1728 if (ret)
1729 return ret;
1730
1731 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
dcc914f4
JP
1732
1733 orig_insn = find_insn(file, special_alt->orig_sec,
1734 special_alt->orig_off);
1735 if (!orig_insn) {
1736 WARN_FUNC("special: can't find orig instruction",
1737 special_alt->orig_sec, special_alt->orig_off);
1738 ret = -1;
1739 goto out;
1740 }
1741
1742 new_insn = NULL;
1743 if (!special_alt->group || special_alt->new_len) {
1744 new_insn = find_insn(file, special_alt->new_sec,
1745 special_alt->new_off);
1746 if (!new_insn) {
1747 WARN_FUNC("special: can't find new instruction",
1748 special_alt->new_sec,
1749 special_alt->new_off);
1750 ret = -1;
1751 goto out;
1752 }
1753 }
1754
1755 if (special_alt->group) {
7170cf47
JT
1756 if (!special_alt->orig_len) {
1757 WARN_FUNC("empty alternative entry",
1758 orig_insn->sec, orig_insn->offset);
1759 continue;
1760 }
1761
dcc914f4
JP
1762 ret = handle_group_alt(file, special_alt, orig_insn,
1763 &new_insn);
1764 if (ret)
1765 goto out;
1766 } else if (special_alt->jump_or_nop) {
1767 ret = handle_jump_alt(file, special_alt, orig_insn,
1768 &new_insn);
1769 if (ret)
1770 goto out;
1771 }
1772
258c7605
JP
1773 alt = malloc(sizeof(*alt));
1774 if (!alt) {
1775 WARN("malloc failed");
1776 ret = -1;
1777 goto out;
1778 }
1779
dcc914f4 1780 alt->insn = new_insn;
764eef4b 1781 alt->skip_orig = special_alt->skip_orig;
ea24213d 1782 orig_insn->ignore_alts |= special_alt->skip_alt;
dcc914f4
JP
1783 list_add_tail(&alt->list, &orig_insn->alts);
1784
1785 list_del(&special_alt->list);
1786 free(special_alt);
1787 }
1788
2daf7fab 1789 if (opts.stats) {
e2d9494b
PZ
1790 printf("jl\\\tNOP\tJMP\n");
1791 printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
1792 printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
1793 }
1794
dcc914f4
JP
1795out:
1796 return ret;
1797}
1798
e7c2bc37 1799static int add_jump_table(struct objtool_file *file, struct instruction *insn,
f1974222 1800 struct reloc *table)
dcc914f4 1801{
f1974222 1802 struct reloc *reloc = table;
e7c2bc37 1803 struct instruction *dest_insn;
dcc914f4 1804 struct alternative *alt;
fd35c88b
JP
1805 struct symbol *pfunc = insn->func->pfunc;
1806 unsigned int prev_offset = 0;
dcc914f4 1807
e7c2bc37 1808 /*
f1974222 1809 * Each @reloc is a switch table relocation which points to the target
e7c2bc37
JP
1810 * instruction.
1811 */
f1974222 1812 list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
bd98c813
JH
1813
1814 /* Check for the end of the table: */
f1974222 1815 if (reloc != table && reloc->jump_table_start)
dcc914f4
JP
1816 break;
1817
e7c2bc37 1818 /* Make sure the table entries are consecutive: */
f1974222 1819 if (prev_offset && reloc->offset != prev_offset + 8)
fd35c88b
JP
1820 break;
1821
1822 /* Detect function pointers from contiguous objects: */
f1974222
MH
1823 if (reloc->sym->sec == pfunc->sec &&
1824 reloc->addend == pfunc->offset)
fd35c88b
JP
1825 break;
1826
f1974222 1827 dest_insn = find_insn(file, reloc->sym->sec, reloc->addend);
e7c2bc37 1828 if (!dest_insn)
dcc914f4
JP
1829 break;
1830
e7c2bc37 1831 /* Make sure the destination is in the same function: */
e65050b9 1832 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
13810435 1833 break;
dcc914f4
JP
1834
1835 alt = malloc(sizeof(*alt));
1836 if (!alt) {
1837 WARN("malloc failed");
1838 return -1;
1839 }
1840
e7c2bc37 1841 alt->insn = dest_insn;
dcc914f4 1842 list_add_tail(&alt->list, &insn->alts);
f1974222 1843 prev_offset = reloc->offset;
fd35c88b
JP
1844 }
1845
1846 if (!prev_offset) {
1847 WARN_FUNC("can't find switch jump table",
1848 insn->sec, insn->offset);
1849 return -1;
dcc914f4
JP
1850 }
1851
1852 return 0;
1853}
1854
1855/*
d871f7b5
RG
1856 * find_jump_table() - Given a dynamic jump, find the switch jump table
1857 * associated with it.
dcc914f4 1858 */
f1974222 1859static struct reloc *find_jump_table(struct objtool_file *file,
dcc914f4
JP
1860 struct symbol *func,
1861 struct instruction *insn)
1862{
d871f7b5 1863 struct reloc *table_reloc;
113d4bc9 1864 struct instruction *dest_insn, *orig_insn = insn;
dcc914f4 1865
99ce7962
PZ
1866 /*
1867 * Backward search using the @first_jump_src links, these help avoid
1868 * much of the 'in between' code. Which avoids us getting confused by
1869 * it.
1870 */
7dec80cc 1871 for (;
1119d265
JP
1872 insn && insn->func && insn->func->pfunc == func;
1873 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
99ce7962 1874
7dec80cc 1875 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
dcc914f4
JP
1876 break;
1877
1878 /* allow small jumps within the range */
1879 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1880 insn->jump_dest &&
1881 (insn->jump_dest->offset <= insn->offset ||
1882 insn->jump_dest->offset > orig_insn->offset))
1883 break;
1884
d871f7b5 1885 table_reloc = arch_find_switch_table(file, insn);
f1974222 1886 if (!table_reloc)
e7c2bc37 1887 continue;
f1974222 1888 dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend);
113d4bc9
JP
1889 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1890 continue;
7dec80cc 1891
f1974222 1892 return table_reloc;
dcc914f4
JP
1893 }
1894
1895 return NULL;
1896}
1897
bd98c813
JH
1898/*
1899 * First pass: Mark the head of each jump table so that in the next pass,
1900 * we know when a given jump table ends and the next one starts.
1901 */
1902static void mark_func_jump_tables(struct objtool_file *file,
1903 struct symbol *func)
dcc914f4 1904{
bd98c813 1905 struct instruction *insn, *last = NULL;
f1974222 1906 struct reloc *reloc;
dcc914f4 1907
f0f70adb 1908 func_for_each_insn(file, func, insn) {
99ce7962
PZ
1909 if (!last)
1910 last = insn;
1911
1912 /*
1913 * Store back-pointers for unconditional forward jumps such
e7c2bc37 1914 * that find_jump_table() can back-track using those and
99ce7962
PZ
1915 * avoid some potentially confusing code.
1916 */
1917 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1918 insn->offset > last->offset &&
1919 insn->jump_dest->offset > insn->offset &&
1920 !insn->jump_dest->first_jump_src) {
1921
1922 insn->jump_dest->first_jump_src = insn;
1923 last = insn->jump_dest;
1924 }
1925
dcc914f4
JP
1926 if (insn->type != INSN_JUMP_DYNAMIC)
1927 continue;
1928
f1974222
MH
1929 reloc = find_jump_table(file, func, insn);
1930 if (reloc) {
1931 reloc->jump_table_start = true;
1932 insn->jump_table = reloc;
dcc914f4 1933 }
dcc914f4 1934 }
bd98c813
JH
1935}
1936
1937static int add_func_jump_tables(struct objtool_file *file,
1938 struct symbol *func)
1939{
1940 struct instruction *insn;
1941 int ret;
1942
f0f70adb 1943 func_for_each_insn(file, func, insn) {
bd98c813
JH
1944 if (!insn->jump_table)
1945 continue;
dcc914f4 1946
bd98c813 1947 ret = add_jump_table(file, insn, insn->jump_table);
dcc914f4
JP
1948 if (ret)
1949 return ret;
1950 }
1951
1952 return 0;
1953}
1954
1955/*
1956 * For some switch statements, gcc generates a jump table in the .rodata
1957 * section which contains a list of addresses within the function to jump to.
1958 * This finds these jump tables and adds them to the insn->alts lists.
1959 */
e7c2bc37 1960static int add_jump_table_alts(struct objtool_file *file)
dcc914f4
JP
1961{
1962 struct section *sec;
1963 struct symbol *func;
1964 int ret;
1965
4a60aa05 1966 if (!file->rodata)
dcc914f4
JP
1967 return 0;
1968
baa41469 1969 for_each_sec(file, sec) {
dcc914f4
JP
1970 list_for_each_entry(func, &sec->symbol_list, list) {
1971 if (func->type != STT_FUNC)
1972 continue;
1973
bd98c813 1974 mark_func_jump_tables(file, func);
e7c2bc37 1975 ret = add_func_jump_tables(file, func);
dcc914f4
JP
1976 if (ret)
1977 return ret;
1978 }
1979 }
1980
1981 return 0;
1982}
1983
b735bd3e
JP
1984static void set_func_state(struct cfi_state *state)
1985{
1986 state->cfa = initial_func_cfi.cfa;
1987 memcpy(&state->regs, &initial_func_cfi.regs,
1988 CFI_NUM_REGS * sizeof(struct cfi_reg));
1989 state->stack_size = initial_func_cfi.cfa.offset;
1990}
1991
39358a03
JP
1992static int read_unwind_hints(struct objtool_file *file)
1993{
8b946cc3 1994 struct cfi_state cfi = init_cfi;
f1974222 1995 struct section *sec, *relocsec;
39358a03
JP
1996 struct unwind_hint *hint;
1997 struct instruction *insn;
8b946cc3 1998 struct reloc *reloc;
39358a03
JP
1999 int i;
2000
2001 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
2002 if (!sec)
2003 return 0;
2004
f1974222
MH
2005 relocsec = sec->reloc;
2006 if (!relocsec) {
39358a03
JP
2007 WARN("missing .rela.discard.unwind_hints section");
2008 return -1;
2009 }
2010
fe255fe6 2011 if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
39358a03
JP
2012 WARN("struct unwind_hint size mismatch");
2013 return -1;
2014 }
2015
2016 file->hints = true;
2017
fe255fe6 2018 for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
39358a03
JP
2019 hint = (struct unwind_hint *)sec->data->d_buf + i;
2020
f1974222
MH
2021 reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
2022 if (!reloc) {
2023 WARN("can't find reloc for unwind_hints[%d]", i);
39358a03
JP
2024 return -1;
2025 }
2026
f1974222 2027 insn = find_insn(file, reloc->sym->sec, reloc->addend);
39358a03
JP
2028 if (!insn) {
2029 WARN("can't find insn for unwind_hints[%d]", i);
2030 return -1;
2031 }
2032
b735bd3e 2033 insn->hint = true;
39358a03 2034
a09a6e23 2035 if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
08f87a93
PZ
2036 struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
2037
a09a6e23
PZ
2038 if (sym && sym->bind == STB_GLOBAL) {
2039 if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) {
2040 WARN_FUNC("UNWIND_HINT_IRET_REGS without ENDBR",
2041 insn->sec, insn->offset);
2042 }
2043
2044 insn->entry = 1;
08f87a93
PZ
2045 }
2046 }
2047
a09a6e23
PZ
2048 if (hint->type == UNWIND_HINT_TYPE_ENTRY) {
2049 hint->type = UNWIND_HINT_TYPE_CALL;
2050 insn->entry = 1;
2051 }
2052
b735bd3e 2053 if (hint->type == UNWIND_HINT_TYPE_FUNC) {
8b946cc3 2054 insn->cfi = &func_cfi;
39358a03
JP
2055 continue;
2056 }
2057
8b946cc3
PZ
2058 if (insn->cfi)
2059 cfi = *(insn->cfi);
2060
2061 if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
39358a03
JP
2062 WARN_FUNC("unsupported unwind_hint sp base reg %d",
2063 insn->sec, insn->offset, hint->sp_reg);
2064 return -1;
2065 }
2066
8b946cc3
PZ
2067 cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
2068 cfi.type = hint->type;
2069 cfi.end = hint->end;
2070
2071 insn->cfi = cfi_hash_find_or_add(&cfi);
39358a03
JP
2072 }
2073
2074 return 0;
2075}
2076
96db4a98
PZ
2077static int read_noendbr_hints(struct objtool_file *file)
2078{
2079 struct section *sec;
2080 struct instruction *insn;
2081 struct reloc *reloc;
2082
2083 sec = find_section_by_name(file->elf, ".rela.discard.noendbr");
2084 if (!sec)
2085 return 0;
2086
2087 list_for_each_entry(reloc, &sec->reloc_list, list) {
2088 insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
2089 if (!insn) {
2090 WARN("bad .discard.noendbr entry");
2091 return -1;
2092 }
2093
08f87a93
PZ
2094 if (insn->type == INSN_ENDBR)
2095 WARN_FUNC("ANNOTATE_NOENDBR on ENDBR", insn->sec, insn->offset);
2096
96db4a98
PZ
2097 insn->noendbr = 1;
2098 }
2099
2100 return 0;
2101}
2102
b5bc2231
PZ
2103static int read_retpoline_hints(struct objtool_file *file)
2104{
63474dc4 2105 struct section *sec;
b5bc2231 2106 struct instruction *insn;
f1974222 2107 struct reloc *reloc;
b5bc2231 2108
63474dc4 2109 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
b5bc2231
PZ
2110 if (!sec)
2111 return 0;
2112
f1974222
MH
2113 list_for_each_entry(reloc, &sec->reloc_list, list) {
2114 if (reloc->sym->type != STT_SECTION) {
63474dc4 2115 WARN("unexpected relocation symbol type in %s", sec->name);
b5bc2231
PZ
2116 return -1;
2117 }
2118
f1974222 2119 insn = find_insn(file, reloc->sym->sec, reloc->addend);
b5bc2231 2120 if (!insn) {
63474dc4 2121 WARN("bad .discard.retpoline_safe entry");
b5bc2231
PZ
2122 return -1;
2123 }
2124
2125 if (insn->type != INSN_JUMP_DYNAMIC &&
9bb2ec60 2126 insn->type != INSN_CALL_DYNAMIC &&
a09a6e23
PZ
2127 insn->type != INSN_RETURN &&
2128 insn->type != INSN_NOP) {
2129 WARN_FUNC("retpoline_safe hint not an indirect jump/call/ret/nop",
b5bc2231
PZ
2130 insn->sec, insn->offset);
2131 return -1;
2132 }
2133
2134 insn->retpoline_safe = true;
2135 }
2136
2137 return 0;
2138}
2139
c4a33939
PZ
2140static int read_instr_hints(struct objtool_file *file)
2141{
2142 struct section *sec;
2143 struct instruction *insn;
f1974222 2144 struct reloc *reloc;
c4a33939
PZ
2145
2146 sec = find_section_by_name(file->elf, ".rela.discard.instr_end");
2147 if (!sec)
2148 return 0;
2149
f1974222
MH
2150 list_for_each_entry(reloc, &sec->reloc_list, list) {
2151 if (reloc->sym->type != STT_SECTION) {
c4a33939
PZ
2152 WARN("unexpected relocation symbol type in %s", sec->name);
2153 return -1;
2154 }
2155
f1974222 2156 insn = find_insn(file, reloc->sym->sec, reloc->addend);
c4a33939
PZ
2157 if (!insn) {
2158 WARN("bad .discard.instr_end entry");
2159 return -1;
2160 }
2161
2162 insn->instr--;
2163 }
2164
2165 sec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
2166 if (!sec)
2167 return 0;
2168
f1974222
MH
2169 list_for_each_entry(reloc, &sec->reloc_list, list) {
2170 if (reloc->sym->type != STT_SECTION) {
c4a33939
PZ
2171 WARN("unexpected relocation symbol type in %s", sec->name);
2172 return -1;
2173 }
2174
f1974222 2175 insn = find_insn(file, reloc->sym->sec, reloc->addend);
c4a33939
PZ
2176 if (!insn) {
2177 WARN("bad .discard.instr_begin entry");
2178 return -1;
2179 }
2180
2181 insn->instr++;
2182 }
2183
2184 return 0;
2185}
2186
8aa8eb2a
AC
2187static int read_intra_function_calls(struct objtool_file *file)
2188{
2189 struct instruction *insn;
2190 struct section *sec;
f1974222 2191 struct reloc *reloc;
8aa8eb2a
AC
2192
2193 sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
2194 if (!sec)
2195 return 0;
2196
f1974222 2197 list_for_each_entry(reloc, &sec->reloc_list, list) {
8aa8eb2a
AC
2198 unsigned long dest_off;
2199
f1974222 2200 if (reloc->sym->type != STT_SECTION) {
8aa8eb2a
AC
2201 WARN("unexpected relocation symbol type in %s",
2202 sec->name);
2203 return -1;
2204 }
2205
f1974222 2206 insn = find_insn(file, reloc->sym->sec, reloc->addend);
8aa8eb2a
AC
2207 if (!insn) {
2208 WARN("bad .discard.intra_function_call entry");
2209 return -1;
2210 }
2211
2212 if (insn->type != INSN_CALL) {
2213 WARN_FUNC("intra_function_call not a direct call",
2214 insn->sec, insn->offset);
2215 return -1;
2216 }
2217
2218 /*
2219 * Treat intra-function CALLs as JMPs, but with a stack_op.
2220 * See add_call_destinations(), which strips stack_ops from
2221 * normal CALLs.
2222 */
2223 insn->type = INSN_JUMP_UNCONDITIONAL;
2224
2225 dest_off = insn->offset + insn->len + insn->immediate;
2226 insn->jump_dest = find_insn(file, insn->sec, dest_off);
2227 if (!insn->jump_dest) {
2228 WARN_FUNC("can't find call dest at %s+0x%lx",
2229 insn->sec, insn->offset,
2230 insn->sec->name, dest_off);
2231 return -1;
2232 }
2233 }
2234
2235 return 0;
2236}
2237
05098119
ME
2238/*
2239 * Return true if name matches an instrumentation function, where calls to that
2240 * function from noinstr code can safely be removed, but compilers won't do so.
2241 */
2242static bool is_profiling_func(const char *name)
2243{
2244 /*
2245 * Many compilers cannot disable KCOV with a function attribute.
2246 */
2247 if (!strncmp(name, "__sanitizer_cov_", 16))
2248 return true;
2249
2250 /*
2251 * Some compilers currently do not remove __tsan_func_entry/exit nor
2252 * __tsan_atomic_signal_fence (used for barrier instrumentation) with
2253 * the __no_sanitize_thread attribute, remove them. Once the kernel's
2254 * minimum Clang version is 14.0, this can be removed.
2255 */
2256 if (!strncmp(name, "__tsan_func_", 12) ||
2257 !strcmp(name, "__tsan_atomic_signal_fence"))
2258 return true;
2259
2260 return false;
2261}
2262
1739c66e 2263static int classify_symbols(struct objtool_file *file)
1e7e4788
JP
2264{
2265 struct section *sec;
2266 struct symbol *func;
2267
2268 for_each_sec(file, sec) {
2269 list_for_each_entry(func, &sec->symbol_list, list) {
1739c66e
PZ
2270 if (func->bind != STB_GLOBAL)
2271 continue;
2272
2273 if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
1e7e4788
JP
2274 strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2275 func->static_call_tramp = true;
1739c66e
PZ
2276
2277 if (arch_is_retpoline(func))
2278 func->retpoline_thunk = true;
2279
d9e9d230
PZ
2280 if (arch_is_rethunk(func))
2281 func->return_thunk = true;
2282
1739c66e
PZ
2283 if (!strcmp(func->name, "__fentry__"))
2284 func->fentry = true;
2285
05098119
ME
2286 if (is_profiling_func(func->name))
2287 func->profiling_func = true;
1e7e4788
JP
2288 }
2289 }
2290
2291 return 0;
2292}
2293
4a60aa05
AX
2294static void mark_rodata(struct objtool_file *file)
2295{
2296 struct section *sec;
2297 bool found = false;
2298
2299 /*
87b512de
JP
2300 * Search for the following rodata sections, each of which can
2301 * potentially contain jump tables:
2302 *
2303 * - .rodata: can contain GCC switch tables
2304 * - .rodata.<func>: same, if -fdata-sections is being used
2305 * - .rodata..c_jump_table: contains C annotated jump tables
2306 *
2307 * .rodata.str1.* sections are ignored; they don't contain jump tables.
4a60aa05
AX
2308 */
2309 for_each_sec(file, sec) {
1ee44470
MS
2310 if (!strncmp(sec->name, ".rodata", 7) &&
2311 !strstr(sec->name, ".str1.")) {
4a60aa05
AX
2312 sec->rodata = true;
2313 found = true;
2314 }
2315 }
2316
2317 file->rodata = found;
2318}
2319
dcc914f4
JP
2320static int decode_sections(struct objtool_file *file)
2321{
2322 int ret;
2323
4a60aa05
AX
2324 mark_rodata(file);
2325
db2b0c5d
PZ
2326 ret = init_pv_ops(file);
2327 if (ret)
2328 return ret;
2329
dcc914f4
JP
2330 ret = decode_instructions(file);
2331 if (ret)
2332 return ret;
2333
dcc914f4 2334 add_ignores(file);
ea24213d 2335 add_uaccess_safe(file);
dcc914f4 2336
ff05ab23 2337 ret = add_ignore_alternatives(file);
258c7605
JP
2338 if (ret)
2339 return ret;
2340
08f87a93
PZ
2341 /*
2342 * Must be before read_unwind_hints() since that needs insn->noendbr.
2343 */
96db4a98
PZ
2344 ret = read_noendbr_hints(file);
2345 if (ret)
2346 return ret;
2347
a958c4fe
PZ
2348 /*
2349 * Must be before add_{jump_call}_destination.
2350 */
1739c66e 2351 ret = classify_symbols(file);
5b06fd3b
PZ
2352 if (ret)
2353 return ret;
2354
43d5430a 2355 /*
34c861e8
JP
2356 * Must be before add_jump_destinations(), which depends on 'func'
2357 * being set for alternatives, to enable proper sibling call detection.
43d5430a 2358 */
34c861e8 2359 ret = add_special_section_alts(file);
dcc914f4
JP
2360 if (ret)
2361 return ret;
2362
34c861e8 2363 ret = add_jump_destinations(file);
dcc914f4
JP
2364 if (ret)
2365 return ret;
2366
a958c4fe
PZ
2367 /*
2368 * Must be before add_call_destination(); it changes INSN_CALL to
2369 * INSN_JUMP.
2370 */
8aa8eb2a
AC
2371 ret = read_intra_function_calls(file);
2372 if (ret)
2373 return ret;
2374
a845c7cf 2375 ret = add_call_destinations(file);
dcc914f4
JP
2376 if (ret)
2377 return ret;
2378
0e5b613b
PZ
2379 /*
2380 * Must be after add_call_destinations() such that it can override
2381 * dead_end_function() marks.
2382 */
2383 ret = add_dead_ends(file);
2384 if (ret)
2385 return ret;
2386
e7c2bc37 2387 ret = add_jump_table_alts(file);
dcc914f4
JP
2388 if (ret)
2389 return ret;
2390
39358a03
JP
2391 ret = read_unwind_hints(file);
2392 if (ret)
2393 return ret;
2394
b5bc2231
PZ
2395 ret = read_retpoline_hints(file);
2396 if (ret)
2397 return ret;
2398
c4a33939
PZ
2399 ret = read_instr_hints(file);
2400 if (ret)
2401 return ret;
2402
dcc914f4
JP
2403 return 0;
2404}
2405
2406static bool is_fentry_call(struct instruction *insn)
2407{
1739c66e
PZ
2408 if (insn->type == INSN_CALL &&
2409 insn->call_dest &&
2410 insn->call_dest->fentry)
dcc914f4
JP
2411 return true;
2412
2413 return false;
2414}
2415
e25eea89 2416static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
dcc914f4 2417{
e7c0219b 2418 struct cfi_state *cfi = &state->cfi;
baa41469
JP
2419 int i;
2420
e7c0219b 2421 if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
e25eea89
PZ
2422 return true;
2423
b735bd3e 2424 if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
baa41469
JP
2425 return true;
2426
b735bd3e 2427 if (cfi->stack_size != initial_func_cfi.cfa.offset)
e25eea89
PZ
2428 return true;
2429
2430 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b
PZ
2431 if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
2432 cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
baa41469 2433 return true;
e25eea89 2434 }
baa41469
JP
2435
2436 return false;
2437}
2438
fb084fde
JT
2439static bool check_reg_frame_pos(const struct cfi_reg *reg,
2440 int expected_offset)
2441{
2442 return reg->base == CFI_CFA &&
2443 reg->offset == expected_offset;
2444}
2445
baa41469
JP
2446static bool has_valid_stack_frame(struct insn_state *state)
2447{
e7c0219b
PZ
2448 struct cfi_state *cfi = &state->cfi;
2449
fb084fde
JT
2450 if (cfi->cfa.base == CFI_BP &&
2451 check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
2452 check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
baa41469
JP
2453 return true;
2454
e7c0219b 2455 if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
baa41469
JP
2456 return true;
2457
2458 return false;
dcc914f4
JP
2459}
2460
e7c0219b
PZ
2461static int update_cfi_state_regs(struct instruction *insn,
2462 struct cfi_state *cfi,
65ea47dc 2463 struct stack_op *op)
627fce14 2464{
e7c0219b 2465 struct cfi_reg *cfa = &cfi->cfa;
627fce14 2466
d8dd25a4 2467 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
627fce14
JP
2468 return 0;
2469
2470 /* push */
ea24213d 2471 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
627fce14
JP
2472 cfa->offset += 8;
2473
2474 /* pop */
ea24213d 2475 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
627fce14
JP
2476 cfa->offset -= 8;
2477
2478 /* add immediate to sp */
2479 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
2480 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
2481 cfa->offset -= op->src.offset;
2482
2483 return 0;
2484}
2485
e7c0219b 2486static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
dcc914f4 2487{
bf4d1a83 2488 if (arch_callee_saved_reg(reg) &&
e7c0219b
PZ
2489 cfi->regs[reg].base == CFI_UNDEFINED) {
2490 cfi->regs[reg].base = base;
2491 cfi->regs[reg].offset = offset;
baa41469 2492 }
dcc914f4
JP
2493}
2494
e7c0219b 2495static void restore_reg(struct cfi_state *cfi, unsigned char reg)
dcc914f4 2496{
e7c0219b
PZ
2497 cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
2498 cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
baa41469
JP
2499}
2500
2501/*
2502 * A note about DRAP stack alignment:
2503 *
2504 * GCC has the concept of a DRAP register, which is used to help keep track of
2505 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
2506 * register. The typical DRAP pattern is:
2507 *
2508 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
2509 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
2510 * 41 ff 72 f8 pushq -0x8(%r10)
2511 * 55 push %rbp
2512 * 48 89 e5 mov %rsp,%rbp
2513 * (more pushes)
2514 * 41 52 push %r10
2515 * ...
2516 * 41 5a pop %r10
2517 * (more pops)
2518 * 5d pop %rbp
2519 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2520 * c3 retq
2521 *
2522 * There are some variations in the epilogues, like:
2523 *
2524 * 5b pop %rbx
2525 * 41 5a pop %r10
2526 * 41 5c pop %r12
2527 * 41 5d pop %r13
2528 * 41 5e pop %r14
2529 * c9 leaveq
2530 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2531 * c3 retq
2532 *
2533 * and:
2534 *
2535 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
2536 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
2537 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
2538 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
2539 * c9 leaveq
2540 * 49 8d 62 f8 lea -0x8(%r10),%rsp
2541 * c3 retq
2542 *
2543 * Sometimes r13 is used as the DRAP register, in which case it's saved and
2544 * restored beforehand:
2545 *
2546 * 41 55 push %r13
2547 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
2548 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
2549 * ...
2550 * 49 8d 65 f0 lea -0x10(%r13),%rsp
2551 * 41 5d pop %r13
2552 * c3 retq
2553 */
d54dba41
PZ
2554static int update_cfi_state(struct instruction *insn,
2555 struct instruction *next_insn,
2556 struct cfi_state *cfi, struct stack_op *op)
baa41469 2557{
e7c0219b
PZ
2558 struct cfi_reg *cfa = &cfi->cfa;
2559 struct cfi_reg *regs = cfi->regs;
baa41469
JP
2560
2561 /* stack operations don't make sense with an undefined CFA */
2562 if (cfa->base == CFI_UNDEFINED) {
2563 if (insn->func) {
2564 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
2565 return -1;
2566 }
2567 return 0;
2568 }
2569
ee819aed
JT
2570 if (cfi->type == UNWIND_HINT_TYPE_REGS ||
2571 cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
e7c0219b 2572 return update_cfi_state_regs(insn, cfi, op);
627fce14 2573
baa41469
JP
2574 switch (op->dest.type) {
2575
2576 case OP_DEST_REG:
2577 switch (op->src.type) {
2578
2579 case OP_SRC_REG:
0d0970ee
JP
2580 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
2581 cfa->base == CFI_SP &&
fb084fde 2582 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
0d0970ee
JP
2583
2584 /* mov %rsp, %rbp */
2585 cfa->base = op->dest.reg;
e7c0219b 2586 cfi->bp_scratch = false;
0d0970ee 2587 }
dd88a0a0 2588
0d0970ee 2589 else if (op->src.reg == CFI_SP &&
e7c0219b 2590 op->dest.reg == CFI_BP && cfi->drap) {
dd88a0a0 2591
0d0970ee
JP
2592 /* drap: mov %rsp, %rbp */
2593 regs[CFI_BP].base = CFI_BP;
e7c0219b
PZ
2594 regs[CFI_BP].offset = -cfi->stack_size;
2595 cfi->bp_scratch = false;
0d0970ee 2596 }
dd88a0a0 2597
0d0970ee
JP
2598 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
2599
2600 /*
2601 * mov %rsp, %reg
2602 *
2603 * This is needed for the rare case where GCC
2604 * does:
2605 *
2606 * mov %rsp, %rax
2607 * ...
2608 * mov %rax, %rsp
2609 */
e7c0219b
PZ
2610 cfi->vals[op->dest.reg].base = CFI_CFA;
2611 cfi->vals[op->dest.reg].offset = -cfi->stack_size;
dd88a0a0
JP
2612 }
2613
3c1f0583 2614 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
ffc7e74f 2615 (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
3c1f0583
JP
2616
2617 /*
2618 * mov %rbp, %rsp
2619 *
2620 * Restore the original stack pointer (Clang).
2621 */
e7c0219b 2622 cfi->stack_size = -cfi->regs[CFI_BP].offset;
3c1f0583
JP
2623 }
2624
dd88a0a0
JP
2625 else if (op->dest.reg == cfa->base) {
2626
2627 /* mov %reg, %rsp */
2628 if (cfa->base == CFI_SP &&
e7c0219b 2629 cfi->vals[op->src.reg].base == CFI_CFA) {
dd88a0a0
JP
2630
2631 /*
2632 * This is needed for the rare case
2633 * where GCC does something dumb like:
2634 *
2635 * lea 0x8(%rsp), %rcx
2636 * ...
2637 * mov %rcx, %rsp
2638 */
e7c0219b
PZ
2639 cfa->offset = -cfi->vals[op->src.reg].offset;
2640 cfi->stack_size = cfa->offset;
dd88a0a0 2641
aafeb14e
PZ
2642 } else if (cfa->base == CFI_SP &&
2643 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2644 cfi->vals[op->src.reg].offset == cfa->offset) {
2645
2646 /*
2647 * Stack swizzle:
2648 *
2649 * 1: mov %rsp, (%[tos])
2650 * 2: mov %[tos], %rsp
2651 * ...
2652 * 3: pop %rsp
2653 *
2654 * Where:
2655 *
2656 * 1 - places a pointer to the previous
2657 * stack at the Top-of-Stack of the
2658 * new stack.
2659 *
2660 * 2 - switches to the new stack.
2661 *
2662 * 3 - pops the Top-of-Stack to restore
2663 * the original stack.
2664 *
2665 * Note: we set base to SP_INDIRECT
2666 * here and preserve offset. Therefore
2667 * when the unwinder reaches ToS it
2668 * will dereference SP and then add the
2669 * offset to find the next frame, IOW:
2670 * (%rsp) + offset.
2671 */
2672 cfa->base = CFI_SP_INDIRECT;
2673
dd88a0a0
JP
2674 } else {
2675 cfa->base = CFI_UNDEFINED;
2676 cfa->offset = 0;
2677 }
baa41469
JP
2678 }
2679
724c8a23
PZ
2680 else if (op->dest.reg == CFI_SP &&
2681 cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
2682 cfi->vals[op->src.reg].offset == cfa->offset) {
2683
2684 /*
2685 * The same stack swizzle case 2) as above. But
2686 * because we can't change cfa->base, case 3)
2687 * will become a regular POP. Pretend we're a
2688 * PUSH so things don't go unbalanced.
2689 */
2690 cfi->stack_size += 8;
2691 }
2692
2693
baa41469
JP
2694 break;
2695
2696 case OP_SRC_ADD:
2697 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
2698
2699 /* add imm, %rsp */
e7c0219b 2700 cfi->stack_size -= op->src.offset;
baa41469
JP
2701 if (cfa->base == CFI_SP)
2702 cfa->offset -= op->src.offset;
2703 break;
2704 }
2705
2706 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
2707
2708 /* lea disp(%rbp), %rsp */
e7c0219b 2709 cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
baa41469
JP
2710 break;
2711 }
2712
468af56a
JT
2713 if (!cfi->drap && op->src.reg == CFI_SP &&
2714 op->dest.reg == CFI_BP && cfa->base == CFI_SP &&
2715 check_reg_frame_pos(&regs[CFI_BP], -cfa->offset + op->src.offset)) {
2716
2717 /* lea disp(%rsp), %rbp */
2718 cfa->base = CFI_BP;
2719 cfa->offset -= op->src.offset;
2720 cfi->bp_scratch = false;
2721 break;
2722 }
2723
dd88a0a0 2724 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
baa41469
JP
2725
2726 /* drap: lea disp(%rsp), %drap */
e7c0219b 2727 cfi->drap_reg = op->dest.reg;
dd88a0a0
JP
2728
2729 /*
2730 * lea disp(%rsp), %reg
2731 *
2732 * This is needed for the rare case where GCC
2733 * does something dumb like:
2734 *
2735 * lea 0x8(%rsp), %rcx
2736 * ...
2737 * mov %rcx, %rsp
2738 */
e7c0219b
PZ
2739 cfi->vals[op->dest.reg].base = CFI_CFA;
2740 cfi->vals[op->dest.reg].offset = \
2741 -cfi->stack_size + op->src.offset;
dd88a0a0 2742
baa41469
JP
2743 break;
2744 }
2745
e7c0219b
PZ
2746 if (cfi->drap && op->dest.reg == CFI_SP &&
2747 op->src.reg == cfi->drap_reg) {
baa41469
JP
2748
2749 /* drap: lea disp(%drap), %rsp */
2750 cfa->base = CFI_SP;
e7c0219b
PZ
2751 cfa->offset = cfi->stack_size = -op->src.offset;
2752 cfi->drap_reg = CFI_UNDEFINED;
2753 cfi->drap = false;
baa41469
JP
2754 break;
2755 }
2756
d54dba41 2757 if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
baa41469
JP
2758 WARN_FUNC("unsupported stack register modification",
2759 insn->sec, insn->offset);
2760 return -1;
2761 }
2762
2763 break;
2764
2765 case OP_SRC_AND:
2766 if (op->dest.reg != CFI_SP ||
e7c0219b
PZ
2767 (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
2768 (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
baa41469
JP
2769 WARN_FUNC("unsupported stack pointer realignment",
2770 insn->sec, insn->offset);
2771 return -1;
2772 }
2773
e7c0219b 2774 if (cfi->drap_reg != CFI_UNDEFINED) {
baa41469 2775 /* drap: and imm, %rsp */
e7c0219b
PZ
2776 cfa->base = cfi->drap_reg;
2777 cfa->offset = cfi->stack_size = 0;
2778 cfi->drap = true;
baa41469
JP
2779 }
2780
2781 /*
2782 * Older versions of GCC (4.8ish) realign the stack
2783 * without DRAP, with a frame pointer.
2784 */
2785
2786 break;
2787
2788 case OP_SRC_POP:
ea24213d 2789 case OP_SRC_POPF:
aafeb14e
PZ
2790 if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2791
2792 /* pop %rsp; # restore from a stack swizzle */
2793 cfa->base = CFI_SP;
2794 break;
2795 }
2796
e7c0219b 2797 if (!cfi->drap && op->dest.reg == cfa->base) {
baa41469
JP
2798
2799 /* pop %rbp */
2800 cfa->base = CFI_SP;
2801 }
2802
e7c0219b
PZ
2803 if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
2804 op->dest.reg == cfi->drap_reg &&
2805 cfi->drap_offset == -cfi->stack_size) {
baa41469 2806
bf4d1a83 2807 /* drap: pop %drap */
e7c0219b 2808 cfa->base = cfi->drap_reg;
bf4d1a83 2809 cfa->offset = 0;
e7c0219b 2810 cfi->drap_offset = -1;
baa41469 2811
ffc7e74f 2812 } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
baa41469 2813
bf4d1a83 2814 /* pop %reg */
e7c0219b 2815 restore_reg(cfi, op->dest.reg);
baa41469
JP
2816 }
2817
e7c0219b 2818 cfi->stack_size -= 8;
baa41469
JP
2819 if (cfa->base == CFI_SP)
2820 cfa->offset -= 8;
2821
2822 break;
2823
2824 case OP_SRC_REG_INDIRECT:
201ef5a9
JT
2825 if (!cfi->drap && op->dest.reg == cfa->base &&
2826 op->dest.reg == CFI_BP) {
2827
2828 /* mov disp(%rsp), %rbp */
2829 cfa->base = CFI_SP;
2830 cfa->offset = cfi->stack_size;
2831 }
2832
e7c0219b
PZ
2833 if (cfi->drap && op->src.reg == CFI_BP &&
2834 op->src.offset == cfi->drap_offset) {
bf4d1a83
JP
2835
2836 /* drap: mov disp(%rbp), %drap */
e7c0219b 2837 cfa->base = cfi->drap_reg;
bf4d1a83 2838 cfa->offset = 0;
e7c0219b 2839 cfi->drap_offset = -1;
bf4d1a83
JP
2840 }
2841
e7c0219b 2842 if (cfi->drap && op->src.reg == CFI_BP &&
baa41469
JP
2843 op->src.offset == regs[op->dest.reg].offset) {
2844
2845 /* drap: mov disp(%rbp), %reg */
e7c0219b 2846 restore_reg(cfi, op->dest.reg);
baa41469
JP
2847
2848 } else if (op->src.reg == cfa->base &&
2849 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
2850
2851 /* mov disp(%rbp), %reg */
2852 /* mov disp(%rsp), %reg */
e7c0219b 2853 restore_reg(cfi, op->dest.reg);
201ef5a9
JT
2854
2855 } else if (op->src.reg == CFI_SP &&
2856 op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
2857
2858 /* mov disp(%rsp), %reg */
2859 restore_reg(cfi, op->dest.reg);
baa41469
JP
2860 }
2861
2862 break;
2863
2864 default:
2865 WARN_FUNC("unknown stack-related instruction",
2866 insn->sec, insn->offset);
2867 return -1;
2868 }
2869
2870 break;
2871
2872 case OP_DEST_PUSH:
ea24213d 2873 case OP_DEST_PUSHF:
e7c0219b 2874 cfi->stack_size += 8;
baa41469
JP
2875 if (cfa->base == CFI_SP)
2876 cfa->offset += 8;
2877
2878 if (op->src.type != OP_SRC_REG)
2879 break;
2880
e7c0219b
PZ
2881 if (cfi->drap) {
2882 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
baa41469
JP
2883
2884 /* drap: push %drap */
2885 cfa->base = CFI_BP_INDIRECT;
e7c0219b 2886 cfa->offset = -cfi->stack_size;
baa41469 2887
bf4d1a83 2888 /* save drap so we know when to restore it */
e7c0219b 2889 cfi->drap_offset = -cfi->stack_size;
baa41469 2890
e7c0219b 2891 } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
baa41469
JP
2892
2893 /* drap: push %rbp */
e7c0219b 2894 cfi->stack_size = 0;
baa41469 2895
f4f80398 2896 } else {
baa41469
JP
2897
2898 /* drap: push %reg */
e7c0219b 2899 save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
baa41469
JP
2900 }
2901
2902 } else {
2903
2904 /* push %reg */
e7c0219b 2905 save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
baa41469
JP
2906 }
2907
2908 /* detect when asm code uses rbp as a scratch register */
72064474 2909 if (opts.stackval && insn->func && op->src.reg == CFI_BP &&
baa41469 2910 cfa->base != CFI_BP)
e7c0219b 2911 cfi->bp_scratch = true;
baa41469
JP
2912 break;
2913
2914 case OP_DEST_REG_INDIRECT:
2915
e7c0219b
PZ
2916 if (cfi->drap) {
2917 if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
baa41469
JP
2918
2919 /* drap: mov %drap, disp(%rbp) */
2920 cfa->base = CFI_BP_INDIRECT;
2921 cfa->offset = op->dest.offset;
2922
bf4d1a83 2923 /* save drap offset so we know when to restore it */
e7c0219b 2924 cfi->drap_offset = op->dest.offset;
f4f80398 2925 } else {
baa41469
JP
2926
2927 /* drap: mov reg, disp(%rbp) */
e7c0219b 2928 save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
baa41469
JP
2929 }
2930
2931 } else if (op->dest.reg == cfa->base) {
2932
2933 /* mov reg, disp(%rbp) */
2934 /* mov reg, disp(%rsp) */
e7c0219b
PZ
2935 save_reg(cfi, op->src.reg, CFI_CFA,
2936 op->dest.offset - cfi->cfa.offset);
201ef5a9
JT
2937
2938 } else if (op->dest.reg == CFI_SP) {
2939
2940 /* mov reg, disp(%rsp) */
2941 save_reg(cfi, op->src.reg, CFI_CFA,
2942 op->dest.offset - cfi->stack_size);
aafeb14e
PZ
2943
2944 } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2945
2946 /* mov %rsp, (%reg); # setup a stack swizzle. */
2947 cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2948 cfi->vals[op->dest.reg].offset = cfa->offset;
baa41469
JP
2949 }
2950
2951 break;
2952
baa41469 2953 case OP_DEST_MEM:
ea24213d 2954 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
baa41469
JP
2955 WARN_FUNC("unknown stack-related memory operation",
2956 insn->sec, insn->offset);
2957 return -1;
2958 }
2959
2960 /* pop mem */
e7c0219b 2961 cfi->stack_size -= 8;
baa41469
JP
2962 if (cfa->base == CFI_SP)
2963 cfa->offset -= 8;
2964
2965 break;
2966
2967 default:
2968 WARN_FUNC("unknown stack-related instruction",
2969 insn->sec, insn->offset);
2970 return -1;
2971 }
2972
2973 return 0;
2974}
2975
c9c324dc
JP
2976/*
2977 * The stack layouts of alternatives instructions can sometimes diverge when
2978 * they have stack modifications. That's fine as long as the potential stack
2979 * layouts don't conflict at any given potential instruction boundary.
2980 *
2981 * Flatten the CFIs of the different alternative code streams (both original
2982 * and replacement) into a single shared CFI array which can be used to detect
2983 * conflicts and nicely feed a linear array of ORC entries to the unwinder.
2984 */
2985static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
65ea47dc 2986{
c9c324dc
JP
2987 struct cfi_state **alt_cfi;
2988 int group_off;
65ea47dc 2989
c9c324dc
JP
2990 if (!insn->alt_group)
2991 return 0;
65ea47dc 2992
8b946cc3
PZ
2993 if (!insn->cfi) {
2994 WARN("CFI missing");
2995 return -1;
2996 }
2997
c9c324dc
JP
2998 alt_cfi = insn->alt_group->cfi;
2999 group_off = insn->offset - insn->alt_group->first_insn->offset;
65ea47dc 3000
c9c324dc 3001 if (!alt_cfi[group_off]) {
8b946cc3 3002 alt_cfi[group_off] = insn->cfi;
c9c324dc 3003 } else {
8b946cc3 3004 if (cficmp(alt_cfi[group_off], insn->cfi)) {
c9c324dc
JP
3005 WARN_FUNC("stack layout conflict in alternatives",
3006 insn->sec, insn->offset);
ab3852ab
PZ
3007 return -1;
3008 }
c9c324dc
JP
3009 }
3010
3011 return 0;
3012}
3013
d54dba41
PZ
3014static int handle_insn_ops(struct instruction *insn,
3015 struct instruction *next_insn,
3016 struct insn_state *state)
c9c324dc
JP
3017{
3018 struct stack_op *op;
3019
3020 list_for_each_entry(op, &insn->stack_ops, list) {
3021
d54dba41 3022 if (update_cfi_state(insn, next_insn, &state->cfi, op))
c9c324dc 3023 return 1;
ab3852ab 3024
ba08abca
PZ
3025 if (!insn->alt_group)
3026 continue;
3027
65ea47dc
JT
3028 if (op->dest.type == OP_DEST_PUSHF) {
3029 if (!state->uaccess_stack) {
3030 state->uaccess_stack = 1;
3031 } else if (state->uaccess_stack >> 31) {
3032 WARN_FUNC("PUSHF stack exhausted",
3033 insn->sec, insn->offset);
3034 return 1;
3035 }
3036 state->uaccess_stack <<= 1;
3037 state->uaccess_stack |= state->uaccess;
3038 }
3039
3040 if (op->src.type == OP_SRC_POPF) {
3041 if (state->uaccess_stack) {
3042 state->uaccess = state->uaccess_stack & 1;
3043 state->uaccess_stack >>= 1;
3044 if (state->uaccess_stack == 1)
3045 state->uaccess_stack = 0;
3046 }
3047 }
3048 }
3049
3050 return 0;
3051}
3052
e7c0219b 3053static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
baa41469 3054{
8b946cc3 3055 struct cfi_state *cfi1 = insn->cfi;
baa41469
JP
3056 int i;
3057
8b946cc3
PZ
3058 if (!cfi1) {
3059 WARN("CFI missing");
3060 return false;
3061 }
3062
e7c0219b
PZ
3063 if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
3064
baa41469
JP
3065 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
3066 insn->sec, insn->offset,
e7c0219b
PZ
3067 cfi1->cfa.base, cfi1->cfa.offset,
3068 cfi2->cfa.base, cfi2->cfa.offset);
baa41469 3069
e7c0219b 3070 } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
baa41469 3071 for (i = 0; i < CFI_NUM_REGS; i++) {
e7c0219b 3072 if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
baa41469
JP
3073 sizeof(struct cfi_reg)))
3074 continue;
3075
3076 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
3077 insn->sec, insn->offset,
e7c0219b
PZ
3078 i, cfi1->regs[i].base, cfi1->regs[i].offset,
3079 i, cfi2->regs[i].base, cfi2->regs[i].offset);
baa41469
JP
3080 break;
3081 }
3082
e7c0219b
PZ
3083 } else if (cfi1->type != cfi2->type) {
3084
627fce14 3085 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
e7c0219b
PZ
3086 insn->sec, insn->offset, cfi1->type, cfi2->type);
3087
3088 } else if (cfi1->drap != cfi2->drap ||
3089 (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
3090 (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
627fce14 3091
bf4d1a83 3092 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
baa41469 3093 insn->sec, insn->offset,
e7c0219b
PZ
3094 cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
3095 cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
baa41469
JP
3096
3097 } else
3098 return true;
3099
3100 return false;
dcc914f4
JP
3101}
3102
ea24213d
PZ
3103static inline bool func_uaccess_safe(struct symbol *func)
3104{
3105 if (func)
e10cd8fe 3106 return func->uaccess_safe;
ea24213d
PZ
3107
3108 return false;
3109}
3110
0c1ddd33 3111static inline const char *call_dest_name(struct instruction *insn)
ea24213d 3112{
82880283 3113 static char pvname[19];
db2b0c5d
PZ
3114 struct reloc *rel;
3115 int idx;
3116
ea24213d
PZ
3117 if (insn->call_dest)
3118 return insn->call_dest->name;
3119
db2b0c5d
PZ
3120 rel = insn_reloc(NULL, insn);
3121 if (rel && !strcmp(rel->sym->name, "pv_ops")) {
3122 idx = (rel->addend / sizeof(void *));
3123 snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
3124 return pvname;
3125 }
3126
ea24213d
PZ
3127 return "{dynamic}";
3128}
3129
db2b0c5d
PZ
3130static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
3131{
3132 struct symbol *target;
3133 struct reloc *rel;
3134 int idx;
3135
3136 rel = insn_reloc(file, insn);
3137 if (!rel || strcmp(rel->sym->name, "pv_ops"))
3138 return false;
3139
3140 idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *));
3141
3142 if (file->pv_ops[idx].clean)
3143 return true;
3144
3145 file->pv_ops[idx].clean = true;
3146
3147 list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
3148 if (!target->sec->noinstr) {
3149 WARN("pv_ops[%d]: %s", idx, target->name);
3150 file->pv_ops[idx].clean = false;
3151 }
3152 }
3153
3154 return file->pv_ops[idx].clean;
3155}
3156
3157static inline bool noinstr_call_dest(struct objtool_file *file,
3158 struct instruction *insn,
3159 struct symbol *func)
6b643a07
PZ
3160{
3161 /*
3162 * We can't deal with indirect function calls at present;
3163 * assume they're instrumented.
3164 */
db2b0c5d
PZ
3165 if (!func) {
3166 if (file->pv_ops)
3167 return pv_call_dest(file, insn);
3168
6b643a07 3169 return false;
db2b0c5d 3170 }
6b643a07
PZ
3171
3172 /*
3173 * If the symbol is from a noinstr section; we good.
3174 */
3175 if (func->sec->noinstr)
3176 return true;
3177
3178 /*
3179 * The __ubsan_handle_*() calls are like WARN(), they only happen when
3180 * something 'BAD' happened. At the risk of taking the machine down,
3181 * let them proceed to get the message out.
3182 */
3183 if (!strncmp(func->name, "__ubsan_handle_", 15))
3184 return true;
3185
3186 return false;
3187}
3188
db2b0c5d
PZ
3189static int validate_call(struct objtool_file *file,
3190 struct instruction *insn,
3191 struct insn_state *state)
ea24213d 3192{
c4a33939 3193 if (state->noinstr && state->instr <= 0 &&
db2b0c5d 3194 !noinstr_call_dest(file, insn, insn->call_dest)) {
c4a33939
PZ
3195 WARN_FUNC("call to %s() leaves .noinstr.text section",
3196 insn->sec, insn->offset, call_dest_name(insn));
3197 return 1;
3198 }
3199
ea24213d
PZ
3200 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
3201 WARN_FUNC("call to %s() with UACCESS enabled",
0c1ddd33 3202 insn->sec, insn->offset, call_dest_name(insn));
ea24213d
PZ
3203 return 1;
3204 }
3205
2f0f9e9a
PZ
3206 if (state->df) {
3207 WARN_FUNC("call to %s() with DF set",
0c1ddd33 3208 insn->sec, insn->offset, call_dest_name(insn));
2f0f9e9a
PZ
3209 return 1;
3210 }
3211
ea24213d
PZ
3212 return 0;
3213}
3214
db2b0c5d
PZ
3215static int validate_sibling_call(struct objtool_file *file,
3216 struct instruction *insn,
3217 struct insn_state *state)
54262aa2 3218{
e25eea89 3219 if (has_modified_stack_frame(insn, state)) {
54262aa2
PZ
3220 WARN_FUNC("sibling call from callable instruction with modified stack frame",
3221 insn->sec, insn->offset);
3222 return 1;
3223 }
3224
db2b0c5d 3225 return validate_call(file, insn, state);
54262aa2
PZ
3226}
3227
a92e92d1
PZ
3228static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
3229{
c4a33939
PZ
3230 if (state->noinstr && state->instr > 0) {
3231 WARN_FUNC("return with instrumentation enabled",
3232 insn->sec, insn->offset);
3233 return 1;
3234 }
3235
a92e92d1
PZ
3236 if (state->uaccess && !func_uaccess_safe(func)) {
3237 WARN_FUNC("return with UACCESS enabled",
3238 insn->sec, insn->offset);
3239 return 1;
3240 }
3241
3242 if (!state->uaccess && func_uaccess_safe(func)) {
3243 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
3244 insn->sec, insn->offset);
3245 return 1;
3246 }
3247
3248 if (state->df) {
3249 WARN_FUNC("return with DF set",
3250 insn->sec, insn->offset);
3251 return 1;
3252 }
3253
e25eea89 3254 if (func && has_modified_stack_frame(insn, state)) {
a92e92d1
PZ
3255 WARN_FUNC("return with modified stack frame",
3256 insn->sec, insn->offset);
3257 return 1;
3258 }
3259
e7c0219b 3260 if (state->cfi.bp_scratch) {
b2966952
JP
3261 WARN_FUNC("BP used as a scratch register",
3262 insn->sec, insn->offset);
a92e92d1
PZ
3263 return 1;
3264 }
3265
3266 return 0;
3267}
3268
c9c324dc
JP
3269static struct instruction *next_insn_to_validate(struct objtool_file *file,
3270 struct instruction *insn)
7117f16b 3271{
b23cc71c 3272 struct alt_group *alt_group = insn->alt_group;
7117f16b 3273
c9c324dc
JP
3274 /*
3275 * Simulate the fact that alternatives are patched in-place. When the
3276 * end of a replacement alt_group is reached, redirect objtool flow to
3277 * the end of the original alt_group.
3278 */
3279 if (alt_group && insn == alt_group->last_insn && alt_group->orig_group)
3280 return next_insn_same_sec(file, alt_group->orig_group->last_insn);
3281
3282 return next_insn_same_sec(file, insn);
7117f16b
PZ
3283}
3284
dcc914f4
JP
3285/*
3286 * Follow the branch starting at the given instruction, and recursively follow
3287 * any other branches (jumps). Meanwhile, track the frame pointer state at
3288 * each instruction and validate all the rules described in
3289 * tools/objtool/Documentation/stack-validation.txt.
3290 */
c705cecc 3291static int validate_branch(struct objtool_file *file, struct symbol *func,
b7460462 3292 struct instruction *insn, struct insn_state state)
dcc914f4
JP
3293{
3294 struct alternative *alt;
8b946cc3 3295 struct instruction *next_insn, *prev_insn = NULL;
dcc914f4 3296 struct section *sec;
882a0db9 3297 u8 visited;
dcc914f4
JP
3298 int ret;
3299
dcc914f4 3300 sec = insn->sec;
dcc914f4 3301
dcc914f4 3302 while (1) {
c9c324dc 3303 next_insn = next_insn_to_validate(file, insn);
39358a03 3304
08feafe8 3305 if (func && insn->func && func != insn->func->pfunc) {
ee97638b
JP
3306 WARN("%s() falls through to next function %s()",
3307 func->name, insn->func->name);
3308 return 1;
dcc914f4
JP
3309 }
3310
4855022a
JP
3311 if (func && insn->ignore) {
3312 WARN_FUNC("BUG: why am I validating an ignored function?",
3313 sec, insn->offset);
12b25729 3314 return 1;
4855022a
JP
3315 }
3316
a09a6e23
PZ
3317 visited = VISITED_BRANCH << state.uaccess;
3318 if (insn->visited & VISITED_BRANCH_MASK) {
e7c0219b 3319 if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
dcc914f4 3320 return 1;
dcc914f4 3321
882a0db9 3322 if (insn->visited & visited)
ea24213d 3323 return 0;
8b946cc3
PZ
3324 } else {
3325 nr_insns_visited++;
dcc914f4
JP
3326 }
3327
c4a33939
PZ
3328 if (state.noinstr)
3329 state.instr += insn->instr;
3330
8b946cc3
PZ
3331 if (insn->hint) {
3332 state.cfi = *insn->cfi;
3333 } else {
3334 /* XXX track if we actually changed state.cfi */
3335
3336 if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
3337 insn->cfi = prev_insn->cfi;
3338 nr_cfi_reused++;
3339 } else {
3340 insn->cfi = cfi_hash_find_or_add(&state.cfi);
3341 }
3342 }
dcc914f4 3343
882a0db9 3344 insn->visited |= visited;
baa41469 3345
c9c324dc
JP
3346 if (propagate_alt_cfi(file, insn))
3347 return 1;
3348
7117f16b 3349 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
764eef4b
PZ
3350 bool skip_orig = false;
3351
a845c7cf 3352 list_for_each_entry(alt, &insn->alts, list) {
764eef4b
PZ
3353 if (alt->skip_orig)
3354 skip_orig = true;
3355
c705cecc 3356 ret = validate_branch(file, func, alt->insn, state);
7697eee3 3357 if (ret) {
2daf7fab 3358 if (opts.backtrace)
7697eee3
PZ
3359 BT_FUNC("(alt)", insn);
3360 return ret;
3361 }
a845c7cf 3362 }
764eef4b
PZ
3363
3364 if (skip_orig)
3365 return 0;
dcc914f4
JP
3366 }
3367
d54dba41 3368 if (handle_insn_ops(insn, next_insn, &state))
60041bcd
PZ
3369 return 1;
3370
dcc914f4
JP
3371 switch (insn->type) {
3372
dcc914f4 3373 case INSN_RETURN:
a92e92d1 3374 return validate_return(func, insn, &state);
dcc914f4
JP
3375
3376 case INSN_CALL:
ea24213d 3377 case INSN_CALL_DYNAMIC:
db2b0c5d 3378 ret = validate_call(file, insn, &state);
ea24213d
PZ
3379 if (ret)
3380 return ret;
dcc914f4 3381
72064474 3382 if (opts.stackval && func && !is_fentry_call(insn) &&
c9bab22b 3383 !has_valid_stack_frame(&state)) {
dcc914f4
JP
3384 WARN_FUNC("call without frame pointer save/setup",
3385 sec, insn->offset);
3386 return 1;
3387 }
c9bab22b 3388
0e5b613b 3389 if (insn->dead_end)
c9bab22b
JP
3390 return 0;
3391
dcc914f4
JP
3392 break;
3393
3394 case INSN_JUMP_CONDITIONAL:
3395 case INSN_JUMP_UNCONDITIONAL:
ecf11ba4 3396 if (is_sibling_call(insn)) {
db2b0c5d 3397 ret = validate_sibling_call(file, insn, &state);
dcc914f4 3398 if (ret)
54262aa2 3399 return ret;
4855022a 3400
0c1ddd33 3401 } else if (insn->jump_dest) {
c705cecc
JP
3402 ret = validate_branch(file, func,
3403 insn->jump_dest, state);
7697eee3 3404 if (ret) {
2daf7fab 3405 if (opts.backtrace)
7697eee3
PZ
3406 BT_FUNC("(branch)", insn);
3407 return ret;
3408 }
4855022a 3409 }
dcc914f4
JP
3410
3411 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3412 return 0;
3413
3414 break;
3415
3416 case INSN_JUMP_DYNAMIC:
b68b9907 3417 case INSN_JUMP_DYNAMIC_CONDITIONAL:
ecf11ba4 3418 if (is_sibling_call(insn)) {
db2b0c5d 3419 ret = validate_sibling_call(file, insn, &state);
54262aa2
PZ
3420 if (ret)
3421 return ret;
dcc914f4
JP
3422 }
3423
b68b9907
JP
3424 if (insn->type == INSN_JUMP_DYNAMIC)
3425 return 0;
3426
3427 break;
dcc914f4 3428
39358a03
JP
3429 case INSN_CONTEXT_SWITCH:
3430 if (func && (!next_insn || !next_insn->hint)) {
3431 WARN_FUNC("unsupported instruction in callable function",
3432 sec, insn->offset);
3433 return 1;
3434 }
3435 return 0;
3436
ea24213d
PZ
3437 case INSN_STAC:
3438 if (state.uaccess) {
3439 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
3440 return 1;
3441 }
3442
3443 state.uaccess = true;
3444 break;
3445
3446 case INSN_CLAC:
c705cecc 3447 if (!state.uaccess && func) {
ea24213d
PZ
3448 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
3449 return 1;
3450 }
3451
3452 if (func_uaccess_safe(func) && !state.uaccess_stack) {
3453 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
3454 return 1;
3455 }
3456
3457 state.uaccess = false;
baa41469
JP
3458 break;
3459
2f0f9e9a 3460 case INSN_STD:
6f567c93 3461 if (state.df) {
2f0f9e9a 3462 WARN_FUNC("recursive STD", sec, insn->offset);
6f567c93
JP
3463 return 1;
3464 }
2f0f9e9a
PZ
3465
3466 state.df = true;
3467 break;
3468
3469 case INSN_CLD:
6f567c93 3470 if (!state.df && func) {
2f0f9e9a 3471 WARN_FUNC("redundant CLD", sec, insn->offset);
6f567c93
JP
3472 return 1;
3473 }
2f0f9e9a
PZ
3474
3475 state.df = false;
baa41469
JP
3476 break;
3477
dcc914f4
JP
3478 default:
3479 break;
3480 }
3481
3482 if (insn->dead_end)
3483 return 0;
3484
00d96180 3485 if (!next_insn) {
e7c0219b 3486 if (state.cfi.cfa.base == CFI_UNDEFINED)
00d96180 3487 return 0;
dcc914f4
JP
3488 WARN("%s: unexpected end of section", sec->name);
3489 return 1;
3490 }
00d96180 3491
8b946cc3 3492 prev_insn = insn;
00d96180 3493 insn = next_insn;
dcc914f4
JP
3494 }
3495
3496 return 0;
3497}
3498
932f8e98 3499static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
39358a03
JP
3500{
3501 struct instruction *insn;
39358a03 3502 struct insn_state state;
932f8e98 3503 int ret, warnings = 0;
39358a03
JP
3504
3505 if (!file->hints)
3506 return 0;
3507
753da417 3508 init_insn_state(file, &state, sec);
39358a03 3509
932f8e98
PZ
3510 if (sec) {
3511 insn = find_insn(file, sec, 0);
3512 if (!insn)
3513 return 0;
3514 } else {
3515 insn = list_first_entry(&file->insn_list, typeof(*insn), list);
3516 }
3517
3518 while (&insn->list != &file->insn_list && (!sec || insn->sec == sec)) {
5b284b19 3519 if (insn->hint && !insn->visited && !insn->ignore) {
c705cecc 3520 ret = validate_branch(file, insn->func, insn, state);
2daf7fab 3521 if (ret && opts.backtrace)
7697eee3 3522 BT_FUNC("<=== (hint)", insn);
39358a03
JP
3523 warnings += ret;
3524 }
932f8e98
PZ
3525
3526 insn = list_next_entry(insn, list);
39358a03
JP
3527 }
3528
3529 return warnings;
3530}
3531
a09a6e23
PZ
3532/*
3533 * Validate rethunk entry constraint: must untrain RET before the first RET.
3534 *
3535 * Follow every branch (intra-function) and ensure ANNOTATE_UNRET_END comes
3536 * before an actual RET instruction.
3537 */
3538static int validate_entry(struct objtool_file *file, struct instruction *insn)
3539{
3540 struct instruction *next, *dest;
3541 int ret, warnings = 0;
3542
3543 for (;;) {
3544 next = next_insn_to_validate(file, insn);
3545
3546 if (insn->visited & VISITED_ENTRY)
3547 return 0;
3548
3549 insn->visited |= VISITED_ENTRY;
3550
3551 if (!insn->ignore_alts && !list_empty(&insn->alts)) {
3552 struct alternative *alt;
3553 bool skip_orig = false;
3554
3555 list_for_each_entry(alt, &insn->alts, list) {
3556 if (alt->skip_orig)
3557 skip_orig = true;
3558
3559 ret = validate_entry(file, alt->insn);
3560 if (ret) {
3561 if (opts.backtrace)
3562 BT_FUNC("(alt)", insn);
3563 return ret;
3564 }
3565 }
3566
3567 if (skip_orig)
3568 return 0;
3569 }
3570
3571 switch (insn->type) {
3572
3573 case INSN_CALL_DYNAMIC:
3574 case INSN_JUMP_DYNAMIC:
3575 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3576 WARN_FUNC("early indirect call", insn->sec, insn->offset);
3577 return 1;
3578
3579 case INSN_JUMP_UNCONDITIONAL:
3580 case INSN_JUMP_CONDITIONAL:
3581 if (!is_sibling_call(insn)) {
3582 if (!insn->jump_dest) {
3583 WARN_FUNC("unresolved jump target after linking?!?",
3584 insn->sec, insn->offset);
3585 return -1;
3586 }
3587 ret = validate_entry(file, insn->jump_dest);
3588 if (ret) {
3589 if (opts.backtrace) {
3590 BT_FUNC("(branch%s)", insn,
3591 insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
3592 }
3593 return ret;
3594 }
3595
3596 if (insn->type == INSN_JUMP_UNCONDITIONAL)
3597 return 0;
3598
3599 break;
3600 }
3601
3602 /* fallthrough */
3603 case INSN_CALL:
3604 dest = find_insn(file, insn->call_dest->sec,
3605 insn->call_dest->offset);
3606 if (!dest) {
3607 WARN("Unresolved function after linking!?: %s",
3608 insn->call_dest->name);
3609 return -1;
3610 }
3611
3612 ret = validate_entry(file, dest);
3613 if (ret) {
3614 if (opts.backtrace)
3615 BT_FUNC("(call)", insn);
3616 return ret;
3617 }
3618 /*
3619 * If a call returns without error, it must have seen UNTRAIN_RET.
3620 * Therefore any non-error return is a success.
3621 */
3622 return 0;
3623
3624 case INSN_RETURN:
3625 WARN_FUNC("RET before UNTRAIN", insn->sec, insn->offset);
3626 return 1;
3627
3628 case INSN_NOP:
3629 if (insn->retpoline_safe)
3630 return 0;
3631 break;
3632
3633 default:
3634 break;
3635 }
3636
3637 if (!next) {
3638 WARN_FUNC("teh end!", insn->sec, insn->offset);
3639 return -1;
3640 }
3641 insn = next;
3642 }
3643
3644 return warnings;
3645}
3646
3647/*
3648 * Validate that all branches starting at 'insn->entry' encounter UNRET_END
3649 * before RET.
3650 */
3651static int validate_unret(struct objtool_file *file)
3652{
3653 struct instruction *insn;
3654 int ret, warnings = 0;
3655
3656 for_each_insn(file, insn) {
3657 if (!insn->entry)
3658 continue;
3659
3660 ret = validate_entry(file, insn);
3661 if (ret < 0) {
3662 WARN_FUNC("Failed UNRET validation", insn->sec, insn->offset);
3663 return ret;
3664 }
3665 warnings += ret;
3666 }
3667
3668 return warnings;
3669}
3670
b5bc2231
PZ
3671static int validate_retpoline(struct objtool_file *file)
3672{
3673 struct instruction *insn;
3674 int warnings = 0;
3675
3676 for_each_insn(file, insn) {
3677 if (insn->type != INSN_JUMP_DYNAMIC &&
9bb2ec60
PZ
3678 insn->type != INSN_CALL_DYNAMIC &&
3679 insn->type != INSN_RETURN)
b5bc2231
PZ
3680 continue;
3681
3682 if (insn->retpoline_safe)
3683 continue;
3684
ca41b97e
PZ
3685 /*
3686 * .init.text code is ran before userspace and thus doesn't
3687 * strictly need retpolines, except for modules which are
3688 * loaded late, they very much do need retpoline in their
3689 * .init.text
3690 */
2daf7fab 3691 if (!strcmp(insn->sec->name, ".init.text") && !opts.module)
ca41b97e
PZ
3692 continue;
3693
9bb2ec60
PZ
3694 if (insn->type == INSN_RETURN) {
3695 WARN_FUNC("'naked' return found in RETPOLINE build",
3696 insn->sec, insn->offset);
3697 } else {
3698 WARN_FUNC("indirect %s found in RETPOLINE build",
3699 insn->sec, insn->offset,
3700 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
3701 }
b5bc2231
PZ
3702
3703 warnings++;
3704 }
3705
3706 return warnings;
3707}
3708
dcc914f4
JP
3709static bool is_kasan_insn(struct instruction *insn)
3710{
3711 return (insn->type == INSN_CALL &&
3712 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
3713}
3714
3715static bool is_ubsan_insn(struct instruction *insn)
3716{
3717 return (insn->type == INSN_CALL &&
3718 !strcmp(insn->call_dest->name,
3719 "__ubsan_handle_builtin_unreachable"));
3720}
3721
14db1f0a 3722static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
dcc914f4
JP
3723{
3724 int i;
14db1f0a 3725 struct instruction *prev_insn;
dcc914f4 3726
1ffbe4e9 3727 if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
baa41469
JP
3728 return true;
3729
3730 /*
82a8954a 3731 * Ignore alternative replacement instructions. This can happen
0e2bb2bc 3732 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 3733 */
82a8954a 3734 if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
0e2bb2bc 3735 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
3736 return true;
3737
4adb2368 3738 /*
753da417 3739 * Whole archive runs might encounter dead code from weak symbols.
4adb2368
PZ
3740 * This is where the linker will have dropped the weak symbol in
3741 * favour of a regular symbol, but leaves the code in place.
3742 *
3743 * In this case we'll find a piece of code (whole function) that is not
3744 * covered by a !section symbol. Ignore them.
3745 */
753da417 3746 if (opts.link && !insn->func) {
4adb2368
PZ
3747 int size = find_symbol_hole_containing(insn->sec, insn->offset);
3748 unsigned long end = insn->offset + size;
3749
3750 if (!size) /* not a hole */
3751 return false;
3752
3753 if (size < 0) /* hole until the end */
3754 return true;
3755
3756 sec_for_each_insn_continue(file, insn) {
3757 /*
3758 * If we reach a visited instruction at or before the
3759 * end of the hole, ignore the unreachable.
3760 */
3761 if (insn->visited)
3762 return true;
3763
3764 if (insn->offset >= end)
3765 break;
3766
3767 /*
3768 * If this hole jumps to a .cold function, mark it ignore too.
3769 */
3770 if (insn->jump_dest && insn->jump_dest->func &&
3771 strstr(insn->jump_dest->func->name, ".cold")) {
3772 struct instruction *dest = insn->jump_dest;
3773 func_for_each_insn(file, dest->func, dest)
3774 dest->ignore = true;
3775 }
3776 }
3777
3778 return false;
3779 }
3780
bd841d61
JP
3781 if (!insn->func)
3782 return false;
3783
2105a927
PZ
3784 if (insn->func->static_call_tramp)
3785 return true;
3786
bd841d61
JP
3787 /*
3788 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
3789 * __builtin_unreachable(). The BUG() macro has an unreachable() after
3790 * the UD2, which causes GCC's undefined trap logic to emit another UD2
3791 * (or occasionally a JMP to UD2).
14db1f0a
IH
3792 *
3793 * It may also insert a UD2 after calling a __noreturn function.
bd841d61 3794 */
14db1f0a
IH
3795 prev_insn = list_prev_entry(insn, list);
3796 if ((prev_insn->dead_end || dead_end_function(file, prev_insn->call_dest)) &&
bd841d61
JP
3797 (insn->type == INSN_BUG ||
3798 (insn->type == INSN_JUMP_UNCONDITIONAL &&
3799 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
3800 return true;
3801
dcc914f4
JP
3802 /*
3803 * Check if this (or a subsequent) instruction is related to
3804 * CONFIG_UBSAN or CONFIG_KASAN.
3805 *
3806 * End the search at 5 instructions to avoid going into the weeds.
3807 */
3808 for (i = 0; i < 5; i++) {
3809
3810 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
3811 return true;
3812
fe24e271
JP
3813 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
3814 if (insn->jump_dest &&
3815 insn->jump_dest->func == insn->func) {
3816 insn = insn->jump_dest;
3817 continue;
3818 }
3819
3820 break;
dcc914f4
JP
3821 }
3822
baa41469 3823 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
dcc914f4 3824 break;
fe24e271 3825
dcc914f4
JP
3826 insn = list_next_entry(insn, list);
3827 }
3828
3829 return false;
3830}
3831
4b5e2e7f
PZ
3832static int validate_symbol(struct objtool_file *file, struct section *sec,
3833 struct symbol *sym, struct insn_state *state)
dcc914f4 3834{
dcc914f4 3835 struct instruction *insn;
4b5e2e7f
PZ
3836 int ret;
3837
3838 if (!sym->len) {
3839 WARN("%s() is missing an ELF size annotation", sym->name);
3840 return 1;
3841 }
3842
3843 if (sym->pfunc != sym || sym->alias != sym)
3844 return 0;
3845
3846 insn = find_insn(file, sec, sym->offset);
3847 if (!insn || insn->ignore || insn->visited)
3848 return 0;
3849
3850 state->uaccess = sym->uaccess_safe;
3851
3852 ret = validate_branch(file, insn->func, insn, *state);
2daf7fab 3853 if (ret && opts.backtrace)
4b5e2e7f
PZ
3854 BT_FUNC("<=== (sym)", insn);
3855 return ret;
3856}
3857
3858static int validate_section(struct objtool_file *file, struct section *sec)
3859{
baa41469 3860 struct insn_state state;
4b5e2e7f
PZ
3861 struct symbol *func;
3862 int warnings = 0;
dcc914f4 3863
350994bf
PZ
3864 list_for_each_entry(func, &sec->symbol_list, list) {
3865 if (func->type != STT_FUNC)
3866 continue;
e10cd8fe 3867
753da417 3868 init_insn_state(file, &state, sec);
b735bd3e 3869 set_func_state(&state.cfi);
0699e551 3870
4b5e2e7f 3871 warnings += validate_symbol(file, sec, func, &state);
dcc914f4
JP
3872 }
3873
dcc914f4
JP
3874 return warnings;
3875}
3876
753da417 3877static int validate_noinstr_sections(struct objtool_file *file)
c4a33939
PZ
3878{
3879 struct section *sec;
932f8e98 3880 int warnings = 0;
c4a33939
PZ
3881
3882 sec = find_section_by_name(file->elf, ".noinstr.text");
0cc9ac8d
TG
3883 if (sec) {
3884 warnings += validate_section(file, sec);
3885 warnings += validate_unwind_hints(file, sec);
3886 }
c4a33939 3887
0cc9ac8d
TG
3888 sec = find_section_by_name(file->elf, ".entry.text");
3889 if (sec) {
3890 warnings += validate_section(file, sec);
3891 warnings += validate_unwind_hints(file, sec);
3892 }
932f8e98
PZ
3893
3894 return warnings;
c4a33939
PZ
3895}
3896
350994bf
PZ
3897static int validate_functions(struct objtool_file *file)
3898{
3899 struct section *sec;
3900 int warnings = 0;
3901
da837bd6
PZ
3902 for_each_sec(file, sec) {
3903 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
3904 continue;
3905
350994bf 3906 warnings += validate_section(file, sec);
da837bd6 3907 }
350994bf
PZ
3908
3909 return warnings;
3910}
3911
3c6f9f77 3912static void mark_endbr_used(struct instruction *insn)
08f87a93 3913{
3c6f9f77
JP
3914 if (!list_empty(&insn->call_node))
3915 list_del_init(&insn->call_node);
3916}
3917
3918static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
3919{
3920 struct instruction *dest;
08f87a93 3921 struct reloc *reloc;
3c6f9f77
JP
3922 unsigned long off;
3923 int warnings = 0;
08f87a93 3924
3c6f9f77
JP
3925 /*
3926 * Looking for function pointer load relocations. Ignore
3927 * direct/indirect branches:
3928 */
3929 switch (insn->type) {
3930 case INSN_CALL:
3931 case INSN_CALL_DYNAMIC:
3932 case INSN_JUMP_CONDITIONAL:
3933 case INSN_JUMP_UNCONDITIONAL:
3934 case INSN_JUMP_DYNAMIC:
3935 case INSN_JUMP_DYNAMIC_CONDITIONAL:
3936 case INSN_RETURN:
3937 case INSN_NOP:
3938 return 0;
3939 default:
3940 break;
3941 }
08f87a93 3942
3c6f9f77
JP
3943 for (reloc = insn_reloc(file, insn);
3944 reloc;
3945 reloc = find_reloc_by_dest_range(file->elf, insn->sec,
3946 reloc->offset + 1,
3947 (insn->offset + insn->len) - (reloc->offset + 1))) {
08f87a93 3948
3c6f9f77
JP
3949 /*
3950 * static_call_update() references the trampoline, which
3951 * doesn't have (or need) ENDBR. Skip warning in that case.
3952 */
3953 if (reloc->sym->static_call_tramp)
08f87a93
PZ
3954 continue;
3955
3c6f9f77
JP
3956 off = reloc->sym->offset;
3957 if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)
3958 off += arch_dest_reloc_offset(reloc->addend);
3959 else
3960 off += reloc->addend;
3961
3962 dest = find_insn(file, reloc->sym->sec, off);
3963 if (!dest)
08f87a93
PZ
3964 continue;
3965
3c6f9f77
JP
3966 if (dest->type == INSN_ENDBR) {
3967 mark_endbr_used(dest);
08f87a93 3968 continue;
3c6f9f77 3969 }
08f87a93 3970
3c6f9f77
JP
3971 if (dest->func && dest->func == insn->func) {
3972 /*
3973 * Anything from->to self is either _THIS_IP_ or
3974 * IRET-to-self.
3975 *
3976 * There is no sane way to annotate _THIS_IP_ since the
3977 * compiler treats the relocation as a constant and is
3978 * happy to fold in offsets, skewing any annotation we
3979 * do, leading to vast amounts of false-positives.
3980 *
3981 * There's also compiler generated _THIS_IP_ through
3982 * KCOV and such which we have no hope of annotating.
3983 *
3984 * As such, blanket accept self-references without
3985 * issue.
3986 */
08f87a93 3987 continue;
3c6f9f77 3988 }
08f87a93 3989
3c6f9f77 3990 if (dest->noendbr)
08f87a93
PZ
3991 continue;
3992
3c6f9f77
JP
3993 WARN_FUNC("relocation to !ENDBR: %s",
3994 insn->sec, insn->offset,
3995 offstr(dest->sec, dest->offset));
3996
3997 warnings++;
3998 }
3999
4000 return warnings;
4001}
4002
4003static int validate_ibt_data_reloc(struct objtool_file *file,
4004 struct reloc *reloc)
4005{
4006 struct instruction *dest;
4007
4008 dest = find_insn(file, reloc->sym->sec,
4009 reloc->sym->offset + reloc->addend);
4010 if (!dest)
4011 return 0;
4012
4013 if (dest->type == INSN_ENDBR) {
4014 mark_endbr_used(dest);
4015 return 0;
4016 }
4017
4018 if (dest->noendbr)
4019 return 0;
4020
4021 WARN_FUNC("data relocation to !ENDBR: %s",
4022 reloc->sec->base, reloc->offset,
4023 offstr(dest->sec, dest->offset));
4024
4025 return 1;
4026}
4027
4028/*
4029 * Validate IBT rules and remove used ENDBR instructions from the seal list.
4030 * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
4031 * NOPs) later, in create_ibt_endbr_seal_sections().
4032 */
4033static int validate_ibt(struct objtool_file *file)
4034{
4035 struct section *sec;
4036 struct reloc *reloc;
4037 struct instruction *insn;
4038 int warnings = 0;
4039
4040 for_each_insn(file, insn)
4041 warnings += validate_ibt_insn(file, insn);
4042
4043 for_each_sec(file, sec) {
4044
4045 /* Already done by validate_ibt_insn() */
4046 if (sec->sh.sh_flags & SHF_EXECINSTR)
08f87a93
PZ
4047 continue;
4048
3c6f9f77
JP
4049 if (!sec->reloc)
4050 continue;
08f87a93 4051
3c6f9f77
JP
4052 /*
4053 * These sections can reference text addresses, but not with
4054 * the intent to indirect branch to them.
4055 */
4056 if (!strncmp(sec->name, ".discard", 8) ||
4057 !strncmp(sec->name, ".debug", 6) ||
4058 !strcmp(sec->name, ".altinstructions") ||
4059 !strcmp(sec->name, ".ibt_endbr_seal") ||
4060 !strcmp(sec->name, ".orc_unwind_ip") ||
4061 !strcmp(sec->name, ".parainstructions") ||
4062 !strcmp(sec->name, ".retpoline_sites") ||
4063 !strcmp(sec->name, ".smp_locks") ||
4064 !strcmp(sec->name, ".static_call_sites") ||
4065 !strcmp(sec->name, "_error_injection_whitelist") ||
4066 !strcmp(sec->name, "_kprobe_blacklist") ||
4067 !strcmp(sec->name, "__bug_table") ||
4068 !strcmp(sec->name, "__ex_table") ||
4069 !strcmp(sec->name, "__jump_table") ||
4070 !strcmp(sec->name, "__mcount_loc") ||
4071 !strcmp(sec->name, "__tracepoints"))
4072 continue;
08f87a93 4073
3c6f9f77
JP
4074 list_for_each_entry(reloc, &sec->reloc->reloc_list, list)
4075 warnings += validate_ibt_data_reloc(file, reloc);
08f87a93
PZ
4076 }
4077
3c6f9f77 4078 return warnings;
08f87a93
PZ
4079}
4080
c2bdd61c
JP
4081static int validate_sls(struct objtool_file *file)
4082{
4083 struct instruction *insn, *next_insn;
4084 int warnings = 0;
4085
4086 for_each_insn(file, insn) {
4087 next_insn = next_insn_same_sec(file, insn);
4088
4089 if (insn->retpoline_safe)
4090 continue;
4091
4092 switch (insn->type) {
4093 case INSN_RETURN:
4094 if (!next_insn || next_insn->type != INSN_TRAP) {
4095 WARN_FUNC("missing int3 after ret",
4096 insn->sec, insn->offset);
4097 warnings++;
4098 }
4099
4100 break;
4101 case INSN_JUMP_DYNAMIC:
4102 if (!next_insn || next_insn->type != INSN_TRAP) {
4103 WARN_FUNC("missing int3 after indirect jump",
4104 insn->sec, insn->offset);
4105 warnings++;
4106 }
4107 break;
4108 default:
4109 break;
4110 }
4111 }
4112
4113 return warnings;
4114}
4115
baa41469 4116static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4
JP
4117{
4118 struct instruction *insn;
baa41469
JP
4119
4120 if (file->ignore_unreachables)
4121 return 0;
dcc914f4
JP
4122
4123 for_each_insn(file, insn) {
14db1f0a 4124 if (insn->visited || ignore_unreachable_insn(file, insn))
baa41469
JP
4125 continue;
4126
baa41469
JP
4127 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
4128 return 1;
dcc914f4
JP
4129 }
4130
baa41469 4131 return 0;
dcc914f4
JP
4132}
4133
d44becb9 4134int check(struct objtool_file *file)
dcc914f4 4135{
dcc914f4
JP
4136 int ret, warnings = 0;
4137
baa41469 4138 arch_initial_func_cfi_state(&initial_func_cfi);
8b946cc3
PZ
4139 init_cfi_state(&init_cfi);
4140 init_cfi_state(&func_cfi);
4141 set_func_state(&func_cfi);
4142
4143 if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
4144 goto out;
4145
4146 cfi_hash_add(&init_cfi);
4147 cfi_hash_add(&func_cfi);
baa41469 4148
6545eb03 4149 ret = decode_sections(file);
dcc914f4
JP
4150 if (ret < 0)
4151 goto out;
8b946cc3 4152
dcc914f4
JP
4153 warnings += ret;
4154
6545eb03 4155 if (list_empty(&file->insn_list))
dcc914f4 4156 goto out;
dcc914f4 4157
2daf7fab 4158 if (opts.retpoline) {
6545eb03 4159 ret = validate_retpoline(file);
b5bc2231
PZ
4160 if (ret < 0)
4161 return ret;
4162 warnings += ret;
4163 }
4164
c2bdd61c 4165 if (opts.stackval || opts.orc || opts.uaccess) {
7dce6204
JP
4166 ret = validate_functions(file);
4167 if (ret < 0)
4168 goto out;
4169 warnings += ret;
39358a03 4170
7dce6204 4171 ret = validate_unwind_hints(file, NULL);
08f87a93
PZ
4172 if (ret < 0)
4173 goto out;
4174 warnings += ret;
7dce6204
JP
4175
4176 if (!warnings) {
4177 ret = validate_reachable_instructions(file);
4178 if (ret < 0)
4179 goto out;
4180 warnings += ret;
4181 }
753da417
JP
4182
4183 } else if (opts.noinstr) {
4184 ret = validate_noinstr_sections(file);
4185 if (ret < 0)
4186 goto out;
4187 warnings += ret;
08f87a93
PZ
4188 }
4189
a09a6e23
PZ
4190 if (opts.unret) {
4191 /*
4192 * Must be after validate_branch() and friends, it plays
4193 * further games with insn->visited.
4194 */
4195 ret = validate_unret(file);
4196 if (ret < 0)
4197 return ret;
4198 warnings += ret;
4199 }
4200
7dce6204
JP
4201 if (opts.ibt) {
4202 ret = validate_ibt(file);
baa41469
JP
4203 if (ret < 0)
4204 goto out;
c2bdd61c
JP
4205 warnings += ret;
4206 }
4207
4208 if (opts.sls) {
4209 ret = validate_sls(file);
4210 if (ret < 0)
4211 goto out;
baa41469
JP
4212 warnings += ret;
4213 }
4214
26e17689
JP
4215 if (opts.static_call) {
4216 ret = create_static_call_sections(file);
4217 if (ret < 0)
4218 goto out;
4219 warnings += ret;
4220 }
1e7e4788 4221
2daf7fab 4222 if (opts.retpoline) {
134ab5bd
PZ
4223 ret = create_retpoline_sites_sections(file);
4224 if (ret < 0)
4225 goto out;
4226 warnings += ret;
d9e9d230
PZ
4227
4228 ret = create_return_sites_sections(file);
4229 if (ret < 0)
4230 goto out;
4231 warnings += ret;
134ab5bd
PZ
4232 }
4233
2daf7fab 4234 if (opts.mcount) {
99d00215
PZ
4235 ret = create_mcount_loc_sections(file);
4236 if (ret < 0)
4237 goto out;
4238 warnings += ret;
4239 }
4240
2daf7fab 4241 if (opts.ibt) {
89bc853e
PZ
4242 ret = create_ibt_endbr_seal_sections(file);
4243 if (ret < 0)
4244 goto out;
4245 warnings += ret;
4246 }
4247
b51277eb
JP
4248 if (opts.orc && !list_empty(&file->insn_list)) {
4249 ret = orc_create(file);
4250 if (ret < 0)
4251 goto out;
4252 warnings += ret;
4253 }
4254
4255
2daf7fab 4256 if (opts.stats) {
8b946cc3
PZ
4257 printf("nr_insns_visited: %ld\n", nr_insns_visited);
4258 printf("nr_cfi: %ld\n", nr_cfi);
4259 printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
4260 printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
4261 }
4262
dcc914f4 4263out:
655cf865
JP
4264 /*
4265 * For now, don't fail the kernel build on fatal warnings. These
4266 * errors are still fairly common due to the growing matrix of
4267 * supported toolchains and their recent pace of change.
4268 */
dcc914f4
JP
4269 return 0;
4270}