]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Relocations.h
Merge clang 7.0.1 and several follow-up changes
[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 SectionBase;
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_ADDEND,
36   R_ARM_SBREL,
37   R_GOT,
38   R_GOTONLY_PC,
39   R_GOTONLY_PC_FROM_END,
40   R_GOTREL,
41   R_GOTREL_FROM_END,
42   R_GOT_FROM_END,
43   R_GOT_OFF,
44   R_GOT_PAGE_PC,
45   R_GOT_PC,
46   R_HINT,
47   R_MIPS_GOTREL,
48   R_MIPS_GOT_GP,
49   R_MIPS_GOT_GP_PC,
50   R_MIPS_GOT_LOCAL_PAGE,
51   R_MIPS_GOT_OFF,
52   R_MIPS_GOT_OFF32,
53   R_MIPS_TLSGD,
54   R_MIPS_TLSLD,
55   R_NEG_TLS,
56   R_NONE,
57   R_PAGE_PC,
58   R_PC,
59   R_PLT,
60   R_PLT_PAGE_PC,
61   R_PLT_PC,
62   R_PPC_CALL,
63   R_PPC_CALL_PLT,
64   R_PPC_TOC,
65   R_RELAX_GOT_PC,
66   R_RELAX_GOT_PC_NOPIC,
67   R_RELAX_TLS_GD_TO_IE,
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,
72   R_RELAX_TLS_GD_TO_LE,
73   R_RELAX_TLS_GD_TO_LE_NEG,
74   R_RELAX_TLS_IE_TO_LE,
75   R_RELAX_TLS_LD_TO_LE,
76   R_RELAX_TLS_LD_TO_LE_ABS,
77   R_SIZE,
78   R_TLS,
79   R_TLSDESC,
80   R_TLSDESC_CALL,
81   R_TLSDESC_PAGE,
82   R_TLSGD_GOT,
83   R_TLSGD_GOT_FROM_END,
84   R_TLSGD_PC,
85   R_TLSLD_GOT,
86   R_TLSLD_GOT_FROM_END,
87   R_TLSLD_GOT_OFF,
88   R_TLSLD_HINT,
89   R_TLSLD_PC,
90 };
91
92 // Build a bitmask with one bit set for each RelExpr.
93 //
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; }
100 };
101
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();
109   }
110 };
111
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();
120 }
121
122 // Architecture-neutral representation of relocation.
123 struct Relocation {
124   RelExpr Expr;
125   RelType Type;
126   uint64_t Offset;
127   int64_t Addend;
128   Symbol *Sym;
129 };
130
131 template <class ELFT> void scanRelocations(InputSectionBase &);
132
133 class ThunkSection;
134 class Thunk;
135 struct InputSectionDescription;
136
137 class ThunkCreator {
138 public:
139   // Return true if Thunks have been added to OutputSections
140   bool createThunks(ArrayRef<OutputSection *> OutputSections);
141
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.
145   uint32_t Pass = 0;
146
147 private:
148   void mergeThunks(ArrayRef<OutputSection *> OutputSections);
149
150   ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
151                                InputSectionDescription *ISD, uint32_t Type,
152                                uint64_t Src);
153
154   ThunkSection *getISThunkSec(InputSection *IS);
155
156   void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
157
158   void forEachInputSectionDescription(
159       ArrayRef<OutputSection *> OutputSections,
160       llvm::function_ref<void(OutputSection *, InputSectionDescription *)> Fn);
161
162   std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
163
164   ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
165                                 uint64_t Off);
166
167   bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
168
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;
173
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;
177
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;
183 };
184
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) {
189   return 0;
190 }
191 template <class ELFT>
192 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {
193   return Rel.r_addend;
194 }
195 } // namespace elf
196 } // namespace lld
197
198 #endif