Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <inttypes.h> |
| 6 | |
| 7 | #include <capstone.h> |
| 8 | |
| 9 | struct platform { |
| 10 | cs_arch arch; |
| 11 | cs_mode mode; |
| 12 | unsigned char *code; |
| 13 | size_t size; |
| 14 | char *comment; |
| 15 | }; |
| 16 | |
| 17 | static csh handle; |
| 18 | |
| 19 | static void print_string_hex(char *comment, unsigned char *str, int len) |
| 20 | { |
| 21 | unsigned char *c; |
| 22 | |
| 23 | printf("%s", comment); |
| 24 | for (c = str; c < str + len; c++) { |
| 25 | printf("0x%02x ", *c & 0xff); |
| 26 | } |
| 27 | |
| 28 | printf("\n"); |
| 29 | } |
| 30 | |
| 31 | static void print_insn_detail(cs_insn *ins) |
| 32 | { |
Nguyen Anh Quynh | 54015f4 | 2014-04-10 00:02:04 +0800 | [diff] [blame] | 33 | cs_sysz *sysz; |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 34 | int i; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 35 | |
Nguyen Anh Quynh | 54015f4 | 2014-04-10 00:02:04 +0800 | [diff] [blame] | 36 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 37 | if (ins->detail == NULL) |
| 38 | return; |
| 39 | |
| 40 | sysz = &(ins->detail->sysz); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 41 | if (sysz->op_count) |
| 42 | printf("\top_count: %u\n", sysz->op_count); |
| 43 | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 44 | for (i = 0; i < sysz->op_count; i++) { |
| 45 | cs_sysz_op *op = &(sysz->operands[i]); |
| 46 | switch((int)op->type) { |
| 47 | default: |
| 48 | break; |
| 49 | case SYSZ_OP_REG: |
| 50 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 51 | break; |
Nguyen Anh Quynh | da1e833 | 2014-03-23 11:12:07 +0800 | [diff] [blame] | 52 | case SYSZ_OP_ACREG: |
| 53 | printf("\t\toperands[%u].type: ACREG = %u\n", i, op->reg); |
| 54 | break; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 55 | case SYSZ_OP_IMM: |
Nguyen Anh Quynh | cf2f9e1 | 2014-03-23 09:20:47 +0800 | [diff] [blame] | 56 | printf("\t\toperands[%u].type: IMM = 0x%"PRIx64"\n", i, op->imm); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 57 | break; |
| 58 | case SYSZ_OP_MEM: |
| 59 | printf("\t\toperands[%u].type: MEM\n", i); |
Nguyen Anh Quynh | 7eff377 | 2014-04-10 16:42:06 +0800 | [diff] [blame] | 60 | if (op->mem.base != SYSZ_REG_INVALID) |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 61 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 62 | i, cs_reg_name(handle, op->mem.base)); |
Nguyen Anh Quynh | 7eff377 | 2014-04-10 16:42:06 +0800 | [diff] [blame] | 63 | if (op->mem.index != SYSZ_REG_INVALID) |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 64 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", |
| 65 | i, cs_reg_name(handle, op->mem.index)); |
Nguyen Anh Quynh | e4fae87 | 2014-03-24 17:26:57 +0800 | [diff] [blame] | 66 | if (op->mem.length != 0) |
| 67 | printf("\t\t\toperands[%u].mem.length: 0x%"PRIx64"\n", i, op->mem.length); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 68 | if (op->mem.disp != 0) |
| 69 | printf("\t\t\toperands[%u].mem.disp: 0x%"PRIx64"\n", i, op->mem.disp); |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (sysz->cc != 0) |
| 76 | printf("\tCode condition: %u\n", sysz->cc); |
| 77 | |
| 78 | printf("\n"); |
| 79 | } |
| 80 | |
| 81 | static void test() |
| 82 | { |
Nguyen Anh Quynh | 2579a3f | 2014-04-10 18:14:08 +0800 | [diff] [blame] | 83 | #define SYSZ_CODE "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78\xec\x18\x00\x00\xc1\x7f" |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 84 | |
| 85 | struct platform platforms[] = { |
| 86 | { |
Axel 0vercl0k Souchet | d3eb98b | 2014-05-09 21:28:07 +0100 | [diff] [blame] | 87 | CS_ARCH_SYSZ, |
| 88 | CS_MODE_BIG_ENDIAN, |
| 89 | (unsigned char*)SYSZ_CODE, |
| 90 | sizeof(SYSZ_CODE) - 1, |
| 91 | "SystemZ", |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 92 | }, |
| 93 | }; |
| 94 | |
| 95 | uint64_t address = 0x1000; |
| 96 | cs_insn *insn; |
| 97 | int i; |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 98 | size_t count; |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 99 | |
| 100 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 101 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 102 | if (err) { |
| 103 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 108 | |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 109 | count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 110 | if (count) { |
Nguyen Anh Quynh | 5b556e5 | 2014-04-11 10:15:26 +0800 | [diff] [blame] | 111 | size_t j; |
| 112 | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 113 | printf("****************\n"); |
| 114 | printf("Platform: %s\n", platforms[i].comment); |
| 115 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 116 | printf("Disasm:\n"); |
| 117 | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 118 | for (j = 0; j < count; j++) { |
| 119 | printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
| 120 | print_insn_detail(&insn[j]); |
| 121 | } |
| 122 | printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size); |
| 123 | |
| 124 | // free memory allocated by cs_disasm_ex() |
| 125 | cs_free(insn, count); |
| 126 | } else { |
| 127 | printf("****************\n"); |
| 128 | printf("Platform: %s\n", platforms[i].comment); |
| 129 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 130 | printf("ERROR: Failed to disasm given code!\n"); |
| 131 | } |
| 132 | |
| 133 | printf("\n"); |
| 134 | |
| 135 | cs_close(&handle); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int main() |
| 140 | { |
| 141 | test(); |
| 142 | |
| 143 | return 0; |
| 144 | } |