1 //===- Relocations.h -------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_ELF_RELOCATIONS_H 10 #define LLD_ELF_RELOCATIONS_H 11 12 #include "lld/Common/LLVM.h" 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/ADT/STLExtras.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 using RelType = uint32_t; 28 using JumpModType = uint32_t; 29 30 // List of target-independent relocation types. Relocations read 31 // from files are converted to these types so that the main code 32 // doesn't have to know about architecture-specific details. 33 enum RelExpr { 34 R_ABS, 35 R_ADDEND, 36 R_DTPREL, 37 R_GOT, 38 R_GOT_OFF, 39 R_GOT_PC, 40 R_GOTONLY_PC, 41 R_GOTPLTONLY_PC, 42 R_GOTPLT, 43 R_GOTPLTREL, 44 R_GOTREL, 45 R_NONE, 46 R_PC, 47 R_PLT, 48 R_PLT_PC, 49 R_PLT_GOTPLT, 50 R_RELAX_GOT_PC, 51 R_RELAX_GOT_PC_NOPIC, 52 R_RELAX_TLS_GD_TO_IE, 53 R_RELAX_TLS_GD_TO_IE_ABS, 54 R_RELAX_TLS_GD_TO_IE_GOT_OFF, 55 R_RELAX_TLS_GD_TO_IE_GOTPLT, 56 R_RELAX_TLS_GD_TO_LE, 57 R_RELAX_TLS_GD_TO_LE_NEG, 58 R_RELAX_TLS_IE_TO_LE, 59 R_RELAX_TLS_LD_TO_LE, 60 R_RELAX_TLS_LD_TO_LE_ABS, 61 R_SIZE, 62 R_TPREL, 63 R_TPREL_NEG, 64 R_TLSDESC, 65 R_TLSDESC_CALL, 66 R_TLSDESC_PC, 67 R_TLSDESC_GOTPLT, 68 R_TLSGD_GOT, 69 R_TLSGD_GOTPLT, 70 R_TLSGD_PC, 71 R_TLSIE_HINT, 72 R_TLSLD_GOT, 73 R_TLSLD_GOTPLT, 74 R_TLSLD_GOT_OFF, 75 R_TLSLD_HINT, 76 R_TLSLD_PC, 77 78 // The following is abstract relocation types used for only one target. 79 // 80 // Even though RelExpr is intended to be a target-neutral representation 81 // of a relocation type, there are some relocations whose semantics are 82 // unique to a target. Such relocation are marked with R_<TARGET_NAME>. 83 R_AARCH64_GOT_PAGE_PC, 84 R_AARCH64_GOT_PAGE, 85 R_AARCH64_PAGE_PC, 86 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC, 87 R_AARCH64_TLSDESC_PAGE, 88 R_ARM_PCA, 89 R_ARM_SBREL, 90 R_MIPS_GOTREL, 91 R_MIPS_GOT_GP, 92 R_MIPS_GOT_GP_PC, 93 R_MIPS_GOT_LOCAL_PAGE, 94 R_MIPS_GOT_OFF, 95 R_MIPS_GOT_OFF32, 96 R_MIPS_TLSGD, 97 R_MIPS_TLSLD, 98 R_PPC32_PLTREL, 99 R_PPC64_CALL, 100 R_PPC64_CALL_PLT, 101 R_PPC64_RELAX_TOC, 102 R_PPC64_TOCBASE, 103 R_PPC64_RELAX_GOT_PC, 104 R_RISCV_ADD, 105 R_RISCV_PC_INDIRECT, 106 }; 107 108 // Architecture-neutral representation of relocation. 109 struct Relocation { 110 RelExpr expr; 111 RelType type; 112 uint64_t offset; 113 int64_t addend; 114 Symbol *sym; 115 }; 116 117 // Manipulate jump instructions with these modifiers. These are used to relax 118 // jump instruction opcodes at basic block boundaries and are particularly 119 // useful when basic block sections are enabled. 120 struct JumpInstrMod { 121 uint64_t offset; 122 JumpModType original; 123 unsigned size; 124 }; 125 126 // This function writes undefined symbol diagnostics to an internal buffer. 127 // Call reportUndefinedSymbols() after calling scanRelocations() to emit 128 // the diagnostics. 129 template <class ELFT> void scanRelocations(InputSectionBase &); 130 void postScanRelocations(); 131 132 template <class ELFT> void reportUndefinedSymbols(); 133 134 void hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections); 135 bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections); 136 137 class ThunkSection; 138 class Thunk; 139 class InputSectionDescription; 140 141 class ThunkCreator { 142 public: 143 // Return true if Thunks have been added to OutputSections 144 bool createThunks(ArrayRef<OutputSection *> outputSections); 145 146 // The number of completed passes of createThunks this permits us 147 // to do one time initialization on Pass 0 and put a limit on the 148 // number of times it can be called to prevent infinite loops. 149 uint32_t pass = 0; 150 151 private: 152 void mergeThunks(ArrayRef<OutputSection *> outputSections); 153 154 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec, 155 InputSectionDescription *isd, 156 const Relocation &rel, uint64_t src); 157 158 ThunkSection *getISThunkSec(InputSection *isec); 159 160 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections); 161 162 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel, 163 uint64_t src); 164 165 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *, 166 uint64_t off); 167 168 bool normalizeExistingThunk(Relocation &rel, uint64_t src); 169 170 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol 171 // is represented as a (section, offset) pair. There may be multiple 172 // relocations sharing the same (section, offset + addend) pair. We may revert 173 // a relocation back to its original non-Thunk target, and restore the 174 // original addend, so we cannot fold offset + addend. A nested pair is used 175 // because DenseMapInfo is not specialized for std::tuple. 176 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>, 177 std::vector<Thunk *>> 178 thunkedSymbolsBySectionAndAddend; 179 llvm::DenseMap<std::pair<Symbol *, int64_t>, std::vector<Thunk *>> 180 thunkedSymbols; 181 182 // Find a Thunk from the Thunks symbol definition, we can use this to find 183 // the Thunk from a relocation to the Thunks symbol definition. 184 llvm::DenseMap<Symbol *, Thunk *> thunks; 185 186 // Track InputSections that have an inline ThunkSection placed in front 187 // an inline ThunkSection may have control fall through to the section below 188 // so we need to make sure that there is only one of them. 189 // The Mips LA25 Thunk is an example of an inline ThunkSection. 190 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections; 191 }; 192 193 // Return a int64_t to make sure we get the sign extension out of the way as 194 // early as possible. 195 template <class ELFT> 196 static inline int64_t getAddend(const typename ELFT::Rel &rel) { 197 return 0; 198 } 199 template <class ELFT> 200 static inline int64_t getAddend(const typename ELFT::Rela &rel) { 201 return rel.r_addend; 202 } 203 204 template <typename RelTy> 205 ArrayRef<RelTy> sortRels(ArrayRef<RelTy> rels, SmallVector<RelTy, 0> &storage) { 206 auto cmp = [](const RelTy &a, const RelTy &b) { 207 return a.r_offset < b.r_offset; 208 }; 209 if (!llvm::is_sorted(rels, cmp)) { 210 storage.assign(rels.begin(), rels.end()); 211 llvm::stable_sort(storage, cmp); 212 rels = storage; 213 } 214 return rels; 215 } 216 } // namespace elf 217 } // namespace lld 218 219 #endif 220