Why use RIP-relative addressing in NASM?

前端 未结 2 1401
梦毁少年i
梦毁少年i 2021-02-06 13:31

I have an assembly hello world program for Mac OS X that looks like this:

global _main


section .text

_main:
    mov rax, 0x2000004
    mov rdi, 1
    lea rsi,         


        
2条回答
  •  忘掉有多难
    2021-02-06 14:00

    What is so special about lea that mov can't do?

    LEA r, [rel symbol] can access RIP at run-time. mov r, imm can't. The immediate constant is encoded into the binary representation of the instruction, which means it won't work if the code+data are mapped to an address that isn't known at link time. (i.e. it's position-dependent code.)

    This is why RIP-relative addressing is so nice for PIC (position-independent code): instead of needing a level of indirection through the Global Offset Table to access even static data defined in the same object file, you can just use RIP-relative addresses.

    MacOS X requires PIC even in executables (not just shared libraries), so you need to avoid absolute addressing in all cases.

提交回复
热议问题