Coverage Report

Created: 2018-08-19 14:04

/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
78.8k
uint32_t OutputSection::getPhdrFlags() const {
45
78.8k
  uint32_t Ret = 0;
46
78.8k
  if (Config->EMachine != EM_ARM || 
!(Flags & SHF_ARM_PURECODE)670
)
47
78.8k
    Ret |= PF_R;
48
78.8k
  if (Flags & SHF_WRITE)
49
3.35k
    Ret |= PF_W;
50
78.8k
  if (Flags & SHF_EXECINSTR)
51
4.55k
    Ret |= PF_X;
52
78.8k
  return Ret;
53
78.8k
}
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.49k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
2.49k
  Shdr->sh_entsize = Entsize;
58
2.49k
  Shdr->sh_addralign = Alignment;
59
2.49k
  Shdr->sh_type = Type;
60
2.49k
  Shdr->sh_offset = Offset;
61
2.49k
  Shdr->sh_flags = Flags;
62
2.49k
  Shdr->sh_info = Info;
63
2.49k
  Shdr->sh_link = Link;
64
2.49k
  Shdr->sh_addr = Addr;
65
2.49k
  Shdr->sh_size = Size;
66
2.49k
  Shdr->sh_name = ShName;
67
2.49k
}
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.51k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
1.51k
  Shdr->sh_entsize = Entsize;
58
1.51k
  Shdr->sh_addralign = Alignment;
59
1.51k
  Shdr->sh_type = Type;
60
1.51k
  Shdr->sh_offset = Offset;
61
1.51k
  Shdr->sh_flags = Flags;
62
1.51k
  Shdr->sh_info = Info;
63
1.51k
  Shdr->sh_link = Link;
64
1.51k
  Shdr->sh_addr = Addr;
65
1.51k
  Shdr->sh_size = Size;
66
1.51k
  Shdr->sh_name = ShName;
67
1.51k
}
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
930
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
930
  Shdr->sh_entsize = Entsize;
58
930
  Shdr->sh_addralign = Alignment;
59
930
  Shdr->sh_type = Type;
60
930
  Shdr->sh_offset = Offset;
61
930
  Shdr->sh_flags = Flags;
62
930
  Shdr->sh_info = Info;
63
930
  Shdr->sh_link = Link;
64
930
  Shdr->sh_addr = Addr;
65
930
  Shdr->sh_size = Size;
66
930
  Shdr->sh_name = ShName;
67
930
}
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
375k
                  /*Info*/ 0, /*Link*/ 0) {
73
375k
  Live = false;
74
375k
}
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
377k
void OutputSection::addSection(InputSection *IS) {
89
377k
  if (!Live) {
90
368k
    // If IS is the first section to be added to this section,
91
368k
    // initialize Type, Entsize and flags from IS.
92
368k
    Live = true;
93
368k
    Type = IS->Type;
94
368k
    Entsize = IS->Entsize;
95
368k
    Flags = IS->Flags;
96
368k
  } else {
97
9.03k
    // Otherwise, check if new type or flags are compatible with existing ones.
98
9.03k
    unsigned Mask = SHF_ALLOC | SHF_TLS | SHF_LINK_ORDER;
99
9.03k
    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.03k
104
9.03k
    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.03k
  }
114
377k
115
377k
  IS->Parent = this;
116
377k
  uint64_t AndMask =
117
377k
      Config->EMachine == EM_ARM ? 
(uint64_t)SHF_ARM_PURECODE3.26k
:
0374k
;
118
377k
  uint64_t OrMask = ~AndMask;
119
377k
  uint64_t AndFlags = (Flags & IS->Flags) & AndMask;
120
377k
  uint64_t OrFlags = (Flags | IS->Flags) & OrMask;
121
377k
  Flags = AndFlags | OrFlags;
122
377k
123
377k
  Alignment = std::max(Alignment, IS->Alignment);
124
377k
125
377k
  // If this section contains a table of fixed-size entries, sh_entsize
126
377k
  // holds the element size. If it contains elements of different size we
127
377k
  // set sh_entsize to 0.
128
377k
  if (Entsize != IS->Entsize)
129
13
    Entsize = 0;
130
377k
131
377k
  if (!IS->Assigned) {
132
376k
    IS->Assigned = true;
133
376k
    if (SectionCommands.empty() ||
134
376k
        
!isa<InputSectionDescription>(SectionCommands.back())8.62k
)
135
368k
      SectionCommands.push_back(make<InputSectionDescription>(""));
136
376k
    auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
137
376k
    ISD->Sections.push_back(IS);
138
376k
  }
139
377k
}
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.28k
uint64_t elf::getHeaderSize() {
156
4.28k
  if (Config->OFormatBinary)
157
3
    return 0;
158
4.28k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
159
4.28k
}
160
161
5.80M
bool OutputSection::classof(const BaseCommand *C) {
162
5.80M
  return C->Kind == OutputSectionKind;
163
5.80M
}
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.4k
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
175
10.4k
  size_t I = 0;
