Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/OutputSections.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- OutputSections.cpp -------------------------------------------------===//
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
#include "OutputSections.h"
11
#include "Config.h"
12
#include "LinkerScript.h"
13
#include "Memory.h"
14
#include "Strings.h"
15
#include "SymbolTable.h"
16
#include "SyntheticSections.h"
17
#include "Target.h"
18
#include "Threads.h"
19
#include "llvm/BinaryFormat/Dwarf.h"
20
#include "llvm/Support/Compression.h"
21
#include "llvm/Support/MD5.h"
22
#include "llvm/Support/MathExtras.h"
23
#include "llvm/Support/SHA1.h"
24
25
using namespace llvm;
26
using namespace llvm::dwarf;
27
using namespace llvm::object;
28
using namespace llvm::support::endian;
29
using namespace llvm::ELF;
30
31
using namespace lld;
32
using namespace lld::elf;
33
34
uint8_t Out::First;
35
OutputSection *Out::Opd;
36
uint8_t *Out::OpdBuf;
37
PhdrEntry *Out::TlsPhdr;
38
OutputSection *Out::DebugInfo;
39
OutputSection *Out::ElfHeader;
40
OutputSection *Out::ProgramHeaders;
41
OutputSection *Out::PreinitArray;
42
OutputSection *Out::InitArray;
43
OutputSection *Out::FiniArray;
44
45
std::vector<OutputSection *> elf::OutputSections;
46
47
72.9k
uint32_t OutputSection::getPhdrFlags() const {
48
72.9k
  uint32_t Ret = PF_R;
49
72.9k
  if (Flags & SHF_WRITE)
50
2.42k
    Ret |= PF_W;
51
72.9k
  if (Flags & SHF_EXECINSTR)
52
1.77k
    Ret |= PF_X;
53
72.9k
  return Ret;
54
72.9k
}
55
56
template <class ELFT>
57
209k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
209k
  Shdr->sh_entsize = Entsize;
59
209k
  Shdr->sh_addralign = Alignment;
60
209k
  Shdr->sh_type = Type;
61
209k
  Shdr->sh_offset = Offset;
62
209k
  Shdr->sh_flags = Flags;
63
209k
  Shdr->sh_info = Info;
64
209k
  Shdr->sh_link = Link;
65
209k
  Shdr->sh_addr = Addr;
66
209k
  Shdr->sh_size = Size;
67
209k
  Shdr->sh_name = ShName;
68
209k
}
void lld::elf::OutputSection::writeHeaderTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr*)
Line
Count
Source
57
529
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
529
  Shdr->sh_entsize = Entsize;
59
529
  Shdr->sh_addralign = Alignment;
60
529
  Shdr->sh_type = Type;
61
529
  Shdr->sh_offset = Offset;
62
529
  Shdr->sh_flags = Flags;
63
529
  Shdr->sh_info = Info;
64
529
  Shdr->sh_link = Link;
65
529
  Shdr->sh_addr = Addr;
66
529
  Shdr->sh_size = Size;
67
529
  Shdr->sh_name = ShName;
68
529
}
void lld::elf::OutputSection::writeHeaderTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr*)
Line
Count
Source
57
205k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
205k
  Shdr->sh_entsize = Entsize;
59
205k
  Shdr->sh_addralign = Alignment;
60
205k
  Shdr->sh_type = Type;
61
205k
  Shdr->sh_offset = Offset;
62
205k
  Shdr->sh_flags = Flags;
63
205k
  Shdr->sh_info = Info;
64
205k
  Shdr->sh_link = Link;
65
205k
  Shdr->sh_addr = Addr;
66
205k
  Shdr->sh_size = Size;
67
205k
  Shdr->sh_name = ShName;
68
205k
}
void lld::elf::OutputSection::writeHeaderTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr*)
Line
Count
Source
57
1.19k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
1.19k
  Shdr->sh_entsize = Entsize;
59
1.19k
  Shdr->sh_addralign = Alignment;
60
1.19k
  Shdr->sh_type = Type;
61
1.19k
  Shdr->sh_offset = Offset;
62
1.19k
  Shdr->sh_flags = Flags;
63
1.19k
  Shdr->sh_info = Info;
64
1.19k
  Shdr->sh_link = Link;
65
1.19k
  Shdr->sh_addr = Addr;
66
1.19k
  Shdr->sh_size = Size;
67
1.19k
  Shdr->sh_name = ShName;
68
1.19k
}
void lld::elf::OutputSection::writeHeaderTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr*)
Line
Count
Source
57
1.77k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
1.77k
  Shdr->sh_entsize = Entsize;
59
1.77k
  Shdr->sh_addralign = Alignment;
60
1.77k
  Shdr->sh_type = Type;
61
1.77k
  Shdr->sh_offset = Offset;
62
1.77k
  Shdr->sh_flags = Flags;
63
1.77k
  Shdr->sh_info = Info;
64
1.77k
  Shdr->sh_link = Link;
65
1.77k
  Shdr->sh_addr = Addr;
66
1.77k
  Shdr->sh_size = Size;
67
1.77k
  Shdr->sh_name = ShName;
68
1.77k
}
69
70
OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
71
    : BaseCommand(OutputSectionKind),
72
      SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
73
                  /*Info*/ 0,
74
                  /*Link*/ 0),
75
226k
      SectionIndex(INT_MAX) {
76
226k
  Live = false;
77
226k
}
78
79
228k
static uint64_t updateOffset(uint64_t Off, InputSection *S) {
80
228k
  Off = alignTo(Off, S->Alignment);
81
228k
  S->OutSecOff = Off;
82
228k
  return Off + S->getSize();
83
228k
}
84
85
228k
void OutputSection::addSection(InputSection *S) {
86
228k
  assert(S->Live);
87
228k
  Live = true;
88
228k
  S->Parent = this;
89
228k
  this->updateAlignment(S->Alignment);
90
228k
91
228k
  // The actual offsets will be computed by assignAddresses. For now, use
92
228k
  // crude approximation so that it is at least easy for other code to know the
93
228k
  // section order. It is also used to calculate the output section size early
94
228k
  // for compressed debug sections.
95
228k
  this->Size = updateOffset(Size, S);
96
228k
97
228k
  // If this section contains a table of fixed-size entries, sh_entsize
98
228k
  // holds the element size. Consequently, if this contains two or more
99
228k
  // input sections, all of them must have the same sh_entsize. However,
100
228k
  // you can put different types of input sections into one output
101
228k
  // sectin by using linker scripts. I don't know what to do here.
102
228k
  // Probably we sholuld handle that as an error. But for now we just
103
228k
  // pick the largest sh_entsize.
104
228k
  this->Entsize = std::max(this->Entsize, S->Entsize);
105
228k
106
228k
  if (
!S->Assigned228k
) {
107
227k
    S->Assigned = true;
108
227k
    if (
Commands.empty() || 227k
!isa<InputSectionDescription>(Commands.back())5.80k
)
109
221k
      Commands.push_back(make<InputSectionDescription>(""));
110
227k
    auto *ISD = cast<InputSectionDescription>(Commands.back());
111
227k
    ISD->Sections.push_back(S);
112
227k
  }
113
228k
}
114
115
227k
static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
116
227k
  //  The ELF spec just says
117
227k
  // ----------------------------------------------------------------
118
227k
  // In the first phase, input sections that match in name, type and
119
227k
  // attribute flags should be concatenated into single sections.
120
227k
  // ----------------------------------------------------------------
121
227k
  //
122
227k
  // However, it is clear that at least some flags have to be ignored for
123
227k
  // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
124
227k
  // ignored. We should not have two output .text sections just because one was
125
227k
  // in a group and another was not for example.
126
227k
  //
127
227k
  // It also seems that that wording was a late addition and didn't get the
128
227k
  // necessary scrutiny.
129
227k
  //
130
227k
  // Merging sections with different flags is expected by some users. One
131
227k
  // reason is that if one file has
132
227k
  //
133
227k
  // int *const bar __attribute__((section(".foo"))) = (int *)0;
134
227k
  //
135
227k
  // gcc with -fPIC will produce a read only .foo section. But if another
136
227k
  // file has
137
227k
  //
138
227k
  // int zed;
139
227k
  // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
140
227k
  //
141
227k
  // gcc with -fPIC will produce a read write section.
142
227k
  //
143
227k
  // Last but not least, when using linker script the merge rules are forced by
144
227k
  // the script. Unfortunately, linker scripts are name based. This means that
145
227k
  // expressions like *(.foo*) can refer to multiple input sections with
146
227k
  // different flags. We cannot put them in different output sections or we
147
227k
  // would produce wrong results for
148
227k
  //
149
227k
  // start = .; *(.foo.*) end = .; *(.bar)
150
227k
  //
151
227k
  // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
152
227k
  // another. The problem is that there is no way to layout those output
153
227k
  // sections such that the .foo sections are the only thing between the start
154
227k
  // and end symbols.
155
227k
  //
156
227k
  // Given the above issues, we instead merge sections by name and error on
157
227k
  // incompatible types and flags.
158
227k
159
227k
  uint32_t Alignment = 0;
160
227k
  uint64_t Flags = 0;
161
227k
  if (
Config->Relocatable && 227k
(C->Flags & SHF_MERGE)932
) {
162
5
    Alignment = std::max<uint64_t>(C->Alignment, C->Entsize);
163
5
    Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
164
5
  }
165
227k
166
227k
  return SectionKey{OutsecName, Flags, Alignment};
167
227k
}
168
169
1.59k
OutputSectionFactory::OutputSectionFactory() {}
170
171
12.0k
static uint64_t getIncompatibleFlags(uint64_t Flags) {
172
12.0k
  return Flags & (SHF_ALLOC | SHF_TLS);
173
12.0k
}
174
175
// We allow sections of types listed below to merged into a
176
// single progbits section. This is typically done by linker
177
// scripts. Merging nobits and progbits will force disk space
178
// to be allocated for nobits sections. Other ones don't require
179
// any special treatment on top of progbits, so there doesn't
180
// seem to be a harm in merging them.
181
11
static bool canMergeToProgbits(unsigned Type) {
182
11
  return Type == SHT_NOBITS || 
Type == SHT_PROGBITS10
||
Type == SHT_INIT_ARRAY5
||
183
11
         
Type == SHT_PREINIT_ARRAY4
||
Type == SHT_FINI_ARRAY3
||
184
2
         Type == SHT_NOTE;
185
11
}
186
187
void elf::sortByOrder(MutableArrayRef<InputSection *> In,
188
86
                      std::function<int(InputSectionBase *S)> Order) {
189
86
  typedef std::pair<int, InputSection *> Pair;
190
46
  auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
191
86
192
86
  std::vector<Pair> V;
193
86
  for (InputSection *S : In)
194
122
    V.push_back({Order(S), S});
195
86
  std::stable_sort(V.begin(), V.end(), Comp);
196
86
197
208
  for (size_t I = 0; 
I < V.size()208
;
++I122
)
198
122
    In[I] = V[I].second;
199
86
}
200
201
493
void elf::reportDiscarded(InputSectionBase *IS) {
202
493
  if (!Config->PrintGcSections)
203
476
    return;
204
17
  message("removing unused section from '" + IS->Name + "' in file '" +
205
17
          IS->File->getName() + "'");
206
17
}
207
208
static OutputSection *addSection(InputSectionBase *IS, StringRef OutsecName,
209
228k
                                 OutputSection *Sec) {
210
228k
  if (
Sec && 228k
Sec->Live6.44k
) {
211
6.02k
    if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
212
2
      error("incompatible section flags for " + Sec->Name + "\n>>> " +
213
2
            toString(IS) + ": 0x" + utohexstr(IS->Flags) +
214
2
            "\n>>> output section " + Sec->Name + ": 0x" +
215
2
            utohexstr(Sec->Flags));
216
6.02k
    if (
Sec->Type != IS->Type6.02k
) {
217
6
      if (
canMergeToProgbits(Sec->Type) && 6
canMergeToProgbits(IS->Type)5
)
218
5
        Sec->Type = SHT_PROGBITS;
219
6
      else
220
1
        error("section type mismatch for " + IS->Name + "\n>>> " +
221
1
              toString(IS) + ": " +
222
1
              getELFSectionTypeName(Config->EMachine, IS->Type) +
223
1
              "\n>>> output section " + Sec->Name + ": " +
224
1
              getELFSectionTypeName(Config->EMachine, Sec->Type));
225
6
    }
226
6.02k
    Sec->Flags |= IS->Flags;
227
228k
  } else {
228
222k
    if (
!Sec222k
) {
229
221k
      Sec = Script->createOutputSection(OutsecName, "<internal>");
230
221k
      Script->Opt.Commands.push_back(Sec);
231
221k
    }
232
222k
    Sec->Type = IS->Type;
233
222k
    Sec->Flags = IS->Flags;
234
222k
  }
235
228k
236
228k
  Sec->addSection(cast<InputSection>(IS));
237
228k
  return Sec;
238
228k
}
239
240
void OutputSectionFactory::addInputSec(InputSectionBase *IS,
241
                                       StringRef OutsecName,
242
228k
                                       OutputSection *OS) {
243
228k
  if (
!IS->Live228k
) {
244
405
    reportDiscarded(IS);
245
405
    return;
246
405
  }
247
228k
248
228k
  // If we have destination output section - use it directly.
249
228k
  
if (228k
OS228k
) {
250
703
    addSection(IS, OutsecName, OS);
251
703
    return;
252
703
  }
253
227k
254
227k
  // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
255
227k
  // option is given. A section with SHT_GROUP defines a "section group", and
256
227k
  // its members have SHF_GROUP attribute. Usually these flags have already been
257
227k
  // stripped by InputFiles.cpp as section groups are processed and uniquified.
258
227k
  // However, for the -r option, we want to pass through all section groups
259
227k
  // as-is because adding/removing members or merging them with other groups
260
227k
  // change their semantics.
261
227k
  
if (227k
IS->Type == SHT_GROUP || 227k
(IS->Flags & SHF_GROUP)227k
) {
262
19
    addSection(IS, OutsecName, nullptr);
263
19
    return;
264
19
  }
265
227k
266
227k
  // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
267
227k
  // relocation sections .rela.foo and .rela.bar for example. Most tools do
268
227k
  // not allow multiple REL[A] sections for output section. Hence we
269
227k
  // should combine these relocation sections into single output.
270
227k
  // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
271
227k
  // other REL[A] sections created by linker itself.
272
227k
  
if (227k
!isa<SyntheticSection>(IS) &&
273
227k
      
(IS->Type == SHT_REL || 199k
IS->Type == SHT_RELA199k
)) {
274
68
    auto *Sec = cast<InputSection>(IS);
275
68
    OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
276
68
    Out->RelocationSection = addSection(IS, OutsecName, Out->RelocationSection);
277
68
    return;
278
68
  }
279
227k
280
227k
  SectionKey Key = createKey(IS, OutsecName);
281
227k
  OutputSection *&Sec = Map[Key];
282
227k
  Sec = addSection(IS, OutsecName, Sec);
283
227k
}
284
285
1.59k
OutputSectionFactory::~OutputSectionFactory() {}
286
287
756k
SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
288
756k
  return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
