Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 3 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 4 | #include <stddef.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <capstone.h> |
| 9 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 10 | #include "utils.h" |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 11 | #include "MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 12 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 13 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 14 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 15 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 16 | |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 17 | // we need this trick to enable module constructors in static lib |
danghvu | 701b850 | 2014-01-09 11:06:44 +0700 | [diff] [blame] | 18 | extern void enable_arm(); |
| 19 | extern void enable_arm64(); |
| 20 | extern void enable_mips(); |
| 21 | extern void enable_x86(); |
| 22 | extern void enable_powerpc(); |
| 23 | |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 24 | void enable_construct() |
| 25 | { |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 26 | #ifdef CAPSTONE_HAS_ARM |
| 27 | enable_arm(); |
| 28 | #endif |
| 29 | #ifdef CAPSTONE_HAS_ARM64 |
| 30 | enable_arm64(); |
| 31 | #endif |
| 32 | #ifdef CAPSTONE_HAS_MIPS |
| 33 | enable_mips(); |
| 34 | #endif |
| 35 | #ifdef CAPSTONE_HAS_X86 |
| 36 | enable_x86(); |
| 37 | #endif |
| 38 | #ifdef CAPSTONE_HAS_POWERPC |
| 39 | enable_powerpc(); |
| 40 | #endif |
| 41 | } |
| 42 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 43 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 44 | |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 45 | #ifdef USE_SYS_DYN_MEM |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 46 | cs_malloc_t cs_mem_malloc = malloc; |
| 47 | cs_calloc_t cs_mem_calloc = calloc; |
| 48 | cs_realloc_t cs_mem_realloc = realloc; |
| 49 | cs_free_t cs_mem_free = free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 50 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 51 | #else |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 52 | cs_malloc_t cs_mem_malloc = NULL; |
| 53 | cs_calloc_t cs_mem_calloc = NULL; |
| 54 | cs_realloc_t cs_mem_realloc = NULL; |
| 55 | cs_free_t cs_mem_free = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 56 | cs_vsnprintf_t cs_vsnprintf = NULL; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 57 | #endif |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 58 | |
Nguyen Anh Quynh | b90cb99 | 2013-12-28 13:59:09 +0800 | [diff] [blame] | 59 | unsigned int cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 60 | { |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 61 | if (major != NULL && minor != NULL) { |
| 62 | *major = CS_API_MAJOR; |
| 63 | *minor = CS_API_MINOR; |
| 64 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 65 | |
| 66 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 67 | } |
| 68 | |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 69 | bool cs_support(int arch) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 70 | { |
| 71 | if (arch == CS_ARCH_ALL) |
| 72 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 73 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
| 74 | (1 << CS_ARCH_PPC)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 75 | |
| 76 | return all_arch & (1 << arch); |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 79 | cs_err cs_errno(csh handle) |
| 80 | { |
| 81 | if (!handle) |
| 82 | return CS_ERR_CSH; |
| 83 | |
| 84 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 85 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 86 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 89 | const char *cs_strerror(cs_err code) |
| 90 | { |
| 91 | switch(code) { |
| 92 | default: |
| 93 | return "Unknown error code"; |
| 94 | case CS_ERR_OK: |
| 95 | return "OK (CS_ERR_OK)"; |
| 96 | case CS_ERR_MEM: |
| 97 | return "Out of memory (CS_ERR_MEM)"; |
| 98 | case CS_ERR_ARCH: |
| 99 | return "Invalid architecture (CS_ERR_ARCH)"; |
| 100 | case CS_ERR_HANDLE: |
| 101 | return "Invalid handle (CS_ERR_HANDLE)"; |
| 102 | case CS_ERR_CSH: |
| 103 | return "Invalid csh (CS_ERR_CSH)"; |
| 104 | case CS_ERR_MODE: |
| 105 | return "Invalid mode (CS_ERR_MODE)"; |
| 106 | case CS_ERR_OPTION: |
| 107 | return "Invalid option (CS_ERR_OPTION)"; |
| 108 | case CS_ERR_DETAIL: |
| 109 | return "Details are unavailable (CS_ERR_DETAIL)"; |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 110 | case CS_ERR_MEMSETUP: |
| 111 | return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)"; |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 115 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 116 | { |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 117 | if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf) |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 118 | // Error: before cs_open(), dynamic memory management must be initialized |
| 119 | // with cs_option(CS_OPT_MEM) |
| 120 | return CS_ERR_MEMSETUP; |
| 121 | |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 122 | if (arch < CS_ARCH_MAX && arch_init[arch]) { |
| 123 | cs_struct *ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 124 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 125 | ud = cs_mem_calloc(1, sizeof(*ud)); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 126 | if (!ud) { |
| 127 | // memory insufficient |
| 128 | return CS_ERR_MEM; |
| 129 | } |
| 130 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 131 | ud->errnum = CS_ERR_OK; |
| 132 | ud->arch = arch; |
| 133 | ud->mode = mode; |
| 134 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 135 | // by default, do not break instruction into details |
| 136 | ud->detail = CS_OPT_OFF; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 137 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 138 | arch_init[ud->arch](ud); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 139 | |
| 140 | *handle = (uintptr_t)ud; |
| 141 | |
| 142 | return CS_ERR_OK; |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 143 | } else { |
| 144 | *handle = 0; |
| 145 | return CS_ERR_ARCH; |
| 146 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | cs_err cs_close(csh handle) |
| 150 | { |
| 151 | if (!handle) |
| 152 | return CS_ERR_CSH; |
| 153 | |
| 154 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 155 | |
| 156 | switch (ud->arch) { |
| 157 | case CS_ARCH_X86: |
| 158 | break; |
| 159 | case CS_ARCH_ARM: |
| 160 | case CS_ARCH_MIPS: |
| 161 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 162 | case CS_ARCH_PPC: |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 163 | cs_mem_free(ud->printer_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 164 | break; |
| 165 | default: // unsupported architecture |
| 166 | return CS_ERR_HANDLE; |
| 167 | } |
| 168 | |
Nguyen Anh Quynh | 9fac512 | 2014-01-07 10:56:04 +0800 | [diff] [blame] | 169 | // arch_destroy[ud->arch](ud); |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 170 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 171 | cs_mem_free(ud->insn_cache); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 172 | memset(ud, 0, sizeof(*ud)); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 173 | cs_mem_free(ud); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 174 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 175 | return CS_ERR_OK; |
| 176 | } |
| 177 | |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 178 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 179 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 180 | // fill insn with mnemonic & operands info |
| 181 | static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 182 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 183 | { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 184 | if (handle->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 185 | // avoiding copy insn->detail |
| 186 | memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 187 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 188 | // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8 |
| 189 | // copy from @regs_read until @arm |
| 190 | memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read), |
| 191 | offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read)); |
| 192 | // then copy from @arm until end |
| 193 | memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm), |
| 194 | sizeof(cs_detail) - offsetof(cs_detail, arm)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 195 | } else { |
| 196 | insn->address = mci->address; |
| 197 | insn->size = mci->insn_size; |
| 198 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 199 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 200 | // fill the instruction bytes |
| 201 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
| 202 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 203 | // map internal instruction opcode to public insn ID |
| 204 | if (handle->insn_id) |
Nguyen Anh Quynh | 1acfd0b | 2014-01-06 10:56:59 +0800 | [diff] [blame] | 205 | handle->insn_id(handle, insn, MCInst_getOpcode(mci)); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 206 | |
| 207 | // alias instruction might have ID saved in OpcodePub |
| 208 | if (MCInst_getOpcodePub(mci)) |
| 209 | insn->id = MCInst_getOpcodePub(mci); |
| 210 | |
| 211 | // post printer handles some corner cases (hacky) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 212 | if (postprinter) |
| 213 | postprinter((csh)handle, insn, buffer); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 214 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 215 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 216 | // find first space or tab |
| 217 | char *sp = buffer; |
| 218 | for (sp = buffer; *sp; sp++) |
| 219 | if (*sp == ' '||*sp == '\t') |
| 220 | break; |
| 221 | if (*sp) { |
| 222 | *sp = '\0'; |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 223 | // find the next non-space char |
| 224 | sp++; |
| 225 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 226 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 227 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 228 | } else |
| 229 | insn->op_str[0] = '\0'; |
| 230 | |
| 231 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
| 232 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
| 233 | } |
| 234 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 235 | cs_err cs_option(csh ud, cs_opt_type type, size_t value) |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 236 | { |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 237 | // cs_option() can be called with NULL handle just for CS_OPT_MEM |
| 238 | // This is supposed to be executed before all other APIs (even cs_open()) |
| 239 | if (type == CS_OPT_MEM) { |
| 240 | cs_opt_mem *mem = (cs_opt_mem *)value; |
| 241 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 242 | cs_mem_malloc = mem->malloc; |
| 243 | cs_mem_calloc = mem->calloc; |
| 244 | cs_mem_realloc = mem->realloc; |
| 245 | cs_mem_free = mem->free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 246 | cs_vsnprintf = mem->vsnprintf; |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 247 | |
| 248 | return CS_ERR_OK; |
| 249 | } |
| 250 | |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 251 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 252 | if (!handle) |
| 253 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 254 | |
danghvu | 0b6ea04 | 2013-12-19 23:07:26 -0600 | [diff] [blame] | 255 | if (type == CS_OPT_DETAIL) { |
Nguyen Anh Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 256 | handle->detail = value; |
| 257 | return CS_ERR_OK; |
| 258 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 259 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 260 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 263 | // dynamicly allocate memory to contain disasm insn |
| 264 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
Nguyen Anh Quynh | 04c19be | 2013-12-25 13:26:22 +0800 | [diff] [blame] | 265 | size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 266 | { |
| 267 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 268 | MCInst mci; |
| 269 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 270 | size_t c = 0, f = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 271 | cs_insn insn_cache[64]; |
| 272 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 273 | size_t total_size = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 274 | |
| 275 | if (!handle) { |
| 276 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 277 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 281 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 282 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 283 | memset(insn_cache, 0, sizeof(insn_cache)); |
| 284 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 285 | while (size > 0) { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 286 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 287 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 288 | |
| 289 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 290 | if (r) { |
| 291 | SStream ss; |
| 292 | SStream_Init(&ss); |
| 293 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 294 | // relative branches need to know the address & size of current insn |
| 295 | mci.insn_size = insn_size; |
| 296 | mci.address = offset; |
| 297 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 298 | if (handle->detail) { |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 299 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 300 | mci.flat_insn.address = offset; |
| 301 | mci.flat_insn.size = insn_size; |
| 302 | // allocate memory for @detail pointer |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 303 | insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 306 | handle->printer(&mci, &ss, handle->printer_info); |
| 307 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 308 | fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 309 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 310 | f++; |
| 311 | |
| 312 | if (f == ARR_SIZE(insn_cache)) { |
| 313 | // resize total to contain newly disasm insns |
| 314 | total_size += sizeof(insn_cache); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 315 | void *tmp = cs_mem_realloc(total, total_size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 316 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 317 | cs_mem_free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 318 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | total = tmp; |
| 323 | memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache)); |
| 324 | // reset f back to 0 |
| 325 | f = 0; |
| 326 | } |
| 327 | |
| 328 | c++; |
| 329 | buffer += insn_size; |
| 330 | size -= insn_size; |
| 331 | offset += insn_size; |
| 332 | |
| 333 | if (count > 0 && c == count) |
| 334 | break; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 335 | } else { |
| 336 | // encounter a broken instruction |
| 337 | // XXX: TODO: JOXEAN continue here |
| 338 | break; |
| 339 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (f) { |
| 343 | // resize total to contain newly disasm insns |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 344 | void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0])); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 345 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 346 | cs_mem_free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 347 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | total = tmp; |
| 352 | memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0])); |
| 353 | } |
| 354 | |
| 355 | *insn = total; |
| 356 | |
| 357 | return c; |
| 358 | } |
| 359 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 360 | void cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 361 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 362 | size_t i; |
| 363 | |
| 364 | // free all detail pointers |
| 365 | for (i = 0; i < count; i++) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 366 | cs_mem_free(insn[i].detail); |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 367 | |
| 368 | // then free pointer to cs_insn array |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 369 | cs_mem_free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 373 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 374 | { |
| 375 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 376 | |
| 377 | if (!handle || handle->reg_name == NULL) { |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 381 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 382 | } |
| 383 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 384 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 385 | { |
| 386 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 387 | |
| 388 | if (!handle || handle->insn_name == NULL) { |
| 389 | return NULL; |
| 390 | } |
| 391 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 392 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 393 | } |
| 394 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 395 | static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 396 | { |
| 397 | int i; |
| 398 | |
| 399 | for (i = 0; i < max; i++) { |
| 400 | if (arr[i] == id) |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | return false; |
| 405 | } |
| 406 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 407 | bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 408 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 409 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 410 | return false; |
| 411 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 412 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 413 | if (!handle->detail) { |
| 414 | handle->errnum = CS_ERR_DETAIL; |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 419 | } |
| 420 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 421 | bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 422 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 423 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 424 | return false; |
| 425 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 426 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 427 | if (!handle->detail) { |
| 428 | handle->errnum = CS_ERR_DETAIL; |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 433 | } |
| 434 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 435 | bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 436 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 437 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 438 | return false; |
| 439 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 440 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 441 | if (!handle->detail) { |
| 442 | handle->errnum = CS_ERR_DETAIL; |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 450 | { |
| 451 | if (!ud) |
| 452 | return -1; |
| 453 | |
| 454 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame^] | 455 | if (!handle->detail) { |
| 456 | handle->errnum = CS_ERR_DETAIL; |
| 457 | return -1; |
| 458 | } |
| 459 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 460 | unsigned int count = 0, i; |
| 461 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 462 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 463 | |
| 464 | switch (handle->arch) { |
| 465 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 466 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 467 | return -1; |
| 468 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 469 | for (i = 0; i < insn->detail->arm.op_count; i++) |
| 470 | if (insn->detail->arm.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 471 | count++; |
| 472 | break; |
| 473 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 474 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
| 475 | if (insn->detail->arm64.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 476 | count++; |
| 477 | break; |
| 478 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 479 | for (i = 0; i < insn->detail->x86.op_count; i++) |
| 480 | if (insn->detail->x86.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 481 | count++; |
| 482 | break; |
| 483 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 484 | for (i = 0; i < insn->detail->mips.op_count; i++) |
| 485 | if (insn->detail->mips.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 486 | count++; |
| 487 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 488 | case CS_ARCH_PPC: |
| 489 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
| 490 | if (insn->detail->ppc.operands[i].type == op_type) |
| 491 | count++; |
| 492 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return count; |
| 496 | } |
| 497 | |
| 498 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 499 | unsigned int post) |
| 500 | { |
| 501 | if (!ud) |
| 502 | return -1; |
| 503 | |
| 504 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame^] | 505 | if (!handle->detail) { |
| 506 | handle->errnum = CS_ERR_DETAIL; |
| 507 | return -1; |
| 508 | } |
| 509 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 510 | unsigned int count = 0, i; |
| 511 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 512 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 513 | |
| 514 | switch (handle->arch) { |
| 515 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 516 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 517 | return -1; |
| 518 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 519 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
| 520 | if (insn->detail->arm.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 521 | count++; |
| 522 | if (count == post) |
| 523 | return i; |
| 524 | } |
| 525 | break; |
| 526 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 527 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
| 528 | if (insn->detail->arm64.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 529 | count++; |
| 530 | if (count == post) |
| 531 | return i; |
| 532 | } |
| 533 | break; |
| 534 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 535 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
| 536 | if (insn->detail->x86.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 537 | count++; |
| 538 | if (count == post) |
| 539 | return i; |
| 540 | } |
| 541 | break; |
| 542 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 543 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
| 544 | if (insn->detail->mips.operands[i].type == op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 545 | count++; |
| 546 | if (count == post) |
| 547 | return i; |
| 548 | } |
| 549 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 550 | case CS_ARCH_PPC: |
| 551 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
| 552 | if (insn->detail->ppc.operands[i].type == op_type) |
| 553 | count++; |
| 554 | if (count == post) |
| 555 | return i; |
| 556 | } |
| 557 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | return -1; |
| 561 | } |