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.
36 R_AARCH64_GOT_PAGE_PC,
37 // The expression is used for IFUNC support. Describes PC-relative
38 // address of the memory page of GOT entry. This entry is used for
39 // a redirection to IPLT.
40 R_AARCH64_GOT_PAGE_PC_PLT,
41 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
43 R_AARCH64_PLT_PAGE_PC,
44 R_AARCH64_TLSDESC_PAGE,
47 // The expression is used for IFUNC support. Evaluates to GOT entry,
48 // containing redirection to the IPLT.
51 R_GOTONLY_PC_FROM_END,
62 R_MIPS_GOT_LOCAL_PAGE,
78 R_RELAX_TLS_GD_TO_IE_ABS,
79 R_RELAX_TLS_GD_TO_IE_END,
80 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
82 R_RELAX_TLS_GD_TO_LE_NEG,
85 R_RELAX_TLS_LD_TO_LE_ABS,
102 // Build a bitmask with one bit set for each RelExpr.
104 // Constexpr function arguments can't be used in static asserts, so we
105 // use template arguments to build the mask.
106 // But function template partial specializations don't exist (needed
107 // for base case of the recursion), so we need a dummy struct.
108 template <RelExpr... Exprs> struct RelExprMaskBuilder {
109 static inline uint64_t build() { return 0; }
112 // Specialization for recursive case.
113 template <RelExpr Head, RelExpr... Tail>
114 struct RelExprMaskBuilder<Head, Tail...> {
115 static inline uint64_t build() {
116 static_assert(0 <= Head && Head < 64,
117 "RelExpr is too large for 64-bit mask!");
118 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
122 // Return true if `Expr` is one of `Exprs`.
123 // There are fewer than 64 RelExpr's, so we can represent any set of
124 // RelExpr's as a constant bit mask and test for membership with a
125 // couple cheap bitwise operations.
126 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
127 assert(0 <= Expr && (int)Expr < 64 &&
128 "RelExpr is too large for 64-bit mask!");
129 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
132 // Architecture-neutral representation of relocation.
141 struct RelocationOffsetComparator {
142 bool operator()(const Relocation &Lhs, const Relocation &Rhs) {
143 return Lhs.Offset < Rhs.Offset;
146 // For std::lower_bound, std::upper_bound, std::equal_range.
147 bool operator()(const Relocation &Rel, uint64_t Val) {
148 return Rel.Offset < Val;
151 bool operator()(uint64_t Val, const Relocation &Rel) {
152 return Val < Rel.Offset;
156 template <class ELFT> void scanRelocations(InputSectionBase &);
160 struct InputSectionDescription;
164 // Return true if Thunks have been added to OutputSections
165 bool createThunks(ArrayRef<OutputSection *> OutputSections);
167 // The number of completed passes of createThunks this permits us
168 // to do one time initialization on Pass 0 and put a limit on the
169 // number of times it can be called to prevent infinite loops.
173 void mergeThunks(ArrayRef<OutputSection *> OutputSections);
175 ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
176 InputSectionDescription *ISD, uint32_t Type,
179 ThunkSection *getISThunkSec(InputSection *IS);
181 void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
183 std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
185 ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
188 bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
190 // Record all the available Thunks for a Symbol
191 llvm::DenseMap<std::pair<SectionBase *, uint64_t>, std::vector<Thunk *>>
192 ThunkedSymbolsBySection;
193 llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols;
195 // Find a Thunk from the Thunks symbol definition, we can use this to find
196 // the Thunk from a relocation to the Thunks symbol definition.
197 llvm::DenseMap<Symbol *, Thunk *> Thunks;
199 // Track InputSections that have an inline ThunkSection placed in front
200 // an inline ThunkSection may have control fall through to the section below
201 // so we need to make sure that there is only one of them.
202 // The Mips LA25 Thunk is an example of an inline ThunkSection.
203 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
206 // Return a int64_t to make sure we get the sign extension out of the way as
207 // early as possible.
208 template <class ELFT>
209 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
212 template <class ELFT>
213 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {