2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
Copyright (c) 2018-2020, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE MSCOFF;
|
|
|
|
|
|
|
|
IMPORT BIN, PE32, KOS, WR := WRITER, UTILS, ERRORS, LISTS, CHL := CHUNKLISTS;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
SIZE_OF_DWORD = 4;
|
|
|
|
|
|
|
|
(* SectionHeader.Characteristics *)
|
|
|
|
|
|
|
|
SHC_flat = 040500020H;
|
|
|
|
SHC_data = 0C0500040H;
|
|
|
|
SHC_bss = 0C03000C0H;
|
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
FH = PE32.IMAGE_FILE_HEADER;
|
|
|
|
|
|
|
|
SH = PE32.IMAGE_SECTION_HEADER;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE WriteReloc (VirtualAddress, SymbolTableIndex, Type: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(VirtualAddress);
|
|
|
|
WR.Write32LE(SymbolTableIndex);
|
|
|
|
WR.Write16LE(Type)
|
2019-03-11 09:59:55 +01:00
|
|
|
END WriteReloc;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Reloc (program: BIN.PROGRAM);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
2020-10-13 09:58:51 +02:00
|
|
|
reloc: BIN.RELOC;
|
|
|
|
offset: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
reloc := program.rel_list.first(BIN.RELOC);
|
|
|
|
WHILE reloc # NIL DO
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
offset := reloc.offset;
|
2019-03-11 09:59:55 +01:00
|
|
|
CASE reloc.opcode OF
|
2020-05-25 22:48:33 +02:00
|
|
|
|BIN.RIMP,
|
2020-10-13 09:58:51 +02:00
|
|
|
BIN.IMPTAB: WriteReloc(offset, 4, 6)
|
|
|
|
|BIN.RBSS: WriteReloc(offset, 5, 6)
|
|
|
|
|BIN.RDATA: WriteReloc(offset, 2, 6)
|
|
|
|
|BIN.RCODE: WriteReloc(offset, 1, 6)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
reloc := reloc.next(BIN.RELOC)
|
|
|
|
END;
|
|
|
|
END Reloc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE RelocCount (program: BIN.PROGRAM): INTEGER;
|
|
|
|
VAR
|
|
|
|
reloc: BIN.RELOC;
|
|
|
|
iproc: BIN.IMPRT;
|
|
|
|
res, L: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
offset: INTEGER;
|
2020-05-25 22:48:33 +02:00
|
|
|
code: CHL.BYTELIST;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
res := 0;
|
2020-05-25 22:48:33 +02:00
|
|
|
code := program.code;
|
2019-03-11 09:59:55 +01:00
|
|
|
reloc := program.rel_list.first(BIN.RELOC);
|
|
|
|
WHILE reloc # NIL DO
|
|
|
|
|
|
|
|
INC(res);
|
2020-10-13 09:58:51 +02:00
|
|
|
offset := reloc.offset;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF reloc.opcode = BIN.RIMP THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
L := BIN.get32le(code, offset);
|
2019-03-11 09:59:55 +01:00
|
|
|
iproc := BIN.GetIProc(program, L);
|
2020-10-13 09:58:51 +02:00
|
|
|
BIN.put32le(code, offset, iproc.label)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
IF reloc.opcode = BIN.RCODE THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
L := BIN.get32le(code, offset);
|
|
|
|
BIN.put32le(code, offset, BIN.GetLabel(program, L))
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
reloc := reloc.next(BIN.RELOC)
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END RelocCount;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE write* (program: BIN.PROGRAM; FileName: ARRAY OF CHAR; ver: INTEGER);
|
|
|
|
VAR
|
|
|
|
exp: BIN.EXPRT;
|
|
|
|
n, i: INTEGER;
|
|
|
|
|
|
|
|
szversion: PE32.NAME;
|
|
|
|
|
|
|
|
ImportTable: CHL.INTLIST;
|
|
|
|
ILen, LibCount, isize: INTEGER;
|
|
|
|
|
|
|
|
ExpCount: INTEGER;
|
|
|
|
|
|
|
|
icount, ecount, dcount, ccount: INTEGER;
|
|
|
|
|
|
|
|
FileHeader: FH;
|
|
|
|
|
|
|
|
flat, data, edata, idata, bss: SH;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ICount (ImportTable: CHL.INTLIST; ILen: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
i, res: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
res := 0;
|
|
|
|
|
|
|
|
FOR i := 0 TO ILen - 1 DO
|
|
|
|
IF CHL.GetInt(ImportTable, i) # 0 THEN
|
|
|
|
INC(res)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END ICount;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetNumberOfRelocations (VAR section: SH; NumberOfRelocations: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
IF NumberOfRelocations >= 65536 THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
ERRORS.Error(202)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
section.NumberOfRelocations := WCHR(NumberOfRelocations)
|
|
|
|
END SetNumberOfRelocations;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
szversion := "version";
|
|
|
|
|
|
|
|
ASSERT(LENGTH(szversion) = 7);
|
|
|
|
|
|
|
|
KOS.Import(program, 0, ImportTable, ILen, LibCount, isize);
|
|
|
|
ExpCount := LISTS.count(program.exp_list);
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
icount := CHL.Length(program._import);
|
2019-03-11 09:59:55 +01:00
|
|
|
dcount := CHL.Length(program.data);
|
|
|
|
ccount := CHL.Length(program.code);
|
|
|
|
ecount := CHL.Length(program.export);
|
|
|
|
|
|
|
|
FileHeader.Machine := 014CX;
|
|
|
|
FileHeader.NumberOfSections := 5X;
|
|
|
|
FileHeader.TimeDateStamp := UTILS.UnixTime();
|
2020-05-25 22:48:33 +02:00
|
|
|
(* FileHeader.PointerToSymbolTable := 0; *)
|
2019-03-11 09:59:55 +01:00
|
|
|
FileHeader.NumberOfSymbols := 6;
|
|
|
|
FileHeader.SizeOfOptionalHeader := 0X;
|
|
|
|
FileHeader.Characteristics := 0184X;
|
|
|
|
|
|
|
|
flat.Name := ".flat";
|
|
|
|
flat.VirtualSize := 0;
|
|
|
|
flat.VirtualAddress := 0;
|
|
|
|
flat.SizeOfRawData := ccount;
|
|
|
|
flat.PointerToRawData := ORD(FileHeader.NumberOfSections) * PE32.SIZE_OF_IMAGE_SECTION_HEADER + PE32.SIZE_OF_IMAGE_FILE_HEADER;
|
2020-05-25 22:48:33 +02:00
|
|
|
(* flat.PointerToRelocations := 0; *)
|
2019-03-11 09:59:55 +01:00
|
|
|
flat.PointerToLinenumbers := 0;
|
|
|
|
SetNumberOfRelocations(flat, RelocCount(program));
|
|
|
|
flat.NumberOfLinenumbers := 0X;
|
|
|
|
flat.Characteristics := SHC_flat;
|
|
|
|
|
|
|
|
data.Name := ".data";
|
|
|
|
data.VirtualSize := 0;
|
|
|
|
data.VirtualAddress := 0;
|
|
|
|
data.SizeOfRawData := dcount;
|
|
|
|
data.PointerToRawData := flat.PointerToRawData + flat.SizeOfRawData;
|
|
|
|
data.PointerToRelocations := 0;
|
|
|
|
data.PointerToLinenumbers := 0;
|
|
|
|
data.NumberOfRelocations := 0X;
|
|
|
|
data.NumberOfLinenumbers := 0X;
|
|
|
|
data.Characteristics := SHC_data;
|
|
|
|
|
|
|
|
edata.Name := ".edata";
|
|
|
|
edata.VirtualSize := 0;
|
|
|
|
edata.VirtualAddress := 0;
|
|
|
|
edata.SizeOfRawData := ((ExpCount + 1) * 2 + 1) * SIZE_OF_DWORD + LENGTH(szversion) + 1 + ecount;
|
|
|
|
edata.PointerToRawData := data.PointerToRawData + data.SizeOfRawData;
|
2020-05-25 22:48:33 +02:00
|
|
|
(* edata.PointerToRelocations := 0; *)
|
2019-03-11 09:59:55 +01:00
|
|
|
edata.PointerToLinenumbers := 0;
|
|
|
|
SetNumberOfRelocations(edata, ExpCount * 2 + 1);
|
|
|
|
edata.NumberOfLinenumbers := 0X;
|
|
|
|
edata.Characteristics := SHC_data;
|
|
|
|
|
|
|
|
idata.Name := ".idata";
|
|
|
|
idata.VirtualSize := 0;
|
|
|
|
idata.VirtualAddress := 0;
|
|
|
|
idata.SizeOfRawData := isize;
|
|
|
|
idata.PointerToRawData := edata.PointerToRawData + edata.SizeOfRawData;
|
2020-05-25 22:48:33 +02:00
|
|
|
(* idata.PointerToRelocations := 0; *)
|
2019-03-11 09:59:55 +01:00
|
|
|
idata.PointerToLinenumbers := 0;
|
|
|
|
SetNumberOfRelocations(idata, ICount(ImportTable, ILen));
|
|
|
|
idata.NumberOfLinenumbers := 0X;
|
|
|
|
idata.Characteristics := SHC_data;
|
|
|
|
|
|
|
|
bss.Name := ".bss";
|
|
|
|
bss.VirtualSize := 0;
|
|
|
|
bss.VirtualAddress := 0;
|
|
|
|
bss.SizeOfRawData := program.bss;
|
|
|
|
bss.PointerToRawData := 0;
|
|
|
|
bss.PointerToRelocations := 0;
|
|
|
|
bss.PointerToLinenumbers := 0;
|
|
|
|
bss.NumberOfRelocations := 0X;
|
|
|
|
bss.NumberOfLinenumbers := 0X;
|
|
|
|
bss.Characteristics := SHC_bss;
|
|
|
|
|
|
|
|
flat.PointerToRelocations := idata.PointerToRawData + idata.SizeOfRawData;
|
|
|
|
edata.PointerToRelocations := flat.PointerToRelocations + ORD(flat.NumberOfRelocations) * 10;
|
|
|
|
idata.PointerToRelocations := edata.PointerToRelocations + ORD(edata.NumberOfRelocations) * 10;
|
|
|
|
|
|
|
|
FileHeader.PointerToSymbolTable := idata.PointerToRelocations + ORD(idata.NumberOfRelocations) * 10;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Create(FileName);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteFileHeader(FileHeader);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteSectionHeader(flat);
|
|
|
|
PE32.WriteSectionHeader(data);
|
|
|
|
PE32.WriteSectionHeader(edata);
|
|
|
|
PE32.WriteSectionHeader(idata);
|
|
|
|
PE32.WriteSectionHeader(bss);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
CHL.WriteToFile(program.code);
|
|
|
|
CHL.WriteToFile(program.data);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
exp := program.exp_list.first(BIN.EXPRT);
|
|
|
|
WHILE exp # NIL DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(exp.nameoffs + edata.SizeOfRawData - ecount);
|
|
|
|
WR.Write32LE(exp.label);
|
2019-03-11 09:59:55 +01:00
|
|
|
exp := exp.next(BIN.EXPRT)
|
|
|
|
END;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(((ExpCount + 1) * 2 + 1) * SIZE_OF_DWORD);
|
|
|
|
WR.Write32LE(ver);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(0);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(szversion);
|
|
|
|
CHL.WriteToFile(program.export);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
FOR i := 0 TO ILen - 1 DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(CHL.GetInt(ImportTable, i))
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
CHL.WriteToFile(program._import);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
Reloc(program);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
n := 0;
|
|
|
|
exp := program.exp_list.first(BIN.EXPRT);
|
|
|
|
WHILE exp # NIL DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteReloc(n, 3, 6);
|
2019-03-11 09:59:55 +01:00
|
|
|
INC(n, 4);
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteReloc(n, 1, 6);
|
2019-03-11 09:59:55 +01:00
|
|
|
INC(n, 4);
|
|
|
|
|
|
|
|
exp := exp.next(BIN.EXPRT)
|
|
|
|
END;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteReloc(n, 3, 6);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
FOR i := 0 TO LibCount * 2 - 1 DO
|
|
|
|
WriteReloc(i * SIZE_OF_DWORD, 4, 6)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
FOR i := LibCount * 2 TO ILen - 1 DO
|
|
|
|
IF CHL.GetInt(ImportTable, i) # 0 THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteReloc(i * SIZE_OF_DWORD, 4, 6)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName("EXPORTS");
|
|
|
|
WriteReloc(0, 3, 2);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(".flat");
|
|
|
|
WriteReloc(0, 1, 3);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(".data");
|
|
|
|
WriteReloc(0, 2, 3);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(".edata");
|
|
|
|
WriteReloc(0, 3, 3);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(".idata");
|
|
|
|
WriteReloc(0, 4, 3);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PE32.WriteName(".bss");
|
|
|
|
WriteReloc(0, 5, 3);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Write32LE(4);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
WR.Close
|
2019-03-11 09:59:55 +01:00
|
|
|
END write;
|
|
|
|
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
END MSCOFF.
|