Coverage Report

Created: 2019-01-18 03:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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 "SymbolTable.h"
14
#include "SyntheticSections.h"
15
#include "Target.h"
16
#include "lld/Common/Memory.h"
17
#include "lld/Common/Strings.h"
18
#include "lld/Common/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
PhdrEntry *Out::TlsPhdr;
36
OutputSection *Out::ElfHeader;
37
OutputSection *Out::ProgramHeaders;
38
OutputSection *Out::PreinitArray;
39
OutputSection *Out::InitArray;
40
OutputSection *Out::FiniArray;
41
42
std::vector<OutputSection *> elf::OutputSections;
43
44
79.7k
uint32_t OutputSection::getPhdrFlags() const {
45
79.7k
  uint32_t Ret = 0;
46
79.7k
  if (Config->EMachine != EM_ARM || 
!(Flags & SHF_ARM_PURECODE)743
)
47
79.7k
    Ret |= PF_R;
48
79.7k
  if (Flags & SHF_WRITE)
49
3.79k
    Ret |= PF_W;
50
79.7k
  if (Flags & SHF_EXECINSTR)
51
4.76k
    Ret |= PF_X;
52
79.7k
  return Ret;
53
79.7k
}
54
55
template <class ELFT>
56
349k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
349k
  Shdr->sh_entsize = Entsize;
58
349k
  Shdr->sh_addralign = Alignment;
59
349k
  Shdr->sh_type = Type;
60
349k
  Shdr->sh_offset = Offset;
61
349k
  Shdr->sh_flags = Flags;
62
349k
  Shdr->sh_info = Info;
63
349k
  Shdr->sh_link = Link;
64
349k
  Shdr->sh_addr = Addr;
65
349k
  Shdr->sh_size = Size;
66
349k
  Shdr->sh_name = ShName;
67
349k
}
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
56
2.80k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
2.80k
  Shdr->sh_entsize = Entsize;
58
2.80k
  Shdr->sh_addralign = Alignment;
59
2.80k
  Shdr->sh_type = Type;
60
2.80k
  Shdr->sh_offset = Offset;
61
2.80k
  Shdr->sh_flags = Flags;
62
2.80k
  Shdr->sh_info = Info;
63
2.80k
  Shdr->sh_link = Link;
64
2.80k
  Shdr->sh_addr = Addr;
65
2.80k
  Shdr->sh_size = Size;
66
2.80k
  Shdr->sh_name = ShName;
67
2.80k
}
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
56
1.57k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
1.57k
  Shdr->sh_entsize = Entsize;
58
1.57k
  Shdr->sh_addralign = Alignment;
59
1.57k
  Shdr->sh_type = Type;
60
1.57k
  Shdr->sh_offset = Offset;
61
1.57k
  Shdr->sh_flags = Flags;
62
1.57k
  Shdr->sh_info = Info;
63
1.57k
  Shdr->sh_link = Link;
64
1.57k
  Shdr->sh_addr = Addr;
65
1.57k
  Shdr->sh_size = Size;
66
1.57k
  Shdr->sh_name = ShName;
67
1.57k
}
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
56
344k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
344k
  Shdr->sh_entsize = Entsize;
58
344k
  Shdr->sh_addralign = Alignment;
59
344k
  Shdr->sh_type = Type;
60
344k
  Shdr->sh_offset = Offset;
61
344k
  Shdr->sh_flags = Flags;
62
344k
  Shdr->sh_info = Info;
63
344k
  Shdr->sh_link = Link;
64
344k
  Shdr->sh_addr = Addr;
65
344k
  Shdr->sh_size = Size;
66
344k
  Shdr->sh_name = ShName;
67
344k
}
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
56
1.21k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
1.21k
  Shdr->sh_entsize = Entsize;
58
1.21k
  Shdr->sh_addralign = Alignment;
59
1.21k
  Shdr->sh_type = Type;
60
1.21k
  Shdr->sh_offset = Offset;
61
1.21k
  Shdr->sh_flags = Flags;
62
1.21k
  Shdr->sh_info = Info;
63
1.21k
  Shdr->sh_link = Link;
64
1.21k
  Shdr->sh_addr = Addr;
65
1.21k
  Shdr->sh_size = Size;
66
1.21k
  Shdr->sh_name = ShName;
67
1.21k
}
68
69
OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
70
    : BaseCommand(OutputSectionKind),
71
      SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
