Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm confused why would IP-relative addressing be useful. Have you got some interesting examples?


It's the basis of efficient relocations in position independent code, which is now very common.

PIC can be emitted on older x86 machines without RIP-relative addressing, but the code is larger and slower. As an example, consider -m32 gcc output for the C program

    int x;

    int getx() {
        return x;
    }
With no PIC:

    getx:
	movl	x, %eax
	ret
With PIC:

    getx:
	call    __x86.get_pc_thunk.cx
	addl    $_GLOBAL_OFFSET_TABLE_, %ecx
	movl    x@GOT(%ecx), %eax
	movl    (%eax), %eax
	ret
And with -m64, which emits PIC and uses x86-64s RIP-relative addressing:

    getx:
 	movl	x(%rip), %eax
	ret
Hopefully that makes the motivation clear.


Thanks, I've seen it so many times it seems I developed (%rip) blindness :) Of course it's useful this way.


For any shared object loaded on an unknown address it makes it trivially easy to load data also in that library.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: