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:
@@ -48,12 +48,14 @@ def SHT_SYMTAB_C: u32 = 2u32;
|
||||
def SHT_STRTAB_C: u32 = 3u32;
|
||||
def SHT_RELA_C: u32 = 4u32;
|
||||
|
||||
def SHF_WRITE: u64 = 1u64;
|
||||
def SHF_ALLOC: u64 = 2u64;
|
||||
def SHF_EXECINSTR: u64 = 4u64;
|
||||
def SHF_INFO_LINK: u64 = 64u64; // 0x40
|
||||
|
||||
def STB_GLOBAL: u8 = 1u8;
|
||||
def STT_NOTYPE: u8 = 0u8;
|
||||
def STT_OBJECT: u8 = 1u8;
|
||||
def STT_FUNC: u8 = 2u8;
|
||||
|
||||
// Sizes of fixed structures.
|
||||
@@ -133,15 +135,56 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
let str_: buf; bufinit(&str_, a.a);
|
||||
let sym: buf; bufinit(&sym, a.a);
|
||||
let rela: buf; bufinit(&rela, a.a);
|
||||
let relad: buf; bufinit(&relad, a.a);
|
||||
|
||||
// Index 0 = empty.
|
||||
let zero: u8 = 0u8;
|
||||
bufputb(&shstr, &zero, 1u64);
|
||||
bufputb(&str_, &zero, 1u64);
|
||||
|
||||
// Section name offsets.
|
||||
let hasdata: bool = a.datalen > 0u64;
|
||||
let hasdatarelocs: bool = false;
|
||||
let rscan: *areloc = a.relocs;
|
||||
for (rscan != nil) {
|
||||
if (rscan.section == 1) { hasdatarelocs = true; };
|
||||
rscan = rscan.rnext;
|
||||
};
|
||||
|
||||
// Section indices (mirror cmd/w6a/obj.c):
|
||||
// without data, without data-relocs:
|
||||
// 1=.text 2=.rela.text 3=.symtab 4=.strtab 5=.shstrtab
|
||||
// with data, no data-relocs:
|
||||
// 1=.text 2=.rela.text 3=.data 4=.symtab 5=.strtab 6=.shstrtab
|
||||
// with data + data-relocs:
|
||||
// 1=.text 2=.rela.text 3=.data 4=.rela.data 5=.symtab
|
||||
// 6=.strtab 7=.shstrtab
|
||||
let SH_TEXT: u16 = 1u16;
|
||||
let SH_DATA: u16 = 0u16;
|
||||
let SH_RELAD: u16 = 0u16;
|
||||
let SH_SYMTAB: u16 = 3u16;
|
||||
if (hasdata) {
|
||||
SH_DATA = 3u16;
|
||||
if (hasdatarelocs) {
|
||||
SH_RELAD = 4u16;
|
||||
SH_SYMTAB = 5u16;
|
||||
} else {
|
||||
SH_SYMTAB = 4u16;
|
||||
};
|
||||
};
|
||||
let SH_STRTAB: u16 = SH_SYMTAB + 1u16;
|
||||
let SH_SHSTR: u16 = SH_STRTAB + 1u16;
|
||||
|
||||
// Section name offsets. Append .data / .rela.data only when
|
||||
// used so the .shstrtab buffer stays byte-identical for the
|
||||
// no-DATAW case (test 991 byte-diff invariant).
|
||||
let shntext: u32 = bufputcstr(&shstr, ".text");
|
||||
let shnrela: u32 = bufputcstr(&shstr, ".rela.text");
|
||||
let shndata: u32 = 0u32;
|
||||
let shnrelad: u32 = 0u32;
|
||||
if (hasdata) { shndata = bufputcstr(&shstr, ".data"); };
|
||||
if (hasdata) { if (hasdatarelocs) {
|
||||
shnrelad = bufputcstr(&shstr, ".rela.data");
|
||||
};};
|
||||
let shnsymtab: u32 = bufputcstr(&shstr, ".symtab");
|
||||
let shnstrtab: u32 = bufputcstr(&shstr, ".strtab");
|
||||
let shnshstrtab: u32 = bufputcstr(&shstr, ".shstrtab");
|
||||
@@ -152,8 +195,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
for (zi < 24) { zsym[zi] = 0u8; zi += 1; };
|
||||
bufputb(&sym, zsym.ptr, 24u64);
|
||||
|
||||
let SH_TEXT: u16 = 1u16;
|
||||
|
||||
// Build symbols.
|
||||
let idx: i32 = 1;
|
||||
let s: *asym = a.syms;
|
||||
@@ -164,8 +205,13 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
let stname: u32 = bufputcstr(&str_, s.name);
|
||||
wru32(entry.ptr, 0u64, stname);
|
||||
if (s.defined != 0) {
|
||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
||||
wru16(entry.ptr, 6u64, SH_TEXT);
|
||||
if (s.isdata != 0) {
|
||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_OBJECT));
|
||||
wru16(entry.ptr, 6u64, SH_DATA);
|
||||
} else {
|
||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
||||
wru16(entry.ptr, 6u64, SH_TEXT);
|
||||
};
|
||||
wru64(entry.ptr, 8u64, s.addr);
|
||||
} else {
|
||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_NOTYPE));
|
||||
@@ -177,7 +223,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
s = s.snext;
|
||||
};
|
||||
|
||||
// Build relocations.
|
||||
// Build relocations — split into text vs data buffers.
|
||||
let r: *areloc = a.relocs;
|
||||
for (r != nil) {
|
||||
let entry: [24]u8;
|
||||
@@ -185,7 +231,11 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
let rinfo: u64 = (r.asy.idx: u64 << 32u64) | (r.kind: u64 & 4294967295u64);
|
||||
wru64(entry.ptr, 8u64, rinfo);
|
||||
wru64(entry.ptr, 16u64, r.addend: u64);
|
||||
bufputb(&rela, entry.ptr, 24u64);
|
||||
if (r.section == 1) {
|
||||
bufputb(&relad, entry.ptr, 24u64);
|
||||
} else {
|
||||
bufputb(&rela, entry.ptr, 24u64);
|
||||
};
|
||||
r = r.rnext;
|
||||
};
|
||||
|
||||
@@ -193,12 +243,20 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
let off: u64 = EHDR_SZ;
|
||||
let offtext: u64 = off; off = off + a.textlen;
|
||||
let offrela: u64 = off; off = off + rela.n;
|
||||
let offdata: u64 = off; if (hasdata) { off = off + a.datalen; };
|
||||
let offrelad: u64 = off; if (hasdata) { if (hasdatarelocs) {
|
||||
off = off + relad.n;
|
||||
};};
|
||||
let offsym: u64 = off; off = off + sym.n;
|
||||
let offstr: u64 = off; off = off + str_.n;
|
||||
let offshstr: u64 = off; off = off + shstr.n;
|
||||
for ((off & 7u64) != 0u64) { off += 1u64; };
|
||||
let offshdr: u64 = off;
|
||||
let NSECT: u16 = 6u16;
|
||||
if (hasdata) {
|
||||
if (hasdatarelocs) { NSECT = 8u16; }
|
||||
else { NSECT = 7u16; };
|
||||
};
|
||||
|
||||
// ---- Ehdr ----
|
||||
let eh: [64]u8;
|
||||
@@ -223,7 +281,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru16(eh.ptr, 56u64, 0u16); // e_phnum
|
||||
wru16(eh.ptr, 58u64, 64u16); // e_shentsize
|
||||
wru16(eh.ptr, 60u64, NSECT); // e_shnum
|
||||
wru16(eh.ptr, 62u64, 5u16); // e_shstrndx
|
||||
wru16(eh.ptr, 62u64, SH_SHSTR); // e_shstrndx
|
||||
|
||||
if (!wrn(fd, eh.ptr, 64u64, 64i64)) { return -1; };
|
||||
if (a.textlen > 0u64) {
|
||||
@@ -232,6 +290,16 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
if (rela.n > 0u64) {
|
||||
if (!wrn(fd, rela.p, rela.n, rela.n: i64)) { return -1; };
|
||||
};
|
||||
if (hasdata) {
|
||||
if (a.datalen > 0u64) {
|
||||
if (!wrn(fd, a.data, a.datalen, a.datalen: i64)) { return -1; };
|
||||
};
|
||||
if (hasdatarelocs) {
|
||||
if (relad.n > 0u64) {
|
||||
if (!wrn(fd, relad.p, relad.n, relad.n: i64)) { return -1; };
|
||||
};
|
||||
};
|
||||
};
|
||||
if (sym.n > 0u64) {
|
||||
if (!wrn(fd, sym.p, sym.n, sym.n: i64)) { return -1; };
|
||||
};
|
||||
@@ -244,6 +312,10 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
|
||||
// Pad to 8 before shdrs.
|
||||
let written: u64 = EHDR_SZ + a.textlen + rela.n + sym.n + str_.n + shstr.n;
|
||||
if (hasdata) {
|
||||
written += a.datalen;
|
||||
if (hasdatarelocs) { written += relad.n; };
|
||||
};
|
||||
for ((written & 7u64) != 0u64) {
|
||||
wrdrop(fd, &zero, 1u64);
|
||||
written += 1u64;
|
||||
@@ -273,11 +345,38 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
||||
wru64(shbuf.ptr, 24u64, offrela);
|
||||
wru64(shbuf.ptr, 32u64, rela.n);
|
||||
wru32(shbuf.ptr, 40u64, 3u32); // sh_link = symtab idx
|
||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
||||
wru32(shbuf.ptr, 40u64, SH_SYMTAB: u32); // sh_link
|
||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
||||
wru64(shbuf.ptr, 48u64, 8u64);
|
||||
wru64(shbuf.ptr, 56u64, RELA_SZ);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
if (hasdata) {
|
||||
// .data
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
wru32(shbuf.ptr, 0u64, shndata);
|
||||
wru32(shbuf.ptr, 4u64, SHT_PROGBITS_C);
|
||||
wru64(shbuf.ptr, 8u64, SHF_ALLOC | SHF_WRITE);
|
||||
wru64(shbuf.ptr, 24u64, offdata);
|
||||
wru64(shbuf.ptr, 32u64, a.datalen);
|
||||
wru64(shbuf.ptr, 48u64, 8u64); // sh_addralign
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
if (hasdatarelocs) {
|
||||
// .rela.data
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
wru32(shbuf.ptr, 0u64, shnrelad);
|
||||
wru32(shbuf.ptr, 4u64, SHT_RELA_C);
|
||||
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
||||
wru64(shbuf.ptr, 24u64, offrelad);
|
||||
wru64(shbuf.ptr, 32u64, relad.n);
|
||||
wru32(shbuf.ptr, 40u64, SH_SYMTAB: u32);
|
||||
wru32(shbuf.ptr, 44u64, SH_DATA: u32); // applies to .data
|
||||
wru64(shbuf.ptr, 48u64, 8u64);
|
||||
wru64(shbuf.ptr, 56u64, RELA_SZ);
|
||||
wrdrop(fd, shbuf.ptr, 64u64);
|
||||
};
|
||||
};
|
||||
// .symtab
|
||||
sn = 0;
|
||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||
@@ -285,7 +384,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
||||
wru32(shbuf.ptr, 4u64, SHT_SYMTAB_C);
|
||||
wru64(shbuf.ptr, 24u64, offsym);
|
||||
wru64(shbuf.ptr, 32u64, sym.n);
|
||||
wru32(shbuf.ptr, 40u64, 4u32); // sh_link = strtab
|
||||
wru32(shbuf.ptr, 40u64, SH_STRTAB: u32); // sh_link
|
||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
||||
wru64(shbuf.ptr, 48u64, 8u64);
|
||||
wru64(shbuf.ptr, 56u64, SYM_SZ);
|
||||
|
||||
Reference in New Issue
Block a user