blob: b517f4e8b4f6016a536cf0eaad54e6444f101b3c [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
112 if (ud->mode & CS_MODE_MICRO)
113 ud->micro_mips = true;
114
115 break;
116 }
117 case CS_ARCH_ARM64: {
118 MCRegisterInfo *mri = malloc(sizeof(*mri));
119
120 AArch64_init(mri);
121 ud->printer = AArch64_printInst;
122 ud->printer_info = mri;
123 ud->getinsn_info = mri;
124 ud->disasm = AArch64_getInstruction;
125 ud->reg_name = AArch64_reg_name;
126 ud->insn_id = AArch64_get_insn_id;
127 ud->insn_name = AArch64_insn_name;
128 ud->post_printer = AArch64_post_printer;
129 break;
130 }
131 default: // unsupported architecture
132 free(ud);
133 return CS_ERR_ARCH;
134 }
135
136 *handle = (uintptr_t)ud;
137
138 return CS_ERR_OK;
139}
140
141cs_err cs_close(csh handle)
142{
143 if (!handle)
144 return CS_ERR_CSH;
145
146 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
147
148 switch (ud->arch) {
149 case CS_ARCH_X86:
150 break;
151 case CS_ARCH_ARM:
152 case CS_ARCH_MIPS:
153 case CS_ARCH_ARM64:
154 free(ud->printer_info);
155 break;
156 default: // unsupported architecture
157 return CS_ERR_HANDLE;
158 }
159
160 memset(ud, 0, sizeof(*ud));
161 free(ud);
162
163 return CS_ERR_OK;
164}
165
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800166#define MIN(x, y) ((x) < (y) ? (x) : (y))
167
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168// fill insn with mnemonic & operands info
169static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Joxean114df0e2013-12-04 07:11:32 +0100170 PostPrinter_t printer, unsigned char *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171{
172 memcpy(insn, &mci->pub_insn, sizeof(*insn));
173
174 // map internal instruction opcode to public insn ID
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800175 if (handle->insn_id)
176 handle->insn_id(insn, MCInst_getOpcode(mci));
177
178 // alias instruction might have ID saved in OpcodePub
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800179 if (MCInst_getOpcodePub(mci))
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800180 insn->id = MCInst_getOpcodePub(mci);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181
182 if (printer)
183 printer(insn->id, insn, buffer);
184
185 // fill in mnemonic & operands
186 char *tab = strchr(buffer, '\t');
187 if (tab) {
188 *tab = '\0';
189 strncpy(insn->op_str, tab + 1, sizeof(insn->op_str) - 1);
190 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
191 } else
192 insn->op_str[0] = '\0';
193
194 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
195 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800196
197 // fill the instruction bytes
198 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199}
200
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800201cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800202{
203 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
204 if (!handle)
205 return CS_ERR_CSH;
206
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800207 switch (handle->arch) {
208 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800209 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800210 return CS_ERR_OPTION;
211
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800212 case CS_ARCH_X86:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800213 if (type & CS_OPT_SYNTAX) {
214 switch(value) {
215 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800216 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800217 return CS_ERR_OPTION;
218
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800219 case CS_OPT_SYNTAX_INTEL:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800220 handle->printer = X86_Intel_printInst;
221 break;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800222
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800223 case CS_OPT_SYNTAX_ATT:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800224 handle->printer = X86_ATT_printInst;
225 break;
226 }
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800227 } else {
228 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800229 return CS_ERR_OPTION;
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800230 }
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800231 break;
232 }
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800233
234 return CS_ERR_OK;
235}
236
pancakec04f8732013-12-03 02:51:46 +0100237size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, uint64_t offset, size_t count, cs_insn *insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800238{
239 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
240 MCInst mci;
241 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800242 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800243
244 if (!handle) {
245 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800246 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800247 return 0;
248 }
249
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800250 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251
252 while (size > 0) {
253 MCInst_Init(&mci);
254
255 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
256 if (r) {
257 SStream ss;
258 SStream_Init(&ss);
259
260 mci.pub_insn.size = insn_size;
261 mci.pub_insn.address = offset;
262 mci.mode = handle->mode;
263 handle->printer(&mci, &ss, handle->printer_info);
264
Joxean114df0e2013-12-04 07:11:32 +0100265 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266
267 c++;
268 insn++;
269 buffer += insn_size;
270 size -= insn_size;
271 offset += insn_size;
272
273 if (count > 0) {
274 if (c == count)
275 return c;
276 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800277 } else
278 // face a broken instruction?
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279 return c;
280 }
281
282 return c;
283}
284
285// dynamicly allocate memory to contain disasm insn
286// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakec04f8732013-12-03 02:51:46 +0100287size_t cs_disasm_dyn(csh ud, unsigned char *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288{
289 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
290 MCInst mci;
291 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800292 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800293 cs_insn insn_cache[64];
294 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800295 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296
297 if (!handle) {
298 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800299 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300 return 0;
301 }
302
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800303 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304
305 while (size > 0) {
306 MCInst_Init(&mci);
307
308 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
309 if (r) {
310 SStream ss;
311 SStream_Init(&ss);
312
313 mci.pub_insn.size = insn_size;
314 mci.pub_insn.address = offset;
315 mci.mode = handle->mode;
316 handle->printer(&mci, &ss, handle->printer_info);
317
Joxean114df0e2013-12-04 07:11:32 +0100318 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 f++;
320
321 if (f == ARR_SIZE(insn_cache)) {
322 // resize total to contain newly disasm insns
323 total_size += sizeof(insn_cache);
324 void *tmp = realloc(total, total_size);
325 if (tmp == NULL) { // insufficient memory
326 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800327 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328 return 0;
329 }
330
331 total = tmp;
332 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
333 // reset f back to 0
334 f = 0;
335 }
336
337 c++;
338 buffer += insn_size;
339 size -= insn_size;
340 offset += insn_size;
341
342 if (count > 0 && c == count)
343 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800344 } else {
345 // encounter a broken instruction
346 // XXX: TODO: JOXEAN continue here
347 break;
348 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349 }
350
351 if (f) {
352 // resize total to contain newly disasm insns
353 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
354 if (tmp == NULL) { // insufficient memory
355 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800356 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357 return 0;
358 }
359
360 total = tmp;
361 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
362 }
363
364 *insn = total;
365
366 return c;
367}
368
369void cs_free(void *m)
370{
371 free(m);
372}
373
374// return friendly name of regiser in a string
375char *cs_reg_name(csh ud, unsigned int reg)
376{
377 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
378
379 if (!handle || handle->reg_name == NULL) {
380 return NULL;
381 }
382
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800383 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800384}
385
386char *cs_insn_name(csh ud, unsigned int insn)
387{
388 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
389
390 if (!handle || handle->insn_name == NULL) {
391 return NULL;
392 }
393
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800394 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395}
396
397static bool arr_exist(unsigned int *arr, int max, unsigned int id)
398{
399 int i;
400
401 for (i = 0; i < max; i++) {
402 if (arr[i] == id)
403 return true;
404 }
405
406 return false;
407}
408
409bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
410{
411 if (!handle)
412 return false;
413
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800414 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415}
416
417bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
418{
419 if (!handle)
420 return false;
421
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800422 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423}
424
425bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
426{
427 if (!handle)
428 return false;
429
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800430 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431}
432
433int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
434{
435 if (!ud)
436 return -1;
437
438 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
439 unsigned int count = 0, i;
440
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800441 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442
443 switch (handle->arch) {
444 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800445 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446 return -1;
447 case CS_ARCH_ARM:
448 for (i = 0; i < insn->arm.op_count; i++)
449 if (insn->arm.operands[i].type == op_type)
450 count++;
451 break;
452 case CS_ARCH_ARM64:
453 for (i = 0; i < insn->arm64.op_count; i++)
454 if (insn->arm64.operands[i].type == op_type)
455 count++;
456 break;
457 case CS_ARCH_X86:
458 for (i = 0; i < insn->x86.op_count; i++)
459 if (insn->x86.operands[i].type == op_type)
460 count++;
461 break;
462 case CS_ARCH_MIPS:
463 for (i = 0; i < insn->mips.op_count; i++)
464 if (insn->mips.operands[i].type == op_type)
465 count++;
466 break;
467 }
468
469 return count;
470}
471
472int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
473 unsigned int post)
474{
475 if (!ud)
476 return -1;
477
478 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
479 unsigned int count = 0, i;
480
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800481 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482
483 switch (handle->arch) {
484 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800485 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800486 return -1;
487 case CS_ARCH_ARM:
488 for (i = 0; i < insn->arm.op_count; i++) {
489 if (insn->arm.operands[i].type == op_type)
490 count++;
491 if (count == post)
492 return i;
493 }
494 break;
495 case CS_ARCH_ARM64:
496 for (i = 0; i < insn->arm64.op_count; i++) {
497 if (insn->arm64.operands[i].type == op_type)
498 count++;
499 if (count == post)
500 return i;
501 }
502 break;
503 case CS_ARCH_X86:
504 for (i = 0; i < insn->x86.op_count; i++) {
505 if (insn->x86.operands[i].type == op_type)
506 count++;
507 if (count == post)
508 return i;
509 }
510 break;
511 case CS_ARCH_MIPS:
512 for (i = 0; i < insn->mips.op_count; i++) {
513 if (insn->mips.operands[i].type == op_type)
514 count++;
515 if (count == post)
516 return i;
517 }
518 break;
519 }
520
521 return -1;
522}