Files
reverse-engineering-anything/src/application/MipsElfSharedLoad.ts
T
170429eb7d feat(ghidra): admit plain MIPS32 PIC shared images (#2362)
* feat(ghidra): admit plain MIPS32 PIC shared images

Add an elf32-dyn-o32-arch32r2-standard-v1 lane for ET_DYN images without
an ASE flag (shared libraries and ET_DYN executables), beside the MIPS16
lane. It reuses the complete dynamic-relocation inspection and admits
R_MIPS_NONE, symbol-bearing R_MIPS_REL32 and the TLS loader slots
DTPMOD32/DTPREL32/TPREL32, which stay unapplied with a limitation.

Ghidra's default moves an image linked at 0 to 0x10000 and then shifts
only nonzero local GOT entries, so page-zero GOT entries (how GNU-ld
libraries call static functions) keep unshifted values and calls through
them decompile to wrong addresses. The lane imports at the image's own
ELF image base instead (lowest PT_LOAD in the first 20 headers, as Ghidra
computes it), commits it in the analysis profile and requires the bridge
handshake to report it. Images with a prelink trailer are refused.

verify:ghidra:mips32-shared checks LE, BE and soft-float LLD fixtures laid
out like GNU ld output against an independent readelf oracle: every
REL32, local GOT entry (including a zero page entry), global GOT entry
(EXTERNAL or lazy-binding stub) and unapplied TLS slot, plus named calls
in the decompilation, snapshot round trip and CLI evidence.

Refs #2338

* style(ghidra): format link-base regression scenarios

* test(ghidra): require zero-valued local GOT coverage

* test(ghidra): cover TPREL32 in the shared lane and drop duplicated session suites

Add an initial-exec TLS variable so the MIPS32 shared fixture declares
R_MIPS_TLS_TPREL32 beside DTPMOD32/DTPREL32, and require all three slots
to stay UNSUPPORTED with unchanged bytes. Remove the soft-float and
mutation handshake suites that the main reconciliation duplicated in
GhidraSessionValues.test.ts.

* test(ghidra): read the section table in the MIPS32 shared-library oracle

parseMipsReadelf now confirms the fixture's section types from readelf -S
(main added -S to the executable, relocatable and PSP oracles). Request it in
the shared-library oracle too.

* test(ghidra): follow main's verifier contracts in the MIPS32 shared lane

Main made the Ghidra installation probe asynchronous behind the shared
inspectVerifierGhidraInstallation helper, and analyze_function now returns a
summary unless detail "complete" is requested. Use both, as the other MIPS
lanes do.

* refactor(ghidra): keep Ghidra's image-base rule out of shared ELF parsing

The shared MIPS ELF reader computed Ghidra's ElfHeader.getImageBase rule
(lowest PT_LOAD address among the first 20 program headers) and the domain
type described it as Ghidra's base. Record source facts instead: every
PT_LOAD virtual address with its program-header index, plus the Android
prelink trailer. ghidraMipsElfImageBase in GhidraMipsProfile now applies
Ghidra's rule to those facts. The committed elf_image_base is unchanged.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-11-19 01:51:37 +00:00

284 lines
9.5 KiB
TypeScript

import { readElfRelocationEntry } from "../domain/elfRelocation.js";
import { readElfDynamicEntry } from "../domain/elfDynamicEntry.js";
import { BinaryReader } from "../domain/binaryContainer.js";
import type { ElfProgramHeader } from "../domain/elfProgramHeader.js";
import type { MipsElfMetadata } from "../domain/binaryTargetTypes.js";
import { err, ok, type Result } from "../domain/result.js";
type ReadRange = (
offset: number,
length: number,
label: string,
) => Promise<Result<Buffer, string>>;
export interface MipsElfRelocationSection {
readonly type: number;
readonly offset: number;
readonly size: number;
readonly entrySize: number;
readonly compressed: boolean;
}
type SharedLoadFacts = Extract<
NonNullable<MipsElfMetadata["sharedLoad"]>,
{ status: "complete" }
>;
/** Source ELF facts only; the provider owns interpretation and admission. */
export const readMipsElfSharedLoad = async (
programHeaders: readonly ElfProgramHeader[],
little: boolean,
fileSize: number,
read: ReadRange,
checkCancelled: () => void,
sections: readonly MipsElfRelocationSection[],
): Promise<Result<SharedLoadFacts, string>> => {
const loads: {
index: number;
offset: number;
address: number;
size: number;
memorySize: number;
}[] = [];
let dynamic: { offset: number; address: number; size: number } | undefined;
for (const [index, program] of programHeaders.entries()) {
checkCancelled();
const { type } = program;
const offset = Number(program.offset);
const size = Number(program.fileSize);
if (type === 1) {
const address = Number(program.virtualAddress);
const memorySize = Number(program.memorySize);
if (
offset > fileSize - size ||
size > memorySize ||
address + memorySize > 0x100000000
)
return err("invalid or incomplete MIPS PT_LOAD range");
loads.push({ index, offset, address, size, memorySize });
}
if (type === 2) {
if (dynamic !== undefined) return err("duplicate MIPS PT_DYNAMIC tables");
dynamic = { offset, address: Number(program.virtualAddress), size };
}
}
if (dynamic === undefined)
return err("MIPS shared image has no PT_DYNAMIC table");
if (dynamic.size === 0 || dynamic.size % 8 !== 0)
return err("invalid MIPS ELF32 dynamic table size");
if (dynamic.offset > fileSize - dynamic.size)
return err("MIPS PT_DYNAMIC range extends beyond the file");
// Source facts only: every PT_LOAD address with its program-header index.
// Providers apply their own image-base selection rules to these.
const loadAddresses = loads.map((load) => ({
headerIndex: load.index,
virtualAddress: load.address,
}));
// Android apriori prelink trailer: the prelinked base word, then the tag
// "PRE " (padding around the tag is tolerated).
let prelinkBase: number | null = null;
if (fileSize >= 8) {
const trailer = await read(fileSize - 8, 8, "MIPS prelink trailer");
if (!trailer.ok) return err(trailer.error);
const tag = trailer.value.subarray(4).toString("latin1");
if (tag.replace(/^[\u0000-\u0020]+|[\u0000-\u0020]+$/gu, "") === "PRE")
prelinkBase = new BinaryReader(trailer.value, little, "elf").u32(0);
}
const ranges = loads
.filter((load) => load.memorySize > 0)
.sort((left, right) => left.address - right.address);
for (let i = 1; i < ranges.length; i += 1) {
checkCancelled();
const previous = ranges[i - 1]!;
if (ranges[i]!.address < previous.address + previous.memorySize)
return err(
"overlapping MIPS PT_LOAD memory ranges require separate mapping verification",
);
}
// One normalized index prevents relocation count * program-header count work.
const mapped = (address: number, length: number, fileBacked: boolean) => {
let low = 0;
let high = ranges.length;
while (low < high) {
const middle = Math.floor((low + high) / 2);
if (ranges[middle]!.address <= address) low = middle + 1;
else high = middle;
}
const range = ranges[low - 1];
return range !== undefined &&
address - range.address <=
(fileBacked ? range.size : range.memorySize) - length
? range
: undefined;
};
const dynamicMapping = mapped(dynamic.address, dynamic.size, true);
if (
dynamicMapping === undefined ||
dynamicMapping.offset + dynamic.address - dynamicMapping.address !==
dynamic.offset
)
return err(
"MIPS PT_DYNAMIC has no unique matching file-backed PT_LOAD range",
);
// Bound temporary storage, not admitted artifact size. ELF32 records are
// exact eight-byte structures; every declared record is examined or refused.
const chunkBytes = 64 * 1024;
const DT_FLAGS_1 = 0x6ffffffb;
const relevant = new Set([
2,
7,
8,
9,
17,
18,
19,
20,
23,
35,
36,
37,
DT_FLAGS_1,
]);
const tags = new Map<number, number>();
let terminated = false;
for (
let offset = 0;
offset < dynamic.size && !terminated;
offset += chunkBytes
) {
const chunk = await read(
dynamic.offset + offset,
Math.min(chunkBytes, dynamic.size - offset),
"dynamic table",
);
if (!chunk.ok) return chunk;
for (let position = 0; position < chunk.value.length; position += 8) {
checkCancelled();
const parsedEntry = readElfDynamicEntry(
chunk.value.subarray(position, position + 8),
32,
little,
);
if (!parsedEntry.ok) return err(parsedEntry.error.message);
const tag = Number(BigInt.asUintN(32, parsedEntry.value.tag));
if (tag === 0) {
terminated = true;
break;
}
if (!relevant.has(tag)) continue;
if (tags.has(tag)) return err(`duplicate MIPS dynamic tag ${tag}`);
tags.set(tag, Number(parsedEntry.value.value));
}
}
if (!terminated) return err("MIPS dynamic table has no DT_NULL terminator");
const DF_1_PIE = 0x08000000;
const imageFacts = {
loadAddresses,
prelinkBase,
...(((tags.get(DT_FLAGS_1) ?? 0) & DF_1_PIE) === 0
? {}
: { declaresPie: true as const }),
};
if ([35, 36, 37].some((tag) => tags.has(tag)))
return err(
"MIPS shared RELR relocation declarations require separate verification",
);
if ([2, 7, 8, 9, 23].some((tag) => (tags.get(tag) ?? 0) !== 0))
return err(
"MIPS shared RELA or PLT relocation declarations require separate verification",
);
const address = tags.get(17);
const size = tags.get(18);
const stride = tags.get(19);
const checkSections = (
offset: number,
length: number,
): string | undefined => {
let observed = 0;
for (const section of sections) {
checkCancelled();
if (section.compressed)
return `MIPS section relocation declarations at file offset ${section.offset} use compressed storage; decoded relocation coverage is unavailable`;
if (section.size === 0) continue;
if (
section.type !== 9 ||
section.entrySize !== 8 ||
section.offset !== offset ||
section.size !== length
)
return "MIPS section relocation declarations outside the dynamic REL table require separate verification";
observed += 1;
}
return observed > 1
? "duplicate MIPS section relocation declarations require separate verification"
: undefined;
};
const emptyRelocations = (): Result<SharedLoadFacts, string> => {
const reason = checkSections(0, 0);
return reason === undefined
? ok({
status: "complete",
relocationCount: 0,
relocationTypes: [],
symbolRelativeCount: 0,
...imageFacts,
})
: err(reason);
};
if (address === undefined && size === undefined && stride === undefined)
return emptyRelocations();
if (
address === undefined ||
size === undefined ||
stride !== 8 ||
size % 8 !== 0
)
return err(
"incomplete or invalid MIPS DT_REL/DT_RELSZ/DT_RELENT declaration",
);
if (size === 0) return emptyRelocations();
const mapping = mapped(address, size, true);
if (mapping === undefined)
return err(
"MIPS DT_REL range has no unique complete file-backed PT_LOAD mapping",
);
const fileOffset = mapping.offset + address - mapping.address;
const sectionReason = checkSections(fileOffset, size);
if (sectionReason !== undefined) return err(sectionReason);
let symbolRelativeCount = 0;
const types = new Set<number>();
for (let offset = 0; offset < size; offset += chunkBytes) {
const chunk = await read(
fileOffset + offset,
Math.min(chunkBytes, size - offset),
"dynamic REL records",
);
if (!chunk.ok) return chunk;
for (let position = 0; position < chunk.value.length; position += 8) {
checkCancelled();
const parsedEntry = readElfRelocationEntry(
chunk.value.subarray(position, position + 8),
32,
little,
"rel",
);
if (!parsedEntry.ok) return err(parsedEntry.error.message);
const { type, symbolIndex, offset: rawAddress } = parsedEntry.value;
const relocationAddress = Number(rawAddress);
if (type === 3 && mapped(relocationAddress, 4, false) === undefined)
return err(
"MIPS REL32 destination has no unique complete PT_LOAD memory range",
);
types.add(type);
if (type === 3 && symbolIndex !== 0) symbolRelativeCount += 1;
}
}
return ok({
status: "complete",
relocationCount: size / 8,
relocationTypes: [...types].sort((left, right) => left - right),
symbolRelativeCount,
...imageFacts,
});
};