x86_insn.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. x86_op_t * x86_insn_t::get_dest() {
  82. x86_oplist_t *op_lst;
  83. assert(this);
  84. if ( ! operands ) {
  85. return NULL;
  86. }
  87. assert(this->x86_operand_count(op_dest)==1);
  88. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  89. if ( op_lst->op.access & op_write)
  90. return &(op_lst->op);
  91. }
  92. return NULL;
  93. }
  94. /** \brief Get Immediate: return the x86_op_t containing the immediate operand
  95. for this instruction, or NULL if there is no immediate operand. There
  96. can be only one immediate operand per instruction */
  97. x86_op_t * x86_insn_t::x86_get_imm() {
  98. x86_oplist_t *op_lst;
  99. assert(this);
  100. if ( ! operands ) {
  101. return NULL;
  102. }
  103. for (op_lst = operands; op_lst; op_lst = op_lst->next ) {
  104. if ( op_lst->op.type == op_immediate ) {
  105. return &(op_lst->op);
  106. }
  107. }
  108. return NULL;
  109. }
  110. #define IS_PROPER_IMM( x ) \
  111. x->op.type == op_immediate && ! (x->op.flags.op_hardcode)
  112. /** \brief if there is an immediate value in the instruction, return a pointer to it
  113. * Get Raw Immediate Data: returns a pointer to the immediate data encoded
  114. * in the instruction. This is useful for large data types [>32 bits] currently
  115. * not supported by libdisasm, or for determining if the disassembler
  116. * screwed up the conversion of the immediate data. Note that 'imm' in this
  117. * context refers to immediate data encoded at the end of an instruction as
  118. * detailed in the Intel Manual Vol II Chapter 2; it does not refer to the
  119. * 'op_imm' operand (the third operand in instructions like 'mul' */
  120. uint8_t *x86_insn_t::x86_get_raw_imm() {
  121. int size, offset;
  122. x86_op_t *op = NULL;
  123. assert(this);
  124. if (! operands ) {
  125. return(NULL);
  126. }
  127. /* a bit inelegant, but oh well... */
  128. if ( IS_PROPER_IMM( operands ) ) {
  129. op = &operands->op;
  130. } else if ( operands->next ) {
  131. if ( IS_PROPER_IMM( operands->next ) ) {
  132. op = &operands->next->op;
  133. } else if ( operands->next->next &&
  134. IS_PROPER_IMM( operands->next->next ) ) {
  135. op = &operands->next->next->op;
  136. }
  137. }
  138. if (! op ) {
  139. return( NULL );
  140. }
  141. /* immediate data is at the end of the insn */
  142. size = op->operand_size();
  143. offset = size - size;
  144. return( &bytes[offset] );
  145. }
  146. size_t x86_op_t::operand_size() {
  147. switch (datatype ) {
  148. case op_byte: return 1;
  149. case op_word: return 2;
  150. case op_dword: return 4;
  151. case op_qword: return 8;
  152. case op_dqword: return 16;
  153. case op_sreal: return 4;
  154. case op_dreal: return 8;
  155. case op_extreal: return 10;
  156. case op_bcd: return 10;
  157. case op_ssimd: return 16;
  158. case op_dsimd: return 16;
  159. case op_sssimd: return 4;
  160. case op_sdsimd: return 8;
  161. case op_descr32: return 6;
  162. case op_descr16: return 4;
  163. case op_pdescr32: return 6;
  164. case op_pdescr16: return 6;
  165. case op_bounds16: return 4;
  166. case op_bounds32: return 8;
  167. case op_fpuenv16: return 14;
  168. case op_fpuenv32: return 28;
  169. case op_fpustate16: return 94;
  170. case op_fpustate32: return 108;
  171. case op_fpregset: return 512;
  172. case op_fpreg: return 10;
  173. case op_none: return 0;
  174. }
  175. return(4); /* default size */
  176. }
  177. void x86_insn_t::x86_set_insn_addr( uint32_t _addr ) {
  178. addr = _addr;
  179. }
  180. void x86_insn_t::x86_set_insn_offset( unsigned int _offset ){
  181. offset = _offset;
  182. }
  183. void x86_insn_t::x86_set_insn_function( void * func ){
  184. function = func;
  185. }
  186. void x86_insn_t::x86_set_insn_block( void * _block ){
  187. block = _block;
  188. }
  189. void x86_insn_t::x86_tag_insn(){
  190. tag = 1;
  191. }
  192. void x86_insn_t::x86_untag_insn(){
  193. tag = 0;
  194. }
  195. int x86_insn_t::x86_insn_is_tagged(){
  196. return tag;
  197. }