-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.nix
More file actions
50 lines (40 loc) · 1.36 KB
/
default.nix
File metadata and controls
50 lines (40 loc) · 1.36 KB
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
{ lib ? import ./lib/lib.nix { } }:
let
parser = import ./core/xml-parser.nix { inherit lib; };
# Use the bundled XML
specXml = builtins.readFile ./data/x86_64.xml;
spec = parser.parseInstructions specXml;
assembler = import ./core/assembler.nix { inherit lib spec; };
elf = import ./core/elf.nix { inherit lib; };
syntax = import ./frontend/syntax.nix { inherit lib; };
compiler = import ./frontend/compiler.nix { inherit lib; };
# (10 + 32) -> print "42\n" -> exit(42)
expr = [ "seq"
[ "lit" 10 ]
[ "lit" 32 ]
[ "add" ]
[ "print" ]
[ "exit" ]
];
asm = compiler.compile expr;
instructions = syntax.parseLines asm;
code = assembler.assemble instructions;
headerSize = 64 + 56;
fileSize = headerSize + (lib.length code);
vaddr = 4194304; # 0x400000
entry = vaddr + headerSize;
elfHeader = elf.makeElfHeader entry 64;
progHeader = elf.makeProgramHeader 0 vaddr fileSize;
allBytes = elfHeader ++ progHeader ++ code;
# Helper to convert byte list to hex string for emitter
toHex = bytes: lib.concatMapStrings (b: lib.fixedWidthString 2 "0" (lib.toHexString b)) bytes;
# Raw derivation
system = builtins.currentSystem or "x86_64-linux";
in
builtins.derivation {
name = "test-elf";
inherit system;
# Use our 360-byte hex emitter as the builder
builder = ./bin/emitter-bin;
args = [ (toHex allBytes) ];
}