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 #include "llvm/MC/MCAsmInfo.h" 13 #include "llvm/MC/MCContext.h" 14 #include "llvm/MC/MCDisassembler.h" 15 #include "llvm/MC/MCInst.h" 16 #include "llvm/MC/MCInstPrinter.h" 17 #include "llvm/MC/MCInstrInfo.h" 18 #include "llvm/MC/MCRegisterInfo.h" 19 #include "llvm/MC/MCRelocationInfo.h" 20 #include "llvm/MC/MCSubtargetInfo.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/MemoryObject.h" 23 #include "llvm/Support/TargetRegistry.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 LLVMCreateDisasmCPU(const char *Triple, const char *CPU, 38 void *DisInfo, int TagType, 39 LLVMOpInfoCallback GetOpInfo, 40 LLVMSymbolLookupCallback SymbolLookUp){ 41 // Get the target. 42 std::string Error; 43 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 44 if (!TheTarget) 45 return 0; 46 47 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple); 48 if (!MRI) 49 return 0; 50 51 // Get the assembler info needed to setup the MCContext. 52 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple); 53 if (!MAI) 54 return 0; 55 56 const MCInstrInfo *MII = TheTarget->createMCInstrInfo(); 57 if (!MII) 58 return 0; 59 60 // Package up features to be passed to target/subtarget 61 std::string FeaturesStr; 62 63 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU, 64 FeaturesStr); 65 if (!STI) 66 return 0; 67 68 // Set up the MCContext for creating symbols and MCExpr's. 69 MCContext *Ctx = new MCContext(*MAI, *MRI, 0); 70 if (!Ctx) 71 return 0; 72 73 // Set up disassembler. 74 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); 75 if (!DisAsm) 76 return 0; 77 78 OwningPtr<MCRelocationInfo> RelInfo( 79 TheTarget->createMCRelocationInfo(Triple, *Ctx)); 80 if (!RelInfo) 81 return 0; 82 83 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, 84 Ctx, RelInfo); 85 86 // Set up the instruction printer. 87 int AsmPrinterVariant = MAI->getAssemblerDialect(); 88 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, 89 *MAI, *MII, *MRI, *STI); 90 if (!IP) 91 return 0; 92 93 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType, 94 GetOpInfo, SymbolLookUp, 95 TheTarget, MAI, MRI, 96 STI, MII, Ctx, DisAsm, IP); 97 if (!DC) 98 return 0; 99 100 return DC; 101 } 102 103 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo, 104 int TagType, LLVMOpInfoCallback GetOpInfo, 105 LLVMSymbolLookupCallback SymbolLookUp) { 106 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo, 107 SymbolLookUp); 108 } 109 110 // 111 // LLVMDisasmDispose() disposes of the disassembler specified by the context. 112 // 113 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 114 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 115 delete DC; 116 } 117 118 namespace { 119 // 120 // The memory object created by LLVMDisasmInstruction(). 121 // 122 class DisasmMemoryObject : public MemoryObject { 123 uint8_t *Bytes; 124 uint64_t Size; 125 uint64_t BasePC; 126 public: 127 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 128 Bytes(bytes), Size(size), BasePC(basePC) {} 129 130 uint64_t getBase() const { return BasePC; } 131 uint64_t getExtent() const { return Size; } 132 133 int readByte(uint64_t Addr, uint8_t *Byte) const { 134 if (Addr - BasePC >= Size) 135 return -1; 136 *Byte = Bytes[Addr - BasePC]; 137 return 0; 138 } 139 }; 140 } // end anonymous namespace 141 142 // 143 // LLVMDisasmInstruction() disassembles a single instruction using the 144 // disassembler context specified in the parameter DC. The bytes of the 145 // instruction are specified in the parameter Bytes, and contains at least 146 // BytesSize number of bytes. The instruction is at the address specified by 147 // the PC parameter. If a valid instruction can be disassembled its string is 148 // returned indirectly in OutString which whos size is specified in the 149 // parameter OutStringSize. This function returns the number of bytes in the 150 // instruction or zero if there was no valid instruction. If this function 151 // returns zero the caller will have to pick how many bytes they want to step 152 // over by printing a .byte, .long etc. to continue. 153 // 154 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 155 uint64_t BytesSize, uint64_t PC, char *OutString, 156 size_t OutStringSize){ 157 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 158 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 159 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 160 161 uint64_t Size; 162 MCInst Inst; 163 const MCDisassembler *DisAsm = DC->getDisAsm(); 164 MCInstPrinter *IP = DC->getIP(); 165 MCDisassembler::DecodeStatus S; 166 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, 167 /*REMOVE*/ nulls(), DC->CommentStream); 168 switch (S) { 169 case MCDisassembler::Fail: 170 case MCDisassembler::SoftFail: 171 // FIXME: Do something different for soft failure modes? 172 return 0; 173 174 case MCDisassembler::Success: { 175 DC->CommentStream.flush(); 176 StringRef Comments = DC->CommentsToEmit.str(); 177 178 SmallVector<char, 64> InsnStr; 179 raw_svector_ostream OS(InsnStr); 180 IP->printInst(&Inst, OS, Comments); 181 OS.flush(); 182 183 // Tell the comment stream that the vector changed underneath it. 184 DC->CommentsToEmit.clear(); 185 DC->CommentStream.resync(); 186 187 assert(OutStringSize != 0 && "Output buffer cannot be zero size"); 188 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); 189 std::memcpy(OutString, InsnStr.data(), OutputSize); 190 OutString[OutputSize] = '\0'; // Terminate string. 191 192 return Size; 193 } 194 } 195 llvm_unreachable("Invalid DecodeStatus!"); 196 } 197 198 // 199 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it 200 // can set all the Options and 0 otherwise. 201 // 202 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ 203 if (Options & LLVMDisassembler_Option_UseMarkup){ 204 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 205 MCInstPrinter *IP = DC->getIP(); 206 IP->setUseMarkup(1); 207 Options &= ~LLVMDisassembler_Option_UseMarkup; 208 } 209 if (Options & LLVMDisassembler_Option_PrintImmHex){ 210 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 211 MCInstPrinter *IP = DC->getIP(); 212 IP->setPrintImmHex(1); 213 Options &= ~LLVMDisassembler_Option_PrintImmHex; 214 } 215 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){ 216 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 217 // Try to set up the new instruction printer. 218 const MCAsmInfo *MAI = DC->getAsmInfo(); 219 const MCInstrInfo *MII = DC->getInstrInfo(); 220 const MCRegisterInfo *MRI = DC->getRegisterInfo(); 221 const MCSubtargetInfo *STI = DC->getSubtargetInfo(); 222 int AsmPrinterVariant = MAI->getAssemblerDialect(); 223 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0; 224 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter( 225 AsmPrinterVariant, *MAI, *MII, *MRI, *STI); 226 if (IP) { 227 DC->setIP(IP); 228 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant; 229 } 230 } 231 return (Options == 0); 232 } 233