Coverage Report

Created: 2019-05-22 02:55

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/InputSection.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputSection.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 "InputSection.h"
10
#include "Config.h"
11
#include "EhFrame.h"
12
#include "InputFiles.h"
13
#include "LinkerScript.h"
14
#include "OutputSections.h"
15
#include "Relocations.h"
16
#include "SymbolTable.h"
17
#include "Symbols.h"
18
#include "SyntheticSections.h"
19
#include "Target.h"
20
#include "Thunks.h"
21
#include "lld/Common/ErrorHandler.h"
22
#include "lld/Common/Memory.h"
23
#include "llvm/Support/Compiler.h"
24
#include "llvm/Support/Compression.h"
25
#include "llvm/Support/Endian.h"
26
#include "llvm/Support/Threading.h"
27
#include "llvm/Support/xxhash.h"
28
#include <algorithm>
29
#include <mutex>
30
#include <set>
31
#include <vector>
32
33
using namespace llvm;
34
using namespace llvm::ELF;
35
using namespace llvm::object;
36
using namespace llvm::support;
37
using namespace llvm::support::endian;
38
using namespace llvm::sys;
39
40
using namespace lld;
41
using namespace lld::elf;
42
43
std::vector<InputSectionBase *> elf::InputSections;
44
45
// Returns a string to construct an error message.
46
232
std::string lld::toString(const InputSectionBase *Sec) {
47
232
  return (toString(Sec->File) + ":(" + Sec->Name + ")").str();
48
232
}
49
50
template <class ELFT>
51
static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &File,
52
333k
                                            const typename ELFT::Shdr &Hdr) {
53
333k
  if (Hdr.sh_type == SHT_NOBITS)
54
230
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
55
332k
  return check(File.getObj().getSectionContents(&Hdr));
56
332k
}
InputSection.cpp:llvm::ArrayRef<unsigned char> getSectionContents<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const&)
Line
Count
Source
52
665
                                            const typename ELFT::Shdr &Hdr) {
53
665
  if (Hdr.sh_type == SHT_NOBITS)
54
28
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
55
637
  return check(File.getObj().getSectionContents(&Hdr));
56
637
}
InputSection.cpp:llvm::ArrayRef<unsigned char> getSectionContents<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const&)
Line
Count
Source
52
483
                                            const typename ELFT::Shdr &Hdr) {
53
483
  if (Hdr.sh_type == SHT_NOBITS)
54
88
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
55
395
  return check(File.getObj().getSectionContents(&Hdr));
56
395
}
InputSection.cpp:llvm::ArrayRef<unsigned char> getSectionContents<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const&)
Line
Count
Source
52
331k
                                            const typename ELFT::Shdr &Hdr) {
53
331k
  if (Hdr.sh_type == SHT_NOBITS)
54
86
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
55
331k
  return check(File.getObj().getSectionContents(&Hdr));
56
331k
}
InputSection.cpp:llvm::ArrayRef<unsigned char> getSectionContents<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const&)
Line
Count
Source
52
237
                                            const typename ELFT::Shdr &Hdr) {
53
237
  if (Hdr.sh_type == SHT_NOBITS)
54
28
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
55
209
  return check(File.getObj().getSectionContents(&Hdr));
56
209
}
57
58
InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags,
59
                                   uint32_t Type, uint64_t Entsize,
60
                                   uint32_t Link, uint32_t Info,
61
                                   uint32_t Alignment, ArrayRef<uint8_t> Data,
62
                                   StringRef Name, Kind SectionKind)
63
    : SectionBase(SectionKind, Name, Flags, Entsize, Alignment, Type, Info,
64
                  Link),
65
363k
      File(File), RawData(Data) {
66
363k
  // In order to reduce memory allocation, we assume that mergeable
67
363k
  // sections are smaller than 4 GiB, which is not an unreasonable
68
363k
  // assumption as of 2017.
69
363k
  if (SectionKind == SectionBase::Merge && 
RawData.size() > UINT32_MAX1.29k
)
70
363k
    
error(toString(this) + ": section too large")0
;
71
363k
72
363k
  NumRelocations = 0;
73
363k
  AreRelocsRela = false;
74
363k
75
363k
  // The ELF spec states that a value of 0 means the section has
76
363k
  // no alignment constraits.
77
363k
  uint32_t V = std::max<uint32_t>(Alignment, 1);
78
363k
  if (!isPowerOf2_64(V))
79
0
    fatal(toString(this) + ": sh_addralign is not a power of 2");
80
363k
  this->Alignment = V;
81
363k
82
363k
  // In ELF, each section can be compressed by zlib, and if compressed,
83
363k
  // section name may be mangled by appending "z" (e.g. ".zdebug_info").
84
363k
  // If that's the case, demangle section name so that we can handle a
85
363k
  // section as if it weren't compressed.
86
363k
  if ((Flags & SHF_COMPRESSED) || 
Name.startswith(".zdebug")363k
) {
87
7
    if (!zlib::isAvailable())
88
0
      error(toString(File) + ": contains a compressed section, " +
89
0
            "but zlib is not available");
90
7
    parseCompressedHeader();
91
7
  }
92
363k
}
93
94
// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
95
// SHF_GROUP is a marker that a section belongs to some comdat group.
96
// That flag doesn't make sense in an executable.
97
333k
static uint64_t getFlags(uint64_t Flags) {
98
333k
  Flags &= ~(uint64_t)SHF_INFO_LINK;
99
333k
  if (!Config->Relocatable)
100
267k
    Flags &= ~(uint64_t)SHF_GROUP;
101
333k
  return Flags;
102
333k
}
103
104
// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
105
// March 2017) fail to infer section types for sections starting with
106
// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of
107
// SHF_INIT_ARRAY. As a result, the following assembler directive
108
// creates ".init_array.100" with SHT_PROGBITS, for example.
109
//
110
//   .section .init_array.100, "aw"
111
//
112
// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
113
// incorrect inputs as if they were correct from the beginning.
114
333k
static uint64_t getType(uint64_t Type, StringRef Name) {
115
333k
  if (Type == SHT_PROGBITS && 
Name.startswith(".init_array.")332k
)
116
0
    return SHT_INIT_ARRAY;
117
333k
  if (Type == SHT_PROGBITS && 
Name.startswith(".fini_array.")332k
)
118
0
    return SHT_FINI_ARRAY;
119
333k
  return Type;
120
333k
}
121
122
template <class ELFT>
123
InputSectionBase::InputSectionBase(ObjFile<ELFT> &File,
124
                                   const typename ELFT::Shdr &Hdr,
125
                                   StringRef Name, Kind SectionKind)
126
    : InputSectionBase(&File, getFlags(Hdr.sh_flags),
127
                       getType(Hdr.sh_type, Name), Hdr.sh_entsize, Hdr.sh_link,
128
                       Hdr.sh_info, Hdr.sh_addralign,
129
333k
                       getSectionContents(File, Hdr), Name, SectionKind) {
130
333k
  // We reject object files having insanely large alignments even though
131
333k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
132
333k
  // We might want to relax this in the future.
133
333k
  if (Hdr.sh_addralign > UINT32_MAX)
134
333k
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
135
333k
}
lld::elf::InputSectionBase::InputSectionBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const&, llvm::StringRef, lld::elf::SectionBase::Kind)
Line
Count
Source
129
665
                       getSectionContents(File, Hdr), Name, SectionKind) {
130
665
  // We reject object files having insanely large alignments even though
131
665
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
132
665
  // We might want to relax this in the future.
133
665
  if (Hdr.sh_addralign > UINT32_MAX)
134
665
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
135
665
}
lld::elf::InputSectionBase::InputSectionBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const&, llvm::StringRef, lld::elf::SectionBase::Kind)
Line
Count
Source
129
483
                       getSectionContents(File, Hdr), Name, SectionKind) {
130
483
  // We reject object files having insanely large alignments even though
131
483
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
132
483
  // We might want to relax this in the future.
133
483
  if (Hdr.sh_addralign > UINT32_MAX)
134
483
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
135
483
}
lld::elf::InputSectionBase::InputSectionBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const&, llvm::StringRef, lld::elf::SectionBase::Kind)
Line
Count
Source
129
331k
                       getSectionContents(File, Hdr), Name, SectionKind) {
130
331k
  // We reject object files having insanely large alignments even though
131
331k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
132
331k
  // We might want to relax this in the future.
133
331k
  if (Hdr.sh_addralign > UINT32_MAX)
134
331k
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
135
331k
}
lld::elf::InputSectionBase::InputSectionBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const&, llvm::StringRef, lld::elf::SectionBase::Kind)
Line
Count
Source
129
237
                       getSectionContents(File, Hdr), Name, SectionKind) {
130
237
  // We reject object files having insanely large alignments even though
131
237
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
132
237
  // We might want to relax this in the future.
133
237
  if (Hdr.sh_addralign > UINT32_MAX)
134
237
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
135
237
}
136
137
5.57M
size_t InputSectionBase::getSize() const {
138
5.57M
  if (auto *S = dyn_cast<SyntheticSection>(this))
139
37.8k
    return S->getSize();
140
5.53M
  if (UncompressedSize >= 0)
141
8
    return UncompressedSize;
142
5.53M
  return RawData.size();
143
5.53M
}
144
145
4
void InputSectionBase::uncompress() const {
146
4
  size_t Size = UncompressedSize;
147
4
  char *UncompressedBuf;
148
4
  {
149
4
    static std::mutex Mu;
150
4
    std::lock_guard<std::mutex> Lock(Mu);
151
4
    UncompressedBuf = BAlloc.Allocate<char>(Size);
152
4
  }
153
4
154
4
  if (Error E = zlib::uncompress(toStringRef(RawData), UncompressedBuf, Size))
155
0
    fatal(toString(this) +
156
0
          ": uncompress failed: " + llvm::toString(std::move(E)));
157
4
  RawData = makeArrayRef((uint8_t *)UncompressedBuf, Size);
158
4
  UncompressedSize = -1;
159
4
}
160
161
71
uint64_t InputSectionBase::getOffsetInFile() const {
162
71
  const uint8_t *FileStart = (const uint8_t *)File->MB.getBufferStart();
163
71
  const uint8_t *SecStart = data().begin();
164
71
  return SecStart - FileStart;
165
71
}
166
167
132k
uint64_t SectionBase::getOffset(uint64_t Offset) const {
168
132k
  switch (kind()) {
169
132k
  case Output: {
170
1.14k
    auto *OS = cast<OutputSection>(this);
171
1.14k
    // For output sections we treat offset -1 as the end of the section.
172
1.14k
    return Offset == uint64_t(-1) ? 
OS->Size42
:
Offset1.10k
;
173
132k
  }
174
132k
  case Regular:
175
131k
  case Synthetic:
176
131k
    return cast<InputSection>(this)->getOffset(Offset);
177
131k
  case EHFrame:
178
4
    // The file crtbeginT.o has relocations pointing to the start of an empty
179
4
    // .eh_frame that is known to be the first in the link. It does that to
180
4
    // identify the start of the output .eh_frame.
181
4
    return Offset;
182
131k
  case Merge:
183
52
    const MergeInputSection *MS = cast<MergeInputSection>(this);
184
52
    if (InputSection *IS = MS->getParent())
185
51
      return IS->getOffset(MS->getParentOffset(Offset));
186
1
    return MS->getParentOffset(Offset);
187
0
  }
188
0
  llvm_unreachable("invalid section kind");
189
0
}
190
191
131k
uint64_t SectionBase::getVA(uint64_t Offset) const {
192
131k
  const OutputSection *Out = getOutputSection();
193
131k
  return (Out ? 
Out->Addr131k
:
012
) + getOffset(Offset);
194
131k
}
195
196
291k
OutputSection *SectionBase::getOutputSection() {
197
291k
  InputSection *Sec;
198
291k
  if (auto *IS = dyn_cast<InputSection>(this))
199
289k
    Sec = IS;
200
1.75k
  else if (auto *MS = dyn_cast<MergeInputSection>(this))
201
68
    Sec = MS->getParent();
202
1.68k
  else if (auto *EH = dyn_cast<EhInputSection>(this))
203
77
    Sec = EH->getParent();
204
1.60k
  else
205
1.60k
    return cast<OutputSection>(this);
206
289k
  return Sec ? 
Sec->getParent()289k
:
nullptr1
;
207
289k
}
208
209
// When a section is compressed, `RawData` consists with a header followed
210
// by zlib-compressed data. This function parses a header to initialize
211
// `UncompressedSize` member and remove the header from `RawData`.
212
7
void InputSectionBase::parseCompressedHeader() {
213
7
  using Chdr64 = typename ELF64LE::Chdr;
214
7
  using Chdr32 = typename ELF32LE::Chdr;
215
7
216
7
  // Old-style header
217
7
  if (Name.startswith(".zdebug")) {
218
5
    if (!toStringRef(RawData).startswith("ZLIB")) {
219
0
      error(toString(this) + ": corrupted compressed section header");
220
0
      return;
221
0
    }
222
5
    RawData = RawData.slice(4);
223
5
224
5
    if (RawData.size() < 8) {
225
0
      error(toString(this) + ": corrupted compressed section header");
226
0
      return;
227
0
    }
228
5
229
5
    UncompressedSize = read64be(RawData.data());
230
5
    RawData = RawData.slice(8);
231
5
232
5
    // Restore the original section name.
233
5
    // (e.g. ".zdebug_info" -> ".debug_info")
234
5
    Name = Saver.save("." + Name.substr(2));
235
5
    return;
236
5
  }
237
2
238
2
  assert(Flags & SHF_COMPRESSED);
239
2
  Flags &= ~(uint64_t)SHF_COMPRESSED;
240
2
241
2
  // New-style 64-bit header
242
2
  if (Config->Is64) {
243
0
    if (RawData.size() < sizeof(Chdr64)) {
244
0
      error(toString(this) + ": corrupted compressed section");
245
0
      return;
246
0
    }
247
0
248
0
    auto *Hdr = reinterpret_cast<const Chdr64 *>(RawData.data());
249
0
    if (Hdr->ch_type != ELFCOMPRESS_ZLIB) {
250
0
      error(toString(this) + ": unsupported compression type");
251
0
      return;
252
0
    }
253
0
254
0
    UncompressedSize = Hdr->ch_size;
255
0
    Alignment = std::max<uint32_t>(Hdr->ch_addralign, 1);
256
0
    RawData = RawData.slice(sizeof(*Hdr));
257
0
    return;
258
0
  }
259
2
260
2
  // New-style 32-bit header
261
2
  if (RawData.size() < sizeof(Chdr32)) {
262
0
    error(toString(this) + ": corrupted compressed section");
263
0
    return;
264
0
  }
265
2
266
2
  auto *Hdr = reinterpret_cast<const Chdr32 *>(RawData.data());
267
2
  if (Hdr->ch_type != ELFCOMPRESS_ZLIB) {
268
0
    error(toString(this) + ": unsupported compression type");
269
0
    return;
270
0
  }
271
2
272
2
  UncompressedSize = Hdr->ch_size;
273
2
  Alignment = std::max<uint32_t>(Hdr->ch_addralign, 1);
274
2
  RawData = RawData.slice(sizeof(*Hdr));
275
2
}
276
277
26
InputSection *InputSectionBase::getLinkOrderDep() const {
278
26
  assert(Link);
279
26
  assert(Flags & SHF_LINK_ORDER);
280
26
  return cast<InputSection>(File->getSections()[Link]);
281
26
}
282
283
// Find a function symbol that encloses a given location.
284
template <class ELFT>
285
1.85k
Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
286
1.85k
  for (Symbol *B : File->getSymbols())
