This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
disassemble.go
368 lines (327 loc) · 9.13 KB
/
disassemble.go
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package arm64
import "fmt"
func (i *Instruction) disassemble(decimalImm bool) (string, error) {
if i.operation == ARM64_UNDEFINED || i.operation == AMD64_END_TYPE {
return "", fmt.Errorf("failed to disassemble operation")
}
for idx, operand := range i.operands {
switch operand.OpClass {
case FIMM32:
fallthrough
case IMM32:
fallthrough
case IMM64:
fallthrough
case LABEL:
if err := operand.getShiftedImmediate(decimalImm); err != nil {
return "", fmt.Errorf("failed to disassemble operation: %v", err)
}
i.operands[idx].strRepr = operand.strRepr
break
case REG:
if err := operand.getRegister(0, decimalImm); err != nil {
return "", fmt.Errorf("failed to disassemble operation: %v", err)
}
i.operands[idx].strRepr = operand.strRepr
break
case SYS_REG:
i.operands[idx].strRepr = SystemReg(operand.Reg[0]).String()
break
case MULTI_REG:
if err := operand.getMultiregOperand(decimalImm); err != nil {
return "", fmt.Errorf("failed to disassemble operation: %v", err)
}
i.operands[idx].strRepr = operand.strRepr
break
case IMPLEMENTATION_SPECIFIC:
if err := operand.getImplementationSpecific(); err != nil {
return "", fmt.Errorf("failed to disassemble operation: %v", err)
}
i.operands[idx].strRepr = operand.strRepr
break
case MEM_REG:
fallthrough
case MEM_OFFSET:
fallthrough
case MEM_EXTENDED:
fallthrough
case MEM_PRE_IDX:
fallthrough
case MEM_POST_IDX:
if err := operand.getMemoryOperand(decimalImm); err != nil {
return "", fmt.Errorf("failed to disassemble operation: %v", err)
}
i.operands[idx].strRepr = operand.strRepr
break
case CONDITION:
i.operands[idx].strRepr = Condition(operand.Reg[0]).String()
break
case NONE:
break
}
if operand.OpClass != NONE {
if idx == 0 {
// i.operands[idx].strRepr = fmt.Sprintf(" %s", i.operands[idx].strRepr)
i.operands[idx].strRepr = fmt.Sprintf("\t%s", i.operands[idx].strRepr)
} else {
i.operands[idx].strRepr = fmt.Sprintf(", %s", i.operands[idx].strRepr)
}
}
}
return fmt.Sprintf("%s%s%s%s%s%s",
i.operation,
i.operands[0],
i.operands[1],
i.operands[2],
i.operands[3],
i.operands[4]), nil
}
func (op *InstructionOperand) getShiftedImmediate(decimalImm bool) error {
var shiftBuff string
var immBuff string
var sign string
if op == nil {
return failedToDisassembleOperand
}
if op.SignedImm == 1 && int64(op.Immediate) < 0 {
sign = "-"
}
if op.ShiftType != SHIFT_NONE {
if op.ShiftValueUsed != 0 {
if decimalImm {
immBuff = fmt.Sprintf(" #%d", op.ShiftValue)
} else {
immBuff = fmt.Sprintf(" #%#x", op.ShiftValue)
}
}
shiftBuff = fmt.Sprintf(", %s%s", op.ShiftType, immBuff)
}
if op.OpClass == FIMM32 {
shiftBuff = fmt.Sprintf("#%f%s", float64(op.Immediate), shiftBuff)
} else if op.OpClass == IMM32 {
if decimalImm {
shiftBuff = fmt.Sprintf("#%s%d%s", sign, uint32(op.Immediate), shiftBuff)
} else {
shiftBuff = fmt.Sprintf("#%s%#x%s", sign, uint32(op.Immediate), shiftBuff)
}
} else {
if op.SignedImm == 1 && int64(op.Immediate) < 0 {
if decimalImm {
shiftBuff = fmt.Sprintf("#%s%d%s", sign, int64(op.Immediate), shiftBuff)
} else {
shiftBuff = fmt.Sprintf("#%s%#016x%s", sign, int64(op.Immediate), shiftBuff)
}
}
if decimalImm {
shiftBuff = fmt.Sprintf("#%s%d%s", sign, op.Immediate, shiftBuff)
} else {
shiftBuff = fmt.Sprintf("#%s%#x%s", sign, op.Immediate, shiftBuff)
}
}
op.strRepr = shiftBuff
return nil
}
// func (op *InstructionOperand) getShiftedImmediate() error {
// var shiftBuff string
// var immBuff string
// var sign string
// if op == nil {
// return failedToDisassembleOperand
// }
// imm := int64(op.Immediate)
// if op.SignedImm == 1 && imm < 0 {
// sign = "-"
// imm = -imm
// }
// if op.ShiftType != SHIFT_NONE {
// if op.ShiftValueUsed != 0 {
// immBuff = fmt.Sprintf(" #%#x", op.ShiftValue)
// }
// shiftBuff = fmt.Sprintf(", %s%s", ShiftType(op.ShiftType), immBuff)
// }
// if op.OpClass == FIMM32 {
// op.strRepr = fmt.Sprintf("#%f%s", float64(op.Immediate), shiftBuff)
// } else if op.OpClass == IMM32 {
// op.strRepr = fmt.Sprintf("#%s%#x%s", sign, uint32(imm), shiftBuff)
// } else {
// op.strRepr = fmt.Sprintf("#%s%#x%s", sign, imm, shiftBuff)
// }
// return nil
// }
func (op *InstructionOperand) getRegister(registerNumber int, decimalImm bool) error {
var scale string
if op.Scale != 0 {
scale = fmt.Sprintf("[%d]", 0x7fffffff&op.Scale)
}
if op.OpClass == SYS_REG {
op.strRepr = fmt.Sprintf("%s", SystemReg(op.Reg[registerNumber]))
return nil
} else if op.OpClass != REG && op.OpClass != MULTI_REG {
return operandIsNotRegister
}
if op.ShiftType != SHIFT_NONE {
return op.getShiftedRegister(registerNumber, decimalImm)
} else if op.ElementSize == 0 {
op.strRepr = fmt.Sprintf("%s", Register(op.Reg[registerNumber]))
return nil
}
var elementSize string
switch op.ElementSize {
case 1:
elementSize = "b"
break
case 2:
elementSize = "h"
break
case 4:
elementSize = "s"
break
case 8:
elementSize = "d"
break
case 16:
elementSize = "q"
break
default:
return failedToDisassembleRegister
}
if op.DataSize != 0 {
if registerNumber > 3 || (op.DataSize != 1 && op.DataSize != 2 && op.DataSize != 4 && op.DataSize != 8 && op.DataSize != 16) {
return failedToDisassembleRegister
}
op.strRepr = fmt.Sprintf("%s.%d%s%s", Register(op.Reg[registerNumber]), op.DataSize, elementSize, scale)
} else {
if registerNumber > 3 {
return failedToDisassembleRegister
}
op.strRepr = fmt.Sprintf("%s.%s%s", Register(op.Reg[registerNumber]), elementSize, scale)
}
return nil
}
func (op *InstructionOperand) getShiftedRegister(registerNumber int, decimalImm bool) error {
var immBuff string
var shiftBuff string
reg := Register(op.Reg[registerNumber])
if reg == REG_NONE {
return failedToDisassembleRegister
}
if op.ShiftType != SHIFT_NONE {
if op.ShiftValueUsed != 0 {
if decimalImm {
immBuff = fmt.Sprintf(" #%d", op.ShiftValue)
} else {
immBuff = fmt.Sprintf(" #%#x", op.ShiftValue)
}
}
shiftBuff = fmt.Sprintf(", %s%s", ShiftType(op.ShiftType), immBuff)
}
op.strRepr = fmt.Sprintf("%s%s", reg, shiftBuff)
return nil
}
func (op *InstructionOperand) getMultiregOperand(decimalImm bool) error {
var indexBuff string
var registers []Register
var elementCount int
for _, opReg := range op.Reg {
if Register(opReg) != REG_NONE {
if err := op.getRegister(elementCount, decimalImm); err != nil {
return err
}
registers = append(registers, Register(opReg))
elementCount++
}
}
if op.Index != 0 {
indexBuff = fmt.Sprintf("[%d]", op.Index)
}
switch elementCount {
case 1:
op.strRepr = fmt.Sprintf("{%s}%s", registers[0], indexBuff)
break
case 2:
op.strRepr = fmt.Sprintf("{%s, %s}%s", registers[0], registers[1], indexBuff)
break
case 3:
op.strRepr = fmt.Sprintf("{%s, %s, %s}%s", registers[0], registers[1], registers[2], indexBuff)
break
case 4:
op.strRepr = fmt.Sprintf("{%s, %s, %s, %s}%s", registers[0], registers[1], registers[2], registers[3], indexBuff)
break
default:
return failedToDisassembleOperand
}
return nil
}
func (op *InstructionOperand) getImplementationSpecific() error {
op.strRepr = fmt.Sprintf("s%d_%d_c%d_c%d_%d", op.Reg[0], op.Reg[1], op.Reg[2], op.Reg[3], op.Reg[4])
return nil
}
func (op *InstructionOperand) getMemoryOperand(decimalImm bool) error {
var immBuff string
var extendBuff string
var paramBuff string
var outBuffer string
var sign string
reg1 := Register(op.Reg[0])
reg2 := Register(op.Reg[1])
if op == nil {
return failedToDisassembleOperand
}
imm := int64(op.Immediate)
if op.SignedImm == 1 && imm < 0 {
sign = "-"
imm = -imm
}
switch op.OpClass {
case MEM_REG:
outBuffer = fmt.Sprintf("[%s]", Register(op.Reg[0]))
break
case MEM_PRE_IDX:
if decimalImm {
outBuffer = fmt.Sprintf("[%s, #%s%d]!", Register(op.Reg[0]), sign, uint64(imm))
} else {
outBuffer = fmt.Sprintf("[%s, #%s%#x]!", Register(op.Reg[0]), sign, uint64(imm))
}
break
case MEM_POST_IDX: // [<reg>], <reg|imm>
if Register(op.Reg[1]) != REG_NONE {
paramBuff = fmt.Sprintf(", %s", Register(op.Reg[1]))
} else {
if decimalImm {
paramBuff = fmt.Sprintf(", #%s%d", sign, uint64(imm))
} else {
paramBuff = fmt.Sprintf(", #%s%#x", sign, uint64(imm))
}
}
outBuffer = fmt.Sprintf("[%s]%s", Register(op.Reg[0]), paramBuff)
break
case MEM_OFFSET: // [<reg> optional(imm)]
if imm != 0 {
if decimalImm {
immBuff = fmt.Sprintf(", #%s%d", sign, uint64(imm))
} else {
immBuff = fmt.Sprintf(", #%s%#x", sign, uint64(imm))
}
}
outBuffer = fmt.Sprintf("[%s%s]", Register(op.Reg[0]), immBuff)
break
case MEM_EXTENDED:
if reg1 == REG_NONE || reg2 == REG_NONE {
return failedToDisassembleOperand
}
if decimalImm {
immBuff = fmt.Sprintf(", #%d", op.ShiftValue)
} else {
immBuff = fmt.Sprintf(", #%#x", op.ShiftValue)
}
if op.ShiftType != SHIFT_NONE {
extendBuff = fmt.Sprintf(", %s%s", ShiftType(op.ShiftType), immBuff)
}
outBuffer = fmt.Sprintf("[%s, %s%s]", reg1, reg2, extendBuff)
break
default:
return notMemoryOperand
}
op.strRepr = outBuffer
return nil
}