#!/bin/bash # Phase 0: build core (non-PIC reactor, libc statically linked, all-exported) # and unit (-fPIC, wasm-ld -shared, dylink.0) with wasi-sdk. # Runs on k-uce. Artifacts go to /tmp/uce/wasm-phase0. set -e cd "$(dirname "$0")" SDK=/opt/wasi-sdk OUT=/tmp/uce/wasm-phase0 mkdir -p "$OUT" # Core module: reactor (no main, exports _initialize), exceptions off per # WASM-PROPOSAL §11.1 (error codes at unit boundaries). # --export-all so unit modules can import libc/libc++/core symbols from it; # --import-table so the host creates the shared funcref table (sized with # headroom up front — WAMR cannot grow or write tables from the host side); # __stack_pointer exported because PIC side modules import it (dylink ABI). "$SDK/bin/clang++" --target=wasm32-wasip1 -mexec-model=reactor \ -O1 -fno-exceptions \ core.cpp -o "$OUT/core.wasm" \ -Wl,--export-all \ -Wl,--import-table \ -Wl,--export=__stack_pointer \ -Wl,--export=__heap_base \ -Wl,--undefined=_ZTVN10__cxxabiv117__class_type_infoE # Unit module: PIC object, linked -shared with no libc/libc++ of its own — # every undefined symbol must become an import (resolved from core by the # loader). Inline/template code (std::string etc.) instantiates locally, # which is fine; the allocator must NOT be defined here (§3.2). # -fvisibility-inlines-hidden: vague-linkage code (templates, lambdas) # binds locally instead of being interposable — without it, one libc++ # tree-emplace lambda became a self-import the loader cannot satisfy. "$SDK/bin/clang++" --target=wasm32-wasip1 -fPIC -fvisibility=default \ -fvisibility-inlines-hidden \ -O1 -fno-exceptions \ -c unit.cpp -o "$OUT/unit.o" # --Bsymbolic: bind weak/vague-linkage symbols the unit defines itself # (template instantiations, lambdas) locally instead of emitting # self-imports that the loader would have to lazy-bind. "$SDK/bin/wasm-ld" -shared --experimental-pic \ --unresolved-symbols=import-dynamic \ --Bsymbolic \ "$OUT/unit.o" -o "$OUT/unit.wasm" \ --export=uce_unit_render echo "--- core.wasm ---" ls -la "$OUT/core.wasm" echo "--- unit.wasm ---" ls -la "$OUT/unit.wasm"