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