Term: PC-relative address — pc-relative address
1. Introduction to the Current Program Counter (PC)
The current Program Counter (PC) cannot be treated as a general-purpose register, and therefore cannot be used as a source or destination for arithmetic instructions, nor as a base, index, or transfer register for load/store instructions.
-
Methods to read the PC are: Instructions with PC-relative addresses (ADR, ADRP, literal load, and direct branches), as well as branch-and-link instructions (BL and BLR).
-
Methods to modify the PC are using explicit control flow instructions: conditional branches, unconditional branches, exception generation, and exception return instructions.
When the PC is read by an instruction to calculate a PC-relative address, its value is the address of the instruction, which differs from A32 and T32, as there is no implicit offset of 4 or 8 bytes.
Original text:
2. ADRP and ADR
ADRP is an address loading instruction that reads the address based on the PC’s relative offset into the target register. The ADR instruction uses the format: adr register exper.
At compile time, the offset #offset from the current PC to exper is first calculated, and then this instruction is replaced with an ADD or SUB instruction; for example, equivalent to: ADD register, PC, #offset to exper, where register is the address of exper.
ADRP instruction uses the format: adrp register exper.
At compile time, the offset #offset from the current PC to exper is first calculated, and the lower 12 bits of the PC are cleared, then the offset is added to give the register the address, which is the base address of a 4KB aligned memory area containing the label.
3. Usage Examples
(1) The following demonstrates reading the PC pointer example, using ADR and ADRP instructions, which can be equivalent to armv7 and earlier <span><span><span>mov x0, pc</span></span></span>
or <span><span><span>mov x0, r15</span></span></span>
instructions.
(2) For modifying the value of the PC (or jumping), in armv8 aarch64, there is no <span><span><span>mov pc, x0</span></span></span>
or similar. To jump, one can use instructions like b, bl, br, blr, eret, etc.
Recommended | “From Beginner to Expert in Armv8/Armv9 Architecture” – Three Sessions |
“Trustzone/TEE/Security from Beginner to Expert” – Standard Edition | |
Arm Selected – Platinum VIP Courses💋 All Courses | |
🌍Consult via WeChat: sami01_2023 |