72
379k
                  /*Info*/ 0, /*Link*/ 0) {
73
379k
  Live = false;
74
379k
}
75
76
// We allow sections of types listed below to merged into a
77
// single progbits section. This is typically done by linker
78
// scripts. Merging nobits and progbits will force disk space
79
// to be allocated for nobits sections. Other ones don't require
80
// any special treatment on top of progbits, so there doesn't
81
// seem to be a harm in merging them.
82
21
static bool canMergeToProgbits(unsigned Type) {
83
21
  return Type == SHT_NOBITS || 
Type == SHT_PROGBITS15
||
Type == SHT_INIT_ARRAY5
||
84
21
         
Type == SHT_PREINIT_ARRAY4
||
Type == SHT_FINI_ARRAY3
||
85
21
         
Type == SHT_NOTE2
;
86
21
}
87
88
381k
void OutputSection::addSection(InputSection *IS) {
89
381k
  if (!Live) {
90
372k
    // If IS is the first section to be added to this section,
91
372k
    // initialize Type, Entsize and flags from IS.
92
372k
    Live = true;
93
372k
    Type = IS->Type;
94
372k
    Entsize = IS->Entsize;
95
372k
    Flags = IS->Flags;
96
372k
  } else {
97
9.64k
    // Otherwise, check if new type or flags are compatible with existing ones.
98
9.64k
    unsigned Mask = SHF_TLS | SHF_LINK_ORDER;
99
9.64k
    if ((Flags & Mask) != (IS->Flags & Mask))
100
3
      error("incompatible section flags for " + Name + "\n>>> " + toString(IS) +
101
3
            ": 0x" + utohexstr(IS->Flags) + "\n>>> output section " + Name +
102
3
            ": 0x" + utohexstr(Flags));
103
9.64k
104
9.64k
    if (Type != IS->Type) {
105
11
      if (!canMergeToProgbits(Type) || 
!canMergeToProgbits(IS->Type)10
)
106
1
        error("section type mismatch for " + IS->Name + "\n>>> " +
107
1
              toString(IS) + ": " +
108
1
              getELFSectionTypeName(Config->EMachine, IS->Type) +
109
1
              "\n>>> output section " + Name + ": " +
110
1
              getELFSectionTypeName(Config->EMachine, Type));
111
11
      Type = SHT_PROGBITS;
112
11
    }
113
9.64k
  }
114
381k
115
381k
  IS->Parent = this;
116
381k
  uint64_t AndMask =
117
381k
      Config->EMachine == EM_ARM ? 
(uint64_t)SHF_ARM_PURECODE3.67k
:
0378k
;
118
381k
  uint64_t OrMask = ~AndMask;
119
381k
  uint64_t AndFlags = (Flags & IS->Flags) & AndMask;
120
381k
  uint64_t OrFlags = (Flags | IS->Flags) & OrMask;
121
381k
  Flags = AndFlags | OrFlags;
122
381k
123
381k
  Alignment = std::max(Alignment, IS->Alignment);
124
381k
125
381k
  // If this section contains a table of fixed-size entries, sh_entsize
126
381k
  // holds the element size. If it contains elements of different size we
127
381k
  // set sh_entsize to 0.
128
381k
  if (Entsize != IS->Entsize)
129
13
    Entsize = 0;
130
381k
131
381k
  if (!IS->Assigned) {
132
380k
    IS->Assigned = true;
133
380k
    if (SectionCommands.empty() ||
134
380k
        
!isa<InputSectionDescription>(SectionCommands.back())9.22k
)
135
371k
      SectionCommands.push_back(make<InputSectionDescription>(""));
136
380k
    auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
137
380k
    ISD->Sections.push_back(IS);
138
380k
  }
139
381k
}
140
141
static void sortByOrder(MutableArrayRef<InputSection *> In,
142
9
                        llvm::function_ref<int(InputSectionBase *S)> Order) {
143
9
  typedef std::pair<int, InputSection *> Pair;
144
12
  auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
145
9
146
9
  std::vector<Pair> V;
147
9
  for (InputSection *S : In)
148
17
    V.push_back({Order(S), S});
149
9
  std::stable_sort(V.begin(), V.end(), Comp);
150
9
151
26
  for (size_t I = 0; I < V.size(); 
++I17
)
152
17
    In[I] = V[I].second;
153
9
}
154
155
4.72k
uint64_t elf::getHeaderSize() {
156
4.72k
  if (Config->OFormatBinary)
157
3
    return 0;
158
4.72k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
159
4.72k
}
160
161
5.47M
bool OutputSection::classof(const BaseCommand *C) {
162
5.47M
  return C->Kind == OutputSectionKind;
163
5.47M
}
164
165
9
void OutputSection::sort(llvm::function_ref<int(InputSectionBase *S)> Order) {
166
9
  assert(Live);
167
9
  for (BaseCommand *B : SectionCommands)
168
9
    if (auto *ISD = dyn_cast<InputSectionDescription>(B))
169
9
      sortByOrder(ISD->Sections, Order);
170
9
}
171
172
// Fill [Buf, Buf + Size) with Filler.
173
// This is used for linker script "=fillexp" command.
174
static void fill(uint8_t *Buf, size_t Size,
175
10.8k
                 const std::array<uint8_t, 4> &Filler) {
176
10.8k
  size_t I = 0;
177
60.2M
  for (; I + 4 < Size; 
I += 460.2M
)
178
60.2M
    memcpy(Buf + I, Filler.data(), 4);
179
10.8k
  memcpy(Buf + I, Filler.data(), Size - I);
180
10.8k
}
181
182
// Compress section contents if this section contains debug info.
183
349k
template <class ELFT> void OutputSection::maybeCompress() {
184
349k
  typedef typename ELFT::Chdr Elf_Chdr;
185
349k
186
349k
  // Compress only DWARF debug sections.
187
349k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
188
349k
      
!Name.startswith(".debug_")32
)
189
349k
    return;
