forked from pult/libssh2_delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHVDll.pas
529 lines (471 loc) · 13.1 KB
/
HVDll.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
unit HVDll;
//
// https://github.com/pult/dll_load_delay
// https://bitbucket.org/VadimLV/dll_load_delay
// http://hallvards.blogspot.com/2008/03/tdm8-delayloading-of-dlls.html
//
// Support for DelayLoading of DLLs like VC++6.0 or latest Delphi (delayed)
// Written by Hallvard Vassbotn ([email protected]), January 1999
//
interface
{$UNDEF SUPPORTED}
{$IFDEF WIN32}
{$DEFINE SUPPORTED} // TODO: Check Delphi and FPC
{$ENDIF}
{$IFDEF WIN64}
{.$DEFINE SUPPORTED} // TODO: Check Delphi and FPC
{$ENDIF}
{$IFDEF SUPPORTED}
{$IFDEF FPC}
{$ALIGN 8} // For packed record
{$MINENUMSIZE 1}
{$ELSE}
{$IFDEF UNICODE}
{$ALIGN 8} // For packed record
{$MINENUMSIZE 1}
{$IF CompilerVersion >= 25.00}{XE4Up}
{$ZEROBASEDSTRINGS OFF}
{$IFEND}
{$ENDIF}
{$ENDIF}
uses
Windows,
Types,
Classes,
SysUtils,
HVHeaps;
const
uHVDll = 19990128; // 1999-01-28
{$EXTERNALSYM uHVDll}
(*
// Sample for checking:
// <sample>
{$warn comparison_true off}
{$if (not declared(uHVDll)) or (uHVDll < 19990128)}
{$warn message_directive on}{$MESSAGE WARN 'Please use last HVDll.pas'}
//{$MESSAGE FATAL 'Please use last HVDll.pas'}
{$ifend}{$warnings on}
// <\sample>
//*)
type
// Structures to keep the address of function variables and name/id pairs
PPointer = ^Pointer;
PEntry = ^TEntry;
TEntry = packed record
Proc: PPointer;
case Integer of
0 : (Name: PChar);
1 : (ID : LongInt);
end;
PEntries = ^TEntries;
TEntries = packed array[0..High(Word)-1] of TEntry;
// Structures to generate the per-routine thunks
PThunk = ^TThunk;
TThunk = packed record
CALL : Byte;
OFFSET: Integer;
end;
PThunks = ^TThunks;
TThunks = packed array[0..High(Word)-1] of TThunk;
// Structure to generate the per-DLL thunks
TThunkHeader = packed record
PUSH : Byte;
VALUE : Pointer;
JMP : Byte;
OFFSET : Integer;
end;
// The combined per-DLL and per-routine thunks
PThunkingCode = ^TThunkingCode;
TThunkingCode = packed record
ThunkHeader : TThunkHeader;
Thunks : TThunks;
end;
// The base class that provides DelayLoad capability
TDll = class//(TObject)
private
FEntries : PEntries;
FThunkingCode: PThunkingCode;
FCount : Integer;
FFullPath : string;
FHandle : HMODULE;
function GetHandle: HMODULE;
procedure SetFullPath(const Value: string);
function GetProcs(Index: Integer): Pointer;
procedure SetProcs(Index: Integer; Value: Pointer);
function GetAvailable: Boolean;
function GetLoaded: Boolean;
function LoadProcAddrFromIndex(Index: Integer; var Addr: Pointer): Boolean;
procedure ActivateThunks;
function GetEntryName(Index: Integer): string;
protected
function LoadHandle: HMODULE; virtual;
class procedure Error(const Msg: string; Args: array of const);
procedure CreateThunks;
procedure DestroyThunks;
function HasThunk(Thunk: PThunk): Boolean;
function GetProcAddrFromIndex(Index: Integer): Pointer;
function DelayLoadFromThunk(Thunk: PThunk): Pointer; register;
function DelayLoadIndex(Index: Integer): Pointer;
function GetIndexFromThunk(Thunk: PThunk): Integer;
function GetIndexFromProc(Proc: PPointer): Integer;
function ValidIndex(Index: Integer): Boolean;
procedure CheckIndex(Index: Integer);
property Procs[Index: Integer]: Pointer read GetProcs write SetProcs;
public
constructor Create(const DllName: string; const Entries: array of TEntry);
destructor Destroy; override;
procedure Load;
procedure Unload;
function HasRoutine(Proc: PPointer): Boolean;
function HookRoutine(Proc: PPointer; HookProc: Pointer; var OrgProc{: Pointer}): Boolean;
function UnHookRoutine(Proc: PPointer; var OrgProc{: Pointer}): Boolean;
property FullPath: string read FFullPath write SetFullPath;
property Handle: HMODULE read GetHandle;
property Loaded: Boolean read GetLoaded;
property Available: Boolean read GetAvailable;
property Count: Integer read FCount;
property EntryName[Index: Integer]: string read GetEntryName;
end;
// The class that keeps a list of all created TDll instances in one place
TDllNotifyAction = (daLoadedDll, daUnloadedDll, daLinkedRoutine);
TDllNotifyEvent = procedure(Sender: TDll; Action: TDllNotifyAction; Index: Integer) of object;
TDlls = class(TList)
private
FCodeHeap: TCodeHeap;
FOnDllNotify: TDllNotifyEvent;
function GetDlls(Index: Integer): TDll;
protected
procedure DllNotify(Sender: TDll; Action: TDllNotifyAction; Index: Integer);
property CodeHeap: TCodeHeap read FCodeHeap;
public
constructor Create;
destructor Destroy; override;
property Dlls[Index: Integer]: TDll read GetDlls; default;
property OnDllNotify: TDllNotifyEvent read FOnDllNotify write FOnDllNotify;
end;
EDllError = class(Exception);
var
Dlls: TDlls;
{$ENDIF SUPPORTED}
implementation
{$IFDEF SUPPORTED}
{$IFDEF VER90}
const
{$ELSE}
resourcestring
{$ENDIF}
SIndexOutOfRange = 'DLL-entry index out of range (%d)';
SOrdinal = 'ordinal #';
SCannotLoadLibrary = 'Could not find the library: "%s"'#13#10'(%s)';
SCannotGetProcAddress = 'Could not find the routine "%s" in the library "%s"'#13#10'(%s)';
SCannotFindThunk = 'Could not find the TDll object corresponding to the thunk address %p';
{ Helper routines }
function EntryToString(const Entry: TEntry): string;
begin
if Hi(Entry.ID) <> 0
then Result := string(Entry.Name)
else Result := SOrdinal+IntToStr(Entry.ID);
end;
//{$IFDEF WIN64}
//procedure ThunkingTarget(ASelf, AThunk: Pointer);
//{$ELSE}
procedure ThunkingTarget;
//{$ENDIF}
const
TThunkSize = SizeOf(TThunk);
asm
{$IFDEF WIN64}
// TODO: ...
//.PARAMS 3
fail code !!!
PUSH RAX
PUSH RDX
PUSH RCX
MOV EAX, [ESP+12] //?12 // Self
MOV EDX, [ESP+16] //?16 // Thunk
SUB EDX, TThunkSize //TYPE TThunk // Using SizeOf(TThunk) here does not work. BASM bug?
CALL TDll.DelayLoadFromThunk{(Self, Thunk);}
MOV [ESP+16], EAX
POP RCX
POP RDX
POP RAX
ADD ESP, 8 //?
{$ENDIF WIN64}
{$IFDEF WIN32}
// Save register-based parameters
PUSH EAX
PUSH EDX
PUSH ECX
{ Stack layout at this point:
24 [Stack based parameters]
20 [User code RetAdr]
16 [Thunk Ret-Adr]
12 [Self]
8 [EAX]
4 [EDX]
0 [ECX] <-ESP}
// Get the caller's return address (i.e. one of the thunks)
MOV EAX, [ESP+12] // Self
MOV EDX, [ESP+16] // Thunk
// The return address is just after the thunk that
// called us, so go back one step
SUB EDX, TYPE TThunk // Using SizeOf(TThunk) here does not work. BASM bug?
// Do the rest in Pascal
CALL TDll.DelayLoadFromThunk{(Self, Thunk);}
// Now patch the return address on the stack so that we "return" to the DLL routine
MOV [ESP+16], EAX
// Restore register-based parameters
POP ECX
POP EDX
POP EAX
// Remove the Self Pointer!
ADD ESP, 4
// "Return" to the DLL!
{$ENDIF WIN32}
end;
{ TDll }
constructor TDll.Create(const DllName: string; const Entries: array of TEntry);
begin
inherited Create;
FFullPath := DllName;
FEntries := @Entries;
FCount := High(Entries) - Low(Entries) + 1;
CreateThunks;
ActivateThunks;
Dlls.Add(Self);
end;
destructor TDll.Destroy;
begin
Dlls.Remove(Self);
Unload;
DestroyThunks;
inherited Destroy;
end;
procedure TDll.CreateThunks;
const
CallInstruction = $E8;
PushInstruction = $68;
JumpInstruction = $E9;
var
i : Integer;
begin
// Get a memory block large enough for the thunks
Dlls.CodeHeap.GetMem(FThunkingCode, SizeOf(TThunkHeader) + SizeOf(TThunk) * Count);
// Generate some machine code in the thunks
with FThunkingCode^, ThunkHeader do
begin
// The per-Dll thunk does this:
// PUSH Self
// JMP ThunkingTarget
PUSH := PushInstruction;
VALUE := Self;
JMP := JumpInstruction;
OFFSET := PAnsiChar(@ThunkingTarget) - PAnsiChar(@Thunks[0]);
for i := 0 to Count-1 do
with Thunks[i] do
begin
// The per-entry thunk does this:
// CALL @ThunkingCode^.ThunkHeader
CALL := CallInstruction;
OFFSET := PAnsiChar(@FThunkingCode^.ThunkHeader) - PAnsiChar(@Thunks[i+1]);
end;
end;
end;
procedure TDll.DestroyThunks;
begin
if Assigned(FThunkingCode) then
begin
Dlls.CodeHeap.FreeMem(FThunkingCode);
FThunkingCode := nil;
end;
end;
function TDll.DelayLoadFromThunk(Thunk: PThunk): Pointer; register;
begin
Result := DelayLoadIndex(GetIndexFromThunk(Thunk));
end;
function TDll.DelayLoadIndex(Index: Integer): Pointer;
begin
Result := GetProcAddrFromIndex(Index);
FEntries^[Index].Proc^ := Result;
end;
class procedure TDll.Error(const Msg: string; Args: array of const);
begin
raise EDllError.CreateFmt(Msg, Args);
end;
function TDll.LoadHandle: HMODULE;
begin
if FHandle = 0 then
begin
FHandle := Windows.LoadLibrary(PChar(FullPath));
if FHandle <> 0 then
Dlls.DllNotify(Self, daLoadedDll, -1);
end;
Result := FHandle;
end;
function TDll.GetHandle: HMODULE;
begin
Result := FHandle;
if Result = 0 then
begin
Result := LoadHandle;
if Result = 0 then
Error(SCannotLoadLibrary, [FullPath, SysErrorMessage(GetLastError)]);
end;
end;
function TDll.GetIndexFromThunk(Thunk: PThunk): Integer;
begin
// We calculate the thunk index by subtracting the start of the array
// and dividing by the size of the array elements
Result := (PAnsiChar(Thunk) - PAnsiChar(@FThunkingCode^.Thunks[0])) div SizeOf(TThunk);
end;
function TDll.LoadProcAddrFromIndex(Index: Integer; var Addr: Pointer): Boolean;
begin
Result := ValidIndex(Index);
if Result then
begin
Addr := Windows.GetProcAddress(Handle, FEntries^[Index].Name);
Result := Assigned(Addr);
if Result then
Dlls.DllNotify(Self, daLinkedRoutine, Index);
end;
end;
function TDll.GetProcAddrFromIndex(Index: Integer): Pointer;
begin
if not LoadProcAddrFromIndex(Index, Result) then
Error(SCannotGetProcAddress, [EntryName[Index], FullPath, SysErrorMessage(GetLastError)]);
end;
function TDll.HasThunk(Thunk: PThunk): Boolean;
begin
// The thunk belongs to us if its address is in the thunk array
Result := (PAnsiChar(Thunk) >= PAnsiChar(@FThunkingCode^.Thunks[0])) and
(PAnsiChar(Thunk) <= PAnsiChar(@FThunkingCode^.Thunks[Count-1]));
end;
procedure TDll.Load;
var
i : Integer;
begin
for i := 0 to Count-1 do
DelayLoadIndex(i);
end;
procedure TDll.SetFullPath(const Value: string);
begin
if CompareText(FFullPath, Value) <> 0 then
begin
Unload;
FFullPath := Value;
end;
end;
function TDll.GetEntryName(Index: Integer): string;
begin
if ValidIndex(Index)
then Result := EntryToString(FEntries^[Index])
else Result := Format(SIndexOutOfRange, [Index]);
end;
procedure TDll.ActivateThunks;
// Patch the procedure variables to point to the generated thunks
var
i : Integer;
begin
for i := 0 to Count-1 do
FEntries^[i].Proc^ := @FThunkingCode^.Thunks[i];
end;
procedure TDll.Unload;
begin
ActivateThunks;
if FHandle <> 0 then
begin
FreeLibrary(FHandle);
Dlls.DllNotify(Self, daUnloadedDll, -1);
FHandle := 0;
end;
end;
function TDll.ValidIndex(Index: Integer): Boolean;
begin
Result := (Index >= 0) and (Index <= Count-1);
end;
procedure TDll.CheckIndex(Index: Integer);
begin
if not ValidIndex(Index) then
Error(SIndexOutOfRange, [Index]);
end;
function TDll.GetProcs(Index: Integer): Pointer;
begin
CheckIndex(Index);
Result := FEntries^[Index].Proc^;
end;
procedure TDll.SetProcs(Index: Integer; Value: Pointer);
begin
CheckIndex(Index);
FEntries^[Index].Proc^ := Value;
end;
function TDll.GetAvailable: Boolean;
begin
Result := (LoadHandle <> 0);
end;
function TDll.GetLoaded: Boolean;
begin
Result := (FHandle <> 0);
end;
function TDll.GetIndexFromProc(Proc: PPointer): Integer;
begin
for Result := 0 to Count-1 do
if FEntries^[Result].Proc = Proc then
Exit;
Result := -1;
end;
function TDll.HasRoutine(Proc: PPointer): Boolean;
begin
Result := Available and
((not HasThunk(Proc^)) or
LoadProcAddrFromIndex(GetIndexFromProc(Proc), Proc^));
end;
function TDll.HookRoutine(Proc: PPointer; HookProc: Pointer; var OrgProc{: Pointer}): Boolean;
begin
Result := HasRoutine(Proc);
if Result then
begin
Pointer(OrgProc) := Proc^;
Proc^ := HookProc;
end;
end;
function TDll.UnHookRoutine(Proc: PPointer; var OrgProc{: Pointer}): Boolean;
begin
Result := Assigned(Pointer(OrgProc));
if Result then
begin
Proc^ := Pointer(OrgProc);
Pointer(OrgProc) := nil;
end;
end;
{ TDlls }
constructor TDlls.Create;
begin
inherited Create;
FCodeHeap := TCodeHeap.Create;
end;
destructor TDlls.Destroy;
var
i : Integer;
begin
for i := Count-1 downto 0 do
Dlls[i].Free;
FCodeHeap.Free;
FCodeHeap := nil;
inherited Destroy;
end;
procedure TDlls.DllNotify(Sender: TDll; Action: TDllNotifyAction; Index: Integer);
begin
if Assigned(FOnDllNotify) then
FOnDllNotify(Sender, Action, Index);
end;
function TDlls.GetDlls(Index: Integer): TDll;
begin
Result := TDll(Items[Index]);
end;
initialization
Dlls := TDlls.Create;
finalization
Dlls.Free;
Dlls := nil;
{$ENDIF SUPPORTED}
end.