Nick Lewycky | 833a003 | 2011-09-01 21:09:04 +0000 | [diff] [blame] | 1 | //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===// |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 9 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 10 | #include "Disassembler.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 11 | #include "llvm-c/Disassembler.h" |
| 12 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Evan Cheng | 4c81648 | 2011-07-20 06:54:19 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCDisassembler.h" |
| 16 | #include "llvm/MC/MCInst.h" |
| 17 | #include "llvm/MC/MCInstPrinter.h" |
Evan Cheng | 4c81648 | 2011-07-20 06:54:19 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCRegisterInfo.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryObject.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetRegistry.h" |
Kevin Enderby | fc0d740 | 2012-02-17 19:18:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetSelect.h" |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | class Target; |
| 26 | } // namespace llvm |
| 27 | using namespace llvm; |
| 28 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 29 | // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic |
| 30 | // disassembly is supported by passing a block of information in the DisInfo |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 31 | // parameter and specifying the TagType and callback functions as described in |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 32 | // the header llvm-c/Disassembler.h . The pointer to the block and the |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 33 | // functions can all be passed as NULL. If successful, this returns a |
| 34 | // disassembler context. If not, it returns NULL. |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 35 | // |
| 36 | LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, |
| 37 | int TagType, LLVMOpInfoCallback GetOpInfo, |
| 38 | LLVMSymbolLookupCallback SymbolLookUp) { |
Kevin Enderby | fc0d740 | 2012-02-17 19:18:29 +0000 | [diff] [blame] | 39 | // Initialize targets and assembly printers/parsers. |
| 40 | // FIXME: Clients are responsible for initializing the targets. And this |
| 41 | // would be done by calling routines in "llvm-c/Target.h" which are static |
| 42 | // line functions. But the current use of LLVMCreateDisasm() is to dynamically |
Kevin Enderby | d9165eb | 2012-02-17 19:26:00 +0000 | [diff] [blame] | 43 | // load libLTO with dlopen() and then lookup the symbols using dlsym(). |
Kevin Enderby | fc0d740 | 2012-02-17 19:18:29 +0000 | [diff] [blame] | 44 | // And since these initialize routines are static that does not work which |
| 45 | // is why the call to them in this 'C' library API was added back. |
| 46 | llvm::InitializeAllTargetInfos(); |
| 47 | llvm::InitializeAllTargetMCs(); |
| 48 | llvm::InitializeAllAsmParsers(); |
| 49 | llvm::InitializeAllDisassemblers(); |
| 50 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 51 | // Get the target. |
| 52 | std::string Error; |
| 53 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 54 | assert(TheTarget && "Unable to create target!"); |
| 55 | |
| 56 | // Get the assembler info needed to setup the MCContext. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 57 | const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 58 | assert(MAI && "Unable to create target asm info!"); |
| 59 | |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 60 | const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); |
| 61 | assert(MRI && "Unable to create target register info!"); |
| 62 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 63 | // Package up features to be passed to target/subtarget |
| 64 | std::string FeaturesStr; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 65 | std::string CPU; |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 66 | |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 67 | const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU, |
| 68 | FeaturesStr); |
| 69 | assert(STI && "Unable to create subtarget info!"); |
| 70 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 71 | // Set up the MCContext for creating symbols and MCExpr's. |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 72 | MCContext *Ctx = new MCContext(*MAI, *MRI, 0); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 73 | assert(Ctx && "Unable to create MCContext!"); |
| 74 | |
| 75 | // Set up disassembler. |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 76 | MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 77 | assert(DisAsm && "Unable to create disassembler!"); |
Kevin Enderby | 9e5887b | 2011-10-04 22:44:48 +0000 | [diff] [blame] | 78 | DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 79 | |
| 80 | // Set up the instruction printer. |
| 81 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 82 | MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, |
Jim Grosbach | c6449b6 | 2012-03-05 19:33:20 +0000 | [diff] [blame^] | 83 | *MAI, *MRI, *STI); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 84 | assert(IP && "Unable to create instruction printer!"); |
| 85 | |
| 86 | LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, |
| 87 | GetOpInfo, SymbolLookUp, |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 88 | TheTarget, MAI, MRI, |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 89 | Ctx, DisAsm, IP); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 90 | assert(DC && "Allocation failure!"); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 91 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 92 | return DC; |
| 93 | } |
| 94 | |
| 95 | // |
| 96 | // LLVMDisasmDispose() disposes of the disassembler specified by the context. |
| 97 | // |
| 98 | void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ |
| 99 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 100 | delete DC; |
| 101 | } |
| 102 | |
| 103 | namespace { |
| 104 | // |
| 105 | // The memory object created by LLVMDisasmInstruction(). |
| 106 | // |
| 107 | class DisasmMemoryObject : public MemoryObject { |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 108 | uint8_t *Bytes; |
| 109 | uint64_t Size; |
| 110 | uint64_t BasePC; |
| 111 | public: |
| 112 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 113 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 114 | |
| 115 | uint64_t getBase() const { return BasePC; } |
Derek Schuff | adef06a | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 116 | uint64_t getExtent() const { return Size; } |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 117 | |
Derek Schuff | adef06a | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 118 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 119 | if (Addr - BasePC >= Size) |
| 120 | return -1; |
| 121 | *Byte = Bytes[Addr - BasePC]; |
| 122 | return 0; |
| 123 | } |
| 124 | }; |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 125 | } // end anonymous namespace |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 126 | |
| 127 | // |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 128 | // LLVMDisasmInstruction() disassembles a single instruction using the |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 129 | // disassembler context specified in the parameter DC. The bytes of the |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 130 | // instruction are specified in the parameter Bytes, and contains at least |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 131 | // BytesSize number of bytes. The instruction is at the address specified by |
| 132 | // the PC parameter. If a valid instruction can be disassembled its string is |
| 133 | // returned indirectly in OutString which whos size is specified in the |
| 134 | // parameter OutStringSize. This function returns the number of bytes in the |
| 135 | // instruction or zero if there was no valid instruction. If this function |
| 136 | // returns zero the caller will have to pick how many bytes they want to step |
| 137 | // over by printing a .byte, .long etc. to continue. |
| 138 | // |
| 139 | size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, |
| 140 | uint64_t BytesSize, uint64_t PC, char *OutString, |
| 141 | size_t OutStringSize){ |
| 142 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 143 | // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. |
| 144 | DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); |
| 145 | |
| 146 | uint64_t Size; |
| 147 | MCInst Inst; |
| 148 | const MCDisassembler *DisAsm = DC->getDisAsm(); |
| 149 | MCInstPrinter *IP = DC->getIP(); |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 150 | MCDisassembler::DecodeStatus S; |
| 151 | S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 152 | /*REMOVE*/ nulls(), DC->CommentStream); |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 153 | switch (S) { |
| 154 | case MCDisassembler::Fail: |
| 155 | case MCDisassembler::SoftFail: |
James Molloy | c047dca | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 156 | // FIXME: Do something different for soft failure modes? |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 157 | return 0; |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 158 | |
| 159 | case MCDisassembler::Success: { |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 160 | DC->CommentStream.flush(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 161 | StringRef Comments = DC->CommentsToEmit.str(); |
| 162 | |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 163 | SmallVector<char, 64> InsnStr; |
| 164 | raw_svector_ostream OS(InsnStr); |
| 165 | IP->printInst(&Inst, OS, Comments); |
| 166 | OS.flush(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 167 | |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 168 | // Tell the comment stream that the vector changed underneath it. |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 169 | DC->CommentsToEmit.clear(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 170 | DC->CommentStream.resync(); |
| 171 | |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 172 | assert(OutStringSize != 0 && "Output buffer cannot be zero size"); |
| 173 | size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); |
| 174 | std::memcpy(OutString, InsnStr.data(), OutputSize); |
| 175 | OutString[OutputSize] = '\0'; // Terminate string. |
| 176 | |
| 177 | return Size; |
James Molloy | c047dca | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 178 | } |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 179 | } |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 180 | llvm_unreachable("Invalid DecodeStatus!"); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 181 | } |