190
8
191
8
  // Create a section header.
192
8
  ZDebugHeader.resize(sizeof(Elf_Chdr));
193
8
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
194
8
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
195
8
  Hdr->ch_size = Size;
196
8
  Hdr->ch_addralign = Alignment;
197
8
198
8
  // Write section contents to a temporary buffer and compress it.
199
8
  std::vector<uint8_t> Buf(Size);
200
8
  writeTo<ELFT>(Buf.data());
201
8
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
202
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
203
8
204
8
  // Update section headers.
205
8
  Size = sizeof(Elf_Chdr) + CompressedData.size();
206
8
  Flags |= SHF_COMPRESSED;
207
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
183
2.81k
template <class ELFT> void OutputSection::maybeCompress() {
184
2.81k
  typedef typename ELFT::Chdr Elf_Chdr;
185
2.81k
186
2.81k
  // Compress only DWARF debug sections.
187
2.81k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
188
2.81k
      
!Name.startswith(".debug_")0
)
189
2.81k
    return;
190
0
191
0
  // Create a section header.
192
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
193
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
194
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
195
0
  Hdr->ch_size = Size;
196
0
  Hdr->ch_addralign = Alignment;
197
0
198
0
  // Write section contents to a temporary buffer and compress it.
199
0
  std::vector<uint8_t> Buf(Size);
200
0
  writeTo<ELFT>(Buf.data());
201
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
202
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
203
0
204
0
  // Update section headers.
205
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
206
0
  Flags |= SHF_COMPRESSED;
207
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
183
1.57k
template <class ELFT> void OutputSection::maybeCompress() {
184
1.57k
  typedef typename ELFT::Chdr Elf_Chdr;
185
1.57k
186
1.57k
  // Compress only DWARF debug sections.
187
1.57k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
188
1.57k
      
!Name.startswith(".debug_")0
)
189
1.57k
    return;
190
0
191
0
  // Create a section header.
192
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
193
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
194
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
195
0
  Hdr->ch_size = Size;
196
0
  Hdr->ch_addralign = Alignment;
197
0
198
0
  // Write section contents to a temporary buffer and compress it.
199
0
  std::vector<uint8_t> Buf(Size);
200
0
  writeTo<ELFT>(Buf.data());
201
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
202
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
203
0
204
0
  // Update section headers.
205
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
206
0
  Flags |= SHF_COMPRESSED;
207
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
183
344k
template <class ELFT> void OutputSection::maybeCompress() {
184
344k
  typedef typename ELFT::Chdr Elf_Chdr;
185
344k
186
344k
  // Compress only DWARF debug sections.
187
344k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
188
344k
      
!Name.startswith(".debug_")32
)
189
344k
    return;
190
8
191
8
  // Create a section header.
192
8
  ZDebugHeader.resize(sizeof(Elf_Chdr));
193
8
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
194
8
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
195
8
  Hdr->ch_size = Size;
196
8
  Hdr->ch_addralign = Alignment;
197
8
198
8
  // Write section contents to a temporary buffer and compress it.
199
8
  std::vector<uint8_t> Buf(Size);
200
8
  writeTo<ELFT>(Buf.data());
201
8
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
202
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
203
8
204
8
  // Update section headers.
205
8
  Size = sizeof(Elf_Chdr) + CompressedData.size();
206
8
  Flags |= SHF_COMPRESSED;
207
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
183
1.21k
template <class ELFT> void OutputSection::maybeCompress() {
184
1.21k
  typedef typename ELFT::Chdr Elf_Chdr;
185
1.21k
186
1.21k
  // Compress only DWARF debug sections.
187
1.21k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
188
1.21k
      
!Name.startswith(".debug_")0
)
189
1.21k
    return;
190
0
191
0
  // Create a section header.
192
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
193
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
194
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
195
0
  Hdr->ch_size = Size;
196
0
  Hdr->ch_addralign = Alignment;
197
0
198
0
  // Write section contents to a temporary buffer and compress it.
199
0
  std::vector<uint8_t> Buf(Size);
200
0
  writeTo<ELFT>(Buf.data());
201
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
202
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
203
0
204
0
  // Update section headers.
205
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
206
0
  Flags |= SHF_COMPRESSED;
207
0
}
208
209
43
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
210
43
  if (Size == 1)
211
15
    *Buf = Data;
212
28
  else if (Size == 2)
213
7
    write16(Buf, Data);
214
21
  else if (Size == 4)
215
8
    write32(Buf, Data);
216
13
  else if (Size == 8)
217
13
    write64(Buf, Data);
218
13
  else
219
13
    
llvm_unreachable0
("unsupported Size argument");
220
43
}
221
222
349k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
223
349k
  if (Type == SHT_NOBITS)
224
504
    return;
225
349k
226
349k
  Loc = Buf;
227
349k
228
349k
  // If -compress-debug-section is specified and if this is a debug seciton,
229
349k
  // we've already compressed section contents. If that's the case,
230
349k
  // just write it down.
231
349k
  if (!CompressedData.empty()) {
232
8
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
233
8
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
234
8
           CompressedData.size());
