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