287
18.1M
    if (Defined *D = dyn_cast<Defined>(B))
288
18.1M
      if (D->Section == this && 
D->Type == STT_FUNC18.1M
&&
D->Value <= Offset42
&&
289
18.1M
          
Offset < D->Value + D->Size30
)
290
16
        return D;
291
1.85k
  
return nullptr1.83k
;
292
1.85k
}
lld::elf::Defined* lld::elf::InputSectionBase::getEnclosingFunction<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned long long)
Line
Count
Source
285
4
Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
286
4
  for (Symbol *B : File->getSymbols())
287
9
    if (Defined *D = dyn_cast<Defined>(B))
288
5
      if (D->Section == this && 
D->Type == STT_FUNC1
&&
D->Value <= Offset0
&&
289
5
          
Offset < D->Value + D->Size0
)
290
0
        return D;
291
4
  return nullptr;
292
4
}
lld::elf::Defined* lld::elf::InputSectionBase::getEnclosingFunction<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned long long)
Line
Count
Source
285
2
Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
286
2
  for (Symbol *B : File->getSymbols())
287
6
    if (Defined *D = dyn_cast<Defined>(B))
288
4
      if (D->Section == this && 
D->Type == STT_FUNC2
&&
D->Value <= Offset0
&&
289
4
          
Offset < D->Value + D->Size0
)
290
0
        return D;
291
2
  return nullptr;
292
2
}
lld::elf::Defined* lld::elf::InputSectionBase::getEnclosingFunction<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned long long)
Line
Count
Source
285
30
Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
286
30
  for (Symbol *B : File->getSymbols())
287
149
    if (Defined *D = dyn_cast<Defined>(B))
288
118
      if (D->Section == this && 
D->Type == STT_FUNC46
&&
D->Value <= Offset27
&&
289
118
          
Offset < D->Value + D->Size21
)
290
10
        return D;
291
30
  
return nullptr20
;
292
30
}
lld::elf::Defined* lld::elf::InputSectionBase::getEnclosingFunction<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned long long)
Line
Count
Source
285
1.81k
Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
286
1.81k
  for (Symbol *B : File->getSymbols())
287
18.1M
    if (Defined *D = dyn_cast<Defined>(B))
288
18.1M
      if (D->Section == this && 
D->Type == STT_FUNC18.1M
&&
D->Value <= Offset15
&&
289
18.1M
          
Offset < D->Value + D->Size9
)
290
6
        return D;
291
1.81k
  
return nullptr1.81k
;
292
1.81k
}
293
294
// Returns a source location string. Used to construct an error message.
295
template <class ELFT>
296
1.84k
std::string InputSectionBase::getLocation(uint64_t Offset) {
297
1.84k
  std::string SecAndOffset = (Name + "+0x" + utohexstr(Offset)).str();
298
1.84k
299
1.84k
  // We don't have file for synthetic sections.
300
1.84k
  if (getFile<ELFT>() == nullptr)
301
2
    return (Config->OutputFile + ":(" + SecAndOffset + ")")
302
2
        .str();
303
1.84k
304
1.84k
  // First check if we can get desired values from debugging information.
305
1.84k
  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
306
1
    return Info->FileName + ":" + std::to_string(Info->Line) + ":(" +
307
1
           SecAndOffset + ")";
308
1.84k
309
1.84k
  // File->SourceFile contains STT_FILE symbol that contains a
310
1.84k
  // source file name. If it's missing, we use an object file name.
311
1.84k
  std::string SrcFile = getFile<ELFT>()->SourceFile;
312
1.84k
  if (SrcFile.empty())
313
1.84k
    SrcFile = toString(File);
314
1.84k
315
1.84k
  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
316
1
    return SrcFile + ":(function " + toString(*D) + ": " + SecAndOffset + ")";
317
1.83k
318
1.83k
  // If there's no symbol, print out the offset in the section.
319
1.83k
  return (SrcFile + ":(" + SecAndOffset + ")");
320
1.83k
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getLocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned long long)
Line
Count
Source
296
5
std::string InputSectionBase::getLocation(uint64_t Offset) {
297
5
  std::string SecAndOffset = (Name + "+0x" + utohexstr(Offset)).str();
298
5
299
5
  // We don't have file for synthetic sections.
300
5
  if (getFile<ELFT>() == nullptr)
301
1
    return (Config->OutputFile + ":(" + SecAndOffset + ")")
302
1
        .str();
303
4
304
4
  // First check if we can get desired values from debugging information.
305
4
  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
306
0
    return Info->FileName + ":" + std::to_string(Info->Line) + ":(" +
307
0
           SecAndOffset + ")";
308
4
309
4
  // File->SourceFile contains STT_FILE symbol that contains a
310
4
  // source file name. If it's missing, we use an object file name.
311
4
  std::string SrcFile = getFile<ELFT>()->SourceFile;
312
4
  if (SrcFile.empty())
313
4
    SrcFile = toString(File);
314
4
315
4
  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
316
0
    return SrcFile + ":(function " + toString(*D) + ": " + SecAndOffset + ")";
317
4
318
4
  // If there's no symbol, print out the offset in the section.
319
4
  return (SrcFile + ":(" + SecAndOffset + ")");
320
4
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getLocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned long long)
Line
Count
Source
296
2
std::string InputSectionBase::getLocation(uint64_t Offset) {
297
2
  std::string SecAndOffset = (Name + "+0x" + utohexstr(Offset)).str();
298
2
299
2
  // We don't have file for synthetic sections.
300
2
  if (getFile<ELFT>() == nullptr)
301
0
    return (Config->OutputFile + ":(" + SecAndOffset + ")")
302
0
        .str();
303
2
304
2
  // First check if we can get desired values from debugging information.
305
2
  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
306
0
    return Info->FileName + ":" + std::to_string(Info->Line) + ":(" +
307
0
           SecAndOffset + ")";
308
2
309
2
  // File->SourceFile contains STT_FILE symbol that contains a
310
2
  // source file name. If it's missing, we use an object file name.
311
2
  std::string SrcFile = getFile<ELFT>()->SourceFile;
312
2
  if (SrcFile.empty())
313
2
    SrcFile = toString(File);
314
2
315
2
  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
316
0
    return SrcFile + ":(function " + toString(*D) + ": " + SecAndOffset + ")";
317
2
318
2
  // If there's no symbol, print out the offset in the section.
319
2
  return (SrcFile + ":(" + SecAndOffset + ")");
320
2
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getLocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned long long)
Line
Count
Source
296
23
std::string InputSectionBase::getLocation(uint64_t Offset) {
297
23
  std::string SecAndOffset = (Name + "+0x" + utohexstr(Offset)).str();
298
23
299
23
  // We don't have file for synthetic sections.
300
23
  if (getFile<ELFT>() == nullptr)
301
1
    return (Config->OutputFile + ":(" + SecAndOffset + ")")
302
1
        .str();
303
22
304
22
  // First check if we can get desired values from debugging information.
305
22
  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
306
1
    return Info->FileName + ":" + std::to_string(Info->Line) + ":(" +
307
1
           SecAndOffset + ")";
308
21
309
21
  // File->SourceFile contains STT_FILE symbol that contains a
310
21
  // source file name. If it's missing, we use an object file name.
311
21
  std::string SrcFile = getFile<ELFT>()->SourceFile;
312
21
  if (SrcFile.empty())
313
21
    SrcFile = toString(File);
314
21
315
21
  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
316
1
    return SrcFile + ":(function " + toString(*D) + ": " + SecAndOffset + ")";
317
20
318
20
  // If there's no symbol, print out the offset in the section.
319
20
  return (SrcFile + ":(" + SecAndOffset + ")");
320
20
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getLocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned long long)
Line
Count
Source
296
1.81k
std::string InputSectionBase::getLocation(uint64_t Offset) {
297
1.81k
  std::string SecAndOffset = (Name + "+0x" + utohexstr(Offset)).str();
298
1.81k
299
1.81k
  // We don't have file for synthetic sections.
300
1.81k
  if (getFile<ELFT>() == nullptr)
301
0
    return (Config->OutputFile + ":(" + SecAndOffset + ")")
302
0
        .str();
303
1.81k
304
1.81k
  // First check if we can get desired values from debugging information.
305
1.81k
  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
306
0
    return Info->FileName + ":" + std::to_string(Info->Line) + ":(" +
307
0
           SecAndOffset + ")";
308
1.81k
309
1.81k
  // File->SourceFile contains STT_FILE symbol that contains a
310
1.81k
  // source file name. If it's missing, we use an object file name.
311
1.81k
  std::string SrcFile = getFile<ELFT>()->SourceFile;
312
1.81k
  if (SrcFile.empty())
313
1.81k
    SrcFile = toString(File);
314
1.81k
315
1.81k
  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
316
0
    return SrcFile + ":(function " + toString(*D) + ": " + SecAndOffset + ")";
317
1.81k
318
1.81k
  // If there's no symbol, print out the offset in the section.
319
1.81k
  return (SrcFile + ":(" + SecAndOffset + ")");
320
1.81k
}
321
322
// This function is intended to be used for constructing an error message.
323
// The returned message looks like this:
324
//
325
//   foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)
326
//
327
//  Returns an empty string if there's no way to get line info.
328
122
std::string InputSectionBase::getSrcMsg(const Symbol &Sym, uint64_t Offset) {
329
122
  return File->getSrcMsg(Sym, *this, Offset);
330
122
}
331
332
// Returns a filename string along with an optional section name. This
333
// function is intended to be used for constructing an error
334
// message. The returned message looks like this:
335
//
336
//   path/to/foo.o:(function bar)
337
//
338
// or
339
//
340
//   path/to/foo.o:(function bar) in archive path/to/bar.a
341
122
std::string InputSectionBase::getObjMsg(uint64_t Off) {
342
122
  std::string Filename = File->getName();
343
122
344
122
  std::string Archive;
345
122
  if (!File->ArchiveName.empty())
346
3
    Archive = " in archive " + File->ArchiveName;
347
122
348
122
  // Find a symbol that encloses a given location.
349
122
  for (Symbol *B : File->getSymbols())
350
551
    if (auto *D = dyn_cast<Defined>(B))
351
285
      if (D->Section == this && 
D->Value <= Off132
&&
Off < D->Value + D->Size123
)
352
12
        return Filename + ":(" + toString(*D) + ")" + Archive;
353
122
354
122
  // If there's no symbol, print out the offset in the section.
355
122
  return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive)
356
110
      .str();
357
122
}
358
359
InputSection InputSection::Discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
360
361
InputSection::InputSection(InputFile *F, uint64_t Flags, uint32_t Type,
362
                           uint32_t Alignment, ArrayRef<uint8_t> Data,
363
                           StringRef Name, Kind K)
364
    : InputSectionBase(F, Flags, Type,
365
                       /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, Alignment, Data,
366
28.9k
                       Name, K) {}
367
368
template <class ELFT>
369
InputSection::InputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header,
370
                           StringRef Name)
371
333k
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
lld::elf::InputSection::InputSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const&, llvm::StringRef)
Line
Count
Source
371
661
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
lld::elf::InputSection::InputSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const&, llvm::StringRef)
Line
Count
Source
371
483
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
lld::elf::InputSection::InputSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const&, llvm::StringRef)
Line
Count
Source
371
331k
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
lld::elf::InputSection::InputSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const&, llvm::StringRef)
Line
Count
Source
371
235
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
372
373
5.58M
bool InputSection::classof(const SectionBase *S) {
374
5.58M
  return S->kind() == SectionBase::Regular ||
375
5.58M
         
S->kind() == SectionBase::Synthetic110k
;
376
5.58M
}
377
378
345k
OutputSection *InputSection::getParent() const {
379
345k
  return cast_or_null<OutputSection>(Parent);
380
345k
}
381
382
// Copy SHT_GROUP section contents. Used only for the -r option.
383
7
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
384
7
  // ELFT::Word is the 32-bit integral type in the target endianness.
385
7
  using u32 = typename ELFT::Word;
386
7
  ArrayRef<u32> From = getDataAs<u32>();
387
7
  auto *To = reinterpret_cast<u32 *>(Buf);
388
7
389
7
  // The first entry is not a section number but a flag.
390
7
  *To++ = From[0];
391
7
392
7
  // Adjust section numbers because section numbers in an input object
393
7
  // files are different in the output.
394
7
  ArrayRef<InputSectionBase *> Sections = File->getSections();
395
7
  for (uint32_t Idx : From.slice(1))
396
12
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
397
7
}
Unexecuted instantiation: void lld::elf::InputSection::copyShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Unexecuted instantiation: void lld::elf::InputSection::copyShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
void lld::elf::InputSection::copyShtGroup<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
383
7
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
384
7
  // ELFT::Word is the 32-bit integral type in the target endianness.
385
7
  using u32 = typename ELFT::Word;
386
7
  ArrayRef<u32> From = getDataAs<u32>();
387
7
  auto *To = reinterpret_cast<u32 *>(Buf);
388
7
389
7
  // The first entry is not a section number but a flag.
390
7
  *To++ = From[0];
391
7
392
7
  // Adjust section numbers because section numbers in an input object
393
7
  // files are different in the output.
394
7
  ArrayRef<InputSectionBase *> Sections = File->getSections();
395
7
  for (uint32_t Idx : From.slice(1))
396
12
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
397
7
}
Unexecuted instantiation: void lld::elf::InputSection::copyShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
398
399
4.98M
InputSectionBase *InputSection::getRelocatedSection() const {
400
4.98M
  if (!File || 
(4.92M
Type != SHT_RELA4.92M
&&
Type != SHT_REL4.92M
))
401
4.98M
    return nullptr;
402
249
  ArrayRef<InputSectionBase *> Sections = File->getSections();
403
249
  return Sections[Info];
404
249
}
405
406
// This is used for -r and --emit-relocs. We can't use memcpy to copy
407
// relocations because we need to update symbol table offset and section index
408
// for each relocation. So we copy relocations one by one.
409
template <class ELFT, class RelTy>
410
61
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
411
61
  InputSectionBase *Sec = getRelocatedSection();
412
61
413
103
  for (const RelTy &Rel : Rels) {
414
103
    RelType Type = Rel.getType(Config->IsMips64EL);
415
103
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
416
103
417
103
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
418
103
    Buf += sizeof(RelTy);
419
103
420
103
    if (RelTy::IsRela)
421
73
      P->r_addend = getAddend<ELFT>(Rel);
422
103
423
103
    // Output section VA is zero for -r, so r_offset is an offset within the
424
103
    // section, but for --emit-relocs it is an virtual address.
425
103
    P->r_offset = Sec->getVA(Rel.r_offset);
426
103
    P->setSymbolAndType(In.SymTab->getSymbolIndex(&Sym), Type,
427
103
                        Config->IsMips64EL);
428
103
429
103
    if (Sym.Type == STT_SECTION) {
430
49
      // We combine multiple section symbols into only one per
431
49
      // section. This means we have to update the addend. That is
432
49
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
433
49
      // section data. We do that by adding to the Relocation vector.
434
49
435
49
      // .eh_frame is horribly special and can reference discarded sections. To
436
49
      // avoid having to parse and recreate .eh_frame, we just replace any
437
49
      // relocation in it pointing to discarded sections with R_*_NONE, which
438
49
      // hopefully creates a frame that is ignored at runtime.
439
49
      auto *D = dyn_cast<Defined>(&Sym);
440
49
      if (!D) {
441
0
        error("STT_SECTION symbol should be defined");
442
0
        continue;
443
0
      }
444
49
      SectionBase *Section = D->Section->Repl;
445
49
      if (!Section->Live) {
446
2
        P->setSymbolAndType(0, 0, false);
447
2
        continue;
448
2
      }
449
47
450
47
      int64_t Addend = getAddend<ELFT>(Rel);
451
47
      const uint8_t *BufLoc = Sec->data().begin() + Rel.r_offset;
452
47
      if (!RelTy::IsRela)
453
20
        Addend = Target->getImplicitAddend(BufLoc, Type);
454
47
455
47
      if (Config->EMachine == EM_MIPS && 
Config->Relocatable10
&&
456
47
          
Target->getRelExpr(Type, Sym, BufLoc) == R_MIPS_GOTREL10
) {
457
2
        // Some MIPS relocations depend on "gp" value. By default,
458
2
        // this value has 0x7ff0 offset from a .got section. But
459
2
        // relocatable files produced by a complier or a linker
460
2
        // might redefine this default value and we must use it
461
2
        // for a calculation of the relocation result. When we
462
2
        // generate EXE or DSO it's trivial. Generating a relocatable
463
2
        // output is more difficult case because the linker does
464
2
        // not calculate relocations in this mode and loses
465
2
        // individual "gp" values used by each input object file.
466
2
        // As a workaround we add the "gp" value to the relocation
467
2
        // addend and save it back to the file.
468
2
        Addend += Sec->getFile<ELFT>()->MipsGp0;
469
2
      }
470
47
471
47
      if (RelTy::IsRela)
472
27
        P->r_addend = Sym.getVA(Addend) - Section->getOutputSection()->Addr;
473
20
      else if (Config->Relocatable && Type != Target->NoneRel)
474
20
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset, Addend, &Sym});
475
47
    }
476
103
  }
477
61
}
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
410
13
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
411
13
  InputSectionBase *Sec = getRelocatedSection();
412
13
413
22
  for (const RelTy &Rel : Rels) {
414
22
    RelType Type = Rel.getType(Config->IsMips64EL);
415
22
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
416
22
417
22
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
418
22
    Buf += sizeof(RelTy);
419
22
420
22
    if (RelTy::IsRela)
421
0
      P->r_addend = getAddend<ELFT>(Rel);
422
22
423
22
    // Output section VA is zero for -r, so r_offset is an offset within the
424
22
    // section, but for --emit-relocs it is an virtual address.
425
22
    P->r_offset = Sec->getVA(Rel.r_offset);
426
22
    P->setSymbolAndType(In.SymTab->getSymbolIndex(&Sym), Type,
427
22
                        Config->IsMips64EL);
428
22
429
22
    if (Sym.Type == STT_SECTION) {
430
14
      // We combine multiple section symbols into only one per
431
14
      // section. This means we have to update the addend. That is
432
14
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
433
14
      // section data. We do that by adding to the Relocation vector.
434
14
435
14
      // .eh_frame is horribly special and can reference discarded sections. To
436
14
      // avoid having to parse and recreate .eh_frame, we just replace any
437
14
      // relocation in it pointing to discarded sections with R_*_NONE, which
438
14
      // hopefully creates a frame that is ignored at runtime.
439
14
      auto *D = dyn_cast<Defined>(&Sym);
440
14
      if (!D) {
441
0
        error("STT_SECTION symbol should be defined");
442
0
        continue;
443
0
      }
444
14
      SectionBase *Section = D->Section->Repl;
445
14
      if (!Section->Live) {
446
0
        P->setSymbolAndType(0, 0, false);
447
0
        continue;
448
0
      }
449
14
450
14
      int64_t Addend = getAddend<ELFT>(Rel);
451
14
      const uint8_t *BufLoc = Sec->data().begin() + Rel.r_offset;
452
14
      if (!RelTy::IsRela)
453
14
        Addend = Target->getImplicitAddend(BufLoc, Type);
454
14
455
14
      if (Config->EMachine == EM_MIPS && 
Config->Relocatable4
&&
456
14
          
Target->getRelExpr(Type, Sym, BufLoc) == R_MIPS_GOTREL4
) {
457
0
        // Some MIPS relocations depend on "gp" value. By default,
458
0
        // this value has 0x7ff0 offset from a .got section. But
459
0
        // relocatable files produced by a complier or a linker
460
0
        // might redefine this default value and we must use it
461
0
        // for a calculation of the relocation result. When we
462
0
        // generate EXE or DSO it's trivial. Generating a relocatable
463
0
        // output is more difficult case because the linker does
464
0
        // not calculate relocations in this mode and loses
465
0
        // individual "gp" values used by each input object file.
466
0
        // As a workaround we add the "gp" value to the relocation
467
0
        // addend and save it back to the file.
468
0
        Addend += Sec->getFile<ELFT>()->MipsGp0;
469
0
      }
470
14
471
14
      if (RelTy::IsRela)
472
0
        P->r_addend = Sym.getVA(Addend) - Section->getOutputSection()->Addr;
473
14
      else if (Config->Relocatable && Type != Target->NoneRel)
474
14
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset, Addend, &Sym});
475
14
    }
476
22
  }
477
13
}
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
410
4
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
411
4
  InputSectionBase *Sec = getRelocatedSection();
412
4
413
7
  for (const RelTy &Rel : Rels) {
414
7
    RelType Type = Rel.getType(Config->IsMips64EL);
415
7
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
416
7
417
7
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
418
7
    Buf += sizeof(RelTy);
419
7
420
7
    if (RelTy::IsRela)
421
0
      P->r_addend = getAddend<ELFT>(Rel);
422
7
423
7
    // Output section VA is zero for -r, so r_offset is an offset within the
424
7
    // section, but for --emit-relocs it is an virtual address.
425
7
    P->r_offset = Sec->getVA(Rel.r_offset);
426
7
    P->setSymbolAndType(In.SymTab->getSymbolIndex(&Sym), Type,
427
7
                        Config->IsMips64EL);
428
7
429
7
    if (Sym.Type == STT_SECTION) {
430
6
      // We combine multiple section symbols into only one per
431
6
      // section. This means we have to update the addend. That is
432
6
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
433
6
      // section data. We do that by adding to the Relocation vector.
434
6
435
6
      // .eh_frame is horribly special and can reference discarded sections. To
436
6
      // avoid having to parse and recreate .eh_frame, we just replace any
437
6
      // relocation in it pointing to discarded sections with R_*_NONE, which
438
6
      // hopefully creates a frame that is ignored at runtime.
439
6
      auto *D = dyn_cast<Defined>(&Sym);
440
6
      if (!D) {
441
0
        error("STT_SECTION symbol should be defined");
442
0
        continue;
443
0
      }
444
6
      SectionBase *Section = D->Section->Repl;
445
6
      if (!Section->Live) {
446
0
        P->setSymbolAndType(0, 0, false);
447
0
        continue;
448
0
      }
449
6
450
6
      int64_t Addend = getAddend<ELFT>(Rel);
451
6
      const uint8_t *BufLoc = Sec->data().begin() + Rel.r_offset;
452
6
      if (!RelTy::IsRela)
453
6
        Addend = Target->getImplicitAddend(BufLoc, Type);
454
6
455
6
      if (Config->EMachine == EM_MIPS && Config->Relocatable &&
456
6
          Target->getRelExpr(Type, Sym, BufLoc) == R_MIPS_GOTREL) {
457
2
        // Some MIPS relocations depend on "gp" value. By default,
458
2
        // this value has 0x7ff0 offset from a .got section. But
459
2
        // relocatable files produced by a complier or a linker
460
2
        // might redefine this default value and we must use it
461
2
        // for a calculation of the relocation result. When we
462
2
        // generate EXE or DSO it's trivial. Generating a relocatable
463
2
        // output is more difficult case because the linker does
464
2
        // not calculate relocations in this mode and loses
465
2
        // individual "gp" values used by each input object file.
466
2
        // As a workaround we add the "gp" value to the relocation
467
2
        // addend and save it back to the file.
468
2
        Addend += Sec->getFile<ELFT>()->MipsGp0;
469
2
      }
470
6
471
6
      if (RelTy::IsRela)
472
0
        P->r_addend = Sym.getVA(Addend) - Section->getOutputSection()->Addr;
473
6
      else if (Config->Relocatable && Type != Target->NoneRel)
474
6
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset, Addend, &Sym});
475
6
    }
476
7
  }
477
4
}
void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
410
44
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
411
44
  InputSectionBase *Sec = getRelocatedSection();
412
44
413
74
  for (const RelTy &Rel : Rels) {
414
74
    RelType Type = Rel.getType(Config->IsMips64EL);
415
74
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
416
74
417
74
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
418
74
    Buf += sizeof(RelTy);
419
74
420
74
    if (RelTy::IsRela)
421
73
      P->r_addend = getAddend<ELFT>(Rel);
422
74
423
74
    // Output section VA is zero for -r, so r_offset is an offset within the
424
74
    // section, but for --emit-relocs it is an virtual address.
425
74
    P->r_offset = Sec->getVA(Rel.r_offset);
426
74
    P->setSymbolAndType(In.SymTab->getSymbolIndex(&Sym), Type,
427
74
                        Config->IsMips64EL);
428
74
429
74
    if (Sym.Type == STT_SECTION) {
430
29
      // We combine multiple section symbols into only one per
431
29
      // section. This means we have to update the addend. That is
432
29
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
433
29
      // section data. We do that by adding to the Relocation vector.
434
29
435
29
      // .eh_frame is horribly special and can reference discarded sections. To
436
29
      // avoid having to parse and recreate .eh_frame, we just replace any
437
29
      // relocation in it pointing to discarded sections with R_*_NONE, which
438
29
      // hopefully creates a frame that is ignored at runtime.
439
29
      auto *D = dyn_cast<Defined>(&Sym);
440
29
      if (!D) {
441
0
        error("STT_SECTION symbol should be defined");
442
0
        continue;
443
0
      }
444
29
      SectionBase *Section = D->Section->Repl;
445
29
      if (!Section->Live) {
446
2
        P->setSymbolAndType(0, 0, false);
447
2
        continue;
448
2
      }
449
27
450
27
      int64_t Addend = getAddend<ELFT>(Rel);
451
27
      const uint8_t *BufLoc = Sec->data().begin() + Rel.r_offset;
452
27
      if (!RelTy::IsRela)
453
0
        Addend = Target->getImplicitAddend(BufLoc, Type);
454
27
455
27
      if (Config->EMachine == EM_MIPS && 
Config->Relocatable0
&&
456
27
          
Target->getRelExpr(Type, Sym, BufLoc) == R_MIPS_GOTREL0
) {
457
0
        // Some MIPS relocations depend on "gp" value. By default,
458
0
        // this value has 0x7ff0 offset from a .got section. But
459
0
        // relocatable files produced by a complier or a linker
460
0
        // might redefine this default value and we must use it
461
0
        // for a calculation of the relocation result. When we
462
0
        // generate EXE or DSO it's trivial. Generating a relocatable
463
0
        // output is more difficult case because the linker does
464
0
        // not calculate relocations in this mode and loses
465
0
        // individual "gp" values used by each input object file.
466
0
        // As a workaround we add the "gp" value to the relocation
467
0
        // addend and save it back to the file.
468
0
        Addend += Sec->getFile<ELFT>()->MipsGp0;
469
0
      }
470
27
471
27
      if (RelTy::IsRela)
472
27
        P->r_addend = Sym.getVA(Addend) - Section->getOutputSection()->Addr;
473
0
      else if (Config->Relocatable && Type != Target->NoneRel)
474
0
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset, Addend, &Sym});
475
27
    }
476
74
  }
