Coverage Report

Created: 2018-06-25 02:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/SyntheticSections.h
Line
Count
Source (jump to first uncovered line)
1
//===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// Synthetic sections represent chunks of linker-created data. If you
11
// need to create a chunk of data that to be included in some section
12
// in the result, you probably want to create that as a synthetic section.
13
//
14
// Synthetic sections are designed as input sections as opposed to
15
// output sections because we want to allow them to be manipulated
16
// using linker scripts just like other input sections from regular
17
// files.
18
//
19
//===----------------------------------------------------------------------===//
20
21
#ifndef LLD_ELF_SYNTHETIC_SECTION_H
22
#define LLD_ELF_SYNTHETIC_SECTION_H
23
24
#include "EhFrame.h"
25
#include "GdbIndex.h"
26
#include "InputSection.h"
27
#include "llvm/ADT/MapVector.h"
28
#include "llvm/MC/StringTableBuilder.h"
29
#include <functional>
30
31
namespace lld {
32
namespace elf {
33
class Defined;
34
class SharedSymbol;
35
36
class SyntheticSection : public InputSection {
37
public:
38
  SyntheticSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
39
                   StringRef Name)
40
      : InputSection(nullptr, Flags, Type, Alignment, {}, Name,
41
42.6k
                     InputSectionBase::Synthetic) {
42
42.6k
    this->Live = true;
43
42.6k
  }
44
45
42.6k
  virtual ~SyntheticSection() = default;
46
  virtual void writeTo(uint8_t *Buf) = 0;
47
  virtual size_t getSize() const = 0;
48
5.58k
  virtual void finalizeContents() {}
49
  // If the section has the SHF_ALLOC flag and the size may be changed if
50
  // thunks are added, update the section size.
51
511
  virtual bool updateAllocSize() { return false; }
52
  // If any additional finalization of contents are needed post thunk creation.
53
0
  virtual void postThunkContents() {}
54
44.0k
  virtual bool empty() const { return false; }
55
56
1.17M
  static bool classof(const SectionBase *D) {
57
1.17M
    return D->kind() == InputSectionBase::Synthetic;
58
1.17M
  }
59
};
60
61
struct CieRecord {
62
  EhSectionPiece *Cie = nullptr;
63
  std::vector<EhSectionPiece *> Fdes;
64
};
65
66
// Section for .eh_frame.
67
class EhFrameSection final : public SyntheticSection {
68
public:
69
  EhFrameSection();
70
  void writeTo(uint8_t *Buf) override;
71
  void finalizeContents() override;
72
6.10k
  bool empty() const override { return Sections.empty(); }
73
171
  size_t getSize() const override { return Size; }
74
75
  template <class ELFT> void addSection(InputSectionBase *S);
76
77
  std::vector<EhInputSection *> Sections;
78
  size_t NumFdes = 0;
79
80
  struct FdeData {
81
    uint32_t Pc;
82
    uint32_t FdeVA;
83
  };
84
85
  std::vector<FdeData> getFdeData() const;
86
4
  ArrayRef<CieRecord *> getCieRecords() const { return CieRecords; }
87
88
private:
89
  // This is used only when parsing EhInputSection. We keep it here to avoid
90
  // allocating one for each EhInputSection.
91
  llvm::DenseMap<size_t, CieRecord *> OffsetToCie;
92
93
  uint64_t Size = 0;
94
95
  template <class ELFT, class RelTy>
96
  void addSectionAux(EhInputSection *S, llvm::ArrayRef<RelTy> Rels);
97
98
  template <class ELFT, class RelTy>
99
  CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
100
101
  template <class ELFT, class RelTy>
102
  bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
103
104
  uint64_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc) const;
105
106
  std::vector<CieRecord *> CieRecords;
107
108
  // CIE records are uniquified by their contents and personality functions.
109
  llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> CieMap;
110
};
111
112
class GotSection : public SyntheticSection {
113
public:
114
  GotSection();
115
482
  size_t getSize() const override { return Size; }
116
  void finalizeContents() override;
117
  bool empty() const override;
118
  void writeTo(uint8_t *Buf) override;
119
120
  void addEntry(Symbol &Sym);
121
  bool addDynTlsEntry(Symbol &Sym);
122
  bool addTlsIndex();
123
  uint64_t getGlobalDynAddr(const Symbol &B) const;
124
  uint64_t getGlobalDynOffset(const Symbol &B) const;
125
126
5
  uint64_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
127
31
  uint32_t getTlsIndexOff() const { return TlsIndexOff; }
128
129
  // Flag to force GOT to be in output if we have relocations
130
  // that relies on its address.
131
  bool HasGotOffRel = false;
132
133
protected:
134
  size_t NumEntries = 0;
135
  uint32_t TlsIndexOff = -1;
136
  uint64_t Size = 0;
137
};
138
139
// .note.gnu.build-id section.
140
class BuildIdSection : public SyntheticSection {
141
  // First 16 bytes are a header.
142
  static const unsigned HeaderSize = 16;
143
144
public:
145
  BuildIdSection();
146
  void writeTo(uint8_t *Buf) override;
147
32
  size_t getSize() const override { return HeaderSize + HashSize; }
148
  void writeBuildId(llvm::ArrayRef<uint8_t> Buf);
149
150
private:
151
  void computeHash(llvm::ArrayRef<uint8_t> Buf,
152
                   std::function<void(uint8_t *, ArrayRef<uint8_t>)> Hash);
153
154
  size_t HashSize;
155
  uint8_t *HashBuf;
156
};
157
158
// BssSection is used to reserve space for copy relocations and common symbols.
159
// We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
160
// that are used for writable symbols, read-only symbols and common symbols,
161
// respectively.
162
class BssSection final : public SyntheticSection {
163
public:
164
  BssSection(StringRef Name, uint64_t Size, uint32_t Alignment);
165
0
  void writeTo(uint8_t *) override {}
166
8.29k
  bool empty() const override { return getSize() == 0; }
167
8.62k
  size_t getSize() const override { return Size; }
168
169
372
  static bool classof(const SectionBase *S) { return S->Bss; }
170
  uint64_t Size;
171
};
172
173
class MipsGotSection final : public SyntheticSection {
174
public:
175
  MipsGotSection();
176
  void writeTo(uint8_t *Buf) override;
177
648
  size_t getSize() const override { return Size; }
178
  bool updateAllocSize() override;
179
  void finalizeContents() override;
180
  bool empty() const override;
181
182
  // Join separate GOTs built for each input file to generate
183
  // primary and optional multiple secondary GOTs.
184
  template <class ELFT> void build();
185
186
  void addEntry(InputFile &File, Symbol &Sym, int64_t Addend, RelExpr Expr);
187
  void addDynTlsEntry(InputFile &File, Symbol &Sym);
188
  void addTlsIndex(InputFile &File);
189
190
  uint64_t getPageEntryOffset(const InputFile *F, const Symbol &S,
191
                              int64_t Addend) const;
192
  uint64_t getSymEntryOffset(const InputFile *F, const Symbol &S,
193
                             int64_t Addend) const;
194
  uint64_t getGlobalDynOffset(const InputFile *F, const Symbol &S) const;
195
  uint64_t getTlsIndexOffset(const InputFile *F) const;
196
197
  // Returns the symbol which corresponds to the first entry of the global part
198
  // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
199
  // table properties.
200
  // Returns nullptr if the global part is empty.
201
  const Symbol *getFirstGlobalEntry() const;
202
203
  // Returns the number of entries in the local part of GOT including
204
  // the number of reserved entries.
205
  unsigned getLocalEntriesNum() const;
206
207
  // Return _gp value for primary GOT (nullptr) or particular input file.
208
  uint64_t getGp(const InputFile *F = nullptr) const;
209
210
private:
211
  // MIPS GOT consists of three parts: local, global and tls. Each part
212
  // contains different types of entries. Here is a layout of GOT:
213
  // - Header entries                |
214
  // - Page entries                  |   Local part
215
  // - Local entries (16-bit access) |
216
  // - Local entries (32-bit access) |
217
  // - Normal global entries         ||  Global part
218
  // - Reloc-only global entries     ||
219
  // - TLS entries                   ||| TLS part
220
  //
221
  // Header:
222
  //   Two entries hold predefined value 0x0 and 0x80000000.
223
  // Page entries:
224
  //   These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
225
  //   relocation against local symbols. They are initialized by higher 16-bit
226
  //   of the corresponding symbol's value. So each 64kb of address space
227
  //   requires a single GOT entry.
228
  // Local entries (16-bit access):
229
  //   These entries created by GOT relocations against global non-preemptible
230
  //   symbols so dynamic linker is not necessary to resolve the symbol's
231
  //   values. "16-bit access" means that corresponding relocations address
232
  //   GOT using 16-bit index. Each unique Symbol-Addend pair has its own
233
  //   GOT entry.
234
  // Local entries (32-bit access):
235
  //   These entries are the same as above but created by relocations which
236
  //   address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
237
  // Normal global entries:
238
  //   These entries created by GOT relocations against preemptible global
239
  //   symbols. They need to be initialized by dynamic linker and they ordered
240
  //   exactly as the corresponding entries in the dynamic symbols table.
241
  // Reloc-only global entries:
242
  //   These entries created for symbols that are referenced by dynamic
243
  //   relocations R_MIPS_REL32. These entries are not accessed with gp-relative
244
  //   addressing, but MIPS ABI requires that these entries be present in GOT.
245
  // TLS entries:
246
  //   Entries created by TLS relocations.
247
  //
248
  // If the sum of local, global and tls entries is less than 64K only single
249
  // got is enough. Otherwise, multi-got is created. Series of primary and
250
  // multiple secondary GOTs have the following layout:
251
  // - Primary GOT
252
  //     Header
253
  //     Local entries
254
  //     Global entries
255
  //     Relocation only entries
256
  //     TLS entries
257
  //
258
  // - Secondary GOT
259
  //     Local entries
260
  //     Global entries
261
  //     TLS entries
262
  // ...
263
  //
264
  // All GOT entries required by relocations from a single input file entirely
265
  // belong to either primary or one of secondary GOTs. To reference GOT entries
266
  // each GOT has its own _gp value points to the "middle" of the GOT.
267
  // In the code this value loaded to the register which is used for GOT access.
268
  //
269
  // MIPS 32 function's prologue:
270
  //   lui     v0,0x0
271
  //   0: R_MIPS_HI16  _gp_disp
272
  //   addiu   v0,v0,0
273
  //   4: R_MIPS_LO16  _gp_disp
274
  //
275
  // MIPS 64:
276
  //   lui     at,0x0
277
  //   14: R_MIPS_GPREL16  main
278
  //
279
  // Dynamic linker does not know anything about secondary GOTs and cannot
280
  // use a regular MIPS mechanism for GOT entries initialization. So we have
281
  // to use an approach accepted by other architectures and create dynamic
282
  // relocations R_MIPS_REL32 to initialize global entries (and local in case
283
  // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
284
  // requires GOT entries and correspondingly ordered dynamic symbol table
285
  // entries to deal with dynamic relocations. To handle this problem
286
  // relocation-only section in the primary GOT contains entries for all
287
  // symbols referenced in global parts of secondary GOTs. Although the sum
288
  // of local and normal global entries of the primary got should be less
289
  // than 64K, the size of the primary got (including relocation-only entries
290
  // can be greater than 64K, because parts of the primary got that overflow
291
  // the 64K limit are used only by the dynamic linker at dynamic link-time
292
  // and not by 16-bit gp-relative addressing at run-time.
293
  //
294
  // For complete multi-GOT description see the following link
295
  // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
296
297
  // Number of "Header" entries.
298
  static const unsigned HeaderEntriesNum = 2;
299
300
  uint64_t Size = 0;
301
302
  size_t LocalEntriesNum = 0;
303
304
  // Symbol and addend.
305
  typedef std::pair<Symbol *, int64_t> GotEntry;
306
307
  struct FileGot {
308
    InputFile *File = nullptr;
309
    size_t StartIndex = 0;
310
311
    struct PageBlock {
312
      size_t FirstIndex = 0;
313
      size_t Count = 0;
314
    };
315
316
    // Map output sections referenced by MIPS GOT relocations
317
    // to the description (index/count) "page" entries allocated
318
    // for this section.
319
    llvm::SmallMapVector<const OutputSection *, PageBlock, 16> PagesMap;
320
    // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
321
    llvm::MapVector<GotEntry, size_t> Local16;
322
    llvm::MapVector<GotEntry, size_t> Local32;
323
    llvm::MapVector<Symbol *, size_t> Global;
324
    llvm::MapVector<Symbol *, size_t> Relocs;
325
    llvm::MapVector<Symbol *, size_t> Tls;
326
    // Set of symbols referenced by dynamic TLS relocations.
327
    llvm::MapVector<Symbol *, size_t> DynTlsSymbols;
328
329
    // Total number of all entries.
330
    size_t getEntriesNum() const;
331
    // Number of "page" entries.
332
    size_t getPageEntriesNum() const;
333
    // Number of entries require 16-bit index to access.
334
    size_t getIndexedEntriesNum() const;
335
336
    bool isOverflow() const;
337
  };
338
339
  // Container of GOT created for each input file.
340
  // After building a final series of GOTs this container
341
  // holds primary and secondary GOT's.
342
  std::vector<FileGot> Gots;
343
344
  // Return (and create if necessary) `FileGot`.
345
  FileGot &getGot(InputFile &F);
346
347
  // Try to merge two GOTs. In case of success the `Dst` contains
348
  // result of merging and the function returns true. In case of
349
  // ovwerflow the `Dst` is unchanged and the function returns false.
350
  bool tryMergeGots(FileGot & Dst, FileGot & Src, bool IsPrimary);
351
};
352
353
class GotPltSection final : public SyntheticSection {
354
public:
355
  GotPltSection();
356
  void addEntry(Symbol &Sym);
357
  size_t getSize() const override;
358
  void writeTo(uint8_t *Buf) override;
359
  bool empty() const override;
360
361
private:
362
  std::vector<const Symbol *> Entries;
363
};
364
365
// The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
366
// Symbols that will be relocated by Target->IRelativeRel.
367
// On most Targets the IgotPltSection will immediately follow the GotPltSection
368
// on ARM the IgotPltSection will immediately follow the GotSection.
369
class IgotPltSection final : public SyntheticSection {
370
public:
371
  IgotPltSection();
372
  void addEntry(Symbol &Sym);
373
  size_t getSize() const override;
374
  void writeTo(uint8_t *Buf) override;
375
4.09k
  bool empty() const override { return Entries.empty(); }
376
377
private:
378
  std::vector<const Symbol *> Entries;
379
};
380
381
class StringTableSection final : public SyntheticSection {
382
public:
383
  StringTableSection(StringRef Name, bool Dynamic);
384
  unsigned addString(StringRef S, bool HashIt = true);
385
  void writeTo(uint8_t *Buf) override;
386
13.9k
  size_t getSize() const override { return Size; }
387
9.72k
  bool isDynamic() const { return Dynamic; }
388
389
private:
390
  const bool Dynamic;
391
392
  uint64_t Size = 0;
393
394
  llvm::DenseMap<StringRef, unsigned> StringMap;
395
  std::vector<StringRef> Strings;
396
};
397
398
class DynamicReloc {
399
public:
400
  DynamicReloc(RelType Type, const InputSectionBase *InputSec,
401
               uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
402
      : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
403
839
        UseSymVA(UseSymVA), Addend(Addend), OutputSec(nullptr) {}
404
  // This constructor records dynamic relocation settings used by MIPS
405
  // multi-GOT implementation. It's to relocate addresses of 64kb pages
406
  // lie inside the output section.
407
  DynamicReloc(RelType Type, const InputSectionBase *InputSec,
408
               uint64_t OffsetInSec, const OutputSection *OutputSec,
409
               int64_t Addend)
410
      : Type(Type), Sym(nullptr), InputSec(InputSec), OffsetInSec(OffsetInSec),
411
6
        UseSymVA(false), Addend(Addend), OutputSec(OutputSec) {}
412
413
  uint64_t getOffset() const;
414
  uint32_t getSymIndex() const;
415
0
  const InputSectionBase *getInputSec() const { return InputSec; }
416
417
  // Computes the addend of the dynamic relocation. Note that this is not the
418
  // same as the Addend member variable as it also includes the symbol address
419
  // if UseSymVA is true.
420
  int64_t computeAddend() const;
421
422
  RelType Type;
423
424
private:
425
  Symbol *Sym;
426
  const InputSectionBase *InputSec = nullptr;
427
  uint64_t OffsetInSec;
428
  // If this member is true, the dynamic relocation will not be against the
429
  // symbol but will instead be a relative relocation that simply adds the
430
  // load address. This means we need to write the symbol virtual address
431
  // plus the original addend as the final relocation addend.
432
  bool UseSymVA;
433
  int64_t Addend;
434
  const OutputSection *OutputSec;
435
};
436
437
template <class ELFT> class DynamicSection final : public SyntheticSection {
438
  typedef typename ELFT::Dyn Elf_Dyn;
439
  typedef typename ELFT::Rel Elf_Rel;
440
  typedef typename ELFT::Rela Elf_Rela;
441
  typedef typename ELFT::Shdr Elf_Shdr;
442
  typedef typename ELFT::Sym Elf_Sym;
443
444
  // finalizeContents() fills this vector with the section contents.
445
  std::vector<std::pair<int32_t, std::function<uint64_t()>>> Entries;
446
447
public:
448
  DynamicSection();
449
  void finalizeContents() override;
450
  void writeTo(uint8_t *Buf) override;
451
2.61k
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Line
Count
Source
451
416
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
Line
Count
Source
451
280
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Line
Count
Source
451
1.71k
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
451
204
  size_t getSize() const override { return Size; }
452
453
private:
454
  void add(int32_t Tag, std::function<uint64_t()> Fn);
455
  void addInt(int32_t Tag, uint64_t Val);
456
  void addInSec(int32_t Tag, InputSection *Sec);
457
  void addInSecRelative(int32_t Tag, InputSection *Sec);
458
  void addOutSec(int32_t Tag, OutputSection *Sec);
459
  void addSize(int32_t Tag, OutputSection *Sec);
460
  void addSym(int32_t Tag, Symbol *Sym);
461
462
  uint64_t Size = 0;
463
};
464
465
class RelocationBaseSection : public SyntheticSection {
466
public:
467
  RelocationBaseSection(StringRef Name, uint32_t Type, int32_t DynamicTag,
468
                        int32_t SizeDynamicTag);
469
  void addReloc(RelType DynType, InputSectionBase *IS, uint64_t OffsetInSec,
470
                Symbol *Sym);
471
  // Add a dynamic relocation that might need an addend. This takes care of
472
  // writing the addend to the output section if needed.
473
  void addReloc(RelType DynType, InputSectionBase *InputSec,
474
                uint64_t OffsetInSec, Symbol *Sym, int64_t Addend, RelExpr Expr,
475
                RelType Type);
476
  void addReloc(const DynamicReloc &Reloc);
477
11.8k
  bool empty() const override { return Relocs.empty(); }
478
1.19k
  size_t getSize() const override { return Relocs.size() * this->Entsize; }
479
191
  size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
480
  void finalizeContents() override;
481
  int32_t DynamicTag, SizeDynamicTag;
482
483
protected:
484
  std::vector<DynamicReloc> Relocs;
485
  size_t NumRelativeRelocs = 0;
486
};
487
488
template <class ELFT>
489
class RelocationSection final : public RelocationBaseSection {
490
  typedef typename ELFT::Rel Elf_Rel;
491
  typedef typename ELFT::Rela Elf_Rela;
492
493
public:
494
  RelocationSection(StringRef Name, bool Sort);
495
  unsigned getRelocOffset();
496
  void writeTo(uint8_t *Buf) override;
497
498
private:
499
  bool Sort;
500
};
501
502
template <class ELFT>
503
class AndroidPackedRelocationSection final : public RelocationBaseSection {
504
  typedef typename ELFT::Rel Elf_Rel;
505
  typedef typename ELFT::Rela Elf_Rela;
506
507
public:
508
  AndroidPackedRelocationSection(StringRef Name);
509
510
  bool updateAllocSize() override;
511
12
  size_t getSize() const override { return RelocData.size(); }
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Line
Count
Source
511
6
  size_t getSize() const override { return RelocData.size(); }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Line
Count
Source
511
6
  size_t getSize() const override { return RelocData.size(); }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
512
2
  void writeTo(uint8_t *Buf) override {
513
2
    memcpy(Buf, RelocData.data(), RelocData.size());
514
2
  }
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
512
1
  void writeTo(uint8_t *Buf) override {
513
1
    memcpy(Buf, RelocData.data(), RelocData.size());
514
1
  }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
512
1
  void writeTo(uint8_t *Buf) override {
513
1
    memcpy(Buf, RelocData.data(), RelocData.size());
514
1
  }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
515
516
private:
517
  SmallVector<char, 0> RelocData;
518
};
519
520
struct SymbolTableEntry {
521
  Symbol *Sym;
522
  size_t StrTabOffset;
523
};
524
525
class SymbolTableBaseSection : public SyntheticSection {
526
public:
527
  SymbolTableBaseSection(StringTableSection &StrTabSec);
528
  void finalizeContents() override;
529
  void postThunkContents() override;
530
7.73k
  size_t getSize() const override { return getNumSymbols() * Entsize; }
531
  void addSymbol(Symbol *Sym);
532
11.7k
  unsigned getNumSymbols() const { return Symbols.size() + 1; }
533
  size_t getSymbolIndex(Symbol *Sym);
534
1.18k
  ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
535
536
protected:
537
  // A vector of symbols and their string table offsets.
538
  std::vector<SymbolTableEntry> Symbols;
539
540
  StringTableSection &StrTabSec;
541
542
  llvm::once_flag OnceFlag;
543
  llvm::DenseMap<Symbol *, size_t> SymbolIndexMap;
544
  llvm::DenseMap<OutputSection *, size_t> SectionIndexMap;
545
};
546
547
template <class ELFT>
548
class SymbolTableSection final : public SymbolTableBaseSection {
549
  typedef typename ELFT::Sym Elf_Sym;
550
551
public:
552
  SymbolTableSection(StringTableSection &StrTabSec);
553
  void writeTo(uint8_t *Buf) override;
554
};
555
556
// Outputs GNU Hash section. For detailed explanation see:
557
// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
558
class GnuHashTableSection final : public SyntheticSection {
559
public:
560
  GnuHashTableSection();
561
  void finalizeContents() override;
562
  void writeTo(uint8_t *Buf) override;
563
1.89k
  size_t getSize() const override { return Size; }
564
565
  // Adds symbols to the hash table.
566
  // Sorts the input to satisfy GNU hash section requirements.
567
  void addSymbols(std::vector<SymbolTableEntry> &Symbols);
568
569
private:
570
  enum { Shift2 = 6 };
571
572
  void writeBloomFilter(uint8_t *Buf);
573
  void writeHashTable(uint8_t *Buf);
574
575
  struct Entry {
576
    Symbol *Sym;
577
    size_t StrTabOffset;
578
    uint32_t Hash;
579
    uint32_t BucketIdx;
580
  };
581
582
  std::vector<Entry> Symbols;
583
  size_t MaskWords;
584
  size_t NBuckets = 0;
585
  size_t Size = 0;
586
};
587
588
class HashTableSection final : public SyntheticSection {
589
public:
590
  HashTableSection();
591
  void finalizeContents() override;
592
  void writeTo(uint8_t *Buf) override;
593
2.59k
  size_t getSize() const override { return Size; }
594
595
private:
596
  size_t Size = 0;
597
};
598
599
// The PltSection is used for both the Plt and Iplt. The former usually has a
600
// header as its first entry that is used at run-time to resolve lazy binding.
601
// The latter is used for GNU Ifunc symbols, that will be subject to a
602
// Target->IRelativeRel.
603
class PltSection : public SyntheticSection {
604
public:
605
  PltSection(bool IsIplt);
606
  void writeTo(uint8_t *Buf) override;
607
  size_t getSize() const override;
608
12.7k
  bool empty() const override { return Entries.empty(); }
609
  void addSymbols();
610
611
  template <class ELFT> void addEntry(Symbol &Sym);
612
613
private:
614
  unsigned getPltRelocOff() const;
615
  std::vector<std::pair<const Symbol *, unsigned>> Entries;
616
  size_t HeaderSize;
617
  bool IsIplt;
618
};
619
620
// GdbIndexChunk is created for each .debug_info section and contains
621
// information to create a part of .gdb_index for a given input section.
622
struct GdbIndexChunk {
623
  struct AddressEntry {
624
    InputSection *Section;
625
    uint64_t LowAddress;
626
    uint64_t HighAddress;
627
    uint32_t CuIndex;
628
  };
629
630
  struct CuEntry {
631
    uint64_t CuOffset;
632
    uint64_t CuLength;
633
  };
634
635
  struct NameTypeEntry {
636
    llvm::CachedHashStringRef Name;
637
    uint8_t Type;
638
  };
639
640
  InputSection *DebugInfoSec;
641
  std::vector<AddressEntry> AddressAreas;
642
  std::vector<CuEntry> CompilationUnits;
643
  std::vector<NameTypeEntry> NamesAndTypes;
644
};
645
646
// The symbol type for the .gdb_index section.
647
struct GdbSymbol {
648
  uint32_t NameHash;
649
  size_t NameOffset;
650
  size_t CuVectorIndex;
651
};
652
653
class GdbIndexSection final : public SyntheticSection {
654
public:
655
  GdbIndexSection(std::vector<GdbIndexChunk> &&Chunks);
656
  void writeTo(uint8_t *Buf) override;
657
  size_t getSize() const override;
658
  bool empty() const override;
659
660
private:
661
  void fixCuIndex();
662
  std::vector<std::vector<uint32_t>> createCuVectors();
663
  std::vector<GdbSymbol *> createGdbSymtab();
664
665
  // A symbol table for this .gdb_index section.
666
  std::vector<GdbSymbol *> GdbSymtab;
667
668
  // CU vector is a part of constant pool area of section.
669
  std::vector<std::vector<uint32_t>> CuVectors;
670
671
  // Symbol table contents.
672
  llvm::DenseMap<llvm::CachedHashStringRef, GdbSymbol *> Symbols;
673
674
  // Each chunk contains information gathered from a debug sections of single
675
  // object and used to build different areas of gdb index.
676
  std::vector<GdbIndexChunk> Chunks;
677
678
  static constexpr uint32_t CuListOffset = 24;
679
  uint32_t CuTypesOffset;
680
  uint32_t SymtabOffset;
681
  uint32_t ConstantPoolOffset;
682
  uint32_t StringPoolOffset;
683
  uint32_t StringPoolSize;
684
685
  std::vector<size_t> CuVectorOffsets;
686
};
687
688
template <class ELFT> GdbIndexSection *createGdbIndex();
689
690
// --eh-frame-hdr option tells linker to construct a header for all the
691
// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
692
// and also to a PT_GNU_EH_FRAME segment.
693
// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
694
// calling dl_iterate_phdr.
695
// This section contains a lookup table for quick binary search of FDEs.
696
// Detailed info about internals can be found in Ian Lance Taylor's blog:
697
// http://www.airs.com/blog/archives/460 (".eh_frame")
698
// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
699
class EhFrameHeader final : public SyntheticSection {
700
public:
701
  EhFrameHeader();
702
  void writeTo(uint8_t *Buf) override;
703
  size_t getSize() const override;
704
  bool empty() const override;
705
};
706
707
// For more information about .gnu.version and .gnu.version_r see:
708
// https://www.akkadia.org/drepper/symbol-versioning
709
710
// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
711
// contain symbol version definitions. The number of entries in this section
712
// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
713
// The section shall contain an array of Elf_Verdef structures, optionally
714
// followed by an array of Elf_Verdaux structures.
715
template <class ELFT>
716
class VersionDefinitionSection final : public SyntheticSection {
717
  typedef typename ELFT::Verdef Elf_Verdef;
718
  typedef typename ELFT::Verdaux Elf_Verdaux;
719
720
public:
721
  VersionDefinitionSection();
722
  void finalizeContents() override;
723
  size_t getSize() const override;
724
  void writeTo(uint8_t *Buf) override;
725
726
private:
727
  void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
728
729
  unsigned FileDefNameOff;
730
};
731
732
// The .gnu.version section specifies the required version of each symbol in the
733
// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
734
// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
735
// identifier defined in the either .gnu.version_r or .gnu.version_d section.
736
// The values 0 and 1 are reserved. All other values are used for versions in
737
// the own object or in any of the dependencies.
738
template <class ELFT>
739
class VersionTableSection final : public SyntheticSection {
740
  typedef typename ELFT::Versym Elf_Versym;
741
742
public:
743
  VersionTableSection();
744
  void finalizeContents() override;
745
  size_t getSize() const override;
746
  void writeTo(uint8_t *Buf) override;
747
  bool empty() const override;
748
};
749
750
// The .gnu.version_r section defines the version identifiers used by
751
// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
752
// Elf_Verneed specifies the version requirements for a single DSO, and contains
753
// a reference to a linked list of Elf_Vernaux data structures which define the
754
// mapping from version identifiers to version names.
755
template <class ELFT> class VersionNeedSection final : public SyntheticSection {
756
  typedef typename ELFT::Verneed Elf_Verneed;
757
  typedef typename ELFT::Vernaux Elf_Vernaux;
758
759
  // A vector of shared files that need Elf_Verneed data structures and the
760
  // string table offsets of their sonames.
761
  std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
762
763
  // The next available version identifier.
764
  unsigned NextIndex;
765
766
public:
767
  VersionNeedSection();
768
  void addSymbol(Symbol *Sym);
769
  void finalizeContents() override;
770
  void writeTo(uint8_t *Buf) override;
771
  size_t getSize() const override;
772
5.11k
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getNeedNum() const
Line
Count
Source
772
680
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getNeedNum() const
Line
Count
Source
772
360
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getNeedNum() const
Line
Count
Source
772
3.84k
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getNeedNum() const
Line
Count
Source
772
230
  size_t getNeedNum() const { return Needed.size(); }
773
  bool empty() const override;
774
};
775
776
// MergeSyntheticSection is a class that allows us to put mergeable sections
777
// with different attributes in a single output sections. To do that
778
// we put them into MergeSyntheticSection synthetic input sections which are
779
// attached to regular output sections.
780
class MergeSyntheticSection : public SyntheticSection {
781
public:
782
  void addSection(MergeInputSection *MS);
783
  std::vector<MergeInputSection *> Sections;
784
785
protected:
786
  MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
787
                        uint32_t Alignment)
788
2.13k
      : SyntheticSection(Flags, Type, Alignment, Name) {}
789
};
790
791
class MergeTailSection final : public MergeSyntheticSection {
792
public:
793
  MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
794
                   uint32_t Alignment);
795
796
  size_t getSize() const override;
797
  void writeTo(uint8_t *Buf) override;
798
  void finalizeContents() override;
799
800
private:
801
  llvm::StringTableBuilder Builder;
802
};
803
804
class MergeNoTailSection final : public MergeSyntheticSection {
805
public:
806
  MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
807
                     uint32_t Alignment)
808
2.12k
      : MergeSyntheticSection(Name, Type, Flags, Alignment) {}
809
810
5.08k
  size_t getSize() const override { return Size; }
811
  void writeTo(uint8_t *Buf) override;
812
  void finalizeContents() override;
813
814
private:
815
  // We use the most significant bits of a hash as a shard ID.
816
  // The reason why we don't want to use the least significant bits is
817
  // because DenseMap also uses lower bits to determine a bucket ID.
818
  // If we use lower bits, it significantly increases the probability of
819
  // hash collisons.
820
11.0k
  size_t getShardId(uint32_t Hash) {
821
11.0k
    return Hash >> (32 - llvm::countTrailingZeros(NumShards));
822
11.0k
  }
823
824
  // Section size
825
  size_t Size;
826
827
  // String table contents
828
  constexpr static size_t NumShards = 32;
829
  std::vector<llvm::StringTableBuilder> Shards;
830
  size_t ShardOffsets[NumShards];
