goboot hex0-to-go.md
11 kB 226 lines
Markdown
at main

Hex0 to Go: The Shortest Bootstrap Path #

A study of the stagex bootstrap history (stage0/stage1) and the standalone ~/Sources/bootstrap experiment, distilled into the shortest known build path from 181 bytes of hex0 to a self-hosting Go toolchain.

Sources studied:

  • stagex git history (packages/bootstrap/stage0, packages/bootstrap/stage1, packages/core/go)
  • ~/Sources/bootstrap (the August 2026 pnut experiment, branch main, HEAD 5a5ab5a91 "Complete TCC 0.9.26 bootstrap from pnut")
  • ~/Sources/live-bootstrap (upstream reference, incl. PR 554: pnut-exe replaces mes for tcc-0.9.26)

1. History: how stagex bootstrapped Go, and when it stopped #

Stage0 (then) #

The original stage0 (903a11358f, Dec 2023) was simply the stage0-posix kaem-optional-seed run on a host distro (built redundantly on Debian, Arch, and Alpine and cross-verified by the produced sha256sum against itself). It consumed a system gcc to assemble the seeds. Not hex0-pure; the host compiler was a trust root.

The overhaul (220ff3cddd, Jan 2025, "overhaul stage0-1 for improved trust/cache/debugging/reliability") made stage0 self-contained: an 181-byte hex0-seed binary committed to the repo boots the entire chain in a FROM scratch container with no host toolchain at all:

hex0-seed -> hex0 -> hex1 -> hex2-0 -> catm -> M0 -> cc_x86 -> M2 (M2-Planet seed)
          -> blood-elf, M1, hex2, kaem, M2-Mesoplanet, mescc-tools-extra

Stage0 ships hex2, kaem, M1 (and friends) as its package output.

Stage1 (then, the tinycc era) #

