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
->type
== STT_SECTION
&&
128 rela
->sym
->sec
== func
->sec
&&
129 rela
->addend
== func
->offset
)
131 if (rela
->sym
->type
== STT_FUNC
&& rela
->sym
== func
)
135 /* check if it has a context switching instruction */
136 func_for_each_insn(file
, func
, insn
)
137 if (insn
->type
== INSN_CONTEXT_SWITCH
)
144 * This checks to see if the given function is a "noreturn" function.
146 * For global functions which are outside the scope of this object file, we
147 * have to keep a manual list of them.
149 * For local functions, we have to detect them manually by simply looking for
150 * the lack of a return instruction.
157 static int __dead_end_function(struct objtool_file
*file
, struct symbol
*func
,
161 struct instruction
*insn
;
165 * Unfortunately these have to be hard coded because the noreturn
166 * attribute isn't provided in ELF data.
168 static const char * const global_noreturns
[] = {
172 "__module_put_and_exit",
174 "kvm_spurious_fault",
179 if (func
->bind
== STB_WEAK
)
182 if (func
->bind
== STB_GLOBAL
)
183 for (i
= 0; i
< ARRAY_SIZE(global_noreturns
); i
++)
184 if (!strcmp(func
->name
, global_noreturns
[i
]))
190 func_for_each_insn(file
, func
, insn
) {
193 if (insn
->type
== INSN_RETURN
)
201 * A function can have a sibling call instead of a return. In that
202 * case, the function's dead-end status depends on whether the target
203 * of the sibling call returns.
205 func_for_each_insn(file
, func
, insn
) {
206 if (insn
->sec
!= func
->sec
||
207 insn
->offset
>= func
->offset
+ func
->len
)
210 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
) {
211 struct instruction
*dest
= insn
->jump_dest
;
212 struct symbol
*dest_func
;
215 /* sibling call to another file */
218 if (dest
->sec
!= func
->sec
||
219 dest
->offset
< func
->offset
||
220 dest
->offset
>= func
->offset
+ func
->len
) {
221 /* local sibling call */
222 dest_func
= find_symbol_by_offset(dest
->sec
,
227 if (recursion
== 5) {
228 WARN_FUNC("infinite recursion (objtool bug!)",
229 dest
->sec
, dest
->offset
);
233 return __dead_end_function(file
, dest_func
,
238 if (insn
->type
== INSN_JUMP_DYNAMIC
&& list_empty(&insn
->alts
))
246 static int dead_end_function(struct objtool_file
*file
, struct symbol
*func
)
248 return __dead_end_function(file
, func
, 0);
252 * Call the arch-specific instruction decoder for all the instructions and add
253 * them to the global instruction list.
255 static int decode_instructions(struct objtool_file
*file
)
259 unsigned long offset
;
260 struct instruction
*insn
;
263 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
265 if (!(sec
->sh
.sh_flags
& SHF_EXECINSTR
))
268 for (offset
= 0; offset
< sec
->len
; offset
+= insn
->len
) {
269 insn
= malloc(sizeof(*insn
));
270 memset(insn
, 0, sizeof(*insn
));
272 INIT_LIST_HEAD(&insn
->alts
);
274 insn
->offset
= offset
;
276 ret
= arch_decode_instruction(file
->elf
, sec
, offset
,
278 &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 * Warnings shouldn't be reported for ignored functions.
315 static void add_ignores(struct objtool_file
*file
)
317 struct instruction
*insn
;
321 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
322 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
323 if (func
->type
!= STT_FUNC
)
326 if (!ignore_func(file
, func
))
329 func_for_each_insn(file
, func
, insn
)
330 insn
->visited
= true;
336 * Find the destination instructions for all jumps.
338 static int add_jump_destinations(struct objtool_file
*file
)
340 struct instruction
*insn
;
342 struct section
*dest_sec
;
343 unsigned long dest_off
;
345 for_each_insn(file
, insn
) {
346 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
347 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
354 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
357 dest_sec
= insn
->sec
;
358 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
359 } else if (rela
->sym
->type
== STT_SECTION
) {
360 dest_sec
= rela
->sym
->sec
;
361 dest_off
= rela
->addend
+ 4;
362 } else if (rela
->sym
->sec
->idx
) {
363 dest_sec
= rela
->sym
->sec
;
364 dest_off
= rela
->sym
->sym
.st_value
+ rela
->addend
+ 4;
371 insn
->jump_dest
= find_insn(file
, dest_sec
, dest_off
);
372 if (!insn
->jump_dest
) {
375 * This is a special case where an alt instruction
376 * jumps past the end of the section. These are
377 * handled later in handle_group_alt().
379 if (!strcmp(insn
->sec
->name
, ".altinstr_replacement"))
382 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
383 insn
->sec
, insn
->offset
, dest_sec
->name
,
393 * Find the destination instructions for all calls.
395 static int add_call_destinations(struct objtool_file
*file
)
397 struct instruction
*insn
;
398 unsigned long dest_off
;
401 for_each_insn(file
, insn
) {
402 if (insn
->type
!= INSN_CALL
)
405 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
408 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
409 insn
->call_dest
= find_symbol_by_offset(insn
->sec
,
411 if (!insn
->call_dest
) {
412 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
413 insn
->sec
, insn
->offset
, dest_off
);
416 } else if (rela
->sym
->type
== STT_SECTION
) {
417 insn
->call_dest
= find_symbol_by_offset(rela
->sym
->sec
,
419 if (!insn
->call_dest
||
420 insn
->call_dest
->type
!= STT_FUNC
) {
421 WARN_FUNC("can't find call dest symbol at %s+0x%x",
422 insn
->sec
, insn
->offset
,
423 rela
->sym
->sec
->name
,
428 insn
->call_dest
= rela
->sym
;
435 * The .alternatives section requires some extra special care, over and above
436 * what other special sections require:
438 * 1. Because alternatives are patched in-place, we need to insert a fake jump
439 * instruction at the end so that validate_branch() skips all the original
440 * replaced instructions when validating the new instruction path.
442 * 2. An added wrinkle is that the new instruction length might be zero. In
443 * that case the old instructions are replaced with noops. We simulate that
444 * by creating a fake jump as the only new instruction.
446 * 3. In some cases, the alternative section includes an instruction which
447 * conditionally jumps to the _end_ of the entry. We have to modify these
448 * jumps' destinations to point back to .text rather than the end of the
449 * entry in .altinstr_replacement.
451 * 4. It has been requested that we don't validate the !POPCNT feature path
452 * which is a "very very small percentage of machines".
454 static int handle_group_alt(struct objtool_file
*file
,
455 struct special_alt
*special_alt
,
456 struct instruction
*orig_insn
,
457 struct instruction
**new_insn
)
459 struct instruction
*last_orig_insn
, *last_new_insn
, *insn
, *fake_jump
;
460 unsigned long dest_off
;
462 last_orig_insn
= NULL
;
464 sec_for_each_insn_from(file
, insn
) {
465 if (insn
->offset
>= special_alt
->orig_off
+ special_alt
->orig_len
)
468 if (special_alt
->skip_orig
)
469 insn
->type
= INSN_NOP
;
471 insn
->alt_group
= true;
472 last_orig_insn
= insn
;
475 if (!next_insn_same_sec(file
, last_orig_insn
)) {
476 WARN("%s: don't know how to handle alternatives at end of section",
477 special_alt
->orig_sec
->name
);
481 fake_jump
= malloc(sizeof(*fake_jump
));
483 WARN("malloc failed");
486 memset(fake_jump
, 0, sizeof(*fake_jump
));
487 INIT_LIST_HEAD(&fake_jump
->alts
);
488 fake_jump
->sec
= special_alt
->new_sec
;
489 fake_jump
->offset
= -1;
490 fake_jump
->type
= INSN_JUMP_UNCONDITIONAL
;
491 fake_jump
->jump_dest
= list_next_entry(last_orig_insn
, list
);
493 if (!special_alt
->new_len
) {
494 *new_insn
= fake_jump
;
498 last_new_insn
= NULL
;
500 sec_for_each_insn_from(file
, insn
) {
501 if (insn
->offset
>= special_alt
->new_off
+ special_alt
->new_len
)
504 last_new_insn
= insn
;
506 if (insn
->type
!= INSN_JUMP_CONDITIONAL
&&
507 insn
->type
!= INSN_JUMP_UNCONDITIONAL
)
510 if (!insn
->immediate
)
513 dest_off
= insn
->offset
+ insn
->len
+ insn
->immediate
;
514 if (dest_off
== special_alt
->new_off
+ special_alt
->new_len
)
515 insn
->jump_dest
= fake_jump
;
517 if (!insn
->jump_dest
) {
518 WARN_FUNC("can't find alternative jump destination",
519 insn
->sec
, insn
->offset
);
524 if (!last_new_insn
) {
525 WARN_FUNC("can't find last new alternative instruction",
526 special_alt
->new_sec
, special_alt
->new_off
);
530 list_add(&fake_jump
->list
, &last_new_insn
->list
);
536 * A jump table entry can either convert a nop to a jump or a jump to a nop.
537 * If the original instruction is a jump, make the alt entry an effective nop
538 * by just skipping the original instruction.
540 static int handle_jump_alt(struct objtool_file
*file
,
541 struct special_alt
*special_alt
,
542 struct instruction
*orig_insn
,
543 struct instruction
**new_insn
)
545 if (orig_insn
->type
== INSN_NOP
)
548 if (orig_insn
->type
!= INSN_JUMP_UNCONDITIONAL
) {
549 WARN_FUNC("unsupported instruction at jump label",
550 orig_insn
->sec
, orig_insn
->offset
);
554 *new_insn
= list_next_entry(orig_insn
, list
);
559 * Read all the special sections which have alternate instructions which can be
560 * patched in or redirected to at runtime. Each instruction having alternate
561 * instruction(s) has them added to its insn->alts list, which will be
562 * traversed in validate_branch().
564 static int add_special_section_alts(struct objtool_file
*file
)
566 struct list_head special_alts
;
567 struct instruction
*orig_insn
, *new_insn
;
568 struct special_alt
*special_alt
, *tmp
;
569 struct alternative
*alt
;
572 ret
= special_get_alts(file
->elf
, &special_alts
);
576 list_for_each_entry_safe(special_alt
, tmp
, &special_alts
, list
) {
577 alt
= malloc(sizeof(*alt
));
579 WARN("malloc failed");
584 orig_insn
= find_insn(file
, special_alt
->orig_sec
,
585 special_alt
->orig_off
);
587 WARN_FUNC("special: can't find orig instruction",
588 special_alt
->orig_sec
, special_alt
->orig_off
);
594 if (!special_alt
->group
|| special_alt
->new_len
) {
595 new_insn
= find_insn(file
, special_alt
->new_sec
,
596 special_alt
->new_off
);
598 WARN_FUNC("special: can't find new instruction",
599 special_alt
->new_sec
,
600 special_alt
->new_off
);
606 if (special_alt
->group
) {
607 ret
= handle_group_alt(file
, special_alt
, orig_insn
,
611 } else if (special_alt
->jump_or_nop
) {
612 ret
= handle_jump_alt(file
, special_alt
, orig_insn
,
618 alt
->insn
= new_insn
;
619 list_add_tail(&alt
->list
, &orig_insn
->alts
);
621 list_del(&special_alt
->list
);
629 static int add_switch_table(struct objtool_file
*file
, struct symbol
*func
,
630 struct instruction
*insn
, struct rela
*table
,
631 struct rela
*next_table
)
633 struct rela
*rela
= table
;
634 struct instruction
*alt_insn
;
635 struct alternative
*alt
;
637 list_for_each_entry_from(rela
, &file
->rodata
->rela
->rela_list
, list
) {
638 if (rela
== next_table
)
641 if (rela
->sym
->sec
!= insn
->sec
||
642 rela
->addend
<= func
->offset
||
643 rela
->addend
>= func
->offset
+ func
->len
)
646 alt_insn
= find_insn(file
, insn
->sec
, rela
->addend
);
648 WARN("%s: can't find instruction at %s+0x%x",
649 file
->rodata
->rela
->name
, insn
->sec
->name
,
654 alt
= malloc(sizeof(*alt
));
656 WARN("malloc failed");
660 alt
->insn
= alt_insn
;
661 list_add_tail(&alt
->list
, &insn
->alts
);
667 static int add_func_switch_tables(struct objtool_file
*file
,
670 struct instruction
*insn
, *prev_jump
;
671 struct rela
*text_rela
, *rodata_rela
, *prev_rela
= NULL
;
676 func_for_each_insn(file
, func
, insn
) {
677 if (insn
->type
!= INSN_JUMP_DYNAMIC
)
680 text_rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
,
682 if (!text_rela
|| text_rela
->sym
!= file
->rodata
->sym
)
685 /* common case: jmpq *[addr](,%rax,8) */
686 rodata_rela
= find_rela_by_dest(file
->rodata
,
690 * rare case: jmpq *[addr](%rip)
692 * This check is for a rare gcc quirk, currently only seen in
693 * three driver functions in the kernel, only with certain
694 * obscure non-distro configs.
696 * As part of an optimization, gcc makes a copy of an existing
697 * switch jump table, modifies it, and then hard-codes the jump
698 * (albeit with an indirect jump) to use a single entry in the
699 * table. The rest of the jump table and some of its jump
700 * targets remain as dead code.
702 * In such a case we can just crudely ignore all unreachable
703 * instruction warnings for the entire object file. Ideally we
704 * would just ignore them for the function, but that would
705 * require redesigning the code quite a bit. And honestly
706 * that's just not worth doing: unreachable instruction
707 * warnings are of questionable value anyway, and this is such
711 * - https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
712 * - https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
713 * - https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com
716 * - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604
719 rodata_rela
= find_rela_by_dest(file
->rodata
,
720 text_rela
->addend
+ 4);
722 file
->ignore_unreachables
= true;
729 * We found a switch table, but we don't know yet how big it
730 * is. Don't add it until we reach the end of the function or
731 * the beginning of another switch table in the same function.
734 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
,
741 prev_rela
= rodata_rela
;
745 ret
= add_switch_table(file
, func
, prev_jump
, prev_rela
, NULL
);
754 * For some switch statements, gcc generates a jump table in the .rodata
755 * section which contains a list of addresses within the function to jump to.
756 * This finds these jump tables and adds them to the insn->alts lists.
758 static int add_switch_table_alts(struct objtool_file
*file
)
764 if (!file
->rodata
|| !file
->rodata
->rela
)
767 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
768 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
769 if (func
->type
!= STT_FUNC
)
772 ret
= add_func_switch_tables(file
, func
);
781 static int decode_sections(struct objtool_file
*file
)
785 ret
= decode_instructions(file
);
791 ret
= add_jump_destinations(file
);
795 ret
= add_call_destinations(file
);
799 ret
= add_special_section_alts(file
);
803 ret
= add_switch_table_alts(file
);
810 static bool is_fentry_call(struct instruction
*insn
)
812 if (insn
->type
== INSN_CALL
&&
813 insn
->call_dest
->type
== STT_NOTYPE
&&
814 !strcmp(insn
->call_dest
->name
, "__fentry__"))
820 static bool has_modified_stack_frame(struct instruction
*insn
)
822 return (insn
->state
& STATE_FP_SAVED
) ||
823 (insn
->state
& STATE_FP_SETUP
);
826 static bool has_valid_stack_frame(struct instruction
*insn
)
828 return (insn
->state
& STATE_FP_SAVED
) &&
829 (insn
->state
& STATE_FP_SETUP
);
832 static unsigned int frame_state(unsigned long state
)
834 return (state
& (STATE_FP_SAVED
| STATE_FP_SETUP
));
838 * Follow the branch starting at the given instruction, and recursively follow
839 * any other branches (jumps). Meanwhile, track the frame pointer state at
840 * each instruction and validate all the rules described in
841 * tools/objtool/Documentation/stack-validation.txt.
843 static int validate_branch(struct objtool_file
*file
,
844 struct instruction
*first
, unsigned char first_state
)
846 struct alternative
*alt
;
847 struct instruction
*insn
;
849 struct symbol
*func
= NULL
;
857 if (insn
->alt_group
&& list_empty(&insn
->alts
)) {
858 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
864 if (file
->c_file
&& insn
->func
) {
865 if (func
&& func
!= insn
->func
) {
866 WARN("%s() falls through to next function %s()",
867 func
->name
, insn
->func
->name
);
875 if (frame_state(insn
->state
) != frame_state(state
)) {
876 WARN_FUNC("frame pointer state mismatch",
884 insn
->visited
= true;
887 list_for_each_entry(alt
, &insn
->alts
, list
) {
888 ret
= validate_branch(file
, alt
->insn
, state
);
893 switch (insn
->type
) {
897 if (state
& STATE_FP_SAVED
) {
898 WARN_FUNC("duplicate frame pointer save",
902 state
|= STATE_FP_SAVED
;
908 if (state
& STATE_FP_SETUP
) {
909 WARN_FUNC("duplicate frame pointer setup",
913 state
|= STATE_FP_SETUP
;
917 case INSN_FP_RESTORE
:
919 if (has_valid_stack_frame(insn
))
920 state
&= ~STATE_FP_SETUP
;
922 state
&= ~STATE_FP_SAVED
;
927 if (!nofp
&& has_modified_stack_frame(insn
)) {
928 WARN_FUNC("return without frame pointer restore",
935 if (is_fentry_call(insn
)) {
936 state
|= STATE_FENTRY
;
940 ret
= dead_end_function(file
, insn
->call_dest
);
947 case INSN_CALL_DYNAMIC
:
948 if (!nofp
&& !has_valid_stack_frame(insn
)) {
949 WARN_FUNC("call without frame pointer save/setup",
955 case INSN_JUMP_CONDITIONAL
:
956 case INSN_JUMP_UNCONDITIONAL
:
957 if (insn
->jump_dest
) {
958 ret
= validate_branch(file
, insn
->jump_dest
,
962 } else if (has_modified_stack_frame(insn
)) {
963 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
966 } /* else it's a sibling call */
968 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
)
973 case INSN_JUMP_DYNAMIC
:
974 if (list_empty(&insn
->alts
) &&
975 has_modified_stack_frame(insn
)) {
976 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
990 insn
= next_insn_same_sec(file
, insn
);
992 WARN("%s: unexpected end of section", sec
->name
);
1000 static bool is_gcov_insn(struct instruction
*insn
)
1003 struct section
*sec
;
1005 unsigned long offset
;
1007 rela
= find_rela_by_dest_range(insn
->sec
, insn
->offset
, insn
->len
);
1011 if (rela
->sym
->type
!= STT_SECTION
)
1014 sec
= rela
->sym
->sec
;
1015 offset
= rela
->addend
+ insn
->offset
+ insn
->len
- rela
->offset
;
1017 list_for_each_entry(sym
, &sec
->symbol_list
, list
) {
1018 if (sym
->type
!= STT_OBJECT
)
1021 if (offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
1022 return (!memcmp(sym
->name
, "__gcov0.", 8));
1028 static bool is_kasan_insn(struct instruction
*insn
)
1030 return (insn
->type
== INSN_CALL
&&
1031 !strcmp(insn
->call_dest
->name
, "__asan_handle_no_return"));
1034 static bool is_ubsan_insn(struct instruction
*insn
)
1036 return (insn
->type
== INSN_CALL
&&
1037 !strcmp(insn
->call_dest
->name
,
1038 "__ubsan_handle_builtin_unreachable"));
1041 static bool ignore_unreachable_insn(struct symbol
*func
,
1042 struct instruction
*insn
)
1046 if (insn
->type
== INSN_NOP
)
1049 if (is_gcov_insn(insn
))
1053 * Check if this (or a subsequent) instruction is related to
1054 * CONFIG_UBSAN or CONFIG_KASAN.
1056 * End the search at 5 instructions to avoid going into the weeds.
1058 for (i
= 0; i
< 5; i
++) {
1060 if (is_kasan_insn(insn
) || is_ubsan_insn(insn
))
1063 if (insn
->type
== INSN_JUMP_UNCONDITIONAL
&& insn
->jump_dest
) {
1064 insn
= insn
->jump_dest
;
1068 if (insn
->offset
+ insn
->len
>= func
->offset
+ func
->len
)
1070 insn
= list_next_entry(insn
, list
);
1076 static int validate_functions(struct objtool_file
*file
)
1078 struct section
*sec
;
1079 struct symbol
*func
;
1080 struct instruction
*insn
;
1081 int ret
, warnings
= 0;
1083 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1084 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1085 if (func
->type
!= STT_FUNC
)
1088 insn
= find_insn(file
, sec
, func
->offset
);
1092 ret
= validate_branch(file
, insn
, 0);
1097 list_for_each_entry(sec
, &file
->elf
->sections
, list
) {
1098 list_for_each_entry(func
, &sec
->symbol_list
, list
) {
1099 if (func
->type
!= STT_FUNC
)
1102 func_for_each_insn(file
, func
, insn
) {
1106 insn
->visited
= true;
1108 if (file
->ignore_unreachables
|| warnings
||
1109 ignore_unreachable_insn(func
, insn
))
1112 WARN_FUNC("function has unreachable instruction", insn
->sec
, insn
->offset
);
1121 static int validate_uncallable_instructions(struct objtool_file
*file
)
1123 struct instruction
*insn
;
1126 for_each_insn(file
, insn
) {
1127 if (!insn
->visited
&& insn
->type
== INSN_RETURN
) {
1128 WARN_FUNC("return instruction outside of a callable function",
1129 insn
->sec
, insn
->offset
);
1137 static void cleanup(struct objtool_file
*file
)
1139 struct instruction
*insn
, *tmpinsn
;
1140 struct alternative
*alt
, *tmpalt
;
1142 list_for_each_entry_safe(insn
, tmpinsn
, &file
->insn_list
, list
) {
1143 list_for_each_entry_safe(alt
, tmpalt
, &insn
->alts
, list
) {
1144 list_del(&alt
->list
);
1147 list_del(&insn
->list
);
1148 hash_del(&insn
->hash
);
1151 elf_close(file
->elf
);
1154 const char * const check_usage
[] = {
1155 "objtool check [<options>] file.o",
1159 int cmd_check(int argc
, const char **argv
)
1161 struct objtool_file file
;
1162 int ret
, warnings
= 0;
1164 const struct option options
[] = {
1165 OPT_BOOLEAN('f', "no-fp", &nofp
, "Skip frame pointer validation"),
1169 argc
= parse_options(argc
, argv
, options
, check_usage
, 0);
1172 usage_with_options(check_usage
, options
);
1176 file
.elf
= elf_open(objname
);
1178 fprintf(stderr
, "error reading elf file %s\n", objname
);
1182 INIT_LIST_HEAD(&file
.insn_list
);
1183 hash_init(file
.insn_hash
);
1184 file
.whitelist
= find_section_by_name(file
.elf
, "__func_stack_frame_non_standard");
1185 file
.rodata
= find_section_by_name(file
.elf
, ".rodata");
1186 file
.ignore_unreachables
= false;
1187 file
.c_file
= find_section_by_name(file
.elf
, ".comment");
1189 ret
= decode_sections(&file
);
1194 ret
= validate_functions(&file
);
1199 ret
= validate_uncallable_instructions(&file
);
1207 /* ignore warnings for now until we get all the code cleaned up */
1208 if (ret
|| warnings
)
This page took 0.05436 seconds and 6 git commands to generate.