Coverage Report

Created: 2018-01-17 17:22

/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 "Symbols.h"
18
#include "SyntheticSections.h"
19
#include "Target.h"
20
#include "Thunks.h"
21
#include "lld/Common/ErrorHandler.h"
22
#include "lld/Common/Memory.h"
23
#include "llvm/Object/Decompressor.h"
24
#include "llvm/Support/Compiler.h"
25
#include "llvm/Support/Compression.h"
26
#include "llvm/Support/Endian.h"
27
#include "llvm/Support/Threading.h"
28
#include "llvm/Support/xxhash.h"
29
#include <mutex>
30
31
using namespace llvm;
32
using namespace llvm::ELF;
33
using namespace llvm::object;
34
using namespace llvm::support;
35
using namespace llvm::support::endian;
36
using namespace llvm::sys;
37
38
using namespace lld;
39
using namespace lld::elf;
40
41
std::vector<InputSectionBase *> elf::InputSections;
42
43
// Returns a string to construct an error message.
44
142
std::string lld::toString(const InputSectionBase *Sec) {
45
142
  return (toString(Sec->File) + ":(" + Sec->Name + ")").str();
46
142
}
47
48
3.12k
DenseMap<SectionBase *, int> elf::buildSectionOrder() {
49
3.12k
  DenseMap<SectionBase *, int> SectionOrder;
50
3.12k
  if (Config->SymbolOrderingFile.empty())
51
3.10k
    return SectionOrder;
52
11
53
11
  // Build a map from symbols to their priorities. Symbols that didn't
54
11
  // appear in the symbol ordering file have the lowest priority 0.
55
11
  // All explicitly mentioned symbols have negative (higher) priorities.
56
11
  DenseMap<StringRef, int> SymbolOrder;
57
11
  int Priority = -Config->SymbolOrderingFile.size();
58
11
  for (StringRef S : Config->SymbolOrderingFile)
59
32
    SymbolOrder.insert({S, Priority++});
60
11
61
11
  // Build a map from sections to their priorities.
62
15
  for (InputFile *File : ObjectFiles) {
63
63
    for (Symbol *Sym : File->getSymbols()) {
64
63
      auto *D = dyn_cast<Defined>(Sym);
65
63
      if (!D || 
!D->Section48
)
66
19
        continue;
67
44
      int &Priority = SectionOrder[D->Section];
68
44
      Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
69
44
    }
70
15
  }
71
11
  return SectionOrder;
72
11
}
73
74
template <class ELFT>
75
static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &File,
76
201k
                                            const typename ELFT::Shdr &Hdr) {
77
201k
  if (Hdr.sh_type == SHT_NOBITS)
78
335
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
79
201k
  return check(File.getObj().getSectionContents(&Hdr));
80
201k
}
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
76
962
                                            const typename ELFT::Shdr &Hdr) {
77
962
  if (Hdr.sh_type == SHT_NOBITS)
78
44
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
79
918
  return check(File.getObj().getSectionContents(&Hdr));
80
918
}
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
76
745
                                            const typename ELFT::Shdr &Hdr) {
77
745
  if (Hdr.sh_type == SHT_NOBITS)
78
136
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
79
609
  return check(File.getObj().getSectionContents(&Hdr));
80
609
}
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
76
199k
                                            const typename ELFT::Shdr &Hdr) {
77
199k
  if (Hdr.sh_type == SHT_NOBITS)
78
116
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
79
199k
  return check(File.getObj().getSectionContents(&Hdr));
80
199k
}
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
76
266
                                            const typename ELFT::Shdr &Hdr) {
77
266
  if (Hdr.sh_type == SHT_NOBITS)
78
39
    return makeArrayRef<uint8_t>(nullptr, Hdr.sh_size);
79
227
  return check(File.getObj().getSectionContents(&Hdr));
80
227
}
81
82
InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags,
83
                                   uint32_t Type, uint64_t Entsize,
84
                                   uint32_t Link, uint32_t Info,
85
                                   uint32_t Alignment, ArrayRef<uint8_t> Data,
86
                                   StringRef Name, Kind SectionKind)
87
    : SectionBase(SectionKind, Name, Flags, Entsize, Alignment, Type, Info,
88
                  Link),
89
241k
      File(File), Data(Data) {
90
241k
  // In order to reduce memory allocation, we assume that mergeable
91
241k
  // sections are smaller than 4 GiB, which is not an unreasonable
92
241k
  // assumption as of 2017.
93
241k
  if (SectionKind == SectionBase::Merge && 
Data.size() > UINT32_MAX1.82k
)
94
241k
    
error(toString(this) + ": section too large")0
;
95
241k
96
241k
  NumRelocations = 0;
97
241k
  AreRelocsRela = false;
98
241k
99
241k
  // The ELF spec states that a value of 0 means the section has
100
241k
  // no alignment constraits.
101
241k
  uint32_t V = std::max<uint64_t>(Alignment, 1);
102
241k
  if (!isPowerOf2_64(V))
103
0
    fatal(toString(File) + ": section sh_addralign is not a power of 2");
104
241k
  this->Alignment = V;
105
241k
}
106
107
// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
108
// SHF_GROUP is a marker that a section belongs to some comdat group.
109
// That flag doesn't make sense in an executable.
110
201k
static uint64_t getFlags(uint64_t Flags) {
111
201k
  Flags &= ~(uint64_t)SHF_INFO_LINK;
112
201k
  if (!Config->Relocatable)
113
201k
    Flags &= ~(uint64_t)SHF_GROUP;
114
201k
  return Flags;
115
201k
}
116
117
// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
118
// March 2017) fail to infer section types for sections starting with
119
// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of
120
// SHF_INIT_ARRAY. As a result, the following assembler directive
121
// creates ".init_array.100" with SHT_PROGBITS, for example.
122
//
123
//   .section .init_array.100, "aw"
124
//
125
// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
126
// incorrect inputs as if they were correct from the beginning.
127
201k
static uint64_t getType(uint64_t Type, StringRef Name) {
128
201k
  if (Type == SHT_PROGBITS && 
Name.startswith(".init_array.")200k
)
129
1
    return SHT_INIT_ARRAY;
130
201k
  if (Type == SHT_PROGBITS && 
Name.startswith(".fini_array.")200k
)
131
1
    return SHT_FINI_ARRAY;
132
201k
  return Type;
133
201k
}
134
135
template <class ELFT>
136
InputSectionBase::InputSectionBase(ObjFile<ELFT> &File,
137
                                   const typename ELFT::Shdr &Hdr,
138
                                   StringRef Name, Kind SectionKind)
