capstone.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. #ifndef CAPSTONE_ENGINE_H
  2. #define CAPSTONE_ENGINE_H
  3. /* Capstone Disassembly Engine */
  4. /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2016 */
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #include <stdarg.h>
  9. #if defined(CAPSTONE_HAS_OSXKERNEL)
  10. #include <libkern/libkern.h>
  11. #else
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #endif
  15. #include "platform.h"
  16. #ifdef _MSC_VER
  17. #pragma warning(disable:4201)
  18. #pragma warning(disable:4100)
  19. #define CAPSTONE_API __cdecl
  20. #ifdef CAPSTONE_SHARED
  21. #define CAPSTONE_EXPORT __declspec(dllexport)
  22. #else // defined(CAPSTONE_STATIC)
  23. #define CAPSTONE_EXPORT
  24. #endif
  25. #else
  26. #define CAPSTONE_API
  27. #if defined(__GNUC__) && !defined(CAPSTONE_STATIC)
  28. #define CAPSTONE_EXPORT __attribute__((visibility("default")))
  29. #else // defined(CAPSTONE_STATIC)
  30. #define CAPSTONE_EXPORT
  31. #endif
  32. #endif
  33. #ifdef __GNUC__
  34. #define CAPSTONE_DEPRECATED __attribute__((deprecated))
  35. #elif defined(_MSC_VER)
  36. #define CAPSTONE_DEPRECATED __declspec(deprecated)
  37. #else
  38. #pragma message("WARNING: You need to implement CAPSTONE_DEPRECATED for this compiler")
  39. #define CAPSTONE_DEPRECATED
  40. #endif
  41. // Capstone API version
  42. #define CS_API_MAJOR 4
  43. #define CS_API_MINOR 0
  44. // Version for bleeding edge code of the Github's "next" branch.
  45. // Use this if you want the absolutely latest development code.
  46. // This version number will be bumped up whenever we have a new major change.
  47. #define CS_NEXT_VERSION 5
  48. // Capstone package version
  49. #define CS_VERSION_MAJOR CS_API_MAJOR
  50. #define CS_VERSION_MINOR CS_API_MINOR
  51. #define CS_VERSION_EXTRA 1
  52. /// Macro to create combined version which can be compared to
  53. /// result of cs_version() API.
  54. #define CS_MAKE_VERSION(major, minor) ((major << 8) + minor)
  55. /// Maximum size of an instruction mnemonic string.
  56. #define CS_MNEMONIC_SIZE 32
  57. // Handle using with all API
  58. typedef size_t csh;
  59. /// Architecture type
  60. typedef enum cs_arch {
  61. CS_ARCH_ARM = 0, ///< ARM architecture (including Thumb, Thumb-2)
  62. CS_ARCH_ARM64, ///< ARM-64, also called AArch64
  63. CS_ARCH_MIPS, ///< Mips architecture
  64. CS_ARCH_X86, ///< X86 architecture (including x86 & x86-64)
  65. CS_ARCH_PPC, ///< PowerPC architecture
  66. CS_ARCH_SPARC, ///< Sparc architecture
  67. CS_ARCH_SYSZ, ///< SystemZ architecture
  68. CS_ARCH_XCORE, ///< XCore architecture
  69. CS_ARCH_M68K, ///< 68K architecture
  70. CS_ARCH_TMS320C64X, ///< TMS320C64x architecture
  71. CS_ARCH_M680X, ///< 680X architecture
  72. CS_ARCH_EVM, ///< Ethereum architecture
  73. CS_ARCH_MAX,
  74. CS_ARCH_ALL = 0xFFFF, // All architectures - for cs_support()
  75. } cs_arch;
  76. // Support value to verify diet mode of the engine.
  77. // If cs_support(CS_SUPPORT_DIET) return True, the engine was compiled
  78. // in diet mode.
  79. #define CS_SUPPORT_DIET (CS_ARCH_ALL + 1)
  80. // Support value to verify X86 reduce mode of the engine.
  81. // If cs_support(CS_SUPPORT_X86_REDUCE) return True, the engine was compiled
  82. // in X86 reduce mode.
  83. #define CS_SUPPORT_X86_REDUCE (CS_ARCH_ALL + 2)
  84. /// Mode type
  85. typedef enum cs_mode {
  86. CS_MODE_LITTLE_ENDIAN = 0, ///< little-endian mode (default mode)
  87. CS_MODE_ARM = 0, ///< 32-bit ARM
  88. CS_MODE_16 = 1 << 1, ///< 16-bit mode (X86)
  89. CS_MODE_32 = 1 << 2, ///< 32-bit mode (X86)
  90. CS_MODE_64 = 1 << 3, ///< 64-bit mode (X86, PPC)
  91. CS_MODE_THUMB = 1 << 4, ///< ARM's Thumb mode, including Thumb-2
  92. CS_MODE_MCLASS = 1 << 5, ///< ARM's Cortex-M series
  93. CS_MODE_V8 = 1 << 6, ///< ARMv8 A32 encodings for ARM
  94. CS_MODE_MICRO = 1 << 4, ///< MicroMips mode (MIPS)
  95. CS_MODE_MIPS3 = 1 << 5, ///< Mips III ISA
  96. CS_MODE_MIPS32R6 = 1 << 6, ///< Mips32r6 ISA
  97. CS_MODE_MIPS2 = 1 << 7, ///< Mips II ISA
  98. CS_MODE_V9 = 1 << 4, ///< SparcV9 mode (Sparc)
  99. CS_MODE_QPX = 1 << 4, ///< Quad Processing eXtensions mode (PPC)
  100. CS_MODE_M68K_000 = 1 << 1, ///< M68K 68000 mode
  101. CS_MODE_M68K_010 = 1 << 2, ///< M68K 68010 mode
  102. CS_MODE_M68K_020 = 1 << 3, ///< M68K 68020 mode
  103. CS_MODE_M68K_030 = 1 << 4, ///< M68K 68030 mode
  104. CS_MODE_M68K_040 = 1 << 5, ///< M68K 68040 mode
  105. CS_MODE_M68K_060 = 1 << 6, ///< M68K 68060 mode
  106. CS_MODE_BIG_ENDIAN = 1 << 31, ///< big-endian mode
  107. CS_MODE_MIPS32 = CS_MODE_32, ///< Mips32 ISA (Mips)
  108. CS_MODE_MIPS64 = CS_MODE_64, ///< Mips64 ISA (Mips)
  109. CS_MODE_M680X_6301 = 1 << 1, ///< M680X Hitachi 6301,6303 mode
  110. CS_MODE_M680X_6309 = 1 << 2, ///< M680X Hitachi 6309 mode
  111. CS_MODE_M680X_6800 = 1 << 3, ///< M680X Motorola 6800,6802 mode
  112. CS_MODE_M680X_6801 = 1 << 4, ///< M680X Motorola 6801,6803 mode
  113. CS_MODE_M680X_6805 = 1 << 5, ///< M680X Motorola/Freescale 6805 mode
  114. CS_MODE_M680X_6808 = 1 << 6, ///< M680X Motorola/Freescale/NXP 68HC08 mode
  115. CS_MODE_M680X_6809 = 1 << 7, ///< M680X Motorola 6809 mode
  116. CS_MODE_M680X_6811 = 1 << 8, ///< M680X Motorola/Freescale/NXP 68HC11 mode
  117. CS_MODE_M680X_CPU12 = 1 << 9, ///< M680X Motorola/Freescale/NXP CPU12
  118. ///< used on M68HC12/HCS12
  119. CS_MODE_M680X_HCS08 = 1 << 10, ///< M680X Freescale/NXP HCS08 mode
  120. } cs_mode;
  121. typedef void* (CAPSTONE_API *cs_malloc_t)(size_t size);
  122. typedef void* (CAPSTONE_API *cs_calloc_t)(size_t nmemb, size_t size);
  123. typedef void* (CAPSTONE_API *cs_realloc_t)(void *ptr, size_t size);
  124. typedef void (CAPSTONE_API *cs_free_t)(void *ptr);
  125. typedef int (CAPSTONE_API *cs_vsnprintf_t)(char *str, size_t size, const char *format, va_list ap);
  126. /// User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf()
  127. /// By default, Capstone uses system's malloc(), calloc(), realloc(), free() & vsnprintf().
  128. typedef struct cs_opt_mem {
  129. cs_malloc_t malloc;
  130. cs_calloc_t calloc;
  131. cs_realloc_t realloc;
  132. cs_free_t free;
  133. cs_vsnprintf_t vsnprintf;
  134. } cs_opt_mem;
  135. /// Customize mnemonic for instructions with alternative name.
  136. /// To reset existing customized instruction to its default mnemonic,
  137. /// call cs_option(CS_OPT_MNEMONIC) again with the same @id and NULL value
  138. /// for @mnemonic.
  139. typedef struct cs_opt_mnem {
  140. /// ID of instruction to be customized.
  141. unsigned int id;
  142. /// Customized instruction mnemonic.
  143. const char *mnemonic;
  144. } cs_opt_mnem;
  145. /// Runtime option for the disassembled engine
  146. typedef enum cs_opt_type {
  147. CS_OPT_INVALID = 0, ///< No option specified
  148. CS_OPT_SYNTAX, ///< Assembly output syntax
  149. CS_OPT_DETAIL, ///< Break down instruction structure into details
  150. CS_OPT_MODE, ///< Change engine's mode at run-time
  151. CS_OPT_MEM, ///< User-defined dynamic memory related functions
  152. CS_OPT_SKIPDATA, ///< Skip data when disassembling. Then engine is in SKIPDATA mode.
  153. CS_OPT_SKIPDATA_SETUP, ///< Setup user-defined function for SKIPDATA option
  154. CS_OPT_MNEMONIC, ///< Customize instruction mnemonic
  155. CS_OPT_UNSIGNED, ///< print immediate operands in unsigned form
  156. } cs_opt_type;
  157. /// Runtime option value (associated with option type above)
  158. typedef enum cs_opt_value {
  159. CS_OPT_OFF = 0, ///< Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
  160. CS_OPT_ON = 3, ///< Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
  161. CS_OPT_SYNTAX_DEFAULT = 0, ///< Default asm syntax (CS_OPT_SYNTAX).
  162. CS_OPT_SYNTAX_INTEL, ///< X86 Intel asm syntax - default on X86 (CS_OPT_SYNTAX).
  163. CS_OPT_SYNTAX_ATT, ///< X86 ATT asm syntax (CS_OPT_SYNTAX).
  164. CS_OPT_SYNTAX_NOREGNAME, ///< Prints register name with only number (CS_OPT_SYNTAX)
  165. CS_OPT_SYNTAX_MASM, ///< X86 Intel Masm syntax (CS_OPT_SYNTAX).
  166. } cs_opt_value;
  167. /// Common instruction operand types - to be consistent across all architectures.
  168. typedef enum cs_op_type {
  169. CS_OP_INVALID = 0, ///< uninitialized/invalid operand.
  170. CS_OP_REG, ///< Register operand.
  171. CS_OP_IMM, ///< Immediate operand.
  172. CS_OP_MEM, ///< Memory operand.
  173. CS_OP_FP, ///< Floating-Point operand.
  174. } cs_op_type;
  175. /// Common instruction operand access types - to be consistent across all architectures.
  176. /// It is possible to combine access types, for example: CS_AC_READ | CS_AC_WRITE
  177. typedef enum cs_ac_type {
  178. CS_AC_INVALID = 0, ///< Uninitialized/invalid access type.
  179. CS_AC_READ = 1 << 0, ///< Operand read from memory or register.
  180. CS_AC_WRITE = 1 << 1, ///< Operand write to memory or register.
  181. } cs_ac_type;
  182. /// Common instruction groups - to be consistent across all architectures.
  183. typedef enum cs_group_type {
  184. CS_GRP_INVALID = 0, ///< uninitialized/invalid group.
  185. CS_GRP_JUMP, ///< all jump instructions (conditional+direct+indirect jumps)
  186. CS_GRP_CALL, ///< all call instructions
  187. CS_GRP_RET, ///< all return instructions
  188. CS_GRP_INT, ///< all interrupt instructions (int+syscall)
  189. CS_GRP_IRET, ///< all interrupt return instructions
  190. CS_GRP_PRIVILEGE, ///< all privileged instructions
  191. CS_GRP_BRANCH_RELATIVE, ///< all relative branching instructions
  192. } cs_group_type;
  193. /**
  194. User-defined callback function for SKIPDATA option.
  195. See tests/test_skipdata.c for sample code demonstrating this API.
  196. @code: the input buffer containing code to be disassembled.
  197. This is the same buffer passed to cs_disasm().
  198. @code_size: size (in bytes) of the above @code buffer.
  199. @offset: the position of the currently-examining byte in the input
  200. buffer @code mentioned above.
  201. @user_data: user-data passed to cs_option() via @user_data field in
  202. cs_opt_skipdata struct below.
  203. @return: return number of bytes to skip, or 0 to immediately stop disassembling.
  204. */
  205. typedef size_t (CAPSTONE_API *cs_skipdata_cb_t)(const uint8_t *code, size_t code_size, size_t offset, void *user_data);
  206. /// User-customized setup for SKIPDATA option
  207. typedef struct cs_opt_skipdata {
  208. /// Capstone considers data to skip as special "instructions".
  209. /// User can specify the string for this instruction's "mnemonic" here.
  210. /// By default (if @mnemonic is NULL), Capstone use ".byte".
  211. const char *mnemonic;
  212. /// User-defined callback function to be called when Capstone hits data.
  213. /// If the returned value from this callback is positive (>0), Capstone
  214. /// will skip exactly that number of bytes & continue. Otherwise, if
  215. /// the callback returns 0, Capstone stops disassembling and returns
  216. /// immediately from cs_disasm()
  217. /// NOTE: if this callback pointer is NULL, Capstone would skip a number
  218. /// of bytes depending on architectures, as following:
  219. /// Arm: 2 bytes (Thumb mode) or 4 bytes.
  220. /// Arm64: 4 bytes.
  221. /// Mips: 4 bytes.
  222. /// M680x: 1 byte.
  223. /// PowerPC: 4 bytes.
  224. /// Sparc: 4 bytes.
  225. /// SystemZ: 2 bytes.
  226. /// X86: 1 bytes.
  227. /// XCore: 2 bytes.
  228. /// EVM: 1 bytes.
  229. cs_skipdata_cb_t callback; // default value is NULL
  230. /// User-defined data to be passed to @callback function pointer.
  231. void *user_data;
  232. } cs_opt_skipdata;
  233. #include "arm.h"
  234. #include "arm64.h"
  235. #include "m68k.h"
  236. #include "mips.h"
  237. #include "ppc.h"
  238. #include "sparc.h"
  239. #include "systemz.h"
  240. #include "x86.h"
  241. #include "xcore.h"
  242. #include "tms320c64x.h"
  243. #include "m680x.h"
  244. #include "evm.h"
  245. /// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
  246. /// Initialized as memset(., 0, offsetof(cs_detail, ARCH)+sizeof(cs_ARCH))
  247. /// by ARCH_getInstruction in arch/ARCH/ARCHDisassembler.c
  248. /// if cs_detail changes, in particular if a field is added after the union,
  249. /// then update arch/ARCH/ARCHDisassembler.c accordingly
  250. typedef struct cs_detail {
  251. uint16_t regs_read[12]; ///< list of implicit registers read by this insn
  252. uint8_t regs_read_count; ///< number of implicit registers read by this insn
  253. uint16_t regs_write[20]; ///< list of implicit registers modified by this insn
  254. uint8_t regs_write_count; ///< number of implicit registers modified by this insn
  255. uint8_t groups[8]; ///< list of group this instruction belong to
  256. uint8_t groups_count; ///< number of groups this insn belongs to
  257. /// Architecture-specific instruction info
  258. union {
  259. cs_x86 x86; ///< X86 architecture, including 16-bit, 32-bit & 64-bit mode
  260. cs_arm64 arm64; ///< ARM64 architecture (aka AArch64)
  261. cs_arm arm; ///< ARM architecture (including Thumb/Thumb2)
  262. cs_m68k m68k; ///< M68K architecture
  263. cs_mips mips; ///< MIPS architecture
  264. cs_ppc ppc; ///< PowerPC architecture
  265. cs_sparc sparc; ///< Sparc architecture
  266. cs_sysz sysz; ///< SystemZ architecture
  267. cs_xcore xcore; ///< XCore architecture
  268. cs_tms320c64x tms320c64x; ///< TMS320C64x architecture
  269. cs_m680x m680x; ///< M680X architecture
  270. cs_evm evm; ///< Ethereum architecture
  271. };
  272. } cs_detail;
  273. /// Detail information of disassembled instruction
  274. typedef struct cs_insn {
  275. /// Instruction ID (basically a numeric ID for the instruction mnemonic)
  276. /// Find the instruction id in the '[ARCH]_insn' enum in the header file
  277. /// of corresponding architecture, such as 'arm_insn' in arm.h for ARM,
  278. /// 'x86_insn' in x86.h for X86, etc...
  279. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  280. /// NOTE: in Skipdata mode, "data" instruction has 0 for this id field.
  281. unsigned int id;
  282. /// Address (EIP) of this instruction
  283. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  284. uint64_t address;
  285. /// Size of this instruction
  286. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  287. uint16_t size;
  288. /// Machine bytes of this instruction, with number of bytes indicated by @size above
  289. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  290. uint8_t bytes[16];
  291. /// Ascii text of instruction mnemonic
  292. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  293. char mnemonic[CS_MNEMONIC_SIZE];
  294. /// Ascii text of instruction operands
  295. /// This information is available even when CS_OPT_DETAIL = CS_OPT_OFF
  296. char op_str[160];
  297. /// Pointer to cs_detail.
  298. /// NOTE: detail pointer is only valid when both requirements below are met:
  299. /// (1) CS_OP_DETAIL = CS_OPT_ON
  300. /// (2) Engine is not in Skipdata mode (CS_OP_SKIPDATA option set to CS_OPT_ON)
  301. ///
  302. /// NOTE 2: when in Skipdata mode, or when detail mode is OFF, even if this pointer
  303. /// is not NULL, its content is still irrelevant.
  304. cs_detail *detail;
  305. } cs_insn;
  306. /// Calculate the offset of a disassembled instruction in its buffer, given its position
  307. /// in its array of disassembled insn
  308. /// NOTE: this macro works with position (>=1), not index
  309. #define CS_INSN_OFFSET(insns, post) (insns[post - 1].address - insns[0].address)
  310. /// All type of errors encountered by Capstone API.
  311. /// These are values returned by cs_errno()
  312. typedef enum cs_err {
  313. CS_ERR_OK = 0, ///< No error: everything was fine
  314. CS_ERR_MEM, ///< Out-Of-Memory error: cs_open(), cs_disasm(), cs_disasm_iter()
  315. CS_ERR_ARCH, ///< Unsupported architecture: cs_open()
  316. CS_ERR_HANDLE, ///< Invalid handle: cs_op_count(), cs_op_index()
  317. CS_ERR_CSH, ///< Invalid csh argument: cs_close(), cs_errno(), cs_option()
  318. CS_ERR_MODE, ///< Invalid/unsupported mode: cs_open()
  319. CS_ERR_OPTION, ///< Invalid/unsupported option: cs_option()
  320. CS_ERR_DETAIL, ///< Information is unavailable because detail option is OFF
  321. CS_ERR_MEMSETUP, ///< Dynamic memory management uninitialized (see CS_OPT_MEM)
  322. CS_ERR_VERSION, ///< Unsupported version (bindings)
  323. CS_ERR_DIET, ///< Access irrelevant data in "diet" engine
  324. CS_ERR_SKIPDATA, ///< Access irrelevant data for "data" instruction in SKIPDATA mode
  325. CS_ERR_X86_ATT, ///< X86 AT&T syntax is unsupported (opt-out at compile time)
  326. CS_ERR_X86_INTEL, ///< X86 Intel syntax is unsupported (opt-out at compile time)
  327. CS_ERR_X86_MASM, ///< X86 Intel syntax is unsupported (opt-out at compile time)
  328. } cs_err;
  329. /**
  330. Return combined API version & major and minor version numbers.
  331. @major: major number of API version
  332. @minor: minor number of API version
  333. @return hexical number as (major << 8 | minor), which encodes both
  334. major & minor versions.
  335. NOTE: This returned value can be compared with version number made
  336. with macro CS_MAKE_VERSION
  337. For example, second API version would return 1 in @major, and 1 in @minor
  338. The return value would be 0x0101
  339. NOTE: if you only care about returned value, but not major and minor values,
  340. set both @major & @minor arguments to NULL.
  341. */
  342. CAPSTONE_EXPORT
  343. unsigned int CAPSTONE_API cs_version(int *major, int *minor);
  344. /**
  345. This API can be used to either ask for archs supported by this library,
  346. or check to see if the library was compile with 'diet' option (or called
  347. in 'diet' mode).
  348. To check if a particular arch is supported by this library, set @query to
  349. arch mode (CS_ARCH_* value).
  350. To verify if this library supports all the archs, use CS_ARCH_ALL.
  351. To check if this library is in 'diet' mode, set @query to CS_SUPPORT_DIET.
  352. @return True if this library supports the given arch, or in 'diet' mode.
  353. */
  354. CAPSTONE_EXPORT
  355. bool CAPSTONE_API cs_support(int query);
  356. /**
  357. Initialize CS handle: this must be done before any usage of CS.
  358. @arch: architecture type (CS_ARCH_*)
  359. @mode: hardware mode. This is combined of CS_MODE_*
  360. @handle: pointer to handle, which will be updated at return time
  361. @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
  362. for detailed error).
  363. */
  364. CAPSTONE_EXPORT
  365. cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle);
  366. /**
  367. Close CS handle: MUST do to release the handle when it is not used anymore.
  368. NOTE: this must be only called when there is no longer usage of Capstone,
  369. not even access to cs_insn array. The reason is the this API releases some
  370. cached memory, thus access to any Capstone API after cs_close() might crash
  371. your application.
  372. In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0).
  373. @handle: pointer to a handle returned by cs_open()
  374. @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
  375. for detailed error).
  376. */
  377. CAPSTONE_EXPORT
  378. cs_err CAPSTONE_API cs_close(csh *handle);
  379. /**
  380. Set option for disassembling engine at runtime
  381. @handle: handle returned by cs_open()
  382. @type: type of option to be set
  383. @value: option value corresponding with @type
  384. @return: CS_ERR_OK on success, or other value on failure.
  385. Refer to cs_err enum for detailed error.
  386. NOTE: in the case of CS_OPT_MEM, handle's value can be anything,
  387. so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called
  388. even before cs_open()
  389. */
  390. CAPSTONE_EXPORT
  391. cs_err CAPSTONE_API cs_option(csh handle, cs_opt_type type, size_t value);
  392. /**
  393. Report the last error number when some API function fail.
  394. Like glibc's errno, cs_errno might not retain its old value once accessed.
  395. @handle: handle returned by cs_open()
  396. @return: error code of cs_err enum type (CS_ERR_*, see above)
  397. */
  398. CAPSTONE_EXPORT
  399. cs_err CAPSTONE_API cs_errno(csh handle);
  400. /**
  401. Return a string describing given error code.
  402. @code: error code (see CS_ERR_* above)
  403. @return: returns a pointer to a string that describes the error code
  404. passed in the argument @code
  405. */
  406. CAPSTONE_EXPORT
  407. const char * CAPSTONE_API cs_strerror(cs_err code);
  408. /**
  409. Disassemble binary code, given the code buffer, size, address and number
  410. of instructions to be decoded.
  411. This API dynamically allocate memory to contain disassembled instruction.
  412. Resulting instructions will be put into @*insn
  413. NOTE 1: this API will automatically determine memory needed to contain
  414. output disassembled instructions in @insn.
  415. NOTE 2: caller must free the allocated memory itself to avoid memory leaking.
  416. NOTE 3: for system with scarce memory to be dynamically allocated such as
  417. OS kernel or firmware, the API cs_disasm_iter() might be a better choice than
  418. cs_disasm(). The reason is that with cs_disasm(), based on limited available
  419. memory, we have to calculate in advance how many instructions to be disassembled,
  420. which complicates things. This is especially troublesome for the case @count=0,
  421. when cs_disasm() runs uncontrollably (until either end of input buffer, or
  422. when it encounters an invalid instruction).
  423. @handle: handle returned by cs_open()
  424. @code: buffer containing raw binary code to be disassembled.
  425. @code_size: size of the above code buffer.
  426. @address: address of the first instruction in given raw code buffer.
  427. @insn: array of instructions filled in by this API.
  428. NOTE: @insn will be allocated by this function, and should be freed
  429. with cs_free() API.
  430. @count: number of instructions to be disassembled, or 0 to get all of them
  431. @return: the number of successfully disassembled instructions,
  432. or 0 if this function failed to disassemble the given code
  433. On failure, call cs_errno() for error code.
  434. */
  435. CAPSTONE_EXPORT
  436. size_t CAPSTONE_API cs_disasm(csh handle,
  437. const uint8_t *code, size_t code_size,
  438. uint64_t address,
  439. size_t count,
  440. cs_insn **insn);
  441. /**
  442. Deprecated function - to be retired in the next version!
  443. Use cs_disasm() instead of cs_disasm_ex()
  444. */
  445. CAPSTONE_EXPORT
  446. CAPSTONE_DEPRECATED
  447. size_t CAPSTONE_API cs_disasm_ex(csh handle,
  448. const uint8_t *code, size_t code_size,
  449. uint64_t address,
  450. size_t count,
  451. cs_insn **insn);
  452. /**
  453. Free memory allocated by cs_malloc() or cs_disasm() (argument @insn)
  454. @insn: pointer returned by @insn argument in cs_disasm() or cs_malloc()
  455. @count: number of cs_insn structures returned by cs_disasm(), or 1
  456. to free memory allocated by cs_malloc().
  457. */
  458. CAPSTONE_EXPORT
  459. void CAPSTONE_API cs_free(cs_insn *insn, size_t count);
  460. /**
  461. Allocate memory for 1 instruction to be used by cs_disasm_iter().
  462. @handle: handle returned by cs_open()
  463. NOTE: when no longer in use, you can reclaim the memory allocated for
  464. this instruction with cs_free(insn, 1)
  465. */
  466. CAPSTONE_EXPORT
  467. cs_insn * CAPSTONE_API cs_malloc(csh handle);
  468. /**
  469. Fast API to disassemble binary code, given the code buffer, size, address
  470. and number of instructions to be decoded.
  471. This API puts the resulting instruction into a given cache in @insn.
  472. See tests/test_iter.c for sample code demonstrating this API.
  473. NOTE 1: this API will update @code, @size & @address to point to the next
  474. instruction in the input buffer. Therefore, it is convenient to use
  475. cs_disasm_iter() inside a loop to quickly iterate all the instructions.
  476. While decoding one instruction at a time can also be achieved with
  477. cs_disasm(count=1), some benchmarks shown that cs_disasm_iter() can be 30%
  478. faster on random input.
  479. NOTE 2: the cache in @insn can be created with cs_malloc() API.
  480. NOTE 3: for system with scarce memory to be dynamically allocated such as
  481. OS kernel or firmware, this API is recommended over cs_disasm(), which
  482. allocates memory based on the number of instructions to be disassembled.
  483. The reason is that with cs_disasm(), based on limited available memory,
  484. we have to calculate in advance how many instructions to be disassembled,
  485. which complicates things. This is especially troublesome for the case
  486. @count=0, when cs_disasm() runs uncontrollably (until either end of input
  487. buffer, or when it encounters an invalid instruction).
  488. @handle: handle returned by cs_open()
  489. @code: buffer containing raw binary code to be disassembled
  490. @size: size of above code
  491. @address: address of the first insn in given raw code buffer
  492. @insn: pointer to instruction to be filled in by this API.
  493. @return: true if this API successfully decode 1 instruction,
  494. or false otherwise.
  495. On failure, call cs_errno() for error code.
  496. */
  497. CAPSTONE_EXPORT
  498. bool CAPSTONE_API cs_disasm_iter(csh handle,
  499. const uint8_t **code, size_t *size,
  500. uint64_t *address, cs_insn *insn);
  501. /**
  502. Return friendly name of register in a string.
  503. Find the instruction id from header file of corresponding architecture (arm.h for ARM,
  504. x86.h for X86, ...)
  505. WARN: when in 'diet' mode, this API is irrelevant because engine does not
  506. store register name.
  507. @handle: handle returned by cs_open()
  508. @reg_id: register id
  509. @return: string name of the register, or NULL if @reg_id is invalid.
  510. */
  511. CAPSTONE_EXPORT
  512. const char * CAPSTONE_API cs_reg_name(csh handle, unsigned int reg_id);
  513. /**
  514. Return friendly name of an instruction in a string.
  515. Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  516. WARN: when in 'diet' mode, this API is irrelevant because the engine does not
  517. store instruction name.
  518. @handle: handle returned by cs_open()
  519. @insn_id: instruction id
  520. @return: string name of the instruction, or NULL if @insn_id is invalid.
  521. */
  522. CAPSTONE_EXPORT
  523. const char * CAPSTONE_API cs_insn_name(csh handle, unsigned int insn_id);
  524. /**
  525. Return friendly name of a group id (that an instruction can belong to)
  526. Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  527. WARN: when in 'diet' mode, this API is irrelevant because the engine does not
  528. store group name.
  529. @handle: handle returned by cs_open()
  530. @group_id: group id
  531. @return: string name of the group, or NULL if @group_id is invalid.
  532. */
  533. CAPSTONE_EXPORT
  534. const char * CAPSTONE_API cs_group_name(csh handle, unsigned int group_id);
  535. /**
  536. Check if a disassembled instruction belong to a particular group.
  537. Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  538. Internally, this simply verifies if @group_id matches any member of insn->groups array.
  539. NOTE: this API is only valid when detail option is ON (which is OFF by default).
  540. WARN: when in 'diet' mode, this API is irrelevant because the engine does not
  541. update @groups array.
  542. @handle: handle returned by cs_open()
  543. @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
  544. @group_id: group that you want to check if this instruction belong to.
  545. @return: true if this instruction indeed belongs to the given group, or false otherwise.
  546. */
  547. CAPSTONE_EXPORT
  548. bool CAPSTONE_API cs_insn_group(csh handle, const cs_insn *insn, unsigned int group_id);
  549. /**
  550. Check if a disassembled instruction IMPLICITLY used a particular register.
  551. Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  552. Internally, this simply verifies if @reg_id matches any member of insn->regs_read array.
  553. NOTE: this API is only valid when detail option is ON (which is OFF by default)
  554. WARN: when in 'diet' mode, this API is irrelevant because the engine does not
  555. update @regs_read array.
  556. @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
  557. @reg_id: register that you want to check if this instruction used it.
  558. @return: true if this instruction indeed implicitly used the given register, or false otherwise.
  559. */
  560. CAPSTONE_EXPORT
  561. bool CAPSTONE_API cs_reg_read(csh handle, const cs_insn *insn, unsigned int reg_id);
  562. /**
  563. Check if a disassembled instruction IMPLICITLY modified a particular register.
  564. Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  565. Internally, this simply verifies if @reg_id matches any member of insn->regs_write array.
  566. NOTE: this API is only valid when detail option is ON (which is OFF by default)
  567. WARN: when in 'diet' mode, this API is irrelevant because the engine does not
  568. update @regs_write array.
  569. @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
  570. @reg_id: register that you want to check if this instruction modified it.
  571. @return: true if this instruction indeed implicitly modified the given register, or false otherwise.
  572. */
  573. CAPSTONE_EXPORT
  574. bool CAPSTONE_API cs_reg_write(csh handle, const cs_insn *insn, unsigned int reg_id);
  575. /**
  576. Count the number of operands of a given type.
  577. Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  578. NOTE: this API is only valid when detail option is ON (which is OFF by default)
  579. @handle: handle returned by cs_open()
  580. @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
  581. @op_type: Operand type to be found.
  582. @return: number of operands of given type @op_type in instruction @insn,
  583. or -1 on failure.
  584. */
  585. CAPSTONE_EXPORT
  586. int CAPSTONE_API cs_op_count(csh handle, const cs_insn *insn, unsigned int op_type);
  587. /**
  588. Retrieve the position of operand of given type in <arch>.operands[] array.
  589. Later, the operand can be accessed using the returned position.
  590. Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...)
  591. NOTE: this API is only valid when detail option is ON (which is OFF by default)
  592. @handle: handle returned by cs_open()
  593. @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_iter()
  594. @op_type: Operand type to be found.
  595. @position: position of the operand to be found. This must be in the range
  596. [1, cs_op_count(handle, insn, op_type)]
  597. @return: index of operand of given type @op_type in <arch>.operands[] array
  598. in instruction @insn, or -1 on failure.
  599. */
  600. CAPSTONE_EXPORT
  601. int CAPSTONE_API cs_op_index(csh handle, const cs_insn *insn, unsigned int op_type,
  602. unsigned int position);
  603. /// Type of array to keep the list of registers
  604. typedef uint16_t cs_regs[64];
  605. /**
  606. Retrieve all the registers accessed by an instruction, either explicitly or
  607. implicitly.
  608. WARN: when in 'diet' mode, this API is irrelevant because engine does not
  609. store registers.
  610. @handle: handle returned by cs_open()
  611. @insn: disassembled instruction structure returned from cs_disasm() or cs_disasm_iter()
  612. @regs_read: on return, this array contains all registers read by instruction.
  613. @regs_read_count: number of registers kept inside @regs_read array.
  614. @regs_write: on return, this array contains all registers written by instruction.
  615. @regs_write_count: number of registers kept inside @regs_write array.
  616. @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
  617. for detailed error).
  618. */
  619. CAPSTONE_EXPORT
  620. cs_err CAPSTONE_API cs_regs_access(csh handle, const cs_insn *insn,
  621. cs_regs regs_read, uint8_t *regs_read_count,
  622. cs_regs regs_write, uint8_t *regs_write_count);
  623. #ifdef __cplusplus
  624. }
  625. #endif
  626. #endif