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