289
756k
}
290
291
531k
SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
292
531k
  return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
293
531k
}
294
295
526k
unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
296
526k
  return hash_combine(Val.Name, Val.Flags, Val.Alignment);
297
526k
}
298
299
bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
300
3.84M
                                       const SectionKey &RHS) {
301
3.84M
  return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
302
3.84M
         
LHS.Flags == RHS.Flags1.12M
&&
LHS.Alignment == RHS.Alignment1.12M
;
303
3.84M
}
304
305
2.94k
uint64_t elf::getHeaderSize() {
306
2.94k
  if (Config->OFormatBinary)
307
3
    return 0;
308
2.94k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
309
2.94k
}
310
311
4.31M
bool OutputSection::classof(const BaseCommand *C) {
312
4.31M
  return C->Kind == OutputSectionKind;
313
4.31M
}
314
315
85
void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
316
85
  assert(Commands.size() == 1);
317
85
  sortByOrder(cast<InputSectionDescription>(Commands[0])->Sections, Order);
318
85
}
319
320
// Fill [Buf, Buf + Size) with Filler.
321
// This is used for linker script "=fillexp" command.
322
4.09k
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
323
4.09k
  size_t I = 0;
324
6.48k
  for (; 
I + 4 < Size6.48k
;
I += 42.39k
)
325
2.39k
    memcpy(Buf + I, &Filler, 4);
326
4.09k
  memcpy(Buf + I, &Filler, Size - I);
