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.
39 R_GOTONLY_PC_FROM_END,
50 R_MIPS_GOT_LOCAL_PAGE,
68 R_RELAX_TLS_GD_TO_IE_ABS,
69 R_RELAX_TLS_GD_TO_IE_END,
70 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
71 R_RELAX_TLS_GD_TO_IE_PAGE_PC,
73 R_RELAX_TLS_GD_TO_LE_NEG,
76 R_RELAX_TLS_LD_TO_LE_ABS,
92 // Build a bitmask with one bit set for each RelExpr.
94 // Constexpr function arguments can't be used in static asserts, so we
95 // use template arguments to build the mask.
96 // But function template partial specializations don't exist (needed
97 // for base case of the recursion), so we need a dummy struct.
98 template <RelExpr... Exprs> struct RelExprMaskBuilder {
99 static inline uint64_t build() { return 0; }
102 // Specialization for recursive case.
103 template <RelExpr Head, RelExpr... Tail>
104 struct RelExprMaskBuilder<Head, Tail...> {
105 static inline uint64_t build() {
106 static_assert(0 <= Head && Head < 64,
107 "RelExpr is too large for 64-bit mask!");
108 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
112 // Return true if `Expr` is one of `Exprs`.
113 // There are fewer than 64 RelExpr's, so we can represent any set of
114 // RelExpr's as a constant bit mask and test for membership with a
115 // couple cheap bitwise operations.
116 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
117 assert(0 <= Expr && (int)Expr < 64 &&
118 "RelExpr is too large for 64-bit mask!");
119 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
122 // Architecture-neutral representation of relocation.
131 template <class ELFT> void scanRelocations(InputSectionBase &);
135 struct InputSectionDescription;
139 // Return true if Thunks have been added to OutputSections
140 bool createThunks(ArrayRef<OutputSection *> OutputSections);
142 // The number of completed passes of createThunks this permits us
143 // to do one time initialization on Pass 0 and put a limit on the
144 // number of times it can be called to prevent infinite loops.
148 void mergeThunks(ArrayRef<OutputSection *> OutputSections);
150 ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
151 InputSectionDescription *ISD, uint32_t Type,
154 ThunkSection *getISThunkSec(InputSection *IS);
156 void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
158 void forEachInputSectionDescription(
159 ArrayRef<OutputSection *> OutputSections,
160 llvm::function_ref<void(OutputSection *, InputSectionDescription *)> Fn);
162 std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
164 ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
167 bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
169 // Record all the available Thunks for a Symbol
170 llvm::DenseMap<std::pair<SectionBase *, uint64_t>, std::vector<Thunk *>>
171 ThunkedSymbolsBySection;
172 llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols;
174 // Find a Thunk from the Thunks symbol definition, we can use this to find
175 // the Thunk from a relocation to the Thunks symbol definition.
176 llvm::DenseMap<Symbol *, Thunk *> Thunks;
178 // Track InputSections that have an inline ThunkSection placed in front
179 // an inline ThunkSection may have control fall through to the section below
180 // so we need to make sure that there is only one of them.
181 // The Mips LA25 Thunk is an example of an inline ThunkSection.
182 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
185 // Return a int64_t to make sure we get the sign extension out of the way as
186 // early as possible.
187 template <class ELFT>
188 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
191 template <class ELFT>
192 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {