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