327
4.09k
}
328
329
// Compress section contents if this section contains debug info.
330
209k
template <class ELFT> void OutputSection::maybeCompress() {
331
209k
  typedef typename ELFT::Chdr Elf_Chdr;
332
209k
333
209k
  // Compress only DWARF debug sections.
334
209k
  if (
!Config->CompressDebugSections || 209k
(Flags & SHF_ALLOC)24
||
335
20
      !Name.startswith(".debug_"))
336
209k
    return;
337
18.4E
338
18.4E
  // Create a section header.
339
18.4E
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
18.4E
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
18.4E
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
18.4E
  Hdr->ch_size = Size;
343
18.4E
  Hdr->ch_addralign = Alignment;
344
18.4E
345
18.4E
  // Write section contents to a temporary buffer and compress it.
346
18.4E
  std::vector<uint8_t> Buf(Size);
347
18.4E
  writeTo<ELFT>(Buf.data());
348
18.4E
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
18.4E
351
18.4E
  // Update section headers.
352
18.4E
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
18.4E
  Flags |= SHF_COMPRESSED;
354
18.4E
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
330
527
template <class ELFT> void OutputSection::maybeCompress() {
331
527
  typedef typename ELFT::Chdr Elf_Chdr;
332
527
333
527
  // Compress only DWARF debug sections.
334
527
  if (
!Config->CompressDebugSections || 527
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
529
    return;
337
18.4E
338
18.4E
  // Create a section header.
339
18.4E
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
18.4E
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
18.4E
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
18.4E
  Hdr->ch_size = Size;
343
18.4E
  Hdr->ch_addralign = Alignment;
344
18.4E
345
18.4E
  // Write section contents to a temporary buffer and compress it.
346
18.4E
  std::vector<uint8_t> Buf(Size);
347
18.4E
  writeTo<ELFT>(Buf.data());
348
18.4E
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
18.4E
351
18.4E
  // Update section headers.
352
18.4E
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
18.4E
  Flags |= SHF_COMPRESSED;
354
18.4E
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
330
1.18k
template <class ELFT> void OutputSection::maybeCompress() {
331
1.18k
  typedef typename ELFT::Chdr Elf_Chdr;
332
1.18k
333
1.18k
  // Compress only DWARF debug sections.
334
1.18k
  if (
!Config->CompressDebugSections || 1.18k
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
1.19k
    return;
337
18.4E
338
18.4E
  // Create a section header.
339
18.4E
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
18.4E
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
18.4E
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
18.4E
  Hdr->ch_size = Size;
343
18.4E
  Hdr->ch_addralign = Alignment;
344
18.4E
345
18.4E
  // Write section contents to a temporary buffer and compress it.
346
18.4E
  std::vector<uint8_t> Buf(Size);
347
18.4E
  writeTo<ELFT>(Buf.data());
348
18.4E
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
18.4E
351
18.4E
  // Update section headers.
352
18.4E
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
18.4E
  Flags |= SHF_COMPRESSED;
354
18.4E
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
330
205k
template <class ELFT> void OutputSection::maybeCompress() {
331
205k
  typedef typename ELFT::Chdr Elf_Chdr;
332
205k
333
205k
  // Compress only DWARF debug sections.
334
205k
  if (
!Config->CompressDebugSections || 205k
(Flags & SHF_ALLOC)24
||
335
20
      !Name.startswith(".debug_"))
336
205k
    return;
337
18.4E
338
18.4E
  // Create a section header.
339
18.4E
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
18.4E
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
18.4E
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
18.4E
  Hdr->ch_size = Size;
343
18.4E
  Hdr->ch_addralign = Alignment;
344
18.4E
345
18.4E
  // Write section contents to a temporary buffer and compress it.
346
18.4E
  std::vector<uint8_t> Buf(Size);
347
18.4E
  writeTo<ELFT>(Buf.data());
348
18.4E
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
18.4E
351
18.4E
  // Update section headers.
352
18.4E
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
18.4E
  Flags |= SHF_COMPRESSED;
354
18.4E
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
330
1.77k
template <class ELFT> void OutputSection::maybeCompress() {
331
1.77k
  typedef typename ELFT::Chdr Elf_Chdr;
332
1.77k
333
1.77k
  // Compress only DWARF debug sections.
334
1.77k
  if (
!Config->CompressDebugSections || 1.77k
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
1.77k
    return;
337
18.4E
338
18.4E
  // Create a section header.
339
18.4E
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
18.4E
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
18.4E
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
18.4E
  Hdr->ch_size = Size;
343
18.4E
  Hdr->ch_addralign = Alignment;
344
18.4E
345
18.4E
  // Write section contents to a temporary buffer and compress it.
346
18.4E
  std::vector<uint8_t> Buf(Size);
347
18.4E
  writeTo<ELFT>(Buf.data());
348
18.4E
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
18.4E
351
18.4E
  // Update section headers.
352
18.4E
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
18.4E
  Flags |= SHF_COMPRESSED;
354
18.4E
}
355
356
19
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
357
19
  if (Size == 1)
358
5
    *Buf = Data;
359
14
  else 
if (14
Size == 214
)
360
4
    write16(Buf, Data, Config->Endianness);
361
10
  else 
if (10
Size == 410
)
362
5
    write32(Buf, Data, Config->Endianness);
363
5
  else 
if (5
Size == 85
)
364
5
    write64(Buf, Data, Config->Endianness);
365
5
  else
366
0
    llvm_unreachable("unsupported Size argument");
367
19
}
368
369
209k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
209k
  if (Type == SHT_NOBITS)
371
310
    return;
372
208k
373
208k
  Loc = Buf;
374
208k
375
208k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
208k
  // we've already compressed section contents. If that's the case,
377
208k
  // just write it down.
378
208k
  if (
!CompressedData.empty()208k
) {
379
4
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
380
4
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
381
4
           CompressedData.size());
382
4
    return;
383
4
  }
384
208k
385
208k
  // Write leading padding.
386
208k
  std::vector<InputSection *> Sections;
387
208k
  for (BaseCommand *Cmd : Commands)
388
208k
    
if (auto *208k
ISD208k
= dyn_cast<InputSectionDescription>(Cmd))
389
208k
      for (InputSection *IS : ISD->Sections)
390
209k
        
if (209k
IS->Live209k
)
391
209k
          Sections.push_back(IS);
392
208k
  uint32_t Filler = getFiller();
393
208k
  if (Filler)
394
1.73k
    
fill(Buf, Sections.empty() ? 1.73k
Size1
:
Sections[0]->OutSecOff1.73k
, Filler);
395
208k
396
209k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
209k
    InputSection *IS = Sections[I];
398
209k
    IS->writeTo<ELFT>(Buf);
399
209k
400
209k
    // Fill gaps between sections.
401
209k
    if (
Filler209k
) {
402
2.35k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
2.35k
      uint8_t *End;
404
2.35k
      if (I + 1 == Sections.size())
405
1.73k
        End = Buf + Size;
406
2.35k
      else
407
614
        End = Buf + Sections[I + 1]->OutSecOff;
408
2.35k
      fill(Start, End - Start, Filler);
409
2.35k
    }
410
209k
  });
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
396
1.19k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.19k
    InputSection *IS = Sections[I];
398
1.19k
    IS->writeTo<ELFT>(Buf);
399
1.19k
400
1.19k
    // Fill gaps between sections.
401
1.19k
    if (
Filler1.19k
) {
402
165
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
165
      uint8_t *End;
404
165
      if (I + 1 == Sections.size())
405
101
        End = Buf + Size;
406
165
      else
407
64
        End = Buf + Sections[I + 1]->OutSecOff;
408
165
      fill(Start, End - Start, Filler);
409
165
    }
410
1.19k
  });
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
396
523
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
523
    InputSection *IS = Sections[I];
398
523
    IS->writeTo<ELFT>(Buf);
399
523
400
523
    // Fill gaps between sections.
401
523
    if (
Filler523
) {
402
38
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
38
      uint8_t *End;
404
38
      if (I + 1 == Sections.size())
405
28
        End = Buf + Size;
406
38
      else
407
10
        End = Buf + Sections[I + 1]->OutSecOff;
408
38
      fill(Start, End - Start, Filler);
409
38
    }
410
523
  });
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
396
206k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
206k
    InputSection *IS = Sections[I];
398
206k
    IS->writeTo<ELFT>(Buf);
399
206k
400
206k
    // Fill gaps between sections.
401
206k
    if (
Filler206k
) {
402
1.79k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.79k
      uint8_t *End;
404
1.79k
      if (I + 1 == Sections.size())
405
1.32k
        End = Buf + Size;
406
1.79k
      else
407
468
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.79k
      fill(Start, End - Start, Filler);
409
1.79k
    }
410
206k
  });
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
396
1.84k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.84k
    InputSection *IS = Sections[I];
398
1.84k
    IS->writeTo<ELFT>(Buf);
399
1.84k
400
1.84k
    // Fill gaps between sections.
401
1.84k
    if (
Filler1.84k
) {
402
357
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
357
      uint8_t *End;
404
357
      if (I + 1 == Sections.size())
405
285
        End = Buf + Size;
406
357
      else
407
72
        End = Buf + Sections[I + 1]->OutSecOff;
408
357
      fill(Start, End - Start, Filler);
409
357
    }
410
1.84k
  });
411
208k
412
208k
  // Linker scripts may have BYTE()-family commands with which you
413
208k
  // can write arbitrary bytes to the output. Process them if any.
414
208k
  for (BaseCommand *Base : Commands)
415
208k
    
