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