x86_insn.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cassert>
  4. #include "libdis.h"
  5. #ifdef _MSC_VER
  6. #define snprintf _snprintf
  7. #define inline __inline
  8. #endif
  9. int x86_insn_is_valid( x86_insn_t *insn ) {
  10. if ( insn && insn->type != insn_invalid && insn->size > 0 ) {
  11. return 1;
  12. }
  13. return 0;
  14. }
  15. bool x86_insn_t::is_valid( )
  16. {
  17. if ( this && this->type != insn_invalid && this->size > 0 )
  18. {
  19. return 1;
  20. }
  21. return 0;
  22. }
  23. /* Get Address: return the value of an offset operand, or the offset of
  24. * a segment:offset absolute address */
  25. uint32_t x86_insn_t::x86_get_address()
  26. {
  27. x86_oplist_t *op_lst;
  28. assert(this);
  29. if (! operands )
  30. {
  31. return 0;
  32. }
  33. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  34. if ( op_lst->op.type == op_offset ) {
  35. return op_lst->op.data.offset;
  36. } else if ( op_lst->op.type == op_absolute ) {
  37. if ( op_lst->op.datatype == op_descr16 ) {
  38. return (uint32_t)
  39. op_lst->op.data.absolute.offset.off16;
  40. }
  41. return op_lst->op.data.absolute.offset.off32;
  42. }
  43. }
  44. return 0;
  45. }
  46. /** Get Relative Offset: return as a sign-extended int32_t the near or far
  47. * relative offset operand, or 0 if there is none. There can be only one
  48. * relaive offset operand in an instruction. */
  49. int32_t x86_insn_t::x86_get_rel_offset( ) {
  50. x86_oplist_t *op_lst;
  51. assert(this);
  52. if (! operands ) {
  53. return 0;
  54. }
  55. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  56. if ( op_lst->op.type == op_relative_near ) {
  57. return (int32_t) op_lst->op.data.relative_near;
  58. } else if ( op_lst->op.type == op_relative_far ) {
  59. return op_lst->op.data.relative_far;
  60. }
  61. }
  62. return 0;
  63. }
  64. /** Get Branch Target: return the x86_op_t containing the target of
  65. * a jump or call operand, or NULL if there is no branch target.
  66. * Internally, a 'branch target' is defined as any operand with
  67. * Execute Access set. There can be only one branch target per instruction. */
  68. x86_op_t * x86_insn_t::x86_get_branch_target() {
  69. x86_oplist_t *op_lst;
  70. assert(this);
  71. if (! operands ) {
  72. return NULL;
  73. }
  74. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  75. if ( op_lst->op.access & op_execute ) {
  76. return &(op_lst->op);
  77. }
  78. }
  79. return NULL;
  80. }
  81. const x86_op_t * x86_insn_t::get_dest() const {
  82. x86_oplist_t *op_lst;
  83. assert(this);
  84. if ( ! operands ) {
  85. return NULL;
  86. }
  87. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  88. if ( op_lst->op.access & op_write)
  89. return &(op_lst->op);
  90. }
  91. return NULL;
  92. }
  93. /** \brief Get Immediate: return the x86_op_t containing the immediate operand
  94. for this instruction, or NULL if there is no immediate operand. There
  95. can be only one immediate operand per instruction */
  96. x86_op_t * x86_insn_t::x86_get_imm() {
  97. x86_oplist_t *op_lst;
  98. assert(this);
  99. if ( ! operands ) {
  100. return NULL;
  101. }
  102. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  103. if ( op_lst->op.type == op_immediate ) {
  104. return &(op_lst->op);
  105. }
  106. }
  107. return NULL;
  108. }
  109. #define IS_PROPER_IMM( x ) \
  110. x->op.type == op_immediate && ! (x->op.flags.op_hardcode)
  111. /** \brief if there is an immediate value in the instruction, return a pointer to it
  112. * Get Raw Immediate Data: returns a pointer to the immediate data encoded
  113. * in the instruction. This is useful for large data types [>32 bits] currently
  114. * not supported by libdisasm, or for determining if the disassembler
  115. * screwed up the conversion of the immediate data. Note that 'imm' in this
  116. * context refers to immediate data encoded at the end of an instruction as
  117. * detailed in the Intel Manual Vol II Chapter 2; it does not refer to the
  118. * 'op_imm' operand (the third operand in instructions like 'mul' */
  119. uint8_t *x86_insn_t::x86_get_raw_imm() {
  120. int size, offset;
  121. x86_op_t *op = NULL;
  122. assert(this);
  123. if (! operands ) {
  124. return(NULL);
  125. }
  126. /* a bit inelegant, but oh well... */
  127. if ( IS_PROPER_IMM( operands ) ) {
  128. op = &operands->op;
  129. } else if ( operands->next ) {
  130. if ( IS_PROPER_IMM( operands->next ) ) {
  131. op = &operands->next->op;
  132. } else if ( operands->next->next &&
  133. IS_PROPER_IMM( operands->next->next ) ) {
  134. op = &operands->next->next->op;
  135. }
  136. }
  137. if (! op ) {
  138. return( NULL );
  139. }
  140. /* immediate data is at the end of the insn */
  141. size = op->operand_size();
  142. offset = size - size;
  143. return( &bytes[offset] );
  144. }
  145. size_t x86_op_t::operand_size() const {
  146. switch (datatype ) {
  147. case op_byte: return 1;
  148. case op_word: return 2;
  149. case op_dword: return 4;
  150. case op_qword: return 8;
  151. case op_dqword: return 16;
  152. case op_sreal: return 4;
  153. case op_dreal: return 8;
  154. case op_extreal: return 10;
  155. case op_bcd: return 10;
  156. case op_ssimd: return 16;
  157. case op_dsimd: return 16;
  158. case op_sssimd: return 4;
  159. case op_sdsimd: return 8;
  160. case op_descr32: return 6;
  161. case op_descr16: return 4;
  162. case op_pdescr32: return 6;
  163. case op_pdescr16: return 6;
  164. case op_bounds16: return 4;
  165. case op_bounds32: return 8;
  166. case op_fpuenv16: return 14;
  167. case op_fpuenv32: return 28;
  168. case op_fpustate16: return 94;
  169. case op_fpustate32: return 108;
  170. case op_fpregset: return 512;
  171. case op_fpreg: return 10;
  172. case op_none: return 0;
  173. }
  174. return(4); /* default size */
  175. }
  176. void x86_insn_t::x86_set_insn_addr( uint32_t _addr ) {
  177. addr = _addr;
  178. }
  179. void x86_insn_t::x86_set_insn_offset( unsigned int _offset ){
  180. offset = _offset;
  181. }
  182. void x86_insn_t::x86_set_insn_function( void * func ){
  183. function = func;
  184. }
  185. void x86_insn_t::x86_set_insn_block( void * _block ){
  186. block = _block;
  187. }
  188. void x86_insn_t::x86_tag_insn(){
  189. tag = 1;
  190. }
  191. void x86_insn_t::x86_untag_insn(){
  192. tag = 0;
  193. }
  194. int x86_insn_t::x86_insn_is_tagged(){
  195. return tag;
  196. }