-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathPE.Build.pas
165 lines (139 loc) · 4.57 KB
/
PE.Build.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
unit PE.Build;
interface
uses
System.Classes,
PE.Common,
PE.Section;
{
* Rebuild directory data.
*
* If TryToOverwritesection is True, it will try to put new section at
* old section space (if new section is smaller).
*
* If new section is bigger than old it will be forced to create new section.
*
* Result is new section if it was created or nil if old section was replaced.
}
function ReBuildDirData(PE: TObject; DDIR_ID: integer; Overwrite: boolean): TPESection;
implementation
uses
PE.Image,
PE.Types.Directories,
PE.Build.Common,
PE.Build.Export,
PE.Build.Import,
PE.Build.Resource,
PE.Build.Relocs;
const
RebuilderTable: array [0 .. DDIR_LAST] of TDirectoryBuilderClass =
(
PE.Build.Export.TExportBuilder, // export
PE.Build.Import.TImportBuilder, // import
PE.Build.Resource.TRsrcBuilder, // resources
nil, // exception
nil, // certificate
PE.Build.Relocs.TRelocBuilder, // relocations
nil, // debug
nil, // architecture
nil, // global ptr
nil, // tls
nil, // load config
nil, // bound import
nil, // iat
nil, // delay import
nil // clr runtime header
);
function ReBuildDirData(PE: TObject; DDIR_ID: integer; Overwrite: boolean): TPESection;
var
stream: TMemoryStream;
builder: TDirectoryBuilder;
img: TPEImage;
sec: TPESection;
dir: TImageDataDirectory;
prognoseRVA, destRVA: TRVA;
destMem: Pointer;
destSize: uint32;
begin
Result := nil;
if (DDIR_ID < 0) or (DDIR_ID > High(RebuilderTable)) then
exit; // no builder found
if RebuilderTable[DDIR_ID] = nil then
exit; // no builder found
img := PE as TPEImage;
builder := RebuilderTable[DDIR_ID].Create(img);
stream := TMemoryStream.Create;
try
// Prognose dest RVA.
if img.DataDirectories.Get(DDIR_ID, @dir) then
prognoseRVA := dir.VirtualAddress
else
prognoseRVA := 0;
// Build to get size.
builder.Build(prognoseRVA, stream);
sec := nil;
destRVA := 0; // compiler friendly
destSize := 0; // compiler friendly
// Try to get old section space.
if Overwrite then
if img.DataDirectories.Get(DDIR_ID, @dir) then
if dir.Size >= stream.Size then
if img.RVAToSec(dir.VirtualAddress, @sec) then
begin
// If directory occupies whole section.
if (sec.RVA = dir.VirtualAddress) and (sec.RawSize = dir.Size) then
begin
// Leave section as it is.
end;
// Set dest rva/size (reuse this section).
destRVA := dir.VirtualAddress;
destSize := dir.Size;
end;
// If stream is empty, no need to rebuild anything.
if stream.Size <> 0 then
begin
// If we still have no section, create new with default name and flags.
// User can change it later.
if sec = nil then
begin
sec := img.Sections.AddNew(builder.GetDefaultSectionName,
stream.Size, builder.GetDefaultSectionFlags, nil);
destRVA := sec.RVA;
destSize := stream.Size;
// Make old data directory region unused.
if img.DataDirectories.Get(DDIR_ID, @dir) then
img.RegionRemove(dir.VirtualAddress, dir.Size);
end;
// Rebuild data to have valid RVAs (if prognose is wrong)
if builder.NeedRebuildingIfRVAChanged then
if prognoseRVA <> destRVA then
begin
stream.Clear;
builder.Build(destRVA, stream);
end;
// Get address where data of built directory should reside.
destMem := img.RVAToMem(destRVA);
// Move built data to section.
Move(stream.Memory^, destMem^, stream.Size);
end
else
begin
// If stream size = 0
destRVA := 0;
destSize := 0;
end;
// Update directory pointer.
img.DataDirectories.Put(DDIR_ID, destRVA, destSize);
// For imports also update IAT table.
if DDIR_ID = DDIR_IMPORT then
begin
img.DataDirectories.Put(DDIR_IAT,
TImportBuilder(builder).BuiltIatRVA,
TImportBuilder(builder).BuiltIatSize);
end;
Result := sec;
finally
builder.Free;
stream.Free;
end;
end;
end.