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/MCDisassembler.h" 15 #include "llvm/MC/MCInst.h" 16 #include "llvm/MC/MCInstPrinter.h" 17 #include "llvm/MC/MCContext.h" 18 #include "llvm/Target/TargetRegistry.h" 19 #include "llvm/Target/TargetAsmInfo.h" // FIXME. 20 #include "llvm/Target/TargetMachine.h" // FIXME. 21 #include "llvm/Target/TargetSelect.h" 22 #include "llvm/Support/MemoryObject.h" 23 24 namespace llvm { 25 class Target; 26 } // namespace llvm 27 using namespace llvm; 28 29 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic 30 // disassembly is supported by passing a block of information in the DisInfo 31 // parameter and specifying the TagType and callback functions as described in 32 // the header llvm-c/Disassembler.h . The pointer to the block and the 33 // functions can all be passed as NULL. If successful, this returns a 34 // disassembler context. If not, it returns NULL. 35 // 36 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, 37 int TagType, LLVMOpInfoCallback GetOpInfo, 38 LLVMSymbolLookupCallback SymbolLookUp) { 39 // Initialize targets and assembly printers/parsers. 40 llvm::InitializeAllTargetInfos(); 41 // FIXME: We shouldn't need to initialize the Target(Machine)s. 42 llvm::InitializeAllTargets(); 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->createAsmInfo(TripleName); 54 assert(MAI && "Unable to create target asm info!"); 55 56 // Package up features to be passed to target/subtarget 57 std::string FeaturesStr; 58 59 // FIXME: We shouldn't need to do this (and link in codegen). 60 // When we split this out, we should do it in a way that makes 61 // it straightforward to switch subtargets on the fly. 62 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, FeaturesStr); 63 assert(TM && "Unable to create target machine!"); 64 65 // Get the target assembler info needed to setup the context. 66 const TargetAsmInfo *tai = new TargetAsmInfo(*TM); 67 assert(tai && "Unable to create target assembler!"); 68 69 // Set up the MCContext for creating symbols and MCExpr's. 70 MCContext *Ctx = new MCContext(*MAI, tai); 71 assert(Ctx && "Unable to create MCContext!"); 72 73 // Set up disassembler. 74 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(); 75 assert(DisAsm && "Unable to create disassembler!"); 76 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); 77 78 // Set up the instruction printer. 79 int AsmPrinterVariant = MAI->getAssemblerDialect(); 80 MCInstPrinter *IP = TheTarget->createMCInstPrinter(*TM, AsmPrinterVariant, 81 *MAI); 82 assert(IP && "Unable to create instruction printer!"); 83 84 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, 85 GetOpInfo, SymbolLookUp, 86 TheTarget, MAI, TM, tai, Ctx, 87 DisAsm, IP); 88 assert(DC && "Allocation failure!"); 89 return DC; 90 } 91 92 // 93 // LLVMDisasmDispose() disposes of the disassembler specified by the context. 94 // 95 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 96 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 97 delete DC; 98 } 99 100 namespace { 101 // 102 // The memory object created by LLVMDisasmInstruction(). 103 // 104 class DisasmMemoryObject : public MemoryObject { 105 uint8_t *Bytes; 106 uint64_t Size; 107 uint64_t BasePC; 108 public: 109 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 110 Bytes(bytes), Size(size), BasePC(basePC) {} 111 112 uint64_t getBase() const { return BasePC; } 113 uint64_t getExtent() const { return Size; } 114 115 int readByte(uint64_t Addr, uint8_t *Byte) const { 116 if (Addr - BasePC >= Size) 117 return -1; 118 *Byte = Bytes[Addr - BasePC]; 119 return 0; 120 } 121 }; 122 } // end anonymous namespace 123 124 // 125 // LLVMDisasmInstruction() disassembles a single instruction using the 126 // disassembler context specified in the parameter DC. The bytes of the 127 // instruction are specified in the parameter Bytes, and contains at least 128 // BytesSize number of bytes. The instruction is at the address specified by 129 // the PC parameter. If a valid instruction can be disassembled its string is 130 // returned indirectly in OutString which whos size is specified in the 131 // parameter OutStringSize. This function returns the number of bytes in the 132 // instruction or zero if there was no valid instruction. If this function 133 // returns zero the caller will have to pick how many bytes they want to step 134 // over by printing a .byte, .long etc. to continue. 135 // 136 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 137 uint64_t BytesSize, uint64_t PC, char *OutString, 138 size_t OutStringSize){ 139 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 140 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 141 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 142 143 uint64_t Size; 144 MCInst Inst; 145 const MCDisassembler *DisAsm = DC->getDisAsm(); 146 MCInstPrinter *IP = DC->getIP(); 147 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls())) 148 return 0; 149 150 std::string InsnStr; 151 raw_string_ostream OS(InsnStr); 152 IP->printInst(&Inst, OS); 153 OS.flush(); 154 155 assert(OutStringSize != 0 && "Output buffer cannot be zero size"); 156 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); 157 std::memcpy(OutString, InsnStr.data(), OutputSize); 158 OutString[OutputSize] = '\0'; // Terminate string. 159 160 return Size; 161 } 162