Section 4.45

Link register (LR)

Full name: Link register (LR), register R14 of the 32-bit Arm architecture.

Definition

The link register (LR) is the Arm register that holds a return address: where execution continues after a function finishes. A branch-with-link instruction jumps to a function and writes the address of the instruction after the branch into LR, and the function returns by jumping to that address. On Arm’s Cortex-M microcontroller §6.1 processors, entering an exception handler §6.15, code run in response to an interrupt §6.11 or a fault, instead puts a code named EXC_RETURN into LR. This code, not an address, tells the processor how to leave the handler.

The idea

A function can be called from many places, so it cannot know by itself where to go back to; each caller must hand it a return address. Processors of the x86 family push the return address onto the stack, the region of memory where functions keep saved values. Arm puts it in a register, LR. A register is faster than memory: a function that calls no other function can return without touching memory at all. The price is that LR holds only one return address. A function that calls another function must first save its own return address, usually on the stack, or the inner call overwrites it.

Exceptions create a second need. An exception is an event, such as an interrupt from a peripheral or a fault, that makes the processor stop the running code between two instructions and run a handler. On Cortex-M processors, Arm’s processors for microcontrollers, a handler can be an ordinary C function that ends with an ordinary return instruction. Returning from an exception takes more than a jump, though: the registers saved on entry must be reloaded, and the processor’s mode and stack must be switched back. So on exception entry the processor puts into LR a value that can never be a code address. When the handler’s ordinary return jumps to that value, the processor recognizes it and performs the whole exception return.

A cloakroom ticket is a fair picture of EXC_RETURN. The ticket is not the coat; it tells the attendant which rack holds the coat. Likewise, EXC_RETURN does not say where the interrupted code stopped. It says which stack holds the saved registers, and the saved registers include that address. The picture stops at uniqueness: every handler that interrupts the same kind of code receives the same value, so the code identifies a kind of return, not one particular exception.

How it works

Registers. In 32-bit Arm, LR is register R14. Two neighbors matter here: R13 is the stack pointer, the register that holds the address of the top of the stack, and R15 is the program counter §2.9 (PC), which holds the address of the instruction being run. Cortex-M processors run only the Thumb instruction set §4.40, Arm’s compact encoding with 2-byte and 4-byte instructions.

An ordinary call and return.

  1. The caller runs BL (branch with link), whose target is encoded in the instruction itself, or BLX (branch with link and exchange), whose target is in a register. Either instruction writes the address of the next instruction into LR and jumps to the target.

  2. Bit 0 of that LR value is set to 1. Instructions always start at even addresses, so bit 0 is free, and Arm uses it to mark the Thumb instruction set: when a jump takes its target from a register or from memory, bit 0 equal to 1 means continue in Thumb. On Cortex-M, which has no other instruction set, such a jump with bit 0 equal to 0 causes a fault.

  3. A function that calls no other function can leave its return address in LR and return with BX LR (branch and exchange to the address in LR).

  4. A function that calls other functions first saves LR on the stack, for example with PUSH {R4, LR}, and returns by loading the saved value straight into the PC, for example with POP {R4, PC}.

The AAPCS (Procedure Call Standard for the Arm Architecture), Arm’s calling convention, does not list LR among the registers that a called function must preserve. After a function has saved its return address, the compiler may therefore use LR to hold other data.

Exception entry. On Cortex-M processors of the Armv7-M architecture, such as the Cortex-M3 and Cortex-M4 §6.3, exception entry is done by hardware and has these effects.

  1. The processor saves eight 4-byte values on the stack that the interrupted code was using: R0, R1, R2, R3, R12, LR, the return address and the xPSR (the combined program status register, which holds the condition flags §4.42 and the execution state). The return address is the address of the instruction at which the interrupted code will continue. These 32 bytes form the exception stack frame.

  2. With 8-byte stack alignment §4.22, which the Cortex-M4 enables by default, the frame must start at an address that is a multiple of 8. If the stack pointer is 4 bytes past such an address, the processor leaves the 4 bytes just below the stack pointer unused and records this in bit 9 of the saved xPSR. The basic frame therefore takes up to 32+4=3632 + 4 = 36 bytes of the interrupted code’s stack.

  3. If the interrupted code had been using the floating-point unit, the frame also holds 16 floating-point registers, the floating-point status register and one reserved word: 8+16+1+1=268 + 16 + 1 + 1 = 26 words, which is 26×4=10426 \times 4 = 104 bytes, or 108 bytes with the unused word.

  4. The processor writes EXC_RETURN into LR, enters Handler mode, the mode in which exception handlers run, and jumps to the handler address that it reads from the vector table §6.14, a table of handler addresses in memory.

