#!/usr/bin/env bash # wasmcc-glibc: the glibc analogue of wasmcc (HWJS-B-2). A full cross C-compiler # driver that compiles/links bash, coreutils, /init and conftests against the # wasm32 GLIBC port (brintos/glibc branch wasm32) on the brintium coro arm. # # Differs from the musl wasmcc only in the libc it targets: # - headers : the durable glibc sysroot (make install-headers; §B-2 rig) # - target : wasm32-hwjs-linux-gnu on COMPILE (glibc headers need __linux__) # - startfile: crt1_glibc_hwjs.bc (the glibc-aware coro _start) # - libc : the glibc coro BITCODE pool (WASMLD_HWJS_LIBC_BC) + the native # .S objects archive (libglibc_native.a) — NOT musl-bc / -lc # - plugin : the submodule-derived HwjsCoroutinize.so (provenance-checked # against this checkout's HEAD; see below) # # COMPILE emits LLVM bitcode (the HwjsCoroutinize pass runs once over the merged # module in wasmld). LINK injects the glibc coro startfiles + drives wasmld with # the glibc pool. Assembly (.s/.S) has no IR form -> native compile. NEVER edits # bash/coreutils/glibc source — it only wires the toolchain. # A caller's exported POSIXLY_CORRECT (gnulib's getopt check) must not disable <(...). set +o posix set -euo pipefail # A C++ driver that execs this one hands over its stage directory, which holds # objects on this line, in BRINTIUM_CXX_STAGE; every EXIT trap here removes it. hwjs_owned_stage="${BRINTIUM_CXX_STAGE:-}" unset BRINTIUM_CXX_STAGE [[ -d "$hwjs_owned_stage" && "${hwjs_owned_stage##*/}" == wasmcxx-*stage.* ]] || hwjs_owned_stage="" hwjs_exit_trap() { trap "$1; [[ -z \"\$hwjs_owned_stage\" ]] || rm -rf \"\$hwjs_owned_stage\"" EXIT; } hwjs_exit_trap : # PATH PROBES # # dirname and basename in the shell. path_dirname_v and path_basename_v answer # in $PATH_DIRNAME and $PATH_BASENAME what "$(dirname -- "$1")" and # "$(basename -- "$1")" hold, the trailing newlines the substitution strips # included, and start no process. The same block is in bin/brintcc, # libexec/config.sh, libexec/brintcc-glibc and libexec/brintld. path_dirname_v() { local _pd="$1" _pd="${_pd%"${_pd##*[!/]}"}" if [[ -z "$_pd" ]]; then if [[ -n "$1" ]]; then PATH_DIRNAME=/; else PATH_DIRNAME=.; fi elif [[ "$_pd" != */* ]]; then PATH_DIRNAME=. else _pd="${_pd%/*}" _pd="${_pd%"${_pd##*[!/]}"}" PATH_DIRNAME="${_pd:-/}" fi PATH_DIRNAME="${PATH_DIRNAME%"${PATH_DIRNAME##*[!$'\n']}"}" } path_basename_v() { local _pb="$1" _pb="${_pb%"${_pb##*[!/]}"}" if [[ -n "$_pb" ]]; then PATH_BASENAME="${_pb##*/}" elif [[ -n "$1" ]]; then PATH_BASENAME=/ else PATH_BASENAME= fi PATH_BASENAME="${PATH_BASENAME%"${PATH_BASENAME##*[!$'\n']}"}" } # End of the path probes. path_dirname_v "${BASH_SOURCE[0]}" HWJS_HOME="$(cd "$PATH_DIRNAME/.." && pwd)" # shellcheck source=libexec/find-llvm.sh source "$HWJS_HOME/libexec/find-llvm.sh" # shellcheck source=libexec/line-kind.sh source "$HWJS_HOME/libexec/line-kind.sh" # shellcheck source=libexec/link-direct.sh source "$HWJS_HOME/libexec/link-direct.sh" # shellcheck source=libexec/installed-rt.sh source "$HWJS_HOME/libexec/installed-rt.sh" source "$HWJS_HOME/libexec/link-facts.sh" # shellcheck source=libexec/ld-script.sh source "$HWJS_HOME/libexec/ld-script.sh" # A caller's arguments may arrive as @file (CMake and Meson write one for a long # line). Everything below decides from the arguments what kind of line this # is, so the line is expanded before the first reader. What is handed on keeps # the caller's @file (libexec/response-file.sh): a compile gives clang # CALLER_ARGS, which reads the file itself, and a link, whose line is rewritten # here, reaches the linker as a response file of its own (HWJS_LINK_RSP, # libexec/link-direct.sh). # shellcheck source=libexec/response-file.sh source "$HWJS_HOME/libexec/response-file.sh" RSP_TOOL=wasmcc-glibc CALLER_ARGS=( "$@" ) HWJS_LINK_RSP=0 rsp_expand_line "$@" if (( RSP_FILES > 0 )); then set -- ${RSP_EXPANDED[@]+"${RSP_EXPANDED[@]}"} HWJS_LINK_RSP=1 fi # ── BrintOS wasm target triples (hwjs vendor: the prescriptive coroutinized # platform). COMPILE uses a *-linux-gnu triple so glibc's headers see __linux__; # clang's DRIVER can only LINK a wasm32-*-unknown triple, so LINK uses -unknown. COMPILE_TARGET="--target=wasm32-hwjs-linux-gnu" LINK_TARGET="--target=wasm32-hwjs-unknown" # Tell wasmld (reached via -fuse-ld below) which COMPILE triple the program # bitcode carries, so the helper bitcode it synthesizes (abi_keepalive/main_bridge) # is stamped with the SAME -gnu triple and llvm-link sees one triple — no "Linking # two modules of different target triples" warning. export WASMLD_HWJS_COMPILE_TARGET="${WASMLD_HWJS_COMPILE_TARGET:-wasm32-hwjs-linux-gnu}" # PIC MODEL (glibc arm): unlike the musl arm, the glibc port has ALWAYS built PIC # — its pool/program/crt were compiled with a *-linux triple (wasm32-unknown-linux # -> wasm32-hwjs-linux-gnu, both PIC-default) and the wasm32 glibc userland is # proven booting that way. Keep PIC here (do NOT add -fno-pic like wasmcc's musl # arm) so program TUs match the PIC glibc bitcode pool built by ccw-glibc. CORO_FEATURES=( -matomics -mbulk-memory -mmutable-globals -msign-ext -mnontrapping-fptoint -msimd128 ) # The rt-import publication seed's command line. A complete one: an installed # clang's config file holds a per-file compile's defaults, which are not this # seed's. The seed includes nothing, so it needs no include directory. # sysroot/rt-import-seed.sh compiles the installed seed from --print-seed-flags, # and a link takes that seed only while these flags are still the same. SEED_FLAGS=( --no-default-config $COMPILE_TARGET -nostdlibinc "${CORO_FEATURES[@]}" -fno-builtin -fvisibility=default ) # gcc answers -print-prog-name=ld with the linker it was configured with; clang # echoes back a bare `ld` it could not find, which is not a program on this # target. Answer with the linker that links for this driver (libexec/config.sh). for a in "$@"; do case "$a" in -print-prog-name=ld|--print-prog-name=ld) _print_ld="${BRINTIUM_LD:-$HWJS_HOME/bin/brintld}" [[ -x "$_print_ld" && ! -d "$_print_ld" ]] || { echo "wasmcc-glibc: FATAL: -print-prog-name=ld: BRINTIUM_LD='$_print_ld' is not an executable file. It is the linker this driver names to a build system: brintld, or a wrapper that sources its environment first." >&2 exit 1 } printf '%s\n' "$_print_ld" exit 0 ;; --print-seed-flags) printf '%s\n' "${SEED_FLAGS[@]}" exit 0 ;; esac done # The glibc sysroot, bitcode pool and startfiles all live under one tree, and # that tree is the sysroot directory of this checkout. The default used to # name a shared directory inside a distribution of the repository this # toolchain was a submodule of; it names nothing here. bin/brintcc resolves # and exports the pointer for every real invocation, so what changes is what a # direct call gets. No machine-specific absolute path. TOOLS_DIR="${HWJS_TOOLS_DIR:-${BRINTIUM_SYSROOT:-$HWJS_HOME/sysroot}}" HWJS_LLVM_BIN="$(hwjs_llvm_bin "$HWJS_HOME/libexec")" \ || { echo "wasmcc-glibc: FATAL: pinned LLVM not found (set WASMLD_HWJS_LLVM_BIN, or build it per toolchain/llvm-from-source.md)." >&2; exit 1; } CLANG="$HWJS_LLVM_BIN/clang" WASMLD="${HWJS_WASMLD:-$HWJS_HOME/libexec/brintld}" # ---- durable glibc libc (B-2 rig; override via env) ------------------------ GLIBC_BUILD="${GLIBC_BUILD:-$TOOLS_DIR/build/glibc-2.43-build}" GLIBC_SYSINC="${GLIBC_SYSINC:-$GLIBC_BUILD/sysroot/usr/include}" # Linux UAPI kernel headers (linux/*.h, asm/*.h) — glibc's headers (bits/ # local_lim.h -> linux/limits.h, etc.) include them; the same dir glibc # configure used as --with-headers. This arm puts them on EVERY command line # with -isystem, so an absent pointer is a refusal here. # # THERE IS NO DEFAULT, and there was one for years: a path into one particular # distribution's build tree, in a repository this toolchain no longer sits in. # It had already stopped being where the headers were installed before that, # and nothing failed, because clang ignores an -isystem naming a directory that # is not there — so the skew showed up as a missing linux/*.h in a glibc # compile with no hint that the wrong tree had been named. A guessed default is # also a guess at WHICH kernel, and a userland compiled against one kernel's # ABI for a guest that runs on another fails at a system call, not at a build # step. bin/brintcc refuses before it gets here; this covers a direct call. GLIBC_UAPI_INC="${GLIBC_UAPI_INC:-${BRINTIUM_KERNEL_HEADERS:-}}" [ -n "$GLIBC_UAPI_INC" ] || { echo "wasmcc-glibc: FATAL: neither BRINTIUM_KERNEL_HEADERS nor GLIBC_UAPI_INC is set, and this arm compiles against the kernel UAPI headers (linux/*.h, asm/*.h) on every line. They are installed by the kernel build, not by this toolchain, so there is nothing to fall back to. Set BRINTIUM_KERNEL_HEADERS to the installed UAPI include directory — the one holding linux/ and asm/." >&2 exit 1 } [ -d "$GLIBC_UAPI_INC" ] || { echo "wasmcc-glibc: FATAL: the kernel UAPI header directory '$GLIBC_UAPI_INC' does not exist. It must be the kernel build's installed UAPI include directory (the one holding linux/ and asm/), not a kernel source tree and not the install prefix above it." >&2 exit 1 } GLIBC_BC_POOL="${GLIBC_BC_POOL:-$GLIBC_BUILD/glibc-bc}" GLIBC_NATIVE_A="${GLIBC_NATIVE_A:-$GLIBC_BUILD/lib/libglibc_native.a}" # STATUS-CHANNEL crt selection (drive_abi=4; STATUS-CHANNEL-CONTRACT §10 crt # bullet — the glibc twin of libexec/brintcc-musl's selection): the SAME value-keyed # knob that selects abi-4 codegen selects the §4 terminus crt. An explicit # GLIBC_CRT override still wins. case "${HWJS_STATUS_CHANNEL:-}" in ''|0|1) ;; *) echo "wasmcc-glibc: FATAL: HWJS_STATUS_CHANNEL='${HWJS_STATUS_CHANNEL}' — this knob is VALUE-keyed (1 = on, 0 = off, unset/empty = default); refusing to guess (linux-in-the-browser#41)." >&2 exit 1 ;; esac # LANE-AWARE CHANNEL ADDRESSING (PLAN-THREADS-SINGLE-MEMORY §4 step 2) — the # glibc twin of libexec/brintcc-musl's guard: garbage HWJS_RT_LANES values and # lanes>1-without-the-channel refuse here too. The kernel packaging ships # the installed runtime LANED by default now (§4 step 4); a lanes>1 link # verifies that posture against the artifact's own symbol surface at the # link vantage below (a single-lane rebuild would alias other RT data # silently for every lane>=1, I3-2). The crt needs no laned twin (bitcode; # it inherits lane addressing in the merged pass run). WACKY_HWJS_RT (a # caller-built WIP RT) is the sanctioned escape, same as libexec/brintcc-musl. case "${HWJS_RT_LANES:-}" in ''|0|1) ;; *) if ! [[ "${HWJS_RT_LANES}" =~ ^[0-9]+$ && "${HWJS_RT_LANES}" -ge 2 && "${HWJS_RT_LANES}" -le 32 ]]; then echo "wasmcc-glibc: FATAL: HWJS_RT_LANES='${HWJS_RT_LANES}' — this knob is the lane COUNT: an integer 0..32 (unset/0/1 = single-lane; 2..32 = lane-aware channel addressing). Refusing to guess (PLAN-THREADS-SINGLE-MEMORY §4 step 2)." >&2 exit 1 fi if [[ "${HWJS_STATUS_CHANNEL:-0}" != "1" ]]; then echo "wasmcc-glibc: FATAL: HWJS_RT_LANES>1 without HWJS_STATUS_CHANNEL=1 — lane-aware addressing is a property of the status channel (drive_abi=4). Set both or neither (PLAN-THREADS-SINGLE-MEMORY §4 step 2)." >&2 exit 1 fi # Refusal (b) is LINK-vantage-only (a -c compile injects no RT): # recorded here, enforced below once is_link_mode is known. HWJS_LANES_LINK_GUARD=1 ;; esac if [[ -n "${GLIBC_CRT:-}" ]]; then hwjs_note "wasmcc-glibc: NOTE (CRT-PROVENANCE): explicit GLIBC_CRT override ($GLIBC_CRT) — WIP crt, provenance UNVERIFIED" elif [[ "${HWJS_STATUS_CHANNEL:-0}" == "1" ]]; then GLIBC_CRT="$GLIBC_BUILD/startfiles/crt1_glibc_hwjs_status.bc" else GLIBC_CRT="$GLIBC_BUILD/startfiles/crt1_glibc_hwjs.bc" fi # CRT-PROVENANCE (2026-07-21; glibc twin of libexec/brintcc-musl's check — the RT had # an attestation gate, the crt did not; a stale §4 terminus links silently). # Producer: build-glibc-libc.sh writes the sidecar. Verified whenever the # glibc source tree is present; explicit GLIBC_CRT override skips (noted). # # THE SOURCE IS RESOLVED FROM THIS CHECKOUT, for the reason written out at the # same function in libexec/brintcc-musl: it used to be spelled as a package # subpath under a super-repository, that path names nothing here, and "source # absent" meant "check skipped" — so the gate was off entirely and silently. # glibc is a submodule of this repository; BRINTIUM_GLIBC_SRC moves it. crt_provenance_verify() { [[ "$GLIBC_CRT" == "$GLIBC_BUILD/startfiles/"crt1_glibc_hwjs*.bc ]] || return 0 local glibc_src="${BRINTIUM_GLIBC_SRC:-$HWJS_HOME/glibc}" local crt_src="$glibc_src/sysdeps/wasm32/crt1-hwjs.c" if [[ ! -f "$crt_src" ]]; then hwjs_note "wasmcc-glibc: NOTE (CRT-PROVENANCE): NOT CHECKED — no glibc crt source at $crt_src, so the installed crt cannot be tied to any source. Point BRINTIUM_GLIBC_SRC at the glibc checkout, or run 'git submodule update --init glibc'." return 0 fi local crt_stamp="$GLIBC_CRT.stamp" if [[ ! -f "$crt_stamp" ]]; then echo "wasmcc-glibc: FATAL (CRT-PROVENANCE): no attestation sidecar at $crt_stamp — the installed crt cannot be tied to the checked-out glibc crt source. Rebuild: 'make -C sysroot glibc-libc'." >&2 exit 1 fi local crt_key crt_line crt_key="$(sha256sum "$crt_src" | cut -d' ' -f1)" crt_line="$(cat "$crt_stamp")" if [[ "$crt_line" != "CRT-PROVENANCE src=$crt_key" ]]; then echo "wasmcc-glibc: FATAL (CRT-PROVENANCE): the INSTALLED crt is STALE: $crt_stamp attests '$crt_line' but the checked-out crt source ($crt_src) keys to src=$crt_key — rebuild via 'make -C sysroot glibc-libc'. Refusing at the injection vantage." >&2 exit 1 fi } # ---- coro substrate startfiles (shared with musl) -------------------------- # PLAN-HWJS-RT-PSABI R3: the runtime TU is KERNEL-owned; the default-path # artifact must carry a verified RT-PROVENANCE sidecar (same contract as # libexec/brintcc-musl — see hwjs_rt_verify there). Explicit WACKY_HWJS_RT override # = WIP-runtime dev knob, honored with a loud NOTE. if [[ -n "${WACKY_HWJS_RT:-}" ]]; then hwjs_note "wasmcc-glibc: NOTE (RT-PROVENANCE): explicit WACKY_HWJS_RT override ($WACKY_HWJS_RT) — WIP runtime, provenance UNVERIFIED" HWJS_RT="$WACKY_HWJS_RT" HWJS_RT_VERIFY=0 else HWJS_RT="$TOOLS_DIR/build/hwjs_user_rt.bc" HWJS_RT_VERIFY=1 fi # NARROWED KEY (2026-07-21): verified against the RT-input srckey (the # ONE shared definition, linux scripts/hwjs-rt-srckey.sh), not the whole # linux HEAD — mirror of libexec/brintcc-musl hwjs_rt_verify, see the rationale # there. hwjs_rt_verify() { local rt="$1" stamp="$1.stamp" # An installed tree has no kernel checkout; its installer's record of the # runtime stands in for one (libexec/installed-rt.sh). if [[ -z "${BRINTIUM_KERNEL_SRC:-}" ]] \ && hwjs_rt_installed_record_v "$HWJS_HOME/libexec/hwjs-installed.stamp"; then hwjs_rt_installed_verify wasmcc-glibc "$rt" "$HWJS_HOME/libexec/hwjs-installed.stamp" return 0 fi # Same pointer, TAKEN AND NOT COMPUTED, and the same situations, as # libexec/brintcc-musl — see the note there for what the removed fallback # cost. local linux_src="${BRINTIUM_KERNEL_SRC:-}" [[ -n "$linux_src" ]] || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): BRINTIUM_KERNEL_SRC is not set, and this link injects the kernel-owned runtime $rt, whose attestation can only be re-keyed by running the kernel's own scripts/hwjs-rt-srckey.sh over the kernel's own sources. This toolchain has no kernel of its own and there is nothing here to guess from. Set BRINTIUM_KERNEL_SRC to the kernel checkout that produced this runtime, or set WACKY_HWJS_RT to take the artifact verbatim, which is honoured and says on every link that its provenance is unverified." >&2 exit 1 } [[ -f "$stamp" ]] || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): no attestation sidecar at $stamp — the kernel owns hwjs_user_rt and attests it at the kernel link; rebuild it there (or override WACKY_HWJS_RT explicitly for a work-in-progress runtime)" >&2 exit 1 } [[ -d "$linux_src" ]] || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): BRINTIUM_KERNEL_SRC='$linux_src' is not a directory, so the attestation beside $rt cannot be re-keyed against the kernel it claims to come from. Point BRINTIUM_KERNEL_SRC at the kernel checkout that produced this runtime, or set WACKY_HWJS_RT to take the artifact verbatim — which is honoured, and says on every link that its provenance is unverified." >&2 exit 1 } local line; line="$(cat "$stamp")" local keyscript="$linux_src/scripts/hwjs-rt-srckey.sh" [[ -x "$keyscript" ]] || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): $keyscript is missing or not executable, and it is the one definition of the key this sidecar is written against. Either BRINTIUM_KERNEL_SRC='$linux_src' is not a kernel checkout, or it sits on a commit that predates the script; move it to one that carries it." >&2 exit 1 } local out; out="$("$keyscript" "$linux_src")" || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): hwjs-rt-srckey.sh failed for $linux_src — cannot verify $stamp" >&2 exit 1 } local key="${out%% *}" dirt="${out##* }" [[ "$dirt" == "0" ]] || { echo "wasmcc-glibc: FATAL (RT-PROVENANCE): the LINUX CHECKOUT carries uncommitted edits to the RT inputs (git -C $linux_src status -- arch/wasm32/vdso scripts/build-hwjs-rt.sh scripts/wasm-add-custom-section.py scripts/hwjs-rt-srckey.sh) — no attestation can cover uncommitted inputs; commit or revert, then 'make kernel FORCE=1'. (Pathspec-limited: edits elsewhere in the linux tree do NOT trip this.)" >&2 exit 1 } if [[ ! "$line" =~ ^RT-PROVENANCE\ commit=[0-9a-f]{40}\ dirty=0\ src=[0-9a-f]{64}\ srckey=$key$ ]]; then if [[ "$line" =~ \ srckey=[0-9a-f]{64}$ ]]; then echo "wasmcc-glibc: FATAL (RT-PROVENANCE): the INSTALLED RUNTIME ARTIFACT is STALE (or attests dirty RT inputs): $stamp attests '$line' but the checked-out linux RT inputs key to srckey=$key — re-attest via 'make kernel FORCE=1' then 'make -C sysroot hwjs-startfiles'. Refusing at the injection vantage." >&2 else echo "wasmcc-glibc: FATAL (RT-PROVENANCE): the INSTALLED RUNTIME ARTIFACT predates the narrowed RT-provenance key (no srckey= field): $stamp attests '$line' — the PRODUCER side is stale; rebuild via 'make kernel FORCE=1' then 'make -C sysroot hwjs-startfiles'. Refusing at the injection vantage." >&2 fi exit 1 fi } # The entry joins the program's merge, so it is this driver's own build of it # (sysroot/startfiles.mk), stamped with the compile triple below; the musl # driver's hwjs_user_entry.bc carries the musl triple. HWJS_ENTRY="${WACKY_HWJS_ENTRY:-$TOOLS_DIR/build/hwjs_user_entry-glibc.bc}" HWJS_THUNKS="${WACKY_HWJS_THUNKS:-$TOOLS_DIR/build/hwjs_user_thunks.o}" HWJS_WTHR="${WACKY_HWJS_WTHR:-$TOOLS_DIR/build/wasm_thread.k7hwjs.o}" BUILTINS="${WACKY_BUILTINS:-$TOOLS_DIR/build/libclang_rt.builtins-wasm32.a}" # _Unwind_Backtrace and _Unwind_GetIP as weak stubs that find no frame (sysroot/ # builtins.mk). libc.so keeps its copies hidden, so every program and side module # links this archive last, as gcc links libgcc_eh. Every name it defines only # pretends to work, so a configure link probe on the fast path below must answer # no for it and the package keep its fallback: the probe allowlist drops them. UNWIND_STUBS="${WACKY_UNWIND_STUBS:-$TOOLS_DIR/build/libbrint_unwind_stubs.a}" UNWIND_A=() [[ -f "$UNWIND_STUBS" ]] && UNWIND_A=( "$UNWIND_STUBS" ) # ---- the tree this driver's linker must consume --------------------------- # NO PLUGIN PATH IS NAMED HERE. This driver used to decide the plugin home # itself, and so did half a dozen other files, each differently: this one # listed three plugins, one sibling listed none, and a fourth plugin was in # nobody's list at all. The linker derives the plugin home from its own # location now, and takes the libc pool, the startfiles and the builtins from # HWJS_TOOLS_DIR — which is the same pointer TOOLS_DIR above already resolved. # Explicit WASMLD_HWJS_*_PLUGIN / WASMLD_HWJS_SRC_DIR still win. # # THE EXPORT IS UNCONDITIONAL. It used to be guarded by "does a super-repo # layout exist", which is a question about a directory arrangement this # toolchain no longer has — so the guard is now always false, and the linker # would fall back to its own default for the pool while this driver resolved # the headers and startfiles from TOOLS_DIR. Two answers to one question is # the whole failure this pointer exists to remove: a link whose headers come # from one libc tree and whose bitcode comes from another produces an image # that loads and runs until it doesn't. Whatever tree this driver reads, its # linker reads. export WASMLD_HWJS=1 export HWJS_TOOLS_DIR="$TOOLS_DIR" # The compiler-rt bitcode TU a link merges, and the object beside it that a # program link takes instead (libexec/brintld, THE BUILTINS OBJECT): the build # for this driver's triple, so that a glibc link merges no musl module. export WASMLD_HWJS_LIBC_BUILTINS_BC="${WASMLD_HWJS_LIBC_BUILTINS_BC:-$TOOLS_DIR/build/builtins-wasm32-gnu-pic.bc}" export WASMLD_HWJS_LIBC_BC="${WASMLD_HWJS_LIBC_BC:-$GLIBC_BC_POOL}" # B-1.e loader decouple DCE (prunes runtime-dead ELF loader/locale) + keep the # dlopen/fork host-ABI roots public (so the dynamic RED arm's _Fork etc. export). export WASMLD_HWJS_DCE_MERGE="${WASMLD_HWJS_DCE_MERGE:-1}" # _Fork: the dlopen/fork host-ABI root (RED arm). wcslen: bash/coreutils # address-take the WEAK alias `wcslen`->`__wcslen`; without it as a DCE root the # internalize+globaldce pre-pass drops the alias (keeping only local `__wcslen`), # so `wcslen` leaks as a missing env import at real-kernel instantiate time (the # kernel-less harness stopgapped it with a JS import; the real kernel does not). # bcmp: same shape as wcslen — it is a WEAK alias -> `memcmp` (string_memcmp.bc, # force-included above). clang lowers `memcmp(a,b,n)==0` to a `bcmp` libcall, so # every bash/coreutils TU references `bcmp`; force-including the TU pulls the weak # alias in, but the internalize+globaldce pre-pass drops it unless it is ALSO a # DCE root, so it must be listed here too (else it leaks env.bcmp at instantiate). export WASMLD_HWJS_DCE_EXTRA_ROOTS="${WASMLD_HWJS_DCE_EXTRA_ROOTS:-_Fork,wcslen,bcmp}" # DYNAMIC_MAIN default ON for glibc userland: bash/coreutils are dlopen-capable # dynamic mains (import the shared __indirect_function_table) so the B-2 RED arm # (a real .so loaded through _dl_open at runtime) is exercised. Both =0 is a # self-contained static image, and under the status channel a link with # WASMLD_LIBC_IMPORT=0 and neither knob set gets that (libexec/link-form.sh); the guest # ld wrapper (meta-brintos brintos-llvm-target-sysroot) takes the same default from that file. # shellcheck source=libexec/link-form.sh source "$HWJS_HOME/libexec/link-form.sh" brintium_dynamic_defaults "$@" static_line=0 brintium_line_is_static_link "$@" && static_line=1 # Under the status channel a dynamic main must consume libc.so's published partition, and the link # refuses one that does not. config.sh turns the channel on for both front doors, so a plain # `wasm32-hwjs-linux-gnu-gcc hello.c` was a fatal link. A caller that said nothing about the dynamic # libc gets the consumer arm the glibc rig spells out (DYNAMIC_LIBC_CONSUMER_ENV), against the # sysroot's own libc.so, when that libc.so has been built. if [[ "${HWJS_STATUS_CHANNEL:-0}" == "1" && -z "${WASMLD_LIBC_IMPORT:-}" && "$WASMLD_DYNAMIC_MAIN" == "1" \ && -f "$TOOLS_DIR/build/libc-glibc.so" && -f "$TOOLS_DIR/build/libc-glibc.so.partition" ]]; then export WASMLD_LIBC_IMPORT=1 WASMLD_RT_IMPORT="${WASMLD_RT_IMPORT:-1}" HWJS_EH_PRELOWER="${HWJS_EH_PRELOWER:-1}" export WASMLD_LIBC_SO_PATH="${WASMLD_LIBC_SO_PATH:-$TOOLS_DIR/build/libc-glibc.so}" export WASMLD_HWJS_XMOD_SUSPENDERS="${WASMLD_HWJS_XMOD_SUSPENDERS:-$TOOLS_DIR/build/libc-glibc.so.partition}" fi COMMON_INC=( -nostdlibinc -isystem "$GLIBC_SYSINC" -isystem "$GLIBC_UAPI_INC" ) # How the compile of a source on a link line treats one argument of that line. # Both such compiles use it, the single-step one and the conftest fast path's, so # the two drop the same link-only arguments. Sets link_arg_class: skip (drop it and # the word after it), opnd (keep it and the word after it), drop, or keep. link_arg_compile_class() { case "$1" in -o|-L|-Xlinker|-x|--language) link_arg_class=skip ;; -I|-D|-U|-include|-imacros|-isystem|-idirafter|-iquote|-Xclang) link_arg_class=opnd ;; -o*|-L*|-l*|-Wl,*|-x?*|--language=*|-shared|--shared) link_arg_class=drop ;; -*) link_arg_class=keep ;; *) link_arg_class=drop ;; esac } # ---- phase detection (mirror wasmcc) --------------------------------------- is_link_mode=1 has_asm_input=0 # A -shared link builds a SIDE MODULE, not a program image (see the injection # note at the link line below). # A FINISHED side module, by content -- never by name. readline's build names # its object files readline.so, vi_mode.so, funmap.so ...; those are bitcode, # not shared libraries, and dropping them leaves the -shared link with no # inputs at all ("no bitcode roots on the link line"). Test the wasm magic and # the dylink.0 section instead, which is the same question wasmld's own # classifier asks. _hwjs_is_wasm_module() { [[ -f "$1" ]] || return 1 [[ "$(head -c4 "$1" 2>/dev/null | od -An -tx1 | tr -d ' \n')" == "0061736d" ]] } _hwjs_is_side_module() { _hwjs_is_wasm_module "$1" || return 1 head -c 4096 "$1" 2>/dev/null | grep -qa 'dylink.0' } # The first lib.so on the -L path that is a side module, by the same test. _hwjs_ldirs_side_module() { local d for d in "${Ldirs[@]}"; do _hwjs_is_side_module "$d/lib$1.so" && { printf '%s\n' "$d/lib$1.so"; return 0; } done return 1 } # gcc links against the library dirs it prints for -print-search-dirs. Of # clang's, take only the multiarch ones, /lib/ and # /usr/lib/, in its order: the plain /lib and /usr/lib it prints on # a line without --sysroot are the build host's. Clang lists a multiarch dir only # when it exists, so with neither present clang is not run. # multiarch_ldirs_add : appends what lacks. multiarch_ldirs_add() { local -n _mla_dirs="$1" local root="${2%/}" triple="${COMPILE_TARGET#--target=}" out line="" d e local -a printed=() [[ -d "$root/lib/$triple" || -d "$root/usr/lib/$triple" ]] || return 0 out="$("$CLANG" $COMPILE_TARGET ${2:+"--sysroot=$2"} -print-search-dirs 2>/dev/null)" || return 0 while IFS= read -r line; do [[ "$line" == "libraries: ="* ]] && break; line=""; done <<< "$out" IFS=: read -ra printed <<< "${line#libraries: =}" for d in ${printed[@]+"${printed[@]}"}; do [[ "$d" == */lib/"$triple" ]] || continue for e in ${_mla_dirs[@]+"${_mla_dirs[@]}"}; do [[ "$e" == "$d" ]] && continue 2; done _mla_dirs+=( "$d" ) done return 0 } # hwjs-cc#121: a lib.so that is a GNU ld script (ncurses installs # `INPUT(libncurses.so.6 AS_NEEDED(-ltinfo))`) links what the script names, as # GNU ld does. A side module it names joins needed_sos like any -l hit, an # archive or object joins the line, a nested -l or script resolves the same way, # and glibc's own libraries are consumed (the pool is our libc). AS_NEEDED # members are recorded like the others: this linker records every -l it # resolves. The seen-set stops a cycle, the depth cap a runaway chain. declare -A _hwjs_ld_script_seen=() _hwjs_ld_script_expand() { #