fix(elf): centralize fixed header validation across consumers
Validate ELF identification/header versions and declared header bounds at the shared owner. Reuse parsed facts in target selection, Go, Dart and MIPS readers while preserving their stricter format policies and x32 support. Repair synthetic ELF fixtures and remove a superseded Go error-text assertion. Validation: 74 source tests, 202 filesystem/Go/MIPS boundary tests, typecheck and lint. No new real-provider claims.
This commit is contained in:
@@ -7,6 +7,7 @@ import type {
|
||||
UniversalMachOSlice,
|
||||
} from "./binaryTargetTypes.js";
|
||||
import { err, ok, type Result } from "./result.js";
|
||||
import { readElfFixedHeader } from "./elfFixedHeader.js";
|
||||
import { observeMzWindowsHeader, parseDosMzHeader } from "./dosMz.js";
|
||||
import { decodePeMachine } from "./peMachine.js";
|
||||
import {
|
||||
@@ -191,20 +192,14 @@ const parseElf = (
|
||||
bytes: Buffer,
|
||||
fileSize: number,
|
||||
): Result<ExecutableMetadata, string> => {
|
||||
if (bytes.length < 6) return err("truncated ELF header");
|
||||
if (bytes[4] !== 1 && bytes[4] !== 2) return err("unsupported ELF class");
|
||||
const little = bytes[5] === 1;
|
||||
if (!little && bytes[5] !== 2) return err("unsupported ELF endianness");
|
||||
const headerSize = bytes[4] === 1 ? 52 : 64;
|
||||
if (bytes.length < headerSize)
|
||||
return err(
|
||||
`truncated ELF header: an ELF${bytes[4] === 1 ? 32 : 64} header needs ${headerSize} bytes; the file has ${fileSize}`,
|
||||
);
|
||||
const fixedHeader = readElfFixedHeader(bytes, fileSize);
|
||||
if (!fixedHeader.ok) return fixedHeader;
|
||||
const { elfClass, byteOrder } = fixedHeader.value;
|
||||
const little = byteOrder === "little";
|
||||
const machine = little ? bytes.readUInt16LE(18) : bytes.readUInt16BE(18);
|
||||
const architecture = elfArchitecture(machine);
|
||||
if (architecture === undefined) return err("unsupported ELF architecture");
|
||||
if (architecture === "mips" || architecture === "riscv") {
|
||||
const elfClass = bytes[4] === 1 ? 32 : 64;
|
||||
const flagsOffset = elfClass === 32 ? 36 : 48;
|
||||
const metadata: ElfFamilyMetadata = {
|
||||
elfClass,
|
||||
|
||||
Reference in New Issue
Block a user