raz / commit 6a6bdf5c

commit
6a6bdf5cfef3a0213f21cd4f7ec422b9a99ce95e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
57aca694
The C library a choice: -libc glibc|musl, musl by default where only its loader is

Elf names glibc's loader and libc.so.6, or musl's and libc.so, which
musl's loader takes for itself (setMusl). Raz takes -libc, or else
chooses musl on a machine with musl's loader and not glibc's, as
Alpine has.

Sys over C, to run over musl: close_range through syscall, as musl
has no close_range; and inetAddr reads an address into a buffer of its
own first, since musl's inet_pton leaves what it read of an IPv4
address it refuses, which fell in the IPv6 one's flow bytes.

make check runs test/libc again over musl where its loader is
installed; make alpine (test/alpine.sh) compiles and runs it in an
Alpine container, where the default must be musl.

patch · log

11 files changed, +146 -31

Makefile (history) modified +5 -1

@@ -81,6 +81,10 @@
8181 # native threads under load: 40,000 spawned and joined, 200 alive at a
8282 # time, all waiting on one Mutex; then, with little memory, spawn must
8383 # fail with Thread.Failed. make check runs a small version.
84+# test/libc on Alpine, over musl, in docker
85+alpine: raz
86+ test/alpine.sh
87+
8488 stress: raz | build
8589 ./raz -o build/Stress lib/Thread.linux-x86_64.raz
8690 build/Stress 40000 200
@@ -181,4 +185,4 @@
181185 site: raz pdf
182186 python3 site/build.py
183187
184-.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress fmt loc man pdf dist site clean install uninstall
188+.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site clean install uninstall

README.md (history) modified +1 -1

@@ -83,7 +83,7 @@
8383
8484 ## Using it
8585
86- raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] Main.raz
86+ raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] Main.raz
8787 raz run [option]... Main.raz [arg]...
8888 raz -h
8989 raz --version

doc/compiler.md (history) modified +14 -5

