Coverage Report

Created: 2018-01-17 21: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 "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 SharedSymbol;
34
35
class SyntheticSection : public InputSection {
36
public:
37
  SyntheticSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
38
                   StringRef Name)
39
      : InputSection(nullptr, Flags, Type, Alignment, {}, Name,
40
18.7k
                     InputSectionBase::Synthetic) {
41
18.7k
    this->Live = true;
42
18.7k
  }
43
44
18.7k
  virtual ~SyntheticSection() = default;
45
  virtual void writeTo(uint8_t *Buf) = 0;
46
  virtual size_t getSize() const = 0;
47
2.43k
  virtual void finalizeContents() {}
48
  // If the section has the SHF_ALLOC flag and the size may be changed if
49
  // thunks are added, update the section size.
50
192
  virtual bool updateAllocSize() { return false; }
51
  // If any additional finalization of contents are needed post thunk creation.
52
0
  virtual void postThunkContents() {}
53
18.9k
  virtual bool empty() const { return false; }
54
  uint64_t getVA() const;
55
56
413k
  static bool classof(const SectionBase *D) {
57
413k
    return D->kind() == InputSectionBase::Synthetic;
58
413k
  }
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
2.69k
  bool empty() const override { return Sections.empty(); }
73
101
  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
87
private:
88
  uint64_t Size = 0;
89
90
  template <class ELFT, class RelTy>
91
  void addSectionAux(EhInputSection *S, llvm::ArrayRef<RelTy> Rels);
92
93
  template <class ELFT, class RelTy>
94
  CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
95
96
  template <class ELFT, class RelTy>
97
  bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
98
99
  uint64_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc) const;
100
101
  std::vector<CieRecord *> CieRecords;
102
103
  // CIE records are uniquified by their contents and personality functions.
104
  llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> CieMap;