477
44
}
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
478
479
// The ARM and AArch64 ABI handle pc-relative relocations to undefined weak
480
// references specially. The general rule is that the value of the symbol in
481
// this context is the address of the place P. A further special case is that
482
// branch relocations to an undefined weak reference resolve to the next
483
// instruction.
484
static uint32_t getARMUndefinedRelativeWeakVA(RelType Type, uint32_t A,
485
15
                                              uint32_t P) {
486
15
  switch (Type) {
487
15
  // Unresolved branch relocations to weak references resolve to next
488
15
  // instruction, this will be either 2 or 4 bytes on from P.
489
15
  case R_ARM_THM_JUMP11:
490
0
    return P + 2 + A;
491
15
  case R_ARM_CALL:
492
7
  case R_ARM_JUMP24:
493
7
  case R_ARM_PC24:
494
7
  case R_ARM_PLT32:
495
7
  case R_ARM_PREL31:
496
7
  case R_ARM_THM_JUMP19:
497
7
  case R_ARM_THM_JUMP24:
498
7
    return P + 4 + A;
499
7
  case R_ARM_THM_CALL:
500
3
    // We don't want an interworking BLX to ARM
501
3
    return P + 5 + A;
502
7
  // Unresolved non branch pc-relative relocations
503
7
  // R_ARM_TARGET2 which can be resolved relatively is not present as it never
504
7
  // targets a weak-reference.
505
7
  case R_ARM_MOVW_PREL_NC:
506
5
  case R_ARM_MOVT_PREL:
507
5
  case R_ARM_REL32:
508
5
  case R_ARM_THM_MOVW_PREL_NC:
509
5
  case R_ARM_THM_MOVT_PREL:
510
5
    return P + A;
511
0
  }
512
0
  llvm_unreachable("ARM pc-relative relocation expected\n");
513
0
}
514
515
// The comment above getARMUndefinedRelativeWeakVA applies to this function.
516
static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t Type, uint64_t A,
517
0
                                                  uint64_t P) {
518
0
  switch (Type) {
519
0
  // Unresolved branch relocations to weak references resolve to next
520
0
  // instruction, this is 4 bytes on from P.
521
0
  case R_AARCH64_CALL26:
522
0
  case R_AARCH64_CONDBR19:
523
0
  case R_AARCH64_JUMP26:
524
0
  case R_AARCH64_TSTBR14:
525
0
    return P + 4 + A;
526
0
  // Unresolved non branch pc-relative relocations
527
0
  case R_AARCH64_PREL16:
528
0
  case R_AARCH64_PREL32:
529
0
  case R_AARCH64_PREL64:
530
0
  case R_AARCH64_ADR_PREL_LO21:
531
0
  case R_AARCH64_LD_PREL_LO19:
532
0
    return P + A;
533
0
  }
534
0
  llvm_unreachable("AArch64 pc-relative relocation expected\n");
535
0
}
536
537
// ARM SBREL relocations are of the form S + A - B where B is the static base
538
// The ARM ABI defines base to be "addressing origin of the output segment
539
// defining the symbol S". We defined the "addressing origin"/static base to be
540
// the base of the PT_LOAD segment containing the Sym.
541
// The procedure call standard only defines a Read Write Position Independent
542
// RWPI variant so in practice we should expect the static base to be the base
543
// of the RW segment.
544
0
static uint64_t getARMStaticBase(const Symbol &Sym) {
545
0
  OutputSection *OS = Sym.getOutputSection();
546
0
  if (!OS || !OS->PtLoad || !OS->PtLoad->FirstSec)
547
0
    fatal("SBREL relocation to " + Sym.getName() + " without static base");
548
0
  return OS->PtLoad->FirstSec->Addr;
549
0
}
550
551
// For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually
552
// points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA
553
// is calculated using PCREL_HI20's symbol.
554
//
555
// This function returns the R_RISCV_PCREL_HI20 relocation from
556
// R_RISCV_PCREL_LO12's symbol and addend.
557
0
static Relocation *getRISCVPCRelHi20(const Symbol *Sym, uint64_t Addend) {
558
0
  const Defined *D = cast<Defined>(Sym);
559
0
  InputSection *IS = cast<InputSection>(D->Section);
560
0
561
0
  if (Addend != 0)
562
0
    warn("Non-zero addend in R_RISCV_PCREL_LO12 relocation to " +
563
0
         IS->getObjMsg(D->Value) + " is ignored");
564
0
565
0
  // Relocations are sorted by offset, so we can use std::equal_range to do
566
0
  // binary search.
567
0
  Relocation R;
568
0
  R.Offset = D->Value;
569
0
  auto Range =
570
0
      std::equal_range(IS->Relocations.begin(), IS->Relocations.end(), R,
571
0
                       [](const Relocation &LHS, const Relocation &RHS) {
572
0
                         return LHS.Offset < RHS.Offset;
573
0
                       });
574
0
575
0
  for (auto It = Range.first; It != Range.second; ++It)
576
0
    if (It->Expr == R_PC)
577
0
      return &*It;
578
0
579
0
  error("R_RISCV_PCREL_LO12 relocation points to " + IS->getObjMsg(D->Value) +
580
0
        " without an associated R_RISCV_PCREL_HI20 relocation");
581
0
  return nullptr;
582
0
}
583
584
// A TLS symbol's virtual address is relative to the TLS segment. Add a
585
// target-specific adjustment to produce a thread-pointer-relative offset.
586
54
static int64_t getTlsTpOffset() {
587
54
  switch (Config->EMachine) {
588
54
  case EM_ARM:
589
26
  case EM_AARCH64:
590
26
    // Variant 1. The thread pointer points to a TCB with a fixed 2-word size,
591
26
    // followed by a variable amount of alignment padding, followed by the TLS
592
26
    // segment.
593
26
    return alignTo(Config->Wordsize * 2, Out::TlsPhdr->p_align);
594
26
  case EM_386:
595
23
  case EM_X86_64:
596
23
    // Variant 2. The TLS segment is located just before the thread pointer.
597
23
    return -alignTo(Out::TlsPhdr->p_memsz, Out::TlsPhdr->p_align);
598
23
  case EM_PPC64:
599
5
    // The thread pointer points to a fixed offset from the start of the
600
5
    // executable's TLS segment. An offset of 0x7000 allows a signed 16-bit
601
5
    // offset to reach 0x1000 of TCB/thread-library data and 0xf000 of the
602
5
    // program's TLS segment.
603
5
    return -0x7000;
604
23
  default:
605
0
    llvm_unreachable("unhandled Config->EMachine");
606
54
  }
607
54
}
608
609
static uint64_t getRelocTargetVA(const InputFile *File, RelType Type, int64_t A,
610
12.0k
                                 uint64_t P, const Symbol &Sym, RelExpr Expr) {
611
12.0k
  switch (Expr) {
612
12.0k
  case R_ABS:
613
566
  case R_DTPREL:
614
566
  case R_RELAX_TLS_LD_TO_LE_ABS:
615
566
  case R_RELAX_GOT_PC_NOPIC:
616
566
    return Sym.getVA(A);
617
566
  case R_ADDEND:
618
8
    return A;
619
566
  case R_ARM_SBREL:
620
0
    return Sym.getVA(A) - getARMStaticBase(Sym);
621
566
  case R_GOT:
622
29
  case R_RELAX_TLS_GD_TO_IE_ABS:
623
29
    return Sym.getGotVA() + A;
624
29
  case R_GOTONLY_PC:
625
0
    return In.Got->getVA() + A - P;
626
29
  case R_GOTPLTONLY_PC:
627
6
    return In.GotPlt->getVA() + A - P;
628
75
  case R_GOTREL:
629
75
  case R_PPC64_RELAX_TOC:
630
75
    return Sym.getVA(A) - In.Got->getVA();
631
75
  case R_GOTPLTREL:
632
1
    return Sym.getVA(A) - In.GotPlt->getVA();
633
75
  case R_GOTPLT:
634
16
  case R_RELAX_TLS_GD_TO_IE_GOTPLT:
635
16
    return Sym.getGotVA() + A - In.GotPlt->getVA();
636
45
  case R_TLSLD_GOT_OFF:
637
45
  case R_GOT_OFF:
638
45
  case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
639
45
    return Sym.getGotOffset() + A;
640
45
  case R_AARCH64_GOT_PAGE_PC:
641
6
  case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
642
6
    return getAArch64Page(Sym.getGotVA() + A) - getAArch64Page(P);
643
80
  case R_GOT_PC:
644
80
  case R_RELAX_TLS_GD_TO_IE:
645
80
    return Sym.getGotVA() + A - P;
646
80
  case R_HEXAGON_GOT:
647
4
    return Sym.getGotVA() - In.GotPlt->getVA();
648
80
  case R_MIPS_GOTREL:
649
6
    return Sym.getVA(A) - In.MipsGot->getGp(File);
650
80
  case R_MIPS_GOT_GP:
651
2
    return In.MipsGot->getGp(File) + A;
652
80
  case R_MIPS_GOT_GP_PC: {
653
18
    // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
654
18
    // is _gp_disp symbol. In that case we should use the following
655
18
    // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
656
18
    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
657
18
    // microMIPS variants of these relocations use slightly different
658
18
    // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
659
18
    // to correctly handle less-sugnificant bit of the microMIPS symbol.
660
18
    uint64_t V = In.MipsGot->getGp(File) + A - P;
661
18
    if (Type == R_MIPS_LO16 || 
Type == R_MICROMIPS_LO1611
)
662
9
      V += 4;
663
18
    if (Type == R_MICROMIPS_LO16 || 
Type == R_MICROMIPS_HI1616
)
664
4
      V -= 1;
665
18
    return V;
666
80
  }
667
80
  case R_MIPS_GOT_LOCAL_PAGE:
668
10
    // If relocation against MIPS local symbol requires GOT entry, this entry
669
10
    // should be initialized by 'page address'. This address is high 16-bits
670
10
    // of sum the symbol's value and the addend.
671
10
    return In.MipsGot->getVA() + In.MipsGot->getPageEntryOffset(File, Sym, A) -
672
10
           In.MipsGot->getGp(File);
673
10.0k
  case R_MIPS_GOT_OFF:
674
10.0k
  case R_MIPS_GOT_OFF32:
675
10.0k
    // In case of MIPS if a GOT relocation has non-zero addend this addend
676
10.0k
    // should be applied to the GOT entry content not to the GOT entry offset.
677
10.0k
    // That is why we use separate expression type.
678
10.0k
    return In.MipsGot->getVA() + In.MipsGot->getSymEntryOffset(File, Sym, A) -
679
10.0k
           In.MipsGot->getGp(File);
680
10.0k
  case R_MIPS_TLSGD:
681
10
    return In.MipsGot->getVA() + In.MipsGot->getGlobalDynOffset(File, Sym) -
682
10
           In.MipsGot->getGp(File);
683
10.0k
  case R_MIPS_TLSLD:
684
5
    return In.MipsGot->getVA() + In.MipsGot->getTlsIndexOffset(File) -
685
5
           In.MipsGot->getGp(File);
686
10.0k
  case R_AARCH64_PAGE_PC: {
687
38
    uint64_t Val = Sym.isUndefWeak() ? 
P + A0
: Sym.getVA(A);
688
38
    return getAArch64Page(Val) - getAArch64Page(P);
689
10.0k
  }
690
10.0k
  case R_RISCV_PC_INDIRECT: {
691
0
    if (const Relocation *HiRel = getRISCVPCRelHi20(&Sym, A))
692
0
      return getRelocTargetVA(File, HiRel->Type, HiRel->Addend, Sym.getVA(),
693
0
                              *HiRel->Sym, HiRel->Expr);
694
0
    return 0;
695
0
  }
696
690
  case R_PC: {
697
690
    uint64_t Dest;
698
690
    if (Sym.isUndefWeak()) {
699
19
      // On ARM and AArch64 a branch to an undefined weak resolves to the
700
19
      // next instruction, otherwise the place.
701
19
      if (Config->EMachine == EM_ARM)
702
15
        Dest = getARMUndefinedRelativeWeakVA(Type, A, P);
703
4
      else if (Config->EMachine == EM_AARCH64)
704
0
        Dest = getAArch64UndefinedRelativeWeakVA(Type, A, P);
705
4
      else
706
4
        Dest = Sym.getVA(A);
707
671
    } else {
708
671
      Dest = Sym.getVA(A);
709
671
    }
710
690
    return Dest - P;
711
0
  }
712
7
  case R_PLT:
713
7
    return Sym.getPltVA() + A;
714
159
  case R_PLT_PC:
715
159
  case R_PPC_CALL_PLT:
716
159
    return Sym.getPltVA() + A - P;
717
159
  case R_PPC_CALL: {
718
80
    uint64_t SymVA = Sym.getVA(A);
719
80
    // If we have an undefined weak symbol, we might get here with a symbol
720
80
    // address of zero. That could overflow, but the code must be unreachable,
721
80
    // so don't bother doing anything at all.
722
80
    if (!SymVA)
723
1
      return 0;
724
79
725
79
    // PPC64 V2 ABI describes two entry points to a function. The global entry
726
79
    // point is used for calls where the caller and callee (may) have different
727
79
    // TOC base pointers and r2 needs to be modified to hold the TOC base for
728
79
    // the callee. For local calls the caller and callee share the same
729
79
    // TOC base and so the TOC pointer initialization code should be skipped by
730
79
    // branching to the local entry point.
731
79
    return SymVA - P + getPPC64GlobalEntryToLocalEntryOffset(Sym.StOther);
732
79
  }
733
79
  case R_PPC_TOC:
734
0
    return getPPC64TocBase() + A;
735
79
  case R_RELAX_GOT_PC:
736
21
    return Sym.getVA(A) - P;
737
79
  case R_RELAX_TLS_GD_TO_LE:
738
54
  case R_RELAX_TLS_IE_TO_LE:
739
54
  case R_RELAX_TLS_LD_TO_LE:
740
54
  case R_TLS:
741
54
    // It is not very clear what to return if the symbol is undefined. With
742
54
    // --noinhibit-exec, even a non-weak undefined reference may reach here.
743
54
    // Just return A, which matches R_ABS, and the behavior of some dynamic
744
54
    // loaders.
745
54
    if (Sym.isUndefined())
746
0
      return A;
747
54
    return Sym.getVA(A) + getTlsTpOffset();
748
54
  case R_RELAX_TLS_GD_TO_LE_NEG:
749
2
  case R_NEG_TLS:
750
2
    if (Sym.isUndefined())
751
2
      return A;
752
0
    return -Sym.getVA(0) - getTlsTpOffset() + A;
753
6
  case R_SIZE:
754
6
    return Sym.getSize() + A;
755
8
  case R_TLSDESC:
756
8
    return In.Got->getGlobalDynAddr(Sym) + A;
757
4
  case R_AARCH64_TLSDESC_PAGE:
758
4
    return getAArch64Page(In.Got->getGlobalDynAddr(Sym) + A) -
759
4
           getAArch64Page(P);
760
4
  case R_TLSGD_GOT:
761
4
    return In.Got->getGlobalDynOffset(Sym) + A;
762
2
  case R_TLSGD_GOTPLT:
763
2
    return In.Got->getVA() + In.Got->getGlobalDynOffset(Sym) + A - In.GotPlt->getVA();
764
15
  case R_TLSGD_PC:
765
15
    return In.Got->getGlobalDynAddr(Sym) + A - P;
766
3
  case R_TLSLD_GOTPLT:
767
3
    return In.Got->getVA() + In.Got->getTlsIndexOff() + A - In.GotPlt->getVA();
768
10
  case R_TLSLD_GOT:
769
10
    return In.Got->getTlsIndexOff() + A;
770
2
  case R_TLSLD_PC:
771
2
    return In.Got->getTlsIndexVA() + A - P;
772
0
  default:
773
0
    llvm_unreachable("invalid expression");
774
12.0k
  }
775
12.0k
}
776
777
// This function applies relocations to sections without SHF_ALLOC bit.
778
// Such sections are never mapped to memory at runtime. Debug sections are
779
// an example. Relocations in non-alloc sections are much easier to
780
// handle than in allocated sections because it will never need complex
781
// treatement such as GOT or PLT (because at runtime no one refers them).
782
// So, we handle relocations for non-alloc sections directly in this
783
// function as a performance optimization.
784
template <class ELFT, class RelTy>
785
196k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
196k
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
196k
788
196k
  for (const RelTy &Rel : Rels) {
789
57
    RelType Type = Rel.getType(Config->IsMips64EL);
790
57
791
57
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
57
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
57
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
57
    // need to keep this bug-compatible code for a while.
795
57
    if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC2
)
796
0
      continue;
