mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
* feat(ghidra): admit raw 8051 images Add 8051 as a raw-only architecture: byte-aligned entry, mapping within 0x0000..0xffff, analyzed by Ghidra with 8051:BE:16:default in the CODE space. Hopper, IDA, Kuna and Windows Ghidra P0 keep refusing it. Document the lane, including that bank-switch thunks and Keil ?C?CCASE switch arms are not followed by auto-analysis and need REA_GHIDRA_SEED_FILE entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(mcp): update the advertised open_binary schema summary for raw 8051 The architecture enum gained 8051, so the committed input schema digest for open_binary changed. Regenerated with `npm run verify:advertised-schemas -- --update-summary`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(ghidra): pin the 8051 CODE binding and correct the 8051 docs - Assert the 8051 language binding (8051:BE:16:default, CODE) and that load-image attestation requires the CODE default space; replacing CODE with ram now fails three tests. - Assert that Windows Ghidra P0 refuses raw 8051. - Derive the raw-binary architecture refusal from BINARY_ARCHITECTURES with Intl.ListFormat; the wording, and so the admission grid digest, is unchanged. - Docs: list the memory blocks Ghidra really creates for 8051 (no external RAM block), explain bank addressing for paged banks and the entry-decode requirement, state when the seed file is read, and add 8051 to the n64 guide's family list. Fix a duplicated "and" in the generic-language limitation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Alex Petrochenko <5717309+petrochen@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
515 lines
18 KiB
TypeScript
515 lines
18 KiB
TypeScript
import type { AnalysisDatabaseEngine } from "./analysisDatabase.js";
|
|
import type { DolHeader } from "./gamecubeDol.js";
|
|
import type {
|
|
ArchitectureVariant,
|
|
RawEntryIsaMode,
|
|
} from "./architectureVariant.js";
|
|
import type { TargetFormatsOfKind } from "./targetFormats.js";
|
|
import type { BundleExecutableSelection } from "./apple/bundleExecutableProvenance.js";
|
|
import {
|
|
EF_MIPS_32BITMODE,
|
|
EF_MIPS_ABI,
|
|
EF_MIPS_ABI2,
|
|
EF_MIPS_ABI_ON32,
|
|
EF_MIPS_ARCH,
|
|
EF_MIPS_ARCH_1,
|
|
EF_MIPS_ARCH_32,
|
|
EF_MIPS_ARCH_32R2,
|
|
EF_MIPS_ARCH_ASE,
|
|
EF_MIPS_FP64,
|
|
EF_MIPS_MACH,
|
|
EF_MIPS_NAN2008,
|
|
} from "./mipsElfFlags.js";
|
|
|
|
/** Provider-neutral CPU families detected from headers or explicitly selected raw loads. */
|
|
export const BINARY_ARCHITECTURES = [
|
|
"x86",
|
|
"x86_64",
|
|
"arm",
|
|
"arm64",
|
|
"arm64_32",
|
|
"mips",
|
|
"6502",
|
|
"riscv",
|
|
"powerpc",
|
|
"m68k",
|
|
"hexagon",
|
|
"xtensa",
|
|
"8051",
|
|
] as const;
|
|
export type BinaryArchitecture = (typeof BINARY_ARCHITECTURES)[number];
|
|
|
|
/**
|
|
* Families admitted only by explicit raw-binary selection. Header detection
|
|
* never yields them, so detected targets and Evidence subjects exclude them.
|
|
*/
|
|
export const RAW_ONLY_ARCHITECTURES = [
|
|
"6502",
|
|
"m68k",
|
|
"8051",
|
|
] as const satisfies readonly BinaryArchitecture[];
|
|
export type RawOnlyArchitecture = (typeof RAW_ONLY_ARCHITECTURES)[number];
|
|
|
|
/**
|
|
* ELF header facts for a family whose ISA and ABI are not established by the
|
|
* family name alone. Consumers admit a lane from these declarations rather than
|
|
* re-reading the header.
|
|
*/
|
|
export interface ElfFamilyMetadata {
|
|
readonly elfClass: 32 | 64;
|
|
readonly byteOrder: "little" | "big";
|
|
readonly type: number;
|
|
readonly flags: number;
|
|
readonly hasInterpreter?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Admitted headerless load. File offset 0 maps at `baseAddress` and analysis
|
|
* starts at `entryPoint`; consumers read each role here instead of deriving
|
|
* one from the other.
|
|
*/
|
|
export interface RawBinaryLoad {
|
|
/** Canonical address of file offset 0. */
|
|
readonly baseAddress: string;
|
|
/** Caller-named profile variant; absent for the family's generic decoder. */
|
|
readonly architectureVariant?: ArchitectureVariant;
|
|
/** Canonical code entry. */
|
|
readonly entryPoint: string;
|
|
/** Instruction set at the entry, when the variant fixes one. */
|
|
readonly entryIsaMode?: RawEntryIsaMode;
|
|
readonly entrySource: RawBinaryEntrySource;
|
|
}
|
|
|
|
/**
|
|
* Whether the caller chose the entry or it was read from a vector table.
|
|
* Only canonical values are kept: caller spellings are not facts about the
|
|
* load, and a session reused across spellings must report the same load.
|
|
*/
|
|
export type RawBinaryEntrySource =
|
|
| { readonly kind: "caller" }
|
|
| {
|
|
readonly kind: "vector-table";
|
|
/** Canonical address of word 0. */
|
|
readonly address: string;
|
|
/** Word 1 as read, including its Thumb bit. */
|
|
readonly resetVector: string;
|
|
/** Word 0 as read; observed and not validated. */
|
|
readonly initialStackPointer: string;
|
|
};
|
|
|
|
/** Prefix shared by CodeView RVA mapping and both payload readers. */
|
|
export const PE_CODEVIEW_PREFIX_BYTES = 4096;
|
|
|
|
export type PeCodeViewPath =
|
|
| {
|
|
readonly pdbPath: string;
|
|
readonly pdbFileName: string;
|
|
}
|
|
| {
|
|
readonly pdbPath: null;
|
|
readonly pdbFileName: null;
|
|
/** Exact bytes before the record's NUL terminator, with unknown text decoding. */
|
|
readonly pdbPathBytesHex: string;
|
|
};
|
|
|
|
/** Referenced CodeView debug symbols from a PE debug directory. */
|
|
export type PeCodeViewRecord = PeCodeViewPath & {
|
|
readonly guid: string | null;
|
|
readonly age: number;
|
|
};
|
|
|
|
/** Families whose parsed target variant carries `ElfFamilyMetadata`. */
|
|
export const ELF_METADATA_ARCHITECTURES = [
|
|
"mips",
|
|
"riscv",
|
|
"powerpc",
|
|
] as const satisfies readonly BinaryArchitecture[];
|
|
|
|
/** Families that require a specialized target variant instead of generic headers. */
|
|
export const HEADER_ARCHITECTURE_EXCLUSIONS = [
|
|
...ELF_METADATA_ARCHITECTURES,
|
|
...RAW_ONLY_ARCHITECTURES,
|
|
"hexagon",
|
|
"xtensa",
|
|
"arm64_32",
|
|
] as const satisfies readonly BinaryArchitecture[];
|
|
|
|
/** Recognized family with no specialized target member; the header alone is the fact. */
|
|
export type HeaderArchitecture = Exclude<
|
|
BinaryArchitecture,
|
|
(typeof HEADER_ARCHITECTURE_EXCLUSIONS)[number]
|
|
>;
|
|
|
|
/** Typed declaration-inspection limitation; consumers switch on `kind`, never prose. */
|
|
export type MipsElfLimitation =
|
|
| {
|
|
readonly kind: "truncated_section_table";
|
|
readonly offset: number;
|
|
readonly examined: number;
|
|
readonly declared: number;
|
|
readonly message: string;
|
|
}
|
|
| {
|
|
readonly kind: "unexamined_declarations";
|
|
readonly message: string;
|
|
};
|
|
|
|
/** Extract human-readable messages from typed MIPS ELF inspection limitations. */
|
|
export const mipsLimitationMessages = (
|
|
limitations?: readonly MipsElfLimitation[],
|
|
): readonly string[] =>
|
|
(limitations ?? []).map((limitation) => limitation.message);
|
|
|
|
const MIPS_ET_EXEC = 2;
|
|
|
|
/**
|
|
* Whether an ELF header has the recordless legacy ELF32 MIPS ET_EXEC shape:
|
|
* MIPS-I / MIPS32 / MIPS32r2 with unspecified ABI and no ASE, machine,
|
|
* FP64, NAN2008, n32, or 32bitmode flags. Does not by itself establish lane
|
|
* admission (ABI flags, GNU attributes, and limitations are checked by the
|
|
* provider profile).
|
|
*/
|
|
export const isRecordlessLegacyMipsExecHeader = (
|
|
metadata: ElfFamilyMetadata,
|
|
): boolean => {
|
|
const { flags } = metadata;
|
|
const arch = flags & EF_MIPS_ARCH;
|
|
return (
|
|
metadata.elfClass === 32 &&
|
|
metadata.type === MIPS_ET_EXEC &&
|
|
(arch === EF_MIPS_ARCH_1 ||
|
|
arch === EF_MIPS_ARCH_32 ||
|
|
arch === EF_MIPS_ARCH_32R2) &&
|
|
(flags &
|
|
(EF_MIPS_ARCH_ASE |
|
|
EF_MIPS_MACH |
|
|
EF_MIPS_ABI |
|
|
EF_MIPS_ABI2 |
|
|
EF_MIPS_ABI_ON32 |
|
|
EF_MIPS_32BITMODE |
|
|
EF_MIPS_FP64 |
|
|
EF_MIPS_NAN2008)) ===
|
|
0
|
|
);
|
|
};
|
|
|
|
/** Raw ELF facts; family recognition does not establish ISA or ABI support. */
|
|
export interface MipsElfMetadata extends ElfFamilyMetadata {
|
|
/** Undefined until inspected; null means no ABI record was observed. */
|
|
readonly abiFlags?: MipsAbiFlags | null;
|
|
/** Undefined until inspected; null means no GNU attribute section was observed. */
|
|
readonly gnuAttributes?: MipsGnuAttributes | null;
|
|
/** Unavailable declaration sources; missing records there remain unknown. */
|
|
readonly limitations?: readonly MipsElfLimitation[];
|
|
/** Complete dynamic REL declarations, or the reason they could not be inspected. */
|
|
readonly sharedLoad?:
|
|
| {
|
|
readonly status: "complete";
|
|
readonly relocationCount: number;
|
|
/** The dynamic table declares DF_1_PIE. */
|
|
readonly declaresPie?: true;
|
|
readonly relocationTypes: readonly number[];
|
|
readonly symbolRelativeCount: number;
|
|
/** Every PT_LOAD virtual address with its program-header index, in header order. */
|
|
readonly loadAddresses: readonly {
|
|
readonly headerIndex: number;
|
|
readonly virtualAddress: number;
|
|
}[];
|
|
/** Android prelink trailer base; null when the file has no trailer. */
|
|
readonly prelinkBase: number | null;
|
|
}
|
|
| { readonly status: "unavailable"; readonly reason: string };
|
|
}
|
|
|
|
/** Raw ELF facts; family recognition does not establish ISA or ABI support. */
|
|
export interface RiscvElfMetadata extends ElfFamilyMetadata {
|
|
/** Undefined until `.riscv.attributes` is inspected. */
|
|
readonly isa?: RiscvIsaDeclaration;
|
|
/** Undefined until the section table is inspected; read for ET_EXEC and ET_DYN. */
|
|
readonly sectionOverlap?: ElfSectionOverlap;
|
|
}
|
|
|
|
/** One allocated section as its header declares it; values are lowercase 0x-hex. */
|
|
export interface ElfSectionExtent {
|
|
readonly index: number;
|
|
/** Display only; null when the section-name table was not read. */
|
|
readonly name: string | null;
|
|
readonly address: string;
|
|
readonly size: string;
|
|
}
|
|
|
|
/** A section that takes part in an overlap of another shape. */
|
|
export interface ElfSectionWitness extends ElfSectionExtent {
|
|
readonly nobits: boolean;
|
|
readonly executable: boolean;
|
|
}
|
|
|
|
/**
|
|
* Allocated address ranges that an executable section of a fixed-address image
|
|
* shares with another section. `code-reservation` is the one verified shape: a
|
|
* NOBITS section that starts with an executable PROGBITS section and covers it,
|
|
* with no other section overlapping either, as ESP-IDF's `.flash_rodata_dummy`
|
|
* reserves `.flash.text`.
|
|
*/
|
|
export type ElfSectionOverlap =
|
|
| { readonly status: "none" }
|
|
| {
|
|
readonly status: "code-reservation";
|
|
readonly code: ElfSectionExtent & { readonly offset: string };
|
|
readonly reservation: ElfSectionExtent;
|
|
}
|
|
/** The two sections whose overlap departs from that shape. */
|
|
| {
|
|
readonly status: "unsupported";
|
|
readonly witnesses: readonly [ElfSectionWitness, ElfSectionWitness];
|
|
}
|
|
| { readonly status: "unavailable"; readonly reason: string };
|
|
|
|
/** The ISA an image declares in `.riscv.attributes`; not proof of every instruction. */
|
|
export type RiscvIsaDeclaration =
|
|
| {
|
|
readonly status: "declared";
|
|
/** `Tag_RISCV_arch` exactly as declared. */
|
|
readonly arch: string;
|
|
readonly xlen: 32 | 64;
|
|
readonly base: "i" | "e";
|
|
/** Extension names in declaration order, without versions. */
|
|
readonly extensions: readonly string[];
|
|
}
|
|
/** Every section header was examined and none declares `Tag_RISCV_arch`. */
|
|
| { readonly status: "absent" }
|
|
| {
|
|
readonly status: "unavailable";
|
|
readonly reason: string;
|
|
/** The declared string, when one was read but not understood. */
|
|
readonly arch?: string;
|
|
};
|
|
|
|
/** Declared fields of the 24-byte MIPS ABI flags record, not inferred semantics. */
|
|
export interface MipsAbiFlags {
|
|
readonly version: number;
|
|
readonly isaLevel: number;
|
|
readonly isaRevision: number;
|
|
readonly gprSize: number;
|
|
readonly cpr1Size: number;
|
|
readonly cpr2Size: number;
|
|
readonly fpAbi: number;
|
|
readonly isaExtension: number;
|
|
readonly ases: number;
|
|
readonly flags1: number;
|
|
readonly flags2: number;
|
|
}
|
|
|
|
/** File-scope GNU object attributes parsed from `SHT_GNU_ATTRIBUTES`. */
|
|
export interface MipsGnuAttributes {
|
|
/** Tag_GNU_MIPS_ABI_FP; null means this file did not declare the tag. */
|
|
readonly fpAbi: number | null;
|
|
/** Tag_GNU_MIPS_ABI_MSA vector-argument ABI; null means undeclared. */
|
|
readonly msa: number | null;
|
|
}
|
|
|
|
/** Byte range of the selected slice inside its universal Mach-O container. */
|
|
export interface UniversalMachOSlice {
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
}
|
|
|
|
/** FAT-table identity; CPU family alone cannot identify a universal slice. */
|
|
export interface UniversalMachOSliceIdentity extends UniversalMachOSlice {
|
|
readonly cpuType: number;
|
|
readonly cpuSubtype: number;
|
|
}
|
|
|
|
interface BinaryTargetIdentity {
|
|
readonly path: string;
|
|
readonly sourcePath?: string;
|
|
/** Verbatim selected spelling when it differs from the inspected file path. */
|
|
readonly callerInputPath?: string;
|
|
/** Selected artifact spelling determines name-based format hints and external companions. */
|
|
readonly artifactSelectedPath?: string;
|
|
/** Info.plist of the Apple bundle the target was resolved from. */
|
|
readonly bundleInfoPlist?: string;
|
|
readonly bundleExecutableSelection?: BundleExecutableSelection;
|
|
readonly sha256: string;
|
|
}
|
|
|
|
type NonExecutableMetadata = {
|
|
readonly architecture?: never;
|
|
readonly availableArchitectures?: never;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
};
|
|
|
|
type ExecutableTarget = BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly architecture: HeaderArchitecture;
|
|
readonly availableArchitectures: readonly HeaderArchitecture[];
|
|
};
|
|
|
|
/**
|
|
* Canonical local target identity and provider-neutral file classification.
|
|
* Each variant carries only metadata established for that exact file kind.
|
|
*/
|
|
export type BinaryTarget =
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "elf";
|
|
readonly architecture: "mips";
|
|
readonly availableArchitectures: readonly "mips"[];
|
|
readonly mips: MipsElfMetadata;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "elf";
|
|
readonly architecture: "riscv";
|
|
readonly availableArchitectures: readonly "riscv"[];
|
|
readonly riscv: RiscvElfMetadata;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "elf";
|
|
readonly architecture: "powerpc";
|
|
readonly availableArchitectures: readonly "powerpc"[];
|
|
/** Family identity does not select a processor variant. */
|
|
readonly powerpc: ElfFamilyMetadata;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "elf";
|
|
readonly architecture: "hexagon";
|
|
readonly availableArchitectures: readonly "hexagon"[];
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "elf";
|
|
readonly architecture: "xtensa";
|
|
readonly availableArchitectures: readonly "xtensa"[];
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "mach-o";
|
|
readonly architecture: "arm64_32";
|
|
readonly availableArchitectures: readonly "arm64_32"[];
|
|
readonly universalSlice?: never;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (ExecutableTarget & {
|
|
readonly format: "mach-o" | "elf" | "dos-mz" | "dos-com";
|
|
readonly universalSlice?: never;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (ExecutableTarget & {
|
|
readonly format: "mach-o-universal";
|
|
/** Selected slice range a provider must analyze instead of the container. */
|
|
readonly universalSlice: UniversalMachOSlice;
|
|
/** Exact validated FAT declarations, when supplied by target admission. */
|
|
readonly universalSlices?: readonly UniversalMachOSliceIdentity[];
|
|
/** Physical FAT table width observed at target admission. */
|
|
readonly universalContainerKind?: "fat32" | "fat64";
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "raw-binary";
|
|
readonly architecture: HeaderArchitecture | RawOnlyArchitecture;
|
|
readonly availableArchitectures: readonly (
|
|
| HeaderArchitecture
|
|
| RawOnlyArchitecture
|
|
)[];
|
|
readonly rawLoad: RawBinaryLoad;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (BinaryTargetIdentity & {
|
|
readonly kind: "executable";
|
|
readonly format: "gamecube-dol";
|
|
/** Fixed by the DOL selection; the header does not carry a CPU family. */
|
|
readonly architecture: "powerpc";
|
|
readonly availableArchitectures: readonly ["powerpc"];
|
|
readonly byteOrder: "big";
|
|
readonly bits: 32;
|
|
/** Canonical hex entry taken from the DOL header. */
|
|
readonly entryPoint: string;
|
|
readonly entryPointSource: "dol-header";
|
|
/** Validated header: used sections, BSS, and BSS-overlapping data slots. */
|
|
readonly dol: DolHeader;
|
|
readonly executableRole?: never;
|
|
readonly managed?: never;
|
|
})
|
|
| (ExecutableTarget & {
|
|
readonly format: "pe";
|
|
/** Original COFF machine; undefined means this header fact is unavailable. */
|
|
readonly peMachine?: number;
|
|
/** Provider-neutral PE image role observed from COFF characteristics. */
|
|
readonly executableRole:
|
|
| "application"
|
|
| "shared-library"
|
|
| "non-executable";
|
|
/** Whether the PE declares a non-empty CLI header data-directory entry. */
|
|
readonly managed: boolean;
|
|
/** Referenced CodeView debug symbols from the PE debug directory, if present. */
|
|
readonly codeView?: PeCodeViewRecord;
|
|
})
|
|
| (BinaryTargetIdentity &
|
|
NonExecutableMetadata & {
|
|
readonly kind: "database";
|
|
readonly format: "analysis-database";
|
|
/** Engine identified at admission; null when the database is unidentified. */
|
|
readonly databaseEngine: AnalysisDatabaseEngine | null;
|
|
})
|
|
| (BinaryTargetIdentity &
|
|
NonExecutableMetadata & {
|
|
readonly kind: "archive";
|
|
readonly format: Exclude<
|
|
TargetFormatsOfKind<"archive">,
|
|
"esp-flash-image"
|
|
>;
|
|
})
|
|
| (BinaryTargetIdentity &
|
|
NonExecutableMetadata & {
|
|
readonly kind: "archive";
|
|
readonly format: "esp-flash-image";
|
|
/** How the dump is read; null inputs take their defaults. */
|
|
readonly espFlash: EspFlashTargetSelection;
|
|
})
|
|
| (BinaryTargetIdentity &
|
|
NonExecutableMetadata & {
|
|
readonly kind: "artifact";
|
|
readonly format: TargetFormatsOfKind<"artifact">;
|
|
});
|
|
|
|
/** Caller inputs that select how an ESP flash dump is read. */
|
|
export interface EspFlashTargetSelection {
|
|
/** The caller selected the format, so the table scan runs without a bootloader. */
|
|
readonly selected: boolean;
|
|
/** Flash address of the dump's first byte. */
|
|
readonly flashOffset: number | null;
|
|
/** Flash address of the partition table. */
|
|
readonly partitionTableOffset: number | null;
|
|
}
|
|
|
|
/** Whether an executable target contains a thin or universal Mach-O image. */
|
|
export const isMachOFormat = (
|
|
format: unknown,
|
|
): format is "mach-o" | "mach-o-universal" =>
|
|
format === "mach-o" || format === "mach-o-universal";
|
|
|
|
/** Result-affecting artifact selection; canonical packed identity remains target.path. */
|
|
export const artifactTargetSelectedPath = (target: BinaryTarget): string =>
|
|
target.kind === "archive" || target.kind === "artifact"
|
|
? (target.artifactSelectedPath ?? target.sourcePath ?? target.path)
|
|
: (target.sourcePath ?? target.path);
|