| 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Daniel Collin, 2013-2019 */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include <capstone/platform.h> |
| 8 | #include <capstone/capstone.h> |
| 9 | |
| 10 | struct platform { |
| 11 | cs_arch arch; |
| 12 | cs_mode mode; |
| 13 | unsigned char* code; |
| 14 | size_t size; |
| 15 | const char* comment; |
| 16 | }; |
| 17 | |
| 18 | static csh handle; |
| 19 | |
| 20 | static void print_string_hex(const char* comment, unsigned char* str, size_t len) |
| 21 | { |
| 22 | unsigned char *c; |
| 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 | |
| 32 | const char* s_addressing_modes[] = { |
| 33 | "<invalid mode>", |
| 34 | |
| 35 | "Register Direct - Data", |
| 36 | "Register Direct - Address", |
| 37 | |
| 38 | "Register Indirect - Address", |
| 39 | "Register Indirect - Address with Postincrement", |
| 40 | "Register Indirect - Address with Predecrement", |
| 41 | "Register Indirect - Address with Displacement", |
| 42 | |
| 43 | "Address Register Indirect With Index - 8-bit displacement", |
| 44 | "Address Register Indirect With Index - Base displacement", |
| 45 | |
| 46 | "Memory indirect - Postindex", |
| 47 | "Memory indirect - Preindex", |
| 48 | |
| 49 | "Program Counter Indirect - with Displacement", |
| 50 | |
| 51 | "Program Counter Indirect with Index - with 8-Bit Displacement", |
| 52 | "Program Counter Indirect with Index - with Base Displacement", |
| 53 | |
| 54 | "Program Counter Memory Indirect - Postindexed", |
| 55 | "Program Counter Memory Indirect - Preindexed", |
| 56 | |
| 57 | "Absolute Data Addressing - Short", |
| 58 | "Absolute Data Addressing - Long", |
| 59 | "Immediate value", |
| 60 | }; |
| 61 | |
| 62 | static void print_read_write_regs(cs_detail* detail) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | for (i = 0; i < detail->regs_read_count; ++i) |
| 67 | { |
| 68 | uint16_t reg_id = detail->regs_read[i]; |
| 69 | const char* reg_name = cs_reg_name(handle, reg_id); |
| 70 | printf("\treading from reg: %s\n", reg_name); |
| 71 | } |
| 72 | |
| 73 | for (i = 0; i < detail->regs_write_count; ++i) |
| 74 | { |
| 75 | uint16_t reg_id = detail->regs_write[i]; |
| 76 | const char* reg_name = cs_reg_name(handle, reg_id); |
| 77 | printf("\twriting to reg: %s\n", reg_name); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void print_insn_detail(cs_insn *ins) |
| 82 | { |
| 83 | cs_m68k* m68k; |
| 84 | cs_detail* detail; |
| 85 | int i; |
| 86 | |
| 87 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 88 | if (ins->detail == NULL) |
| 89 | return; |
| 90 | |
| 91 | detail = ins->detail; |
| 92 | m68k = &detail->m68k; |
| 93 | if (m68k->op_count) |
| 94 | printf("\top_count: %u\n", m68k->op_count); |
| 95 | |
| 96 | print_read_write_regs(detail); |
| 97 | |
| 98 | printf("\tgroups_count: %u\n", detail->groups_count); |
| 99 | |
| 100 | for (i = 0; i < m68k->op_count; i++) { |
| 101 | cs_m68k_op* op = &(m68k->operands[i]); |
| 102 | |
| 103 | switch((int)op->type) { |
| 104 | default: |
| 105 | break; |
| 106 | case M68K_OP_REG: |
| 107 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 108 | break; |
| 109 | case M68K_OP_IMM: |
| 110 | printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm); |
| 111 | break; |
| 112 | case M68K_OP_MEM: |
| 113 | printf("\t\toperands[%u].type: MEM\n", i); |
| 114 | if (op->mem.base_reg != M68K_REG_INVALID) |
| 115 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 116 | i, cs_reg_name(handle, op->mem.base_reg)); |
| 117 | if (op->mem.index_reg != M68K_REG_INVALID) { |
| 118 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", |
| 119 | i, cs_reg_name(handle, op->mem.index_reg)); |
| 120 | printf("\t\t\toperands[%u].mem.index: size = %c\n", |
| 121 | i, op->mem.index_size ? 'l' : 'w'); |
| 122 | } |
| 123 | if (op->mem.disp != 0) |
| 124 | printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp); |
| 125 | if (op->mem.scale != 0) |
| 126 | printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale); |
| 127 | |
| 128 | printf("\t\taddress mode: %s\n", s_addressing_modes[op->address_mode]); |
| 129 | break; |
| 130 | case M68K_OP_FP_SINGLE: |
| 131 | printf("\t\toperands[%u].type: FP_SINGLE\n", i); |
| 132 | printf("\t\t\toperands[%u].simm: %f\n", i, op->simm); |
| 133 | break; |
| 134 | case M68K_OP_FP_DOUBLE: |
| 135 | printf("\t\toperands[%u].type: FP_DOUBLE\n", i); |
| 136 | printf("\t\t\toperands[%u].dimm: %lf\n", i, op->dimm); |
| 137 | break; |
| 138 | case M68K_OP_REG_BITS: |
| 139 | printf("\t\toperands[%u].type: REG_BITS = $%x\n", i, op->register_bits); |
| 140 | break; |
| 141 | case M68K_OP_REG_PAIR: |
| 142 | printf("\t\toperands[%u].type: REG_PAIR = (%s, %s)\n", i, |
| 143 | cs_reg_name(handle, op->reg_pair.reg_0), |
| 144 | cs_reg_name(handle, op->reg_pair.reg_1)); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | printf("\n"); |
| 150 | } |
| 151 | |
| 152 | static void test() |
| 153 | { |
| 154 | #define M68K_CODE "\xf0\x10\xf0\x00\x48\xaf\xff\xff\x7f\xff\x11\xb0\x01\x37\x7f\xff\xff\xff\x12\x34\x56\x78\x01\x33\x10\x10\x10\x10\x32\x32\x32\x32\x4C\x00\x54\x04\x48\xe7\xe0\x30\x4C\xDF\x0C\x07\xd4\x40\x87\x5a\x4e\x71\x02\xb4\xc0\xde\xc0\xde\x5c\x00\x1d\x80\x71\x12\x01\x23\xf2\x3c\x44\x22\x40\x49\x0e\x56\x54\xc5\xf2\x3c\x44\x00\x44\x7a\x00\x00\xf2\x00\x0a\x28\x4E\xB9\x00\x00\x00\x12\x4E\x75" |
| 155 | struct platform platforms[] = { |
| 156 | { |
| 157 | CS_ARCH_M68K, |
| 158 | (cs_mode)(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_040), |
| 159 | (unsigned char*)M68K_CODE, |
| 160 | sizeof(M68K_CODE) - 1, |
| 161 | "M68K", |
| 162 | }, |
| 163 | }; |
| 164 | |
| 165 | uint64_t address = 0x01000; |
| 166 | cs_insn *insn; |
| 167 | int i; |
| 168 | size_t count; |
| 169 | |
| 170 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 171 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 172 | if (err) { |
| 173 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 174 | abort(); |
| 175 | } |
| 176 | |
| 177 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 178 | |
| 179 | count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
| 180 | if (count) { |
| 181 | size_t j; |
| 182 | |
| 183 | printf("****************\n"); |
| 184 | printf("Platform: %s\n", platforms[i].comment); |
| 185 | print_string_hex("Code: ", platforms[i].code, platforms[i].size); |
| 186 | printf("Disasm:\n"); |
| 187 | |
| 188 | for (j = 0; j < count; j++) { |
| 189 | assert(address == insn[j].address && "this means the size of the previous instruction was incorrect"); |
| 190 | address += insn[j].size; |
| 191 | printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
| 192 | print_insn_detail(&insn[j]); |
| 193 | } |
| 194 | printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size); |
| 195 | |
| 196 | // free memory allocated by cs_disasm() |
| 197 | cs_free(insn, count); |
| 198 | } else { |
| 199 | printf("****************\n"); |
| 200 | printf("Platform: %s\n", platforms[i].comment); |
| 201 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 202 | printf("ERROR: Failed to disasm given code!\n"); |
| 203 | abort(); |
| 204 | } |
| 205 | |
| 206 | printf("\n"); |
| 207 | |
| 208 | cs_close(&handle); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int main() |
| 213 | { |
| 214 | test(); |
| 215 | |
| 216 | return 0; |
| 217 | } |