| 1 | /*===-- X86DisassemblerDecoderInternal.h - Disassembler decoder ---*- C -*-===* |
| 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 | * This file is part of the X86 Disassembler. |
| 11 | * It contains the public interface of the instruction decoder. |
| 12 | * Documentation for the disassembler can be found in X86Disassembler.h. |
| 13 | * |
| 14 | *===----------------------------------------------------------------------===*/ |
| 15 | |
| 16 | /* Capstone Disassembly Engine */ |
| 17 | /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 18 | |
| 19 | #ifndef CS_X86_DISASSEMBLERDECODER_H |
| 20 | #define CS_X86_DISASSEMBLERDECODER_H |
| 21 | |
| 22 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 23 | #include <libkern/libkern.h> |
| 24 | #else |
| 25 | #include <stdio.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "X86DisassemblerDecoderCommon.h" |
| 29 | |
| 30 | /* |
| 31 | * Accessor functions for various fields of an Intel instruction |
| 32 | */ |
| 33 | #define modFromModRM(modRM) (((modRM) & 0xc0) >> 6) |
| 34 | #define regFromModRM(modRM) (((modRM) & 0x38) >> 3) |
| 35 | #define rmFromModRM(modRM) ((modRM) & 0x7) |
| 36 | #define scaleFromSIB(sib) (((sib) & 0xc0) >> 6) |
| 37 | #define indexFromSIB(sib) (((sib) & 0x38) >> 3) |
| 38 | #define baseFromSIB(sib) ((sib) & 0x7) |
| 39 | #define wFromREX(rex) (((rex) & 0x8) >> 3) |
| 40 | #define rFromREX(rex) (((rex) & 0x4) >> 2) |
| 41 | #define xFromREX(rex) (((rex) & 0x2) >> 1) |
| 42 | #define bFromREX(rex) ((rex) & 0x1) |
| 43 | |
| 44 | #define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7) |
| 45 | #define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6) |
| 46 | #define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5) |
| 47 | #define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4) |
| 48 | #define mmFromEVEX2of4(evex) ((evex) & 0x3) |
| 49 | #define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7) |
| 50 | #define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3) |
| 51 | #define ppFromEVEX3of4(evex) ((evex) & 0x3) |
| 52 | #define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7) |
| 53 | #define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6) |
| 54 | #define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5) |
| 55 | #define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4) |
| 56 | #define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3) |
| 57 | #define aaaFromEVEX4of4(evex) ((evex) & 0x7) |
| 58 | |
| 59 | #define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7) |
| 60 | #define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6) |
| 61 | #define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5) |
| 62 | #define mmmmmFromVEX2of3(vex) ((vex) & 0x1f) |
| 63 | #define wFromVEX3of3(vex) (((vex) & 0x80) >> 7) |
| 64 | #define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3) |
| 65 | #define lFromVEX3of3(vex) (((vex) & 0x4) >> 2) |
| 66 | #define ppFromVEX3of3(vex) ((vex) & 0x3) |
| 67 | |
| 68 | #define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7) |
| 69 | #define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3) |
| 70 | #define lFromVEX2of2(vex) (((vex) & 0x4) >> 2) |
| 71 | #define ppFromVEX2of2(vex) ((vex) & 0x3) |
| 72 | |
| 73 | #define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7) |
| 74 | #define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6) |
| 75 | #define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5) |
| 76 | #define mmmmmFromXOP2of3(xop) ((xop) & 0x1f) |
| 77 | #define wFromXOP3of3(xop) (((xop) & 0x80) >> 7) |
| 78 | #define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3) |
| 79 | #define lFromXOP3of3(xop) (((xop) & 0x4) >> 2) |
| 80 | #define ppFromXOP3of3(xop) ((xop) & 0x3) |
| 81 | |
| 82 | /* |
| 83 | * These enums represent Intel registers for use by the decoder. |
| 84 | */ |
| 85 | |
| 86 | #define REGS_8BIT \ |
| 87 | ENTRY(AL) \ |
| 88 | ENTRY(CL) \ |
| 89 | ENTRY(DL) \ |
| 90 | ENTRY(BL) \ |
| 91 | ENTRY(AH) \ |
| 92 | ENTRY(CH) \ |
| 93 | ENTRY(DH) \ |
| 94 | ENTRY(BH) \ |
| 95 | ENTRY(R8B) \ |
| 96 | ENTRY(R9B) \ |
| 97 | ENTRY(R10B) \ |
| 98 | ENTRY(R11B) \ |
| 99 | ENTRY(R12B) \ |
| 100 | ENTRY(R13B) \ |
| 101 | ENTRY(R14B) \ |
| 102 | ENTRY(R15B) \ |
| 103 | ENTRY(SPL) \ |
| 104 | ENTRY(BPL) \ |
| 105 | ENTRY(SIL) \ |
| 106 | ENTRY(DIL) |
| 107 | |
| 108 | #define EA_BASES_16BIT \ |
| 109 | ENTRY(BX_SI) \ |
| 110 | ENTRY(BX_DI) \ |
| 111 | ENTRY(BP_SI) \ |
| 112 | ENTRY(BP_DI) \ |
| 113 | ENTRY(SI) \ |
| 114 | ENTRY(DI) \ |
| 115 | ENTRY(BP) \ |
| 116 | ENTRY(BX) \ |
| 117 | ENTRY(R8W) \ |
| 118 | ENTRY(R9W) \ |
| 119 | ENTRY(R10W) \ |
| 120 | ENTRY(R11W) \ |
| 121 | ENTRY(R12W) \ |
| 122 | ENTRY(R13W) \ |
| 123 | ENTRY(R14W) \ |
| 124 | ENTRY(R15W) |
| 125 | |
| 126 | #define REGS_16BIT \ |
| 127 | ENTRY(AX) \ |
| 128 | ENTRY(CX) \ |
| 129 | ENTRY(DX) \ |
| 130 | ENTRY(BX) \ |
| 131 | ENTRY(SP) \ |
| 132 | ENTRY(BP) \ |
| 133 | ENTRY(SI) \ |
| 134 | ENTRY(DI) \ |
| 135 | ENTRY(R8W) \ |
| 136 | ENTRY(R9W) \ |
| 137 | ENTRY(R10W) \ |
| 138 | ENTRY(R11W) \ |
| 139 | ENTRY(R12W) \ |
| 140 | ENTRY(R13W) \ |
| 141 | ENTRY(R14W) \ |
| 142 | ENTRY(R15W) |
| 143 | |
| 144 | #define EA_BASES_32BIT \ |
| 145 | ENTRY(EAX) \ |
| 146 | ENTRY(ECX) \ |
| 147 | ENTRY(EDX) \ |
| 148 | ENTRY(EBX) \ |
| 149 | ENTRY(sib) \ |
| 150 | ENTRY(EBP) \ |
| 151 | ENTRY(ESI) \ |
| 152 | ENTRY(EDI) \ |
| 153 | ENTRY(R8D) \ |
| 154 | ENTRY(R9D) \ |
| 155 | ENTRY(R10D) \ |
| 156 | ENTRY(R11D) \ |
| 157 | ENTRY(R12D) \ |
| 158 | ENTRY(R13D) \ |
| 159 | ENTRY(R14D) \ |
| 160 | ENTRY(R15D) |
| 161 | |
| 162 | #define REGS_32BIT \ |
| 163 | ENTRY(EAX) \ |
| 164 | ENTRY(ECX) \ |
| 165 | ENTRY(EDX) \ |
| 166 | ENTRY(EBX) \ |
| 167 | ENTRY(ESP) \ |
| 168 | ENTRY(EBP) \ |
| 169 | ENTRY(ESI) \ |
| 170 | ENTRY(EDI) \ |
| 171 | ENTRY(R8D) \ |
| 172 | ENTRY(R9D) \ |
| 173 | ENTRY(R10D) \ |
| 174 | ENTRY(R11D) \ |
| 175 | ENTRY(R12D) \ |
| 176 | ENTRY(R13D) \ |
| 177 | ENTRY(R14D) \ |
| 178 | ENTRY(R15D) |
| 179 | |
| 180 | #define EA_BASES_64BIT \ |
| 181 | ENTRY(RAX) \ |
| 182 | ENTRY(RCX) \ |
| 183 | ENTRY(RDX) \ |
| 184 | ENTRY(RBX) \ |
| 185 | ENTRY(sib64) \ |
| 186 | ENTRY(RBP) \ |
| 187 | ENTRY(RSI) \ |
| 188 | ENTRY(RDI) \ |
| 189 | ENTRY(R8) \ |
| 190 | ENTRY(R9) \ |
| 191 | ENTRY(R10) \ |
| 192 | ENTRY(R11) \ |
| 193 | ENTRY(R12) \ |
| 194 | ENTRY(R13) \ |
| 195 | ENTRY(R14) \ |
| 196 | ENTRY(R15) |
| 197 | |
| 198 | #define REGS_64BIT \ |
| 199 | ENTRY(RAX) \ |
| 200 | ENTRY(RCX) \ |
| 201 | ENTRY(RDX) \ |
| 202 | ENTRY(RBX) \ |
| 203 | ENTRY(RSP) \ |
| 204 | ENTRY(RBP) \ |
| 205 | ENTRY(RSI) \ |
| 206 | ENTRY(RDI) \ |
| 207 | ENTRY(R8) \ |
| 208 | ENTRY(R9) \ |
| 209 | ENTRY(R10) \ |
| 210 | ENTRY(R11) \ |
| 211 | ENTRY(R12) \ |
| 212 | ENTRY(R13) \ |
| 213 | ENTRY(R14) \ |
| 214 | ENTRY(R15) |
| 215 | |
| 216 | #define REGS_MMX \ |
| 217 | ENTRY(MM0) \ |
| 218 | ENTRY(MM1) \ |
| 219 | ENTRY(MM2) \ |
| 220 | ENTRY(MM3) \ |
| 221 | ENTRY(MM4) \ |
| 222 | ENTRY(MM5) \ |
| 223 | ENTRY(MM6) \ |
| 224 | ENTRY(MM7) |
| 225 | |
| 226 | #define REGS_XMM \ |
| 227 | ENTRY(XMM0) \ |
| 228 | ENTRY(XMM1) \ |
| 229 | ENTRY(XMM2) \ |
| 230 | ENTRY(XMM3) \ |
| 231 | ENTRY(XMM4) \ |
| 232 | ENTRY(XMM5) \ |
| 233 | ENTRY(XMM6) \ |
| 234 | ENTRY(XMM7) \ |
| 235 | ENTRY(XMM8) \ |
| 236 | ENTRY(XMM9) \ |
| 237 | ENTRY(XMM10) \ |
| 238 | ENTRY(XMM11) \ |
| 239 | ENTRY(XMM12) \ |
| 240 | ENTRY(XMM13) \ |
| 241 | ENTRY(XMM14) \ |
| 242 | ENTRY(XMM15) \ |
| 243 | ENTRY(XMM16) \ |
| 244 | ENTRY(XMM17) \ |
| 245 | ENTRY(XMM18) \ |
| 246 | ENTRY(XMM19) \ |
| 247 | ENTRY(XMM20) \ |
| 248 | ENTRY(XMM21) \ |
| 249 | ENTRY(XMM22) \ |
| 250 | ENTRY(XMM23) \ |
| 251 | ENTRY(XMM24) \ |
| 252 | ENTRY(XMM25) \ |
| 253 | ENTRY(XMM26) \ |
| 254 | ENTRY(XMM27) \ |
| 255 | ENTRY(XMM28) \ |
| 256 | ENTRY(XMM29) \ |
| 257 | ENTRY(XMM30) \ |
| 258 | ENTRY(XMM31) |
| 259 | |
| 260 | |
| 261 | #define REGS_YMM \ |
| 262 | ENTRY(YMM0) \ |
| 263 | ENTRY(YMM1) \ |
| 264 | ENTRY(YMM2) \ |
| 265 | ENTRY(YMM3) \ |
| 266 | ENTRY(YMM4) \ |
| 267 | ENTRY(YMM5) \ |
| 268 | ENTRY(YMM6) \ |
| 269 | ENTRY(YMM7) \ |
| 270 | ENTRY(YMM8) \ |
| 271 | ENTRY(YMM9) \ |
| 272 | ENTRY(YMM10) \ |
| 273 | ENTRY(YMM11) \ |
| 274 | ENTRY(YMM12) \ |
| 275 | ENTRY(YMM13) \ |
| 276 | ENTRY(YMM14) \ |
| 277 | ENTRY(YMM15) \ |
| 278 | ENTRY(YMM16) \ |
| 279 | ENTRY(YMM17) \ |
| 280 | ENTRY(YMM18) \ |
| 281 | ENTRY(YMM19) \ |
| 282 | ENTRY(YMM20) \ |
| 283 | ENTRY(YMM21) \ |
| 284 | ENTRY(YMM22) \ |
| 285 | ENTRY(YMM23) \ |
| 286 | ENTRY(YMM24) \ |
| 287 | ENTRY(YMM25) \ |
| 288 | ENTRY(YMM26) \ |
| 289 | ENTRY(YMM27) \ |
| 290 | ENTRY(YMM28) \ |
| 291 | ENTRY(YMM29) \ |
| 292 | ENTRY(YMM30) \ |
| 293 | ENTRY(YMM31) |
| 294 | |
| 295 | #define REGS_ZMM \ |
| 296 | ENTRY(ZMM0) \ |
| 297 | ENTRY(ZMM1) \ |
| 298 | ENTRY(ZMM2) \ |
| 299 | ENTRY(ZMM3) \ |
| 300 | ENTRY(ZMM4) \ |
| 301 | ENTRY(ZMM5) \ |
| 302 | ENTRY(ZMM6) \ |
| 303 | ENTRY(ZMM7) \ |
| 304 | ENTRY(ZMM8) \ |
| 305 | ENTRY(ZMM9) \ |
| 306 | ENTRY(ZMM10) \ |
| 307 | ENTRY(ZMM11) \ |
| 308 | ENTRY(ZMM12) \ |
| 309 | ENTRY(ZMM13) \ |
| 310 | ENTRY(ZMM14) \ |
| 311 | ENTRY(ZMM15) \ |
| 312 | ENTRY(ZMM16) \ |
| 313 | ENTRY(ZMM17) \ |
| 314 | ENTRY(ZMM18) \ |
| 315 | ENTRY(ZMM19) \ |
| 316 | ENTRY(ZMM20) \ |
| 317 | ENTRY(ZMM21) \ |
| 318 | ENTRY(ZMM22) \ |
| 319 | ENTRY(ZMM23) \ |
| 320 | ENTRY(ZMM24) \ |
| 321 | ENTRY(ZMM25) \ |
| 322 | ENTRY(ZMM26) \ |
| 323 | ENTRY(ZMM27) \ |
| 324 | ENTRY(ZMM28) \ |
| 325 | ENTRY(ZMM29) \ |
| 326 | ENTRY(ZMM30) \ |
| 327 | ENTRY(ZMM31) |
| 328 | |
| 329 | #define REGS_MASKS \ |
| 330 | ENTRY(K0) \ |
| 331 | ENTRY(K1) \ |
| 332 | ENTRY(K2) \ |
| 333 | ENTRY(K3) \ |
| 334 | ENTRY(K4) \ |
| 335 | ENTRY(K5) \ |
| 336 | ENTRY(K6) \ |
| 337 | ENTRY(K7) |
| 338 | |
| 339 | #define REGS_SEGMENT \ |
| 340 | ENTRY(ES) \ |
| 341 | ENTRY(CS) \ |
| 342 | ENTRY(SS) \ |
| 343 | ENTRY(DS) \ |
| 344 | ENTRY(FS) \ |
| 345 | ENTRY(GS) |
| 346 | |
| 347 | #define REGS_DEBUG \ |
| 348 | ENTRY(DR0) \ |
| 349 | ENTRY(DR1) \ |
| 350 | ENTRY(DR2) \ |
| 351 | ENTRY(DR3) \ |
| 352 | ENTRY(DR4) \ |
| 353 | ENTRY(DR5) \ |
| 354 | ENTRY(DR6) \ |
| 355 | ENTRY(DR7) \ |
| 356 | ENTRY(DR8) \ |
| 357 | ENTRY(DR9) \ |
| 358 | ENTRY(DR10) \ |
| 359 | ENTRY(DR11) \ |
| 360 | ENTRY(DR12) \ |
| 361 | ENTRY(DR13) \ |
| 362 | ENTRY(DR14) \ |
| 363 | ENTRY(DR15) |
| 364 | |
| 365 | #define REGS_CONTROL \ |
| 366 | ENTRY(CR0) \ |
| 367 | ENTRY(CR1) \ |
| 368 | ENTRY(CR2) \ |
| 369 | ENTRY(CR3) \ |
| 370 | ENTRY(CR4) \ |
| 371 | ENTRY(CR5) \ |
| 372 | ENTRY(CR6) \ |
| 373 | ENTRY(CR7) \ |
| 374 | ENTRY(CR8) \ |
| 375 | ENTRY(CR9) \ |
| 376 | ENTRY(CR10) \ |
| 377 | ENTRY(CR11) \ |
| 378 | ENTRY(CR12) \ |
| 379 | ENTRY(CR13) \ |
| 380 | ENTRY(CR14) \ |
| 381 | ENTRY(CR15) |
| 382 | |
| 383 | #define REGS_BOUND \ |
| 384 | ENTRY(BND0) \ |
| 385 | ENTRY(BND1) \ |
| 386 | ENTRY(BND2) \ |
| 387 | ENTRY(BND3) |
| 388 | |
| 389 | #define ALL_EA_BASES \ |
| 390 | EA_BASES_16BIT \ |
| 391 | EA_BASES_32BIT \ |
| 392 | EA_BASES_64BIT |
| 393 | |
| 394 | #define ALL_SIB_BASES \ |
| 395 | REGS_32BIT \ |
| 396 | REGS_64BIT |
| 397 | |
| 398 | #define ALL_REGS \ |
| 399 | REGS_8BIT \ |
| 400 | REGS_16BIT \ |
| 401 | REGS_32BIT \ |
| 402 | REGS_64BIT \ |
| 403 | REGS_MMX \ |
| 404 | REGS_XMM \ |
| 405 | REGS_YMM \ |
| 406 | REGS_ZMM \ |
| 407 | REGS_MASKS \ |
| 408 | REGS_SEGMENT \ |
| 409 | REGS_DEBUG \ |
| 410 | REGS_CONTROL \ |
| 411 | REGS_BOUND \ |
| 412 | ENTRY(RIP) |
| 413 | |
| 414 | /* |
| 415 | * EABase - All possible values of the base field for effective-address |
| 416 | * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We |
| 417 | * distinguish between bases (EA_BASE_*) and registers that just happen to be |
| 418 | * referred to when Mod == 0b11 (EA_REG_*). |
| 419 | */ |
| 420 | typedef enum { |
| 421 | EA_BASE_NONE, |
| 422 | #define ENTRY(x) EA_BASE_##x, |
| 423 | ALL_EA_BASES |
| 424 | #undef ENTRY |
| 425 | #define ENTRY(x) EA_REG_##x, |
| 426 | ALL_REGS |
| 427 | #undef ENTRY |
| 428 | EA_max |
| 429 | } EABase; |
| 430 | |
| 431 | /* |
| 432 | * SIBIndex - All possible values of the SIB index field. |
| 433 | * Borrows entries from ALL_EA_BASES with the special case that |
| 434 | * sib is synonymous with NONE. |
| 435 | * Vector SIB: index can be XMM or YMM. |
| 436 | */ |
| 437 | typedef enum { |
| 438 | SIB_INDEX_NONE, |
| 439 | #define ENTRY(x) SIB_INDEX_##x, |
| 440 | ALL_EA_BASES |
| 441 | REGS_XMM |
| 442 | REGS_YMM |
| 443 | REGS_ZMM |
| 444 | #undef ENTRY |
| 445 | SIB_INDEX_max |
| 446 | } SIBIndex; |
| 447 | |
| 448 | /* |
| 449 | * SIBBase - All possible values of the SIB base field. |
| 450 | */ |
| 451 | typedef enum { |
| 452 | SIB_BASE_NONE, |
| 453 | #define ENTRY(x) SIB_BASE_##x, |
| 454 | ALL_SIB_BASES |
| 455 | #undef ENTRY |
| 456 | SIB_BASE_max |
| 457 | } SIBBase; |
| 458 | |
| 459 | /* |
| 460 | * EADisplacement - Possible displacement types for effective-address |
| 461 | * computations. |
| 462 | */ |
| 463 | typedef enum { |
| 464 | EA_DISP_NONE, |
| 465 | EA_DISP_8, |
| 466 | EA_DISP_16, |
| 467 | EA_DISP_32 |
| 468 | } EADisplacement; |
| 469 | |
| 470 | /* |
| 471 | * Reg - All possible values of the reg field in the ModR/M byte. |
| 472 | */ |
| 473 | typedef enum { |
| 474 | #define ENTRY(x) MODRM_REG_##x, |
| 475 | ALL_REGS |
| 476 | #undef ENTRY |
| 477 | MODRM_REG_max |
| 478 | } Reg; |
| 479 | |
| 480 | /* |
| 481 | * SegmentOverride - All possible segment overrides. |
| 482 | */ |
| 483 | typedef enum { |
| 484 | SEG_OVERRIDE_NONE, |
| 485 | SEG_OVERRIDE_CS, |
| 486 | SEG_OVERRIDE_SS, |
| 487 | SEG_OVERRIDE_DS, |
| 488 | SEG_OVERRIDE_ES, |
| 489 | SEG_OVERRIDE_FS, |
| 490 | SEG_OVERRIDE_GS, |
| 491 | SEG_OVERRIDE_max |
| 492 | } SegmentOverride; |
| 493 | |
| 494 | /* |
| 495 | * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field |
| 496 | */ |
| 497 | typedef enum { |
| 498 | VEX_LOB_0F = 0x1, |
| 499 | VEX_LOB_0F38 = 0x2, |
| 500 | VEX_LOB_0F3A = 0x3 |
| 501 | } VEXLeadingOpcodeByte; |
| 502 | |
| 503 | typedef enum { |
| 504 | XOP_MAP_SELECT_8 = 0x8, |
| 505 | XOP_MAP_SELECT_9 = 0x9, |
| 506 | XOP_MAP_SELECT_A = 0xA |
| 507 | } XOPMapSelect; |
| 508 | |
| 509 | /* |
| 510 | * VEXPrefixCode - Possible values for the VEX.pp/EVEX.pp field |
| 511 | */ |
| 512 | typedef enum { |
| 513 | VEX_PREFIX_NONE = 0x0, |
| 514 | VEX_PREFIX_66 = 0x1, |
| 515 | VEX_PREFIX_F3 = 0x2, |
| 516 | VEX_PREFIX_F2 = 0x3 |
| 517 | } VEXPrefixCode; |
| 518 | |
| 519 | typedef enum { |
| 520 | TYPE_NO_VEX_XOP = 0x0, |
| 521 | TYPE_VEX_2B = 0x1, |
| 522 | TYPE_VEX_3B = 0x2, |
| 523 | TYPE_EVEX = 0x3, |
| 524 | TYPE_XOP = 0x4 |
| 525 | } VectorExtensionType; |
| 526 | |
| 527 | struct reader_info { |
| 528 | const uint8_t *code; |
| 529 | uint64_t size; |
| 530 | uint64_t offset; |
| 531 | }; |
| 532 | |
| 533 | /* |
| 534 | * byteReader_t - Type for the byte reader that the consumer must provide to |
| 535 | * the decoder. Reads a single byte from the instruction's address space. |
| 536 | * @param arg - A baton that the consumer can associate with any internal |
| 537 | * state that it needs. |
| 538 | * @param byte - A pointer to a single byte in memory that should be set to |
| 539 | * contain the value at address. |
| 540 | * @param address - The address in the instruction's address space that should |
| 541 | * be read from. |
| 542 | * @return - -1 if the byte cannot be read for any reason; 0 otherwise. |
| 543 | */ |
| 544 | typedef int (*byteReader_t)(const struct reader_info *arg, uint8_t* byte, uint64_t address); |
| 545 | |
| 546 | /// The specification for how to extract and interpret a full instruction and |
| 547 | /// its operands. |
| 548 | struct InstructionSpecifier { |
| 549 | #ifdef CAPSTONE_X86_REDUCE |
| 550 | uint8_t operands; |
| 551 | #else |
| 552 | uint16_t operands; |
| 553 | #endif |
| 554 | }; |
| 555 | |
| 556 | /* |
| 557 | * The x86 internal instruction, which is produced by the decoder. |
| 558 | */ |
| 559 | typedef struct InternalInstruction { |
| 560 | // from here, all members must be initialized to ZERO to work properly |
| 561 | uint8_t operandSize; |
| 562 | uint8_t prefix0, prefix1, prefix2, prefix3; |
| 563 | /* The value of the REX prefix, if present */ |
| 564 | uint8_t rexPrefix; |
| 565 | /* The segment override type */ |
| 566 | SegmentOverride segmentOverride; |
| 567 | bool consumedModRM; |
| 568 | uint8_t orgModRM; // save original modRM because we will modify modRM |
| 569 | /* The SIB byte, used for more complex 32- or 64-bit memory operands */ |
| 570 | bool consumedSIB; |
| 571 | uint8_t sib; |
| 572 | /* The displacement, used for memory operands */ |
| 573 | bool consumedDisplacement; |
| 574 | int64_t displacement; |
| 575 | /* The value of the two-byte escape prefix (usually 0x0f) */ |
| 576 | uint8_t twoByteEscape; |
| 577 | /* The value of the three-byte escape prefix (usually 0x38 or 0x3a) */ |
| 578 | uint8_t threeByteEscape; |
| 579 | /* SIB state */ |
| 580 | SIBIndex sibIndexBase; |
| 581 | SIBIndex sibIndex; |
| 582 | uint8_t sibScale; |
| 583 | SIBBase sibBase; |
| 584 | |
| 585 | // Embedded rounding control. |
| 586 | uint8_t RC; |
| 587 | |
| 588 | uint8_t numImmediatesConsumed; |
| 589 | /* 0xf2 or 0xf3 is xacquire or xrelease */ |
| 590 | uint8_t xAcquireRelease; |
| 591 | |
| 592 | // Address-size override |
| 593 | bool hasAdSize; |
| 594 | // Operand-size override |
| 595 | bool hasOpSize; |
| 596 | // Lock prefix |
| 597 | bool hasLockPrefix; |
| 598 | // The repeat prefix if any |
| 599 | uint8_t repeatPrefix; |
| 600 | |
| 601 | // The possible mandatory prefix |
| 602 | uint8_t mandatoryPrefix; |
| 603 | |
| 604 | /* The value of the vector extension prefix(EVEX/VEX/XOP), if present */ |
| 605 | uint8_t vectorExtensionPrefix[4]; |
| 606 | |
| 607 | /* Offsets from the start of the instruction to the pieces of data, which is |
| 608 | needed to find relocation entries for adding symbolic operands */ |
| 609 | uint8_t displacementOffset; |
| 610 | uint8_t immediateOffset; |
| 611 | uint8_t modRMOffset; |
| 612 | |
| 613 | // end-of-zero-members |
| 614 | |
| 615 | /* Reader interface (C) */ |
| 616 | byteReader_t reader; |
| 617 | |
| 618 | /* Opaque value passed to the reader */ |
| 619 | const void* readerArg; |
| 620 | /* The address of the next byte to read via the reader */ |
| 621 | uint64_t readerCursor; |
| 622 | |
| 623 | /* General instruction information */ |
| 624 | |
| 625 | /* The mode to disassemble for (64-bit, protected, real) */ |
| 626 | DisassemblerMode mode; |
| 627 | /* The start of the instruction, usable with the reader */ |
| 628 | uint64_t startLocation; |
| 629 | /* The length of the instruction, in bytes */ |
| 630 | size_t length; |
| 631 | |
| 632 | /* Prefix state */ |
| 633 | |
| 634 | /* The type of the vector extension prefix */ |
| 635 | VectorExtensionType vectorExtensionType; |
| 636 | |
| 637 | /* Sizes of various critical pieces of data, in bytes */ |
| 638 | uint8_t registerSize; |
| 639 | uint8_t addressSize; |
| 640 | uint8_t displacementSize; |
| 641 | uint8_t immediateSize; |
| 642 | |
| 643 | uint8_t immSize; // immediate size for X86_OP_IMM operand |
| 644 | |
| 645 | /* opcode state */ |
| 646 | |
| 647 | /* The last byte of the opcode, not counting any ModR/M extension */ |
| 648 | uint8_t opcode; |
| 649 | |
| 650 | /* decode state */ |
| 651 | |
| 652 | /* The type of opcode, used for indexing into the array of decode tables */ |
| 653 | OpcodeType opcodeType; |
| 654 | /* The instruction ID, extracted from the decode table */ |
| 655 | uint16_t instructionID; |
| 656 | /* The specifier for the instruction, from the instruction info table */ |
| 657 | const struct InstructionSpecifier *spec; |
| 658 | |
| 659 | /* state for additional bytes, consumed during operand decode. Pattern: |
| 660 | consumed___ indicates that the byte was already consumed and does not |
| 661 | need to be consumed again */ |
| 662 | |
| 663 | /* The VEX.vvvv field, which contains a third register operand for some AVX |
| 664 | instructions */ |
| 665 | Reg vvvv; |
| 666 | |
| 667 | /* The writemask for AVX-512 instructions which is contained in EVEX.aaa */ |
| 668 | Reg writemask; |
| 669 | |
| 670 | /* The ModR/M byte, which contains most register operands and some portion of |
| 671 | all memory operands */ |
| 672 | uint8_t modRM; |
| 673 | |
| 674 | // special data to handle MOVcr, MOVdr, MOVrc, MOVrd |
| 675 | uint8_t firstByte; // save the first byte in stream |
| 676 | |
| 677 | /* Immediates. There can be two in some cases */ |
| 678 | uint8_t numImmediatesTranslated; |
| 679 | uint64_t immediates[2]; |
| 680 | |
| 681 | /* A register or immediate operand encoded into the opcode */ |
| 682 | Reg opcodeRegister; |
| 683 | |
| 684 | /* Portions of the ModR/M byte */ |
| 685 | |
| 686 | /* These fields determine the allowable values for the ModR/M fields, which |
| 687 | depend on operand and address widths */ |
| 688 | EABase eaRegBase; |
| 689 | Reg regBase; |
| 690 | |
| 691 | /* The Mod and R/M fields can encode a base for an effective address, or a |
| 692 | register. These are separated into two fields here */ |
| 693 | EABase eaBase; |
| 694 | EADisplacement eaDisplacement; |
| 695 | /* The reg field always encodes a register */ |
| 696 | Reg reg; |
| 697 | |
| 698 | const struct OperandSpecifier *operands; |
| 699 | } InternalInstruction; |
| 700 | |
| 701 | /* decodeInstruction - Decode one instruction and store the decoding results in |
| 702 | * a buffer provided by the consumer. |
| 703 | * @param insn - The buffer to store the instruction in. Allocated by the |
| 704 | * consumer. |
| 705 | * @param reader - The byteReader_t for the bytes to be read. |
| 706 | * @param readerArg - An argument to pass to the reader for storing context |
| 707 | * specific to the consumer. May be NULL. |
| 708 | * @param logger - The dlog_t to be used in printing status messages from the |
| 709 | * disassembler. May be NULL. |
| 710 | * @param loggerArg - An argument to pass to the logger for storing context |
| 711 | * specific to the logger. May be NULL. |
| 712 | * @param startLoc - The address (in the reader's address space) of the first |
| 713 | * byte in the instruction. |
| 714 | * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in. |
| 715 | * @return - Nonzero if there was an error during decode, 0 otherwise. |
| 716 | */ |
| 717 | int decodeInstruction(struct InternalInstruction* insn, |
| 718 | byteReader_t reader, |
| 719 | const void* readerArg, |
| 720 | uint64_t startLoc, |
| 721 | DisassemblerMode mode); |
| 722 | |
| 723 | //const char *x86DisassemblerGetInstrName(unsigned Opcode, const void *mii); |
| 724 | |
| 725 | #endif |