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