Coverage Report

Created: 2018-08-19 21:11

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