x86_insn.cpp 6.3 KB

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