// Static check that ELF64 files can be mapped on kernels with large pages (Asahi Linux uses 16 KiB): // every PT_LOAD must have p_align >= PAGE and p_offset ≡ p_vaddr (mod PAGE). // Also lists DT_NEEDED libraries and the highest GLIBC_x.y symbol version referenced. // // node scripts/elf-pagesize.mjs [...] [--page 16384] [--json out.json] import { readFileSync, readdirSync, statSync, writeFileSync, openSync, readSync, closeSync } from 'node:fs'; import path from 'node:path'; const argv = process.argv.slice(2); const pi = argv.indexOf('--page'); const PAGE = pi >= 0 ? Number(argv[pi + 1]) : 16384; const ji = argv.indexOf('--json'); const jsonOut = ji >= 0 ? argv[ji + 1] : null; const valueIdx = new Set([pi, ji].filter((i) => i >= 0).map((i) => i + 1)); const inputs = argv.filter((a, i) => !a.startsWith('--') && !valueIdx.has(i)); function isElf(file) { const fd = openSync(file, 'r'); try { const b = Buffer.alloc(4); return readSync(fd, b, 0, 4, 0) === 4 && b.readUInt32BE(0) === 0x7f454c46; } finally { closeSync(fd); } } const missing = []; function walk(p, out) { const st = statSync(p, { throwIfNoEntry: false }); if (!st) { missing.push(p); return; } if (st.isDirectory()) for (const e of readdirSync(p)) walk(path.join(p, e), out); else if (st.isFile() && st.size > 64 && isElf(p)) out.push(p); } function inspect(file) { const b = readFileSync(file); if (b[4] !== 2 || b[5] !== 1) return { file, error: 'not ELF64 little-endian' }; const machine = b.readUInt16LE(18); const phoff = Number(b.readBigUInt64LE(32)); const phentsize = b.readUInt16LE(54); const phnum = b.readUInt16LE(56); const shoff = Number(b.readBigUInt64LE(40)); const shentsize = b.readUInt16LE(58); const shnum = b.readUInt16LE(60); const loads = []; let dynamic = null; for (let i = 0; i < phnum; i += 1) { const o = phoff + i * phentsize; const type = b.readUInt32LE(o); const offset = b.readBigUInt64LE(o + 8); const vaddr = b.readBigUInt64LE(o + 16); const filesz = b.readBigUInt64LE(o + 32); const align = b.readBigUInt64LE(o + 48); if (type === 1) { const congruent = offset % BigInt(PAGE) === vaddr % BigInt(PAGE); loads.push({ align: Number(align), ok: align >= BigInt(PAGE) && congruent, congruent }); } else if (type === 2) { dynamic = { offset: Number(offset), size: Number(filesz) }; } } // DT_NEEDED via .dynamic + .dynstr (section headers), GLIBC versions via raw string scan. const needed = []; if (dynamic && shoff && shnum) { let dynstr = null; for (let i = 0; i < shnum; i += 1) { const o = shoff + i * shentsize; const type = b.readUInt32LE(o + 4); const link = b.readUInt32LE(o + 40); if (type === 6) { const lo = shoff + link * shentsize; dynstr = Number(b.readBigUInt64LE(lo + 24)); } } if (dynstr !== null) { for (let o = dynamic.offset; o + 16 <= dynamic.offset + dynamic.size; o += 16) { const tag = b.readBigInt64LE(o); if (tag === 0n) break; if (tag === 1n) { const start = dynstr + Number(b.readBigUInt64LE(o + 8)); needed.push(b.toString('latin1', start, b.indexOf(0, start))); } } } } const glibc = [...new Set(b.toString('latin1').match(/GLIBC_[0-9]+(\.[0-9]+)+/g) ?? [])] .sort((x, y) => x.slice(6).split('.').map(Number).reduce((acc, n, i) => acc || n - (y.slice(6).split('.').map(Number)[i] ?? 0), 0)); const type = b.readUInt16LE(16); const loadable = type === 2 || type === 3; // ET_EXEC, ET_DYN if (loadable && loads.length === 0) return { file, error: 'executable/shared object without PT_LOAD segments' }; return { file, type: type === 2 ? 'exec' : type === 3 ? 'dyn' : type === 1 ? 'rel' : `type-${type}`, machine: machine === 0xb7 ? 'aarch64' : machine === 0x3e ? 'x86-64' : `0x${machine.toString(16)}`, minLoadAlign: loads.length ? Math.min(...loads.map((l) => l.align)) : null, // Relocatable objects are never mapped by the loader; everything loadable must satisfy every PT_LOAD. pageOk: !loadable || loads.every((l) => l.ok), needed, maxGlibc: glibc.at(-1) ?? null, }; } if (!Number.isInteger(PAGE) || PAGE <= 0 || (PAGE & (PAGE - 1)) !== 0) { console.error(`--page must be a positive power of two, got ${pi >= 0 ? argv[pi + 1] : PAGE}`); process.exit(2); } if (inputs.length === 0) { console.error('no input path given'); process.exit(2); } const files = []; for (const input of inputs) walk(path.resolve(input), files); const results = files.map(inspect); let bad = 0; for (const r of results) { if (r.error || !r.pageOk) bad += 1; console.log(`${r.error || !r.pageOk ? 'FAIL' : 'ok '} ${r.machine ?? ''} ${r.type ?? ''} align=${r.minLoadAlign ?? '-'} glibc=${r.maxGlibc ?? '-'} ${path.relative(process.cwd(), r.file)}${r.needed?.length ? ` needs ${r.needed.join(',')}` : ''}${r.error ? ` (${r.error})` : ''}`); } for (const m of missing) console.error(`FAIL missing input: ${m}`); if (files.length === 0) console.error('FAIL no ELF files found in the given inputs'); console.log(`${results.length} ELF files, ${bad} incompatible with ${PAGE}-byte pages, ${missing.length} missing inputs`); if (jsonOut) writeFileSync(jsonOut, JSON.stringify({ page: PAGE, missing, results }, null, 2)); process.exit(bad || missing.length || files.length === 0 ? 1 : 0);