235
8
    return;
236
8
  }
237
349k
238
349k
  // Write leading padding.
239
349k
  std::vector<InputSection *> Sections = getInputSections(this);
240
349k
  std::array<uint8_t, 4> Filler = getFiller();
241
349k
  bool NonZeroFiller = read32(Filler.data()) != 0;
242
349k
  if (NonZeroFiller)
243
4.70k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff4.70k
, Filler);
244
349k
245
351k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
351k
    InputSection *IS = Sections[I];
247
351k
    IS->writeTo<ELFT>(Buf);
248
351k
249
351k
    // Fill gaps between sections.
250
351k
    if (NonZeroFiller) {
251
6.18k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
6.18k
      uint8_t *End;
253
6.18k
      if (I + 1 == Sections.size())
254
4.70k
        End = Buf + Size;
255
1.48k
      else
256
1.48k
        End = Buf + Sections[I + 1]->OutSecOff;
257
6.18k
      fill(Start, End - Start, Filler);
258
6.18k
    }
259
351k
  });
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
245
3.29k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
3.29k
    InputSection *IS = Sections[I];
247
3.29k
    IS->writeTo<ELFT>(Buf);
248
3.29k
249
3.29k
    // Fill gaps between sections.
250
3.29k
    if (NonZeroFiller) {
251
901
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
901
      uint8_t *End;
253
901
      if (I + 1 == Sections.size())
254
423
        End = Buf + Size;
255
478
      else
256
478
        End = Buf + Sections[I + 1]->OutSecOff;
257
901
      fill(Start, End - Start, Filler);
258
901
    }
259
3.29k
  });
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
245
1.57k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
1.57k
    InputSection *IS = Sections[I];
247
1.57k
    IS->writeTo<ELFT>(Buf);
248
1.57k
249
1.57k
    // Fill gaps between sections.
250
1.57k
    if (NonZeroFiller) {
251
207
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
207
      uint8_t *End;
253
207
      if (I + 1 == Sections.size())
254
133
        End = Buf + Size;
255
74
      else
256
74
        End = Buf + Sections[I + 1]->OutSecOff;
257
207
      fill(Start, End - Start, Filler);
258
207
    }
259
1.57k
  });
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
245
345k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
345k
    InputSection *IS = Sections[I];
247
345k
    IS->writeTo<ELFT>(Buf);
248
345k
249
345k
    // Fill gaps between sections.
250
345k
    if (NonZeroFiller) {
251
4.89k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
4.89k
      uint8_t *End;
253
4.89k
      if (I + 1 == Sections.size())
254
4.01k
        End = Buf + Size;
255
888
      else
256
888
        End = Buf + Sections[I + 1]->OutSecOff;
257
4.89k
      fill(Start, End - Start, Filler);
258
4.89k
    }
259
345k
  });
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
245
1.18k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
1.18k
    InputSection *IS = Sections[I];
247
1.18k
    IS->writeTo<ELFT>(Buf);
248
1.18k
249
1.18k
    // Fill gaps between sections.
250
1.18k
    if (NonZeroFiller) {
251
175
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
175
      uint8_t *End;
253
175
      if (I + 1 == Sections.size())
254
135
        End = Buf + Size;
255
40
      else
256
40
        End = Buf + Sections[I + 1]->OutSecOff;
257
175
      fill(Start, End - Start, Filler);
258
175
    }
259
1.18k
  });
260
349k
261
349k
  // Linker scripts may have BYTE()-family commands with which you
262
349k
  // can write arbitrary bytes to the output. Process them if any.
263
349k
  for (BaseCommand *Base : SectionCommands)
264
349k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
265
43
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
266
349k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
222
2.80k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
223
2.80k
  if (Type == SHT_NOBITS)
224
56
    return;
225
2.74k
226
2.74k
  Loc = Buf;
227
2.74k
228
2.74k
  // If -compress-debug-section is specified and if this is a debug seciton,
229
2.74k
  // we've already compressed section contents. If that's the case,
230
2.74k
  // just write it down.
231
2.74k
  if (!CompressedData.empty()) {
232
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
233
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
234
0
           CompressedData.size());
235
0
    return;
236
0
  }
237
2.74k
238
2.74k
  // Write leading padding.
239
2.74k
  std::vector<InputSection *> Sections = getInputSections(this);
240
2.74k
  std::array<uint8_t, 4> Filler = getFiller();
241
2.74k
  bool NonZeroFiller = read32(Filler.data()) != 0;
242
2.74k
  if (NonZeroFiller)
243
423
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
244
2.74k
245
2.74k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
2.74k
    InputSection *IS = Sections[I];
247
2.74k
    IS->writeTo<ELFT>(Buf);
248
2.74k
249
2.74k
    // Fill gaps between sections.
250
2.74k
    if (NonZeroFiller) {
251
2.74k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
2.74k
      uint8_t *End;
253
2.74k
      if (I + 1 == Sections.size())
254
2.74k
        End = Buf + Size;
255
2.74k
      else
256
2.74k
        End = Buf + Sections[I + 1]->OutSecOff;
257
2.74k
      fill(Start, End - Start, Filler);
258
2.74k
    }
