Coverage Report

Created: 2018-10-23 15:26

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