Skip to content

feat(ghidra): admit RISC-V ELF analysis through a pinned generic lane - #2151

Merged
morluto merged 5 commits into
morluto:mainfrom
NIU-123370:feat/ghidra-riscv-lane
Oct 12, 2026
Merged

morluto merged 5 commits into
morluto:mainfrom
NIU-123370:feat/ghidra-riscv-lane

Conversation

@NIU-123370

Copy link
Copy Markdown
Contributor

Summary

Adds RISC-V ELF admission for the Ghidra provider, closing the reported target_unavailable: unsupported ELF architecture failure (#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, raw e_flags) alongside MIPS.
  • Admitted shape: little-endian ELF32/ELF64 ET_EXEC declaring EF_RISCV_FLOAT_ABI_DOUBLE, pinned to Ghidra's RISCV:LE:32:default / RISCV:LE:64:default with the gcc compiler specification.
  • Everything else is refused with a named reason rather than a guessed profile.

On the two questions in the issue: I found no one else working on this — no open PR referenced #2063, and main had no RISC-V branch in elfArchitecture() at 60259a9. 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.opinion pins variant="default" for the 64-bit constraint, and leaves the 32-bit constraint with the in-file comment:

<constraint primary="243" processor="RISCV" endian="little" size="32"/> <!-- must choose between 'default' and 'AndeStar' variants -->

Measured on the installed release tree with analyzeHeadless against source-owned fixtures:

fixture Ghidra auto-selected
RV64 ELF, e_flags 0x5 (RVC, double-float ABI) RISCV:LE:64:default:gcc
RV32 ELF, e_flags 0x5 (RVC, double-float ABI), no Andes declaration RISCV:LE:32:AndeStar_v5:gcc

So 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

Declaration Outcome
LE, ELF64, ET_EXEC, float ABI double admitted, RISCV:LE:64:default + gcc
LE, ELF32, ET_EXEC, float ABI double admitted, RISCV:LE:32:default + gcc
big-endian refused — Ghidra 12.1.4 ships no big-endian RISC-V language
ET_REL, ET_DYN, other e_type refused with the declared value
EF_RISCV_RVE refused — 15-register ABI needs its own pspec/compiler verification
EF_RISCV_TSO refused — total-store-order memory model not verified here
soft / single / quad float ABI refused — the pinned generic cspec models double-float

Raw e_flags stay in Evidence as declarations (rvc_declared, rve_declared, tso_declared) and are not presented as instruction semantics.

One owner for the pinned import commitment

GhidraProviderClient compared 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 with The committed analysis profile and REA_GHIDRA_LANGUAGE_ID/REA_GHIDRA_COMPILER_SPEC_ID differ before 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 read HeaderArchitecture, derived from ELF_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.
  • Pre-existing MIPS/Hopper/IDA/language-override suites re-run unchanged: 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 only linux_x86_64 and win_x86_64), and its Makefile hard-codes -arch x86_64 on Darwin, so decompile and sleigh were built from the release sources for os/mac_arm_64; rea doctor then reports native_decompiler: ok matching darwin/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 no verify:ghidra:riscv script consumes it yet.

command result
rea analyze rv64.elf --provider ghidra exit 0, provider Ghidra 12.1.4
rea analyze rv32.elf --provider ghidra exit 0, provider Ghidra 12.1.4
rea decompile rv64.elf rea_riscv_entry --provider ghidra exit 0, pseudocode returned
rea decompile rv32.elf rea_riscv_entry --provider ghidra exit 0, pseudocode returned

Committed profile readings from that evidence:

  • RV64: 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}
  • RV32: same shape with RISCV:LE:32:default and elf32-exec-le-double-float-generic-v1

RV64 pseudocode:

void entry(void)
{
  gp = &__global_pointer_;
  rea_sum_table();
  return;
}

Refusal evidence on the same engine — a soft-float RV64 fixture (e_flags 0x1) is rejected before import with:

architecture_unsupported: This Ghidra RISC-V lane requires the EF_RISCV_FLOAT_ABI_DOUBLE declaration
(unspecified (soft-float convention) observed); the pinned generic language uses the double-float
compiler specification.

Not in this change

  • No verify:ghidra:riscv lane script. The runs above were performed manually against the installed engine. If you want the lane script in the shape of verify:ghidra:mips, I will add it here — I stopped rather than guess at the conformance-manifest contract.
  • Caller-supplied raw-binary entry keeps its existing four-family list; this is ELF-header admission only.
  • MCP-surface evidence for this lane is the shared CLI/MCP workflow plus unit tests; I did not run a live MCP session against the fixtures.
  • Andes, big-endian, RVE and TSO are refused, not supported.
  • Decompile output above is one function per fixture; I did not assert a call-graph or string-inventory completeness claim for RISC-V.

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T16:10:58.964966Z 2a05881 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@NIU-123370 NIU-123370 Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/ghidra/GhidraRiscvProfile.ts Outdated
Comment on lines +68 to +69
if (metadata.type !== ET_EXEC)
return `This Ghidra RISC-V lane supports static ET_EXEC only; ${

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@NIU-123370
NIU-123370 force-pushed the feat/ghidra-riscv-lane branch from f45f7f1 to 323c416 Compare October 12, 2026 12:28
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.
@morluto
morluto merged commit 0baad58 into morluto:main Oct 12, 2026
31 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 12, 2026
3 tasks done
@morluto

morluto commented Oct 12, 2026

Copy link
Copy Markdown
Owner

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants