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).
95 lines
2.1 KiB
C
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;
|
|
}
|