Skip to content

fix(arch): detect MIPS N32 binaries as 64-bit - #1235

Open
kisnner26 wants to merge 1 commit into
hugsy:mainfrom
kisnner26:fix/mips-n32-arch-detection
Open

kisnner26 wants to merge 1 commit into
hugsy:mainfrom
kisnner26:fix/mips-n32-arch-detection

Conversation

@kisnner26

Copy link
Copy Markdown

Description

MIPS N32 executables are ELF32 files (EI_CLASS = 32-bit) flagged with EF_MIPS_ABI2 (e_flags & 0x20), but they use 64-bit registers. GEF only checked the ELF class, so N32 binaries were matched to MIPS (32-bit) and registers/addresses were shown with the wrong width.

This adds Elf.is_mips_n32 and uses it in two places:

  • MIPS64.supports_gdb_arch, so a running N32 process is detected as MIPS64
  • the ELF-header fallback in reset_architecture, so a not-yet-running N32 binary is also detected as MIPS64

O32 (ELF32 without EF_MIPS_ABI2) and N64 (ELF64) behave as before.

Fixes #1226

Testing

Not tested inside GDB: I had no GDB/qemu-mips available. I verified the detection logic by loading synthetic ELF headers through Elf with a stubbed gdb module:

  • o32 (e_flags=0x50001007, ELF32): is_mips_n32 False, MIPS64.supports_gdb_arch False
  • n32 (e_flags=0x80000027, ELF32): is_mips_n32 True, MIPS64.supports_gdb_arch True
  • n64 (ELF64): is_mips_n32 False, MIPS64.supports_gdb_arch True

No test was added because the test suite requires GDB. A check against a real N32 binary under GDB would be welcome.

Contribution

  • I have read and agree to the CONTRIBUTING document.

Licensing

  • The code of this PR contains LLM-generated code.

Checklist

  • My code follows the code style of this project.
  • My change includes a change to the documentation, if required.
  • If my change adds new code, adequate tests have been added.

N32 executables are ELF32 files flagged with EF_MIPS_ABI2 but use 64-bit registers. They were matched to the MIPS32 architecture, so registers and addresses were printed with the wrong width. Treat ELF32 MIPS files with EF_MIPS_ABI2 set as MIPS64, both when GDB reports the architecture and when falling back to the ELF header.

Fixes hugsy#1226
@github-actions

Copy link
Copy Markdown

🤖 Coverage update for f78c657 🟢

Old New
Commit 49c07dc f78c657
Score 70.2473% 70.2473% (0)

Comment thread gef.py
ELF_64_BITS = 0x02

ELF_MAGIC = 0x7F454C46
EF_MIPS_ABI2 = 0x00000020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what is EF? should this be ELF?

Copilot AI 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.

🟡 Changes recommended

Selecting MIPS64 also assigns eight-byte pointers to N32, breaking memory decoding for its four-byte pointers.

1 open issue
What changed in this PR

This PR addresses #1226 by recognizing MIPS N32 binaries as using 64-bit registers despite their ELF32 format.

Changes:

  • Adds an ELF property identifying N32 through EF_MIPS_ABI2.
  • Selects MIPS64 through both GDB architecture detection and the ELF-header fallback.
File Description
gef.py Adds N32 identification and updates both architecture-detection paths.

Review effort: Balanced

Comment thread gef.py
gdb_arch.startswith("mips") and gef.binary.e_class == Elf.Class.ELF_64_BITS
# N32 binaries are ELF32 files but use 64-bit registers
return gdb_arch.startswith("mips") and (
gef.binary.e_class == Elf.Class.ELF_64_BITS or gef.binary.is_mips_n32

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MIPS N32 ABI has 32-bit registers

3 participants