@@ -58,99 +58,87 @@ bool AreOperandsStatic(const _DInst &instruction, const int prefixLength)
58
58
return true ;
59
59
}
60
60
61
- int GetStaticInstructionBytes (const _DInst &instruction, const uint8_t *data )
61
+ _CodeInfo CreateCodeInfo (const uint8_t * address, int length, const _OffsetType virtualAddress )
62
62
{
63
63
_CodeInfo info = {};
64
- info.codeOffset = reinterpret_cast <_OffsetType>(data) ;
65
- info.code = data ;
66
- info.codeLen = instruction. size ;
64
+ info.codeOffset = virtualAddress ;
65
+ info.code = address ;
66
+ info.codeLen = length ;
67
67
info.features = DF_NONE;
68
+
68
69
#ifdef RECLASSNET32
69
70
info.dt = Decode32Bits;
70
71
#else
71
72
info.dt = Decode64Bits;
72
73
#endif
73
74
74
- _PrefixState ps = {};
75
- memset (ps.pfxIndexer , PFXIDX_NONE, sizeof (int ) * PFXIDX_MAX);
76
- ps.start = data;
77
- ps.last = data;
78
-
79
- prefixes_decode (data, info.codeLen , &ps, info.dt );
75
+ return info;
76
+ }
80
77
81
- info.codeOffset = reinterpret_cast <_OffsetType>(ps.last );
82
- info.code = ps.last ;
83
78
84
- const auto prefixLength = static_cast <int >(ps.start - ps.last );
85
- info.codeLen -= prefixLength;
79
+ int GetStaticInstructionBytes (const _DInst &instruction, const uint8_t *data)
80
+ {
81
+ auto info = CreateCodeInfo (data, instruction.size , reinterpret_cast <_OffsetType>(data));
86
82
87
- inst_lookup (&info, &ps);
83
+ _PrefixState ps = {};
84
+ int isPrefixed;
85
+ inst_lookup (&info, &ps, &isPrefixed);
88
86
89
- if (AreOperandsStatic (instruction, prefixLength ))
87
+ if (AreOperandsStatic (instruction, ps. count ))
90
88
{
91
89
return instruction.size ;
92
90
}
93
91
94
- return instruction.size - info.codeLen ;
92
+ return instruction.size - info.codeLen - ps. count ;
95
93
}
96
94
97
- _CodeInfo CreateCodeInfo (const RC_Pointer address, const RC_Size length, const RC_Pointer virtualAddress)
98
- {
99
- _CodeInfo info = {};
100
- info.codeOffset = reinterpret_cast <_OffsetType>(virtualAddress);
101
- info.code = reinterpret_cast <const uint8_t *>(address);
102
- info.codeLen = static_cast <int >(length);
103
- info.features = DF_NONE;
104
-
105
- #ifdef RECLASSNET32
106
- info.dt = Decode32Bits;
107
- #else
108
- info.dt = Decode64Bits;
109
- #endif
110
-
111
- return info;
112
- }
113
-
114
- void FillInstructionData (const RC_Pointer address, const _DInst& instruction, const _DecodedInst& instructionInfo, const bool determineStaticInstructionBytes, InstructionData* data)
95
+ void FillInstructionData (const _CodeInfo& info, const RC_Pointer address, const _DInst& instruction, const bool determineStaticInstructionBytes, InstructionData* data)
115
96
{
116
97
data->Address = reinterpret_cast <RC_Pointer>(instruction.addr );
117
- data->Length = instructionInfo.size ;
118
- std::memcpy (data->Data , address, instructionInfo.size );
119
-
120
- MultiByteToUnicode (
121
- reinterpret_cast <const char *>(instructionInfo.mnemonic .p ),
122
- data->Instruction ,
123
- instructionInfo.mnemonic .length
124
- );
125
- if (instructionInfo.operands .length != 0 )
126
- {
127
- data->Instruction [instructionInfo.mnemonic .length ] = ' ' ;
128
-
129
- MultiByteToUnicode (
130
- reinterpret_cast <const char *>(instructionInfo.operands .p ),
131
- 0 ,
132
- data->Instruction ,
133
- instructionInfo.mnemonic .length + 1 ,
134
- std::min<int >(64 - 1 - instructionInfo.mnemonic .length , instructionInfo.operands .length )
135
- );
136
- }
98
+ data->Length = instruction.size ;
99
+ std::memcpy (data->Data , address, instruction.size );
100
+ data->StaticInstructionBytes = -1 ;
137
101
138
- if (determineStaticInstructionBytes )
102
+ if (instruction. flags == FLAG_NOT_DECODABLE )
139
103
{
140
- data->StaticInstructionBytes = GetStaticInstructionBytes (
141
- instruction,
142
- reinterpret_cast <const uint8_t *>(address)
143
- );
104
+ std::memcpy (data->Instruction , L" ???" , sizeof (RC_UnicodeChar) * 3 );
144
105
}
145
106
else
146
107
{
147
- data->StaticInstructionBytes = -1 ;
108
+ _DecodedInst instructionInfo = {};
109
+ distorm_format (&info, &instruction, &instructionInfo);
110
+
111
+ MultiByteToUnicode (
112
+ reinterpret_cast <const char *>(instructionInfo.mnemonic .p ),
113
+ data->Instruction ,
114
+ instructionInfo.mnemonic .length
115
+ );
116
+ if (instructionInfo.operands .length != 0 )
117
+ {
118
+ data->Instruction [instructionInfo.mnemonic .length ] = ' ' ;
119
+
120
+ MultiByteToUnicode (
121
+ reinterpret_cast <const char *>(instructionInfo.operands .p ),
122
+ 0 ,
123
+ data->Instruction ,
124
+ instructionInfo.mnemonic .length + 1 ,
125
+ std::min<int >(64 - 1 - instructionInfo.mnemonic .length , instructionInfo.operands .length )
126
+ );
127
+ }
128
+
129
+ if (determineStaticInstructionBytes)
130
+ {
131
+ data->StaticInstructionBytes = GetStaticInstructionBytes (
132
+ instruction,
133
+ reinterpret_cast <const uint8_t *>(address)
134
+ );
135
+ }
148
136
}
149
137
}
150
138
151
139
bool DisassembleInstructionsImpl (const RC_Pointer address, const RC_Size length, const RC_Pointer virtualAddress, const bool determineStaticInstructionBytes, EnumerateInstructionCallback callback)
152
140
{
153
- auto info = CreateCodeInfo (address, length, virtualAddress);
141
+ auto info = CreateCodeInfo (static_cast < const uint8_t *>( address), static_cast < int >( length), reinterpret_cast <_OffsetType>( virtualAddress) );
154
142
155
143
const unsigned MaxInstructions = 50 ;
156
144
@@ -169,26 +157,25 @@ bool DisassembleInstructionsImpl(const RC_Pointer address, const RC_Size length,
169
157
170
158
for (auto i = 0u ; i < count; ++i)
171
159
{
172
- _DecodedInst instructionInfo = {};
173
- distorm_format (&info, &decodedInstructions[i], &instructionInfo);
160
+ const auto & instruction = decodedInstructions[i];
174
161
175
162
InstructionData data = {};
176
- FillInstructionData (instructionAddress, decodedInstructions[i], instructionInfo , determineStaticInstructionBytes, &data);
163
+ FillInstructionData (info, instructionAddress, instruction , determineStaticInstructionBytes, &data);
177
164
178
165
if (callback (&data) == false )
179
166
{
180
167
return true ;
181
168
}
182
169
183
- instructionAddress += decodedInstructions[i] .size ;
170
+ instructionAddress += instruction .size ;
184
171
}
185
172
186
173
if (res == DECRES_SUCCESS || count == 0 )
187
174
{
188
175
return true ;
189
176
}
190
177
191
- const auto offset = static_cast <unsigned >(decodedInstructions[count - 1 ].addr - info.codeOffset );
178
+ const auto offset = static_cast <unsigned >(decodedInstructions[count - 1 ].addr + decodedInstructions[count - 1 ]. size - info.codeOffset );
192
179
193
180
info.codeOffset += offset;
194
181
info.code += offset;
0 commit comments