nx_pe_compile_win_sw.nx
buildroot/runtime/nx_pe_compile_win_sw.nx
about
nx_pe_compile_win_sw.nx -- W3b-4: an organ's REAL sys_write reaches WriteFile, fd-routed.
W3b-3 (nx_pe_compile_win_write) proved a CUSTOM shim (nx_win_write) reaches WriteFile. This rung
redirects the GENUINE nx_syscalls name `sys_write` -- VERIFIED the compiler emits a direct
`call sys_write` (SysV rdi=fd, rsi=buf, rdx=n), the same redirectable label as any function. So an
organ that calls the real `sys_write(fd, buf, n)` prints natively on Windows, with fd routed to the
correct console handle (1->STD_OUTPUT, 2->STD_ERROR). No compiler or assembler edits.
thunk (SysV in: rdi=fd, rsi=buf, rdx=n):
sub rsp, 0x38
mov [rsp+0x30], rdx ; save n (rdx MS-x64 VOLATILE)
mov ecx, -11 ; default STD_OUTPUT_HANDLE
cmp edi, 2 / jne +5 / mov ecx, -12 ; fd==2 -> STD_ERROR_HANDLE
call [rip+GetStdHandle] ; rax=handle; rsi(buf) survives (MS-x64 non-volatile)
mov rcx, rax / mov rdx, rsi / mov r8,[rsp+0x30] / lea r9,[rsp+0x28] / mov qword[rsp+0x20],0
call [rip+WriteFile]
add rsp, 0x38 / ret
NO-FALSE-GREEN: an organ defining the canonical `sys_write` (body = __syscall(64,...), overwritten)
and calling sys_write(FD, "...", N). FD=1 -> the bytes land on STDOUT (stderr empty); FD=2 -> on
STDERR (stdout empty). Capturing the two streams separately proves the fd arg routes the handle
(a hardcoded-stdout thunk would fail FD=2). Tamper (corrupt WriteFile import) -> 0xC0000139.
PIPELINE (build WSL sovereign, run native): src.nx
-> ./_offc/nx_compile_x86_native.elf <src> > /tmp/nxwin.s
-> ./_offc/nx_sov_build_run.elf nx_pe_compile_win_sw (reads /tmp/nxwin.s)
-> _offc/nx_win_compiled_sw.exe -> run native on Windows 11.
HONEST SCOPE: redirecting sys_write makes WRITE-to-console work for an organ's genuine sys_write
call. A full I/O organ also calls sys_mmap/sys_exit/sys_read/etc. -- those still emit Linux
`syscall` (dead on Windows) and need the same label-redirect (if bodied) or a syscall-dispatch
layer. So this is NOT yet "any organ runs"; it is "the real sys_write name lowers to WriteFile,
fd-routed". Proven with a local sys_write definition; the imported nx_syscalls.sys_write is the
SAME label -> same redirect. Replicates keystone assemble (reuses axc_pass; no shared-assembler
edit). lineage_id: substrate_pe_compile_win_sw_v1
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnxasm_x86.nxnx_pe_writer.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 41 | const NXSW_CODE_CAP: i64 = 1048576 |
| 42 | const NXSW_FILE_SIZE: i64 = 0x800 |
| 43 | const NXSW_STUB_LEN: i64 = 18 |
| 44 | const NXSW_THUNK_LEN: i64 = 66 // fd-routed GetStdHandle+WriteFile returning thunk |
| 45 | const NXSW_TEXT_CAP: i64 = 0x200 |
| 46 | const NXSW_RVA_TEXT: i64 = 0x1000 |
| 47 | const NXSW_RVA_RDATA: i64 = 0x2000 |
| 48 | const NXSW_RVA_IDATA: i64 = 0x3000 |
| 49 | const NXSW_FOFF_TEXT: i64 = 0x200 |
| 50 | const NXSW_FOFF_RDATA:i64 = 0x400 |
| 51 | const NXSW_FOFF_IDATA:i64 = 0x600 |
| 52 | const NXSW_IAT_GSH: i64 = 0x3048 |
| 53 | const NXSW_IAT_WF: i64 = 0x3050 |
| 54 | const NXSW_IAT_EXIT: i64 = 0x3058 |
functions
| 57 | func nxsw_assemble(src: *u8, n: i64, out: *u8, out_cap: i64, p_main: *i64, p_sw: *i64) -> i64 |
| 94 | func nxsw_emit_pe(buf: *u8, code: *u8, code_len: i64, main_off: i64, sw_off: i64) -> i64 |
| 200 | func main() -> i64 |