boot2

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

build-p1.sh (2926B)


      1 #!/bin/sh
      2 ## build-p1.sh -- build a P1v2 .P1pp source for the M1pp-based P1 pipeline.
      3 ##
      4 ## Pipeline:
      5 ##   1. catm     p1/P1-<arch>.M1pp + p1/P1.M1pp + <source.P1pp> -> combined.M1pp
      6 ##   2. m1pp     combined.M1pp -> expanded.M1 (macros -> raw hex + labels)
      7 ##   3. M1       expanded.M1 -> prog.hex2 (stringify literals, pass hex through)
      8 ##   4. catm     ELF-<arch>.hex2 + prog.hex2 -> linked.hex2
      9 ##   5. hex2     linked.hex2 -> raw ELF
     10 ##   6. chmod + trim to p_filesz, deposit at <output>
     11 ##
     12 ## Usage: m1pp/build-p1.sh <arch> <source.P1pp> <output>
     13 ##   arch: aarch64 | riscv64 | amd64
     14 ##
     15 ## The m1pp expander itself is architecture-neutral; we run the aarch64
     16 ## binary under podman linux/arm64 for all target arches. M1/hex2 run
     17 ## natively on the host via build/native-tools/.
     18 
     19 set -eu
     20 
     21 if [ "$#" -ne 3 ]; then
     22     echo "usage: $0 <arch> <source.P1pp> <output>" >&2
     23     exit 2
     24 fi
     25 
     26 ARCH=$1
     27 SRC=$2
     28 OUT=$3
     29 
     30 case "$ARCH" in
     31     aarch64) ELF_HDR=build/upstream/AArch64/ELF-aarch64.hex2 ;;
     32     amd64)   ELF_HDR=build/upstream/AMD64/ELF-amd64.hex2 ;;
     33     riscv64) ELF_HDR=build/upstream/riscv64/ELF-riscv64.hex2 ;;
     34     *) echo "build-p1.sh: unsupported arch '$ARCH'" >&2; exit 1 ;;
     35 esac
     36 
     37 REPO=$(cd "$(dirname "$0")/.." && pwd)
     38 cd "$REPO"
     39 
     40 FRONTEND=p1/P1.M1pp
     41 BACKEND=p1/P1-$ARCH.M1pp
     42 
     43 for f in "$BACKEND" "$FRONTEND" "$ELF_HDR" "$SRC"; do
     44     if [ ! -e "$f" ]; then
     45         echo "build-p1.sh: missing input: $f" >&2
     46         exit 1
     47     fi
     48 done
     49 
     50 ## Host-compiled m1pp has 64K-token / 512K-text buffers, versus the ~4K /
     51 ## 32K cap in the aarch64 self-hosted m1pp.P1. The combined backend +
     52 ## frontend + source easily blows past the 4K-token cap, so use native.
     53 NATIVE_M1PP=build/native-tools/m1pp
     54 NATIVE_M1=build/native-tools/M1
     55 NATIVE_HEX2=build/native-tools/hex2
     56 if [ ! -x "$NATIVE_M1PP" ]; then
     57     : "${CC:=cc}"
     58     mkdir -p build/native-tools
     59     $CC -O2 -std=c99 -o "$NATIVE_M1PP" m1pp/m1pp.c
     60 fi
     61 if [ ! -x "$NATIVE_M1" ] || [ ! -x "$NATIVE_HEX2" ]; then
     62     sh m1pp/build-native-tools.sh
     63 fi
     64 
     65 NAME=$(basename "$SRC" .P1pp)
     66 WORK=build/p1v2-m1pp/$ARCH/$NAME.work
     67 mkdir -p "$WORK" "$(dirname "$OUT")"
     68 
     69 COMBINED=$WORK/combined.M1pp
     70 EXPANDED=$WORK/expanded.M1
     71 PROG_HEX2=$WORK/prog.hex2
     72 LINKED=$WORK/linked.hex2
     73 RAW=$WORK/prog.raw
     74 
     75 cat "$BACKEND" "$FRONTEND" "$SRC" > "$COMBINED"
     76 
     77 "$NATIVE_M1PP" "$COMBINED" "$EXPANDED"
     78 
     79 "$NATIVE_M1" --architecture "$ARCH" --little-endian \
     80     -f "$EXPANDED" -o "$PROG_HEX2"
     81 
     82 cat "$ELF_HDR" "$PROG_HEX2" > "$LINKED"
     83 
     84 "$NATIVE_HEX2" --architecture "$ARCH" --little-endian \
     85     --base-address 0x600000 \
     86     -f "$LINKED" -o "$RAW"
     87 
     88 ## Trim trailing zero padding past p_filesz (lives at byte offset 96 as a
     89 ## little-endian u32 in the ELF64 program header). The hex2 output
     90 ## zero-fills to p_memsz; the kernel zero-fills the BSS gap at load time
     91 ## so we can chop anything past p_filesz on disk.
     92 size=$(od -An -tu4 -N4 -j96 "$RAW" | tr -d ' ')
     93 head -c "$size" "$RAW" > "$OUT"
     94 chmod 0700 "$OUT"