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