The compiler doesn’t know where a function or variable will end up, so it leaves a placeholder and writes a relocation: a note telling the linker what to patch. Each section has its own list, found through the section header.
The relocation record
IMAGE_RELOCATION, 10 bytes:
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | VirtualAddress | The address of the item to which the relocation is applied: the offset from the beginning of the section plus the value of the section header’s VirtualAddress field (so if the first byte of the section has address 0x10, the third byte has 0x12). In the first record of a section with IMAGE_SCN_LNK_NRELOC_OVFL whose NumberOfRelocations is 0xFFFF, this field instead holds the real number of relocations. |
| 4 | 4 | SymbolTableIndex | A zero-based index into the symbol table (auxiliary records count as entries). That symbol gives the address to use. If it has the storage class IMAGE_SYM_CLASS_SECTION, its address is the beginning of the section; the section is usually in the same file, except when the object file is a member of an archive, where it can be in any other member with the same archive-member name (this is how import tables, .idata, are linked). In a PAIR relocation this field is not an index but a displacement. |
| 8 | 2 | Type | How to patch the field, which depends on the machine; see Types. |
Image files do not contain COFF relocations; an image carries base relocations in its
.reloc section instead (unless it has IMAGE_FILE_RELOCS_STRIPPED).
COFF relocations (the record above) exist only in object files. The record has no addend
field; the specification names no addend except for the Itanium ADDEND type below. In
practice the bytes at the patched address hold the addend, as the REL32_1 to REL32_5 types
suggest, but that is how the linkers behave and not something PE Format spells out.
Types
The Type field says what kind of relocation to perform, and the valid types depend on
the machine in the file header. This table is an overview of the four common machines;
the meaning of each value, and the types of the other machines, follow it. The prefix
of the constant is IMAGE_REL_AMD64_, IMAGE_REL_I386_,
IMAGE_REL_ARM_ or IMAGE_REL_ARM64_. A value not listed for a machine is not defined
by PE Format (0x13 for ARM is marked “Unused”). Abbreviations: VA is a virtual address
(with the image base), RVA one relative to the image base.
| Value | x64 | x86 | ARM | ARM64 |
|---|---|---|---|---|
0x0 | ABSOLUTE | ABSOLUTE | ABSOLUTE | ABSOLUTE |
0x1 | ADDR64 | DIR16 | ADDR32 | ADDR32 |
0x2 | ADDR32 | REL16 | ADDR32NB | ADDR32NB |
0x3 | ADDR32NB | BRANCH24 | BRANCH26 | |
0x4 | REL32 | BRANCH11 | PAGEBASE_REL21 | |
0x5 | REL32_1 | REL21 | ||
0x6 | REL32_2 | DIR32 | PAGEOFFSET_12A | |
0x7 | REL32_3 | DIR32NB | PAGEOFFSET_12L | |
0x8 | REL32_4 | SECREL | ||
0x9 | REL32_5 | SEG12 | SECREL_LOW12A | |
0xA | SECTION | SECTION | REL32 | SECREL_HIGH12A |
0xB | SECREL | SECREL | SECREL_LOW12L | |
0xC | SECREL7 | TOKEN | TOKEN | |
0xD | TOKEN | SECREL7 | SECTION | |
0xE | SREL32 | SECTION | ADDR64 | |
0xF | PAIR | SECREL | BRANCH19 | |
0x10 | SSPAN32 | MOV32 | BRANCH14 | |
0x11 | THUMB_MOV32 | REL32 | ||
0x12 | THUMB_BRANCH20 | |||
0x14 | REL32 | THUMB_BRANCH24 | ||
0x15 | THUMB_BLX23 | |||
0x16 | PAIR |
Sources: PE Format, Type Indicators (every machine below), and LLVM COFF.h
(RelocationTypeI386, RelocationTypeAMD64,
RelocationTypesARM, RelocationTypesARM64,
RelocationTypesMips). LLVM defines no types for SH, PowerPC, Itanium or M32R.
x64 (IMAGE_REL_AMD64_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | ADDR64 | The 64-bit VA of the target |
0x2 | ADDR32 | The 32-bit VA of the target |
0x3 | ADDR32NB | The 32-bit address without an image base (RVA) |
0x4 | REL32 | The 32-bit address relative to the byte following the relocation |
0x5 to 0x9 | REL32_1 to REL32_5 | The 32-bit address relative to a byte distance of 1, 2, 3, 4 or 5 from the relocation |
0xA | SECTION | The 16-bit section index of the section that contains the target (used for debugging information) |
0xB | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0xC | SECREL7 | A 7-bit unsigned offset from the base of the section that contains the target |
0xD | TOKEN | CLR tokens |
0xE | SREL32 | A 32-bit signed span-dependent value emitted into the object |
0xF | PAIR | A pair that must immediately follow every span-dependent value |
0x10 | SSPAN32 | A 32-bit signed span-dependent value applied at link time |
x86 (IMAGE_REL_I386_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | DIR16 | Not supported |
0x2 | REL16 | Not supported |
0x6 | DIR32 | The target’s 32-bit VA |
0x7 | DIR32NB | The target’s 32-bit RVA |
0x9 | SEG12 | Not supported |
0xA | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xB | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0xC | TOKEN | The CLR token |
0xD | SECREL7 | A 7-bit offset from the base of the section that contains the target |
0x14 | REL32 | The 32-bit relative displacement to the target; supports the x86 relative branch and call instructions |
ARM and Thumb (IMAGE_REL_ARM_*, IMAGE_REL_THUMB_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ARM_ABSOLUTE | Ignored |
0x1 | ARM_ADDR32 | The 32-bit VA of the target |
0x2 | ARM_ADDR32NB | The 32-bit RVA of the target |
0x3 | ARM_BRANCH24 | The 24-bit relative displacement to the target |
0x4 | ARM_BRANCH11 | A subroutine call: two 16-bit instructions with 11-bit offsets |
0xA | ARM_REL32 | The 32-bit address relative to the byte following the relocation |
0xE | ARM_SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xF | ARM_SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0x10 | ARM_MOV32 | The 32-bit VA of the target, applied with a MOVW for the low 16 bits followed by a MOVT for the high 16 bits |
0x11 | THUMB_MOV32 | The same, in Thumb instructions |
0x12 | THUMB_BRANCH20 | The instruction is fixed up with the 21-bit relative displacement to the 2-byte aligned target; the least significant bit is always zero and is not stored. A Thumb-2 32-bit conditional B instruction |
0x13 | (unused) | |
0x14 | THUMB_BRANCH24 | The 25-bit relative displacement to the 2-byte aligned target; the least significant bit is zero and is not stored. A Thumb-2 B instruction |
0x15 | THUMB_BLX23 | The 25-bit relative displacement to the 4-byte aligned target; the low 2 bits are zero and are not stored. A Thumb-2 BLX instruction |
0x16 | ARM_PAIR | Valid only immediately after an ARM_REFHI or THUMB_REFHI (names PE Format uses but does not list with values); its SymbolTableIndex is a displacement, not an index |
LLVM’s COFF.h also defines three ARM values that PE Format’s table does not list:
0x5 IMAGE_REL_ARM_TOKEN, 0x8 IMAGE_REL_ARM_BLX24 and 0x9
IMAGE_REL_ARM_BLX11 (it names 0x10 and 0x11 MOV32A and MOV32T, 0x12, 0x14 and 0x15
BRANCH20T, BRANCH24T and BLX23T). Microsoft gives no meaning for those three; their names
suggest a CLR token and ARM BLX branches with 24-bit and 11-bit displacements
(inference from the names).
ARM64 (IMAGE_REL_ARM64_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | ADDR32 | The 32-bit VA of the target |
0x2 | ADDR32NB | The 32-bit RVA of the target |
0x3 | BRANCH26 | The 26-bit relative displacement to the target, for B and BL |
0x4 | PAGEBASE_REL21 | The page base of the target, for ADRP |
0x5 | REL21 | The relative displacement to the target, for ADR (Microsoft’s text says “12-bit”) |
0x6 | PAGEOFFSET_12A | The 12-bit page offset of the target, for ADD/ADDS (immediate) with zero shift |
0x7 | PAGEOFFSET_12L | The 12-bit page offset of the target, for LDR (indexed, unsigned immediate) |
0x8 | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0x9 | SECREL_LOW12A | Bits 0 to 11 of the section offset of the target, for ADD/ADDS (immediate) with zero shift |
0xA | SECREL_HIGH12A | Bits 12 to 23 of the section offset of the target, for ADD/ADDS (immediate) with zero shift |
0xB | SECREL_LOW12L | Bits 0 to 11 of the section offset of the target, for LDR (indexed, unsigned immediate) |
0xC | TOKEN | CLR token |
0xD | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xE | ADDR64 | The 64-bit VA of the relocation target |
0xF | BRANCH19 | The 19-bit offset to the target, for the conditional B |
0x10 | BRANCH14 | The 14-bit offset to the target, for TBZ and TBNZ |
0x11 | REL32 | The 32-bit address relative to the byte following the relocation |
Hitachi SuperH (IMAGE_REL_SH3_*, IMAGE_REL_SHM_*)
For SH3 and SH4; the SHM (SH Media) names are SH5-specific.
| Value | Name | Meaning |
|---|---|---|
0x0 | SH3_ABSOLUTE | Ignored |
0x1 | SH3_DIRECT16 | A reference to the 16-bit location that contains the VA of the target symbol |
0x2 | SH3_DIRECT32 | The 32-bit VA of the target symbol |
0x3 | SH3_DIRECT8 | A reference to the 8-bit location that contains the VA of the target symbol |
0x4 | SH3_DIRECT8_WORD | A reference to the 8-bit instruction that contains the effective 16-bit VA of the target symbol |
0x5 | SH3_DIRECT8_LONG | A reference to the 8-bit instruction that contains the effective 32-bit VA of the target symbol |
0x6 | SH3_DIRECT4 | A reference to the 8-bit location whose low 4 bits contain the VA of the target symbol |
0x7 | SH3_DIRECT4_WORD | A reference to the 8-bit instruction whose low 4 bits contain the effective 16-bit VA of the target symbol |
0x8 | SH3_DIRECT4_LONG | A reference to the 8-bit instruction whose low 4 bits contain the effective 32-bit VA of the target symbol |
0x9 | SH3_PCREL8_WORD | A reference to the 8-bit instruction that contains the effective 16-bit relative offset of the target symbol |
0xA | SH3_PCREL8_LONG | A reference to the 8-bit instruction that contains the effective 32-bit relative offset of the target symbol |
0xB | SH3_PCREL12_WORD | A reference to the 16-bit instruction whose low 12 bits contain the effective 16-bit relative offset of the target symbol |
0xC | SH3_STARTOF_SECTION | A reference to a 32-bit location that is the VA of the section that contains the target symbol |
0xD | SH3_SIZEOF_SECTION | A reference to the 32-bit location that is the size of the section that contains the target symbol |
0xE | SH3_SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xF | SH3_SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0x10 | SH3_DIRECT32_NB | The 32-bit RVA of the target symbol |
0x11 | SH3_GPREL4_LONG | GP relative |
0x12 | SH3_TOKEN | CLR token |
0x13 | SHM_PCRELPT | The offset from the current instruction in longwords; if the NOMODE bit is not set, insert the inverse of the low bit at bit 32 to select PTA or PTB |
0x14 | SHM_REFLO | The low 16 bits of the 32-bit address |
0x15 | SHM_REFHALF | The high 16 bits of the 32-bit address |
0x16 | SHM_RELLO | The low 16 bits of the relative address |
0x17 | SHM_RELHALF | The high 16 bits of the relative address |
0x18 | SHM_PAIR | Valid only immediately after a REFHALF, RELHALF or RELLO relocation; its SymbolTableIndex is a displacement, not an index |
0x8000 | SHM_NOMODE | The relocation ignores section mode |
IBM PowerPC (IMAGE_REL_PPC_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | ADDR64 | The 64-bit VA of the target |
0x2 | ADDR32 | The 32-bit VA of the target |
0x3 | ADDR24 | The low 24 bits of the VA of the target; valid only when the target symbol is absolute and can be sign-extended to its original value |
0x4 | ADDR16 | The low 16 bits of the target’s VA |
0x5 | ADDR14 | The low 14 bits of the target’s VA; valid only when the target symbol is absolute and can be sign-extended to its original value |
0x6 | REL24 | A 24-bit PC-relative offset to the symbol’s location |
0x7 | REL14 | A 14-bit PC-relative offset to the symbol’s location |
0xA | ADDR32NB | The 32-bit RVA of the target |
0xB | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0xC | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xF | SECREL16 | The 16-bit offset of the target from the beginning of its section |
0x10 | REFHI | The high 16 bits of the target’s 32-bit VA, for the first instruction of a two-instruction sequence that loads a full address. It must be immediately followed by a PAIR relocation whose SymbolTableIndex holds a signed 16-bit displacement that is added to the upper 16 bits taken from the location being relocated |
0x11 | REFLO | The low 16 bits of the target’s VA |
0x12 | PAIR | Valid only immediately after a REFHI or SECRELHI relocation; its SymbolTableIndex is a displacement, not an index |
0x13 | SECRELLO | The low 16 bits of the 32-bit offset of the target from the beginning of its section |
0x15 | GPREL | The 16-bit signed displacement of the target relative to the GP register |
0x16 | TOKEN | The CLR token |
Intel Itanium (IMAGE_REL_IA64_*)
Relocations on instructions use the bundle’s offset and slot number for the relocation offset.
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | IMM14 | May be followed by an ADDEND relocation whose value is added to the target address before it is inserted into the slot of the IMM14 bundle. The target must be absolute or the image must be fixed |
0x2 | IMM22 | As IMM14, for the IMM22 bundle |
0x3 | IMM64 | The slot number of this relocation must be one (1). May be followed by an ADDEND relocation, added to the target address before it is stored in all three slots of the IMM64 bundle |
0x4 | DIR32 | The target’s 32-bit VA; supported only for /LARGEADDRESSAWARE:NO images |
0x5 | DIR64 | The target’s 64-bit VA |
0x6 | PCREL21B | Fixed up with the 25-bit relative displacement to the 16-bit aligned target; the low 4 bits are zero and not stored |
0x7 | PCREL21M | As PCREL21B |
0x8 | PCREL21F | The least significant bits of this relocation’s offset must contain the slot number and the rest is the bundle address; the bundle is fixed up with the 25-bit relative displacement to the 16-bit aligned target (low 4 bits zero, not stored) |
0x9 | GPREL22 | May be followed by an ADDEND relocation whose value is added to the target address, and then a 22-bit GP-relative offset is calculated and applied to the GPREL22 bundle |
0xA | LTOFF22 | Fixed up with the 22-bit GP-relative offset to the target symbol’s literal table entry; the linker creates that entry from this relocation and the ADDEND that might follow |
0xB | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xC | SECREL22 | Fixed up with the 22-bit offset of the target from the beginning of its section; may be followed immediately by an ADDEND relocation whose Value field holds the 32-bit unsigned offset of the target from the beginning of the section |
0xD | SECREL64I | The slot number must be one (1). Fixed up with the 64-bit offset of the target from the beginning of its section; may be followed by an ADDEND relocation as for SECREL22 |
0xE | SECREL32 | The address of data to be fixed up with the 32-bit offset of the target from the beginning of its section |
0x10 | DIR32NB | The target’s 32-bit RVA |
0x11 | SREL14 | Applied to a signed 14-bit immediate that holds the difference between two relocatable targets; a declarative field telling the linker the compiler already emitted this value |
0x12 | SREL22 | As SREL14, for a signed 22-bit immediate |
0x13 | SREL32 | As SREL14, for a signed 32-bit immediate |
0x14 | UREL32 | As SREL14, for an unsigned 32-bit immediate |
0x15 | PCREL60X | A 60-bit PC-relative fixup that always stays as a BRL instruction of an MLX bundle |
0x16 | PCREL60B | A 60-bit PC-relative fixup. If the displacement fits a signed 25-bit field, the whole bundle becomes an MBB bundle with NOP.B in slot 1 and a 25-bit BR instruction (4 lowest bits zero and dropped) in slot 2 |
0x17 | PCREL60F | As PCREL60B, converting to an MFB bundle with NOP.F in slot 1 |
0x18 | PCREL60I | As PCREL60B, converting to an MIB bundle with NOP.I in slot 1 |
0x19 | PCREL60M | As PCREL60B, converting to an MMB bundle with NOP.M in slot 1 |
0x1A | IMMGPREL64 | A 64-bit GP-relative fixup |
0x1B | TOKEN | A CLR token |
0x1C | GPREL32 | A 32-bit GP-relative fixup |
0x1F | ADDEND | Valid only immediately after one of IMM14, IMM22, IMM64, GPREL22, LTOFF22, LTOFF64, SECREL22, SECREL64I or SECREL32 (LTOFF64 appears in that sentence without a value); its value is the addend for instructions within a bundle, not for data |
MIPS (IMAGE_REL_MIPS_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | REFHALF | The high 16 bits of the target’s 32-bit VA |
0x2 | REFWORD | The target’s 32-bit VA |
0x3 | JMPADDR | The low 26 bits of the target’s VA; supports the J and JAL instructions |
0x4 | REFHI | The high 16 bits of the target’s 32-bit VA, for the first instruction of a two-instruction address load; must be immediately followed by a PAIR relocation whose SymbolTableIndex holds a signed 16-bit displacement added to the upper 16 bits taken from the location being relocated |
0x5 | REFLO | The low 16 bits of the target’s VA |
0x6 | GPREL | A 16-bit signed displacement of the target relative to the GP register |
0x7 | LITERAL | The same as GPREL |
0xA | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xB | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0xC | SECRELLO | The low 16 bits of the 32-bit offset of the target from the beginning of its section |
0xD | SECRELHI | The high 16 bits of the 32-bit section offset; a PAIR relocation must immediately follow, with a signed 16-bit displacement in its SymbolTableIndex added to the upper 16 bits taken from the location being relocated |
0x10 | JMPADDR16 | The low 26 bits of the target’s VA; supports the MIPS16 JAL instruction |
0x22 | REFWORDNB | The target’s 32-bit RVA |
0x25 | PAIR | Valid only immediately after a REFHI or SECRELHI relocation; its SymbolTableIndex is a displacement, not an index |
Mitsubishi M32R (IMAGE_REL_M32R_*)
| Value | Name | Meaning |
|---|---|---|
0x0 | ABSOLUTE | Ignored |
0x1 | ADDR32 | The target’s 32-bit VA |
0x2 | ADDR32NB | The target’s 32-bit RVA |
0x3 | ADDR24 | The target’s 24-bit VA |
0x4 | GPREL16 | The target’s 16-bit offset from the GP register |
0x5 | PCREL24 | The target’s 24-bit offset from the program counter, shifted left by 2 bits and sign-extended |
0x6 | PCREL16 | The target’s 16-bit offset from the PC, shifted left by 2 bits and sign-extended |
0x7 | PCREL8 | The target’s 8-bit offset from the PC, shifted left by 2 bits and sign-extended |
0x8 | REFHALF | The 16 most significant bits of the target VA |
0x9 | REFHI | The 16 MSBs of the target VA, adjusted for LSB sign extension; for the first instruction of a two-instruction sequence that loads a full 32-bit address; must be immediately followed by a PAIR relocation whose SymbolTableIndex holds a signed 16-bit displacement added to the upper 16 bits taken from the location being relocated |
0xA | REFLO | The 16 LSBs of the target VA |
0xB | PAIR | Must follow the REFHI relocation; its SymbolTableIndex is a displacement, not an index |
0xC | SECTION | The 16-bit section index of the section that contains the target (debugging information) |
0xD | SECREL | The 32-bit offset of the target from the beginning of its section (debugging information and static thread-local storage) |
0xE | TOKEN | The CLR token |
PE Format lists no relocation types for the other machines of the
Machine table (Alpha, AM33, EBC,
LoongArch, RISC-V, ARM64EC, ARM64X, SH3DSP/SH5 apart from the SHM names above). For ARM64EC and ARM64X, LLVM’s
isAnyArm64 in COFF.h treats them like ARM64, which suggests (an inference) that
the ARM64 numbers apply. A type value that is not in these tables is undefined: show the raw value.