| Nick Lewycky | 2e2d75f | 2011-09-01 21:09:04 +0000 | [diff] [blame] | 1 | //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===// |
| Kevin Enderby | f3070dc | 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 | 0388fb0 | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 9 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 10 | #include "Disassembler.h" |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 11 | #include "llvm-c/Disassembler.h" |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
| Evan Cheng | 345b6b4 | 2011-07-20 06:54:19 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCDisassembler.h" |
| 15 | #include "llvm/MC/MCInst.h" |
| 16 | #include "llvm/MC/MCInstPrinter.h" |
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCInstrInfo.h" |
| Evan Cheng | 345b6b4 | 2011-07-20 06:54:19 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCRegisterInfo.h" |
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSubtargetInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryObject.h" |
| Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
| Kevin Enderby | f3070dc | 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 | f3070dc | 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 | 0388fb0 | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 31 | // parameter and specifying the TagType and callback functions as described in |
| Kevin Enderby | f3070dc | 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 | 0388fb0 | 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 | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 35 | // |
| Jim Grosbach | 0ca9d5b | 2012-12-07 23:53:27 +0000 | [diff] [blame] | 36 | LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, |
| 37 | void *DisInfo, int TagType, |
| 38 | LLVMOpInfoCallback GetOpInfo, |
| 39 | LLVMSymbolLookupCallback SymbolLookUp){ |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 40 | // Get the target. |
| 41 | std::string Error; |
| Jim Grosbach | 0ca9d5b | 2012-12-07 23:53:27 +0000 | [diff] [blame] | 42 | const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); |
| Kevin Enderby | 64d9345 | 2013-05-23 00:32:34 +0000 | [diff] [blame^] | 43 | if (!TheTarget) |
| 44 | return 0; |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 45 | |
| Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 46 | const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple); |
| 47 | if (!MRI) |
| 48 | return 0; |
| 49 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 50 | // Get the assembler info needed to setup the MCContext. |
| Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 51 | const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 52 | if (!MAI) |
| 53 | return 0; |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 54 | |
| Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 55 | const MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 56 | if (!MII) |
| 57 | return 0; |
| Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 58 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 59 | // Package up features to be passed to target/subtarget |
| 60 | std::string FeaturesStr; |
| 61 | |
| Jim Grosbach | 0ca9d5b | 2012-12-07 23:53:27 +0000 | [diff] [blame] | 62 | const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU, |
| James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 63 | FeaturesStr); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 64 | if (!STI) |
| 65 | return 0; |
| James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 66 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 67 | // Set up the MCContext for creating symbols and MCExpr's. |
| Evan Cheng | bbf3b0d | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 68 | MCContext *Ctx = new MCContext(*MAI, *MRI, 0); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 69 | if (!Ctx) |
| 70 | return 0; |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 71 | |
| 72 | // Set up disassembler. |
| James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 73 | MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 74 | if (!DisAsm) |
| 75 | return 0; |
| Kevin Enderby | 5dcda64 | 2011-10-04 22:44:48 +0000 | [diff] [blame] | 76 | DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx); |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 77 | |
| 78 | // Set up the instruction printer. |
| 79 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
| Evan Cheng | ab37af9 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 80 | MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, |
| Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 81 | *MAI, *MII, *MRI, *STI); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 82 | if (!IP) |
| 83 | return 0; |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 84 | |
| Jim Grosbach | 0ca9d5b | 2012-12-07 23:53:27 +0000 | [diff] [blame] | 85 | LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType, |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 86 | GetOpInfo, SymbolLookUp, |
| Evan Cheng | bbf3b0d | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 87 | TheTarget, MAI, MRI, |
| Sean Callanan | e804b5b | 2012-04-06 18:21:09 +0000 | [diff] [blame] | 88 | STI, MII, Ctx, DisAsm, IP); |
| Kevin Enderby | f536c6a | 2013-03-12 18:12:17 +0000 | [diff] [blame] | 89 | if (!DC) |
| 90 | return 0; |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 91 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 92 | return DC; |
| 93 | } |
| 94 | |
| Jim Grosbach | 0ca9d5b | 2012-12-07 23:53:27 +0000 | [diff] [blame] | 95 | LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo, |
| 96 | int TagType, LLVMOpInfoCallback GetOpInfo, |
| 97 | LLVMSymbolLookupCallback SymbolLookUp) { |
| 98 | return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo, |
| 99 | SymbolLookUp); |
| 100 | } |
| 101 | |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 102 | // |
| 103 | // LLVMDisasmDispose() disposes of the disassembler specified by the context. |
| 104 | // |
| 105 | void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ |
| 106 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 107 | delete DC; |
| 108 | } |
| 109 | |
| 110 | namespace { |
| 111 | // |
| 112 | // The memory object created by LLVMDisasmInstruction(). |
| 113 | // |
| 114 | class DisasmMemoryObject : public MemoryObject { |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 115 | uint8_t *Bytes; |
| 116 | uint64_t Size; |
| 117 | uint64_t BasePC; |
| 118 | public: |
| 119 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 120 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 121 | |
| 122 | uint64_t getBase() const { return BasePC; } |
| Derek Schuff | 56b662c | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 123 | uint64_t getExtent() const { return Size; } |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 124 | |
| Derek Schuff | 56b662c | 2012-02-29 01:09:06 +0000 | [diff] [blame] | 125 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 126 | if (Addr - BasePC >= Size) |
| 127 | return -1; |
| 128 | *Byte = Bytes[Addr - BasePC]; |
| 129 | return 0; |
| 130 | } |
| 131 | }; |
| Chris Lattner | 0388fb0 | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 132 | } // end anonymous namespace |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 133 | |
| 134 | // |
| Benjamin Kramer | 26e7768 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 135 | // LLVMDisasmInstruction() disassembles a single instruction using the |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 136 | // disassembler context specified in the parameter DC. The bytes of the |
| Benjamin Kramer | 26e7768 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 137 | // instruction are specified in the parameter Bytes, and contains at least |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 138 | // BytesSize number of bytes. The instruction is at the address specified by |
| 139 | // the PC parameter. If a valid instruction can be disassembled its string is |
| 140 | // returned indirectly in OutString which whos size is specified in the |
| 141 | // parameter OutStringSize. This function returns the number of bytes in the |
| 142 | // instruction or zero if there was no valid instruction. If this function |
| 143 | // returns zero the caller will have to pick how many bytes they want to step |
| 144 | // over by printing a .byte, .long etc. to continue. |
| 145 | // |
| 146 | size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, |
| 147 | uint64_t BytesSize, uint64_t PC, char *OutString, |
| 148 | size_t OutStringSize){ |
| 149 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 150 | // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. |
| 151 | DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); |
| 152 | |
| 153 | uint64_t Size; |
| 154 | MCInst Inst; |
| 155 | const MCDisassembler *DisAsm = DC->getDisAsm(); |
| 156 | MCInstPrinter *IP = DC->getIP(); |
| James Molloy | 5ada2a7 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 157 | MCDisassembler::DecodeStatus S; |
| 158 | S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, |
| Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 159 | /*REMOVE*/ nulls(), DC->CommentStream); |
| James Molloy | 5ada2a7 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 160 | switch (S) { |
| 161 | case MCDisassembler::Fail: |
| 162 | case MCDisassembler::SoftFail: |
| James Molloy | db4ce60 | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 163 | // FIXME: Do something different for soft failure modes? |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 164 | return 0; |
| James Molloy | 5ada2a7 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 165 | |
| 166 | case MCDisassembler::Success: { |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 167 | DC->CommentStream.flush(); |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 168 | StringRef Comments = DC->CommentsToEmit.str(); |
| 169 | |
| Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 170 | SmallVector<char, 64> InsnStr; |
| 171 | raw_svector_ostream OS(InsnStr); |
| 172 | IP->printInst(&Inst, OS, Comments); |
| 173 | OS.flush(); |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 174 | |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 175 | // Tell the comment stream that the vector changed underneath it. |
| Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 176 | DC->CommentsToEmit.clear(); |
| Owen Anderson | 233f130 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 177 | DC->CommentStream.resync(); |
| 178 | |
| James Molloy | 5ada2a7 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 179 | assert(OutStringSize != 0 && "Output buffer cannot be zero size"); |
| 180 | size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); |
| 181 | std::memcpy(OutString, InsnStr.data(), OutputSize); |
| 182 | OutString[OutputSize] = '\0'; // Terminate string. |
| 183 | |
| 184 | return Size; |
| James Molloy | db4ce60 | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 185 | } |
| James Molloy | 5ada2a7 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 186 | } |
| David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 187 | llvm_unreachable("Invalid DecodeStatus!"); |
| Kevin Enderby | f3070dc | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 188 | } |
| Kevin Enderby | 62183c4 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 189 | |
| 190 | // |
| 191 | // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it |
| 192 | // can set all the Options and 0 otherwise. |
| 193 | // |
| 194 | int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ |
| 195 | if (Options & LLVMDisassembler_Option_UseMarkup){ |
| 196 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 197 | MCInstPrinter *IP = DC->getIP(); |
| 198 | IP->setUseMarkup(1); |
| 199 | Options &= ~LLVMDisassembler_Option_UseMarkup; |
| 200 | } |
| Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 201 | if (Options & LLVMDisassembler_Option_PrintImmHex){ |
| 202 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 203 | MCInstPrinter *IP = DC->getIP(); |
| 204 | IP->setPrintImmHex(1); |
| 205 | Options &= ~LLVMDisassembler_Option_PrintImmHex; |
| 206 | } |
| Kevin Enderby | 85cf531 | 2012-12-18 23:47:28 +0000 | [diff] [blame] | 207 | if (Options & LLVMDisassembler_Option_AsmPrinterVariant){ |
| 208 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 209 | // Try to set up the new instruction printer. |
| 210 | const MCAsmInfo *MAI = DC->getAsmInfo(); |
| 211 | const MCInstrInfo *MII = DC->getInstrInfo(); |
| 212 | const MCRegisterInfo *MRI = DC->getRegisterInfo(); |
| 213 | const MCSubtargetInfo *STI = DC->getSubtargetInfo(); |
| 214 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
| 215 | AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0; |
| 216 | MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter( |
| 217 | AsmPrinterVariant, *MAI, *MII, *MRI, *STI); |
| 218 | if (IP) { |
| 219 | DC->setIP(IP); |
| 220 | Options &= ~LLVMDisassembler_Option_AsmPrinterVariant; |
| 221 | } |
| 222 | } |
| Kevin Enderby | 62183c4 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 223 | return (Options == 0); |
| 224 | } |