Coverage Report

Created: 2017-09-19 22:28

/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.8k
uint32_t OutputSection::getPhdrFlags() const {
48
72.8k
  uint32_t Ret = PF_R;
49
72.8k
  if (Flags & SHF_WRITE)
50
2.40k
    Ret |= PF_W;
51
72.8k
  if (Flags & SHF_EXECINSTR)
52
1.75k
    Ret |= PF_X;
53
72.8k
  return Ret;
54
72.8k
}
55
56
template <class ELFT>
57
208k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
208k
  Shdr->sh_entsize = Entsize;
59
208k
  Shdr->sh_addralign = Alignment;
60
208k
  Shdr->sh_type = Type;
61
208k
  Shdr->sh_offset = Offset;
62
208k
  Shdr->sh_flags = Flags;
63
208k
  Shdr->sh_info = Info;
64
208k
  Shdr->sh_link = Link;
65
208k
  Shdr->sh_addr = Addr;
66
208k
  Shdr->sh_size = Size;
67
208k
  Shdr->sh_name = ShName;
68
208k
}
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.17k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
1.17k
  Shdr->sh_entsize = Entsize;
59
1.17k
  Shdr->sh_addralign = Alignment;
60
1.17k
  Shdr->sh_type = Type;
61
1.17k
  Shdr->sh_offset = Offset;
62
1.17k
  Shdr->sh_flags = Flags;
63
1.17k
  Shdr->sh_info = Info;
64
1.17k
  Shdr->sh_link = Link;
65
1.17k
  Shdr->sh_addr = Addr;
66
1.17k
  Shdr->sh_size = Size;
67
1.17k
  Shdr->sh_name = ShName;
68
1.17k
}
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.75k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58
1.75k
  Shdr->sh_entsize = Entsize;
59
1.75k
  Shdr->sh_addralign = Alignment;
60
1.75k
  Shdr->sh_type = Type;
61
1.75k
  Shdr->sh_offset = Offset;
62
1.75k
  Shdr->sh_flags = Flags;
63
1.75k
  Shdr->sh_info = Info;
64
1.75k
  Shdr->sh_link = Link;
65
1.75k
  Shdr->sh_addr = Addr;
66
1.75k
  Shdr->sh_size = Size;
67
1.75k
  Shdr->sh_name = ShName;
68
1.75k
}
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
227k
static uint64_t updateOffset(uint64_t Off, InputSection *S) {
80
227k
  Off = alignTo(Off, S->Alignment);
81
227k
  S->OutSecOff = Off;
82
227k
  return Off + S->getSize();
83
227k
}
84
85
227k
void OutputSection::addSection(InputSection *S) {
86
227k
  assert(S->Live);
87
227k
  Live = true;
88
227k
  S->Parent = this;
89
227k
  this->updateAlignment(S->Alignment);
90
227k
91
227k
  // The actual offsets will be computed by assignAddresses. For now, use
92
227k
  // crude approximation so that it is at least easy for other code to know the
93
227k
  // section order. It is also used to calculate the output section size early
94
227k
  // for compressed debug sections.
95
227k
  this->Size = updateOffset(Size, S);
96
227k
97
227k
  // If this section contains a table of fixed-size entries, sh_entsize
98
227k
  // holds the element size. Consequently, if this contains two or more
99
227k
  // input sections, all of them must have the same sh_entsize. However,
100
227k
  // you can put different types of input sections into one output
101
227k
  // sectin by using linker scripts. I don't know what to do here.
102
227k
  // Probably we sholuld handle that as an error. But for now we just
103
227k
  // pick the largest sh_entsize.
104
227k
  this->Entsize = std::max(this->Entsize, S->Entsize);
105
227k
106
227k
  if (
!S->Assigned227k
) {
107
227k
    S->Assigned = true;
108
227k
    if (
Commands.empty() || 227k
!isa<InputSectionDescription>(Commands.back())5.74k
)
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
227k
}
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.58k
OutputSectionFactory::OutputSectionFactory() {}
170
171
11.9k
static uint64_t getIncompatibleFlags(uint64_t Flags) {
172
11.9k
  return Flags & (SHF_ALLOC | SHF_TLS);
173
11.9k
}
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
489
void elf::reportDiscarded(InputSectionBase *IS) {
202
489
  if (!Config->PrintGcSections)
203
472
    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
227k
                                 OutputSection *Sec) {
210
227k
  if (
Sec && 227k
Sec->Live6.36k
) {
211
5.96k
    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
5.96k
    if (
Sec->Type != IS->Type5.96k
) {
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
5.96k
    Sec->Flags |= IS->Flags;
227
227k
  } else {
228
221k
    if (
!Sec221k
) {
229
221k
      Sec = Script->createOutputSection(OutsecName, "<internal>");
230
221k
      Script->Opt.Commands.push_back(Sec);
231
221k
    }
232
221k
    Sec->Type = IS->Type;
233
221k
    Sec->Flags = IS->Flags;
234
221k
  }
235
227k
236
227k
  Sec->addSection(cast<InputSection>(IS));
237
227k
  return Sec;
238
227k
}
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
227k
248
227k
  // If we have destination output section - use it directly.
249
227k
  
if (227k
OS227k
) {
250
690
    addSection(IS, OutsecName, OS);
251
690
    return;
252
690
  }
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.58k
OutputSectionFactory::~OutputSectionFactory() {}
286
287
755k
SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
288
755k
  return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
289
755k
}
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.91k
uint64_t elf::getHeaderSize() {
306
2.91k
  if (Config->OFormatBinary)
307
3
    return 0;
308
2.91k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
309
2.91k
}
310
311
4.30M
bool OutputSection::classof(const BaseCommand *C) {
312
4.30M
  return C->Kind == OutputSectionKind;
313
4.30M
}
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.04k
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
323
4.04k
  size_t I = 0;
324
6.43k
  for (; 
I + 4 < Size6.43k
;
I += 42.38k
)
325
2.38k
    memcpy(Buf + I, &Filler, 4);
326
4.04k
  memcpy(Buf + I, &Filler, Size - I);
327
4.04k
}
328
329
// Compress section contents if this section contains debug info.
330
208k
template <class ELFT> void OutputSection::maybeCompress() {
331
208k
  typedef typename ELFT::Chdr Elf_Chdr;
332
208k
333
208k
  // Compress only DWARF debug sections.
334
208k
  if (
!Config->CompressDebugSections || 208k
(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
528
template <class ELFT> void OutputSection::maybeCompress() {
331
528
  typedef typename ELFT::Chdr Elf_Chdr;
332
528
333
528
  // Compress only DWARF debug sections.
334
528
  if (
!Config->CompressDebugSections || 528
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
528
    return;
337
0
338
0
  // Create a section header.
339
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
0
  Hdr->ch_size = Size;
343
0
  Hdr->ch_addralign = Alignment;
344
0
345
0
  // Write section contents to a temporary buffer and compress it.
346
0
  std::vector<uint8_t> Buf(Size);
347
0
  writeTo<ELFT>(Buf.data());
348
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
0
351
0
  // Update section headers.
352
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
0
  Flags |= SHF_COMPRESSED;
354
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
330
1.17k
template <class ELFT> void OutputSection::maybeCompress() {
331
1.17k
  typedef typename ELFT::Chdr Elf_Chdr;
332
1.17k
333
1.17k
  // Compress only DWARF debug sections.
334
1.17k
  if (
!Config->CompressDebugSections || 1.17k
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
1.17k
    return;
337
0
338
0
  // Create a section header.
339
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
340
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
341
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
342
0
  Hdr->ch_size = Size;
343
0
  Hdr->ch_addralign = Alignment;
344
0
345
0
  // Write section contents to a temporary buffer and compress it.
346
0
  std::vector<uint8_t> Buf(Size);
347
0
  writeTo<ELFT>(Buf.data());
348
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
349
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
350
0
351
0
  // Update section headers.
352
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
353
0
  Flags |= SHF_COMPRESSED;
354
0
}
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.75k
template <class ELFT> void OutputSection::maybeCompress() {
331
1.75k
  typedef typename ELFT::Chdr Elf_Chdr;
332
1.75k
333
1.75k
  // Compress only DWARF debug sections.
334
1.75k
  if (
!Config->CompressDebugSections || 1.75k
(Flags & SHF_ALLOC)0
||
335
0
      !Name.startswith(".debug_"))
336
1.75k
    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
208k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
208k
  if (Type == SHT_NOBITS)
371
308
    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.71k
    
fill(Buf, Sections.empty() ? 1.71k
Size1
:
Sections[0]->OutSecOff1.71k
, 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.32k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
2.32k
      uint8_t *End;
404
2.32k
      if (I + 1 == Sections.size())
405
1.71k
        End = Buf + Size;
406
2.32k
      else
407
603
        End = Buf + Sections[I + 1]->OutSecOff;
408
2.32k
      fill(Start, End - Start, Filler);
409
2.32k
    }
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.18k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.18k
    InputSection *IS = Sections[I];
398
1.18k
    IS->writeTo<ELFT>(Buf);
399
1.18k
400
1.18k
    // Fill gaps between sections.
401
1.18k
    if (
Filler1.18k
) {
402
164
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
164
      uint8_t *End;
404
164
      if (I + 1 == Sections.size())
405
100
        End = Buf + Size;
406
164
      else
407
64
        End = Buf + Sections[I + 1]->OutSecOff;
408
164
      fill(Start, End - Start, Filler);
409
164
    }
410
1.18k
  });
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.77k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.77k
      uint8_t *End;
404
1.77k
      if (I + 1 == Sections.size())
405
1.31k
        End = Buf + Size;
406
1.77k
      else
407
461
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.77k
      fill(Start, End - Start, Filler);
409
1.77k
    }
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.82k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.82k
    InputSection *IS = Sections[I];
398
1.82k
    IS->writeTo<ELFT>(Buf);
399
1.82k
400
1.82k
    // Fill gaps between sections.
401
1.82k
    if (
Filler1.82k
) {
402
347
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
347
      uint8_t *End;
404
347
      if (I + 1 == Sections.size())
405
279
        End = Buf + Size;
406
347
      else
407
68
        End = Buf + Sections[I + 1]->OutSecOff;
408
347
      fill(Start, End - Start, Filler);
409
347
    }
410
1.82k
  });
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
208k
}
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
147
    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.31k
    
fill(Buf, Sections.empty() ? 1.31k
Size1
:
Sections[0]->OutSecOff1.31k
, 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.17k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
1.17k
  if (Type == SHT_NOBITS)
371
87
    return;
372
1.08k
373
1.08k
  Loc = Buf;
374
1.08k
375
1.08k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
1.08k
  // we've already compressed section contents. If that's the case,
377
1.08k
  // just write it down.
378
1.08k
  if (
!CompressedData.empty()1.08k
) {
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.08k
385
1.08k
  // Write leading padding.
386
1.08k
  std::vector<InputSection *> Sections;
387
1.08k
  for (BaseCommand *Cmd : Commands)
388
1.09k
    
if (auto *1.09k
ISD1.09k
= dyn_cast<InputSectionDescription>(Cmd))
389
1.09k
      for (InputSection *IS : ISD->Sections)
390
1.18k
        
if (1.18k
IS->Live1.18k
)
391
1.18k
          Sections.push_back(IS);
392
1.08k
  uint32_t Filler = getFiller();
393
1.08k
  if (Filler)
394
100
    
fill(Buf, Sections.empty() ? 100
Size0
:
Sections[0]->OutSecOff100
, Filler);
395
1.08k
396
1.08k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.08k
    InputSection *IS = Sections[I];
398
1.08k
    IS->writeTo<ELFT>(Buf);
399
1.08k
400
1.08k
    // Fill gaps between sections.
401
1.08k
    if (Filler) {
402
1.08k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.08k
      uint8_t *End;
404
1.08k
      if (I + 1 == Sections.size())
405
1.08k
        End = Buf + Size;
406
1.08k
      else
407
1.08k
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.08k
      fill(Start, End - Start, Filler);
409
1.08k
    }
410
1.08k
  });
411
1.08k
412
1.08k
  // Linker scripts may have BYTE()-family commands with which you
413
1.08k
  // can write arbitrary bytes to the output. Process them if any.
414
1.08k
  for (BaseCommand *Base : Commands)
415
1.09k
    
if (auto *1.09k
Data1.09k
= dyn_cast<BytesDataCommand>(Base))
416
0
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
1.17k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
369
1.75k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
370
1.75k
  if (Type == SHT_NOBITS)
371
46
    return;
372
1.70k
373
1.70k
  Loc = Buf;
374
1.70k
375
1.70k
  // If -compress-debug-section is specified and if this is a debug seciton,
376
1.70k
  // we've already compressed section contents. If that's the case,
377
1.70k
  // just write it down.
378
1.70k
  if (
!CompressedData.empty()1.70k
) {
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.70k
385
1.70k
  // Write leading padding.
386
1.70k
  std::vector<InputSection *> Sections;
387
1.70k
  for (BaseCommand *Cmd : Commands)
388
1.71k
    
if (auto *1.71k
ISD1.71k
= dyn_cast<InputSectionDescription>(Cmd))
389
1.70k
      for (InputSection *IS : ISD->Sections)
390
1.82k
        
if (1.82k
IS->Live1.82k
)
391
1.82k
          Sections.push_back(IS);
392
1.70k
  uint32_t Filler = getFiller();
393
1.70k
  if (Filler)
394
279
    
fill(Buf, Sections.empty() ? 279
Size0
:
Sections[0]->OutSecOff279
, Filler);
395
1.70k
396
1.70k
  parallelForEachN(0, Sections.size(), [=](size_t I) {
397
1.70k
    InputSection *IS = Sections[I];
398
1.70k
    IS->writeTo<ELFT>(Buf);
399
1.70k
400
1.70k
    // Fill gaps between sections.
401
1.70k
    if (Filler) {
402
1.70k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
403
1.70k
      uint8_t *End;
404
1.70k
      if (I + 1 == Sections.size())
405
1.70k
        End = Buf + Size;
406
1.70k
      else
407
1.70k
        End = Buf + Sections[I + 1]->OutSecOff;
408
1.70k
      fill(Start, End - Start, Filler);
409
1.70k
    }
410
1.70k
  });
411
1.70k
412
1.70k
  // Linker scripts may have BYTE()-family commands with which you
413
1.70k
  // can write arbitrary bytes to the output. Process them if any.
414
1.70k
  for (BaseCommand *Base : Commands)
415
1.71k
    
if (auto *1.71k
Data1.71k
= dyn_cast<BytesDataCommand>(Base))
416
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
417
1.75k
}
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 *209k
ISD209k
= dyn_cast<InputSectionDescription>(Base))
456
209k
      
