RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / scripts / build-fixtures.sh
2226 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
# Build the kernel's ABI test programs ("fixtures") from the pinned kernel
# source (out/kernel-src/user, exported by build-kernel.sh), with exactly the
# commands the kernel's build.rs uses. Under boot contract v1 the image
# carries them in /bin because the self-test runs them; they are *not* part
# of Foundry OS and their source stays in the kernel repo.
#
# Usage: scripts/build-fixtures.sh KERNEL_SRC OUT_DIR "rust names" "c names" "musl name:kind ..."
set -euo pipefail
SRC=$(realpath "$1")/user OUT=$(realpath -m "$2")
RUST=$3 C=$4 MUSL=$5
mkdir -p "$OUT"
RUSTC=${RUSTC:-rustc}
# Use the kernel's toolchain (rust-toolchain.toml in KERNEL_SRC): run from there.
cd "$SRC/.."
for p in $RUST; do
    "$RUSTC" --edition 2024 --target x86_64-unknown-none \
        -C panic=abort -C opt-level=2 -C debuginfo=0 -C strip=debuginfo \
        -C relocation-model=static -C code-model=large \
        -C "link-arg=-T$SRC/link.ld" -o "$OUT/$p" "$SRC/$p.rs"
done
# No libc at all: raw Linux syscalls, an ordinary static ET_EXEC.
for p in $C; do
    ${CC:-cc} -O2 -static -nostdlib -ffreestanding -fno-stack-protector \
        -fpie -no-pie -Wall -Wextra \
        -Wl,-Ttext-segment=0x8000400000 -Wl,--build-id=none -z noexecstack \
        -o "$OUT/$p" "$SRC/$p.c"
done
# musl (Debian's musl-tools). Rustbelt 0.19.0 loads static-PIE, so "pie"
# is plain `musl-gcc -static-pie` with no tricks; "exec" is the old
# ET_EXEC-at-the-user-base recipe (kept as a fixture for the loader).
MUSL_LIB=/usr/lib/x86_64-linux-musl
for entry in $MUSL; do
    p=${entry%%:*} kind=${entry#*:}
    common=(-O2 -Wall -Wextra -pthread -Wl,--build-id=none)
    case $kind in
        pie)  musl-gcc "${common[@]}" -static-pie "$SRC/$p.c" -o "$OUT/$p" ;;
        dyn)  musl-gcc "${common[@]}" -fPIE -pie "$SRC/$p.c" -o "$OUT/$p" ;;
        exec) musl-gcc "${common[@]}" -static -no-pie -nostartfiles \
                  -Wl,-Ttext-segment=0x8000400000 -Wl,--defsym=_DYNAMIC=0x8000400000 -Wl,--no-relax \
                  $MUSL_LIB/crt1.o $MUSL_LIB/crti.o "$SRC/$p.c" $MUSL_LIB/crtn.o -o "$OUT/$p" ;;
        *) echo "unknown musl kind $kind for $p"; exit 1 ;;
    esac
done
echo "[fixtures] $(ls "$OUT" | wc -l) programs in $OUT"