if (auto *208k
Data208k
= dyn_cast<BytesDataCommand>(Base))
416
19
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
209k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
369
529
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
529
  if (Type == SHT_NOBITS)
371
28
    return;
372
501
373
501
  Loc = Buf;
374
501
375
501
  // If -compress-debug-section is specified and if this is a debug seciton,
376
501
  // we've already compressed section contents. If that's the case,
377
501
  // just write it down.
378
501
  if (
!CompressedData.empty()501
) {
379
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
380
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
381
0
           CompressedData.size());
382
0
    return;
383
0
  }
384
501
385
501
  // Write leading padding.
386
501
  std::vector<InputSection *> Sections;
387
501
  for (BaseCommand *Cmd : Commands)
388
515
    
if (auto *515
ISD515
= dyn_cast<InputSectionDescription>(Cmd))
389
507
      for (InputSection *IS : ISD->Sections)
390
523
        
if (523
IS->Live523
)
391
523
          Sections.push_back(IS);
392
501
  uint32_t Filler = getFiller();
393
501
  if (Filler)
394
28
    
fill(Buf, Sections.empty() ? 28
Size0
:
Sections[0]->OutSecOff28
, Filler);
395
501
396
501
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
501
    InputSection *IS = Sections[I];
398
501
    IS->writeTo<ELFT>(Buf);
399
501
400
501
    // Fill gaps between sections.
401
501
    if (Filler) {
402
501
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
501
      uint8_t *End;
404
501
      if (I + 1 == Sections.size())
405
501
        End = Buf + Size;
406
501
      else
407
501
        End = Buf + Sections[I + 1]->OutSecOff;
408
501
      fill(Start, End - Start, Filler);
409
501
    }
410
501
  });
411
501
412
501
  // Linker scripts may have BYTE()-family commands with which you
413
501
  // can write arbitrary bytes to the output. Process them if any.
414
501
  for (BaseCommand *Base : Commands)
415
515
    
if (auto *515
Data515
= dyn_cast<BytesDataCommand>(Base))
416
8
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
529
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
369
205k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
205k
  if (Type == SHT_NOBITS)
371
148
    return;
372
205k
373
205k
  Loc = Buf;
374
205k
375
205k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
205k
  // we've already compressed section contents. If that's the case,
377
205k
  // just write it down.
378
205k
  if (
!CompressedData.empty()205k
) {
379
4
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
380
4
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
381
4
           CompressedData.size());
382
4
    return;
383
4
  }
384
205k
385
205k
  // Write leading padding.
386
205k
  std::vector<InputSection *> Sections;
387
205k
  for (BaseCommand *Cmd : Commands)
388
205k
    
if (auto *205k
ISD205k
= dyn_cast<InputSectionDescription>(Cmd))
389
205k
      for (InputSection *IS : ISD->Sections)
390
206k
        
if (206k
IS->Live206k
)
391
206k
          Sections.push_back(IS);
392
205k
  uint32_t Filler = getFiller();
393
205k
  if (Filler)
394
1.32k
    
fill(Buf, Sections.empty() ? 1.32k
Size1
:
Sections[0]->OutSecOff1.32k
, Filler);
395
205k
396
205k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
205k
    InputSection *IS = Sections[I];
398
205k
    IS->writeTo<ELFT>(Buf);
399
205k
400
205k
    // Fill gaps between sections.
401
205k
    if (Filler) {
402
205k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
205k
      uint8_t *End;
404
205k
      if (I + 1 == Sections.size())
405
205k
        End = Buf + Size;
406
205k
      else
407
205k
        End = Buf + Sections[I + 1]->OutSecOff;
408
205k
      fill(Start, End - Start, Filler);
409
205k
    }
410
205k
  });
411
205k
412
205k
  // Linker scripts may have BYTE()-family commands with which you
413
205k
  // can write arbitrary bytes to the output. Process them if any.
414
205k
  for (BaseCommand *Base : Commands)
415
205k
    
if (auto *205k
Data205k
= dyn_cast<BytesDataCommand>(Base))
416
10
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
205k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
369
1.19k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
1.19k
  if (Type == SHT_NOBITS)
371
88
    return;
372
1.10k
373
1.10k
  Loc = Buf;
374
1.10k
375
1.10k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
1.10k
  // we've already compressed section contents. If that's the case,
377
1.10k
  // just write it down.
378
1.10k
  if (
!CompressedData.empty()1.10k
) {
379
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
380
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
381
0
           CompressedData.size());
382
0
    return;
383
0
  }
384
1.10k
385
1.10k
  // Write leading padding.
386
1.10k
  std::vector<InputSection *> Sections;
387
1.10k
  for (BaseCommand *Cmd : Commands)
388
1.10k
    
if (auto *1.10k
ISD1.10k
= dyn_cast<InputSectionDescription>(Cmd))
389
1.10k
      for (InputSection *IS : ISD->Sections)
390
1.19k
        
if (1.19k
IS->Live1.19k
)
391
1.19k
          Sections.push_back(IS);
392
1.10k
  uint32_t Filler = getFiller();
393
1.10k
  if (Filler)
394
101
    
fill(Buf, Sections.empty() ? 101
Size0
:
Sections[0]->OutSecOff101
, Filler);
395
1.10k
396
1.10k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.10k
    InputSection *IS = Sections[I];
398
1.10k
    IS->writeTo<ELFT>(Buf);
399
1.10k
400
1.10k
    // Fill gaps between sections.
401
1.10k
    if (Filler) {
402
1.10k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.10k
      uint8_t *End;
404
1.10k
      if (I + 1 == Sections.size())
405
1.10k
        End = Buf + Size;
406
1.10k
      else
407
1.10k
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.10k
      fill(Start, End - Start, Filler);
409
1.10k
    }
410
1.10k
  });
411
1.10k
412
1.10k
  // Linker scripts may have BYTE()-family commands with which you
413
1.10k
  // can write arbitrary bytes to the output. Process them if any.
414
1.10k
  for (BaseCommand *Base : Commands)
415
1.10k
    
if (auto *1.10k
Data1.10k
= dyn_cast<BytesDataCommand>(Base))
416
0
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
1.19k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
369
1.77k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
1.77k
  if (Type == SHT_NOBITS)
371
46
    return;
372
1.73k
373
1.73k
  Loc = Buf;
374
1.73k
375
1.73k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
1.73k
  // we've already compressed section contents. If that's the case,
377
1.73k
  // just write it down.
378
1.73k
  if (
!CompressedData.empty()1.73k
) {
379
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
380
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
381
0
           CompressedData.size());
382
0
    return;
383
0
  }
384
1.73k
385
1.73k
  // Write leading padding.
386
1.73k
  std::vector<InputSection *> Sections;
387
1.73k
  for (BaseCommand *Cmd : Commands)
388
1.73k
    
if (auto *1.73k
ISD1.73k
= dyn_cast<InputSectionDescription>(Cmd))
389
1.73k
      for (InputSection *IS : ISD->Sections)
390
1.84k
        
if (1.84k
IS->Live1.84k
)
391
1.84k
          Sections.push_back(IS);
392
1.73k
  uint32_t Filler = getFiller();
393
1.73k
  if (Filler)
394
285
    
fill(Buf, Sections.empty() ? 285
Size0
:
Sections[0]->OutSecOff285
, Filler);
395
1.73k
396
1.73k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.73k
    InputSection *IS = Sections[I];
398
1.73k
    IS->writeTo<ELFT>(Buf);
399
1.73k
400
1.73k
    // Fill gaps between sections.
401
1.73k
    if (Filler) {
402
1.73k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.73k
      uint8_t *End;
404
1.73k
      if (I + 1 == Sections.size())
405
1.73k
        End = Buf + Size;
406
1.73k
      else
407
1.73k
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.73k
      fill(Start, End - Start, Filler);
409
1.73k
    }
410
1.73k
  });
411
1.73k
412
1.73k
  // Linker scripts may have BYTE()-family commands with which you
413
1.73k
  // can write arbitrary bytes to the output. Process them if any.
414
1.73k
  for (BaseCommand *Base : Commands)
415
1.73k
    