for (InputSection *&IS : ISD->Sections) 209k
{
457
210k
        ScriptSections.push_back(&IS);
458
210k
        Sections.push_back(IS);
459
210k
      }
460
209k
461
209k
  if (
Flags & SHF_LINK_ORDER209k
) {
462
32
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
463
102
    for (int I = 0, N = Sections.size(); 
I < N102
;
++I70
)
464
70
      *ScriptSections[I] = Sections[I];
465
32
466
32
    // We must preserve the link order dependency of sections with the
467
32
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
468
32
    // need to translate the InputSection sh_link to the OutputSection sh_link,
469
32
    // all InputSections in the OutputSection have the same dependency.
470
32
    if (auto *D = Sections.front()->getLinkOrderDep())
471
31
      Link = D->getParent()->SectionIndex;
472
32
  }
473
209k
474
209k
  if (
Type == SHT_GROUP209k
) {
475
8
    finalizeShtGroup<ELFT>(this, Sections);
476
8
    return;
477
8
  }
478
209k
479
209k
  
if (209k
!Config->CopyRelocs || 209k
(Type != SHT_RELA && 482
Type != SHT_REL446
))
480
208k
    return;
481
51
482
51
  InputSection *First = Sections[0];
483
51
  if (isa<SyntheticSection>(First))
