/src/capstonenext/cs_priv.h
Line | Count | Source |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | |
4 | | #ifndef CS_PRIV_H |
5 | | #define CS_PRIV_H |
6 | | |
7 | | #ifdef CAPSTONE_DEBUG |
8 | | #include <assert.h> |
9 | | #endif |
10 | | #include <capstone/capstone.h> |
11 | | |
12 | | #include "MCInst.h" |
13 | | #include "SStream.h" |
14 | | |
15 | | typedef void (*Printer_t)(MCInst *MI, SStream *OS, void *info); |
16 | | |
17 | | // function to be called after Printer_t |
18 | | // this is the best time to gather insn's characteristics |
19 | | typedef void (*PostPrinter_t)(csh handle, cs_insn *, SStream *mnem, |
20 | | MCInst *mci); |
21 | | |
22 | | typedef bool (*Disasm_t)(csh handle, const uint8_t *code, size_t code_len, |
23 | | MCInst *instr, uint16_t *size, uint64_t address, |
24 | | void *info); |
25 | | |
26 | | typedef const char *(*GetName_t)(csh handle, unsigned int id); |
27 | | |
28 | | typedef void (*GetID_t)(cs_struct *h, cs_insn *insn, unsigned int id); |
29 | | |
30 | | // return registers accessed by instruction |
31 | | typedef void (*GetRegisterAccess_t)(const cs_insn *insn, cs_regs regs_read, |
32 | | uint8_t *regs_read_count, |
33 | | cs_regs regs_write, |
34 | | uint8_t *regs_write_count); |
35 | | |
36 | | // for ARM only |
37 | | typedef struct ARM_ITBlock { |
38 | | unsigned char ITStates[8]; |
39 | | unsigned int size; |
40 | | } ARM_ITBlock; |
41 | | |
42 | | typedef struct ARM_VPTBlock { |
43 | | unsigned char VPTStates[8]; |
44 | | unsigned int size; |
45 | | } ARM_VPTBlock; |
46 | | |
47 | | // Customize mnemonic for instructions with alternative name. |
48 | | struct customized_mnem { |
49 | | // ID of instruction to be customized. |
50 | | unsigned int id; |
51 | | // Customized instruction mnemonic. |
52 | | char mnemonic[CS_MNEMONIC_SIZE]; |
53 | | }; |
54 | | |
55 | | struct insn_mnem { |
56 | | struct customized_mnem insn; |
57 | | struct insn_mnem *next; // linked list of customized mnemonics |
58 | | }; |
59 | | |
60 | | // map instruction to its characteristics |
61 | | typedef struct insn_map { |
62 | | unsigned short id; // The LLVM instruction id |
63 | | unsigned short mapid; // The Capstone instruction id |
64 | | #ifndef CAPSTONE_DIET |
65 | | uint16_t regs_use[MAX_IMPL_R_REGS]; ///< list of implicit registers used by |
66 | | ///< this instruction |
67 | | uint16_t regs_mod[MAX_IMPL_W_REGS]; ///< list of implicit registers modified |
68 | | ///< by this instruction |
69 | | unsigned char groups |
70 | | [MAX_NUM_GROUPS]; ///< list of group this instruction belong to |
71 | | bool branch; // branch instruction? |
72 | | bool indirect_branch; // indirect branch instruction? |
73 | | union { |
74 | | ppc_suppl_info ppc; |
75 | | loongarch_suppl_info loongarch; |
76 | | aarch64_suppl_info aarch64; |
77 | | systemz_suppl_info systemz; |
78 | | arm_suppl_info arm; |
79 | | xtensa_suppl_info xtensa; |
80 | | sparc_suppl_info sparc; |
81 | | } suppl_info; // Supplementary information for each instruction. |
82 | | #endif |
83 | | } insn_map; |
84 | | |
85 | | struct cs_struct { |
86 | | cs_arch arch; |
87 | | cs_mode mode; |
88 | | Printer_t printer; // asm printer |
89 | | void *printer_info; // aux info for printer |
90 | | Disasm_t disasm; // disassembler |
91 | | void *getinsn_info; // auxiliary info for printer |
92 | | GetName_t reg_name; |
93 | | GetName_t insn_name; |
94 | | GetName_t group_name; |
95 | | GetID_t insn_id; |
96 | | PostPrinter_t post_printer; |
97 | | cs_err errnum; |
98 | | ARM_ITBlock ITBlock; // for Arm only |
99 | | ARM_VPTBlock VPTBlock; // for ARM only |
100 | | bool PrintBranchImmAsAddress; |
101 | | bool ShowVSRNumsAsVR; |
102 | | cs_opt_value detail_opt, imm_unsigned; |
103 | | int syntax; // asm syntax for simple printer such as ARM, Mips & PPC |
104 | | bool doing_mem; // handling memory operand in InstPrinter code |
105 | | bool doing_SME_Index; // handling a SME instruction that has index |
106 | | unsigned short *insn_cache; // index caching for mapping.c |
107 | | uint16_t *x86_insn_lut; // x86 instruction id -> insns[] index |
108 | | uint32_t *x86_insn_reg_lut; // x86 packed Intel/ATT implicit register entries |
109 | | unsigned int x86_insn_lut_max; |
110 | | // A mapping of LLVM instruction IDs to capstone instruction IDs, with |
111 | | // some supplementary information, sorted in ascending order by LLVM |
112 | | // instruction ID. |
113 | | const insn_map *insn_map; |
114 | | // The number of elements in the array pointed to by .insn_map |
115 | | unsigned short insn_map_size; |
116 | | bool skipdata; // set this to True if we skip data when disassembling |
117 | | uint8_t skipdata_size; // how many bytes to skip |
118 | | cs_opt_skipdata skipdata_setup; // user-defined skipdata setup |
119 | | const uint8_t *regsize_map; // map to register size (x86-only for now) |
120 | | GetRegisterAccess_t reg_access; |
121 | | struct insn_mnem |
122 | | *mnem_list; // linked list of customized instruction mnemonic |
123 | | uint32_t LITBASE; ///< The LITBASE register content. Bit 0 (LSB) indicatess if it is set. Bit[23:8] are the literal base address. |
124 | | }; |
125 | | |
126 | | #define MAX_ARCH CS_ARCH_MAX |
127 | | |
128 | | // Returns a bool (0 or 1) whether big endian is enabled for a mode |
129 | 1.57M | #define MODE_IS_BIG_ENDIAN(mode) (((mode) & CS_MODE_BIG_ENDIAN) != 0) |
130 | | |
131 | | /// Returns true of the 16bit flag is set. |
132 | | #define IS_16BIT(mode) ((mode & CS_MODE_16) != 0) |
133 | | /// Returns true of the 32bit flag is set. |
134 | 3.24k | #define IS_32BIT(mode) ((mode & CS_MODE_32) != 0) |
135 | | /// Returns true of the 64bit flag is set. |
136 | 3.24k | #define IS_64BIT(mode) ((mode & CS_MODE_64) != 0) |
137 | | |
138 | | extern cs_malloc_t cs_mem_malloc; |
139 | | extern cs_calloc_t cs_mem_calloc; |
140 | | extern cs_realloc_t cs_mem_realloc; |
141 | | extern cs_free_t cs_mem_free; |
142 | | extern cs_vsnprintf_t cs_vsnprintf; |
143 | | |
144 | | static inline bool cs_mem_is_setup() |
145 | 35.2k | { |
146 | 35.2k | return cs_mem_malloc && cs_mem_calloc && cs_mem_realloc && |
147 | 35.2k | cs_mem_free && cs_vsnprintf; |
148 | 35.2k | } Line | Count | Source | 145 | 35.2k | { | 146 | 35.2k | return cs_mem_malloc && cs_mem_calloc && cs_mem_realloc && | 147 | 35.2k | cs_mem_free && cs_vsnprintf; | 148 | 35.2k | } |
Unexecuted instantiation: Mapping.c:cs_mem_is_setup Unexecuted instantiation: MCInst.c:cs_mem_is_setup Unexecuted instantiation: SStream.c:cs_mem_is_setup Unexecuted instantiation: utils.c:cs_mem_is_setup Unexecuted instantiation: ARMModule.c:cs_mem_is_setup Unexecuted instantiation: AArch64Module.c:cs_mem_is_setup Unexecuted instantiation: MipsModule.c:cs_mem_is_setup Unexecuted instantiation: PPCModule.c:cs_mem_is_setup Unexecuted instantiation: X86Module.c:cs_mem_is_setup Unexecuted instantiation: X86ATTInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: SparcModule.c:cs_mem_is_setup Unexecuted instantiation: SystemZModule.c:cs_mem_is_setup Unexecuted instantiation: XCoreModule.c:cs_mem_is_setup Unexecuted instantiation: M68KModule.c:cs_mem_is_setup Unexecuted instantiation: TMS320C64xModule.c:cs_mem_is_setup Unexecuted instantiation: M680XModule.c:cs_mem_is_setup Unexecuted instantiation: EVMModule.c:cs_mem_is_setup Unexecuted instantiation: WASMModule.c:cs_mem_is_setup Unexecuted instantiation: MOS65XXModule.c:cs_mem_is_setup Unexecuted instantiation: MOS65XXDisassembler.c:cs_mem_is_setup Unexecuted instantiation: BPFModule.c:cs_mem_is_setup Unexecuted instantiation: RISCVModule.c:cs_mem_is_setup Unexecuted instantiation: SHModule.c:cs_mem_is_setup Unexecuted instantiation: TriCoreModule.c:cs_mem_is_setup Unexecuted instantiation: AlphaModule.c:cs_mem_is_setup Unexecuted instantiation: HPPAModule.c:cs_mem_is_setup Unexecuted instantiation: LoongArchModule.c:cs_mem_is_setup Unexecuted instantiation: XtensaModule.c:cs_mem_is_setup Unexecuted instantiation: ARCModule.c:cs_mem_is_setup Unexecuted instantiation: ARMMapping.c:cs_mem_is_setup Unexecuted instantiation: AArch64Mapping.c:cs_mem_is_setup Unexecuted instantiation: MipsMapping.c:cs_mem_is_setup Unexecuted instantiation: PPCMapping.c:cs_mem_is_setup Unexecuted instantiation: X86Disassembler.c:cs_mem_is_setup Unexecuted instantiation: X86DisassemblerDecoder.c:cs_mem_is_setup Unexecuted instantiation: X86IntelInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: X86InstPrinterCommon.c:cs_mem_is_setup Unexecuted instantiation: X86Mapping.c:cs_mem_is_setup Unexecuted instantiation: SparcMapping.c:cs_mem_is_setup Unexecuted instantiation: SystemZMapping.c:cs_mem_is_setup Unexecuted instantiation: XCoreDisassembler.c:cs_mem_is_setup Unexecuted instantiation: XCoreInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: XCoreMapping.c:cs_mem_is_setup Unexecuted instantiation: M68KDisassembler.c:cs_mem_is_setup Unexecuted instantiation: M68KInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: TMS320C64xDisassembler.c:cs_mem_is_setup Unexecuted instantiation: TMS320C64xInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: TMS320C64xMapping.c:cs_mem_is_setup Unexecuted instantiation: M680XDisassembler.c:cs_mem_is_setup Unexecuted instantiation: M680XInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: EVMInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: EVMMapping.c:cs_mem_is_setup Unexecuted instantiation: WASMDisassembler.c:cs_mem_is_setup Unexecuted instantiation: WASMInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: WASMMapping.c:cs_mem_is_setup Unexecuted instantiation: BPFDisassembler.c:cs_mem_is_setup Unexecuted instantiation: BPFInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: BPFMapping.c:cs_mem_is_setup Unexecuted instantiation: RISCVDisassembler.c:cs_mem_is_setup Unexecuted instantiation: RISCVDisassemblerExtension.c:cs_mem_is_setup Unexecuted instantiation: RISCVInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: RISCVMapping.c:cs_mem_is_setup Unexecuted instantiation: SHDisassembler.c:cs_mem_is_setup Unexecuted instantiation: SHInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: TriCoreDisassembler.c:cs_mem_is_setup Unexecuted instantiation: TriCoreMapping.c:cs_mem_is_setup Unexecuted instantiation: AlphaDisassembler.c:cs_mem_is_setup Unexecuted instantiation: AlphaMapping.c:cs_mem_is_setup Unexecuted instantiation: HPPADisassembler.c:cs_mem_is_setup Unexecuted instantiation: HPPAInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: HPPAMapping.c:cs_mem_is_setup Unexecuted instantiation: LoongArchMapping.c:cs_mem_is_setup Unexecuted instantiation: XtensaMapping.c:cs_mem_is_setup Unexecuted instantiation: ARCMapping.c:cs_mem_is_setup Unexecuted instantiation: MCInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: ARMBaseInfo.c:cs_mem_is_setup Unexecuted instantiation: ARMDisassembler.c:cs_mem_is_setup Unexecuted instantiation: ARMDisassemblerExtension.c:cs_mem_is_setup Unexecuted instantiation: ARMInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: AArch64BaseInfo.c:cs_mem_is_setup Unexecuted instantiation: AArch64Disassembler.c:cs_mem_is_setup Unexecuted instantiation: AArch64DisassemblerExtension.c:cs_mem_is_setup Unexecuted instantiation: AArch64InstPrinter.c:cs_mem_is_setup Unexecuted instantiation: MipsDisassembler.c:cs_mem_is_setup Unexecuted instantiation: MipsInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: PPCDisassembler.c:cs_mem_is_setup Unexecuted instantiation: PPCInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: SparcDisassembler.c:cs_mem_is_setup Unexecuted instantiation: SparcInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: SystemZDisassembler.c:cs_mem_is_setup Unexecuted instantiation: SystemZDisassemblerExtension.c:cs_mem_is_setup Unexecuted instantiation: SystemZInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: SystemZMCTargetDesc.c:cs_mem_is_setup Unexecuted instantiation: RISCVBaseInfo.c:cs_mem_is_setup Unexecuted instantiation: TriCoreInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: AlphaInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: LoongArchDisassembler.c:cs_mem_is_setup Unexecuted instantiation: LoongArchInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: XtensaDisassembler.c:cs_mem_is_setup Unexecuted instantiation: XtensaInstPrinter.c:cs_mem_is_setup Unexecuted instantiation: ARCDisassembler.c:cs_mem_is_setup Unexecuted instantiation: ARCInstPrinter.c:cs_mem_is_setup |
149 | | |
150 | | /// Capstone assert macros. They can be configured to print warnings |
151 | | /// when the `expr` is false. |
152 | | /// This can be enabled by defining CAPSTONE_ASSERTION_WARNINGS. |
153 | | /// Debug builds will always include an `assert(expr)` and hard fail |
154 | | /// if `!expr`. |
155 | | /// Release builds will not have `assert(expr)` code. |
156 | | |
157 | | /// An simple assert. |
158 | | #if defined(CAPSTONE_DEBUG) && !defined(CAPSTONE_ASSERTION_WARNINGS) |
159 | | #define CS_ASSERT(expr) assert(expr) |
160 | | #elif defined(CAPSTONE_DEBUG) && defined(CAPSTONE_ASSERTION_WARNINGS) |
161 | | #define CS_ASSERT(expr) \ |
162 | | do { \ |
163 | | if (!(expr)) { \ |
164 | | fprintf(stderr, \ |
165 | | "Capstone hit the assert: \"" #expr \ |
166 | | "\": %s:%" PRIu32 "\n", \ |
167 | | __FILE__, __LINE__); \ |
168 | | assert(expr) \ |
169 | | } \ |
170 | | } while (0) |
171 | | #elif defined(CAPSTONE_ASSERTION_WARNINGS) |
172 | | #define CS_ASSERT(expr) \ |
173 | | do { \ |
174 | | if (!(expr)) { \ |
175 | | fprintf(stderr, \ |
176 | | "Capstone hit the assert: \"" #expr \ |
177 | | "\": %s:%" PRIu32 "\n", \ |
178 | | __FILE__, __LINE__); \ |
179 | | } \ |
180 | | } while (0) |
181 | | #else |
182 | | #define CS_ASSERT(expr) |
183 | | #endif |
184 | | |
185 | | /// An assert which returns the value in release builds if `!expr`. |
186 | | #if defined(CAPSTONE_DEBUG) && !defined(CAPSTONE_ASSERTION_WARNINGS) |
187 | | #define CS_ASSERT_RET_VAL(expr, val) assert(expr) |
188 | | #elif defined(CAPSTONE_ASSERTION_WARNINGS) |
189 | | #define CS_ASSERT_RET_VAL(expr, val) \ |
190 | | do { \ |
191 | | if (!(expr)) { \ |
192 | | CS_ASSERT(expr); \ |
193 | | return val; \ |
194 | | } \ |
195 | | } while (0) |
196 | | #else |
197 | | #define CS_ASSERT_RET_VAL(expr, val) \ |
198 | 7.28M | do { \ |
199 | 21.7M | if (!(expr)) { \ |
200 | 0 | return val; \ |
201 | 0 | } \ |
202 | 7.28M | } while (0) |
203 | | #endif |
204 | | |
205 | | /// An assert which returns in release builds if `!expr`. |
206 | | #if defined(CAPSTONE_DEBUG) && !defined(CAPSTONE_ASSERTION_WARNINGS) |
207 | | #define CS_ASSERT_RET(expr) assert(expr) |
208 | | #elif defined(CAPSTONE_ASSERTION_WARNINGS) |
209 | | #define CS_ASSERT_RET(expr) \ |
210 | | do { \ |
211 | | if (!(expr)) { \ |
212 | | CS_ASSERT(expr); \ |
213 | | return; \ |
214 | | } \ |
215 | | } while (0) |
216 | | #else |
217 | | #define CS_ASSERT_RET(expr) \ |
218 | 18.4M | do { \ |
219 | 18.4M | if (!(expr)) { \ |
220 | 247 | return; \ |
221 | 247 | } \ |
222 | 18.4M | } while (0) |
223 | | #endif |
224 | | |
225 | | #endif |