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