-
Notifications
You must be signed in to change notification settings - Fork 2
/
ana.cpp
332 lines (311 loc) · 11.8 KB
/
ana.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* KIRK IDP analysis - (c) Proxima 2023
*/
#pragma warning (disable : 4146)
#include "kirk.hpp"
typedef unsigned int INSN;
ea_t rambase = 0xE0000000;
int f_opcode(INSN insn) { return (insn >> 24) & 0xFF; }
ea_t f_addr1(INSN insn) { return (((insn >> 12) & 0xFFF) << 2) | rambase; }
ea_t f_addr2(INSN insn) { return ((insn & 0xFFF) << 2) | rambase; }
ea_t f_addr3(INSN insn) { return (insn << 2) | rambase; }
int f_imm1(INSN insn) { return (insn >> 12) & 0xFFF; }
int f_imm2(INSN insn) { return insn & 0xFFF; }
ea_t f_branch(INSN insn) { return ((insn & 0xFFF) << 2); }
/* Analyze the current instruction. */
int kirk_t::kirk_ana(insn_t* _insn)
{
insn_t& ida_insn = *_insn;
KIRK_INSN_TYPE itype;
INSN insn;
unsigned int data;
insn = get_dword(ida_insn.ea);
int opcode = f_opcode(insn);
switch (opcode)
{
case 0x00://nop
//data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_00_NOP; goto decode_insn_format_noops;
break;
case 0x08://store2 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_08_STORE2; goto decode_insn_format_addr_data;
break;
case 0x09://store ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_09_STORE; goto decode_insn_format_addr_data;
break;
case 0x0A://op0A ($addr1), ($addr1)
itype = KIRK_INSN_0A_MOVX; goto decode_insn_format_addr_addr;
break;
case 0x0B://op0B ($addr1), ($addr1)
itype = KIRK_INSN_0B_MOV32; goto decode_insn_format_addr_addr;
break;
case 0x0C://op0C ($addr1), ($addr1)
itype = KIRK_INSN_0C_MOVY; goto decode_insn_format_addr_addr;
break;
case 0x0D://op0D ($addr1), ($addr1)
itype = KIRK_INSN_0D_MOV32A; goto decode_insn_format_addr_addr;
break;
case 0x0E://op0E ($addr1), ($addr1)
itype = KIRK_INSN_0E_MOV32AA; goto decode_insn_format_addr_addr;
break;
case 0x10://cmp ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_10_CMPX; goto decode_insn_format_addr_data;
break;
case 0x11://cmp ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_11_CMPI; goto decode_insn_format_addr_data;
break;
case 0x12://op12 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_12_CMPPLUS; goto decode_insn_format_addr_data;
break;
case 0x13://op13 ($addr1), ($addr1)
itype = KIRK_INSN_13_CMPA; goto decode_insn_format_addr_addr;
break;
case 0x16://op16 ($addr1), ($addr1)
itype = KIRK_INSN_16_SUBA; goto decode_insn_format_addr_addr;
break;
case 0x19://op19 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_19_ADDI; goto decode_insn_format_addr_data;
break;
case 0x1B://op1B ($addr1), ($addr1)
itype = KIRK_INSN_1B_ADDA; goto decode_insn_format_addr_addr;
break;
case 0x21://op21 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_21_ADDC; goto decode_insn_format_addr_data;
break;
case 0x29://op29 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_29_SUBI; goto decode_insn_format_addr_data;
break;
case 0x30://op30 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_30_OP30; goto decode_insn_format_addr_data;
break;
case 0x38://op38 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_38_OP38; goto decode_insn_format_addr_data;
break;
case 0x39://op39 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_39_ANDI; goto decode_insn_format_addr_data;
break;
case 0x3A://and ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_3A_ANDA; goto decode_insn_format_addr_data;
break;
case 0x41://op41 ($addr1), $data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_41_MOV8; goto decode_insn_format_addr_data;
break;
case 0x43://op43 ($addr1), ($addr1)
itype = KIRK_INSN_43_MOV16; goto decode_insn_format_addr_addr;
break;
case 0x44://op44 ($addr1), ($addr1)
itype = KIRK_INSN_44_ORA; goto decode_insn_format_addr_addr;
break;
case 0x49://op49 ($addr1),$data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_49_XORI; goto decode_insn_format_addr_data;
break;
case 0x4B://op4B ($addr1), ($addr1)
itype = KIRK_INSN_4B_XORA; goto decode_insn_format_addr_addr;
break;
case 0x4E://op4E ($addr1), ($addr1)
itype = KIRK_INSN_4E_XORX; goto decode_insn_format_addr_addr;
break;
case 0x53://op53 ($addr1), ($addr1)
itype = KIRK_INSN_53_MOV128; goto decode_insn_format_addr_addr;
break;
case 0x62://jz $branch
itype = KIRK_INSN_62_JZ; goto decode_insn_format_branch;
break;
case 0x68://jnz $branch
itype = KIRK_INSN_68_JNZ; goto decode_insn_format_branch;
break;
case 0x80://op80 ($addr1), $imm
itype = KIRK_INSN_80_INC32; goto decode_insn_format_addr;
break;
case 0x88://op88 ($addr1), $imm
itype = KIRK_INSN_88_DEC32; goto decode_insn_format_addr;
break;
case 0x89://op89 ($addr1), $data
data = get_dword(ida_insn.ea + 4);
itype = KIRK_INSN_89_OP89; goto decode_insn_format_addr_data;
break;
case 0x8B://op8B ($addr1), $imm
itype = KIRK_INSN_8B_OP8B; goto decode_insn_format_addr_addr;
break;
case 0x90://op90 ($addr1), $imm
itype = KIRK_INSN_90_LSH; goto decode_insn_format_addr_imm;
break;
case 0x98://op98 ($addr1), $imm
itype = KIRK_INSN_98_NAND; goto decode_insn_format_addr_imm;
break;
case 0xA0://opA0 ($addr1), $imm
itype = KIRK_INSN_A0_SETMODE; goto decode_insn_format_addr_imm;
break;
case 0xB0://opB0 ($addr1), $imm
itype = KIRK_INSN_B0_BYTESWAP; goto decode_insn_format_addr;
break;
case 0xC0://test ($addr1), $imm
itype = KIRK_INSN_C0_TEST; goto decode_insn_format_addr_imm;
break;
case 0xC3://test ($addr1), $imm
itype = KIRK_INSN_C3_CHECK3; goto decode_insn_format_addr_imm;
break;
case 0xC6://test ($addr1), $imm
itype = KIRK_INSN_C6_CHECK6; goto decode_insn_format_addr_imm;
break;
case 0xC7://test ($addr1), $imm
itype = KIRK_INSN_C7_CHECK7; goto decode_insn_format_addr_imm;
break;
case 0xC8://opC8 ($addr1), $imm
itype = KIRK_INSN_C8_BITSET; goto decode_insn_format_addr_imm;
break;
case 0xC9://test ($addr1), $imm
itype = KIRK_INSN_C9_CHECK9; goto decode_insn_format_addr_imm;
break;
case 0xD0://opD0 ($addr1), $imm
itype = KIRK_INSN_D0_BITCLEAR; goto decode_insn_format_addr_imm;
break;
case 0xDA://opDA $imm, $imm
itype = KIRK_INSN_DA_CLRZ; goto decode_insn_format_noops;
break;
case 0xDB://opDB $imm, $imm
itype = KIRK_INSN_DB_SETZ; goto decode_insn_format_noops;
break;
case 0xE0://bra $branch
itype = KIRK_INSN_E0_B; goto decode_insn_format_branch;
break;
case 0xE1://bne $branch
itype = KIRK_INSN_E1_BNZ; goto decode_insn_format_branch;
break;
case 0xE2://beq $branch
itype = KIRK_INSN_E2_BZ; goto decode_insn_format_branch;
break;
case 0xE3://bge $branch
itype = KIRK_INSN_E3_BGT; goto decode_insn_format_branch;
break;
case 0xE4://ble $branch
itype = KIRK_INSN_E4_BLT; goto decode_insn_format_branch;
break;
case 0xE8://call $branch
itype = KIRK_INSN_E8_CALL; goto decode_insn_format_branch;
break;
case 0xE9://call2 $branch
itype = KIRK_INSN_E9_CALL2; goto decode_insn_format_branch;
break;
case 0xF0://ret
itype = KIRK_INSN_F0_RET; goto decode_insn_format_noops;
break;
case 0xF8://opF8 $imm, $imm
itype = KIRK_INSN_F8_INTR; goto decode_insn_format_imm_imm;
break;
default:
itype = KIRK_INSN_X_INVALID; goto decode_insn_format_empty;
break;
}
/* The instruction has been decoded, now extract the fields. */
decode_insn_format_empty:
{
ida_insn.itype = itype;
ida_insn.size = 0;
return 0;
}
decode_insn_format_noops:
{
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
decode_insn_format_addr_data:
{
ea_t addr1 = f_addr1(insn);
ida_insn.Op1.type = o_mem;
ida_insn.Op1.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op1.addr = addr1;
ida_insn.Op2.type = o_imm;
ida_insn.Op2.kirk_type = KIRK_OPERAND_UIMM;
ida_insn.Op2.value = data;
refinfo_t ri;
if (get_refinfo(&ri, ida_insn.ea, 1))
{
ea_t target, base;
ida_insn.Op2.type = o_mem;
calc_reference_data(&target, &base, ida_insn.ea, ri, (data << 2) | rambase);
ida_insn.Op2.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op2.addr = target;
}
ida_insn.itype = itype;
ida_insn.size = 8;
return 8;
}
decode_insn_format_addr:
{
ea_t addr1 = f_addr1(insn);
ea_t addr2 = f_addr2(insn);
ida_insn.Op1.type = o_mem;
ida_insn.Op1.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op1.addr = addr1;
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
decode_insn_format_addr_addr:
{
ea_t addr1 = f_addr1(insn);
ea_t addr2 = f_addr2(insn);
ida_insn.Op2.type = o_mem;
ida_insn.Op2.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op2.addr = addr1;
ida_insn.Op1.type = o_mem;
ida_insn.Op1.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op1.addr = addr2;
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
decode_insn_format_addr_imm:
{
ea_t addr1 = f_addr1(insn);
int imm2 = f_imm2(insn);
ida_insn.Op1.type = o_mem;
ida_insn.Op1.kirk_type = KIRK_OPERAND_MEM;
ida_insn.Op1.addr = addr1;
ida_insn.Op2.type = o_imm;
ida_insn.Op2.kirk_type = KIRK_OPERAND_UIMM;
ida_insn.Op2.value = imm2;
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
decode_insn_format_imm_imm:
{
int imm1 = f_imm1(insn);
int imm2 = f_imm2(insn);
ida_insn.Op1.type = o_imm;
ida_insn.Op1.kirk_type = KIRK_OPERAND_UIMM;
ida_insn.Op1.value = imm1;
ida_insn.Op2.type = o_imm;
ida_insn.Op2.kirk_type = KIRK_OPERAND_UIMM;
ida_insn.Op2.value = imm2;
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
decode_insn_format_branch:
{
ea_t branch = f_branch(insn);
ida_insn.Op1.type = o_near;
ida_insn.Op1.kirk_type = KIRK_OPERAND_BRANCH;
ida_insn.Op1.addr = branch;
ida_insn.itype = itype;
ida_insn.size = 4;
return 4;
}
}