Coverage Report

Created: 2017-09-21 03:39

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
14
#include "InputFiles.h"
15
#include "LinkerScript.h"
16
#include "Memory.h"
17
#include "OutputSections.h"
18
#include "Relocations.h"
19
#include "SyntheticSections.h"
20
#include "Target.h"
21
#include "Thunks.h"
22
#include "llvm/Object/Decompressor.h"
23
#include "llvm/Support/Compiler.h"
24
#include "llvm/Support/Compression.h"
25
#include "llvm/Support/Endian.h"
26
#include "llvm/Support/Path.h"
27
#include "llvm/Support/Threading.h"
28
#include <mutex>
29
30
using namespace llvm;
31
using namespace llvm::ELF;
32
using namespace llvm::object;
33
using namespace llvm::support;
34
using namespace llvm::support::endian;
35
using namespace llvm::sys;
36
37
using namespace lld;
38
using namespace lld::elf;
39
40
std::vector<InputSectionBase *> elf::InputSections;
41
42
// Returns a string to construct an error message.
43
41
std::string lld::toString(const InputSectionBase *Sec) {
44
41
  return (toString(Sec->File) + ":(" + Sec->Name + ")").str();
45
41
}
46
47
6
DenseMap<SectionBase *, int> elf::buildSectionOrder() {
48
6
  // Build a map from symbols to their priorities. Symbols that didn't
49
6
  // appear in the symbol ordering file have the lowest priority 0.
50
6
  // All explicitly mentioned symbols have negative (higher) priorities.
51
6
  DenseMap<StringRef, int> SymbolOrder;
52
6
  int Priority = -Config->SymbolOrderingFile.size();
53
6
  for (StringRef S : Config->SymbolOrderingFile)
54
17
    SymbolOrder.insert({S, Priority++});
55
6
56
6
  // Build a map from sections to their priorities.
57
6
  DenseMap<SectionBase *, int> SectionOrder;
58
8
  for (InputFile *File : ObjectFiles) {
59
33
    for (SymbolBody *Body : File->getSymbols()) {
60
33
      auto *D = dyn_cast<DefinedRegular>(Body);
61
33
      if (
!D || 33
!D->Section25
)
62
10
        continue;
63
23
      int &Priority = SectionOrder[D->Section];
64
23
      Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
65
23
    }
66
8
  }
67
6
  return SectionOrder;
68
6
}
69
70
template <class ELFT>
71
static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> *File,
72
201k
                                            const typename ELFT::Shdr *Hdr) {
73
201k
  if (
!File || 201k
Hdr->sh_type == SHT_NOBITS200k
)
74
1.83k
    return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
75
200k
  return check(File->getObj().getSectionContents(Hdr));
76
200k
}
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
72
279
                                            const typename ELFT::Shdr *Hdr) {
73
279
  if (
!File || 279
Hdr->sh_type == SHT_NOBITS236
)
74
77
    return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
75
202
  return check(File->getObj().getSectionContents(Hdr));
76
202
}
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
72
766
                                            const typename ELFT::Shdr *Hdr) {
73
766
  if (
!File || 766
Hdr->sh_type == SHT_NOBITS679
)
74
211
    return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
75
555
  return check(File->getObj().getSectionContents(Hdr));
76
555
}
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
72
200k
                                            const typename ELFT::Shdr *Hdr) {
73
200k
  if (
!File || 200k
Hdr->sh_type == SHT_NOBITS198k
)
74
1.31k
    return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
75
198k
  return check(File->getObj().getSectionContents(Hdr));
76
198k
}
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
72
721
                                            const typename ELFT::Shdr *Hdr) {
73
721
  if (
!File || 721
Hdr->sh_type == SHT_NOBITS536
)
74
226
    return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
75
495
  return check(File->getObj().getSectionContents(Hdr));
76
495
}
77
78
// Return true if a section with given section flags is live (will never be
79
// GCed) by default. If a section can be GCed, this function returns false.
80
232k
static bool isLiveByDefault(uint64_t Flags, uint32_t Type) {
81
232k
  // If GC is enabled, all memory-mapped sections are subject of GC.
82
232k
  if (!Config->GcSections)
83
231k
    return true;
84
1.38k
  
if (1.38k
Flags & SHF_ALLOC1.38k
)
85
1.06k
    return false;
86
315
87
315
  // Besides that, relocation sections can also be GCed because their
88
315
  // relocation target sections may be GCed. This doesn't really matter
89
315
  // in most cases because the linker usually consumes relocation
90
315
  // sections instead of emitting them, but -emit-reloc needs this.
91
315
  
return Type != SHT_REL && 315
Type != SHT_RELA315
;
92
232k
}
93
94
InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags,
95
                                   uint32_t Type, uint64_t Entsize,
96
                                   uint32_t Link, uint32_t Info,
97
                                   uint32_t Alignment, ArrayRef<uint8_t> Data,
98
                                   StringRef Name, Kind SectionKind)
99
    : SectionBase(SectionKind, Name, Flags, Entsize, Alignment, Type, Info,
100
                  Link),
101
232k
      File(File), Data(Data), Repl(this) {
102
232k
  Live = isLiveByDefault(Flags, Type);
103
232k
  Assigned = false;
104
232k
  NumRelocations = 0;
105
232k
  AreRelocsRela = false;
106
232k
107
232k
  // The ELF spec states that a value of 0 means the section has
108
232k
  // no alignment constraits.
109
232k
  uint32_t V = std::max<uint64_t>(Alignment, 1);
110
232k
  if (!isPowerOf2_64(V))
111
0
    fatal(toString(File) + ": section sh_addralign is not a power of 2");
112
232k
  this->Alignment = V;
113
232k
}
114
115
// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
116
// SHF_GROUP is a marker that a section belongs to some comdat group.
117
// That flag doesn't make sense in an executable.
118
201k
static uint64_t getFlags(uint64_t Flags) {
119
201k
  Flags &= ~(uint64_t)SHF_INFO_LINK;
120
201k
  if (!Config->Relocatable)
121
201k
    Flags &= ~(uint64_t)SHF_GROUP;
122
201k
  return Flags;
123
201k
}
124
125
// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
126
// March 2017) fail to infer section types for sections starting with
127
// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of
128
// SHF_INIT_ARRAY. As a result, the following assembler directive
129
// creates ".init_array.100" with SHT_PROGBITS, for example.
130
//
131
//   .section .init_array.100, "aw"
132
//
133
// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
134
// incorrect inputs as if they were correct from the beginning.
135
201k
static uint64_t getType(uint64_t Type, StringRef Name) {
136
201k
  if (
Type == SHT_PROGBITS && 201k
Name.startswith(".init_array.")201k
)
137
1
    return SHT_INIT_ARRAY;
138
201k
  
if (201k
Type == SHT_PROGBITS && 201k
Name.startswith(".fini_array.")201k
)
139
1
    return SHT_FINI_ARRAY;
140
201k
  return Type;
141
201k
}
142
143
template <class ELFT>
144
InputSectionBase::InputSectionBase(ObjFile<ELFT> *File,
145
                                   const typename ELFT::Shdr *Hdr,
146
                                   StringRef Name, Kind SectionKind)
