blob: 70ae05e55ee793bf717fc20a0aa814a6ac25f37a [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
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800183 // find first space or tab
184 char *sp = buffer;
185 for (sp = buffer; *sp; sp++)
186 if (*sp == ' '||*sp == '\t')
187 break;
188 if (*sp) {
189 *sp = '\0';
190 strncpy(insn->op_str, sp + 1, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800191 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
192 } else
193 insn->op_str[0] = '\0';
194
195 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
196 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800197
198 // fill the instruction bytes
199 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200}
201
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800202cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800203{
204 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
205 if (!handle)
206 return CS_ERR_CSH;
207
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800208 switch (handle->arch) {
209 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800210 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800211 return CS_ERR_OPTION;
212
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800213 case CS_ARCH_X86:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800214 if (type & CS_OPT_SYNTAX) {
215 switch(value) {
216 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800217 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800218 return CS_ERR_OPTION;
219
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800220 case CS_OPT_SYNTAX_INTEL:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800221 handle->printer = X86_Intel_printInst;
222 break;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800223
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800224 case CS_OPT_SYNTAX_ATT:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800225 handle->printer = X86_ATT_printInst;
226 break;
227 }
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800228 } else {
229 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800230 return CS_ERR_OPTION;
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800231 }
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800232 break;
233 }
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800234
235 return CS_ERR_OK;
236}
237
pancakef0e4eed2013-12-11 22:14:42 +0100238size_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 +0800239{
240 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
241 MCInst mci;
242 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800243 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244
245 if (!handle) {
246 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800247 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248 return 0;
249 }
250
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800251 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252
253 while (size > 0) {
254 MCInst_Init(&mci);
255
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
261 mci.pub_insn.size = insn_size;
262 mci.pub_insn.address = offset;
263 mci.mode = handle->mode;
264 handle->printer(&mci, &ss, handle->printer_info);
265
Joxean114df0e2013-12-04 07:11:32 +0100266 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267
268 c++;
269 insn++;
270 buffer += insn_size;
271 size -= insn_size;
272 offset += insn_size;
273
274 if (count > 0) {
275 if (c == count)
276 return c;
277 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800278 } else
279 // face a broken instruction?
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280 return c;
281 }
282
283 return c;
284}
285
286// dynamicly allocate memory to contain disasm insn
287// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100288size_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 +0800289{
290 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
291 MCInst mci;
292 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800293 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800294 cs_insn insn_cache[64];
295 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800296 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297
298 if (!handle) {
299 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800300 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 return 0;
302 }
303
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800304 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305
306 while (size > 0) {
307 MCInst_Init(&mci);
308
309 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
310 if (r) {
311 SStream ss;
312 SStream_Init(&ss);
313
314 mci.pub_insn.size = insn_size;
315 mci.pub_insn.address = offset;
316 mci.mode = handle->mode;
317 handle->printer(&mci, &ss, handle->printer_info);
318
Joxean114df0e2013-12-04 07:11:32 +0100319 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320 f++;
321
322 if (f == ARR_SIZE(insn_cache)) {
323 // resize total to contain newly disasm insns
324 total_size += sizeof(insn_cache);
325 void *tmp = realloc(total, total_size);
326 if (tmp == NULL) { // insufficient memory
327 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800328 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329 return 0;
330 }
331
332 total = tmp;
333 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
334 // reset f back to 0
335 f = 0;
336 }
337
338 c++;
339 buffer += insn_size;
340 size -= insn_size;
341 offset += insn_size;
342
343 if (count > 0 && c == count)
344 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800345 } else {
346 // encounter a broken instruction
347 // XXX: TODO: JOXEAN continue here
348 break;
349 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800350 }
351
352 if (f) {
353 // resize total to contain newly disasm insns
354 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
355 if (tmp == NULL) { // insufficient memory
356 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800357 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800358 return 0;
359 }
360
361 total = tmp;
362 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
363 }
364
365 *insn = total;
366
367 return c;
368}
369
370void cs_free(void *m)
371{
372 free(m);
373}
374
375// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100376const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800377{
378 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
379
380 if (!handle || handle->reg_name == NULL) {
381 return NULL;
382 }
383
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800384 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800385}
386
pancakef0e4eed2013-12-11 22:14:42 +0100387const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800388{
389 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
390
391 if (!handle || handle->insn_name == NULL) {
392 return NULL;
393 }
394
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800395 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396}
397
398static bool arr_exist(unsigned int *arr, int max, unsigned int id)
399{
400 int i;
401
402 for (i = 0; i < max; i++) {
403 if (arr[i] == id)
404 return true;
405 }
406
407 return false;
408}
409
410bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
411{
412 if (!handle)
413 return false;
414
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800415 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416}
417
418bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
419{
420 if (!handle)
421 return false;
422
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800423 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424}
425
426bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
427{
428 if (!handle)
429 return false;
430
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800431 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432}
433
434int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
435{
436 if (!ud)
437 return -1;
438
439 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
440 unsigned int count = 0, i;
441
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800442 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800443
444 switch (handle->arch) {
445 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800446 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800447 return -1;
448 case CS_ARCH_ARM:
449 for (i = 0; i < insn->arm.op_count; i++)
450 if (insn->arm.operands[i].type == op_type)
451 count++;
452 break;
453 case CS_ARCH_ARM64:
454 for (i = 0; i < insn->arm64.op_count; i++)
455 if (insn->arm64.operands[i].type == op_type)
456 count++;
457 break;
458 case CS_ARCH_X86:
459 for (i = 0; i < insn->x86.op_count; i++)
460 if (insn->x86.operands[i].type == op_type)
461 count++;
462 break;
463 case CS_ARCH_MIPS:
464 for (i = 0; i < insn->mips.op_count; i++)
465 if (insn->mips.operands[i].type == op_type)
466 count++;
467 break;
468 }
469
470 return count;
471}
472
473int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
474 unsigned int post)
475{
476 if (!ud)
477 return -1;
478
479 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
480 unsigned int count = 0, i;
481
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800482 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483
484 switch (handle->arch) {
485 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800486 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 return -1;
488 case CS_ARCH_ARM:
489 for (i = 0; i < insn->arm.op_count; i++) {
490 if (insn->arm.operands[i].type == op_type)
491 count++;
492 if (count == post)
493 return i;
494 }
495 break;
496 case CS_ARCH_ARM64:
497 for (i = 0; i < insn->arm64.op_count; i++) {
498 if (insn->arm64.operands[i].type == op_type)
499 count++;
500 if (count == post)
501 return i;
502 }
503 break;
504 case CS_ARCH_X86:
505 for (i = 0; i < insn->x86.op_count; i++) {
506 if (insn->x86.operands[i].type == op_type)
507 count++;
508 if (count == post)
509 return i;
510 }
511 break;
512 case CS_ARCH_MIPS:
513 for (i = 0; i < insn->mips.op_count; i++) {
514 if (insn->mips.operands[i].type == op_type)
515 count++;
516 if (count == post)
517 return i;
518 }
519 break;
520 }
521
522 return -1;
523}