105
};
106
107
class GotSection : public SyntheticSection {
108
public:
109
  GotSection();
110
194
  size_t getSize() const override { return Size; }
111
  void finalizeContents() override;
112
  bool empty() const override;
113
  void writeTo(uint8_t *Buf) override;
114
115
  void addEntry(Symbol &Sym);
116
  bool addDynTlsEntry(Symbol &Sym);
117
  bool addTlsIndex();
118
  uint64_t getGlobalDynAddr(const Symbol &B) const;
119
  uint64_t getGlobalDynOffset(const Symbol &B) const;
120
121
3
  uint64_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
122
5
  uint32_t getTlsIndexOff() const { return TlsIndexOff; }
123
124
  // Flag to force GOT to be in output if we have relocations
125
  // that relies on its address.
126
  bool HasGotOffRel = false;
127
128
protected:
129
  size_t NumEntries = 0;
130
  uint32_t TlsIndexOff = -1;
131
  uint64_t Size = 0;
132
};
133
134
// .note.gnu.build-id section.
135
class BuildIdSection : public SyntheticSection {
136
  // First 16 bytes are a header.
137
  static const unsigned HeaderSize = 16;
138
139
public:
140
  BuildIdSection();
141
  void writeTo(uint8_t *Buf) override;
142
12
  size_t getSize() const override { return HeaderSize + HashSize; }
143
  void writeBuildId(llvm::ArrayRef<uint8_t> Buf);
144
145
private:
146
  void computeHash(llvm::ArrayRef<uint8_t> Buf,
147
                   std::function<void(uint8_t *, ArrayRef<uint8_t>)> Hash);
148
149
  size_t HashSize;
150
  uint8_t *HashBuf;
151
};
152
153
// BssSection is used to reserve space for copy relocations and common symbols.
154
// We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
155
// that are used for writable symbols, read-only symbols and common symbols,
156
// respectively.
157
class BssSection final : public SyntheticSection {
158
public:
159
  BssSection(StringRef Name, uint64_t Size, uint32_t Alignment);
160
0
  void writeTo(uint8_t *) override {}
161
3.63k
  bool empty() const override { return getSize() == 0; }
162
3.81k
  size_t getSize() const override { return Size; }
163
164
141
  static bool classof(const SectionBase *S) { return S->Bss; }
165
  uint64_t Size;
166
};
167
168
class MipsGotSection final : public SyntheticSection {
169
public:
170
  MipsGotSection();
171
  void writeTo(uint8_t *Buf) override;
172
280
  size_t getSize() const override { return Size; }
173
  bool updateAllocSize() override;
174
  void finalizeContents() override;
175
  bool empty() const override;
176
  void addEntry(Symbol &Sym, int64_t Addend, RelExpr Expr);
177
  bool addDynTlsEntry(Symbol &Sym);
178
  bool addTlsIndex();
179
  uint64_t getPageEntryOffset(const Symbol &B, int64_t Addend) const;
180
  uint64_t getSymEntryOffset(const Symbol &B, int64_t Addend) const;
181
  uint64_t getGlobalDynOffset(const Symbol &B) const;
182
183
  // Returns the symbol which corresponds to the first entry of the global part
184
  // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
185
  // table properties.
186
  // Returns nullptr if the global part is empty.
187
  const Symbol *getFirstGlobalEntry() const;
188
189
  // Returns the number of entries in the local part of GOT including
190
  // the number of reserved entries.
191
  unsigned getLocalEntriesNum() const;
192
193
  // Returns offset of TLS part of the MIPS GOT table. This part goes
194
  // after 'local' and 'global' entries.
195
  uint64_t getTlsOffset() const;
196
197
1
  uint32_t getTlsIndexOff() const { return TlsIndexOff; }
198
199
  uint64_t getGp() const;
200
201
private:
202
  // MIPS GOT consists of three parts: local, global and tls. Each part
203
  // contains different types of entries. Here is a layout of GOT:
204
  // - Header entries                |
205
  // - Page entries                  |   Local part
206
  // - Local entries (16-bit access) |
207
  // - Local entries (32-bit access) |
208
  // - Normal global entries         ||  Global part
209
  // - Reloc-only global entries     ||
210
  // - TLS entries                   ||| TLS part
211
  //
212
  // Header:
213
  //   Two entries hold predefined value 0x0 and 0x80000000.
214
  // Page entries:
215
  //   These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
216
  //   relocation against local symbols. They are initialized by higher 16-bit
217
  //   of the corresponding symbol's value. So each 64kb of address space
218
  //   requires a single GOT entry.
219
  // Local entries (16-bit access):
220
  //   These entries created by GOT relocations against global non-preemptible
221
  //   symbols so dynamic linker is not necessary to resolve the symbol's
222
  //   values. "16-bit access" means that corresponding relocations address
223
  //   GOT using 16-bit index. Each unique Symbol-Addend pair has its own
224
  //   GOT entry.
225
  // Local entries (32-bit access):
226
  //   These entries are the same as above but created by relocations which
227
  //   address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
228
  // Normal global entries:
229
  //   These entries created by GOT relocations against preemptible global
230
  //   symbols. They need to be initialized by dynamic linker and they ordered
231
  //   exactly as the corresponding entries in the dynamic symbols table.
232
  // Reloc-only global entries:
233
  //   These entries created for symbols that are referenced by dynamic
234
  //   relocations R_MIPS_REL32. These entries are not accessed with gp-relative
235
  //   addressing, but MIPS ABI requires that these entries be present in GOT.
236
  // TLS entries:
237
  //   Entries created by TLS relocations.
238
239
  // Number of "Header" entries.
240
  static const unsigned HeaderEntriesNum = 2;
241
  // Number of allocated "Page" entries.
242
  uint32_t PageEntriesNum = 0;
243
  // Map output sections referenced by MIPS GOT relocations
244
  // to the first index of "Page" entries allocated for this section.
245
  llvm::SmallMapVector<const OutputSection *, size_t, 16> PageIndexMap;
246
247
  typedef std::pair<const Symbol *, uint64_t> GotEntry;
248
  typedef std::vector<GotEntry> GotEntries;
249
  // Map from Symbol-Addend pair to the GOT index.
250
  llvm::DenseMap<GotEntry, size_t> EntryIndexMap;
251
  // Local entries (16-bit access).
252
  GotEntries LocalEntries;
253
  // Local entries (32-bit access).
254
  GotEntries LocalEntries32;
255
256
  // Normal and reloc-only global entries.
257
  GotEntries GlobalEntries;
258
259
  // TLS entries.
260
  std::vector<const Symbol *> TlsEntries;
261
262
  uint32_t TlsIndexOff = -1;
263
  uint64_t Size = 0;
264
};
265
266
class GotPltSection final : public SyntheticSection {
267
public:
268
  GotPltSection();
269
  void addEntry(Symbol &Sym);
270
  size_t getSize() const override;
271
  void writeTo(uint8_t *Buf) override;
272
1.88k
  bool empty() const override { return Entries.empty(); }
273
274
private:
275
  std::vector<const Symbol *> Entries;
276
};
277
278
// The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
279
// Symbols that will be relocated by Target->IRelativeRel.
280
// On most Targets the IgotPltSection will immediately follow the GotPltSection
281
// on ARM the IgotPltSection will immediately follow the GotSection.
282
class IgotPltSection final : public SyntheticSection {
283
public:
284
  IgotPltSection();
285
  void addEntry(Symbol &Sym);
286
  size_t getSize() const override;
287
  void writeTo(uint8_t *Buf) override;
288
1.79k
  bool empty() const override { return Entries.empty(); }
289
290
private:
291
  std::vector<const Symbol *> Entries;
292
};
293
294
class StringTableSection final : public SyntheticSection {
295
public:
296
  StringTableSection(StringRef Name, bool Dynamic);
297
  unsigned addString(StringRef S, bool HashIt = true);
298
  void writeTo(uint8_t *Buf) override;
299
5.88k
  size_t getSize() const override { return Size; }
300
4.27k
  bool isDynamic() const { return Dynamic; }
301
302
private:
303
  const bool Dynamic;
304
305
  uint64_t Size = 0;
306
307
  llvm::DenseMap<StringRef, unsigned> StringMap;
308
  std::vector<StringRef> Strings;
309
};
310
311
class DynamicReloc {
312
public:
313
  DynamicReloc(uint32_t Type, const InputSectionBase *InputSec,
314
               uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
315
      : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
316
434
        UseSymVA(UseSymVA), Addend(Addend) {}
317
318
  uint64_t getOffset() const;
319
  int64_t getAddend() const;
320
  uint32_t getSymIndex() const;
321
11
  const InputSectionBase *getInputSec() const { return InputSec; }
322
323
  uint32_t Type;
324
325
private:
326
  Symbol *Sym;
327
  const InputSectionBase *InputSec = nullptr;
328
  uint64_t OffsetInSec;
329
  bool UseSymVA;
330
  int64_t Addend;
331
};
332
333
template <class ELFT> class DynamicSection final : public SyntheticSection {
334
  typedef typename ELFT::Dyn Elf_Dyn;
335
  typedef typename ELFT::Rel Elf_Rel;
336
  typedef typename ELFT::Rela Elf_Rela;
337
  typedef typename ELFT::Shdr Elf_Shdr;
338
  typedef typename ELFT::Sym Elf_Sym;
339
340
  // finalizeContents() fills this vector with the section contents.
341
  std::vector<std::pair<int32_t, std::function<uint64_t()>>> Entries;
342
343
public:
344
  DynamicSection();
345
  void finalizeContents() override;
346
  void writeTo(uint8_t *Buf) override;
347
1.09k
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const
Line
Count
Source
347
208
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const
Line
Count
Source
347
110
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
Line
Count
Source
347
721
  size_t getSize() const override { return Size; }
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
347
56
  size_t getSize() const override { return Size; }
348
349
private:
350
  void add(int32_t Tag, std::function<uint64_t()> Fn);
351
  void addInt(int32_t Tag, uint64_t Val);
352
  void addInSec(int32_t Tag, InputSection *Sec);
353
  void addOutSec(int32_t Tag, OutputSection *Sec);
354
  void addSize(int32_t Tag, OutputSection *Sec);
355
  void addSym(int32_t Tag, Symbol *Sym);
356
357
  uint64_t Size = 0;
358
};
359
360
class RelocationBaseSection : public SyntheticSection {
361
public:
362
  RelocationBaseSection(StringRef Name, uint32_t Type, int32_t DynamicTag,
363
                        int32_t SizeDynamicTag);
364
  void addReloc(uint32_t DynType, InputSectionBase *InputSec,
365
                uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym,
366
                int64_t Addend, RelExpr Expr, RelType Type);
367
  void addReloc(const DynamicReloc &Reloc);
368
5.15k
  bool empty() const override { return Relocs.empty(); }
369
482
  size_t getSize() const override { return Relocs.size() * this->Entsize; }
370
87
  size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
371
  void finalizeContents() override;
372
  int32_t DynamicTag, SizeDynamicTag;
373
374
protected:
375
  std::vector<DynamicReloc> Relocs;
376
  size_t NumRelativeRelocs = 0;
377
};
378
379
template <class ELFT>
380
class RelocationSection final : public RelocationBaseSection {
381
  typedef typename ELFT::Rel Elf_Rel;
382
  typedef typename ELFT::Rela Elf_Rela;
383
384
public:
385
  RelocationSection(StringRef Name, bool Sort);
386
  unsigned getRelocOffset();
387
  void writeTo(uint8_t *Buf) override;
388
389
private:
390
  bool Sort;
391
};
392
393
template <class ELFT>
394
class AndroidPackedRelocationSection final : public RelocationBaseSection {
395
  typedef typename ELFT::Rel Elf_Rel;
396
  typedef typename ELFT::Rela Elf_Rela;
397
398
public:
399
  AndroidPackedRelocationSection(StringRef Name);
400
401
  bool updateAllocSize() override;
402
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
402
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
402
6
  size_t getSize() const override { return RelocData.size(); }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
403
2
  void writeTo(uint8_t *Buf) override {
404
2
    memcpy(Buf, RelocData.data(), RelocData.size());
405
2
  }
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
403
1
  void writeTo(uint8_t *Buf) override {
404
1
    memcpy(Buf, RelocData.data(), RelocData.size());
405
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
403
1
  void writeTo(uint8_t *Buf) override {
404
1
    memcpy(Buf, RelocData.data(), RelocData.size());
405
1
  }
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
406
407
private:
408
  SmallVector<char, 0> RelocData;
409
};
410
411
struct SymbolTableEntry {
412
  Symbol *Sym;
413
  size_t StrTabOffset;
414
};
415
416
class SymbolTableBaseSection : public SyntheticSection {
417
public:
418
  SymbolTableBaseSection(StringTableSection &StrTabSec);
419
  void finalizeContents() override;
420
  void postThunkContents() override;
421
3.25k
  size_t getSize() const override { return getNumSymbols() * Entsize; }
422
  void addSymbol(Symbol *Sym);
423
5.01k
  unsigned getNumSymbols() const { return Symbols.size() + 1; }
424
  size_t getSymbolIndex(Symbol *Sym);
425
527
  ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
426
427
protected:
428
  // A vector of symbols and their string table offsets.
429
  std::vector<SymbolTableEntry> Symbols;
430
431
  StringTableSection &StrTabSec;
432
433
  llvm::once_flag OnceFlag;
434
  llvm::DenseMap<Symbol *, size_t> SymbolIndexMap;
435
  llvm::DenseMap<OutputSection *, size_t> SectionIndexMap;
436
};
437
438
template <class ELFT>
439
class SymbolTableSection final : public SymbolTableBaseSection {
440
  typedef typename ELFT::Sym Elf_Sym;
441
442
public:
443
  SymbolTableSection(StringTableSection &StrTabSec);
444
  void writeTo(uint8_t *Buf) override;
445
};
446
447
// Outputs GNU Hash section. For detailed explanation see:
448
// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
449
class GnuHashTableSection final : public SyntheticSection {
450
public:
451
  GnuHashTableSection();
452
  void finalizeContents() override;
453
  void writeTo(uint8_t *Buf) override;
454
761
  size_t getSize() const override { return Size; }
455
456
  // Adds symbols to the hash table.
457
  // Sorts the input to satisfy GNU hash section requirements.
458
  void addSymbols(std::vector<SymbolTableEntry> &Symbols);
459
460
private:
461
769
  size_t getShift2() const { return Config->Is64 ? 
6648
:
5121
; }
462
463
  void writeBloomFilter(uint8_t *Buf);
464
  void writeHashTable(uint8_t *Buf);
465
466
  struct Entry {
467
    Symbol *Sym;
468
    size_t StrTabOffset;
469
    uint32_t Hash;
470
    uint32_t BucketIdx;
471
  };
472
473
  std::vector<Entry> Symbols;
474
  size_t MaskWords;
475
  size_t NBuckets = 0;
476
  size_t Size = 0;
477
};
478
479
class HashTableSection final : public SyntheticSection {
480
public:
481
  HashTableSection();
482
  void finalizeContents() override;
483
  void writeTo(uint8_t *Buf) override;
484
1.08k
  size_t getSize() const override { return Size; }
485
486
private:
487
  size_t Size = 0;
488
};
489
490
// The PltSection is used for both the Plt and Iplt. The former usually has a
491
// header as its first entry that is used at run-time to resolve lazy binding.
492
// The latter is used for GNU Ifunc symbols, that will be subject to a
493
// Target->IRelativeRel.
494
class PltSection : public SyntheticSection {
495
public:
496
  PltSection(bool IsIplt);
497
  void writeTo(uint8_t *Buf) override;
498
  size_t getSize() const override;
499
5.54k
  bool empty() const override { return Entries.empty(); }
500
  void addSymbols();
501
502
  template <class ELFT> void addEntry(Symbol &Sym);
503
504
private:
505
  unsigned getPltRelocOff() const;
506
  std::vector<std::pair<const Symbol *, unsigned>> Entries;
507
  size_t HeaderSize;
508
  bool IsIplt;
509
};
510
511
// GdbIndexChunk is created for each .debug_info section and contains
512
// information to create a part of .gdb_index for a given input section.
513
struct GdbIndexChunk {
514
  struct AddressEntry {
515
    InputSection *Section;
516
    uint64_t LowAddress;
517
    uint64_t HighAddress;
518
    uint32_t CuIndex;
519
  };
520
521
  struct CuEntry {
522
    uint64_t CuOffset;
523
    uint64_t CuLength;
524
  };
525
526
  struct NameTypeEntry {
527
    llvm::CachedHashStringRef Name;
528
    uint8_t Type;
529
  };
530
531
  InputSection *DebugInfoSec;
532
  std::vector<AddressEntry> AddressAreas;
533
  std::vector<CuEntry> CompilationUnits;
534
  std::vector<NameTypeEntry> NamesAndTypes;
535
};
536
537
// The symbol type for the .gdb_index section.
538
struct GdbSymbol {
539
  uint32_t NameHash;
540
  size_t NameOffset;
541
  size_t CuVectorIndex;
542
};
543
544
class GdbIndexSection final : public SyntheticSection {
545
public:
546
  GdbIndexSection(std::vector<GdbIndexChunk> &&Chunks);
547
  void writeTo(uint8_t *Buf) override;
548
  size_t getSize() const override;
549
  bool empty() const override;
550
551
private:
552
  void fixCuIndex();
553
  std::vector<std::vector<uint32_t>> createCuVectors();
554
  std::vector<GdbSymbol *> createGdbSymtab();
555
556
  // A symbol table for this .gdb_index section.
557
  std::vector<GdbSymbol *> GdbSymtab;
558
559
  // CU vector is a part of constant pool area of section.
560
  std::vector<std::vector<uint32_t>> CuVectors;
561
562
  // Symbol table contents.
563
  llvm::DenseMap<llvm::CachedHashStringRef, GdbSymbol *> Symbols;
564
565
  // Each chunk contains information gathered from a debug sections of single
566
  // object and used to build different areas of gdb index.
567
  std::vector<GdbIndexChunk> Chunks;
568
569
  static constexpr uint32_t CuListOffset = 24;
570
  uint32_t CuTypesOffset;
571
  uint32_t SymtabOffset;
572
  uint32_t ConstantPoolOffset;
573
  uint32_t StringPoolOffset;
574
  uint32_t StringPoolSize;
575
576
  std::vector<size_t> CuVectorOffsets;
577
};
578
579
template <class ELFT> GdbIndexSection *createGdbIndex();
580
581
// --eh-frame-hdr option tells linker to construct a header for all the
582
// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
583
// and also to a PT_GNU_EH_FRAME segment.
584
// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
585
// calling dl_iterate_phdr.
586
// This section contains a lookup table for quick binary search of FDEs.
587
// Detailed info about internals can be found in Ian Lance Taylor's blog:
588
// http://www.airs.com/blog/archives/460 (".eh_frame")
589
// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
590
class EhFrameHeader final : public SyntheticSection {
591
public:
592
  EhFrameHeader();
593
  void writeTo(uint8_t *Buf) override;
594
  size_t getSize() const override;
595
  bool empty() const override;
596
};
597
598
// For more information about .gnu.version and .gnu.version_r see:
599
// https://www.akkadia.org/drepper/symbol-versioning
600
601
// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
602
// contain symbol version definitions. The number of entries in this section
603
// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
604
// The section shall contain an array of Elf_Verdef structures, optionally
605
// followed by an array of Elf_Verdaux structures.
606
template <class ELFT>
607
class VersionDefinitionSection final : public SyntheticSection {
608
  typedef typename ELFT::Verdef Elf_Verdef;
609
  typedef typename ELFT::Verdaux Elf_Verdaux;
610
611
public:
612
  VersionDefinitionSection();
613
  void finalizeContents() override;
614
  size_t getSize() const override;
615
  void writeTo(uint8_t *Buf) override;
616
617
private:
618
  void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
619
620
  unsigned FileDefNameOff;
621
};
622
623
// The .gnu.version section specifies the required version of each symbol in the
624
// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
625
// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
626
// identifier defined in the either .gnu.version_r or .gnu.version_d section.
627
// The values 0 and 1 are reserved. All other values are used for versions in
628
// the own object or in any of the dependencies.
629
template <class ELFT>
630
class VersionTableSection final : public SyntheticSection {
631
  typedef typename ELFT::Versym Elf_Versym;
632
633
public:
634
  VersionTableSection();
635
  void finalizeContents() override;
636
  size_t getSize() const override;
637
  void writeTo(uint8_t *Buf) override;
638
  bool empty() const override;
639
};
640
641
// The .gnu.version_r section defines the version identifiers used by
642
// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
643
// Elf_Verneed specifies the version requirements for a single DSO, and contains
644
// a reference to a linked list of Elf_Vernaux data structures which define the
645
// mapping from version identifiers to version names.
646
template <class ELFT> class VersionNeedSection final : public SyntheticSection {
647
  typedef typename ELFT::Verneed Elf_Verneed;
648
  typedef typename ELFT::Vernaux Elf_Vernaux;
649
650
  // A vector of shared files that need Elf_Verneed data structures and the
651
  // string table offsets of their sonames.
652
  std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
653
654
  // The next available version identifier.
655
  unsigned NextIndex;
656
657
public:
658
  VersionNeedSection();
659
  void addSymbol(SharedSymbol *SS);
660
  void finalizeContents() override;
661
  void writeTo(uint8_t *Buf) override;
662
  size_t getSize() const override;
663
2.25k
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getNeedNum() const
Line
Count
Source
663
335
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getNeedNum() const
Line
Count
Source
663
140
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getNeedNum() const
Line
Count
Source
663
1.69k
  size_t getNeedNum() const { return Needed.size(); }
lld::elf::VersionNeedSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getNeedNum() const
Line
Count
Source
663
85
  size_t getNeedNum() const { return Needed.size(); }
664
  bool empty() const override;
665
};
666
667
// MergeSyntheticSection is a class that allows us to put mergeable sections
668
// with different attributes in a single output sections. To do that
669
// we put them into MergeSyntheticSection synthetic input sections which are
670
// attached to regular output sections.
671
class MergeSyntheticSection : public SyntheticSection {
672
public:
673
  void addSection(MergeInputSection *MS);
674
675
protected:
676
  MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
677
                        uint32_t Alignment)
678
940
      : SyntheticSection(Flags, Type, Alignment, Name) {}
679
680
  std::vector<MergeInputSection *> Sections;
681
};
682
683
class MergeTailSection final : public MergeSyntheticSection {
684
public:
685
  MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
686
                   uint32_t Alignment);
687
688
  size_t getSize() const override;
689
  void writeTo(uint8_t *Buf) override;
690
  void finalizeContents() override;
691
692
private:
693
  llvm::StringTableBuilder Builder;
694
};
695
696
class MergeNoTailSection final : public MergeSyntheticSection {
697
public:
698
  MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
699
                     uint32_t Alignment)
700
931
      : MergeSyntheticSection(Name, Type, Flags, Alignment) {}
701
702
2.15k
  size_t getSize() const override { return Size; }
703
  void writeTo(uint8_t *Buf) override;
704
  void finalizeContents() override;
705
706
private:
707
  // We use the most significant bits of a hash as a shard ID.
708
  // The reason why we don't want to use the least significant bits is
709
  // because DenseMap also uses lower bits to determine a bucket ID.
710
  // If we use lower bits, it significantly increases the probability of
711
  // hash collisons.
712
8.81k
  size_t getShardId(uint32_t Hash) {
713
8.81k
    return Hash >> (32 - llvm::countTrailingZeros(NumShards));
714
8.81k
  }
715
716
  // Section size
717
  size_t Size;
718
719
  // String table contents
720
  constexpr static size_t NumShards = 32;
721
  std::vector<llvm::StringTableBuilder> Shards;
722
  size_t ShardOffsets[NumShards];
723
};
724
725
// .MIPS.abiflags section.
726
template <class ELFT>
727
class MipsAbiFlagsSection final : public SyntheticSection {
728
  typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
729
730
public:
731
  static MipsAbiFlagsSection *create();
732
733
  MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
734
292
  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
734
18
  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
734
202
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
Unexecuted instantiation: lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
734
72
  size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
735
  void writeTo(uint8_t *Buf) override;
736
737
private:
738
  Elf_Mips_ABIFlags Flags;
739
};
740
741
// .MIPS.options section.
742
template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
743
  typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
