Coverage Report

Created: 2018-10-20 12:32

/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::ELF;
29
30
using namespace lld;
31
using namespace lld::elf;
32
33
uint8_t Out::First;
34
PhdrEntry *Out::TlsPhdr;
35
OutputSection *Out::DebugInfo;
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.1k
uint32_t OutputSection::getPhdrFlags() const {
45
79.1k
  uint32_t Ret = 0;
46
79.1k
  if (Config->EMachine != EM_ARM || 
!(Flags & SHF_ARM_PURECODE)681
)
47
79.1k
    Ret |= PF_R;
48
79.1k
  if (Flags & SHF_WRITE)
49
3.48k
    Ret |= PF_W;
50
79.1k
  if (Flags & SHF_EXECINSTR)
51
4.64k
    Ret |= PF_X;
52
79.1k
  return Ret;
53
79.1k
}
54
55
template <class ELFT>
56
348k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
348k
  Shdr->sh_entsize = Entsize;
58
348k
  Shdr->sh_addralign = Alignment;
59
348k
  Shdr->sh_type = Type;
60
348k
  Shdr->sh_offset = Offset;
61
348k
  Shdr->sh_flags = Flags;
62
348k
  Shdr->sh_info = Info;
63
348k
  Shdr->sh_link = Link;
64
348k
  Shdr->sh_addr = Addr;
65
348k
  Shdr->sh_size = Size;
66
348k
  Shdr->sh_name = ShName;
67
348k
}
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.59k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
2.59k
  Shdr->sh_entsize = Entsize;
58
2.59k
  Shdr->sh_addralign = Alignment;
59
2.59k
  Shdr->sh_type = Type;
60
2.59k
  Shdr->sh_offset = Offset;
61
2.59k
  Shdr->sh_flags = Flags;
62
2.59k
  Shdr->sh_info = Info;
63
2.59k
  Shdr->sh_link = Link;
64
2.59k
  Shdr->sh_addr = Addr;
65
2.59k
  Shdr->sh_size = Size;
66
2.59k
  Shdr->sh_name = ShName;
67
2.59k
}
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.52k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
1.52k
  Shdr->sh_entsize = Entsize;
58
1.52k
  Shdr->sh_addralign = Alignment;
59
1.52k
  Shdr->sh_type = Type;
60
1.52k
  Shdr->sh_offset = Offset;
61
1.52k
  Shdr->sh_flags = Flags;
62
1.52k
  Shdr->sh_info = Info;
63
1.52k
  Shdr->sh_link = Link;
64
1.52k
  Shdr->sh_addr = Addr;
65
1.52k
  Shdr->sh_size = Size;
66
1.52k
  Shdr->sh_name = ShName;
67
1.52k
}
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
343k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
343k
  Shdr->sh_entsize = Entsize;
58
343k
  Shdr->sh_addralign = Alignment;
59
343k
  Shdr->sh_type = Type;
60
343k
  Shdr->sh_offset = Offset;
61
343k
  Shdr->sh_flags = Flags;
62
343k
  Shdr->sh_info = Info;
63
343k
  Shdr->sh_link = Link;
64
343k
  Shdr->sh_addr = Addr;
65
343k
  Shdr->sh_size = Size;
66
343k
  Shdr->sh_name = ShName;
67
343k
}
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.07k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
1.07k
  Shdr->sh_entsize = Entsize;
58
1.07k
  Shdr->sh_addralign = Alignment;
59
1.07k
  Shdr->sh_type = Type;
60
1.07k
  Shdr->sh_offset = Offset;
61
1.07k
  Shdr->sh_flags = Flags;
62
1.07k
  Shdr->sh_info = Info;
63
1.07k
  Shdr->sh_link = Link;
64
1.07k
  Shdr->sh_addr = Addr;
65
1.07k
  Shdr->sh_size = Size;
66
1.07k
  Shdr->sh_name = ShName;
