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