Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/InputSection.h
Line
Count
Source (jump to first uncovered line)
1
//===- InputSection.h -------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLD_ELF_INPUT_SECTION_H
10
#define LLD_ELF_INPUT_SECTION_H
11
12
#include "Config.h"
13
#include "Relocations.h"
14
#include "Thunks.h"
15
#include "lld/Common/LLVM.h"
16
#include "llvm/ADT/CachedHashString.h"
17
#include "llvm/ADT/DenseSet.h"
18
#include "llvm/ADT/TinyPtrVector.h"
19
#include "llvm/Object/ELF.h"
20
21
namespace lld {
22
namespace elf {
23
24
class Symbol;
25
struct SectionPiece;
26
27
class Defined;
28
struct Partition;
29
class SyntheticSection;
30
class MergeSyntheticSection;
31
template <class ELFT> class ObjFile;
32
class OutputSection;
33
34
extern std::vector<Partition> partitions;
35
36
// This is the base class of all sections that lld handles. Some are sections in
37
// input files, some are sections in the produced output file and some exist
38
// just as a convenience for implementing special ways of combining some
39
// sections.
40
class SectionBase {
41
public:
42
  enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
43
44
14.4M
  Kind kind() const { return (Kind)sectionKind; }
45
46
  StringRef name;
47
48
  // This pointer points to the "real" instance of this instance.
49
  // Usually Repl == this. However, if ICF merges two sections,
50
  // Repl pointer of one section points to another section. So,
51
  // if you need to get a pointer to this instance, do not use
52
  // this but instead this->Repl.
53
  SectionBase *repl;
54
55
  unsigned sectionKind : 3;
56
57
  // The next three bit fields are only used by InputSectionBase, but we
58
  // put them here so the struct packs better.
59
60
  // True if this section has already been placed to a linker script
61
  // output section. This is needed because, in a linker script, you
62
  // can refer to the same section more than once. For example, in
63
  // the following linker script,
64
  //
65
  //   .foo : { *(.text) }
66
  //   .bar : { *(.text) }
67
  //
68
  // .foo takes all .text sections, and .bar becomes empty. To achieve
69
  // this, we need to memorize whether a section has been placed or
70
  // not for each input section.
71
  unsigned assigned : 1;
72
73
  unsigned bss : 1;
74
75
  // Set for sections that should not be folded by ICF.
76
  unsigned keepUnique : 1;
77
78
  // The 1-indexed partition that this section is assigned to by the garbage
79
  // collector, or 0 if this section is dead. Normally there is only one
80
  // partition, so this will either be 0 or 1.
81
  uint8_t partition;
82
  elf::Partition &getPartition() const;
83
84
  // These corresponds to the fields in Elf_Shdr.
85
  uint32_t alignment;
86
  uint64_t flags;
87
  uint64_t entsize;
88
  uint32_t type;
89
  uint32_t link;
90
  uint32_t info;
91
92
  OutputSection *getOutputSection();
93
148k
  const OutputSection *getOutputSection() const {
94
148k
    return const_cast<SectionBase *>(this)->getOutputSection();
95
148k
  }
96
97
  // Translate an offset in the input section to an offset in the output
98
  // section.
99
  uint64_t getOffset(uint64_t offset) const;
100
101
  uint64_t getVA(uint64_t offset = 0) const;
102
103
1.62M
  bool isLive() const { return partition != 0; }
104
400k
  void markLive() { partition = 1; }
105
23.5k
  void markDead() { partition = 0; }
106
107
protected:
108
  SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
109
              uint64_t entsize, uint64_t alignment, uint32_t type,
110
              uint32_t info, uint32_t link)
111
      : name(name), repl(this), sectionKind(sectionKind), assigned(false),
112
        bss(false), keepUnique(false), partition(0), alignment(alignment),
113
792k
        flags(flags), entsize(entsize), type(type), link(link), info(info) {}
114
};
115
116
// This corresponds to a section of an input file.
117
class InputSectionBase : public SectionBase {
118
public:
119
  template <class ELFT>
120
  InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
121
                   StringRef name, Kind sectionKind);
122
123
  InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
124
                   uint64_t entsize, uint32_t link, uint32_t info,
125
                   uint32_t alignment, ArrayRef<uint8_t> data, StringRef name,
126
                   Kind sectionKind);
127
128
85.0k
  static bool classof(const SectionBase *s) { return s->kind() != Output; }
129
130
  // Relocations that refer to this section.
131
  unsigned numRelocations : 31;
132
  unsigned areRelocsRela : 1;
133
  const void *firstRelocation = nullptr;
134
135
  // The file which contains this section. Its dynamic type is always
136
  // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
137
  // its static type.
138
  InputFile *file;
