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

Problem with encoding jmps to absolute addresses #108

Closed
fotisl opened this issue May 25, 2016 · 7 comments
Closed

Problem with encoding jmps to absolute addresses #108

fotisl opened this issue May 25, 2016 · 7 comments

Comments

@fotisl
Copy link
Contributor

fotisl commented May 25, 2016

Jumps to absolute addresses are not always encoded correctly.
For example:

ks = Ks(KS_ARCH_ARM, KS_MODE_ARM)
ks.asm(b"b #0x0010", 0x0000)
ks.asm(b"b #0x1010", 0x1000)

Since the b instruction encodes a relative offset to the target address, the instruction encoding should be the same, however it isn't. You can verify the incorrect encoding using capstone.

The same happens with many architectures, such as KS_ARCH_ARM(other instructions such as bl), KS_ARCH_ARM64 (b, bl etc), KS_ARCH_MIPS(KS_MODE_MIPS32 with b, beq, etc), KS_ARCH_SPARC(b, etc).

The problem originates at the MCCodeEmitter (for example, for the SPARC architecture at llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp) which returns the immediate when it encounters it as an argument at a branch, and not a relative offset.

@aquynh
Copy link
Member

aquynh commented May 25, 2016 via email

@fotisl
Copy link
Contributor Author

fotisl commented May 25, 2016

I have made a simple program that demonstrates this bug for ARM, ARM64, SPARC, MIPS, PPC and SystemZ.
bug.txt

@aquynh
Copy link
Member

aquynh commented May 25, 2016

can you rewrite this into a regress test and send a PR to put it under directory suite/regress/? this is very simple, you can take https://github.com/keystone-engine/beta/blob/master/suite/regress/test1.py as example.

@fotisl
Copy link
Contributor Author

fotisl commented May 25, 2016

Pull request #109 created.

@aquynh
Copy link
Member

aquynh commented May 26, 2016

how far have you gone to attempt to fix this issue?

@aquynh aquynh added the bug label May 26, 2016
@fotisl
Copy link
Contributor Author

fotisl commented May 26, 2016

I have found the source of the problem, and given enough time I can fix it. When do you plan to release keystone?

aquynh added a commit that referenced this issue May 27, 2016
Fixed rel address issues for SystemZ (Issue #108)
@aquynh
Copy link
Member

aquynh commented May 27, 2016

fixed all now, thanks!

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

No branches or pull requests

2 participants