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