blob: d3d037aa6f846c94fb46ebb214e2bacdac8e4389 [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 Quynha836b752014-03-06 03:36:03 +080013#ifndef CAPSTONE_DIET
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080014#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080015#else
16#define INSN_CACHE_SIZE 8
17#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080018
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080019cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080020cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
21void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080022
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080023extern void ARM_enable(void);
24extern void AArch64_enable(void);
25extern void Mips_enable(void);
26extern void X86_enable(void);
27extern void PPC_enable(void);
danghvu701b8502014-01-09 11:06:44 +070028
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080029static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080030{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080031 static bool initialized = false;
32
33 if (initialized)
34 return;
35
danghvub33bd2c2014-01-09 12:22:56 +070036#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070038#endif
39#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070041#endif
42#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070044#endif
45#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
48#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 Quynhc272e9d2014-01-19 12:03:22 +080051
52 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070053}
54
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080055unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080056
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080057#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080058cs_malloc_t cs_mem_malloc = malloc;
59cs_calloc_t cs_mem_calloc = calloc;
60cs_realloc_t cs_mem_realloc = realloc;
61cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080062cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080063#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080064cs_malloc_t cs_mem_malloc = NULL;
65cs_calloc_t cs_mem_calloc = NULL;
66cs_realloc_t cs_mem_realloc = NULL;
67cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080068cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080069#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080070
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080071unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080072{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080073 archs_enable();
74
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080075 if (major != NULL && minor != NULL) {
76 *major = CS_API_MAJOR;
77 *minor = CS_API_MINOR;
78 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080079
80 return (CS_API_MAJOR << 8) + CS_API_MINOR;
81}
82
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080083bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080084{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080085 archs_enable();
86
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080087 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080088 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080089 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
90 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080091
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +080092 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080093 return all_arch & (1 << query);
94
95 if (query == CS_SUPPORT_DIET) {
96#ifdef CAPSTONE_DIET
97 return true;
98#else
99 return false;
100#endif
101 }
102
103 // unsupported query
104 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800105}
106
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800107cs_err cs_errno(csh handle)
108{
109 if (!handle)
110 return CS_ERR_CSH;
111
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800112 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800113
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800114 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800115}
116
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800117const char *cs_strerror(cs_err code)
118{
119 switch(code) {
120 default:
121 return "Unknown error code";
122 case CS_ERR_OK:
123 return "OK (CS_ERR_OK)";
124 case CS_ERR_MEM:
125 return "Out of memory (CS_ERR_MEM)";
126 case CS_ERR_ARCH:
127 return "Invalid architecture (CS_ERR_ARCH)";
128 case CS_ERR_HANDLE:
129 return "Invalid handle (CS_ERR_HANDLE)";
130 case CS_ERR_CSH:
131 return "Invalid csh (CS_ERR_CSH)";
132 case CS_ERR_MODE:
133 return "Invalid mode (CS_ERR_MODE)";
134 case CS_ERR_OPTION:
135 return "Invalid option (CS_ERR_OPTION)";
136 case CS_ERR_DETAIL:
137 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800138 case CS_ERR_MEMSETUP:
139 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800140 case CS_ERR_VERSION:
141 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800142 case CS_ERR_DIET:
143 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800144 }
145}
146
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
148{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800149 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800150 // Error: before cs_open(), dynamic memory management must be initialized
151 // with cs_option(CS_OPT_MEM)
152 return CS_ERR_MEMSETUP;
153
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800154 archs_enable();
155
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800156 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800157 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800158
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800159 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800160 if (!ud) {
161 // memory insufficient
162 return CS_ERR_MEM;
163 }
164
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800165 ud->errnum = CS_ERR_OK;
166 ud->arch = arch;
167 ud->mode = mode;
168 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800169 // by default, do not break instruction into details
170 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800172 cs_err err = arch_init[ud->arch](ud);
173 if (err) {
174 cs_mem_free(ud);
175 *handle = 0;
176 return err;
177 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800178
179 *handle = (uintptr_t)ud;
180
181 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800182 } else {
183 *handle = 0;
184 return CS_ERR_ARCH;
185 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800186}
187
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800188cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800190 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800191 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800192 return CS_ERR_CSH;
193
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800194 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800196 if (ud->printer_info)
197 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800198
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800199 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800200
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800201 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800202 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800203 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800204
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800205 // invalidate this handle by ZERO out its value.
206 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800207 *handle = 0;
208
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800209 return CS_ERR_OK;
210}
211
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800212#define MIN(x, y) ((x) < (y) ? (x) : (y))
213
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800215static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800216 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800217{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800218 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800219 // avoiding copy insn->detail
220 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800222 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
223 // copy from @regs_read until @arm
224 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
225 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
226 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800227 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
228 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800229 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800230 } else {
231 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800232 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800233 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800235 // fill the instruction bytes
236 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
237
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800238 // map internal instruction opcode to public insn ID
239 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800240 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800241
242 // alias instruction might have ID saved in OpcodePub
243 if (MCInst_getOpcodePub(mci))
244 insn->id = MCInst_getOpcodePub(mci);
245
246 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800247 if (postprinter)
248 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800249
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800250#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800251 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800252 // find first space or tab
253 char *sp = buffer;
254 for (sp = buffer; *sp; sp++)
255 if (*sp == ' '||*sp == '\t')
256 break;
257 if (*sp) {
258 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800259 // find the next non-space char
260 sp++;
261 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
262 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
264 } else
265 insn->op_str[0] = '\0';
266
267 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
268 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800269#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800270}
271
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800272cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800273{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800274 archs_enable();
275
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800276 // cs_option() can be called with NULL handle just for CS_OPT_MEM
277 // This is supposed to be executed before all other APIs (even cs_open())
278 if (type == CS_OPT_MEM) {
279 cs_opt_mem *mem = (cs_opt_mem *)value;
280
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800281 cs_mem_malloc = mem->malloc;
282 cs_mem_calloc = mem->calloc;
283 cs_mem_realloc = mem->realloc;
284 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800285 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800286
287 return CS_ERR_OK;
288 }
289
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800290 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600291 if (!handle)
292 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800293
danghvu0b6ea042013-12-19 23:07:26 -0600294 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800295 handle->detail = value;
296 return CS_ERR_OK;
297 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800298
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800299 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800300}
301
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800302// get previous instruction, which can be in the cache, or in total buffer
303static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
304{
305 if (f == 0) {
306 if (total == NULL)
307 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800308 // get the trailing insn from total buffer, which is at
309 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800310 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800311 } else
312 return &cache[f - 1];
313}
314
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800315// dynamicly allocate memory to contain disasm insn
316// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800317size_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 +0800318{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800319 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320 MCInst mci;
321 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800322 size_t c = 0;
323 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800324 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800326 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800327 bool r;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800329 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800331 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800332 return 0;
333 }
334
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800335 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800336
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800337 // reset previous prefix for X86
338 handle->prev_prefix = 0;
339
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800340 memset(insn_cache, 0, sizeof(insn_cache));
341
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800342 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600343 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800344 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800346 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800347 if (r) {
348 SStream ss;
349 SStream_Init(&ss);
350
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800351 // relative branches need to know the address & size of current insn
352 mci.insn_size = insn_size;
353 mci.address = offset;
354
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800355 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800356 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800357 mci.flat_insn.address = offset;
358 mci.flat_insn.size = insn_size;
359 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800360 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800361 }
362
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800363 handle->printer(&mci, &ss, handle->printer_info);
364
Joxean114df0e2013-12-04 07:11:32 +0100365 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800366
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800367 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
368 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800369
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800370 if (f == ARR_SIZE(insn_cache)) {
371 // resize total to contain newly disasm insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800372 void *tmp;
373
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800374 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800375 tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800376 if (tmp == NULL) { // insufficient memory
377 cs_mem_free(total);
378 handle->errnum = CS_ERR_MEM;
379 return 0;
380 }
381
382 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800383 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800384
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800385 // reset f back to 0
386 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800387 }
388
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800389 c++;
390 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800391 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800392 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
393 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394 }
395
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396 buffer += insn_size;
397 size -= insn_size;
398 offset += insn_size;
399
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800400 if (count > 0) {
401 // x86 hacky
402 if (!handle->prev_prefix) {
403 if (c == count)
404 break;
405 } else {
406 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800407 if (c == count + 1) {
408 // the last insn is redundant
409 c--;
410 f--;
411 // free allocated detail pointer of the last redundant instruction
412 if (handle->detail)
413 cs_mem_free(insn_cache[f].detail);
414
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800415 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800416 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800417 }
418 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800419 } else {
420 // encounter a broken instruction
421 // XXX: TODO: JOXEAN continue here
422 break;
423 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424 }
425
426 if (f) {
427 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800428 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800430 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800431 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432 return 0;
433 }
434
435 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800436 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800437
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438 }
439
440 *insn = total;
441
442 return c;
443}
444
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800445void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800447 size_t i;
448
449 // free all detail pointers
450 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800451 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800452
453 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800454 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455}
456
457// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100458const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800459{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800460 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800461
462 if (!handle || handle->reg_name == NULL) {
463 return NULL;
464 }
465
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800466 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800467}
468
pancakef0e4eed2013-12-11 22:14:42 +0100469const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800470{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800471 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800472
473 if (!handle || handle->insn_name == NULL) {
474 return NULL;
475 }
476
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800477 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800478}
479
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800480static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481{
482 int i;
483
484 for (i = 0; i < max; i++) {
485 if (arr[i] == id)
486 return true;
487 }
488
489 return false;
490}
491
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800492bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800494 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800495 return false;
496
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800497 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800498 if (!handle->detail) {
499 handle->errnum = CS_ERR_DETAIL;
500 return false;
501 }
502
503 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800504}
505
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800506bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800507{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800508 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800509 return false;
510
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800511 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800512 if (!handle->detail) {
513 handle->errnum = CS_ERR_DETAIL;
514 return false;
515 }
516
517 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518}
519
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800520bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800521{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800522 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800523 return false;
524
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800525 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800526 if (!handle->detail) {
527 handle->errnum = CS_ERR_DETAIL;
528 return false;
529 }
530
531 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800532}
533
534int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
535{
536 if (!ud)
537 return -1;
538
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800539 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800540 if (!handle->detail) {
541 handle->errnum = CS_ERR_DETAIL;
542 return -1;
543 }
544
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800545 unsigned int count = 0, i;
546
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800547 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800548
549 switch (handle->arch) {
550 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800551 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552 return -1;
553 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800554 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800555 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556 count++;
557 break;
558 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800559 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800560 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800561 count++;
562 break;
563 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800564 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800565 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800566 count++;
567 break;
568 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800569 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800570 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800571 count++;
572 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800573 case CS_ARCH_PPC:
574 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800575 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800576 count++;
577 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800578 }
579
580 return count;
581}
582
583int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
584 unsigned int post)
585{
586 if (!ud)
587 return -1;
588
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800589 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800590 if (!handle->detail) {
591 handle->errnum = CS_ERR_DETAIL;
592 return -1;
593 }
594
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595 unsigned int count = 0, i;
596
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800597 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598
599 switch (handle->arch) {
600 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800601 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800602 return -1;
603 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800604 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800605 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800606 count++;
607 if (count == post)
608 return i;
609 }
610 break;
611 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800612 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800613 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614 count++;
615 if (count == post)
616 return i;
617 }
618 break;
619 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800620 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800621 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800622 count++;
623 if (count == post)
624 return i;
625 }
626 break;
627 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800628 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800629 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800630 count++;
631 if (count == post)
632 return i;
633 }
634 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800635 case CS_ARCH_PPC:
636 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800637 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800638 count++;
639 if (count == post)
640 return i;
641 }
642 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800643 }
644
645 return -1;
646}