147
    : InputSectionBase(File, getFlags(Hdr->sh_flags),
148
                       getType(Hdr->sh_type, Name), Hdr->sh_entsize,
149
                       Hdr->sh_link, Hdr->sh_info, Hdr->sh_addralign,
150
201k
                       getSectionContents(File, Hdr), Name, SectionKind) {
151
201k
  // We reject object files having insanely large alignments even though
152
201k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
153
201k
  // We might want to relax this in the future.
154
201k
  if (Hdr->sh_addralign > UINT32_MAX)
155
0
    fatal(toString(File) + ": section sh_addralign is too large");
156
201k
}
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
150
279
                       getSectionContents(File, Hdr), Name, SectionKind) {
151
279
  // We reject object files having insanely large alignments even though
152
279
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
153
279
  // We might want to relax this in the future.
154
279
  if (Hdr->sh_addralign > UINT32_MAX)
155
0
    fatal(toString(File) + ": section sh_addralign is too large");
156
279
}
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
150
200k
                       getSectionContents(File, Hdr), Name, SectionKind) {
151
200k
  // We reject object files having insanely large alignments even though
152
200k
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
153
200k
  // We might want to relax this in the future.
154
200k
  if (Hdr->sh_addralign > UINT32_MAX)
155
0
    fatal(toString(File) + ": section sh_addralign is too large");
156
200k
}
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
150
766
                       getSectionContents(File, Hdr), Name, SectionKind) {
151
766
  // We reject object files having insanely large alignments even though
152
766
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
153
766
  // We might want to relax this in the future.
154
766
  if (Hdr->sh_addralign > UINT32_MAX)
155
0
    fatal(toString(File) + ": section sh_addralign is too large");
156
766
}
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
150
721
                       getSectionContents(File, Hdr), Name, SectionKind) {
151
721
  // We reject object files having insanely large alignments even though
152
721
  // they are allowed by the spec. I think 4GB is a reasonable limitation.
153
721
  // We might want to relax this in the future.
154
721
  if (Hdr->sh_addralign > UINT32_MAX)
155
0
    fatal(toString(File) + ": section sh_addralign is too large");
156
721
}
157
158
656k
size_t InputSectionBase::getSize() const {
159
656k
  if (auto *S = dyn_cast<SyntheticSection>(this))
160
53.2k
    return S->getSize();
161
603k
162
603k
  return Data.size();
163
603k
}
164
165
198
uint64_t InputSectionBase::getOffsetInFile() const {
166
198
  const uint8_t *FileStart = (const uint8_t *)File->MB.getBufferStart();
167
198
  const uint8_t *SecStart = Data.begin();
168
198
  return SecStart - FileStart;
169
198
}
170
171
10.1k
uint64_t SectionBase::getOffset(uint64_t Offset) const {
172
10.1k
  switch (kind()) {
173
667
  case Output: {
174
667
    auto *OS = cast<OutputSection>(this);
175
667
    // For output sections we treat offset -1 as the end of the section.
176
667
    return Offset == uint64_t(-1) ? 
OS->Size50
:
Offset617
;
177
10.1k
  }
178
7.80k
  case Regular:
179
7.80k
    return cast<InputSection>(this)->OutSecOff + Offset;
180
1.46k
  case Synthetic: {
181
1.46k
    auto *IS = cast<InputSection>(this);
182
1.46k
    // For synthetic sections we treat offset -1 as the end of the section.
183
1.46k
    return IS->OutSecOff + (Offset == uint64_t(-1) ? 
IS->getSize()24
:
Offset1.44k
);
184
10.1k
  }
185
115
  case EHFrame:
186
115
    // The file crtbeginT.o has relocations pointing to the start of an empty
187
115
    // .eh_frame that is known to be the first in the link. It does that to
188
115
    // identify the start of the output .eh_frame.
189
115
    return Offset;
190
66
  case Merge:
191
66
    const MergeInputSection *MS = cast<MergeInputSection>(this);
192
66
    if (InputSection *IS = MS->getParent())
193
65
      return IS->OutSecOff + MS->getOffset(Offset);
194
1
    return MS->getOffset(Offset);
195
0
  }
196
0
  
llvm_unreachable0
("invalid section kind");
197
0
}
198
199
15.5k
OutputSection *SectionBase::getOutputSection() {
200
15.5k
  InputSection *Sec;
201
15.5k
  if (auto *IS = dyn_cast<InputSection>(this))
202
14.3k
    Sec = cast<InputSection>(IS->Repl);
203
1.14k
  else 
if (auto *1.14k
MS1.14k
= dyn_cast<MergeInputSection>(this))
204
87
    Sec = MS->getParent();
205
1.06k
  else 
if (auto *1.06k
EH1.06k
= dyn_cast<EhInputSection>(this))
206
117
    Sec = EH->getParent();
207
1.06k
  else
208
945
    return cast<OutputSection>(this);
209
14.5k
  
return Sec ? 14.5k
Sec->getParent()14.5k
:
nullptr0
;
210
15.5k
}
211
212
// Uncompress section contents. Note that this function is called
213
// from parallelForEach, so it must be thread-safe.
214
3
void InputSectionBase::uncompress() {
215
3
  Decompressor Dec = check(Decompressor::create(Name, toStringRef(Data),
216
3
                                                Config->IsLE, Config->Is64));
217
3
218
3
  size_t Size = Dec.getDecompressedSize();
219
3
  UncompressBuf.reset(new char[Size]());
220
3
  if (Error E = Dec.decompress({UncompressBuf.get(), Size}))
221
0
    fatal(toString(this) +
222
0
          ": decompress failed: " + llvm::toString(std::move(E)));
223
3
224
3
  this->Data = makeArrayRef((uint8_t *)UncompressBuf.get(), Size);
225
3
  this->Flags &= ~(uint64_t)SHF_COMPRESSED;
226
3
}
227
228
0
uint64_t SectionBase::getOffset(const DefinedRegular &Sym) const {
229
0
  return getOffset(Sym.Value);
230
0
}
231
232
112
InputSection *InputSectionBase::getLinkOrderDep() const {
233
112
  if (
(Flags & SHF_LINK_ORDER) && 112
Link != 0112
) {
234
112
    InputSectionBase *L = File->getSections()[Link];
235
112
    if (auto *IS = dyn_cast<InputSection>(L))
236
111
      return IS;
237
1
    error(
238
1
        "Merge and .eh_frame sections are not supported with SHF_LINK_ORDER " +
239
1
        toString(L));
240
1
  }
241
1
  return nullptr;
242
112
}
243
244
// Returns a source location string. Used to construct an error message.
245
template <class ELFT>
246
40
std::string InputSectionBase::getLocation(uint64_t Offset) {
247
40
  // We don't have file for synthetic sections.
248
40
  if (getFile<ELFT>() == nullptr)
249
1
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
250
1
        .str();
251
39
252
39
  // First check if we can get desired values from debugging information.
253
39
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
254
39
  if (!LineInfo.empty())
255
0
    return LineInfo;
256
39
257
39
  // File->SourceFile contains STT_FILE symbol that contains a
258
39
  // source file name. If it's missing, we use an object file name.
259
39
  std::string SrcFile = getFile<ELFT>()->SourceFile;
260
39
  if (SrcFile.empty())
261
39
    SrcFile = toString(File);
262
39
263
39
  // Find a function symbol that encloses a given location.
264
39
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
265
161
    
if (auto *161
D161
= dyn_cast<DefinedRegular>(B))
266
121
      
if (121
D->Section == this && 121
D->Type == STT_FUNC43
)
267
10
        
if (10
D->Value <= Offset && 10
Offset < D->Value + D->Size9
)
268
0
          return SrcFile + ":(function " + toString(*D) + ")";
269
39
270
39
  // If there's no symbol, print out the offset in the section.
271
39
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
272
39
}
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
246
1
std::string InputSectionBase::getLocation(uint64_t Offset) {
247
1
  // We don't have file for synthetic sections.
248
1
  if (getFile<ELFT>() == nullptr)
249
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
250
0
        .str();
251
1
252
1
  // First check if we can get desired values from debugging information.
253
1
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
254
1
  if (!LineInfo.empty())
255
0
    return LineInfo;
256
1
257
1
  // File->SourceFile contains STT_FILE symbol that contains a
258
1
  // source file name. If it's missing, we use an object file name.
259
1
  std::string SrcFile = getFile<ELFT>()->SourceFile;
260
1
  if (SrcFile.empty())
261
1
    SrcFile = toString(File);
262
1
263
1
  // Find a function symbol that encloses a given location.
264
1
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
265
2
    
if (auto *2
D2
= dyn_cast<DefinedRegular>(B))
266
1
      
if (1
D->Section == this && 1
D->Type == STT_FUNC0
)
267
0
        
if (0
D->Value <= Offset && 0
Offset < D->Value + D->Size0
)
268
0
          return SrcFile + ":(function " + toString(*D) + ")";
269
1
270
1
  // If there's no symbol, print out the offset in the section.
271
1
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
272
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, false> >(unsigned long long)
Line
Count
Source
246
10
std::string InputSectionBase::getLocation(uint64_t Offset) {
247
10
  // We don't have file for synthetic sections.
248
10
  if (getFile<ELFT>() == nullptr)
249
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
250
0
        .str();
251
10
252
10
  // First check if we can get desired values from debugging information.
253
10
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
254
10
  if (!LineInfo.empty())
255
0
    return LineInfo;
256
10
257
10
  // File->SourceFile contains STT_FILE symbol that contains a
258
10
  // source file name. If it's missing, we use an object file name.
259
10
  std::string SrcFile = getFile<ELFT>()->SourceFile;
260
10
  if (SrcFile.empty())
261
10
    SrcFile = toString(File);
262
10
263
10
  // Find a function symbol that encloses a given location.
264
10
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
265
45
    
if (auto *45
D45
= dyn_cast<DefinedRegular>(B))
266
35
      
if (35
D->Section == this && 35
D->Type == STT_FUNC13
)
267
6
        
if (6
D->Value <= Offset && 6
Offset < D->Value + D->Size5
)
268
0
          return SrcFile + ":(function " + toString(*D) + ")";
269
10
270
10
  // If there's no symbol, print out the offset in the section.
271
10
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
272
10
}
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
246
1
std::string InputSectionBase::getLocation(uint64_t Offset) {
247
1
  // We don't have file for synthetic sections.
248
1
  if (getFile<ELFT>() == nullptr)
249
0
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
250
0
        .str();
251
1
252
1
  // First check if we can get desired values from debugging information.
253
1
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
254
1
  if (!LineInfo.empty())
255
0
    return LineInfo;
256
1
257
1
  // File->SourceFile contains STT_FILE symbol that contains a
258
1
  // source file name. If it's missing, we use an object file name.
259
1
  std::string SrcFile = getFile<ELFT>()->SourceFile;
260
1
  if (SrcFile.empty())
261
1
    SrcFile = toString(File);
262
1
263
1
  // Find a function symbol that encloses a given location.
264
1
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
265
3
    
if (auto *3
D3
= dyn_cast<DefinedRegular>(B))
266
2
      
if (2
D->Section == this && 2
D->Type == STT_FUNC1
)
267
0
        
if (0
D->Value <= Offset && 0
Offset < D->Value + D->Size0
)
268
0
          return SrcFile + ":(function " + toString(*D) + ")";
269
1
270
1
  // If there's no symbol, print out the offset in the section.
271
1
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
272
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
246
28
std::string InputSectionBase::getLocation(uint64_t Offset) {
247
28
  // We don't have file for synthetic sections.
248
28
  if (getFile<ELFT>() == nullptr)
249
1
    return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")")
250
1
        .str();
251
27
252
27
  // First check if we can get desired values from debugging information.
253
27
  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
254
27
  if (!LineInfo.empty())
255
0
    return LineInfo;
256
27
257
27
  // File->SourceFile contains STT_FILE symbol that contains a
258
27
  // source file name. If it's missing, we use an object file name.
259
27
  std::string SrcFile = getFile<ELFT>()->SourceFile;
260
27
  if (SrcFile.empty())
261
27
    SrcFile = toString(File);
262
27
263
27
  // Find a function symbol that encloses a given location.
264
27
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
265
111
    
if (auto *111
D111
= dyn_cast<DefinedRegular>(B))
266
83
      
if (83
D->Section == this && 83
D->Type == STT_FUNC29
)
267
4
        
if (4
D->Value <= Offset && 4
Offset < D->Value + D->Size4
)
268
0
          return SrcFile + ":(function " + toString(*D) + ")";
269
27
270
27
  // If there's no symbol, print out the offset in the section.
271
27
  return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
272
27
}
273
274
// Returns a source location string. This function is intended to be
275
// used for constructing an error message. The returned message looks
276
// like this:
277
//
278
//   foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)
279
//
280
// Returns an empty string if there's no way to get line info.
281
130
template <class ELFT> std::string InputSectionBase::getSrcMsg(uint64_t Offset) {
282
130
  // Synthetic sections don't have input files.
283
130
  ObjFile<ELFT> *File = getFile<ELFT>();
284
130
  if (!File)
285
2
    return "";
286
128
287
128
  Optional<DILineInfo> Info = File->getDILineInfo(this, Offset);
288
128
289
128
  // File->SourceFile contains STT_FILE symbol, and that is a last resort.
290
128
  if (!Info)
291
117
    return File->SourceFile;
292
11
293
11
  std::string Path = Info->FileName;
294
11
  std::string Filename = path::filename(Path);
295
11
  std::string Lineno = ":" + std::to_string(Info->Line);
296
11
  if (Filename == Path)
297
2
    return Filename + Lineno;
298
9
  return Filename + Lineno + " (" + Path + Lineno + ")";
299
9
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getSrcMsg<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned long long)
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getSrcMsg<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned long long)
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getSrcMsg<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned long long)
Line
Count
Source
281
125
template <class ELFT> std::string InputSectionBase::getSrcMsg(uint64_t Offset) {
282
125
  // Synthetic sections don't have input files.
283
125
  ObjFile<ELFT> *File = getFile<ELFT>();
284
125
  if (!File)
285
2
    return "";
286
123
287
123
  Optional<DILineInfo> Info = File->getDILineInfo(this, Offset);
288
123
289
123
  // File->SourceFile contains STT_FILE symbol, and that is a last resort.
290
123
  if (!Info)
291
112
    return File->SourceFile;
292
11
293
11
  std::string Path = Info->FileName;
294
11
  std::string Filename = path::filename(Path);
295
11
  std::string Lineno = ":" + std::to_string(Info->Line);
296
11
  if (Filename == Path)
297
2
    return Filename + Lineno;
298
9
  return Filename + Lineno + " (" + Path + Lineno + ")";
299
9
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getSrcMsg<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned long long)
Line
Count
Source
281
5
template <class ELFT> std::string InputSectionBase::getSrcMsg(uint64_t Offset) {
282
5
  // Synthetic sections don't have input files.
283
5
  ObjFile<ELFT> *File = getFile<ELFT>();
284
5
  if (!File)
285
0
    return "";
286
5
287
5
  Optional<DILineInfo> Info = File->getDILineInfo(this, Offset);
288
5
289
5
  // File->SourceFile contains STT_FILE symbol, and that is a last resort.
290
5
  if (!Info)
291
5
    return File->SourceFile;
292
0
293
0
  std::string Path = Info->FileName;
294
0
  std::string Filename = path::filename(Path);
295
0
  std::string Lineno = ":" + std::to_string(Info->Line);
296
0
  if (Filename == Path)
297
0
    return Filename + Lineno;
298
0
  return Filename + Lineno + " (" + Path + Lineno + ")";
299
0
}
300
301
// Returns a filename string along with an optional section name. This
302
// function is intended to be used for constructing an error
303
// message. The returned message looks like this:
304
//
305
//   path/to/foo.o:(function bar)
306
//
307
// or
308
//
309
//   path/to/foo.o:(function bar) in archive path/to/bar.a
310
130
template <class ELFT> std::string InputSectionBase::getObjMsg(uint64_t Off) {
311
130
  // Synthetic sections don't have input files.
312
130
  ObjFile<ELFT> *File = getFile<ELFT>();
313
130
  if (!File)
314
2
    return ("(internal):(" + Name + "+0x" + utohexstr(Off) + ")").str();
315
128
  std::string Filename = File->getName();
316
128
317
128
  std::string Archive;
318
128
  if (!File->ArchiveName.empty())
319
6
    Archive = (" in archive " + File->ArchiveName).str();
320
128
321
128
  // Find a symbol that encloses a given location.
322
128
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
323
468
    
if (auto *468
D468
= dyn_cast<DefinedRegular>(B))
324
199
      
if (199
D->Section == this && 199
D->Value <= Off128
&&
Off < D->Value + D->Size119
)
325
7
        return Filename + ":(" + toString(*D) + ")" + Archive;
326
121
327
121
  // If there's no symbol, print out the offset in the section.
328
121
  return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive)
329
121
      .str();
330
121
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getObjMsg<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned long long)
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getObjMsg<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned long long)
Line
Count
Source
310
125
template <class ELFT> std::string InputSectionBase::getObjMsg(uint64_t Off) {
311
125
  // Synthetic sections don't have input files.
312
125
  ObjFile<ELFT> *File = getFile<ELFT>();
313
125
  if (!File)
314
2
    return ("(internal):(" + Name + "+0x" + utohexstr(Off) + ")").str();
315
123
  std::string Filename = File->getName();
316
123
317
123
  std::string Archive;
318
123
  if (!File->ArchiveName.empty())
319
6
    Archive = (" in archive " + File->ArchiveName).str();
320
123
321
123
  // Find a symbol that encloses a given location.
322
123
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
323
443
    
if (auto *443
D443
= dyn_cast<DefinedRegular>(B))
324
179
      
if (179
D->Section == this && 179
D->Value <= Off114
&&
Off < D->Value + D->Size109
)
325
7
        return Filename + ":(" + toString(*D) + ")" + Archive;
326
116
327
116
  // If there's no symbol, print out the offset in the section.
328
116
  return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive)
329
116
      .str();
330
116
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getObjMsg<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned long long)
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > lld::elf::InputSectionBase::getObjMsg<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned long long)
Line
Count
Source
310
5
template <class ELFT> std::string InputSectionBase::getObjMsg(uint64_t Off) {
311
5
  // Synthetic sections don't have input files.
312
5
  ObjFile<ELFT> *File = getFile<ELFT>();
313
5
  if (!File)
314
0
    return ("(internal):(" + Name + "+0x" + utohexstr(Off) + ")").str();
315
5
  std::string Filename = File->getName();
316
5
317
5
  std::string Archive;
318
5
  if (!File->ArchiveName.empty())
319
0
    Archive = (" in archive " + File->ArchiveName).str();
320
5
321
5
  // Find a symbol that encloses a given location.
322
5
  for (SymbolBody *B : getFile<ELFT>()->getSymbols())
323
25
    
if (auto *25
D25
= dyn_cast<DefinedRegular>(B))
324
20
      
if (20
D->Section == this && 20
D->Value <= Off14
&&
Off < D->Value + D->Size10
)
325
0
        return Filename + ":(" + toString(*D) + ")" + Archive;
326
5
327
5
  // If there's no symbol, print out the offset in the section.
328
5
  return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive)
329
5
      .str();
330
5
}
331
332
InputSectionBase InputSectionBase::Discarded;
333
334
InputSection::InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
335
                           ArrayRef<uint8_t> Data, StringRef Name, Kind K)
336
    : InputSectionBase(nullptr, Flags, Type,
337
                       /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, Alignment, Data,
338
30.6k
                       Name, K) {}
339
340
template <class ELFT>
341
InputSection::InputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header,
342
                           StringRef Name)
343
200k
    : 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
343
529
    : 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
343
233
    : 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
343
198k
    : 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
343
677
    : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {}
344
345
243k
bool InputSection::classof(const SectionBase *S) {
346
243k
  return S->kind() == SectionBase::Regular ||
347
31.1k
         S->kind() == SectionBase::Synthetic;
348
243k
}
349
350
11.9k
bool InputSectionBase::classof(const SectionBase *S) {
351
11.9k
  return S->kind() != Output;
352
11.9k
}
353
354
104k
OutputSection *InputSection::getParent() const {
355
104k
  return cast_or_null<OutputSection>(Parent);
356
104k
}
357
358
// Copy SHT_GROUP section contents. Used only for the -r option.
359
8
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
360
8
  // ELFT::Word is the 32-bit integral type in the target endianness.
361
8
  typedef typename ELFT::Word u32;
362
8
  ArrayRef<u32> From = getDataAs<u32>();
363
8
  auto *To = reinterpret_cast<u32 *>(Buf);
364
8
365
8
  // The first entry is not a section number but a flag.
366
8
  *To++ = From[0];
367
8
368
8
  // Adjust section numbers because section numbers in an input object
369
8
  // files are different in the output.
370
8
  ArrayRef<InputSectionBase *> Sections = this->File->getSections();
371
8
  for (uint32_t Idx : From.slice(1))
372
11
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
373
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, true> >(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
359
8
template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
360
8
  // ELFT::Word is the 32-bit integral type in the target endianness.
361
8
  typedef typename ELFT::Word u32;
362
8
  ArrayRef<u32> From = getDataAs<u32>();
363
8
  auto *To = reinterpret_cast<u32 *>(Buf);
364
8
365
8
  // The first entry is not a section number but a flag.
366
8
  *To++ = From[0];
367
8
368
8
  // Adjust section numbers because section numbers in an input object
369
8
  // files are different in the output.
370
8
  ArrayRef<InputSectionBase *> Sections = this->File->getSections();
371
8
  for (uint32_t Idx : From.slice(1))
372
11
    *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
373
8
}
374
375
182
InputSectionBase *InputSection::getRelocatedSection() {
376
182
  assert(this->Type == SHT_RELA || this->Type == SHT_REL);
377
182
  ArrayRef<InputSectionBase *> Sections = this->File->getSections();
378
182
  return Sections[this->Info];
379
182
}
380
381
// This is used for -r and --emit-relocs. We can't use memcpy to copy
382
// relocations because we need to update symbol table offset and section index
383
// for each relocation. So we copy relocations one by one.
384
template <class ELFT, class RelTy>
385
66
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
386
66
  InputSectionBase *RelocatedSection = getRelocatedSection();
387
66
388
66
  // Loop is slow and have complexity O(N*M), where N - amount of
389
66
  // relocations and M - amount of symbols in symbol table.
390
66
  // That happens because getSymbolIndex(...) call below performs
391
66
  // simple linear search.
392
101
  for (const RelTy &Rel : Rels) {
393
101
    uint32_t Type = Rel.getType(Config->IsMips64EL);
394
101
    SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel);
395
101
396
101
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
397
101
    Buf += sizeof(RelTy);
398
101
399
101
    if (Config->IsRela)
400
76
      P->r_addend = getAddend<ELFT>(Rel);
401
101
402
101
    // Output section VA is zero for -r, so r_offset is an offset within the
403
101
    // section, but for --emit-relocs it is an virtual address.
404
101
    P->r_offset = RelocatedSection->getOutputSection()->Addr +
405
101
                  RelocatedSection->getOffset(Rel.r_offset);
406
101
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Body), Type,
407
101
                        Config->IsMips64EL);
408
101
409
101
    if (
Body.Type == STT_SECTION101
) {
410
47
      // We combine multiple section symbols into only one per
411
47
      // section. This means we have to update the addend. That is
412
47
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
413
47
      // section data. We do that by adding to the Relocation vector.
414
47
415
47
      // .eh_frame is horribly special and can reference discarded sections. To
416
47
      // avoid having to parse and recreate .eh_frame, we just replace any
417
47
      // relocation in it pointing to discarded sections with R_*_NONE, which
418
47
      // hopefully creates a frame that is ignored at runtime.
419
47
      SectionBase *Section = cast<DefinedRegular>(Body).Section;
420
47
      if (
Section == &InputSection::Discarded47
) {
421
2
        P->setSymbolAndType(0, 0, false);
422
2
        continue;
423
2
      }
424
45
425
45
      
if (45
Config->IsRela45
) {
426
25
        P->r_addend += Body.getVA() - Section->getOutputSection()->Addr;
427
45
      } else 
if (20
Config->Relocatable20
) {
428
20
        const uint8_t *BufLoc = RelocatedSection->Data.begin() + Rel.r_offset;
429
20
        RelocatedSection->Relocations.push_back(
430
20
            {R_ABS, Type, Rel.r_offset, Target->getImplicitAddend(BufLoc, Type),
431
20
             &Body});
432
20
      }
433
47
    }
434
101
435
101
  }
436
66
}
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
385
6
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
386
6
  InputSectionBase *RelocatedSection = getRelocatedSection();
387
6
388
6
  // Loop is slow and have complexity O(N*M), where N - amount of
389
6
  // relocations and M - amount of symbols in symbol table.
390
6
  // That happens because getSymbolIndex(...) call below performs
391
6
  // simple linear search.
392
9
  for (const RelTy &Rel : Rels) {
393
9
    uint32_t Type = Rel.getType(Config->IsMips64EL);
394
9
    SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel);
395
9
396
9
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
397
9
    Buf += sizeof(RelTy);
398
9
399
9
    if (Config->IsRela)
400
0
      P->r_addend = getAddend<ELFT>(Rel);
401
9
402
9
    // Output section VA is zero for -r, so r_offset is an offset within the
403
9
    // section, but for --emit-relocs it is an virtual address.
404
9
    P->r_offset = RelocatedSection->getOutputSection()->Addr +
405
9
                  RelocatedSection->getOffset(Rel.r_offset);
406
9
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Body), Type,
407
9
                        Config->IsMips64EL);
408
9
409
9
    if (
Body.Type == STT_SECTION9
) {
410
7
      // We combine multiple section symbols into only one per
411
7
      // section. This means we have to update the addend. That is
412
7
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
413
7
      // section data. We do that by adding to the Relocation vector.
414
7
415
7
      // .eh_frame is horribly special and can reference discarded sections. To
416
7
      // avoid having to parse and recreate .eh_frame, we just replace any
417
7
      // relocation in it pointing to discarded sections with R_*_NONE, which
418
7
      // hopefully creates a frame that is ignored at runtime.
419
7
      SectionBase *Section = cast<DefinedRegular>(Body).Section;
420
7
      if (
Section == &InputSection::Discarded7
) {
421
0
        P->setSymbolAndType(0, 0, false);
422
0
        continue;
423
0
      }
424
7
425
7
      
if (7
Config->IsRela7
) {
426
0
        P->r_addend += Body.getVA() - Section->getOutputSection()->Addr;
427
7
      } else 
if (7
Config->Relocatable7
) {
428
7
        const uint8_t *BufLoc = RelocatedSection->Data.begin() + Rel.r_offset;
429
7
        RelocatedSection->Relocations.push_back(
430
7
            {R_ABS, Type, Rel.r_offset, Target->getImplicitAddend(BufLoc, Type),
431
7
             &Body});
432
7
      }
433
7
    }
434
9
435
9
  }
436
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
385
48
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
386
48
  InputSectionBase *RelocatedSection = getRelocatedSection();
387
48
388
48
  // Loop is slow and have complexity O(N*M), where N - amount of
389
48
  // relocations and M - amount of symbols in symbol table.
390
48
  // That happens because getSymbolIndex(...) call below performs
391
48
  // simple linear search.
392
77
  for (const RelTy &Rel : Rels) {
393
77
    uint32_t Type = Rel.getType(Config->IsMips64EL);
394
77
    SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel);
395
77
396
77
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
397
77
    Buf += sizeof(RelTy);
398
77
399
77
    if (Config->IsRela)
400
76
      P->r_addend = getAddend<ELFT>(Rel);
401
77
402
77
    // Output section VA is zero for -r, so r_offset is an offset within the
403
77
    // section, but for --emit-relocs it is an virtual address.
404
77
    P->r_offset = RelocatedSection->getOutputSection()->Addr +
405
77
                  RelocatedSection->getOffset(Rel.r_offset);
406
77
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Body), Type,
407
77
                        Config->IsMips64EL);
408
77
409
77
    if (
Body.Type == STT_SECTION77
) {
410
27
      // We combine multiple section symbols into only one per
411
27
      // section. This means we have to update the addend. That is
412
27
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
413
27
      // section data. We do that by adding to the Relocation vector.
414
27
415
27
      // .eh_frame is horribly special and can reference discarded sections. To
416
27
      // avoid having to parse and recreate .eh_frame, we just replace any
417
27
      // relocation in it pointing to discarded sections with R_*_NONE, which
418
27
      // hopefully creates a frame that is ignored at runtime.
419
27
      SectionBase *Section = cast<DefinedRegular>(Body).Section;
420
27
      if (
Section == &InputSection::Discarded27
) {
421
2
        P->setSymbolAndType(0, 0, false);
422
2
        continue;
423
2
      }
424
25
425
25
      
if (25
Config->IsRela25
) {
426
25
        P->r_addend += Body.getVA() - Section->getOutputSection()->Addr;
427
25
      } else 
if (0
Config->Relocatable0
) {
428
0
        const uint8_t *BufLoc = RelocatedSection->Data.begin() + Rel.r_offset;
429
0
        RelocatedSection->Relocations.push_back(
430
0
            {R_ABS, Type, Rel.r_offset, Target->getImplicitAddend(BufLoc, Type),
431
0
             &Body});
432
0
      }
433
27
    }
434
77
435
77
  }
436
48
}
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> >)
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)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
385
12
void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
386
12
  InputSectionBase *RelocatedSection = getRelocatedSection();
387
12
388
12
  // Loop is slow and have complexity O(N*M), where N - amount of
389
12
  // relocations and M - amount of symbols in symbol table.
390
12
  // That happens because getSymbolIndex(...) call below performs
391
12
  // simple linear search.
392
15
  for (const RelTy &Rel : Rels) {
393
15
    uint32_t Type = Rel.getType(Config->IsMips64EL);
394
15
    SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel);
395
15
396
15
    auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf);
397
15
    Buf += sizeof(RelTy);
398
15
399
15
    if (Config->IsRela)
400
0
      P->r_addend = getAddend<ELFT>(Rel);
401
15
402
15
    // Output section VA is zero for -r, so r_offset is an offset within the
403
15
    // section, but for --emit-relocs it is an virtual address.
404
15
    P->r_offset = RelocatedSection->getOutputSection()->Addr +
405
15
                  RelocatedSection->getOffset(Rel.r_offset);
406
15
    P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Body), Type,
407
15
                        Config->IsMips64EL);
408
15
409
15
    if (
Body.Type == STT_SECTION15
) {
410
13
      // We combine multiple section symbols into only one per
411
13
      // section. This means we have to update the addend. That is
412
13
      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
413
13
      // section data. We do that by adding to the Relocation vector.
414
13
415
13
      // .eh_frame is horribly special and can reference discarded sections. To
416
13
      // avoid having to parse and recreate .eh_frame, we just replace any
417
13
      // relocation in it pointing to discarded sections with R_*_NONE, which
418
13
      // hopefully creates a frame that is ignored at runtime.
419
13
      SectionBase *Section = cast<DefinedRegular>(Body).Section;
420
13
      if (
Section == &InputSection::Discarded13
) {
421
0
        P->setSymbolAndType(0, 0, false);
422
0
        continue;
423
0
      }
424
13
425
13
      
if (13
Config->IsRela13
) {
426
0
        P->r_addend += Body.getVA() - Section->getOutputSection()->Addr;
427
13
      } else 
if (13
Config->Relocatable13
) {
428
13
        const uint8_t *BufLoc = RelocatedSection->Data.begin() + Rel.r_offset;
429
13
        RelocatedSection->Relocations.push_back(
430
13
            {R_ABS, Type, Rel.r_offset, Target->getImplicitAddend(BufLoc, Type),
431
13
             &Body});
432
13
      }
433
13
    }
434
15
435
15
  }
436
12
}
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> >)
437
438
// The ARM and AArch64 ABI handle pc-relative relocations to undefined weak
439
// references specially. The general rule is that the value of the symbol in
440
// this context is the address of the place P. A further special case is that
441
// branch relocations to an undefined weak reference resolve to the next
442
// instruction.
443
static uint32_t getARMUndefinedRelativeWeakVA(uint32_t Type, uint32_t A,
444
15
                                              uint32_t P) {
445
15
  switch (Type) {
446
15
  // Unresolved branch relocations to weak references resolve to next
447
15
  // instruction, this will be either 2 or 4 bytes on from P.
448
0
  case R_ARM_THM_JUMP11:
449
0
    return P + 2 + A;
450
7
  case R_ARM_CALL:
451
7
  case R_ARM_JUMP24:
452
7
  case R_ARM_PC24:
453
7
  case R_ARM_PLT32:
454
7
  case R_ARM_PREL31:
455
7
  case R_ARM_THM_JUMP19:
456
7
  case R_ARM_THM_JUMP24:
457
7
    return P + 4 + A;
458
3
  case R_ARM_THM_CALL:
459
3
    // We don't want an interworking BLX to ARM
460
3
    return P + 5 + A;
461
7
  // Unresolved non branch pc-relative relocations
462
7
  // R_ARM_TARGET2 which can be resolved relatively is not present as it never
463
7
  // targets a weak-reference.
464
5
  case R_ARM_MOVW_PREL_NC:
465
5
  case R_ARM_MOVT_PREL:
466
5
  case R_ARM_REL32:
467
5
  case R_ARM_THM_MOVW_PREL_NC:
468
5
  case R_ARM_THM_MOVT_PREL:
469
5
    return P + A;
470
0
  }
471
0
  
llvm_unreachable0
("ARM pc-relative relocation expected\n");
472
0
}
473
474
// The comment above getARMUndefinedRelativeWeakVA applies to this function.
475
static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t Type, uint64_t A,
476
9
                                                  uint64_t P) {
477
9
  switch (Type) {
478
9
  // Unresolved branch relocations to weak references resolve to next
479
9
  // instruction, this is 4 bytes on from P.
480
4
  case R_AARCH64_CALL26:
481
4
  case R_AARCH64_CONDBR19:
482
4
  case R_AARCH64_JUMP26:
483
4
  case R_AARCH64_TSTBR14:
484
4
    return P + 4 + A;
485
4
  // Unresolved non branch pc-relative relocations
486
5
  case R_AARCH64_PREL16:
487
5
  case R_AARCH64_PREL32:
488
5
  case R_AARCH64_PREL64:
489
5
  case R_AARCH64_ADR_PREL_LO21:
490
5
  case R_AARCH64_LD_PREL_LO19:
491
5
    return P + A;
492
0
  }
493
0
  
llvm_unreachable0
("AArch64 pc-relative relocation expected\n");
494
0
}
495
496
// ARM SBREL relocations are of the form S + A - B where B is the static base
497
// The ARM ABI defines base to be "addressing origin of the output segment
498
// defining the symbol S". We defined the "addressing origin"/static base to be
499
// the base of the PT_LOAD segment containing the Body.
500
// The procedure call standard only defines a Read Write Position Independent
501
// RWPI variant so in practice we should expect the static base to be the base
502
// of the RW segment.
503
4
static uint64_t getARMStaticBase(const SymbolBody &Body) {
504
4
  OutputSection *OS = Body.getOutputSection();
505
4
  if (
!OS || 4
!OS->PtLoad4
||
!OS->PtLoad->FirstSec4
)
506
0
    fatal("SBREL relocation to " + Body.getName() + " without static base");
507
4
  return OS->PtLoad->FirstSec->Addr;
508
4
}
509
510
static uint64_t getRelocTargetVA(uint32_t Type, int64_t A, uint64_t P,
511
1.85k
                                 const SymbolBody &Body, RelExpr Expr) {
512
1.85k
  switch (Expr) {
513
524
  case R_ABS:
514
524
  case R_RELAX_GOT_PC_NOPIC:
515
524
    return Body.getVA(A);
516
4
  case R_ARM_SBREL:
517
4
    return Body.getVA(A) - getARMStaticBase(Body);
518
24
  case R_GOT:
519
24
  case R_RELAX_TLS_GD_TO_IE_ABS:
520
24
    return Body.getGotVA() + A;
521
0
  case R_GOTONLY_PC:
522
0
    return InX::Got->getVA() + A - P;
523
6
  case R_GOTONLY_PC_FROM_END:
524
6
    return InX::Got->getVA() + A - P + InX::Got->getSize();
525
7
  case R_GOTREL:
526
7
    return Body.getVA(A) - InX::Got->getVA();
527
4
  case R_GOTREL_FROM_END:
528
4
    return Body.getVA(A) - InX::Got->getVA() - InX::Got->getSize();
529
25
  case R_GOT_FROM_END:
530
25
  case R_RELAX_TLS_GD_TO_IE_END:
531
25
    return Body.getGotOffset() + A - InX::Got->getSize();
532
5
  case R_GOT_OFF:
533
5
    return Body.getGotOffset() + A;
534
8
  case R_GOT_PAGE_PC:
535
8
  case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
536
8
    return getAArch64Page(Body.getGotVA() + A) - getAArch64Page(P);
537
97
  case R_GOT_PC:
538
97
  case R_RELAX_TLS_GD_TO_IE:
539
97
    return Body.getGotVA() + A - P;
540
0
  case R_HINT:
541
0
  case R_NONE:
542
0
  case R_TLSDESC_CALL:
543
0
    llvm_unreachable("cannot relocate hint relocs");
544
19
  case R_MIPS_GOTREL:
545
19
    return Body.getVA(A) - InX::MipsGot->getGp();
546
2
  case R_MIPS_GOT_GP:
547
2
    return InX::MipsGot->getGp() + A;
548
20
  case R_MIPS_GOT_GP_PC: {
549
20
    // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
550
20
    // is _gp_disp symbol. In that case we should use the following
551
20
    // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
552
20
    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
553
20
    uint64_t V = InX::MipsGot->getGp() + A - P;
554
20
    if (
Type == R_MIPS_LO16 || 20
Type == R_MICROMIPS_LO1612
)
555
10
      V += 4;
556
20
    return V;
557
0
  }
558
18
  case R_MIPS_GOT_LOCAL_PAGE:
559
18
    // If relocation against MIPS local symbol requires GOT entry, this entry
560
18
    // should be initialized by 'page address'. This address is high 16-bits
561
18
    // of sum the symbol's value and the addend.
562
18
    return InX::MipsGot->getVA() + InX::MipsGot->getPageEntryOffset(Body, A) -
563
18
           InX::MipsGot->getGp();
564
74
  case R_MIPS_GOT_OFF:
565
74
  case R_MIPS_GOT_OFF32:
566
74
    // In case of MIPS if a GOT relocation has non-zero addend this addend
567
74
    // should be applied to the GOT entry content not to the GOT entry offset.
568
74
    // That is why we use separate expression type.
569
74
    return InX::MipsGot->getVA() + InX::MipsGot->getBodyEntryOffset(Body, A) -
570
74
           InX::MipsGot->getGp();
571
9
  case R_MIPS_TLSGD:
572
9
    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
573
9
           InX::MipsGot->getGlobalDynOffset(Body) - InX::MipsGot->getGp();
574
5
  case R_MIPS_TLSLD:
575
5
    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
576
5
           InX::MipsGot->getTlsIndexOff() - InX::MipsGot->getGp();
577
6
  case R_PAGE_PC:
578
6
  case R_PLT_PAGE_PC: {
579
6
    uint64_t Dest;
580
6
    if (Body.isUndefWeak())
581
1
      Dest = getAArch64Page(A);
582
6
    else
583
5
      Dest = getAArch64Page(Body.getVA(A));
584
6
    return Dest - getAArch64Page(P);
585
6
  }
586
576
  case R_PC: {
587
576
    uint64_t Dest;
588
576
    if (
Body.isUndefWeak()576
) {
589
28
      // On ARM and AArch64 a branch to an undefined weak resolves to the
590
28
      // next instruction, otherwise the place.
591
28
      if (Config->EMachine == EM_ARM)
592
15
        Dest = getARMUndefinedRelativeWeakVA(Type, A, P);
593
13
      else 
if (13
Config->EMachine == EM_AARCH6413
)
594
9
        Dest = getAArch64UndefinedRelativeWeakVA(Type, A, P);
595
13
      else
596
4
        Dest = Body.getVA(A);
597
576
    } else {
598
548
      Dest = Body.getVA(A);
599
548
    }
600
576
    return Dest - P;
601
6
  }
602
25
  case R_PLT:
603
25
    return Body.getPltVA() + A;
604
216
  case R_PLT_PC:
605
216
  case R_PPC_PLT_OPD:
606
216
    return Body.getPltVA() + A - P;
607
3
  case R_PPC_OPD: {
608
3
    uint64_t SymVA = Body.getVA(A);
609
3
    // If we have an undefined weak symbol, we might get here with a symbol
610
3
    // address of zero. That could overflow, but the code must be unreachable,
611
3
    // so don't bother doing anything at all.
612
3
    if (!SymVA)
613
1
      return 0;
614
2
    
if (2
Out::Opd2
) {
615
2
      // If this is a local call, and we currently have the address of a
616
2
      // function-descriptor, get the underlying code address instead.
617
2
      uint64_t OpdStart = Out::Opd->Addr;
618
2
      uint64_t OpdEnd = OpdStart + Out::Opd->Size;
619
1
      bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
620
2
      if (InOpd)
621
1
        SymVA = read64be(&Out::OpdBuf[SymVA - OpdStart]);
622
2
    }
623
2
    return SymVA - P;
624
2
  }
625
5
  case R_PPC_TOC:
626
5
    return getPPC64TocBase() + A;
627
21
  case R_RELAX_GOT_PC:
628
21
    return Body.getVA(A) - P;
629
86
  case R_RELAX_TLS_GD_TO_LE:
630
86
  case R_RELAX_TLS_IE_TO_LE:
631
86
  case R_RELAX_TLS_LD_TO_LE:
632
86
  case R_TLS:
633
86
    // A weak undefined TLS symbol resolves to the base of the TLS
634
86
    // block, i.e. gets a value of zero. If we pass --gc-sections to
635
86
    // lld and .tbss is not referenced, it gets reclaimed and we don't
636
86
    // create a TLS program header. Therefore, we resolve this
637
86
    // statically to zero.
638
86
    if (
Body.isTls() && 86
(Body.isLazy() || 86
Body.isUndefined()84
) &&
639
3
        Body.symbol()->isWeak())
640
3
      return 0;
641
83
    
if (83
Target->TcbSize83
)
642
21
      return Body.getVA(A) + alignTo(Target->TcbSize, Out::TlsPhdr->p_align);
643
62
    return Body.getVA(A) - Out::TlsPhdr->p_memsz;
644
4
  case R_RELAX_TLS_GD_TO_LE_NEG:
645
4
  case R_NEG_TLS:
646
4
    return Out::TlsPhdr->p_memsz - Body.getVA(A);
647
24
  case R_SIZE:
648
24
    return A; // Body.getSize was already folded into the addend.
649
8
  case R_TLSDESC:
650
8
    return InX::Got->getGlobalDynAddr(Body) + A;
651
4
  case R_TLSDESC_PAGE:
652
4
    return getAArch64Page(InX::Got->getGlobalDynAddr(Body) + A) -
653
4
           getAArch64Page(P);
654
2
  case R_TLSGD:
655
2
    return InX::Got->getGlobalDynOffset(Body) + A - InX::Got->getSize();
656
18
  case R_TLSGD_PC:
657
18
    return InX::Got->getGlobalDynAddr(Body) + A - P;
658
2
  case R_TLSLD:
659
2
    return InX::Got->getTlsIndexOff() + A - InX::Got->getSize();
660
4
  case R_TLSLD_PC:
661
4
    return InX::Got->getTlsIndexVA() + A - P;
662
0
  }
663
0
  
llvm_unreachable0
("Invalid expression");
664
0
}
665
666
// This function applies relocations to sections without SHF_ALLOC bit.
667
// Such sections are never mapped to memory at runtime. Debug sections are
668
// an example. Relocations in non-alloc sections are much easier to
669
// handle than in allocated sections because it will never need complex
670
// treatement such as GOT or PLT (because at runtime no one refers them).
671
// So, we handle relocations for non-alloc sections directly in this
672
// function as a performance optimization.
673
template <class ELFT, class RelTy>
674
130k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
77
  for (const RelTy &Rel : Rels) {
676
77
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
77
    uint64_t Offset = getOffset(Rel.r_offset);
678
77
    uint8_t *BufLoc = Buf + Offset;
679
77
    int64_t Addend = getAddend<ELFT>(Rel);
680
77
    if (!RelTy::IsRela)
681
3
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
77
683
77
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
77
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
77
    if (Expr == R_NONE)
686
1
      continue;
687
76
    
if (76
Expr != R_ABS76
) {
688
2
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
2
      return;
690
2
    }
691
74
692
74
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
74
    uint64_t SymVA = 0;
694
74
    if (
!Sym.isTls() || 74
Out::TlsPhdr3
)
695
73
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
73
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
77
    Target->relocateOne(BufLoc, Type, SymVA);
698
77
  }