744
  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
745
746
public:
747
  static MipsOptionsSection *create();
748
749
  MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
750
  void writeTo(uint8_t *Buf) override;
751
752
90
  size_t getSize() const override {
753
90
    return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
754
90
  }
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
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const
Line
Count
Source
752
90
  size_t getSize() const override {
753
90
    return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
754
90
  }
755
756
private:
757
  Elf_Mips_RegInfo Reginfo;
758
};
759
760
// MIPS .reginfo section.
761
template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
762
  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
763
764
public:
765
  static MipsReginfoSection *create();
766
767
  MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
768
220
  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
768
18
  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
768
202
  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
769
  void writeTo(uint8_t *Buf) override;
770
771
private:
772
  Elf_Mips_RegInfo Reginfo;
773
};
774
775
// This is a MIPS specific section to hold a space within the data segment
776
// of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
777
// See "Dynamic section" in Chapter 5 in the following document:
778
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
779
class MipsRldMapSection : public SyntheticSection {
780
public:
781
  MipsRldMapSection();
782
40
  size_t getSize() const override { return Config->Wordsize; }
783
10
  void writeTo(uint8_t *Buf) override {}
784
};
785
786
class ARMExidxSentinelSection : public SyntheticSection {
787
public:
788
  ARMExidxSentinelSection();
789
40
  size_t getSize() const override { return 8; }
790
  void writeTo(uint8_t *Buf) override;
791
  bool empty() const override;
792
793
  // The last section referenced by a regular .ARM.exidx section.
794
  // It is found and filled in Writer<ELFT>::resolveShfLinkOrder().
795
  // The sentinel points at the end of that section.
796
  InputSection *Highest = nullptr;
797
};
798
799
// A container for one or more linker generated thunks. Instances of these
800
// thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
801
class ThunkSection : public SyntheticSection {
802
public:
803
  // ThunkSection in OS, with desired OutSecOff of Off
804
  ThunkSection(OutputSection *OS, uint64_t Off);
805
806
  // Add a newly created Thunk to this container:
807
  // Thunk is given offset from start of this InputSection
808
  // Thunk defines a symbol in this InputSection that can be used as target
809
  // of a relocation
810
  void addThunk(Thunk *T);
811
486
  size_t getSize() const override { return Size; }
812
  void writeTo(uint8_t *Buf) override;
813
  InputSection *getTargetInputSection() const;
814
815
private:
816
  std::vector<const Thunk *> Thunks;
817
  size_t Size = 0;
818
};
819
820
InputSection *createInterpSection();
821
MergeInputSection *createCommentSection();
822
void decompressSections();
823
void mergeSections();
824
825
Symbol *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
826
                          uint64_t Size, InputSectionBase &Section);
