1 /* udis86 - libudis86/types.h 2 * 3 * Copyright (c) 2002-2013 Vivek Thampi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 #ifndef UD_TYPES_H 27 #define UD_TYPES_H 28 29 #ifdef __KERNEL__ 30 /* 31 * -D__KERNEL__ is automatically passed on the command line when 32 * building something as part of the Linux kernel. Assume standalone 33 * mode. 34 */ 35 # include <linux/kernel.h> 36 # include <linux/string.h> 37 # ifndef __UD_STANDALONE__ 38 # define __UD_STANDALONE__ 1 39 # endif 40 #endif /* __KERNEL__ */ 41 42 #if !defined(__UD_STANDALONE__) 43 # include <stdint.h> 44 # include <stdio.h> 45 #endif 46 47 /* gcc specific extensions */ 48 #ifdef __GNUC__ 49 # define UD_ATTR_PACKED __attribute__((packed)) 50 #else 51 # define UD_ATTR_PACKED 52 #endif /* UD_ATTR_PACKED */ 53 54 55 /* ----------------------------------------------------------------------------- 56 * All possible "types" of objects in udis86. Order is Important! 57 * ----------------------------------------------------------------------------- 58 */ 59 enum ud_type 60 { 61 UD_NONE, 62 63 /* 8 bit GPRs */ 64 UD_R_AL, UD_R_CL, UD_R_DL, UD_R_BL, 65 UD_R_AH, UD_R_CH, UD_R_DH, UD_R_BH, 66 UD_R_SPL, UD_R_BPL, UD_R_SIL, UD_R_DIL, 67 UD_R_R8B, UD_R_R9B, UD_R_R10B, UD_R_R11B, 68 UD_R_R12B, UD_R_R13B, UD_R_R14B, UD_R_R15B, 69 70 /* 16 bit GPRs */ 71 UD_R_AX, UD_R_CX, UD_R_DX, UD_R_BX, 72 UD_R_SP, UD_R_BP, UD_R_SI, UD_R_DI, 73 UD_R_R8W, UD_R_R9W, UD_R_R10W, UD_R_R11W, 74 UD_R_R12W, UD_R_R13W, UD_R_R14W, UD_R_R15W, 75 76 /* 32 bit GPRs */ 77 UD_R_EAX, UD_R_ECX, UD_R_EDX, UD_R_EBX, 78 UD_R_ESP, UD_R_EBP, UD_R_ESI, UD_R_EDI, 79 UD_R_R8D, UD_R_R9D, UD_R_R10D, UD_R_R11D, 80 UD_R_R12D, UD_R_R13D, UD_R_R14D, UD_R_R15D, 81 82 /* 64 bit GPRs */ 83 UD_R_RAX, UD_R_RCX, UD_R_RDX, UD_R_RBX, 84 UD_R_RSP, UD_R_RBP, UD_R_RSI, UD_R_RDI, 85 UD_R_R8, UD_R_R9, UD_R_R10, UD_R_R11, 86 UD_R_R12, UD_R_R13, UD_R_R14, UD_R_R15, 87 88 /* segment registers */ 89 UD_R_ES, UD_R_CS, UD_R_SS, UD_R_DS, 90 UD_R_FS, UD_R_GS, 91 92 /* control registers*/ 93 UD_R_CR0, UD_R_CR1, UD_R_CR2, UD_R_CR3, 94 UD_R_CR4, UD_R_CR5, UD_R_CR6, UD_R_CR7, 95 UD_R_CR8, UD_R_CR9, UD_R_CR10, UD_R_CR11, 96 UD_R_CR12, UD_R_CR13, UD_R_CR14, UD_R_CR15, 97 98 /* debug registers */ 99 UD_R_DR0, UD_R_DR1, UD_R_DR2, UD_R_DR3, 100 UD_R_DR4, UD_R_DR5, UD_R_DR6, UD_R_DR7, 101 UD_R_DR8, UD_R_DR9, UD_R_DR10, UD_R_DR11, 102 UD_R_DR12, UD_R_DR13, UD_R_DR14, UD_R_DR15, 103 104 /* mmx registers */ 105 UD_R_MM0, UD_R_MM1, UD_R_MM2, UD_R_MM3, 106 UD_R_MM4, UD_R_MM5, UD_R_MM6, UD_R_MM7, 107 108 /* x87 registers */ 109 UD_R_ST0, UD_R_ST1, UD_R_ST2, UD_R_ST3, 110 UD_R_ST4, UD_R_ST5, UD_R_ST6, UD_R_ST7, 111 112 /* extended multimedia registers */ 113 UD_R_XMM0, UD_R_XMM1, UD_R_XMM2, UD_R_XMM3, 114 UD_R_XMM4, UD_R_XMM5, UD_R_XMM6, UD_R_XMM7, 115 UD_R_XMM8, UD_R_XMM9, UD_R_XMM10, UD_R_XMM11, 116 UD_R_XMM12, UD_R_XMM13, UD_R_XMM14, UD_R_XMM15, 117 118 /* 256B multimedia registers */ 119 UD_R_YMM0, UD_R_YMM1, UD_R_YMM2, UD_R_YMM3, 120 UD_R_YMM4, UD_R_YMM5, UD_R_YMM6, UD_R_YMM7, 121 UD_R_YMM8, UD_R_YMM9, UD_R_YMM10, UD_R_YMM11, 122 UD_R_YMM12, UD_R_YMM13, UD_R_YMM14, UD_R_YMM15, 123 124 UD_R_RIP, 125 126 /* Operand Types */ 127 UD_OP_REG, UD_OP_MEM, UD_OP_PTR, UD_OP_IMM, 128 UD_OP_JIMM, UD_OP_CONST 129 }; 130 131 #include "itab.h" 132 133 union ud_lval { 134 int8_t sbyte; 135 uint8_t ubyte; 136 int16_t sword; 137 uint16_t uword; 138 int32_t sdword; 139 uint32_t udword; 140 int64_t sqword; 141 uint64_t uqword; 142 struct { 143 uint16_t seg; 144 uint32_t off; 145 } ptr; 146 }; 147 148 /* ----------------------------------------------------------------------------- 149 * struct ud_operand - Disassembled instruction Operand. 150 * ----------------------------------------------------------------------------- 151 */ 152 struct ud_operand { 153 enum ud_type type; 154 uint16_t size; 155 enum ud_type base; 156 enum ud_type index; 157 uint8_t scale; 158 uint8_t offset; 159 union ud_lval lval; 160 /* 161 * internal use only 162 */ 163 uint64_t _legacy; /* this will be removed in 1.8 */ 164 uint8_t _oprcode; 165 }; 166 167 /* ----------------------------------------------------------------------------- 168 * struct ud - The udis86 object. 169 * ----------------------------------------------------------------------------- 170 */ 171 struct ud 172 { 173 /* 174 * input buffering 175 */ 176 int (*inp_hook) (struct ud*); 177 #ifndef __UD_STANDALONE__ 178 FILE* inp_file; 179 #endif 180 const uint8_t* inp_buf; 181 size_t inp_buf_size; 182 size_t inp_buf_index; 183 uint8_t inp_curr; 184 size_t inp_ctr; 185 uint8_t inp_sess[64]; 186 int inp_end; 187 int inp_peek; 188 189 void (*translator)(struct ud*); 190 uint64_t insn_offset; 191 char insn_hexcode[64]; 192 193 /* 194 * Assembly output buffer 195 */ 196 char *asm_buf; 197 size_t asm_buf_size; 198 size_t asm_buf_fill; 199 char asm_buf_int[128]; 200 201 /* 202 * Symbol resolver for use in the translation phase. 203 */ 204 const char* (*sym_resolver)(struct ud*, uint64_t addr, int64_t *offset); 205 206 uint8_t dis_mode; 207 uint64_t pc; 208 uint8_t vendor; 209 enum ud_mnemonic_code mnemonic; 210 struct ud_operand operand[4]; 211 uint8_t error; 212 uint8_t _rex; 213 uint8_t pfx_rex; 214 uint8_t pfx_seg; 215 uint8_t pfx_opr; 216 uint8_t pfx_adr; 217 uint8_t pfx_lock; 218 uint8_t pfx_str; 219 uint8_t pfx_rep; 220 uint8_t pfx_repe; 221 uint8_t pfx_repne; 222 uint8_t opr_mode; 223 uint8_t adr_mode; 224 uint8_t br_far; 225 uint8_t br_near; 226 uint8_t have_modrm; 227 uint8_t modrm; 228 uint8_t modrm_offset; 229 uint8_t vex_op; 230 uint8_t vex_b1; 231 uint8_t vex_b2; 232 uint8_t primary_opcode; 233 void * user_opaque_data; 234 struct ud_itab_entry * itab_entry; 235 struct ud_lookup_table_list_entry *le; 236 }; 237 238 /* ----------------------------------------------------------------------------- 239 * Type-definitions 240 * ----------------------------------------------------------------------------- 241 */ 242 typedef enum ud_type ud_type_t; 243 typedef enum ud_mnemonic_code ud_mnemonic_code_t; 244 245 typedef struct ud ud_t; 246 typedef struct ud_operand ud_operand_t; 247 248 #define UD_SYN_INTEL ud_translate_intel 249 #define UD_SYN_ATT ud_translate_att 250 #define UD_EOI (-1) 251 #define UD_INP_CACHE_SZ 32 252 #define UD_VENDOR_AMD 0 253 #define UD_VENDOR_INTEL 1 254 #define UD_VENDOR_ANY 2 255 256 #endif 257 258 /* 259 vim: set ts=2 sw=2 expandtab 260 */ 261