699
130k
}
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
674
2
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
0
  for (const RelTy &Rel : Rels) {
676
0
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
0
    uint64_t Offset = getOffset(Rel.r_offset);
678
0
    uint8_t *BufLoc = Buf + Offset;
679
0
    int64_t Addend = getAddend<ELFT>(Rel);
680
0
    if (!RelTy::IsRela)
681
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
0
683
0
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
0
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
0
    if (Expr == R_NONE)
686
0
      continue;
687
0
    
if (0
Expr != R_ABS0
) {
688
0
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
0
      return;
690
0
    }
691
0
692
0
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
0
    uint64_t SymVA = 0;
694
0
    if (
!Sym.isTls() || 0
Out::TlsPhdr0
)
695
0
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
0
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
0
    Target->relocateOne(BufLoc, Type, SymVA);
698
0
  }
699
2
}
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> >)
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)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
674
130k
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
0
  for (const RelTy &Rel : Rels) {
676
0
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
0
    uint64_t Offset = getOffset(Rel.r_offset);
678
0
    uint8_t *BufLoc = Buf + Offset;
679
0
    int64_t Addend = getAddend<ELFT>(Rel);
680
0
    if (!RelTy::IsRela)
681
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
0
683
0
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
0
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
0
    if (Expr == R_NONE)
686
0
      continue;
687
0
    
if (0
Expr != R_ABS0
) {
688
0
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
0
      return;
690
0
    }
691
0
692
0
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
0
    uint64_t SymVA = 0;
694
0
    if (
!Sym.isTls() || 0
Out::TlsPhdr0
)
695
0
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
0
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
0
    Target->relocateOne(BufLoc, Type, SymVA);
698
0
  }
699
130k
}
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
674
30
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
74
  for (const RelTy &Rel : Rels) {
676
74
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
74
    uint64_t Offset = getOffset(Rel.r_offset);
678
74
    uint8_t *BufLoc = Buf + Offset;
679
74
    int64_t Addend = getAddend<ELFT>(Rel);
680
74
    if (!RelTy::IsRela)
681
0
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
74
683
74
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
74
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
74
    if (Expr == R_NONE)
686
1
      continue;
687
73
    
if (73
Expr != R_ABS73
) {
688
1
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
1
      return;
690
1
    }
691
72
692
72
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
72
    uint64_t SymVA = 0;
694
72
    if (
!Sym.isTls() || 72
Out::TlsPhdr3
)
695
71
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
71
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
74
    Target->relocateOne(BufLoc, Type, SymVA);
698
74
  }
699
30
}
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
674
4
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
2
  for (const RelTy &Rel : Rels) {
676
2
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
2
    uint64_t Offset = getOffset(Rel.r_offset);
678
2
    uint8_t *BufLoc = Buf + Offset;
679
2
    int64_t Addend = getAddend<ELFT>(Rel);
680
2
    if (!RelTy::IsRela)
681
2
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
2
683
2
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
2
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
2
    if (Expr == R_NONE)
686
0
      continue;
687
2
    
if (2
Expr != R_ABS2
) {
688
0
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
0
      return;
690
0
    }
691
2
692
2
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
2
    uint64_t SymVA = 0;
694
2
    if (
!Sym.isTls() || 2
Out::TlsPhdr0
)
695
2
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
2
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
2
    Target->relocateOne(BufLoc, Type, SymVA);
698
2
  }
699
4
}
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
674
7
void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
675
1
  for (const RelTy &Rel : Rels) {
676
1
    uint32_t Type = Rel.getType(Config->IsMips64EL);
677
1
    uint64_t Offset = getOffset(Rel.r_offset);
678
1
    uint8_t *BufLoc = Buf + Offset;
679
1
    int64_t Addend = getAddend<ELFT>(Rel);
680
1
    if (!RelTy::IsRela)
681
1
      Addend += Target->getImplicitAddend(BufLoc, Type);
682
1
683
1
    SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel);
684
1
    RelExpr Expr = Target->getRelExpr(Type, Sym, *File, BufLoc);
685
1
    if (Expr == R_NONE)
686
0
      continue;
687
1
    
if (1
Expr != R_ABS1
) {
688
1
      error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc");
689
1
      return;
690
1
    }
691
0
692
0
    uint64_t AddrLoc = getParent()->Addr + Offset;
693
0
    uint64_t SymVA = 0;
694
0
    if (
!Sym.isTls() || 0
Out::TlsPhdr0
)
695
0
      SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>(
696
0
          getRelocTargetVA(Type, Addend, AddrLoc, Sym, R_ABS));
697
1
    Target->relocateOne(BufLoc, Type, SymVA);
698
1
  }
