summaryrefslogtreecommitdiff
path: root/aout.myr
blob: 15a9c4ff287247718a9570c75947182f6fb40b0d (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
use std
use bio

use "types"

pkg =
	const emitaout	: (lnk : lnk# -> void)
;;

const emitaout = {lnk
	match bio.create(lnk.outf, bio.Wr, 0o755)
	| `std.Err e:
		std.fatal("could not open output: {}\n", e)
	| `std.Ok f:
		w32(f, magic(26))
		w32(f, lnk.text.len)
		w32(f, lnk.data.len)
		w32(f, (lnk.bss : uint32))
		w32(f, 0)	/* syms */
		w32(f, (lnk.entry : uint32))
		w32(f, 0)	/* pc/sp offset tab */
		w32(f, 0)	/* pc/lnum tab */
		wstr(f, lnk.text)
		wstr(f, lnk.data)
		bio.close(f)
	;;
}

const magic = {arch
	-> 0x00008000 | 4*arch*arch+7
}

const wstr	= {f, s; check(bio.write(f, s))}
const w32	= {f, v : uint32; check(bio.putbe32(f, v))}

generic check = {st
	match st
	| `std.Ok _:	/* nothing */
	| `std.Err e:	std.fatal("i/o error: {}\n", e)
	;;
}