827
828
// Linker generated sections which can be used as inputs.
829
struct InX {
830
  static InputSection *ARMAttributes;
831
  static BssSection *Bss;
832
  static BssSection *BssRelRo;
833
  static BuildIdSection *BuildId;
834
  static EhFrameHeader *EhFrameHdr;
835
  static EhFrameSection *EhFrame;
836
  static SyntheticSection *Dynamic;
837
  static StringTableSection *DynStrTab;
838
  static SymbolTableBaseSection *DynSymTab;
839
  static GnuHashTableSection *GnuHashTab;
840
  static HashTableSection *HashTab;
841
  static InputSection *Interp;
842
  static GdbIndexSection *GdbIndex;
843
  static GotSection *Got;
844
  static GotPltSection *GotPlt;
845
  static IgotPltSection *IgotPlt;
846
  static MipsGotSection *MipsGot;
847
  static MipsRldMapSection *MipsRldMap;
848
  static PltSection *Plt;
849
  static PltSection *Iplt;
850
  static RelocationBaseSection *RelaDyn;
851
  static RelocationBaseSection *RelaPlt;
852
  static RelocationBaseSection *RelaIplt;
853
  static StringTableSection *ShStrTab;
854
  static StringTableSection *StrTab;
855
  static SymbolTableBaseSection *SymTab;
856
};
857
858
template <class ELFT> struct In {
859
  static VersionDefinitionSection<ELFT> *VerDef;
860
  static VersionTableSection<ELFT> *VerSym;
861
  static VersionNeedSection<ELFT> *VerNeed;
862
};
863
864
template <class ELFT> VersionDefinitionSection<ELFT> *In<ELFT>::VerDef;
865
template <class ELFT> VersionTableSection<ELFT> *In<ELFT>::VerSym;
866
template <class ELFT> VersionNeedSection<ELFT> *In<ELFT>::VerNeed;
867
} // namespace elf
868
} // namespace lld
869
870
#endif