176
65.8M
  for (; I + 4 < Size; 
I += 465.8M
)
177
65.8M
    memcpy(Buf + I, &Filler, 4);
178
10.4k
  memcpy(Buf + I, &Filler, Size - I);
179
10.4k
}
180
181
// Compress section contents if this section contains debug info.
182
348k
template <class ELFT> void OutputSection::maybeCompress() {
183
348k
  typedef typename ELFT::Chdr Elf_Chdr;
184
348k
185
348k
  // Compress only DWARF debug sections.
186
348k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
187
348k
      
!Name.startswith(".debug_")32
)
188
348k
    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.50k
template <class ELFT> void OutputSection::maybeCompress() {
183
2.50k
  typedef typename ELFT::Chdr Elf_Chdr;
184
2.50k
185
2.50k
  // Compress only DWARF debug sections.
186
2.50k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
2.50k
      
!Name.startswith(".debug_")0
)
188
2.50k
    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.51k
template <class ELFT> void OutputSection::maybeCompress() {
183
1.51k
  typedef typename ELFT::Chdr Elf_Chdr;
184
1.51k
185
1.51k
  // Compress only DWARF debug sections.
186
1.51k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
1.51k
      
!Name.startswith(".debug_")0
)
188
1.51k
    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
930
template <class ELFT> void OutputSection::maybeCompress() {
183
930
  typedef typename ELFT::Chdr Elf_Chdr;
184
930
185
930
  // Compress only DWARF debug sections.
186
930
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
930
      
!Name.startswith(".debug_")0
)
188
930
    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
38
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
209
38
  if (Size == 1)
210
15
    *Buf = Data;
211
23
  else if (Size == 2)
212
7
    write16(Buf, Data);
213
16
  else if (Size == 4)
214
8
    write32(Buf, Data);
215
8
  else if (Size == 8)
216
8
    write64(Buf, Data);
217
8
  else
218
8
    
llvm_unreachable0
("unsupported Size argument");
219
38
}
220
221
348k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
348k
  if (Type == SHT_NOBITS)
223
415
    return;
224
347k
225
347k
  Loc = Buf;
226
347k
227
347k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
347k
  // we've already compressed section contents. If that's the case,
229
347k
  // just write it down.
230
347k
  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
347k
237
347k
  // Write leading padding.
238
347k
  std::vector<InputSection *> Sections = getInputSections(this);
239
347k
  uint32_t Filler = getFiller();
240
347k
  if (Filler)
241
4.51k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff4.50k
, Filler);
242
347k
243
349k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
349k
    InputSection *IS = Sections[I];
245
349k
    IS->writeTo<ELFT>(Buf);
246
349k
247
349k
    // Fill gaps between sections.
248
349k
    if (Filler) {
249
5.92k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
5.92k
      uint8_t *End;
251
5.92k
      if (I + 1 == Sections.size())
252
4.50k
        End = Buf + Size;
253
1.41k
      else
254
1.41k
        End = Buf + Sections[I + 1]->OutSecOff;
255
5.92k
      fill(Start, End - Start, Filler);
256
5.92k
    }
