1 /* udis86 - libudis86/decode.c
3 * Copyright (c) 2002-2009 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
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.
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.
31 #include "udis86_extern.h"
32 #include "udis86_types.h"
33 #include "udis86_input.h"
34 #include "udis86_decode.h"
35 #include <wtf/Assertions.h>
38 /* #define dbg printf */
40 #ifndef __UD_STANDALONE__
42 #endif /* __UD_STANDALONE__ */
44 /* The max number of prefixes to an instruction */
45 #define MAX_PREFIXES 15
47 /* instruction aliases and special cases */
48 static struct ud_itab_entry s_ie__invalid
=
49 { UD_Iinvalid
, O_NONE
, O_NONE
, O_NONE
, P_none
};
52 decode_ext(struct ud
*u
, uint16_t ptr
);
56 eff_opr_mode(int dis_mode
, int rex_w
, int pfx_opr
)
59 return rex_w
? 64 : (pfx_opr
? 16 : 32);
60 } else if (dis_mode
== 32) {
61 return pfx_opr
? 16 : 32;
63 ASSERT(dis_mode
== 16);
64 return pfx_opr
? 32 : 16;
70 eff_adr_mode(int dis_mode
, int pfx_adr
)
73 return pfx_adr
? 32 : 64;
74 } else if (dis_mode
== 32) {
75 return pfx_adr
? 16 : 32;
77 ASSERT(dis_mode
== 16);
78 return pfx_adr
? 32 : 16;
83 /* Looks up mnemonic code in the mnemonic string table
84 * Returns NULL if the mnemonic code is invalid
86 const char * ud_lookup_mnemonic( enum ud_mnemonic_code c
)
88 return ud_mnemonics_str
[ c
];
95 * Extracts instruction prefixes.
98 decode_prefixes(struct ud
*u
)
100 unsigned int have_pfx
= 1;
104 /* if in error state, bail out */
108 /* keep going as long as there are prefixes available */
109 for ( i
= 0; have_pfx
; ++i
) {
115 curr
= ud_inp_curr( u
);
117 /* rex prefixes in 64bit mode */
118 if ( u
->dis_mode
== 64 && ( curr
& 0xF0 ) == 0x40 ) {
124 u
->pfx_seg
= UD_R_CS
;
128 u
->pfx_seg
= UD_R_SS
;
132 u
->pfx_seg
= UD_R_DS
;
136 u
->pfx_seg
= UD_R_ES
;
140 u
->pfx_seg
= UD_R_FS
;
144 u
->pfx_seg
= UD_R_GS
;
147 case 0x67 : /* adress-size override prefix */
156 /* the 0x66 sse prefix is only effective if no other sse prefix
157 * has already been specified.
159 if ( !u
->pfx_insn
) u
->pfx_insn
= 0x66;
175 /* No more prefixes */
181 /* check if we reached max instruction length */
182 if ( i
+ 1 == MAX_INSN_LENGTH
) {
192 /* rewind back one byte in stream, since the above loop
193 * stops with a non-prefix byte.
200 static inline unsigned int modrm( struct ud
* u
)
202 if ( !u
->have_modrm
) {
203 u
->modrm
= ud_inp_next( u
);
210 static unsigned int resolve_operand_size( const struct ud
* u
, unsigned int s
)
215 return ( u
->opr_mode
);
217 return ( u
->opr_mode
== 16 ) ? 16 : 32;
219 return ( u
->opr_mode
== 16 ) ? SZ_WP
: SZ_DP
;
221 return ( u
->opr_mode
== 16 ) ? 32 : u
->opr_mode
;
223 return ( u
->dis_mode
== 64 ) ? 64 : 32;
230 static int resolve_mnemonic( struct ud
* u
)
235 /* readjust operand sizes for call/jmp instrcutions */
236 if ( u
->mnemonic
== UD_Icall
|| u
->mnemonic
== UD_Ijmp
) {
237 /* WP: 16:16 pointer */
238 if ( u
->operand
[ 0 ].size
== SZ_WP
) {
239 u
->operand
[ 0 ].size
= 16;
242 /* DP: 32:32 pointer */
243 } else if ( u
->operand
[ 0 ].size
== SZ_DP
) {
244 u
->operand
[ 0 ].size
= 32;
251 /* resolve 3dnow weirdness. */
252 } else if ( u
->mnemonic
== UD_I3dnow
) {
253 u
->mnemonic
= ud_itab
[ u
->le
->table
[ ud_inp_curr( u
) ] ].mnemonic
;
255 /* SWAPGS is only valid in 64bits mode */
256 if ( u
->mnemonic
== UD_Iswapgs
&& u
->dis_mode
!= 64 ) {
261 if (u
->mnemonic
== UD_Ixchg
) {
262 if ((u
->operand
[0].type
== UD_OP_REG
&& u
->operand
[0].base
== UD_R_AX
&&
263 u
->operand
[1].type
== UD_OP_REG
&& u
->operand
[1].base
== UD_R_AX
) ||
264 (u
->operand
[0].type
== UD_OP_REG
&& u
->operand
[0].base
== UD_R_EAX
&&
265 u
->operand
[1].type
== UD_OP_REG
&& u
->operand
[1].base
== UD_R_EAX
)) {
266 u
->operand
[0].type
= UD_NONE
;
267 u
->operand
[1].type
= UD_NONE
;
268 u
->mnemonic
= UD_Inop
;
272 if (u
->mnemonic
== UD_Inop
&& u
->pfx_rep
) {
274 u
->mnemonic
= UD_Ipause
;
280 /* -----------------------------------------------------------------------------
281 * decode_a()- Decodes operands of the type seg:offset
282 * -----------------------------------------------------------------------------
285 decode_a(struct ud
* u
, struct ud_operand
*op
)
287 if (u
->opr_mode
== 16) {
289 op
->type
= UD_OP_PTR
;
291 op
->lval
.ptr
.off
= ud_inp_uint16(u
);
292 op
->lval
.ptr
.seg
= ud_inp_uint16(u
);
295 op
->type
= UD_OP_PTR
;
297 op
->lval
.ptr
.off
= ud_inp_uint32(u
);
298 op
->lval
.ptr
.seg
= ud_inp_uint16(u
);
302 /* -----------------------------------------------------------------------------
303 * decode_gpr() - Returns decoded General Purpose Register
304 * -----------------------------------------------------------------------------
307 decode_gpr(register struct ud
* u
, unsigned int s
, unsigned char rm
)
309 s
= resolve_operand_size(u
, s
);
313 return UD_R_RAX
+ rm
;
316 return UD_R_EAX
+ rm
;
321 if (u
->dis_mode
== 64 && u
->pfx_rex
) {
323 return UD_R_SPL
+ (rm
-4);
325 } else return UD_R_AL
+ rm
;
331 /* -----------------------------------------------------------------------------
332 * resolve_gpr64() - 64bit General Purpose Register-Selection.
333 * -----------------------------------------------------------------------------
336 resolve_gpr64(struct ud
* u
, enum ud_operand_code gpr_op
, enum ud_operand_size
* size
)
338 if (gpr_op
>= OP_rAXr8
&& gpr_op
<= OP_rDIr15
)
339 gpr_op
= (gpr_op
- OP_rAXr8
) | (REX_B(u
->pfx_rex
) << 3);
340 else gpr_op
= (gpr_op
- OP_rAX
);
342 if (u
->opr_mode
== 16) {
344 return gpr_op
+ UD_R_AX
;
346 if (u
->dis_mode
== 32 ||
347 (u
->opr_mode
== 32 && ! (REX_W(u
->pfx_rex
) || u
->default64
))) {
349 return gpr_op
+ UD_R_EAX
;
353 return gpr_op
+ UD_R_RAX
;
356 /* -----------------------------------------------------------------------------
357 * resolve_gpr32 () - 32bit General Purpose Register-Selection.
358 * -----------------------------------------------------------------------------
361 resolve_gpr32(struct ud
* u
, enum ud_operand_code gpr_op
)
363 gpr_op
= gpr_op
- OP_eAX
;
365 if (u
->opr_mode
== 16)
366 return gpr_op
+ UD_R_AX
;
368 return gpr_op
+ UD_R_EAX
;
371 /* -----------------------------------------------------------------------------
372 * resolve_reg() - Resolves the register type
373 * -----------------------------------------------------------------------------
376 resolve_reg(struct ud
* u
, unsigned int type
, unsigned char i
)
379 case T_MMX
: return UD_R_MM0
+ (i
& 7);
380 case T_XMM
: return UD_R_XMM0
+ i
;
381 case T_CRG
: return UD_R_CR0
+ i
;
382 case T_DBG
: return UD_R_DR0
+ i
;
385 * Only 6 segment registers, anything else is an error.
390 return UD_R_ES
+ (i
& 7);
394 default: return UD_NONE
;
398 /* -----------------------------------------------------------------------------
399 * decode_imm() - Decodes Immediate values.
400 * -----------------------------------------------------------------------------
403 decode_imm(struct ud
* u
, unsigned int s
, struct ud_operand
*op
)
405 op
->size
= resolve_operand_size(u
, s
);
406 op
->type
= UD_OP_IMM
;
409 case 8: op
->lval
.sbyte
= ud_inp_uint8(u
); break;
410 case 16: op
->lval
.uword
= ud_inp_uint16(u
); break;
411 case 32: op
->lval
.udword
= ud_inp_uint32(u
); break;
412 case 64: op
->lval
.uqword
= ud_inp_uint64(u
); break;
421 * Decodes reg field of mod/rm byte
425 decode_modrm_reg(struct ud
*u
,
426 struct ud_operand
*operand
,
430 uint8_t reg
= (REX_R(u
->pfx_rex
) << 3) | MODRM_REG(modrm(u
));
431 operand
->type
= UD_OP_REG
;
432 operand
->size
= resolve_operand_size(u
, size
);
435 operand
->base
= decode_gpr(u
, operand
->size
, reg
);
437 operand
->base
= resolve_reg(u
, type
, reg
);
445 * Decodes rm field of mod/rm byte
449 decode_modrm_rm(struct ud
*u
,
450 struct ud_operand
*op
,
455 unsigned char mod
, rm
, reg
;
457 /* get mod, r/m and reg fields */
458 mod
= MODRM_MOD(modrm(u
));
459 rm
= (REX_B(u
->pfx_rex
) << 3) | MODRM_RM(modrm(u
));
460 reg
= (REX_R(u
->pfx_rex
) << 3) | MODRM_REG(modrm(u
));
464 op
->size
= resolve_operand_size(u
, size
);
467 * If mod is 11b, then the modrm.rm specifies a register.
471 op
->type
= UD_OP_REG
;
473 op
->base
= decode_gpr(u
, op
->size
, rm
);
475 op
->base
= resolve_reg(u
, type
, (REX_B(u
->pfx_rex
) << 3) | (rm
& 7));
482 * !11 => Memory Address
484 op
->type
= UD_OP_MEM
;
486 if (u
->adr_mode
== 64) {
487 op
->base
= UD_R_RAX
+ rm
;
490 } else if (mod
== 2) {
492 } else if (mod
== 0 && (rm
& 7) == 5) {
499 * Scale-Index-Base (SIB)
504 op
->scale
= (1 << SIB_S(ud_inp_curr(u
))) & ~1;
505 op
->index
= UD_R_RAX
+ (SIB_I(ud_inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
506 op
->base
= UD_R_RAX
+ (SIB_B(ud_inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
508 /* special conditions for base reference */
509 if (op
->index
== UD_R_RSP
) {
514 if (op
->base
== UD_R_RBP
|| op
->base
== UD_R_R13
) {
525 } else if (u
->adr_mode
== 32) {
526 op
->base
= UD_R_EAX
+ rm
;
529 } else if (mod
== 2) {
531 } else if (mod
== 0 && rm
== 5) {
538 /* Scale-Index-Base (SIB) */
542 op
->scale
= (1 << SIB_S(ud_inp_curr(u
))) & ~1;
543 op
->index
= UD_R_EAX
+ (SIB_I(ud_inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
544 op
->base
= UD_R_EAX
+ (SIB_B(ud_inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
546 if (op
->index
== UD_R_ESP
) {
551 /* special condition for base reference */
552 if (op
->base
== UD_R_EBP
) {
564 const unsigned int bases
[] = { UD_R_BX
, UD_R_BX
, UD_R_BP
, UD_R_BP
,
565 UD_R_SI
, UD_R_DI
, UD_R_BP
, UD_R_BX
};
566 const unsigned int indices
[] = { UD_R_SI
, UD_R_DI
, UD_R_SI
, UD_R_DI
,
567 UD_NONE
, UD_NONE
, UD_NONE
, UD_NONE
};
568 op
->base
= bases
[rm
& 7];
569 op
->index
= indices
[rm
& 7];
570 if (mod
== 0 && rm
== 6) {
573 } else if (mod
== 1) {
575 } else if (mod
== 2) {
581 * extract offset, if any
583 switch (op
->offset
) {
584 case 8 : op
->lval
.ubyte
= ud_inp_uint8(u
); break;
585 case 16: op
->lval
.uword
= ud_inp_uint16(u
); break;
586 case 32: op
->lval
.udword
= ud_inp_uint32(u
); break;
587 case 64: op
->lval
.uqword
= ud_inp_uint64(u
); break;
592 /* -----------------------------------------------------------------------------
593 * decode_o() - Decodes offset
594 * -----------------------------------------------------------------------------
597 decode_o(struct ud
* u
, unsigned int s
, struct ud_operand
*op
)
599 switch (u
->adr_mode
) {
602 op
->lval
.uqword
= ud_inp_uint64(u
);
606 op
->lval
.udword
= ud_inp_uint32(u
);
610 op
->lval
.uword
= ud_inp_uint16(u
);
615 op
->type
= UD_OP_MEM
;
616 op
->size
= resolve_operand_size(u
, s
);
619 /* -----------------------------------------------------------------------------
620 * decode_operands() - Disassembles Operands.
621 * -----------------------------------------------------------------------------
624 decode_operand(struct ud
*u
,
625 struct ud_operand
*operand
,
626 enum ud_operand_code type
,
631 decode_a(u
, operand
);
634 if (MODRM_MOD(modrm(u
)) == 3) {
635 decode_modrm_rm(u
, operand
, T_GPR
,
636 size
== SZ_DY
? SZ_MDQ
: SZ_V
);
637 } else if (size
== SZ_WV
) {
638 decode_modrm_rm( u
, operand
, T_GPR
, SZ_W
);
639 } else if (size
== SZ_BV
) {
640 decode_modrm_rm( u
, operand
, T_GPR
, SZ_B
);
641 } else if (size
== SZ_DY
) {
642 decode_modrm_rm( u
, operand
, T_GPR
, SZ_D
);
644 ASSERT(!"unexpected size");
648 if (MODRM_MOD(modrm(u
)) == 3) {
651 /* intended fall through */
653 decode_modrm_rm(u
, operand
, T_GPR
, size
);
656 decode_modrm_reg(u
, operand
, T_GPR
, size
);
659 decode_imm(u
, size
, operand
);
662 operand
->type
= UD_OP_CONST
;
663 operand
->lval
.udword
= 1;
666 if (MODRM_MOD(modrm(u
)) != 3) {
669 decode_modrm_rm(u
, operand
, T_MMX
, size
);
672 decode_modrm_reg(u
, operand
, T_MMX
, size
);
675 if (MODRM_MOD(modrm(u
)) != 3) {
678 /* intended fall through */
680 decode_modrm_rm(u
, operand
, T_XMM
, size
);
683 decode_modrm_reg(u
, operand
, T_XMM
, size
);
686 decode_modrm_reg(u
, operand
, T_SEG
, size
);
696 operand
->type
= UD_OP_REG
;
697 operand
->base
= UD_R_AL
+ (type
- OP_AL
);
701 operand
->type
= UD_OP_REG
;
702 operand
->base
= UD_R_DX
;
706 decode_o(u
, size
, operand
);
724 operand
->type
= UD_OP_REG
;
725 operand
->base
= resolve_gpr64(u
, type
, &operand
->size
);
735 ud_type_t gpr
= (type
- OP_ALr8b
) + UD_R_AL
736 + (REX_B(u
->pfx_rex
) << 3);
737 if (UD_R_AH
<= gpr
&& u
->pfx_rex
) {
740 operand
->type
= UD_OP_REG
;
752 operand
->type
= UD_OP_REG
;
753 operand
->base
= resolve_gpr32(u
, type
);
754 operand
->size
= u
->opr_mode
== 16 ? 16 : 32;
762 /* in 64bits mode, only fs and gs are allowed */
763 if (u
->dis_mode
== 64) {
764 if (type
!= OP_FS
&& type
!= OP_GS
) {
768 operand
->type
= UD_OP_REG
;
769 operand
->base
= (type
- OP_ES
) + UD_R_ES
;
773 decode_imm(u
, size
, operand
);
774 operand
->type
= UD_OP_JIMM
;
777 decode_modrm_rm(u
, operand
, T_MMX
, size
);
780 decode_modrm_rm(u
, operand
, T_GPR
, size
);
783 decode_modrm_reg(u
, operand
, T_CRG
, size
);
786 decode_modrm_reg(u
, operand
, T_DBG
, size
);
789 operand
->type
= UD_OP_CONST
;
790 operand
->lval
.sbyte
= 3;
800 operand
->type
= UD_OP_REG
;
801 operand
->base
= (type
- OP_ST0
) + UD_R_ST0
;
805 operand
->type
= UD_OP_REG
;
806 operand
->base
= UD_R_AX
;
810 operand
->type
= UD_NONE
;
820 * Disassemble upto 3 operands of the current instruction being
821 * disassembled. By the end of the function, the operand fields
822 * of the ud structure will have been filled.
825 decode_operands(struct ud
* u
)
827 decode_operand(u
, &u
->operand
[0],
828 u
->itab_entry
->operand1
.type
,
829 u
->itab_entry
->operand1
.size
);
830 decode_operand(u
, &u
->operand
[1],
831 u
->itab_entry
->operand2
.type
,
832 u
->itab_entry
->operand2
.size
);
833 decode_operand(u
, &u
->operand
[2],
834 u
->itab_entry
->operand3
.type
,
835 u
->itab_entry
->operand3
.size
);
839 /* -----------------------------------------------------------------------------
840 * clear_insn() - clear instruction structure
841 * -----------------------------------------------------------------------------
844 clear_insn(register struct ud
* u
)
856 u
->mnemonic
= UD_Inone
;
857 u
->itab_entry
= NULL
;
860 memset( &u
->operand
[ 0 ], 0, sizeof( struct ud_operand
) );
861 memset( &u
->operand
[ 1 ], 0, sizeof( struct ud_operand
) );
862 memset( &u
->operand
[ 2 ], 0, sizeof( struct ud_operand
) );
866 resolve_mode( struct ud
* u
)
868 /* if in error state, bail out */
869 if ( u
->error
) return -1;
871 /* propagate prefix effects */
872 if ( u
->dis_mode
== 64 ) { /* set 64bit-mode flags */
874 /* Check validity of instruction m64 */
875 if ( P_INV64( u
->itab_entry
->prefix
) ) {
880 /* effective rex prefix is the effective mask for the
881 * instruction hard-coded in the opcode map.
883 u
->pfx_rex
= ( u
->pfx_rex
& 0x40 ) |
884 ( u
->pfx_rex
& REX_PFX_MASK( u
->itab_entry
->prefix
) );
886 /* whether this instruction has a default operand size of
887 * 64bit, also hardcoded into the opcode map.
889 u
->default64
= P_DEF64( u
->itab_entry
->prefix
);
890 /* calculate effective operand size */
891 if ( REX_W( u
->pfx_rex
) ) {
893 } else if ( u
->pfx_opr
) {
896 /* unless the default opr size of instruction is 64,
897 * the effective operand size in the absence of rex.w
900 u
->opr_mode
= ( u
->default64
) ? 64 : 32;
903 /* calculate effective address size */
904 u
->adr_mode
= (u
->pfx_adr
) ? 32 : 64;
905 } else if ( u
->dis_mode
== 32 ) { /* set 32bit-mode flags */
906 u
->opr_mode
= ( u
->pfx_opr
) ? 16 : 32;
907 u
->adr_mode
= ( u
->pfx_adr
) ? 16 : 32;
908 } else if ( u
->dis_mode
== 16 ) { /* set 16bit-mode flags */
909 u
->opr_mode
= ( u
->pfx_opr
) ? 32 : 16;
910 u
->adr_mode
= ( u
->pfx_adr
) ? 32 : 16;
913 /* These flags determine which operand to apply the operand size
916 u
->c1
= ( P_C1( u
->itab_entry
->prefix
) ) ? 1 : 0;
917 u
->c2
= ( P_C2( u
->itab_entry
->prefix
) ) ? 1 : 0;
918 u
->c3
= ( P_C3( u
->itab_entry
->prefix
) ) ? 1 : 0;
920 /* set flags for implicit addressing */
921 u
->implicit_addr
= P_IMPADDR( u
->itab_entry
->prefix
);
926 static int gen_hex( struct ud
*u
)
929 unsigned char *src_ptr
= ud_inp_sess( u
);
932 /* bail out if in error stat. */
933 if ( u
->error
) return -1;
934 /* output buffer pointe */
935 src_hex
= ( char* ) u
->insn_hexcode
;
936 /* for each byte used to decode instruction */
937 for ( i
= 0; i
< u
->inp_ctr
; ++i
, ++src_ptr
) {
938 sprintf( src_hex
, "%02x", *src_ptr
& 0xFF );
946 decode_insn(struct ud
*u
, uint16_t ptr
)
948 ASSERT((ptr
& 0x8000) == 0);
949 u
->itab_entry
= &ud_itab
[ ptr
];
950 u
->mnemonic
= u
->itab_entry
->mnemonic
;
951 return (resolve_mode(u
) == 0 &&
952 decode_operands(u
) == 0 &&
953 resolve_mnemonic(u
) == 0) ? 0 : -1;
960 * Decoding 3dnow is a little tricky because of its strange opcode
961 * structure. The final opcode disambiguation depends on the last
962 * byte that comes after the operands have been decoded. Fortunately,
963 * all 3dnow instructions have the same set of operand types. So we
964 * go ahead and decode the instruction by picking an arbitrarily chosen
965 * valid entry in the table, decode the operands, and read the final
966 * byte to resolve the menmonic.
969 decode_3dnow(struct ud
* u
)
972 ASSERT(u
->le
->type
== UD_TAB__OPC_3DNOW
);
973 ASSERT(u
->le
->table
[0xc] != 0);
974 decode_insn(u
, u
->le
->table
[0xc]);
979 ptr
= u
->le
->table
[ud_inp_curr(u
)];
980 ASSERT((ptr
& 0x8000) == 0);
981 u
->mnemonic
= ud_itab
[ptr
].mnemonic
;
987 decode_ssepfx(struct ud
*u
)
989 uint8_t idx
= ((u
->pfx_insn
& 0xf) + 1) / 2;
990 if (u
->le
->table
[idx
] == 0) {
993 if (idx
&& u
->le
->table
[idx
] != 0) {
995 * "Consume" the prefix as a part of the opcode, so it is no
996 * longer exported as an instruction prefix.
998 switch (u
->pfx_insn
) {
1011 return decode_ext(u
, u
->le
->table
[idx
]);
1018 * Decode opcode extensions (if any)
1021 decode_ext(struct ud
*u
, uint16_t ptr
)
1024 if ((ptr
& 0x8000) == 0) {
1025 return decode_insn(u
, ptr
);
1027 u
->le
= &ud_lookup_table_list
[(~0x8000 & ptr
)];
1028 if (u
->le
->type
== UD_TAB__OPC_3DNOW
) {
1029 return decode_3dnow(u
);
1032 switch (u
->le
->type
) {
1033 case UD_TAB__OPC_MOD
:
1034 /* !11 = 0, 11 = 1 */
1035 idx
= (MODRM_MOD(modrm(u
)) + 1) / 4;
1037 /* disassembly mode/operand size/address size based tables.
1038 * 16 = 0,, 32 = 1, 64 = 2
1040 case UD_TAB__OPC_MODE
:
1041 idx
= u
->dis_mode
/ 32;
1043 case UD_TAB__OPC_OSIZE
:
1044 idx
= eff_opr_mode(u
->dis_mode
, REX_W(u
->pfx_rex
), u
->pfx_opr
) / 32;
1046 case UD_TAB__OPC_ASIZE
:
1047 idx
= eff_adr_mode(u
->dis_mode
, u
->pfx_adr
) / 32;
1049 case UD_TAB__OPC_X87
:
1050 idx
= modrm(u
) - 0xC0;
1052 case UD_TAB__OPC_VENDOR
:
1053 if (u
->vendor
== UD_VENDOR_ANY
) {
1054 /* choose a valid entry */
1055 idx
= (u
->le
->table
[idx
] != 0) ? 0 : 1;
1056 } else if (u
->vendor
== UD_VENDOR_AMD
) {
1062 case UD_TAB__OPC_RM
:
1063 idx
= MODRM_RM(modrm(u
));
1065 case UD_TAB__OPC_REG
:
1066 idx
= MODRM_REG(modrm(u
));
1068 case UD_TAB__OPC_SSE
:
1069 return decode_ssepfx(u
);
1071 ASSERT(!"not reached");
1075 return decode_ext(u
, u
->le
->table
[idx
]);
1080 decode_opcode(struct ud
*u
)
1083 ASSERT(u
->le
->type
== UD_TAB__OPC_TABLE
);
1088 ptr
= u
->le
->table
[ud_inp_curr(u
)];
1090 u
->le
= &ud_lookup_table_list
[ptr
& ~0x8000];
1091 if (u
->le
->type
== UD_TAB__OPC_TABLE
) {
1092 return decode_opcode(u
);
1095 return decode_ext(u
, ptr
);
1099 /* =============================================================================
1100 * ud_decode() - Instruction decoder. Returns the number of bytes decoded.
1101 * =============================================================================
1104 ud_decode(struct ud
*u
)
1108 u
->le
= &ud_lookup_table_list
[0];
1109 u
->error
= decode_prefixes(u
) == -1 ||
1110 decode_opcode(u
) == -1 ||
1112 /* Handle decode error. */
1114 /* clear out the decode data. */
1116 /* mark the sequence of bytes as invalid. */
1117 u
->itab_entry
= & s_ie__invalid
;
1118 u
->mnemonic
= u
->itab_entry
->mnemonic
;
1121 /* maybe this stray segment override byte
1122 * should be spewed out?
1124 if ( !P_SEG( u
->itab_entry
->prefix
) &&
1125 u
->operand
[0].type
!= UD_OP_MEM
&&
1126 u
->operand
[1].type
!= UD_OP_MEM
)
1129 u
->insn_offset
= u
->pc
; /* set offset of instruction */
1130 u
->insn_fill
= 0; /* set translation buffer index to 0 */
1131 u
->pc
+= u
->inp_ctr
; /* move program counter by bytes decoded */
1132 gen_hex( u
); /* generate hex code */
1134 /* return number of bytes disassembled. */
1139 vim: set ts=2 sw=2 expandtab
1142 #endif // USE(UDIS86)