Coverage Report

Created: 2017-09-21 03:39

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