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/Core/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
22 class InputSectionBase;
25 // List of target-independent relocation types. Relocations read
26 // from files are converted to these types so that the main code
27 // doesn't have to know about architecture-specific details.
32 R_GOTONLY_PC_FROM_END,
43 R_MIPS_GOT_LOCAL_PAGE,
61 R_RELAX_TLS_GD_TO_IE_ABS,
62 R_RELAX_TLS_GD_TO_IE_END,
63 R_RELAX_TLS_GD_TO_IE_PAGE_PC,
65 R_RELAX_TLS_GD_TO_LE_NEG,
79 // Build a bitmask with one bit set for each RelExpr.
81 // Constexpr function arguments can't be used in static asserts, so we
82 // use template arguments to build the mask.
83 // But function template partial specializations don't exist (needed
84 // for base case of the recursion), so we need a dummy struct.
85 template <RelExpr... Exprs> struct RelExprMaskBuilder {
86 static inline uint64_t build() { return 0; }
89 // Specialization for recursive case.
90 template <RelExpr Head, RelExpr... Tail>
91 struct RelExprMaskBuilder<Head, Tail...> {
92 static inline uint64_t build() {
93 static_assert(0 <= Head && Head < 64,
94 "RelExpr is too large for 64-bit mask!");
95 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
99 // Return true if `Expr` is one of `Exprs`.
100 // There are fewer than 64 RelExpr's, so we can represent any set of
101 // RelExpr's as a constant bit mask and test for membership with a
102 // couple cheap bitwise operations.
103 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
104 assert(0 <= Expr && (int)Expr < 64 && "RelExpr is too large for 64-bit mask!");
105 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
108 // Architecture-neutral representation of relocation.
117 template <class ELFT> void scanRelocations(InputSectionBase &);
122 template <class ELFT> class ThunkCreator {
124 // Return true if Thunks have been added to OutputSections
125 bool createThunks(ArrayRef<OutputSection *> OutputSections);
128 void mergeThunks(OutputSection *OS, std::vector<ThunkSection *> &Thunks);
129 ThunkSection *getOSThunkSec(ThunkSection *&TS, OutputSection *OS);
130 ThunkSection *getISThunkSec(InputSection *IS, OutputSection *OS);
131 std::pair<Thunk *, bool> getThunk(SymbolBody &Body, uint32_t Type);
133 // Track Symbols that already have a Thunk
134 llvm::DenseMap<SymbolBody *, Thunk *> ThunkedSymbols;
136 // Track InputSections that have a ThunkSection placed in front
137 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
139 // Track the ThunksSections that need to be inserted into an OutputSection
140 std::map<OutputSection *, std::vector<ThunkSection *>> ThunkSections;
143 // Return a int64_t to make sure we get the sign extension out of the way as
144 // early as possible.
145 template <class ELFT>
146 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
149 template <class ELFT>
150 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {