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.
This commit is contained in:
2026-05-11 13:49:27 +09:00
parent e217cd32d1
commit 2c33228b7e
96 changed files with 2129 additions and 973 deletions

142
cmd/w6l/main.c Normal file
View File

@@ -0,0 +1,142 @@
/*
* w6l — amd64 linker. Reads relocatable ELF .o files (from w6a) plus
* .a archives, resolves, relocates, writes a static ELF executable.
* Dynamic linking against .so files is the next increment; the -L/-l
* flag plumbing here is its first step.
*
* w6l -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
*
* The first symbol named "_start" defined among the inputs becomes
* the entry point. If none is found, fall back to "main".
*/
#include "l.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* `path` is acceptable iff it's either an archive ("!<arch>\n") or
* an ELF file ("\x7fELF"). Distros often ship lib<name>.so as a GNU
* ld linker script (plain text); we skip those rather than parse the
* GROUP/INPUT directives.
*/
static int
is_linkable(const char *path)
{
FILE *f = fopen(path, "rb");
if (f == NULL) return 0;
u8 magic[8] = {0};
size_t n = fread(magic, 1, 8, f);
fclose(f);
if (n >= 8 && memcmp(magic, "!<arch>\n", 8) == 0) return 1;
if (n >= 4 && memcmp(magic, "\x7f""ELF", 4) == 0) return 1;
return 0;
}
/* Resolve -l<name> to a filesystem path by walking the libdirs we
* collected. Order: lib<name>.so → lib<name>.so.<N> globs → lib<name>.a.
* Skip anything that isn't a real archive or ELF (e.g. ld scripts).
*/
static const char *
resolve_lib(const char *name, char **libdirs, int n_libdirs)
{
static char buf[1024];
for (int i = 0; i < n_libdirs; i++) {
snprintf(buf, sizeof buf, "%s/lib%s.so", libdirs[i], name);
if (access(buf, 0) == 0 && is_linkable(buf)) return strdup(buf);
for (int v = 0; v <= 8; v++) {
snprintf(buf, sizeof buf, "%s/lib%s.so.%d",
libdirs[i], name, v);
if (access(buf, 0) == 0 && is_linkable(buf))
return strdup(buf);
}
snprintf(buf, sizeof buf, "%s/lib%s.a", libdirs[i], name);
if (access(buf, 0) == 0 && is_linkable(buf)) return strdup(buf);
}
return NULL;
}
int
main(int argc, char **argv)
{
const char *out = NULL;
const char **inputs = calloc(argc, sizeof *inputs);
int ninputs = 0;
char **libdirs = calloc(argc, sizeof *libdirs);
int n_libdirs = 0;
const char **lflags = calloc(argc, sizeof *lflags);
int n_lflags = 0;
u64 base = 0x400000;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) {
out = argv[++i];
} else if (strncmp(argv[i], "-L", 2) == 0 && argv[i][2]) {
libdirs[n_libdirs++] = strdup(argv[i] + 2);
} else if (strcmp(argv[i], "-L") == 0 && i + 1 < argc) {
libdirs[n_libdirs++] = strdup(argv[++i]);
} else if (strncmp(argv[i], "-l", 2) == 0 && argv[i][2]) {
lflags[n_lflags++] = argv[i] + 2;
} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
lflags[n_lflags++] = argv[++i];
} else if (argv[i][0] == '-') {
fprintf(stderr, "w6l: unknown flag %s\n", argv[i]);
return 2;
} else {
inputs[ninputs++] = argv[i];
}
}
if (out == NULL || ninputs == 0) {
fputs("usage: w6l -o exe [-L<dir>...] [-l<name>...] "
"file1.o [file2.o...]\n", stderr);
return 2;
}
/* Append -l-resolved files to the input list, after the .o/.a the
* caller passed positionally. They obey the same archive-pull
* semantics as a positional .a. */
for (int i = 0; i < n_lflags; i++) {
const char *p = resolve_lib(lflags[i], libdirs, n_libdirs);
if (p == NULL) {
fprintf(stderr, "w6l: cannot find -l%s\n", lflags[i]);
return 1;
}
inputs[ninputs++] = p;
}
Lnk l = {0};
/* Seed the symbol table with the entry point so archive pulls
* include the .o that defines it. Without this, a libwwrt.a
* containing start.o is silently skipped if no user .o
* references _start, and the entry falls back to main — which
* has no proper exit path. */
(void)l_intern(&l, "_start");
for (int i = 0; i < ninputs; i++) {
if (l_load(&l, inputs[i]) != 0) return 1;
}
if (l_resolve(&l) != 0) return 1;
if (l_relocate(&l, base + 0x1000) != 0) return 1;
Lsym *entry = l_lookup(&l, "_start");
if (entry == NULL || !entry->defined) entry = l_lookup(&l, "main");
if (entry == NULL || !entry->defined) {
fprintf(stderr, "w6l: no _start or main symbol defined\n");
return 1;
}
FILE *f = fopen(out, "wb");
if (f == NULL) {
fprintf(stderr, "w6l: cannot open %s\n", out);
return 1;
}
int rc = l_emit_elf(&l, f, base, base + 0x1000 + entry->val);
fclose(f);
if (rc == 0) {
/* chmod +x */
char cmd[1024];
snprintf(cmd, sizeof cmd, "chmod +x %s", out);
(void)system(cmd);
}
free(inputs);
return rc;
}