Force RIP-relative access to fixed offset in NASM

For a binary instrumentation project I wrote a chunk of assembly code in NASM, which gets mapped into a binary’s address space at runtime.

The chunk gets loaded at address instrument_addr, and needs to access data at address instrument_addr+data_offset, where data_offset is some fixed 31-bit number. Due to ASLR I don’t know the value of instrument_addr at compile time.

Since I don’t know the absolute address of my instrumentation code, but the relative offset of my data, I would like to use RIP-relative addressing:

; Example for data_offset = 0x1000
0:  48 8b 05 f9 0f 00 00    mov    rax, QWORD PTR [rip+0xff9]    # 1000

However, the most straightforward approach

; This is offset 0 of my assembly file
    mov rax, qword [rel 0x1000]

only leads to:

$ nasm -f elf64 -o instrument.o instrument.asm
instrument.asm:3: warning: absolute address can not be RIP-relative [-w+other]

Using [absolute 0x1000] with a dummy label produces the same warning.

How can I force NASM to generate RIP-relative accesses to a certain fixed offset?

>Solution :

The syntax you’re looking for is [rel $+0x1000] for an offset from the current location, or [rel instrument+0x1000] for an offset from the label. In the example in your question, those happen to be the same since the mov is the first thing after the label, but if there were anything in between, they’d be different.

Leave a Reply