selfhost: mirror writable .data + R_X86_64_64 across the wwstage

Bring the wwstage toolchain to parity with C-side DATAW / DATAR /
.data / .rela.data support. With this, w6c_ww + w6a_ww + w6l_ww can
compile, assemble and link `let g: str = "lit";` (and the scalar /
str / slice / struct globals that landed earlier) end-to-end, with
output that's byte-identical to the C-side pipeline.

w6a (types.ww / parse.ww / asm.ww / obj.ww):
  - A_DATAW + A_DATAR opcodes; parser learns `name+disp(SB)`;
    A_DATAR records an R_X86_64_64 reloc in .data via the new
    addrelocdata helper; areloc gains a `section` flag and asym
    an `isdata` flag; obj.ww splits relocs into .rela.text /
    .rela.data, emits .data PROGBITS + .rela.data conditionally,
    and shuffles section indices the same way cmd/w6a/obj.c does
    so byte output stays identical when no DATAW/DATAR are used.

w6l (sym.ww / obj.ww / pass.ww / out.ww / dynout.ww / main.ww):
  - lrel grows `section`; lsym grows `indata`; lobj tracks
    dataoff / datasize; lnk grows combined .data buffer;
  - obj.ww loads .data and .rela.data, registers data symbols
    with indata=1 and val shifted by the input's data_off, and
    the archive scanner includes both .text and .data globals;
  - pass.ww adds R_X86_64_64 (patch 8 bytes in .text or .data
    with sym_va + addend); relocate's signature becomes
    (textva, datava);
  - out.ww emits a second PT_LOAD (R+W) when datalen > 0, with
    .data at the page-aligned offset after .text;
  - dynout.ww refuses .data + -l/-L cleanly (matches the C-side
    error message);
  - main.ww computes text_va / data_va and passes both to
    relocate.

wcc cgen (cgen.ww / cgendecl.ww):
  - letpreintern walks top-level str-lets and interns the strlit
    BEFORE emitdatasection emits its DATA row, so emitletdataw
    can later look up the same label;
  - emitletdataw's 16B branch detects non-empty strlit init and
    emits the 8-zero + 8-LE-len DATAW plus a DATAR slot+0,strlit
    reloc, mirroring cmd/w6c/cgen.c.

Verified: `wwdump_ww -c` byte-matches `w6c` on a `let g: str =
"hello world\n";` fixture; `w6a_ww` and `w6l_ww` produce a
binary byte-identical to the C-side pipeline that runs and
prints "hello world". Bootstrap fixed-point holds (ww2 == ww3 ==
ww4), 26/26 tests green.
This commit is contained in:
2026-05-12 13:03:03 +09:00
parent 003f707618
commit 1ac9980f7e
16 changed files with 1132 additions and 145 deletions

View File

@@ -18,9 +18,11 @@ def ELFCLASS64: u8 = 2u8;
def ELFDATA2LSB: u8 = 1u8;
def PT_LOAD: u32 = 1u32;
def PF_X: u32 = 1u32;
def PF_W: u32 = 2u32;
def PF_R: u32 = 4u32;
def TEXT_OFF: u64 = 4096u64; // 0x1000
def PAGE_SZ: u64 = 4096u64;
// ---- little-endian byte writers ----------------------------------------
@@ -52,7 +54,14 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
};
};
let filesz: u64 = TEXT_OFF + l.textlen;
let hasdata: bool = l.datalen > 0u64;
let rxend: u64 = TEXT_OFF + l.textlen;
// .data lands at the next page boundary so the loader can give
// it fresh R+W permissions without overlapping the R+X mapping.
let dataoff: u64 = 0u64;
if (hasdata) {
dataoff = (rxend + PAGE_SZ - 1u64) & ~(PAGE_SZ - 1u64);
};
// One contiguous header buffer covering [0..0x1000), then .text.
let hdr: *u8 = os.alloc(TEXT_OFF): *u8; // zero-initialised by mmap
@@ -74,21 +83,34 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
wru32(hdr, 48u64, 0u32); // e_flags
wru16(hdr, 52u64, 64u16); // e_ehsize
wru16(hdr, 54u64, 56u16); // e_phentsize
wru16(hdr, 56u64, 1u16); // e_phnum
if (hasdata) { wru16(hdr, 56u64, 2u16); }
else { wru16(hdr, 56u64, 1u16); };
wru16(hdr, 58u64, 0u16); // e_shentsize
wru16(hdr, 60u64, 0u16); // e_shnum
wru16(hdr, 62u64, 0u16); // e_shstrndx
// --- Phdr (56 bytes) at offset 64 ---
wru32(hdr, 64u64, PT_LOAD); // p_type
wru32(hdr, 68u64, PF_R | PF_X); // p_flags
// --- Phdr #1 (R+X) at offset 64 ---
wru32(hdr, 64u64, PT_LOAD);
wru32(hdr, 68u64, PF_R | PF_X);
wru64(hdr, 72u64, 0u64); // p_offset
wru64(hdr, 80u64, base); // p_vaddr
wru64(hdr, 88u64, base); // p_paddr
wru64(hdr, 96u64, filesz); // p_filesz
wru64(hdr, 104u64, filesz); // p_memsz
wru64(hdr, 96u64, rxend); // p_filesz
wru64(hdr, 104u64, rxend); // p_memsz
wru64(hdr, 112u64, TEXT_OFF); // p_align
if (hasdata) {
// --- Phdr #2 (R+W) at offset 64+56=120 ---
wru32(hdr, 120u64, PT_LOAD);
wru32(hdr, 124u64, PF_R | PF_W);
wru64(hdr, 128u64, dataoff); // p_offset
wru64(hdr, 136u64, base + dataoff); // p_vaddr
wru64(hdr, 144u64, base + dataoff); // p_paddr
wru64(hdr, 152u64, l.datalen); // p_filesz
wru64(hdr, 160u64, l.datalen); // p_memsz
wru64(hdr, 168u64, PAGE_SZ); // p_align
};
// Write [0..0x1000) then .text.
let r1: (i64 | os.oserror) = os.writeall(fd, hdr, TEXT_OFF);
let n1: i64 = 0i64;
@@ -106,5 +128,25 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
};
if (n2 != l.textlen: i64) { return -1; };
};
if (hasdata) {
// Pad to the page-aligned data offset, then write .data.
let here: u64 = TEXT_OFF + l.textlen;
let zero: u8 = 0u8;
for (here < dataoff) {
let r3: (i64 | os.oserror) = os.writeall(fd, &zero, 1u64);
match (r3) {
case let v: i64 => { };
case let e: os.oserror => return -1;
};
here += 1u64;
};
let r4: (i64 | os.oserror) = os.writeall(fd, l.data, l.datalen);
let n4: i64 = 0i64;
match (r4) {
case let v: i64 => n4 = v;
case let e: os.oserror => return -1;
};
if (n4 != l.datalen: i64) { return -1; };
};
return 0;
};