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.
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.
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/>.
27 #include <linux/hashtable.h>
28 #include <linux/kernel.h>
31 struct list_head list;
32 struct instruction *insn;
37 struct cfi_state initial_func_cfi;
39 struct instruction *find_insn(struct objtool_file *file,
40 struct section *sec, unsigned long offset)
42 struct instruction *insn;
44 hash_for_each_possible(file->insn_hash, insn, hash, offset)
45 if (insn->sec == sec && insn->offset == offset)
51 static struct instruction *next_insn_same_sec(struct objtool_file *file,
52 struct instruction *insn)
54 struct instruction *next = list_next_entry(insn, list);
56 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
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))
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))
75 #define sec_for_each_insn_from(file, insn) \
76 for (; insn; insn = next_insn_same_sec(file, insn))
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))
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.
87 static bool ignore_func(struct objtool_file *file, struct symbol *func)
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)
98 if (rela->sym->type == STT_FUNC && rela->sym == func)
106 * This checks to see if the given function is a "noreturn" function.
108 * For global functions which are outside the scope of this object file, we
109 * have to keep a manual list of them.
111 * For local functions, we have to detect them manually by simply looking for
112 * the lack of a return instruction.
119 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
123 struct instruction *insn;
127 * Unfortunately these have to be hard coded because the noreturn
128 * attribute isn't provided in ELF data.
130 static const char * const global_noreturns[] = {
135 "__module_put_and_exit",
137 "kvm_spurious_fault",
143 if (func->bind == STB_WEAK)
146 if (func->bind == STB_GLOBAL)
147 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
148 if (!strcmp(func->name, global_noreturns[i]))
154 func_for_each_insn(file, func, insn) {
157 if (insn->type == INSN_RETURN)
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.
169 func_for_each_insn(file, func, insn) {
170 if (insn->sec != func->sec ||
171 insn->offset >= func->offset + func->len)
174 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
175 struct instruction *dest = insn->jump_dest;
176 struct symbol *dest_func;
179 /* sibling call to another file */
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,
191 if (recursion == 5) {
192 WARN_FUNC("infinite recursion (objtool bug!)",
193 dest->sec, dest->offset);
197 return __dead_end_function(file, dest_func,
202 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
210 static int dead_end_function(struct objtool_file *file, struct symbol *func)
212 return __dead_end_function(file, func, 0);
215 static void clear_insn_state(struct insn_state *state)
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->vals[i].base = CFI_UNDEFINED;
225 state->drap_reg = CFI_UNDEFINED;
226 state->drap_offset = -1;
230 * Call the arch-specific instruction decoder for all the instructions and add
231 * them to the global instruction list.
233 static int decode_instructions(struct objtool_file *file)
237 unsigned long offset;
238 struct instruction *insn;
241 for_each_sec(file, sec) {
243 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
246 if (strcmp(sec->name, ".altinstr_replacement") &&
247 strcmp(sec->name, ".altinstr_aux") &&
248 strncmp(sec->name, ".discard.", 9))
251 for (offset = 0; offset < sec->len; offset += insn->len) {
252 insn = malloc(sizeof(*insn));
254 WARN("malloc failed");
257 memset(insn, 0, sizeof(*insn));
258 INIT_LIST_HEAD(&insn->alts);
259 clear_insn_state(&insn->state);
262 insn->offset = offset;
264 ret = arch_decode_instruction(file->elf, sec, offset,
266 &insn->len, &insn->type,
272 if (!insn->type || insn->type > INSN_LAST) {
273 WARN_FUNC("invalid instruction type %d",
274 insn->sec, insn->offset, insn->type);
279 hash_add(file->insn_hash, &insn->hash, insn->offset);
280 list_add_tail(&insn->list, &file->insn_list);
283 list_for_each_entry(func, &sec->symbol_list, list) {
284 if (func->type != STT_FUNC)
287 if (!find_insn(file, sec, func->offset)) {
288 WARN("%s(): can't find starting instruction",
293 func_for_each_insn(file, func, insn)
307 * Mark "ud2" instructions and manually annotated dead ends.
309 static int add_dead_ends(struct objtool_file *file)
313 struct instruction *insn;
317 * By default, "ud2" is a dead end unless otherwise annotated, because
318 * GCC 7 inserts it for certain divide-by-zero cases.
320 for_each_insn(file, insn)
321 if (insn->type == INSN_BUG)
322 insn->dead_end = true;
325 * Check for manually annotated dead ends.
327 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
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);
336 insn = find_insn(file, rela->sym->sec, rela->addend);
338 insn = list_prev_entry(insn, list);
339 else if (rela->addend == rela->sym->sec->len) {
341 list_for_each_entry_reverse(insn, &file->insn_list, list) {
342 if (insn->sec == rela->sym->sec) {
349 WARN("can't find unreachable insn at %s+0x%x",
350 rela->sym->sec->name, rela->addend);
354 WARN("can't find unreachable insn at %s+0x%x",
355 rela->sym->sec->name, rela->addend);
359 insn->dead_end = true;
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
369 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
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);
378 insn = find_insn(file, rela->sym->sec, rela->addend);
380 insn = list_prev_entry(insn, list);
381 else if (rela->addend == rela->sym->sec->len) {
383 list_for_each_entry_reverse(insn, &file->insn_list, list) {
384 if (insn->sec == rela->sym->sec) {
391 WARN("can't find reachable insn at %s+0x%x",
392 rela->sym->sec->name, rela->addend);
396 WARN("can't find reachable insn at %s+0x%x",
397 rela->sym->sec->name, rela->addend);
401 insn->dead_end = false;
408 * Warnings shouldn't be reported for ignored functions.
410 static void add_ignores(struct objtool_file *file)
412 struct instruction *insn;
416 for_each_sec(file, sec) {
417 list_for_each_entry(func, &sec->symbol_list, list) {
418 if (func->type != STT_FUNC)
421 if (!ignore_func(file, func))
424 func_for_each_insn(file, func, insn)
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
436 static int add_nospec_ignores(struct objtool_file *file)
440 struct instruction *insn;
442 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
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);
452 insn = find_insn(file, rela->sym->sec, rela->addend);
454 WARN("bad .discard.nospec entry");
458 insn->ignore_alts = true;
465 * Find the destination instructions for all jumps.
467 static int add_jump_destinations(struct objtool_file *file)
469 struct instruction *insn;
471 struct section *dest_sec;
472 unsigned long dest_off;
474 for_each_insn(file, insn) {
475 if (insn->type != INSN_JUMP_CONDITIONAL &&
476 insn->type != INSN_JUMP_UNCONDITIONAL)
482 rela = find_rela_by_dest_range(insn->sec, insn->offset,
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;
493 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
495 * Retpoline jumps are really dynamic jumps in
496 * disguise, so convert them accordingly.
498 insn->type = INSN_JUMP_DYNAMIC;
506 insn->jump_dest = find_insn(file, dest_sec, dest_off);
507 if (!insn->jump_dest) {
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().
514 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
517 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
518 insn->sec, insn->offset, dest_sec->name,
528 * Find the destination instructions for all calls.
530 static int add_call_destinations(struct objtool_file *file)
532 struct instruction *insn;
533 unsigned long dest_off;
536 for_each_insn(file, insn) {
537 if (insn->type != INSN_CALL)
540 rela = find_rela_by_dest_range(insn->sec, insn->offset,
543 dest_off = insn->offset + insn->len + insn->immediate;
544 insn->call_dest = find_symbol_by_offset(insn->sec,
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.
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);
558 } else if (rela->sym->type == STT_SECTION) {
559 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
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,
570 insn->call_dest = rela->sym;
577 * The .alternatives section requires some extra special care, over and above
578 * what other special sections require:
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.
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.
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.
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".
596 static int handle_group_alt(struct objtool_file *file,
597 struct special_alt *special_alt,
598 struct instruction *orig_insn,
599 struct instruction **new_insn)
601 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
602 unsigned long dest_off;
604 last_orig_insn = NULL;
606 sec_for_each_insn_from(file, insn) {
607 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
610 if (special_alt->skip_orig)
611 insn->type = INSN_NOP;
613 insn->alt_group = true;
614 last_orig_insn = insn;
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);
623 fake_jump = malloc(sizeof(*fake_jump));
625 WARN("malloc failed");
628 memset(fake_jump, 0, sizeof(*fake_jump));
629 INIT_LIST_HEAD(&fake_jump->alts);
630 clear_insn_state(&fake_jump->state);
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);
636 fake_jump->ignore = true;
638 if (!special_alt->new_len) {
639 *new_insn = fake_jump;
643 last_new_insn = NULL;
645 sec_for_each_insn_from(file, insn) {
646 if (insn->offset >= special_alt->new_off + special_alt->new_len)
649 last_new_insn = insn;
651 if (insn->type != INSN_JUMP_CONDITIONAL &&
652 insn->type != INSN_JUMP_UNCONDITIONAL)
655 if (!insn->immediate)
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;
662 if (!insn->jump_dest) {
663 WARN_FUNC("can't find alternative jump destination",
664 insn->sec, insn->offset);
669 if (!last_new_insn) {
670 WARN_FUNC("can't find last new alternative instruction",
671 special_alt->new_sec, special_alt->new_off);
675 list_add(&fake_jump->list, &last_new_insn->list);
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.
685 static int handle_jump_alt(struct objtool_file *file,
686 struct special_alt *special_alt,
687 struct instruction *orig_insn,
688 struct instruction **new_insn)
690 if (orig_insn->type == INSN_NOP)
693 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
694 WARN_FUNC("unsupported instruction at jump label",
695 orig_insn->sec, orig_insn->offset);
699 *new_insn = list_next_entry(orig_insn, list);
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().
709 static int add_special_section_alts(struct objtool_file *file)
711 struct list_head special_alts;
712 struct instruction *orig_insn, *new_insn;
713 struct special_alt *special_alt, *tmp;
714 struct alternative *alt;
717 ret = special_get_alts(file->elf, &special_alts);
721 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
723 orig_insn = find_insn(file, special_alt->orig_sec,
724 special_alt->orig_off);
726 WARN_FUNC("special: can't find orig instruction",
727 special_alt->orig_sec, special_alt->orig_off);
732 /* Ignore retpoline alternatives. */
733 if (orig_insn->ignore_alts)
737 if (!special_alt->group || special_alt->new_len) {
738 new_insn = find_insn(file, special_alt->new_sec,
739 special_alt->new_off);
741 WARN_FUNC("special: can't find new instruction",
742 special_alt->new_sec,
743 special_alt->new_off);
749 if (special_alt->group) {
750 ret = handle_group_alt(file, special_alt, orig_insn,
754 } else if (special_alt->jump_or_nop) {
755 ret = handle_jump_alt(file, special_alt, orig_insn,
761 alt = malloc(sizeof(*alt));
763 WARN("malloc failed");
768 alt->insn = new_insn;
769 list_add_tail(&alt->list, &orig_insn->alts);
771 list_del(&special_alt->list);
779 static int add_switch_table(struct objtool_file *file, struct symbol *func,
780 struct instruction *insn, struct rela *table,
781 struct rela *next_table)
783 struct rela *rela = table;
784 struct instruction *alt_insn;
785 struct alternative *alt;
787 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
788 if (rela == next_table)
791 if (rela->sym->sec != insn->sec ||
792 rela->addend <= func->offset ||
793 rela->addend >= func->offset + func->len)
796 alt_insn = find_insn(file, insn->sec, rela->addend);
798 WARN("%s: can't find instruction at %s+0x%x",
799 file->rodata->rela->name, insn->sec->name,
804 alt = malloc(sizeof(*alt));
806 WARN("malloc failed");
810 alt->insn = alt_insn;
811 list_add_tail(&alt->list, &insn->alts);
818 * find_switch_table() - Given a dynamic jump, find the switch jump table in
819 * .rodata associated with it.
821 * There are 3 basic patterns:
823 * 1. jmpq *[rodata addr](,%reg,8)
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.
828 * 2. jmpq *[rodata addr](%rip)
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.
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.
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.
844 * 3. mov [rodata addr],%reg1
845 * ... some instructions ...
846 * jmpq *(%reg1,%reg2,8)
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.
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.
854 static struct rela *find_switch_table(struct objtool_file *file,
856 struct instruction *insn)
858 struct rela *text_rela, *rodata_rela;
859 struct instruction *orig_insn = insn;
861 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
862 if (text_rela && text_rela->sym == file->rodata->sym) {
864 rodata_rela = find_rela_by_dest(file->rodata,
870 rodata_rela = find_rela_by_dest(file->rodata,
871 text_rela->addend + 4);
874 file->ignore_unreachables = true;
879 func_for_each_insn_continue_reverse(file, func, insn) {
880 if (insn->type == INSN_JUMP_DYNAMIC)
883 /* allow small jumps within the range */
884 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
886 (insn->jump_dest->offset <= insn->offset ||
887 insn->jump_dest->offset > orig_insn->offset))
890 /* look for a relocation which references .rodata */
891 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
893 if (!text_rela || text_rela->sym != file->rodata->sym)
897 * Make sure the .rodata address isn't associated with a
898 * symbol. gcc jump tables are anonymous data.
900 if (find_symbol_containing(file->rodata, text_rela->addend))
903 return find_rela_by_dest(file->rodata, text_rela->addend);
909 static int add_func_switch_tables(struct objtool_file *file,
912 struct instruction *insn, *prev_jump = NULL;
913 struct rela *rela, *prev_rela = NULL;
916 func_for_each_insn(file, func, insn) {
917 if (insn->type != INSN_JUMP_DYNAMIC)
920 rela = find_switch_table(file, func, insn);
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.
930 ret = add_switch_table(file, func, prev_jump, prev_rela,
941 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
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.
954 static int add_switch_table_alts(struct objtool_file *file)
960 if (!file->rodata || !file->rodata->rela)
963 for_each_sec(file, sec) {
964 list_for_each_entry(func, &sec->symbol_list, list) {
965 if (func->type != STT_FUNC)
968 ret = add_func_switch_tables(file, func);
977 static int read_unwind_hints(struct objtool_file *file)
979 struct section *sec, *relasec;
981 struct unwind_hint *hint;
982 struct instruction *insn;
986 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
992 WARN("missing .rela.discard.unwind_hints section");
996 if (sec->len % sizeof(struct unwind_hint)) {
997 WARN("struct unwind_hint size mismatch");
1003 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1004 hint = (struct unwind_hint *)sec->data->d_buf + i;
1006 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1008 WARN("can't find rela for unwind_hints[%d]", i);
1012 insn = find_insn(file, rela->sym->sec, rela->addend);
1014 WARN("can't find insn for unwind_hints[%d]", i);
1018 cfa = &insn->state.cfa;
1020 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1024 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1025 insn->restore = true;
1032 switch (hint->sp_reg) {
1033 case ORC_REG_UNDEFINED:
1034 cfa->base = CFI_UNDEFINED;
1042 case ORC_REG_SP_INDIRECT:
1043 cfa->base = CFI_SP_INDIRECT;
1046 cfa->base = CFI_R10;
1049 cfa->base = CFI_R13;
1058 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1059 insn->sec, insn->offset, hint->sp_reg);
1063 cfa->offset = hint->sp_offset;
1064 insn->state.type = hint->type;
1070 static int decode_sections(struct objtool_file *file)
1074 ret = decode_instructions(file);
1078 ret = add_dead_ends(file);
1084 ret = add_nospec_ignores(file);
1088 ret = add_jump_destinations(file);
1092 ret = add_call_destinations(file);
1096 ret = add_special_section_alts(file);
1100 ret = add_switch_table_alts(file);
1104 ret = read_unwind_hints(file);
1111 static bool is_fentry_call(struct instruction *insn)
1113 if (insn->type == INSN_CALL &&
1114 insn->call_dest->type == STT_NOTYPE &&
1115 !strcmp(insn->call_dest->name, "__fentry__"))
1121 static bool has_modified_stack_frame(struct insn_state *state)
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 ||
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)
1139 static bool has_valid_stack_frame(struct insn_state *state)
1141 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1142 state->regs[CFI_BP].offset == -16)
1145 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1151 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1153 struct cfi_reg *cfa = &state->cfa;
1154 struct stack_op *op = &insn->stack_op;
1156 if (cfa->base != CFI_SP)
1160 if (op->dest.type == OP_DEST_PUSH)
1164 if (op->src.type == OP_SRC_POP)
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;
1175 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1178 if (arch_callee_saved_reg(reg) &&
1179 state->regs[reg].base == CFI_UNDEFINED) {
1180 state->regs[reg].base = base;
1181 state->regs[reg].offset = offset;
1185 static void restore_reg(struct insn_state *state, unsigned char reg)
1187 state->regs[reg].base = CFI_UNDEFINED;
1188 state->regs[reg].offset = 0;
1192 * A note about DRAP stack alignment:
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:
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)
1202 * 48 89 e5 mov %rsp,%rbp
1209 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1212 * There are some variations in the epilogues, like:
1220 * 49 8d 62 f8 lea -0x8(%r10),%rsp
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
1230 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1233 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1234 * restored beforehand:
1237 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1238 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1240 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1244 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1246 struct stack_op *op = &insn->stack_op;
1247 struct cfi_reg *cfa = &state->cfa;
1248 struct cfi_reg *regs = state->regs;
1250 /* stack operations don't make sense with an undefined CFA */
1251 if (cfa->base == CFI_UNDEFINED) {
1253 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1259 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1260 return update_insn_state_regs(insn, state);
1262 switch (op->dest.type) {
1265 switch (op->src.type) {
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) {
1273 /* mov %rsp, %rbp */
1274 cfa->base = op->dest.reg;
1275 state->bp_scratch = false;
1278 else if (op->src.reg == CFI_SP &&
1279 op->dest.reg == CFI_BP && state->drap) {
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;
1287 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1292 * This is needed for the rare case where GCC
1299 state->vals[op->dest.reg].base = CFI_CFA;
1300 state->vals[op->dest.reg].offset = -state->stack_size;
1303 else if (op->dest.reg == cfa->base) {
1305 /* mov %reg, %rsp */
1306 if (cfa->base == CFI_SP &&
1307 state->vals[op->src.reg].base == CFI_CFA) {
1310 * This is needed for the rare case
1311 * where GCC does something dumb like:
1313 * lea 0x8(%rsp), %rcx
1317 cfa->offset = -state->vals[op->src.reg].offset;
1318 state->stack_size = cfa->offset;
1321 cfa->base = CFI_UNDEFINED;
1329 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1332 state->stack_size -= op->src.offset;
1333 if (cfa->base == CFI_SP)
1334 cfa->offset -= op->src.offset;
1338 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1340 /* lea disp(%rbp), %rsp */
1341 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1345 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1347 /* drap: lea disp(%rsp), %drap */
1348 state->drap_reg = op->dest.reg;
1351 * lea disp(%rsp), %reg
1353 * This is needed for the rare case where GCC
1354 * does something dumb like:
1356 * lea 0x8(%rsp), %rcx
1360 state->vals[op->dest.reg].base = CFI_CFA;
1361 state->vals[op->dest.reg].offset = \
1362 -state->stack_size + op->src.offset;
1367 if (state->drap && op->dest.reg == CFI_SP &&
1368 op->src.reg == state->drap_reg) {
1370 /* drap: lea disp(%drap), %rsp */
1372 cfa->offset = state->stack_size = -op->src.offset;
1373 state->drap_reg = CFI_UNDEFINED;
1374 state->drap = false;
1378 if (op->dest.reg == state->cfa.base) {
1379 WARN_FUNC("unsupported stack register modification",
1380 insn->sec, insn->offset);
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);
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;
1403 * Older versions of GCC (4.8ish) realign the stack
1404 * without DRAP, with a frame pointer.
1410 if (!state->drap && op->dest.type == OP_DEST_REG &&
1411 op->dest.reg == cfa->base) {
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) {
1422 /* drap: pop %drap */
1423 cfa->base = state->drap_reg;
1425 state->drap_offset = -1;
1427 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1430 restore_reg(state, op->dest.reg);
1433 state->stack_size -= 8;
1434 if (cfa->base == CFI_SP)
1439 case OP_SRC_REG_INDIRECT:
1440 if (state->drap && op->src.reg == CFI_BP &&
1441 op->src.offset == state->drap_offset) {
1443 /* drap: mov disp(%rbp), %drap */
1444 cfa->base = state->drap_reg;
1446 state->drap_offset = -1;
1449 if (state->drap && op->src.reg == CFI_BP &&
1450 op->src.offset == regs[op->dest.reg].offset) {
1452 /* drap: mov disp(%rbp), %reg */
1453 restore_reg(state, op->dest.reg);
1455 } else if (op->src.reg == cfa->base &&
1456 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1458 /* mov disp(%rbp), %reg */
1459 /* mov disp(%rsp), %reg */
1460 restore_reg(state, op->dest.reg);
1466 WARN_FUNC("unknown stack-related instruction",
1467 insn->sec, insn->offset);
1474 state->stack_size += 8;
1475 if (cfa->base == CFI_SP)
1478 if (op->src.type != OP_SRC_REG)
1482 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1484 /* drap: push %drap */
1485 cfa->base = CFI_BP_INDIRECT;
1486 cfa->offset = -state->stack_size;
1488 /* save drap so we know when to restore it */
1489 state->drap_offset = -state->stack_size;
1491 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1493 /* drap: push %rbp */
1494 state->stack_size = 0;
1496 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1498 /* drap: push %reg */
1499 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1505 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1508 /* detect when asm code uses rbp as a scratch register */
1509 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1510 cfa->base != CFI_BP)
1511 state->bp_scratch = true;
1514 case OP_DEST_REG_INDIRECT:
1517 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1519 /* drap: mov %drap, disp(%rbp) */
1520 cfa->base = CFI_BP_INDIRECT;
1521 cfa->offset = op->dest.offset;
1523 /* save drap offset so we know when to restore it */
1524 state->drap_offset = op->dest.offset;
1527 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1529 /* drap: mov reg, disp(%rbp) */
1530 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1533 } else if (op->dest.reg == cfa->base) {
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);
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);
1551 /* leave (mov %rbp, %rsp; pop %rbp) */
1553 state->stack_size = -state->regs[CFI_BP].offset - 8;
1554 restore_reg(state, CFI_BP);
1564 if (op->src.type != OP_SRC_POP) {
1565 WARN_FUNC("unknown stack-related memory operation",
1566 insn->sec, insn->offset);
1571 state->stack_size -= 8;
1572 if (cfa->base == CFI_SP)
1578 WARN_FUNC("unknown stack-related instruction",
1579 insn->sec, insn->offset);
1586 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1588 struct insn_state *state1 = &insn->state, *state2 = state;
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);
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)))
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);
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);
1614 } else if (state1->drap != state2->drap ||
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)",
1618 insn->sec, insn->offset,
1619 state1->drap, state1->drap_reg, state1->drap_offset,
1620 state2->drap, state2->drap_reg, state2->drap_offset);
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.
1634 static int validate_branch(struct objtool_file *file, struct instruction *first,
1635 struct insn_state state)
1637 struct alternative *alt;
1638 struct instruction *insn, *next_insn;
1639 struct section *sec;
1640 struct symbol *func = NULL;
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",
1653 next_insn = next_insn_same_sec(file, insn);
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);
1665 if (func && insn->ignore) {
1666 WARN_FUNC("BUG: why am I validating an ignored function?",
1671 if (insn->visited) {
1672 if (!insn->hint && !insn_state_match(insn, &state))
1679 if (insn->restore) {
1680 struct instruction *save_insn, *i;
1684 func_for_each_insn_continue_reverse(file, func, i) {
1692 WARN_FUNC("no corresponding CFI save for CFI restore",
1697 if (!save_insn->visited) {
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
1708 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1713 insn->state = save_insn->state;
1716 state = insn->state;
1719 insn->state = state;
1721 insn->visited = true;
1723 list_for_each_entry(alt, &insn->alts, list) {
1724 ret = validate_branch(file, alt->insn, state);
1729 switch (insn->type) {
1732 if (func && has_modified_stack_frame(&state)) {
1733 WARN_FUNC("return with modified stack frame",
1738 if (state.bp_scratch) {
1739 WARN("%s uses BP as a scratch register",
1747 if (is_fentry_call(insn))
1750 ret = dead_end_function(file, insn->call_dest);
1757 case INSN_CALL_DYNAMIC:
1758 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1759 WARN_FUNC("call without frame pointer save/setup",
1765 case INSN_JUMP_CONDITIONAL:
1766 case INSN_JUMP_UNCONDITIONAL:
1767 if (insn->jump_dest &&
1768 (!func || !insn->jump_dest->func ||
1769 func == insn->jump_dest->func)) {
1770 ret = validate_branch(file, insn->jump_dest,
1775 } else if (func && has_modified_stack_frame(&state)) {
1776 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1781 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1786 case INSN_JUMP_DYNAMIC:
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",
1796 case INSN_CONTEXT_SWITCH:
1797 if (func && (!next_insn || !next_insn->hint)) {
1798 WARN_FUNC("unsupported instruction in callable function",
1805 if (update_insn_state(insn, &state))
1818 if (state.cfa.base == CFI_UNDEFINED)
1820 WARN("%s: unexpected end of section", sec->name);
1830 static int validate_unwind_hints(struct objtool_file *file)
1832 struct instruction *insn;
1833 int ret, warnings = 0;
1834 struct insn_state state;
1839 clear_insn_state(&state);
1841 for_each_insn(file, insn) {
1842 if (insn->hint && !insn->visited) {
1843 ret = validate_branch(file, insn, state);
1851 static bool is_kasan_insn(struct instruction *insn)
1853 return (insn->type == INSN_CALL &&
1854 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1857 static bool is_ubsan_insn(struct instruction *insn)
1859 return (insn->type == INSN_CALL &&
1860 !strcmp(insn->call_dest->name,
1861 "__ubsan_handle_builtin_unreachable"));
1864 static bool ignore_unreachable_insn(struct instruction *insn)
1868 if (insn->ignore || insn->type == INSN_NOP)
1872 * Ignore any unused exceptions. This can happen when a whitelisted
1873 * function has an exception table entry.
1875 * Also ignore alternative replacement instructions. This can happen
1876 * when a whitelisted function uses one of the ALTERNATIVE macros.
1878 if (!strcmp(insn->sec->name, ".fixup") ||
1879 !strcmp(insn->sec->name, ".altinstr_replacement") ||
1880 !strcmp(insn->sec->name, ".altinstr_aux"))
1884 * Check if this (or a subsequent) instruction is related to
1885 * CONFIG_UBSAN or CONFIG_KASAN.
1887 * End the search at 5 instructions to avoid going into the weeds.
1891 for (i = 0; i < 5; i++) {
1893 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1896 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1897 insn = insn->jump_dest;
1901 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
1903 insn = list_next_entry(insn, list);
1909 static int validate_functions(struct objtool_file *file)
1911 struct section *sec;
1912 struct symbol *func;
1913 struct instruction *insn;
1914 struct insn_state state;
1915 int ret, warnings = 0;
1917 clear_insn_state(&state);
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;
1924 for_each_sec(file, sec) {
1925 list_for_each_entry(func, &sec->symbol_list, list) {
1926 if (func->type != STT_FUNC)
1929 insn = find_insn(file, sec, func->offset);
1930 if (!insn || insn->ignore)
1933 ret = validate_branch(file, insn, state);
1941 static int validate_reachable_instructions(struct objtool_file *file)
1943 struct instruction *insn;
1945 if (file->ignore_unreachables)
1948 for_each_insn(file, insn) {
1949 if (insn->visited || ignore_unreachable_insn(insn))
1952 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
1959 static void cleanup(struct objtool_file *file)
1961 struct instruction *insn, *tmpinsn;
1962 struct alternative *alt, *tmpalt;
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);
1969 list_del(&insn->list);
1970 hash_del(&insn->hash);
1973 elf_close(file->elf);
1976 int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc)
1978 struct objtool_file file;
1979 int ret, warnings = 0;
1984 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
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");
1992 file.c_file = find_section_by_name(file.elf, ".comment");
1993 file.ignore_unreachables = no_unreachable;
1996 arch_initial_func_cfi_state(&initial_func_cfi);
1998 ret = decode_sections(&file);
2003 if (list_empty(&file.insn_list))
2006 ret = validate_functions(&file);
2011 ret = validate_unwind_hints(&file);
2017 ret = validate_reachable_instructions(&file);
2024 ret = create_orc(&file);
2028 ret = create_orc_sections(&file);
2032 ret = elf_write(file.elf);
2040 /* ignore warnings for now until we get all the code cleaned up */
2041 if (ret || warnings)