139
140
28.6k
  template <class ELFT> ObjFile<ELFT> *getFile() const {
141
28.6k
    return cast_or_null<ObjFile<ELFT>>(file);
142
28.6k
  }
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >() const
Line
Count
Source
140
2.20k
  template <class ELFT> ObjFile<ELFT> *getFile() const {
141
2.20k
    return cast_or_null<ObjFile<ELFT>>(file);
142
2.20k
  }
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >() const
Line
Count
Source
140
924
  template <class ELFT> ObjFile<ELFT> *getFile() const {
141
924
    return cast_or_null<ObjFile<ELFT>>(file);
142
924
  }
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >() const
Line
Count
Source
140
9.35k
  template <class ELFT> ObjFile<ELFT> *getFile() const {
141
9.35k
    return cast_or_null<ObjFile<ELFT>>(file);
142
9.35k
  }
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >() const
Line
Count
Source
140
16.1k
  template <class ELFT> ObjFile<ELFT> *getFile() const {
141
16.1k
    return cast_or_null<ObjFile<ELFT>>(file);
142
16.1k
  }
143
144
5.23M
  ArrayRef<uint8_t> data() const {
145
5.23M
    if (uncompressedSize >= 0)
146
7
      uncompress();
147
5.23M
    return rawData;
148
5.23M
  }
149
150
  uint64_t getOffsetInFile() const;
151
152
  // Input sections are part of an output section. Special sections
153
  // like .eh_frame and merge sections are first combined into a
154
  // synthetic section that is then added to an output section. In all
155
  // cases this points one level up.
156
  SectionBase *parent = nullptr;
157
158
312k
  template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
159
312k
    assert(!areRelocsRela);
160
312k
    return llvm::makeArrayRef(
161
312k
        static_cast<const typename ELFT::Rel *>(firstRelocation),
162
312k
        numRelocations);
163
312k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, false>::Rel> lld::elf::InputSectionBase::rels<llvm::object::ELFType<(llvm::support::endianness)1, false> >() const
Line
Count
Source
158
6.64k
  template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
159
6.64k
    assert(!areRelocsRela);
160
6.64k
    return llvm::makeArrayRef(
161
6.64k
        static_cast<const typename ELFT::Rel *>(firstRelocation),
162
6.64k
        numRelocations);
163
6.64k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, false>::Rel> lld::elf::InputSectionBase::rels<llvm::object::ELFType<(llvm::support::endianness)0, false> >() const
Line
Count
Source
158
2.88k
  template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
159
2.88k
    assert(!areRelocsRela);
160
2.88k
    return llvm::makeArrayRef(
161
2.88k
        static_cast<const typename ELFT::Rel *>(firstRelocation),
162
2.88k
        numRelocations);
163
2.88k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, true>::Rel> lld::elf::InputSectionBase::rels<llvm::object::ELFType<(llvm::support::endianness)1, true> >() const
Line
Count
Source
158
300k
  template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
159
300k
    assert(!areRelocsRela);
160
300k
    return llvm::makeArrayRef(
161
300k
        static_cast<const typename ELFT::Rel *>(firstRelocation),
162
300k
        numRelocations);
163
300k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, true>::Rel> lld::elf::InputSectionBase::rels<llvm::object::ELFType<(llvm::support::endianness)0, true> >() const
Line
Count
Source
158
1.80k
  template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
159
1.80k
    assert(!areRelocsRela);
160
1.80k
    return llvm::makeArrayRef(
161
1.80k
        static_cast<const typename ELFT::Rel *>(firstRelocation),
162
1.80k
        numRelocations);
163
1.80k
  }
164
165
2.33k
  template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
166
2.33k
    assert(areRelocsRela);
167
2.33k
    return llvm::makeArrayRef(
168
2.33k
        static_cast<const typename ELFT::Rela *>(firstRelocation),
169
2.33k
        numRelocations);
170
2.33k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela> lld::elf::InputSectionBase::relas<llvm::object::ELFType<(llvm::support::endianness)1, true> >() const
Line
Count
Source
165
2.12k
  template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
166
2.12k
    assert(areRelocsRela);
167
2.12k
    return llvm::makeArrayRef(
168
2.12k
        static_cast<const typename ELFT::Rela *>(firstRelocation),
169
2.12k
        numRelocations);
170
2.12k
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela> lld::elf::InputSectionBase::relas<llvm::object::ELFType<(llvm::support::endianness)0, true> >() const
Line
Count
Source
165
134
  template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
166
134
    assert(areRelocsRela);
167
134
    return llvm::makeArrayRef(
168
134
        static_cast<const typename ELFT::Rela *>(firstRelocation),
169
134
        numRelocations);
170
134
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela> lld::elf::InputSectionBase::relas<llvm::object::ELFType<(llvm::support::endianness)1, false> >() const
Line
Count
Source
165
42
  template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
166
42
    assert(areRelocsRela);
