capstone

Форк
0
/
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
10
static 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
13
	unsigned short max_id = insns[size - 1].id;
14
	unsigned int i;
15

16
	unsigned short *cache =
17
		(unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache));
18

19
	for (i = 1; i < size; i++)
20
		cache[insns[i].id] = i;
21

22
	return 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
27
unsigned short insn_find(const insn_map *insns, unsigned int max,
28
			 unsigned int id, unsigned short **cache)
29
{
30
	if (id > insns[max - 1].id)
31
		return 0;
32

33
	if (*cache == NULL)
34
		*cache = make_id2insn(insns, max);
35

36
	return (*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.
41
int name2id(const name_map *map, int max, const char *name)
42
{
43
	int i;
44

45
	for (i = 0; i < max; i++) {
46
		if (!strcmp(map[i].name, name)) {
47
			return map[i].id;
48
		}
49
	}
50

51
	// nothing match
52
	return -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.
57
const char *id2name(const name_map *map, int max, const unsigned int id)
58
{
59
	int i;
60

61
	for (i = 0; i < max; i++) {
62
		if (map[i].id == id) {
63
			return map[i].name;
64
		}
65
	}
66

67
	// nothing match
68
	return NULL;
69
}
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
{
75
	if (!MI->flat_insn->detail)
76
		return;
77

78
	uint16_t *regs_write = MI->flat_insn->detail->regs_write;
79
	for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
80
		if (i == MI->flat_insn->detail->regs_write_count) {
81
			regs_write[i] = Reg;
82
			MI->flat_insn->detail->regs_write_count++;
83
			return;
84
		}
85
		if (regs_write[i] == Reg)
86
			return;
87
	}
88
}
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
{
94
	if (!MI->flat_insn->detail)
95
		return;
96

97
	uint16_t *regs_read = MI->flat_insn->detail->regs_read;
98
	for (int i = 0; i < MAX_IMPL_R_REGS; ++i) {
99
		if (i == MI->flat_insn->detail->regs_read_count) {
100
			regs_read[i] = Reg;
101
			MI->flat_insn->detail->regs_read_count++;
102
			return;
103
		}
104
		if (regs_read[i] == Reg)
105
			return;
106
	}
107
}
108

109
/// Removes a register from the implicit write register list.
110
void map_remove_implicit_write(MCInst *MI, uint32_t Reg)
111
{
112
	if (!MI->flat_insn->detail)
113
		return;
114

115
	uint16_t *regs_write = MI->flat_insn->detail->regs_write;
116
	bool shorten_list = false;
117
	for (int i = 0; i < MAX_IMPL_W_REGS; ++i) {
118
		if (shorten_list) {
119
			regs_write[i - 1] = regs_write[i];
120
		}
121
		if (i >= MI->flat_insn->detail->regs_write_count)
122
			return;
123

124
		if (regs_write[i] == Reg) {
125
			MI->flat_insn->detail->regs_write_count--;
126
			// The register should exist only once in the list.
127
			assert(!shorten_list);
128
			shorten_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.
135
void map_implicit_reads(MCInst *MI, const insn_map *imap)
136
{
137
#ifndef CAPSTONE_DIET
138
	if (!MI->flat_insn->detail)
139
		return;
140

141
	cs_detail *detail = MI->flat_insn->detail;
142
	unsigned Opcode = MCInst_getOpcode(MI);
143
	unsigned i = 0;
144
	uint16_t reg = imap[Opcode].regs_use[i];
145
	while (reg != 0) {
146
		if (i >= MAX_IMPL_R_REGS ||
147
		    detail->regs_read_count >= MAX_IMPL_R_REGS) {
148
			printf("ERROR: Too many implicit read register defined in "
149
			       "instruction mapping.\n");
150
			return;
151
		}
152
		detail->regs_read[detail->regs_read_count++] = reg;
153
		reg = 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.
160
void map_implicit_writes(MCInst *MI, const insn_map *imap)
161
{
162
#ifndef CAPSTONE_DIET
163
	if (!MI->flat_insn->detail)
164
		return;
165

166
	cs_detail *detail = MI->flat_insn->detail;
167
	unsigned Opcode = MCInst_getOpcode(MI);
168
	unsigned i = 0;
169
	uint16_t reg = imap[Opcode].regs_mod[i];
170
	while (reg != 0) {
171
		if (i >= MAX_IMPL_W_REGS ||
172
		    detail->regs_write_count >= MAX_IMPL_W_REGS) {
173
			printf("ERROR: Too many implicit write register defined in "
174
			       "instruction mapping.\n");
175
			return;
176
		}
177
		detail->regs_write[detail->regs_write_count++] = reg;
178
		reg = 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.
185
void add_group(MCInst *MI, unsigned /* arch_group */ group)
186
{
187
#ifndef CAPSTONE_DIET
188
	if (!MI->flat_insn->detail)
189
		return;
190

191
	cs_detail *detail = MI->flat_insn->detail;
192
	if (detail->groups_count >= MAX_NUM_GROUPS) {
193
		printf("ERROR: Too many groups defined.\n");
194
		return;
195
	}
196
	for (int i = 0; i < detail->groups_count; ++i) {
197
		if (detail->groups[i] == group) {
198
			return;
199
		}
200
	}
201
	detail->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.
207
void map_groups(MCInst *MI, const insn_map *imap)
208
{
209
#ifndef CAPSTONE_DIET
210
	if (!MI->flat_insn->detail)
211
		return;
212

213
	cs_detail *detail = MI->flat_insn->detail;
214
	unsigned Opcode = MCInst_getOpcode(MI);
215
	unsigned i = 0;
216
	uint16_t group = imap[Opcode].groups[i];
217
	while (group != 0) {
218
		if (detail->groups_count >= MAX_NUM_GROUPS) {
219
			printf("ERROR: Too many groups defined in instruction mapping.\n");
220
			return;
221
		}
222
		detail->groups[detail->groups_count++] = group;
223
		group = 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.
230
const void *map_get_suppl_info(MCInst *MI, const insn_map *imap)
231
{
232
#ifndef CAPSTONE_DIET
233
	if (!MI->flat_insn->detail)
234
		return NULL;
235

236
	unsigned Opcode = MCInst_getOpcode(MI);
237
	return &imap[Opcode].suppl_info;
238
#else
239
	return 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.
245
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
246
			unsigned imap_size)
247
{
248
	// binary searching since the IDs are sorted in order
249
	unsigned int left, right, m;
250
	unsigned int max = imap_size;
251

252
	right = max - 1;
253

254
	if (MC_Opcode < imap[0].id || MC_Opcode > imap[right].id)
255
		// not found
256
		return -1;
257

258
	left = 0;
259

260
	while (left <= right) {
261
		m = (left + right) / 2;
262
		if (MC_Opcode == imap[m].id) {
263
			return m;
264
		}
265

266
		if (MC_Opcode < imap[m].id)
267
			right = m - 1;
268
		else
269
			left = m + 1;
270
	}
271

272
	return -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.
277
void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size)
278
{
279
	unsigned int i = find_cs_id(MCInst_getOpcode(MI), imap, imap_size);
280
	if (i != -1) {
281
		MI->flat_insn->id = imap[i].mapid;
282
		return;
283
	}
284
	printf("ERROR: Could not find CS id for MCInst opcode: %d\n",
285
	       MCInst_getOpcode(MI));
286
	return;
287
}
288

289
/// Returns the operand type information from the
290
/// mapping table for instruction operands.
291
/// Only usable by `auto-sync` archs!
292
const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum,
293
				     const map_insn_ops *insn_ops_map,
294
				     size_t map_size)
295
{
296
	assert(MI);
297
	assert(MI->Opcode < map_size);
298
	assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
299
			       sizeof(insn_ops_map[MI->Opcode].ops[0]));
300

301
	return 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!
307
const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
308
				       const map_insn_ops *insn_ops_map,
309
				       size_t map_size)
310
{
311
	assert(MI);
312
	assert(MI->Opcode < map_size);
313
	assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
314
			       sizeof(insn_ops_map[MI->Opcode].ops[0]));
315

316
	cs_ac_type access = insn_ops_map[MI->Opcode].ops[OpNum].access;
317
	if (MCInst_opIsTied(MI, OpNum) || MCInst_opIsTying(MI, OpNum))
318
		access |= (access == CS_AC_READ) ? CS_AC_WRITE : CS_AC_READ;
319
	return 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) \
325
	cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \
326
	{ \
327
		if (!MI->flat_insn->detail) \
328
			return NULL; \
329
		int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \
330
		assert(OpIdx >= 0 && OpIdx < MAX_MC_OPS); \
331
		return &MI->flat_insn->detail->arch.operands[OpIdx]; \
332
	}
333

334
DEFINE_get_detail_op(arm, ARM);
335
DEFINE_get_detail_op(ppc, PPC);
336
DEFINE_get_detail_op(tricore, TriCore);
337
DEFINE_get_detail_op(aarch64, AArch64);
338
DEFINE_get_detail_op(alpha, Alpha);
339
DEFINE_get_detail_op(hppa, HPPA);
340
DEFINE_get_detail_op(loongarch, LoongArch);
341
DEFINE_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.
347
bool map_use_alias_details(const MCInst *MI) {
348
	assert(MI);
349
	return !(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.
354
void map_set_fill_detail_ops(MCInst *MI, bool Val) {
355
	assert(MI);
356
	if (!detail_is_set(MI)) {
357
		MI->fillDetailOps = false;
358
		return;
359
	}
360

361
	MI->fillDetailOps = Val;
362
}
363

364
/// Sets the instruction alias flags and the given alias id.
365
void map_set_is_alias_insn(MCInst *MI, bool Val, uint64_t Alias) {
366
	assert(MI);
367
	MI->isAliasInstr = Val;
368
	MI->flat_insn->is_alias = Val;
369
	MI->flat_insn->alias_id = Alias;
370
}
371

372
static inline bool char_ends_mnem(const char c) {
373
	return (!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.
379
void map_set_alias_id(MCInst *MI, const SStream *O, const name_map *alias_mnem_id_map, int map_size) {
380
	if (!MCInst_isAlias(MI))
381
		return;
382

383
	char alias_mnem[16] = { 0 };
384
	int i = 0, j = 0;
385
	const char *asm_str_buf = O->buffer;
386
	// Skip spaces and tabs
387
	while (is_blank_char(asm_str_buf[i])) {
388
		if (!asm_str_buf[i]) {
389
			MI->flat_insn->alias_id = -1;
390
			return;
391
		}
392
		++i;
393
	}
394
	for (; j < sizeof(alias_mnem) - 1; ++j, ++i) {
395
		if (char_ends_mnem(asm_str_buf[i]))
396
			break;
397
		alias_mnem[j] = asm_str_buf[i];
398
	}
399

400
	MI->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.
409
uint64_t enum_map_bin_search(const cs_enum_id_map *map, size_t map_len,
410
			     const char *id, bool *found)
411
{
412
	size_t l = 0;
413
	size_t r = map_len;
414
	size_t id_len = strlen(id);
415

416
	while (l <= r) {
417
		size_t m = (l + r) / 2;
418
		size_t j = 0;
419
		size_t i = 0;
420
		size_t entry_len = strlen(map[m].str);
421

422
		while (j < entry_len && i < id_len && id[i] == map[m].str[j]) {
423
			++j, ++i;
424
		}
425
		if (i == id_len && j == entry_len) {
426
			*found = true;
427
			return map[m].val;
428
		}
429

430
		if (id[i] < map[m].str[j]) {
431
			r = m - 1;
432
		} else if (id[i] > map[m].str[j]) {
433
			l = m + 1;
434
		}
435
		if (m == 0 || (l + r) / 2 >= map_len) {
436
			// Break before we go out of bounds.
437
			break;
438
		}
439
	}
440
	*found = false;
441
	return UINT64_MAX;
442
}
443

444

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.