259
2.74k
  });
260
2.74k
261
2.74k
  // Linker scripts may have BYTE()-family commands with which you
262
2.74k
  // can write arbitrary bytes to the output. Process them if any.
263
2.74k
  for (BaseCommand *Base : SectionCommands)
264
2.76k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
265
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
266
2.74k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
222
1.57k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
223
1.57k
  if (Type == SHT_NOBITS)
224
115
    return;
225
1.46k
226
1.46k
  Loc = Buf;
227
1.46k
228
1.46k
  // If -compress-debug-section is specified and if this is a debug seciton,
229
1.46k
  // we've already compressed section contents. If that's the case,
230
1.46k
  // just write it down.
231
1.46k
  if (!CompressedData.empty()) {
232
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
233
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
234
0
           CompressedData.size());
235
0
    return;
236
0
  }
237
1.46k
238
1.46k
  // Write leading padding.
239
1.46k
  std::vector<InputSection *> Sections = getInputSections(this);
240
1.46k
  std::array<uint8_t, 4> Filler = getFiller();
241
1.46k
  bool NonZeroFiller = read32(Filler.data()) != 0;
242
1.46k
  if (NonZeroFiller)
243
133
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
244
1.46k
245
1.46k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
1.46k
    InputSection *IS = Sections[I];
247
1.46k
    IS->writeTo<ELFT>(Buf);
248
1.46k
249
1.46k
    // Fill gaps between sections.
250
1.46k
    if (NonZeroFiller) {
251
1.46k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
1.46k
      uint8_t *End;
253
1.46k
      if (I + 1 == Sections.size())
254
1.46k
        End = Buf + Size;
255
1.46k
      else
256
1.46k
        End = Buf + Sections[I + 1]->OutSecOff;
257
1.46k
      fill(Start, End - Start, Filler);
258
1.46k
    }
259
1.46k
  });
260
1.46k
261
1.46k
  // Linker scripts may have BYTE()-family commands with which you
262
1.46k
  // can write arbitrary bytes to the output. Process them if any.
263
1.46k
  for (BaseCommand *Base : SectionCommands)
264
1.46k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
265
0
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
266
1.46k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
222
344k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
223
344k
  if (Type == SHT_NOBITS)
224
254
    return;
225
343k
226
343k
  Loc = Buf;
227
343k
228
343k
  // If -compress-debug-section is specified and if this is a debug seciton,
229
343k
  // we've already compressed section contents. If that's the case,
230
343k
  // just write it down.
231
343k
  if (!CompressedData.empty()) {
232
8
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
233
8
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
234
8
           CompressedData.size());
235
8
    return;
236
8
  }
237
343k
238
343k
  // Write leading padding.
239
343k
  std::vector<InputSection *> Sections = getInputSections(this);
240
343k
  std::array<uint8_t, 4> Filler = getFiller();
241
343k
  bool NonZeroFiller = read32(Filler.data()) != 0;
242
343k
  if (NonZeroFiller)
243
4.01k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff4.01k
, Filler);
244
343k
245
343k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
343k
    InputSection *IS = Sections[I];
247
343k
    IS->writeTo<ELFT>(Buf);
248
343k
249
343k
    // Fill gaps between sections.
250
343k
    if (NonZeroFiller) {
251
343k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
343k
      uint8_t *End;
253
343k
      if (I + 1 == Sections.size())
254
343k
        End = Buf + Size;
255
343k
      else
256
343k
        End = Buf + Sections[I + 1]->OutSecOff;
257
343k
      fill(Start, End - Start, Filler);
258
343k
    }
259
343k
  });
260
343k
261
343k
  // Linker scripts may have BYTE()-family commands with which you
262
343k
  // can write arbitrary bytes to the output. Process them if any.
263
343k
  for (BaseCommand *Base : SectionCommands)
264
344k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
265
34
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
266
343k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
222
1.21k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
223
1.21k
  if (Type == SHT_NOBITS)
224
79
    return;
225
1.13k
226
1.13k
  Loc = Buf;
227
1.13k
228
1.13k
  // If -compress-debug-section is specified and if this is a debug seciton,
229
1.13k
  // we've already compressed section contents. If that's the case,
230
1.13k
  // just write it down.
231
1.13k
  if (!CompressedData.empty()) {
232
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
233
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
234
0
           CompressedData.size());
235
0
    return;
236
0
  }
237
1.13k
238
1.13k
  // Write leading padding.
239
1.13k
  std::vector<InputSection *> Sections = getInputSections(this);
240
1.13k
  std::array<uint8_t, 4> Filler = getFiller();
241
1.13k
  bool NonZeroFiller = read32(Filler.data()) != 0;
242
1.13k
  if (NonZeroFiller)
243
135
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
244
1.13k
245
1.13k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
246
1.13k
    InputSection *IS = Sections[I];
247
1.13k
    IS->writeTo<ELFT>(Buf);
248
1.13k
249
1.13k
    // Fill gaps between sections.
