]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Relocations.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305145, and update
[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/Core/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include <map>
16 #include <vector>
17
18 namespace lld {
19 namespace elf {
20 class SymbolBody;
21 class InputSection;
22 class InputSectionBase;
23 class OutputSection;
24 struct OutputSectionCommand;
25
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.
29 enum RelExpr {
30   R_ABS,
31   R_ARM_SBREL,
32   R_GOT,
33   R_GOTONLY_PC,
34   R_GOTONLY_PC_FROM_END,
35   R_GOTREL,
36   R_GOTREL_FROM_END,
37   R_GOT_FROM_END,
38   R_GOT_OFF,
39   R_GOT_PAGE_PC,
40   R_GOT_PC,
41   R_HINT,
42   R_MIPS_GOTREL,
43   R_MIPS_GOT_GP,
44   R_MIPS_GOT_GP_PC,
45   R_MIPS_GOT_LOCAL_PAGE,
46   R_MIPS_GOT_OFF,
47   R_MIPS_GOT_OFF32,
48   R_MIPS_TLSGD,
49   R_MIPS_TLSLD,
50   R_NEG_TLS,
51   R_NONE,
52   R_PAGE_PC,
53   R_PC,
54   R_PLT,
55   R_PLT_PAGE_PC,
56   R_PLT_PC,
57   R_PPC_OPD,
58   R_PPC_PLT_OPD,
59   R_PPC_TOC,
60   R_RELAX_GOT_PC,
61   R_RELAX_GOT_PC_NOPIC,
62   R_RELAX_TLS_GD_TO_IE,
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,
66   R_RELAX_TLS_GD_TO_LE,
67   R_RELAX_TLS_GD_TO_LE_NEG,
68   R_RELAX_TLS_IE_TO_LE,
69   R_RELAX_TLS_LD_TO_LE,
70   R_SIZE,
71   R_TLS,
72   R_TLSDESC,
73   R_TLSDESC_CALL,
74   R_TLSDESC_PAGE,
75   R_TLSGD,
76   R_TLSGD_PC,
77   R_TLSLD,
78   R_TLSLD_PC,
79 };
80
81 // Build a bitmask with one bit set for each RelExpr.
82 //
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; }
89 };
90
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();
98   }
99 };
100
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 && "RelExpr is too large for 64-bit mask!");
107   return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
108 }
109
110 // Architecture-neutral representation of relocation.
111 struct Relocation {
112   RelExpr Expr;
113   uint32_t Type;
114   uint64_t Offset;
115   int64_t Addend;
116   SymbolBody *Sym;
117 };
118
119 template <class ELFT> void scanRelocations(InputSectionBase &);
120
121 class ThunkSection;
122 class Thunk;
123
124 class ThunkCreator {
125 public:
126   // Return true if Thunks have been added to OutputSections
127   bool createThunks(ArrayRef<OutputSectionCommand *> OutputSections);
128
129 private:
130   void mergeThunks();
131   ThunkSection *getOSThunkSec(OutputSection *OS,
132                               std::vector<InputSection *> *ISR);
133   ThunkSection *getISThunkSec(InputSection *IS, OutputSection *OS);
134   void forEachExecInputSection(
135       ArrayRef<OutputSectionCommand *> OutputSections,
136       std::function<void(OutputSection *, std::vector<InputSection *> *,
137                          InputSection *)>
138           Fn);
139   std::pair<Thunk *, bool> getThunk(SymbolBody &Body, uint32_t Type);
140
141   // Track Symbols that already have a Thunk
142   llvm::DenseMap<SymbolBody *, Thunk *> ThunkedSymbols;
143
144   // Track InputSections that have a ThunkSection placed in front
145   llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
146
147   // Track the ThunksSections that need to be inserted into an OutputSection
148   std::map<std::vector<InputSection *> *, std::vector<ThunkSection *>>
149       ThunkSections;
150
151   // The ThunkSection for this vector of InputSections
152   ThunkSection *CurTS;
153 };
154
155 // Return a int64_t to make sure we get the sign extension out of the way as
156 // early as possible.
157 template <class ELFT>
158 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
159   return 0;
160 }
161 template <class ELFT>
162 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {
163   return Rel.r_addend;
164 }
165 }
166 }
167
168 #endif