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