2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
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 * Find the destination instructions for all jumps.
433 static int add_jump_destinations(struct objtool_file *file)
435 struct instruction *insn;
437 struct section *dest_sec;
438 unsigned long dest_off;
440 for_each_insn(file, insn) {
441 if (insn->type != INSN_JUMP_CONDITIONAL &&
442 insn->type != INSN_JUMP_UNCONDITIONAL)
448 rela = find_rela_by_dest_range(insn->sec, insn->offset,
451 dest_sec = insn->sec;
452 dest_off = insn->offset + insn->len + insn->immediate;
453 } else if (rela->sym->type == STT_SECTION) {
454 dest_sec = rela->sym->sec;
455 dest_off = rela->addend + 4;
456 } else if (rela->sym->sec->idx) {
457 dest_sec = rela->sym->sec;
458 dest_off = rela->sym->sym.st_value + rela->addend + 4;
459 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
461 * Retpoline jumps are really dynamic jumps in
462 * disguise, so convert them accordingly.
464 insn->type = INSN_JUMP_DYNAMIC;
472 insn->jump_dest = find_insn(file, dest_sec, dest_off);
473 if (!insn->jump_dest) {
476 * This is a special case where an alt instruction
477 * jumps past the end of the section. These are
478 * handled later in handle_group_alt().
480 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
483 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
484 insn->sec, insn->offset, dest_sec->name,
494 * Find the destination instructions for all calls.
496 static int add_call_destinations(struct objtool_file *file)
498 struct instruction *insn;
499 unsigned long dest_off;
502 for_each_insn(file, insn) {
503 if (insn->type != INSN_CALL)
506 rela = find_rela_by_dest_range(insn->sec, insn->offset,
509 dest_off = insn->offset + insn->len + insn->immediate;
510 insn->call_dest = find_symbol_by_offset(insn->sec,
512 if (!insn->call_dest) {
513 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
514 insn->sec, insn->offset, dest_off);
517 } else if (rela->sym->type == STT_SECTION) {
518 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
520 if (!insn->call_dest ||
521 insn->call_dest->type != STT_FUNC) {
522 WARN_FUNC("can't find call dest symbol at %s+0x%x",
523 insn->sec, insn->offset,
524 rela->sym->sec->name,
529 insn->call_dest = rela->sym;
536 * The .alternatives section requires some extra special care, over and above
537 * what other special sections require:
539 * 1. Because alternatives are patched in-place, we need to insert a fake jump
540 * instruction at the end so that validate_branch() skips all the original
541 * replaced instructions when validating the new instruction path.
543 * 2. An added wrinkle is that the new instruction length might be zero. In
544 * that case the old instructions are replaced with noops. We simulate that
545 * by creating a fake jump as the only new instruction.
547 * 3. In some cases, the alternative section includes an instruction which
548 * conditionally jumps to the _end_ of the entry. We have to modify these
549 * jumps' destinations to point back to .text rather than the end of the
550 * entry in .altinstr_replacement.
552 * 4. It has been requested that we don't validate the !POPCNT feature path
553 * which is a "very very small percentage of machines".
555 static int handle_group_alt(struct objtool_file *file,
556 struct special_alt *special_alt,
557 struct instruction *orig_insn,
558 struct instruction **new_insn)
560 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
561 unsigned long dest_off;
563 last_orig_insn = NULL;
565 sec_for_each_insn_from(file, insn) {
566 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
569 if (special_alt->skip_orig)
570 insn->type = INSN_NOP;
572 insn->alt_group = true;
573 last_orig_insn = insn;
576 if (!next_insn_same_sec(file, last_orig_insn)) {
577 WARN("%s: don't know how to handle alternatives at end of section",
578 special_alt->orig_sec->name);
582 fake_jump = malloc(sizeof(*fake_jump));
584 WARN("malloc failed");
587 memset(fake_jump, 0, sizeof(*fake_jump));
588 INIT_LIST_HEAD(&fake_jump->alts);
589 clear_insn_state(&fake_jump->state);
591 fake_jump->sec = special_alt->new_sec;
592 fake_jump->offset = -1;
593 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
594 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
595 fake_jump->ignore = true;
597 if (!special_alt->new_len) {
598 *new_insn = fake_jump;
602 last_new_insn = NULL;
604 sec_for_each_insn_from(file, insn) {
605 if (insn->offset >= special_alt->new_off + special_alt->new_len)
608 last_new_insn = insn;
610 if (insn->type != INSN_JUMP_CONDITIONAL &&
611 insn->type != INSN_JUMP_UNCONDITIONAL)
614 if (!insn->immediate)
617 dest_off = insn->offset + insn->len + insn->immediate;
618 if (dest_off == special_alt->new_off + special_alt->new_len)
619 insn->jump_dest = fake_jump;
621 if (!insn->jump_dest) {
622 WARN_FUNC("can't find alternative jump destination",
623 insn->sec, insn->offset);
628 if (!last_new_insn) {
629 WARN_FUNC("can't find last new alternative instruction",
630 special_alt->new_sec, special_alt->new_off);
634 list_add(&fake_jump->list, &last_new_insn->list);
640 * A jump table entry can either convert a nop to a jump or a jump to a nop.
641 * If the original instruction is a jump, make the alt entry an effective nop
642 * by just skipping the original instruction.
644 static int handle_jump_alt(struct objtool_file *file,
645 struct special_alt *special_alt,
646 struct instruction *orig_insn,
647 struct instruction **new_insn)
649 if (orig_insn->type == INSN_NOP)
652 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
653 WARN_FUNC("unsupported instruction at jump label",
654 orig_insn->sec, orig_insn->offset);
658 *new_insn = list_next_entry(orig_insn, list);
663 * Read all the special sections which have alternate instructions which can be
664 * patched in or redirected to at runtime. Each instruction having alternate
665 * instruction(s) has them added to its insn->alts list, which will be
666 * traversed in validate_branch().
668 static int add_special_section_alts(struct objtool_file *file)
670 struct list_head special_alts;
671 struct instruction *orig_insn, *new_insn;
672 struct special_alt *special_alt, *tmp;
673 struct alternative *alt;
676 ret = special_get_alts(file->elf, &special_alts);
680 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
681 alt = malloc(sizeof(*alt));
683 WARN("malloc failed");
688 orig_insn = find_insn(file, special_alt->orig_sec,
689 special_alt->orig_off);
691 WARN_FUNC("special: can't find orig instruction",
692 special_alt->orig_sec, special_alt->orig_off);
698 if (!special_alt->group || special_alt->new_len) {
699 new_insn = find_insn(file, special_alt->new_sec,
700 special_alt->new_off);
702 WARN_FUNC("special: can't find new instruction",
703 special_alt->new_sec,
704 special_alt->new_off);
710 if (special_alt->group) {
711 ret = handle_group_alt(file, special_alt, orig_insn,
715 } else if (special_alt->jump_or_nop) {
716 ret = handle_jump_alt(file, special_alt, orig_insn,
722 alt->insn = new_insn;
723 list_add_tail(&alt->list, &orig_insn->alts);
725 list_del(&special_alt->list);
733 static int add_switch_table(struct objtool_file *file, struct symbol *func,
734 struct instruction *insn, struct rela *table,
735 struct rela *next_table)
737 struct rela *rela = table;
738 struct instruction *alt_insn;
739 struct alternative *alt;
741 list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
742 if (rela == next_table)
745 if (rela->sym->sec != insn->sec ||
746 rela->addend <= func->offset ||
747 rela->addend >= func->offset + func->len)
750 alt_insn = find_insn(file, insn->sec, rela->addend);
752 WARN("%s: can't find instruction at %s+0x%x",
753 file->rodata->rela->name, insn->sec->name,
758 alt = malloc(sizeof(*alt));
760 WARN("malloc failed");
764 alt->insn = alt_insn;
765 list_add_tail(&alt->list, &insn->alts);
772 * find_switch_table() - Given a dynamic jump, find the switch jump table in
773 * .rodata associated with it.
775 * There are 3 basic patterns:
777 * 1. jmpq *[rodata addr](,%reg,8)
779 * This is the most common case by far. It jumps to an address in a simple
780 * jump table which is stored in .rodata.
782 * 2. jmpq *[rodata addr](%rip)
784 * This is caused by a rare GCC quirk, currently only seen in three driver
785 * functions in the kernel, only with certain obscure non-distro configs.
787 * As part of an optimization, GCC makes a copy of an existing switch jump
788 * table, modifies it, and then hard-codes the jump (albeit with an indirect
789 * jump) to use a single entry in the table. The rest of the jump table and
790 * some of its jump targets remain as dead code.
792 * In such a case we can just crudely ignore all unreachable instruction
793 * warnings for the entire object file. Ideally we would just ignore them
794 * for the function, but that would require redesigning the code quite a
795 * bit. And honestly that's just not worth doing: unreachable instruction
796 * warnings are of questionable value anyway, and this is such a rare issue.
798 * 3. mov [rodata addr],%reg1
799 * ... some instructions ...
800 * jmpq *(%reg1,%reg2,8)
802 * This is a fairly uncommon pattern which is new for GCC 6. As of this
803 * writing, there are 11 occurrences of it in the allmodconfig kernel.
805 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
806 * ensure the same register is used in the mov and jump instructions.
808 static struct rela *find_switch_table(struct objtool_file *file,
810 struct instruction *insn)
812 struct rela *text_rela, *rodata_rela;
813 struct instruction *orig_insn = insn;
815 text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
816 if (text_rela && text_rela->sym == file->rodata->sym) {
818 rodata_rela = find_rela_by_dest(file->rodata,
824 rodata_rela = find_rela_by_dest(file->rodata,
825 text_rela->addend + 4);
828 file->ignore_unreachables = true;
833 func_for_each_insn_continue_reverse(file, func, insn) {
834 if (insn->type == INSN_JUMP_DYNAMIC)
837 /* allow small jumps within the range */
838 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
840 (insn->jump_dest->offset <= insn->offset ||
841 insn->jump_dest->offset > orig_insn->offset))
844 /* look for a relocation which references .rodata */
845 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
847 if (!text_rela || text_rela->sym != file->rodata->sym)
851 * Make sure the .rodata address isn't associated with a
852 * symbol. gcc jump tables are anonymous data.
854 if (find_symbol_containing(file->rodata, text_rela->addend))
857 return find_rela_by_dest(file->rodata, text_rela->addend);
863 static int add_func_switch_tables(struct objtool_file *file,
866 struct instruction *insn, *prev_jump = NULL;
867 struct rela *rela, *prev_rela = NULL;
870 func_for_each_insn(file, func, insn) {
871 if (insn->type != INSN_JUMP_DYNAMIC)
874 rela = find_switch_table(file, func, insn);
879 * We found a switch table, but we don't know yet how big it
880 * is. Don't add it until we reach the end of the function or
881 * the beginning of another switch table in the same function.
884 ret = add_switch_table(file, func, prev_jump, prev_rela,
895 ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
904 * For some switch statements, gcc generates a jump table in the .rodata
905 * section which contains a list of addresses within the function to jump to.
906 * This finds these jump tables and adds them to the insn->alts lists.
908 static int add_switch_table_alts(struct objtool_file *file)
914 if (!file->rodata || !file->rodata->rela)
917 for_each_sec(file, sec) {
918 list_for_each_entry(func, &sec->symbol_list, list) {
919 if (func->type != STT_FUNC)
922 ret = add_func_switch_tables(file, func);
931 static int read_unwind_hints(struct objtool_file *file)
933 struct section *sec, *relasec;
935 struct unwind_hint *hint;
936 struct instruction *insn;
940 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
946 WARN("missing .rela.discard.unwind_hints section");
950 if (sec->len % sizeof(struct unwind_hint)) {
951 WARN("struct unwind_hint size mismatch");
957 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
958 hint = (struct unwind_hint *)sec->data->d_buf + i;
960 rela = find_rela_by_dest(sec, i * sizeof(*hint));
962 WARN("can't find rela for unwind_hints[%d]", i);
966 insn = find_insn(file, rela->sym->sec, rela->addend);
968 WARN("can't find insn for unwind_hints[%d]", i);
972 cfa = &insn->state.cfa;
974 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
978 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
979 insn->restore = true;
986 switch (hint->sp_reg) {
987 case ORC_REG_UNDEFINED:
988 cfa->base = CFI_UNDEFINED;
996 case ORC_REG_SP_INDIRECT:
997 cfa->base = CFI_SP_INDIRECT;
1000 cfa->base = CFI_R10;
1003 cfa->base = CFI_R13;
1012 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1013 insn->sec, insn->offset, hint->sp_reg);
1017 cfa->offset = hint->sp_offset;
1018 insn->state.type = hint->type;
1024 static int decode_sections(struct objtool_file *file)
1028 ret = decode_instructions(file);
1032 ret = add_dead_ends(file);
1038 ret = add_jump_destinations(file);
1042 ret = add_call_destinations(file);
1046 ret = add_special_section_alts(file);
1050 ret = add_switch_table_alts(file);
1054 ret = read_unwind_hints(file);
1061 static bool is_fentry_call(struct instruction *insn)
1063 if (insn->type == INSN_CALL &&
1064 insn->call_dest->type == STT_NOTYPE &&
1065 !strcmp(insn->call_dest->name, "__fentry__"))
1071 static bool has_modified_stack_frame(struct insn_state *state)
1075 if (state->cfa.base != initial_func_cfi.cfa.base ||
1076 state->cfa.offset != initial_func_cfi.cfa.offset ||
1077 state->stack_size != initial_func_cfi.cfa.offset ||
1081 for (i = 0; i < CFI_NUM_REGS; i++)
1082 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1083 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1089 static bool has_valid_stack_frame(struct insn_state *state)
1091 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1092 state->regs[CFI_BP].offset == -16)
1095 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1101 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1103 struct cfi_reg *cfa = &state->cfa;
1104 struct stack_op *op = &insn->stack_op;
1106 if (cfa->base != CFI_SP)
1110 if (op->dest.type == OP_DEST_PUSH)
1114 if (op->src.type == OP_SRC_POP)
1117 /* add immediate to sp */
1118 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1119 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1120 cfa->offset -= op->src.offset;
1125 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1128 if (arch_callee_saved_reg(reg) &&
1129 state->regs[reg].base == CFI_UNDEFINED) {
1130 state->regs[reg].base = base;
1131 state->regs[reg].offset = offset;
1135 static void restore_reg(struct insn_state *state, unsigned char reg)
1137 state->regs[reg].base = CFI_UNDEFINED;
1138 state->regs[reg].offset = 0;
1142 * A note about DRAP stack alignment:
1144 * GCC has the concept of a DRAP register, which is used to help keep track of
1145 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1146 * register. The typical DRAP pattern is:
1148 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1149 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1150 * 41 ff 72 f8 pushq -0x8(%r10)
1152 * 48 89 e5 mov %rsp,%rbp
1159 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1162 * There are some variations in the epilogues, like:
1170 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1175 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1176 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1177 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1178 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1180 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1183 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1184 * restored beforehand:
1187 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1188 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1190 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1194 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1196 struct stack_op *op = &insn->stack_op;
1197 struct cfi_reg *cfa = &state->cfa;
1198 struct cfi_reg *regs = state->regs;
1200 /* stack operations don't make sense with an undefined CFA */
1201 if (cfa->base == CFI_UNDEFINED) {
1203 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1209 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1210 return update_insn_state_regs(insn, state);
1212 switch (op->dest.type) {
1215 switch (op->src.type) {
1218 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1219 cfa->base == CFI_SP &&
1220 regs[CFI_BP].base == CFI_CFA &&
1221 regs[CFI_BP].offset == -cfa->offset) {
1223 /* mov %rsp, %rbp */
1224 cfa->base = op->dest.reg;
1225 state->bp_scratch = false;
1228 else if (op->src.reg == CFI_SP &&
1229 op->dest.reg == CFI_BP && state->drap) {
1231 /* drap: mov %rsp, %rbp */
1232 regs[CFI_BP].base = CFI_BP;
1233 regs[CFI_BP].offset = -state->stack_size;
1234 state->bp_scratch = false;
1237 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1242 * This is needed for the rare case where GCC
1249 state->vals[op->dest.reg].base = CFI_CFA;
1250 state->vals[op->dest.reg].offset = -state->stack_size;
1253 else if (op->dest.reg == cfa->base) {
1255 /* mov %reg, %rsp */
1256 if (cfa->base == CFI_SP &&
1257 state->vals[op->src.reg].base == CFI_CFA) {
1260 * This is needed for the rare case
1261 * where GCC does something dumb like:
1263 * lea 0x8(%rsp), %rcx
1267 cfa->offset = -state->vals[op->src.reg].offset;
1268 state->stack_size = cfa->offset;
1271 cfa->base = CFI_UNDEFINED;
1279 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1282 state->stack_size -= op->src.offset;
1283 if (cfa->base == CFI_SP)
1284 cfa->offset -= op->src.offset;
1288 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1290 /* lea disp(%rbp), %rsp */
1291 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1295 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1297 /* drap: lea disp(%rsp), %drap */
1298 state->drap_reg = op->dest.reg;
1301 * lea disp(%rsp), %reg
1303 * This is needed for the rare case where GCC
1304 * does something dumb like:
1306 * lea 0x8(%rsp), %rcx
1310 state->vals[op->dest.reg].base = CFI_CFA;
1311 state->vals[op->dest.reg].offset = \
1312 -state->stack_size + op->src.offset;
1317 if (state->drap && op->dest.reg == CFI_SP &&
1318 op->src.reg == state->drap_reg) {
1320 /* drap: lea disp(%drap), %rsp */
1322 cfa->offset = state->stack_size = -op->src.offset;
1323 state->drap_reg = CFI_UNDEFINED;
1324 state->drap = false;
1328 if (op->dest.reg == state->cfa.base) {
1329 WARN_FUNC("unsupported stack register modification",
1330 insn->sec, insn->offset);
1337 if (op->dest.reg != CFI_SP ||
1338 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1339 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1340 WARN_FUNC("unsupported stack pointer realignment",
1341 insn->sec, insn->offset);
1345 if (state->drap_reg != CFI_UNDEFINED) {
1346 /* drap: and imm, %rsp */
1347 cfa->base = state->drap_reg;
1348 cfa->offset = state->stack_size = 0;
1353 * Older versions of GCC (4.8ish) realign the stack
1354 * without DRAP, with a frame pointer.
1360 if (!state->drap && op->dest.type == OP_DEST_REG &&
1361 op->dest.reg == cfa->base) {
1367 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1368 op->dest.type == OP_DEST_REG &&
1369 op->dest.reg == state->drap_reg &&
1370 state->drap_offset == -state->stack_size) {
1372 /* drap: pop %drap */
1373 cfa->base = state->drap_reg;
1375 state->drap_offset = -1;
1377 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1380 restore_reg(state, op->dest.reg);
1383 state->stack_size -= 8;
1384 if (cfa->base == CFI_SP)
1389 case OP_SRC_REG_INDIRECT:
1390 if (state->drap && op->src.reg == CFI_BP &&
1391 op->src.offset == state->drap_offset) {
1393 /* drap: mov disp(%rbp), %drap */
1394 cfa->base = state->drap_reg;
1396 state->drap_offset = -1;
1399 if (state->drap && op->src.reg == CFI_BP &&
1400 op->src.offset == regs[op->dest.reg].offset) {
1402 /* drap: mov disp(%rbp), %reg */
1403 restore_reg(state, op->dest.reg);
1405 } else if (op->src.reg == cfa->base &&
1406 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1408 /* mov disp(%rbp), %reg */
1409 /* mov disp(%rsp), %reg */
1410 restore_reg(state, op->dest.reg);
1416 WARN_FUNC("unknown stack-related instruction",
1417 insn->sec, insn->offset);
1424 state->stack_size += 8;
1425 if (cfa->base == CFI_SP)
1428 if (op->src.type != OP_SRC_REG)
1432 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1434 /* drap: push %drap */
1435 cfa->base = CFI_BP_INDIRECT;
1436 cfa->offset = -state->stack_size;
1438 /* save drap so we know when to restore it */
1439 state->drap_offset = -state->stack_size;
1441 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1443 /* drap: push %rbp */
1444 state->stack_size = 0;
1446 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1448 /* drap: push %reg */
1449 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1455 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1458 /* detect when asm code uses rbp as a scratch register */
1459 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1460 cfa->base != CFI_BP)
1461 state->bp_scratch = true;
1464 case OP_DEST_REG_INDIRECT:
1467 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1469 /* drap: mov %drap, disp(%rbp) */
1470 cfa->base = CFI_BP_INDIRECT;
1471 cfa->offset = op->dest.offset;
1473 /* save drap offset so we know when to restore it */
1474 state->drap_offset = op->dest.offset;
1477 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1479 /* drap: mov reg, disp(%rbp) */
1480 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1483 } else if (op->dest.reg == cfa->base) {
1485 /* mov reg, disp(%rbp) */
1486 /* mov reg, disp(%rsp) */
1487 save_reg(state, op->src.reg, CFI_CFA,
1488 op->dest.offset - state->cfa.offset);
1494 if ((!state->drap && cfa->base != CFI_BP) ||
1495 (state->drap && cfa->base != state->drap_reg)) {
1496 WARN_FUNC("leave instruction with modified stack frame",
1497 insn->sec, insn->offset);
1501 /* leave (mov %rbp, %rsp; pop %rbp) */
1503 state->stack_size = -state->regs[CFI_BP].offset - 8;
1504 restore_reg(state, CFI_BP);
1514 if (op->src.type != OP_SRC_POP) {
1515 WARN_FUNC("unknown stack-related memory operation",
1516 insn->sec, insn->offset);
1521 state->stack_size -= 8;
1522 if (cfa->base == CFI_SP)
1528 WARN_FUNC("unknown stack-related instruction",
1529 insn->sec, insn->offset);
1536 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1538 struct insn_state *state1 = &insn->state, *state2 = state;
1541 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1542 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1543 insn->sec, insn->offset,
1544 state1->cfa.base, state1->cfa.offset,
1545 state2->cfa.base, state2->cfa.offset);
1547 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1548 for (i = 0; i < CFI_NUM_REGS; i++) {
1549 if (!memcmp(&state1->regs[i], &state2->regs[i],
1550 sizeof(struct cfi_reg)))
1553 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1554 insn->sec, insn->offset,
1555 i, state1->regs[i].base, state1->regs[i].offset,
1556 i, state2->regs[i].base, state2->regs[i].offset);
1560 } else if (state1->type != state2->type) {
1561 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1562 insn->sec, insn->offset, state1->type, state2->type);
1564 } else if (state1->drap != state2->drap ||
1565 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1566 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1567 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1568 insn->sec, insn->offset,
1569 state1->drap, state1->drap_reg, state1->drap_offset,
1570 state2->drap, state2->drap_reg, state2->drap_offset);
1579 * Follow the branch starting at the given instruction, and recursively follow
1580 * any other branches (jumps). Meanwhile, track the frame pointer state at
1581 * each instruction and validate all the rules described in
1582 * tools/objtool/Documentation/stack-validation.txt.
1584 static int validate_branch(struct objtool_file *file, struct instruction *first,
1585 struct insn_state state)
1587 struct alternative *alt;
1588 struct instruction *insn, *next_insn;
1589 struct section *sec;
1590 struct symbol *func = NULL;
1596 if (insn->alt_group && list_empty(&insn->alts)) {
1597 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1603 next_insn = next_insn_same_sec(file, insn);
1606 if (file->c_file && func && insn->func && func != insn->func) {
1607 WARN("%s() falls through to next function %s()",
1608 func->name, insn->func->name);
1615 if (func && insn->ignore) {
1616 WARN_FUNC("BUG: why am I validating an ignored function?",
1621 if (insn->visited) {
1622 if (!insn->hint && !insn_state_match(insn, &state))
1629 if (insn->restore) {
1630 struct instruction *save_insn, *i;
1634 func_for_each_insn_continue_reverse(file, func, i) {
1642 WARN_FUNC("no corresponding CFI save for CFI restore",
1647 if (!save_insn->visited) {
1649 * Oops, no state to copy yet.
1650 * Hopefully we can reach this
1651 * instruction from another branch
1652 * after the save insn has been
1658 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1663 insn->state = save_insn->state;
1666 state = insn->state;
1669 insn->state = state;
1671 insn->visited = true;
1673 list_for_each_entry(alt, &insn->alts, list) {
1674 ret = validate_branch(file, alt->insn, state);
1679 switch (insn->type) {
1682 if (func && has_modified_stack_frame(&state)) {
1683 WARN_FUNC("return with modified stack frame",
1688 if (state.bp_scratch) {
1689 WARN("%s uses BP as a scratch register",
1697 if (is_fentry_call(insn))
1700 ret = dead_end_function(file, insn->call_dest);
1707 case INSN_CALL_DYNAMIC:
1708 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1709 WARN_FUNC("call without frame pointer save/setup",
1715 case INSN_JUMP_CONDITIONAL:
1716 case INSN_JUMP_UNCONDITIONAL:
1717 if (insn->jump_dest &&
1718 (!func || !insn->jump_dest->func ||
1719 func == insn->jump_dest->func)) {
1720 ret = validate_branch(file, insn->jump_dest,
1725 } else if (func && has_modified_stack_frame(&state)) {
1726 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1731 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1736 case INSN_JUMP_DYNAMIC:
1737 if (func && list_empty(&insn->alts) &&
1738 has_modified_stack_frame(&state)) {
1739 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1746 case INSN_CONTEXT_SWITCH:
1747 if (func && (!next_insn || !next_insn->hint)) {
1748 WARN_FUNC("unsupported instruction in callable function",
1755 if (update_insn_state(insn, &state))
1768 if (state.cfa.base == CFI_UNDEFINED)
1770 WARN("%s: unexpected end of section", sec->name);
1780 static int validate_unwind_hints(struct objtool_file *file)
1782 struct instruction *insn;
1783 int ret, warnings = 0;
1784 struct insn_state state;
1789 clear_insn_state(&state);
1791 for_each_insn(file, insn) {
1792 if (insn->hint && !insn->visited) {
1793 ret = validate_branch(file, insn, state);
1801 static bool is_kasan_insn(struct instruction *insn)
1803 return (insn->type == INSN_CALL &&
1804 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
1807 static bool is_ubsan_insn(struct instruction *insn)
1809 return (insn->type == INSN_CALL &&
1810 !strcmp(insn->call_dest->name,
1811 "__ubsan_handle_builtin_unreachable"));
1814 static bool ignore_unreachable_insn(struct instruction *insn)
1818 if (insn->ignore || insn->type == INSN_NOP)
1822 * Ignore any unused exceptions. This can happen when a whitelisted
1823 * function has an exception table entry.
1825 * Also ignore alternative replacement instructions. This can happen
1826 * when a whitelisted function uses one of the ALTERNATIVE macros.
1828 if (!strcmp(insn->sec->name, ".fixup") ||
1829 !strcmp(insn->sec->name, ".altinstr_replacement") ||
1830 !strcmp(insn->sec->name, ".altinstr_aux"))
1834 * Check if this (or a subsequent) instruction is related to
1835 * CONFIG_UBSAN or CONFIG_KASAN.
1837 * End the search at 5 instructions to avoid going into the weeds.
1841 for (i = 0; i < 5; i++) {
1843 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
1846 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
1847 insn = insn->jump_dest;
1851 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
1853 insn = list_next_entry(insn, list);
1859 static int validate_functions(struct objtool_file *file)
1861 struct section *sec;
1862 struct symbol *func;
1863 struct instruction *insn;
1864 struct insn_state state;
1865 int ret, warnings = 0;
1867 clear_insn_state(&state);
1869 state.cfa = initial_func_cfi.cfa;
1870 memcpy(&state.regs, &initial_func_cfi.regs,
1871 CFI_NUM_REGS * sizeof(struct cfi_reg));
1872 state.stack_size = initial_func_cfi.cfa.offset;
1874 for_each_sec(file, sec) {
1875 list_for_each_entry(func, &sec->symbol_list, list) {
1876 if (func->type != STT_FUNC)
1879 insn = find_insn(file, sec, func->offset);
1880 if (!insn || insn->ignore)
1883 ret = validate_branch(file, insn, state);
1891 static int validate_reachable_instructions(struct objtool_file *file)
1893 struct instruction *insn;
1895 if (file->ignore_unreachables)
1898 for_each_insn(file, insn) {
1899 if (insn->visited || ignore_unreachable_insn(insn))
1902 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
1909 static void cleanup(struct objtool_file *file)
1911 struct instruction *insn, *tmpinsn;
1912 struct alternative *alt, *tmpalt;
1914 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
1915 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1916 list_del(&alt->list);
1919 list_del(&insn->list);
1920 hash_del(&insn->hash);
1923 elf_close(file->elf);
1926 int check(const char *_objname, bool _no_fp, bool no_unreachable, bool orc)
1928 struct objtool_file file;
1929 int ret, warnings = 0;
1934 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
1938 INIT_LIST_HEAD(&file.insn_list);
1939 hash_init(file.insn_hash);
1940 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
1941 file.rodata = find_section_by_name(file.elf, ".rodata");
1942 file.c_file = find_section_by_name(file.elf, ".comment");
1943 file.ignore_unreachables = no_unreachable;
1946 arch_initial_func_cfi_state(&initial_func_cfi);
1948 ret = decode_sections(&file);
1953 if (list_empty(&file.insn_list))
1956 ret = validate_functions(&file);
1961 ret = validate_unwind_hints(&file);
1967 ret = validate_reachable_instructions(&file);
1974 ret = create_orc(&file);
1978 ret = create_orc_sections(&file);
1982 ret = elf_write(file.elf);
1990 /* ignore warnings for now until we get all the code cleaned up */
1991 if (ret || warnings)