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 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
39 #define STATE_FP_SAVED 0x1
40 #define STATE_FP_SETUP 0x2
41 #define STATE_FENTRY 0x4
44 struct list_head list;
47 unsigned int len, state;
49 unsigned long immediate;
50 bool alt_group, visited;
51 struct symbol *call_dest;
52 struct instruction *jump_dest;
53 struct list_head alts;
57 struct list_head list;
58 struct instruction *insn;
63 struct list_head insns;
69 static struct instruction *find_instruction(struct objtool_file *file,
73 struct instruction *insn;
75 list_for_each_entry(insn, &file->insns, list)
76 if (insn->sec == sec && insn->offset == offset)
83 * Check if the function has been manually whitelisted with the
84 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
85 * due to its use of a context switching instruction.
87 static bool ignore_func(struct objtool_file *file, struct symbol *func)
89 struct section *macro_sec;
91 struct instruction *insn;
93 /* check for STACK_FRAME_NON_STANDARD */
94 macro_sec = find_section_by_name(file->elf, "__func_stack_frame_non_standard");
95 if (macro_sec && macro_sec->rela)
96 list_for_each_entry(rela, ¯o_sec->rela->relas, list)
97 if (rela->sym->sec == func->sec &&
98 rela->addend == func->offset)
101 /* check if it has a context switching instruction */
102 insn = find_instruction(file, func->sec, func->offset);
105 list_for_each_entry_from(insn, &file->insns, list) {
106 if (insn->sec != func->sec ||
107 insn->offset >= func->offset + func->len)
109 if (insn->type == INSN_CONTEXT_SWITCH)
117 * This checks to see if the given function is a "noreturn" function.
119 * For global functions which are outside the scope of this object file, we
120 * have to keep a manual list of them.
122 * For local functions, we have to detect them manually by simply looking for
123 * the lack of a return instruction.
130 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
134 struct instruction *insn, *func_insn;
138 * Unfortunately these have to be hard coded because the noreturn
139 * attribute isn't provided in ELF data.
141 static const char * const global_noreturns[] = {
145 "__module_put_and_exit",
147 "kvm_spurious_fault",
152 if (func->bind == STB_WEAK)
155 if (func->bind == STB_GLOBAL)
156 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
157 if (!strcmp(func->name, global_noreturns[i]))
163 func_insn = find_instruction(file, func->sec, func->offset);
168 list_for_each_entry_from(insn, &file->insns, list) {
169 if (insn->sec != func->sec ||
170 insn->offset >= func->offset + func->len)
175 if (insn->type == INSN_RETURN)
183 * A function can have a sibling call instead of a return. In that
184 * case, the function's dead-end status depends on whether the target
185 * of the sibling call returns.
188 list_for_each_entry_from(insn, &file->insns, list) {
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)
229 static int dead_end_function(struct objtool_file *file, struct symbol *func)
231 return __dead_end_function(file, func, 0);
235 * Call the arch-specific instruction decoder for all the instructions and add
236 * them to the global insns list.
238 static int decode_instructions(struct objtool_file *file)
241 unsigned long offset;
242 struct instruction *insn;
245 INIT_LIST_HEAD(&file->insns);
247 list_for_each_entry(sec, &file->elf->sections, list) {
249 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
252 for (offset = 0; offset < sec->len; offset += insn->len) {
253 insn = malloc(sizeof(*insn));
254 memset(insn, 0, sizeof(*insn));
256 INIT_LIST_HEAD(&insn->alts);
258 insn->offset = offset;
260 ret = arch_decode_instruction(file->elf, sec, offset,
262 &insn->len, &insn->type,
267 if (!insn->type || insn->type > INSN_LAST) {
268 WARN_FUNC("invalid instruction type %d",
269 insn->sec, insn->offset, insn->type);
273 list_add_tail(&insn->list, &file->insns);
281 * Warnings shouldn't be reported for ignored functions.
283 static void get_ignores(struct objtool_file *file)
285 struct instruction *insn;
289 list_for_each_entry(sec, &file->elf->sections, list) {
290 list_for_each_entry(func, &sec->symbols, list) {
291 if (func->type != STT_FUNC)
294 if (!ignore_func(file, func))
297 insn = find_instruction(file, sec, func->offset);
301 list_for_each_entry_from(insn, &file->insns, list) {
302 if (insn->sec != func->sec ||
303 insn->offset >= func->offset + func->len)
306 insn->visited = true;
313 * Find the destination instructions for all jumps.
315 static int get_jump_destinations(struct objtool_file *file)
317 struct instruction *insn;
319 struct section *dest_sec;
320 unsigned long dest_off;
322 list_for_each_entry(insn, &file->insns, list) {
323 if (insn->type != INSN_JUMP_CONDITIONAL &&
324 insn->type != INSN_JUMP_UNCONDITIONAL)
331 rela = find_rela_by_dest_range(insn->sec, insn->offset,
334 dest_sec = insn->sec;
335 dest_off = insn->offset + insn->len + insn->immediate;
336 } else if (rela->sym->type == STT_SECTION) {
337 dest_sec = rela->sym->sec;
338 dest_off = rela->addend + 4;
339 } else if (rela->sym->sec->idx) {
340 dest_sec = rela->sym->sec;
341 dest_off = rela->sym->sym.st_value + rela->addend + 4;
348 insn->jump_dest = find_instruction(file, dest_sec, dest_off);
349 if (!insn->jump_dest) {
352 * This is a special case where an alt instruction
353 * jumps past the end of the section. These are
354 * handled later in handle_group_alt().
356 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
359 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
360 insn->sec, insn->offset, dest_sec->name,
370 * Find the destination instructions for all calls.
372 static int get_call_destinations(struct objtool_file *file)
374 struct instruction *insn;
375 unsigned long dest_off;
378 list_for_each_entry(insn, &file->insns, list) {
379 if (insn->type != INSN_CALL)
382 rela = find_rela_by_dest_range(insn->sec, insn->offset,
385 dest_off = insn->offset + insn->len + insn->immediate;
386 insn->call_dest = find_symbol_by_offset(insn->sec,
388 if (!insn->call_dest) {
389 WARN_FUNC("can't find call dest symbol at offset 0x%lx",
390 insn->sec, insn->offset, dest_off);
393 } else if (rela->sym->type == STT_SECTION) {
394 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
396 if (!insn->call_dest ||
397 insn->call_dest->type != STT_FUNC) {
398 WARN_FUNC("can't find call dest symbol at %s+0x%x",
399 insn->sec, insn->offset,
400 rela->sym->sec->name,
405 insn->call_dest = rela->sym;
412 * The .alternatives section requires some extra special care, over and above
413 * what other special sections require:
415 * 1. Because alternatives are patched in-place, we need to insert a fake jump
416 * instruction at the end so that validate_branch() skips all the original
417 * replaced instructions when validating the new instruction path.
419 * 2. An added wrinkle is that the new instruction length might be zero. In
420 * that case the old instructions are replaced with noops. We simulate that
421 * by creating a fake jump as the only new instruction.
423 * 3. In some cases, the alternative section includes an instruction which
424 * conditionally jumps to the _end_ of the entry. We have to modify these
425 * jumps' destinations to point back to .text rather than the end of the
426 * entry in .altinstr_replacement.
428 * 4. It has been requested that we don't validate the !POPCNT feature path
429 * which is a "very very small percentage of machines".
431 static int handle_group_alt(struct objtool_file *file,
432 struct special_alt *special_alt,
433 struct instruction *orig_insn,
434 struct instruction **new_insn)
436 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
437 unsigned long dest_off;
439 last_orig_insn = NULL;
441 list_for_each_entry_from(insn, &file->insns, list) {
442 if (insn->sec != special_alt->orig_sec ||
443 insn->offset >= special_alt->orig_off + special_alt->orig_len)
446 if (special_alt->skip_orig)
447 insn->type = INSN_NOP;
449 insn->alt_group = true;
450 last_orig_insn = insn;
453 if (list_is_last(&last_orig_insn->list, &file->insns) ||
454 list_next_entry(last_orig_insn, list)->sec != special_alt->orig_sec) {
455 WARN("%s: don't know how to handle alternatives at end of section",
456 special_alt->orig_sec->name);
460 fake_jump = malloc(sizeof(*fake_jump));
462 WARN("malloc failed");
465 memset(fake_jump, 0, sizeof(*fake_jump));
466 INIT_LIST_HEAD(&fake_jump->alts);
467 fake_jump->sec = special_alt->new_sec;
468 fake_jump->offset = -1;
469 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
470 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
472 if (!special_alt->new_len) {
473 *new_insn = fake_jump;
477 last_new_insn = NULL;
479 list_for_each_entry_from(insn, &file->insns, list) {
480 if (insn->sec != special_alt->new_sec ||
481 insn->offset >= special_alt->new_off + special_alt->new_len)
484 last_new_insn = insn;
486 if (insn->type != INSN_JUMP_CONDITIONAL &&
487 insn->type != INSN_JUMP_UNCONDITIONAL)
490 if (!insn->immediate)
493 dest_off = insn->offset + insn->len + insn->immediate;
494 if (dest_off == special_alt->new_off + special_alt->new_len)
495 insn->jump_dest = fake_jump;
497 if (!insn->jump_dest) {
498 WARN_FUNC("can't find alternative jump destination",
499 insn->sec, insn->offset);
504 if (!last_new_insn) {
505 WARN_FUNC("can't find last new alternative instruction",
506 special_alt->new_sec, special_alt->new_off);
510 list_add(&fake_jump->list, &last_new_insn->list);
516 * A jump table entry can either convert a nop to a jump or a jump to a nop.
517 * If the original instruction is a jump, make the alt entry an effective nop
518 * by just skipping the original instruction.
520 static int handle_jump_alt(struct objtool_file *file,
521 struct special_alt *special_alt,
522 struct instruction *orig_insn,
523 struct instruction **new_insn)
525 if (orig_insn->type == INSN_NOP)
528 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
529 WARN_FUNC("unsupported instruction at jump label",
530 orig_insn->sec, orig_insn->offset);
534 *new_insn = list_next_entry(orig_insn, list);
539 * Read all the special sections which have alternate instructions which can be
540 * patched in or redirected to at runtime. Each instruction having alternate
541 * instruction(s) has them added to its insn->alts list, which will be
542 * traversed in validate_branch().
544 static int get_special_section_alts(struct objtool_file *file)
546 struct list_head special_alts;
547 struct instruction *orig_insn, *new_insn;
548 struct special_alt *special_alt, *tmp;
549 struct alternative *alt;
552 ret = special_get_alts(file->elf, &special_alts);
556 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
557 alt = malloc(sizeof(*alt));
559 WARN("malloc failed");
564 orig_insn = find_instruction(file, special_alt->orig_sec,
565 special_alt->orig_off);
567 WARN_FUNC("special: can't find orig instruction",
568 special_alt->orig_sec, special_alt->orig_off);
574 if (!special_alt->group || special_alt->new_len) {
575 new_insn = find_instruction(file, special_alt->new_sec,
576 special_alt->new_off);
578 WARN_FUNC("special: can't find new instruction",
579 special_alt->new_sec,
580 special_alt->new_off);
586 if (special_alt->group) {
587 ret = handle_group_alt(file, special_alt, orig_insn,
591 } else if (special_alt->jump_or_nop) {
592 ret = handle_jump_alt(file, special_alt, orig_insn,
598 alt->insn = new_insn;
599 list_add_tail(&alt->list, &orig_insn->alts);
601 list_del(&special_alt->list);
610 * For some switch statements, gcc generates a jump table in the .rodata
611 * section which contains a list of addresses within the function to jump to.
612 * This finds these jump tables and adds them to the insn->alts lists.
614 static int get_switch_alts(struct objtool_file *file)
616 struct instruction *insn, *alt_insn;
617 struct rela *rodata_rela, *rela;
618 struct section *rodata;
620 struct alternative *alt;
622 list_for_each_entry(insn, &file->insns, list) {
623 if (insn->type != INSN_JUMP_DYNAMIC)
626 rodata_rela = find_rela_by_dest_range(insn->sec, insn->offset,
628 if (!rodata_rela || strcmp(rodata_rela->sym->name, ".rodata"))
631 rodata = find_section_by_name(file->elf, ".rodata");
632 if (!rodata || !rodata->rela)
635 /* common case: jmpq *[addr](,%rax,8) */
636 rela = find_rela_by_dest(rodata, rodata_rela->addend);
638 /* rare case: jmpq *[addr](%rip) */
640 rela = find_rela_by_dest(rodata,
641 rodata_rela->addend + 4);
645 func = find_containing_func(insn->sec, insn->offset);
647 WARN_FUNC("can't find containing func",
648 insn->sec, insn->offset);
652 list_for_each_entry_from(rela, &rodata->rela->relas, list) {
653 if (rela->sym->sec != insn->sec ||
654 rela->addend <= func->offset ||
655 rela->addend >= func->offset + func->len)
658 alt_insn = find_instruction(file, insn->sec,
661 WARN("%s: can't find instruction at %s+0x%x",
662 rodata->rela->name, insn->sec->name,
667 alt = malloc(sizeof(*alt));
669 WARN("malloc failed");
673 alt->insn = alt_insn;
674 list_add_tail(&alt->list, &insn->alts);
681 static int decode_sections(struct objtool_file *file)
685 ret = decode_instructions(file);
691 ret = get_jump_destinations(file);
695 ret = get_call_destinations(file);
699 ret = get_special_section_alts(file);
703 ret = get_switch_alts(file);
710 static bool is_fentry_call(struct instruction *insn)
712 if (insn->type == INSN_CALL &&
713 insn->call_dest->type == STT_NOTYPE &&
714 !strcmp(insn->call_dest->name, "__fentry__"))
720 static bool has_modified_stack_frame(struct instruction *insn)
722 return (insn->state & STATE_FP_SAVED) ||
723 (insn->state & STATE_FP_SETUP);
726 static bool has_valid_stack_frame(struct instruction *insn)
728 return (insn->state & STATE_FP_SAVED) &&
729 (insn->state & STATE_FP_SETUP);
733 * Follow the branch starting at the given instruction, and recursively follow
734 * any other branches (jumps). Meanwhile, track the frame pointer state at
735 * each instruction and validate all the rules described in
736 * tools/objtool/Documentation/stack-validation.txt.
738 static int validate_branch(struct objtool_file *file,
739 struct instruction *first, unsigned char first_state)
741 struct alternative *alt;
742 struct instruction *insn;
745 int ret, warnings = 0;
751 if (insn->alt_group && list_empty(&insn->alts)) {
752 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
759 if (insn->state != state) {
760 WARN_FUNC("frame pointer state mismatch",
769 * Catch a rare case where a noreturn function falls through to
772 if (is_fentry_call(insn) && (state & STATE_FENTRY))
775 insn->visited = true;
778 list_for_each_entry(alt, &insn->alts, list) {
779 ret = validate_branch(file, alt->insn, state);
783 switch (insn->type) {
787 if (state & STATE_FP_SAVED) {
788 WARN_FUNC("duplicate frame pointer save",
792 state |= STATE_FP_SAVED;
798 if (state & STATE_FP_SETUP) {
799 WARN_FUNC("duplicate frame pointer setup",
803 state |= STATE_FP_SETUP;
807 case INSN_FP_RESTORE:
809 if (has_valid_stack_frame(insn))
810 state &= ~STATE_FP_SETUP;
812 state &= ~STATE_FP_SAVED;
817 if (!nofp && has_modified_stack_frame(insn)) {
818 WARN_FUNC("return without frame pointer restore",
825 if (is_fentry_call(insn)) {
826 state |= STATE_FENTRY;
830 ret = dead_end_function(file, insn->call_dest);
837 case INSN_CALL_DYNAMIC:
838 if (!nofp && !has_valid_stack_frame(insn)) {
839 WARN_FUNC("call without frame pointer save/setup",
845 case INSN_JUMP_CONDITIONAL:
846 case INSN_JUMP_UNCONDITIONAL:
847 if (insn->jump_dest) {
848 ret = validate_branch(file, insn->jump_dest,
851 } else if (has_modified_stack_frame(insn)) {
852 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
855 } /* else it's a sibling call */
857 if (insn->type == INSN_JUMP_UNCONDITIONAL)
862 case INSN_JUMP_DYNAMIC:
863 if (list_empty(&insn->alts) &&
864 has_modified_stack_frame(insn)) {
865 WARN_FUNC("sibling call from callable instruction with changed frame pointer",
879 insn = list_next_entry(insn, list);
881 if (&insn->list == &file->insns || insn->sec != sec) {
882 WARN("%s: unexpected end of section", sec->name);
891 static bool is_gcov_insn(struct instruction *insn)
896 unsigned long offset;
898 rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
902 if (rela->sym->type != STT_SECTION)
905 sec = rela->sym->sec;
906 offset = rela->addend + insn->offset + insn->len - rela->offset;
908 list_for_each_entry(sym, &sec->symbols, list) {
909 if (sym->type != STT_OBJECT)
912 if (offset >= sym->offset && offset < sym->offset + sym->len)
913 return (!memcmp(sym->name, "__gcov0.", 8));
919 static bool is_kasan_insn(struct instruction *insn)
921 return (insn->type == INSN_CALL &&
922 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
925 static bool is_ubsan_insn(struct instruction *insn)
927 return (insn->type == INSN_CALL &&
928 !strcmp(insn->call_dest->name,
929 "__ubsan_handle_builtin_unreachable"));
932 static bool ignore_unreachable_insn(struct instruction *insn,
933 unsigned long func_end)
937 if (insn->type == INSN_NOP)
940 if (is_gcov_insn(insn))
944 * Check if this (or a subsequent) instruction is related to
945 * CONFIG_UBSAN or CONFIG_KASAN.
947 * End the search at 5 instructions to avoid going into the weeds.
949 for (i = 0; i < 5; i++) {
951 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
954 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
955 insn = insn->jump_dest;
959 if (insn->offset + insn->len >= func_end)
961 insn = list_next_entry(insn, list);
967 static int validate_functions(struct objtool_file *file)
971 struct instruction *insn;
972 unsigned long func_end;
973 int ret, warnings = 0;
975 list_for_each_entry(sec, &file->elf->sections, list) {
976 list_for_each_entry(func, &sec->symbols, list) {
977 if (func->type != STT_FUNC)
980 insn = find_instruction(file, sec, func->offset);
982 WARN("%s(): can't find starting instruction",
988 ret = validate_branch(file, insn, 0);
993 list_for_each_entry(sec, &file->elf->sections, list) {
994 list_for_each_entry(func, &sec->symbols, list) {
995 if (func->type != STT_FUNC)
998 insn = find_instruction(file, sec, func->offset);
1002 func_end = func->offset + func->len;
1004 list_for_each_entry_from(insn, &file->insns, list) {
1005 if (insn->sec != func->sec ||
1006 insn->offset >= func_end)
1012 if (!ignore_unreachable_insn(insn, func_end)) {
1013 WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
1017 insn->visited = true;
1025 static int validate_uncallable_instructions(struct objtool_file *file)
1027 struct instruction *insn;
1030 list_for_each_entry(insn, &file->insns, list) {
1031 if (!insn->visited && insn->type == INSN_RETURN) {
1032 WARN_FUNC("return instruction outside of a callable function",
1033 insn->sec, insn->offset);
1041 static void cleanup(struct objtool_file *file)
1043 struct instruction *insn, *tmpinsn;
1044 struct alternative *alt, *tmpalt;
1046 list_for_each_entry_safe(insn, tmpinsn, &file->insns, list) {
1047 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
1048 list_del(&alt->list);
1051 list_del(&insn->list);
1054 elf_close(file->elf);
1057 const char * const check_usage[] = {
1058 "objtool check [<options>] file.o",
1062 int cmd_check(int argc, const char **argv)
1064 struct objtool_file file;
1065 int ret, warnings = 0;
1067 const struct option options[] = {
1068 OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
1072 argc = parse_options(argc, argv, options, check_usage, 0);
1075 usage_with_options(check_usage, options);
1079 file.elf = elf_open(objname);
1081 fprintf(stderr, "error reading elf file %s\n", objname);
1085 INIT_LIST_HEAD(&file.insns);
1087 ret = decode_sections(&file);
1092 ret = validate_functions(&file);
1097 ret = validate_uncallable_instructions(&file);
1105 /* ignore warnings for now until we get all the code cleaned up */
1106 if (ret || warnings)