Disassembler.cpp revision 98c5ddabca1debf935a07d14d0cbc9732374bdb8
1//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===// 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#include "Disassembler.h" 11#include "llvm-c/Disassembler.h" 12 13#include "llvm/MC/MCAsmInfo.h" 14#include "llvm/MC/MCContext.h" 15#include "llvm/MC/MCDisassembler.h" 16#include "llvm/MC/MCInst.h" 17#include "llvm/MC/MCInstPrinter.h" 18#include "llvm/MC/MCRegisterInfo.h" 19#include "llvm/Support/MemoryObject.h" 20#include "llvm/Support/TargetRegistry.h" 21#include "llvm/Support/TargetSelect.h" 22 23namespace llvm { 24class Target; 25} // namespace llvm 26using namespace llvm; 27 28// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic 29// disassembly is supported by passing a block of information in the DisInfo 30// parameter and specifying the TagType and callback functions as described in 31// the header llvm-c/Disassembler.h . The pointer to the block and the 32// functions can all be passed as NULL. If successful, this returns a 33// disassembler context. If not, it returns NULL. 34// 35LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, 36 int TagType, LLVMOpInfoCallback GetOpInfo, 37 LLVMSymbolLookupCallback SymbolLookUp) { 38 // Initialize targets and assembly printers/parsers. 39 llvm::InitializeAllTargetInfos(); 40 llvm::InitializeAllTargetMCs(); 41 llvm::InitializeAllAsmParsers(); 42 llvm::InitializeAllDisassemblers(); 43 44 // Get the target. 45 std::string Error; 46 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); 47 assert(TheTarget && "Unable to create target!"); 48 49 // Get the assembler info needed to setup the MCContext. 50 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); 51 assert(MAI && "Unable to create target asm info!"); 52 53 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); 54 assert(MRI && "Unable to create target register info!"); 55 56 // Package up features to be passed to target/subtarget 57 std::string FeaturesStr; 58 std::string CPU; 59 60 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU, 61 FeaturesStr); 62 assert(STI && "Unable to create subtarget info!"); 63 64 // Set up the MCContext for creating symbols and MCExpr's. 65 MCContext *Ctx = new MCContext(*MAI, *MRI, 0); 66 assert(Ctx && "Unable to create MCContext!"); 67 68 // Set up disassembler. 69 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); 70 assert(DisAsm && "Unable to create disassembler!"); 71 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); 72 73 // Set up the instruction printer. 74 int AsmPrinterVariant = MAI->getAssemblerDialect(); 75 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, 76 *MAI, *STI); 77 assert(IP && "Unable to create instruction printer!"); 78 79 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, 80 GetOpInfo, SymbolLookUp, 81 TheTarget, MAI, MRI, 82 Ctx, DisAsm, IP); 83 assert(DC && "Allocation failure!"); 84 85 IP->setCommentStream(DC->CommentStream); 86 87 return DC; 88} 89 90// 91// LLVMDisasmDispose() disposes of the disassembler specified by the context. 92// 93void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 94 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 95 delete DC; 96} 97 98namespace { 99// 100// The memory object created by LLVMDisasmInstruction(). 101// 102class DisasmMemoryObject : public MemoryObject { 103 uint8_t *Bytes; 104 uint64_t Size; 105 uint64_t BasePC; 106public: 107 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 108 Bytes(bytes), Size(size), BasePC(basePC) {} 109 110 uint64_t getBase() const { return BasePC; } 111 uint64_t getExtent() const { return Size; } 112 113 int readByte(uint64_t Addr, uint8_t *Byte) const { 114 if (Addr - BasePC >= Size) 115 return -1; 116 *Byte = Bytes[Addr - BasePC]; 117 return 0; 118 } 119}; 120} // end anonymous namespace 121 122// 123// LLVMDisasmInstruction() disassembles a single instruction using the 124// disassembler context specified in the parameter DC. The bytes of the 125// instruction are specified in the parameter Bytes, and contains at least 126// BytesSize number of bytes. The instruction is at the address specified by 127// the PC parameter. If a valid instruction can be disassembled its string is 128// returned indirectly in OutString which whos size is specified in the 129// parameter OutStringSize. This function returns the number of bytes in the 130// instruction or zero if there was no valid instruction. If this function 131// returns zero the caller will have to pick how many bytes they want to step 132// over by printing a .byte, .long etc. to continue. 133// 134size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 135 uint64_t BytesSize, uint64_t PC, char *OutString, 136 size_t OutStringSize){ 137 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 138 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 139 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 140 141 uint64_t Size; 142 MCInst Inst; 143 const MCDisassembler *DisAsm = DC->getDisAsm(); 144 MCInstPrinter *IP = DC->getIP(); 145 MCDisassembler::DecodeStatus S; 146 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, 147 /*REMOVE*/ nulls(), DC->CommentStream); 148 switch (S) { 149 case MCDisassembler::Fail: 150 case MCDisassembler::SoftFail: 151 // FIXME: Do something different for soft failure modes? 152 return 0; 153 154 case MCDisassembler::Success: { 155 DC->CommentStream.flush(); 156 StringRef Comments = DC->CommentsToEmit.str(); 157 158 SmallVector<char, 64> InsnStr; 159 raw_svector_ostream OS(InsnStr); 160 IP->printInst(&Inst, OS, Comments); 161 OS.flush(); 162 163 // Tell the comment stream that the vector changed underneath it. 164 DC->CommentsToEmit.clear(); 165 DC->CommentStream.resync(); 166 167 assert(OutStringSize != 0 && "Output buffer cannot be zero size"); 168 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); 169 std::memcpy(OutString, InsnStr.data(), OutputSize); 170 OutString[OutputSize] = '\0'; // Terminate string. 171 172 return Size; 173 } 174 } 175 return 0; 176} 177