2020-05-25 23:48:33 +03:00
|
|
|
(*
|
2019-03-11 11:59:55 +03:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Copyright (c) 2018-2020, Anton Krotov
|
2019-03-11 11:59:55 +03:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE PE32;
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
IMPORT BIN, LISTS, UTILS, WR := WRITER, CHL := CHUNKLISTS;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
SIZE_OF_DWORD = 4;
|
|
|
|
SIZE_OF_WORD = 2;
|
|
|
|
|
|
|
|
SIZE_OF_IMAGE_EXPORT_DIRECTORY = 40;
|
|
|
|
|
|
|
|
IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
|
|
|
|
|
|
|
|
IMAGE_SIZEOF_SHORT_NAME = 8;
|
|
|
|
|
|
|
|
SIZE_OF_IMAGE_FILE_HEADER* = 20;
|
|
|
|
|
|
|
|
SIZE_OF_IMAGE_SECTION_HEADER* = 40;
|
|
|
|
|
|
|
|
(* SectionHeader.Characteristics *)
|
|
|
|
|
|
|
|
SHC_text = 060000020H;
|
2019-10-06 20:55:12 +03:00
|
|
|
SHC_data = 040000040H;
|
|
|
|
SHC_bss = 0C0000080H;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
SectionAlignment = 1000H;
|
|
|
|
FileAlignment = 200H;
|
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
WORD = WCHAR;
|
|
|
|
DWORD = INTEGER;
|
|
|
|
|
|
|
|
NAME* = ARRAY IMAGE_SIZEOF_SHORT_NAME OF CHAR;
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_DATA_DIRECTORY = RECORD
|
|
|
|
|
|
|
|
VirtualAddress: DWORD;
|
|
|
|
Size: DWORD
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_OPTIONAL_HEADER = RECORD
|
|
|
|
|
|
|
|
Magic: WORD;
|
|
|
|
MajorLinkerVersion: BYTE;
|
|
|
|
MinorLinkerVersion: BYTE;
|
|
|
|
SizeOfCode: DWORD;
|
|
|
|
SizeOfInitializedData: DWORD;
|
|
|
|
SizeOfUninitializedData: DWORD;
|
|
|
|
AddressOfEntryPoint: DWORD;
|
|
|
|
BaseOfCode: DWORD;
|
|
|
|
BaseOfData: DWORD;
|
|
|
|
ImageBase: DWORD;
|
|
|
|
SectionAlignment: DWORD;
|
|
|
|
FileAlignment: DWORD;
|
|
|
|
MajorOperatingSystemVersion: WORD;
|
|
|
|
MinorOperatingSystemVersion: WORD;
|
|
|
|
MajorImageVersion: WORD;
|
|
|
|
MinorImageVersion: WORD;
|
|
|
|
MajorSubsystemVersion: WORD;
|
|
|
|
MinorSubsystemVersion: WORD;
|
|
|
|
Win32VersionValue: DWORD;
|
|
|
|
SizeOfImage: DWORD;
|
|
|
|
SizeOfHeaders: DWORD;
|
|
|
|
CheckSum: DWORD;
|
|
|
|
Subsystem: WORD;
|
|
|
|
DllCharacteristics: WORD;
|
|
|
|
SizeOfStackReserve: DWORD;
|
|
|
|
SizeOfStackCommit: DWORD;
|
|
|
|
SizeOfHeapReserve: DWORD;
|
|
|
|
SizeOfHeapCommit: DWORD;
|
|
|
|
LoaderFlags: DWORD;
|
|
|
|
NumberOfRvaAndSizes: DWORD;
|
|
|
|
|
|
|
|
DataDirectory: ARRAY IMAGE_NUMBEROF_DIRECTORY_ENTRIES OF IMAGE_DATA_DIRECTORY
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_FILE_HEADER* = RECORD
|
|
|
|
|
|
|
|
Machine*: WORD;
|
|
|
|
NumberOfSections*: WORD;
|
|
|
|
TimeDateStamp*: DWORD;
|
|
|
|
PointerToSymbolTable*: DWORD;
|
|
|
|
NumberOfSymbols*: DWORD;
|
|
|
|
SizeOfOptionalHeader*: WORD;
|
|
|
|
Characteristics*: WORD
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_SECTION_HEADER* = RECORD
|
|
|
|
|
|
|
|
Name*: NAME;
|
|
|
|
|
|
|
|
VirtualSize*,
|
|
|
|
VirtualAddress*,
|
|
|
|
SizeOfRawData*,
|
|
|
|
PointerToRawData*,
|
|
|
|
PointerToRelocations*,
|
|
|
|
PointerToLinenumbers*: DWORD;
|
|
|
|
|
|
|
|
NumberOfRelocations*,
|
|
|
|
NumberOfLinenumbers*: WORD;
|
|
|
|
|
|
|
|
Characteristics*: DWORD
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_EXPORT_DIRECTORY = RECORD
|
|
|
|
|
|
|
|
Characteristics: DWORD;
|
|
|
|
TimeDateStamp: DWORD;
|
|
|
|
MajorVersion: WORD;
|
|
|
|
MinorVersion: WORD;
|
|
|
|
Name,
|
|
|
|
Base,
|
|
|
|
NumberOfFunctions,
|
|
|
|
NumberOfNames,
|
|
|
|
AddressOfFunctions,
|
|
|
|
AddressOfNames,
|
|
|
|
AddressOfNameOrdinals: DWORD
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
VIRTUAL_ADDR* = RECORD
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Code*, Data*, Bss*, Import*: INTEGER
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Signature: ARRAY 4 OF BYTE;
|
|
|
|
FileHeader: IMAGE_FILE_HEADER;
|
|
|
|
OptionalHeader: IMAGE_OPTIONAL_HEADER;
|
|
|
|
|
2019-03-11 11:59:55 +03:00
|
|
|
msdos: ARRAY 128 OF BYTE;
|
|
|
|
SectionHeaders: ARRAY 16 OF IMAGE_SECTION_HEADER;
|
|
|
|
libcnt: INTEGER;
|
2020-05-25 23:48:33 +03:00
|
|
|
SizeOfWord: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Export (program: BIN.PROGRAM; name: INTEGER; VAR ExportDir: IMAGE_EXPORT_DIRECTORY): INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
ExportDir.Characteristics := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
ExportDir.TimeDateStamp := FileHeader.TimeDateStamp;
|
2019-03-11 11:59:55 +03:00
|
|
|
ExportDir.MajorVersion := 0X;
|
|
|
|
ExportDir.MinorVersion := 0X;
|
2020-10-13 10:58:51 +03:00
|
|
|
ExportDir.Name := name;
|
2019-03-11 11:59:55 +03:00
|
|
|
ExportDir.Base := 0;
|
|
|
|
ExportDir.NumberOfFunctions := LISTS.count(program.exp_list);
|
|
|
|
ExportDir.NumberOfNames := ExportDir.NumberOfFunctions;
|
|
|
|
ExportDir.AddressOfFunctions := SIZE_OF_IMAGE_EXPORT_DIRECTORY;
|
|
|
|
ExportDir.AddressOfNames := ExportDir.AddressOfFunctions + ExportDir.NumberOfFunctions * SIZE_OF_DWORD;
|
|
|
|
ExportDir.AddressOfNameOrdinals := ExportDir.AddressOfNames + ExportDir.NumberOfFunctions * SIZE_OF_DWORD
|
|
|
|
|
|
|
|
RETURN SIZE_OF_IMAGE_EXPORT_DIRECTORY + ExportDir.NumberOfFunctions * (2 * SIZE_OF_DWORD + SIZE_OF_WORD)
|
|
|
|
END Export;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetProcCount (lib: BIN.IMPRT): INTEGER;
|
|
|
|
VAR
|
2020-10-13 10:58:51 +03:00
|
|
|
imp: BIN.IMPRT;
|
|
|
|
res: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
res := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := lib.next(BIN.IMPRT);
|
|
|
|
WHILE (imp # NIL) & (imp.label # 0) DO
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(res);
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END GetProcCount;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetImportSize (imp_list: LISTS.LIST): INTEGER;
|
|
|
|
VAR
|
2020-10-13 10:58:51 +03:00
|
|
|
imp: BIN.IMPRT;
|
2019-03-11 11:59:55 +03:00
|
|
|
proccnt: INTEGER;
|
|
|
|
procoffs: INTEGER;
|
|
|
|
OriginalCurrentThunk,
|
|
|
|
CurrentThunk: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
libcnt := 0;
|
|
|
|
proccnt := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE imp # NIL DO
|
|
|
|
IF imp.label = 0 THEN
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(libcnt)
|
|
|
|
ELSE
|
|
|
|
INC(proccnt)
|
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
procoffs := 0;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE imp # NIL DO
|
|
|
|
IF imp.label = 0 THEN
|
|
|
|
imp.OriginalFirstThunk := procoffs;
|
|
|
|
imp.FirstThunk := procoffs + (GetProcCount(imp) + 1);
|
|
|
|
OriginalCurrentThunk := imp.OriginalFirstThunk;
|
|
|
|
CurrentThunk := imp.FirstThunk;
|
|
|
|
INC(procoffs, (GetProcCount(imp) + 1) * 2)
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
imp.OriginalFirstThunk := OriginalCurrentThunk;
|
|
|
|
imp.FirstThunk := CurrentThunk;
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(OriginalCurrentThunk);
|
|
|
|
INC(CurrentThunk)
|
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
RETURN (libcnt + 1) * 5 * SIZE_OF_DWORD + (proccnt + libcnt) * 2 * SizeOfWord
|
2019-03-11 11:59:55 +03:00
|
|
|
END GetImportSize;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE fixup* (program: BIN.PROGRAM; Address: VIRTUAL_ADDR; amd64: BOOLEAN);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
2020-05-25 23:48:33 +03:00
|
|
|
reloc: BIN.RELOC;
|
|
|
|
iproc: BIN.IMPRT;
|
|
|
|
code: CHL.BYTELIST;
|
2020-10-13 10:58:51 +03:00
|
|
|
L, delta, delta0, AdrImp, offset: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
AdrImp := Address.Import + (libcnt + 1) * 5 * SIZE_OF_DWORD;
|
2020-05-25 23:48:33 +03:00
|
|
|
code := program.code;
|
2019-03-11 11:59:55 +03:00
|
|
|
reloc := program.rel_list.first(BIN.RELOC);
|
2020-10-13 10:58:51 +03:00
|
|
|
delta0 := 3 - 7 * ORD(amd64) - Address.Code;
|
2020-05-25 23:48:33 +03:00
|
|
|
|
2019-03-11 11:59:55 +03:00
|
|
|
WHILE reloc # NIL DO
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
offset := reloc.offset;
|
|
|
|
L := BIN.get32le(code, offset);
|
|
|
|
delta := delta0 - offset;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
CASE reloc.opcode OF
|
|
|
|
|BIN.PICDATA:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, L + Address.Data)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICCODE:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, BIN.GetLabel(program, L) + Address.Code)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICBSS:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, L + Address.Bss)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICIMP:
|
2020-10-13 10:58:51 +03:00
|
|
|
iproc := BIN.GetIProc(program, L);
|
|
|
|
INC(delta, iproc.FirstThunk * SizeOfWord + AdrImp)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
BIN.put32le(code, offset, delta);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
reloc := reloc.next(BIN.RELOC)
|
|
|
|
END
|
|
|
|
END fixup;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteWord (w: WORD);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write16LE(ORD(w))
|
2019-03-11 11:59:55 +03:00
|
|
|
END WriteWord;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteName* (name: NAME);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i, nameLen: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
nameLen := LENGTH(name);
|
|
|
|
|
|
|
|
FOR i := 0 TO nameLen - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(ORD(name[i]))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
i := LEN(name) - nameLen;
|
|
|
|
WHILE i > 0 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(0);
|
2019-03-11 11:59:55 +03:00
|
|
|
DEC(i)
|
|
|
|
END
|
|
|
|
|
|
|
|
END WriteName;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteSectionHeader* (h: IMAGE_SECTION_HEADER);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i, nameLen: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
nameLen := LENGTH(h.Name);
|
|
|
|
|
|
|
|
FOR i := 0 TO nameLen - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(ORD(h.Name[i]))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
i := LEN(h.Name) - nameLen;
|
|
|
|
WHILE i > 0 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(0);
|
2019-03-11 11:59:55 +03:00
|
|
|
DEC(i)
|
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.VirtualSize);
|
|
|
|
WR.Write32LE(h.VirtualAddress);
|
|
|
|
WR.Write32LE(h.SizeOfRawData);
|
|
|
|
WR.Write32LE(h.PointerToRawData);
|
|
|
|
WR.Write32LE(h.PointerToRelocations);
|
|
|
|
WR.Write32LE(h.PointerToLinenumbers);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteWord(h.NumberOfRelocations);
|
|
|
|
WriteWord(h.NumberOfLinenumbers);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.Characteristics)
|
2019-03-11 11:59:55 +03:00
|
|
|
END WriteSectionHeader;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteFileHeader* (h: IMAGE_FILE_HEADER);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteWord(h.Machine);
|
|
|
|
WriteWord(h.NumberOfSections);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.TimeDateStamp);
|
|
|
|
WR.Write32LE(h.PointerToSymbolTable);
|
|
|
|
WR.Write32LE(h.NumberOfSymbols);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteWord(h.SizeOfOptionalHeader);
|
|
|
|
WriteWord(h.Characteristics)
|
2019-03-11 11:59:55 +03:00
|
|
|
END WriteFileHeader;
|
|
|
|
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
PROCEDURE write* (program: BIN.PROGRAM; FileName: ARRAY OF CHAR; console, dll, amd64: BOOLEAN);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
2020-10-13 10:58:51 +03:00
|
|
|
i, n, temp: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
Size: RECORD
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Code, Data, Bss, Import, Reloc, Export: INTEGER
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
BaseAddress: INTEGER;
|
|
|
|
|
2019-03-11 11:59:55 +03:00
|
|
|
Address: VIRTUAL_ADDR;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
_import: BIN.IMPRT;
|
2019-03-11 11:59:55 +03:00
|
|
|
ImportTable: CHL.INTLIST;
|
|
|
|
|
|
|
|
ExportDir: IMAGE_EXPORT_DIRECTORY;
|
|
|
|
export: BIN.EXPRT;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteExportDir (e: IMAGE_EXPORT_DIRECTORY);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(e.Characteristics);
|
|
|
|
WR.Write32LE(e.TimeDateStamp);
|
|
|
|
|
|
|
|
WriteWord(e.MajorVersion);
|
|
|
|
WriteWord(e.MinorVersion);
|
|
|
|
|
|
|
|
WR.Write32LE(e.Name);
|
|
|
|
WR.Write32LE(e.Base);
|
|
|
|
WR.Write32LE(e.NumberOfFunctions);
|
|
|
|
WR.Write32LE(e.NumberOfNames);
|
|
|
|
WR.Write32LE(e.AddressOfFunctions);
|
|
|
|
WR.Write32LE(e.AddressOfNames);
|
|
|
|
WR.Write32LE(e.AddressOfNameOrdinals)
|
2019-03-11 11:59:55 +03:00
|
|
|
END WriteExportDir;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteOptHeader (h: IMAGE_OPTIONAL_HEADER; amd64: BOOLEAN);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteWord(h.Magic);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(h.MajorLinkerVersion);
|
|
|
|
WR.WriteByte(h.MinorLinkerVersion);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.SizeOfCode);
|
|
|
|
WR.Write32LE(h.SizeOfInitializedData);
|
|
|
|
WR.Write32LE(h.SizeOfUninitializedData);
|
|
|
|
WR.Write32LE(h.AddressOfEntryPoint);
|
|
|
|
WR.Write32LE(h.BaseOfCode);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
IF amd64 THEN
|
|
|
|
WR.Write64LE(h.ImageBase)
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.BaseOfData);
|
|
|
|
WR.Write32LE(h.ImageBase)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.SectionAlignment);
|
|
|
|
WR.Write32LE(h.FileAlignment);
|
|
|
|
|
|
|
|
WriteWord(h.MajorOperatingSystemVersion);
|
|
|
|
WriteWord(h.MinorOperatingSystemVersion);
|
|
|
|
WriteWord(h.MajorImageVersion);
|
|
|
|
WriteWord(h.MinorImageVersion);
|
|
|
|
WriteWord(h.MajorSubsystemVersion);
|
|
|
|
WriteWord(h.MinorSubsystemVersion);
|
|
|
|
|
|
|
|
WR.Write32LE(h.Win32VersionValue);
|
|
|
|
WR.Write32LE(h.SizeOfImage);
|
|
|
|
WR.Write32LE(h.SizeOfHeaders);
|
|
|
|
WR.Write32LE(h.CheckSum);
|
|
|
|
|
|
|
|
WriteWord(h.Subsystem);
|
|
|
|
WriteWord(h.DllCharacteristics);
|
|
|
|
|
|
|
|
IF amd64 THEN
|
|
|
|
WR.Write64LE(h.SizeOfStackReserve);
|
|
|
|
WR.Write64LE(h.SizeOfStackCommit);
|
|
|
|
WR.Write64LE(h.SizeOfHeapReserve);
|
|
|
|
WR.Write64LE(h.SizeOfHeapCommit)
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.SizeOfStackReserve);
|
|
|
|
WR.Write32LE(h.SizeOfStackCommit);
|
|
|
|
WR.Write32LE(h.SizeOfHeapReserve);
|
|
|
|
WR.Write32LE(h.SizeOfHeapCommit)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.LoaderFlags);
|
|
|
|
WR.Write32LE(h.NumberOfRvaAndSizes);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
FOR i := 0 TO LEN(h.DataDirectory) - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(h.DataDirectory[i].VirtualAddress);
|
|
|
|
WR.Write32LE(h.DataDirectory[i].Size)
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
|
|
|
|
END WriteOptHeader;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE InitSection (VAR section: IMAGE_SECTION_HEADER; Name: NAME; VirtualSize: INTEGER; Characteristics: DWORD);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
|
|
|
section.Name := Name;
|
2020-10-13 10:58:51 +03:00
|
|
|
section.VirtualSize := VirtualSize;
|
|
|
|
section.SizeOfRawData := WR.align(VirtualSize, FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
section.PointerToRelocations := 0;
|
|
|
|
section.PointerToLinenumbers := 0;
|
|
|
|
section.NumberOfRelocations := 0X;
|
|
|
|
section.NumberOfLinenumbers := 0X;
|
|
|
|
section.Characteristics := Characteristics
|
|
|
|
END InitSection;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
SizeOfWord := SIZE_OF_DWORD * (ORD(amd64) + 1);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
Size.Code := CHL.Length(program.code);
|
|
|
|
Size.Data := CHL.Length(program.data);
|
|
|
|
Size.Bss := program.bss;
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
IF dll THEN
|
|
|
|
BaseAddress := 10000000H
|
|
|
|
ELSE
|
|
|
|
BaseAddress := 400000H
|
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Signature[0] := 50H;
|
|
|
|
Signature[1] := 45H;
|
|
|
|
Signature[2] := 0;
|
|
|
|
Signature[3] := 0;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
IF amd64 THEN
|
2020-10-13 10:58:51 +03:00
|
|
|
FileHeader.Machine := 08664X
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
FileHeader.Machine := 014CX
|
|
|
|
END;
|
|
|
|
|
|
|
|
FileHeader.NumberOfSections := WCHR(4 + ORD(dll));
|
|
|
|
|
|
|
|
FileHeader.TimeDateStamp := UTILS.UnixTime();
|
|
|
|
FileHeader.PointerToSymbolTable := 0H;
|
|
|
|
FileHeader.NumberOfSymbols := 0H;
|
|
|
|
FileHeader.SizeOfOptionalHeader := WCHR(0E0H + 10H * ORD(amd64));
|
|
|
|
FileHeader.Characteristics := WCHR(010EH + (20H - 100H) * ORD(amd64) + 2000H * ORD(dll));
|
|
|
|
|
|
|
|
OptionalHeader.Magic := WCHR(010BH + 100H * ORD(amd64));
|
|
|
|
OptionalHeader.MajorLinkerVersion := UTILS.vMajor;
|
|
|
|
OptionalHeader.MinorLinkerVersion := UTILS.vMinor;
|
|
|
|
OptionalHeader.SizeOfCode := WR.align(Size.Code, FileAlignment);
|
|
|
|
OptionalHeader.SizeOfInitializedData := 0;
|
|
|
|
OptionalHeader.SizeOfUninitializedData := 0;
|
|
|
|
OptionalHeader.AddressOfEntryPoint := SectionAlignment;
|
|
|
|
OptionalHeader.BaseOfCode := SectionAlignment;
|
|
|
|
OptionalHeader.BaseOfData := OptionalHeader.BaseOfCode + WR.align(Size.Code, SectionAlignment);
|
|
|
|
OptionalHeader.ImageBase := BaseAddress;
|
|
|
|
OptionalHeader.SectionAlignment := SectionAlignment;
|
|
|
|
OptionalHeader.FileAlignment := FileAlignment;
|
|
|
|
OptionalHeader.MajorOperatingSystemVersion := 1X;
|
|
|
|
OptionalHeader.MinorOperatingSystemVersion := 0X;
|
|
|
|
OptionalHeader.MajorImageVersion := 0X;
|
|
|
|
OptionalHeader.MinorImageVersion := 0X;
|
|
|
|
OptionalHeader.MajorSubsystemVersion := 4X;
|
|
|
|
OptionalHeader.MinorSubsystemVersion := 0X;
|
|
|
|
OptionalHeader.Win32VersionValue := 0H;
|
|
|
|
OptionalHeader.SizeOfImage := SectionAlignment;
|
|
|
|
OptionalHeader.SizeOfHeaders := 400H;
|
|
|
|
OptionalHeader.CheckSum := 0;
|
|
|
|
OptionalHeader.Subsystem := WCHR((2 + ORD(console)) * ORD(~dll));
|
|
|
|
OptionalHeader.DllCharacteristics := 0040X;
|
|
|
|
OptionalHeader.SizeOfStackReserve := 100000H;
|
|
|
|
OptionalHeader.SizeOfStackCommit := 10000H;
|
|
|
|
OptionalHeader.SizeOfHeapReserve := 100000H;
|
|
|
|
OptionalHeader.SizeOfHeapCommit := 10000H;
|
|
|
|
OptionalHeader.LoaderFlags := 0;
|
|
|
|
OptionalHeader.NumberOfRvaAndSizes := IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
|
|
|
|
|
|
|
|
FOR i := 0 TO IMAGE_NUMBEROF_DIRECTORY_ENTRIES - 1 DO
|
|
|
|
OptionalHeader.DataDirectory[i].VirtualAddress := 0;
|
|
|
|
OptionalHeader.DataDirectory[i].Size := 0
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
InitSection(SectionHeaders[0], ".text", Size.Code, SHC_text);
|
2020-05-25 23:48:33 +03:00
|
|
|
SectionHeaders[0].VirtualAddress := SectionAlignment;
|
2020-10-13 10:58:51 +03:00
|
|
|
SectionHeaders[0].PointerToRawData := OptionalHeader.SizeOfHeaders;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
InitSection(SectionHeaders[1], ".data", Size.Data, SHC_data);
|
|
|
|
SectionHeaders[1].VirtualAddress := WR.align(SectionHeaders[0].VirtualAddress + SectionHeaders[0].VirtualSize, SectionAlignment);
|
2020-05-25 23:48:33 +03:00
|
|
|
SectionHeaders[1].PointerToRawData := SectionHeaders[0].PointerToRawData + SectionHeaders[0].SizeOfRawData;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
InitSection(SectionHeaders[2], ".bss", Size.Bss, SHC_bss);
|
|
|
|
SectionHeaders[2].VirtualAddress := WR.align(SectionHeaders[1].VirtualAddress + SectionHeaders[1].VirtualSize, SectionAlignment);
|
2020-05-25 23:48:33 +03:00
|
|
|
SectionHeaders[2].PointerToRawData := SectionHeaders[1].PointerToRawData + SectionHeaders[1].SizeOfRawData;
|
2020-10-13 10:58:51 +03:00
|
|
|
SectionHeaders[2].SizeOfRawData := 0;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
Size.Import := GetImportSize(program.imp_list);
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
InitSection(SectionHeaders[3], ".idata", Size.Import + CHL.Length(program._import), SHC_data);
|
|
|
|
SectionHeaders[3].VirtualAddress := WR.align(SectionHeaders[2].VirtualAddress + SectionHeaders[2].VirtualSize, SectionAlignment);
|
2020-05-25 23:48:33 +03:00
|
|
|
SectionHeaders[3].PointerToRawData := SectionHeaders[2].PointerToRawData + SectionHeaders[2].SizeOfRawData;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Address.Code := SectionHeaders[0].VirtualAddress + OptionalHeader.ImageBase;
|
|
|
|
Address.Data := SectionHeaders[1].VirtualAddress + OptionalHeader.ImageBase;
|
|
|
|
Address.Bss := SectionHeaders[2].VirtualAddress + OptionalHeader.ImageBase;
|
|
|
|
Address.Import := SectionHeaders[3].VirtualAddress + OptionalHeader.ImageBase;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
fixup(program, Address, amd64);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
IF dll THEN
|
2020-10-13 10:58:51 +03:00
|
|
|
Size.Export := Export(program, SectionHeaders[1].VirtualAddress + program.modname, ExportDir);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
InitSection(SectionHeaders[4], ".edata", Size.Export + CHL.Length(program.export), SHC_data);
|
|
|
|
SectionHeaders[4].VirtualAddress := WR.align(SectionHeaders[3].VirtualAddress + SectionHeaders[3].VirtualSize, SectionAlignment);
|
2020-05-25 23:48:33 +03:00
|
|
|
SectionHeaders[4].PointerToRawData := SectionHeaders[3].PointerToRawData + SectionHeaders[3].SizeOfRawData;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
OptionalHeader.DataDirectory[0].VirtualAddress := SectionHeaders[4].VirtualAddress;
|
|
|
|
OptionalHeader.DataDirectory[0].Size := SectionHeaders[4].VirtualSize
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
OptionalHeader.DataDirectory[1].VirtualAddress := SectionHeaders[3].VirtualAddress;
|
|
|
|
OptionalHeader.DataDirectory[1].Size := SectionHeaders[3].VirtualSize;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
FOR i := 1 TO ORD(FileHeader.NumberOfSections) - 1 DO
|
|
|
|
INC(OptionalHeader.SizeOfInitializedData, SectionHeaders[i].SizeOfRawData)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
OptionalHeader.SizeOfUninitializedData := WR.align(SectionHeaders[2].VirtualSize, FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
FOR i := 0 TO ORD(FileHeader.NumberOfSections) - 1 DO
|
|
|
|
INC(OptionalHeader.SizeOfImage, WR.align(SectionHeaders[i].VirtualSize, SectionAlignment))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
n := 0;
|
|
|
|
BIN.InitArray(msdos, n, "4D5A80000100000004001000FFFF000040010000000000004000000000000000");
|
|
|
|
BIN.InitArray(msdos, n, "0000000000000000000000000000000000000000000000000000000080000000");
|
|
|
|
BIN.InitArray(msdos, n, "0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F");
|
|
|
|
BIN.InitArray(msdos, n, "742062652072756E20696E20444F53206D6F64652E0D0A240000000000000000");
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Create(FileName);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write(msdos, LEN(msdos));
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write(Signature, LEN(Signature));
|
|
|
|
WriteFileHeader(FileHeader);
|
|
|
|
WriteOptHeader(OptionalHeader, amd64);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
FOR i := 0 TO ORD(FileHeader.NumberOfSections) - 1 DO
|
|
|
|
WriteSectionHeader(SectionHeaders[i])
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Padding(FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(program.code);
|
|
|
|
WR.Padding(FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(program.data);
|
|
|
|
WR.Padding(FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
n := (libcnt + 1) * 5;
|
|
|
|
ImportTable := CHL.CreateIntList();
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
FOR i := 0 TO (Size.Import - n * SIZE_OF_DWORD) DIV SizeOfWord + n - 1 DO
|
2019-03-11 11:59:55 +03:00
|
|
|
CHL.PushInt(ImportTable, 0)
|
|
|
|
END;
|
|
|
|
|
|
|
|
i := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
_import := program.imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE _import # NIL DO
|
|
|
|
IF _import.label = 0 THEN
|
|
|
|
CHL.SetInt(ImportTable, i + 0, _import.OriginalFirstThunk * SizeOfWord + SectionHeaders[3].VirtualAddress + n * SIZE_OF_DWORD);
|
2019-03-11 11:59:55 +03:00
|
|
|
CHL.SetInt(ImportTable, i + 1, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 2, 0);
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.SetInt(ImportTable, i + 3, _import.nameoffs + Size.Import + SectionHeaders[3].VirtualAddress);
|
|
|
|
CHL.SetInt(ImportTable, i + 4, _import.FirstThunk * SizeOfWord + SectionHeaders[3].VirtualAddress + n * SIZE_OF_DWORD);
|
|
|
|
INC(i, 5)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
_import := _import.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
CHL.SetInt(ImportTable, i + 0, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 1, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 2, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 3, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 4, 0);
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
_import := program.imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE _import # NIL DO
|
|
|
|
IF _import.label # 0 THEN
|
|
|
|
temp := _import.nameoffs + Size.Import + SectionHeaders[3].VirtualAddress - 2;
|
|
|
|
CHL.SetInt(ImportTable, _import.OriginalFirstThunk + n, temp);
|
|
|
|
CHL.SetInt(ImportTable, _import.FirstThunk + n, temp)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
_import := _import.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
FOR i := 0 TO n - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(CHL.GetInt(ImportTable, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
FOR i := n TO CHL.Length(ImportTable) - 1 DO
|
|
|
|
IF amd64 THEN
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write64LE(CHL.GetInt(ImportTable, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(CHL.GetInt(ImportTable, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(program._import);
|
|
|
|
WR.Padding(FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
IF dll THEN
|
|
|
|
|
|
|
|
INC(ExportDir.AddressOfFunctions, SectionHeaders[4].VirtualAddress);
|
|
|
|
INC(ExportDir.AddressOfNames, SectionHeaders[4].VirtualAddress);
|
|
|
|
INC(ExportDir.AddressOfNameOrdinals, SectionHeaders[4].VirtualAddress);
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteExportDir(ExportDir);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
export := program.exp_list.first(BIN.EXPRT);
|
|
|
|
WHILE export # NIL DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(export.label + SectionHeaders[0].VirtualAddress);
|
2019-03-11 11:59:55 +03:00
|
|
|
export := export.next(BIN.EXPRT)
|
|
|
|
END;
|
|
|
|
|
|
|
|
export := program.exp_list.first(BIN.EXPRT);
|
|
|
|
WHILE export # NIL DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(export.nameoffs + Size.Export + SectionHeaders[4].VirtualAddress);
|
2019-03-11 11:59:55 +03:00
|
|
|
export := export.next(BIN.EXPRT)
|
|
|
|
END;
|
|
|
|
|
|
|
|
FOR i := 0 TO ExportDir.NumberOfFunctions - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteWord(WCHR(i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(program.export);
|
|
|
|
WR.Padding(FileAlignment)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Close
|
2019-03-11 11:59:55 +03:00
|
|
|
END write;
|
|
|
|
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
END PE32.
|