boot2

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

boot-undef.sh (2020B)


      1 #!/bin/sh
      2 ## scripts/boot-undef.sh — list hex2pp references with no matching definition.
      3 ##
      4 ## Cheap-and-cheerful linker-diagnostic for the live boot pipeline. M1pp
      5 ## emits expanded.hex2pp in asm-style: `:label` defines, `&label` (and
      6 ## other-sigil) references. A symbol with refs but no def is unresolved
      7 ## — the same thing hex2pp would flag, except hex2pp only prints the
      8 ## first miss before bailing, so this dumps the full list.
      9 ##
     10 ## Defaults to the expanded.hex2pp produced by the most recent
     11 ## `make tcc-boot2 ARCH=<arch>` build. Run that first if missing.
     12 ##
     13 ## Caveats:
     14 ##   - Reads post-m1pp output, so %la(...) macro args are already
     15 ##     expanded. Running this on the raw .P1pp would miss them.
     16 ##   - m1pp rewrites local labels (@body, @end, ...) to per-expansion suffixed
     17 ##     names, so they appear under both refs and defs naturally.
     18 ##
     19 ## Usage:
     20 ##   scripts/boot-undef.sh [--arch <aarch64|amd64|riscv64>] [<expanded.hex2pp>]
     21 
     22 set -eu
     23 
     24 ARCH=aarch64
     25 LINKED=
     26 while [ $# -gt 0 ]; do
     27     case "$1" in
     28         --arch)    ARCH=$2; shift 2 ;;
     29         -h|--help) sed -n 's/^## \{0,1\}//p' "$0"; exit 0 ;;
     30         --) shift; break ;;
     31         -*) echo "unknown arg: $1" >&2; exit 2 ;;
     32         *)  LINKED=$1; shift ;;
     33     esac
     34 done
     35 
     36 ROOT=$(cd "$(dirname "$0")/.." && pwd)
     37 : "${LINKED:=$ROOT/build/$ARCH/.work/tcc-boot2/tcc-boot2/expanded.hex2pp}"
     38 
     39 [ -r "$LINKED" ] || {
     40     echo "missing $LINKED" >&2
     41     echo "  run: make tcc-boot2 ARCH=$ARCH" >&2
     42     exit 1
     43 }
     44 
     45 REFS=$(mktemp)
     46 DEFS=$(mktemp)
     47 trap 'rm -f "$REFS" "$DEFS"' EXIT
     48 
     49 grep -oE '&[a-zA-Z_][a-zA-Z_0-9]*' "$LINKED" | cut -c2- | sort -u > "$REFS"
     50 grep -oE '^:[a-zA-Z_][a-zA-Z_0-9]*' "$LINKED" | cut -c2- | sort -u > "$DEFS"
     51 
     52 UNDEF=$(comm -23 "$REFS" "$DEFS")
     53 NREF=$(wc -l < "$REFS" | tr -d ' ')
     54 NDEF=$(wc -l < "$DEFS" | tr -d ' ')
     55 NUND=$(printf '%s\n' "$UNDEF" | grep -c . || true)
     56 
     57 printf '[boot-undef %s] %s\n' "$ARCH" "$LINKED" >&2
     58 printf '  refs=%s defs=%s undef=%s\n' "$NREF" "$NDEF" "$NUND" >&2
     59 
     60 [ "$NUND" -eq 0 ] || printf '%s\n' "$UNDEF"