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