1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface -*- C -*-===// 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/Target/TargetRegistry.h" 20 #include "llvm/Target/TargetSelect.h" 21 #include "llvm/Support/MemoryObject.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 // Initialize targets and assembly printers/parsers. 39 llvm::InitializeAllTargetInfos(); 40 llvm::InitializeAllMCAsmInfos(); 41 llvm::InitializeAllMCCodeGenInfos(); 42 llvm::InitializeAllMCRegisterInfos(); 43 llvm::InitializeAllAsmPrinters(); 44 llvm::InitializeAllAsmParsers(); 45 llvm::InitializeAllDisassemblers(); 46 47 // Get the target. 48 std::string Error; 49 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); 50 assert(TheTarget && "Unable to create target!"); 51 52 // Get the assembler info needed to setup the MCContext. 53 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); 54 assert(MAI && "Unable to create target asm info!"); 55 56 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); 57 assert(MRI && "Unable to create target register info!"); 58 59 // Package up features to be passed to target/subtarget 60 std::string FeaturesStr; 61 std::string CPU; 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(); 69 assert(DisAsm && "Unable to create disassembler!"); 70 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); 71 72 // Set up the instruction printer. 73 int AsmPrinterVariant = MAI->getAssemblerDialect(); 74 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, 75 *MAI); 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 Ctx, DisAsm, IP); 82 assert(DC && "Allocation failure!"); 83 return DC; 84 } 85 86 // 87 // LLVMDisasmDispose() disposes of the disassembler specified by the context. 88 // 89 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 90 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 91 delete DC; 92 } 93 94 namespace { 95 // 96 // The memory object created by LLVMDisasmInstruction(). 97 // 98 class DisasmMemoryObject : public MemoryObject { 99 uint8_t *Bytes; 100 uint64_t Size; 101 uint64_t BasePC; 102 public: 103 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 104 Bytes(bytes), Size(size), BasePC(basePC) {} 105 106 uint64_t getBase() const { return BasePC; } 107 uint64_t getExtent() const { return Size; } 108 109 int readByte(uint64_t Addr, uint8_t *Byte) const { 110 if (Addr - BasePC >= Size) 111 return -1; 112 *Byte = Bytes[Addr - BasePC]; 113 return 0; 114 } 115 }; 116 } // end anonymous namespace 117 118 // 119 // LLVMDisasmInstruction() disassembles a single instruction using the 120 // disassembler context specified in the parameter DC. The bytes of the 121 // instruction are specified in the parameter Bytes, and contains at least 122 // BytesSize number of bytes. The instruction is at the address specified by 123 // the PC parameter. If a valid instruction can be disassembled its string is 124 // returned indirectly in OutString which whos size is specified in the 125 // parameter OutStringSize. This function returns the number of bytes in the 126 // instruction or zero if there was no valid instruction. If this function 127 // returns zero the caller will have to pick how many bytes they want to step 128 // over by printing a .byte, .long etc. to continue. 129 // 130 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 131 uint64_t BytesSize, uint64_t PC, char *OutString, 132 size_t OutStringSize){ 133 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 134 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 135 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 136 137 uint64_t Size; 138 MCInst Inst; 139 const MCDisassembler *DisAsm = DC->getDisAsm(); 140 MCInstPrinter *IP = DC->getIP(); 141 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls())) 142 return 0; 143 144 SmallVector<char, 64> InsnStr; 145 raw_svector_ostream OS(InsnStr); 146 IP->printInst(&Inst, OS); 147 OS.flush(); 148 149 assert(OutStringSize != 0 && "Output buffer cannot be zero size"); 150 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); 151 std::memcpy(OutString, InsnStr.data(), OutputSize); 152 OutString[OutputSize] = '\0'; // Terminate string. 153 154 return Size; 155 } 156