831
};
832
833
// .MIPS.abiflags section.
834
template <class ELFT>
835
class MipsAbiFlagsSection final : public SyntheticSection {
836
  typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
837
838
public:
839
  static MipsAbiFlagsSection *create();
840
841
  MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
842
688
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Line
Count
Source
842
68
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
Line
Count
Source
842
468
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Line
Count
Source
842
8
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
842
144
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
843
  void writeTo(uint8_t *Buf) override;
844
845
private:
846
  Elf_Mips_ABIFlags Flags;
847
};
848
849
// .MIPS.options section.
850
template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
851
  typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
852
  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
853
854
public:
855
  static MipsOptionsSection *create();
856
857
  MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
858
  void writeTo(uint8_t *Buf) override;
859
860
190
  size_t getSize() const override {
861
190
    return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
862
190
  }
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Line
Count
Source
860
10
  size_t getSize() const override {
861
10
    return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
862
10
  }
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
860
180
  size_t getSize() const override {
861
180
    return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
862
180
  }
863
864
private:
865
  Elf_Mips_RegInfo Reginfo;
866
};
867
868
// MIPS .reginfo section.
869
template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
870
  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
871
872
public:
873
  static MipsReginfoSection *create();
874
875
  MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
876
536
  size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Line
Count
Source
876
68
  size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
Line
Count
Source
876
468
  size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
877
  void writeTo(uint8_t *Buf) override;
878
879
private:
880
  Elf_Mips_RegInfo Reginfo;
881
};
882
883
// This is a MIPS specific section to hold a space within the data segment
884
// of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
885
// See "Dynamic section" in Chapter 5 in the following document:
886
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
887
class MipsRldMapSection : public SyntheticSection {
888
public:
889
  MipsRldMapSection();
890
116
  size_t getSize() const override { return Config->Wordsize; }
891
29
  void writeTo(uint8_t *Buf) override {}
892
};
893
894
class ARMExidxSentinelSection : public SyntheticSection {
895
public:
896
  ARMExidxSentinelSection();
897
88
  size_t getSize() const override { return 8; }
898
  void writeTo(uint8_t *Buf) override;
899
  bool empty() const override;
900
901
  // The last section referenced by a regular .ARM.exidx section.
902
  // It is found and filled in Writer<ELFT>::resolveShfLinkOrder().
903
  // The sentinel points at the end of that section.
904
  InputSection *Highest = nullptr;
905
};
906
907
// A container for one or more linker generated thunks. Instances of these
908
// thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
909
class ThunkSection : public SyntheticSection {
910
public:
911
  // ThunkSection in OS, with desired OutSecOff of Off
912
  ThunkSection(OutputSection *OS, uint64_t Off);
913
914
  // Add a newly created Thunk to this container:
915
  // Thunk is given offset from start of this InputSection
916
  // Thunk defines a symbol in this InputSection that can be used as target
917
  // of a relocation
918
  void addThunk(Thunk *T);
919
1.13k
  size_t getSize() const override { return Size; }
920
  void writeTo(uint8_t *Buf) override;
921
  InputSection *getTargetInputSection() const;
922
  bool assignOffsets();
923
924
private:
925
  std::vector<Thunk *> Thunks;
926
  size_t Size = 0;
927
};
928
929
InputSection *createInterpSection();
930
MergeInputSection *createCommentSection();
931
void decompressSections();
932
template <class ELFT> void splitSections();
933
void mergeSections();
934
935
Defined *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
936
                           uint64_t Size, InputSectionBase &Section);
