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