Files
ww/selfhost/cmd/w6l/pass.ww
Hojun-Cho 2c33228b7e ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
2026-05-11 13:49:27 +09:00

116 lines
3.0 KiB
Plaintext

// selfhost/cmd/w6l/pass.ww — port of cmd/w6l/pass.c.
//
// Resolution + relocation. l_resolve flags every undefined symbol
// referenced by a relocation, and promotes those provided by some
// loaded .so to "dynamic" with a freshly-assigned PLT slot.
// l_relocate walks the rel list and patches the .text bytes in place
// once the final virtual base is known. Dynamic refs are deferred:
// their site is patched later in dynout, once the PLT vaddr is known.
//
// Supported relocation kinds: PC32 (=2), PLT32 (=4); both are 32-bit
// PC-relative displacements (PLT32 == PC32 for static).
use os;
use sym;
use dyn;
def R_X86_64_PC32: i32 = 2;
def R_X86_64_PLT32: i32 = 4;
export fn l_resolve(l: *lnk) i32 = {
// Initialise dynamic-linking sentinels. amalloc zeroes, so
// is_dyn/dyn_lib start clean — but plt_idx and dynsym_idx
// must be -1, not 0.
let si: *lsym = l.syms;
for (si != nil) {
si.plt_idx = -1;
si.dynsym_idx = -1;
si = si.snext;
};
// Promote each undefined sym that some lso exports to dynamic
// and hand it a PLT slot. Iteration order over the relocation
// list determines slot numbering and is stable across runs.
let r: *lrel = l.rels;
for (r != nil) {
if (r.sym != nil) {
if (r.sym.defined == 0) {
let sym: *lsym = r.sym;
if (sym.is_dyn == 0) {
let so: *lso = l.sos;
for (so != nil) {
if (l_so_provides(so, sym.name) != 0) {
sym.is_dyn = 1;
sym.dyn_lib = so;
sym.plt_idx = l.dyn_n;
l.dyn_n += 1;
so = nil; // break
} else {
so = so.sonext;
};
};
};
};
};
r = r.rnext;
};
// What remains undefined truly is undefined.
let r2: *lrel = l.rels;
for (r2 != nil) {
if (r2.sym != nil) {
if (r2.sym.defined == 0) {
if (r2.sym.is_dyn == 0) {
os.write(2, "w6l: undefined reference to '".ptr, 28u64);
let nm: str = r2.sym.name;
os.write(2, nm.ptr, nm.len: u64);
os.write(2, "'\n".ptr, 2u64);
l.errs += 1;
};
};
};
r2 = r2.rnext;
};
return l.errs;
};
fn patch_u32(p: *u8, v: u32) void = {
p[0] = (v & 255u32): u8;
p[1] = ((v >> 8u32) & 255u32): u8;
p[2] = ((v >> 16u32) & 255u32): u8;
p[3] = ((v >> 24u32) & 255u32): u8;
};
export fn l_relocate(l: *lnk, base: u64) i32 = {
let r: *lrel = l.rels;
for (r != nil) {
if (r.sym != nil) {
// Dynamic refs are patched later in dynout once the
// PLT vaddr is known.
if (r.sym.is_dyn != 0) {
r = r.rnext;
continue;
};
if (r.sym.defined != 0) {
let k: i32 = r.kind;
if (k == R_X86_64_PC32) {
let site: u64 = base + r.off;
let target: i64 = (base + r.sym.val): i64;
let rel: i64 = (target - site: i64) + r.addend;
patch_u32(l.text + r.off, rel: u32);
} else { if (k == R_X86_64_PLT32) {
let site: u64 = base + r.off;
let target: i64 = (base + r.sym.val): i64;
let rel: i64 = (target - site: i64) + r.addend;
patch_u32(l.text + r.off, rel: u32);
} else {
os.write(2, "w6l: unsupported reloc kind\n".ptr, 27u64);
l.errs += 1;
};};
};
};
r = r.rnext;
};
return l.errs;
};