/
mips.cc
311 lines (294 loc) · 10.6 KB
/
mips.cc
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
// Copyright 2011-2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/zynamics/binexport/ida/mips.h"
#include <cinttypes>
#include <string>
// clang-format off
#include "third_party/zynamics/binexport/ida/begin_idasdk.inc" // NOLINT
#include <idp.hpp> // NOLINT
#include <allins.hpp> // NOLINT
#include <bytes.hpp> // NOLINT
#include <ida.hpp> // NOLINT
#include <ua.hpp> // NOLINT
#include "third_party/zynamics/binexport/ida/end_idasdk.inc" // NOLINT
// clang-format on
#include "third_party/absl/log/log.h"
#include "third_party/absl/strings/str_cat.h"
#include "third_party/zynamics/binexport/call_graph.h"
#include "third_party/zynamics/binexport/ida/names.h"
#include "third_party/zynamics/binexport/util/format.h"
namespace security::binexport {
namespace {
std::string GetCoprocessorRegisterName(int index) {
switch (index) {
case 0:
return "Index";
case 1:
return "Random";
case 2:
return "EntryLo0";
case 3:
return "EntryLo1";
case 4:
return "Context";
case 5:
return "PageMask";
case 6:
return "Wired";
case 7:
return "Reserved";
case 8:
return "BadVAddr";
case 9:
return "Count";
case 10:
return "EntryHi";
case 11:
return "Compare";
case 12:
return "Status";
case 13:
return "Cause";
case 14:
return "EPC";
case 15:
return "PRId";
case 16:
return "Config";
case 17:
return "LLAddr";
case 18:
return "WatchLo";
case 19:
return "WatchHi";
case 20:
return "Reserved";
case 21:
return "Reserved";
case 22:
return "Reserved";
case 23:
return "Debug";
case 24:
return "DEPC";
case 25:
return "Reserved";
case 26:
return "ErrCtl";
case 27:
return "Reserved";
case 28:
return "TagLo";
case 29:
return "Reserved";
case 30:
return "ErrorEPC";
case 31:
return "DESAVE";
}
return "unknown coprocessor register";
}
std::string GetFloatingPointRegisterName(const size_t register_id) {
return "$f" + std::to_string(register_id);
}
Operands DecodeOperandsMips(const insn_t& instruction) {
Operands operands;
for (int operand_position = 0;
operand_position < UA_MAXOP &&
instruction.ops[operand_position].type != o_void;
++operand_position) {
Expressions expressions;
// const insn_t & instruction = cmd;
const op_t& operand = instruction.ops[operand_position];
Expression* expression = nullptr;
size_t operand_size = GetOperandByteSize(instruction, operand);
switch (operand.type) {
case o_void: // No operand
break;
case o_reg: // Register
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(
expression, GetRegisterName(operand.reg, operand_size), 0,
Expression::TYPE_REGISTER, 0);
expressions.push_back(expression);
break;
case o_mem: { // Direct memory reference
const Address immediate = operand.addr;
const Name name = GetName(instruction.ea, immediate, operand_position,
false); // @bug: we don't get =unk_ names yet
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(expression, "[", 0,
Expression::TYPE_DEREFERENCE, 0);
expressions.push_back(expression);
expression = Expression::Create(
expression, name.name, immediate,
name.empty() ? Expression::TYPE_IMMEDIATE_INT : name.type, 0);
expressions.push_back(expression);
break;
}
case o_phrase: // reg(reg)
// @todo: test! I have not encountered this case yet!
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(expression, "+", 0,
Expression::TYPE_OPERATOR, 0);
expressions.push_back(expression);
expressions.push_back(Expression::Create(
expression, GetRegisterName(operand.reg, operand_size), 0,
Expression::TYPE_REGISTER, 0));
expressions.push_back(Expression::Create(
expression, GetRegisterName(operand.specflag1, operand_size), 0,
Expression::TYPE_REGISTER, 1));
break;
case o_displ: { // imm(reg)
const Address immediate = operand.addr;
const Name name =
GetName(instruction.ea, operand.addr, operand_position, false);
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(expression, "+", 0,
Expression::TYPE_OPERATOR, 0);
expressions.push_back(expression);
expressions.push_back(Expression::Create(
expression, GetRegisterName(operand.reg, operand_size), 0,
Expression::TYPE_REGISTER, 0));
expressions.push_back(Expression::Create(
expression, name.name, immediate,
name.empty() ? Expression::TYPE_IMMEDIATE_INT : name.type, 1));
break;
}
case o_imm: { // immediate value
const Address immediate = operand.value;
const Name name =
GetName(instruction.ea, immediate, operand_position, false);
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(
expression, name.name, immediate,
name.empty() ? Expression::TYPE_IMMEDIATE_INT : name.type, 0);
expressions.push_back(expression);
break;
}
case o_far: // immediate Far Address (CODE)
case o_near: // Immediate Near Address (CODE)
{
const Address immediate = operand.addr;
const Name name =
GetName(instruction.ea, immediate, operand_position, false);
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
expression = Expression::Create(
expression, name.name, immediate,
name.empty() ? Expression::TYPE_IMMEDIATE_INT : name.type, 0);
expressions.push_back(expression);
break;
}
case o_idpspec0:
// #define o_cmtimm o_idpspec0 // commented out immediate number
LOG(INFO) << absl::StrCat(
"Warning: commented out immediate number operand type not "
"supported at ",
FormatAddress(instruction.ea));
break;
case o_idpspec1:
// #define o_coreg o_idpspec1 // coprocesor register
// #define del specflag1 // rsp: del is here
if (operand_size != 0) {
expression =
Expression::Create(expression, GetSizePrefix(operand_size), 0,
Expression::TYPE_SIZEPREFIX, 0);
expressions.push_back(expression);
}
if (instruction.auxpref & 0x5) {
expression = Expression::Create(
expression, GetFloatingPointRegisterName(operand.reg), 0,
Expression::TYPE_REGISTER, 0);
expressions.push_back(expression);
} else {
expression = Expression::Create(
expression, GetCoprocessorRegisterName(operand.reg), 0,
Expression::TYPE_REGISTER, 0);
expressions.push_back(expression);
}
break;
case o_idpspec3:
// #define o_cc o_idpspec3 // coprocessor condition
LOG(INFO) << absl::StrCat(
"Warning: coprocessor condition not yet supported at ",
FormatAddress(instruction.ea));
break;
case o_idpspec2:
case o_idpspec4:
case o_idpspec5:
default:
LOG(INFO) << absl::StrCat("Warning: unknown operand type ",
operand.type, " at ",
FormatAddress(instruction.ea));
break;
}
operands.push_back(Operand::CreateOperand(expressions));
}
Operands(operands).swap(operands);
return operands;
}
} // namespace
Instruction ParseInstructionIdaMips(const insn_t& instruction,
CallGraph* /* call_graph */,
FlowGraph* /* flow_graph */) {
if (!IsCode(instruction.ea)) {
return Instruction(instruction.ea);
}
std::string mnemonic = GetMnemonic(instruction.ea);
if (mnemonic.empty()) {
return Instruction(instruction.ea);
}
Address next_instruction = 0;
xrefblk_t xref;
for (bool ok = xref.first_from(instruction.ea, XREF_ALL); ok && xref.iscode;
ok = xref.next_from()) {
if (xref.type == fl_F) {
next_instruction = xref.to;
break;
}
}
return Instruction(instruction.ea, next_instruction, instruction.size,
mnemonic, DecodeOperandsMips(instruction));
}
} // namespace security::binexport