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
|
unit fpDbgSymTableContext;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
FpDbgLoader,
FpImgReaderBase,
DbgIntfBaseTypes,
fpDbgSymTable,
FpdMemoryTools,
FpDbgInfo,
FpDbgCommon;
type
{ TFpSymbolTableProc }
TFpSymbolTableProc = class(TFpSymbol)
public
constructor Create(const AName: String; AnAddr: TDbgPtr);
end;
TFpSymbolInfo = class;
{ TFpSymbolContext }
TFpSymbolContext = class(TFpDbgSymbolScope)
private
FFpSymbolInfo: TFpSymbolInfo;
FSizeOfAddress: integer;
protected
function GetSizeOfAddress: Integer; override;
public
constructor Create(ALocationContext: TFpDbgLocationContext; AFpSymbolInfo: TFpSymbolInfo);
function FindSymbol(const AName: String; const OnlyUnitName: String = ''): TFpValue; override;
end;
{ TFpSymbolInfo }
TFpSymbolInfo = class(TDbgInfo)
private
FSymbolList: TfpSymbolList;
FLibName: String;
public
constructor Create(ALoaderList: TDbgImageLoaderList; AMemManager: TFpDbgMemManager); override;
constructor Create(ALoaderList: TDbgImageLoaderList; AMemManager: TFpDbgMemManager; ALibName: String);
destructor Destroy; override;
function FindSymbolScope(ALocationContext: TFpDbgLocationContext; AAddress: TDbgPtr = 0): TFpDbgSymbolScope; override;
function FindProcSymbol(const AName: String): TFpSymbol; override; overload;
function FindProcSymbol(AnAdress: TDbgPtr): TFpSymbol; overload;
end;
implementation
{ TFpSymbolTableProc }
constructor TFpSymbolTableProc.Create(const AName: String; AnAddr: TDbgPtr);
begin
inherited Create(AName);
SetAddress(TargetLoc(AnAddr));
SetKind(skProcedure);
SetSymbolType(stType);
end;
{ TFpSymbolContext }
function TFpSymbolContext.GetSizeOfAddress: Integer;
begin
result := FSizeOfAddress;
end;
constructor TFpSymbolContext.Create(ALocationContext: TFpDbgLocationContext;
AFpSymbolInfo: TFpSymbolInfo);
begin
inherited create(ALocationContext);
FFpSymbolInfo:=AFpSymbolInfo;
end;
function TFpSymbolContext.FindSymbol(const AName: String;
const OnlyUnitName: String): TFpValue;
var
i: integer;
val: TFpDbgMemLocation;
begin
i := FFpSymbolInfo.FSymbolList.IndexOf(AName);
if i > -1 then
begin
val := Default(TFpDbgMemLocation);
val.Address:=FFpSymbolInfo.FSymbolList.DataPtr[i]^.Addr;
val.MType:=mlfTargetMem;
result := TFpValueConstAddress.Create(val);
end
else
result := nil;
end;
{ TFpSymbolInfo }
constructor TFpSymbolInfo.Create(ALoaderList: TDbgImageLoaderList;
AMemManager: TFpDbgMemManager);
var
i: Integer;
begin
inherited Create(ALoaderList, AMemManager);
FSymbolList := TfpSymbolList.Create;
for i := 0 to ALoaderList.Count-1 do
ALoaderList[i].ParseSymbolTable(FSymbolList);
FTargetInfo := ALoaderList.TargetInfo;
if FSymbolList.Count > 0 then
SetHasInfo;
end;
constructor TFpSymbolInfo.Create(ALoaderList: TDbgImageLoaderList;
AMemManager: TFpDbgMemManager; ALibName: String);
begin
FLibName := ALibName;
Create(ALoaderList, AMemManager);
end;
destructor TFpSymbolInfo.Destroy;
begin
FSymbolList.Free;
inherited Destroy;
end;
function TFpSymbolInfo.FindSymbolScope(ALocationContext: TFpDbgLocationContext;
AAddress: TDbgPtr): TFpDbgSymbolScope;
begin
assert(False, 'TFpSymbolInfo.FindSymbolScope: False');
Result := TFpSymbolContext.Create(ALocationContext, Self);
end;
function TFpSymbolInfo.FindProcSymbol(const AName: String): TFpSymbol;
var
i: integer;
begin
i := FSymbolList.IndexOf(AName);
if i >= 0 then
Result := TFpSymbolTableProc.Create(AName, FSymbolList.DataPtr[i]^.Addr)
else
result := nil;
end;
function TFpSymbolInfo.FindProcSymbol(AnAdress: TDbgPtr): TFpSymbol;
var
CheckRange: Boolean;
i, NearestIdx: integer;
a, NearestAddr: TDBGPtr;
NPreFix: String;
d: PfpLinkerSymbol;
begin
NPreFix := '';
if FLibName <> '' then
NPreFix := FLibName+':';
CheckRange :=
(FSymbolList.HighAddr > FSymbolList.LowAddr) and
(AnAdress >= FSymbolList.LowAddr) and
(AnAdress < FSymbolList.HighAddr);
NearestIdx := -1;
NearestAddr := 0;
Result := nil;
i := FSymbolList.Count - 1;
while i >= 0 do begin
d := FSymbolList.DataPtr[i];
a := d^.Addr;
if (a = AnAdress) then begin
Result := TFpSymbolTableProc.Create(NPreFix + FSymbolList.Keys[i], a);
exit;
end;
if CheckRange and (a <= AnAdress) and (a > NearestAddr) and
( (d^.SectionEnd = 0) or (AnAdress <= d^.SectionEnd) )
then begin
NearestIdx := i;
NearestAddr := a;
end;
dec(i);
end;
if NearestIdx >= 0 then begin
Result := TFpSymbolTableProc.Create(NPreFix + FSymbolList.Keys[NearestIdx], FSymbolList.DataPtr[NearestIdx]^.Addr);
end;
end;
end.
|