feat(ghidra): admit soft-float MIPS32r2 ELF with integer-register float storage (#1949)
* feat(ghidra): admit soft-float MIPS32r2 ELF with integer-register float storage Soft-float o32 executables (Linux kernel builds, bare-metal firmware) were refused because their ABI flags record declares no CPR1 and FP ABI 3. Ghidra's MIPS32 compiler specifications model only hard-float o32, so typed float/double signatures (DWARF) would be bound to $f12/$f14/$f0 and the decompiler loses the real dataflow: `return sel ? a : b` decompiled to `return in_f0;`, and callers passed phantom FPU inputs. Admit ELF32 ET_EXEC o32 MIPS32r2 targets whose ABI record declares soft-float with no CPR1, and commit `mips_soft_float_storage: o32-integer-registers-v1` in their analysis profile only (hard-float profile digests are unchanged). ReaGhidraMipsSoftFloat.java runs after auto-analysis and, for each function with a float/double formal type, either keeps the types with the storage Ghidra's o32 model assigns to a same-size integer (pair alignment, stack spill), or, for auto-parameters such as hidden struct-return pointers, swaps float types for same-size integers under dynamic storage. No function is left on hard-float storage. The report is written last; the provider client requires it exactly when the profile commits the policy and refuses the session before any operation otherwise. Verification: real Ghidra 12.1.4 lane (verify:ghidra:mips) now builds hard- and soft-float fixtures in both byte orders and checks exact parameter/return storage through inspect_native_api, caller constants, snapshot profile and Evidence outcome; test:fast, verify:package and an installed-tarball soft-float run pass. * fix(ghidra): model soft-float MIPS as a prototype for every typed callable Review feedback on #1949: rewriting storage per Function left external functions and indirect calls through typed function pointers on Ghidra's hard-float o32 prototype, which still read $f12/$f14/$f0 while the session reported success. A negative control confirms it: without the change, `(*rea_mips_soft_callback)(8.5f, 9.5f, sel)` decompiles to `(*rea_mips_soft_callback)(in_f12, in_f14, sel)`. ReaGhidraMipsSoftFloat.java now derives a `__rea_soft_float` prototype from the program's own default o32 model (encoded, float pentries and float-consuming rules removed, so byte-order-specific aggregate rules and the hidden struct-return pointer are kept), installs it as a specification extension, and assigns it with dynamic storage to every non-thunk function, external function and function-pointer definition whose formal types use float or double. Thunks follow their targets. Float types are kept everywhere, so the integer-retype fallback is removed. The script fails without writing its report if the derived prototype or any reassigned function still uses an FPU register. The policy becomes `o32-soft-float-prototype-v1` with a {functions, definitions} report. The soft-float fixture adds an indirect call through a typed pointer; the real-Ghidra lane requires its constants with no FPU inputs and the struct return's `float scale` in $a1. * test(ghidra): pin ODDSPREG acceptance for soft-float MIPS ABI records Review on #1949 suggested requiring flags1 == 0 for soft-float, since MIPS_AFL_FLAGS1_ODDSPREG names CPR1 registers. GCC 5.2 kernel modules, LLVM/Rust firmware and Clang -msoft-float objects all record flags1 = 1 with FP ABI soft-float: it is the MIPS32 o32 toolchain default and selects nothing without CPR1. Keep accepting it, say why at the check, and test that soft-float with ODDSPREG is admitted while any other general flag is refused.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/* Soft-float o32 storage fixture; linked with mips.c, never executed by REA.
|
||||
* Without an FPU, float/double values travel in integer registers and stack
|
||||
* slots: pick uses $a0/$a1/$a2 and returns in $v0, wide uses the $a0:$a1 pair,
|
||||
* and mixed aligns d to $a2:$a3 so f and e spill to the stack at 16 and 24.
|
||||
* structured returns through a hidden $a0 pointer, moving scale to $a1. The
|
||||
* typed function pointer makes the caller's indirect call use a prototype that
|
||||
* belongs to no function. No arithmetic, so no compiler-rt helpers are needed.
|
||||
*/
|
||||
volatile unsigned int rea_mips_soft_sink;
|
||||
|
||||
__attribute__((noinline)) float rea_mips_soft_pick(float a, float b, int sel)
|
||||
{
|
||||
return sel ? a : b;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) double rea_mips_soft_wide(double a, int sel)
|
||||
{
|
||||
return sel ? a : 0.5;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) double rea_mips_soft_mixed(int i, double d, float f, double e)
|
||||
{
|
||||
rea_mips_soft_sink = *(volatile unsigned int *)&f;
|
||||
return i ? d : e;
|
||||
}
|
||||
|
||||
struct rea_mips_soft_pair {
|
||||
double first;
|
||||
double second;
|
||||
};
|
||||
|
||||
__attribute__((noinline)) struct rea_mips_soft_pair rea_mips_soft_struct(float scale, int sel)
|
||||
{
|
||||
struct rea_mips_soft_pair pair = { 0.25, 0.75 };
|
||||
rea_mips_soft_sink = *(volatile unsigned int *)&scale;
|
||||
if (sel)
|
||||
pair.first = 1.25;
|
||||
return pair;
|
||||
}
|
||||
|
||||
/* Volatile so the indirect call through this typed pointer is not folded. */
|
||||
float (*volatile rea_mips_soft_callback)(float, float, int) = rea_mips_soft_pick;
|
||||
|
||||
__attribute__((noinline)) unsigned int rea_mips_soft_caller(int sel)
|
||||
{
|
||||
float picked = rea_mips_soft_pick(1.5f, 2.5f, sel);
|
||||
double wide = rea_mips_soft_wide(3.25, sel);
|
||||
double mixed = rea_mips_soft_mixed(sel, 4.75, 5.5f, 6.125);
|
||||
struct rea_mips_soft_pair pair = rea_mips_soft_struct(7.5f, sel);
|
||||
float called = rea_mips_soft_callback(8.5f, 9.5f, sel);
|
||||
return *(unsigned int *)&picked ^ *(unsigned int *)&wide ^ *(unsigned int *)&mixed ^
|
||||
*(unsigned int *)&pair.first ^ *(unsigned int *)&called;
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
import type { MipsElfMetadata } from "../../../src/domain/binaryTargetTypes.js";
|
||||
|
||||
export type MipsFloatAbi = "hard" | "soft";
|
||||
export interface MipsReadelfFacts {
|
||||
mips: MipsElfMetadata;
|
||||
symbols: Record<
|
||||
| "rea_mips_entry"
|
||||
| "rea_mips_leaf"
|
||||
| "rea_mips_probe"
|
||||
| "rea_mips_global"
|
||||
| "rea_mips_marker",
|
||||
string
|
||||
>;
|
||||
symbols: Record<string, string>;
|
||||
}
|
||||
export function parseMipsReadelf(
|
||||
text: string,
|
||||
byteOrder: string,
|
||||
floatAbi?: MipsFloatAbi,
|
||||
): MipsReadelfFacts;
|
||||
export function inspectMipsReadelf(
|
||||
path: string,
|
||||
byteOrder: string,
|
||||
floatAbi?: MipsFloatAbi,
|
||||
): Promise<
|
||||
MipsReadelfFacts & { command: string; version: string; raw: string }
|
||||
>;
|
||||
@@ -33,3 +29,7 @@ export function assertMipsProbe(
|
||||
address: string,
|
||||
): void;
|
||||
export function assertMipsGlobal(bytes: string, byteOrder: string): void;
|
||||
export function assertMipsSoftFloatStorage(
|
||||
boundaries: Record<string, unknown>,
|
||||
byteOrder: string,
|
||||
): void;
|
||||
|
||||
@@ -15,8 +15,17 @@ const wordHex = (value, byteOrder) => {
|
||||
return bytes.toString("hex");
|
||||
};
|
||||
|
||||
const SOFT_FLOAT_SYMBOLS = [
|
||||
"rea_mips_soft_pick",
|
||||
"rea_mips_soft_wide",
|
||||
"rea_mips_soft_mixed",
|
||||
"rea_mips_soft_struct",
|
||||
"rea_mips_soft_caller",
|
||||
];
|
||||
|
||||
/** Parse only the documented GNU readelf fields needed by our tiny fixture. */
|
||||
export function parseMipsReadelf(text, byteOrder) {
|
||||
export function parseMipsReadelf(text, byteOrder, floatAbi = "hard") {
|
||||
assert.ok(floatAbi === "hard" || floatAbi === "soft");
|
||||
language(byteOrder);
|
||||
const field = (expression, label) => {
|
||||
const matches = [...text.matchAll(expression)];
|
||||
@@ -44,8 +53,14 @@ export function parseMipsReadelf(text, byteOrder) {
|
||||
const fpAbi = new Map([
|
||||
["Hard float (double precision)", 1],
|
||||
["Hard float (32-bit CPU, Any FPU)", 5],
|
||||
["Soft float", 3],
|
||||
]).get(fp);
|
||||
assert.ok(fpAbi !== undefined, `Unrecognized fixture FP ABI: ${fp}`);
|
||||
assert.equal(
|
||||
fpAbi === 3,
|
||||
floatAbi === "soft",
|
||||
`Fixture FP ABI ${fp} differs from the ${floatAbi}-float build intent`,
|
||||
);
|
||||
assert.equal(field(/^ISA Extension:\s*(.+)$/gmu, "ISA extension"), "None");
|
||||
assert.equal(field(/^ASEs:\s*\n[ \t]*(.+)$/gmu, "ASEs"), "None");
|
||||
const abiFlags = {
|
||||
@@ -76,6 +91,7 @@ export function parseMipsReadelf(text, byteOrder) {
|
||||
"rea_mips_probe",
|
||||
"rea_mips_global",
|
||||
"rea_mips_marker",
|
||||
...(floatAbi === "soft" ? SOFT_FLOAT_SYMBOLS : []),
|
||||
]) {
|
||||
const value = field(
|
||||
new RegExp(
|
||||
@@ -93,7 +109,7 @@ export function parseMipsReadelf(text, byteOrder) {
|
||||
}
|
||||
|
||||
/** Independent reader is a bounded test prerequisite, never an REA provider. */
|
||||
export async function inspectMipsReadelf(path, byteOrder) {
|
||||
export async function inspectMipsReadelf(path, byteOrder, floatAbi = "hard") {
|
||||
const command = process.env.REA_MIPS_READELF ?? "readelf";
|
||||
const options = {
|
||||
env: { ...process.env, LC_ALL: "C" },
|
||||
@@ -117,7 +133,7 @@ export async function inspectMipsReadelf(path, byteOrder) {
|
||||
`readelf diagnostics: ${result.stderr}`,
|
||||
);
|
||||
return {
|
||||
...parseMipsReadelf(result.stdout, byteOrder),
|
||||
...parseMipsReadelf(result.stdout, byteOrder, floatAbi),
|
||||
command,
|
||||
version: version.stdout.split("\n")[0],
|
||||
raw: result.stdout,
|
||||
@@ -178,3 +194,67 @@ export function assertMipsProbe(move, branch, byteOrder, address) {
|
||||
export function assertMipsGlobal(bytes, byteOrder) {
|
||||
assert.equal(bytes, wordHex(7, byteOrder));
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-float o32 places float/double values exactly where a same-size integer
|
||||
* goes: Ghidra joins register pairs most-significant word first. The struct
|
||||
* return keeps its hidden $a0 pointer and its float in $a1.
|
||||
*/
|
||||
export function assertMipsSoftFloatStorage(boundaries, byteOrder) {
|
||||
language(byteOrder);
|
||||
const pair = (low, high) =>
|
||||
byteOrder === "little" ? `${high}:4,${low}:4` : `${low}:4,${high}:4`;
|
||||
const expected = {
|
||||
rea_mips_soft_pick: {
|
||||
return: ["float", "v0:4"],
|
||||
parameters: [
|
||||
["a", "float", "a0:4"],
|
||||
["b", "float", "a1:4"],
|
||||
["sel", "int", "a2:4"],
|
||||
],
|
||||
},
|
||||
rea_mips_soft_wide: {
|
||||
return: ["double", pair("v0", "v1")],
|
||||
parameters: [
|
||||
["a", "double", pair("a0", "a1")],
|
||||
["sel", "int", "a2:4"],
|
||||
],
|
||||
},
|
||||
rea_mips_soft_mixed: {
|
||||
return: ["double", pair("v0", "v1")],
|
||||
parameters: [
|
||||
["i", "int", "a0:4"],
|
||||
["d", "double", pair("a2", "a3")],
|
||||
["f", "float", "Stack[0x10]:4"],
|
||||
["e", "double", "Stack[0x18]:8"],
|
||||
],
|
||||
},
|
||||
rea_mips_soft_struct: {
|
||||
return: ["rea_mips_soft_pair *", "v0:4"],
|
||||
parameters: [
|
||||
["__return_storage_ptr__", "rea_mips_soft_pair *", "a0:4 (auto)"],
|
||||
["scale", "float", "a1:4"],
|
||||
["sel", "int", "a2:4"],
|
||||
],
|
||||
},
|
||||
};
|
||||
assert.deepEqual(
|
||||
Object.keys(boundaries).sort(),
|
||||
Object.keys(expected).sort(),
|
||||
);
|
||||
for (const [name, want] of Object.entries(expected)) {
|
||||
const boundary = boundaries[name];
|
||||
assert.deepEqual(
|
||||
[boundary.return_type.data_type, boundary.return_type.storage],
|
||||
want.return,
|
||||
`${name} return storage`,
|
||||
);
|
||||
assert.deepEqual(
|
||||
[...boundary.parameters]
|
||||
.sort((left, right) => left.ordinal - right.ordinal)
|
||||
.map((item) => [item.name, item.data_type, item.storage]),
|
||||
want.parameters,
|
||||
`${name} parameter storage`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
assertMipsGlobal,
|
||||
assertMipsLoadedImage,
|
||||
assertMipsProbe,
|
||||
assertMipsSoftFloatStorage,
|
||||
parseMipsReadelf,
|
||||
} from "./mips-checks.mjs";
|
||||
|
||||
@@ -227,3 +228,117 @@ describe("MIPS real-verifier independent expectations", () => {
|
||||
expect(() => assertMipsGlobal("0700", "little")).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("MIPS soft-float real-verifier expectations", () => {
|
||||
const softReadelf = readelf
|
||||
.replace("CPR1 size: 32", "CPR1 size: 0")
|
||||
.replace("Hard float (32-bit CPU, Any FPU)", "Soft float")
|
||||
.replace(
|
||||
" 8: 00020240 32 FUNC GLOBAL DEFAULT 4 rea_mips_probe\n",
|
||||
[
|
||||
" 8: 00020240 32 FUNC GLOBAL DEFAULT 4 rea_mips_probe",
|
||||
" 9: 00020300 64 FUNC GLOBAL DEFAULT 4 rea_mips_soft_pick",
|
||||
" 10: 00020340 64 FUNC GLOBAL DEFAULT 4 rea_mips_soft_wide",
|
||||
" 11: 00020380 96 FUNC GLOBAL DEFAULT 4 rea_mips_soft_mixed",
|
||||
" 12: 000203e0 96 FUNC GLOBAL DEFAULT 4 rea_mips_soft_struct",
|
||||
" 13: 00020440 160 FUNC GLOBAL DEFAULT 4 rea_mips_soft_caller",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
|
||||
it("reads the soft-float ABI record and its fixture symbols", () => {
|
||||
const parsed = parseMipsReadelf(softReadelf, "little", "soft");
|
||||
expect(parsed.mips.abiFlags).toMatchObject({ cpr1Size: 0, fpAbi: 3 });
|
||||
expect(parsed.symbols.rea_mips_soft_mixed).toBe("0x20380");
|
||||
});
|
||||
|
||||
it("rejects a float ABI that differs from the build intent", () => {
|
||||
expect(() => parseMipsReadelf(softReadelf, "little", "hard")).toThrow(
|
||||
/build intent/u,
|
||||
);
|
||||
expect(() => parseMipsReadelf(readelf, "little", "soft")).toThrow(
|
||||
/build intent/u,
|
||||
);
|
||||
});
|
||||
|
||||
const boundaries = (byteOrder: "little" | "big") => {
|
||||
const pair = (low: string, high: string) =>
|
||||
byteOrder === "little" ? `${high}:4,${low}:4` : `${low}:4,${high}:4`;
|
||||
const item = (
|
||||
ordinal: number,
|
||||
name: string,
|
||||
dataType: string,
|
||||
storage: string,
|
||||
) => ({ ordinal, name, data_type: dataType, storage });
|
||||
return {
|
||||
rea_mips_soft_pick: {
|
||||
return_type: { data_type: "float", storage: "v0:4" },
|
||||
parameters: [
|
||||
item(2, "sel", "int", "a2:4"),
|
||||
item(0, "a", "float", "a0:4"),
|
||||
item(1, "b", "float", "a1:4"),
|
||||
],
|
||||
},
|
||||
rea_mips_soft_wide: {
|
||||
return_type: { data_type: "double", storage: pair("v0", "v1") },
|
||||
parameters: [
|
||||
item(0, "a", "double", pair("a0", "a1")),
|
||||
item(1, "sel", "int", "a2:4"),
|
||||
],
|
||||
},
|
||||
rea_mips_soft_mixed: {
|
||||
return_type: { data_type: "double", storage: pair("v0", "v1") },
|
||||
parameters: [
|
||||
item(0, "i", "int", "a0:4"),
|
||||
item(1, "d", "double", pair("a2", "a3")),
|
||||
item(2, "f", "float", "Stack[0x10]:4"),
|
||||
item(3, "e", "double", "Stack[0x18]:8"),
|
||||
],
|
||||
},
|
||||
rea_mips_soft_struct: {
|
||||
return_type: { data_type: "rea_mips_soft_pair *", storage: "v0:4" },
|
||||
parameters: [
|
||||
item(
|
||||
0,
|
||||
"__return_storage_ptr__",
|
||||
"rea_mips_soft_pair *",
|
||||
"a0:4 (auto)",
|
||||
),
|
||||
item(1, "scale", "float", "a1:4"),
|
||||
item(2, "sel", "int", "a2:4"),
|
||||
],
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
it("checks soft-float o32 storage, including pair order, alignment and spill", () => {
|
||||
assertMipsSoftFloatStorage(boundaries("little"), "little");
|
||||
assertMipsSoftFloatStorage(boundaries("big"), "big");
|
||||
expect(() =>
|
||||
assertMipsSoftFloatStorage(boundaries("big"), "little"),
|
||||
).toThrow();
|
||||
const hardFloat = boundaries("little");
|
||||
const pickA = hardFloat.rea_mips_soft_pick.parameters.find(
|
||||
(item) => item.ordinal === 0,
|
||||
);
|
||||
if (pickA === undefined) throw new Error("fixture lacks pick parameter a");
|
||||
pickA.storage = "f12:4";
|
||||
expect(() => assertMipsSoftFloatStorage(hardFloat, "little")).toThrow();
|
||||
const unaligned = boundaries("little");
|
||||
const mixedD = unaligned.rea_mips_soft_mixed.parameters.find(
|
||||
(item) => item.ordinal === 1,
|
||||
);
|
||||
if (mixedD === undefined)
|
||||
throw new Error("fixture lacks mixed parameter d");
|
||||
mixedD.storage = "a2:4,a1:4";
|
||||
expect(() => assertMipsSoftFloatStorage(unaligned, "little")).toThrow();
|
||||
const lostHiddenReturn = boundaries("little");
|
||||
lostHiddenReturn.rea_mips_soft_struct.parameters = [
|
||||
{ ordinal: 0, name: "scale", data_type: "float", storage: "a0:4" },
|
||||
{ ordinal: 1, name: "sel", data_type: "int", storage: "a1:4" },
|
||||
];
|
||||
expect(() =>
|
||||
assertMipsSoftFloatStorage(lostHiddenReturn, "little"),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,18 +8,29 @@ import { promisify } from "node:util";
|
||||
const execute = promisify(execFile);
|
||||
|
||||
/** Build a source-owned freestanding ELF; never execute its target code. */
|
||||
export async function buildMipsFixture(directory, byteOrder) {
|
||||
export async function buildMipsFixture(
|
||||
directory,
|
||||
byteOrder,
|
||||
floatAbi = "hard",
|
||||
) {
|
||||
if (byteOrder !== "little" && byteOrder !== "big")
|
||||
throw new Error("MIPS fixture byte order must be little or big");
|
||||
if (floatAbi !== "hard" && floatAbi !== "soft")
|
||||
throw new Error("MIPS fixture float ABI must be hard or soft");
|
||||
const soft = floatAbi === "soft";
|
||||
const compiler = process.env.REA_MIPS_CLANG ?? "clang";
|
||||
const target = byteOrder === "little" ? "mipsel-linux-gnu" : "mips-linux-gnu";
|
||||
const source = fileURLToPath(new URL("../c/mips.c", import.meta.url));
|
||||
const softSource = fileURLToPath(
|
||||
new URL("../c/mips-soft-float.c", import.meta.url),
|
||||
);
|
||||
const probe = fileURLToPath(new URL("./mips-probe.S", import.meta.url));
|
||||
const path = join(directory, `${target}.elf`);
|
||||
const path = join(directory, `${target}${soft ? "-soft-float" : ""}.elf`);
|
||||
const args = [
|
||||
`--target=${target}`,
|
||||
"-march=mips32r2",
|
||||
"-mabi=32",
|
||||
...(soft ? ["-msoft-float"] : []),
|
||||
"-mno-abicalls",
|
||||
"-fno-pic",
|
||||
"-fuse-ld=lld",
|
||||
@@ -30,6 +41,7 @@ export async function buildMipsFixture(directory, byteOrder) {
|
||||
"-Wl,-e,rea_mips_entry",
|
||||
"-Wl,--build-id=none",
|
||||
source,
|
||||
...(soft ? [softSource] : []),
|
||||
probe,
|
||||
"-o",
|
||||
path,
|
||||
@@ -60,11 +72,19 @@ export async function buildMipsFixture(directory, byteOrder) {
|
||||
return {
|
||||
path,
|
||||
byte_order: byteOrder,
|
||||
float_abi: floatAbi,
|
||||
flags: read32(36),
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
source_sha256: createHash("sha256")
|
||||
.update(await readFile(source))
|
||||
.digest("hex"),
|
||||
...(soft
|
||||
? {
|
||||
soft_source_sha256: createHash("sha256")
|
||||
.update(await readFile(softSource))
|
||||
.digest("hex"),
|
||||
}
|
||||
: {}),
|
||||
probe_sha256: createHash("sha256")
|
||||
.update(await readFile(probe))
|
||||
.digest("hex"),
|
||||
|
||||
Reference in New Issue
Block a user