139
    : InputSectionBase(&File, getFlags(Hdr.sh_flags),
140
                       getType(Hdr.sh_type, Name), Hdr.sh_entsize, Hdr.sh_link,
141
                       Hdr.sh_info, Hdr.sh_addralign,
142
201k
                       getSectionContents(File, Hdr), Name, SectionKind) {
143
201k
  // We reject object files having insanely large alignments even though
144
201k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
145
201k
  // We might want to relax this in the future.
146
201k
  if (Hdr.sh_addralign > UINT32_MAX)
147
201k
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
148
201k
}
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
142
962
                       getSectionContents(File, Hdr), Name, SectionKind) {
143
962
  // We reject object files having insanely large alignments even though
144
962
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
145
962
  // We might want to relax this in the future.
146
962
  if (Hdr.sh_addralign > UINT32_MAX)
147
962
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
148
962
}
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
142
745
                       getSectionContents(File, Hdr), Name, SectionKind) {
143
745
  // We reject object files having insanely large alignments even though
144
745
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
145
745
  // We might want to relax this in the future.
146
745
  if (Hdr.sh_addralign > UINT32_MAX)
147
745
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
148
745
}
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
142
199k
                       getSectionContents(File, Hdr), Name, SectionKind) {
143
199k
  // We reject object files having insanely large alignments even though
144
199k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
145
199k
  // We might want to relax this in the future.
146
199k
  if (Hdr.sh_addralign > UINT32_MAX)
147
199k
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
148
199k
}
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
142
266
                       getSectionContents(File, Hdr), Name, SectionKind) {
143
266
  // We reject object files having insanely large alignments even though
144
266
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
145
266
  // We might want to relax this in the future.
146
266
  if (Hdr.sh_addralign > UINT32_MAX)
147
266
    
fatal(toString(&File) + ": section sh_addralign is too large")0
;
148
266
}
149
150
443k
size_t InputSectionBase::getSize() const {
151
443k
  if (auto *S = dyn_cast<SyntheticSection>(this))
152
32.2k
    return S->getSize();
153
410k
154
410k
  return Data.size();
155
410k
}
156
157
2.03k
uint64_t InputSectionBase::getOffsetInFile() const {
158
2.03k
  const uint8_t *FileStart = (const uint8_t *)File->MB.getBufferStart();
159
2.03k
  const uint8_t *SecStart = Data.begin();
160
2.03k
  return SecStart - FileStart;
161
2.03k
}
162
163
53.8k
uint64_t SectionBase::getOffset(uint64_t Offset) const {
164
53.8k
  switch (kind()) {
165
53.8k
  case Output: {
166
10.8k
    auto *OS = cast<OutputSection>(this);
167
10.8k
    // For output sections we treat offset -1 as the end of the section.
168
10.8k
    return Offset == uint64_t(-1) ? 
OS->Size57
:
Offset10.8k
;
169
53.8k
  }
170
53.8k
  case Regular:
171
40.2k
    return cast<InputSection>(this)->OutSecOff + Offset;
172
53.8k
  case Synthetic: {
173
2.51k
    auto *IS = cast<InputSection>(this);
174
2.51k
    // For synthetic sections we treat offset -1 as the end of the section.
175
2.51k
    return IS->OutSecOff + (Offset == uint64_t(-1) ? 
IS->getSize()25
:
Offset2.48k
);
176
53.8k
  }
177
53.8k
  case EHFrame:
178
122
    // The file crtbeginT.o has relocations pointing to the start of an empty
179
122
    // .eh_frame that is known to be the first in the link. It does that to
180
122
    // identify the start of the output .eh_frame.
181
122
    return Offset;
182
53.8k
  case Merge:
183
83
    const MergeInputSection *MS = cast<MergeInputSection>(this);
184
83
    if (InputSection *IS = MS->getParent())
185
82
      return IS->OutSecOff + MS->getOffset(Offset);
186
1
    return MS->getOffset(Offset);
187
0
  }
188
0
  llvm_unreachable("invalid section kind");
189
0
}
190
191
71.0k
OutputSection *SectionBase::getOutputSection() {
192
71.0k
  InputSection *Sec;
193
71.0k
  if (auto *IS = dyn_cast<InputSection>(this))
194
59.6k
    return IS->getParent();
195
11.4k
  else if (auto *MS = dyn_cast<MergeInputSection>(this))
196
115
    Sec = MS->getParent();
197
11.2k
  else if (auto *EH = dyn_cast<EhInputSection>(this))
198
124
    Sec = EH->getParent();
199
11.1k
  else
200
11.1k
    return cast<OutputSection>(this);
201
239
  return Sec ? 
Sec->getParent()238
:
nullptr1
;
202
239
}
203
204
// Uncompress section contents if required. Note that this function
205
// is called from parallelForEach, so it must be thread-safe.
206
197k
void InputSectionBase::maybeUncompress() {
207
199k
  if (
UncompressBuf197k
|| !Decompressor::isCompressedELFSection(Flags, Name))
208
199k
    return;
209
18.4E
210
18.4E
  Decompressor Dec = check(Decompressor::create(Name, toStringRef(Data),
211
18.4E
                                                Config->IsLE, Config->Is64));
212
18.4E
213
18.4E
  size_t Size = Dec.getDecompressedSize();
214
18.4E
  UncompressBuf.reset(new char[Size]());
215
18.4E
  if (Error E = Dec.decompress({UncompressBuf.get(), Size}))
216
0
    fatal(toString(this) +
217
0
          ": decompress failed: " + llvm::toString(std::move(E)));
218
18.4E
219
18.4E
  Data = makeArrayRef((uint8_t *)UncompressBuf.get(), Size);
220
18.4E
  Flags &= ~(uint64_t)SHF_COMPRESSED;
221
18.4E
}
222
223
149
InputSection *InputSectionBase::getLinkOrderDep() const {
224
149
  if ((Flags & SHF_LINK_ORDER) && Link != 0) {
225
149
    InputSectionBase *L = File->getSections()[Link];
226
149
    if (auto *IS = dyn_cast<InputSection>(L))
227
148
      return IS;
228
1
    error("a section with SHF_LINK_ORDER should not refer a non-regular "
229
1
          "section: " +
230
1
          toString(L));
231
1
  }
232
149
  
return nullptr1
;
233
149
}
234
235
// Returns a source location string. Used to construct an error message.
236
template <class ELFT>
237
1.84k
std::string InputSectionBase::getLocation(uint64_t Offset) {
238
1.84k
  // We don't have file for synthetic sections.
239
1.84k
  if (getFile<ELFT>() == nullptr)
240
1
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
241
1
        .str();
242
1.84k
243
1.84k
  // First check if we can get desired values from debugging information.
244
1.84k
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
245
1.84k
  if (!LineInfo.empty())
246
0
    return LineInfo;
247
1.84k
248
1.84k
  // File->SourceFile contains STT_FILE symbol that contains a
249
1.84k
  // source file name. If it's missing, we use an object file name.
250
1.84k
  std::string SrcFile = getFile<ELFT>()->SourceFile;
251
1.84k
  if (SrcFile.empty())
252
1.84k
    SrcFile = toString(File);
253
1.84k
254
1.84k
  // Find a function symbol that encloses a given location.
255
1.84k
  for (Symbol *B : File->getSymbols())
256
18.1M
    if (auto *D = dyn_cast<Defined>(B))
257
18.1M
      if (D->Section == this && 
D->Type == STT_FUNC18.1M
)
258
4
        if (D->Value <= Offset && Offset < D->Value + D->Size)
259
0
          return SrcFile + ":(function " + toString(*D) + ")";
260
1.84k
261
1.84k
  // If there's no symbol, print out the offset in the section.
262
1.84k
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
263
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
237
4
std::string InputSectionBase::getLocation(uint64_t Offset) {
238
4
  // We don't have file for synthetic sections.
239
4
  if (getFile<ELFT>() == nullptr)
240
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
241
0
        .str();
242
4
243
4
  // First check if we can get desired values from debugging information.
244
4
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
245
4
  if (!LineInfo.empty())
246
0
    return LineInfo;
247
4
248
4
  // File->SourceFile contains STT_FILE symbol that contains a
249
4
  // source file name. If it's missing, we use an object file name.
250
4
  std::string SrcFile = getFile<ELFT>()->SourceFile;
251
4
  if (SrcFile.empty())
252
4
    SrcFile = toString(File);
253
4
254
4
  // Find a function symbol that encloses a given location.
255
4
  for (Symbol *B : File->getSymbols())
256
9
    if (auto *D = dyn_cast<Defined>(B))
257
5
      if (D->Section == this && 
D->Type == STT_FUNC1
)
258
0
        if (D->Value <= Offset && Offset < D->Value + D->Size)
259
0
          return SrcFile + ":(function " + toString(*D) + ")";
260
4
261
4
  // If there's no symbol, print out the offset in the section.
262
4
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
263
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
237
1
std::string InputSectionBase::getLocation(uint64_t Offset) {
238
1
  // We don't have file for synthetic sections.
239
1
  if (getFile<ELFT>() == nullptr)
240
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
241
0
        .str();
242
1
243
1
  // First check if we can get desired values from debugging information.
244
1
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
245
1
  if (!LineInfo.empty())
246
0
    return LineInfo;
247
1
248
1
  // File->SourceFile contains STT_FILE symbol that contains a
249
1
  // source file name. If it's missing, we use an object file name.
250
1
  std::string SrcFile = getFile<ELFT>()->SourceFile;
251
1
  if (SrcFile.empty())
252
1
    SrcFile = toString(File);
253
1
254
1
  // Find a function symbol that encloses a given location.
255
1
  for (Symbol *B : File->getSymbols())
256
3
    if (auto *D = dyn_cast<Defined>(B))
257
2
      if (D->Section == this && 
D->Type == STT_FUNC1
)
258
0
        if (D->Value <= Offset && Offset < D->Value + D->Size)
259
0
          return SrcFile + ":(function " + toString(*D) + ")";
260
1
261
1
  // If there's no symbol, print out the offset in the section.
262
1
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
263
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
237
26
std::string InputSectionBase::getLocation(uint64_t Offset) {
238
26
  // We don't have file for synthetic sections.
239
26
  if (getFile<ELFT>() == nullptr)
240
1
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
241
1
        .str();
242
25
243
25
  // First check if we can get desired values from debugging information.
244
25
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
245
25
  if (!LineInfo.empty())
246
0
    return LineInfo;
247
25
248
25
  // File->SourceFile contains STT_FILE symbol that contains a
249
25
  // source file name. If it's missing, we use an object file name.
250
25
  std::string SrcFile = getFile<ELFT>()->SourceFile;
251
25
  if (SrcFile.empty())
252
25
    SrcFile = toString(File);
253
25
254
25
  // Find a function symbol that encloses a given location.
255
25
  for (Symbol *B : File->getSymbols())
256
103
    if (auto *D = dyn_cast<Defined>(B))
257
77
      if (D->Section == this && 
D->Type == STT_FUNC25
)
258
4
        if (D->Value <= Offset && Offset < D->Value + D->Size)
259
0
          return SrcFile + ":(function " + toString(*D) + ")";
260
25
261
25
  // If there's no symbol, print out the offset in the section.
262
25
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
263
25
}
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
237
1.81k
std::string InputSectionBase::getLocation(uint64_t Offset) {
238
1.81k
  // We don't have file for synthetic sections.
239
1.81k
  if (getFile<ELFT>() == nullptr)
240
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
241
0
        .str();
242
1.81k
243
1.81k
  // First check if we can get desired values from debugging information.
244
1.81k
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
245
1.81k
  if (!LineInfo.empty())
246
0
    return LineInfo;
247
1.81k
248
1.81k
  // File->SourceFile contains STT_FILE symbol that contains a
249
1.81k
  // source file name. If it's missing, we use an object file name.
250
1.81k
  std::string SrcFile = getFile<ELFT>()->SourceFile;
251
1.81k
  if (SrcFile.empty())
252
1.81k
    SrcFile = toString(File);
253
1.81k
254
1.81k
  // Find a function symbol that encloses a given location.
255
1.81k
  for (Symbol *B : File->getSymbols())
256
18.1M
    if (auto *D = dyn_cast<Defined>(B))
257
18.1M
      if (D->Section == this && 
D->Type == STT_FUNC18.1M
)
258
0
        if (D->Value <= Offset && Offset < D->Value + D->Size)
259
0
          return SrcFile + ":(function " + toString(*D) + ")";
260
1.81k
261
1.81k
  // If there's no symbol, print out the offset in the section.
262
1.81k
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
263
1.81k
}
264
265
// This function is intended to be used for constructing an error message.
266
// The returned message looks like this:
267
//
268
//   foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)
269
//
270
//  Returns an empty string if there's no way to get line info.
271
158
std::string InputSectionBase::getSrcMsg(const Symbol &Sym, uint64_t Offset) {
272
158
  // Synthetic sections don't have input files.
273
158
  if (!File)
274
2
    return "";
275
156
  return File->getSrcMsg(Sym, *this, Offset);
276
156
}
277
278
// Returns a filename string along with an optional section name. This
279
// function is intended to be used for constructing an error
280
// message. The returned message looks like this:
281
//
282
//   path/to/foo.o:(function bar)
283
//
284
// or
285
//
286
//   path/to/foo.o:(function bar) in archive path/to/bar.a
287
158
std::string InputSectionBase::getObjMsg(uint64_t Off) {
288
158
  // Synthetic sections don't have input files.
289
158
  if (!File)
290
2
    return ("<internal>:(" + Name + "+0x" + utohexstr(Off) + ")").str();
291
156
  std::string Filename = File->getName();
292
156
293
156
  std::string Archive;
294
156
  if (!File->ArchiveName.empty())
295
5
    Archive = (" in archive " + File->ArchiveName).str();
296
156
297
156
  // Find a symbol that encloses a given location.
298
156
  for (Symbol *B : File->getSymbols())
299
537
    if (auto *D = dyn_cast<Defined>(B))
300
231
      if (D->Section == this && 
D->Value <= Off147
&&
Off < D->Value + D->Size137
)
301
11
        return Filename + ":(" + toString(*D) + ")" + Archive;
302
156
303
156
  // If there's no symbol, print out the offset in the section.
304
156
  return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive)
305
145
      .str();
306
156
}
307
308
InputSection InputSection::Discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
309
310
InputSection::InputSection(InputFile *F, uint64_t Flags, uint32_t Type,
311
                           uint32_t Alignment, ArrayRef<uint8_t> Data,
312
                           StringRef Name, Kind K)
313
    : InputSectionBase(F, Flags, Type,
314
                       /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, Alignment, Data,
315
38.2k
                       Name, K) {}
316
317
template <class ELFT>
318
InputSection::InputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header,
319
                           StringRef Name)
320
201k
    : 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
320
953
    : 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
320
743
    : 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
320
199k
    : 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
320
263
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
321
322
306k
bool InputSection::classof(const SectionBase *S) {
323
306k
  return S->kind() == SectionBase::Regular ||
324
306k
         
S->kind() == SectionBase::Synthetic47.6k
;
325
306k
}
326
327
190k
OutputSection *InputSection::getParent() const {
328
190k
  return cast_or_null<OutputSection>(Parent);
329
190k
}
330
331
// Copy SHT_GROUP section contents. Used only for the -r option.
332
8
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
333
8
  // ELFT::Word is the 32-bit integral type in the target endianness.
334
8
  typedef typename ELFT::Word u32;
335
8
  ArrayRef<u32> From = getDataAs<u32>();
336
8
  auto *To = reinterpret_cast<u32 *>(Buf);
337
8
338
8
  // The first entry is not a section number but a flag.
339
8
  *To++ = From[0];
340
8
341
8
  // Adjust section numbers because section numbers in an input object
342
8
  // files are different in the output.
343
8
  ArrayRef<InputSectionBase *> Sections = File->getSections();
344
8
  for (uint32_t Idx : From.slice(1))
345
11
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
346
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
332
8
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
333
8
  // ELFT::Word is the 32-bit integral type in the target endianness.
334
8
  typedef typename ELFT::Word u32;
335
8
  ArrayRef<u32> From = getDataAs<u32>();
336
8
  auto *To = reinterpret_cast<u32 *>(Buf);
337
8
338
8
  // The first entry is not a section number but a flag.
339
8
  *To++ = From[0];
340
8
341
8
  // Adjust section numbers because section numbers in an input object
342
8
  // files are different in the output.
343
8
  ArrayRef<InputSectionBase *> Sections = File->getSections();
344
8
  for (uint32_t Idx : From.slice(1))
345
11
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
346
8
}
Unexecuted instantiation: void lld::elf::InputSection::copyShtGroup<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
347
348
221
InputSectionBase *InputSection::getRelocatedSection() {
349
221
  assert(Type == SHT_RELA || Type == SHT_REL);
350
221
  ArrayRef<InputSectionBase *> Sections = File->getSections();
351
221
  return Sections[Info];
352
221
}
353
354
// This is used for -r and --emit-relocs. We can't use memcpy to copy
355
// relocations because we need to update symbol table offset and section index
356
// for each relocation. So we copy relocations one by one.
357
template <class ELFT, class RelTy>
358
72
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
359
72
  InputSectionBase *Sec = getRelocatedSection();
360
72
361
113
  for (const RelTy &Rel : Rels) {
362
113
    RelType Type = Rel.getType(Config->IsMips64EL);
363
113
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
364
113
365
113
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
366
113
    Buf += sizeof(RelTy);
367
113
368
113
    if (Config->IsRela)
369
86
      P->r_addend = getAddend<ELFT>(Rel);
370
113
371
113
    // Output section VA is zero for -r, so r_offset is an offset within the
372
113
    // section, but for --emit-relocs it is an virtual address.
373
113
    P->r_offset = Sec->getOutputSection()->Addr + Sec->getOffset(Rel.r_offset);
374
113
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Sym), Type,
375
113
                        Config->IsMips64EL);
376
113
377
113
    if (Sym.Type == STT_SECTION) {
378
53
      // We combine multiple section symbols into only one per
379
53
      // section. This means we have to update the addend. That is
380
53
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
381
53
      // section data. We do that by adding to the Relocation vector.
382
53
383
53
      // .eh_frame is horribly special and can reference discarded sections. To
384
53
      // avoid having to parse and recreate .eh_frame, we just replace any
385
53
      // relocation in it pointing to discarded sections with R_*_NONE, which
386
53
      // hopefully creates a frame that is ignored at runtime.
387
53
      auto *D = dyn_cast<Defined>(&Sym);
388
53
      if (!D) {
389
1
        error("STT_SECTION symbol should be defined");
390
1
        continue;
391
1
      }
392
52
      SectionBase *Section = D->Section;
393
52
      if (Section == &InputSection::Discarded) {
394
2
        P->setSymbolAndType(0, 0, false);
395
2
        continue;
396
2
      }
397
50
398
50
      if (Config->IsRela) {
399
29
        P->r_addend =
400
29
            Sym.getVA(getAddend<ELFT>(Rel)) - Section->getOutputSection()->Addr;
401
29
      } else 
if (21
Config->Relocatable21
) {
402
20
        const uint8_t *BufLoc = Sec->Data.begin() + Rel.r_offset;
403
20
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset,
404
20
                                    Target->getImplicitAddend(BufLoc, Type),
405
20
                                    &Sym});
406
20
      }
407
50
    }
408
113
409
113
  }
410
72
}
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
358
13
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
359
13
  InputSectionBase *Sec = getRelocatedSection();
360
13
361
19
  for (const RelTy &Rel : Rels) {
362
19
    RelType Type = Rel.getType(Config->IsMips64EL);
363
19
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
364
19
365
19
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
366
19
    Buf += sizeof(RelTy);
367
19
368
19
    if (Config->IsRela)
369
0
      P->r_addend = getAddend<ELFT>(Rel);
370
19
371
19
    // Output section VA is zero for -r, so r_offset is an offset within the
372
19
    // section, but for --emit-relocs it is an virtual address.
373
19
    P->r_offset = Sec->getOutputSection()->Addr + Sec->getOffset(Rel.r_offset);
374
19
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Sym), Type,
375
19
                        Config->IsMips64EL);
376
19
377
19
    if (Sym.Type == STT_SECTION) {
378
13
      // We combine multiple section symbols into only one per
379
13
      // section. This means we have to update the addend. That is
380
13
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
381
13
      // section data. We do that by adding to the Relocation vector.
382
13
383
13
      // .eh_frame is horribly special and can reference discarded sections. To
384
13
      // avoid having to parse and recreate .eh_frame, we just replace any
385
13
      // relocation in it pointing to discarded sections with R_*_NONE, which
386
13
      // hopefully creates a frame that is ignored at runtime.
387
13
      auto *D = dyn_cast<Defined>(&Sym);
388
13
      if (!D) {
389
0
        error("STT_SECTION symbol should be defined");
390
0
        continue;
391
0
      }
392
13
      SectionBase *Section = D->Section;
393
13
      if (Section == &InputSection::Discarded) {
394
0
        P->setSymbolAndType(0, 0, false);
395
0
        continue;
396
0
      }
397
13
398
13
      if (Config->IsRela) {
399
0
        P->r_addend =
400
0
            Sym.getVA(getAddend<ELFT>(Rel)) - Section->getOutputSection()->Addr;
401
13
      } else if (Config->Relocatable) {
402
13
        const uint8_t *BufLoc = Sec->Data.begin() + Rel.r_offset;
403
13
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset,
404
13
                                    Target->getImplicitAddend(BufLoc, Type),
405
13
                                    &Sym});
406
13
      }
407
13
    }
408
19
409
19
  }
410
13
}
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
358
6
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
359
6
  InputSectionBase *Sec = getRelocatedSection();
