Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | #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 |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | #include <stdbool.h> |
| 13 | |
| 14 | // Handle using with all API |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 15 | typedef size_t csh; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 16 | |
| 17 | // Architecture type |
| 18 | typedef 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 |
| 27 | typedef enum cs_mode { |
| 28 | CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 29 | 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 36 | |
| 37 | CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode |
| 38 | } cs_mode; |
| 39 | |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 40 | // Option type |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame^] | 41 | typedef enum cs_opt_type { |
| 42 | CS_OPT_SYNTAX = 1, // Asssembly syntax option |
| 43 | } cs_opt_type; |
| 44 | |
| 45 | // Option value |
| 46 | typedef enum cs_opt_value { |
| 47 | CS_OPT_V_INTEL = 1, // X86 Intel asm syntax (CS_OPT_SYNTAX) |
| 48 | CS_OPT_V_ATT, // X86 ATT asm syntax (CS_OPT_SYNTAX) |
| 49 | } cs_opt_value; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 50 | |
| 51 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 52 | #include "arm.h" |
| 53 | #include "arm64.h" |
| 54 | #include "mips.h" |
| 55 | #include "x86.h" |
| 56 | |
| 57 | // Detail information of disassembled instruction |
| 58 | typedef 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 Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 64 | // Address of this instruction |
Nguyen Anh Quynh | f2a649e | 2013-12-03 12:21:01 +0800 | [diff] [blame] | 65 | uint64_t address; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 66 | |
| 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 Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 77 | unsigned int regs_read_count; // number of implicit registers read by this insn |
| 78 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 79 | unsigned int regs_write[32]; // list of implicit registers modified by this instruction |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 80 | unsigned int regs_write_count; // number of implicit registers modified by this insn |
| 81 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 82 | unsigned int groups[8]; // list of group this instruction belong to |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 83 | unsigned int groups_count; // number of groups this insn belongs to |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 84 | |
| 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() |
| 96 | typedef 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 | */ |
| 113 | void 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 | */ |
| 125 | cs_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 | */ |
| 135 | cs_err cs_close(csh handle); |
| 136 | |
| 137 | /* |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 138 | Set option for disassembling |
| 139 | |
| 140 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame^] | 141 | @type: type of option to be set |
| 142 | @value: option value for corresponding , which can be OR by several cs_opt enums |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 143 | |
| 144 | @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum |
| 145 | for detailed error). |
| 146 | */ |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame^] | 147 | cs_err cs_option(csh handle, cs_opt_type type, cs_opt_value value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 148 | |
| 149 | /* |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 150 | 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 | */ |
| 157 | cs_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 Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 167 | @address: address of the first insn in given raw code buffer |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 168 | @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 Quynh | 029df20 | 2013-12-03 11:36:54 +0800 | [diff] [blame] | 173 | |
| 174 | On failure, call cs_errno() for error code. |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 175 | */ |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 176 | size_t cs_disasm(csh handle, |
| 177 | unsigned char *code, size_t code_size, |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 178 | uint64_t address, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 179 | size_t count, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 180 | 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 Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 193 | @address: address of the first insn in given raw code buffer |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 194 | @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 Quynh | 029df20 | 2013-12-03 11:36:54 +0800 | [diff] [blame] | 199 | |
| 200 | On failure, call cs_errno() for error code. |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 201 | */ |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 202 | size_t cs_disasm_dyn(csh handle, |
| 203 | unsigned char *code, size_t code_size, |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 204 | uint64_t address, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 205 | size_t count, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 206 | 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 | */ |
| 213 | void 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 Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 219 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 220 | @reg: register id |
| 221 | @return: string name of the register, or NULL if @reg_id is invalid. |
| 222 | */ |
| 223 | char *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 Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 229 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 230 | @insn: instruction id |
| 231 | |
| 232 | @return: string name of the instruction, or NULL if @insn_id is invalid. |
| 233 | */ |
| 234 | char *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 | */ |
| 247 | bool 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 | */ |
| 259 | bool 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 | */ |
| 271 | bool 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 Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 277 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 278 | @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 | */ |
| 284 | int 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 Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 291 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 292 | @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 | */ |
| 300 | int 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 |