code wiki / (root) / nx_nxld_main.nx

nx_nxld_main.nx source

↩ module page · 56 lines · 1709 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 21// nx_safety_envelope: 22// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 23// sil_target: SIL1 24// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 25// verdict: NOT_YET_EVALUATED 26 27import "nx_syscalls.nx" 28import "nx_nxld.nx" 29 30func main(argc: i64, argv: *i64) -> i64 { 31 if argc < 2 { 32 return __syscall(93, 1, 0, 0, 0, 0, 0) 33 } 34 let path: *u8 = (argv[1]) as *u8 35 36 let len_raw: *u8 = sys_mmap(16) 37 let len_out: *i64 = len_raw as *i64 38 *len_out = 0 39 let bytes: *u8 = sys_read_file(path, len_out) 40 if bytes == (0 as *u8) { 41 return __syscall(93, 2, 0, 0, 0, 0, 0) 42 } 43 44 let obj_raw: *u8 = sys_mmap(256) 45 let obj: *ElfObject = obj_raw as *ElfObject 46 let rc: i64 = nxld_open(obj, bytes, *len_out) 47 if rc != 0 { 48 return __syscall(93, 3, 0, 0, 0, 0, 0) 49 } 50 51 let n: i64 = nxld_link(obj, 1) 52 if n < 0 { 53 return __syscall(93, 4, 0, 0, 0, 0, 0) 54 } 55 return __syscall(93, 0, 0, 0, 0, 0, 0) 56}