360
6
361
9
  for (const RelTy &Rel : Rels) {
362
9
    RelType Type = Rel.getType(Config->IsMips64EL);
363
9
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
364
9
365
9
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
366
9
    Buf += sizeof(RelTy);
367
9
368
9
    if (Config->IsRela)
369
0
      P->r_addend = getAddend<ELFT>(Rel);
370
9
371
9
    // Output section VA is zero for -r, so r_offset is an offset within the
372
9
    // section, but for --emit-relocs it is an virtual address.
373
9
    P->r_offset = Sec->getOutputSection()->Addr + Sec->getOffset(Rel.r_offset);
374
9
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Sym), Type,
375
9
                        Config->IsMips64EL);
376
9
377
9
    if (Sym.Type == STT_SECTION) {
378
7
      // We combine multiple section symbols into only one per
379
7
      // section. This means we have to update the addend. That is
380
7
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
381
7
      // section data. We do that by adding to the Relocation vector.
382
7
383
7
      // .eh_frame is horribly special and can reference discarded sections. To
384
7
      // avoid having to parse and recreate .eh_frame, we just replace any
385
7
      // relocation in it pointing to discarded sections with R_*_NONE, which
386
7
      // hopefully creates a frame that is ignored at runtime.
387
7
      auto *D = dyn_cast<Defined>(&Sym);
388
7
      if (!D) {
389
0
        error("STT_SECTION symbol should be defined");
390
0
        continue;
391
0
      }
392
7
      SectionBase *Section = D->Section;
393
7
      if (Section == &InputSection::Discarded) {
394
0
        P->setSymbolAndType(0, 0, false);
395
0
        continue;
396
0
      }
397
7
398
7
      if (Config->IsRela) {
399
0
        P->r_addend =
400
0
            Sym.getVA(getAddend<ELFT>(Rel)) - Section->getOutputSection()->Addr;
401
7
      } else if (Config->Relocatable) {
402
7
        const uint8_t *BufLoc = Sec->Data.begin() + Rel.r_offset;
403
7
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset,
404
7
                                    Target->getImplicitAddend(BufLoc, Type),
405
7
                                    &Sym});
406
7
      }
407
7
    }
408
9
409
9
  }
410
6
}
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
358
53
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
359
53
  InputSectionBase *Sec = getRelocatedSection();
360
53
361
85
  for (const RelTy &Rel : Rels) {
362
85
    RelType Type = Rel.getType(Config->IsMips64EL);
363
85
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
364
85
365
85
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
366
85
    Buf += sizeof(RelTy);
367
85
368
85
    if (Config->IsRela)
369
86
      P->r_addend = getAddend<ELFT>(Rel);
370
85
371
85
    // Output section VA is zero for -r, so r_offset is an offset within the
372
85
    // section, but for --emit-relocs it is an virtual address.
373
85
    P->r_offset = Sec->getOutputSection()->Addr + Sec->getOffset(Rel.r_offset);
374
85
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Sym), Type,
375
85
                        Config->IsMips64EL);
376
85
377
85
    if (Sym.Type == STT_SECTION) {
378
33
      // We combine multiple section symbols into only one per
379
33
      // section. This means we have to update the addend. That is
380
33
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
381
33
      // section data. We do that by adding to the Relocation vector.
382
33
383
33
      // .eh_frame is horribly special and can reference discarded sections. To
384
33
      // avoid having to parse and recreate .eh_frame, we just replace any
385
33
      // relocation in it pointing to discarded sections with R_*_NONE, which
386
33
      // hopefully creates a frame that is ignored at runtime.
387
33
      auto *D = dyn_cast<Defined>(&Sym);
388
33
      if (!D) {
389
1
        error("STT_SECTION symbol should be defined");
390
1
        continue;
391
1
      }
392
32
      SectionBase *Section = D->Section;
393
32
      if (Section == &InputSection::Discarded) {
394
2
        P->setSymbolAndType(0, 0, false);
395
2
        continue;
396
2
      }
397
30
398
30
      if (Config->IsRela) {
399
29
        P->r_addend =
400
29
            Sym.getVA(getAddend<ELFT>(Rel)) - Section->getOutputSection()->Addr;
401
29
      } else 
if (1
Config->Relocatable1
) {
402
0
        const uint8_t *BufLoc = Sec->Data.begin() + Rel.r_offset;
403
0
        Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset,
404
0
                                    Target->getImplicitAddend(BufLoc, Type),
405
0
                                    &Sym});
406
0
      }
407
30
    }
408
85
409
85
  }
