diff --git a/docs/ghidra-ppc.md b/docs/ghidra-ppc.md index 84f9183436..577157f3fb 100644 --- a/docs/ghidra-ppc.md +++ b/docs/ghidra-ppc.md @@ -8,10 +8,14 @@ PowerPC variants remain separate follow-ups. ## Admission boundary -ELF `EM_PPC` (20) is retained as the provider-neutral `powerpc` family. The -target also retains class, byte order, `e_type` and raw `e_flags`. Recognition -does not mean every provider or PowerPC variant is supported. `EM_PPC64` (21) -is not recognized as this family. +ELF `EM_PPC` (20) is retained as the provider-neutral `powerpc` family, and so +is `EM_PPC64` (21) when the header is ELF64. An ELF32 header that claims +`EM_PPC64` stays unrecognized. The target also retains class, byte order, +`e_type` and raw `e_flags`; for `EM_PPC64` the low two bits of `e_flags` declare +the ELFv1 or ELFv2 ABI. Recognition does not mean every provider or PowerPC +variant is supported: no provider lane admits `EM_PPC64` yet. Static ELF readers +are unchanged: they report the machine as `ppc64` and their Evidence subject +carries no family for it. The Ghidra lane admits ELF32 `ET_EXEC`, big endian. ELF64, little-endian, relocatable and shared objects are refused with a specific unsupported diff --git a/src/domain/binaryTarget.powerpc.test.ts b/src/domain/binaryTarget.powerpc.test.ts index d8e6912fb6..83529134a5 100644 --- a/src/domain/binaryTarget.powerpc.test.ts +++ b/src/domain/binaryTarget.powerpc.test.ts @@ -49,10 +49,45 @@ describe("PowerPC ELF header boundaries", () => { }); }); - it("does not recognize EM_PPC64 as the 32-bit family", () => { - expect(parseExecutableHeader(header(false, 64, 21), "x64")).toEqual({ + it.each([ + [true, 2], + [false, 1], + ])( + "classifies EM_PPC64 in the family with its class and ABI flags (little=%s)", + (little, abi) => { + const bytes = header(little, 64, 21); + if (little) bytes.writeUInt32LE(abi, 48); + else bytes.writeUInt32BE(abi, 48); + const parsed = parseExecutableHeader(bytes, "x64"); + if (!parsed.ok) throw new Error(parsed.error); + expect(parsed.value).toEqual({ + format: "elf", + architecture: "powerpc", + availableArchitectures: ["powerpc"], + powerpc: { + elfClass: 64, + byteOrder: little ? "little" : "big", + type: 2, + flags: abi, + }, + }); + }, + ); + + it.each([false, true])( + "refuses an ELF32 header that claims EM_PPC64 (little=%s)", + (little) => { + expect(parseExecutableHeader(header(little, 32, 21), "x64")).toEqual({ + ok: false, + error: "unsupported ELF architecture: e_machine 21", + }); + }, + ); + + it("names the machine when no family covers it", () => { + expect(parseExecutableHeader(header(false, 64, 22), "x64")).toEqual({ ok: false, - error: "unsupported ELF architecture: e_machine 21", + error: "unsupported ELF architecture: e_machine 22", }); }); }); diff --git a/src/domain/elfFixedHeader.test.ts b/src/domain/elfFixedHeader.test.ts index 6b7a010c7a..f536bfbf0a 100644 --- a/src/domain/elfFixedHeader.test.ts +++ b/src/domain/elfFixedHeader.test.ts @@ -1,5 +1,9 @@ import { expect, it } from "vitest"; -import { readElfFixedHeader, readElfHeaderFacts } from "./elfFixedHeader.js"; +import { + elfArchitecture, + readElfFixedHeader, + readElfHeaderFacts, +} from "./elfFixedHeader.js"; import { elf } from "./binaryTarget.fixture.js"; it.each([ @@ -84,6 +88,14 @@ it("checks declared header size against the whole file, not the prefix", () => { expect(readElfFixedHeader(bytes, 127)).toMatchObject({ ok: false }); }); +it("classifies EM_PPC64 only for a caller that commits ELF64", () => { + expect(elfArchitecture(21, 64)).toBe("powerpc"); + expect(elfArchitecture(21, 32)).toBeUndefined(); + // Readers that pass only the machine keep their reports as they were. + expect(elfArchitecture(21)).toBeUndefined(); + expect(elfArchitecture(20)).toBe("powerpc"); +}); + it("preserves an unknown ELF machine as a fact without rejecting the header", () => { expect(readElfFixedHeader(elf(2, 2, 0xfffe))).toMatchObject({ ok: true, diff --git a/src/domain/elfFixedHeader.ts b/src/domain/elfFixedHeader.ts index 4fc5448059..1d283daa4b 100644 --- a/src/domain/elfFixedHeader.ts +++ b/src/domain/elfFixedHeader.ts @@ -110,12 +110,18 @@ export const admitElfFixedHeaderFacts = ( flags: facts.flags, osAbi: facts.osAbi, abiVersion: facts.abiVersion, - architecture: elfArchitecture(facts.machine) ?? null, + 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: @@ -126,6 +132,10 @@ export const elfArchitecture = ( 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). diff --git a/src/domain/native/nativeExportParser.test.ts b/src/domain/native/nativeExportParser.test.ts index 36352eb58a..847c310a9f 100644 --- a/src/domain/native/nativeExportParser.test.ts +++ b/src/domain/native/nativeExportParser.test.ts @@ -709,6 +709,24 @@ describe("ELF dynamic symbol exports", () => { exports: [{ name: "be_fn", value: "0x10000", size: "8" }], }); }); + + it("keeps the ppc64 reader name for EM_PPC64", () => { + const result = parse( + elfExportImage({ + bits: 64, + little: true, + machine: 21, + symbols: [{ name: "le_fn", value: 0x10000n, size: 8n }], + }), + ); + + expect(result).toMatchObject({ + bits: 64, + byte_order: "little", + architecture: "ppc64", + exports: [{ name: "le_fn", value: "0x10000", size: "8" }], + }); + }); }); describe("ELF version index 0 unversioned exports", () => { diff --git a/src/ghidra/GhidraPowerPcProfile.test.ts b/src/ghidra/GhidraPowerPcProfile.test.ts index 8f4595a4a4..7709e70dc0 100644 --- a/src/ghidra/GhidraPowerPcProfile.test.ts +++ b/src/ghidra/GhidraPowerPcProfile.test.ts @@ -131,6 +131,9 @@ describe("bounded Ghidra PowerPC admission", () => { it.each([ [{ elfClass: 64 }, "ELF32"], + [{ elfClass: 64, byteOrder: "little", flags: 2 }, "PPC64"], + [{ elfClass: 64, byteOrder: "little", type: 3, flags: 2 }, "PPC64"], + [{ elfClass: 64, type: 3, flags: 1 }, "PPC64"], [{ byteOrder: "little" }, "big-endian"], [{ type: 1 }, "ET_EXEC"], [{ type: 3 }, "ET_EXEC"], @@ -161,5 +164,8 @@ describe("bounded Ghidra PowerPC admission", () => { expect(hopperLoaderArgsForTarget(target()).ok).toBe(false); const ida = new IdaProvider(config, {}); expect(ida.inspectTargetSupport(target()).status).toBe("unsupported"); + const ppc64 = target({ elfClass: 64, byteOrder: "little", flags: 2 }); + expect(hopper.inspectTargetSupport(ppc64).status).toBe("unsupported"); + expect(ida.inspectTargetSupport(ppc64).status).toBe("unsupported"); }); });