blob: f36a120a46802c36393a5d924490d1fa4c5c2ca4 [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 Quynhc34959b2014-01-22 09:46:42 +080013#define INSN_CACHE_SIZE 64
14
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080015cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080016cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
17void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080019extern void ARM_enable(void);
20extern void AArch64_enable(void);
21extern void Mips_enable(void);
22extern void X86_enable(void);
23extern void PPC_enable(void);
danghvu701b8502014-01-09 11:06:44 +070024
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080025static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080026{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080027 static bool initialized = false;
28
29 if (initialized)
30 return;
31
danghvub33bd2c2014-01-09 12:22:56 +070032#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080033 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070034#endif
35#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080036 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070037#endif
38#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080039 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070040#endif
41#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080042 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070043#endif
44#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070046#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080047
48 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070049}
50
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080051unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080052
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080053#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080054cs_malloc_t cs_mem_malloc = malloc;
55cs_calloc_t cs_mem_calloc = calloc;
56cs_realloc_t cs_mem_realloc = realloc;
57cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080058cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080059#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080060cs_malloc_t cs_mem_malloc = NULL;
61cs_calloc_t cs_mem_calloc = NULL;
62cs_realloc_t cs_mem_realloc = NULL;
63cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080064cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080065#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080066
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080067unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080068{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080069 if (major != NULL && minor != NULL) {
70 *major = CS_API_MAJOR;
71 *minor = CS_API_MINOR;
72 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080073
74 return (CS_API_MAJOR << 8) + CS_API_MINOR;
75}
76
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080077bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080078{
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080079 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080080 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080081 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
82 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +080084 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080085 return all_arch & (1 << query);
86
87 if (query == CS_SUPPORT_DIET) {
88#ifdef CAPSTONE_DIET
89 return true;
90#else
91 return false;
92#endif
93 }
94
95 // unsupported query
96 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080097}
98
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080099cs_err cs_errno(csh handle)
100{
101 if (!handle)
102 return CS_ERR_CSH;
103
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800104 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800105
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800106 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800107}
108
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800109const char *cs_strerror(cs_err code)
110{
111 switch(code) {
112 default:
113 return "Unknown error code";
114 case CS_ERR_OK:
115 return "OK (CS_ERR_OK)";
116 case CS_ERR_MEM:
117 return "Out of memory (CS_ERR_MEM)";
118 case CS_ERR_ARCH:
119 return "Invalid architecture (CS_ERR_ARCH)";
120 case CS_ERR_HANDLE:
121 return "Invalid handle (CS_ERR_HANDLE)";
122 case CS_ERR_CSH:
123 return "Invalid csh (CS_ERR_CSH)";
124 case CS_ERR_MODE:
125 return "Invalid mode (CS_ERR_MODE)";
126 case CS_ERR_OPTION:
127 return "Invalid option (CS_ERR_OPTION)";
128 case CS_ERR_DETAIL:
129 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800130 case CS_ERR_MEMSETUP:
131 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800132 case CS_ERR_VERSION:
133 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800134 case CS_ERR_DIET:
135 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800136 }
137}
138
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800139cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
140{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800141 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800142 // Error: before cs_open(), dynamic memory management must be initialized
143 // with cs_option(CS_OPT_MEM)
144 return CS_ERR_MEMSETUP;
145
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800146 archs_enable();
147
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800148 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800149 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800150
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800151 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800152 if (!ud) {
153 // memory insufficient
154 return CS_ERR_MEM;
155 }
156
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800157 ud->errnum = CS_ERR_OK;
158 ud->arch = arch;
159 ud->mode = mode;
160 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800161 // by default, do not break instruction into details
162 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800164 cs_err err = arch_init[ud->arch](ud);
165 if (err) {
166 cs_mem_free(ud);
167 *handle = 0;
168 return err;
169 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800170
171 *handle = (uintptr_t)ud;
172
173 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800174 } else {
175 *handle = 0;
176 return CS_ERR_ARCH;
177 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178}
179
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800180cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800182 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800183 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800184 return CS_ERR_CSH;
185
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800186 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800188 if (ud->printer_info)
189 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800191 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800192
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800193 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800194 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800195 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800196
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800197 // invalidate this handle by ZERO out its value.
198 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800199 *handle = 0;
200
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800201 return CS_ERR_OK;
202}
203
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800204#define MIN(x, y) ((x) < (y) ? (x) : (y))
205
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800206// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800207static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800208 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800209{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800210 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800211 // avoiding copy insn->detail
212 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800213
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800214 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
215 // copy from @regs_read until @arm
216 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
217 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
218 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800219 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
220 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800221 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800222 } else {
223 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800224 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800225 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800227 // fill the instruction bytes
228 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
229
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800230 // map internal instruction opcode to public insn ID
231 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800232 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800233
234 // alias instruction might have ID saved in OpcodePub
235 if (MCInst_getOpcodePub(mci))
236 insn->id = MCInst_getOpcodePub(mci);
237
238 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800239 if (postprinter)
240 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800241
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800242#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800243 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800244 // find first space or tab
245 char *sp = buffer;
246 for (sp = buffer; *sp; sp++)
247 if (*sp == ' '||*sp == '\t')
248 break;
249 if (*sp) {
250 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800251 // find the next non-space char
252 sp++;
253 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
254 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
256 } else
257 insn->op_str[0] = '\0';
258
259 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
260 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800261#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800262}
263
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800264cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800265{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800266 // cs_option() can be called with NULL handle just for CS_OPT_MEM
267 // This is supposed to be executed before all other APIs (even cs_open())
268 if (type == CS_OPT_MEM) {
269 cs_opt_mem *mem = (cs_opt_mem *)value;
270
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800271 cs_mem_malloc = mem->malloc;
272 cs_mem_calloc = mem->calloc;
273 cs_mem_realloc = mem->realloc;
274 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800275 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800276
277 return CS_ERR_OK;
278 }
279
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800280 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600281 if (!handle)
282 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800283
danghvu0b6ea042013-12-19 23:07:26 -0600284 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800285 handle->detail = value;
286 return CS_ERR_OK;
287 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800288
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800289 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800290}
291
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800292// get previous instruction, which can be in the cache, or in total buffer
293static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
294{
295 if (f == 0) {
296 if (total == NULL)
297 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800298 // get the trailing insn from total buffer, which is at
299 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800300 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800301 } else
302 return &cache[f - 1];
303}
304
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305// dynamicly allocate memory to contain disasm insn
306// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800307size_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 +0800308{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800309 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310 MCInst mci;
311 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800312 size_t c = 0;
313 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800314 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800315 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800316 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800318 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800320 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800321 return 0;
322 }
323
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800324 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800326 // reset previous prefix for X86
327 handle->prev_prefix = 0;
328
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800329 memset(insn_cache, 0, sizeof(insn_cache));
330
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800331 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600332 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800333 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334
335 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
336 if (r) {
337 SStream ss;
338 SStream_Init(&ss);
339
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800340 // relative branches need to know the address & size of current insn
341 mci.insn_size = insn_size;
342 mci.address = offset;
343
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800344 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800345 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800346 mci.flat_insn.address = offset;
347 mci.flat_insn.size = insn_size;
348 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800349 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800350 }
351
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352 handle->printer(&mci, &ss, handle->printer_info);
353
Joxean114df0e2013-12-04 07:11:32 +0100354 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800355
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800356 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
357 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800358
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800359 if (f == ARR_SIZE(insn_cache)) {
360 // resize total to contain newly disasm insns
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800361 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800362 void *tmp = cs_mem_realloc(total, total_size);
363 if (tmp == NULL) { // insufficient memory
364 cs_mem_free(total);
365 handle->errnum = CS_ERR_MEM;
366 return 0;
367 }
368
369 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800370 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800371
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800372 // reset f back to 0
373 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800374 }
375
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800376 c++;
377 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800378 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800379 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
380 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 }
382
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800383 buffer += insn_size;
384 size -= insn_size;
385 offset += insn_size;
386
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800387 if (count > 0) {
388 // x86 hacky
389 if (!handle->prev_prefix) {
390 if (c == count)
391 break;
392 } else {
393 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800394 if (c == count + 1) {
395 // the last insn is redundant
396 c--;
397 f--;
398 // free allocated detail pointer of the last redundant instruction
399 if (handle->detail)
400 cs_mem_free(insn_cache[f].detail);
401
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800402 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800403 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800404 }
405 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800406 } else {
407 // encounter a broken instruction
408 // XXX: TODO: JOXEAN continue here
409 break;
410 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800411 }
412
413 if (f) {
414 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800415 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800417 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800418 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419 return 0;
420 }
421
422 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800423 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800424
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 }
426
427 *insn = total;
428
429 return c;
430}
431
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800432void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800434 size_t i;
435
436 // free all detail pointers
437 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800438 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800439
440 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800441 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442}
443
444// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100445const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800447 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800448
449 if (!handle || handle->reg_name == NULL) {
450 return NULL;
451 }
452
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800453 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800454}
455
pancakef0e4eed2013-12-11 22:14:42 +0100456const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800457{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800458 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800459
460 if (!handle || handle->insn_name == NULL) {
461 return NULL;
462 }
463
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800464 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800465}
466
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800467static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800468{
469 int i;
470
471 for (i = 0; i < max; i++) {
472 if (arr[i] == id)
473 return true;
474 }
475
476 return false;
477}
478
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800479bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800481 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482 return false;
483
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800484 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800485 if (!handle->detail) {
486 handle->errnum = CS_ERR_DETAIL;
487 return false;
488 }
489
490 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800491}
492
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800493bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800494{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800495 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496 return false;
497
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800498 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800499 if (!handle->detail) {
500 handle->errnum = CS_ERR_DETAIL;
501 return false;
502 }
503
504 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800505}
506
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800507bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800508{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800509 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800510 return false;
511
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800512 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800513 if (!handle->detail) {
514 handle->errnum = CS_ERR_DETAIL;
515 return false;
516 }
517
518 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519}
520
521int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
522{
523 if (!ud)
524 return -1;
525
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800526 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800527 if (!handle->detail) {
528 handle->errnum = CS_ERR_DETAIL;
529 return -1;
530 }
531
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800532 unsigned int count = 0, i;
533
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800534 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535
536 switch (handle->arch) {
537 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800538 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800539 return -1;
540 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800541 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800542 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800543 count++;
544 break;
545 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800546 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800547 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800548 count++;
549 break;
550 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800551 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800552 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800553 count++;
554 break;
555 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800556 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800557 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800558 count++;
559 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800560 case CS_ARCH_PPC:
561 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800562 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800563 count++;
564 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800565 }
566
567 return count;
568}
569
570int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
571 unsigned int post)
572{
573 if (!ud)
574 return -1;
575
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800576 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800577 if (!handle->detail) {
578 handle->errnum = CS_ERR_DETAIL;
579 return -1;
580 }
581
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800582 unsigned int count = 0, i;
583
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800584 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800585
586 switch (handle->arch) {
587 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800588 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800589 return -1;
590 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800591 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800592 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800593 count++;
594 if (count == post)
595 return i;
596 }
597 break;
598 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800599 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800600 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800601 count++;
602 if (count == post)
603 return i;
604 }
605 break;
606 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800607 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800608 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800609 count++;
610 if (count == post)
611 return i;
612 }
613 break;
614 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800615 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800616 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800617 count++;
618 if (count == post)
619 return i;
620 }
621 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800622 case CS_ARCH_PPC:
623 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800624 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800625 count++;
626 if (count == post)
627 return i;
628 }
629 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800630 }
631
632 return -1;
633}