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