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