-
Notifications
You must be signed in to change notification settings - Fork 4
/
lpdisassembler.pas
315 lines (268 loc) · 9.05 KB
/
lpdisassembler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
{
Author: Niels A.D
Project: Lape (http://code.google.com/p/la-pe/)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Bytecode disassembler.
}
unit lpdisassembler;
{$I lape.inc}
{$IFNDEF FPC}
{$UNDEF Lape_Inline}
{$ENDIF}
interface
uses
Classes, SysUtils,
lptypes, lpvartypes;
type
TLapeDisassemblerPointerMap = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<string>;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDisassemblerPointerMap); overload;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeCompilerBase); overload;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDeclArray = nil); overload;
implementation
uses
lpexceptions, lpinterpreter, lpeval, lputils;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDisassemblerPointerMap);
var
CodeBase: PByte;
{$IFDEF Lape_EmitPos}p: TDocPos;{$ENDIF}
function IntToStr(i: Int64): string; overload;
begin
Result := SysUtils.IntToStr(i);
end;
function IntToStr(p: Pointer): string; overload;
var s: string;
begin
if (p = nil) then
Result := 'nil'
else
begin
s := IntToHex(PtrUInt(p), 0);
if (PointerNames <> nil) and PointerNames.ExistsKey(lpString(s)) then
Result := PointerNames[lpString(s)]
else
Result := '$' + s;
end;
end;
procedure _WriteLn(s: string); overload; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
WriteLn('$', IntToHex(Code - CodeBase, 8), ' :: ', s);
end;
procedure _WriteLn(s: string; args: array of const); overload;
begin
_WriteLn(Format(s, args));
end;
procedure DoCheckInternal; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('IsInternal');
_WriteLn('IncStack %d', [SizeOf(EvalBool) - SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoInitStackLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitStackLen %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitVarLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitVarStackLen %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('ExpandVarStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('ExpandVarStackAndInit %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowVarStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowVarStackAndInit %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoPopVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('PopVarStack');
Inc(Code, ocSize);
end;
procedure DoPopStackToVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_PopStackToVar(PtrUInt(Code) + ocSize)^ do
begin
_WriteLn('PopStackToVar %d %d', [Size, VOffset]);
_WriteLn('DecStackPos %d', [Size]);
end;
Inc(Code, ocSize + SizeOf(TOC_PopStackToVar));
end;
procedure DoPopVarToStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_PopStackToVar(PtrUInt(Code) + ocSize)^ do
begin
_WriteLn('PopVarToStack %d %d', [Size, VOffset]);
_WriteLn('IncStackPos %d', [Size]);
end;
Inc(Code, ocSize + SizeOf(TOC_PopStackToVar));
end;
procedure DoJmpVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpVar');
Inc(Code, ocSize);
end;
procedure DoJmpSafe; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpSafe $%x', [PCodePos(PtrUInt(Code) + ocSize)^]);
Inc(Code, ocSize + SizeOf(TCodePos));
end;
procedure DoJmpSafeR; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpSafeR $%x', [PtrInt(Code - CodeBase) + PCodeOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, ocSize + SizeOf(TCodeOffset));
end;
procedure DoIncTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_IncTry(PtrUInt(Code) + ocSize)^ do
if (JmpFinally = Try_NoFinally) then
_WriteLn('IncTry $%x (NoFinally)', [PtrInt(Code - CodeBase) + Jmp])
else if (JmpFinally = Try_NoExcept) then
_WriteLn('IncTry $%x (NoExcept)', [PtrInt(Code - CodeBase) + Jmp])
else
_WriteLn('IncTry $%x $%x', [PtrInt(Code - CodeBase) + Jmp, PtrInt(Code - CodeBase) + Jmp + Int32(JmpFinally)]);
Inc(Code, ocSize + SizeOf(TOC_IncTry));
end;
procedure DoDecTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecTry');
Inc(Code, ocSize);
end;
procedure DoEndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('EndTry');
Inc(Code, ocSize);
end;
procedure DoCatchException; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('CatchException');
Inc(Code, ocSize);
end;
procedure DoDecCall; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecCall');
Inc(Code, ocSize);
end;
procedure DoDecCall_EndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecCall_EndTry');
Inc(Code, ocSize);
end;
{$I lpdisassembler_doinvoke.inc}
{$I lpdisassembler_dojump.inc}
{$I lpdisassembler_doeval.inc}
begin
CodeBase := Code;
{$IFDEF Lape_EmitPos}
p.Line := 0;
p.Col := 0;
{$ENDIF}
try
while True do
begin
{$IFDEF Lape_EmitPos}
with PDocPos(PtrUInt(Code) + SizeOf(opCodeType))^ do
if (p.FileName <> FileName) or (p.Line <> Line) or (p.Col <> Col) then
begin
p.FileName := FileName;
p.Line := Line;
p.Col := Col;
_WriteLn('--> File "'+string(FileName)+'", Line '+IntToStr(Line)+', Col '+IntToStr(Col));
end;
{$ENDIF}
{$I lpinterpreter_opcodecase.inc}
end;
except
on E: Exception do
LapeExceptionFmt(lpeRuntime, [E.Message] {$IFDEF Lape_EmitPos}, PDocPos(PtrUInt(Code) + SizeOf(opCodeType))^ {$ENDIF});
end;
end;
procedure Disassemble__EvalProcs(pMap: TLapeDisassemblerPointerMap);
var
op: EOperator;
t1, t2: ELapeBaseType;
proc: TLapeEvalProc;
begin
Assert(pMap <> nil);
for op := Low(EOperator) to High(EOperator) do
begin
if (op_name[op] = '') then
Continue;
for t1 := High(ELapeBaseType) downto Low(ELapeBaseType) do
for t2 := High(ELapeBaseType) downto Low(ELapeBaseType) do
begin
proc := getEvalProc(op, t1, t2);
if ValidEvalFunction(proc) then
if (t1 = ltUnknown) then
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(op_name[op])
else if (t2 = ltUnknown) then
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op])
else
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op]+'_'+LapeTypeToString(t2));
end;
end;
end;
procedure Disassemble__PointerMap(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
begin
if (AName = '') then
TLapeDisassemblerPointerMap(Arg)[lpString(IntToHex(PtrUInt(v.Ptr), 0))] := string(v.AsString)
else
TLapeDisassemblerPointerMap(Arg)[lpString(IntToHex(PtrUInt(v.Ptr), 0))] := string(AName);
end;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeCompilerBase);
var
pMap: TLapeDisassemblerPointerMap;
begin
pMap := TLapeDisassemblerPointerMap.Create('', dupIgnore, True);
try
Disassemble__EvalProcs(pMap);
TraverseGlobals(PointerNames, @Disassemble__PointerMap, pMap);
DisassembleCode(Code, pMap);
finally
pMap.Free();
end;
end;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDeclArray = nil);
var
pMap: TLapeDisassemblerPointerMap;
i: Integer;
begin
pMap := TLapeDisassemblerPointerMap.Create('', dupIgnore, True);
try
Disassemble__EvalProcs(pMap);
for i := 0 to High(PointerNames) do
if (PointerNames[i].Name = '') and (PointerNames[i] is TLapeGlobalVar) then
pMap[lpString(IntToHex(PtrUInt(TLapeGlobalVar(PointerNames[i]).Ptr), 0))] := string(TLapeGlobalVar(PointerNames[i]).AsString)
else if (PointerNames[i] is TLapeGlobalVar) then
pMap[lpString(IntToHex(PtrUInt(TLapeGlobalVar(PointerNames[i]).Ptr), 0))] := string(PointerNames[i].Name)
else
pMap[lpString(IntToHex(PtrUInt(PointerNames[i]), 0))] := string(PointerNames[i].Name);
DisassembleCode(Code, pMap);
finally
pMap.Free();
end;
end;
end.