67
1.07k
}
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
377k
                  /*Info*/ 0, /*Link*/ 0) {
73
377k
  Live = false;
74
377k
}
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
379k
void OutputSection::addSection(InputSection *IS) {
89
379k
  if (!Live) {
90
370k
    // If IS is the first section to be added to this section,
91
370k
    // initialize Type, Entsize and flags from IS.
92
370k
    Live = true;
93
370k
    Type = IS->Type;
94
370k
    Entsize = IS->Entsize;
95
370k
    Flags = IS->Flags;
96
370k
  } else {
97
9.34k
    // Otherwise, check if new type or flags are compatible with existing ones.
98
9.34k
    unsigned Mask = SHF_ALLOC | SHF_TLS | SHF_LINK_ORDER;
99
9.34k
    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.34k
104
9.34k
    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.34k
  }
114
379k
115
379k
  IS->Parent = this;
116
379k
  uint64_t AndMask =
117
379k
      Config->EMachine == EM_ARM ? 
(uint64_t)SHF_ARM_PURECODE3.36k
:
0376k
;
118
379k
  uint64_t OrMask = ~AndMask;
119
379k
  uint64_t AndFlags = (Flags & IS->Flags) & AndMask;
120
379k
  uint64_t OrFlags = (Flags | IS->Flags) & OrMask;
121
379k
  Flags = AndFlags | OrFlags;
122
379k
123
379k
  Alignment = std::max(Alignment, IS->Alignment);
124
379k
125
379k
  // If this section contains a table of fixed-size entries, sh_entsize
126
379k
  // holds the element size. If it contains elements of different size we
127
379k
  // set sh_entsize to 0.
128
379k
  if (Entsize != IS->Entsize)
129
13
    Entsize = 0;
130
379k
131
379k
  if (!IS->Assigned) {
132
378k
    IS->Assigned = true;
133
378k
    if (SectionCommands.empty() ||
134
378k
        
!isa<InputSectionDescription>(SectionCommands.back())8.93k
)
135
369k
      SectionCommands.push_back(make<InputSectionDescription>(""));
136
378k
    auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
137
378k
    ISD->Sections.push_back(IS);
138
378k
  }
139
379k
}
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.51k
uint64_t elf::getHeaderSize() {
156
4.51k
  if (Config->OFormatBinary)
157
3
    return 0;
158
4.51k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
159
4.51k
}
160
161
5.82M
bool OutputSection::classof(const BaseCommand *C) {
162
5.82M
  return C->Kind == OutputSectionKind;
163
5.82M
}
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
10.6k
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
175
10.6k
  size_t I = 0;
176
66.5M
  for (; I + 4 < Size; 
I += 466.4M
)
177
66.4M
    memcpy(Buf + I, &Filler, 4);
178
10.6k
  memcpy(Buf + I, &Filler, Size - I);
179
10.6k
}
180
181
// Compress section contents if this section contains debug info.
182
349k
template <class ELFT> void OutputSection::maybeCompress() {
183
349k
  typedef typename ELFT::Chdr Elf_Chdr;
184
349k
185
349k
  // Compress only DWARF debug sections.
186
349k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
187
349k
      
!Name.startswith(".debug_")32
)
188
349k
    return;
189
8
190
8
  // Create a section header.
191
8
  ZDebugHeader.resize(sizeof(Elf_Chdr));
192
8
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
193
8
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
194
8
  Hdr->ch_size = Size;
195
8
  Hdr->ch_addralign = Alignment;
196
8
197
8
  // Write section contents to a temporary buffer and compress it.
198
8
  std::vector<uint8_t> Buf(Size);
199
8
  writeTo<ELFT>(Buf.data());
200
8
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
201
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
202
8
203
8
  // Update section headers.
204
8
  Size = sizeof(Elf_Chdr) + CompressedData.size();
205
8
  Flags |= SHF_COMPRESSED;
206
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
182
2.60k
template <class ELFT> void OutputSection::maybeCompress() {
183
2.60k
  typedef typename ELFT::Chdr Elf_Chdr;
184
2.60k
185
2.60k
  // Compress only DWARF debug sections.
186
2.60k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
2.60k
      
!Name.startswith(".debug_")0
)
188
2.60k
    return;
189
0
190
0
  // Create a section header.
191
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
192
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
193
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
194
0
  Hdr->ch_size = Size;
195
0
  Hdr->ch_addralign = Alignment;
196
0
197
0
  // Write section contents to a temporary buffer and compress it.
198
0
  std::vector<uint8_t> Buf(Size);
199
0
  writeTo<ELFT>(Buf.data());
200
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
201
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
202
0
203
0
  // Update section headers.
204
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
205
0
  Flags |= SHF_COMPRESSED;
206
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
182
1.52k
template <class ELFT> void OutputSection::maybeCompress() {
183
1.52k
  typedef typename ELFT::Chdr Elf_Chdr;
184
1.52k
185
1.52k
  // Compress only DWARF debug sections.
186
1.52k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
1.52k
      
!Name.startswith(".debug_")0
)
188
1.52k
    return;