410
53
}
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Unexecuted instantiation: void lld::elf::InputSection::copyRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
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
15
                                              uint32_t P) {
419
15
  switch (Type) {
420
15
  // Unresolved branch relocations to weak references resolve to next
421
15
  // instruction, this will be either 2 or 4 bytes on from P.
422
15
  case R_ARM_THM_JUMP11:
423
0
    return P + 2 + A;
424
15
  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
static uint64_t getRelocTargetVA(RelType Type, int64_t A, uint64_t P,
485
12.2k
                                 const Symbol &Sym, RelExpr Expr) {
486
12.2k
  switch (Expr) {
487
12.2k
  case R_INVALID:
488
2
    return 0;
489
12.2k
  case R_ABS:
490
574
  case R_RELAX_GOT_PC_NOPIC:
491
574
    return Sym.getVA(A);
492
574
  case R_ARM_SBREL:
493
4
    return Sym.getVA(A) - getARMStaticBase(Sym);
494
574
  case R_GOT:
495
74
  case R_RELAX_TLS_GD_TO_IE_ABS:
496
74
    return Sym.getGotVA() + A;
497
74
  case R_GOTONLY_PC:
498
0
    return InX::Got->getVA() + A - P;
499
74
  case R_GOTONLY_PC_FROM_END:
500
6
    return InX::Got->getVA() + A - P + InX::Got->getSize();
501
74
  case R_GOTREL:
502
7
    return Sym.getVA(A) - InX::Got->getVA();
503
74
  case R_GOTREL_FROM_END:
504
4
    return Sym.getVA(A) - InX::Got->getVA() - InX::Got->getSize();
505
74
  case R_GOT_FROM_END:
506
28
  case R_RELAX_TLS_GD_TO_IE_END:
507
28
    return Sym.getGotOffset() + A - InX::Got->getSize();
508
28
  case R_GOT_OFF:
509
5
    return Sym.getGotOffset() + A;
510
28
  case R_GOT_PAGE_PC:
511
8
  case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
512
8
    return getAArch64Page(Sym.getGotVA() + A) - getAArch64Page(P);
513
97
  case R_GOT_PC:
514
97
  case R_RELAX_TLS_GD_TO_IE:
515
97
    return Sym.getGotVA() + A - P;
516
97
  case R_HINT:
517
0
  case R_NONE:
518
0
  case R_TLSDESC_CALL:
519
0
    llvm_unreachable("cannot relocate hint relocs");
520
19
  case R_MIPS_GOTREL:
521
19
    return Sym.getVA(A) - InX::MipsGot->getGp();
522
2
  case R_MIPS_GOT_GP:
523
2
    return InX::MipsGot->getGp() + A;
524
20
  case R_MIPS_GOT_GP_PC: {
525
20
    // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
526
20
    // is _gp_disp symbol. In that case we should use the following
527
20
    // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
528
20
    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
529
20
    // microMIPS variants of these relocations use slightly different
530
20
    // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
531
20
    // to correctly handle less-sugnificant bit of the microMIPS symbol.
532
20
    uint64_t V = InX::MipsGot->getGp() + A - P;
533
20
    if (Type == R_MIPS_LO16 || 
Type == R_MICROMIPS_LO1612
)
534
10
      V += 4;
535
20
    if (Type == R_MICROMIPS_LO16 || 
Type == R_MICROMIPS_HI1618
)
536
4
      V -= 1;
537
20
    return V;
538
0
  }
539
24
  case R_MIPS_GOT_LOCAL_PAGE:
540
24
    // If relocation against MIPS local symbol requires GOT entry, this entry
541
24
    // should be initialized by 'page address'. This address is high 16-bits
542
24
    // of sum the symbol's value and the addend.
543
24
    return InX::MipsGot->getVA() + InX::MipsGot->getPageEntryOffset(Sym, A) -
544
24
           InX::MipsGot->getGp();
545
10.0k
  case R_MIPS_GOT_OFF:
546
10.0k
  case R_MIPS_GOT_OFF32:
547
10.0k
    // In case of MIPS if a GOT relocation has non-zero addend this addend
548
10.0k
    // should be applied to the GOT entry content not to the GOT entry offset.
549
10.0k
    // That is why we use separate expression type.
550
10.0k
    return InX::MipsGot->getVA() + InX::MipsGot->getSymEntryOffset(Sym, A) -
551
10.0k
           InX::MipsGot->getGp();
552
10.0k
  case R_MIPS_TLSGD:
553
9
    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
554
9
           InX::MipsGot->getGlobalDynOffset(Sym) - InX::MipsGot->getGp();
555
10.0k
  case R_MIPS_TLSLD:
556
5
    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
557
5
           InX::MipsGot->getTlsIndexOff() - InX::MipsGot->getGp();
558
10.0k
  case R_PAGE_PC:
559
82
  case R_PLT_PAGE_PC: {
560
82
    uint64_t Dest;
561
82
    if (Sym.isUndefWeak())
562
1
      Dest = getAArch64Page(A);
563
81
    else
564
81
      Dest = getAArch64Page(Sym.getVA(A));
565
82
    return Dest - getAArch64Page(P);
566
82
  }
567
745
  case R_PC: {
568
745
    uint64_t Dest;
569
745
    if (Sym.isUndefWeak()) {
570
28
      // On ARM and AArch64 a branch to an undefined weak resolves to the
571
28
      // next instruction, otherwise the place.
572
28
      if (Config->EMachine == EM_ARM)
573
15
        Dest = getARMUndefinedRelativeWeakVA(Type, A, P);
574
13
      else if (Config->EMachine == EM_AARCH64)
575
9
        Dest = getAArch64UndefinedRelativeWeakVA(Type, A, P);
576
4
      else
577
4
        Dest = Sym.getVA(A);
578
717
    } else {
579
717
      Dest = Sym.getVA(A);
580
717
    }
581
745
    return Dest - P;
582
82
  }
583
82
  case R_PLT:
584
35
    return Sym.getPltVA() + A;
585
250
  case R_PLT_PC:
586
250
  case R_PPC_PLT_OPD:
587
250
    return Sym.getPltVA() + A - P;
588
250
  case R_PPC_OPD: {
589
3
    uint64_t SymVA = Sym.getVA(A);
590
3
    // If we have an undefined weak symbol, we might get here with a symbol
591
3
    // address of zero. That could overflow, but the code must be unreachable,
592
3
    // so don't bother doing anything at all.
593
3
    if (!SymVA)
594
1
      return 0;
595
2
    if (Out::Opd) {
596
2
      // If this is a local call, and we currently have the address of a
597
2
      // function-descriptor, get the underlying code address instead.
598
2
      uint64_t OpdStart = Out::Opd->Addr;
599
2
      uint64_t OpdEnd = OpdStart + Out::Opd->Size;
600
2
      bool InOpd = OpdStart <= SymVA && 
SymVA < OpdEnd1
;
601
2
      if (InOpd)
602
1
        SymVA = read64be(&Out::OpdBuf[SymVA - OpdStart]);
603
2
    }
604
2
    return SymVA - P;
605
2
  }
606
5
  case R_PPC_TOC:
607
5
    return getPPC64TocBase() + A;
608
21
  case R_RELAX_GOT_PC:
609
21
    return Sym.getVA(A) - P;
610
88
  case R_RELAX_TLS_GD_TO_LE:
611
88
  case R_RELAX_TLS_IE_TO_LE:
612
88
  case R_RELAX_TLS_LD_TO_LE:
613
88
  case R_TLS:
614
88
    // A weak undefined TLS symbol resolves to the base of the TLS
615
88
    // block, i.e. gets a value of zero. If we pass --gc-sections to
616
88
    // lld and .tbss is not referenced, it gets reclaimed and we don't
617
88
    // create a TLS program header. Therefore, we resolve this
618
88
    // statically to zero.
619
88
    if (Sym.isTls() && Sym.isUndefWeak())
620
3
      return 0;
621
85
    if (Target->TcbSize)
622
20
      return Sym.getVA(A) + alignTo(Target->TcbSize, Out::TlsPhdr->p_align);
623
65
    return Sym.getVA(A) - Out::TlsPhdr->p_memsz;
624
65
  case R_RELAX_TLS_GD_TO_LE_NEG:
625
4
  case R_NEG_TLS:
626
4
    return Out::TlsPhdr->p_memsz - Sym.getVA(A);
627
24
  case R_SIZE:
628
24
    return Sym.getSize() + A;
629
8
  case R_TLSDESC:
630
8
    return InX::Got->getGlobalDynAddr(Sym) + A;
631
4
  case R_TLSDESC_PAGE:
632
4
    return getAArch64Page(InX::Got->getGlobalDynAddr(Sym) + A) -
633
4
           getAArch64Page(P);
634
4
  case R_TLSGD:
635
2
    return InX::Got->getGlobalDynOffset(Sym) + A - InX::Got->getSize();
636
18
  case R_TLSGD_PC:
637
18
    return InX::Got->getGlobalDynAddr(Sym) + A - P;
638
4
  case R_TLSLD:
639
2
    return InX::Got->getTlsIndexOff() + A - InX::Got->getSize();
640
4
  case R_TLSLD_PC:
641
4
    return InX::Got->getTlsIndexVA() + A - P;
642
0
  }
643
0
  llvm_unreachable("Invalid expression");
644
0
}
645
646
// This function applies relocations to sections without SHF_ALLOC bit.
647
// Such sections are never mapped to memory at runtime. Debug sections are
648
// an example. Relocations in non-alloc sections are much easier to
649
// handle than in allocated sections because it will never need complex
650
// treatement such as GOT or PLT (because at runtime no one refers them).
651
// So, we handle relocations for non-alloc sections directly in this
652
// function as a performance optimization.
653
template <class ELFT, class RelTy>
654
130k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
130k
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
130k
657
130k
  for (const RelTy &Rel : Rels) {
658
78
    RelType Type = Rel.getType(Config->IsMips64EL);
659
78
    uint64_t Offset = getOffset(Rel.r_offset);
660
78
    uint8_t *BufLoc = Buf + Offset;
661
78
    int64_t Addend = getAddend<ELFT>(Rel);
662
78
    if (!RelTy::IsRela)
663
5
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
78
665
78
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
78
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
78
    if (Expr == R_NONE)
668
1
      continue;
669
77
    if (Expr != R_ABS) {
670
3
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
3
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
3
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
3
      // but we need to keep this bug-compatible code for a while.
674
3
      if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC2
)
675
1
        continue;
676
2
677
2
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
2
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
2
      return;
680
2
    }
681
74
682
74
    if (Sym.isTls() && 
!Out::TlsPhdr3
)
683
1
      Target->relocateOne(BufLoc, Type, 0);
684
73
    else
685
73
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
74
  }
687
130k
}
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
654
10
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
10
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
10
657
10
  for (const RelTy &Rel : Rels) {
658
2
    RelType Type = Rel.getType(Config->IsMips64EL);
659
2
    uint64_t Offset = getOffset(Rel.r_offset);
660
2
    uint8_t *BufLoc = Buf + Offset;
661
2
    int64_t Addend = getAddend<ELFT>(Rel);
662
2
    if (!RelTy::IsRela)
663
2
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
2
665
2
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
2
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
2
    if (Expr == R_NONE)
668
0
      continue;
669
2
    if (Expr != R_ABS) {
670
2
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
2
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
2
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
2
      // but we need to keep this bug-compatible code for a while.
674
2
      if (Config->EMachine == EM_386 && Type == R_386_GOTPC)
675
1
        continue;
676
1
677
1
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
1
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
1
      return;
680
1
    }
681
0
682
0
    if (Sym.isTls() && !Out::TlsPhdr)
683
0
      Target->relocateOne(BufLoc, Type, 0);
684
0
    else
685
0
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
0
  }
687
10
}
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
654
5
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
5
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
5
657
5
  for (const RelTy &Rel : Rels) {
658
3
    RelType Type = Rel.getType(Config->IsMips64EL);
659
3
    uint64_t Offset = getOffset(Rel.r_offset);
660
3
    uint8_t *BufLoc = Buf + Offset;
661
3
    int64_t Addend = getAddend<ELFT>(Rel);
662
3
    if (!RelTy::IsRela)
663
3
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
3
665
3
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
3
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
3
    if (Expr == R_NONE)
668
0
      continue;
669
3
    if (Expr != R_ABS) {
670
0
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
0
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
0
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
0
      // but we need to keep this bug-compatible code for a while.
674
0
      if (Config->EMachine == EM_386 && Type == R_386_GOTPC)
675
0
        continue;
676
0
677
0
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
0
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
0
      return;
680
0
    }
681
3
682
3
    if (Sym.isTls() && 
!Out::TlsPhdr0
)
683
0
      Target->relocateOne(BufLoc, Type, 0);
684
3
    else
685
3
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
3
  }
687
5
}
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
654
30
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
30
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
30
657
73
  for (const RelTy &Rel : Rels) {
658
73
    RelType Type = Rel.getType(Config->IsMips64EL);
659
73
    uint64_t Offset = getOffset(Rel.r_offset);
660
73
    uint8_t *BufLoc = Buf + Offset;
661
73
    int64_t Addend = getAddend<ELFT>(Rel);
662
73
    if (!RelTy::IsRela)
663
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
73
665
73
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
73
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
73
    if (Expr == R_NONE)
668
1
      continue;
669
72
    if (Expr != R_ABS) {
670
1
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
1
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
1
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
1
      // but we need to keep this bug-compatible code for a while.
674
1
      if (Config->EMachine == EM_386 && 
Type == R_386_GOTPC0
)
675
0
        continue;
676
1
677
1
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
1
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
1
      return;
680
1
    }
681
71
682
71
    if (Sym.isTls() && 
!Out::TlsPhdr3
)
683
1
      Target->relocateOne(BufLoc, Type, 0);
684
70
    else
685
70
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
71
  }
687
30
}
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
654
130k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
130k
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
130k
657
130k
  for (const RelTy &Rel : Rels) {
658
0
    RelType Type = Rel.getType(Config->IsMips64EL);
659
0
    uint64_t Offset = getOffset(Rel.r_offset);
660
0
    uint8_t *BufLoc = Buf + Offset;
661
0
    int64_t Addend = getAddend<ELFT>(Rel);
662
0
    if (!RelTy::IsRela)
663
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
0
665
0
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
0
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
0
    if (Expr == R_NONE)
668
0
      continue;
669
0
    if (Expr != R_ABS) {
670
0
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
0
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
0
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
0
      // but we need to keep this bug-compatible code for a while.
674
0
      if (Config->EMachine == EM_386 && Type == R_386_GOTPC)
675
0
        continue;
676
0
677
0
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
0
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
0
      return;
680
0
    }
681
0
682
0
    if (Sym.isTls() && !Out::TlsPhdr)
683
0
      Target->relocateOne(BufLoc, Type, 0);
684
0
    else
685
0
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
0
  }
