ww: import toolchain — C bootstrap + ww-side self-host (phases 0-10)
C bootstrap (phases 0-9):
cmd/wwc, cmd/6c, cmd/6a, cmd/6l, cmd/ww, rt, lib/*.
ww-side self-host (phase 10):
selfhost/cmd/wwc — ww-cgen frontend; bootstrap fixed point.
selfhost/cmd/6a — assembler; byte-identical to C 6a (test 991).
selfhost/cmd/6l — linker w/ archive (.a) support; byte-identical
to C 6l (test 992).
selfhost/cmd/ww — driver (build/run/version); byte-identical to
C ww (test 993).
make test: 15/15. make bootstrap: ww2.s == ww3.s, ww2.o == ww3.o,
ww2 == ww3 byte-identical, with the full ww-tooled chain.
This commit is contained in:
74
cmd/6l/l.h
Normal file
74
cmd/6l/l.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* l.h — 6l-private header. Loads relocatable ELF64 .o files (the
|
||||
* format produced by 6a) and links them into a static executable.
|
||||
*
|
||||
* No archives yet (phase 8). No dynamic linking ever.
|
||||
*/
|
||||
#ifndef SIX_L_H
|
||||
#define SIX_L_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef struct Lsym Lsym;
|
||||
typedef struct Lrel Lrel;
|
||||
typedef struct Lobj Lobj;
|
||||
typedef struct Lnk Lnk;
|
||||
|
||||
struct Lsym {
|
||||
const char *name;
|
||||
u64 val; /* offset within combined .text once linked */
|
||||
int defined; /* 1 if a Lobj defines this symbol */
|
||||
Lobj *owner;
|
||||
int idx_in_owner;
|
||||
Lsym *next;
|
||||
};
|
||||
|
||||
struct Lrel {
|
||||
u64 off; /* offset within combined .text */
|
||||
int kind; /* R_X86_64_* */
|
||||
Lsym *sym;
|
||||
i64 addend;
|
||||
Lrel *next;
|
||||
};
|
||||
|
||||
struct Lobj {
|
||||
const char *path;
|
||||
u8 *buf; /* mmapped or read-in object bytes */
|
||||
u64 len;
|
||||
u64 text_off; /* offset of .text in combined output */
|
||||
u64 text_size;
|
||||
Lobj *next;
|
||||
};
|
||||
|
||||
struct Lnk {
|
||||
Lobj *objs;
|
||||
Lsym *syms;
|
||||
Lrel *rels;
|
||||
u8 *text; /* combined .text */
|
||||
u64 textcap, textlen;
|
||||
int errs;
|
||||
};
|
||||
|
||||
/* obj.c */
|
||||
int l_load(Lnk*, const char *path);
|
||||
/* sym.c */
|
||||
Lsym *l_intern(Lnk*, const char *name);
|
||||
Lsym *l_lookup(Lnk*, const char *name);
|
||||
/* pass.c */
|
||||
int l_resolve(Lnk*);
|
||||
int l_relocate(Lnk*, u64 base);
|
||||
/* out.c */
|
||||
int l_emit_elf(Lnk*, FILE *out, u64 base, u64 entry);
|
||||
|
||||
#endif
|
||||
73
cmd/6l/main.c
Normal file
73
cmd/6l/main.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 6l — amd64 static linker. Reads relocatable ELF .o files, resolves,
|
||||
* relocates, writes a static ELF executable.
|
||||
*
|
||||
* 6l -o out 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>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *out = NULL;
|
||||
const char **inputs = calloc(argc, sizeof *inputs);
|
||||
int ninputs = 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 (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 file1.o [file2.o...]\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
324
cmd/6l/obj.c
Normal file
324
cmd/6l/obj.c
Normal file
@@ -0,0 +1,324 @@
|
||||
/*
|
||||
* obj.c — load an ELF64 relocatable object emitted by 6a, append its
|
||||
* .text bytes to the combined image, and pull its symbols and
|
||||
* relocations into the global tables (with offsets adjusted to the
|
||||
* combined section).
|
||||
*/
|
||||
#include "l.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ET_REL 1
|
||||
#define EM_X86_64 62
|
||||
|
||||
#define SHT_PROGBITS 1
|
||||
#define SHT_SYMTAB 2
|
||||
#define SHT_STRTAB 3
|
||||
#define SHT_RELA 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 sh_name, sh_type;
|
||||
u64 sh_flags, sh_addr, sh_offset, sh_size;
|
||||
u32 sh_link, sh_info;
|
||||
u64 sh_addralign, sh_entsize;
|
||||
} Shdr;
|
||||
|
||||
typedef struct {
|
||||
u32 st_name;
|
||||
u8 st_info, st_other;
|
||||
u16 st_shndx;
|
||||
u64 st_value, st_size;
|
||||
} Sym64;
|
||||
|
||||
typedef struct {
|
||||
u64 r_offset;
|
||||
u64 r_info;
|
||||
i64 r_addend;
|
||||
} Rela64;
|
||||
#pragma pack(pop)
|
||||
|
||||
#define ELF64_R_SYM(i) ((u32)((i) >> 32))
|
||||
#define ELF64_R_TYPE(i) ((u32)((i) & 0xffffffff))
|
||||
#define ELF64_ST_TYPE(i) ((i) & 0xf)
|
||||
#define ELF64_ST_BIND(i) ((i) >> 4)
|
||||
|
||||
static int
|
||||
read_all(const char *path, u8 **out, u64 *len)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (f == NULL) return -1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n < 0) { fclose(f); return -1; }
|
||||
u8 *b = malloc((size_t)n);
|
||||
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
|
||||
fclose(f);
|
||||
*out = b;
|
||||
*len = (u64)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
emit_text(Lnk *l, const u8 *src, u64 n)
|
||||
{
|
||||
if (l->textlen + n > l->textcap) {
|
||||
u64 nc = l->textcap ? l->textcap * 2 : 4096;
|
||||
while (nc < l->textlen + n) nc *= 2;
|
||||
l->text = realloc(l->text, nc);
|
||||
l->textcap = nc;
|
||||
}
|
||||
memcpy(l->text + l->textlen, src, n);
|
||||
l->textlen += n;
|
||||
}
|
||||
|
||||
/* Internal: load a single ELF .o image already in memory. The caller
|
||||
* gives us the bytes (we own them) and a path tag for diagnostics.
|
||||
* If the bytes look like an archive (magic "!<arch>\n") we recurse
|
||||
* over each member instead.
|
||||
*/
|
||||
static int load_image(Lnk *l, const char *path, u8 *buf, u64 len);
|
||||
|
||||
static u64
|
||||
ar_field(const u8 *p, int n)
|
||||
{
|
||||
/* decimal field, space-padded */
|
||||
u64 v = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (p[i] >= '0' && p[i] <= '9') v = v * 10 + (p[i] - '0');
|
||||
else if (p[i] == ' ') break;
|
||||
else if (p[i] == 0) break;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Read an ELF .o image's globally-defined symbol names without
|
||||
* actually appending it to the link. Returns a heap-allocated
|
||||
* NULL-terminated array; caller frees the array (not the strings,
|
||||
* which point into the .o image and must remain alive).
|
||||
*/
|
||||
static char **
|
||||
elf_globals(const u8 *buf, u64 len)
|
||||
{
|
||||
if (len < sizeof(Ehdr)) return NULL;
|
||||
Ehdr *eh = (Ehdr *)buf;
|
||||
if (memcmp(eh->e_ident, "\x7f""ELF", 4) != 0) return NULL;
|
||||
Shdr *sh = (Shdr *)(buf + eh->e_shoff);
|
||||
int idx_text = -1, idx_symtab = -1;
|
||||
const char *shstr = (const char *)(buf + sh[eh->e_shstrndx].sh_offset);
|
||||
for (u16 i = 0; i < eh->e_shnum; i++) {
|
||||
if (sh[i].sh_type == SHT_PROGBITS &&
|
||||
strcmp(shstr + sh[i].sh_name, ".text") == 0)
|
||||
idx_text = i;
|
||||
else if (sh[i].sh_type == SHT_SYMTAB)
|
||||
idx_symtab = i;
|
||||
}
|
||||
if (idx_text < 0 || idx_symtab < 0) return NULL;
|
||||
int idx_strtab = sh[idx_symtab].sh_link;
|
||||
Sym64 *symtab = (Sym64 *)(buf + sh[idx_symtab].sh_offset);
|
||||
u64 nsyms = sh[idx_symtab].sh_size / sizeof(Sym64);
|
||||
const char *str = (const char *)(buf + sh[idx_strtab].sh_offset);
|
||||
|
||||
char **out = calloc(nsyms + 1, sizeof *out);
|
||||
int n = 0;
|
||||
for (u64 i = 1; i < nsyms; i++) {
|
||||
if (symtab[i].st_shndx == 0) continue;
|
||||
if ((symtab[i].st_info >> 4) != 1) continue; /* STB_GLOBAL */
|
||||
if ((int)symtab[i].st_shndx != idx_text) continue;
|
||||
out[n++] = strdup(str + symtab[i].st_name);
|
||||
}
|
||||
out[n] = NULL;
|
||||
return out;
|
||||
}
|
||||
|
||||
typedef struct ArMember ArMember;
|
||||
struct ArMember {
|
||||
u8 *data; /* heap copy; freed if never loaded */
|
||||
u64 size;
|
||||
char **defs; /* NULL-terminated list of defined globals */
|
||||
int loaded;
|
||||
ArMember *next;
|
||||
};
|
||||
|
||||
static int
|
||||
member_defines_undef(Lnk *l, ArMember *m)
|
||||
{
|
||||
if (m->defs == NULL) return 0;
|
||||
for (int i = 0; m->defs[i]; i++) {
|
||||
Lsym *s = l_lookup(l, m->defs[i]);
|
||||
if (s != NULL && !s->defined) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
load_archive(Lnk *l, const char *path, u8 *buf, u64 len)
|
||||
{
|
||||
/* Pass 1: index members. We copy each member's bytes (cheap; few
|
||||
* tens of KB per stdlib module) so the archive buffer can be
|
||||
* freed once we're done indexing. */
|
||||
ArMember *head = NULL, *tail = NULL;
|
||||
u64 pos = 8; /* past "!<arch>\n" */
|
||||
while (pos + 60 <= len) {
|
||||
const u8 *hdr = buf + pos;
|
||||
u64 size = ar_field(hdr + 48, 10);
|
||||
u64 hdr_end = pos + 60;
|
||||
if (hdr_end + size > len) break;
|
||||
if (hdr[0] != '/' && hdr[0] != 0 && hdr[0] != ' ') {
|
||||
ArMember *m = calloc(1, sizeof *m);
|
||||
m->size = size;
|
||||
m->data = malloc((size_t)size);
|
||||
memcpy(m->data, buf + hdr_end, (size_t)size);
|
||||
m->defs = elf_globals(m->data, size);
|
||||
if (head == NULL) head = m;
|
||||
else tail->next = m;
|
||||
tail = m;
|
||||
}
|
||||
pos = hdr_end + size;
|
||||
if (size & 1) pos++;
|
||||
}
|
||||
free(buf);
|
||||
|
||||
/* Pass 2: iteratively pull members that define a currently-
|
||||
* undefined symbol. Each pull may introduce new undefs, so loop. */
|
||||
int changed = 1;
|
||||
while (changed) {
|
||||
changed = 0;
|
||||
for (ArMember *m = head; m; m = m->next) {
|
||||
if (m->loaded) continue;
|
||||
if (!member_defines_undef(l, m)) continue;
|
||||
u8 *copy = malloc((size_t)m->size);
|
||||
memcpy(copy, m->data, m->size);
|
||||
if (load_image(l, path, copy, m->size) == 0) {
|
||||
m->loaded = 1;
|
||||
changed = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Free unloaded members; loaded ones had their bytes consumed
|
||||
* by load_image (which took the copy). */
|
||||
while (head) {
|
||||
ArMember *next = head->next;
|
||||
free(head->data);
|
||||
if (head->defs) {
|
||||
for (int i = 0; head->defs[i]; i++) free(head->defs[i]);
|
||||
free(head->defs);
|
||||
}
|
||||
free(head);
|
||||
head = next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
l_load(Lnk *l, const char *path)
|
||||
{
|
||||
u8 *buf;
|
||||
u64 len;
|
||||
if (read_all(path, &buf, &len) < 0) return -1;
|
||||
if (len >= 8 && memcmp(buf, "!<arch>\n", 8) == 0)
|
||||
return load_archive(l, path, buf, len);
|
||||
return load_image(l, path, buf, len);
|
||||
}
|
||||
|
||||
static int
|
||||
load_image(Lnk *l, const char *path, u8 *buf, u64 len)
|
||||
{
|
||||
if (len < sizeof(Ehdr)) { free(buf); return -1; }
|
||||
Ehdr *eh = (Ehdr *)buf;
|
||||
if (memcmp(eh->e_ident, "\x7f""ELF", 4) != 0 || eh->e_ident[4] != 2
|
||||
|| eh->e_machine != EM_X86_64 || eh->e_type != ET_REL) {
|
||||
fprintf(stderr, "6l: %s: not an amd64 ELF64 relocatable\n", path);
|
||||
free(buf);
|
||||
return -1;
|
||||
}
|
||||
Shdr *sh = (Shdr *)(buf + eh->e_shoff);
|
||||
if (eh->e_shstrndx >= eh->e_shnum) { free(buf); return -1; }
|
||||
const char *shstr = (const char *)(buf + sh[eh->e_shstrndx].sh_offset);
|
||||
|
||||
/* find .text, .symtab, .strtab, .rela.text */
|
||||
int idx_text = -1, idx_symtab = -1, idx_strtab = -1, idx_rela = -1;
|
||||
for (u16 i = 0; i < eh->e_shnum; i++) {
|
||||
const char *nm = shstr + sh[i].sh_name;
|
||||
if (sh[i].sh_type == SHT_PROGBITS && strcmp(nm, ".text") == 0)
|
||||
idx_text = i;
|
||||
else if (sh[i].sh_type == SHT_SYMTAB)
|
||||
idx_symtab = i;
|
||||
else if (sh[i].sh_type == SHT_RELA && strcmp(nm, ".rela.text") == 0)
|
||||
idx_rela = i;
|
||||
}
|
||||
if (idx_text < 0 || idx_symtab < 0) {
|
||||
fprintf(stderr, "6l: %s: missing .text or .symtab\n", path);
|
||||
free(buf);
|
||||
return -1;
|
||||
}
|
||||
idx_strtab = sh[idx_symtab].sh_link;
|
||||
|
||||
Lobj *ob = calloc(1, sizeof *ob);
|
||||
ob->path = strdup(path);
|
||||
ob->buf = buf;
|
||||
ob->len = len;
|
||||
ob->text_off = l->textlen;
|
||||
ob->text_size = sh[idx_text].sh_size;
|
||||
ob->next = l->objs;
|
||||
l->objs = ob;
|
||||
|
||||
/* append .text */
|
||||
emit_text(l, buf + sh[idx_text].sh_offset, sh[idx_text].sh_size);
|
||||
|
||||
/* per-object: load symbols */
|
||||
Sym64 *symtab = (Sym64 *)(buf + sh[idx_symtab].sh_offset);
|
||||
u64 nsyms = sh[idx_symtab].sh_size / sizeof(Sym64);
|
||||
const char *str = (const char *)(buf + sh[idx_strtab].sh_offset);
|
||||
|
||||
/* map per-object sym index → global Lsym */
|
||||
Lsym **map = calloc(nsyms, sizeof *map);
|
||||
for (u64 i = 1; i < nsyms; i++) {
|
||||
const char *nm = str + symtab[i].st_name;
|
||||
if (nm[0] == '\0') continue;
|
||||
Lsym *gs = l_intern(l, nm);
|
||||
if (symtab[i].st_shndx != 0 /* SHN_UNDEF */
|
||||
&& symtab[i].st_shndx == idx_text) {
|
||||
if (gs->defined) {
|
||||
fprintf(stderr, "6l: %s: duplicate symbol %s\n",
|
||||
path, nm);
|
||||
l->errs++;
|
||||
} else {
|
||||
gs->defined = 1;
|
||||
gs->owner = ob;
|
||||
gs->idx_in_owner = (int)i;
|
||||
gs->val = ob->text_off + symtab[i].st_value;
|
||||
}
|
||||
}
|
||||
map[i] = gs;
|
||||
}
|
||||
|
||||
/* per-object: collect relocations */
|
||||
if (idx_rela >= 0) {
|
||||
Rela64 *rt = (Rela64 *)(buf + sh[idx_rela].sh_offset);
|
||||
u64 nrel = sh[idx_rela].sh_size / sizeof(Rela64);
|
||||
for (u64 i = 0; i < nrel; i++) {
|
||||
Lrel *r = calloc(1, sizeof *r);
|
||||
r->off = ob->text_off + rt[i].r_offset;
|
||||
r->kind = (int)ELF64_R_TYPE(rt[i].r_info);
|
||||
u32 sidx = ELF64_R_SYM(rt[i].r_info);
|
||||
r->sym = (sidx < nsyms) ? map[sidx] : NULL;
|
||||
r->addend = rt[i].r_addend;
|
||||
r->next = l->rels;
|
||||
l->rels = r;
|
||||
}
|
||||
}
|
||||
free(map);
|
||||
return 0;
|
||||
}
|
||||
89
cmd/6l/out.c
Normal file
89
cmd/6l/out.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
63
cmd/6l/pass.c
Normal file
63
cmd/6l/pass.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* pass.c — resolution + relocation. After all objects are loaded:
|
||||
*
|
||||
* l_resolve : check that every symbol referenced by a relocation
|
||||
* is defined somewhere. Errors get logged.
|
||||
* l_relocate: with the final virtual base address known, walk the
|
||||
* relocation list and patch the .text bytes in place.
|
||||
*
|
||||
* Supported relocation kinds: PC32 (2), PLT32 (4). Both are PC-relative
|
||||
* 32-bit displacements; for static linking PLT32 collapses to PC32.
|
||||
*/
|
||||
#include "l.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define R_X86_64_PC32 2
|
||||
#define R_X86_64_PLT32 4
|
||||
|
||||
int
|
||||
l_resolve(Lnk *l)
|
||||
{
|
||||
for (Lrel *r = l->rels; r; r = r->next) {
|
||||
if (r->sym == NULL) continue;
|
||||
if (!r->sym->defined) {
|
||||
fprintf(stderr, "6l: undefined reference to '%s'\n",
|
||||
r->sym->name);
|
||||
l->errs++;
|
||||
}
|
||||
}
|
||||
return l->errs;
|
||||
}
|
||||
|
||||
static void
|
||||
patch_u32(u8 *p, u32 v)
|
||||
{
|
||||
p[0] = (u8)(v & 0xff);
|
||||
p[1] = (u8)((v >> 8) & 0xff);
|
||||
p[2] = (u8)((v >> 16) & 0xff);
|
||||
p[3] = (u8)((v >> 24) & 0xff);
|
||||
}
|
||||
|
||||
int
|
||||
l_relocate(Lnk *l, u64 base)
|
||||
{
|
||||
for (Lrel *r = l->rels; r; r = r->next) {
|
||||
if (r->sym == NULL || !r->sym->defined) continue;
|
||||
switch (r->kind) {
|
||||
case R_X86_64_PC32:
|
||||
case R_X86_64_PLT32: {
|
||||
u64 site = base + r->off;
|
||||
i64 target = (i64)(base + r->sym->val);
|
||||
i64 rel = target - (i64)site + r->addend;
|
||||
patch_u32(l->text + r->off, (u32)(i32)rel);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fprintf(stderr, "6l: unsupported reloc kind %d\n",
|
||||
r->kind);
|
||||
l->errs++;
|
||||
}
|
||||
}
|
||||
return l->errs;
|
||||
}
|
||||
27
cmd/6l/sym.c
Normal file
27
cmd/6l/sym.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* sym.c — global symbol table for the linker. Plain singly-linked
|
||||
* list; usually a few hundred entries, hashing isn't worth it yet.
|
||||
*/
|
||||
#include "l.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Lsym *
|
||||
l_intern(Lnk *l, const char *name)
|
||||
{
|
||||
for (Lsym *s = l->syms; s; s = s->next)
|
||||
if (strcmp(s->name, name) == 0) return s;
|
||||
Lsym *s = calloc(1, sizeof *s);
|
||||
s->name = strdup(name);
|
||||
s->next = l->syms;
|
||||
l->syms = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
Lsym *
|
||||
l_lookup(Lnk *l, const char *name)
|
||||
{
|
||||
for (Lsym *s = l->syms; s; s = s->next)
|
||||
if (strcmp(s->name, name) == 0) return s;
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user