boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

bootstrap.sh (2863B)


      1 #!/bin/sh
      2 # Bootstrap the seed0 toolchain we need to assemble + link P1 programs:
      3 # hex0-seed -> hex0 -> hex1 -> hex2-0 -> catm -> M0.
      4 #
      5 # Runs inside a target-arch alpine container. All produced binaries are
      6 # target-arch Linux ELF and are written to $OUT as: hex0 hex1 hex2-0 catm M0.
      7 #
      8 # The only non-source input is bootstrap-seeds/POSIX/<Arch>/hex0-seed (~400
      9 # bytes, hand-assembled, shipped by stage0-posix). Nothing above M0 is built,
     10 # which is the whole point — no C compiler is involved, not even cc_<arch>.
     11 #
     12 # Inputs are read from build/upstream/, which populate-upstream.sh mirrors
     13 # from live-bootstrap's stage0-posix on the host. The container mounts only
     14 # curdir, so everything bootstrap.sh needs must already live inside it.
     15 #
     16 # Phase map (stage0-posix mescc-tools-{seed,mini}-kaem.kaem phases 0-3):
     17 #   0)  hex0-seed + hex0_<A>.hex0             -> hex0
     18 #   1)  hex0      + hex1_<A>.hex0             -> hex1
     19 #   2)  hex1      + hex2_<A>.hex1             -> hex2-0
     20 #   2b)  hex2-0   + catm_<A>.hex2             -> catm
     21 #   3a) catm   : ELF header + M0_<A>.hex2     -> M0.hex2
     22 #   3b) hex2-0 : M0.hex2                      -> M0
     23 #
     24 # Usage: bootstrap.sh <arch> <out-dir>
     25 #   arch:    aarch64 | amd64 | riscv64
     26 #   out-dir: absolute path where tool binaries should land
     27 set -eu
     28 
     29 ARCH=$1
     30 OUT=$2
     31 
     32 # Map lispcc's lowercase ARCH to stage0-posix's dir name.
     33 case "$ARCH" in
     34     aarch64) A=AArch64 ;;
     35     amd64)   A=AMD64   ;;
     36     riscv64) A=riscv64 ;;
     37     *) echo "bootstrap.sh: unsupported arch '$ARCH'" >&2 ; exit 1 ;;
     38 esac
     39 
     40 S=build/upstream
     41 mkdir -p "$OUT"
     42 
     43 # qemu-user amd64 workaround: the shipped hex0-seed and the hex0 it produces
     44 # both have a program header with p_flags=0x01 (PF_X only, no PF_R). A native
     45 # Linux kernel treats x86-64's hardware coercion as making this loadable, but
     46 # qemu-user's stricter ELF loader faults when fetching instructions from an
     47 # unreadable segment. Copy the seed to a writable location and flip p_flags
     48 # to 0x05 (PF_R|PF_X) before use. All later seed sources already use 0x07.
     49 #
     50 # This only affects foreign-arch builds on non-amd64 hosts; on a native amd64
     51 # host the patch is a no-op (binary would load fine either way).
     52 SEED="$S"/bootstrap-seeds/POSIX/"$A"/hex0-seed
     53 if [ "$ARCH" = amd64 ]; then
     54     cp "$SEED" "$OUT"/hex0-seed
     55     printf '\5' | dd of="$OUT"/hex0-seed bs=1 seek=68 count=1 conv=notrunc status=none
     56     chmod +x "$OUT"/hex0-seed
     57     SEED="$OUT"/hex0-seed
     58 fi
     59 
     60 "$SEED" "$S"/"$A"/hex0_"$A".hex0 "$OUT"/hex0
     61 if [ "$ARCH" = amd64 ]; then
     62     printf '\5' | dd of="$OUT"/hex0 bs=1 seek=68 count=1 conv=notrunc status=none
     63 fi
     64 
     65 "$OUT"/hex0  "$S"/"$A"/hex1_"$A".hex0 "$OUT"/hex1
     66 "$OUT"/hex1  "$S"/"$A"/hex2_"$A".hex1 "$OUT"/hex2-0
     67 "$OUT"/hex2-0 "$S"/"$A"/catm_"$A".hex2 "$OUT"/catm
     68 "$OUT"/catm  "$OUT"/M0.hex2 "$S"/"$A"/ELF-"$ARCH".hex2 "$S"/"$A"/M0_"$A".hex2
     69 "$OUT"/hex2-0 "$OUT"/M0.hex2 "$OUT"/M0