189
0
190
0
  // Create a section header.
191
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
192
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
193
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
194
0
  Hdr->ch_size = Size;
195
0
  Hdr->ch_addralign = Alignment;
196
0
197
0
  // Write section contents to a temporary buffer and compress it.
198
0
  std::vector<uint8_t> Buf(Size);
199
0
  writeTo<ELFT>(Buf.data());
200
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
201
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
202
0
203
0
  // Update section headers.
204
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
205
0
  Flags |= SHF_COMPRESSED;
206
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
182
343k
template <class ELFT> void OutputSection::maybeCompress() {
183
343k
  typedef typename ELFT::Chdr Elf_Chdr;
184
343k
185
343k
  // Compress only DWARF debug sections.
186
343k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
187
343k
      
!Name.startswith(".debug_")32
)
188
343k
    return;
189
8
190
8
  // Create a section header.
191
8
  ZDebugHeader.resize(sizeof(Elf_Chdr));
192
8
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
193
8
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
194
8
  Hdr->ch_size = Size;
195
8
  Hdr->ch_addralign = Alignment;
196
8
197
8
  // Write section contents to a temporary buffer and compress it.
198
8
  std::vector<uint8_t> Buf(Size);
199
8
  writeTo<ELFT>(Buf.data());
200
8
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
201
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
202
8
203
8
  // Update section headers.
204
8
  Size = sizeof(Elf_Chdr) + CompressedData.size();
205
8
  Flags |= SHF_COMPRESSED;
206
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
182
1.07k
template <class ELFT> void OutputSection::maybeCompress() {
183
1.07k
  typedef typename ELFT::Chdr Elf_Chdr;
184
1.07k
185
1.07k
  // Compress only DWARF debug sections.
186
1.07k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
1.07k
      
!Name.startswith(".debug_")0
)
188
1.07k
    return;
189
0
190
0
  // Create a section header.
191
0
  ZDebugHeader.resize(sizeof(Elf_Chdr));
192
0
  auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
193
0
  Hdr->ch_type = ELFCOMPRESS_ZLIB;
194
0
  Hdr->ch_size = Size;
195
0
  Hdr->ch_addralign = Alignment;
196
0
197
0
  // Write section contents to a temporary buffer and compress it.
198
0
  std::vector<uint8_t> Buf(Size);
199
0
  writeTo<ELFT>(Buf.data());
200
0
  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
201
0
    fatal("compress failed: " + llvm::toString(std::move(E)));
202
0
203
0
  // Update section headers.
204
0
  Size = sizeof(Elf_Chdr) + CompressedData.size();
205
0
  Flags |= SHF_COMPRESSED;
206
0
}
207
208
43
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
209
43
  if (Size == 1)
210
15
    *Buf = Data;
211
28
  else if (Size == 2)
212
7
    write16(Buf, Data);
213
21
  else if (Size == 4)
214
8
    write32(Buf, Data);
215
13
  else if (Size == 8)
216
13
    write64(Buf, Data);
217
13
  else
218
13
    
llvm_unreachable0
("unsupported Size argument");
219
43
}
220
221
348k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
348k
  if (Type == SHT_NOBITS)
223
430
    return;
224
348k
225
348k
  Loc = Buf;
226
348k
227
348k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
348k
  // we've already compressed section contents. If that's the case,
229
348k
  // just write it down.
230
348k
  if (!CompressedData.empty()) {
231
8
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
232
8
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
233
8
           CompressedData.size());
234
8
    return;
235
8
  }
236
348k
237
348k
  // Write leading padding.
238
348k
  std::vector<InputSection *> Sections = getInputSections(this);
239
348k
  uint32_t Filler = getFiller();
240
348k
  if (Filler)
241
4.59k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff4.59k
, Filler);
242
348k
243
350k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
350k
    InputSection *IS = Sections[I];
245
350k
    IS->writeTo<ELFT>(Buf);
246
350k
247
350k
    // Fill gaps between sections.
248
350k
    if (Filler) {
249
6.05k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
6.05k
      uint8_t *End;
251
6.05k
      if (I + 1 == Sections.size())
252
4.59k
        End = Buf + Size;
253
1.45k
      else
254
1.45k
        End = Buf + Sections[I + 1]->OutSecOff;
255
6.05k
      fill(Start, End - Start, Filler);
256
6.05k
    }
257
350k
  });
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
243
3.07k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
3.07k
    InputSection *IS = Sections[I];
