blob: 496da2ab16de34b33d3f3c7c1b8ed39ad9e6ff6c [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"
Elliott Hughes92301d92012-04-10 15:57:52 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers706a10e2012-03-23 17:00:55 -070025namespace art {
26namespace x86 {
27
28DisassemblerX86::DisassemblerX86() {
29}
30
31void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
32 size_t length = 0;
33 for (const uint8_t* cur = begin; cur < end; cur += length) {
34 length = DumpInstruction(os, cur);
35 }
36}
37
38static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
39static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "di", "si" };
40static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "edi", "esi" };
41
42static void DumpReg0(std::ostream& os, uint8_t /*rex*/, size_t reg,
43 bool byte_operand, uint8_t size_override) {
44 DCHECK_LT(reg, 8u);
45 // TODO: combine rex into size
46 size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : 4);
47 switch (size) {
48 case 1: os << gReg8Names[reg]; break;
49 case 2: os << gReg16Names[reg]; break;
50 case 4: os << gReg32Names[reg]; break;
51 default: LOG(FATAL) << "unexpected size " << size;
52 }
53}
54
55static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
56 bool byte_operand, uint8_t size_override) {
57 size_t reg_num = reg; // TODO: combine with REX.R on 64bit
58 DumpReg0(os, rex, reg_num, byte_operand, size_override);
59}
60
Ian Rogers7caad772012-03-30 01:07:54 -070061static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070062 size_t reg_num = reg; // TODO: combine with REX.B on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070063 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070064}
65
Ian Rogers7caad772012-03-30 01:07:54 -070066static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070067 int reg_num = reg; // TODO: combine with REX.X on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070068 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070069}
70
Elliott Hughes92301d92012-04-10 15:57:52 -070071enum SegmentPrefix {
72 kCs = 0x2e,
73 kSs = 0x36,
74 kDs = 0x3e,
75 kEs = 0x26,
76 kFs = 0x64,
77 kGs = 0x65,
78};
79
Ian Rogers706a10e2012-03-23 17:00:55 -070080static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
81 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -070082 case kCs: os << "cs:"; break;
83 case kSs: os << "ss:"; break;
84 case kDs: os << "ds:"; break;
85 case kEs: os << "es:"; break;
86 case kFs: os << "fs:"; break;
87 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -070088 default: break;
89 }
90}
91
92size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
93 const uint8_t* begin_instr = instr;
94 bool have_prefixes = true;
95 uint8_t prefix[4] = {0, 0, 0, 0};
96 const char** modrm_opcodes = NULL;
97 do {
98 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -070099 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700100 case 0xF0:
101 case 0xF2:
102 case 0xF3:
103 prefix[0] = *instr;
104 break;
105 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700106 case kCs:
107 case kSs:
108 case kDs:
109 case kEs:
110 case kFs:
111 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700112 prefix[1] = *instr;
113 break;
114 // Group 3 - operand size override:
115 case 0x66:
116 prefix[2] = *instr;
117 break;
118 // Group 4 - address size override:
119 case 0x67:
120 prefix[3] = *instr;
121 break;
122 default:
123 have_prefixes = false;
124 break;
125 }
126 if (have_prefixes) {
127 instr++;
128 }
129 } while (have_prefixes);
130 uint8_t rex = (*instr >= 0x40 && *instr <= 0x4F) ? *instr : 0;
131 bool has_modrm = false;
132 bool reg_is_opcode = false;
133 size_t immediate_bytes = 0;
134 size_t branch_bytes = 0;
135 std::ostringstream opcode;
136 bool store = false; // stores to memory (ie rm is on the left)
137 bool load = false; // loads from memory (ie rm is on the right)
138 bool byte_operand = false;
139 bool ax = false; // implicit use of ax
140 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
141 switch (*instr) {
142#define DISASSEMBLER_ENTRY(opname, \
143 rm8_r8, rm32_r32, \
144 r8_rm8, r32_rm32, \
145 ax8_i8, ax32_i32) \
146 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
147 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
148 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
149 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
150 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
151 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
152
153DISASSEMBLER_ENTRY(add,
154 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
155 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
156 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
157DISASSEMBLER_ENTRY(or,
158 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
159 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
160 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
161DISASSEMBLER_ENTRY(adc,
162 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
163 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
164 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
165DISASSEMBLER_ENTRY(sbb,
166 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
167 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
168 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
169DISASSEMBLER_ENTRY(and,
170 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
171 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
172 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
173DISASSEMBLER_ENTRY(sub,
174 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
175 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
176 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
177DISASSEMBLER_ENTRY(xor,
178 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
179 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
180 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
181DISASSEMBLER_ENTRY(cmp,
182 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
183 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
184 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
185
186#undef DISASSEMBLER_ENTRY
187 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
188 opcode << "push";
189 reg_in_opcode = true;
190 break;
191 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
192 opcode << "pop";
193 reg_in_opcode = true;
194 break;
195 case 0x68: opcode << "push"; immediate_bytes = 4; break;
196 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
197 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
198 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
199 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700200 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
201 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700202 };
203 opcode << "j" << condition_codes[*instr & 0xF];
204 branch_bytes = 1;
205 break;
206 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
207 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
208 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
209 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
210
211 case 0x0F: // 2 byte extended opcode
212 instr++;
213 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700214 case 0x10: case 0x11:
215 if (prefix[0] == 0xF2) {
216 opcode << "movsd";
217 } else if (prefix[0] == 0xF3) {
218 opcode << "movss";
219 } else if (prefix[2] == 0x66) {
220 opcode << "movupd";
221 } else {
222 opcode << "movups";
223 }
224 has_modrm = true;
225 load = *instr == 0x10;
226 store = !load;
227 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700228 case 0x38: // 3 byte extended opcode
229 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
230 break;
231 case 0x3A: // 3 byte extended opcode
232 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
233 break;
234 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
235 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
236 opcode << "j" << condition_codes[*instr & 0xF];
237 branch_bytes = 4;
238 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700239 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
240 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
241 opcode << "set" << condition_codes[*instr & 0xF];
242 modrm_opcodes = NULL;
243 reg_is_opcode = true;
244 has_modrm = true;
245 store = true;
246 break;
247 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
248 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700249 default:
250 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
251 break;
252 }
253 break;
254 case 0x80: case 0x81: case 0x82: case 0x83:
255 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
256 modrm_opcodes = x80_opcodes;
257 has_modrm = true;
258 reg_is_opcode = true;
259 store = true;
260 byte_operand = (*instr & 1) == 0;
261 immediate_bytes = *instr == 0x81 ? 4 : 1;
262 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700263 case 0x8D:
264 opcode << "lea";
265 has_modrm = true;
266 load = true;
267 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700268 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
269 opcode << "mov";
270 immediate_bytes = 1;
271 reg_in_opcode = true;
272 break;
273 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
274 opcode << "mov";
275 immediate_bytes = 4;
276 reg_in_opcode = true;
277 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700278 case 0xC0: case 0xC1:
279 static const char* shift_opcodes[] =
280 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
281 modrm_opcodes = shift_opcodes;
282 has_modrm = true;
283 reg_is_opcode = true;
284 store = true;
285 immediate_bytes = 1;
286 byte_operand = *instr == 0xC0;
287 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700288 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700289 case 0xC7:
290 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
291 modrm_opcodes = c7_opcodes;
292 store = true;
293 immediate_bytes = 4;
294 has_modrm = true;
295 reg_is_opcode = true;
296 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700297 case 0xCC: opcode << "int 3"; break;
298 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700299 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
300 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
301 case 0xFF:
302 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
303 modrm_opcodes = ff_opcodes;
304 has_modrm = true;
305 reg_is_opcode = true;
306 load = true;
307 break;
308 default:
309 opcode << StringPrintf("unknown opcode '%02X'", *instr);
310 break;
311 }
312 std::ostringstream args;
313 if (reg_in_opcode) {
314 DCHECK(!has_modrm);
315 DumpReg(args, rex, *instr & 0x7, false, prefix[2]);
316 }
317 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700318 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700319 if (has_modrm) {
320 uint8_t modrm = *instr;
321 instr++;
322 uint8_t mod = modrm >> 6;
323 uint8_t reg_or_opcode = (modrm >> 3) & 7;
324 uint8_t rm = modrm & 7;
325 std::ostringstream address;
326 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700327 address_bits = *reinterpret_cast<const uint32_t*>(instr);
328 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700329 instr += 4;
330 } else if (rm == 4 && mod != 3) { // SIB
331 uint8_t sib = *instr;
332 instr++;
333 uint8_t ss = (sib >> 6) & 3;
334 uint8_t index = (sib >> 3) & 7;
335 uint8_t base = sib & 7;
336 address << "[";
337 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700338 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700339 if (index != 4) {
340 address << " + ";
341 }
342 }
343 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700344 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700345 if (ss != 0) {
346 address << StringPrintf(" * %d", 1 << ss);
347 }
348 }
349 if (mod == 1) {
350 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
351 instr++;
352 } else if (mod == 2) {
353 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
354 instr += 4;
355 }
356 address << "]";
357 } else {
358 if (mod != 3) {
359 address << "[";
360 }
Ian Rogers7caad772012-03-30 01:07:54 -0700361 DumpBaseReg(address, rex, rm);
Ian Rogers706a10e2012-03-23 17:00:55 -0700362 if (mod == 1) {
363 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
364 instr++;
365 } else if (mod == 2) {
366 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
367 instr += 4;
368 }
369 if (mod != 3) {
370 address << "]";
371 }
372 }
373
Ian Rogers7caad772012-03-30 01:07:54 -0700374 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700375 opcode << modrm_opcodes[reg_or_opcode];
376 }
377 if (load) {
378 if (!reg_is_opcode) {
379 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2]);
380 args << ", ";
381 }
382 DumpSegmentOverride(args, prefix[1]);
383 args << address.str();
384 } else {
385 DCHECK(store);
386 DumpSegmentOverride(args, prefix[1]);
387 args << address.str();
388 if (!reg_is_opcode) {
389 args << ", ";
390 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2]);
391 }
392 }
393 }
394 if (ax) {
395 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2]);
396 }
397 if (immediate_bytes > 0) {
398 if (has_modrm || reg_in_opcode || ax) {
399 args << ", ";
400 }
401 if (immediate_bytes == 1) {
402 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
403 instr++;
404 } else {
405 CHECK_EQ(immediate_bytes, 4u);
406 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
407 instr += 4;
408 }
409 } else if (branch_bytes > 0) {
410 DCHECK(!has_modrm);
411 int32_t displacement;
412 if (branch_bytes == 1) {
413 displacement = *reinterpret_cast<const int8_t*>(instr);
414 instr++;
415 } else {
416 CHECK_EQ(branch_bytes, 4u);
417 displacement = *reinterpret_cast<const int32_t*>(instr);
418 instr += 4;
419 }
420 args << StringPrintf("%d (%p)", displacement, instr + displacement);
421 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700422 if (prefix[1] == kFs) {
423 args << " ; ";
424 Thread::DumpThreadOffset(args, address_bits, 4);
425 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700426 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700427 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700428 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700429 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700430 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 -0700431 return instr - begin_instr;
432}
433
434} // namespace x86
435} // namespace art