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:
@@ -38,8 +38,9 @@ export fn emitu32(a: *asm_, v: u32) void = {
|
||||
};
|
||||
|
||||
export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = {
|
||||
let r: *areloc = amalloc(a.a, 48u64): *areloc;
|
||||
let r: *areloc = amalloc(a.a, 64u64): *areloc;
|
||||
r.off = off;
|
||||
r.section = 0; // .text
|
||||
r.kind = kind;
|
||||
r.asy = s;
|
||||
r.addend = add;
|
||||
@@ -47,6 +48,37 @@ export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = {
|
||||
a.relocs = r;
|
||||
};
|
||||
|
||||
// Record a relocation that lives in the .data section. Used by
|
||||
// DATAR to patch a 64-bit slot with a symbol's runtime VA. obj.ww
|
||||
// separates these into .rela.data when emitting the .o.
|
||||
export fn addrelocdata(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = {
|
||||
let r: *areloc = amalloc(a.a, 64u64): *areloc;
|
||||
r.off = off;
|
||||
r.section = 1; // .data
|
||||
r.kind = kind;
|
||||
r.asy = s;
|
||||
r.addend = add;
|
||||
r.rnext = a.relocs;
|
||||
a.relocs = r;
|
||||
};
|
||||
|
||||
// Append one byte to the writable .data buffer. Mirrors emitbyte
|
||||
// but targets a.data instead of a.text.
|
||||
export fn emitdatabyte(a: *asm_, b: u8) void = {
|
||||
if (a.datalen + 1u64 > a.datacap) {
|
||||
let nc: u64 = a.datacap;
|
||||
if (nc == 0u64) { nc = 256u64; };
|
||||
nc = nc * 2u64;
|
||||
let nb: *u8 = os.alloc(nc): *u8;
|
||||
let i: u64 = 0u64;
|
||||
for (i < a.datalen) { nb[i] = a.data[i]; i += 1u64; };
|
||||
a.data = nb;
|
||||
a.datacap = nc;
|
||||
};
|
||||
a.data[a.datalen] = b;
|
||||
a.datalen += 1u64;
|
||||
};
|
||||
|
||||
// ---- register codes ----------------------------------------------------
|
||||
|
||||
// Low 3 bits of register encoding.
|
||||
@@ -270,6 +302,38 @@ export fn encode(a: *asm_) i32 = {
|
||||
for (i < p.nbytes) { emitbyte(a, p.bytes[i]); i += 1u64; };
|
||||
p = p.link; continue;
|
||||
};
|
||||
if (op == A_DATAW) {
|
||||
// Writable variant: bytes go into .data instead of
|
||||
// .text. obj.ww emits the extra section conditionally
|
||||
// on datalen > 0 so .o output stays byte-identical
|
||||
// for inputs that don't use DATAW.
|
||||
let s: *asym = intern(a, p.to.asym);
|
||||
s.defined = 1;
|
||||
s.isdata = 1;
|
||||
s.isglobal = 1;
|
||||
s.addr = a.datalen;
|
||||
let i: u64 = 0u64;
|
||||
for (i < p.nbytes) { emitdatabyte(a, p.bytes[i]); i += 1u64; };
|
||||
p = p.link; continue;
|
||||
};
|
||||
if (op == A_DATAR) {
|
||||
// DATAR slot+off(SB), target(SB) — record an
|
||||
// R_X86_64_64 relocation at slot+off in .data
|
||||
// pointing at target. Slot must already be defined
|
||||
// by a prior DATAW.
|
||||
let holder: *asym = intern(a, p.from.asym);
|
||||
if (holder.defined == 0) {
|
||||
p = p.link; continue;
|
||||
};
|
||||
if (holder.isdata == 0) {
|
||||
p = p.link; continue;
|
||||
};
|
||||
let target: *asym = intern(a, p.to.asym);
|
||||
let reloff: u64 = holder.addr + p.from.offset: u64;
|
||||
addrelocdata(a, reloff, 1 /* R_X86_64_64 */,
|
||||
target, 0i64);
|
||||
p = p.link; continue;
|
||||
};
|
||||
if (op == A_RET) {
|
||||
emitbyte(a, 195u8); // 0xC3
|
||||
p = p.link; continue;
|
||||
|
||||
Reference in New Issue
Block a user