]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Relocations.h
lld: Only lookup LMARegion once. NFC.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Relocations.h
1 //===- Relocations.h -------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_RELOCATIONS_H
11 #define LLD_ELF_RELOCATIONS_H
12
13 #include "lld/Common/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include <map>
16 #include <vector>
17
18 namespace lld {
19 namespace elf {
20 class Symbol;
21 class InputSection;
22 class InputSectionBase;
23 class OutputSection;
24 class OutputSection;
25
26 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
27 typedef uint32_t RelType;
28
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.
32 enum RelExpr {
33   R_INVALID,
34   R_ABS,
35   R_ARM_SBREL,
36   R_GOT,
37   R_GOTONLY_PC,
38   R_GOTONLY_PC_FROM_END,
39   R_GOTREL,
40   R_GOTREL_FROM_END,
41   R_GOT_FROM_END,
42   R_GOT_OFF,
43   R_GOT_PAGE_PC,
44   R_GOT_PC,
45   R_HINT,
46   R_MIPS_GOTREL,
47   R_MIPS_GOT_GP,
48   R_MIPS_GOT_GP_PC,
49   R_MIPS_GOT_LOCAL_PAGE,
50   R_MIPS_GOT_OFF,
51   R_MIPS_GOT_OFF32,
52   R_MIPS_TLSGD,
53   R_MIPS_TLSLD,
54   R_NEG_TLS,
55   R_NONE,
56   R_PAGE_PC,
57   R_PC,
58   R_PLT,
59   R_PLT_PAGE_PC,
60   R_PLT_PC,
61   R_PPC_OPD,
62   R_PPC_PLT_OPD,
63   R_PPC_TOC,
64   R_RELAX_GOT_PC,
65   R_RELAX_GOT_PC_NOPIC,
66   R_RELAX_TLS_GD_TO_IE,
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,
70   R_RELAX_TLS_GD_TO_LE,
71   R_RELAX_TLS_GD_TO_LE_NEG,
72   R_RELAX_TLS_IE_TO_LE,
73   R_RELAX_TLS_LD_TO_LE,
74   R_SIZE,
75   R_TLS,
76   R_TLSDESC,
77   R_TLSDESC_CALL,
78   R_TLSDESC_PAGE,
79   R_TLSGD,
80   R_TLSGD_PC,
81   R_TLSLD,
82   R_TLSLD_PC,
83 };
84
85 // Build a bitmask with one bit set for each RelExpr.
86 //
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; }
93 };
94
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();
102   }
103 };
104
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();
113 }
114
115 // Architecture-neutral representation of relocation.
116 struct Relocation {
117   RelExpr Expr;
118   RelType Type;
119   uint64_t Offset;
120   int64_t Addend;
121   Symbol *Sym;
122 };
123
124 template <class ELFT> void scanRelocations(InputSectionBase &);
125
126 class ThunkSection;
127 class Thunk;
128 struct InputSectionDescription;
129
130 class ThunkCreator {
131 public:
132   // Return true if Thunks have been added to OutputSections
133   bool createThunks(ArrayRef<OutputSection *> OutputSections);
134
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.
138   uint32_t Pass = 0;
139
140 private:
141   void mergeThunks(ArrayRef<OutputSection *> OutputSections);
142
143   ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
144                                InputSectionDescription *ISD, uint32_t Type,
145                                uint64_t Src);
146
147   ThunkSection *getISThunkSec(InputSection *IS);
148
149   void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
150
151   void forEachInputSectionDescription(
152       ArrayRef<OutputSection *> OutputSections,
153       std::function<void(OutputSection *, InputSectionDescription *)> Fn);
154
155   std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
156
157   ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
158                                 uint64_t Off);
159
160   bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
161
162   // Record all the available Thunks for a Symbol
163   llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols;
164
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;
168
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;
174 };
175
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) {
180   return 0;
181 }
182 template <class ELFT>
183 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {
184   return Rel.r_addend;
185 }
186 } // namespace elf
187 } // namespace lld
188
189 #endif