797
57
798
57
    uint64_t Offset = getOffset(Rel.r_offset);
799
57
    uint8_t *BufLoc = Buf + Offset;
800
57
    int64_t Addend = getAddend<ELFT>(Rel);
801
57
    if (!RelTy::IsRela)
802
5
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
57
804
57
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
57
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
57
    if (Expr == R_NONE)
807
2
      continue;
808
55
809
55
    if (Expr != R_ABS && 
Expr != R_DTPREL3
) {
810
2
      std::string Msg = getLocation<ELFT>(Offset) +
811
2
                        ": has non-ABS relocation " + toString(Type) +
812
2
                        " against symbol '" + toString(Sym) + "'";
813
2
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
2
818
2
      // If the control reaches here, we found a PC-relative relocation in a
819
2
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
2
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
2
      // this is a usage error. However, GNU linkers historically accept such
822
2
      // relocations without any errors and relocate them as if they were at
823
2
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
2
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
2
      warn(Msg);
826
2
      Target->relocateOne(BufLoc, Type,
827
2
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
2
      continue;
829
2
    }
830
53
831
53
    if (Sym.isTls() && 
!Out::TlsPhdr2
)
832
1
      Target->relocateOne(BufLoc, Type, 0);
833
52
    else
834
52
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
53
  }
836
196k
}
Unexecuted instantiation: void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
785
33
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
33
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
33
788
33
  for (const RelTy &Rel : Rels) {
789
3
    RelType Type = Rel.getType(Config->IsMips64EL);
790
3
791
3
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
3
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
3
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
3
    // need to keep this bug-compatible code for a while.
795
3
    if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC2
)
796
0
      continue;
797
3
798
3
    uint64_t Offset = getOffset(Rel.r_offset);
799
3
    uint8_t *BufLoc = Buf + Offset;
800
3
    int64_t Addend = getAddend<ELFT>(Rel);
801
3
    if (!RelTy::IsRela)
802
3
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
3
804
3
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
3
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
3
    if (Expr == R_NONE)
807
2
      continue;
808
1
809
1
    if (Expr != R_ABS && 
Expr != R_DTPREL0
) {
810
0
      std::string Msg = getLocation<ELFT>(Offset) +
811
0
                        ": has non-ABS relocation " + toString(Type) +
812
0
                        " against symbol '" + toString(Sym) + "'";
813
0
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
0
818
0
      // If the control reaches here, we found a PC-relative relocation in a
819
0
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
0
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
0
      // this is a usage error. However, GNU linkers historically accept such
822
0
      // relocations without any errors and relocate them as if they were at
823
0
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
0
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
0
      warn(Msg);
826
0
      Target->relocateOne(BufLoc, Type,
827
0
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
0
      continue;
829
0
    }
830
1
831
1
    if (Sym.isTls() && 
!Out::TlsPhdr0
)
832
0
      Target->relocateOne(BufLoc, Type, 0);
833
1
    else
834
1
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
1
  }
836
33
}
Unexecuted instantiation: void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
785
2
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
2
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
2
788
2
  for (const RelTy &Rel : Rels) {
789
2
    RelType Type = Rel.getType(Config->IsMips64EL);
790
2
791
2
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
2
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
2
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
2
    // need to keep this bug-compatible code for a while.
795
2
    if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC0
)
796
0
      continue;
797
2
798
2
    uint64_t Offset = getOffset(Rel.r_offset);
799
2
    uint8_t *BufLoc = Buf + Offset;
800
2
    int64_t Addend = getAddend<ELFT>(Rel);
801
2
    if (!RelTy::IsRela)
802
2
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
2
804
2
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
2
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
2
    if (Expr == R_NONE)
807
0
      continue;
808
2
809
2
    if (Expr != R_ABS && 
Expr != R_DTPREL0
) {
810
0
      std::string Msg = getLocation<ELFT>(Offset) +
811
0
                        ": has non-ABS relocation " + toString(Type) +
812
0
                        " against symbol '" + toString(Sym) + "'";
813
0
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
0
818
0
      // If the control reaches here, we found a PC-relative relocation in a
819
0
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
0
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
0
      // this is a usage error. However, GNU linkers historically accept such
822
0
      // relocations without any errors and relocate them as if they were at
823
0
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
0
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
0
      warn(Msg);
826
0
      Target->relocateOne(BufLoc, Type,
827
0
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
0
      continue;
829
0
    }
830
2
831
2
    if (Sym.isTls() && 
!Out::TlsPhdr0
)
832
0
      Target->relocateOne(BufLoc, Type, 0);
833
2
    else
834
2
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
2
  }
836
2
}
void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
785
25
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
25
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
25
788
51
  for (const RelTy &Rel : Rels) {
789
51
    RelType Type = Rel.getType(Config->IsMips64EL);
790
51
791
51
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
51
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
51
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
51
    // need to keep this bug-compatible code for a while.
795
51
    if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC0
)
796
0
      continue;
797
51
798
51
    uint64_t Offset = getOffset(Rel.r_offset);
799
51
    uint8_t *BufLoc = Buf + Offset;
800
51
    int64_t Addend = getAddend<ELFT>(Rel);
801
51
    if (!RelTy::IsRela)
802
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
51
804
51
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
51
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
51
    if (Expr == R_NONE)
807
0
      continue;
808
51
809
51
    if (Expr != R_ABS && 
Expr != R_DTPREL3
) {
810
2
      std::string Msg = getLocation<ELFT>(Offset) +
811
2
                        ": has non-ABS relocation " + toString(Type) +
812
2
                        " against symbol '" + toString(Sym) + "'";
813
2
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
2
818
2
      // If the control reaches here, we found a PC-relative relocation in a
819
2
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
2
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
2
      // this is a usage error. However, GNU linkers historically accept such
822
2
      // relocations without any errors and relocate them as if they were at
823
2
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
2
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
2
      warn(Msg);
826
2
      Target->relocateOne(BufLoc, Type,
827
2
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
2
      continue;
829
2
    }
830
49
831
49
    if (Sym.isTls() && 
!Out::TlsPhdr2
)
832
1
      Target->relocateOne(BufLoc, Type, 0);
833
48
    else
834
48
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
49
  }
836
25
}
void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
785
196k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
196k
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
196k
788
196k
  for (const RelTy &Rel : Rels) {
789
0
    RelType Type = Rel.getType(Config->IsMips64EL);
790
0
791
0
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
0
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
0
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
0
    // need to keep this bug-compatible code for a while.
795
0
    if (Config->EMachine == EM_386 && Type == R_386_GOTPC)
796
0
      continue;
797
0
798
0
    uint64_t Offset = getOffset(Rel.r_offset);
799
0
    uint8_t *BufLoc = Buf + Offset;
800
0
    int64_t Addend = getAddend<ELFT>(Rel);
801
0
    if (!RelTy::IsRela)
802
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
0
804
0
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
0
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
0
    if (Expr == R_NONE)
807
0
      continue;
808
0
809
0
    if (Expr != R_ABS && Expr != R_DTPREL) {
810
0
      std::string Msg = getLocation<ELFT>(Offset) +
811
0
                        ": has non-ABS relocation " + toString(Type) +
812
0
                        " against symbol '" + toString(Sym) + "'";
813
0
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
0
818
0
      // If the control reaches here, we found a PC-relative relocation in a
819
0
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
0
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
0
      // this is a usage error. However, GNU linkers historically accept such
822
0
      // relocations without any errors and relocate them as if they were at
823
0
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
0
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
0
      warn(Msg);
826
0
      Target->relocateOne(BufLoc, Type,
827
0
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
0
      continue;
829
0
    }
830
0
831
0
    if (Sym.isTls() && !Out::TlsPhdr)
832
0
      Target->relocateOne(BufLoc, Type, 0);
833
0
    else
834
0
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
0
  }
836
196k
}
void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
785
1
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
786
1
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
787
1
788
1
  for (const RelTy &Rel : Rels) {
789
1
    RelType Type = Rel.getType(Config->IsMips64EL);
790
1
791
1
    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
792
1
    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
793
1
    // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
794
1
    // need to keep this bug-compatible code for a while.
795
1
    if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC0
)
796
0
      continue;
797
1
798
1
    uint64_t Offset = getOffset(Rel.r_offset);
799
1
    uint8_t *BufLoc = Buf + Offset;
800
1
    int64_t Addend = getAddend<ELFT>(Rel);
801
1
    if (!RelTy::IsRela)
802
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
803
1
804
1
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
805
1
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
806
1
    if (Expr == R_NONE)
807
0
      continue;
808
1
809
1
    if (Expr != R_ABS && 
Expr != R_DTPREL0
) {
810
0
      std::string Msg = getLocation<ELFT>(Offset) +
811
0
                        ": has non-ABS relocation " + toString(Type) +
812
0
                        " against symbol '" + toString(Sym) + "'";
813
0
      if (Expr != R_PC) {
814
0
        error(Msg);
815
0
        return;
816
0
      }
817
0
818
0
      // If the control reaches here, we found a PC-relative relocation in a
819
0
      // non-ALLOC section. Since non-ALLOC section is not loaded into memory
820
0
      // at runtime, the notion of PC-relative doesn't make sense here. So,
821
0
      // this is a usage error. However, GNU linkers historically accept such
822
0
      // relocations without any errors and relocate them as if they were at
823
0
      // address 0. For bug-compatibilty, we accept them with warnings. We
824
0
      // know Steel Bank Common Lisp as of 2018 have this bug.
825
0
      warn(Msg);
826
0
      Target->relocateOne(BufLoc, Type,
827
0
                          SignExtend64<Bits>(Sym.getVA(Addend - Offset)));
828
0
      continue;
829
0
    }
830
1
831
1
    if (Sym.isTls() && 
!Out::TlsPhdr0
)
832
0
      Target->relocateOne(BufLoc, Type, 0);
833
1
    else
834
1
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
835
1
  }
836
1
}
Unexecuted instantiation: void lld::elf::InputSection::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
837
838
// This is used when '-r' is given.
839
// For REL targets, InputSection::copyRelocations() may store artificial
840
// relocations aimed to update addends. They are handled in relocateAlloc()
841
// for allocatable sections, and this function does the same for
842
// non-allocatable sections, such as sections with debug information.
843
65.5k
static void relocateNonAllocForRelocatable(InputSection *Sec, uint8_t *Buf) {
844
65.5k
  const unsigned Bits = Config->Is64 ? 
6465.5k
:
321
;
845
65.5k
846
65.5k
  for (const Relocation &Rel : Sec->Relocations) {
847
0
    // InputSection::copyRelocations() adds only R_ABS relocations.
848
0
    assert(Rel.Expr == R_ABS);
849
0
    uint8_t *BufLoc = Buf + Rel.Offset + Sec->OutSecOff;
850
0
    uint64_t TargetVA = SignExtend64(Rel.Sym->getVA(Rel.Addend), Bits);
851
0
    Target->relocateOne(BufLoc, Rel.Type, TargetVA);
852
0
  }
853
65.5k
}
854
855
template <class ELFT>
856
332k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
857
332k
  if (Flags & SHF_EXECINSTR)
858
4.27k
    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
859
332k
860
332k
  if (Flags & SHF_ALLOC) {
861
70.3k
    relocateAlloc(Buf, BufEnd);
862
70.3k
    return;
863
70.3k
  }
864
261k
865
261k
  auto *Sec = cast<InputSection>(this);
866
261k
  if (Config->Relocatable)
867
65.5k
    relocateNonAllocForRelocatable(Sec, Buf);
868
196k
  else if (Sec->AreRelocsRela)
869
26
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
870
196k
  else
871
196k
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
872
261k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, unsigned char*)
Line
Count
Source
856
512
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
857
512
  if (Flags & SHF_EXECINSTR)
858
420
    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
859
512
860
512
  if (Flags & SHF_ALLOC) {
861
477
    relocateAlloc(Buf, BufEnd);
862
477
    return;
863
477
  }
864
35
865
35
  auto *Sec = cast<InputSection>(this);
866
35
  if (Config->Relocatable)
867
1
    relocateNonAllocForRelocatable(Sec, Buf);
868
34
  else if (Sec->AreRelocsRela)
869
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
870
34
  else
871
34
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
872
35
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, unsigned char*)
Line
Count
Source
856
214
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
857
214
  if (Flags & SHF_EXECINSTR)
858
116
    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
859
214
860
214
  if (Flags & SHF_ALLOC) {
861
212
    relocateAlloc(Buf, BufEnd);
862
212
    return;
863
212
  }
864
2
865
2
  auto *Sec = cast<InputSection>(this);
866
2
  if (Config->Relocatable)
867
0
    relocateNonAllocForRelocatable(Sec, Buf);
868
2
  else if (Sec->AreRelocsRela)
869
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
870
2
  else
871
2
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
872
2
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, unsigned char*)
Line
Count
Source
856
331k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
857
331k
  if (Flags & SHF_EXECINSTR)
858
3.65k
    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
859
331k
860
331k
  if (Flags & SHF_ALLOC) {
861
69.4k
    relocateAlloc(Buf, BufEnd);
862
69.4k
    return;
863
69.4k
  }
864
261k
865
261k
  auto *Sec = cast<InputSection>(this);
866
261k
  if (Config->Relocatable)
867
65.5k
    relocateNonAllocForRelocatable(Sec, Buf);
868
196k
  else if (Sec->AreRelocsRela)
869
25
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
870
196k
  else
871
196k
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
872
261k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, unsigned char*)
Line
Count
Source
856
149
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
857
149
  if (Flags & SHF_EXECINSTR)
858
88
    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
859
149
860
149
  if (Flags & SHF_ALLOC) {
861
148
    relocateAlloc(Buf, BufEnd);
862
148
    return;
863
148
  }
864
1
865
1
  auto *Sec = cast<InputSection>(this);