245
3.07k
    IS->writeTo<ELFT>(Buf);
246
3.07k
247
3.07k
    // Fill gaps between sections.
248
3.07k
    if (Filler) {
249
849
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
849
      uint8_t *End;
251
849
      if (I + 1 == Sections.size())
252
389
        End = Buf + Size;
253
460
      else
254
460
        End = Buf + Sections[I + 1]->OutSecOff;
255
849
      fill(Start, End - Start, Filler);
256
849
    }
257
3.07k
  });
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
243
1.52k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
1.52k
    InputSection *IS = Sections[I];
245
1.52k
    IS->writeTo<ELFT>(Buf);
246
1.52k
247
1.52k
    // Fill gaps between sections.
248
1.52k
    if (Filler) {
249
200
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
200
      uint8_t *End;
251
200
      if (I + 1 == Sections.size())
252
128
        End = Buf + Size;
253
72
      else
254
72
        End = Buf + Sections[I + 1]->OutSecOff;
255
200
      fill(Start, End - Start, Filler);
256
200
    }
257
1.52k
  });
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
243
344k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
344k
    InputSection *IS = Sections[I];
245
344k
    IS->writeTo<ELFT>(Buf);
246
344k
247
344k
    // Fill gaps between sections.
248
344k
    if (Filler) {
249
4.83k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
4.83k
      uint8_t *End;
251
4.83k
      if (I + 1 == Sections.size())
252
3.95k
        End = Buf + Size;
253
883
      else
254
883
        End = Buf + Sections[I + 1]->OutSecOff;
255
4.83k
      fill(Start, End - Start, Filler);
256
4.83k
    }
257
344k
  });
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
243
1.07k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
1.07k
    InputSection *IS = Sections[I];
245
1.07k
    IS->writeTo<ELFT>(Buf);
246
1.07k
247
1.07k
    // Fill gaps between sections.
248
1.07k
    if (Filler) {
249
164
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
164
      uint8_t *End;
251
164
      if (I + 1 == Sections.size())
252
126
        End = Buf + Size;
253
38
      else
254
38
        End = Buf + Sections[I + 1]->OutSecOff;
255
164
      fill(Start, End - Start, Filler);
256
164
    }
257
1.07k
  });
258
348k
259
348k
  // Linker scripts may have BYTE()-family commands with which you
260
348k
  // can write arbitrary bytes to the output. Process them if any.
261
348k
  for (BaseCommand *Base : SectionCommands)
262
348k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
43
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
348k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
221
2.59k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
2.59k
  if (Type == SHT_NOBITS)
223
50
    return;
224
2.54k
225
2.54k
  Loc = Buf;
226
2.54k
227
2.54k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
2.54k
  // we've already compressed section contents. If that's the case,
229
2.54k
  // just write it down.
230
2.54k
  if (!CompressedData.empty()) {
231
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
232
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
233
0
           CompressedData.size());
234
0
    return;
235
0
  }
236
2.54k
237
2.54k
  // Write leading padding.
238
2.54k
  std::vector<InputSection *> Sections = getInputSections(this);
239
2.54k
  uint32_t Filler = getFiller();
240
2.54k
  if (Filler)
241
389
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
2.54k
243
2.54k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
2.54k
    InputSection *IS = Sections[I];
245
2.54k
    IS->writeTo<ELFT>(Buf);
246
2.54k
247
2.54k
    // Fill gaps between sections.
248
2.54k
    if (Filler) {
249
2.54k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
2.54k
      uint8_t *End;
251
2.54k
      if (I + 1 == Sections.size())
252
2.54k
        End = Buf + Size;
253
2.54k
      else
254
2.54k
        End = Buf + Sections[I + 1]->OutSecOff;
255
2.54k
      fill(Start, End - Start, Filler);
256
2.54k
    }
257
2.54k
  });
258
2.54k
259
2.54k
  // Linker scripts may have BYTE()-family commands with which you
260
2.54k
  // can write arbitrary bytes to the output. Process them if any.
261
2.54k
  for (BaseCommand *Base : SectionCommands)
262
2.56k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
2.54k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
221
1.52k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
1.52k
  if (Type == SHT_NOBITS)
223
110
    return;
224
1.41k
225
1.41k
  Loc = Buf;
226
1.41k
227
1.41k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
1.41k
  // we've already compressed section contents. If that's the case,
229
1.41k
  // just write it down.