167
42
    return llvm::makeArrayRef(
168
42
        static_cast<const typename ELFT::Rela *>(firstRelocation),
169
42
        numRelocations);
170
42
  }
llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela> lld::elf::InputSectionBase::relas<llvm::object::ELFType<(llvm::support::endianness)0, false> >() const
Line
Count
Source
165
37
  template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
166
37
    assert(areRelocsRela);
167
37
    return llvm::makeArrayRef(
168
37
        static_cast<const typename ELFT::Rela *>(firstRelocation),
169
37
        numRelocations);
170
37
  }
171
172
  // InputSections that are dependent on us (reverse dependency for GC)
173
  llvm::TinyPtrVector<InputSection *> dependentSections;
174
175
  // Returns the size of this section (even if this is a common or BSS.)
176
  size_t getSize() const;
177
178
  InputSection *getLinkOrderDep() const;
179
180
  // Get the function symbol that encloses this offset from within the
181
  // section.
182
  template <class ELFT>
183
  Defined *getEnclosingFunction(uint64_t offset);
184
185
  // Returns a source location string. Used to construct an error message.
186
  template <class ELFT> std::string getLocation(uint64_t offset);
187
  std::string getSrcMsg(const Symbol &sym, uint64_t offset);
188
  std::string getObjMsg(uint64_t offset);
189
190
  // Each section knows how to relocate itself. These functions apply
191
  // relocations, assuming that Buf points to this section's copy in
192
  // the mmap'ed output buffer.
193
  template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
194
  void relocateAlloc(uint8_t *buf, uint8_t *bufEnd);
195
196
  // The native ELF reloc data type is not very convenient to handle.
197
  // So we convert ELF reloc records to our own records in Relocations.cpp.
198
  // This vector contains such "cooked" relocations.
199
  std::vector<Relocation> relocations;
200
201
  // A function compiled with -fsplit-stack calling a function
202
  // compiled without -fsplit-stack needs its prologue adjusted. Find
203
  // such functions and adjust their prologues.  This is very similar
204
  // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
205
  // information.
206
  template <typename ELFT>
207
  void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
208
209
210
162
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
162
    size_t s = data().size();
212
162
    assert(s % sizeof(T) == 0);
213
162
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
162
  }
Unexecuted instantiation: llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >() const
llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >() const
Line
Count
Source
210
20
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
20
    size_t s = data().size();
212
20
    assert(s % sizeof(T) == 0);
213
20
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
20
  }
llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)1, 1ul> > lld::elf::InputSectionBase::getDataAs<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)1, 1ul> >() const
Line
Count
Source
210
10
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
10
    size_t s = data().size();
212
10
    assert(s % sizeof(T) == 0);
213
10
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
10
  }
Unexecuted instantiation: llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >() const
llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >() const
Line
Count
Source
210
10
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
10
    size_t s = data().size();
212
10
    assert(s % sizeof(T) == 0);
213
10
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
10
  }
Unexecuted instantiation: llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)0, 1ul> > lld::elf::InputSectionBase::getDataAs<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)0, 1ul> >() const
llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >() const
Line
Count
Source
210
80
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
80
    size_t s = data().size();
212
80
    assert(s % sizeof(T) == 0);
213
80
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
80
  }
Unexecuted instantiation: llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >() const
llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >() const
Line
Count
Source
210
2
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
2
    size_t s = data().size();
212
2
    assert(s % sizeof(T) == 0);
213
2
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
2
  }
Unexecuted instantiation: llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> > lld::elf::InputSectionBase::getDataAs<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >() const
SyntheticSections.cpp:llvm::ArrayRef<isDuplicateArmExidxSec(lld::elf::InputSection*, lld::elf::InputSection*)::ExidxEntry> lld::elf::InputSectionBase::getDataAs<isDuplicateArmExidxSec(lld::elf::InputSection*, lld::elf::InputSection*)::ExidxEntry>() const
Line
Count
Source
210
40
  template <typename T> llvm::ArrayRef<T> getDataAs() const {
211
40
    size_t s = data().size();
212
40
    assert(s % sizeof(T) == 0);
213
40
    return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
214
40
  }
215
216
protected:
217
  void parseCompressedHeader();
218
  void uncompress() const;
219
220
  mutable ArrayRef<uint8_t> rawData;
221
222
  // This field stores the uncompressed size of the compressed data in rawData,
223
  // or -1 if rawData is not compressed (either because the section wasn't
224
  // compressed in the first place, or because we ended up uncompressing it).
225
  // Since the feature is not used often, this is usually -1.
226
  mutable int64_t uncompressedSize = -1;
