Files
client/scripts/elf-pagesize.mjs
T
2026-10-24 03:43:41 +03:00

107 lines
4.3 KiB
JavaScript

// 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 <file-or-dir> [...] [--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);
}
}
function walk(p, out) {
const st = statSync(p, { throwIfNoEntry: false });
if (!st) 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));
return {
file,
machine: machine === 0xb7 ? 'aarch64' : machine === 0x3e ? 'x86-64' : `0x${machine.toString(16)}`,
minLoadAlign: loads.length ? Math.min(...loads.map((l) => l.align)) : null,
pageOk: loads.every((l) => l.ok),
needed,
maxGlibc: glibc.at(-1) ?? null,
};
}
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.pageOk ? 'ok ' : 'FAIL'} ${r.machine ?? ''} align=${r.minLoadAlign} glibc=${r.maxGlibc ?? '-'} ${path.relative(process.cwd(), r.file)}${r.needed?.length ? ` needs ${r.needed.join(',')}` : ''}${r.error ? ` (${r.error})` : ''}`);
}
console.log(`${results.length} ELF files, ${bad} incompatible with ${PAGE}-byte pages`);
if (jsonOut) writeFileSync(jsonOut, JSON.stringify({ page: PAGE, results }, null, 2));
process.exit(bad ? 1 : 0);