Coverage Report

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