Spike: build a static ELF via wasm2elf with WAT syscall shims

Add elf/ (flatten.py, shim.wat, wasm2elf.patch pinned to wasm2elf-mb rev) and
just elf / test-elf recipes. The ELF handles plain HTTP, DNS, files and stdin;
26/27 repo tests pass, https:// is unsupported (no TLS).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-21 18:01:13 -08:00
1 parent ed06fa994a
commit 27ce306c7c
7 files changed
+770 -1

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""Flatten curl.wat's component (mem module + curl core module + canon-lowered wasi
imports) into ONE plain core module for wasm2elf. The lowered `wasi` imports are replaced
by the WAT shims in shim.wat, which talk to Linux through the `sys` host intrinsics
that elf/wasm2elf.patch adds to wasm2elf.
usage: flatten.py curl.wat shim.wat out.wat
"""
import re, sys
src = open(sys.argv[1]).read().split("\n")
shim = open(sys.argv[2]).read()
def find(pred, start=0):
for i in range(start, len(src)):
if pred(src[i]):
return i
raise SystemExit("marker not found")
# mem module body: from the realloc func to the line before the closing paren of $mem_mod
m0 = find(lambda l: '(func $realloc (export "realloc")' in l)
m1 = find(lambda l: l.startswith(" (core instance $mem_inst"), m0)
mem = src[m0:m1]
while mem and mem[-1].strip() == "":
mem.pop()
assert mem.pop() == " )" # closing paren of (core module $mem_mod
mem = [l.replace('(func (export "alloc")', "(func $alloc")
.replace('(func (export "heap_mark")', "(func $heap_mark")
.replace('(func (export "heap_reset")', "(func $heap_reset")
.replace('(export "realloc")', "") for l in mem]
# curl core module body: from its header to the line before the section-5 banner
c0 = find(lambda l: l.startswith(" (core module $curl"))
c1 = find(lambda l: "5. Instantiate Core $curl" in l, c0) - 1 # banner starts with a ;; ==== line
while src[c1].strip().startswith(";;") or src[c1].strip() == "":
c1 -= 1
body = src[c0:c1 + 1]
body.pop() # closing paren of (core module $curl
imports, rest = [], []
for l in body:
m = re.match(r'\s*\(import "wasi" "(\w+)" \(func (\$\w+) (.*)\)\)\s*$', l)
if m:
imports.append((m.group(1), m.group(2), m.group(3)))
elif re.match(r'\s*\(import "mem"', l) or l.startswith(" (core module"):
pass
else:
rest.append(l)
defined = set(re.findall(r"\(func (\$\w+)", shim))
out = [
"(module",
'(import "sys" "syscall" (func $syscall (param i64 i64 i64 i64 i64 i64 i64) (result i64)))',
'(import "sys" "membase" (func $membase (result i64)))',
'(import "sys" "argv_base" (func $argv_base (result i64)))',
'(import "sys" "peek64" (func $peek64 (param i64) (result i64)))',
'(import "sys" "strlen" (func $strlen (param i64) (result i64)))',
'(import "sys" "copy_in" (func $copy_in (param i32 i64 i32)))',
'(memory (export "memory") 1024)',
"(global $heap (mut i32) (i32.const 65536))",
]
out += mem
for name, fid, sig in imports:
if fid not in defined:
out.append(f"(func {fid} {sig} unreachable)")
out.append(shim)
out += rest
out.append(")")
open(sys.argv[3], "w").write("\n".join(out) + "\n")