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