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