blob: 71ed9d8c13b87cb029121e582515f16d9996bb5d [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
13#include "arch/X86/X86Disassembler.h"
14#include "arch/X86/X86InstPrinter.h"
15#include "arch/X86/mapping.h"
16
17#include "arch/ARM/ARMDisassembler.h"
18#include "arch/ARM/ARMInstPrinter.h"
19#include "arch/ARM/mapping.h"
20
21#include "arch/Mips/MipsDisassembler.h"
22#include "arch/Mips/MipsInstPrinter.h"
23#include "arch/Mips/mapping.h"
24
25#include "arch/AArch64/AArch64Disassembler.h"
26#include "arch/AArch64/AArch64InstPrinter.h"
27#include "arch/AArch64/mapping.h"
28
29#include "utils.h"
30
Nguyen Anh Quynh5dbe12a2013-12-03 12:27:46 +080031// Package version
32#define PKG_MAJOR 1
33#define PKG_MINOR 0
34
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080035
36void cs_version(int *major, int *minor)
37{
38 *major = CS_API_MAJOR;
39 *minor = CS_API_MINOR;
40}
41
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080042cs_err cs_errno(csh handle)
43{
44 if (!handle)
45 return CS_ERR_CSH;
46
47 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
48
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080049 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050}
51
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080052cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
53{
54 cs_struct *ud;
55
56 ud = calloc(1, sizeof(*ud));
57 if (!ud) {
58 // memory insufficient
59 return CS_ERR_MEM;
60 }
61
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080062 ud->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080063 ud->arch = arch;
64 ud->mode = mode;
65 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
66 ud->reg_name = NULL;
67
68 switch (ud->arch) {
69 case CS_ARCH_X86:
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080070 // by default, we use Intel syntax
71 ud->printer = X86_Intel_printInst;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080072 ud->printer_info = NULL;
73 ud->disasm = X86_getInstruction;
74 ud->reg_name = X86_reg_name;
75 ud->insn_id = X86_get_insn_id;
76 ud->insn_name = X86_insn_name;
77 break;
78 case CS_ARCH_ARM: {
79 MCRegisterInfo *mri = malloc(sizeof(*mri));
80
81 ARM_init(mri);
82
83 ud->printer = ARM_printInst;
84 ud->printer_info = mri;
85 ud->reg_name = ARM_reg_name;
86 ud->insn_id = ARM_get_insn_id;
87 ud->insn_name = ARM_insn_name;
88 ud->post_printer = ARM_post_printer;
89
90 if (ud->mode & CS_MODE_THUMB)
91 ud->disasm = Thumb_getInstruction;
92 else
93 ud->disasm = ARM_getInstruction;
94 break;
95 }
96 case CS_ARCH_MIPS: {
97 MCRegisterInfo *mri = malloc(sizeof(*mri));
98
99 Mips_init(mri);
100 ud->printer = Mips_printInst;
101 ud->printer_info = mri;
102 ud->getinsn_info = mri;
103 ud->reg_name = Mips_reg_name;
104 ud->insn_id = Mips_get_insn_id;
105 ud->insn_name = Mips_insn_name;
106
107 if (ud->mode & CS_MODE_32)
108 ud->disasm = Mips_getInstruction;
109 else
110 ud->disasm = Mips64_getInstruction;
111
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800112 break;
113 }
114 case CS_ARCH_ARM64: {
115 MCRegisterInfo *mri = malloc(sizeof(*mri));
116
117 AArch64_init(mri);
118 ud->printer = AArch64_printInst;
119 ud->printer_info = mri;
120 ud->getinsn_info = mri;
121 ud->disasm = AArch64_getInstruction;
122 ud->reg_name = AArch64_reg_name;
123 ud->insn_id = AArch64_get_insn_id;
124 ud->insn_name = AArch64_insn_name;
125 ud->post_printer = AArch64_post_printer;
126 break;
127 }
128 default: // unsupported architecture
129 free(ud);
130 return CS_ERR_ARCH;
131 }
132
133 *handle = (uintptr_t)ud;
134
135 return CS_ERR_OK;
136}
137
138cs_err cs_close(csh handle)
139{
140 if (!handle)
141 return CS_ERR_CSH;
142
143 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
144
145 switch (ud->arch) {
146 case CS_ARCH_X86:
147 break;
148 case CS_ARCH_ARM:
149 case CS_ARCH_MIPS:
150 case CS_ARCH_ARM64:
151 free(ud->printer_info);
152 break;
153 default: // unsupported architecture
154 return CS_ERR_HANDLE;
155 }
156
157 memset(ud, 0, sizeof(*ud));
158 free(ud);
159
160 return CS_ERR_OK;
161}
162
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800163#define MIN(x, y) ((x) < (y) ? (x) : (y))
164
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800165// fill insn with mnemonic & operands info
166static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
pancakef0e4eed2013-12-11 22:14:42 +0100167 PostPrinter_t printer, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168{
169 memcpy(insn, &mci->pub_insn, sizeof(*insn));
170
171 // map internal instruction opcode to public insn ID
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800172 if (handle->insn_id)
173 handle->insn_id(insn, MCInst_getOpcode(mci));
174
175 // alias instruction might have ID saved in OpcodePub
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800176 if (MCInst_getOpcodePub(mci))
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800177 insn->id = MCInst_getOpcodePub(mci);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178
179 if (printer)
180 printer(insn->id, insn, buffer);
181
182 // fill in mnemonic & operands
183 char *tab = strchr(buffer, '\t');
184 if (tab) {
185 *tab = '\0';
186 strncpy(insn->op_str, tab + 1, sizeof(insn->op_str) - 1);
187 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
188 } else
189 insn->op_str[0] = '\0';
190
191 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
192 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800193
194 // fill the instruction bytes
195 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196}
197
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800198cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800199{
200 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
201 if (!handle)
202 return CS_ERR_CSH;
203
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800204 switch (handle->arch) {
205 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800206 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800207 return CS_ERR_OPTION;
208
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800209 case CS_ARCH_X86:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800210 if (type & CS_OPT_SYNTAX) {
211 switch(value) {
212 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800213 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800214 return CS_ERR_OPTION;
215
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800216 case CS_OPT_SYNTAX_INTEL:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800217 handle->printer = X86_Intel_printInst;
218 break;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800219
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800220 case CS_OPT_SYNTAX_ATT:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800221 handle->printer = X86_ATT_printInst;
222 break;
223 }
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800224 } else {
225 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800226 return CS_ERR_OPTION;
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800227 }
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800228 break;
229 }
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800230
231 return CS_ERR_OK;
232}
233
pancakef0e4eed2013-12-11 22:14:42 +0100234size_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 +0800235{
236 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
237 MCInst mci;
238 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800239 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240
241 if (!handle) {
242 // FIXME: 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
249 while (size > 0) {
250 MCInst_Init(&mci);
251
252 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
253 if (r) {
254 SStream ss;
255 SStream_Init(&ss);
256
257 mci.pub_insn.size = insn_size;
258 mci.pub_insn.address = offset;
259 mci.mode = handle->mode;
260 handle->printer(&mci, &ss, handle->printer_info);
261
Joxean114df0e2013-12-04 07:11:32 +0100262 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263
264 c++;
265 insn++;
266 buffer += insn_size;
267 size -= insn_size;
268 offset += insn_size;
269
270 if (count > 0) {
271 if (c == count)
272 return c;
273 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800274 } else
275 // face a broken instruction?
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276 return c;
277 }
278
279 return c;
280}
281
282// dynamicly allocate memory to contain disasm insn
283// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100284size_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 +0800285{
286 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
287 MCInst mci;
288 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800289 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800290 cs_insn insn_cache[64];
291 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800292 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800293
294 if (!handle) {
295 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800296 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297 return 0;
298 }
299
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800300 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301
302 while (size > 0) {
303 MCInst_Init(&mci);
304
305 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
306 if (r) {
307 SStream ss;
308 SStream_Init(&ss);
309
310 mci.pub_insn.size = insn_size;
311 mci.pub_insn.address = offset;
312 mci.mode = handle->mode;
313 handle->printer(&mci, &ss, handle->printer_info);
314
Joxean114df0e2013-12-04 07:11:32 +0100315 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316 f++;
317
318 if (f == ARR_SIZE(insn_cache)) {
319 // resize total to contain newly disasm insns
320 total_size += sizeof(insn_cache);
321 void *tmp = realloc(total, total_size);
322 if (tmp == NULL) { // insufficient memory
323 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800324 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325 return 0;
326 }
327
328 total = tmp;
329 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
330 // reset f back to 0
331 f = 0;
332 }
333
334 c++;
335 buffer += insn_size;
336 size -= insn_size;
337 offset += insn_size;
338
339 if (count > 0 && c == count)
340 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800341 } else {
342 // encounter a broken instruction
343 // XXX: TODO: JOXEAN continue here
344 break;
345 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346 }
347
348 if (f) {
349 // resize total to contain newly disasm insns
350 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
351 if (tmp == NULL) { // insufficient memory
352 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800353 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354 return 0;
355 }
356
357 total = tmp;
358 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
359 }
360
361 *insn = total;
362
363 return c;
364}
365
366void cs_free(void *m)
367{
368 free(m);
369}
370
371// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100372const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800373{
374 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
375
376 if (!handle || handle->reg_name == NULL) {
377 return NULL;
378 }
379
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800380 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381}
382
pancakef0e4eed2013-12-11 22:14:42 +0100383const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800384{
385 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
386
387 if (!handle || handle->insn_name == NULL) {
388 return NULL;
389 }
390
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800391 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800392}
393
394static bool arr_exist(unsigned int *arr, int max, unsigned int id)
395{
396 int i;
397
398 for (i = 0; i < max; i++) {
399 if (arr[i] == id)
400 return true;
401 }
402
403 return false;
404}
405
406bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
407{
408 if (!handle)
409 return false;
410
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800411 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412}
413
414bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
415{
416 if (!handle)
417 return false;
418
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800419 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800420}
421
422bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
423{
424 if (!handle)
425 return false;
426
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800427 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428}
429
430int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
431{
432 if (!ud)
433 return -1;
434
435 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
436 unsigned int count = 0, i;
437
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800438 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439
440 switch (handle->arch) {
441 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800442 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800443 return -1;
444 case CS_ARCH_ARM:
445 for (i = 0; i < insn->arm.op_count; i++)
446 if (insn->arm.operands[i].type == op_type)
447 count++;
448 break;
449 case CS_ARCH_ARM64:
450 for (i = 0; i < insn->arm64.op_count; i++)
451 if (insn->arm64.operands[i].type == op_type)
452 count++;
453 break;
454 case CS_ARCH_X86:
455 for (i = 0; i < insn->x86.op_count; i++)
456 if (insn->x86.operands[i].type == op_type)
457 count++;
458 break;
459 case CS_ARCH_MIPS:
460 for (i = 0; i < insn->mips.op_count; i++)
461 if (insn->mips.operands[i].type == op_type)
462 count++;
463 break;
464 }
465
466 return count;
467}
468
469int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
470 unsigned int post)
471{
472 if (!ud)
473 return -1;
474
475 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
476 unsigned int count = 0, i;
477
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800478 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479
480 switch (handle->arch) {
481 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800482 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483 return -1;
484 case CS_ARCH_ARM:
485 for (i = 0; i < insn->arm.op_count; i++) {
486 if (insn->arm.operands[i].type == op_type)
487 count++;
488 if (count == post)
489 return i;
490 }
491 break;
492 case CS_ARCH_ARM64:
493 for (i = 0; i < insn->arm64.op_count; i++) {
494 if (insn->arm64.operands[i].type == op_type)
495 count++;
496 if (count == post)
497 return i;
498 }
499 break;
500 case CS_ARCH_X86:
501 for (i = 0; i < insn->x86.op_count; i++) {
502 if (insn->x86.operands[i].type == op_type)
503 count++;
504 if (count == post)
505 return i;
506 }
507 break;
508 case CS_ARCH_MIPS:
509 for (i = 0; i < insn->mips.op_count; i++) {
510 if (insn->mips.operands[i].type == op_type)
511 count++;
512 if (count == post)
513 return i;
514 }
515 break;
516 }
517
518 return -1;
519}