Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Axel 0vercl0k Souchet | 605faf1 | 2014-05-09 20:40:00 +0100 | [diff] [blame] | 3 | #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64) |
| 4 | #pragma warning(disable:4996) |
| 5 | #endif |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 6 | #include <stddef.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <capstone.h> |
| 11 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 12 | #include "utils.h" |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 13 | #include "MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | |
Axel 0vercl0k Souchet | 84fecf2 | 2014-05-10 09:49:29 +0100 | [diff] [blame] | 15 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | 472a4a4 | 2014-03-06 09:13:04 +0800 | [diff] [blame] | 16 | #define INSN_CACHE_SIZE 32 |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 17 | #else |
Nguyen Anh Quynh | 5ee2b45 | 2014-03-07 08:40:35 +0800 | [diff] [blame] | 18 | // reduce stack variable size for kernel/firmware |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 19 | #define INSN_CACHE_SIZE 8 |
| 20 | #endif |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 21 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 22 | // default SKIPDATA mnemonic |
Nguyen Anh Quynh | c75a909 | 2014-04-10 10:26:49 +0800 | [diff] [blame] | 23 | #define SKIPDATA_MNEM ".byte" |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 24 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 25 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 26 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 27 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 28 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 29 | extern void ARM_enable(void); |
| 30 | extern void AArch64_enable(void); |
| 31 | extern void Mips_enable(void); |
| 32 | extern void X86_enable(void); |
| 33 | extern void PPC_enable(void); |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 34 | extern void Sparc_enable(void); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 35 | extern void SystemZ_enable(void); |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 36 | extern void XCore_enable(void); |
danghvu | 701b850 | 2014-01-09 11:06:44 +0700 | [diff] [blame] | 37 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 38 | static void archs_enable(void) |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 39 | { |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 40 | static bool initialized = false; |
| 41 | |
| 42 | if (initialized) |
| 43 | return; |
| 44 | |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 45 | #ifdef CAPSTONE_HAS_ARM |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 46 | ARM_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 47 | #endif |
| 48 | #ifdef CAPSTONE_HAS_ARM64 |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 49 | AArch64_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef CAPSTONE_HAS_MIPS |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 52 | Mips_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 53 | #endif |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 54 | #ifdef CAPSTONE_HAS_POWERPC |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 55 | PPC_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 56 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 57 | #ifdef CAPSTONE_HAS_SPARC |
| 58 | Sparc_enable(); |
| 59 | #endif |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 60 | #ifdef CAPSTONE_HAS_SYSZ |
| 61 | SystemZ_enable(); |
| 62 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 63 | #ifdef CAPSTONE_HAS_X86 |
| 64 | X86_enable(); |
| 65 | #endif |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 66 | #ifdef CAPSTONE_HAS_XCORE |
| 67 | XCore_enable(); |
| 68 | #endif |
| 69 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 70 | |
| 71 | initialized = true; |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 72 | } |
| 73 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 74 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 75 | |
Axel 0vercl0k Souchet | 84fecf2 | 2014-05-10 09:49:29 +0100 | [diff] [blame] | 76 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 77 | cs_malloc_t cs_mem_malloc = malloc; |
| 78 | cs_calloc_t cs_mem_calloc = calloc; |
| 79 | cs_realloc_t cs_mem_realloc = realloc; |
| 80 | cs_free_t cs_mem_free = free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 81 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 82 | #else |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 83 | cs_malloc_t cs_mem_malloc = NULL; |
| 84 | cs_calloc_t cs_mem_calloc = NULL; |
| 85 | cs_realloc_t cs_mem_realloc = NULL; |
| 86 | cs_free_t cs_mem_free = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 87 | cs_vsnprintf_t cs_vsnprintf = NULL; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 88 | #endif |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 89 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 90 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | b90cb99 | 2013-12-28 13:59:09 +0800 | [diff] [blame] | 91 | unsigned int cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 92 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 93 | archs_enable(); |
| 94 | |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 95 | if (major != NULL && minor != NULL) { |
| 96 | *major = CS_API_MAJOR; |
| 97 | *minor = CS_API_MINOR; |
| 98 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 99 | |
| 100 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 101 | } |
| 102 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 103 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 104 | bool cs_support(int query) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 105 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 106 | archs_enable(); |
| 107 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 108 | if (query == CS_ARCH_ALL) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 109 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 110 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 111 | (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) | |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 112 | (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 113 | |
Nguyen Anh Quynh | 48d5832 | 2014-02-27 13:54:28 +0800 | [diff] [blame] | 114 | if ((unsigned int)query < CS_ARCH_MAX) |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 115 | return all_arch & (1 << query); |
| 116 | |
| 117 | if (query == CS_SUPPORT_DIET) { |
| 118 | #ifdef CAPSTONE_DIET |
| 119 | return true; |
| 120 | #else |
| 121 | return false; |
| 122 | #endif |
| 123 | } |
| 124 | |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 125 | if (query == CS_SUPPORT_X86_REDUCE) { |
| 126 | #if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE) |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 127 | return true; |
| 128 | #else |
| 129 | return false; |
| 130 | #endif |
| 131 | } |
| 132 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 133 | // unsupported query |
| 134 | return false; |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 137 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 138 | cs_err cs_errno(csh handle) |
| 139 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 140 | struct cs_struct *ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 141 | if (!handle) |
| 142 | return CS_ERR_CSH; |
| 143 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 144 | ud = (struct cs_struct *)(uintptr_t)handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 145 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 146 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 149 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 150 | const char *cs_strerror(cs_err code) |
| 151 | { |
| 152 | switch(code) { |
| 153 | default: |
| 154 | return "Unknown error code"; |
| 155 | case CS_ERR_OK: |
| 156 | return "OK (CS_ERR_OK)"; |
| 157 | case CS_ERR_MEM: |
| 158 | return "Out of memory (CS_ERR_MEM)"; |
| 159 | case CS_ERR_ARCH: |
| 160 | return "Invalid architecture (CS_ERR_ARCH)"; |
| 161 | case CS_ERR_HANDLE: |
| 162 | return "Invalid handle (CS_ERR_HANDLE)"; |
| 163 | case CS_ERR_CSH: |
| 164 | return "Invalid csh (CS_ERR_CSH)"; |
| 165 | case CS_ERR_MODE: |
| 166 | return "Invalid mode (CS_ERR_MODE)"; |
| 167 | case CS_ERR_OPTION: |
| 168 | return "Invalid option (CS_ERR_OPTION)"; |
| 169 | case CS_ERR_DETAIL: |
| 170 | return "Details are unavailable (CS_ERR_DETAIL)"; |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 171 | case CS_ERR_MEMSETUP: |
| 172 | return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)"; |
Nguyen Anh Quynh | 4f22028 | 2014-02-09 00:19:15 +0800 | [diff] [blame] | 173 | case CS_ERR_VERSION: |
| 174 | return "Different API version between core & binding (CS_ERR_VERSION)"; |
Nguyen Anh Quynh | f7cdbdf | 2014-02-24 16:47:36 +0800 | [diff] [blame] | 175 | case CS_ERR_DIET: |
| 176 | return "Information irrelevant in diet engine (CS_ERR_DIET)"; |
Nguyen Anh Quynh | 11ec881 | 2014-04-10 17:20:01 +0800 | [diff] [blame] | 177 | case CS_ERR_SKIPDATA: |
| 178 | return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)"; |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 182 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 183 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 184 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 185 | cs_err err; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 186 | struct cs_struct *ud; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 187 | 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] | 188 | // Error: before cs_open(), dynamic memory management must be initialized |
| 189 | // with cs_option(CS_OPT_MEM) |
| 190 | return CS_ERR_MEMSETUP; |
| 191 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 192 | archs_enable(); |
| 193 | |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 194 | if (arch < CS_ARCH_MAX && arch_init[arch]) { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 195 | ud = cs_mem_calloc(1, sizeof(*ud)); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 196 | if (!ud) { |
| 197 | // memory insufficient |
| 198 | return CS_ERR_MEM; |
| 199 | } |
| 200 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 201 | ud->errnum = CS_ERR_OK; |
| 202 | ud->arch = arch; |
| 203 | ud->mode = mode; |
| 204 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 205 | // by default, do not break instruction into details |
| 206 | ud->detail = CS_OPT_OFF; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 207 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 208 | // default skipdata setup |
| 209 | ud->skipdata_setup.mnemonic = SKIPDATA_MNEM; |
| 210 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 211 | err = arch_init[ud->arch](ud); |
Nguyen Anh Quynh | 06b3c05 | 2014-01-21 15:26:02 +0800 | [diff] [blame] | 212 | if (err) { |
| 213 | cs_mem_free(ud); |
| 214 | *handle = 0; |
| 215 | return err; |
| 216 | } |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 217 | |
| 218 | *handle = (uintptr_t)ud; |
| 219 | |
| 220 | return CS_ERR_OK; |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 221 | } else { |
| 222 | *handle = 0; |
| 223 | return CS_ERR_ARCH; |
| 224 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 227 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 228 | cs_err cs_close(csh *handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 229 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 230 | struct cs_struct *ud; |
| 231 | |
Nguyen Anh Quynh | ef3d04d | 2014-02-27 23:42:49 +0800 | [diff] [blame] | 232 | if (*handle == 0) |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 233 | // invalid handle |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 234 | return CS_ERR_CSH; |
| 235 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 236 | ud = (struct cs_struct *)(*handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 237 | |
Nguyen Anh Quynh | fbe10a5 | 2014-02-27 23:59:08 +0800 | [diff] [blame] | 238 | if (ud->printer_info) |
| 239 | cs_mem_free(ud->printer_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 240 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 241 | cs_mem_free(ud->insn_cache); |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 242 | |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 243 | memset(ud, 0, sizeof(*ud)); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 244 | cs_mem_free(ud); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 245 | |
Nguyen Anh Quynh | c8e0785 | 2014-02-28 09:38:11 +0800 | [diff] [blame] | 246 | // invalidate this handle by ZERO out its value. |
| 247 | // this is to make sure it is unusable after cs_close() |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 248 | *handle = 0; |
| 249 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 250 | return CS_ERR_OK; |
| 251 | } |
| 252 | |
| 253 | // fill insn with mnemonic & operands info |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 254 | 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] | 255 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 256 | { |
Nguyen Anh Quynh | 8c2e2db | 2014-05-14 07:32:56 +0800 | [diff] [blame] | 257 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 258 | char *sp, *mnem; |
Nguyen Anh Quynh | 8c2e2db | 2014-05-14 07:32:56 +0800 | [diff] [blame] | 259 | #endif |
Nguyen Anh Quynh | 6c0dd63 | 2014-10-30 20:34:22 +0800 | [diff] [blame] | 260 | unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 261 | |
Nguyen Anh Quynh | 6c0dd63 | 2014-10-30 20:34:22 +0800 | [diff] [blame] | 262 | // fill the instruction bytes. |
| 263 | // we might skip some redundant bytes in front in the case of X86 |
| 264 | memcpy(insn->bytes, code + insn->size - copy_size, copy_size); |
| 265 | insn->size = copy_size; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 266 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 267 | // alias instruction might have ID saved in OpcodePub |
| 268 | if (MCInst_getOpcodePub(mci)) |
| 269 | insn->id = MCInst_getOpcodePub(mci); |
| 270 | |
| 271 | // post printer handles some corner cases (hacky) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 272 | if (postprinter) |
Nguyen Anh Quynh | 6456481 | 2014-05-19 16:46:31 +0800 | [diff] [blame] | 273 | postprinter((csh)handle, insn, buffer, mci); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 274 | |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 275 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 276 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 277 | // find first space or tab |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 278 | sp = buffer; |
Nguyen Anh Quynh | 7566a2d | 2014-06-08 22:09:31 +0800 | [diff] [blame] | 279 | mnem = insn->mnemonic; |
Nguyen Anh Quynh | 25b7f76 | 2014-07-02 12:22:39 +0800 | [diff] [blame] | 280 | for (sp = buffer; *sp; sp++) { |
| 281 | if (*sp == ' '|| *sp == '\t') |
| 282 | break; |
| 283 | if (*sp == '|') // lock|rep prefix for x86 |
| 284 | *sp = ' '; |
| 285 | // copy to @mnemonic |
| 286 | *mnem = *sp; |
| 287 | mnem++; |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Nguyen Anh Quynh | 7566a2d | 2014-06-08 22:09:31 +0800 | [diff] [blame] | 290 | *mnem = '\0'; |
| 291 | |
| 292 | // copy @op_str |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 293 | if (*sp) { |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 294 | // find the next non-space char |
| 295 | sp++; |
| 296 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 297 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 298 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 299 | } else |
| 300 | insn->op_str[0] = '\0'; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 301 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 304 | // how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)? |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 305 | // this very much depends on instruction alignment requirement of each arch. |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 306 | static uint8_t skipdata_size(cs_struct *handle) |
| 307 | { |
| 308 | switch(handle->arch) { |
| 309 | default: |
| 310 | // should never reach |
| 311 | return -1; |
| 312 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 313 | // skip 2 bytes on Thumb mode. |
| 314 | if (handle->mode & CS_MODE_THUMB) |
| 315 | return 2; |
| 316 | // otherwise, skip 4 bytes |
| 317 | return 4; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 318 | case CS_ARCH_ARM64: |
| 319 | case CS_ARCH_MIPS: |
| 320 | case CS_ARCH_PPC: |
| 321 | case CS_ARCH_SPARC: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 322 | // skip 4 bytes |
| 323 | return 4; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 324 | case CS_ARCH_SYSZ: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 325 | // SystemZ instruction's length can be 2, 4 or 6 bytes, |
| 326 | // so we just skip 2 bytes |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 327 | return 2; |
| 328 | case CS_ARCH_X86: |
| 329 | // X86 has no restriction on instruction alignment |
| 330 | return 1; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 331 | case CS_ARCH_XCORE: |
| 332 | // XCore instruction's length can be 2 or 4 bytes, |
| 333 | // so we just skip 2 bytes |
| 334 | return 2; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 338 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 339 | 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] | 340 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 341 | struct cs_struct *handle; |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 342 | archs_enable(); |
| 343 | |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 344 | // cs_option() can be called with NULL handle just for CS_OPT_MEM |
| 345 | // This is supposed to be executed before all other APIs (even cs_open()) |
| 346 | if (type == CS_OPT_MEM) { |
| 347 | cs_opt_mem *mem = (cs_opt_mem *)value; |
| 348 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 349 | cs_mem_malloc = mem->malloc; |
| 350 | cs_mem_calloc = mem->calloc; |
| 351 | cs_mem_realloc = mem->realloc; |
| 352 | cs_mem_free = mem->free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 353 | cs_vsnprintf = mem->vsnprintf; |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 354 | |
| 355 | return CS_ERR_OK; |
| 356 | } |
| 357 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 358 | handle = (struct cs_struct *)(uintptr_t)ud; |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 359 | if (!handle) |
| 360 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 361 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 362 | switch(type) { |
| 363 | default: |
| 364 | break; |
| 365 | case CS_OPT_DETAIL: |
| 366 | handle->detail = value; |
| 367 | return CS_ERR_OK; |
| 368 | case CS_OPT_SKIPDATA: |
| 369 | handle->skipdata = (value == CS_OPT_ON); |
| 370 | if (handle->skipdata) { |
| 371 | if (handle->skipdata_size == 0) { |
| 372 | // set the default skipdata size |
| 373 | handle->skipdata_size = skipdata_size(handle); |
| 374 | } |
| 375 | } |
| 376 | return CS_ERR_OK; |
| 377 | case CS_OPT_SKIPDATA_SETUP: |
| 378 | if (value) |
| 379 | handle->skipdata_setup = *((cs_opt_skipdata *)value); |
| 380 | return CS_ERR_OK; |
Nguyen Anh Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 381 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 382 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 383 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 384 | } |
| 385 | |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 386 | // generate @op_str for data instruction of SKIPDATA |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 387 | static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size) |
| 388 | { |
| 389 | char *p = opstr; |
| 390 | int len; |
| 391 | size_t i; |
| 392 | |
| 393 | if (!size) { |
| 394 | opstr[0] = '\0'; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | len = sprintf(p, "0x%02x", buffer[0]); |
| 399 | p+= len; |
| 400 | |
| 401 | for(i = 1; i < size; i++) { |
| 402 | len = sprintf(p, ", 0x%02x", buffer[i]); |
| 403 | p+= len; |
| 404 | } |
| 405 | } |
| 406 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 407 | // dynamicly allocate memory to contain disasm insn |
| 408 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 409 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 410 | size_t cs_disasm(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] | 411 | { |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 412 | struct cs_struct *handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 413 | MCInst mci; |
| 414 | uint16_t insn_size; |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 415 | size_t c = 0, i; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 416 | unsigned int f = 0; // index of the next instruction in the cache |
| 417 | cs_insn *insn_cache; // cache contains disassembled instructions |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 418 | void *total = NULL; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 419 | size_t total_size = 0; // total size of output buffer containing all insns |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 420 | bool r; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 421 | void *tmp; |
| 422 | size_t skipdata_bytes; |
Nguyen Anh Quynh | 523ca99 | 2014-10-01 10:46:37 +0800 | [diff] [blame] | 423 | uint64_t offset_org; // save all the original info of the buffer |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 424 | size_t size_org; |
| 425 | const uint8_t *buffer_org; |
Nguyen Anh Quynh | ac98ca0 | 2014-09-30 13:17:36 +0800 | [diff] [blame] | 426 | unsigned int cache_size = INSN_CACHE_SIZE; |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 427 | size_t next_offset; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 428 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 429 | handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 5848aaa | 2014-02-27 11:10:41 +0800 | [diff] [blame] | 430 | if (!handle) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 431 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 432 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 436 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 437 | |
Nguyen Anh Quynh | 50eeba2 | 2014-09-30 13:28:02 +0800 | [diff] [blame] | 438 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | 16f330c | 2014-10-02 10:09:59 +0800 | [diff] [blame] | 439 | if (count > 0 && count <= INSN_CACHE_SIZE) |
flyingsymbols | d91f964 | 2014-10-22 03:21:43 -0400 | [diff] [blame] | 440 | cache_size = (unsigned int) count; |
Nguyen Anh Quynh | 50eeba2 | 2014-09-30 13:28:02 +0800 | [diff] [blame] | 441 | #endif |
Nguyen Anh Quynh | ac98ca0 | 2014-09-30 13:17:36 +0800 | [diff] [blame] | 442 | |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 443 | // save the original offset for SKIPDATA |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 444 | buffer_org = buffer; |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 445 | offset_org = offset; |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 446 | size_org = size; |
Nguyen Anh Quynh | 523ca99 | 2014-10-01 10:46:37 +0800 | [diff] [blame] | 447 | |
| 448 | total_size = sizeof(cs_insn) * cache_size; |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 449 | total = cs_mem_malloc(total_size); |
| 450 | insn_cache = total; |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 451 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 452 | while (size > 0) { |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 453 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 454 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 455 | |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 456 | // relative branches need to know the address & size of current insn |
| 457 | mci.address = offset; |
| 458 | |
| 459 | if (handle->detail) { |
| 460 | // allocate memory for @detail pointer |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 461 | insn_cache->detail = cs_mem_malloc(sizeof(cs_detail)); |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 462 | } else { |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 463 | insn_cache->detail = NULL; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 467 | mci.flat_insn = insn_cache; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 468 | mci.flat_insn->address = offset; |
Nguyen Anh Quynh | 0c07cc9 | 2014-08-27 22:31:54 +0800 | [diff] [blame] | 469 | #ifdef CAPSTONE_DIET |
| 470 | // zero out mnemonic & op_str |
| 471 | mci.flat_insn->mnemonic[0] = '\0'; |
| 472 | mci.flat_insn->op_str[0] = '\0'; |
| 473 | #endif |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 474 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 475 | 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] | 476 | if (r) { |
| 477 | SStream ss; |
| 478 | SStream_Init(&ss); |
| 479 | |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 480 | mci.flat_insn->size = insn_size; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 481 | |
| 482 | // map internal instruction opcode to public insn ID |
| 483 | handle->insn_id(handle, insn_cache, mci.Opcode); |
| 484 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 485 | handle->printer(&mci, &ss, handle->printer_info); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 486 | |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 487 | fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 488 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 489 | next_offset = insn_size; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 490 | } else { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 491 | // encounter a broken instruction |
| 492 | |
| 493 | // free memory of @detail pointer |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 494 | if (handle->detail) { |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 495 | cs_mem_free(insn_cache->detail); |
| 496 | } |
| 497 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 498 | // if there is no request to skip data, or remaining data is too small, |
| 499 | // then bail out |
| 500 | if (!handle->skipdata || handle->skipdata_size > size) |
| 501 | break; |
| 502 | |
| 503 | if (handle->skipdata_setup.callback) { |
Nguyen Anh Quynh | 0df7e93 | 2014-07-10 15:42:16 +0800 | [diff] [blame] | 504 | skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org, |
Nguyen Anh Quynh | 4b6b15f | 2014-08-26 15:57:04 +0800 | [diff] [blame] | 505 | (size_t)(offset - offset_org), handle->skipdata_setup.user_data); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 506 | if (skipdata_bytes > size) |
| 507 | // remaining data is not enough |
| 508 | break; |
| 509 | |
| 510 | if (!skipdata_bytes) |
| 511 | // user requested not to skip data, so bail out |
| 512 | break; |
| 513 | } else |
| 514 | skipdata_bytes = handle->skipdata_size; |
| 515 | |
| 516 | // we have to skip some amount of data, depending on arch & mode |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 517 | insn_cache->id = 0; // invalid ID for this "data" instruction |
| 518 | insn_cache->address = offset; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 519 | insn_cache->size = (uint16_t)skipdata_bytes; |
Nguyen Anh Quynh | ee58394 | 2014-06-09 00:13:31 +0700 | [diff] [blame] | 520 | memcpy(insn_cache->bytes, buffer, skipdata_bytes); |
| 521 | strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic, |
| 522 | sizeof(insn_cache->mnemonic) - 1); |
| 523 | skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes); |
| 524 | insn_cache->detail = NULL; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 525 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 526 | next_offset = skipdata_bytes; |
| 527 | } |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 528 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 529 | // one more instruction entering the cache |
| 530 | f++; |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 531 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 532 | // one more instruction disassembled |
| 533 | c++; |
| 534 | if (count > 0 && c == count) |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 535 | // already got requested number of instructions |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 536 | break; |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 537 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 538 | if (f == cache_size) { |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 539 | // full cache, so expand the cache to contain incoming insns |
danghvu | 2fb7c8e | 2014-10-02 07:38:24 -0500 | [diff] [blame] | 540 | cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 541 | total_size += (sizeof(cs_insn) * cache_size); |
| 542 | tmp = cs_mem_realloc(total, total_size); |
| 543 | if (tmp == NULL) { // insufficient memory |
| 544 | if (handle->detail) { |
| 545 | insn_cache = (cs_insn *)total; |
| 546 | for (i = 0; i < c; i++, insn_cache++) |
| 547 | cs_mem_free(insn_cache->detail); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 548 | } |
| 549 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 550 | cs_mem_free(total); |
| 551 | *insn = NULL; |
| 552 | handle->errnum = CS_ERR_MEM; |
| 553 | return 0; |
| 554 | } |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 555 | |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 556 | total = tmp; |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 557 | // continue to fill in the cache after the last instruction |
| 558 | insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c); |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 559 | |
Nguyen Anh Quynh | 4d3ccf4 | 2014-10-03 00:39:56 +0800 | [diff] [blame] | 560 | // reset f back to 0, so we fill in the cache from begining |
danghvu | 0d1aad1 | 2014-10-01 23:12:18 -0500 | [diff] [blame] | 561 | f = 0; |
| 562 | } else |
| 563 | insn_cache++; |
| 564 | |
| 565 | buffer += next_offset; |
| 566 | size -= next_offset; |
| 567 | offset += next_offset; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 568 | } |
| 569 | |
Nguyen Anh Quynh | 27a4a08 | 2014-10-02 10:31:37 +0800 | [diff] [blame] | 570 | if (!c) { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 571 | // we did not disassemble any instruction |
Nguyen Anh Quynh | 27a4a08 | 2014-10-02 10:31:37 +0800 | [diff] [blame] | 572 | cs_mem_free(total); |
| 573 | total = NULL; |
| 574 | } else if (f != cache_size) { |
Nguyen Anh Quynh | f9d8a89 | 2014-10-02 12:37:32 +0800 | [diff] [blame] | 575 | // total did not fully use the last cache, so downsize it |
Nguyen Anh Quynh | ac98ca0 | 2014-09-30 13:17:36 +0800 | [diff] [blame] | 576 | void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 577 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 578 | // free all detail pointers |
| 579 | if (handle->detail) { |
| 580 | insn_cache = (cs_insn *)total; |
| 581 | for (i = 0; i < c; i++, insn_cache++) |
| 582 | cs_mem_free(insn_cache->detail); |
| 583 | } |
| 584 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 585 | cs_mem_free(total); |
Nguyen Anh Quynh | dab17fd | 2014-06-19 11:15:18 +0800 | [diff] [blame] | 586 | *insn = NULL; |
Nguyen Anh Quynh | 6c182ae | 2014-06-18 21:50:25 +0800 | [diff] [blame] | 587 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 588 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | total = tmp; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | *insn = total; |
| 596 | |
| 597 | return c; |
| 598 | } |
| 599 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 600 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 601 | CAPSTONE_DEPRECATED |
| 602 | size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
| 603 | { |
| 604 | return cs_disasm(ud, buffer, size, offset, count, insn); |
| 605 | } |
| 606 | |
| 607 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 608 | void cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 609 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 610 | size_t i; |
| 611 | |
| 612 | // free all detail pointers |
| 613 | for (i = 0; i < count; i++) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 614 | cs_mem_free(insn[i].detail); |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 615 | |
| 616 | // then free pointer to cs_insn array |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 617 | cs_mem_free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 618 | } |
| 619 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 620 | CAPSTONE_EXPORT |
| 621 | cs_insn *cs_malloc(csh ud) |
| 622 | { |
| 623 | cs_insn *insn; |
| 624 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
| 625 | |
| 626 | insn = cs_mem_malloc(sizeof(cs_insn)); |
| 627 | if (!insn) { |
| 628 | // insufficient memory |
| 629 | handle->errnum = CS_ERR_MEM; |
| 630 | return NULL; |
| 631 | } else { |
| 632 | if (handle->detail) { |
| 633 | // allocate memory for @detail pointer |
| 634 | insn->detail = cs_mem_malloc(sizeof(cs_detail)); |
| 635 | if (insn->detail == NULL) { // insufficient memory |
| 636 | cs_mem_free(insn); |
| 637 | handle->errnum = CS_ERR_MEM; |
| 638 | return NULL; |
| 639 | } |
| 640 | } else |
| 641 | insn->detail = NULL; |
| 642 | } |
| 643 | |
| 644 | return insn; |
| 645 | } |
| 646 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 647 | // iterator for instruction "single-stepping" |
| 648 | CAPSTONE_EXPORT |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 649 | bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, |
| 650 | uint64_t *address, cs_insn *insn) |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 651 | { |
| 652 | struct cs_struct *handle; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 653 | uint16_t insn_size; |
| 654 | MCInst mci; |
| 655 | bool r; |
| 656 | |
| 657 | handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 658 | if (!handle) { |
Nguyen Anh Quynh | 29ce6c3 | 2014-10-12 15:28:34 +0800 | [diff] [blame] | 659 | return false; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | handle->errnum = CS_ERR_OK; |
| 663 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 664 | MCInst_Init(&mci); |
| 665 | mci.csh = handle; |
| 666 | |
| 667 | // relative branches need to know the address & size of current insn |
| 668 | mci.address = *address; |
| 669 | |
| 670 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 671 | mci.flat_insn = insn; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 672 | mci.flat_insn->address = *address; |
| 673 | #ifdef CAPSTONE_DIET |
| 674 | // zero out mnemonic & op_str |
| 675 | mci.flat_insn->mnemonic[0] = '\0'; |
| 676 | mci.flat_insn->op_str[0] = '\0'; |
| 677 | #endif |
| 678 | |
| 679 | r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info); |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 680 | if (r) { |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 681 | SStream ss; |
| 682 | SStream_Init(&ss); |
| 683 | |
| 684 | mci.flat_insn->size = insn_size; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 685 | |
| 686 | // map internal instruction opcode to public insn ID |
| 687 | handle->insn_id(handle, insn, mci.Opcode); |
| 688 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 689 | handle->printer(&mci, &ss, handle->printer_info); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 690 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 691 | fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code); |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 692 | |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 693 | *code += insn_size; |
| 694 | *size -= insn_size; |
| 695 | *address += insn_size; |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 696 | } else { // encounter a broken instruction |
| 697 | size_t skipdata_bytes; |
| 698 | |
| 699 | // if there is no request to skip data, or remaining data is too small, |
| 700 | // then bail out |
| 701 | if (!handle->skipdata || handle->skipdata_size > *size) |
| 702 | return false; |
| 703 | |
| 704 | if (handle->skipdata_setup.callback) { |
| 705 | skipdata_bytes = handle->skipdata_setup.callback(*code, *size, |
| 706 | 0, handle->skipdata_setup.user_data); |
| 707 | if (skipdata_bytes > *size) |
| 708 | // remaining data is not enough |
| 709 | return false; |
| 710 | |
| 711 | if (!skipdata_bytes) |
| 712 | // user requested not to skip data, so bail out |
| 713 | return false; |
| 714 | } else |
| 715 | skipdata_bytes = handle->skipdata_size; |
| 716 | |
| 717 | // we have to skip some amount of data, depending on arch & mode |
| 718 | insn->id = 0; // invalid ID for this "data" instruction |
| 719 | insn->address = *address; |
| 720 | insn->size = (uint16_t)skipdata_bytes; |
| 721 | memcpy(insn->bytes, *code, skipdata_bytes); |
| 722 | strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic, |
| 723 | sizeof(insn->mnemonic) - 1); |
| 724 | skipdata_opstr(insn->op_str, *code, skipdata_bytes); |
| 725 | |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 726 | *code += skipdata_bytes; |
| 727 | *size -= skipdata_bytes; |
| 728 | *address += skipdata_bytes; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 729 | } |
Nguyen Anh Quynh | 0a2eca7 | 2014-10-11 00:36:16 +0800 | [diff] [blame] | 730 | |
| 731 | return true; |
hlide | 993f362 | 2014-10-05 18:14:40 +0200 | [diff] [blame] | 732 | } |
| 733 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 734 | // return friendly name of regiser in a string |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 735 | CAPSTONE_EXPORT |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 736 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 737 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 738 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 739 | |
| 740 | if (!handle || handle->reg_name == NULL) { |
| 741 | return NULL; |
| 742 | } |
| 743 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 744 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 745 | } |
| 746 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 747 | CAPSTONE_EXPORT |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 748 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 749 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 750 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 751 | |
| 752 | if (!handle || handle->insn_name == NULL) { |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 756 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 757 | } |
| 758 | |
Nguyen Anh Quynh | 650f96c | 2014-07-08 08:59:27 +0800 | [diff] [blame] | 759 | CAPSTONE_EXPORT |
| 760 | const char *cs_group_name(csh ud, unsigned int group) |
| 761 | { |
| 762 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
| 763 | |
| 764 | if (!handle || handle->group_name == NULL) { |
| 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | return handle->group_name(ud, group); |
| 769 | } |
| 770 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 771 | 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] | 772 | { |
| 773 | int i; |
| 774 | |
| 775 | for (i = 0; i < max; i++) { |
| 776 | if (arr[i] == id) |
| 777 | return true; |
| 778 | } |
| 779 | |
| 780 | return false; |
| 781 | } |
| 782 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 783 | CAPSTONE_EXPORT |
obs | 876b6b6 | 2014-08-21 00:57:04 +0200 | [diff] [blame] | 784 | bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 785 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 786 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 787 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 788 | return false; |
| 789 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 790 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 791 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 792 | if (!handle->detail) { |
| 793 | handle->errnum = CS_ERR_DETAIL; |
| 794 | return false; |
| 795 | } |
| 796 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 797 | if(!insn->id) { |
| 798 | handle->errnum = CS_ERR_SKIPDATA; |
| 799 | return false; |
| 800 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 801 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 802 | if(!insn->detail) { |
| 803 | handle->errnum = CS_ERR_DETAIL; |
| 804 | return false; |
| 805 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 806 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 807 | 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] | 808 | } |
| 809 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 810 | CAPSTONE_EXPORT |
obs | 876b6b6 | 2014-08-21 00:57:04 +0200 | [diff] [blame] | 811 | bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 812 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 813 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 814 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 815 | return false; |
| 816 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 817 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 818 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 819 | if (!handle->detail) { |
| 820 | handle->errnum = CS_ERR_DETAIL; |
| 821 | return false; |
| 822 | } |
| 823 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 824 | if(!insn->id) { |
| 825 | handle->errnum = CS_ERR_SKIPDATA; |
| 826 | return false; |
| 827 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 828 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 829 | if(!insn->detail) { |
| 830 | handle->errnum = CS_ERR_DETAIL; |
| 831 | return false; |
| 832 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 833 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 834 | 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] | 835 | } |
| 836 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 837 | CAPSTONE_EXPORT |
obs | 876b6b6 | 2014-08-21 00:57:04 +0200 | [diff] [blame] | 838 | bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 839 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 840 | struct cs_struct *handle; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 841 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 842 | return false; |
| 843 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 844 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 845 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 846 | if (!handle->detail) { |
| 847 | handle->errnum = CS_ERR_DETAIL; |
| 848 | return false; |
| 849 | } |
| 850 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 851 | if(!insn->id) { |
| 852 | handle->errnum = CS_ERR_SKIPDATA; |
| 853 | return false; |
| 854 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 855 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 856 | if(!insn->detail) { |
| 857 | handle->errnum = CS_ERR_DETAIL; |
| 858 | return false; |
| 859 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 860 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 861 | 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] | 862 | } |
| 863 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 864 | CAPSTONE_EXPORT |
obs | 876b6b6 | 2014-08-21 00:57:04 +0200 | [diff] [blame] | 865 | int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 866 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 867 | struct cs_struct *handle; |
| 868 | unsigned int count = 0, i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 869 | if (!ud) |
| 870 | return -1; |
| 871 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 872 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 873 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 874 | if (!handle->detail) { |
| 875 | handle->errnum = CS_ERR_DETAIL; |
| 876 | return -1; |
| 877 | } |
| 878 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 879 | if(!insn->id) { |
| 880 | handle->errnum = CS_ERR_SKIPDATA; |
| 881 | return -1; |
| 882 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 883 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 884 | if(!insn->detail) { |
| 885 | handle->errnum = CS_ERR_DETAIL; |
| 886 | return -1; |
| 887 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 888 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 889 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 890 | |
| 891 | switch (handle->arch) { |
| 892 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 893 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 894 | return -1; |
| 895 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 896 | for (i = 0; i < insn->detail->arm.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 897 | 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] | 898 | count++; |
| 899 | break; |
| 900 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 901 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 902 | 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] | 903 | count++; |
| 904 | break; |
| 905 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 906 | for (i = 0; i < insn->detail->x86.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 907 | 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] | 908 | count++; |
| 909 | break; |
| 910 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 911 | for (i = 0; i < insn->detail->mips.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 912 | 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] | 913 | count++; |
| 914 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 915 | case CS_ARCH_PPC: |
| 916 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 917 | 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] | 918 | count++; |
| 919 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 920 | case CS_ARCH_SPARC: |
| 921 | for (i = 0; i < insn->detail->sparc.op_count; i++) |
| 922 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 923 | count++; |
| 924 | break; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 925 | case CS_ARCH_SYSZ: |
| 926 | for (i = 0; i < insn->detail->sysz.op_count; i++) |
| 927 | if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type) |
| 928 | count++; |
| 929 | break; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 930 | case CS_ARCH_XCORE: |
| 931 | for (i = 0; i < insn->detail->xcore.op_count; i++) |
| 932 | if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type) |
| 933 | count++; |
| 934 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | return count; |
| 938 | } |
| 939 | |
Nguyen Anh Quynh | 07c3693 | 2014-06-03 18:33:15 +0800 | [diff] [blame] | 940 | CAPSTONE_EXPORT |
obs | 876b6b6 | 2014-08-21 00:57:04 +0200 | [diff] [blame] | 941 | int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 942 | unsigned int post) |
| 943 | { |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 944 | struct cs_struct *handle; |
| 945 | unsigned int count = 0, i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 946 | if (!ud) |
| 947 | return -1; |
| 948 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 949 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 950 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 951 | if (!handle->detail) { |
| 952 | handle->errnum = CS_ERR_DETAIL; |
| 953 | return -1; |
| 954 | } |
| 955 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 956 | if(!insn->id) { |
| 957 | handle->errnum = CS_ERR_SKIPDATA; |
| 958 | return -1; |
| 959 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 960 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 961 | if(!insn->detail) { |
| 962 | handle->errnum = CS_ERR_DETAIL; |
| 963 | return -1; |
| 964 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 965 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 966 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 967 | |
| 968 | switch (handle->arch) { |
| 969 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 970 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 971 | return -1; |
| 972 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 973 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 974 | 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] | 975 | count++; |
| 976 | if (count == post) |
| 977 | return i; |
| 978 | } |
| 979 | break; |
| 980 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 981 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 982 | 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] | 983 | count++; |
| 984 | if (count == post) |
| 985 | return i; |
| 986 | } |
| 987 | break; |
| 988 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 989 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 990 | 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] | 991 | count++; |
| 992 | if (count == post) |
| 993 | return i; |
| 994 | } |
| 995 | break; |
| 996 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 997 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 998 | 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] | 999 | count++; |
| 1000 | if (count == post) |
| 1001 | return i; |
| 1002 | } |
| 1003 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 1004 | case CS_ARCH_PPC: |
| 1005 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 1006 | 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] | 1007 | count++; |
| 1008 | if (count == post) |
| 1009 | return i; |
| 1010 | } |
| 1011 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 1012 | case CS_ARCH_SPARC: |
| 1013 | for (i = 0; i < insn->detail->sparc.op_count; i++) { |
| 1014 | if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type) |
| 1015 | count++; |
| 1016 | if (count == post) |
| 1017 | return i; |
| 1018 | } |
| 1019 | break; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 1020 | case CS_ARCH_SYSZ: |
| 1021 | for (i = 0; i < insn->detail->sysz.op_count; i++) { |
| 1022 | if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type) |
| 1023 | count++; |
| 1024 | if (count == post) |
| 1025 | return i; |
| 1026 | } |
| 1027 | break; |
Nguyen Anh Quynh | c80d840 | 2014-05-26 23:02:48 +0800 | [diff] [blame] | 1028 | case CS_ARCH_XCORE: |
| 1029 | for (i = 0; i < insn->detail->xcore.op_count; i++) { |
| 1030 | if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type) |
| 1031 | count++; |
| 1032 | if (count == post) |
| 1033 | return i; |
| 1034 | } |
| 1035 | break; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | return -1; |
| 1039 | } |