Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 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 | |
| 37 | #ifndef GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_ |
| 38 | #define GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_ |
| 39 | |
| 40 | #include "config.h" |
| 41 | #include <windows.h> |
| 42 | #include "mini_disassembler_types.h" |
| 43 | |
| 44 | // compatibility shim |
| 45 | #include "base/logging.h" |
| 46 | #define SIDESTEP_ASSERT(cond) RAW_DCHECK(cond, #cond) |
| 47 | #define SIDESTEP_LOG(msg) RAW_VLOG(1, msg) |
| 48 | |
| 49 | namespace sidestep { |
| 50 | |
| 51 | // This small disassembler is very limited |
| 52 | // in its functionality, and in fact does only the bare minimum required by the |
| 53 | // preamble patching utility. It may be useful for other purposes, however. |
| 54 | // |
| 55 | // The limitations include at least the following: |
| 56 | // -# No support for coprocessor opcodes, MMX, etc. |
| 57 | // -# No machine-readable identification of opcodes or decoding of |
| 58 | // assembly parameters. The name of the opcode (as a string) is given, |
| 59 | // however, to aid debugging. |
| 60 | // |
| 61 | // You may ask what this little disassembler actually does, then? The answer is |
| 62 | // that it does the following, which is exactly what the patching utility needs: |
| 63 | // -# Indicates if opcode is a jump (any kind) or a return (any kind) |
| 64 | // because this is important for the patching utility to determine if |
| 65 | // a function is too short or there are jumps too early in it for it |
| 66 | // to be preamble patched. |
| 67 | // -# The opcode length is always calculated, so that the patching utility |
| 68 | // can figure out where the next instruction starts, and whether it |
| 69 | // already has enough instructions to replace with the absolute jump |
| 70 | // to the patching code. |
| 71 | // |
| 72 | // The usage is quite simple; just create a MiniDisassembler and use its |
| 73 | // Disassemble() method. |
| 74 | // |
| 75 | // If you would like to extend this disassembler, please refer to the |
| 76 | // IA-32 Intel® Architecture Software Developer’s Manual Volume 2: |
| 77 | // Instruction Set Reference for information about operand decoding |
| 78 | // etc. |
| 79 | class PERFTOOLS_DLL_DECL MiniDisassembler { |
| 80 | public: |
| 81 | |
| 82 | // Creates a new instance and sets defaults. |
| 83 | // |
| 84 | // @param operand_default_32_bits If true, the default operand size is |
| 85 | // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits. |
| 86 | // @param address_default_32_bits If true, the default address size is |
| 87 | // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits. |
| 88 | MiniDisassembler(bool operand_default_32_bits, |
| 89 | bool address_default_32_bits); |
| 90 | |
| 91 | // Equivalent to MiniDisassembler(true, true); |
| 92 | MiniDisassembler(); |
| 93 | |
| 94 | // Attempts to disassemble a single instruction starting from the |
| 95 | // address in memory it is pointed to. |
| 96 | // |
| 97 | // @param start Address where disassembly should start. |
| 98 | // @param instruction_bytes Variable that will be <b>incremented</b> by |
| 99 | // the length in bytes of the instruction. |
| 100 | // @return enItJump, enItReturn or enItGeneric on success. enItUnknown |
| 101 | // if unable to disassemble, enItUnused if this seems to be an unused |
| 102 | // opcode. In the last two (error) cases, cbInstruction will be set |
| 103 | // to 0xffffffff. |
| 104 | // |
| 105 | // @post This instance of the disassembler is ready to be used again, |
| 106 | // with unchanged defaults from creation time. |
| 107 | InstructionType Disassemble(unsigned char* start, unsigned int& instruction_bytes); |
| 108 | |
| 109 | private: |
| 110 | |
| 111 | // Makes the disassembler ready for reuse. |
| 112 | void Initialize(); |
| 113 | |
| 114 | // Sets the flags for address and operand sizes. |
| 115 | // @return Number of prefix bytes. |
| 116 | InstructionType ProcessPrefixes(unsigned char* start, unsigned int& size); |
| 117 | |
| 118 | // Sets the flag for whether we have ModR/M, and increments |
| 119 | // operand_bytes_ if any are specifies by the opcode directly. |
| 120 | // @return Number of opcode bytes. |
| 121 | InstructionType ProcessOpcode(unsigned char* start, |
| 122 | unsigned int table, |
| 123 | unsigned int& size); |
| 124 | |
| 125 | // Checks the type of the supplied operand. Increments |
| 126 | // operand_bytes_ if it directly indicates an immediate etc. |
| 127 | // operand. Asserts have_modrm_ if the operand specifies |
| 128 | // a ModR/M byte. |
| 129 | bool ProcessOperand(int flag_operand); |
| 130 | |
| 131 | // Increments operand_bytes_ by size specified by ModR/M and |
| 132 | // by SIB if present. |
| 133 | // @return 0 in case of error, 1 if there is just a ModR/M byte, |
| 134 | // 2 if there is a ModR/M byte and a SIB byte. |
| 135 | bool ProcessModrm(unsigned char* start, unsigned int& size); |
| 136 | |
| 137 | // Processes the SIB byte that it is pointed to. |
| 138 | // @param start Pointer to the SIB byte. |
| 139 | // @param mod The mod field from the ModR/M byte. |
| 140 | // @return 1 to indicate success (indicates 1 SIB byte) |
| 141 | bool ProcessSib(unsigned char* start, unsigned char mod, unsigned int& size); |
| 142 | |
| 143 | // The instruction type we have decoded from the opcode. |
| 144 | InstructionType instruction_type_; |
| 145 | |
| 146 | // Counts the number of bytes that is occupied by operands in |
| 147 | // the current instruction (note: we don't care about how large |
| 148 | // operands stored in registers etc. are). |
| 149 | unsigned int operand_bytes_; |
| 150 | |
| 151 | // True iff there is a ModR/M byte in this instruction. |
| 152 | bool have_modrm_; |
| 153 | |
| 154 | // True iff we need to decode the ModR/M byte (sometimes it just |
| 155 | // points to a register, we can tell by the addressing mode). |
| 156 | bool should_decode_modrm_; |
| 157 | |
| 158 | // Current operand size is 32 bits if true, 16 bits if false. |
| 159 | bool operand_is_32_bits_; |
| 160 | |
| 161 | // Default operand size is 32 bits if true, 16 bits if false. |
| 162 | bool operand_default_is_32_bits_; |
| 163 | |
| 164 | // Current address size is 32 bits if true, 16 bits if false. |
| 165 | bool address_is_32_bits_; |
| 166 | |
| 167 | // Default address size is 32 bits if true, 16 bits if false. |
| 168 | bool address_default_is_32_bits_; |
| 169 | |
| 170 | // Determines if 64 bit operands are supported (x64). |
| 171 | bool operand_default_support_64_bits_; |
| 172 | |
| 173 | // Current operand size is 64 bits if true, 32 bits if false. |
| 174 | bool operand_is_64_bits_; |
| 175 | |
| 176 | // Huge big opcode table based on the IA-32 manual, defined |
| 177 | // in Ia32OpcodeMap.cc |
| 178 | static const OpcodeTable s_ia32_opcode_map_[]; |
| 179 | |
| 180 | // Somewhat smaller table to help with decoding ModR/M bytes |
| 181 | // when 16-bit addressing mode is being used. Defined in |
| 182 | // Ia32ModrmMap.cc |
| 183 | static const ModrmEntry s_ia16_modrm_map_[]; |
| 184 | |
| 185 | // Somewhat smaller table to help with decoding ModR/M bytes |
| 186 | // when 32-bit addressing mode is being used. Defined in |
| 187 | // Ia32ModrmMap.cc |
| 188 | static const ModrmEntry s_ia32_modrm_map_[]; |
| 189 | |
| 190 | // Indicators of whether we got certain prefixes that certain |
| 191 | // silly Intel instructions depend on in nonstandard ways for |
| 192 | // their behaviors. |
| 193 | bool got_f2_prefix_, got_f3_prefix_, got_66_prefix_; |
| 194 | }; |
| 195 | |
| 196 | }; // namespace sidestep |
| 197 | |
| 198 | #endif // GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H_ |