Two stack pointers. A Cortex-M processor has two stack pointer registers: the MSP (main stack pointer) and the PSP (process stack pointer). R13 names whichever one is in use. Handler mode always uses the MSP. Thread mode, the mode for ordinary code, uses either one, as a bit in the CONTROL register selects. A real-time operating system typically gives each thread its own stack through the PSP and lets all handlers share the main stack. The frame always goes on the stack that was in use when the exception arrived. A thread that runs on the PSP must therefore keep room for one frame below its deepest point; exceptions that interrupt a handler put their frames on the main stack.

The EXC_RETURN code. On Armv7-M, bits 31 to 5 of EXC_RETURN are all 1, and the low bits describe the return:

With a basic frame this gives three values: 0xFFFFFFF1 (return to Handler mode, main stack), 0xFFFFFFF9 (return to Thread mode, main stack) and 0xFFFFFFFD (return to Thread mode, process stack). With floating-point registers in the frame they become 0xFFFFFFE1, 0xFFFFFFE9 and 0xFFFFFFED. Armv8-M processors with the TrustZone security extension use further bits to record the security state.

Exception return. The processor checks every value that BX, POP, LDR or LDM loads into the PC. In Handler mode, a value whose top four bits are all 1, that is any value from 0xF0000000 upward, starts an exception return. The processor reads the frame from the stack that EXC_RETURN names, reloads the saved registers, adds the unused 4 bytes back to the stack pointer if bit 9 of the saved xPSR says they were left out, enters the mode that EXC_RETURN names, and continues at the saved return address. In Thread mode, the same load is an ordinary jump to that address. The Armv7-M memory map makes every address from 0xE0000000 upward execute-never, so no code can run there. A real return address is therefore never mistaken for EXC_RETURN, and the ordinary jump in Thread mode faults.

Worked example

Calls. Function A contains a 4-byte BL B at address 0x100. The next instruction is at 0x100 plus 4, which is 0x104, so BL sets LR to 0x104 plus 1, which is 0x105; the added 1 is the Thumb bit. Function B begins with PUSH {R4, LR}. The stack pointer holds 0x20000800; the push lowers it by 2×4=82 \times 4 = 8 bytes to 0x200007F8, stores R4 at 0x200007F8 and stores 0x105 at 0x200007FC. At address 0x200, B runs a 4-byte BL C, which sets LR to 0x205 and so overwrites 0x105 in LR. Function C calls nothing and ends with BX LR: the processor clears bit 0, jumps to 0x204 and stays in Thumb. Function B ends with POP {R4, PC}, which loads 0x105 from 0x200007FC into the PC, and execution continues at 0x104 in A. Had B skipped the push and ended with BX LR, it would have jumped to 0x204, back inside B itself.

An exception. Code in Thread mode runs on the PSP, which holds 0x20001004, and its next instruction is at 0x08000124 when an interrupt arrives. The code has not used the floating-point unit. The address 0x20001004 is 4 past 0x20001000, a multiple of 8, so the processor leaves the word at 0x20001000 unused and stores the 32-byte frame just below it, from 0x20000FE0 to 0x20000FFF: R0 at 0x20000FE0, then R1, R2, R3, R12 and the interrupted code’s LR, then the return address 0x08000124 at 0x20000FF8 and the xPSR, with bit 9 set, at 0x20000FFC. The PSP becomes 0x20000FE0, which is 4+32=364 + 32 = 36 bytes below its old value. LR becomes 0xFFFFFFFD: Thread mode, process stack, basic frame.

