From 2ddab09733fe96aeb603fb9459f7c3df846ad82e Mon Sep 17 00:00:00 2001 From: Delphi-FPC-Lazarus Date: Thu, 24 Sep 2020 13:22:14 +0200 Subject: [PATCH] fastpatch working with delphi xe10 (32/64 Bit) inspired by https://stackoverflow.com/questions/7581321/how-to-make-fastcodepatch-work-in-delphi-xe2-win64-platform/7581606 --- FastcodePatch.pas | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/FastcodePatch.pas b/FastcodePatch.pas index 3e112af..1280105 100644 --- a/FastcodePatch.pas +++ b/FastcodePatch.pas @@ -27,7 +27,7 @@ interface -function FastcodeGetAddress(AStub: Pointer): Pointer; +function FastcodeGetAddress(AProc: Pointer): Pointer; procedure FastcodeAddressPatch(const ASource, ADestination: Pointer); implementation @@ -39,18 +39,24 @@ implementation PJump = ^TJump; TJump = packed record OpCode: Byte; - Distance: Pointer; + Distance: integer; end; -function FastcodeGetAddress(AStub: Pointer): Pointer; +function FastcodeGetAddress(AProc: Pointer): Pointer; +type + PAbsoluteIndirectJmp = ^TAbsoluteIndirectJmp; + TAbsoluteIndirectJmp = packed record + OpCode: Word; //$FF25(Jmp, FF /4) + Addr: Cardinal; + end; +var J: PAbsoluteIndirectJmp; begin - if PBYTE(AStub)^ = $E8 then - begin - Inc(Integer(AStub)); - Result := Pointer(Integer(AStub) + SizeOf(Pointer) + PInteger(AStub)^); - end + J := PAbsoluteIndirectJmp(AProc); + if (J.OpCode = $25FF) then + {$ifdef Win32}Result := PPointer(J.Addr)^{$endif} + {$ifdef Win64}Result := PPointer(NativeUInt(AProc) + J.Addr + 6{Instruction Size})^{$endif} else - Result := nil; + Result := AProc; end; procedure FastcodeAddressPatch(const ASource, ADestination: Pointer); @@ -64,11 +70,11 @@ procedure FastcodeAddressPatch(const ASource, ADestination: Pointer); begin NewJump := PJump(ASource); NewJump.OpCode := $E9; - NewJump.Distance := Pointer(Integer(ADestination) - Integer(ASource) - 5); + NewJump.Distance := NativeUInt(ADestination) - NativeUInt(ASource) - Size; FlushInstructionCache(GetCurrentProcess, ASource, SizeOf(TJump)); VirtualProtect(ASource, Size, OldProtect, @OldProtect); end; end; -end. +end. \ No newline at end of file