blob: f04af71210d6660a991bb9157f5ab531bdf1b553 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001#ifndef __CS_H__
2#define __CS_H__
3
4/* Capstone Disassembler Engine */
5/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include <stdint.h>
12#include <stdbool.h>
13
14// Handle using with all API
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080015typedef size_t csh;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016
17// Architecture type
18typedef enum cs_arch {
19 CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2)
20 CS_ARCH_ARM64, // ARM-64, also called AArch64
21 CS_ARCH_MIPS, // Mips architecture
22 CS_ARCH_X86, // X86 architecture (including x86 & x86-64)
23 CS_ARCH_MAX,
24} cs_arch;
25
26// Mode type
27typedef enum cs_mode {
28 CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080029 CS_MODE_ARM = 0, // 32-bit ARM
30 CS_MODE_16 = 1 << 1, // 16-bit mode
31 CS_MODE_32 = 1 << 2, // 32-bit mode
32 CS_MODE_64 = 1 << 3, // 64-bit mode
33 CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2
34 CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS architecture)
35 CS_MODE_N64 = 1 << 5, // Nintendo-64 mode (MIPS architecture)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080036
37 CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode
38} cs_mode;
39
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080040// Option type
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +080041typedef enum cs_opt_type {
42 CS_OPT_SYNTAX = 1, // Asssembly syntax option
43} cs_opt_type;
44
45// Option value
46typedef enum cs_opt_value {
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +080047 CS_OPT_SYNTAX_INTEL = 1, // X86 Intel asm syntax (CS_OPT_SYNTAX)
48 CS_OPT_SYNTAX_ATT, // X86 ATT asm syntax (CS_OPT_SYNTAX)
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +080049} cs_opt_value;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080050
51
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080052#include "arm.h"
53#include "arm64.h"
54#include "mips.h"
55#include "x86.h"
56
57// Detail information of disassembled instruction
58typedef struct cs_insn {
59 // Instruction ID
60 // Find the instruction id from header file of corresponding architecture,
61 // such as arm.h for ARM, x86.h for X86, etc...
62 unsigned int id;
63
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +080064 // Address of this instruction
Nguyen Anh Quynhf2a649e2013-12-03 12:21:01 +080065 uint64_t address;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080066
67 // Size of this instruction
68 uint16_t size;
69
70 // Ascii text of instruction mnemonic
71 char mnemonic[32];
72
73 // Ascii text of instruction operands
74 char op_str[96];
75
76 unsigned int regs_read[32]; // list of implicit registers read by this instruction
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080077 unsigned int regs_read_count; // number of implicit registers read by this insn
78
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080079 unsigned int regs_write[32]; // list of implicit registers modified by this instruction
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080080 unsigned int regs_write_count; // number of implicit registers modified by this insn
81
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080082 unsigned int groups[8]; // list of group this instruction belong to
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080083 unsigned int groups_count; // number of groups this insn belongs to
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080084
85 // Architecture-specific instruction info
86 union {
87 cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode
88 cs_arm64 arm64; // ARM64 architecture (aka AArch64)
89 cs_arm arm; // ARM architecture (including Thumb/Thumb2)
90 cs_mips mips; // MIPS architecture
91 };
92} cs_insn;
93
94// All type of errors encountered by Capstone API.
95// These are values returned by cs_errno()
96typedef enum cs_err {
97 CS_ERR_OK = 0, // No error: everything was fine
98 CS_ERR_MEM, // Out-Of-Memory error
99 CS_ERR_ARCH, // Unsupported architecture
100 CS_ERR_HANDLE, // Invalid handle
101 CS_ERR_CSH, // Invalid csh argument
102 CS_ERR_MODE, // Invalid/unsupported mode
103} cs_err;
104
105/*
106 Return API version in major and minor numbers.
107
108 @major: major number of API version (for ex: 1)
109 @minor: minor number of API version (for ex: 0)
110
111 For example, first API version would return 1 in @major, and 0 in @minor
112*/
113void cs_version(int *major, int *minor);
114
115/*
116 Initialize CS handle: this must be done before any usage of CS.
117
118 @arch: architecture type (CS_ARCH_*)
119 @mode: hardware mode. This is combined of CS_MODE_*
120 @handle: pointer to handle, which will be updated at return time
121
122 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
123 for detailed error).
124*/
125cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle);
126
127/*
128 Close CS handle: MUST do to release the handle when it is not used anymore.
129
130 @handle: handle returned by cs_open()
131
132 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
133 for detailed error).
134*/
135cs_err cs_close(csh handle);
136
137/*
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800138 Set option for disassembling
139
140 @handle: handle returned by cs_open()
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800141 @type: type of option to be set
142 @value: option value for corresponding , which can be OR by several cs_opt enums
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800143
144 @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
145 for detailed error).
146*/
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800147cs_err cs_option(csh handle, cs_opt_type type, size_t value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800148
149/*
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800150 Report the last error number when some API function fail.
151 Like glibc's errno, cs_errno might not retain its old value once accessed.
152
153 @handle: handle returned by cs_open()
154
155 @return: error code of cs_err enum type (CS_ERR_*, see above)
156*/
157cs_err cs_errno(csh handle);
158
159/*
160 Disasm the binary code in @buffer.
161 Disassembled instructions will be put into @insn
162 NOTE: this API requires the pre-allocated buffer in @insn
163
164 @handle: handle returned by cs_open()
165 @code: buffer containing raw binary code to be disassembled
166 @code_size: size of above code
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800167 @address: address of the first insn in given raw code buffer
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168 @insn: array of insn filled in by this function
169 NOTE: @insn size must be at least @count to avoid buffer overflow
170 @count: number of instrutions to be disassembled, or 0 to get all of them
171 @return: the number of succesfully disassembled instructions,
172 or 0 if this function failed to disassemble the given code
Nguyen Anh Quynh029df202013-12-03 11:36:54 +0800173
174 On failure, call cs_errno() for error code.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800175*/
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800176size_t cs_disasm(csh handle,
177 unsigned char *code, size_t code_size,
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800178 uint64_t address,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800179 size_t count,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800180 cs_insn *insn);
181
182/*
183 Dynamicly allocate memory to contain disasm insn
184 Disassembled instructions will be put into @*insn
185
186 NOTE 1: this API will automatically determine memory needed to contain
187 output disassembled instructions in @insn.
188 NOTE 2: caller must free() the allocated memory itself to avoid memory leaking
189
190 @handle: handle returned by cs_open()
191 @code: buffer containing raw binary code to be disassembled
192 @code_size: size of above code
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800193 @address: address of the first insn in given raw code buffer
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800194 @insn: array of insn filled in by this function
195 NOTE: @insn will be allocated by this function
196 @count: number of instrutions to be disassembled, or 0 to get all of them
197 @return: the number of succesfully disassembled instructions,
198 or 0 if this function failed to disassemble the given code
Nguyen Anh Quynh029df202013-12-03 11:36:54 +0800199
200 On failure, call cs_errno() for error code.
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800201*/
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800202size_t cs_disasm_dyn(csh handle,
203 unsigned char *code, size_t code_size,
Nguyen Anh Quynh612b5d22013-12-03 12:23:09 +0800204 uint64_t address,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800205 size_t count,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800206 cs_insn **insn);
207
208/*
209 Free memory allocated in @insn of cs_disasm_dyn()
210
211 @mem: pointer returned by @insn argument in cs_disasm_dyn()
212*/
213void cs_free(void *mem);
214
215/*
216 Return friendly name of regiser in a string
217 Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
218
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800219 @handle: handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220 @reg: register id
221 @return: string name of the register, or NULL if @reg_id is invalid.
222*/
223char *cs_reg_name(csh handle, unsigned int reg_id);
224
225/*
226 Return friendly name of an instruction in a string
227 Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
228
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800229 @handle: handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800230 @insn: instruction id
231
232 @return: string name of the instruction, or NULL if @insn_id is invalid.
233*/
234char *cs_insn_name(csh handle, unsigned int insn_id);
235
236/*
237 Check if a disassembled instruction belong to a particular group.
238 Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
239 Internally, this simply verifies if @group_id matches any member of insn->groups array.
240
241 @handle: handle returned by cs_open()
242 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn()
243 @group_id: group that you want to check if this instruction belong to.
244
245 @return: true if this instruction indeed belongs to aboved group, or false otherwise.
246*/
247bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id);
248
249/*
250 Check if a disassembled instruction IMPLICITLY used a particular register.
251 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
252 Internally, this simply verifies if @reg_id matches any member of insn->regs_read array.
253
254 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn()
255 @reg_id: register that you want to check if this instruction used it.
256
257 @return: true if this instruction indeed implicitly used aboved register, or false otherwise.
258*/
259bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id);
260
261/*
262 Check if a disassembled instruction IMPLICITLY modified a particular register.
263 Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
264 Internally, this simply verifies if @reg_id matches any member of insn->regs_write array.
265
266 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn()
267 @reg_id: register that you want to check if this instruction modified it.
268
269 @return: true if this instruction indeed implicitly modified aboved register, or false otherwise.
270*/
271bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id);
272
273/*
274 Count the number of operands of a given type.
275 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
276
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800277 @handle: handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800278 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn()
279 @op_type: Operand type to be found.
280
281 @return: number of operands of given type @op_type in instruction @insn,
282 or -1 on failure.
283*/
284int cs_op_count(csh handle, cs_insn *insn, unsigned int op_type);
285
286/*
287 Retrieve the position of operand of given type in arch.op_info[] array.
288 Later, the operand can be accessed using the returned position.
289 Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
290
Nguyen Anh Quynh3640f3c2013-12-01 00:26:27 +0800291 @handle: handle returned by cs_open()
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800292 @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn()
293 @op_type: Operand type to be found.
294 @position: position of the operand to be found. This must be in the range
295 [1, cs_op_count(handle, insn, op_type)]
296
297 @return: index of operand of given type @op_type in arch.op_info[] array
298 in instruction @insn, or -1 on failure.
299*/
300int cs_op_index(csh handle, cs_insn *insn, unsigned int op_type,
301 unsigned int position);
302
303#ifdef __cplusplus
304}
305#endif
306
307#endif