Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ghidra): reject undefined recordless MIPS ELF flags
  • Loading branch information
Bobtoshi committed Nov 13, 2026
commit 5923bf4eeaecd2a2f971c9742a8df152827b0fb9
18 changes: 14 additions & 4 deletions src/ghidra/GhidraMipsProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ describe("recordless legacy MIPS admission", () => {
[{ flags: 0x00000001 }, "elf32-exec-mips1-legacy-pinned-v1"],
[{ flags: 0x50000001 }, "elf32-exec-arch32-legacy-pinned-v1"],
[{ flags: 0x70000001 }, "elf32-exec-arch32-legacy-pinned-v1"],
[{ flags: 0x5000009f }, "elf32-exec-arch32-legacy-pinned-v1"],
] satisfies [Partial<MipsElfMetadata>, string][])(
"admits a recordless legacy header (%j) with a pinned 32-bit language",
async (change, lane) => {
Expand Down Expand Up @@ -303,6 +304,9 @@ describe("recordless legacy MIPS admission", () => {
],
[{ flags: 0x50000201 }, "EF_MIPS_FP64"],
[{ flags: 0x50000401 }, "EF_MIPS_NAN2008"],
[{ flags: 0x00000801 }, "unsupported e_flags bits (0x800)"],
[{ flags: 0x50000801 }, "unsupported e_flags bits (0x800)"],
[{ flags: 0x70000801 }, "unsupported e_flags bits (0x800)"],
[{ flags: 0x60000001 }, "32R2"],
[
{
Expand All @@ -315,14 +319,20 @@ describe("recordless legacy MIPS admission", () => {
],
] satisfies [Partial<MipsElfMetadata>, string][])(
"keeps refusing unverified recordless variants %j",
(change, reason) => {
async (change, reason) => {
const ghidra = new GhidraProvider(config, silentLogger, {}, host);
expect(
ghidra.inspectTargetSupport(target({ ...change, abiFlags: null })),
).toMatchObject({
const executable = target({ ...change, abiFlags: null });
expect(ghidra.inspectTargetSupport(executable)).toMatchObject({
status: "unsupported",
reason: expect.stringContaining(reason),
});
expect(await ghidra.resolveAnalysisProfile(executable)).toMatchObject({
ok: false,
error: {
_tag: "AnalysisUnsupportedTargetError",
reason: expect.stringContaining(reason),
},
});
},
);

Expand Down
18 changes: 14 additions & 4 deletions src/ghidra/GhidraMipsProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const MIPS_ET_REL = 1;
const MIPS_ET_EXEC = 2;
const MIPS_ET_DYN = 3;
const MIPS16_HEADER_ASE = 0x04000000;
const MIPS_ARCH_MASK = 0xf0000000;
const MIPS_ARCH_1 = 0x00000000;
const MIPS_ARCH_32 = 0x50000000;
const MIPS_ARCH_32R2 = 0x70000000;
// GNU Binutils elfcpp/mips.h defines NOREORDER, PIC, CPIC, XGOT, UCODE and
// OPTIONS_FIRST. Preserve these bits alongside the separately checked ISA.
const MIPS_RECORDLESS_ALLOWED_FLAGS = MIPS_ARCH_MASK | 0x9f;

/** A distinct compressed shared-image shape; admission still checks every declaration. */
const isGhidraMips16SharedTarget = (target: BinaryTarget): boolean =>
Expand Down Expand Up @@ -52,7 +56,7 @@ const hasLegacyMipsHeaderShape = (target: BinaryTarget): boolean => {
)
return false;
const { flags, abiFlags, limitations } = target.mips;
const arch = flags & 0xf0000000;
const arch = flags & MIPS_ARCH_MASK;
return (
// The lane and its verification cover executable firmware images only;
// recordless relocatable objects stay outside the inferred-o32 assumption.
Expand Down Expand Up @@ -135,7 +139,7 @@ export const ghidraMipsUnsupportedReason = (
return "MIPS16, microMIPS and other ASE encodings are outside this standard-instruction MIPS lane.";
if ((metadata.flags & 0x00ff0000) !== 0)
return "Machine-specific MIPS variants, including PSP Allegrex, need a separately supported processor profile.";
const arch = metadata.flags & 0xf0000000;
const arch = metadata.flags & MIPS_ARCH_MASK;
if (arch !== MIPS_ARCH_1 && arch !== MIPS_ARCH_32 && arch !== MIPS_ARCH_32R2)
return "This Ghidra MIPS lane requires EF_MIPS_ARCH_32R2, or a recordless legacy EF_MIPS_ARCH_1 (MIPS-I) or EF_MIPS_ARCH_32 header; other ISA revisions need separate verification.";
const recordlessShape =
Expand All @@ -151,7 +155,13 @@ export const ghidraMipsUnsupportedReason = (
return "EF_MIPS_FP64 requires separate verification of 64-bit floating-point register semantics in this Ghidra lane.";
if ((metadata.flags & 0x00000400) !== 0)
return "EF_MIPS_NAN2008 requires separate verification of NaN encoding semantics in this Ghidra lane.";
if (hasLegacyMipsHeaderShape(target)) return null;
if (hasLegacyMipsHeaderShape(target)) {
const unsupportedFlags =
(metadata.flags & ~MIPS_RECORDLESS_ALLOWED_FLAGS) >>> 0;
return unsupportedFlags === 0
? null
: `The recordless MIPS ELF header carries unsupported e_flags bits (0x${unsupportedFlags.toString(16)}).`;
}
// A recordless relocatable object is not an incomplete inspection; the
// lane is scoped to executable firmware images by design.
if (metadata.type !== MIPS_ET_EXEC)
Expand Down Expand Up @@ -223,7 +233,7 @@ export const isLegacyMipsAdmission = (target: BinaryTarget): boolean =>
const isMips1Header = (target: BinaryTarget): boolean =>
target.architecture === "mips" &&
target.mips !== undefined &&
(target.mips.flags & 0xf0000000) === MIPS_ARCH_1;
(target.mips.flags & MIPS_ARCH_MASK) === MIPS_ARCH_1;

/** Whether the target is admitted through the recordless MIPS-I legacy lane. */
export const isMips1LegacyAdmission = (target: BinaryTarget): boolean =>
Expand Down