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