257
349k
  });
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
2.96k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
2.96k
    InputSection *IS = Sections[I];
245
2.96k
    IS->writeTo<ELFT>(Buf);
246
2.96k
247
2.96k
    // Fill gaps between sections.
248
2.96k
    if (Filler) {
249
837
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
837
      uint8_t *End;
251
837
      if (I + 1 == Sections.size())
252
383
        End = Buf + Size;
253
454
      else
254
454
        End = Buf + Sections[I + 1]->OutSecOff;
255
837
      fill(Start, End - Start, Filler);
256
837
    }
257
2.96k
  });
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
201
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
201
      uint8_t *End;
251
201
      if (I + 1 == Sections.size())
252
128
        End = Buf + Size;
253
73
      else
254
73
        End = Buf + Sections[I + 1]->OutSecOff;
255
201
      fill(Start, End - Start, Filler);
256
201
    }
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.75k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
4.75k
      uint8_t *End;
251
4.75k
      if (I + 1 == Sections.size())
252
3.89k
        End = Buf + Size;
253
865
      else
254
865
        End = Buf + Sections[I + 1]->OutSecOff;
255
4.75k
      fill(Start, End - Start, Filler);
256
4.75k
    }
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
922
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
922
    InputSection *IS = Sections[I];
245
922
    IS->writeTo<ELFT>(Buf);
246
922
247
922
    // Fill gaps between sections.
248
922
    if (Filler) {
249
130
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
130
      uint8_t *End;
251
130
      if (I + 1 == Sections.size())
252
104
        End = Buf + Size;
253
26
      else
254
26
        End = Buf + Sections[I + 1]->OutSecOff;
255
130
      fill(Start, End - Start, Filler);
256
130
    }
257
922
  });
258
347k
259
347k
  // Linker scripts may have BYTE()-family commands with which you
260
347k
  // can write arbitrary bytes to the output. Process them if any.
261
347k
  for (BaseCommand *Base : SectionCommands)
262
347k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
38
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
347k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
221
2.49k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
2.49k
  if (Type == SHT_NOBITS)
223
50
    return;
224
2.44k
225
2.44k
  Loc = Buf;
226
2.44k
227
2.44k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
2.44k
  // we've already compressed section contents. If that's the case,
229
2.44k
  // just write it down.
230
2.44k
  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.44k
237
2.44k
  // Write leading padding.
238
2.44k
  std::vector<InputSection *> Sections = getInputSections(this);
239
2.44k
  uint32_t Filler = getFiller();
240
2.44k
  if (Filler)
241
383
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
2.44k
243
2.44k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
2.44k
    InputSection *IS = Sections[I];
245
2.44k
    IS->writeTo<ELFT>(Buf);
246
2.44k
247
2.44k
    // Fill gaps between sections.
248
2.44k
    if (Filler) {
249
2.44k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
2.44k
      uint8_t *End;
251
2.44k
      if (I + 1 == Sections.size())
252
2.44k
        End = Buf + Size;
253
2.44k
      else
254
2.44k
        End = Buf + Sections[I + 1]->OutSecOff;
255
2.44k
      fill(Start, End - Start, Filler);
256
2.44k
    }
257
2.44k
  });
258
2.44k
259
2.44k
  // Linker scripts may have BYTE()-family commands with which you
260
2.44k
  // can write arbitrary bytes to the output. Process them if any.
261
2.44k
  for (BaseCommand *Base : SectionCommands)
262
2.45k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
2.44k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
221
1.51k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
1.51k
  if (Type == SHT_NOBITS)
223
110
    return;
224
1.40k
225
1.40k
  Loc = Buf;
226
1.40k
227
1.40k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
1.40k
  // we've already compressed section contents. If that's the case,
229
1.40k
  // just write it down.
230
1.40k
  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.40k
237
1.40k
  // Write leading padding.
238
1.40k
  std::vector<InputSection *> Sections = getInputSections(this);
239
1.40k
  uint32_t Filler = getFiller();
240
1.40k
  if (Filler)
241
128
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
1.40k
243
1.40k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
1.40k
    InputSection *IS = Sections[I];
