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