blob: 574b52f354d2c976eab89b120449c13eaa9fc881 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
reverserbcf09f42015-04-09 18:28:19 +01006#if defined(CAPSTONE_HAS_OSXKERNEL)
7#include <libkern/libkern.h>
8#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08009#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <stdio.h>
11#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
15#include <capstone.h>
16
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080018#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010020#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080021#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080022#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080023// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080024#define INSN_CACHE_SIZE 8
25#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080026
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080027// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080028#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080029
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080030cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080031cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
32void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080033
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034extern void ARM_enable(void);
35extern void AArch64_enable(void);
36extern void Mips_enable(void);
37extern void X86_enable(void);
38extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080039extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080040extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080041extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070042
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080044{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 static bool initialized = false;
46
47 if (initialized)
48 return;
49
danghvub33bd2c2014-01-09 12:22:56 +070050#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080051 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070052#endif
53#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070055#endif
56#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080057 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070058#endif
danghvub33bd2c2014-01-09 12:22:56 +070059#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080060 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070061#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080062#ifdef CAPSTONE_HAS_SPARC
63 Sparc_enable();
64#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080065#ifdef CAPSTONE_HAS_SYSZ
66 SystemZ_enable();
67#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080068#ifdef CAPSTONE_HAS_X86
69 X86_enable();
70#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080071#ifdef CAPSTONE_HAS_XCORE
72 XCore_enable();
73#endif
74
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080075
76 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070077}
78
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080079unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080080
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010081#ifdef CAPSTONE_USE_SYS_DYN_MEM
Pbb8741bd2015-11-10 23:02:26 +010082#ifndef CAPSTONE_HAS_OSXKERNEL
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080083cs_malloc_t cs_mem_malloc = malloc;
84cs_calloc_t cs_mem_calloc = calloc;
85cs_realloc_t cs_mem_realloc = realloc;
86cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080087cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080088#else
Pbb8741bd2015-11-10 23:02:26 +010089extern void* kern_os_malloc(size_t size);
90extern void kern_os_free(void* addr);
91extern void* kern_os_realloc(void* addr, size_t nsize);
92
93static void* cs_kern_os_calloc(size_t num, size_t size)
94{
95 return kern_os_malloc(num * size); // malloc bzeroes the buffer
96}
97
98cs_malloc_t cs_mem_malloc = kern_os_malloc;
99cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
100cs_realloc_t cs_mem_realloc = kern_os_realloc;
101cs_free_t cs_mem_free = kern_os_free;
102cs_vsnprintf_t cs_vsnprintf = vsnprintf;
103#endif
104#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800105cs_malloc_t cs_mem_malloc = NULL;
106cs_calloc_t cs_mem_calloc = NULL;
107cs_realloc_t cs_mem_realloc = NULL;
108cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800109cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800110#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800111
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800112CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800113unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800114{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800115 archs_enable();
116
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800117 if (major != NULL && minor != NULL) {
118 *major = CS_API_MAJOR;
119 *minor = CS_API_MINOR;
120 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800121
122 return (CS_API_MAJOR << 8) + CS_API_MINOR;
123}
124
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800125CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800126bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800127{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800128 archs_enable();
129
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800130 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800131 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800132 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800133 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800134 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800135
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800136 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800137 return all_arch & (1 << query);
138
139 if (query == CS_SUPPORT_DIET) {
140#ifdef CAPSTONE_DIET
141 return true;
142#else
143 return false;
144#endif
145 }
146
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800147 if (query == CS_SUPPORT_X86_REDUCE) {
148#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800149 return true;
150#else
151 return false;
152#endif
153 }
154
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800155 // unsupported query
156 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800157}
158
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800159CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800160cs_err cs_errno(csh handle)
161{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800162 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163 if (!handle)
164 return CS_ERR_CSH;
165
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100166 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800167
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800168 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800169}
170
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800171CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800172const char *cs_strerror(cs_err code)
173{
174 switch(code) {
175 default:
176 return "Unknown error code";
177 case CS_ERR_OK:
178 return "OK (CS_ERR_OK)";
179 case CS_ERR_MEM:
180 return "Out of memory (CS_ERR_MEM)";
181 case CS_ERR_ARCH:
182 return "Invalid architecture (CS_ERR_ARCH)";
183 case CS_ERR_HANDLE:
184 return "Invalid handle (CS_ERR_HANDLE)";
185 case CS_ERR_CSH:
186 return "Invalid csh (CS_ERR_CSH)";
187 case CS_ERR_MODE:
188 return "Invalid mode (CS_ERR_MODE)";
189 case CS_ERR_OPTION:
190 return "Invalid option (CS_ERR_OPTION)";
191 case CS_ERR_DETAIL:
192 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800193 case CS_ERR_MEMSETUP:
194 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800195 case CS_ERR_VERSION:
196 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800197 case CS_ERR_DIET:
198 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800199 case CS_ERR_SKIPDATA:
200 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800201 }
202}
203
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800204CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800205cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
206{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800207 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800208 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800209 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800210 // Error: before cs_open(), dynamic memory management must be initialized
211 // with cs_option(CS_OPT_MEM)
212 return CS_ERR_MEMSETUP;
213
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800214 archs_enable();
215
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800216 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800217 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800218 if (!ud) {
219 // memory insufficient
220 return CS_ERR_MEM;
221 }
222
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800223 ud->errnum = CS_ERR_OK;
224 ud->arch = arch;
225 ud->mode = mode;
226 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800227 // by default, do not break instruction into details
228 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800230 // default skipdata setup
231 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
232
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100233 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800234 if (err) {
235 cs_mem_free(ud);
236 *handle = 0;
237 return err;
238 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800239
240 *handle = (uintptr_t)ud;
241
242 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800243 } else {
244 *handle = 0;
245 return CS_ERR_ARCH;
246 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800247}
248
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800249CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800250cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800252 struct cs_struct *ud;
253
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800254 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800255 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256 return CS_ERR_CSH;
257
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100258 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800260 if (ud->printer_info)
261 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800262
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800263 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800264
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800265 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800266 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800267
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800268 // invalidate this handle by ZERO out its value.
269 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800270 *handle = 0;
271
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272 return CS_ERR_OK;
273}
274
275// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800276static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800277 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800278{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800279#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700280 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800281#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800282 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800283
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800284 // fill the instruction bytes.
285 // we might skip some redundant bytes in front in the case of X86
286 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
287 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800288
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800289 // alias instruction might have ID saved in OpcodePub
290 if (MCInst_getOpcodePub(mci))
291 insn->id = MCInst_getOpcodePub(mci);
292
293 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800294 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800295 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800296
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800297#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800298 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800299 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100300 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800301 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800302 for (sp = buffer; *sp; sp++) {
303 if (*sp == ' '|| *sp == '\t')
304 break;
305 if (*sp == '|') // lock|rep prefix for x86
306 *sp = ' ';
307 // copy to @mnemonic
308 *mnem = *sp;
309 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800310 }
311
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800312 *mnem = '\0';
313
314 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800315 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800316 // find the next non-space char
317 sp++;
318 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
319 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
321 } else
322 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800323#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324}
325
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800326// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800327// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800328static uint8_t skipdata_size(cs_struct *handle)
329{
330 switch(handle->arch) {
331 default:
332 // should never reach
333 return -1;
334 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800335 // skip 2 bytes on Thumb mode.
336 if (handle->mode & CS_MODE_THUMB)
337 return 2;
338 // otherwise, skip 4 bytes
339 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800340 case CS_ARCH_ARM64:
341 case CS_ARCH_MIPS:
342 case CS_ARCH_PPC:
343 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800344 // skip 4 bytes
345 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800346 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800347 // SystemZ instruction's length can be 2, 4 or 6 bytes,
348 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800349 return 2;
350 case CS_ARCH_X86:
351 // X86 has no restriction on instruction alignment
352 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800353 case CS_ARCH_XCORE:
354 // XCore instruction's length can be 2 or 4 bytes,
355 // so we just skip 2 bytes
356 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800357 }
358}
359
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800360CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800361cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800362{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800363 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800364 archs_enable();
365
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800366 // cs_option() can be called with NULL handle just for CS_OPT_MEM
367 // This is supposed to be executed before all other APIs (even cs_open())
368 if (type == CS_OPT_MEM) {
369 cs_opt_mem *mem = (cs_opt_mem *)value;
370
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800371 cs_mem_malloc = mem->malloc;
372 cs_mem_calloc = mem->calloc;
373 cs_mem_realloc = mem->realloc;
374 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800375 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800376
377 return CS_ERR_OK;
378 }
379
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100380 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600381 if (!handle)
382 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800383
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800384 switch(type) {
385 default:
386 break;
387 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500388 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800389 return CS_ERR_OK;
390 case CS_OPT_SKIPDATA:
391 handle->skipdata = (value == CS_OPT_ON);
392 if (handle->skipdata) {
393 if (handle->skipdata_size == 0) {
394 // set the default skipdata size
395 handle->skipdata_size = skipdata_size(handle);
396 }
397 }
398 return CS_ERR_OK;
399 case CS_OPT_SKIPDATA_SETUP:
400 if (value)
401 handle->skipdata_setup = *((cs_opt_skipdata *)value);
402 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800403 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800404
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800405 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800406}
407
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800408// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800409static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
410{
411 char *p = opstr;
412 int len;
413 size_t i;
414
415 if (!size) {
416 opstr[0] = '\0';
417 return;
418 }
419
420 len = sprintf(p, "0x%02x", buffer[0]);
421 p+= len;
422
423 for(i = 1; i < size; i++) {
424 len = sprintf(p, ", 0x%02x", buffer[i]);
425 p+= len;
426 }
427}
428
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429// dynamicly allocate memory to contain disasm insn
430// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800431CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800432size_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 +0800433{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800434 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800435 MCInst mci;
436 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800437 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800438 unsigned int f = 0; // index of the next instruction in the cache
439 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800441 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800442 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800443 void *tmp;
444 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800445 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800446 size_t size_org;
447 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800448 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500449 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800450
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800451 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800452 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800453 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800454 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455 return 0;
456 }
457
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800458 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800459
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800460#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800461 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400462 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800463#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800464
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800465 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800466 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800467 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800468 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800469
470 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700471 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800472 if (total == NULL) {
473 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500474 handle->errnum = CS_ERR_MEM;
475 return 0;
476 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800477
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700478 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800479
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800481 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800482 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700484 // relative branches need to know the address & size of current insn
485 mci.address = offset;
486
487 if (handle->detail) {
488 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700489 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700490 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700491 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700492 }
493
494 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700495 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700496 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800497#ifdef CAPSTONE_DIET
498 // zero out mnemonic & op_str
499 mci.flat_insn->mnemonic[0] = '\0';
500 mci.flat_insn->op_str[0] = '\0';
501#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700502
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800503 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800504 if (r) {
505 SStream ss;
506 SStream_Init(&ss);
507
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700508 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800509
510 // map internal instruction opcode to public insn ID
511 handle->insn_id(handle, insn_cache, mci.Opcode);
512
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800514
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700515 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800516
danghvu0d1aad12014-10-01 23:12:18 -0500517 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800518 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800519 // encounter a broken instruction
520
521 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800522 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800523 cs_mem_free(insn_cache->detail);
524 }
525
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800526 // if there is no request to skip data, or remaining data is too small,
527 // then bail out
528 if (!handle->skipdata || handle->skipdata_size > size)
529 break;
530
531 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800532 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800533 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800534 if (skipdata_bytes > size)
535 // remaining data is not enough
536 break;
537
538 if (!skipdata_bytes)
539 // user requested not to skip data, so bail out
540 break;
541 } else
542 skipdata_bytes = handle->skipdata_size;
543
544 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700545 insn_cache->id = 0; // invalid ID for this "data" instruction
546 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800547 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700548 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
549 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
550 sizeof(insn_cache->mnemonic) - 1);
551 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
552 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800553
danghvu0d1aad12014-10-01 23:12:18 -0500554 next_offset = skipdata_bytes;
555 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800556
danghvu0d1aad12014-10-01 23:12:18 -0500557 // one more instruction entering the cache
558 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800559
danghvu0d1aad12014-10-01 23:12:18 -0500560 // one more instruction disassembled
561 c++;
562 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800563 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500564 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800565
danghvu0d1aad12014-10-01 23:12:18 -0500566 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800567 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500568 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500569 total_size += (sizeof(cs_insn) * cache_size);
570 tmp = cs_mem_realloc(total, total_size);
571 if (tmp == NULL) { // insufficient memory
572 if (handle->detail) {
573 insn_cache = (cs_insn *)total;
574 for (i = 0; i < c; i++, insn_cache++)
575 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800576 }
577
danghvu0d1aad12014-10-01 23:12:18 -0500578 cs_mem_free(total);
579 *insn = NULL;
580 handle->errnum = CS_ERR_MEM;
581 return 0;
582 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800583
danghvu0d1aad12014-10-01 23:12:18 -0500584 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800585 // continue to fill in the cache after the last instruction
586 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800587
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800588 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500589 f = 0;
590 } else
591 insn_cache++;
592
593 buffer += next_offset;
594 size -= next_offset;
595 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800596 }
597
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800598 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800599 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800600 cs_mem_free(total);
601 total = NULL;
602 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800603 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -0400604 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800605 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800606 // free all detail pointers
607 if (handle->detail) {
608 insn_cache = (cs_insn *)total;
609 for (i = 0; i < c; i++, insn_cache++)
610 cs_mem_free(insn_cache->detail);
611 }
612
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800613 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800614 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800615
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800616 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800617 return 0;
618 }
619
620 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800621 }
622
623 *insn = total;
624
625 return c;
626}
627
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800628CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800629CAPSTONE_DEPRECATED
630size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
631{
632 return cs_disasm(ud, buffer, size, offset, count, insn);
633}
634
635CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800636void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800637{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800638 size_t i;
639
640 // free all detail pointers
641 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800642 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800643
644 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800645 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800646}
647
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800648CAPSTONE_EXPORT
649cs_insn *cs_malloc(csh ud)
650{
651 cs_insn *insn;
652 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
653
654 insn = cs_mem_malloc(sizeof(cs_insn));
655 if (!insn) {
656 // insufficient memory
657 handle->errnum = CS_ERR_MEM;
658 return NULL;
659 } else {
660 if (handle->detail) {
661 // allocate memory for @detail pointer
662 insn->detail = cs_mem_malloc(sizeof(cs_detail));
663 if (insn->detail == NULL) { // insufficient memory
664 cs_mem_free(insn);
665 handle->errnum = CS_ERR_MEM;
666 return NULL;
667 }
668 } else
669 insn->detail = NULL;
670 }
671
672 return insn;
673}
674
hlide993f3622014-10-05 18:14:40 +0200675// iterator for instruction "single-stepping"
676CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800677bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
678 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200679{
680 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200681 uint16_t insn_size;
682 MCInst mci;
683 bool r;
684
685 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800686 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800687 return false;
hlide993f3622014-10-05 18:14:40 +0200688 }
689
690 handle->errnum = CS_ERR_OK;
691
hlide993f3622014-10-05 18:14:40 +0200692 MCInst_Init(&mci);
693 mci.csh = handle;
694
695 // relative branches need to know the address & size of current insn
696 mci.address = *address;
697
698 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800699 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200700 mci.flat_insn->address = *address;
701#ifdef CAPSTONE_DIET
702 // zero out mnemonic & op_str
703 mci.flat_insn->mnemonic[0] = '\0';
704 mci.flat_insn->op_str[0] = '\0';
705#endif
706
707 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800708 if (r) {
hlide993f3622014-10-05 18:14:40 +0200709 SStream ss;
710 SStream_Init(&ss);
711
712 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800713
714 // map internal instruction opcode to public insn ID
715 handle->insn_id(handle, insn, mci.Opcode);
716
hlide993f3622014-10-05 18:14:40 +0200717 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800718
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800719 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800720
hlide993f3622014-10-05 18:14:40 +0200721 *code += insn_size;
722 *size -= insn_size;
723 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800724 } else { // encounter a broken instruction
725 size_t skipdata_bytes;
726
727 // if there is no request to skip data, or remaining data is too small,
728 // then bail out
729 if (!handle->skipdata || handle->skipdata_size > *size)
730 return false;
731
732 if (handle->skipdata_setup.callback) {
733 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
734 0, handle->skipdata_setup.user_data);
735 if (skipdata_bytes > *size)
736 // remaining data is not enough
737 return false;
738
739 if (!skipdata_bytes)
740 // user requested not to skip data, so bail out
741 return false;
742 } else
743 skipdata_bytes = handle->skipdata_size;
744
745 // we have to skip some amount of data, depending on arch & mode
746 insn->id = 0; // invalid ID for this "data" instruction
747 insn->address = *address;
748 insn->size = (uint16_t)skipdata_bytes;
749 memcpy(insn->bytes, *code, skipdata_bytes);
750 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
751 sizeof(insn->mnemonic) - 1);
752 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
753
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800754 *code += skipdata_bytes;
755 *size -= skipdata_bytes;
756 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200757 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800758
759 return true;
hlide993f3622014-10-05 18:14:40 +0200760}
761
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800762// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800763CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100764const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800765{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800766 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800767
768 if (!handle || handle->reg_name == NULL) {
769 return NULL;
770 }
771
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800772 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800773}
774
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800775CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100776const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800777{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800778 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800779
780 if (!handle || handle->insn_name == NULL) {
781 return NULL;
782 }
783
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800784 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800785}
786
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800787CAPSTONE_EXPORT
788const char *cs_group_name(csh ud, unsigned int group)
789{
790 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
791
792 if (!handle || handle->group_name == NULL) {
793 return NULL;
794 }
795
796 return handle->group_name(ud, group);
797}
798
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800799static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800{
801 int i;
802
803 for (i = 0; i < max; i++) {
804 if (arr[i] == id)
805 return true;
806 }
807
808 return false;
809}
810
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800811CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200812bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800813{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800814 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800815 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800816 return false;
817
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100818 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200819
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800820 if (!handle->detail) {
821 handle->errnum = CS_ERR_DETAIL;
822 return false;
823 }
824
Giovanni Condello95657e02014-05-07 17:31:27 +0200825 if(!insn->id) {
826 handle->errnum = CS_ERR_SKIPDATA;
827 return false;
828 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200829
Giovanni Condello95657e02014-05-07 17:31:27 +0200830 if(!insn->detail) {
831 handle->errnum = CS_ERR_DETAIL;
832 return false;
833 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200834
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800835 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800836}
837
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800838CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200839bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800840{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800841 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800842 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800843 return false;
844
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100845 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200846
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800847 if (!handle->detail) {
848 handle->errnum = CS_ERR_DETAIL;
849 return false;
850 }
851
Giovanni Condello95657e02014-05-07 17:31:27 +0200852 if(!insn->id) {
853 handle->errnum = CS_ERR_SKIPDATA;
854 return false;
855 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200856
Giovanni Condello95657e02014-05-07 17:31:27 +0200857 if(!insn->detail) {
858 handle->errnum = CS_ERR_DETAIL;
859 return false;
860 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200861
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800862 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800863}
864
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800865CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200866bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800867{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800868 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800869 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800870 return false;
871
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100872 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200873
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800874 if (!handle->detail) {
875 handle->errnum = CS_ERR_DETAIL;
876 return false;
877 }
878
Giovanni Condello95657e02014-05-07 17:31:27 +0200879 if(!insn->id) {
880 handle->errnum = CS_ERR_SKIPDATA;
881 return false;
882 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200883
Giovanni Condello95657e02014-05-07 17:31:27 +0200884 if(!insn->detail) {
885 handle->errnum = CS_ERR_DETAIL;
886 return false;
887 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200888
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800889 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800890}
891
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800892CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200893int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800894{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800895 struct cs_struct *handle;
896 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800897 if (!ud)
898 return -1;
899
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100900 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200901
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800902 if (!handle->detail) {
903 handle->errnum = CS_ERR_DETAIL;
904 return -1;
905 }
906
Giovanni Condello95657e02014-05-07 17:31:27 +0200907 if(!insn->id) {
908 handle->errnum = CS_ERR_SKIPDATA;
909 return -1;
910 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200911
Giovanni Condello95657e02014-05-07 17:31:27 +0200912 if(!insn->detail) {
913 handle->errnum = CS_ERR_DETAIL;
914 return -1;
915 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200916
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800917 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800918
919 switch (handle->arch) {
920 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800921 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800922 return -1;
923 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800924 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800925 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800926 count++;
927 break;
928 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800929 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800930 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800931 count++;
932 break;
933 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800934 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800935 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936 count++;
937 break;
938 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800939 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800940 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800941 count++;
942 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800943 case CS_ARCH_PPC:
944 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800945 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800946 count++;
947 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800948 case CS_ARCH_SPARC:
949 for (i = 0; i < insn->detail->sparc.op_count; i++)
950 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
951 count++;
952 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800953 case CS_ARCH_SYSZ:
954 for (i = 0; i < insn->detail->sysz.op_count; i++)
955 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
956 count++;
957 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800958 case CS_ARCH_XCORE:
959 for (i = 0; i < insn->detail->xcore.op_count; i++)
960 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
961 count++;
962 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963 }
964
965 return count;
966}
967
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800968CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200969int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800970 unsigned int post)
971{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800972 struct cs_struct *handle;
973 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800974 if (!ud)
975 return -1;
976
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100977 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200978
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800979 if (!handle->detail) {
980 handle->errnum = CS_ERR_DETAIL;
981 return -1;
982 }
983
Giovanni Condello95657e02014-05-07 17:31:27 +0200984 if(!insn->id) {
985 handle->errnum = CS_ERR_SKIPDATA;
986 return -1;
987 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200988
Giovanni Condello95657e02014-05-07 17:31:27 +0200989 if(!insn->detail) {
990 handle->errnum = CS_ERR_DETAIL;
991 return -1;
992 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200993
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800994 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800995
996 switch (handle->arch) {
997 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800998 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800999 return -1;
1000 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001001 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001002 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001003 count++;
1004 if (count == post)
1005 return i;
1006 }
1007 break;
1008 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001009 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001010 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001011 count++;
1012 if (count == post)
1013 return i;
1014 }
1015 break;
1016 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001017 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001018 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001019 count++;
1020 if (count == post)
1021 return i;
1022 }
1023 break;
1024 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001025 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001026 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001027 count++;
1028 if (count == post)
1029 return i;
1030 }
1031 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001032 case CS_ARCH_PPC:
1033 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001034 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001035 count++;
1036 if (count == post)
1037 return i;
1038 }
1039 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001040 case CS_ARCH_SPARC:
1041 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1042 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1043 count++;
1044 if (count == post)
1045 return i;
1046 }
1047 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001048 case CS_ARCH_SYSZ:
1049 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1050 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1051 count++;
1052 if (count == post)
1053 return i;
1054 }
1055 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001056 case CS_ARCH_XCORE:
1057 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1058 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1059 count++;
1060 if (count == post)
1061 return i;
1062 }
1063 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001064 }
1065
1066 return -1;
1067}