keystone-engine / keystone

Keystone assembler framework: Core (Arm, Arm64, Hexagon, Mips, PowerPC, Sparc, SystemZ & X86) + bindings
http://www.keystone-engine.org
GNU General Public License v2.0
2.3k stars 458 forks source link

Keystone automatically converting immediates for relative instructions as if they were absolute? #567

Open tmsrise opened 12 months ago

tmsrise commented 12 months ago

I was manually calculating offsets for jump targets and running into issues before realizing that Keystone was assembling my immediates as if they were absolute.

For example, in armv7, the branch (b) instruction uses relative addressing.

I would expect

b 0x1234

to jump to pc + 0x1234, but Keystone instead acts as if I want to jump to 0x1234 and effectively replaces it with an entirely different offset. (This of course only occurs when using a non-zero starting address). While very convenient, it's surprising and unintuitive.

Is there a way to explicitly control this behavior? Can I continue to integrate with keystone under the assumption that this isn't a bug from accidentally treating an immediate like a parsed label?

tmsrise commented 12 months ago

Probably due to #108, not sure if this "fix" is intended (though very much appreciated, if integrated correctly.). Either way it's non-obvious with no documentation other than an offhand vague mention in powerpoint. Also causes issues like #568