The handler runs on the MSP. If it calls a function, it first pushes LR like any other function, and its final POP loads 0xFFFFFFFD into the PC; a handler that calls nothing ends with BX LR, which loads the same value. The processor is in Handler mode and the value starts with hexadecimal F, so this is an exception return. The processor reads the eight saved registers from 0x20000FE0, sees bit 9 set and adds the unused 4 bytes back, so the PSP returns to 0x20000FE0 plus 36, which is 0x20001004. Execution continues at 0x08000124 in Thread mode.

Stated precisely

Let aa be the address of a call instruction and nn its length in bytes: 4 for BL, 2 for BLX with a register. In Thumb state the call sets LR to a+n+1a + n + 1; the address a+na + n is even, and the added 1 is the Thumb bit. LR keeps that value until the next event that writes LR: another call, an exception entry, an exception return, which reloads the interrupted code’s LR from the stack frame, or an instruction that names LR as its destination, such as a POP that restores a saved value.

On an M-profile (Cortex-M) processor, the value in LR means:

On Armv7-M, when BX, POP, LDR or LDM loads a value vv into the PC, the processor performs an exception return if and only if it is in Handler mode and vv is at least 0xF0000000. Otherwise it jumps to vv with bit 0 cleared, and the jump faults if bit 0 of vv was 0.

Limits and common mistakes

LR holds only the most recent return address. After a call, LR no longer holds the calling function’s own return address, so any code or analysis that needs that address must find the copy the function saved.

In the middle of a function, LR need not be the return address at all, because the compiler may use LR for other data once the return address is saved. A debugger that shows the caller of a stopped function finds the saved copy with the stack-unwinding information that the compiler emits, not by reading LR.

Inside an exception handler, LR says nothing about where the interrupted code was. That code’s resume address is in the return-address slot of the stack frame, and the code’s own LR value is in the frame’s LR slot. A fault handler that reports LR as the location of a crash reports 0xFFFFFFFD or a similar code, not a code address.

Bit 0 is a state flag, not part of the address. A call at 0x100 returns to 0x104, and no instruction ever begins at 0x105.

The saved copy of LR is ordinary writable memory. A stack buffer overflow §4.4 that overwrites it makes the function return to an address the attacker chose. Return-oriented programming builds whole attacks this way, by chaining fragments of existing code that each end in a return. A forged stack frame or EXC_RETURN value redirects an exception return in the same way. The pointer authentication §1.6 instructions of Armv8.1-M sign the return address before it is saved and check the signature before it is used.

The exception-return code is specific to M-profile Arm. Processors of the x86 family have no link register: their call instruction pushes the return address onto the stack. The 64-bit Arm architecture uses register X30 as its link register and records the address at which an exception was taken in a separate exception link register. In 32-bit Arm processors of the A-profile and the R-profile, each exception mode has its own copy of LR, and exception entry writes the interrupted address, plus a fixed offset, into that copy.

Questions answered

When is LR an ordinary return address and when an exception-return token?

LR (the link register) is an ordinary return address when the last thing that wrote it was a call instruction, BL or BLX (branch with link), or a restore of a value that such a call produced. It is an exception-return token, EXC_RETURN, when the last thing that wrote it was entry into an exception handler on an M-profile (Cortex-M) Arm processor, or a restore of that value. A call writes the address of the instruction after the call, with bit 0 set to 1 to mark the Thumb instruction set: a call at 0x100 sets LR to 0x105, for a return to 0x104. Exception entry saves the interrupted code’s registers and resume address on the stack, then writes a fixed code into LR, such as 0xFFFFFFFD on Armv7-M. The code says which stack holds the saved registers, which mode to return to, and whether floating-point registers were saved.

The value tells the two cases apart, since no code can execute at or above 0xE0000000 and every EXC_RETURN value is at least 0xF0000000. The effect depends on the processor’s mode: only in Handler mode, the mode in which exception handlers run, does loading such a value into the program counter (PC) perform an exception return; in Thread mode it is a jump that faults. Once a function has saved its return address, it may use LR for other data, and then LR is neither.

Related entries: Control-flow graph (CFG) (§3.13), Live-in register (§4.44), Arm Thumb and Thumb-2 (§4.40), Exception and fault handler (§6.15), Vector table (§6.14), Buffer overflow (§4.4), Native application binary interface (ABI) (§12.47). Further reading: [5, 6, 82].