w6l: route writable globals into a second PT_LOAD
Second step toward top-level mutable `let`. The static path now loads .data PROGBITS sections from input .o files, page-aligns them after .text, and emits a second PT_LOAD (R+W) covering them. Relocations targeting data symbols compute against the data VA; text→text displacements still cancel the absolute VAs and stay correct. Inputs without any .data keep the original single-PT_LOAD layout byte-for-byte — 992 (selfhost w6l .o diff) and 995 (self-rebuild) depend on that invariant. Dynamic-link path (-l/-L) rejects .data for now with a clear error; folding writable globals into the existing R+W segment alongside .got.plt/.dynamic is a follow-up.
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
/*
|
||||
* out.c — emit a static ELF64 executable.
|
||||
*
|
||||
* Layout (file order):
|
||||
* Layout (file order) without .data:
|
||||
* [0..64) ELF header
|
||||
* [64..120) program header (one PT_LOAD)
|
||||
* [64..120) one program header (PT_LOAD R+X)
|
||||
* [120..0x1000) zero pad
|
||||
* [0x1000..) .text bytes
|
||||
*
|
||||
* The single PT_LOAD covers the whole file, R+X. No interpreter,
|
||||
* no dynamic, no .bss yet. Entry point is the address of the
|
||||
* symbol named "_start" (or whatever main supplies via -e).
|
||||
* With .data (any input .o has writable globals):
|
||||
* [0..64) ELF header
|
||||
* [64..176) two program headers (PT_LOAD R+X, PT_LOAD R+W)
|
||||
* [176..0x1000) zero pad
|
||||
* [0x1000..) .text bytes
|
||||
* [data_off..) .data bytes (file offset and vaddr page-aligned)
|
||||
*
|
||||
* No interpreter, no dynamic, no .bss yet. Entry point is the address
|
||||
* of the symbol named "_start" (or whatever main supplies via -e).
|
||||
*/
|
||||
#include "l.h"
|
||||
#include <stdio.h>
|
||||
@@ -52,9 +58,17 @@ l_emit_elf(Lnk *l, FILE *f, u64 base, u64 entry)
|
||||
if (l->sos != NULL && l->dyn_n > 0)
|
||||
return l_emit_dyn_elf(l, f, base, entry);
|
||||
|
||||
const u64 page = 0x1000;
|
||||
const u64 text_off = 0x1000;
|
||||
const u64 text_va = base + text_off;
|
||||
const u64 filesz = text_off + l->textlen;
|
||||
const u64 rx_end = text_off + l->textlen;
|
||||
const int has_data = (l->datalen > 0);
|
||||
|
||||
/* data goes at the next page boundary so the loader can grant a
|
||||
* fresh page of R+W permissions without overlapping the R+X mapping. */
|
||||
const u64 data_off = has_data ? ((rx_end + page - 1) & ~(page - 1)) : 0;
|
||||
const u64 data_va = has_data ? (base + data_off) : 0;
|
||||
const u64 file_end = has_data ? (data_off + l->datalen) : rx_end;
|
||||
(void)data_va;
|
||||
|
||||
Ehdr eh = {0};
|
||||
memcpy(eh.e_ident, "\x7f""ELF", 4);
|
||||
@@ -68,21 +82,36 @@ l_emit_elf(Lnk *l, FILE *f, u64 base, u64 entry)
|
||||
eh.e_phoff = sizeof(Ehdr);
|
||||
eh.e_ehsize = sizeof(Ehdr);
|
||||
eh.e_phentsize = sizeof(Phdr);
|
||||
eh.e_phnum = 1;
|
||||
(void)text_va;
|
||||
eh.e_phnum = has_data ? 2 : 1;
|
||||
|
||||
Phdr ph = {0};
|
||||
ph.p_type = PT_LOAD;
|
||||
ph.p_flags = PF_R | PF_X;
|
||||
ph.p_offset = 0;
|
||||
ph.p_vaddr = base;
|
||||
ph.p_paddr = base;
|
||||
ph.p_filesz = filesz;
|
||||
ph.p_memsz = filesz;
|
||||
ph.p_align = 0x1000;
|
||||
/* R+X load covering [0, rx_end). When .data is present we still
|
||||
* round up to a page in memsz so the loader doesn't try to give
|
||||
* the same page both R+X and R+W permissions. */
|
||||
Phdr phx = {0};
|
||||
phx.p_type = PT_LOAD;
|
||||
phx.p_flags = PF_R | PF_X;
|
||||
phx.p_offset = 0;
|
||||
phx.p_vaddr = base;
|
||||
phx.p_paddr = base;
|
||||
phx.p_filesz = rx_end;
|
||||
phx.p_memsz = rx_end;
|
||||
phx.p_align = page;
|
||||
|
||||
Phdr phw = {0};
|
||||
if (has_data) {
|
||||
phw.p_type = PT_LOAD;
|
||||
phw.p_flags = PF_R | PF_W;
|
||||
phw.p_offset = data_off;
|
||||
phw.p_vaddr = base + data_off;
|
||||
phw.p_paddr = base + data_off;
|
||||
phw.p_filesz = l->datalen;
|
||||
phw.p_memsz = l->datalen;
|
||||
phw.p_align = page;
|
||||
}
|
||||
|
||||
fwrite(&eh, 1, sizeof eh, f);
|
||||
fwrite(&ph, 1, sizeof ph, f);
|
||||
fwrite(&phx, 1, sizeof phx, f);
|
||||
if (has_data) fwrite(&phw, 1, sizeof phw, f);
|
||||
|
||||
/* pad to text_off */
|
||||
long here = ftell(f);
|
||||
@@ -90,5 +119,13 @@ l_emit_elf(Lnk *l, FILE *f, u64 base, u64 entry)
|
||||
|
||||
if (l->textlen) fwrite(l->text, 1, l->textlen, f);
|
||||
|
||||
if (has_data) {
|
||||
/* pad to data_off */
|
||||
here = ftell(f);
|
||||
for (long i = here; i < (long)data_off; i++) fputc(0, f);
|
||||
fwrite(l->data, 1, l->datalen, f);
|
||||
}
|
||||
(void)file_end;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user