@@ -22,8 +22,8 @@
2222 | `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 4,176 | 3,615 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
25-| `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
26-| `src/Raz.raz` | 305 | 238 | The command line |
25+| `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
26+| `src/Raz.raz` | 326 | 253 | The command line |
2727
2828 Every file opens with a comment that says what it is, what it depends
2929 on and what it leaves out. Read those first; they are the map.
@@ -1636,8 +1636,15 @@
16361636 section naming the libraries; and, for each C symbol the program uses,
16371637 a symbol and a `GLOB_DAT` relocation that has the loader write the
16381638 symbol's address into the quadword `Chunk.externChunk` made. Code calls C
1639-through that quadword. There is no PLT and no lazy binding.
1640-`sizeTables` and `putTables` lay these out in the room `Link.raz` left
1639+through that quadword. There is no PLT and no lazy binding. The
1640+loader and the first library are glibc's,
1641+`/lib64/ld-linux-x86-64.so.2` and `libc.so.6`, or musl's,
1642+`/lib/ld-musl-x86_64.so.1` and `libc.so`, which musl's loader, being
1643+the library itself, takes to mean itself (`Elf.setMusl`). `-libc`
1644+chooses; left out, `Raz.muslHere` chooses musl where the machine has
1645+its loader and not glibc's, as Alpine does. A symbol that isn't live
1646+gets no relocation, so a program runs over a C library that lacks a
1647+procedure it declares but never reaches. `sizeTables` and `putTables` lay these out in the room `Link.raz` left
16411648 at the end of rodata.
16421649
16431650 Line numbers are DWARF 4: the line table `makeLines` made from the
@@ -1682,7 +1689,9 @@
16821689 - **The target.** x86-64 Linux, `linux-x86_64`: `INT` is 64 bits, and
16831690 there are reals. Every module of a program is compiled together, in
16841691 one run, into a static ELF executable, or a dynamic one if the
1685- program uses C; no object files.
1692+ program uses C; no object files. The C library is glibc or musl,
1693+ as `-libc` says, or by default glibc, and musl where only its
1694+ loader is there.
16861695 - **Finding modules.** `raz Main.raz` looks for `X`, imported, in
16871696 `Main.raz`'s directory, then in each `-I dir` in the order given
16881697 (16 at most), then in `lib` beside the `raz` executable, following

doc/raz.1 (history) modified +10 -0

@@ -103,6 +103,16 @@
103103 Leave out the profiler, which
104104 .Ev RAZ_PROFILE
105105 turns on, but keep the rest.
106+.It Fl libc Ar name
107+Link a program that uses C against
108+.Ar name ,
109+.Cm glibc
110+or
111+.Cm musl .
112+Left out, it is glibc, or musl on a machine that has musl's loader,
113+.Pa /lib/ld-musl-x86_64.so.1 ,
114+and not glibc's, as Alpine has.
115+A program without C needs no C library, and runs on either.
106116 .It Fl h , Fl \-help
107117 Say the options.
108118 .It Fl \-version

lib/C.raz (history) modified +1 -0

@@ -76,6 +76,7 @@
7676 extern proc dup2*(fd, fd2: I32): I32;
7777 extern proc dup*(fd: I32): I32;
7878 extern proc close_range*(first, last: U32; flags: I32): I32;
79+extern proc syscall*(n: INT; ...): INT; (* a system call by its number, on Linux *)
7980 extern proc execve*(path: ^U8; argv, envp: ^^U8): I32;
8081 extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32;
8182 extern proc kill*(pid, signal: I32): I32;

lib/Sys.raz (history) modified +20 -7

@@ -7,7 +7,8 @@
77 only the two struct timeval that begin struct rusage are read. What
88 is glibc's, on x86-64: where a name lies in struct dirent; struct
99 stat; open's flags; the numbers of the requests TIOCGWINSZ,
10- TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range;
10+ TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range's
11+ system call;
1112 SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs
1213 start with a byte of length; the numbers of the signals and of the
1314 limits, and RLIM_INFINITY, all ones. A C library that differs needs
@@ -772,13 +773,24 @@
772773 of its struct sockaddr_in or sockaddr_in6, or 0 if either is
773774 wrong. *)
774775 proc inetAddr(addr: []U8; port: INT; a: ^Sockaddr): INT;
775-var s: [64]U8;
776+var s: [64]U8; b: [16]U8;
776777 begin
777778 if (port < 0) or (port > 65535) or not cstr(addr, s) then return 0; end;
778779 a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *)
779780 a^.port[1] := U8(port & 255);
780- if C.inet_pton(2, ADR(s[0]), ADR(a^.data[0])) = 1 then a^.family := 2; return 16; end; (* AF_INET *)
781- if C.inet_pton(10, ADR(s[0]), ADR(a^.data[4])) = 1 then a^.family := 10; return 28; end; (* AF_INET6 *)
781+ (* into b first, since musl's inet_pton leaves what it read of an
782+ address it refuses, and the IPv6 one's first four bytes are its
783+ flow, to be zero *)
784+ if C.inet_pton(2, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET *)
785+ a^.family := 2;
786+ COPY(a^.data[0..4], b[0..4]);
787+ return 16;
788+ end;
789+ if C.inet_pton(10, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET6 *)
790+ a^.family := 10;
791+ COPY(a^.data[4..20], b);
792+ return 28;
793+ end;
782794 return 0;
783795 end;
784796
@@ -1378,7 +1390,8 @@
13781390 (* Closes every fd from 3 on but those of keep, which stay open in the
13791391 program exec starts. It runs in spawn's child, after the fork, so it
13801392 takes no memory: it closes the runs between the fds kept, the lowest
1381- first, each with one close_range. *)
1393+ first, each with one close_range: through syscall, since musl has no
1394+ close_range of its own. *)
13821395 proc closeAllBut(keep: []INT);
13831396 var lo, next, i: INT;
13841397 begin
@@ -1389,9 +1402,9 @@
13891402 if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end;
13901403 end;
13911404 if next < 0 then
1392- _ := C.close_range(U32(lo), 0FFFFFFFFH, 0); (* the rest *)
1405+ _ := C.syscall(436, lo, 0FFFFFFFFH, 0); (* close_range, the rest *)
13931406 else
1394- if next > lo then _ := C.close_range(U32(lo), U32(next - 1), 0); end;
1407+ if next > lo then _ := C.syscall(436, lo, next - 1, 0); end;
13951408 _ := C.fcntl(I32(next), 2, 0); (* F_SETFD: kept on exec *)
13961409 lo := next + 1;
13971410 end;

src/Elf.raz modified +24 -6

@@ -19,8 +19,11 @@
1919 Leaves out: position independent executables, symbol versions, the
2020 rest of DWARF.
2121
22- Exported variables can't be set from outside, so needLib, setMainPath
23- and setStripped do it. *)
22+ The C library is glibc's or musl's, as setMusl says: the loader the
23+ program names, and the library it needs first.
24+
25+ Exported variables can't be set from outside, so needLib, setMusl,
26+ setMainPath and setStripped do it. *)
2427 module Elf;
2528 import Sys, Files, Fmt, Diag, Scan, Chunk, Link;
2629
@@ -29,6 +32,8 @@
2932 numNeeded*: INT; (* none: a static program *)
3033 mainPath: []U8; (* the main module's file, for gdb *)
3134 stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *)
35+ interp: []U8; (* the loader a program that uses C names *)
36+ libc*: []U8; (* and the C library it needs *)
3237
3338 buf: ^U8;
3439 len, cap: INT;
@@ -53,6 +58,20 @@
5358 numNeeded := 0;
5459 end;
5560
61+(* A program that uses C is linked against musl, if yes, or glibc.
62+ musl's library is its own loader, which takes libc.so to mean
63+ itself, whatever file the system keeps it in. *)
64+proc setMusl*(yes: BOOL);
65+begin
66+ if yes then
67+ interp := "/lib/ld-musl-x86_64.so.1";
68+ libc := "libc.so";
69+ else
70+ interp := "/lib64/ld-linux-x86-64.so.2";
71+ libc := "libc.so.6";
72+ end;
73+end;
74+
5675 proc setMainPath*(path: []U8);
5776 begin
5877 mainPath := path;
@@ -195,7 +214,6 @@
195214 end;
196215
197216 const
198- Interp = "/lib64/ld-linux-x86-64.so.2";
199217 (* program header types *)
200218 PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H;
201219
@@ -224,7 +242,7 @@
224242 strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1;
225243 end;
226244 end;
227- symAt := (LEN(Interp) + 1 + 7) div 8 * 8;
245+ symAt := (LEN(interp) + 1 + 7) div 8 * 8;
228246 relaAt := symAt + nsym * 24;
229247 hashAt := relaAt + (nsym - 1) * 24;
230248 strAt := hashAt + 4 * (3 + nsym);
@@ -240,7 +258,7 @@
240258 var c, k, i, at, name: INT;
241259 begin
242260 len := Link.extraAdr - Link.roAdr + Link.roOff;
243- putStr(Interp);
261+ putStr(interp);
244262 padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt);
245263 putN(0, 24);
246264 name := strLen;
@@ -334,7 +352,7 @@
334352
335353 (* flags: 4 read, 5 read and run, 6 read and write *)
336354 if dyn then
337- phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(Interp) + 1, LEN(Interp) + 1, 1);
355+ phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1);
338356 end;
339357 (* the first segment maps the headers too, so text can follow them *)
340358 phdr(PtLoad, 5, 0, Link.Base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page);

src/Raz.raz (history) modified +25 -4

@@ -1,6 +1,6 @@
11 (* Raz: the command line.
22
3- raz [-I dir]... [-o file] [-Werror] [-S] Main.raz
3+ raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] Main.raz
44 raz run [-I dir]... Main.raz [arg]...
55 raz --version
66
@@ -18,7 +18,9 @@
1818 status. A static program does this from its entry point. One that
1919 uses C starts through the C library, __libc_start_main, which sets
2020 C up and calls this as C's main; first it gives Rt C's exit, which
21- Rt.exit then ends the program with.
21+ Rt.exit then ends the program with. The C library is glibc, or musl
22+ on a machine that has musl's loader and not glibc's, unless -libc
23+ names one.
2224
2325 Depends on: everything else, and Version, a module the build makes
2426 (build/src/Version.raz) from what git describe says, as the seed's
@@ -72,7 +74,7 @@
7274 begin
7375 st := nil; c := nil;
7476 Elf.noLibs();
75- _ := Elf.needLib(internStr("libc.so.6"));
77+ _ := Elf.needLib(internStr(Elf.libc));
7678 m := Parse.firstModule;
7779 while m # nil do
7880 if m^.used and m^.linkStatic then st := m; end;
@@ -104,12 +106,23 @@
104106 Out.str(" -s strips symbols, line numbers, the places a failure is told\n");
105107 Out.str(" by and the profiler: a failure says only its address\n");
106108 Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n");
109+ Out.str(" -libc name links a program that uses C against glibc or musl; by\n");
110+ Out.str(" default glibc, or musl where only its loader is there\n");
107111 Out.str(" -h, --help says this\n");
108112 Out.str(" --version says the version, and the report it follows\n");
109113 Out.str("see raz(1), and the report, doc/raz.md\n");
110114 Out.flush();
111115 end;
112116
117+(* Whether a program that uses C is linked against musl when -libc
118+ doesn't say: only if the machine has musl's loader and not glibc's,
119+ as Alpine has *)
120+proc muslHere: BOOL;
121+begin
122+ return (Sys.access("/lib64/ld-linux-x86-64.so.2", {}) # 0)
123+ and (Sys.access("/lib/ld-musl-x86_64.so.1", {}) = 0);
124+end;
125+
113126 (* The module named n, or nil if the program has none *)
114127 proc moduleNamed(n: []U8): ^Sym.Module;
115128 var m: ^Sym.Module;
@@ -159,12 +172,13 @@
159172 path, outPath, s: []U8; dir: []U8;
160173 i, first, entry, fd, last, errno: INT;
161174 m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj;
162- run, noProfile, listing, dynamic: BOOL; err: ERROR;
175+ run, noProfile, listing, dynamic, libcSaid: BOOL; err: ERROR;
163176 begin
164177 path := "";
165178 outPath := "";
166179 noProfile := false;
167180 listing := false;
181+ libcSaid := false;
168182 run := (ARGC() >= 2) and (Files.arg(1) = "run");
169183 if run then i := 2; else i := 1; end;
170184 while i < ARGC() do
@@ -185,6 +199,12 @@
185199 Elf.setStripped(true);
186200 elsif s = "-noprofile" then
187201 noProfile := true;
202+ elsif s = "-libc" then
203+ INC(i);
204+ s := Files.arg(i);
205+ if (s # "glibc") and (s # "musl") then Diag.fatal(say1("unknown C library {}: glibc or musl", s)); end;
206+ Elf.setMusl(s = "musl");
207+ libcSaid := true;
188208 elsif (s = "-h") or (s = "--help") then
189209 usage();
190210 return 0;
@@ -206,6 +226,7 @@
206226 return 1;
207227 end;
208228 if run and (LEN(outPath) > 0) then Diag.fatal("raz run takes no -o"); end;
229+ if not libcSaid then Elf.setMusl(muslHere()); end;
209230 (* where raz is, as the system says, for the library beside it *)
210231 dir, errno := Sys.readLink("/proc/self/exe");
211232 if errno # 0 then dir := Files.arg(0); end;

test/alpine.sh (history) added (executable) +32 -0

@@ -0,0 +1,32 @@
1+#!/bin/sh
2+# Runs test/libc on Alpine, whose C library is musl, in a docker
3+# container: raz, a static program, compiles each program there, so it
4+# links it against musl by default, having only musl's loader; then
5+# each runs, its output and status compared as test/check.sh compares
6+# them. Needs docker, and the network the first time, for the image.
7+
8+image=${ALPINE:-alpine:3.20}
9+exec docker run --rm -v "$PWD":/raz:ro -w /raz "$image" sh -c '
10+pass=0; fail=0; out=/tmp/out; mkdir -p $out
11+for f in test/libc/*.raz; do
12+ name=$(basename $f .raz)
13+ if ! ./raz -Werror -I test/libc/mods -o $out/$name $f 2>$out/$name.cerr; then
14+ echo "FAIL $f: does not compile: $(cat $out/$name.cerr)"; fail=$((fail + 1)); continue
15+ fi
16+ if ! grep -q ld-musl $out/$name; then # the loader it names
17+ echo "FAIL $f: not linked against musl"; fail=$((fail + 1)); continue
18+ fi
19+ [ -f test/libc/$name.expect ] || { pass=$((pass + 1)); continue; }
20+ status=$( { ( cd /tmp && exec $out/$name ) > $out/$name.out 2>$out/$name.err; echo $?; } 2>/dev/null )
21+ want=0; [ -f test/libc/$name.status ] && want=$(cat test/libc/$name.status)
22+ if ! cmp -s $out/$name.out test/libc/$name.expect; then
23+ echo "FAIL $f: output differs"; diff $out/$name.out test/libc/$name.expect | head -5; fail=$((fail + 1))
24+ elif [ $status != $want ]; then
25+ echo "FAIL $f: status $status, not $want"; cat $out/$name.err | head -5; fail=$((fail + 1))
26+ else
27+ pass=$((pass + 1))
28+ fi
29+done
30+cat /etc/alpine-release | sed "s/^/Alpine /"
31+echo "$pass passed, $fail failed"
32+[ $fail = 0 ]'

test/check.sh (history) modified +12 -5

@@ -35,8 +35,8 @@
3535 dir=$1; name=$2
3636 flags=
3737 [ $dir = examples ] || [ $dir = test/libc ] && flags=-Werror
38- if ! $raz $flags -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then
39- echo "FAIL $dir/$name: doesn't compile: $(cat $out/$name.cerr)"
38+ if ! $raz $flags $libc -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then
39+ echo "FAIL $dir/$name$libc: doesn't compile: $(cat $out/$name.cerr)"
4040 fail=$((fail + 1)); return
4141 fi
4242 if [ ! -f $dir/$name.expect ]; then pass=$((pass + 1)); return; fi
@@ -50,15 +50,15 @@
5050 want=0
5151 [ -f $dir/$name.status ] && want=$(cat $dir/$name.status)
5252 if ! cmp -s $out/$name.out $dir/$name.expect; then
53- echo "FAIL $dir/$name: output differs"
53+ echo "FAIL $dir/$name$libc: output differs"
5454 diff $out/$name.out $dir/$name.expect | head -5
5555 fail=$((fail + 1))
5656 elif [ $status != $want ]; then
57- echo "FAIL $dir/$name: status $status, not $want"
57+ echo "FAIL $dir/$name$libc: status $status, not $want"
5858 fail=$((fail + 1))
5959 elif [ -f $dir/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err |
6060 cmp -s - $dir/$name.stderr; then
61- echo "FAIL $dir/$name: standard error differs"
61+ echo "FAIL $dir/$name$libc: standard error differs"
6262 diff $out/$name.err $dir/$name.stderr | head -5
6363 fail=$((fail + 1))
6464 else
@@ -66,9 +66,16 @@
6666 fi
6767 }
6868
69+libc=
6970 for f in examples/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz; do
7071 run $(dirname $f) $(basename $f .raz)
7172 done
73+# test/libc again over musl, where its loader is installed
74+if [ -e /lib/ld-musl-x86_64.so.1 ]; then
75+ libc=' -libc musl'
76+ for f in test/libc/*.raz; do run test/libc $(basename $f .raz); done
77+ libc=
78+fi
7279
7380 # raz run: a static program and one over C, from memory, with their
7481 # arguments, output and status passed through

test/err/Clash.err (history) modified +2 -2

@@ -1,5 +1,5 @@
1-lib/Sys.raz:16:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz
2- 16 | import C;
1+lib/Sys.raz:17:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz
2+ 17 | import C;
33 | ^
44 help: a program has one module of each name; a symbolic link to one of these files chooses it
55 raz: 1 error