1//===- SyntheticSection.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// Synthetic sections represent chunks of linker-created data. If you
10// need to create a chunk of data that to be included in some section
11// in the result, you probably want to create that as a synthetic section.
12//
13// Synthetic sections are designed as input sections as opposed to
14// output sections because we want to allow them to be manipulated
15// using linker scripts just like other input sections from regular
16// files.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLD_ELF_SYNTHETIC_SECTIONS_H
21#define LLD_ELF_SYNTHETIC_SECTIONS_H
22
23#include "Config.h"
24#include "DWARF.h"
25#include "InputSection.h"
26#include "Symbols.h"
27#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/FoldingSet.h"
29#include "llvm/ADT/MapVector.h"
30#include "llvm/ADT/STLFunctionalExtras.h"
31#include "llvm/BinaryFormat/ELF.h"
32#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
33#include "llvm/MC/StringTableBuilder.h"
34#include "llvm/Support/Allocator.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Endian.h"
37#include "llvm/Support/Parallel.h"
38#include "llvm/Support/Threading.h"
39
40namespace lld::elf {
41class Defined;
42struct PhdrEntry;
43class SymbolTableBaseSection;
44
45struct CieRecord {
46 EhSectionPiece *cie = nullptr;
47 SmallVector<EhSectionPiece *, 0> fdes;
48};
49
50// Section for .eh_frame.
51class EhFrameSection final : public SyntheticSection {
52public:
53 EhFrameSection(Ctx &);
54 void writeTo(uint8_t *buf) override;
55 void finalizeContents() override;
56 bool isNeeded() const override { return !sections.empty(); }
57 size_t getSize() const override { return size; }
58
59 static bool classof(const SectionBase *d) {
60 return SyntheticSection::classof(sec: d) && d->name == ".eh_frame";
61 }
62
63 SmallVector<EhInputSection *, 0> sections;
64 size_t numFdes = 0;
65
66 struct FdeData {
67 uint32_t pcRel;
68 uint32_t fdeVARel;
69 };
70
71 ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; }
72 template <class ELFT>
73 void iterateFDEWithLSDA(llvm::function_ref<void(InputSection &)> fn);
74
75private:
76 // This is used only when parsing EhInputSection. We keep it here to avoid
77 // allocating one for each EhInputSection.
78 llvm::DenseMap<size_t, CieRecord *> offsetToCie;
79
80 template <llvm::endianness E> void addRecords(EhInputSection *s);
81 template <class ELFT>
82 void iterateFDEWithLSDAAux(EhInputSection &sec,
83 llvm::DenseSet<size_t> &ciesWithLSDA,
84 llvm::function_ref<void(InputSection &)> fn);
85
86 CieRecord *addCie(EhSectionPiece &piece, ArrayRef<Relocation> rels);
87 Defined *isFdeLive(EhSectionPiece &piece, ArrayRef<Relocation> rels);
88
89 uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
90
91 SmallVector<CieRecord *, 0> cieRecords;
92
93 // CIE records are uniquified by their contents and personality functions.
94 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
95};
96
97// .eh_frame_hdr contains a binary search table for .eh_frame FDEs. The section
98// is covered by a PT_GNU_EH_FRAME segment, which allows the runtime unwinder to
99// locate it via functions like `dl_iterate_phdr`.
100class EhFrameHeader final : public SyntheticSection {
101public:
102 EhFrameHeader(Ctx &);
103 void writeTo(uint8_t *buf) override;
104 size_t getSize() const override;
105 bool isNeeded() const override;
106};
107
108class GotSection final : public SyntheticSection {
109public:
110 GotSection(Ctx &);
111 size_t getSize() const override { return size; }
112 void finalizeContents() override;
113 bool isNeeded() const override;
114 void writeTo(uint8_t *buf) override;
115
116 void addConstant(const Relocation &r) { addReloc(r); }
117 void addEntry(const Symbol &sym);
118 void addAuthEntry(const Symbol &sym);
119 bool addTlsDescEntry(const Symbol &sym);
120 void addTlsDescAuthEntry();
121 bool addDynTlsEntry(const Symbol &sym);
122 bool addTlsIndex();
123 uint32_t getTlsDescOffset(const Symbol &sym) const;
124 uint64_t getTlsDescAddr(const Symbol &sym) const;
125 uint64_t getGlobalDynAddr(const Symbol &b) const;
126 uint64_t getGlobalDynOffset(const Symbol &b) const;
127
128 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
129 uint32_t getTlsIndexOff() const { return tlsIndexOff; }
130
131 // Flag to force GOT to be in output if we have relocations
132 // that relies on its address.
133 std::atomic<bool> hasGotOffRel = false;
134
135protected:
136 size_t numEntries = 0;
137 uint32_t tlsIndexOff = -1;
138 struct AuthEntryInfo {
139 size_t offset;
140 bool isSymbolFunc;
141 };
142 SmallVector<AuthEntryInfo, 0> authEntries;
143};
144
145// .note.GNU-stack section.
146class GnuStackSection : public SyntheticSection {
147public:
148 GnuStackSection(Ctx &ctx)
149 : SyntheticSection(ctx, ".note.GNU-stack", llvm::ELF::SHT_PROGBITS, 0,
150 1) {}
151 void writeTo(uint8_t *buf) override {}
152 size_t getSize() const override { return 0; }
153};
154
155class GnuPropertySection final : public SyntheticSection {
156public:
157 GnuPropertySection(Ctx &);
158 void writeTo(uint8_t *buf) override;
159 size_t getSize() const override;
160};
161
162// .note.gnu.build-id section.
163class BuildIdSection : public SyntheticSection {
164 // First 16 bytes are a header.
165 static const unsigned headerSize = 16;
166
167public:
168 const size_t hashSize;
169 BuildIdSection(Ctx &);
170 void writeTo(uint8_t *buf) override;
171 size_t getSize() const override { return headerSize + hashSize; }
172 void writeBuildId(llvm::ArrayRef<uint8_t> buf);
173
174private:
175 uint8_t *hashBuf;
176};
177
178// BssSection is used to reserve space for copy relocations and common symbols.
179// We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
180// that are used for writable symbols, read-only symbols and common symbols,
181// respectively.
182class BssSection final : public SyntheticSection {
183public:
184 BssSection(Ctx &, StringRef name, uint64_t size, uint32_t addralign);
185 void writeTo(uint8_t *) override {}
186 bool isNeeded() const override { return size != 0; }
187 size_t getSize() const override { return size; }
188
189 static bool classof(const SectionBase *s) {
190 return isa<SyntheticSection>(Val: s) && cast<SyntheticSection>(Val: s)->bss;
191 }
192};
193
194class MipsGotSection final : public SyntheticSection {
195public:
196 MipsGotSection(Ctx &);
197 void writeTo(uint8_t *buf) override;
198 size_t getSize() const override { return size; }
199 bool updateAllocSize(Ctx &) override;
200 void finalizeContents() override;
201 bool isNeeded() const override;
202
203 // Join separate GOTs built for each input file to generate
204 // primary and optional multiple secondary GOTs.
205 void build();
206
207 void addConstant(const Relocation &r);
208 void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
209 void addDynTlsEntry(InputFile &file, Symbol &sym);
210 void addTlsIndex(InputFile &file);
211
212 uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
213 int64_t addend) const;
214 uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
215 int64_t addend) const;
216 uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
217 uint64_t getTlsIndexOffset(const InputFile *f) const;
218
219 // Returns the symbol which corresponds to the first entry of the global part
220 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
221 // table properties.
222 // Returns nullptr if the global part is empty.
223 const Symbol *getFirstGlobalEntry() const;
224
225 // Returns the number of entries in the local part of GOT including
226 // the number of reserved entries.
227 unsigned getLocalEntriesNum() const;
228
229 // Return _gp value for primary GOT (nullptr) or particular input file.
230 uint64_t getGp(const InputFile *f = nullptr) const;
231
232private:
233 // MIPS GOT consists of three parts: local, global and tls. Each part
234 // contains different types of entries. Here is a layout of GOT:
235 // - Header entries |
236 // - Page entries | Local part
237 // - Local entries (16-bit access) |
238 // - Local entries (32-bit access) |
239 // - Normal global entries || Global part
240 // - Reloc-only global entries ||
241 // - TLS entries ||| TLS part
242 //
243 // Header:
244 // Two entries hold predefined value 0x0 and 0x80000000.
245 // Page entries:
246 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
247 // relocation against local symbols. They are initialized by higher 16-bit
248 // of the corresponding symbol's value. So each 64kb of address space
249 // requires a single GOT entry.
250 // Local entries (16-bit access):
251 // These entries created by GOT relocations against global non-preemptible
252 // symbols so dynamic linker is not necessary to resolve the symbol's
253 // values. "16-bit access" means that corresponding relocations address
254 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own
255 // GOT entry.
256 // Local entries (32-bit access):
257 // These entries are the same as above but created by relocations which
258 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
259 // Normal global entries:
260 // These entries created by GOT relocations against preemptible global
261 // symbols. They need to be initialized by dynamic linker and they ordered
262 // exactly as the corresponding entries in the dynamic symbols table.
263 // Reloc-only global entries:
264 // These entries created for symbols that are referenced by dynamic
265 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative
266 // addressing, but MIPS ABI requires that these entries be present in GOT.
267 // TLS entries:
268 // Entries created by TLS relocations.
269 //
270 // If the sum of local, global and tls entries is less than 64K only single
271 // got is enough. Otherwise, multi-got is created. Series of primary and
272 // multiple secondary GOTs have the following layout:
273 // - Primary GOT
274 // Header
275 // Local entries
276 // Global entries
277 // Relocation only entries
278 // TLS entries
279 //
280 // - Secondary GOT
281 // Local entries
282 // Global entries
283 // TLS entries
284 // ...
285 //
286 // All GOT entries required by relocations from a single input file entirely
287 // belong to either primary or one of secondary GOTs. To reference GOT entries
288 // each GOT has its own _gp value points to the "middle" of the GOT.
289 // In the code this value loaded to the register which is used for GOT access.
290 //
291 // MIPS 32 function's prologue:
292 // lui v0,0x0
293 // 0: R_MIPS_HI16 _gp_disp
294 // addiu v0,v0,0
295 // 4: R_MIPS_LO16 _gp_disp
296 //
297 // MIPS 64:
298 // lui at,0x0
299 // 14: R_MIPS_GPREL16 main
300 //
301 // Dynamic linker does not know anything about secondary GOTs and cannot
302 // use a regular MIPS mechanism for GOT entries initialization. So we have
303 // to use an approach accepted by other architectures and create dynamic
304 // relocations R_MIPS_REL32 to initialize global entries (and local in case
305 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
306 // requires GOT entries and correspondingly ordered dynamic symbol table
307 // entries to deal with dynamic relocations. To handle this problem
308 // relocation-only section in the primary GOT contains entries for all
309 // symbols referenced in global parts of secondary GOTs. Although the sum
310 // of local and normal global entries of the primary got should be less
311 // than 64K, the size of the primary got (including relocation-only entries
312 // can be greater than 64K, because parts of the primary got that overflow
313 // the 64K limit are used only by the dynamic linker at dynamic link-time
314 // and not by 16-bit gp-relative addressing at run-time.
315 //
316 // For complete multi-GOT description see the following link
317 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
318
319 // Number of "Header" entries.
320 static const unsigned headerEntriesNum = 2;
321
322 // Symbol and addend.
323 using GotEntry = std::pair<Symbol *, int64_t>;
324
325 struct FileGot {
326 InputFile *file = nullptr;
327 size_t startIndex = 0;
328
329 struct PageBlock {
330 Symbol *repSym; // Representative symbol for the OutputSection
331 size_t firstIndex;
332 size_t count;
333 PageBlock(Symbol *repSym = nullptr)
334 : repSym(repSym), firstIndex(0), count(0) {}
335 };
336
337 // Map output sections referenced by MIPS GOT relocations
338 // to the description (index/count) "page" entries allocated
339 // for this section.
340 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
341 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
342 llvm::MapVector<GotEntry, size_t> local16;
343 llvm::MapVector<GotEntry, size_t> local32;
344 llvm::MapVector<Symbol *, size_t> global;
345 llvm::MapVector<Symbol *, size_t> relocs;
346 llvm::MapVector<Symbol *, size_t> tls;
347 // Set of symbols referenced by dynamic TLS relocations.
348 llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
349
350 // Total number of all entries.
351 size_t getEntriesNum() const;
352 // Number of "page" entries.
353 size_t getPageEntriesNum() const;
354 // Number of entries require 16-bit index to access.
355 size_t getIndexedEntriesNum() const;
356 };
357
358 // Container of GOT created for each input file.
359 // After building a final series of GOTs this container
360 // holds primary and secondary GOT's.
361 std::vector<FileGot> gots;
362
363 // Return (and create if necessary) `FileGot`.
364 FileGot &getGot(InputFile &f);
365
366 // Try to merge two GOTs. In case of success the `Dst` contains
367 // result of merging and the function returns true. In case of
368 // overflow the `Dst` is unchanged and the function returns false.
369 bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
370};
371
372class GotPltSection final : public SyntheticSection {
373public:
374 GotPltSection(Ctx &);
375 void addEntry(Symbol &sym);
376 size_t getSize() const override;
377 void writeTo(uint8_t *buf) override;
378 bool isNeeded() const override;
379
380 // Flag to force GotPlt to be in output if we have relocations
381 // that relies on its address.
382 std::atomic<bool> hasGotPltOffRel = false;
383
384private:
385 SmallVector<const Symbol *, 0> entries;
386};
387
388// The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
389// Symbols that will be relocated by Target->IRelativeRel.
390// On most Targets the IgotPltSection will immediately follow the GotPltSection
391// on ARM the IgotPltSection will immediately follow the GotSection.
392class IgotPltSection final : public SyntheticSection {
393public:
394 IgotPltSection(Ctx &);
395 void addEntry(Symbol &sym);
396 size_t getSize() const override;
397 void writeTo(uint8_t *buf) override;
398 bool isNeeded() const override { return !entries.empty(); }
399
400private:
401 SmallVector<const Symbol *, 0> entries;
402};
403
404class StringTableSection final : public SyntheticSection {
405public:
406 StringTableSection(Ctx &, StringRef name, bool dynamic);
407 unsigned addString(StringRef s, bool hashIt = true);
408 void writeTo(uint8_t *buf) override;
409 size_t getSize() const override { return size; }
410 bool isDynamic() const { return dynamic; }
411
412private:
413 const bool dynamic;
414
415 llvm::DenseMap<llvm::CachedHashStringRef, unsigned> stringMap;
416 SmallVector<StringRef, 0> strings;
417};
418
419class DynamicReloc {
420public:
421 /// This constructor records a normal relocation.
422 DynamicReloc(RelType type, const InputSectionBase *inputSec,
423 uint64_t offsetInSec, bool isAgainstSymbol, Symbol &sym,
424 int64_t addend, RelExpr expr)
425 : sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
426 addend(addend), isAgainstSymbol(isAgainstSymbol), isFinal(false),
427 expr(expr) {}
428 /// This constructor records a relative relocation with no symbol.
429 DynamicReloc(RelType type, const InputSectionBase *inputSec,
430 uint64_t offsetInSec, int64_t addend = 0)
431 : DynamicReloc(type, inputSec, offsetInSec, false,
432 *inputSec->getCtx().dummySym, addend, R_ADDEND) {}
433
434 uint64_t getOffset() const;
435 uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
436 bool needsDynSymIndex() const { return isAgainstSymbol; }
437
438 /// Computes the addend of the dynamic relocation. Note that this is not the
439 /// same as the #addend member variable as it may also include the symbol
440 /// address/the address of the corresponding GOT entry/etc.
441 int64_t computeAddend(Ctx &) const;
442
443 void finalize(Ctx &, SymbolTableBaseSection *symt);
444
445 Symbol *sym;
446 const InputSectionBase *inputSec;
447 uint64_t offsetInSec;
448 uint64_t r_offset;
449 RelType type;
450 uint32_t r_sym;
451 // Initially input addend, then the output addend after
452 // RelocationSection<ELFT>::writeTo.
453 int64_t addend;
454
455private:
456 /// Whether this was constructed with a Kind of AgainstSymbol.
457 LLVM_PREFERRED_TYPE(bool)
458 uint8_t isAgainstSymbol : 1;
459
460 /// The resulting dynamic relocation has already had its addend computed.
461 /// Calling computeAddend() is an error.
462 LLVM_PREFERRED_TYPE(bool)
463 uint8_t isFinal : 1;
464
465 // The kind of expression used to calculate the added (required e.g. for
466 // relative GOT relocations).
467 RelExpr expr;
468};
469
470template <class ELFT> class DynamicSection final : public SyntheticSection {
471 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
472
473public:
474 DynamicSection(Ctx &);
475 void finalizeContents() override;
476 void writeTo(uint8_t *buf) override;
477 size_t getSize() const override { return size; }
478
479private:
480 std::vector<std::pair<int32_t, uint64_t>> computeContents();
481};
482
483class RelocationBaseSection : public SyntheticSection {
484public:
485 RelocationBaseSection(Ctx &, StringRef name, uint32_t type,
486 int32_t dynamicTag, int32_t sizeDynamicTag,
487 bool combreloc, unsigned concurrency);
488 /// Add a dynamic relocation without writing an addend to the output section.
489 /// This overload can be used if the addends are written directly instead of
490 /// using relocations on the input section (e.g. MipsGotSection::writeTo()).
491 template <bool shard = false> void addReloc(const DynamicReloc &reloc) {
492 relocs.push_back(Elt: reloc);
493 }
494 /// Add a dynamic relocation against \p sym with an optional addend.
495 void addSymbolReloc(RelType dynType, InputSectionBase &isec,
496 uint64_t offsetInSec, Symbol &sym, int64_t addend = 0,
497 std::optional<RelType> addendRelType = {});
498 /// Add a relative dynamic relocation that uses the target address of \p sym
499 /// (i.e. InputSection::getRelocTargetVA()) + \p addend as the addend.
500 /// This function should only be called for non-preemptible symbols or
501 /// RelExpr values that refer to an address inside the output file (e.g. the
502 /// address of the GOT entry for a potentially preemptible symbol).
503 template <bool shard = false>
504 void addRelativeReloc(RelType dynType, InputSectionBase &isec,
505 uint64_t offsetInSec, Symbol &sym, int64_t addend,
506 RelType addendRelType, RelExpr expr) {
507 assert(expr != R_ADDEND && "expected non-addend relocation expression");
508 addReloc<shard>(false, dynType, isec, offsetInSec, sym, addend, expr,
509 addendRelType);
510 }
511 /// Add a dynamic relocation using the target address of \p sym as the addend
512 /// if \p sym is non-preemptible. Otherwise add a relocation against \p sym.
513 void addAddendOnlyRelocIfNonPreemptible(RelType dynType,
514 InputSectionBase &isec,
515 uint64_t offsetInSec, Symbol &sym,
516 RelType addendRelType);
517 template <bool shard = false>
518 void addReloc(bool isAgainstSymbol, RelType dynType, InputSectionBase &sec,
519 uint64_t offsetInSec, Symbol &sym, int64_t addend, RelExpr expr,
520 RelType addendRelType) {
521 // Write the addends to the relocated address if required. We skip
522 // it if the written value would be zero.
523 if (ctx.arg.writeAddends && (expr != R_ADDEND || addend != 0))
524 sec.addReloc(r: {.expr: expr, .type: addendRelType, .offset: offsetInSec, .addend: addend, .sym: &sym});
525 addReloc<shard>(
526 {dynType, &sec, offsetInSec, isAgainstSymbol, sym, addend, expr});
527 }
528 bool isNeeded() const override {
529 return !relocs.empty() ||
530 llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); });
531 }
532 size_t getSize() const override {
533 size_t count = relocs.size();
534 for (const auto &v : relocsVec)
535 count += v.size();
536 return count * this->entsize;
537 }
538 size_t getRelativeRelocCount() const { return numRelativeRelocs; }
539 void finalizeContents() override;
540
541 int32_t dynamicTag, sizeDynamicTag;
542 SmallVector<DynamicReloc, 0> relocs;
543
544protected:
545 void mergeRels();
546 void partitionRels();
547 void computeRels();
548 // Used when parallel relocation scanning adds relocations. The elements
549 // will be moved into relocs by mergeRel().
550 SmallVector<SmallVector<DynamicReloc, 0>, 0> relocsVec;
551 size_t numRelativeRelocs = 0; // used by -z combreloc
552 bool combreloc;
553};
554
555template <>
556inline void RelocationBaseSection::addReloc<true>(const DynamicReloc &reloc) {
557 relocsVec[llvm::parallel::getThreadIndex()].push_back(Elt: reloc);
558}
559
560template <class ELFT>
561class RelocationSection final : public RelocationBaseSection {
562 using Elf_Rel = typename ELFT::Rel;
563 using Elf_Rela = typename ELFT::Rela;
564
565public:
566 RelocationSection(Ctx &, StringRef name, bool combreloc,
567 unsigned concurrency);
568 void writeTo(uint8_t *buf) override;
569};
570
571template <class ELFT>
572class AndroidPackedRelocationSection final : public RelocationBaseSection {
573 using Elf_Rel = typename ELFT::Rel;
574 using Elf_Rela = typename ELFT::Rela;
575
576public:
577 AndroidPackedRelocationSection(Ctx &, StringRef name, unsigned concurrency);
578
579 bool updateAllocSize(Ctx &) override;
580 size_t getSize() const override { return relocData.size(); }
581 void writeTo(uint8_t *buf) override {
582 memcpy(dest: buf, src: relocData.data(), n: relocData.size());
583 }
584
585private:
586 SmallVector<char, 0> relocData;
587};
588
589struct RelativeReloc {
590 uint64_t getOffset() const {
591 return inputSec->getVA(offset: inputSec->relocs()[relocIdx].offset);
592 }
593
594 InputSectionBase *inputSec;
595 size_t relocIdx;
596};
597
598class RelrBaseSection : public SyntheticSection {
599public:
600 RelrBaseSection(Ctx &, unsigned concurrency, bool isAArch64Auth = false);
601 /// Add a dynamic relocation without writing an addend to the output section.
602 /// This overload can be used if the addends are written directly instead of
603 /// using relocations on the input section.
604 template <bool shard = false> void addReloc(const RelativeReloc &reloc) {
605 relocs.push_back(Elt: reloc);
606 }
607 /// Add a relative dynamic relocation that uses the target address of \p sym
608 /// (i.e. InputSection::getRelocTargetVA()) + \p addend as the addend.
609 template <bool shard = false>
610 void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
611 Symbol &sym, int64_t addend, RelType addendRelType,
612 RelExpr expr) {
613 assert(expr != R_ADDEND && "expected non-addend relocation expression");
614 isec.addReloc(r: {.expr: expr, .type: addendRelType, .offset: offsetInSec, .addend: addend, .sym: &sym});
615 addReloc<shard>({&isec, isec.relocs().size() - 1});
616 }
617 bool isNeeded() const override {
618 return !relocs.empty() ||
619 llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); });
620 }
621 void finalizeContents() override;
622 SmallVector<RelativeReloc, 0> relocs;
623
624protected:
625 void mergeRels();
626 SmallVector<SmallVector<RelativeReloc, 0>, 0> relocsVec;
627};
628
629template <>
630inline void RelrBaseSection::addReloc<true>(const RelativeReloc &reloc) {
631 relocsVec[llvm::parallel::getThreadIndex()].push_back(Elt: reloc);
632}
633
634// RelrSection is used to encode offsets for relative relocations.
635// Proposal for adding SHT_RELR sections to generic-abi is here:
636// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
637// For more details, see the comment in RelrSection::updateAllocSize(Ctx &ctx).
638template <class ELFT> class RelrSection final : public RelrBaseSection {
639 using Elf_Relr = typename ELFT::Relr;
640
641public:
642 RelrSection(Ctx &, unsigned concurrency, bool isAArch64Auth = false);
643
644 bool updateAllocSize(Ctx &) override;
645 size_t getSize() const override { return relrRelocs.size() * this->entsize; }
646 void writeTo(uint8_t *buf) override {
647 memcpy(buf, relrRelocs.data(), getSize());
648 }
649
650private:
651 SmallVector<Elf_Relr, 0> relrRelocs;
652};
653
654struct SymbolTableEntry {
655 Symbol *sym;
656 size_t strTabOffset;
657};
658
659class SymbolTableBaseSection : public SyntheticSection {
660public:
661 SymbolTableBaseSection(Ctx &ctx, StringTableSection &strTabSec);
662 void finalizeContents() override;
663 size_t getSize() const override { return getNumSymbols() * entsize; }
664 void addSymbol(Symbol *sym);
665 unsigned getNumSymbols() const { return symbols.size() + 1; }
666 size_t getSymbolIndex(const Symbol &sym);
667 ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
668
669protected:
670 void sortSymTabSymbols();
671
672 // A vector of symbols and their string table offsets.
673 SmallVector<SymbolTableEntry, 0> symbols;
674
675 StringTableSection &strTabSec;
676
677 llvm::once_flag onceFlag;
678 llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
679 llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
680};
681
682template <class ELFT>
683class SymbolTableSection final : public SymbolTableBaseSection {
684 using Elf_Sym = typename ELFT::Sym;
685
686public:
687 SymbolTableSection(Ctx &, StringTableSection &strTabSec);
688 void writeTo(uint8_t *buf) override;
689};
690
691class SymtabShndxSection final : public SyntheticSection {
692public:
693 SymtabShndxSection(Ctx &);
694
695 void writeTo(uint8_t *buf) override;
696 size_t getSize() const override;
697 bool isNeeded() const override;
698 void finalizeContents() override;
699};
700
701// Outputs GNU Hash section. For detailed explanation see:
702// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
703class GnuHashTableSection final : public SyntheticSection {
704public:
705 GnuHashTableSection(Ctx &);
706 void finalizeContents() override;
707 void writeTo(uint8_t *buf) override;
708 size_t getSize() const override { return size; }
709
710 // Adds symbols to the hash table.
711 // Sorts the input to satisfy GNU hash section requirements.
712 void addSymbols(llvm::SmallVectorImpl<SymbolTableEntry> &symbols);
713
714private:
715 // See the comment in writeBloomFilter.
716 enum { Shift2 = 26 };
717
718 struct Entry {
719 Symbol *sym;
720 size_t strTabOffset;
721 uint32_t hash;
722 uint32_t bucketIdx;
723 };
724
725 SmallVector<Entry, 0> symbols;
726 size_t maskWords;
727 size_t nBuckets = 0;
728 size_t size = 0;
729};
730
731class HashTableSection final : public SyntheticSection {
732public:
733 HashTableSection(Ctx &);
734 void finalizeContents() override;
735 void writeTo(uint8_t *buf) override;
736 size_t getSize() const override { return size; }
737
738private:
739 size_t size = 0;
740};
741
742// Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT
743// entry is associated with a JUMP_SLOT relocation, which may be resolved lazily
744// at runtime.
745//
746// On PowerPC, this section contains lazy symbol resolvers. A branch instruction
747// jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a
748// lazy symbol resolver.
749//
750// On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs.
751// A call instruction jumps to a .plt.sec entry, which will then jump to the
752// target (BIND_NOW) or a .plt entry.
753class PltSection : public SyntheticSection {
754public:
755 PltSection(Ctx &);
756 void writeTo(uint8_t *buf) override;
757 size_t getSize() const override;
758 bool isNeeded() const override;
759 void addSymbols();
760 void addEntry(Symbol &sym);
761 size_t getNumEntries() const { return entries.size(); }
762
763 size_t headerSize;
764
765 SmallVector<const Symbol *, 0> entries;
766};
767
768// Used for non-preemptible ifuncs. It does not have a header. Each entry is
769// associated with an IRELATIVE relocation, which will be resolved eagerly at
770// runtime. PltSection can only contain entries associated with JUMP_SLOT
771// relocations, so IPLT entries are in a separate section.
772class IpltSection final : public SyntheticSection {
773 SmallVector<const Symbol *, 0> entries;
774
775public:
776 IpltSection(Ctx &);
777 void writeTo(uint8_t *buf) override;
778 size_t getSize() const override;
779 bool isNeeded() const override { return !entries.empty(); }
780 void addSymbols();
781 void addEntry(Symbol &sym);
782};
783
784class PPC32GlinkSection : public PltSection {
785public:
786 PPC32GlinkSection(Ctx &);
787 void writeTo(uint8_t *buf) override;
788 size_t getSize() const override;
789
790 SmallVector<const Symbol *, 0> canonical_plts;
791 static constexpr size_t footerSize = 64;
792};
793
794// This is x86-only.
795class IBTPltSection : public SyntheticSection {
796public:
797 IBTPltSection(Ctx &);
798 void writeTo(uint8_t *Buf) override;
799 bool isNeeded() const override;
800 size_t getSize() const override;
801};
802
803// Used to align the end of the PT_GNU_RELRO segment and the associated PT_LOAD
804// segment to a common-page-size boundary. This padding section ensures that all
805// pages in the PT_LOAD segment is covered by at least one section.
806class RelroPaddingSection final : public SyntheticSection {
807public:
808 RelroPaddingSection(Ctx &);
809 size_t getSize() const override { return 0; }
810 void writeTo(uint8_t *buf) override {}
811};
812
813class PaddingSection final : public SyntheticSection {
814public:
815 PaddingSection(Ctx &ctx, uint64_t amount, OutputSection *parent);
816 size_t getSize() const override { return size; }
817 void writeTo(uint8_t *buf) override;
818};
819
820// Used by the merged DWARF32 .debug_names (a per-module index). If we
821// move to DWARF64, most of this data will need to be re-sized.
822class DebugNamesBaseSection : public SyntheticSection {
823public:
824 struct Abbrev : llvm::FoldingSetNode {
825 uint32_t code;
826 uint32_t tag;
827 SmallVector<llvm::DWARFDebugNames::AttributeEncoding, 2> attributes;
828
829 void Profile(llvm::FoldingSetNodeID &id) const;
830 };
831
832 struct AttrValue {
833 uint32_t attrValue;
834 uint8_t attrSize;
835 };
836
837 struct IndexEntry {
838 uint32_t abbrevCode;
839 uint32_t poolOffset;
840 union {
841 uint64_t parentOffset = 0;
842 IndexEntry *parentEntry;
843 };
844 SmallVector<AttrValue, 3> attrValues;
845 };
846
847 struct NameEntry {
848 const char *name;
849 uint32_t hashValue;
850 uint32_t stringOffset;
851 uint32_t entryOffset;
852 // Used to relocate `stringOffset` in the merged section.
853 uint32_t chunkIdx;
854 SmallVector<IndexEntry *, 0> indexEntries;
855
856 llvm::iterator_range<
857 llvm::pointee_iterator<typename SmallVector<IndexEntry *, 0>::iterator>>
858 entries() {
859 return llvm::make_pointee_range(Range&: indexEntries);
860 }
861 };
862
863 // The contents of one input .debug_names section. An InputChunk
864 // typically contains one NameData, but might contain more, especially
865 // in LTO builds.
866 struct NameData {
867 llvm::DWARFDebugNames::Header hdr;
868 llvm::DenseMap<uint32_t, uint32_t> abbrevCodeMap;
869 SmallVector<NameEntry, 0> nameEntries;
870 };
871
872 // InputChunk and OutputChunk hold per-file contributions to the merged index.
873 // InputChunk instances will be discarded after `init` completes.
874 struct InputChunk {
875 uint32_t baseCuIdx;
876 LLDDWARFSection section;
877 SmallVector<NameData, 0> nameData;
878 std::optional<llvm::DWARFDebugNames> llvmDebugNames;
879 };
880
881 struct OutputChunk {
882 // Pointer to the .debug_info section that contains compile units, used to
883 // compute the relocated CU offsets.
884 InputSection *infoSec;
885 // This initially holds section offsets. After relocation, the section
886 // offsets are changed to CU offsets relative the the output section.
887 SmallVector<uint32_t, 0> compUnits;
888 };
889
890 DebugNamesBaseSection(Ctx &);
891 size_t getSize() const override { return size; }
892 bool isNeeded() const override { return numChunks > 0; }
893
894protected:
895 void init(llvm::function_ref<void(InputFile *, InputChunk &, OutputChunk &)>);
896 static void
897 parseDebugNames(Ctx &, InputChunk &inputChunk, OutputChunk &chunk,
898 llvm::DWARFDataExtractor &namesExtractor,
899 llvm::DataExtractor &strExtractor,
900 llvm::function_ref<SmallVector<uint32_t, 0>(
901 uint32_t numCUs, const llvm::DWARFDebugNames::Header &hdr,
902 const llvm::DWARFDebugNames::DWARFDebugNamesOffsets &)>
903 readOffsets);
904 void computeHdrAndAbbrevTable(MutableArrayRef<InputChunk> inputChunks);
905 std::pair<uint32_t, uint32_t>
906 computeEntryPool(MutableArrayRef<InputChunk> inputChunks);
907
908 // Input .debug_names sections for relocating string offsets in the name table
909 // in `finalizeContents`.
910 SmallVector<InputSection *, 0> inputSections;
911
912 llvm::DWARFDebugNames::Header hdr;
913 size_t numChunks;
914 std::unique_ptr<OutputChunk[]> chunks;
915 llvm::SpecificBumpPtrAllocator<Abbrev> abbrevAlloc;
916 SmallVector<Abbrev *, 0> abbrevTable;
917 SmallVector<char, 0> abbrevTableBuf;
918
919 ArrayRef<OutputChunk> getChunks() const {
920 return ArrayRef(chunks.get(), numChunks);
921 }
922
923 // Sharded name entries that will be used to compute bucket_count and the
924 // count name table.
925 static constexpr size_t numShards = 32;
926 SmallVector<NameEntry, 0> nameVecs[numShards];
927};
928
929// Complement DebugNamesBaseSection for ELFT-aware code: reading offsets,
930// relocating string offsets, and writeTo.
931template <class ELFT>
932class DebugNamesSection final : public DebugNamesBaseSection {
933public:
934 DebugNamesSection(Ctx &);
935 void finalizeContents() override;
936 void writeTo(uint8_t *buf) override;
937
938 template <class RelTy>
939 void getNameRelocs(const InputFile &file,
940 llvm::DenseMap<uint32_t, uint32_t> &relocs,
941 Relocs<RelTy> rels);
942
943private:
944 static void readOffsets(InputChunk &inputChunk, OutputChunk &chunk,
945 llvm::DWARFDataExtractor &namesExtractor,
946 llvm::DataExtractor &strExtractor);
947};
948
949class GdbIndexSection final : public SyntheticSection {
950public:
951 struct AddressEntry {
952 InputSection *section;
953 uint64_t lowAddress;
954 uint64_t highAddress;
955 uint32_t cuIndex;
956 };
957
958 struct CuEntry {
959 uint64_t cuOffset;
960 uint64_t cuLength;
961 };
962
963 struct NameAttrEntry {
964 llvm::CachedHashStringRef name;
965 uint32_t cuIndexAndAttrs;
966 };
967
968 struct GdbChunk {
969 InputSection *sec;
970 SmallVector<AddressEntry, 0> addressAreas;
971 SmallVector<CuEntry, 0> compilationUnits;
972 };
973
974 struct GdbSymbol {
975 llvm::CachedHashStringRef name;
976 SmallVector<uint32_t, 0> cuVector;
977 uint32_t nameOff;
978 uint32_t cuVectorOff;
979 };
980
981 GdbIndexSection(Ctx &);
982 template <typename ELFT>
983 static std::unique_ptr<GdbIndexSection> create(Ctx &);
984 void writeTo(uint8_t *buf) override;
985 size_t getSize() const override { return size; }
986 bool isNeeded() const override;
987
988private:
989 struct GdbIndexHeader {
990 llvm::support::ulittle32_t version;
991 llvm::support::ulittle32_t cuListOff;
992 llvm::support::ulittle32_t cuTypesOff;
993 llvm::support::ulittle32_t addressAreaOff;
994 llvm::support::ulittle32_t symtabOff;
995 llvm::support::ulittle32_t constantPoolOff;
996 };
997
998 size_t computeSymtabSize() const;
999
1000 // Each chunk contains information gathered from debug sections of a
1001 // single object file.
1002 SmallVector<GdbChunk, 0> chunks;
1003
1004 // A symbol table for this .gdb_index section.
1005 SmallVector<GdbSymbol, 0> symbols;
1006
1007 size_t size;
1008};
1009
1010// For more information about .gnu.version and .gnu.version_r see:
1011// https://www.akkadia.org/drepper/symbol-versioning
1012
1013// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
1014// contain symbol version definitions. The number of entries in this section
1015// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
1016// The section shall contain an array of Elf_Verdef structures, optionally
1017// followed by an array of Elf_Verdaux structures.
1018class VersionDefinitionSection final : public SyntheticSection {
1019public:
1020 VersionDefinitionSection(Ctx &);
1021 void finalizeContents() override;
1022 size_t getSize() const override;
1023 void writeTo(uint8_t *buf) override;
1024
1025private:
1026 enum { EntrySize = 28 };
1027 void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
1028 StringRef getFileDefName();
1029
1030 unsigned fileDefNameOff;
1031 SmallVector<unsigned, 0> verDefNameOffs;
1032};
1033
1034// The .gnu.version section specifies the required version of each symbol in the
1035// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
1036// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
1037// identifier defined in the either .gnu.version_r or .gnu.version_d section.
1038// The values 0 and 1 are reserved. All other values are used for versions in
1039// the own object or in any of the dependencies.
1040class VersionTableSection final : public SyntheticSection {
1041public:
1042 VersionTableSection(Ctx &);
1043 void finalizeContents() override;
1044 size_t getSize() const override;
1045 void writeTo(uint8_t *buf) override;
1046 bool isNeeded() const override;
1047};
1048
1049// The .gnu.version_r section defines the version identifiers used by
1050// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
1051// Elf_Verneed specifies the version requirements for a single DSO, and contains
1052// a reference to a linked list of Elf_Vernaux data structures which define the
1053// mapping from version identifiers to version names.
1054template <class ELFT>
1055class VersionNeedSection final : public SyntheticSection {
1056 using Elf_Verneed = typename ELFT::Verneed;
1057 using Elf_Vernaux = typename ELFT::Vernaux;
1058
1059 struct Vernaux {
1060 uint64_t hash;
1061 SharedFile::VerneedInfo verneedInfo;
1062 uint64_t nameStrTab;
1063 };
1064
1065 struct Verneed {
1066 uint64_t nameStrTab;
1067 std::vector<Vernaux> vernauxs;
1068 };
1069
1070 SmallVector<Verneed, 0> verneeds;
1071
1072public:
1073 VersionNeedSection(Ctx &);
1074 void finalizeContents() override;
1075 void writeTo(uint8_t *buf) override;
1076 size_t getSize() const override;
1077 bool isNeeded() const override;
1078};
1079
1080// MergeSyntheticSection is a class that allows us to put mergeable sections
1081// with different attributes in a single output sections. To do that
1082// we put them into MergeSyntheticSection synthetic input sections which are
1083// attached to regular output sections.
1084class MergeSyntheticSection : public SyntheticSection {
1085public:
1086 void addSection(MergeInputSection *ms);
1087 SmallVector<MergeInputSection *, 0> sections;
1088
1089protected:
1090 MergeSyntheticSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags,
1091 uint32_t addralign)
1092 : SyntheticSection(ctx, name, type, flags, addralign) {}
1093};
1094
1095class MergeTailSection final : public MergeSyntheticSection {
1096public:
1097 MergeTailSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags,
1098 uint32_t addralign);
1099
1100 size_t getSize() const override;
1101 void writeTo(uint8_t *buf) override;
1102 void finalizeContents() override;
1103
1104private:
1105 llvm::StringTableBuilder builder;
1106};
1107
1108class MergeNoTailSection final : public MergeSyntheticSection {
1109public:
1110 MergeNoTailSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags,
1111 uint32_t addralign)
1112 : MergeSyntheticSection(ctx, name, type, flags, addralign) {}
1113
1114 size_t getSize() const override { return size; }
1115 void writeTo(uint8_t *buf) override;
1116 void finalizeContents() override;
1117
1118private:
1119 // We use the most significant bits of a hash as a shard ID.
1120 // The reason why we don't want to use the least significant bits is
1121 // because DenseMap also uses lower bits to determine a bucket ID.
1122 // If we use lower bits, it significantly increases the probability of
1123 // hash collisions.
1124 size_t getShardId(uint32_t hash) {
1125 assert((hash >> 31) == 0);
1126 return hash >> (31 - llvm::countr_zero(Val: numShards));
1127 }
1128
1129 // Section size
1130 size_t size;
1131
1132 // String table contents
1133 constexpr static size_t numShards = 32;
1134 SmallVector<llvm::StringTableBuilder, 0> shards;
1135 size_t shardOffsets[numShards];
1136};
1137
1138// .MIPS.abiflags section.
1139template <class ELFT>
1140class MipsAbiFlagsSection final : public SyntheticSection {
1141 using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
1142
1143public:
1144 static std::unique_ptr<MipsAbiFlagsSection> create(Ctx &);
1145
1146 MipsAbiFlagsSection(Ctx &, Elf_Mips_ABIFlags flags);
1147 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
1148 void writeTo(uint8_t *buf) override;
1149
1150private:
1151 Elf_Mips_ABIFlags flags;
1152};
1153
1154// .MIPS.options section.
1155template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
1156 using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
1157 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
1158
1159public:
1160 static std::unique_ptr<MipsOptionsSection<ELFT>> create(Ctx &);
1161
1162 MipsOptionsSection(Ctx &, Elf_Mips_RegInfo reginfo);
1163 void writeTo(uint8_t *buf) override;
1164
1165 size_t getSize() const override {
1166 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
1167 }
1168
1169private:
1170 Elf_Mips_RegInfo reginfo;
1171};
1172
1173// MIPS .reginfo section.
1174template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
1175 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
1176
1177public:
1178 static std::unique_ptr<MipsReginfoSection> create(Ctx &);
1179
1180 MipsReginfoSection(Ctx &, Elf_Mips_RegInfo reginfo);
1181 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
1182 void writeTo(uint8_t *buf) override;
1183
1184private:
1185 Elf_Mips_RegInfo reginfo;
1186};
1187
1188// This is a MIPS specific section to hold a space within the data segment
1189// of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
1190// See "Dynamic section" in Chapter 5 in the following document:
1191// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1192class MipsRldMapSection final : public SyntheticSection {
1193public:
1194 MipsRldMapSection(Ctx &);
1195 size_t getSize() const override { return ctx.arg.wordsize; }
1196 void writeTo(uint8_t *buf) override {}
1197};
1198
1199// Representation of the combined .ARM.Exidx input sections. We process these
1200// as a SyntheticSection like .eh_frame as we need to merge duplicate entries
1201// and add terminating sentinel entries.
1202//
1203// The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
1204// a table that the unwinder can derive (Addresses are encoded as offsets from
1205// table):
1206// | Address of function | Unwind instructions for function |
1207// where the unwind instructions are either a small number of unwind or the
1208// special EXIDX_CANTUNWIND entry representing no unwinding information.
1209// When an exception is thrown from an address A, the unwinder searches the
1210// table for the closest table entry with Address of function <= A. This means
1211// that for two consecutive table entries:
1212// | A1 | U1 |
1213// | A2 | U2 |
1214// The range of addresses described by U1 is [A1, A2)
1215//
1216// There are two cases where we need a linker generated table entry to fixup
1217// the address ranges in the table
1218// Case 1:
1219// - A sentinel entry added with an address higher than all
1220// executable sections. This was needed to work around libunwind bug pr31091.
1221// - After address assignment we need to find the highest addressed executable
1222// section and use the limit of that section so that the unwinder never
1223// matches it.
1224// Case 2:
1225// - InputSections without a .ARM.exidx section (usually from Assembly)
1226// need a table entry so that they terminate the range of the previously
1227// function. This is pr40277.
1228//
1229// Instead of storing pointers to the .ARM.exidx InputSections from
1230// InputObjects, we store pointers to the executable sections that need
1231// .ARM.exidx sections. We can then use the dependentSections of these to
1232// either find the .ARM.exidx section or know that we need to generate one.
1233class ARMExidxSyntheticSection : public SyntheticSection {
1234public:
1235 ARMExidxSyntheticSection(Ctx &);
1236
1237 // Add an input section to the ARMExidxSyntheticSection. Returns whether the
1238 // section needs to be removed from the main input section list.
1239 bool addSection(InputSection *isec);
1240
1241 size_t getSize() const override { return size; }
1242 void writeTo(uint8_t *buf) override;
1243 bool isNeeded() const override;
1244 // Sort and remove duplicate entries.
1245 void finalizeContents() override;
1246 InputSection *getLinkOrderDep() const;
1247
1248 static bool classof(const SectionBase *sec) {
1249 return sec->kind() == InputSectionBase::Synthetic &&
1250 sec->type == llvm::ELF::SHT_ARM_EXIDX;
1251 }
1252
1253 // Links to the ARMExidxSections so we can transfer the relocations once the
1254 // layout is known.
1255 SmallVector<InputSection *, 0> exidxSections;
1256
1257private:
1258 size_t size = 0;
1259
1260 // Instead of storing pointers to the .ARM.exidx InputSections from
1261 // InputObjects, we store pointers to the executable sections that need
1262 // .ARM.exidx sections. We can then use the dependentSections of these to
1263 // either find the .ARM.exidx section or know that we need to generate one.
1264 SmallVector<InputSection *, 0> executableSections;
1265
1266 // Value of executableSecitons before finalizeContents(), so that it can be
1267 // run repeateadly during fixed point iteration.
1268 SmallVector<InputSection *, 0> originalExecutableSections;
1269
1270 // The executable InputSection with the highest address to use for the
1271 // sentinel. We store separately from ExecutableSections as merging of
1272 // duplicate entries may mean this InputSection is removed from
1273 // ExecutableSections.
1274 InputSection *sentinel = nullptr;
1275};
1276
1277// A container for one or more linker generated thunks. Instances of these
1278// thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1279class ThunkSection final : public SyntheticSection {
1280public:
1281 // ThunkSection in OS, with desired outSecOff of Off
1282 ThunkSection(Ctx &, OutputSection *os, uint64_t off);
1283
1284 // Add a newly created Thunk to this container:
1285 // Thunk is given offset from start of this InputSection
1286 // Thunk defines a symbol in this InputSection that can be used as target
1287 // of a relocation
1288 void addThunk(Thunk *t);
1289 size_t getSize() const override;
1290 void writeTo(uint8_t *buf) override;
1291 InputSection *getTargetInputSection() const;
1292 bool assignOffsets();
1293
1294 // When true, round up reported size of section to 4 KiB. See comment
1295 // in addThunkSection() for more details.
1296 bool roundUpSizeForErrata = false;
1297
1298private:
1299 SmallVector<Thunk *, 0> thunks;
1300 size_t size = 0;
1301};
1302
1303// Cortex-M Security Extensions. Prefix for functions that should be exported
1304// for the non-secure world.
1305const char ACLESESYM_PREFIX[] = "__acle_se_";
1306const int ACLESESYM_SIZE = 8;
1307
1308class ArmCmseSGVeneer {
1309public:
1310 ArmCmseSGVeneer(Symbol *sym, Symbol *acleSeSym,
1311 std::optional<uint64_t> addr = std::nullopt)
1312 : sym(sym), acleSeSym(acleSeSym), entAddr{addr} {}
1313 static const size_t size{ACLESESYM_SIZE};
1314 std::optional<uint64_t> getAddr() const { return entAddr; };
1315
1316 Symbol *sym;
1317 Symbol *acleSeSym;
1318 uint64_t offset = 0;
1319
1320private:
1321 const std::optional<uint64_t> entAddr;
1322};
1323
1324class ArmCmseSGSection final : public SyntheticSection {
1325public:
1326 ArmCmseSGSection(Ctx &ctx);
1327 bool isNeeded() const override { return !entries.empty(); }
1328 size_t getSize() const override;
1329 void writeTo(uint8_t *buf) override;
1330 void addSGVeneer(Symbol *sym, Symbol *ext_sym);
1331 void addMappingSymbol();
1332 void finalizeContents() override;
1333 void exportEntries(SymbolTableBaseSection *symTab);
1334 uint64_t impLibMaxAddr = 0;
1335
1336private:
1337 SmallVector<std::pair<Symbol *, Symbol *>, 0> entries;
1338 SmallVector<std::unique_ptr<ArmCmseSGVeneer>, 0> sgVeneers;
1339 uint64_t newEntries = 0;
1340};
1341
1342// Used to compute outSecOff of .got2 in each object file. This is needed to
1343// synthesize PLT entries for PPC32 Secure PLT ABI.
1344class PPC32Got2Section final : public SyntheticSection {
1345public:
1346 PPC32Got2Section(Ctx &);
1347 size_t getSize() const override { return 0; }
1348 bool isNeeded() const override;
1349 void finalizeContents() override;
1350 void writeTo(uint8_t *buf) override {}
1351};
1352
1353// This section is used to store the addresses of functions that are called
1354// in range-extending thunks on PowerPC64. When producing position dependent
1355// code the addresses are link-time constants and the table is written out to
1356// the binary. When producing position-dependent code the table is allocated and
1357// filled in by the dynamic linker.
1358class PPC64LongBranchTargetSection final : public SyntheticSection {
1359public:
1360 PPC64LongBranchTargetSection(Ctx &);
1361 uint64_t getEntryVA(const Symbol *sym, int64_t addend);
1362 std::optional<uint32_t> addEntry(const Symbol *sym, int64_t addend);
1363 size_t getSize() const override;
1364 void writeTo(uint8_t *buf) override;
1365 bool isNeeded() const override;
1366 void finalizeContents() override { finalized = true; }
1367
1368private:
1369 SmallVector<std::pair<const Symbol *, int64_t>, 0> entries;
1370 llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index;
1371 bool finalized = false;
1372};
1373
1374template <typename ELFT>
1375class PartitionElfHeaderSection final : public SyntheticSection {
1376public:
1377 PartitionElfHeaderSection(Ctx &);
1378 size_t getSize() const override;
1379 void writeTo(uint8_t *buf) override;
1380};
1381
1382template <typename ELFT>
1383class PartitionProgramHeadersSection final : public SyntheticSection {
1384public:
1385 PartitionProgramHeadersSection(Ctx &);
1386 size_t getSize() const override;
1387 void writeTo(uint8_t *buf) override;
1388};
1389
1390class PartitionIndexSection final : public SyntheticSection {
1391public:
1392 PartitionIndexSection(Ctx &);
1393 size_t getSize() const override;
1394 void finalizeContents() override;
1395 void writeTo(uint8_t *buf) override;
1396};
1397
1398// See the following link for the Android-specific loader code that operates on
1399// this section:
1400// https://cs.android.com/android/platform/superproject/+/master:bionic/libc/bionic/libc_init_static.cpp;drc=9425b16978f9c5aa8f2c50c873db470819480d1d;l=192
1401class MemtagAndroidNote final : public SyntheticSection {
1402public:
1403 MemtagAndroidNote(Ctx &ctx)
1404 : SyntheticSection(ctx, ".note.android.memtag", llvm::ELF::SHT_NOTE,
1405 llvm::ELF::SHF_ALLOC, /*addralign=*/4) {}
1406 void writeTo(uint8_t *buf) override;
1407 size_t getSize() const override;
1408};
1409
1410class PackageMetadataNote final : public SyntheticSection {
1411public:
1412 PackageMetadataNote(Ctx &ctx)
1413 : SyntheticSection(ctx, ".note.package", llvm::ELF::SHT_NOTE,
1414 llvm::ELF::SHF_ALLOC, /*addralign=*/4) {}
1415 void writeTo(uint8_t *buf) override;
1416 size_t getSize() const override;
1417};
1418
1419class MemtagGlobalDescriptors final : public SyntheticSection {
1420public:
1421 MemtagGlobalDescriptors(Ctx &ctx)
1422 : SyntheticSection(ctx, ".memtag.globals.dynamic",
1423 llvm::ELF::SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC,
1424 llvm::ELF::SHF_ALLOC, /*addralign=*/4) {}
1425 void writeTo(uint8_t *buf) override;
1426 // The size of the section is non-computable until all addresses are
1427 // synthetized, because the section's contents contain a sorted
1428 // varint-compressed list of pointers to global variables. We only know the
1429 // final size after `finalizeAddressDependentContent()`.
1430 size_t getSize() const override;
1431 bool updateAllocSize(Ctx &) override;
1432
1433 void addSymbol(const Symbol &sym) {
1434 symbols.push_back(Elt: &sym);
1435 }
1436
1437 bool isNeeded() const override { return !symbols.empty(); }
1438
1439private:
1440 SmallVector<const Symbol *, 0> symbols;
1441};
1442
1443template <class ELFT> void createSyntheticSections(Ctx &);
1444InputSection *createInterpSection(Ctx &);
1445MergeInputSection *createCommentSection(Ctx &);
1446template <class ELFT> void splitSections(Ctx &);
1447void combineEhSections(Ctx &);
1448
1449bool hasMemtag(Ctx &);
1450bool canHaveMemtagGlobals(Ctx &);
1451
1452template <typename ELFT> void writeEhdr(Ctx &, uint8_t *buf, Partition &part);
1453template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
1454
1455Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
1456 uint64_t value, uint64_t size,
1457 InputSectionBase &section);
1458
1459void addVerneed(Ctx &, Symbol &ss);
1460
1461// This describes a program header entry.
1462// Each contains type, access flags and range of output sections that will be
1463// placed in it.
1464struct PhdrEntry {
1465 PhdrEntry(Ctx &ctx, unsigned type, unsigned flags)
1466 : p_align(type == llvm::ELF::PT_LOAD ? ctx.arg.maxPageSize : 0),
1467 p_type(type), p_flags(flags) {}
1468 void add(OutputSection *sec);
1469
1470 uint64_t p_paddr = 0;
1471 uint64_t p_vaddr = 0;
1472 uint64_t p_memsz = 0;
1473 uint64_t p_filesz = 0;
1474 uint64_t p_offset = 0;
1475 uint32_t p_align = 0;
1476 uint32_t p_type = 0;
1477 uint32_t p_flags = 0;
1478
1479 OutputSection *firstSec = nullptr;
1480 OutputSection *lastSec = nullptr;
1481 bool hasLMA = false;
1482
1483 uint64_t lmaOffset = 0;
1484};
1485
1486// Linker generated per-partition sections.
1487struct Partition {
1488 Ctx &ctx;
1489 StringRef name;
1490 uint64_t nameStrTab;
1491
1492 std::unique_ptr<SyntheticSection> elfHeader;
1493 std::unique_ptr<SyntheticSection> programHeaders;
1494 SmallVector<std::unique_ptr<PhdrEntry>, 0> phdrs;
1495
1496 std::unique_ptr<ARMExidxSyntheticSection> armExidx;
1497 std::unique_ptr<BuildIdSection> buildId;
1498 std::unique_ptr<SyntheticSection> dynamic;
1499 std::unique_ptr<StringTableSection> dynStrTab;
1500 std::unique_ptr<SymbolTableBaseSection> dynSymTab;
1501 std::unique_ptr<EhFrameHeader> ehFrameHdr;
1502 std::unique_ptr<EhFrameSection> ehFrame;
1503 std::unique_ptr<GnuHashTableSection> gnuHashTab;
1504 std::unique_ptr<HashTableSection> hashTab;
1505 std::unique_ptr<MemtagAndroidNote> memtagAndroidNote;
1506 std::unique_ptr<MemtagGlobalDescriptors> memtagGlobalDescriptors;
1507 std::unique_ptr<PackageMetadataNote> packageMetadataNote;
1508 std::unique_ptr<RelocationBaseSection> relaDyn;
1509 std::unique_ptr<RelrBaseSection> relrDyn;
1510 std::unique_ptr<RelrBaseSection> relrAuthDyn;
1511 std::unique_ptr<VersionDefinitionSection> verDef;
1512 std::unique_ptr<SyntheticSection> verNeed;
1513 std::unique_ptr<VersionTableSection> verSym;
1514
1515 Partition(Ctx &ctx) : ctx(ctx) {}
1516 unsigned getNumber(Ctx &ctx) const { return this - &ctx.partitions[0] + 1; }
1517};
1518
1519inline Partition &SectionBase::getPartition(Ctx &ctx) const {
1520 assert(isLive());
1521 return ctx.partitions[partition - 1];
1522}
1523
1524} // namespace lld::elf
1525
1526#endif
1527