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