What is PC-relative addressing?
Specifying a branch target or data address as a distance from the current instruction's address (PC). The distance is unchanged wherever the program is loaded, so it underpins position-independent code.
Field: ISA & Assembly
Japanese name: PC相対アドレッシング
How the target is computed
The instruction holds a signed offset, and the hardware adds it, after sign extension, to a reference address derived from the program counter (PC). In RISC-V the reference is the address of the branch itself. Conditional branches encode a 13-bit even offset, reaching about ±4 KiB, and jal encodes a 21-bit offset, about ±1 MiB. For anything farther, auipc adds a 20-bit upper immediate shifted left by 12 to the PC, and a following jalr, addi or load supplies the low 12 bits, together reaching about ±2 GiB. Other ISAs use different reference points, such as the address of the next instruction on x86.
Why it enables position-independent code
Position-independent code (PIC) must run correctly at whatever address it is loaded. With absolute addresses, the loader would have to patch every instruction that names an address, which takes time, makes each process's copy of the code different and prevents sharing the same physical pages among processes. PC-relative offsets between parts of the same image do not change when the whole image moves, so no patching is needed. Shared libraries and executables loaded at randomized addresses for security (address space layout randomization) depend on this.
Reaching data, not just code
PC-relative addressing is equally important for data. x86-64 introduced RIP-relative addressing so that loads and stores can name global variables relative to the instruction pointer, something 32-bit x86 lacked, forcing PIC on that architecture to obtain the PC with a call trick and dedicate a register to it. For symbols whose final address is only known at run time, such as variables in another shared library, the code uses a PC-relative load from the global offset table (GOT), a table of addresses that the dynamic linker fills in once at load time.
PC-relative vs. other addressing modes
Absolute addressing names a fixed address and must be relocated if the code moves. Register-indirect addressing jumps or loads through an address held in a register, which is how function pointers, virtual calls and returns work, since their targets are not known when the code is assembled. PC-relative addressing covers the large middle ground of targets that are fixed relative to the code: loop branches, calls within a program and accesses to its own constants and globals. Its limitation is the offset field's width, which is why long-range sequences such as auipc plus jalr exist.
Try it: questions about PC-relative addressing
- AThe branch can always reach any target with one instruction, however far
- BBranch prediction becomes unnecessary and the pipeline never stalls
- CThe program runs without modifying its instructions no matter where in memory it is loaded
Show answer and explanation
Answer: C. The program runs without modifying its instructions no matter where in memory it is loaded
A PC-relative branch states its target as "so many bytes from here", so if the whole program is shifted to another address the distance is unchanged and it still works. This is the foundation of position-independent code (PIC) used in shared libraries. The distance field has limited bits, though, so far jumps combine auipc and jalr. The need for branch prediction is unrelated to how the target is encoded.
FAQ
What is the range of a RISC-V branch instruction?
Conditional branches such as beq have a 13-bit signed offset in multiples of 2 bytes, about ±4 KiB from the branch. jal reaches about ±1 MiB, and auipc combined with jalr reaches about ±2 GiB.
Why does x86-64 use RIP-relative addressing?
It lets code access global data relative to the current instruction, so position-independent code needs no load-time patching and no dedicated base register. 32-bit x86 had no such mode for data.
Is PC-relative addressing required for shared libraries?
In practice yes. Shared libraries are loaded at different addresses in different processes, and PC-relative branches and data references, plus indirection through tables like the GOT, let the same code pages work everywhere.
Books on ISA & Assembly Ad
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary