Skip to content

Commit 5fa7c8a

Browse files
committed
Fixed building imports when there is mix of original parsed libraries and new added libraries.
Import library now raise exception on attempt to add new function to original library (new library should be added instead). IAT updated only for added libraries.
1 parent 122cb60 commit 5fa7c8a

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

PE.Build.Import.pas

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,18 @@ procedure WriteFunctionNamesOrOrdinalsAndIat(
136136
exit;
137137

138138
idt.ImportLookupTableRVA := DirRVA + ofs_name_pointers;
139-
idt.ImportAddressTable := DirRVA + ofs_iat;
140139

141-
// Update IAT in library.
142-
Lib.IatRva := idt.ImportAddressTable;
140+
if (not Lib.Original) then
141+
begin
142+
idt.ImportAddressTable := DirRVA + ofs_iat;
143+
144+
// Update IAT in library.
145+
Lib.IatRva := idt.ImportAddressTable;
146+
end
147+
else
148+
begin
149+
idt.ImportAddressTable := Lib.IatRva;
150+
end;
143151

144152
hint := 0;
145153
for fn in Lib.Functions do

PE.Imports.Lib.pas

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface
44

55
uses
66
System.Classes,
7+
System.SysUtils,
78

89
PE.Common,
910
PE.Imports.Func;
@@ -15,6 +16,8 @@ TPEImportLibrary = class
1516
FBound: Boolean;
1617
FFunctions: TPEImportFunctions;
1718
FTimeDateStamp: uint32;
19+
FOriginal: boolean;
20+
procedure CheckAddingToOriginalLib;
1821
public
1922
// Relative address of IAT region for this library.
2023
// It is address of first word in array of words (4/8 bytes) corresponding
@@ -29,7 +32,7 @@ TPEImportLibrary = class
2932
// when import directory is rebuilt.
3033
IatRva: TRVA;
3134

32-
constructor Create(const AName: String; Bound: Boolean = False);
35+
constructor Create(const AName: String; Bound: Boolean = False; Original: Boolean = False);
3336
destructor Destroy; override;
3437

3538
function NewFunction(const Name: string): TPEImportFunction; overload;
@@ -43,18 +46,24 @@ TPEImportLibrary = class
4346

4447
property Bound: Boolean read FBound;
4548
property TimeDateStamp: uint32 read FTimeDateStamp write FTimeDateStamp;
49+
50+
// True if it is library parsed from executable.
51+
// You can't add new functions to this library, because IAT must stay untouched.
52+
// Add new library instead.
53+
property Original: boolean read FOriginal;
4654
end;
4755

4856
implementation
4957

5058
{ TImportLibrary }
5159

52-
constructor TPEImportLibrary.Create(const AName: String; Bound: Boolean);
60+
constructor TPEImportLibrary.Create(const AName: String; Bound: Boolean; Original: Boolean);
5361
begin
5462
inherited Create;
5563
FFunctions := TPEImportFunctions.Create;
5664
FName := AName;
5765
FBound := Bound;
66+
FOriginal := Original;
5867
end;
5968

6069
destructor TPEImportLibrary.Destroy;
@@ -63,14 +72,22 @@ destructor TPEImportLibrary.Destroy;
6372
inherited;
6473
end;
6574

75+
procedure TPEImportLibrary.CheckAddingToOriginalLib();
76+
begin
77+
if (Original) then
78+
raise Exception.Create('You can''t add new function to original library.');
79+
end;
80+
6681
function TPEImportLibrary.NewFunction(const Name: string): TPEImportFunction;
6782
begin
83+
CheckAddingToOriginalLib();
6884
Result := TPEImportFunction.Create(Name);
6985
FFunctions.Add(Result);
7086
end;
7187

7288
function TPEImportLibrary.NewFunction(Ordinal: uint16): TPEImportFunction;
7389
begin
90+
CheckAddingToOriginalLib();
7491
Result := TPEImportFunction.Create('', Ordinal);
7592
FFunctions.Add(Result);
7693
end;

PE.Parser.Import.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function TPEImportParser.Parse: TParserResult;
192192
begin
193193
// Create lib once in loop.
194194
// Added after loop (if not discarded).
195-
Lib := TPEImportLibrary.Create(LibraryName, IDir.IsBound);
195+
Lib := TPEImportLibrary.Create(LibraryName, IDir.IsBound, True);
196196
Lib.TimeDateStamp := IDir.TimeDateStamp;
197197
Lib.IATRVA := IATRVA;
198198
end;

PE.Parser.ImportDelayed.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function ParseTable(
8383

8484
if not Testing then
8585
begin
86-
Lib := TPEImportLibrary.Create(DllName);
86+
Lib := TPEImportLibrary.Create(DllName, False, True);
8787
PE.ImportsDelayed.Add(Lib);
8888
end
8989
else

0 commit comments

Comments
 (0)