Files
ww/selfhost/cmd/w6l/main.ww

335 lines
8.6 KiB
Plaintext

// selfhost/cmd/w6l/main.ww — port of cmd/w6l/main.c.
//
// w6l = amd64 linker. Reads relocatable ELF .o files, SysV `ar`
// archives, and shared objects (ET_DYN). Resolves symbols, applies
// relocations, writes a static or dynamic-linked ELF executable.
//
// w6l_ww -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
package main;
import os;
import rt;
import strings;
import sym;
import obj;
import dyn;
import pass;
import out;
def BASE: u64 = 4194304u64; // 0x400000
def CODE_VA_OFF: u64 = 4096u64; // .text starts at base + 0x1000
fn mklnk() *lnk = {
let l: *lnk = alloc(lnk { })!;
return l;
};
// `cstreq` lives in obj.ww — same bundle, single definition.
// `cstrlen` lives in obj.ww — same bundle, single definition.
// Build "<dir>/lib<name>.<ext>" into dst (NUL-terminated). Returns total
// length excluding NUL. dst must be large enough.
fn buildpath(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = {
let i: u64 = 0u64;
let dn: u64 = cstrlen(dir);
let nn: u64 = cstrlen(name);
let k: u64 = 0u64;
for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; };
dst[i] = '/';
i += 1u64;
dst[i] = 'l';
i += 1u64;
dst[i] = 'i';
i += 1u64;
dst[i] = 'b';
i += 1u64;
k = 0u64;
for (k < nn) { dst[i] = name[k]; i += 1u64; k += 1u64; };
k = 0u64;
for (k < ext.len: u64) {
let li: i32 = k: i32;
dst[i] = ext[li];
i += 1u64;
k += 1u64;
};
dst[i] = 0u8;
return i;
};
// Append decimal n to dst at offset i. Returns new offset.
fn appenddec(dst: *u8, i: u64, n: u64) u64 = {
if (n == 0u64) {
dst[i] = 48u8;
return i + 1u64;
};
let buf: [16]u8;
let k: u64 = 0u64;
let v: u64 = n;
for (v > 0u64) {
buf[k] = (v % 10u64): u8 + 48u8;
v = v / 10u64;
k += 1u64;
};
let oi: u64 = i;
for (k > 0u64) {
k -= 1u64;
dst[oi] = buf[k];
oi += 1u64;
};
return oi;
};
fn buildpathv(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = {
let i: u64 = 0u64;
let dn: u64 = cstrlen(dir);
let nn: u64 = cstrlen(name);
let k: u64 = 0u64;
for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; };
dst[i] = '/'; i += 1u64;
dst[i] = 'l'; i += 1u64;
dst[i] = 'i'; i += 1u64;
dst[i] = 'b'; i += 1u64;
k = 0u64;
for (k < nn) { dst[i] = name[k]; i += 1u64; k += 1u64; };
dst[i] = '.'; i += 1u64; dst[i] = 's'; i += 1u64; dst[i] = 'o'; i += 1u64; dst[i] = '.'; i += 1u64;
i = appenddec(dst, i, v);
dst[i] = 0u8;
return i;
};
// islinkable: read first 8 bytes; require !<arch>\n or \x7fELF.
fn islinkable(path: *u8) bool = {
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
if (fd < 0) { return false; };
let mp: [8]u8;
let n: i64 = os.read(fd, &mp[0], 8u64);
os.close(fd);
if (n < 4i64) { return false; };
// hasprefix length-bails when the header is shorter than the magic,
// matching the old n>=8 archive guard (str len < 8 => no match).
let hn: i32 = n: i32;
let hdr: str = strings.frombytes(mp[0:hn]);
if (strings.hasprefix(hdr, "!<arch>\n")) { return true; };
if (strings.hasprefix(hdr, "\x7fELF")) { return true; };
return false;
};
// Walk libdirs[0..n) trying lib<name>.so, then lib<name>.so.{0..8},
// then lib<name>.a. Return a heap-allocated NUL-terminated path on
// success, nil on miss.
fn resolvelib(name: *u8, libdirs: **u8, nlibdirs: i32) *u8 = {
let bufp: []u8 = alloc([], 1024u64)!;
bufp.len = 1024;
let i: i32 = 0;
for (i < nlibdirs) {
let dir: *u8 = libdirs[i];
let _l1: u64 = buildpath(bufp.ptr, dir, name, ".so");
if (islinkable(bufp.ptr)) {
let pl: u64 = cstrlen(bufp.ptr);
let p: []u8 = alloc([], pl + 1u64)!;
let k: u64 = 0u64;
for (k <= pl) { p[k] = bufp[k]; k += 1u64; };
return p.ptr;
};
let v: u64 = 0u64;
for (v <= 8u64) {
let _l2: u64 = buildpathv(bufp.ptr, dir, name, v);
if (islinkable(bufp.ptr)) {
let pl2: u64 = cstrlen(bufp.ptr);
let p2: []u8 = alloc([], pl2 + 1u64)!;
let k2: u64 = 0u64;
for (k2 <= pl2) { p2[k2] = bufp[k2]; k2 += 1u64; };
return p2.ptr;
};
v += 1u64;
};
let _l3: u64 = buildpath(bufp.ptr, dir, name, ".a");
if (islinkable(bufp.ptr)) {
let pl3: u64 = cstrlen(bufp.ptr);
let p3: []u8 = alloc([], pl3 + 1u64)!;
let k3: u64 = 0u64;
for (k3 <= pl3) { p3[k3] = bufp[k3]; k3 += 1u64; };
return p3.ptr;
};
i += 1;
};
return nil;
};
// Read first 20 bytes; return 1 for ET_DYN .so, 0 for ar/.o.
fn isso(path: *u8) i32 = {
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
if (fd < 0) { return 0; };
let mp: [20]u8;
let n: i64 = os.read(fd, &mp[0], 20u64);
os.close(fd);
if (n < 20i64) { return 0; };
let hdr: str = strings.frombytes(mp[0:4]);
if (!strings.hasprefix(hdr, "\x7fELF")) { return 0; };
// e_type at offset 16, u16 little-endian
let t: u16 = (mp[16u64]: u16) | ((mp[17u64]: u16) << 8u16);
if (t == 3u16) { return 1; };
return 0;
};
export fn main(argc: i32, argv: **u8) i32 = {
let outpath: *u8 = nil;
let maxinputs: i32 = 64;
let inputs: []*u8 = alloc([], maxinputs: u64)!;
inputs.len = maxinputs;
let ninputs: i32 = 0;
let libdirs: []*u8 = alloc([], maxinputs: u64)!;
libdirs.len = maxinputs;
let nlibdirs: i32 = 0;
let lflags: []*u8 = alloc([], maxinputs: u64)!;
lflags.len = maxinputs;
let nlflags: i32 = 0;
let i: i32 = 1;
for (i < argc) {
let a: *u8 = argv[i];
if (cstreq(a, "-o")) {
i += 1;
if (i >= argc) {
let m: str = "w6l: -o requires argument\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
outpath = argv[i];
} else { if (cstreq(a, "-L")) {
i += 1;
if (i >= argc) {
let m: str = "w6l: -L requires argument\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
libdirs[nlibdirs] = argv[i];
nlibdirs += 1;
} else { if (cstreq(a, "-l")) {
i += 1;
if (i >= argc) {
let m: str = "w6l: -l requires argument\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
lflags[nlflags] = argv[i];
nlflags += 1;
} else { if (a[0u64] == '-') {
// -L<dir> joined form.
if (a[1u64] == 'L') {
if (a[2u64] != 0u8) {
libdirs[nlibdirs] = a + 2u64;
nlibdirs += 1;
} else {
let m: str = "w6l: bare -L\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
} else { if (a[1u64] == 'l') {
if (a[2u64] != 0u8) {
lflags[nlflags] = a + 2u64;
nlflags += 1;
} else {
let m: str = "w6l: bare -l\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
} else {
let m: str = "w6l: unknown flag\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};};
} else {
if (ninputs >= maxinputs) {
let m: str = "w6l: too many inputs\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
inputs[ninputs] = a;
ninputs += 1;
};};};};
i += 1;
};
if (outpath == nil) {
let m: str = "usage: w6l_ww -o exe [-L<dir>...] [-l<name>...] file1.o [file2.o...]\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
if (ninputs == 0) {
let m: str = "w6l: no inputs\n";
os.write(2, m.ptr, m.len: u64);
return 2;
};
let l: *lnk = mklnk();
// Seed _start so libwwrt-style start.o is recognised as wanted.
intern(l, "_start");
// Load positional inputs first (preserving order).
let k: i32 = 0;
for (k < ninputs) {
if (load(l, inputs[k]) != 0) {
return 1;
};
k += 1;
};
// Then resolve -l flags and load each. Archives append; shared
// objects register their exports.
let lf: i32 = 0;
for (lf < nlflags) {
let p: *u8 = resolvelib(lflags[lf], libdirs.ptr, nlibdirs);
if (p == nil) {
let m: str = "w6l: cannot find -l";
os.write(2, m.ptr, m.len: u64);
let nm: *u8 = lflags[lf];
os.write(2, nm, cstrlen(nm));
let nl: str = "\n";
os.write(2, nl.ptr, nl.len: u64);
return 1;
};
if (isso(p) != 0) {
if (loadso(l, p) != 0) { return 1; };
} else {
if (load(l, p) != 0) { return 1; };
};
lf += 1;
};
if (resolve(l) != 0) { return 1; };
// Relocation is deferred to the emit functions — each path
// knows its own layout (textva, datava); the static and dyn
// paths place .data at different VAs.
let entrysym: *lsym = lookup(l, "_start");
if (entrysym == nil) { entrysym = lookup(l, "main"); }
else { if (entrysym.defined == 0) { entrysym = lookup(l, "main"); }; };
if (entrysym == nil) {
let m: str = "w6l: no _start or main symbol\n";
os.write(2, m.ptr, m.len: u64);
return 1;
};
if (entrysym.defined == 0) {
let m: str = "w6l: no _start or main symbol\n";
os.write(2, m.ptr, m.len: u64);
return 1;
};
let flags: os.flag = os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC;
let fd: i32 = os.open(pathstr(outpath), flags, 493i32); // 0o755
if (fd < 0) {
let m: str = "w6l: cannot open output\n";
os.write(2, m.ptr, m.len: u64);
return 1;
};
let entryva: u64 = BASE + CODE_VA_OFF + entrysym.val;
let rc: i32 = emitelf(l, fd, BASE, entryva);
os.close(fd);
return rc;
};