blob: bdacee8570d78474e04b9802fc77a28e4635809b [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);
danghvu701b8502014-01-09 11:06:44 +070029
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080030static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080031{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080032 static bool initialized = false;
33
34 if (initialized)
35 return;
36
danghvub33bd2c2014-01-09 12:22:56 +070037#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080038 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070039#endif
40#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080041 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070042#endif
43#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080044 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070045#endif
46#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080047 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070048#endif
49#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080050 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070051#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052
53 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070054}
55
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080056unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080057
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080058#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080059cs_malloc_t cs_mem_malloc = malloc;
60cs_calloc_t cs_mem_calloc = calloc;
61cs_realloc_t cs_mem_realloc = realloc;
62cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080063cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080064#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080065cs_malloc_t cs_mem_malloc = NULL;
66cs_calloc_t cs_mem_calloc = NULL;
67cs_realloc_t cs_mem_realloc = NULL;
68cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080069cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080070#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080071
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080072unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080073{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080074 archs_enable();
75
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080076 if (major != NULL && minor != NULL) {
77 *major = CS_API_MAJOR;
78 *minor = CS_API_MINOR;
79 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080080
81 return (CS_API_MAJOR << 8) + CS_API_MINOR;
82}
83
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080084bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080085{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080086 archs_enable();
87
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080088 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080089 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080090 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
91 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080092
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +080093 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080094 return all_arch & (1 << query);
95
96 if (query == CS_SUPPORT_DIET) {
97#ifdef CAPSTONE_DIET
98 return true;
99#else
100 return false;
101#endif
102 }
103
104 // unsupported query
105 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800106}
107
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800108cs_err cs_errno(csh handle)
109{
110 if (!handle)
111 return CS_ERR_CSH;
112
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800113 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800114
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800115 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800116}
117
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800118const char *cs_strerror(cs_err code)
119{
120 switch(code) {
121 default:
122 return "Unknown error code";
123 case CS_ERR_OK:
124 return "OK (CS_ERR_OK)";
125 case CS_ERR_MEM:
126 return "Out of memory (CS_ERR_MEM)";
127 case CS_ERR_ARCH:
128 return "Invalid architecture (CS_ERR_ARCH)";
129 case CS_ERR_HANDLE:
130 return "Invalid handle (CS_ERR_HANDLE)";
131 case CS_ERR_CSH:
132 return "Invalid csh (CS_ERR_CSH)";
133 case CS_ERR_MODE:
134 return "Invalid mode (CS_ERR_MODE)";
135 case CS_ERR_OPTION:
136 return "Invalid option (CS_ERR_OPTION)";
137 case CS_ERR_DETAIL:
138 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800139 case CS_ERR_MEMSETUP:
140 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800141 case CS_ERR_VERSION:
142 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800143 case CS_ERR_DIET:
144 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800145 }
146}
147
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
149{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800150 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800151 // Error: before cs_open(), dynamic memory management must be initialized
152 // with cs_option(CS_OPT_MEM)
153 return CS_ERR_MEMSETUP;
154
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800155 archs_enable();
156
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800157 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800158 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800159
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800160 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800161 if (!ud) {
162 // memory insufficient
163 return CS_ERR_MEM;
164 }
165
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800166 ud->errnum = CS_ERR_OK;
167 ud->arch = arch;
168 ud->mode = mode;
169 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800170 // by default, do not break instruction into details
171 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800172
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800173 cs_err err = arch_init[ud->arch](ud);
174 if (err) {
175 cs_mem_free(ud);
176 *handle = 0;
177 return err;
178 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800179
180 *handle = (uintptr_t)ud;
181
182 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800183 } else {
184 *handle = 0;
185 return CS_ERR_ARCH;
186 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187}
188
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800189cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800191 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800192 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193 return CS_ERR_CSH;
194
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800195 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800197 if (ud->printer_info)
198 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800200 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800201
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800202 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800203 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800204 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800205
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800206 // invalidate this handle by ZERO out its value.
207 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800208 *handle = 0;
209
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800210 return CS_ERR_OK;
211}
212
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800213#define MIN(x, y) ((x) < (y) ? (x) : (y))
214
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800216static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800217 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800218{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800219 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800220 // avoiding copy insn->detail
221 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800222
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800223 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
224 // copy from @regs_read until @arm
225 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
226 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
227 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800228 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
229 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800230 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800231 } else {
232 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800233 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800234 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800236 // fill the instruction bytes
237 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
238
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800239 // map internal instruction opcode to public insn ID
240 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800241 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800242
243 // alias instruction might have ID saved in OpcodePub
244 if (MCInst_getOpcodePub(mci))
245 insn->id = MCInst_getOpcodePub(mci);
246
247 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800248 if (postprinter)
249 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800250
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800251#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800253 // find first space or tab
254 char *sp = buffer;
255 for (sp = buffer; *sp; sp++)
256 if (*sp == ' '||*sp == '\t')
257 break;
258 if (*sp) {
259 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800260 // find the next non-space char
261 sp++;
262 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
263 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800264 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
265 } else
266 insn->op_str[0] = '\0';
267
268 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
269 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800270#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271}
272
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800273cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800274{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800275 archs_enable();
276
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800277 // cs_option() can be called with NULL handle just for CS_OPT_MEM
278 // This is supposed to be executed before all other APIs (even cs_open())
279 if (type == CS_OPT_MEM) {
280 cs_opt_mem *mem = (cs_opt_mem *)value;
281
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800282 cs_mem_malloc = mem->malloc;
283 cs_mem_calloc = mem->calloc;
284 cs_mem_realloc = mem->realloc;
285 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800286 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800287
288 return CS_ERR_OK;
289 }
290
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800291 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600292 if (!handle)
293 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800294
danghvu0b6ea042013-12-19 23:07:26 -0600295 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800296 handle->detail = value;
297 return CS_ERR_OK;
298 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800299
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800300 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800301}
302
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800303// get previous instruction, which can be in the cache, or in total buffer
304static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
305{
306 if (f == 0) {
307 if (total == NULL)
308 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800309 // get the trailing insn from total buffer, which is at
310 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800311 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800312 } else
313 return &cache[f - 1];
314}
315
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316// dynamicly allocate memory to contain disasm insn
317// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800318size_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 +0800319{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800320 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800321 MCInst mci;
322 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800323 size_t c = 0;
324 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800325 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800326 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800327 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800328 bool r;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800330 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800331 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800332 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800333 return 0;
334 }
335
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800336 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800337
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800338 // reset previous prefix for X86
339 handle->prev_prefix = 0;
340
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800341 memset(insn_cache, 0, sizeof(insn_cache));
342
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800343 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600344 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800345 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800347 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348 if (r) {
349 SStream ss;
350 SStream_Init(&ss);
351
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800352 // relative branches need to know the address & size of current insn
353 mci.insn_size = insn_size;
354 mci.address = offset;
355
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800356 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800357 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800358 mci.flat_insn.address = offset;
359 mci.flat_insn.size = insn_size;
360 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800361 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800362 }
363
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800364 handle->printer(&mci, &ss, handle->printer_info);
365
Joxean114df0e2013-12-04 07:11:32 +0100366 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800367
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800368 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
369 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800371 if (f == ARR_SIZE(insn_cache)) {
372 // resize total to contain newly disasm insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800373 void *tmp;
374
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800375 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800376 tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800377 if (tmp == NULL) { // insufficient memory
378 cs_mem_free(total);
379 handle->errnum = CS_ERR_MEM;
380 return 0;
381 }
382
383 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800384 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800385
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800386 // reset f back to 0
387 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800388 }
389
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800390 c++;
391 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800392 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800393 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
394 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395 }
396
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800397 buffer += insn_size;
398 size -= insn_size;
399 offset += insn_size;
400
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800401 if (count > 0) {
402 // x86 hacky
403 if (!handle->prev_prefix) {
404 if (c == count)
405 break;
406 } else {
407 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800408 if (c == count + 1) {
409 // the last insn is redundant
410 c--;
411 f--;
412 // free allocated detail pointer of the last redundant instruction
413 if (handle->detail)
414 cs_mem_free(insn_cache[f].detail);
415
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800416 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800417 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800418 }
419 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800420 } else {
421 // encounter a broken instruction
422 // XXX: TODO: JOXEAN continue here
423 break;
424 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 }
426
427 if (f) {
428 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800429 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800430 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800431 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800432 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433 return 0;
434 }
435
436 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800437 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800438
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 }
440
441 *insn = total;
442
443 return c;
444}
445
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800446void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800447{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800448 size_t i;
449
450 // free all detail pointers
451 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800452 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800453
454 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800455 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800456}
457
458// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100459const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800461 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800462
463 if (!handle || handle->reg_name == NULL) {
464 return NULL;
465 }
466
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800467 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800468}
469
pancakef0e4eed2013-12-11 22:14:42 +0100470const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800472 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800473
474 if (!handle || handle->insn_name == NULL) {
475 return NULL;
476 }
477
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800478 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479}
480
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800481static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482{
483 int i;
484
485 for (i = 0; i < max; i++) {
486 if (arr[i] == id)
487 return true;
488 }
489
490 return false;
491}
492
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800493bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_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->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800505}
506
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800507bool cs_reg_read(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_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519}
520
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800521bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800523 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800524 return false;
525
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800526 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800527 if (!handle->detail) {
528 handle->errnum = CS_ERR_DETAIL;
529 return false;
530 }
531
532 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800533}
534
535int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
536{
537 if (!ud)
538 return -1;
539
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800540 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800541 if (!handle->detail) {
542 handle->errnum = CS_ERR_DETAIL;
543 return -1;
544 }
545
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546 unsigned int count = 0, i;
547
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800548 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800549
550 switch (handle->arch) {
551 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800552 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800553 return -1;
554 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800555 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800556 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800557 count++;
558 break;
559 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800560 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800561 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800562 count++;
563 break;
564 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800565 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800566 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800567 count++;
568 break;
569 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800570 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800571 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800572 count++;
573 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800574 case CS_ARCH_PPC:
575 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800576 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800577 count++;
578 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 }
580
581 return count;
582}
583
584int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
585 unsigned int post)
586{
587 if (!ud)
588 return -1;
589
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800590 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800591 if (!handle->detail) {
592 handle->errnum = CS_ERR_DETAIL;
593 return -1;
594 }
595
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800596 unsigned int count = 0, i;
597
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800598 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800599
600 switch (handle->arch) {
601 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800602 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800603 return -1;
604 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800605 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800606 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800607 count++;
608 if (count == post)
609 return i;
610 }
611 break;
612 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800613 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800614 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800615 count++;
616 if (count == post)
617 return i;
618 }
619 break;
620 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800621 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800622 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800623 count++;
624 if (count == post)
625 return i;
626 }
627 break;
628 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800629 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800630 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800631 count++;
632 if (count == post)
633 return i;
634 }
635 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800636 case CS_ARCH_PPC:
637 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800638 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800639 count++;
640 if (count == post)
641 return i;
642 }
643 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800644 }
645
646 return -1;
647}