LLVM API Documentation

include/llvm-c/Disassembler.h
Go to the documentation of this file.
00001 /*===-- llvm-c/Disassembler.h - Disassembler Public C Interface ---*- C -*-===*\
00002 |*                                                                            *|
00003 |*                     The LLVM Compiler Infrastructure                       *|
00004 |*                                                                            *|
00005 |* This file is distributed under the University of Illinois Open Source      *|
00006 |* License. See LICENSE.TXT for details.                                      *|
00007 |*                                                                            *|
00008 |*===----------------------------------------------------------------------===*|
00009 |*                                                                            *|
00010 |* This header provides a public interface to a disassembler library.         *|
00011 |* LLVM provides an implementation of this interface.                         *|
00012 |*                                                                            *|
00013 \*===----------------------------------------------------------------------===*/
00014 
00015 #ifndef LLVM_C_DISASSEMBLER_H
00016 #define LLVM_C_DISASSEMBLER_H
00017 
00018 #include "llvm/Support/DataTypes.h"
00019 #include <stddef.h>
00020 
00021 /**
00022  * @defgroup LLVMCDisassembler Disassembler
00023  * @ingroup LLVMC
00024  *
00025  * @{
00026  */
00027 
00028 /**
00029  * An opaque reference to a disassembler context.
00030  */
00031 typedef void *LLVMDisasmContextRef;
00032 
00033 /**
00034  * The type for the operand information call back function.  This is called to
00035  * get the symbolic information for an operand of an instruction.  Typically
00036  * this is from the relocation information, symbol table, etc.  That block of
00037  * information is saved when the disassembler context is created and passed to
00038  * the call back in the DisInfo parameter.  The instruction containing operand
00039  * is at the PC parameter.  For some instruction sets, there can be more than
00040  * one operand with symbolic information.  To determine the symbolic operand
00041  * information for each operand, the bytes for the specific operand in the
00042  * instruction are specified by the Offset parameter and its byte widith is the
00043  * size parameter.  For instructions sets with fixed widths and one symbolic
00044  * operand per instruction, the Offset parameter will be zero and Size parameter
00045  * will be the instruction width.  The information is returned in TagBuf and is
00046  * Triple specific with its specific information defined by the value of
00047  * TagType for that Triple.  If symbolic information is returned the function
00048  * returns 1, otherwise it returns 0.
00049  */
00050 typedef int (*LLVMOpInfoCallback)(void *DisInfo, uint64_t PC,
00051                                   uint64_t Offset, uint64_t Size,
00052                                   int TagType, void *TagBuf);
00053 
00054 /**
00055  * The initial support in LLVM MC for the most general form of a relocatable
00056  * expression is "AddSymbol - SubtractSymbol + Offset".  For some Darwin targets
00057  * this full form is encoded in the relocation information so that AddSymbol and
00058  * SubtractSymbol can be link edited independent of each other.  Many other
00059  * platforms only allow a relocatable expression of the form AddSymbol + Offset
00060  * to be encoded.
00061  *
00062  * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
00063  * LLVMOpInfo1.  The value of the relocatable expression for the operand,
00064  * including any PC adjustment, is passed in to the call back in the Value
00065  * field.  The symbolic information about the operand is returned using all
00066  * the fields of the structure with the Offset of the relocatable expression
00067  * returned in the Value field.  It is possible that some symbols in the
00068  * relocatable expression were assembly temporary symbols, for example
00069  * "Ldata - LpicBase + constant", and only the Values of the symbols without
00070  * symbol names are present in the relocation information.  The VariantKind
00071  * type is one of the Target specific #defines below and is used to print
00072  * operands like "_foo@GOT", ":lower16:_foo", etc.
00073  */
00074 struct LLVMOpInfoSymbol1 {
00075   uint64_t Present;  /* 1 if this symbol is present */
00076   const char *Name;  /* symbol name if not NULL */
00077   uint64_t Value;    /* symbol value if name is NULL */
00078 };
00079 
00080 struct LLVMOpInfo1 {
00081   struct LLVMOpInfoSymbol1 AddSymbol;
00082   struct LLVMOpInfoSymbol1 SubtractSymbol;
00083   uint64_t Value;
00084   uint64_t VariantKind;
00085 };
00086 
00087 /**
00088  * The operand VariantKinds for symbolic disassembly.
00089  */
00090 #define LLVMDisassembler_VariantKind_None 0 /* all targets */
00091 
00092 /**
00093  * The ARM target VariantKinds.
00094  */
00095 #define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */
00096 #define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */
00097 
00098 /**
00099  * The ARM64 target VariantKinds.
00100  */
00101 #define LLVMDisassembler_VariantKind_ARM64_PAGE       1 /* @page */
00102 #define LLVMDisassembler_VariantKind_ARM64_PAGEOFF    2 /* @pageoff */
00103 #define LLVMDisassembler_VariantKind_ARM64_GOTPAGE    3 /* @gotpage */
00104 #define LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF 4 /* @gotpageoff */
00105 #define LLVMDisassembler_VariantKind_ARM64_TLVP       5 /* @tvlppage */
00106 #define LLVMDisassembler_VariantKind_ARM64_TLVOFF     6 /* @tvlppageoff */
00107 
00108 /**
00109  * The type for the symbol lookup function.  This may be called by the
00110  * disassembler for things like adding a comment for a PC plus a constant
00111  * offset load instruction to use a symbol name instead of a load address value.
00112  * It is passed the block information is saved when the disassembler context is
00113  * created and the ReferenceValue to look up as a symbol.  If no symbol is found
00114  * for the ReferenceValue NULL is returned.  The ReferenceType of the
00115  * instruction is passed indirectly as is the PC of the instruction in
00116  * ReferencePC.  If the output reference can be determined its type is returned
00117  * indirectly in ReferenceType along with ReferenceName if any, or that is set
00118  * to NULL.
00119  */
00120 typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
00121                                                 uint64_t ReferenceValue,
00122                                                 uint64_t *ReferenceType,
00123                                                 uint64_t ReferencePC,
00124                                                 const char **ReferenceName);
00125 /**
00126  * The reference types on input and output.
00127  */
00128 /* No input reference type or no output reference type. */
00129 #define LLVMDisassembler_ReferenceType_InOut_None 0
00130 
00131 /* The input reference is from a branch instruction. */
00132 #define LLVMDisassembler_ReferenceType_In_Branch 1
00133 /* The input reference is from a PC relative load instruction. */
00134 #define LLVMDisassembler_ReferenceType_In_PCrel_Load 2
00135 
00136 /* The input reference is from an ARM64::ADRP instruction. */
00137 #define LLVMDisassembler_ReferenceType_In_ARM64_ADRP 0x100000001
00138 /* The input reference is from an ARM64::ADDXri instruction. */
00139 #define LLVMDisassembler_ReferenceType_In_ARM64_ADDXri 0x100000002
00140 /* The input reference is from an ARM64::LDRXui instruction. */
00141 #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXui 0x100000003
00142 /* The input reference is from an ARM64::LDRXl instruction. */
00143 #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXl 0x100000004
00144 /* The input reference is from an ARM64::ADR instruction. */
00145 #define LLVMDisassembler_ReferenceType_In_ARM64_ADR 0x100000005
00146 
00147 /* The output reference is to as symbol stub. */
00148 #define LLVMDisassembler_ReferenceType_Out_SymbolStub 1
00149 /* The output reference is to a symbol address in a literal pool. */
00150 #define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr 2
00151 /* The output reference is to a cstring address in a literal pool. */
00152 #define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr 3
00153 
00154 /* The output reference is to a Objective-C CoreFoundation string. */
00155 #define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref 4
00156 /* The output reference is to a Objective-C message. */
00157 #define LLVMDisassembler_ReferenceType_Out_Objc_Message 5
00158 /* The output reference is to a Objective-C message ref. */
00159 #define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref 6
00160 /* The output reference is to a Objective-C selector ref. */
00161 #define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref 7
00162 /* The output reference is to a Objective-C class ref. */
00163 #define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref 8
00164 
00165 /* The output reference is to a C++ symbol name. */
00166 #define LLVMDisassembler_ReferenceType_DeMangled_Name 9
00167 
00168 #ifdef __cplusplus
00169 extern "C" {
00170 #endif /* !defined(__cplusplus) */
00171 
00172 /**
00173  * Create a disassembler for the TripleName.  Symbolic disassembly is supported
00174  * by passing a block of information in the DisInfo parameter and specifying the
00175  * TagType and callback functions as described above.  These can all be passed
00176  * as NULL.  If successful, this returns a disassembler context.  If not, it
00177  * returns NULL. This function is equivalent to calling LLVMCreateDisasmCPU()
00178  * with an empty CPU name.
00179  */
00180 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
00181                                       int TagType, LLVMOpInfoCallback GetOpInfo,
00182                                       LLVMSymbolLookupCallback SymbolLookUp);
00183 
00184 /**
00185  * Create a disassembler for the TripleName and a specific CPU.  Symbolic
00186  * disassembly is supported by passing a block of information in the DisInfo
00187  * parameter and specifying the TagType and callback functions as described
00188  * above.  These can all be passed * as NULL.  If successful, this returns a
00189  * disassembler context.  If not, it returns NULL.
00190  */
00191 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
00192                                          void *DisInfo, int TagType,
00193                                          LLVMOpInfoCallback GetOpInfo,
00194                                          LLVMSymbolLookupCallback SymbolLookUp);
00195 
00196 /**
00197  * Set the disassembler's options.  Returns 1 if it can set the Options and 0
00198  * otherwise.
00199  */
00200 int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options);
00201 
00202 /* The option to produce marked up assembly. */
00203 #define LLVMDisassembler_Option_UseMarkup 1
00204 /* The option to print immediates as hex. */
00205 #define LLVMDisassembler_Option_PrintImmHex 2
00206 /* The option use the other assembler printer variant */
00207 #define LLVMDisassembler_Option_AsmPrinterVariant 4
00208 /* The option to set comment on instructions */
00209 #define LLVMDisassembler_Option_SetInstrComments 8
00210   /* The option to print latency information alongside instructions */
00211 #define LLVMDisassembler_Option_PrintLatency 16
00212 
00213 /**
00214  * Dispose of a disassembler context.
00215  */
00216 void LLVMDisasmDispose(LLVMDisasmContextRef DC);
00217 
00218 /**
00219  * Disassemble a single instruction using the disassembler context specified in
00220  * the parameter DC.  The bytes of the instruction are specified in the
00221  * parameter Bytes, and contains at least BytesSize number of bytes.  The
00222  * instruction is at the address specified by the PC parameter.  If a valid
00223  * instruction can be disassembled, its string is returned indirectly in
00224  * OutString whose size is specified in the parameter OutStringSize.  This
00225  * function returns the number of bytes in the instruction or zero if there was
00226  * no valid instruction.
00227  */
00228 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes,
00229                              uint64_t BytesSize, uint64_t PC,
00230                              char *OutString, size_t OutStringSize);
00231 
00232 /**
00233  * @}
00234  */
00235 
00236 #ifdef __cplusplus
00237 }
00238 #endif /* !defined(__cplusplus) */
00239 
00240 #endif /* !defined(LLVM_C_DISASSEMBLER_H) */