From 6a6bdf5cfef3a0213f21cd4f7ec422b9a99ce95e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:04:38 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index 9de5564..cad4c6d 100644 --- a/Makefile +++ b/Makefile @@ -81,6 +81,10 @@ # native threads under load: 40,000 spawned and joined, 200 alive at a # time, all waiting on one Mutex; then, with little memory, spawn must # fail with Thread.Failed. make check runs a small version. +# test/libc on Alpine, over musl, in docker +alpine: raz + test/alpine.sh + stress: raz | build ./raz -o build/Stress lib/Thread.linux-x86_64.raz build/Stress 40000 200 @@ -181,4 +185,4 @@ site: raz pdf python3 site/build.py -.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress fmt loc man pdf dist site clean install uninstall +.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site clean install uninstall diff --git a/README.md b/README.md index 5ececbe..b763b7a 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ ## Using it - raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] Main.raz raz run [option]... Main.raz [arg]... raz -h raz --version diff --git a/doc/compiler.md b/doc/compiler.md index 3d8542f..6c57db3 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -22,8 +22,8 @@ | `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,176 | 3,615 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | -| `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 305 | 238 | The command line | +| `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | +| `src/Raz.raz` | 326 | 253 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -1636,8 +1636,15 @@ section naming the libraries; and, for each C symbol the program uses, a symbol and a `GLOB_DAT` relocation that has the loader write the symbol's address into the quadword `Chunk.externChunk` made. Code calls C -through that quadword. There is no PLT and no lazy binding. -`sizeTables` and `putTables` lay these out in the room `Link.raz` left +through that quadword. There is no PLT and no lazy binding. The +loader and the first library are glibc's, +`/lib64/ld-linux-x86-64.so.2` and `libc.so.6`, or musl's, +`/lib/ld-musl-x86_64.so.1` and `libc.so`, which musl's loader, being +the library itself, takes to mean itself (`Elf.setMusl`). `-libc` +chooses; left out, `Raz.muslHere` chooses musl where the machine has +its loader and not glibc's, as Alpine does. A symbol that isn't live +gets no relocation, so a program runs over a C library that lacks a +procedure it declares but never reaches. `sizeTables` and `putTables` lay these out in the room `Link.raz` left at the end of rodata. Line numbers are DWARF 4: the line table `makeLines` made from the @@ -1682,7 +1689,9 @@ - **The target.** x86-64 Linux, `linux-x86_64`: `INT` is 64 bits, and there are reals. Every module of a program is compiled together, in one run, into a static ELF executable, or a dynamic one if the - program uses C; no object files. + program uses C; no object files. The C library is glibc or musl, + as `-libc` says, or by default glibc, and musl where only its + loader is there. - **Finding modules.** `raz Main.raz` looks for `X`, imported, in `Main.raz`'s directory, then in each `-I dir` in the order given (16 at most), then in `lib` beside the `raz` executable, following diff --git a/doc/raz.1 b/doc/raz.1 index 38af755..a95ea52 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -103,6 +103,16 @@ Leave out the profiler, which .Ev RAZ_PROFILE turns on, but keep the rest. +.It Fl libc Ar name +Link a program that uses C against +.Ar name , +.Cm glibc +or +.Cm musl . +Left out, it is glibc, or musl on a machine that has musl's loader, +.Pa /lib/ld-musl-x86_64.so.1 , +and not glibc's, as Alpine has. +A program without C needs no C library, and runs on either. .It Fl h , Fl \-help Say the options. .It Fl \-version diff --git a/lib/C.raz b/lib/C.raz index 7e4b986..24cda85 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -76,6 +76,7 @@ extern proc dup2*(fd, fd2: I32): I32; extern proc dup*(fd: I32): I32; extern proc close_range*(first, last: U32; flags: I32): I32; +extern proc syscall*(n: INT; ...): INT; (* a system call by its number, on Linux *) extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; extern proc kill*(pid, signal: I32): I32; diff --git a/lib/Sys.raz b/lib/Sys.raz index ae8bbd4..dbc048e 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -7,7 +7,8 @@ only the two struct timeval that begin struct rusage are read. What is glibc's, on x86-64: where a name lies in struct dirent; struct stat; open's flags; the numbers of the requests TIOCGWINSZ, - TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range; + TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range's + system call; SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs start with a byte of length; the numbers of the signals and of the limits, and RLIM_INFINITY, all ones. A C library that differs needs @@ -772,13 +773,24 @@ of its struct sockaddr_in or sockaddr_in6, or 0 if either is wrong. *) proc inetAddr(addr: []U8; port: INT; a: ^Sockaddr): INT; -var s: [64]U8; +var s: [64]U8; b: [16]U8; begin if (port < 0) or (port > 65535) or not cstr(addr, s) then return 0; end; a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *) a^.port[1] := U8(port & 255); - if C.inet_pton(2, ADR(s[0]), ADR(a^.data[0])) = 1 then a^.family := 2; return 16; end; (* AF_INET *) - if C.inet_pton(10, ADR(s[0]), ADR(a^.data[4])) = 1 then a^.family := 10; return 28; end; (* AF_INET6 *) + (* into b first, since musl's inet_pton leaves what it read of an + address it refuses, and the IPv6 one's first four bytes are its + flow, to be zero *) + if C.inet_pton(2, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET *) + a^.family := 2; + COPY(a^.data[0..4], b[0..4]); + return 16; + end; + if C.inet_pton(10, ADR(s[0]), ADR(b[0])) = 1 then (* AF_INET6 *) + a^.family := 10; + COPY(a^.data[4..20], b); + return 28; + end; return 0; end; @@ -1378,7 +1390,8 @@ (* Closes every fd from 3 on but those of keep, which stay open in the program exec starts. It runs in spawn's child, after the fork, so it takes no memory: it closes the runs between the fds kept, the lowest - first, each with one close_range. *) + first, each with one close_range: through syscall, since musl has no + close_range of its own. *) proc closeAllBut(keep: []INT); var lo, next, i: INT; begin @@ -1389,9 +1402,9 @@ if (keep[i] >= lo) and ((next < 0) or (keep[i] < next)) then next := keep[i]; end; end; if next < 0 then - _ := C.close_range(U32(lo), 0FFFFFFFFH, 0); (* the rest *) + _ := C.syscall(436, lo, 0FFFFFFFFH, 0); (* close_range, the rest *) else - if next > lo then _ := C.close_range(U32(lo), U32(next - 1), 0); end; + if next > lo then _ := C.syscall(436, lo, next - 1, 0); end; _ := C.fcntl(I32(next), 2, 0); (* F_SETFD: kept on exec *) lo := next + 1; end; diff --git a/src/Elf.raz b/src/Elf.raz index 19defdb..6f4b84e 100644 --- a/src/Elf.raz +++ b/src/Elf.raz @@ -19,8 +19,11 @@ Leaves out: position independent executables, symbol versions, the rest of DWARF. - Exported variables can't be set from outside, so needLib, setMainPath - and setStripped do it. *) + The C library is glibc's or musl's, as setMusl says: the loader the + program names, and the library it needs first. + + Exported variables can't be set from outside, so needLib, setMusl, + setMainPath and setStripped do it. *) module Elf; import Sys, Files, Fmt, Diag, Scan, Chunk, Link; @@ -29,6 +32,8 @@ numNeeded*: INT; (* none: a static program *) mainPath: []U8; (* the main module's file, for gdb *) stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *) + interp: []U8; (* the loader a program that uses C names *) + libc*: []U8; (* and the C library it needs *) buf: ^U8; len, cap: INT; @@ -53,6 +58,20 @@ numNeeded := 0; end; +(* A program that uses C is linked against musl, if yes, or glibc. + musl's library is its own loader, which takes libc.so to mean + itself, whatever file the system keeps it in. *) +proc setMusl*(yes: BOOL); +begin + if yes then + interp := "/lib/ld-musl-x86_64.so.1"; + libc := "libc.so"; + else + interp := "/lib64/ld-linux-x86-64.so.2"; + libc := "libc.so.6"; + end; +end; + proc setMainPath*(path: []U8); begin mainPath := path; @@ -195,7 +214,6 @@ end; const - Interp = "/lib64/ld-linux-x86-64.so.2"; (* program header types *) PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H; @@ -224,7 +242,7 @@ strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; end; end; - symAt := (LEN(Interp) + 1 + 7) div 8 * 8; + symAt := (LEN(interp) + 1 + 7) div 8 * 8; relaAt := symAt + nsym * 24; hashAt := relaAt + (nsym - 1) * 24; strAt := hashAt + 4 * (3 + nsym); @@ -240,7 +258,7 @@ var c, k, i, at, name: INT; begin len := Link.extraAdr - Link.roAdr + Link.roOff; - putStr(Interp); + putStr(interp); padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt); putN(0, 24); name := strLen; @@ -334,7 +352,7 @@ (* flags: 4 read, 5 read and run, 6 read and write *) if dyn then - phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(Interp) + 1, LEN(Interp) + 1, 1); + phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1); end; (* the first segment maps the headers too, so text can follow them *) phdr(PtLoad, 5, 0, Link.Base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); diff --git a/src/Raz.raz b/src/Raz.raz index 5f9cffa..5c7405a 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -1,6 +1,6 @@ (* Raz: the command line. - raz [-I dir]... [-o file] [-Werror] [-S] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] Main.raz raz run [-I dir]... Main.raz [arg]... raz --version @@ -18,7 +18,9 @@ status. A static program does this from its entry point. One that uses C starts through the C library, __libc_start_main, which sets C up and calls this as C's main; first it gives Rt C's exit, which - Rt.exit then ends the program with. + Rt.exit then ends the program with. The C library is glibc, or musl + on a machine that has musl's loader and not glibc's, unless -libc + names one. Depends on: everything else, and Version, a module the build makes (build/src/Version.raz) from what git describe says, as the seed's @@ -72,7 +74,7 @@ begin st := nil; c := nil; Elf.noLibs(); - _ := Elf.needLib(internStr("libc.so.6")); + _ := Elf.needLib(internStr(Elf.libc)); m := Parse.firstModule; while m # nil do if m^.used and m^.linkStatic then st := m; end; @@ -104,12 +106,23 @@ Out.str(" -s strips symbols, line numbers, the places a failure is told\n"); Out.str(" by and the profiler: a failure says only its address\n"); Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n"); + Out.str(" -libc name links a program that uses C against glibc or musl; by\n"); + Out.str(" default glibc, or musl where only its loader is there\n"); Out.str(" -h, --help says this\n"); Out.str(" --version says the version, and the report it follows\n"); Out.str("see raz(1), and the report, doc/raz.md\n"); Out.flush(); end; +(* Whether a program that uses C is linked against musl when -libc + doesn't say: only if the machine has musl's loader and not glibc's, + as Alpine has *) +proc muslHere: BOOL; +begin + return (Sys.access("/lib64/ld-linux-x86-64.so.2", {}) # 0) + and (Sys.access("/lib/ld-musl-x86_64.so.1", {}) = 0); +end; + (* The module named n, or nil if the program has none *) proc moduleNamed(n: []U8): ^Sym.Module; var m: ^Sym.Module; @@ -159,12 +172,13 @@ path, outPath, s: []U8; dir: []U8; i, first, entry, fd, last, errno: INT; m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; - run, noProfile, listing, dynamic: BOOL; err: ERROR; + run, noProfile, listing, dynamic, libcSaid: BOOL; err: ERROR; begin path := ""; outPath := ""; noProfile := false; listing := false; + libcSaid := false; run := (ARGC() >= 2) and (Files.arg(1) = "run"); if run then i := 2; else i := 1; end; while i < ARGC() do @@ -185,6 +199,12 @@ Elf.setStripped(true); elsif s = "-noprofile" then noProfile := true; + elsif s = "-libc" then + INC(i); + s := Files.arg(i); + if (s # "glibc") and (s # "musl") then Diag.fatal(say1("unknown C library {}: glibc or musl", s)); end; + Elf.setMusl(s = "musl"); + libcSaid := true; elsif (s = "-h") or (s = "--help") then usage(); return 0; @@ -206,6 +226,7 @@ return 1; end; if run and (LEN(outPath) > 0) then Diag.fatal("raz run takes no -o"); end; + if not libcSaid then Elf.setMusl(muslHere()); end; (* where raz is, as the system says, for the library beside it *) dir, errno := Sys.readLink("/proc/self/exe"); if errno # 0 then dir := Files.arg(0); end; diff --git a/test/alpine.sh b/test/alpine.sh new file mode 100755 index 0000000..8c1106a --- /dev/null +++ b/test/alpine.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Runs test/libc on Alpine, whose C library is musl, in a docker +# container: raz, a static program, compiles each program there, so it +# links it against musl by default, having only musl's loader; then +# each runs, its output and status compared as test/check.sh compares +# them. Needs docker, and the network the first time, for the image. + +image=${ALPINE:-alpine:3.20} +exec docker run --rm -v "$PWD":/raz:ro -w /raz "$image" sh -c ' +pass=0; fail=0; out=/tmp/out; mkdir -p $out +for f in test/libc/*.raz; do + name=$(basename $f .raz) + if ! ./raz -Werror -I test/libc/mods -o $out/$name $f 2>$out/$name.cerr; then + echo "FAIL $f: does not compile: $(cat $out/$name.cerr)"; fail=$((fail + 1)); continue + fi + if ! grep -q ld-musl $out/$name; then # the loader it names + echo "FAIL $f: not linked against musl"; fail=$((fail + 1)); continue + fi + [ -f test/libc/$name.expect ] || { pass=$((pass + 1)); continue; } + status=$( { ( cd /tmp && exec $out/$name ) > $out/$name.out 2>$out/$name.err; echo $?; } 2>/dev/null ) + want=0; [ -f test/libc/$name.status ] && want=$(cat test/libc/$name.status) + if ! cmp -s $out/$name.out test/libc/$name.expect; then + echo "FAIL $f: output differs"; diff $out/$name.out test/libc/$name.expect | head -5; fail=$((fail + 1)) + elif [ $status != $want ]; then + echo "FAIL $f: status $status, not $want"; cat $out/$name.err | head -5; fail=$((fail + 1)) + else + pass=$((pass + 1)) + fi +done +cat /etc/alpine-release | sed "s/^/Alpine /" +echo "$pass passed, $fail failed" +[ $fail = 0 ]' diff --git a/test/check.sh b/test/check.sh index 041ea08..9f8bf5c 100755 --- a/test/check.sh +++ b/test/check.sh @@ -35,8 +35,8 @@ dir=$1; name=$2 flags= [ $dir = examples ] || [ $dir = test/libc ] && flags=-Werror - if ! $raz $flags -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then - echo "FAIL $dir/$name: doesn't compile: $(cat $out/$name.cerr)" + if ! $raz $flags $libc -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then + echo "FAIL $dir/$name$libc: doesn't compile: $(cat $out/$name.cerr)" fail=$((fail + 1)); return fi if [ ! -f $dir/$name.expect ]; then pass=$((pass + 1)); return; fi @@ -50,15 +50,15 @@ want=0 [ -f $dir/$name.status ] && want=$(cat $dir/$name.status) if ! cmp -s $out/$name.out $dir/$name.expect; then - echo "FAIL $dir/$name: output differs" + echo "FAIL $dir/$name$libc: output differs" diff $out/$name.out $dir/$name.expect | head -5 fail=$((fail + 1)) elif [ $status != $want ]; then - echo "FAIL $dir/$name: status $status, not $want" + echo "FAIL $dir/$name$libc: status $status, not $want" fail=$((fail + 1)) elif [ -f $dir/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err | cmp -s - $dir/$name.stderr; then - echo "FAIL $dir/$name: standard error differs" + echo "FAIL $dir/$name$libc: standard error differs" diff $out/$name.err $dir/$name.stderr | head -5 fail=$((fail + 1)) else @@ -66,9 +66,16 @@ fi } +libc= for f in examples/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz; do run $(dirname $f) $(basename $f .raz) done +# test/libc again over musl, where its loader is installed +if [ -e /lib/ld-musl-x86_64.so.1 ]; then + libc=' -libc musl' + for f in test/libc/*.raz; do run test/libc $(basename $f .raz); done + libc= +fi # raz run: a static program and one over C, from memory, with their # arguments, output and status passed through diff --git a/test/err/Clash.err b/test/err/Clash.err index 71cb9e1..0443842 100644 --- a/test/err/Clash.err +++ b/test/err/Clash.err @@ -1,5 +1,5 @@ -lib/Sys.raz:16:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz - 16 | import C; +lib/Sys.raz:17:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz + 17 | import C; | ^ help: a program has one module of each name; a symbolic link to one of these files chooses it raz: 1 error