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;
24 struct OutputSectionCommand;
26 // List of target-independent relocation types. Relocations read
27 // from files are converted to these types so that the main code
28 // doesn't have to know about architecture-specific details.
34 R_GOTONLY_PC_FROM_END,
45 R_MIPS_GOT_LOCAL_PAGE,
63 R_RELAX_TLS_GD_TO_IE_ABS,
64 R_RELAX_TLS_GD_TO_IE_END,
65 R_RELAX_TLS_GD_TO_IE_PAGE_PC,
67 R_RELAX_TLS_GD_TO_LE_NEG,
81 // Build a bitmask with one bit set for each RelExpr.
83 // Constexpr function arguments can't be used in static asserts, so we
84 // use template arguments to build the mask.
85 // But function template partial specializations don't exist (needed
86 // for base case of the recursion), so we need a dummy struct.
87 template <RelExpr... Exprs> struct RelExprMaskBuilder {
88 static inline uint64_t build() { return 0; }
91 // Specialization for recursive case.
92 template <RelExpr Head, RelExpr... Tail>
93 struct RelExprMaskBuilder<Head, Tail...> {
94 static inline uint64_t build() {
95 static_assert(0 <= Head && Head < 64,
96 "RelExpr is too large for 64-bit mask!");
97 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
101 // Return true if `Expr` is one of `Exprs`.
102 // There are fewer than 64 RelExpr's, so we can represent any set of
103 // RelExpr's as a constant bit mask and test for membership with a
104 // couple cheap bitwise operations.
105 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
106 assert(0 <= Expr && (int)Expr < 64 &&
107 "RelExpr is too large for 64-bit mask!");
108 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
111 // Architecture-neutral representation of relocation.
120 template <class ELFT> void scanRelocations(InputSectionBase &);
127 // Return true if Thunks have been added to OutputSections
128 bool createThunks(ArrayRef<OutputSectionCommand *> OutputSections);
130 // The number of completed passes of createThunks this permits us
131 // to do one time initialization on Pass 0 and put a limit on the
132 // number of times it can be called to prevent infinite loops.
137 ThunkSection *getOSThunkSec(OutputSectionCommand *Cmd,
138 std::vector<InputSection *> *ISR);
139 ThunkSection *getISThunkSec(InputSection *IS, OutputSection *OS);
140 void forEachExecInputSection(
141 ArrayRef<OutputSectionCommand *> OutputSections,
142 std::function<void(OutputSectionCommand *, std::vector<InputSection *> *,
145 std::pair<Thunk *, bool> getThunk(SymbolBody &Body, uint32_t Type);
146 ThunkSection *addThunkSection(OutputSection *OS,
147 std::vector<InputSection *> *, uint64_t Off);
148 // Record all the available Thunks for a Symbol
149 llvm::DenseMap<SymbolBody *, std::vector<Thunk *>> ThunkedSymbols;
151 // Find a Thunk from the Thunks symbol definition, we can use this to find
152 // the Thunk from a relocation to the Thunks symbol definition.
153 llvm::DenseMap<SymbolBody *, Thunk *> Thunks;
155 // Track InputSections that have an inline ThunkSection placed in front
156 // an inline ThunkSection may have control fall through to the section below
157 // so we need to make sure that there is only one of them.
158 // The Mips LA25 Thunk is an example of an inline ThunkSection.
159 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
161 // All the ThunkSections that we have created, organised by OutputSection
162 // will contain a mix of ThunkSections that have been created this pass, and
163 // ThunkSections that have been merged into the OutputSection on previous
165 std::map<std::vector<InputSection *> *, std::vector<ThunkSection *>>
168 // The ThunkSection for this vector of InputSections
172 // Return a int64_t to make sure we get the sign extension out of the way as
173 // early as possible.
174 template <class ELFT>
175 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
178 template <class ELFT>
179 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {