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_AARCH64_GOT_PAGE_PC, 37 // The expression is used for IFUNC support. Describes PC-relative 38 // address of the memory page of GOT entry. This entry is used for 39 // a redirection to IPLT. 40 R_AARCH64_GOT_PAGE_PC_PLT, 41 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC, 42 R_AARCH64_PAGE_PC, 43 R_AARCH64_PLT_PAGE_PC, 44 R_AARCH64_TLSDESC_PAGE, 45 R_ARM_SBREL, 46 R_GOT, 47 // The expression is used for IFUNC support. Evaluates to GOT entry, 48 // containing redirection to the IPLT. 49 R_GOT_PLT, 50 R_GOTONLY_PC, 51 R_GOTONLY_PC_FROM_END, 52 R_GOTREL, 53 R_GOTREL_FROM_END, 54 R_GOT_FROM_END, 55 R_GOT_OFF, 56 R_GOT_PC, 57 R_HEXAGON_GOT, 58 R_HINT, 59 R_MIPS_GOTREL, 60 R_MIPS_GOT_GP, 61 R_MIPS_GOT_GP_PC, 62 R_MIPS_GOT_LOCAL_PAGE, 63 R_MIPS_GOT_OFF, 64 R_MIPS_GOT_OFF32, 65 R_MIPS_TLSGD, 66 R_MIPS_TLSLD, 67 R_NEG_TLS, 68 R_NONE, 69 R_PC, 70 R_PLT, 71 R_PLT_PC, 72 R_PPC_CALL, 73 R_PPC_CALL_PLT, 74 R_PPC_TOC, 75 R_RELAX_GOT_PC, 76 R_RELAX_GOT_PC_NOPIC, 77 R_RELAX_TLS_GD_TO_IE, 78 R_RELAX_TLS_GD_TO_IE_ABS, 79 R_RELAX_TLS_GD_TO_IE_END, 80 R_RELAX_TLS_GD_TO_IE_GOT_OFF, 81 R_RELAX_TLS_GD_TO_LE, 82 R_RELAX_TLS_GD_TO_LE_NEG, 83 R_RELAX_TLS_IE_TO_LE, 84 R_RELAX_TLS_LD_TO_LE, 85 R_RELAX_TLS_LD_TO_LE_ABS, 86 R_RISCV_PC_INDIRECT, 87 R_SIZE, 88 R_TLS, 89 R_TLSDESC, 90 R_TLSDESC_CALL, 91 R_TLSGD_GOT, 92 R_TLSGD_GOT_FROM_END, 93 R_TLSGD_PC, 94 R_TLSIE_HINT, 95 R_TLSLD_GOT, 96 R_TLSLD_GOT_FROM_END, 97 R_TLSLD_GOT_OFF, 98 R_TLSLD_HINT, 99 R_TLSLD_PC, 100 }; 101 102 // Build a bitmask with one bit set for each RelExpr. 103 // 104 // Constexpr function arguments can't be used in static asserts, so we 105 // use template arguments to build the mask. 106 // But function template partial specializations don't exist (needed 107 // for base case of the recursion), so we need a dummy struct. 108 template <RelExpr... Exprs> struct RelExprMaskBuilder { buildRelExprMaskBuilder109 static inline uint64_t build() { return 0; } 110 }; 111 112 // Specialization for recursive case. 113 template <RelExpr Head, RelExpr... Tail> 114 struct RelExprMaskBuilder<Head, Tail...> { 115 static inline uint64_t build() { 116 static_assert(0 <= Head && Head < 64, 117 "RelExpr is too large for 64-bit mask!"); 118 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build(); 119 } 120 }; 121 122 // Return true if `Expr` is one of `Exprs`. 123 // There are fewer than 64 RelExpr's, so we can represent any set of 124 // RelExpr's as a constant bit mask and test for membership with a 125 // couple cheap bitwise operations. 126 template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) { 127 assert(0 <= Expr && (int)Expr < 64 && 128 "RelExpr is too large for 64-bit mask!"); 129 return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build(); 130 } 131 132 // Architecture-neutral representation of relocation. 133 struct Relocation { 134 RelExpr Expr; 135 RelType Type; 136 uint64_t Offset; 137 int64_t Addend; 138 Symbol *Sym; 139 }; 140 141 struct RelocationOffsetComparator { 142 bool operator()(const Relocation &Lhs, const Relocation &Rhs) { 143 return Lhs.Offset < Rhs.Offset; 144 } 145 146 // For std::lower_bound, std::upper_bound, std::equal_range. 147 bool operator()(const Relocation &Rel, uint64_t Val) { 148 return Rel.Offset < Val; 149 } 150 151 bool operator()(uint64_t Val, const Relocation &Rel) { 152 return Val < Rel.Offset; 153 } 154 }; 155 156 template <class ELFT> void scanRelocations(InputSectionBase &); 157 158 class ThunkSection; 159 class Thunk; 160 struct InputSectionDescription; 161 162 class ThunkCreator { 163 public: 164 // Return true if Thunks have been added to OutputSections 165 bool createThunks(ArrayRef<OutputSection *> OutputSections); 166 167 // The number of completed passes of createThunks this permits us 168 // to do one time initialization on Pass 0 and put a limit on the 169 // number of times it can be called to prevent infinite loops. 170 uint32_t Pass = 0; 171 172 private: 173 void mergeThunks(ArrayRef<OutputSection *> OutputSections); 174 175 ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS, 176 InputSectionDescription *ISD, uint32_t Type, 177 uint64_t Src); 178 179 ThunkSection *getISThunkSec(InputSection *IS); 180 181 void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections); 182 183 std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src); 184 185 ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *, 186 uint64_t Off); 187 188 bool normalizeExistingThunk(Relocation &Rel, uint64_t Src); 189 190 // Record all the available Thunks for a Symbol 191 llvm::DenseMap<std::pair<SectionBase *, uint64_t>, std::vector<Thunk *>> 192 ThunkedSymbolsBySection; 193 llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols; 194 195 // Find a Thunk from the Thunks symbol definition, we can use this to find 196 // the Thunk from a relocation to the Thunks symbol definition. 197 llvm::DenseMap<Symbol *, Thunk *> Thunks; 198 199 // Track InputSections that have an inline ThunkSection placed in front 200 // an inline ThunkSection may have control fall through to the section below 201 // so we need to make sure that there is only one of them. 202 // The Mips LA25 Thunk is an example of an inline ThunkSection. 203 llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections; 204 }; 205 206 // Return a int64_t to make sure we get the sign extension out of the way as 207 // early as possible. 208 template <class ELFT> 209 static inline int64_t getAddend(const typename ELFT::Rel &Rel) { 210 return 0; 211 } 212 template <class ELFT> 213 static inline int64_t getAddend(const typename ELFT::Rela &Rel) { 214 return Rel.r_addend; 215 } 216 } // namespace elf 217 } // namespace lld 218 219 #endif 220