0

Does linker or compiler count RIP during compilation? If no, how does that instruction work? There will be hardcoded at linking time address, or it is special feature of processor architecture, that allows pointer arithmetic at runtime?

leaq    boot_heap(%rip), %rsi
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Belenot
  • 248
  • 1
  • 3
  • 10
  • 2
    x86-64 has hardware support for RIP-relative addressing, that's the whole point of having new syntax for it instead of just `lea boot_heap, %rsi` or `mov $boot_heap, %rsi` absolute addressing. That's why it can be used in position-independent code. – Peter Cordes May 22 '21 at 05:07
  • [NASM x86\_64 assembly in 32-bit mode: Why does this instruction produce RIP-Relative Addressing code?](https://stackoverflow.com/a/49122235) has some info about the change in machine code between 32 and 64-bit mode that introduces this new encoding for ModRM addressing modes. – Peter Cordes May 22 '21 at 05:14
  • A simple assembler can use a simple notion of a "location counter", which is basically just a fancy way to describe the size of the (code and/or data) section so far during the assembly process as some instructions/data have already been assembled and accounted for and some have yet to be processed. Using this or similar mechanism, the assembler learns (1) where each instruction is, (2) where each label is, and can do relative arithmetic, such as needed for certain addressing modes (i.e. subtract the address of label from current instruction's location to encode a pc-relative immediate). – Erik Eidt May 23 '21 at 01:21

0 Answers0