blob: 9fc6fbf5f7be7eccc7d52456e403383dbd39d5ef [file] [log] [blame]
Ian Rogers706a10e2012-03-23 17:00:55 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_x86.h"
18
Ian Rogers706a10e2012-03-23 17:00:55 -070019#include <iostream>
20
Elliott Hughes0f3c5532012-03-30 14:51:51 -070021#include "logging.h"
22#include "stringprintf.h"
23
Ian Rogers706a10e2012-03-23 17:00:55 -070024namespace art {
25namespace x86 {
26
27DisassemblerX86::DisassemblerX86() {
28}
29
30void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
31 size_t length = 0;
32 for (const uint8_t* cur = begin; cur < end; cur += length) {
33 length = DumpInstruction(os, cur);
34 }
35}
36
37static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
38static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "di", "si" };
39static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "edi", "esi" };
40
41static void DumpReg0(std::ostream& os, uint8_t /*rex*/, size_t reg,
42 bool byte_operand, uint8_t size_override) {
43 DCHECK_LT(reg, 8u);
44 // TODO: combine rex into size
45 size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : 4);
46 switch (size) {
47 case 1: os << gReg8Names[reg]; break;
48 case 2: os << gReg16Names[reg]; break;
49 case 4: os << gReg32Names[reg]; break;
50 default: LOG(FATAL) << "unexpected size " << size;
51 }
52}
53
54static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
55 bool byte_operand, uint8_t size_override) {
56 size_t reg_num = reg; // TODO: combine with REX.R on 64bit
57 DumpReg0(os, rex, reg_num, byte_operand, size_override);
58}
59
Ian Rogers7caad772012-03-30 01:07:54 -070060static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070061 size_t reg_num = reg; // TODO: combine with REX.B on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070062 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070063}
64
Ian Rogers7caad772012-03-30 01:07:54 -070065static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070066 int reg_num = reg; // TODO: combine with REX.X on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070067 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070068}
69
70static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
71 switch (segment_prefix) {
72 case 0x2E: os << "cs:"; break;
73 case 0x36: os << "ss:"; break;
74 case 0x3E: os << "ds:"; break;
75 case 0x26: os << "es:"; break;
76 case 0x64: os << "fs:"; break;
77 case 0x65: os << "gs:"; break;
78 default: break;
79 }
80}
81
82size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
83 const uint8_t* begin_instr = instr;
84 bool have_prefixes = true;
85 uint8_t prefix[4] = {0, 0, 0, 0};
86 const char** modrm_opcodes = NULL;
87 do {
88 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -070089 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -070090 case 0xF0:
91 case 0xF2:
92 case 0xF3:
93 prefix[0] = *instr;
94 break;
95 // Group 2 - segment override prefixes:
96 case 0x2E:
97 case 0x36:
98 case 0x3E:
99 case 0x26:
100 case 0x64:
101 case 0x65:
102 prefix[1] = *instr;
103 break;
104 // Group 3 - operand size override:
105 case 0x66:
106 prefix[2] = *instr;
107 break;
108 // Group 4 - address size override:
109 case 0x67:
110 prefix[3] = *instr;
111 break;
112 default:
113 have_prefixes = false;
114 break;
115 }
116 if (have_prefixes) {
117 instr++;
118 }
119 } while (have_prefixes);
120 uint8_t rex = (*instr >= 0x40 && *instr <= 0x4F) ? *instr : 0;
121 bool has_modrm = false;
122 bool reg_is_opcode = false;
123 size_t immediate_bytes = 0;
124 size_t branch_bytes = 0;
125 std::ostringstream opcode;
126 bool store = false; // stores to memory (ie rm is on the left)
127 bool load = false; // loads from memory (ie rm is on the right)
128 bool byte_operand = false;
129 bool ax = false; // implicit use of ax
130 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
131 switch (*instr) {
132#define DISASSEMBLER_ENTRY(opname, \
133 rm8_r8, rm32_r32, \
134 r8_rm8, r32_rm32, \
135 ax8_i8, ax32_i32) \
136 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
137 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
138 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
139 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
140 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
141 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
142
143DISASSEMBLER_ENTRY(add,
144 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
145 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
146 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
147DISASSEMBLER_ENTRY(or,
148 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
149 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
150 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
151DISASSEMBLER_ENTRY(adc,
152 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
153 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
154 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
155DISASSEMBLER_ENTRY(sbb,
156 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
157 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
158 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
159DISASSEMBLER_ENTRY(and,
160 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
161 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
162 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
163DISASSEMBLER_ENTRY(sub,
164 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
165 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
166 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
167DISASSEMBLER_ENTRY(xor,
168 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
169 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
170 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
171DISASSEMBLER_ENTRY(cmp,
172 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
173 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
174 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
175
176#undef DISASSEMBLER_ENTRY
177 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
178 opcode << "push";
179 reg_in_opcode = true;
180 break;
181 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
182 opcode << "pop";
183 reg_in_opcode = true;
184 break;
185 case 0x68: opcode << "push"; immediate_bytes = 4; break;
186 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
187 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
188 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
189 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700190 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
191 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700192 };
193 opcode << "j" << condition_codes[*instr & 0xF];
194 branch_bytes = 1;
195 break;
196 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
197 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
198 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
199 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
200
201 case 0x0F: // 2 byte extended opcode
202 instr++;
203 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700204 case 0x10: case 0x11:
205 if (prefix[0] == 0xF2) {
206 opcode << "movsd";
207 } else if (prefix[0] == 0xF3) {
208 opcode << "movss";
209 } else if (prefix[2] == 0x66) {
210 opcode << "movupd";
211 } else {
212 opcode << "movups";
213 }
214 has_modrm = true;
215 load = *instr == 0x10;
216 store = !load;
217 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700218 case 0x38: // 3 byte extended opcode
219 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
220 break;
221 case 0x3A: // 3 byte extended opcode
222 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
223 break;
224 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
225 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
226 opcode << "j" << condition_codes[*instr & 0xF];
227 branch_bytes = 4;
228 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700229 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
230 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
231 opcode << "set" << condition_codes[*instr & 0xF];
232 modrm_opcodes = NULL;
233 reg_is_opcode = true;
234 has_modrm = true;
235 store = true;
236 break;
237 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
238 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700239 default:
240 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
241 break;
242 }
243 break;
244 case 0x80: case 0x81: case 0x82: case 0x83:
245 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
246 modrm_opcodes = x80_opcodes;
247 has_modrm = true;
248 reg_is_opcode = true;
249 store = true;
250 byte_operand = (*instr & 1) == 0;
251 immediate_bytes = *instr == 0x81 ? 4 : 1;
252 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700253 case 0x8D:
254 opcode << "lea";
255 has_modrm = true;
256 load = true;
257 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700258 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
259 opcode << "mov";
260 immediate_bytes = 1;
261 reg_in_opcode = true;
262 break;
263 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
264 opcode << "mov";
265 immediate_bytes = 4;
266 reg_in_opcode = true;
267 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700268 case 0xC0: case 0xC1:
269 static const char* shift_opcodes[] =
270 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
271 modrm_opcodes = shift_opcodes;
272 has_modrm = true;
273 reg_is_opcode = true;
274 store = true;
275 immediate_bytes = 1;
276 byte_operand = *instr == 0xC0;
277 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700278 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700279 case 0xC7:
280 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
281 modrm_opcodes = c7_opcodes;
282 store = true;
283 immediate_bytes = 4;
284 has_modrm = true;
285 reg_is_opcode = true;
286 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700287 case 0xCC: opcode << "int 3"; break;
288 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700289 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
290 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
291 case 0xFF:
292 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
293 modrm_opcodes = ff_opcodes;
294 has_modrm = true;
295 reg_is_opcode = true;
296 load = true;
297 break;
298 default:
299 opcode << StringPrintf("unknown opcode '%02X'", *instr);
300 break;
301 }
302 std::ostringstream args;
303 if (reg_in_opcode) {
304 DCHECK(!has_modrm);
305 DumpReg(args, rex, *instr & 0x7, false, prefix[2]);
306 }
307 instr++;
308 if (has_modrm) {
309 uint8_t modrm = *instr;
310 instr++;
311 uint8_t mod = modrm >> 6;
312 uint8_t reg_or_opcode = (modrm >> 3) & 7;
313 uint8_t rm = modrm & 7;
314 std::ostringstream address;
315 if (mod == 0 && rm == 5) { // fixed address
316 address << StringPrintf("[0x%X]", *reinterpret_cast<const uint32_t*>(instr));
317 instr += 4;
318 } else if (rm == 4 && mod != 3) { // SIB
319 uint8_t sib = *instr;
320 instr++;
321 uint8_t ss = (sib >> 6) & 3;
322 uint8_t index = (sib >> 3) & 7;
323 uint8_t base = sib & 7;
324 address << "[";
325 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700326 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700327 if (index != 4) {
328 address << " + ";
329 }
330 }
331 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700332 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700333 if (ss != 0) {
334 address << StringPrintf(" * %d", 1 << ss);
335 }
336 }
337 if (mod == 1) {
338 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
339 instr++;
340 } else if (mod == 2) {
341 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
342 instr += 4;
343 }
344 address << "]";
345 } else {
346 if (mod != 3) {
347 address << "[";
348 }
Ian Rogers7caad772012-03-30 01:07:54 -0700349 DumpBaseReg(address, rex, rm);
Ian Rogers706a10e2012-03-23 17:00:55 -0700350 if (mod == 1) {
351 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
352 instr++;
353 } else if (mod == 2) {
354 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
355 instr += 4;
356 }
357 if (mod != 3) {
358 address << "]";
359 }
360 }
361
Ian Rogers7caad772012-03-30 01:07:54 -0700362 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700363 opcode << modrm_opcodes[reg_or_opcode];
364 }
365 if (load) {
366 if (!reg_is_opcode) {
367 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2]);
368 args << ", ";
369 }
370 DumpSegmentOverride(args, prefix[1]);
371 args << address.str();
372 } else {
373 DCHECK(store);
374 DumpSegmentOverride(args, prefix[1]);
375 args << address.str();
376 if (!reg_is_opcode) {
377 args << ", ";
378 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2]);
379 }
380 }
381 }
382 if (ax) {
383 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2]);
384 }
385 if (immediate_bytes > 0) {
386 if (has_modrm || reg_in_opcode || ax) {
387 args << ", ";
388 }
389 if (immediate_bytes == 1) {
390 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
391 instr++;
392 } else {
393 CHECK_EQ(immediate_bytes, 4u);
394 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
395 instr += 4;
396 }
397 } else if (branch_bytes > 0) {
398 DCHECK(!has_modrm);
399 int32_t displacement;
400 if (branch_bytes == 1) {
401 displacement = *reinterpret_cast<const int8_t*>(instr);
402 instr++;
403 } else {
404 CHECK_EQ(branch_bytes, 4u);
405 displacement = *reinterpret_cast<const int32_t*>(instr);
406 instr += 4;
407 }
408 args << StringPrintf("%d (%p)", displacement, instr + displacement);
409 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700410 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700411 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700412 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700413 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700414 os << StringPrintf("\t\t\t%p: %22s \t%-7s ", begin_instr, hex.str().c_str(), opcode.str().c_str()) << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -0700415 return instr - begin_instr;
416}
417
418} // namespace x86
419} // namespace art