Search code examples
assemblyx8664-bitnasm

How can I assemble a relative call to an absolute ptr addr (relative to current rip) using NASM?


I'd like NASM to generate code similar to:

call qword ptr [rip + 0x540]

but can't seem to figure out how to do this.

My current code is:

BITS 64
DEFAULT REL
ORG 0x10000000
abs_addr_of_func_ptr EQU 0x10000600

...

call qword [abs_addr_of_func_ptr]

NASM should have enough info to figure out the current rip at the call, determine the offset to abs_addr_of_func_ptr, and assemble a relative call to the ptr. But it doesn't seem to want to do this (or at least I haven't found a way to tell it to). How can I do this?


Solution

  • A workaround:

    BITS 64
    DEFAULT REL
    base_addr EQU 0x10000000
    ORG base_addr
    abs_addr_of_func_ptr EQU 0x10000600 - base_addr + $$
    
    call [abs_addr_of_func_ptr]
    

    Of course you can remove the base_addr by hardcoding the 0x10000000 if you wish:

    BITS 64
    DEFAULT REL
    ORG 0x10000000
    abs_addr_of_func_ptr EQU 0x00000600 + $$
    
    call [abs_addr_of_func_ptr]