w6l+selfhost: BSS optimisation — trim trailing .data zeros from filesz
Scan the consolidated .data buffer (post-relocation) for trailing zero bytes; set the R+W PT_LOAD's p_filesz to exclude them while p_memsz covers the full region. The loader zero-fills the gap, so behaviour is unchanged. Saves up to a page per binary on programs whose globals are zero-init. Mirrored in selfhost/cmd/w6l/out.ww so test 992's byte-identity diff still holds. Dynamic-link path is untouched — it still errors on any mutable global; that's the next feature.
This commit is contained in:
@@ -63,6 +63,21 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
||||
dataoff = (rxend + PAGE_SZ - 1u64) & ~(PAGE_SZ - 1u64);
|
||||
};
|
||||
|
||||
// BSS optimisation: trailing zero bytes in .data can be left
|
||||
// out of the file. The loader zero-fills the gap between
|
||||
// p_filesz and p_memsz. Scan after l_relocate has applied any
|
||||
// DATAR patches — anything still zero at the tail genuinely is
|
||||
// zero-init. Matches cmd/w6l/out.c byte-for-byte.
|
||||
let bsslen: u64 = 0u64;
|
||||
if (hasdata) {
|
||||
for (bsslen < l.datalen) {
|
||||
let b: u8 = l.data[l.datalen - 1u64 - bsslen];
|
||||
if (b != 0u8) { break; };
|
||||
bsslen += 1u64;
|
||||
};
|
||||
};
|
||||
let datafilelen: u64 = l.datalen - bsslen;
|
||||
|
||||
// One contiguous header buffer covering [0..0x1000), then .text.
|
||||
let hdr: *u8 = os.alloc(TEXT_OFF): *u8; // zero-initialised by mmap
|
||||
|
||||
@@ -106,7 +121,7 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
||||
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, 152u64, datafilelen); // p_filesz
|
||||
wru64(hdr, 160u64, l.datalen); // p_memsz
|
||||
wru64(hdr, 168u64, PAGE_SZ); // p_align
|
||||
};
|
||||
@@ -128,8 +143,10 @@ 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.
|
||||
if (hasdata && datafilelen > 0u64) {
|
||||
// Pad to the page-aligned data offset, then write only
|
||||
// the non-zero prefix of .data. The rest is BSS — the
|
||||
// loader zero-fills from p_filesz to p_memsz.
|
||||
let here: u64 = TEXT_OFF + l.textlen;
|
||||
let zero: u8 = 0u8;
|
||||
for (here < dataoff) {
|
||||
@@ -140,13 +157,13 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
||||
};
|
||||
here += 1u64;
|
||||
};
|
||||
let r4: (i64 | os.oserror) = os.writeall(fd, l.data, l.datalen);
|
||||
let r4: (i64 | os.oserror) = os.writeall(fd, l.data, datafilelen);
|
||||
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; };
|
||||
if (n4 != datafilelen: i64) { return -1; };
|
||||
};
|
||||
return 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user