Coverage Report

Created: 2018-08-19 21:11

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