Coverage Report

Created: 2019-05-22 02:55

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/InputFiles.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.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 "InputFiles.h"
10
#include "Driver.h"
11
#include "InputSection.h"
12
#include "LinkerScript.h"
13
#include "SymbolTable.h"
14
#include "Symbols.h"
15
#include "SyntheticSections.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/Memory.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/CodeGen/Analysis.h"
20
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
21
#include "llvm/IR/LLVMContext.h"
22
#include "llvm/IR/Module.h"
23
#include "llvm/LTO/LTO.h"
24
#include "llvm/MC/StringTableBuilder.h"
25
#include "llvm/Object/ELFObjectFile.h"
26
#include "llvm/Support/ARMAttributeParser.h"
27
#include "llvm/Support/ARMBuildAttributes.h"
28
#include "llvm/Support/Path.h"
29
#include "llvm/Support/TarWriter.h"
30
#include "llvm/Support/raw_ostream.h"
31
32
using namespace llvm;
33
using namespace llvm::ELF;
34
using namespace llvm::object;
35
using namespace llvm::sys;
36
using namespace llvm::sys::fs;
37
38
using namespace lld;
39
using namespace lld::elf;
40
41
bool InputFile::IsInGroup;
42
uint32_t InputFile::NextGroupId;
43
std::vector<BinaryFile *> elf::BinaryFiles;
44
std::vector<BitcodeFile *> elf::BitcodeFiles;
45
std::vector<LazyObjFile *> elf::LazyObjFiles;
46
std::vector<InputFile *> elf::ObjectFiles;
47
std::vector<SharedFile *> elf::SharedFiles;
48
49
std::unique_ptr<TarWriter> elf::Tar;
50
51
InputFile::InputFile(Kind K, MemoryBufferRef M)
52
2.10k
    : MB(M), GroupId(NextGroupId), FileKind(K) {
53
2.10k
  // All files within the same --{start,end}-group get the same group ID.
54
2.10k
  // Otherwise, a new file will get a new group ID.
55
2.10k
  if (!IsInGroup)
56
2.07k
    ++NextGroupId;
57
2.10k
}
58
59
2.34k
Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
60
2.34k
  // The --chroot option changes our virtual root directory.
61
2.34k
  // This is useful when you are dealing with files created by --reproduce.
62
2.34k
  if (!Config->Chroot.empty() && 
Path.startswith("/")2
)
63
2
    Path = Saver.save(Config->Chroot + Path);
64
2.34k
65
2.34k
  log(Path);
66
2.34k
67
2.34k
  auto MBOrErr = MemoryBuffer::getFile(Path, -1, false);
68
2.34k
  if (auto EC = MBOrErr.getError()) {
69
9
    error("cannot open " + Path + ": " + EC.message());
70
9
    return None;
71
9
  }
72
2.33k
73
2.33k
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
74
2.33k
  MemoryBufferRef MBRef = MB->getMemBufferRef();
75
2.33k
  make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
76
2.33k
77
2.33k
  if (Tar)
78
10
    Tar->append(relativeToRoot(Path), MBRef.getBuffer());
79
2.33k
  return MBRef;
80
2.33k
}
81
82
// All input object files must be for the same architecture
83
// (e.g. it does not make sense to link x86 object files with
84
// MIPS object files.) This function checks for that error.
85
1.93k
static bool isCompatible(InputFile *File) {
86
1.93k
  if (!File->isElf() && 
!isa<BitcodeFile>(File)203
)
87
83
    return true;
88
1.84k
89
1.84k
  if (File->EKind == Config->EKind && 
File->EMachine == Config->EMachine1.84k
) {
90
1.84k
    if (Config->EMachine != EM_MIPS)
91
1.68k
      return true;
92
159
    if (isMipsN32Abi(File) == Config->MipsN32Abi)
93
158
      return true;
94
10
  }
95
10
96
10
  if (!Config->Emulation.empty()) {
97
5
    error(toString(File) + " is incompatible with " + Config->Emulation);
98
5
  } else {
99
5
    InputFile *Existing;
100
5
    if (!ObjectFiles.empty())
101
4
      Existing = ObjectFiles[0];
102
1
    else if (!SharedFiles.empty())
103
1
      Existing = SharedFiles[0];
104
0
    else
105
0
      Existing = BitcodeFiles[0];
106
5
107
5
    error(toString(File) + " is incompatible with " + toString(Existing));
108
5
  }
109
10
110
10
  return false;
111
10
}
112
113
1.93k
template <class ELFT> static void doParseFile(InputFile *File) {
114
1.93k
  // Comdat groups define "link once" sections. If two comdat groups have the
115
1.93k
  // same name, only one of them is linked, and the other is ignored. This set
116
1.93k
  // is used to uniquify them.
117
1.93k
  static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118
1.93k
119
1.93k
  if (!isCompatible(File))
120
10
    return;
121
1.92k
122
1.92k
  // Binary file
123
1.92k
  if (auto *F = dyn_cast<BinaryFile>(File)) {
124
5
    BinaryFiles.push_back(F);
125
5
    F->parse();
126
5
    return;
127
5
  }
128
1.91k
129
1.91k
  // .a file
130
1.91k
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
131
64
    F->parse();
132
64
    return;
133
64
  }
134
1.85k
135
1.85k
  // Lazy object file
136
1.85k
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
137
14
    LazyObjFiles.push_back(F);
138
14
    F->parse<ELFT>();
139
14
    return;
140
14
  }
141
1.83k
142
1.83k
  if (Config->Trace)
143
8
    message(toString(File));
144
1.83k
145
1.83k
  // .so file
146
1.83k
  if (auto *F = dyn_cast<SharedFile>(File)) {
147
187
    F->parse<ELFT>();
148
187
    return;
149
187
  }
150
1.65k
151
1.65k
  // LLVM bitcode file
152
1.65k
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
153
119
    BitcodeFiles.push_back(F);
154
119
    F->parse<ELFT>(ComdatGroups);
155
119
    return;
156
119
  }
157
1.53k
158
1.53k
  // Regular object file
159
1.53k
  ObjectFiles.push_back(File);
160
1.53k
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161
1.53k
}
InputFiles.cpp:void doParseFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputFile*)
Line
Count
Source
113
226
template <class ELFT> static void doParseFile(InputFile *File) {
114
226
  // Comdat groups define "link once" sections. If two comdat groups have the
115
226
  // same name, only one of them is linked, and the other is ignored. This set
116
226
  // is used to uniquify them.
117
226
  static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118
226
119
226
  if (!isCompatible(File))
120
4
    return;
121
222
122
222
  // Binary file
123
222
  if (auto *F = dyn_cast<BinaryFile>(File)) {
124
0
    BinaryFiles.push_back(F);
125
0
    F->parse();
126
0
    return;
127
0
  }
128
222
129
222
  // .a file
130
222
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
131
1
    F->parse();
132
1
    return;
133
1
  }
134
221
135
221
  // Lazy object file
136
221
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
137
0
    LazyObjFiles.push_back(F);
138
0
    F->parse<ELFT>();
139
0
    return;
140
0
  }
141
221
142
221
  if (Config->Trace)
143
0
    message(toString(File));
144
221
145
221
  // .so file
146
221
  if (auto *F = dyn_cast<SharedFile>(File)) {
147
26
    F->parse<ELFT>();
148
26
    return;
149
26
  }
150
195
151
195
  // LLVM bitcode file
152
195
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
153
0
    BitcodeFiles.push_back(F);
154
0
    F->parse<ELFT>(ComdatGroups);
155
0
    return;
156
0
  }
157
195
158
195
  // Regular object file
159
195
  ObjectFiles.push_back(File);
160
195
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161
195
}
InputFiles.cpp:void doParseFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputFile*)
Line
Count
Source
113
110
template <class ELFT> static void doParseFile(InputFile *File) {
114
110
  // Comdat groups define "link once" sections. If two comdat groups have the
115
110
  // same name, only one of them is linked, and the other is ignored. This set
116
110
  // is used to uniquify them.
117
110
  static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118
110
119
110
  if (!isCompatible(File))
120
1
    return;
121
109
122
109
  // Binary file
123
109
  if (auto *F = dyn_cast<BinaryFile>(File)) {
124
0
    BinaryFiles.push_back(F);
125
0
    F->parse();
126
0
    return;
127
0
  }
128
109
129
109
  // .a file
130
109
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
131
0
    F->parse();
132
0
    return;
133
0
  }
134
109
135
109
  // Lazy object file
136
109
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
137
0
    LazyObjFiles.push_back(F);
138
0
    F->parse<ELFT>();
139
0
    return;
140
0
  }
141
109
142
109
  if (Config->Trace)
143
0
    message(toString(File));
144
109
145
109
  // .so file
146
109
  if (auto *F = dyn_cast<SharedFile>(File)) {
147
13
    F->parse<ELFT>();
148
13
    return;
149
13
  }
150
96
151
96
  // LLVM bitcode file
152
96
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
153
0
    BitcodeFiles.push_back(F);
154
0
    F->parse<ELFT>(ComdatGroups);
155
0
    return;
156
0
  }
157
96
158
96
  // Regular object file
159
96
  ObjectFiles.push_back(File);
160
96
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161
96
}
InputFiles.cpp:void doParseFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputFile*)
Line
Count
Source
113
1.50k
template <class ELFT> static void doParseFile(InputFile *File) {
114
1.50k
  // Comdat groups define "link once" sections. If two comdat groups have the
115
1.50k
  // same name, only one of them is linked, and the other is ignored. This set
116
1.50k
  // is used to uniquify them.
117
1.50k
  static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118
1.50k
119
1.50k
  if (!isCompatible(File))
120
2
    return;
121
1.50k
122
1.50k
  // Binary file
123
1.50k
  if (auto *F = dyn_cast<BinaryFile>(File)) {
124
5
    BinaryFiles.push_back(F);
125
5
    F->parse();
126
5
    return;
127
5
  }
128
1.49k
129
1.49k
  // .a file
130
1.49k
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
131
63
    F->parse();
132
63
    return;
133
63
  }
134
1.43k
135
1.43k
  // Lazy object file
136
1.43k
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
137
14
    LazyObjFiles.push_back(F);
138
14
    F->parse<ELFT>();
139
14
    return;
140
14
  }
141
1.41k
142
1.41k
  if (Config->Trace)
143
8
    message(toString(File));
144
1.41k
145
1.41k
  // .so file
146
1.41k
  if (auto *F = dyn_cast<SharedFile>(File)) {
147
136
    F->parse<ELFT>();
148
136
    return;
149
136
  }
150
1.28k
151
1.28k
  // LLVM bitcode file
152
1.28k
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
153
119
    BitcodeFiles.push_back(F);
154
119
    F->parse<ELFT>(ComdatGroups);
155
119
    return;
156
119
  }
157
1.16k
158
1.16k
  // Regular object file
159
1.16k
  ObjectFiles.push_back(File);
160
1.16k
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161
1.16k
}
InputFiles.cpp:void doParseFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputFile*)
Line
Count
Source
113
94
template <class ELFT> static void doParseFile(InputFile *File) {
114
94
  // Comdat groups define "link once" sections. If two comdat groups have the
115
94
  // same name, only one of them is linked, and the other is ignored. This set
116
94
  // is used to uniquify them.
117
94
  static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118
94
119
94
  if (!isCompatible(File))
120
3
    return;
121
91
122
91
  // Binary file
123
91
  if (auto *F = dyn_cast<BinaryFile>(File)) {
124
0
    BinaryFiles.push_back(F);
125
0
    F->parse();
126
0
    return;
127
0
  }
128
91
129
91
  // .a file
130
91
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
131
0
    F->parse();
132
0
    return;
133
0
  }
134
91
135
91
  // Lazy object file
136
91
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
137
0
    LazyObjFiles.push_back(F);
138
0
    F->parse<ELFT>();
139
0
    return;
140
0
  }
141
91
142
91
  if (Config->Trace)
143
0
    message(toString(File));
144
91
145
91
  // .so file
146
91
  if (auto *F = dyn_cast<SharedFile>(File)) {
147
12
    F->parse<ELFT>();
148
12
    return;
149
12
  }
150
79
151
79
  // LLVM bitcode file
152
79
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
153
0
    BitcodeFiles.push_back(F);
154
0
    F->parse<ELFT>(ComdatGroups);
155
0
    return;
156
0
  }
157
79
158
79
  // Regular object file
159
79
  ObjectFiles.push_back(File);
160
79
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161
79
}
162
163
// Add symbols in File to the symbol table.
164
1.93k
void elf::parseFile(InputFile *File) {
165
1.93k
  switch (Config->EKind) {
166
1.93k
  case ELF32LEKind:
167
226
    doParseFile<ELF32LE>(File);
168
226
    return;
169
1.93k
  case ELF32BEKind:
170
110
    doParseFile<ELF32BE>(File);
171
110
    return;
172
1.93k
  case ELF64LEKind:
173
1.50k
    doParseFile<ELF64LE>(File);
174
1.50k
    return;
175
1.93k
  case ELF64BEKind:
176
94
    doParseFile<ELF64BE>(File);
177
94
    return;
178
1.93k
  default:
179
0
    llvm_unreachable("unknown ELFT");
180
1.93k
  }
181
1.93k
}
182
183
// Concatenates arguments to construct a string representing an error location.
184
29
static std::string createFileLineMsg(StringRef Path, unsigned Line) {
185
29
  std::string Filename = path::filename(Path);
186
29
  std::string Lineno = ":" + std::to_string(Line);
187
29
  if (Filename == Path)
188
12
    return Filename + Lineno;
189
17
  return Filename + Lineno + " (" + Path.str() + Lineno + ")";
190
17
}
191
192
template <class ELFT>
193
static std::string getSrcMsgAux(ObjFile<ELFT> &File, const Symbol &Sym,
194
121
                                InputSectionBase &Sec, uint64_t Offset) {
195
121
  // In DWARF, functions and variables are stored to different places.
196
121
  // First, lookup a function for a given offset.
197
121
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
198
17
    return createFileLineMsg(Info->FileName, Info->Line);
199
104
200
104
  // If it failed, lookup again as a variable.
201
104
  if (Optional<std::pair<std::string, unsigned>> FileLine =
202
12
          File.getVariableLoc(Sym.getName()))
203
12
    return createFileLineMsg(FileLine->first, FileLine->second);
204
92
205
92
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
206
92
  return File.SourceFile;
207
92
}
InputFiles.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getSrcMsgAux<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, lld::elf::Symbol const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
194
6
                                InputSectionBase &Sec, uint64_t Offset) {
195
6
  // In DWARF, functions and variables are stored to different places.
196
6
  // First, lookup a function for a given offset.
197
6
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
198
2
    return createFileLineMsg(Info->FileName, Info->Line);
199
4
200
4
  // If it failed, lookup again as a variable.
201
4
  if (Optional<std::pair<std::string, unsigned>> FileLine =
202
0
          File.getVariableLoc(Sym.getName()))
203
0
    return createFileLineMsg(FileLine->first, FileLine->second);
204
4
205
4
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
206
4
  return File.SourceFile;
207
4
}
Unexecuted instantiation: InputFiles.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getSrcMsgAux<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, lld::elf::Symbol const&, lld::elf::InputSectionBase&, unsigned long long)
InputFiles.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getSrcMsgAux<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, lld::elf::Symbol const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
194
115
                                InputSectionBase &Sec, uint64_t Offset) {
195
115
  // In DWARF, functions and variables are stored to different places.
196
115
  // First, lookup a function for a given offset.
197
115
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
198
15
    return createFileLineMsg(Info->FileName, Info->Line);
199
100
200
100
  // If it failed, lookup again as a variable.
201
100
  if (Optional<std::pair<std::string, unsigned>> FileLine =
202
12
          File.getVariableLoc(Sym.getName()))
203
12
    return createFileLineMsg(FileLine->first, FileLine->second);
204
88
205
88
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
206
88
  return File.SourceFile;
207
88
}
Unexecuted instantiation: InputFiles.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getSrcMsgAux<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, lld::elf::Symbol const&, lld::elf::InputSectionBase&, unsigned long long)
208
209
std::string InputFile::getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
210
122
                                 uint64_t Offset) {
211
122
  if (kind() != ObjKind)
212
1
    return "";
213
121
  switch (Config->EKind) {
214
121
  default:
215
0
    llvm_unreachable("Invalid kind");
216
121
  case ELF32LEKind:
217
6
    return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), Sym, Sec, Offset);
218
121
  case ELF32BEKind:
219
0
    return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), Sym, Sec, Offset);
220
121
  case ELF64LEKind:
221
115
    return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), Sym, Sec, Offset);
222
121
  case ELF64BEKind:
223
0
    return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), Sym, Sec, Offset);
224
121
  }
225
121
}
226
227
104
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
228
104
  Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
229
104
  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
230
23
    auto Report = [](Error Err) {
231
4
      handleAllErrors(std::move(Err),
232
4
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const::'lambda'(llvm::ErrorInfoBase&)::operator()(llvm::ErrorInfoBase&) const
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const::'lambda'(llvm::ErrorInfoBase&)::operator()(llvm::ErrorInfoBase&) const
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const::'lambda'(llvm::ErrorInfoBase&)::operator()(llvm::ErrorInfoBase&) const
Line
Count
Source
232
4
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const::'lambda'(llvm::ErrorInfoBase&)::operator()(llvm::ErrorInfoBase&) const
233
4
    };
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const
Line
Count
Source
230
4
    auto Report = [](Error Err) {
231
4
      handleAllErrors(std::move(Err),
232
4
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
233
4
    };
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeDwarf()::'lambda'(llvm::Error)::operator()(llvm::Error) const
234
23
    Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
235
23
        Dwarf->getLineTableForUnit(CU.get(), Report);
236
23
    const DWARFDebugLine::LineTable *LT = nullptr;
237
23
    if (ExpectedLT)
238
21
      LT = *ExpectedLT;
239
2
    else
240
2
      Report(ExpectedLT.takeError());
241
23
    if (!LT)
242
2
      continue;
243
21
    LineTables.push_back(LT);
244
21
245
21
    // Loop over variable records and insert them to VariableLoc.
246
70
    for (const auto &Entry : CU->dies()) {
247
70
      DWARFDie Die(CU.get(), &Entry);
248
70
      // Skip all tags that are not variables.
249
70
      if (Die.getTag() != dwarf::DW_TAG_variable)
250
52
        continue;
251
18
252
18
      // Skip if a local variable because we don't need them for generating
253
18
      // error messages. In general, only non-local symbols can fail to be
254
18
      // linked.
255
18
      if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
256
2
        continue;
257
16
258
16
      // Get the source filename index for the variable.
259
16
      unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
260
16
      if (!LT->hasFileAtIndex(File))
261
2
        continue;
262
14
263
14
      // Get the line number on which the variable is declared.
264
14
      unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
265
14
266
14
      // Here we want to take the variable name to add it into VariableLoc.
267
14
      // Variable can have regular and linkage name associated. At first, we try
268
14
      // to get linkage name as it can be different, for example when we have
269
14
      // two variables in different namespaces of the same object. Use common
270
14
      // name otherwise, but handle the case when it also absent in case if the
271
14
      // input object file lacks some debug info.
272
14
      StringRef Name =
273
14
          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
274
14
                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
275
14
      if (!Name.empty())
276
14
        VariableLoc.insert({Name, {LT, File, Line}});
277
14
    }
278
21
  }
279
104
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeDwarf()
Line
Count
Source
227
10
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
228
10
  Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
229
10
  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
230
2
    auto Report = [](Error Err) {
231
2
      handleAllErrors(std::move(Err),
232
2
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
233
2
    };
234
2
    Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
235
2
        Dwarf->getLineTableForUnit(CU.get(), Report);
236
2
    const DWARFDebugLine::LineTable *LT = nullptr;
237
2
    if (ExpectedLT)
238
2
      LT = *ExpectedLT;
239
0
    else
240
0
      Report(ExpectedLT.takeError());
241
2
    if (!LT)
242
0
      continue;
243
2
    LineTables.push_back(LT);
244
2
245
2
    // Loop over variable records and insert them to VariableLoc.
246
2
    for (const auto &Entry : CU->dies()) {
247
2
      DWARFDie Die(CU.get(), &Entry);
248
2
      // Skip all tags that are not variables.
249
2
      if (Die.getTag() != dwarf::DW_TAG_variable)
250
2
        continue;
251
0
252
0
      // Skip if a local variable because we don't need them for generating
253
0
      // error messages. In general, only non-local symbols can fail to be
254
0
      // linked.
255
0
      if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
256
0
        continue;
257
0
258
0
      // Get the source filename index for the variable.
259
0
      unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
260
0
      if (!LT->hasFileAtIndex(File))
261
0
        continue;
262
0
263
0
      // Get the line number on which the variable is declared.
264
0
      unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
265
0
266
0
      // Here we want to take the variable name to add it into VariableLoc.
267
0
      // Variable can have regular and linkage name associated. At first, we try
268
0
      // to get linkage name as it can be different, for example when we have
269
0
      // two variables in different namespaces of the same object. Use common
270
0
      // name otherwise, but handle the case when it also absent in case if the
271
0
      // input object file lacks some debug info.
272
0
      StringRef Name =
273
0
          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
274
0
                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
275
0
      if (!Name.empty())
276
0
        VariableLoc.insert({Name, {LT, File, Line}});
277
0
    }
278
2
  }
279
10
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeDwarf()
Line
Count
Source
227
1
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
228
1
  Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
229
1
  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
230
0
    auto Report = [](Error Err) {
231
0
      handleAllErrors(std::move(Err),
232
0
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
233
0
    };
234
0
    Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
235
0
        Dwarf->getLineTableForUnit(CU.get(), Report);
236
0
    const DWARFDebugLine::LineTable *LT = nullptr;
237
0
    if (ExpectedLT)
238
0
      LT = *ExpectedLT;
239
0
    else
240
0
      Report(ExpectedLT.takeError());
241
0
    if (!LT)
242
0
      continue;
243
0
    LineTables.push_back(LT);
244
0
245
0
    // Loop over variable records and insert them to VariableLoc.
246
0
    for (const auto &Entry : CU->dies()) {
247
0
      DWARFDie Die(CU.get(), &Entry);
248
0
      // Skip all tags that are not variables.
249
0
      if (Die.getTag() != dwarf::DW_TAG_variable)
250
0
        continue;
251
0
252
0
      // Skip if a local variable because we don't need them for generating
253
0
      // error messages. In general, only non-local symbols can fail to be
254
0
      // linked.
255
0
      if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
256
0
        continue;
257
0
258
0
      // Get the source filename index for the variable.
259
0
      unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
260
0
      if (!LT->hasFileAtIndex(File))
261
0
        continue;
262
0
263
0
      // Get the line number on which the variable is declared.
264
0
      unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
265
0
266
0
      // Here we want to take the variable name to add it into VariableLoc.
267
0
      // Variable can have regular and linkage name associated. At first, we try
268
0
      // to get linkage name as it can be different, for example when we have
269
0
      // two variables in different namespaces of the same object. Use common
270
0
      // name otherwise, but handle the case when it also absent in case if the
271
0
      // input object file lacks some debug info.
272
0
      StringRef Name =
273
0
          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
274
0
                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
275
0
      if (!Name.empty())
276
0
        VariableLoc.insert({Name, {LT, File, Line}});
277
0
    }
278
0
  }
279
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeDwarf()
Line
Count
Source
227
90
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
228
90
  Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
229
90
  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
230
21
    auto Report = [](Error Err) {
231
21
      handleAllErrors(std::move(Err),
232
21
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
233
21
    };
234
21
    Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
235
21
        Dwarf->getLineTableForUnit(CU.get(), Report);
236
21
    const DWARFDebugLine::LineTable *LT = nullptr;
237
21
    if (ExpectedLT)
238
19
      LT = *ExpectedLT;
239
2
    else
240
2
      Report(ExpectedLT.takeError());
241
21
    if (!LT)
242
2
      continue;
243
19
    LineTables.push_back(LT);
244
19
245
19
    // Loop over variable records and insert them to VariableLoc.
246
68
    for (const auto &Entry : CU->dies()) {
247
68
      DWARFDie Die(CU.get(), &Entry);
248
68
      // Skip all tags that are not variables.
249
68
      if (Die.getTag() != dwarf::DW_TAG_variable)
250
50
        continue;
251
18
252
18
      // Skip if a local variable because we don't need them for generating
253
18
      // error messages. In general, only non-local symbols can fail to be
254
18
      // linked.
255
18
      if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
256
2
        continue;
257
16
258
16
      // Get the source filename index for the variable.
259
16
      unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
260
16
      if (!LT->hasFileAtIndex(File))
261
2
        continue;
262
14
263
14
      // Get the line number on which the variable is declared.
264
14
      unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
265
14
266
14
      // Here we want to take the variable name to add it into VariableLoc.
267
14
      // Variable can have regular and linkage name associated. At first, we try
268
14
      // to get linkage name as it can be different, for example when we have
269
14
      // two variables in different namespaces of the same object. Use common
270
14
      // name otherwise, but handle the case when it also absent in case if the
271
14
      // input object file lacks some debug info.
272
14
      StringRef Name =
273
14
          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
274
14
                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
275
14
      if (!Name.empty())
276
14
        VariableLoc.insert({Name, {LT, File, Line}});
277
14
    }
278
19
  }
279
90
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeDwarf()
Line
Count
Source
227
3
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
228
3
  Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
229
3
  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
230
0
    auto Report = [](Error Err) {
231
0
      handleAllErrors(std::move(Err),
232
0
                      [](ErrorInfoBase &Info) { warn(Info.message()); });
233
0
    };
234
0
    Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
235
0
        Dwarf->getLineTableForUnit(CU.get(), Report);
236
0
    const DWARFDebugLine::LineTable *LT = nullptr;
237
0
    if (ExpectedLT)
238
0
      LT = *ExpectedLT;
239
0
    else
240
0
      Report(ExpectedLT.takeError());
241
0
    if (!LT)
242
0
      continue;
243
0
    LineTables.push_back(LT);
244
0
245
0
    // Loop over variable records and insert them to VariableLoc.
246
0
    for (const auto &Entry : CU->dies()) {
247
0
      DWARFDie Die(CU.get(), &Entry);
248
0
      // Skip all tags that are not variables.
249
0
      if (Die.getTag() != dwarf::DW_TAG_variable)
250
0
        continue;
251
0
252
0
      // Skip if a local variable because we don't need them for generating
253
0
      // error messages. In general, only non-local symbols can fail to be
254
0
      // linked.
255
0
      if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
256
0
        continue;
257
0
258
0
      // Get the source filename index for the variable.
259
0
      unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
260
0
      if (!LT->hasFileAtIndex(File))
261
0
        continue;
262
0
263
0
      // Get the line number on which the variable is declared.
264
0
      unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
265
0
266
0
      // Here we want to take the variable name to add it into VariableLoc.
267
0
      // Variable can have regular and linkage name associated. At first, we try
268
0
      // to get linkage name as it can be different, for example when we have
269
0
      // two variables in different namespaces of the same object. Use common
270
0
      // name otherwise, but handle the case when it also absent in case if the
271
0
      // input object file lacks some debug info.
272
0
      StringRef Name =
273
0
          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
274
0
                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
275
0
      if (!Name.empty())
276
0
        VariableLoc.insert({Name, {LT, File, Line}});
277
0
    }
278
0
  }
