Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
Nguyen Anh Quynh | bfcaba5 | 2015-03-04 17:45:23 +0800 | [diff] [blame] | 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
Nguyen Anh Quynh | ae3649f | 2014-01-02 13:15:07 +0800 | [diff] [blame] | 4 | #ifndef CS_PRIV_H |
| 5 | #define CS_PRIV_H |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 6 | |
pancake | 9c10ace | 2015-02-24 04:55:55 +0100 | [diff] [blame] | 7 | #include <capstone/capstone.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 8 | |
| 9 | #include "MCInst.h" |
| 10 | #include "SStream.h" |
| 11 | |
| 12 | typedef void (*Printer_t)(MCInst *MI, SStream *OS, void *info); |
| 13 | |
| 14 | // function to be called after Printer_t |
| 15 | // this is the best time to gather insn's characteristics |
Nguyen Anh Quynh | 6456481 | 2014-05-19 16:46:31 +0800 | [diff] [blame] | 16 | typedef void (*PostPrinter_t)(csh handle, cs_insn *, char *mnem, MCInst *mci); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 18 | typedef bool (*Disasm_t)(csh handle, const uint8_t *code, size_t code_len, MCInst *instr, uint16_t *size, uint64_t address, void *info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 19 | |
Nguyen Anh Quynh | 650f96c | 2014-07-08 08:59:27 +0800 | [diff] [blame] | 20 | typedef const char *(*GetName_t)(csh handle, unsigned int id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | |
Nguyen Anh Quynh | 1acfd0b | 2014-01-06 10:56:59 +0800 | [diff] [blame] | 22 | typedef void (*GetID_t)(cs_struct *h, cs_insn *insn, unsigned int id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 23 | |
Nguyen Anh Quynh | 2ff665a | 2014-03-11 00:18:50 +0800 | [diff] [blame] | 24 | // return register name, given register ID |
| 25 | typedef char *(*GetRegisterName_t)(unsigned RegNo); |
| 26 | |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 27 | // return registers accessed by instruction |
| 28 | typedef void (*GetRegisterAccess_t)(const cs_insn *insn, |
| 29 | cs_regs regs_read, uint8_t *regs_read_count, |
| 30 | cs_regs regs_write, uint8_t *regs_write_count); |
| 31 | |
Nguyen Anh Quynh | 7c7a8bc | 2013-12-02 13:16:44 +0800 | [diff] [blame] | 32 | // for ARM only |
| 33 | typedef struct ARM_ITStatus { |
Nguyen Anh Quynh | ab6cc88 | 2015-06-03 22:25:22 +0800 | [diff] [blame] | 34 | unsigned char ITStates[8]; |
Nguyen Anh Quynh | 7c7a8bc | 2013-12-02 13:16:44 +0800 | [diff] [blame] | 35 | unsigned int size; |
| 36 | } ARM_ITStatus; |
| 37 | |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 38 | // Customize mnemonic for instructions with alternative name. |
| 39 | struct customized_mnem { |
| 40 | // ID of instruction to be customized. |
| 41 | unsigned int id; |
| 42 | // Customized instruction mnemonic. |
Nguyen Anh Quynh | de654dd | 2015-04-27 09:47:59 +0800 | [diff] [blame] | 43 | char mnemonic[CS_MNEMONIC_SIZE]; |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct insn_mnem { |
| 47 | struct customized_mnem insn; |
| 48 | struct insn_mnem *next; // linked list of customized mnemonics |
| 49 | }; |
| 50 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 51 | struct cs_struct { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 52 | cs_arch arch; |
| 53 | cs_mode mode; |
| 54 | Printer_t printer; // asm printer |
| 55 | void *printer_info; // aux info for printer |
| 56 | Disasm_t disasm; // disassembler |
| 57 | void *getinsn_info; // auxiliary info for printer |
| 58 | bool big_endian; |
| 59 | GetName_t reg_name; |
| 60 | GetName_t insn_name; |
Nguyen Anh Quynh | 650f96c | 2014-07-08 08:59:27 +0800 | [diff] [blame] | 61 | GetName_t group_name; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 62 | GetID_t insn_id; |
| 63 | PostPrinter_t post_printer; |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 64 | cs_err errnum; |
Nguyen Anh Quynh | 7c7a8bc | 2013-12-02 13:16:44 +0800 | [diff] [blame] | 65 | ARM_ITStatus ITBlock; // for Arm only |
Nguyen Anh Quynh | 256090a | 2016-03-14 13:52:23 +0800 | [diff] [blame] | 66 | cs_opt_value detail, imm_unsigned; |
Nguyen Anh Quynh | 2ff665a | 2014-03-11 00:18:50 +0800 | [diff] [blame] | 67 | int syntax; // asm syntax for simple printer such as ARM, Mips & PPC |
Nguyen Anh Quynh | 19b0de3 | 2013-12-31 22:40:04 +0800 | [diff] [blame] | 68 | bool doing_mem; // handling memory operand in InstPrinter code |
Nguyen Anh Quynh | 1acfd0b | 2014-01-06 10:56:59 +0800 | [diff] [blame] | 69 | unsigned short *insn_cache; // index caching for mapping.c |
Nguyen Anh Quynh | 2ff665a | 2014-03-11 00:18:50 +0800 | [diff] [blame] | 70 | GetRegisterName_t get_regname; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 71 | bool skipdata; // set this to True if we skip data when disassembling |
| 72 | uint8_t skipdata_size; // how many bytes to skip |
| 73 | cs_opt_skipdata skipdata_setup; // user-defined skipdata setup |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 74 | uint8_t *regsize_map; // map to register size (x86-only for now) |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 75 | GetRegisterAccess_t reg_access; |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 76 | struct insn_mnem *mnem_list; // linked list of customized instruction mnemonic |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 77 | }; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 78 | |
Daniel Collin | 2ee675c | 2015-08-03 18:45:08 +0200 | [diff] [blame] | 79 | #define MAX_ARCH 9 |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 80 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 81 | // constructor initialization for all archs |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 82 | extern cs_err (*arch_init[MAX_ARCH]) (cs_struct *); |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 83 | |
| 84 | // support cs_option() for all archs |
| 85 | extern cs_err (*arch_option[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value); |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 86 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 87 | extern unsigned int all_arch; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 88 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 89 | extern cs_malloc_t cs_mem_malloc; |
| 90 | extern cs_calloc_t cs_mem_calloc; |
| 91 | extern cs_realloc_t cs_mem_realloc; |
| 92 | extern cs_free_t cs_mem_free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 93 | extern cs_vsnprintf_t cs_vsnprintf; |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 94 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 95 | #endif |