blob: fde25a230b72e6251a5a487b39443932a9f7acdf [file] [log] [blame]
Rui Ueyama0fcdc732016-05-24 20:24:431//===- Relocations.h -------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Rui Ueyama0fcdc732016-05-24 20:24:436//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_ELF_RELOCATIONS_H
10#define LLD_ELF_RELOCATIONS_H
11
Rui Ueyama3f851702017-10-02 21:00:4112#include "lld/Common/LLVM.h"
Peter Smith56abf202017-04-05 10:30:0913#include "llvm/ADT/DenseMap.h"
serge-sans-paille5f290c02022-01-24 10:57:0914#include "llvm/ADT/STLExtras.h"
Fangrui Song0af07c02024-08-01 17:22:0315#include "llvm/Object/ELFTypes.h"
Peter Smith56abf202017-04-05 10:30:0916#include <vector>
Rui Ueyama0fcdc732016-05-24 20:24:4317
Nico Weber87248ba2022-08-10 19:31:5818namespace lld::elf {
Fangrui Songa94060c2024-09-28 07:26:2319struct Ctx;
Peter Smithc4d9cd82024-10-01 12:12:2920class Defined;
Rui Ueyamaf52496e2017-11-03 21:21:4721class Symbol;
Rafael Espindola774ea7d2017-02-23 16:49:0722class InputSection;
Rafael Espindolab4c9b812017-02-23 02:28:2823class InputSectionBase;
Rafael Espindola24e6f362017-02-24 15:07:3024class OutputSection;
pcc970d6d22024-12-05 01:35:0525class RelocationBaseSection;
Peter Collingbourne04ff1222018-03-09 17:54:4326class SectionBase;
Rui Ueyama0fcdc732016-05-24 20:24:4327
Rui Ueyama67533a22017-10-11 22:49:2428// Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
Fangrui Songc1a6def2024-11-17 04:26:3329struct RelType {
30 uint32_t v = 0;
31 /*implicit*/ constexpr RelType(uint32_t v = 0) : v(v) {}
32 /*implicit*/ operator uint32_t() const { return v; }
33};
34
Sriraman Tallam94317872020-04-07 13:48:1835using JumpModType = uint32_t;
Rui Ueyama67533a22017-10-11 22:49:2436
Rui Ueyama865d9862016-10-21 04:52:1137// List of target-independent relocation types. Relocations read
38// from files are converted to these types so that the main code
39// doesn't have to know about architecture-specific details.
Rui Ueyama0fcdc732016-05-24 20:24:4340enum RelExpr {
41 R_ABS,
Alexander Richardsoncfb60932018-02-16 10:01:1742 R_ADDEND,
Fangrui Songbc4b1592019-04-22 03:10:4043 R_DTPREL,
Rui Ueyama0fcdc732016-05-24 20:24:4344 R_GOT,
Rui Ueyama0fcdc732016-05-24 20:24:4345 R_GOT_OFF,
Rui Ueyama0fcdc732016-05-24 20:24:4346 R_GOT_PC,
Fangrui Song210949a2019-03-25 23:46:1947 R_GOTONLY_PC,
48 R_GOTPLTONLY_PC,
49 R_GOTPLT,
50 R_GOTPLTREL,
51 R_GOTREL,
Ulrich Weigandfe3406e2024-02-13 10:29:2152 R_GOTPLT_GOTREL,
53 R_GOTPLT_PC,
Petr Hosekb27bb592017-02-23 06:22:2854 R_NONE,
Rui Ueyama0fcdc732016-05-24 20:24:4355 R_PC,
56 R_PLT,
Rui Ueyamaff524bf2017-03-26 04:10:4357 R_PLT_PC,
Fangrui Songca8105b2021-10-25 20:05:1758 R_PLT_GOTPLT,
Ulrich Weigandfe3406e2024-02-13 10:29:2159 R_PLT_GOTREL,
Fangrui Song6611d582022-07-07 17:16:0960 R_RELAX_HINT,
George Rimar5c33b912016-05-25 14:31:3761 R_RELAX_GOT_PC,
George Rimarf10c8292016-06-01 16:45:3062 R_RELAX_GOT_PC_NOPIC,
Rui Ueyama0fcdc732016-05-24 20:24:4363 R_RELAX_TLS_GD_TO_IE,
Rafael Espindolae1979ae2016-06-04 23:33:3164 R_RELAX_TLS_GD_TO_IE_ABS,
Sean Fertilef60cb342018-06-26 19:38:1865 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
Fangrui Songe1f31912019-04-22 02:48:3766 R_RELAX_TLS_GD_TO_IE_GOTPLT,
Rui Ueyama0fcdc732016-05-24 20:24:4367 R_RELAX_TLS_GD_TO_LE,
Rafael Espindola69f54022016-06-04 23:22:3468 R_RELAX_TLS_GD_TO_LE_NEG,
Rui Ueyama0fcdc732016-05-24 20:24:4369 R_RELAX_TLS_IE_TO_LE,
70 R_RELAX_TLS_LD_TO_LE,
Zaara Syeda75c348a2018-07-09 16:35:5171 R_RELAX_TLS_LD_TO_LE_ABS,
Rui Ueyama0fcdc732016-05-24 20:24:4372 R_SIZE,
Fangrui Song22c1bd52020-12-18 16:24:4273 R_TPREL,
74 R_TPREL_NEG,
Rafael Espindolae37d13b2016-06-02 19:49:5375 R_TLSDESC,
Peter Smithd6486032016-10-20 09:59:2676 R_TLSDESC_CALL,
Fangrui Song71932242019-05-29 02:03:5677 R_TLSDESC_PC,
Fangrui Songe39c1382021-10-29 00:52:0378 R_TLSDESC_GOTPLT,
Sean Fertileef0f7492018-05-29 14:34:3879 R_TLSGD_GOT,
Fangrui Song210949a2019-03-25 23:46:1980 R_TLSGD_GOTPLT,
Rui Ueyama0fcdc732016-05-24 20:24:4381 R_TLSGD_PC,
Zaara Syeda662d1462018-08-21 15:13:5382 R_TLSIE_HINT,
Sean Fertile1a8343f2018-05-31 18:44:1283 R_TLSLD_GOT,
Fangrui Song210949a2019-03-25 23:46:1984 R_TLSLD_GOTPLT,
Zaara Syedade54f582018-06-27 13:55:4185 R_TLSLD_GOT_OFF,
Zaara Syeda75c348a2018-07-09 16:35:5186 R_TLSLD_HINT,
Rui Ueyama865d9862016-10-21 04:52:1187 R_TLSLD_PC,
Rui Ueyamaf69bbbb2019-02-14 18:50:5988
89 // The following is abstract relocation types used for only one target.
90 //
91 // Even though RelExpr is intended to be a target-neutral representation
92 // of a relocation type, there are some relocations whose semantics are
Fangrui Song04996a22024-12-03 17:17:1793 // unique to a target. Such relocation are marked with RE_<TARGET_NAME>.
94 RE_AARCH64_GOT_PAGE_PC,
Daniil Kovalev417d2d72024-12-17 07:23:0195 RE_AARCH64_AUTH_GOT_PAGE_PC,
Fangrui Song04996a22024-12-03 17:17:1796 RE_AARCH64_GOT_PAGE,
Daniil Kovalev417d2d72024-12-17 07:23:0197 RE_AARCH64_AUTH_GOT,
Daniil Kovalev1ef5b982024-12-18 06:41:5498 RE_AARCH64_AUTH_GOT_PC,
Fangrui Song04996a22024-12-03 17:17:1799 RE_AARCH64_PAGE_PC,
100 RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
101 RE_AARCH64_TLSDESC_PAGE,
102 RE_AARCH64_AUTH,
103 RE_ARM_PCA,
104 RE_ARM_SBREL,
105 RE_MIPS_GOTREL,
106 RE_MIPS_GOT_GP,
107 RE_MIPS_GOT_GP_PC,
108 RE_MIPS_GOT_LOCAL_PAGE,
109 RE_MIPS_GOT_OFF,
110 RE_MIPS_GOT_OFF32,
111 RE_MIPS_TLSGD,
112 RE_MIPS_TLSLD,
113 RE_PPC32_PLTREL,
114 RE_PPC64_CALL,
115 RE_PPC64_CALL_PLT,
116 RE_PPC64_RELAX_TOC,
117 RE_PPC64_TOCBASE,
118 RE_PPC64_RELAX_GOT_PC,
119 RE_RISCV_ADD,
120 RE_RISCV_LEB128,
121 RE_RISCV_PC_INDIRECT,
WANG Xuerui6084ee72023-07-25 09:03:28122 // Same as R_PC but with page-aligned semantics.
Fangrui Song04996a22024-12-03 17:17:17123 RE_LOONGARCH_PAGE_PC,
WANG Xuerui6084ee72023-07-25 09:03:28124 // Same as R_PLT_PC but with page-aligned semantics.
Fangrui Song04996a22024-12-03 17:17:17125 RE_LOONGARCH_PLT_PAGE_PC,
WANG Xuerui6084ee72023-07-25 09:03:28126 // In addition to having page-aligned semantics, LoongArch GOT relocs are
127 // also reused for TLS, making the semantics differ from other architectures.
Fangrui Song04996a22024-12-03 17:17:17128 RE_LOONGARCH_GOT,
129 RE_LOONGARCH_GOT_PAGE_PC,
130 RE_LOONGARCH_TLSGD_PAGE_PC,
131 RE_LOONGARCH_TLSDESC_PAGE_PC,
Rui Ueyama0fcdc732016-05-24 20:24:43132};
133
Rui Ueyama865d9862016-10-21 04:52:11134// Architecture-neutral representation of relocation.
Rafael Espindola664c6522016-09-07 20:37:34135struct Relocation {
Rui Ueyama3837f422019-07-10 05:00:37136 RelExpr expr;
137 RelType type;
138 uint64_t offset;
139 int64_t addend;
140 Symbol *sym;
Rui Ueyama0fcdc732016-05-24 20:24:43141};
142
Sriraman Tallam94317872020-04-07 13:48:18143// Manipulate jump instructions with these modifiers. These are used to relax
144// jump instruction opcodes at basic block boundaries and are particularly
145// useful when basic block sections are enabled.
146struct JumpInstrMod {
Sriraman Tallam94317872020-04-07 13:48:18147 uint64_t offset;
Fangrui Songe90c8c02021-12-27 06:17:30148 JumpModType original;
Sriraman Tallam94317872020-04-07 13:48:18149 unsigned size;
150};
151
Nico Weber2c450432019-06-20 18:25:57152// This function writes undefined symbol diagnostics to an internal buffer.
153// Call reportUndefinedSymbols() after calling scanRelocations() to emit
154// the diagnostics.
Fangrui Songa94060c2024-09-28 07:26:23155template <class ELFT> void scanRelocations(Ctx &ctx);
Fangrui Song29783f72024-09-29 02:17:18156template <class ELFT> void checkNoCrossRefs(Ctx &ctx);
Fangrui Songc490d342024-09-29 23:15:32157void reportUndefinedSymbols(Ctx &);
Fangrui Songa94060c2024-09-28 07:26:23158void postScanRelocations(Ctx &ctx);
Fangrui Song29783f72024-09-29 02:17:18159void addGotEntry(Ctx &ctx, Symbol &sym);
Rui Ueyamac98e4d82016-07-02 08:50:03160
Fangrui Song29783f72024-09-29 02:17:18161void hexagonTLSSymbolUpdate(Ctx &ctx);
Sid Manning5a5a0752020-02-10 23:27:53162bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
163
Peter Smith56abf202017-04-05 10:30:09164class ThunkSection;
165class Thunk;
Andrew Ng77152a62020-09-09 09:48:21166class InputSectionDescription;
Peter Smith56abf202017-04-05 10:30:09167
George Rimarec84ffc2017-05-17 07:10:59168class ThunkCreator {
Peter Smith56abf202017-04-05 10:30:09169public:
Fangrui Song37e39662024-11-20 07:16:35170 // Thunk may be incomplete. Avoid inline ctor/dtor.
171 ThunkCreator(Ctx &ctx);
172 ~ThunkCreator();
Peter Smith56abf202017-04-05 10:30:09173 // Return true if Thunks have been added to OutputSections
Fangrui Song6611d582022-07-07 17:16:09174 bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);
Peter Smith32980272017-06-16 13:10:08175
Peter Smith56abf202017-04-05 10:30:09176private:
Rui Ueyama3837f422019-07-10 05:00:37177 void mergeThunks(ArrayRef<OutputSection *> outputSections);
Peter Smith75030b6d2017-10-27 09:04:11178
Rui Ueyama3837f422019-07-10 05:00:37179 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
Peter Smithe35929e2021-02-26 13:14:21180 InputSectionDescription *isd,
181 const Relocation &rel, uint64_t src);
Peter Smith75030b6d2017-10-27 09:04:11182
Rui Ueyama3837f422019-07-10 05:00:37183 ThunkSection *getISThunkSec(InputSection *isec);
Peter Smith4a8e1152017-10-27 08:56:20184
Rui Ueyama3837f422019-07-10 05:00:37185 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
Peter Smithf0c70f82017-10-27 08:58:28186
Rui Ueyama3837f422019-07-10 05:00:37187 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
188 uint64_t src);
Peter Smithf0c70f82017-10-27 08:58:28189
Peter Smithc4d9cd82024-10-01 12:12:29190 std::pair<Thunk *, bool> getSyntheticLandingPad(Defined &d, int64_t a);
191
Rui Ueyama3837f422019-07-10 05:00:37192 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
193 uint64_t off);
Peter Smithf0c70f82017-10-27 08:58:28194
Rui Ueyama3837f422019-07-10 05:00:37195 bool normalizeExistingThunk(Relocation &rel, uint64_t src);
Peter Smith6c9df3f2017-10-27 09:07:10196
Peter Smith098b0d12024-11-15 18:18:18197 bool addSyntheticLandingPads();
198
Fangrui Songa94060c2024-09-28 07:26:23199 Ctx &ctx;
200
Fangrui Songbf535ac2019-11-23 08:57:54201 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
202 // is represented as a (section, offset) pair. There may be multiple
203 // relocations sharing the same (section, offset + addend) pair. We may revert
204 // a relocation back to its original non-Thunk target, and restore the
205 // original addend, so we cannot fold offset + addend. A nested pair is used
206 // because DenseMapInfo is not specialized for std::tuple.
207 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
Fangrui Song37e39662024-11-20 07:16:35208 SmallVector<std::unique_ptr<Thunk>, 0>>
Fangrui Songbf535ac2019-11-23 08:57:54209 thunkedSymbolsBySectionAndAddend;
Fangrui Song37e39662024-11-20 07:16:35210 llvm::DenseMap<std::pair<Symbol *, int64_t>,
211 SmallVector<std::unique_ptr<Thunk>, 0>>
Fangrui Songbf535ac2019-11-23 08:57:54212 thunkedSymbols;
Peter Smith56abf202017-04-05 10:30:09213
Peter Smith32980272017-06-16 13:10:08214 // Find a Thunk from the Thunks symbol definition, we can use this to find
215 // the Thunk from a relocation to the Thunks symbol definition.
Rui Ueyama3837f422019-07-10 05:00:37216 llvm::DenseMap<Symbol *, Thunk *> thunks;
Peter Smith32980272017-06-16 13:10:08217
Peter Smithfa237642017-07-05 09:53:33218 // Track InputSections that have an inline ThunkSection placed in front
219 // an inline ThunkSection may have control fall through to the section below
220 // so we need to make sure that there is only one of them.
Peter Smithc4d9cd82024-10-01 12:12:29221 // The Mips LA25 Thunk is an example of an inline ThunkSection, as is
222 // the AArch64BTLandingPadThunk.
Rui Ueyama3837f422019-07-10 05:00:37223 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
Fangrui Song6611d582022-07-07 17:16:09224
Peter Smithc4d9cd82024-10-01 12:12:29225 // Record landing pads, generated for a section + offset destination.
226 // Landling pads are alternative entry points for destinations that need
227 // to be reached via thunks that use indirect branches. A destination
228 // needs at most one landing pad as that can be reused by all callers.
229 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
Fangrui Song37e39662024-11-20 07:16:35230 std::unique_ptr<Thunk>>
Peter Smithc4d9cd82024-10-01 12:12:29231 landingPadsBySectionAndAddend;
232
Peter Smith098b0d12024-11-15 18:18:18233 // All the nonLandingPad thunks that have been created, in order of creation.
234 std::vector<Thunk *> allThunks;
235
Fangrui Song6611d582022-07-07 17:16:09236 // The number of completed passes of createThunks this permits us
237 // to do one time initialization on Pass 0 and put a limit on the
238 // number of times it can be called to prevent infinite loops.
239 uint32_t pass = 0;
Peter Smith56abf202017-04-05 10:30:09240};
Rafael Espindola0f7ceda2016-07-20 17:58:07241
Fangrui Song0af07c02024-08-01 17:22:03242// Decode LEB128 without error checking. Only used by performance critical code
243// like RelocsCrel.
244inline uint64_t readLEB128(const uint8_t *&p, uint64_t leb) {
245 uint64_t acc = 0, shift = 0, byte;
246 do {
247 byte = *p++;
248 acc |= (byte - 128 * (byte >= leb)) << shift;
249 shift += 7;
250 } while (byte >= 128);
251 return acc;
252}
253inline uint64_t readULEB128(const uint8_t *&p) { return readLEB128(p, 128); }
254inline int64_t readSLEB128(const uint8_t *&p) { return readLEB128(p, 64); }
255
256// This class implements a CREL iterator that does not allocate extra memory.
257template <bool is64> struct RelocsCrel {
258 using uint = std::conditional_t<is64, uint64_t, uint32_t>;
259 struct const_iterator {
260 using iterator_category = std::forward_iterator_tag;
261 using value_type = llvm::object::Elf_Crel_Impl<is64>;
262 using difference_type = ptrdiff_t;
263 using pointer = value_type *;
264 using reference = const value_type &;
265 uint32_t count;
266 uint8_t flagBits, shift;
267 const uint8_t *p;
268 llvm::object::Elf_Crel_Impl<is64> crel{};
269 const_iterator(size_t hdr, const uint8_t *p)
270 : count(hdr / 8), flagBits(hdr & 4 ? 3 : 2), shift(hdr % 4), p(p) {
271 if (count)
272 step();
273 }
274 void step() {
275 // See object::decodeCrel.
276 const uint8_t b = *p++;
277 crel.r_offset += b >> flagBits << shift;
278 if (b >= 0x80)
279 crel.r_offset +=
280 ((readULEB128(p) << (7 - flagBits)) - (0x80 >> flagBits)) << shift;
281 if (b & 1)
282 crel.r_symidx += readSLEB128(p);
283 if (b & 2)
284 crel.r_type += readSLEB128(p);
285 if (b & 4 && flagBits == 3)
286 crel.r_addend += static_cast<uint>(readSLEB128(p));
287 }
288 llvm::object::Elf_Crel_Impl<is64> operator*() const { return crel; };
289 const llvm::object::Elf_Crel_Impl<is64> *operator->() const {
290 return &crel;
291 }
292 // For llvm::enumerate.
293 bool operator==(const const_iterator &r) const { return count == r.count; }
294 bool operator!=(const const_iterator &r) const { return count != r.count; }
295 const_iterator &operator++() {
296 if (--count)
297 step();
298 return *this;
299 }
300 // For RelocationScanner::scanOne.
301 void operator+=(size_t n) {
302 for (; n; --n)
303 operator++();
304 }
305 };
306
307 size_t hdr = 0;
308 const uint8_t *p = nullptr;
309
310 constexpr RelocsCrel() = default;
311 RelocsCrel(const uint8_t *p) : hdr(readULEB128(p)) { this->p = p; }
312 size_t size() const { return hdr / 8; }
313 const_iterator begin() const { return {hdr, p}; }
314 const_iterator end() const { return {0, nullptr}; }
315};
316
Fangrui Song6efc3772024-07-27 17:55:17317template <class RelTy> struct Relocs : ArrayRef<RelTy> {
318 Relocs() = default;
319 Relocs(ArrayRef<RelTy> a) : ArrayRef<RelTy>(a) {}
320};
321
Fangrui Song0af07c02024-08-01 17:22:03322template <bool is64>
323struct Relocs<llvm::object::Elf_Crel_Impl<is64>> : RelocsCrel<is64> {
324 using RelocsCrel<is64>::RelocsCrel;
325};
326
Rafael Espindola7386cea2017-02-16 00:12:34327// Return a int64_t to make sure we get the sign extension out of the way as
328// early as possible.
Rafael Espindola0f7ceda2016-07-20 17:58:07329template <class ELFT>
Rui Ueyama3837f422019-07-10 05:00:37330static inline int64_t getAddend(const typename ELFT::Rel &rel) {
Rui Ueyamac98e4d82016-07-02 08:50:03331 return 0;
332}
Rui Ueyamac98e4d82016-07-02 08:50:03333template <class ELFT>
Rui Ueyama3837f422019-07-10 05:00:37334static inline int64_t getAddend(const typename ELFT::Rela &rel) {
335 return rel.r_addend;
Rui Ueyamac98e4d82016-07-02 08:50:03336}
Fangrui Song0af07c02024-08-01 17:22:03337template <class ELFT>
338static inline int64_t getAddend(const typename ELFT::Crel &rel) {
339 return rel.r_addend;
340}
Fangrui Songc9b1bd102021-04-29 15:51:09341
342template <typename RelTy>
Fangrui Song6efc3772024-07-27 17:55:17343inline Relocs<RelTy> sortRels(Relocs<RelTy> rels,
344 SmallVector<RelTy, 0> &storage) {
Fangrui Songc9b1bd102021-04-29 15:51:09345 auto cmp = [](const RelTy &a, const RelTy &b) {
346 return a.r_offset < b.r_offset;
347 };
348 if (!llvm::is_sorted(rels, cmp)) {
349 storage.assign(rels.begin(), rels.end());
350 llvm::stable_sort(storage, cmp);
Fangrui Song6efc3772024-07-27 17:55:17351 rels = Relocs<RelTy>(storage);
Fangrui Songc9b1bd102021-04-29 15:51:09352 }
353 return rels;
354}
Mitch Phillipsca35a192023-07-31 15:07:26355
Fangrui Song0af07c02024-08-01 17:22:03356template <bool is64>
357inline Relocs<llvm::object::Elf_Crel_Impl<is64>>
358sortRels(Relocs<llvm::object::Elf_Crel_Impl<is64>> rels,
359 SmallVector<llvm::object::Elf_Crel_Impl<is64>, 0> &storage) {
360 return {};
361}
362
pcc970d6d22024-12-05 01:35:05363RelocationBaseSection &getIRelativeSection(Ctx &ctx);
364
Mitch Phillipsca35a192023-07-31 15:07:26365// Returns true if Expr refers a GOT entry. Note that this function returns
366// false for TLS variables even though they need GOT, because TLS variables uses
367// GOT differently than the regular variables.
368bool needsGot(RelExpr expr);
Nico Weber87248ba2022-08-10 19:31:58369} // namespace lld::elf
Rui Ueyama0fcdc732016-05-24 20:24:43370
371#endif