]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/objtool/check.c
selftests/x86: Disable tests requiring 32-bit support on pure 64-bit systems
[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
21#include "check.h"
22#include "elf.h"
23#include "special.h"
24#include "arch.h"
25#include "warn.h"
26
27#include <linux/hashtable.h>
28#include <linux/kernel.h>
29
dcc914f4
JP
30struct alternative {
31 struct list_head list;
32 struct instruction *insn;
33};
34
35const char *objname;
867ac9d7 36static bool no_fp;
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;
499 continue;
dcc914f4
JP
500 } else {
501 /* sibling call */
502 insn->jump_dest = 0;
503 continue;
504 }
505
506 insn->jump_dest = find_insn(file, dest_sec, dest_off);
507 if (!insn->jump_dest) {
508
509 /*
510 * This is a special case where an alt instruction
511 * jumps past the end of the section. These are
512 * handled later in handle_group_alt().
513 */
514 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
515 continue;
516
517 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
518 insn->sec, insn->offset, dest_sec->name,
519 dest_off);
520 return -1;
521 }
522 }
523
524 return 0;
525}
526
527/*
528 * Find the destination instructions for all calls.
529 */
530static int add_call_destinations(struct objtool_file *file)
531{
532 struct instruction *insn;
533 unsigned long dest_off;
534 struct rela *rela;
535
536 for_each_insn(file, insn) {
537 if (insn->type != INSN_CALL)
538 continue;
539
540 rela = find_rela_by_dest_range(insn->sec, insn->offset,
541 insn->len);
542 if (!rela) {
543 dest_off = insn->offset + insn->len + insn->immediate;
544 insn->call_dest = find_symbol_by_offset(insn->sec,
545 dest_off);
a845c7cf
JP
546
547 if (!insn->call_dest && !insn->ignore) {
548 WARN_FUNC("unsupported intra-function call",
549 insn->sec, insn->offset);
550 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
dcc914f4
JP
551 return -1;
552 }
a845c7cf 553
dcc914f4
JP
554 } else if (rela->sym->type == STT_SECTION) {
555 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
556 rela->addend+4);
557 if (!insn->call_dest ||
558 insn->call_dest->type != STT_FUNC) {
559 WARN_FUNC("can't find call dest symbol at %s+0x%x",
560 insn->sec, insn->offset,
561 rela->sym->sec->name,
562 rela->addend + 4);
563 return -1;
564 }
565 } else
566 insn->call_dest = rela->sym;
567 }
568
569 return 0;
570}
571
572/*
573 * The .alternatives section requires some extra special care, over and above
574 * what other special sections require:
575 *
576 * 1. Because alternatives are patched in-place, we need to insert a fake jump
577 * instruction at the end so that validate_branch() skips all the original
578 * replaced instructions when validating the new instruction path.
579 *
580 * 2. An added wrinkle is that the new instruction length might be zero. In
581 * that case the old instructions are replaced with noops. We simulate that
582 * by creating a fake jump as the only new instruction.
583 *
584 * 3. In some cases, the alternative section includes an instruction which
585 * conditionally jumps to the _end_ of the entry. We have to modify these
586 * jumps' destinations to point back to .text rather than the end of the
587 * entry in .altinstr_replacement.
588 *
589 * 4. It has been requested that we don't validate the !POPCNT feature path
590 * which is a "very very small percentage of machines".
591 */
592static int handle_group_alt(struct objtool_file *file,
593 struct special_alt *special_alt,
594 struct instruction *orig_insn,
595 struct instruction **new_insn)
596{
17bc3391 597 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
dcc914f4
JP
598 unsigned long dest_off;
599
600 last_orig_insn = NULL;
601 insn = orig_insn;
602 sec_for_each_insn_from(file, insn) {
603 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
604 break;
605
606 if (special_alt->skip_orig)
607 insn->type = INSN_NOP;
608
609 insn->alt_group = true;
610 last_orig_insn = insn;
611 }
612
17bc3391
JP
613 if (next_insn_same_sec(file, last_orig_insn)) {
614 fake_jump = malloc(sizeof(*fake_jump));
615 if (!fake_jump) {
616 WARN("malloc failed");
617 return -1;
618 }
619 memset(fake_jump, 0, sizeof(*fake_jump));
620 INIT_LIST_HEAD(&fake_jump->alts);
621 clear_insn_state(&fake_jump->state);
622
623 fake_jump->sec = special_alt->new_sec;
624 fake_jump->offset = -1;
625 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
626 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
627 fake_jump->ignore = true;
dcc914f4 628 }
dcc914f4
JP
629
630 if (!special_alt->new_len) {
17bc3391
JP
631 if (!fake_jump) {
632 WARN("%s: empty alternative at end of section",
633 special_alt->orig_sec->name);
634 return -1;
635 }
636
dcc914f4
JP
637 *new_insn = fake_jump;
638 return 0;
639 }
640
641 last_new_insn = NULL;
642 insn = *new_insn;
643 sec_for_each_insn_from(file, insn) {
644 if (insn->offset >= special_alt->new_off + special_alt->new_len)
645 break;
646
647 last_new_insn = insn;
648
a845c7cf
JP
649 insn->ignore = orig_insn->ignore_alts;
650
dcc914f4
JP
651 if (insn->type != INSN_JUMP_CONDITIONAL &&
652 insn->type != INSN_JUMP_UNCONDITIONAL)
653 continue;
654
655 if (!insn->immediate)
656 continue;
657
658 dest_off = insn->offset + insn->len + insn->immediate;
17bc3391
JP
659 if (dest_off == special_alt->new_off + special_alt->new_len) {
660 if (!fake_jump) {
661 WARN("%s: alternative jump to end of section",
662 special_alt->orig_sec->name);
663 return -1;
664 }
dcc914f4 665 insn->jump_dest = fake_jump;
17bc3391 666 }
dcc914f4
JP
667
668 if (!insn->jump_dest) {
669 WARN_FUNC("can't find alternative jump destination",
670 insn->sec, insn->offset);
671 return -1;
672 }
673 }
674
675 if (!last_new_insn) {
676 WARN_FUNC("can't find last new alternative instruction",
677 special_alt->new_sec, special_alt->new_off);
678 return -1;
679 }
680
17bc3391
JP
681 if (fake_jump)
682 list_add(&fake_jump->list, &last_new_insn->list);
dcc914f4
JP
683
684 return 0;
685}
686
687/*
688 * A jump table entry can either convert a nop to a jump or a jump to a nop.
689 * If the original instruction is a jump, make the alt entry an effective nop
690 * by just skipping the original instruction.
691 */
692static int handle_jump_alt(struct objtool_file *file,
693 struct special_alt *special_alt,
694 struct instruction *orig_insn,
695 struct instruction **new_insn)
696{
697 if (orig_insn->type == INSN_NOP)
698 return 0;
699
700 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
701 WARN_FUNC("unsupported instruction at jump label",
702 orig_insn->sec, orig_insn->offset);
703 return -1;
704 }
705
706 *new_insn = list_next_entry(orig_insn, list);
707 return 0;
708}
709
710/*
711 * Read all the special sections which have alternate instructions which can be
712 * patched in or redirected to at runtime. Each instruction having alternate
713 * instruction(s) has them added to its insn->alts list, which will be
714 * traversed in validate_branch().
715 */
716static int add_special_section_alts(struct objtool_file *file)
717{
718 struct list_head special_alts;
719 struct instruction *orig_insn, *new_insn;
720 struct special_alt *special_alt, *tmp;
721 struct alternative *alt;
722 int ret;
723
724 ret = special_get_alts(file->elf, &special_alts);
725 if (ret)
726 return ret;
727
728 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
dcc914f4
JP
729
730 orig_insn = find_insn(file, special_alt->orig_sec,
731 special_alt->orig_off);
732 if (!orig_insn) {
733 WARN_FUNC("special: can't find orig instruction",
734 special_alt->orig_sec, special_alt->orig_off);
735 ret = -1;
736 goto out;
737 }
738
739 new_insn = NULL;
740 if (!special_alt->group || special_alt->new_len) {
741 new_insn = find_insn(file, special_alt->new_sec,
742 special_alt->new_off);
743 if (!new_insn) {
744 WARN_FUNC("special: can't find new instruction",
745 special_alt->new_sec,
746 special_alt->new_off);
747 ret = -1;
748 goto out;
749 }
750 }
751
752 if (special_alt->group) {
753 ret = handle_group_alt(file, special_alt, orig_insn,
754 &new_insn);
755 if (ret)
756 goto out;
757 } else if (special_alt->jump_or_nop) {
758 ret = handle_jump_alt(file, special_alt, orig_insn,
759 &new_insn);
760 if (ret)
761 goto out;
762 }
763
258c7605
JP
764 alt = malloc(sizeof(*alt));
765 if (!alt) {
766 WARN("malloc failed");
767 ret = -1;
768 goto out;
769 }
770
dcc914f4
JP
771 alt->insn = new_insn;
772 list_add_tail(&alt->list, &orig_insn->alts);
773
774 list_del(&special_alt->list);
775 free(special_alt);
776 }
777
778out:
779 return ret;
780}
781
782static int add_switch_table(struct objtool_file *file, struct symbol *func,
783 struct instruction *insn, struct rela *table,
784 struct rela *next_table)
785{
786 struct rela *rela = table;
787 struct instruction *alt_insn;
788 struct alternative *alt;
789
790 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
791 if (rela == next_table)
792 break;
793
794 if (rela->sym->sec != insn->sec ||
795 rela->addend <= func->offset ||
796 rela->addend >= func->offset + func->len)
797 break;
798
799 alt_insn = find_insn(file, insn->sec, rela->addend);
800 if (!alt_insn) {
801 WARN("%s: can't find instruction at %s+0x%x",
802 file->rodata->rela->name, insn->sec->name,
803 rela->addend);
804 return -1;
805 }
806
807 alt = malloc(sizeof(*alt));
808 if (!alt) {
809 WARN("malloc failed");
810 return -1;
811 }
812
813 alt->insn = alt_insn;
814 list_add_tail(&alt->list, &insn->alts);
815 }
816
817 return 0;
818}
819
820/*
821 * find_switch_table() - Given a dynamic jump, find the switch jump table in
822 * .rodata associated with it.
823 *
824 * There are 3 basic patterns:
825 *
826 * 1. jmpq *[rodata addr](,%reg,8)
827 *
828 * This is the most common case by far. It jumps to an address in a simple
829 * jump table which is stored in .rodata.
830 *
831 * 2. jmpq *[rodata addr](%rip)
832 *
833 * This is caused by a rare GCC quirk, currently only seen in three driver
834 * functions in the kernel, only with certain obscure non-distro configs.
835 *
836 * As part of an optimization, GCC makes a copy of an existing switch jump
837 * table, modifies it, and then hard-codes the jump (albeit with an indirect
838 * jump) to use a single entry in the table. The rest of the jump table and
839 * some of its jump targets remain as dead code.
840 *
841 * In such a case we can just crudely ignore all unreachable instruction
842 * warnings for the entire object file. Ideally we would just ignore them
843 * for the function, but that would require redesigning the code quite a
844 * bit. And honestly that's just not worth doing: unreachable instruction
845 * warnings are of questionable value anyway, and this is such a rare issue.
846 *
847 * 3. mov [rodata addr],%reg1
848 * ... some instructions ...
849 * jmpq *(%reg1,%reg2,8)
850 *
851 * This is a fairly uncommon pattern which is new for GCC 6. As of this
852 * writing, there are 11 occurrences of it in the allmodconfig kernel.
853 *
99ce7962
PZ
854 * As of GCC 7 there are quite a few more of these and the 'in between' code
855 * is significant. Esp. with KASAN enabled some of the code between the mov
856 * and jmpq uses .rodata itself, which can confuse things.
857 *
dcc914f4
JP
858 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
859 * ensure the same register is used in the mov and jump instructions.
99ce7962
PZ
860 *
861 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
dcc914f4
JP
862 */
863static struct rela *find_switch_table(struct objtool_file *file,
864 struct symbol *func,
865 struct instruction *insn)
866{
867 struct rela *text_rela, *rodata_rela;
868 struct instruction *orig_insn = insn;
869
870 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
871 if (text_rela && text_rela->sym == file->rodata->sym) {
872 /* case 1 */
873 rodata_rela = find_rela_by_dest(file->rodata,
874 text_rela->addend);
875 if (rodata_rela)
876 return rodata_rela;
877
878 /* case 2 */
879 rodata_rela = find_rela_by_dest(file->rodata,
880 text_rela->addend + 4);
881 if (!rodata_rela)
882 return NULL;
99ce7962 883
dcc914f4
JP
884 file->ignore_unreachables = true;
885 return rodata_rela;
886 }
887
888 /* case 3 */
99ce7962
PZ
889 /*
890 * Backward search using the @first_jump_src links, these help avoid
891 * much of the 'in between' code. Which avoids us getting confused by
892 * it.
893 */
894 for (insn = list_prev_entry(insn, list);
895
896 &insn->list != &file->insn_list &&
897 insn->sec == func->sec &&
898 insn->offset >= func->offset;
899
900 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
901
dcc914f4
JP
902 if (insn->type == INSN_JUMP_DYNAMIC)
903 break;
904
905 /* allow small jumps within the range */
906 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
907 insn->jump_dest &&
908 (insn->jump_dest->offset <= insn->offset ||
909 insn->jump_dest->offset > orig_insn->offset))
910 break;
911
912 /* look for a relocation which references .rodata */
913 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
914 insn->len);
915 if (!text_rela || text_rela->sym != file->rodata->sym)
916 continue;
917
918 /*
919 * Make sure the .rodata address isn't associated with a
920 * symbol. gcc jump tables are anonymous data.
921 */
922 if (find_symbol_containing(file->rodata, text_rela->addend))
923 continue;
924
925 return find_rela_by_dest(file->rodata, text_rela->addend);
926 }
927
928 return NULL;
929}
930
99ce7962 931
dcc914f4
JP
932static int add_func_switch_tables(struct objtool_file *file,
933 struct symbol *func)
934{
99ce7962 935 struct instruction *insn, *last = NULL, *prev_jump = NULL;
dcc914f4
JP
936 struct rela *rela, *prev_rela = NULL;
937 int ret;
938
939 func_for_each_insn(file, func, insn) {
99ce7962
PZ
940 if (!last)
941 last = insn;
942
943 /*
944 * Store back-pointers for unconditional forward jumps such
945 * that find_switch_table() can back-track using those and
946 * avoid some potentially confusing code.
947 */
948 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
949 insn->offset > last->offset &&
950 insn->jump_dest->offset > insn->offset &&
951 !insn->jump_dest->first_jump_src) {
952
953 insn->jump_dest->first_jump_src = insn;
954 last = insn->jump_dest;
955 }
956
dcc914f4
JP
957 if (insn->type != INSN_JUMP_DYNAMIC)
958 continue;
959
960 rela = find_switch_table(file, func, insn);
961 if (!rela)
962 continue;
963
964 /*
965 * We found a switch table, but we don't know yet how big it
966 * is. Don't add it until we reach the end of the function or
967 * the beginning of another switch table in the same function.
968 */
969 if (prev_jump) {
970 ret = add_switch_table(file, func, prev_jump, prev_rela,
971 rela);
972 if (ret)
973 return ret;
974 }
975
976 prev_jump = insn;
977 prev_rela = rela;
978 }
979
980 if (prev_jump) {
981 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
982 if (ret)
983 return ret;
984 }
985
986 return 0;
987}
988
989/*
990 * For some switch statements, gcc generates a jump table in the .rodata
991 * section which contains a list of addresses within the function to jump to.
992 * This finds these jump tables and adds them to the insn->alts lists.
993 */
994static int add_switch_table_alts(struct objtool_file *file)
995{
996 struct section *sec;
997 struct symbol *func;
998 int ret;
999
1000 if (!file->rodata || !file->rodata->rela)
1001 return 0;
1002
baa41469 1003 for_each_sec(file, sec) {
dcc914f4
JP
1004 list_for_each_entry(func, &sec->symbol_list, list) {
1005 if (func->type != STT_FUNC)
1006 continue;
1007
1008 ret = add_func_switch_tables(file, func);
1009 if (ret)
1010 return ret;
1011 }
1012 }
1013
1014 return 0;
1015}
1016
39358a03
JP
1017static int read_unwind_hints(struct objtool_file *file)
1018{
1019 struct section *sec, *relasec;
1020 struct rela *rela;
1021 struct unwind_hint *hint;
1022 struct instruction *insn;
1023 struct cfi_reg *cfa;
1024 int i;
1025
1026 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1027 if (!sec)
1028 return 0;
1029
1030 relasec = sec->rela;
1031 if (!relasec) {
1032 WARN("missing .rela.discard.unwind_hints section");
1033 return -1;
1034 }
1035
1036 if (sec->len % sizeof(struct unwind_hint)) {
1037 WARN("struct unwind_hint size mismatch");
1038 return -1;
1039 }
1040
1041 file->hints = true;
1042
1043 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1044 hint = (struct unwind_hint *)sec->data->d_buf + i;
1045
1046 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1047 if (!rela) {
1048 WARN("can't find rela for unwind_hints[%d]", i);
1049 return -1;
1050 }
1051
1052 insn = find_insn(file, rela->sym->sec, rela->addend);
1053 if (!insn) {
1054 WARN("can't find insn for unwind_hints[%d]", i);
1055 return -1;
1056 }
1057
1058 cfa = &insn->state.cfa;
1059
1060 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1061 insn->save = true;
1062 continue;
1063
1064 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1065 insn->restore = true;
1066 insn->hint = true;
1067 continue;
1068 }
1069
1070 insn->hint = true;
1071
1072 switch (hint->sp_reg) {
1073 case ORC_REG_UNDEFINED:
1074 cfa->base = CFI_UNDEFINED;
1075 break;
1076 case ORC_REG_SP:
1077 cfa->base = CFI_SP;
1078 break;
1079 case ORC_REG_BP:
1080 cfa->base = CFI_BP;
1081 break;
1082 case ORC_REG_SP_INDIRECT:
1083 cfa->base = CFI_SP_INDIRECT;
1084 break;
1085 case ORC_REG_R10:
1086 cfa->base = CFI_R10;
1087 break;
1088 case ORC_REG_R13:
1089 cfa->base = CFI_R13;
1090 break;
1091 case ORC_REG_DI:
1092 cfa->base = CFI_DI;
1093 break;
1094 case ORC_REG_DX:
1095 cfa->base = CFI_DX;
1096 break;
1097 default:
1098 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1099 insn->sec, insn->offset, hint->sp_reg);
1100 return -1;
1101 }
1102
1103 cfa->offset = hint->sp_offset;
1104 insn->state.type = hint->type;
1105 }
1106
1107 return 0;
1108}
1109
dcc914f4
JP
1110static int decode_sections(struct objtool_file *file)
1111{
1112 int ret;
1113
1114 ret = decode_instructions(file);
1115 if (ret)
1116 return ret;
1117
1118 ret = add_dead_ends(file);
1119 if (ret)
1120 return ret;
1121
1122 add_ignores(file);
1123
258c7605
JP
1124 ret = add_nospec_ignores(file);
1125 if (ret)
1126 return ret;
1127
dcc914f4
JP
1128 ret = add_jump_destinations(file);
1129 if (ret)
1130 return ret;
1131
a845c7cf 1132 ret = add_special_section_alts(file);
dcc914f4
JP
1133 if (ret)
1134 return ret;
1135
a845c7cf 1136 ret = add_call_destinations(file);
dcc914f4
JP
1137 if (ret)
1138 return ret;
1139
1140 ret = add_switch_table_alts(file);
1141 if (ret)
1142 return ret;
1143
39358a03
JP
1144 ret = read_unwind_hints(file);
1145 if (ret)
1146 return ret;
1147
dcc914f4
JP
1148 return 0;
1149}
1150
1151static bool is_fentry_call(struct instruction *insn)
1152{
1153 if (insn->type == INSN_CALL &&
1154 insn->call_dest->type == STT_NOTYPE &&
1155 !strcmp(insn->call_dest->name, "__fentry__"))
1156 return true;
1157
1158 return false;
1159}
1160
baa41469 1161static bool has_modified_stack_frame(struct insn_state *state)
dcc914f4 1162{
baa41469
JP
1163 int i;
1164
1165 if (state->cfa.base != initial_func_cfi.cfa.base ||
1166 state->cfa.offset != initial_func_cfi.cfa.offset ||
1167 state->stack_size != initial_func_cfi.cfa.offset ||
1168 state->drap)
1169 return true;
1170
1171 for (i = 0; i < CFI_NUM_REGS; i++)
1172 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1173 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1174 return true;
1175
1176 return false;
1177}
1178
1179static bool has_valid_stack_frame(struct insn_state *state)
1180{
1181 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1182 state->regs[CFI_BP].offset == -16)
1183 return true;
1184
1185 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1186 return true;
1187
1188 return false;
dcc914f4
JP
1189}
1190
627fce14
JP
1191static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1192{
1193 struct cfi_reg *cfa = &state->cfa;
1194 struct stack_op *op = &insn->stack_op;
1195
1196 if (cfa->base != CFI_SP)
1197 return 0;
1198
1199 /* push */
1200 if (op->dest.type == OP_DEST_PUSH)
1201 cfa->offset += 8;
1202
1203 /* pop */
1204 if (op->src.type == OP_SRC_POP)
1205 cfa->offset -= 8;
1206
1207 /* add immediate to sp */
1208 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1209 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1210 cfa->offset -= op->src.offset;
1211
1212 return 0;
1213}
1214
baa41469
JP
1215static void save_reg(struct insn_state *state, unsigned char reg, int base,
1216 int offset)
dcc914f4 1217{
bf4d1a83 1218 if (arch_callee_saved_reg(reg) &&
baa41469
JP
1219 state->regs[reg].base == CFI_UNDEFINED) {
1220 state->regs[reg].base = base;
1221 state->regs[reg].offset = offset;
1222 }
dcc914f4
JP
1223}
1224
baa41469 1225static void restore_reg(struct insn_state *state, unsigned char reg)
dcc914f4 1226{
baa41469
JP
1227 state->regs[reg].base = CFI_UNDEFINED;
1228 state->regs[reg].offset = 0;
1229}
1230
1231/*
1232 * A note about DRAP stack alignment:
1233 *
1234 * GCC has the concept of a DRAP register, which is used to help keep track of
1235 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1236 * register. The typical DRAP pattern is:
1237 *
1238 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1239 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1240 * 41 ff 72 f8 pushq -0x8(%r10)
1241 * 55 push %rbp
1242 * 48 89 e5 mov %rsp,%rbp
1243 * (more pushes)
1244 * 41 52 push %r10
1245 * ...
1246 * 41 5a pop %r10
1247 * (more pops)
1248 * 5d pop %rbp
1249 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1250 * c3 retq
1251 *
1252 * There are some variations in the epilogues, like:
1253 *
1254 * 5b pop %rbx
1255 * 41 5a pop %r10
1256 * 41 5c pop %r12
1257 * 41 5d pop %r13
1258 * 41 5e pop %r14
1259 * c9 leaveq
1260 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1261 * c3 retq
1262 *
1263 * and:
1264 *
1265 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1266 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1267 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1268 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1269 * c9 leaveq
1270 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1271 * c3 retq
1272 *
1273 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1274 * restored beforehand:
1275 *
1276 * 41 55 push %r13
1277 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1278 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1279 * ...
1280 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1281 * 41 5d pop %r13
1282 * c3 retq
1283 */
1284static int update_insn_state(struct instruction *insn, struct insn_state *state)
1285{
1286 struct stack_op *op = &insn->stack_op;
1287 struct cfi_reg *cfa = &state->cfa;
1288 struct cfi_reg *regs = state->regs;
1289
1290 /* stack operations don't make sense with an undefined CFA */
1291 if (cfa->base == CFI_UNDEFINED) {
1292 if (insn->func) {
1293 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1294 return -1;
1295 }
1296 return 0;
1297 }
1298
627fce14
JP
1299 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1300 return update_insn_state_regs(insn, state);
1301
baa41469
JP
1302 switch (op->dest.type) {
1303
1304 case OP_DEST_REG:
1305 switch (op->src.type) {
1306
1307 case OP_SRC_REG:
0d0970ee
JP
1308 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1309 cfa->base == CFI_SP &&
1310 regs[CFI_BP].base == CFI_CFA &&
1311 regs[CFI_BP].offset == -cfa->offset) {
1312
1313 /* mov %rsp, %rbp */
1314 cfa->base = op->dest.reg;
1315 state->bp_scratch = false;
1316 }
dd88a0a0 1317
0d0970ee
JP
1318 else if (op->src.reg == CFI_SP &&
1319 op->dest.reg == CFI_BP && state->drap) {
dd88a0a0 1320
0d0970ee
JP
1321 /* drap: mov %rsp, %rbp */
1322 regs[CFI_BP].base = CFI_BP;
1323 regs[CFI_BP].offset = -state->stack_size;
1324 state->bp_scratch = false;
1325 }
dd88a0a0 1326
0d0970ee
JP
1327 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1328
1329 /*
1330 * mov %rsp, %reg
1331 *
1332 * This is needed for the rare case where GCC
1333 * does:
1334 *
1335 * mov %rsp, %rax
1336 * ...
1337 * mov %rax, %rsp
1338 */
1339 state->vals[op->dest.reg].base = CFI_CFA;
1340 state->vals[op->dest.reg].offset = -state->stack_size;
dd88a0a0
JP
1341 }
1342
1343 else if (op->dest.reg == cfa->base) {
1344
1345 /* mov %reg, %rsp */
1346 if (cfa->base == CFI_SP &&
1347 state->vals[op->src.reg].base == CFI_CFA) {
1348
1349 /*
1350 * This is needed for the rare case
1351 * where GCC does something dumb like:
1352 *
1353 * lea 0x8(%rsp), %rcx
1354 * ...
1355 * mov %rcx, %rsp
1356 */
1357 cfa->offset = -state->vals[op->src.reg].offset;
1358 state->stack_size = cfa->offset;
1359
1360 } else {
1361 cfa->base = CFI_UNDEFINED;
1362 cfa->offset = 0;
1363 }
baa41469
JP
1364 }
1365
1366 break;
1367
1368 case OP_SRC_ADD:
1369 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1370
1371 /* add imm, %rsp */
1372 state->stack_size -= op->src.offset;
1373 if (cfa->base == CFI_SP)
1374 cfa->offset -= op->src.offset;
1375 break;
1376 }
1377
1378 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1379
1380 /* lea disp(%rbp), %rsp */
1381 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1382 break;
1383 }
1384
dd88a0a0 1385 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
baa41469
JP
1386
1387 /* drap: lea disp(%rsp), %drap */
1388 state->drap_reg = op->dest.reg;
dd88a0a0
JP
1389
1390 /*
1391 * lea disp(%rsp), %reg
1392 *
1393 * This is needed for the rare case where GCC
1394 * does something dumb like:
1395 *
1396 * lea 0x8(%rsp), %rcx
1397 * ...
1398 * mov %rcx, %rsp
1399 */
1400 state->vals[op->dest.reg].base = CFI_CFA;
1401 state->vals[op->dest.reg].offset = \
1402 -state->stack_size + op->src.offset;
1403
baa41469
JP
1404 break;
1405 }
1406
1407 if (state->drap && op->dest.reg == CFI_SP &&
1408 op->src.reg == state->drap_reg) {
1409
1410 /* drap: lea disp(%drap), %rsp */
1411 cfa->base = CFI_SP;
1412 cfa->offset = state->stack_size = -op->src.offset;
1413 state->drap_reg = CFI_UNDEFINED;
1414 state->drap = false;
1415 break;
1416 }
1417
1418 if (op->dest.reg == state->cfa.base) {
1419 WARN_FUNC("unsupported stack register modification",
1420 insn->sec, insn->offset);
1421 return -1;
1422 }
1423
1424 break;
1425
1426 case OP_SRC_AND:
1427 if (op->dest.reg != CFI_SP ||
1428 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1429 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1430 WARN_FUNC("unsupported stack pointer realignment",
1431 insn->sec, insn->offset);
1432 return -1;
1433 }
1434
1435 if (state->drap_reg != CFI_UNDEFINED) {
1436 /* drap: and imm, %rsp */
1437 cfa->base = state->drap_reg;
1438 cfa->offset = state->stack_size = 0;
1439 state->drap = true;
baa41469
JP
1440 }
1441
1442 /*
1443 * Older versions of GCC (4.8ish) realign the stack
1444 * without DRAP, with a frame pointer.
1445 */
1446
1447 break;
1448
1449 case OP_SRC_POP:
1450 if (!state->drap && op->dest.type == OP_DEST_REG &&
1451 op->dest.reg == cfa->base) {
1452
1453 /* pop %rbp */
1454 cfa->base = CFI_SP;
1455 }
1456
bf4d1a83
JP
1457 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1458 op->dest.type == OP_DEST_REG &&
1459 op->dest.reg == state->drap_reg &&
1460 state->drap_offset == -state->stack_size) {
baa41469 1461
bf4d1a83
JP
1462 /* drap: pop %drap */
1463 cfa->base = state->drap_reg;
1464 cfa->offset = 0;
1465 state->drap_offset = -1;
baa41469 1466
bf4d1a83 1467 } else if (regs[op->dest.reg].offset == -state->stack_size) {
baa41469 1468
bf4d1a83 1469 /* pop %reg */
baa41469
JP
1470 restore_reg(state, op->dest.reg);
1471 }
1472
1473 state->stack_size -= 8;
1474 if (cfa->base == CFI_SP)
1475 cfa->offset -= 8;
1476
1477 break;
1478
1479 case OP_SRC_REG_INDIRECT:
bf4d1a83
JP
1480 if (state->drap && op->src.reg == CFI_BP &&
1481 op->src.offset == state->drap_offset) {
1482
1483 /* drap: mov disp(%rbp), %drap */
1484 cfa->base = state->drap_reg;
1485 cfa->offset = 0;
1486 state->drap_offset = -1;
1487 }
1488
baa41469
JP
1489 if (state->drap && op->src.reg == CFI_BP &&
1490 op->src.offset == regs[op->dest.reg].offset) {
1491
1492 /* drap: mov disp(%rbp), %reg */
baa41469
JP
1493 restore_reg(state, op->dest.reg);
1494
1495 } else if (op->src.reg == cfa->base &&
1496 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1497
1498 /* mov disp(%rbp), %reg */
1499 /* mov disp(%rsp), %reg */
1500 restore_reg(state, op->dest.reg);
1501 }
1502
1503 break;
1504
1505 default:
1506 WARN_FUNC("unknown stack-related instruction",
1507 insn->sec, insn->offset);
1508 return -1;
1509 }
1510
1511 break;
1512
1513 case OP_DEST_PUSH:
1514 state->stack_size += 8;
1515 if (cfa->base == CFI_SP)
1516 cfa->offset += 8;
1517
1518 if (op->src.type != OP_SRC_REG)
1519 break;
1520
1521 if (state->drap) {
1522 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1523
1524 /* drap: push %drap */
1525 cfa->base = CFI_BP_INDIRECT;
1526 cfa->offset = -state->stack_size;
1527
bf4d1a83
JP
1528 /* save drap so we know when to restore it */
1529 state->drap_offset = -state->stack_size;
baa41469
JP
1530
1531 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1532
1533 /* drap: push %rbp */
1534 state->stack_size = 0;
1535
1536 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1537
1538 /* drap: push %reg */
1539 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1540 }
1541
1542 } else {
1543
1544 /* push %reg */
1545 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1546 }
1547
1548 /* detect when asm code uses rbp as a scratch register */
867ac9d7 1549 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
baa41469
JP
1550 cfa->base != CFI_BP)
1551 state->bp_scratch = true;
1552 break;
1553
1554 case OP_DEST_REG_INDIRECT:
1555
1556 if (state->drap) {
1557 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1558
1559 /* drap: mov %drap, disp(%rbp) */
1560 cfa->base = CFI_BP_INDIRECT;
1561 cfa->offset = op->dest.offset;
1562
bf4d1a83
JP
1563 /* save drap offset so we know when to restore it */
1564 state->drap_offset = op->dest.offset;
baa41469
JP
1565 }
1566
1567 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1568
1569 /* drap: mov reg, disp(%rbp) */
1570 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1571 }
1572
1573 } else if (op->dest.reg == cfa->base) {
1574
1575 /* mov reg, disp(%rbp) */
1576 /* mov reg, disp(%rsp) */
1577 save_reg(state, op->src.reg, CFI_CFA,
1578 op->dest.offset - state->cfa.offset);
1579 }
1580
1581 break;
1582
1583 case OP_DEST_LEAVE:
1584 if ((!state->drap && cfa->base != CFI_BP) ||
1585 (state->drap && cfa->base != state->drap_reg)) {
1586 WARN_FUNC("leave instruction with modified stack frame",
1587 insn->sec, insn->offset);
1588 return -1;
1589 }
1590
1591 /* leave (mov %rbp, %rsp; pop %rbp) */
1592
1593 state->stack_size = -state->regs[CFI_BP].offset - 8;
1594 restore_reg(state, CFI_BP);
1595
1596 if (!state->drap) {
1597 cfa->base = CFI_SP;
1598 cfa->offset -= 8;
1599 }
1600
1601 break;
1602
1603 case OP_DEST_MEM:
1604 if (op->src.type != OP_SRC_POP) {
1605 WARN_FUNC("unknown stack-related memory operation",
1606 insn->sec, insn->offset);
1607 return -1;
1608 }
1609
1610 /* pop mem */
1611 state->stack_size -= 8;
1612 if (cfa->base == CFI_SP)
1613 cfa->offset -= 8;
1614
1615 break;
1616
1617 default:
1618 WARN_FUNC("unknown stack-related instruction",
1619 insn->sec, insn->offset);
1620 return -1;
1621 }
1622
1623 return 0;
1624}
1625
1626static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1627{
1628 struct insn_state *state1 = &insn->state, *state2 = state;
1629 int i;
1630
1631 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1632 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1633 insn->sec, insn->offset,
1634 state1->cfa.base, state1->cfa.offset,
1635 state2->cfa.base, state2->cfa.offset);
1636
1637 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1638 for (i = 0; i < CFI_NUM_REGS; i++) {
1639 if (!memcmp(&state1->regs[i], &state2->regs[i],
1640 sizeof(struct cfi_reg)))
1641 continue;
1642
1643 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1644 insn->sec, insn->offset,
1645 i, state1->regs[i].base, state1->regs[i].offset,
1646 i, state2->regs[i].base, state2->regs[i].offset);
1647 break;
1648 }
1649
627fce14
JP
1650 } else if (state1->type != state2->type) {
1651 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1652 insn->sec, insn->offset, state1->type, state2->type);
1653
baa41469 1654 } else if (state1->drap != state2->drap ||
bf4d1a83
JP
1655 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1656 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1657 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
baa41469 1658 insn->sec, insn->offset,
bf4d1a83
JP
1659 state1->drap, state1->drap_reg, state1->drap_offset,
1660 state2->drap, state2->drap_reg, state2->drap_offset);
baa41469
JP
1661
1662 } else
1663 return true;
1664
1665 return false;
dcc914f4
JP
1666}
1667
1668/*
1669 * Follow the branch starting at the given instruction, and recursively follow
1670 * any other branches (jumps). Meanwhile, track the frame pointer state at
1671 * each instruction and validate all the rules described in
1672 * tools/objtool/Documentation/stack-validation.txt.
1673 */
baa41469
JP
1674static int validate_branch(struct objtool_file *file, struct instruction *first,
1675 struct insn_state state)
dcc914f4
JP
1676{
1677 struct alternative *alt;
39358a03 1678 struct instruction *insn, *next_insn;
dcc914f4
JP
1679 struct section *sec;
1680 struct symbol *func = NULL;
dcc914f4
JP
1681 int ret;
1682
1683 insn = first;
1684 sec = insn->sec;
dcc914f4
JP
1685
1686 if (insn->alt_group && list_empty(&insn->alts)) {
1687 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1688 sec, insn->offset);
12b25729 1689 return 1;
dcc914f4
JP
1690 }
1691
1692 while (1) {
39358a03
JP
1693 next_insn = next_insn_same_sec(file, insn);
1694
ee97638b
JP
1695
1696 if (file->c_file && func && insn->func && func != insn->func) {
1697 WARN("%s() falls through to next function %s()",
1698 func->name, insn->func->name);
1699 return 1;
dcc914f4
JP
1700 }
1701
ee97638b
JP
1702 if (insn->func)
1703 func = insn->func;
baa41469 1704
4855022a
JP
1705 if (func && insn->ignore) {
1706 WARN_FUNC("BUG: why am I validating an ignored function?",
1707 sec, insn->offset);
12b25729 1708 return 1;
4855022a
JP
1709 }
1710
dcc914f4 1711 if (insn->visited) {
39358a03 1712 if (!insn->hint && !insn_state_match(insn, &state))
dcc914f4 1713 return 1;
dcc914f4
JP
1714
1715 return 0;
1716 }
1717
39358a03
JP
1718 if (insn->hint) {
1719 if (insn->restore) {
1720 struct instruction *save_insn, *i;
1721
1722 i = insn;
1723 save_insn = NULL;
1724 func_for_each_insn_continue_reverse(file, func, i) {
1725 if (i->save) {
1726 save_insn = i;
1727 break;
1728 }
1729 }
1730
1731 if (!save_insn) {
1732 WARN_FUNC("no corresponding CFI save for CFI restore",
1733 sec, insn->offset);
1734 return 1;
1735 }
1736
1737 if (!save_insn->visited) {
1738 /*
1739 * Oops, no state to copy yet.
1740 * Hopefully we can reach this
1741 * instruction from another branch
1742 * after the save insn has been
1743 * visited.
1744 */
1745 if (insn == first)
1746 return 0;
1747
1748 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1749 sec, insn->offset);
1750 return 1;
1751 }
1752
1753 insn->state = save_insn->state;
1754 }
1755
1756 state = insn->state;
1757
1758 } else
1759 insn->state = state;
dcc914f4 1760
baa41469
JP
1761 insn->visited = true;
1762
a845c7cf
JP
1763 if (!insn->ignore_alts) {
1764 list_for_each_entry(alt, &insn->alts, list) {
1765 ret = validate_branch(file, alt->insn, state);
1766 if (ret)
1767 return 1;
1768 }
dcc914f4
JP
1769 }
1770
1771 switch (insn->type) {
1772
dcc914f4 1773 case INSN_RETURN:
baa41469
JP
1774 if (func && has_modified_stack_frame(&state)) {
1775 WARN_FUNC("return with modified stack frame",
dcc914f4
JP
1776 sec, insn->offset);
1777 return 1;
1778 }
baa41469
JP
1779
1780 if (state.bp_scratch) {
1781 WARN("%s uses BP as a scratch register",
1782 insn->func->name);
1783 return 1;
1784 }
1785
dcc914f4
JP
1786 return 0;
1787
1788 case INSN_CALL:
baa41469 1789 if (is_fentry_call(insn))
dcc914f4 1790 break;
dcc914f4
JP
1791
1792 ret = dead_end_function(file, insn->call_dest);
1793 if (ret == 1)
1794 return 0;
1795 if (ret == -1)
1796 return 1;
1797
1798 /* fallthrough */
1799 case INSN_CALL_DYNAMIC:
867ac9d7 1800 if (!no_fp && func && !has_valid_stack_frame(&state)) {
dcc914f4
JP
1801 WARN_FUNC("call without frame pointer save/setup",
1802 sec, insn->offset);
1803 return 1;
1804 }
1805 break;
1806
1807 case INSN_JUMP_CONDITIONAL:
1808 case INSN_JUMP_UNCONDITIONAL:
4855022a
JP
1809 if (insn->jump_dest &&
1810 (!func || !insn->jump_dest->func ||
1811 func == insn->jump_dest->func)) {
dcc914f4
JP
1812 ret = validate_branch(file, insn->jump_dest,
1813 state);
1814 if (ret)
1815 return 1;
4855022a 1816
baa41469
JP
1817 } else if (func && has_modified_stack_frame(&state)) {
1818 WARN_FUNC("sibling call from callable instruction with modified stack frame",
dcc914f4
JP
1819 sec, insn->offset);
1820 return 1;
4855022a 1821 }
dcc914f4
JP
1822
1823 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1824 return 0;
1825
1826 break;
1827
1828 case INSN_JUMP_DYNAMIC:
baa41469
JP
1829 if (func && list_empty(&insn->alts) &&
1830 has_modified_stack_frame(&state)) {
1831 WARN_FUNC("sibling call from callable instruction with modified stack frame",
dcc914f4
JP
1832 sec, insn->offset);
1833 return 1;
1834 }
1835
1836 return 0;
1837
39358a03
JP
1838 case INSN_CONTEXT_SWITCH:
1839 if (func && (!next_insn || !next_insn->hint)) {
1840 WARN_FUNC("unsupported instruction in callable function",
1841 sec, insn->offset);
1842 return 1;
1843 }
1844 return 0;
1845
baa41469
JP
1846 case INSN_STACK:
1847 if (update_insn_state(insn, &state))
12b25729 1848 return 1;
baa41469
JP
1849
1850 break;
1851
dcc914f4
JP
1852 default:
1853 break;
1854 }
1855
1856 if (insn->dead_end)
1857 return 0;
1858
00d96180
JP
1859 if (!next_insn) {
1860 if (state.cfa.base == CFI_UNDEFINED)
1861 return 0;
dcc914f4
JP
1862 WARN("%s: unexpected end of section", sec->name);
1863 return 1;
1864 }
00d96180
JP
1865
1866 insn = next_insn;
dcc914f4
JP
1867 }
1868
1869 return 0;
1870}
1871
39358a03
JP
1872static int validate_unwind_hints(struct objtool_file *file)
1873{
1874 struct instruction *insn;
1875 int ret, warnings = 0;
1876 struct insn_state state;
1877
1878 if (!file->hints)
1879 return 0;
1880
1881 clear_insn_state(&state);
1882
1883 for_each_insn(file, insn) {
1884 if (insn->hint && !insn->visited) {
1885 ret = validate_branch(file, insn, state);
1886 warnings += ret;
1887 }
1888 }
1889
1890 return warnings;
1891}
1892
dcc914f4
JP
1893static bool is_kasan_insn(struct instruction *insn)
1894{
1895 return (insn->type == INSN_CALL &&
1896 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1897}
1898
1899static bool is_ubsan_insn(struct instruction *insn)
1900{
1901 return (insn->type == INSN_CALL &&
1902 !strcmp(insn->call_dest->name,
1903 "__ubsan_handle_builtin_unreachable"));
1904}
1905
baa41469 1906static bool ignore_unreachable_insn(struct instruction *insn)
dcc914f4
JP
1907{
1908 int i;
1909
baa41469
JP
1910 if (insn->ignore || insn->type == INSN_NOP)
1911 return true;
1912
1913 /*
1914 * Ignore any unused exceptions. This can happen when a whitelisted
1915 * function has an exception table entry.
0e2bb2bc
JP
1916 *
1917 * Also ignore alternative replacement instructions. This can happen
1918 * when a whitelisted function uses one of the ALTERNATIVE macros.
baa41469 1919 */
0e2bb2bc
JP
1920 if (!strcmp(insn->sec->name, ".fixup") ||
1921 !strcmp(insn->sec->name, ".altinstr_replacement") ||
1922 !strcmp(insn->sec->name, ".altinstr_aux"))
dcc914f4
JP
1923 return true;
1924
1925 /*
1926 * Check if this (or a subsequent) instruction is related to
1927 * CONFIG_UBSAN or CONFIG_KASAN.
1928 *
1929 * End the search at 5 instructions to avoid going into the weeds.
1930 */
baa41469
JP
1931 if (!insn->func)
1932 return false;
dcc914f4
JP
1933 for (i = 0; i < 5; i++) {
1934
1935 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1936 return true;
1937
1938 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1939 insn = insn->jump_dest;
1940 continue;
1941 }
1942
baa41469 1943 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
dcc914f4
JP
1944 break;
1945 insn = list_next_entry(insn, list);
1946 }
1947
1948 return false;
1949}
1950
1951static int validate_functions(struct objtool_file *file)
1952{
1953 struct section *sec;
1954 struct symbol *func;
1955 struct instruction *insn;
baa41469 1956 struct insn_state state;
dcc914f4
JP
1957 int ret, warnings = 0;
1958
baa41469
JP
1959 clear_insn_state(&state);
1960
1961 state.cfa = initial_func_cfi.cfa;
1962 memcpy(&state.regs, &initial_func_cfi.regs,
1963 CFI_NUM_REGS * sizeof(struct cfi_reg));
1964 state.stack_size = initial_func_cfi.cfa.offset;
1965
1966 for_each_sec(file, sec) {
dcc914f4
JP
1967 list_for_each_entry(func, &sec->symbol_list, list) {
1968 if (func->type != STT_FUNC)
1969 continue;
1970
1971 insn = find_insn(file, sec, func->offset);
baa41469 1972 if (!insn || insn->ignore)
dcc914f4
JP
1973 continue;
1974
baa41469 1975 ret = validate_branch(file, insn, state);
dcc914f4
JP
1976 warnings += ret;
1977 }
1978 }
1979
dcc914f4
JP
1980 return warnings;
1981}
1982
baa41469 1983static int validate_reachable_instructions(struct objtool_file *file)
dcc914f4
JP
1984{
1985 struct instruction *insn;
baa41469
JP
1986
1987 if (file->ignore_unreachables)
1988 return 0;
dcc914f4
JP
1989
1990 for_each_insn(file, insn) {
baa41469
JP
1991 if (insn->visited || ignore_unreachable_insn(insn))
1992 continue;
1993
baa41469
JP
1994 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
1995 return 1;
dcc914f4
JP
1996 }
1997
baa41469 1998 return 0;
dcc914f4
JP
1999}
2000
2001static void cleanup(struct objtool_file *file)
2002{
2003 struct instruction *insn, *tmpinsn;
2004 struct alternative *alt, *tmpalt;
2005
2006 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2007 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2008 list_del(&alt->list);
2009 free(alt);
2010 }
2011 list_del(&insn->list);
2012 hash_del(&insn->hash);
2013 free(insn);
2014 }
2015 elf_close(file->elf);
2016}
2017
867ac9d7 2018int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc)
dcc914f4
JP
2019{
2020 struct objtool_file file;
2021 int ret, warnings = 0;
2022
2023 objname = _objname;
867ac9d7 2024 no_fp = _no_fp;
dcc914f4 2025
627fce14 2026 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
baa41469 2027 if (!file.elf)
dcc914f4 2028 return 1;
dcc914f4
JP
2029
2030 INIT_LIST_HEAD(&file.insn_list);
2031 hash_init(file.insn_hash);
2032 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2033 file.rodata = find_section_by_name(file.elf, ".rodata");
dcc914f4 2034 file.c_file = find_section_by_name(file.elf, ".comment");
867ac9d7 2035 file.ignore_unreachables = no_unreachable;
39358a03 2036 file.hints = false;
dcc914f4 2037
baa41469
JP
2038 arch_initial_func_cfi_state(&initial_func_cfi);
2039
dcc914f4
JP
2040 ret = decode_sections(&file);
2041 if (ret < 0)
2042 goto out;
2043 warnings += ret;
2044
baa41469 2045 if (list_empty(&file.insn_list))
dcc914f4 2046 goto out;
dcc914f4 2047
baa41469 2048 ret = validate_functions(&file);
dcc914f4
JP
2049 if (ret < 0)
2050 goto out;
2051 warnings += ret;
2052
39358a03
JP
2053 ret = validate_unwind_hints(&file);
2054 if (ret < 0)
2055 goto out;
2056 warnings += ret;
2057
baa41469
JP
2058 if (!warnings) {
2059 ret = validate_reachable_instructions(&file);
2060 if (ret < 0)
2061 goto out;
2062 warnings += ret;
2063 }
2064
627fce14
JP
2065 if (orc) {
2066 ret = create_orc(&file);
2067 if (ret < 0)
2068 goto out;
2069
2070 ret = create_orc_sections(&file);
2071 if (ret < 0)
2072 goto out;
2073
2074 ret = elf_write(file.elf);
2075 if (ret < 0)
2076 goto out;
2077 }
2078
dcc914f4
JP
2079out:
2080 cleanup(&file);
2081
2082 /* ignore warnings for now until we get all the code cleaned up */
2083 if (ret || warnings)
2084 return 0;
2085 return 0;
2086}