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 successfull 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 const MCDisassembler *DisAsm = TheTarget->createMCDisassembler(); 81 assert(DisAsm && "Unable to create disassembler!"); 82 83 // Set up the instruction printer. 84 int AsmPrinterVariant = MAI->getAssemblerDialect(); 85 MCInstPrinter *IP = TheTarget->createMCInstPrinter(*TM, AsmPrinterVariant, 86 *MAI); 87 assert(IP && "Unable to create instruction printer!"); 88 89 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, 90 GetOpInfo, SymbolLookUp, 91 TheTarget, MAI, TM, tai, Ctx, 92 DisAsm, IP); 93 assert(DC && "Allocation failure!"); 94 return DC; 95 } 96 97 // 98 // LLVMDisasmDispose() disposes of the disassembler specified by the context. 99 // 100 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ 101 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 102 delete DC; 103 } 104 105 namespace { 106 // 107 // The memory object created by LLVMDisasmInstruction(). 108 // 109 class DisasmMemoryObject : public MemoryObject { 110 private: 111 uint8_t *Bytes; 112 uint64_t Size; 113 uint64_t BasePC; 114 public: 115 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : 116 Bytes(bytes), Size(size), BasePC(basePC) {} 117 118 uint64_t getBase() const { return BasePC; } 119 uint64_t getExtent() const { return Size; } 120 121 int readByte(uint64_t Addr, uint8_t *Byte) const { 122 if (Addr - BasePC >= Size) 123 return -1; 124 *Byte = Bytes[Addr - BasePC]; 125 return 0; 126 } 127 }; 128 } // namespace 129 130 // 131 // LLVMDisasmInstruction() disassmbles a single instruction using the 132 // disassembler context specified in the parameter DC. The bytes of the 133 // instuction are specified in the parameter Bytes, and contains at least 134 // BytesSize number of bytes. The instruction is at the address specified by 135 // the PC parameter. If a valid instruction can be disassembled its string is 136 // returned indirectly in OutString which whos size is specified in the 137 // parameter OutStringSize. This function returns the number of bytes in the 138 // instruction or zero if there was no valid instruction. If this function 139 // returns zero the caller will have to pick how many bytes they want to step 140 // over by printing a .byte, .long etc. to continue. 141 // 142 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, 143 uint64_t BytesSize, uint64_t PC, char *OutString, 144 size_t OutStringSize){ 145 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; 146 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. 147 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); 148 149 uint64_t Size; 150 MCInst Inst; 151 const MCDisassembler *DisAsm = DC->getDisAsm(); 152 MCInstPrinter *IP = DC->getIP(); 153 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls())) 154 return 0; 155 156 std::string InsnStr; 157 raw_string_ostream OS(InsnStr); 158 raw_ostream &Out = OS; 159 IP->printInst(&Inst, Out); 160 161 std::string p; 162 p = OS.str(); 163 snprintf(OutString, OutStringSize, "%s", p.c_str()); 164 return Size; 165 } 166 167 #ifdef __cplusplus 168 } 169 #endif // __cplusplus 170