]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/objtool/check.c
objtool: Introduce validate_return()
[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:
7acfe531
JP
423 func = find_func_by_offset(rela->sym->sec, rela->addend);
424 if (!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;
7acfe531
JP
668 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
669 if (!insn->call_dest)
670 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
a845c7cf 671
7acfe531
JP
672 if (insn->ignore)
673 continue;
674
675 if (!insn->call_dest) {
a845c7cf
JP
676 WARN_FUNC("unsupported intra-function call",
677 insn->sec, insn->offset);
b5bc2231
PZ
678 if (retpoline)
679 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
dcc914f4
JP
680 return -1;
681 }
a845c7cf 682
7acfe531
JP
683 if (insn->func && insn->call_dest->type != STT_FUNC) {
684 WARN_FUNC("unsupported call to non-function",
685 insn->sec, insn->offset);
686 return -1;
687 }
688
dcc914f4 689 } else if (rela->sym->type == STT_SECTION) {
7acfe531
JP
690 insn->call_dest = find_func_by_offset(rela->sym->sec,
691 rela->addend+4);
692 if (!insn->call_dest) {
dcc914f4
JP
693 WARN_FUNC("can't find call dest symbol at %s+0x%x",
694 insn->sec, insn->offset,
695 rela->sym->sec->name,
696 rela->addend + 4);
697 return -1;
698 }
699 } else
700 insn->call_dest = rela->sym;
701 }
702
703 return 0;
704}
705
706/*
707 * The .alternatives section requires some extra special care, over and above
708 * what other special sections require:
709 *
710 * 1. Because alternatives are patched in-place, we need to insert a fake jump
711 * instruction at the end so that validate_branch() skips all the original
712 * replaced instructions when validating the new instruction path.
713 *
714 * 2. An added wrinkle is that the new instruction length might be zero. In
715 * that case the old instructions are replaced with noops. We simulate that
716 * by creating a fake jump as the only new instruction.
717 *
718 * 3. In some cases, the alternative section includes an instruction which
719 * conditionally jumps to the _end_ of the entry. We have to modify these
720 * jumps' destinations to point back to .text rather than the end of the
721 * entry in .altinstr_replacement.
dcc914f4
JP
722 */
723static int handle_group_alt(struct objtool_file *file,
724 struct special_alt *special_alt,
725 struct instruction *orig_insn,
726 struct instruction **new_insn)
727{
17bc3391 728 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
dcc914f4
JP
729 unsigned long dest_off;
730
731 last_orig_insn = NULL;
732 insn = orig_insn;
733 sec_for_each_insn_from(file, insn) {
734 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
735 break;
736
dcc914f4
JP
737 insn->alt_group = true;
738 last_orig_insn = insn;
739 }
740
17bc3391
JP
741 if (next_insn_same_sec(file, last_orig_insn)) {
742 fake_jump = malloc(sizeof(*fake_jump));
743 if (!fake_jump) {
744 WARN("malloc failed");
745 return -1;
746 }
747 memset(fake_jump, 0, sizeof(*fake_jump));
748 INIT_LIST_HEAD(&fake_jump->alts);
749 clear_insn_state(&fake_jump->state);
750
751 fake_jump->sec = special_alt->new_sec;
e6da9567 752 fake_jump->offset = FAKE_JUMP_OFFSET;
17bc3391
JP
753 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
754 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
e6da9567 755 fake_jump->func = orig_insn->func;
dcc914f4 756 }
dcc914f4
JP
757
758 if (!special_alt->new_len) {
17bc3391
JP
759 if (!fake_jump) {
760 WARN("%s: empty alternative at end of section",
761 special_alt->orig_sec->name);
762 return -1;
763 }
764
dcc914f4
JP
765 *new_insn = fake_jump;
766 return 0;
767 }
768
769 last_new_insn = NULL;
770 insn = *new_insn;
771 sec_for_each_insn_from(file, insn) {
772 if (insn->offset >= special_alt->new_off + special_alt->new_len)
773 break;
774
775 last_new_insn = insn;
776
a845c7cf 777 insn->ignore = orig_insn->ignore_alts;
a4d09dde 778 insn->func = orig_insn->func;
a845c7cf 779
dc419723
JP
780 /*
781 * Since alternative replacement code is copy/pasted by the
782 * kernel after applying relocations, generally such code can't
783 * have relative-address relocation references to outside the
784 * .altinstr_replacement section, unless the arch's
785 * alternatives code can adjust the relative offsets
786 * accordingly.
787 *
788 * The x86 alternatives code adjusts the offsets only when it
789 * encounters a branch instruction at the very beginning of the
790 * replacement group.
791 */
792 if ((insn->offset != special_alt->new_off ||
793 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
794 find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
795
796 WARN_FUNC("unsupported relocation in alternatives section",
797 insn->sec, insn->offset);
798 return -1;
799 }
800
a2296140 801 if (!is_static_jump(insn))
dcc914f4
JP
802 continue;
803
804 if (!insn->immediate)
805 continue;
806
807 dest_off = insn->offset + insn->len + insn->immediate;
17bc3391
JP
808 if (dest_off == special_alt->new_off + special_alt->new_len) {
809 if (!fake_jump) {
810 WARN("%s: alternative jump to end of section",
811 special_alt->orig_sec->name);
812 return -1;
813 }
dcc914f4 814 insn->jump_dest = fake_jump;
17bc3391 815 }
dcc914f4
JP
816
817 if (!insn->jump_dest) {
818 WARN_FUNC("can't find alternative jump destination",
819 insn->sec, insn->offset);
820 return -1;
821 }
822 }
823
824 if (!last_new_insn) {
825 WARN_FUNC("can't find last new alternative instruction",
826 special_alt->new_sec, special_alt->new_off);
827 return -1;
828 }
829
17bc3391
JP
830 if (fake_jump)
831 list_add(&fake_jump->list, &last_new_insn->list);
dcc914f4
JP
832
833 return 0;
834}
835
836/*
837 * A jump table entry can either convert a nop to a jump or a jump to a nop.
838 * If the original instruction is a jump, make the alt entry an effective nop
839 * by just skipping the original instruction.
840 */
841static int handle_jump_alt(struct objtool_file *file,
842 struct special_alt *special_alt,
843 struct instruction *orig_insn,
844 struct instruction **new_insn)
845{
846 if (orig_insn->type == INSN_NOP)
847 return 0;
848
849 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
850 WARN_FUNC("unsupported instruction at jump label",
851 orig_insn->sec, orig_insn->offset);
852 return -1;
853 }
854
855 *new_insn = list_next_entry(orig_insn, list);
856 return 0;
857}
858
859/*
860 * Read all the special sections which have alternate instructions which can be
861 * patched in or redirected to at runtime. Each instruction having alternate
862 * instruction(s) has them added to its insn->alts list, which will be
863 * traversed in validate_branch().
864 */
865static int add_special_section_alts(struct objtool_file *file)
866{
867 struct list_head special_alts;
868 struct instruction *orig_insn, *new_insn;
869 struct special_alt *special_alt, *tmp;
870 struct alternative *alt;
871 int ret;
872
873 ret = special_get_alts(file->elf, &special_alts);
874 if (ret)
875 return ret;
876
877 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
dcc914f4
JP
878
879 orig_insn = find_insn(file, special_alt->orig_sec,
880 special_alt->orig_off);
881 if (!orig_insn) {
882 WARN_FUNC("special: can't find orig instruction",
883 special_alt->orig_sec, special_alt->orig_off);
884 ret = -1;
885 goto out;
886 }
887
888 new_insn = NULL;
889 if (!special_alt->group || special_alt->new_len) {
890 new_insn = find_insn(file, special_alt->new_sec,
891 special_alt->new_off);
892 if (!new_insn) {
893 WARN_FUNC("special: can't find new instruction",
894 special_alt->new_sec,
895 special_alt->new_off);
896 ret = -1;
897 goto out;
898 }
899 }
900
901 if (special_alt->group) {
902 ret = handle_group_alt(file, special_alt, orig_insn,
903 &new_insn);
904 if (ret)
905 goto out;
906 } else if (special_alt->jump_or_nop) {
907 ret = handle_jump_alt(file, special_alt, orig_insn,
908 &new_insn);
909 if (ret)
910 goto out;
911 }
912
258c7605
JP
913 alt = malloc(sizeof(*alt));
914 if (!alt) {
915 WARN("malloc failed");
916 ret = -1;
917 goto out;
918 }
919
dcc914f4 920 alt->insn = new_insn;
764eef4b 921 alt->skip_orig = special_alt->skip_orig;
ea24213d 922 orig_insn->ignore_alts |= special_alt->skip_alt;
dcc914f4
JP
923 list_add_tail(&alt->list, &orig_insn->alts);
924
925 list_del(&special_alt->list);
926 free(special_alt);
927 }
928
929out:
930 return ret;
931}
932
e7c2bc37 933static int add_jump_table(struct objtool_file *file, struct instruction *insn,
bd98c813 934 struct rela *table)
dcc914f4
JP
935{
936 struct rela *rela = table;
e7c2bc37 937 struct instruction *dest_insn;
dcc914f4 938 struct alternative *alt;
fd35c88b
JP
939 struct symbol *pfunc = insn->func->pfunc;
940 unsigned int prev_offset = 0;
dcc914f4 941
e7c2bc37
JP
942 /*
943 * Each @rela is a switch table relocation which points to the target
944 * instruction.
945 */
946 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
bd98c813
JH
947
948 /* Check for the end of the table: */
949 if (rela != table && rela->jump_table_start)
dcc914f4
JP
950 break;
951
e7c2bc37 952 /* Make sure the table entries are consecutive: */
fd35c88b
JP
953 if (prev_offset && rela->offset != prev_offset + 8)
954 break;
955
956 /* Detect function pointers from contiguous objects: */
957 if (rela->sym->sec == pfunc->sec &&
958 rela->addend == pfunc->offset)
959 break;
960
e7c2bc37
JP
961 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
962 if (!dest_insn)
dcc914f4
JP
963 break;
964
e7c2bc37 965 /* Make sure the destination is in the same function: */
e65050b9 966 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
13810435 967 break;
dcc914f4
JP
968
969 alt = malloc(sizeof(*alt));
970 if (!alt) {
971 WARN("malloc failed");
972 return -1;
973 }
974
e7c2bc37 975 alt->insn = dest_insn;
dcc914f4 976 list_add_tail(&alt->list, &insn->alts);
fd35c88b
JP
977 prev_offset = rela->offset;
978 }
979
980 if (!prev_offset) {
981 WARN_FUNC("can't find switch jump table",
982 insn->sec, insn->offset);
983 return -1;
dcc914f4
JP
984 }
985
986 return 0;
987}
988
989/*
e7c2bc37 990 * find_jump_table() - Given a dynamic jump, find the switch jump table in
dcc914f4
JP
991 * .rodata associated with it.
992 *
993 * There are 3 basic patterns:
994 *
995 * 1. jmpq *[rodata addr](,%reg,8)
996 *
997 * This is the most common case by far. It jumps to an address in a simple
998 * jump table which is stored in .rodata.
999 *
1000 * 2. jmpq *[rodata addr](%rip)
1001 *
1002 * This is caused by a rare GCC quirk, currently only seen in three driver
1003 * functions in the kernel, only with certain obscure non-distro configs.
1004 *
1005 * As part of an optimization, GCC makes a copy of an existing switch jump
1006 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1007 * jump) to use a single entry in the table. The rest of the jump table and
1008 * some of its jump targets remain as dead code.
1009 *
1010 * In such a case we can just crudely ignore all unreachable instruction
1011 * warnings for the entire object file. Ideally we would just ignore them
1012 * for the function, but that would require redesigning the code quite a
1013 * bit. And honestly that's just not worth doing: unreachable instruction
1014 * warnings are of questionable value anyway, and this is such a rare issue.
1015 *
1016 * 3. mov [rodata addr],%reg1
1017 * ... some instructions ...
1018 * jmpq *(%reg1,%reg2,8)
1019 *
1020 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1021 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1022 *
99ce7962
PZ
1023 * As of GCC 7 there are quite a few more of these and the 'in between' code
1024 * is significant. Esp. with KASAN enabled some of the code between the mov
1025 * and jmpq uses .rodata itself, which can confuse things.
1026 *
dcc914f4
JP
1027 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1028 * ensure the same register is used in the mov and jump instructions.
99ce7962
PZ
1029 *
1030 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
dcc914f4 1031 */
e7c2bc37 1032static struct rela *find_jump_table(struct objtool_file *file,
dcc914f4
JP
1033 struct symbol *func,
1034 struct instruction *insn)
1035{
e7c2bc37 1036 struct rela *text_rela, *table_rela;
113d4bc9 1037 struct instruction *dest_insn, *orig_insn = insn;
e7c2bc37 1038 struct section *table_sec;
6f5ec299 1039 unsigned long table_offset;
dcc914f4 1040
99ce7962
PZ
1041 /*
1042 * Backward search using the @first_jump_src links, these help avoid
1043 * much of the 'in between' code. Which avoids us getting confused by
1044 * it.
1045 */
7dec80cc 1046 for (;
99ce7962
PZ
1047 &insn->list != &file->insn_list &&
1048 insn->sec == func->sec &&
1049 insn->offset >= func->offset;
1050
1051 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
1052
7dec80cc 1053 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
dcc914f4
JP
1054 break;
1055
1056 /* allow small jumps within the range */
1057 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1058 insn->jump_dest &&
1059 (insn->jump_dest->offset <= insn->offset ||
1060 insn->jump_dest->offset > orig_insn->offset))
1061 break;
1062
1063 /* look for a relocation which references .rodata */
1064 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
1065 insn->len);
4a60aa05
AX
1066 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1067 !text_rela->sym->sec->rodata)
dcc914f4
JP
1068 continue;
1069
6f5ec299 1070 table_offset = text_rela->addend;
e7c2bc37 1071 table_sec = text_rela->sym->sec;
4a60aa05 1072
6f5ec299
JP
1073 if (text_rela->type == R_X86_64_PC32)
1074 table_offset += 4;
1075
dcc914f4
JP
1076 /*
1077 * Make sure the .rodata address isn't associated with a
87b512de
JP
1078 * symbol. GCC jump tables are anonymous data.
1079 *
1080 * Also support C jump tables which are in the same format as
1081 * switch jump tables. For objtool to recognize them, they
1082 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1083 * have symbols associated with them.
dcc914f4 1084 */
e7c2bc37
JP
1085 if (find_symbol_containing(table_sec, table_offset) &&
1086 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1402fd8e
JP
1087 continue;
1088
113d4bc9
JP
1089 /*
1090 * Each table entry has a rela associated with it. The rela
1091 * should reference text in the same function as the original
1092 * instruction.
1093 */
e7c2bc37
JP
1094 table_rela = find_rela_by_dest(table_sec, table_offset);
1095 if (!table_rela)
1096 continue;
113d4bc9
JP
1097 dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
1098 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1099 continue;
7dec80cc 1100
e7c2bc37
JP
1101 /*
1102 * Use of RIP-relative switch jumps is quite rare, and
1103 * indicates a rare GCC quirk/bug which can leave dead code
1104 * behind.
1105 */
1106 if (text_rela->type == R_X86_64_PC32)
1107 file->ignore_unreachables = true;
1108
1109 return table_rela;
dcc914f4
JP
1110 }
1111
1112 return NULL;
1113}
1114
bd98c813
JH
1115/*
1116 * First pass: Mark the head of each jump table so that in the next pass,
1117 * we know when a given jump table ends and the next one starts.
1118 */
1119static void mark_func_jump_tables(struct objtool_file *file,
1120 struct symbol *func)
dcc914f4 1121{
bd98c813
JH
1122 struct instruction *insn, *last = NULL;
1123 struct rela *rela;
dcc914f4 1124
13810435 1125 func_for_each_insn_all(file, func, insn) {
99ce7962
PZ
1126 if (!last)
1127 last = insn;
1128
1129 /*
1130 * Store back-pointers for unconditional forward jumps such
e7c2bc37 1131 * that find_jump_table() can back-track using those and
99ce7962
PZ
1132 * avoid some potentially confusing code.
1133 */
1134 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1135 insn->offset > last->offset &&
1136 insn->jump_dest->offset > insn->offset &&
1137 !insn->jump_dest->first_jump_src) {
1138
1139 insn->jump_dest->first_jump_src = insn;
1140 last = insn->jump_dest;
1141 }
1142
dcc914f4
JP
1143 if (insn->type != INSN_JUMP_DYNAMIC)
1144 continue;
1145
e7c2bc37 1146 rela = find_jump_table(file, func, insn);
bd98c813
JH
1147 if (rela) {
1148 rela->jump_table_start = true;
1149 insn->jump_table = rela;
dcc914f4 1150 }
dcc914f4 1151 }
bd98c813
JH
1152}
1153
1154static int add_func_jump_tables(struct objtool_file *file,
1155 struct symbol *func)
1156{
1157 struct instruction *insn;
1158 int ret;
1159
1160 func_for_each_insn_all(file, func, insn) {
1161 if (!insn->jump_table)
1162 continue;
dcc914f4 1163
bd98c813 1164 ret = add_jump_table(file, insn, insn->jump_table);
dcc914f4
JP
1165 if (ret)
1166 return ret;
1167 }
1168
1169 return 0;
1170}
1171
1172/*
1173 * For some switch statements, gcc generates a jump table in the .rodata
1174 * section which contains a list of addresses within the function to jump to.
1175 * This finds these jump tables and adds them to the insn->alts lists.
1176 */
e7c2bc37 1177static int add_jump_table_alts(struct objtool_file *file)
dcc914f4
JP
1178{
1179 struct section *sec;
1180 struct symbol *func;
1181 int ret;
1182
4a60aa05 1183 if (!file->rodata)
dcc914f4
JP
1184 return 0;
1185
baa41469 1186 for_each_sec(file, sec) {
dcc914f4
JP
1187 list_for_each_entry(func, &sec->symbol_list, list) {
1188 if (func->type != STT_FUNC)
1189 continue;
1190
bd98c813 1191 mark_func_jump_tables(file, func);
e7c2bc37 1192 ret = add_func_jump_tables(file, func);
dcc914f4
JP
1193 if (ret)
1194 return ret;
1195 }
1196 }
1197
1198 return 0;
1199}
1200
39358a03
JP
1201static int read_unwind_hints(struct objtool_file *file)
1202{
1203 struct section *sec, *relasec;
1204 struct rela *rela;
1205 struct unwind_hint *hint;
1206 struct instruction *insn;
1207 struct cfi_reg *cfa;
1208 int i;
1209
1210 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1211 if (!sec)
1212 return 0;
1213
1214 relasec = sec->rela;
1215 if (!relasec) {
1216 WARN("missing .rela.discard.unwind_hints section");
1217 return -1;
1218 }
1219
1220 if (sec->len % sizeof(struct unwind_hint)) {
1221 WARN("struct unwind_hint size mismatch");
1222 return -1;
1223 }
1224
1225 file->hints = true;
1226
1227 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1228 hint = (struct unwind_hint *)sec->data->d_buf + i;
1229
1230 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1231 if (!rela) {
1232 WARN("can't find rela for unwind_hints[%d]", i);
1233 return -1;
1234 }
1235
1236 insn = find_insn(file, rela->sym->sec, rela->addend);
1237 if (!insn) {
1238 WARN("can't find insn for unwind_hints[%d]", i);
1239 return -1;
1240 }
1241
1242 cfa = &insn->state.cfa;
1243
1244 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1245 insn->save = true;
1246 continue;
1247
1248 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1249 insn->restore = true;
1250 insn->hint = true;
1251 continue;
1252 }
1253
1254 insn->hint = true;
1255
1256 switch (hint->sp_reg) {
1257 case ORC_REG_UNDEFINED:
1258 cfa->base = CFI_UNDEFINED;
1259 break;
1260 case ORC_REG_SP:
1261 cfa->base = CFI_SP;
1262 break;
1263 case ORC_REG_BP:
1264 cfa->base = CFI_BP;
1265 break;
1266 case ORC_REG_SP_INDIRECT:
1267 cfa->base = CFI_SP_INDIRECT;
1268 break;
1269 case ORC_REG_R10:
1270 cfa->base = CFI_R10;
1271 break;
1272 case ORC_REG_R13:
1273 cfa->base = CFI_R13;
1274 break;
1275 case ORC_REG_DI:
1276 cfa->base = CFI_DI;
1277 break;
1278 case ORC_REG_DX:
1279 cfa->base = CFI_DX;
1280 break;
1281 default:
1282 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1283 insn->sec, insn->offset, hint->sp_reg);
1284 return -1;
1285 }
1286
1287 cfa->offset = hint->sp_offset;
1288 insn->state.type = hint->type;
d31a5802 1289 insn->state.end = hint->end;
39358a03
JP
1290 }
1291
1292 return 0;
1293}
1294
b5bc2231
PZ
1295static int read_retpoline_hints(struct objtool_file *file)
1296{
63474dc4 1297 struct section *sec;
b5bc2231
PZ
1298 struct instruction *insn;
1299 struct rela *rela;
b5bc2231 1300
63474dc4 1301 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
b5bc2231
PZ
1302 if (!sec)
1303 return 0;
1304
63474dc4
JP
1305 list_for_each_entry(rela, &sec->rela_list, list) {
1306 if (rela->sym->type != STT_SECTION) {
1307 WARN("unexpected relocation symbol type in %s", sec->name);
b5bc2231
PZ
1308 return -1;
1309 }
1310
1311 insn = find_insn(file, rela->sym->sec, rela->addend);
1312 if (!insn) {
63474dc4 1313 WARN("bad .discard.retpoline_safe entry");
b5bc2231
PZ
1314 return -1;
1315 }
1316
1317 if (insn->type != INSN_JUMP_DYNAMIC &&
1318 insn->type != INSN_CALL_DYNAMIC) {
63474dc4 1319 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
b5bc2231
PZ
1320 insn->sec, insn->offset);
1321 return -1;
1322 }
1323
1324 insn->retpoline_safe = true;
1325 }
1326
1327 return 0;
1328}
1329
4a60aa05
AX
1330static void mark_rodata(struct objtool_file *file)
1331{
1332 struct section *sec;
1333 bool found = false;
1334
1335 /*
87b512de
JP
1336 * Search for the following rodata sections, each of which can
1337 * potentially contain jump tables:
1338 *
1339 * - .rodata: can contain GCC switch tables
1340 * - .rodata.<func>: same, if -fdata-sections is being used
1341 * - .rodata..c_jump_table: contains C annotated jump tables
1342 *
1343 * .rodata.str1.* sections are ignored; they don't contain jump tables.
4a60aa05
AX
1344 */
1345 for_each_sec(file, sec) {
87b512de
JP
1346 if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
1347 !strcmp(sec->name, C_JUMP_TABLE_SECTION)) {
4a60aa05
AX
1348 sec->rodata = true;
1349 found = true;
1350 }
1351 }
1352
1353 file->rodata = found;
1354}
1355
dcc914f4
JP
1356static int decode_sections(struct objtool_file *file)
1357{
1358 int ret;
1359
4a60aa05
AX
1360 mark_rodata(file);
1361
dcc914f4
JP
1362 ret = decode_instructions(file);
1363 if (ret)
1364 return ret;
1365
1366 ret = add_dead_ends(file);
1367 if (ret)
1368 return ret;
1369
1370 add_ignores(file);
ea24213d 1371 add_uaccess_safe(file);
dcc914f4 1372
ff05ab23 1373 ret = add_ignore_alternatives(file);
258c7605
JP
1374 if (ret)
1375 return ret;
1376
dcc914f4
JP
1377 ret = add_jump_destinations(file);
1378 if (ret)
1379 return ret;
1380
a845c7cf 1381 ret = add_special_section_alts(file);
dcc914f4
JP
1382 if (ret)
1383 return ret;
1384
a845c7cf 1385 ret = add_call_destinations(file);
dcc914f4
JP
1386 if (ret)
1387 return ret;
1388
e7c2bc37 1389 ret = add_jump_table_alts(file);
dcc914f4
JP
1390 if (ret)
1391 return ret;
1392
39358a03
JP
1393 ret = read_unwind_hints(file);
1394 if (ret)
1395 return ret;
1396
b5bc2231
PZ
1397 ret = read_retpoline_hints(file);
1398 if (ret)
1399 return ret;
1400
dcc914f4
JP
1401 return 0;
1402}
1403
1404static bool is_fentry_call(struct instruction *insn)
1405{
1406 if (insn->type == INSN_CALL &&
1407 insn->call_dest->type == STT_NOTYPE &&
1408 !strcmp(insn->call_dest->name, "__fentry__"))
1409 return true;
1410
1411 return false;
1412}
1413
baa41469 1414static bool has_modified_stack_frame(struct insn_state *state)
dcc914f4 1415{
baa41469
JP
1416 int i;
1417
1418 if (state->cfa.base != initial_func_cfi.cfa.base ||
1419 state->cfa.offset != initial_func_cfi.cfa.offset ||
1420 state->stack_size != initial_func_cfi.cfa.offset ||
1421 state->drap)
1422 return true;
1423
1424 for (i = 0; i < CFI_NUM_REGS; i++)
1425 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1426 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1427 return true;
1428
1429 return false;
1430}
1431
1432static bool has_valid_stack_frame(struct insn_state *state)
1433{
1434 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1435 state->regs[CFI_BP].offset == -16)
1436 return true;
1437
1438 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1439 return true;
1440
1441 return false;
dcc914f4
JP
1442}
1443
627fce14
JP
1444static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1445{
1446 struct cfi_reg *cfa = &state->cfa;
1447 struct stack_op *op = &insn->stack_op;
1448
1449 if (cfa->base != CFI_SP)
1450 return 0;
1451
1452 /* push */
ea24213d 1453 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
627fce14
JP
1454 cfa->offset += 8;
1455
1456 /* pop */
ea24213d 1457 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
627fce14
JP
1458 cfa->offset -= 8;
1459
1460 /* add immediate to sp */
1461 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1462 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1463 cfa->offset -= op->src.offset;
1464
1465 return 0;
1466}
1467
baa41469
JP
1468static void save_reg(struct insn_state *state, unsigned char reg, int base,
1469 int offset)
dcc914f4 1470{
bf4d1a83 1471 if (arch_callee_saved_reg(reg) &&
baa41469
JP
1472 state->regs[reg].base == CFI_UNDEFINED) {
1473 state->regs[reg].base = base;
1474 state->regs[reg].offset = offset;
1475 }
dcc914f4
JP
1476}
1477
baa41469 1478static void restore_reg(struct insn_state *state, unsigned char reg)
dcc914f4 1479{
baa41469
JP
1480 state->regs[reg].base = CFI_UNDEFINED;
1481 state->regs[reg].offset = 0;
1482}
1483
1484/*
1485 * A note about DRAP stack alignment:
1486 *
1487 * GCC has the concept of a DRAP register, which is used to help keep track of
1488 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1489 * register. The typical DRAP pattern is:
1490 *
1491 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1492 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1493 * 41 ff 72 f8 pushq -0x8(%r10)
1494 * 55 push %rbp
1495 * 48 89 e5 mov %rsp,%rbp
1496 * (more pushes)
1497 * 41 52 push %r10
1498 * ...
1499 * 41 5a pop %r10
1500 * (more pops)
1501 * 5d pop %rbp
1502 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1503 * c3 retq
1504 *
1505 * There are some variations in the epilogues, like:
1506 *
1507 * 5b pop %rbx
1508 * 41 5a pop %r10
1509 * 41 5c pop %r12
1510 * 41 5d pop %r13
1511 * 41 5e pop %r14
1512 * c9 leaveq
1513 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1514 * c3 retq
1515 *
1516 * and:
1517 *
1518 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1519 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1520 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1521 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1522 * c9 leaveq
1523 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1524 * c3 retq
1525 *
1526 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1527 * restored beforehand:
1528 *
1529 * 41 55 push %r13
1530 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1531 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1532 * ...
1533 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1534 * 41 5d pop %r13
1535 * c3 retq
1536 */
1537static int update_insn_state(struct instruction *insn, struct insn_state *state)
1538{
1539 struct stack_op *op = &insn->stack_op;
1540 struct cfi_reg *cfa = &state->cfa;
1541 struct cfi_reg *regs = state->regs;
1542
1543 /* stack operations don't make sense with an undefined CFA */
1544 if (cfa->base == CFI_UNDEFINED) {
1545 if (insn->func) {
1546 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1547 return -1;
1548 }
1549 return 0;
1550 }
1551
627fce14
JP
1552 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1553 return update_insn_state_regs(insn, state);
1554
baa41469
JP
1555 switch (op->dest.type) {
1556
1557 case OP_DEST_REG:
1558 switch (op->src.type) {
1559
1560 case OP_SRC_REG:
0d0970ee
JP
1561 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1562 cfa->base == CFI_SP &&
1563 regs[CFI_BP].base == CFI_CFA &&
1564 regs[CFI_BP].offset == -cfa->offset) {
1565
1566 /* mov %rsp, %rbp */
1567 cfa->base = op->dest.reg;
1568 state->bp_scratch = false;
1569 }
dd88a0a0 1570
0d0970ee
JP
1571 else if (op->src.reg == CFI_SP &&
1572 op->dest.reg == CFI_BP && state->drap) {
dd88a0a0 1573
0d0970ee
JP
1574 /* drap: mov %rsp, %rbp */
1575 regs[CFI_BP].base = CFI_BP;
1576 regs[CFI_BP].offset = -state->stack_size;
1577 state->bp_scratch = false;
1578 }
dd88a0a0 1579
0d0970ee
JP
1580 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1581
1582 /*
1583 * mov %rsp, %reg
1584 *
1585 * This is needed for the rare case where GCC
1586 * does:
1587 *
1588 * mov %rsp, %rax
1589 * ...
1590 * mov %rax, %rsp
1591 */
1592 state->vals[op->dest.reg].base = CFI_CFA;
1593 state->vals[op->dest.reg].offset = -state->stack_size;
dd88a0a0
JP
1594 }
1595
3c1f0583
JP
1596 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1597 cfa->base == CFI_BP) {
1598
1599 /*
1600 * mov %rbp, %rsp
1601 *
1602 * Restore the original stack pointer (Clang).
1603 */
1604 state->stack_size = -state->regs[CFI_BP].offset;
1605 }
1606
dd88a0a0
JP
1607 else if (op->dest.reg == cfa->base) {
1608
1609 /* mov %reg, %rsp */
1610 if (cfa->base == CFI_SP &&
1611 state->vals[op->src.reg].base == CFI_CFA) {
1612
1613 /*
1614 * This is needed for the rare case
1615 * where GCC does something dumb like:
1616 *
1617 * lea 0x8(%rsp), %rcx
1618 * ...
1619 * mov %rcx, %rsp
1620 */
1621 cfa->offset = -state->vals[op->src.reg].offset;
1622 state->stack_size = cfa->offset;
1623
1624 } else {
1625 cfa->base = CFI_UNDEFINED;
1626 cfa->offset = 0;
1627 }
baa41469
JP
1628 }
1629
1630 break;
1631
1632 case OP_SRC_ADD:
1633 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1634
1635 /* add imm, %rsp */
1636 state->stack_size -= op->src.offset;
1637 if (cfa->base == CFI_SP)
1638 cfa->offset -= op->src.offset;
1639 break;
1640 }
1641
1642 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1643
1644 /* lea disp(%rbp), %rsp */
1645 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1646 break;
1647 }
1648
dd88a0a0 1649 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
baa41469
JP
1650
1651 /* drap: lea disp(%rsp), %drap */
1652 state->drap_reg = op->dest.reg;
dd88a0a0
JP
1653
1654 /*
1655 * lea disp(%rsp), %reg
1656 *
1657 * This is needed for the rare case where GCC
1658 * does something dumb like:
1659 *
1660 * lea 0x8(%rsp), %rcx
1661 * ...
1662 * mov %rcx, %rsp
1663 */
1664 state->vals[op->dest.reg].base = CFI_CFA;
1665 state->vals[op->dest.reg].offset = \
1666 -state->stack_size + op->src.offset;
1667
baa41469
JP
1668 break;
1669 }
1670
1671 if (state->drap && op->dest.reg == CFI_SP &&
1672 op->src.reg == state->drap_reg) {
1673
1674 /* drap: lea disp(%drap), %rsp */
1675 cfa->base = CFI_SP;
1676 cfa->offset = state->stack_size = -op->src.offset;
1677 state->drap_reg = CFI_UNDEFINED;
1678 state->drap = false;
1679 break;
1680 }
1681
1682 if (op->dest.reg == state->cfa.base) {
1683 WARN_FUNC("unsupported stack register modification",
1684 insn->sec, insn->offset);
1685 return -1;
1686 }
1687
1688 break;
1689
1690 case OP_SRC_AND:
1691 if (op->dest.reg != CFI_SP ||
1692 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1693 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1694 WARN_FUNC("unsupported stack pointer realignment",
1695 insn->sec, insn->offset);
1696 return -1;
1697 }
1698
1699 if (state->drap_reg != CFI_UNDEFINED) {
1700 /* drap: and imm, %rsp */
1701 cfa->base = state->drap_reg;
1702 cfa->offset = state->stack_size = 0;
1703 state->drap = true;
baa41469
JP
1704 }
1705
1706 /*
1707 * Older versions of GCC (4.8ish) realign the stack
1708 * without DRAP, with a frame pointer.
1709 */
1710
1711 break;
1712
1713 case OP_SRC_POP:
ea24213d 1714 case OP_SRC_POPF:
baa41469
JP
1715 if (!state->drap && op->dest.type == OP_DEST_REG &&
1716 op->dest.reg == cfa->base) {
1717
1718 /* pop %rbp */
1719 cfa->base = CFI_SP;
1720 }
1721
bf4d1a83
JP
1722 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1723 op->dest.type == OP_DEST_REG &&
1724 op->dest.reg == state->drap_reg &&
1725 state->drap_offset == -state->stack_size) {
baa41469 1726
bf4d1a83
JP
1727 /* drap: pop %drap */
1728 cfa->base = state->drap_reg;
1729 cfa->offset = 0;
1730 state->drap_offset = -1;
baa41469 1731
bf4d1a83 1732 } else if (regs[op->dest.reg].offset == -state->stack_size) {
baa41469 1733
bf4d1a83 1734 /* pop %reg */
baa41469
JP
1735 restore_reg(state, op->dest.reg);
1736 }
1737
1738 state->stack_size -= 8;
1739 if (cfa->base == CFI_SP)
1740 cfa->offset -= 8;
1741
1742 break;
1743
1744 case OP_SRC_REG_INDIRECT:
bf4d1a83
JP
1745 if (state->drap && op->src.reg == CFI_BP &&
1746 op->src.offset == state->drap_offset) {
1747
1748 /* drap: mov disp(%rbp), %drap */
1749 cfa->base = state->drap_reg;
1750 cfa->offset = 0;
1751 state->drap_offset = -1;
1752 }
1753
baa41469
JP
1754 if (state->drap && op->src.reg == CFI_BP &&
1755 op->src.offset == regs[op->dest.reg].offset) {
1756
1757 /* drap: mov disp(%rbp), %reg */
baa41469
JP
1758 restore_reg(state, op->dest.reg);
1759
1760 } else if (op->src.reg == cfa->base &&
1761 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1762
1763 /* mov disp(%rbp), %reg */
1764 /* mov disp(%rsp), %reg */
1765 restore_reg(state, op->dest.reg);
1766 }
1767
1768 break;
1769
1770 default:
1771 WARN_FUNC("unknown stack-related instruction",
1772 insn->sec, insn->offset);
1773 return -1;
1774 }
1775
1776 break;
1777
1778 case OP_DEST_PUSH:
ea24213d 1779 case OP_DEST_PUSHF:
baa41469
JP
1780 state->stack_size += 8;
1781 if (cfa->base == CFI_SP)
1782 cfa->offset += 8;
1783
1784 if (op->src.type != OP_SRC_REG)
1785 break;
1786
1787 if (state->drap) {
1788 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1789
1790 /* drap: push %drap */
1791 cfa->base = CFI_BP_INDIRECT;
1792 cfa->offset = -state->stack_size;
1793
bf4d1a83
JP
1794 /* save drap so we know when to restore it */
1795 state->drap_offset = -state->stack_size;
baa41469
JP
1796
1797 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1798
1799 /* drap: push %rbp */
1800 state->stack_size = 0;
1801
1802 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1803
1804 /* drap: push %reg */
1805 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1806 }
1807
1808 } else {
1809
1810 /* push %reg */
1811 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1812 }
1813
1814 /* detect when asm code uses rbp as a scratch register */
867ac9d7 1815 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
baa41469
JP
1816 cfa->base != CFI_BP)
1817 state->bp_scratch = true;
1818 break;
1819
1820 case OP_DEST_REG_INDIRECT:
1821
1822 if (state->drap) {
1823 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1824
1825 /* drap: mov %drap, disp(%rbp) */
1826 cfa->base = CFI_BP_INDIRECT;
1827 cfa->offset = op->dest.offset;
1828
bf4d1a83
JP
1829 /* save drap offset so we know when to restore it */
1830 state->drap_offset = op->dest.offset;
baa41469
JP
1831 }
1832
1833 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1834
1835 /* drap: mov reg, disp(%rbp) */
1836 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1837 }
1838
1839 } else if (op->dest.reg == cfa->base) {
1840
1841 /* mov reg, disp(%rbp) */
1842 /* mov reg, disp(%rsp) */
1843 save_reg(state, op->src.reg, CFI_CFA,
1844 op->dest.offset - state->cfa.offset);
1845 }
1846
1847 break;
1848
1849 case OP_DEST_LEAVE:
1850 if ((!state->drap && cfa->base != CFI_BP) ||
1851 (state->drap && cfa->base != state->drap_reg)) {
1852 WARN_FUNC("leave instruction with modified stack frame",
1853 insn->sec, insn->offset);
1854 return -1;
1855 }
1856
1857 /* leave (mov %rbp, %rsp; pop %rbp) */
1858
1859 state->stack_size = -state->regs[CFI_BP].offset - 8;
1860 restore_reg(state, CFI_BP);
1861
1862 if (!state->drap) {
1863 cfa->base = CFI_SP;
1864 cfa->offset -= 8;
1865 }
1866
1867 break;
1868
1869 case OP_DEST_MEM:
ea24213d 1870 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
baa41469
JP
1871 WARN_FUNC("unknown stack-related memory operation",
1872 insn->sec, insn->offset);
1873 return -1;
1874 }
1875
1876 /* pop mem */
1877 state->stack_size -= 8;
1878 if (cfa->base == CFI_SP)
1879 cfa->offset -= 8;
1880
1881 break;
1882
1883 default:
1884 WARN_FUNC("unknown stack-related instruction",
1885 insn->sec, insn->offset);
1886 return -1;
1887 }
1888
1889 return 0;
1890}
1891
1892static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1893{
1894 struct insn_state *state1 = &insn->state, *state2 = state;
1895 int i;
1896
1897 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1898 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1899 insn->sec, insn->offset,
1900 state1->cfa.base, state1->cfa.offset,
1901 state2->cfa.base, state2->cfa.offset);
1902
1903 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1904 for (i = 0; i < CFI_NUM_REGS; i++) {
1905 if (!memcmp(&state1->regs[i], &state2->regs[i],
1906 sizeof(struct cfi_reg)))
1907 continue;
1908
1909 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1910 insn->sec, insn->offset,
1911 i, state1->regs[i].base, state1->regs[i].offset,
1912 i, state2->regs[i].base, state2->regs[i].offset);
1913 break;
1914 }
1915
627fce14
JP
1916 } else if (state1->type != state2->type) {
1917 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1918 insn->sec, insn->offset, state1->type, state2->type);
1919
baa41469 1920 } else if (state1->drap != state2->drap ||
bf4d1a83
JP
1921 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1922 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1923 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
baa41469 1924 insn->sec, insn->offset,
bf4d1a83
JP
1925 state1->drap, state1->drap_reg, state1->drap_offset,
1926 state2->drap, state2->drap_reg, state2->drap_offset);
baa41469
JP
1927
1928 } else
1929 return true;
1930
1931 return false;
dcc914f4
JP
1932}
1933
ea24213d
PZ
1934static inline bool func_uaccess_safe(struct symbol *func)
1935{
1936 if (func)
e10cd8fe 1937 return func->uaccess_safe;
ea24213d
PZ
1938
1939 return false;
1940}
1941
0c1ddd33 1942static inline const char *call_dest_name(struct instruction *insn)
ea24213d
PZ
1943{
1944 if (insn->call_dest)
1945 return insn->call_dest->name;
1946
1947 return "{dynamic}";
1948}
1949
1950static int validate_call(struct instruction *insn, struct insn_state *state)
1951{
1952 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1953 WARN_FUNC("call to %s() with UACCESS enabled",
0c1ddd33 1954 insn->sec, insn->offset, call_dest_name(insn));
ea24213d
PZ
1955 return 1;
1956 }
1957
2f0f9e9a
PZ
1958 if (state->df) {
1959 WARN_FUNC("call to %s() with DF set",
0c1ddd33 1960 insn->sec, insn->offset, call_dest_name(insn));
2f0f9e9a
PZ
1961 return 1;
1962 }
1963
ea24213d
PZ
1964 return 0;
1965}
1966
54262aa2
PZ
1967static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1968{
1969 if (has_modified_stack_frame(state)) {
1970 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1971 insn->sec, insn->offset);
1972 return 1;
1973 }
1974
ea24213d 1975 return validate_call(insn, state);
54262aa2
PZ
1976}
1977
a92e92d1
PZ
1978static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
1979{
1980 if (state->uaccess && !func_uaccess_safe(func)) {
1981 WARN_FUNC("return with UACCESS enabled",
1982 insn->sec, insn->offset);
1983 return 1;
1984 }
1985
1986 if (!state->uaccess && func_uaccess_safe(func)) {
1987 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
1988 insn->sec, insn->offset);
1989 return 1;
1990 }
1991
1992 if (state->df) {
1993 WARN_FUNC("return with DF set",
1994 insn->sec, insn->offset);
1995 return 1;
1996 }
1997
1998 if (func && has_modified_stack_frame(state)) {
1999 WARN_FUNC("return with modified stack frame",
2000 insn->sec, insn->offset);
2001 return 1;
2002 }
2003
2004 if (state->bp_scratch) {
2005 WARN("%s uses BP as a scratch register",
2006 func->name);
2007 return 1;
2008 }
2009
2010 return 0;
2011}
2012
dcc914f4
JP
2013/*
2014 * Follow the branch starting at the given instruction, and recursively follow
2015 * any other branches (jumps). Meanwhile, track the frame pointer state at
2016 * each instruction and validate all the rules described in
2017 * tools/objtool/Documentation/stack-validation.txt.
2018 */
c705cecc
JP
2019static int validate_branch(struct objtool_file *file, struct symbol *func,
2020 struct instruction *first, struct insn_state state)
dcc914f4
JP
2021{
2022 struct alternative *alt;
39358a03 2023 struct instruction *insn, *next_insn;
dcc914f4 2024 struct section *sec;
882a0db9 2025 u8 visited;
dcc914f4
JP
2026 int ret;
2027
2028 insn = first;
2029 sec = insn->sec;
dcc914f4
JP
2030
2031 if (insn->alt_group && list_empty(&insn->alts)) {
2032 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
2033 sec, insn->offset);
12b25729 2034 return 1;
dcc914f4
JP
2035 }
2036
2037 while (1) {
39358a03
JP
2038 next_insn = next_insn_same_sec(file, insn);
2039
13810435 2040 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
ee97638b
JP
2041 WARN("%s() falls through to next function %s()",
2042 func->name, insn->func->name);
2043 return 1;
dcc914f4
JP
2044 }
2045
4855022a
JP
2046 if (func && insn->ignore) {
2047 WARN_FUNC("BUG: why am I validating an ignored function?",
2048 sec, insn->offset);
12b25729 2049 return 1;
4855022a
JP
2050 }
2051
882a0db9 2052 visited = 1 << state.uaccess;
dcc914f4 2053 if (insn->visited) {
39358a03 2054 if (!insn->hint && !insn_state_match(insn, &state))
dcc914f4 2055 return 1;
dcc914f4 2056
882a0db9 2057 if (insn->visited & visited)
ea24213d 2058 return 0;
dcc914f4
JP
2059 }
2060
39358a03
JP
2061 if (insn->hint) {
2062 if (insn->restore) {
2063 struct instruction *save_insn, *i;
2064
2065 i = insn;
2066 save_insn = NULL;
c705cecc 2067 func_for_each_insn_continue_reverse(file, func, i) {
39358a03
JP
2068 if (i->save) {
2069 save_insn = i;
2070 break;
2071 }
2072 }
2073
2074 if (!save_insn) {
2075 WARN_FUNC("no corresponding CFI save for CFI restore",
2076 sec, insn->offset);
2077 return 1;
2078 }
2079
2080 if (!save_insn->visited) {
2081 /*
2082 * Oops, no state to copy yet.
2083 * Hopefully we can reach this
2084 * instruction from another branch
2085 * after the save insn has been
2086 * visited.
2087 */
2088 if (insn == first)
2089 return 0;
2090
2091 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2092 sec, insn->offset);
2093 return 1;
2094 }
2095
2096 insn->state = save_insn->state;
2097 }
2098
2099 state = insn->state;
2100
2101 } else
2102 insn->state = state;
dcc914f4 2103
882a0db9 2104 insn->visited |= visited;
baa41469 2105
a845c7cf 2106 if (!insn->ignore_alts) {
764eef4b
PZ
2107 bool skip_orig = false;
2108
a845c7cf 2109 list_for_each_entry(alt, &insn->alts, list) {
764eef4b
PZ
2110 if (alt->skip_orig)
2111 skip_orig = true;
2112
c705cecc 2113 ret = validate_branch(file, func, alt->insn, state);
7697eee3
PZ
2114 if (ret) {
2115 if (backtrace)
2116 BT_FUNC("(alt)", insn);
2117 return ret;
2118 }
a845c7cf 2119 }
764eef4b
PZ
2120
2121 if (skip_orig)
2122 return 0;
dcc914f4
JP
2123 }
2124
2125 switch (insn->type) {
2126
dcc914f4 2127 case INSN_RETURN:
a92e92d1 2128 return validate_return(func, insn, &state);
dcc914f4
JP
2129
2130 case INSN_CALL:
ea24213d
PZ
2131 case INSN_CALL_DYNAMIC:
2132 ret = validate_call(insn, &state);
2133 if (ret)
2134 return ret;
dcc914f4 2135
c9bab22b
JP
2136 if (!no_fp && func && !is_fentry_call(insn) &&
2137 !has_valid_stack_frame(&state)) {
dcc914f4
JP
2138 WARN_FUNC("call without frame pointer save/setup",
2139 sec, insn->offset);
2140 return 1;
2141 }
c9bab22b
JP
2142
2143 if (dead_end_function(file, insn->call_dest))
2144 return 0;
2145
dcc914f4
JP
2146 break;
2147
2148 case INSN_JUMP_CONDITIONAL:
2149 case INSN_JUMP_UNCONDITIONAL:
0c1ddd33 2150 if (func && is_sibling_call(insn)) {
54262aa2 2151 ret = validate_sibling_call(insn, &state);
dcc914f4 2152 if (ret)
54262aa2 2153 return ret;
4855022a 2154
0c1ddd33 2155 } else if (insn->jump_dest) {
c705cecc
JP
2156 ret = validate_branch(file, func,
2157 insn->jump_dest, state);
7697eee3
PZ
2158 if (ret) {
2159 if (backtrace)
2160 BT_FUNC("(branch)", insn);
2161 return ret;
2162 }
4855022a 2163 }
dcc914f4
JP
2164
2165 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2166 return 0;
2167
2168 break;
2169
2170 case INSN_JUMP_DYNAMIC:
b68b9907 2171 case INSN_JUMP_DYNAMIC_CONDITIONAL:
0c1ddd33 2172 if (func && is_sibling_call(insn)) {
54262aa2
PZ
2173 ret = validate_sibling_call(insn, &state);
2174 if (ret)
2175 return ret;
dcc914f4
JP
2176 }
2177
b68b9907
JP
2178 if (insn->type == INSN_JUMP_DYNAMIC)
2179 return 0;
2180
2181 break;
dcc914f4 2182
39358a03
JP
2183 case INSN_CONTEXT_SWITCH:
2184 if (func && (!next_insn || !next_insn->hint)) {
2185 WARN_FUNC("unsupported instruction in callable function",
2186 sec, insn->offset);
2187 return 1;
2188 }
2189 return 0;
2190
baa41469
JP
2191 case INSN_STACK:
2192 if (update_insn_state(insn, &state))
12b25729 2193 return 1;
baa41469 2194
ea24213d
PZ
2195 if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2196 if (!state.uaccess_stack) {
2197 state.uaccess_stack = 1;
2198 } else if (state.uaccess_stack >> 31) {
2199 WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2200 return 1;
2201 }
2202 state.uaccess_stack <<= 1;
2203 state.uaccess_stack |= state.uaccess;
2204 }
2205
2206 if (insn->stack_op.src.type == OP_SRC_POPF) {
2207 if (state.uaccess_stack) {
2208 state.uaccess = state.uaccess_stack & 1;
2209 state.uaccess_stack >>= 1;
2210 if (state.uaccess_stack == 1)
2211 state.uaccess_stack = 0;
2212 }
2213 }
2214
2215 break;
2216
2217 case INSN_STAC:
2218 if (state.uaccess) {
2219 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2220 return 1;
2221 }
2222
2223 state.uaccess = true;
2224 break;
2225
2226 case INSN_CLAC:
c705cecc 2227 if (!state.uaccess && func) {
ea24213d
PZ
2228 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2229 return 1;
2230 }
2231
2232 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2233 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2234 return 1;
2235 }
2236
2237 state.uaccess = false;
baa41469
JP
2238 break;
2239
2f0f9e9a
PZ
2240 case INSN_STD:
2241 if (state.df)
2242 WARN_FUNC("recursive STD", sec, insn->offset);
2243
2244 state.df = true;
2245 break;
2246
2247 case INSN_CLD:
c705cecc 2248 if (!state.df && func)
2f0f9e9a
PZ
2249 WARN_FUNC("redundant CLD", sec, insn->offset);
2250
2251 state.df = false;
baa41469
JP
2252 break;
2253
dcc914f4
JP
2254 default:
2255 break;
2256 }
2257
2258 if (insn->dead_end)
2259 return 0;
2260
00d96180
JP
2261 if (!next_insn) {
2262 if (state.cfa.base == CFI_UNDEFINED)
2263 return 0;
dcc914f4
JP
2264 WARN("%s: unexpected end of section", sec->name);
2265 return 1;
2266 }
00d96180
JP
2267
2268 insn = next_insn;
dcc914f4
JP
2269 }
2270
2271 return 0;
2272}
2273
39358a03
JP
2274static int validate_unwind_hints(struct objtool_file *file)
2275{
2276 struct instruction *insn;
2277 int ret, warnings = 0;
2278 struct insn_state state;
2279
2280 if (!file->hints)
2281 return 0;
2282
2283 clear_insn_state(&state);
2284
2285 for_each_insn(file, insn) {
2286 if (insn->hint && !insn->visited) {
c705cecc 2287 ret = validate_branch(file, insn->func, insn, state);
7697eee3
PZ
2288 if (ret && backtrace)
2289 BT_FUNC("<=== (hint)", insn);
39358a03
JP
2290 warnings += ret;
2291 }
2292 }
2293
2294 return warnings;
2295}
2296
b5bc2231
PZ
2297static int validate_retpoline(struct objtool_file *file)
2298{
2299 struct instruction *insn;
2300 int warnings = 0;
2301
2302 for_each_insn(file, insn) {
2303 if (insn->type != INSN_JUMP_DYNAMIC &&
2304 insn->type != INSN_CALL_DYNAMIC)
2305 continue;
2306
2307 if (insn->retpoline_safe)
2308 continue;
2309
ca41b97e
PZ
2310 /*
2311 * .init.text code is ran before userspace and thus doesn't
2312 * strictly need retpolines, except for modules which are
2313 * loaded late, they very much do need retpoline in their
2314 * .init.text
2315 */
2316 if (!strcmp(insn->sec->name, ".init.text") && !module)
2317 continue;
2318
b5bc2231
PZ
2319 WARN_FUNC("indirect %s found in RETPOLINE build",
2320 insn->sec, insn->offset,
2321 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2322
2323 warnings++;
2324 }
2325
2326 return warnings;
2327}
2328
dcc914f4
JP
2329static bool is_kasan_insn(struct instruction *insn)
2330{
2331 return (insn->type == INSN_CALL &&
2332 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2333}
2334
2335static bool is_ubsan_insn(struct instruction *insn)
2336{
2337 return (insn->type == INSN_CALL &&
2338 !strcmp(insn->call_dest->name,
2339 "__ubsan_handle_builtin_unreachable"));
2340}
2341
baa41469 2342static bool ignore_unreachable_insn(struct instruction *insn)
dcc914f4
JP
2343{
2344 int i;
2345
baa41469
JP
2346 if (insn->ignore || insn->type == INSN_NOP)
2347 return true;
2348
2349 /*
2350 * Ignore any unused exceptions. This can happen when a whitelisted
2351 * function has an exception table entry.
0e2bb2bc
JP
2352 *
2353 * Also ignore alternative replacement instructions. This can happen
2354 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 2355 */
0e2bb2bc
JP
2356 if (!strcmp(insn->sec->name, ".fixup") ||
2357 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2358 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
2359 return true;
2360
2361 /*
2362 * Check if this (or a subsequent) instruction is related to
2363 * CONFIG_UBSAN or CONFIG_KASAN.
2364 *
2365 * End the search at 5 instructions to avoid going into the weeds.
2366 */
baa41469
JP
2367 if (!insn->func)
2368 return false;
dcc914f4
JP
2369 for (i = 0; i < 5; i++) {
2370
2371 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2372 return true;
2373
fe24e271
JP
2374 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2375 if (insn->jump_dest &&
2376 insn->jump_dest->func == insn->func) {
2377 insn = insn->jump_dest;
2378 continue;
2379 }
2380
2381 break;
dcc914f4
JP
2382 }
2383
baa41469 2384 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
dcc914f4 2385 break;
fe24e271 2386
dcc914f4
JP
2387 insn = list_next_entry(insn, list);
2388 }
2389
2390 return false;
2391}
2392
2393static int validate_functions(struct objtool_file *file)
2394{
2395 struct section *sec;
2396 struct symbol *func;
2397 struct instruction *insn;
baa41469 2398 struct insn_state state;
dcc914f4
JP
2399 int ret, warnings = 0;
2400
baa41469
JP
2401 clear_insn_state(&state);
2402
2403 state.cfa = initial_func_cfi.cfa;
2404 memcpy(&state.regs, &initial_func_cfi.regs,
2405 CFI_NUM_REGS * sizeof(struct cfi_reg));
2406 state.stack_size = initial_func_cfi.cfa.offset;
2407
2408 for_each_sec(file, sec) {
dcc914f4 2409 list_for_each_entry(func, &sec->symbol_list, list) {
e10cd8fe
JP
2410 if (func->type != STT_FUNC)
2411 continue;
2412
61e9b75a
JP
2413 if (!func->len) {
2414 WARN("%s() is missing an ELF size annotation",
2415 func->name);
2416 warnings++;
2417 }
2418
e10cd8fe 2419 if (func->pfunc != func || func->alias != func)
dcc914f4
JP
2420 continue;
2421
2422 insn = find_insn(file, sec, func->offset);
c705cecc 2423 if (!insn || insn->ignore || insn->visited)
dcc914f4
JP
2424 continue;
2425
e10cd8fe 2426 state.uaccess = func->uaccess_safe;
ea24213d 2427
c705cecc 2428 ret = validate_branch(file, func, insn, state);
7697eee3
PZ
2429 if (ret && backtrace)
2430 BT_FUNC("<=== (func)", insn);
dcc914f4
JP
2431 warnings += ret;
2432 }
2433 }
2434
dcc914f4
JP
2435 return warnings;
2436}
2437
baa41469 2438static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4
JP
2439{
2440 struct instruction *insn;
baa41469
JP
2441
2442 if (file->ignore_unreachables)
2443 return 0;
dcc914f4
JP
2444
2445 for_each_insn(file, insn) {
baa41469
JP
2446 if (insn->visited || ignore_unreachable_insn(insn))
2447 continue;
2448
baa41469
JP
2449 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2450 return 1;
dcc914f4
JP
2451 }
2452
baa41469 2453 return 0;
dcc914f4
JP
2454}
2455
2456static void cleanup(struct objtool_file *file)
2457{
2458 struct instruction *insn, *tmpinsn;
2459 struct alternative *alt, *tmpalt;
2460
2461 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2462 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2463 list_del(&alt->list);
2464 free(alt);
2465 }
2466 list_del(&insn->list);
2467 hash_del(&insn->hash);
2468 free(insn);
2469 }
2470 elf_close(file->elf);
2471}
2472
0c671812
JP
2473static struct objtool_file file;
2474
43a4525f 2475int check(const char *_objname, bool orc)
dcc914f4 2476{
dcc914f4
JP
2477 int ret, warnings = 0;
2478
2479 objname = _objname;
dcc914f4 2480
8e144797 2481 file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
baa41469 2482 if (!file.elf)
dcc914f4 2483 return 1;
dcc914f4
JP
2484
2485 INIT_LIST_HEAD(&file.insn_list);
2486 hash_init(file.insn_hash);
dcc914f4 2487 file.c_file = find_section_by_name(file.elf, ".comment");
867ac9d7 2488 file.ignore_unreachables = no_unreachable;
39358a03 2489 file.hints = false;
dcc914f4 2490
baa41469
JP
2491 arch_initial_func_cfi_state(&initial_func_cfi);
2492
dcc914f4
JP
2493 ret = decode_sections(&file);
2494 if (ret < 0)
2495 goto out;
2496 warnings += ret;
2497
baa41469 2498 if (list_empty(&file.insn_list))
dcc914f4 2499 goto out;
dcc914f4 2500
b5bc2231
PZ
2501 if (retpoline) {
2502 ret = validate_retpoline(&file);
2503 if (ret < 0)
2504 return ret;
2505 warnings += ret;
2506 }
2507
baa41469 2508 ret = validate_functions(&file);
dcc914f4
JP
2509 if (ret < 0)
2510 goto out;
2511 warnings += ret;
2512
39358a03
JP
2513 ret = validate_unwind_hints(&file);
2514 if (ret < 0)
2515 goto out;
2516 warnings += ret;
2517
baa41469
JP
2518 if (!warnings) {
2519 ret = validate_reachable_instructions(&file);
2520 if (ret < 0)
2521 goto out;
2522 warnings += ret;
2523 }
2524
627fce14
JP
2525 if (orc) {
2526 ret = create_orc(&file);
2527 if (ret < 0)
2528 goto out;
2529
2530 ret = create_orc_sections(&file);
2531 if (ret < 0)
2532 goto out;
2533
2534 ret = elf_write(file.elf);
2535 if (ret < 0)
2536 goto out;
2537 }
2538
dcc914f4
JP
2539out:
2540 cleanup(&file);
2541
644592d3
JP
2542 if (ret < 0) {
2543 /*
2544 * Fatal error. The binary is corrupt or otherwise broken in
2545 * some way, or objtool itself is broken. Fail the kernel
2546 * build.
2547 */
2548 return ret;
2549 }
2550
dcc914f4
JP
2551 return 0;
2552}