if (auto *1.73k
Data1.73k
= dyn_cast<BytesDataCommand>(Base))
416
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
1.77k
}
418
419
47
static bool compareByFilePosition(InputSection *A, InputSection *B) {
420
47
  // Synthetic doesn't have link order dependecy, stable_sort will keep it last
421
47
  if (A->kind() == InputSectionBase::Synthetic ||
422
31
      B->kind() == InputSectionBase::Synthetic)
423
16
    return false;
424
31
  InputSection *LA = A->getLinkOrderDep();
425
31
  InputSection *LB = B->getLinkOrderDep();
426
31
  OutputSection *AOut = LA->getParent();
427
31
  OutputSection *BOut = LB->getParent();
428
31
  if (AOut != BOut)
429
15
    return AOut->SectionIndex < BOut->SectionIndex;
430
16
  return LA->OutSecOff < LB->OutSecOff;
431
16
}
432
433
template <class ELFT>
434
static void finalizeShtGroup(OutputSection *OS,
435
8
                             ArrayRef<InputSection *> Sections) {
436
8
  assert(Config->Relocatable && Sections.size() == 1);
437
8
438
8
  // sh_link field for SHT_GROUP sections should contain the section index of
439
8
  // the symbol table.
440
8
  OS->Link = InX::SymTab->getParent()->SectionIndex;
441
8
442
8
  // sh_info then contain index of an entry in symbol table section which
443
8
  // provides signature of the section group.
444
8
  ObjFile<ELFT> *Obj = Sections[0]->getFile<ELFT>();
445
8
  ArrayRef<SymbolBody *> Symbols = Obj->getSymbols();
446
8
  OS->Info = InX::SymTab->getSymbolIndex(Symbols[Sections[0]->Info]);
447
8
}
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::OutputSection*, llvm::ArrayRef<lld::elf::InputSection*>)
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::OutputSection*, llvm::ArrayRef<lld::elf::InputSection*>)
OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::OutputSection*, llvm::ArrayRef<lld::elf::InputSection*>)
Line
Count
Source
435
8
                             ArrayRef<InputSection *> Sections) {
436
8
  assert(Config->Relocatable && Sections.size() == 1);
437
8
438
8
  // sh_link field for SHT_GROUP sections should contain the section index of
439
8
  // the symbol table.
440
8
  OS->Link = InX::SymTab->getParent()->SectionIndex;
441
8
442
8
  // sh_info then contain index of an entry in symbol table section which
443
8
  // provides signature of the section group.
444
8
  ObjFile<ELFT> *Obj = Sections[0]->getFile<ELFT>();
445
8
  ArrayRef<SymbolBody *> Symbols = Obj->getSymbols();
446
8
  OS->Info = InX::SymTab->getSymbolIndex(Symbols[Sections[0]->Info]);
447
8
}
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::OutputSection*, llvm::ArrayRef<lld::elf::InputSection*>)
448
449
209k
template <class ELFT> void OutputSection::finalize() {
450
209k
  // Link order may be distributed across several InputSectionDescriptions
451
209k
  // but sort must consider them all at once.
452
209k
  std::vector<InputSection **> ScriptSections;
453
209k
  std::vector<InputSection *> Sections;
454
209k
  for (BaseCommand *Base : Commands) {
455
209k
    if (auto *
ISD209k
= dyn_cast<InputSectionDescription>(Base)) {
456
210k
      for (InputSection *&IS : ISD->Sections) {
457
210k
        ScriptSections.push_back(&IS);
458
210k
        Sections.push_back(IS);
459
210k
      }
460
209k
    }
461
209k
  }
462
209k
463
209k
  if (
Flags & SHF_LINK_ORDER209k
) {
464
32
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
465
102
    for (int I = 0, N = Sections.size(); 
I < N102
;
++I70
)
466
70
      *ScriptSections[I] = Sections[I];
467
32
468
32
    // We must preserve the link order dependency of sections with the
469
32
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
470
32
    // need to translate the InputSection sh_link to the OutputSection sh_link,
471
32
    // all InputSections in the OutputSection have the same dependency.
472
32
    if (auto *D = Sections.front()->getLinkOrderDep())
473
31
      Link = D->getParent()->SectionIndex;
474
32
  }
475
209k
476
209k
  if (
Type == SHT_GROUP209k
) {
477
8
    finalizeShtGroup<ELFT>(this, Sections);
478
8
    return;
479
8
  }
480
209k
481
209k
  
if (209k
!Config->CopyRelocs || 209k
(Type != SHT_RELA && 482
Type != SHT_REL446
))
482
209k
    return;
483
51
484
51
  InputSection *First = Sections[0];
485
51
  if (isa<SyntheticSection>(First))
486
3
    return;
487
48
488
48
  Link = InX::SymTab->getParent()->SectionIndex;
489
48
  // sh_info for SHT_REL[A] sections should contain the section header index of
490
48
  // the section to which the relocation applies.
491
48
  InputSectionBase *S = First->getRelocatedSection();
492
48
  Info = S->getOutputSection()->SectionIndex;
493
48
  Flags |= SHF_INFO_LINK;
494
48
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
449
529
template <class ELFT> void OutputSection::finalize() {
450
529
  // Link order may be distributed across several InputSectionDescriptions
451
529
  // but sort must consider them all at once.
452
529
  std::vector<InputSection **> ScriptSections;
453
529
  std::vector<InputSection *> Sections;
454
543
  for (BaseCommand *Base : Commands) {
455
543
    if (auto *
ISD543
= dyn_cast<InputSectionDescription>(Base)) {
456
557
      for (InputSection *&IS : ISD->Sections) {
457
557
        ScriptSections.push_back(&IS);
458
557
        Sections.push_back(IS);
459
557
      }
460
535
    }
461
543
  }
462
529
463
529
  if (
Flags & SHF_LINK_ORDER529
) {
464
0
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
465
0
    for (int I = 0, N = Sections.size(); 
I < N0
;
++I0
)
466
0
      *ScriptSections[I] = Sections[I];
467
0
468
0
    // We must preserve the link order dependency of sections with the
469
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
470
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
471
0
    // all InputSections in the OutputSection have the same dependency.
472
0
    if (auto *D = Sections.front()->getLinkOrderDep())
473
0
      Link = D->getParent()->SectionIndex;
474
0
  }
475
529
476
529
  if (
Type == SHT_GROUP529
) {
477
0
    finalizeShtGroup<ELFT>(this, Sections);
478
0
    return;
479
0
  }
480
529
481
529
  
if (529
!Config->CopyRelocs || 529
(Type != SHT_RELA && 10
Type != SHT_REL10
))
482
529
    return;
483
0
484
0
  InputSection *First = Sections[0];
485
0
  if (isa<SyntheticSection>(First))
486
0
    return;
487
0
488
0
  Link = InX::SymTab->getParent()->SectionIndex;
489
0
  // sh_info for SHT_REL[A] sections should contain the section header index of
490
0
  // the section to which the relocation applies.
491
0
  InputSectionBase *S = First->getRelocatedSection();
492
0
  Info = S->getOutputSection()->SectionIndex;
493
0
  Flags |= SHF_INFO_LINK;
494
0
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
449
1.19k
template <class ELFT> void OutputSection::finalize() {
450
1.19k
  // Link order may be distributed across several InputSectionDescriptions
451
1.19k
  // but sort must consider them all at once.
452
1.19k
  std::vector<InputSection **> ScriptSections;
453
1.19k
  std::vector<InputSection *> Sections;
454
1.19k
  for (BaseCommand *Base : Commands) {
455
1.19k
    if (auto *
ISD1.19k
= dyn_cast<InputSectionDescription>(Base)) {
456
1.32k
      for (InputSection *&IS : ISD->Sections) {
457
1.32k
        ScriptSections.push_back(&IS);
458
1.32k
        Sections.push_back(IS);
459
1.32k
      }
460
1.19k
    }
461
1.19k
  }
462
1.19k
463
1.19k
  if (
Flags & SHF_LINK_ORDER1.19k
) {
464
0
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
465
0
    for (int I = 0, N = Sections.size(); 
I < N0
;
++I0
)
466
0
      *ScriptSections[I] = Sections[I];
467
0
468
0
    // We must preserve the link order dependency of sections with the
469
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
470
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
471
0
    // all InputSections in the OutputSection have the same dependency.
472
0
    if (auto *D = Sections.front()->getLinkOrderDep())
473
0
      Link = D->getParent()->SectionIndex;
474
0
  }
475
1.19k
476
1.19k
  if (
Type == SHT_GROUP1.19k
) {
477
0
    finalizeShtGroup<ELFT>(this, Sections);
478
0
    return;
479
0
  }
480
1.19k
481
1.19k
  
if (1.19k
!Config->CopyRelocs || 1.19k
(Type != SHT_RELA && 65
Type != SHT_REL65
))
482
1.18k
    return;
483
5
484
5
  InputSection *First = Sections[0];
485
5
  if (isa<SyntheticSection>(First))
486
0
    return;
487
5
488
5
  Link = InX::SymTab->getParent()->SectionIndex;
489
5
  // sh_info for SHT_REL[A] sections should contain the section header index of
490
5
  // the section to which the relocation applies.
491
5
  InputSectionBase *S = First->getRelocatedSection();
492
5
  Info = S->getOutputSection()->SectionIndex;
493
5
  Flags |= SHF_INFO_LINK;
494
5
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
449
205k
template <class ELFT> void OutputSection::finalize() {
450
205k
  // Link order may be distributed across several InputSectionDescriptions
451
205k
  // but sort must consider them all at once.
452
205k
  std::vector<InputSection **> ScriptSections;
453
205k
  std::vector<InputSection *> Sections;
454
205k
  for (BaseCommand *Base : Commands) {
455
205k
    if (auto *
ISD205k
= dyn_cast<InputSectionDescription>(Base)) {
456
206k
      for (InputSection *&IS : ISD->Sections) {
457
206k
        ScriptSections.push_back(&IS);
458
206k
        Sections.push_back(IS);
459
206k
      }
460
205k
    }
461
205k
  }
462
205k
463
205k
  if (
Flags & SHF_LINK_ORDER205k
) {
464
6
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
465
14
    for (int I = 0, N = Sections.size(); 
I < N14
;
++I8
)
466
8
      *ScriptSections[I] = Sections[I];
467
6
468
6
    // We must preserve the link order dependency of sections with the
469
6
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
470
6
    // need to translate the InputSection sh_link to the OutputSection sh_link,
471
6
    // all InputSections in the OutputSection have the same dependency.
472
6
    if (auto *D = Sections.front()->getLinkOrderDep())
473
5
      Link = D->getParent()->SectionIndex;
474
6
  }
475
205k
476
205k
  if (
Type == SHT_GROUP205k
) {
477
8
    finalizeShtGroup<ELFT>(this, Sections);
478
8
    return;
479
8
  }
480
205k
481
205k
  
if (205k
!Config->CopyRelocs || 205k
(Type != SHT_RELA && 347
Type != SHT_REL311
))
482
205k
    return;
483
36
484
36
  InputSection *First = Sections[0];
485
36
  if (isa<SyntheticSection>(First))
486
3
    return;
487
33
488
33
  Link = InX::SymTab->getParent()->SectionIndex;
489
33
  // sh_info for SHT_REL[A] sections should contain the section header index of
490
33
  // the section to which the relocation applies.
491
33
  InputSectionBase *S = First->getRelocatedSection();
492
33
  Info = S->getOutputSection()->SectionIndex;
493
33
  Flags |= SHF_INFO_LINK;
494
33
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
449
1.78k
template <class ELFT> void OutputSection::finalize() {
450
1.78k
  // Link order may be distributed across several InputSectionDescriptions
451
1.78k
  // but sort must consider them all at once.
452
1.78k
  std::vector<InputSection **> ScriptSections;
453
1.78k
  std::vector<InputSection *> Sections;
454
1.78k
  for (BaseCommand *Base : Commands) {
455
1.78k
    if (auto *
ISD1.78k
= dyn_cast<InputSectionDescription>(Base)) {
456
1.90k
      for (InputSection *&IS : ISD->Sections) {
457
1.90k
        ScriptSections.push_back(&IS);
458
1.90k
        Sections.push_back(IS);
459
1.90k
      }
460
1.78k
    }
461
1.78k
  }
462
1.78k
463
1.78k
  if (
Flags & SHF_LINK_ORDER1.78k
) {
464
26
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
465
88
    for (int I = 0, N = Sections.size(); 
I < N88
;
++I62
)
466
62
      *ScriptSections[I] = Sections[I];
467
26
468
26
    // We must preserve the link order dependency of sections with the
469
26
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
470
26
    // need to translate the InputSection sh_link to the OutputSection sh_link,
471
26
    // all InputSections in the OutputSection have the same dependency.
472
26
    if (auto *D = Sections.front()->getLinkOrderDep())
473
26
      Link = D->getParent()->SectionIndex;
474
26
  }
475
1.78k
476
1.78k
  if (
Type == SHT_GROUP1.78k
) {
477
0
    finalizeShtGroup<ELFT>(this, Sections);
478
0
    return;
479
0
  }
480
1.78k
481
1.78k
  
if (1.78k
!Config->CopyRelocs || 1.78k
(Type != SHT_RELA && 60
Type != SHT_REL60
))
482
1.77k
    return;
483
10
484
10
  InputSection *First = Sections[0];
485
10
  if (isa<SyntheticSection>(First))
486
0
    return;
487
10
488
10
  Link = InX::SymTab->getParent()->SectionIndex;
489
10
  // sh_info for SHT_REL[A] sections should contain the section header index of
490
10
  // the section to which the relocation applies.
491
10
  InputSectionBase *S = First->getRelocatedSection();
492
10
  Info = S->getOutputSection()->SectionIndex;
493
10
  Flags |= SHF_INFO_LINK;
494
10
}
495
496
// Returns true if S matches /Filename.?\.o$/.
497
76
static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
498
76
  if (!S.endswith(".o"))
499
60
    return false;
500
16
  S = S.drop_back(2);
501
16
  if (S.endswith(Filename))
502
12
    return true;
503
4
  
return !S.empty() && 4
S.drop_back().endswith(Filename)4
;
504
76
}
505
506
48
static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
507
28
static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
508
509
// .ctors and .dtors are sorted by this priority from highest to lowest.
510
//
511
//  1. The section was contained in crtbegin (crtbegin contains
512
//     some sentinel value in its .ctors and .dtors so that the runtime
513
//     can find the beginning of the sections.)
514
//
515
//  2. The section has an optional priority value in the form of ".ctors.N"
516
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
517
//     they are compared as string rather than number.
518
//
519
//  3. The section is just ".ctors" or ".dtors".
520
//
521
//  4. The section was contained in crtend, which contains an end marker.
522
//
523
// In an ideal world, we don't need this function because .init_array and
524
// .ctors are duplicate features (and .init_array is newer.) However, there
525
// are too many real-world use cases of .ctors, so we had no choice to
526
// support that with this rather ad-hoc semantics.
527
24
static bool compCtors(const InputSection *A, const InputSection *B) {
528
24
  bool BeginA = isCrtbegin(A->File->getName());
529
24
  bool BeginB = isCrtbegin(B->File->getName());
530
24
  if (BeginA != BeginB)
531
10
    return BeginA;
532
14
  bool EndA = isCrtend(A->File->getName());
533
14
  bool EndB = isCrtend(B->File->getName());
534
14
  if (EndA != EndB)
535
2
    return EndB;
536
12
  StringRef X = A->Name;
537
12
  StringRef Y = B->Name;
538
12
  assert(X.startswith(".ctors") || X.startswith(".dtors"));
539
12
  assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
540
12
  X = X.substr(6);
541
12
  Y = Y.substr(6);
542
12
  if (
X.empty() && 12
Y.empty()6
)
543
2
    return false;
544
10
  return X < Y;
545
10
}
546
547
// Sorts input sections by the special rules for .ctors and .dtors.
548
// Unfortunately, the rules are different from the one for .{init,fini}_array.
549
// Read the comment above.
550
8
void OutputSection::sortCtorsDtors() {
551
8
  assert(Commands.size() == 1);
552
8
  auto *ISD = cast<InputSectionDescription>(Commands[0]);
553
8
  std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
554
8
}
555
556
// If an input string is in the form of "foo.N" where N is a number,
557
// return N. Otherwise, returns 65536, which is one greater than the
558
// lowest priority.
559
19
int elf::getPriority(StringRef S) {
560
19
  size_t Pos = S.rfind('.');
561
19
  if (Pos == StringRef::npos)
562
0
    return 65536;
563
19
  int V;
564
19
  if (!to_integer(S.substr(Pos + 1), V, 10))
565
11
    return 65536;
566
8
  return V;
567
8
}
568
569
// Sorts input sections by section name suffixes, so that .foo.N comes
570
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
571
// We want to keep the original order if the priorities are the same
572
// because the compiler keeps the original initialization order in a
573
// translation unit and we need to respect that.
574
// For more detail, read the section of the GCC's manual about init_priority.
575
9
void OutputSection::sortInitFini() {
576
9
  // Sort sections by priority.
577
17
  sort([](InputSectionBase *S) { return getPriority(S->Name); });
578
9
}
579
580
208k
uint32_t OutputSection::getFiller() {
581
208k
  if (Filler)
582
10
    return *Filler;
583
208k
  
if (208k
Flags & SHF_EXECINSTR208k
)
584
1.86k
    return Target->TrapInstr;
585
206k
  return 0;
586
206k
}
587
588
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
589
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
590
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
591
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
592
593
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
594
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
595
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
596
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
597
598
template void OutputSection::maybeCompress<ELF32LE>();
599
template void OutputSection::maybeCompress<ELF32BE>();
600
template void OutputSection::maybeCompress<ELF64LE>();
601
template void OutputSection::maybeCompress<ELF64BE>();
602
603
template void OutputSection::finalize<ELF32LE>();
604
template void OutputSection::finalize<ELF32BE>();
605
template void OutputSection::finalize<ELF64LE>();
606
template void OutputSection::finalize<ELF64BE>();