250
1.13k
    if (NonZeroFiller) {
251
1.13k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
252
1.13k
      uint8_t *End;
253
1.13k
      if (I + 1 == Sections.size())
254
1.13k
        End = Buf + Size;
255
1.13k
      else
256
1.13k
        End = Buf + Sections[I + 1]->OutSecOff;
257
1.13k
      fill(Start, End - Start, Filler);
258
1.13k
    }
259
1.13k
  });
260
1.13k
261
1.13k
  // Linker scripts may have BYTE()-family commands with which you
262
1.13k
  // can write arbitrary bytes to the output. Process them if any.
263
1.13k
  for (BaseCommand *Base : SectionCommands)
264
1.14k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
265
8
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
266
1.13k
}
267
268
template <class ELFT>
269
static void finalizeShtGroup(OutputSection *OS,
270
8
                             InputSection *Section) {
271
8
  assert(Config->Relocatable);
272
8
273
8
  // sh_link field for SHT_GROUP sections should contain the section index of
274
8
  // the symbol table.
275
8
  OS->Link = In.SymTab->getParent()->SectionIndex;
276
8
277
8
  // sh_info then contain index of an entry in symbol table section which
278
8
  // provides signature of the section group.
279
8
  ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
280
8
  ArrayRef<Symbol *> Symbols = Obj->getSymbols();
281
8
  OS->Info = In.SymTab->getSymbolIndex(Symbols[Section->Info]);
282
8
}
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::OutputSection*, lld::elf::InputSection*)
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::OutputSection*, lld::elf::InputSection*)
OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::OutputSection*, lld::elf::InputSection*)
Line
Count
Source
270
8
                             InputSection *Section) {
271
8
  assert(Config->Relocatable);
272
8
273
8
  // sh_link field for SHT_GROUP sections should contain the section index of
274
8
  // the symbol table.
275
8
  OS->Link = In.SymTab->getParent()->SectionIndex;
276
8
277
8
  // sh_info then contain index of an entry in symbol table section which
278
8
  // provides signature of the section group.
279
8
  ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
280
8
  ArrayRef<Symbol *> Symbols = Obj->getSymbols();
281
8
  OS->Info = In.SymTab->getSymbolIndex(Symbols[Section->Info]);
282
8
}
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::OutputSection*, lld::elf::InputSection*)
283
284
350k
template <class ELFT> void OutputSection::finalize() {
285
350k
  if (Type == SHT_NOBITS)
286
518
    for (BaseCommand *Base : SectionCommands)
287
519
      if (isa<ByteCommand>(Base))
288
8
        Type = SHT_PROGBITS;
289
350k
290
350k
  std::vector<InputSection *> V = getInputSections(this);
291
350k
  InputSection *First = V.empty() ? 
nullptr31
:
V[0]350k
;
292
350k
293
350k
  if (Flags & SHF_LINK_ORDER) {
294
40
    // We must preserve the link order dependency of sections with the
295
40
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
296
40
    // need to translate the InputSection sh_link to the OutputSection sh_link,
297
40
    // all InputSections in the OutputSection have the same dependency.
298
40
    if (auto *D = First->getLinkOrderDep())
299
40
      Link = D->getParent()->SectionIndex;
300
40
  }
301
350k
302
350k
  if (Type == SHT_GROUP) {
303
8
    finalizeShtGroup<ELFT>(this, First);
304
8
    return;
305
8
  }
306
350k
307
350k
  if (!Config->CopyRelocs || 
(66.2k
Type != SHT_RELA66.2k
&&
Type != SHT_REL66.2k
))
308
349k
    return;
309
74
310
74
  if (isa<SyntheticSection>(First))
311
3
    return;
312
71
313
71
  Link = In.SymTab->getParent()->SectionIndex;
314
71
  // sh_info for SHT_REL[A] sections should contain the section header index of
315
71
  // the section to which the relocation applies.
316
71
  InputSectionBase *S = First->getRelocatedSection();
317
71
  Info = S->getOutputSection()->SectionIndex;
318
71
  Flags |= SHF_INFO_LINK;
319
71
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
284
2.81k
template <class ELFT> void OutputSection::finalize() {
285
2.81k
  if (Type == SHT_NOBITS)
286
56
    for (BaseCommand *Base : SectionCommands)
287
56
      if (isa<ByteCommand>(Base))
288
0
        Type = SHT_PROGBITS;
289
2.81k
290
2.81k
  std::vector<InputSection *> V = getInputSections(this);
291
2.81k
  InputSection *First = V.empty() ? 
nullptr2
:
V[0]2.80k
;
292
2.81k
293
2.81k
  if (Flags & SHF_LINK_ORDER) {
294
31
    // We must preserve the link order dependency of sections with the
295
31
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
296
31
    // need to translate the InputSection sh_link to the OutputSection sh_link,
297
31
    // all InputSections in the OutputSection have the same dependency.
298
31
    if (auto *D = First->getLinkOrderDep())
299
31
      Link = D->getParent()->SectionIndex;
300
31
  }
301
2.81k
302
2.81k
  if (Type == SHT_GROUP) {
303
0
    finalizeShtGroup<ELFT>(this, First);
304
0
    return;
305
0
  }
306
2.81k
307
2.81k
  if (!Config->CopyRelocs || 
(98
Type != SHT_RELA98
&&
Type != SHT_REL98
))
308
2.79k
    return;
309
14
310
14
  if (isa<SyntheticSection>(First))
311
0
    return;
312
14
313
14
  Link = In.SymTab->getParent()->SectionIndex;
314
14
  // sh_info for SHT_REL[A] sections should contain the section header index of
315
14
  // the section to which the relocation applies.
316
14
  InputSectionBase *S = First->getRelocatedSection();
317
14
  Info = S->getOutputSection()->SectionIndex;
318
14
  Flags |= SHF_INFO_LINK;
319
14
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
284
1.57k
template <class ELFT> void OutputSection::finalize() {
285
1.57k
  if (Type == SHT_NOBITS)
286
115
    for (BaseCommand *Base : SectionCommands)
287
115
      if (isa<ByteCommand>(Base))
288
0
        Type = SHT_PROGBITS;
289
1.57k
290
1.57k
  std::vector<InputSection *> V = getInputSections(this);
291
1.57k
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
292
1.57k
293
1.57k
  if (Flags & SHF_LINK_ORDER) {
294
0
    // We must preserve the link order dependency of sections with the
295
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
296
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
297
0
    // all InputSections in the OutputSection have the same dependency.
298
0
    if (auto *D = First->getLinkOrderDep())
299
0
      Link = D->getParent()->SectionIndex;
300
0
  }
301
1.57k
302
1.57k
  if (Type == SHT_GROUP) {
303
0
    finalizeShtGroup<ELFT>(this, First);
304
0
    return;
305
0
  }
306
1.57k
307
1.57k
  if (!Config->CopyRelocs || 
(103
Type != SHT_RELA103
&&
Type != SHT_REL103
))
308
1.56k
    return;
309
8
310
8
  if (isa<SyntheticSection>(First))
311
0
    return;
312
8
313
8
  Link = In.SymTab->getParent()->SectionIndex;
314
8
  // sh_info for SHT_REL[A] sections should contain the section header index of
315
8
  // the section to which the relocation applies.
316
8
  InputSectionBase *S = First->getRelocatedSection();
317
8
  Info = S->getOutputSection()->SectionIndex;
318
8
  Flags |= SHF_INFO_LINK;
319
8
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
284
344k
template <class ELFT> void OutputSection::finalize() {
285
344k
  if (Type == SHT_NOBITS)
286
268
    for (BaseCommand *Base : SectionCommands)
287
269
      if (isa<ByteCommand>(Base))
288
8
        Type = SHT_PROGBITS;
289
344k
290
344k
  std::vector<InputSection *> V = getInputSections(this);
291
344k
  InputSection *First = V.empty() ? 
nullptr29
:
V[0]344k
;
292
344k
293
344k
  if (Flags & SHF_LINK_ORDER) {
294
9
    // We must preserve the link order dependency of sections with the
295
9
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
296
9
    // need to translate the InputSection sh_link to the OutputSection sh_link,
297
9
    // all InputSections in the OutputSection have the same dependency.
298
9
    if (auto *D = First->getLinkOrderDep())
299
9
      Link = D->getParent()->SectionIndex;
300
9
  }
301
344k
302
344k
  if (Type == SHT_GROUP) {
303
8
    finalizeShtGroup<ELFT>(this, First);
304
8
    return;
305
8
  }
306
344k
307
344k
  if (!Config->CopyRelocs || 
(66.0k
Type != SHT_RELA66.0k
&&
Type != SHT_REL66.0k
))
308
344k
    return;
309
51
310
51
  if (isa<SyntheticSection>(First))
311
3
    return;
312
48
313
48
  Link = In.SymTab->getParent()->SectionIndex;
314
48
  // sh_info for SHT_REL[A] sections should contain the section header index of
315
48
  // the section to which the relocation applies.
316
48
  InputSectionBase *S = First->getRelocatedSection();
317
48
  Info = S->getOutputSection()->SectionIndex;
318
48
  Flags |= SHF_INFO_LINK;
319
48
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
284
1.21k
template <class ELFT> void OutputSection::finalize() {
285
1.21k
  if (Type == SHT_NOBITS)
286
79
    for (BaseCommand *Base : SectionCommands)
287
79
      if (isa<ByteCommand>(Base))
288
0
        Type = SHT_PROGBITS;
289
1.21k
290
1.21k
  std::vector<InputSection *> V = getInputSections(this);
291
1.21k
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
292
1.21k
293
1.21k
  if (Flags & SHF_LINK_ORDER) {
294
0
    // We must preserve the link order dependency of sections with the
295
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
296
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
297
0
    // all InputSections in the OutputSection have the same dependency.
298
0
    if (auto *D = First->getLinkOrderDep())
299
0
      Link = D->getParent()->SectionIndex;
300
0
  }
301
1.21k
302
1.21k
  if (Type == SHT_GROUP) {
303
0
    finalizeShtGroup<ELFT>(this, First);
304
0
    return;
305
0
  }
306
1.21k
307
1.21k
  if (!Config->CopyRelocs || 
(21
Type != SHT_RELA21
&&
Type != SHT_REL20
))
308
1.20k
    return;
309
1
310
1
  if (isa<SyntheticSection>(First))
311
0
    return;
312
1
313
1
  Link = In.SymTab->getParent()->SectionIndex;
314
1
  // sh_info for SHT_REL[A] sections should contain the section header index of
315
1
  // the section to which the relocation applies.
316
1
  InputSectionBase *S = First->getRelocatedSection();
317
1
  Info = S->getOutputSection()->SectionIndex;
318
1
  Flags |= SHF_INFO_LINK;
319
1
}
320
321
// Returns true if S matches /Filename.?\.o$/.
322
76
static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
323
76
  if (!S.endswith(".o"))
324
60
    return false;
325
16
  S = S.drop_back(2);
326
16
  if (S.endswith(Filename))
327
12
    return true;
328
4
  return !S.empty() && S.drop_back().endswith(Filename);
329
4
}
330
331
48
static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
332
28
static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
333
334
// .ctors and .dtors are sorted by this priority from highest to lowest.
335
//
336
//  1. The section was contained in crtbegin (crtbegin contains
337
//     some sentinel value in its .ctors and .dtors so that the runtime
338
//     can find the beginning of the sections.)
339
//
340
//  2. The section has an optional priority value in the form of ".ctors.N"
341
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
342
//     they are compared as string rather than number.
343
//
344
//  3. The section is just ".ctors" or ".dtors".
345
//
346
//  4. The section was contained in crtend, which contains an end marker.
347
//
348
// In an ideal world, we don't need this function because .init_array and
349
// .ctors are duplicate features (and .init_array is newer.) However, there
350
// are too many real-world use cases of .ctors, so we had no choice to
351
// support that with this rather ad-hoc semantics.
352
24
static bool compCtors(const InputSection *A, const InputSection *B) {
353
24
  bool BeginA = isCrtbegin(A->File->getName());
354
24
  bool BeginB = isCrtbegin(B->File->getName());
355
24
  if (BeginA != BeginB)
356
10
    return BeginA;
357
14
  bool EndA = isCrtend(A->File->getName());
358
14
  bool EndB = isCrtend(B->File->getName());
359
14
  if (EndA != EndB)
360
2
    return EndB;
361
12
  StringRef X = A->Name;
362
12
  StringRef Y = B->Name;
363
12
  assert(X.startswith(".ctors") || X.startswith(".dtors"));
364
12
  assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
365
12
  X = X.substr(6);
366
12
  Y = Y.substr(6);
367
12
  return X < Y;
368
12
}
369
370
// Sorts input sections by the special rules for .ctors and .dtors.
371
// Unfortunately, the rules are different from the one for .{init,fini}_array.
372
// Read the comment above.
373
8
void OutputSection::sortCtorsDtors() {
374
8
  assert(SectionCommands.size() == 1);
375
8
  auto *ISD = cast<InputSectionDescription>(SectionCommands[0]);
376
8
  std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
377
8
}
378
379
// If an input string is in the form of "foo.N" where N is a number,
380
// return N. Otherwise, returns 65536, which is one greater than the
381
// lowest priority.
382
23
int elf::getPriority(StringRef S) {
383
23
  size_t Pos = S.rfind('.');
384
23
  if (Pos == StringRef::npos)
385
2
    return 65536;
386
21
  int V;
387
21
  if (!to_integer(S.substr(Pos + 1), V, 10))
388
11
    return 65536;
389
10
  return V;
390
10
}
391
392
1.35M
std::vector<InputSection *> elf::getInputSections(OutputSection *OS) {
393
1.35M
  std::vector<InputSection *> Ret;
394
1.35M
  for (BaseCommand *Base : OS->SectionCommands)
395
1.35M
    if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
396
1.35M
      Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end());
397
1.35M
  return Ret;
398
1.35M
}
399
400
// Sorts input sections by section name suffixes, so that .foo.N comes
401
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
402
// We want to keep the original order if the priorities are the same
403
// because the compiler keeps the original initialization order in a
404
// translation unit and we need to respect that.
405
// For more detail, read the section of the GCC's manual about init_priority.
406
9
void OutputSection::sortInitFini() {
407
9
  // Sort sections by priority.
408
17
  sort([](InputSectionBase *S) { return getPriority(S->Name); });
409
9
}
410
411
349k
std::array<uint8_t, 4> OutputSection::getFiller() {
412
349k
  if (Filler)
413
10
    return *Filler;
414
349k
  if (Flags & SHF_EXECINSTR)
415
4.85k
    return Target->TrapInstr;
416
344k
  return {0, 0, 0, 0};
417
344k
}
418
419
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
420
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
421
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
422
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
423
424
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
425
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
426
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
427
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
428
429
template void OutputSection::maybeCompress<ELF32LE>();
430
template void OutputSection::maybeCompress<ELF32BE>();
431
template void OutputSection::maybeCompress<ELF64LE>();
432
template void OutputSection::maybeCompress<ELF64BE>();
433
434
template void OutputSection::finalize<ELF32LE>();
435
template void OutputSection::finalize<ELF32BE>();
436
template void OutputSection::finalize<ELF64LE>();
437
template void OutputSection::finalize<ELF64BE>();