279
3
}
280
281
// Returns the pair of file name and line number describing location of data
282
// object (variable, array, etc) definition.
283
template <class ELFT>
284
Optional<std::pair<std::string, unsigned>>
285
104
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
286
104
  llvm::call_once(InitDwarfLine, [this]() 
{ initializeDwarf(); }0
);
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getVariableLoc(llvm::StringRef)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getVariableLoc(llvm::StringRef)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getVariableLoc(llvm::StringRef)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getVariableLoc(llvm::StringRef)::'lambda'()::operator()() const
287
104
288
104
  // Return if we have no debug information about data object.
289
104
  auto It = VariableLoc.find(Name);
290
104
  if (It == VariableLoc.end())
291
92
    return None;
292
12
293
12
  // Take file name string from line table.
294
12
  std::string FileName;
295
12
  if (!It->second.LT->getFileNameByIndex(
296
12
          It->second.File, nullptr,
297
12
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
298
0
    return None;
299
12
300
12
  return std::make_pair(FileName, It->second.Line);
301
12
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getVariableLoc(llvm::StringRef)
Line
Count
Source
285
4
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
286
4
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
287
4
288
4
  // Return if we have no debug information about data object.
289
4
  auto It = VariableLoc.find(Name);
290
4
  if (It == VariableLoc.end())
291
4
    return None;
292
0
293
0
  // Take file name string from line table.
294
0
  std::string FileName;
295
0
  if (!It->second.LT->getFileNameByIndex(
296
0
          It->second.File, nullptr,
297
0
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
298
0
    return None;
299
0
300
0
  return std::make_pair(FileName, It->second.Line);
301
0
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getVariableLoc(llvm::StringRef)
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getVariableLoc(llvm::StringRef)
Line
Count
Source
285
100
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
286
100
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
287
100
288
100
  // Return if we have no debug information about data object.
289
100
  auto It = VariableLoc.find(Name);
290
100
  if (It == VariableLoc.end())
291
88
    return None;
292
12
293
12
  // Take file name string from line table.
294
12
  std::string FileName;
295
12
  if (!It->second.LT->getFileNameByIndex(
296
12
          It->second.File, nullptr,
297
12
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
298
0
    return None;
299
12
300
12
  return std::make_pair(FileName, It->second.Line);
301
12
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getVariableLoc(llvm::StringRef)
302
303
// Returns source line information for a given offset
304
// using DWARF debug info.
305
template <class ELFT>
306
Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
307
1.96k
                                                  uint64_t Offset) {
308
1.96k
  llvm::call_once(InitDwarfLine, [this]() 
{ initializeDwarf(); }104
);
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
308
10
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
308
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
308
90
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
308
3
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
309
1.96k
310
1.96k
  // Detect SectionIndex for specified section.
311
1.96k
  uint64_t SectionIndex = object::SectionedAddress::UndefSection;
312
1.96k
  ArrayRef<InputSectionBase *> Sections = S->File->getSections();
313
5.94k
  for (uint64_t CurIndex = 0; CurIndex < Sections.size(); 
++CurIndex3.98k
) {
314
5.94k
    if (S == Sections[CurIndex]) {
315
1.96k
      SectionIndex = CurIndex;
316
1.96k
      break;
317
1.96k
    }
318
5.94k
  }
319
1.96k
320
1.96k
  // Use fake address calcuated by adding section file offset and offset in
321
1.96k
  // section. See comments for ObjectInfo class.
322
1.96k
  DILineInfo Info;
323
1.96k
  for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
324
35
    if (LT->getFileLineInfoForAddress(
325
35
            {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
326
35
            DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
327
18
      return Info;
328
35
  }
329
1.96k
  
return None1.94k
;
330
1.96k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
307
10
                                                  uint64_t Offset) {
308
10
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
309
10
310
10
  // Detect SectionIndex for specified section.
311
10
  uint64_t SectionIndex = object::SectionedAddress::UndefSection;
312
10
  ArrayRef<InputSectionBase *> Sections = S->File->getSections();
313
30
  for (uint64_t CurIndex = 0; CurIndex < Sections.size(); 
++CurIndex20
) {
314
30
    if (S == Sections[CurIndex]) {
315
10
      SectionIndex = CurIndex;
316
10
      break;
317
10
    }
318
30
  }
319
10
320
10
  // Use fake address calcuated by adding section file offset and offset in
321
10
  // section. See comments for ObjectInfo class.
322
10
  DILineInfo Info;
323
10
  for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
324
2
    if (LT->getFileLineInfoForAddress(
325
2
            {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
326
2
            DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
327
2
      return Info;
328
2
  }
329
10
  
return None8
;
330
10
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
307
2
                                                  uint64_t Offset) {
308
2
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
309
2
310
2
  // Detect SectionIndex for specified section.
311
2
  uint64_t SectionIndex = object::SectionedAddress::UndefSection;
312
2
  ArrayRef<InputSectionBase *> Sections = S->File->getSections();
313
6
  for (uint64_t CurIndex = 0; CurIndex < Sections.size(); 
++CurIndex4
) {
314
6
    if (S == Sections[CurIndex]) {
315
2
      SectionIndex = CurIndex;
316
2
      break;
317
2
    }
318
6
  }
319
2
320
2
  // Use fake address calcuated by adding section file offset and offset in
321
2
  // section. See comments for ObjectInfo class.
322
2
  DILineInfo Info;
323
2
  for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
324
0
    if (LT->getFileLineInfoForAddress(
325
0
            {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
326
0
            DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
327
0
      return Info;
328
0
  }
329
2
  return None;
330
2
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
307
137
                                                  uint64_t Offset) {
308
137
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
309
137
310
137
  // Detect SectionIndex for specified section.
311
137
  uint64_t SectionIndex = object::SectionedAddress::UndefSection;
312
137
  ArrayRef<InputSectionBase *> Sections = S->File->getSections();
313
474
  for (uint64_t CurIndex = 0; CurIndex < Sections.size(); 
++CurIndex337
) {
314
474
    if (S == Sections[CurIndex]) {
315
137
      SectionIndex = CurIndex;
316
137
      break;
317
137
    }
318
474
  }
319
137
320
137
  // Use fake address calcuated by adding section file offset and offset in
321
137
  // section. See comments for ObjectInfo class.
322
137
  DILineInfo Info;
323
137
  for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
324
33
    if (LT->getFileLineInfoForAddress(
325
33
            {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
326
33
            DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
327
16
      return Info;
328
33
  }
329
137
  
return None121
;
330
137
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
307
1.81k
                                                  uint64_t Offset) {
308
1.81k
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
309
1.81k
310
1.81k
  // Detect SectionIndex for specified section.
311
1.81k
  uint64_t SectionIndex = object::SectionedAddress::UndefSection;
312
1.81k
  ArrayRef<InputSectionBase *> Sections = S->File->getSections();
313
5.43k
  for (uint64_t CurIndex = 0; CurIndex < Sections.size(); 
++CurIndex3.62k
) {
314
5.43k
    if (S == Sections[CurIndex]) {
315
1.81k
      SectionIndex = CurIndex;
316
1.81k
      break;
317
1.81k
    }
318
5.43k
  }
319
1.81k
320
1.81k
  // Use fake address calcuated by adding section file offset and offset in
321
1.81k
  // section. See comments for ObjectInfo class.
322
1.81k
  DILineInfo Info;
323
1.81k
  for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
324
0
    if (LT->getFileLineInfoForAddress(
325
0
            {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
326
0
            DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
327
0
      return Info;
328
0
  }
329
1.81k
  return None;
330
1.81k
}
331
332
// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
333
2.37k
std::string lld::toString(const InputFile *F) {
334
2.37k
  if (!F)
335
103
    return "<internal>";
336
2.27k
337
2.27k
  if (F->ToStringCache.empty()) {
338
315
    if (F->ArchiveName.empty())
339
305
      F->ToStringCache = F->getName();
340
10
    else
341
10
      F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
342
315
  }
343
2.27k
  return F->ToStringCache;
344
2.27k
}
345
346
1.89k
ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {}
347
348
1.89k
template <class ELFT> void ELFFileBase::parseHeader() {
349
1.89k
  if (ELFT::TargetEndianness == support::little)
350
1.69k
    EKind = ELFT::Is64Bits ? 
ELF64LEKind1.46k
:
ELF32LEKind224
;
351
201
  else
352
201
    EKind = ELFT::Is64Bits ? 
ELF64BEKind91
:
ELF32BEKind110
;
353
1.89k
354
1.89k
  EMachine = getObj<ELFT>().getHeader()->e_machine;
355
1.89k
  OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
356
1.89k
  ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
357
1.89k
}
void lld::elf::ELFFileBase::parseHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
348
224
template <class ELFT> void ELFFileBase::parseHeader() {
349
224
  if (ELFT::TargetEndianness == support::little)
350
224
    EKind = ELFT::Is64Bits ? 
ELF64LEKind0
: ELF32LEKind;
351
0
  else
352
0
    EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
353
224
354
224
  EMachine = getObj<ELFT>().getHeader()->e_machine;
355
224
  OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
356
224
  ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
357
224
}
void lld::elf::ELFFileBase::parseHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
348
110
template <class ELFT> void ELFFileBase::parseHeader() {
349
110
  if (ELFT::TargetEndianness == support::little)
350
0
    EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
351
110
  else
352
110
    EKind = ELFT::Is64Bits ? 
ELF64BEKind0
: ELF32BEKind;
353
110
354
110
  EMachine = getObj<ELFT>().getHeader()->e_machine;
355
110
  OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
356
110
  ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
357
110
}
void lld::elf::ELFFileBase::parseHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
348
1.46k
template <class ELFT> void ELFFileBase::parseHeader() {
349
1.46k
  if (ELFT::TargetEndianness == support::little)
350
1.46k
    EKind = ELFT::Is64Bits ? ELF64LEKind : 
ELF32LEKind0
;
351
0
  else
352
0
    EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
353
1.46k
354
1.46k
  EMachine = getObj<ELFT>().getHeader()->e_machine;
355
1.46k
  OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
356
1.46k
  ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
357
1.46k
}
void lld::elf::ELFFileBase::parseHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
348
91
template <class ELFT> void ELFFileBase::parseHeader() {
349
91
  if (ELFT::TargetEndianness == support::little)
350
0
    EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
351
91
  else
352
91
    EKind = ELFT::Is64Bits ? ELF64BEKind : 
ELF32BEKind0
;
353
91
354
91
  EMachine = getObj<ELFT>().getHeader()->e_machine;
355
91
  OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
356
91
  ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
357
91
}
358
359
template <class ELFT>
360
void ELFFileBase::initSymtab(ArrayRef<typename ELFT::Shdr> Sections,
361
1.85k
                             const typename ELFT::Shdr *Symtab) {
362
1.85k
  FirstGlobal = Symtab->sh_info;
363
1.85k
  ArrayRef<typename ELFT::Sym> ELFSyms =
364
1.85k
      CHECK(getObj<ELFT>().symbols(Symtab), this);
365
1.85k
  if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
366
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
367
1.85k
  this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
368
1.85k
  this->NumELFSyms = ELFSyms.size();
369
1.85k
370
1.85k
  StringTable =
371
1.85k
      CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
372
1.85k
}
void lld::elf::ELFFileBase::initSymtab<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const*)
Line
Count
Source
361
221
                             const typename ELFT::Shdr *Symtab) {
362
221
  FirstGlobal = Symtab->sh_info;
363
221
  ArrayRef<typename ELFT::Sym> ELFSyms =
364
221
      CHECK(getObj<ELFT>().symbols(Symtab), this);
365
221
  if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
366
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
367
221
  this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
368
221
  this->NumELFSyms = ELFSyms.size();
369
221
370
221
  StringTable =
371
221
      CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
372
221
}
void lld::elf::ELFFileBase::initSymtab<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const*)
Line
Count
Source
361
109
                             const typename ELFT::Shdr *Symtab) {
362
109
  FirstGlobal = Symtab->sh_info;
363
109
  ArrayRef<typename ELFT::Sym> ELFSyms =
364
109
      CHECK(getObj<ELFT>().symbols(Symtab), this);
365
109
  if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
366
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
367
109
  this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
368
109
  this->NumELFSyms = ELFSyms.size();
369
109
370
109
  StringTable =
371
109
      CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
372
109
}
void lld::elf::ELFFileBase::initSymtab<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const*)
Line
Count
Source
361
1.43k
                             const typename ELFT::Shdr *Symtab) {
362
1.43k
  FirstGlobal = Symtab->sh_info;
363
1.43k
  ArrayRef<typename ELFT::Sym> ELFSyms =
364
1.43k
      CHECK(getObj<ELFT>().symbols(Symtab), this);
365
1.43k
  if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
366
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
367
1.43k
  this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
368
1.43k
  this->NumELFSyms = ELFSyms.size();
369
1.43k
370
1.43k
  StringTable =
371
1.43k
      CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
372
1.43k
}
void lld::elf::ELFFileBase::initSymtab<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const*)
Line
Count
Source
361
91
                             const typename ELFT::Shdr *Symtab) {
362
91
  FirstGlobal = Symtab->sh_info;
363
91
  ArrayRef<typename ELFT::Sym> ELFSyms =
364
91
      CHECK(getObj<ELFT>().symbols(Symtab), this);
365
91
  if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
366
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
367
91
  this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
368
91
  this->NumELFSyms = ELFSyms.size();
369
91
370
91
  StringTable =
371
91
      CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
372
91
}
373
374
template <class ELFT>
375
ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
376
1.70k
    : ELFFileBase(ObjKind, M) {
377
1.70k
  parseHeader<ELFT>();
378
1.70k
  this->ArchiveName = ArchiveName;
379
1.70k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
376
197
    : ELFFileBase(ObjKind, M) {
377
197
  parseHeader<ELFT>();
378
197
  this->ArchiveName = ArchiveName;
379
197
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
376
97
    : ELFFileBase(ObjKind, M) {
377
97
  parseHeader<ELFT>();
378
97
  this->ArchiveName = ArchiveName;
379
97
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
376
1.33k
    : ELFFileBase(ObjKind, M) {
377
1.33k
  parseHeader<ELFT>();
378
1.33k
  this->ArchiveName = ArchiveName;
379
1.33k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
376
79
    : ELFFileBase(ObjKind, M) {
377
79
  parseHeader<ELFT>();
378
79
  this->ArchiveName = ArchiveName;
379
79
}
380
381
template <class ELFT>
382
82.0k
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
383
82.0k
  return CHECK(
384
82.0k
      this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
385
82.0k
      this);
386
82.0k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSectionIndex(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&) const
Line
Count
Source
382
1.19k
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
383
1.19k
  return CHECK(
384
1.19k
      this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
385
1.19k
      this);
386
1.19k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSectionIndex(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&) const
Line
Count
Source
382
376
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
383
376
  return CHECK(
384
376
      this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
385
376
      this);
386
376
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSectionIndex(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&) const
Line
Count
Source
382
70.1k
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
383
70.1k
  return CHECK(
384
70.1k
      this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
385
70.1k
      this);
386
70.1k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSectionIndex(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&) const
Line
Count
Source
382
10.3k
uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
383
10.3k
  return CHECK(
384
10.3k
      this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
385
10.3k
      this);
386
10.3k
}
387
388
1.57k
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
389
1.57k
  if (this->Symbols.empty())
390
1
    return {};
391
1.57k
  return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
392
1.57k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getLocalSymbols()
Line
Count
Source
388
179
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
389
179
  if (this->Symbols.empty())
390
0
    return {};
391
179
  return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
392
179
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getLocalSymbols()
Line
Count
Source
388
96
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
389
96
  if (this->Symbols.empty())
390
0
    return {};
391
96
  return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
392
96
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getLocalSymbols()
Line
Count
Source
388
1.22k
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
389
1.22k
  if (this->Symbols.empty())
390
1
    return {};
391
1.22k
  return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
392
1.22k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getLocalSymbols()
Line
Count
Source
388
78
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
389
78
  if (this->Symbols.empty())
390
0
    return {};
391
78
  return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
392
78
}
393
394
104
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() {
395
104
  return makeArrayRef(this->Symbols).slice(this->FirstGlobal);
396
104
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getGlobalSymbols()
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getGlobalSymbols()
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getGlobalSymbols()
Line
Count
Source
394
104
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() {
395
104
  return makeArrayRef(this->Symbols).slice(this->FirstGlobal);
396
104
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getGlobalSymbols()
397
398
template <class ELFT>
399
1.63k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
400
1.63k
  // Read a section table. JustSymbols is usually false.
401
1.63k
  if (this->JustSymbols)
402
2
    initializeJustSymbols();
403
1.63k
  else
404
1.63k
    initializeSections(ComdatGroups);
405
1.63k
406
1.63k
  // Read a symbol table.
407
1.63k
  initializeSymbols();
408
1.63k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
399
195
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
400
195
  // Read a section table. JustSymbols is usually false.
401
195
  if (this->JustSymbols)
402
0
    initializeJustSymbols();
403
195
  else
404
195
    initializeSections(ComdatGroups);
405
195
406
195
  // Read a symbol table.
407
195
  initializeSymbols();
408
195
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
399
96
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
400
96
  // Read a section table. JustSymbols is usually false.
401
96
  if (this->JustSymbols)
402
0
    initializeJustSymbols();
403
96
  else
404
96
    initializeSections(ComdatGroups);
405
96
406
96
  // Read a symbol table.
407
96
  initializeSymbols();
408
96
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
399
1.26k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
400
1.26k
  // Read a section table. JustSymbols is usually false.
401
1.26k
  if (this->JustSymbols)
402
2
    initializeJustSymbols();
403
1.26k
  else
404
1.26k
    initializeSections(ComdatGroups);
405
1.26k
406
1.26k
  // Read a symbol table.
407
1.26k
  initializeSymbols();
408
1.26k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
399
79
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
400
79
  // Read a section table. JustSymbols is usually false.
401
79
  if (this->JustSymbols)
402
0
    initializeJustSymbols();
403
79
  else
404
79
    initializeSections(ComdatGroups);
405
79
406
79
  // Read a symbol table.
407
79
  initializeSymbols();
408
79
}
409
410
// Sections with SHT_GROUP and comdat bits define comdat section groups.
411
// They are identified and deduplicated by group name. This function
412
// returns a group name.
413
template <class ELFT>
414
StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
415
30
                                              const Elf_Shdr &Sec) {
416
30
  // Group signatures are stored as symbol names in object files.
417
30
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
418
30
  if (this->getELFSyms<ELFT>().empty())
419
30
    this->initSymtab<ELFT>(
420
30
        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
421
30
422
30
  const Elf_Sym *Sym =
423
30
      CHECK(object::getSymbol<ELFT>(this->getELFSyms<ELFT>(), Sec.sh_info), this);
424
30
  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
425
30
426
30
  // As a special case, if a symbol is a section symbol and has no name,
427
30
  // we use a section name as a signature.
428
30
  //
429
30
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
430
30
  // standard, but GNU gold 1.14 (the newest version as of July 2017) or
431
30
  // older produce such sections as outputs for the -r option, so we need
432
30
  // a bug-compatibility.
433
30
  if (Signature.empty() && 
Sym->getType() == STT_SECTION0
)
434
0
    return getSectionName(Sec);
435
30
  return Signature;
436
30
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getShtGroupSignature(llvm::ArrayRef<llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > >, llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getShtGroupSignature(llvm::ArrayRef<llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > >, llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getShtGroupSignature(llvm::ArrayRef<llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > >, llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
415
30
                                              const Elf_Shdr &Sec) {
416
30
  // Group signatures are stored as symbol names in object files.
417
30
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
418
30
  if (this->getELFSyms<ELFT>().empty())
419
30
    this->initSymtab<ELFT>(
420
30
        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
421
30
422
30
  const Elf_Sym *Sym =
423
30
      CHECK(object::getSymbol<ELFT>(this->getELFSyms<ELFT>(), Sec.sh_info), this);
424
30
  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
425
30
426
30
  // As a special case, if a symbol is a section symbol and has no name,
427
30
  // we use a section name as a signature.
428
30
  //
429
30
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
430
30
  // standard, but GNU gold 1.14 (the newest version as of July 2017) or
431
30
  // older produce such sections as outputs for the -r option, so we need
432
30
  // a bug-compatibility.
433
30
  if (Signature.empty() && 
Sym->getType() == STT_SECTION0
)
434
0
    return getSectionName(Sec);
435
30
  return Signature;
436
30
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getShtGroupSignature(llvm::ArrayRef<llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > >, llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
437
438
332k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
439
332k
  // On a regular link we don't merge sections if -O0 (default is -O1). This
440
332k
  // sometimes makes the linker significantly faster, although the output will
441
332k
  // be bigger.
442
332k
  //
443
332k
  // Doing the same for -r would create a problem as it would combine sections
444
332k
  // with different sh_entsize. One option would be to just copy every SHF_MERGE
445
332k
  // section as is to the output. While this would produce a valid ELF file with
446
332k
  // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
447
332k
  // they see two .debug_str. We could have separate logic for combining
448
332k
  // SHF_MERGE sections based both on their name and sh_entsize, but that seems
449
332k
  // to be more trouble than it is worth. Instead, we just use the regular (-O1)
450
332k
  // logic for -r.
451
332k
  if (Config->Optimize == 0 && 
!Config->Relocatable11
)
452
11
    return false;
453
332k
454
332k
  // A mergeable section with size 0 is useless because they don't have
455
332k
  // any data to merge. A mergeable string section with size 0 can be
456
332k
  // argued as invalid because it doesn't end with a null character.
457
332k
  // We'll avoid a mess by handling them as if they were non-mergeable.
458
332k
  if (Sec.sh_size == 0)
459
329k
    return false;
460
3.01k
461
3.01k
  // Check for sh_entsize. The ELF spec is not clear about the zero
462
3.01k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
463
3.01k
  // the section does not hold a table of fixed-size entries". We know
464
3.01k
  // that Rust 1.13 produces a string mergeable section with a zero
465
3.01k
  // sh_entsize. Here we just accept it rather than being picky about it.
466
3.01k
  uint64_t EntSize = Sec.sh_entsize;
467
3.01k
  if (EntSize == 0)
468
2.69k
    return false;
469
328
  if (Sec.sh_size % EntSize)
470
0
    fatal(toString(this) +
471
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
472
328
473
328
  uint64_t Flags = Sec.sh_flags;
474
328
  if (!(Flags & SHF_MERGE))
475
269
    return false;
476
59
  if (Flags & SHF_WRITE)
477
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
478
59
479
59
  return true;
480
59
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::shouldMerge(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Line
Count
Source
438
611
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
439
611
  // On a regular link we don't merge sections if -O0 (default is -O1). This
440
611
  // sometimes makes the linker significantly faster, although the output will
441
611
  // be bigger.
442
611
  //
443
611
  // Doing the same for -r would create a problem as it would combine sections
444
611
  // with different sh_entsize. One option would be to just copy every SHF_MERGE
445
611
  // section as is to the output. While this would produce a valid ELF file with
446
611
  // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
447
611
  // they see two .debug_str. We could have separate logic for combining
448
611
  // SHF_MERGE sections based both on their name and sh_entsize, but that seems
449
611
  // to be more trouble than it is worth. Instead, we just use the regular (-O1)
450
611
  // logic for -r.
451
611
  if (Config->Optimize == 0 && 
!Config->Relocatable0
)
452
0
    return false;
453
611
454
611
  // A mergeable section with size 0 is useless because they don't have
455
611
  // any data to merge. A mergeable string section with size 0 can be
456
611
  // argued as invalid because it doesn't end with a null character.
457
611
  // We'll avoid a mess by handling them as if they were non-mergeable.
458
611
  if (Sec.sh_size == 0)
459
112
    return false;
460
499
461
499
  // Check for sh_entsize. The ELF spec is not clear about the zero
462
499
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
463
499
  // the section does not hold a table of fixed-size entries". We know
464
499
  // that Rust 1.13 produces a string mergeable section with a zero
465
499
  // sh_entsize. Here we just accept it rather than being picky about it.
466
499
  uint64_t EntSize = Sec.sh_entsize;
467
499
  if (EntSize == 0)
468
468
    return false;
469
31
  if (Sec.sh_size % EntSize)
470
0
    fatal(toString(this) +
471
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
472
31
473
31
  uint64_t Flags = Sec.sh_flags;
474
31
  if (!(Flags & SHF_MERGE))
475
28
    return false;
476
3
  if (Flags & SHF_WRITE)
477
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
478
3
479
3
  return true;
480
3
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::shouldMerge(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
Line
Count
Source
438
479
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
439
479
  // On a regular link we don't merge sections if -O0 (default is -O1). This
440
479
  // sometimes makes the linker significantly faster, although the output will
441
479
  // be bigger.
442
479
  //
443
479
  // Doing the same for -r would create a problem as it would combine sections
444
479
  // with different sh_entsize. One option would be to just copy every SHF_MERGE
445
479
  // section as is to the output. While this would produce a valid ELF file with
446
479
  // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
447
479
  // they see two .debug_str. We could have separate logic for combining
448
479
  // SHF_MERGE sections based both on their name and sh_entsize, but that seems
449
479
  // to be more trouble than it is worth. Instead, we just use the regular (-O1)
450
479
  // logic for -r.
451
479
  if (Config->Optimize == 0 && 
!Config->Relocatable0
)
452
0
    return false;
453
479
454
479
  // A mergeable section with size 0 is useless because they don't have
455
479
  // any data to merge. A mergeable string section with size 0 can be
456
479
  // argued as invalid because it doesn't end with a null character.
457
479
  // We'll avoid a mess by handling them as if they were non-mergeable.
458
479
  if (Sec.sh_size == 0)
459
176
    return false;
460
303
461
303
  // Check for sh_entsize. The ELF spec is not clear about the zero
462
303
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
463
303
  // the section does not hold a table of fixed-size entries". We know
464
303
  // that Rust 1.13 produces a string mergeable section with a zero
465
303
  // sh_entsize. Here we just accept it rather than being picky about it.
466
303
  uint64_t EntSize = Sec.sh_entsize;
467
303
  if (EntSize == 0)
468
127
    return false;
469
176
  if (Sec.sh_size % EntSize)
470
0
    fatal(toString(this) +
471
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
472
176
473
176
  uint64_t Flags = Sec.sh_flags;
474
176
  if (!(Flags & SHF_MERGE))
475
176
    return false;
476
0
  if (Flags & SHF_WRITE)
477
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
478
0
479
0
  return true;
480
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::shouldMerge(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
438
331k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
439
331k
  // On a regular link we don't merge sections if -O0 (default is -O1). This
440
331k
  // sometimes makes the linker significantly faster, although the output will
441
331k
  // be bigger.
442
331k
  //
443
331k
  // Doing the same for -r would create a problem as it would combine sections
444
331k
  // with different sh_entsize. One option would be to just copy every SHF_MERGE
445
331k
  // section as is to the output. While this would produce a valid ELF file with
446
331k
  // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
447
331k
  // they see two .debug_str. We could have separate logic for combining
448
331k
  // SHF_MERGE sections based both on their name and sh_entsize, but that seems
449
331k
  // to be more trouble than it is worth. Instead, we just use the regular (-O1)
450
331k
  // logic for -r.
451
331k
  if (Config->Optimize == 0 && 
!Config->Relocatable11
)
452
11
    return false;
453
331k
454
331k
  // A mergeable section with size 0 is useless because they don't have
455
331k
  // any data to merge. A mergeable string section with size 0 can be
456
331k
  // argued as invalid because it doesn't end with a null character.
457
331k
  // We'll avoid a mess by handling them as if they were non-mergeable.
458
331k
  if (Sec.sh_size == 0)
459
329k
    return false;
460
2.03k
461
2.03k
  // Check for sh_entsize. The ELF spec is not clear about the zero
462
2.03k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
463
2.03k
  // the section does not hold a table of fixed-size entries". We know
464
2.03k
  // that Rust 1.13 produces a string mergeable section with a zero
465
2.03k
  // sh_entsize. Here we just accept it rather than being picky about it.
466
2.03k
  uint64_t EntSize = Sec.sh_entsize;
467
2.03k
  if (EntSize == 0)
468
1.97k
    return false;
469
65
  if (Sec.sh_size % EntSize)
470
0
    fatal(toString(this) +
471
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
472
65
473
65
  uint64_t Flags = Sec.sh_flags;
474
65
  if (!(Flags & SHF_MERGE))
475
9
    return false;
476
56
  if (Flags & SHF_WRITE)
477
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
478
56
479
56
  return true;
480
56
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::shouldMerge(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
Line
Count
Source
438
235
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
439
235
  // On a regular link we don't merge sections if -O0 (default is -O1). This
440
235
  // sometimes makes the linker significantly faster, although the output will
441
235
  // be bigger.
442
235
  //
443
235
  // Doing the same for -r would create a problem as it would combine sections
444
235
  // with different sh_entsize. One option would be to just copy every SHF_MERGE
445
235
  // section as is to the output. While this would produce a valid ELF file with
446
235
  // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
447
235
  // they see two .debug_str. We could have separate logic for combining
448
235
  // SHF_MERGE sections based both on their name and sh_entsize, but that seems
449
235
  // to be more trouble than it is worth. Instead, we just use the regular (-O1)
450
235
  // logic for -r.
451
235
  if (Config->Optimize == 0 && 
!Config->Relocatable0
)
452
0
    return false;
453
235
454
235
  // A mergeable section with size 0 is useless because they don't have
455
235
  // any data to merge. A mergeable string section with size 0 can be
456
235
  // argued as invalid because it doesn't end with a null character.
457
235
  // We'll avoid a mess by handling them as if they were non-mergeable.
458
235
  if (Sec.sh_size == 0)
459
56
    return false;
460
179
461
179
  // Check for sh_entsize. The ELF spec is not clear about the zero
462
179
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
463
179
  // the section does not hold a table of fixed-size entries". We know
464
179
  // that Rust 1.13 produces a string mergeable section with a zero
465
179
  // sh_entsize. Here we just accept it rather than being picky about it.
466
179
  uint64_t EntSize = Sec.sh_entsize;
467
179
  if (EntSize == 0)
468
123
    return false;
469
56
  if (Sec.sh_size % EntSize)
470
0
    fatal(toString(this) +
471
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
472
56
473
56
  uint64_t Flags = Sec.sh_flags;
474
56
  if (!(Flags & SHF_MERGE))
475
56
    return false;
476
0
  if (Flags & SHF_WRITE)
477
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
478
0
479
0
  return true;
480
0
}
481
482
// This is for --just-symbols.
483
//
484
// --just-symbols is a very minor feature that allows you to link your
485
// output against other existing program, so that if you load both your
486
// program and the other program into memory, your output can refer the
487
// other program's symbols.
488
//
489
// When the option is given, we link "just symbols". The section table is
490
// initialized with null pointers.
491
2
template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
492
2
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
493
2
  this->Sections.resize(ObjSections.size());
494
2
495
9
  for (const Elf_Shdr &Sec : ObjSections) {
496
9
    if (Sec.sh_type != SHT_SYMTAB)
497
7
      continue;
498
2
    this->initSymtab<ELFT>(ObjSections, &Sec);
499
2
    return;
500
2
  }
501
2
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeJustSymbols()
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeJustSymbols()
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeJustSymbols()
Line
Count
Source
491
2
template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
492
2
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
493
2
  this->Sections.resize(ObjSections.size());
494
2
495
9
  for (const Elf_Shdr &Sec : ObjSections) {
496
9
    if (Sec.sh_type != SHT_SYMTAB)
497
7
      continue;
498
2
    this->initSymtab<ELFT>(ObjSections, &Sec);
499
2
    return;
500
2
  }
501
2
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeJustSymbols()
502
503
// An ELF object file may contain a `.deplibs` section. If it exists, the
504
// section contains a list of library specifiers such as `m` for libm. This
505
// function resolves a given name by finding the first matching library checking
506
// the various ways that a library can be specified to LLD. This ELF extension
507
// is a form of autolinking and is called `dependent libraries`. It is currently
508
// unique to LLVM and lld.
509
10
static void addDependentLibrary(StringRef Specifier, const InputFile *F) {
510
10
  if (!Config->DependentLibraries)
511
2
    return;
512
8
  if (fs::exists(Specifier))
513
1
    Driver->addFile(Specifier, /*WithLOption=*/false);
514
7
  else if (Optional<std::string> S = findFromSearchPaths(Specifier))
515
0
    Driver->addFile(*S, /*WithLOption=*/true);
516
7
  else if (Optional<std::string> S = searchLibraryBaseName(Specifier))
517
1
    Driver->addFile(*S, /*WithLOption=*/true);
518
6
  else
519
6
    error(toString(F) +
520
6
          ": unable to find library from dependent library specifier: " +
521
6
          Specifier);
522
8
}
523
524
template <class ELFT>
525
void ObjFile<ELFT>::initializeSections(
526
1.63k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
527
1.63k
  const ELFFile<ELFT> &Obj = this->getObj();
528
1.63k
529
1.63k
  ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
530
1.63k
  uint64_t Size = ObjSections.size();
531
1.63k
  this->Sections.resize(Size);
532
1.63k
  this->SectionStringTable =
533
1.63k
      CHECK(Obj.getSectionStringTable(ObjSections), this);
534
1.63k
535
340k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++339k
) {
536
339k
    if (this->Sections[I] == &InputSection::Discarded)
537
14
      continue;
538
339k
    const Elf_Shdr &Sec = ObjSections[I];
539
339k
540
339k
    if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
541
7
      CGProfile =
542
7
          check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
543
339k
544
339k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
545
339k
    // if -r is given, we'll let the final link discard such sections.
546
339k
    // This is compatible with GNU.
547
339k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable115
) {
548
112
      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
549
105
        // We ignore the address-significance table if we know that the object
550
105
        // file was created by objcopy or ld -r. This is because these tools
551
105
        // will reorder the symbols in the symbol table, invalidating the data
552
105
        // in the address-significance table, which refers to symbols by index.
553
105
        if (Sec.sh_link != 0)
554
105
          this->AddrsigSec = &Sec;
555
0
        else if (Config->ICF == ICFLevel::Safe)
556
0
          warn(toString(this) + ": --icf=safe is incompatible with object "
557
0
                                "files created using objcopy or ld -r");
558
105
      }
559
112
      this->Sections[I] = &InputSection::Discarded;
560
112
      continue;
561
112
    }
562
339k
563
339k
    switch (Sec.sh_type) {
564
339k
    case SHT_GROUP: {
565
30
      // De-duplicate section groups by their signatures.
566
30
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
567
30
      this->Sections[I] = &InputSection::Discarded;
568
30
569
30
570
30
      ArrayRef<Elf_Word> Entries =
571
30
          CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
572
30
      if (Entries.empty())
573
0
        fatal(toString(this) + ": empty SHT_GROUP");
574
30
575
30
      // The first word of a SHT_GROUP section contains flags. Currently,
576
30
      // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
577
30
      // An group with the empty flag doesn't define anything; such sections
578
30
      // are just skipped.
579
30
      if (Entries[0] == 0)
580
0
        continue;
581
30
582
30
      if (Entries[0] != GRP_COMDAT)
583
0
        fatal(toString(this) + ": unsupported SHT_GROUP format");
584
30
585
30
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
586
30
      if (IsNew) {
587
19
        if (Config->Relocatable)
588
7
          this->Sections[I] = createInputSection(Sec);
589
19
        continue;
590
19
      }
591
11
592
11
      // Otherwise, discard group members.
593
14
      
for (uint32_t SecIndex : Entries.slice(1))11
{
594
14
        if (SecIndex >= Size)
595
0
          fatal(toString(this) +
596
0
                ": invalid section index in group: " + Twine(SecIndex));
597
14
        this->Sections[SecIndex] = &InputSection::Discarded;
598
14
      }
599
11
      break;
600
11
    }
601
1.63k
    case SHT_SYMTAB:
602
1.63k
      this->initSymtab<ELFT>(ObjSections, &Sec);
603
1.63k
      break;
604
11
    case SHT_SYMTAB_SHNDX:
605
5
      ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
606
5
      break;
607
3.30k
    case SHT_STRTAB:
608
3.30k
    case SHT_NULL:
609
3.30k
      break;
610
334k
    default:
611
334k
      this->Sections[I] = createInputSection(Sec);
612
339k
    }
613
339k
614
339k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
615
339k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
616
339k
    
if (339k
Sec.sh_flags & SHF_LINK_ORDER339k
) {
617
61
      InputSectionBase *LinkSec = nullptr;
618
61
      if (Sec.sh_link < this->Sections.size())
619
61
        LinkSec = this->Sections[Sec.sh_link];
620
61
      if (!LinkSec)
621
0
        fatal(toString(this) +
622
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
623
61
624
61
      InputSection *IS = cast<InputSection>(this->Sections[I]);
625
61
      LinkSec->DependentSections.push_back(IS);
626
61
      if (!isa<InputSection>(LinkSec))
627
1
        error("a section " + IS->Name +
628
1
              " with SHF_LINK_ORDER should not refer a non-regular "
629
1
              "section: " +
630
1
              toString(LinkSec));
631
61
    }
632
339k
  }
633
1.63k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
526
195
    DenseSet<CachedHashStringRef> &ComdatGroups) {
527
195
  const ELFFile<ELFT> &Obj = this->getObj();
528
195
529
195
  ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
530
195
  uint64_t Size = ObjSections.size();
531
195
  this->Sections.resize(Size);
532
195
  this->SectionStringTable =
533
195
      CHECK(Obj.getSectionStringTable(ObjSections), this);
534
195
535
1.64k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++1.44k
) {
536
1.44k
    if (this->Sections[I] == &InputSection::Discarded)
537
0
      continue;
538
1.44k
    const Elf_Shdr &Sec = ObjSections[I];
539
1.44k
540
1.44k
    if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
541
0
      CGProfile =
542
0
          check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
543
1.44k
544
1.44k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
545
1.44k
    // if -r is given, we'll let the final link discard such sections.
546
1.44k
    // This is compatible with GNU.
547
1.44k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
548
0
      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
549
0
        // We ignore the address-significance table if we know that the object
550
0
        // file was created by objcopy or ld -r. This is because these tools
551
0
        // will reorder the symbols in the symbol table, invalidating the data
552
0
        // in the address-significance table, which refers to symbols by index.
553
0
        if (Sec.sh_link != 0)
554
0
          this->AddrsigSec = &Sec;
555
0
        else if (Config->ICF == ICFLevel::Safe)
556
0
          warn(toString(this) + ": --icf=safe is incompatible with object "
557
0
                                "files created using objcopy or ld -r");
558
0
      }
559
0
      this->Sections[I] = &InputSection::Discarded;
560
0
      continue;
561
0
    }
562
1.44k
563
1.44k
    switch (Sec.sh_type) {
564
1.44k
    case SHT_GROUP: {
565
0
      // De-duplicate section groups by their signatures.
566
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
567
0
      this->Sections[I] = &InputSection::Discarded;
568
0
569
0
570
0
      ArrayRef<Elf_Word> Entries =
571
0
          CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
572
0
      if (Entries.empty())
573
0
        fatal(toString(this) + ": empty SHT_GROUP");
574
0
575
0
      // The first word of a SHT_GROUP section contains flags. Currently,
576
0
      // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
577
0
      // An group with the empty flag doesn't define anything; such sections
578
0
      // are just skipped.
579
0
      if (Entries[0] == 0)
580
0
        continue;
581
0
582
0
      if (Entries[0] != GRP_COMDAT)
583
0
        fatal(toString(this) + ": unsupported SHT_GROUP format");
584
0
585
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
586
0
      if (IsNew) {
587
0
        if (Config->Relocatable)
588
0
          this->Sections[I] = createInputSection(Sec);
589
0
        continue;
590
0
      }
591
0
592
0
      // Otherwise, discard group members.
593
0
      for (uint32_t SecIndex : Entries.slice(1)) {
594
0
        if (SecIndex >= Size)
595
0
          fatal(toString(this) +
596
0
                ": invalid section index in group: " + Twine(SecIndex));
597
0
        this->Sections[SecIndex] = &InputSection::Discarded;
598
0
      }
599
0
      break;
600
0
    }
601
195
    case SHT_SYMTAB:
602
195
      this->initSymtab<ELFT>(ObjSections, &Sec);
603
195
      break;
604
0
    case SHT_SYMTAB_SHNDX:
605
0
      ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
606
0
      break;
607
396
    case SHT_STRTAB:
608
396
    case SHT_NULL:
609
396
      break;
610
858
    default:
611
858
      this->Sections[I] = createInputSection(Sec);
612
1.44k
    }
613
1.44k
614
1.44k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
615
1.44k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
616
1.44k
    if (Sec.sh_flags & SHF_LINK_ORDER) {
617
45
      InputSectionBase *LinkSec = nullptr;
618
45
      if (Sec.sh_link < this->Sections.size())
619
45
        LinkSec = this->Sections[Sec.sh_link];
620
45
      if (!LinkSec)
621
0
        fatal(toString(this) +
622
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
623
45
624
45
      InputSection *IS = cast<InputSection>(this->Sections[I]);
625
45
      LinkSec->DependentSections.push_back(IS);
626
45
      if (!isa<InputSection>(LinkSec))
627
0
        error("a section " + IS->Name +
628
0
              " with SHF_LINK_ORDER should not refer a non-regular "
629
0
              "section: " +
630
0
              toString(LinkSec));
631
45
    }
632
1.44k
  }
633
195
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
526
96
    DenseSet<CachedHashStringRef> &ComdatGroups) {
527
96
  const ELFFile<ELFT> &Obj = this->getObj();
528
96
529
96
  ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
530
96
  uint64_t Size = ObjSections.size();
531
96
  this->Sections.resize(Size);
532
96
  this->SectionStringTable =
533
96
      CHECK(Obj.getSectionStringTable(ObjSections), this);
534
96
535
933
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++837
) {
536
837
    if (this->Sections[I] == &InputSection::Discarded)
537
0
      continue;
538
837
    const Elf_Shdr &Sec = ObjSections[I];
539
837
540
837
    if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
541
0
      CGProfile =
542
0
          check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
543
837
544
837
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
545
837
    // if -r is given, we'll let the final link discard such sections.
546
837
    // This is compatible with GNU.
547
837
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
548
0
      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
549
0
        // We ignore the address-significance table if we know that the object
550
0
        // file was created by objcopy or ld -r. This is because these tools
551
0
        // will reorder the symbols in the symbol table, invalidating the data
552
0
        // in the address-significance table, which refers to symbols by index.
553
0
        if (Sec.sh_link != 0)
554
0
          this->AddrsigSec = &Sec;
555
0
        else if (Config->ICF == ICFLevel::Safe)
556
0
          warn(toString(this) + ": --icf=safe is incompatible with object "
557
0
                                "files created using objcopy or ld -r");
558
0
      }
559
0
      this->Sections[I] = &InputSection::Discarded;
560
0
      continue;
561
0
    }
562
837
563
837
    switch (Sec.sh_type) {
564
837
    case SHT_GROUP: {
565
0
      // De-duplicate section groups by their signatures.
566
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
567
0
      this->Sections[I] = &InputSection::Discarded;
568
0
569
0
570
0
      ArrayRef<Elf_Word> Entries =
571
0
          CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
572
0
      if (Entries.empty())
573
0
        fatal(toString(this) + ": empty SHT_GROUP");
574
0
575
0
      // The first word of a SHT_GROUP section contains flags. Currently,
576
0
      // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
577
0
      // An group with the empty flag doesn't define anything; such sections
578
0
      // are just skipped.
579
0
      if (Entries[0] == 0)
580
0
        continue;
581
0
582
0
      if (Entries[0] != GRP_COMDAT)
583
0
        fatal(toString(this) + ": unsupported SHT_GROUP format");
584
0
585
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
586
0
      if (IsNew) {
587
0
        if (Config->Relocatable)
588
0
          this->Sections[I] = createInputSection(Sec);
589
0
        continue;
590
0
      }
591
0
592
0
      // Otherwise, discard group members.
593
0
      for (uint32_t SecIndex : Entries.slice(1)) {
594
0
        if (SecIndex >= Size)
595
0
          fatal(toString(this) +
596
0
                ": invalid section index in group: " + Twine(SecIndex));
597
0
        this->Sections[SecIndex] = &InputSection::Discarded;
598
0
      }
599
0
      break;
600
0
    }
601
96
    case SHT_SYMTAB:
602
96
      this->initSymtab<ELFT>(ObjSections, &Sec);
603
96
      break;
604
0
    case SHT_SYMTAB_SHNDX:
605
0
      ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
606
0
      break;
607
200
    case SHT_STRTAB:
608
200
    case SHT_NULL:
609
200
      break;
610
541
    default:
611
541
      this->Sections[I] = createInputSection(Sec);
612
837
    }
613
837
614
837
    // .ARM.exidx sections have a reverse dependency on the InputSection they
615
837
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
616
837
    if (Sec.sh_flags & SHF_LINK_ORDER) {
617
0
      InputSectionBase *LinkSec = nullptr;
618
0
      if (Sec.sh_link < this->Sections.size())
619
0
        LinkSec = this->Sections[Sec.sh_link];
620
0
      if (!LinkSec)
621
0
        fatal(toString(this) +
622
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
623
0
624
0
      InputSection *IS = cast<InputSection>(this->Sections[I]);
625
0
      LinkSec->DependentSections.push_back(IS);
626
0
      if (!isa<InputSection>(LinkSec))
627
0
        error("a section " + IS->Name +
628
0
              " with SHF_LINK_ORDER should not refer a non-regular "
629
0
              "section: " +
630
0
              toString(LinkSec));
631
0
    }
632
837
  }
633
96
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
526
1.26k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
527
1.26k
  const ELFFile<ELFT> &Obj = this->getObj();
528
1.26k
529
1.26k
  ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
530
1.26k
  uint64_t Size = ObjSections.size();
531
1.26k
  this->Sections.resize(Size);
532
1.26k
  this->SectionStringTable =
533
1.26k
      CHECK(Obj.getSectionStringTable(ObjSections), this);
534
1.26k
535
337k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++336k
) {
536
336k
    if (this->Sections[I] == &InputSection::Discarded)
537
14
      continue;
538
336k
    const Elf_Shdr &Sec = ObjSections[I];
539
336k
540
336k
    if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
541
7
      CGProfile =
542
7
          check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
543
336k
544
336k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
545
336k
    // if -r is given, we'll let the final link discard such sections.
546
336k
    // This is compatible with GNU.
547
336k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable115
) {
548
112
      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
549
105
        // We ignore the address-significance table if we know that the object
550
105
        // file was created by objcopy or ld -r. This is because these tools
551
105
        // will reorder the symbols in the symbol table, invalidating the data
552
105
        // in the address-significance table, which refers to symbols by index.
553
105
        if (Sec.sh_link != 0)
554
105
          this->AddrsigSec = &Sec;
555
0
        else if (Config->ICF == ICFLevel::Safe)
556
0
          warn(toString(this) + ": --icf=safe is incompatible with object "
557
0
                                "files created using objcopy or ld -r");
558
105
      }
559
112
      this->Sections[I] = &InputSection::Discarded;
560
112
      continue;
561
112
    }
562
336k
563
336k
    switch (Sec.sh_type) {
564
336k
    case SHT_GROUP: {
565
30
      // De-duplicate section groups by their signatures.
566
30
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
567
30
      this->Sections[I] = &InputSection::Discarded;
568
30
569
30
570
30
      ArrayRef<Elf_Word> Entries =
571
30
          CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
572
30
      if (Entries.empty())
573
0
        fatal(toString(this) + ": empty SHT_GROUP");
574
30
575
30
      // The first word of a SHT_GROUP section contains flags. Currently,
576
30
      // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
577
30
      // An group with the empty flag doesn't define anything; such sections
578
30
      // are just skipped.
579
30
      if (Entries[0] == 0)
580
0
        continue;
581
30
582
30
      if (Entries[0] != GRP_COMDAT)
583
0
        fatal(toString(this) + ": unsupported SHT_GROUP format");
584
30
585
30
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
586
30
      if (IsNew) {
587
19
        if (Config->Relocatable)
588
7
          this->Sections[I] = createInputSection(Sec);
589
19
        continue;
590
19
      }
591
11
592
11
      // Otherwise, discard group members.
593
14
      
for (uint32_t SecIndex : Entries.slice(1))11
{
594
14
        if (SecIndex >= Size)
595
0
          fatal(toString(this) +
596
0
                ": invalid section index in group: " + Twine(SecIndex));
597
14
        this->Sections[SecIndex] = &InputSection::Discarded;
598
14
      }
599
11
      break;
600
11
    }
601
1.26k
    case SHT_SYMTAB:
602
1.26k
      this->initSymtab<ELFT>(ObjSections, &Sec);
603
1.26k
      break;
604
11
    case SHT_SYMTAB_SHNDX:
605
5
      ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
606
5
      break;
607
2.54k
    case SHT_STRTAB:
608
2.54k
    case SHT_NULL:
609
2.54k
      break;
610
332k
    default:
611
332k
      this->Sections[I] = createInputSection(Sec);
612
336k
    }
613
336k
614
336k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
615
336k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
616
336k
    
if (336k
Sec.sh_flags & SHF_LINK_ORDER336k
) {
617
16
      InputSectionBase *LinkSec = nullptr;
618
16
      if (Sec.sh_link < this->Sections.size())
619
16
        LinkSec = this->Sections[Sec.sh_link];
620
16
      if (!LinkSec)
621
0
        fatal(toString(this) +
622
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
623
16
624
16
      InputSection *IS = cast<InputSection>(this->Sections[I]);
625
16
      LinkSec->DependentSections.push_back(IS);
626
16
      if (!isa<InputSection>(LinkSec))
627
1
        error("a section " + IS->Name +
628
1
              " with SHF_LINK_ORDER should not refer a non-regular "
629
1
              "section: " +
630
1
              toString(LinkSec));
631
16
    }
632
336k
  }
633
1.26k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
526
79
    DenseSet<CachedHashStringRef> &ComdatGroups) {
527
79
  const ELFFile<ELFT> &Obj = this->getObj();
528
79
529
79
  ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
530
79
  uint64_t Size = ObjSections.size();
531
79
  this->Sections.resize(Size);
532
79
  this->SectionStringTable =
533
79
      CHECK(Obj.getSectionStringTable(ObjSections), this);
534
79
535
632
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++553
) {
536
553
    if (this->Sections[I] == &InputSection::Discarded)
537
0
      continue;
538
553
    const Elf_Shdr &Sec = ObjSections[I];
539
553
540
553
    if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
541
0
      CGProfile =
542
0
          check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
543
553
544
553
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
545
553
    // if -r is given, we'll let the final link discard such sections.
546
553
    // This is compatible with GNU.
547
553
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
548
0
      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
549
0
        // We ignore the address-significance table if we know that the object
550
0
        // file was created by objcopy or ld -r. This is because these tools
551
0
        // will reorder the symbols in the symbol table, invalidating the data
552
0
        // in the address-significance table, which refers to symbols by index.
553
0
        if (Sec.sh_link != 0)
554
0
          this->AddrsigSec = &Sec;
555
0
        else if (Config->ICF == ICFLevel::Safe)
556
0
          warn(toString(this) + ": --icf=safe is incompatible with object "
557
0
                                "files created using objcopy or ld -r");
558
0
      }
559
0
      this->Sections[I] = &InputSection::Discarded;
560
0
      continue;
561
0
    }
562
553
563
553
    switch (Sec.sh_type) {
564
553
    case SHT_GROUP: {
565
0
      // De-duplicate section groups by their signatures.
566
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
567
0
      this->Sections[I] = &InputSection::Discarded;
568
0
569
0
570
0
      ArrayRef<Elf_Word> Entries =
571
0
          CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
572
0
      if (Entries.empty())
573
0
        fatal(toString(this) + ": empty SHT_GROUP");
574
0
575
0
      // The first word of a SHT_GROUP section contains flags. Currently,
576
0
      // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
577
0
      // An group with the empty flag doesn't define anything; such sections
578
0
      // are just skipped.
579
0
      if (Entries[0] == 0)
580
0
        continue;
581
0
582
0
      if (Entries[0] != GRP_COMDAT)
583
0
        fatal(toString(this) + ": unsupported SHT_GROUP format");
584
0
585
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
586
0
      if (IsNew) {
587
0
        if (Config->Relocatable)
588
0
          this->Sections[I] = createInputSection(Sec);
589
0
        continue;
590
0
      }
591
0
592
0
      // Otherwise, discard group members.
593
0
      for (uint32_t SecIndex : Entries.slice(1)) {
594
0
        if (SecIndex >= Size)
595
0
          fatal(toString(this) +
596
0
                ": invalid section index in group: " + Twine(SecIndex));
597
0
        this->Sections[SecIndex] = &InputSection::Discarded;
598
0
      }
599
0
      break;
600
0
    }
601
79
    case SHT_SYMTAB:
602
79
      this->initSymtab<ELFT>(ObjSections, &Sec);
603
79
      break;
604
0
    case SHT_SYMTAB_SHNDX:
605
0
      ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
606
0
      break;
607
164
    case SHT_STRTAB:
608
164
    case SHT_NULL:
609
164
      break;
610
310
    default:
611
310
      this->Sections[I] = createInputSection(Sec);
612
553
    }
613
553
614
553
    // .ARM.exidx sections have a reverse dependency on the InputSection they
615
553
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
616
553
    if (Sec.sh_flags & SHF_LINK_ORDER) {
617
0
      InputSectionBase *LinkSec = nullptr;
618
0
      if (Sec.sh_link < this->Sections.size())
619
0
        LinkSec = this->Sections[Sec.sh_link];
620
0
      if (!LinkSec)
621
0
        fatal(toString(this) +
622
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
623
0
624
0
      InputSection *IS = cast<InputSection>(this->Sections[I]);
625
0
      LinkSec->DependentSections.push_back(IS);
626
0
      if (!isa<InputSection>(LinkSec))
627
0
        error("a section " + IS->Name +
628
0
              " with SHF_LINK_ORDER should not refer a non-regular "
629
0
              "section: " +
630
0
              toString(LinkSec));
631
0
    }
632
553
  }
633
79
}
634
635
// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
636
// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
637
// the input objects have been compiled.
638
static void updateARMVFPArgs(const ARMAttributeParser &Attributes,
639
47
                             const InputFile *F) {
640
47
  if (!Attributes.hasAttribute(ARMBuildAttrs::ABI_VFP_args))
641
34
    // If an ABI tag isn't present then it is implicitly given the value of 0
642
34
    // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
643
34
    // including some in glibc that don't use FP args (and should have value 3)
644
34
    // don't have the attribute so we do not consider an implicit value of 0
645
34
    // as a clash.
646
34
    return;
647
13
648
13
  unsigned VFPArgs = Attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
649
13
  ARMVFPArgKind Arg;
650
13
  switch (VFPArgs) {
651
13
  case ARMBuildAttrs::BaseAAPCS:
652
4
    Arg = ARMVFPArgKind::Base;
653
4
    break;
654
13
  case ARMBuildAttrs::HardFPAAPCS:
655
3
    Arg = ARMVFPArgKind::VFP;
656
3
    break;
657
13
  case ARMBuildAttrs::ToolChainFPPCS:
658
2
    // Tool chain specific convention that conforms to neither AAPCS variant.
659
2
    Arg = ARMVFPArgKind::ToolChain;
660
2
    break;
661
13
  case ARMBuildAttrs::CompatibleFPAAPCS:
662
4
    // Object compatible with all conventions.
663
4
    return;
664
13
  default:
665
0
    error(toString(F) + ": unknown Tag_ABI_VFP_args value: " + Twine(VFPArgs));
666
0
    return;
667
9
  }
668
9
  // Follow ld.bfd and error if there is a mix of calling conventions.
669
9
  if (Config->ARMVFPArgs != Arg && Config->ARMVFPArgs != ARMVFPArgKind::Default)
670
4
    error(toString(F) + ": incompatible Tag_ABI_VFP_args");
671
5
  else
672
5
    Config->ARMVFPArgs = Arg;
673
9
}
674
675
// The ARM support in lld makes some use of instructions that are not available
676
// on all ARM architectures. Namely:
677
// - Use of BLX instruction for interworking between ARM and Thumb state.
678
// - Use of the extended Thumb branch encoding in relocation.
679
// - Use of the MOVT/MOVW instructions in Thumb Thunks.
680
// The ARM Attributes section contains information about the architecture chosen
681
// at compile time. We follow the convention that if at least one input object
682
// is compiled with an architecture that supports these features then lld is
683
// permitted to use them.
684
47
static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) {
685
47
  if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
686
0
    return;
687
47
  auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
688
47
  switch (Arch) {
689
47
  case ARMBuildAttrs::Pre_v4:
690
1
  case ARMBuildAttrs::v4:
691
1
  case ARMBuildAttrs::v4T:
692
1
    // Architectures prior to v5 do not support BLX instruction
693
1
    break;
694
3
  case ARMBuildAttrs::v5T:
695
3
  case ARMBuildAttrs::v5TE:
696
3
  case ARMBuildAttrs::v5TEJ:
697
3
  case ARMBuildAttrs::v6:
698
3
  case ARMBuildAttrs::v6KZ:
699
3
  case ARMBuildAttrs::v6K:
700
3
    Config->ARMHasBlx = true;
701
3
    // Architectures used in pre-Cortex processors do not support
702
3
    // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
703
3
    // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
704
3
    break;
705
43
  default:
706
43
    // All other Architectures have BLX and extended branch encoding
707
43
    Config->ARMHasBlx = true;
708
43
    Config->ARMJ1J2BranchEncoding = true;
709
43
    if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M)
710
42
      // All Architectures used in Cortex processors with the exception
711
42
      // of v6-M and v6S-M have the MOVT and MOVW instructions.
712
42
      Config->ARMHasMovtMovw = true;
713
43
    break;
714
47
  }
715
47
}
716
717
template <class ELFT>
718
1.03k
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
719
1.03k
  uint32_t Idx = Sec.sh_info;
720
1.03k
  if (Idx >= this->Sections.size())
721
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
722
1.03k
  InputSectionBase *Target = this->Sections[Idx];
723
1.03k
724
1.03k
  // Strictly speaking, a relocation section must be included in the
725
1.03k
  // group of the section it relocates. However, LLVM 3.3 and earlier
726
1.03k
  // would fail to do so, so we gracefully handle that case.
727
1.03k
  if (Target == &InputSection::Discarded)
728
4
    return nullptr;
729
1.02k
730
1.02k
  if (!Target)
731
0
    fatal(toString(this) + ": unsupported relocation reference");
732
1.02k
  return Target;
733
1.02k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getRelocTarget(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Line
Count
Source
718
198
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
719
198
  uint32_t Idx = Sec.sh_info;
720
198
  if (Idx >= this->Sections.size())
721
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
722
198
  InputSectionBase *Target = this->Sections[Idx];
723
198
724
198
  // Strictly speaking, a relocation section must be included in the
725
198
  // group of the section it relocates. However, LLVM 3.3 and earlier
726
198
  // would fail to do so, so we gracefully handle that case.
727
198
  if (Target == &InputSection::Discarded)
728
0
    return nullptr;
729
198
730
198
  if (!Target)
731
0
    fatal(toString(this) + ": unsupported relocation reference");
732
198
  return Target;
733
198
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getRelocTarget(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
Line
Count
Source
718
58
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
719
58
  uint32_t Idx = Sec.sh_info;
720
58
  if (Idx >= this->Sections.size())
721
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
722
58
  InputSectionBase *Target = this->Sections[Idx];
723
58
724
58
  // Strictly speaking, a relocation section must be included in the
725
58
  // group of the section it relocates. However, LLVM 3.3 and earlier
726
58
  // would fail to do so, so we gracefully handle that case.
727
58
  if (Target == &InputSection::Discarded)
728
0
    return nullptr;
729
58
730
58
  if (!Target)
731
0
    fatal(toString(this) + ": unsupported relocation reference");
732
58
  return Target;
733
58
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getRelocTarget(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
718
705
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
719
705
  uint32_t Idx = Sec.sh_info;
720
705
  if (Idx >= this->Sections.size())
721
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
722
705
  InputSectionBase *Target = this->Sections[Idx];
723
705
724
705
  // Strictly speaking, a relocation section must be included in the
725
705
  // group of the section it relocates. However, LLVM 3.3 and earlier
726
705
  // would fail to do so, so we gracefully handle that case.
727
705
  if (Target == &InputSection::Discarded)
728
4
    return nullptr;
729
701
730
701
  if (!Target)
731
0
    fatal(toString(this) + ": unsupported relocation reference");
732
701
  return Target;
733
701
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getRelocTarget(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
Line
Count
Source
718
70
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
719
70
  uint32_t Idx = Sec.sh_info;
720
70
  if (Idx >= this->Sections.size())
721
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
722
70
  InputSectionBase *Target = this->Sections[Idx];
723
70
724
70
  // Strictly speaking, a relocation section must be included in the
725
70
  // group of the section it relocates. However, LLVM 3.3 and earlier
726
70
  // would fail to do so, so we gracefully handle that case.
727
70
  if (Target == &InputSection::Discarded)
728
0
    return nullptr;
729
70
730
70
  if (!Target)
731
0
    fatal(toString(this) + ": unsupported relocation reference");
732
70
  return Target;
733
70
}
734
735
// Create a regular InputSection class that has the same contents
736
// as a given section.
737
3
static InputSection *toRegularSection(MergeInputSection *Sec) {
738
3
  return make<InputSection>(Sec->File, Sec->Flags, Sec->Type, Sec->Alignment,
739
3
                            Sec->data(), Sec->Name);
740
3
}
741
742
template <class ELFT>
743
334k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
744
334k
  StringRef Name = getSectionName(Sec);
745
334k
746
334k
  switch (Sec.sh_type) {
747
334k
  case SHT_ARM_ATTRIBUTES: {
748
47
    if (Config->EMachine != EM_ARM)
749
0
      break;
750
47
    ARMAttributeParser Attributes;
751
47
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
752
47
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
753
47
    updateSupportedARMFeatures(Attributes);
754
47
    updateARMVFPArgs(Attributes, this);
755
47
756
47
    // FIXME: Retain the first attribute section we see. The eglibc ARM
757
47
    // dynamic loaders require the presence of an attribute section for dlopen
758
47
    // to work. In a full implementation we would merge all attribute sections.
759
47
    if (In.ARMAttributes == nullptr) {
760
35
      In.ARMAttributes = make<InputSection>(*this, Sec, Name);
761
35
      return In.ARMAttributes;
762
35
    }
763
12
    return &InputSection::Discarded;
764
12
  }
765
12
  case SHT_LLVM_DEPENDENT_LIBRARIES: {
766
7
    if (Config->Relocatable)
767
2
      break;
768
5
    ArrayRef<char> Data =
769
5
        CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this);
770
5
    if (!Data.empty() && Data.back() != '\0') {
771
1
      error(toString(this) +
772
1
            ": corrupted dependent libraries section (unterminated string): " +
773
1
            Name);
774
1
      return &InputSection::Discarded;
775
1
    }
776
12
    
for (const char *D = Data.begin(), *E = Data.end(); 4
D < E;) {
777
8
      StringRef S(D);
778
8
      addDependentLibrary(S, this);
779
8
      D += S.size() + 1;
780
8
    }
781
4
    return &InputSection::Discarded;
782
4
  }
783
1.03k
  case SHT_RELA:
784
1.03k
  case SHT_REL: {
785
1.03k
    // Find a relocation target section and associate this section with that.
786
1.03k
    // Target may have been discarded if it is in a different section group
787
1.03k
    // and the group is discarded, even though it's a violation of the
788
1.03k
    // spec. We handle that situation gracefully by discarding dangling
789
1.03k
    // relocation sections.
790
1.03k
    InputSectionBase *Target = getRelocTarget(Sec);
791
1.03k
    if (!Target)
792
4
      return nullptr;
793
1.02k
794
1.02k
    // This section contains relocation information.
795
1.02k
    // If -r is given, we do not interpret or apply relocation
796
1.02k
    // but just copy relocation sections to output.
797
1.02k
    if (Config->Relocatable) {
798
51
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
799
51
      // We want to add a dependency to target, similar like we do for
800
51
      // -emit-relocs below. This is useful for the case when linker script
801
51
      // contains the "/DISCARD/". It is perhaps uncommon to use a script with
802
51
      // -r, but we faced it in the Linux kernel and have to handle such case
803
51
      // and not to crash.
804
51
      Target->DependentSections.push_back(RelocSec);
805
51
      return RelocSec;
806
51
    }
807
976
808
976
    if (Target->FirstRelocation)
809
0
      fatal(toString(this) +
810
0
            ": multiple relocation sections to one section are not supported");
811
976
812
976
    // ELF spec allows mergeable sections with relocations, but they are
813
976
    // rare, and it is in practice hard to merge such sections by contents,
814
976
    // because applying relocations at end of linking changes section
815
976
    // contents. So, we simply handle such sections as non-mergeable ones.
816
976
    // Degrading like this is acceptable because section merging is optional.
817
976
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
818
3
      Target = toRegularSection(MS);
819
3
      this->Sections[Sec.sh_info] = Target;
820
3
    }
821
976
822
976
    if (Sec.sh_type == SHT_RELA) {
823
750
      ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
824
750
      Target->FirstRelocation = Rels.begin();
825
750
      Target->NumRelocations = Rels.size();
826
750
      Target->AreRelocsRela = true;
827
750
    } else {
828
226
      ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
829
226
      Target->FirstRelocation = Rels.begin();
830
226
      Target->NumRelocations = Rels.size();
831
226
      Target->AreRelocsRela = false;
832
226
    }
833
976
    assert(isUInt<31>(Target->NumRelocations));
834
976
835
976
    // Relocation sections processed by the linker are usually removed
836
976
    // from the output, so returning `nullptr` for the normal case.
837
976
    // However, if -emit-relocs is given, we need to leave them in the output.
838
976
    // (Some post link analysis tools need this information.)
839
976
    if (Config->EmitRelocs) {
840
20
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
841
20
      // We will not emit relocation section if target was discarded.
842
20
      Target->DependentSections.push_back(RelocSec);
843
20
      return RelocSec;
844
20
    }
845
956
    return nullptr;
846
956
  }
847
333k
  }
848
333k
849
333k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
850
333k
  // that the code in the object file does not expect that the stack is
851
333k
  // executable (in terms of NX bit). If all input files have the marker,
852
333k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
853
333k
  // make the stack non-executable. Most object files have this section as
854
333k
  // of 2017.
855
333k
  //
856
333k
  // But making the stack non-executable is a norm today for security
857
333k
  // reasons. Failure to do so may result in a serious security issue.
858
333k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
859
333k
  // explicitly told to do otherwise (by -z execstack). Because the stack
860
333k
  // executable-ness is controlled solely by command line options,
861
333k
  // .note.GNU-stack sections are simply ignored.
862
333k
  if (Name == ".note.GNU-stack")
863
121
    return &InputSection::Discarded;
864
333k
865
333k
  // Split stacks is a feature to support a discontiguous stack,
866
333k
  // commonly used in the programming language Go. For the details,
867
333k
  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
868
333k
  // for split stack will include a .note.GNU-split-stack section.
869
333k
  if (Name == ".note.GNU-split-stack") {
870
11
    if (Config->Relocatable) {
871
2
      error("cannot mix split-stack and non-split-stack in a relocatable link");
872
2
      return &InputSection::Discarded;
873
2
    }
874
9
    this->SplitStack = true;
875
9
    return &InputSection::Discarded;
876
9
  }
877
333k
878
333k
  // An object file cmpiled for split stack, but where some of the
879
333k
  // functions were compiled with the no_split_stack_attribute will
880
333k
  // include a .note.GNU-no-split-stack section.
881
333k
  if (Name == ".note.GNU-no-split-stack") {
882
0
    this->SomeNoSplitStack = true;
883
0
    return &InputSection::Discarded;
884
0
  }
885
333k
886
333k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
887
333k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
888
333k
  // sections. Drop those sections to avoid duplicate symbol errors.
889
333k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
890
333k
  // fixed for a while.
891
333k
  if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
892
333k
      Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
893
0
    return &InputSection::Discarded;
894
333k
895
333k
  // If we are creating a new .build-id section, strip existing .build-id
896
333k
  // sections so that the output won't have more than one .build-id.
897
333k
  // This is not usually a problem because input object files normally don't
898
333k
  // have .build-id sections, but you can create such files by
899
333k
  // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
900
333k
  if (Name == ".note.gnu.build-id" && 
Config->BuildId != BuildIdKind::None1
)
901
1
    return &InputSection::Discarded;
902
333k
903
333k
  // The linker merges EH (exception handling) frames and creates a
904
333k
  // .eh_frame_hdr section for runtime. So we handle them with a special
905
333k
  // class. For relocatable outputs, they are just passed through.
906
333k
  if (Name == ".eh_frame" && 
!Config->Relocatable65
)
907
60
    return make<EhInputSection>(*this, Sec, Name);
908
332k
909
332k
  if (shouldMerge(Sec))
910
59
    return make<MergeInputSection>(*this, Sec, Name);
911
332k
  return make<InputSection>(*this, Sec, Name);
912
332k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::createInputSection(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Line
Count
Source
743
858
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
744
858
  StringRef Name = getSectionName(Sec);
745
858
746
858
  switch (Sec.sh_type) {
747
858
  case SHT_ARM_ATTRIBUTES: {
748
47
    if (Config->EMachine != EM_ARM)
749
0
      break;
750
47
    ARMAttributeParser Attributes;
751
47
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
752
47
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
753
47
    updateSupportedARMFeatures(Attributes);
754
47
    updateARMVFPArgs(Attributes, this);
755
47
756
47
    // FIXME: Retain the first attribute section we see. The eglibc ARM
757
47
    // dynamic loaders require the presence of an attribute section for dlopen
758
47
    // to work. In a full implementation we would merge all attribute sections.
759
47
    if (In.ARMAttributes == nullptr) {
760
35
      In.ARMAttributes = make<InputSection>(*this, Sec, Name);
761
35
      return In.ARMAttributes;
762
35
    }
763
12
    return &InputSection::Discarded;
764
12
  }
765
12
  case SHT_LLVM_DEPENDENT_LIBRARIES: {
766
0
    if (Config->Relocatable)
767
0
      break;
768
0
    ArrayRef<char> Data =
769
0
        CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this);
770
0
    if (!Data.empty() && Data.back() != '\0') {
771
0
      error(toString(this) +
772
0
            ": corrupted dependent libraries section (unterminated string): " +
773
0
            Name);
774
0
      return &InputSection::Discarded;
775
0
    }
776
0
    for (const char *D = Data.begin(), *E = Data.end(); D < E;) {
777
0
      StringRef S(D);
778
0
      addDependentLibrary(S, this);
779
0
      D += S.size() + 1;
780
0
    }
781
0
    return &InputSection::Discarded;
782
0
  }
783
198
  case SHT_RELA:
784
198
  case SHT_REL: {
785
198
    // Find a relocation target section and associate this section with that.
786
198
    // Target may have been discarded if it is in a different section group
787
198
    // and the group is discarded, even though it's a violation of the
788
198
    // spec. We handle that situation gracefully by discarding dangling
789
198
    // relocation sections.
790
198
    InputSectionBase *Target = getRelocTarget(Sec);
791
198
    if (!Target)
792
0
      return nullptr;
793
198
794
198
    // This section contains relocation information.
795
198
    // If -r is given, we do not interpret or apply relocation
796
198
    // but just copy relocation sections to output.
797
198
    if (Config->Relocatable) {
798
12
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
799
12
      // We want to add a dependency to target, similar like we do for
800
12
      // -emit-relocs below. This is useful for the case when linker script
801
12
      // contains the "/DISCARD/". It is perhaps uncommon to use a script with
802
12
      // -r, but we faced it in the Linux kernel and have to handle such case
803
12
      // and not to crash.
804
12
      Target->DependentSections.push_back(RelocSec);
805
12
      return RelocSec;
806
12
    }
807
186
808
186
    if (Target->FirstRelocation)
809
0
      fatal(toString(this) +
810
0
            ": multiple relocation sections to one section are not supported");
811
186
812
186
    // ELF spec allows mergeable sections with relocations, but they are
813
186
    // rare, and it is in practice hard to merge such sections by contents,
814
186
    // because applying relocations at end of linking changes section
815
186
    // contents. So, we simply handle such sections as non-mergeable ones.
816
186
    // Degrading like this is acceptable because section merging is optional.
817
186
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
818
0
      Target = toRegularSection(MS);
819
0
      this->Sections[Sec.sh_info] = Target;
820
0
    }
821
186
822
186
    if (Sec.sh_type == SHT_RELA) {
823
3
      ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
824
3
      Target->FirstRelocation = Rels.begin();
825
3
      Target->NumRelocations = Rels.size();
826
3
      Target->AreRelocsRela = true;
827
183
    } else {
828
183
      ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
829
183
      Target->FirstRelocation = Rels.begin();
830
183
      Target->NumRelocations = Rels.size();
831
183
      Target->AreRelocsRela = false;
832
183
    }
833
186
    assert(isUInt<31>(Target->NumRelocations));
834
186
835
186
    // Relocation sections processed by the linker are usually removed
836
186
    // from the output, so returning `nullptr` for the normal case.
837
186
    // However, if -emit-relocs is given, we need to leave them in the output.
838
186
    // (Some post link analysis tools need this information.)
839
186
    if (Config->EmitRelocs) {
840
6
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
841
6
      // We will not emit relocation section if target was discarded.
842
6
      Target->DependentSections.push_back(RelocSec);
843
6
      return RelocSec;
844
6
    }
845
180
    return nullptr;
846
180
  }
847
613
  }
848
613
849
613
  // The GNU linker uses .note.GNU-stack section as a marker indicating
850
613
  // that the code in the object file does not expect that the stack is
851
613
  // executable (in terms of NX bit). If all input files have the marker,
852
613
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
853
613
  // make the stack non-executable. Most object files have this section as
854
613
  // of 2017.
855
613
  //
856
613
  // But making the stack non-executable is a norm today for security
857
613
  // reasons. Failure to do so may result in a serious security issue.
858
613
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
859
613
  // explicitly told to do otherwise (by -z execstack). Because the stack
860
613
  // executable-ness is controlled solely by command line options,
861
613
  // .note.GNU-stack sections are simply ignored.
862
613
  if (Name == ".note.GNU-stack")
863
1
    return &InputSection::Discarded;
864
612
865
612
  // Split stacks is a feature to support a discontiguous stack,
866
612
  // commonly used in the programming language Go. For the details,
867
612
  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
868
612
  // for split stack will include a .note.GNU-split-stack section.
869
612
  if (Name == ".note.GNU-split-stack") {
870
0
    if (Config->Relocatable) {
871
0
      error("cannot mix split-stack and non-split-stack in a relocatable link");
872
0
      return &InputSection::Discarded;
873
0
    }
874
0
    this->SplitStack = true;
875
0
    return &InputSection::Discarded;
876
0
  }
877
612
878
612
  // An object file cmpiled for split stack, but where some of the
879
612
  // functions were compiled with the no_split_stack_attribute will
880
612
  // include a .note.GNU-no-split-stack section.
881
612
  if (Name == ".note.GNU-no-split-stack") {
882
0
    this->SomeNoSplitStack = true;
883
0
    return &InputSection::Discarded;
884
0
  }
885
612
886
612
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
887
612
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
888
612
  // sections. Drop those sections to avoid duplicate symbol errors.
889
612
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
890
612
  // fixed for a while.
891
612
  if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
892
612
      Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
893
0
    return &InputSection::Discarded;
894
612
895
612
  // If we are creating a new .build-id section, strip existing .build-id
896
612
  // sections so that the output won't have more than one .build-id.
897
612
  // This is not usually a problem because input object files normally don't
898
612
  // have .build-id sections, but you can create such files by
899
612
  // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
900
612
  if (Name == ".note.gnu.build-id" && 
Config->BuildId != BuildIdKind::None0
)
901
0
    return &InputSection::Discarded;
902
612
903
612
  // The linker merges EH (exception handling) frames and creates a
904
612
  // .eh_frame_hdr section for runtime. So we handle them with a special
905
612
  // class. For relocatable outputs, they are just passed through.
906
612
  if (Name == ".eh_frame" && 
!Config->Relocatable1
)
907
1
    return make<EhInputSection>(*this, Sec, Name);
908
611
909
611
  if (shouldMerge(Sec))
910
3
    return make<MergeInputSection>(*this, Sec, Name);
911
608
  return make<InputSection>(*this, Sec, Name);
912
608
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::createInputSection(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
Line
Count
Source
743
541
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
744
541
  StringRef Name = getSectionName(Sec);
745
541
746
541
  switch (Sec.sh_type) {
747
541
  case SHT_ARM_ATTRIBUTES: {
748
0
    if (Config->EMachine != EM_ARM)
749
0
      break;
750
0
    ARMAttributeParser Attributes;
751
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
752
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
753
0
    updateSupportedARMFeatures(Attributes);
754
0
    updateARMVFPArgs(Attributes, this);
755
0
756
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
757
0
    // dynamic loaders require the presence of an attribute section for dlopen
758
0
    // to work. In a full implementation we would merge all attribute sections.
759
0
    if (In.ARMAttributes == nullptr) {
760
0
      In.ARMAttributes = make<InputSection>(*this, Sec, Name);
761
0
      return In.ARMAttributes;
762
0
    }
763
0
    return &InputSection::Discarded;
764
0
  }
765
0
  case SHT_LLVM_DEPENDENT_LIBRARIES: {
766
0
    if (Config->Relocatable)
767
0
      break;
768
0
    ArrayRef<char> Data =
769
0
        CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this);
770
0
    if (!Data.empty() && Data.back() != '\0') {
771
0
      error(toString(this) +
772
0
            ": corrupted dependent libraries section (unterminated string): " +
773
0
            Name);
774
0
      return &InputSection::Discarded;
775
0
    }
776
0
    for (const char *D = Data.begin(), *E = Data.end(); D < E;) {
777
0
      StringRef S(D);
778
0
      addDependentLibrary(S, this);
779
0
      D += S.size() + 1;
780
0
    }
781
0
    return &InputSection::Discarded;
782
0
  }
783
58
  case SHT_RELA:
784
58
  case SHT_REL: {
785
58
    // Find a relocation target section and associate this section with that.
786
58
    // Target may have been discarded if it is in a different section group
787
58
    // and the group is discarded, even though it's a violation of the
788
58
    // spec. We handle that situation gracefully by discarding dangling
789
58
    // relocation sections.
790
58
    InputSectionBase *Target = getRelocTarget(Sec);
791
58
    if (!Target)
792
0
      return nullptr;
793
58
794
58
    // This section contains relocation information.
795
58
    // If -r is given, we do not interpret or apply relocation
796
58
    // but just copy relocation sections to output.
797
58
    if (Config->Relocatable) {
798
4
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
799
4
      // We want to add a dependency to target, similar like we do for
800
4
      // -emit-relocs below. This is useful for the case when linker script
801
4
      // contains the "/DISCARD/". It is perhaps uncommon to use a script with
802
4
      // -r, but we faced it in the Linux kernel and have to handle such case
803
4
      // and not to crash.
804
4
      Target->DependentSections.push_back(RelocSec);
805
4
      return RelocSec;
806
4
    }
807
54
808
54
    if (Target->FirstRelocation)
809
0
      fatal(toString(this) +
810
0
            ": multiple relocation sections to one section are not supported");
811
54
812
54
    // ELF spec allows mergeable sections with relocations, but they are
813
54
    // rare, and it is in practice hard to merge such sections by contents,
814
54
    // because applying relocations at end of linking changes section
815
54
    // contents. So, we simply handle such sections as non-mergeable ones.
816
54
    // Degrading like this is acceptable because section merging is optional.
817
54
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
818
0
      Target = toRegularSection(MS);
819
0
      this->Sections[Sec.sh_info] = Target;
820
0
    }
821
54
822
54
    if (Sec.sh_type == SHT_RELA) {
823
11
      ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
824
11
      Target->FirstRelocation = Rels.begin();
825
11
      Target->NumRelocations = Rels.size();
826
11
      Target->AreRelocsRela = true;
827
43
    } else {
828
43
      ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
829
43
      Target->FirstRelocation = Rels.begin();
830
43
      Target->NumRelocations = Rels.size();
831
43
      Target->AreRelocsRela = false;
832
43
    }
833
54
    assert(isUInt<31>(Target->NumRelocations));
834
54
835
54
    // Relocation sections processed by the linker are usually removed
836
54
    // from the output, so returning `nullptr` for the normal case.
837
54
    // However, if -emit-relocs is given, we need to leave them in the output.
838
54
    // (Some post link analysis tools need this information.)
839
54
    if (Config->EmitRelocs) {
840
0
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
841
0
      // We will not emit relocation section if target was discarded.
842
0
      Target->DependentSections.push_back(RelocSec);
843
0
      return RelocSec;
844
0
    }
845
54
    return nullptr;
846
54
  }
847
483
  }
848
483
849
483
  // The GNU linker uses .note.GNU-stack section as a marker indicating
850
483
  // that the code in the object file does not expect that the stack is
851
483
  // executable (in terms of NX bit). If all input files have the marker,
852
483
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
853
483
  // make the stack non-executable. Most object files have this section as
854
483
  // of 2017.
855
483
  //
856
483
  // But making the stack non-executable is a norm today for security
857
483
  // reasons. Failure to do so may result in a serious security issue.
858
483
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
859
483
  // explicitly told to do otherwise (by -z execstack). Because the stack
860
483
  // executable-ness is controlled solely by command line options,
861
483
  // .note.GNU-stack sections are simply ignored.
862
483
  if (Name == ".note.GNU-stack")
863
4
    return &InputSection::Discarded;
864
479
865
479
  // Split stacks is a feature to support a discontiguous stack,
866
479
  // commonly used in the programming language Go. For the details,
867
479
  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
868
479
  // for split stack will include a .note.GNU-split-stack section.
869
479
  if (Name == ".note.GNU-split-stack") {
870
0
    if (Config->Relocatable) {
871
0
      error("cannot mix split-stack and non-split-stack in a relocatable link");
872
0
      return &InputSection::Discarded;
873
0
    }
874
0
    this->SplitStack = true;
875
0
    return &InputSection::Discarded;
876
0
  }
877
479
878
479
  // An object file cmpiled for split stack, but where some of the
879
479
  // functions were compiled with the no_split_stack_attribute will
880
479
  // include a .note.GNU-no-split-stack section.
881
479
  if (Name == ".note.GNU-no-split-stack") {
882
0
    this->SomeNoSplitStack = true;
883
0
    return &InputSection::Discarded;
884
0
  }
885
479
886
479
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
887
479
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
888
479
  // sections. Drop those sections to avoid duplicate symbol errors.
889
479
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
890
479
  // fixed for a while.
891
479
  if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
892
479
      Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
893
0
    return &InputSection::Discarded;
894
479
895
479
  // If we are creating a new .build-id section, strip existing .build-id
896
479
  // sections so that the output won't have more than one .build-id.
897
479
  // This is not usually a problem because input object files normally don't
898
479
  // have .build-id sections, but you can create such files by
899
479
  // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
900
479
  if (Name == ".note.gnu.build-id" && 
Config->BuildId != BuildIdKind::None0
)
901
0
    return &InputSection::Discarded;
902
479
903
479
  // The linker merges EH (exception handling) frames and creates a
904
479
  // .eh_frame_hdr section for runtime. So we handle them with a special
905
479
  // class. For relocatable outputs, they are just passed through.
906
479
  if (Name == ".eh_frame" && 
!Config->Relocatable0
)
907
0
    return make<EhInputSection>(*this, Sec, Name);
908
479
909
479
  if (shouldMerge(Sec))
910
0
    return make<MergeInputSection>(*this, Sec, Name);
911
479
  return make<InputSection>(*this, Sec, Name);
912
479
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::createInputSection(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
743
332k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
744
332k
  StringRef Name = getSectionName(Sec);
745
332k
746
332k
  switch (Sec.sh_type) {
747
332k
  case SHT_ARM_ATTRIBUTES: {
748
0
    if (Config->EMachine != EM_ARM)
749
0
      break;
750
0
    ARMAttributeParser Attributes;
751
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
752
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
753
0
    updateSupportedARMFeatures(Attributes);
754
0
    updateARMVFPArgs(Attributes, this);
755
0
756
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
757
0
    // dynamic loaders require the presence of an attribute section for dlopen
758
0
    // to work. In a full implementation we would merge all attribute sections.
759
0
    if (In.ARMAttributes == nullptr) {
760
0
      In.ARMAttributes = make<InputSection>(*this, Sec, Name);
761
0
      return In.ARMAttributes;
762
0
    }
763
0
    return &InputSection::Discarded;
764
0
  }
765
7
  case SHT_LLVM_DEPENDENT_LIBRARIES: {
766
7
    if (Config->Relocatable)
767
2
      break;
768
5
    ArrayRef<char> Data =
769
5
        CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this);
770
5
    if (!Data.empty() && Data.back() != '\0') {
771
1
      error(toString(this) +
772
1
            ": corrupted dependent libraries section (unterminated string): " +
773
1
            Name);
774
1
      return &InputSection::Discarded;
775
1
    }
776
12
    
for (const char *D = Data.begin(), *E = Data.end(); 4
D < E;) {
777
8
      StringRef S(D);
778
8
      addDependentLibrary(S, this);
779
8
      D += S.size() + 1;
780
8
    }
781
4
    return &InputSection::Discarded;
782
4
  }
783
705
  case SHT_RELA:
784
705
  case SHT_REL: {
785
705
    // Find a relocation target section and associate this section with that.
786
705
    // Target may have been discarded if it is in a different section group
787
705
    // and the group is discarded, even though it's a violation of the
788
705
    // spec. We handle that situation gracefully by discarding dangling
789
705
    // relocation sections.
790
705
    InputSectionBase *Target = getRelocTarget(Sec);
791
705
    if (!Target)
792
4
      return nullptr;
793
701
794
701
    // This section contains relocation information.
795
701
    // If -r is given, we do not interpret or apply relocation
796
701
    // but just copy relocation sections to output.
797
701
    if (Config->Relocatable) {
798
35
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
799
35
      // We want to add a dependency to target, similar like we do for
800
35
      // -emit-relocs below. This is useful for the case when linker script
801
35
      // contains the "/DISCARD/". It is perhaps uncommon to use a script with
802
35
      // -r, but we faced it in the Linux kernel and have to handle such case
803
35
      // and not to crash.
804
35
      Target->DependentSections.push_back(RelocSec);
805
35
      return RelocSec;
806
35
    }
807
666
808
666
    if (Target->FirstRelocation)
809
0
      fatal(toString(this) +
810
0
            ": multiple relocation sections to one section are not supported");
811
666
812
666
    // ELF spec allows mergeable sections with relocations, but they are
813
666
    // rare, and it is in practice hard to merge such sections by contents,
814
666
    // because applying relocations at end of linking changes section
815
666
    // contents. So, we simply handle such sections as non-mergeable ones.
816
666
    // Degrading like this is acceptable because section merging is optional.
817
666
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
818
3
      Target = toRegularSection(MS);
819
3
      this->Sections[Sec.sh_info] = Target;
820
3
    }
821
666
822
666
    if (Sec.sh_type == SHT_RELA) {
823
666
      ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
824
666
      Target->FirstRelocation = Rels.begin();
825
666
      Target->NumRelocations = Rels.size();
826
666
      Target->AreRelocsRela = true;
827
666
    } else {
828
0
      ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
829
0
      Target->FirstRelocation = Rels.begin();
830
0
      Target->NumRelocations = Rels.size();
831
0
      Target->AreRelocsRela = false;
832
0
    }
833
666
    assert(isUInt<31>(Target->NumRelocations));
834
666
835
666
    // Relocation sections processed by the linker are usually removed
836
666
    // from the output, so returning `nullptr` for the normal case.
837
666
    // However, if -emit-relocs is given, we need to leave them in the output.
838
666
    // (Some post link analysis tools need this information.)
839
666
    if (Config->EmitRelocs) {
840
14
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
841
14
      // We will not emit relocation section if target was discarded.
842
14
      Target->DependentSections.push_back(RelocSec);
843
14
      return RelocSec;
844
14
    }
845
652
    return nullptr;
846
652
  }
847
331k
  }
848
331k
849
331k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
850
331k
  // that the code in the object file does not expect that the stack is
851
331k
  // executable (in terms of NX bit). If all input files have the marker,
852
331k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
853
331k
  // make the stack non-executable. Most object files have this section as
854
331k
  // of 2017.
855
331k
  //
856
331k
  // But making the stack non-executable is a norm today for security
857
331k
  // reasons. Failure to do so may result in a serious security issue.
858
331k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
859
331k
  // explicitly told to do otherwise (by -z execstack). Because the stack
860
331k
  // executable-ness is controlled solely by command line options,
861
331k
  // .note.GNU-stack sections are simply ignored.
862
331k
  if (Name == ".note.GNU-stack")
863
116
    return &InputSection::Discarded;
864
331k
865
331k
  // Split stacks is a feature to support a discontiguous stack,
866
331k
  // commonly used in the programming language Go. For the details,
867
331k
  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
868
331k
  // for split stack will include a .note.GNU-split-stack section.
869
331k
  if (Name == ".note.GNU-split-stack") {
870
8
    if (Config->Relocatable) {
871
2
      error("cannot mix split-stack and non-split-stack in a relocatable link");
872
2
      return &InputSection::Discarded;
873
2
    }
874
6
    this->SplitStack = true;
875
6
    return &InputSection::Discarded;
876
6
  }
877
331k
878
331k
  // An object file cmpiled for split stack, but where some of the
879
331k
  // functions were compiled with the no_split_stack_attribute will
880
331k
  // include a .note.GNU-no-split-stack section.
881
331k
  if (Name == ".note.GNU-no-split-stack") {
882
0
    this->SomeNoSplitStack = true;
883
0
    return &InputSection::Discarded;
884
0
  }
885
331k
886
331k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
887
331k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
888
331k
  // sections. Drop those sections to avoid duplicate symbol errors.
889
331k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
890
331k
  // fixed for a while.
891
331k
  if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
892
331k
      Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
893
0
    return &InputSection::Discarded;
894
331k
895
331k
  // If we are creating a new .build-id section, strip existing .build-id
896
331k
  // sections so that the output won't have more than one .build-id.
897
331k
  // This is not usually a problem because input object files normally don't
898
331k
  // have .build-id sections, but you can create such files by
899
331k
  // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
900
331k
  if (Name == ".note.gnu.build-id" && 
Config->BuildId != BuildIdKind::None1
)
901
1
    return &InputSection::Discarded;
902
331k
903
331k
  // The linker merges EH (exception handling) frames and creates a
904
331k
  // .eh_frame_hdr section for runtime. So we handle them with a special
905
331k
  // class. For relocatable outputs, they are just passed through.
906
331k
  if (Name == ".eh_frame" && 
!Config->Relocatable62
)
907
57
    return make<EhInputSection>(*this, Sec, Name);
908
331k
909
331k
  if (shouldMerge(Sec))
910
56
    return make<MergeInputSection>(*this, Sec, Name);
911
331k
  return make<InputSection>(*this, Sec, Name);
912
331k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::createInputSection(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
Line
Count
Source
743
310
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
744
310
  StringRef Name = getSectionName(Sec);
745
310
746
310
  switch (Sec.sh_type) {
747
310
  case SHT_ARM_ATTRIBUTES: {
748
0
    if (Config->EMachine != EM_ARM)
749
0
      break;
750
0
    ARMAttributeParser Attributes;
751
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
752
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
753
0
    updateSupportedARMFeatures(Attributes);
754
0
    updateARMVFPArgs(Attributes, this);
755
0
756
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
757
0
    // dynamic loaders require the presence of an attribute section for dlopen
758
0
    // to work. In a full implementation we would merge all attribute sections.
759
0
    if (In.ARMAttributes == nullptr) {
760
0
      In.ARMAttributes = make<InputSection>(*this, Sec, Name);
761
0
      return In.ARMAttributes;
762
0
    }
763
0
    return &InputSection::Discarded;
764
0
  }
765
0
  case SHT_LLVM_DEPENDENT_LIBRARIES: {
766
0
    if (Config->Relocatable)
767
0
      break;
768
0
    ArrayRef<char> Data =
769
0
        CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this);
770
0
    if (!Data.empty() && Data.back() != '\0') {
771
0
      error(toString(this) +
772
0
            ": corrupted dependent libraries section (unterminated string): " +
773
0
            Name);
774
0
      return &InputSection::Discarded;
775
0
    }
776
0
    for (const char *D = Data.begin(), *E = Data.end(); D < E;) {
777
0
      StringRef S(D);
778
0
      addDependentLibrary(S, this);
779
0
      D += S.size() + 1;
780
0
    }
781
0
    return &InputSection::Discarded;
782
0
  }
783
70
  case SHT_RELA:
784
70
  case SHT_REL: {
785
70
    // Find a relocation target section and associate this section with that.
786
70
    // Target may have been discarded if it is in a different section group
787
70
    // and the group is discarded, even though it's a violation of the
788
70
    // spec. We handle that situation gracefully by discarding dangling
789
70
    // relocation sections.
790
70
    InputSectionBase *Target = getRelocTarget(Sec);
791
70
    if (!Target)
792
0
      return nullptr;
793
70
794
70
    // This section contains relocation information.
795
70
    // If -r is given, we do not interpret or apply relocation
796
70
    // but just copy relocation sections to output.
797
70
    if (Config->Relocatable) {
798
0
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
799
0
      // We want to add a dependency to target, similar like we do for
800
0
      // -emit-relocs below. This is useful for the case when linker script
801
0
      // contains the "/DISCARD/". It is perhaps uncommon to use a script with
802
0
      // -r, but we faced it in the Linux kernel and have to handle such case
803
0
      // and not to crash.
804
0
      Target->DependentSections.push_back(RelocSec);
805
0
      return RelocSec;
806
0
    }
807
70
808
70
    if (Target->FirstRelocation)
809
0
      fatal(toString(this) +
810
0
            ": multiple relocation sections to one section are not supported");
811
70
812
70
    // ELF spec allows mergeable sections with relocations, but they are
813
70
    // rare, and it is in practice hard to merge such sections by contents,
814
70
    // because applying relocations at end of linking changes section
815
70
    // contents. So, we simply handle such sections as non-mergeable ones.
816
70
    // Degrading like this is acceptable because section merging is optional.
817
70
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
818
0
      Target = toRegularSection(MS);
819
0
      this->Sections[Sec.sh_info] = Target;
820
0
    }
821
70
822
70
    if (Sec.sh_type == SHT_RELA) {
823
70
      ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
824
70
      Target->FirstRelocation = Rels.begin();
825
70
      Target->NumRelocations = Rels.size();
826
70
      Target->AreRelocsRela = true;
827
70
    } else {
828
0
      ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
829
0
      Target->FirstRelocation = Rels.begin();
830
0
      Target->NumRelocations = Rels.size();
831
0
      Target->AreRelocsRela = false;
832
0
    }
833
70
    assert(isUInt<31>(Target->NumRelocations));
834
70
835
70
    // Relocation sections processed by the linker are usually removed
836
70
    // from the output, so returning `nullptr` for the normal case.
837
70
    // However, if -emit-relocs is given, we need to leave them in the output.
838
70
    // (Some post link analysis tools need this information.)
839
70
    if (Config->EmitRelocs) {
840
0
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
841
0
      // We will not emit relocation section if target was discarded.
842
0
      Target->DependentSections.push_back(RelocSec);
843
0
      return RelocSec;
844
0
    }
845
70
    return nullptr;
846
70
  }
847
240
  }
848
240
849
240
  // The GNU linker uses .note.GNU-stack section as a marker indicating
850
240
  // that the code in the object file does not expect that the stack is
851
240
  // executable (in terms of NX bit). If all input files have the marker,
852
240
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
853
240
  // make the stack non-executable. Most object files have this section as
854
240
  // of 2017.
855
240
  //
856
240
  // But making the stack non-executable is a norm today for security
857
240
  // reasons. Failure to do so may result in a serious security issue.
858
240
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
859
240
  // explicitly told to do otherwise (by -z execstack). Because the stack
860
240
  // executable-ness is controlled solely by command line options,
861
240
  // .note.GNU-stack sections are simply ignored.
862
240
  if (Name == ".note.GNU-stack")
863
0
    return &InputSection::Discarded;
864
240
865
240
  // Split stacks is a feature to support a discontiguous stack,
866
240
  // commonly used in the programming language Go. For the details,
867
240
  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
868
240
  // for split stack will include a .note.GNU-split-stack section.
869
240
  if (Name == ".note.GNU-split-stack") {
870
3
    if (Config->Relocatable) {
871
0
      error("cannot mix split-stack and non-split-stack in a relocatable link");
872
0
      return &InputSection::Discarded;
873
0
    }
874
3
    this->SplitStack = true;
875
3
    return &InputSection::Discarded;
876
3
  }
877
237
878
237
  // An object file cmpiled for split stack, but where some of the
879
237
  // functions were compiled with the no_split_stack_attribute will
880
237
  // include a .note.GNU-no-split-stack section.
881
237
  if (Name == ".note.GNU-no-split-stack") {
882
0
    this->SomeNoSplitStack = true;
883
0
    return &InputSection::Discarded;
884
0
  }
885
237
886
237
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
887
237
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
888
237
  // sections. Drop those sections to avoid duplicate symbol errors.
889
237
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
890
237
  // fixed for a while.
891
237
  if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
892
237
      Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
893
0
    return &InputSection::Discarded;
894
237
895
237
  // If we are creating a new .build-id section, strip existing .build-id
896
237
  // sections so that the output won't have more than one .build-id.
897
237
  // This is not usually a problem because input object files normally don't
898
237
  // have .build-id sections, but you can create such files by
899
237
  // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
900
237
  if (Name == ".note.gnu.build-id" && 
Config->BuildId != BuildIdKind::None0
)
901
0
    return &InputSection::Discarded;
902
237
903
237
  // The linker merges EH (exception handling) frames and creates a
904
237
  // .eh_frame_hdr section for runtime. So we handle them with a special
905
237
  // class. For relocatable outputs, they are just passed through.
906
237
  if (Name == ".eh_frame" && 
!Config->Relocatable2
)
907
2
    return make<EhInputSection>(*this, Sec, Name);
908
235
909
235
  if (shouldMerge(Sec))
910
0
    return make<MergeInputSection>(*this, Sec, Name);
911
235
  return make<InputSection>(*this, Sec, Name);
912
235
}
913
914
template <class ELFT>
915
334k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
916
334k
  return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
917
334k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSectionName(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Line
Count
Source
915
858
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
916
858
  return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
917
858
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSectionName(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
Line
Count
Source
915
541
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
916
541
  return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
917
541
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSectionName(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
915
332k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
916
332k
  return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
917
332k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSectionName(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
Line
Count
Source
915
310
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
916
310
  return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
917
310
}
918
919
1.63k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
920
1.63k
  this->Symbols.reserve(this->getELFSyms<ELFT>().size());
921
1.63k
  for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
922
81.9k
    this->Symbols.push_back(createSymbol(&Sym));
923
1.63k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSymbols()
Line
Count
Source
919
195
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
920
195
  this->Symbols.reserve(this->getELFSyms<ELFT>().size());
921
195
  for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
922
1.19k
    this->Symbols.push_back(createSymbol(&Sym));
923
195
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSymbols()
Line
Count
Source
919
96
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
920
96
  this->Symbols.reserve(this->getELFSyms<ELFT>().size());
921
96
  for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
922
376
    this->Symbols.push_back(createSymbol(&Sym));
923
96
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSymbols()
Line
Count
Source
919
1.26k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
920
1.26k
  this->Symbols.reserve(this->getELFSyms<ELFT>().size());
921
1.26k
  for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
922
70.0k
    this->Symbols.push_back(createSymbol(&Sym));
923
1.26k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSymbols()
Line
Count
Source
919
79
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
920
79
  this->Symbols.reserve(this->getELFSyms<ELFT>().size());
921
79
  for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
922
10.3k
    this->Symbols.push_back(createSymbol(&Sym));
923
79
}
924
925
81.9k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
926
81.9k
  uint32_t SecIdx = getSectionIndex(*Sym);
927
81.9k
  if (SecIdx >= this->Sections.size())
928
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
929
81.9k
930
81.9k
  InputSectionBase *Sec = this->Sections[SecIdx];
931
81.9k
  uint8_t Binding = Sym->getBinding();
932
81.9k
  uint8_t StOther = Sym->st_other;
933
81.9k
  uint8_t Type = Sym->getType();
934
81.9k
  uint64_t Value = Sym->st_value;
935
81.9k
  uint64_t Size = Sym->st_size;
936
81.9k
937
81.9k
  if (Binding == STB_LOCAL) {
938
68.6k
    if (Sym->getType() == STT_FILE)
939
120
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
940
68.6k
941
68.6k
    if (this->StringTable.size() <= Sym->st_name)
942
0
      fatal(toString(this) + ": invalid symbol name offset");
943
68.6k
944
68.6k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
945
68.6k
    if (Sym->st_shndx == SHN_UNDEF)
946
1.63k
      return make<Undefined>(this, Name, Binding, StOther, Type);
947
66.9k
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
948
66.9k
  }
949
13.3k
950
13.3k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
951
13.3k
952
13.3k
  switch (Sym->st_shndx) {
953
13.3k
  case SHN_UNDEF:
954
865
    return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
955
13.3k
  case SHN_COMMON:
956
56
    if (Value == 0 || Value >= UINT32_MAX)
957
56
      fatal(toString(this) + ": common symbol '" + Name +
958
0
            "' has invalid alignment: " + Twine(Value));
959
56
    return Symtab->addSymbol(
960
56
        CommonSymbol{this, Name, Binding, StOther, Type, Value, Size});
961
12.3k
  }
962
12.3k
963
12.3k
  switch (Binding) {
964
12.3k
  default:
965
0
    fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding));
966
12.3k
  case STB_GLOBAL:
967
12.3k
  case STB_WEAK:
968
12.3k
  case STB_GNU_UNIQUE:
969
12.3k
    if (Sec == &InputSection::Discarded)
970
5
      return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
971
12.3k
    return Symtab->addSymbol(
972
12.3k
        Defined{this, Name, Binding, StOther, Type, Value, Size, Sec});
973
12.3k
  }
974
12.3k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::createSymbol(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*)
Line
Count
Source
925
1.19k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
926
1.19k
  uint32_t SecIdx = getSectionIndex(*Sym);
927
1.19k
  if (SecIdx >= this->Sections.size())
928
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
929
1.19k
930
1.19k
  InputSectionBase *Sec = this->Sections[SecIdx];
931
1.19k
  uint8_t Binding = Sym->getBinding();
932
1.19k
  uint8_t StOther = Sym->st_other;
933
1.19k
  uint8_t Type = Sym->getType();
934
1.19k
  uint64_t Value = Sym->st_value;
935
1.19k
  uint64_t Size = Sym->st_size;
936
1.19k
937
1.19k
  if (Binding == STB_LOCAL) {
938
619
    if (Sym->getType() == STT_FILE)
939
2
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
940
619
941
619
    if (this->StringTable.size() <= Sym->st_name)
942
0
      fatal(toString(this) + ": invalid symbol name offset");
943
619
944
619
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
945
619
    if (Sym->st_shndx == SHN_UNDEF)
946
195
      return make<Undefined>(this, Name, Binding, StOther, Type);
947
424
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
948
424
  }
949
573
950
573
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
951
573
952
573
  switch (Sym->st_shndx) {
953
573
  case SHN_UNDEF:
954
107
    return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
955
573
  case SHN_COMMON:
956
2
    if (Value == 0 || Value >= UINT32_MAX)
957
2
      fatal(toString(this) + ": common symbol '" + Name +
958
0
            "' has invalid alignment: " + Twine(Value));
959
2
    return Symtab->addSymbol(
960
2
        CommonSymbol{this, Name, Binding, StOther, Type, Value, Size});
961
464
  }
962
464
963
464
  switch (Binding) {
964
464
  default:
965
0
    fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding));
966
464
  case STB_GLOBAL:
967
464
  case STB_WEAK:
968
464
  case STB_GNU_UNIQUE:
969
464
    if (Sec == &InputSection::Discarded)
970
0
      return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
971
464
    return Symtab->addSymbol(
972
464
        Defined{this, Name, Binding, StOther, Type, Value, Size, Sec});
973
464
  }
974
464
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::createSymbol(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*)
Line
Count
Source
925
376
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
926
376
  uint32_t SecIdx = getSectionIndex(*Sym);
927
376
  if (SecIdx >= this->Sections.size())
928
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
929
376
930
376
  InputSectionBase *Sec = this->Sections[SecIdx];
931
376
  uint8_t Binding = Sym->getBinding();
932
376
  uint8_t StOther = Sym->st_other;
933
376
  uint8_t Type = Sym->getType();
934
376
  uint64_t Value = Sym->st_value;
935
376
  uint64_t Size = Sym->st_size;
936
376
937
376
  if (Binding == STB_LOCAL) {
938
155
    if (Sym->getType() == STT_FILE)
939
0
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
940
155
941
155
    if (this->StringTable.size() <= Sym->st_name)
942
0
      fatal(toString(this) + ": invalid symbol name offset");
943
155
944
155
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
945
155
    if (Sym->st_shndx == SHN_UNDEF)
946
96
      return make<Undefined>(this, Name, Binding, StOther, Type);
947
59
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
948
59
  }
949
221
950
221
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
951
221
952
221
  switch (Sym->st_shndx) {
953
221
  case SHN_UNDEF:
954
58
    return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
955
221
  case SHN_COMMON:
956
0
    if (Value == 0 || Value >= UINT32_MAX)
957
0
      fatal(toString(this) + ": common symbol '" + Name +
958
0
            "' has invalid alignment: " + Twine(Value));
959
0
    return Symtab->addSymbol(
960
0
        CommonSymbol{this, Name, Binding, StOther, Type, Value, Size});
961
163
  }
962
163
963
163
  switch (Binding) {
964
163
  default:
965
0
    fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding));
966
163
  case STB_GLOBAL:
967
163
  case STB_WEAK:
968
163
  case STB_GNU_UNIQUE:
969
163
    if (Sec == &InputSection::Discarded)
970
0
      return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
971
163
    return Symtab->addSymbol(
972
163
        Defined{this, Name, Binding, StOther, Type, Value, Size, Sec});
973
163
  }
974
163
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::createSymbol(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*)
Line
Count
Source
925
70.0k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
926
70.0k
  uint32_t SecIdx = getSectionIndex(*Sym);
927
70.0k
  if (SecIdx >= this->Sections.size())
928
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
929
70.0k
930
70.0k
  InputSectionBase *Sec = this->Sections[SecIdx];
931
70.0k
  uint8_t Binding = Sym->getBinding();
932
70.0k
  uint8_t StOther = Sym->st_other;
933
70.0k
  uint8_t Type = Sym->getType();
934
70.0k
  uint64_t Value = Sym->st_value;
935
70.0k
  uint64_t Size = Sym->st_size;
936
70.0k
937
70.0k
  if (Binding == STB_LOCAL) {
938
67.7k
    if (Sym->getType() == STT_FILE)
939
117
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
940
67.7k
941
67.7k
    if (this->StringTable.size() <= Sym->st_name)
942
0
      fatal(toString(this) + ": invalid symbol name offset");
943
67.7k
944
67.7k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
945
67.7k
    if (Sym->st_shndx == SHN_UNDEF)
946
1.26k
      return make<Undefined>(this, Name, Binding, StOther, Type);
947
66.4k
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
948
66.4k
  }
949
2.30k
950
2.30k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
951
2.30k
952
2.30k
  switch (Sym->st_shndx) {
953
2.30k
  case SHN_UNDEF:
954
626
    return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
955
2.30k
  case SHN_COMMON:
956
50
    if (Value == 0 || Value >= UINT32_MAX)
957
50
      fatal(toString(this) + ": common symbol '" + Name +
958
0
            "' has invalid alignment: " + Twine(Value));
959
50
    return Symtab->addSymbol(
960
50
        CommonSymbol{this, Name, Binding, StOther, Type, Value, Size});
961
1.63k
  }
962
1.63k
963
1.63k
  switch (Binding) {
964
1.63k
  default:
965
0
    fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding));
966
1.63k
  case STB_GLOBAL:
967
1.63k
  case STB_WEAK:
968
1.63k
  case STB_GNU_UNIQUE:
969
1.63k
    if (Sec == &InputSection::Discarded)
970
5
      return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
971
1.62k
    return Symtab->addSymbol(
972
1.62k
        Defined{this, Name, Binding, StOther, Type, Value, Size, Sec});
973
1.63k
  }
974
1.63k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::createSymbol(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*)
Line
Count
Source
925
10.3k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
926
10.3k
  uint32_t SecIdx = getSectionIndex(*Sym);
927
10.3k
  if (SecIdx >= this->Sections.size())
928
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
929
10.3k
930
10.3k
  InputSectionBase *Sec = this->Sections[SecIdx];
931
10.3k
  uint8_t Binding = Sym->getBinding();
932
10.3k
  uint8_t StOther = Sym->st_other;
933
10.3k
  uint8_t Type = Sym->getType();
934
10.3k
  uint64_t Value = Sym->st_value;
935
10.3k
  uint64_t Size = Sym->st_size;
936
10.3k
937
10.3k
  if (Binding == STB_LOCAL) {
938
125
    if (Sym->getType() == STT_FILE)
939
1
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
940
125
941
125
    if (this->StringTable.size() <= Sym->st_name)
942
0
      fatal(toString(this) + ": invalid symbol name offset");
943
125
944
125
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
945
125
    if (Sym->st_shndx == SHN_UNDEF)
946
79
      return make<Undefined>(this, Name, Binding, StOther, Type);
947
46
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
948
46
  }
949
10.2k
950
10.2k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
951
10.2k
952
10.2k
  switch (Sym->st_shndx) {
953
10.2k
  case SHN_UNDEF:
954
74
    return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
955
10.2k
  case SHN_COMMON:
956
4
    if (Value == 0 || Value >= UINT32_MAX)
957
4
      fatal(toString(this) + ": common symbol '" + Name +
958
0
            "' has invalid alignment: " + Twine(Value));
959
4
    return Symtab->addSymbol(
960
4
        CommonSymbol{this, Name, Binding, StOther, Type, Value, Size});
961
10.1k
  }
962
10.1k
963
10.1k
  switch (Binding) {
964
10.1k
  default:
965
0
    fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding));
966
10.1k
  case STB_GLOBAL:
967
10.1k
  case STB_WEAK:
968
10.1k
  case STB_GNU_UNIQUE:
969
10.1k
    if (Sec == &InputSection::Discarded)
970
0
      return Symtab->addSymbol(Undefined{this, Name, Binding, StOther, Type});
971
10.1k
    return Symtab->addSymbol(
972
10.1k
        Defined{this, Name, Binding, StOther, Type, Value, Size, Sec});
973
10.1k
  }
974
10.1k
}
975
976
ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
977
    : InputFile(ArchiveKind, File->getMemoryBufferRef()),
978
65
      File(std::move(File)) {}
979
980
64
void ArchiveFile::parse() {
981
64
  for (const Archive::Symbol &Sym : File->symbols())
982
119
    Symtab->addSymbol(LazyArchive{*this, Sym});
983
64
}
984
985
// Returns a buffer pointing to a member file containing a given symbol.
986
53
InputFile *ArchiveFile::fetch(const Archive::Symbol &Sym) {
987
53
  Archive::Child C =
988
53
      CHECK(Sym.getMember(), toString(this) +
989
53
                                 ": could not get the member for symbol " +
990
53
                                 Sym.getName());
991
53
992
53
  if (!Seen.insert(C.getChildOffset()).second)
993
0
    return nullptr;
994
53
995
53
  MemoryBufferRef MB =
996
53
      CHECK(C.getMemoryBufferRef(),
997
53
            toString(this) +
998
53
                ": could not get the buffer for the member defining symbol " +
999
53
                Sym.getName());
1000
53
1001
53
  if (Tar && 
C.getParent()->isThin()1
)
1002
1
    Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), MB.getBuffer());
1003
53
1004
53
  InputFile *File = createObjectFile(
1005
53
      MB, getName(), C.getParent()->isThin() ? 
03
:
C.getChildOffset()50
);
1006
53
  File->GroupId = GroupId;
