Repository navigation
MC Layer 05 fixups relocations
Relevant source files
The following files were used as context for generating this wiki page:
The life of a fixup from the code emitter to the object file, with AArch64's fixup kinds and relocation choice.
| Field | Meaning |
|---|---|
| Offset | Byte offset in the fragment (the streamer shifts it from instruction-relative to fragment-relative) |
| Value | The MCExpr to compute |
| Kind |
MCFixupKind: generic (FK_Data_4, FK_Data_8) or a target kind |
-show-mc-encoding prints them. Bits that depend on a fixup are shown as A:
adrp x8, :got:gv // encoding: [0x08'A',A,A,0x90'A']
// fixup A - offset: 0, value: :got:gv, kind: fixup_aarch64_pcrel_adrp_imm21
ldr x8, [x8, :got_lo12:gv] // encoding: [0x08,0bAAAAAA01,0b01AAAAAA,0xf9]
// fixup A - offset: 0, value: :got_lo12:gv, kind: fixup_aarch64_ldst_imm12_scale8
bl g // encoding: [A,A,A,0b100101AA]
// fixup A - offset: 0, value: g, kind: fixup_aarch64_pcrel_call26
Read bl g: the top six bits 100101 are the BL opcode; the low 26 bits are the branch offset, unknown yet.
A target declares one kind per way an expression can be placed into an instruction. AArch64 (excerpt):
fixup_aarch64_pcrel_adr_imm21 = FirstTargetFixupKind,
fixup_aarch64_pcrel_adrp_imm21,
fixup_aarch64_add_imm12,
fixup_aarch64_ldst_imm12_scale1,
...
fixup_aarch64_ldst_imm12_scale8,
...
fixup_aarch64_pcrel_branch19,
fixup_aarch64_pcrel_branch26,
fixup_aarch64_pcrel_call26,getFixupKindInfo describes each one: bit offset, bit size, and whether it is PC-relative. The generic code uses that to know which bits to touch.
Sources: llvm/lib/Target/AArch64/MCTargetDesc/AArch64FixupKinds.h:19-64, llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp:44-73
flowchart TD
F["fixup after layout"] --> E["MCAssembler::evaluateFixup<br/>expression to MCValue"]
E --> Q{"value fully known?<br/>(local symbol, same section,<br/>no forced relocation)"}
Q -->|"yes"| A["AsmBackend::applyFixup<br/>patch bits"]
Q -->|"no"| R["ELFObjectWriter::recordRelocation<br/>target getRelocType picks R_* type"]
R --> A2["applyFixup patches any addend bits"]
AArch64's applyFixup shows both paths in a few lines:
if (shouldForceRelocation(Fixup))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
...
if (!Value)
return; // Doesn't change encoding.
...
// Apply any target-specific value adjustments.
Value = adjustFixupValue(Fixup, Target, Value, Ctx, TheTriple, IsResolved);
// Shift the value into position.
Value <<= Info.TargetOffset;
...
Data[i] |= uint8_t((Value >> (i * 8)) & 0xff);adjustFixupValue converts a raw value into the field's encoding and checks its range. For branches, the byte offset is divided by 4 (return Value >> 2;), because AArch64 branch offsets count instructions.
Sources: llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp:138-191, llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp:404-470, llvm/lib/MC/MCAssembler.cpp:144
The target's ELF writer maps fixup kind + specifier + PC-relativity to an ELF relocation number in getRelocType. For the fixups above, the mapping in AArch64ELFObjectWriter.cpp is:
| Fixup (from the listing) | Relocation | Source line |
|---|---|---|
fixup_aarch64_pcrel_adrp_imm21 with :got:
|
R_AARCH64_ADR_GOT_PAGE |
168 |
fixup_aarch64_pcrel_adrp_imm21 with :gottprel:
|
R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 |
178 |
fixup_aarch64_pcrel_branch26 |
R_AARCH64_JUMP26 |
192 |
fixup_aarch64_pcrel_call26 |
R_AARCH64_CALL26 |
194 |
fixup_aarch64_pcrel_branch19 |
R_AARCH64_CONDBR19 |
221 |
The table is read from the source, not from an object file: this build has no llvm-readelf to print the relocation section.
The specifier matters as much as the kind: the same adrp fixup becomes a different relocation for :got:, :gottprel: or a plain symbol. getRelocType also reports impossible combinations (for example a :plt: specifier anywhere other than a .word).
Sources: llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp:85-221
ELFObjectWriter (in lib/MC) does the format work so targets do not have to: recordRelocation collects relocations per section, executePostLayoutBinding resolves symbol aliases, and ELFWriter::writeObject writes the header, sections, symbol table, string tables and .rela sections. The target only supplies getRelocType and a few policy hooks (needsRelocateWithSymbol: relocate against the symbol or against its section).
Sources: llvm/lib/MC/ELFObjectWriter.cpp:722-991, llvm/lib/MC/ELFObjectWriter.cpp:1171-1403, llvm/include/llvm/MC/MCELFObjectWriter.h:88-91
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look