Section 12.50

Relocation: turning a symbolic reference into exact encoded bits

Definition

A relocation is a record that a compiler or assembler leaves in an object file to mark where a still-unknown address must be filled in later, and to say how to compute and encode it. The linker §12.4, or for a shared library the dynamic linker §12.49, applies it once the referenced symbol has an address: it evaluates the formula of the relocation type, checks that the result fits the field, and writes the bits. Symbol resolution decides which definition a name means; relocation turns that definition’s address into exact bits.

The idea

A compiler translates one source file at a time. When main.c calls a function h defined in another file, the compiler cannot know where h will end up: that depends on the other files and on the linker’s layout. So it emits the call with a placeholder in the address field and adds a note: at this byte, put the address of h, encoded this way. Pointers to global variables and jumps into other sections get the same treatment.

A manuscript with cross references such as see page __ is a fair picture: the blanks are filled in after typesetting, when every page number is known. The picture stops at the encoding. A page number is printed the same way everywhere, but an instruction may store its value as a distance rather than an address, in a narrow field, scaled, or split across separate bit fields.

How it works

An ELF §12.6 (Executable and Linking Format) relocation record has three parts.

32-bit Arm and 32-bit x86 use Rel; 64-bit Arm and x86-64 use Rela.

The formulas use three letters, as the ABI documents do: SS is the address of the symbol, AA the addend and PP the address of the place. Applying one relocation takes six steps.

  1. Check the record: the place and the whole field lie inside the section, the symbol index exists, and the tool supports the type.

  2. Resolve the symbol to obtain SS. This is symbol resolution: the linker picks the definition the name refers to, using the symbol tables of its inputs.

  3. Obtain AA, from r_addend, or for Rel by decoding the field at the place.

  4. Compute the value the type defines. Two common formulas are S+AS + A, an absolute address, and S+A−PS + A - P, a PC-relative value: a distance measured from the program counter §2.9 (PC), the register that holds the address of the current instruction.

  5. Check the range. A signed field of nn bits holds the values −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1. If the value does not fit, stop with an error. Some types also require alignment §4.22, such as a value that is a multiple of 2.

  6. Encode. Write the value into exactly the bits the type names, in the target’s byte order §4.21, and leave every other bit as it was.

The static linker applies most relocations when it builds the executable. Code whose load address is chosen at run time, a shared library or a position-independent executable, keeps dynamic relocations, which the dynamic linker applies at load time; a common one says to add the load address to the value stored at the place.

Worked example

A call on x86-64 is the byte e8 followed by a signed 32-bit displacement, stored little-endian, which the processor adds to the address of the next instruction. The linker has placed a call at address 0x1000, so the displacement field occupies 0x1001 to 0x1004 and the next instruction starts at 0x1005. The function h is at 0x1234. The assembler recorded relocation type R_X86_64_PC32, whose formula is S+A−PS + A - P, at the place P=𝟶𝚡𝟷𝟶𝟶𝟷P = \mathtt{0x1001} with addend A=−4A = -4. (Recent assemblers record a call as R_X86_64_PLT32, which gives the same result when h is in the same executable.)

  1. Compute. S+A−P=𝟶𝚡𝟷𝟸𝟹𝟺−4−𝟶𝚡𝟷𝟶𝟶𝟷=𝟶𝚡𝟸𝟸𝙵S + A - P = \mathtt{0x1234} - 4 - \mathtt{0x1001} = \mathtt{0x22F}, which is 559 in decimal. The addend −4-4 turns a distance measured from the field into a distance measured from the next instruction, since P+4=𝟶𝚡𝟷𝟶𝟶𝟻P + 4 = \mathtt{0x1005}.

  2. Check the range. A signed 32-bit field holds −231-2^{31} to 231−12^{31} - 1, and 559 fits.

  3. Encode little-endian as 2f 02 00 00. The instruction becomes e8 2f 02 00 00.

  4. Confirm. At run time the processor computes 𝟶𝚡𝟷𝟶𝟶𝟻+𝟶𝚡𝟸𝟸𝙵=𝟶𝚡𝟷𝟸𝟹𝟺\mathtt{0x1005} + \mathtt{0x22F} = \mathtt{0x1234}, the address of h.

Two wrong versions show what the type contributes. Storing the address S=𝟶𝚡𝟷𝟸𝟹𝟺S = \mathtt{0x1234} in the field would send the call to 𝟶𝚡𝟷𝟶𝟶𝟻+𝟶𝚡𝟷𝟸𝟹𝟺=𝟶𝚡𝟸𝟸𝟹𝟿\mathtt{0x1005} + \mathtt{0x1234} = \mathtt{0x2239}. Leaving out the addend would store S−P=𝟶𝚡𝟸𝟹𝟹S - P = \mathtt{0x233} and send the call to 𝟶𝚡𝟷𝟶𝟶𝟻+𝟶𝚡𝟸𝟹𝟹=𝟶𝚡𝟷𝟸𝟹𝟾\mathtt{0x1005} + \mathtt{0x233} = \mathtt{0x1238}, 4 bytes past the start of h.

Now take a field of 8 bits, as in the x86 short jump eb, whose displacement is also counted from the next instruction. It holds −128-128 to 127127. A target 200 bytes past the next instruction does not fit. Keeping the low 8 bits would write 0xc8, and read as a signed 8-bit number that is 200−256=−56200 - 256 = -56: the jump would land 56 bytes before the next instruction instead of 200 bytes after it. A correct linker stops instead, and ld reports relocation truncated to fit.

Stated precisely

A relocation type tt defines three things: a function ft(S,A,P)f_t(S, A, P); a field, meaning its width nn in bits, which bits of the instruction or data word hold it, any scaling such as dropping low bits that must be zero, and its byte order; and a range check. Two common functions are absolute:ft(S,A,P)=S+A,PC-relative:ft(S,A,P)=S+A−P.\begin{aligned} \text{absolute:} \quad & f_t(S, A, P) = S + A, \\ \text{PC-relative:} \quad & f_t(S, A, P) = S + A - P. \end{aligned} For a signed field of nn bits the range is −2n−1≤ft(S,A,P)≤2n−1−1-2^{n-1} \le f_t(S, A, P) \le 2^{n-1} - 1; for an unsigned field it is 0≤ft(S,A,P)≤2n−10 \le f_t(S, A, P) \le 2^{n} - 1.

Applying a relocation is correct when, afterwards, the field holds the encoding of ft(S,A,P)f_t(S, A, P), that value lies in the field’s range, every other bit is unchanged, and SS is the address of the definition that symbol resolution selected. The statement assumes that the processor’s ABI document defines tt, that SS and PP are addresses in one address space, measured in bytes, and, for Rel, that AA is read from the place before anything is written there.

Relocations in eBPF object files

A compiler that produces eBPF §2.1 (extended Berkeley Packet Filter) bytecode §2.4, such as Clang §12.17, also writes ELF object files with relocations, and a loader must apply them before the program can run correctly.

A tool that copies only the raw bytes of the code section and applies no relocations therefore produces bytecode in which every such lddw still loads its placeholder, zero or an offset, instead of a reference to the data. A verifier rejects the first memory access through that value; a virtual machine §2.3 without one reads the wrong memory. Calls to functions outside the copied section break the same way.

Limits and common mistakes

Relocation encodes whatever symbol resolution chose. If two libraries define h and the unintended one is selected, the relocation correctly encodes a call to the wrong function.

A correct address does not make a correct call. The callee must follow the same ABI as the caller, which no relocation checks.

Real fields are rarely a plain 32-bit word. The Thumb BL instruction (branch with link, the call instruction) of 32-bit Arm stores a signed count of 2-byte units, 24 bits wide, spread over several bit fields of its two 16-bit halves, so it reaches about 16 MiB in either direction. For a target farther away, linkers insert a veneer, a small piece of code within range that jumps on to the target.

Applying a Rel relocation twice is wrong: the second pass reads the result of the first as its addend.

Relocation changes the bytes of code. If code was checked or signed before relocation, the bytes that run differ from the bytes that were checked, a time-of-check to time-of-use §9.26 gap, unless the check also covers the relocation records and the symbol values they use.

Questions answered

How do symbol resolution and relocation differ?

Symbol resolution decides which definition a name refers to, and so yields its address; relocation writes that address, in the exact form an instruction or data word needs, into the place that refers to the name. Resolution works on names: among all the definitions of h in the linked files and libraries, it selects one. Relocation works on bits: it computes the formula of its type, for example S+A−PS + A - P (symbol address plus addend minus the address of the place), checks the range and writes the field. Either can fail alone: resolution can pick the wrong definition, which relocation then encodes perfectly, or pick the right one at a distance that the field cannot hold.

Why is the addend not always just the existing integer in a machine word?

Because where the addend is kept, and in what format, depends on the kind of record and on the relocation type. In ELF (Executable and Linking Format) records of the kind Rela, the addend is an explicit field of the record. In records of the kind Rel, it is stored at the place, but in the field’s own format: possibly fewer bits than a word, scaled, split across separate bit fields of an instruction as in the Arm call instruction BL, and in the target’s byte order. Reading it as a plain native-width integer gives a wrong value, and reading it after the place has been written gives the previous result, not the addend.

Why must a linker diagnose overflow instead of keeping low bits?

A linker must diagnose overflow because the low bits of a value that does not fit are a different value, and the instruction will obey them. A signed 8-bit field holds −128-128 to 127127. A displacement of 200 does not fit; its low 8 bits, 0xc8, read as a signed number, are 200−256=−56200 - 256 = -56, so the jump would land 56 bytes before the next instruction instead of 200 bytes after it. The program would link silently and then run code at the wrong address. An error, such as relocation truncated to fit from ld, lets the developer move the code or use a longer instruction form.

Can a valid relocation make an unsafe program safe?

No. A valid relocation only makes an address reference point where the relocation type and the chosen symbol say it should. It does not check that the right definition was chosen, that caller and callee follow the same ABI (application binary interface), that the code is harmless, or that the program may do what it does. Relocation also changes the bytes of the code, so a safety check or signature must cover the relocated bytes, or the relocation records and the values they use, to remain valid.

Related entries: Linker (§12.4), ELF (§12.6), Symbol table (§12.8), Native application binary interface (ABI) (§12.47), Signed versus unsigned comparison (§2.12), Structural bytecode admission (§2.26), Time-of-check to time-of-use (TOCTOU) (§9.26), Wide immediate instruction (§2.10), BPF map (§2.18), Program loading: from an ELF file to a running native process (§12.49). Further reading: [197].