1007
53
  return File;
1008
53
}
1009
1010
unsigned SharedFile::VernauxNum;
1011
1012
SharedFile::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
1013
    : ELFFileBase(SharedKind, M), SoName(DefaultSoName),
1014
189
      IsNeeded(!Config->AsNeeded) {}
1015
1016
// Parse the version definitions in the object file if present, and return a
1017
// vector whose nth element contains a pointer to the Elf_Verdef for version
1018
// identifier n. Version identifiers that are not definitions map to nullptr.
1019
template <typename ELFT>
1020
static std::vector<const void *> parseVerdefs(const uint8_t *Base,
1021
186
                                              const typename ELFT::Shdr *Sec) {
1022
186
  if (!Sec)
1023
176
    return {};
1024
10
1025
10
  // We cannot determine the largest verdef identifier without inspecting
1026
10
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
1027
10
  // sequentially starting from 1, so we predict that the largest identifier
1028
10
  // will be VerdefCount.
1029
10
  unsigned VerdefCount = Sec->sh_info;
1030
10
  std::vector<const void *> Verdefs(VerdefCount + 1);
1031
10
1032
10
  // Build the Verdefs array by following the chain of Elf_Verdef objects
1033
10
  // from the start of the .gnu.version_d section.
1034
10
  const uint8_t *Verdef = Base + Sec->sh_offset;
1035
40
  for (unsigned I = 0; I != VerdefCount; 
++I30
) {
1036
30
    auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
1037
30
    Verdef += CurVerdef->vd_next;
1038
30
    unsigned VerdefIndex = CurVerdef->vd_ndx;
1039
30
    Verdefs.resize(VerdefIndex + 1);
1040
30
    Verdefs[VerdefIndex] = CurVerdef;
1041
30
  }
1042
10
  return Verdefs;
1043
10
}
InputFiles.cpp:std::__1::vector<void const*, std::__1::allocator<void const*> > parseVerdefs<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char const*, llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr const*)
Line
Count
Source
1021
26
                                              const typename ELFT::Shdr *Sec) {
1022
26
  if (!Sec)
1023
26
    return {};
1024
0
1025
0
  // We cannot determine the largest verdef identifier without inspecting
1026
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
1027
0
  // sequentially starting from 1, so we predict that the largest identifier
1028
0
  // will be VerdefCount.
1029
0
  unsigned VerdefCount = Sec->sh_info;
1030
0
  std::vector<const void *> Verdefs(VerdefCount + 1);
1031
0
1032
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
1033
0
  // from the start of the .gnu.version_d section.
1034
0
  const uint8_t *Verdef = Base + Sec->sh_offset;
1035
0
  for (unsigned I = 0; I != VerdefCount; ++I) {
1036
0
    auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
1037
0
    Verdef += CurVerdef->vd_next;
1038
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
1039
0
    Verdefs.resize(VerdefIndex + 1);
1040
0
    Verdefs[VerdefIndex] = CurVerdef;
1041
0
  }
1042
0
  return Verdefs;
1043
0
}
InputFiles.cpp:std::__1::vector<void const*, std::__1::allocator<void const*> > parseVerdefs<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char const*, llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr const*)
Line
Count
Source
1021
13
                                              const typename ELFT::Shdr *Sec) {
1022
13
  if (!Sec)
1023
13
    return {};
1024
0
1025
0
  // We cannot determine the largest verdef identifier without inspecting
1026
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
1027
0
  // sequentially starting from 1, so we predict that the largest identifier
1028
0
  // will be VerdefCount.
1029
0
  unsigned VerdefCount = Sec->sh_info;
1030
0
  std::vector<const void *> Verdefs(VerdefCount + 1);
1031
0
1032
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
1033
0
  // from the start of the .gnu.version_d section.
1034
0
  const uint8_t *Verdef = Base + Sec->sh_offset;
1035
0
  for (unsigned I = 0; I != VerdefCount; ++I) {
1036
0
    auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
1037
0
    Verdef += CurVerdef->vd_next;
1038
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
1039
0
    Verdefs.resize(VerdefIndex + 1);
1040
0
    Verdefs[VerdefIndex] = CurVerdef;
1041
0
  }
1042
0
  return Verdefs;
1043
0
}
InputFiles.cpp:std::__1::vector<void const*, std::__1::allocator<void const*> > parseVerdefs<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char const*, llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr const*)
Line
Count
Source
1021
135
                                              const typename ELFT::Shdr *Sec) {
1022
135
  if (!Sec)
1023
125
    return {};
1024
10
1025
10
  // We cannot determine the largest verdef identifier without inspecting
1026
10
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
1027
10
  // sequentially starting from 1, so we predict that the largest identifier
1028
10
  // will be VerdefCount.
1029
10
  unsigned VerdefCount = Sec->sh_info;
1030
10
  std::vector<const void *> Verdefs(VerdefCount + 1);
1031
10
1032
10
  // Build the Verdefs array by following the chain of Elf_Verdef objects
1033
10
  // from the start of the .gnu.version_d section.
1034
10
  const uint8_t *Verdef = Base + Sec->sh_offset;
1035
40
  for (unsigned I = 0; I != VerdefCount; 
++I30
) {
1036
30
    auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
1037
30
    Verdef += CurVerdef->vd_next;
1038
30
    unsigned VerdefIndex = CurVerdef->vd_ndx;
1039
30
    Verdefs.resize(VerdefIndex + 1);
1040
30
    Verdefs[VerdefIndex] = CurVerdef;
1041
30
  }
1042
10
  return Verdefs;
1043
10
}
InputFiles.cpp:std::__1::vector<void const*, std::__1::allocator<void const*> > parseVerdefs<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char const*, llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr const*)
Line
Count
Source
1021
12
                                              const typename ELFT::Shdr *Sec) {
1022
12
  if (!Sec)
1023
12
    return {};
1024
0
1025
0
  // We cannot determine the largest verdef identifier without inspecting
1026
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
1027
0
  // sequentially starting from 1, so we predict that the largest identifier
1028
0
  // will be VerdefCount.
1029
0
  unsigned VerdefCount = Sec->sh_info;
1030
0
  std::vector<const void *> Verdefs(VerdefCount + 1);
1031
0
1032
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
1033
0
  // from the start of the .gnu.version_d section.
1034
0
  const uint8_t *Verdef = Base + Sec->sh_offset;
1035
0
  for (unsigned I = 0; I != VerdefCount; ++I) {
1036
0
    auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
1037
0
    Verdef += CurVerdef->vd_next;
1038
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
1039
0
    Verdefs.resize(VerdefIndex + 1);
1040
0
    Verdefs[VerdefIndex] = CurVerdef;
1041
0
  }
1042
0
  return Verdefs;
1043
0
}
1044
1045
// We do not usually care about alignments of data in shared object
1046
// files because the loader takes care of it. However, if we promote a
1047
// DSO symbol to point to .bss due to copy relocation, we need to keep
1048
// the original alignment requirements. We infer it in this function.
1049
template <typename ELFT>
1050
static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> Sections,
1051
424
                             const typename ELFT::Sym &Sym) {
1052
424
  uint64_t Ret = UINT64_MAX;
1053
424
  if (Sym.st_value)
1054
412
    Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
1055
424
  if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1056
413
    Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1057
424
  return (Ret > UINT32_MAX) ? 
01
:
Ret423
;
1058
424
}
InputFiles.cpp:unsigned long long getAlignment<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, false>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)1, false>::Sym const&)
Line
Count
Source
1051
55
                             const typename ELFT::Sym &Sym) {
1052
55
  uint64_t Ret = UINT64_MAX;
1053
55
  if (Sym.st_value)
1054
52
    Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
1055
55
  if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1056
55
    Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1057
55
  return (Ret > UINT32_MAX) ? 
00
: Ret;
1058
55
}
InputFiles.cpp:unsigned long long getAlignment<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, false>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)0, false>::Sym const&)
Line
Count
Source
1051
47
                             const typename ELFT::Sym &Sym) {
1052
47
  uint64_t Ret = UINT64_MAX;
1053
47
  if (Sym.st_value)
1054
46
    Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
1055
47
  if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1056
46
    Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1057
47
  return (Ret > UINT32_MAX) ? 
00
: Ret;
1058
47
}
InputFiles.cpp:unsigned long long getAlignment<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)1, true>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)1, true>::Sym const&)
Line
Count
Source
1051
294
                             const typename ELFT::Sym &Sym) {
1052
294
  uint64_t Ret = UINT64_MAX;
1053
294
  if (Sym.st_value)
1054
289
    Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
1055
294
  if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1056
284
    Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1057
294
  return (Ret > UINT32_MAX) ? 
01
:
Ret293
;
1058
294
}
InputFiles.cpp:unsigned long long getAlignment<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::ArrayRef<llvm::object::ELFType<(llvm::support::endianness)0, true>::Shdr>, llvm::object::ELFType<(llvm::support::endianness)0, true>::Sym const&)
Line
Count
Source
1051
28
                             const typename ELFT::Sym &Sym) {
1052
28
  uint64_t Ret = UINT64_MAX;
1053
28
  if (Sym.st_value)
1054
25
    Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
1055
28
  if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1056
28
    Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1057
28
  return (Ret > UINT32_MAX) ? 
00
: Ret;
1058
28
}
1059
1060
// Fully parse the shared object file.
1061
//
1062
// This function parses symbol versions. If a DSO has version information,
1063
// the file has a ".gnu.version_d" section which contains symbol version
1064
// definitions. Each symbol is associated to one version through a table in
1065
// ".gnu.version" section. That table is a parallel array for the symbol
1066
// table, and each table entry contains an index in ".gnu.version_d".
1067
//
1068
// The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
1069
// VER_NDX_GLOBAL. There's no table entry for these special versions in
1070
// ".gnu.version_d".
1071
//
1072
// The file format for symbol versioning is perhaps a bit more complicated
1073
// than necessary, but you can easily understand the code if you wrap your
1074
// head around the data structure described above.
1075
187
template <class ELFT> void SharedFile::parse() {
1076
187
  using Elf_Dyn = typename ELFT::Dyn;
1077
187
  using Elf_Shdr = typename ELFT::Shdr;
1078
187
  using Elf_Sym = typename ELFT::Sym;
1079
187
  using Elf_Verdef = typename ELFT::Verdef;
1080
187
  using Elf_Versym = typename ELFT::Versym;
1081
187
1082
187
  ArrayRef<Elf_Dyn> DynamicTags;
1083
187
  const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1084
187
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1085
187
1086
187
  const Elf_Shdr *VersymSec = nullptr;
1087
187
  const Elf_Shdr *VerdefSec = nullptr;
1088
187
1089
187
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1090
2.23k
  for (const Elf_Shdr &Sec : Sections) {
1091
2.23k
    switch (Sec.sh_type) {
1092
2.23k
    default:
1093
1.84k
      continue;
1094
2.23k
    case SHT_DYNSYM:
1095
187
      this->initSymtab<ELFT>(Sections, &Sec);
1096
187
      break;
1097
2.23k
    case SHT_DYNAMIC:
1098
187
      DynamicTags =
1099
187
          CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1100
187
      break;
1101
2.23k
    case SHT_GNU_versym:
1102
10
      VersymSec = &Sec;
1103
10
      break;
1104
2.23k
    case SHT_GNU_verdef:
1105
10
      VerdefSec = &Sec;
1106
10
      break;
1107
2.23k
    }
1108
2.23k
  }
1109
187
1110
187
  if (VersymSec && 
this->getELFSyms<ELFT>().empty()10
) {
1111
0
    error("SHT_GNU_versym should be associated with symbol table");
1112
0
    return;
1113
0
  }
1114
187
1115
187
  // Search for a DT_SONAME tag to initialize this->SoName.
1116
1.55k
  
for (const Elf_Dyn &Dyn : DynamicTags)187
{
1117
1.55k
    if (Dyn.d_tag == DT_NEEDED) {
1118
2
      uint64_t Val = Dyn.getVal();
1119
2
      if (Val >= this->StringTable.size())
1120
0
        fatal(toString(this) + ": invalid DT_NEEDED entry");
1121
2
      DtNeeded.push_back(this->StringTable.data() + Val);
1122
1.54k
    } else if (Dyn.d_tag == DT_SONAME) {
1123
31
      uint64_t Val = Dyn.getVal();
1124
31
      if (Val >= this->StringTable.size())
1125
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
1126
31
      SoName = this->StringTable.data() + Val;
1127
31
    }
1128
1.55k
  }
1129
187
1130
187
  // DSOs are uniquified not by filename but by soname.
1131
187
  DenseMap<StringRef, SharedFile *>::iterator It;
1132
187
  bool WasInserted;
1133
187
  std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1134
187
1135
187
  // If a DSO appears more than once on the command line with and without
1136
187
  // --as-needed, --no-as-needed takes precedence over --as-needed because a
1137
187
  // user can add an extra DSO with --no-as-needed to force it to be added to
1138
187
  // the dependency list.
1139
187
  It->second->IsNeeded |= IsNeeded;
1140
187
  if (!WasInserted)
1141
1
    return;
1142
186
1143
186
  SharedFiles.push_back(this);
1144
186
1145
186
  Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1146
186
1147
186
  // Parse ".gnu.version" section which is a parallel array for the symbol
1148
186
  // table. If a given file doesn't have a ".gnu.version" section, we use
1149
186
  // VER_NDX_GLOBAL.
1150
186
  size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1151
186
  std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1152
186
  if (VersymSec) {
1153
10
    ArrayRef<Elf_Versym> Versym =
1154
10
        CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1155
10
              this)
1156
10
            .slice(FirstGlobal);
1157
54
    for (size_t I = 0; I < Size; 
++I44
)
1158
44
      Versyms[I] = Versym[I].vs_index;
1159
10
  }
1160
186
1161
186
  // System libraries can have a lot of symbols with versions. Using a
1162
186
  // fixed buffer for computing the versions name (foo@ver) can save a
1163
186
  // lot of allocations.
1164
186
  SmallString<0> VersionedNameBuffer;
1165
186
1166
186
  // Add symbols to the symbol table.
1167
186
  ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1168
635
  for (size_t I = 0; I < Syms.size(); 
++I449
) {
1169
449
    const Elf_Sym &Sym = Syms[I];
1170
449
1171
449
    // ELF spec requires that all local symbols precede weak or global
1172
449
    // symbols in each symbol table, and the index of first non-local symbol
1173
449
    // is stored to sh_info. If a local symbol appears after some non-local
1174
449
    // symbol, that's a violation of the spec.
1175
449
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1176
449
    if (Sym.getBinding() == STB_LOCAL) {
1177
0
      warn("found local symbol '" + Name +
1178
0
           "' in global part of symbol table in file " + toString(this));
1179
0
      continue;
1180
0
    }
1181
449
1182
449
    if (Sym.isUndefined()) {
1183
25
      Symbol *S = Symtab->addSymbol(
1184
25
          Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()});
1185
25
      S->ExportDynamic = true;
1186
25
      continue;
1187
25
    }
1188
424
1189
424
    // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1190
