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 "DWARF.h"
24 #include "EhFrame.h"
25 #include "InputSection.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/MC/StringTableBuilder.h"
29 #include "llvm/Support/Endian.h"
30 #include <functional>
31
32 namespace lld {
33 namespace elf {
34 class Defined;
35 struct PhdrEntry;
36 class SymbolTableBaseSection;
37 class VersionNeedBaseSection;
38
39 class SyntheticSection : public InputSection {
40 public:
SyntheticSection(uint64_t flags,uint32_t type,uint32_t alignment,StringRef name)41 SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
42 StringRef name)
43 : InputSection(nullptr, flags, type, alignment, {}, name,
44 InputSectionBase::Synthetic) {
45 markLive();
46 }
47
48 virtual ~SyntheticSection() = default;
49 virtual void writeTo(uint8_t *buf) = 0;
50 virtual size_t getSize() const = 0;
finalizeContents()51 virtual void finalizeContents() {}
52 // If the section has the SHF_ALLOC flag and the size may be changed if
53 // thunks are added, update the section size.
updateAllocSize()54 virtual bool updateAllocSize() { return false; }
isNeeded()55 virtual bool isNeeded() const { return true; }
56
classof(const SectionBase * d)57 static bool classof(const SectionBase *d) {
58 return d->kind() == InputSectionBase::Synthetic;
59 }
60 };
61
62 struct CieRecord {
63 EhSectionPiece *cie = nullptr;
64 std::vector<EhSectionPiece *> fdes;
65 };
66
67 // Section for .eh_frame.
68 class EhFrameSection final : public SyntheticSection {
69 public:
70 EhFrameSection();
71 void writeTo(uint8_t *buf) override;
72 void finalizeContents() override;
isNeeded()73 bool isNeeded() const override { return !sections.empty(); }
getSize()74 size_t getSize() const override { return size; }
75
classof(const SectionBase * d)76 static bool classof(const SectionBase *d) {
77 return SyntheticSection::classof(d) && d->name == ".eh_frame";
78 }
79
80 void addSection(EhInputSection *sec);
81
82 std::vector<EhInputSection *> sections;
83 size_t numFdes = 0;
84
85 struct FdeData {
86 uint32_t pcRel;
87 uint32_t fdeVARel;
88 };
89
90 std::vector<FdeData> getFdeData() const;
getCieRecords()91 ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; }
92 template <class ELFT>
93 void iterateFDEWithLSDA(llvm::function_ref<void(InputSection &)> fn);
94
95 private:
96 // This is used only when parsing EhInputSection. We keep it here to avoid
97 // allocating one for each EhInputSection.
98 llvm::DenseMap<size_t, CieRecord *> offsetToCie;
99
100 uint64_t size = 0;
101
102 template <class ELFT, class RelTy>
103 void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels);
104 template <class ELFT> void addSectionAux(EhInputSection *s);
105 template <class ELFT, class RelTy>
106 void iterateFDEWithLSDAAux(EhInputSection &sec, ArrayRef<RelTy> rels,
107 llvm::DenseSet<size_t> &ciesWithLSDA,
108 llvm::function_ref<void(InputSection &)> fn);
109
110 template <class ELFT, class RelTy>
111 CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels);
112
113 template <class ELFT, class RelTy>
114 Defined *isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels);
115
116 uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
117
118 std::vector<CieRecord *> cieRecords;
119
120 // CIE records are uniquified by their contents and personality functions.
121 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
122 };
123
124 class GotSection : public SyntheticSection {
125 public:
126 GotSection();
getSize()127 size_t getSize() const override { return size; }
128 void finalizeContents() override;
129 bool isNeeded() const override;
130 void writeTo(uint8_t *buf) override;
131
132 void addEntry(Symbol &sym);
133 bool addDynTlsEntry(Symbol &sym);
134 bool addTlsIndex();
135 uint64_t getGlobalDynAddr(const Symbol &b) const;
136 uint64_t getGlobalDynOffset(const Symbol &b) const;
137
getTlsIndexVA()138 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
getTlsIndexOff()139 uint32_t getTlsIndexOff() const { return tlsIndexOff; }
140
141 // Flag to force GOT to be in output if we have relocations
142 // that relies on its address.
143 bool hasGotOffRel = false;
144
145 protected:
146 size_t numEntries = 0;
147 uint32_t tlsIndexOff = -1;
148 uint64_t size = 0;
149 };
150
151 // .note.GNU-stack section.
152 class GnuStackSection : public SyntheticSection {
153 public:
GnuStackSection()154 GnuStackSection()
155 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
writeTo(uint8_t * buf)156 void writeTo(uint8_t *buf) override {}
getSize()157 size_t getSize() const override { return 0; }
158 };
159
160 class GnuPropertySection : public SyntheticSection {
161 public:
162 GnuPropertySection();
163 void writeTo(uint8_t *buf) override;
164 size_t getSize() const override;
165 };
166
167 // .note.gnu.build-id section.
168 class BuildIdSection : public SyntheticSection {
169 // First 16 bytes are a header.
170 static const unsigned headerSize = 16;
171
172 public:
173 const size_t hashSize;
174 BuildIdSection();
175 void writeTo(uint8_t *buf) override;
getSize()176 size_t getSize() const override { return headerSize + hashSize; }
177 void writeBuildId(llvm::ArrayRef<uint8_t> buf);
178
179 private:
180 uint8_t *hashBuf;
181 };
182
183 // BssSection is used to reserve space for copy relocations and common symbols.
184 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
185 // that are used for writable symbols, read-only symbols and common symbols,
186 // respectively.
187 class BssSection final : public SyntheticSection {
188 public:
189 BssSection(StringRef name, uint64_t size, uint32_t alignment);
writeTo(uint8_t *)190 void writeTo(uint8_t *) override {
191 llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
192 }
isNeeded()193 bool isNeeded() const override { return size != 0; }
getSize()194 size_t getSize() const override { return size; }
195
classof(const SectionBase * s)196 static bool classof(const SectionBase *s) { return s->bss; }
197 uint64_t size;
198 };
199
200 class MipsGotSection final : public SyntheticSection {
201 public:
202 MipsGotSection();
203 void writeTo(uint8_t *buf) override;
getSize()204 size_t getSize() const override { return size; }
205 bool updateAllocSize() 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 addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
214 void addDynTlsEntry(InputFile &file, Symbol &sym);
215 void addTlsIndex(InputFile &file);
216
217 uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
218 int64_t addend) const;
219 uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
220 int64_t addend) const;
221 uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
222 uint64_t getTlsIndexOffset(const InputFile *f) const;
223
224 // Returns the symbol which corresponds to the first entry of the global part
225 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
226 // table properties.
227 // Returns nullptr if the global part is empty.
228 const Symbol *getFirstGlobalEntry() const;
229
230 // Returns the number of entries in the local part of GOT including
231 // the number of reserved entries.
232 unsigned getLocalEntriesNum() const;
233
234 // Return _gp value for primary GOT (nullptr) or particular input file.
235 uint64_t getGp(const InputFile *f = nullptr) const;
236
237 private:
238 // MIPS GOT consists of three parts: local, global and tls. Each part
239 // contains different types of entries. Here is a layout of GOT:
240 // - Header entries |
241 // - Page entries | Local part
242 // - Local entries (16-bit access) |
243 // - Local entries (32-bit access) |
244 // - Normal global entries || Global part
245 // - Reloc-only global entries ||
246 // - TLS entries ||| TLS part
247 //
248 // Header:
249 // Two entries hold predefined value 0x0 and 0x80000000.
250 // Page entries:
251 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
252 // relocation against local symbols. They are initialized by higher 16-bit
253 // of the corresponding symbol's value. So each 64kb of address space
254 // requires a single GOT entry.
255 // Local entries (16-bit access):
256 // These entries created by GOT relocations against global non-preemptible
257 // symbols so dynamic linker is not necessary to resolve the symbol's
258 // values. "16-bit access" means that corresponding relocations address
259 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own
260 // GOT entry.
261 // Local entries (32-bit access):
262 // These entries are the same as above but created by relocations which
263 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
264 // Normal global entries:
265 // These entries created by GOT relocations against preemptible global
266 // symbols. They need to be initialized by dynamic linker and they ordered
267 // exactly as the corresponding entries in the dynamic symbols table.
268 // Reloc-only global entries:
269 // These entries created for symbols that are referenced by dynamic
270 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative
271 // addressing, but MIPS ABI requires that these entries be present in GOT.
272 // TLS entries:
273 // Entries created by TLS relocations.
274 //
275 // If the sum of local, global and tls entries is less than 64K only single
276 // got is enough. Otherwise, multi-got is created. Series of primary and
277 // multiple secondary GOTs have the following layout:
278 // - Primary GOT
279 // Header
280 // Local entries
281 // Global entries
282 // Relocation only entries
283 // TLS entries
284 //
285 // - Secondary GOT
286 // Local entries
287 // Global entries
288 // TLS entries
289 // ...
290 //
291 // All GOT entries required by relocations from a single input file entirely
292 // belong to either primary or one of secondary GOTs. To reference GOT entries
293 // each GOT has its own _gp value points to the "middle" of the GOT.
294 // In the code this value loaded to the register which is used for GOT access.
295 //
296 // MIPS 32 function's prologue:
297 // lui v0,0x0
298 // 0: R_MIPS_HI16 _gp_disp
299 // addiu v0,v0,0
300 // 4: R_MIPS_LO16 _gp_disp
301 //
302 // MIPS 64:
303 // lui at,0x0
304 // 14: R_MIPS_GPREL16 main
305 //
306 // Dynamic linker does not know anything about secondary GOTs and cannot
307 // use a regular MIPS mechanism for GOT entries initialization. So we have
308 // to use an approach accepted by other architectures and create dynamic
309 // relocations R_MIPS_REL32 to initialize global entries (and local in case
310 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
311 // requires GOT entries and correspondingly ordered dynamic symbol table
312 // entries to deal with dynamic relocations. To handle this problem
313 // relocation-only section in the primary GOT contains entries for all
314 // symbols referenced in global parts of secondary GOTs. Although the sum
315 // of local and normal global entries of the primary got should be less
316 // than 64K, the size of the primary got (including relocation-only entries
317 // can be greater than 64K, because parts of the primary got that overflow
318 // the 64K limit are used only by the dynamic linker at dynamic link-time
319 // and not by 16-bit gp-relative addressing at run-time.
320 //
321 // For complete multi-GOT description see the following link
322 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
323
324 // Number of "Header" entries.
325 static const unsigned headerEntriesNum = 2;
326
327 uint64_t size = 0;
328
329 // Symbol and addend.
330 using GotEntry = std::pair<Symbol *, int64_t>;
331
332 struct FileGot {
333 InputFile *file = nullptr;
334 size_t startIndex = 0;
335
336 struct PageBlock {
337 size_t firstIndex;
338 size_t count;
PageBlockFileGot::PageBlock339 PageBlock() : firstIndex(0), count(0) {}
340 };
341
342 // Map output sections referenced by MIPS GOT relocations
343 // to the description (index/count) "page" entries allocated
344 // for this section.
345 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
346 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
347 llvm::MapVector<GotEntry, size_t> local16;
348 llvm::MapVector<GotEntry, size_t> local32;
349 llvm::MapVector<Symbol *, size_t> global;
350 llvm::MapVector<Symbol *, size_t> relocs;
351 llvm::MapVector<Symbol *, size_t> tls;
352 // Set of symbols referenced by dynamic TLS relocations.
353 llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
354
355 // Total number of all entries.
356 size_t getEntriesNum() const;
357 // Number of "page" entries.
358 size_t getPageEntriesNum() const;
359 // Number of entries require 16-bit index to access.
360 size_t getIndexedEntriesNum() const;
361 };
362
363 // Container of GOT created for each input file.
364 // After building a final series of GOTs this container
365 // holds primary and secondary GOT's.
366 std::vector<FileGot> gots;
367
368 // Return (and create if necessary) `FileGot`.
369 FileGot &getGot(InputFile &f);
370
371 // Try to merge two GOTs. In case of success the `Dst` contains
372 // result of merging and the function returns true. In case of
373 // overflow the `Dst` is unchanged and the function returns false.
374 bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
375 };
376
377 class GotPltSection final : public SyntheticSection {
378 public:
379 GotPltSection();
380 void addEntry(Symbol &sym);
381 size_t getSize() const override;
382 void writeTo(uint8_t *buf) override;
383 bool isNeeded() const override;
384
385 // Flag to force GotPlt to be in output if we have relocations
386 // that relies on its address.
387 bool hasGotPltOffRel = false;
388
389 private:
390 std::vector<const Symbol *> entries;
391 };
392
393 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
394 // Symbols that will be relocated by Target->IRelativeRel.
395 // On most Targets the IgotPltSection will immediately follow the GotPltSection
396 // on ARM the IgotPltSection will immediately follow the GotSection.
397 class IgotPltSection final : public SyntheticSection {
398 public:
399 IgotPltSection();
400 void addEntry(Symbol &sym);
401 size_t getSize() const override;
402 void writeTo(uint8_t *buf) override;
isNeeded()403 bool isNeeded() const override { return !entries.empty(); }
404
405 private:
406 std::vector<const Symbol *> entries;
407 };
408
409 class StringTableSection final : public SyntheticSection {
410 public:
411 StringTableSection(StringRef name, bool dynamic);
412 unsigned addString(StringRef s, bool hashIt = true);
413 void writeTo(uint8_t *buf) override;
getSize()414 size_t getSize() const override { return size; }
isDynamic()415 bool isDynamic() const { return dynamic; }
416
417 private:
418 const bool dynamic;
419
420 uint64_t size = 0;
421
422 llvm::DenseMap<StringRef, unsigned> stringMap;
423 std::vector<StringRef> strings;
424 };
425
426 class DynamicReloc {
427 public:
DynamicReloc(RelType type,const InputSectionBase * inputSec,uint64_t offsetInSec,bool useSymVA,Symbol * sym,int64_t addend)428 DynamicReloc(RelType type, const InputSectionBase *inputSec,
429 uint64_t offsetInSec, bool useSymVA, Symbol *sym, int64_t addend)
430 : type(type), sym(sym), inputSec(inputSec), offsetInSec(offsetInSec),
431 useSymVA(useSymVA), addend(addend), outputSec(nullptr) {}
432 // This constructor records dynamic relocation settings used by MIPS
433 // multi-GOT implementation. It's to relocate addresses of 64kb pages
434 // lie inside the output section.
DynamicReloc(RelType type,const InputSectionBase * inputSec,uint64_t offsetInSec,const OutputSection * outputSec,int64_t addend)435 DynamicReloc(RelType type, const InputSectionBase *inputSec,
436 uint64_t offsetInSec, const OutputSection *outputSec,
437 int64_t addend)
438 : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec),
439 useSymVA(false), addend(addend), outputSec(outputSec) {}
440
441 uint64_t getOffset() const;
442 uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
443
444 // Computes the addend of the dynamic relocation. Note that this is not the
445 // same as the addend member variable as it also includes the symbol address
446 // if useSymVA is true.
447 int64_t computeAddend() const;
448
449 RelType type;
450
451 Symbol *sym;
452 const InputSectionBase *inputSec = nullptr;
453 uint64_t offsetInSec;
454 // If this member is true, the dynamic relocation will not be against the
455 // symbol but will instead be a relative relocation that simply adds the
456 // load address. This means we need to write the symbol virtual address
457 // plus the original addend as the final relocation addend.
458 bool useSymVA;
459 int64_t addend;
460 const OutputSection *outputSec;
461 };
462
463 template <class ELFT> class DynamicSection final : public SyntheticSection {
464 using Elf_Dyn = typename ELFT::Dyn;
465 using Elf_Rel = typename ELFT::Rel;
466 using Elf_Rela = typename ELFT::Rela;
467 using Elf_Relr = typename ELFT::Relr;
468 using Elf_Shdr = typename ELFT::Shdr;
469 using Elf_Sym = typename ELFT::Sym;
470
471 // finalizeContents() fills this vector with the section contents.
472 std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries;
473
474 public:
475 DynamicSection();
476 void finalizeContents() override;
477 void writeTo(uint8_t *buf) override;
getSize()478 size_t getSize() const override { return size; }
479
480 private:
481 void add(int32_t tag, std::function<uint64_t()> fn);
482 void addInt(int32_t tag, uint64_t val);
483 void addInSec(int32_t tag, InputSection *sec);
484 void addInSecRelative(int32_t tag, InputSection *sec);
485 void addOutSec(int32_t tag, OutputSection *sec);
486 void addSize(int32_t tag, OutputSection *sec);
487 void addSym(int32_t tag, Symbol *sym);
488
489 uint64_t size = 0;
490 };
491
492 class RelocationBaseSection : public SyntheticSection {
493 public:
494 RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag,
495 int32_t sizeDynamicTag);
496 void addReloc(RelType dynType, InputSectionBase *isec, uint64_t offsetInSec,
497 Symbol *sym);
498 // Add a dynamic relocation that might need an addend. This takes care of
499 // writing the addend to the output section if needed.
500 void addReloc(RelType dynType, InputSectionBase *inputSec,
501 uint64_t offsetInSec, Symbol *sym, int64_t addend, RelExpr expr,
502 RelType type);
503 void addReloc(const DynamicReloc &reloc);
isNeeded()504 bool isNeeded() const override { return !relocs.empty(); }
getSize()505 size_t getSize() const override { return relocs.size() * this->entsize; }
getRelativeRelocCount()506 size_t getRelativeRelocCount() const { return numRelativeRelocs; }
507 void finalizeContents() override;
508 int32_t dynamicTag, sizeDynamicTag;
509 std::vector<DynamicReloc> relocs;
510
511 protected:
512 size_t numRelativeRelocs = 0;
513 };
514
515 template <class ELFT>
516 class RelocationSection final : public RelocationBaseSection {
517 using Elf_Rel = typename ELFT::Rel;
518 using Elf_Rela = typename ELFT::Rela;
519
520 public:
521 RelocationSection(StringRef name, bool sort);
522 void writeTo(uint8_t *buf) override;
523
524 private:
525 bool sort;
526 };
527
528 template <class ELFT>
529 class AndroidPackedRelocationSection final : public RelocationBaseSection {
530 using Elf_Rel = typename ELFT::Rel;
531 using Elf_Rela = typename ELFT::Rela;
532
533 public:
534 AndroidPackedRelocationSection(StringRef name);
535
536 bool updateAllocSize() override;
getSize()537 size_t getSize() const override { return relocData.size(); }
writeTo(uint8_t * buf)538 void writeTo(uint8_t *buf) override {
539 memcpy(buf, relocData.data(), relocData.size());
540 }
541
542 private:
543 SmallVector<char, 0> relocData;
544 };
545
546 struct RelativeReloc {
getOffsetRelativeReloc547 uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
548
549 const InputSectionBase *inputSec;
550 uint64_t offsetInSec;
551 };
552
553 class RelrBaseSection : public SyntheticSection {
554 public:
555 RelrBaseSection();
isNeeded()556 bool isNeeded() const override { return !relocs.empty(); }
557 std::vector<RelativeReloc> relocs;
558 };
559
560 // RelrSection is used to encode offsets for relative relocations.
561 // Proposal for adding SHT_RELR sections to generic-abi is here:
562 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
563 // For more details, see the comment in RelrSection::updateAllocSize().
564 template <class ELFT> class RelrSection final : public RelrBaseSection {
565 using Elf_Relr = typename ELFT::Relr;
566
567 public:
568 RelrSection();
569
570 bool updateAllocSize() override;
getSize()571 size_t getSize() const override { return relrRelocs.size() * this->entsize; }
writeTo(uint8_t * buf)572 void writeTo(uint8_t *buf) override {
573 memcpy(buf, relrRelocs.data(), getSize());
574 }
575
576 private:
577 std::vector<Elf_Relr> relrRelocs;
578 };
579
580 struct SymbolTableEntry {
581 Symbol *sym;
582 size_t strTabOffset;
583 };
584
585 class SymbolTableBaseSection : public SyntheticSection {
586 public:
587 SymbolTableBaseSection(StringTableSection &strTabSec);
588 void finalizeContents() override;
getSize()589 size_t getSize() const override { return getNumSymbols() * entsize; }
590 void addSymbol(Symbol *sym);
getNumSymbols()591 unsigned getNumSymbols() const { return symbols.size() + 1; }
592 size_t getSymbolIndex(Symbol *sym);
getSymbols()593 ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
594
595 protected:
596 void sortSymTabSymbols();
597
598 // A vector of symbols and their string table offsets.
599 std::vector<SymbolTableEntry> symbols;
600
601 StringTableSection &strTabSec;
602
603 llvm::once_flag onceFlag;
604 llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
605 llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
606 };
607
608 template <class ELFT>
609 class SymbolTableSection final : public SymbolTableBaseSection {
610 using Elf_Sym = typename ELFT::Sym;
611
612 public:
613 SymbolTableSection(StringTableSection &strTabSec);
614 void writeTo(uint8_t *buf) override;
615 };
616
617 class SymtabShndxSection final : public SyntheticSection {
618 public:
619 SymtabShndxSection();
620
621 void writeTo(uint8_t *buf) override;
622 size_t getSize() const override;
623 bool isNeeded() const override;
624 void finalizeContents() override;
625 };
626
627 // Outputs GNU Hash section. For detailed explanation see:
628 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
629 class GnuHashTableSection final : public SyntheticSection {
630 public:
631 GnuHashTableSection();
632 void finalizeContents() override;
633 void writeTo(uint8_t *buf) override;
getSize()634 size_t getSize() const override { return size; }
635
636 // Adds symbols to the hash table.
637 // Sorts the input to satisfy GNU hash section requirements.
638 void addSymbols(std::vector<SymbolTableEntry> &symbols);
639
640 private:
641 // See the comment in writeBloomFilter.
642 enum { Shift2 = 26 };
643
644 void writeBloomFilter(uint8_t *buf);
645 void writeHashTable(uint8_t *buf);
646
647 struct Entry {
648 Symbol *sym;
649 size_t strTabOffset;
650 uint32_t hash;
651 uint32_t bucketIdx;
652 };
653
654 std::vector<Entry> symbols;
655 size_t maskWords;
656 size_t nBuckets = 0;
657 size_t size = 0;
658 };
659
660 class HashTableSection final : public SyntheticSection {
661 public:
662 HashTableSection();
663 void finalizeContents() override;
664 void writeTo(uint8_t *buf) override;
getSize()665 size_t getSize() const override { return size; }
666
667 private:
668 size_t size = 0;
669 };
670
671 // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT
672 // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily
673 // at runtime.
674 //
675 // On PowerPC, this section contains lazy symbol resolvers. A branch instruction
676 // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a
677 // lazy symbol resolver.
678 //
679 // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs.
680 // A call instruction jumps to a .plt.sec entry, which will then jump to the
681 // target (BIND_NOW) or a .plt entry.
682 class PltSection : public SyntheticSection {
683 public:
684 PltSection();
685 void writeTo(uint8_t *buf) override;
686 size_t getSize() const override;
687 bool isNeeded() const override;
688 void addSymbols();
689 void addEntry(Symbol &sym);
getNumEntries()690 size_t getNumEntries() const { return entries.size(); }
691
692 size_t headerSize;
693
694 std::vector<const Symbol *> entries;
695 };
696
697 // Used for non-preemptible ifuncs. It does not have a header. Each entry is
698 // associated with an IRELATIVE relocation, which will be resolved eagerly at
699 // runtime. PltSection can only contain entries associated with JUMP_SLOT
700 // relocations, so IPLT entries are in a separate section.
701 class IpltSection final : public SyntheticSection {
702 std::vector<const Symbol *> entries;
703
704 public:
705 IpltSection();
706 void writeTo(uint8_t *buf) override;
707 size_t getSize() const override;
isNeeded()708 bool isNeeded() const override { return !entries.empty(); }
709 void addSymbols();
710 void addEntry(Symbol &sym);
711 };
712
713 class PPC32GlinkSection : public PltSection {
714 public:
715 PPC32GlinkSection();
716 void writeTo(uint8_t *buf) override;
717 size_t getSize() const override;
718
719 std::vector<const Symbol *> canonical_plts;
720 static constexpr size_t footerSize = 64;
721 };
722
723 // This is x86-only.
724 class IBTPltSection : public SyntheticSection {
725 public:
726 IBTPltSection();
727 void writeTo(uint8_t *Buf) override;
728 size_t getSize() const override;
729 };
730
731 class GdbIndexSection final : public SyntheticSection {
732 public:
733 struct AddressEntry {
734 InputSection *section;
735 uint64_t lowAddress;
736 uint64_t highAddress;
737 uint32_t cuIndex;
738 };
739
740 struct CuEntry {
741 uint64_t cuOffset;
742 uint64_t cuLength;
743 };
744
745 struct NameAttrEntry {
746 llvm::CachedHashStringRef name;
747 uint32_t cuIndexAndAttrs;
748 };
749
750 struct GdbChunk {
751 InputSection *sec;
752 std::vector<AddressEntry> addressAreas;
753 std::vector<CuEntry> compilationUnits;
754 };
755
756 struct GdbSymbol {
757 llvm::CachedHashStringRef name;
758 std::vector<uint32_t> cuVector;
759 uint32_t nameOff;
760 uint32_t cuVectorOff;
761 };
762
763 GdbIndexSection();
764 template <typename ELFT> static GdbIndexSection *create();
765 void writeTo(uint8_t *buf) override;
getSize()766 size_t getSize() const override { return size; }
767 bool isNeeded() const override;
768
769 private:
770 struct GdbIndexHeader {
771 llvm::support::ulittle32_t version;
772 llvm::support::ulittle32_t cuListOff;
773 llvm::support::ulittle32_t cuTypesOff;
774 llvm::support::ulittle32_t addressAreaOff;
775 llvm::support::ulittle32_t symtabOff;
776 llvm::support::ulittle32_t constantPoolOff;
777 };
778
779 void initOutputSize();
780 size_t computeSymtabSize() const;
781
782 // Each chunk contains information gathered from debug sections of a
783 // single object file.
784 std::vector<GdbChunk> chunks;
785
786 // A symbol table for this .gdb_index section.
787 std::vector<GdbSymbol> symbols;
788
789 size_t size;
790 };
791
792 // --eh-frame-hdr option tells linker to construct a header for all the
793 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
794 // and also to a PT_GNU_EH_FRAME segment.
795 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
796 // calling dl_iterate_phdr.
797 // This section contains a lookup table for quick binary search of FDEs.
798 // Detailed info about internals can be found in Ian Lance Taylor's blog:
799 // http://www.airs.com/blog/archives/460 (".eh_frame")
800 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
801 class EhFrameHeader final : public SyntheticSection {
802 public:
803 EhFrameHeader();
804 void write();
805 void writeTo(uint8_t *buf) override;
806 size_t getSize() const override;
807 bool isNeeded() const override;
808 };
809
810 // For more information about .gnu.version and .gnu.version_r see:
811 // https://www.akkadia.org/drepper/symbol-versioning
812
813 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
814 // contain symbol version definitions. The number of entries in this section
815 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
816 // The section shall contain an array of Elf_Verdef structures, optionally
817 // followed by an array of Elf_Verdaux structures.
818 class VersionDefinitionSection final : public SyntheticSection {
819 public:
820 VersionDefinitionSection();
821 void finalizeContents() override;
822 size_t getSize() const override;
823 void writeTo(uint8_t *buf) override;
824
825 private:
826 enum { EntrySize = 28 };
827 void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
828 StringRef getFileDefName();
829
830 unsigned fileDefNameOff;
831 std::vector<unsigned> verDefNameOffs;
832 };
833
834 // The .gnu.version section specifies the required version of each symbol in the
835 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
836 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
837 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
838 // The values 0 and 1 are reserved. All other values are used for versions in
839 // the own object or in any of the dependencies.
840 class VersionTableSection final : public SyntheticSection {
841 public:
842 VersionTableSection();
843 void finalizeContents() override;
844 size_t getSize() const override;
845 void writeTo(uint8_t *buf) override;
846 bool isNeeded() const override;
847 };
848
849 // The .gnu.version_r section defines the version identifiers used by
850 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
851 // Elf_Verneed specifies the version requirements for a single DSO, and contains
852 // a reference to a linked list of Elf_Vernaux data structures which define the
853 // mapping from version identifiers to version names.
854 template <class ELFT>
855 class VersionNeedSection final : public SyntheticSection {
856 using Elf_Verneed = typename ELFT::Verneed;
857 using Elf_Vernaux = typename ELFT::Vernaux;
858
859 struct Vernaux {
860 uint64_t hash;
861 uint32_t verneedIndex;
862 uint64_t nameStrTab;
863 };
864
865 struct Verneed {
866 uint64_t nameStrTab;
867 std::vector<Vernaux> vernauxs;
868 };
869
870 std::vector<Verneed> verneeds;
871
872 public:
873 VersionNeedSection();
874 void finalizeContents() override;
875 void writeTo(uint8_t *buf) override;
876 size_t getSize() const override;
877 bool isNeeded() const override;
878 };
879
880 // MergeSyntheticSection is a class that allows us to put mergeable sections
881 // with different attributes in a single output sections. To do that
882 // we put them into MergeSyntheticSection synthetic input sections which are
883 // attached to regular output sections.
884 class MergeSyntheticSection : public SyntheticSection {
885 public:
886 void addSection(MergeInputSection *ms);
887 std::vector<MergeInputSection *> sections;
888
889 protected:
MergeSyntheticSection(StringRef name,uint32_t type,uint64_t flags,uint32_t alignment)890 MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
891 uint32_t alignment)
892 : SyntheticSection(flags, type, alignment, name) {}
893 };
894
895 class MergeTailSection final : public MergeSyntheticSection {
896 public:
897 MergeTailSection(StringRef name, uint32_t type, uint64_t flags,
898 uint32_t alignment);
899
900 size_t getSize() const override;
901 void writeTo(uint8_t *buf) override;
902 void finalizeContents() override;
903
904 private:
905 llvm::StringTableBuilder builder;
906 };
907
908 class MergeNoTailSection final : public MergeSyntheticSection {
909 public:
MergeNoTailSection(StringRef name,uint32_t type,uint64_t flags,uint32_t alignment)910 MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags,
911 uint32_t alignment)
912 : MergeSyntheticSection(name, type, flags, alignment) {}
913
getSize()914 size_t getSize() const override { return size; }
915 void writeTo(uint8_t *buf) override;
916 void finalizeContents() override;
917
918 private:
919 // We use the most significant bits of a hash as a shard ID.
920 // The reason why we don't want to use the least significant bits is
921 // because DenseMap also uses lower bits to determine a bucket ID.
922 // If we use lower bits, it significantly increases the probability of
923 // hash collisons.
getShardId(uint32_t hash)924 size_t getShardId(uint32_t hash) {
925 assert((hash >> 31) == 0);
926 return hash >> (31 - llvm::countTrailingZeros(numShards));
927 }
928
929 // Section size
930 size_t size;
931
932 // String table contents
933 constexpr static size_t numShards = 32;
934 std::vector<llvm::StringTableBuilder> shards;
935 size_t shardOffsets[numShards];
936 };
937
938 // .MIPS.abiflags section.
939 template <class ELFT>
940 class MipsAbiFlagsSection final : public SyntheticSection {
941 using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
942
943 public:
944 static MipsAbiFlagsSection *create();
945
946 MipsAbiFlagsSection(Elf_Mips_ABIFlags flags);
getSize()947 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
948 void writeTo(uint8_t *buf) override;
949
950 private:
951 Elf_Mips_ABIFlags flags;
952 };
953
954 // .MIPS.options section.
955 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
956 using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
957 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
958
959 public:
960 static MipsOptionsSection *create();
961
962 MipsOptionsSection(Elf_Mips_RegInfo reginfo);
963 void writeTo(uint8_t *buf) override;
964
getSize()965 size_t getSize() const override {
966 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
967 }
968
969 private:
970 Elf_Mips_RegInfo reginfo;
971 };
972
973 // MIPS .reginfo section.
974 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
975 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
976
977 public:
978 static MipsReginfoSection *create();
979
980 MipsReginfoSection(Elf_Mips_RegInfo reginfo);
getSize()981 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
982 void writeTo(uint8_t *buf) override;
983
984 private:
985 Elf_Mips_RegInfo reginfo;
986 };
987
988 // This is a MIPS specific section to hold a space within the data segment
989 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
990 // See "Dynamic section" in Chapter 5 in the following document:
991 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
992 class MipsRldMapSection : public SyntheticSection {
993 public:
994 MipsRldMapSection();
getSize()995 size_t getSize() const override { return config->wordsize; }
writeTo(uint8_t * buf)996 void writeTo(uint8_t *buf) override {}
997 };
998
999 // Representation of the combined .ARM.Exidx input sections. We process these
1000 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries
1001 // and add terminating sentinel entries.
1002 //
1003 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
1004 // a table that the unwinder can derive (Addresses are encoded as offsets from
1005 // table):
1006 // | Address of function | Unwind instructions for function |
1007 // where the unwind instructions are either a small number of unwind or the
1008 // special EXIDX_CANTUNWIND entry representing no unwinding information.
1009 // When an exception is thrown from an address A, the unwinder searches the
1010 // table for the closest table entry with Address of function <= A. This means
1011 // that for two consecutive table entries:
1012 // | A1 | U1 |
1013 // | A2 | U2 |
1014 // The range of addresses described by U1 is [A1, A2)
1015 //
1016 // There are two cases where we need a linker generated table entry to fixup
1017 // the address ranges in the table
1018 // Case 1:
1019 // - A sentinel entry added with an address higher than all
1020 // executable sections. This was needed to work around libunwind bug pr31091.
1021 // - After address assignment we need to find the highest addressed executable
1022 // section and use the limit of that section so that the unwinder never
1023 // matches it.
1024 // Case 2:
1025 // - InputSections without a .ARM.exidx section (usually from Assembly)
1026 // need a table entry so that they terminate the range of the previously
1027 // function. This is pr40277.
1028 //
1029 // Instead of storing pointers to the .ARM.exidx InputSections from
1030 // InputObjects, we store pointers to the executable sections that need
1031 // .ARM.exidx sections. We can then use the dependentSections of these to
1032 // either find the .ARM.exidx section or know that we need to generate one.
1033 class ARMExidxSyntheticSection : public SyntheticSection {
1034 public:
1035 ARMExidxSyntheticSection();
1036
1037 // Add an input section to the ARMExidxSyntheticSection. Returns whether the
1038 // section needs to be removed from the main input section list.
1039 bool addSection(InputSection *isec);
1040
getSize()1041 size_t getSize() const override { return size; }
1042 void writeTo(uint8_t *buf) override;
1043 bool isNeeded() const override;
1044 // Sort and remove duplicate entries.
1045 void finalizeContents() override;
1046 InputSection *getLinkOrderDep() const;
1047
1048 static bool classof(const SectionBase *d);
1049
1050 // Links to the ARMExidxSections so we can transfer the relocations once the
1051 // layout is known.
1052 std::vector<InputSection *> exidxSections;
1053
1054 private:
1055 size_t size = 0;
1056
1057 // Instead of storing pointers to the .ARM.exidx InputSections from
1058 // InputObjects, we store pointers to the executable sections that need
1059 // .ARM.exidx sections. We can then use the dependentSections of these to
1060 // either find the .ARM.exidx section or know that we need to generate one.
1061 std::vector<InputSection *> executableSections;
1062
1063 // The executable InputSection with the highest address to use for the
1064 // sentinel. We store separately from ExecutableSections as merging of
1065 // duplicate entries may mean this InputSection is removed from
1066 // ExecutableSections.
1067 InputSection *sentinel = nullptr;
1068 };
1069
1070 // A container for one or more linker generated thunks. Instances of these
1071 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1072 class ThunkSection : public SyntheticSection {
1073 public:
1074 // ThunkSection in OS, with desired outSecOff of Off
1075 ThunkSection(OutputSection *os, uint64_t off);
1076
1077 // Add a newly created Thunk to this container:
1078 // Thunk is given offset from start of this InputSection
1079 // Thunk defines a symbol in this InputSection that can be used as target
1080 // of a relocation
1081 void addThunk(Thunk *t);
1082 size_t getSize() const override;
1083 void writeTo(uint8_t *buf) override;
1084 InputSection *getTargetInputSection() const;
1085 bool assignOffsets();
1086
1087 // When true, round up reported size of section to 4 KiB. See comment
1088 // in addThunkSection() for more details.
1089 bool roundUpSizeForErrata = false;
1090
1091 private:
1092 std::vector<Thunk *> thunks;
1093 size_t size = 0;
1094 };
1095
1096 // Used to compute outSecOff of .got2 in each object file. This is needed to
1097 // synthesize PLT entries for PPC32 Secure PLT ABI.
1098 class PPC32Got2Section final : public SyntheticSection {
1099 public:
1100 PPC32Got2Section();
getSize()1101 size_t getSize() const override { return 0; }
1102 bool isNeeded() const override;
1103 void finalizeContents() override;
writeTo(uint8_t * buf)1104 void writeTo(uint8_t *buf) override {}
1105 };
1106
1107 // This section is used to store the addresses of functions that are called
1108 // in range-extending thunks on PowerPC64. When producing position dependent
1109 // code the addresses are link-time constants and the table is written out to
1110 // the binary. When producing position-dependent code the table is allocated and
1111 // filled in by the dynamic linker.
1112 class PPC64LongBranchTargetSection final : public SyntheticSection {
1113 public:
1114 PPC64LongBranchTargetSection();
1115 uint64_t getEntryVA(const Symbol *sym, int64_t addend);
1116 llvm::Optional<uint32_t> addEntry(const Symbol *sym, int64_t addend);
1117 size_t getSize() const override;
1118 void writeTo(uint8_t *buf) override;
1119 bool isNeeded() const override;
finalizeContents()1120 void finalizeContents() override { finalized = true; }
1121
1122 private:
1123 std::vector<std::pair<const Symbol *, int64_t>> entries;
1124 llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index;
1125 bool finalized = false;
1126 };
1127
1128 template <typename ELFT>
1129 class PartitionElfHeaderSection : public SyntheticSection {
1130 public:
1131 PartitionElfHeaderSection();
1132 size_t getSize() const override;
1133 void writeTo(uint8_t *buf) override;
1134 };
1135
1136 template <typename ELFT>
1137 class PartitionProgramHeadersSection : public SyntheticSection {
1138 public:
1139 PartitionProgramHeadersSection();
1140 size_t getSize() const override;
1141 void writeTo(uint8_t *buf) override;
1142 };
1143
1144 class PartitionIndexSection : public SyntheticSection {
1145 public:
1146 PartitionIndexSection();
1147 size_t getSize() const override;
1148 void finalizeContents() override;
1149 void writeTo(uint8_t *buf) override;
1150 };
1151
1152 InputSection *createInterpSection();
1153 MergeInputSection *createCommentSection();
1154 MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type,
1155 uint64_t flags, uint32_t alignment);
1156 template <class ELFT> void splitSections();
1157
1158 template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
1159 template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
1160
1161 Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
1162 uint64_t size, InputSectionBase §ion);
1163
1164 void addVerneed(Symbol *ss);
1165
1166 // Linker generated per-partition sections.
1167 struct Partition {
1168 StringRef name;
1169 uint64_t nameStrTab;
1170
1171 SyntheticSection *elfHeader;
1172 SyntheticSection *programHeaders;
1173 std::vector<PhdrEntry *> phdrs;
1174
1175 ARMExidxSyntheticSection *armExidx;
1176 BuildIdSection *buildId;
1177 SyntheticSection *dynamic;
1178 StringTableSection *dynStrTab;
1179 SymbolTableBaseSection *dynSymTab;
1180 EhFrameHeader *ehFrameHdr;
1181 EhFrameSection *ehFrame;
1182 GnuHashTableSection *gnuHashTab;
1183 HashTableSection *hashTab;
1184 RelocationBaseSection *relaDyn;
1185 RelrBaseSection *relrDyn;
1186 VersionDefinitionSection *verDef;
1187 SyntheticSection *verNeed;
1188 VersionTableSection *verSym;
1189
getNumberPartition1190 unsigned getNumber() const { return this - &partitions[0] + 1; }
1191 };
1192
1193 extern Partition *mainPart;
1194
getPartition()1195 inline Partition &SectionBase::getPartition() const {
1196 assert(isLive());
1197 return partitions[partition - 1];
1198 }
1199
1200 // Linker generated sections which can be used as inputs and are not specific to
1201 // a partition.
1202 struct InStruct {
1203 InputSection *attributes;
1204 BssSection *bss;
1205 BssSection *bssRelRo;
1206 GotSection *got;
1207 GotPltSection *gotPlt;
1208 IgotPltSection *igotPlt;
1209 PPC64LongBranchTargetSection *ppc64LongBranchTarget;
1210 MipsGotSection *mipsGot;
1211 MipsRldMapSection *mipsRldMap;
1212 SyntheticSection *partEnd;
1213 SyntheticSection *partIndex;
1214 PltSection *plt;
1215 IpltSection *iplt;
1216 PPC32Got2Section *ppc32Got2;
1217 IBTPltSection *ibtPlt;
1218 RelocationBaseSection *relaPlt;
1219 RelocationBaseSection *relaIplt;
1220 StringTableSection *shStrTab;
1221 StringTableSection *strTab;
1222 SymbolTableBaseSection *symTab;
1223 SymtabShndxSection *symTabShndx;
1224 };
1225
1226 extern InStruct in;
1227
1228 } // namespace elf
1229 } // namespace lld
1230
1231 #endif
1232