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 KOS;
|
|
|
|
|
|
|
|
IMPORT BIN, WR := WRITER, LISTS, CHL := CHUNKLISTS;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
HEADER_SIZE = 36;
|
|
|
|
|
|
|
|
SIZE_OF_DWORD = 4;
|
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
HEADER = RECORD
|
|
|
|
|
|
|
|
menuet01: ARRAY 9 OF CHAR;
|
|
|
|
ver, start, size, mem, sp, param, path: INTEGER
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Import* (program: BIN.PROGRAM; idata: INTEGER; VAR ImportTable: CHL.INTLIST; VAR len, libcount, size: INTEGER);
|
|
|
|
VAR
|
2020-10-13 10:58:51 +03:00
|
|
|
i: INTEGER;
|
|
|
|
imp: BIN.IMPRT;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
libcount := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := program.imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE imp # NIL DO
|
|
|
|
IF imp.label = 0 THEN
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(libcount)
|
|
|
|
END;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
|
|
|
len := libcount * 2 + 2;
|
|
|
|
size := (LISTS.count(program.imp_list) + len + 1) * SIZE_OF_DWORD;
|
|
|
|
|
|
|
|
ImportTable := CHL.CreateIntList();
|
|
|
|
FOR i := 0 TO size DIV SIZE_OF_DWORD - 1 DO
|
|
|
|
CHL.PushInt(ImportTable, 0)
|
|
|
|
END;
|
|
|
|
|
|
|
|
i := 0;
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := program.imp_list.first(BIN.IMPRT);
|
|
|
|
WHILE imp # NIL DO
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
IF imp.label = 0 THEN
|
2019-03-11 11:59:55 +03:00
|
|
|
CHL.SetInt(ImportTable, len, 0);
|
|
|
|
INC(len);
|
|
|
|
CHL.SetInt(ImportTable, i, idata + len * SIZE_OF_DWORD);
|
|
|
|
INC(i);
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.SetInt(ImportTable, i, imp.nameoffs + size + idata);
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(i)
|
|
|
|
ELSE
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.SetInt(ImportTable, len, imp.nameoffs + size + idata);
|
|
|
|
imp.label := len * SIZE_OF_DWORD;
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(len)
|
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
imp := imp.next(BIN.IMPRT)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
CHL.SetInt(ImportTable, len, 0);
|
|
|
|
CHL.SetInt(ImportTable, i, 0);
|
|
|
|
CHL.SetInt(ImportTable, i + 1, 0);
|
|
|
|
INC(len);
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(size, CHL.Length(program._import))
|
2019-03-11 11:59:55 +03:00
|
|
|
END Import;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE write* (program: BIN.PROGRAM; FileName: ARRAY OF CHAR);
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
PARAM_SIZE = 2048;
|
|
|
|
FileAlignment = 16;
|
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
header: HEADER;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
base, text, data, idata, bss, offset: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
reloc: BIN.RELOC;
|
|
|
|
iproc: BIN.IMPRT;
|
|
|
|
L: INTEGER;
|
|
|
|
delta: INTEGER;
|
|
|
|
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
ImportTable: CHL.INTLIST;
|
|
|
|
ILen, libcount, isize: INTEGER;
|
|
|
|
|
|
|
|
icount, dcount, ccount: INTEGER;
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
code: CHL.BYTELIST;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
base := 0;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
icount := CHL.Length(program._import);
|
2019-03-11 11:59:55 +03:00
|
|
|
dcount := CHL.Length(program.data);
|
|
|
|
ccount := CHL.Length(program.code);
|
|
|
|
|
|
|
|
text := base + HEADER_SIZE;
|
2020-10-13 10:58:51 +03:00
|
|
|
data := WR.align(text + ccount, FileAlignment);
|
|
|
|
idata := WR.align(data + dcount, FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
Import(program, idata, ImportTable, ILen, libcount, isize);
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
bss := WR.align(idata + isize, FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
header.menuet01 := "MENUET01";
|
|
|
|
header.ver := 1;
|
|
|
|
header.start := text;
|
|
|
|
header.size := idata + isize - base;
|
2020-10-13 10:58:51 +03:00
|
|
|
header.mem := WR.align(header.size + program.stack + program.bss + PARAM_SIZE * 2 + 4096, FileAlignment);
|
2019-03-11 11:59:55 +03:00
|
|
|
header.sp := base + header.mem - PARAM_SIZE * 2;
|
|
|
|
header.param := header.sp;
|
|
|
|
header.path := header.param + PARAM_SIZE;
|
|
|
|
|
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);
|
|
|
|
WHILE reloc # NIL DO
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
offset := reloc.offset;
|
|
|
|
L := BIN.get32le(code, offset);
|
|
|
|
delta := 3 - offset - text;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
CASE reloc.opcode OF
|
|
|
|
|
|
|
|
|BIN.RIMP:
|
|
|
|
iproc := BIN.GetIProc(program, L);
|
2020-10-13 10:58:51 +03:00
|
|
|
delta := idata + iproc.label
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.RBSS:
|
2020-10-13 10:58:51 +03:00
|
|
|
delta := L + bss
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.RDATA:
|
2020-10-13 10:58:51 +03:00
|
|
|
delta := L + data
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.RCODE:
|
2020-10-13 10:58:51 +03:00
|
|
|
delta := BIN.GetLabel(program, L) + text
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICDATA:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, L + 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) + text)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICBSS:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, L + bss)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.PICIMP:
|
|
|
|
iproc := BIN.GetIProc(program, L);
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, idata + iproc.label)
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|BIN.IMPTAB:
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(delta, idata)
|
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;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Create(FileName);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
FOR i := 0 TO 7 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.WriteByte(ORD(header.menuet01[i]))
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(header.ver);
|
|
|
|
WR.Write32LE(header.start);
|
|
|
|
WR.Write32LE(header.size);
|
|
|
|
WR.Write32LE(header.mem);
|
|
|
|
WR.Write32LE(header.sp);
|
|
|
|
WR.Write32LE(header.param);
|
|
|
|
WR.Write32LE(header.path);
|
2019-09-26 23:23:06 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(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
|
|
|
|
|
|
|
FOR i := 0 TO ILen - 1 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WR.Write32LE(CHL.GetInt(ImportTable, i))
|
2019-09-26 23:23:06 +03:00
|
|
|
END;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
CHL.WriteToFile(program._import);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
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 KOS.
|