Coverage Report

Created: 2018-06-18 20:01

/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
76.4k
uint32_t OutputSection::getPhdrFlags() const {
45
76.4k
  uint32_t Ret = 0;
46
76.4k
  if (Config->EMachine != EM_ARM || 
!(Flags & SHF_ARM_PURECODE)637
)
47
76.4k
    Ret |= PF_R;
48
76.4k
  if (Flags & SHF_WRITE)
49
3.27k
    Ret |= PF_W;
50
76.4k
  if (Flags & SHF_EXECINSTR)
51
2.40k
    Ret |= PF_X;
52
76.4k
  return Ret;
53
76.4k
}
54
55
template <class ELFT>
56
214k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
214k
  Shdr->sh_entsize = Entsize;
58
214k
  Shdr->sh_addralign = Alignment;
59
214k
  Shdr->sh_type = Type;
60
214k
  Shdr->sh_offset = Offset;
61
214k
  Shdr->sh_flags = Flags;
62
214k
  Shdr->sh_info = Info;
63
214k
  Shdr->sh_link = Link;
64
214k
  Shdr->sh_addr = Addr;
65
214k
  Shdr->sh_size = Size;
66
214k
  Shdr->sh_name = ShName;
67
214k
}
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.39k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
2.39k
  Shdr->sh_entsize = Entsize;
58
2.39k
  Shdr->sh_addralign = Alignment;
59
2.39k
  Shdr->sh_type = Type;
60
2.39k
  Shdr->sh_offset = Offset;
61
2.39k
  Shdr->sh_flags = Flags;
62
2.39k
  Shdr->sh_info = Info;
63
2.39k
  Shdr->sh_link = Link;
64
2.39k
  Shdr->sh_addr = Addr;
65
2.39k
  Shdr->sh_size = Size;
66
2.39k
  Shdr->sh_name = ShName;
67
2.39k
}
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
209k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
209k
  Shdr->sh_entsize = Entsize;
58
209k
  Shdr->sh_addralign = Alignment;
59
209k
  Shdr->sh_type = Type;
60
209k
  Shdr->sh_offset = Offset;
61
209k
  Shdr->sh_flags = Flags;
62
209k
  Shdr->sh_info = Info;
63
209k
  Shdr->sh_link = Link;
64
209k
  Shdr->sh_addr = Addr;
65
209k
  Shdr->sh_size = Size;
66
209k
  Shdr->sh_name = ShName;
67
209k
}
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
904
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
57
904
  Shdr->sh_entsize = Entsize;
58
904
  Shdr->sh_addralign = Alignment;
59
904
  Shdr->sh_type = Type;
60
904
  Shdr->sh_offset = Offset;
61
904
  Shdr->sh_flags = Flags;
62
904
  Shdr->sh_info = Info;
63
904
  Shdr->sh_link = Link;
64
904
  Shdr->sh_addr = Addr;
65
904
  Shdr->sh_size = Size;
66
904
  Shdr->sh_name = ShName;
