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