1 //===- X86Disassembler.cpp - Disassembler for x86 and x86_64 ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is part of the X86 Disassembler. 11 // It contains code to translate the data produced by the decoder into 12 // MCInsts. 13 // Documentation for the disassembler can be found in X86Disassembler.h. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "X86Disassembler.h" 18 #include "X86DisassemblerDecoder.h" 19 20 #include "llvm/MC/EDInstInfo.h" 21 #include "llvm/MC/MCDisassembler.h" 22 #include "llvm/MC/MCDisassembler.h" 23 #include "llvm/MC/MCInst.h" 24 #include "llvm/Target/TargetRegistry.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/MemoryObject.h" 27 #include "llvm/Support/raw_ostream.h" 28 29 #include "X86GenRegisterNames.inc" 30 #include "X86GenEDInfo.inc" 31 32 using namespace llvm; 33 using namespace llvm::X86Disassembler; 34 35 void x86DisassemblerDebug(const char *file, 36 unsigned line, 37 const char *s) { 38 dbgs() << file << ":" << line << ": " << s; 39 } 40 41 #define debug(s) DEBUG(x86DisassemblerDebug(__FILE__, __LINE__, s)); 42 43 namespace llvm { 44 45 // Fill-ins to make the compiler happy. These constants are never actually 46 // assigned; they are just filler to make an automatically-generated switch 47 // statement work. 48 namespace X86 { 49 enum { 50 BX_SI = 500, 51 BX_DI = 501, 52 BP_SI = 502, 53 BP_DI = 503, 54 sib = 504, 55 sib64 = 505 56 }; 57 } 58 59 extern Target TheX86_32Target, TheX86_64Target; 60 61 } 62 63 static bool translateInstruction(MCInst &target, 64 InternalInstruction &source); 65 66 X86GenericDisassembler::X86GenericDisassembler(DisassemblerMode mode) : 67 MCDisassembler(), 68 fMode(mode) { 69 } 70 71 X86GenericDisassembler::~X86GenericDisassembler() { 72 } 73 74 EDInstInfo *X86GenericDisassembler::getEDInfo() const { 75 return instInfoX86; 76 } 77 78 /// regionReader - a callback function that wraps the readByte method from 79 /// MemoryObject. 80 /// 81 /// @param arg - The generic callback parameter. In this case, this should 82 /// be a pointer to a MemoryObject. 83 /// @param byte - A pointer to the byte to be read. 84 /// @param address - The address to be read. 85 static int regionReader(void* arg, uint8_t* byte, uint64_t address) { 86 MemoryObject* region = static_cast<MemoryObject*>(arg); 87 return region->readByte(address, byte); 88 } 89 90 /// logger - a callback function that wraps the operator<< method from 91 /// raw_ostream. 92 /// 93 /// @param arg - The generic callback parameter. This should be a pointe 94 /// to a raw_ostream. 95 /// @param log - A string to be logged. logger() adds a newline. 96 static void logger(void* arg, const char* log) { 97 if (!arg) 98 return; 99 100 raw_ostream &vStream = *(static_cast<raw_ostream*>(arg)); 101 vStream << log << "\n"; 102 } 103 104 // 105 // Public interface for the disassembler 106 // 107 108 bool X86GenericDisassembler::getInstruction(MCInst &instr, 109 uint64_t &size, 110 const MemoryObject ®ion, 111 uint64_t address, 112 raw_ostream &vStream) const { 113 InternalInstruction internalInstr; 114 115 int ret = decodeInstruction(&internalInstr, 116 regionReader, 117 (void*)®ion, 118 logger, 119 (void*)&vStream, 120 address, 121 fMode); 122 123 if (ret) { 124 size = internalInstr.readerCursor - address; 125 return false; 126 } 127 else { 128 size = internalInstr.length; 129 return !translateInstruction(instr, internalInstr); 130 } 131 } 132 133 // 134 // Private code that translates from struct InternalInstructions to MCInsts. 135 // 136 137 /// translateRegister - Translates an internal register to the appropriate LLVM 138 /// register, and appends it as an operand to an MCInst. 139 /// 140 /// @param mcInst - The MCInst to append to. 141 /// @param reg - The Reg to append. 142 static void translateRegister(MCInst &mcInst, Reg reg) { 143 #define ENTRY(x) X86::x, 144 uint8_t llvmRegnums[] = { 145 ALL_REGS 146 0 147 }; 148 #undef ENTRY 149 150 uint8_t llvmRegnum = llvmRegnums[reg]; 151 mcInst.addOperand(MCOperand::CreateReg(llvmRegnum)); 152 } 153 154 /// translateImmediate - Appends an immediate operand to an MCInst. 155 /// 156 /// @param mcInst - The MCInst to append to. 157 /// @param immediate - The immediate value to append. 158 /// @param operand - The operand, as stored in the descriptor table. 159 /// @param insn - The internal instruction. 160 static void translateImmediate(MCInst &mcInst, 161 uint64_t immediate, 162 OperandSpecifier &operand, 163 InternalInstruction &insn) { 164 // Sign-extend the immediate if necessary. 165 166 OperandType type = operand.type; 167 168 if (type == TYPE_RELv) { 169 switch (insn.displacementSize) { 170 default: 171 break; 172 case 8: 173 type = TYPE_MOFFS8; 174 break; 175 case 16: 176 type = TYPE_MOFFS16; 177 break; 178 case 32: 179 type = TYPE_MOFFS32; 180 break; 181 case 64: 182 type = TYPE_MOFFS64; 183 break; 184 } 185 } 186 187 switch (type) { 188 case TYPE_MOFFS8: 189 case TYPE_REL8: 190 if(immediate & 0x80) 191 immediate |= ~(0xffull); 192 break; 193 case TYPE_MOFFS16: 194 if(immediate & 0x8000) 195 immediate |= ~(0xffffull); 196 break; 197 case TYPE_MOFFS32: 198 case TYPE_REL32: 199 case TYPE_REL64: 200 if(immediate & 0x80000000) 201 immediate |= ~(0xffffffffull); 202 break; 203 case TYPE_MOFFS64: 204 default: 205 // operand is 64 bits wide. Do nothing. 206 break; 207 } 208 209 mcInst.addOperand(MCOperand::CreateImm(immediate)); 210 } 211 212 /// translateRMRegister - Translates a register stored in the R/M field of the 213 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst. 214 /// @param mcInst - The MCInst to append to. 215 /// @param insn - The internal instruction to extract the R/M field 216 /// from. 217 /// @return - 0 on success; -1 otherwise 218 static bool translateRMRegister(MCInst &mcInst, 219 InternalInstruction &insn) { 220 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { 221 debug("A R/M register operand may not have a SIB byte"); 222 return true; 223 } 224 225 switch (insn.eaBase) { 226 default: 227 debug("Unexpected EA base register"); 228 return true; 229 case EA_BASE_NONE: 230 debug("EA_BASE_NONE for ModR/M base"); 231 return true; 232 #define ENTRY(x) case EA_BASE_##x: 233 ALL_EA_BASES 234 #undef ENTRY 235 debug("A R/M register operand may not have a base; " 236 "the operand must be a register."); 237 return true; 238 #define ENTRY(x) \ 239 case EA_REG_##x: \ 240 mcInst.addOperand(MCOperand::CreateReg(X86::x)); break; 241 ALL_REGS 242 #undef ENTRY 243 } 244 245 return false; 246 } 247 248 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M 249 /// fields of an internal instruction (and possibly its SIB byte) to a memory 250 /// operand in LLVM's format, and appends it to an MCInst. 251 /// 252 /// @param mcInst - The MCInst to append to. 253 /// @param insn - The instruction to extract Mod, R/M, and SIB fields 254 /// from. 255 /// @return - 0 on success; nonzero otherwise 256 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn) { 257 // Addresses in an MCInst are represented as five operands: 258 // 1. basereg (register) The R/M base, or (if there is a SIB) the 259 // SIB base 260 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified 261 // scale amount 262 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB) 263 // the index (which is multiplied by the 264 // scale amount) 265 // 4. displacement (immediate) 0, or the displacement if there is one 266 // 5. segmentreg (register) x86_registerNONE for now, but could be set 267 // if we have segment overrides 268 269 MCOperand baseReg; 270 MCOperand scaleAmount; 271 MCOperand indexReg; 272 MCOperand displacement; 273 MCOperand segmentReg; 274 275 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) { 276 if (insn.sibBase != SIB_BASE_NONE) { 277 switch (insn.sibBase) { 278 default: 279 debug("Unexpected sibBase"); 280 return true; 281 #define ENTRY(x) \ 282 case SIB_BASE_##x: \ 283 baseReg = MCOperand::CreateReg(X86::x); break; 284 ALL_SIB_BASES 285 #undef ENTRY 286 } 287 } else { 288 baseReg = MCOperand::CreateReg(0); 289 } 290 291 if (insn.sibIndex != SIB_INDEX_NONE) { 292 switch (insn.sibIndex) { 293 default: 294 debug("Unexpected sibIndex"); 295 return true; 296 #define ENTRY(x) \ 297 case SIB_INDEX_##x: \ 298 indexReg = MCOperand::CreateReg(X86::x); break; 299 EA_BASES_32BIT 300 EA_BASES_64BIT 301 #undef ENTRY 302 } 303 } else { 304 indexReg = MCOperand::CreateReg(0); 305 } 306 307 scaleAmount = MCOperand::CreateImm(insn.sibScale); 308 } else { 309 switch (insn.eaBase) { 310 case EA_BASE_NONE: 311 if (insn.eaDisplacement == EA_DISP_NONE) { 312 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base"); 313 return true; 314 } 315 if (insn.mode == MODE_64BIT) 316 baseReg = MCOperand::CreateReg(X86::RIP); // Section 2.2.1.6 317 else 318 baseReg = MCOperand::CreateReg(0); 319 320 indexReg = MCOperand::CreateReg(0); 321 break; 322 case EA_BASE_BX_SI: 323 baseReg = MCOperand::CreateReg(X86::BX); 324 indexReg = MCOperand::CreateReg(X86::SI); 325 break; 326 case EA_BASE_BX_DI: 327 baseReg = MCOperand::CreateReg(X86::BX); 328 indexReg = MCOperand::CreateReg(X86::DI); 329 break; 330 case EA_BASE_BP_SI: 331 baseReg = MCOperand::CreateReg(X86::BP); 332 indexReg = MCOperand::CreateReg(X86::SI); 333 break; 334 case EA_BASE_BP_DI: 335 baseReg = MCOperand::CreateReg(X86::BP); 336 indexReg = MCOperand::CreateReg(X86::DI); 337 break; 338 default: 339 indexReg = MCOperand::CreateReg(0); 340 switch (insn.eaBase) { 341 default: 342 debug("Unexpected eaBase"); 343 return true; 344 // Here, we will use the fill-ins defined above. However, 345 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and 346 // sib and sib64 were handled in the top-level if, so they're only 347 // placeholders to keep the compiler happy. 348 #define ENTRY(x) \ 349 case EA_BASE_##x: \ 350 baseReg = MCOperand::CreateReg(X86::x); break; 351 ALL_EA_BASES 352 #undef ENTRY 353 #define ENTRY(x) case EA_REG_##x: 354 ALL_REGS 355 #undef ENTRY 356 debug("A R/M memory operand may not be a register; " 357 "the base field must be a base."); 358 return true; 359 } 360 } 361 362 scaleAmount = MCOperand::CreateImm(1); 363 } 364 365 displacement = MCOperand::CreateImm(insn.displacement); 366 367 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = { 368 0, // SEG_OVERRIDE_NONE 369 X86::CS, 370 X86::SS, 371 X86::DS, 372 X86::ES, 373 X86::FS, 374 X86::GS 375 }; 376 377 segmentReg = MCOperand::CreateReg(segmentRegnums[insn.segmentOverride]); 378 379 mcInst.addOperand(baseReg); 380 mcInst.addOperand(scaleAmount); 381 mcInst.addOperand(indexReg); 382 mcInst.addOperand(displacement); 383 mcInst.addOperand(segmentReg); 384 return false; 385 } 386 387 /// translateRM - Translates an operand stored in the R/M (and possibly SIB) 388 /// byte of an instruction to LLVM form, and appends it to an MCInst. 389 /// 390 /// @param mcInst - The MCInst to append to. 391 /// @param operand - The operand, as stored in the descriptor table. 392 /// @param insn - The instruction to extract Mod, R/M, and SIB fields 393 /// from. 394 /// @return - 0 on success; nonzero otherwise 395 static bool translateRM(MCInst &mcInst, 396 OperandSpecifier &operand, 397 InternalInstruction &insn) { 398 switch (operand.type) { 399 default: 400 debug("Unexpected type for a R/M operand"); 401 return true; 402 case TYPE_R8: 403 case TYPE_R16: 404 case TYPE_R32: 405 case TYPE_R64: 406 case TYPE_Rv: 407 case TYPE_MM: 408 case TYPE_MM32: 409 case TYPE_MM64: 410 case TYPE_XMM: 411 case TYPE_XMM32: 412 case TYPE_XMM64: 413 case TYPE_XMM128: 414 case TYPE_DEBUGREG: 415 case TYPE_CONTROLREG: 416 return translateRMRegister(mcInst, insn); 417 case TYPE_M: 418 case TYPE_M8: 419 case TYPE_M16: 420 case TYPE_M32: 421 case TYPE_M64: 422 case TYPE_M128: 423 case TYPE_M512: 424 case TYPE_Mv: 425 case TYPE_M32FP: 426 case TYPE_M64FP: 427 case TYPE_M80FP: 428 case TYPE_M16INT: 429 case TYPE_M32INT: 430 case TYPE_M64INT: 431 case TYPE_M1616: 432 case TYPE_M1632: 433 case TYPE_M1664: 434 case TYPE_LEA: 435 return translateRMMemory(mcInst, insn); 436 } 437 } 438 439 /// translateFPRegister - Translates a stack position on the FPU stack to its 440 /// LLVM form, and appends it to an MCInst. 441 /// 442 /// @param mcInst - The MCInst to append to. 443 /// @param stackPos - The stack position to translate. 444 /// @return - 0 on success; nonzero otherwise. 445 static bool translateFPRegister(MCInst &mcInst, 446 uint8_t stackPos) { 447 if (stackPos >= 8) { 448 debug("Invalid FP stack position"); 449 return true; 450 } 451 452 mcInst.addOperand(MCOperand::CreateReg(X86::ST0 + stackPos)); 453 454 return false; 455 } 456 457 /// translateOperand - Translates an operand stored in an internal instruction 458 /// to LLVM's format and appends it to an MCInst. 459 /// 460 /// @param mcInst - The MCInst to append to. 461 /// @param operand - The operand, as stored in the descriptor table. 462 /// @param insn - The internal instruction. 463 /// @return - false on success; true otherwise. 464 static bool translateOperand(MCInst &mcInst, 465 OperandSpecifier &operand, 466 InternalInstruction &insn) { 467 switch (operand.encoding) { 468 default: 469 debug("Unhandled operand encoding during translation"); 470 return true; 471 case ENCODING_REG: 472 translateRegister(mcInst, insn.reg); 473 return false; 474 case ENCODING_RM: 475 return translateRM(mcInst, operand, insn); 476 case ENCODING_CB: 477 case ENCODING_CW: 478 case ENCODING_CD: 479 case ENCODING_CP: 480 case ENCODING_CO: 481 case ENCODING_CT: 482 debug("Translation of code offsets isn't supported."); 483 return true; 484 case ENCODING_IB: 485 case ENCODING_IW: 486 case ENCODING_ID: 487 case ENCODING_IO: 488 case ENCODING_Iv: 489 case ENCODING_Ia: 490 translateImmediate(mcInst, 491 insn.immediates[insn.numImmediatesTranslated++], 492 operand, 493 insn); 494 return false; 495 case ENCODING_RB: 496 case ENCODING_RW: 497 case ENCODING_RD: 498 case ENCODING_RO: 499 translateRegister(mcInst, insn.opcodeRegister); 500 return false; 501 case ENCODING_I: 502 return translateFPRegister(mcInst, insn.opcodeModifier); 503 case ENCODING_Rv: 504 translateRegister(mcInst, insn.opcodeRegister); 505 return false; 506 case ENCODING_DUP: 507 return translateOperand(mcInst, 508 insn.spec->operands[operand.type - TYPE_DUP0], 509 insn); 510 } 511 } 512 513 /// translateInstruction - Translates an internal instruction and all its 514 /// operands to an MCInst. 515 /// 516 /// @param mcInst - The MCInst to populate with the instruction's data. 517 /// @param insn - The internal instruction. 518 /// @return - false on success; true otherwise. 519 static bool translateInstruction(MCInst &mcInst, 520 InternalInstruction &insn) { 521 if (!insn.spec) { 522 debug("Instruction has no specification"); 523 return true; 524 } 525 526 mcInst.setOpcode(insn.instructionID); 527 528 int index; 529 530 insn.numImmediatesTranslated = 0; 531 532 for (index = 0; index < X86_MAX_OPERANDS; ++index) { 533 if (insn.spec->operands[index].encoding != ENCODING_NONE) { 534 if (translateOperand(mcInst, insn.spec->operands[index], insn)) { 535 return true; 536 } 537 } 538 } 539 540 return false; 541 } 542 543 static MCDisassembler *createX86_32Disassembler(const Target &T) { 544 return new X86Disassembler::X86_32Disassembler; 545 } 546 547 static MCDisassembler *createX86_64Disassembler(const Target &T) { 548 return new X86Disassembler::X86_64Disassembler; 549 } 550 551 extern "C" void LLVMInitializeX86Disassembler() { 552 // Register the disassembler. 553 TargetRegistry::RegisterMCDisassembler(TheX86_32Target, 554 createX86_32Disassembler); 555 TargetRegistry::RegisterMCDisassembler(TheX86_64Target, 556 createX86_64Disassembler); 557 } 558