67
904
}
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
238k
                  /*Info*/ 0, /*Link*/ 0) {
73
238k
  Live = false;
74
238k
}
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
240k
void OutputSection::addSection(InputSection *IS) {
89
240k
  if (!Live) {
90
232k
    // If IS is the first section to be added to this section,
91
232k
    // initialize Type, Entsize and flags from IS.
92
232k
    Live = true;
93
232k
    Type = IS->Type;
94
232k
    Entsize = IS->Entsize;
95
232k
    Flags = IS->Flags;
96
232k
  } else {
97
8.58k
    // Otherwise, check if new type or flags are compatible with existing ones.
98
8.58k
    unsigned Mask = SHF_ALLOC | SHF_TLS | SHF_LINK_ORDER;
99
8.58k
    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
8.58k
104
8.58k
    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
8.58k
  }
114
240k
115
240k
  IS->Parent = this;
116
240k
  uint64_t AndMask =
117
240k
      Config->EMachine == EM_ARM ? 
(uint64_t)SHF_ARM_PURECODE2.91k
:
0237k
;
118
240k
  uint64_t OrMask = ~AndMask;
119
240k
  uint64_t AndFlags = (Flags & IS->Flags) & AndMask;
120
240k
  uint64_t OrFlags = (Flags | IS->Flags) & OrMask;
121
240k
  Flags = AndFlags | OrFlags;
122
240k
123
240k
  Alignment = std::max(Alignment, IS->Alignment);
124
240k
125
240k
  // If this section contains a table of fixed-size entries, sh_entsize
126
240k
  // holds the element size. If it contains elements of different size we
127
240k
  // set sh_entsize to 0.
128
240k
  if (Entsize != IS->Entsize)
129
13
    Entsize = 0;
130
240k
131
240k
  if (!IS->Assigned) {
132
239k
    IS->Assigned = true;
133
239k
    if (SectionCommands.empty() ||
134
239k
        
!isa<InputSectionDescription>(SectionCommands.back())8.17k
)
135
231k
      SectionCommands.push_back(make<InputSectionDescription>(""));
136
239k
    auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
137
239k
    ISD->Sections.push_back(IS);
138
239k
  }
139
240k
}
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.10k
uint64_t elf::getHeaderSize() {
156
4.10k
  if (Config->OFormatBinary)
157
3
    return 0;
158
4.09k
  return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
159
4.09k
}
160
161
3.79M
bool OutputSection::classof(const BaseCommand *C) {
162
3.79M
  return C->Kind == OutputSectionKind;
163
3.79M
}
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
6.06k
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
175
6.06k
  size_t I = 0;
176
64.7M
  for (; I + 4 < Size; 
I += 464.7M
)
177
64.7M
    memcpy(Buf + I, &Filler, 4);
178
6.06k
  memcpy(Buf + I, &Filler, Size - I);
179
6.06k
}
180
181
// Compress section contents if this section contains debug info.
182
214k
template <class ELFT> void OutputSection::maybeCompress() {
183
214k
  typedef typename ELFT::Chdr Elf_Chdr;
184
214k
185
214k
  // Compress only DWARF debug sections.
186
214k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
187
214k
      
!Name.startswith(".debug_")32
)
188
214k
    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.40k
template <class ELFT> void OutputSection::maybeCompress() {
183
2.40k
  typedef typename ELFT::Chdr Elf_Chdr;
184
2.40k
185
2.40k
  // Compress only DWARF debug sections.
186
2.40k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
2.40k
      
!Name.startswith(".debug_")0
)
188
2.40k
    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
209k
template <class ELFT> void OutputSection::maybeCompress() {
183
209k
  typedef typename ELFT::Chdr Elf_Chdr;
184
209k
185
209k
  // Compress only DWARF debug sections.
186
209k
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)38
||
187
209k
      
!Name.startswith(".debug_")32
)
188
209k
    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
904
template <class ELFT> void OutputSection::maybeCompress() {
183
904
  typedef typename ELFT::Chdr Elf_Chdr;
184
904
185
904
  // Compress only DWARF debug sections.
186
904
  if (!Config->CompressDebugSections || 
(Flags & SHF_ALLOC)0
||
187
904
      
!Name.startswith(".debug_")0
)
188
904
    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
214k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
214k
  if (Type == SHT_NOBITS)
223
405
    return;
224
213k
225
213k
  Loc = Buf;
226
213k
227
213k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
213k
  // we've already compressed section contents. If that's the case,
229
213k
  // just write it down.
230
213k
  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
213k
237
213k
  // Write leading padding.
238
213k
  std::vector<InputSection *> Sections = getInputSections(this);
239
213k
  uint32_t Filler = getFiller();
240
213k
  if (Filler)
241
2.36k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff2.36k
, Filler);
242
213k
243
215k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
215k
    InputSection *IS = Sections[I];
245
215k
    IS->writeTo<ELFT>(Buf);
246
215k
247
215k
    // Fill gaps between sections.
248
215k
    if (Filler) {
249
3.70k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
3.70k
      uint8_t *End;
251
3.70k
      if (I + 1 == Sections.size())
252
2.36k
        End = Buf + Size;
253
1.33k
      else
254
1.33k
        End = Buf + Sections[I + 1]->OutSecOff;
255
3.70k
      fill(Start, End - Start, Filler);
256
3.70k
    }