484
3
    return;
485
48
486
48
  Link = InX::SymTab->getParent()->SectionIndex;
487
48
  // sh_info for SHT_REL[A] sections should contain the section header index of
488
48
  // the section to which the relocation applies.
489
48
  InputSectionBase *S = First->getRelocatedSection();
490
48
  Info = S->getOutputSection()->SectionIndex;
491
48
  Flags |= SHF_INFO_LINK;
492
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
529
  for (BaseCommand *Base : Commands)
455
543
    
if (auto *543
ISD543
= dyn_cast<InputSectionDescription>(Base))
456
535
      
for (InputSection *&IS : ISD->Sections) 535
{
457
557
        ScriptSections.push_back(&IS);
458
557
        Sections.push_back(IS);
459
557
      }
460
529
461
529
  if (
Flags & SHF_LINK_ORDER529
) {
462
0
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
463
0
    for (int I = 0, N = Sections.size(); 
I < N0
;
++I0
)
464
0
      *ScriptSections[I] = Sections[I];
465
0
466
0
    // We must preserve the link order dependency of sections with the
467
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
468
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
469
0
    // all InputSections in the OutputSection have the same dependency.
470
0
    if (auto *D = Sections.front()->getLinkOrderDep())
471
0
      Link = D->getParent()->SectionIndex;
472
0
  }
