Files
ww/Makefile
Hojun-Cho ecf0a84127 6l: dynamic linking with symbol versioning
Teach the linker to consume ET_DYN shared objects and emit a
dynamically-linked ELF executable. Snake et al. can now link
against libncurses + libc through the system dynamic loader.

Pipeline additions:

- dyn.c: read ET_DYN, parse .dynsym + DT_SONAME, walk
  .gnu.version_d / .gnu.version to learn each export's default
  version (skip hidden entries).
- pass.c: when an undefined sym is provided by some Lso,
  promote it to dynamic, assign a PLT slot, record the
  matched version on the Lsym.
- dynout.c: emit PT_INTERP + PT_DYNAMIC, .dynsym/.dynstr/.hash,
  .plt + .got.plt + .rela.plt, .gnu.version + .gnu.version_r,
  and the full DT_* set with DT_BIND_NOW. Patch PC32/PLT32
  references against dyn syms to point at their PLT stubs.
- main.c: -L<dir> and -l<name> flag parsing; resolve <name>
  via .so / .so.<N> / .a in libdir order, skipping GNU ld
  linker scripts (libc.so on most distros).
- ww driver: collect -l/-L (joined and split forms) and pass
  through to 6l.

Design choices:

- DT_BIND_NOW so the loader resolves all PLT slots at startup;
  no PLT0 lazy resolver stub.
- SysV .hash, not .gnu.hash. One bucket; loader scans the
  chain. Slow at scale, fine for snake-class binaries.
- Non-PIE at fixed 0x400000.
- No section headers — loader uses program headers, but
  readelf -V/-S won't display anything.

Symbol versioning is the only correctness item beyond the
basic PLT/GOT machinery: glibc symbols default to versions
later than GLIBC_2.2.5 (e.g. clock_gettime → GLIBC_2.17 for
the vDSO impl), and the loader rejects unversioned references
to those without a matching Vernaux entry.

test/wwc/810_dyn covers four cases: bare libc dyn call,
multi-PLT, clock_gettime versioning, and fn-pointer to FFI
binding (which exercises the codegen fixes from the parent
commit alongside the new linker path).
2026-05-11 09:42:27 +09:00

286 lines
11 KiB
Makefile

