Developer knowledge network · moderated exchange

UnreliableCode Topluluğu

Geliştirici Araştırması, Tersine Mühendislik ve Kodlama Topluluğu

Knowledge indexCanlı
4Categories
919Threads
2.8KGönderiler
Guide

Calculating 64-bit RIP-Relative Displacements in x64 Assembly Detours

x64_assembler
Assembly Guru
MEMBER
Temsilci: 99
Katılım Tarihi: Sep 2022
Gönderiler: 14
Teşekkürler: 16
1 ay önce · Jun 27, 2026 12:17 AM
#1

When hooking 64-bit functions with a 5-byte JMP (E9 xx xx xx xx):

Formula for the 32-bit signed relative displacement:

CPP
int32_t displacement = (int32_t)((uintptr_t)pDestination - ((uintptr_t)pSource + 5));

If the trampoline distance exceeds 2GB (|pDestination - pSource| > 0x7FFFFFFF):
Use an indirect 64-bit jump without registers:

ASM
FF 25 00 00 00 00 [8-byte absolute address]

This reads the 64-bit address from RIP + 0 and jumps directly without clobbering RAX/RCX/RDX!

hook_doctor
Hooking Specialist
MEMBER
Temsilci: 181
Katılım Tarihi: Jul 2019
Gönderiler: 21
Teşekkürler: 50
1 ay önce · Jun 27, 2026 1:57 AM
#2

FF 25 00 00 00 00 (jmp [rip+0]) is the cleanest 14-byte absolute jump on x64 because it preserves all GPRs.

ptr_arithmetic
C++ Wizard
MEMBER
Temsilci: 162
Katılım Tarihi: May 2018
Gönderiler: 73
Teşekkürler: 42
1 ay önce · Jun 27, 2026 3:58 PM
#3

Always remember to subtract 5 (the length of the jmp instruction) when computing relative E9 displacements.