687
130k
}
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>, true> >(unsigned char*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
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> >)
Line
Count
Source
654
2
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
655
2
  const unsigned Bits = sizeof(typename ELFT::uint) * 8;
656
2
657
2
  for (const RelTy &Rel : Rels) {
658
0
    RelType Type = Rel.getType(Config->IsMips64EL);
659
0
    uint64_t Offset = getOffset(Rel.r_offset);
660
0
    uint8_t *BufLoc = Buf + Offset;
661
0
    int64_t Addend = getAddend<ELFT>(Rel);
662
0
    if (!RelTy::IsRela)
663
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
664
0
665
0
    Symbol &Sym = getFile<ELFT>()->getRelocTargetSym(Rel);
666
0
    RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc);
667
0
    if (Expr == R_NONE)
668
0
      continue;
669
0
    if (Expr != R_ABS) {
670
0
      // GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
671
0
      // against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
672
0
      // been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
673
0
      // but we need to keep this bug-compatible code for a while.
674
0
      if (Config->EMachine == EM_386 && Type == R_386_GOTPC)
675
0
        continue;
676
0
677
0
      error(getLocation<ELFT>(Offset) + ": has non-ABS relocation " +
678
0
            toString(Type) + " against symbol '" + toString(Sym) + "'");
679
0
      return;
680
0
    }
681
0
682
0
    if (Sym.isTls() && !Out::TlsPhdr)
683
0
      Target->relocateOne(BufLoc, Type, 0);
684
0
    else
685
0
      Target->relocateOne(BufLoc, Type, SignExtend64<Bits>(Sym.getVA(Addend)));
686
0
  }
687
2
}
688
689
template <class ELFT>
690
199k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
691
199k
  if (Flags & SHF_ALLOC) {
692
69.3k
    relocateAlloc(Buf, BufEnd);
693
69.3k
    return;
694
69.3k
  }
695
130k
696
130k
  auto *Sec = cast<InputSection>(this);
697
130k
  if (Sec->AreRelocsRela)
698
30
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
699
130k
  else
700
130k
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
701
130k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, unsigned char*)
Line
Count
Source
690
831
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
691
831
  if (Flags & SHF_ALLOC) {
692
821
    relocateAlloc(Buf, BufEnd);
693
821
    return;
694
821
  }
695
10
696
10
  auto *Sec = cast<InputSection>(this);
697
10
  if (Sec->AreRelocsRela)
698
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
699
10
  else
700
10
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
701
10
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, unsigned char*)
Line
Count
Source
690
309
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
691
309
  if (Flags & SHF_ALLOC) {
692
304
    relocateAlloc(Buf, BufEnd);
693
304
    return;
694
304
  }
695
5
696
5
  auto *Sec = cast<InputSection>(this);
697
5
  if (Sec->AreRelocsRela)
698
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
699
5
  else
700
5
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
701
5
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, unsigned char*)
Line
Count
Source
690
198k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
691
198k
  if (Flags & SHF_ALLOC) {
692
68.0k
    relocateAlloc(Buf, BufEnd);
693
68.0k
    return;
694
68.0k
  }
695
130k
696
130k
  auto *Sec = cast<InputSection>(this);
697
130k
  if (Sec->AreRelocsRela)
698
30
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
699
130k
  else
700
130k
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
701
130k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, unsigned char*)
Line
Count
Source
690
139
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
691
139
  if (Flags & SHF_ALLOC) {
692
137
    relocateAlloc(Buf, BufEnd);
693
137
    return;
694
137
  }
695
2
696
2
  auto *Sec = cast<InputSection>(this);
697
2
  if (Sec->AreRelocsRela)
698
0
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
699
2
  else
700
2
    Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
701
2
}
702
703
69.5k
void InputSectionBase::relocateAlloc(uint8_t *Buf, uint8_t *BufEnd) {
704
69.5k
  assert(Flags & SHF_ALLOC);
705
69.5k
  const unsigned Bits = Config->Wordsize * 8;
706
69.5k
707
69.5k
  for (const Relocation &Rel : Relocations) {
708
12.2k
    uint64_t Offset = getOffset(Rel.Offset);
709
12.2k
    uint8_t *BufLoc = Buf + Offset;
710
12.2k
    RelType Type = Rel.Type;
711
12.2k
712
12.2k
    uint64_t AddrLoc = getOutputSection()->Addr + Offset;
713
12.2k
    RelExpr Expr = Rel.Expr;
714
12.2k
    uint64_t TargetVA = SignExtend64(
715
12.2k
        getRelocTargetVA(Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr), Bits);
716
12.2k
717
12.2k
    switch (Expr) {
718
12.2k
    case R_RELAX_GOT_PC:
719
30
    case R_RELAX_GOT_PC_NOPIC:
720
30
      Target->relaxGot(BufLoc, TargetVA);
721
30
      break;
722
48
    case R_RELAX_TLS_IE_TO_LE:
723
48
      Target->relaxTlsIeToLe(BufLoc, Type, TargetVA);
724
48
      break;
725
30
    case R_RELAX_TLS_LD_TO_LE:
726
12
      Target->relaxTlsLdToLe(BufLoc, Type, TargetVA);
727
12
      break;
728
30
    case R_RELAX_TLS_GD_TO_LE:
729
13
    case R_RELAX_TLS_GD_TO_LE_NEG:
730
13
      Target->relaxTlsGdToLe(BufLoc, Type, TargetVA);
731
13
      break;
732
13
    case R_RELAX_TLS_GD_TO_IE:
733
10
    case R_RELAX_TLS_GD_TO_IE_ABS:
734
10
    case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
735
10
    case R_RELAX_TLS_GD_TO_IE_END:
736
10
      Target->relaxTlsGdToIe(BufLoc, Type, TargetVA);
737
10
      break;
738
10
    case R_PPC_PLT_OPD:
739
8
      // Patch a nop (0x60000000) to a ld.
740
8
      if (BufLoc + 8 <= BufEnd && 
read32be(BufLoc + 4) == 0x600000006
)
741
2
        write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
742
8
      LLVM_FALLTHROUGH;
743
12.1k
    default:
744
12.1k
      Target->relocateOne(BufLoc, Type, TargetVA);
745
12.1k
      break;
746
12.2k
    }
747
12.2k
  }
748
69.5k
}
749
750
212k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
751
212k
  if (Type == SHT_NOBITS)
752
1
    return;
753
212k
754
212k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
755
12.5k
    S->writeTo(Buf + OutSecOff);
756
12.5k
    return;
757
12.5k
  }
758
200k
759
200k
  // If -r or --emit-relocs is given, then an InputSection
760
200k
  // may be a relocation section.
761
200k
  if (Type == SHT_RELA) {
762
54
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
763
54
    return;
764
54
  }
765
200k
  if (Type == SHT_REL) {
766
19
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
767
19
    return;
768
19
  }
769
200k
770
200k
  // If -r is given, we may have a SHT_GROUP section.
771
200k
  if (Type == SHT_GROUP) {
772
8
    copyShtGroup<ELFT>(Buf + OutSecOff);
773
8
    return;
774
8
  }
775
200k
776
200k
  // Copy section contents from source object file to output file
777
200k
  // and then apply relocations.
778
200k
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
779
200k
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
780
200k
  relocate<ELFT>(Buf, BufEnd);
781
200k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
750
2.61k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
751
2.61k
  if (Type == SHT_NOBITS)
752
0
    return;
753
2.61k
754
2.61k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
755
1.76k
    S->writeTo(Buf + OutSecOff);
756
1.76k
    return;
757
1.76k
  }
758
843
759
843
  // If -r or --emit-relocs is given, then an InputSection
760
843
  // may be a relocation section.
761
843
  if (Type == SHT_RELA) {
762
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
763
0
    return;
764
0
  }
765
843
  if (Type == SHT_REL) {
766
13
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
767
13
    return;
768
13
  }
769
830
770
830
  // If -r is given, we may have a SHT_GROUP section.
771
830
  if (Type == SHT_GROUP) {
772
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
773
0
    return;
774
0
  }
775
830
776
830
  // Copy section contents from source object file to output file
777
830
  // and then apply relocations.
778
830
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
779
830
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
780
830
  relocate<ELFT>(Buf, BufEnd);
781
830
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
750
1.29k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
751
1.29k
  if (Type == SHT_NOBITS)
752
0
    return;
753
1.29k
754
1.29k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
755
978
    S->writeTo(Buf + OutSecOff);
756
978
    return;
757
978
  }
758
315
759
315
  // If -r or --emit-relocs is given, then an InputSection
760
315
  // may be a relocation section.
761
315
  if (Type == SHT_RELA) {
762
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
763
0
    return;
764
0
  }
765
315
  if (Type == SHT_REL) {
766
6
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
767
6
    return;
768
6
  }
769
309
770
309
  // If -r is given, we may have a SHT_GROUP section.
771
309
  if (Type == SHT_GROUP) {
772
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
773
0
    return;
774
0
  }
775
309
776
309
  // Copy section contents from source object file to output file
777
309
  // and then apply relocations.