424
    // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1191
424
    // workaround for this bug.
1192
424
    uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1193
424
    if (Config->EMachine == EM_MIPS && 
Idx == VER_NDX_LOCAL65
&&
1194
424
        
Name == "_gp_disp"0
)
1195
0
      continue;
1196
424
1197
424
    uint32_t Alignment = getAlignment<ELFT>(Sections, Sym);
1198
424
    if (!(Versyms[I] & VERSYM_HIDDEN)) {
1199
409
      Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(),
1200
409
                                     Sym.st_other, Sym.getType(), Sym.st_value,
1201
409
                                     Sym.st_size, Alignment, Idx});
1202
409
    }
1203
424
1204
424
    // Also add the symbol with the versioned name to handle undefined symbols
1205
424
    // with explicit versions.
1206
424
    if (Idx == VER_NDX_GLOBAL)
1207
394
      continue;
1208
30
1209
30
    if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1210
0
      error("corrupt input file: version definition index " + Twine(Idx) +
1211
0
            " for symbol " + Name + " is out of bounds\n>>> defined in " +
1212
0
            toString(this));
1213
0
      continue;
1214
0
    }
1215
30
1216
30
    StringRef VerName =
1217
30
        this->StringTable.data() +
1218
30
        reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1219
30
    VersionedNameBuffer.clear();
