code wiki / _hdl_build / nx_byte_diff.nx

nx_byte_diff.nx source

↩ module page · 60 lines · 2546 B

1// nx_byte_diff.nx -- item-1 of the PM plan (x86 toolchain byte-exact differential vs gcc): compare the 2// .text (machine code) of two ELFs built from the SAME .s -- one by sovereign nxasm_x86, one by gcc/gas. 3// Both consume identical assembly, so a byte-exact .text proves we replicate gas's encoding exactly = 4// strong triangulated S-class evidence; a mismatch localizes the first differing byte to inspect. Reuses 5// the sovereign ELF reader. license_tier: ORIGINAL 6 7import "../nx_elf_read.nx" 8import "nx_syscalls.nx" 9 10struct BdText { found: i64, off: i64, size: i64 } 11struct BdCmp { okA: i64, okB: i64, sizeA: i64, sizeB: i64, common_match: i64, first_mismatch: i64, exact: i64 } 12 13func bd_streq(a: *u8, b: *u8) -> i64 { 14 var i: i64 = 0 15 while b[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 16 if a[i] != (0 as u8) { return 0 } 17 return 1 18} 19 20// locate the .text section in a parsed ELF. 21func bd_find_text(buf: *u8, h: *NxElfHeader) -> *BdText { 22 let r: *BdText = sys_mmap(32) as *BdText 23 r.found = 0; r.off = 0; r.size = 0 24 if h.valid != 1 { return r } 25 if h.e_shnum <= 0 { return r } 26 let shstr: *NxElfShdr = nx_elf_read_shdr(buf, h, h.e_shstrndx) 27 let strtab_off: i64 = shstr.sh_offset 28 var i: i64 = 0 29 while i < h.e_shnum { 30 let s: *NxElfShdr = nx_elf_read_shdr(buf, h, i) 31 let nm: *u8 = nx_elf_strtab_at(buf, strtab_off, s.sh_name) 32 if bd_streq(nm, ".text" as *u8) == 1 { r.found = 1; r.off = s.sh_offset; r.size = s.sh_size; return r } 33 i = i + 1 34 } 35 return r 36} 37 38// compare .text of two ELF buffers. 39func bd_compare(bufA: *u8, lenA: i64, bufB: *u8, lenB: i64) -> *BdCmp { 40 let c: *BdCmp = sys_mmap(64) as *BdCmp 41 c.okA = 0; c.okB = 0; c.sizeA = 0; c.sizeB = 0; c.common_match = 0; c.first_mismatch = 0 - 1; c.exact = 0 42 let hA: *NxElfHeader = nx_elf_parse_header(bufA, lenA) 43 let hB: *NxElfHeader = nx_elf_parse_header(bufB, lenB) 44 let tA: *BdText = bd_find_text(bufA, hA) 45 let tB: *BdText = bd_find_text(bufB, hB) 46 c.okA = tA.found; c.okB = tB.found 47 if tA.found == 0 { return c } 48 if tB.found == 0 { return c } 49 c.sizeA = tA.size; c.sizeB = tB.size 50 var lim: i64 = tA.size 51 if tB.size < lim { lim = tB.size } 52 var i: i64 = 0 53 while i < lim { 54 if bufA[tA.off + i] == bufB[tB.off + i] { c.common_match = c.common_match + 1 } 55 else { if c.first_mismatch < 0 { c.first_mismatch = i } } 56 i = i + 1 57 } 58 if tA.size == tB.size { if c.common_match == tA.size { c.exact = 1 } } 59 return c 60}