`type oserror = i64` carries -errno (Hare's errors::errno-shaped named-i64). The three convenience wrappers move off the i64 = -1 sentinel and onto the tagged-union surface. Callers updated across the selfhost (wwdump, w6c, w6a, w6l, ww driver). The slurp paths in w6c/w6a/w6l/wwdump now match on the filesize and readall results; the ELF-emitting writeall sites in w6a/obj.ww are wrapped through two small local helpers (`wrn` for "wrote N bytes ok?", `wrdrop` for fire-and-forget) so the existing 11-callsite write loop stays readable. selfhost/test/smoke.ww kept using raw os.read instead of os.readall: the 990 cgen-match probe compiles smoke.ww standalone (no `use` expansion), and cross-module type references like `os.oserror` can't be resolved in that mode. Two selfhost-side gaps surfaced and got plugged: - lib/ww/parse/parse.ww parsetype now collapses dotted type names (`pkg.Type` → single N_TNAME with the joined string), mirroring C parsetype's dotted-path loop. Local `joindotted` helper because there's no arena-based string-concat in the selfhost lib yet. - selfhost/cmd/wcc/check.ww name-resolver applies the dotted-prefix rule from cmd/wcc/check.c's resolve_typename: split at the last dot, look up the head as a `use` import, then the leaf as a type.
111 lines
3.3 KiB
Plaintext
111 lines
3.3 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 wru16(buf: *u8, off: u64, v: u16) void = {
|
|
buf[off] = (v & 255u16): u8;
|
|
buf[off + 1u64] = ((v >> 8u16) & 255u16): u8;
|
|
};
|
|
|
|
fn wru32(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 wru64(buf: *u8, off: u64, v: u64) void = {
|
|
wru32(buf, off, (v & 4294967295u64): u32);
|
|
wru32(buf, off + 4u64, ((v >> 32u64) & 4294967295u64): u32);
|
|
};
|
|
|
|
// ---- emit ---------------------------------------------------------------
|
|
|
|
export fn emitelf(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.dynn > 0) {
|
|
return emitdynelf(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;
|
|
wru16(hdr, 16u64, ET_EXEC); // e_type
|
|
wru16(hdr, 18u64, EM_X86_64_W); // e_machine
|
|
wru32(hdr, 20u64, EV_CURRENT); // e_version
|
|
wru64(hdr, 24u64, entry); // e_entry
|
|
wru64(hdr, 32u64, 64u64); // e_phoff = sizeof(Ehdr)
|
|
wru64(hdr, 40u64, 0u64); // e_shoff
|
|
wru32(hdr, 48u64, 0u32); // e_flags
|
|
wru16(hdr, 52u64, 64u16); // e_ehsize
|
|
wru16(hdr, 54u64, 56u16); // e_phentsize
|
|
wru16(hdr, 56u64, 1u16); // e_phnum
|
|
wru16(hdr, 58u64, 0u16); // e_shentsize
|
|
wru16(hdr, 60u64, 0u16); // e_shnum
|
|
wru16(hdr, 62u64, 0u16); // e_shstrndx
|
|
|
|
// --- Phdr (56 bytes) at offset 64 ---
|
|
wru32(hdr, 64u64, PT_LOAD); // p_type
|
|
wru32(hdr, 68u64, PF_R | PF_X); // p_flags
|
|
wru64(hdr, 72u64, 0u64); // p_offset
|
|
wru64(hdr, 80u64, base); // p_vaddr
|
|
wru64(hdr, 88u64, base); // p_paddr
|
|
wru64(hdr, 96u64, filesz); // p_filesz
|
|
wru64(hdr, 104u64, filesz); // p_memsz
|
|
wru64(hdr, 112u64, TEXT_OFF); // p_align
|
|
|
|
// Write [0..0x1000) then .text.
|
|
let r1: (i64 | os.oserror) = os.writeall(fd, hdr, TEXT_OFF);
|
|
let n1: i64 = 0i64;
|
|
match (r1) {
|
|
case let v: i64 => n1 = v;
|
|
case let e: os.oserror => return -1;
|
|
};
|
|
if (n1 != TEXT_OFF: i64) { return -1; };
|
|
if (l.textlen > 0u64) {
|
|
let r2: (i64 | os.oserror) = os.writeall(fd, l.text, l.textlen);
|
|
let n2: i64 = 0i64;
|
|
match (r2) {
|
|
case let v: i64 => n2 = v;
|
|
case let e: os.oserror => return -1;
|
|
};
|
|
if (n2 != l.textlen: i64) { return -1; };
|
|
};
|
|
return 0;
|
|
};
|