778
309
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
779
309
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
780
309
  relocate<ELFT>(Buf, BufEnd);
781
309
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
750
208k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
751
208k
  if (Type == SHT_NOBITS)
752
1
    return;
753
208k
754
208k
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
755
9.28k
    S->writeTo(Buf + OutSecOff);
756
9.28k
    return;
757
9.28k
  }
758
198k
759
198k
  // If -r or --emit-relocs is given, then an InputSection
760
198k
  // may be a relocation section.
761
198k
  if (Type == SHT_RELA) {
762
54
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
763
54
    return;
764
54
  }
765
198k
  if (Type == SHT_REL) {
766
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
767
0
    return;
768
0
  }
769
198k
770
198k
  // If -r is given, we may have a SHT_GROUP section.
771
198k
  if (Type == SHT_GROUP) {
772
8
    copyShtGroup<ELFT>(Buf + OutSecOff);
773
8
    return;
774
8
  }
775
198k
776
198k
  // Copy section contents from source object file to output file
777
198k
  // and then apply relocations.
778
198k
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
779
198k
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
780
198k
  relocate<ELFT>(Buf, BufEnd);
781
198k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
750
613
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
751
613
  if (Type == SHT_NOBITS)
752
0
    return;
753
613
754
613
  if (auto *S = dyn_cast<SyntheticSection>(this)) {
755
473
    S->writeTo(Buf + OutSecOff);
756
473
    return;
757
473
  }
758
140
759
140
  // If -r or --emit-relocs is given, then an InputSection
760
140
  // may be a relocation section.
761
140
  if (Type == SHT_RELA) {
762
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rela>());
763
0
    return;
764
0
  }
765
140
  if (Type == SHT_REL) {
766
0
    copyRelocations<ELFT>(Buf + OutSecOff, getDataAs<typename ELFT::Rel>());
767
0
    return;
768
0
  }
769
140
770
140
  // If -r is given, we may have a SHT_GROUP section.
771
140
  if (Type == SHT_GROUP) {
772
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
773
0
    return;
774
0
  }
775
140
776
140
  // Copy section contents from source object file to output file
777
140
  // and then apply relocations.
778
140
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
779
140
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
780
140
  relocate<ELFT>(Buf, BufEnd);
781
140
}
782
783
18
void InputSection::replace(InputSection *Other) {
784
18
  Alignment = std::max(Alignment, Other->Alignment);
785
18
  Other->Repl = Repl;
786
18
  Other->Live = false;
787
18
}
788
789
template <class ELFT>
790
EhInputSection::EhInputSection(ObjFile<ELFT> &F,
791
                               const typename ELFT::Shdr &Header,
792
                               StringRef Name)
793
98
    : 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
793
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
793
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
793
90
    : 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
793
3
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {}
794
795
124
SyntheticSection *EhInputSection::getParent() const {
796
124
  return cast_or_null<SyntheticSection>(Parent);
797
124
}
798
799
// Returns the index of the first relocation that points to a region between
800
// Begin and Begin+Size.
801
template <class IntTy, class RelTy>
802
static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels,
803
219
                         unsigned &RelocI) {
804
219
  // Start search from RelocI for fast access. That works because the
805
219
  // relocations are sorted in .eh_frame.
806
261
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI42
) {
807
253
    const RelTy &Rel = Rels[RelocI];
808
253
    if (Rel.r_offset < Begin)
809
42
      continue;
810
211
811
211
    if (Rel.r_offset < Begin + Size)
812
130
      return RelocI;
813
81
    return -1;
814
81
  }
815
219
  
return -18
;
816
219
}
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
803
8
                         unsigned &RelocI) {
804
8
  // Start search from RelocI for fast access. That works because the
805
8
  // relocations are sorted in .eh_frame.
806
10
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI2
) {
807
10
    const RelTy &Rel = Rels[RelocI];
808
10
    if (Rel.r_offset < Begin)
809
2
      continue;
810
8
811
8
    if (Rel.r_offset < Begin + Size)
812
6
      return RelocI;
813
2
    return -1;
814
2
  }
815
8
  
return -10
;
816
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
803
2
                         unsigned &RelocI) {
804
2
  // Start search from RelocI for fast access. That works because the
805
2
  // relocations are sorted in .eh_frame.
806
2
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
807
2
    const RelTy &Rel = Rels[RelocI];
808
2
    if (Rel.r_offset < Begin)
809
0
      continue;
810
2
811
2
    if (Rel.r_offset < Begin + Size)
812
1
      return RelocI;
813
1
    return -1;
814
1
  }
815
2
  
return -10
;
816
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
803
197
                         unsigned &RelocI) {
804
197
  // Start search from RelocI for fast access. That works because the
805
197
  // relocations are sorted in .eh_frame.
806
237
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI40
) {
807
237
    const RelTy &Rel = Rels[RelocI];
808
237
    if (Rel.r_offset < Begin)
809
40
      continue;
810
197
811
197
    if (Rel.r_offset < Begin + Size)
812
121
      return RelocI;
813
76
    return -1;
814
76
  }
815
197
  
return -10
;
816
197
}
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
803
7
                         unsigned &RelocI) {
804
7
  // Start search from RelocI for fast access. That works because the
805
7
  // relocations are sorted in .eh_frame.
806
7
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
807
0
    const RelTy &Rel = Rels[RelocI];
808
0
    if (Rel.r_offset < Begin)
809
0
      continue;
810
0
811
0
    if (Rel.r_offset < Begin + Size)
812
0
      return RelocI;
813
0
    return -1;
814
0
  }
815
7
  return -1;
816
7
}
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
803
4
                         unsigned &RelocI) {
804
4
  // Start search from RelocI for fast access. That works because the
805
4
  // relocations are sorted in .eh_frame.
806
4
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
807
4
    const RelTy &Rel = Rels[RelocI];
808
4
    if (Rel.r_offset < Begin)
809
0
      continue;
810
4
811
4
    if (Rel.r_offset < Begin + Size)
812
2
      return RelocI;
813
2
    return -1;
814
2
  }
815
4
  
return -10
;
816
4
}
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
803
1
                         unsigned &RelocI) {
804
1
  // Start search from RelocI for fast access. That works because the
805
1
  // relocations are sorted in .eh_frame.
806
1
  for (unsigned N = Rels.size(); RelocI < N; 
++RelocI0
) {
807
0
    const RelTy &Rel = Rels[RelocI];
808
0
    if (Rel.r_offset < Begin)
809
0
      continue;
810
0
811
0
    if (Rel.r_offset < Begin + Size)
812
0
      return RelocI;
813
0
    return -1;
814
0
  }
815
1
  return -1;
816
1
}
817
818
// .eh_frame is a sequence of CIE or FDE records.
819
// This function splits an input section into records and returns them.
820
103
template <class ELFT> void EhInputSection::split() {
821
103
  // Early exit if already split.
822
103
  if (!Pieces.empty())
823
5
    return;
824
98
825
98
  if (AreRelocsRela)
826
83
    split<ELFT>(relas<ELFT>());
827
15
  else
828
15
    split<ELFT>(rels<ELFT>());
829
98
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
820
6
template <class ELFT> void EhInputSection::split() {
821
6
  // Early exit if already split.
822
6
  if (!Pieces.empty())
823
2
    return;
824
4
825
4
  if (AreRelocsRela)
826
0
    split<ELFT>(relas<ELFT>());
827
4
  else
828
4
    split<ELFT>(rels<ELFT>());
829
4
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
820
1
template <class ELFT> void EhInputSection::split() {
821
1
  // Early exit if already split.
822
1
  if (!Pieces.empty())
823
0
    return;
824
1
825
1
  if (AreRelocsRela)
826
0
    split<ELFT>(relas<ELFT>());
827
1
  else
828
1
    split<ELFT>(rels<ELFT>());
829
1
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
820
93
template <class ELFT> void EhInputSection::split() {
821
93
  // Early exit if already split.
822
93
  if (!Pieces.empty())
823
3
    return;
824
90
825
90
  if (AreRelocsRela)
826
81
    split<ELFT>(relas<ELFT>());
827
9
  else
828
9
    split<ELFT>(rels<ELFT>());
829
90
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
820
3
template <class ELFT> void EhInputSection::split() {
821
3
  // Early exit if already split.
822
3
  if (!Pieces.empty())
823
0
    return;
824
3
825
3
  if (AreRelocsRela)
826
2
    split<ELFT>(relas<ELFT>());
827
1
  else
828
1
    split<ELFT>(rels<ELFT>());
829
3
}
830
831
template <class ELFT, class RelTy>
832
98
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
98
  unsigned RelI = 0;
834
312
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
219
    size_t Size = readEhRecordSize(this, Off);
836
219
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
219
    // The empty record is the end marker.
838
219
    if (Size == 4)
839
5
      break;
840
214
    Off += Size;
841
214
  }
842
98
}
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
832
4
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
4
  unsigned RelI = 0;
834
12
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
8
    size_t Size = readEhRecordSize(this, Off);
836
8
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
8
    // The empty record is the end marker.
838
8
    if (Size == 4)
839
0
      break;
840
8
    Off += Size;
841
8
  }
842
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
832
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
1
  unsigned RelI = 0;
834
3
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
2
    size_t Size = readEhRecordSize(this, Off);
836
2
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
2
    // The empty record is the end marker.
838
2
    if (Size == 4)
839
0
      break;
840
2
    Off += Size;
841
2
  }
842
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
832
81
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
81
  unsigned RelI = 0;
834
278
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
197
    size_t Size = readEhRecordSize(this, Off);