473
529
474
529
  if (
Type == SHT_GROUP529
) {
475
0
    finalizeShtGroup<ELFT>(this, Sections);
476
0
    return;
477
0
  }
478
529
479
529
  
if (529
!Config->CopyRelocs || 529
(Type != SHT_RELA && 10
Type != SHT_REL10
))
480
529
    return;
481
0
482
0
  InputSection *First = Sections[0];
483
0
  if (isa<SyntheticSection>(First))
484
0
    return;
485
0
486
0
  Link = InX::SymTab->getParent()->SectionIndex;
487
0
  // sh_info for SHT_REL[A] sections should contain the section header index of
488
0
  // the section to which the relocation applies.
489
0
  InputSectionBase *S = First->getRelocatedSection();
490
0
  Info = S->getOutputSection()->SectionIndex;
491
0
  Flags |= SHF_INFO_LINK;
492
0
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
449
1.17k
template <class ELFT> void OutputSection::finalize() {
450
1.17k
  // Link order may be distributed across several InputSectionDescriptions
451
1.17k
  // but sort must consider them all at once.
452
1.17k
  std::vector<InputSection **> ScriptSections;
453
1.17k
  std::vector<InputSection *> Sections;
454
1.17k
  for (BaseCommand *Base : Commands)
455
1.17k
    
if (auto *1.17k
ISD1.17k
= dyn_cast<InputSectionDescription>(Base))
456
1.17k
      
for (InputSection *&IS : ISD->Sections) 1.17k
{
457
1.30k
        ScriptSections.push_back(&IS);
458
1.30k
        Sections.push_back(IS);
459
1.30k
      }
460
1.17k
461
1.17k
  if (
Flags & SHF_LINK_ORDER1.17k
) {
462
0
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
463
0
    for (int I = 0, N = Sections.size(); 
I < N0
;
++I0
)
464
0
      *ScriptSections[I] = Sections[I];
465
0
466
0
    // We must preserve the link order dependency of sections with the
467
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
468
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
469
0
    // all InputSections in the OutputSection have the same dependency.
470
0
    if (auto *D = Sections.front()->getLinkOrderDep())
471
0
      Link = D->getParent()->SectionIndex;
472
0
  }
473
1.17k
474
1.17k
  if (
Type == SHT_GROUP1.17k
) {
475
0
    finalizeShtGroup<ELFT>(this, Sections);
476
0
    return;
477
0
  }
478
1.17k
479
1.17k
  
if (1.17k
!Config->CopyRelocs || 1.17k
(Type != SHT_RELA && 65
Type != SHT_REL65
))
480
1.17k
    return;
481
5
482
5
  InputSection *First = Sections[0];
483
5
  if (isa<SyntheticSection>(First))
484
0
    return;
485
5
486
5
  Link = InX::SymTab->getParent()->SectionIndex;
487
5
  // sh_info for SHT_REL[A] sections should contain the section header index of
488
5
  // the section to which the relocation applies.
489
5
  InputSectionBase *S = First->getRelocatedSection();
490
5
  Info = S->getOutputSection()->SectionIndex;
491
5
  Flags |= SHF_INFO_LINK;
492
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 *205k
ISD205k
= dyn_cast<InputSectionDescription>(Base))
456
205k
      
