1 //===- Relocations.h -------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_RELOCATIONS_H
11 #define LLD_ELF_RELOCATIONS_H
13 #include "lld/Common/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
22 class InputSectionBase;
26 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
27 typedef uint32_t RelType;
29 // List of target-independent relocation types. Relocations read
30 // from files are converted to these types so that the main code
31 // doesn't have to know about architecture-specific details.
38 R_GOTONLY_PC_FROM_END,
49 R_MIPS_GOT_LOCAL_PAGE,
67 R_RELAX_TLS_GD_TO_IE_ABS,
68 R_RELAX_TLS_GD_TO_IE_END,
69 R_RELAX_TLS_GD_TO_IE_PAGE_PC,
71 R_RELAX_TLS_GD_TO_LE_NEG,
85 // Build a bitmask with one bit set for each RelExpr.
87 // Constexpr function arguments can't be used in static asserts, so we
88 // use template arguments to build the mask.
89 // But function template partial specializations don't exist (needed
90 // for base case of the recursion), so we need a dummy struct.
91 template <RelExpr... Exprs> struct RelExprMaskBuilder {
92 static inline uint64_t build() { return 0; }
95 // Specialization for recursive case.
96 template <RelExpr Head, RelExpr... Tail>
97 struct RelExprMaskBuilder<Head, Tail...> {
98 static inline uint64_t build() {
99 static_assert(0 <= Head && Head < 64,
100 "RelExpr is too large for 64-bit mask!");
101 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
105 // Return true if `Expr` is one of `Exprs`.
106 // There are fewer than 64 RelExpr's, so we can represent any set of
107 // RelExpr's as a constant bit mask and test for membership with a
108 // couple cheap bitwise operations.
109 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
110 assert(0 <= Expr && (int)Expr < 64 &&
111 "RelExpr is too large for 64-bit mask!");
112 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
115 // Architecture-neutral representation of relocation.
124 template <class ELFT> void scanRelocations(InputSectionBase &);
128 struct InputSectionDescription;
132 // Return true if Thunks have been added to OutputSections
133 bool createThunks(ArrayRef<OutputSection *> OutputSections);
135 // The number of completed passes of createThunks this permits us
136 // to do one time initialization on Pass 0 and put a limit on the
137 // number of times it can be called to prevent infinite loops.
141 void mergeThunks(ArrayRef<OutputSection *> OutputSections);
143 ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
144 InputSectionDescription *ISD, uint32_t Type,
147 ThunkSection *getISThunkSec(InputSection *IS);
149 void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
151 void forEachInputSectionDescription(
152 ArrayRef<OutputSection *> OutputSections,
153 std::function<void(OutputSection *, InputSectionDescription *)> Fn);
155 std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
157 ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
160 bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
162 // Record all the available Thunks for a Symbol
163 llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols;
165 // Find a Thunk from the Thunks symbol definition, we can use this to find
166 // the Thunk from a relocation to the Thunks symbol definition.
167 llvm::DenseMap<Symbol *, Thunk *> Thunks;
169 // Track InputSections that have an inline ThunkSection placed in front
170 // an inline ThunkSection may have control fall through to the section below
171 // so we need to make sure that there is only one of them.
172 // The Mips LA25 Thunk is an example of an inline ThunkSection.
173 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
176 // Return a int64_t to make sure we get the sign extension out of the way as
177 // early as possible.
178 template <class ELFT>
179 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
182 template <class ELFT>
183 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {