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