capstone
/
Mapping.c
442 строки · 11.4 Кб
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
10static unsigned short *make_id2insn(const insn_map *insns, unsigned int size)
11{
12// NOTE: assume that the max id is always put at the end of insns array
13unsigned short max_id = insns[size - 1].id;
14unsigned int i;
15
16unsigned short *cache =
17(unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache));
18
19for (i = 1; i < size; i++)
20cache[insns[i].id] = i;
21
22return cache;
23}
24
25// look for @id in @insns, given its size in @max. first time call will update
26// @cache. return 0 if not found
27unsigned short insn_find(const insn_map *insns, unsigned int max,
28unsigned int id, unsigned short **cache)
29{
30if (id > insns[max - 1].id)
31return 0;
32
33if (*cache == NULL)
34*cache = make_id2insn(insns, max);
35
36return (*cache)[id];
37}
38
39// Gives the id for the given @name if it is saved in @map.
40// Returns the id or -1 if not found.
41int name2id(const name_map *map, int max, const char *name)
42{
43int i;
44
45for (i = 0; i < max; i++) {
46if (!strcmp(map[i].name, name)) {
47return map[i].id;
48}
49}
50
51// nothing match
52return -1;
53}
54
55// Gives the name for the given @id if it is saved in @map.
56// Returns the name or NULL if not found.
57const char *id2name(const name_map *map, int max, const unsigned int id)
58{
59int i;
60
61for (i = 0; i < max; i++) {
62if (map[i].id == id) {
63return map[i].name;
64}
65}
66
67// nothing match
68return NULL;
69}
70
71/// Adds a register to the implicit write register list.
72/// It will not add the same register twice.
73void map_add_implicit_write(MCInst *MI, uint32_t Reg)
74{
75if (!MI->flat_insn->detail)
76return;
77
78uint16_t *regs_write = MI->flat_insn->detail->regs_write;
79for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
80if (i == MI->flat_insn->detail->regs_write_count) {
81regs_write[i] = Reg;
82MI->flat_insn->detail->regs_write_count++;
83return;
84}
85if (regs_write[i] == Reg)
86return;
87}
88}
89
90/// Adds a register to the implicit read register list.
91/// It will not add the same register twice.
92void map_add_implicit_read(MCInst *MI, uint32_t Reg)
93{
94if (!MI->flat_insn->detail)
95return;
96
97uint16_t *regs_read = MI->flat_insn->detail->regs_read;
98for (int i = 0; i < MAX_IMPL_R_REGS; ++i) {
99if (i == MI->flat_insn->detail->regs_read_count) {
100regs_read[i] = Reg;
101MI->flat_insn->detail->regs_read_count++;
102return;
103}
104if (regs_read[i] == Reg)
105return;
106}
107}
108
109/// Removes a register from the implicit write register list.
110void map_remove_implicit_write(MCInst *MI, uint32_t Reg)
111{
112if (!MI->flat_insn->detail)
113return;
114
115uint16_t *regs_write = MI->flat_insn->detail->regs_write;
116bool shorten_list = false;
117for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
118if (shorten_list) {
119regs_write[i - 1] = regs_write[i];
120}
121if (i >= MI->flat_insn->detail->regs_write_count)
122return;
123
124if (regs_write[i] == Reg) {
125MI->flat_insn->detail->regs_write_count--;
126// The register should exist only once in the list.
127assert(!shorten_list);
128shorten_list = true;
129}
130}
131}
132
133/// Copies the implicit read registers of @imap to @MI->flat_insn.
134/// Already present registers will be preserved.
135void map_implicit_reads(MCInst *MI, const insn_map *imap)
136{
137#ifndef CAPSTONE_DIET
138if (!MI->flat_insn->detail)
139return;
140
141cs_detail *detail = MI->flat_insn->detail;
142unsigned Opcode = MCInst_getOpcode(MI);
143unsigned i = 0;
144uint16_t reg = imap[Opcode].regs_use[i];
145while (reg != 0) {
146if (i >= MAX_IMPL_R_REGS ||
147detail->regs_read_count >= MAX_IMPL_R_REGS) {
148printf("ERROR: Too many implicit read register defined in "
149"instruction mapping.\n");
150return;
151}
152detail->regs_read[detail->regs_read_count++] = reg;
153reg = imap[Opcode].regs_use[++i];
154}
155#endif // CAPSTONE_DIET
156}
157
158/// Copies the implicit write registers of @imap to @MI->flat_insn.
159/// Already present registers will be preserved.
160void map_implicit_writes(MCInst *MI, const insn_map *imap)
161{
162#ifndef CAPSTONE_DIET
163if (!MI->flat_insn->detail)
164return;
165
166cs_detail *detail = MI->flat_insn->detail;
167unsigned Opcode = MCInst_getOpcode(MI);
168unsigned i = 0;
169uint16_t reg = imap[Opcode].regs_mod[i];
170while (reg != 0) {
171if (i >= MAX_IMPL_W_REGS ||
172detail->regs_write_count >= MAX_IMPL_W_REGS) {
173printf("ERROR: Too many implicit write register defined in "
174"instruction mapping.\n");
175return;
176}
177detail->regs_write[detail->regs_write_count++] = reg;
178reg = imap[Opcode].regs_mod[++i];
179}
180#endif // CAPSTONE_DIET
181}
182
183/// Adds a given group to @MI->flat_insn.
184/// A group is never added twice.
185void add_group(MCInst *MI, unsigned /* arch_group */ group)
186{
187#ifndef CAPSTONE_DIET
188if (!MI->flat_insn->detail)
189return;
190
191cs_detail *detail = MI->flat_insn->detail;
192if (detail->groups_count >= MAX_NUM_GROUPS) {
193printf("ERROR: Too many groups defined.\n");
194return;
195}
196for (int i = 0; i < detail->groups_count; ++i) {
197if (detail->groups[i] == group) {
198return;
199}
200}
201detail->groups[detail->groups_count++] = group;
202#endif // CAPSTONE_DIET
203}
204
205/// Copies the groups from @imap to @MI->flat_insn.
206/// Already present groups will be preserved.
207void map_groups(MCInst *MI, const insn_map *imap)
208{
209#ifndef CAPSTONE_DIET
210if (!MI->flat_insn->detail)
211return;
212
213cs_detail *detail = MI->flat_insn->detail;
214unsigned Opcode = MCInst_getOpcode(MI);
215unsigned i = 0;
216uint16_t group = imap[Opcode].groups[i];
217while (group != 0) {
218if (detail->groups_count >= MAX_NUM_GROUPS) {
219printf("ERROR: Too many groups defined in instruction mapping.\n");
220return;
221}
222detail->groups[detail->groups_count++] = group;
223group = imap[Opcode].groups[++i];
224}
225#endif // CAPSTONE_DIET
226}
227
228/// Returns the pointer to the supllementary information in
229/// the instruction mapping table @imap or NULL in case of failure.
230const void *map_get_suppl_info(MCInst *MI, const insn_map *imap)
231{
232#ifndef CAPSTONE_DIET
233if (!MI->flat_insn->detail)
234return NULL;
235
236unsigned Opcode = MCInst_getOpcode(MI);
237return &imap[Opcode].suppl_info;
238#else
239return NULL;
240#endif // CAPSTONE_DIET
241}
242
243// Search for the CS instruction id for the given @MC_Opcode in @imap.
244// return -1 if none is found.
245unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
246unsigned imap_size)
247{
248// binary searching since the IDs are sorted in order
249unsigned int left, right, m;
250unsigned int max = imap_size;
251
252right = max - 1;
253
254if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id)
255// not found
256return -1;
257
258left = 0;
259
260while (left <= right) {
261m = (left + right) / 2;
262if (MC_Opcode == imap[m].id) {
263return m;
264}
265
266if (MC_Opcode < imap[m].id)
267right = m - 1;
268else
269left = m + 1;
270}
271
272return -1;
273}
274
275/// Sets the Capstone instruction id which maps to the @MI opcode.
276/// If no mapping is found the function returns and prints an error.
277void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size)
278{
279unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size);
280if (i != -1) {
281MI->flat_insn->id = imap[i].mapid;
282return;
283}
284printf("ERROR: Could not find CS id for MCInst opcode: %d\n",
285MCInst_getOpcode(MI));
286return;
287}
288
289/// Returns the operand type information from the
290/// mapping table for instruction operands.
291/// Only usable by `auto-sync` archs!
292const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum,
293const map_insn_ops *insn_ops_map,
294size_t map_size)
295{
296assert(MI);
297assert(MI->Opcode < map_size);
298assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
299sizeof(insn_ops_map[MI->Opcode].ops[0]));
300
301return insn_ops_map[MI->Opcode].ops[OpNum].type;
302}
303
304/// Returns the operand access flags from the
305/// mapping table for instruction operands.
306/// Only usable by `auto-sync` archs!
307const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
308const map_insn_ops *insn_ops_map,
309size_t map_size)
310{
311assert(MI);
312assert(MI->Opcode < map_size);
313assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
314sizeof(insn_ops_map[MI->Opcode].ops[0]));
315
316cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access;
317if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum))
318access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ;
319return access;
320}
321
322/// Returns the operand at detail->arch.operands[op_count + offset]
323/// Or NULL if detail is not set.
324#define DEFINE_get_detail_op(arch, ARCH) \
325cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \
326{ \
327if (!MI->flat_insn->detail) \
328return NULL; \
329int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \
330assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \
331return &MI->flat_insn->detail->arch.operands[OpIdx]; \
332}
333
334DEFINE_get_detail_op(arm, ARM);
335DEFINE_get_detail_op(ppc, PPC);
336DEFINE_get_detail_op(tricore, TriCore);
337DEFINE_get_detail_op(aarch64, AArch64);
338DEFINE_get_detail_op(alpha, Alpha);
339DEFINE_get_detail_op(hppa, HPPA);
340DEFINE_get_detail_op(loongarch, LoongArch);
341DEFINE_get_detail_op(riscv, RISCV);
342
343/// Returns true if for this architecture the
344/// alias operands should be filled.
345/// TODO: Replace this with a proper option.
346/// So it can be toggled between disas() calls.
347bool map_use_alias_details(const MCInst *MI) {
348assert(MI);
349return !(MI->csh->detail_opt & CS_OPT_DETAIL_REAL);
350}
351
352/// Sets the setDetailOps flag to @p Val.
353/// If detail == NULLit refuses to set the flag to true.
354void map_set_fill_detail_ops(MCInst *MI, bool Val) {
355assert(MI);
356if (!detail_is_set(MI)) {
357MI->fillDetailOps = false;
358return;
359}
360
361MI->fillDetailOps = Val;
362}
363
364/// Sets the instruction alias flags and the given alias id.
365void map_set_is_alias_insn(MCInst *MI, bool Val, uint64_t Alias) {
366assert(MI);
367MI->isAliasInstr = Val;
368MI->flat_insn->is_alias = Val;
369MI->flat_insn->alias_id = Alias;
370}
371
372static inline bool char_ends_mnem(const char c) {
373return (!c || c == ' ' || c == '\t');
374}
375
376/// Sets an alternative id for some instruction.
377/// Or -1 if it fails.
378/// You must add (<ARCH>_INS_ALIAS_BEGIN + 1) to the id to get the real id.
379void map_set_alias_id(MCInst *MI, const SStream *O, const name_map *alias_mnem_id_map, int map_size) {
380if (!MCInst_isAlias(MI))
381return;
382
383char alias_mnem[16] = { 0 };
384int i = 0, j = 0;
385const char *asm_str_buf = O->buffer;
386// Skip spaces and tabs
387while (is_blank_char(asm_str_buf[i])) {
388if (!asm_str_buf[i]) {
389MI->flat_insn->alias_id = -1;
390return;
391}
392++i;
393}
394for (; j < sizeof(alias_mnem) - 1; ++j, ++i) {
395if (char_ends_mnem(asm_str_buf[i]))
396break;
397alias_mnem[j] = asm_str_buf[i];
398}
399
400MI->flat_insn->alias_id = name2id(alias_mnem_id_map, map_size, alias_mnem);
401}
402
403/// Does a binary search over the given map and searches for @id.
404/// If @id exists in @map, it sets @found to true and returns
405/// the value for the @id.
406/// Otherwise, @found is set to false and it returns UINT64_MAX.
407///
408/// Of course it assumes the map is sorted.
409uint64_t enum_map_bin_search(const cs_enum_id_map *map, size_t map_len,
410const char *id, bool *found)
411{
412size_t l = 0;
413size_t r = map_len;
414size_t id_len = strlen(id);
415
416while (l <= r) {
417size_t m = (l + r) / 2;
418size_t j = 0;
419size_t i = 0;
420size_t entry_len = strlen(map[m].str);
421
422while (j < entry_len && i < id_len && id[i] == map[m].str[j]) {
423++j, ++i;
424}
425if (i == id_len && j == entry_len) {
426*found = true;
427return map[m].val;
428}
429
430if (id[i] < map[m].str[j]) {
431r = m - 1;
432} else if (id[i] > map[m].str[j]) {
433l = m + 1;
434}
435if (m == 0 || (l + r) / 2 >= map_len) {
436// Break before we go out of bounds.
437break;
438}
439}
440*found = false;
441return UINT64_MAX;
442}
443
444