699
7
}
700
701
3.40k
template <class ELFT> ObjFile<ELFT> *InputSectionBase::getFile() const {
702
3.40k
  return cast_or_null<ObjFile<ELFT>>(File);
703
3.40k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >() const
Line
Count
Source
701
624
template <class ELFT> ObjFile<ELFT> *InputSectionBase::getFile() const {
702
624
  return cast_or_null<ObjFile<ELFT>>(File);
703
624
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >() const
Line
Count
Source
701
560
template <class ELFT> ObjFile<ELFT> *InputSectionBase::getFile() const {
702
560
  return cast_or_null<ObjFile<ELFT>>(File);
703
560
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >() const
Line
Count
Source
701
2.01k
template <class ELFT> ObjFile<ELFT> *InputSectionBase::getFile() const {
702
2.01k
  return cast_or_null<ObjFile<ELFT>>(File);
703
2.01k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >* lld::elf::InputSectionBase::getFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >() const
Line
Count
Source
701
206
template <class ELFT> ObjFile<ELFT> *InputSectionBase::getFile() const {
702
206
  return cast_or_null<ObjFile<ELFT>>(File);
703
206
}
704
705
template <class ELFT>
706
199k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
707
199k
  if (Flags & SHF_ALLOC)
708
68.6k
    relocateAlloc(Buf, BufEnd);
709
199k
  else
710
130k
    relocateNonAlloc<ELFT>(Buf, BufEnd);
711
199k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, unsigned char*)
Line
Count
Source
706
131
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
707
131
  if (Flags & SHF_ALLOC)
708
129
    relocateAlloc(Buf, BufEnd);
709
131
  else
710
2
    relocateNonAlloc<ELFT>(Buf, BufEnd);
711
131
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, unsigned char*)
Line
Count
Source
706
198k
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
707
198k
  if (Flags & SHF_ALLOC)
708
67.7k
    relocateAlloc(Buf, BufEnd);
709
198k
  else
710
130k
    relocateNonAlloc<ELFT>(Buf, BufEnd);
711
198k
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, unsigned char*)
Line
Count
Source
706
287
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
707
287
  if (Flags & SHF_ALLOC)
708
283
    relocateAlloc(Buf, BufEnd);
709
287
  else
710
4
    relocateNonAlloc<ELFT>(Buf, BufEnd);
711
287
}
void lld::elf::InputSectionBase::relocate<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, unsigned char*)
Line
Count
Source
706
426
void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
707
426
  if (Flags & SHF_ALLOC)
708
419
    relocateAlloc(Buf, BufEnd);
709
426
  else
710
7
    relocateNonAlloc<ELFT>(Buf, BufEnd);
711
426
}
712
713
template <class ELFT>
714
130k
void InputSectionBase::relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd) {
715
130k
  // scanReloc function in Writer.cpp constructs Relocations
716
130k
  // vector only for SHF_ALLOC'ed sections. For other sections,
717
130k
  // we handle relocations directly here.
718
130k
  auto *IS = cast<InputSection>(this);
719
130k
  assert(!(IS->Flags & SHF_ALLOC));
720
130k
  if (IS->AreRelocsRela)
721
30
    IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>());
722
130k
  else
723
130k
    IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>());
724
130k
}
void lld::elf::InputSectionBase::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, unsigned char*)
Line
Count
Source
714
4
void InputSectionBase::relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd) {
715
4
  // scanReloc function in Writer.cpp constructs Relocations
716
4
  // vector only for SHF_ALLOC'ed sections. For other sections,
717
4
  // we handle relocations directly here.
718
4
  auto *IS = cast<InputSection>(this);
719
4
  assert(!(IS->Flags & SHF_ALLOC));
720
4
  if (IS->AreRelocsRela)
721
0
    IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>());
722
4
  else
723
4
    IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>());
724
4
}
void lld::elf::InputSectionBase::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, unsigned char*)
Line
Count
Source
714
7
void InputSectionBase::relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd) {
715
7
  // scanReloc function in Writer.cpp constructs Relocations
716
7
  // vector only for SHF_ALLOC'ed sections. For other sections,
717
7
  // we handle relocations directly here.
718
7
  auto *IS = cast<InputSection>(this);
719
7
  assert(!(IS->Flags & SHF_ALLOC));
720
7
  if (IS->AreRelocsRela)
721
0
    IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>());
722
7
  else
723
7
    IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>());
724
7
}
void lld::elf::InputSectionBase::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, unsigned char*)
Line
Count
Source
714
2
void InputSectionBase::relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd) {
715
2
  // scanReloc function in Writer.cpp constructs Relocations
716
2
  // vector only for SHF_ALLOC'ed sections. For other sections,
717
2
  // we handle relocations directly here.
718
2
  auto *IS = cast<InputSection>(this);
719
2
  assert(!(IS->Flags & SHF_ALLOC));
720
2
  if (IS->AreRelocsRela)
721
0
    IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>());
722
2
  else
723
2
    IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>());
724
2
}
void lld::elf::InputSectionBase::relocateNonAlloc<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, unsigned char*)
Line
Count
Source
714
130k
void InputSectionBase::relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd) {
715
130k
  // scanReloc function in Writer.cpp constructs Relocations
716
130k
  // vector only for SHF_ALLOC'ed sections. For other sections,
717
130k
  // we handle relocations directly here.
718
130k
  auto *IS = cast<InputSection>(this);
719
130k
  assert(!(IS->Flags & SHF_ALLOC));
720
130k
  if (IS->AreRelocsRela)
721
30
    IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>());
722
130k
  else
723
130k
    IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>());
724
130k
}
725
726
68.7k
void InputSectionBase::relocateAlloc(uint8_t *Buf, uint8_t *BufEnd) {
727
68.7k
  assert(Flags & SHF_ALLOC);
728
68.7k
  const unsigned Bits = Config->Wordsize * 8;
729
1.78k
  for (const Relocation &Rel : Relocations) {
730
1.78k
    uint64_t Offset = getOffset(Rel.Offset);
731
1.78k
    uint8_t *BufLoc = Buf + Offset;
732
1.78k
    uint32_t Type = Rel.Type;
733
1.78k
734
1.78k
    uint64_t AddrLoc = getOutputSection()->Addr + Offset;
735
1.78k
    RelExpr Expr = Rel.Expr;
736
1.78k
    uint64_t TargetVA = SignExtend64(
737
1.78k
        getRelocTargetVA(Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr), Bits);
738
1.78k
739
1.78k
    switch (Expr) {
740
30
    case R_RELAX_GOT_PC:
741
30
    case R_RELAX_GOT_PC_NOPIC:
742
30
      Target->relaxGot(BufLoc, TargetVA);
743
30
      break;
744
48
    case R_RELAX_TLS_IE_TO_LE:
745
48
      Target->relaxTlsIeToLe(BufLoc, Type, TargetVA);
746
48
      break;
747
12
    case R_RELAX_TLS_LD_TO_LE:
748
12
      Target->relaxTlsLdToLe(BufLoc, Type, TargetVA);
749
12
      break;
750
10
    case R_RELAX_TLS_GD_TO_LE:
751
10
    case R_RELAX_TLS_GD_TO_LE_NEG:
752
10
      Target->relaxTlsGdToLe(BufLoc, Type, TargetVA);
753
10
      break;
754
10
    case R_RELAX_TLS_GD_TO_IE:
755
10
    case R_RELAX_TLS_GD_TO_IE_ABS:
756
10
    case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
757
10
    case R_RELAX_TLS_GD_TO_IE_END:
758
10
      Target->relaxTlsGdToIe(BufLoc, Type, TargetVA);
759
10
      break;
760
6
    case R_PPC_PLT_OPD:
761
6
      // Patch a nop (0x60000000) to a ld.
762
6
      if (
BufLoc + 8 <= BufEnd && 6
read32be(BufLoc + 4) == 0x600000005
)
763
2
        write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
764
6
      LLVM_FALLTHROUGH;
765
1.67k
    default:
766
1.67k
      Target->relocateOne(BufLoc, Type, TargetVA);
767
1.67k
      break;
768
68.7k
    }
769
68.7k
  }
770
68.7k
}
771
772
209k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
773
209k
  if (this->Type == SHT_NOBITS)
774
1
    return;
775
209k
776
209k
  
if (auto *209k
S209k
= dyn_cast<SyntheticSection>(this)) {
777
10.3k
    S->writeTo(Buf + OutSecOff);
778
10.3k
    return;
779
10.3k
  }
780
199k
781
199k
  // If -r or --emit-relocs is given, then an InputSection
782
199k
  // may be a relocation section.
783
199k
  
if (199k
this->Type == SHT_RELA199k
) {
784
48
    copyRelocations<ELFT>(Buf + OutSecOff,
785
48
                          this->template getDataAs<typename ELFT::Rela>());
786
48
    return;
787
48
  }
788
199k
  
if (199k
this->Type == SHT_REL199k
) {
789
18
    copyRelocations<ELFT>(Buf + OutSecOff,
790
18
                          this->template getDataAs<typename ELFT::Rel>());
791
18
    return;
792
18
  }
793
199k
794
199k
  // If -r is given, we may have a SHT_GROUP section.
795
199k
  
if (199k
this->Type == SHT_GROUP199k
) {
796
8
    copyShtGroup<ELFT>(Buf + OutSecOff);
797
8
    return;
798
8
  }
799
199k
800
199k
  // Copy section contents from source object file to output file
801
199k
  // and then apply relocations.
802
199k
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
803
199k
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
804
199k
  this->relocate<ELFT>(Buf, BufEnd);
805
199k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*)
Line
Count
Source
772
1.19k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
773
1.19k
  if (this->Type == SHT_NOBITS)
774
0
    return;
775
1.19k
776
1.19k
  
if (auto *1.19k
S1.19k
= dyn_cast<SyntheticSection>(this)) {
777
906
    S->writeTo(Buf + OutSecOff);
778
906
    return;
779
906
  }
780
293
781
293
  // If -r or --emit-relocs is given, then an InputSection
782
293
  // may be a relocation section.
783
293
  
if (293
this->Type == SHT_RELA293
) {
784
0
    copyRelocations<ELFT>(Buf + OutSecOff,
785
0
                          this->template getDataAs<typename ELFT::Rela>());
786
0
    return;
787
0
  }
788
293
  
if (293
this->Type == SHT_REL293
) {
789
6
    copyRelocations<ELFT>(Buf + OutSecOff,
790
6
                          this->template getDataAs<typename ELFT::Rel>());
791
6
    return;
792
6
  }
793
287
794
287
  // If -r is given, we may have a SHT_GROUP section.
795
287
  
if (287
this->Type == SHT_GROUP287
) {
796
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
797
0
    return;
798
0
  }
799
287
800
287
  // Copy section contents from source object file to output file
801
287
  // and then apply relocations.
802
287
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
803
287
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
804
287
  this->relocate<ELFT>(Buf, BufEnd);
805
287
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*)
Line
Count
Source
772
523
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
773
523
  if (this->Type == SHT_NOBITS)
774
0
    return;
775
523
776
523
  
if (auto *523
S523
= dyn_cast<SyntheticSection>(this)) {
777
392
    S->writeTo(Buf + OutSecOff);
778
392
    return;
779
392
  }
780
131
781
131
  // If -r or --emit-relocs is given, then an InputSection
782
131
  // may be a relocation section.
783
131
  
if (131
this->Type == SHT_RELA131
) {
784
0
    copyRelocations<ELFT>(Buf + OutSecOff,
785
0
                          this->template getDataAs<typename ELFT::Rela>());
786
0
    return;
787
0
  }
788
131
  
if (131
this->Type == SHT_REL131
) {
789
0
    copyRelocations<ELFT>(Buf + OutSecOff,
790
0
                          this->template getDataAs<typename ELFT::Rel>());
791
0
    return;
792
0
  }
793
131
794
131
  // If -r is given, we may have a SHT_GROUP section.
795
131
  
if (131
this->Type == SHT_GROUP131
) {
796
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
797
0
    return;
798
0
  }
799
131
800
131
  // Copy section contents from source object file to output file
801
131
  // and then apply relocations.
802
131
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
803
131
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
804
131
  this->relocate<ELFT>(Buf, BufEnd);
805
131
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*)
Line
Count
Source
772
206k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
773
206k
  if (this->Type == SHT_NOBITS)
774
1
    return;
775
206k
776
206k
  
if (auto *206k
S206k
= dyn_cast<SyntheticSection>(this)) {
777
7.70k
    S->writeTo(Buf + OutSecOff);
778
7.70k
    return;
779
7.70k
  }
780
198k
781
198k
  // If -r or --emit-relocs is given, then an InputSection
782
198k
  // may be a relocation section.