for (InputSection *&IS : ISD->Sections) 205k
{
457
206k
        ScriptSections.push_back(&IS);
458
206k
        Sections.push_back(IS);
459
206k
      }
460
205k
461
205k
  if (
Flags & SHF_LINK_ORDER205k
) {
462
6
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
463
14
    for (int I = 0, N = Sections.size(); 
I < N14
;
++I8
)
464
8
      *ScriptSections[I] = Sections[I];
465
6
466
6
    // We must preserve the link order dependency of sections with the
467
6
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
468
6
    // need to translate the InputSection sh_link to the OutputSection sh_link,
469
6
    // all InputSections in the OutputSection have the same dependency.
470
6
    if (auto *D = Sections.front()->getLinkOrderDep())
471
5
      Link = D->getParent()->SectionIndex;
472
6
  }
473
205k
474
205k
  if (
Type == SHT_GROUP205k
) {
475
8
    finalizeShtGroup<ELFT>(this, Sections);
476
8
    return;
477
8
  }
478
205k
479
205k
  
if (205k
!Config->CopyRelocs || 205k
(Type != SHT_RELA && 347
Type != SHT_REL311
))
480
205k
    return;
481
36
482
36
  InputSection *First = Sections[0];
483
36
  if (isa<SyntheticSection>(First))
484
3
    return;
485
33
486
33
  Link = InX::SymTab->getParent()->SectionIndex;
487
33
  // sh_info for SHT_REL[A] sections should contain the section header index of
488
33
  // the section to which the relocation applies.
489
33
  InputSectionBase *S = First->getRelocatedSection();
490
33
  Info = S->getOutputSection()->SectionIndex;
491
33
  Flags |= SHF_INFO_LINK;
492
33
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
449
1.76k
template <class ELFT> void OutputSection::finalize() {
450
1.76k
  // Link order may be distributed across several InputSectionDescriptions
451
1.76k
  // but sort must consider them all at once.
452
1.76k
  std::vector<InputSection **> ScriptSections;
453
1.76k
  std::vector<InputSection *> Sections;
454
1.76k
  for (BaseCommand *Base : Commands)
455
1.76k
    
if (auto *1.76k
ISD1.76k
= dyn_cast<InputSectionDescription>(Base))
456
1.75k
      
for (InputSection *&IS : ISD->Sections) 1.75k
{
457
1.87k
        ScriptSections.push_back(&IS);
458
1.87k
        Sections.push_back(IS);
459
1.87k
      }
460
1.76k
461
1.76k
  if (
Flags & SHF_LINK_ORDER1.76k
) {
462
26
    std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
463
88
    for (int I = 0, N = Sections.size(); 
I < N88
;
++I62
)
464
62
      *ScriptSections[I] = Sections[I];
465
26
466
26
    // We must preserve the link order dependency of sections with the
467
26
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
468
26
    // need to translate the InputSection sh_link to the OutputSection sh_link,
469
26
    // all InputSections in the OutputSection have the same dependency.
470
26
    if (auto *D = Sections.front()->getLinkOrderDep())
471
26
      Link = D->getParent()->SectionIndex;
472
26
  }
473
1.76k
474
1.76k
  if (
Type == SHT_GROUP1.76k
) {
475
0
    finalizeShtGroup<ELFT>(this, Sections);
476
0
    return;
477
0
  }
478
1.76k
479
1.76k
  
if (1.76k
!Config->CopyRelocs || 1.76k
(Type != SHT_RELA && 60
Type != SHT_REL60
))
480
1.75k
    return;
481
10
482
10
  InputSection *First = Sections[0];
483
10
  if (isa<SyntheticSection>(First))
484
0
    return;
485
10
486
10
  Link = InX::SymTab->getParent()->SectionIndex;
487
10
  // sh_info for SHT_REL[A] sections should contain the section header index of
488
10
  // the section to which the relocation applies.
489
10
  InputSectionBase *S = First->getRelocatedSection();
490
10
  Info = S->getOutputSection()->SectionIndex;
491
10
  Flags |= SHF_INFO_LINK;
492
10
}
493
494
// Returns true if S matches /Filename.?\.o$/.
495
76
static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
496
76
  if (!S.endswith(".o"))
497
60
    return false;
498
16
  S = S.drop_back(2);
