Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
Yegor Derevenets | ced9d24 | 2014-09-21 17:27:11 +0200 | [diff] [blame] | 6 | #include "../inttypes.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 7 | |
| 8 | #include <capstone.h> |
| 9 | |
| 10 | struct platform { |
| 11 | cs_arch arch; |
| 12 | cs_mode mode; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 13 | unsigned char *code; |
| 14 | size_t size; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 15 | char *comment; |
| 16 | }; |
| 17 | |
| 18 | static csh handle; |
| 19 | |
Mr. eXoDia | 9be1f93 | 2014-08-26 12:46:15 +0200 | [diff] [blame] | 20 | static void print_string_hex(char *comment, unsigned char *str, size_t len) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | { |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 22 | unsigned char *c; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 23 | |
| 24 | printf("%s", comment); |
| 25 | for (c = str; c < str + len; c++) { |
| 26 | printf("0x%02x ", *c & 0xff); |
| 27 | } |
| 28 | |
| 29 | printf("\n"); |
| 30 | } |
| 31 | |
Nguyen Anh Quynh | 397d0de | 2013-12-16 23:37:08 +0800 | [diff] [blame] | 32 | static void print_insn_detail(cs_insn *ins) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 33 | { |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 34 | int i; |
Nguyen Anh Quynh | 54015f4 | 2014-04-10 00:02:04 +0800 | [diff] [blame] | 35 | cs_mips *mips; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 36 | |
Nguyen Anh Quynh | 54015f4 | 2014-04-10 00:02:04 +0800 | [diff] [blame] | 37 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 38 | if (ins->detail == NULL) |
| 39 | return; |
| 40 | |
| 41 | mips = &(ins->detail->mips); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 42 | if (mips->op_count) |
| 43 | printf("\top_count: %u\n", mips->op_count); |
| 44 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 45 | for (i = 0; i < mips->op_count; i++) { |
| 46 | cs_mips_op *op = &(mips->operands[i]); |
| 47 | switch((int)op->type) { |
| 48 | default: |
| 49 | break; |
| 50 | case MIPS_OP_REG: |
| 51 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 52 | break; |
| 53 | case MIPS_OP_IMM: |
| 54 | printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm); |
| 55 | break; |
| 56 | case MIPS_OP_MEM: |
| 57 | printf("\t\toperands[%u].type: MEM\n", i); |
| 58 | if (op->mem.base != X86_REG_INVALID) |
| 59 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 60 | i, cs_reg_name(handle, op->mem.base)); |
| 61 | if (op->mem.disp != 0) |
| 62 | printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp); |
| 63 | |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | printf("\n"); |
| 70 | } |
| 71 | |
| 72 | static void test() |
| 73 | { |
| 74 | //#define MIPS_CODE "\x8f\xa2\x00\x00" |
| 75 | //#define MIPS_CODE "\x00\x00\xa7\xac\x10\x00\xa2\x8f" |
| 76 | //#define MIPS_CODE "\x21\x30\xe6\x70" // clo $6, $7 |
| 77 | //#define MIPS_CODE "\x00\x00\x00\x00" // nop |
| 78 | //#define MIPS_CODE "\xc6\x23\xe9\xe4" // swc1 $f9, 0x23c6($7) |
| 79 | //#define MIPS_CODE "\x21\x38\x00\x01" // move $7, $8 |
| 80 | #define MIPS_CODE "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56" |
Nguyen Anh Quynh | 9f523d1 | 2013-12-06 13:49:23 +0800 | [diff] [blame] | 81 | //#define MIPS_CODE "\x04\x11\x00\x01" // bal 0x8 |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 82 | #define MIPS_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00" |
Nguyen Anh Quynh | 248519e | 2014-11-09 14:07:07 +0800 | [diff] [blame] | 83 | #define MIPS_32R6M "\x00\x07\x00\x07\x00\x11\x93\x7c\x01\x8c\x8b\x7c\x00\xc7\x48\xd0" |
| 84 | #define MIPS_32R6 "\xec\x80\x00\x19\x7c\x43\x22\xa0" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 85 | |
| 86 | struct platform platforms[] = { |
| 87 | { |
Axel 0vercl0k Souchet | a072459 | 2014-05-09 21:14:19 +0100 | [diff] [blame] | 88 | CS_ARCH_MIPS, |
Nguyen Anh Quynh | 84df600 | 2014-11-13 11:27:51 +0800 | [diff] [blame] | 89 | (cs_mode)(CS_MODE_MIPS32 + CS_MODE_BIG_ENDIAN), |
Axel 0vercl0k Souchet | a072459 | 2014-05-09 21:14:19 +0100 | [diff] [blame] | 90 | (unsigned char *)MIPS_CODE, |
| 91 | sizeof(MIPS_CODE) - 1, |
| 92 | "MIPS-32 (Big-endian)" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 93 | }, |
| 94 | { |
Axel 0vercl0k Souchet | a072459 | 2014-05-09 21:14:19 +0100 | [diff] [blame] | 95 | CS_ARCH_MIPS, |
Nguyen Anh Quynh | 84df600 | 2014-11-13 11:27:51 +0800 | [diff] [blame] | 96 | (cs_mode)(CS_MODE_MIPS64 + CS_MODE_LITTLE_ENDIAN), |
Axel 0vercl0k Souchet | a072459 | 2014-05-09 21:14:19 +0100 | [diff] [blame] | 97 | (unsigned char *)MIPS_CODE2, |
| 98 | sizeof(MIPS_CODE2) - 1, |
| 99 | "MIPS-64-EL (Little-endian)" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 100 | }, |
Nguyen Anh Quynh | 5720cb7 | 2014-10-29 22:35:02 +0800 | [diff] [blame] | 101 | { |
| 102 | CS_ARCH_MIPS, |
Nguyen Anh Quynh | 0d97a3b | 2014-11-13 11:12:52 +0800 | [diff] [blame] | 103 | (cs_mode)(CS_MODE_MIPS32R6 + CS_MODE_MICRO + CS_MODE_BIG_ENDIAN), |
Nguyen Anh Quynh | 248519e | 2014-11-09 14:07:07 +0800 | [diff] [blame] | 104 | (unsigned char*)MIPS_32R6M, |
| 105 | sizeof(MIPS_32R6M) - 1, |
| 106 | "MIPS-32R6 | Micro (Big-endian)" |
| 107 | }, |
| 108 | { |
| 109 | CS_ARCH_MIPS, |
Nguyen Anh Quynh | 0d97a3b | 2014-11-13 11:12:52 +0800 | [diff] [blame] | 110 | (cs_mode)(CS_MODE_MIPS32R6 + CS_MODE_BIG_ENDIAN), |
Nguyen Anh Quynh | 5720cb7 | 2014-10-29 22:35:02 +0800 | [diff] [blame] | 111 | (unsigned char*)MIPS_32R6, |
| 112 | sizeof(MIPS_32R6) - 1, |
Nguyen Anh Quynh | 248519e | 2014-11-09 14:07:07 +0800 | [diff] [blame] | 113 | "MIPS-32R6 (Big-endian)" |
Nguyen Anh Quynh | 5720cb7 | 2014-10-29 22:35:02 +0800 | [diff] [blame] | 114 | }, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 115 | }; |
| 116 | |
Nguyen Anh Quynh | 5df9e4b | 2013-12-03 15:02:12 +0800 | [diff] [blame] | 117 | uint64_t address = 0x1000; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 118 | cs_insn *insn; |
| 119 | int i; |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 120 | size_t count; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 121 | |
| 122 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
Nguyen Anh Quynh | ceae16d | 2014-01-19 16:04:23 +0800 | [diff] [blame] | 123 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 124 | if (err) { |
| 125 | printf("Failed on cs_open() with error returned: %u\n", err); |
Nguyen Anh Quynh | 4914691 | 2014-02-22 16:54:44 +0800 | [diff] [blame] | 126 | continue; |
Nguyen Anh Quynh | ceae16d | 2014-01-19 16:04:23 +0800 | [diff] [blame] | 127 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 128 | |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 129 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 130 | |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 131 | count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 132 | if (count) { |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 133 | size_t j; |
| 134 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 135 | printf("****************\n"); |
| 136 | printf("Platform: %s\n", platforms[i].comment); |
| 137 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 138 | printf("Disasm:\n"); |
| 139 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 140 | for (j = 0; j < count; j++) { |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 141 | printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
Nguyen Anh Quynh | 397d0de | 2013-12-16 23:37:08 +0800 | [diff] [blame] | 142 | print_insn_detail(&insn[j]); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 143 | } |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 144 | printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 145 | |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 146 | // free memory allocated by cs_disasm() |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 147 | cs_free(insn, count); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 148 | } else { |
| 149 | printf("****************\n"); |
| 150 | printf("Platform: %s\n", platforms[i].comment); |
| 151 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 152 | printf("ERROR: Failed to disasm given code!\n"); |
| 153 | } |
| 154 | |
| 155 | printf("\n"); |
| 156 | |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 157 | cs_close(&handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | int main() |
| 162 | { |
| 163 | test(); |
| 164 | |
| 165 | return 0; |
| 166 | } |