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