257
215k
  });
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.85k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
2.85k
    InputSection *IS = Sections[I];
245
2.85k
    IS->writeTo<ELFT>(Buf);
246
2.85k
247
2.85k
    // Fill gaps between sections.
248
2.85k
    if (Filler) {
249
814
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
814
      uint8_t *End;
251
814
      if (I + 1 == Sections.size())
252
370
        End = Buf + Size;
253
444
      else
254
444
        End = Buf + Sections[I + 1]->OutSecOff;
255
814
      fill(Start, End - Start, Filler);
256
814
    }
257
2.85k
  });
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
210k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
210k
    InputSection *IS = Sections[I];
245
210k
    IS->writeTo<ELFT>(Buf);
246
210k
247
210k
    // Fill gaps between sections.
248
210k
    if (Filler) {
249
2.56k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
2.56k
      uint8_t *End;
251
2.56k
      if (I + 1 == Sections.size())
252
1.76k
        End = Buf + Size;
253
794
      else
254
794
        End = Buf + Sections[I + 1]->OutSecOff;
255
2.56k
      fill(Start, End - Start, Filler);
256
2.56k
    }
257
210k
  });
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
891
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
891
    InputSection *IS = Sections[I];
245
891
    IS->writeTo<ELFT>(Buf);
246
891
247
891
    // Fill gaps between sections.
248
891
    if (Filler) {
249
125
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
125
      uint8_t *End;
251
125
      if (I + 1 == Sections.size())
252
101
        End = Buf + Size;
253
24
      else
254
24
        End = Buf + Sections[I + 1]->OutSecOff;
255
125
      fill(Start, End - Start, Filler);
256
125
    }
257
891
  });
258
213k
259
213k
  // Linker scripts may have BYTE()-family commands with which you
260
213k
  // can write arbitrary bytes to the output. Process them if any.
261
213k
  for (BaseCommand *Base : SectionCommands)
262
214k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
38
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
213k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
221
2.39k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
2.39k
  if (Type == SHT_NOBITS)
223
51
    return;
224
2.34k
225
2.34k
  Loc = Buf;
226
2.34k
227
2.34k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
2.34k
  // we've already compressed section contents. If that's the case,
229
2.34k
  // just write it down.
230
2.34k
  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.34k
237
2.34k
  // Write leading padding.
238
2.34k
  std::vector<InputSection *> Sections = getInputSections(this);
239
2.34k
  uint32_t Filler = getFiller();
240
2.34k
  if (Filler)
241
370
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
2.34k
243
2.34k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
2.34k
    InputSection *IS = Sections[I];
245
2.34k
    IS->writeTo<ELFT>(Buf);
246
2.34k
247
2.34k
    // Fill gaps between sections.
248
2.34k
    if (Filler) {
249
2.34k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
2.34k
      uint8_t *End;
251
2.34k
      if (I + 1 == Sections.size())
252
2.34k
        End = Buf + Size;
253
2.34k
      else
254
2.34k
        End = Buf + Sections[I + 1]->OutSecOff;
255
2.34k
      fill(Start, End - Start, Filler);
256
2.34k
    }
257
2.34k
  });
258
2.34k
259
2.34k
  // Linker scripts may have BYTE()-family commands with which you
260
2.34k
  // can write arbitrary bytes to the output. Process them if any.
261
2.34k
  for (BaseCommand *Base : SectionCommands)
262
2.36k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
1
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
2.34k
}
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
209k
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
209k
  if (Type == SHT_NOBITS)
223
194
    return;
224
209k
225
209k
  Loc = Buf;
226
209k
227
209k
  // If -compress-debug-section is specified and if this is a debug seciton,
228
209k
  // we've already compressed section contents. If that's the case,
229
209k
  // just write it down.
230
209k
  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
209k
237
209k
  // Write leading padding.
238
209k
  std::vector<InputSection *> Sections = getInputSections(this);
239
209k
  uint32_t Filler = getFiller();
240
209k
  if (Filler)