# ww — Plan 9-organised toolchain. POSIX make.
#
# Build a small static frontend library libwwc.a and the user-facing
# driver `ww`. Per-target tools (6c, 6a, 6l) and the runtime are added
# in their own phases. Everything lands under out/.
PREFIX ?= /usr/local
CC ?= cc
AR ?= ar
CFLAGS ?= -O0 -g -Wall -Wextra -Wpedantic -Wno-unused-parameter
CFLAGS += -std=c99 -fno-strict-aliasing -D_POSIX_C_SOURCE=200809L
INCS = -Icmd/wwc
OUT = out
BIN = $(OUT)/bin
LIB = $(OUT)/lib
OBJ = $(OUT)/obj
WWC_SRC = cmd/wwc/mem.c cmd/wwc/err.c cmd/wwc/tok.c cmd/wwc/lex.c \
cmd/wwc/ast.c cmd/wwc/parse.c cmd/wwc/sym.c cmd/wwc/type.c \
cmd/wwc/check.c
WWC_OBJ = $(WWC_SRC:cmd/wwc/%.c=$(OBJ)/wwc/%.o)
WW_SRC = cmd/ww/main.c
WW_OBJ = $(WW_SRC:cmd/ww/%.c=$(OBJ)/ww/%.o)
WD_SRC = cmd/wwdump/main.c
WD_OBJ = $(WD_SRC:cmd/wwdump/%.c=$(OBJ)/wwdump/%.o)
C6_SRC = cmd/6c/main.c cmd/6c/cgen.c cmd/6c/txt.c cmd/6c/swt.c \
cmd/6c/peep.c cmd/6c/reg.c
C6_OBJ = $(C6_SRC:cmd/6c/%.c=$(OBJ)/6c/%.o)
A6_SRC = cmd/6a/main.c cmd/6a/lex.c cmd/6a/parse.c cmd/6a/asm.c cmd/6a/obj.c
A6_OBJ = $(A6_SRC:cmd/6a/%.c=$(OBJ)/6a/%.o)
L6_SRC = cmd/6l/main.c cmd/6l/obj.c cmd/6l/sym.c cmd/6l/pass.c cmd/6l/out.c \
cmd/6l/dyn.c cmd/6l/dynout.c
L6_OBJ = $(L6_SRC:cmd/6l/%.c=$(OBJ)/6l/%.o)
RT_S = rt/start.s rt/syscall.s rt/alloc.s rt/streq.s rt/abort.s
RT_OBJ = $(RT_S:rt/%.s=$(OBJ)/rt/%.o)
# Cstage: one-time C bootstrap (see BOOTSTRAP.md). Built by `cc`.
# Goes away at v1.0, replaced by a checked-in stage-0 binary.
CSTAGE_BINS = $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l $(BIN)/wwdump
# Wwstage: ww-rewritten toolchain. Built by Cstage today; will be the
# only toolchain after Phase 10 closeout. Byte-identical to Cstage
# (tests 990-993).
WWSTAGE_BINS = $(BIN)/wwdump_ww $(BIN)/6a_ww $(BIN)/6l_ww $(BIN)/ww_ww
BINS = $(CSTAGE_BINS) $(WWSTAGE_BINS)
LIBS = $(LIB)/libwwc.a $(LIB)/libwwrt.a
all: cstage wwstage
cstage: $(CSTAGE_BINS) $(LIBS)
wwstage: $(WWSTAGE_BINS)
# ---- libwwc.a ----------------------------------------------------------
$(LIB)/libwwc.a: $(WWC_OBJ) | $(LIB)
$(AR) rcs $@ $(WWC_OBJ)
$(OBJ)/wwc/%.o: cmd/wwc/%.c cmd/wwc/ww.h | $(OBJ)/wwc
$(CC) $(CFLAGS) $(INCS) -c -o $@ $<
# ---- ww driver ---------------------------------------------------------
$(BIN)/ww: $(WW_OBJ) $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $(WW_OBJ) -L$(LIB) -lwwc
$(OBJ)/ww/%.o: cmd/ww/%.c cmd/wwc/ww.h | $(OBJ)/ww
$(CC) $(CFLAGS) $(INCS) -c -o $@ $<
# ---- wwdump (lex/AST diff anchor) --------------------------------------
$(BIN)/wwdump: $(WD_OBJ) $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $(WD_OBJ) -L$(LIB) -lwwc
$(OBJ)/wwdump/%.o: cmd/wwdump/%.c cmd/wwc/ww.h | $(OBJ)/wwdump
$(CC) $(CFLAGS) $(INCS) -c -o $@ $<
# ---- 6c amd64 compiler -------------------------------------------------
$(BIN)/6c: $(C6_OBJ) $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $(C6_OBJ) -L$(LIB) -lwwc
$(OBJ)/6c/%.o: cmd/6c/%.c cmd/6c/gc.h cmd/6c/6.out.h cmd/wwc/ww.h | $(OBJ)/6c
$(CC) $(CFLAGS) $(INCS) -Icmd/6c -c -o $@ $<
# ---- 6a amd64 assembler ------------------------------------------------
$(BIN)/6a: $(A6_OBJ) | $(BIN)
$(CC) $(CFLAGS) -o $@ $(A6_OBJ)
$(OBJ)/6a/%.o: cmd/6a/%.c cmd/6a/a.h cmd/6c/6.out.h | $(OBJ)/6a
$(CC) $(CFLAGS) -Icmd/6a -Icmd/6c -c -o $@ $<
# ---- 6l amd64 linker ---------------------------------------------------
$(BIN)/6l: $(L6_OBJ) | $(BIN)
$(CC) $(CFLAGS) -o $@ $(L6_OBJ)
$(OBJ)/6l/%.o: cmd/6l/%.c cmd/6l/l.h | $(OBJ)/6l
$(CC) $(CFLAGS) -Icmd/6l -c -o $@ $<
# ---- ww-side wwdump (the lexer port, exercised by 990_selfhost) --------
# Built via the user-facing ww driver, with -I selfhost/cmd/wwc so it
# can find the lex/tok/mem ports. Output named wwdump_ww to avoid
# colliding with the C-side wwdump in $(BIN).
$(BIN)/wwdump_ww: selfhost/cmd/wwdump/main.ww \
selfhost/cmd/wwc/lex.ww selfhost/cmd/wwc/tok.ww \
selfhost/cmd/wwc/mem.ww selfhost/cmd/wwc/ast.ww \
selfhost/cmd/wwc/parse.ww selfhost/cmd/wwc/typ.ww \
selfhost/cmd/wwc/sym.ww selfhost/cmd/wwc/check.ww \
selfhost/cmd/wwc/cgen.ww \
$(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a | $(BIN)
cd $(BIN) && ./ww build -I $$PWD/../../selfhost/cmd/wwc \
$$PWD/../../selfhost/cmd/wwdump/main.ww
mv $(BIN)/main $@
# ---- ww-side 6a (assembler port, exercised by 991_6a_ww) ---------------
# Built like wwdump_ww. Needs -I selfhost/cmd/6a for the local types/lex/
# parse/asm/obj modules and -I selfhost/cmd/wwc to find `mem`.
$(BIN)/6a_ww: selfhost/cmd/6a/main.ww selfhost/cmd/6a/types.ww \
selfhost/cmd/6a/lex.ww selfhost/cmd/6a/parse.ww \
selfhost/cmd/6a/asm.ww selfhost/cmd/6a/obj.ww \
selfhost/cmd/wwc/mem.ww \
$(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a | $(BIN)
cd $(BIN) && ./ww build \
-I $$PWD/../../selfhost/cmd/6a \
-I $$PWD/../../selfhost/cmd/wwc \
$$PWD/../../selfhost/cmd/6a/main.ww
mv $(BIN)/main $@
# ---- ww-side 6l (linker port, exercised by 992_6l_ww) ------------------
# Built like 6a_ww. Needs -I selfhost/cmd/6l for the local sym/obj/pass/
# out modules and -I selfhost/cmd/wwc to find `mem`.
$(BIN)/6l_ww: selfhost/cmd/6l/main.ww selfhost/cmd/6l/sym.ww \
selfhost/cmd/6l/obj.ww selfhost/cmd/6l/pass.ww \
selfhost/cmd/6l/out.ww selfhost/cmd/wwc/mem.ww \
$(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a | $(BIN)
cd $(BIN) && ./ww build \
-I $$PWD/../../selfhost/cmd/6l \
-I $$PWD/../../selfhost/cmd/wwc \
$$PWD/../../selfhost/cmd/6l/main.ww
mv $(BIN)/main $@
# ---- ww-side ww driver (exercised by 993_ww_ww) ------------------------
# The driver pulls in lib/os (default search path) and selfhost/cmd/wwc
# (for the bump arena). It then orchestrates 6c/6a/6l like the C driver.
$(BIN)/ww_ww: selfhost/cmd/ww/main.ww selfhost/cmd/wwc/mem.ww lib/os/os.ww \
$(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a | $(BIN)
cd $(BIN) && ./ww build \
-I $$PWD/../../selfhost/cmd/wwc \
$$PWD/../../selfhost/cmd/ww/main.ww
mv $(BIN)/main $@
# ---- runtime (libwwrt.a, assembled by our own 6a) ----------------------
$(OBJ)/rt/%.o: rt/%.s $(BIN)/6a | $(OBJ)/rt
$(BIN)/6a -o $@ $<
$(LIB)/libwwrt.a: $(RT_OBJ) | $(LIB)
$(AR) rcs $@ $(RT_OBJ)
# ---- directories -------------------------------------------------------
$(BIN) $(LIB) $(OBJ)/wwc $(OBJ)/ww $(OBJ)/wwdump $(OBJ)/6c $(OBJ)/6a $(OBJ)/6l $(OBJ)/rt:
mkdir -p $@
# ---- tests -------------------------------------------------------------
# Each phase adds a $(BIN)/test_<name> target; the runner walks them.
TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_6c $(BIN)/test_6a $(BIN)/test_6l $(BIN)/test_arch \
$(BIN)/test_e2e $(BIN)/test_ffi $(BIN)/test_dyn $(BIN)/test_stdlib \
$(BIN)/test_selfhost $(BIN)/test_6a_ww $(BIN)/test_6l_ww $(BIN)/test_ww_ww
$(BIN)/test_smoke: test/wwc/000_smoke.c $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) $(INCS) -o $@ $< -L$(LIB) -lwwc
$(BIN)/test_lex: test/wwc/100_lex.c $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) $(INCS) -o $@ $< -L$(LIB) -lwwc
$(BIN)/test_parse: test/wwc/200_parse.c $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) $(INCS) -o $@ $< -L$(LIB) -lwwc
$(BIN)/test_check: test/wwc/300_check.c $(LIB)/libwwc.a | $(BIN)
$(CC) $(CFLAGS) $(INCS) -o $@ $< -L$(LIB) -lwwc
$(BIN)/test_6c: test/wwc/400_6c.c $(BIN)/6c | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_6a: test/wwc/500_6a.c $(BIN)/6c $(BIN)/6a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_6l: test/wwc/600_6l.c $(BIN)/6c $(BIN)/6a $(BIN)/6l | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_arch: test/wwc/610_arch.c $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(OBJ)/rt/start.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_e2e: test/wwc/700_e2e.c $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a $(OBJ)/rt/start.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_ffi: test/wwc/800_ffi.c $(BIN)/6c | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_dyn: test/wwc/810_dyn.c $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_stdlib: test/wwc/900_stdlib.c $(BIN)/6c | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_selfhost: test/wwc/990_selfhost.c $(BIN)/6c $(BIN)/6a $(BIN)/6l \
$(BIN)/ww $(BIN)/wwdump $(BIN)/wwdump_ww $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_6a_ww: test/wwc/991_6a_ww.c $(BIN)/6a $(BIN)/6a_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_6l_ww: test/wwc/992_6l_ww.c $(BIN)/6l $(BIN)/6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_ww_ww: test/wwc/993_ww_ww.c $(BIN)/ww $(BIN)/ww_ww \
$(BIN)/6c $(BIN)/6a $(BIN)/6l $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
test: all $(TESTS)
@WW=$(BIN)/ww BIN=$(BIN) sh test/run
install: all
mkdir -p $(PREFIX)/bin $(PREFIX)/lib
cp $(BIN)/ww $(PREFIX)/bin/
cp $(LIB)/libwwc.a $(PREFIX)/lib/
clean:
rm -rf $(OUT)
# ---- bootstrap (phase 10) ----------------------------------------------
# Three-stage self-host: Cstage (these C tools) → ww1 → ww2 → ww3 with
# cmp ww2 == ww3. Stage 1 is wwdump_ww (the ww-built frontend); stages
# 2 and 3 assemble with 6a_ww and link with 6l_ww — every tool below
# the driver is now in ww. The C `ww` driver still orchestrates the
# pipeline (porting it is the remaining phase-10 work).
BS = $(OUT)/bootstrap
bootstrap: all
@echo "=== stage 0 (Cstage): C-built tools ==="
@echo " $(BIN)/ww $(BIN)/6c $(BIN)/6a $(BIN)/6l"
@echo
@echo "=== stage 1: ww-built tools, used to drive stages 2 and 3 ==="
@ls -l $(BIN)/wwdump_ww $(BIN)/6a_ww $(BIN)/6l_ww $(BIN)/ww_ww
@rm -rf $(BS) && mkdir -p $(BS)
@echo
@echo "=== stage 2: ww2 = ww1 self-compiles main.combined.ww ==="
@$(BIN)/wwdump_ww -c selfhost/cmd/wwdump/main.combined.ww > $(BS)/ww2.s
@$(BIN)/6a_ww -o $(BS)/ww2.o $(BS)/ww2.s
@$(BIN)/6l_ww -o $(BS)/ww2 $(BS)/ww2.o $(LIB)/libwwrt.a
@ls -l $(BS)/ww2
@echo
@echo "=== stage 3: ww3 = ww2 self-compiles main.combined.ww ==="
@$(BS)/ww2 -c selfhost/cmd/wwdump/main.combined.ww > $(BS)/ww3.s
@$(BIN)/6a_ww -o $(BS)/ww3.o $(BS)/ww3.s
@$(BIN)/6l_ww -o $(BS)/ww3 $(BS)/ww3.o $(LIB)/libwwrt.a
@ls -l $(BS)/ww3
@echo
@echo "=== fixed-point gates ==="
@cmp $(BS)/ww2.s $(BS)/ww3.s && echo " ww2.s == ww3.s ✓"
@cmp $(BS)/ww2.o $(BS)/ww3.o && echo " ww2.o == ww3.o ✓ (byte-identical .o)"
@cmp $(BS)/ww2 $(BS)/ww3 && echo " ww2 == ww3 ✓ (byte-identical exe)"
@echo
@echo "BOOTSTRAP OK: ww-cgen + 6a_ww + 6l_ww reach a fixed point."
@echo
@echo "Phase 10 remaining work:"
@echo " - delete cmd/wwc/ cmd/6c/ cmd/6a/ cmd/6l/ cmd/ww/ in the final commit"
@echo
@echo "Already in selfhost/:"
@echo " - cmd/wwc/ : ww-cgen (lex/parse/check/cgen) — bootstrap fixed point"
@echo " - cmd/6a/ : ww-6a (lex/parse/asm/obj) — byte-identical to C 6a (test 991)"
@echo " - cmd/6l/ : ww-6l (linker w/ archive support) — byte-identical to C 6l (test 992)"
@echo " - cmd/ww/ : ww-driver (build/run/version) — byte-identical to C ww (test 993)"
.PHONY: all cstage wwstage test install clean bootstrap