mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* feat(domain): classify EM_PPC64 ELF in the powerpc family
An ELF with e_machine 21 was refused as "unsupported ELF architecture" before any provider was consulted, so every entry point answered a 64-bit PowerPC file with the generic file-format refusal.
The header reader now places EM_PPC64 in the powerpc family and keeps its class, byte order, e_type and e_flags as it does for EM_PPC. No provider lane admits these files: the Ghidra PowerPC gate already refuses ELF64 by naming PPC64, and Hopper, IDA and Kuna decline the family. Static ELF readers keep the name ppc64 for the machine.
Refs morluto/rea#12085.
* feat(domain): map the ppc64 reader name to the powerpc Evidence subject
Static readers report a 64-bit PowerPC ELF as ppc64, and the Evidence subject of those reports had no architecture because the name mapped to no family. With EM_PPC64 classified as powerpc, the subject now carries that family, as it does for ppc.
The Go verifier and the Go and Rust guides state the new mapping, and the boundary test keeps an unknown machine (s390) to show that unrecognized names still map to no family.
Refs morluto/rea#12085.
* revert(domain): keep ppc64 reader reports without a subject family
This reverts commit bbd7f7f661.
Evidence saved before the mapping carries no subject architecture for a ppc64 report. Views over those records compare the saved subject with the family the reader name maps to, so the mapping made them refuse records they accepted before. Nothing in this branch produces Evidence that needs the family, so the mapping can wait for a provider lane.
Refs morluto/rea#12085.
* fix(domain): classify EM_PPC64 only for an ELF64 header
Classifying machine 21 by number alone had two effects beyond target resolution. An ELF32 big-endian ET_EXEC header that claims EM_PPC64 became a powerpc target with class 32, which the 32-bit Ghidra lane admits because it sees only class, byte order, type and flags. And every static reader that maps a machine to a family started to put powerpc in its Evidence subject, which changes what views expect from records saved earlier.
elfArchitecture now takes the ELF class and names the family for machine 21 only when the caller commits ELF64. Header admission passes the class. Readers that pass only a machine keep the machine unclassified, so their reports, reader names and Evidence subjects are as they were.
Refs morluto/rea#12085.
152 lines
5.1 KiB
TypeScript
152 lines
5.1 KiB
TypeScript
import type {
|
|
BinaryArchitecture,
|
|
RawOnlyArchitecture,
|
|
} from "./binaryTargetTypes.js";
|
|
import { err, ok, type Result } from "./result.js";
|
|
|
|
/** CPU families an ELF e_machine can name; arm64_32 is a Mach-O-only CPU type. */
|
|
type ElfArchitecture = Exclude<
|
|
BinaryArchitecture,
|
|
RawOnlyArchitecture | "arm64_32"
|
|
>;
|
|
|
|
/** Fixed declarations after common ELF framing, before consumer admission. */
|
|
export interface ElfHeaderFacts {
|
|
readonly elfClass: 32 | 64;
|
|
readonly byteOrder: "little" | "big";
|
|
readonly headerSize: number;
|
|
readonly machine: number;
|
|
readonly type: number;
|
|
readonly flags: number;
|
|
readonly osAbi: number;
|
|
readonly version: number;
|
|
readonly abiVersion: number;
|
|
}
|
|
|
|
export interface ElfFixedHeader extends Omit<ElfHeaderFacts, "version"> {
|
|
readonly architecture: ElfArchitecture | null;
|
|
}
|
|
|
|
/**
|
|
* Decode common ELF framing and fixed fields; version and declared-size admission belong to consumers.
|
|
*
|
|
* `bytes` may be a bounded prefix. `totalBytes` is the containing file size;
|
|
* common framing requires a complete fixed header in both. The declared
|
|
* headerSize is retained without requiring or admitting that entire extent.
|
|
*/
|
|
export const readElfHeaderFacts = (
|
|
bytes: Buffer,
|
|
totalBytes = bytes.length,
|
|
): Result<ElfHeaderFacts, string> => {
|
|
if (bytes.length >= 4 && bytes.toString("hex", 0, 4) !== "7f454c46")
|
|
return err("not an ELF image");
|
|
if (bytes.length < 16) return err("ELF identification is truncated");
|
|
const classByte = bytes[4];
|
|
if (classByte !== 1 && classByte !== 2) return err("unsupported ELF class");
|
|
const dataByte = bytes[5];
|
|
if (dataByte !== 1 && dataByte !== 2)
|
|
return err("unsupported ELF endianness");
|
|
if (bytes[6] !== 1) return err("unsupported ELF identification version");
|
|
|
|
const elfClass = classByte === 1 ? 32 : 64;
|
|
const minimumSize = elfClass === 32 ? 52 : 64;
|
|
if (
|
|
!Number.isSafeInteger(totalBytes) ||
|
|
totalBytes < bytes.length ||
|
|
totalBytes < minimumSize ||
|
|
bytes.length < minimumSize
|
|
)
|
|
return err(
|
|
`truncated ELF header: an ELF${elfClass} header needs ${minimumSize} bytes; the file has ${totalBytes}`,
|
|
);
|
|
const littleEndian = dataByte === 1;
|
|
const readU16 = (offset: number): number =>
|
|
littleEndian ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset);
|
|
const readU32 = (offset: number): number =>
|
|
littleEndian ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
|
|
const machine = readU16(18);
|
|
const headerSize = readU16(elfClass === 32 ? 40 : 52);
|
|
return ok({
|
|
elfClass,
|
|
byteOrder: littleEndian ? "little" : "big",
|
|
headerSize,
|
|
machine,
|
|
type: readU16(16),
|
|
flags: readU32(elfClass === 32 ? 36 : 48),
|
|
osAbi: bytes[7]!,
|
|
version: readU32(20),
|
|
abiVersion: bytes[8]!,
|
|
});
|
|
};
|
|
|
|
/** Admit the ELF version and declared header extent for general analysis. */
|
|
export const readElfFixedHeader = (
|
|
bytes: Buffer,
|
|
totalBytes = bytes.length,
|
|
): Result<ElfFixedHeader, string> => {
|
|
const parsed = readElfHeaderFacts(bytes, totalBytes);
|
|
return parsed.ok
|
|
? admitElfFixedHeaderFacts(parsed.value, totalBytes)
|
|
: parsed;
|
|
};
|
|
|
|
/** Admit decoded facts without rereading or discarding their identity on failure. */
|
|
export const admitElfFixedHeaderFacts = (
|
|
facts: ElfHeaderFacts,
|
|
totalBytes: number,
|
|
): Result<ElfFixedHeader, string> => {
|
|
// NVIDIA CUBIN uses ELFOSABI_CUDA and a vendor ABI version in e_version.
|
|
if (facts.version !== 1 && !(facts.machine === 190 && facts.osAbi === 0x33))
|
|
return err("unsupported ELF header version");
|
|
const minimumSize = facts.elfClass === 32 ? 52 : 64;
|
|
if (facts.headerSize < minimumSize || facts.headerSize > totalBytes)
|
|
return err("invalid ELF header size");
|
|
return ok({
|
|
elfClass: facts.elfClass,
|
|
byteOrder: facts.byteOrder,
|
|
headerSize: facts.headerSize,
|
|
machine: facts.machine,
|
|
type: facts.type,
|
|
flags: facts.flags,
|
|
osAbi: facts.osAbi,
|
|
abiVersion: facts.abiVersion,
|
|
architecture: elfArchitecture(facts.machine, facts.elfClass) ?? null,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* The CPU family an ELF machine names. A machine that is a family only for one
|
|
* ELF class needs that class from the caller; without it the machine stays
|
|
* unclassified.
|
|
*/
|
|
export const elfArchitecture = (
|
|
machine: number,
|
|
elfClass?: 32 | 64,
|
|
): ElfArchitecture | undefined => {
|
|
switch (machine) {
|
|
case 3:
|
|
return "x86";
|
|
case 62:
|
|
return "x86_64";
|
|
case 8: // EM_MIPS: ISA, ABI and byte order remain separate ELF facts.
|
|
return "mips";
|
|
case 20: // EM_PPC; byte order and processor variant remain ELF declarations.
|
|
return "powerpc";
|
|
case 21:
|
|
// EM_PPC64 objects are ELF64. An ELF32 header that claims it would pass
|
|
// for EM_PPC downstream, where only class and byte order remain.
|
|
return elfClass === 64 ? "powerpc" : undefined;
|
|
case 40:
|
|
return "arm";
|
|
case 164: // EM_HEXAGON: Qualcomm Hexagon DSP (QDSP6).
|
|
return "hexagon";
|
|
case 94: // EM_XTENSA: Tensilica Xtensa.
|
|
return "xtensa";
|
|
case 183:
|
|
return "arm64";
|
|
case 243: // EM_RISCV: XLEN, byte order and e_flags remain separate ELF facts.
|
|
return "riscv";
|
|
}
|
|
return undefined;
|
|
};
|