Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Second-Best 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 | static csh handle; |
| 11 | |
| 12 | struct platform { |
| 13 | cs_arch arch; |
| 14 | cs_mode mode; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 15 | unsigned char *code; |
| 16 | size_t size; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | char *comment; |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 18 | cs_opt_type opt_type; |
| 19 | cs_opt_value opt_value; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 20 | }; |
| 21 | |
Mr. eXoDia | 9be1f93 | 2014-08-26 12:46:15 +0200 | [diff] [blame] | 22 | 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] | 23 | { |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 24 | unsigned char *c; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 25 | |
| 26 | printf("%s", comment); |
| 27 | for (c = str; c < str + len; c++) { |
| 28 | printf("0x%02x ", *c & 0xff); |
| 29 | } |
| 30 | |
| 31 | printf("\n"); |
| 32 | } |
| 33 | |
| 34 | static void print_insn_detail(csh ud, cs_mode mode, cs_insn *ins) |
| 35 | { |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 36 | int count, i; |
Nguyen Anh Quynh | 54015f4 | 2014-04-10 00:02:04 +0800 | [diff] [blame] | 37 | cs_x86 *x86; |
| 38 | |
| 39 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 40 | if (ins->detail == NULL) |
| 41 | return; |
| 42 | |
| 43 | x86 = &(ins->detail->x86); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 44 | |
Nguyen Anh Quynh | d948dd4 | 2014-06-20 13:55:24 +0800 | [diff] [blame] | 45 | print_string_hex("\tPrefix:", x86->prefix, 4); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 46 | |
Nguyen Anh Quynh | bb6440c | 2014-06-24 21:46:54 +0800 | [diff] [blame] | 47 | print_string_hex("\tOpcode:", x86->opcode, 4); |
Nguyen Anh Quynh | 9f6ed71 | 2014-07-01 10:13:28 +0800 | [diff] [blame] | 48 | |
| 49 | printf("\trex: 0x%x\n", x86->rex); |
| 50 | |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 51 | printf("\taddr_size: %u\n", x86->addr_size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 52 | printf("\tmodrm: 0x%x\n", x86->modrm); |
| 53 | printf("\tdisp: 0x%x\n", x86->disp); |
| 54 | |
| 55 | // SIB is not available in 16-bit mode |
| 56 | if ((mode & CS_MODE_16) == 0) { |
| 57 | printf("\tsib: 0x%x\n", x86->sib); |
Nguyen Anh Quynh | cb2c4f9 | 2014-05-17 13:12:29 +0800 | [diff] [blame] | 58 | if (x86->sib_base != X86_REG_INVALID) |
| 59 | printf("\t\tsib_base: %s\n", cs_reg_name(handle, x86->sib_base)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 60 | if (x86->sib_index != X86_REG_INVALID) |
Nguyen Anh Quynh | cb2c4f9 | 2014-05-17 13:12:29 +0800 | [diff] [blame] | 61 | printf("\t\tsib_index: %s\n", cs_reg_name(handle, x86->sib_index)); |
| 62 | if (x86->sib_scale != 0) |
| 63 | printf("\t\tsib_scale: %d\n", x86->sib_scale); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Nguyen Anh Quynh | 1a66fec | 2014-06-26 12:09:15 +0800 | [diff] [blame] | 66 | // SSE code condition |
Nguyen Anh Quynh | 4c5eabc | 2014-06-24 23:50:41 +0800 | [diff] [blame] | 67 | if (x86->sse_cc != X86_SSE_CC_INVALID) { |
| 68 | printf("\tsse_cc: %u\n", x86->sse_cc); |
| 69 | } |
| 70 | |
Nguyen Anh Quynh | 1a66fec | 2014-06-26 12:09:15 +0800 | [diff] [blame] | 71 | // AVX code condition |
Nguyen Anh Quynh | 4c5eabc | 2014-06-24 23:50:41 +0800 | [diff] [blame] | 72 | if (x86->avx_cc != X86_AVX_CC_INVALID) { |
| 73 | printf("\tavx_cc: %u\n", x86->avx_cc); |
| 74 | } |
| 75 | |
Nguyen Anh Quynh | 1a66fec | 2014-06-26 12:09:15 +0800 | [diff] [blame] | 76 | // AVX Suppress All Exception |
| 77 | if (x86->avx_sae) { |
| 78 | printf("\tavx_sae: %u\n", x86->avx_sae); |
| 79 | } |
| 80 | |
| 81 | // AVX Rounding Mode |
| 82 | if (x86->avx_rm != X86_AVX_RM_INVALID) { |
| 83 | printf("\tavx_rm: %u\n", x86->avx_rm); |
| 84 | } |
| 85 | |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 86 | count = cs_op_count(ud, ins, X86_OP_IMM); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 87 | if (count) { |
| 88 | printf("\timm_count: %u\n", count); |
| 89 | for (i = 1; i < count + 1; i++) { |
| 90 | int index = cs_op_index(ud, ins, X86_OP_IMM, i); |
| 91 | printf("\t\timms[%u]: 0x%"PRIx64 "\n", i, x86->operands[index].imm); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (x86->op_count) |
| 96 | printf("\top_count: %u\n", x86->op_count); |
| 97 | for (i = 0; i < x86->op_count; i++) { |
| 98 | cs_x86_op *op = &(x86->operands[i]); |
| 99 | |
| 100 | switch((int)op->type) { |
| 101 | case X86_OP_REG: |
| 102 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 103 | break; |
| 104 | case X86_OP_IMM: |
| 105 | printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm); |
| 106 | break; |
| 107 | case X86_OP_FP: |
| 108 | printf("\t\toperands[%u].type: FP = %f\n", i, op->fp); |
| 109 | break; |
| 110 | case X86_OP_MEM: |
| 111 | printf("\t\toperands[%u].type: MEM\n", i); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 112 | if (op->mem.segment != X86_REG_INVALID) |
| 113 | printf("\t\t\toperands[%u].mem.segment: REG = %s\n", i, cs_reg_name(handle, op->mem.segment)); |
| 114 | if (op->mem.base != X86_REG_INVALID) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 115 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base)); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 116 | if (op->mem.index != X86_REG_INVALID) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 117 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index)); |
| 118 | if (op->mem.scale != 1) |
| 119 | printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale); |
| 120 | if (op->mem.disp != 0) |
| 121 | printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp); |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 126 | |
Nguyen Anh Quynh | 92a3d4c | 2014-06-25 23:10:39 +0800 | [diff] [blame] | 127 | // AVX broadcast type |
Nguyen Anh Quynh | 0d71645 | 2014-06-24 22:51:56 +0800 | [diff] [blame] | 128 | if (op->avx_bcast != X86_AVX_BCAST_INVALID) |
| 129 | printf("\t\toperands[%u].avx_bcast: %u\n", i, op->avx_bcast); |
| 130 | |
Nguyen Anh Quynh | 92a3d4c | 2014-06-25 23:10:39 +0800 | [diff] [blame] | 131 | // AVX zero opmask {z} |
Nguyen Anh Quynh | 1a66fec | 2014-06-26 12:09:15 +0800 | [diff] [blame] | 132 | if (op->avx_zero_opmask != false) |
| 133 | printf("\t\toperands[%u].avx_zero_opmask: TRUE\n", i); |
Nguyen Anh Quynh | 92a3d4c | 2014-06-25 23:10:39 +0800 | [diff] [blame] | 134 | |
Nguyen Anh Quynh | f1ec526 | 2014-06-25 22:03:18 +0800 | [diff] [blame] | 135 | printf("\t\toperands[%u].size: %u\n", i, op->size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | printf("\n"); |
| 139 | } |
| 140 | |
| 141 | static void test() |
| 142 | { |
| 143 | //#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00" |
| 144 | //#define X86_CODE32 "\x05\x78\x56\x00\x00" |
| 145 | //#define X86_CODE32 "\x01\xd8" |
| 146 | //#define X86_CODE32 "\x05\x23\x01\x00\x00" |
| 147 | //#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00" |
| 148 | //#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00" |
| 149 | //#define X86_CODE32 "\xb4\xc6" // mov ah, 0x6c |
| 150 | //#define X86_CODE32 "\x77\x04" // ja +6 |
| 151 | #define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00" |
Nguyen Anh Quynh | 6d552e5 | 2013-12-03 18:23:53 +0800 | [diff] [blame] | 152 | //#define X86_CODE64 "\xe9\x79\xff\xff\xff" // jmp 0xf7e |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 153 | |
| 154 | #define X86_CODE16 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6" |
| 155 | //#define X86_CODE16 "\x67\x00\x18" |
| 156 | #define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6" |
Nguyen Anh Quynh | b4ce383 | 2013-12-06 08:06:21 +0800 | [diff] [blame] | 157 | //#define X86_CODE32 "\x0f\xa7\xc0" // xstorerng |
Nguyen Anh Quynh | b9b3d29 | 2013-12-02 16:21:55 +0800 | [diff] [blame] | 158 | //#define X86_CODE32 "\x64\xa1\x18\x00\x00\x00" // mov eax, dword ptr fs:[18] |
| 159 | //#define X86_CODE32 "\x64\xa3\x00\x00\x00\x00" // mov [fs:0x0], eax |
Nguyen Anh Quynh | 9c950c1 | 2013-12-02 23:20:07 +0800 | [diff] [blame] | 160 | //#define X86_CODE32 "\xd1\xe1" // shl ecx, 1 |
| 161 | //#define X86_CODE32 "\xd1\xc8" // ror eax, 1 |
Nguyen Anh Quynh | d57fa4f | 2013-12-03 00:45:39 +0800 | [diff] [blame] | 162 | //#define X86_CODE32 "\x83\xC0\x80" // add eax, -x80 |
Nguyen Anh Quynh | 641b783 | 2013-12-04 16:56:40 +0800 | [diff] [blame] | 163 | //#define X86_CODE32 "\xe8\x26\xfe\xff\xff" // call 0xe2b |
| 164 | //#define X86_CODE32 "\xcd\x80" // int 0x80 |
Nguyen Anh Quynh | d57fa4f | 2013-12-03 00:45:39 +0800 | [diff] [blame] | 165 | //#define X86_CODE32 "\x24\xb8" // and $0xb8,%al |
Nguyen Anh Quynh | bdaf3b5 | 2013-12-09 00:55:49 +0800 | [diff] [blame] | 166 | //#define X86_CODE32 "\xf0\x01\xd8" // lock add eax,ebx |
| 167 | //#define X86_CODE32 "\xf3\xaa" // rep stosb |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 168 | |
| 169 | struct platform platforms[] = { |
| 170 | { |
Axel 0vercl0k Souchet | e47b812 | 2014-05-09 21:03:29 +0100 | [diff] [blame] | 171 | CS_ARCH_X86, |
| 172 | CS_MODE_16, |
| 173 | (unsigned char *)X86_CODE16, |
| 174 | sizeof(X86_CODE16) - 1, |
| 175 | "X86 16bit (Intel syntax)" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 176 | }, |
| 177 | { |
Axel 0vercl0k Souchet | e47b812 | 2014-05-09 21:03:29 +0100 | [diff] [blame] | 178 | CS_ARCH_X86, |
| 179 | CS_MODE_32, |
| 180 | (unsigned char *)X86_CODE32, |
| 181 | sizeof(X86_CODE32) - 1, |
| 182 | "X86 32 (AT&T syntax)", |
| 183 | CS_OPT_SYNTAX, |
| 184 | CS_OPT_SYNTAX_ATT, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 185 | }, |
| 186 | { |
Axel 0vercl0k Souchet | e47b812 | 2014-05-09 21:03:29 +0100 | [diff] [blame] | 187 | CS_ARCH_X86, |
| 188 | CS_MODE_32, |
| 189 | (unsigned char *)X86_CODE32, |
| 190 | sizeof(X86_CODE32) - 1, |
| 191 | "X86 32 (Intel syntax)" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 192 | }, |
| 193 | { |
Axel 0vercl0k Souchet | e47b812 | 2014-05-09 21:03:29 +0100 | [diff] [blame] | 194 | CS_ARCH_X86, |
| 195 | CS_MODE_64, |
| 196 | (unsigned char *)X86_CODE64, |
| 197 | sizeof(X86_CODE64) - 1, |
| 198 | "X86 64 (Intel syntax)" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 199 | }, |
| 200 | }; |
| 201 | |
Nguyen Anh Quynh | 5df9e4b | 2013-12-03 15:02:12 +0800 | [diff] [blame] | 202 | uint64_t address = 0x1000; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 203 | cs_insn *insn; |
| 204 | int i; |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 205 | size_t count; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 206 | |
| 207 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
Nguyen Anh Quynh | ceae16d | 2014-01-19 16:04:23 +0800 | [diff] [blame] | 208 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 209 | if (err) { |
| 210 | printf("Failed on cs_open() with error returned: %u\n", err); |
Nguyen Anh Quynh | 4914691 | 2014-02-22 16:54:44 +0800 | [diff] [blame] | 211 | continue; |
Nguyen Anh Quynh | ceae16d | 2014-01-19 16:04:23 +0800 | [diff] [blame] | 212 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 213 | |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 214 | if (platforms[i].opt_type) |
| 215 | cs_option(handle, platforms[i].opt_type, platforms[i].opt_value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 216 | |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 217 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 218 | |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 219 | 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] | 220 | if (count) { |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 221 | size_t j; |
| 222 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 223 | printf("****************\n"); |
| 224 | printf("Platform: %s\n", platforms[i].comment); |
| 225 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 226 | printf("Disasm:\n"); |
| 227 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 228 | for (j = 0; j < count; j++) { |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 229 | printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 230 | print_insn_detail(handle, platforms[i].mode, &insn[j]); |
| 231 | } |
Nguyen Anh Quynh | 7b7b40c | 2013-12-03 12:24:06 +0800 | [diff] [blame] | 232 | 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] | 233 | |
Nguyen Anh Quynh | 0beb0d4 | 2014-08-27 22:55:29 +0800 | [diff] [blame] | 234 | // free memory allocated by cs_disasm() |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 235 | cs_free(insn, count); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 236 | } else { |
| 237 | printf("****************\n"); |
| 238 | printf("Platform: %s\n", platforms[i].comment); |
| 239 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 240 | printf("ERROR: Failed to disasm given code!\n"); |
| 241 | } |
| 242 | |
| 243 | printf("\n"); |
| 244 | |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 245 | cs_close(&handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | int main() |
| 250 | { |
| 251 | test(); |
| 252 | |
| 253 | return 0; |
| 254 | } |