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