code wiki / (root) / nxld_main.nx

nxld_main.nx source

↩ module page · 50 lines · 1502 B

1// nxld_main.nx -- CLI driver for the sovereign linker. 2// 3// Usage: nxld <input.o> > <output.elf> 4// 5// Reads input.o via sys_read_file, opens it as an Elf object, 6// runs nxld_link() to produce a statically-linked RV64 Linux ELF 7// executable, writes the bytes to stdout. 8// 9// Exit codes: 10// 0 success 11// 1 no args 12// 2 read failed 13// 3 open failed (bad ELF magic / not RV64 / etc.) 14// 4 link failed 15// 16// Eventual goal: replace `riscv64-linux-gnu-ld` in verify.sh and 17// bootstrap_proof.sh with `nxld`. Removes one of the two 18// remaining gcc dependencies in the verify chain (the other 19// being gcc-as, which the next sovereign tool nxasm-cli replaces). 20 21import "syscalls.nx" 22import "nxld.nx" 23 24func main(argc: i64, argv: *i64) -> i64 { 25 if argc < 2 { 26 return __syscall(93, 1, 0, 0, 0, 0, 0) 27 } 28 let path: *u8 = (argv[1]) as *u8 29 30 let len_raw: *u8 = sys_mmap(16) 31 let len_out: *i64 = len_raw as *i64 32 *len_out = 0 33 let bytes: *u8 = sys_read_file(path, len_out) 34 if bytes == (0 as *u8) { 35 return __syscall(93, 2, 0, 0, 0, 0, 0) 36 } 37 38 let obj_raw: *u8 = sys_mmap(256) 39 let obj: *ElfObject = obj_raw as *ElfObject 40 let rc: i64 = nxld_open(obj, bytes, *len_out) 41 if rc != 0 { 42 return __syscall(93, 3, 0, 0, 0, 0, 0) 43 } 44 45 let n: i64 = nxld_link(obj, 1) 46 if n < 0 { 47 return __syscall(93, 4, 0, 0, 0, 0, 0) 48 } 49 return __syscall(93, 0, 0, 0, 0, 0, 0) 50}