866
1
  if (Config->Relocatable)
867
0
    relocateNonAllocForRelocatable(Sec, Buf);
868
1
  else if (Sec->AreRelocsRela)
869
1
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
870
0
  else
871
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
872
1
}
873
874
70.5k
void InputSectionBase::relocateAlloc(uint8_t *Buf, uint8_t *BufEnd) {
875
70.5k
  assert(Flags & SHF_ALLOC);
876
70.5k
  const unsigned Bits = Config->Wordsize * 8;
877
70.5k
878
70.5k
  for (const Relocation &Rel : Relocations) {
879
12.0k
    uint64_t Offset = Rel.Offset;
880
12.0k
    if (auto *Sec = dyn_cast<InputSection>(this))
881
11.9k
      Offset += Sec->OutSecOff;
882
12.0k
    uint8_t *BufLoc = Buf + Offset;
883
12.0k
    RelType Type = Rel.Type;
884
12.0k
885
12.0k
    uint64_t AddrLoc = getOutputSection()->Addr + Offset;
886
12.0k
    RelExpr Expr = Rel.Expr;
887
12.0k
    uint64_t TargetVA = SignExtend64(
888
12.0k
        getRelocTargetVA(File, Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr),
889
12.0k
        Bits);
890
12.0k
891
12.0k
    switch (Expr) {
892
12.0k
    case R_RELAX_GOT_PC:
893
30
    case R_RELAX_GOT_PC_NOPIC:
894
30
      Target->relaxGot(BufLoc, Type, TargetVA);
895
30
      break;
896
47
    case R_PPC64_RELAX_TOC:
897
47
      if (!tryRelaxPPC64TocIndirection(Type, Rel, BufLoc))
898
21
        Target->relocateOne(BufLoc, Type, TargetVA);
899
47
      break;
900
30
    case R_RELAX_TLS_IE_TO_LE:
901
24
      Target->relaxTlsIeToLe(BufLoc, Type, TargetVA);
902
24
      break;
903
30
    case R_RELAX_TLS_LD_TO_LE:
904
7
    case R_RELAX_TLS_LD_TO_LE_ABS:
905
7
      Target->relaxTlsLdToLe(BufLoc, Type, TargetVA);
906
7
      break;
907
12
    case R_RELAX_TLS_GD_TO_LE:
908
12
    case R_RELAX_TLS_GD_TO_LE_NEG:
909
12
      Target->relaxTlsGdToLe(BufLoc, Type, TargetVA);
910
12
      break;
911
12
    case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
912
11
    case R_RELAX_TLS_GD_TO_IE:
913
11
    case R_RELAX_TLS_GD_TO_IE_ABS:
914
11
    case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
915
11
    case R_RELAX_TLS_GD_TO_IE_GOTPLT:
916
11
      Target->relaxTlsGdToIe(BufLoc, Type, TargetVA);
917
11
      break;
918
80
    case R_PPC_CALL:
919
80
      // If this is a call to __tls_get_addr, it may be part of a TLS
920
80
      // sequence that has been relaxed and turned into a nop. In this
921
80
      // case, we don't want to handle it as a call.
922
80
      if (read32(BufLoc) == 0x60000000) // nop
923
0
        break;
924
80
925
80
      // Patch a nop (0x60000000) to a ld.
926
80
      if (Rel.Sym->NeedsTocRestore) {
927
21
        if (BufLoc + 8 > BufEnd || read32(BufLoc + 4) != 0x60000000) {
928
1
          error(getErrorLocation(BufLoc) + "call lacks nop, can't restore toc");
929
1
          break;
930
1
        }
931
20
        write32(BufLoc + 4, 0xe8410018); // ld %r2, 24(%r1)
932
20
      }
933
80
      Target->relocateOne(BufLoc, Type, TargetVA);
934
79
      break;
935
11.8k
    default:
936
11.8k
      Target->relocateOne(BufLoc, Type, TargetVA);
937
11.8k
      break;
938
12.0k
    }
939
12.0k
  }
940
70.5k
}
941
942
// For each function-defining prologue, find any calls to __morestack,
943
// and replace them with calls to __morestack_non_split.
944
static void switchMorestackCallsToMorestackNonSplit(
945
4
    DenseSet<Defined *> &Prologues, std::vector<Relocation *> &MorestackCalls) {
946
4
947
4
  // If the target adjusted a function's prologue, all calls to
948
4
  // __morestack inside that function should be switched to
949
4
  // __morestack_non_split.
950
4
  Symbol *MoreStackNonSplit = Symtab->find("__morestack_non_split");
951
4
  if (!MoreStackNonSplit) {
952
2
    error("Mixing split-stack objects requires a definition of "
953
2
          "__morestack_non_split");
954
2
    return;
955
2
  }
956
2
957
2
  // Sort both collections to compare addresses efficiently.
958
5
  
llvm::sort(MorestackCalls, [](const Relocation *L, const Relocation *R) 2
{
959
5
    return L->Offset < R->Offset;
960
5
  });
961
2
  std::vector<Defined *> Functions(Prologues.begin(), Prologues.end());
962
3
  llvm::sort(Functions, [](const Defined *L, const Defined *R) {
963
3
    return L->Value < R->Value;
964
3
  });
965
2
966
2
  auto It = MorestackCalls.begin();
967
3
  for (Defined *F : Functions) {
968
3
    // Find the first call to __morestack within the function.
969
6
    while (It != MorestackCalls.end() && (*It)->Offset < F->Value)
970
3
      ++It;
971
3
    // Adjust all calls inside the function.
972
6
    while (It != MorestackCalls.end() && 
(*It)->Offset < F->Value + F->Size5
) {
973
3
      (*It)->Sym = MoreStackNonSplit;
974
3
      ++It;
975
3
    }
976
3
  }
977
2
}
978
979
static bool enclosingPrologueAttempted(uint64_t Offset,
980
18
                                       const DenseSet<Defined *> &Prologues) {
981
18
  for (Defined *F : Prologues)
982
19
    if (F->Value <= Offset && Offset < F->Value + F->Size)
983
3
      return true;
984
18
  
return false15
;
985
18
}
986
987
// If a function compiled for split stack calls a function not
988
// compiled for split stack, then the caller needs its prologue
989
// adjusted to ensure that the called function will have enough stack
990
// available. Find those functions, and adjust their prologues.
991
template <class ELFT>
992
void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *Buf,
993
4.27k
                                                         uint8_t *End) {
994
4.27k
  if (!getFile<ELFT>()->SplitStack)
995
4.25k
    return;
996
15
  DenseSet<Defined *> Prologues;
997
15
  std::vector<Relocation *> MorestackCalls;
998
15
999
89
  for (Relocation &Rel : Relocations) {
1000
89
    // Local symbols can't possibly be cross-calls, and should have been
1001
89
    // resolved long before this line.
1002
89
    if (Rel.Sym->isLocal())
1003
1
      continue;
1004
88
1005
88
    // Ignore calls into the split-stack api.
1006
88
    if (Rel.Sym->getName().startswith("__morestack")) {
1007
21
      if (Rel.Sym->getName().equals("__morestack"))
1008
21
        MorestackCalls.push_back(&Rel);
1009
21
      continue;
1010
21
    }
1011
67
1012
67
    // A relocation to non-function isn't relevant. Sometimes
1013
67
    // __morestack is not marked as a function, so this check comes
1014
67
    // after the name check.
1015
67
    if (Rel.Sym->Type != STT_FUNC)
1016
34
      continue;
1017
33
1018
33
    // If the callee's-file was compiled with split stack, nothing to do.  In
1019
33
    // this context, a "Defined" symbol is one "defined by the binary currently
1020
33
    // being produced". So an "undefined" symbol might be provided by a shared
1021
33
    // library. It is not possible to tell how such symbols were compiled, so be
1022
33
    // conservative.
1023
33
    if (Defined *D = dyn_cast<Defined>(Rel.Sym))
1024
33
      if (InputSection *IS = cast_or_null<InputSection>(D->Section))
1025
33
        if (!IS || !IS->getFile<ELFT>() || IS->getFile<ELFT>()->SplitStack)
1026
15
          continue;
1027
18
1028
18
    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
1029
3
      continue;
1030
15
1031
15
    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
1032
15
      Prologues.insert(F);
1033
15
      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
1034
15
                                                   End, F->StOther))
1035
13
        continue;
1036
2
      if (!getFile<ELFT>()->SomeNoSplitStack)
1037
2
        error(lld::toString(this) + ": " + F->getName() +
1038
2
              " (with -fsplit-stack) calls " + Rel.Sym->getName() +
1039
2
              " (without -fsplit-stack), but couldn't adjust its prologue");
1040
2
    }
1041
15
  }
1042
15
1043
15
  if (Target->NeedsMoreStackNonSplit)
1044
4
    switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
1045
15
}
void lld::elf::InputSectionBase::adjustSplitStackFunctionPrologues<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, unsigned char*)
Line
Count
Source
993
420
                                                         uint8_t *End) {
994
420
  if (!getFile<ELFT>()->SplitStack)
995
420
    return;
996
0
  DenseSet<Defined *> Prologues;
997
0
  std::vector<Relocation *> MorestackCalls;
998
0
999
0
  for (Relocation &Rel : Relocations) {
1000
0
    // Local symbols can't possibly be cross-calls, and should have been
1001
0
    // resolved long before this line.
1002
0
    if (Rel.Sym->isLocal())
1003
0
      continue;
1004
0
1005
0
    // Ignore calls into the split-stack api.
1006
0
    if (Rel.Sym->getName().startswith("__morestack")) {
1007
0
      if (Rel.Sym->getName().equals("__morestack"))
1008
0
        MorestackCalls.push_back(&Rel);
1009
0
      continue;
1010
0
    }
1011
0
1012
0
    // A relocation to non-function isn't relevant. Sometimes
1013
0
    // __morestack is not marked as a function, so this check comes
1014
0
    // after the name check.
1015
0
    if (Rel.Sym->Type != STT_FUNC)
1016
0
      continue;
1017
0
1018
0
    // If the callee's-file was compiled with split stack, nothing to do.  In
1019
0
    // this context, a "Defined" symbol is one "defined by the binary currently
1020
0
    // being produced". So an "undefined" symbol might be provided by a shared
1021
0
    // library. It is not possible to tell how such symbols were compiled, so be
1022
0
    // conservative.
1023
0
    if (Defined *D = dyn_cast<Defined>(Rel.Sym))
1024
0
      if (InputSection *IS = cast_or_null<InputSection>(D->Section))
1025
0
        if (!IS || !IS->getFile<ELFT>() || IS->getFile<ELFT>()->SplitStack)
1026
0
          continue;
1027
0
1028
0
    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
1029
0
      continue;
1030
0
1031
0
    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
1032
0
      Prologues.insert(F);
1033
0
      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
1034
0
                                                   End, F->StOther))
1035
0
        continue;
1036
0
      if (!getFile<ELFT>()->SomeNoSplitStack)
1037
0
        error(lld::toString(this) + ": " + F->getName() +
1038
0
              " (with -fsplit-stack) calls " + Rel.Sym->getName() +
1039
0
              " (without -fsplit-stack), but couldn't adjust its prologue");
1040
0
    }
1041
0
  }
1042
0
1043
0
  if (Target->NeedsMoreStackNonSplit)
1044
0
    switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
1045
0
}
void lld::elf::InputSectionBase::adjustSplitStackFunctionPrologues<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, unsigned char*)
Line
Count
Source
993
116
                                                         uint8_t *End) {
994
116
  if (!getFile<ELFT>()->SplitStack)
995
116
    return;
996
0
  DenseSet<Defined *> Prologues;
997
0
  std::vector<Relocation *> MorestackCalls;
998
0
999
0
  for (Relocation &Rel : Relocations) {
1000
0
    // Local symbols can't possibly be cross-calls, and should have been
1001
0
    // resolved long before this line.
1002
0
    if (Rel.Sym->isLocal())
1003
0
      continue;
1004
0
1005
0
    // Ignore calls into the split-stack api.
1006
0
    if (Rel.Sym->getName().startswith("__morestack")) {
1007
0
      if (Rel.Sym->getName().equals("__morestack"))
1008
0
        MorestackCalls.push_back(&Rel);
1009
0
      continue;
1010
0
    }
1011
0
1012
0
    // A relocation to non-function isn't relevant. Sometimes
1013
0
    // __morestack is not marked as a function, so this check comes
1014
0
    // after the name check.
1015
0
    if (Rel.Sym->Type != STT_FUNC)
1016
0
      continue;
1017
0
1018
0
    // If the callee's-file was compiled with split stack, nothing to do.  In
1019
0
    // this context, a "Defined" symbol is one "defined by the binary currently
1020
0
    // being produced". So an "undefined" symbol might be provided by a shared
1021
0
    // library. It is not possible to tell how such symbols were compiled, so be
1022
0
    // conservative.
1023
0
    if (Defined *D = dyn_cast<Defined>(Rel.Sym))
1024
0
      if (InputSection *IS = cast_or_null<InputSection>(D->Section))
1025
0
        if (!IS || !IS->getFile<ELFT>() || IS->getFile<ELFT>()->SplitStack)
1026
0
          continue;
1027
0
1028
0
    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
1029
0
      continue;
1030
0
1031
0
    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
1032
0
      Prologues.insert(F);
1033
0
      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
1034
0
                                                   End, F->StOther))
1035
0
        continue;
1036
0
      if (!getFile<ELFT>()->SomeNoSplitStack)
1037
0
        error(lld::toString(this) + ": " + F->getName() +
1038
0
              " (with -fsplit-stack) calls " + Rel.Sym->getName() +
1039
0
              " (without -fsplit-stack), but couldn't adjust its prologue");
1040
0
    }
1041
0
  }
1042
0
1043
0
  if (Target->NeedsMoreStackNonSplit)
1044
0
    switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
