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 "llvm/Object/ELFTypes.h"
16#include <vector>
17
18namespace lld::elf {
19struct Ctx;
20struct ELFSyncStream;
21class Defined;
22class Undefined;
23class Symbol;
24class InputSection;
25class InputSectionBase;
26class OutputSection;
27class RelocationBaseSection;
28class SectionBase;
29
30// Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
31struct RelType {
32 uint32_t v = 0;
33 /*implicit*/ constexpr RelType(uint32_t v = 0) : v(v) {}
34 /*implicit*/ operator uint32_t() const { return v; }
35};
36
37using JumpModType = uint32_t;
38
39// List of target-independent relocation types. Relocations read
40// from files are converted to these types so that the main code
41// doesn't have to know about architecture-specific details.
42enum RelExpr {
43 R_ABS,
44 R_ADDEND,
45 R_ADDEND_NEG,
46 R_DTPREL,
47 R_GOT,
48 R_GOT_OFF,
49 R_GOT_PC,
50 R_GOTONLY_PC,
51 R_GOTPLTONLY_PC,
52 R_GOTPLT,
53 R_GOTPLTREL,
54 R_GOTREL,
55 R_GOTPLT_GOTREL,
56 R_GOTPLT_PC,
57 R_NONE,
58 R_PC,
59 R_PLT,
60 R_PLT_PC,
61 R_PLT_GOTPLT,
62 R_PLT_GOTREL,
63 R_RELAX_HINT,
64 R_RELAX_GOT_PC,
65 R_RELAX_GOT_PC_NOPIC,
66 R_RELAX_TLS_GD_TO_IE,
67 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
68 R_RELAX_TLS_GD_TO_IE_GOTPLT,
69 R_RELAX_TLS_GD_TO_LE,
70 R_RELAX_TLS_GD_TO_LE_NEG,
71 R_RELAX_TLS_IE_TO_LE,
72 R_RELAX_TLS_LD_TO_LE,
73 R_SIZE,
74 R_TPREL,
75 R_TPREL_NEG,
76 R_TLSDESC,
77 R_TLSDESC_CALL,
78 R_TLSDESC_PC,
79 R_TLSDESC_GOTPLT,
80 R_TLSGD_GOT,
81 R_TLSGD_GOTPLT,
82 R_TLSGD_PC,
83 R_TLSIE_HINT,
84 R_TLSLD_GOT,
85 R_TLSLD_GOTPLT,
86 R_TLSLD_GOT_OFF,
87 R_TLSLD_HINT,
88 R_TLSLD_PC,
89
90 // The following is abstract relocation types used for only one target.
91 //
92 // Even though RelExpr is intended to be a target-neutral representation
93 // of a relocation type, there are some relocations whose semantics are
94 // unique to a target. Such relocation are marked with RE_<TARGET_NAME>.
95 RE_AARCH64_GOT_PAGE_PC,
96 RE_AARCH64_GOT_PAGE,
97 RE_AARCH64_PAGE_PC,
98 RE_AARCH64_TLSDESC_PAGE,
99 RE_AARCH64_AUTH,
100 RE_ARM_PCA,
101 RE_ARM_SBREL,
102 RE_MIPS_GOTREL,
103 RE_MIPS_GOT_GP,
104 RE_MIPS_GOT_GP_PC,
105 RE_MIPS_GOT_LOCAL_PAGE,
106 RE_MIPS_GOT_OFF,
107 RE_MIPS_GOT_OFF32,
108 RE_MIPS_OSEC_LOCAL_PAGE,
109 RE_MIPS_TLSGD,
110 RE_MIPS_TLSLD,
111 RE_PPC32_PLTREL,
112 RE_PPC64_CALL,
113 RE_PPC64_CALL_PLT,
114 RE_PPC64_TOCBASE,
115 RE_RISCV_ADD,
116 RE_RISCV_LEB128,
117 RE_RISCV_PC_INDIRECT,
118 // Same as R_PC but with page-aligned semantics.
119 RE_LOONGARCH_PAGE_PC,
120 // Same as R_PLT_PC but with page-aligned semantics.
121 RE_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 RE_LOONGARCH_GOT,
125 RE_LOONGARCH_GOT_PAGE_PC,
126 RE_LOONGARCH_PC_INDIRECT,
127 RE_LOONGARCH_TLSGD_PAGE_PC,
128 RE_LOONGARCH_TLSDESC_PAGE_PC,
129};
130
131// Architecture-neutral representation of relocation.
132struct Relocation {
133 RelExpr expr;
134 RelType type;
135 uint64_t offset;
136 int64_t addend;
137 Symbol *sym;
138};
139
140// Manipulate jump instructions with these modifiers. These are used to relax
141// jump instruction opcodes at basic block boundaries and are particularly
142// useful when basic block sections are enabled.
143struct JumpInstrMod {
144 uint64_t offset;
145 JumpModType original;
146 unsigned size;
147};
148
149void printLocation(ELFSyncStream &s, InputSectionBase &sec, const Symbol &sym,
150 uint64_t off);
151
152// This function writes undefined symbol diagnostics to an internal buffer.
153// Call reportUndefinedSymbols() after calling scanRelocations() to emit
154// the diagnostics.
155template <class ELFT> void scanRelocations(Ctx &ctx);
156template <class ELFT> void checkNoCrossRefs(Ctx &ctx);
157void reportUndefinedSymbols(Ctx &);
158bool maybeReportUndefined(Ctx &, Undefined &sym, InputSectionBase &sec,
159 uint64_t offset);
160void postScanRelocations(Ctx &ctx);
161void addGotEntry(Ctx &ctx, Symbol &sym);
162
163bool isAbsolute(const Symbol &sym);
164
165class ThunkSection;
166class Thunk;
167class InputSectionDescription;
168
169class ThunkCreator {
170public:
171 // Thunk may be incomplete. Avoid inline ctor/dtor.
172 ThunkCreator(Ctx &ctx);
173 ~ThunkCreator();
174 // Return true if Thunks have been added to OutputSections
175 bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);
176
177private:
178 void mergeThunks(ArrayRef<OutputSection *> outputSections);
179
180 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
181 InputSectionDescription *isd,
182 const Relocation &rel, uint64_t src);
183
184 ThunkSection *getISThunkSec(InputSection *isec);
185
186 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
187
188 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
189 uint64_t src);
190
191 std::pair<Thunk *, bool> getSyntheticLandingPad(Defined &d, int64_t a);
192
193 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
194 uint64_t off, bool isPrefix = false);
195
196 bool normalizeExistingThunk(Relocation &rel, uint64_t src);
197
198 bool addSyntheticLandingPads();
199
200 Ctx &ctx;
201
202 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
203 // is represented as a (section, offset) pair. There may be multiple
204 // relocations sharing the same (section, offset + addend) pair. We may revert
205 // a relocation back to its original non-Thunk target, and restore the
206 // original addend, so we cannot fold offset + addend. A nested pair is used
207 // because DenseMapInfo is not specialized for std::tuple.
208 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
209 SmallVector<std::unique_ptr<Thunk>, 0>>
210 thunkedSymbolsBySectionAndAddend;
211 llvm::DenseMap<std::pair<Symbol *, int64_t>,
212 SmallVector<std::unique_ptr<Thunk>, 0>>
213 thunkedSymbols;
214
215 // Find a Thunk from the Thunks symbol definition, we can use this to find
216 // the Thunk from a relocation to the Thunks symbol definition.
217 llvm::DenseMap<Symbol *, Thunk *> thunks;
218
219 // Track InputSections that have an inline ThunkSection placed in front
220 // an inline ThunkSection may have control fall through to the section below
221 // so we need to make sure that there is only one of them.
222 // The Mips LA25 Thunk is an example of an inline ThunkSection, as is
223 // the AArch64BTLandingPadThunk.
224 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
225
226 // Record landing pads, generated for a section + offset destination.
227 // Landling pads are alternative entry points for destinations that need
228 // to be reached via thunks that use indirect branches. A destination
229 // needs at most one landing pad as that can be reused by all callers.
230 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
231 std::unique_ptr<Thunk>>
232 landingPadsBySectionAndAddend;
233
234 // All the nonLandingPad thunks that have been created, in order of creation.
235 std::vector<Thunk *> allThunks;
236
237 // The number of completed passes of createThunks this permits us
238 // to do one time initialization on Pass 0 and put a limit on the
239 // number of times it can be called to prevent infinite loops.
240 uint32_t pass = 0;
241};
242
243// Decode LEB128 without error checking. Only used by performance critical code
244// like RelocsCrel.
245inline uint64_t readLEB128(const uint8_t *&p, uint64_t leb) {
246 uint64_t acc = 0, shift = 0, byte;
247 do {
248 byte = *p++;
249 acc |= (byte - 128 * (byte >= leb)) << shift;
250 shift += 7;
251 } while (byte >= 128);
252 return acc;
253}
254inline uint64_t readULEB128(const uint8_t *&p) { return readLEB128(p, leb: 128); }
255inline int64_t readSLEB128(const uint8_t *&p) { return readLEB128(p, leb: 64); }
256
257// This class implements a CREL iterator that does not allocate extra memory.
258template <bool is64> struct RelocsCrel {
259 using uint = std::conditional_t<is64, uint64_t, uint32_t>;
260 struct const_iterator {
261 using iterator_category = std::forward_iterator_tag;
262 using value_type = llvm::object::Elf_Crel_Impl<is64>;
263 using difference_type = ptrdiff_t;
264 using pointer = value_type *;
265 using reference = const value_type &;
266 uint32_t count;
267 uint8_t flagBits, shift;
268 const uint8_t *p;
269 llvm::object::Elf_Crel_Impl<is64> crel{};
270 const_iterator(size_t hdr, const uint8_t *p)
271 : count(hdr / 8), flagBits(hdr & 4 ? 3 : 2), shift(hdr % 4), p(p) {
272 if (count)
273 step();
274 }
275 void step() {
276 // See object::decodeCrel.
277 const uint8_t b = *p++;
278 crel.r_offset += b >> flagBits << shift;
279 if (b >= 0x80)
280 crel.r_offset +=
281 ((readULEB128(p) << (7 - flagBits)) - (0x80 >> flagBits)) << shift;
282 if (b & 1)
283 crel.r_symidx += readSLEB128(p);
284 if (b & 2)
285 crel.r_type += readSLEB128(p);
286 if (b & 4 && flagBits == 3)
287 crel.r_addend += static_cast<uint>(readSLEB128(p));
288 }
289 llvm::object::Elf_Crel_Impl<is64> operator*() const { return crel; };
290 const llvm::object::Elf_Crel_Impl<is64> *operator->() const {
291 return &crel;
292 }
293 // For llvm::enumerate.
294 bool operator==(const const_iterator &r) const { return count == r.count; }
295 bool operator!=(const const_iterator &r) const { return count != r.count; }
296 const_iterator &operator++() {
297 if (--count)
298 step();
299 return *this;
300 }
301 // For RelocScan::scan when TLS relocations consume multiple entries.
302 void operator+=(size_t n) {
303 for (; n; --n)
304 operator++();
305 }
306 };
307
308 size_t hdr = 0;
309 const uint8_t *p = nullptr;
310
311 constexpr RelocsCrel() = default;
312 RelocsCrel(const uint8_t *p) : hdr(readULEB128(p)) { this->p = p; }
313 size_t size() const { return hdr / 8; }
314 const_iterator begin() const { return {hdr, p}; }
315 const_iterator end() const { return {0, nullptr}; }
316};
317
318template <class RelTy> struct Relocs : ArrayRef<RelTy> {
319 Relocs() = default;
320 Relocs(ArrayRef<RelTy> a) : ArrayRef<RelTy>(a) {}
321};
322
323template <bool is64>
324struct Relocs<llvm::object::Elf_Crel_Impl<is64>> : RelocsCrel<is64> {
325 using RelocsCrel<is64>::RelocsCrel;
326};
327
328// Return a int64_t to make sure we get the sign extension out of the way as
329// early as possible.
330template <class ELFT>
331static inline int64_t getAddend(const typename ELFT::Rel &rel) {
332 return 0;
333}
334template <class ELFT>
335static inline int64_t getAddend(const typename ELFT::Rela &rel) {
336 return rel.r_addend;
337}
338template <class ELFT>
339static inline int64_t getAddend(const typename ELFT::Crel &rel) {
340 return rel.r_addend;
341}
342
343RelocationBaseSection &getIRelativeSection(Ctx &ctx);
344
345// Returns true if Expr refers a GOT entry. Note that this function returns
346// false for TLS variables even though they need GOT, because TLS variables uses
347// GOT differently than the regular variables.
348bool needsGot(RelExpr expr);
349} // namespace lld::elf
350
351#endif
352