toolchain: banner purge + WHY-only comment sweep (rule 8)

selfhost/, cmd/, internal/ join the tree-wide sweep: every section
banner dies (91 selfhost + the cmd C-style dividers -> 0); narration
and stale contracts deleted (pre-#22 bundler notes, retired
single-PT_LOAD and no-archive claims, superseded ABI tables); every
ref/harec/qbe cite, task cite, encoding/ELF contract, and rule-10
twin pointer kept; lost lifetime/rationale lines restored where the
sweep over-cut (elf_globals ownership, kwtab linear-scan). Comment-
only proven: all five wwstage tool binaries byte-identical across
the sweep; test-commit, test-byteid (161+1399, 0 pinned-divergent),
and test-bootstrap (fixed point + 991-995 byte-id) all exit 0.
The read-through banked 66 latent-bug leads (checkpoint).
This commit is contained in:
2026-08-08 23:14:03 +09:00
parent 83f5956df2
commit 62b9d20383
60 changed files with 232 additions and 1045 deletions

View File

@@ -1,12 +1,4 @@
// selfhost/cmd/w6l/obj.ww — port of cmd/w6l/obj.c.
//
// Loads relocatable ELF64 .o files emitted by w6a, appends .text to
// the combined image, and pulls in symbols + relocations with
// offsets adjusted to the combined section.
//
// Also handles SysV `ar` archives (libwwrt.a). The two-pass loader
// indexes members on the first pass and iteratively pulls members
// that define currently-undefined symbols on subsequent passes.
// Port of cmd/w6l/obj.c.
package w6l;
@@ -22,7 +14,6 @@ def SHT_SYMTAB: i32 = 2;
def SHT_STRTAB: i32 = 3;
def SHT_RELA: i32 = 4;
// ---- little-endian byte readers ----------------------------------------
// w6a/w6l use straight LE on amd64. Reading via byte offsets keeps us off
// the cgen's u16 field-load story for now (MOVZBQ exists; MOVZWQ doesn't).
@@ -46,7 +37,6 @@ fn rdu64(p: *u8, off: u64) u64 = {
return lo | (hi << 32u64);
};
// ---- ELF64 section header offsets (40 bytes total) --------------------
def SHDR_SIZE: u64 = 64u64; // sizeof(Shdr) per ELF64 spec
def SHDR_NAME: u64 = 0u64;
def SHDR_TYPE: u64 = 4u64;
@@ -76,8 +66,6 @@ def RELA_OFFSET: u64 = 0u64;
def RELA_INFO: u64 = 8u64;
def RELA_ADDEND: u64 = 16u64;
// ---- file slurp --------------------------------------------------------
fn slurp(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
@@ -100,8 +88,6 @@ fn slurp(path: *u8) (*u8, u64) = {
return buf.ptr, n: u64;
};
// ---- text buffer growth ------------------------------------------------
fn emittext(l: *lnk, src: *u8, n: u64) void = {
if (l.textlen + n > l.textcap) {
let nc: u64 = l.textcap;
@@ -149,17 +135,13 @@ fn emitdata(l: *lnk, src: *u8, n: u64) void = {
l.datalen += n;
};
// ---- C-string helpers --------------------------------------------------
fn cstrlen(p: *u8) u64 = {
let n: u64 = 0u64;
for (p[n] != 0u8) { n += 1u64; };
return n;
};
// pathstr — view a NUL-terminated *u8 as a str. Bridges argv-style
// callers to lib/os entrypoints (str post-task-#23). Shared with
// main.ww and dyn.ww via the w6l bundle.
// Bridges argv-style callers to lib/os entrypoints (str post-task-#23).
fn pathstr(p: *u8) str = {
let r: str;
r.ptr = p;
@@ -179,7 +161,6 @@ fn cstreq(p: *u8, lit: str) bool = {
return true;
};
// Build a ww str from a NUL-terminated *u8 (for passing to intern).
fn cstrtostr(p: *u8) str = {
let n: u64 = cstrlen(p);
let view: str;
@@ -188,8 +169,6 @@ fn cstrtostr(p: *u8) str = {
return strings.dup(view);
};
// ---- archive (SysV ar) types and helpers -------------------------------
//
// Each archive member starts with a 60-byte ar_hdr. The fields we care
// about are the first byte (member type) and the size at offset 48 (a
// 10-byte, space-padded decimal). Member bodies are 2-byte aligned.
@@ -231,9 +210,8 @@ fn arfield(p: *u8, n: u64) u64 = {
return v;
};
// elfglobals — return a linked list of names of globally-defined
// (STB_GLOBAL) symbols whose section is `.text`. Names are owned
// heap copies, so the source ELF buffer can be freed afterward.
// Names are owned heap copies, so the source ELF buffer can be freed
// afterward.
fn elfglobals(buf: *u8, len: u64) *defent = {
if (len < EHDR_SIZE) { return nil; };
if (buf[0u64] != 127u8) { return nil; };
@@ -310,9 +288,8 @@ fn elfglobals(buf: *u8, len: u64) *defent = {
return head;
};
// memberdefinesundef — true if any of m's defined globals matches a
// currently-undefined symbol in the linker's symbol table. Names not
// already interned are uninteresting (the link doesn't need them yet).
// Names not already interned are uninteresting (the link doesn't need
// them yet).
fn memberdefinesundef(l: *lnk, m: *armember) bool = {
let de: *defent = m.defs;
for (de != nil) {
@@ -406,8 +383,6 @@ fn loadarchive(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
return 0;
};
// ---- main loader -------------------------------------------------------
export fn load(l: *lnk, path: *u8) i32 = {
let bufp: *u8;
let buflen: u64;
@@ -441,7 +416,6 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
let shstrshoff: u64 = rdu64(buf, shoff + (shstrndx: u64) * SHDR_SIZE + SHDR_OFFSET);
let shstr: *u8 = buf + shstrshoff;
// find .text, .data, .symtab, .rela.text, .rela.data
let idxtext: i32 = -1;
let idxdata: i32 = -1;
let idxsymtab: i32 = -1;
@@ -497,7 +471,6 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
datasize = rdu64(buf, datash + SHDR_SIZE_F);
};
// Track this object.
let ob: *lobj = alloc(lobj {
path = cstrtostr(path),
buf = buf,
@@ -510,18 +483,16 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
})!;
l.objs = ob;
// Append .text bytes to the combined image.
emittext(l, buf + textoff, textsize);
// Append .data bytes (if present) to the combined .data buffer.
if (idxdata >= 0) {
if (datasize > 0u64) {
emitdata(l, buf + dataoff, datasize);
};
};
// Walk symbols. We don't keep a per-object map[] of *lsym. Instead
// the reloc loop re-walks symtab and re-interns by name. Simpler
// than dancing around the cgen's u64-shift gaps.
// We don't keep a per-object map[] of *lsym. Instead the reloc
// loop re-walks symtab and re-interns by name. Simpler than
// dancing around the cgen's u64-shift gaps.
let si: u64 = 1u64; // skip index 0 (always undef sentinel)
for (si < nsyms) {
let symp: u64 = symoff + si * SYM_SIZE;
@@ -569,7 +540,6 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
si += 1u64;
};
// Per-object relocation collection.
if (idxrela >= 0) {
let relash: u64 = shoff + (idxrela: u64) * SHDR_SIZE;
let relaoff: u64 = rdu64(buf, relash + SHDR_OFFSET);
@@ -590,7 +560,6 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
addend = raddend: i64,
rnext = l.rels,
})!;
// Look up the referenced sym by name (re-walk symtab).
if ((rsymidx: u64) < nsyms) {
let sp: u64 = symoff + (rsymidx: u64) * SYM_SIZE;
let sname: u32 = rdu32(buf, sp + SYM_NAME);
@@ -605,8 +574,8 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
};
};
// Data-reloc collection. Offsets land in .data, shifted by
// this object's data_off so they index the combined buffer.
// Offsets land in .data, shifted by this object's dataoff so
// they index the combined buffer.
if (idxrelad >= 0) {
let relash: u64 = shoff + (idxrelad: u64) * SHDR_SIZE;
let relaoff: u64 = rdu64(buf, relash + SHDR_OFFSET);