230
1.41k
  if (!CompressedData.empty()) {
231
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
232
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
233
0
           CompressedData.size());
234
0
    return;
235
0
  }
236
1.41k
237
1.41k
  // Write leading padding.
238
1.41k
  std::vector<InputSection *> Sections = getInputSections(this);
239
1.41k
  uint32_t Filler = getFiller();
240
1.41k
  if (Filler)
241
128
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
1.41k
243
1.41k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
1.41k
    InputSection *IS = Sections[I];
245
1.41k
    IS->writeTo<ELFT>(Buf);
246
1.41k
247
1.41k
    // Fill gaps between sections.
248
1.41k
    if (Filler) {
249
1.41k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
1.41k
      uint8_t *End;
251
1.41k
      if (I + 1 == Sections.size())
252
1.41k
        End = Buf + Size;
253
1.41k
      else
254
1.41k
        End = Buf + Sections[I + 1]->OutSecOff;
255
1.41k
      fill(Start, End - Start, Filler);
256
1.41k
    }
257
1.41k
  });
258
1.41k
259
1.41k
  // Linker scripts may have BYTE()-family commands with which you
260
1.41k
  // can write arbitrary bytes to the output. Process them if any.
261
1.41k
  for (BaseCommand *Base : SectionCommands)
262
1.42k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
0
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
1.41k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
221
343k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
343k
  if (Type == SHT_NOBITS)
223
216
    return;
224
343k
225
343k
  Loc = Buf;
226
343k
227
343k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
343k
  // we've already compressed section contents. If that's the case,
229
343k
  // just write it down.
230
343k
  if (!CompressedData.empty()) {
231
8
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
232
8
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
233
8
           CompressedData.size());
234
8
    return;
235
8
  }
236
343k
237
343k
  // Write leading padding.
238
343k
  std::vector<InputSection *> Sections = getInputSections(this);
239
343k
  uint32_t Filler = getFiller();
240
343k
  if (Filler)
241
3.95k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff3.95k
, Filler);
242
343k
243
343k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
343k
    InputSection *IS = Sections[I];
245
343k
    IS->writeTo<ELFT>(Buf);
246
343k
247
343k
    // Fill gaps between sections.
248
343k
    if (Filler) {
249
343k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
343k
      uint8_t *End;
251
343k
      if (I + 1 == Sections.size())
252
343k
        End = Buf + Size;
253
343k
      else
254
343k
        End = Buf + Sections[I + 1]->OutSecOff;
255
343k
      fill(Start, End - Start, Filler);
256
343k
    }
257
343k
  });
258
343k
259
343k
  // Linker scripts may have BYTE()-family commands with which you
260
343k
  // can write arbitrary bytes to the output. Process them if any.
261
343k
  for (BaseCommand *Base : SectionCommands)
262
343k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
34
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
343k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
221
1.07k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
1.07k
  if (Type == SHT_NOBITS)
223
54
    return;
224
1.01k
225
1.01k
  Loc = Buf;
226
1.01k
227
1.01k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
1.01k
  // we've already compressed section contents. If that's the case,
229
1.01k
  // just write it down.
230
1.01k
  if (!CompressedData.empty()) {
231
0
    memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
232
0
    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
233
0
           CompressedData.size());
234
0
    return;
235
0
  }
236
1.01k
237
1.01k
  // Write leading padding.
238
1.01k
  std::vector<InputSection *> Sections = getInputSections(this);
239
1.01k
  uint32_t Filler = getFiller();
240
1.01k
  if (Filler)
241
126
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
1.01k
243
1.01k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
1.01k
    InputSection *IS = Sections[I];
245
1.01k
    IS->writeTo<ELFT>(Buf);
246
1.01k
247
1.01k
    // Fill gaps between sections.
248
1.01k
    if (Filler) {
249
1.01k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
1.01k
      uint8_t *End;
251
1.01k
      if (I + 1 == Sections.size())
252
1.01k
        End = Buf + Size;
253
1.01k
      else
254
1.01k
        End = Buf + Sections[I + 1]->OutSecOff;
255
1.01k
      fill(Start, End - Start, Filler);
256
1.01k
    }
257
1.01k
  });
258
1.01k
259
1.01k
  // Linker scripts may have BYTE()-family commands with which you
260
1.01k
  // can write arbitrary bytes to the output. Process them if any.
261
1.01k
  for (BaseCommand *Base : SectionCommands)
