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