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