Files
ww/cmd/w6l/out.c
Hojun-Cho 38e0b6510a 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.
2026-05-12 11:42:30 +09:00

132 lines
3.4 KiB
C

/*
* out.c — emit a static ELF64 executable.
*
* Layout (file order) without .data:
* [0..64) ELF header
* [64..120) one program header (PT_LOAD R+X)
* [120..0x1000) zero pad
* [0x1000..) .text bytes
*
* 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>
#include <stdlib.h>
#include <string.h>
#define ET_EXEC 2
#define EM_X86_64 62
#define EV_CURRENT 1
#define ELFCLASS64 2
#define ELFDATA2LSB 1
#define PT_LOAD 1
#define PF_X 1
#define PF_W 2
#define PF_R 4
#pragma pack(push, 1)
typedef struct {
u8 e_ident[16];
u16 e_type, e_machine;
u32 e_version;
u64 e_entry, e_phoff, e_shoff;
u32 e_flags;
u16 e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx;
} Ehdr;
typedef struct {
u32 p_type, p_flags;
u64 p_offset, p_vaddr, p_paddr;
u64 p_filesz, p_memsz, p_align;
} Phdr;
#pragma pack(pop)
int
l_emit_elf(Lnk *l, FILE *f, u64 base, u64 entry)
{
/* Dispatch: any loaded shared object plus any dynamic ref means
* we owe the loader a real PT_INTERP/PT_DYNAMIC binary. */
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 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);
eh.e_ident[4] = ELFCLASS64;
eh.e_ident[5] = ELFDATA2LSB;
eh.e_ident[6] = EV_CURRENT;
eh.e_type = ET_EXEC;
eh.e_machine = EM_X86_64;
eh.e_version = EV_CURRENT;
eh.e_entry = entry;
eh.e_phoff = sizeof(Ehdr);
eh.e_ehsize = sizeof(Ehdr);
eh.e_phentsize = sizeof(Phdr);
eh.e_phnum = has_data ? 2 : 1;
/* 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(&phx, 1, sizeof phx, f);
if (has_data) fwrite(&phw, 1, sizeof phw, f);
/* pad to text_off */
long here = ftell(f);
for (long i = here; i < (long)text_off; i++) fputc(0, f);
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;
}