Skip to content

fix(elf): admit Xtensa binaries through Ghidra - #10774

Closed
bezabyte wants to merge 1 commit into
morluto:mainfrom
bezabyte:codex/fix-xtensa-elf-admission
Closed

bezabyte wants to merge 1 commit into
morluto:mainfrom
bezabyte:codex/fix-xtensa-elf-admission

Conversation

@bezabyte

Copy link
Copy Markdown
Contributor

Summary

Fixes #10765.

Xtensa ELF files (EM_XTENSA = 94), including ESP32-S3 firmware, are now admitted through the normal Ghidra ELF path instead of being rejected before analysis.

Problem and expected behavior

The ELF header parser did not recognize machine 94, so parseElf returned unsupported ELF architecture before a provider could run. Even if that gate was opened, the canonical architecture enum and Ghidra admission set also lacked xtensa.

Expected behavior is for Xtensa ELF files to produce the canonical xtensa target family, pass Ghidra support admission, and use Ghidra's auto-from-header language selection. Other providers without a verified Xtensa lane should continue to decline it explicitly.

Change and scope

  • Map ELF machine 94 to xtensa.
  • Add xtensa to the canonical binary architecture set and ELF target metadata.
  • Add xtensa to Ghidra's supported architecture set.
  • Keep headerless raw-binary selection unchanged; this contribution is for ELF headers only.
  • Make Hopper decline Xtensa with its existing Ghidra-directed reason rather than trying to derive an unsupported loader flag.

No new processor profile, calling-convention policy, CLI/MCP tool, generated catalog, or package/install behavior is included.

Contract and boundary impact

  • Semantic owner and earliest changed stage: ELF header parsing owns the machine-to-family fact; canonical target typing and Ghidra admission consume that fact.

  • Contribution scope fit and linked maintainer decision: routine support fix within existing ELF/Ghidra boundaries; maintainer scope decision not applicable.

  • CLI and MCP/tool-catalog contract: no schema or tool change; existing CLI/MCP artifact workflows can now reach the provider for this ELF family.

  • Provider, bridge, target-format, or platform compatibility: adds ELF Xtensa recognition; Ghidra uses its header-selected Xtensa language, while Hopper remains explicitly unsupported.

  • Evidence, artifact, provenance, or reconstruction contract: adds one canonical architecture value and preserves the existing target identity/evidence flow.

  • Target selection, authority, upstream behavior, and cleanup details: no new target execution, injection, mutation, network, or persistence behavior.

  • Generated metadata, package, or installation impact: none.

  • Read existing files or captured data.

  • Launch or attach to a process.

  • Inject instrumentation or run scripts/code.

  • Change process memory, registers, files, or configuration.

  • Contact network services.

  • Recover protected code or work around protections.

  • Documentation or usability only; no target interaction.

  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: ELF parser recognition, Ghidra Xtensa profile selection, Hopper's explicit decline path, and the admitted-family diagnostic expectation.
  • Base reproduction or other evidence: issue ELF files with e_machine = EM_XTENSA (94) are rejected before analysis, although Ghidra supports Xtensa #10765 reports an ESP32-S3 ELF failing with unsupported ELF architecture: e_machine 94 before analysis; source inspection confirms the missing parser and provider gates on the base revision. The issue reporter supplied real Ghidra verification, but that runtime was not available on this host.
  • User-visible CLI/MCP output: not run with a real Xtensa artifact on this host.
  • Remaining proof gaps: a real ESP32-S3 ELF through CLI and MCP with Ghidra remains a CI/maintainer verification step.

Before/after source-and-verification snapshots:

Before: Xtensa ELF rejected before analysis

After: Xtensa ELF admitted through Ghidra

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm exec vitest -- run src/domain/binaryTarget.test.ts src/ghidra/GhidraProvider.behavior.test.ts src/hopper/HopperProvider.test.ts — 3 files passed; 363 tests passed.
  • npm run check:fast — typecheck and lint passed; 2 tasks successful.
  • npm exec oxfmt -- ...changed files... — formatting passed.
  • xmllint --noout pr_screenshots/rea-10765-before.svg pr_screenshots/rea-10765-after.svg — both evidence assets parsed successfully.
  • git diff --check — no whitespace errors.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: no real Xtensa artifact or Ghidra installation was available on this host; source parser/provider tests passed. The issue's external ESP32-S3/Ghidra measurements are cited as reported evidence, not re-run evidence.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no new execution or mutation capability; this only broadens static ELF admission to an existing Ghidra workflow.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches the bounded workflow for analyzing Xtensa ELF files.

The runtime summary schemas must match the ELF-only Xtensa type before merging.

Findings

  1. P2 Schemas allow impossible Xtensa targets ▶
Fix with agent prompt
### Issue 1
src/domain/binaryTargetTypes.ts:undefined-72
This change excludes `xtensa` from `HeaderArchitecture`, but `headerArchitectureSchema` in `src/domain/liveBinaryTargetSummary.ts` still includes it through `BINARY_ARCHITECTURES`. The CLI/MCP output schemas therefore advertise Xtensa PE and Mach-O targets, although `BinaryTarget` permits Xtensa only for ELF.

Add `xtensa` to the runtime exclusion and test that summaries accept Xtensa ELF but reject those other combinations. The repository requires runtime schemas and exported types to agree when constrained values change. That requirement must be satisfied before merging.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds ELF machine 94 recognition and admits the resulting xtensa targets through Ghidra's existing header-selected import path.

  • Xtensa ELF files can now reach Ghidra’s normal analysis path.

