Coverage Report

Created: 2019-01-21 03:01

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