Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relocations(mips): fixed R_MIPS_HI16 and R_MIPS_LO16 #516

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

DanielBotnik
Copy link
Contributor

@DanielBotnik DanielBotnik commented Oct 1, 2024

Closes #482

This MR Solves multiple issues in R_MIPS_HI16 and R_MIPS_LO16 relocations.

  • Take into consideration the binary endianness, (off by 2 bytes)
  • Take into consideration R_MIPS_HI16 sign.
  • Take into consideration the pre placed compiler value, that need to be added to the target symbol.

I've also added a test using the binary in the mentioned issue above, which requires the following binaries PR:
angr/binaries#127

@DanielBotnik
Copy link
Contributor Author

I've seen that my test didnt run, so I changed it to unittest, now it suppose to work.

@DennyDai
Copy link
Contributor

DennyDai commented Oct 1, 2024

@DanielBotnik Thanks for trying to fix it!

While this is more correct than the existing version, it might still to be wrong for some corner cases.

The information from the documentation is equivalent to

AHL = (AHI << 16) + ALO
R_MIPS_HI16: (AHL + S) >> 16
R_MIPS_LO16: (AHL + S) & 0xFFFF

(AHL is 32 bits, ALO/AHI are 16 bits)

Which we can then simplify to

R_MIPS_HI16: (AHI + ((ALO + S) >> 16))
R_MIPS_LO16: (ALO + S) & 0xFFFF

While we no longer need AHI for LO16 after simplification, we still need ALO for correctly calculating HI16.

Your implementation is equivalent to the following

R_MIPS_HI16: (AHI + (S >> 16)) + (1 if S & 0x8000)

Consider the following case:

S = 0x10
AHI = 0x1
ALO = 0xFFFA

Your implementation: 0x1
Correct result: 0x2

The result will be different in this case.

So if the maintainer decide to merge this PR (because it's still a lot better than existing version and will yield correct result in most cases), I'd prefer to leave the referenced issue #482 open for the corner case.

@DanielBotnik
Copy link
Contributor Author

DanielBotnik commented Oct 1, 2024

@DennyDai can you take another look, I think it should work now :)

@DennyDai
Copy link
Contributor

DennyDai commented Oct 1, 2024

@DanielBotnik The overall logic looks good to me, thanks!

@twizmwazin twizmwazin merged commit d6530d3 into angr:master Oct 2, 2024
16 of 17 checks passed
@twizmwazin
Copy link
Member

Thanks again!

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.

R_MIPS_HI16 and R_MIPS_LO16 are wrong
3 participants