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