]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Relocations.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304659, 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
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.
28 enum RelExpr {
29   R_ABS,
30   R_ARM_SBREL,
31   R_GOT,
32   R_GOTONLY_PC,
33   R_GOTONLY_PC_FROM_END,
34   R_GOTREL,
35   R_GOTREL_FROM_END,
36   R_GOT_FROM_END,
37   R_GOT_OFF,
38   R_GOT_PAGE_PC,
39   R_GOT_PC,
40   R_HINT,
41   R_MIPS_GOTREL,
42   R_MIPS_GOT_GP,
43   R_MIPS_GOT_GP_PC,
44   R_MIPS_GOT_LOCAL_PAGE,
45   R_MIPS_GOT_OFF,
46   R_MIPS_GOT_OFF32,
47   R_MIPS_TLSGD,
48   R_MIPS_TLSLD,
49   R_NEG_TLS,
50   R_NONE,
51   R_PAGE_PC,
52   R_PC,
53   R_PLT,
54   R_PLT_PAGE_PC,
55   R_PLT_PC,
56   R_PPC_OPD,
57   R_PPC_PLT_OPD,
58   R_PPC_TOC,
59   R_RELAX_GOT_PC,
60   R_RELAX_GOT_PC_NOPIC,
61   R_RELAX_TLS_GD_TO_IE,
62   R_RELAX_TLS_GD_TO_IE_ABS,
63   R_RELAX_TLS_GD_TO_IE_END,
64   R_RELAX_TLS_GD_TO_IE_PAGE_PC,
65   R_RELAX_TLS_GD_TO_LE,
66   R_RELAX_TLS_GD_TO_LE_NEG,
67   R_RELAX_TLS_IE_TO_LE,
68   R_RELAX_TLS_LD_TO_LE,
69   R_SIZE,
70   R_TLS,
71   R_TLSDESC,
72   R_TLSDESC_CALL,
73   R_TLSDESC_PAGE,
74   R_TLSGD,
75   R_TLSGD_PC,
76   R_TLSLD,
77   R_TLSLD_PC,
78 };
79
80 // Build a bitmask with one bit set for each RelExpr.
81 //
82 // Constexpr function arguments can't be used in static asserts, so we
83 // use template arguments to build the mask.
84 // But function template partial specializations don't exist (needed
85 // for base case of the recursion), so we need a dummy struct.
86 template <RelExpr... Exprs> struct RelExprMaskBuilder {
87   static inline uint64_t build() { return 0; }
88 };
89
90 // Specialization for recursive case.
91 template <RelExpr Head, RelExpr... Tail>
92 struct RelExprMaskBuilder<Head, Tail...> {
93   static inline uint64_t build() {
94     static_assert(0 <= Head && Head < 64,
95                   "RelExpr is too large for 64-bit mask!");
96     return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
97   }
98 };
99
100 // Return true if `Expr` is one of `Exprs`.
101 // There are fewer than 64 RelExpr's, so we can represent any set of
102 // RelExpr's as a constant bit mask and test for membership with a
103 // couple cheap bitwise operations.
104 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
105   assert(0 <= Expr && (int)Expr < 64 && "RelExpr is too large for 64-bit mask!");
106   return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
107 }
108
109 // Architecture-neutral representation of relocation.
110 struct Relocation {
111   RelExpr Expr;
112   uint32_t Type;
113   uint64_t Offset;
114   int64_t Addend;
115   SymbolBody *Sym;
116 };
117
118 template <class ELFT> void scanRelocations(InputSectionBase &);
119
120 class ThunkSection;
121 class Thunk;
122
123 class ThunkCreator {
124 public:
125   // Return true if Thunks have been added to OutputSections
126   bool createThunks(ArrayRef<OutputSection *> OutputSections);
127
128 private:
129   void mergeThunks(OutputSection *OS, std::vector<ThunkSection *> &Thunks);
130   ThunkSection *getOSThunkSec(ThunkSection *&TS, OutputSection *OS);
131   ThunkSection *getISThunkSec(InputSection *IS, OutputSection *OS);
132   std::pair<Thunk *, bool> getThunk(SymbolBody &Body, uint32_t Type);
133
134   // Track Symbols that already have a Thunk
135   llvm::DenseMap<SymbolBody *, Thunk *> ThunkedSymbols;
136
137   // Track InputSections that have a ThunkSection placed in front
138   llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
139
140   // Track the ThunksSections that need to be inserted into an OutputSection
141   std::map<OutputSection *, std::vector<ThunkSection *>> ThunkSections;
142 };
143
144 // Return a int64_t to make sure we get the sign extension out of the way as
145 // early as possible.
146 template <class ELFT>
147 static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
148   return 0;
149 }
150 template <class ELFT>
151 static inline int64_t getAddend(const typename ELFT::Rela &Rel) {
152   return Rel.r_addend;
153 }
154 }
155 }
156
157 #endif