From c8feb2d0e8d72e8145c52b9a0b6055adbdc8344c Mon Sep 17 00:00:00 2001 From: Joost van der Sluis Date: Sun, 2 Jan 2022 00:38:29 +0100 Subject: [PATCH] FpDebug: Replaced some Pascalish code to perform pointer calculations without range-overflows with code with range-checking disabled --- components/fpdebug/fpdbgcommon.pas | 16 -------------- components/fpdebug/fpdbgdwarfdataclasses.pas | 7 +++++-- components/fpdebug/fpdbgloader.pp | 22 +++++++------------- components/fpdebug/fpimgreaderbase.pas | 11 +++++----- components/fpdebug/fpimgreaderelf.pas | 6 +++++- components/fpdebug/fpimgreaderwinpe.pas | 7 +++---- 6 files changed, 26 insertions(+), 43 deletions(-) diff --git a/components/fpdebug/fpdbgcommon.pas b/components/fpdebug/fpdbgcommon.pas index 1c1a77ff20..e507c3d190 100644 --- a/components/fpdebug/fpdbgcommon.pas +++ b/components/fpdebug/fpdbgcommon.pas @@ -14,12 +14,6 @@ type TByteOrder = (boNone, boLSB, boMSB); TOperatingSystem = (osNone, osBSD, osDarwin, osEmbedded, osLinux, osUnix, osMac, osWindows); - TDBGPtrSign = (sPositive, sNegative); - TDBGPtrOffset = record - Offset: Int64; - Sign: TDBGPtrSign; - end; - TTargetDescriptor = record machineType: TMachineType; bitness: TBitness; @@ -38,8 +32,6 @@ procedure SetCurrentFpDebugThreadIdForAssert(AnId: TThreadID); property CurrentFpDebugThreadIdForAssert: TThreadID write SetCurrentFpDebugThreadIdForAssert; {$ENDIF} -Operator + (Addr : QWord; Offset : TDBGPtrOffset) Res : QWord; - implementation function hostDescriptor: TTargetDescriptor; @@ -66,14 +58,6 @@ begin end; end; -operator+(Addr: QWord; Offset: TDBGPtrOffset) Res: QWord; -begin - if Offset.Sign=sPositive then - Res := Addr + Offset.Offset - else - Res := Addr - Offset.Offset; -end; - {$IFDEF FPDEBUG_THREAD_CHECK} var FCurrentFpDebugThreadIdForAssert: TThreadID; diff --git a/components/fpdebug/fpdbgdwarfdataclasses.pas b/components/fpdebug/fpdbgdwarfdataclasses.pas index 95e1167002..61219eb18d 100644 --- a/components/fpdebug/fpdbgdwarfdataclasses.pas +++ b/components/fpdebug/fpdbgdwarfdataclasses.pas @@ -758,7 +758,7 @@ type FFiles: array of TDwarfDebugFile; private FImageBase: QWord; - FRelocationOffset: TDBGPtrOffset; + FRelocationOffset: QWord; function GetCompilationUnit(AIndex: Integer): TDwarfCompilationUnit; inline; protected function GetCompilationUnitClass: TDwarfCompilationUnitClass; virtual; @@ -781,7 +781,7 @@ type property CompilationUnits[AIndex: Integer]: TDwarfCompilationUnit read GetCompilationUnit; property ImageBase: QWord read FImageBase; - property RelocationOffset: TDBGPtrOffset read FRelocationOffset; + property RelocationOffset: QWord read FRelocationOffset; property WorkQueue: TFpGlobalThreadWorkerQueue read FWorkQueue; end; @@ -4957,7 +4957,10 @@ end; function TDwarfCompilationUnit.CalculateRelocatedAddress(AValue: QWord): QWord; begin + {$push} + {$Q-}{$R-} Result := AValue + FOwner.RelocationOffset; + {$pop} end; function TDwarfCompilationUnit.GetProcStartEnd(const AAddress: TDBGPtr; out diff --git a/components/fpdebug/fpdbgloader.pp b/components/fpdebug/fpdbgloader.pp index 1976221005..e6071a769c 100644 --- a/components/fpdebug/fpdbgloader.pp +++ b/components/fpdebug/fpdbgloader.pp @@ -62,7 +62,7 @@ type FImgReader: TDbgImageReader; function GetAddressMapList: TDbgAddressMapList; function GetImageBase: QWord; - function GetRelocationOffset: TDBGPtrOffset; + function GetRelocationOffset: QWord; function GetReaderErrors: String; function GetSubFiles: TStrings; function GetTargetInfo: TTargetDescriptor; @@ -88,7 +88,7 @@ type property FileName: String read FFileName; // Empty if using USE_WIN_FILE_MAPPING property ImageBase: QWord read GetImageBase; - property RelocationOffset: TDBGPtrOffset read GetRelocationOffset; + property RelocationOffset: QWord read GetRelocationOffset; property TargetInfo: TTargetDescriptor read GetTargetInfo; property UUID: TGuid read GetUUID; @@ -114,7 +114,7 @@ type TDbgImageLoaderList = class(TFPObjectList) private - function GetRelocationOffset: TDBGPtrOffset; + function GetRelocationOffset: QWord; function GetImageBase: QWord; function GetTargetInfo: TTargetDescriptor; function GetItem(Index: Integer): TDbgImageLoader; @@ -124,7 +124,7 @@ type property Items[Index: Integer]: TDbgImageLoader read GetItem write SetItem; default; property ImageBase: QWord read GetImageBase; - property RelocationOffset: TDBGPtrOffset read GetRelocationOffset; + property RelocationOffset: QWord read GetRelocationOffset; property TargetInfo: TTargetDescriptor read GetTargetInfo; end; @@ -132,15 +132,12 @@ implementation { TDbgImageLoaderList } -function TDbgImageLoaderList.GetRelocationOffset: TDBGPtrOffset; +function TDbgImageLoaderList.GetRelocationOffset: QWord; begin if Count>0 then result := Items[0].RelocationOffset else - begin - Result.Offset := 0; - Result.Sign := sPositive; - end; + Result := 0; end; function TDbgImageLoaderList.GetImageBase: QWord; @@ -214,15 +211,12 @@ begin Result := 0; end; -function TDbgImageLoader.GetRelocationOffset: TDBGPtrOffset; +function TDbgImageLoader.GetRelocationOffset: QWord; begin if Assigned(FImgReader) then Result := FImgReader.RelocationOffset else - begin - Result.Offset := 0; - Result.Sign := sPositive; - end; + Result := 0; end; function TDbgImageLoader.GetReaderErrors: String; diff --git a/components/fpdebug/fpimgreaderbase.pas b/components/fpdebug/fpimgreaderbase.pas index df64229ec9..c64d0ae218 100644 --- a/components/fpdebug/fpimgreaderbase.pas +++ b/components/fpdebug/fpimgreaderbase.pas @@ -97,7 +97,7 @@ type private FImageBase: QWord; FImageSize: QWord; - FRelocationOffset: TDBGPtrOffset; + FRelocationOffset: QWord; FLoadedTargetImageAddr: TDBGPtr; FReaderErrors: String; FUUID: TGuid; @@ -110,7 +110,7 @@ type procedure SetUUID(AGuid: TGuid); procedure SetImageBase(ABase: QWord); procedure SetImageSize(ASize: QWord); - procedure SetRelocationOffset(AnOffset: TDBGPtr; Sign: TDBGPtrSign); + procedure SetRelocationOffset(AnOffset: QWord); procedure AddReaderError(AnError: String); function ReadGnuDebugLinkSection(out AFileName: String; out ACrc: Cardinal): Boolean; function LoadGnuDebugLink(ASearchPath, AFileName: String; ACrc: Cardinal): TDbgFileLoader; @@ -144,7 +144,7 @@ type // On linux it is equal to the LoadedTargetImageAddr. // On Windows it is 0, except for libraries which are re-located. In that // case the offset is LoadedTargetImageAddr-ImageBase. - property RelocationOffset: TDBGPtrOffset read FRelocationOffset; + property RelocationOffset: QWord read FRelocationOffset; property TargetInfo: TTargetDescriptor read FTargetInfo; @@ -433,10 +433,9 @@ begin FImageSize := ASize; end; -procedure TDbgImageReader.SetRelocationOffset(AnOffset: TDBGPtr; Sign: TDBGPtrSign); +procedure TDbgImageReader.SetRelocationOffset(AnOffset: QWord); begin - FRelocationOffset.Offset := AnOffset; - FRelocationOffset.Sign := Sign; + FRelocationOffset := AnOffset; end; procedure TDbgImageReader.AddReaderError(AnError: String); diff --git a/components/fpdebug/fpimgreaderelf.pas b/components/fpdebug/fpimgreaderelf.pas index b6160f4a03..eafd627c00 100644 --- a/components/fpdebug/fpimgreaderelf.pas +++ b/components/fpdebug/fpimgreaderelf.pas @@ -414,7 +414,7 @@ begin // Elf-binaries do not have an internal offset encoded into the binary (ImageBase) // so their reloction-offset is just equal to the location at which the binary // has been loaded into memory. (The LoadedTargetImageAddr) - SetRelocationOffset(ALoadedTargetImageAddr, sPositive); + SetRelocationOffset(ALoadedTargetImageAddr); FFileLoader := ASource; fOwnSource := OwnSource; @@ -495,6 +495,7 @@ begin continue; // not loaded, symbol not in memory SymbolName:=pchar(SymbolStr+SymbolArr64^[i].st_name); + {$Q-} AfpSymbolInfo.Add(SymbolName, TDbgPtr(SymbolArr64^[i].st_value+RelocationOffset), Sect^.Address + Sect^.Size + RelocationOffset); end; @@ -519,8 +520,11 @@ begin continue; // not loaded, symbol not in memory SymbolName:=pchar(SymbolStr+SymbolArr32^[i].st_name); + {$push} + {$Q-}{$R-} AfpSymbolInfo.Add(SymbolName, TDBGPtr(SymbolArr32^[i].st_value+RelocationOffset), Sect^.Address + Sect^.Size+RelocationOffset); + {$pop} end; end end; diff --git a/components/fpdebug/fpimgreaderwinpe.pas b/components/fpdebug/fpimgreaderwinpe.pas index 2979a866c4..a30ac03163 100644 --- a/components/fpdebug/fpimgreaderwinpe.pas +++ b/components/fpdebug/fpimgreaderwinpe.pas @@ -463,10 +463,9 @@ begin // relocated) all addresses need a correction. // The difference between the LoadedTargetImageAddr and ImageBase is the offset // that has to be used to calculate the actual addresses in memory. - if LoadedTargetImageAddr >= ImageBase then - SetRelocationOffset(LoadedTargetImageAddr-ImageBase, sPositive) - else - SetRelocationOffset(ImageBase-LoadedTargetImageAddr, sNegative); + {$PUSH}{$Q-}{$R-} + SetRelocationOffset(LoadedTargetImageAddr-ImageBase); + {$POP} FCodeBase := NtHeaders.W32.OptionalHeader.BaseOfCode; SectionMax := FFileLoader.LoadMemory( DosHeader.e_lfanew +