1220
30
    Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1221
30
    Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(),
1222
30
                                   Sym.st_other, Sym.getType(), Sym.st_value,
1223
30
                                   Sym.st_size, Alignment, Idx});
1224
30
  }
1225
186
}
void lld::elf::SharedFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
1075
26
template <class ELFT> void SharedFile::parse() {
1076
26
  using Elf_Dyn = typename ELFT::Dyn;
1077
26
  using Elf_Shdr = typename ELFT::Shdr;
1078
26
  using Elf_Sym = typename ELFT::Sym;
1079
26
  using Elf_Verdef = typename ELFT::Verdef;
1080
26
  using Elf_Versym = typename ELFT::Versym;
1081
26
1082
26
  ArrayRef<Elf_Dyn> DynamicTags;
1083
26
  const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1084
26
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1085
26
1086
26
  const Elf_Shdr *VersymSec = nullptr;
1087
26
  const Elf_Shdr *VerdefSec = nullptr;
1088
26
1089
26
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1090
293
  for (const Elf_Shdr &Sec : Sections) {
1091
293
    switch (Sec.sh_type) {
1092
293
    default:
1093
241
      continue;
1094
293
    case SHT_DYNSYM:
1095
26
      this->initSymtab<ELFT>(Sections, &Sec);
1096
26
      break;
1097
293
    case SHT_DYNAMIC:
1098
26
      DynamicTags =
1099
26
          CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1100
26
      break;
1101
293
    case SHT_GNU_versym:
1102
0
      VersymSec = &Sec;
1103
0
      break;
1104
293
    case SHT_GNU_verdef:
1105
0
      VerdefSec = &Sec;
1106
0
      break;
1107
293
    }
1108
293
  }
1109
26
1110
26
  if (VersymSec && 
this->getELFSyms<ELFT>().empty()0
) {
1111
0
    error("SHT_GNU_versym should be associated with symbol table");
1112
0
    return;
1113
0
  }
1114
26
1115
26
  // Search for a DT_SONAME tag to initialize this->SoName.
1116
192
  
for (const Elf_Dyn &Dyn : DynamicTags)26
{
1117
192
    if (Dyn.d_tag == DT_NEEDED) {
1118
0
      uint64_t Val = Dyn.getVal();
1119
0
      if (Val >= this->StringTable.size())
1120
0
        fatal(toString(this) + ": invalid DT_NEEDED entry");
1121
0
      DtNeeded.push_back(this->StringTable.data() + Val);
1122
192
    } else if (Dyn.d_tag == DT_SONAME) {
1123
7
      uint64_t Val = Dyn.getVal();
1124
7
      if (Val >= this->StringTable.size())
1125
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
1126
7
      SoName = this->StringTable.data() + Val;
1127
7
    }
1128
192
  }
1129
26
1130
26
  // DSOs are uniquified not by filename but by soname.
1131
26
  DenseMap<StringRef, SharedFile *>::iterator It;
1132
26
  bool WasInserted;
1133
26
  std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1134
26
1135
26
  // If a DSO appears more than once on the command line with and without
1136
26
  // --as-needed, --no-as-needed takes precedence over --as-needed because a
1137
26
  // user can add an extra DSO with --no-as-needed to force it to be added to
1138
26
  // the dependency list.
1139
26
  It->second->IsNeeded |= IsNeeded;
1140
26
  if (!WasInserted)
1141
0
    return;
1142
26
1143
26
  SharedFiles.push_back(this);
1144
26
1145
26
  Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1146
26
1147
26
  // Parse ".gnu.version" section which is a parallel array for the symbol
1148
26
  // table. If a given file doesn't have a ".gnu.version" section, we use
1149
26
  // VER_NDX_GLOBAL.
1150
26
  size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1151
26
  std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1152
26
  if (VersymSec) {
1153
0
    ArrayRef<Elf_Versym> Versym =
1154
0
        CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1155
0
              this)
1156
0
            .slice(FirstGlobal);
1157
0
    for (size_t I = 0; I < Size; ++I)
1158
0
      Versyms[I] = Versym[I].vs_index;
1159
0
  }
1160
26
1161
26
  // System libraries can have a lot of symbols with versions. Using a
1162
26
  // fixed buffer for computing the versions name (foo@ver) can save a
1163
26
  // lot of allocations.
1164
26
  SmallString<0> VersionedNameBuffer;
1165
26
1166
26
  // Add symbols to the symbol table.
1167
26
  ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1168
82
  for (size_t I = 0; I < Syms.size(); 
++I56
) {
1169
56
    const Elf_Sym &Sym = Syms[I];
1170
56
1171
56
    // ELF spec requires that all local symbols precede weak or global
1172
56
    // symbols in each symbol table, and the index of first non-local symbol
1173
56
    // is stored to sh_info. If a local symbol appears after some non-local
1174
56
    // symbol, that's a violation of the spec.
1175
56
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1176
56
    if (Sym.getBinding() == STB_LOCAL) {
1177
0
      warn("found local symbol '" + Name +
1178
0
           "' in global part of symbol table in file " + toString(this));
1179
0
      continue;
1180
0
    }
1181
56
1182
56
    if (Sym.isUndefined()) {
1183
1
      Symbol *S = Symtab->addSymbol(
1184
1
          Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()});
1185
1
      S->ExportDynamic = true;
1186
1
      continue;
1187
1
    }
1188
55
1189
55
    // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1190
55
    // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1191
55
    // workaround for this bug.
1192
55
    uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1193
55
    if (Config->EMachine == EM_MIPS && 
Idx == VER_NDX_LOCAL0
&&
1194
55
        
Name == "_gp_disp"0
)
1195
0
      continue;
1196
55
1197
55
    uint32_t Alignment = getAlignment<ELFT>(Sections, Sym);
1198
55
    if (!(Versyms[I] & VERSYM_HIDDEN)) {
1199
55
      Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(),
1200
55
                                     Sym.st_other, Sym.getType(), Sym.st_value,
1201
55
                                     Sym.st_size, Alignment, Idx});
1202
55
    }
1203
55
1204
55
    // Also add the symbol with the versioned name to handle undefined symbols
1205
55
    // with explicit versions.
1206
55
    if (Idx == VER_NDX_GLOBAL)
1207
55
      continue;
1208
0
1209
0
    if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1210
0
      error("corrupt input file: version definition index " + Twine(Idx) +
1211
0
            " for symbol " + Name + " is out of bounds\n>>> defined in " +
1212
0
            toString(this));
1213
0
      continue;
1214
0
    }
1215
0
1216
0
    StringRef VerName =
1217
0
        this->StringTable.data() +
1218
0
        reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1219
0
    VersionedNameBuffer.clear();
1220
0
    Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1221
0
    Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(),
1222
0
                                   Sym.st_other, Sym.getType(), Sym.st_value,
1223
0
                                   Sym.st_size, Alignment, Idx});
1224
0
  }
1225
26
}
void lld::elf::SharedFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
1075
13
template <class ELFT> void SharedFile::parse() {
1076
13
  using Elf_Dyn = typename ELFT::Dyn;
1077
13
  using Elf_Shdr = typename ELFT::Shdr;
1078
13
  using Elf_Sym = typename ELFT::Sym;
1079
13
  using Elf_Verdef = typename ELFT::Verdef;
1080
13
  using Elf_Versym = typename ELFT::Versym;
1081
13
1082
13
  ArrayRef<Elf_Dyn> DynamicTags;
1083
13
  const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1084
13
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1085
13
1086
13
  const Elf_Shdr *VersymSec = nullptr;
1087
13
  const Elf_Shdr *VerdefSec = nullptr;
1088
13
1089
13
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1090
195
  for (const Elf_Shdr &Sec : Sections) {
1091
195
    switch (Sec.sh_type) {
1092
195
    default:
1093
169
      continue;
1094
195
    case SHT_DYNSYM:
1095
13
      this->initSymtab<ELFT>(Sections, &Sec);
1096
13
      break;
1097
195
    case SHT_DYNAMIC:
1098
13
      DynamicTags =
1099
13
          CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1100
13
      break;
1101
195
    case SHT_GNU_versym:
1102
0
      VersymSec = &Sec;
1103
0
      break;
1104
195
    case SHT_GNU_verdef:
1105
0
      VerdefSec = &Sec;
1106
0
      break;
1107
195
    }
1108
195
  }
1109
13
1110
13
  if (VersymSec && 
this->getELFSyms<ELFT>().empty()0
) {
1111
0
    error("SHT_GNU_versym should be associated with symbol table");
1112
0
    return;
1113
0
  }
1114
13
1115
13
  // Search for a DT_SONAME tag to initialize this->SoName.
1116
169
  
for (const Elf_Dyn &Dyn : DynamicTags)13
{
1117
169
    if (Dyn.d_tag == DT_NEEDED) {
1118
0
      uint64_t Val = Dyn.getVal();
1119
0
      if (Val >= this->StringTable.size())
1120
0
        fatal(toString(this) + ": invalid DT_NEEDED entry");
1121
0
      DtNeeded.push_back(this->StringTable.data() + Val);
1122
169
    } else if (Dyn.d_tag == DT_SONAME) {
1123
0
      uint64_t Val = Dyn.getVal();
1124
0
      if (Val >= this->StringTable.size())
1125
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
1126
0
      SoName = this->StringTable.data() + Val;
1127
0
    }
1128
169
  }
1129
13
1130
13
  // DSOs are uniquified not by filename but by soname.
1131
13
  DenseMap<StringRef, SharedFile *>::iterator It;
1132
13
  bool WasInserted;
1133
13
  std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1134
13
1135
13
  // If a DSO appears more than once on the command line with and without
1136
13
  // --as-needed, --no-as-needed takes precedence over --as-needed because a
1137
13
  // user can add an extra DSO with --no-as-needed to force it to be added to
1138
13
  // the dependency list.
1139
13
  It->second->IsNeeded |= IsNeeded;
1140
13
  if (!WasInserted)
1141
0
    return;
1142
13
1143
13
  SharedFiles.push_back(this);
1144
13
1145
13
  Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1146
13
1147
13
  // Parse ".gnu.version" section which is a parallel array for the symbol
1148
13
  // table. If a given file doesn't have a ".gnu.version" section, we use
1149
13
  // VER_NDX_GLOBAL.
1150
13
  size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1151
13
  std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1152
13
  if (VersymSec) {
1153
0
    ArrayRef<Elf_Versym> Versym =
1154
0
        CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1155
0
              this)
1156
0
            .slice(FirstGlobal);
1157
0
    for (size_t I = 0; I < Size; ++I)
1158
0
      Versyms[I] = Versym[I].vs_index;
1159
0
  }
1160
13
1161
13
  // System libraries can have a lot of symbols with versions. Using a
1162
13
  // fixed buffer for computing the versions name (foo@ver) can save a
1163
13
  // lot of allocations.
1164
13
  SmallString<0> VersionedNameBuffer;
1165
13
1166
13
  // Add symbols to the symbol table.
1167
13
  ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1168
60
  for (size_t I = 0; I < Syms.size(); 
++I47
) {
1169
47
    const Elf_Sym &Sym = Syms[I];
1170
47
1171
47
    // ELF spec requires that all local symbols precede weak or global
1172
47
    // symbols in each symbol table, and the index of first non-local symbol
1173
47
    // is stored to sh_info. If a local symbol appears after some non-local
1174
47
    // symbol, that's a violation of the spec.
1175
47
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1176
47
    if (Sym.getBinding() == STB_LOCAL) {
1177
0
      warn("found local symbol '" + Name +
1178
0
           "' in global part of symbol table in file " + toString(this));
1179
0
      continue;
1180
0
    }
1181
47
1182
47
    if (Sym.isUndefined()) {
1183
0
      Symbol *S = Symtab->addSymbol(
1184
0
          Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()});
1185
0
      S->ExportDynamic = true;
1186
0
      continue;
1187
0
    }
1188
47
1189
47
    // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1190
47
    // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1191
47
    // workaround for this bug.
1192
47
    uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1193
47
    if (Config->EMachine == EM_MIPS && Idx == VER_NDX_LOCAL &&
1194
47
        
Name == "_gp_disp"0
)
1195
0
      continue;
1196
47
1197
47
    uint32_t Alignment = getAlignment<ELFT>(Sections, Sym);
1198
47
    if (!(Versyms[I] & VERSYM_HIDDEN)) {
1199
47
      Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(),
1200
47
                                     Sym.st_other, Sym.getType(), Sym.st_value,
1201
47
                                     Sym.st_size, Alignment, Idx});
1202
47
    }
1203
47
1204
47
    // Also add the symbol with the versioned name to handle undefined symbols
1205
47
    // with explicit versions.
1206
47
    if (Idx == VER_NDX_GLOBAL)
1207
47
      continue;
1208
0
1209
0
    if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1210
0
      error("corrupt input file: version definition index " + Twine(Idx) +
1211
0
            " for symbol " + Name + " is out of bounds\n>>> defined in " +
1212
0
            toString(this));
1213
0
      continue;
1214
0
    }
1215
0
1216
0
    StringRef VerName =
1217
0
        this->StringTable.data() +
1218
0
        reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1219
0
    VersionedNameBuffer.clear();
1220
0
    Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1221
0
    Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(),
1222
0
                                   Sym.st_other, Sym.getType(), Sym.st_value,
1223
0
                                   Sym.st_size, Alignment, Idx});
1224
0
  }
1225
13
}
void lld::elf::SharedFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1075
136
template <class ELFT> void SharedFile::parse() {
1076
136
  using Elf_Dyn = typename ELFT::Dyn;
1077
136
  using Elf_Shdr = typename ELFT::Shdr;
1078
136
  using Elf_Sym = typename ELFT::Sym;
1079
136
  using Elf_Verdef = typename ELFT::Verdef;
1080
136
  using Elf_Versym = typename ELFT::Versym;
1081
136
1082
136
  ArrayRef<Elf_Dyn> DynamicTags;
1083
136
  const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1084
136
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1085
136
1086
136
  const Elf_Shdr *VersymSec = nullptr;
1087
136
  const Elf_Shdr *VerdefSec = nullptr;
1088
136
1089
136
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1090
1.58k
  for (const Elf_Shdr &Sec : Sections) {
1091
1.58k
    switch (Sec.sh_type) {
1092
1.58k
    default:
1093
1.28k
      continue;
1094
1.58k
    case SHT_DYNSYM:
1095
136
      this->initSymtab<ELFT>(Sections, &Sec);
1096
136
      break;
1097
1.58k
    case SHT_DYNAMIC:
1098
136
      DynamicTags =
1099
136
          CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1100
136
      break;
1101
1.58k
    case SHT_GNU_versym:
1102
10
      VersymSec = &Sec;
1103
10
      break;
1104
1.58k
    case SHT_GNU_verdef:
1105
10
      VerdefSec = &Sec;
1106
10
      break;
1107
1.58k
    }
1108
1.58k
  }
1109
136
1110
136
  if (VersymSec && 
this->getELFSyms<ELFT>().empty()10
) {
1111
0
    error("SHT_GNU_versym should be associated with symbol table");
1112
0
    return;
1113
0
  }
1114
136
1115
136
  // Search for a DT_SONAME tag to initialize this->SoName.
1116
1.06k
  
for (const Elf_Dyn &Dyn : DynamicTags)136
{
1117
1.06k
    if (Dyn.d_tag == DT_NEEDED) {
1118
2
      uint64_t Val = Dyn.getVal();
1119
2
      if (Val >= this->StringTable.size())
1120
0
        fatal(toString(this) + ": invalid DT_NEEDED entry");
1121
2
      DtNeeded.push_back(this->StringTable.data() + Val);
1122
1.06k
    } else if (Dyn.d_tag == DT_SONAME) {
1123
24
      uint64_t Val = Dyn.getVal();
1124
24
      if (Val >= this->StringTable.size())
1125
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
1126
24
      SoName = this->StringTable.data() + Val;
1127
24
    }
1128
1.06k
  }
1129
136
1130
136
  // DSOs are uniquified not by filename but by soname.
1131
136
  DenseMap<StringRef, SharedFile *>::iterator It;
1132
136
  bool WasInserted;
1133
136
  std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1134
136
1135
136
  // If a DSO appears more than once on the command line with and without
1136
136
  // --as-needed, --no-as-needed takes precedence over --as-needed because a
1137
136
  // user can add an extra DSO with --no-as-needed to force it to be added to
1138
136
  // the dependency list.
1139
136
  It->second->IsNeeded |= IsNeeded;
1140
136
  if (!WasInserted)
1141
1
    return;
1142
135
1143
135
  SharedFiles.push_back(this);
1144
135
1145
135
  Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1146
135
1147
135
  // Parse ".gnu.version" section which is a parallel array for the symbol
1148
135
  // table. If a given file doesn't have a ".gnu.version" section, we use
1149
135
  // VER_NDX_GLOBAL.
1150
135
  size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1151
135
  std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1152
135
  if (VersymSec) {
1153
10
    ArrayRef<Elf_Versym> Versym =
1154
10
        CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1155
10
              this)
1156
10
            .slice(FirstGlobal);
1157
54
    for (size_t I = 0; I < Size; 
++I44
)
1158
44
      Versyms[I] = Versym[I].vs_index;
1159
10
  }
1160
135
1161
135
  // System libraries can have a lot of symbols with versions. Using a
1162
135
  // fixed buffer for computing the versions name (foo@ver) can save a
1163
135
  // lot of allocations.
1164
135
  SmallString<0> VersionedNameBuffer;
1165
135
1166
135
  // Add symbols to the symbol table.
1167
135
  ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1168
453
  for (size_t I = 0; I < Syms.size(); 
++I318
) {
1169
318
    const Elf_Sym &Sym = Syms[I];
1170
318
1171
318
    // ELF spec requires that all local symbols precede weak or global
1172
318
    // symbols in each symbol table, and the index of first non-local symbol
1173
318
    // is stored to sh_info. If a local symbol appears after some non-local
1174
318
    // symbol, that's a violation of the spec.
1175
318
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1176
318
    if (Sym.getBinding() == STB_LOCAL) {
1177
0
      warn("found local symbol '" + Name +
1178
0
           "' in global part of symbol table in file " + toString(this));
1179
0
      continue;
1180
0
    }
1181
318
1182
318
    if (Sym.isUndefined()) {
1183
24
      Symbol *S = Symtab->addSymbol(
1184
24
          Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()});
1185
24
      S->ExportDynamic = true;
1186
24
      continue;
1187
24
    }
1188
294
1189
294
    // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1190
294
    // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1191
294
    // workaround for this bug.
1192
294
    uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1193
294
    if (Config->EMachine == EM_MIPS && 
Idx == VER_NDX_LOCAL0
&&
1194
294
        
Name == "_gp_disp"0
)
1195
0
      continue;
1196
294
1197
294
    uint32_t Alignment = getAlignment<ELFT>(Sections, Sym);
1198
294
    if (!(Versyms[I] & VERSYM_HIDDEN)) {
1199
279
      Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(),
1200
279
                                     Sym.st_other, Sym.getType(), Sym.st_value,
1201
279
                                     Sym.st_size, Alignment, Idx});
1202
279
    }
1203
294
1204
294
    // Also add the symbol with the versioned name to handle undefined symbols
1205
294
    // with explicit versions.
1206
294
    if (Idx == VER_NDX_GLOBAL)
1207
264
      continue;
1208
30
1209
30
    if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1210
