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