241
1.76k
    fill(Buf, Sections.empty() ? 
Size1
:
Sections[0]->OutSecOff1.76k
, Filler);
242
209k
243
209k
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
209k
    InputSection *IS = Sections[I];
245
209k
    IS->writeTo<ELFT>(Buf);
246
209k
247
209k
    // Fill gaps between sections.
248
209k
    if (Filler) {
249
209k
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
209k
      uint8_t *End;
251
209k
      if (I + 1 == Sections.size())
252
209k
        End = Buf + Size;
253
209k
      else
254
209k
        End = Buf + Sections[I + 1]->OutSecOff;
255
209k
      fill(Start, End - Start, Filler);
256
209k
    }
257
209k
  });
258
209k
259
209k
  // Linker scripts may have BYTE()-family commands with which you
260
209k
  // can write arbitrary bytes to the output. Process them if any.
261
209k
  for (BaseCommand *Base : SectionCommands)
262
209k
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
29
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
209k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
221
904
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
222
904
  if (Type == SHT_NOBITS)
223
50
    return;
224
854
225
854
  Loc = Buf;
226
854
227
854
  // If -compress-debug-section is specified and if this is a debug seciton,
228
854
  // we've already compressed section contents. If that's the case,
229
854
  // just write it down.
230
854
  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
854
237
854
  // Write leading padding.
238
854
  std::vector<InputSection *> Sections = getInputSections(this);
239
854
  uint32_t Filler = getFiller();
240
854
  if (Filler)
241
101
    fill(Buf, Sections.empty() ? 
Size0
: Sections[0]->OutSecOff, Filler);
242
854
243
854
  parallelForEachN(0, Sections.size(), [&](size_t I) {
244
854
    InputSection *IS = Sections[I];
245
854
    IS->writeTo<ELFT>(Buf);
246
854
247
854
    // Fill gaps between sections.
248
854
    if (Filler) {
249
854
      uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
250
854
      uint8_t *End;
251
854
      if (I + 1 == Sections.size())
252
854
        End = Buf + Size;
253
854
      else
254
854
        End = Buf + Sections[I + 1]->OutSecOff;
255
854
      fill(Start, End - Start, Filler);
256
854
    }
257
854
  });
258
854
259
854
  // Linker scripts may have BYTE()-family commands with which you
260
854
  // can write arbitrary bytes to the output. Process them if any.
261
854
  for (BaseCommand *Base : SectionCommands)
262
868
    if (auto *Data = dyn_cast<ByteCommand>(Base))
263
8
      writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
