raz / src / Host.linux.raz

103 lines · 4.0 KiB · raw · history · last change 6d1c7219

1(* Host: what the compiler asks of the system it runs on, beyond the
2 library. The host is the system running raz; the target, the one its
3 program will run on. This file is Linux's.
4
5 Another host stages its own file in its place: copy src/ to a
6 directory, put that host's Host.raz there instead of this one, and
7 build the copy with the host's library (doc/compiler.md says how).
8 Raz and Exe import only this module for what follows, so a host
9 gives these five procedures and keeps every export:
10
11 libraryDir where the standard library is, as a directory path
12 cLibrary which C library a program that uses C is linked
13 against by default, or none if the host can't make one
14 writeExecutable the file at path made to hold an executable
15 prepareRun the path raz run writes its program to, or an error
16 if this host can't run it; asked first, before any
17 source is read, so its error is the refusal
18 execute replaces the compiler with the program written there
19
20 A host that can't run programs refuses in prepareRun and gives
21 execute any body; one that has no C gives cLibrary the value none.
22 Strings it returns must live until the compiler exits.
23
24 Depends on: Sys and Files for Linux's services, Scan for memory that
25 lasts until exit, Diag and Fmt for paths. Leaves out: compiling and
26 the executable format, which are Parse and Exe's. *)
27module Host;
28import Sys, Files, Fmt, Diag, Scan;
29import SYSTEM;
30
31type
32 CLib* = (none, glibc, musl); (* the C library to link against *)
33
34const
35 LoaderGlibc = "/lib64/ld-linux-x86-64.so.2";
36 LoaderMusl = "/lib/ld-musl-x86_64.so.1";
37 MemfdCreate = 319; (* the system calls raz run makes *)
38 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
39 Execve = 59;
40 Mode755 = 1EDH; (* rwxr-xr-x *)
41 CreateRun: ERROR = "can't create a memfd";
42
43(* The directory of the library, ending in what the name of raz leaves,
44 "lib": the one beside raz as the system says (a link to raz is
45 followed), or, with no /proc, beside argument 0 as it was typed *)
46proc libraryDir*: []const U8;
47var path: []const U8; error, i: INT;
48begin
49 path, error := Sys.readLink("/proc/self/exe");
50 if error # 0 then path := Files.arg(0); end;
51 i := LEN(path);
52 while (i > 0) and (path[i - 1] # '/') do DEC(i); end;
53 if i = 0 then return "lib"; end;
54 return Diag.say("{}lib", [Fmt.str(path[0..i])]);
55end;
56
57(* The C library a program that uses C is linked against when -libc
58 doesn't say: glibc, or musl only if the machine has musl's loader and
59 not glibc's, as Alpine has *)
60proc cLibrary*: CLib;
61begin
62 if (Sys.access(LoaderGlibc, {}) # 0) and (Sys.access(LoaderMusl, {}) = 0) then return musl; end;
63 return glibc;
64end;
65
66(* The file at path made to hold data, an executable: written, then
67 given mode 755. False if either failed. *)
68proc writeExecutable*(path, data: []const U8): BOOL;
69begin
70 return Files.write(path, data) and (Sys.chmod(path, Mode755) >= 0);
71end;
72
73(* s as a C string, in memory that is never freed *)
74proc cstr(s: []const U8): ^U8;
75var p: ^U8;
76begin
77 p := Scan.grab(LEN(s) + 1);
78 if LEN(s) > 0 then COPY(p[0..LEN(s)], s); end;
79 p[LEN(s)] := 0;
80 return p;
81end;
82
83(* The path raz run writes its program to: a memfd, a file that lives
84 only in memory. Its descriptor stays open until execute, whose
85 close-on-exec flag lets the kernel release it, or until the compiler
86 ends after an error. *)
87proc prepareRun*: ([]const U8, ERROR);
88var fd: INT;
89begin
90 fd := SYSTEM.SYSCALL(MemfdCreate, cstr("raz"), MfdCloexec);
91 if fd < 0 then return "", CreateRun; end;
92 return Diag.say("/proc/self/fd/{}", [Fmt.int(fd)]), nil;
93end;
94
95(* The program at path runs in the compiler's place, with the compiler's
96 arguments from number first on as its own, first as its argument 0,
97 and the environment. It returns only if that failed: minus the errno. *)
98proc execute*(path: []const U8; first: INT): INT;
99begin
100 return SYSTEM.SYSCALL(Execve, cstr(path), SYSTEM.ARGV() + first, SYSTEM.ARGV() + (Files.argCount() + 1));
101end;
102
103end Host.