245
1.40k
    IS->writeTo<ELFT>(Buf);
246
1.40k
247
1.40k
    // Fill gaps between sections.
248
1.40k
    if (Filler) {
249
1.40k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
1.40k
      uint8_t *End;
251
1.40k
      if (I + 1 == Sections.size())
252
1.40k
        End = Buf + Size;
253
1.40k
      else
254
1.40k
        End = Buf + Sections[I + 1]->OutSecOff;
255
1.40k
      fill(Start, End - Start, Filler);
256
1.40k
    }
257
1.40k
  });
258
1.40k
259
1.40k
  // Linker scripts may have BYTE()-family commands with which you
260
1.40k
  // can write arbitrary bytes to the output. Process them if any.
261
1.40k
  for (BaseCommand *Base : SectionCommands)
262
1.41k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
0
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
1.40k
}
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
206
    return;
224
342k
225
342k
  Loc = Buf;
226
342k
227
342k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
342k
  // we've already compressed section contents. If that's the case,
229
342k
  // just write it down.
230
342k
  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
342k
237
342k
  // Write leading padding.
238
342k
  std::vector<InputSection *> Sections = getInputSections(this);
239
342k
  uint32_t Filler = getFiller();
240
342k
  if (Filler)
241
3.89k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff3.89k
, Filler);
242
342k
243
342k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
342k
    InputSection *IS = Sections[I];
245
342k
    IS->writeTo<ELFT>(Buf);
246
342k
247
342k
    // Fill gaps between sections.
248
342k
    if (Filler) {
249
342k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
342k
      uint8_t *End;
251
342k
      if (I + 1 == Sections.size())
252
342k
        End = Buf + Size;
253
342k
      else
254
342k
        End = Buf + Sections[I + 1]->OutSecOff;
255
342k
      fill(Start, End - Start, Filler);
256
342k
    }
257
342k
  });
258
342k
259
342k
  // Linker scripts may have BYTE()-family commands with which you
260
342k
  // can write arbitrary bytes to the output. Process them if any.
261
342k
  for (BaseCommand *Base : SectionCommands)
262
343k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
29
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
342k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
221
930
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
930
  if (Type == SHT_NOBITS)
223
49
    return;
224
881
225
881
  Loc = Buf;
226
881
227
881
  // If -compress-debug-section is specified and if this is a debug seciton,
228
881
  // we've already compressed section contents. If that's the case,
229
881
  // just write it down.
230
881
  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
881
237
881
  // Write leading padding.
238
881
  std::vector<InputSection *> Sections = getInputSections(this);
239
881
  uint32_t Filler = getFiller();
240
881
  if (Filler)
241
104
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
881
243
881
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
881
    InputSection *IS = Sections[I];
245
881
    IS->writeTo<ELFT>(Buf);
246
881
247
881
    // Fill gaps between sections.
248
881
    if (Filler) {
249
881
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
881
      uint8_t *End;
251
881
      if (I + 1 == Sections.size())
252
881
        End = Buf + Size;
253
881
      else
254
881
        End = Buf + Sections[I + 1]->OutSecOff;
255
881
      fill(Start, End - Start, Filler);
256
881
    }
257
881
  });
258
881
259
881
  // Linker scripts may have BYTE()-family commands with which you
260
881
  // can write arbitrary bytes to the output. Process them if any.
261
881
  for (BaseCommand *Base : SectionCommands)
