| 1 | /* Capstone Disassembly Engine */ |
| 2 | /* TMS320C64x Backend by Fotis Loukos <[email protected]> 2016 */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include <capstone/capstone.h> |
| 7 | |
| 8 | struct platform { |
| 9 | cs_arch arch; |
| 10 | cs_mode mode; |
| 11 | unsigned char *code; |
| 12 | size_t size; |
| 13 | const char *comment; |
| 14 | }; |
| 15 | |
| 16 | static csh handle; |
| 17 | |
| 18 | static void print_string_hex(const char *comment, unsigned char *str, size_t len) |
| 19 | { |
| 20 | unsigned char *c; |
| 21 | |
| 22 | printf("%s", comment); |
| 23 | for (c = str; c < str + len; c++) { |
| 24 | printf("0x%02x ", *c & 0xff); |
| 25 | } |
| 26 | |
| 27 | printf("\n"); |
| 28 | } |
| 29 | |
| 30 | static void print_insn_detail(cs_insn *ins) |
| 31 | { |
| 32 | cs_tms320c64x *tms320c64x; |
| 33 | int i; |
| 34 | |
| 35 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 36 | if (ins->detail == NULL) |
| 37 | return; |
| 38 | |
| 39 | tms320c64x = &(ins->detail->tms320c64x); |
| 40 | if (tms320c64x->op_count) |
| 41 | printf("\top_count: %u\n", tms320c64x->op_count); |
| 42 | |
| 43 | for (i = 0; i < tms320c64x->op_count; i++) { |
| 44 | cs_tms320c64x_op *op = &(tms320c64x->operands[i]); |
| 45 | switch((int)op->type) { |
| 46 | default: |
| 47 | break; |
| 48 | case TMS320C64X_OP_REG: |
| 49 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 50 | break; |
| 51 | case TMS320C64X_OP_IMM: |
| 52 | printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm); |
| 53 | break; |
| 54 | case TMS320C64X_OP_MEM: |
| 55 | printf("\t\toperands[%u].type: MEM\n", i); |
| 56 | if (op->mem.base != TMS320C64X_REG_INVALID) |
| 57 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 58 | i, cs_reg_name(handle, op->mem.base)); |
| 59 | printf("\t\t\toperands[%u].mem.disptype: ", i); |
| 60 | if(op->mem.disptype == TMS320C64X_MEM_DISP_INVALID) { |
| 61 | printf("Invalid\n"); |
| 62 | printf("\t\t\toperands[%u].mem.disp: %u\n", i, op->mem.disp); |
| 63 | } |
| 64 | if(op->mem.disptype == TMS320C64X_MEM_DISP_CONSTANT) { |
| 65 | printf("Constant\n"); |
| 66 | printf("\t\t\toperands[%u].mem.disp: %u\n", i, op->mem.disp); |
| 67 | } |
| 68 | if(op->mem.disptype == TMS320C64X_MEM_DISP_REGISTER) { |
| 69 | printf("Register\n"); |
| 70 | printf("\t\t\toperands[%u].mem.disp: %s\n", i, cs_reg_name(handle, op->mem.disp)); |
| 71 | } |
| 72 | printf("\t\t\toperands[%u].mem.unit: %u\n", i, op->mem.unit); |
| 73 | printf("\t\t\toperands[%u].mem.direction: ", i); |
| 74 | if(op->mem.direction == TMS320C64X_MEM_DIR_INVALID) |
| 75 | printf("Invalid\n"); |
| 76 | if(op->mem.direction == TMS320C64X_MEM_DIR_FW) |
| 77 | printf("Forward\n"); |
| 78 | if(op->mem.direction == TMS320C64X_MEM_DIR_BW) |
| 79 | printf("Backward\n"); |
| 80 | printf("\t\t\toperands[%u].mem.modify: ", i); |
| 81 | if(op->mem.modify == TMS320C64X_MEM_MOD_INVALID) |
| 82 | printf("Invalid\n"); |
| 83 | if(op->mem.modify == TMS320C64X_MEM_MOD_NO) |
| 84 | printf("No\n"); |
| 85 | if(op->mem.modify == TMS320C64X_MEM_MOD_PRE) |
| 86 | printf("Pre\n"); |
| 87 | if(op->mem.modify == TMS320C64X_MEM_MOD_POST) |
| 88 | printf("Post\n"); |
| 89 | printf("\t\t\toperands[%u].mem.scaled: %u\n", i, op->mem.scaled); |
| 90 | |
| 91 | |
| 92 | break; |
| 93 | case TMS320C64X_OP_REGPAIR: |
| 94 | printf("\t\toperands[%u].type: REGPAIR = %s:%s\n", i, cs_reg_name(handle, op->reg + 1), cs_reg_name(handle, op->reg)); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | printf("\tFunctional unit: "); |
| 100 | switch(tms320c64x->funit.unit) { |
| 101 | case TMS320C64X_FUNIT_D: |
| 102 | printf("D%u\n", tms320c64x->funit.side); |
| 103 | break; |
| 104 | case TMS320C64X_FUNIT_L: |
| 105 | printf("L%u\n", tms320c64x->funit.side); |
| 106 | break; |
| 107 | case TMS320C64X_FUNIT_M: |
| 108 | printf("M%u\n", tms320c64x->funit.side); |
| 109 | break; |
| 110 | case TMS320C64X_FUNIT_S: |
| 111 | printf("S%u\n", tms320c64x->funit.side); |
| 112 | break; |
| 113 | case TMS320C64X_FUNIT_NO: |
| 114 | printf("No Functional Unit\n"); |
| 115 | break; |
| 116 | default: |
| 117 | printf("Unknown (Unit %u, Side %u)\n", tms320c64x->funit.unit, tms320c64x->funit.side); |
| 118 | break; |
| 119 | } |
| 120 | if(tms320c64x->funit.crosspath == 1) |
| 121 | printf("\tCrosspath: 1\n"); |
| 122 | |
| 123 | if(tms320c64x->condition.reg != TMS320C64X_REG_INVALID) |
| 124 | printf("\tCondition: [%c%s]\n", (tms320c64x->condition.zero == 1) ? '!' : ' ', cs_reg_name(handle, tms320c64x->condition.reg)); |
| 125 | printf("\tParallel: %s\n", (tms320c64x->parallel == 1) ? "true" : "false"); |
| 126 | |
| 127 | printf("\n"); |
| 128 | } |
| 129 | |
| 130 | static void test() |
| 131 | { |
| 132 | #define TMS320C64X_CODE "\x01\xac\x88\x40\x81\xac\x88\x43\x00\x00\x00\x00\x02\x90\x32\x96\x02\x80\x46\x9e\x05\x3c\x83\xe6\x0b\x0c\x8b\x24" |
| 133 | |
| 134 | struct platform platforms[] = { |
| 135 | { |
| 136 | CS_ARCH_TMS320C64X, |
| 137 | CS_MODE_BIG_ENDIAN, |
| 138 | (unsigned char*)TMS320C64X_CODE, |
| 139 | sizeof(TMS320C64X_CODE) - 1, |
| 140 | "TMS320C64x", |
| 141 | }, |
| 142 | }; |
| 143 | |
| 144 | uint64_t address = 0x1000; |
| 145 | cs_insn *insn; |
| 146 | int i; |
| 147 | size_t count; |
| 148 | |
| 149 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 150 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 151 | if (err) { |
| 152 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 157 | |
| 158 | count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
| 159 | if (count) { |
| 160 | size_t j; |
| 161 | |
| 162 | printf("****************\n"); |
| 163 | printf("Platform: %s\n", platforms[i].comment); |
| 164 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 165 | printf("Disasm:\n"); |
| 166 | |
| 167 | for (j = 0; j < count; j++) { |
| 168 | printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
| 169 | print_insn_detail(&insn[j]); |
| 170 | } |
| 171 | printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size); |
| 172 | |
| 173 | // free memory allocated by cs_disasm() |
| 174 | cs_free(insn, count); |
| 175 | } else { |
| 176 | printf("****************\n"); |
| 177 | printf("Platform: %s\n", platforms[i].comment); |
| 178 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 179 | printf("ERROR: Failed to disasm given code!\n"); |
| 180 | } |
| 181 | |
| 182 | printf("\n"); |
| 183 | |
| 184 | cs_close(&handle); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | int main() |
| 189 | { |
| 190 | test(); |
| 191 | |
| 192 | return 0; |
| 193 | } |