2020-05-25 23:48:33 +03:00
|
|
|
(*
|
2019-03-11 11:59:55 +03:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
Copyright (c) 2019-2020, Anton Krotov
|
2019-03-11 11:59:55 +03:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE LINAPI;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
IMPORT SYSTEM, API, Libdl;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
TP* = ARRAY 2 OF INTEGER;
|
|
|
|
SOFINI* = PROCEDURE;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
libc*, librt*: INTEGER;
|
|
|
|
|
|
|
|
stdout*,
|
|
|
|
stdin*,
|
|
|
|
stderr* : INTEGER;
|
|
|
|
|
|
|
|
malloc* : PROCEDURE [linux] (size: INTEGER): INTEGER;
|
|
|
|
free* : PROCEDURE [linux] (ptr: INTEGER);
|
|
|
|
exit* : PROCEDURE [linux] (code: INTEGER);
|
|
|
|
puts* : PROCEDURE [linux] (pStr: INTEGER);
|
|
|
|
fwrite*,
|
|
|
|
fread* : PROCEDURE [linux] (buffer, bytes, blocks, file: INTEGER): INTEGER;
|
|
|
|
fopen* : PROCEDURE [linux] (fname, fmode: INTEGER): INTEGER;
|
|
|
|
fclose* : PROCEDURE [linux] (file: INTEGER): INTEGER;
|
|
|
|
time* : PROCEDURE [linux] (ptr: INTEGER): INTEGER;
|
|
|
|
|
|
|
|
clock_gettime* : PROCEDURE [linux] (clock_id: INTEGER; VAR tp: TP): INTEGER;
|
|
|
|
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
PROCEDURE SetFini* (ProcFini: SOFINI);
|
|
|
|
BEGIN
|
|
|
|
API.SetFini(ProcFini)
|
|
|
|
END SetFini;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE GetSym (lib: INTEGER; name: ARRAY OF CHAR; VarAdr: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
2020-10-13 10:58:51 +03:00
|
|
|
sym: INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
sym := Libdl.sym(lib, name);
|
|
|
|
ASSERT(sym # 0);
|
|
|
|
SYSTEM.PUT(VarAdr, sym)
|
|
|
|
END GetSym;
|
|
|
|
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE init;
|
|
|
|
BEGIN
|
2019-03-11 11:59:55 +03:00
|
|
|
libc := API.libc;
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
GetSym(libc, "exit", SYSTEM.ADR(exit));
|
|
|
|
GetSym(libc, "puts", SYSTEM.ADR(puts));
|
|
|
|
GetSym(libc, "malloc", SYSTEM.ADR(malloc));
|
|
|
|
GetSym(libc, "free", SYSTEM.ADR(free));
|
|
|
|
GetSym(libc, "fread", SYSTEM.ADR(fread));
|
|
|
|
GetSym(libc, "fwrite", SYSTEM.ADR(fwrite));
|
|
|
|
GetSym(libc, "fopen", SYSTEM.ADR(fopen));
|
|
|
|
GetSym(libc, "fclose", SYSTEM.ADR(fclose));
|
|
|
|
GetSym(libc, "time", SYSTEM.ADR(time));
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
GetSym(libc, "stdout", SYSTEM.ADR(stdout)); SYSTEM.GET(stdout - SYSTEM.SIZE(INTEGER), stdout);
|
|
|
|
GetSym(libc, "stdin", SYSTEM.ADR(stdin)); SYSTEM.GET(stdin - SYSTEM.SIZE(INTEGER), stdin);
|
|
|
|
GetSym(libc, "stderr", SYSTEM.ADR(stderr)); SYSTEM.GET(stderr - SYSTEM.SIZE(INTEGER), stderr);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
librt := Libdl.open("librt.so.1", Libdl.LAZY);
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
GetSym(librt, "clock_gettime", SYSTEM.ADR(clock_gettime))
|
2019-03-11 11:59:55 +03:00
|
|
|
END init;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE [stdcall-] syscall* (eax, ebx, ecx, edx, esi, edi: INTEGER): INTEGER;
|
|
|
|
BEGIN
|
|
|
|
SYSTEM.CODE(
|
|
|
|
053H, (* push ebx *)
|
|
|
|
056H, (* push esi *)
|
|
|
|
057H, (* push edi *)
|
|
|
|
08BH, 045H, 008H, (* mov eax, dword [ebp + 8] *)
|
|
|
|
08BH, 05DH, 00CH, (* mov ebx, dword [ebp + 12] *)
|
|
|
|
08BH, 04DH, 010H, (* mov ecx, dword [ebp + 16] *)
|
|
|
|
08BH, 055H, 014H, (* mov edx, dword [ebp + 20] *)
|
|
|
|
08BH, 075H, 018H, (* mov esi, dword [ebp + 24] *)
|
|
|
|
08BH, 07DH, 01CH, (* mov edi, dword [ebp + 28] *)
|
|
|
|
0CDH, 080H, (* int 128 *)
|
|
|
|
05FH, (* pop edi *)
|
|
|
|
05EH, (* pop esi *)
|
|
|
|
05BH, (* pop ebx *)
|
|
|
|
0C9H, (* leave *)
|
|
|
|
0C2H, 018H, 000H (* ret 24 *)
|
|
|
|
)
|
|
|
|
RETURN 0
|
|
|
|
END syscall;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
init
|
2020-05-25 23:48:33 +03:00
|
|
|
END LINAPI.
|