1045
0
}
void lld::elf::InputSectionBase::adjustSplitStackFunctionPrologues<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, unsigned char*)
Line
Count
Source
993
3.64k
                                                         uint8_t *End) {
994
3.64k
  if (!getFile<ELFT>()->SplitStack)
995
3.63k
    return;
996
12
  DenseSet<Defined *> Prologues;
997
12
  std::vector<Relocation *> MorestackCalls;
998
12
999
54
  for (Relocation &Rel : Relocations) {
1000
54
    // Local symbols can't possibly be cross-calls, and should have been
1001
54
    // resolved long before this line.
1002
54
    if (Rel.Sym->isLocal())
1003
1
      continue;
1004
53
1005
53
    // Ignore calls into the split-stack api.
1006
53
    if (Rel.Sym->getName().startswith("__morestack")) {
1007
13
      if (Rel.Sym->getName().equals("__morestack"))
1008
13
        MorestackCalls.push_back(&Rel);
1009
13
      continue;
1010
13
    }
1011
40
1012
40
    // A relocation to non-function isn't relevant. Sometimes
1013
40
    // __morestack is not marked as a function, so this check comes
1014
40
    // after the name check.
1015
40
    if (Rel.Sym->Type != STT_FUNC)
1016
18
      continue;
1017
22
1018
22
    // If the callee's-file was compiled with split stack, nothing to do.  In
1019
22
    // this context, a "Defined" symbol is one "defined by the binary currently
1020
22
    // being produced". So an "undefined" symbol might be provided by a shared
1021
22
    // library. It is not possible to tell how such symbols were compiled, so be
1022
22
    // conservative.
1023
22
    if (Defined *D = dyn_cast<Defined>(Rel.Sym))
1024
22
      if (InputSection *IS = cast_or_null<InputSection>(D->Section))
1025
22
        if (!IS || !IS->getFile<ELFT>() || IS->getFile<ELFT>()->SplitStack)
1026
10
          continue;
1027
12
1028
12
    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
1029
3
      continue;
1030
9
1031
9
    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
1032
9
      Prologues.insert(F);
1033
9
      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
1034
9
                                                   End, F->StOther))
1035
8
        continue;
1036
1
      if (!getFile<ELFT>()->SomeNoSplitStack)
1037
1
        error(lld::toString(this) + ": " + F->getName() +
1038
1
              " (with -fsplit-stack) calls " + Rel.Sym->getName() +
1039
1
              " (without -fsplit-stack), but couldn't adjust its prologue");
1040
1
    }
1041
9
  }
1042
12
1043
12
  if (Target->NeedsMoreStackNonSplit)
1044
4
    switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
1045
12
}
void lld::elf::InputSectionBase::adjustSplitStackFunctionPrologues<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, unsigned char*)
Line
Count
Source
993
88
                                                         uint8_t *End) {
994
88
  if (!getFile<ELFT>()->SplitStack)
995
85
    return;
996
3
  DenseSet<Defined *> Prologues;
997
3
  std::vector<Relocation *> MorestackCalls;
998
3
999
35
  for (Relocation &Rel : Relocations) {
1000
35
    // Local symbols can't possibly be cross-calls, and should have been
1001
35
    // resolved long before this line.
1002
35
    if (Rel.Sym->isLocal())
1003
0
      continue;
1004
35
1005
35
    // Ignore calls into the split-stack api.
1006
35
    if (Rel.Sym->getName().startswith("__morestack")) {
1007
8
      if (Rel.Sym->getName().equals("__morestack"))
1008
8
        MorestackCalls.push_back(&Rel);
1009
8
      continue;
1010
8
    }
1011
27
1012
27
    // A relocation to non-function isn't relevant. Sometimes
1013
27
    // __morestack is not marked as a function, so this check comes
1014
27
    // after the name check.
1015
27
    if (Rel.Sym->Type != STT_FUNC)
1016
16
      continue;
1017
11
1018
11
    // If the callee's-file was compiled with split stack, nothing to do.  In
1019
11
    // this context, a "Defined" symbol is one "defined by the binary currently
1020
11
    // being produced". So an "undefined" symbol might be provided by a shared
1021
11
    // library. It is not possible to tell how such symbols were compiled, so be
1022
11
    // conservative.
1023
11
    if (Defined *D = dyn_cast<Defined>(Rel.Sym))
1024
11
      if (InputSection *IS = cast_or_null<InputSection>(D->Section))
1025
11
        if (!IS || !IS->getFile<ELFT>() || IS->getFile<ELFT>()->SplitStack)
1026
5
          continue;
1027
6
1028
6
    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
1029
0
      continue;
1030
6
1031
6
    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
1032
6
      Prologues.insert(F);
1033
6
      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
1034
6
                                                   End, F->StOther))
1035
5
        continue;
1036
1
      if (!getFile<ELFT>()->SomeNoSplitStack)
1037
1
        error(lld::toString(this) + ": " + F->getName() +
1038
1
              " (with -fsplit-stack) calls " + Rel.Sym->getName() +
1039
1
              " (without -fsplit-stack), but couldn't adjust its prologue");
1040
1
    }
1041
6
  }
1042
3
1043
3
  if (Target->NeedsMoreStackNonSplit)
1044
0
    switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
1045
3
}
1046
1047
341k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
1048
341k
  if (Type == SHT_NOBITS)
1049
1
    return;
1050
341k
1051
341k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
1052
8.91k
    S->writeTo(Buf + OutSecOff);
1053
8.91k
    return;
1054
8.91k
  }
1055
332k
1056
332k
  // If -r or --emit-relocs is given, then an InputSection
1057
332k
  // may be a relocation section.
1058
332k
  if (Type == SHT_RELA) {
1059
44
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
1060
44
    return;
1061
44
  }
1062
332k
  if (Type == SHT_REL) {
1063
17
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
1064
17
    return;
1065
17
  }
1066
332k
1067
332k
  // If -r is given, we may have a SHT_GROUP section.
1068
332k
  if (Type == SHT_GROUP) {
1069
7
    copyShtGroup<ELFT>(Buf + OutSecOff);
1070
7
    return;
1071
7
  }
1072
332k
1073
332k
  // If this is a compressed section, uncompress section contents directly
1074
332k
  // to the buffer.
1075
332k
  if (UncompressedSize >= 0) {
1076
2
    size_t Size = UncompressedSize;
1077
2
    if (Error E = zlib::uncompress(toStringRef(RawData),
1078
0
                                   (char *)(Buf + OutSecOff), Size))
1079
0
      fatal(toString(this) +
1080
0
            ": uncompress failed: " + llvm::toString(std::move(E)));
1081
2
    uint8_t *BufEnd = Buf + OutSecOff + Size;
1082
2
    relocate<ELFT>(Buf, BufEnd);
1083
2
    return;
1084
2
  }
1085
332k
1086
332k
  // Copy section contents from source object file to output file
1087
332k
  // and then apply relocations.
1088
332k
  memcpy(Buf + OutSecOff, data().data(), data().size());
1089
332k
  uint8_t *BufEnd = Buf + OutSecOff + data().size();
1090
332k
  relocate<ELFT>(Buf, BufEnd);
1091
332k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
1047
1.69k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
1048
1.69k
  if (Type == SHT_NOBITS)
1049
0
    return;
1050
1.69k
1051
1.69k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
1052
1.16k
    S->writeTo(Buf + OutSecOff);
1053
1.16k
    return;
1054
1.16k
  }
1055
525
1056
525
  // If -r or --emit-relocs is given, then an InputSection
1057
525
  // may be a relocation section.
1058
525
  if (Type == SHT_RELA) {
1059
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
1060
0
    return;
1061
0
  }
1062
525
  if (Type == SHT_REL) {
1063
13
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
1064
13
    return;
1065
13
  }
1066
512
1067
512
  // If -r is given, we may have a SHT_GROUP section.
1068
512
  if (Type == SHT_GROUP) {
1069
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
1070
0
    return;
1071
0
  }
1072
512
1073
512
  // If this is a compressed section, uncompress section contents directly
1074
512
  // to the buffer.
1075
512
  if (UncompressedSize >= 0) {
1076
0
    size_t Size = UncompressedSize;
1077
0
    if (Error E = zlib::uncompress(toStringRef(RawData),
1078
0
                                   (char *)(Buf + OutSecOff), Size))
1079
0
      fatal(toString(this) +
1080
0
            ": uncompress failed: " + llvm::toString(std::move(E)));
1081
0
    uint8_t *BufEnd = Buf + OutSecOff + Size;
1082
0
    relocate<ELFT>(Buf, BufEnd);
1083
0
    return;
1084
0
  }
1085
512
1086
512
  // Copy section contents from source object file to output file
1087
512
  // and then apply relocations.
1088
512
  memcpy(Buf + OutSecOff, data().data(), data().size());
1089
512
  uint8_t *BufEnd = Buf + OutSecOff + data().size();
1090
512
  relocate<ELFT>(Buf, BufEnd);
1091
512
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
1047
853
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
1048
853
  if (Type == SHT_NOBITS)
1049
0
    return;
1050
853
1051
853
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
1052
635
    S->writeTo(Buf + OutSecOff);
1053
635
    return;
1054
635
  }
1055
218
1056
218
  // If -r or --emit-relocs is given, then an InputSection
1057
218
  // may be a relocation section.
1058
218
  if (Type == SHT_RELA) {
1059
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
1060
0
    return;
1061
0
  }
1062
218
  if (Type == SHT_REL) {
1063
4
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
1064
4
    return;
1065
4
  }
1066
214
1067
214
  // If -r is given, we may have a SHT_GROUP section.
1068
214
  if (Type == SHT_GROUP) {
1069
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
1070
0
    return;
1071
0
  }
1072
214
1073
214
  // If this is a compressed section, uncompress section contents directly
1074
214
  // to the buffer.
1075
214
  if (UncompressedSize >= 0) {
1076
0
    size_t Size = UncompressedSize;
1077
0
    if (Error E = zlib::uncompress(toStringRef(RawData),
1078
0
                                   (char *)(Buf + OutSecOff), Size))
1079
0
      fatal(toString(this) +
1080
0
            ": uncompress failed: " + llvm::toString(std::move(E)));
1081
0
    uint8_t *BufEnd = Buf + OutSecOff + Size;
1082
0
    relocate<ELFT>(Buf, BufEnd);
1083
0
    return;
1084
0
  }
1085
214
1086
214
  // Copy section contents from source object file to output file
1087
214
  // and then apply relocations.
1088
214
  memcpy(Buf + OutSecOff, data().data(), data().size());
1089
214
  uint8_t *BufEnd = Buf + OutSecOff + data().size();
1090
214
  relocate<ELFT>(Buf, BufEnd);
1091
214
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
1047
337k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
1048
337k
  if (Type == SHT_NOBITS)
1049
1
    return;
1050
337k
1051
337k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
1052
6.54k
    S->writeTo(Buf + OutSecOff);
1053
6.54k
    return;
1054
6.54k
  }
1055
331k
1056
331k
  // If -r or --emit-relocs is given, then an InputSection
1057
331k
  // may be a relocation section.
1058
331k
  if (Type == SHT_RELA) {
1059
44
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
1060
44
    return;
1061
44
  }
1062
331k
  if (Type == SHT_REL) {
1063
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
1064
0
    return;
1065
0
  }
1066
331k
1067
331k
  // If -r is given, we may have a SHT_GROUP section.
1068
331k
  if (Type == SHT_GROUP) {
1069
7
    copyShtGroup<ELFT>(Buf + OutSecOff);
1070
7
    return;
1071
7
  }
1072
331k
1073
331k
  // If this is a compressed section, uncompress section contents directly
1074
331k
  // to the buffer.
1075
331k
  if (UncompressedSize >= 0) {
1076
2
    size_t Size = UncompressedSize;
1077
2
    if (Error E = zlib::uncompress(toStringRef(RawData),
1078
0
                                   (char *)(Buf + OutSecOff), Size))
1079
0
      fatal(toString(this) +
1080
0
            ": uncompress failed: " + llvm::toString(std::move(E)));
1081
2
    uint8_t *BufEnd = Buf + OutSecOff + Size;
1082
2
    relocate<ELFT>(Buf, BufEnd);
1083
2
    return;
1084
2
  }
1085
331k
1086
331k
  // Copy section contents from source object file to output file
1087
331k
  // and then apply relocations.
1088
331k
  memcpy(Buf + OutSecOff, data().data(), data().size());
1089
331k
  uint8_t *BufEnd = Buf + OutSecOff + data().size();
1090
331k
  relocate<ELFT>(Buf, BufEnd);
1091
331k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
1047
718
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
1048
718
  if (Type == SHT_NOBITS)
1049
0
    return;
1050
718
1051
718
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
1052
569
    S->writeTo(Buf + OutSecOff);
1053
569
    return;
1054
569
  }
1055
149
1056
149
  // If -r or --emit-relocs is given, then an InputSection
1057
149
  // may be a relocation section.
1058
149
  if (Type == SHT_RELA) {
1059
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
1060
0
    return;
1061
0
  }
1062
149
  if (Type == SHT_REL) {
1063
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
1064
0
    return;
1065
0
  }
1066
149
1067
149
  // If -r is given, we may have a SHT_GROUP section.
1068
149
  if (Type == SHT_GROUP) {
1069
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
1070
0
    return;
1071
0
  }
1072
149
1073
149
  // If this is a compressed section, uncompress section contents directly
1074
149
  // to the buffer.
1075
149
  if (UncompressedSize >= 0) {
1076
0
    size_t Size = UncompressedSize;
1077
0
    if (Error E = zlib::uncompress(toStringRef(RawData),
1078
0
                                   (char *)(Buf + OutSecOff), Size))
1079
0
      fatal(toString(this) +
1080
0
            ": uncompress failed: " + llvm::toString(std::move(E)));
1081
0
    uint8_t *BufEnd = Buf + OutSecOff + Size;
1082
0
    relocate<ELFT>(Buf, BufEnd);
1083
0
    return;
1084
0
  }
1085
149
1086
149
  // Copy section contents from source object file to output file
1087
149
  // and then apply relocations.
1088
149
  memcpy(Buf + OutSecOff, data().data(), data().size());
1089
149
  uint8_t *BufEnd = Buf + OutSecOff + data().size();
1090
149
  relocate<ELFT>(Buf, BufEnd);
1091
149
}
1092
1093
41
void InputSection::replace(InputSection *Other) {
1094
41
  Alignment = std::max(Alignment, Other->Alignment);
1095
41
  Other->Repl = Repl;
1096
41
  Other->Live = false;
1097
41
}
1098
1099
template <class ELFT>
1100
EhInputSection::EhInputSection(ObjFile<ELFT> &F,
1101
                               const typename ELFT::Shdr &Header,
1102
                               StringRef Name)
