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
));
462 op
->size
= resolve_operand_size(u
, size
);
465 * If mod is 11b, then the modrm.rm specifies a register.
469 op
->type
= UD_OP_REG
;
471 op
->base
= decode_gpr(u
, op
->size
, rm
);
473 op
->base
= resolve_reg(u
, type
, (REX_B(u
->pfx_rex
) << 3) | (rm
& 7));
480 * !11 => Memory Address
482 op
->type
= UD_OP_MEM
;
484 if (u
->adr_mode
== 64) {
485 op
->base
= UD_R_RAX
+ rm
;
488 } else if (mod
== 2) {
490 } else if (mod
== 0 && (rm
& 7) == 5) {
497 * Scale-Index-Base (SIB)
502 op
->scale
= (1 << SIB_S(ud_inp_curr(u
))) & ~1;
503 op
->index
= UD_R_RAX
+ (SIB_I(ud_inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
504 op
->base
= UD_R_RAX
+ (SIB_B(ud_inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
506 /* special conditions for base reference */
507 if (op
->index
== UD_R_RSP
) {
512 if (op
->base
== UD_R_RBP
|| op
->base
== UD_R_R13
) {
523 } else if (u
->adr_mode
== 32) {
524 op
->base
= UD_R_EAX
+ rm
;
527 } else if (mod
== 2) {
529 } else if (mod
== 0 && rm
== 5) {
536 /* Scale-Index-Base (SIB) */
540 op
->scale
= (1 << SIB_S(ud_inp_curr(u
))) & ~1;
541 op
->index
= UD_R_EAX
+ (SIB_I(ud_inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
542 op
->base
= UD_R_EAX
+ (SIB_B(ud_inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
544 if (op
->index
== UD_R_ESP
) {
549 /* special condition for base reference */
550 if (op
->base
== UD_R_EBP
) {
562 const unsigned int bases
[] = { UD_R_BX
, UD_R_BX
, UD_R_BP
, UD_R_BP
,
563 UD_R_SI
, UD_R_DI
, UD_R_BP
, UD_R_BX
};
564 const unsigned int indices
[] = { UD_R_SI
, UD_R_DI
, UD_R_SI
, UD_R_DI
,
565 UD_NONE
, UD_NONE
, UD_NONE
, UD_NONE
};
566 op
->base
= bases
[rm
& 7];
567 op
->index
= indices
[rm
& 7];
568 if (mod
== 0 && rm
== 6) {
571 } else if (mod
== 1) {
573 } else if (mod
== 2) {
579 * extract offset, if any
581 switch (op
->offset
) {
582 case 8 : op
->lval
.ubyte
= ud_inp_uint8(u
); break;
583 case 16: op
->lval
.uword
= ud_inp_uint16(u
); break;
584 case 32: op
->lval
.udword
= ud_inp_uint32(u
); break;
585 case 64: op
->lval
.uqword
= ud_inp_uint64(u
); break;
590 /* -----------------------------------------------------------------------------
591 * decode_o() - Decodes offset
592 * -----------------------------------------------------------------------------
595 decode_o(struct ud
* u
, unsigned int s
, struct ud_operand
*op
)
597 switch (u
->adr_mode
) {
600 op
->lval
.uqword
= ud_inp_uint64(u
);
604 op
->lval
.udword
= ud_inp_uint32(u
);
608 op
->lval
.uword
= ud_inp_uint16(u
);
613 op
->type
= UD_OP_MEM
;
614 op
->size
= resolve_operand_size(u
, s
);
617 /* -----------------------------------------------------------------------------
618 * decode_operands() - Disassembles Operands.
619 * -----------------------------------------------------------------------------
622 decode_operand(struct ud
*u
,
623 struct ud_operand
*operand
,
624 enum ud_operand_code type
,
629 decode_a(u
, operand
);
632 if (MODRM_MOD(modrm(u
)) == 3) {
633 decode_modrm_rm(u
, operand
, T_GPR
,
634 size
== SZ_DY
? SZ_MDQ
: SZ_V
);
635 } else if (size
== SZ_WV
) {
636 decode_modrm_rm( u
, operand
, T_GPR
, SZ_W
);
637 } else if (size
== SZ_BV
) {
638 decode_modrm_rm( u
, operand
, T_GPR
, SZ_B
);
639 } else if (size
== SZ_DY
) {
640 decode_modrm_rm( u
, operand
, T_GPR
, SZ_D
);
642 ASSERT(!"unexpected size");
646 if (MODRM_MOD(modrm(u
)) == 3) {
649 /* intended fall through */
651 decode_modrm_rm(u
, operand
, T_GPR
, size
);
655 decode_modrm_reg(u
, operand
, T_GPR
, size
);
658 decode_imm(u
, size
, operand
);
661 operand
->type
= UD_OP_CONST
;
662 operand
->lval
.udword
= 1;
665 if (MODRM_MOD(modrm(u
)) != 3) {
668 decode_modrm_rm(u
, operand
, T_MMX
, size
);
671 decode_modrm_reg(u
, operand
, T_MMX
, size
);
674 if (MODRM_MOD(modrm(u
)) != 3) {
677 /* intended fall through */
679 decode_modrm_rm(u
, operand
, T_XMM
, size
);
682 decode_modrm_reg(u
, operand
, T_XMM
, size
);
685 decode_modrm_reg(u
, operand
, T_SEG
, size
);
695 operand
->type
= UD_OP_REG
;
696 operand
->base
= UD_R_AL
+ (type
- OP_AL
);
700 operand
->type
= UD_OP_REG
;
701 operand
->base
= UD_R_DX
;
705 decode_o(u
, size
, operand
);
723 operand
->type
= UD_OP_REG
;
724 operand
->base
= resolve_gpr64(u
, type
, &operand
->size
);
734 ud_type_t gpr
= (type
- OP_ALr8b
) + UD_R_AL
735 + (REX_B(u
->pfx_rex
) << 3);
736 if (UD_R_AH
<= gpr
&& u
->pfx_rex
) {
739 operand
->type
= UD_OP_REG
;
751 operand
->type
= UD_OP_REG
;
752 operand
->base
= resolve_gpr32(u
, type
);
753 operand
->size
= u
->opr_mode
== 16 ? 16 : 32;
761 /* in 64bits mode, only fs and gs are allowed */
762 if (u
->dis_mode
== 64) {
763 if (type
!= OP_FS
&& type
!= OP_GS
) {
767 operand
->type
= UD_OP_REG
;
768 operand
->base
= (type
- OP_ES
) + UD_R_ES
;
772 decode_imm(u
, size
, operand
);
773 operand
->type
= UD_OP_JIMM
;
776 decode_modrm_rm(u
, operand
, T_MMX
, size
);
779 decode_modrm_rm(u
, operand
, T_GPR
, size
);
782 decode_modrm_reg(u
, operand
, T_CRG
, size
);
785 decode_modrm_reg(u
, operand
, T_DBG
, size
);
788 operand
->type
= UD_OP_CONST
;
789 operand
->lval
.sbyte
= 3;
799 operand
->type
= UD_OP_REG
;
800 operand
->base
= (type
- OP_ST0
) + UD_R_ST0
;
804 operand
->type
= UD_OP_REG
;
805 operand
->base
= UD_R_AX
;
809 operand
->type
= UD_NONE
;
819 * Disassemble upto 3 operands of the current instruction being
820 * disassembled. By the end of the function, the operand fields
821 * of the ud structure will have been filled.
824 decode_operands(struct ud
* u
)
826 decode_operand(u
, &u
->operand
[0],
827 u
->itab_entry
->operand1
.type
,
828 u
->itab_entry
->operand1
.size
);
829 decode_operand(u
, &u
->operand
[1],
830 u
->itab_entry
->operand2
.type
,
831 u
->itab_entry
->operand2
.size
);
832 decode_operand(u
, &u
->operand
[2],
833 u
->itab_entry
->operand3
.type
,
834 u
->itab_entry
->operand3
.size
);
838 /* -----------------------------------------------------------------------------
839 * clear_insn() - clear instruction structure
840 * -----------------------------------------------------------------------------
843 clear_insn(register struct ud
* u
)
855 u
->mnemonic
= UD_Inone
;
856 u
->itab_entry
= NULL
;
859 memset( &u
->operand
[ 0 ], 0, sizeof( struct ud_operand
) );
860 memset( &u
->operand
[ 1 ], 0, sizeof( struct ud_operand
) );
861 memset( &u
->operand
[ 2 ], 0, sizeof( struct ud_operand
) );
865 resolve_mode( struct ud
* u
)
867 /* if in error state, bail out */
868 if ( u
->error
) return -1;
870 /* propagate prefix effects */
871 if ( u
->dis_mode
== 64 ) { /* set 64bit-mode flags */
873 /* Check validity of instruction m64 */
874 if ( P_INV64( u
->itab_entry
->prefix
) ) {
879 /* effective rex prefix is the effective mask for the
880 * instruction hard-coded in the opcode map.
882 u
->pfx_rex
= ( u
->pfx_rex
& 0x40 ) |
883 ( u
->pfx_rex
& REX_PFX_MASK( u
->itab_entry
->prefix
) );
885 /* whether this instruction has a default operand size of
886 * 64bit, also hardcoded into the opcode map.
888 u
->default64
= P_DEF64( u
->itab_entry
->prefix
);
889 /* calculate effective operand size */
890 if ( REX_W( u
->pfx_rex
) ) {
892 } else if ( u
->pfx_opr
) {
895 /* unless the default opr size of instruction is 64,
896 * the effective operand size in the absence of rex.w
899 u
->opr_mode
= ( u
->default64
) ? 64 : 32;
902 /* calculate effective address size */
903 u
->adr_mode
= (u
->pfx_adr
) ? 32 : 64;
904 } else if ( u
->dis_mode
== 32 ) { /* set 32bit-mode flags */
905 u
->opr_mode
= ( u
->pfx_opr
) ? 16 : 32;
906 u
->adr_mode
= ( u
->pfx_adr
) ? 16 : 32;
907 } else if ( u
->dis_mode
== 16 ) { /* set 16bit-mode flags */
908 u
->opr_mode
= ( u
->pfx_opr
) ? 32 : 16;
909 u
->adr_mode
= ( u
->pfx_adr
) ? 32 : 16;
912 /* These flags determine which operand to apply the operand size
915 u
->c1
= ( P_C1( u
->itab_entry
->prefix
) ) ? 1 : 0;
916 u
->c2
= ( P_C2( u
->itab_entry
->prefix
) ) ? 1 : 0;
917 u
->c3
= ( P_C3( u
->itab_entry
->prefix
) ) ? 1 : 0;
919 /* set flags for implicit addressing */
920 u
->implicit_addr
= P_IMPADDR( u
->itab_entry
->prefix
);
925 static int gen_hex( struct ud
*u
)
928 unsigned char *src_ptr
= ud_inp_sess( u
);
931 /* bail out if in error stat. */
932 if ( u
->error
) return -1;
933 /* output buffer pointe */
934 src_hex
= ( char* ) u
->insn_hexcode
;
935 /* for each byte used to decode instruction */
936 for ( i
= 0; i
< u
->inp_ctr
; ++i
, ++src_ptr
) {
937 sprintf( src_hex
, "%02x", *src_ptr
& 0xFF );
945 decode_insn(struct ud
*u
, uint16_t ptr
)
947 ASSERT((ptr
& 0x8000) == 0);
948 u
->itab_entry
= &ud_itab
[ ptr
];
949 u
->mnemonic
= u
->itab_entry
->mnemonic
;
950 return (resolve_mode(u
) == 0 &&
951 decode_operands(u
) == 0 &&
952 resolve_mnemonic(u
) == 0) ? 0 : -1;
959 * Decoding 3dnow is a little tricky because of its strange opcode
960 * structure. The final opcode disambiguation depends on the last
961 * byte that comes after the operands have been decoded. Fortunately,
962 * all 3dnow instructions have the same set of operand types. So we
963 * go ahead and decode the instruction by picking an arbitrarily chosen
964 * valid entry in the table, decode the operands, and read the final
965 * byte to resolve the menmonic.
968 decode_3dnow(struct ud
* u
)
971 ASSERT(u
->le
->type
== UD_TAB__OPC_3DNOW
);
972 ASSERT(u
->le
->table
[0xc] != 0);
973 decode_insn(u
, u
->le
->table
[0xc]);
978 ptr
= u
->le
->table
[ud_inp_curr(u
)];
979 ASSERT((ptr
& 0x8000) == 0);
980 u
->mnemonic
= ud_itab
[ptr
].mnemonic
;
986 decode_ssepfx(struct ud
*u
)
988 uint8_t idx
= ((u
->pfx_insn
& 0xf) + 1) / 2;
989 if (u
->le
->table
[idx
] == 0) {
992 if (idx
&& u
->le
->table
[idx
] != 0) {
994 * "Consume" the prefix as a part of the opcode, so it is no
995 * longer exported as an instruction prefix.
997 switch (u
->pfx_insn
) {
1010 return decode_ext(u
, u
->le
->table
[idx
]);
1017 * Decode opcode extensions (if any)
1020 decode_ext(struct ud
*u
, uint16_t ptr
)
1023 if ((ptr
& 0x8000) == 0) {
1024 return decode_insn(u
, ptr
);
1026 u
->le
= &ud_lookup_table_list
[(~0x8000 & ptr
)];
1027 if (u
->le
->type
== UD_TAB__OPC_3DNOW
) {
1028 return decode_3dnow(u
);
1031 switch (u
->le
->type
) {
1032 case UD_TAB__OPC_MOD
:
1033 /* !11 = 0, 11 = 1 */
1034 idx
= (MODRM_MOD(modrm(u
)) + 1) / 4;
1036 /* disassembly mode/operand size/address size based tables.
1037 * 16 = 0,, 32 = 1, 64 = 2
1039 case UD_TAB__OPC_MODE
:
1040 idx
= u
->dis_mode
/ 32;
1042 case UD_TAB__OPC_OSIZE
:
1043 idx
= eff_opr_mode(u
->dis_mode
, REX_W(u
->pfx_rex
), u
->pfx_opr
) / 32;
1045 case UD_TAB__OPC_ASIZE
:
1046 idx
= eff_adr_mode(u
->dis_mode
, u
->pfx_adr
) / 32;
1048 case UD_TAB__OPC_X87
:
1049 idx
= modrm(u
) - 0xC0;
1051 case UD_TAB__OPC_VENDOR
:
1052 if (u
->vendor
== UD_VENDOR_ANY
) {
1053 /* choose a valid entry */
1054 idx
= (u
->le
->table
[idx
] != 0) ? 0 : 1;
1055 } else if (u
->vendor
== UD_VENDOR_AMD
) {
1061 case UD_TAB__OPC_RM
:
1062 idx
= MODRM_RM(modrm(u
));
1064 case UD_TAB__OPC_REG
:
1065 idx
= MODRM_REG(modrm(u
));
1067 case UD_TAB__OPC_SSE
:
1068 return decode_ssepfx(u
);
1070 ASSERT(!"not reached");
1074 return decode_ext(u
, u
->le
->table
[idx
]);
1079 decode_opcode(struct ud
*u
)
1082 ASSERT(u
->le
->type
== UD_TAB__OPC_TABLE
);
1087 ptr
= u
->le
->table
[ud_inp_curr(u
)];
1089 u
->le
= &ud_lookup_table_list
[ptr
& ~0x8000];
1090 if (u
->le
->type
== UD_TAB__OPC_TABLE
) {
1091 return decode_opcode(u
);
1094 return decode_ext(u
, ptr
);
1098 /* =============================================================================
1099 * ud_decode() - Instruction decoder. Returns the number of bytes decoded.
1100 * =============================================================================
1103 ud_decode(struct ud
*u
)
1107 u
->le
= &ud_lookup_table_list
[0];
1108 u
->error
= decode_prefixes(u
) == -1 ||
1109 decode_opcode(u
) == -1 ||
1111 /* Handle decode error. */
1113 /* clear out the decode data. */
1115 /* mark the sequence of bytes as invalid. */
1116 u
->itab_entry
= & s_ie__invalid
;
1117 u
->mnemonic
= u
->itab_entry
->mnemonic
;
1120 /* maybe this stray segment override byte
1121 * should be spewed out?
1123 if ( !P_SEG( u
->itab_entry
->prefix
) &&
1124 u
->operand
[0].type
!= UD_OP_MEM
&&
1125 u
->operand
[1].type
!= UD_OP_MEM
)
1128 u
->insn_offset
= u
->pc
; /* set offset of instruction */
1129 u
->insn_fill
= 0; /* set translation buffer index to 0 */
1130 u
->pc
+= u
->inp_ctr
; /* move program counter by bytes decoded */
1131 gen_hex( u
); /* generate hex code */
1133 /* return number of bytes disassembled. */
1138 vim: set ts=2 sw=2 expandtab
1141 #endif // USE(UDIS86)