2020-05-25 22:48:33 +02:00
|
|
|
(*
|
|
|
|
BSD 2-Clause License
|
|
|
|
|
|
|
|
Copyright (c) 2019-2020, Anton Krotov
|
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE API;
|
|
|
|
|
|
|
|
IMPORT SYSTEM;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
eol* = 0AX;
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
BIT_DEPTH* = 64;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
RTLD_LAZY = 1;
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
SOFINI = PROCEDURE;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
MainParam*, libc*: INTEGER;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
dlopen* : PROCEDURE [linux] (filename, flag: INTEGER): INTEGER;
|
|
|
|
dlsym* : PROCEDURE [linux] (handle, symbol: INTEGER): INTEGER;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
exit*,
|
|
|
|
exit_thread* : PROCEDURE [linux] (code: INTEGER);
|
|
|
|
puts : PROCEDURE [linux] (pStr: INTEGER);
|
|
|
|
malloc : PROCEDURE [linux] (size: INTEGER): INTEGER;
|
|
|
|
free : PROCEDURE [linux] (ptr: INTEGER);
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
fini: SOFINI;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE DebugMsg* (lpText, lpCaption: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
puts(lpCaption);
|
|
|
|
puts(lpText)
|
|
|
|
END DebugMsg;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE _NEW* (size: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
res, ptr, words: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
res := malloc(size);
|
|
|
|
IF res # 0 THEN
|
|
|
|
ptr := res;
|
|
|
|
words := size DIV SYSTEM.SIZE(INTEGER);
|
|
|
|
WHILE words > 0 DO
|
|
|
|
SYSTEM.PUT(ptr, 0);
|
|
|
|
INC(ptr, SYSTEM.SIZE(INTEGER));
|
|
|
|
DEC(words)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END _NEW;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE _DISPOSE* (p: INTEGER): INTEGER;
|
|
|
|
BEGIN
|
|
|
|
free(p)
|
|
|
|
RETURN 0
|
|
|
|
END _DISPOSE;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE GetSym (lib: INTEGER; name: ARRAY OF CHAR; VarAdr: INTEGER);
|
2020-05-25 22:48:33 +02:00
|
|
|
VAR
|
|
|
|
sym: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
sym := dlsym(lib, SYSTEM.ADR(name[0]));
|
|
|
|
ASSERT(sym # 0);
|
|
|
|
SYSTEM.PUT(VarAdr, sym)
|
2020-10-13 09:58:51 +02:00
|
|
|
END GetSym;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE init* (sp, code: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
fini := NIL;
|
|
|
|
SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER) * 2, dlopen);
|
|
|
|
SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER), dlsym);
|
|
|
|
MainParam := sp;
|
|
|
|
|
|
|
|
libc := dlopen(SYSTEM.SADR("libc.so.6"), RTLD_LAZY);
|
2020-10-13 09:58:51 +02:00
|
|
|
GetSym(libc, "exit", SYSTEM.ADR(exit_thread));
|
|
|
|
exit := exit_thread;
|
|
|
|
GetSym(libc, "puts", SYSTEM.ADR(puts));
|
|
|
|
GetSym(libc, "malloc", SYSTEM.ADR(malloc));
|
|
|
|
GetSym(libc, "free", SYSTEM.ADR(free));
|
2020-05-25 22:48:33 +02:00
|
|
|
END init;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE dllentry* (hinstDLL, fdwReason, lpvReserved: INTEGER): INTEGER;
|
|
|
|
RETURN 0
|
|
|
|
END dllentry;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE sofinit*;
|
|
|
|
BEGIN
|
|
|
|
IF fini # NIL THEN
|
|
|
|
fini
|
|
|
|
END
|
|
|
|
END sofinit;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetFini* (ProcFini: SOFINI);
|
|
|
|
BEGIN
|
|
|
|
fini := ProcFini
|
|
|
|
END SetFini;
|
|
|
|
|
|
|
|
|
|
|
|
END API.
|