Coverage Report

Created: 2019-05-19 14:56

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