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