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