LLVM API Documentation
00001 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "Disassembler.h" 00011 #include "llvm-c/Disassembler.h" 00012 #include "llvm/MC/MCAsmInfo.h" 00013 #include "llvm/MC/MCContext.h" 00014 #include "llvm/MC/MCDisassembler.h" 00015 #include "llvm/MC/MCInst.h" 00016 #include "llvm/MC/MCInstPrinter.h" 00017 #include "llvm/MC/MCInstrInfo.h" 00018 #include "llvm/MC/MCRegisterInfo.h" 00019 #include "llvm/MC/MCSubtargetInfo.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 #include "llvm/Support/MemoryObject.h" 00022 #include "llvm/Support/TargetRegistry.h" 00023 00024 namespace llvm { 00025 class Target; 00026 } // namespace llvm 00027 using namespace llvm; 00028 00029 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic 00030 // disassembly is supported by passing a block of information in the DisInfo 00031 // parameter and specifying the TagType and callback functions as described in 00032 // the header llvm-c/Disassembler.h . The pointer to the block and the 00033 // functions can all be passed as NULL. If successful, this returns a 00034 // disassembler context. If not, it returns NULL. 00035 // 00036 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, 00037 void *DisInfo, int TagType, 00038 LLVMOpInfoCallback GetOpInfo, 00039 LLVMSymbolLookupCallback SymbolLookUp){ 00040 // Get the target. 00041 std::string Error; 00042 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 00043 assert(TheTarget && "Unable to create target!"); 00044 00045 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple); 00046 if (!MRI) 00047 return 0; 00048 00049 // Get the assembler info needed to setup the MCContext. 00050 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple); 00051 if (!MAI) 00052 return 0; 00053 00054 const MCInstrInfo *MII = TheTarget->createMCInstrInfo(); 00055 if (!MII) 00056 return 0; 00057 00058 // Package up features to be passed to target/subtarget 00059 std::string FeaturesStr; 00060 00061 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU, 00062 FeaturesStr); 00063 if (!STI) 00064 return 0; 00065 00066 // Set up the MCContext for creating symbols and MCExpr's. 00067 MCContext *Ctx = new MCContext(*MAI, *MRI, 0); 00068 if (!Ctx) 00069 return 0; 00070 00071 // Set up disassembler. 00072 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); 00073 if (!DisAsm) 00074 return 0; 00075 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx); 00076 00077 // Set up the instruction printer. 00078 int AsmPrinterVariant = MAI->getAssemblerDialect(); 00079 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, 00080 *MAI, *MII, *MRI, *STI); 00081 if (!IP) 00082 return 0; 00083 00084 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType, 00085 GetOpInfo, SymbolLookUp, 00086 TheTarget, MAI, MRI, 00087 STI, MII, Ctx, DisAsm, IP); 00088 if (!DC) 00089 return 0; 00090 00091 return DC; 00092 } 00093 00094 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo, 00095 int TagType, LLVMOpInfoCallback GetOpInfo, 00096 LLVMSymbolLookupCallback SymbolLookUp) { 00097 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo, 00098 SymbolLookUp); 00099 } 00100 00101 // 00102 // LLVMDisasmDispose() disposes of the disassembler specified by the context. 00103 // 00104 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 00105 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 00106 delete DC; 00107 } 00108 00109 namespace { 00110 // 00111 // The memory object created by LLVMDisasmInstruction(). 00112 // 00113 class DisasmMemoryObject : public MemoryObject { 00114 uint8_t *Bytes; 00115 uint64_t Size; 00116 uint64_t BasePC; 00117 public: 00118 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 00119 Bytes(bytes), Size(size), BasePC(basePC) {} 00120 00121 uint64_t getBase() const { return BasePC; } 00122 uint64_t getExtent() const { return Size; } 00123 00124 int readByte(uint64_t Addr, uint8_t *Byte) const { 00125 if (Addr - BasePC >= Size) 00126 return -1; 00127 *Byte = Bytes[Addr - BasePC]; 00128 return 0; 00129 } 00130 }; 00131 } // end anonymous namespace 00132 00133 // 00134 // LLVMDisasmInstruction() disassembles a single instruction using the 00135 // disassembler context specified in the parameter DC. The bytes of the 00136 // instruction are specified in the parameter Bytes, and contains at least 00137 // BytesSize number of bytes. The instruction is at the address specified by 00138 // the PC parameter. If a valid instruction can be disassembled its string is 00139 // returned indirectly in OutString which whos size is specified in the 00140 // parameter OutStringSize. This function returns the number of bytes in the 00141 // instruction or zero if there was no valid instruction. If this function 00142 // returns zero the caller will have to pick how many bytes they want to step 00143 // over by printing a .byte, .long etc. to continue. 00144 // 00145 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 00146 uint64_t BytesSize, uint64_t PC, char *OutString, 00147 size_t OutStringSize){ 00148 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 00149 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 00150 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 00151 00152 uint64_t Size; 00153 MCInst Inst; 00154 const MCDisassembler *DisAsm = DC->getDisAsm(); 00155 MCInstPrinter *IP = DC->getIP(); 00156 MCDisassembler::DecodeStatus S; 00157 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, 00158 /*REMOVE*/ nulls(), DC->CommentStream); 00159 switch (S) { 00160 case MCDisassembler::Fail: 00161 case MCDisassembler::SoftFail: 00162 // FIXME: Do something different for soft failure modes? 00163 return 0; 00164 00165 case MCDisassembler::Success: { 00166 DC->CommentStream.flush(); 00167 StringRef Comments = DC->CommentsToEmit.str(); 00168 00169 SmallVector<char, 64> InsnStr; 00170 raw_svector_ostream OS(InsnStr); 00171 IP->printInst(&Inst, OS, Comments); 00172 OS.flush(); 00173 00174 // Tell the comment stream that the vector changed underneath it. 00175 DC->CommentsToEmit.clear(); 00176 DC->CommentStream.resync(); 00177 00178 assert(OutStringSize != 0 && "Output buffer cannot be zero size"); 00179 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); 00180 std::memcpy(OutString, InsnStr.data(), OutputSize); 00181 OutString[OutputSize] = '\0'; // Terminate string. 00182 00183 return Size; 00184 } 00185 } 00186 llvm_unreachable("Invalid DecodeStatus!"); 00187 } 00188 00189 // 00190 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it 00191 // can set all the Options and 0 otherwise. 00192 // 00193 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ 00194 if (Options & LLVMDisassembler_Option_UseMarkup){ 00195 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 00196 MCInstPrinter *IP = DC->getIP(); 00197 IP->setUseMarkup(1); 00198 Options &= ~LLVMDisassembler_Option_UseMarkup; 00199 } 00200 if (Options & LLVMDisassembler_Option_PrintImmHex){ 00201 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 00202 MCInstPrinter *IP = DC->getIP(); 00203 IP->setPrintImmHex(1); 00204 Options &= ~LLVMDisassembler_Option_PrintImmHex; 00205 } 00206 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){ 00207 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 00208 // Try to set up the new instruction printer. 00209 const MCAsmInfo *MAI = DC->getAsmInfo(); 00210 const MCInstrInfo *MII = DC->getInstrInfo(); 00211 const MCRegisterInfo *MRI = DC->getRegisterInfo(); 00212 const MCSubtargetInfo *STI = DC->getSubtargetInfo(); 00213 int AsmPrinterVariant = MAI->getAssemblerDialect(); 00214 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0; 00215 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter( 00216 AsmPrinterVariant, *MAI, *MII, *MRI, *STI); 00217 if (IP) { 00218 DC->setIP(IP); 00219 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant; 00220 } 00221 } 00222 return (Options == 0); 00223 }