1 // Capstone Java binding
2 // By Nguyen Anh Quynh & Dang Hoang Vu,  2013
3 
4 import capstone.Capstone;
5 import capstone.Arm;
6 
7 import static capstone.Arm_const.*;
8 
9 public class TestArm {
10 
hexString2Byte(String s)11   static byte[] hexString2Byte(String s) {
12     // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
13     int len = s.length();
14     byte[] data = new byte[len / 2];
15     for (int i = 0; i < len; i += 2) {
16       data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
17           + Character.digit(s.charAt(i+1), 16));
18     }
19     return data;
20   }
21 
22   static final String ARM_CODE = "EDFFFFEB04e02de500000000e08322e5f102030e0000a0e30230c1e7000053e3000201f10540d0e8";
23   static final String ARM_CODE2 = "d1e800f0f02404071f3cf2c000004ff00001466c";
24   static final String THUMB_CODE2 = "4ff00001bde80088d1e800f018bfadbff3ff0b0c86f3008980f3008c4ffa99f6d0ffa201";
25   static final String THUMB_CODE  = "7047eb4683b0c9681fb130bfaff32084";
26 
27   public static Capstone cs;
28 
hex(int i)29   private static String hex(int i) {
30     return Integer.toString(i, 16);
31   }
32 
hex(long i)33   private static String hex(long i) {
34     return Long.toString(i, 16);
35   }
36 
print_ins_detail(Capstone.CsInsn ins)37   public static void print_ins_detail(Capstone.CsInsn ins) {
38     System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
39 
40     Arm.OpInfo operands = (Arm.OpInfo) ins.operands;
41 
42     if (operands.op.length != 0) {
43       System.out.printf("\top_count: %d\n", operands.op.length);
44       for (int c=0; c<operands.op.length; c++) {
45         Arm.Operand i = (Arm.Operand) operands.op[c];
46         String imm = hex(i.value.imm);
47         if (i.type == ARM_OP_SYSREG)
48           System.out.printf("\t\toperands[%d].type: SYSREG = %d\n", c, i.value.reg);
49         if (i.type == ARM_OP_REG)
50           System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
51         if (i.type == ARM_OP_IMM)
52           System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
53         if (i.type == ARM_OP_PIMM)
54           System.out.printf("\t\toperands[%d].type: P-IMM = %d\n", c, i.value.imm);
55         if (i.type == ARM_OP_CIMM)
56           System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
57         if (i.type == ARM_OP_SETEND)
58   				System.out.printf("\t\toperands[%d].type: SETEND = %s\n", c, i.value.setend == ARM_SETEND_BE? "be" : "le");
59         if (i.type == ARM_OP_FP)
60           System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
61         if (i.type == ARM_OP_MEM) {
62           System.out.printf("\t\toperands[%d].type: MEM\n",c);
63           String base = ins.regName(i.value.mem.base);
64           String index = ins.regName(i.value.mem.index);
65           if (base != null)
66             System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
67           if (index != null)
68             System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
69           if (i.value.mem.scale != 1)
70             System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, (i.value.mem.scale));
71           if (i.value.mem.disp != 0)
72             System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, (i.value.mem.disp));
73           if (i.value.mem.lshift != 0)
74             System.out.printf("\t\t\toperands[%d].mem.lshift: 0x%x\n", c, (i.value.mem.lshift));
75         }
76         if (i.vector_index > 0)
77           System.out.printf("\t\t\toperands[%d].vector_index = %d\n", c, (i.vector_index));
78         if (i.shift.type != ARM_SFT_INVALID && i.shift.value > 0)
79           System.out.printf("\t\t\tShift: %d = %d\n", i.shift.type, i.shift.value);
80         if (i.subtracted)
81           System.out.printf("\t\t\toperands[%d].subtracted = True\n", c);
82       }
83     }
84     if (operands.writeback)
85       System.out.println("\tWrite-back: True");
86 
87     if (operands.updateFlags)
88       System.out.println("\tUpdate-flags: True");
89 
90     if (operands.cc != ARM_CC_AL && operands.cc != ARM_CC_INVALID)
91       System.out.printf("\tCode condition: %d\n",  operands.cc);
92 
93     if (operands.cpsMode > 0)
94       System.out.printf("\tCPSI-mode: %d\n", operands.cpsMode);
95 
96     if (operands.cpsFlag > 0)
97       System.out.printf("\tCPSI-flag: %d\n", operands.cpsFlag);
98 
99     if (operands.vectorData > 0)
100       System.out.printf("\tVector-data: %d\n", operands.vectorData);
101 
102     if (operands.vectorSize > 0)
103       System.out.printf("\tVector-size: %d\n", operands.vectorSize);
104 
105     if (operands.usermode)
106       System.out.printf("\tUser-mode: True\n");
107   }
108 
main(String argv[])109   public static void main(String argv[]) {
110 
111     final TestBasic.platform[] all_tests = {
112       new TestBasic.platform(Capstone.CS_ARCH_ARM, Capstone.CS_MODE_ARM, hexString2Byte(ARM_CODE), "ARM"),
113       new TestBasic.platform(Capstone.CS_ARCH_ARM, Capstone.CS_MODE_THUMB, hexString2Byte(THUMB_CODE), "Thumb"),
114       new TestBasic.platform(Capstone.CS_ARCH_ARM, Capstone.CS_MODE_THUMB, hexString2Byte(ARM_CODE2), "Thumb-mixed"),
115       new TestBasic.platform(Capstone.CS_ARCH_ARM, Capstone.CS_MODE_THUMB, Capstone.CS_OPT_SYNTAX_NOREGNAME, hexString2Byte(THUMB_CODE2), "Thumb-2 & register named with numbers"),
116     };
117 
118     for (int i=0; i<all_tests.length; i++) {
119       TestBasic.platform test = all_tests[i];
120       System.out.println(new String(new char[16]).replace("\0", "*"));
121       System.out.println("Platform: " + test.comment);
122       System.out.println("Code: " + TestBasic.stringToHex(test.code));
123       System.out.println("Disasm:");
124 
125       cs = new Capstone(test.arch, test.mode);
126       cs.setDetail(Capstone.CS_OPT_ON);
127       if (test.syntax != 0)
128         cs.setSyntax(test.syntax);
129       Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
130 
131       for (int j = 0; j < all_ins.length; j++) {
132         print_ins_detail(all_ins[j]);
133         System.out.println();
134       }
135       System.out.printf("0x%x:\n\n", (all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size));
136 
137       // Close when done
138 	  cs.close();
139     }
140   }
141 
142 }
143