Repository navigation
feat(ghidra): admit RISC-V ELF analysis through a pinned generic lane - #2151
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f45f7f1dce
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return `This Ghidra RISC-V lane requires the EF_RISCV_FLOAT_ABI_DOUBLE declaration (${riscvUnsupportedFloatAbi( | ||
| abi, | ||
| )} observed); the pinned generic language uses the double-float compiler specification.`; | ||
| return null; |
There was a problem hiding this comment.
Reject unrecognized RISC-V e_flags bits
For an ELF whose e_flags combines the admitted double-float value with any reserved or future bit (for example 0x80000005), the RVE, TSO, and float-ABI checks all pass and this returns null, so a malformed or newer declaration is silently committed to the generic verified lane. Reject bits outside the explicitly supported mask with a named reason rather than treating their applicability as aligned.
AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 323c416 (the review was against f45f7f1).
e_flags bits outside EF_RISCV_RVC | EF_RISCV_FLOAT_ABI are now refused, with the offending value printed rather than swallowed:
RISC-V e_flags declares bits outside the verified lane (0x80000000); reserved or
newer declarations are not treated as compatible with this profile.
The named EF_RISCV_RVE / EF_RISCV_TSO / float-ABI reasons stay ahead of the mask so those declarations keep their specific text. Two cases cover it: 0x8000_0005 (double-float plus a reserved high bit) and 0x24 (double-float plus an unknown low bit) — the first shape you gave passes every earlier check, so it was a real hole rather than a wording issue.
| if (metadata.type !== ET_EXEC) | ||
| return `This Ghidra RISC-V lane supports static ET_EXEC only; ${ |
There was a problem hiding this comment.
Verify static linkage instead of equating it with ET_EXEC
When a dynamically linked non-PIE RISC-V executable has e_type == ET_EXEC, this condition admits it even if its program headers contain PT_INTERP and dynamic dependencies; ELF ET_EXEC means a fixed-address executable, not a statically linked one. readExecutableMetadata returns RISC-V at BinaryTargetResolver.ts:462-468 without inspecting those program headers, so such a target receives the lane that this error text claims is static-only despite its load shape being unverified; inspect and reject dynamic linkage or verify it as a separate lane.
AGENTS.md reference: AGENTS.md:L17-L17
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, and this was an overclaim in my text rather than in the admission logic.
The refusal now says "fixed-address ET_EXEC" and the lane states the boundary in the session limitations:
Admission reads `e_type` only: a fixed-address ET_EXEC declaration does not
establish static linkage, and program headers (`PT_INTERP`, dynamic dependencies)
are not inspected by this lane.
A unit test pins that sentence so the claim cannot drift back. I did not add program-header inspection here: it would need the same dynamic-table reading the MIPS shared-image profile does, and bundling it would hide what this lane actually verified. Happy to make it a follow-up if you want it in this PR instead.
| "arm", | ||
| "arm64", | ||
| "mips", | ||
| "riscv", |
There was a problem hiding this comment.
Document the advertised RISC-V provider lane
Adding RISC-V to the supported set makes this a user-facing Ghidra capability, but a repo-wide RISC-V search finds no positive support or lane-boundary documentation in README.md or its linked guides, while docs/firmware-analysis.md:125-128 still tells users that RISC-V decompilation was not added. Document the admitted/refused shapes and setup path where provider support is required to live so users do not receive contradictory guidance.
AGENTS.md reference: AGENTS.md:L5-L5
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed. docs/firmware-analysis.md said "does not add MIPS/PPC/RISC-V decompilation" — that line was already stale for MIPS before this PR, and this PR made it wrong twice.
Added docs/ghidra-riscv.md in the shape of docs/ghidra-mips.md: admission boundary, the refusal table, why the language is pinned per class (with the measured RISCV:LE:32:AndeStar_v5 auto-selection for an ordinary RV32 image), the Evidence fields, and an explicit "what these fixtures do not establish" section including the missing verify:ghidra:riscv script. docs/firmware-analysis.md now names PPC only and cross-links both lane guides.
I left the VitePress nav alone because ghidra-mips.md and ghidra-psp.md are not in it either — say the word if provider lane guides should start appearing there and I will add all three.
Refs morluto#2063. Recognize `EM_RISCV` (243) as a provider-neutral family that carries its own ELF facts, and bound the first Ghidra lane to little-endian `ET_EXEC` images that declare the double-float ABI, pinned to `RISCV:LE:32:default` or `RISCV:LE:64:default` with the `gcc` compiler specification. Ghidra 12.1.4 is not reliable without the pin: its `riscv.opinion` pins `variant="default"` for the 64-bit constraint and leaves the 32-bit constraint with the comment that the loader "must choose between 'default' and 'AndeStar' variants". Measured on the installed release tree, a source-owned generic RV32 ELF with no Andes declaration imports as `RISCV:LE:32:AndeStar_v5:gcc`, while RV64 resolves to `RISCV:LE:64:default:gcc`. The lane therefore states the language per width and refuses any caller-selected processor outside it instead of inheriting the header guess. Refusals name the declaration rather than falling back to a guessed profile: big-endian images (Ghidra 12.1.4 ships no big-endian RISC-V language), non-`ET_EXEC` images, `EF_RISCV_RVE`, `EF_RISCV_TSO`, and every float ABI other than double, which the pinned generic compiler specifications model. The raw e_flags bits stay in Evidence as declarations, not as instruction semantics. Family recognition now has one owner for the ELF-metadata families: `ELF_METADATA_ARCHITECTURES`, `isElfMetadataArchitecture`, and `HeaderArchitecture` replace ten `Exclude<BinaryArchitecture, "mips">` sites that would each have needed a second hand-edited member. `ghidraPinnedLanguage` is that owner for the import commitment: the session readback previously re-derived only the MIPS pin, so a profile committing any second pinned language failed with a language-mismatch error before the bridge started. Hopper and IDA refuse RISC-V by name, matching the MIPS wording. Caller-supplied raw-binary entry keeps its existing four-family list; this lane is ELF-header admission only. Verification on macOS 15.7.7 arm64, Node 24.18.0, Ghidra 12.1.4 PUBLIC and JDK 21.0.12.1, with the host decompiler built from the release sources for `os/mac_arm_64`: `npm run check` passes; `npm run test:fast` passes 396 test files; `npm run verify:test-discovery` discovers all 917 test files exactly once. Source-owned RV32 and RV64 double-float `ET_EXEC` fixtures complete `rea analyze --provider ghidra` and `rea decompile rea_riscv_entry`, committing `RISCV:LE:32:default` and `RISCV:LE:64:default` with `language_selection: provider-pinned-v1`; the soft-float RV64 shape is refused with the named float-ABI reason, and the full refusal matrix including the RV32 and width-mismatch shapes is covered by unit tests. No `verify:ghidra:riscv` lane script is included in this change; the runs above were performed manually against the installed engine. Fork pull requests receive no CI check runs, so the real-provider scope above is the evidence available for review.
Address review feedback on morluto#2151. `e_flags` bits outside `EF_RISCV_RVC | EF_RISCV_FLOAT_ABI` are now refused with the offending value named, so a reserved or newer producer declaration cannot enter the verified generic lane by falling through the RVE, TSO and float-ABI checks. Admission reads `e_type` only, so the refusal text no longer calls `ET_EXEC` static: a fixed-address declaration does not establish static linkage, and `PT_INTERP` and dynamic dependencies are not inspected. The session records that boundary as a limitation rather than inferring a load model. Add `docs/ghidra-riscv.md` in the shape of the MIPS lane guide (admission boundary, refusal table, why the language is pinned per class, Evidence fields, and what the fixtures do not establish), and correct `docs/firmware-analysis.md`, which still told users that MIPS and RISC-V decompilation had not been added. Verification after the change: `npm run check` and `npm run docs:check` pass; the RISC-V module tests pass (17 lane cases plus 9 parser cases); on Ghidra 12.1.4 with JDK 21.0.12.1 the RV32 and RV64 double-float fixtures still analyze and decompile with `RISCV:LE:32:default` / `RISCV:LE:64:default` and `language_selection: provider-pinned-v1`, the soft-float fixture is still refused by name, and the new linkage limitation appears in the emitted Evidence.
f45f7f1 to
323c416
Compare
Merge current main into the RISC-V Ghidra lane. Retain the required selected slice bounds for universal Mach-O targets and the dedicated RISC-V ELF metadata, using HeaderArchitecture for the remaining executable variants. Validation: 253 focused tests; npm run check; npm run docs:check; npm run verify:test-discovery. Real Ghidra workflows were not rerun for this conflict resolution.
Reuse the complete lane admission boundary when selecting a language, retain unsigned unsupported flag bits in diagnostics, and derive float ABI metadata from the source declaration.
Build source-owned RV32/RV64 fixtures with Clang and LLD, establish independent ELF and symbol facts with LLVM readelf, and verify real provider evidence, instruction bytes, calls, decompilation, schema projections, snapshot replay, refusal diagnostics, source immutability and cleanup. Verified on macOS ARM64 with Ghidra 12.1.4, JDK 21 and LLVM/LLD 23.1.2. Dynamic linkage, big-endian, Andes and RVE/TSO workflows remain unverified; ISA attributes are not inspected.
|
@NIU-123370, thank you for adding a deliberately bounded RISC-V lane and spelling out which ELF declarations it admits. Pinning the language per width is especially well motivated by your finding that Ghidra can auto-select its AndeStar variant for an ordinary RV32 image; the explicit refusal boundaries keep that uncertainty visible. Welcome to REA! |
Summary
Adds RISC-V ELF admission for the Ghidra provider, closing the reported
target_unavailable: unsupported ELF architecturefailure (#2063) for RV32 and RV64.The lane is deliberately bounded and pinned:
EM_RISCV(243) becomes a provider-neutral family that carries its own ELF facts (elfClass,byteOrder,e_type, rawe_flags) alongside MIPS.ET_EXECdeclaringEF_RISCV_FLOAT_ABI_DOUBLE, pinned to Ghidra'sRISCV:LE:32:default/RISCV:LE:64:defaultwith thegcccompiler specification.On the two questions in the issue: I found no one else working on this — no open PR referenced #2063, and
mainhad no RISC-V branch inelfArchitecture()at60259a9. I also know from the issue text that @desayVincent intended to implement this; if this shape is not the one you want, say so and I will drop it or rebase onto yours — I would rather this land as your design than mine. The measured language-selection finding below is offered either way.Why the language is pinned, not auto-selected
Ghidra 12.1.4's
Ghidra/Processors/RISCV/data/languages/riscv.opinionpinsvariant="default"for the 64-bit constraint, and leaves the 32-bit constraint with the in-file comment:Measured on the installed release tree with
analyzeHeadlessagainst source-owned fixtures:e_flags 0x5(RVC, double-float ABI)RISCV:LE:64:default:gcce_flags 0x5(RVC, double-float ABI), no Andes declarationRISCV:LE:32:AndeStar_v5:gccSo the AndeStar result in the report is not specific to an Andes binary — it is what Ghidra chooses for an ordinary RV32 ELF. Accepting the header-based selection would silently analyze generic RV32 images under an Andes instruction set, so the lane states the language per width and refuses a caller-selected processor outside it.
Lane boundary
ET_EXEC, float ABI doubleRISCV:LE:64:default+gccET_EXEC, float ABI doubleRISCV:LE:32:default+gccET_REL,ET_DYN, othere_typeEF_RISCV_RVEEF_RISCV_TSORaw
e_flagsstay in Evidence as declarations (rvc_declared,rve_declared,tso_declared) and are not presented as instruction semantics.One owner for the pinned import commitment
GhidraProviderClientcompared the committed profile's language against a re-derived MIPS-only pin. That is fine while MIPS is the only pinned lane, but any second family committing a pin fails withThe committed analysis profile and REA_GHIDRA_LANGUAGE_ID/REA_GHIDRA_COMPILER_SPEC_ID differbefore the bridge starts — which is exactly what happened to this lane when it first ran.ghidraPinnedLanguage(target)is now the single owner used by both the profile commitment and the session readback, with a regression test asserting they agree for RV32 and RV64.Relatedly, ten
Exclude<BinaryArchitecture, "mips">sites (peMachine,rawBinary,GhidraRawBinaryLanguage, Hopper profiles,binaryTarget/binaryTargetTypes) would each have needed a hand-added second member. They now readHeaderArchitecture, derived fromELF_METADATA_ARCHITECTURES/isElfMetadataArchitecture.Hopper and IDA refuse RISC-V by name, matching the existing MIPS wording.
Verification
Host: macOS 15.7.7 arm64, Node 24.18.0 (
.nvmrc), npm 11.16.0 (packageManager).npm run check— passes (typecheck, lint + module boundaries,format:check, knip,metadata:check, architecture guards).npm run test:fast— passes, 396 test files.npm run verify:test-discovery— all 917 test files discovered exactly once.GhidraMipsProfile(41),GhidraLanguageOverride(18),HopperAnalysisProfile,binaryTarget(35),binaryTarget.mips(6) all pass.Real provider: Ghidra 12.1.4 PUBLIC + JDK 21.0.12.1. Ghidra 12.1.4 ships no macOS decompiler (
Decompiler/os/contains onlylinux_x86_64andwin_x86_64), and itsMakefilehard-codes-arch x86_64on Darwin, sodecompileandsleighwere built from the release sources foros/mac_arm_64;rea doctorthen reportsnative_decompiler: okmatchingdarwin/arm64.Fixtures were built locally for these runs from a small freestanding assembly probe with
riscv64-elf-gcc 16.2.0(-nostdlib -static -fno-pic -Wl,-e,rea_riscv_entry) — no third-party binaries, and the probe is not committed in this change because noverify:ghidra:riscvscript consumes it yet.rea analyze rv64.elf --provider ghidrarea analyze rv32.elf --provider ghidrarea decompile rv64.elf rea_riscv_entry --provider ghidrarea decompile rv32.elf rea_riscv_entry --provider ghidraCommitted profile readings from that evidence:
language_id: RISCV:LE:64:default,compiler_spec_id: gcc,language_selection: provider-pinned-v1,riscv_support_lane: elf64-exec-le-double-float-generic-v1,riscv_elf: {elf_class: 64, byte_order: "little", type: 2, flags: 5, rvc_declared: true}RISCV:LE:32:defaultandelf32-exec-le-double-float-generic-v1RV64 pseudocode:
Refusal evidence on the same engine — a soft-float RV64 fixture (
e_flags 0x1) is rejected before import with:Not in this change
verify:ghidra:riscvlane script. The runs above were performed manually against the installed engine. If you want the lane script in the shape ofverify:ghidra:mips, I will add it here — I stopped rather than guess at the conformance-manifest contract.Follow-ups worth an issue if you want them: an ELF64 RV32-class mismatch shape (refused today by width-derived pinning, not by a dedicated reason), and any RISC-V relocation/PIE lane.