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 static 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 static bool gcov_enabled(struct objtool_file
*file
)
67 for_each_sec(file
, sec
)
68 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
69 if (!strncmp(sym
->name
, "__gcov_.", 8))
75 #define func_for_each_insn(file, func, insn) \
76 for (insn = find_insn(file, func->sec, func->offset); \
77 insn && &insn->list != &file->insn_list && \
78 insn->sec == func->sec && \
79 insn->offset < func->offset + func->len; \
80 insn = list_next_entry(insn, list))
82 #define func_for_each_insn_continue_reverse(file, func, insn) \
83 for (insn = list_prev_entry(insn, list); \
84 &insn->list != &file->insn_list && \
85 insn->sec == func->sec && insn->offset >= func->offset; \
86 insn = list_prev_entry(insn, list))
88 #define sec_for_each_insn_from(file, insn) \
89 for (; insn; insn = next_insn_same_sec(file, insn))
91 #define sec_for_each_insn_continue(file, insn) \
92 for (insn = next_insn_same_sec(file, insn); insn; \
93 insn = next_insn_same_sec(file, insn))
96 * Check if the function has been manually whitelisted with the
97 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
98 * due to its use of a context switching instruction.
100 static bool ignore_func(struct objtool_file
*file
, struct symbol
*func
)
103 struct instruction
*insn
;
105 /* check for STACK_FRAME_NON_STANDARD */
106 if (file
->whitelist
&& file
->whitelist
->rela
)
107 list_for_each_entry(rela
, &file
->whitelist
->rela
->rela_list
, list
) {
108 if (rela
->sym
->type
== STT_SECTION
&&
109 rela
->sym
->sec
== func
->sec
&&
110 rela
->addend
== func
->offset
)
112 if (rela
->sym
->type
== STT_FUNC
&& rela
->sym
== func
)
116 /* check if it has a context switching instruction */
117 func_for_each_insn(file
, func
, insn
)
118 if (insn
->type
== INSN_CONTEXT_SWITCH
)
125 * This checks to see if the given function is a "noreturn" function.
127 * For global functions which are outside the scope of this object file, we
128 * have to keep a manual list of them.
130 * For local functions, we have to detect them manually by simply looking for
131 * the lack of a return instruction.
138 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
142 struct instruction
*insn
;
146 * Unfortunately these have to be hard coded because the noreturn
147 * attribute isn't provided in ELF data.
149 static const char * const global_noreturns
[] = {
154 "__module_put_and_exit",
156 "kvm_spurious_fault",
162 if (func
->bind
== STB_WEAK
)
165 if (func
->bind
== STB_GLOBAL
)
166 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
167 if (!strcmp(func
->name
, global_noreturns
[i
]))
173 func_for_each_insn(file
, func
, insn
) {
176 if (insn
->type
== INSN_RETURN
)
184 * A function can have a sibling call instead of a return. In that
185 * case, the function's dead-end status depends on whether the target
186 * of the sibling call returns.
188 func_for_each_insn(file
, func
, insn
) {
189 if (insn
->sec
!= func
->sec
||
190 insn
->offset
>= func
->offset
+ func
->len
)
193 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
194 struct instruction
*dest
= insn
->jump_dest
;
195 struct symbol
*dest_func
;
198 /* sibling call to another file */
201 if (dest
->sec
!= func
->sec
||
202 dest
->offset
< func
->offset
||
203 dest
->offset
>= func
->offset
+ func
->len
) {
204 /* local sibling call */
205 dest_func
= find_symbol_by_offset(dest
->sec
,
210 if (recursion
== 5) {
211 WARN_FUNC("infinite recursion (objtool bug!)",
212 dest
->sec
, dest
->offset
);
216 return __dead_end_function(file
, dest_func
,
221 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
229 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
231 return __dead_end_function(file
, func
, 0);
234 static void clear_insn_state(struct insn_state
*state
)
238 memset(state
, 0, sizeof(*state
));
239 state
->cfa
.base
= CFI_UNDEFINED
;
240 for (i
= 0; i
< CFI_NUM_REGS
; i
++)
241 state
->regs
[i
].base
= CFI_UNDEFINED
;
242 state
->drap_reg
= CFI_UNDEFINED
;
246 * Call the arch-specific instruction decoder for all the instructions and add
247 * them to the global instruction list.
249 static int decode_instructions(struct objtool_file
*file
)
253 unsigned long offset
;
254 struct instruction
*insn
;
257 for_each_sec(file
, sec
) {
259 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
262 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
263 insn
= malloc(sizeof(*insn
));
265 WARN("malloc failed");
268 memset(insn
, 0, sizeof(*insn
));
269 INIT_LIST_HEAD(&insn
->alts
);
270 clear_insn_state(&insn
->state
);
273 insn
->offset
= offset
;
275 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
277 &insn
->len
, &insn
->type
,
283 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
284 WARN_FUNC("invalid instruction type %d",
285 insn
->sec
, insn
->offset
, insn
->type
);
289 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
290 list_add_tail(&insn
->list
, &file
->insn_list
);
293 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
294 if (func
->type
!= STT_FUNC
)
297 if (!find_insn(file
, sec
, func
->offset
)) {
298 WARN("%s(): can't find starting instruction",
303 func_for_each_insn(file
, func
, insn
)
313 * Find all uses of the unreachable() macro, which are code path dead ends.
315 static int add_dead_ends(struct objtool_file
*file
)
319 struct instruction
*insn
;
322 sec
= find_section_by_name(file
->elf
, ".rela.discard.unreachable");
326 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
327 if (rela
->sym
->type
!= STT_SECTION
) {
328 WARN("unexpected relocation symbol type in %s", sec
->name
);
331 insn
= find_insn(file
, rela
->sym
->sec
, rela
->addend
);
333 insn
= list_prev_entry(insn
, list
);
334 else if (rela
->addend
== rela
->sym
->sec
->len
) {
336 list_for_each_entry_reverse(insn
, &file
->insn_list
, list
) {
337 if (insn
->sec
== rela
->sym
->sec
) {
344 WARN("can't find unreachable insn at %s+0x%x",
345 rela
->sym
->sec
->name
, rela
->addend
);
349 WARN("can't find unreachable insn at %s+0x%x",
350 rela
->sym
->sec
->name
, rela
->addend
);
354 insn
->dead_end
= true;
361 * Warnings shouldn't be reported for ignored functions.
363 static void add_ignores(struct objtool_file
*file
)
365 struct instruction
*insn
;
369 for_each_sec(file
, sec
) {
370 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
371 if (func
->type
!= STT_FUNC
)
374 if (!ignore_func(file
, func
))
377 func_for_each_insn(file
, func
, insn
)
384 * Find the destination instructions for all jumps.
386 static int add_jump_destinations(struct objtool_file
*file
)
388 struct instruction
*insn
;
390 struct section
*dest_sec
;
391 unsigned long dest_off
;
393 for_each_insn(file
, insn
) {
394 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
395 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
401 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
404 dest_sec
= insn
->sec
;
405 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
406 } else if (rela
->sym
->type
== STT_SECTION
) {
407 dest_sec
= rela
->sym
->sec
;
408 dest_off
= rela
->addend
+ 4;
409 } else if (rela
->sym
->sec
->idx
) {
410 dest_sec
= rela
->sym
->sec
;
411 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
418 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
419 if (!insn
->jump_dest
) {
422 * This is a special case where an alt instruction
423 * jumps past the end of the section. These are
424 * handled later in handle_group_alt().
426 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
429 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
430 insn
->sec
, insn
->offset
, dest_sec
->name
,
440 * Find the destination instructions for all calls.
442 static int add_call_destinations(struct objtool_file
*file
)
444 struct instruction
*insn
;
445 unsigned long dest_off
;
448 for_each_insn(file
, insn
) {
449 if (insn
->type
!= INSN_CALL
)
452 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
455 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
456 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
458 if (!insn
->call_dest
) {
459 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
460 insn
->sec
, insn
->offset
, dest_off
);
463 } else if (rela
->sym
->type
== STT_SECTION
) {
464 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
466 if (!insn
->call_dest
||
467 insn
->call_dest
->type
!= STT_FUNC
) {
468 WARN_FUNC("can't find call dest symbol at %s+0x%x",
469 insn
->sec
, insn
->offset
,
470 rela
->sym
->sec
->name
,
475 insn
->call_dest
= rela
->sym
;
482 * The .alternatives section requires some extra special care, over and above
483 * what other special sections require:
485 * 1. Because alternatives are patched in-place, we need to insert a fake jump
486 * instruction at the end so that validate_branch() skips all the original
487 * replaced instructions when validating the new instruction path.
489 * 2. An added wrinkle is that the new instruction length might be zero. In
490 * that case the old instructions are replaced with noops. We simulate that
491 * by creating a fake jump as the only new instruction.
493 * 3. In some cases, the alternative section includes an instruction which
494 * conditionally jumps to the _end_ of the entry. We have to modify these
495 * jumps' destinations to point back to .text rather than the end of the
496 * entry in .altinstr_replacement.
498 * 4. It has been requested that we don't validate the !POPCNT feature path
499 * which is a "very very small percentage of machines".
501 static int handle_group_alt(struct objtool_file
*file
,
502 struct special_alt
*special_alt
,
503 struct instruction
*orig_insn
,
504 struct instruction
**new_insn
)
506 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
;
507 unsigned long dest_off
;
509 last_orig_insn
= NULL
;
511 sec_for_each_insn_from(file
, insn
) {
512 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
515 if (special_alt
->skip_orig
)
516 insn
->type
= INSN_NOP
;
518 insn
->alt_group
= true;
519 last_orig_insn
= insn
;
522 if (!next_insn_same_sec(file
, last_orig_insn
)) {
523 WARN("%s: don't know how to handle alternatives at end of section",
524 special_alt
->orig_sec
->name
);
528 fake_jump
= malloc(sizeof(*fake_jump
));
530 WARN("malloc failed");
533 memset(fake_jump
, 0, sizeof(*fake_jump
));
534 INIT_LIST_HEAD(&fake_jump
->alts
);
535 clear_insn_state(&fake_jump
->state
);
537 fake_jump
->sec
= special_alt
->new_sec
;
538 fake_jump
->offset
= -1;
539 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
540 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
541 fake_jump
->ignore
= true;
543 if (!special_alt
->new_len
) {
544 *new_insn
= fake_jump
;
548 last_new_insn
= NULL
;
550 sec_for_each_insn_from(file
, insn
) {
551 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
554 last_new_insn
= insn
;
556 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
557 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
560 if (!insn
->immediate
)
563 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
564 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
565 insn
->jump_dest
= fake_jump
;
567 if (!insn
->jump_dest
) {
568 WARN_FUNC("can't find alternative jump destination",
569 insn
->sec
, insn
->offset
);
574 if (!last_new_insn
) {
575 WARN_FUNC("can't find last new alternative instruction",
576 special_alt
->new_sec
, special_alt
->new_off
);
580 list_add(&fake_jump
->list
, &last_new_insn
->list
);
586 * A jump table entry can either convert a nop to a jump or a jump to a nop.
587 * If the original instruction is a jump, make the alt entry an effective nop
588 * by just skipping the original instruction.
590 static int handle_jump_alt(struct objtool_file
*file
,
591 struct special_alt
*special_alt
,
592 struct instruction
*orig_insn
,
593 struct instruction
**new_insn
)
595 if (orig_insn
->type
== INSN_NOP
)
598 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
599 WARN_FUNC("unsupported instruction at jump label",
600 orig_insn
->sec
, orig_insn
->offset
);
604 *new_insn
= list_next_entry(orig_insn
, list
);
609 * Read all the special sections which have alternate instructions which can be
610 * patched in or redirected to at runtime. Each instruction having alternate
611 * instruction(s) has them added to its insn->alts list, which will be
612 * traversed in validate_branch().
614 static int add_special_section_alts(struct objtool_file
*file
)
616 struct list_head special_alts
;
617 struct instruction
*orig_insn
, *new_insn
;
618 struct special_alt
*special_alt
, *tmp
;
619 struct alternative
*alt
;
622 ret
= special_get_alts(file
->elf
, &special_alts
);
626 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
627 alt
= malloc(sizeof(*alt
));
629 WARN("malloc failed");
634 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
635 special_alt
->orig_off
);
637 WARN_FUNC("special: can't find orig instruction",
638 special_alt
->orig_sec
, special_alt
->orig_off
);
644 if (!special_alt
->group
|| special_alt
->new_len
) {
645 new_insn
= find_insn(file
, special_alt
->new_sec
,
646 special_alt
->new_off
);
648 WARN_FUNC("special: can't find new instruction",
649 special_alt
->new_sec
,
650 special_alt
->new_off
);
656 if (special_alt
->group
) {
657 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
661 } else if (special_alt
->jump_or_nop
) {
662 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
668 alt
->insn
= new_insn
;
669 list_add_tail(&alt
->list
, &orig_insn
->alts
);
671 list_del(&special_alt
->list
);
679 static int add_switch_table(struct objtool_file
*file
, struct symbol
*func
,
680 struct instruction
*insn
, struct rela
*table
,
681 struct rela
*next_table
)
683 struct rela
*rela
= table
;
684 struct instruction
*alt_insn
;
685 struct alternative
*alt
;
687 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
688 if (rela
== next_table
)
691 if (rela
->sym
->sec
!= insn
->sec
||
692 rela
->addend
<= func
->offset
||
693 rela
->addend
>= func
->offset
+ func
->len
)
696 alt_insn
= find_insn(file
, insn
->sec
, rela
->addend
);
698 WARN("%s: can't find instruction at %s+0x%x",
699 file
->rodata
->rela
->name
, insn
->sec
->name
,
704 alt
= malloc(sizeof(*alt
));
706 WARN("malloc failed");
710 alt
->insn
= alt_insn
;
711 list_add_tail(&alt
->list
, &insn
->alts
);
718 * find_switch_table() - Given a dynamic jump, find the switch jump table in
719 * .rodata associated with it.
721 * There are 3 basic patterns:
723 * 1. jmpq *[rodata addr](,%reg,8)
725 * This is the most common case by far. It jumps to an address in a simple
726 * jump table which is stored in .rodata.
728 * 2. jmpq *[rodata addr](%rip)
730 * This is caused by a rare GCC quirk, currently only seen in three driver
731 * functions in the kernel, only with certain obscure non-distro configs.
733 * As part of an optimization, GCC makes a copy of an existing switch jump
734 * table, modifies it, and then hard-codes the jump (albeit with an indirect
735 * jump) to use a single entry in the table. The rest of the jump table and
736 * some of its jump targets remain as dead code.
738 * In such a case we can just crudely ignore all unreachable instruction
739 * warnings for the entire object file. Ideally we would just ignore them
740 * for the function, but that would require redesigning the code quite a
741 * bit. And honestly that's just not worth doing: unreachable instruction
742 * warnings are of questionable value anyway, and this is such a rare issue.
744 * 3. mov [rodata addr],%reg1
745 * ... some instructions ...
746 * jmpq *(%reg1,%reg2,8)
748 * This is a fairly uncommon pattern which is new for GCC 6. As of this
749 * writing, there are 11 occurrences of it in the allmodconfig kernel.
751 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
752 * ensure the same register is used in the mov and jump instructions.
754 static struct rela
*find_switch_table(struct objtool_file
*file
,
756 struct instruction
*insn
)
758 struct rela
*text_rela
, *rodata_rela
;
759 struct instruction
*orig_insn
= insn
;
761 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
, insn
->len
);
762 if (text_rela
&& text_rela
->sym
== file
->rodata
->sym
) {
764 rodata_rela
= find_rela_by_dest(file
->rodata
,
770 rodata_rela
= find_rela_by_dest(file
->rodata
,
771 text_rela
->addend
+ 4);
774 file
->ignore_unreachables
= true;
779 func_for_each_insn_continue_reverse(file
, func
, insn
) {
780 if (insn
->type
== INSN_JUMP_DYNAMIC
)
783 /* allow small jumps within the range */
784 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&&
786 (insn
->jump_dest
->offset
<= insn
->offset
||
787 insn
->jump_dest
->offset
> orig_insn
->offset
))
790 /* look for a relocation which references .rodata */
791 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
793 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
797 * Make sure the .rodata address isn't associated with a
798 * symbol. gcc jump tables are anonymous data.
800 if (find_symbol_containing(file
->rodata
, text_rela
->addend
))
803 return find_rela_by_dest(file
->rodata
, text_rela
->addend
);
809 static int add_func_switch_tables(struct objtool_file
*file
,
812 struct instruction
*insn
, *prev_jump
= NULL
;
813 struct rela
*rela
, *prev_rela
= NULL
;
816 func_for_each_insn(file
, func
, insn
) {
817 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
820 rela
= find_switch_table(file
, func
, insn
);
825 * We found a switch table, but we don't know yet how big it
826 * is. Don't add it until we reach the end of the function or
827 * the beginning of another switch table in the same function.
830 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
,
841 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
, NULL
);
850 * For some switch statements, gcc generates a jump table in the .rodata
851 * section which contains a list of addresses within the function to jump to.
852 * This finds these jump tables and adds them to the insn->alts lists.
854 static int add_switch_table_alts(struct objtool_file
*file
)
860 if (!file
->rodata
|| !file
->rodata
->rela
)
863 for_each_sec(file
, sec
) {
864 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
865 if (func
->type
!= STT_FUNC
)
868 ret
= add_func_switch_tables(file
, func
);
877 static int decode_sections(struct objtool_file
*file
)
881 ret
= decode_instructions(file
);
885 ret
= add_dead_ends(file
);
891 ret
= add_jump_destinations(file
);
895 ret
= add_call_destinations(file
);
899 ret
= add_special_section_alts(file
);
903 ret
= add_switch_table_alts(file
);
910 static bool is_fentry_call(struct instruction
*insn
)
912 if (insn
->type
== INSN_CALL
&&
913 insn
->call_dest
->type
== STT_NOTYPE
&&
914 !strcmp(insn
->call_dest
->name
, "__fentry__"))
920 static bool has_modified_stack_frame(struct insn_state
*state
)
924 if (state
->cfa
.base
!= initial_func_cfi
.cfa
.base
||
925 state
->cfa
.offset
!= initial_func_cfi
.cfa
.offset
||
926 state
->stack_size
!= initial_func_cfi
.cfa
.offset
||
930 for (i
= 0; i
< CFI_NUM_REGS
; i
++)
931 if (state
->regs
[i
].base
!= initial_func_cfi
.regs
[i
].base
||
932 state
->regs
[i
].offset
!= initial_func_cfi
.regs
[i
].offset
)
938 static bool has_valid_stack_frame(struct insn_state
*state
)
940 if (state
->cfa
.base
== CFI_BP
&& state
->regs
[CFI_BP
].base
== CFI_CFA
&&
941 state
->regs
[CFI_BP
].offset
== -16)
944 if (state
->drap
&& state
->regs
[CFI_BP
].base
== CFI_BP
)
950 static void save_reg(struct insn_state
*state
, unsigned char reg
, int base
,
953 if ((arch_callee_saved_reg(reg
) ||
954 (state
->drap
&& reg
== state
->drap_reg
)) &&
955 state
->regs
[reg
].base
== CFI_UNDEFINED
) {
956 state
->regs
[reg
].base
= base
;
957 state
->regs
[reg
].offset
= offset
;
961 static void restore_reg(struct insn_state
*state
, unsigned char reg
)
963 state
->regs
[reg
].base
= CFI_UNDEFINED
;
964 state
->regs
[reg
].offset
= 0;
968 * A note about DRAP stack alignment:
970 * GCC has the concept of a DRAP register, which is used to help keep track of
971 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
972 * register. The typical DRAP pattern is:
974 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
975 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
976 * 41 ff 72 f8 pushq -0x8(%r10)
978 * 48 89 e5 mov %rsp,%rbp
985 * 49 8d 62 f8 lea -0x8(%r10),%rsp
988 * There are some variations in the epilogues, like:
996 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1001 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1002 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1003 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1004 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1006 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1009 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1010 * restored beforehand:
1013 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1014 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1016 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1020 static int update_insn_state(struct instruction
*insn
, struct insn_state
*state
)
1022 struct stack_op
*op
= &insn
->stack_op
;
1023 struct cfi_reg
*cfa
= &state
->cfa
;
1024 struct cfi_reg
*regs
= state
->regs
;
1026 /* stack operations don't make sense with an undefined CFA */
1027 if (cfa
->base
== CFI_UNDEFINED
) {
1029 WARN_FUNC("undefined stack state", insn
->sec
, insn
->offset
);
1035 switch (op
->dest
.type
) {
1038 switch (op
->src
.type
) {
1041 if (cfa
->base
== op
->src
.reg
&& cfa
->base
== CFI_SP
&&
1042 op
->dest
.reg
== CFI_BP
&& regs
[CFI_BP
].base
== CFI_CFA
&&
1043 regs
[CFI_BP
].offset
== -cfa
->offset
) {
1045 /* mov %rsp, %rbp */
1046 cfa
->base
= op
->dest
.reg
;
1047 state
->bp_scratch
= false;
1048 } else if (state
->drap
) {
1050 /* drap: mov %rsp, %rbp */
1051 regs
[CFI_BP
].base
= CFI_BP
;
1052 regs
[CFI_BP
].offset
= -state
->stack_size
;
1053 state
->bp_scratch
= false;
1056 WARN_FUNC("unknown stack-related register move",
1057 insn
->sec
, insn
->offset
);
1064 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_SP
) {
1067 state
->stack_size
-= op
->src
.offset
;
1068 if (cfa
->base
== CFI_SP
)
1069 cfa
->offset
-= op
->src
.offset
;
1073 if (op
->dest
.reg
== CFI_SP
&& op
->src
.reg
== CFI_BP
) {
1075 /* lea disp(%rbp), %rsp */
1076 state
->stack_size
= -(op
->src
.offset
+ regs
[CFI_BP
].offset
);
1080 if (op
->dest
.reg
!= CFI_BP
&& op
->src
.reg
== CFI_SP
&&
1081 cfa
->base
== CFI_SP
) {
1083 /* drap: lea disp(%rsp), %drap */
1084 state
->drap_reg
= op
->dest
.reg
;
1088 if (state
->drap
&& op
->dest
.reg
== CFI_SP
&&
1089 op
->src
.reg
== state
->drap_reg
) {
1091 /* drap: lea disp(%drap), %rsp */
1093 cfa
->offset
= state
->stack_size
= -op
->src
.offset
;
1094 state
->drap_reg
= CFI_UNDEFINED
;
1095 state
->drap
= false;
1099 if (op
->dest
.reg
== state
->cfa
.base
) {
1100 WARN_FUNC("unsupported stack register modification",
1101 insn
->sec
, insn
->offset
);
1108 if (op
->dest
.reg
!= CFI_SP
||
1109 (state
->drap_reg
!= CFI_UNDEFINED
&& cfa
->base
!= CFI_SP
) ||
1110 (state
->drap_reg
== CFI_UNDEFINED
&& cfa
->base
!= CFI_BP
)) {
1111 WARN_FUNC("unsupported stack pointer realignment",
1112 insn
->sec
, insn
->offset
);
1116 if (state
->drap_reg
!= CFI_UNDEFINED
) {
1117 /* drap: and imm, %rsp */
1118 cfa
->base
= state
->drap_reg
;
1119 cfa
->offset
= state
->stack_size
= 0;
1125 * Older versions of GCC (4.8ish) realign the stack
1126 * without DRAP, with a frame pointer.
1132 if (!state
->drap
&& op
->dest
.type
== OP_DEST_REG
&&
1133 op
->dest
.reg
== cfa
->base
) {
1139 if (regs
[op
->dest
.reg
].offset
== -state
->stack_size
) {
1141 if (state
->drap
&& cfa
->base
== CFI_BP_INDIRECT
&&
1142 op
->dest
.type
== OP_DEST_REG
&&
1143 op
->dest
.reg
== state
->drap_reg
) {
1145 /* drap: pop %drap */
1146 cfa
->base
= state
->drap_reg
;
1150 restore_reg(state
, op
->dest
.reg
);
1153 state
->stack_size
-= 8;
1154 if (cfa
->base
== CFI_SP
)
1159 case OP_SRC_REG_INDIRECT
:
1160 if (state
->drap
&& op
->src
.reg
== CFI_BP
&&
1161 op
->src
.offset
== regs
[op
->dest
.reg
].offset
) {
1163 /* drap: mov disp(%rbp), %reg */
1164 if (op
->dest
.reg
== state
->drap_reg
) {
1165 cfa
->base
= state
->drap_reg
;
1169 restore_reg(state
, op
->dest
.reg
);
1171 } else if (op
->src
.reg
== cfa
->base
&&
1172 op
->src
.offset
== regs
[op
->dest
.reg
].offset
+ cfa
->offset
) {
1174 /* mov disp(%rbp), %reg */
1175 /* mov disp(%rsp), %reg */
1176 restore_reg(state
, op
->dest
.reg
);
1182 WARN_FUNC("unknown stack-related instruction",
1183 insn
->sec
, insn
->offset
);
1190 state
->stack_size
+= 8;
1191 if (cfa
->base
== CFI_SP
)
1194 if (op
->src
.type
!= OP_SRC_REG
)
1198 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1200 /* drap: push %drap */
1201 cfa
->base
= CFI_BP_INDIRECT
;
1202 cfa
->offset
= -state
->stack_size
;
1204 /* save drap so we know when to undefine it */
1205 save_reg(state
, op
->src
.reg
, CFI_CFA
, -state
->stack_size
);
1207 } else if (op
->src
.reg
== CFI_BP
&& cfa
->base
== state
->drap_reg
) {
1209 /* drap: push %rbp */
1210 state
->stack_size
= 0;
1212 } else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1214 /* drap: push %reg */
1215 save_reg(state
, op
->src
.reg
, CFI_BP
, -state
->stack_size
);
1221 save_reg(state
, op
->src
.reg
, CFI_CFA
, -state
->stack_size
);
1224 /* detect when asm code uses rbp as a scratch register */
1225 if (!nofp
&& insn
->func
&& op
->src
.reg
== CFI_BP
&&
1226 cfa
->base
!= CFI_BP
)
1227 state
->bp_scratch
= true;
1230 case OP_DEST_REG_INDIRECT
:
1233 if (op
->src
.reg
== cfa
->base
&& op
->src
.reg
== state
->drap_reg
) {
1235 /* drap: mov %drap, disp(%rbp) */
1236 cfa
->base
= CFI_BP_INDIRECT
;
1237 cfa
->offset
= op
->dest
.offset
;
1239 /* save drap so we know when to undefine it */
1240 save_reg(state
, op
->src
.reg
, CFI_CFA
, op
->dest
.offset
);
1243 else if (regs
[op
->src
.reg
].base
== CFI_UNDEFINED
) {
1245 /* drap: mov reg, disp(%rbp) */
1246 save_reg(state
, op
->src
.reg
, CFI_BP
, op
->dest
.offset
);
1249 } else if (op
->dest
.reg
== cfa
->base
) {
1251 /* mov reg, disp(%rbp) */
1252 /* mov reg, disp(%rsp) */
1253 save_reg(state
, op
->src
.reg
, CFI_CFA
,
1254 op
->dest
.offset
- state
->cfa
.offset
);
1260 if ((!state
->drap
&& cfa
->base
!= CFI_BP
) ||
1261 (state
->drap
&& cfa
->base
!= state
->drap_reg
)) {
1262 WARN_FUNC("leave instruction with modified stack frame",
1263 insn
->sec
, insn
->offset
);
1267 /* leave (mov %rbp, %rsp; pop %rbp) */
1269 state
->stack_size
= -state
->regs
[CFI_BP
].offset
- 8;
1270 restore_reg(state
, CFI_BP
);
1280 if (op
->src
.type
!= OP_SRC_POP
) {
1281 WARN_FUNC("unknown stack-related memory operation",
1282 insn
->sec
, insn
->offset
);
1287 state
->stack_size
-= 8;
1288 if (cfa
->base
== CFI_SP
)
1294 WARN_FUNC("unknown stack-related instruction",
1295 insn
->sec
, insn
->offset
);
1302 static bool insn_state_match(struct instruction
*insn
, struct insn_state
*state
)
1304 struct insn_state
*state1
= &insn
->state
, *state2
= state
;
1307 if (memcmp(&state1
->cfa
, &state2
->cfa
, sizeof(state1
->cfa
))) {
1308 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1309 insn
->sec
, insn
->offset
,
1310 state1
->cfa
.base
, state1
->cfa
.offset
,
1311 state2
->cfa
.base
, state2
->cfa
.offset
);
1313 } else if (memcmp(&state1
->regs
, &state2
->regs
, sizeof(state1
->regs
))) {
1314 for (i
= 0; i
< CFI_NUM_REGS
; i
++) {
1315 if (!memcmp(&state1
->regs
[i
], &state2
->regs
[i
],
1316 sizeof(struct cfi_reg
)))
1319 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1320 insn
->sec
, insn
->offset
,
1321 i
, state1
->regs
[i
].base
, state1
->regs
[i
].offset
,
1322 i
, state2
->regs
[i
].base
, state2
->regs
[i
].offset
);
1326 } else if (state1
->drap
!= state2
->drap
||
1327 (state1
->drap
&& state1
->drap_reg
!= state2
->drap_reg
)) {
1328 WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
1329 insn
->sec
, insn
->offset
,
1330 state1
->drap
, state1
->drap_reg
,
1331 state2
->drap
, state2
->drap_reg
);
1340 * Follow the branch starting at the given instruction, and recursively follow
1341 * any other branches (jumps). Meanwhile, track the frame pointer state at
1342 * each instruction and validate all the rules described in
1343 * tools/objtool/Documentation/stack-validation.txt.
1345 static int validate_branch(struct objtool_file
*file
, struct instruction
*first
,
1346 struct insn_state state
)
1348 struct alternative
*alt
;
1349 struct instruction
*insn
;
1350 struct section
*sec
;
1351 struct symbol
*func
= NULL
;
1357 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
1358 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1364 if (file
->c_file
&& insn
->func
) {
1365 if (func
&& func
!= insn
->func
) {
1366 WARN("%s() falls through to next function %s()",
1367 func
->name
, insn
->func
->name
);
1374 if (func
&& insn
->ignore
) {
1375 WARN_FUNC("BUG: why am I validating an ignored function?",
1380 if (insn
->visited
) {
1381 if (!!insn_state_match(insn
, &state
))
1387 insn
->state
= state
;
1389 insn
->visited
= true;
1391 list_for_each_entry(alt
, &insn
->alts
, list
) {
1392 ret
= validate_branch(file
, alt
->insn
, state
);
1397 switch (insn
->type
) {
1400 if (func
&& has_modified_stack_frame(&state
)) {
1401 WARN_FUNC("return with modified stack frame",
1406 if (state
.bp_scratch
) {
1407 WARN("%s uses BP as a scratch register",
1415 if (is_fentry_call(insn
))
1418 ret
= dead_end_function(file
, insn
->call_dest
);
1425 case INSN_CALL_DYNAMIC
:
1426 if (!nofp
&& func
&& !has_valid_stack_frame(&state
)) {
1427 WARN_FUNC("call without frame pointer save/setup",
1433 case INSN_JUMP_CONDITIONAL
:
1434 case INSN_JUMP_UNCONDITIONAL
:
1435 if (insn
->jump_dest
&&
1436 (!func
|| !insn
->jump_dest
->func
||
1437 func
== insn
->jump_dest
->func
)) {
1438 ret
= validate_branch(file
, insn
->jump_dest
,
1443 } else if (func
&& has_modified_stack_frame(&state
)) {
1444 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1449 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
1454 case INSN_JUMP_DYNAMIC
:
1455 if (func
&& list_empty(&insn
->alts
) &&
1456 has_modified_stack_frame(&state
)) {
1457 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1465 if (update_insn_state(insn
, &state
))
1477 insn
= next_insn_same_sec(file
, insn
);
1479 WARN("%s: unexpected end of section", sec
->name
);
1487 static bool is_kasan_insn(struct instruction
*insn
)
1489 return (insn
->type
== INSN_CALL
&&
1490 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1493 static bool is_ubsan_insn(struct instruction
*insn
)
1495 return (insn
->type
== INSN_CALL
&&
1496 !strcmp(insn
->call_dest
->name
,
1497 "__ubsan_handle_builtin_unreachable"));
1500 static bool ignore_unreachable_insn(struct instruction
*insn
)
1504 if (insn
->ignore
|| insn
->type
== INSN_NOP
)
1508 * Ignore any unused exceptions. This can happen when a whitelisted
1509 * function has an exception table entry.
1511 if (!strcmp(insn
->sec
->name
, ".fixup"))
1515 * Check if this (or a subsequent) instruction is related to
1516 * CONFIG_UBSAN or CONFIG_KASAN.
1518 * End the search at 5 instructions to avoid going into the weeds.
1522 for (i
= 0; i
< 5; i
++) {
1524 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
1527 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
) {
1528 insn
= insn
->jump_dest
;
1532 if (insn
->offset
+ insn
->len
>= insn
->func
->offset
+ insn
->func
->len
)
1534 insn
= list_next_entry(insn
, list
);
1540 static int validate_functions(struct objtool_file
*file
)
1542 struct section
*sec
;
1543 struct symbol
*func
;
1544 struct instruction
*insn
;
1545 struct insn_state state
;
1546 int ret
, warnings
= 0;
1548 clear_insn_state(&state
);
1550 state
.cfa
= initial_func_cfi
.cfa
;
1551 memcpy(&state
.regs
, &initial_func_cfi
.regs
,
1552 CFI_NUM_REGS
* sizeof(struct cfi_reg
));
1553 state
.stack_size
= initial_func_cfi
.cfa
.offset
;
1555 for_each_sec(file
, sec
) {
1556 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1557 if (func
->type
!= STT_FUNC
)
1560 insn
= find_insn(file
, sec
, func
->offset
);
1561 if (!insn
|| insn
->ignore
)
1564 ret
= validate_branch(file
, insn
, state
);
1572 static int validate_reachable_instructions(struct objtool_file
*file
)
1574 struct instruction
*insn
;
1576 if (file
->ignore_unreachables
)
1579 for_each_insn(file
, insn
) {
1580 if (insn
->visited
|| ignore_unreachable_insn(insn
))
1584 * gcov produces a lot of unreachable instructions. If we get
1585 * an unreachable warning and the file has gcov enabled, just
1586 * ignore it, and all other such warnings for the file. Do
1587 * this here because this is an expensive function.
1589 if (gcov_enabled(file
))
1592 WARN_FUNC("unreachable instruction", insn
->sec
, insn
->offset
);
1599 static void cleanup(struct objtool_file
*file
)
1601 struct instruction
*insn
, *tmpinsn
;
1602 struct alternative
*alt
, *tmpalt
;
1604 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
1605 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
1606 list_del(&alt
->list
);
1609 list_del(&insn
->list
);
1610 hash_del(&insn
->hash
);
1613 elf_close(file
->elf
);
1616 int check(const char *_objname
, bool _nofp
)
1618 struct objtool_file file
;
1619 int ret
, warnings
= 0;
1624 file
.elf
= elf_open(objname
);
1628 INIT_LIST_HEAD(&file
.insn_list
);
1629 hash_init(file
.insn_hash
);
1630 file
.whitelist
= find_section_by_name(file
.elf
, ".discard.func_stack_frame_non_standard");
1631 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
1632 file
.ignore_unreachables
= false;
1633 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
1635 arch_initial_func_cfi_state(&initial_func_cfi
);
1637 ret
= decode_sections(&file
);
1642 if (list_empty(&file
.insn_list
))
1645 ret
= validate_functions(&file
);
1651 ret
= validate_reachable_instructions(&file
);
1660 /* ignore warnings for now until we get all the code cleaned up */
1661 if (ret
|| warnings
)