summaryrefslogtreecommitdiff
path: root/main.myr
blob: 7cb835ae93d805f15b3789f76d161319240374b0 (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
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,

		.outf=outf,
		.sys=sys,
		.stab=std.mkht(std.strhash, std.streq)
	]
	for a : cmd.args
		loadobj(&lnk, a)
	;;
	link(&lnk)
	std.put("text: {}\n", lnk.text)
	match sys
	/* generic binary targets */
	| "elf":	emitelf(&lnk)
	| "macho":	emitmacho(&lnk)
	| "aout":	emitaout(&lnk)

	/* and figure it out by system */
	| "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 addsym = {lnk, buf, sym
	match std.htget(lnk.stab, sym.name)
	| `std.None:
		sym.off = buf#.len
		std.sljoin(buf, sym.buf)
		std.htput(lnk.stab, sym.name, sym)
	| `std.Some old:
		if sym.kind & Symonce == 0 || old.kind & Symonce == 0
			std.fatal("duplicate symbol {}\n", sym.name)
		;;
	;;
}

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:
		std.put("relocating: {}\n", r)
		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)
			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)
		;;
		std.put("addr: {}\n", addr)
		/* FIXME: endianness */
		std.putle64(buf[:], addr)
		std.slcp(lnk.cur[r.off:r.off+sz], buf[:sz])
	;;
}

const loadobj = {lnk, path
	var rel, str, sym, 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)
	;;

	(str, o)  = r64(obj.buf, 4)
	(rel, o)  = r64(obj.buf, o)
	(sym, o)  = r64(obj.buf, o)
	obj.str = obj.buf[str:rel]
	obj.rel = readrel(obj.buf[rel:sym], obj.str)
	obj.sym = readsym(obj.buf[o:str], o, obj.str, obj.buf)

	std.slpush(&lnk.obj, obj)
}

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

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

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

	o = 0
	sym = [][:]
	while o != hbuf.len
		(s.name, o) = rsym(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 rsym = {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)
}