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