]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/objtool/check.c
objtool: Add relocation check for alternative sections
[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
43a4525f 9#include "builtin.h"
dcc914f4
JP
10#include "check.h"
11#include "elf.h"
12#include "special.h"
13#include "arch.h"
14#include "warn.h"
15
16#include <linux/hashtable.h>
17#include <linux/kernel.h>
18
e6da9567
JP
19#define FAKE_JUMP_OFFSET -1
20
87b512de
JP
21#define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
22
dcc914f4
JP
23struct alternative {
24 struct list_head list;
25 struct instruction *insn;
764eef4b 26 bool skip_orig;
dcc914f4
JP
27};
28
29const char *objname;
baa41469 30struct cfi_state initial_func_cfi;
dcc914f4 31
627fce14
JP
32struct instruction *find_insn(struct objtool_file *file,
33 struct section *sec, unsigned long offset)
dcc914f4
JP
34{
35 struct instruction *insn;
36
37 hash_for_each_possible(file->insn_hash, insn, hash, offset)
38 if (insn->sec == sec && insn->offset == offset)
39 return insn;
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
75#define func_for_each_insn_all(file, func, insn) \
76 for (insn = find_insn(file, func->sec, func->offset); \
77 insn; \
78 insn = next_insn_same_func(file, insn))
79
dcc914f4
JP
80#define func_for_each_insn(file, func, insn) \
81 for (insn = find_insn(file, func->sec, func->offset); \
82 insn && &insn->list != &file->insn_list && \
83 insn->sec == func->sec && \
84 insn->offset < func->offset + func->len; \
85 insn = list_next_entry(insn, list))
86
87#define func_for_each_insn_continue_reverse(file, func, insn) \
88 for (insn = list_prev_entry(insn, list); \
89 &insn->list != &file->insn_list && \
90 insn->sec == func->sec && insn->offset >= func->offset; \
91 insn = list_prev_entry(insn, list))
92
93#define sec_for_each_insn_from(file, insn) \
94 for (; insn; insn = next_insn_same_sec(file, insn))
95
baa41469
JP
96#define sec_for_each_insn_continue(file, insn) \
97 for (insn = next_insn_same_sec(file, insn); insn; \
98 insn = next_insn_same_sec(file, insn))
dcc914f4 99
a2296140
JP
100static bool is_static_jump(struct instruction *insn)
101{
102 return insn->type == INSN_JUMP_CONDITIONAL ||
103 insn->type == INSN_JUMP_UNCONDITIONAL;
104}
105
0c1ddd33
JP
106static bool is_sibling_call(struct instruction *insn)
107{
108 /* An indirect jump is either a sibling call or a jump to a table. */
109 if (insn->type == INSN_JUMP_DYNAMIC)
110 return list_empty(&insn->alts);
111
a2296140 112 if (!is_static_jump(insn))
0c1ddd33
JP
113 return false;
114
115 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
116 return !!insn->call_dest;
117}
118
dcc914f4
JP
119/*
120 * This checks to see if the given function is a "noreturn" function.
121 *
122 * For global functions which are outside the scope of this object file, we
123 * have to keep a manual list of them.
124 *
125 * For local functions, we have to detect them manually by simply looking for
126 * the lack of a return instruction.
dcc914f4 127 */
8e25c9f8
JP
128static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
129 int recursion)
dcc914f4
JP
130{
131 int i;
132 struct instruction *insn;
133 bool empty = true;
134
135 /*
136 * Unfortunately these have to be hard coded because the noreturn
137 * attribute isn't provided in ELF data.
138 */
139 static const char * const global_noreturns[] = {
140 "__stack_chk_fail",
141 "panic",
142 "do_exit",
143 "do_task_dead",
144 "__module_put_and_exit",
145 "complete_and_exit",
dcc914f4
JP
146 "__reiserfs_panic",
147 "lbug_with_loc",
148 "fortify_panic",
b394d468 149 "usercopy_abort",
684fb246 150 "machine_real_restart",
4fa5ecda 151 "rewind_stack_do_exit",
33adf80f 152 "kunit_try_catch_throw",
dcc914f4
JP
153 };
154
c9bab22b
JP
155 if (!func)
156 return false;
157
dcc914f4 158 if (func->bind == STB_WEAK)
8e25c9f8 159 return false;
dcc914f4
JP
160
161 if (func->bind == STB_GLOBAL)
162 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
163 if (!strcmp(func->name, global_noreturns[i]))
8e25c9f8 164 return true;
dcc914f4 165
13810435 166 if (!func->len)
8e25c9f8 167 return false;
dcc914f4 168
13810435
JP
169 insn = find_insn(file, func->sec, func->offset);
170 if (!insn->func)
8e25c9f8 171 return false;
13810435
JP
172
173 func_for_each_insn_all(file, func, insn) {
dcc914f4
JP
174 empty = false;
175
176 if (insn->type == INSN_RETURN)
8e25c9f8 177 return false;
dcc914f4
JP
178 }
179
180 if (empty)
8e25c9f8 181 return false;
dcc914f4
JP
182
183 /*
184 * A function can have a sibling call instead of a return. In that
185 * case, the function's dead-end status depends on whether the target
186 * of the sibling call returns.
187 */
13810435 188 func_for_each_insn_all(file, func, insn) {
0c1ddd33 189 if (is_sibling_call(insn)) {
dcc914f4 190 struct instruction *dest = insn->jump_dest;
dcc914f4
JP
191
192 if (!dest)
193 /* sibling call to another file */
8e25c9f8 194 return false;
dcc914f4 195
0c1ddd33
JP
196 /* local sibling call */
197 if (recursion == 5) {
198 /*
199 * Infinite recursion: two functions have
200 * sibling calls to each other. This is a very
201 * rare case. It means they aren't dead ends.
202 */
203 return false;
dcc914f4 204 }
dcc914f4 205
0c1ddd33
JP
206 return __dead_end_function(file, dest->func, recursion+1);
207 }
dcc914f4
JP
208 }
209
8e25c9f8 210 return true;
dcc914f4
JP
211}
212
8e25c9f8 213static bool dead_end_function(struct objtool_file *file, struct symbol *func)
dcc914f4
JP
214{
215 return __dead_end_function(file, func, 0);
216}
217
baa41469
JP
218static void clear_insn_state(struct insn_state *state)
219{
220 int i;
221
222 memset(state, 0, sizeof(*state));
223 state->cfa.base = CFI_UNDEFINED;
dd88a0a0 224 for (i = 0; i < CFI_NUM_REGS; i++) {
baa41469 225 state->regs[i].base = CFI_UNDEFINED;
dd88a0a0
JP
226 state->vals[i].base = CFI_UNDEFINED;
227 }
baa41469 228 state->drap_reg = CFI_UNDEFINED;
bf4d1a83 229 state->drap_offset = -1;
baa41469
JP
230}
231
dcc914f4
JP
232/*
233 * Call the arch-specific instruction decoder for all the instructions and add
234 * them to the global instruction list.
235 */
236static int decode_instructions(struct objtool_file *file)
237{
238 struct section *sec;
239 struct symbol *func;
240 unsigned long offset;
241 struct instruction *insn;
242 int ret;
243
baa41469 244 for_each_sec(file, sec) {
dcc914f4
JP
245
246 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
247 continue;
248
627fce14
JP
249 if (strcmp(sec->name, ".altinstr_replacement") &&
250 strcmp(sec->name, ".altinstr_aux") &&
251 strncmp(sec->name, ".discard.", 9))
252 sec->text = true;
253
dcc914f4
JP
254 for (offset = 0; offset < sec->len; offset += insn->len) {
255 insn = malloc(sizeof(*insn));
baa41469
JP
256 if (!insn) {
257 WARN("malloc failed");
258 return -1;
259 }
dcc914f4 260 memset(insn, 0, sizeof(*insn));
dcc914f4 261 INIT_LIST_HEAD(&insn->alts);
baa41469
JP
262 clear_insn_state(&insn->state);
263
dcc914f4
JP
264 insn->sec = sec;
265 insn->offset = offset;
266
267 ret = arch_decode_instruction(file->elf, sec, offset,
268 sec->len - offset,
269 &insn->len, &insn->type,
baa41469
JP
270 &insn->immediate,
271 &insn->stack_op);
dcc914f4 272 if (ret)
b7037983 273 goto err;
dcc914f4 274
dcc914f4
JP
275 hash_add(file->insn_hash, &insn->hash, insn->offset);
276 list_add_tail(&insn->list, &file->insn_list);
277 }
278
279 list_for_each_entry(func, &sec->symbol_list, list) {
e10cd8fe 280 if (func->type != STT_FUNC || func->alias != func)
dcc914f4
JP
281 continue;
282
283 if (!find_insn(file, sec, func->offset)) {
284 WARN("%s(): can't find starting instruction",
285 func->name);
286 return -1;
287 }
288
289 func_for_each_insn(file, func, insn)
e10cd8fe 290 insn->func = func;
dcc914f4
JP
291 }
292 }
293
294 return 0;
b7037983
KB
295
296err:
297 free(insn);
298 return ret;
dcc914f4
JP
299}
300
301/*
649ea4d5 302 * Mark "ud2" instructions and manually annotated dead ends.
dcc914f4
JP
303 */
304static int add_dead_ends(struct objtool_file *file)
305{
306 struct section *sec;
307 struct rela *rela;
308 struct instruction *insn;
309 bool found;
310
649ea4d5
JP
311 /*
312 * By default, "ud2" is a dead end unless otherwise annotated, because
313 * GCC 7 inserts it for certain divide-by-zero cases.
314 */
315 for_each_insn(file, insn)
316 if (insn->type == INSN_BUG)
317 insn->dead_end = true;
318
319 /*
320 * Check for manually annotated dead ends.
321 */
dcc914f4
JP
322 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
323 if (!sec)
649ea4d5 324 goto reachable;
dcc914f4
JP
325
326 list_for_each_entry(rela, &sec->rela_list, list) {
327 if (rela->sym->type != STT_SECTION) {
328 WARN("unexpected relocation symbol type in %s", sec->name);
329 return -1;
330 }
331 insn = find_insn(file, rela->sym->sec, rela->addend);
332 if (insn)
333 insn = list_prev_entry(insn, list);
334 else if (rela->addend == rela->sym->sec->len) {
335 found = false;
336 list_for_each_entry_reverse(insn, &file->insn_list, list) {
337 if (insn->sec == rela->sym->sec) {
338 found = true;
339 break;
340 }
341 }
342
343 if (!found) {
344 WARN("can't find unreachable insn at %s+0x%x",
345 rela->sym->sec->name, rela->addend);
346 return -1;
347 }
348 } else {
349 WARN("can't find unreachable insn at %s+0x%x",
350 rela->sym->sec->name, rela->addend);
351 return -1;
352 }
353
354 insn->dead_end = true;
355 }
356
649ea4d5
JP
357reachable:
358 /*
359 * These manually annotated reachable checks are needed for GCC 4.4,
360 * where the Linux unreachable() macro isn't supported. In that case
361 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
362 * not a dead end.
363 */
364 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
365 if (!sec)
366 return 0;
367
368 list_for_each_entry(rela, &sec->rela_list, list) {
369 if (rela->sym->type != STT_SECTION) {
370 WARN("unexpected relocation symbol type in %s", sec->name);
371 return -1;
372 }
373 insn = find_insn(file, rela->sym->sec, rela->addend);
374 if (insn)
375 insn = list_prev_entry(insn, list);
376 else if (rela->addend == rela->sym->sec->len) {
377 found = false;
378 list_for_each_entry_reverse(insn, &file->insn_list, list) {
379 if (insn->sec == rela->sym->sec) {
380 found = true;
381 break;
382 }
383 }
384
385 if (!found) {
386 WARN("can't find reachable insn at %s+0x%x",
387 rela->sym->sec->name, rela->addend);
388 return -1;
389 }
390 } else {
391 WARN("can't find reachable insn at %s+0x%x",
392 rela->sym->sec->name, rela->addend);
393 return -1;
394 }
395
396 insn->dead_end = false;
397 }
398
dcc914f4
JP
399 return 0;
400}
401
402/*
403 * Warnings shouldn't be reported for ignored functions.
404 */
405static void add_ignores(struct objtool_file *file)
406{
407 struct instruction *insn;
408 struct section *sec;
409 struct symbol *func;
aaf5c623 410 struct rela *rela;
dcc914f4 411
aaf5c623
PZ
412 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
413 if (!sec)
414 return;
dcc914f4 415
aaf5c623
PZ
416 list_for_each_entry(rela, &sec->rela_list, list) {
417 switch (rela->sym->type) {
418 case STT_FUNC:
419 func = rela->sym;
420 break;
421
422 case STT_SECTION:
423 func = find_symbol_by_offset(rela->sym->sec, rela->addend);
424 if (!func || func->type != STT_FUNC)
dcc914f4 425 continue;
aaf5c623 426 break;
dcc914f4 427
aaf5c623
PZ
428 default:
429 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
430 continue;
dcc914f4 431 }
aaf5c623
PZ
432
433 func_for_each_insn_all(file, func, insn)
434 insn->ignore = true;
dcc914f4
JP
435 }
436}
437
ea24213d
PZ
438/*
439 * This is a whitelist of functions that is allowed to be called with AC set.
440 * The list is meant to be minimal and only contains compiler instrumentation
441 * ABI and a few functions used to implement *_{to,from}_user() functions.
442 *
443 * These functions must not directly change AC, but may PUSHF/POPF.
444 */
445static const char *uaccess_safe_builtin[] = {
446 /* KASAN */
447 "kasan_report",
448 "check_memory_region",
449 /* KASAN out-of-line */
450 "__asan_loadN_noabort",
451 "__asan_load1_noabort",
452 "__asan_load2_noabort",
453 "__asan_load4_noabort",
454 "__asan_load8_noabort",
455 "__asan_load16_noabort",
456 "__asan_storeN_noabort",
457 "__asan_store1_noabort",
458 "__asan_store2_noabort",
459 "__asan_store4_noabort",
460 "__asan_store8_noabort",
461 "__asan_store16_noabort",
462 /* KASAN in-line */
463 "__asan_report_load_n_noabort",
464 "__asan_report_load1_noabort",
465 "__asan_report_load2_noabort",
466 "__asan_report_load4_noabort",
467 "__asan_report_load8_noabort",
468 "__asan_report_load16_noabort",
469 "__asan_report_store_n_noabort",
470 "__asan_report_store1_noabort",
471 "__asan_report_store2_noabort",
472 "__asan_report_store4_noabort",
473 "__asan_report_store8_noabort",
474 "__asan_report_store16_noabort",
475 /* KCOV */
476 "write_comp_data",
477 "__sanitizer_cov_trace_pc",
478 "__sanitizer_cov_trace_const_cmp1",
479 "__sanitizer_cov_trace_const_cmp2",
480 "__sanitizer_cov_trace_const_cmp4",
481 "__sanitizer_cov_trace_const_cmp8",
482 "__sanitizer_cov_trace_cmp1",
483 "__sanitizer_cov_trace_cmp2",
484 "__sanitizer_cov_trace_cmp4",
485 "__sanitizer_cov_trace_cmp8",
486 /* UBSAN */
487 "ubsan_type_mismatch_common",
488 "__ubsan_handle_type_mismatch",
489 "__ubsan_handle_type_mismatch_v1",
9a50dcaf 490 "__ubsan_handle_shift_out_of_bounds",
ea24213d
PZ
491 /* misc */
492 "csum_partial_copy_generic",
493 "__memcpy_mcsafe",
a7e47f26 494 "mcsafe_handle_tail",
ea24213d
PZ
495 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
496 NULL
497};
498
499static void add_uaccess_safe(struct objtool_file *file)
500{
501 struct symbol *func;
502 const char **name;
503
504 if (!uaccess)
505 return;
506
507 for (name = uaccess_safe_builtin; *name; name++) {
508 func = find_symbol_by_name(file->elf, *name);
509 if (!func)
510 continue;
511
e10cd8fe 512 func->uaccess_safe = true;
dcc914f4
JP
513 }
514}
515
258c7605
JP
516/*
517 * FIXME: For now, just ignore any alternatives which add retpolines. This is
518 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
519 * But it at least allows objtool to understand the control flow *around* the
520 * retpoline.
521 */
ff05ab23 522static int add_ignore_alternatives(struct objtool_file *file)
258c7605
JP
523{
524 struct section *sec;
525 struct rela *rela;
526 struct instruction *insn;
527
ff05ab23 528 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
258c7605
JP
529 if (!sec)
530 return 0;
531
532 list_for_each_entry(rela, &sec->rela_list, list) {
533 if (rela->sym->type != STT_SECTION) {
534 WARN("unexpected relocation symbol type in %s", sec->name);
535 return -1;
536 }
537
538 insn = find_insn(file, rela->sym->sec, rela->addend);
539 if (!insn) {
ff05ab23 540 WARN("bad .discard.ignore_alts entry");
258c7605
JP
541 return -1;
542 }
543
544 insn->ignore_alts = true;
545 }
546
547 return 0;
548}
549
dcc914f4
JP
550/*
551 * Find the destination instructions for all jumps.
552 */
553static int add_jump_destinations(struct objtool_file *file)
554{
555 struct instruction *insn;
556 struct rela *rela;
557 struct section *dest_sec;
558 unsigned long dest_off;
559
560 for_each_insn(file, insn) {
a2296140 561 if (!is_static_jump(insn))
dcc914f4
JP
562 continue;
563
e6da9567 564 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
dcc914f4
JP
565 continue;
566
567 rela = find_rela_by_dest_range(insn->sec, insn->offset,
568 insn->len);
569 if (!rela) {
570 dest_sec = insn->sec;
571 dest_off = insn->offset + insn->len + insn->immediate;
572 } else if (rela->sym->type == STT_SECTION) {
573 dest_sec = rela->sym->sec;
574 dest_off = rela->addend + 4;
575 } else if (rela->sym->sec->idx) {
576 dest_sec = rela->sym->sec;
577 dest_off = rela->sym->sym.st_value + rela->addend + 4;
39b73533
JP
578 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
579 /*
580 * Retpoline jumps are really dynamic jumps in
581 * disguise, so convert them accordingly.
582 */
b68b9907
JP
583 if (insn->type == INSN_JUMP_UNCONDITIONAL)
584 insn->type = INSN_JUMP_DYNAMIC;
585 else
586 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
587
b5bc2231 588 insn->retpoline_safe = true;
39b73533 589 continue;
dcc914f4 590 } else {
0c1ddd33 591 /* external sibling call */
54262aa2 592 insn->call_dest = rela->sym;
dcc914f4
JP
593 continue;
594 }
595
596 insn->jump_dest = find_insn(file, dest_sec, dest_off);
597 if (!insn->jump_dest) {
598
599 /*
600 * This is a special case where an alt instruction
601 * jumps past the end of the section. These are
602 * handled later in handle_group_alt().
603 */
604 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
605 continue;
606
607 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
608 insn->sec, insn->offset, dest_sec->name,
609 dest_off);
610 return -1;
611 }
cd77849a
JP
612
613 /*
54262aa2 614 * Cross-function jump.
cd77849a
JP
615 */
616 if (insn->func && insn->jump_dest->func &&
54262aa2
PZ
617 insn->func != insn->jump_dest->func) {
618
619 /*
620 * For GCC 8+, create parent/child links for any cold
621 * subfunctions. This is _mostly_ redundant with a
622 * similar initialization in read_symbols().
623 *
624 * If a function has aliases, we want the *first* such
625 * function in the symbol table to be the subfunction's
626 * parent. In that case we overwrite the
627 * initialization done in read_symbols().
628 *
629 * However this code can't completely replace the
630 * read_symbols() code because this doesn't detect the
631 * case where the parent function's only reference to a
e7c2bc37 632 * subfunction is through a jump table.
54262aa2
PZ
633 */
634 if (!strstr(insn->func->name, ".cold.") &&
635 strstr(insn->jump_dest->func->name, ".cold.")) {
636 insn->func->cfunc = insn->jump_dest->func;
637 insn->jump_dest->func->pfunc = insn->func;
638
639 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
640 insn->jump_dest->offset == insn->jump_dest->func->offset) {
641
0c1ddd33 642 /* internal sibling call */
54262aa2 643 insn->call_dest = insn->jump_dest->func;
54262aa2 644 }
cd77849a 645 }
dcc914f4
JP
646 }
647
648 return 0;
649}
650
651/*
652 * Find the destination instructions for all calls.
653 */
654static int add_call_destinations(struct objtool_file *file)
655{
656 struct instruction *insn;
657 unsigned long dest_off;
658 struct rela *rela;
659
660 for_each_insn(file, insn) {
661 if (insn->type != INSN_CALL)
662 continue;
663
664 rela = find_rela_by_dest_range(insn->sec, insn->offset,
665 insn->len);
666 if (!rela) {
667 dest_off = insn->offset + insn->len + insn->immediate;
668 insn->call_dest = find_symbol_by_offset(insn->sec,
669 dest_off);
a845c7cf
JP
670
671 if (!insn->call_dest && !insn->ignore) {
672 WARN_FUNC("unsupported intra-function call",
673 insn->sec, insn->offset);
b5bc2231
PZ
674 if (retpoline)
675 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
dcc914f4
JP
676 return -1;
677 }
a845c7cf 678
dcc914f4
JP
679 } else if (rela->sym->type == STT_SECTION) {
680 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
681 rela->addend+4);
682 if (!insn->call_dest ||
683 insn->call_dest->type != STT_FUNC) {
684 WARN_FUNC("can't find call dest symbol at %s+0x%x",
685 insn->sec, insn->offset,
686 rela->sym->sec->name,
687 rela->addend + 4);
688 return -1;
689 }
690 } else
691 insn->call_dest = rela->sym;
692 }
693
694 return 0;
695}
696
697/*
698 * The .alternatives section requires some extra special care, over and above
699 * what other special sections require:
700 *
701 * 1. Because alternatives are patched in-place, we need to insert a fake jump
702 * instruction at the end so that validate_branch() skips all the original
703 * replaced instructions when validating the new instruction path.
704 *
705 * 2. An added wrinkle is that the new instruction length might be zero. In
706 * that case the old instructions are replaced with noops. We simulate that
707 * by creating a fake jump as the only new instruction.
708 *
709 * 3. In some cases, the alternative section includes an instruction which
710 * conditionally jumps to the _end_ of the entry. We have to modify these
711 * jumps' destinations to point back to .text rather than the end of the
712 * entry in .altinstr_replacement.
dcc914f4
JP
713 */
714static int handle_group_alt(struct objtool_file *file,
715 struct special_alt *special_alt,
716 struct instruction *orig_insn,
717 struct instruction **new_insn)
718{
17bc3391 719 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
dcc914f4
JP
720 unsigned long dest_off;
721
722 last_orig_insn = NULL;
723 insn = orig_insn;
724 sec_for_each_insn_from(file, insn) {
725 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
726 break;
727
dcc914f4
JP
728 insn->alt_group = true;
729 last_orig_insn = insn;
730 }
731
17bc3391
JP
732 if (next_insn_same_sec(file, last_orig_insn)) {
733 fake_jump = malloc(sizeof(*fake_jump));
734 if (!fake_jump) {
735 WARN("malloc failed");
736 return -1;
737 }
738 memset(fake_jump, 0, sizeof(*fake_jump));
739 INIT_LIST_HEAD(&fake_jump->alts);
740 clear_insn_state(&fake_jump->state);
741
742 fake_jump->sec = special_alt->new_sec;
e6da9567 743 fake_jump->offset = FAKE_JUMP_OFFSET;
17bc3391
JP
744 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
745 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
e6da9567 746 fake_jump->func = orig_insn->func;
dcc914f4 747 }
dcc914f4
JP
748
749 if (!special_alt->new_len) {
17bc3391
JP
750 if (!fake_jump) {
751 WARN("%s: empty alternative at end of section",
752 special_alt->orig_sec->name);
753 return -1;
754 }
755
dcc914f4
JP
756 *new_insn = fake_jump;
757 return 0;
758 }
759
760 last_new_insn = NULL;
761 insn = *new_insn;
762 sec_for_each_insn_from(file, insn) {
763 if (insn->offset >= special_alt->new_off + special_alt->new_len)
764 break;
765
766 last_new_insn = insn;
767
a845c7cf 768 insn->ignore = orig_insn->ignore_alts;
a4d09dde 769 insn->func = orig_insn->func;
a845c7cf 770
dc419723
JP
771 /*
772 * Since alternative replacement code is copy/pasted by the
773 * kernel after applying relocations, generally such code can't
774 * have relative-address relocation references to outside the
775 * .altinstr_replacement section, unless the arch's
776 * alternatives code can adjust the relative offsets
777 * accordingly.
778 *
779 * The x86 alternatives code adjusts the offsets only when it
780 * encounters a branch instruction at the very beginning of the
781 * replacement group.
782 */
783 if ((insn->offset != special_alt->new_off ||
784 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
785 find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
786
787 WARN_FUNC("unsupported relocation in alternatives section",
788 insn->sec, insn->offset);
789 return -1;
790 }
791
a2296140 792 if (!is_static_jump(insn))
dcc914f4
JP
793 continue;
794
795 if (!insn->immediate)
796 continue;
797
798 dest_off = insn->offset + insn->len + insn->immediate;
17bc3391
JP
799 if (dest_off == special_alt->new_off + special_alt->new_len) {
800 if (!fake_jump) {
801 WARN("%s: alternative jump to end of section",
802 special_alt->orig_sec->name);
803 return -1;
804 }
dcc914f4 805 insn->jump_dest = fake_jump;
17bc3391 806 }
dcc914f4
JP
807
808 if (!insn->jump_dest) {
809 WARN_FUNC("can't find alternative jump destination",
810 insn->sec, insn->offset);
811 return -1;
812 }
813 }
814
815 if (!last_new_insn) {
816 WARN_FUNC("can't find last new alternative instruction",
817 special_alt->new_sec, special_alt->new_off);
818 return -1;
819 }
820
17bc3391
JP
821 if (fake_jump)
822 list_add(&fake_jump->list, &last_new_insn->list);
dcc914f4
JP
823
824 return 0;
825}
826
827/*
828 * A jump table entry can either convert a nop to a jump or a jump to a nop.
829 * If the original instruction is a jump, make the alt entry an effective nop
830 * by just skipping the original instruction.
831 */
832static int handle_jump_alt(struct objtool_file *file,
833 struct special_alt *special_alt,
834 struct instruction *orig_insn,
835 struct instruction **new_insn)
836{
837 if (orig_insn->type == INSN_NOP)
838 return 0;
839
840 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
841 WARN_FUNC("unsupported instruction at jump label",
842 orig_insn->sec, orig_insn->offset);
843 return -1;
844 }
845
846 *new_insn = list_next_entry(orig_insn, list);
847 return 0;
848}
849
850/*
851 * Read all the special sections which have alternate instructions which can be
852 * patched in or redirected to at runtime. Each instruction having alternate
853 * instruction(s) has them added to its insn->alts list, which will be
854 * traversed in validate_branch().
855 */
856static int add_special_section_alts(struct objtool_file *file)
857{
858 struct list_head special_alts;
859 struct instruction *orig_insn, *new_insn;
860 struct special_alt *special_alt, *tmp;
861 struct alternative *alt;
862 int ret;
863
864 ret = special_get_alts(file->elf, &special_alts);
865 if (ret)
866 return ret;
867
868 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
dcc914f4
JP
869
870 orig_insn = find_insn(file, special_alt->orig_sec,
871 special_alt->orig_off);
872 if (!orig_insn) {
873 WARN_FUNC("special: can't find orig instruction",
874 special_alt->orig_sec, special_alt->orig_off);
875 ret = -1;
876 goto out;
877 }
878
879 new_insn = NULL;
880 if (!special_alt->group || special_alt->new_len) {
881 new_insn = find_insn(file, special_alt->new_sec,
882 special_alt->new_off);
883 if (!new_insn) {
884 WARN_FUNC("special: can't find new instruction",
885 special_alt->new_sec,
886 special_alt->new_off);
887 ret = -1;
888 goto out;
889 }
890 }
891
892 if (special_alt->group) {
893 ret = handle_group_alt(file, special_alt, orig_insn,
894 &new_insn);
895 if (ret)
896 goto out;
897 } else if (special_alt->jump_or_nop) {
898 ret = handle_jump_alt(file, special_alt, orig_insn,
899 &new_insn);
900 if (ret)
901 goto out;
902 }
903
258c7605
JP
904 alt = malloc(sizeof(*alt));
905 if (!alt) {
906 WARN("malloc failed");
907 ret = -1;
908 goto out;
909 }
910
dcc914f4 911 alt->insn = new_insn;
764eef4b 912 alt->skip_orig = special_alt->skip_orig;
ea24213d 913 orig_insn->ignore_alts |= special_alt->skip_alt;
dcc914f4
JP
914 list_add_tail(&alt->list, &orig_insn->alts);
915
916 list_del(&special_alt->list);
917 free(special_alt);
918 }
919
920out:
921 return ret;
922}
923
e7c2bc37 924static int add_jump_table(struct objtool_file *file, struct instruction *insn,
bd98c813 925 struct rela *table)
dcc914f4
JP
926{
927 struct rela *rela = table;
e7c2bc37 928 struct instruction *dest_insn;
dcc914f4 929 struct alternative *alt;
fd35c88b
JP
930 struct symbol *pfunc = insn->func->pfunc;
931 unsigned int prev_offset = 0;
dcc914f4 932
e7c2bc37
JP
933 /*
934 * Each @rela is a switch table relocation which points to the target
935 * instruction.
936 */
937 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
bd98c813
JH
938
939 /* Check for the end of the table: */
940 if (rela != table && rela->jump_table_start)
dcc914f4
JP
941 break;
942
e7c2bc37 943 /* Make sure the table entries are consecutive: */
fd35c88b
JP
944 if (prev_offset && rela->offset != prev_offset + 8)
945 break;
946
947 /* Detect function pointers from contiguous objects: */
948 if (rela->sym->sec == pfunc->sec &&
949 rela->addend == pfunc->offset)
950 break;
951
e7c2bc37
JP
952 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
953 if (!dest_insn)
dcc914f4
JP
954 break;
955
e7c2bc37 956 /* Make sure the destination is in the same function: */
e65050b9 957 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
13810435 958 break;
dcc914f4
JP
959
960 alt = malloc(sizeof(*alt));
961 if (!alt) {
962 WARN("malloc failed");
963 return -1;
964 }
965
e7c2bc37 966 alt->insn = dest_insn;
dcc914f4 967 list_add_tail(&alt->list, &insn->alts);
fd35c88b
JP
968 prev_offset = rela->offset;
969 }
970
971 if (!prev_offset) {
972 WARN_FUNC("can't find switch jump table",
973 insn->sec, insn->offset);
974 return -1;
dcc914f4
JP
975 }
976
977 return 0;
978}
979
980/*
e7c2bc37 981 * find_jump_table() - Given a dynamic jump, find the switch jump table in
dcc914f4
JP
982 * .rodata associated with it.
983 *
984 * There are 3 basic patterns:
985 *
986 * 1. jmpq *[rodata addr](,%reg,8)
987 *
988 * This is the most common case by far. It jumps to an address in a simple
989 * jump table which is stored in .rodata.
990 *
991 * 2. jmpq *[rodata addr](%rip)
992 *
993 * This is caused by a rare GCC quirk, currently only seen in three driver
994 * functions in the kernel, only with certain obscure non-distro configs.
995 *
996 * As part of an optimization, GCC makes a copy of an existing switch jump
997 * table, modifies it, and then hard-codes the jump (albeit with an indirect
998 * jump) to use a single entry in the table. The rest of the jump table and
999 * some of its jump targets remain as dead code.
1000 *
1001 * In such a case we can just crudely ignore all unreachable instruction
1002 * warnings for the entire object file. Ideally we would just ignore them
1003 * for the function, but that would require redesigning the code quite a
1004 * bit. And honestly that's just not worth doing: unreachable instruction
1005 * warnings are of questionable value anyway, and this is such a rare issue.
1006 *
1007 * 3. mov [rodata addr],%reg1
1008 * ... some instructions ...
1009 * jmpq *(%reg1,%reg2,8)
1010 *
1011 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1012 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1013 *
99ce7962
PZ
1014 * As of GCC 7 there are quite a few more of these and the 'in between' code
1015 * is significant. Esp. with KASAN enabled some of the code between the mov
1016 * and jmpq uses .rodata itself, which can confuse things.
1017 *
dcc914f4
JP
1018 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1019 * ensure the same register is used in the mov and jump instructions.
99ce7962
PZ
1020 *
1021 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
dcc914f4 1022 */
e7c2bc37 1023static struct rela *find_jump_table(struct objtool_file *file,
dcc914f4
JP
1024 struct symbol *func,
1025 struct instruction *insn)
1026{
e7c2bc37 1027 struct rela *text_rela, *table_rela;
dcc914f4 1028 struct instruction *orig_insn = insn;
e7c2bc37 1029 struct section *table_sec;
6f5ec299 1030 unsigned long table_offset;
dcc914f4 1031
99ce7962
PZ
1032 /*
1033 * Backward search using the @first_jump_src links, these help avoid
1034 * much of the 'in between' code. Which avoids us getting confused by
1035 * it.
1036 */
7dec80cc 1037 for (;
99ce7962
PZ
1038 &insn->list != &file->insn_list &&
1039 insn->sec == func->sec &&
1040 insn->offset >= func->offset;
1041
1042 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1043
7dec80cc 1044 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
dcc914f4
JP
1045 break;
1046
1047 /* allow small jumps within the range */
1048 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1049 insn->jump_dest &&
1050 (insn->jump_dest->offset <= insn->offset ||
1051 insn->jump_dest->offset > orig_insn->offset))
1052 break;
1053
1054 /* look for a relocation which references .rodata */
1055 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1056 insn->len);
4a60aa05
AX
1057 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1058 !text_rela->sym->sec->rodata)
dcc914f4
JP
1059 continue;
1060
6f5ec299 1061 table_offset = text_rela->addend;
e7c2bc37 1062 table_sec = text_rela->sym->sec;
4a60aa05 1063
6f5ec299
JP
1064 if (text_rela->type == R_X86_64_PC32)
1065 table_offset += 4;
1066
dcc914f4
JP
1067 /*
1068 * Make sure the .rodata address isn't associated with a
87b512de
JP
1069 * symbol. GCC jump tables are anonymous data.
1070 *
1071 * Also support C jump tables which are in the same format as
1072 * switch jump tables. For objtool to recognize them, they
1073 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1074 * have symbols associated with them.
dcc914f4 1075 */
e7c2bc37
JP
1076 if (find_symbol_containing(table_sec, table_offset) &&
1077 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1402fd8e
JP
1078 continue;
1079
e7c2bc37
JP
1080 /* Each table entry has a rela associated with it. */
1081 table_rela = find_rela_by_dest(table_sec, table_offset);
1082 if (!table_rela)
1083 continue;
7dec80cc 1084
e7c2bc37
JP
1085 /*
1086 * Use of RIP-relative switch jumps is quite rare, and
1087 * indicates a rare GCC quirk/bug which can leave dead code
1088 * behind.
1089 */
1090 if (text_rela->type == R_X86_64_PC32)
1091 file->ignore_unreachables = true;
1092
1093 return table_rela;
dcc914f4
JP
1094 }
1095
1096 return NULL;
1097}
1098
bd98c813
JH
1099/*
1100 * First pass: Mark the head of each jump table so that in the next pass,
1101 * we know when a given jump table ends and the next one starts.
1102 */
1103static void mark_func_jump_tables(struct objtool_file *file,
1104 struct symbol *func)
dcc914f4 1105{
bd98c813
JH
1106 struct instruction *insn, *last = NULL;
1107 struct rela *rela;
dcc914f4 1108
13810435 1109 func_for_each_insn_all(file, func, insn) {
99ce7962
PZ
1110 if (!last)
1111 last = insn;
1112
1113 /*
1114 * Store back-pointers for unconditional forward jumps such
e7c2bc37 1115 * that find_jump_table() can back-track using those and
99ce7962
PZ
1116 * avoid some potentially confusing code.
1117 */
1118 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1119 insn->offset > last->offset &&
1120 insn->jump_dest->offset > insn->offset &&
1121 !insn->jump_dest->first_jump_src) {
1122
1123 insn->jump_dest->first_jump_src = insn;
1124 last = insn->jump_dest;
1125 }
1126
dcc914f4
JP
1127 if (insn->type != INSN_JUMP_DYNAMIC)
1128 continue;
1129
e7c2bc37 1130 rela = find_jump_table(file, func, insn);
bd98c813
JH
1131 if (rela) {
1132 rela->jump_table_start = true;
1133 insn->jump_table = rela;
dcc914f4 1134 }
dcc914f4 1135 }
bd98c813
JH
1136}
1137
1138static int add_func_jump_tables(struct objtool_file *file,
1139 struct symbol *func)
1140{
1141 struct instruction *insn;
1142 int ret;
1143
1144 func_for_each_insn_all(file, func, insn) {
1145 if (!insn->jump_table)
1146 continue;
dcc914f4 1147
bd98c813 1148 ret = add_jump_table(file, insn, insn->jump_table);
dcc914f4
JP
1149 if (ret)
1150 return ret;
1151 }
1152
1153 return 0;
1154}
1155
1156/*
1157 * For some switch statements, gcc generates a jump table in the .rodata
1158 * section which contains a list of addresses within the function to jump to.
1159 * This finds these jump tables and adds them to the insn->alts lists.
1160 */
e7c2bc37 1161static int add_jump_table_alts(struct objtool_file *file)
dcc914f4
JP
1162{
1163 struct section *sec;
1164 struct symbol *func;
1165 int ret;
1166
4a60aa05 1167 if (!file->rodata)
dcc914f4
JP
1168 return 0;
1169
baa41469 1170 for_each_sec(file, sec) {
dcc914f4
JP
1171 list_for_each_entry(func, &sec->symbol_list, list) {
1172 if (func->type != STT_FUNC)
1173 continue;
1174
bd98c813 1175 mark_func_jump_tables(file, func);
e7c2bc37 1176 ret = add_func_jump_tables(file, func);
dcc914f4
JP
1177 if (ret)
1178 return ret;
1179 }
1180 }
1181
1182 return 0;
1183}
1184
39358a03
JP
1185static int read_unwind_hints(struct objtool_file *file)
1186{
1187 struct section *sec, *relasec;
1188 struct rela *rela;
1189 struct unwind_hint *hint;
1190 struct instruction *insn;
1191 struct cfi_reg *cfa;
1192 int i;
1193
1194 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1195 if (!sec)
1196 return 0;
1197
1198 relasec = sec->rela;
1199 if (!relasec) {
1200 WARN("missing .rela.discard.unwind_hints section");
1201 return -1;
1202 }
1203
1204 if (sec->len % sizeof(struct unwind_hint)) {
1205 WARN("struct unwind_hint size mismatch");
1206 return -1;
1207 }
1208
1209 file->hints = true;
1210
1211 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1212 hint = (struct unwind_hint *)sec->data->d_buf + i;
1213
1214 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1215 if (!rela) {
1216 WARN("can't find rela for unwind_hints[%d]", i);
1217 return -1;
1218 }
1219
1220 insn = find_insn(file, rela->sym->sec, rela->addend);
1221 if (!insn) {
1222 WARN("can't find insn for unwind_hints[%d]", i);
1223 return -1;
1224 }
1225
1226 cfa = &insn->state.cfa;
1227
1228 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1229 insn->save = true;
1230 continue;
1231
1232 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1233 insn->restore = true;
1234 insn->hint = true;
1235 continue;
1236 }
1237
1238 insn->hint = true;
1239
1240 switch (hint->sp_reg) {
1241 case ORC_REG_UNDEFINED:
1242 cfa->base = CFI_UNDEFINED;
1243 break;
1244 case ORC_REG_SP:
1245 cfa->base = CFI_SP;
1246 break;
1247 case ORC_REG_BP:
1248 cfa->base = CFI_BP;
1249 break;
1250 case ORC_REG_SP_INDIRECT:
1251 cfa->base = CFI_SP_INDIRECT;
1252 break;
1253 case ORC_REG_R10:
1254 cfa->base = CFI_R10;
1255 break;
1256 case ORC_REG_R13:
1257 cfa->base = CFI_R13;
1258 break;
1259 case ORC_REG_DI:
1260 cfa->base = CFI_DI;
1261 break;
1262 case ORC_REG_DX:
1263 cfa->base = CFI_DX;
1264 break;
1265 default:
1266 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1267 insn->sec, insn->offset, hint->sp_reg);
1268 return -1;
1269 }
1270
1271 cfa->offset = hint->sp_offset;
1272 insn->state.type = hint->type;
d31a5802 1273 insn->state.end = hint->end;
39358a03
JP
1274 }
1275
1276 return 0;
1277}
1278
b5bc2231
PZ
1279static int read_retpoline_hints(struct objtool_file *file)
1280{
63474dc4 1281 struct section *sec;
b5bc2231
PZ
1282 struct instruction *insn;
1283 struct rela *rela;
b5bc2231 1284
63474dc4 1285 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
b5bc2231
PZ
1286 if (!sec)
1287 return 0;
1288
63474dc4
JP
1289 list_for_each_entry(rela, &sec->rela_list, list) {
1290 if (rela->sym->type != STT_SECTION) {
1291 WARN("unexpected relocation symbol type in %s", sec->name);
b5bc2231
PZ
1292 return -1;
1293 }
1294
1295 insn = find_insn(file, rela->sym->sec, rela->addend);
1296 if (!insn) {
63474dc4 1297 WARN("bad .discard.retpoline_safe entry");
b5bc2231
PZ
1298 return -1;
1299 }
1300
1301 if (insn->type != INSN_JUMP_DYNAMIC &&
1302 insn->type != INSN_CALL_DYNAMIC) {
63474dc4 1303 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
b5bc2231
PZ
1304 insn->sec, insn->offset);
1305 return -1;
1306 }
1307
1308 insn->retpoline_safe = true;
1309 }
1310
1311 return 0;
1312}
1313
4a60aa05
AX
1314static void mark_rodata(struct objtool_file *file)
1315{
1316 struct section *sec;
1317 bool found = false;
1318
1319 /*
87b512de
JP
1320 * Search for the following rodata sections, each of which can
1321 * potentially contain jump tables:
1322 *
1323 * - .rodata: can contain GCC switch tables
1324 * - .rodata.<func>: same, if -fdata-sections is being used
1325 * - .rodata..c_jump_table: contains C annotated jump tables
1326 *
1327 * .rodata.str1.* sections are ignored; they don't contain jump tables.
4a60aa05
AX
1328 */
1329 for_each_sec(file, sec) {
87b512de
JP
1330 if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
1331 !strcmp(sec->name, C_JUMP_TABLE_SECTION)) {
4a60aa05
AX
1332 sec->rodata = true;
1333 found = true;
1334 }
1335 }
1336
1337 file->rodata = found;
1338}
1339
dcc914f4
JP
1340static int decode_sections(struct objtool_file *file)
1341{
1342 int ret;
1343
4a60aa05
AX
1344 mark_rodata(file);
1345
dcc914f4
JP
1346 ret = decode_instructions(file);
1347 if (ret)
1348 return ret;
1349
1350 ret = add_dead_ends(file);
1351 if (ret)
1352 return ret;
1353
1354 add_ignores(file);
ea24213d 1355 add_uaccess_safe(file);
dcc914f4 1356
ff05ab23 1357 ret = add_ignore_alternatives(file);
258c7605
JP
1358 if (ret)
1359 return ret;
1360
dcc914f4
JP
1361 ret = add_jump_destinations(file);
1362 if (ret)
1363 return ret;
1364
a845c7cf 1365 ret = add_special_section_alts(file);
dcc914f4
JP
1366 if (ret)
1367 return ret;
1368
a845c7cf 1369 ret = add_call_destinations(file);
dcc914f4
JP
1370 if (ret)
1371 return ret;
1372
e7c2bc37 1373 ret = add_jump_table_alts(file);
dcc914f4
JP
1374 if (ret)
1375 return ret;
1376
39358a03
JP
1377 ret = read_unwind_hints(file);
1378 if (ret)
1379 return ret;
1380
b5bc2231
PZ
1381 ret = read_retpoline_hints(file);
1382 if (ret)
1383 return ret;
1384
dcc914f4
JP
1385 return 0;
1386}
1387
1388static bool is_fentry_call(struct instruction *insn)
1389{
1390 if (insn->type == INSN_CALL &&
1391 insn->call_dest->type == STT_NOTYPE &&
1392 !strcmp(insn->call_dest->name, "__fentry__"))
1393 return true;
1394
1395 return false;
1396}
1397
baa41469 1398static bool has_modified_stack_frame(struct insn_state *state)
dcc914f4 1399{
baa41469
JP
1400 int i;
1401
1402 if (state->cfa.base != initial_func_cfi.cfa.base ||
1403 state->cfa.offset != initial_func_cfi.cfa.offset ||
1404 state->stack_size != initial_func_cfi.cfa.offset ||
1405 state->drap)
1406 return true;
1407
1408 for (i = 0; i < CFI_NUM_REGS; i++)
1409 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1410 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1411 return true;
1412
1413 return false;
1414}
1415
1416static bool has_valid_stack_frame(struct insn_state *state)
1417{
1418 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1419 state->regs[CFI_BP].offset == -16)
1420 return true;
1421
1422 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1423 return true;
1424
1425 return false;
dcc914f4
JP
1426}
1427
627fce14
JP
1428static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1429{
1430 struct cfi_reg *cfa = &state->cfa;
1431 struct stack_op *op = &insn->stack_op;
1432
1433 if (cfa->base != CFI_SP)
1434 return 0;
1435
1436 /* push */
ea24213d 1437 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
627fce14
JP
1438 cfa->offset += 8;
1439
1440 /* pop */
ea24213d 1441 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
627fce14
JP
1442 cfa->offset -= 8;
1443
1444 /* add immediate to sp */
1445 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1446 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1447 cfa->offset -= op->src.offset;
1448
1449 return 0;
1450}
1451
baa41469
JP
1452static void save_reg(struct insn_state *state, unsigned char reg, int base,
1453 int offset)
dcc914f4 1454{
bf4d1a83 1455 if (arch_callee_saved_reg(reg) &&
baa41469
JP
1456 state->regs[reg].base == CFI_UNDEFINED) {
1457 state->regs[reg].base = base;
1458 state->regs[reg].offset = offset;
1459 }
dcc914f4
JP
1460}
1461
baa41469 1462static void restore_reg(struct insn_state *state, unsigned char reg)
dcc914f4 1463{
baa41469
JP
1464 state->regs[reg].base = CFI_UNDEFINED;
1465 state->regs[reg].offset = 0;
1466}
1467
1468/*
1469 * A note about DRAP stack alignment:
1470 *
1471 * GCC has the concept of a DRAP register, which is used to help keep track of
1472 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1473 * register. The typical DRAP pattern is:
1474 *
1475 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1476 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1477 * 41 ff 72 f8 pushq -0x8(%r10)
1478 * 55 push %rbp
1479 * 48 89 e5 mov %rsp,%rbp
1480 * (more pushes)
1481 * 41 52 push %r10
1482 * ...
1483 * 41 5a pop %r10
1484 * (more pops)
1485 * 5d pop %rbp
1486 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1487 * c3 retq
1488 *
1489 * There are some variations in the epilogues, like:
1490 *
1491 * 5b pop %rbx
1492 * 41 5a pop %r10
1493 * 41 5c pop %r12
1494 * 41 5d pop %r13
1495 * 41 5e pop %r14
1496 * c9 leaveq
1497 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1498 * c3 retq
1499 *
1500 * and:
1501 *
1502 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1503 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1504 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1505 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1506 * c9 leaveq
1507 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1508 * c3 retq
1509 *
1510 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1511 * restored beforehand:
1512 *
1513 * 41 55 push %r13
1514 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1515 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1516 * ...
1517 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1518 * 41 5d pop %r13
1519 * c3 retq
1520 */
1521static int update_insn_state(struct instruction *insn, struct insn_state *state)
1522{
1523 struct stack_op *op = &insn->stack_op;
1524 struct cfi_reg *cfa = &state->cfa;
1525 struct cfi_reg *regs = state->regs;
1526
1527 /* stack operations don't make sense with an undefined CFA */
1528 if (cfa->base == CFI_UNDEFINED) {
1529 if (insn->func) {
1530 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1531 return -1;
1532 }
1533 return 0;
1534 }
1535
627fce14
JP
1536 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1537 return update_insn_state_regs(insn, state);
1538
baa41469
JP
1539 switch (op->dest.type) {
1540
1541 case OP_DEST_REG:
1542 switch (op->src.type) {
1543
1544 case OP_SRC_REG:
0d0970ee
JP
1545 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1546 cfa->base == CFI_SP &&
1547 regs[CFI_BP].base == CFI_CFA &&
1548 regs[CFI_BP].offset == -cfa->offset) {
1549
1550 /* mov %rsp, %rbp */
1551 cfa->base = op->dest.reg;
1552 state->bp_scratch = false;
1553 }
dd88a0a0 1554
0d0970ee
JP
1555 else if (op->src.reg == CFI_SP &&
1556 op->dest.reg == CFI_BP && state->drap) {
dd88a0a0 1557
0d0970ee
JP
1558 /* drap: mov %rsp, %rbp */
1559 regs[CFI_BP].base = CFI_BP;
1560 regs[CFI_BP].offset = -state->stack_size;
1561 state->bp_scratch = false;
1562 }
dd88a0a0 1563
0d0970ee
JP
1564 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1565
1566 /*
1567 * mov %rsp, %reg
1568 *
1569 * This is needed for the rare case where GCC
1570 * does:
1571 *
1572 * mov %rsp, %rax
1573 * ...
1574 * mov %rax, %rsp
1575 */
1576 state->vals[op->dest.reg].base = CFI_CFA;
1577 state->vals[op->dest.reg].offset = -state->stack_size;
dd88a0a0
JP
1578 }
1579
3c1f0583
JP
1580 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1581 cfa->base == CFI_BP) {
1582
1583 /*
1584 * mov %rbp, %rsp
1585 *
1586 * Restore the original stack pointer (Clang).
1587 */
1588 state->stack_size = -state->regs[CFI_BP].offset;
1589 }
1590
dd88a0a0
JP
1591 else if (op->dest.reg == cfa->base) {
1592
1593 /* mov %reg, %rsp */
1594 if (cfa->base == CFI_SP &&
1595 state->vals[op->src.reg].base == CFI_CFA) {
1596
1597 /*
1598 * This is needed for the rare case
1599 * where GCC does something dumb like:
1600 *
1601 * lea 0x8(%rsp), %rcx
1602 * ...
1603 * mov %rcx, %rsp
1604 */
1605 cfa->offset = -state->vals[op->src.reg].offset;
1606 state->stack_size = cfa->offset;
1607
1608 } else {
1609 cfa->base = CFI_UNDEFINED;
1610 cfa->offset = 0;
1611 }
baa41469
JP
1612 }
1613
1614 break;
1615
1616 case OP_SRC_ADD:
1617 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1618
1619 /* add imm, %rsp */
1620 state->stack_size -= op->src.offset;
1621 if (cfa->base == CFI_SP)
1622 cfa->offset -= op->src.offset;
1623 break;
1624 }
1625
1626 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1627
1628 /* lea disp(%rbp), %rsp */
1629 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1630 break;
1631 }
1632
dd88a0a0 1633 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
baa41469
JP
1634
1635 /* drap: lea disp(%rsp), %drap */
1636 state->drap_reg = op->dest.reg;
dd88a0a0
JP
1637
1638 /*
1639 * lea disp(%rsp), %reg
1640 *
1641 * This is needed for the rare case where GCC
1642 * does something dumb like:
1643 *
1644 * lea 0x8(%rsp), %rcx
1645 * ...
1646 * mov %rcx, %rsp
1647 */
1648 state->vals[op->dest.reg].base = CFI_CFA;
1649 state->vals[op->dest.reg].offset = \
1650 -state->stack_size + op->src.offset;
1651
baa41469
JP
1652 break;
1653 }
1654
1655 if (state->drap && op->dest.reg == CFI_SP &&
1656 op->src.reg == state->drap_reg) {
1657
1658 /* drap: lea disp(%drap), %rsp */
1659 cfa->base = CFI_SP;
1660 cfa->offset = state->stack_size = -op->src.offset;
1661 state->drap_reg = CFI_UNDEFINED;
1662 state->drap = false;
1663 break;
1664 }
1665
1666 if (op->dest.reg == state->cfa.base) {
1667 WARN_FUNC("unsupported stack register modification",
1668 insn->sec, insn->offset);
1669 return -1;
1670 }
1671
1672 break;
1673
1674 case OP_SRC_AND:
1675 if (op->dest.reg != CFI_SP ||
1676 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1677 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1678 WARN_FUNC("unsupported stack pointer realignment",
1679 insn->sec, insn->offset);
1680 return -1;
1681 }
1682
1683 if (state->drap_reg != CFI_UNDEFINED) {
1684 /* drap: and imm, %rsp */
1685 cfa->base = state->drap_reg;
1686 cfa->offset = state->stack_size = 0;
1687 state->drap = true;
baa41469
JP
1688 }
1689
1690 /*
1691 * Older versions of GCC (4.8ish) realign the stack
1692 * without DRAP, with a frame pointer.
1693 */
1694
1695 break;
1696
1697 case OP_SRC_POP:
ea24213d 1698 case OP_SRC_POPF:
baa41469
JP
1699 if (!state->drap && op->dest.type == OP_DEST_REG &&
1700 op->dest.reg == cfa->base) {
1701
1702 /* pop %rbp */
1703 cfa->base = CFI_SP;
1704 }
1705
bf4d1a83
JP
1706 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1707 op->dest.type == OP_DEST_REG &&
1708 op->dest.reg == state->drap_reg &&
1709 state->drap_offset == -state->stack_size) {
baa41469 1710
bf4d1a83
JP
1711 /* drap: pop %drap */
1712 cfa->base = state->drap_reg;
1713 cfa->offset = 0;
1714 state->drap_offset = -1;
baa41469 1715
bf4d1a83 1716 } else if (regs[op->dest.reg].offset == -state->stack_size) {
baa41469 1717
bf4d1a83 1718 /* pop %reg */
baa41469
JP
1719 restore_reg(state, op->dest.reg);
1720 }
1721
1722 state->stack_size -= 8;
1723 if (cfa->base == CFI_SP)
1724 cfa->offset -= 8;
1725
1726 break;
1727
1728 case OP_SRC_REG_INDIRECT:
bf4d1a83
JP
1729 if (state->drap && op->src.reg == CFI_BP &&
1730 op->src.offset == state->drap_offset) {
1731
1732 /* drap: mov disp(%rbp), %drap */
1733 cfa->base = state->drap_reg;
1734 cfa->offset = 0;
1735 state->drap_offset = -1;
1736 }
1737
baa41469
JP
1738 if (state->drap && op->src.reg == CFI_BP &&
1739 op->src.offset == regs[op->dest.reg].offset) {
1740
1741 /* drap: mov disp(%rbp), %reg */
baa41469
JP
1742 restore_reg(state, op->dest.reg);
1743
1744 } else if (op->src.reg == cfa->base &&
1745 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1746
1747 /* mov disp(%rbp), %reg */
1748 /* mov disp(%rsp), %reg */
1749 restore_reg(state, op->dest.reg);
1750 }
1751
1752 break;
1753
1754 default:
1755 WARN_FUNC("unknown stack-related instruction",
1756 insn->sec, insn->offset);
1757 return -1;
1758 }
1759
1760 break;
1761
1762 case OP_DEST_PUSH:
ea24213d 1763 case OP_DEST_PUSHF:
baa41469
JP
1764 state->stack_size += 8;
1765 if (cfa->base == CFI_SP)
1766 cfa->offset += 8;
1767
1768 if (op->src.type != OP_SRC_REG)
1769 break;
1770
1771 if (state->drap) {
1772 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1773
1774 /* drap: push %drap */
1775 cfa->base = CFI_BP_INDIRECT;
1776 cfa->offset = -state->stack_size;
1777
bf4d1a83
JP
1778 /* save drap so we know when to restore it */
1779 state->drap_offset = -state->stack_size;
baa41469
JP
1780
1781 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1782
1783 /* drap: push %rbp */
1784 state->stack_size = 0;
1785
1786 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1787
1788 /* drap: push %reg */
1789 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1790 }
1791
1792 } else {
1793
1794 /* push %reg */
1795 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1796 }
1797
1798 /* detect when asm code uses rbp as a scratch register */
867ac9d7 1799 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
baa41469
JP
1800 cfa->base != CFI_BP)
1801 state->bp_scratch = true;
1802 break;
1803
1804 case OP_DEST_REG_INDIRECT:
1805
1806 if (state->drap) {
1807 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1808
1809 /* drap: mov %drap, disp(%rbp) */
1810 cfa->base = CFI_BP_INDIRECT;
1811 cfa->offset = op->dest.offset;
1812
bf4d1a83
JP
1813 /* save drap offset so we know when to restore it */
1814 state->drap_offset = op->dest.offset;
baa41469
JP
1815 }
1816
1817 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1818
1819 /* drap: mov reg, disp(%rbp) */
1820 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1821 }
1822
1823 } else if (op->dest.reg == cfa->base) {
1824
1825 /* mov reg, disp(%rbp) */
1826 /* mov reg, disp(%rsp) */
1827 save_reg(state, op->src.reg, CFI_CFA,
1828 op->dest.offset - state->cfa.offset);
1829 }
1830
1831 break;
1832
1833 case OP_DEST_LEAVE:
1834 if ((!state->drap && cfa->base != CFI_BP) ||
1835 (state->drap && cfa->base != state->drap_reg)) {
1836 WARN_FUNC("leave instruction with modified stack frame",
1837 insn->sec, insn->offset);
1838 return -1;
1839 }
1840
1841 /* leave (mov %rbp, %rsp; pop %rbp) */
1842
1843 state->stack_size = -state->regs[CFI_BP].offset - 8;
1844 restore_reg(state, CFI_BP);
1845
1846 if (!state->drap) {
1847 cfa->base = CFI_SP;
1848 cfa->offset -= 8;
1849 }
1850
1851 break;
1852
1853 case OP_DEST_MEM:
ea24213d 1854 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
baa41469
JP
1855 WARN_FUNC("unknown stack-related memory operation",
1856 insn->sec, insn->offset);
1857 return -1;
1858 }
1859
1860 /* pop mem */
1861 state->stack_size -= 8;
1862 if (cfa->base == CFI_SP)
1863 cfa->offset -= 8;
1864
1865 break;
1866
1867 default:
1868 WARN_FUNC("unknown stack-related instruction",
1869 insn->sec, insn->offset);
1870 return -1;
1871 }
1872
1873 return 0;
1874}
1875
1876static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1877{
1878 struct insn_state *state1 = &insn->state, *state2 = state;
1879 int i;
1880
1881 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1882 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1883 insn->sec, insn->offset,
1884 state1->cfa.base, state1->cfa.offset,
1885 state2->cfa.base, state2->cfa.offset);
1886
1887 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1888 for (i = 0; i < CFI_NUM_REGS; i++) {
1889 if (!memcmp(&state1->regs[i], &state2->regs[i],
1890 sizeof(struct cfi_reg)))
1891 continue;
1892
1893 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1894 insn->sec, insn->offset,
1895 i, state1->regs[i].base, state1->regs[i].offset,
1896 i, state2->regs[i].base, state2->regs[i].offset);
1897 break;
1898 }
1899
627fce14
JP
1900 } else if (state1->type != state2->type) {
1901 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1902 insn->sec, insn->offset, state1->type, state2->type);
1903
baa41469 1904 } else if (state1->drap != state2->drap ||
bf4d1a83
JP
1905 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1906 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1907 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
baa41469 1908 insn->sec, insn->offset,
bf4d1a83
JP
1909 state1->drap, state1->drap_reg, state1->drap_offset,
1910 state2->drap, state2->drap_reg, state2->drap_offset);
baa41469
JP
1911
1912 } else
1913 return true;
1914
1915 return false;
dcc914f4
JP
1916}
1917
ea24213d
PZ
1918static inline bool func_uaccess_safe(struct symbol *func)
1919{
1920 if (func)
e10cd8fe 1921 return func->uaccess_safe;
ea24213d
PZ
1922
1923 return false;
1924}
1925
0c1ddd33 1926static inline const char *call_dest_name(struct instruction *insn)
ea24213d
PZ
1927{
1928 if (insn->call_dest)
1929 return insn->call_dest->name;
1930
1931 return "{dynamic}";
1932}
1933
1934static int validate_call(struct instruction *insn, struct insn_state *state)
1935{
1936 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1937 WARN_FUNC("call to %s() with UACCESS enabled",
0c1ddd33 1938 insn->sec, insn->offset, call_dest_name(insn));
ea24213d
PZ
1939 return 1;
1940 }
1941
2f0f9e9a
PZ
1942 if (state->df) {
1943 WARN_FUNC("call to %s() with DF set",
0c1ddd33 1944 insn->sec, insn->offset, call_dest_name(insn));
2f0f9e9a
PZ
1945 return 1;
1946 }
1947
ea24213d
PZ
1948 return 0;
1949}
1950
54262aa2
PZ
1951static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1952{
1953 if (has_modified_stack_frame(state)) {
1954 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1955 insn->sec, insn->offset);
1956 return 1;
1957 }
1958
ea24213d 1959 return validate_call(insn, state);
54262aa2
PZ
1960}
1961
dcc914f4
JP
1962/*
1963 * Follow the branch starting at the given instruction, and recursively follow
1964 * any other branches (jumps). Meanwhile, track the frame pointer state at
1965 * each instruction and validate all the rules described in
1966 * tools/objtool/Documentation/stack-validation.txt.
1967 */
c705cecc
JP
1968static int validate_branch(struct objtool_file *file, struct symbol *func,
1969 struct instruction *first, struct insn_state state)
dcc914f4
JP
1970{
1971 struct alternative *alt;
39358a03 1972 struct instruction *insn, *next_insn;
dcc914f4 1973 struct section *sec;
882a0db9 1974 u8 visited;
dcc914f4
JP
1975 int ret;
1976
1977 insn = first;
1978 sec = insn->sec;
dcc914f4
JP
1979
1980 if (insn->alt_group && list_empty(&insn->alts)) {
1981 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1982 sec, insn->offset);
12b25729 1983 return 1;
dcc914f4
JP
1984 }
1985
1986 while (1) {
39358a03
JP
1987 next_insn = next_insn_same_sec(file, insn);
1988
13810435 1989 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
ee97638b
JP
1990 WARN("%s() falls through to next function %s()",
1991 func->name, insn->func->name);
1992 return 1;
dcc914f4
JP
1993 }
1994
4855022a
JP
1995 if (func && insn->ignore) {
1996 WARN_FUNC("BUG: why am I validating an ignored function?",
1997 sec, insn->offset);
12b25729 1998 return 1;
4855022a
JP
1999 }
2000
882a0db9 2001 visited = 1 << state.uaccess;
dcc914f4 2002 if (insn->visited) {
39358a03 2003 if (!insn->hint && !insn_state_match(insn, &state))
dcc914f4 2004 return 1;
dcc914f4 2005
882a0db9 2006 if (insn->visited & visited)
ea24213d 2007 return 0;
dcc914f4
JP
2008 }
2009
39358a03
JP
2010 if (insn->hint) {
2011 if (insn->restore) {
2012 struct instruction *save_insn, *i;
2013
2014 i = insn;
2015 save_insn = NULL;
c705cecc 2016 func_for_each_insn_continue_reverse(file, func, i) {
39358a03
JP
2017 if (i->save) {
2018 save_insn = i;
2019 break;
2020 }
2021 }
2022
2023 if (!save_insn) {
2024 WARN_FUNC("no corresponding CFI save for CFI restore",
2025 sec, insn->offset);
2026 return 1;
2027 }
2028
2029 if (!save_insn->visited) {
2030 /*
2031 * Oops, no state to copy yet.
2032 * Hopefully we can reach this
2033 * instruction from another branch
2034 * after the save insn has been
2035 * visited.
2036 */
2037 if (insn == first)
2038 return 0;
2039
2040 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2041 sec, insn->offset);
2042 return 1;
2043 }
2044
2045 insn->state = save_insn->state;
2046 }
2047
2048 state = insn->state;
2049
2050 } else
2051 insn->state = state;
dcc914f4 2052
882a0db9 2053 insn->visited |= visited;
baa41469 2054
a845c7cf 2055 if (!insn->ignore_alts) {
764eef4b
PZ
2056 bool skip_orig = false;
2057
a845c7cf 2058 list_for_each_entry(alt, &insn->alts, list) {
764eef4b
PZ
2059 if (alt->skip_orig)
2060 skip_orig = true;
2061
c705cecc 2062 ret = validate_branch(file, func, alt->insn, state);
7697eee3
PZ
2063 if (ret) {
2064 if (backtrace)
2065 BT_FUNC("(alt)", insn);
2066 return ret;
2067 }
a845c7cf 2068 }
764eef4b
PZ
2069
2070 if (skip_orig)
2071 return 0;
dcc914f4
JP
2072 }
2073
2074 switch (insn->type) {
2075
dcc914f4 2076 case INSN_RETURN:
ea24213d
PZ
2077 if (state.uaccess && !func_uaccess_safe(func)) {
2078 WARN_FUNC("return with UACCESS enabled", sec, insn->offset);
2079 return 1;
2080 }
2081
2082 if (!state.uaccess && func_uaccess_safe(func)) {
2083 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function", sec, insn->offset);
2084 return 1;
2085 }
2086
2f0f9e9a
PZ
2087 if (state.df) {
2088 WARN_FUNC("return with DF set", sec, insn->offset);
2089 return 1;
2090 }
2091
baa41469
JP
2092 if (func && has_modified_stack_frame(&state)) {
2093 WARN_FUNC("return with modified stack frame",
dcc914f4
JP
2094 sec, insn->offset);
2095 return 1;
2096 }
baa41469
JP
2097
2098 if (state.bp_scratch) {
2099 WARN("%s uses BP as a scratch register",
c705cecc 2100 func->name);
baa41469
JP
2101 return 1;
2102 }
2103
dcc914f4
JP
2104 return 0;
2105
2106 case INSN_CALL:
ea24213d
PZ
2107 case INSN_CALL_DYNAMIC:
2108 ret = validate_call(insn, &state);
2109 if (ret)
2110 return ret;
dcc914f4 2111
c9bab22b
JP
2112 if (!no_fp && func && !is_fentry_call(insn) &&
2113 !has_valid_stack_frame(&state)) {
dcc914f4
JP
2114 WARN_FUNC("call without frame pointer save/setup",
2115 sec, insn->offset);
2116 return 1;
2117 }
c9bab22b
JP
2118
2119 if (dead_end_function(file, insn->call_dest))
2120 return 0;
2121
dcc914f4
JP
2122 break;
2123
2124 case INSN_JUMP_CONDITIONAL:
2125 case INSN_JUMP_UNCONDITIONAL:
0c1ddd33 2126 if (func && is_sibling_call(insn)) {
54262aa2 2127 ret = validate_sibling_call(insn, &state);
dcc914f4 2128 if (ret)
54262aa2 2129 return ret;
4855022a 2130
0c1ddd33 2131 } else if (insn->jump_dest) {
c705cecc
JP
2132 ret = validate_branch(file, func,
2133 insn->jump_dest, state);
7697eee3
PZ
2134 if (ret) {
2135 if (backtrace)
2136 BT_FUNC("(branch)", insn);
2137 return ret;
2138 }
4855022a 2139 }
dcc914f4
JP
2140
2141 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2142 return 0;
2143
2144 break;
2145
2146 case INSN_JUMP_DYNAMIC:
b68b9907 2147 case INSN_JUMP_DYNAMIC_CONDITIONAL:
0c1ddd33 2148 if (func && is_sibling_call(insn)) {
54262aa2
PZ
2149 ret = validate_sibling_call(insn, &state);
2150 if (ret)
2151 return ret;
dcc914f4
JP
2152 }
2153
b68b9907
JP
2154 if (insn->type == INSN_JUMP_DYNAMIC)
2155 return 0;
2156
2157 break;
dcc914f4 2158
39358a03
JP
2159 case INSN_CONTEXT_SWITCH:
2160 if (func && (!next_insn || !next_insn->hint)) {
2161 WARN_FUNC("unsupported instruction in callable function",
2162 sec, insn->offset);
2163 return 1;
2164 }
2165 return 0;
2166
baa41469
JP
2167 case INSN_STACK:
2168 if (update_insn_state(insn, &state))
12b25729 2169 return 1;
baa41469 2170
ea24213d
PZ
2171 if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2172 if (!state.uaccess_stack) {
2173 state.uaccess_stack = 1;
2174 } else if (state.uaccess_stack >> 31) {
2175 WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2176 return 1;
2177 }
2178 state.uaccess_stack <<= 1;
2179 state.uaccess_stack |= state.uaccess;
2180 }
2181
2182 if (insn->stack_op.src.type == OP_SRC_POPF) {
2183 if (state.uaccess_stack) {
2184 state.uaccess = state.uaccess_stack & 1;
2185 state.uaccess_stack >>= 1;
2186 if (state.uaccess_stack == 1)
2187 state.uaccess_stack = 0;
2188 }
2189 }
2190
2191 break;
2192
2193 case INSN_STAC:
2194 if (state.uaccess) {
2195 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2196 return 1;
2197 }
2198
2199 state.uaccess = true;
2200 break;
2201
2202 case INSN_CLAC:
c705cecc 2203 if (!state.uaccess && func) {
ea24213d
PZ
2204 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2205 return 1;
2206 }
2207
2208 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2209 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2210 return 1;
2211 }
2212
2213 state.uaccess = false;
baa41469
JP
2214 break;
2215
2f0f9e9a
PZ
2216 case INSN_STD:
2217 if (state.df)
2218 WARN_FUNC("recursive STD", sec, insn->offset);
2219
2220 state.df = true;
2221 break;
2222
2223 case INSN_CLD:
c705cecc 2224 if (!state.df && func)
2f0f9e9a
PZ
2225 WARN_FUNC("redundant CLD", sec, insn->offset);
2226
2227 state.df = false;
baa41469
JP
2228 break;
2229
dcc914f4
JP
2230 default:
2231 break;
2232 }
2233
2234 if (insn->dead_end)
2235 return 0;
2236
00d96180
JP
2237 if (!next_insn) {
2238 if (state.cfa.base == CFI_UNDEFINED)
2239 return 0;
dcc914f4
JP
2240 WARN("%s: unexpected end of section", sec->name);
2241 return 1;
2242 }
00d96180
JP
2243
2244 insn = next_insn;
dcc914f4
JP
2245 }
2246
2247 return 0;
2248}
2249
39358a03
JP
2250static int validate_unwind_hints(struct objtool_file *file)
2251{
2252 struct instruction *insn;
2253 int ret, warnings = 0;
2254 struct insn_state state;
2255
2256 if (!file->hints)
2257 return 0;
2258
2259 clear_insn_state(&state);
2260
2261 for_each_insn(file, insn) {
2262 if (insn->hint && !insn->visited) {
c705cecc 2263 ret = validate_branch(file, insn->func, insn, state);
7697eee3
PZ
2264 if (ret && backtrace)
2265 BT_FUNC("<=== (hint)", insn);
39358a03
JP
2266 warnings += ret;
2267 }
2268 }
2269
2270 return warnings;
2271}
2272
b5bc2231
PZ
2273static int validate_retpoline(struct objtool_file *file)
2274{
2275 struct instruction *insn;
2276 int warnings = 0;
2277
2278 for_each_insn(file, insn) {
2279 if (insn->type != INSN_JUMP_DYNAMIC &&
2280 insn->type != INSN_CALL_DYNAMIC)
2281 continue;
2282
2283 if (insn->retpoline_safe)
2284 continue;
2285
ca41b97e
PZ
2286 /*
2287 * .init.text code is ran before userspace and thus doesn't
2288 * strictly need retpolines, except for modules which are
2289 * loaded late, they very much do need retpoline in their
2290 * .init.text
2291 */
2292 if (!strcmp(insn->sec->name, ".init.text") && !module)
2293 continue;
2294
b5bc2231
PZ
2295 WARN_FUNC("indirect %s found in RETPOLINE build",
2296 insn->sec, insn->offset,
2297 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2298
2299 warnings++;
2300 }
2301
2302 return warnings;
2303}
2304
dcc914f4
JP
2305static bool is_kasan_insn(struct instruction *insn)
2306{
2307 return (insn->type == INSN_CALL &&
2308 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2309}
2310
2311static bool is_ubsan_insn(struct instruction *insn)
2312{
2313 return (insn->type == INSN_CALL &&
2314 !strcmp(insn->call_dest->name,
2315 "__ubsan_handle_builtin_unreachable"));
2316}
2317
baa41469 2318static bool ignore_unreachable_insn(struct instruction *insn)
dcc914f4
JP
2319{
2320 int i;
2321
baa41469
JP
2322 if (insn->ignore || insn->type == INSN_NOP)
2323 return true;
2324
2325 /*
2326 * Ignore any unused exceptions. This can happen when a whitelisted
2327 * function has an exception table entry.
0e2bb2bc
JP
2328 *
2329 * Also ignore alternative replacement instructions. This can happen
2330 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 2331 */
0e2bb2bc
JP
2332 if (!strcmp(insn->sec->name, ".fixup") ||
2333 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2334 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
2335 return true;
2336
2337 /*
2338 * Check if this (or a subsequent) instruction is related to
2339 * CONFIG_UBSAN or CONFIG_KASAN.
2340 *
2341 * End the search at 5 instructions to avoid going into the weeds.
2342 */
baa41469
JP
2343 if (!insn->func)
2344 return false;
dcc914f4
JP
2345 for (i = 0; i < 5; i++) {
2346
2347 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2348 return true;
2349
fe24e271
JP
2350 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2351 if (insn->jump_dest &&
2352 insn->jump_dest->func == insn->func) {
2353 insn = insn->jump_dest;
2354 continue;
2355 }
2356
2357 break;
dcc914f4
JP
2358 }
2359
baa41469 2360 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
dcc914f4 2361 break;
fe24e271 2362
dcc914f4
JP
2363 insn = list_next_entry(insn, list);
2364 }
2365
2366 return false;
2367}
2368
2369static int validate_functions(struct objtool_file *file)
2370{
2371 struct section *sec;
2372 struct symbol *func;
2373 struct instruction *insn;
baa41469 2374 struct insn_state state;
dcc914f4
JP
2375 int ret, warnings = 0;
2376
baa41469
JP
2377 clear_insn_state(&state);
2378
2379 state.cfa = initial_func_cfi.cfa;
2380 memcpy(&state.regs, &initial_func_cfi.regs,
2381 CFI_NUM_REGS * sizeof(struct cfi_reg));
2382 state.stack_size = initial_func_cfi.cfa.offset;
2383
2384 for_each_sec(file, sec) {
dcc914f4 2385 list_for_each_entry(func, &sec->symbol_list, list) {
e10cd8fe
JP
2386 if (func->type != STT_FUNC)
2387 continue;
2388
61e9b75a
JP
2389 if (!func->len) {
2390 WARN("%s() is missing an ELF size annotation",
2391 func->name);
2392 warnings++;
2393 }
2394
e10cd8fe 2395 if (func->pfunc != func || func->alias != func)
dcc914f4
JP
2396 continue;
2397
2398 insn = find_insn(file, sec, func->offset);
c705cecc 2399 if (!insn || insn->ignore || insn->visited)
dcc914f4
JP
2400 continue;
2401
e10cd8fe 2402 state.uaccess = func->uaccess_safe;
ea24213d 2403
c705cecc 2404 ret = validate_branch(file, func, insn, state);
7697eee3
PZ
2405 if (ret && backtrace)
2406 BT_FUNC("<=== (func)", insn);
dcc914f4
JP
2407 warnings += ret;
2408 }
2409 }
2410
dcc914f4
JP
2411 return warnings;
2412}
2413
baa41469 2414static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4
JP
2415{
2416 struct instruction *insn;
baa41469
JP
2417
2418 if (file->ignore_unreachables)
2419 return 0;
dcc914f4
JP
2420
2421 for_each_insn(file, insn) {
baa41469
JP
2422 if (insn->visited || ignore_unreachable_insn(insn))
2423 continue;
2424
baa41469
JP
2425 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2426 return 1;
dcc914f4
JP
2427 }
2428
baa41469 2429 return 0;
dcc914f4
JP
2430}
2431
2432static void cleanup(struct objtool_file *file)
2433{
2434 struct instruction *insn, *tmpinsn;
2435 struct alternative *alt, *tmpalt;
2436
2437 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2438 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2439 list_del(&alt->list);
2440 free(alt);
2441 }
2442 list_del(&insn->list);
2443 hash_del(&insn->hash);
2444 free(insn);
2445 }
2446 elf_close(file->elf);
2447}
2448
0c671812
JP
2449static struct objtool_file file;
2450
43a4525f 2451int check(const char *_objname, bool orc)
dcc914f4 2452{
dcc914f4
JP
2453 int ret, warnings = 0;
2454
2455 objname = _objname;
dcc914f4 2456
8e144797 2457 file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
baa41469 2458 if (!file.elf)
dcc914f4 2459 return 1;
dcc914f4
JP
2460
2461 INIT_LIST_HEAD(&file.insn_list);
2462 hash_init(file.insn_hash);
dcc914f4 2463 file.c_file = find_section_by_name(file.elf, ".comment");
867ac9d7 2464 file.ignore_unreachables = no_unreachable;
39358a03 2465 file.hints = false;
dcc914f4 2466
baa41469
JP
2467 arch_initial_func_cfi_state(&initial_func_cfi);
2468
dcc914f4
JP
2469 ret = decode_sections(&file);
2470 if (ret < 0)
2471 goto out;
2472 warnings += ret;
2473
baa41469 2474 if (list_empty(&file.insn_list))
dcc914f4 2475 goto out;
dcc914f4 2476
b5bc2231
PZ
2477 if (retpoline) {
2478 ret = validate_retpoline(&file);
2479 if (ret < 0)
2480 return ret;
2481 warnings += ret;
2482 }
2483
baa41469 2484 ret = validate_functions(&file);
dcc914f4
JP
2485 if (ret < 0)
2486 goto out;
2487 warnings += ret;
2488
39358a03
JP
2489 ret = validate_unwind_hints(&file);
2490 if (ret < 0)
2491 goto out;
2492 warnings += ret;
2493
baa41469
JP
2494 if (!warnings) {
2495 ret = validate_reachable_instructions(&file);
2496 if (ret < 0)
2497 goto out;
2498 warnings += ret;
2499 }
2500
627fce14
JP
2501 if (orc) {
2502 ret = create_orc(&file);
2503 if (ret < 0)
2504 goto out;
2505
2506 ret = create_orc_sections(&file);
2507 if (ret < 0)
2508 goto out;
2509
2510 ret = elf_write(file.elf);
2511 if (ret < 0)
2512 goto out;
2513 }
2514
dcc914f4
JP
2515out:
2516 cleanup(&file);
2517
644592d3
JP
2518 if (ret < 0) {
2519 /*
2520 * Fatal error. The binary is corrupt or otherwise broken in
2521 * some way, or objtool itself is broken. Fail the kernel
2522 * build.
2523 */
2524 return ret;
2525 }
2526
dcc914f4
JP
2527 return 0;
2528}