mini_disassembler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. /* Copyright (c) 2007, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * ---
  32. * Author: Joi Sigurdsson
  33. *
  34. * Definition of MiniDisassembler.
  35. */
  36. #ifndef GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_
  37. #define GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_
  38. #include "config.h"
  39. #include <windows.h>
  40. #include "mini_disassembler_types.h"
  41. // compatibility shim
  42. #include "base/logging.h"
  43. #define SIDESTEP_ASSERT(cond) RAW_DCHECK(cond, #cond)
  44. #define SIDESTEP_LOG(msg) RAW_VLOG(1, msg)
  45. namespace sidestep {
  46. // This small disassembler is very limited
  47. // in its functionality, and in fact does only the bare minimum required by the
  48. // preamble patching utility. It may be useful for other purposes, however.
  49. //
  50. // The limitations include at least the following:
  51. // -# No support for coprocessor opcodes, MMX, etc.
  52. // -# No machine-readable identification of opcodes or decoding of
  53. // assembly parameters. The name of the opcode (as a string) is given,
  54. // however, to aid debugging.
  55. //
  56. // You may ask what this little disassembler actually does, then? The answer is
  57. // that it does the following, which is exactly what the patching utility needs:
  58. // -# Indicates if opcode is a jump (any kind) or a return (any kind)
  59. // because this is important for the patching utility to determine if
  60. // a function is too short or there are jumps too early in it for it
  61. // to be preamble patched.
  62. // -# The opcode length is always calculated, so that the patching utility
  63. // can figure out where the next instruction starts, and whether it
  64. // already has enough instructions to replace with the absolute jump
  65. // to the patching code.
  66. //
  67. // The usage is quite simple; just create a MiniDisassembler and use its
  68. // Disassemble() method.
  69. //
  70. // If you would like to extend this disassembler, please refer to the
  71. // IA-32 Intel® Architecture Software Developer’s Manual Volume 2:
  72. // Instruction Set Reference for information about operand decoding
  73. // etc.
  74. class PERFTOOLS_DLL_DECL MiniDisassembler {
  75. public:
  76. // Creates a new instance and sets defaults.
  77. //
  78. // @param operand_default_32_bits If true, the default operand size is
  79. // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
  80. // @param address_default_32_bits If true, the default address size is
  81. // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
  82. MiniDisassembler(bool operand_default_32_bits,
  83. bool address_default_32_bits);
  84. // Equivalent to MiniDisassembler(true, true);
  85. MiniDisassembler();
  86. // Attempts to disassemble a single instruction starting from the
  87. // address in memory it is pointed to.
  88. //
  89. // @param start Address where disassembly should start.
  90. // @param instruction_bytes Variable that will be <b>incremented</b> by
  91. // the length in bytes of the instruction.
  92. // @return enItJump, enItReturn or enItGeneric on success. enItUnknown
  93. // if unable to disassemble, enItUnused if this seems to be an unused
  94. // opcode. In the last two (error) cases, cbInstruction will be set
  95. // to 0xffffffff.
  96. //
  97. // @post This instance of the disassembler is ready to be used again,
  98. // with unchanged defaults from creation time.
  99. InstructionType Disassemble(unsigned char* start, unsigned int& instruction_bytes);
  100. private:
  101. // Makes the disassembler ready for reuse.
  102. void Initialize();
  103. // Sets the flags for address and operand sizes.
  104. // @return Number of prefix bytes.
  105. InstructionType ProcessPrefixes(unsigned char* start, unsigned int& size);
  106. // Sets the flag for whether we have ModR/M, and increments
  107. // operand_bytes_ if any are specifies by the opcode directly.
  108. // @return Number of opcode bytes.
  109. InstructionType ProcessOpcode(unsigned char* start,
  110. unsigned int table,
  111. unsigned int& size);
  112. // Checks the type of the supplied operand. Increments
  113. // operand_bytes_ if it directly indicates an immediate etc.
  114. // operand. Asserts have_modrm_ if the operand specifies
  115. // a ModR/M byte.
  116. bool ProcessOperand(int flag_operand);
  117. // Increments operand_bytes_ by size specified by ModR/M and
  118. // by SIB if present.
  119. // @return 0 in case of error, 1 if there is just a ModR/M byte,
  120. // 2 if there is a ModR/M byte and a SIB byte.
  121. bool ProcessModrm(unsigned char* start, unsigned int& size);
  122. // Processes the SIB byte that it is pointed to.
  123. // @param start Pointer to the SIB byte.
  124. // @param mod The mod field from the ModR/M byte.
  125. // @return 1 to indicate success (indicates 1 SIB byte)
  126. bool ProcessSib(unsigned char* start, unsigned char mod, unsigned int& size);
  127. // The instruction type we have decoded from the opcode.
  128. InstructionType instruction_type_;
  129. // Counts the number of bytes that is occupied by operands in
  130. // the current instruction (note: we don't care about how large
  131. // operands stored in registers etc. are).
  132. unsigned int operand_bytes_;
  133. // True iff there is a ModR/M byte in this instruction.
  134. bool have_modrm_;
  135. // True iff we need to decode the ModR/M byte (sometimes it just
  136. // points to a register, we can tell by the addressing mode).
  137. bool should_decode_modrm_;
  138. // Current operand size is 32 bits if true, 16 bits if false.
  139. bool operand_is_32_bits_;
  140. // Default operand size is 32 bits if true, 16 bits if false.
  141. bool operand_default_is_32_bits_;
  142. // Current address size is 32 bits if true, 16 bits if false.
  143. bool address_is_32_bits_;
  144. // Default address size is 32 bits if true, 16 bits if false.
  145. bool address_default_is_32_bits_;
  146. // Determines if 64 bit operands are supported (x64).
  147. bool operand_default_support_64_bits_;
  148. // Current operand size is 64 bits if true, 32 bits if false.
  149. bool operand_is_64_bits_;
  150. // Huge big opcode table based on the IA-32 manual, defined
  151. // in Ia32OpcodeMap.cc
  152. static const OpcodeTable s_ia32_opcode_map_[];
  153. // Somewhat smaller table to help with decoding ModR/M bytes
  154. // when 16-bit addressing mode is being used. Defined in
  155. // Ia32ModrmMap.cc
  156. static const ModrmEntry s_ia16_modrm_map_[];
  157. // Somewhat smaller table to help with decoding ModR/M bytes
  158. // when 32-bit addressing mode is being used. Defined in
  159. // Ia32ModrmMap.cc
  160. static const ModrmEntry s_ia32_modrm_map_[];
  161. // Indicators of whether we got certain prefixes that certain
  162. // silly Intel instructions depend on in nonstandard ways for
  163. // their behaviors.
  164. bool got_f2_prefix_, got_f3_prefix_, got_66_prefix_;
  165. };
  166. }; // namespace sidestep
  167. #endif // GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_