264
854
}
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
214k
template <class ELFT> void OutputSection::finalize() {
283
214k
  if (Type == SHT_NOBITS)
284
415
    for (BaseCommand *Base : SectionCommands)
285
416
      if (isa<ByteCommand>(Base))
286
4
        Type = SHT_PROGBITS;
287
214k
288
214k
  std::vector<InputSection *> V = getInputSections(this);
289
214k
  InputSection *First = V.empty() ? 
nullptr26
:
V[0]214k
;
290
214k
291
214k
  if (Flags & SHF_LINK_ORDER) {
292
39
    // We must preserve the link order dependency of sections with the
293
39
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
39
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
39
    // all InputSections in the OutputSection have the same dependency.
296
39
    if (auto *D = First->getLinkOrderDep())
297
39
      Link = D->getParent()->SectionIndex;
298
39
  }
299
214k
300
214k
  if (Type == SHT_GROUP) {
301
8
    finalizeShtGroup<ELFT>(this, First);
302
8
    return;
303
8
  }
304
214k
305
214k
  if (!Config->CopyRelocs || 
(622
Type != SHT_RELA622
&&
Type != SHT_REL572
))
306
214k
    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.40k
template <class ELFT> void OutputSection::finalize() {
283
2.40k
  if (Type == SHT_NOBITS)
284
51
    for (BaseCommand *Base : SectionCommands)
285
51
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
2.40k
288
2.40k
  std::vector<InputSection *> V = getInputSections(this);
289
2.40k
  InputSection *First = V.empty() ? 
nullptr3
:
V[0]2.40k
;
290
2.40k
291
2.40k
  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.40k
300
2.40k
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
2.40k
305
2.40k
  if (!Config->CopyRelocs || 
(69
Type != SHT_RELA69
&&
Type != SHT_REL69
))
306
2.39k
    return;
307
11
308
11
  if (isa<SyntheticSection>(First))
309
0
    return;
310
11
311
11
  Link = InX::SymTab->getParent()->SectionIndex;
312
11
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
11
  // the section to which the relocation applies.
314
11
  InputSectionBase *S = First->getRelocatedSection();
315
11
  Info = S->getOutputSection()->SectionIndex;
316
11
  Flags |= SHF_INFO_LINK;
317
11
}
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
209k
template <class ELFT> void OutputSection::finalize() {
283
209k
  if (Type == SHT_NOBITS)
284
204
    for (BaseCommand *Base : SectionCommands)
285
205
      if (isa<ByteCommand>(Base))
286
4
        Type = SHT_PROGBITS;
287
209k
288
209k
  std::vector<InputSection *> V = getInputSections(this);
289
209k
  InputSection *First = V.empty() ? 
nullptr23
:
V[0]209k
;
290
209k
291
209k
  if (Flags & SHF_LINK_ORDER) {
292
8
    // We must preserve the link order dependency of sections with the
293
8
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
294
8
    // need to translate the InputSection sh_link to the OutputSection sh_link,
295
8
    // all InputSections in the OutputSection have the same dependency.
296
8
    if (auto *D = First->getLinkOrderDep())
297
8
      Link = D->getParent()->SectionIndex;
298
8
  }
299
209k
300
209k
  if (Type == SHT_GROUP) {
301
8
    finalizeShtGroup<ELFT>(this, First);
302
8
    return;
303
8
  }
304
209k
305
209k
  if (!Config->CopyRelocs || 
(439
Type != SHT_RELA439
&&
Type != SHT_REL391
))
306
209k
    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
904
template <class ELFT> void OutputSection::finalize() {
283
904
  if (Type == SHT_NOBITS)
284
50
    for (BaseCommand *Base : SectionCommands)
285
50
      if (isa<ByteCommand>(Base))
286
0
        Type = SHT_PROGBITS;
287
904
288
904
  std::vector<InputSection *> V = getInputSections(this);
289
904
  InputSection *First = V.empty() ? 
nullptr0
: V[0];
290
904
291
904
  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
904
300
904
  if (Type == SHT_GROUP) {
301
0
    finalizeShtGroup<ELFT>(this, First);
302
0
    return;
303
0
  }
304
904
305
904
  if (!Config->CopyRelocs || 
(30
Type != SHT_RELA30
&&
Type != SHT_REL28
))
306
902
    return;
307
2
308
2
  if (isa<SyntheticSection>(First))
309
0
    return;
310
2
311
2
  Link = InX::SymTab->getParent()->SectionIndex;
312
2
  // sh_info for SHT_REL[A] sections should contain the section header index of
313
2
  // the section to which the relocation applies.
314
2
  InputSectionBase *S = First->getRelocatedSection();
315
2
  Info = S->getOutputSection()->SectionIndex;
316
2
  Flags |= SHF_INFO_LINK;
317
2
}
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
19
int elf::getPriority(StringRef S) {
381
19
  size_t Pos = S.rfind('.');
382
19
  if (Pos == StringRef::npos)
383
0
    return 65536;
384
19
  int V;
385
19
  if (!to_integer(S.substr(Pos + 1), V, 10))
386
11
    return 65536;
387
8
  return V;
388
8
}
389
390
872k
std::vector<InputSection *> elf::getInputSections(OutputSection *OS) {
391
872k
  std::vector<InputSection *> Ret;
392
872k
  for (BaseCommand *Base : OS->SectionCommands)
393
873k
    if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
394
873k
      Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end());
395
872k
  return Ret;
396
872k
}
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
213k
uint32_t OutputSection::getFiller() {
410
213k
  if (Filler)
411
10
    return *Filler;
412
213k
  if (Flags & SHF_EXECINSTR)
413
2.49k
    return Target->TrapInstr;
414
211k
  return 0;
415
211k
}
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>();