summaryrefslogtreecommitdiff
path: root/main.myr
blob: 18a2ba5811b7e2d947ac46d0d83c88edfc07fd1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std
use bio

use "types"
use "elf"
use "macho"
use "aout"

const main = {args
	var outf, sys
	var lnk : lnk
	var cmd

	outf = "lnk.6"
	sys = ""
	cmd = std.optparse(args, &[
		.argdesc="objs...",
		.opts=[
			[.opt='o', .arg="out",
				.desc="output to file `out`",
				.dest=`std.Some &outf],
			[.opt='s', .arg="sys",
				.desc="emit for system `sys`",
				.dest=`std.Some &sys],
		][:]
	])

	lnk = [
		.obj=[][:],

		.text="",
		.data="",
		.rodata="",
		.bss = 0,
		.base=0x200028,

		.outf=outf,
		.sys=sys,
		.stab=std.mkht()
	]
	for a : cmd.args
		loadobj(&lnk, a)
	;;
	link(&lnk)
	/*
	  sadly, we need to brand binaries appropriately.
	  this means that simply knowing the format is
	  not enough, we need to know the target system.
	 */
	match sys
	| "openbsd":	emitelf(&lnk)
	| "freebsd":	emitelf(&lnk)
	| "netbsd":	emitelf(&lnk)
	| "linux":	emitelf(&lnk)
	| "osx":	emitmacho(&lnk)
	| "plan9":	emitaout(&lnk)
	| _:		std.fatal("unknown target {}\n", sys)
	;;
}

const link = {lnk
	for o : lnk.obj
		for s : o.sym
			match s.kind & Symkind
			| Symtxt:	addsym(lnk, &lnk.text, &s)
			| Symdat:	addsym(lnk, &lnk.data, &s)
			| Symro:	addsym(lnk, &lnk.rodata, &s)
			| Symbss:
				s.off = lnk.bss
				lnk.bss += s.len
			| _:	/* skip in output */
			;;
		;;
	;;
	for o : lnk.obj
		for r : o.rel
			relocate(lnk, r)
		;;
	;;
}

const relocate = {lnk, r
	var buf : byte[8]
	var addr, sz
	
	match std.htget(lnk.stab, r.sym)
	| `std.None:
		std.fatal("unknown symbol {}\n", r.sym)
	| `std.Some s:
		addr = lnk.base + s.off + r.arg
		match r.kind & Relkind
		| Rel8:		sz = 1
		| Rel16:	sz = 2
		| Rel32:	sz = 4
		| Rel64:	sz = 8
		| Relbase:
			match s.kind & Symkind
			| Symtxt:	lnk.cur = lnk.text[s.off:s.off+s.len]
			| Symdat:	lnk.cur = lnk.data[s.off:s.off+s.len]
			| Symro:	lnk.cur = lnk.rodata[s.off:s.off+s.len]
			| Symbss:	std.fatal("relocation in bss\n")
			| _: 		std.fatal("corrupt relocation\n")
			;;
			-> void
		| _:
			std.fatal("corrupt relocation\n")
		;;
		if addr > (1ul << 8*sz + lnk.base)
			std.fatal("relocation overflow in {}\n", s.name)
		;;
		if r.off + sz > lnk.cur.len
			std.fatal("out of bounds relocation in {}\n", s.name)
		;;
		/* FIXME: endianness */
		std.putle64(buf[:], addr)
		std.slcp(lnk.cur[r.off:r.off+sz], buf[:sz])
	;;
}

const addsym = {lnk, buf, sym
	match std.htget(lnk.stab, sym.name)
	| `std.Some old:
		if sym.kind & Symonce == 0 || old.kind & Symonce == 0
			std.fatal("duplicate symbol {}\n", sym.name)
		;;
		-> void
	| `std.None: /* ok */
	;;
	sym.off = buf#.len
	std.sljoin(buf, sym.buf)
	std.htput(lnk.stab, sym.name, sym)
	if std.sleq(sym.name, "_start") || std.sleq(sym.name, "start")
		lnk.entry = lnk.base + sym.off
	;;
}

const loadobj = {lnk, path
	var relsz, strsz, symsz, secsz, dstart, o
	var obj

	match std.slurp(path)
	| `std.Ok buf:	obj.buf = buf
	| `std.Err e:	std.fatal("could not open {}: {}\n", path, e)
	;;

	if !std.hasprefix(obj.buf, "uobj")
		std.fatal("unable to read {}: bad magic number\n", path)
	;;

	(strsz, o)  = r64(obj.buf, 4)
	(secsz, o)  = r64(obj.buf, o)
	(symsz, o)  = r64(obj.buf, o)
	(relsz, o)  = r64(obj.buf, o)
	dstart = o + strsz + symsz + relsz

	obj.str = obj.buf[o:o+strsz]
	o += strsz
	obj.sect = readsec(obj.buf[o:o+secsz], obj.str)
	o += secsz
	obj.sym = readsym(obj.buf[o:o+symsz], obj.str, obj.buf[dstart:])
	o += symsz
	obj.rel = readrel(obj.buf[o:o+relsz], obj.str)
	o += relsz
	
	obj.buf = obj.buf[o:]
	std.slpush(&lnk.obj, obj)
}

const readsec = {buf, strs
	var o, s, sec

	o = 0
	sec = [][:]
	while o != buf.len
		(s.name, o) = rname(buf, o, strs)
		(s.flg, o) = r32(buf, o)
		std.slpush(&sec, s)
	;;
	-> sec
}

const readsym = {hbuf, strs, dbuf
	var o, off, len
	var sym, s

	o = 0
	sym = [][:]
	while o != hbuf.len
		(s.name, o) = rname(hbuf, o, strs)
		(s.kind, o) = r32(hbuf, o)
		(off, o) = r64(hbuf, o)
		(len, o) = r64(hbuf, o)
		s.len = len
		if s.kind != Symbss
			s.buf = dbuf[off:off + len]
		;;
		std.slpush(&sym, s)
	;;
	-> sym
}


const readrel = {buf, strs
	var rel, r, o

	o = 0
	rel = [][:]
	while o != buf.len
		(r.sym, o) = rname(buf, o, strs)
		(r.kind, o) = r32(buf, o)
		(r.off, o) = r64(buf, o)
		if (r.kind & Relarg) != 0
			(r.arg, o) = r64(buf, o)
		else
			r.arg = 0
		;;
		std.slpush(&rel, r)
	;;
	-> rel
}

const rname = {buf, off, strs
	var sym, idx

	(idx, off) = r32(buf, off)
	sym = std.cstrconv(strs[idx:])
	-> (sym, off)
}

const r32 = {buf, off
	-> (std.getle32(buf[off:off+4]), off + 4)
}

const r64 = {buf, off
	-> (std.getle64(buf[off:off+8]), off + 8)
}