blob: 0c6e31c7665f91988928512014ec1500969c90e7 [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
danghvu629a6d82013-12-20 01:44:17 -060013#include "module.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include "utils.h"
15
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080016void cs_version(int *major, int *minor)
17{
18 *major = CS_API_MAJOR;
19 *minor = CS_API_MINOR;
20}
21
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022cs_err cs_errno(csh handle)
23{
24 if (!handle)
25 return CS_ERR_CSH;
26
27 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
28
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080029 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080030}
31
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080032cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
33{
danghvu2b192962013-12-19 22:40:28 -060034 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035
danghvu2b192962013-12-19 22:40:28 -060036 ud = calloc(1, sizeof(*ud));
37 if (!ud) {
38 // memory insufficient
39 return CS_ERR_MEM;
40 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080041
danghvu2b192962013-12-19 22:40:28 -060042 ud->errnum = CS_ERR_OK;
43 ud->arch = arch;
44 ud->mode = mode;
45 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
46 ud->reg_name = NULL;
47 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080048
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +080049 if (init_arch[ud->arch])
50 init_arch[ud->arch](ud);
danghvu0b6ea042013-12-19 23:07:26 -060051 else
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +080052 return CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080053
danghvu2b192962013-12-19 22:40:28 -060054 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055
danghvu2b192962013-12-19 22:40:28 -060056 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080057}
58
59cs_err cs_close(csh handle)
60{
61 if (!handle)
62 return CS_ERR_CSH;
63
64 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
65
66 switch (ud->arch) {
67 case CS_ARCH_X86:
68 break;
69 case CS_ARCH_ARM:
70 case CS_ARCH_MIPS:
71 case CS_ARCH_ARM64:
72 free(ud->printer_info);
73 break;
74 default: // unsupported architecture
75 return CS_ERR_HANDLE;
76 }
77
78 memset(ud, 0, sizeof(*ud));
79 free(ud);
80
81 return CS_ERR_OK;
82}
83
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +080084#define MIN(x, y) ((x) < (y) ? (x) : (y))
85
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080086// fill insn with mnemonic & operands info
87static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
pancakef0e4eed2013-12-11 22:14:42 +010088 PostPrinter_t printer, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080090 if (handle->detail) {
91 memcpy(insn, &mci->pub_insn, sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080092
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080093 // fill the instruction bytes
94 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +080095
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080096 } else {
97 insn->address = mci->address;
98 insn->size = mci->insn_size;
99 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800100
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800101 // map internal instruction opcode to public insn ID
102 if (handle->insn_id)
103 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
104
105 // alias instruction might have ID saved in OpcodePub
106 if (MCInst_getOpcodePub(mci))
107 insn->id = MCInst_getOpcodePub(mci);
108
109 // post printer handles some corner cases (hacky)
110 if (printer)
111 printer((csh)handle, insn, buffer);
112
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800113 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800114 // find first space or tab
115 char *sp = buffer;
116 for (sp = buffer; *sp; sp++)
117 if (*sp == ' '||*sp == '\t')
118 break;
119 if (*sp) {
120 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800121 // find the next non-space char
122 sp++;
123 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
124 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
126 } else
127 insn->op_str[0] = '\0';
128
129 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
130 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
131}
132
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800133cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800134{
danghvu2b192962013-12-19 22:40:28 -0600135 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
136 if (!handle)
137 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800138
danghvu0b6ea042013-12-19 23:07:26 -0600139 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800140 handle->detail = value;
141 return CS_ERR_OK;
142 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800143
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800144 if (option_arch[handle->arch])
danghvu0b6ea042013-12-19 23:07:26 -0600145 return option_arch[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800146
danghvu2b192962013-12-19 22:40:28 -0600147 return CS_ERR_OK;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800148}
149
pancakef0e4eed2013-12-11 22:14:42 +0100150size_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 +0800151{
152 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
153 MCInst mci;
154 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800155 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800156
157 if (!handle) {
158 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800159 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800160 return 0;
161 }
162
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800163 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800164 memset(insn, 0, count * sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800165
166 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600167 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800168 mci.detail = handle->detail;
169 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
171 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
172 if (r) {
173 SStream ss;
174 SStream_Init(&ss);
175
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800176 // relative branches need to know the address & size of current insn
177 mci.insn_size = insn_size;
178 mci.address = offset;
179
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800180 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800181 // save all the information for non-detailed mode
182 mci.pub_insn.address = offset;
183 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800184 }
185
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800186 handle->printer(&mci, &ss, handle->printer_info);
187
Joxean114df0e2013-12-04 07:11:32 +0100188 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189
190 c++;
191 insn++;
192 buffer += insn_size;
193 size -= insn_size;
194 offset += insn_size;
195
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800196 if (c == count)
197 return c;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800198 } else
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800199 // face a broken instruction? then we stop here
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200 return c;
201 }
202
203 return c;
204}
205
206// dynamicly allocate memory to contain disasm insn
207// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100208size_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 +0800209{
210 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
211 MCInst mci;
212 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800213 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214 cs_insn insn_cache[64];
215 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800216 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800217
218 if (!handle) {
219 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800220 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221 return 0;
222 }
223
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800224 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800226 memset(insn_cache, 0, sizeof(insn_cache));
227
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600229 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800230 mci.detail = handle->detail;
231 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800232
233 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
234 if (r) {
235 SStream ss;
236 SStream_Init(&ss);
237
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800238 // relative branches need to know the address & size of current insn
239 mci.insn_size = insn_size;
240 mci.address = offset;
241
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800242 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800243 // save all the information for non-detailed mode
244 mci.pub_insn.address = offset;
245 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800246 }
247
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248 handle->printer(&mci, &ss, handle->printer_info);
249
Joxean114df0e2013-12-04 07:11:32 +0100250 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800251
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252 f++;
253
254 if (f == ARR_SIZE(insn_cache)) {
255 // resize total to contain newly disasm insns
256 total_size += sizeof(insn_cache);
257 void *tmp = realloc(total, total_size);
258 if (tmp == NULL) { // insufficient memory
259 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800260 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261 return 0;
262 }
263
264 total = tmp;
265 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
266 // reset f back to 0
267 f = 0;
268 }
269
270 c++;
271 buffer += insn_size;
272 size -= insn_size;
273 offset += insn_size;
274
275 if (count > 0 && c == count)
276 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800277 } else {
278 // encounter a broken instruction
279 // XXX: TODO: JOXEAN continue here
280 break;
281 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 }
283
284 if (f) {
285 // resize total to contain newly disasm insns
286 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
287 if (tmp == NULL) { // insufficient memory
288 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800289 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800290 return 0;
291 }
292
293 total = tmp;
294 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
295 }
296
297 *insn = total;
298
299 return c;
300}
301
302void cs_free(void *m)
303{
304 free(m);
305}
306
307// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100308const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800309{
310 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
311
312 if (!handle || handle->reg_name == NULL) {
313 return NULL;
314 }
315
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800316 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317}
318
pancakef0e4eed2013-12-11 22:14:42 +0100319const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320{
321 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
322
323 if (!handle || handle->insn_name == NULL) {
324 return NULL;
325 }
326
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800327 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328}
329
330static bool arr_exist(unsigned int *arr, int max, unsigned int id)
331{
332 int i;
333
334 for (i = 0; i < max; i++) {
335 if (arr[i] == id)
336 return true;
337 }
338
339 return false;
340}
341
342bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
343{
344 if (!handle)
345 return false;
346
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800347 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348}
349
350bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
351{
352 if (!handle)
353 return false;
354
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800355 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356}
357
358bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
359{
360 if (!handle)
361 return false;
362
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800363 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800364}
365
366int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
367{
368 if (!ud)
369 return -1;
370
371 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
372 unsigned int count = 0, i;
373
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800374 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800375
376 switch (handle->arch) {
377 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800378 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379 return -1;
380 case CS_ARCH_ARM:
381 for (i = 0; i < insn->arm.op_count; i++)
382 if (insn->arm.operands[i].type == op_type)
383 count++;
384 break;
385 case CS_ARCH_ARM64:
386 for (i = 0; i < insn->arm64.op_count; i++)
387 if (insn->arm64.operands[i].type == op_type)
388 count++;
389 break;
390 case CS_ARCH_X86:
391 for (i = 0; i < insn->x86.op_count; i++)
392 if (insn->x86.operands[i].type == op_type)
393 count++;
394 break;
395 case CS_ARCH_MIPS:
396 for (i = 0; i < insn->mips.op_count; i++)
397 if (insn->mips.operands[i].type == op_type)
398 count++;
399 break;
400 }
401
402 return count;
403}
404
405int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
406 unsigned int post)
407{
408 if (!ud)
409 return -1;
410
411 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
412 unsigned int count = 0, i;
413
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800414 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415
416 switch (handle->arch) {
417 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800418 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419 return -1;
420 case CS_ARCH_ARM:
421 for (i = 0; i < insn->arm.op_count; i++) {
422 if (insn->arm.operands[i].type == op_type)
423 count++;
424 if (count == post)
425 return i;
426 }
427 break;
428 case CS_ARCH_ARM64:
429 for (i = 0; i < insn->arm64.op_count; i++) {
430 if (insn->arm64.operands[i].type == op_type)
431 count++;
432 if (count == post)
433 return i;
434 }
435 break;
436 case CS_ARCH_X86:
437 for (i = 0; i < insn->x86.op_count; i++) {
438 if (insn->x86.operands[i].type == op_type)
439 count++;
440 if (count == post)
441 return i;
442 }
443 break;
444 case CS_ARCH_MIPS:
445 for (i = 0; i < insn->mips.op_count; i++) {
446 if (insn->mips.operands[i].type == op_type)
447 count++;
448 if (count == post)
449 return i;
450 }
451 break;
452 }
453
454 return -1;
455}