262
895
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
8
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
881
}
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 = InX::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 = InX::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 = InX::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 = InX::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
348k
template <class ELFT> void OutputSection::finalize() {
283
348k
  if (Type == SHT_NOBITS)
284
425
    for (BaseCommand *Base : SectionCommands)
285
426
      if (isa<ByteCommand>(Base))
286
4
        Type = SHT_PROGBITS;
287
348k
288
348k
  std::vector<InputSection *> V = getInputSections(this);
289
348k
  InputSection *First = V.empty() ? 
nullptr27
:
V[0]348k
;
290
348k
291
348k
  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
348k
300
348k
  if (Type == SHT_GROUP) {
301
8
    finalizeShtGroup<ELFT>(this, First);
302
8
    return;
303
8
  }
304
348k
305
348k
  if (!Config->CopyRelocs || 
(66.1k
Type != SHT_RELA66.1k
&&
Type != SHT_REL66.1k
))
306
348k
    return;
307
68
308
68
  if (isa<SyntheticSection>(First))
309
3
    return;
310
65
311
65
  Link = InX::SymTab->getParent()->SectionIndex;
312
65
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
65
  // the section to which the relocation applies.
314
65
  InputSectionBase *S = First->getRelocatedSection();
315
65
  Info = S->getOutputSection()->SectionIndex;
316
65
  Flags |= SHF_INFO_LINK;
317
65
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
282
2.50k
template <class ELFT> void OutputSection::finalize() {
283
2.50k
  if (Type == SHT_NOBITS)
284
50
    for (BaseCommand *Base : SectionCommands)
285
50
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
2.50k
288
2.50k
  std::vector<InputSection *> V = getInputSections(this);
289
2.50k
  InputSection *First = V.empty() ? 
nullptr2
:
V[0]2.49k
;
290
2.50k
291
2.50k
  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.50k
300
2.50k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
2.50k
305
2.50k
  if (!Config->CopyRelocs || 
(75
Type != SHT_RELA75
&&
Type != SHT_REL75
))
306
2.48k
    return;
307
12
308
12
  if (isa<SyntheticSection>(First))
309
0
    return;
310
12
311
12
  Link = InX::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.51k
template <class ELFT> void OutputSection::finalize() {
283
1.51k
  if (Type == SHT_NOBITS)
284
110
    for (BaseCommand *Base : SectionCommands)
285
110
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
1.51k
288
1.51k
  std::vector<InputSection *> V = getInputSections(this);
289
1.51k
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
290
1.51k
291
1.51k
  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.51k
300
1.51k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
1.51k
305
1.51k
  if (!Config->CopyRelocs || 
(84
Type != SHT_RELA84
&&
Type != SHT_REL84
))
306
1.50k
    return;
307
7
308
7
  if (isa<SyntheticSection>(First))
309
0
    return;
310
7
311
7
  Link = InX::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
216
    for (BaseCommand *Base : SectionCommands)
285
217
      if (isa<ByteCommand>(Base))
286
4
        Type = SHT_PROGBITS;
287
343k
288
343k
  std::vector<InputSection *> V = getInputSections(this);
289
343k
  InputSection *First = V.empty() ? 
nullptr25
:
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 || 
(65.9k
Type != SHT_RELA65.9k
&&
Type != SHT_REL65.9k
))
306
343k
    return;
307
48
308
48
  if (isa<SyntheticSection>(First))
309
3
    return;
310
45
311
45
  Link = InX::SymTab->getParent()->SectionIndex;
312
45
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
45
  // the section to which the relocation applies.
314
45
  InputSectionBase *S = First->getRelocatedSection();
315
45
  Info = S->getOutputSection()->SectionIndex;
316
45
  Flags |= SHF_INFO_LINK;
317
45
}
void lld::elf::OutputSection::finalize<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
282
930
template <class ELFT> void OutputSection::finalize() {
283
930
  if (Type == SHT_NOBITS)
284
49
    for (BaseCommand *Base : SectionCommands)
285
49
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
930
288
930
  std::vector<InputSection *> V = getInputSections(this);
289
930
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
290
930
291
930
  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
930
300
930
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
930
305
930
  if (!Config->CopyRelocs || 
(19
Type != SHT_RELA19
&&
Type != SHT_REL18
))
306
929
    return;
307
1
308
1
  if (isa<SyntheticSection>(First))
309
0
    return;
310
1
311
1
  Link = InX::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
347k
uint32_t OutputSection::getFiller() {
410
347k
  if (Filler)
411
10
    return *Filler;
412
347k
  if (Flags & SHF_EXECINSTR)
413
4.63k
    return Target->TrapInstr;
414
342k
  return 0;
415
342k
}
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>();