Coverage Report

Created: 2018-10-20 12:32

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