Files
ww/selfhost/cmd/w6l/out.ww
Hojun-Cho 2c33228b7e ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
2026-05-11 13:49:27 +09:00

101 lines
3.1 KiB
Plaintext

// selfhost/cmd/w6l/out.ww — port of cmd/w6l/out.c.
//
// Emit a static ELF64 executable. File layout (per the C original):
// [0..64) Ehdr
// [64..120) Phdr (one PT_LOAD)
// [120..0x1000) zero pad
// [0x1000..) .text bytes
// Single PT_LOAD covers the whole file, R+X. No interpreter, no .bss.
use os;
use sym;
use dynout;
def ET_EXEC: u16 = 2u16;
def EM_X86_64_W: u16 = 62u16;
def EV_CURRENT: u32 = 1u32;
def ELFCLASS64: u8 = 2u8;
def ELFDATA2LSB: u8 = 1u8;
def PT_LOAD: u32 = 1u32;
def PF_X: u32 = 1u32;
def PF_R: u32 = 4u32;
def TEXT_OFF: u64 = 4096u64; // 0x1000
// ---- little-endian byte writers ----------------------------------------
fn wr_u16(buf: *u8, off: u64, v: u16) void = {
buf[off] = (v & 255u16): u8;
buf[off + 1u64] = ((v >> 8u16) & 255u16): u8;
};
fn wr_u32(buf: *u8, off: u64, v: u32) void = {
buf[off] = (v & 255u32): u8;
buf[off + 1u64] = ((v >> 8u32) & 255u32): u8;
buf[off + 2u64] = ((v >> 16u32) & 255u32): u8;
buf[off + 3u64] = ((v >> 24u32) & 255u32): u8;
};
fn wr_u64(buf: *u8, off: u64, v: u64) void = {
wr_u32(buf, off, (v & 4294967295u64): u32);
wr_u32(buf, off + 4u64, ((v >> 32u64) & 4294967295u64): u32);
};
// ---- emit ---------------------------------------------------------------
export fn l_emit_elf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
// Dispatch: any loaded shared object plus any dynamic ref means
// we owe the loader a real PT_INTERP/PT_DYNAMIC binary.
if (l.sos != nil) {
if (l.dyn_n > 0) {
return l_emit_dyn_elf(l, fd, base, entry);
};
};
let filesz: u64 = TEXT_OFF + l.textlen;
// One contiguous header buffer covering [0..0x1000), then .text.
let hdr: *u8 = os.alloc(TEXT_OFF): *u8; // zero-initialised by mmap
// --- Ehdr (64 bytes) ---
hdr[0u64] = 127u8; // 0x7f
hdr[1u64] = 69u8; // 'E'
hdr[2u64] = 76u8; // 'L'
hdr[3u64] = 70u8; // 'F'
hdr[4u64] = ELFCLASS64;
hdr[5u64] = ELFDATA2LSB;
hdr[6u64] = EV_CURRENT: u8;
wr_u16(hdr, 16u64, ET_EXEC); // e_type
wr_u16(hdr, 18u64, EM_X86_64_W); // e_machine
wr_u32(hdr, 20u64, EV_CURRENT); // e_version
wr_u64(hdr, 24u64, entry); // e_entry
wr_u64(hdr, 32u64, 64u64); // e_phoff = sizeof(Ehdr)
wr_u64(hdr, 40u64, 0u64); // e_shoff
wr_u32(hdr, 48u64, 0u32); // e_flags
wr_u16(hdr, 52u64, 64u16); // e_ehsize
wr_u16(hdr, 54u64, 56u16); // e_phentsize
wr_u16(hdr, 56u64, 1u16); // e_phnum
wr_u16(hdr, 58u64, 0u16); // e_shentsize
wr_u16(hdr, 60u64, 0u16); // e_shnum
wr_u16(hdr, 62u64, 0u16); // e_shstrndx
// --- Phdr (56 bytes) at offset 64 ---
wr_u32(hdr, 64u64, PT_LOAD); // p_type
wr_u32(hdr, 68u64, PF_R | PF_X); // p_flags
wr_u64(hdr, 72u64, 0u64); // p_offset
wr_u64(hdr, 80u64, base); // p_vaddr
wr_u64(hdr, 88u64, base); // p_paddr
wr_u64(hdr, 96u64, filesz); // p_filesz
wr_u64(hdr, 104u64, filesz); // p_memsz
wr_u64(hdr, 112u64, TEXT_OFF); // p_align
// Write [0..0x1000) then .text.
let n1: i64 = os.writefull(fd, hdr, TEXT_OFF);
if (n1 != TEXT_OFF: i64) { return -1; };
if (l.textlen > 0u64) {
let n2: i64 = os.writefull(fd, l.text, l.textlen);
if (n2 != l.textlen: i64) { return -1; };
};
return 0;
};