Files
ww/cmd/w6l/out.c
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

95 lines
2.1 KiB
C

/*
* out.c — emit a static ELF64 executable.
*
* Layout (file order):
* [0..64) ELF header
* [64..120) program header (one PT_LOAD)
* [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).
*/
#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 text_off = 0x1000;
const u64 text_va = base + text_off;
const u64 filesz = text_off + l->textlen;
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 = 1;
(void)text_va;
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;
fwrite(&eh, 1, sizeof eh, f);
fwrite(&ph, 1, sizeof ph, 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);
return 0;
}