forked from vdisasm/pe-image-for-delphi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPE.ExportSym.pas
203 lines (165 loc) · 4.52 KB
/
PE.ExportSym.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
{
PE Exported symbols are case-sensitive.
}
unit PE.ExportSym;
interface
uses
Generics.Collections,
SysUtils,
PE.Common;
type
{ TPEExportSym }
TPEExportSym = class
public
RVA: TRVA;
Ordinal: uint32;
Name: String;
ForwarderName: String;
Forwarder: boolean;
// Is this symbol has RVA or is forwarder.
function IsValid: boolean; inline;
procedure Clear;
function Clone: TPEExportSym;
// Parse forwarder name of following structure: "LibName.FuncName".
// Return true if both names before and after dot found.
function GetForwarderLibAndFuncName(out Lib, Name: string): boolean;
end;
PPEExportSym = ^TPEExportSym;
TPEExportSymVec = TList<TPEExportSym>;
TPEExportSymByRVA = TDictionary<TRVA, TPEExportSym>;
{ TPEExportSyms }
TPEExportSyms = class
private
FItems: TPEExportSymVec;
// FItemsByRVA: TPEExportSymByRVA;
function GetCount: integer;
//procedure ExportSymNotify(Sender: TObject; const Item: TPEExportSym;
//Action: TCollectionNotification);
procedure ExportSymNotify(Sender: TObject; constref Item: TPEExportSym;
Action: TCollectionNotification);
public
constructor Create;
destructor Destroy; override;
// Add item to list of symbols.
// If SetOrdinal is True, src Item ordinal will be set to last sym number.
procedure Add(Sym: TPEExportSym; SetOrdinal: boolean = false);
// Add symbol by Name.
procedure AddByName(RVA: TRVA; const Name: String);
// Usually you don't need to set Ordinal, because ordinals are auto-incremented.
procedure AddByOrdinal(RVA: TRVA; Ordinal: dword = 0);
procedure AddForwarder(const Name, ForwarderName: String);
procedure Clear;
// Get item by RVA or nil if not found.
// todo: there can be many exports with same RVA
// function GetItemByRVA(RVA: TRVA): TPEExportSym; inline;
property Count: integer read GetCount;
property Items: TPEExportSymVec read FItems;
end;
implementation
function TPEExportSym.Clone: TPEExportSym;
begin
result := TPEExportSym.Create;
result.RVA := self.RVA;
result.Ordinal := self.Ordinal;
result.Name := self.Name;
result.ForwarderName := self.ForwarderName;
result.Forwarder := self.Forwarder;
end;
function TPEExportSym.GetForwarderLibAndFuncName(out Lib, Name: string): boolean;
var
arr: TArray<string>;
begin
arr := string(ForwarderName).Split(['.']);
result := length(arr) = 2;
if result then
begin
Lib := arr[0];
name := arr[1];
end
else
begin
Lib := '';
name := '';
end;
end;
function TPEExportSym.IsValid: boolean;
begin
// Either forwarder or has rva.
result := Forwarder or (RVA <> 0);
end;
procedure TPEExportSym.Clear;
begin
RVA := 0;
Ordinal := 0;
Name := '';
ForwarderName := '';
Forwarder := false;
end;
{ TExportSyms }
procedure TPEExportSyms.Add(Sym: TPEExportSym; SetOrdinal: boolean = false);
begin
if SetOrdinal then
Sym.Ordinal := FItems.Count + 1;
FItems.Add(Sym);
end;
procedure TPEExportSyms.AddByName(RVA: TRVA; const Name: String);
var
Sym: TPEExportSym;
begin
Sym := TPEExportSym.Create;
Sym.RVA := RVA;
Sym.Name := Name;
Add(Sym, True);
end;
procedure TPEExportSyms.AddByOrdinal(RVA: TRVA; Ordinal: dword);
var
Sym: TPEExportSym;
begin
Sym := TPEExportSym.Create;
Sym.RVA := RVA;
Sym.Ordinal := Ordinal;
Add(Sym, Ordinal = 0);
end;
procedure TPEExportSyms.AddForwarder(const Name, ForwarderName: String);
var
Sym: TPEExportSym;
begin
Sym := TPEExportSym.Create;
Sym.Name := Name;
Sym.ForwarderName := ForwarderName;
Sym.Forwarder := True;
Add(Sym, True);
end;
procedure TPEExportSyms.Clear;
begin
FItems.Clear;
// FItemsByRVA.Clear;
end;
constructor TPEExportSyms.Create;
begin
FItems := TPEExportSymVec.Create;
FItems.OnNotify := ExportSymNotify;
// FItemsByRVA := TPEExportSymByRVA.Create;
end;
destructor TPEExportSyms.Destroy;
begin
// FItemsByRVA.Free;
FItems.Free;
inherited;
end;
procedure TPEExportSyms.ExportSymNotify(Sender: TObject;
constref Item: TPEExportSym; Action: TCollectionNotification);
begin
if Action = cnRemoved then
Item.Free;
end;
function TPEExportSyms.GetCount: integer;
begin
result := FItems.Count;
end;
// function TPEExportSyms.GetItemByRVA(RVA: TRVA): TPEExportSym;
// begin
// if not FItemsByRVA.TryGetValue(RVA, result) then
// result := nil;
// end;
end.