/src/capstonenext/Mapping.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | /* Rot127 <unisono@quyllur.org>, 2022-2023 */ |
4 | | |
5 | | #include "Mapping.h" |
6 | | #include "capstone/capstone.h" |
7 | | #include "utils.h" |
8 | | |
9 | | // create a cache for fast id lookup |
10 | | static unsigned short *make_id2insn(const insn_map *insns, unsigned int size) |
11 | 10.0k | { |
12 | | // NOTE: assume that the max id is always put at the end of insns array |
13 | 10.0k | unsigned short max_id = insns[size - 1].id; |
14 | 10.0k | unsigned short i; |
15 | | |
16 | 10.0k | unsigned short *cache = |
17 | 10.0k | (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache)); |
18 | | |
19 | 10.7M | for (i = 1; i < size; i++) |
20 | 10.7M | cache[insns[i].id] = i; |
21 | | |
22 | 10.0k | return cache; |
23 | 10.0k | } |
24 | | |
25 | | // look for @id in @insns, given its size in @max. first time call will update |
26 | | // @cache. return 0 if not found |
27 | | unsigned short insn_find(const insn_map *insns, unsigned int max, |
28 | | unsigned int id, unsigned short **cache) |
29 | 271k | { |
30 | 271k | if (id > insns[max - 1].id) |
31 | 0 | return 0; |
32 | | |
33 | 271k | if (*cache == NULL) |
34 | 10.0k | *cache = make_id2insn(insns, max); |
35 | | |
36 | 271k | return (*cache)[id]; |
37 | 271k | } |
38 | | |
39 | | // Gives the id for the given @name if it is saved in @map. |
40 | | // Returns the id or -1 if not found. |
41 | | int name2id(const name_map *map, int max, const char *name) |
42 | 99.6k | { |
43 | 99.6k | int i; |
44 | | |
45 | 17.3M | for (i = 0; i < max; i++) { |
46 | 17.3M | if (!strcmp(map[i].name, name)) { |
47 | 77.1k | return map[i].id; |
48 | 77.1k | } |
49 | 17.3M | } |
50 | | |
51 | | // nothing match |
52 | 22.5k | return -1; |
53 | 99.6k | } |
54 | | |
55 | | // Gives the name for the given @id if it is saved in @map. |
56 | | // Returns the name or NULL if not found. |
57 | | const char *id2name(const name_map *map, int max, const unsigned int id) |
58 | 2.45M | { |
59 | 2.45M | int i; |
60 | | |
61 | 47.8M | for (i = 0; i < max; i++) { |
62 | 47.8M | if (map[i].id == id) { |
63 | 2.45M | return map[i].name; |
64 | 2.45M | } |
65 | 47.8M | } |
66 | | |
67 | | // nothing match |
68 | 0 | return NULL; |
69 | 2.45M | } |
70 | | |
71 | | /// Adds a register to the implicit write register list. |
72 | | /// It will not add the same register twice. |
73 | | void map_add_implicit_write(MCInst *MI, uint32_t Reg) |
74 | 385k | { |
75 | 385k | if (!MI->flat_insn->detail) |
76 | 0 | return; |
77 | | |
78 | 385k | uint16_t *regs_write = MI->flat_insn->detail->regs_write; |
79 | 387k | for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { |
80 | 387k | if (i == MI->flat_insn->detail->regs_write_count) { |
81 | 368k | regs_write[i] = Reg; |
82 | 368k | MI->flat_insn->detail->regs_write_count++; |
83 | 368k | return; |
84 | 368k | } |
85 | 18.1k | if (regs_write[i] == Reg) |
86 | 16.9k | return; |
87 | 18.1k | } |
88 | 385k | } |
89 | | |
90 | | /// Adds a register to the implicit read register list. |
91 | | /// It will not add the same register twice. |
92 | | void map_add_implicit_read(MCInst *MI, uint32_t Reg) |
93 | 170k | { |
94 | 170k | if (!MI->flat_insn->detail) |
95 | 0 | return; |
96 | | |
97 | 170k | uint16_t *regs_read = MI->flat_insn->detail->regs_read; |
98 | 177k | for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { |
99 | 177k | if (i == MI->flat_insn->detail->regs_read_count) { |
100 | 154k | regs_read[i] = Reg; |
101 | 154k | MI->flat_insn->detail->regs_read_count++; |
102 | 154k | return; |
103 | 154k | } |
104 | 23.6k | if (regs_read[i] == Reg) |
105 | 16.0k | return; |
106 | 23.6k | } |
107 | 170k | } |
108 | | |
109 | | /// Removes a register from the implicit write register list. |
110 | | void map_remove_implicit_write(MCInst *MI, uint32_t Reg) |
111 | 28.7k | { |
112 | 28.7k | if (!MI->flat_insn->detail) |
113 | 0 | return; |
114 | | |
115 | 28.7k | uint16_t *regs_write = MI->flat_insn->detail->regs_write; |
116 | 28.7k | bool shorten_list = false; |
117 | 31.9k | for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { |
118 | 31.9k | if (shorten_list) { |
119 | 3.20k | regs_write[i - 1] = regs_write[i]; |
120 | 3.20k | } |
121 | 31.9k | if (i >= MI->flat_insn->detail->regs_write_count) |
122 | 28.7k | return; |
123 | | |
124 | 3.20k | if (regs_write[i] == Reg) { |
125 | 3.20k | MI->flat_insn->detail->regs_write_count--; |
126 | | // The register should exist only once in the list. |
127 | 3.20k | assert(!shorten_list); |
128 | 3.20k | shorten_list = true; |
129 | 3.20k | } |
130 | 3.20k | } |
131 | 28.7k | } |
132 | | |
133 | | /// Copies the implicit read registers of @imap to @MI->flat_insn. |
134 | | /// Already present registers will be preserved. |
135 | | void map_implicit_reads(MCInst *MI, const insn_map *imap) |
136 | 1.44M | { |
137 | 1.44M | #ifndef CAPSTONE_DIET |
138 | 1.44M | if (!MI->flat_insn->detail) |
139 | 0 | return; |
140 | | |
141 | 1.44M | cs_detail *detail = MI->flat_insn->detail; |
142 | 1.44M | unsigned Opcode = MCInst_getOpcode(MI); |
143 | 1.44M | unsigned i = 0; |
144 | 1.44M | uint16_t reg = imap[Opcode].regs_use[i]; |
145 | 1.51M | while (reg != 0) { |
146 | 71.3k | if (i >= MAX_IMPL_R_REGS || |
147 | 71.3k | detail->regs_read_count >= MAX_IMPL_R_REGS) { |
148 | 0 | printf("ERROR: Too many implicit read register defined in " |
149 | 0 | "instruction mapping.\n"); |
150 | 0 | return; |
151 | 0 | } |
152 | 71.3k | detail->regs_read[detail->regs_read_count++] = reg; |
153 | 71.3k | reg = imap[Opcode].regs_use[++i]; |
154 | 71.3k | } |
155 | 1.44M | #endif // CAPSTONE_DIET |
156 | 1.44M | } |
157 | | |
158 | | /// Copies the implicit write registers of @imap to @MI->flat_insn. |
159 | | /// Already present registers will be preserved. |
160 | | void map_implicit_writes(MCInst *MI, const insn_map *imap) |
161 | 1.44M | { |
162 | 1.44M | #ifndef CAPSTONE_DIET |
163 | 1.44M | if (!MI->flat_insn->detail) |
164 | 0 | return; |
165 | | |
166 | 1.44M | cs_detail *detail = MI->flat_insn->detail; |
167 | 1.44M | unsigned Opcode = MCInst_getOpcode(MI); |
168 | 1.44M | unsigned i = 0; |
169 | 1.44M | uint16_t reg = imap[Opcode].regs_mod[i]; |
170 | 1.69M | while (reg != 0) { |
171 | 246k | if (i >= MAX_IMPL_W_REGS || |
172 | 246k | detail->regs_write_count >= MAX_IMPL_W_REGS) { |
173 | 0 | printf("ERROR: Too many implicit write register defined in " |
174 | 0 | "instruction mapping.\n"); |
175 | 0 | return; |
176 | 0 | } |
177 | 246k | detail->regs_write[detail->regs_write_count++] = reg; |
178 | 246k | reg = imap[Opcode].regs_mod[++i]; |
179 | 246k | } |
180 | 1.44M | #endif // CAPSTONE_DIET |
181 | 1.44M | } |
182 | | |
183 | | /// Adds a given group to @MI->flat_insn. |
184 | | void add_group(MCInst *MI, unsigned /* arch_group */ group) |
185 | 45.1k | { |
186 | 45.1k | #ifndef CAPSTONE_DIET |
187 | 45.1k | if (!MI->flat_insn->detail) |
188 | 0 | return; |
189 | | |
190 | 45.1k | cs_detail *detail = MI->flat_insn->detail; |
191 | 45.1k | if (detail->groups_count >= MAX_NUM_GROUPS) { |
192 | 0 | printf("ERROR: Too many groups defined.\n"); |
193 | 0 | return; |
194 | 0 | } |
195 | 45.1k | detail->groups[detail->groups_count++] = group; |
196 | 45.1k | #endif // CAPSTONE_DIET |
197 | 45.1k | } |
198 | | |
199 | | /// Copies the groups from @imap to @MI->flat_insn. |
200 | | /// Already present groups will be preserved. |
201 | | void map_groups(MCInst *MI, const insn_map *imap) |
202 | 1.44M | { |
203 | 1.44M | #ifndef CAPSTONE_DIET |
204 | 1.44M | if (!MI->flat_insn->detail) |
205 | 0 | return; |
206 | | |
207 | 1.44M | cs_detail *detail = MI->flat_insn->detail; |
208 | 1.44M | unsigned Opcode = MCInst_getOpcode(MI); |
209 | 1.44M | unsigned i = 0; |
210 | 1.44M | uint16_t group = imap[Opcode].groups[i]; |
211 | 3.00M | while (group != 0) { |
212 | 1.55M | if (detail->groups_count >= MAX_NUM_GROUPS) { |
213 | 0 | printf("ERROR: Too many groups defined in instruction mapping.\n"); |
214 | 0 | return; |
215 | 0 | } |
216 | 1.55M | detail->groups[detail->groups_count++] = group; |
217 | 1.55M | group = imap[Opcode].groups[++i]; |
218 | 1.55M | } |
219 | 1.44M | #endif // CAPSTONE_DIET |
220 | 1.44M | } |
221 | | |
222 | | // Search for the CS instruction id for the given @MC_Opcode in @imap. |
223 | | // return -1 if none is found. |
224 | | unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap, |
225 | | unsigned imap_size) |
226 | 1.44M | { |
227 | | // binary searching since the IDs are sorted in order |
228 | 1.44M | unsigned int left, right, m; |
229 | 1.44M | unsigned int max = imap_size; |
230 | | |
231 | 1.44M | right = max - 1; |
232 | | |
233 | 1.44M | if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id) |
234 | | // not found |
235 | 0 | return -1; |
236 | | |
237 | 1.44M | left = 0; |
238 | | |
239 | 16.4M | while (left <= right) { |
240 | 16.4M | m = (left + right) / 2; |
241 | 16.4M | if (MC_Opcode == imap[m].id) { |
242 | 1.44M | return m; |
243 | 1.44M | } |
244 | | |
245 | 14.9M | if (MC_Opcode < imap[m].id) |
246 | 4.53M | right = m - 1; |
247 | 10.4M | else |
248 | 10.4M | left = m + 1; |
249 | 14.9M | } |
250 | | |
251 | 0 | return -1; |
252 | 1.44M | } |
253 | | |
254 | | /// Sets the Capstone instruction id which maps to the @MI opcode. |
255 | | /// If no mapping is found the function returns and prints an error. |
256 | | void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size) |
257 | 1.44M | { |
258 | 1.44M | unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size); |
259 | 1.44M | if (i != -1) { |
260 | 1.44M | MI->flat_insn->id = imap[i].mapid; |
261 | 1.44M | return; |
262 | 1.44M | } |
263 | 0 | printf("ERROR: Could not find CS id for MCInst opcode: %d\n", |
264 | 0 | MCInst_getOpcode(MI)); |
265 | 0 | return; |
266 | 1.44M | } |
267 | | |
268 | | /// Returns the operand type information from the |
269 | | /// mapping table for instruction operands. |
270 | | /// Only usable by `auto-sync` archs! |
271 | | const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum, |
272 | | const map_insn_ops *insn_ops_map, |
273 | | size_t map_size) |
274 | 12.6M | { |
275 | 12.6M | assert(MI); |
276 | 12.6M | assert(MI->Opcode < map_size); |
277 | 12.6M | assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
278 | 12.6M | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
279 | | |
280 | 12.6M | return insn_ops_map[MI->Opcode].ops[OpNum].type; |
281 | 12.6M | } |
282 | | |
283 | | /// Returns the operand access flags from the |
284 | | /// mapping table for instruction operands. |
285 | | /// Only usable by `auto-sync` archs! |
286 | | const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum, |
287 | | const map_insn_ops *insn_ops_map, |
288 | | size_t map_size) |
289 | 4.21M | { |
290 | 4.21M | assert(MI); |
291 | 4.21M | assert(MI->Opcode < map_size); |
292 | 4.21M | assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
293 | 4.21M | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
294 | | |
295 | 4.21M | cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access; |
296 | 4.21M | if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum)) |
297 | 338k | access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ; |
298 | 4.21M | return access; |
299 | 4.21M | } |
300 | | |
301 | | /// Returns the operand at detail->arch.operands[op_count + offset] |
302 | | /// Or NULL if detail is not set. |
303 | | #define DEFINE_get_detail_op(arch, ARCH) \ |
304 | | cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \ |
305 | 16.5M | { \ |
306 | 16.5M | if (!MI->flat_insn->detail) \ |
307 | 16.5M | return NULL; \ |
308 | 16.5M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ |
309 | 16.5M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ |
310 | 16.5M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ |
311 | 16.5M | } Line | Count | Source | 305 | 13.1M | { \ | 306 | 13.1M | if (!MI->flat_insn->detail) \ | 307 | 13.1M | return NULL; \ | 308 | 13.1M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 309 | 13.1M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 310 | 13.1M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 311 | 13.1M | } |
Line | Count | Source | 305 | 557k | { \ | 306 | 557k | if (!MI->flat_insn->detail) \ | 307 | 557k | return NULL; \ | 308 | 557k | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 309 | 557k | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 310 | 557k | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 311 | 557k | } |
Unexecuted instantiation: TriCore_get_detail_op Line | Count | Source | 305 | 2.87M | { \ | 306 | 2.87M | if (!MI->flat_insn->detail) \ | 307 | 2.87M | return NULL; \ | 308 | 2.87M | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ | 309 | 2.87M | assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \ | 310 | 2.87M | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ | 311 | 2.87M | } |
|
312 | | |
313 | | DEFINE_get_detail_op(arm, ARM); |
314 | | DEFINE_get_detail_op(ppc, PPC); |
315 | | DEFINE_get_detail_op(tricore, TriCore); |
316 | | DEFINE_get_detail_op(aarch64, AArch64); |
317 | | |
318 | | /// Returns true if for this architecture the |
319 | | /// alias operands should be filled. |
320 | | /// TODO: Replace this with a proper option. |
321 | | /// So it can be toggled between disas() calls. |
322 | 2.86M | bool map_use_alias_details(const MCInst *MI) { |
323 | 2.86M | assert(MI); |
324 | 2.86M | return !(MI->csh->detail_opt & CS_OPT_DETAIL_REAL); |
325 | 2.86M | } |
326 | | |
327 | | /// Sets the setDetailOps flag to @p Val. |
328 | | /// If detail == NULLit refuses to set the flag to true. |
329 | 2.80M | void map_set_fill_detail_ops(MCInst *MI, bool Val) { |
330 | 2.80M | assert(MI); |
331 | 2.80M | if (!detail_is_set(MI)) { |
332 | 0 | MI->fillDetailOps = false; |
333 | 0 | return; |
334 | 0 | } |
335 | | |
336 | 2.80M | MI->fillDetailOps = Val; |
337 | 2.80M | } |
338 | | |
339 | | /// Sets the instruction alias flags and the given alias id. |
340 | 0 | void map_set_is_alias_insn(MCInst *MI, bool Val, uint64_t Alias) { |
341 | 0 | assert(MI); |
342 | 0 | MI->isAliasInstr = Val; |
343 | 0 | MI->flat_insn->is_alias = Val; |
344 | 0 | MI->flat_insn->alias_id = Alias; |
345 | 0 | } |
346 | | |
347 | 273k | static inline bool char_ends_mnem(const char c) { |
348 | 273k | return (!c || c == ' ' || c == '\t'); |
349 | 273k | } |
350 | | |
351 | | /// Sets an alternative id for some instruction. |
352 | | /// Or -1 if it fails. |
353 | | /// You must add (<ARCH>_INS_ALIAS_BEGIN + 1) to the id to get the real id. |
354 | 1.43M | void map_set_alias_id(MCInst *MI, const SStream *O, const name_map *alias_mnem_id_map, int map_size) { |
355 | 1.43M | if (!MCInst_isAlias(MI)) |
356 | 1.37M | return; |
357 | | |
358 | 58.1k | char alias_mnem[16] = { 0 }; |
359 | 58.1k | int i = 0, j = 0; |
360 | 58.1k | const char *asm_str_buf = O->buffer; |
361 | | // Skip spaces and tabs |
362 | 59.7k | while (is_blank_char(asm_str_buf[i])) { |
363 | 1.57k | if (!asm_str_buf[i]) { |
364 | 0 | MI->flat_insn->alias_id = -1; |
365 | 0 | return; |
366 | 0 | } |
367 | 1.57k | ++i; |
368 | 1.57k | } |
369 | 273k | for (; j < sizeof(alias_mnem) - 1; ++j, ++i) { |
370 | 273k | if (char_ends_mnem(asm_str_buf[i])) |
371 | 58.1k | break; |
372 | 215k | alias_mnem[j] = asm_str_buf[i]; |
373 | 215k | } |
374 | | |
375 | 58.1k | MI->flat_insn->alias_id = name2id(alias_mnem_id_map, map_size, alias_mnem); |
376 | 58.1k | } |
377 | | |