[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lld / ELF / Relocations.h
blob041bd480485878ed08a51490792e66c74e111235
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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_RELOCATIONS_H
10 #define LLD_ELF_RELOCATIONS_H
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Object/ELFTypes.h"
16 #include <vector>
18 namespace lld::elf {
19 struct Ctx;
20 class Defined;
21 class Symbol;
22 class InputSection;
23 class InputSectionBase;
24 class OutputSection;
25 class SectionBase;
27 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
28 struct RelType {
29 uint32_t v = 0;
30 /*implicit*/ constexpr RelType(uint32_t v = 0) : v(v) {}
31 /*implicit*/ operator uint32_t() const { return v; }
34 using JumpModType = uint32_t;
36 // List of target-independent relocation types. Relocations read
37 // from files are converted to these types so that the main code
38 // doesn't have to know about architecture-specific details.
39 enum RelExpr {
40 R_ABS,
41 R_ADDEND,
42 R_DTPREL,
43 R_GOT,
44 R_GOT_OFF,
45 R_GOT_PC,
46 R_GOTONLY_PC,
47 R_GOTPLTONLY_PC,
48 R_GOTPLT,
49 R_GOTPLTREL,
50 R_GOTREL,
51 R_GOTPLT_GOTREL,
52 R_GOTPLT_PC,
53 R_NONE,
54 R_PC,
55 R_PLT,
56 R_PLT_PC,
57 R_PLT_GOTPLT,
58 R_PLT_GOTREL,
59 R_RELAX_HINT,
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_GOT_OFF,
65 R_RELAX_TLS_GD_TO_IE_GOTPLT,
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_RELAX_TLS_LD_TO_LE_ABS,
71 R_SIZE,
72 R_TPREL,
73 R_TPREL_NEG,
74 R_TLSDESC,
75 R_TLSDESC_CALL,
76 R_TLSDESC_PC,
77 R_TLSDESC_GOTPLT,
78 R_TLSGD_GOT,
79 R_TLSGD_GOTPLT,
80 R_TLSGD_PC,
81 R_TLSIE_HINT,
82 R_TLSLD_GOT,
83 R_TLSLD_GOTPLT,
84 R_TLSLD_GOT_OFF,
85 R_TLSLD_HINT,
86 R_TLSLD_PC,
88 // The following is abstract relocation types used for only one target.
90 // Even though RelExpr is intended to be a target-neutral representation
91 // of a relocation type, there are some relocations whose semantics are
92 // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
93 R_AARCH64_GOT_PAGE_PC,
94 R_AARCH64_GOT_PAGE,
95 R_AARCH64_PAGE_PC,
96 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
97 R_AARCH64_TLSDESC_PAGE,
98 R_AARCH64_AUTH,
99 R_ARM_PCA,
100 R_ARM_SBREL,
101 R_MIPS_GOTREL,
102 R_MIPS_GOT_GP,
103 R_MIPS_GOT_GP_PC,
104 R_MIPS_GOT_LOCAL_PAGE,
105 R_MIPS_GOT_OFF,
106 R_MIPS_GOT_OFF32,
107 R_MIPS_TLSGD,
108 R_MIPS_TLSLD,
109 R_PPC32_PLTREL,
110 R_PPC64_CALL,
111 R_PPC64_CALL_PLT,
112 R_PPC64_RELAX_TOC,
113 R_PPC64_TOCBASE,
114 R_PPC64_RELAX_GOT_PC,
115 R_RISCV_ADD,
116 R_RISCV_LEB128,
117 R_RISCV_PC_INDIRECT,
118 // Same as R_PC but with page-aligned semantics.
119 R_LOONGARCH_PAGE_PC,
120 // Same as R_PLT_PC but with page-aligned semantics.
121 R_LOONGARCH_PLT_PAGE_PC,
122 // In addition to having page-aligned semantics, LoongArch GOT relocs are
123 // also reused for TLS, making the semantics differ from other architectures.
124 R_LOONGARCH_GOT,
125 R_LOONGARCH_GOT_PAGE_PC,
126 R_LOONGARCH_TLSGD_PAGE_PC,
127 R_LOONGARCH_TLSDESC_PAGE_PC,
130 // Architecture-neutral representation of relocation.
131 struct Relocation {
132 RelExpr expr;
133 RelType type;
134 uint64_t offset;
135 int64_t addend;
136 Symbol *sym;
139 // Manipulate jump instructions with these modifiers. These are used to relax
140 // jump instruction opcodes at basic block boundaries and are particularly
141 // useful when basic block sections are enabled.
142 struct JumpInstrMod {
143 uint64_t offset;
144 JumpModType original;
145 unsigned size;
148 // This function writes undefined symbol diagnostics to an internal buffer.
149 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
150 // the diagnostics.
151 template <class ELFT> void scanRelocations(Ctx &ctx);
152 template <class ELFT> void checkNoCrossRefs(Ctx &ctx);
153 void reportUndefinedSymbols(Ctx &);
154 void postScanRelocations(Ctx &ctx);
155 void addGotEntry(Ctx &ctx, Symbol &sym);
157 void hexagonTLSSymbolUpdate(Ctx &ctx);
158 bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
160 class ThunkSection;
161 class Thunk;
162 class InputSectionDescription;
164 class ThunkCreator {
165 public:
166 // Thunk may be incomplete. Avoid inline ctor/dtor.
167 ThunkCreator(Ctx &ctx);
168 ~ThunkCreator();
169 // Return true if Thunks have been added to OutputSections
170 bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);
172 private:
173 void mergeThunks(ArrayRef<OutputSection *> outputSections);
175 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
176 InputSectionDescription *isd,
177 const Relocation &rel, uint64_t src);
179 ThunkSection *getISThunkSec(InputSection *isec);
181 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
183 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
184 uint64_t src);
186 std::pair<Thunk *, bool> getSyntheticLandingPad(Defined &d, int64_t a);
188 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
189 uint64_t off);
191 bool normalizeExistingThunk(Relocation &rel, uint64_t src);
193 bool addSyntheticLandingPads();
195 Ctx &ctx;
197 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
198 // is represented as a (section, offset) pair. There may be multiple
199 // relocations sharing the same (section, offset + addend) pair. We may revert
200 // a relocation back to its original non-Thunk target, and restore the
201 // original addend, so we cannot fold offset + addend. A nested pair is used
202 // because DenseMapInfo is not specialized for std::tuple.
203 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
204 SmallVector<std::unique_ptr<Thunk>, 0>>
205 thunkedSymbolsBySectionAndAddend;
206 llvm::DenseMap<std::pair<Symbol *, int64_t>,
207 SmallVector<std::unique_ptr<Thunk>, 0>>
208 thunkedSymbols;
210 // Find a Thunk from the Thunks symbol definition, we can use this to find
211 // the Thunk from a relocation to the Thunks symbol definition.
212 llvm::DenseMap<Symbol *, Thunk *> thunks;
214 // Track InputSections that have an inline ThunkSection placed in front
215 // an inline ThunkSection may have control fall through to the section below
216 // so we need to make sure that there is only one of them.
217 // The Mips LA25 Thunk is an example of an inline ThunkSection, as is
218 // the AArch64BTLandingPadThunk.
219 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
221 // Record landing pads, generated for a section + offset destination.
222 // Landling pads are alternative entry points for destinations that need
223 // to be reached via thunks that use indirect branches. A destination
224 // needs at most one landing pad as that can be reused by all callers.
225 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
226 std::unique_ptr<Thunk>>
227 landingPadsBySectionAndAddend;
229 // All the nonLandingPad thunks that have been created, in order of creation.
230 std::vector<Thunk *> allThunks;
232 // The number of completed passes of createThunks this permits us
233 // to do one time initialization on Pass 0 and put a limit on the
234 // number of times it can be called to prevent infinite loops.
235 uint32_t pass = 0;
238 // Decode LEB128 without error checking. Only used by performance critical code
239 // like RelocsCrel.
240 inline uint64_t readLEB128(const uint8_t *&p, uint64_t leb) {
241 uint64_t acc = 0, shift = 0, byte;
242 do {
243 byte = *p++;
244 acc |= (byte - 128 * (byte >= leb)) << shift;
245 shift += 7;
246 } while (byte >= 128);
247 return acc;
249 inline uint64_t readULEB128(const uint8_t *&p) { return readLEB128(p, 128); }
250 inline int64_t readSLEB128(const uint8_t *&p) { return readLEB128(p, 64); }
252 // This class implements a CREL iterator that does not allocate extra memory.
253 template <bool is64> struct RelocsCrel {
254 using uint = std::conditional_t<is64, uint64_t, uint32_t>;
255 struct const_iterator {
256 using iterator_category = std::forward_iterator_tag;
257 using value_type = llvm::object::Elf_Crel_Impl<is64>;
258 using difference_type = ptrdiff_t;
259 using pointer = value_type *;
260 using reference = const value_type &;
261 uint32_t count;
262 uint8_t flagBits, shift;
263 const uint8_t *p;
264 llvm::object::Elf_Crel_Impl<is64> crel{};
265 const_iterator(size_t hdr, const uint8_t *p)
266 : count(hdr / 8), flagBits(hdr & 4 ? 3 : 2), shift(hdr % 4), p(p) {
267 if (count)
268 step();
270 void step() {
271 // See object::decodeCrel.
272 const uint8_t b = *p++;
273 crel.r_offset += b >> flagBits << shift;
274 if (b >= 0x80)
275 crel.r_offset +=
276 ((readULEB128(p) << (7 - flagBits)) - (0x80 >> flagBits)) << shift;
277 if (b & 1)
278 crel.r_symidx += readSLEB128(p);
279 if (b & 2)
280 crel.r_type += readSLEB128(p);
281 if (b & 4 && flagBits == 3)
282 crel.r_addend += static_cast<uint>(readSLEB128(p));
284 llvm::object::Elf_Crel_Impl<is64> operator*() const { return crel; };
285 const llvm::object::Elf_Crel_Impl<is64> *operator->() const {
286 return &crel;
288 // For llvm::enumerate.
289 bool operator==(const const_iterator &r) const { return count == r.count; }
290 bool operator!=(const const_iterator &r) const { return count != r.count; }
291 const_iterator &operator++() {
292 if (--count)
293 step();
294 return *this;
296 // For RelocationScanner::scanOne.
297 void operator+=(size_t n) {
298 for (; n; --n)
299 operator++();
303 size_t hdr = 0;
304 const uint8_t *p = nullptr;
306 constexpr RelocsCrel() = default;
307 RelocsCrel(const uint8_t *p) : hdr(readULEB128(p)) { this->p = p; }
308 size_t size() const { return hdr / 8; }
309 const_iterator begin() const { return {hdr, p}; }
310 const_iterator end() const { return {0, nullptr}; }
313 template <class RelTy> struct Relocs : ArrayRef<RelTy> {
314 Relocs() = default;
315 Relocs(ArrayRef<RelTy> a) : ArrayRef<RelTy>(a) {}
318 template <bool is64>
319 struct Relocs<llvm::object::Elf_Crel_Impl<is64>> : RelocsCrel<is64> {
320 using RelocsCrel<is64>::RelocsCrel;
323 // Return a int64_t to make sure we get the sign extension out of the way as
324 // early as possible.
325 template <class ELFT>
326 static inline int64_t getAddend(const typename ELFT::Rel &rel) {
327 return 0;
329 template <class ELFT>
330 static inline int64_t getAddend(const typename ELFT::Rela &rel) {
331 return rel.r_addend;
333 template <class ELFT>
334 static inline int64_t getAddend(const typename ELFT::Crel &rel) {
335 return rel.r_addend;
338 template <typename RelTy>
339 inline Relocs<RelTy> sortRels(Relocs<RelTy> rels,
340 SmallVector<RelTy, 0> &storage) {
341 auto cmp = [](const RelTy &a, const RelTy &b) {
342 return a.r_offset < b.r_offset;
344 if (!llvm::is_sorted(rels, cmp)) {
345 storage.assign(rels.begin(), rels.end());
346 llvm::stable_sort(storage, cmp);
347 rels = Relocs<RelTy>(storage);
349 return rels;
352 template <bool is64>
353 inline Relocs<llvm::object::Elf_Crel_Impl<is64>>
354 sortRels(Relocs<llvm::object::Elf_Crel_Impl<is64>> rels,
355 SmallVector<llvm::object::Elf_Crel_Impl<is64>, 0> &storage) {
356 return {};
359 // Returns true if Expr refers a GOT entry. Note that this function returns
360 // false for TLS variables even though they need GOT, because TLS variables uses
361 // GOT differently than the regular variables.
362 bool needsGot(RelExpr expr);
363 } // namespace lld::elf
365 #endif