1*9880d681SAndroid Build Coastguard Worker /*===-- llvm-c/Disassembler.h - Disassembler Public C Interface ---*- C -*-===*\ 2*9880d681SAndroid Build Coastguard Worker |* *| 3*9880d681SAndroid Build Coastguard Worker |* The LLVM Compiler Infrastructure *| 4*9880d681SAndroid Build Coastguard Worker |* *| 5*9880d681SAndroid Build Coastguard Worker |* This file is distributed under the University of Illinois Open Source *| 6*9880d681SAndroid Build Coastguard Worker |* License. See LICENSE.TXT for details. *| 7*9880d681SAndroid Build Coastguard Worker |* *| 8*9880d681SAndroid Build Coastguard Worker |*===----------------------------------------------------------------------===*| 9*9880d681SAndroid Build Coastguard Worker |* *| 10*9880d681SAndroid Build Coastguard Worker |* This header provides a public interface to a disassembler library. *| 11*9880d681SAndroid Build Coastguard Worker |* LLVM provides an implementation of this interface. *| 12*9880d681SAndroid Build Coastguard Worker |* *| 13*9880d681SAndroid Build Coastguard Worker \*===----------------------------------------------------------------------===*/ 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_C_DISASSEMBLER_H 16*9880d681SAndroid Build Coastguard Worker #define LLVM_C_DISASSEMBLER_H 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/DataTypes.h" 19*9880d681SAndroid Build Coastguard Worker #ifdef __cplusplus 20*9880d681SAndroid Build Coastguard Worker #include <cstddef> 21*9880d681SAndroid Build Coastguard Worker #else 22*9880d681SAndroid Build Coastguard Worker #include <stddef.h> 23*9880d681SAndroid Build Coastguard Worker #endif 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard Worker /** 26*9880d681SAndroid Build Coastguard Worker * @defgroup LLVMCDisassembler Disassembler 27*9880d681SAndroid Build Coastguard Worker * @ingroup LLVMC 28*9880d681SAndroid Build Coastguard Worker * 29*9880d681SAndroid Build Coastguard Worker * @{ 30*9880d681SAndroid Build Coastguard Worker */ 31*9880d681SAndroid Build Coastguard Worker 32*9880d681SAndroid Build Coastguard Worker /** 33*9880d681SAndroid Build Coastguard Worker * An opaque reference to a disassembler context. 34*9880d681SAndroid Build Coastguard Worker */ 35*9880d681SAndroid Build Coastguard Worker typedef void *LLVMDisasmContextRef; 36*9880d681SAndroid Build Coastguard Worker 37*9880d681SAndroid Build Coastguard Worker /** 38*9880d681SAndroid Build Coastguard Worker * The type for the operand information call back function. This is called to 39*9880d681SAndroid Build Coastguard Worker * get the symbolic information for an operand of an instruction. Typically 40*9880d681SAndroid Build Coastguard Worker * this is from the relocation information, symbol table, etc. That block of 41*9880d681SAndroid Build Coastguard Worker * information is saved when the disassembler context is created and passed to 42*9880d681SAndroid Build Coastguard Worker * the call back in the DisInfo parameter. The instruction containing operand 43*9880d681SAndroid Build Coastguard Worker * is at the PC parameter. For some instruction sets, there can be more than 44*9880d681SAndroid Build Coastguard Worker * one operand with symbolic information. To determine the symbolic operand 45*9880d681SAndroid Build Coastguard Worker * information for each operand, the bytes for the specific operand in the 46*9880d681SAndroid Build Coastguard Worker * instruction are specified by the Offset parameter and its byte widith is the 47*9880d681SAndroid Build Coastguard Worker * size parameter. For instructions sets with fixed widths and one symbolic 48*9880d681SAndroid Build Coastguard Worker * operand per instruction, the Offset parameter will be zero and Size parameter 49*9880d681SAndroid Build Coastguard Worker * will be the instruction width. The information is returned in TagBuf and is 50*9880d681SAndroid Build Coastguard Worker * Triple specific with its specific information defined by the value of 51*9880d681SAndroid Build Coastguard Worker * TagType for that Triple. If symbolic information is returned the function 52*9880d681SAndroid Build Coastguard Worker * returns 1, otherwise it returns 0. 53*9880d681SAndroid Build Coastguard Worker */ 54*9880d681SAndroid Build Coastguard Worker typedef int (*LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, 55*9880d681SAndroid Build Coastguard Worker uint64_t Offset, uint64_t Size, 56*9880d681SAndroid Build Coastguard Worker int TagType, void *TagBuf); 57*9880d681SAndroid Build Coastguard Worker 58*9880d681SAndroid Build Coastguard Worker /** 59*9880d681SAndroid Build Coastguard Worker * The initial support in LLVM MC for the most general form of a relocatable 60*9880d681SAndroid Build Coastguard Worker * expression is "AddSymbol - SubtractSymbol + Offset". For some Darwin targets 61*9880d681SAndroid Build Coastguard Worker * this full form is encoded in the relocation information so that AddSymbol and 62*9880d681SAndroid Build Coastguard Worker * SubtractSymbol can be link edited independent of each other. Many other 63*9880d681SAndroid Build Coastguard Worker * platforms only allow a relocatable expression of the form AddSymbol + Offset 64*9880d681SAndroid Build Coastguard Worker * to be encoded. 65*9880d681SAndroid Build Coastguard Worker * 66*9880d681SAndroid Build Coastguard Worker * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct 67*9880d681SAndroid Build Coastguard Worker * LLVMOpInfo1. The value of the relocatable expression for the operand, 68*9880d681SAndroid Build Coastguard Worker * including any PC adjustment, is passed in to the call back in the Value 69*9880d681SAndroid Build Coastguard Worker * field. The symbolic information about the operand is returned using all 70*9880d681SAndroid Build Coastguard Worker * the fields of the structure with the Offset of the relocatable expression 71*9880d681SAndroid Build Coastguard Worker * returned in the Value field. It is possible that some symbols in the 72*9880d681SAndroid Build Coastguard Worker * relocatable expression were assembly temporary symbols, for example 73*9880d681SAndroid Build Coastguard Worker * "Ldata - LpicBase + constant", and only the Values of the symbols without 74*9880d681SAndroid Build Coastguard Worker * symbol names are present in the relocation information. The VariantKind 75*9880d681SAndroid Build Coastguard Worker * type is one of the Target specific #defines below and is used to print 76*9880d681SAndroid Build Coastguard Worker * operands like "_foo@GOT", ":lower16:_foo", etc. 77*9880d681SAndroid Build Coastguard Worker */ 78*9880d681SAndroid Build Coastguard Worker struct LLVMOpInfoSymbol1 { 79*9880d681SAndroid Build Coastguard Worker uint64_t Present; /* 1 if this symbol is present */ 80*9880d681SAndroid Build Coastguard Worker const char *Name; /* symbol name if not NULL */ 81*9880d681SAndroid Build Coastguard Worker uint64_t Value; /* symbol value if name is NULL */ 82*9880d681SAndroid Build Coastguard Worker }; 83*9880d681SAndroid Build Coastguard Worker 84*9880d681SAndroid Build Coastguard Worker struct LLVMOpInfo1 { 85*9880d681SAndroid Build Coastguard Worker struct LLVMOpInfoSymbol1 AddSymbol; 86*9880d681SAndroid Build Coastguard Worker struct LLVMOpInfoSymbol1 SubtractSymbol; 87*9880d681SAndroid Build Coastguard Worker uint64_t Value; 88*9880d681SAndroid Build Coastguard Worker uint64_t VariantKind; 89*9880d681SAndroid Build Coastguard Worker }; 90*9880d681SAndroid Build Coastguard Worker 91*9880d681SAndroid Build Coastguard Worker /** 92*9880d681SAndroid Build Coastguard Worker * The operand VariantKinds for symbolic disassembly. 93*9880d681SAndroid Build Coastguard Worker */ 94*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_None 0 /* all targets */ 95*9880d681SAndroid Build Coastguard Worker 96*9880d681SAndroid Build Coastguard Worker /** 97*9880d681SAndroid Build Coastguard Worker * The ARM target VariantKinds. 98*9880d681SAndroid Build Coastguard Worker */ 99*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */ 100*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */ 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker /** 103*9880d681SAndroid Build Coastguard Worker * The ARM64 target VariantKinds. 104*9880d681SAndroid Build Coastguard Worker */ 105*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_PAGE 1 /* @page */ 106*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_PAGEOFF 2 /* @pageoff */ 107*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_GOTPAGE 3 /* @gotpage */ 108*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF 4 /* @gotpageoff */ 109*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_TLVP 5 /* @tvlppage */ 110*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_VariantKind_ARM64_TLVOFF 6 /* @tvlppageoff */ 111*9880d681SAndroid Build Coastguard Worker 112*9880d681SAndroid Build Coastguard Worker /** 113*9880d681SAndroid Build Coastguard Worker * The type for the symbol lookup function. This may be called by the 114*9880d681SAndroid Build Coastguard Worker * disassembler for things like adding a comment for a PC plus a constant 115*9880d681SAndroid Build Coastguard Worker * offset load instruction to use a symbol name instead of a load address value. 116*9880d681SAndroid Build Coastguard Worker * It is passed the block information is saved when the disassembler context is 117*9880d681SAndroid Build Coastguard Worker * created and the ReferenceValue to look up as a symbol. If no symbol is found 118*9880d681SAndroid Build Coastguard Worker * for the ReferenceValue NULL is returned. The ReferenceType of the 119*9880d681SAndroid Build Coastguard Worker * instruction is passed indirectly as is the PC of the instruction in 120*9880d681SAndroid Build Coastguard Worker * ReferencePC. If the output reference can be determined its type is returned 121*9880d681SAndroid Build Coastguard Worker * indirectly in ReferenceType along with ReferenceName if any, or that is set 122*9880d681SAndroid Build Coastguard Worker * to NULL. 123*9880d681SAndroid Build Coastguard Worker */ 124*9880d681SAndroid Build Coastguard Worker typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo, 125*9880d681SAndroid Build Coastguard Worker uint64_t ReferenceValue, 126*9880d681SAndroid Build Coastguard Worker uint64_t *ReferenceType, 127*9880d681SAndroid Build Coastguard Worker uint64_t ReferencePC, 128*9880d681SAndroid Build Coastguard Worker const char **ReferenceName); 129*9880d681SAndroid Build Coastguard Worker /** 130*9880d681SAndroid Build Coastguard Worker * The reference types on input and output. 131*9880d681SAndroid Build Coastguard Worker */ 132*9880d681SAndroid Build Coastguard Worker /* No input reference type or no output reference type. */ 133*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_InOut_None 0 134*9880d681SAndroid Build Coastguard Worker 135*9880d681SAndroid Build Coastguard Worker /* The input reference is from a branch instruction. */ 136*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_Branch 1 137*9880d681SAndroid Build Coastguard Worker /* The input reference is from a PC relative load instruction. */ 138*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_PCrel_Load 2 139*9880d681SAndroid Build Coastguard Worker 140*9880d681SAndroid Build Coastguard Worker /* The input reference is from an ARM64::ADRP instruction. */ 141*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_ARM64_ADRP 0x100000001 142*9880d681SAndroid Build Coastguard Worker /* The input reference is from an ARM64::ADDXri instruction. */ 143*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_ARM64_ADDXri 0x100000002 144*9880d681SAndroid Build Coastguard Worker /* The input reference is from an ARM64::LDRXui instruction. */ 145*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXui 0x100000003 146*9880d681SAndroid Build Coastguard Worker /* The input reference is from an ARM64::LDRXl instruction. */ 147*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXl 0x100000004 148*9880d681SAndroid Build Coastguard Worker /* The input reference is from an ARM64::ADR instruction. */ 149*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_In_ARM64_ADR 0x100000005 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard Worker /* The output reference is to as symbol stub. */ 152*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_SymbolStub 1 153*9880d681SAndroid Build Coastguard Worker /* The output reference is to a symbol address in a literal pool. */ 154*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr 2 155*9880d681SAndroid Build Coastguard Worker /* The output reference is to a cstring address in a literal pool. */ 156*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr 3 157*9880d681SAndroid Build Coastguard Worker 158*9880d681SAndroid Build Coastguard Worker /* The output reference is to a Objective-C CoreFoundation string. */ 159*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref 4 160*9880d681SAndroid Build Coastguard Worker /* The output reference is to a Objective-C message. */ 161*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_Objc_Message 5 162*9880d681SAndroid Build Coastguard Worker /* The output reference is to a Objective-C message ref. */ 163*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref 6 164*9880d681SAndroid Build Coastguard Worker /* The output reference is to a Objective-C selector ref. */ 165*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref 7 166*9880d681SAndroid Build Coastguard Worker /* The output reference is to a Objective-C class ref. */ 167*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref 8 168*9880d681SAndroid Build Coastguard Worker 169*9880d681SAndroid Build Coastguard Worker /* The output reference is to a C++ symbol name. */ 170*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_ReferenceType_DeMangled_Name 9 171*9880d681SAndroid Build Coastguard Worker 172*9880d681SAndroid Build Coastguard Worker #ifdef __cplusplus 173*9880d681SAndroid Build Coastguard Worker extern "C" { 174*9880d681SAndroid Build Coastguard Worker #endif /* !defined(__cplusplus) */ 175*9880d681SAndroid Build Coastguard Worker 176*9880d681SAndroid Build Coastguard Worker /** 177*9880d681SAndroid Build Coastguard Worker * Create a disassembler for the TripleName. Symbolic disassembly is supported 178*9880d681SAndroid Build Coastguard Worker * by passing a block of information in the DisInfo parameter and specifying the 179*9880d681SAndroid Build Coastguard Worker * TagType and callback functions as described above. These can all be passed 180*9880d681SAndroid Build Coastguard Worker * as NULL. If successful, this returns a disassembler context. If not, it 181*9880d681SAndroid Build Coastguard Worker * returns NULL. This function is equivalent to calling 182*9880d681SAndroid Build Coastguard Worker * LLVMCreateDisasmCPUFeatures() with an empty CPU name and feature set. 183*9880d681SAndroid Build Coastguard Worker */ 184*9880d681SAndroid Build Coastguard Worker LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, 185*9880d681SAndroid Build Coastguard Worker int TagType, LLVMOpInfoCallback GetOpInfo, 186*9880d681SAndroid Build Coastguard Worker LLVMSymbolLookupCallback SymbolLookUp); 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard Worker /** 189*9880d681SAndroid Build Coastguard Worker * Create a disassembler for the TripleName and a specific CPU. Symbolic 190*9880d681SAndroid Build Coastguard Worker * disassembly is supported by passing a block of information in the DisInfo 191*9880d681SAndroid Build Coastguard Worker * parameter and specifying the TagType and callback functions as described 192*9880d681SAndroid Build Coastguard Worker * above. These can all be passed * as NULL. If successful, this returns a 193*9880d681SAndroid Build Coastguard Worker * disassembler context. If not, it returns NULL. This function is equivalent 194*9880d681SAndroid Build Coastguard Worker * to calling LLVMCreateDisasmCPUFeatures() with an empty feature set. 195*9880d681SAndroid Build Coastguard Worker */ 196*9880d681SAndroid Build Coastguard Worker LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, 197*9880d681SAndroid Build Coastguard Worker void *DisInfo, int TagType, 198*9880d681SAndroid Build Coastguard Worker LLVMOpInfoCallback GetOpInfo, 199*9880d681SAndroid Build Coastguard Worker LLVMSymbolLookupCallback SymbolLookUp); 200*9880d681SAndroid Build Coastguard Worker 201*9880d681SAndroid Build Coastguard Worker /** 202*9880d681SAndroid Build Coastguard Worker * Create a disassembler for the TripleName, a specific CPU and specific feature 203*9880d681SAndroid Build Coastguard Worker * string. Symbolic disassembly is supported by passing a block of information 204*9880d681SAndroid Build Coastguard Worker * in the DisInfo parameter and specifying the TagType and callback functions as 205*9880d681SAndroid Build Coastguard Worker * described above. These can all be passed * as NULL. If successful, this 206*9880d681SAndroid Build Coastguard Worker * returns a disassembler context. If not, it returns NULL. 207*9880d681SAndroid Build Coastguard Worker */ 208*9880d681SAndroid Build Coastguard Worker LLVMDisasmContextRef 209*9880d681SAndroid Build Coastguard Worker LLVMCreateDisasmCPUFeatures(const char *Triple, const char *CPU, 210*9880d681SAndroid Build Coastguard Worker const char *Features, void *DisInfo, int TagType, 211*9880d681SAndroid Build Coastguard Worker LLVMOpInfoCallback GetOpInfo, 212*9880d681SAndroid Build Coastguard Worker LLVMSymbolLookupCallback SymbolLookUp); 213*9880d681SAndroid Build Coastguard Worker 214*9880d681SAndroid Build Coastguard Worker /** 215*9880d681SAndroid Build Coastguard Worker * Set the disassembler's options. Returns 1 if it can set the Options and 0 216*9880d681SAndroid Build Coastguard Worker * otherwise. 217*9880d681SAndroid Build Coastguard Worker */ 218*9880d681SAndroid Build Coastguard Worker int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options); 219*9880d681SAndroid Build Coastguard Worker 220*9880d681SAndroid Build Coastguard Worker /* The option to produce marked up assembly. */ 221*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_Option_UseMarkup 1 222*9880d681SAndroid Build Coastguard Worker /* The option to print immediates as hex. */ 223*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_Option_PrintImmHex 2 224*9880d681SAndroid Build Coastguard Worker /* The option use the other assembler printer variant */ 225*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_Option_AsmPrinterVariant 4 226*9880d681SAndroid Build Coastguard Worker /* The option to set comment on instructions */ 227*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_Option_SetInstrComments 8 228*9880d681SAndroid Build Coastguard Worker /* The option to print latency information alongside instructions */ 229*9880d681SAndroid Build Coastguard Worker #define LLVMDisassembler_Option_PrintLatency 16 230*9880d681SAndroid Build Coastguard Worker 231*9880d681SAndroid Build Coastguard Worker /** 232*9880d681SAndroid Build Coastguard Worker * Dispose of a disassembler context. 233*9880d681SAndroid Build Coastguard Worker */ 234*9880d681SAndroid Build Coastguard Worker void LLVMDisasmDispose(LLVMDisasmContextRef DC); 235*9880d681SAndroid Build Coastguard Worker 236*9880d681SAndroid Build Coastguard Worker /** 237*9880d681SAndroid Build Coastguard Worker * Disassemble a single instruction using the disassembler context specified in 238*9880d681SAndroid Build Coastguard Worker * the parameter DC. The bytes of the instruction are specified in the 239*9880d681SAndroid Build Coastguard Worker * parameter Bytes, and contains at least BytesSize number of bytes. The 240*9880d681SAndroid Build Coastguard Worker * instruction is at the address specified by the PC parameter. If a valid 241*9880d681SAndroid Build Coastguard Worker * instruction can be disassembled, its string is returned indirectly in 242*9880d681SAndroid Build Coastguard Worker * OutString whose size is specified in the parameter OutStringSize. This 243*9880d681SAndroid Build Coastguard Worker * function returns the number of bytes in the instruction or zero if there was 244*9880d681SAndroid Build Coastguard Worker * no valid instruction. 245*9880d681SAndroid Build Coastguard Worker */ 246*9880d681SAndroid Build Coastguard Worker size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes, 247*9880d681SAndroid Build Coastguard Worker uint64_t BytesSize, uint64_t PC, 248*9880d681SAndroid Build Coastguard Worker char *OutString, size_t OutStringSize); 249*9880d681SAndroid Build Coastguard Worker 250*9880d681SAndroid Build Coastguard Worker /** 251*9880d681SAndroid Build Coastguard Worker * @} 252*9880d681SAndroid Build Coastguard Worker */ 253*9880d681SAndroid Build Coastguard Worker 254*9880d681SAndroid Build Coastguard Worker #ifdef __cplusplus 255*9880d681SAndroid Build Coastguard Worker } 256*9880d681SAndroid Build Coastguard Worker #endif /* !defined(__cplusplus) */ 257*9880d681SAndroid Build Coastguard Worker 258*9880d681SAndroid Build Coastguard Worker #endif /* LLVM_C_DISASSEMBLER_H */ 259