blob: 8de123ae59ff2a1be2a76cf9f42a2f4a6f91e6fe [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 Quynhf1851802013-12-21 12:16:47 +080015void (*init_arch[MAX_ARCH]) (cs_struct *);
16cs_err (*option_arch[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value);
17
18
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080019void cs_version(int *major, int *minor)
20{
21 *major = CS_API_MAJOR;
22 *minor = CS_API_MINOR;
23}
24
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025cs_err cs_errno(csh handle)
26{
27 if (!handle)
28 return CS_ERR_CSH;
29
30 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
31
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080032 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080033}
34
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
36{
danghvu2b192962013-12-19 22:40:28 -060037 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038
danghvu2b192962013-12-19 22:40:28 -060039 ud = calloc(1, sizeof(*ud));
40 if (!ud) {
41 // memory insufficient
42 return CS_ERR_MEM;
43 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080044
danghvu2b192962013-12-19 22:40:28 -060045 ud->errnum = CS_ERR_OK;
46 ud->arch = arch;
47 ud->mode = mode;
48 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
49 ud->reg_name = NULL;
50 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080051
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +080052 if (init_arch[ud->arch])
53 init_arch[ud->arch](ud);
danghvu0b6ea042013-12-19 23:07:26 -060054 else
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +080055 return CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080056
danghvu2b192962013-12-19 22:40:28 -060057 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058
danghvu2b192962013-12-19 22:40:28 -060059 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060}
61
62cs_err cs_close(csh handle)
63{
64 if (!handle)
65 return CS_ERR_CSH;
66
67 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
68
69 switch (ud->arch) {
70 case CS_ARCH_X86:
71 break;
72 case CS_ARCH_ARM:
73 case CS_ARCH_MIPS:
74 case CS_ARCH_ARM64:
75 free(ud->printer_info);
76 break;
77 default: // unsupported architecture
78 return CS_ERR_HANDLE;
79 }
80
81 memset(ud, 0, sizeof(*ud));
82 free(ud);
83
84 return CS_ERR_OK;
85}
86
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +080087#define MIN(x, y) ((x) < (y) ? (x) : (y))
88
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089// fill insn with mnemonic & operands info
90static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
pancakef0e4eed2013-12-11 22:14:42 +010091 PostPrinter_t printer, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080092{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080093 if (handle->detail) {
94 memcpy(insn, &mci->pub_insn, sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080095
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080096 // fill the instruction bytes
97 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +080098
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080099 } else {
100 insn->address = mci->address;
101 insn->size = mci->insn_size;
102 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800103
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800104 // map internal instruction opcode to public insn ID
105 if (handle->insn_id)
106 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
107
108 // alias instruction might have ID saved in OpcodePub
109 if (MCInst_getOpcodePub(mci))
110 insn->id = MCInst_getOpcodePub(mci);
111
112 // post printer handles some corner cases (hacky)
113 if (printer)
114 printer((csh)handle, insn, buffer);
115
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800116 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800117 // find first space or tab
118 char *sp = buffer;
119 for (sp = buffer; *sp; sp++)
120 if (*sp == ' '||*sp == '\t')
121 break;
122 if (*sp) {
123 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800124 // find the next non-space char
125 sp++;
126 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
127 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800128 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
129 } else
130 insn->op_str[0] = '\0';
131
132 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
133 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
134}
135
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800136cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800137{
danghvu2b192962013-12-19 22:40:28 -0600138 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
139 if (!handle)
140 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800141
danghvu0b6ea042013-12-19 23:07:26 -0600142 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800143 handle->detail = value;
144 return CS_ERR_OK;
145 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800146
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800147 if (option_arch[handle->arch])
danghvu0b6ea042013-12-19 23:07:26 -0600148 return option_arch[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800149
danghvu2b192962013-12-19 22:40:28 -0600150 return CS_ERR_OK;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800151}
152
pancakef0e4eed2013-12-11 22:14:42 +0100153size_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 +0800154{
155 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
156 MCInst mci;
157 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800158 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800159
160 if (!handle) {
161 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800162 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163 return 0;
164 }
165
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800166 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800167 memset(insn, 0, count * sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168
169 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600170 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800171 mci.detail = handle->detail;
172 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173
174 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
175 if (r) {
176 SStream ss;
177 SStream_Init(&ss);
178
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800179 // relative branches need to know the address & size of current insn
180 mci.insn_size = insn_size;
181 mci.address = offset;
182
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800183 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800184 // save all the information for non-detailed mode
185 mci.pub_insn.address = offset;
186 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800187 }
188
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189 handle->printer(&mci, &ss, handle->printer_info);
190
Joxean114df0e2013-12-04 07:11:32 +0100191 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800192
193 c++;
194 insn++;
195 buffer += insn_size;
196 size -= insn_size;
197 offset += insn_size;
198
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800199 if (c == count)
200 return c;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800201 } else
Nguyen Anh Quynh9a0dbab2013-12-15 22:25:58 +0800202 // face a broken instruction? then we stop here
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203 return c;
204 }
205
206 return c;
207}
208
209// dynamicly allocate memory to contain disasm insn
210// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100211size_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 +0800212{
213 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
214 MCInst mci;
215 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800216 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800217 cs_insn insn_cache[64];
218 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800219 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220
221 if (!handle) {
222 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800223 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224 return 0;
225 }
226
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800227 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800229 memset(insn_cache, 0, sizeof(insn_cache));
230
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600232 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800233 mci.detail = handle->detail;
234 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235
236 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
237 if (r) {
238 SStream ss;
239 SStream_Init(&ss);
240
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800241 // relative branches need to know the address & size of current insn
242 mci.insn_size = insn_size;
243 mci.address = offset;
244
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800245 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800246 // save all the information for non-detailed mode
247 mci.pub_insn.address = offset;
248 mci.pub_insn.size = insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800249 }
250
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251 handle->printer(&mci, &ss, handle->printer_info);
252
Joxean114df0e2013-12-04 07:11:32 +0100253 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800254
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255 f++;
256
257 if (f == ARR_SIZE(insn_cache)) {
258 // resize total to contain newly disasm insns
259 total_size += sizeof(insn_cache);
260 void *tmp = realloc(total, total_size);
261 if (tmp == NULL) { // insufficient memory
262 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800263 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800264 return 0;
265 }
266
267 total = tmp;
268 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
269 // reset f back to 0
270 f = 0;
271 }
272
273 c++;
274 buffer += insn_size;
275 size -= insn_size;
276 offset += insn_size;
277
278 if (count > 0 && c == count)
279 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800280 } else {
281 // encounter a broken instruction
282 // XXX: TODO: JOXEAN continue here
283 break;
284 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285 }
286
287 if (f) {
288 // resize total to contain newly disasm insns
289 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
290 if (tmp == NULL) { // insufficient memory
291 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800292 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800293 return 0;
294 }
295
296 total = tmp;
297 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
298 }
299
300 *insn = total;
301
302 return c;
303}
304
305void cs_free(void *m)
306{
307 free(m);
308}
309
310// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100311const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800312{
313 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
314
315 if (!handle || handle->reg_name == NULL) {
316 return NULL;
317 }
318
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800319 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320}
321
pancakef0e4eed2013-12-11 22:14:42 +0100322const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800323{
324 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
325
326 if (!handle || handle->insn_name == NULL) {
327 return NULL;
328 }
329
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800330 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800331}
332
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800333static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334{
335 int i;
336
337 for (i = 0; i < max; i++) {
338 if (arr[i] == id)
339 return true;
340 }
341
342 return false;
343}
344
345bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
346{
347 if (!handle)
348 return false;
349
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800350 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800351}
352
353bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
354{
355 if (!handle)
356 return false;
357
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800358 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800359}
360
361bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
362{
363 if (!handle)
364 return false;
365
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800366 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367}
368
369int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
370{
371 if (!ud)
372 return -1;
373
374 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
375 unsigned int count = 0, i;
376
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800377 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800378
379 switch (handle->arch) {
380 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800381 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800382 return -1;
383 case CS_ARCH_ARM:
384 for (i = 0; i < insn->arm.op_count; i++)
385 if (insn->arm.operands[i].type == op_type)
386 count++;
387 break;
388 case CS_ARCH_ARM64:
389 for (i = 0; i < insn->arm64.op_count; i++)
390 if (insn->arm64.operands[i].type == op_type)
391 count++;
392 break;
393 case CS_ARCH_X86:
394 for (i = 0; i < insn->x86.op_count; i++)
395 if (insn->x86.operands[i].type == op_type)
396 count++;
397 break;
398 case CS_ARCH_MIPS:
399 for (i = 0; i < insn->mips.op_count; i++)
400 if (insn->mips.operands[i].type == op_type)
401 count++;
402 break;
403 }
404
405 return count;
406}
407
408int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
409 unsigned int post)
410{
411 if (!ud)
412 return -1;
413
414 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
415 unsigned int count = 0, i;
416
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800417 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418
419 switch (handle->arch) {
420 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800421 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800422 return -1;
423 case CS_ARCH_ARM:
424 for (i = 0; i < insn->arm.op_count; i++) {
425 if (insn->arm.operands[i].type == op_type)
426 count++;
427 if (count == post)
428 return i;
429 }
430 break;
431 case CS_ARCH_ARM64:
432 for (i = 0; i < insn->arm64.op_count; i++) {
433 if (insn->arm64.operands[i].type == op_type)
434 count++;
435 if (count == post)
436 return i;
437 }
438 break;
439 case CS_ARCH_X86:
440 for (i = 0; i < insn->x86.op_count; i++) {
441 if (insn->x86.operands[i].type == op_type)
442 count++;
443 if (count == post)
444 return i;
445 }
446 break;
447 case CS_ARCH_MIPS:
448 for (i = 0; i < insn->mips.op_count; i++) {
449 if (insn->mips.operands[i].type == op_type)
450 count++;
451 if (count == post)
452 return i;
453 }
454 break;
455 }
456
457 return -1;
458}