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:
2026-05-12 13:44:27 +09:00
parent 548547a1d0
commit 2480f4c272
4 changed files with 142 additions and 16 deletions

View File

@@ -239,6 +239,84 @@ test_no_data_single_load(const char *bin)
return rc;
}
/* Test 5: BSS optimisation — trailing zero bytes in .data are dropped
* from the file (p_filesz < p_memsz) and the loader zero-fills the gap.
* Asserts that a trailing zero-init slot doesn't bloat the binary. */
static int
test_bss_filesz(const char *bin)
{
char exe[64];
snprintf(exe, sizeof exe, "/tmp/wwt_dl_%d.x5", getpid());
/* Two DATAW slots: a 1-byte non-zero followed by a 31-byte zero
* run. Memsz must cover all 32B; filesz should stop after the 1B
* non-zero (so file_data == 1, memsz_data == 32). */
const char *body =
"TEXT _start,$0\n"
"\tMOVQ\tnz(SB), DI\n"
"\tMOVQ\t$60, AX\n"
"\tSYSCALL\n"
"DATAW nz(SB),\"\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\"\n"
"DATAW zlong(SB),\"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\"\n";
if (build(bin, body, exe) != 0) {
fprintf(stderr, "test_bss_filesz: build failed\n");
return -1;
}
uint8_t *buf = NULL;
size_t n = 0;
if (slurp(exe, &buf, &n) < 0) {
fprintf(stderr, "test_bss_filesz: cannot read exe\n");
unlink(exe);
return -1;
}
int rc = 0;
const Ehdr *eh = (const Ehdr *)buf;
const Phdr *ph = (const Phdr *)(buf + eh->e_phoff);
int saw_rw = 0;
for (int i = 0; i < eh->e_phnum; i++) {
if (ph[i].p_type != PT_LOAD) continue;
if (ph[i].p_flags != (PF_R | PF_W)) continue;
saw_rw = 1;
if (ph[i].p_filesz >= ph[i].p_memsz) {
fprintf(stderr, "test_bss_filesz: filesz=%lu, "
"memsz=%lu (want filesz<memsz)\n",
(unsigned long)ph[i].p_filesz,
(unsigned long)ph[i].p_memsz);
rc = -1;
}
/* The scan trims every trailing zero — it doesn't know
* symbol boundaries. nz is \x07 then 7 zero bytes, so
* the file ends at byte 1 (the 0x07). Memsz still covers
* all 32B and the loader zero-fills. */
if (ph[i].p_filesz != 1) {
fprintf(stderr, "test_bss_filesz: filesz=%lu, "
"want 1\n", (unsigned long)ph[i].p_filesz);
rc = -1;
}
if (ph[i].p_memsz != 32) {
fprintf(stderr, "test_bss_filesz: memsz=%lu, "
"want 32\n", (unsigned long)ph[i].p_memsz);
rc = -1;
}
}
if (!saw_rw) {
fprintf(stderr, "test_bss_filesz: no R+W PT_LOAD found\n");
rc = -1;
}
free(buf);
if (rc != 0) { unlink(exe); return rc; }
/* Behaviour check: the zero-init slot must still read as zero
* at runtime — the loader has to zero-fill the BSS gap. */
int er = run_exit(exe);
unlink(exe);
if (er != 7) {
fprintf(stderr, "test_bss_filesz: exit=%d, want 7\n", er);
return -1;
}
return 0;
}
int
main(void)
{
@@ -249,10 +327,11 @@ main(void)
if (test_write(bin) != 0) fail++;
if (test_two_loads(bin) != 0) fail++;
if (test_no_data_single_load(bin) != 0) fail++;
if (test_bss_filesz(bin) != 0) fail++;
if (fail) {
fprintf(stderr, "data_link: %d/4 subtests failed\n", fail);
fprintf(stderr, "data_link: %d/5 subtests failed\n", fail);
return 1;
}
printf("data_link: 4/4 ok\n");
printf("data_link: 5/5 ok\n");
return 0;
}