Teach the linker to consume ET_DYN shared objects and emit a dynamically-linked ELF executable. Snake et al. can now link against libncurses + libc through the system dynamic loader. Pipeline additions: - dyn.c: read ET_DYN, parse .dynsym + DT_SONAME, walk .gnu.version_d / .gnu.version to learn each export's default version (skip hidden entries). - pass.c: when an undefined sym is provided by some Lso, promote it to dynamic, assign a PLT slot, record the matched version on the Lsym. - dynout.c: emit PT_INTERP + PT_DYNAMIC, .dynsym/.dynstr/.hash, .plt + .got.plt + .rela.plt, .gnu.version + .gnu.version_r, and the full DT_* set with DT_BIND_NOW. Patch PC32/PLT32 references against dyn syms to point at their PLT stubs. - main.c: -L<dir> and -l<name> flag parsing; resolve <name> via .so / .so.<N> / .a in libdir order, skipping GNU ld linker scripts (libc.so on most distros). - ww driver: collect -l/-L (joined and split forms) and pass through to 6l. Design choices: - DT_BIND_NOW so the loader resolves all PLT slots at startup; no PLT0 lazy resolver stub. - SysV .hash, not .gnu.hash. One bucket; loader scans the chain. Slow at scale, fine for snake-class binaries. - Non-PIE at fixed 0x400000. - No section headers — loader uses program headers, but readelf -V/-S won't display anything. Symbol versioning is the only correctness item beyond the basic PLT/GOT machinery: glibc symbols default to versions later than GLIBC_2.2.5 (e.g. clock_gettime → GLIBC_2.17 for the vDSO impl), and the loader rejects unversioned references to those without a matching Vernaux entry. test/wwc/810_dyn covers four cases: bare libc dyn call, multi-PLT, clock_gettime versioning, and fn-pointer to FFI binding (which exercises the codegen fixes from the parent commit alongside the new linker path).
143 lines
4.4 KiB
C
143 lines
4.4 KiB
C
/*
|
|
* 6l — amd64 linker. Reads relocatable ELF .o files (from 6a) 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.
|
|
*
|
|
* 6l -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, "6l: unknown flag %s\n", argv[i]);
|
|
return 2;
|
|
} else {
|
|
inputs[ninputs++] = argv[i];
|
|
}
|
|
}
|
|
if (out == NULL || ninputs == 0) {
|
|
fputs("usage: 6l -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, "6l: 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, "6l: no _start or main symbol defined\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE *f = fopen(out, "wb");
|
|
if (f == NULL) {
|
|
fprintf(stderr, "6l: 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;
|
|
}
|