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 | 5ee2b45 | 2014-03-07 08:40:35 +0800 | [diff] [blame] | 13 | #ifdef USE_SYS_DYN_MEM |
Nguyen Anh Quynh | 472a4a4 | 2014-03-06 09:13:04 +0800 | [diff] [blame] | 14 | #define INSN_CACHE_SIZE 32 |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 15 | #else |
Nguyen Anh Quynh | 5ee2b45 | 2014-03-07 08:40:35 +0800 | [diff] [blame] | 16 | // reduce stack variable size for kernel/firmware |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 17 | #define INSN_CACHE_SIZE 8 |
| 18 | #endif |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 19 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 20 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 21 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 22 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 23 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 24 | extern void ARM_enable(void); |
| 25 | extern void AArch64_enable(void); |
| 26 | extern void Mips_enable(void); |
| 27 | extern void X86_enable(void); |
| 28 | extern void PPC_enable(void); |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame^] | 29 | extern void Sparc_enable(void); |
danghvu | 701b850 | 2014-01-09 11:06:44 +0700 | [diff] [blame] | 30 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 31 | static void archs_enable(void) |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 32 | { |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 33 | static bool initialized = false; |
| 34 | |
| 35 | if (initialized) |
| 36 | return; |
| 37 | |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 38 | #ifdef CAPSTONE_HAS_ARM |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 39 | ARM_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 40 | #endif |
| 41 | #ifdef CAPSTONE_HAS_ARM64 |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 42 | AArch64_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 43 | #endif |
| 44 | #ifdef CAPSTONE_HAS_MIPS |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 45 | Mips_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 46 | #endif |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 47 | #ifdef CAPSTONE_HAS_POWERPC |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 48 | PPC_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 49 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame^] | 50 | #ifdef CAPSTONE_HAS_SPARC |
| 51 | Sparc_enable(); |
| 52 | #endif |
| 53 | #ifdef CAPSTONE_HAS_X86 |
| 54 | X86_enable(); |
| 55 | #endif |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 56 | |
| 57 | initialized = true; |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 58 | } |
| 59 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 60 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 61 | |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 62 | #ifdef USE_SYS_DYN_MEM |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 63 | cs_malloc_t cs_mem_malloc = malloc; |
| 64 | cs_calloc_t cs_mem_calloc = calloc; |
| 65 | cs_realloc_t cs_mem_realloc = realloc; |
| 66 | cs_free_t cs_mem_free = free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 67 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 68 | #else |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 69 | cs_malloc_t cs_mem_malloc = NULL; |
| 70 | cs_calloc_t cs_mem_calloc = NULL; |
| 71 | cs_realloc_t cs_mem_realloc = NULL; |
| 72 | cs_free_t cs_mem_free = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 73 | cs_vsnprintf_t cs_vsnprintf = NULL; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 74 | #endif |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 75 | |
Nguyen Anh Quynh | b90cb99 | 2013-12-28 13:59:09 +0800 | [diff] [blame] | 76 | unsigned int cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 77 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 78 | archs_enable(); |
| 79 | |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 80 | if (major != NULL && minor != NULL) { |
| 81 | *major = CS_API_MAJOR; |
| 82 | *minor = CS_API_MINOR; |
| 83 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 84 | |
| 85 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 86 | } |
| 87 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 88 | bool cs_support(int query) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 89 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 90 | archs_enable(); |
| 91 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 92 | if (query == CS_ARCH_ALL) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 93 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 94 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame^] | 95 | (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 96 | |
Nguyen Anh Quynh | 48d5832 | 2014-02-27 13:54:28 +0800 | [diff] [blame] | 97 | if ((unsigned int)query < CS_ARCH_MAX) |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 98 | return all_arch & (1 << query); |
| 99 | |
| 100 | if (query == CS_SUPPORT_DIET) { |
| 101 | #ifdef CAPSTONE_DIET |
| 102 | return true; |
| 103 | #else |
| 104 | return false; |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | // unsupported query |
| 109 | return false; |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 110 | } |
| 111 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 112 | cs_err cs_errno(csh handle) |
| 113 | { |
| 114 | if (!handle) |
| 115 | return CS_ERR_CSH; |
| 116 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 117 | struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 118 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 119 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 122 | const char *cs_strerror(cs_err code) |
| 123 | { |
| 124 | switch(code) { |
| 125 | default: |
| 126 | return "Unknown error code"; |
| 127 | case CS_ERR_OK: |
| 128 | return "OK (CS_ERR_OK)"; |
| 129 | case CS_ERR_MEM: |
| 130 | return "Out of memory (CS_ERR_MEM)"; |
| 131 | case CS_ERR_ARCH: |
| 132 | return "Invalid architecture (CS_ERR_ARCH)"; |
| 133 | case CS_ERR_HANDLE: |
| 134 | return "Invalid handle (CS_ERR_HANDLE)"; |
| 135 | case CS_ERR_CSH: |
| 136 | return "Invalid csh (CS_ERR_CSH)"; |
| 137 | case CS_ERR_MODE: |
| 138 | return "Invalid mode (CS_ERR_MODE)"; |
| 139 | case CS_ERR_OPTION: |
| 140 | return "Invalid option (CS_ERR_OPTION)"; |
| 141 | case CS_ERR_DETAIL: |
| 142 | return "Details are unavailable (CS_ERR_DETAIL)"; |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 143 | case CS_ERR_MEMSETUP: |
| 144 | return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)"; |
Nguyen Anh Quynh | 4f22028 | 2014-02-09 00:19:15 +0800 | [diff] [blame] | 145 | case CS_ERR_VERSION: |
| 146 | return "Different API version between core & binding (CS_ERR_VERSION)"; |
Nguyen Anh Quynh | f7cdbdf | 2014-02-24 16:47:36 +0800 | [diff] [blame] | 147 | case CS_ERR_DIET: |
| 148 | return "Information irrelevant in diet engine (CS_ERR_DIET)"; |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 152 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 153 | { |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 154 | if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf) |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 155 | // Error: before cs_open(), dynamic memory management must be initialized |
| 156 | // with cs_option(CS_OPT_MEM) |
| 157 | return CS_ERR_MEMSETUP; |
| 158 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 159 | archs_enable(); |
| 160 | |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 161 | if (arch < CS_ARCH_MAX && arch_init[arch]) { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 162 | struct cs_struct *ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 163 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 164 | ud = cs_mem_calloc(1, sizeof(*ud)); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 165 | if (!ud) { |
| 166 | // memory insufficient |
| 167 | return CS_ERR_MEM; |
| 168 | } |
| 169 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 170 | ud->errnum = CS_ERR_OK; |
| 171 | ud->arch = arch; |
| 172 | ud->mode = mode; |
| 173 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 174 | // by default, do not break instruction into details |
| 175 | ud->detail = CS_OPT_OFF; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 176 | |
Nguyen Anh Quynh | 06b3c05 | 2014-01-21 15:26:02 +0800 | [diff] [blame] | 177 | cs_err err = arch_init[ud->arch](ud); |
| 178 | if (err) { |
| 179 | cs_mem_free(ud); |
| 180 | *handle = 0; |
| 181 | return err; |
| 182 | } |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 183 | |
| 184 | *handle = (uintptr_t)ud; |
| 185 | |
| 186 | return CS_ERR_OK; |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 187 | } else { |
| 188 | *handle = 0; |
| 189 | return CS_ERR_ARCH; |
| 190 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 193 | cs_err cs_close(csh *handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 194 | { |
Nguyen Anh Quynh | ef3d04d | 2014-02-27 23:42:49 +0800 | [diff] [blame] | 195 | if (*handle == 0) |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 196 | // invalid handle |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 197 | return CS_ERR_CSH; |
| 198 | |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 199 | struct cs_struct *ud = (struct cs_struct *)(*handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 200 | |
Nguyen Anh Quynh | fbe10a5 | 2014-02-27 23:59:08 +0800 | [diff] [blame] | 201 | if (ud->printer_info) |
| 202 | cs_mem_free(ud->printer_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 203 | |
Nguyen Anh Quynh | 9fac512 | 2014-01-07 10:56:04 +0800 | [diff] [blame] | 204 | // arch_destroy[ud->arch](ud); |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 205 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 206 | cs_mem_free(ud->insn_cache); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 207 | memset(ud, 0, sizeof(*ud)); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 208 | cs_mem_free(ud); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 209 | |
Nguyen Anh Quynh | c8e0785 | 2014-02-28 09:38:11 +0800 | [diff] [blame] | 210 | // invalidate this handle by ZERO out its value. |
| 211 | // this is to make sure it is unusable after cs_close() |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 212 | *handle = 0; |
| 213 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 214 | return CS_ERR_OK; |
| 215 | } |
| 216 | |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 217 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 218 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 219 | // fill insn with mnemonic & operands info |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 220 | static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 221 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 222 | { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 223 | if (handle->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 224 | // avoiding copy insn->detail |
| 225 | memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 226 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 227 | // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8 |
| 228 | // copy from @regs_read until @arm |
| 229 | memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read), |
| 230 | offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read)); |
| 231 | // then copy from @arm until end |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 232 | memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)), |
| 233 | (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)), |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 234 | sizeof(cs_detail) - offsetof(cs_detail, arm)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 235 | } else { |
| 236 | insn->address = mci->address; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 237 | insn->size = (uint16_t)mci->insn_size; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 238 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 239 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 240 | // fill the instruction bytes |
| 241 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
| 242 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 243 | // map internal instruction opcode to public insn ID |
| 244 | if (handle->insn_id) |
Nguyen Anh Quynh | 1acfd0b | 2014-01-06 10:56:59 +0800 | [diff] [blame] | 245 | handle->insn_id(handle, insn, MCInst_getOpcode(mci)); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 246 | |
| 247 | // alias instruction might have ID saved in OpcodePub |
| 248 | if (MCInst_getOpcodePub(mci)) |
| 249 | insn->id = MCInst_getOpcodePub(mci); |
| 250 | |
| 251 | // post printer handles some corner cases (hacky) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 252 | if (postprinter) |
| 253 | postprinter((csh)handle, insn, buffer); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 254 | |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 255 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 256 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 257 | // find first space or tab |
| 258 | char *sp = buffer; |
| 259 | for (sp = buffer; *sp; sp++) |
| 260 | if (*sp == ' '||*sp == '\t') |
| 261 | break; |
| 262 | if (*sp) { |
| 263 | *sp = '\0'; |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 264 | // find the next non-space char |
| 265 | sp++; |
| 266 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 267 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 268 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 269 | } else |
| 270 | insn->op_str[0] = '\0'; |
| 271 | |
| 272 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
| 273 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 274 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 277 | 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] | 278 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 279 | archs_enable(); |
| 280 | |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 281 | // cs_option() can be called with NULL handle just for CS_OPT_MEM |
| 282 | // This is supposed to be executed before all other APIs (even cs_open()) |
| 283 | if (type == CS_OPT_MEM) { |
| 284 | cs_opt_mem *mem = (cs_opt_mem *)value; |
| 285 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 286 | cs_mem_malloc = mem->malloc; |
| 287 | cs_mem_calloc = mem->calloc; |
| 288 | cs_mem_realloc = mem->realloc; |
| 289 | cs_mem_free = mem->free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 290 | cs_vsnprintf = mem->vsnprintf; |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 291 | |
| 292 | return CS_ERR_OK; |
| 293 | } |
| 294 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 295 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 296 | if (!handle) |
| 297 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 298 | |
danghvu | 0b6ea04 | 2013-12-19 23:07:26 -0600 | [diff] [blame] | 299 | if (type == CS_OPT_DETAIL) { |
Nguyen Anh Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 300 | handle->detail = value; |
| 301 | return CS_ERR_OK; |
| 302 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 303 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 304 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 307 | // get previous instruction, which can be in the cache, or in total buffer |
| 308 | static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size) |
| 309 | { |
| 310 | if (f == 0) { |
| 311 | if (total == NULL) |
| 312 | return NULL; |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 313 | // get the trailing insn from total buffer, which is at |
| 314 | // the end of the latest cache trunk |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 315 | return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn))); |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 316 | } else |
| 317 | return &cache[f - 1]; |
| 318 | } |
| 319 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 320 | // dynamicly allocate memory to contain disasm insn |
| 321 | // 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] | 322 | 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] | 323 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 324 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 325 | MCInst mci; |
| 326 | uint16_t insn_size; |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 327 | size_t c = 0; |
| 328 | unsigned int f = 0; |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 329 | cs_insn insn_cache[INSN_CACHE_SIZE]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 330 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 331 | size_t total_size = 0; |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 332 | bool r; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 333 | |
Nguyen Anh Quynh | 5848aaa | 2014-02-27 11:10:41 +0800 | [diff] [blame] | 334 | if (!handle) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 335 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 336 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 340 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 341 | |
Nguyen Anh Quynh | 11b0519 | 2014-01-22 11:04:25 +0800 | [diff] [blame] | 342 | // reset previous prefix for X86 |
| 343 | handle->prev_prefix = 0; |
| 344 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 345 | memset(insn_cache, 0, sizeof(insn_cache)); |
| 346 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 347 | while (size > 0) { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 348 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 349 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 350 | |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 351 | r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 352 | if (r) { |
| 353 | SStream ss; |
| 354 | SStream_Init(&ss); |
| 355 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 356 | // relative branches need to know the address & size of current insn |
| 357 | mci.insn_size = insn_size; |
| 358 | mci.address = offset; |
| 359 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 360 | if (handle->detail) { |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 361 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 362 | mci.flat_insn.address = offset; |
| 363 | mci.flat_insn.size = insn_size; |
| 364 | // allocate memory for @detail pointer |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 365 | insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 368 | handle->printer(&mci, &ss, handle->printer_info); |
| 369 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 370 | 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] | 371 | |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 372 | if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) { |
| 373 | f++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 374 | |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 375 | if (f == ARR_SIZE(insn_cache)) { |
| 376 | // resize total to contain newly disasm insns |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 377 | void *tmp; |
| 378 | |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 379 | total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE); |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 380 | tmp = cs_mem_realloc(total, total_size); |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 381 | if (tmp == NULL) { // insufficient memory |
| 382 | cs_mem_free(total); |
| 383 | handle->errnum = CS_ERR_MEM; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | total = tmp; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 388 | memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache)); |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 389 | |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 390 | // reset f back to 0 |
| 391 | f = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 394 | c++; |
| 395 | } else { |
Nguyen Anh Quynh | 94020d8 | 2014-01-25 14:22:15 +0800 | [diff] [blame] | 396 | // combine this instruction with previous prefix "instruction" |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 397 | cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size); |
| 398 | handle->combine(handle, &insn_cache[f], prev); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 401 | buffer += insn_size; |
| 402 | size -= insn_size; |
| 403 | offset += insn_size; |
| 404 | |
Nguyen Anh Quynh | 94020d8 | 2014-01-25 14:22:15 +0800 | [diff] [blame] | 405 | if (count > 0) { |
| 406 | // x86 hacky |
| 407 | if (!handle->prev_prefix) { |
| 408 | if (c == count) |
| 409 | break; |
| 410 | } else { |
| 411 | // only combine 1 prefix with regular instruction |
Nguyen Anh Quynh | 8ce50e4 | 2014-01-27 18:15:28 +0800 | [diff] [blame] | 412 | if (c == count + 1) { |
| 413 | // the last insn is redundant |
| 414 | c--; |
| 415 | f--; |
| 416 | // free allocated detail pointer of the last redundant instruction |
| 417 | if (handle->detail) |
| 418 | cs_mem_free(insn_cache[f].detail); |
| 419 | |
Nguyen Anh Quynh | 94020d8 | 2014-01-25 14:22:15 +0800 | [diff] [blame] | 420 | break; |
Nguyen Anh Quynh | 8ce50e4 | 2014-01-27 18:15:28 +0800 | [diff] [blame] | 421 | } |
Nguyen Anh Quynh | 94020d8 | 2014-01-25 14:22:15 +0800 | [diff] [blame] | 422 | } |
| 423 | } |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 424 | } else { |
| 425 | // encounter a broken instruction |
| 426 | // XXX: TODO: JOXEAN continue here |
| 427 | break; |
| 428 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | if (f) { |
| 432 | // resize total to contain newly disasm insns |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 433 | void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0])); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 434 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 435 | cs_mem_free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 436 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | total = tmp; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 441 | memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0])); |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 442 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | *insn = total; |
| 446 | |
| 447 | return c; |
| 448 | } |
| 449 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 450 | void cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 451 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 452 | size_t i; |
| 453 | |
| 454 | // free all detail pointers |
| 455 | for (i = 0; i < count; i++) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 456 | cs_mem_free(insn[i].detail); |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 457 | |
| 458 | // then free pointer to cs_insn array |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 459 | cs_mem_free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 463 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 464 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 465 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 466 | |
| 467 | if (!handle || handle->reg_name == NULL) { |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 471 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 472 | } |
| 473 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 474 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 475 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 476 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 477 | |
| 478 | if (!handle || handle->insn_name == NULL) { |
| 479 | return NULL; |
| 480 | } |
| 481 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 482 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 483 | } |
| 484 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 485 | 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] | 486 | { |
| 487 | int i; |
| 488 | |
| 489 | for (i = 0; i < max; i++) { |
| 490 | if (arr[i] == id) |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | return false; |
| 495 | } |
| 496 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 497 | 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] | 498 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 499 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 500 | return false; |
| 501 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 502 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 503 | if (!handle->detail) { |
| 504 | handle->errnum = CS_ERR_DETAIL; |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | 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] | 509 | } |
| 510 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 511 | 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] | 512 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 513 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 514 | return false; |
| 515 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 516 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 517 | if (!handle->detail) { |
| 518 | handle->errnum = CS_ERR_DETAIL; |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | 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] | 523 | } |
| 524 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 525 | 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] | 526 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 527 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 528 | return false; |
| 529 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 530 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 531 | if (!handle->detail) { |
| 532 | handle->errnum = CS_ERR_DETAIL; |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | 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] | 537 | } |
| 538 | |
| 539 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 540 | { |
| 541 | if (!ud) |
| 542 | return -1; |
| 543 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 544 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 545 | if (!handle->detail) { |
| 546 | handle->errnum = CS_ERR_DETAIL; |
| 547 | return -1; |
| 548 | } |
| 549 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 550 | unsigned int count = 0, i; |
| 551 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 552 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 553 | |
| 554 | switch (handle->arch) { |
| 555 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 556 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 557 | return -1; |
| 558 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 559 | for (i = 0; i < insn->detail->arm.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 560 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 561 | count++; |
| 562 | break; |
| 563 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 564 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 565 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 566 | count++; |
| 567 | break; |
| 568 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 569 | for (i = 0; i < insn->detail->x86.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 570 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 571 | count++; |
| 572 | break; |
| 573 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 574 | for (i = 0; i < insn->detail->mips.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 575 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 576 | count++; |
| 577 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 578 | case CS_ARCH_PPC: |
| 579 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 580 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 581 | count++; |
| 582 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame^] | 583 | case CS_ARCH_SPARC: |
| 584 | for (i = 0; i < insn->detail->sparc.op_count; i++) |
| 585 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 586 | count++; |
| 587 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | return count; |
| 591 | } |
| 592 | |
| 593 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 594 | unsigned int post) |
| 595 | { |
| 596 | if (!ud) |
| 597 | return -1; |
| 598 | |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 599 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 600 | if (!handle->detail) { |
| 601 | handle->errnum = CS_ERR_DETAIL; |
| 602 | return -1; |
| 603 | } |
| 604 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 605 | unsigned int count = 0, i; |
| 606 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 607 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 608 | |
| 609 | switch (handle->arch) { |
| 610 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 611 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 612 | return -1; |
| 613 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 614 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 615 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 616 | count++; |
| 617 | if (count == post) |
| 618 | return i; |
| 619 | } |
| 620 | break; |
| 621 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 622 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 623 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 624 | count++; |
| 625 | if (count == post) |
| 626 | return i; |
| 627 | } |
| 628 | break; |
| 629 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 630 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 631 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 632 | count++; |
| 633 | if (count == post) |
| 634 | return i; |
| 635 | } |
| 636 | break; |
| 637 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 638 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 639 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 640 | count++; |
| 641 | if (count == post) |
| 642 | return i; |
| 643 | } |
| 644 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 645 | case CS_ARCH_PPC: |
| 646 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 647 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 648 | count++; |
| 649 | if (count == post) |
| 650 | return i; |
| 651 | } |
| 652 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame^] | 653 | case CS_ARCH_SPARC: |
| 654 | for (i = 0; i < insn->detail->sparc.op_count; i++) { |
| 655 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 656 | count++; |
| 657 | if (count == post) |
| 658 | return i; |
| 659 | } |
| 660 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return -1; |
| 664 | } |