Files
reverse-engineering-anything/src/native/metadata/ElfSectionNameTable.ts
T
morluto 99d2d7ad22 fix(parsers): retain independent facts and boundary identity
Share bounded ELF section-name decoding across native and note readers; retain read-only ranges and coverage reasons when names are unknown. Preserve ELF symbol-version bytes, dyld-bound method uncertainty, partial gRPC-Web trailers, and per-parent model identity checks. Compare GPU signatures as bytes and require a PTX module header. Share selected-path errno classification across EVM and BEAM.

Validation: 397 shared-parser tests passed (one existing skip), 5 MCP schema tests passed, 14 EVM error-mapping tests passed, and 23 protobuf CLI/MCP matrix cases passed across focused runs. Build, typecheck/lint, docs, formatting, and 2134-file discovery passed. Full EVM provider workflow remains locally unverified on Darwin arm64; format controls are synthetic.

Fixes #6738
Fixes #6737
Fixes #6745
Fixes #6740
Fixes #6749
Fixes #6747
Fixes #6739
2026-10-23 22:18:43 +08:00

74 lines
2.2 KiB
TypeScript

import { inflateSync } from "node:zlib";
const SHF_COMPRESSED = 0x800n;
const ELFCOMPRESS_ZLIB = 1;
export type ElfSectionNameTableResult =
| {
readonly state: "available";
readonly bytes: Buffer;
readonly compressed: boolean;
}
| { readonly state: "unsupported"; readonly compressionType: number }
| { readonly state: "malformed"; readonly message: string }
| {
readonly state: "limit";
readonly requiredBytes: bigint;
readonly maximumBytes: number;
};
/** Decode the ELF section-name table once for consumers that need its names. */
export const decodeElfSectionNameTable = (
bytes: Buffer,
flags: bigint,
bits: 32 | 64,
little: boolean,
maximumDecodedBytes: number,
): ElfSectionNameTableResult => {
if ((flags & SHF_COMPRESSED) === 0n)
return { state: "available", bytes, compressed: false };
const headerBytes = bits === 64 ? 24 : 12;
if (bytes.length < headerBytes)
return {
state: "malformed",
message: "ELF compressed section-name table header is truncated",
};
const compressionType = little
? bytes.readUInt32LE(0)
: bytes.readUInt32BE(0);
if (compressionType !== ELFCOMPRESS_ZLIB)
return { state: "unsupported", compressionType };
const declaredSize =
bits === 64
? little
? bytes.readBigUInt64LE(8)
: bytes.readBigUInt64BE(8)
: BigInt(little ? bytes.readUInt32LE(4) : bytes.readUInt32BE(4));
if (declaredSize > BigInt(maximumDecodedBytes))
return {
state: "limit",
requiredBytes: declaredSize,
maximumBytes: maximumDecodedBytes,
};
const expected = Number(declaredSize);
let decoded: Buffer;
try {
decoded = inflateSync(bytes.subarray(headerBytes), {
maxOutputLength: Math.max(1, expected),
});
} catch (cause: unknown) {
return {
state: "malformed",
message: `ELF compressed section-name table could not be decompressed: ${cause instanceof Error ? cause.message : "invalid zlib data"}`,
};
}
if (decoded.length !== expected)
return {
state: "malformed",
message: "ELF compressed section-name table size is inconsistent",
};
return { state: "available", bytes: decoded, compressed: true };
};