Reviews (2) · Last reviewed commit: "fix(elf): admit Xtensa binaries through ..." · Reviewed by Greptile

| ElfMetadataArchitecture
| RawOnlyArchitecture
| "hexagon"
| "xtensa"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Schemas allow impossible Xtensa targets

This change excludes xtensa from HeaderArchitecture, but headerArchitectureSchema in src/domain/liveBinaryTargetSummary.ts still includes it through BINARY_ARCHITECTURES. The CLI/MCP output schemas therefore advertise Xtensa PE and Mach-O targets, although BinaryTarget permits Xtensa only for ELF.

Add xtensa to the runtime exclusion and test that summaries accept Xtensa ELF but reject those other combinations. The repository requires runtime schemas and exported types to agree when constrained values change. That requirement must be satisfied before merging.

Context Used: docs/agents/mcp-tooling.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/domain/binaryTargetTypes.ts
Line: 72

Comment:
**Schemas allow impossible Xtensa targets**

This change excludes `xtensa` from `HeaderArchitecture`, but `headerArchitectureSchema` in `src/domain/liveBinaryTargetSummary.ts` still includes it through `BINARY_ARCHITECTURES`. The CLI/MCP output schemas therefore advertise Xtensa PE and Mach-O targets, although `BinaryTarget` permits Xtensa only for ELF.

Add `xtensa` to the runtime exclusion and test that summaries accept Xtensa ELF but reject those other combinations. The repository requires runtime schemas and exported types to agree when constrained values change. That requirement must be satisfied before merging.

**Context Used:** docs/agents/mcp-tooling.md ([source](https://github.com/morluto/preference/blob/main/docs/agents/mcp-tooling.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@XiaoBinGan XiaoBinGan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I traced xtensa through every consumer of BINARY_ARCHITECTURES and found one place where the change is incomplete, in a way tsc cannot see.

The finding. src/domain/binaryTargetTypes.ts correctly excludes xtensa from HeaderArchitecture (the type says a header-only family cannot claim it). But src/domain/liveBinaryTargetSummary.ts:16-21 builds the matching runtime schema from a hand-written list:

const headerArchitectureSchema = architectureSchema.exclude([
  ...ELF_METADATA_ARCHITECTURES,
  ...RAW_ONLY_ARCHITECTURES,
  "hexagon",
  "arm64_32",
]);

"xtensa" is not in that list, and nothing in the file mentions it. So the type and the schema now disagree, and the schema wins at runtime:

input expected actual
format: "pe", architecture: "xtensa" rejected — xtensa is not a header family accepted
format: "mach-o", architecture: "xtensa" rejected accepted
format: "dos-com", architecture: "xtensa" rejected accepted
format: "pe", architecture: "hexagon" rejected rejected

I called liveBinaryTargetSummarySchema.safeParse directly on those values to confirm it, rather than reasoning from the code. hexagon rejects in exactly the same four formats, which is what makes this a gap rather than a design choice: hexagon is excluded in both the type and the schema, and xtensa is now excluded only in the type.

Why no check caught it. architectureSchema.exclude([...]) is a runtime array literal; TypeScript has no way to tie it to the Exclude<…, "hexagon" | "xtensa" | "arm64_32"> in HeaderArchitecture. tsc --noEmit is 0 errors on this branch. This is the one shape of architecture addition that the compiler cannot enforce, so it has to be a checklist item.

The fix is one line — add "xtensa" beside "hexagon" at liveBinaryTargetSummary.ts:19. A test that asserts headerArchitectureSchema and HeaderArchitecture agree would be better still: this file is now the only place a new family has to be registered twice, and the second registration is invisible to the type system.

Two smaller notes on the PR.

src/domain/binaryTarget.ts:534-544 adds the second architecture === "xtensa" branch, but parseElf already returns through the earlier ok({...}) for ElfMetadataArchitecture. I checked that xtensa is not in ELF_METADATA_ARCHITECTURES (binaryTargetTypes.ts:52-56 lists mips, riscv, powerpc), so the new branch is reached — just worth confirming it is not dead code by intent of the earlier guard.

pr_screenshots/rea-10765-{before,after}.svg add a top-level directory that nothing references (git grep pr_screenshots in src/tests/docs returns nothing) and that is not in package.json's files. AGENTS.md closes with "Never commit binaries, … or local planning artifacts", and an illustrated before/after of a source diff is a planning artifact; the diff and the PR body already carry it.

What I could not verify: no Xtensa hardware, no Ghidra, and no ESP32-S3 binary here, so the claim that Ghidra's Xtensa language module admits these images is taken from the issue and the PR description. Everything above is source-level and reproducible on any host.

gensokyo-dev added a commit that referenced this pull request Nov 15, 2026
Admit ELF machine 94 as the canonical Xtensa family through Ghidra's header-selected import. Share generic-header exclusions between static target types and live CLI/MCP summaries, preserving ELF-only format constraints and other providers' decline paths.

Fixes #10765.
Adapted from #10774.

Co-authored-by: Josh <[email protected]>
Co-authored-by: gensokyo-dev <[email protected]>
@morluto

morluto commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Thank you for this work. #11178 (1995ab7) adapted it with your authorship kept. Xtensa ELF headers now produce canonical xtensa targets that pass Ghidra admission. That fixed #10765. Closing as superseded by that merge. Please reopen if anything from this branch is still missing.

@morluto morluto closed this Nov 15, 2026
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working provider: ghidra Analysis provider / engine: ghidra labels Nov 22, 2026
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 bug Something isn't working provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ELF files with e_machine = EM_XTENSA (94) are rejected before analysis, although Ghidra supports Xtensa

4 participants