Skip to content

elf: fix R_386_PC8 relocation patching on x86-32 - #6841

Open
TharunBabu-05 wants to merge 1 commit into
rizinorg:devfrom
TharunBabu-05:elf-x86-reloc-patching
Open

TharunBabu-05 wants to merge 1 commit into
rizinorg:devfrom
TharunBabu-05:elf-x86-reloc-patching

Conversation

@TharunBabu-05

@TharunBabu-05 TharunBabu-05 commented Oct 5, 2026 •

Copy link
Copy Markdown

Your checklist for this pull request

  • I've read the guidelines for contributing to this repository.
  • I made sure to follow the project's coding style.
  • I've documented every RZ_API function and struct this PR changes. (No RZ_API changes — the edited patch_reloc_x86_32 is static.)
  • I've added tests that prove my changes are effective.
  • I've updated the Rizin book with the relevant information (not needed).
  • I've used AI tools to generate fully or partially these code changes and I'm sure the changes are not copyrighted by somebody else.

Detailed description

In patch_reloc_x86_32() (librz/bin/format/elf/elf_relocs_patching.c), the
R_386_8 / R_386_PC8 case subtracted the place address P only when
rel_type == R_386_PC16 — a copy-paste slip from the R_386_16 / R_386_PC16
case directly above it. So an R_386_PC8 relocation was patched to S + A
instead of the PC-relative S + A - P, writing the wrong byte. R_386_8
(which must stay S + A) was unaffected. The x86-64 patcher already handles
R_X86_64_PC8 correctly.

This is part of the x86 relocation gaps tracked in #5259.

AI disclosure: Claude Code (Anthropic Claude) was used to find the bug and
draft the fix and test; I reviewed and verified everything. The commit carries
the Co-authored-by agent: trailer per AGENTS.md.

Test plan

Added a case to test/db/formats/elf/elf-rel-x86 using a small object with an
R_386_PC8 relocation (and an R_386_8 control), added in
rizinorg/rizin-testbins#6841 as elf/x86_relocs_pc8.o. It reads the
patched bytes:

  • R_386_PC8 site @ 0x08000035 → 0x02 (= 0x08000037 - 0x08000035)
  • R_386_8 site @ 0x08000036 → 0x37 (unchanged)

Verified on CI: with the fix rz-test db/formats/elf/elf-rel-x86 → 2 OK;
with the fix reverted → 1 XX (expected 02, actual 37), confirming the
test catches the regression.

Closing issues

Copilot AI balanced review requested due to automatic review settings October 5, 2026 04:58

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added rz-test RzBin ELF AI/LLM Partially or fully AI generated. Critical thinking is advised! labels Oct 5, 2026
The x86-32 patcher subtracted the place address `P` only when
`rel_type == R_386_PC16`, a copy-paste slip from the `R_386_16`/`R_386_PC16`
case right above it. As a result `R_386_PC8` relocations were patched to
`S + A` instead of the PC-relative `S + A - P`, so the single byte written
was wrong. `R_386_8` (which must stay `S + A`) is unaffected.

The x86-64 patcher already handles `R_X86_64_PC8` correctly.

Add a test to `db/formats/elf/elf-rel-x86` using a small object with an
`R_386_PC8` reloc (and an `R_386_8` control): the patched byte is `0x02`
with the fix and `0x37` without it.

Co-authored-by agent: Claude Code/claude-opus-4-8
@TharunBabu-05
TharunBabu-05 force-pushed the elf-x86-reloc-patching branch from 02a4cd8 to 341e030 Compare October 5, 2026 09:57
@TharunBabu-05

TharunBabu-05 commented Oct 5, 2026 •

Copy link
Copy Markdown
Author

Your checklist for this pull request

  • I've read the guidelines for contributing to this repository.
  • I made sure to follow the project's coding style.
  • I've documented every RZ_API function and struct this PR changes. (No RZ_API changes — the edited patch_reloc_x86_32 is static.)
  • I've added tests that prove my changes are effective.
  • I've updated the Rizin book with the relevant information (not needed).
  • I've used AI tools to generate fully or partially these code changes and I'm sure the changes are not copyrighted by somebody else.

Detailed description

In patch_reloc_x86_32() (librz/bin/format/elf/elf_relocs_patching.c), the
R_386_8 / R_386_PC8 case subtracted the place address P only when
rel_type == R_386_PC16 — a copy-paste slip from the R_386_16 / R_386_PC16
case directly above it. So an R_386_PC8 relocation was patched to S + A
instead of the PC-relative S + A - P, writing the wrong byte. R_386_8
(which must stay S + A) was unaffected. The x86-64 patcher already handles
R_X86_64_PC8 correctly.

This is part of the x86 relocation gaps tracked in #5259.

AI disclosure: Claude Code (Anthropic Claude) was used to find the bug and
draft the fix and test; I reviewed and verified everything. The commit carries
the Co-authored-by agent: trailer per AGENTS.md.

Test plan

Added a case to test/db/formats/elf/elf-rel-x86 using a small object with an
R_386_PC8 relocation (and an R_386_8 control), added in
rizinorg/rizin-testbins#314 as elf/x86_relocs_pc8.o. It reads the
patched bytes:

  • R_386_PC8 site @ 0x08000035 → 0x02 (= 0x08000037 - 0x08000035)
  • R_386_8 site @ 0x08000036 → 0x37 (unchanged)

Verified on CI: with the fix rz-test db/formats/elf/elf-rel-x86 → 2 OK;
with the fix reverted → 1 XX (expected 02, actual 37), confirming the
test catches the regression.

Closing issues

@Rot127

Rot127 commented Oct 11, 2026

Copy link
Copy Markdown
Member

Please rewrite the PR description by hand and give a reference to the ABI specs where that is documented.

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

Labels

AI/LLM Partially or fully AI generated. Critical thinking is advised! ELF rz-test RzBin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants