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