Phase 0 batch 1. Six typed-struct allocations switch from
amalloc(arena, NNu64): *T over-sized byte counts to alloc(T{...})!
with partial struct literal initialization. MAP_ANON-zero from
rt_alloc covers any field the literal omits — same contract the
amalloc bump arena provided via its explicit zero loop, but
without the rule-13 size literal at the call site.
Converted:
- w6a/asm.ww addreloc, addrelocdata, addfixup (areloc, afixup)
- w6l/sym.ww intern (lsym)
- w6l/dyn.ww loadso (lso), lexport
lexport's `if (vernamecs == nil) { e.version.ptr = nil;
e.version.len = 0i32; }` branch dropped — MAP_ANON-zero provides
the empty-version slot for free; the inverted `if (vernamecs !=
nil)` only takes the dcstrtostr path.
w6l/sym.ww:20 comment updated from "amalloc-zeroing" to
"alloc-zeroing gives 0, not -1" so the documented mechanism
matches the call.
w6a/obj.ww's 2 remaining amalloc sites (bufinit + bufgrow) are
runtime-N *u8 byte buffers, deferred to #8 (runtime-N alloc API).
Verified via 991_w6a_ww + 992_w6l_ww + 995_self_rebuild +
996_dyn_ww byte-identity. make test 132/132.
112 lines
2.8 KiB
Plaintext
112 lines
2.8 KiB
Plaintext
// selfhost/cmd/w6l/sym.ww — port of cmd/w6l/sym.c.
|
|
//
|
|
// Linker symbol table. Singly-linked list, usually a few hundred
|
|
// entries; hashing isn't worth it yet.
|
|
|
|
package w6l;
|
|
|
|
import mem;
|
|
|
|
type lsym = struct {
|
|
name: str,
|
|
val: u64, // offset within combined .text (or .data when
|
|
// indata=1) once linked
|
|
defined: i32, // 1 if some lobj defines this symbol
|
|
indata: i32, // 1 if defined in .data (writable globals)
|
|
owner: *lobj,
|
|
idxinowner: i32,
|
|
// Dynamic-linking fields. Set by resolve when an undefined sym
|
|
// is provided by some loaded lso. pltidx and dynsymidx default
|
|
// to -1 (set explicitly by resolve; alloc-zeroing gives 0, not -1).
|
|
isdyn: i32,
|
|
dynlib: *lso,
|
|
dynversion: str, // matched export's version; len 0 if none
|
|
pltidx: i32,
|
|
dynsymidx: i32,
|
|
snext: *lsym,
|
|
};
|
|
|
|
type lrel = struct {
|
|
off: u64, // offset within the relocation's section
|
|
section: i32, // 0 = .text, 1 = .data
|
|
kind: i32, // R_X86_64_*
|
|
sym: *lsym,
|
|
addend: i64,
|
|
rnext: *lrel,
|
|
};
|
|
|
|
type lobj = struct {
|
|
path: str,
|
|
buf: *u8, // object bytes
|
|
len: u64,
|
|
textoff: u64, // offset of .text in combined output
|
|
textsize: u64,
|
|
dataoff: u64, // offset of .data in combined output
|
|
datasize: u64, // bytes contributed to combined .data (0 if none)
|
|
onext: *lobj,
|
|
};
|
|
|
|
// lexport — one entry per GLOBAL/WEAK symbol exported by a loaded .so.
|
|
// Stored as a chain in the order the .so's dynsym presents them, so
|
|
// soprovides_v's first-match semantics agree with the C version.
|
|
type lexport = struct {
|
|
name: str,
|
|
version: str, // len 0 for unversioned globals
|
|
enext: *lexport,
|
|
};
|
|
|
|
type lso = struct {
|
|
path: str, // full filesystem path used to load
|
|
soname: str, // DT_SONAME, or basename if missing
|
|
exports: *lexport, // dynsym-order chain of exported names
|
|
sonext: *lso,
|
|
};
|
|
|
|
type lnk = struct {
|
|
a: *arena,
|
|
objs: *lobj,
|
|
sos: *lso,
|
|
syms: *lsym,
|
|
rels: *lrel,
|
|
text: *u8, // combined .text
|
|
textcap: u64,
|
|
textlen: u64,
|
|
// Combined .data (writable). Empty unless any input .o has a
|
|
// .data PROGBITS section.
|
|
data: *u8,
|
|
datacap: u64,
|
|
datalen: u64,
|
|
errs: i32,
|
|
dynn: i32, // number of syms routed through PLT
|
|
};
|
|
|
|
fn streq(a: str, b: str) bool = {
|
|
if (a.len != b.len) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < a.len) {
|
|
if (a[i] != b[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
export fn intern(l: *lnk, name: str) *lsym = {
|
|
let s: *lsym = l.syms;
|
|
for (s != nil) {
|
|
if (streq(s.name, name)) { return s; };
|
|
s = s.snext;
|
|
};
|
|
let n: *lsym = alloc(lsym { name = name, snext = l.syms })!;
|
|
l.syms = n;
|
|
return n;
|
|
};
|
|
|
|
export fn lookup(l: *lnk, name: str) *lsym = {
|
|
let s: *lsym = l.syms;
|
|
for (s != nil) {
|
|
if (streq(s.name, name)) { return s; };
|
|
s = s.snext;
|
|
};
|
|
return nil;
|
|
};
|