Coverage Report

Created: 2019-07-24 05:18

/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
81.4k
uint32_t OutputSection::getPhdrFlags() const {
45
81.4k
  uint32_t ret = 0;
46
81.4k
  if (config->emachine != EM_ARM || 
!(flags & SHF_ARM_PURECODE)817
)
47
81.3k
    ret |= PF_R;
48
81.4k
  if (flags & SHF_WRITE)
49
4.30k
    ret |= PF_W;
50
81.4k
  if (flags & SHF_EXECINSTR)
51
5.11k
    ret |= PF_X;
52
81.4k
  return ret;
53
81.4k
}
54
55
template <class ELFT>
56
352k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57
352k
  shdr->sh_entsize = entsize;
58
352k
  shdr->sh_addralign = alignment;
59
352k
  shdr->sh_type = type;
60
352k
  shdr->sh_offset = offset;
61
352k
  shdr->sh_flags = flags;
62
352k
  shdr->sh_info = info;
63
352k
  shdr->sh_link = link;
64
352k
  shdr->sh_addr = addr;
65
352k
  shdr->sh_size = size;
66
352k
  shdr->sh_name = shName;
67
352k
}
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
3.42k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57
3.42k
  shdr->sh_entsize = entsize;
58
3.42k
  shdr->sh_addralign = alignment;
59
3.42k
  shdr->sh_type = type;
60
3.42k
  shdr->sh_offset = offset;
61
3.42k
  shdr->sh_flags = flags;
62
3.42k
  shdr->sh_info = info;
63
3.42k
  shdr->sh_link = link;
64
3.42k
  shdr->sh_addr = addr;
65
3.42k
  shdr->sh_size = size;
66
3.42k
  shdr->sh_name = shName;
67
3.42k
}
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.88k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57
1.88k
  shdr->sh_entsize = entsize;
58
1.88k
  shdr->sh_addralign = alignment;
59
1.88k
  shdr->sh_type = type;
60
1.88k
  shdr->sh_offset = offset;
61
1.88k
  shdr->sh_flags = flags;
62
1.88k
  shdr->sh_info = info;
63
1.88k
  shdr->sh_link = link;
64
1.88k
  shdr->sh_addr = addr;
65
1.88k
  shdr->sh_size = size;
66
1.88k
  shdr->sh_name = shName;
67
1.88k
}
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
346k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57
346k
  shdr->sh_entsize = entsize;
58
346k
  shdr->sh_addralign = alignment;
59
346k
  shdr->sh_type = type;
60
346k
  shdr->sh_offset = offset;
61
346k
  shdr->sh_flags = flags;
62
346k
  shdr->sh_info = info;
63
346k
  shdr->sh_link = link;
64
346k
  shdr->sh_addr = addr;
65
346k
  shdr->sh_size = size;
66
346k
  shdr->sh_name = shName;
67
346k
}
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.15k
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57
1.15k
  shdr->sh_entsize = entsize;
58
1.15k
  shdr->sh_addralign = alignment;
59
1.15k
  shdr->sh_type = type;
60
1.15k
  shdr->sh_offset = offset;
61
1.15k
  shdr->sh_flags = flags;
62
1.15k
  shdr->sh_info = info;
63
1.15k
  shdr->sh_link = link;
64
1.15k
  shdr->sh_addr = addr;
65
1.15k
  shdr->sh_size = size;
66
1.15k
  shdr->sh_name = shName;
