]>
git.proxmox.com Git - mirror_ubuntu-artful-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.
29 #include <subcmd/parse-options.h>
37 #include <linux/hashtable.h>
39 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
41 #define STATE_FP_SAVED 0x1
42 #define STATE_FP_SETUP 0x2
43 #define STATE_FENTRY 0x4
46 struct list_head list
;
47 struct hlist_node hash
;
50 unsigned int len
, state
;
52 unsigned long immediate
;
53 bool alt_group
, visited
;
54 struct symbol
*call_dest
;
55 struct instruction
*jump_dest
;
56 struct list_head alts
;
61 struct list_head list
;
62 struct instruction
*insn
;
67 struct list_head insn_list
;
68 DECLARE_HASHTABLE(insn_hash
, 16);
69 struct section
*rodata
, *whitelist
;
70 bool ignore_unreachables
, c_file
;
76 static struct instruction
*find_insn(struct objtool_file
*file
,
77 struct section
*sec
, unsigned long offset
)
79 struct instruction
*insn
;
81 hash_for_each_possible(file
->insn_hash
, insn
, hash
, offset
)
82 if (insn
->sec
== sec
&& insn
->offset
== offset
)
88 static struct instruction
*next_insn_same_sec(struct objtool_file
*file
,
89 struct instruction
*insn
)
91 struct instruction
*next
= list_next_entry(insn
, list
);
93 if (&next
->list
== &file
->insn_list
|| next
->sec
!= insn
->sec
)
99 #define for_each_insn(file, insn) \
100 list_for_each_entry(insn, &file->insn_list, list)
102 #define func_for_each_insn(file, func, insn) \
103 for (insn = find_insn(file, func->sec, func->offset); \
104 insn && &insn->list != &file->insn_list && \
105 insn->sec == func->sec && \
106 insn->offset < func->offset + func->len; \
107 insn = list_next_entry(insn, list))
109 #define sec_for_each_insn_from(file, insn) \
110 for (; insn; insn = next_insn_same_sec(file, insn))
114 * Check if the function has been manually whitelisted with the
115 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
116 * due to its use of a context switching instruction.
118 static bool ignore_func(struct objtool_file
*file
, struct symbol
*func
)
121 struct instruction
*insn
;
123 /* check for STACK_FRAME_NON_STANDARD */
124 if (file
->whitelist
&& file
->whitelist
->rela
)
125 list_for_each_entry(rela
, &file
->whitelist
->rela
->rela_list
, list
) {
126 if (rela
->sym
->type
== STT_SECTION
&&
127 rela
->sym
->sec
== func
->sec
&&
128 rela
->addend
== func
->offset
)
130 if (rela
->sym
->type
== STT_FUNC
&& rela
->sym
== func
)
134 /* check if it has a context switching instruction */
135 func_for_each_insn(file
, func
, insn
)
136 if (insn
->type
== INSN_CONTEXT_SWITCH
)
143 * This checks to see if the given function is a "noreturn" function.
145 * For global functions which are outside the scope of this object file, we
146 * have to keep a manual list of them.
148 * For local functions, we have to detect them manually by simply looking for
149 * the lack of a return instruction.
156 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
160 struct instruction
*insn
;
164 * Unfortunately these have to be hard coded because the noreturn
165 * attribute isn't provided in ELF data.
167 static const char * const global_noreturns
[] = {
171 "__module_put_and_exit",
173 "kvm_spurious_fault",
178 if (func
->bind
== STB_WEAK
)
181 if (func
->bind
== STB_GLOBAL
)
182 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
183 if (!strcmp(func
->name
, global_noreturns
[i
]))
189 func_for_each_insn(file
, func
, insn
) {
192 if (insn
->type
== INSN_RETURN
)
200 * A function can have a sibling call instead of a return. In that
201 * case, the function's dead-end status depends on whether the target
202 * of the sibling call returns.
204 func_for_each_insn(file
, func
, insn
) {
205 if (insn
->sec
!= func
->sec
||
206 insn
->offset
>= func
->offset
+ func
->len
)
209 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
210 struct instruction
*dest
= insn
->jump_dest
;
211 struct symbol
*dest_func
;
214 /* sibling call to another file */
217 if (dest
->sec
!= func
->sec
||
218 dest
->offset
< func
->offset
||
219 dest
->offset
>= func
->offset
+ func
->len
) {
220 /* local sibling call */
221 dest_func
= find_symbol_by_offset(dest
->sec
,
226 if (recursion
== 5) {
227 WARN_FUNC("infinite recursion (objtool bug!)",
228 dest
->sec
, dest
->offset
);
232 return __dead_end_function(file
, dest_func
,
237 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
245 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
247 return __dead_end_function(file
, func
, 0);
251 * Call the arch-specific instruction decoder for all the instructions and add
252 * them to the global instruction list.
254 static int decode_instructions(struct objtool_file
*file
)
258 unsigned long offset
;
259 struct instruction
*insn
;
262 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
264 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
267 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
268 insn
= malloc(sizeof(*insn
));
269 memset(insn
, 0, sizeof(*insn
));
271 INIT_LIST_HEAD(&insn
->alts
);
273 insn
->offset
= offset
;
275 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
277 &insn
->len
, &insn
->type
,
282 if (!insn
->type
|| insn
->type
> INSN_LAST
) {
283 WARN_FUNC("invalid instruction type %d",
284 insn
->sec
, insn
->offset
, insn
->type
);
288 hash_add(file
->insn_hash
, &insn
->hash
, insn
->offset
);
289 list_add_tail(&insn
->list
, &file
->insn_list
);
292 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
293 if (func
->type
!= STT_FUNC
)
296 if (!find_insn(file
, sec
, func
->offset
)) {
297 WARN("%s(): can't find starting instruction",
302 func_for_each_insn(file
, func
, insn
)
312 * Warnings shouldn't be reported for ignored functions.
314 static void add_ignores(struct objtool_file
*file
)
316 struct instruction
*insn
;
320 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
321 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
322 if (func
->type
!= STT_FUNC
)
325 if (!ignore_func(file
, func
))
328 func_for_each_insn(file
, func
, insn
)
329 insn
->visited
= true;
335 * Find the destination instructions for all jumps.
337 static int add_jump_destinations(struct objtool_file
*file
)
339 struct instruction
*insn
;
341 struct section
*dest_sec
;
342 unsigned long dest_off
;
344 for_each_insn(file
, insn
) {
345 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
346 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
353 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
356 dest_sec
= insn
->sec
;
357 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
358 } else if (rela
->sym
->type
== STT_SECTION
) {
359 dest_sec
= rela
->sym
->sec
;
360 dest_off
= rela
->addend
+ 4;
361 } else if (rela
->sym
->sec
->idx
) {
362 dest_sec
= rela
->sym
->sec
;
363 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
370 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
371 if (!insn
->jump_dest
) {
374 * This is a special case where an alt instruction
375 * jumps past the end of the section. These are
376 * handled later in handle_group_alt().
378 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
381 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
382 insn
->sec
, insn
->offset
, dest_sec
->name
,
392 * Find the destination instructions for all calls.
394 static int add_call_destinations(struct objtool_file
*file
)
396 struct instruction
*insn
;
397 unsigned long dest_off
;
400 for_each_insn(file
, insn
) {
401 if (insn
->type
!= INSN_CALL
)
404 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
407 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
408 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
410 if (!insn
->call_dest
) {
411 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
412 insn
->sec
, insn
->offset
, dest_off
);
415 } else if (rela
->sym
->type
== STT_SECTION
) {
416 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
418 if (!insn
->call_dest
||
419 insn
->call_dest
->type
!= STT_FUNC
) {
420 WARN_FUNC("can't find call dest symbol at %s+0x%x",
421 insn
->sec
, insn
->offset
,
422 rela
->sym
->sec
->name
,
427 insn
->call_dest
= rela
->sym
;
434 * The .alternatives section requires some extra special care, over and above
435 * what other special sections require:
437 * 1. Because alternatives are patched in-place, we need to insert a fake jump
438 * instruction at the end so that validate_branch() skips all the original
439 * replaced instructions when validating the new instruction path.
441 * 2. An added wrinkle is that the new instruction length might be zero. In
442 * that case the old instructions are replaced with noops. We simulate that
443 * by creating a fake jump as the only new instruction.
445 * 3. In some cases, the alternative section includes an instruction which
446 * conditionally jumps to the _end_ of the entry. We have to modify these
447 * jumps' destinations to point back to .text rather than the end of the
448 * entry in .altinstr_replacement.
450 * 4. It has been requested that we don't validate the !POPCNT feature path
451 * which is a "very very small percentage of machines".
453 static int handle_group_alt(struct objtool_file
*file
,
454 struct special_alt
*special_alt
,
455 struct instruction
*orig_insn
,
456 struct instruction
**new_insn
)
458 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
;
459 unsigned long dest_off
;
461 last_orig_insn
= NULL
;
463 sec_for_each_insn_from(file
, insn
) {
464 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
467 if (special_alt
->skip_orig
)
468 insn
->type
= INSN_NOP
;
470 insn
->alt_group
= true;
471 last_orig_insn
= insn
;
474 if (!next_insn_same_sec(file
, last_orig_insn
)) {
475 WARN("%s: don't know how to handle alternatives at end of section",
476 special_alt
->orig_sec
->name
);
480 fake_jump
= malloc(sizeof(*fake_jump
));
482 WARN("malloc failed");
485 memset(fake_jump
, 0, sizeof(*fake_jump
));
486 INIT_LIST_HEAD(&fake_jump
->alts
);
487 fake_jump
->sec
= special_alt
->new_sec
;
488 fake_jump
->offset
= -1;
489 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
490 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
492 if (!special_alt
->new_len
) {
493 *new_insn
= fake_jump
;
497 last_new_insn
= NULL
;
499 sec_for_each_insn_from(file
, insn
) {
500 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
503 last_new_insn
= insn
;
505 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
506 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
509 if (!insn
->immediate
)
512 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
513 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
514 insn
->jump_dest
= fake_jump
;
516 if (!insn
->jump_dest
) {
517 WARN_FUNC("can't find alternative jump destination",
518 insn
->sec
, insn
->offset
);
523 if (!last_new_insn
) {
524 WARN_FUNC("can't find last new alternative instruction",
525 special_alt
->new_sec
, special_alt
->new_off
);
529 list_add(&fake_jump
->list
, &last_new_insn
->list
);
535 * A jump table entry can either convert a nop to a jump or a jump to a nop.
536 * If the original instruction is a jump, make the alt entry an effective nop
537 * by just skipping the original instruction.
539 static int handle_jump_alt(struct objtool_file
*file
,
540 struct special_alt
*special_alt
,
541 struct instruction
*orig_insn
,
542 struct instruction
**new_insn
)
544 if (orig_insn
->type
== INSN_NOP
)
547 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
548 WARN_FUNC("unsupported instruction at jump label",
549 orig_insn
->sec
, orig_insn
->offset
);
553 *new_insn
= list_next_entry(orig_insn
, list
);
558 * Read all the special sections which have alternate instructions which can be
559 * patched in or redirected to at runtime. Each instruction having alternate
560 * instruction(s) has them added to its insn->alts list, which will be
561 * traversed in validate_branch().
563 static int add_special_section_alts(struct objtool_file
*file
)
565 struct list_head special_alts
;
566 struct instruction
*orig_insn
, *new_insn
;
567 struct special_alt
*special_alt
, *tmp
;
568 struct alternative
*alt
;
571 ret
= special_get_alts(file
->elf
, &special_alts
);
575 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
576 alt
= malloc(sizeof(*alt
));
578 WARN("malloc failed");
583 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
584 special_alt
->orig_off
);
586 WARN_FUNC("special: can't find orig instruction",
587 special_alt
->orig_sec
, special_alt
->orig_off
);
593 if (!special_alt
->group
|| special_alt
->new_len
) {
594 new_insn
= find_insn(file
, special_alt
->new_sec
,
595 special_alt
->new_off
);
597 WARN_FUNC("special: can't find new instruction",
598 special_alt
->new_sec
,
599 special_alt
->new_off
);
605 if (special_alt
->group
) {
606 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
610 } else if (special_alt
->jump_or_nop
) {
611 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
617 alt
->insn
= new_insn
;
618 list_add_tail(&alt
->list
, &orig_insn
->alts
);
620 list_del(&special_alt
->list
);
628 static int add_switch_table(struct objtool_file
*file
, struct symbol
*func
,
629 struct instruction
*insn
, struct rela
*table
,
630 struct rela
*next_table
)
632 struct rela
*rela
= table
;
633 struct instruction
*alt_insn
;
634 struct alternative
*alt
;
636 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
637 if (rela
== next_table
)
640 if (rela
->sym
->sec
!= insn
->sec
||
641 rela
->addend
<= func
->offset
||
642 rela
->addend
>= func
->offset
+ func
->len
)
645 alt_insn
= find_insn(file
, insn
->sec
, rela
->addend
);
647 WARN("%s: can't find instruction at %s+0x%x",
648 file
->rodata
->rela
->name
, insn
->sec
->name
,
653 alt
= malloc(sizeof(*alt
));
655 WARN("malloc failed");
659 alt
->insn
= alt_insn
;
660 list_add_tail(&alt
->list
, &insn
->alts
);
666 static int add_func_switch_tables(struct objtool_file
*file
,
669 struct instruction
*insn
, *prev_jump
;
670 struct rela
*text_rela
, *rodata_rela
, *prev_rela
;
675 func_for_each_insn(file
, func
, insn
) {
676 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
679 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
681 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
684 /* common case: jmpq *[addr](,%rax,8) */
685 rodata_rela
= find_rela_by_dest(file
->rodata
,
689 * rare case: jmpq *[addr](%rip)
691 * This check is for a rare gcc quirk, currently only seen in
692 * three driver functions in the kernel, only with certain
693 * obscure non-distro configs.
695 * As part of an optimization, gcc makes a copy of an existing
696 * switch jump table, modifies it, and then hard-codes the jump
697 * (albeit with an indirect jump) to use a single entry in the
698 * table. The rest of the jump table and some of its jump
699 * targets remain as dead code.
701 * In such a case we can just crudely ignore all unreachable
702 * instruction warnings for the entire object file. Ideally we
703 * would just ignore them for the function, but that would
704 * require redesigning the code quite a bit. And honestly
705 * that's just not worth doing: unreachable instruction
706 * warnings are of questionable value anyway, and this is such
710 * - https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
711 * - https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
712 * - https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com
715 * - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604
718 rodata_rela
= find_rela_by_dest(file
->rodata
,
719 text_rela
->addend
+ 4);
721 file
->ignore_unreachables
= true;
728 * We found a switch table, but we don't know yet how big it
729 * is. Don't add it until we reach the end of the function or
730 * the beginning of another switch table in the same function.
733 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
,
740 prev_rela
= rodata_rela
;
744 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
, NULL
);
753 * For some switch statements, gcc generates a jump table in the .rodata
754 * section which contains a list of addresses within the function to jump to.
755 * This finds these jump tables and adds them to the insn->alts lists.
757 static int add_switch_table_alts(struct objtool_file
*file
)
763 if (!file
->rodata
|| !file
->rodata
->rela
)
766 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
767 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
768 if (func
->type
!= STT_FUNC
)
771 ret
= add_func_switch_tables(file
, func
);
780 static int decode_sections(struct objtool_file
*file
)
784 ret
= decode_instructions(file
);
790 ret
= add_jump_destinations(file
);
794 ret
= add_call_destinations(file
);
798 ret
= add_special_section_alts(file
);
802 ret
= add_switch_table_alts(file
);
809 static bool is_fentry_call(struct instruction
*insn
)
811 if (insn
->type
== INSN_CALL
&&
812 insn
->call_dest
->type
== STT_NOTYPE
&&
813 !strcmp(insn
->call_dest
->name
, "__fentry__"))
819 static bool has_modified_stack_frame(struct instruction
*insn
)
821 return (insn
->state
& STATE_FP_SAVED
) ||
822 (insn
->state
& STATE_FP_SETUP
);
825 static bool has_valid_stack_frame(struct instruction
*insn
)
827 return (insn
->state
& STATE_FP_SAVED
) &&
828 (insn
->state
& STATE_FP_SETUP
);
831 static unsigned int frame_state(unsigned long state
)
833 return (state
& (STATE_FP_SAVED
| STATE_FP_SETUP
));
837 * Follow the branch starting at the given instruction, and recursively follow
838 * any other branches (jumps). Meanwhile, track the frame pointer state at
839 * each instruction and validate all the rules described in
840 * tools/objtool/Documentation/stack-validation.txt.
842 static int validate_branch(struct objtool_file
*file
,
843 struct instruction
*first
, unsigned char first_state
)
845 struct alternative
*alt
;
846 struct instruction
*insn
;
848 struct symbol
*func
= NULL
;
856 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
857 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
863 if (file
->c_file
&& insn
->func
) {
864 if (func
&& func
!= insn
->func
) {
865 WARN("%s() falls through to next function %s()",
866 func
->name
, insn
->func
->name
);
874 if (frame_state(insn
->state
) != frame_state(state
)) {
875 WARN_FUNC("frame pointer state mismatch",
883 insn
->visited
= true;
886 list_for_each_entry(alt
, &insn
->alts
, list
) {
887 ret
= validate_branch(file
, alt
->insn
, state
);
892 switch (insn
->type
) {
896 if (state
& STATE_FP_SAVED
) {
897 WARN_FUNC("duplicate frame pointer save",
901 state
|= STATE_FP_SAVED
;
907 if (state
& STATE_FP_SETUP
) {
908 WARN_FUNC("duplicate frame pointer setup",
912 state
|= STATE_FP_SETUP
;
916 case INSN_FP_RESTORE
:
918 if (has_valid_stack_frame(insn
))
919 state
&= ~STATE_FP_SETUP
;
921 state
&= ~STATE_FP_SAVED
;
926 if (!nofp
&& has_modified_stack_frame(insn
)) {
927 WARN_FUNC("return without frame pointer restore",
934 if (is_fentry_call(insn
)) {
935 state
|= STATE_FENTRY
;
939 ret
= dead_end_function(file
, insn
->call_dest
);
946 case INSN_CALL_DYNAMIC
:
947 if (!nofp
&& !has_valid_stack_frame(insn
)) {
948 WARN_FUNC("call without frame pointer save/setup",
954 case INSN_JUMP_CONDITIONAL
:
955 case INSN_JUMP_UNCONDITIONAL
:
956 if (insn
->jump_dest
) {
957 ret
= validate_branch(file
, insn
->jump_dest
,
961 } else if (has_modified_stack_frame(insn
)) {
962 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
965 } /* else it's a sibling call */
967 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
972 case INSN_JUMP_DYNAMIC
:
973 if (list_empty(&insn
->alts
) &&
974 has_modified_stack_frame(insn
)) {
975 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
989 insn
= next_insn_same_sec(file
, insn
);
991 WARN("%s: unexpected end of section", sec
->name
);
999 static bool is_gcov_insn(struct instruction
*insn
)
1002 struct section
*sec
;
1004 unsigned long offset
;
1006 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
, insn
->len
);
1010 if (rela
->sym
->type
!= STT_SECTION
)
1013 sec
= rela
->sym
->sec
;
1014 offset
= rela
->addend
+ insn
->offset
+ insn
->len
- rela
->offset
;
1016 list_for_each_entry(sym
, &sec
->symbol_list
, list
) {
1017 if (sym
->type
!= STT_OBJECT
)
1020 if (offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
1021 return (!memcmp(sym
->name
, "__gcov0.", 8));
1027 static bool is_kasan_insn(struct instruction
*insn
)
1029 return (insn
->type
== INSN_CALL
&&
1030 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1033 static bool is_ubsan_insn(struct instruction
*insn
)
1035 return (insn
->type
== INSN_CALL
&&
1036 !strcmp(insn
->call_dest
->name
,
1037 "__ubsan_handle_builtin_unreachable"));
1040 static bool ignore_unreachable_insn(struct symbol
*func
,
1041 struct instruction
*insn
)
1045 if (insn
->type
== INSN_NOP
)
1048 if (is_gcov_insn(insn
))
1052 * Check if this (or a subsequent) instruction is related to
1053 * CONFIG_UBSAN or CONFIG_KASAN.
1055 * End the search at 5 instructions to avoid going into the weeds.
1057 for (i
= 0; i
< 5; i
++) {
1059 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
1062 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
) {
1063 insn
= insn
->jump_dest
;
1067 if (insn
->offset
+ insn
->len
>= func
->offset
+ func
->len
)
1069 insn
= list_next_entry(insn
, list
);
1075 static int validate_functions(struct objtool_file
*file
)
1077 struct section
*sec
;
1078 struct symbol
*func
;
1079 struct instruction
*insn
;
1080 int ret
, warnings
= 0;
1082 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1083 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1084 if (func
->type
!= STT_FUNC
)
1087 insn
= find_insn(file
, sec
, func
->offset
);
1091 ret
= validate_branch(file
, insn
, 0);
1096 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1097 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1098 if (func
->type
!= STT_FUNC
)
1101 func_for_each_insn(file
, func
, insn
) {
1105 insn
->visited
= true;
1107 if (file
->ignore_unreachables
|| warnings
||
1108 ignore_unreachable_insn(func
, insn
))
1111 WARN_FUNC("function has unreachable instruction", insn
->sec
, insn
->offset
);
1120 static int validate_uncallable_instructions(struct objtool_file
*file
)
1122 struct instruction
*insn
;
1125 for_each_insn(file
, insn
) {
1126 if (!insn
->visited
&& insn
->type
== INSN_RETURN
) {
1127 WARN_FUNC("return instruction outside of a callable function",
1128 insn
->sec
, insn
->offset
);
1136 static void cleanup(struct objtool_file
*file
)
1138 struct instruction
*insn
, *tmpinsn
;
1139 struct alternative
*alt
, *tmpalt
;
1141 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
1142 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
1143 list_del(&alt
->list
);
1146 list_del(&insn
->list
);
1147 hash_del(&insn
->hash
);
1150 elf_close(file
->elf
);
1153 const char * const check_usage
[] = {
1154 "objtool check [<options>] file.o",
1158 int cmd_check(int argc
, const char **argv
)
1160 struct objtool_file file
;
1161 int ret
, warnings
= 0;
1163 const struct option options
[] = {
1164 OPT_BOOLEAN('f', "no-fp", &nofp
, "Skip frame pointer validation"),
1168 argc
= parse_options(argc
, argv
, options
, check_usage
, 0);
1171 usage_with_options(check_usage
, options
);
1175 file
.elf
= elf_open(objname
);
1177 fprintf(stderr
, "error reading elf file %s\n", objname
);
1181 INIT_LIST_HEAD(&file
.insn_list
);
1182 hash_init(file
.insn_hash
);
1183 file
.whitelist
= find_section_by_name(file
.elf
, "__func_stack_frame_non_standard");
1184 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
1185 file
.ignore_unreachables
= false;
1186 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
1188 ret
= decode_sections(&file
);
1193 ret
= validate_functions(&file
);
1198 ret
= validate_uncallable_instructions(&file
);
1206 /* ignore warnings for now until we get all the code cleaned up */
1207 if (ret
|| warnings
)