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