You can't just blat the address into memory, you have to blat its
displacement.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9210 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/X86/X86TargetMachine.cpp b/lib/Target/X86/X86TargetMachine.cpp
index 14af520..0a42f19 100644
--- a/lib/Target/X86/X86TargetMachine.cpp
+++ b/lib/Target/X86/X86TargetMachine.cpp
@@ -143,9 +143,12 @@
}
bool X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
+ // FIXME: This code could perhaps live in a more appropriate place.
char *OldByte = (char *) Old;
- *OldByte++ = 0xE9; // JMP
- unsigned *OldWord = (unsigned *) OldByte;
- *OldWord = (unsigned) New;
- return false;
+ *OldByte++ = 0xE9; // Emit JMP opcode.
+ int32_t *OldWord = (int32_t *) OldByte;
+ int32_t NewAddr = (int32_t) New;
+ int32_t OldAddr = (int32_t) OldWord;
+ *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
+ return false; // success!
}