Coverage Report

Created: 2019-07-24 05:18

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