1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6 #ifndef _CHECK_H
7 #define _CHECK_H
8
9 #include <stdbool.h>
10 #include <objtool/cfi.h>
11 #include <objtool/arch.h>
12
13 struct insn_state {
14 struct cfi_state cfi;
15 unsigned int uaccess_stack;
16 bool uaccess;
17 bool df;
18 bool noinstr;
19 s8 instr;
20 };
21
22 struct alt_group {
23 /*
24 * Pointer from a replacement group to the original group. NULL if it
25 * *is* the original group.
26 */
27 struct alt_group *orig_group;
28
29 /* First and last instructions in the group */
30 struct instruction *first_insn, *last_insn, *nop;
31
32 /*
33 * Byte-offset-addressed len-sized array of pointers to CFI structs.
34 * This is shared with the other alt_groups in the same alternative.
35 */
36 struct cfi_state **cfi;
37
38 bool ignore;
39 unsigned int feature;
40 };
41
42 enum alternative_type {
43 ALT_TYPE_INSTRUCTIONS,
44 ALT_TYPE_JUMP_TABLE,
45 ALT_TYPE_EX_TABLE,
46 };
47
48 struct alternative {
49 struct alternative *next;
50 struct instruction *insn;
51 enum alternative_type type;
52 };
53
54 #define INSN_CHUNK_BITS 8
55 #define INSN_CHUNK_SIZE (1 << INSN_CHUNK_BITS)
56 #define INSN_CHUNK_MAX (INSN_CHUNK_SIZE - 1)
57
58 struct instruction {
59 struct hlist_node hash;
60 struct list_head call_node;
61 struct section *sec;
62 unsigned long offset;
63 unsigned long immediate;
64
65 u8 len;
66 u8 prev_len;
67 u8 type;
68 s8 instr;
69
70 u32 idx : INSN_CHUNK_BITS,
71 dead_end : 1,
72 ignore_alts : 1,
73 hint : 1,
74 save : 1,
75 restore : 1,
76 retpoline_safe : 1,
77 noendbr : 1,
78 unret : 1,
79 visited : 4,
80 no_reloc : 1,
81 hole : 1,
82 fake : 1,
83 trace : 1;
84 /* 9 bit hole */
85
86 struct alt_group *alt_group;
87 struct instruction *jump_dest;
88 struct instruction *first_jump_src;
89 union {
90 struct symbol *_call_dest;
91 struct {
92 struct reloc *_jump_table;
93 unsigned long _jump_table_size;
94 };
95 };
96 struct alternative *alts;
97 struct symbol *sym;
98 struct stack_op *stack_ops;
99 struct cfi_state *cfi;
100 };
101
insn_func(struct instruction * insn)102 static inline struct symbol *insn_func(struct instruction *insn)
103 {
104 struct symbol *sym = insn->sym;
105
106 if (sym && sym->type != STT_FUNC)
107 sym = NULL;
108
109 return sym;
110 }
111
112 #define VISITED_BRANCH 0x01
113 #define VISITED_BRANCH_UACCESS 0x02
114 #define VISITED_BRANCH_MASK 0x03
115 #define VISITED_UNRET 0x04
116
is_static_jump(struct instruction * insn)117 static inline bool is_static_jump(struct instruction *insn)
118 {
119 return insn->type == INSN_JUMP_CONDITIONAL ||
120 insn->type == INSN_JUMP_UNCONDITIONAL;
121 }
122
is_dynamic_jump(struct instruction * insn)123 static inline bool is_dynamic_jump(struct instruction *insn)
124 {
125 return insn->type == INSN_JUMP_DYNAMIC ||
126 insn->type == INSN_JUMP_DYNAMIC_CONDITIONAL;
127 }
128
is_jump(struct instruction * insn)129 static inline bool is_jump(struct instruction *insn)
130 {
131 return is_static_jump(insn) || is_dynamic_jump(insn);
132 }
133
insn_call_dest(struct instruction * insn)134 static inline struct symbol *insn_call_dest(struct instruction *insn)
135 {
136 if (insn->type == INSN_JUMP_DYNAMIC ||
137 insn->type == INSN_CALL_DYNAMIC)
138 return NULL;
139
140 return insn->_call_dest;
141 }
142
143 struct instruction *find_insn(struct objtool_file *file,
144 struct section *sec, unsigned long offset);
145
146 struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn);
147
148 #define sec_for_each_insn(file, _sec, insn) \
149 for (insn = find_insn(file, _sec, 0); \
150 insn && insn->sec == _sec; \
151 insn = next_insn_same_sec(file, insn))
152
153 #define sym_for_each_insn(file, sym, insn) \
154 for (insn = find_insn(file, sym->sec, sym->offset); \
155 insn && insn->offset < sym->offset + sym->len; \
156 insn = next_insn_same_sec(file, insn))
157
158 const char *objtool_disas_insn(struct instruction *insn);
159
160 extern size_t sym_name_max_len;
161 extern struct disas_context *objtool_disas_ctx;
162 int pv_ops_idx_off(const char *symname);
163
164 #endif /* _CHECK_H */
165