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