1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Utility functions for use with disassembler callbacks.
16 :
17 : #ifndef SYZYGY_CORE_DISASSEMBLER_UTIL_H_
18 : #define SYZYGY_CORE_DISASSEMBLER_UTIL_H_
19 :
20 : #include <string>
21 :
22 : #include "base/basictypes.h"
23 : #include "distorm.h" // NOLINT
24 :
25 m : namespace core {
26 :
27 : // Wrapper for the distorm_decompose function to patch a bug in distorm. The
28 : // access size for I_FNSTCW and I_FLDCW dst operand is 0 instead of 16.
29 : // TODO(sebmarchand): Remove this code once this is fixed in distorm.
30 : // @param ci Structure containing some information about the code to decompose
31 : // (code origin, code data, code length, decoding mode and features).
32 : // @param result Array of type _DecodeInst which will be used by this function
33 : // in order to return the disassembled instructions.
34 : // @param max_instructions The maximum number of entries in the result array
35 : // that you pass to this function, so it won't exceed its bound.
36 : // @param used_instructions_count Number of the instruction that successfully
37 : // were disassembled and written to the result array.
38 : // @returns DECRES_SUCCESS on success (no more to disassemble), DECRES_INPUTERR
39 : // on input error (null code buffer, invalid decoding mode, etc...),
40 : // DECRES_MEMORYERR when there are not enough entries to use in the result
41 : // array, BUT YOU STILL have to check for usedInstructionsCount!
42 m : _DecodeResult DistormDecompose(_CodeInfo* ci,
43 m : _DInst result[],
44 m : unsigned int max_instructions,
45 m : unsigned int* used_instructions_count);
46 :
47 : // Decodes exactly one instruction from the given buffer.
48 : // @param address the address of the instruction, as an absolute address
49 : // consistent with the image's base address. If this is not provided a
50 : // fake address of 0x10000000 will be used.
51 : // @param buffer the buffer containing the data to decode.
52 : // @param length the length of the buffer.
53 : // @returns true if an instruction was decoded, false otherwise.
54 m : bool DecodeOneInstruction(
55 m : uint32 address, const uint8* buffer, size_t length, _DInst* instruction);
56 m : bool DecodeOneInstruction(
57 m : const uint8* buffer, size_t length, _DInst* instruction);
58 :
59 : // Dump text representation of exactly one instruction to a std::string.
60 : // @param instruction the instruction to dump.
61 : // @param data points to the raw byte sequences.
62 : // @param code_length the size of the raw representation.
63 : // @param buffer receives the text representation.
64 : // @returns true if @p instruction was successfully dumped, false otherwise.
65 m : bool InstructionToString(const _DInst& instruction,
66 m : const uint8_t* data,
67 m : int code_length,
68 m : std::string* buffer);
69 :
70 : // Determines if the given instruction is a recognized no-op. We only recognize
71 : // those instructions that we see generated by the MSVS toolchain.
72 : // @param instruction the instruction to evaluate.
73 : // @returns true if @p instruction is a recognized no-op, false otherwise.
74 m : bool IsNop(const _DInst& instruction);
75 :
76 : // Determines if the given instruction is a CALL.
77 : // @param instruction the instruction to evaluate.
78 : // @returns true if @p instruction is a call, false otherwise.
79 m : bool IsCall(const _DInst& instruction);
80 :
81 : // Determines if the given instruction is a RET.
82 : // @param instruction the instruction to evaluate.
83 : // @returns true if @p instruction is a return, false otherwise.
84 m : bool IsReturn(const _DInst& instruction);
85 :
86 : // Determines if the given instruction is a SYS.
87 : // @param instruction the instruction to evaluate.
88 : // @returns true if @p instruction is a return, false otherwise.
89 m : bool IsSystemCall(const _DInst& instruction);
90 :
91 : // Determines if the given instruction is a conditional branch.
92 : // @param instruction the instruction to evaluate.
93 : // @returns true if @p instruction is a conditional branch, false otherwise.
94 m : bool IsConditionalBranch(const _DInst& instruction);
95 :
96 : // Determines if the given instruction is a unconditional branch.
97 : // @param instruction the instruction to evaluate.
98 : // @returns true if @p instruction is a unconditional branch, false otherwise.
99 m : bool IsUnconditionalBranch(const _DInst& instruction);
100 :
101 : // Determines if the given instruction is a branch or any kind
102 : // @param instruction the instruction to evaluate.
103 : // @returns true if @p instruction is a branch, false otherwise.
104 m : bool IsBranch(const _DInst& instruction);
105 :
106 : // Determines if the given instruction has a PC-relative operand at the
107 : // given operand index.
108 : // @param instruction the instruction to evaluate.
109 : // @param operand_index the operand index to evaluate.
110 : // @returns true if @p instruction has a PC-relative operand at the given index.
111 m : bool HasPcRelativeOperand(const _DInst& instruction, int operand_index);
112 :
113 : // Determines if the given instruction is a control-flow instruction.
114 : // @param instruction the instruction to evaluate.
115 : // @returns true if @p instruction is a control-flow instruction, false
116 : // otherwise.
117 m : bool IsControlFlow(const _DInst& instruction);
118 :
119 : // Determines if the given instruction is an implicit control-flow instruction.
120 : // @param instruction the instruction to evaluate.
121 : // @returns true if @p instruction is an implicit control-flow instruction
122 : // (we can't explicitly compute the target due to the addressing mode)
123 : // false otherwise.
124 m : bool IsImplicitControlFlow(const _DInst& instruction);
125 :
126 : // Determines if the given instruction is an interrupt instruction.
127 : // @param instruction the instruction to evaluate.
128 : // @returns true if @p instruction is an interrupt instruction, false otherwise.
129 m : bool IsInterrupt(const _DInst& instruction);
130 :
131 : // Determines if the given instruction is the debug interrupt instruction.
132 : // @param instruction the instruction to evaluate.
133 : // @returns true if @p instruction is the debug interrupt instruction, false
134 : // otherwise.
135 m : bool IsDebugInterrupt(const _DInst& instruction);
136 :
137 m : } // namespace core
138 :
139 : #endif // SYZYGY_CORE_DISASSEMBLER_UTIL_H_
|