1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Definition of MiniDisassembler.
9 #ifndef GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__
10 #define GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__
13 #include "mini_disassembler_types.h"
16 #include "base/logging.h"
17 #define ASSERT(cond, msg) DCHECK(cond)
18 #define ASSERT1(cond) DCHECK(cond)
22 // This small disassembler is very limited
23 // in its functionality, and in fact does only the bare minimum required by the
24 // preamble patching utility. It may be useful for other purposes, however.
26 // The limitations include at least the following:
27 // -# No support for coprocessor opcodes, MMX, etc.
28 // -# No machine-readable identification of opcodes or decoding of
29 // assembly parameters. The name of the opcode (as a string) is given,
30 // however, to aid debugging.
32 // You may ask what this little disassembler actually does, then? The answer is
33 // that it does the following, which is exactly what the patching utility needs:
34 // -# Indicates if opcode is a jump (any kind) or a return (any kind)
35 // because this is important for the patching utility to determine if
36 // a function is too short or there are jumps too early in it for it
37 // to be preamble patched.
38 // -# The opcode length is always calculated, so that the patching utility
39 // can figure out where the next instruction starts, and whether it
40 // already has enough instructions to replace with the absolute jump
41 // to the patching code.
43 // The usage is quite simple; just create a MiniDisassembler and use its
44 // Disassemble() method.
46 // If you would like to extend this disassembler, please refer to the
47 // IA-32 Intel Architecture Software Developer's Manual Volume 2:
48 // Instruction Set Reference for information about operand decoding
50 class MiniDisassembler
{
53 // Creates a new instance and sets defaults.
55 // @param operand_default_32_bits If true, the default operand size is
56 // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
57 // @param address_default_32_bits If true, the default address size is
58 // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
59 MiniDisassembler(bool operand_default_32_bits
,
60 bool address_default_32_bits
);
62 // Equivalent to MiniDisassembler(true, true);
65 // Attempts to disassemble a single instruction starting from the
66 // address in memory it is pointed to.
68 // @param start Address where disassembly should start.
69 // @param instruction_bytes Variable that will be <b>incremented</b> by
70 // the length in bytes of the instruction.
71 // @return enItJump, enItReturn or enItGeneric on success. enItUnknown
72 // if unable to disassemble, enItUnused if this seems to be an unused
73 // opcode. In the last two (error) cases, cbInstruction will be set
76 // @post This instance of the disassembler is ready to be used again,
77 // with unchanged defaults from creation time.
78 InstructionType
Disassemble(unsigned char* start
, unsigned int& instruction_bytes
);
82 // Makes the disassembler ready for reuse.
85 // Sets the flags for address and operand sizes.
86 // @return Number of prefix bytes.
87 InstructionType
ProcessPrefixes(unsigned char* start
, unsigned int& size
);
89 // Sets the flag for whether we have ModR/M, and increments
90 // operand_bytes_ if any are specifies by the opcode directly.
91 // @return Number of opcode bytes.
92 InstructionType
ProcessOpcode(unsigned char * start
,
96 // Checks the type of the supplied operand. Increments
97 // operand_bytes_ if it directly indicates an immediate etc.
98 // operand. Asserts have_modrm_ if the operand specifies
100 bool ProcessOperand(int flag_operand
);
102 // Increments operand_bytes_ by size specified by ModR/M and
103 // by SIB if present.
104 // @return 0 in case of error, 1 if there is just a ModR/M byte,
105 // 2 if there is a ModR/M byte and a SIB byte.
106 bool ProcessModrm(unsigned char* start
, unsigned int& size
);
108 // Processes the SIB byte that it is pointed to.
109 // @param start Pointer to the SIB byte.
110 // @param mod The mod field from the ModR/M byte.
111 // @return 1 to indicate success (indicates 1 SIB byte)
112 bool ProcessSib(unsigned char* start
, unsigned char mod
, unsigned int& size
);
114 // The instruction type we have decoded from the opcode.
115 InstructionType instruction_type_
;
117 // Counts the number of bytes that is occupied by operands in
118 // the current instruction (note: we don't care about how large
119 // operands stored in registers etc. are).
120 unsigned int operand_bytes_
;
122 // True iff there is a ModR/M byte in this instruction.
125 // True iff we need to decode the ModR/M byte (sometimes it just
126 // points to a register, we can tell by the addressing mode).
127 bool should_decode_modrm_
;
129 // Current operand size is 32 bits if true, 16 bits if false.
130 bool operand_is_32_bits_
;
132 // Default operand size is 32 bits if true, 16 bits if false.
133 bool operand_default_is_32_bits_
;
135 // Current address size is 32 bits if true, 16 bits if false.
136 bool address_is_32_bits_
;
138 // Default address size is 32 bits if true, 16 bits if false.
139 bool address_default_is_32_bits_
;
141 // Huge big opcode table based on the IA-32 manual, defined
142 // in Ia32OpcodeMap.cc
143 static const OpcodeTable s_ia32_opcode_map_
[];
145 // Somewhat smaller table to help with decoding ModR/M bytes
146 // when 16-bit addressing mode is being used. Defined in
148 static const ModrmEntry s_ia16_modrm_map_
[];
150 // Somewhat smaller table to help with decoding ModR/M bytes
151 // when 32-bit addressing mode is being used. Defined in
153 static const ModrmEntry s_ia32_modrm_map_
[];
155 // Indicators of whether we got certain prefixes that certain
156 // silly Intel instructions depend on in nonstandard ways for
158 bool got_f2_prefix_
, got_f3_prefix_
, got_66_prefix_
;
161 }; // namespace sidestep
163 #endif // GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__