blob: 6e2d1122614d02527dde9cf968080f9a2773f165 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
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
reverser160e1982015-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>
reverser160e1982015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010015#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016
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
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080082cs_malloc_t cs_mem_malloc = malloc;
83cs_calloc_t cs_mem_calloc = calloc;
84cs_realloc_t cs_mem_realloc = realloc;
85cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080086cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080087#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080088cs_malloc_t cs_mem_malloc = NULL;
89cs_calloc_t cs_mem_calloc = NULL;
90cs_realloc_t cs_mem_realloc = NULL;
91cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080092cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080093#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080094
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +080095CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080096unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080097{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080098 archs_enable();
99
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800100 if (major != NULL && minor != NULL) {
101 *major = CS_API_MAJOR;
102 *minor = CS_API_MINOR;
103 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800104
105 return (CS_API_MAJOR << 8) + CS_API_MINOR;
106}
107
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800108CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800109bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800110{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800111 archs_enable();
112
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800113 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800114 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800115 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800116 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800117 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800118
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800119 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800120 return all_arch & (1 << query);
121
122 if (query == CS_SUPPORT_DIET) {
123#ifdef CAPSTONE_DIET
124 return true;
125#else
126 return false;
127#endif
128 }
129
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800130 if (query == CS_SUPPORT_X86_REDUCE) {
131#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800132 return true;
133#else
134 return false;
135#endif
136 }
137
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800138 // unsupported query
139 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800140}
141
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800142CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800143cs_err cs_errno(csh handle)
144{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800145 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800146 if (!handle)
147 return CS_ERR_CSH;
148
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100149 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800150
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800151 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800152}
153
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800154CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800155const char *cs_strerror(cs_err code)
156{
157 switch(code) {
158 default:
159 return "Unknown error code";
160 case CS_ERR_OK:
161 return "OK (CS_ERR_OK)";
162 case CS_ERR_MEM:
163 return "Out of memory (CS_ERR_MEM)";
164 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800165 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800166 case CS_ERR_HANDLE:
167 return "Invalid handle (CS_ERR_HANDLE)";
168 case CS_ERR_CSH:
169 return "Invalid csh (CS_ERR_CSH)";
170 case CS_ERR_MODE:
171 return "Invalid mode (CS_ERR_MODE)";
172 case CS_ERR_OPTION:
173 return "Invalid option (CS_ERR_OPTION)";
174 case CS_ERR_DETAIL:
175 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800176 case CS_ERR_MEMSETUP:
177 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800178 case CS_ERR_VERSION:
179 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800180 case CS_ERR_DIET:
181 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800182 case CS_ERR_SKIPDATA:
183 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800184 }
185}
186
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800187CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800188cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
189{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800190 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800191 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800192 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800193 // Error: before cs_open(), dynamic memory management must be initialized
194 // with cs_option(CS_OPT_MEM)
195 return CS_ERR_MEMSETUP;
196
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800197 archs_enable();
198
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800199 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800200 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800201 if (!ud) {
202 // memory insufficient
203 return CS_ERR_MEM;
204 }
205
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800206 ud->errnum = CS_ERR_OK;
207 ud->arch = arch;
208 ud->mode = mode;
209 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800210 // by default, do not break instruction into details
211 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800213 // default skipdata setup
214 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
215
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100216 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800217 if (err) {
218 cs_mem_free(ud);
219 *handle = 0;
220 return err;
221 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800222
223 *handle = (uintptr_t)ud;
224
225 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800226 } else {
227 *handle = 0;
228 return CS_ERR_ARCH;
229 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800230}
231
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800232CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800233cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800235 struct cs_struct *ud;
236
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800237 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800238 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239 return CS_ERR_CSH;
240
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100241 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800242
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800243 if (ud->printer_info)
244 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800246 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800247
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800248 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800249 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800250
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800251 // invalidate this handle by ZERO out its value.
252 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800253 *handle = 0;
254
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255 return CS_ERR_OK;
256}
257
258// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800259static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800260 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800262#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700263 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800264#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800265 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800267 // fill the instruction bytes.
268 // we might skip some redundant bytes in front in the case of X86
269 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
270 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800271
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800272 // alias instruction might have ID saved in OpcodePub
273 if (MCInst_getOpcodePub(mci))
274 insn->id = MCInst_getOpcodePub(mci);
275
276 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800277 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800278 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800279
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800280#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800282 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100283 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800284 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800285 for (sp = buffer; *sp; sp++) {
286 if (*sp == ' '|| *sp == '\t')
287 break;
288 if (*sp == '|') // lock|rep prefix for x86
289 *sp = ' ';
290 // copy to @mnemonic
291 *mnem = *sp;
292 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800293 }
294
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800295 *mnem = '\0';
296
297 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800298 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800299 // find the next non-space char
300 sp++;
301 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
302 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
304 } else
305 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800306#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307}
308
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800309// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800310// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800311static uint8_t skipdata_size(cs_struct *handle)
312{
313 switch(handle->arch) {
314 default:
315 // should never reach
316 return -1;
317 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800318 // skip 2 bytes on Thumb mode.
319 if (handle->mode & CS_MODE_THUMB)
320 return 2;
321 // otherwise, skip 4 bytes
322 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800323 case CS_ARCH_ARM64:
324 case CS_ARCH_MIPS:
325 case CS_ARCH_PPC:
326 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800327 // skip 4 bytes
328 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800329 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800330 // SystemZ instruction's length can be 2, 4 or 6 bytes,
331 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800332 return 2;
333 case CS_ARCH_X86:
334 // X86 has no restriction on instruction alignment
335 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800336 case CS_ARCH_XCORE:
337 // XCore instruction's length can be 2 or 4 bytes,
338 // so we just skip 2 bytes
339 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800340 }
341}
342
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800343CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800344cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800345{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800346 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800347 archs_enable();
348
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800349 // cs_option() can be called with NULL handle just for CS_OPT_MEM
350 // This is supposed to be executed before all other APIs (even cs_open())
351 if (type == CS_OPT_MEM) {
352 cs_opt_mem *mem = (cs_opt_mem *)value;
353
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800354 cs_mem_malloc = mem->malloc;
355 cs_mem_calloc = mem->calloc;
356 cs_mem_realloc = mem->realloc;
357 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800358 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800359
360 return CS_ERR_OK;
361 }
362
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100363 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600364 if (!handle)
365 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800366
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800367 switch(type) {
368 default:
369 break;
370 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500371 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800372 return CS_ERR_OK;
373 case CS_OPT_SKIPDATA:
374 handle->skipdata = (value == CS_OPT_ON);
375 if (handle->skipdata) {
376 if (handle->skipdata_size == 0) {
377 // set the default skipdata size
378 handle->skipdata_size = skipdata_size(handle);
379 }
380 }
381 return CS_ERR_OK;
382 case CS_OPT_SKIPDATA_SETUP:
383 if (value)
384 handle->skipdata_setup = *((cs_opt_skipdata *)value);
385 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800386 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800387
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800388 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800389}
390
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800391// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800392static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
393{
394 char *p = opstr;
395 int len;
396 size_t i;
397
398 if (!size) {
399 opstr[0] = '\0';
400 return;
401 }
402
403 len = sprintf(p, "0x%02x", buffer[0]);
404 p+= len;
405
406 for(i = 1; i < size; i++) {
407 len = sprintf(p, ", 0x%02x", buffer[i]);
408 p+= len;
409 }
410}
411
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412// dynamicly allocate memory to contain disasm insn
413// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800414CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800415size_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 +0800416{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800417 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418 MCInst mci;
419 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800420 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800421 unsigned int f = 0; // index of the next instruction in the cache
422 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800424 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800425 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800426 void *tmp;
427 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800428 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800429 size_t size_org;
430 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800431 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500432 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800434 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800435 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800437 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438 return 0;
439 }
440
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800441 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800443#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800444 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400445 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800446#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800447
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800448 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800449 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800450 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800451 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800452
453 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700454 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800455 if (total == NULL) {
456 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500457 handle->errnum = CS_ERR_MEM;
458 return 0;
459 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800460
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700461 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800462
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800463 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800464 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800465 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700467 // relative branches need to know the address & size of current insn
468 mci.address = offset;
469
470 if (handle->detail) {
471 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700472 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700473 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700474 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700475 }
476
477 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700478 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700479 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800480#ifdef CAPSTONE_DIET
481 // zero out mnemonic & op_str
482 mci.flat_insn->mnemonic[0] = '\0';
483 mci.flat_insn->op_str[0] = '\0';
484#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700485
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800486 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 if (r) {
488 SStream ss;
489 SStream_Init(&ss);
490
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700491 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800492
493 // map internal instruction opcode to public insn ID
494 handle->insn_id(handle, insn_cache, mci.Opcode);
495
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800497
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700498 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800499
danghvu0d1aad12014-10-01 23:12:18 -0500500 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800501 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800502 // encounter a broken instruction
503
504 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800505 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800506 cs_mem_free(insn_cache->detail);
507 }
508
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800509 // if there is no request to skip data, or remaining data is too small,
510 // then bail out
511 if (!handle->skipdata || handle->skipdata_size > size)
512 break;
513
514 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800515 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800516 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800517 if (skipdata_bytes > size)
518 // remaining data is not enough
519 break;
520
521 if (!skipdata_bytes)
522 // user requested not to skip data, so bail out
523 break;
524 } else
525 skipdata_bytes = handle->skipdata_size;
526
527 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700528 insn_cache->id = 0; // invalid ID for this "data" instruction
529 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800530 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700531 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
532 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
533 sizeof(insn_cache->mnemonic) - 1);
534 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
535 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800536
danghvu0d1aad12014-10-01 23:12:18 -0500537 next_offset = skipdata_bytes;
538 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800539
danghvu0d1aad12014-10-01 23:12:18 -0500540 // one more instruction entering the cache
541 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800542
danghvu0d1aad12014-10-01 23:12:18 -0500543 // one more instruction disassembled
544 c++;
545 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800546 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500547 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800548
danghvu0d1aad12014-10-01 23:12:18 -0500549 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800550 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500551 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500552 total_size += (sizeof(cs_insn) * cache_size);
553 tmp = cs_mem_realloc(total, total_size);
554 if (tmp == NULL) { // insufficient memory
555 if (handle->detail) {
556 insn_cache = (cs_insn *)total;
557 for (i = 0; i < c; i++, insn_cache++)
558 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800559 }
560
danghvu0d1aad12014-10-01 23:12:18 -0500561 cs_mem_free(total);
562 *insn = NULL;
563 handle->errnum = CS_ERR_MEM;
564 return 0;
565 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800566
danghvu0d1aad12014-10-01 23:12:18 -0500567 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800568 // continue to fill in the cache after the last instruction
569 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800570
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800571 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500572 f = 0;
573 } else
574 insn_cache++;
575
576 buffer += next_offset;
577 size -= next_offset;
578 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 }
580
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800581 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800582 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800583 cs_mem_free(total);
584 total = NULL;
585 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800586 // total did not fully use the last cache, so downsize it
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800587 void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800589 // free all detail pointers
590 if (handle->detail) {
591 insn_cache = (cs_insn *)total;
592 for (i = 0; i < c; i++, insn_cache++)
593 cs_mem_free(insn_cache->detail);
594 }
595
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800596 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800597 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800598
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800599 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800600 return 0;
601 }
602
603 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800604 }
605
606 *insn = total;
607
608 return c;
609}
610
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800611CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800612CAPSTONE_DEPRECATED
613size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
614{
615 return cs_disasm(ud, buffer, size, offset, count, insn);
616}
617
618CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800619void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800620{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800621 size_t i;
622
623 // free all detail pointers
624 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800625 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800626
627 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800628 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800629}
630
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800631CAPSTONE_EXPORT
632cs_insn *cs_malloc(csh ud)
633{
634 cs_insn *insn;
635 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
636
637 insn = cs_mem_malloc(sizeof(cs_insn));
638 if (!insn) {
639 // insufficient memory
640 handle->errnum = CS_ERR_MEM;
641 return NULL;
642 } else {
643 if (handle->detail) {
644 // allocate memory for @detail pointer
645 insn->detail = cs_mem_malloc(sizeof(cs_detail));
646 if (insn->detail == NULL) { // insufficient memory
647 cs_mem_free(insn);
648 handle->errnum = CS_ERR_MEM;
649 return NULL;
650 }
651 } else
652 insn->detail = NULL;
653 }
654
655 return insn;
656}
657
hlide993f3622014-10-05 18:14:40 +0200658// iterator for instruction "single-stepping"
659CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800660bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
661 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200662{
663 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200664 uint16_t insn_size;
665 MCInst mci;
666 bool r;
667
668 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800669 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800670 return false;
hlide993f3622014-10-05 18:14:40 +0200671 }
672
673 handle->errnum = CS_ERR_OK;
674
hlide993f3622014-10-05 18:14:40 +0200675 MCInst_Init(&mci);
676 mci.csh = handle;
677
678 // relative branches need to know the address & size of current insn
679 mci.address = *address;
680
681 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800682 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200683 mci.flat_insn->address = *address;
684#ifdef CAPSTONE_DIET
685 // zero out mnemonic & op_str
686 mci.flat_insn->mnemonic[0] = '\0';
687 mci.flat_insn->op_str[0] = '\0';
688#endif
689
690 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800691 if (r) {
hlide993f3622014-10-05 18:14:40 +0200692 SStream ss;
693 SStream_Init(&ss);
694
695 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800696
697 // map internal instruction opcode to public insn ID
698 handle->insn_id(handle, insn, mci.Opcode);
699
hlide993f3622014-10-05 18:14:40 +0200700 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800701
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800702 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800703
hlide993f3622014-10-05 18:14:40 +0200704 *code += insn_size;
705 *size -= insn_size;
706 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800707 } else { // encounter a broken instruction
708 size_t skipdata_bytes;
709
710 // if there is no request to skip data, or remaining data is too small,
711 // then bail out
712 if (!handle->skipdata || handle->skipdata_size > *size)
713 return false;
714
715 if (handle->skipdata_setup.callback) {
716 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
717 0, handle->skipdata_setup.user_data);
718 if (skipdata_bytes > *size)
719 // remaining data is not enough
720 return false;
721
722 if (!skipdata_bytes)
723 // user requested not to skip data, so bail out
724 return false;
725 } else
726 skipdata_bytes = handle->skipdata_size;
727
728 // we have to skip some amount of data, depending on arch & mode
729 insn->id = 0; // invalid ID for this "data" instruction
730 insn->address = *address;
731 insn->size = (uint16_t)skipdata_bytes;
732 memcpy(insn->bytes, *code, skipdata_bytes);
733 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
734 sizeof(insn->mnemonic) - 1);
735 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
736
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800737 *code += skipdata_bytes;
738 *size -= skipdata_bytes;
739 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200740 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800741
742 return true;
hlide993f3622014-10-05 18:14:40 +0200743}
744
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800745// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800746CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100747const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800748{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800749 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800750
751 if (!handle || handle->reg_name == NULL) {
752 return NULL;
753 }
754
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800755 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800756}
757
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800758CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100759const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800760{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800761 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800762
763 if (!handle || handle->insn_name == NULL) {
764 return NULL;
765 }
766
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800767 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800768}
769
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800770CAPSTONE_EXPORT
771const char *cs_group_name(csh ud, unsigned int group)
772{
773 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
774
775 if (!handle || handle->group_name == NULL) {
776 return NULL;
777 }
778
779 return handle->group_name(ud, group);
780}
781
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800782CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200783bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800784{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800785 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800786 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800787 return false;
788
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100789 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200790
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800791 if (!handle->detail) {
792 handle->errnum = CS_ERR_DETAIL;
793 return false;
794 }
795
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800796 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200797 handle->errnum = CS_ERR_SKIPDATA;
798 return false;
799 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200800
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800801 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200802 handle->errnum = CS_ERR_DETAIL;
803 return false;
804 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200805
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800806 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800807}
808
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800809CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200810bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800811{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800812 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800813 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800814 return false;
815
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100816 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200817
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800818 if (!handle->detail) {
819 handle->errnum = CS_ERR_DETAIL;
820 return false;
821 }
822
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800823 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200824 handle->errnum = CS_ERR_SKIPDATA;
825 return false;
826 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200827
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800828 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200829 handle->errnum = CS_ERR_DETAIL;
830 return false;
831 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200832
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800833 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800834}
835
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800836CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200837bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800838{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800839 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800840 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800841 return false;
842
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100843 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200844
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800845 if (!handle->detail) {
846 handle->errnum = CS_ERR_DETAIL;
847 return false;
848 }
849
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800850 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200851 handle->errnum = CS_ERR_SKIPDATA;
852 return false;
853 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200854
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800855 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200856 handle->errnum = CS_ERR_DETAIL;
857 return false;
858 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200859
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800860 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800861}
862
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800863CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200864int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800865{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800866 struct cs_struct *handle;
867 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800868 if (!ud)
869 return -1;
870
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100871 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200872
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800873 if (!handle->detail) {
874 handle->errnum = CS_ERR_DETAIL;
875 return -1;
876 }
877
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800878 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200879 handle->errnum = CS_ERR_SKIPDATA;
880 return -1;
881 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200882
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800883 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200884 handle->errnum = CS_ERR_DETAIL;
885 return -1;
886 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200887
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800888 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800889
890 switch (handle->arch) {
891 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800892 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800893 return -1;
894 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800895 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800896 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800897 count++;
898 break;
899 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800900 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800901 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800902 count++;
903 break;
904 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800905 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800906 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800907 count++;
908 break;
909 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800910 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800911 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800912 count++;
913 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800914 case CS_ARCH_PPC:
915 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800916 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800917 count++;
918 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800919 case CS_ARCH_SPARC:
920 for (i = 0; i < insn->detail->sparc.op_count; i++)
921 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
922 count++;
923 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800924 case CS_ARCH_SYSZ:
925 for (i = 0; i < insn->detail->sysz.op_count; i++)
926 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
927 count++;
928 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800929 case CS_ARCH_XCORE:
930 for (i = 0; i < insn->detail->xcore.op_count; i++)
931 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
932 count++;
933 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800934 }
935
936 return count;
937}
938
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800939CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200940int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800941 unsigned int post)
942{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800943 struct cs_struct *handle;
944 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800945 if (!ud)
946 return -1;
947
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100948 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200949
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800950 if (!handle->detail) {
951 handle->errnum = CS_ERR_DETAIL;
952 return -1;
953 }
954
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800955 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200956 handle->errnum = CS_ERR_SKIPDATA;
957 return -1;
958 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200959
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800960 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200961 handle->errnum = CS_ERR_DETAIL;
962 return -1;
963 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200964
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800965 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800966
967 switch (handle->arch) {
968 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800969 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800970 return -1;
971 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800972 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800973 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800974 count++;
975 if (count == post)
976 return i;
977 }
978 break;
979 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800980 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800981 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800982 count++;
983 if (count == post)
984 return i;
985 }
986 break;
987 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800988 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800989 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990 count++;
991 if (count == post)
992 return i;
993 }
994 break;
995 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800996 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800997 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800998 count++;
999 if (count == post)
1000 return i;
1001 }
1002 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001003 case CS_ARCH_PPC:
1004 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001005 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001006 count++;
1007 if (count == post)
1008 return i;
1009 }
1010 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001011 case CS_ARCH_SPARC:
1012 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1013 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1014 count++;
1015 if (count == post)
1016 return i;
1017 }
1018 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001019 case CS_ARCH_SYSZ:
1020 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1021 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1022 count++;
1023 if (count == post)
1024 return i;
1025 }
1026 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001027 case CS_ARCH_XCORE:
1028 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1029 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1030 count++;
1031 if (count == post)
1032 return i;
1033 }
1034 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001035 }
1036
1037 return -1;
1038}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001039
1040CAPSTONE_EXPORT
1041cs_err cs_regs_access(csh ud, const cs_insn *insn,
1042 cs_regs regs_read, uint8_t *regs_read_count,
1043 cs_regs regs_write, uint8_t *regs_write_count)
1044{
1045 struct cs_struct *handle;
1046
1047 if (!ud)
1048 return -1;
1049
1050 handle = (struct cs_struct *)(uintptr_t)ud;
1051
1052#ifdef CAPSTONE_DIET
1053 // This API does not work in DIET mode
1054 handle->errnum = CS_ERR_DIET;
1055 return CS_ERR_DIET;
1056#else
1057 if (!handle->detail) {
1058 handle->errnum = CS_ERR_DETAIL;
1059 return CS_ERR_DETAIL;
1060 }
1061
1062 if (!insn->id) {
1063 handle->errnum = CS_ERR_SKIPDATA;
1064 return CS_ERR_SKIPDATA;
1065 }
1066
1067 if (!insn->detail) {
1068 handle->errnum = CS_ERR_DETAIL;
1069 return CS_ERR_DETAIL;
1070 }
1071
1072 if (handle->reg_access) {
1073 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1074 } else {
1075 // this arch is unsupported yet
1076 handle->errnum = CS_ERR_ARCH;
1077 return CS_ERR_ARCH;
1078 }
1079
1080 return CS_ERR_OK;
1081#endif
1082}