67
1.15k
}
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
386k
                  /*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
389k
void OutputSection::addSection(InputSection *isec) {
87
389k
  if (!hasInputSections) {
88
378k
    // If IS is the first section to be added to this section,
89
378k
    // initialize Partition, Type, Entsize and flags from IS.
90
378k
    hasInputSections = true;
91
378k
    partition = isec->partition;
92
378k
    type = isec->type;
93
378k
    entsize = isec->entsize;
94
378k
    flags = isec->flags;
95
378k
  } else {
96
10.8k
    // Otherwise, check if new type or flags are compatible with existing ones.
97
10.8k
    unsigned mask = SHF_TLS | SHF_LINK_ORDER;
98
10.8k
    if ((flags & mask) != (isec->flags & mask))
99
3
      error("incompatible section flags for " + name + "\n>>> " + toString(isec) +
100
3
            ": 0x" + utohexstr(isec->flags) + "\n>>> output section " + name +
101
3
            ": 0x" + utohexstr(flags));
102
10.8k
103
10.8k
    if (type != isec->type) {
104
11
      if (!canMergeToProgbits(type) || 
!canMergeToProgbits(isec->type)10
)
105
1
        error("section type mismatch for " + isec->name + "\n>>> " +
106
1
              toString(isec) + ": " +
107
1
              getELFSectionTypeName(config->emachine, isec->type) +
108
1
              "\n>>> output section " + name + ": " +
109
1
              getELFSectionTypeName(config->emachine, type));
110
11
      type = SHT_PROGBITS;
111
11
    }
112
10.8k
  }
113
389k
114
389k
  isec->parent = this;
115
389k
  uint64_t andMask =
116
389k
      config->emachine == EM_ARM ? 
(uint64_t)SHF_ARM_PURECODE3.92k
:
0385k
;
117
389k
  uint64_t orMask = ~andMask;
118
389k
  uint64_t andFlags = (flags & isec->flags) & andMask;
119
389k
  uint64_t orFlags = (flags | isec->flags) & orMask;
120
389k
  flags = andFlags | orFlags;
121
389k
122
389k
  alignment = std::max(alignment, isec->alignment);
123
389k
124
389k
  // If this section contains a table of fixed-size entries, sh_entsize
125
389k
  // holds the element size. If it contains elements of different size we
126
389k
  // set sh_entsize to 0.
127
389k
  if (entsize != isec->entsize)
128
14
    entsize = 0;
129
389k
130
389k
  if (!isec->assigned) {
131
388k
    isec->assigned = true;
132
388k
    if (sectionCommands.empty() ||
133
388k
        
!isa<InputSectionDescription>(sectionCommands.back())10.4k
)
134
377k
      sectionCommands.push_back(make<InputSectionDescription>(""));
135
388k
    auto *isd = cast<InputSectionDescription>(sectionCommands.back());
136
388k
    isd->sections.push_back(isec);
137
388k
  }
138
389k
}
139
140
static void sortByOrder(MutableArrayRef<InputSection *> in,
141
10
                        llvm::function_ref<int(InputSectionBase *s)> order) {
142
10
  std::vector<std::pair<int, InputSection *>> v;
143
10
  for (InputSection *s : in)
144
18
    v.push_back({order(s), s});
145
10
  llvm::stable_sort(v, less_first());
146
10
147
28
  for (size_t i = 0; i < v.size(); 
++i18
)
148
18
    in[i] = v[i].second;
149
10
}
150
151
7.01k
uint64_t elf::getHeaderSize() {
152
7.01k
  if (config->oFormatBinary)
153
6
    return 0;
154
7.01k
  return Out::elfHeader->size + Out::programHeaders->size;
155
7.01k
}
156
157
5.54M
bool OutputSection::classof(const BaseCommand *c) {
158
5.54M
  return c->kind == OutputSectionKind;
159
5.54M
}
160
161
10
void OutputSection::sort(llvm::function_ref<int(InputSectionBase *s)> order) {
162
10
  assert(isLive());
163
10
  for (BaseCommand *b : sectionCommands)
164
10
    if (auto *isd = dyn_cast<InputSectionDescription>(b))
165
10
      sortByOrder(isd->sections, order);
166
10
}
167
168
// Fill [Buf, Buf + Size) with Filler.
169
// This is used for linker script "=fillexp" command.
170
static void fill(uint8_t *buf, size_t size,
171
11.6k
                 const std::array<uint8_t, 4> &filler) {
172
11.6k
  size_t i = 0;
173
57.1M
  for (; i + 4 < size; 
i += 457.1M
)
174
57.1M
    memcpy(buf + i, filler.data(), 4);
175
11.6k
  memcpy(buf + i, filler.data(), size - i);
176
11.6k
}
177
178
// Compress section contents if this section contains debug info.
179
352k
template <class ELFT> void OutputSection::maybeCompress() {
180
352k
  using Elf_Chdr = typename ELFT::Chdr;
181
352k
182
352k
  // Compress only DWARF debug sections.
183
352k
  if (!config->compressDebugSections || 
(flags & SHF_ALLOC)38
||
184
352k
      
!name.startswith(".debug_")32
)
185
352k
    return;
186
8
187
8
  // Create a section header.
188
8
  zDebugHeader.resize(sizeof(Elf_Chdr));
189
8
  auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
190
8
  hdr->ch_type = ELFCOMPRESS_ZLIB;
191
8
  hdr->ch_size = size;
192
8
  hdr->ch_addralign = alignment;
193
8
194
8
  // Write section contents to a temporary buffer and compress it.
195
8
  std::vector<uint8_t> buf(size);
196
8
  writeTo<ELFT>(buf.data());
197
8
  if (Error e = zlib::compress(toStringRef(buf), compressedData))
198
0
    fatal("compress failed: " + llvm::toString(std::move(e)));
199
8
200
8
  // Update section headers.
201
8
  size = sizeof(Elf_Chdr) + compressedData.size();
202
8
  flags |= SHF_COMPRESSED;
203
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
179
3.43k
template <class ELFT> void OutputSection::maybeCompress() {
180
3.43k
  using Elf_Chdr = typename ELFT::Chdr;
181
3.43k
182
3.43k
  // Compress only DWARF debug sections.
183
3.43k
  if (!config->compressDebugSections || 
(flags & SHF_ALLOC)0
||
184
3.43k
      
!name.startswith(".debug_")0
)
185
3.43k
    return;
186
0
187
0
  // Create a section header.
188
0
  zDebugHeader.resize(sizeof(Elf_Chdr));
189
0
  auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
190
0
  hdr->ch_type = ELFCOMPRESS_ZLIB;
191
0
  hdr->ch_size = size;
192
0
  hdr->ch_addralign = alignment;
193
0
194
0
  // Write section contents to a temporary buffer and compress it.
195
0
  std::vector<uint8_t> buf(size);
196
0
  writeTo<ELFT>(buf.data());
197
0
  if (Error e = zlib::compress(toStringRef(buf), compressedData))
198
0
    fatal("compress failed: " + llvm::toString(std::move(e)));
199
0
200
0
  // Update section headers.
201
0
  size = sizeof(Elf_Chdr) + compressedData.size();
202
0
  flags |= SHF_COMPRESSED;
203
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
179
1.88k
template <class ELFT> void OutputSection::maybeCompress() {
180
1.88k
  using Elf_Chdr = typename ELFT::Chdr;
181
1.88k
182
1.88k
  // Compress only DWARF debug sections.
183
1.88k
  if (!config->compressDebugSections || 
(flags & SHF_ALLOC)0
||
184
1.88k
      
!name.startswith(".debug_")0
)
185
1.88k
    return;
186
0
187
0
  // Create a section header.
188
0
  zDebugHeader.resize(sizeof(Elf_Chdr));
189
0
  auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
190
0
  hdr->ch_type = ELFCOMPRESS_ZLIB;
191
0
  hdr->ch_size = size;
192
0
  hdr->ch_addralign = alignment;
193
0
194
0
  // Write section contents to a temporary buffer and compress it.
195
0
  std::vector<uint8_t> buf(size);
196
0
  writeTo<ELFT>(buf.data());
197
0
  if (Error e = zlib::compress(toStringRef(buf), compressedData))
198
0
    fatal("compress failed: " + llvm::toString(std::move(e)));
199
0
200
0
  // Update section headers.
201
0
  size = sizeof(Elf_Chdr) + compressedData.size();
202
0
  flags |= SHF_COMPRESSED;
203
0
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
179
346k
template <class ELFT> void OutputSection::maybeCompress() {
180
346k
  using Elf_Chdr = typename ELFT::Chdr;
181
346k
182
346k
  // Compress only DWARF debug sections.
183
346k
  if (!config->compressDebugSections || 
(flags & SHF_ALLOC)38
||
184
346k
      
!name.startswith(".debug_")32
)
185
346k
    return;
186
8
187
8
  // Create a section header.
188
8
  zDebugHeader.resize(sizeof(Elf_Chdr));
189
8
  auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
190
8
  hdr->ch_type = ELFCOMPRESS_ZLIB;
191
8
  hdr->ch_size = size;
192
8
  hdr->ch_addralign = alignment;
193
8
194
8
  // Write section contents to a temporary buffer and compress it.
195
8
  std::vector<uint8_t> buf(size);
196
8
  writeTo<ELFT>(buf.data());
197
8
  if (Error e = zlib::compress(toStringRef(buf), compressedData))
198
0
    fatal("compress failed: " + llvm::toString(std::move(e)));
199
8
200
8
  // Update section headers.
201
8
  size = sizeof(Elf_Chdr) + compressedData.size();
202
8
  flags |= SHF_COMPRESSED;
203
8
}
void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
179
1.15k
template <class ELFT> void OutputSection::maybeCompress() {
180
1.15k
  using Elf_Chdr = typename ELFT::Chdr;
181
1.15k
182
1.15k
  // Compress only DWARF debug sections.
183
1.15k
  if (!config->compressDebugSections || 
(flags & SHF_ALLOC)0
||
184
1.15k
      
!name.startswith(".debug_")0
)
185
1.15k
    return;
186
0
187
0
  // Create a section header.
188
0
  zDebugHeader.resize(sizeof(Elf_Chdr));
189
0
  auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
190
0
  hdr->ch_type = ELFCOMPRESS_ZLIB;
191
0
  hdr->ch_size = size;
192
0
  hdr->ch_addralign = alignment;
193
0
194
0
  // Write section contents to a temporary buffer and compress it.
195
0
  std::vector<uint8_t> buf(size);
196
0
  writeTo<ELFT>(buf.data());
197
0
  if (Error e = zlib::compress(toStringRef(buf), compressedData))
198
0
    fatal("compress failed: " + llvm::toString(std::move(e)));
199
0
200
0
  // Update section headers.
201
0
  size = sizeof(Elf_Chdr) + compressedData.size();
202
0
  flags |= SHF_COMPRESSED;
203
0
}
204
205
44
static void writeInt(uint8_t *buf, uint64_t data, uint64_t size) {
206
44
  if (size == 1)
207
15
    *buf = data;
208
29
  else if (size == 2)
209
7
    write16(buf, data);
210
22
  else if (size == 4)
211
8
    write32(buf, data);
212
14
  else if (size == 8)
213
14
    write64(buf, data);
214
14
  else
215
14
    
llvm_unreachable0
("unsupported Size argument");
216
44
}
217
218
352k
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
219
352k
  if (type == SHT_NOBITS)
220
568
    return;
221
352k
222
352k
  // If -compress-debug-section is specified and if this is a debug seciton,
223
352k
  // we've already compressed section contents. If that's the case,
224
352k
  // just write it down.
225
352k
  if (!compressedData.empty()) {
226
8
    memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
227
8
    memcpy(buf + zDebugHeader.size(), compressedData.data(),
228
8
           compressedData.size());
229
8
    return;
230
8
  }
231
352k
232
352k
  // Write leading padding.
233
352k
  std::vector<InputSection *> sections = getInputSections(this);
234
352k
  std::array<uint8_t, 4> filler = getFiller();
235
352k
  bool nonZeroFiller = read32(filler.data()) != 0;
236
352k
  if (nonZeroFiller)
237
4.97k
    fill(buf, sections.empty() ? 
size4
:
sections[0]->outSecOff4.97k
, filler);
238
352k
239
354k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
354k
    InputSection *isec = sections[i];
241
354k
    isec->writeTo<ELFT>(buf);
242
354k
243
354k
    // Fill gaps between sections.
244
354k
    if (nonZeroFiller) {
245
6.62k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
6.62k
      uint8_t *end;
247
6.62k
      if (i + 1 == sections.size())
248
4.97k
        end = buf + size;
249
1.65k
      else
250
1.65k
        end = buf + sections[i + 1]->outSecOff;
251
6.62k
      fill(start, end - start, filler);
252
6.62k
    }
253
354k
  });
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
239
3.89k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
3.89k
    InputSection *isec = sections[i];
241
3.89k
    isec->writeTo<ELFT>(buf);
242
3.89k
243
3.89k
    // Fill gaps between sections.
244
3.89k
    if (nonZeroFiller) {
245
965
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
965
      uint8_t *end;
247
965
      if (i + 1 == sections.size())
248
458
        end = buf + size;
249
507
      else
250
507
        end = buf + sections[i + 1]->outSecOff;
251
965
      fill(start, end - start, filler);
252
965
    }
253
3.89k
  });
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
239
1.91k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
1.91k
    InputSection *isec = sections[i];
241
1.91k
    isec->writeTo<ELFT>(buf);
242
1.91k
243
1.91k
    // Fill gaps between sections.
244
1.91k
    if (nonZeroFiller) {
245
293
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
293
      uint8_t *end;
247
293
      if (i + 1 == sections.size())
248
186
        end = buf + size;
249
107
      else
250
107
        end = buf + sections[i + 1]->outSecOff;
251
293
      fill(start, end - start, filler);
252
293
    }
253
1.91k
  });
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
239
347k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
347k
    InputSection *isec = sections[i];
241
347k
    isec->writeTo<ELFT>(buf);
242
347k
243
347k
    // Fill gaps between sections.
244
347k
    if (nonZeroFiller) {
245
5.19k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
5.19k
      uint8_t *end;
247
5.19k
      if (i + 1 == sections.size())
248
4.20k
        end = buf + size;
249
997
      else
250
997
        end = buf + sections[i + 1]->outSecOff;
251
5.19k
      fill(start, end - start, filler);
252
5.19k
    }
253
347k
  });
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
239
1.13k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
1.13k
    InputSection *isec = sections[i];
241
1.13k
    isec->writeTo<ELFT>(buf);
242
1.13k
243
1.13k
    // Fill gaps between sections.
244
1.13k
    if (nonZeroFiller) {
245
169
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
169
      uint8_t *end;
247
169
      if (i + 1 == sections.size())
248
128
        end = buf + size;
249
41
      else
250
41
        end = buf + sections[i + 1]->outSecOff;
251
169
      fill(start, end - start, filler);
252
169
    }
253
1.13k
  });
254
352k
255
352k
  // Linker scripts may have BYTE()-family commands with which you
256
352k
  // can write arbitrary bytes to the output. Process them if any.
257
352k
  for (BaseCommand *base : sectionCommands)
258
352k
    if (auto *data = dyn_cast<ByteCommand>(base))
259
44
      writeInt(buf + data->offset, data->expression().getValue(), data->size);
260
352k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
218
3.42k
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
219
3.42k
  if (type == SHT_NOBITS)
220
74
    return;
221
3.35k
222
3.35k
  // If -compress-debug-section is specified and if this is a debug seciton,
223
3.35k
  // we've already compressed section contents. If that's the case,
224
3.35k
  // just write it down.
225
3.35k
  if (!compressedData.empty()) {
226
0
    memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
227
0
    memcpy(buf + zDebugHeader.size(), compressedData.data(),
228
0
           compressedData.size());
229
0
    return;
230
0
  }
231
3.35k
232
3.35k
  // Write leading padding.
233
3.35k
  std::vector<InputSection *> sections = getInputSections(this);
234
3.35k
  std::array<uint8_t, 4> filler = getFiller();
235
3.35k
  bool nonZeroFiller = read32(filler.data()) != 0;
236
3.35k
  if (nonZeroFiller)
237
458
    fill(buf, sections.empty() ? 
size0
: sections[0]->outSecOff, filler);
238
3.35k
239
3.35k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
3.35k
    InputSection *isec = sections[i];
241
3.35k
    isec->writeTo<ELFT>(buf);
242
3.35k
243
3.35k
    // Fill gaps between sections.
244
3.35k
    if (nonZeroFiller) {
245
3.35k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
3.35k
      uint8_t *end;
247
3.35k
      if (i + 1 == sections.size())
248
3.35k
        end = buf + size;
249
3.35k
      else
250
3.35k
        end = buf + sections[i + 1]->outSecOff;
251
3.35k
      fill(start, end - start, filler);
252
3.35k
    }
253
3.35k
  });
254
3.35k
255
3.35k
  // Linker scripts may have BYTE()-family commands with which you
256
3.35k
  // can write arbitrary bytes to the output. Process them if any.
257
3.35k
  for (BaseCommand *base : sectionCommands)
258
3.37k
    if (auto *data = dyn_cast<ByteCommand>(base))
259
1
      writeInt(buf + data->offset, data->expression().getValue(), data->size);
260
3.35k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
218
1.88k
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
219
1.88k
  if (type == SHT_NOBITS)
220
129
    return;
221
1.75k
222
1.75k
  // If -compress-debug-section is specified and if this is a debug seciton,
223
1.75k
  // we've already compressed section contents. If that's the case,
224
1.75k
  // just write it down.
225
1.75k
  if (!compressedData.empty()) {
226
0
    memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
227
0
    memcpy(buf + zDebugHeader.size(), compressedData.data(),
228
0
           compressedData.size());
229
0
    return;
230
0
  }
231
1.75k
232
1.75k
  // Write leading padding.
233
1.75k
  std::vector<InputSection *> sections = getInputSections(this);
234
1.75k
  std::array<uint8_t, 4> filler = getFiller();
235
1.75k
  bool nonZeroFiller = read32(filler.data()) != 0;
236
1.75k
  if (nonZeroFiller)
237
186
    fill(buf, sections.empty() ? 
size0
: sections[0]->outSecOff, filler);
238
1.75k
239
1.75k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
1.75k
    InputSection *isec = sections[i];
241
1.75k
    isec->writeTo<ELFT>(buf);
242
1.75k
243
1.75k
    // Fill gaps between sections.
244
1.75k
    if (nonZeroFiller) {
245
1.75k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
1.75k
      uint8_t *end;
247
1.75k
      if (i + 1 == sections.size())
248
1.75k
        end = buf + size;
249
1.75k
      else
250
1.75k
        end = buf + sections[i + 1]->outSecOff;
251
1.75k
      fill(start, end - start, filler);
252
1.75k
    }
253
1.75k
  });
254
1.75k
255
1.75k
  // Linker scripts may have BYTE()-family commands with which you
256
1.75k
  // can write arbitrary bytes to the output. Process them if any.
257
1.75k
  for (BaseCommand *base : sectionCommands)
258
1.76k
    if (auto *data = dyn_cast<ByteCommand>(base))
259
0
      writeInt(buf + data->offset, data->expression().getValue(), data->size);
260
1.75k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
218
346k
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
219
346k
  if (type == SHT_NOBITS)
220
287
    return;
221
345k
222
345k
  // If -compress-debug-section is specified and if this is a debug seciton,
223
345k
  // we've already compressed section contents. If that's the case,
224
345k
  // just write it down.
225
345k
  if (!compressedData.empty()) {
226
8
    memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
227
8
    memcpy(buf + zDebugHeader.size(), compressedData.data(),
228
8
           compressedData.size());
229
8
    return;
230
8
  }
231
345k
232
345k
  // Write leading padding.
233
345k
  std::vector<InputSection *> sections = getInputSections(this);
234
345k
  std::array<uint8_t, 4> filler = getFiller();
235
345k
  bool nonZeroFiller = read32(filler.data()) != 0;
236
345k
  if (nonZeroFiller)
237
4.20k
    fill(buf, sections.empty() ? 
size4
:
sections[0]->outSecOff4.20k
, filler);
238
345k
239
345k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
345k
    InputSection *isec = sections[i];
241
345k
    isec->writeTo<ELFT>(buf);
242
345k
243
345k
    // Fill gaps between sections.
244
345k
    if (nonZeroFiller) {
245
345k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
345k
      uint8_t *end;
247
345k
      if (i + 1 == sections.size())
248
345k
        end = buf + size;
249
345k
      else
250
345k
        end = buf + sections[i + 1]->outSecOff;
251
345k
      fill(start, end - start, filler);
252
345k
    }
253
345k
  });
254
345k
255
345k
  // Linker scripts may have BYTE()-family commands with which you
256
345k
  // can write arbitrary bytes to the output. Process them if any.
257
345k
  for (BaseCommand *base : sectionCommands)
258
346k
    if (auto *data = dyn_cast<ByteCommand>(base))
259
35
      writeInt(buf + data->offset, data->expression().getValue(), data->size);
260
345k
}
void lld::elf::OutputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
218
1.15k
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
219
1.15k
  if (type == SHT_NOBITS)
220
78
    return;
221
1.07k
222
1.07k
  // If -compress-debug-section is specified and if this is a debug seciton,
223
1.07k
  // we've already compressed section contents. If that's the case,
224
1.07k
  // just write it down.
225
1.07k
  if (!compressedData.empty()) {
226
0
    memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
227
0
    memcpy(buf + zDebugHeader.size(), compressedData.data(),
228
0
           compressedData.size());
229
0
    return;
230
0
  }
231
1.07k
232
1.07k
  // Write leading padding.
233
1.07k
  std::vector<InputSection *> sections = getInputSections(this);
234
1.07k
  std::array<uint8_t, 4> filler = getFiller();
235
1.07k
  bool nonZeroFiller = read32(filler.data()) != 0;
236
1.07k
  if (nonZeroFiller)
237
128
    fill(buf, sections.empty() ? 
size0
: sections[0]->outSecOff, filler);
238
1.07k
239
1.07k
  parallelForEachN(0, sections.size(), [&](size_t i) {
240
1.07k
    InputSection *isec = sections[i];
241
1.07k
    isec->writeTo<ELFT>(buf);
242
1.07k
243
1.07k
    // Fill gaps between sections.
244
1.07k
    if (nonZeroFiller) {
245
1.07k
      uint8_t *start = buf + isec->outSecOff + isec->getSize();
246
1.07k
      uint8_t *end;
247
1.07k
      if (i + 1 == sections.size())
248
1.07k
        end = buf + size;
249
1.07k
      else
250
1.07k
        end = buf + sections[i + 1]->outSecOff;
251
1.07k
      fill(start, end - start, filler);
252
1.07k
    }
253
1.07k
  });
254
1.07k
255
1.07k
  // Linker scripts may have BYTE()-family commands with which you
256
1.07k
  // can write arbitrary bytes to the output. Process them if any.
257
1.07k
  for (BaseCommand *base : sectionCommands)
258
1.08k
    if (auto *data = dyn_cast<ByteCommand>(base))
259
8
      writeInt(buf + data->offset, data->expression().getValue(), data->size);
260
1.07k
}
261
262
static void finalizeShtGroup(OutputSection *os,
263
10
                             InputSection *section) {
264
10
  assert(config->relocatable);
265
10
266
10
  // sh_link field for SHT_GROUP sections should contain the section index of
267
10
  // the symbol table.
268
10
  os->link = in.symTab->getParent()->sectionIndex;
269
10
270
10
  // sh_info then contain index of an entry in symbol table section which
271
10
  // provides signature of the section group.
272
10
  ArrayRef<Symbol *> symbols = section->file->getSymbols();
273
10
  os->info = in.symTab->getSymbolIndex(symbols[section->info]);
274
10
}
275
276
352k
void OutputSection::finalize() {
277
352k
  std::vector<InputSection *> v = getInputSections(this);
278
352k
  InputSection *first = v.empty() ? 
nullptr37
:
v[0]352k
;
279
352k
280
352k
  if (flags & SHF_LINK_ORDER) {
281
44
    // We must preserve the link order dependency of sections with the
282
44
    // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
283
44
    // need to translate the InputSection sh_link to the OutputSection sh_link,
284
44
    // all InputSections in the OutputSection have the same dependency.
285
44
    if (auto *ex = dyn_cast<ARMExidxSyntheticSection>(first))
286
26
      link = ex->getLinkOrderDep()->getParent()->sectionIndex;
287
18
    else if (auto *d = first->getLinkOrderDep())
288
18
      link = d->getParent()->sectionIndex;
289
44
  }
290
352k
291
352k
  if (type == SHT_GROUP) {
292
10
    finalizeShtGroup(this, first);
293
10
    return;
294
10
  }
295
352k
296
352k
  if (!config->copyRelocs || 
(66.3k
type != SHT_RELA66.3k
&&
type != SHT_REL66.3k
))
297
352k
    return;
298
88
299
88
  if (isa<SyntheticSection>(first))
300
3
    return;
301
85
302
85
  link = in.symTab->getParent()->sectionIndex;
303
85
  // sh_info for SHT_REL[A] sections should contain the section header index of
304
85
  // the section to which the relocation applies.
305
85
  InputSectionBase *s = first->getRelocatedSection();
306
85
  info = s->getOutputSection()->sectionIndex;
307
85
  flags |= SHF_INFO_LINK;
308
85
}
309
310
// Returns true if S matches /Filename.?\.o$/.
311
76
static bool isCrtBeginEnd(StringRef s, StringRef filename) {
312
76
  if (!s.endswith(".o"))
313
60
    return false;
314
16
  s = s.drop_back(2);
315
16
  if (s.endswith(filename))
316
12
    return true;
317
4
  return !s.empty() && s.drop_back().endswith(filename);
318
4
}
319
320
48
static bool isCrtbegin(StringRef s) { return isCrtBeginEnd(s, "crtbegin"); }
321
28
static bool isCrtend(StringRef s) { return isCrtBeginEnd(s, "crtend"); }
322
323
// .ctors and .dtors are sorted by this priority from highest to lowest.
324
//
325
//  1. The section was contained in crtbegin (crtbegin contains
326
//     some sentinel value in its .ctors and .dtors so that the runtime
327
//     can find the beginning of the sections.)
328
//
329
//  2. The section has an optional priority value in the form of ".ctors.N"
330
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
331
//     they are compared as string rather than number.
332
//
333
//  3. The section is just ".ctors" or ".dtors".
334
//
335
//  4. The section was contained in crtend, which contains an end marker.
336
//
337
// In an ideal world, we don't need this function because .init_array and
338
// .ctors are duplicate features (and .init_array is newer.) However, there
339
// are too many real-world use cases of .ctors, so we had no choice to
340
// support that with this rather ad-hoc semantics.
341
24
static bool compCtors(const InputSection *a, const InputSection *b) {
342
24
  bool beginA = isCrtbegin(a->file->getName());
343
24
  bool beginB = isCrtbegin(b->file->getName());
344
24
  if (beginA != beginB)
345
10
    return beginA;
346
14
  bool endA = isCrtend(a->file->getName());
347
14
  bool endB = isCrtend(b->file->getName());
348
14
  if (endA != endB)
349
2
    return endB;
350
12
  StringRef x = a->name;
351
12
  StringRef y = b->name;
352
12
  assert(x.startswith(".ctors") || x.startswith(".dtors"));
353
12
  assert(y.startswith(".ctors") || y.startswith(".dtors"));
354
12
  x = x.substr(6);
355
12
  y = y.substr(6);
356
12
  return x < y;
357
12
}
358
359
// Sorts input sections by the special rules for .ctors and .dtors.
360
// Unfortunately, the rules are different from the one for .{init,fini}_array.
361
// Read the comment above.
362
8
void OutputSection::sortCtorsDtors() {
363
8
  assert(sectionCommands.size() == 1);
364
8
  auto *isd = cast<InputSectionDescription>(sectionCommands[0]);
365
8
  llvm::stable_sort(isd->sections, compCtors);
366
8
}
367
368
// If an input string is in the form of "foo.N" where N is a number,
369
// return N. Otherwise, returns 65536, which is one greater than the
370
// lowest priority.
371
24
int elf::getPriority(StringRef s) {
372
24
  size_t pos = s.rfind('.');
373
24
  if (pos == StringRef::npos)
374
2
    return 65536;
375
22
  int v;
376
22
  if (!to_integer(s.substr(pos + 1), v, 10))
377
12
    return 65536;
378
10
  return v;
379
10
}
380
381
1.36M
std::vector<InputSection *> elf::getInputSections(OutputSection *os) {
382
1.36M
  std::vector<InputSection *> ret;
383
1.36M
  for (BaseCommand *base : os->sectionCommands)
384
1.36M
    if (auto *isd = dyn_cast<InputSectionDescription>(base))
385
1.36M
      ret.insert(ret.end(), isd->sections.begin(), isd->sections.end());
386
1.36M
  return ret;
387
1.36M
}
388
389
// Sorts input sections by section name suffixes, so that .foo.N comes
390
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
391
// We want to keep the original order if the priorities are the same
392
// because the compiler keeps the original initialization order in a
393
// translation unit and we need to respect that.
394
// For more detail, read the section of the GCC's manual about init_priority.
395
10
void OutputSection::sortInitFini() {
396
10
  // Sort sections by priority.
397
18
  sort([](InputSectionBase *s) { return getPriority(s->name); });
398
10
}
399
400
352k
std::array<uint8_t, 4> OutputSection::getFiller() {
401
352k
  if (filler)
402
13
    return *filler;
403
352k
  if (flags & SHF_EXECINSTR)
404
5.23k
    return target->trapInstr;
405
346k
  return {0, 0, 0, 0};
406
346k
}
407
408
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
409
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
410
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
411
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
412
413
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
414
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
415
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
416
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
417
418
template void OutputSection::maybeCompress<ELF32LE>();
419
template void OutputSection::maybeCompress<ELF32BE>();
420
template void OutputSection::maybeCompress<ELF64LE>();
421
template void OutputSection::maybeCompress<ELF64BE>();