783
198k
  
if (198k
this->Type == SHT_RELA198k
) {
784
48
    copyRelocations<ELFT>(Buf + OutSecOff,
785
48
                          this->template getDataAs<typename ELFT::Rela>());
786
48
    return;
787
48
  }
788
198k
  
if (198k
this->Type == SHT_REL198k
) {
789
0
    copyRelocations<ELFT>(Buf + OutSecOff,
790
0
                          this->template getDataAs<typename ELFT::Rel>());
791
0
    return;
792
0
  }
793
198k
794
198k
  // If -r is given, we may have a SHT_GROUP section.
795
198k
  
if (198k
this->Type == SHT_GROUP198k
) {
796
8
    copyShtGroup<ELFT>(Buf + OutSecOff);
797
8
    return;
798
8
  }
799
198k
800
198k
  // Copy section contents from source object file to output file
801
198k
  // and then apply relocations.
802
198k
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
803
198k
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
804
198k
  this->relocate<ELFT>(Buf, BufEnd);
805
198k
}
void lld::elf::InputSection::writeTo<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*)
Line
Count
Source
772
1.82k
template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
773
1.82k
  if (this->Type == SHT_NOBITS)
774
0
    return;
775
1.82k
776
1.82k
  
if (auto *1.82k
S1.82k
= dyn_cast<SyntheticSection>(this)) {
777
1.38k
    S->writeTo(Buf + OutSecOff);
778
1.38k
    return;
779
1.38k
  }
780
437
781
437
  // If -r or --emit-relocs is given, then an InputSection
782
437
  // may be a relocation section.
783
437
  
if (437
this->Type == SHT_RELA437
) {
784
0
    copyRelocations<ELFT>(Buf + OutSecOff,
785
0
                          this->template getDataAs<typename ELFT::Rela>());
786
0
    return;
787
0
  }
788
437
  
if (437
this->Type == SHT_REL437
) {
789
12
    copyRelocations<ELFT>(Buf + OutSecOff,
790
12
                          this->template getDataAs<typename ELFT::Rel>());
791
12
    return;
792
12
  }
793
425
794
425
  // If -r is given, we may have a SHT_GROUP section.
795
425
  
if (425
this->Type == SHT_GROUP425
) {
796
0
    copyShtGroup<ELFT>(Buf + OutSecOff);
797
0
    return;
798
0
  }
799
425
800
425
  // Copy section contents from source object file to output file
801
425
  // and then apply relocations.
802
425
  memcpy(Buf + OutSecOff, Data.data(), Data.size());
803
425
  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
804
425
  this->relocate<ELFT>(Buf, BufEnd);
805
425
}
806
807
17
void InputSection::replace(InputSection *Other) {
808
17
  this->Alignment = std::max(this->Alignment, Other->Alignment);
809
17
  Other->Repl = this->Repl;
810
17
  Other->Live = false;
811
17
}
812
813
template <class ELFT>
814
EhInputSection::EhInputSection(ObjFile<ELFT> *F,
815
                               const typename ELFT::Shdr *Header,
816
                               StringRef Name)
817
92
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {
818
92
  // Mark .eh_frame sections as live by default because there are
819
92
  // usually no relocations that point to .eh_frames. Otherwise,
820
92
  // the garbage collector would drop all .eh_frame sections.
821
92
  this->Live = true;
822
92
}
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
817
1
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {
818
1
  // Mark .eh_frame sections as live by default because there are
819
1
  // usually no relocations that point to .eh_frames. Otherwise,
820
1
  // the garbage collector would drop all .eh_frame sections.
821
1
  this->Live = true;
822
1
}
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
817
3
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {
818
3
  // Mark .eh_frame sections as live by default because there are
819
3
  // usually no relocations that point to .eh_frames. Otherwise,
820
3
  // the garbage collector would drop all .eh_frame sections.
821
3
  this->Live = true;
822
3
}
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
817
84
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {
818
84
  // Mark .eh_frame sections as live by default because there are
819
84
  // usually no relocations that point to .eh_frames. Otherwise,
820
84
  // the garbage collector would drop all .eh_frame sections.
821
84
  this->Live = true;
822
84
}
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
817
4
    : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) {
818
4
  // Mark .eh_frame sections as live by default because there are
819
4
  // usually no relocations that point to .eh_frames. Otherwise,
820
4
  // the garbage collector would drop all .eh_frame sections.
821
4
  this->Live = true;
822
4
}
823
824
117
SyntheticSection *EhInputSection::getParent() const {
825
117
  return cast_or_null<SyntheticSection>(Parent);
826
117
}
827
828
319k
bool EhInputSection::classof(const SectionBase *S) {
829
319k
  return S->kind() == InputSectionBase::EHFrame;
830
319k
}
831
832
// Returns the index of the first relocation that points to a region between
833
// Begin and Begin+Size.
834
template <class IntTy, class RelTy>
835
static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels,
836
203
                         unsigned &RelocI) {
837
203
  // Start search from RelocI for fast access. That works because the
838
203
  // relocations are sorted in .eh_frame.
839
241
  for (unsigned N = Rels.size(); 
RelocI < N241
;
++RelocI38
) {
840
233
    const RelTy &Rel = Rels[RelocI];
841
233
    if (Rel.r_offset < Begin)
842
38
      continue;
843
195
844
195
    
if (195
Rel.r_offset < Begin + Size195
)
845
120
      return RelocI;
846
75
    return -1;
847
75
  }
848
8
  return -1;
849
203
}
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
836
188
                         unsigned &RelocI) {
837
188
  // Start search from RelocI for fast access. That works because the
838
188
  // relocations are sorted in .eh_frame.
839
224
  for (unsigned N = Rels.size(); 
RelocI < N224
;
++RelocI36
) {
840
217
    const RelTy &Rel = Rels[RelocI];
841
217
    if (Rel.r_offset < Begin)
842
36
      continue;
843
181
844
181
    
if (181
Rel.r_offset < Begin + Size181
)
845
111
      return RelocI;
846
70
    return -1;
847
70
  }
848
7
  return -1;
849
188
}
Unexecuted instantiation: 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&)
Unexecuted instantiation: 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&)
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)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
836
8
                         unsigned &RelocI) {
837
8
  // Start search from RelocI for fast access. That works because the
838
8
  // relocations are sorted in .eh_frame.
839
10
  for (unsigned N = Rels.size(); 
RelocI < N10
;
++RelocI2
) {
840
10
    const RelTy &Rel = Rels[RelocI];
841
10
    if (Rel.r_offset < Begin)
842
2
      continue;
843
8
844
8
    
if (8
Rel.r_offset < Begin + Size8
)
845
6
      return RelocI;
846
2
    return -1;
847
2
  }
848
0
  return -1;
849
8
}
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)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
836
5
                         unsigned &RelocI) {
837
5
  // Start search from RelocI for fast access. That works because the
838
5
  // relocations are sorted in .eh_frame.
839
5
  for (unsigned N = Rels.size(); 
RelocI < N5
;
++RelocI0
) {
840
4
    const RelTy &Rel = Rels[RelocI];
841
4
    if (Rel.r_offset < Begin)
842
0
      continue;
843
4
844
4
    
if (4
Rel.r_offset < Begin + Size4
)
845
2
      return RelocI;
846
2
    return -1;
847
2
  }
848
1
  return -1;
849
5
}
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
836
2
                         unsigned &RelocI) {
837
2
  // Start search from RelocI for fast access. That works because the
838
2
  // relocations are sorted in .eh_frame.
839
2
  for (unsigned N = Rels.size(); 
RelocI < N2
;
++RelocI0
) {
840
2
    const RelTy &Rel = Rels[RelocI];
841
2
    if (Rel.r_offset < Begin)
842
0
      continue;
843
2
844
2
    
if (2
Rel.r_offset < Begin + Size2
)
845
1
      return RelocI;
846
1
    return -1;
847
1
  }
848
0
  return -1;
849
2
}
850
851
// .eh_frame is a sequence of CIE or FDE records.
852
// This function splits an input section into records and returns them.
853
97
template <class ELFT> void EhInputSection::split() {
854
97
  // Early exit if already split.
855
97
  if (!this->Pieces.empty())
856
5
    return;
857
92
858
92
  
if (92
this->NumRelocations92
) {
859
82
    if (this->AreRelocsRela)
860
77
      split<ELFT>(this->relas<ELFT>());
861
82
    else
862
5
      split<ELFT>(this->rels<ELFT>());
863
82
    return;
864
82
  }
865
10
  split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
866
10
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
853
3
template <class ELFT> void EhInputSection::split() {
854
3
  // Early exit if already split.
855
3
  if (!this->Pieces.empty())
856
0
    return;
857
3
858
3
  
if (3
this->NumRelocations3
) {
859
2
    if (this->AreRelocsRela)
860
2
      split<ELFT>(this->relas<ELFT>());
861
2
    else
862
0
      split<ELFT>(this->rels<ELFT>());
863
2
    return;
864
2
  }
865
1
  split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
866
1
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
853
87
template <class ELFT> void EhInputSection::split() {
854
87
  // Early exit if already split.
855
87
  if (!this->Pieces.empty())
856
3
    return;
857
84
858
84
  
if (84
this->NumRelocations84
) {
859
75
    if (this->AreRelocsRela)
860
75
      split<ELFT>(this->relas<ELFT>());
861
75
    else
862
0
      split<ELFT>(this->rels<ELFT>());
863
75
    return;
864
75
  }
865
9
  split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
866
9
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
853
1
template <class ELFT> void EhInputSection::split() {
854
1
  // Early exit if already split.
855
1
  if (!this->Pieces.empty())
856
0
    return;
857
1
858
1
  
if (1
this->NumRelocations1
) {
859
1
    if (this->AreRelocsRela)
860
0
      split<ELFT>(this->relas<ELFT>());
861
1
    else
862
1
      split<ELFT>(this->rels<ELFT>());
863
1
    return;
864
1
  }
865
0
  split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
866
0
}
void lld::elf::EhInputSection::split<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
853
6
template <class ELFT> void EhInputSection::split() {
854
6
  // Early exit if already split.
855
6
  if (!this->Pieces.empty())
856
2
    return;
857
4
858
4
  
if (4
this->NumRelocations4
) {
859
4
    if (this->AreRelocsRela)
860
0
      split<ELFT>(this->relas<ELFT>());
861
4
    else
862
4
      split<ELFT>(this->rels<ELFT>());
863
4
    return;
864
4
  }
865
0
  split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
866
0
}
867
868
template <class ELFT, class RelTy>
869
92
void EhInputSection::split(ArrayRef<RelTy> Rels) {
870
92
  ArrayRef<uint8_t> Data = this->Data;
871
92
  unsigned RelI = 0;
872
290
  for (size_t Off = 0, End = Data.size(); 
Off != End290
;) {
873
203
    size_t Size = readEhRecordSize<ELFT>(this, Off);
874
203
    this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
875
203
    // The empty record is the end marker.
876
203
    if (Size == 4)
877
5
      break;
878
198
    Off += Size;
879
198
  }
880
92
}
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
869
84
void EhInputSection::split(ArrayRef<RelTy> Rels) {
870
84
  ArrayRef<uint8_t> Data = this->Data;
871
84
  unsigned RelI = 0;
872
267
  for (size_t Off = 0, End = Data.size(); 
Off != End267
;) {
873
188
    size_t Size = readEhRecordSize<ELFT>(this, Off);
874
188
    this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
875
188
    // The empty record is the end marker.
876
188
    if (Size == 4)
877
5
      break;
878
183
    Off += Size;
879
183
  }
880
84
}
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
869
3
void EhInputSection::split(ArrayRef<RelTy> Rels) {
870
3
  ArrayRef<uint8_t> Data = this->Data;
871
3
  unsigned RelI = 0;
872
8
  for (size_t Off = 0, End = Data.size(); 
Off != End8
;) {
873
5
    size_t Size = readEhRecordSize<ELFT>(this, Off);
874
5
    this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
875
5
    // The empty record is the end marker.
876
5
    if (Size == 4)
877
0
      break;
878
5
    Off += Size;
879
5
  }
880
3
}
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
869
1
void EhInputSection::split(ArrayRef<RelTy> Rels) {
870
1
  ArrayRef<uint8_t> Data = this->Data;
871
1
  unsigned RelI = 0;
872
3
  for (size_t Off = 0, End = Data.size(); 
Off != End3
;) {
873
2
    size_t Size = readEhRecordSize<ELFT>(this, Off);
874
2
    this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
875
2
    // The empty record is the end marker.
876
2
    if (Size == 4)
877
0
      break;
878
2
    Off += Size;
879
2
  }
880
1
}
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> >)
Unexecuted instantiation: 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> >)
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
869
4
void EhInputSection::split(ArrayRef<RelTy> Rels) {
870
4
  ArrayRef<uint8_t> Data = this->Data;
871
4
  unsigned RelI = 0;
872
12
  for (size_t Off = 0, End = Data.size(); 
Off != End12
;) {
873
8
    size_t Size = readEhRecordSize<ELFT>(this, Off);
874
8
    this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
875
8
    // The empty record is the end marker.
876
8
    if (Size == 4)
877
0
      break;
878
8
    Off += Size;
879
8
  }
880
4
}
Unexecuted instantiation: 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> >)
881
882
1.62k
static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
883
1.62k
  // Optimize the common case.
