blob: a282c2393bfff2626e1143bd08b44c3404a9c0f0 [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 Quynhedeeb042014-01-15 20:44:03 +080012#include <stdio.h>
Nguyen Anh Quynh64d40832014-01-17 20:55:21 +080013#include <stdarg.h>
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080014#include <stdlib.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015
Nguyen Anh Quynh171eb752014-05-15 21:01:08 +080016#include "platform.h"
17
Alex Ionescu46018db2014-01-22 09:45:00 -080018#ifdef _MSC_VER
Ali Rizvi-Santiago10053ba2014-06-03 21:04:23 +000019 #pragma warning(disable:4201)
20 #pragma warning(disable:4100)
21 #ifdef CAPSTONE_SHARED
22 #define CAPSTONE_EXPORT __declspec(dllexport)
23 #else // defined(CAPSTONE_STATIC)
24 #define CAPSTONE_EXPORT
25 #endif
26#else
27 #define CAPSTONE_EXPORT
Alex Ionescu46018db2014-01-22 09:45:00 -080028#endif
29
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +080030#ifdef __GNUC__
31#define CAPSTONE_DEPRECATED __attribute__((deprecated))
32#elif defined(_MSC_VER)
33#define CAPSTONE_DEPRECATED __declspec(deprecated)
34#else
35#pragma message("WARNING: You need to implement CAPSTONE_DEPRECATED for this compiler")
36#define CAPSTONE_DEPRECATED
37#endif
38
Nguyen Anh Quynhbb64b0b2013-12-10 07:56:17 +080039// Capstone API version
Nguyen Anh Quynhfd0f7982014-08-13 14:15:27 +080040#define CS_API_MAJOR 3
41#define CS_API_MINOR 0
Nguyen Anh Quynhbb64b0b2013-12-10 07:56:17 +080042
Nguyen Anh Quynh2296d5e2013-12-22 21:01:17 +080043// Macro to create combined version which can be compared to
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080044// result of cs_version() API.
Nguyen Anh Quynh2296d5e2013-12-22 21:01:17 +080045#define CS_MAKE_VERSION(major, minor) ((major << 8) + minor)
46
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080047// Handle using with all API
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080048typedef size_t csh;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080049
50// Architecture type
51typedef enum cs_arch {
52 CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2)
53 CS_ARCH_ARM64, // ARM-64, also called AArch64
54 CS_ARCH_MIPS, // Mips architecture
55 CS_ARCH_X86, // X86 architecture (including x86 & x86-64)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080056 CS_ARCH_PPC, // PowerPC architecture
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080057 CS_ARCH_SPARC, // Sparc architecture
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080058 CS_ARCH_SYSZ, // SystemZ architecture
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080059 CS_ARCH_XCORE, // XCore architecture
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060 CS_ARCH_MAX,
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080061 CS_ARCH_ALL = 0xFFFF,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080062} cs_arch;
63
Nguyen Anh Quynh95181482014-03-25 23:20:41 +080064// Support value to verify diet mode of the engine.
65// If cs_support(CS_SUPPORT_DIET) return True, the engine was compiled
66// in diet mode.
Nguyen Anh Quynh492b8ed2014-02-25 08:14:15 +080067#define CS_SUPPORT_DIET (CS_ARCH_ALL + 1)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080068
Nguyen Anh Quynh6d3c7112014-03-27 15:38:23 +080069// Support value to verify X86 reduce mode of the engine.
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +080070// If cs_support(CS_SUPPORT_X86_REDUCE) return True, the engine was compiled
Nguyen Anh Quynh6d3c7112014-03-27 15:38:23 +080071// in X86 reduce mode.
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +080072#define CS_SUPPORT_X86_REDUCE (CS_ARCH_ALL + 2)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +080073
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080074// Mode type
75typedef enum cs_mode {
76 CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077 CS_MODE_ARM = 0, // 32-bit ARM
78 CS_MODE_16 = 1 << 1, // 16-bit mode
79 CS_MODE_32 = 1 << 2, // 32-bit mode
80 CS_MODE_64 = 1 << 3, // 64-bit mode
81 CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2
Nguyen Anh Quynh7c089fd2014-08-13 23:08:40 +080082 CS_MODE_MCLASS = 1 << 5, // ARM's Cortex-M series
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080083 CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS architecture)
84 CS_MODE_N64 = 1 << 5, // Nintendo-64 mode (MIPS architecture)
Nguyen Anh Quynh0f0eb982014-08-14 18:26:39 +080085 CS_MODE_MIPS3 = 1 << 6, // Mips III ISA
86 CS_MODE_MIPS32R6 = 1 << 7, // Mips32r6 ISA
87 CS_MODE_MIPSGP64 = 1 << 8, // General Purpose Registers are 64-bit wide (MIPS arch)
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080088 CS_MODE_V9 = 1 << 4, // SparcV9 mode (Sparc architecture)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089 CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode
90} cs_mode;
91
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080092typedef void* (*cs_malloc_t)(size_t size);
93typedef void* (*cs_calloc_t)(size_t nmemb, size_t size);
94typedef void* (*cs_realloc_t)(void *ptr, size_t size);
95typedef void (*cs_free_t)(void *ptr);
Nguyen Anh Quynhbdd1e342014-01-16 12:34:06 +080096typedef int (*cs_vsnprintf_t)(char *str, size_t size, const char *format, va_list ap);
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080097
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080098
Nguyen Anh Quynh56aba592014-01-15 21:24:28 +080099// User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf()
100// By default, Capstone uses system's malloc(), calloc(), realloc(), free() & vsnprintf().
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800101typedef struct cs_opt_mem {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800102 cs_malloc_t malloc;
103 cs_calloc_t calloc;
104 cs_realloc_t realloc;
105 cs_free_t free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800106 cs_vsnprintf_t vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800107} cs_opt_mem;
108
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800109// Runtime option for the disassembled engine
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800110typedef enum cs_opt_type {
Nguyen Anh Quynh4d70daf2013-12-05 09:50:50 +0800111 CS_OPT_SYNTAX = 1, // Asssembly output syntax
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800112 CS_OPT_DETAIL, // Break down instruction structure into details
Nguyen Anh Quynh1bdb23a2013-12-20 00:04:26 +0800113 CS_OPT_MODE, // Change engine's mode at run-time
Nguyen Anh Quynh56aba592014-01-15 21:24:28 +0800114 CS_OPT_MEM, // User-defined dynamic memory related functions
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800115 CS_OPT_SKIPDATA, // Skip data when disassembling. Then engine is in SKIPDATA mode.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800116 CS_OPT_SKIPDATA_SETUP, // Setup user-defined function for SKIPDATA option
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800117} cs_opt_type;
118
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800119// Runtime option value (associated with option type above)
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800120typedef enum cs_opt_value {
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800121 CS_OPT_OFF = 0, // Turn OFF an option - default option of CS_OPT_DETAIL, CS_OPT_SKIPDATA.
122 CS_OPT_ON = 3, // Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800123 CS_OPT_SYNTAX_DEFAULT = 0, // Default asm syntax (CS_OPT_SYNTAX).
124 CS_OPT_SYNTAX_INTEL, // X86 Intel asm syntax - default on X86 (CS_OPT_SYNTAX).
Nguyen Anh Quynhc263d352014-01-07 23:50:02 +0800125 CS_OPT_SYNTAX_ATT, // X86 ATT asm syntax (CS_OPT_SYNTAX).
Nguyen Anh Quynh2ff665a2014-03-11 00:18:50 +0800126 CS_OPT_SYNTAX_NOREGNAME, // Prints register name with only number (CS_OPT_SYNTAX)
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800127} cs_opt_value;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800128
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800129// User-defined callback function for SKIPDATA option
Nguyen Anh Quynh1efa9c12014-04-14 21:48:59 +0800130// @code: the input buffer containing code to be disassembled. This is the
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800131// same buffer passed to cs_disasm().
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800132// @code_size: size (in bytes) of the above @code buffer.
Nguyen Anh Quynh1efa9c12014-04-14 21:48:59 +0800133// @offset: the position of the currently-examining byte in the input
134// buffer @code mentioned above.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800135// @user_data: user-data passed to cs_option() via @user_data field in
Nguyen Anh Quynh1efa9c12014-04-14 21:48:59 +0800136// cs_opt_skipdata struct below.
Nguyen Anh Quynh3d5b6f32014-04-11 09:53:53 +0800137// @return: return number of bytes to skip, or 0 to immediately stop disassembling.
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800138typedef 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 +0800139
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800140// User-customized setup for SKIPDATA option
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800141typedef struct cs_opt_skipdata {
142 // Capstone considers data to skip as special "instructions".
143 // User can specify the string for this instruction's "mnemonic" here.
Nguyen Anh Quynh2c425fc2014-09-07 09:46:54 +0800144 // By default (if @mnemonic is NULL), Capstone use ".byte".
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800145 const char *mnemonic;
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800146
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800147 // User-defined callback function to be called when Capstone hits data.
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800148 // If the returned value from this callback is positive (>0), Capstone
149 // will skip exactly that number of bytes & continue. Otherwise, if
150 // the callback returns 0, Capstone stops disassembling and returns
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800151 // immediately from cs_disasm()
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800152 // NOTE: if this callback pointer is NULL, Capstone would skip a number
153 // of bytes depending on architectures, as following:
154 // Arm: 2 bytes (Thumb mode) or 4 bytes.
155 // Arm64: 4 bytes.
156 // Mips: 4 bytes.
Nguyen Anh Quynh3d5b6f32014-04-11 09:53:53 +0800157 // PowerPC: 4 bytes.
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800158 // Sparc: 4 bytes.
159 // SystemZ: 2 bytes.
160 // X86: 1 bytes.
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800161 // XCore: 2 bytes.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800162 cs_skipdata_cb_t callback; // default value is NULL
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800163
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800164 // User-defined data to be passed to @callback function pointer.
165 void *user_data;
166} cs_opt_skipdata;
167
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800168
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800169#include "arm.h"
170#include "arm64.h"
171#include "mips.h"
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800172#include "ppc.h"
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800173#include "sparc.h"
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800174#include "systemz.h"
175#include "x86.h"
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800176#include "xcore.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800178// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
179typedef struct cs_detail {
180 uint8_t regs_read[12]; // list of implicit registers read by this insn
181 uint8_t regs_read_count; // number of implicit registers read by this insn
182
183 uint8_t regs_write[20]; // list of implicit registers modified by this insn
184 uint8_t regs_write_count; // number of implicit registers modified by this insn
185
186 uint8_t groups[8]; // list of group this instruction belong to
187 uint8_t groups_count; // number of groups this insn belongs to
188
189 // Architecture-specific instruction info
190 union {
191 cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode
192 cs_arm64 arm64; // ARM64 architecture (aka AArch64)
193 cs_arm arm; // ARM architecture (including Thumb/Thumb2)
194 cs_mips mips; // MIPS architecture
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800195 cs_ppc ppc; // PowerPC architecture
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800196 cs_sparc sparc; // Sparc architecture
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800197 cs_sysz sysz; // SystemZ architecture
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800198 cs_xcore xcore; // XCore architecture
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800199 };
200} cs_detail;
201
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202// Detail information of disassembled instruction
203typedef struct cs_insn {
204 // Instruction ID
205 // Find the instruction id from header file of corresponding architecture,
206 // such as arm.h for ARM, x86.h for X86, etc...
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800207 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800208 unsigned int id;
209
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800210 // Address (EIP) of this instruction
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800211 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynhf2a649e2013-12-03 12:21:01 +0800212 uint64_t address;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800213
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800214 // Size of this instruction
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800215 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800216 uint16_t size;
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800217 // Machine bytes of this instruction, with number of bytes indicated by @size above
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800218 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
pancakef0e4eed2013-12-11 22:14:42 +0100219 uint8_t bytes[16];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220
221 // Ascii text of instruction mnemonic
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800222 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 char mnemonic[32];
Nguyen Anh Quynhcf89cf62014-01-06 09:08:35 +0800224
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225 // Ascii text of instruction operands
Nguyen Anh Quynh46a5afd2013-12-14 11:52:06 +0800226 // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
Nguyen Anh Quynh0636f682014-01-15 17:51:08 +0800227 char op_str[160];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800229 // Pointer to cs_detail.
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800230 // NOTE: detail pointer is only valid (not NULL) when both requirements below are met:
231 // (1) CS_OP_DETAIL = CS_OPT_ON
232 // (2) If engine is in Skipdata mode (CS_OP_SKIPDATA option set to CS_OPT_ON), then
233 // the current instruction is not the "data" instruction (which clearly has no detail).
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800234 cs_detail *detail;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235} cs_insn;
236
Nguyen Anh Quynhbb546032013-12-05 18:29:51 +0800237
238// Calculate the offset of a disassembled instruction in its buffer, given its position
239// in its array of disassembled insn
Nguyen Anh Quynha2369022013-12-05 20:21:09 +0800240// NOTE: this macro works with position (>=1), not index
241#define CS_INSN_OFFSET(insns, post) (insns[post - 1].address - insns[0].address)
Nguyen Anh Quynhbb546032013-12-05 18:29:51 +0800242
243
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244// All type of errors encountered by Capstone API.
245// These are values returned by cs_errno()
246typedef enum cs_err {
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800247 CS_ERR_OK = 0, // No error: everything was fine
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800248 CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm()
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800249 CS_ERR_ARCH, // Unsupported architecture: cs_open()
250 CS_ERR_HANDLE, // Invalid handle: cs_op_count(), cs_op_index()
251 CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option()
252 CS_ERR_MODE, // Invalid/unsupported mode: cs_open()
253 CS_ERR_OPTION, // Invalid/unsupported option: cs_option()
254 CS_ERR_DETAIL, // Information is unavailable because detail option is OFF
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800255 CS_ERR_MEMSETUP, // Dynamic memory management uninitialized (see CS_OPT_MEM)
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800256 CS_ERR_VERSION, // Unsupported version (bindings)
257 CS_ERR_DIET, // Access irrelevant data in "diet" engine
258 CS_ERR_SKIPDATA, // Access irrelevant data for "data" instruction in SKIPDATA mode
Nguyen Anh Quynh66382942014-08-20 14:02:14 +0800259 CS_ERR_X86_ATT, // X86 AT&T syntax is unsupported (opt-out at compile time)
260 CS_ERR_X86_INTEL, // X86 Intel syntax is unsupported (opt-out at compile time)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261} cs_err;
262
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800263/*
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800264 Return combined API version & major and minor version numbers.
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800265
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800266 @major: major number of API version
267 @minor: minor number of API version
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800268
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800269 @return hexical number as (major << 8 | minor), which encodes both
270 major & minor versions.
271 NOTE: This returned value can be compared with version number made
272 with macro CS_MAKE_VERSION
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800273
274 For example, second API version would return 1 in @major, and 1 in @minor
275 The return value would be 0x0101
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800276
277 NOTE: if you only care about returned value, but not major and minor values,
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800278 set both @major & @minor arguments to NULL.
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800279*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800280CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800281unsigned int cs_version(int *major, int *minor);
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800282
283
284/*
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800285 This API can be used to either ask for archs supported by this library,
Nguyen Anh Quynhc70adc32014-02-23 00:03:46 +0800286 or check to see if the library was compile with 'diet' option (or called
287 in 'diet' mode).
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800288
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800289 To check if a particular arch is supported by this library, set @query to
290 arch mode (CS_ARCH_* value).
291 To verify if this library supports all the archs, use CS_ARCH_ALL.
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800292
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800293 To check if this library is in 'diet' mode, set @query to CS_SUPPORT_DIET.
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800294
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800295 @return True if this library supports the given arch, or in 'diet' mode.
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800296*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800297CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800298bool cs_support(int query);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800299
300/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 Initialize CS handle: this must be done before any usage of CS.
302
303 @arch: architecture type (CS_ARCH_*)
304 @mode: hardware mode. This is combined of CS_MODE_*
305 @handle: pointer to handle, which will be updated at return time
306
307 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
308 for detailed error).
309*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800310CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle);
312
313/*
314 Close CS handle: MUST do to release the handle when it is not used anymore.
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800315 NOTE: this must be only called when there is no longer usage of Capstone,
316 not even access to cs_insn array. The reason is the this API releases some
317 cached memory, thus access to any Capstone API after cs_close() might crash
318 your application.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800320 In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0).
321
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800322 @handle: pointer to a handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800323
324 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
325 for detailed error).
326*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800327CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800328cs_err cs_close(csh *handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800329
330/*
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800331 Set option for disassembling engine at runtime
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800332
333 @handle: handle returned by cs_open()
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800334 @type: type of option to be set
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800335 @value: option value corresponding with @type
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800336
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800337 @return CS_ERR_OK on success, or other value on failure.
338 Refer to cs_err enum for detailed error.
Nguyen Anh Quynha60ed8b2014-01-05 23:52:30 +0800339
340 NOTE: in the case of CS_OPT_MEM, handle's value can be anything,
341 so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called
342 even before cs_open()
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800343*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800344CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800345cs_err cs_option(csh handle, cs_opt_type type, size_t value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800346
347/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348 Report the last error number when some API function fail.
349 Like glibc's errno, cs_errno might not retain its old value once accessed.
350
351 @handle: handle returned by cs_open()
352
353 @return: error code of cs_err enum type (CS_ERR_*, see above)
354*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800355CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356cs_err cs_errno(csh handle);
357
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800358
359/*
360 Return a string describing given error code.
361
362 @code: error code (see CS_ERR_* above)
363
364 @return: returns a pointer to a string that describes the error code
365 passed in the argument @code
366*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800367CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800368const char *cs_strerror(cs_err code);
369
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800371 Dynamicly allocate memory to contain disasm insn
372 Disassembled instructions will be put into @*insn
373
374 NOTE 1: this API will automatically determine memory needed to contain
375 output disassembled instructions in @insn.
376 NOTE 2: caller must free() the allocated memory itself to avoid memory leaking
377
378 @handle: handle returned by cs_open()
379 @code: buffer containing raw binary code to be disassembled
380 @code_size: size of above code
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800381 @address: address of the first insn in given raw code buffer
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800382 @insn: array of insn filled in by this function
Nguyen Anh Quynh79976c12013-12-04 23:03:13 +0800383 NOTE: @insn will be allocated by this function, and should be freed
384 with cs_free() API.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800385 @count: number of instrutions to be disassembled, or 0 to get all of them
386 @return: the number of succesfully disassembled instructions,
387 or 0 if this function failed to disassemble the given code
Nguyen Anh Quynh029df202013-12-03 11:36:54 +0800388
389 On failure, call cs_errno() for error code.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800390*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800391CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800392size_t cs_disasm(csh handle,
pancakef0e4eed2013-12-11 22:14:42 +0100393 const uint8_t *code, size_t code_size,
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800394 uint64_t address,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800395 size_t count,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396 cs_insn **insn);
397
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800398/*
399 Deprecated function - to be retired in the next version!
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800400 Use cs_disasm() instead of cs_disasm_ex()
401*/
402CAPSTONE_EXPORT
403CAPSTONE_DEPRECATED
404size_t cs_disasm_ex(csh handle,
405 const uint8_t *code, size_t code_size,
406 uint64_t address,
407 size_t count,
408 cs_insn **insn);
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800409
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410/*
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800411 Free memory allocated in @insn by cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800412
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800413 @insn: pointer returned by @insn argument in cs_disasm()
414 @count: number of cs_insn structures returned by cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800416CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800417void cs_free(cs_insn *insn, size_t count);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418
419/*
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800420 Return friendly name of regiser in a string.
Nguyen Anh Quynhc70adc32014-02-23 00:03:46 +0800421 Find the instruction id from header file of corresponding architecture (arm.h for ARM,
422 x86.h for X86, ...)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800424 WARN: when in 'diet' mode, this API is irrelevant because engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800425 store register name.
426
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800427 @handle: handle returned by cs_open()
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800428 @reg_id: register id
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429 @return: string name of the register, or NULL if @reg_id is invalid.
430*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800431CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100432const char *cs_reg_name(csh handle, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433
434/*
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800435 Return friendly name of an instruction in a string.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
437
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800438 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800439 store instruction name.
440
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800441 @handle: handle returned by cs_open()
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800442 @insn_id: instruction id
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800443
444 @return: string name of the instruction, or NULL if @insn_id is invalid.
445*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800446CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100447const char *cs_insn_name(csh handle, unsigned int insn_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800448
449/*
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800450 Return friendly name of a group id (that an instruction can belong to)
451 Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
452
453 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
454 store group name.
455
456 @handle: handle returned by cs_open()
457 @group_id: group id
458
459 @return: string name of the group, or NULL if @group_id is invalid.
460*/
461CAPSTONE_EXPORT
Nguyen Anh Quynhaa58f7f2014-09-25 16:56:31 +0800462const char *cs_group_name(csh handle, unsigned int group_id);
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800463
464/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800465 Check if a disassembled instruction belong to a particular group.
466 Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
467 Internally, this simply verifies if @group_id matches any member of insn->groups array.
468
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800469 NOTE: this API is only valid when detail option is ON (which is OFF by default).
470
471 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800472 update @groups array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800473
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 @handle: handle returned by cs_open()
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800475 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476 @group_id: group that you want to check if this instruction belong to.
477
478 @return: true if this instruction indeed belongs to aboved group, or false otherwise.
479*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800480CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200481bool cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482
483/*
484 Check if a disassembled instruction IMPLICITLY used a particular register.
485 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
486 Internally, this simply verifies if @reg_id matches any member of insn->regs_read array.
487
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800488 NOTE: this API is only valid when detail option is ON (which is OFF by default)
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800489
490 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800491 update @regs_read array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800492
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800493 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800494 @reg_id: register that you want to check if this instruction used it.
495
496 @return: true if this instruction indeed implicitly used aboved register, or false otherwise.
497*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800498CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200499bool cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800500
501/*
502 Check if a disassembled instruction IMPLICITLY modified a particular register.
503 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
504 Internally, this simply verifies if @reg_id matches any member of insn->regs_write array.
505
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800506 NOTE: this API is only valid when detail option is ON (which is OFF by default)
Nguyen Anh Quynh544e0ff2014-02-23 20:24:47 +0800507
508 WARN: when in 'diet' mode, this API is irrelevant because the engine does not
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800509 update @regs_write array.
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800510
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800511 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800512 @reg_id: register that you want to check if this instruction modified it.
513
514 @return: true if this instruction indeed implicitly modified aboved register, or false otherwise.
515*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800516CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200517bool cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518
519/*
520 Count the number of operands of a given type.
521 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
522
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800523 NOTE: this API is only valid when detail option is ON (which is OFF by default)
524
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800525 @handle: handle returned by cs_open()
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800526 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800527 @op_type: Operand type to be found.
528
529 @return: number of operands of given type @op_type in instruction @insn,
530 or -1 on failure.
531*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800532CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200533int cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800534
535/*
Nguyen Anh Quynh4c0ed0b2014-06-03 15:06:20 +0700536 Retrieve the position of operand of given type in <arch>.operands[] array.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800537 Later, the operand can be accessed using the returned position.
538 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
539
Nguyen Anh Quynh2f05ab22014-01-15 11:07:28 +0800540 NOTE: this API is only valid when detail option is ON (which is OFF by default)
541
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800542 @handle: handle returned by cs_open()
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800543 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800544 @op_type: Operand type to be found.
545 @position: position of the operand to be found. This must be in the range
546 [1, cs_op_count(handle, insn, op_type)]
547
Nguyen Anh Quynh4c0ed0b2014-06-03 15:06:20 +0700548 @return: index of operand of given type @op_type in <arch>.operands[] array
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800549 in instruction @insn, or -1 on failure.
550*/
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800551CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200552int cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800553 unsigned int position);
554
555#ifdef __cplusplus
556}
557#endif
558
559#endif