blob: 135ca41fa451ec48a0b793ad2e1fd41a38421bd8 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <capstone.h>
8
9#include "cs_priv.h"
10
11#include "MCRegisterInfo.h"
12
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080013#include "utils.h"
14
Nguyen Anh Quynh58747ad2013-12-22 13:37:13 +080015void (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080016cs_err (*arch_option[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value);
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080017
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080018unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080019
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080020
21void cs_version(int *major, int *minor)
22{
23 *major = CS_API_MAJOR;
24 *minor = CS_API_MINOR;
25}
26
27unsigned int cs_version_ex(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080028{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080029 if (major != NULL && minor != NULL) {
30 *major = CS_API_MAJOR;
31 *minor = CS_API_MINOR;
32 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080033
34 return (CS_API_MAJOR << 8) + CS_API_MINOR;
35}
36
37bool cs_support(cs_arch arch)
38{
39 if (arch == CS_ARCH_ALL)
40 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
41 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86));
42
43 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080044}
45
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080046cs_err cs_errno(csh handle)
47{
48 if (!handle)
49 return CS_ERR_CSH;
50
51 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
52
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080053 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080054}
55
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080056cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
57{
danghvu2b192962013-12-19 22:40:28 -060058 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059
danghvu2b192962013-12-19 22:40:28 -060060 ud = calloc(1, sizeof(*ud));
61 if (!ud) {
62 // memory insufficient
63 return CS_ERR_MEM;
64 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080065
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080066 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
67 ud->errnum = CS_ERR_OK;
68 ud->arch = arch;
69 ud->mode = mode;
70 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
71 ud->reg_name = NULL;
72 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080073
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080074 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080075 } else {
76 *handle = 0;
77 return CS_ERR_ARCH;
78 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080079
danghvu2b192962013-12-19 22:40:28 -060080 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081
danghvu2b192962013-12-19 22:40:28 -060082 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080083}
84
85cs_err cs_close(csh handle)
86{
87 if (!handle)
88 return CS_ERR_CSH;
89
90 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
91
92 switch (ud->arch) {
93 case CS_ARCH_X86:
94 break;
95 case CS_ARCH_ARM:
96 case CS_ARCH_MIPS:
97 case CS_ARCH_ARM64:
98 free(ud->printer_info);
99 break;
100 default: // unsupported architecture
101 return CS_ERR_HANDLE;
102 }
103
104 memset(ud, 0, sizeof(*ud));
105 free(ud);
106
107 return CS_ERR_OK;
108}
109
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800110#define MIN(x, y) ((x) < (y) ? (x) : (y))
111
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800112// fill insn with mnemonic & operands info
113static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
pancakef0e4eed2013-12-11 22:14:42 +0100114 PostPrinter_t printer, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800115{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800116 if (handle->detail) {
117 memcpy(insn, &mci->pub_insn, sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800118
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800119 // fill the instruction bytes
120 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800121
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800122 } else {
123 insn->address = mci->address;
124 insn->size = mci->insn_size;
125 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800126
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800127 // map internal instruction opcode to public insn ID
128 if (handle->insn_id)
129 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
130
131 // alias instruction might have ID saved in OpcodePub
132 if (MCInst_getOpcodePub(mci))
133 insn->id = MCInst_getOpcodePub(mci);
134
135 // post printer handles some corner cases (hacky)
136 if (printer)
137 printer((csh)handle, insn, buffer);
138
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800139 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800140 // find first space or tab
141 char *sp = buffer;
142 for (sp = buffer; *sp; sp++)
143 if (*sp == ' '||*sp == '\t')
144 break;
145 if (*sp) {
146 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800147 // find the next non-space char
148 sp++;
149 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
150 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800151 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
152 } else
153 insn->op_str[0] = '\0';
154
155 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
156 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
157}
158
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800159cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800160{
danghvu2b192962013-12-19 22:40:28 -0600161 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
162 if (!handle)
163 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800164
danghvu0b6ea042013-12-19 23:07:26 -0600165 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800166 handle->detail = value;
167 return CS_ERR_OK;
168 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800169
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800170 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800171}
172
pancakef0e4eed2013-12-11 22:14:42 +0100173size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn *insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800174{
175 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
176 MCInst mci;
177 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800178 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800179
180 if (!handle) {
181 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800182 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183 return 0;
184 }
185
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800186 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800187 memset(insn, 0, count * sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800188
189 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600190 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800191 mci.detail = handle->detail;
192 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193
194 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
195 if (r) {
196 SStream ss;
197 SStream_Init(&ss);
198
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800199 // relative branches need to know the address & size of current insn
200 mci.insn_size = insn_size;
201 mci.address = offset;
202
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800203 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800204 // save all the information for non-detailed mode
205 mci.pub_insn.address = offset;
206 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800207 }
208
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800209 handle->printer(&mci, &ss, handle->printer_info);
210
Joxean114df0e2013-12-04 07:11:32 +0100211 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212
213 c++;
214 insn++;
215 buffer += insn_size;
216 size -= insn_size;
217 offset += insn_size;
218
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800219 if (c == count)
220 return c;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800221 } else
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800222 // face a broken instruction? then we stop here
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 return c;
224 }
225
226 return c;
227}
228
229// dynamicly allocate memory to contain disasm insn
230// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100231size_t cs_disasm_dyn(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800232{
233 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
234 MCInst mci;
235 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800236 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800237 cs_insn insn_cache[64];
238 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800239 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240
241 if (!handle) {
242 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800243 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244 return 0;
245 }
246
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800247 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800249 memset(insn_cache, 0, sizeof(insn_cache));
250
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600252 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800253 mci.detail = handle->detail;
254 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255
256 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
257 if (r) {
258 SStream ss;
259 SStream_Init(&ss);
260
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800261 // relative branches need to know the address & size of current insn
262 mci.insn_size = insn_size;
263 mci.address = offset;
264
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800265 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800266 // save all the information for non-detailed mode
267 mci.pub_insn.address = offset;
268 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800269 }
270
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271 handle->printer(&mci, &ss, handle->printer_info);
272
Joxean114df0e2013-12-04 07:11:32 +0100273 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800274
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800275 f++;
276
277 if (f == ARR_SIZE(insn_cache)) {
278 // resize total to contain newly disasm insns
279 total_size += sizeof(insn_cache);
280 void *tmp = realloc(total, total_size);
281 if (tmp == NULL) { // insufficient memory
282 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800283 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800284 return 0;
285 }
286
287 total = tmp;
288 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
289 // reset f back to 0
290 f = 0;
291 }
292
293 c++;
294 buffer += insn_size;
295 size -= insn_size;
296 offset += insn_size;
297
298 if (count > 0 && c == count)
299 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800300 } else {
301 // encounter a broken instruction
302 // XXX: TODO: JOXEAN continue here
303 break;
304 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 }
306
307 if (f) {
308 // resize total to contain newly disasm insns
309 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
310 if (tmp == NULL) { // insufficient memory
311 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800312 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313 return 0;
314 }
315
316 total = tmp;
317 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
318 }
319
320 *insn = total;
321
322 return c;
323}
324
325void cs_free(void *m)
326{
327 free(m);
328}
329
330// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100331const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800332{
333 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
334
335 if (!handle || handle->reg_name == NULL) {
336 return NULL;
337 }
338
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800339 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340}
341
pancakef0e4eed2013-12-11 22:14:42 +0100342const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800343{
344 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
345
346 if (!handle || handle->insn_name == NULL) {
347 return NULL;
348 }
349
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800350 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800351}
352
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800353static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354{
355 int i;
356
357 for (i = 0; i < max; i++) {
358 if (arr[i] == id)
359 return true;
360 }
361
362 return false;
363}
364
365bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
366{
367 if (!handle)
368 return false;
369
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800370 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800371}
372
373bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
374{
375 if (!handle)
376 return false;
377
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800378 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379}
380
381bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
382{
383 if (!handle)
384 return false;
385
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800386 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800387}
388
389int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
390{
391 if (!ud)
392 return -1;
393
394 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
395 unsigned int count = 0, i;
396
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800397 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800398
399 switch (handle->arch) {
400 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800401 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800402 return -1;
403 case CS_ARCH_ARM:
404 for (i = 0; i < insn->arm.op_count; i++)
405 if (insn->arm.operands[i].type == op_type)
406 count++;
407 break;
408 case CS_ARCH_ARM64:
409 for (i = 0; i < insn->arm64.op_count; i++)
410 if (insn->arm64.operands[i].type == op_type)
411 count++;
412 break;
413 case CS_ARCH_X86:
414 for (i = 0; i < insn->x86.op_count; i++)
415 if (insn->x86.operands[i].type == op_type)
416 count++;
417 break;
418 case CS_ARCH_MIPS:
419 for (i = 0; i < insn->mips.op_count; i++)
420 if (insn->mips.operands[i].type == op_type)
421 count++;
422 break;
423 }
424
425 return count;
426}
427
428int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
429 unsigned int post)
430{
431 if (!ud)
432 return -1;
433
434 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
435 unsigned int count = 0, i;
436
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800437 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438
439 switch (handle->arch) {
440 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800441 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442 return -1;
443 case CS_ARCH_ARM:
444 for (i = 0; i < insn->arm.op_count; i++) {
445 if (insn->arm.operands[i].type == op_type)
446 count++;
447 if (count == post)
448 return i;
449 }
450 break;
451 case CS_ARCH_ARM64:
452 for (i = 0; i < insn->arm64.op_count; i++) {
453 if (insn->arm64.operands[i].type == op_type)
454 count++;
455 if (count == post)
456 return i;
457 }
458 break;
459 case CS_ARCH_X86:
460 for (i = 0; i < insn->x86.op_count; i++) {
461 if (insn->x86.operands[i].type == op_type)
462 count++;
463 if (count == post)
464 return i;
465 }
466 break;
467 case CS_ARCH_MIPS:
468 for (i = 0; i < insn->mips.op_count; i++) {
469 if (insn->mips.operands[i].type == op_type)
470 count++;
471 if (count == post)
472 return i;
473 }
474 break;
475 }
476
477 return -1;
478}