937
938
// Linker generated sections which can be used as inputs.
939
struct InX {
940
  static InputSection *ARMAttributes;
941
  static BssSection *Bss;
942
  static BssSection *BssRelRo;
943
  static BuildIdSection *BuildId;
944
  static EhFrameHeader *EhFrameHdr;
945
  static EhFrameSection *EhFrame;
946
  static SyntheticSection *Dynamic;
947
  static StringTableSection *DynStrTab;
948
  static SymbolTableBaseSection *DynSymTab;
949
  static GnuHashTableSection *GnuHashTab;
950
  static HashTableSection *HashTab;
951
  static InputSection *Interp;
952
  static GdbIndexSection *GdbIndex;
953
  static GotSection *Got;
954
  static GotPltSection *GotPlt;
955
  static IgotPltSection *IgotPlt;
956
  static MipsGotSection *MipsGot;
957
  static MipsRldMapSection *MipsRldMap;
958
  static PltSection *Plt;
959
  static PltSection *Iplt;
960
  static RelocationBaseSection *RelaDyn;
961
  static RelocationBaseSection *RelaPlt;
962
  static RelocationBaseSection *RelaIplt;
963
  static StringTableSection *ShStrTab;
964
  static StringTableSection *StrTab;
965
  static SymbolTableBaseSection *SymTab;
966
};
967
968
template <class ELFT> struct In {
969
  static VersionDefinitionSection<ELFT> *VerDef;
970
  static VersionTableSection<ELFT> *VerSym;
971
  static VersionNeedSection<ELFT> *VerNeed;
972
};
973
974
template <class ELFT> VersionDefinitionSection<ELFT> *In<ELFT>::VerDef;
975
template <class ELFT> VersionTableSection<ELFT> *In<ELFT>::VerSym;
976
template <class ELFT> VersionNeedSection<ELFT> *In<ELFT>::VerNeed;
977
} // namespace elf
978
} // namespace lld
979
980
#endif