| 1 | /* Capstone Disassembler Engine */ |
| 2 | /* SuperH Backend by Yoshinori Sato <[email protected]> */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include <capstone/platform.h> |
| 8 | #include <capstone/capstone.h> |
| 9 | |
| 10 | #define WITH_DETAILS |
| 11 | |
| 12 | struct platform { |
| 13 | cs_arch arch; |
| 14 | cs_mode mode; |
| 15 | unsigned char *code; |
| 16 | size_t size; |
| 17 | const char *comment; |
| 18 | }; |
| 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 | |
| 26 | for (c = str; c < str + len; c++) |
| 27 | printf("0x%02x ", *c & 0xff); |
| 28 | |
| 29 | printf("\n"); |
| 30 | } |
| 31 | |
| 32 | static void print_string_hex_short(unsigned char *str, size_t len) |
| 33 | { |
| 34 | unsigned char *c; |
| 35 | |
| 36 | for (c = str; c < str + len; c++) |
| 37 | printf("%02x", *c & 0xff); |
| 38 | } |
| 39 | |
| 40 | static void print_read_write_regs(csh handle, cs_detail *detail) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | if (detail->regs_read_count > 0) { |
| 45 | printf("\tRegisters read:"); |
| 46 | |
| 47 | for (i = 0; i < detail->regs_read_count; ++i) |
| 48 | printf(" %s", |
| 49 | cs_reg_name(handle, detail->regs_read[i])); |
| 50 | |
| 51 | printf("\n"); |
| 52 | } |
| 53 | |
| 54 | if (detail->regs_write_count > 0) { |
| 55 | printf("\tRegisters modified:"); |
| 56 | |
| 57 | for (i = 0; i < detail->regs_write_count; ++i) |
| 58 | printf(" %s", |
| 59 | cs_reg_name(handle, detail->regs_write[i])); |
| 60 | |
| 61 | printf("\n"); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static char *reg_address_msg[] = { |
| 66 | "Register indirect", |
| 67 | "Register indirect with predecrement", |
| 68 | "Register indirect with postincrement", |
| 69 | }; |
| 70 | |
| 71 | static void print_insn_detail(csh handle, cs_insn *insn) |
| 72 | { |
| 73 | cs_detail *detail = insn->detail; |
| 74 | cs_sh *sh = NULL; |
| 75 | int i; |
| 76 | |
| 77 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 78 | if (detail == NULL) |
| 79 | return; |
| 80 | |
| 81 | sh = &detail->sh; |
| 82 | |
| 83 | if (sh->op_count) |
| 84 | printf("\top_count: %u\n", sh->op_count); |
| 85 | |
| 86 | for (i = 0; i < sh->op_count; i++) { |
| 87 | cs_sh_op *op = &(sh->operands[i]); |
| 88 | |
| 89 | switch ((int)op->type) { |
| 90 | default: |
| 91 | break; |
| 92 | |
| 93 | case SH_OP_REG: |
| 94 | printf("\t\toperands[%u].type: REGISTER = %s\n", i, |
| 95 | cs_reg_name(handle, op->reg)); |
| 96 | break; |
| 97 | |
| 98 | case SH_OP_IMM: |
| 99 | printf("\t\toperands[%u].type: IMMEDIATE = #%llu\n", i, |
| 100 | op->imm); |
| 101 | break; |
| 102 | |
| 103 | case SH_OP_MEM: |
| 104 | printf("\t\toperands[%u].type: MEM ", i); |
| 105 | switch(op->mem.address) { |
| 106 | case SH_OP_MEM_REG_IND: |
| 107 | case SH_OP_MEM_REG_POST: |
| 108 | case SH_OP_MEM_REG_PRE: |
| 109 | printf("%s REG %s\n", |
| 110 | reg_address_msg[op->mem.address - SH_OP_MEM_REG_IND], |
| 111 | cs_reg_name(handle, op->mem.reg)); |
| 112 | break; |
| 113 | case SH_OP_MEM_REG_DISP: |
| 114 | printf("Register indirect with displacement REG %s, DISP %d\n", |
| 115 | cs_reg_name(handle, op->mem.reg), |
| 116 | op->mem.disp); |
| 117 | break; |
| 118 | |
| 119 | case SH_OP_MEM_REG_R0: |
| 120 | printf("R0 indexed\n"); |
| 121 | break; |
| 122 | |
| 123 | case SH_OP_MEM_GBR_DISP: |
| 124 | printf("GBR base with displacement DISP %d\n", |
| 125 | op->mem.disp); |
| 126 | break; |
| 127 | |
| 128 | case SH_OP_MEM_GBR_R0: |
| 129 | printf("GBR base with R0 indexed\n"); |
| 130 | break; |
| 131 | |
| 132 | case SH_OP_MEM_PCR: |
| 133 | printf("PC relative Address=0x%08x\n", |
| 134 | op->mem.disp); |
| 135 | break; |
| 136 | |
| 137 | case SH_OP_MEM_TBR_DISP: |
| 138 | printf("TBR base with displacement DISP %d\n", |
| 139 | op->mem.disp); |
| 140 | break; |
| 141 | case SH_OP_MEM_INVALID: |
| 142 | break; |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | if (sh->size != 0) |
| 148 | printf("\t\t\tsize: %u\n", sh->size); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | print_read_write_regs(handle, detail); |
| 153 | |
| 154 | if (detail->groups_count) { |
| 155 | printf("\tgroups_count: %u\n", detail->groups_count); |
| 156 | } |
| 157 | |
| 158 | printf("\n"); |
| 159 | } |
| 160 | |
| 161 | static bool consistency_checks() |
| 162 | { |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | static void test() |
| 167 | { |
| 168 | #define SH4A_CODE \ |
| 169 | "\xc\x31\x10\x20\x22\x21\x36\x64\x46\x25\x12\x12\x1c\x2\x8\xc1\x5\xc7\xc" \ |
| 170 | "\x71\x1f\x2\x22\xcf\x6\x89\x23\x0\x2b\x41\xb\x0\xe\x40\x32\x0\xa\xf1\x9\x0" |
| 171 | |
| 172 | #define SH2A_CODE \ |
| 173 | "\x32\x11\x92\x0\x32\x49\x31\x0" |
| 174 | |
| 175 | struct platform platforms[] = { |
| 176 | { |
| 177 | CS_ARCH_SH, |
| 178 | (cs_mode)(CS_MODE_SH4A | CS_MODE_SHFPU), |
| 179 | (unsigned char *)SH4A_CODE, |
| 180 | sizeof(SH4A_CODE) - 1, |
| 181 | "SH_SH4A", |
| 182 | }, |
| 183 | { |
| 184 | CS_ARCH_SH, |
| 185 | (cs_mode)(CS_MODE_SH2A | CS_MODE_SHFPU | CS_MODE_BIG_ENDIAN), |
| 186 | (unsigned char *)SH2A_CODE, |
| 187 | sizeof(SH2A_CODE) - 1, |
| 188 | "SH_SH2A", |
| 189 | }, |
| 190 | }; |
| 191 | |
| 192 | uint64_t address = 0x80000000; |
| 193 | csh handle; |
| 194 | cs_insn *insn; |
| 195 | int i; |
| 196 | size_t count; |
| 197 | const char *nine_spaces = " "; |
| 198 | |
| 199 | if (!consistency_checks()) |
| 200 | abort(); |
| 201 | |
| 202 | for (i = 0; i < sizeof(platforms) / sizeof(platforms[0]); i++) { |
| 203 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, |
| 204 | &handle); |
| 205 | |
| 206 | if (err) { |
| 207 | printf("Failed on cs_open() with error returned: %u\n", |
| 208 | err); |
| 209 | abort(); |
| 210 | } |
| 211 | |
| 212 | #ifdef WITH_DETAILS |
| 213 | cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); |
| 214 | #endif |
| 215 | |
| 216 | count = cs_disasm(handle, platforms[i].code, platforms[i].size, |
| 217 | address, 0, &insn); |
| 218 | |
| 219 | if (count) { |
| 220 | size_t j; |
| 221 | |
| 222 | printf("********************\n"); |
| 223 | printf("Platform: %s\n", platforms[i].comment); |
| 224 | print_string_hex("Code: ", platforms[i].code, |
| 225 | platforms[i].size); |
| 226 | printf("Disasm:\n"); |
| 227 | |
| 228 | for (j = 0; j < count; j++) { |
| 229 | int slen; |
| 230 | printf("0x%08x: ", (uint32_t)insn[j].address); |
| 231 | print_string_hex_short(insn[j].bytes, |
| 232 | insn[j].size); |
| 233 | printf("%.*s", 1 + ((5 - insn[j].size) * 2), |
| 234 | nine_spaces); |
| 235 | printf("%s", insn[j].mnemonic); |
| 236 | slen = (int)strlen(insn[j].mnemonic); |
| 237 | printf("%.*s", 1 + (5 - slen), nine_spaces); |
| 238 | printf("%s\n", insn[j].op_str); |
| 239 | #ifdef WITH_DETAILS |
| 240 | print_insn_detail(handle, &insn[j]); |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | // free memory allocated by cs_disasm() |
| 245 | cs_free(insn, count); |
| 246 | } |
| 247 | else { |
| 248 | printf("********************\n"); |
| 249 | printf("Platform: %s\n", platforms[i].comment); |
| 250 | print_string_hex("Code:", platforms[i].code, |
| 251 | platforms[i].size); |
| 252 | printf("ERROR: Failed to disasm given code!\n"); |
| 253 | abort(); |
| 254 | } |
| 255 | |
| 256 | cs_close(&handle); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | int main() |
| 261 | { |
| 262 | test(); |
| 263 | |
| 264 | return 0; |
| 265 | } |