blob: d7399a9e4661fc6fab0e187a7b020639517213c2 [file] [log] [blame]
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3
4#include <stdio.h>
Yegor Derevenetsced9d242014-09-21 17:27:11 +02005#include "../inttypes.h"
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08006
7#include <capstone.h>
8
9struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 unsigned char *code;
13 size_t size;
14 char *comment;
15};
16
17static csh handle;
18
Mr. eXoDia9be1f932014-08-26 12:46:15 +020019static void print_string_hex(char *comment, unsigned char *str, size_t len)
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080020{
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
31static void print_insn_detail(cs_insn *ins)
32{
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080033 cs_sparc *sparc;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +080034 int i;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080035
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080036 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37 if (ins->detail == NULL)
38 return;
39
40 sparc = &(ins->detail->sparc);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080041 if (sparc->op_count)
42 printf("\top_count: %u\n", sparc->op_count);
43
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080044 for (i = 0; i < sparc->op_count; i++) {
45 cs_sparc_op *op = &(sparc->operands[i]);
46 switch((int)op->type) {
47 default:
48 break;
49 case SPARC_OP_REG:
50 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
51 break;
52 case SPARC_OP_IMM:
53 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
54 break;
55 case SPARC_OP_MEM:
56 printf("\t\toperands[%u].type: MEM\n", i);
57 if (op->mem.base != X86_REG_INVALID)
58 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
59 i, cs_reg_name(handle, op->mem.base));
60 if (op->mem.index != X86_REG_INVALID)
61 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
62 i, cs_reg_name(handle, op->mem.index));
63 if (op->mem.disp != 0)
64 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
65
66 break;
67 }
68 }
69
70 if (sparc->cc != 0)
71 printf("\tCode condition: %u\n", sparc->cc);
72
73 if (sparc->hint != 0)
74 printf("\tHint code: %u\n", sparc->hint);
75
76 printf("\n");
77}
78
79static void test()
80{
81#define SPARC_CODE "\x80\xa0\x40\x02\x85\xc2\x60\x08\x85\xe8\x20\x01\x81\xe8\x00\x00\x90\x10\x20\x01\xd5\xf6\x10\x16\x21\x00\x00\x0a\x86\x00\x40\x02\x01\x00\x00\x00\x12\xbf\xff\xff\x10\xbf\xff\xff\xa0\x02\x00\x09\x0d\xbf\xff\xff\xd4\x20\x60\x00\xd4\x4e\x00\x16\x2a\xc2\x80\x03"
82
Nguyen Anh Quynhea9f4b12014-03-10 20:38:01 +080083#define SPARCV9_CODE "\x81\xa8\x0a\x24\x89\xa0\x10\x20\x89\xa0\x1a\x60\x89\xa0\x00\xe0"
84
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080085 struct platform platforms[] = {
86 {
Axel 0vercl0k Souchetd3eb98b2014-05-09 21:28:07 +010087 CS_ARCH_SPARC,
88 CS_MODE_BIG_ENDIAN,
89 (unsigned char*)SPARC_CODE,
90 sizeof(SPARC_CODE) - 1,
91 "Sparc",
Nguyen Anh Quynhea9f4b12014-03-10 20:38:01 +080092 },
93 {
Axel 0vercl0k Souchetd3eb98b2014-05-09 21:28:07 +010094 CS_ARCH_SPARC,
95 (cs_mode)(CS_MODE_BIG_ENDIAN + CS_MODE_V9),
96 (unsigned char*)SPARCV9_CODE,
97 sizeof(SPARCV9_CODE) - 1,
98 "SparcV9"
Nguyen Anh Quynhea9f4b12014-03-10 20:38:01 +080099 },
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800100 };
101
102 uint64_t address = 0x1000;
103 cs_insn *insn;
104 int i;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800105 size_t count;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800106
107 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
108 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
109 if (err) {
110 printf("Failed on cs_open() with error returned: %u\n", err);
111 continue;
112 }
113
114 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
115
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800116 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800117 if (count) {
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800118 size_t j;
119
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800120 printf("****************\n");
121 printf("Platform: %s\n", platforms[i].comment);
122 print_string_hex("Code:", platforms[i].code, platforms[i].size);
123 printf("Disasm:\n");
124
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800125 for (j = 0; j < count; j++) {
126 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
127 print_insn_detail(&insn[j]);
128 }
129 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
130
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800131 // free memory allocated by cs_disasm()
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800132 cs_free(insn, count);
133 } else {
134 printf("****************\n");
135 printf("Platform: %s\n", platforms[i].comment);
136 print_string_hex("Code:", platforms[i].code, platforms[i].size);
137 printf("ERROR: Failed to disasm given code!\n");
138 }
139
140 printf("\n");
141
142 cs_close(&handle);
143 }
144}
145
146int main()
147{
148 test();
149
150 return 0;
151}