| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include <capstone/platform.h> |
| 5 | #include <capstone/capstone.h> |
| 6 | |
| 7 | struct platform { |
| 8 | cs_arch arch; |
| 9 | cs_mode mode; |
| 10 | unsigned char *code; |
| 11 | size_t size; |
| 12 | const char *comment; |
| 13 | }; |
| 14 | |
| 15 | static csh handle; |
| 16 | |
| 17 | static void print_string_hex(const char *comment, unsigned char *str, size_t len) |
| 18 | { |
| 19 | unsigned char *c; |
| 20 | |
| 21 | printf("%s", comment); |
| 22 | for (c = str; c < str + len; c++) { |
| 23 | printf("0x%02x ", *c & 0xff); |
| 24 | } |
| 25 | |
| 26 | printf("\n"); |
| 27 | } |
| 28 | |
| 29 | static void print_insn_detail(cs_insn *ins) |
| 30 | { |
| 31 | int i; |
| 32 | int n; |
| 33 | cs_riscv *riscv; |
| 34 | cs_detail *detail; |
| 35 | |
| 36 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 37 | if (ins->detail == NULL) |
| 38 | return; |
| 39 | |
| 40 | riscv = &(ins->detail->riscv); |
| 41 | detail = ins->detail; |
| 42 | if (riscv->op_count) |
| 43 | printf("\top_count: %u\n", riscv->op_count); |
| 44 | |
| 45 | for (i = 0; i < riscv->op_count; i++) { |
| 46 | cs_riscv_op *op = &(riscv->operands[i]); |
| 47 | switch((int)op->type) { |
| 48 | default: |
| 49 | printf("\terror in opt_type: %u\n", (int)op->type); |
| 50 | break; |
| 51 | case RISCV_OP_REG: |
| 52 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 53 | break; |
| 54 | case RISCV_OP_IMM: |
| 55 | printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm); |
| 56 | break; |
| 57 | case RISCV_OP_MEM: |
| 58 | printf("\t\toperands[%u].type: MEM\n", i); |
| 59 | if (op->mem.base != RISCV_REG_INVALID) |
| 60 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 61 | i, cs_reg_name(handle, op->mem.base)); |
| 62 | if (op->mem.disp != 0) |
| 63 | printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp); |
| 64 | |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | //print the groups this instruction belongs to |
| 71 | if (detail->groups_count > 0) { |
| 72 | printf("\tThis instruction belongs to groups: "); |
| 73 | for (n = 0; n < detail->groups_count; n++) { |
| 74 | printf("%s ", cs_group_name(handle, detail->groups[n])); |
| 75 | } |
| 76 | printf("\n"); |
| 77 | } |
| 78 | |
| 79 | printf("\n"); |
| 80 | } |
| 81 | |
| 82 | static void test() |
| 83 | { |
| 84 | #define RISCV_CODE32 "\x37\x34\x00\x00\x97\x82\x00\x00\xef\x00\x80\x00\xef\xf0\x1f\xff\xe7\x00\x45\x00\xe7\x00\xc0\xff\x63\x05\x41\x00\xe3\x9d\x61\xfe\x63\xca\x93\x00\x63\x53\xb5\x00\x63\x65\xd6\x00\x63\x76\xf7\x00\x03\x88\x18\x00\x03\x99\x49\x00\x03\xaa\x6a\x00\x03\xcb\x2b\x01\x03\xdc\x8c\x01\x23\x86\xad\x03\x23\x9a\xce\x03\x23\x8f\xef\x01\x93\x00\xe0\x00\x13\xa1\x01\x01\x13\xb2\x02\x7d\x13\xc3\x03\xdd\x13\xe4\xc4\x12\x13\xf5\x85\x0c\x13\x96\xe6\x01\x13\xd7\x97\x01\x13\xd8\xf8\x40\x33\x89\x49\x01\xb3\x0a\x7b\x41\x33\xac\xac\x01\xb3\x3d\xde\x01\x33\xd2\x62\x40\xb3\x43\x94\x00\x33\xe5\xc5\x00\xb3\x76\xf7\x00\xb3\x54\x39\x01\xb3\x50\x31\x00\x33\x9f\x0f\x00" |
| 85 | #define RISCV_CODE64 "\x13\x04\xa8\x7a" // aaa80413 |
| 86 | struct platform platforms[] = { |
| 87 | { |
| 88 | CS_ARCH_RISCV, |
| 89 | CS_MODE_RISCV32, |
| 90 | (unsigned char *)RISCV_CODE32, |
| 91 | sizeof(RISCV_CODE32) - 1, |
| 92 | "riscv32" |
| 93 | }, |
| 94 | { |
| 95 | CS_ARCH_RISCV, |
| 96 | CS_MODE_RISCV64, |
| 97 | (unsigned char *)RISCV_CODE64, |
| 98 | sizeof(RISCV_CODE64) - 1, |
| 99 | "riscv64" |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | uint64_t address = 0x1000; |
| 104 | cs_insn *insn; |
| 105 | int i; |
| 106 | size_t count; |
| 107 | |
| 108 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 109 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 110 | if (err) { |
| 111 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | //To turn on or off the Print Details option |
| 116 | //cs_option(handle, CS_OPT_DETAIL, CS_OPT_OFF); |
| 117 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 118 | |
| 119 | count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
| 120 | if (count) { |
| 121 | size_t j; |
| 122 | |
| 123 | printf("****************\n"); |
| 124 | printf("Platform: %s\n", platforms[i].comment); |
| 125 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 126 | printf("Disasm:\n"); |
| 127 | |
| 128 | for (j = 0; j < count; j++) { |
| 129 | printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str); |
| 130 | print_insn_detail(&insn[j]); |
| 131 | } |
| 132 | printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size); |
| 133 | |
| 134 | // free memory allocated by cs_disasm() |
| 135 | cs_free(insn, count); |
| 136 | } else { |
| 137 | printf("****************\n"); |
| 138 | printf("Platform: %s\n", platforms[i].comment); |
| 139 | print_string_hex("Code:", platforms[i].code, platforms[i].size); |
| 140 | printf("ERROR: Failed to disasm given code!\n"); |
| 141 | } |
| 142 | |
| 143 | printf("\n"); |
| 144 | |
| 145 | cs_close(&handle); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int main() |
| 150 | { |
| 151 | test(); |
| 152 | |
| 153 | return 0; |
| 154 | } |