262
1.03k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
8
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
1.01k
}
265
266
template <class ELFT>
267
static void finalizeShtGroup(OutputSection *OS,
268
8
                             InputSection *Section) {
269
8
  assert(Config->Relocatable);
270
8
271
8
  // sh_link field for SHT_GROUP sections should contain the section index of
272
8
  // the symbol table.
273
8
  OS->Link = In.SymTab->getParent()->SectionIndex;
274
8
275
8
  // sh_info then contain index of an entry in symbol table section which
276
8
  // provides signature of the section group.
277
8
  ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
278
8
  ArrayRef<Symbol *> Symbols = Obj->getSymbols();
279
8
  OS->Info = In.SymTab->getSymbolIndex(Symbols[Section->Info]);
280
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
268
8
                             InputSection *Section) {
269
8
  assert(Config->Relocatable);
270
8
271
8
  // sh_link field for SHT_GROUP sections should contain the section index of
272
8
  // the symbol table.
273
8
  OS->Link = In.SymTab->getParent()->SectionIndex;
274
8
275
8
  // sh_info then contain index of an entry in symbol table section which
276
8
  // provides signature of the section group.
277
8
  ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
278
8
  ArrayRef<Symbol *> Symbols = Obj->getSymbols();
279
8
  OS->Info = In.SymTab->getSymbolIndex(Symbols[Section->Info]);
280
8
}
Unexecuted instantiation: OutputSections.cpp:void finalizeShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::OutputSection*, lld::elf::InputSection*)
281
282
349k
template <class ELFT> void OutputSection::finalize() {
283
349k
  if (Type == SHT_NOBITS)
284
444
    for (BaseCommand *Base : SectionCommands)
285
445
      if (isa<ByteCommand>(Base))
286
8
        Type = SHT_PROGBITS;
287
349k
288
349k
  std::vector<InputSection *> V = getInputSections(this);
289
349k
  InputSection *First = V.empty() ? 
nullptr31
:
V[0]349k
;
290
349k
291
349k
  if (Flags & SHF_LINK_ORDER) {
292
40
    // We must preserve the link order dependency of sections with the
293
40
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
40
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
40
    // all InputSections in the OutputSection have the same dependency.
296
40
    if (auto *D = First->getLinkOrderDep())
297
40
      Link = D->getParent()->SectionIndex;
298
40
  }
299
349k
300
349k
  if (Type == SHT_GROUP) {
301
8
    finalizeShtGroup<ELFT>(this, First);
302
8
    return;
303
8
  }
304
349k
305
349k
  if (!Config->CopyRelocs || 
(66.2k
Type != SHT_RELA66.2k
&&
Type != SHT_REL66.1k
))
306
349k
    return;
307
70
308
70
  if (isa<SyntheticSection>(First))
309
3
    return;
310
67
311
67
  Link = In.SymTab->getParent()->SectionIndex;
312
67
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
67
  // the section to which the relocation applies.
314
67
  InputSectionBase *S = First->getRelocatedSection();
315
67
  Info = S->getOutputSection()->SectionIndex;
316
67
  Flags |= SHF_INFO_LINK;
317
67
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
282
2.60k
template <class ELFT> void OutputSection::finalize() {
283
2.60k
  if (Type == SHT_NOBITS)
284
50
    for (BaseCommand *Base : SectionCommands)
285
50
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
2.60k
288
2.60k
  std::vector<InputSection *> V = getInputSections(this);
289
2.60k
  InputSection *First = V.empty() ? 
nullptr2
:
V[0]2.60k
;
290
2.60k
291
2.60k
  if (Flags & SHF_LINK_ORDER) {
292
31
    // We must preserve the link order dependency of sections with the
293
31
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
31
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
31
    // all InputSections in the OutputSection have the same dependency.
296
31
    if (auto *D = First->getLinkOrderDep())
297
31
      Link = D->getParent()->SectionIndex;
298
31
  }
299
2.60k
300
2.60k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
2.60k
305
2.60k
  if (!Config->CopyRelocs || 
(82
Type != SHT_RELA82
&&
Type != SHT_REL82
))
306
2.59k
    return;
307
12
308
12
  if (isa<SyntheticSection>(First))
309
0
    return;
310
12
311
12
  Link = In.SymTab->getParent()->SectionIndex;
312
12
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
12
  // the section to which the relocation applies.
314
12
  InputSectionBase *S = First->getRelocatedSection();
315
12
  Info = S->getOutputSection()->SectionIndex;
316
12
  Flags |= SHF_INFO_LINK;
317
12
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
282
1.52k
template <class ELFT> void OutputSection::finalize() {
283
1.52k
  if (Type == SHT_NOBITS)
284
110
    for (BaseCommand *Base : SectionCommands)
285
110
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
1.52k
288
1.52k
  std::vector<InputSection *> V = getInputSections(this);
289
1.52k
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
290
1.52k
291
1.52k
  if (Flags & SHF_LINK_ORDER) {
292
0
    // We must preserve the link order dependency of sections with the
293
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
0
    // all InputSections in the OutputSection have the same dependency.
296
0
    if (auto *D = First->getLinkOrderDep())
297
0
      Link = D->getParent()->SectionIndex;
298
0
  }
299
1.52k
300
1.52k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
1.52k
305
1.52k
  if (!Config->CopyRelocs || 
(93
Type != SHT_RELA93
&&
Type != SHT_REL93
))
306
1.51k
    return;
307
7
308
7
  if (isa<SyntheticSection>(First))
309
0
    return;
310
7
311
7
  Link = In.SymTab->getParent()->SectionIndex;
312
7
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
7
  // the section to which the relocation applies.
314
7
  InputSectionBase *S = First->getRelocatedSection();
315
7
  Info = S->getOutputSection()->SectionIndex;
316
7
  Flags |= SHF_INFO_LINK;
317
7
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
282
343k
template <class ELFT> void OutputSection::finalize() {
283
343k
  if (Type == SHT_NOBITS)
284
230
    for (BaseCommand *Base : SectionCommands)
285
231
      if (isa<ByteCommand>(Base))
286
8
        Type = SHT_PROGBITS;
287
343k
288
343k
  std::vector<InputSection *> V = getInputSections(this);
289
343k
  InputSection *First = V.empty() ? 
nullptr29
:
V[0]343k
;
290
343k
291
343k
  if (Flags & SHF_LINK_ORDER) {
292
9
    // We must preserve the link order dependency of sections with the
293
9
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
9
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
9
    // all InputSections in the OutputSection have the same dependency.
296
9
    if (auto *D = First->getLinkOrderDep())
297
9
      Link = D->getParent()->SectionIndex;
298
9
  }
299
343k
300
343k
  if (Type == SHT_GROUP) {
301
8
    finalizeShtGroup<ELFT>(this, First);
302
8
    return;
303
8
  }
304
343k
305
343k
  if (!Config->CopyRelocs || 
(66.0k
Type != SHT_RELA66.0k
&&
Type != SHT_REL66.0k
))
306
343k
    return;
307
50
308
50
  if (isa<SyntheticSection>(First))
309
3
    return;
310
47
311
47
  Link = In.SymTab->getParent()->SectionIndex;
312
47
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
47
  // the section to which the relocation applies.
314
47
  InputSectionBase *S = First->getRelocatedSection();
315
47
  Info = S->getOutputSection()->SectionIndex;
316
47
  Flags |= SHF_INFO_LINK;
317
47
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
282
1.07k
template <class ELFT> void OutputSection::finalize() {
283
1.07k
  if (Type == SHT_NOBITS)
284
54
    for (BaseCommand *Base : SectionCommands)
285
54
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
1.07k
288
1.07k
  std::vector<InputSection *> V = getInputSections(this);
289
1.07k
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
290
1.07k
291
1.07k
  if (Flags & SHF_LINK_ORDER) {
292
0
    // We must preserve the link order dependency of sections with the
293
0
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
0
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
0
    // all InputSections in the OutputSection have the same dependency.
296
0
    if (auto *D = First->getLinkOrderDep())
297
0
      Link = D->getParent()->SectionIndex;
298
0
  }
299
1.07k
300
1.07k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
1.07k
305
1.07k
  if (!Config->CopyRelocs || 
(21
Type != SHT_RELA21
&&
Type != SHT_REL20
))
306
1.07k
    return;
307
1
308
1
  if (isa<SyntheticSection>(First))
309
0
    return;
310
1
311
1
  Link = In.SymTab->getParent()->SectionIndex;
312
1
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
1
  // the section to which the relocation applies.
314
1
  InputSectionBase *S = First->getRelocatedSection();
315
1
  Info = S->getOutputSection()->SectionIndex;
316
1
  Flags |= SHF_INFO_LINK;
317
1
}
318
319
// Returns true if S matches /Filename.?\.o$/.
320
76
static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
321
76
  if (!S.endswith(".o"))
322
60
    return false;
323
16
  S = S.drop_back(2);
324
16
  if (S.endswith(Filename))
325
12
    return true;
326
4
  return !S.empty() && S.drop_back().endswith(Filename);
327
4
}
328
329
48
static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
330
28
static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
331
332
// .ctors and .dtors are sorted by this priority from highest to lowest.
333
//
334
//  1. The section was contained in crtbegin (crtbegin contains
335
//     some sentinel value in its .ctors and .dtors so that the runtime
336
//     can find the beginning of the sections.)
337
//
338
//  2. The section has an optional priority value in the form of ".ctors.N"
339
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
340
//     they are compared as string rather than number.
341
//
342
//  3. The section is just ".ctors" or ".dtors".
343
//
344
//  4. The section was contained in crtend, which contains an end marker.
345
//
346
// In an ideal world, we don't need this function because .init_array and
347
// .ctors are duplicate features (and .init_array is newer.) However, there
348
// are too many real-world use cases of .ctors, so we had no choice to
349
// support that with this rather ad-hoc semantics.
350
24
static bool compCtors(const InputSection *A, const InputSection *B) {
351
24
  bool BeginA = isCrtbegin(A->File->getName());
352
24
  bool BeginB = isCrtbegin(B->File->getName());
353
24
  if (BeginA != BeginB)
354
10
    return BeginA;
355
14
  bool EndA = isCrtend(A->File->getName());
356
14
  bool EndB = isCrtend(B->File->getName());
357
14
  if (EndA != EndB)
358
2
    return EndB;
359
12
  StringRef X = A->Name;
360
12
  StringRef Y = B->Name;
361
12
  assert(X.startswith(".ctors") || X.startswith(".dtors"));
362
12
  assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
363
12
  X = X.substr(6);
364
12
  Y = Y.substr(6);
365
12
  return X < Y;
366
12
}
367
368
// Sorts input sections by the special rules for .ctors and .dtors.
369
// Unfortunately, the rules are different from the one for .{init,fini}_array.
370
// Read the comment above.
371
8
void OutputSection::sortCtorsDtors() {
372
8
  assert(SectionCommands.size() == 1);
373
8
  auto *ISD = cast<InputSectionDescription>(SectionCommands[0]);
374
8
  std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
375
8
}
376
377
// If an input string is in the form of "foo.N" where N is a number,
378
// return N. Otherwise, returns 65536, which is one greater than the
379
// lowest priority.
380
23
int elf::getPriority(StringRef S) {
381
23
  size_t Pos = S.rfind('.');
382
23
  if (Pos == StringRef::npos)
383
2
    return 65536;
384
21
  int V;
385
21
  if (!to_integer(S.substr(Pos + 1), V, 10))
386
11
    return 65536;
387
10
  return V;
388
10
}
389
390
1.34M
std::vector<InputSection *> elf::getInputSections(OutputSection *OS) {
391
1.34M
  std::vector<InputSection *> Ret;
392
1.34M
  for (BaseCommand *Base : OS->SectionCommands)
393
1.34M
    if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
394
1.34M
      Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end());
395
1.34M
  return Ret;
396
1.34M
}
397
398
// Sorts input sections by section name suffixes, so that .foo.N comes
399
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
400
// We want to keep the original order if the priorities are the same
401
// because the compiler keeps the original initialization order in a
402
// translation unit and we need to respect that.
403
// For more detail, read the section of the GCC's manual about init_priority.
404
9
void OutputSection::sortInitFini() {
405
9
  // Sort sections by priority.
406
17
  sort([](InputSectionBase *S) { return getPriority(S->Name); });
407
9
}
408
409
348k
uint32_t OutputSection::getFiller() {
410
348k
  if (Filler)
411
10
    return *Filler;
412
348k
  if (Flags & SHF_EXECINSTR)
413
4.73k
    return Target->TrapInstr;
414
343k
  return 0;
415
343k
}
416
417
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
418
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
419
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
420
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
421
422
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
423
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
424
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
425
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
426
427
template void OutputSection::maybeCompress<ELF32LE>();
428
template void OutputSection::maybeCompress<ELF32BE>();
429
template void OutputSection::maybeCompress<ELF64LE>();
430
template void OutputSection::maybeCompress<ELF64BE>();
431
432
template void OutputSection::finalize<ELF32LE>();
433
template void OutputSection::finalize<ELF32BE>();
434
template void OutputSection::finalize<ELF64LE>();
435
template void OutputSection::finalize<ELF64BE>();