计算JMP指令的地址(x86-64)

问题描述 投票:1回答:1

与此处相同的问题:Calculating JMP instruction's address

如何在64位机器上使用此代码?应该改变什么?

// TODO: 64-bit.
void Manager::InjectCode( PBYTE & p, int k, int n ) {

    * p++ = 0xB8; // mov eax, imm32
    p[0] = k;
    p += sizeof( int );

    * p++ = 0xA3; // mov [assemblyId], eax
    ( int * & ) p[0] = & assemblyId; 
    p += sizeof( int * ); 

    * p++ = 0xB8; // mov eax, imm32
    p[0] = n;
    p += sizeof( int );

    * p++ = 0xA3; // mov [functionId], eax
    ( int * & ) p[0] = & functionId; 
    p += sizeof( int * );         

    // jmp to CallbackFunction. 
    * p++ = 0xE9;
    ( UINT & ) p[0] = ( PBYTE ) ::CallbackFunction - 4 - p;
    p += sizeof( PBYTE );
}
assembly c++-cli x86-64 memory-address
1个回答
1
投票

最终的p += sizeof(PBYTE)需要更改为p += 4,因为偏移仍然是32位,即使指针是64位。你可能还需要一个演员到int。这仍然可以在32位模式下工作,因此您不必为此创建单独的版本。您必须确保跳转目标在32位范围内。否则,您可以使用间接跳转:

mov rax, CallbackFunction ; 48 b8 xx xx xx xx xx xx xx xx
jmp rax                   ; ff e0
© www.soinside.com 2019 - 2024. All rights reserved.