499
16
  if (S.endswith(Filename))
500
12
    return true;
501
4
  
return !S.empty() && 4
S.drop_back().endswith(Filename)4
;
502
76
}
503
504
48
static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
505
28
static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
506
507
// .ctors and .dtors are sorted by this priority from highest to lowest.
508
//
509
//  1. The section was contained in crtbegin (crtbegin contains
510
//     some sentinel value in its .ctors and .dtors so that the runtime
511
//     can find the beginning of the sections.)
512
//
513
//  2. The section has an optional priority value in the form of ".ctors.N"
514
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
515
//     they are compared as string rather than number.
516
//
517
//  3. The section is just ".ctors" or ".dtors".
518
//
519
//  4. The section was contained in crtend, which contains an end marker.
520
//
521
// In an ideal world, we don't need this function because .init_array and
522
// .ctors are duplicate features (and .init_array is newer.) However, there
523
// are too many real-world use cases of .ctors, so we had no choice to
524
// support that with this rather ad-hoc semantics.
525
24
static bool compCtors(const InputSection *A, const InputSection *B) {
526
24
  bool BeginA = isCrtbegin(A->File->getName());
527
24
  bool BeginB = isCrtbegin(B->File->getName());
528
24
  if (BeginA != BeginB)
529
10
    return BeginA;
530
14
  bool EndA = isCrtend(A->File->getName());
531
14
  bool EndB = isCrtend(B->File->getName());
532
14
  if (EndA != EndB)
533
2
    return EndB;
534
12
  StringRef X = A->Name;
535
12
  StringRef Y = B->Name;
536
12
  assert(X.startswith(".ctors") || X.startswith(".dtors"));
537
12
  assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
538
12
  X = X.substr(6);
539
12
  Y = Y.substr(6);
540
12
  if (
X.empty() && 12
Y.empty()6
)
541
2
    return false;
542
10
  return X < Y;
543
10
}
544
545
// Sorts input sections by the special rules for .ctors and .dtors.
546
// Unfortunately, the rules are different from the one for .{init,fini}_array.
547
// Read the comment above.
548
8
void OutputSection::sortCtorsDtors() {
549
8
  assert(Commands.size() == 1);
550
8
  auto *ISD = cast<InputSectionDescription>(Commands[0]);
551
8
  std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
552
8
}
553
554
// If an input string is in the form of "foo.N" where N is a number,
555
// return N. Otherwise, returns 65536, which is one greater than the
556
// lowest priority.
557
19
int elf::getPriority(StringRef S) {
558
19
  size_t Pos = S.rfind('.');
559
19
  if (Pos == StringRef::npos)
560
0
    return 65536;
561
19
  int V;
562
19
  if (!to_integer(S.substr(Pos + 1), V, 10))
563
11
    return 65536;
564
8
  return V;
565
8
}
566
567
// Sorts input sections by section name suffixes, so that .foo.N comes
568
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
569
// We want to keep the original order if the priorities are the same
570
// because the compiler keeps the original initialization order in a
571
// translation unit and we need to respect that.
572
// For more detail, read the section of the GCC's manual about init_priority.
573
9
void OutputSection::sortInitFini() {
574
9
  // Sort sections by priority.
575
17
  sort([](InputSectionBase *S) { return getPriority(S->Name); });
576
9
}
577
578
208k
uint32_t OutputSection::getFiller() {
579
208k
  if (Filler)
580
10
    return *Filler;
581
208k
  
if (208k
Flags & SHF_EXECINSTR208k
)
582
1.84k
    return Target->TrapInstr;
583
206k
  return 0;
584
206k
}
585
586
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
587
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
588
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
589
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
590
591
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
592
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
593
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
594
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
595
596
template void OutputSection::maybeCompress<ELF32LE>();
597
template void OutputSection::maybeCompress<ELF32BE>();
598
template void OutputSection::maybeCompress<ELF64LE>();
599
template void OutputSection::maybeCompress<ELF64BE>();
600
601
template void OutputSection::finalize<ELF32LE>();
602
template void OutputSection::finalize<ELF32BE>();
603
template void OutputSection::finalize<ELF64LE>();
604
template void OutputSection::finalize<ELF64BE>();