Stage1 (FROM scratch, built with stage0's kaem/M1/hex2) ran the live-bootstrap steps in kaem pass1.kaem scripts:

checksum-transcriber -> simple-patch -> mes-0.27 -> tcc-0.9.26 (pass1.kaem)

From there it switched to RUN build <pkg> <script> with kaem, then bash: tcc-0.9.27 pass1, make-3.82, patch, gzip, tar, sed, bzip2, coreutils-5.0, byacc, bash-2.05b, then the long GCC-free list.

Go entered stage1 in 03b47ea4a6 (Jan 19 2025, "feat: bootstrap Go in stage1", Sharshakov/Panek). The compiler at that point was tcc-0.9.27 pass5 (tcc bootstrapped against musl-1.1.24). The chain appended to stage1:

tcc-0.9.27 pass5  --CC-->  go-1.4  ->  go-1.19.11  ->  go-1.20.6  ->  go-1.23.5

All on GOHOSTARCH=386 (linux/386 forced platform). go-1.4 was compiled with CC=tcc bash make.bash; later versions with CGO_ENABLED=0 chaining GOROOT_BOOTSTRAP. A small copy-escape.patch to live-bootstrap's helpers.sh (cd -- "${f}") was needed so the Go source tree's awkward testdata paths survived the timestamp-reset/copy helpers.

Go also went into stage3 (f8f00af292) as an amd64-native cross from the stage1 386 toolchain, since Go only gained arm64 support in 1.9.

Notably go-1.4 never needed anything but tcc: the last GCC builds in stage1 (gcc-4.0.4/4.7.4/10.4/13.1) all happen after Go was already built.

Stage1 (now): Go leaves the bootstrap path #

f9830a7b6e (Feb 22 2026, "feat: bootstrap Go in core") removed Go from stage1 and stage3 entirely and moved the whole ladder into packages/core/go. Rationale from the commit: Go can be bootstrapped independently from the stage1 image (+ core-filesystem for packaging), so the critical bootstrap chain stays smaller. It also notes Go must bootstrap on i386/amd64 because arm64 only exists from Go 1.9.

Current packages/core/go/Containerfile (as of that commit, version 1.26.0):

FROM --platform=linux/386 stagex/bootstrap-stage1
go1.4 -> go1.19.11 -> go1.20.6 -> go1.22.12 -> go1.24.6 -> go${VERSION}
                                       (last step cross-compiles GOARCH=$TARGETARCH)

with a verification step: the freshly built tree's bin/ and pkg/tool binaries must sha512sum -c match the official go${VERSION}.linux-${ARCH}.tar.gz release. That is a reproducibility witness, not a trust dependency: if the check fails, our build stands on its own.

Answer to "when did we last bootstrap Go directly from tinycc": the in-stage1 tcc -> go-1.4 build was live from 2025-01-19 (03b47ea4a6) until 2026-02-22 (f9830a7b6e). After that, Go still descends from tinycc transitively (stage1 still contains tcc-0.9.27 pass5), but core/go consumes the stage1 image rather than sitting in the bootstrap chain, and its go1.4 build no longer pins CC=tcc explicitly (it inherits stage1's default CC).

The other tinycc lineage: stage1's tcc-0.9.26 comes from mes, not pnut #

One subtlety the ~/Sources/bootstrap repo exists to solve. In stagex/stage1 (today and throughout history) the chain into tinycc is:

M2-Planet -> mes-0.27 (Scheme interpreter + Mes libc) -> tcc-0.9.26 (kaem pass1.kaem)

mes is a large dependency (a Scheme interpreter, plus nyacc, on the critical path), and the stage1 Containerfile even carries a comment about how mescc is reload-happy and slow. Upstream live-bootstrap PR 554 ("Adapt tcc-0.9.26's step to use pnut-exe instead of mes", fetched into ~/Sources/live-bootstrap) replaces it: pnut compiles tcc-0.9.26 directly from M2-Planet output, and mes is reduced to a headers-install step.


2. The ~/Sources/bootstrap experiment (Aug 2026): pnut replaces mes #

The last time the repo tree was assembled to go hex0 -> Go-shaped C compiler directly, it was this standalone repo (~/Sources/bootstrap), which is the "live-bootstrap exact setup (pnut laurent branch + TCC 0.9.26)" per its history. It ran 61 steps in one bootstrap.sh / Containerfile, entirely from the hex0-seed, ending in a working tcc-pnut -version (HEAD commit, 2026-08-09). Deliberately no stage1, no bash, no GCC, no mes-the-interpreter.

The key moves (from bootstrap.sh steps 56-61):

  1. M2-Planet compiles pnut.c itself (flags: -Dtarget_i386_linux -DNO_TERNARY_SUPPORT -DONE_PASS_GENERATOR -DSMALL_HEAP, --expand-includes), linked with blood-elf/M1/hex2 into pnut.
  2. pnut pnut.c -D BOOTSTRAP_TCC -o pnut-for-tcc, a self-compile that makes long 32-bit TCC-compatible.
  3. pnut-for-tcc compiles tcc 0.9.26 (tcc.c + pnut's portable_libc/libc.c) with -D BOOTSTRAP=1 -D HAVE_LONG_LONG=0 -D TCC_TARGET_I386=1 -D CONFIG_TCCBOOT=1 -D CONFIG_TCC_STATIC=1 -D CONFIG_USE_LIBGCC=1 -D TCC_VERSION="0.9.26" -D ONE_SOURCE=1, producing tcc-pnut.

The experiment pins tcc 0.9.26 (git subtree of the 0.9.27 tree was replaced: "Remove TCC 0.9.27, will replace with 0.9.26"), matching live-bootstrap's vendored tcc-0.9.26-1147-gee75a10c tarball.

This is the shortest hex0-to-cc path ever run end-to-end in our tree: 61 commands, one FROM scratch container, zero system binaries.


3. The shortest build path, hex0 -> Go #

Combining the two lines of evidence, the shortest known path to a working Go compiler today is the pnut-shortened stage1 plus the core/go ladder. Stated as a chain (each arrow = "compiles/builds"):

hex0-seed (181 bytes)
  -> hex0 -> hex1 -> hex2-0 -> catm -> M0 -> cc_x86 -> M2
  -> M2 bootstraps itself twice into M2-Planet (with blood-elf-0, M1-0, hex2-1, hex2)
  -> stage0 utils: kaem, M2-Mesoplanet, sha256sum, untar, ungz, unbz2, unxz, ...

Divergence A (stagex current, mes lineage) - the shortest shipped path:

M2-Planet -> mes-0.27 -> tcc-0.9.26 -> tcc-0.9.27 (pass1)
  -> coreutils-5.0, sed, bzip2, bash-2.05b, etc via tcc/mes
  -> tcc-0.9.27 pass2/3/4 against musl-1.1.24, ... pass5
  -> tcc pass5 -> go1.4 -> go1.19.11 -> go1.20.6 -> go1.22.12 -> go1.24.6 -> go1.26
  -> (stage3/core) cross to target arch, verify vs official tarball

Divergence B (shortest known, pnut lineage) - removes mes + nyacc + the whole pass1.kaem interpreter dance from the critical path:

M2-Planet -> pnut -> pnut (self, -DBOOTSTRAP_TCC) -> tcc-0.9.26 (BOOTSTRAP, i386, static)
  -> tcc builds: busybox/bash + musl (needed by Go's make.bash)
  -> tcc -> go1.4 -> [Go self-hosting ladder] -> current Go

Why B is shortest, concretely:

  • Compiler steps before cc_x86 are irreducible. hex0 through M2 is the floor: ~15 commands, no source is compiled until M2 exists.
  • pnut cuts the C-compiler bootstrap from 3 interpreters to 0. mes lineage: M2-Planet compiles ~1000 LOC of C, which interprets Scheme to generate the libc that compiles tcc-0.9.26. pnut lineage: M2-Planet compiles pnut.c, pnut compiles pnut, pnut compiles tcc. Two more compiles, zero interpreters, no nyacc, no Guile-load-path ceremony, and stage1's own comment about mes's per-file re-load cost disappears.
  • go-1.4 is the only Go build that needs anything but Go. It needs a C compiler (any of the tccs will do; 32-bit x86 keeps pnut/tcc-0.9.26 viable) plus a POSIX shell. Everything after 1.4 is GOROOT_BOOTSTRAP chaining.
  • The Go ladder rungs are forced by Go itself: 1.4 is the last C-based release; 1.20 is the last that can compile 1.22's requirements, etc. stagex pins 1.4 -> 1.19.11 -> 1.20.6 -> 1.22.12 -> 1.24.6 -> current (the "+2 steps" rule in the core/go comments). You cannot meaningfully shorten this without upstream Go changing its bootstrap policy.

Minimum artifact inventory for divergence B:

Stage Artifacts Trust floor
0 hex0-seed (181 B, in-repo blob) the only seed
0 stage0-posix-x86, M2-Planet, M2-Mesoplanet, M2libc, mescc-tools(-extra) hash-pinned in package.toml
1 pnut branch (laurent/small-fixes-for-TCC), portable_libc, tcc-0.9.26-1147-gee75a10c hash-pinned
2 musl, bash (or busybox ash) built by tcc-pnut
3 go1.4 bootstrap tarball (go1.4-bootstrap-20171003) hash-pinned
4 go1.19.11, 1.20.6, 1.22.12, 1.24.6, current src hash-pinned

Fail-closed properties worth keeping: every source is sha256-pinned; stage0 is platform-forced linux/386; core/go re-hashes against the official Go release as a cross-check witness; the whole chain is reproducible under SOURCE_DATE_EPOCH=0-style timestamp clamping (the "deterministic stage0" / "stage1 is deterministic again" lineage of fixes).

4. Recommendation #

If the goal is the shortest reproducible hex0->Go path in stagex: land the equivalent of live-bootstrap PR 554 (pnut-exe for tcc-0.9.26) into our stage1, delete mes-0.27's build step (keep headers), and let core/go keep its existing ladder. That yields a hex0-to-Go chain with exactly two C compilers ever built (pnut's tcc, then Go's own), one seed binary, and no interpreter on the critical path. The August 2026 bootstrap repo proves steps 1-61 of that path work as-is; the untested seam is only tcc-0.9.26 -> musl/bash -> go-1.4 (previously demonstrated via tcc-0.9.27 in stagex, not yet via pnut-built tcc-0.9.26).