1103
60
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {}
lld::elf::EhInputSection::EhInputSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const&, llvm::StringRef)
Line
Count
Source
1103
1
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {}
Unexecuted instantiation: lld::elf::EhInputSection::EhInputSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const&, llvm::StringRef)
lld::elf::EhInputSection::EhInputSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const&, llvm::StringRef)
Line
Count
Source
1103
57
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {}
lld::elf::EhInputSection::EhInputSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const&, llvm::StringRef)
Line
Count
Source
1103
2
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {}
1104
1105
77
SyntheticSection *EhInputSection::getParent() const {
1106
77
  return cast_or_null<SyntheticSection>(Parent);
1107
77
}
1108
1109
// Returns the index of the first relocation that points to a region between
1110
// Begin and Begin+Size.
1111
template <class IntTy, class RelTy>
1112
static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels,
1113
127
                         unsigned &RelocI) {
1114
127
  // Start search from RelocI for fast access. That works because the
1115
127
  // relocations are sorted in .eh_frame.
1116
151
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI24
) {
1117
141
    const RelTy &Rel = Rels[RelocI];
1118
141
    if (Rel.r_offset < Begin)
1119
24
      continue;
1120
117
1121
117
    if (Rel.r_offset < Begin + Size)
1122
73
      return RelocI;
1123
44
    return -1;
1124
44
  }
1125
127
  
return -110
;
1126
127
}
Unexecuted instantiation: InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> > const&, unsigned int&)
InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> > const&, unsigned int&)
Line
Count
Source
1113
2
                         unsigned &RelocI) {
1114
2
  // Start search from RelocI for fast access. That works because the
1115
2
  // relocations are sorted in .eh_frame.
1116
3
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI1
) {
1117
3
    const RelTy &Rel = Rels[RelocI];
1118
3
    if (Rel.r_offset < Begin)
1119
1
      continue;
1120
2
1121
2
    if (Rel.r_offset < Begin + Size)
1122
2
      return RelocI;
1123
0
    return -1;
1124
0
  }
1125
2
  
return -10
;
1126
2
}
Unexecuted instantiation: InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> > const&, unsigned int&)
Unexecuted instantiation: InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> > const&, unsigned int&)
InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> > const&, unsigned int&)
Line
Count
Source
1113
113
                         unsigned &RelocI) {
1114
113
  // Start search from RelocI for fast access. That works because the
1115
113
  // relocations are sorted in .eh_frame.
1116
135
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI22
) {
1117
135
    const RelTy &Rel = Rels[RelocI];
1118
135
    if (Rel.r_offset < Begin)
1119
22
      continue;
1120
113
1121
113
    if (Rel.r_offset < Begin + Size)
1122
69
      return RelocI;
1123
44
    return -1;
1124
44
  }
1125
113
  
return -10
;
1126
113
}
InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> > const&, unsigned int&)
Line
Count
Source
1113
9
                         unsigned &RelocI) {
1114
9
  // Start search from RelocI for fast access. That works because the
1115
9
  // relocations are sorted in .eh_frame.
1116
9
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
1117
0
    const RelTy &Rel = Rels[RelocI];
1118
0
    if (Rel.r_offset < Begin)
1119
0
      continue;
1120
0
1121
0
    if (Rel.r_offset < Begin + Size)
1122
0
      return RelocI;
1123
0
    return -1;
1124
0
  }
1125
9
  return -1;
1126
9
}
InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> > const&, unsigned int&)
Line
Count
Source
1113
2
                         unsigned &RelocI) {
1114
2
  // Start search from RelocI for fast access. That works because the
1115
2
  // relocations are sorted in .eh_frame.
1116
3
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI1
) {
1117
3
    const RelTy &Rel = Rels[RelocI];
1118
3
    if (Rel.r_offset < Begin)
1119
1
      continue;
1120
2
1121
2
    if (Rel.r_offset < Begin + Size)
1122
2
      return RelocI;
1123
0
    return -1;
1124
0
  }
1125
2
  
return -10
;
1126
2
}
InputSection.cpp:unsigned int getReloc<unsigned long, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(unsigned long, unsigned long, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> > const&, unsigned int&)
Line
Count
Source
1113
1
                         unsigned &RelocI) {
1114
1
  // Start search from RelocI for fast access. That works because the
1115
1
  // relocations are sorted in .eh_frame.
1116
1
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
1117
0
    const RelTy &Rel = Rels[RelocI];
1118
0
    if (Rel.r_offset < Begin)
1119
0
      continue;
1120
0
1121
0
    if (Rel.r_offset < Begin + Size)
1122
0
      return RelocI;
1123
0
    return -1;
1124
0
  }
1125
1
  return -1;
1126
1
}
1127
1128
// .eh_frame is a sequence of CIE or FDE records.
1129
// This function splits an input section into records and returns them.
1130
60
template <class ELFT> void EhInputSection::split() {
1131
60
  if (AreRelocsRela)
1132
48
    split<ELFT>(relas<ELFT>());
1133
12
  else
1134
12
    split<ELFT>(rels<ELFT>());
1135
60
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
1130
1
template <class ELFT> void EhInputSection::split() {
1131
1
  if (AreRelocsRela)
1132
0
    split<ELFT>(relas<ELFT>());
1133
1
  else
1134
1
    split<ELFT>(rels<ELFT>());
1135
1
}
Unexecuted instantiation: void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1130
57
template <class ELFT> void EhInputSection::split() {
1131
57
  if (AreRelocsRela)
1132
47
    split<ELFT>(relas<ELFT>());
1133
10
  else
1134
10
    split<ELFT>(rels<ELFT>());
1135
57
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
1130
2
template <class ELFT> void EhInputSection::split() {
1131
2
  if (AreRelocsRela)
1132
1
    split<ELFT>(relas<ELFT>());
1133
1
  else
1134
1
    split<ELFT>(rels<ELFT>());
1135
2
}
1136
1137
template <class ELFT, class RelTy>
1138
60
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
60
  unsigned RelI = 0;
1140
183
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
127
    size_t Size = readEhRecordSize(this, Off);
1142
127
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
127
    // The empty record is the end marker.
1144
127
    if (Size == 4)
1145
4
      break;
1146
123
    Off += Size;
1147
123
  }
1148
60
}
Unexecuted instantiation: void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
1138
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
1
  unsigned RelI = 0;
1140
3
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
2
    size_t Size = readEhRecordSize(this, Off);
1142
2
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
2
    // The empty record is the end marker.
1144
2
    if (Size == 4)
1145
0
      break;
1146
2
    Off += Size;
1147
2
  }
1148
1
}
Unexecuted instantiation: void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
Unexecuted instantiation: void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
1138
47
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
47
  unsigned RelI = 0;
1140
160
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
113
    size_t Size = readEhRecordSize(this, Off);
1142
113
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
113
    // The empty record is the end marker.
1144
113
    if (Size == 4)
1145
0
      break;
1146
113
    Off += Size;
1147
113
  }
1148
47
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
1138
10
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
10
  unsigned RelI = 0;
1140
15
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
9
    size_t Size = readEhRecordSize(this, Off);
1142
9
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
9
    // The empty record is the end marker.
1144
9
    if (Size == 4)
1145
4
      break;
1146
5
    Off += Size;
1147
5
  }
1148
10
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
1138
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
1
  unsigned RelI = 0;
1140
3
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
2
    size_t Size = readEhRecordSize(this, Off);
1142
2
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
2
    // The empty record is the end marker.
1144
2
    if (Size == 4)
1145
0
      break;
1146
2
    Off += Size;
1147
2
  }
1148
1
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
Line
Count
Source
1138
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
1139
1
  unsigned RelI = 0;
1140
2
  for (size_t Off = 0, End = data().size(); Off != End;) {
1141
1
    size_t Size = readEhRecordSize(this, Off);
1142
1
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
1143
1
    // The empty record is the end marker.
1144
1
    if (Size == 4)
1145
0
      break;
1146
1
    Off += Size;
1147
1
  }
1148
1
}
1149
1150
1.29k
static size_t findNull(StringRef S, size_t EntSize) {
1151
1.29k
  // Optimize the common case.
1152
1.29k
  if (EntSize == 1)
1153
1.29k
    return S.find(0);
1154
4
1155
5
  
for (unsigned I = 0, N = S.size(); 4
I != N;
I += EntSize1
) {
1156
5
    const char *B = S.begin() + I;
1157
27
    if (
std::all_of(B, B + EntSize, [](char C) 5
{ return C == 0; }))
1158
4
      return I;
1159
5
  }
1160
4
  
return StringRef::npos0
;
1161
4
}
1162
1163
124
SyntheticSection *MergeInputSection::getParent() const {
1164
124
  return cast_or_null<SyntheticSection>(Parent);
1165
124
}
1166
1167
// Split SHF_STRINGS section. Such section is a sequence of
1168
// null-terminated strings.
1169
1.27k
void MergeInputSection::splitStrings(ArrayRef<uint8_t> Data, size_t EntSize) {
1170
1.27k
  size_t Off = 0;
1171
1.27k
  bool IsAlloc = Flags & SHF_ALLOC;
1172
1.27k
  StringRef S = toStringRef(Data);
1173
1.27k
1174
2.56k
  while (!S.empty()) {
1175
1.29k
    size_t End = findNull(S, EntSize);
1176
1.29k
    if (End == StringRef::npos)
1177
0
      fatal(toString(this) + ": string is not null terminated");
1178
1.29k
    size_t Size = End + EntSize;
1179
1.29k
1180
1.29k
    Pieces.emplace_back(Off, xxHash64(S.substr(0, Size)), !IsAlloc);
1181
1.29k
    S = S.substr(Size);
1182
1.29k
    Off += Size;
1183
1.29k
  }
1184
1.27k
}
1185
1186
// Split non-SHF_STRINGS section. Such section is a sequence of
1187
// fixed size records.
1188
void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> Data,
1189
18
                                        size_t EntSize) {
1190
18
  size_t Size = Data.size();
1191
18
  assert((Size % EntSize) == 0);
1192
18
  bool IsAlloc = Flags & SHF_ALLOC;
1193
18
1194
48
  for (size_t I = 0; I != Size; 
I += EntSize30
)
1195
30
    Pieces.emplace_back(I, xxHash64(Data.slice(I, EntSize)), !IsAlloc);
1196
18
}
1197
1198
template <class ELFT>
1199
MergeInputSection::MergeInputSection(ObjFile<ELFT> &F,
1200
                                     const typename ELFT::Shdr &Header,
1201
                                     StringRef Name)
1202
59
    : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {}
lld::elf::MergeInputSection::MergeInputSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const&, llvm::StringRef)
Line
Count
Source
1202
3
    : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {}
Unexecuted instantiation: lld::elf::MergeInputSection::MergeInputSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const&, llvm::StringRef)
lld::elf::MergeInputSection::MergeInputSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const&, llvm::StringRef)
Line
Count
Source
1202
56
    : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {}
Unexecuted instantiation: lld::elf::MergeInputSection::MergeInputSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const&, llvm::StringRef)
1203
1204
MergeInputSection::MergeInputSection(uint64_t Flags, uint32_t Type,
1205
                                     uint64_t Entsize, ArrayRef<uint8_t> Data,
1206
                                     StringRef Name)
1207
    : InputSectionBase(nullptr, Flags, Type, Entsize, /*Link*/ 0, /*Info*/ 0,
1208
1.23k
                       /*Alignment*/ Entsize, Data, Name, SectionBase::Merge) {}
1209
1210
// This function is called after we obtain a complete list of input sections
1211
// that need to be linked. This is responsible to split section contents
1212
// into small chunks for further processing.
1213
//
1214
// Note that this function is called from parallelForEach. This must be
1215
// thread-safe (i.e. no memory allocation from the pools).
1216
1.28k
void MergeInputSection::splitIntoPieces() {
1217
1.28k
  assert(Pieces.empty());
1218
1.28k
1219
1.28k
  if (Flags & SHF_STRINGS)
1220
1.27k
    splitStrings(data(), Entsize);
1221
18
  else
1222
18
    splitNonStrings(data(), Entsize);
1223
1.28k
}
1224
1225
93
SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
1226
93
  if (this->data().size() <= Offset)
1227
0
    fatal(toString(this) + ": offset is outside the section");
1228
93
1229
93
  // If Offset is not at beginning of a section piece, it is not in the map.
1230
93
  // In that case we need to  do a binary search of the original section piece vector.
1231
93
  auto It = llvm::bsearch(Pieces,
1232
159
                          [=](SectionPiece P) { return Offset < P.InputOff; });
1233
93
  return &It[-1];
1234
93
}
1235
1236
// Returns the offset in an output section for a given input offset.
1237
// Because contents of a mergeable section is not contiguous in output,
1238
// it is not just an addition to a base output offset.
1239
52
uint64_t MergeInputSection::getParentOffset(uint64_t Offset) const {
1240
52
  // If Offset is not at beginning of a section piece, it is not in the map.
1241
52
  // In that case we need to search from the original section piece vector.
1242
52
  const SectionPiece &Piece =
1243
52
      *(const_cast<MergeInputSection *>(this)->getSectionPiece (Offset));
1244
52
  uint64_t Addend = Offset - Piece.InputOff;
1245
52
  return Piece.OutputOff + Addend;
1246
52
}
1247
1248
template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,
1249
                                    StringRef);
1250
template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &,
1251
                                    StringRef);
1252
template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,
1253
                                    StringRef);
1254
template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,
1255
                                    StringRef);
1256
1257
template std::string InputSectionBase::getLocation<ELF32LE>(uint64_t);
1258
template std::string InputSectionBase::getLocation<ELF32BE>(uint64_t);
1259
template std::string InputSectionBase::getLocation<ELF64LE>(uint64_t);
1260
template std::string InputSectionBase::getLocation<ELF64BE>(uint64_t);
1261
1262
template void InputSection::writeTo<ELF32LE>(uint8_t *);
1263
template void InputSection::writeTo<ELF32BE>(uint8_t *);
1264
template void InputSection::writeTo<ELF64LE>(uint8_t *);
1265
template void InputSection::writeTo<ELF64BE>(uint8_t *);
1266
1267
template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &,
1268
                                              const ELF32LE::Shdr &, StringRef);
1269
template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &,
1270
                                              const ELF32BE::Shdr &, StringRef);
1271
template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &,
1272
                                              const ELF64LE::Shdr &, StringRef);
1273
template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &,
1274
                                              const ELF64BE::Shdr &, StringRef);
1275
1276
template EhInputSection::EhInputSection(ObjFile<ELF32LE> &,
1277
                                        const ELF32LE::Shdr &, StringRef);
1278
template EhInputSection::EhInputSection(ObjFile<ELF32BE> &,
1279
                                        const ELF32BE::Shdr &, StringRef);
1280
template EhInputSection::EhInputSection(ObjFile<ELF64LE> &,
1281
                                        const ELF64LE::Shdr &, StringRef);
1282
template EhInputSection::EhInputSection(ObjFile<ELF64BE> &,
1283
                                        const ELF64BE::Shdr &, StringRef);
1284
1285
template void EhInputSection::split<ELF32LE>();
1286
template void EhInputSection::split<ELF32BE>();
1287
template void EhInputSection::split<ELF64LE>();
1288
template void EhInputSection::split<ELF64BE>();