]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/objtool/builtin-check.c
2 * Copyright (C) 2015 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/>.
21 * This command analyzes every .o file and ensures the validity of its stack
22 * trace metadata. It enforces a set of rules on asm code and C inline
23 * assembly code so that stack traces can be reliable.
25 * For more information, see tools/objtool/Documentation/stack-validation.txt.
30 #include <subcmd/parse-options.h>
38 #include <linux/hashtable.h>
40 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
42 #define STATE_FP_SAVED 0x1
43 #define STATE_FP_SETUP 0x2
44 #define STATE_FENTRY 0x4
47 struct list_head list
;
48 struct hlist_node hash
;
51 unsigned int len
, state
;
53 unsigned long immediate
;
54 bool alt_group
, visited
;
55 struct symbol
*call_dest
;
56 struct instruction
*jump_dest
;
57 struct list_head alts
;
62 struct list_head list
;
63 struct instruction
*insn
;
68 struct list_head insn_list
;
69 DECLARE_HASHTABLE(insn_hash
, 16);
70 struct section
*rodata
, *whitelist
;
71 bool ignore_unreachables
, c_file
;
77 static struct instruction
*find_insn(struct objtool_file
*file
,
78 struct section
*sec
, unsigned long offset
)
80 struct instruction
*insn
;
82 hash_for_each_possible(file
->insn_hash
, insn
, hash
, offset
)
83 if (insn
->sec
== sec
&& insn
->offset
== offset
)
89 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
90 struct instruction
*insn
)
92 struct instruction
*next
= list_next_entry(insn
, list
);
94 if (&next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
100 #define for_each_insn(file, insn) \
101 list_for_each_entry(insn, &file->insn_list, list)
103 #define func_for_each_insn(file, func, insn) \
104 for (insn = find_insn(file, func->sec, func->offset); \
105 insn && &insn->list != &file->insn_list && \
106 insn->sec == func->sec && \
107 insn->offset < func->offset + func->len; \
108 insn = list_next_entry(insn, list))
110 #define sec_for_each_insn_from(file, insn) \
111 for (; insn; insn = next_insn_same_sec(file, insn))
115 * Check if the function has been manually whitelisted with the
116 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
117 * due to its use of a context switching instruction.
119 static bool ignore_func(struct objtool_file
*file
, struct symbol
*func
)
122 struct instruction
*insn
;
124 /* check for STACK_FRAME_NON_STANDARD */
125 if (file
->whitelist
&& file
->whitelist
->rela
)
126 list_for_each_entry(rela
, &file
->whitelist
->rela
->rela_list
, list
)
127 if (rela
->sym
->sec
== func
->sec
&&
128 rela
->addend
== func
->offset
)
131 /* check if it has a context switching instruction */
132 func_for_each_insn(file
, func
, insn
)
133 if (insn
->type
== INSN_CONTEXT_SWITCH
)
140 * This checks to see if the given function is a "noreturn" function.
142 * For global functions which are outside the scope of this object file, we
143 * have to keep a manual list of them.
145 * For local functions, we have to detect them manually by simply looking for
146 * the lack of a return instruction.
153 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
157 struct instruction
*insn
;
161 * Unfortunately these have to be hard coded because the noreturn
162 * attribute isn't provided in ELF data.
164 static const char * const global_noreturns
[] = {
168 "__module_put_and_exit",
170 "kvm_spurious_fault",
175 if (func
->bind
== STB_WEAK
)
178 if (func
->bind
== STB_GLOBAL
)
179 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
180 if (!strcmp(func
->name
, global_noreturns
[i
]))
186 func_for_each_insn(file
, func
, insn
) {
189 if (insn
->type
== INSN_RETURN
)
197 * A function can have a sibling call instead of a return. In that
198 * case, the function's dead-end status depends on whether the target
199 * of the sibling call returns.
201 func_for_each_insn(file
, func
, insn
) {
202 if (insn
->sec
!= func
->sec
||
203 insn
->offset
>= func
->offset
+ func
->len
)
206 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
207 struct instruction
*dest
= insn
->jump_dest
;
208 struct symbol
*dest_func
;
211 /* sibling call to another file */
214 if (dest
->sec
!= func
->sec
||
215 dest
->offset
< func
->offset
||
216 dest
->offset
>= func
->offset
+ func
->len
) {
217 /* local sibling call */
218 dest_func
= find_symbol_by_offset(dest
->sec
,
223 if (recursion
== 5) {
224 WARN_FUNC("infinite recursion (objtool bug!)",
225 dest
->sec
, dest
->offset
);
229 return __dead_end_function(file
, dest_func
,
234 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
242 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
244 return __dead_end_function(file
, func
, 0);
248 * Call the arch-specific instruction decoder for all the instructions and add
249 * them to the global instruction list.
251 static int decode_instructions(struct objtool_file
*file
)
255 unsigned long offset
;
256 struct instruction
*insn
;
259 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
261 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
264 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
265 insn
= malloc(sizeof(*insn
));
266 memset(insn
, 0, sizeof(*insn
));
268 INIT_LIST_HEAD(&insn
->alts
);
270 insn
->offset
= offset
;
272 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
274 &insn
->len
, &insn
->type
,
279 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
280 WARN_FUNC("invalid instruction type %d",
281 insn
->sec
, insn
->offset
, insn
->type
);
285 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
286 list_add_tail(&insn
->list
, &file
->insn_list
);
289 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
290 if (func
->type
!= STT_FUNC
)
293 if (!find_insn(file
, sec
, func
->offset
)) {
294 WARN("%s(): can't find starting instruction",
299 func_for_each_insn(file
, func
, insn
)
309 * Warnings shouldn't be reported for ignored functions.
311 static void add_ignores(struct objtool_file
*file
)
313 struct instruction
*insn
;
317 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
318 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
319 if (func
->type
!= STT_FUNC
)
322 if (!ignore_func(file
, func
))
325 func_for_each_insn(file
, func
, insn
)
326 insn
->visited
= true;
332 * Find the destination instructions for all jumps.
334 static int add_jump_destinations(struct objtool_file
*file
)
336 struct instruction
*insn
;
338 struct section
*dest_sec
;
339 unsigned long dest_off
;
341 for_each_insn(file
, insn
) {
342 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
343 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
350 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
353 dest_sec
= insn
->sec
;
354 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
355 } else if (rela
->sym
->type
== STT_SECTION
) {
356 dest_sec
= rela
->sym
->sec
;
357 dest_off
= rela
->addend
+ 4;
358 } else if (rela
->sym
->sec
->idx
) {
359 dest_sec
= rela
->sym
->sec
;
360 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
367 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
368 if (!insn
->jump_dest
) {
371 * This is a special case where an alt instruction
372 * jumps past the end of the section. These are
373 * handled later in handle_group_alt().
375 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
378 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
379 insn
->sec
, insn
->offset
, dest_sec
->name
,
389 * Find the destination instructions for all calls.
391 static int add_call_destinations(struct objtool_file
*file
)
393 struct instruction
*insn
;
394 unsigned long dest_off
;
397 for_each_insn(file
, insn
) {
398 if (insn
->type
!= INSN_CALL
)
401 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
404 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
405 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
407 if (!insn
->call_dest
) {
408 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
409 insn
->sec
, insn
->offset
, dest_off
);
412 } else if (rela
->sym
->type
== STT_SECTION
) {
413 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
415 if (!insn
->call_dest
||
416 insn
->call_dest
->type
!= STT_FUNC
) {
417 WARN_FUNC("can't find call dest symbol at %s+0x%x",
418 insn
->sec
, insn
->offset
,
419 rela
->sym
->sec
->name
,
424 insn
->call_dest
= rela
->sym
;
431 * The .alternatives section requires some extra special care, over and above
432 * what other special sections require:
434 * 1. Because alternatives are patched in-place, we need to insert a fake jump
435 * instruction at the end so that validate_branch() skips all the original
436 * replaced instructions when validating the new instruction path.
438 * 2. An added wrinkle is that the new instruction length might be zero. In
439 * that case the old instructions are replaced with noops. We simulate that
440 * by creating a fake jump as the only new instruction.
442 * 3. In some cases, the alternative section includes an instruction which
443 * conditionally jumps to the _end_ of the entry. We have to modify these
444 * jumps' destinations to point back to .text rather than the end of the
445 * entry in .altinstr_replacement.
447 * 4. It has been requested that we don't validate the !POPCNT feature path
448 * which is a "very very small percentage of machines".
450 static int handle_group_alt(struct objtool_file
*file
,
451 struct special_alt
*special_alt
,
452 struct instruction
*orig_insn
,
453 struct instruction
**new_insn
)
455 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
;
456 unsigned long dest_off
;
458 last_orig_insn
= NULL
;
460 sec_for_each_insn_from(file
, insn
) {
461 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
464 if (special_alt
->skip_orig
)
465 insn
->type
= INSN_NOP
;
467 insn
->alt_group
= true;
468 last_orig_insn
= insn
;
471 if (!next_insn_same_sec(file
, last_orig_insn
)) {
472 WARN("%s: don't know how to handle alternatives at end of section",
473 special_alt
->orig_sec
->name
);
477 fake_jump
= malloc(sizeof(*fake_jump
));
479 WARN("malloc failed");
482 memset(fake_jump
, 0, sizeof(*fake_jump
));
483 INIT_LIST_HEAD(&fake_jump
->alts
);
484 fake_jump
->sec
= special_alt
->new_sec
;
485 fake_jump
->offset
= -1;
486 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
487 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
489 if (!special_alt
->new_len
) {
490 *new_insn
= fake_jump
;
494 last_new_insn
= NULL
;
496 sec_for_each_insn_from(file
, insn
) {
497 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
500 last_new_insn
= insn
;
502 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
503 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
506 if (!insn
->immediate
)
509 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
510 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
511 insn
->jump_dest
= fake_jump
;
513 if (!insn
->jump_dest
) {
514 WARN_FUNC("can't find alternative jump destination",
515 insn
->sec
, insn
->offset
);
520 if (!last_new_insn
) {
521 WARN_FUNC("can't find last new alternative instruction",
522 special_alt
->new_sec
, special_alt
->new_off
);
526 list_add(&fake_jump
->list
, &last_new_insn
->list
);
532 * A jump table entry can either convert a nop to a jump or a jump to a nop.
533 * If the original instruction is a jump, make the alt entry an effective nop
534 * by just skipping the original instruction.
536 static int handle_jump_alt(struct objtool_file
*file
,
537 struct special_alt
*special_alt
,
538 struct instruction
*orig_insn
,
539 struct instruction
**new_insn
)
541 if (orig_insn
->type
== INSN_NOP
)
544 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
545 WARN_FUNC("unsupported instruction at jump label",
546 orig_insn
->sec
, orig_insn
->offset
);
550 *new_insn
= list_next_entry(orig_insn
, list
);
555 * Read all the special sections which have alternate instructions which can be
556 * patched in or redirected to at runtime. Each instruction having alternate
557 * instruction(s) has them added to its insn->alts list, which will be
558 * traversed in validate_branch().
560 static int add_special_section_alts(struct objtool_file
*file
)
562 struct list_head special_alts
;
563 struct instruction
*orig_insn
, *new_insn
;
564 struct special_alt
*special_alt
, *tmp
;
565 struct alternative
*alt
;
568 ret
= special_get_alts(file
->elf
, &special_alts
);
572 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
573 alt
= malloc(sizeof(*alt
));
575 WARN("malloc failed");
580 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
581 special_alt
->orig_off
);
583 WARN_FUNC("special: can't find orig instruction",
584 special_alt
->orig_sec
, special_alt
->orig_off
);
590 if (!special_alt
->group
|| special_alt
->new_len
) {
591 new_insn
= find_insn(file
, special_alt
->new_sec
,
592 special_alt
->new_off
);
594 WARN_FUNC("special: can't find new instruction",
595 special_alt
->new_sec
,
596 special_alt
->new_off
);
602 if (special_alt
->group
) {
603 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
607 } else if (special_alt
->jump_or_nop
) {
608 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
614 alt
->insn
= new_insn
;
615 list_add_tail(&alt
->list
, &orig_insn
->alts
);
617 list_del(&special_alt
->list
);
625 static int add_switch_table(struct objtool_file
*file
, struct symbol
*func
,
626 struct instruction
*insn
, struct rela
*table
,
627 struct rela
*next_table
)
629 struct rela
*rela
= table
;
630 struct instruction
*alt_insn
;
631 struct alternative
*alt
;
633 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
634 if (rela
== next_table
)
637 if (rela
->sym
->sec
!= insn
->sec
||
638 rela
->addend
<= func
->offset
||
639 rela
->addend
>= func
->offset
+ func
->len
)
642 alt_insn
= find_insn(file
, insn
->sec
, rela
->addend
);
644 WARN("%s: can't find instruction at %s+0x%x",
645 file
->rodata
->rela
->name
, insn
->sec
->name
,
650 alt
= malloc(sizeof(*alt
));
652 WARN("malloc failed");
656 alt
->insn
= alt_insn
;
657 list_add_tail(&alt
->list
, &insn
->alts
);
663 static int add_func_switch_tables(struct objtool_file
*file
,
666 struct instruction
*insn
, *prev_jump
;
667 struct rela
*text_rela
, *rodata_rela
, *prev_rela
= NULL
;
672 func_for_each_insn(file
, func
, insn
) {
673 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
676 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
678 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
681 /* common case: jmpq *[addr](,%rax,8) */
682 rodata_rela
= find_rela_by_dest(file
->rodata
,
686 * rare case: jmpq *[addr](%rip)
688 * This check is for a rare gcc quirk, currently only seen in
689 * three driver functions in the kernel, only with certain
690 * obscure non-distro configs.
692 * As part of an optimization, gcc makes a copy of an existing
693 * switch jump table, modifies it, and then hard-codes the jump
694 * (albeit with an indirect jump) to use a single entry in the
695 * table. The rest of the jump table and some of its jump
696 * targets remain as dead code.
698 * In such a case we can just crudely ignore all unreachable
699 * instruction warnings for the entire object file. Ideally we
700 * would just ignore them for the function, but that would
701 * require redesigning the code quite a bit. And honestly
702 * that's just not worth doing: unreachable instruction
703 * warnings are of questionable value anyway, and this is such
707 * - https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
708 * - https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
709 * - https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com
712 * - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604
715 rodata_rela
= find_rela_by_dest(file
->rodata
,
716 text_rela
->addend
+ 4);
718 file
->ignore_unreachables
= true;
725 * We found a switch table, but we don't know yet how big it
726 * is. Don't add it until we reach the end of the function or
727 * the beginning of another switch table in the same function.
730 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
,
737 prev_rela
= rodata_rela
;
741 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
, NULL
);
750 * For some switch statements, gcc generates a jump table in the .rodata
751 * section which contains a list of addresses within the function to jump to.
752 * This finds these jump tables and adds them to the insn->alts lists.
754 static int add_switch_table_alts(struct objtool_file
*file
)
760 if (!file
->rodata
|| !file
->rodata
->rela
)
763 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
764 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
765 if (func
->type
!= STT_FUNC
)
768 ret
= add_func_switch_tables(file
, func
);
777 static int decode_sections(struct objtool_file
*file
)
781 ret
= decode_instructions(file
);
787 ret
= add_jump_destinations(file
);
791 ret
= add_call_destinations(file
);
795 ret
= add_special_section_alts(file
);
799 ret
= add_switch_table_alts(file
);
806 static bool is_fentry_call(struct instruction
*insn
)
808 if (insn
->type
== INSN_CALL
&&
809 insn
->call_dest
->type
== STT_NOTYPE
&&
810 !strcmp(insn
->call_dest
->name
, "__fentry__"))
816 static bool has_modified_stack_frame(struct instruction
*insn
)
818 return (insn
->state
& STATE_FP_SAVED
) ||
819 (insn
->state
& STATE_FP_SETUP
);
822 static bool has_valid_stack_frame(struct instruction
*insn
)
824 return (insn
->state
& STATE_FP_SAVED
) &&
825 (insn
->state
& STATE_FP_SETUP
);
828 static unsigned int frame_state(unsigned long state
)
830 return (state
& (STATE_FP_SAVED
| STATE_FP_SETUP
));
834 * Follow the branch starting at the given instruction, and recursively follow
835 * any other branches (jumps). Meanwhile, track the frame pointer state at
836 * each instruction and validate all the rules described in
837 * tools/objtool/Documentation/stack-validation.txt.
839 static int validate_branch(struct objtool_file
*file
,
840 struct instruction
*first
, unsigned char first_state
)
842 struct alternative
*alt
;
843 struct instruction
*insn
;
845 struct symbol
*func
= NULL
;
853 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
854 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
860 if (file
->c_file
&& insn
->func
) {
861 if (func
&& func
!= insn
->func
) {
862 WARN("%s() falls through to next function %s()",
863 func
->name
, insn
->func
->name
);
871 if (frame_state(insn
->state
) != frame_state(state
)) {
872 WARN_FUNC("frame pointer state mismatch",
880 insn
->visited
= true;
883 list_for_each_entry(alt
, &insn
->alts
, list
) {
884 ret
= validate_branch(file
, alt
->insn
, state
);
889 switch (insn
->type
) {
893 if (state
& STATE_FP_SAVED
) {
894 WARN_FUNC("duplicate frame pointer save",
898 state
|= STATE_FP_SAVED
;
904 if (state
& STATE_FP_SETUP
) {
905 WARN_FUNC("duplicate frame pointer setup",
909 state
|= STATE_FP_SETUP
;
913 case INSN_FP_RESTORE
:
915 if (has_valid_stack_frame(insn
))
916 state
&= ~STATE_FP_SETUP
;
918 state
&= ~STATE_FP_SAVED
;
923 if (!nofp
&& has_modified_stack_frame(insn
)) {
924 WARN_FUNC("return without frame pointer restore",
931 if (is_fentry_call(insn
)) {
932 state
|= STATE_FENTRY
;
936 ret
= dead_end_function(file
, insn
->call_dest
);
943 case INSN_CALL_DYNAMIC
:
944 if (!nofp
&& !has_valid_stack_frame(insn
)) {
945 WARN_FUNC("call without frame pointer save/setup",
951 case INSN_JUMP_CONDITIONAL
:
952 case INSN_JUMP_UNCONDITIONAL
:
953 if (insn
->jump_dest
) {
954 ret
= validate_branch(file
, insn
->jump_dest
,
958 } else if (has_modified_stack_frame(insn
)) {
959 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
962 } /* else it's a sibling call */
964 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
969 case INSN_JUMP_DYNAMIC
:
970 if (list_empty(&insn
->alts
) &&
971 has_modified_stack_frame(insn
)) {
972 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
986 insn
= next_insn_same_sec(file
, insn
);
988 WARN("%s: unexpected end of section", sec
->name
);
996 static bool is_gcov_insn(struct instruction
*insn
)
1001 unsigned long offset
;
1003 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
, insn
->len
);
1007 if (rela
->sym
->type
!= STT_SECTION
)
1010 sec
= rela
->sym
->sec
;
1011 offset
= rela
->addend
+ insn
->offset
+ insn
->len
- rela
->offset
;
1013 list_for_each_entry(sym
, &sec
->symbol_list
, list
) {
1014 if (sym
->type
!= STT_OBJECT
)
1017 if (offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
1018 return (!memcmp(sym
->name
, "__gcov0.", 8));
1024 static bool is_kasan_insn(struct instruction
*insn
)
1026 return (insn
->type
== INSN_CALL
&&
1027 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1030 static bool is_ubsan_insn(struct instruction
*insn
)
1032 return (insn
->type
== INSN_CALL
&&
1033 !strcmp(insn
->call_dest
->name
,
1034 "__ubsan_handle_builtin_unreachable"));
1037 static bool ignore_unreachable_insn(struct symbol
*func
,
1038 struct instruction
*insn
)
1042 if (insn
->type
== INSN_NOP
)
1045 if (is_gcov_insn(insn
))
1049 * Check if this (or a subsequent) instruction is related to
1050 * CONFIG_UBSAN or CONFIG_KASAN.
1052 * End the search at 5 instructions to avoid going into the weeds.
1054 for (i
= 0; i
< 5; i
++) {
1056 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
1059 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
) {
1060 insn
= insn
->jump_dest
;
1064 if (insn
->offset
+ insn
->len
>= func
->offset
+ func
->len
)
1066 insn
= list_next_entry(insn
, list
);
1072 static int validate_functions(struct objtool_file
*file
)
1074 struct section
*sec
;
1075 struct symbol
*func
;
1076 struct instruction
*insn
;
1077 int ret
, warnings
= 0;
1079 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1080 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1081 if (func
->type
!= STT_FUNC
)
1084 insn
= find_insn(file
, sec
, func
->offset
);
1088 ret
= validate_branch(file
, insn
, 0);
1093 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1094 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1095 if (func
->type
!= STT_FUNC
)
1098 func_for_each_insn(file
, func
, insn
) {
1102 insn
->visited
= true;
1104 if (file
->ignore_unreachables
|| warnings
||
1105 ignore_unreachable_insn(func
, insn
))
1108 WARN_FUNC("function has unreachable instruction", insn
->sec
, insn
->offset
);
1117 static int validate_uncallable_instructions(struct objtool_file
*file
)
1119 struct instruction
*insn
;
1122 for_each_insn(file
, insn
) {
1123 if (!insn
->visited
&& insn
->type
== INSN_RETURN
) {
1124 WARN_FUNC("return instruction outside of a callable function",
1125 insn
->sec
, insn
->offset
);
1133 static void cleanup(struct objtool_file
*file
)
1135 struct instruction
*insn
, *tmpinsn
;
1136 struct alternative
*alt
, *tmpalt
;
1138 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
1139 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
1140 list_del(&alt
->list
);
1143 list_del(&insn
->list
);
1144 hash_del(&insn
->hash
);
1147 elf_close(file
->elf
);
1150 const char * const check_usage
[] = {
1151 "objtool check [<options>] file.o",
1155 int cmd_check(int argc
, const char **argv
)
1157 struct objtool_file file
;
1158 int ret
, warnings
= 0;
1160 const struct option options
[] = {
1161 OPT_BOOLEAN('f', "no-fp", &nofp
, "Skip frame pointer validation"),
1165 argc
= parse_options(argc
, argv
, options
, check_usage
, 0);
1168 usage_with_options(check_usage
, options
);
1172 file
.elf
= elf_open(objname
);
1174 fprintf(stderr
, "error reading elf file %s\n", objname
);
1178 INIT_LIST_HEAD(&file
.insn_list
);
1179 hash_init(file
.insn_hash
);
1180 file
.whitelist
= find_section_by_name(file
.elf
, "__func_stack_frame_non_standard");
1181 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
1182 file
.ignore_unreachables
= false;
1183 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
1185 ret
= decode_sections(&file
);
1190 ret
= validate_functions(&file
);
1195 ret
= validate_uncallable_instructions(&file
);
1203 /* ignore warnings for now until we get all the code cleaned up */
1204 if (ret
|| warnings
)