884
1.62k
  StringRef S((const char *)A.data(), A.size());
885
1.62k
  if (EntSize == 1)
886
1.62k
    return S.find(0);
887
4
888
6
  
for (unsigned I = 0, N = S.size(); 4
I != N6
;
I += EntSize2
) {
889
7
    const char *B = S.begin() + I;
890
30
    if (
std::all_of(B, B + EntSize, [](char C) 7
{ return C == 0; }30
))
891
5
      return I;
892
7
  }
893
18.4E
  return StringRef::npos;
894
1.62k
}
895
896
161
SyntheticSection *MergeInputSection::getParent() const {
897
161
  return cast_or_null<SyntheticSection>(Parent);
898
161
}
899
900
// Split SHF_STRINGS section. Such section is a sequence of
901
// null-terminated strings.
902
1.57k
void MergeInputSection::splitStrings(ArrayRef<uint8_t> Data, size_t EntSize) {
903
1.57k
  size_t Off = 0;
904
1.57k
  bool IsAlloc = this->Flags & SHF_ALLOC;
905
3.20k
  while (
!Data.empty()3.20k
) {
906
1.62k
    size_t End = findNull(Data, EntSize);
907
1.62k
    if (End == StringRef::npos)
908
0
      fatal(toString(this) + ": string is not null terminated");
909
1.62k
    size_t Size = End + EntSize;
910
1.62k
    Pieces.emplace_back(Off, !IsAlloc);
911
1.62k
    Hashes.push_back(hash_value(toStringRef(Data.slice(0, Size))));
912
1.62k
    Data = Data.slice(Size);
913
1.62k
    Off += Size;
914
1.62k
  }
915
1.57k
}
916
917
// Split non-SHF_STRINGS section. Such section is a sequence of
918
// fixed size records.
919
void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> Data,
920
13
                                        size_t EntSize) {
921
13
  size_t Size = Data.size();
922
13
  assert((Size % EntSize) == 0);
923
13
  bool IsAlloc = this->Flags & SHF_ALLOC;
924
32
  for (unsigned I = 0, N = Size; 
I != N32
;
I += EntSize19
) {
925
19
    Hashes.push_back(hash_value(toStringRef(Data.slice(I, EntSize))));
926
19
    Pieces.emplace_back(I, !IsAlloc);
927
19
  }
928
13
}
929
930
template <class ELFT>
931
MergeInputSection::MergeInputSection(ObjFile<ELFT> *F,
932
                                     const typename ELFT::Shdr *Header,
933
                                     StringRef Name)
934
1.60k
    : 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
934
88
    : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {}
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)
Line
Count
Source
934
43
    : 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
934
1.28k
    : 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
934
188
    : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {}
935
936
// This function is called after we obtain a complete list of input sections
937
// that need to be linked. This is responsible to split section contents
938
// into small chunks for further processing.
939
//
940
// Note that this function is called from parallelForEach. This must be
941
// thread-safe (i.e. no memory allocation from the pools).
942
1.58k
void MergeInputSection::splitIntoPieces() {
943
1.58k
  assert(Pieces.empty());
944
1.58k
  ArrayRef<uint8_t> Data = this->Data;
945
1.58k
  uint64_t EntSize = this->Entsize;
946
1.58k
  if (this->Flags & SHF_STRINGS)
947
1.57k
    splitStrings(Data, EntSize);
948
1.58k
  else
949
11
    splitNonStrings(Data, EntSize);
950
1.58k
951
1.58k
  if (
Config->GcSections && 1.58k
(this->Flags & SHF_ALLOC)70
)
952
7
    for (uint64_t Off : LiveOffsets)
953
8
      this->getSectionPiece(Off)->Live = true;
954
1.58k
}
955
956
408k
bool MergeInputSection::classof(const SectionBase *S) {
957
408k
  return S->kind() == InputSectionBase::Merge;
958
408k
}
959
960
// Do binary search to get a section piece at a given input offset.
961
54
SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
962
54
  auto *This = static_cast<const MergeInputSection *>(this);
963
54
  return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
964
54
}
965
966
template <class It, class T, class Compare>
967
59
static It fastUpperBound(It First, It Last, const T &Value, Compare Comp) {
968
59
  size_t Size = std::distance(First, Last);
969
59
  assert(Size != 0);
970
134
  while (
Size != 1134
) {
971
75
    size_t H = Size / 2;
972
75
    const It MI = First + H;
973
75
    Size -= H;
974
75
    First = Comp(Value, *MI) ? 
First47
:
First + H28
;
975
75
  }
976
59
  return Comp(Value, *First) ? 
First0
:
First + 159
;
977
59
}
978
979
59
const SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) const {
980
59
  uint64_t Size = this->Data.size();
981
59
  if (Offset >= Size)
982
0
    fatal(toString(this) + ": entry is past the end of the section");
983
59
984
59
  // Find the element this offset points to.
985
59
  auto I = fastUpperBound(
986
59
      Pieces.begin(), Pieces.end(), Offset,
987
134
      [](const uint64_t &A, const SectionPiece &B) { return A < B.InputOff; });
988
59
  --I;
989
59
  return &*I;
990
59
}
991
992
// Returns the offset in an output section for a given input offset.
993
// Because contents of a mergeable section is not contiguous in output,
994
// it is not just an addition to a base output offset.
995
74
uint64_t MergeInputSection::getOffset(uint64_t Offset) const {
996
74
  // Initialize OffsetMap lazily.
997
34
  llvm::call_once(InitOffsetMap, [&] {
998
34
    OffsetMap.reserve(Pieces.size());
999
34
    for (const SectionPiece &Piece : Pieces)
1000
74
      OffsetMap[Piece.InputOff] = Piece.OutputOff;
1001
34
  });
1002
74
1003
74
  // Find a string starting at a given offset.
1004
74
  auto It = OffsetMap.find(Offset);
1005
74
  if (It != OffsetMap.end())
1006
68
    return It->second;
1007
6
1008
6
  
if (6
!this->Live6
)
1009
1
    return 0;
1010
5
1011
5
  // If Offset is not at beginning of a section piece, it is not in the map.
1012
5
  // In that case we need to search from the original section piece vector.
1013
5
  const SectionPiece &Piece = *this->getSectionPiece(Offset);
1014
5
  if (!Piece.Live)
1015
0
    return 0;
1016
5
1017
5
  uint64_t Addend = Offset - Piece.InputOff;
1018
5
  return Piece.OutputOff + Addend;
1019
5
}
1020
1021
template InputSection::InputSection(ObjFile<ELF32LE> *, const ELF32LE::Shdr *,
1022
                                    StringRef);
1023
template InputSection::InputSection(ObjFile<ELF32BE> *, const ELF32BE::Shdr *,
1024
                                    StringRef);
1025
template InputSection::InputSection(ObjFile<ELF64LE> *, const ELF64LE::Shdr *,
1026
                                    StringRef);
1027
template InputSection::InputSection(ObjFile<ELF64BE> *, const ELF64BE::Shdr *,
1028
                                    StringRef);
1029
1030
template std::string InputSectionBase::getLocation<ELF32LE>(uint64_t);
1031
template std::string InputSectionBase::getLocation<ELF32BE>(uint64_t);
1032
template std::string InputSectionBase::getLocation<ELF64LE>(uint64_t);
1033
template std::string InputSectionBase::getLocation<ELF64BE>(uint64_t);
1034
1035
template std::string InputSectionBase::getSrcMsg<ELF32LE>(uint64_t);
1036
template std::string InputSectionBase::getSrcMsg<ELF32BE>(uint64_t);
1037
template std::string InputSectionBase::getSrcMsg<ELF64LE>(uint64_t);
1038
template std::string InputSectionBase::getSrcMsg<ELF64BE>(uint64_t);
1039
1040
template std::string InputSectionBase::getObjMsg<ELF32LE>(uint64_t);
1041
template std::string InputSectionBase::getObjMsg<ELF32BE>(uint64_t);
1042
template std::string InputSectionBase::getObjMsg<ELF64LE>(uint64_t);
1043
template std::string InputSectionBase::getObjMsg<ELF64BE>(uint64_t);
1044
1045
template void InputSection::writeTo<ELF32LE>(uint8_t *);
1046
template void InputSection::writeTo<ELF32BE>(uint8_t *);
1047
template void InputSection::writeTo<ELF64LE>(uint8_t *);
1048
template void InputSection::writeTo<ELF64BE>(uint8_t *);
1049
1050
template ObjFile<ELF32LE> *InputSectionBase::getFile<ELF32LE>() const;
1051
template ObjFile<ELF32BE> *InputSectionBase::getFile<ELF32BE>() const;
1052
template ObjFile<ELF64LE> *InputSectionBase::getFile<ELF64LE>() const;
1053
template ObjFile<ELF64BE> *InputSectionBase::getFile<ELF64BE>() const;
1054
1055
template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> *,
1056
                                              const ELF32LE::Shdr *, StringRef);
1057
template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> *,
1058
                                              const ELF32BE::Shdr *, StringRef);
1059
template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> *,
1060
                                              const ELF64LE::Shdr *, StringRef);
1061
template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> *,
1062
                                              const ELF64BE::Shdr *, StringRef);
1063
1064
template EhInputSection::EhInputSection(ObjFile<ELF32LE> *,
1065
                                        const ELF32LE::Shdr *, StringRef);
1066
template EhInputSection::EhInputSection(ObjFile<ELF32BE> *,
1067
                                        const ELF32BE::Shdr *, StringRef);
1068
template EhInputSection::EhInputSection(ObjFile<ELF64LE> *,
1069
                                        const ELF64LE::Shdr *, StringRef);
1070
template EhInputSection::EhInputSection(ObjFile<ELF64BE> *,
1071
                                        const ELF64BE::Shdr *, StringRef);
1072
1073
template void EhInputSection::split<ELF32LE>();
1074
template void EhInputSection::split<ELF32BE>();
1075
template void EhInputSection::split<ELF64LE>();
1076
template void EhInputSection::split<ELF64BE>();