227
};
228
229
// SectionPiece represents a piece of splittable section contents.
230
// We allocate a lot of these and binary search on them. This means that they
231
// have to be as compact as possible, which is why we don't store the size (can
232
// be found by looking at the next one).
233
struct SectionPiece {
234
  SectionPiece(size_t off, uint32_t hash, bool live)
235
2.89k
      : inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
236
237
  uint32_t inputOff;
238
  uint32_t live : 1;
239
  uint32_t hash : 31;
240
  uint64_t outputOff = 0;
241
};
242
243
static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
244
245
// This corresponds to a SHF_MERGE section of an input file.
246
class MergeInputSection : public InputSectionBase {
247
public:
248
  template <class ELFT>
249
  MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
250
                    StringRef name);
251
  MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
252
                    ArrayRef<uint8_t> data, StringRef name);
253
254
748k
  static bool classof(const SectionBase *s) { return s->kind() == Merge; }
255
  void splitIntoPieces();
256
257
  // Translate an offset in the input section to an offset in the parent
258
  // MergeSyntheticSection.
259
  uint64_t getParentOffset(uint64_t offset) const;
260
261
  // Splittable sections are handled as a sequence of data
262
  // rather than a single large blob of data.
263
  std::vector<SectionPiece> pieces;
264
265
  // Returns I'th piece's data. This function is very hot when
266
  // string merging is enabled, so we want to inline.
267
  LLVM_ATTRIBUTE_ALWAYS_INLINE
268
2.88k
  llvm::CachedHashStringRef getData(size_t i) const {
269
2.88k
    size_t begin = pieces[i].inputOff;
270
2.88k
    size_t end =
271
2.88k
        (pieces.size() - 1 == i) ? 
data().size()2.78k
:
pieces[i + 1].inputOff103
;
272
2.88k
    return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
273
2.88k
  }
274
275
  // Returns the SectionPiece at a given input section offset.
276
  SectionPiece *getSectionPiece(uint64_t offset);
277
0
  const SectionPiece *getSectionPiece(uint64_t offset) const {
278
0
    return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
279
0
  }
280
281
  SyntheticSection *getParent() const;
282
283
private:
284
  void splitStrings(ArrayRef<uint8_t> a, size_t size);
285
  void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
286
};
287
288
struct EhSectionPiece {
289
  EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
290
                 unsigned firstRelocation)
291
248
      : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
292
293
604
  ArrayRef<uint8_t> data() {
294
604
    return {sec->data().data() + this->inputOff, size};
295
604
  }
296
297
  size_t inputOff;
298
  ssize_t outputOff = -1;
299
  InputSectionBase *sec;
300
  uint32_t size;
301
  unsigned firstRelocation;
302
};
303
304
// This corresponds to a .eh_frame section of an input file.
305
class EhInputSection : public InputSectionBase {
306
public:
307
  template <class ELFT>
308
  EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
309
                 StringRef name);
310
754k
  static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
311
  template <class ELFT> void split();
312
  template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
313
314
  // Splittable sections are handled as a sequence of data
315
  // rather than a single large blob of data.
316
  std::vector<EhSectionPiece> pieces;
317
318
  SyntheticSection *getParent() const;
319
};
320
321
// This is a section that is added directly to an output section
322
// instead of needing special combination via a synthetic section. This
323
// includes all input sections with the exceptions of SHF_MERGE and
324
// .eh_frame. It also includes the synthetic sections themselves.
325
class InputSection : public InputSectionBase {
326
public:
327
  InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment,
328
               ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
329
  template <class ELFT>
330
  InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
331
               StringRef name);
332
333
  // Write this section to a mmap'ed file, assuming Buf is pointing to
334
  // beginning of the output section.
335
  template <class ELFT> void writeTo(uint8_t *buf);
336
337
148k
  uint64_t getOffset(uint64_t offset) const { return outSecOff + offset; }
338
339
  OutputSection *getParent() const;
340
341
  // This variable has two usages. Initially, it represents an index in the
342
  // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
343
  // sections. After assignAddresses is called, it represents the offset from
344
  // the beginning of the output section this section was assigned to.
345
  uint64_t outSecOff = 0;
346
347
  static bool classof(const SectionBase *s);
348
349
  InputSectionBase *getRelocatedSection() const;
350
351
  template <class ELFT, class RelTy>
352
  void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
353
354
  // Used by ICF.
355
  uint32_t eqClass[2] = {0, 0};
356
357
  // Called by ICF to merge two input sections.
358
  void replace(InputSection *other);
359
360
  static InputSection discarded;
361
362
private:
363
  template <class ELFT, class RelTy>
364
  void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
365
366
  template <class ELFT> void copyShtGroup(uint8_t *buf);
367
};
368
369
// The list of all input sections.
370
extern std::vector<InputSectionBase *> inputSections;
371
372
} // namespace elf
373
374
std::string toString(const elf::InputSectionBase *);
375
} // namespace lld
376
377
#endif