blob: 5a79ddcd230b58ac35def79f75b405b65d9c68b5 [file] [log] [blame]
Nguyen Anh Quynhae3649f2014-01-02 13:15:07 +08001#ifndef CAPSTONE_ENGINE_H
2#define CAPSTONE_ENGINE_H
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
Nguyen Anh Quynh7751fbe2014-04-28 11:23:14 +08004/* Capstone Disassembly Engine */
5/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08006
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include <stdint.h>
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080012#include <stdarg.h>
reverserbcf09f42015-04-09 18:28:19 +010013#if defined(CAPSTONE_HAS_OSXKERNEL)
14#include <libkern/libkern.h>
15#else
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080016#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010017#include <stdio.h>
18#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Nguyen Anh Quynh171eb752014-05-15 21:01:08 +080020#include "platform.h"
21
Alex Ionescu46018db2014-01-22 09:45:00 -080022#ifdef _MSC_VER
Nguyen Anh Quynha18abdd2014-10-29 21:32:06 +080023#pragma warning(disable:4201)
24#pragma warning(disable:4100)
25#ifdef CAPSTONE_SHARED
26#define CAPSTONE_EXPORT __declspec(dllexport)
27#else // defined(CAPSTONE_STATIC)
28#define CAPSTONE_EXPORT
29#endif
Ali Rizvi-Santiago10053ba2014-06-03 21:04:23 +000030#else
Hilko Bengend83bf842015-02-26 18:37:32 +010031#ifdef __GNUC__
32#define CAPSTONE_EXPORT __attribute__((visibility("default")))
33#else
Nguyen Anh Quynha18abdd2014-10-29 21:32:06 +080034#define CAPSTONE_EXPORT
Alex Ionescu46018db2014-01-22 09:45:00 -080035#endif
Hilko Bengend83bf842015-02-26 18:37:32 +010036#endif
Alex Ionescu46018db2014-01-22 09:45:00 -080037
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +080038#ifdef __GNUC__
39#define CAPSTONE_DEPRECATED __attribute__((deprecated))
40#elif defined(_MSC_VER)
41#define CAPSTONE_DEPRECATED __declspec(deprecated)
42#else
43#pragma message("WARNING: You need to implement CAPSTONE_DEPRECATED for this compiler")
44#define CAPSTONE_DEPRECATED
45#endif
46
Nguyen Anh Quynhbb64b0b2013-12-10 07:56:17 +080047// Capstone API version
Nguyen Anh Quynhfd0f7982014-08-13 14:15:27 +080048#define CS_API_MAJOR 3
49#define CS_API_MINOR 0
Nguyen Anh Quynhbb64b0b2013-12-10 07:56:17 +080050
Nguyen Anh Quynh2296d5e2013-12-22 21:01:17 +080051// Macro to create combined version which can be compared to
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080052// result of cs_version() API.
Nguyen Anh Quynh2296d5e2013-12-22 21:01:17 +080053#define CS_MAKE_VERSION(major, minor) ((major << 8) + minor)
54
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055// Handle using with all API
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080056typedef size_t csh;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080057
58// Architecture type
59typedef enum cs_arch {
60 CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2)
61 CS_ARCH_ARM64, // ARM-64, also called AArch64
62 CS_ARCH_MIPS, // Mips architecture
63 CS_ARCH_X86, // X86 architecture (including x86 & x86-64)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080064 CS_ARCH_PPC, // PowerPC architecture
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080065 CS_ARCH_SPARC, // Sparc architecture
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080066 CS_ARCH_SYSZ, // SystemZ architecture
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080067 CS_ARCH_XCORE, // XCore architecture
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080068 CS_ARCH_MAX,
Nguyen Anh Quynh753f44a2014-11-12 11:05:42 +080069 CS_ARCH_ALL = 0xFFFF, // All architectures - for cs_support()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070} cs_arch;
71
Nguyen Anh Quynh95181482014-03-25 23:20:41 +080072// Support value to verify diet mode of the engine.
73// If cs_support(CS_SUPPORT_DIET) return True, the engine was compiled
74// in diet mode.
Nguyen Anh Quynh492b8ed2014-02-25 08:14:15 +080075#define CS_SUPPORT_DIET (CS_ARCH_ALL + 1)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080076
Nguyen Anh Quynh6d3c7112014-03-27 15:38:23 +080077// Support value to verify X86 reduce mode of the engine.
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +080078// If cs_support(CS_SUPPORT_X86_REDUCE) return True, the engine was compiled
Nguyen Anh Quynh6d3c7112014-03-27 15:38:23 +080079// in X86 reduce mode.
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +080080#define CS_SUPPORT_X86_REDUCE (CS_ARCH_ALL + 2)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +080081
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080082// Mode type
83typedef enum cs_mode {
Nguyen Anh Quynha7b06fd2014-11-13 12:00:48 +080084 CS_MODE_LITTLE_ENDIAN = 0, // little-endian mode (default mode)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080085 CS_MODE_ARM = 0, // 32-bit ARM
Nguyen Anh Quynha7b06fd2014-11-13 12:00:48 +080086 CS_MODE_16 = 1 << 1, // 16-bit mode (X86)
87 CS_MODE_32 = 1 << 2, // 32-bit mode (X86)
88 CS_MODE_64 = 1 << 3, // 64-bit mode (X86, PPC)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089 CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2
Nguyen Anh Quynh7c089fd2014-08-13 23:08:40 +080090 CS_MODE_MCLASS = 1 << 5, // ARM's Cortex-M series
Nguyen Anh Quynhc942f222014-11-10 21:52:09 +080091 CS_MODE_V8 = 1 << 6, // ARMv8 A32 encodings for ARM
Nguyen Anh Quynha7b06fd2014-11-13 12:00:48 +080092 CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS)
Jay Oster79e253c2014-10-12 16:03:12 -070093 CS_MODE_MIPS3 = 1 << 5, // Mips III ISA
94 CS_MODE_MIPS32R6 = 1 << 6, // Mips32r6 ISA
Nguyen Anh Quynha7b06fd2014-11-13 12:00:48 +080095 CS_MODE_MIPSGP64 = 1 << 7, // General Purpose Registers are 64-bit wide (MIPS)
96 CS_MODE_V9 = 1 << 4, // SparcV9 mode (Sparc)
97 CS_MODE_BIG_ENDIAN = 1 << 31, // big-endian mode
98 CS_MODE_MIPS32 = CS_MODE_32, // Mips32 ISA (Mips)
99 CS_MODE_MIPS64 = CS_MODE_64, // Mips64 ISA (Mips)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800100} cs_mode;
101
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800102typedef void* (*cs_malloc_t)(size_t size);
103typedef void* (*cs_calloc_t)(size_t nmemb, size_t size);
104typedef void* (*cs_realloc_t)(void *ptr, size_t size);
105typedef void (*cs_free_t)(void *ptr);
Nguyen Anh Quynhbdd1e342014-01-16 12:34:06 +0800106typedef int (*cs_vsnprintf_t)(char *str, size_t size, const char *format, va_list ap);
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800107
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800108
Nguyen Anh Quynh56aba592014-01-15 21:24:28 +0800109// User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf()
110// By default, Capstone uses system's malloc(), calloc(), realloc(), free() & vsnprintf().
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800111typedef struct cs_opt_mem {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800112 cs_malloc_t malloc;
113 cs_calloc_t calloc;
114 cs_realloc_t realloc;
115 cs_free_t free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800116 cs_vsnprintf_t vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800117} cs_opt_mem;
118
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800119// Runtime option for the disassembled engine
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800120typedef enum cs_opt_type {
Taras Tsugrii37590df2015-05-05 13:57:15 -0700121 CS_OPT_SYNTAX = 1, // Assembly output syntax
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800122 CS_OPT_DETAIL, // Break down instruction structure into details
Nguyen Anh Quynh1bdb23a2013-12-20 00:04:26 +0800123 CS_OPT_MODE, // Change engine's mode at run-time
Nguyen Anh Quynh56aba592014-01-15 21:24:28 +0800124 CS_OPT_MEM, // User-defined dynamic memory related functions
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800125 CS_OPT_SKIPDATA, // Skip data when disassembling. Then engine is in SKIPDATA mode.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800126 CS_OPT_SKIPDATA_SETUP, // Setup user-defined function for SKIPDATA option
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800127} cs_opt_type;
128
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800129// Runtime option value (associated with option type above)
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800130typedef enum cs_opt_value {
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800131 CS_OPT_OFF = 0, // Turn OFF an option - default option of CS_OPT_DETAIL, CS_OPT_SKIPDATA.
132 CS_OPT_ON = 3, // Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800133 CS_OPT_SYNTAX_DEFAULT = 0, // Default asm syntax (CS_OPT_SYNTAX).
134 CS_OPT_SYNTAX_INTEL, // X86 Intel asm syntax - default on X86 (CS_OPT_SYNTAX).
Nguyen Anh Quynhc263d352014-01-07 23:50:02 +0800135 CS_OPT_SYNTAX_ATT, // X86 ATT asm syntax (CS_OPT_SYNTAX).
Nguyen Anh Quynh2ff665a2014-03-11 00:18:50 +0800136 CS_OPT_SYNTAX_NOREGNAME, // Prints register name with only number (CS_OPT_SYNTAX)
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800137} cs_opt_value;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800138
Nguyen Anh Quynh3ab50912014-10-31 14:40:45 +0800139//> Common instruction operand types - to be consistent across all architectures.
Nguyen Anh Quynh21ac0562014-10-31 13:08:28 +0800140typedef enum cs_op_type {
141 CS_OP_INVALID = 0, // uninitialized/invalid operand.
142 CS_OP_REG, // Register operand.
143 CS_OP_IMM, // Immediate operand.
144 CS_OP_MEM, // Memory operand.
Nguyen Anh Quynh3ab50912014-10-31 14:40:45 +0800145 CS_OP_FP, // Floating-Point operand.
Nguyen Anh Quynh21ac0562014-10-31 13:08:28 +0800146} cs_op_type;
147
Nguyen Anh Quynh3ab50912014-10-31 14:40:45 +0800148//> Common instruction groups - to be consistent across all architectures.
149typedef enum cs_group_type {
150 CS_GRP_INVALID = 0, // uninitialized/invalid group.
151 CS_GRP_JUMP, // all jump instructions (conditional+direct+indirect jumps)
152 CS_GRP_CALL, // all call instructions
153 CS_GRP_RET, // all return instructions
154 CS_GRP_INT, // all interrupt instructions (int+syscall)
155 CS_GRP_IRET, // all interrupt return instructions
156} cs_group_type;
157
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800158/*
Nguyen Anh Quynhff2939a2014-10-12 10:43:02 +0800159 User-defined callback function for SKIPDATA option.
160 See tests/test_skipdata.c for sample code demonstrating this API.
161
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800162 @code: the input buffer containing code to be disassembled.
163 This is the same buffer passed to cs_disasm().
164 @code_size: size (in bytes) of the above @code buffer.
165 @offset: the position of the currently-examining byte in the input
166 buffer @code mentioned above.
167 @user_data: user-data passed to cs_option() via @user_data field in
168 cs_opt_skipdata struct below.
169
170 @return: return number of bytes to skip, or 0 to immediately stop disassembling.
171*/
Nguyen Anh Quynh43efc452014-10-11 00:38:30 +0800172typedef size_t (*cs_skipdata_cb_t)(const uint8_t *code, size_t code_size, size_t offset, void *user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800173
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800174// User-customized setup for SKIPDATA option
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800175typedef struct cs_opt_skipdata {
176 // Capstone considers data to skip as special "instructions".
177 // User can specify the string for this instruction's "mnemonic" here.
Nguyen Anh Quynh2c425fc2014-09-07 09:46:54 +0800178 // By default (if @mnemonic is NULL), Capstone use ".byte".
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800179 const char *mnemonic;
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800180
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800181 // User-defined callback function to be called when Capstone hits data.
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800182 // If the returned value from this callback is positive (>0), Capstone
183 // will skip exactly that number of bytes & continue. Otherwise, if
184 // the callback returns 0, Capstone stops disassembling and returns
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800185 // immediately from cs_disasm()
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800186 // NOTE: if this callback pointer is NULL, Capstone would skip a number
187 // of bytes depending on architectures, as following:
188 // Arm: 2 bytes (Thumb mode) or 4 bytes.
189 // Arm64: 4 bytes.
190 // Mips: 4 bytes.
Nguyen Anh Quynh3d5b6f32014-04-11 09:53:53 +0800191 // PowerPC: 4 bytes.
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800192 // Sparc: 4 bytes.
193 // SystemZ: 2 bytes.
194 // X86: 1 bytes.
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800195 // XCore: 2 bytes.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800196 cs_skipdata_cb_t callback; // default value is NULL
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800197
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800198 // User-defined data to be passed to @callback function pointer.
199 void *user_data;
200} cs_opt_skipdata;
201
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800202
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203#include "arm.h"
204#include "arm64.h"
205#include "mips.h"
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800206#include "ppc.h"
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800207#include "sparc.h"
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800208#include "systemz.h"
209#include "x86.h"
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800210#include "xcore.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800211
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800212// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
213typedef struct cs_detail {
214 uint8_t regs_read[12]; // list of implicit registers read by this insn
215 uint8_t regs_read_count; // number of implicit registers read by this insn
216
217 uint8_t regs_write[20]; // list of implicit registers modified by this insn
218 uint8_t regs_write_count; // number of implicit registers modified by this insn
219
220 uint8_t groups[8]; // list of group this instruction belong to
221 uint8_t groups_count; // number of groups this insn belongs to
222
223 // Architecture-specific instruction info
224 union {
225 cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode
226 cs_arm64 arm64; // ARM64 architecture (aka AArch64)
227 cs_arm arm; // ARM architecture (including Thumb/Thumb2)
228 cs_mips mips; // MIPS architecture
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800229 cs_ppc ppc; // PowerPC architecture
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800230 cs_sparc sparc; // Sparc architecture
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800231 cs_sysz sysz; // SystemZ architecture
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800232 cs_xcore xcore; // XCore architecture
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800233 };
234} cs_detail;
235
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800236// Detail information of disassembled instruction
237typedef struct cs_insn {
mrexodiaac9253c2015-04-24 22:56:55 +0200238 // Instruction ID (basically a numeric ID for the instruction mnemonic)
239 // Find the instruction id in the '[ARCH]_insn' enum in the header file
240 // of corresponding architecture, such as 'arm_insn' in arm.h for ARM,
241 // 'x86_insn' in x86.h for X86, etc...
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800242 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynhfb429b12014-10-12 15:29:12 +0800243 // NOTE: in Skipdata mode, "data" instruction has 0 for this id field.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244 unsigned int id;
245
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800246 // Address (EIP) of this instruction
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800247 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynhf2a649e2013-12-03 12:21:01 +0800248 uint64_t address;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800249
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800250 // Size of this instruction
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800251 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800252 uint16_t size;
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800253 // Machine bytes of this instruction, with number of bytes indicated by @size above
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800254 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
pancakef0e4eed2013-12-11 22:14:42 +0100255 uint8_t bytes[16];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256
257 // Ascii text of instruction mnemonic
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800258 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259 char mnemonic[32];
Nguyen Anh Quynhcf89cf62014-01-06 09:08:35 +0800260
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261 // Ascii text of instruction operands
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800262 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh0636f682014-01-15 17:51:08 +0800263 char op_str[160];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800264
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800265 // Pointer to cs_detail.
Nguyen Anh Quynh801ce2b2014-10-12 10:36:57 +0800266 // NOTE: detail pointer is only valid when both requirements below are met:
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800267 // (1) CS_OP_DETAIL = CS_OPT_ON
Nguyen Anh Quynh801ce2b2014-10-12 10:36:57 +0800268 // (2) Engine is not in Skipdata mode (CS_OP_SKIPDATA option set to CS_OPT_ON)
Nguyen Anh Quynh2c8b2622014-10-12 20:23:08 +0800269 //
270 // NOTE 2: when in Skipdata mode, or when detail mode is OFF, even if this pointer
271 // is not NULL, its content is still irrelevant.
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800272 cs_detail *detail;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273} cs_insn;
274
Nguyen Anh Quynhbb546032013-12-05 18:29:51 +0800275
276// Calculate the offset of a disassembled instruction in its buffer, given its position
277// in its array of disassembled insn
Nguyen Anh Quynha2369022013-12-05 20:21:09 +0800278// NOTE: this macro works with position (>=1), not index
279#define CS_INSN_OFFSET(insns, post) (insns[post - 1].address - insns[0].address)
Nguyen Anh Quynhbb546032013-12-05 18:29:51 +0800280
281
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282// All type of errors encountered by Capstone API.
283// These are values returned by cs_errno()
284typedef enum cs_err {
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800285 CS_ERR_OK = 0, // No error: everything was fine
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800286 CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter()
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800287 CS_ERR_ARCH, // Unsupported architecture: cs_open()
288 CS_ERR_HANDLE, // Invalid handle: cs_op_count(), cs_op_index()
289 CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option()
290 CS_ERR_MODE, // Invalid/unsupported mode: cs_open()
291 CS_ERR_OPTION, // Invalid/unsupported option: cs_option()
292 CS_ERR_DETAIL, // Information is unavailable because detail option is OFF
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800293 CS_ERR_MEMSETUP, // Dynamic memory management uninitialized (see CS_OPT_MEM)
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800294 CS_ERR_VERSION, // Unsupported version (bindings)
295 CS_ERR_DIET, // Access irrelevant data in "diet" engine
296 CS_ERR_SKIPDATA, // Access irrelevant data for "data" instruction in SKIPDATA mode
Nguyen Anh Quynh66382942014-08-20 14:02:14 +0800297 CS_ERR_X86_ATT, // X86 AT&T syntax is unsupported (opt-out at compile time)
298 CS_ERR_X86_INTEL, // X86 Intel syntax is unsupported (opt-out at compile time)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800299} cs_err;
300
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800301/*
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800302 Return combined API version & major and minor version numbers.
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800303
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800304 @major: major number of API version
305 @minor: minor number of API version
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800306
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800307 @return hexical number as (major << 8 | minor), which encodes both
hlide993f3622014-10-05 18:14:40 +0200308 major & minor versions.
309 NOTE: This returned value can be compared with version number made
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800310 with macro CS_MAKE_VERSION
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800311
312 For example, second API version would return 1 in @major, and 1 in @minor
313 The return value would be 0x0101
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800314
315 NOTE: if you only care about returned value, but not major and minor values,
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800316 set both @major & @minor arguments to NULL.
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800317*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800318CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800319unsigned int cs_version(int *major, int *minor);
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800320
321
322/*
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800323 This API can be used to either ask for archs supported by this library,
Nguyen Anh Quynhc70adc32014-02-23 00:03:46 +0800324 or check to see if the library was compile with 'diet' option (or called
325 in 'diet' mode).
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800326
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800327 To check if a particular arch is supported by this library, set @query to
328 arch mode (CS_ARCH_* value).
329 To verify if this library supports all the archs, use CS_ARCH_ALL.
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800330
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800331 To check if this library is in 'diet' mode, set @query to CS_SUPPORT_DIET.
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800332
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800333 @return True if this library supports the given arch, or in 'diet' mode.
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800334*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800335CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800336bool cs_support(int query);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800337
338/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339 Initialize CS handle: this must be done before any usage of CS.
340
341 @arch: architecture type (CS_ARCH_*)
342 @mode: hardware mode. This is combined of CS_MODE_*
343 @handle: pointer to handle, which will be updated at return time
344
345 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
346 for detailed error).
347*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800348CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle);
350
351/*
352 Close CS handle: MUST do to release the handle when it is not used anymore.
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800353 NOTE: this must be only called when there is no longer usage of Capstone,
354 not even access to cs_insn array. The reason is the this API releases some
355 cached memory, thus access to any Capstone API after cs_close() might crash
356 your application.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800358 In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0).
359
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800360 @handle: pointer to a handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361
362 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
363 for detailed error).
364*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800365CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800366cs_err cs_close(csh *handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367
368/*
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800369 Set option for disassembling engine at runtime
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800370
371 @handle: handle returned by cs_open()
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800372 @type: type of option to be set
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800373 @value: option value corresponding with @type
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800374
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800375 @return: CS_ERR_OK on success, or other value on failure.
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800376 Refer to cs_err enum for detailed error.
Nguyen Anh Quynha60ed8b2014-01-05 23:52:30 +0800377
378 NOTE: in the case of CS_OPT_MEM, handle's value can be anything,
379 so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called
380 even before cs_open()
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800381*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800382CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800383cs_err cs_option(csh handle, cs_opt_type type, size_t value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800384
385/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800386 Report the last error number when some API function fail.
387 Like glibc's errno, cs_errno might not retain its old value once accessed.
388
389 @handle: handle returned by cs_open()
390
391 @return: error code of cs_err enum type (CS_ERR_*, see above)
392*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800393CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394cs_err cs_errno(csh handle);
395
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800396
397/*
398 Return a string describing given error code.
399
400 @code: error code (see CS_ERR_* above)
401
402 @return: returns a pointer to a string that describes the error code
hlide993f3622014-10-05 18:14:40 +0200403 passed in the argument @code
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800404*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800405CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800406const char *cs_strerror(cs_err code);
407
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408/*
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800409 Disassemble binary code, given the code buffer, size, address and number
410 of instructions to be decoded.
Taras Tsugrii37590df2015-05-05 13:57:15 -0700411 This API dynamically allocate memory to contain disassembled instruction.
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800412 Resulted instructions will be put into @*insn
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413
414 NOTE 1: this API will automatically determine memory needed to contain
415 output disassembled instructions in @insn.
Nguyen Anh Quynh024e55e2014-10-20 23:32:06 +0800416
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800417 NOTE 2: caller must free the allocated memory itself to avoid memory leaking.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418
Nguyen Anh Quynh024e55e2014-10-20 23:32:06 +0800419 NOTE 3: for system with scarce memory to be dynamically allocated such as
420 OS kernel or firmware, the API cs_disasm_iter() might be a better choice than
421 cs_disasm(). The reason is that with cs_disasm(), based on limited available
422 memory, we have to calculate in advance how many instructions to be disassembled,
423 which complicates things. This is especially troublesome for the case @count=0,
Taras Tsugrii37590df2015-05-05 13:57:15 -0700424 when cs_disasm() runs uncontrollably (until either end of input buffer, or
Nguyen Anh Quynh024e55e2014-10-20 23:32:06 +0800425 when it encounters an invalid instruction).
426
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800427 @handle: handle returned by cs_open()
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800428 @code: buffer containing raw binary code to be disassembled.
429 @code_size: size of the above code buffer.
430 @address: address of the first instruction in given raw code buffer.
431 @insn: array of instructions filled in by this API.
hlide993f3622014-10-05 18:14:40 +0200432 NOTE: @insn will be allocated by this function, and should be freed
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800433 with cs_free() API.
Taras Tsugrii37590df2015-05-05 13:57:15 -0700434 @count: number of instructions to be disassembled, or 0 to get all of them
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800435
Taras Tsugrii37590df2015-05-05 13:57:15 -0700436 @return: the number of successfully disassembled instructions,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437 or 0 if this function failed to disassemble the given code
Nguyen Anh Quynh029df202013-12-03 11:36:54 +0800438
439 On failure, call cs_errno() for error code.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800441CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800442size_t cs_disasm(csh handle,
pancakef0e4eed2013-12-11 22:14:42 +0100443 const uint8_t *code, size_t code_size,
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800444 uint64_t address,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800445 size_t count,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446 cs_insn **insn);
447
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800448/*
449 Deprecated function - to be retired in the next version!
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800450 Use cs_disasm() instead of cs_disasm_ex()
451*/
452CAPSTONE_EXPORT
453CAPSTONE_DEPRECATED
454size_t cs_disasm_ex(csh handle,
455 const uint8_t *code, size_t code_size,
456 uint64_t address,
457 size_t count,
458 cs_insn **insn);
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800459
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460/*
Nguyen Anh Quynh542a5402014-10-11 13:12:15 +0800461 Free memory allocated by cs_malloc() or cs_disasm() (argument @insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800462
Nguyen Anh Quynh542a5402014-10-11 13:12:15 +0800463 @insn: pointer returned by @insn argument in cs_disasm() or cs_malloc()
464 @count: number of cs_insn structures returned by cs_disasm(), or 1
465 to free memory allocated by cs_malloc().
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800467CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800468void cs_free(cs_insn *insn, size_t count);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800470
471/*
472 Allocate memory for 1 instruction to be used by cs_disasm_iter().
473
474 @handle: handle returned by cs_open()
475
Nguyen Anh Quynh5cb3d642014-10-11 01:32:39 +0800476 NOTE: when no longer in use, you can reclaim the memory allocated for
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800477 this instruction with cs_free(insn, 1)
478*/
hlide993f3622014-10-05 18:14:40 +0200479CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800480cs_insn *cs_malloc(csh handle);
481
482/*
Nguyen Anh Quynh5cb3d642014-10-11 01:32:39 +0800483 Fast API to disassemble binary code, given the code buffer, size, address
484 and number of instructions to be decoded.
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800485 This API put the resulted instruction into a given cache in @insn.
Nguyen Anh Quynhff2939a2014-10-12 10:43:02 +0800486 See tests/test_iter.c for sample code demonstrating this API.
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800487
488 NOTE 1: this API will update @code, @size & @address to point to the next
Taras Tsugrii37590df2015-05-05 13:57:15 -0700489 instruction in the input buffer. Therefore, it is convenient to use
Nguyen Anh Quynh5cb3d642014-10-11 01:32:39 +0800490 cs_disasm_iter() inside a loop to quickly iterate all the instructions.
491 While decoding one instruction at a time can also be achieved with
492 cs_disasm(count=1), some benchmarks shown that cs_disasm_iter() can be 30%
493 faster on random input.
494
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800495 NOTE 2: the cache in @insn can be created with cs_malloc() API.
496
Nguyen Anh Quynh024e55e2014-10-20 23:32:06 +0800497 NOTE 3: for system with scarce memory to be dynamically allocated such as
498 OS kernel or firmware, this API is recommended over cs_disasm(), which
499 allocates memory based on the number of instructions to be disassembled.
500 The reason is that with cs_disasm(), based on limited available memory,
501 we have to calculate in advance how many instructions to be disassembled,
502 which complicates things. This is especially troublesome for the case
Taras Tsugrii37590df2015-05-05 13:57:15 -0700503 @count=0, when cs_disasm() runs uncontrollably (until either end of input
Nguyen Anh Quynh024e55e2014-10-20 23:32:06 +0800504 buffer, or when it encounters an invalid instruction).
505
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800506 @handle: handle returned by cs_open()
507 @code: buffer containing raw binary code to be disassembled
508 @code_size: size of above code
509 @address: address of the first insn in given raw code buffer
510 @insn: pointer to instruction to be filled in by this API.
511
512 @return: true if this API successfully decode 1 instruction,
513 or false otherwise.
514
515 On failure, call cs_errno() for error code.
516*/
517CAPSTONE_EXPORT
518bool cs_disasm_iter(csh handle,
hlide993f3622014-10-05 18:14:40 +0200519 const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800520 uint64_t *address, cs_insn *insn);
hlide993f3622014-10-05 18:14:40 +0200521
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522/*
Taras Tsugrii37590df2015-05-05 13:57:15 -0700523 Return friendly name of register in a string.
Nguyen Anh Quynhc70adc32014-02-23 00:03:46 +0800524 Find the instruction id from header file of corresponding architecture (arm.h for ARM,
525 x86.h for X86, ...)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800526
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800527 WARN: when in 'diet' mode, this API is irrelevant because engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800528 store register name.
529
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800530 @handle: handle returned by cs_open()
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800531 @reg_id: register id
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800532
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800533 @return: string name of the register, or NULL if @reg_id is invalid.
534*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800535CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100536const char *cs_reg_name(csh handle, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800537
538/*
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800539 Return friendly name of an instruction in a string.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800540 Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
541
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800542 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800543 store instruction name.
544
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800545 @handle: handle returned by cs_open()
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800546 @insn_id: instruction id
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800547
548 @return: string name of the instruction, or NULL if @insn_id is invalid.
549*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800550CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100551const char *cs_insn_name(csh handle, unsigned int insn_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552
553/*
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800554 Return friendly name of a group id (that an instruction can belong to)
555 Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
556
557 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
558 store group name.
559
560 @handle: handle returned by cs_open()
561 @group_id: group id
562
563 @return: string name of the group, or NULL if @group_id is invalid.
564*/
565CAPSTONE_EXPORT
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800566const char *cs_group_name(csh handle, unsigned int group_id);
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800567
568/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800569 Check if a disassembled instruction belong to a particular group.
570 Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
571 Internally, this simply verifies if @group_id matches any member of insn->groups array.
572
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800573 NOTE: this API is only valid when detail option is ON (which is OFF by default).
574
575 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800576 update @groups array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800577
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800578 @handle: handle returned by cs_open()
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800579 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800580 @group_id: group that you want to check if this instruction belong to.
581
582 @return: true if this instruction indeed belongs to aboved group, or false otherwise.
583*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800584CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200585bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800586
587/*
588 Check if a disassembled instruction IMPLICITLY used a particular register.
589 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
590 Internally, this simply verifies if @reg_id matches any member of insn->regs_read array.
591
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800592 NOTE: this API is only valid when detail option is ON (which is OFF by default)
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800593
594 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800595 update @regs_read array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800596
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800597 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598 @reg_id: register that you want to check if this instruction used it.
599
600 @return: true if this instruction indeed implicitly used aboved register, or false otherwise.
601*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800602CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200603bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800604
605/*
606 Check if a disassembled instruction IMPLICITLY modified a particular register.
607 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
608 Internally, this simply verifies if @reg_id matches any member of insn->regs_write array.
609
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800610 NOTE: this API is only valid when detail option is ON (which is OFF by default)
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800611
612 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800613 update @regs_write array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800614
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800615 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616 @reg_id: register that you want to check if this instruction modified it.
617
618 @return: true if this instruction indeed implicitly modified aboved register, or false otherwise.
619*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800620CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200621bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800622
623/*
624 Count the number of operands of a given type.
625 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
626
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800627 NOTE: this API is only valid when detail option is ON (which is OFF by default)
628
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800629 @handle: handle returned by cs_open()
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800630 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800631 @op_type: Operand type to be found.
632
633 @return: number of operands of given type @op_type in instruction @insn,
634 or -1 on failure.
635*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800636CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200637int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800638
639/*
Nguyen Anh Quynh4c0ed0b2014-06-03 15:06:20 +0700640 Retrieve the position of operand of given type in <arch>.operands[] array.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800641 Later, the operand can be accessed using the returned position.
642 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
643
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800644 NOTE: this API is only valid when detail option is ON (which is OFF by default)
645
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800646 @handle: handle returned by cs_open()
Nguyen Anh Quynhc41da152014-10-15 16:31:35 +0800647 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800648 @op_type: Operand type to be found.
649 @position: position of the operand to be found. This must be in the range
650 [1, cs_op_count(handle, insn, op_type)]
651
Nguyen Anh Quynh4c0ed0b2014-06-03 15:06:20 +0700652 @return: index of operand of given type @op_type in <arch>.operands[] array
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800653 in instruction @insn, or -1 on failure.
654*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800655CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200656int cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800657 unsigned int position);
658
659#ifdef __cplusplus
660}
661#endif
662
663#endif