Linux 5.7.7
[linux/fpc-iii.git] / tools / objtool / check.c
blob5a867a469ba529fffa50c34f771476f25623bc22
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
6 #include <string.h>
7 #include <stdlib.h>
9 #include "builtin.h"
10 #include "check.h"
11 #include "elf.h"
12 #include "special.h"
13 #include "arch.h"
14 #include "warn.h"
16 #include <linux/hashtable.h>
17 #include <linux/kernel.h>
19 #define FAKE_JUMP_OFFSET -1
21 #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
23 struct alternative {
24 struct list_head list;
25 struct instruction *insn;
26 bool skip_orig;
29 const char *objname;
30 struct cfi_state initial_func_cfi;
32 struct instruction *find_insn(struct objtool_file *file,
33 struct section *sec, unsigned long offset)
35 struct instruction *insn;
37 hash_for_each_possible(file->insn_hash, insn, hash, offset)
38 if (insn->sec == sec && insn->offset == offset)
39 return insn;
41 return NULL;
44 static struct instruction *next_insn_same_sec(struct objtool_file *file,
45 struct instruction *insn)
47 struct instruction *next = list_next_entry(insn, list);
49 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
50 return NULL;
52 return next;
55 static struct instruction *next_insn_same_func(struct objtool_file *file,
56 struct instruction *insn)
58 struct instruction *next = list_next_entry(insn, list);
59 struct symbol *func = insn->func;
61 if (!func)
62 return NULL;
64 if (&next->list != &file->insn_list && next->func == func)
65 return next;
67 /* Check if we're already in the subfunction: */
68 if (func == func->cfunc)
69 return NULL;
71 /* Move to the subfunction: */
72 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
75 static struct instruction *prev_insn_same_sym(struct objtool_file *file,
76 struct instruction *insn)
78 struct instruction *prev = list_prev_entry(insn, list);
80 if (&prev->list != &file->insn_list && prev->func == insn->func)
81 return prev;
83 return NULL;
86 #define func_for_each_insn(file, func, insn) \
87 for (insn = find_insn(file, func->sec, func->offset); \
88 insn; \
89 insn = next_insn_same_func(file, insn))
91 #define sym_for_each_insn(file, sym, insn) \
92 for (insn = find_insn(file, sym->sec, sym->offset); \
93 insn && &insn->list != &file->insn_list && \
94 insn->sec == sym->sec && \
95 insn->offset < sym->offset + sym->len; \
96 insn = list_next_entry(insn, list))
98 #define sym_for_each_insn_continue_reverse(file, sym, insn) \
99 for (insn = list_prev_entry(insn, list); \
100 &insn->list != &file->insn_list && \
101 insn->sec == sym->sec && insn->offset >= sym->offset; \
102 insn = list_prev_entry(insn, list))
104 #define sec_for_each_insn_from(file, insn) \
105 for (; insn; insn = next_insn_same_sec(file, insn))
107 #define sec_for_each_insn_continue(file, insn) \
108 for (insn = next_insn_same_sec(file, insn); insn; \
109 insn = next_insn_same_sec(file, insn))
111 static bool is_static_jump(struct instruction *insn)
113 return insn->type == INSN_JUMP_CONDITIONAL ||
114 insn->type == INSN_JUMP_UNCONDITIONAL;
117 static bool is_sibling_call(struct instruction *insn)
119 /* An indirect jump is either a sibling call or a jump to a table. */
120 if (insn->type == INSN_JUMP_DYNAMIC)
121 return list_empty(&insn->alts);
123 if (!is_static_jump(insn))
124 return false;
126 /* add_jump_destinations() sets insn->call_dest for sibling calls. */
127 return !!insn->call_dest;
131 * This checks to see if the given function is a "noreturn" function.
133 * For global functions which are outside the scope of this object file, we
134 * have to keep a manual list of them.
136 * For local functions, we have to detect them manually by simply looking for
137 * the lack of a return instruction.
139 static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
140 int recursion)
142 int i;
143 struct instruction *insn;
144 bool empty = true;
147 * Unfortunately these have to be hard coded because the noreturn
148 * attribute isn't provided in ELF data.
150 static const char * const global_noreturns[] = {
151 "__stack_chk_fail",
152 "panic",
153 "do_exit",
154 "do_task_dead",
155 "__module_put_and_exit",
156 "complete_and_exit",
157 "__reiserfs_panic",
158 "lbug_with_loc",
159 "fortify_panic",
160 "usercopy_abort",
161 "machine_real_restart",
162 "rewind_stack_do_exit",
163 "kunit_try_catch_throw",
166 if (!func)
167 return false;
169 if (func->bind == STB_WEAK)
170 return false;
172 if (func->bind == STB_GLOBAL)
173 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
174 if (!strcmp(func->name, global_noreturns[i]))
175 return true;
177 if (!func->len)
178 return false;
180 insn = find_insn(file, func->sec, func->offset);
181 if (!insn->func)
182 return false;
184 func_for_each_insn(file, func, insn) {
185 empty = false;
187 if (insn->type == INSN_RETURN)
188 return false;
191 if (empty)
192 return false;
195 * A function can have a sibling call instead of a return. In that
196 * case, the function's dead-end status depends on whether the target
197 * of the sibling call returns.
199 func_for_each_insn(file, func, insn) {
200 if (is_sibling_call(insn)) {
201 struct instruction *dest = insn->jump_dest;
203 if (!dest)
204 /* sibling call to another file */
205 return false;
207 /* local sibling call */
208 if (recursion == 5) {
210 * Infinite recursion: two functions have
211 * sibling calls to each other. This is a very
212 * rare case. It means they aren't dead ends.
214 return false;
217 return __dead_end_function(file, dest->func, recursion+1);
221 return true;
224 static bool dead_end_function(struct objtool_file *file, struct symbol *func)
226 return __dead_end_function(file, func, 0);
229 static void clear_insn_state(struct insn_state *state)
231 int i;
233 memset(state, 0, sizeof(*state));
234 state->cfa.base = CFI_UNDEFINED;
235 for (i = 0; i < CFI_NUM_REGS; i++) {
236 state->regs[i].base = CFI_UNDEFINED;
237 state->vals[i].base = CFI_UNDEFINED;
239 state->drap_reg = CFI_UNDEFINED;
240 state->drap_offset = -1;
244 * Call the arch-specific instruction decoder for all the instructions and add
245 * them to the global instruction list.
247 static int decode_instructions(struct objtool_file *file)
249 struct section *sec;
250 struct symbol *func;
251 unsigned long offset;
252 struct instruction *insn;
253 unsigned long nr_insns = 0;
254 int ret;
256 for_each_sec(file, sec) {
258 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
259 continue;
261 if (strcmp(sec->name, ".altinstr_replacement") &&
262 strcmp(sec->name, ".altinstr_aux") &&
263 strncmp(sec->name, ".discard.", 9))
264 sec->text = true;
266 for (offset = 0; offset < sec->len; offset += insn->len) {
267 insn = malloc(sizeof(*insn));
268 if (!insn) {
269 WARN("malloc failed");
270 return -1;
272 memset(insn, 0, sizeof(*insn));
273 INIT_LIST_HEAD(&insn->alts);
274 clear_insn_state(&insn->state);
276 insn->sec = sec;
277 insn->offset = offset;
279 ret = arch_decode_instruction(file->elf, sec, offset,
280 sec->len - offset,
281 &insn->len, &insn->type,
282 &insn->immediate,
283 &insn->stack_op);
284 if (ret)
285 goto err;
287 hash_add(file->insn_hash, &insn->hash, insn->offset);
288 list_add_tail(&insn->list, &file->insn_list);
289 nr_insns++;
292 list_for_each_entry(func, &sec->symbol_list, list) {
293 if (func->type != STT_FUNC || func->alias != func)
294 continue;
296 if (!find_insn(file, sec, func->offset)) {
297 WARN("%s(): can't find starting instruction",
298 func->name);
299 return -1;
302 sym_for_each_insn(file, func, insn)
303 insn->func = func;
307 if (stats)
308 printf("nr_insns: %lu\n", nr_insns);
310 return 0;
312 err:
313 free(insn);
314 return ret;
318 * Mark "ud2" instructions and manually annotated dead ends.
320 static int add_dead_ends(struct objtool_file *file)
322 struct section *sec;
323 struct rela *rela;
324 struct instruction *insn;
325 bool found;
328 * By default, "ud2" is a dead end unless otherwise annotated, because
329 * GCC 7 inserts it for certain divide-by-zero cases.
331 for_each_insn(file, insn)
332 if (insn->type == INSN_BUG)
333 insn->dead_end = true;
336 * Check for manually annotated dead ends.
338 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
339 if (!sec)
340 goto reachable;
342 list_for_each_entry(rela, &sec->rela_list, list) {
343 if (rela->sym->type != STT_SECTION) {
344 WARN("unexpected relocation symbol type in %s", sec->name);
345 return -1;
347 insn = find_insn(file, rela->sym->sec, rela->addend);
348 if (insn)
349 insn = list_prev_entry(insn, list);
350 else if (rela->addend == rela->sym->sec->len) {
351 found = false;
352 list_for_each_entry_reverse(insn, &file->insn_list, list) {
353 if (insn->sec == rela->sym->sec) {
354 found = true;
355 break;
359 if (!found) {
360 WARN("can't find unreachable insn at %s+0x%x",
361 rela->sym->sec->name, rela->addend);
362 return -1;
364 } else {
365 WARN("can't find unreachable insn at %s+0x%x",
366 rela->sym->sec->name, rela->addend);
367 return -1;
370 insn->dead_end = true;
373 reachable:
375 * These manually annotated reachable checks are needed for GCC 4.4,
376 * where the Linux unreachable() macro isn't supported. In that case
377 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
378 * not a dead end.
380 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
381 if (!sec)
382 return 0;
384 list_for_each_entry(rela, &sec->rela_list, list) {
385 if (rela->sym->type != STT_SECTION) {
386 WARN("unexpected relocation symbol type in %s", sec->name);
387 return -1;
389 insn = find_insn(file, rela->sym->sec, rela->addend);
390 if (insn)
391 insn = list_prev_entry(insn, list);
392 else if (rela->addend == rela->sym->sec->len) {
393 found = false;
394 list_for_each_entry_reverse(insn, &file->insn_list, list) {
395 if (insn->sec == rela->sym->sec) {
396 found = true;
397 break;
401 if (!found) {
402 WARN("can't find reachable insn at %s+0x%x",
403 rela->sym->sec->name, rela->addend);
404 return -1;
406 } else {
407 WARN("can't find reachable insn at %s+0x%x",
408 rela->sym->sec->name, rela->addend);
409 return -1;
412 insn->dead_end = false;
415 return 0;
419 * Warnings shouldn't be reported for ignored functions.
421 static void add_ignores(struct objtool_file *file)
423 struct instruction *insn;
424 struct section *sec;
425 struct symbol *func;
426 struct rela *rela;
428 sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
429 if (!sec)
430 return;
432 list_for_each_entry(rela, &sec->rela_list, list) {
433 switch (rela->sym->type) {
434 case STT_FUNC:
435 func = rela->sym;
436 break;
438 case STT_SECTION:
439 func = find_func_by_offset(rela->sym->sec, rela->addend);
440 if (!func)
441 continue;
442 break;
444 default:
445 WARN("unexpected relocation symbol type in %s: %d", sec->name, rela->sym->type);
446 continue;
449 func_for_each_insn(file, func, insn)
450 insn->ignore = true;
455 * This is a whitelist of functions that is allowed to be called with AC set.
456 * The list is meant to be minimal and only contains compiler instrumentation
457 * ABI and a few functions used to implement *_{to,from}_user() functions.
459 * These functions must not directly change AC, but may PUSHF/POPF.
461 static const char *uaccess_safe_builtin[] = {
462 /* KASAN */
463 "kasan_report",
464 "check_memory_region",
465 /* KASAN out-of-line */
466 "__asan_loadN_noabort",
467 "__asan_load1_noabort",
468 "__asan_load2_noabort",
469 "__asan_load4_noabort",
470 "__asan_load8_noabort",
471 "__asan_load16_noabort",
472 "__asan_storeN_noabort",
473 "__asan_store1_noabort",
474 "__asan_store2_noabort",
475 "__asan_store4_noabort",
476 "__asan_store8_noabort",
477 "__asan_store16_noabort",
478 /* KASAN in-line */
479 "__asan_report_load_n_noabort",
480 "__asan_report_load1_noabort",
481 "__asan_report_load2_noabort",
482 "__asan_report_load4_noabort",
483 "__asan_report_load8_noabort",
484 "__asan_report_load16_noabort",
485 "__asan_report_store_n_noabort",
486 "__asan_report_store1_noabort",
487 "__asan_report_store2_noabort",
488 "__asan_report_store4_noabort",
489 "__asan_report_store8_noabort",
490 "__asan_report_store16_noabort",
491 /* KCOV */
492 "write_comp_data",
493 "__sanitizer_cov_trace_pc",
494 "__sanitizer_cov_trace_const_cmp1",
495 "__sanitizer_cov_trace_const_cmp2",
496 "__sanitizer_cov_trace_const_cmp4",
497 "__sanitizer_cov_trace_const_cmp8",
498 "__sanitizer_cov_trace_cmp1",
499 "__sanitizer_cov_trace_cmp2",
500 "__sanitizer_cov_trace_cmp4",
501 "__sanitizer_cov_trace_cmp8",
502 "__sanitizer_cov_trace_switch",
503 /* UBSAN */
504 "ubsan_type_mismatch_common",
505 "__ubsan_handle_type_mismatch",
506 "__ubsan_handle_type_mismatch_v1",
507 "__ubsan_handle_shift_out_of_bounds",
508 /* misc */
509 "csum_partial_copy_generic",
510 "__memcpy_mcsafe",
511 "mcsafe_handle_tail",
512 "ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
513 NULL
516 static void add_uaccess_safe(struct objtool_file *file)
518 struct symbol *func;
519 const char **name;
521 if (!uaccess)
522 return;
524 for (name = uaccess_safe_builtin; *name; name++) {
525 func = find_symbol_by_name(file->elf, *name);
526 if (!func)
527 continue;
529 func->uaccess_safe = true;
534 * FIXME: For now, just ignore any alternatives which add retpolines. This is
535 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
536 * But it at least allows objtool to understand the control flow *around* the
537 * retpoline.
539 static int add_ignore_alternatives(struct objtool_file *file)
541 struct section *sec;
542 struct rela *rela;
543 struct instruction *insn;
545 sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
546 if (!sec)
547 return 0;
549 list_for_each_entry(rela, &sec->rela_list, list) {
550 if (rela->sym->type != STT_SECTION) {
551 WARN("unexpected relocation symbol type in %s", sec->name);
552 return -1;
555 insn = find_insn(file, rela->sym->sec, rela->addend);
556 if (!insn) {
557 WARN("bad .discard.ignore_alts entry");
558 return -1;
561 insn->ignore_alts = true;
564 return 0;
568 * Find the destination instructions for all jumps.
570 static int add_jump_destinations(struct objtool_file *file)
572 struct instruction *insn;
573 struct rela *rela;
574 struct section *dest_sec;
575 unsigned long dest_off;
577 for_each_insn(file, insn) {
578 if (!is_static_jump(insn))
579 continue;
581 if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
582 continue;
584 rela = find_rela_by_dest_range(file->elf, insn->sec,
585 insn->offset, insn->len);
586 if (!rela) {
587 dest_sec = insn->sec;
588 dest_off = insn->offset + insn->len + insn->immediate;
589 } else if (rela->sym->type == STT_SECTION) {
590 dest_sec = rela->sym->sec;
591 dest_off = rela->addend + 4;
592 } else if (rela->sym->sec->idx) {
593 dest_sec = rela->sym->sec;
594 dest_off = rela->sym->sym.st_value + rela->addend + 4;
595 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
597 * Retpoline jumps are really dynamic jumps in
598 * disguise, so convert them accordingly.
600 if (insn->type == INSN_JUMP_UNCONDITIONAL)
601 insn->type = INSN_JUMP_DYNAMIC;
602 else
603 insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
605 insn->retpoline_safe = true;
606 continue;
607 } else {
608 /* external sibling call */
609 insn->call_dest = rela->sym;
610 continue;
613 insn->jump_dest = find_insn(file, dest_sec, dest_off);
614 if (!insn->jump_dest) {
617 * This is a special case where an alt instruction
618 * jumps past the end of the section. These are
619 * handled later in handle_group_alt().
621 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
622 continue;
624 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
625 insn->sec, insn->offset, dest_sec->name,
626 dest_off);
627 return -1;
631 * Cross-function jump.
633 if (insn->func && insn->jump_dest->func &&
634 insn->func != insn->jump_dest->func) {
637 * For GCC 8+, create parent/child links for any cold
638 * subfunctions. This is _mostly_ redundant with a
639 * similar initialization in read_symbols().
641 * If a function has aliases, we want the *first* such
642 * function in the symbol table to be the subfunction's
643 * parent. In that case we overwrite the
644 * initialization done in read_symbols().
646 * However this code can't completely replace the
647 * read_symbols() code because this doesn't detect the
648 * case where the parent function's only reference to a
649 * subfunction is through a jump table.
651 if (!strstr(insn->func->name, ".cold.") &&
652 strstr(insn->jump_dest->func->name, ".cold.")) {
653 insn->func->cfunc = insn->jump_dest->func;
654 insn->jump_dest->func->pfunc = insn->func;
656 } else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
657 insn->jump_dest->offset == insn->jump_dest->func->offset) {
659 /* internal sibling call */
660 insn->call_dest = insn->jump_dest->func;
665 return 0;
669 * Find the destination instructions for all calls.
671 static int add_call_destinations(struct objtool_file *file)
673 struct instruction *insn;
674 unsigned long dest_off;
675 struct rela *rela;
677 for_each_insn(file, insn) {
678 if (insn->type != INSN_CALL)
679 continue;
681 rela = find_rela_by_dest_range(file->elf, insn->sec,
682 insn->offset, insn->len);
683 if (!rela) {
684 dest_off = insn->offset + insn->len + insn->immediate;
685 insn->call_dest = find_func_by_offset(insn->sec, dest_off);
686 if (!insn->call_dest)
687 insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
689 if (insn->ignore)
690 continue;
692 if (!insn->call_dest) {
693 WARN_FUNC("unsupported intra-function call",
694 insn->sec, insn->offset);
695 if (retpoline)
696 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
697 return -1;
700 if (insn->func && insn->call_dest->type != STT_FUNC) {
701 WARN_FUNC("unsupported call to non-function",
702 insn->sec, insn->offset);
703 return -1;
706 } else if (rela->sym->type == STT_SECTION) {
707 insn->call_dest = find_func_by_offset(rela->sym->sec,
708 rela->addend+4);
709 if (!insn->call_dest) {
710 WARN_FUNC("can't find call dest symbol at %s+0x%x",
711 insn->sec, insn->offset,
712 rela->sym->sec->name,
713 rela->addend + 4);
714 return -1;
716 } else
717 insn->call_dest = rela->sym;
720 return 0;
724 * The .alternatives section requires some extra special care, over and above
725 * what other special sections require:
727 * 1. Because alternatives are patched in-place, we need to insert a fake jump
728 * instruction at the end so that validate_branch() skips all the original
729 * replaced instructions when validating the new instruction path.
731 * 2. An added wrinkle is that the new instruction length might be zero. In
732 * that case the old instructions are replaced with noops. We simulate that
733 * by creating a fake jump as the only new instruction.
735 * 3. In some cases, the alternative section includes an instruction which
736 * conditionally jumps to the _end_ of the entry. We have to modify these
737 * jumps' destinations to point back to .text rather than the end of the
738 * entry in .altinstr_replacement.
740 static int handle_group_alt(struct objtool_file *file,
741 struct special_alt *special_alt,
742 struct instruction *orig_insn,
743 struct instruction **new_insn)
745 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
746 unsigned long dest_off;
748 last_orig_insn = NULL;
749 insn = orig_insn;
750 sec_for_each_insn_from(file, insn) {
751 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
752 break;
754 insn->alt_group = true;
755 last_orig_insn = insn;
758 if (next_insn_same_sec(file, last_orig_insn)) {
759 fake_jump = malloc(sizeof(*fake_jump));
760 if (!fake_jump) {
761 WARN("malloc failed");
762 return -1;
764 memset(fake_jump, 0, sizeof(*fake_jump));
765 INIT_LIST_HEAD(&fake_jump->alts);
766 clear_insn_state(&fake_jump->state);
768 fake_jump->sec = special_alt->new_sec;
769 fake_jump->offset = FAKE_JUMP_OFFSET;
770 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
771 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
772 fake_jump->func = orig_insn->func;
775 if (!special_alt->new_len) {
776 if (!fake_jump) {
777 WARN("%s: empty alternative at end of section",
778 special_alt->orig_sec->name);
779 return -1;
782 *new_insn = fake_jump;
783 return 0;
786 last_new_insn = NULL;
787 insn = *new_insn;
788 sec_for_each_insn_from(file, insn) {
789 if (insn->offset >= special_alt->new_off + special_alt->new_len)
790 break;
792 last_new_insn = insn;
794 insn->ignore = orig_insn->ignore_alts;
795 insn->func = orig_insn->func;
798 * Since alternative replacement code is copy/pasted by the
799 * kernel after applying relocations, generally such code can't
800 * have relative-address relocation references to outside the
801 * .altinstr_replacement section, unless the arch's
802 * alternatives code can adjust the relative offsets
803 * accordingly.
805 * The x86 alternatives code adjusts the offsets only when it
806 * encounters a branch instruction at the very beginning of the
807 * replacement group.
809 if ((insn->offset != special_alt->new_off ||
810 (insn->type != INSN_CALL && !is_static_jump(insn))) &&
811 find_rela_by_dest_range(file->elf, insn->sec, insn->offset, insn->len)) {
813 WARN_FUNC("unsupported relocation in alternatives section",
814 insn->sec, insn->offset);
815 return -1;
818 if (!is_static_jump(insn))
819 continue;
821 if (!insn->immediate)
822 continue;
824 dest_off = insn->offset + insn->len + insn->immediate;
825 if (dest_off == special_alt->new_off + special_alt->new_len) {
826 if (!fake_jump) {
827 WARN("%s: alternative jump to end of section",
828 special_alt->orig_sec->name);
829 return -1;
831 insn->jump_dest = fake_jump;
834 if (!insn->jump_dest) {
835 WARN_FUNC("can't find alternative jump destination",
836 insn->sec, insn->offset);
837 return -1;
841 if (!last_new_insn) {
842 WARN_FUNC("can't find last new alternative instruction",
843 special_alt->new_sec, special_alt->new_off);
844 return -1;
847 if (fake_jump)
848 list_add(&fake_jump->list, &last_new_insn->list);
850 return 0;
854 * A jump table entry can either convert a nop to a jump or a jump to a nop.
855 * If the original instruction is a jump, make the alt entry an effective nop
856 * by just skipping the original instruction.
858 static int handle_jump_alt(struct objtool_file *file,
859 struct special_alt *special_alt,
860 struct instruction *orig_insn,
861 struct instruction **new_insn)
863 if (orig_insn->type == INSN_NOP)
864 return 0;
866 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
867 WARN_FUNC("unsupported instruction at jump label",
868 orig_insn->sec, orig_insn->offset);
869 return -1;
872 *new_insn = list_next_entry(orig_insn, list);
873 return 0;
877 * Read all the special sections which have alternate instructions which can be
878 * patched in or redirected to at runtime. Each instruction having alternate
879 * instruction(s) has them added to its insn->alts list, which will be
880 * traversed in validate_branch().
882 static int add_special_section_alts(struct objtool_file *file)
884 struct list_head special_alts;
885 struct instruction *orig_insn, *new_insn;
886 struct special_alt *special_alt, *tmp;
887 struct alternative *alt;
888 int ret;
890 ret = special_get_alts(file->elf, &special_alts);
891 if (ret)
892 return ret;
894 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
896 orig_insn = find_insn(file, special_alt->orig_sec,
897 special_alt->orig_off);
898 if (!orig_insn) {
899 WARN_FUNC("special: can't find orig instruction",
900 special_alt->orig_sec, special_alt->orig_off);
901 ret = -1;
902 goto out;
905 new_insn = NULL;
906 if (!special_alt->group || special_alt->new_len) {
907 new_insn = find_insn(file, special_alt->new_sec,
908 special_alt->new_off);
909 if (!new_insn) {
910 WARN_FUNC("special: can't find new instruction",
911 special_alt->new_sec,
912 special_alt->new_off);
913 ret = -1;
914 goto out;
918 if (special_alt->group) {
919 if (!special_alt->orig_len) {
920 WARN_FUNC("empty alternative entry",
921 orig_insn->sec, orig_insn->offset);
922 continue;
925 ret = handle_group_alt(file, special_alt, orig_insn,
926 &new_insn);
927 if (ret)
928 goto out;
929 } else if (special_alt->jump_or_nop) {
930 ret = handle_jump_alt(file, special_alt, orig_insn,
931 &new_insn);
932 if (ret)
933 goto out;
936 alt = malloc(sizeof(*alt));
937 if (!alt) {
938 WARN("malloc failed");
939 ret = -1;
940 goto out;
943 alt->insn = new_insn;
944 alt->skip_orig = special_alt->skip_orig;
945 orig_insn->ignore_alts |= special_alt->skip_alt;
946 list_add_tail(&alt->list, &orig_insn->alts);
948 list_del(&special_alt->list);
949 free(special_alt);
952 out:
953 return ret;
956 static int add_jump_table(struct objtool_file *file, struct instruction *insn,
957 struct rela *table)
959 struct rela *rela = table;
960 struct instruction *dest_insn;
961 struct alternative *alt;
962 struct symbol *pfunc = insn->func->pfunc;
963 unsigned int prev_offset = 0;
966 * Each @rela is a switch table relocation which points to the target
967 * instruction.
969 list_for_each_entry_from(rela, &table->sec->rela_list, list) {
971 /* Check for the end of the table: */
972 if (rela != table && rela->jump_table_start)
973 break;
975 /* Make sure the table entries are consecutive: */
976 if (prev_offset && rela->offset != prev_offset + 8)
977 break;
979 /* Detect function pointers from contiguous objects: */
980 if (rela->sym->sec == pfunc->sec &&
981 rela->addend == pfunc->offset)
982 break;
984 dest_insn = find_insn(file, rela->sym->sec, rela->addend);
985 if (!dest_insn)
986 break;
988 /* Make sure the destination is in the same function: */
989 if (!dest_insn->func || dest_insn->func->pfunc != pfunc)
990 break;
992 alt = malloc(sizeof(*alt));
993 if (!alt) {
994 WARN("malloc failed");
995 return -1;
998 alt->insn = dest_insn;
999 list_add_tail(&alt->list, &insn->alts);
1000 prev_offset = rela->offset;
1003 if (!prev_offset) {
1004 WARN_FUNC("can't find switch jump table",
1005 insn->sec, insn->offset);
1006 return -1;
1009 return 0;
1013 * find_jump_table() - Given a dynamic jump, find the switch jump table in
1014 * .rodata associated with it.
1016 * There are 3 basic patterns:
1018 * 1. jmpq *[rodata addr](,%reg,8)
1020 * This is the most common case by far. It jumps to an address in a simple
1021 * jump table which is stored in .rodata.
1023 * 2. jmpq *[rodata addr](%rip)
1025 * This is caused by a rare GCC quirk, currently only seen in three driver
1026 * functions in the kernel, only with certain obscure non-distro configs.
1028 * As part of an optimization, GCC makes a copy of an existing switch jump
1029 * table, modifies it, and then hard-codes the jump (albeit with an indirect
1030 * jump) to use a single entry in the table. The rest of the jump table and
1031 * some of its jump targets remain as dead code.
1033 * In such a case we can just crudely ignore all unreachable instruction
1034 * warnings for the entire object file. Ideally we would just ignore them
1035 * for the function, but that would require redesigning the code quite a
1036 * bit. And honestly that's just not worth doing: unreachable instruction
1037 * warnings are of questionable value anyway, and this is such a rare issue.
1039 * 3. mov [rodata addr],%reg1
1040 * ... some instructions ...
1041 * jmpq *(%reg1,%reg2,8)
1043 * This is a fairly uncommon pattern which is new for GCC 6. As of this
1044 * writing, there are 11 occurrences of it in the allmodconfig kernel.
1046 * As of GCC 7 there are quite a few more of these and the 'in between' code
1047 * is significant. Esp. with KASAN enabled some of the code between the mov
1048 * and jmpq uses .rodata itself, which can confuse things.
1050 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1051 * ensure the same register is used in the mov and jump instructions.
1053 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1055 static struct rela *find_jump_table(struct objtool_file *file,
1056 struct symbol *func,
1057 struct instruction *insn)
1059 struct rela *text_rela, *table_rela;
1060 struct instruction *dest_insn, *orig_insn = insn;
1061 struct section *table_sec;
1062 unsigned long table_offset;
1065 * Backward search using the @first_jump_src links, these help avoid
1066 * much of the 'in between' code. Which avoids us getting confused by
1067 * it.
1069 for (;
1070 insn && insn->func && insn->func->pfunc == func;
1071 insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
1073 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
1074 break;
1076 /* allow small jumps within the range */
1077 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
1078 insn->jump_dest &&
1079 (insn->jump_dest->offset <= insn->offset ||
1080 insn->jump_dest->offset > orig_insn->offset))
1081 break;
1083 /* look for a relocation which references .rodata */
1084 text_rela = find_rela_by_dest_range(file->elf, insn->sec,
1085 insn->offset, insn->len);
1086 if (!text_rela || text_rela->sym->type != STT_SECTION ||
1087 !text_rela->sym->sec->rodata)
1088 continue;
1090 table_offset = text_rela->addend;
1091 table_sec = text_rela->sym->sec;
1093 if (text_rela->type == R_X86_64_PC32)
1094 table_offset += 4;
1097 * Make sure the .rodata address isn't associated with a
1098 * symbol. GCC jump tables are anonymous data.
1100 * Also support C jump tables which are in the same format as
1101 * switch jump tables. For objtool to recognize them, they
1102 * need to be placed in the C_JUMP_TABLE_SECTION section. They
1103 * have symbols associated with them.
1105 if (find_symbol_containing(table_sec, table_offset) &&
1106 strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
1107 continue;
1110 * Each table entry has a rela associated with it. The rela
1111 * should reference text in the same function as the original
1112 * instruction.
1114 table_rela = find_rela_by_dest(file->elf, table_sec, table_offset);
1115 if (!table_rela)
1116 continue;
1117 dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
1118 if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
1119 continue;
1122 * Use of RIP-relative switch jumps is quite rare, and
1123 * indicates a rare GCC quirk/bug which can leave dead code
1124 * behind.
1126 if (text_rela->type == R_X86_64_PC32)
1127 file->ignore_unreachables = true;
1129 return table_rela;
1132 return NULL;
1136 * First pass: Mark the head of each jump table so that in the next pass,
1137 * we know when a given jump table ends and the next one starts.
1139 static void mark_func_jump_tables(struct objtool_file *file,
1140 struct symbol *func)
1142 struct instruction *insn, *last = NULL;
1143 struct rela *rela;
1145 func_for_each_insn(file, func, insn) {
1146 if (!last)
1147 last = insn;
1150 * Store back-pointers for unconditional forward jumps such
1151 * that find_jump_table() can back-track using those and
1152 * avoid some potentially confusing code.
1154 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1155 insn->offset > last->offset &&
1156 insn->jump_dest->offset > insn->offset &&
1157 !insn->jump_dest->first_jump_src) {
1159 insn->jump_dest->first_jump_src = insn;
1160 last = insn->jump_dest;
1163 if (insn->type != INSN_JUMP_DYNAMIC)
1164 continue;
1166 rela = find_jump_table(file, func, insn);
1167 if (rela) {
1168 rela->jump_table_start = true;
1169 insn->jump_table = rela;
1174 static int add_func_jump_tables(struct objtool_file *file,
1175 struct symbol *func)
1177 struct instruction *insn;
1178 int ret;
1180 func_for_each_insn(file, func, insn) {
1181 if (!insn->jump_table)
1182 continue;
1184 ret = add_jump_table(file, insn, insn->jump_table);
1185 if (ret)
1186 return ret;
1189 return 0;
1193 * For some switch statements, gcc generates a jump table in the .rodata
1194 * section which contains a list of addresses within the function to jump to.
1195 * This finds these jump tables and adds them to the insn->alts lists.
1197 static int add_jump_table_alts(struct objtool_file *file)
1199 struct section *sec;
1200 struct symbol *func;
1201 int ret;
1203 if (!file->rodata)
1204 return 0;
1206 for_each_sec(file, sec) {
1207 list_for_each_entry(func, &sec->symbol_list, list) {
1208 if (func->type != STT_FUNC)
1209 continue;
1211 mark_func_jump_tables(file, func);
1212 ret = add_func_jump_tables(file, func);
1213 if (ret)
1214 return ret;
1218 return 0;
1221 static int read_unwind_hints(struct objtool_file *file)
1223 struct section *sec, *relasec;
1224 struct rela *rela;
1225 struct unwind_hint *hint;
1226 struct instruction *insn;
1227 struct cfi_reg *cfa;
1228 int i;
1230 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1231 if (!sec)
1232 return 0;
1234 relasec = sec->rela;
1235 if (!relasec) {
1236 WARN("missing .rela.discard.unwind_hints section");
1237 return -1;
1240 if (sec->len % sizeof(struct unwind_hint)) {
1241 WARN("struct unwind_hint size mismatch");
1242 return -1;
1245 file->hints = true;
1247 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1248 hint = (struct unwind_hint *)sec->data->d_buf + i;
1250 rela = find_rela_by_dest(file->elf, sec, i * sizeof(*hint));
1251 if (!rela) {
1252 WARN("can't find rela for unwind_hints[%d]", i);
1253 return -1;
1256 insn = find_insn(file, rela->sym->sec, rela->addend);
1257 if (!insn) {
1258 WARN("can't find insn for unwind_hints[%d]", i);
1259 return -1;
1262 cfa = &insn->state.cfa;
1264 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1265 insn->save = true;
1266 continue;
1268 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1269 insn->restore = true;
1270 insn->hint = true;
1271 continue;
1274 insn->hint = true;
1276 switch (hint->sp_reg) {
1277 case ORC_REG_UNDEFINED:
1278 cfa->base = CFI_UNDEFINED;
1279 break;
1280 case ORC_REG_SP:
1281 cfa->base = CFI_SP;
1282 break;
1283 case ORC_REG_BP:
1284 cfa->base = CFI_BP;
1285 break;
1286 case ORC_REG_SP_INDIRECT:
1287 cfa->base = CFI_SP_INDIRECT;
1288 break;
1289 case ORC_REG_R10:
1290 cfa->base = CFI_R10;
1291 break;
1292 case ORC_REG_R13:
1293 cfa->base = CFI_R13;
1294 break;
1295 case ORC_REG_DI:
1296 cfa->base = CFI_DI;
1297 break;
1298 case ORC_REG_DX:
1299 cfa->base = CFI_DX;
1300 break;
1301 default:
1302 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1303 insn->sec, insn->offset, hint->sp_reg);
1304 return -1;
1307 cfa->offset = hint->sp_offset;
1308 insn->state.type = hint->type;
1309 insn->state.end = hint->end;
1312 return 0;
1315 static int read_retpoline_hints(struct objtool_file *file)
1317 struct section *sec;
1318 struct instruction *insn;
1319 struct rela *rela;
1321 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1322 if (!sec)
1323 return 0;
1325 list_for_each_entry(rela, &sec->rela_list, list) {
1326 if (rela->sym->type != STT_SECTION) {
1327 WARN("unexpected relocation symbol type in %s", sec->name);
1328 return -1;
1331 insn = find_insn(file, rela->sym->sec, rela->addend);
1332 if (!insn) {
1333 WARN("bad .discard.retpoline_safe entry");
1334 return -1;
1337 if (insn->type != INSN_JUMP_DYNAMIC &&
1338 insn->type != INSN_CALL_DYNAMIC) {
1339 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1340 insn->sec, insn->offset);
1341 return -1;
1344 insn->retpoline_safe = true;
1347 return 0;
1350 static void mark_rodata(struct objtool_file *file)
1352 struct section *sec;
1353 bool found = false;
1356 * Search for the following rodata sections, each of which can
1357 * potentially contain jump tables:
1359 * - .rodata: can contain GCC switch tables
1360 * - .rodata.<func>: same, if -fdata-sections is being used
1361 * - .rodata..c_jump_table: contains C annotated jump tables
1363 * .rodata.str1.* sections are ignored; they don't contain jump tables.
1365 for_each_sec(file, sec) {
1366 if ((!strncmp(sec->name, ".rodata", 7) && !strstr(sec->name, ".str1.")) ||
1367 !strcmp(sec->name, C_JUMP_TABLE_SECTION)) {
1368 sec->rodata = true;
1369 found = true;
1373 file->rodata = found;
1376 static int decode_sections(struct objtool_file *file)
1378 int ret;
1380 mark_rodata(file);
1382 ret = decode_instructions(file);
1383 if (ret)
1384 return ret;
1386 ret = add_dead_ends(file);
1387 if (ret)
1388 return ret;
1390 add_ignores(file);
1391 add_uaccess_safe(file);
1393 ret = add_ignore_alternatives(file);
1394 if (ret)
1395 return ret;
1397 ret = add_jump_destinations(file);
1398 if (ret)
1399 return ret;
1401 ret = add_special_section_alts(file);
1402 if (ret)
1403 return ret;
1405 ret = add_call_destinations(file);
1406 if (ret)
1407 return ret;
1409 ret = add_jump_table_alts(file);
1410 if (ret)
1411 return ret;
1413 ret = read_unwind_hints(file);
1414 if (ret)
1415 return ret;
1417 ret = read_retpoline_hints(file);
1418 if (ret)
1419 return ret;
1421 return 0;
1424 static bool is_fentry_call(struct instruction *insn)
1426 if (insn->type == INSN_CALL &&
1427 insn->call_dest->type == STT_NOTYPE &&
1428 !strcmp(insn->call_dest->name, "__fentry__"))
1429 return true;
1431 return false;
1434 static bool has_modified_stack_frame(struct insn_state *state)
1436 int i;
1438 if (state->cfa.base != initial_func_cfi.cfa.base ||
1439 state->cfa.offset != initial_func_cfi.cfa.offset ||
1440 state->stack_size != initial_func_cfi.cfa.offset ||
1441 state->drap)
1442 return true;
1444 for (i = 0; i < CFI_NUM_REGS; i++)
1445 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1446 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1447 return true;
1449 return false;
1452 static bool has_valid_stack_frame(struct insn_state *state)
1454 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1455 state->regs[CFI_BP].offset == -16)
1456 return true;
1458 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1459 return true;
1461 return false;
1464 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1466 struct cfi_reg *cfa = &state->cfa;
1467 struct stack_op *op = &insn->stack_op;
1469 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1470 return 0;
1472 /* push */
1473 if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
1474 cfa->offset += 8;
1476 /* pop */
1477 if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
1478 cfa->offset -= 8;
1480 /* add immediate to sp */
1481 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1482 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1483 cfa->offset -= op->src.offset;
1485 return 0;
1488 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1489 int offset)
1491 if (arch_callee_saved_reg(reg) &&
1492 state->regs[reg].base == CFI_UNDEFINED) {
1493 state->regs[reg].base = base;
1494 state->regs[reg].offset = offset;
1498 static void restore_reg(struct insn_state *state, unsigned char reg)
1500 state->regs[reg].base = CFI_UNDEFINED;
1501 state->regs[reg].offset = 0;
1505 * A note about DRAP stack alignment:
1507 * GCC has the concept of a DRAP register, which is used to help keep track of
1508 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1509 * register. The typical DRAP pattern is:
1511 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1512 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1513 * 41 ff 72 f8 pushq -0x8(%r10)
1514 * 55 push %rbp
1515 * 48 89 e5 mov %rsp,%rbp
1516 * (more pushes)
1517 * 41 52 push %r10
1518 * ...
1519 * 41 5a pop %r10
1520 * (more pops)
1521 * 5d pop %rbp
1522 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1523 * c3 retq
1525 * There are some variations in the epilogues, like:
1527 * 5b pop %rbx
1528 * 41 5a pop %r10
1529 * 41 5c pop %r12
1530 * 41 5d pop %r13
1531 * 41 5e pop %r14
1532 * c9 leaveq
1533 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1534 * c3 retq
1536 * and:
1538 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1539 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1540 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1541 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1542 * c9 leaveq
1543 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1544 * c3 retq
1546 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1547 * restored beforehand:
1549 * 41 55 push %r13
1550 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1551 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1552 * ...
1553 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1554 * 41 5d pop %r13
1555 * c3 retq
1557 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1559 struct stack_op *op = &insn->stack_op;
1560 struct cfi_reg *cfa = &state->cfa;
1561 struct cfi_reg *regs = state->regs;
1563 /* stack operations don't make sense with an undefined CFA */
1564 if (cfa->base == CFI_UNDEFINED) {
1565 if (insn->func) {
1566 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1567 return -1;
1569 return 0;
1572 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1573 return update_insn_state_regs(insn, state);
1575 switch (op->dest.type) {
1577 case OP_DEST_REG:
1578 switch (op->src.type) {
1580 case OP_SRC_REG:
1581 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1582 cfa->base == CFI_SP &&
1583 regs[CFI_BP].base == CFI_CFA &&
1584 regs[CFI_BP].offset == -cfa->offset) {
1586 /* mov %rsp, %rbp */
1587 cfa->base = op->dest.reg;
1588 state->bp_scratch = false;
1591 else if (op->src.reg == CFI_SP &&
1592 op->dest.reg == CFI_BP && state->drap) {
1594 /* drap: mov %rsp, %rbp */
1595 regs[CFI_BP].base = CFI_BP;
1596 regs[CFI_BP].offset = -state->stack_size;
1597 state->bp_scratch = false;
1600 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1603 * mov %rsp, %reg
1605 * This is needed for the rare case where GCC
1606 * does:
1608 * mov %rsp, %rax
1609 * ...
1610 * mov %rax, %rsp
1612 state->vals[op->dest.reg].base = CFI_CFA;
1613 state->vals[op->dest.reg].offset = -state->stack_size;
1616 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1617 cfa->base == CFI_BP) {
1620 * mov %rbp, %rsp
1622 * Restore the original stack pointer (Clang).
1624 state->stack_size = -state->regs[CFI_BP].offset;
1627 else if (op->dest.reg == cfa->base) {
1629 /* mov %reg, %rsp */
1630 if (cfa->base == CFI_SP &&
1631 state->vals[op->src.reg].base == CFI_CFA) {
1634 * This is needed for the rare case
1635 * where GCC does something dumb like:
1637 * lea 0x8(%rsp), %rcx
1638 * ...
1639 * mov %rcx, %rsp
1641 cfa->offset = -state->vals[op->src.reg].offset;
1642 state->stack_size = cfa->offset;
1644 } else {
1645 cfa->base = CFI_UNDEFINED;
1646 cfa->offset = 0;
1650 break;
1652 case OP_SRC_ADD:
1653 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1655 /* add imm, %rsp */
1656 state->stack_size -= op->src.offset;
1657 if (cfa->base == CFI_SP)
1658 cfa->offset -= op->src.offset;
1659 break;
1662 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1664 /* lea disp(%rbp), %rsp */
1665 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1666 break;
1669 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1671 /* drap: lea disp(%rsp), %drap */
1672 state->drap_reg = op->dest.reg;
1675 * lea disp(%rsp), %reg
1677 * This is needed for the rare case where GCC
1678 * does something dumb like:
1680 * lea 0x8(%rsp), %rcx
1681 * ...
1682 * mov %rcx, %rsp
1684 state->vals[op->dest.reg].base = CFI_CFA;
1685 state->vals[op->dest.reg].offset = \
1686 -state->stack_size + op->src.offset;
1688 break;
1691 if (state->drap && op->dest.reg == CFI_SP &&
1692 op->src.reg == state->drap_reg) {
1694 /* drap: lea disp(%drap), %rsp */
1695 cfa->base = CFI_SP;
1696 cfa->offset = state->stack_size = -op->src.offset;
1697 state->drap_reg = CFI_UNDEFINED;
1698 state->drap = false;
1699 break;
1702 if (op->dest.reg == state->cfa.base) {
1703 WARN_FUNC("unsupported stack register modification",
1704 insn->sec, insn->offset);
1705 return -1;
1708 break;
1710 case OP_SRC_AND:
1711 if (op->dest.reg != CFI_SP ||
1712 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1713 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1714 WARN_FUNC("unsupported stack pointer realignment",
1715 insn->sec, insn->offset);
1716 return -1;
1719 if (state->drap_reg != CFI_UNDEFINED) {
1720 /* drap: and imm, %rsp */
1721 cfa->base = state->drap_reg;
1722 cfa->offset = state->stack_size = 0;
1723 state->drap = true;
1727 * Older versions of GCC (4.8ish) realign the stack
1728 * without DRAP, with a frame pointer.
1731 break;
1733 case OP_SRC_POP:
1734 case OP_SRC_POPF:
1735 if (!state->drap && op->dest.type == OP_DEST_REG &&
1736 op->dest.reg == cfa->base) {
1738 /* pop %rbp */
1739 cfa->base = CFI_SP;
1742 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1743 op->dest.type == OP_DEST_REG &&
1744 op->dest.reg == state->drap_reg &&
1745 state->drap_offset == -state->stack_size) {
1747 /* drap: pop %drap */
1748 cfa->base = state->drap_reg;
1749 cfa->offset = 0;
1750 state->drap_offset = -1;
1752 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1754 /* pop %reg */
1755 restore_reg(state, op->dest.reg);
1758 state->stack_size -= 8;
1759 if (cfa->base == CFI_SP)
1760 cfa->offset -= 8;
1762 break;
1764 case OP_SRC_REG_INDIRECT:
1765 if (state->drap && op->src.reg == CFI_BP &&
1766 op->src.offset == state->drap_offset) {
1768 /* drap: mov disp(%rbp), %drap */
1769 cfa->base = state->drap_reg;
1770 cfa->offset = 0;
1771 state->drap_offset = -1;
1774 if (state->drap && op->src.reg == CFI_BP &&
1775 op->src.offset == regs[op->dest.reg].offset) {
1777 /* drap: mov disp(%rbp), %reg */
1778 restore_reg(state, op->dest.reg);
1780 } else if (op->src.reg == cfa->base &&
1781 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1783 /* mov disp(%rbp), %reg */
1784 /* mov disp(%rsp), %reg */
1785 restore_reg(state, op->dest.reg);
1788 break;
1790 default:
1791 WARN_FUNC("unknown stack-related instruction",
1792 insn->sec, insn->offset);
1793 return -1;
1796 break;
1798 case OP_DEST_PUSH:
1799 case OP_DEST_PUSHF:
1800 state->stack_size += 8;
1801 if (cfa->base == CFI_SP)
1802 cfa->offset += 8;
1804 if (op->src.type != OP_SRC_REG)
1805 break;
1807 if (state->drap) {
1808 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1810 /* drap: push %drap */
1811 cfa->base = CFI_BP_INDIRECT;
1812 cfa->offset = -state->stack_size;
1814 /* save drap so we know when to restore it */
1815 state->drap_offset = -state->stack_size;
1817 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1819 /* drap: push %rbp */
1820 state->stack_size = 0;
1822 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1824 /* drap: push %reg */
1825 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1828 } else {
1830 /* push %reg */
1831 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1834 /* detect when asm code uses rbp as a scratch register */
1835 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1836 cfa->base != CFI_BP)
1837 state->bp_scratch = true;
1838 break;
1840 case OP_DEST_REG_INDIRECT:
1842 if (state->drap) {
1843 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1845 /* drap: mov %drap, disp(%rbp) */
1846 cfa->base = CFI_BP_INDIRECT;
1847 cfa->offset = op->dest.offset;
1849 /* save drap offset so we know when to restore it */
1850 state->drap_offset = op->dest.offset;
1853 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1855 /* drap: mov reg, disp(%rbp) */
1856 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1859 } else if (op->dest.reg == cfa->base) {
1861 /* mov reg, disp(%rbp) */
1862 /* mov reg, disp(%rsp) */
1863 save_reg(state, op->src.reg, CFI_CFA,
1864 op->dest.offset - state->cfa.offset);
1867 break;
1869 case OP_DEST_LEAVE:
1870 if ((!state->drap && cfa->base != CFI_BP) ||
1871 (state->drap && cfa->base != state->drap_reg)) {
1872 WARN_FUNC("leave instruction with modified stack frame",
1873 insn->sec, insn->offset);
1874 return -1;
1877 /* leave (mov %rbp, %rsp; pop %rbp) */
1879 state->stack_size = -state->regs[CFI_BP].offset - 8;
1880 restore_reg(state, CFI_BP);
1882 if (!state->drap) {
1883 cfa->base = CFI_SP;
1884 cfa->offset -= 8;
1887 break;
1889 case OP_DEST_MEM:
1890 if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
1891 WARN_FUNC("unknown stack-related memory operation",
1892 insn->sec, insn->offset);
1893 return -1;
1896 /* pop mem */
1897 state->stack_size -= 8;
1898 if (cfa->base == CFI_SP)
1899 cfa->offset -= 8;
1901 break;
1903 default:
1904 WARN_FUNC("unknown stack-related instruction",
1905 insn->sec, insn->offset);
1906 return -1;
1909 return 0;
1912 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1914 struct insn_state *state1 = &insn->state, *state2 = state;
1915 int i;
1917 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1918 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1919 insn->sec, insn->offset,
1920 state1->cfa.base, state1->cfa.offset,
1921 state2->cfa.base, state2->cfa.offset);
1923 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1924 for (i = 0; i < CFI_NUM_REGS; i++) {
1925 if (!memcmp(&state1->regs[i], &state2->regs[i],
1926 sizeof(struct cfi_reg)))
1927 continue;
1929 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1930 insn->sec, insn->offset,
1931 i, state1->regs[i].base, state1->regs[i].offset,
1932 i, state2->regs[i].base, state2->regs[i].offset);
1933 break;
1936 } else if (state1->type != state2->type) {
1937 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1938 insn->sec, insn->offset, state1->type, state2->type);
1940 } else if (state1->drap != state2->drap ||
1941 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1942 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1943 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1944 insn->sec, insn->offset,
1945 state1->drap, state1->drap_reg, state1->drap_offset,
1946 state2->drap, state2->drap_reg, state2->drap_offset);
1948 } else
1949 return true;
1951 return false;
1954 static inline bool func_uaccess_safe(struct symbol *func)
1956 if (func)
1957 return func->uaccess_safe;
1959 return false;
1962 static inline const char *call_dest_name(struct instruction *insn)
1964 if (insn->call_dest)
1965 return insn->call_dest->name;
1967 return "{dynamic}";
1970 static int validate_call(struct instruction *insn, struct insn_state *state)
1972 if (state->uaccess && !func_uaccess_safe(insn->call_dest)) {
1973 WARN_FUNC("call to %s() with UACCESS enabled",
1974 insn->sec, insn->offset, call_dest_name(insn));
1975 return 1;
1978 if (state->df) {
1979 WARN_FUNC("call to %s() with DF set",
1980 insn->sec, insn->offset, call_dest_name(insn));
1981 return 1;
1984 return 0;
1987 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
1989 if (has_modified_stack_frame(state)) {
1990 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1991 insn->sec, insn->offset);
1992 return 1;
1995 return validate_call(insn, state);
1998 static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
2000 if (state->uaccess && !func_uaccess_safe(func)) {
2001 WARN_FUNC("return with UACCESS enabled",
2002 insn->sec, insn->offset);
2003 return 1;
2006 if (!state->uaccess && func_uaccess_safe(func)) {
2007 WARN_FUNC("return with UACCESS disabled from a UACCESS-safe function",
2008 insn->sec, insn->offset);
2009 return 1;
2012 if (state->df) {
2013 WARN_FUNC("return with DF set",
2014 insn->sec, insn->offset);
2015 return 1;
2018 if (func && has_modified_stack_frame(state)) {
2019 WARN_FUNC("return with modified stack frame",
2020 insn->sec, insn->offset);
2021 return 1;
2024 if (state->bp_scratch) {
2025 WARN_FUNC("BP used as a scratch register",
2026 insn->sec, insn->offset);
2027 return 1;
2030 return 0;
2034 * Follow the branch starting at the given instruction, and recursively follow
2035 * any other branches (jumps). Meanwhile, track the frame pointer state at
2036 * each instruction and validate all the rules described in
2037 * tools/objtool/Documentation/stack-validation.txt.
2039 static int validate_branch(struct objtool_file *file, struct symbol *func,
2040 struct instruction *first, struct insn_state state)
2042 struct alternative *alt;
2043 struct instruction *insn, *next_insn;
2044 struct section *sec;
2045 u8 visited;
2046 int ret;
2048 insn = first;
2049 sec = insn->sec;
2051 if (insn->alt_group && list_empty(&insn->alts)) {
2052 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
2053 sec, insn->offset);
2054 return 1;
2057 while (1) {
2058 next_insn = next_insn_same_sec(file, insn);
2060 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
2061 WARN("%s() falls through to next function %s()",
2062 func->name, insn->func->name);
2063 return 1;
2066 if (func && insn->ignore) {
2067 WARN_FUNC("BUG: why am I validating an ignored function?",
2068 sec, insn->offset);
2069 return 1;
2072 visited = 1 << state.uaccess;
2073 if (insn->visited) {
2074 if (!insn->hint && !insn_state_match(insn, &state))
2075 return 1;
2077 if (insn->visited & visited)
2078 return 0;
2081 if (insn->hint) {
2082 if (insn->restore) {
2083 struct instruction *save_insn, *i;
2085 i = insn;
2086 save_insn = NULL;
2087 sym_for_each_insn_continue_reverse(file, func, i) {
2088 if (i->save) {
2089 save_insn = i;
2090 break;
2094 if (!save_insn) {
2095 WARN_FUNC("no corresponding CFI save for CFI restore",
2096 sec, insn->offset);
2097 return 1;
2100 if (!save_insn->visited) {
2102 * Oops, no state to copy yet.
2103 * Hopefully we can reach this
2104 * instruction from another branch
2105 * after the save insn has been
2106 * visited.
2108 if (insn == first)
2109 return 0;
2111 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
2112 sec, insn->offset);
2113 return 1;
2116 insn->state = save_insn->state;
2119 state = insn->state;
2121 } else
2122 insn->state = state;
2124 insn->visited |= visited;
2126 if (!insn->ignore_alts) {
2127 bool skip_orig = false;
2129 list_for_each_entry(alt, &insn->alts, list) {
2130 if (alt->skip_orig)
2131 skip_orig = true;
2133 ret = validate_branch(file, func, alt->insn, state);
2134 if (ret) {
2135 if (backtrace)
2136 BT_FUNC("(alt)", insn);
2137 return ret;
2141 if (skip_orig)
2142 return 0;
2145 switch (insn->type) {
2147 case INSN_RETURN:
2148 return validate_return(func, insn, &state);
2150 case INSN_CALL:
2151 case INSN_CALL_DYNAMIC:
2152 ret = validate_call(insn, &state);
2153 if (ret)
2154 return ret;
2156 if (!no_fp && func && !is_fentry_call(insn) &&
2157 !has_valid_stack_frame(&state)) {
2158 WARN_FUNC("call without frame pointer save/setup",
2159 sec, insn->offset);
2160 return 1;
2163 if (dead_end_function(file, insn->call_dest))
2164 return 0;
2166 break;
2168 case INSN_JUMP_CONDITIONAL:
2169 case INSN_JUMP_UNCONDITIONAL:
2170 if (func && is_sibling_call(insn)) {
2171 ret = validate_sibling_call(insn, &state);
2172 if (ret)
2173 return ret;
2175 } else if (insn->jump_dest) {
2176 ret = validate_branch(file, func,
2177 insn->jump_dest, state);
2178 if (ret) {
2179 if (backtrace)
2180 BT_FUNC("(branch)", insn);
2181 return ret;
2185 if (insn->type == INSN_JUMP_UNCONDITIONAL)
2186 return 0;
2188 break;
2190 case INSN_JUMP_DYNAMIC:
2191 case INSN_JUMP_DYNAMIC_CONDITIONAL:
2192 if (func && is_sibling_call(insn)) {
2193 ret = validate_sibling_call(insn, &state);
2194 if (ret)
2195 return ret;
2198 if (insn->type == INSN_JUMP_DYNAMIC)
2199 return 0;
2201 break;
2203 case INSN_CONTEXT_SWITCH:
2204 if (func && (!next_insn || !next_insn->hint)) {
2205 WARN_FUNC("unsupported instruction in callable function",
2206 sec, insn->offset);
2207 return 1;
2209 return 0;
2211 case INSN_STACK:
2212 if (update_insn_state(insn, &state))
2213 return 1;
2215 if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2216 if (!state.uaccess_stack) {
2217 state.uaccess_stack = 1;
2218 } else if (state.uaccess_stack >> 31) {
2219 WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2220 return 1;
2222 state.uaccess_stack <<= 1;
2223 state.uaccess_stack |= state.uaccess;
2226 if (insn->stack_op.src.type == OP_SRC_POPF) {
2227 if (state.uaccess_stack) {
2228 state.uaccess = state.uaccess_stack & 1;
2229 state.uaccess_stack >>= 1;
2230 if (state.uaccess_stack == 1)
2231 state.uaccess_stack = 0;
2235 break;
2237 case INSN_STAC:
2238 if (state.uaccess) {
2239 WARN_FUNC("recursive UACCESS enable", sec, insn->offset);
2240 return 1;
2243 state.uaccess = true;
2244 break;
2246 case INSN_CLAC:
2247 if (!state.uaccess && func) {
2248 WARN_FUNC("redundant UACCESS disable", sec, insn->offset);
2249 return 1;
2252 if (func_uaccess_safe(func) && !state.uaccess_stack) {
2253 WARN_FUNC("UACCESS-safe disables UACCESS", sec, insn->offset);
2254 return 1;
2257 state.uaccess = false;
2258 break;
2260 case INSN_STD:
2261 if (state.df)
2262 WARN_FUNC("recursive STD", sec, insn->offset);
2264 state.df = true;
2265 break;
2267 case INSN_CLD:
2268 if (!state.df && func)
2269 WARN_FUNC("redundant CLD", sec, insn->offset);
2271 state.df = false;
2272 break;
2274 default:
2275 break;
2278 if (insn->dead_end)
2279 return 0;
2281 if (!next_insn) {
2282 if (state.cfa.base == CFI_UNDEFINED)
2283 return 0;
2284 WARN("%s: unexpected end of section", sec->name);
2285 return 1;
2288 insn = next_insn;
2291 return 0;
2294 static int validate_unwind_hints(struct objtool_file *file)
2296 struct instruction *insn;
2297 int ret, warnings = 0;
2298 struct insn_state state;
2300 if (!file->hints)
2301 return 0;
2303 clear_insn_state(&state);
2305 for_each_insn(file, insn) {
2306 if (insn->hint && !insn->visited) {
2307 ret = validate_branch(file, insn->func, insn, state);
2308 if (ret && backtrace)
2309 BT_FUNC("<=== (hint)", insn);
2310 warnings += ret;
2314 return warnings;
2317 static int validate_retpoline(struct objtool_file *file)
2319 struct instruction *insn;
2320 int warnings = 0;
2322 for_each_insn(file, insn) {
2323 if (insn->type != INSN_JUMP_DYNAMIC &&
2324 insn->type != INSN_CALL_DYNAMIC)
2325 continue;
2327 if (insn->retpoline_safe)
2328 continue;
2331 * .init.text code is ran before userspace and thus doesn't
2332 * strictly need retpolines, except for modules which are
2333 * loaded late, they very much do need retpoline in their
2334 * .init.text
2336 if (!strcmp(insn->sec->name, ".init.text") && !module)
2337 continue;
2339 WARN_FUNC("indirect %s found in RETPOLINE build",
2340 insn->sec, insn->offset,
2341 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2343 warnings++;
2346 return warnings;
2349 static bool is_kasan_insn(struct instruction *insn)
2351 return (insn->type == INSN_CALL &&
2352 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2355 static bool is_ubsan_insn(struct instruction *insn)
2357 return (insn->type == INSN_CALL &&
2358 !strcmp(insn->call_dest->name,
2359 "__ubsan_handle_builtin_unreachable"));
2362 static bool ignore_unreachable_insn(struct instruction *insn)
2364 int i;
2366 if (insn->ignore || insn->type == INSN_NOP)
2367 return true;
2370 * Ignore any unused exceptions. This can happen when a whitelisted
2371 * function has an exception table entry.
2373 * Also ignore alternative replacement instructions. This can happen
2374 * when a whitelisted function uses one of the ALTERNATIVE macros.
2376 if (!strcmp(insn->sec->name, ".fixup") ||
2377 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2378 !strcmp(insn->sec->name, ".altinstr_aux"))
2379 return true;
2381 if (!insn->func)
2382 return false;
2385 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2386 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2387 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2388 * (or occasionally a JMP to UD2).
2390 if (list_prev_entry(insn, list)->dead_end &&
2391 (insn->type == INSN_BUG ||
2392 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2393 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2394 return true;
2397 * Check if this (or a subsequent) instruction is related to
2398 * CONFIG_UBSAN or CONFIG_KASAN.
2400 * End the search at 5 instructions to avoid going into the weeds.
2402 for (i = 0; i < 5; i++) {
2404 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2405 return true;
2407 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2408 if (insn->jump_dest &&
2409 insn->jump_dest->func == insn->func) {
2410 insn = insn->jump_dest;
2411 continue;
2414 break;
2417 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2418 break;
2420 insn = list_next_entry(insn, list);
2423 return false;
2426 static int validate_section(struct objtool_file *file, struct section *sec)
2428 struct symbol *func;
2429 struct instruction *insn;
2430 struct insn_state state;
2431 int ret, warnings = 0;
2433 clear_insn_state(&state);
2435 state.cfa = initial_func_cfi.cfa;
2436 memcpy(&state.regs, &initial_func_cfi.regs,
2437 CFI_NUM_REGS * sizeof(struct cfi_reg));
2438 state.stack_size = initial_func_cfi.cfa.offset;
2440 list_for_each_entry(func, &sec->symbol_list, list) {
2441 if (func->type != STT_FUNC)
2442 continue;
2444 if (!func->len) {
2445 WARN("%s() is missing an ELF size annotation",
2446 func->name);
2447 warnings++;
2450 if (func->pfunc != func || func->alias != func)
2451 continue;
2453 insn = find_insn(file, sec, func->offset);
2454 if (!insn || insn->ignore || insn->visited)
2455 continue;
2457 state.uaccess = func->uaccess_safe;
2459 ret = validate_branch(file, func, insn, state);
2460 if (ret && backtrace)
2461 BT_FUNC("<=== (func)", insn);
2462 warnings += ret;
2465 return warnings;
2468 static int validate_functions(struct objtool_file *file)
2470 struct section *sec;
2471 int warnings = 0;
2473 for_each_sec(file, sec)
2474 warnings += validate_section(file, sec);
2476 return warnings;
2479 static int validate_reachable_instructions(struct objtool_file *file)
2481 struct instruction *insn;
2483 if (file->ignore_unreachables)
2484 return 0;
2486 for_each_insn(file, insn) {
2487 if (insn->visited || ignore_unreachable_insn(insn))
2488 continue;
2490 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2491 return 1;
2494 return 0;
2497 static struct objtool_file file;
2499 int check(const char *_objname, bool orc)
2501 int ret, warnings = 0;
2503 objname = _objname;
2505 file.elf = elf_read(objname, orc ? O_RDWR : O_RDONLY);
2506 if (!file.elf)
2507 return 1;
2509 INIT_LIST_HEAD(&file.insn_list);
2510 hash_init(file.insn_hash);
2511 file.c_file = find_section_by_name(file.elf, ".comment");
2512 file.ignore_unreachables = no_unreachable;
2513 file.hints = false;
2515 arch_initial_func_cfi_state(&initial_func_cfi);
2517 ret = decode_sections(&file);
2518 if (ret < 0)
2519 goto out;
2520 warnings += ret;
2522 if (list_empty(&file.insn_list))
2523 goto out;
2525 if (retpoline) {
2526 ret = validate_retpoline(&file);
2527 if (ret < 0)
2528 return ret;
2529 warnings += ret;
2532 ret = validate_functions(&file);
2533 if (ret < 0)
2534 goto out;
2535 warnings += ret;
2537 ret = validate_unwind_hints(&file);
2538 if (ret < 0)
2539 goto out;
2540 warnings += ret;
2542 if (!warnings) {
2543 ret = validate_reachable_instructions(&file);
2544 if (ret < 0)
2545 goto out;
2546 warnings += ret;
2549 if (orc) {
2550 ret = create_orc(&file);
2551 if (ret < 0)
2552 goto out;
2554 ret = create_orc_sections(&file);
2555 if (ret < 0)
2556 goto out;
2558 ret = elf_write(file.elf);
2559 if (ret < 0)
2560 goto out;
2563 out:
2564 if (ret < 0) {
2566 * Fatal error. The binary is corrupt or otherwise broken in
2567 * some way, or objtool itself is broken. Fail the kernel
2568 * build.
2570 return ret;
2573 return 0;