836
197
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
197
    // The empty record is the end marker.
838
197
    if (Size == 4)
839
0
      break;
840
197
    Off += Size;
841
197
  }
842
81
}
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
832
9
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
9
  unsigned RelI = 0;
834
11
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
7
    size_t Size = readEhRecordSize(this, Off);
836
7
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
7
    // The empty record is the end marker.
838
7
    if (Size == 4)
839
5
      break;
840
2
    Off += Size;
841
2
  }
842
9
}
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
832
2
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
2
  unsigned RelI = 0;
834
6
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
4
    size_t Size = readEhRecordSize(this, Off);
836
4
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
4
    // The empty record is the end marker.
838
4
    if (Size == 4)
839
0
      break;
840
4
    Off += Size;
841
4
  }
842
2
}
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
832
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
833
1
  unsigned RelI = 0;
834
2
  for (size_t Off = 0, End = Data.size(); Off != End;) {
835
1
    size_t Size = readEhRecordSize(this, Off);
836
1
    Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
837
1
    // The empty record is the end marker.
838
1
    if (Size == 4)
839
0
      break;
840
1
    Off += Size;
841
1
  }
842
1
}
843
844
1.85k
static size_t findNull(StringRef S, size_t EntSize) {
845
1.85k
  // Optimize the common case.
846
1.85k
  if (EntSize == 1)
847
1.84k
    return S.find(0);
848
5
849
7
  
for (unsigned I = 0, N = S.size(); 5
I != N;
I += EntSize2
) {
850
7
    const char *B = S.begin() + I;
851
30
    if (
std::all_of(B, B + EntSize, [](char C) 7
{ return C == 0; }))
852
5
      return I;
853
7
  }
854
5
  
return StringRef::npos0
;
855
5
}
856
857
206
SyntheticSection *MergeInputSection::getParent() const {
858
206
  return cast_or_null<SyntheticSection>(Parent);
859
206
}
860
861
// Split SHF_STRINGS section. Such section is a sequence of
862
// null-terminated strings.
863
1.79k
void MergeInputSection::splitStrings(ArrayRef<uint8_t> Data, size_t EntSize) {
864
1.79k
  size_t Off = 0;
865
1.79k
  bool IsAlloc = Flags & SHF_ALLOC;
866
1.79k
  StringRef S = toStringRef(Data);
867
1.79k
868
3.64k
  while (!S.empty()) {
869
1.85k
    size_t End = findNull(S, EntSize);
870
1.85k
    if (End == StringRef::npos)
871
0
      fatal(toString(this) + ": string is not null terminated");
872
1.85k
    size_t Size = End + EntSize;
873
1.85k
874
1.85k
    Pieces.emplace_back(Off, xxHash64(S.substr(0, Size)), !IsAlloc);
875
1.85k
    S = S.substr(Size);
876
1.85k
    Off += Size;
877
1.85k
  }
878
1.79k
}
879
880
// Split non-SHF_STRINGS section. Such section is a sequence of
881
// fixed size records.
882
void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> Data,
883
20
                                        size_t EntSize) {
884
20
  size_t Size = Data.size();
885
20
  assert((Size % EntSize) == 0);
886
20
  bool IsAlloc = Flags & SHF_ALLOC;
887
20
888
49
  for (size_t I = 0; I != Size; 
I += EntSize29
)
889
29
    Pieces.emplace_back(I, xxHash64(toStringRef(Data.slice(I, EntSize))),
890
29
                        !IsAlloc);
891
20
}
892
893
template <class ELFT>
894
MergeInputSection::MergeInputSection(ObjFile<ELFT> &F,
895
                                     const typename ELFT::Shdr &Header,
896
                                     StringRef Name)
897
90
    : 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
897
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
897
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
897
84
    : 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)
898
899
MergeInputSection::MergeInputSection(uint64_t Flags, uint32_t Type,
900
                                     uint64_t Entsize, ArrayRef<uint8_t> Data,
901
                                     StringRef Name)
902
    : InputSectionBase(nullptr, Flags, Type, Entsize, /*Link*/ 0, /*Info*/ 0,
903
1.73k
                       /*Alignment*/ Entsize, Data, Name, SectionBase::Merge) {}
904
905
// This function is called after we obtain a complete list of input sections
906
// that need to be linked. This is responsible to split section contents
907
// into small chunks for further processing.
908
//
909
// Note that this function is called from parallelForEach. This must be
910
// thread-safe (i.e. no memory allocation from the pools).
911
1.81k
void MergeInputSection::splitIntoPieces() {
912
1.81k
  assert(Pieces.empty());
913
1.81k
914
1.81k
  if (Flags & SHF_STRINGS)
915
1.79k
    splitStrings(Data, Entsize);
916
19
  else
917
19
    splitNonStrings(Data, Entsize);
918
1.81k
919
1.81k
  if (Config->GcSections && 
(Flags & SHF_ALLOC)77
)
920
7
    for (uint64_t Off : LiveOffsets)
921
8
      getSectionPiece(Off)->Live = true;
922
1.81k
}
923
924
// Do binary search to get a section piece at a given input offset.
925
64
SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
926
64
  auto *This = static_cast<const MergeInputSection *>(this);
927
64
  return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
928
64
}
929
930
template <class It, class T, class Compare>
931
69
static It fastUpperBound(It First, It Last, const T &Value, Compare Comp) {
932
69
  size_t Size = std::distance(First, Last);
933
69
  assert(Size != 0);
934
154
  while (Size != 1) {
935
85
    size_t H = Size / 2;
936
85
    const It MI = First + H;
937
85
    Size -= H;
938
85
    First = Comp(Value, *MI) ? 
First55
:
First + H30
;
939
85
  }
940
69
  return Comp(Value, *First) ? 
First0
: First + 1;
941
69
}
942
943
69
const SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) const {
944
69
  if (Data.size() <= Offset)
945
0
    fatal(toString(this) + ": entry is past the end of the section");
946
69
947
69
  // Find the element this offset points to.
948
69
  auto I = fastUpperBound(
949
69
      Pieces.begin(), Pieces.end(), Offset,
950
154
      [](const uint64_t &A, const SectionPiece &B) { return A < B.InputOff; });
951
69
  --I;
952
69
  return &*I;
953
69
}
954
955
// Returns the offset in an output section for a given input offset.
956
// Because contents of a mergeable section is not contiguous in output,
957
// it is not just an addition to a base output offset.
958
91
uint64_t MergeInputSection::getOffset(uint64_t Offset) const {
959
91
  if (!Live)
960
1
    return 0;
961
90
962
90
  // Initialize OffsetMap lazily.
963
90
  llvm::call_once(InitOffsetMap, [&] {
964
41
    OffsetMap.reserve(Pieces.size());
965
131
    for (size_t I = 0; I < Pieces.size(); 
++I90
)
966
90
      OffsetMap[Pieces[I].InputOff] = I;
967
41
  });
968
90
969
90
  // Find a string starting at a given offset.
970
90
  auto It = OffsetMap.find(Offset);
971
90
  if (It != OffsetMap.end())
972
85
    return Pieces[It->second].OutputOff;
973
5
974
5
  // If Offset is not at beginning of a section piece, it is not in the map.
975
5
  // In that case we need to search from the original section piece vector.
976
5
  const SectionPiece &Piece = *getSectionPiece(Offset);
977
5
  if (!Piece.Live)
978
0
    return 0;
979
5
980
5
  uint64_t Addend = Offset - Piece.InputOff;
981
5
  return Piece.OutputOff + Addend;
982
5
}
983
984
template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,
985
                                    StringRef);
986
template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &,
987
                                    StringRef);
988
template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,
989
                                    StringRef);
990
template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,
991
                                    StringRef);
992
993
template std::string InputSectionBase::getLocation<ELF32LE>(uint64_t);
994
template std::string InputSectionBase::getLocation<ELF32BE>(uint64_t);
995
template std::string InputSectionBase::getLocation<ELF64LE>(uint64_t);
996
template std::string InputSectionBase::getLocation<ELF64BE>(uint64_t);
997
998
template void InputSection::writeTo<ELF32LE>(uint8_t *);
999
template void InputSection::writeTo<ELF32BE>(uint8_t *);
1000
template void InputSection::writeTo<ELF64LE>(uint8_t *);
1001
template void InputSection::writeTo<ELF64BE>(uint8_t *);
1002
1003
template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &,
1004
                                              const ELF32LE::Shdr &, StringRef);
1005
template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &,
1006
                                              const ELF32BE::Shdr &, StringRef);
1007
template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &,
1008
                                              const ELF64LE::Shdr &, StringRef);
1009
template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &,
1010
                                              const ELF64BE::Shdr &, StringRef);
1011
1012
template EhInputSection::EhInputSection(ObjFile<ELF32LE> &,
1013
                                        const ELF32LE::Shdr &, StringRef);
1014
template EhInputSection::EhInputSection(ObjFile<ELF32BE> &,
1015
                                        const ELF32BE::Shdr &, StringRef);
1016
template EhInputSection::EhInputSection(ObjFile<ELF64LE> &,
1017
                                        const ELF64LE::Shdr &, StringRef);
1018
template EhInputSection::EhInputSection(ObjFile<ELF64BE> &,
1019
                                        const ELF64BE::Shdr &, StringRef);
1020
1021
template void EhInputSection::split<ELF32LE>();
1022
template void EhInputSection::split<ELF32BE>();
1023
template void EhInputSection::split<ELF64LE>();
1024
template void EhInputSection::split<ELF64BE>();