0
      error("corrupt input file: version definition index " + Twine(Idx) +
1211
0
            " for symbol " + Name + " is out of bounds\n>>> defined in " +
1212
0
            toString(this));
1213
0
      continue;
1214
0
    }
1215
30
1216
30
    StringRef VerName =
1217
30
        this->StringTable.data() +
1218
30
        reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1219
30
    VersionedNameBuffer.clear();
1220
30
    Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1221
30
    Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(),
1222
30
                                   Sym.st_other, Sym.getType(), Sym.st_value,
1223
30
                                   Sym.st_size, Alignment, Idx});
1224
30
  }
1225
135
}
void lld::elf::SharedFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
1075
12
template <class ELFT> void SharedFile::parse() {
1076
12
  using Elf_Dyn = typename ELFT::Dyn;
1077
12
  using Elf_Shdr = typename ELFT::Shdr;
1078
12
  using Elf_Sym = typename ELFT::Sym;
1079
12
  using Elf_Verdef = typename ELFT::Verdef;
1080
12
  using Elf_Versym = typename ELFT::Versym;
1081
12
1082
12
  ArrayRef<Elf_Dyn> DynamicTags;
1083
12
  const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1084
12
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1085
12
1086
12
  const Elf_Shdr *VersymSec = nullptr;
1087
12
  const Elf_Shdr *VerdefSec = nullptr;
1088
12
1089
12
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1090
166
  for (const Elf_Shdr &Sec : Sections) {
1091
166
    switch (Sec.sh_type) {
1092
166
    default:
1093
142
      continue;
1094
166
    case SHT_DYNSYM:
1095
12
      this->initSymtab<ELFT>(Sections, &Sec);
1096
12
      break;
1097
166
    case SHT_DYNAMIC:
1098
12
      DynamicTags =
1099
12
          CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1100
12
      break;
1101
166
    case SHT_GNU_versym:
1102
0
      VersymSec = &Sec;
1103
0
      break;
1104
166
    case SHT_GNU_verdef:
1105
0
      VerdefSec = &Sec;
1106
0
      break;
1107
166
    }
1108
166
  }
1109
12
1110
12
  if (VersymSec && 
this->getELFSyms<ELFT>().empty()0
) {
1111
0
    error("SHT_GNU_versym should be associated with symbol table");
1112
0
    return;
1113
0
  }
1114
12
1115
12
  // Search for a DT_SONAME tag to initialize this->SoName.
1116
120
  
for (const Elf_Dyn &Dyn : DynamicTags)12
{
1117
120
    if (Dyn.d_tag == DT_NEEDED) {
1118
0
      uint64_t Val = Dyn.getVal();
1119
0
      if (Val >= this->StringTable.size())
1120
0
        fatal(toString(this) + ": invalid DT_NEEDED entry");
1121
0
      DtNeeded.push_back(this->StringTable.data() + Val);
1122
120
    } else if (Dyn.d_tag == DT_SONAME) {
1123
0
      uint64_t Val = Dyn.getVal();
1124
0
      if (Val >= this->StringTable.size())
1125
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
1126
0
      SoName = this->StringTable.data() + Val;
1127
0
    }
1128
120
  }
1129
12
1130
12
  // DSOs are uniquified not by filename but by soname.
1131
12
  DenseMap<StringRef, SharedFile *>::iterator It;
1132
12
  bool WasInserted;
1133
12
  std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1134
12
1135
12
  // If a DSO appears more than once on the command line with and without
1136
12
  // --as-needed, --no-as-needed takes precedence over --as-needed because a
1137
12
  // user can add an extra DSO with --no-as-needed to force it to be added to
1138
12
  // the dependency list.
1139
12
  It->second->IsNeeded |= IsNeeded;
1140
12
  if (!WasInserted)
1141
0
    return;
1142
12
1143
12
  SharedFiles.push_back(this);
1144
12
1145
12
  Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1146
12
1147
12
  // Parse ".gnu.version" section which is a parallel array for the symbol
1148
12
  // table. If a given file doesn't have a ".gnu.version" section, we use
1149
12
  // VER_NDX_GLOBAL.
1150
12
  size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1151
12
  std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1152
12
  if (VersymSec) {
1153
0
    ArrayRef<Elf_Versym> Versym =
1154
0
        CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1155
0
              this)
1156
0
            .slice(FirstGlobal);
1157
0
    for (size_t I = 0; I < Size; ++I)
1158
0
      Versyms[I] = Versym[I].vs_index;
1159
0
  }
1160
12
1161
12
  // System libraries can have a lot of symbols with versions. Using a
1162
12
  // fixed buffer for computing the versions name (foo@ver) can save a
1163
12
  // lot of allocations.
1164
12
  SmallString<0> VersionedNameBuffer;
1165
12
1166
12
  // Add symbols to the symbol table.
1167
12
  ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1168
40
  for (size_t I = 0; I < Syms.size(); 
++I28
) {
1169
28
    const Elf_Sym &Sym = Syms[I];
1170
28
1171
28
    // ELF spec requires that all local symbols precede weak or global
1172
28
    // symbols in each symbol table, and the index of first non-local symbol
1173
28
    // is stored to sh_info. If a local symbol appears after some non-local
1174
28
    // symbol, that's a violation of the spec.
1175
28
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1176
28
    if (Sym.getBinding() == STB_LOCAL) {
1177
0
      warn("found local symbol '" + Name +
1178
0
           "' in global part of symbol table in file " + toString(this));
1179
0
      continue;
1180
0
    }
1181
28
1182
28
    if (Sym.isUndefined()) {
1183
0
      Symbol *S = Symtab->addSymbol(
1184
0
          Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()});
1185
0
      S->ExportDynamic = true;
1186
0
      continue;
1187
0
    }
1188
28
1189
28
    // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1190
28
    // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1191
28
    // workaround for this bug.
1192
28
    uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1193
28
    if (Config->EMachine == EM_MIPS && 
Idx == VER_NDX_LOCAL18
&&
1194
28
        
Name == "_gp_disp"0
)
1195
0
      continue;
1196
28
1197
28
    uint32_t Alignment = getAlignment<ELFT>(Sections, Sym);
1198
28
    if (!(Versyms[I] & VERSYM_HIDDEN)) {
1199
28
      Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(),
1200
28
                                     Sym.st_other, Sym.getType(), Sym.st_value,
1201
28
                                     Sym.st_size, Alignment, Idx});
1202
28
    }
1203
28
1204
28
    // Also add the symbol with the versioned name to handle undefined symbols
1205
28
    // with explicit versions.
1206
28
    if (Idx == VER_NDX_GLOBAL)
1207
28
      continue;
1208
0
1209
0
    if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1210
0
      error("corrupt input file: version definition index " + Twine(Idx) +
1211
0
            " for symbol " + Name + " is out of bounds\n>>> defined in " +
1212
0
            toString(this));
1213
0
      continue;
1214
0
    }
1215
0
1216
0
    StringRef VerName =
1217
0
        this->StringTable.data() +
1218
0
        reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1219
0
    VersionedNameBuffer.clear();
1220
0
    Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1221
0
    Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(),
1222
0
                                   Sym.st_other, Sym.getType(), Sym.st_value,
1223
0
                                   Sym.st_size, Alignment, Idx});
1224
0
  }
1225
12
}
1226
1227
125
static ELFKind getBitcodeELFKind(const Triple &T) {
1228
125
  if (T.isLittleEndian())
1229
125
    return T.isArch64Bit() ? ELF64LEKind : 
ELF32LEKind0
;
1230
0
  return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
1231
0
}
1232
1233
125
static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
1234
125
  switch (T.getArch()) {
1235
125
  case Triple::aarch64:
1236
0
    return EM_AARCH64;
1237
125
  case Triple::amdgcn:
1238
1
  case Triple::r600:
1239
1
    return EM_AMDGPU;
1240
1
  case Triple::arm:
1241
0
  case Triple::thumb:
1242
0
    return EM_ARM;
1243
0
  case Triple::avr:
1244
0
    return EM_AVR;
1245
0
  case Triple::mips:
1246
0
  case Triple::mipsel:
1247
0
  case Triple::mips64:
1248
0
  case Triple::mips64el:
1249
0
    return EM_MIPS;
1250
0
  case Triple::msp430:
1251
0
    return EM_MSP430;
1252
0
  case Triple::ppc:
1253
0
    return EM_PPC;
1254
0
  case Triple::ppc64:
1255
0
  case Triple::ppc64le:
1256
0
    return EM_PPC64;
1257
0
  case Triple::x86:
1258
0
    return T.isOSIAMCU() ? EM_IAMCU : EM_386;
1259
124
  case Triple::x86_64:
1260
124
    return EM_X86_64;
1261
0
  default:
1262
0
    error(Path + ": could not infer e_machine from bitcode target triple " +
1263
0
          T.str());
1264
0
    return EM_NONE;
1265
125
  }
1266
125
}
1267
1268
BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
1269
                         uint64_t OffsetInArchive)
1270
125
    : InputFile(BitcodeKind, MB) {
1271
125
  this->ArchiveName = ArchiveName;
1272
125
1273
125
  std::string Path = MB.getBufferIdentifier().str();
1274
125
  if (Config->ThinLTOIndexOnly)
1275
9
    Path = replaceThinLTOSuffix(MB.getBufferIdentifier());
1276
125
1277
125
  // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1278
125
  // name. If two archives define two members with the same name, this
1279
125
  // causes a collision which result in only one of the objects being taken
1280
125
  // into consideration at LTO time (which very likely causes undefined
1281
125
  // symbols later in the link stage). So we append file offset to make
1282
125
  // filename unique.
1283
125
  StringRef Name = ArchiveName.empty()
1284
125
                       ? 
Saver.save(Path)112
1285
125
                       : Saver.save(ArchiveName + "(" + Path + " at " +
1286
13
                                    utostr(OffsetInArchive) + ")");
1287
125
  MemoryBufferRef MBRef(MB.getBuffer(), Name);
1288
125
1289
125
  Obj = CHECK(lto::InputFile::create(MBRef), this);
1290
125
1291
125
  Triple T(Obj->getTargetTriple());
1292
125
  EKind = getBitcodeELFKind(T);
1293
125
  EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
1294
125
}
1295
1296
186
static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
1297
186
  switch (GvVisibility) {
1298
186
  case GlobalValue::DefaultVisibility:
1299
172
    return STV_DEFAULT;
1300
186
  case GlobalValue::HiddenVisibility:
1301
13
    return STV_HIDDEN;
1302
186
  case GlobalValue::ProtectedVisibility:
1303
1
    return STV_PROTECTED;
1304
0
  }
1305
0
  llvm_unreachable("unknown visibility");
1306
0
}
1307
1308
template <class ELFT>
1309
static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
1310
                                   const lto::InputFile::Symbol &ObjSym,
1311
186
                                   BitcodeFile &F) {
1312
186
  StringRef Name = Saver.save(ObjSym.getName());
1313
186
  uint8_t Binding = ObjSym.isWeak() ? 
STB_WEAK13
:
STB_GLOBAL173
;
1314
186
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE184
;
1315
186
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
1316
186
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
1317
186
1318
186
  int C = ObjSym.getComdatIndex();
1319
186
  if (ObjSym.isUndefined() || 
(139
C != -1139
&&
!KeptComdats[C]0
)) {
1320
47
    Undefined New(&F, Name, Binding, Visibility, Type);
1321
47
    if (CanOmitFromDynSym)
1322
0
      New.ExportDynamic = false;
1323
47
    return Symtab->addSymbol(New);
1324
47
  }
1325
139
1326
139
  if (ObjSym.isCommon())
1327
3
    return Symtab->addSymbol(
1328
3
        CommonSymbol{&F, Name, Binding, Visibility, STT_OBJECT,
1329
3
                     ObjSym.getCommonAlignment(), ObjSym.getCommonSize()});
1330
136
1331
136
  Defined New(&F, Name, Binding, Visibility, Type, 0, 0, nullptr);
1332
136
  if (CanOmitFromDynSym)
1333
2
    New.ExportDynamic = false;
1334
136
  return Symtab->addSymbol(New);
1335
136
}
Unexecuted instantiation: InputFiles.cpp:lld::elf::Symbol* createBitcodeSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(std::__1::vector<bool, std::__1::allocator<bool> > const&, llvm::lto::InputFile::Symbol const&, lld::elf::BitcodeFile&)
Unexecuted instantiation: InputFiles.cpp:lld::elf::Symbol* createBitcodeSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(std::__1::vector<bool, std::__1::allocator<bool> > const&, llvm::lto::InputFile::Symbol const&, lld::elf::BitcodeFile&)
InputFiles.cpp:lld::elf::Symbol* createBitcodeSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(std::__1::vector<bool, std::__1::allocator<bool> > const&, llvm::lto::InputFile::Symbol const&, lld::elf::BitcodeFile&)
Line
Count
Source
1311
186
                                   BitcodeFile &F) {
1312
186
  StringRef Name = Saver.save(ObjSym.getName());
1313
186
  uint8_t Binding = ObjSym.isWeak() ? 
STB_WEAK13
:
STB_GLOBAL173
;
1314
186
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE184
;
1315
186
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
1316
186
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
1317
186
1318
186
  int C = ObjSym.getComdatIndex();
1319
186
  if (ObjSym.isUndefined() || 
(139
C != -1139
&&
!KeptComdats[C]0
)) {
1320
47
    Undefined New(&F, Name, Binding, Visibility, Type);
1321
47
    if (CanOmitFromDynSym)
1322
0
      New.ExportDynamic = false;
1323
47
    return Symtab->addSymbol(New);
1324
47
  }
1325
139
1326
139
  if (ObjSym.isCommon())
1327
3
    return Symtab->addSymbol(
1328
3
        CommonSymbol{&F, Name, Binding, Visibility, STT_OBJECT,
1329
3
                     ObjSym.getCommonAlignment(), ObjSym.getCommonSize()});
1330
136
1331
136
  Defined New(&F, Name, Binding, Visibility, Type, 0, 0, nullptr);
1332
136
  if (CanOmitFromDynSym)
1333
2
    New.ExportDynamic = false;
1334
136
  return Symtab->addSymbol(New);
1335
136
}
Unexecuted instantiation: InputFiles.cpp:lld::elf::Symbol* createBitcodeSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(std::__1::vector<bool, std::__1::allocator<bool> > const&, llvm::lto::InputFile::Symbol const&, lld::elf::BitcodeFile&)
1336
1337
template <class ELFT>
1338
119
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
1339
119
  std::vector<bool> KeptComdats;
1340
119
  for (StringRef S : Obj->getComdatTable())
1341
0
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
1342
119
1343
119
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
1344
186
    Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
1345
119
1346
119
  for (auto L : Obj->getDependentLibraries())
1347
2
    addDependentLibrary(L, this);
1348
119
}
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
1338
119
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
1339
119
  std::vector<bool> KeptComdats;
1340
119
  for (StringRef S : Obj->getComdatTable())
1341
0
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
1342
119
1343
119
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
1344
186
    Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
1345
119
1346
119
  for (auto L : Obj->getDependentLibraries())
1347
2
    addDependentLibrary(L, this);
1348
119
}
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
1349
1350
1.90k
static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) {
1351
1.90k
  unsigned char Size;
1352
1.90k
  unsigned char Endian;
1353
1.90k
  std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
1354
1.90k
1355
1.90k
  auto Fatal = [&](StringRef Msg) {
1356
0
    StringRef Filename = MB.getBufferIdentifier();
1357
0
    if (ArchiveName.empty())
1358
0
      fatal(Filename + ": " + Msg);
1359
0
    else
1360
0
      fatal(ArchiveName + "(" + Filename + "): " + Msg);
1361
0
  };
1362
1.90k
1363
1.90k
  if (!MB.getBuffer().startswith(ElfMagic))
1364
0
    Fatal("not an ELF file");
1365
1.90k
  if (Endian != ELFDATA2LSB && 
Endian != ELFDATA2MSB201
)
1366
0
    Fatal("corrupted ELF file: invalid data encoding");
1367
1.90k
  if (Size != ELFCLASS32 && 
Size != ELFCLASS641.56k
)
1368
0
    Fatal("corrupted ELF file: invalid file class");
1369
1.90k
1370
1.90k
  size_t BufSize = MB.getBuffer().size();
1371
1.90k
  if ((Size == ELFCLASS32 && 
BufSize < sizeof(Elf32_Ehdr)335
) ||
1372
1.90k
      (Size == ELFCLASS64 && 
BufSize < sizeof(Elf64_Ehdr)1.56k
))
1373
0
    Fatal("corrupted ELF file: file is too short");
1374
1.90k
1375
1.90k
  if (Size == ELFCLASS32)
1376
335
    return (Endian == ELFDATA2LSB) ? 
ELF32LEKind225
:
ELF32BEKind110
;
1377
1.56k
  return (Endian == ELFDATA2LSB) ? 
ELF64LEKind1.47k
:
ELF64BEKind91
;
1378
1.56k
}
1379
1380
5
void BinaryFile::parse() {
1381
5
  ArrayRef<uint8_t> Data = arrayRefFromStringRef(MB.getBuffer());
1382
5
  auto *Section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1383
5
                                     8, Data, ".data");
1384
5
  Sections.push_back(Section);
1385
5
1386
5
  // For each input file foo that is embedded to a result as a binary
1387
5
  // blob, we define _binary_foo_{start,end,size} symbols, so that
1388
5
  // user programs can access blobs by name. Non-alphanumeric
1389
5
  // characters in a filename are replaced with underscore.
1390
5
  std::string S = "_binary_" + MB.getBufferIdentifier().str();
1391
565
  for (size_t I = 0; I < S.size(); 
++I560
)
1392
560
    if (!isAlnum(S[I]))
1393
95
      S[I] = '_';
1394
5
1395
5
  Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_start"), STB_GLOBAL,
1396
5
                            STV_DEFAULT, STT_OBJECT, 0, 0, Section});
1397
5
  Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_end"), STB_GLOBAL,
1398
5
                            STV_DEFAULT, STT_OBJECT, Data.size(), 0, Section});
1399
5
  Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_size"), STB_GLOBAL,
1400
5
                            STV_DEFAULT, STT_OBJECT, Data.size(), 0, nullptr});
1401
5
}
1402
1403
InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
1404
1.83k
                                 uint64_t OffsetInArchive) {
1405
1.83k
  if (isBitcode(MB))
1406
125
    return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
1407
1.70k
1408
1.70k
  switch (getELFKind(MB, ArchiveName)) {
1409
1.70k
  case ELF32LEKind:
1410
197
    return make<ObjFile<ELF32LE>>(MB, ArchiveName);
1411
1.70k
  case ELF32BEKind:
1412
97
    return make<ObjFile<ELF32BE>>(MB, ArchiveName);
1413
1.70k
  case ELF64LEKind:
1414
1.33k
    return make<ObjFile<ELF64LE>>(MB, ArchiveName);
1415
1.70k
  case ELF64BEKind:
1416
79
    return make<ObjFile<ELF64BE>>(MB, ArchiveName);
1417
1.70k
  default:
1418
0
    llvm_unreachable("getELFKind");
1419
1.70k
  }
1420
1.70k
}
1421
1422
189
InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
1423
189
  auto *F = make<SharedFile>(MB, DefaultSoName);
1424
189
  switch (getELFKind(MB, "")) {
1425
189
  case ELF32LEKind:
1426
27
    F->parseHeader<ELF32LE>();
1427
27
    break;
1428
189
  case ELF32BEKind:
1429
13
    F->parseHeader<ELF32BE>();
1430
13
    break;
1431
189
  case ELF64LEKind:
1432
137
    F->parseHeader<ELF64LE>();
1433
137
    break;
1434
189
  case ELF64BEKind:
1435
12
    F->parseHeader<ELF64BE>();
1436
12
    break;
1437
189
  default:
1438
0
    llvm_unreachable("getELFKind");
1439
189
  }
1440
189
  return F;
1441
189
}
1442
1443
7
MemoryBufferRef LazyObjFile::getBuffer() {
1444
7
  if (AddedToLink)
1445
1
    return MemoryBufferRef();
1446
6
  AddedToLink = true;
1447
6
  return MB;
1448
6
}
1449
1450
7
InputFile *LazyObjFile::fetch() {
1451
7
  MemoryBufferRef MBRef = getBuffer();
1452
7
  if (MBRef.getBuffer().empty())
1453
1
    return nullptr;
1454
6
1455
6
  InputFile *File = createObjectFile(MBRef, ArchiveName, OffsetInArchive);
1456
6
  File->GroupId = GroupId;
1457
6
  return File;
1458
6
}
1459
1460
14
template <class ELFT> void LazyObjFile::parse() {
1461
14
  // A lazy object file wraps either a bitcode file or an ELF file.
1462
14
  if (isBitcode(this->MB)) {
1463
7
    std::unique_ptr<lto::InputFile> Obj =
1464
7
        CHECK(lto::InputFile::create(this->MB), this);
1465
10
    for (const lto::InputFile::Symbol &Sym : Obj->symbols()) {
1466
10
      if (Sym.isUndefined())
1467
3
        continue;
1468
7
      Symtab->addSymbol(LazyObject{*this, Saver.save(Sym.getName())});
1469
7
    }
1470
7
    return;
1471
7
  }
1472
7
1473
7
  if (getELFKind(this->MB, ArchiveName) != Config->EKind) {
1474
1
    error("incompatible file: " + this->MB.getBufferIdentifier());
1475
1
    return;
1476
1
  }
1477
6
1478
6
  ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
1479
6
  ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
1480
6
1481
26
  for (const typename ELFT::Shdr &Sec : Sections) {
1482
26
    if (Sec.sh_type != SHT_SYMTAB)
1483
20
      continue;
1484
6
1485
6
    typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this);
1486
6
    uint32_t FirstGlobal = Sec.sh_info;
1487
6
    StringRef StringTable =
1488
6
        CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1489
6
1490
7
    for (const typename ELFT::Sym &Sym : Syms.slice(FirstGlobal)) {
1491
7
      if (Sym.st_shndx == SHN_UNDEF)
1492
0
        continue;
1493
7
      Symtab->addSymbol(
1494
7
          LazyObject{*this, CHECK(Sym.getName(StringTable), this)});
1495
7
    }
1496
6
    return;
1497
6
  }
1498
6
}
Unexecuted instantiation: void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Unexecuted instantiation: void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1460
14
template <class ELFT> void LazyObjFile::parse() {
1461
14
  // A lazy object file wraps either a bitcode file or an ELF file.
1462
14
  if (isBitcode(this->MB)) {
1463
7
    std::unique_ptr<lto::InputFile> Obj =
1464
7
        CHECK(lto::InputFile::create(this->MB), this);
1465
10
    for (const lto::InputFile::Symbol &Sym : Obj->symbols()) {
1466
10
      if (Sym.isUndefined())
1467
3
        continue;
1468
7
      Symtab->addSymbol(LazyObject{*this, Saver.save(Sym.getName())});
1469
7
    }
1470
7
    return;
1471
7
  }
1472
7
1473
7
  if (getELFKind(this->MB, ArchiveName) != Config->EKind) {
1474
1
    error("incompatible file: " + this->MB.getBufferIdentifier());
1475
1
    return;
1476
1
  }
1477
6
1478
6
  ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
1479
6
  ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
1480
6
1481
26
  for (const typename ELFT::Shdr &Sec : Sections) {
1482
26
    if (Sec.sh_type != SHT_SYMTAB)
1483
20
      continue;
1484
6
1485
6
    typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this);
1486
6
    uint32_t FirstGlobal = Sec.sh_info;
1487
6
    StringRef StringTable =
1488
6
        CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1489
6
1490
7
    for (const typename ELFT::Sym &Sym : Syms.slice(FirstGlobal)) {
1491
7
      if (Sym.st_shndx == SHN_UNDEF)
1492
0
        continue;
1493
7
      Symtab->addSymbol(
1494
7
          LazyObject{*this, CHECK(Sym.getName(StringTable), this)});
1495
7
    }
1496
6
    return;
1497
6
  }
1498
6
}
Unexecuted instantiation: void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
1499
1500
11
std::string elf::replaceThinLTOSuffix(StringRef Path) {
1501
11
  StringRef Suffix = Config->ThinLTOObjectSuffixReplace.first;
1502
11
  StringRef Repl = Config->ThinLTOObjectSuffixReplace.second;
1503
11
1504
11
  if (Path.consume_back(Suffix))
1505
10
    return (Path + Repl).str();
1506
1
  return Path;
1507
1
}
1508
1509
template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1510
template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1511
template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1512
template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1513
1514
template void LazyObjFile::parse<ELF32LE>();
1515
template void LazyObjFile::parse<ELF32BE>();
1516
template void LazyObjFile::parse<ELF64LE>();
1517
template void LazyObjFile::parse<ELF64BE>();
1518
1519
template class elf::ObjFile<ELF32LE>;
1520
template class elf::ObjFile<ELF32BE>;
1521
template class elf::ObjFile<ELF64LE>;
1522
template class elf::ObjFile<ELF64BE>;
1523
1524
template void SharedFile::parse<ELF32LE>();
1525
template void SharedFile::parse<ELF32BE>();
1526
template void SharedFile::parse<ELF64LE>();
1527
template void SharedFile::parse<ELF64BE>();