blob: 302450898d541dcd6c816d6f6cf9a67c8e8cfcb7 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080013#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080014#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080015#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080016// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#define INSN_CACHE_SIZE 8
18#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080019
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080020cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080021cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
22void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080023
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080024extern void ARM_enable(void);
25extern void AArch64_enable(void);
26extern void Mips_enable(void);
27extern void X86_enable(void);
28extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080029extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080030extern void SystemZ_enable(void);
danghvu701b8502014-01-09 11:06:44 +070031
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080032static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080033{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034 static bool initialized = false;
35
36 if (initialized)
37 return;
38
danghvub33bd2c2014-01-09 12:22:56 +070039#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070041#endif
42#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070044#endif
45#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
danghvub33bd2c2014-01-09 12:22:56 +070048#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070050#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080051#ifdef CAPSTONE_HAS_SPARC
52 Sparc_enable();
53#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080054#ifdef CAPSTONE_HAS_SYSZ
55 SystemZ_enable();
56#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080057#ifdef CAPSTONE_HAS_X86
58 X86_enable();
59#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080060
61 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070062}
63
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080064unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080065
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080066#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080067cs_malloc_t cs_mem_malloc = malloc;
68cs_calloc_t cs_mem_calloc = calloc;
69cs_realloc_t cs_mem_realloc = realloc;
70cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080071cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080072#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080073cs_malloc_t cs_mem_malloc = NULL;
74cs_calloc_t cs_mem_calloc = NULL;
75cs_realloc_t cs_mem_realloc = NULL;
76cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080077cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080078#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080079
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080080unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080081{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080082 archs_enable();
83
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080084 if (major != NULL && minor != NULL) {
85 *major = CS_API_MAJOR;
86 *minor = CS_API_MINOR;
87 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080088
89 return (CS_API_MAJOR << 8) + CS_API_MINOR;
90}
91
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080092bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080093{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080094 archs_enable();
95
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080096 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080097 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080098 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080099 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
100 (1 << CS_ARCH_SYSZ));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800101
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800102 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800103 return all_arch & (1 << query);
104
105 if (query == CS_SUPPORT_DIET) {
106#ifdef CAPSTONE_DIET
107 return true;
108#else
109 return false;
110#endif
111 }
112
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800113 if (query == CS_SUPPORT_X86_REDUCE) {
114#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800115 return true;
116#else
117 return false;
118#endif
119 }
120
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800121 // unsupported query
122 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800123}
124
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125cs_err cs_errno(csh handle)
126{
127 if (!handle)
128 return CS_ERR_CSH;
129
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800130 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800131
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800132 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800133}
134
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800135const char *cs_strerror(cs_err code)
136{
137 switch(code) {
138 default:
139 return "Unknown error code";
140 case CS_ERR_OK:
141 return "OK (CS_ERR_OK)";
142 case CS_ERR_MEM:
143 return "Out of memory (CS_ERR_MEM)";
144 case CS_ERR_ARCH:
145 return "Invalid architecture (CS_ERR_ARCH)";
146 case CS_ERR_HANDLE:
147 return "Invalid handle (CS_ERR_HANDLE)";
148 case CS_ERR_CSH:
149 return "Invalid csh (CS_ERR_CSH)";
150 case CS_ERR_MODE:
151 return "Invalid mode (CS_ERR_MODE)";
152 case CS_ERR_OPTION:
153 return "Invalid option (CS_ERR_OPTION)";
154 case CS_ERR_DETAIL:
155 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800156 case CS_ERR_MEMSETUP:
157 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800158 case CS_ERR_VERSION:
159 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800160 case CS_ERR_DIET:
161 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800162 }
163}
164
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800165cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
166{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800167 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800168 // Error: before cs_open(), dynamic memory management must be initialized
169 // with cs_option(CS_OPT_MEM)
170 return CS_ERR_MEMSETUP;
171
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800172 archs_enable();
173
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800174 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800175 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800177 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800178 if (!ud) {
179 // memory insufficient
180 return CS_ERR_MEM;
181 }
182
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800183 ud->errnum = CS_ERR_OK;
184 ud->arch = arch;
185 ud->mode = mode;
186 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800187 // by default, do not break instruction into details
188 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800190 cs_err err = arch_init[ud->arch](ud);
191 if (err) {
192 cs_mem_free(ud);
193 *handle = 0;
194 return err;
195 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800196
197 *handle = (uintptr_t)ud;
198
199 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800200 } else {
201 *handle = 0;
202 return CS_ERR_ARCH;
203 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800204}
205
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800206cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800207{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800208 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800209 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800210 return CS_ERR_CSH;
211
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800212 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800213
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800214 if (ud->printer_info)
215 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800216
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800217 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800218
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800219 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800220 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800221 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800222
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800223 // invalidate this handle by ZERO out its value.
224 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800225 *handle = 0;
226
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800227 return CS_ERR_OK;
228}
229
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800230#define MIN(x, y) ((x) < (y) ? (x) : (y))
231
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800232// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800233static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800234 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800236 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800237 // avoiding copy insn->detail
238 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800240 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
241 // copy from @regs_read until @arm
242 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
243 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
244 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800245 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
246 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800247 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800248 } else {
249 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800250 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800251 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800253 // fill the instruction bytes
254 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
255
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800256 // map internal instruction opcode to public insn ID
257 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800258 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800259
260 // alias instruction might have ID saved in OpcodePub
261 if (MCInst_getOpcodePub(mci))
262 insn->id = MCInst_getOpcodePub(mci);
263
264 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800265 if (postprinter)
266 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800267
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800268#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800269 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800270 // find first space or tab
271 char *sp = buffer;
272 for (sp = buffer; *sp; sp++)
273 if (*sp == ' '||*sp == '\t')
274 break;
275 if (*sp) {
276 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800277 // find the next non-space char
278 sp++;
279 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
280 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
282 } else
283 insn->op_str[0] = '\0';
284
285 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
286 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800287#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288}
289
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800290cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800291{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800292 archs_enable();
293
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800294 // cs_option() can be called with NULL handle just for CS_OPT_MEM
295 // This is supposed to be executed before all other APIs (even cs_open())
296 if (type == CS_OPT_MEM) {
297 cs_opt_mem *mem = (cs_opt_mem *)value;
298
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800299 cs_mem_malloc = mem->malloc;
300 cs_mem_calloc = mem->calloc;
301 cs_mem_realloc = mem->realloc;
302 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800303 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800304
305 return CS_ERR_OK;
306 }
307
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800308 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600309 if (!handle)
310 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800311
danghvu0b6ea042013-12-19 23:07:26 -0600312 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800313 handle->detail = value;
314 return CS_ERR_OK;
315 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800316
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800317 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800318}
319
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800320// get previous instruction, which can be in the cache, or in total buffer
321static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
322{
323 if (f == 0) {
324 if (total == NULL)
325 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800326 // get the trailing insn from total buffer, which is at
327 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800328 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800329 } else
330 return &cache[f - 1];
331}
332
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800333// dynamicly allocate memory to contain disasm insn
334// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800335size_t cs_disasm_ex(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 +0800336{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800337 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800338 MCInst mci;
339 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800340 size_t c = 0;
341 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800342 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800343 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800344 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800345 bool r;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800347 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800349 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800350 return 0;
351 }
352
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800353 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800355 // reset previous prefix for X86
356 handle->prev_prefix = 0;
357
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800358 memset(insn_cache, 0, sizeof(insn_cache));
359
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800360 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600361 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800362 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800363
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800364 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800365 if (r) {
366 SStream ss;
367 SStream_Init(&ss);
368
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800369 // relative branches need to know the address & size of current insn
370 mci.insn_size = insn_size;
371 mci.address = offset;
372
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800373 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800374 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800375 mci.flat_insn.address = offset;
376 mci.flat_insn.size = insn_size;
377 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800378 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800379 }
380
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 handle->printer(&mci, &ss, handle->printer_info);
382
Joxean114df0e2013-12-04 07:11:32 +0100383 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800384
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800385 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
386 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800387
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800388 if (f == ARR_SIZE(insn_cache)) {
389 // resize total to contain newly disasm insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800390 void *tmp;
391
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800392 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800393 tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800394 if (tmp == NULL) { // insufficient memory
395 cs_mem_free(total);
396 handle->errnum = CS_ERR_MEM;
397 return 0;
398 }
399
400 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800401 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800402
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800403 // reset f back to 0
404 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405 }
406
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800407 c++;
408 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800409 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800410 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
411 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412 }
413
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800414 buffer += insn_size;
415 size -= insn_size;
416 offset += insn_size;
417
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800418 if (count > 0) {
419 // x86 hacky
420 if (!handle->prev_prefix) {
421 if (c == count)
422 break;
423 } else {
424 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800425 if (c == count + 1) {
426 // the last insn is redundant
427 c--;
428 f--;
429 // free allocated detail pointer of the last redundant instruction
430 if (handle->detail)
431 cs_mem_free(insn_cache[f].detail);
432
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800433 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800434 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800435 }
436 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800437 } else {
438 // encounter a broken instruction
439 // XXX: TODO: JOXEAN continue here
440 break;
441 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442 }
443
444 if (f) {
445 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800446 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800447 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800448 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800449 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800450 return 0;
451 }
452
453 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800454 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800455
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800456 }
457
458 *insn = total;
459
460 return c;
461}
462
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800463void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800464{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800465 size_t i;
466
467 // free all detail pointers
468 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800469 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800470
471 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800472 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800473}
474
475// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100476const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800477{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800478 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479
480 if (!handle || handle->reg_name == NULL) {
481 return NULL;
482 }
483
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800484 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800485}
486
pancakef0e4eed2013-12-11 22:14:42 +0100487const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800488{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800489 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490
491 if (!handle || handle->insn_name == NULL) {
492 return NULL;
493 }
494
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800495 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496}
497
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800498static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800499{
500 int i;
501
502 for (i = 0; i < max; i++) {
503 if (arr[i] == id)
504 return true;
505 }
506
507 return false;
508}
509
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800510bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800511{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800512 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513 return false;
514
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800515 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800516 if (!handle->detail) {
517 handle->errnum = CS_ERR_DETAIL;
518 return false;
519 }
520
521 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522}
523
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800524bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800525{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800526 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800527 return false;
528
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800529 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800530 if (!handle->detail) {
531 handle->errnum = CS_ERR_DETAIL;
532 return false;
533 }
534
535 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800536}
537
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800538bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800539{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800540 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800541 return false;
542
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800543 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800544 if (!handle->detail) {
545 handle->errnum = CS_ERR_DETAIL;
546 return false;
547 }
548
549 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800550}
551
552int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
553{
554 if (!ud)
555 return -1;
556
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800557 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800558 if (!handle->detail) {
559 handle->errnum = CS_ERR_DETAIL;
560 return -1;
561 }
562
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800563 unsigned int count = 0, i;
564
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800565 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800566
567 switch (handle->arch) {
568 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800569 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800570 return -1;
571 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800572 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800573 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800574 count++;
575 break;
576 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800577 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800578 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 count++;
580 break;
581 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800582 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800583 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800584 count++;
585 break;
586 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800587 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800588 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800589 count++;
590 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800591 case CS_ARCH_PPC:
592 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800593 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800594 count++;
595 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800596 case CS_ARCH_SPARC:
597 for (i = 0; i < insn->detail->sparc.op_count; i++)
598 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
599 count++;
600 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800601 case CS_ARCH_SYSZ:
602 for (i = 0; i < insn->detail->sysz.op_count; i++)
603 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
604 count++;
605 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800606 }
607
608 return count;
609}
610
611int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
612 unsigned int post)
613{
614 if (!ud)
615 return -1;
616
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800617 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800618 if (!handle->detail) {
619 handle->errnum = CS_ERR_DETAIL;
620 return -1;
621 }
622
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800623 unsigned int count = 0, i;
624
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800625 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800626
627 switch (handle->arch) {
628 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800629 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800630 return -1;
631 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800632 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800633 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634 count++;
635 if (count == post)
636 return i;
637 }
638 break;
639 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800640 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800641 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800642 count++;
643 if (count == post)
644 return i;
645 }
646 break;
647 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800648 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800649 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800650 count++;
651 if (count == post)
652 return i;
653 }
654 break;
655 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800656 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800657 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800658 count++;
659 if (count == post)
660 return i;
661 }
662 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800663 case CS_ARCH_PPC:
664 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800665 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800666 count++;
667 if (count == post)
668 return i;
669 }
670 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800671 case CS_ARCH_SPARC:
672 for (i = 0; i < insn->detail->sparc.op_count; i++) {
673 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
674 count++;
675 if (count == post)
676 return i;
677 }
678 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800679 case CS_ARCH_SYSZ:
680 for (i = 0; i < insn->detail->sysz.op_count; i++) {
681 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
682 count++;
683 if (count == post)
684 return i;
685 }
686 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800687 }
688
689 return -1;
690}