Coverage Report

Created: 2018-01-17 17:22

/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
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "InputFiles.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
std::vector<BinaryFile *> elf::BinaryFiles;
42
std::vector<BitcodeFile *> elf::BitcodeFiles;
43
std::vector<InputFile *> elf::ObjectFiles;
44
std::vector<InputFile *> elf::SharedFiles;
45
46
TarWriter *elf::Tar;
47
48
2.86k
InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
49
50
3.31k
Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
51
3.31k
  // The --chroot option changes our virtual root directory.
52
3.31k
  // This is useful when you are dealing with files created by --reproduce.
53
3.31k
  if (!Config->Chroot.empty() && 
Path.startswith("/")3
)
54
3
    Path = Saver.save(Config->Chroot + Path);
55
3.31k
56
3.31k
  log(Path);
57
3.31k
58
3.31k
  auto MBOrErr = MemoryBuffer::getFile(Path);
59
3.31k
  if (auto EC = MBOrErr.getError()) {
60
88
    error("cannot open " + Path + ": " + EC.message());
61
88
    return None;
62
88
  }
63
3.23k
64
3.23k
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
65
3.23k
  MemoryBufferRef MBRef = MB->getMemBufferRef();
66
3.23k
  make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
67
3.23k
68
3.23k
  if (Tar)
69
16
    Tar->append(relativeToRoot(Path), MBRef.getBuffer());
70
3.23k
  return MBRef;
71
3.23k
}
72
73
// Concatenates arguments to construct a string representing an error location.
74
22
static std::string createFileLineMsg(StringRef Path, unsigned Line) {
75
22
  std::string Filename = path::filename(Path);
76
22
  std::string Lineno = ":" + std::to_string(Line);
77
22
  if (Filename == Path)
78
14
    return Filename + Lineno;
79
8
  return Filename + Lineno + " (" + Path.str() + Lineno + ")";
80
8
}
81
82
template <class ELFT>
83
static std::string getSrcMsgAux(ObjFile<ELFT> &File, const Symbol &Sym,
84
156
                                InputSectionBase &Sec, uint64_t Offset) {
85
156
  // In DWARF, functions and variables are stored to different places.
86
156
  // First, lookup a function for a given offset.
87
156
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
88
10
    return createFileLineMsg(Info->FileName, Info->Line);
89
146
90
146
  // If it failed, lookup again as a variable.
91
146
  if (Optional<std::pair<std::string, unsigned>> FileLine =
92
12
          File.getVariableLoc(Sym.getName()))
93
12
    return createFileLineMsg(FileLine->first, FileLine->second);
94
134
95
134
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
96
134
  return File.SourceFile;
97
134
}
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
84
8
                                InputSectionBase &Sec, uint64_t Offset) {
85
8
  // In DWARF, functions and variables are stored to different places.
86
8
  // First, lookup a function for a given offset.
87
8
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
88
2
    return createFileLineMsg(Info->FileName, Info->Line);
89
6
90
6
  // If it failed, lookup again as a variable.
91
6
  if (Optional<std::pair<std::string, unsigned>> FileLine =
92
0
          File.getVariableLoc(Sym.getName()))
93
0
    return createFileLineMsg(FileLine->first, FileLine->second);
94
6
95
6
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
96
6
  return File.SourceFile;
97
6
}
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
84
148
                                InputSectionBase &Sec, uint64_t Offset) {
85
148
  // In DWARF, functions and variables are stored to different places.
86
148
  // First, lookup a function for a given offset.
87
148
  if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
88
8
    return createFileLineMsg(Info->FileName, Info->Line);
89
140
90
140
  // If it failed, lookup again as a variable.
91
140
  if (Optional<std::pair<std::string, unsigned>> FileLine =
92
12
          File.getVariableLoc(Sym.getName()))
93
12
    return createFileLineMsg(FileLine->first, FileLine->second);
94
128
95
128
  // File.SourceFile contains STT_FILE symbol, and that is a last resort.
96
128
  return File.SourceFile;
97
128
}
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)
98
99
std::string InputFile::getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
100
156
                                 uint64_t Offset) {
101
156
  if (kind() != ObjKind)
102
0
    return "";
103
156
  switch (Config->EKind) {
104
156
  default:
105
0
    llvm_unreachable("Invalid kind");
106
156
  case ELF32LEKind:
107
8
    return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), Sym, Sec, Offset);
108
156
  case ELF32BEKind:
109
0
    return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), Sym, Sec, Offset);
110
156
  case ELF64LEKind:
111
148
    return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), Sym, Sec, Offset);
112
156
  case ELF64BEKind:
113
0
    return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), Sym, Sec, Offset);
114
0
  }
115
0
}
116
117
145
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
118
145
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
119
145
  const DWARFObject &Obj = Dwarf.getDWARFObj();
120
145
  DwarfLine.reset(new DWARFDebugLine);
121
145
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
122
145
                              Config->Wordsize);
123
145
124
145
  // The second parameter is offset in .debug_line section
125
145
  // for compilation unit (CU) of interest. We have only one
126
145
  // CU (object file), so offset is always 0.
127
145
  // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
128
145
  // needs it in order to find indirect strings.
129
145
  const DWARFDebugLine::LineTable *LT =
130
145
      DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
131
145
132
145
  // Return if there is no debug information about CU available.
133
145
  if (!Dwarf.getNumCompileUnits())
134
139
    return;
135
6
136
6
  // Loop over variable records and insert them to VariableLoc.
137
6
  DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
138
30
  for (const auto &Entry : CU->dies()) {
139
30
    DWARFDie Die(CU, &Entry);
140
30
    // Skip all tags that are not variables.
141
30
    if (Die.getTag() != dwarf::DW_TAG_variable)
142
18
      continue;
143
12
144
12
    // Skip if a local variable because we don't need them for generating error
145
12
    // messages. In general, only non-local symbols can fail to be linked.
146
12
    if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
147
0
      continue;
148
12
149
12
    // Get the source filename index for the variable.
150
12
    unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
151
12
    if (!LT->hasFileAtIndex(File))
152
0
      continue;
153
12
154
12
    // Get the line number on which the variable is declared.
155
12
    unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
156
12
157
12
    // Get the name of the variable and add the collected information to
158
12
    // VariableLoc. Usually Name is non-empty, but it can be empty if the input
159
12
    // object file lacks some debug info.
160
12
    StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
161
12
    if (!Name.empty())
162
12
      VariableLoc.insert({Name, {File, Line}});
163
12
  }
164
6
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeDwarf()
Line
Count
Source
117
11
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
118
11
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
119
11
  const DWARFObject &Obj = Dwarf.getDWARFObj();
120
11
  DwarfLine.reset(new DWARFDebugLine);
121
11
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
122
11
                              Config->Wordsize);
123
11
124
11
  // The second parameter is offset in .debug_line section
125
11
  // for compilation unit (CU) of interest. We have only one
126
11
  // CU (object file), so offset is always 0.
127
11
  // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
128
11
  // needs it in order to find indirect strings.
129
11
  const DWARFDebugLine::LineTable *LT =
130
11
      DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
131
11
132
11
  // Return if there is no debug information about CU available.
133
11
  if (!Dwarf.getNumCompileUnits())
134
11
    return;
135
0
136
0
  // Loop over variable records and insert them to VariableLoc.
137
0
  DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
138
0
  for (const auto &Entry : CU->dies()) {
139
0
    DWARFDie Die(CU, &Entry);
140
0
    // Skip all tags that are not variables.
141
0
    if (Die.getTag() != dwarf::DW_TAG_variable)
142
0
      continue;
143
0
144
0
    // Skip if a local variable because we don't need them for generating error
145
0
    // messages. In general, only non-local symbols can fail to be linked.
146
0
    if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
147
0
      continue;
148
0
149
0
    // Get the source filename index for the variable.
150
0
    unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
151
0
    if (!LT->hasFileAtIndex(File))
152
0
      continue;
153
0
154
0
    // Get the line number on which the variable is declared.
155
0
    unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
156
0
157
0
    // Get the name of the variable and add the collected information to
158
0
    // VariableLoc. Usually Name is non-empty, but it can be empty if the input
159
0
    // object file lacks some debug info.
160
0
    StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
161
0
    if (!Name.empty())
162
0
      VariableLoc.insert({Name, {File, Line}});
163
0
  }
164
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeDwarf()
Line
Count
Source
117
1
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
118
1
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
119
1
  const DWARFObject &Obj = Dwarf.getDWARFObj();
120
1
  DwarfLine.reset(new DWARFDebugLine);
121
1
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
122
1
                              Config->Wordsize);
123
1
124
1
  // The second parameter is offset in .debug_line section
125
1
  // for compilation unit (CU) of interest. We have only one
126
1
  // CU (object file), so offset is always 0.
127
1
  // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
128
1
  // needs it in order to find indirect strings.
129
1
  const DWARFDebugLine::LineTable *LT =
130
1
      DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
131
1
132
1
  // Return if there is no debug information about CU available.
133
1
  if (!Dwarf.getNumCompileUnits())
134
1
    return;
135
0
136
0
  // Loop over variable records and insert them to VariableLoc.
137
0
  DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
138
0
  for (const auto &Entry : CU->dies()) {
139
0
    DWARFDie Die(CU, &Entry);
140
0
    // Skip all tags that are not variables.
141
0
    if (Die.getTag() != dwarf::DW_TAG_variable)
142
0
      continue;
143
0
144
0
    // Skip if a local variable because we don't need them for generating error
145
0
    // messages. In general, only non-local symbols can fail to be linked.
146
0
    if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
147
0
      continue;
148
0
149
0
    // Get the source filename index for the variable.
150
0
    unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
151
0
    if (!LT->hasFileAtIndex(File))
152
0
      continue;
153
0
154
0
    // Get the line number on which the variable is declared.
155
0
    unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
156
0
157
0
    // Get the name of the variable and add the collected information to
158
0
    // VariableLoc. Usually Name is non-empty, but it can be empty if the input
159
0
    // object file lacks some debug info.
160
0
    StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
161
0
    if (!Name.empty())
162
0
      VariableLoc.insert({Name, {File, Line}});
163
0
  }
164
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeDwarf()
Line
Count
Source
117
131
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
118
131
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
119
131
  const DWARFObject &Obj = Dwarf.getDWARFObj();
120
131
  DwarfLine.reset(new DWARFDebugLine);
121
131
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
122
131
                              Config->Wordsize);
123
131
124
131
  // The second parameter is offset in .debug_line section
125
131
  // for compilation unit (CU) of interest. We have only one
126
131
  // CU (object file), so offset is always 0.
127
131
  // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
128
131
  // needs it in order to find indirect strings.
129
131
  const DWARFDebugLine::LineTable *LT =
130
131
      DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
131
131
132
131
  // Return if there is no debug information about CU available.
133
131
  if (!Dwarf.getNumCompileUnits())
134
125
    return;
135
6
136
6
  // Loop over variable records and insert them to VariableLoc.
137
6
  DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
138
30
  for (const auto &Entry : CU->dies()) {
139
30
    DWARFDie Die(CU, &Entry);
140
30
    // Skip all tags that are not variables.
141
30
    if (Die.getTag() != dwarf::DW_TAG_variable)
142
18
      continue;
143
12
144
12
    // Skip if a local variable because we don't need them for generating error
145
12
    // messages. In general, only non-local symbols can fail to be linked.
146
12
    if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
147
0
      continue;
148
12
149
12
    // Get the source filename index for the variable.
150
12
    unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
151
12
    if (!LT->hasFileAtIndex(File))
152
0
      continue;
153
12
154
12
    // Get the line number on which the variable is declared.
155
12
    unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
156
12
157
12
    // Get the name of the variable and add the collected information to
158
12
    // VariableLoc. Usually Name is non-empty, but it can be empty if the input
159
12
    // object file lacks some debug info.
160
12
    StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
161
12
    if (!Name.empty())
162
12
      VariableLoc.insert({Name, {File, Line}});
163
12
  }
164
6
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeDwarf()
Line
Count
Source
117
2
template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
118
2
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
119
2
  const DWARFObject &Obj = Dwarf.getDWARFObj();
120
2
  DwarfLine.reset(new DWARFDebugLine);
121
2
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
122
2
                              Config->Wordsize);
123
2
124
2
  // The second parameter is offset in .debug_line section
125
2
  // for compilation unit (CU) of interest. We have only one
126
2
  // CU (object file), so offset is always 0.
127
2
  // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
128
2
  // needs it in order to find indirect strings.
129
2
  const DWARFDebugLine::LineTable *LT =
130
2
      DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
131
2
132
2
  // Return if there is no debug information about CU available.
133
2
  if (!Dwarf.getNumCompileUnits())
134
2
    return;
135
0
136
0
  // Loop over variable records and insert them to VariableLoc.
137
0
  DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
138
0
  for (const auto &Entry : CU->dies()) {
139
0
    DWARFDie Die(CU, &Entry);
140
0
    // Skip all tags that are not variables.
141
0
    if (Die.getTag() != dwarf::DW_TAG_variable)
142
0
      continue;
143
0
144
0
    // Skip if a local variable because we don't need them for generating error
145
0
    // messages. In general, only non-local symbols can fail to be linked.
146
0
    if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
147
0
      continue;
148
0
149
0
    // Get the source filename index for the variable.
150
0
    unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
151
0
    if (!LT->hasFileAtIndex(File))
152
0
      continue;
153
0
154
0
    // Get the line number on which the variable is declared.
155
0
    unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
156
0
157
0
    // Get the name of the variable and add the collected information to
158
0
    // VariableLoc. Usually Name is non-empty, but it can be empty if the input
159
0
    // object file lacks some debug info.
160
0
    StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
161
0
    if (!Name.empty())
162
0
      VariableLoc.insert({Name, {File, Line}});
163
0
  }
164
0
}
165
166
// Returns the pair of file name and line number describing location of data
167
// object (variable, array, etc) definition.
168
template <class ELFT>
169
Optional<std::pair<std::string, unsigned>>
170
146
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
171
146
  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
172
146
173
146
  // There is always only one CU so it's offset is 0.
174
146
  const DWARFDebugLine::LineTable *LT = DwarfLine->getLineTable(0);
175
146
  if (!LT)
176
0
    return None;
177
146
178
146
  // Return if we have no debug information about data object.
179
146
  auto It = VariableLoc.find(Name);
180
146
  if (It == VariableLoc.end())
181
134
    return None;
182
12
183
12
  // Take file name string from line table.
184
12
  std::string FileName;
185
12
  if (!LT->getFileNameByIndex(
186
12
          It->second.first /* File */, nullptr,
187
12
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
188
0
    return None;
189
12
190
12
  return std::make_pair(FileName, It->second.second /*Line*/);
191
12
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getVariableLoc(llvm::StringRef)
Line
Count
Source
170
6
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
171
6
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
172
6
173
6
  // There is always only one CU so it's offset is 0.
174
6
  const DWARFDebugLine::LineTable *LT = DwarfLine->getLineTable(0);
175
6
  if (!LT)
176
0
    return None;
177
6
178
6
  // Return if we have no debug information about data object.
179
6
  auto It = VariableLoc.find(Name);
180
6
  if (It == VariableLoc.end())
181
6
    return None;
182
0
183
0
  // Take file name string from line table.
184
0
  std::string FileName;
185
0
  if (!LT->getFileNameByIndex(
186
0
          It->second.first /* File */, nullptr,
187
0
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
188
0
    return None;
189
0
190
0
  return std::make_pair(FileName, It->second.second /*Line*/);
191
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
170
140
ObjFile<ELFT>::getVariableLoc(StringRef Name) {
171
140
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
172
140
173
140
  // There is always only one CU so it's offset is 0.
174
140
  const DWARFDebugLine::LineTable *LT = DwarfLine->getLineTable(0);
175
140
  if (!LT)
176
0
    return None;
177
140
178
140
  // Return if we have no debug information about data object.
179
140
  auto It = VariableLoc.find(Name);
180
140
  if (It == VariableLoc.end())
181
128
    return None;
182
12
183
12
  // Take file name string from line table.
184
12
  std::string FileName;
185
12
  if (!LT->getFileNameByIndex(
186
12
          It->second.first /* File */, nullptr,
187
12
          DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
188
0
    return None;
189
12
190
12
  return std::make_pair(FileName, It->second.second /*Line*/);
191
12
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getVariableLoc(llvm::StringRef)
192
193
// Returns source line information for a given offset
194
// using DWARF debug info.
195
template <class ELFT>
196
Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
197
1.99k
                                                  uint64_t Offset) {
198
1.99k
  llvm::call_once(InitDwarfLine, [this]() 
{ initializeDwarf(); }145
);
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
198
11
  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
198
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
198
131
  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
198
2
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
199
1.99k
200
1.99k
  // The offset to CU is 0.
201
1.99k
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
202
1.99k
  if (!Tbl)
203
0
    return None;
204
1.99k
205
1.99k
  // Use fake address calcuated by adding section file offset and offset in
206
1.99k
  // section. See comments for ObjectInfo class.
207
1.99k
  DILineInfo Info;
208
1.99k
  Tbl->getFileLineInfoForAddress(
209
1.99k
      S->getOffsetInFile() + Offset, nullptr,
210
1.99k
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
211
1.99k
  if (Info.Line == 0)
212
1.98k
    return None;
213
10
  return Info;
214
10
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
197
12
                                                  uint64_t Offset) {
198
12
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
199
12
200
12
  // The offset to CU is 0.
201
12
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
202
12
  if (!Tbl)
203
0
    return None;
204
12
205
12
  // Use fake address calcuated by adding section file offset and offset in
206
12
  // section. See comments for ObjectInfo class.
207
12
  DILineInfo Info;
208
12
  Tbl->getFileLineInfoForAddress(
209
12
      S->getOffsetInFile() + Offset, nullptr,
210
12
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
211
12
  if (Info.Line == 0)
212
10
    return None;
213
2
  return Info;
214
2
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
197
1
                                                  uint64_t Offset) {
198
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
199
1
200
1
  // The offset to CU is 0.
201
1
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
202
1
  if (!Tbl)
203
0
    return None;
204
1
205
1
  // Use fake address calcuated by adding section file offset and offset in
206
1
  // section. See comments for ObjectInfo class.
207
1
  DILineInfo Info;
208
1
  Tbl->getFileLineInfoForAddress(
209
1
      S->getOffsetInFile() + Offset, nullptr,
210
1
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
211
1
  if (Info.Line == 0)
212
1
    return None;
213
0
  return Info;
214
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
197
173
                                                  uint64_t Offset) {
198
173
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
199
173
200
173
  // The offset to CU is 0.
201
173
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
202
173
  if (!Tbl)
203
0
    return None;
204
173
205
173
  // Use fake address calcuated by adding section file offset and offset in
206
173
  // section. See comments for ObjectInfo class.
207
173
  DILineInfo Info;
208
173
  Tbl->getFileLineInfoForAddress(
209
173
      S->getOffsetInFile() + Offset, nullptr,
210
173
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
211
173
  if (Info.Line == 0)
212
165
    return None;
213
8
  return Info;
214
8
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
197
1.81k
                                                  uint64_t Offset) {
198
1.81k
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
199
1.81k
200
1.81k
  // The offset to CU is 0.
201
1.81k
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
202
1.81k
  if (!Tbl)
203
0
    return None;
204
1.81k
205
1.81k
  // Use fake address calcuated by adding section file offset and offset in
206
1.81k
  // section. See comments for ObjectInfo class.
207
1.81k
  DILineInfo Info;
208
1.81k
  Tbl->getFileLineInfoForAddress(
209
1.81k
      S->getOffsetInFile() + Offset, nullptr,
210
1.81k
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
211
1.81k
  if (Info.Line == 0)
212
1.81k
    return None;
213
0
  return Info;
214
0
}
215
216
// Returns source line information for a given offset
217
// using DWARF debug info.
218
template <class ELFT>
219
1.84k
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
220
1.84k
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
221
0
    return Info->FileName + ":" + std::to_string(Info->Line);
222
1.84k
  return "";
223
1.84k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
219
4
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
220
4
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
221
0
    return Info->FileName + ":" + std::to_string(Info->Line);
222
4
  return "";
223
4
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
219
1
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
220
1
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
221
0
    return Info->FileName + ":" + std::to_string(Info->Line);
222
1
  return "";
223
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
219
25
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
220
25
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
221
0
    return Info->FileName + ":" + std::to_string(Info->Line);
222
25
  return "";
223
25
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
219
1.81k
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
220
1.81k
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
221
0
    return Info->FileName + ":" + std::to_string(Info->Line);
222
1.81k
  return "";
223
1.81k
}
224
225
// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
226
2.37k
std::string lld::toString(const InputFile *F) {
227
2.37k
  if (!F)
228
116
    return "<internal>";
229
2.25k
230
2.25k
  if (F->ToStringCache.empty()) {
231
353
    if (F->ArchiveName.empty())
232
343
      F->ToStringCache = F->getName();
233
10
    else
234
10
      F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
235
353
  }
236
2.25k
  return F->ToStringCache;
237
2.25k
}
238
239
template <class ELFT>
240
2.59k
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
241
2.59k
  if (ELFT::TargetEndianness == support::little)
242
2.36k
    EKind = ELFT::Is64Bits ? 
ELF64LEKind2.04k
:
ELF32LEKind322
;
243
230
  else
244
230
    EKind = ELFT::Is64Bits ? 
ELF64BEKind70
:
ELF32BEKind160
;
245
2.59k
246
2.59k
  EMachine = getObj().getHeader()->e_machine;
247
2.59k
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
248
2.59k
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
240
322
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
241
322
  if (ELFT::TargetEndianness == support::little)
242
322
    EKind = ELFT::Is64Bits ? 
ELF64LEKind0
: ELF32LEKind;
243
0
  else
244
0
    EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
245
322
246
322
  EMachine = getObj().getHeader()->e_machine;
247
322
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
248
322
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
240
160
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
241
160
  if (ELFT::TargetEndianness == support::little)
242
0
    EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
243
160
  else
244
160
    EKind = ELFT::Is64Bits ? 
ELF64BEKind0
: ELF32BEKind;
245
160
246
160
  EMachine = getObj().getHeader()->e_machine;
247
160
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
248
160
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
240
2.04k
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
241
2.04k
  if (ELFT::TargetEndianness == support::little)
242
2.04k
    EKind = ELFT::Is64Bits ? ELF64LEKind : 
ELF32LEKind0
;
243
0
  else
244
0
    EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
245
2.04k
246
2.04k
  EMachine = getObj().getHeader()->e_machine;
247
2.04k
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
248
2.04k
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
240
70
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
241
70
  if (ELFT::TargetEndianness == support::little)
242
0
    EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
243
70
  else
244
70
    EKind = ELFT::Is64Bits ? ELF64BEKind : 
ELF32BEKind0
;
245
70
246
70
  EMachine = getObj().getHeader()->e_machine;
247
70
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
248
70
}
249
250
template <class ELFT>
251
319
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
252
319
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
253
319
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getGlobalELFSyms()
Line
Count
Source
251
54
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
252
54
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
253
54
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getGlobalELFSyms()
Line
Count
Source
251
24
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
252
24
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
253
24
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getGlobalELFSyms()
Line
Count
Source
251
230
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
252
230
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
253
230
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getGlobalELFSyms()
Line
Count
Source
251
11
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
252
11
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
253
11
}
254
255
template <class ELFT>
256
18.5k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
257
18.5k
  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
258
18.5k
}
lld::elf::ELFFileBase<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
256
1.91k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
257
1.91k
  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
258
1.91k
}
lld::elf::ELFFileBase<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
256
612
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
257
612
  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
258
612
}
lld::elf::ELFFileBase<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
256
5.76k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
257
5.76k
  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
258
5.76k
}
lld::elf::ELFFileBase<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
256
10.2k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
257
10.2k
  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
258
10.2k
}
259
260
template <class ELFT>
261
void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
262
2.51k
                                   const Elf_Shdr *Symtab) {
263
2.51k
  FirstNonLocal = Symtab->sh_info;
264
2.51k
  ELFSyms = CHECK(getObj().symbols(Symtab), this);
265
2.51k
  if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
266
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
267
2.51k
268
2.51k
  StringTable =
269
2.51k
      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
270
2.51k
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initSymtab(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*)
Line
Count
Source
262
318
                                   const Elf_Shdr *Symtab) {
263
318
  FirstNonLocal = Symtab->sh_info;
264
318
  ELFSyms = CHECK(getObj().symbols(Symtab), this);
265
318
  if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
266
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
267
318
268
318
  StringTable =
269
318
      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
270
318
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initSymtab(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*)
Line
Count
Source
262
157
                                   const Elf_Shdr *Symtab) {
263
157
  FirstNonLocal = Symtab->sh_info;
264
157
  ELFSyms = CHECK(getObj().symbols(Symtab), this);
265
157
  if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
266
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
267
157
268
157
  StringTable =
269
157
      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
270
157
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initSymtab(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
262
1.96k
                                   const Elf_Shdr *Symtab) {
263
1.96k
  FirstNonLocal = Symtab->sh_info;
264
1.96k
  ELFSyms = CHECK(getObj().symbols(Symtab), this);
265
1.96k
  if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
266
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
267
1.96k
268
1.96k
  StringTable =
269
1.96k
      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
270
1.96k
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initSymtab(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*)
Line
Count
Source
262
70
                                   const Elf_Shdr *Symtab) {
263
70
  FirstNonLocal = Symtab->sh_info;
264
70
  ELFSyms = CHECK(getObj().symbols(Symtab), this);
265
70
  if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
266
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
267
70
268
70
  StringTable =
269
70
      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
270
70
}
271
272
template <class ELFT>
273
ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
274
2.31k
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
275
2.31k
  this->ArchiveName = ArchiveName;
276
2.31k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
274
270
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
275
270
  this->ArchiveName = ArchiveName;
276
270
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
274
141
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
275
141
  this->ArchiveName = ArchiveName;
276
141
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
274
1.84k
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
275
1.84k
  this->ArchiveName = ArchiveName;
276
1.84k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
274
59
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
275
59
  this->ArchiveName = ArchiveName;
276
59
}
277
278
2.15k
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
279
2.15k
  if (this->Symbols.empty())
280
1
    return {};
281
2.14k
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
282
2.14k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getLocalSymbols()
Line
Count
Source
278
262
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
279
262
  if (this->Symbols.empty())
280
0
    return {};
281
262
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
282
262
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getLocalSymbols()
Line
Count
Source
278
136
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
279
136
  if (this->Symbols.empty())
280
0
    return {};
281
136
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
282
136
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getLocalSymbols()
Line
Count
Source
278
1.69k
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
279
1.69k
  if (this->Symbols.empty())
280
1
    return {};
281
1.69k
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
282
1.69k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getLocalSymbols()
Line
Count
Source
278
57
template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
279
57
  if (this->Symbols.empty())
280
0
    return {};
281
57
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
282
57
}
283
284
template <class ELFT>
285
2.19k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
286
2.19k
  // Read section and symbol tables.
287
2.19k
  initializeSections(ComdatGroups);
288
2.19k
  initializeSymbols();
289
2.19k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
285
267
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
286
267
  // Read section and symbol tables.
287
267
  initializeSections(ComdatGroups);
288
267
  initializeSymbols();
289
267
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
285
138
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
286
138
  // Read section and symbol tables.
287
138
  initializeSections(ComdatGroups);
288
138
  initializeSymbols();
289
138
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
285
1.73k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
286
1.73k
  // Read section and symbol tables.
287
1.73k
  initializeSections(ComdatGroups);
288
1.73k
  initializeSymbols();
289
1.73k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
285
59
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
286
59
  // Read section and symbol tables.
287
59
  initializeSections(ComdatGroups);
288
59
  initializeSymbols();
289
59
}
290
291
// Sections with SHT_GROUP and comdat bits define comdat section groups.
292
// They are identified and deduplicated by group name. This function
293
// returns a group name.
294
template <class ELFT>
295
StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
296
48
                                              const Elf_Shdr &Sec) {
297
48
  // Group signatures are stored as symbol names in object files.
298
48
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
299
48
  if (this->ELFSyms.empty())
300
41
    this->initSymtab(
301
41
        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
302
48
303
48
  const Elf_Sym *Sym =
304
48
      CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
305
48
  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
306
48
307
48
  // As a special case, if a symbol is a section symbol and has no name,
308
48
  // we use a section name as a signature.
309
48
  //
310
48
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
311
48
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
312
48
  // older produce such sections as outputs for the -r option, so we need
313
48
  // a bug-compatibility.
314
48
  if (Signature.empty() && 
Sym->getType() == STT_SECTION4
)
315
4
    return getSectionName(Sec);
316
44
  return Signature;
317
44
}
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&)
Line
Count
Source
296
2
                                              const Elf_Shdr &Sec) {
297
2
  // Group signatures are stored as symbol names in object files.
298
2
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
299
2
  if (this->ELFSyms.empty())
300
1
    this->initSymtab(
301
1
        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
302
2
303
2
  const Elf_Sym *Sym =
304
2
      CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
305
2
  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
306
2
307
2
  // As a special case, if a symbol is a section symbol and has no name,
308
2
  // we use a section name as a signature.
309
2
  //
310
2
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
311
2
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
312
2
  // older produce such sections as outputs for the -r option, so we need
313
2
  // a bug-compatibility.
314
2
  if (Signature.empty() && 
Sym->getType() == STT_SECTION0
)
315
0
    return getSectionName(Sec);
316
2
  return Signature;
317
2
}
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
296
46
                                              const Elf_Shdr &Sec) {
297
46
  // Group signatures are stored as symbol names in object files.
298
46
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
299
46
  if (this->ELFSyms.empty())
300
40
    this->initSymtab(
301
40
        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
302
46
303
46
  const Elf_Sym *Sym =
304
46
      CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
305
46
  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
306
46
307
46
  // As a special case, if a symbol is a section symbol and has no name,
308
46
  // we use a section name as a signature.
309
46
  //
310
46
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
311
46
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
312
46
  // older produce such sections as outputs for the -r option, so we need
313
46
  // a bug-compatibility.
314
46
  if (Signature.empty() && 
Sym->getType() == STT_SECTION4
)
315
4
    return getSectionName(Sec);
316
42
  return Signature;
317
42
}
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&)
318
319
template <class ELFT>
320
ArrayRef<typename ObjFile<ELFT>::Elf_Word>
321
17
ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
322
17
  const ELFFile<ELFT> &Obj = this->getObj();
323
17
  ArrayRef<Elf_Word> Entries =
324
17
      CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
325
17
  if (Entries.empty() || Entries[0] != GRP_COMDAT)
326
0
    fatal(toString(this) + ": unsupported SHT_GROUP format");
327
17
  return Entries.slice(1);
328
17
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getShtGroupEntries(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> >::getShtGroupEntries(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> >::getShtGroupEntries(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
321
17
ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
322
17
  const ELFFile<ELFT> &Obj = this->getObj();
323
17
  ArrayRef<Elf_Word> Entries =
324
17
      CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
325
17
  if (Entries.empty() || Entries[0] != GRP_COMDAT)
326
0
    fatal(toString(this) + ": unsupported SHT_GROUP format");
327
17
  return Entries.slice(1);
328
17
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getShtGroupEntries(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
329
330
201k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
331
201k
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
332
201k
  // the linker significantly faster, although the output will be bigger.
333
201k
  if (Config->Optimize == 0)
334
15
    return false;
335
201k
336
201k
  // A mergeable section with size 0 is useless because they don't have
337
201k
  // any data to merge. A mergeable string section with size 0 can be
338
201k
  // argued as invalid because it doesn't end with a null character.
339
201k
  // We'll avoid a mess by handling them as if they were non-mergeable.
340
201k
  if (Sec.sh_size == 0)
341
197k
    return false;
342
3.93k
343
3.93k
  // Check for sh_entsize. The ELF spec is not clear about the zero
344
3.93k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
345
3.93k
  // the section does not hold a table of fixed-size entries". We know
346
3.93k
  // that Rust 1.13 produces a string mergeable section with a zero
347
3.93k
  // sh_entsize. Here we just accept it rather than being picky about it.
348
3.93k
  uint64_t EntSize = Sec.sh_entsize;
349
3.93k
  if (EntSize == 0)
350
3.45k
    return false;
351
483
  if (Sec.sh_size % EntSize)
352
0
    fatal(toString(this) +
353
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
354
483
355
483
  uint64_t Flags = Sec.sh_flags;
356
483
  if (!(Flags & SHF_MERGE))
357
393
    return false;
358
90
  if (Flags & SHF_WRITE)
359
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
360
90
361
90
  return true;
362
90
}
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
330
939
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
331
939
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
332
939
  // the linker significantly faster, although the output will be bigger.
333
939
  if (Config->Optimize == 0)
334
0
    return false;
335
939
336
939
  // A mergeable section with size 0 is useless because they don't have
337
939
  // any data to merge. A mergeable string section with size 0 can be
338
939
  // argued as invalid because it doesn't end with a null character.
339
939
  // We'll avoid a mess by handling them as if they were non-mergeable.
340
939
  if (Sec.sh_size == 0)
341
150
    return false;
342
789
343
789
  // Check for sh_entsize. The ELF spec is not clear about the zero
344
789
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
345
789
  // the section does not hold a table of fixed-size entries". We know
346
789
  // that Rust 1.13 produces a string mergeable section with a zero
347
789
  // sh_entsize. Here we just accept it rather than being picky about it.
348
789
  uint64_t EntSize = Sec.sh_entsize;
349
789
  if (EntSize == 0)
350
752
    return false;
351
37
  if (Sec.sh_size % EntSize)
352
0
    fatal(toString(this) +
353
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
354
37
355
37
  uint64_t Flags = Sec.sh_flags;
356
37
  if (!(Flags & SHF_MERGE))
357
32
    return false;
358
5
  if (Flags & SHF_WRITE)
359
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
360
5
361
5
  return true;
362
5
}
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
330
736
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
331
736
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
332
736
  // the linker significantly faster, although the output will be bigger.
333
736
  if (Config->Optimize == 0)
334
0
    return false;
335
736
336
736
  // A mergeable section with size 0 is useless because they don't have
337
736
  // any data to merge. A mergeable string section with size 0 can be
338
736
  // argued as invalid because it doesn't end with a null character.
339
736
  // We'll avoid a mess by handling them as if they were non-mergeable.
340
736
  if (Sec.sh_size == 0)
341
253
    return false;
342
483
343
483
  // Check for sh_entsize. The ELF spec is not clear about the zero
344
483
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
345
483
  // the section does not hold a table of fixed-size entries". We know
346
483
  // that Rust 1.13 produces a string mergeable section with a zero
347
483
  // sh_entsize. Here we just accept it rather than being picky about it.
348
483
  uint64_t EntSize = Sec.sh_entsize;
349
483
  if (EntSize == 0)
350
210
    return false;
351
273
  if (Sec.sh_size % EntSize)
352
0
    fatal(toString(this) +
353
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
354
273
355
273
  uint64_t Flags = Sec.sh_flags;
356
273
  if (!(Flags & SHF_MERGE))
357
272
    return false;
358
1
  if (Flags & SHF_WRITE)
359
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
360
1
361
1
  return true;
362
1
}
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
330
199k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
331
199k
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
332
199k
  // the linker significantly faster, although the output will be bigger.
333
199k
  if (Config->Optimize == 0)
334
15
    return false;
335
199k
336
199k
  // A mergeable section with size 0 is useless because they don't have
337
199k
  // any data to merge. A mergeable string section with size 0 can be
338
199k
  // argued as invalid because it doesn't end with a null character.
339
199k
  // We'll avoid a mess by handling them as if they were non-mergeable.
340
199k
  if (Sec.sh_size == 0)
341
196k
    return false;
342
2.48k
343
2.48k
  // Check for sh_entsize. The ELF spec is not clear about the zero
344
2.48k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
345
2.48k
  // the section does not hold a table of fixed-size entries". We know
346
2.48k
  // that Rust 1.13 produces a string mergeable section with a zero
347
2.48k
  // sh_entsize. Here we just accept it rather than being picky about it.
348
2.48k
  uint64_t EntSize = Sec.sh_entsize;
349
2.48k
  if (EntSize == 0)
350
2.39k
    return false;
351
96
  if (Sec.sh_size % EntSize)
352
0
    fatal(toString(this) +
353
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
354
96
355
96
  uint64_t Flags = Sec.sh_flags;
356
96
  if (!(Flags & SHF_MERGE))
357
12
    return false;
358
84
  if (Flags & SHF_WRITE)
359
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
360
84
361
84
  return true;
362
84
}
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
330
263
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
331
263
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
332
263
  // the linker significantly faster, although the output will be bigger.
333
263
  if (Config->Optimize == 0)
334
0
    return false;
335
263
336
263
  // A mergeable section with size 0 is useless because they don't have
337
263
  // any data to merge. A mergeable string section with size 0 can be
338
263
  // argued as invalid because it doesn't end with a null character.
339
263
  // We'll avoid a mess by handling them as if they were non-mergeable.
340
263
  if (Sec.sh_size == 0)
341
86
    return false;
342
177
343
177
  // Check for sh_entsize. The ELF spec is not clear about the zero
344
177
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
345
177
  // the section does not hold a table of fixed-size entries". We know
346
177
  // that Rust 1.13 produces a string mergeable section with a zero
347
177
  // sh_entsize. Here we just accept it rather than being picky about it.
348
177
  uint64_t EntSize = Sec.sh_entsize;
349
177
  if (EntSize == 0)
350
100
    return false;
351
77
  if (Sec.sh_size % EntSize)
352
0
    fatal(toString(this) +
353
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
354
77
355
77
  uint64_t Flags = Sec.sh_flags;
356
77
  if (!(Flags & SHF_MERGE))
357
77
    return false;
358
0
  if (Flags & SHF_WRITE)
359
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
360
0
361
0
  return true;
362
0
}
363
364
template <class ELFT>
365
void ObjFile<ELFT>::initializeSections(
366
2.19k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
367
2.19k
  const ELFFile<ELFT> &Obj = this->getObj();
368
2.19k
369
2.19k
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
370
2.19k
  uint64_t Size = ObjSections.size();
371
2.19k
  this->Sections.resize(Size);
372
2.19k
  this->SectionStringTable =
373
2.19k
      CHECK(Obj.getSectionStringTable(ObjSections), this);
374
2.19k
375
211k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++209k
) {
376
209k
    if (this->Sections[I] == &InputSection::Discarded)
377
18
      continue;
378
209k
    const Elf_Shdr &Sec = ObjSections[I];
379
209k
380
209k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
381
209k
    // if -r is given, we'll let the final link discard such sections.
382
209k
    // This is compatible with GNU.
383
209k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable2
) {
384
1
      this->Sections[I] = &InputSection::Discarded;
385
1
      continue;
386
1
    }
387
209k
388
209k
    switch (Sec.sh_type) {
389
209k
    case SHT_GROUP: {
390
48
      // De-duplicate section groups by their signatures.
391
48
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
392
48
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
393
48
      this->Sections[I] = &InputSection::Discarded;
394
48
395
48
      // If it is a new section group, we want to keep group members.
396
48
      // Group leader sections, which contain indices of group members, are
397
48
      // discarded because they are useless beyond this point. The only
398
48
      // exception is the -r option because in order to produce re-linkable
399
48
      // object files, we want to pass through basically everything.
400
48
      if (IsNew) {
401
31
        if (Config->Relocatable)
402
8
          this->Sections[I] = createInputSection(Sec);
403
31
        continue;
404
31
      }
405
17
406
17
      // Otherwise, discard group members.
407
18
      
for (uint32_t SecIndex : getShtGroupEntries(Sec))17
{
408
18
        if (SecIndex >= Size)
409
0
          fatal(toString(this) +
410
0
                ": invalid section index in group: " + Twine(SecIndex));
411
18
        this->Sections[SecIndex] = &InputSection::Discarded;
412
18
      }
413
17
      break;
414
17
    }
415
2.19k
    case SHT_SYMTAB:
416
2.19k
      this->initSymtab(ObjSections, &Sec);
417
2.19k
      break;
418
17
    case SHT_SYMTAB_SHNDX:
419
3
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
420
3
      break;
421
4.44k
    case SHT_STRTAB:
422
4.44k
    case SHT_NULL:
423
4.44k
      break;
424
202k
    default:
425
202k
      this->Sections[I] = createInputSection(Sec);
426
209k
    }
427
209k
428
209k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
429
209k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
430
209k
    
if (209k
Sec.sh_flags & SHF_LINK_ORDER209k
) {
431
81
      if (Sec.sh_link >= this->Sections.size())
432
0
        fatal(toString(this) +
433
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
434
81
      this->Sections[Sec.sh_link]->DependentSections.push_back(
435
81
          cast<InputSection>(this->Sections[I]));
436
81
    }
437
209k
  }
438
2.19k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
366
267
    DenseSet<CachedHashStringRef> &ComdatGroups) {
367
267
  const ELFFile<ELFT> &Obj = this->getObj();
368
267
369
267
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
370
267
  uint64_t Size = ObjSections.size();
371
267
  this->Sections.resize(Size);
372
267
  this->SectionStringTable =
373
267
      CHECK(Obj.getSectionStringTable(ObjSections), this);
374
267
375
2.32k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++2.06k
) {
376
2.06k
    if (this->Sections[I] == &InputSection::Discarded)
377
0
      continue;
378
2.06k
    const Elf_Shdr &Sec = ObjSections[I];
379
2.06k
380
2.06k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
381
2.06k
    // if -r is given, we'll let the final link discard such sections.
382
2.06k
    // This is compatible with GNU.
383
2.06k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
384
0
      this->Sections[I] = &InputSection::Discarded;
385
0
      continue;
386
0
    }
387
2.06k
388
2.06k
    switch (Sec.sh_type) {
389
2.06k
    case SHT_GROUP: {
390
2
      // De-duplicate section groups by their signatures.
391
2
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
392
2
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
393
2
      this->Sections[I] = &InputSection::Discarded;
394
2
395
2
      // If it is a new section group, we want to keep group members.
396
2
      // Group leader sections, which contain indices of group members, are
397
2
      // discarded because they are useless beyond this point. The only
398
2
      // exception is the -r option because in order to produce re-linkable
399
2
      // object files, we want to pass through basically everything.
400
2
      if (IsNew) {
401
2
        if (Config->Relocatable)
402
0
          this->Sections[I] = createInputSection(Sec);
403
2
        continue;
404
2
      }
405
0
406
0
      // Otherwise, discard group members.
407
0
      for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
408
0
        if (SecIndex >= Size)
409
0
          fatal(toString(this) +
410
0
                ": invalid section index in group: " + Twine(SecIndex));
411
0
        this->Sections[SecIndex] = &InputSection::Discarded;
412
0
      }
413
0
      break;
414
0
    }
415
267
    case SHT_SYMTAB:
416
267
      this->initSymtab(ObjSections, &Sec);
417
267
      break;
418
0
    case SHT_SYMTAB_SHNDX:
419
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
420
0
      break;
421
544
    case SHT_STRTAB:
422
544
    case SHT_NULL:
423
544
      break;
424
1.24k
    default:
425
1.24k
      this->Sections[I] = createInputSection(Sec);
426
2.06k
    }
427
2.06k
428
2.06k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
429
2.06k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
430
2.06k
    
if (2.06k
Sec.sh_flags & SHF_LINK_ORDER2.06k
) {
431
68
      if (Sec.sh_link >= this->Sections.size())
432
0
        fatal(toString(this) +
433
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
434
68
      this->Sections[Sec.sh_link]->DependentSections.push_back(
435
68
          cast<InputSection>(this->Sections[I]));
436
68
    }
437
2.06k
  }
438
267
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
366
138
    DenseSet<CachedHashStringRef> &ComdatGroups) {
367
138
  const ELFFile<ELFT> &Obj = this->getObj();
368
138
369
138
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
370
138
  uint64_t Size = ObjSections.size();
371
138
  this->Sections.resize(Size);
372
138
  this->SectionStringTable =
373
138
      CHECK(Obj.getSectionStringTable(ObjSections), this);
374
138
375
1.38k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++1.25k
) {
376
1.25k
    if (this->Sections[I] == &InputSection::Discarded)
377
0
      continue;
378
1.25k
    const Elf_Shdr &Sec = ObjSections[I];
379
1.25k
380
1.25k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
381
1.25k
    // if -r is given, we'll let the final link discard such sections.
382
1.25k
    // This is compatible with GNU.
383
1.25k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
384
0
      this->Sections[I] = &InputSection::Discarded;
385
0
      continue;
386
0
    }
387
1.25k
388
1.25k
    switch (Sec.sh_type) {
389
1.25k
    case SHT_GROUP: {
390
0
      // De-duplicate section groups by their signatures.
391
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
392
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
393
0
      this->Sections[I] = &InputSection::Discarded;
394
0
395
0
      // If it is a new section group, we want to keep group members.
396
0
      // Group leader sections, which contain indices of group members, are
397
0
      // discarded because they are useless beyond this point. The only
398
0
      // exception is the -r option because in order to produce re-linkable
399
0
      // object files, we want to pass through basically everything.
400
0
      if (IsNew) {
401
0
        if (Config->Relocatable)
402
0
          this->Sections[I] = createInputSection(Sec);
403
0
        continue;
404
0
      }
405
0
406
0
      // Otherwise, discard group members.
407
0
      for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
408
0
        if (SecIndex >= Size)
409
0
          fatal(toString(this) +
410
0
                ": invalid section index in group: " + Twine(SecIndex));
411
0
        this->Sections[SecIndex] = &InputSection::Discarded;
412
0
      }
413
0
      break;
414
0
    }
415
138
    case SHT_SYMTAB:
416
138
      this->initSymtab(ObjSections, &Sec);
417
138
      break;
418
0
    case SHT_SYMTAB_SHNDX:
419
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
420
0
      break;
421
287
    case SHT_STRTAB:
422
287
    case SHT_NULL:
423
287
      break;
424
825
    default:
425
825
      this->Sections[I] = createInputSection(Sec);
426
1.25k
    }
427
1.25k
428
1.25k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
429
1.25k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
430
1.25k
    if (Sec.sh_flags & SHF_LINK_ORDER) {
431
0
      if (Sec.sh_link >= this->Sections.size())
432
0
        fatal(toString(this) +
433
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
434
0
      this->Sections[Sec.sh_link]->DependentSections.push_back(
435
0
          cast<InputSection>(this->Sections[I]));
436
0
    }
437
1.25k
  }
438
138
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
366
1.73k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
367
1.73k
  const ELFFile<ELFT> &Obj = this->getObj();
368
1.73k
369
1.73k
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
370
1.73k
  uint64_t Size = ObjSections.size();
371
1.73k
  this->Sections.resize(Size);
372
1.73k
  this->SectionStringTable =
373
1.73k
      CHECK(Obj.getSectionStringTable(ObjSections), this);
374
1.73k
375
207k
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++205k
) {
376
205k
    if (this->Sections[I] == &InputSection::Discarded)
377
18
      continue;
378
205k
    const Elf_Shdr &Sec = ObjSections[I];
379
205k
380
205k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
381
205k
    // if -r is given, we'll let the final link discard such sections.
382
205k
    // This is compatible with GNU.
383
205k
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable2
) {
384
1
      this->Sections[I] = &InputSection::Discarded;
385
1
      continue;
386
1
    }
387
205k
388
205k
    switch (Sec.sh_type) {
389
205k
    case SHT_GROUP: {
390
46
      // De-duplicate section groups by their signatures.
391
46
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
392
46
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
393
46
      this->Sections[I] = &InputSection::Discarded;
394
46
395
46
      // If it is a new section group, we want to keep group members.
396
46
      // Group leader sections, which contain indices of group members, are
397
46
      // discarded because they are useless beyond this point. The only
398
46
      // exception is the -r option because in order to produce re-linkable
399
46
      // object files, we want to pass through basically everything.
400
46
      if (IsNew) {
401
29
        if (Config->Relocatable)
402
8
          this->Sections[I] = createInputSection(Sec);
403
29
        continue;
404
29
      }
405
17
406
17
      // Otherwise, discard group members.
407
18
      
for (uint32_t SecIndex : getShtGroupEntries(Sec))17
{
408
18
        if (SecIndex >= Size)
409
0
          fatal(toString(this) +
410
0
                ": invalid section index in group: " + Twine(SecIndex));
411
18
        this->Sections[SecIndex] = &InputSection::Discarded;
412
18
      }
413
17
      break;
414
17
    }
415
1.73k
    case SHT_SYMTAB:
416
1.73k
      this->initSymtab(ObjSections, &Sec);
417
1.73k
      break;
418
17
    case SHT_SYMTAB_SHNDX:
419
3
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
420
3
      break;
421
3.49k
    case SHT_STRTAB:
422
3.49k
    case SHT_NULL:
423
3.49k
      break;
424
200k
    default:
425
200k
      this->Sections[I] = createInputSection(Sec);
426
205k
    }
427
205k
428
205k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
429
205k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
430
205k
    
if (205k
Sec.sh_flags & SHF_LINK_ORDER205k
) {
431
13
      if (Sec.sh_link >= this->Sections.size())
432
0
        fatal(toString(this) +
433
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
434
13
      this->Sections[Sec.sh_link]->DependentSections.push_back(
435
13
          cast<InputSection>(this->Sections[I]));
436
13
    }
437
205k
  }
438
1.73k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
366
59
    DenseSet<CachedHashStringRef> &ComdatGroups) {
367
59
  const ELFFile<ELFT> &Obj = this->getObj();
368
59
369
59
  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
370
59
  uint64_t Size = ObjSections.size();
371
59
  this->Sections.resize(Size);
372
59
  this->SectionStringTable =
373
59
      CHECK(Obj.getSectionStringTable(ObjSections), this);
374
59
375
557
  for (size_t I = 0, E = ObjSections.size(); I < E; 
I++498
) {
376
498
    if (this->Sections[I] == &InputSection::Discarded)
377
0
      continue;
378
498
    const Elf_Shdr &Sec = ObjSections[I];
379
498
380
498
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
381
498
    // if -r is given, we'll let the final link discard such sections.
382
498
    // This is compatible with GNU.
383
498
    if ((Sec.sh_flags & SHF_EXCLUDE) && 
!Config->Relocatable0
) {
384
0
      this->Sections[I] = &InputSection::Discarded;
385
0
      continue;
386
0
    }
387
498
388
498
    switch (Sec.sh_type) {
389
498
    case SHT_GROUP: {
390
0
      // De-duplicate section groups by their signatures.
391
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
392
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
393
0
      this->Sections[I] = &InputSection::Discarded;
394
0
395
0
      // If it is a new section group, we want to keep group members.
396
0
      // Group leader sections, which contain indices of group members, are
397
0
      // discarded because they are useless beyond this point. The only
398
0
      // exception is the -r option because in order to produce re-linkable
399
0
      // object files, we want to pass through basically everything.
400
0
      if (IsNew) {
401
0
        if (Config->Relocatable)
402
0
          this->Sections[I] = createInputSection(Sec);
403
0
        continue;
404
0
      }
405
0
406
0
      // Otherwise, discard group members.
407
0
      for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
408
0
        if (SecIndex >= Size)
409
0
          fatal(toString(this) +
410
0
                ": invalid section index in group: " + Twine(SecIndex));
411
0
        this->Sections[SecIndex] = &InputSection::Discarded;
412
0
      }
413
0
      break;
414
0
    }
415
59
    case SHT_SYMTAB:
416
59
      this->initSymtab(ObjSections, &Sec);
417
59
      break;
418
0
    case SHT_SYMTAB_SHNDX:
419
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
420
0
      break;
421
120
    case SHT_STRTAB:
422
120
    case SHT_NULL:
423
120
      break;
424
319
    default:
425
319
      this->Sections[I] = createInputSection(Sec);
426
498
    }
427
498
428
498
    // .ARM.exidx sections have a reverse dependency on the InputSection they
429
498
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
430
498
    if (Sec.sh_flags & SHF_LINK_ORDER) {
431
0
      if (Sec.sh_link >= this->Sections.size())
432
0
        fatal(toString(this) +
433
0
              ": invalid sh_link index: " + Twine(Sec.sh_link));
434
0
      this->Sections[Sec.sh_link]->DependentSections.push_back(
435
0
          cast<InputSection>(this->Sections[I]));
436
0
    }
437
498
  }
438
59
}
439
440
// The ARM support in lld makes some use of instructions that are not available
441
// on all ARM architectures. Namely:
442
// - Use of BLX instruction for interworking between ARM and Thumb state.
443
// - Use of the extended Thumb branch encoding in relocation.
444
// - Use of the MOVT/MOVW instructions in Thumb Thunks.
445
// The ARM Attributes section contains information about the architecture chosen
446
// at compile time. We follow the convention that if at least one input object
447
// is compiled with an architecture that supports these features then lld is
448
// permitted to use them.
449
9
static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) {
450
9
  if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
451
0
    return;
452
9
  auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
453
9
  switch (Arch) {
454
9
  case ARMBuildAttrs::Pre_v4:
455
1
  case ARMBuildAttrs::v4:
456
1
  case ARMBuildAttrs::v4T:
457
1
    // Architectures prior to v5 do not support BLX instruction
458
1
    break;
459
1
  case ARMBuildAttrs::v5T:
460
1
  case ARMBuildAttrs::v5TE:
461
1
  case ARMBuildAttrs::v5TEJ:
462
1
  case ARMBuildAttrs::v6:
463
1
  case ARMBuildAttrs::v6KZ:
464
1
  case ARMBuildAttrs::v6K:
465
1
    Config->ARMHasBlx = true;
466
1
    // Architectures used in pre-Cortex processors do not support
467
1
    // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
468
1
    // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
469
1
    break;
470
7
  default:
471
7
    // All other Architectures have BLX and extended branch encoding
472
7
    Config->ARMHasBlx = true;
473
7
    Config->ARMJ1J2BranchEncoding = true;
474
7
    if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M)
475
7
      // All Architectures used in Cortex processors with the exception
476
7
      // of v6-M and v6S-M have the MOVT and MOVW instructions.
477
7
      Config->ARMHasMovtMovw = true;
478
7
    break;
479
9
  }
480
9
}
481
482
template <class ELFT>
483
1.30k
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
484
1.30k
  uint32_t Idx = Sec.sh_info;
485
1.30k
  if (Idx >= this->Sections.size())
486
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
487
1.30k
  InputSectionBase *Target = this->Sections[Idx];
488
1.30k
489
1.30k
  // Strictly speaking, a relocation section must be included in the
490
1.30k
  // group of the section it relocates. However, LLVM 3.3 and earlier
491
1.30k
  // would fail to do so, so we gracefully handle that case.
492
1.30k
  if (Target == &InputSection::Discarded)
493
3
    return nullptr;
494
1.30k
495
1.30k
  if (!Target)
496
0
    fatal(toString(this) + ": unsupported relocation reference");
497
1.30k
  return Target;
498
1.30k
}
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
483
297
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
484
297
  uint32_t Idx = Sec.sh_info;
485
297
  if (Idx >= this->Sections.size())
486
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
487
297
  InputSectionBase *Target = this->Sections[Idx];
488
297
489
297
  // Strictly speaking, a relocation section must be included in the
490
297
  // group of the section it relocates. However, LLVM 3.3 and earlier
491
297
  // would fail to do so, so we gracefully handle that case.
492
297
  if (Target == &InputSection::Discarded)
493
0
    return nullptr;
494
297
495
297
  if (!Target)
496
0
    fatal(toString(this) + ": unsupported relocation reference");
497
297
  return Target;
498
297
}
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
483
88
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
484
88
  uint32_t Idx = Sec.sh_info;
485
88
  if (Idx >= this->Sections.size())
486
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
487
88
  InputSectionBase *Target = this->Sections[Idx];
488
88
489
88
  // Strictly speaking, a relocation section must be included in the
490
88
  // group of the section it relocates. However, LLVM 3.3 and earlier
491
88
  // would fail to do so, so we gracefully handle that case.
492
88
  if (Target == &InputSection::Discarded)
493
0
    return nullptr;
494
88
495
88
  if (!Target)
496
0
    fatal(toString(this) + ": unsupported relocation reference");
497
88
  return Target;
498
88
}
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
483
871
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
484
871
  uint32_t Idx = Sec.sh_info;
485
871
  if (Idx >= this->Sections.size())
486
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
487
871
  InputSectionBase *Target = this->Sections[Idx];
488
871
489
871
  // Strictly speaking, a relocation section must be included in the
490
871
  // group of the section it relocates. However, LLVM 3.3 and earlier
491
871
  // would fail to do so, so we gracefully handle that case.
492
871
  if (Target == &InputSection::Discarded)
493
3
    return nullptr;
494
868
495
868
  if (!Target)
496
0
    fatal(toString(this) + ": unsupported relocation reference");
497
868
  return Target;
498
868
}
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
483
53
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
484
53
  uint32_t Idx = Sec.sh_info;
485
53
  if (Idx >= this->Sections.size())
486
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
487
53
  InputSectionBase *Target = this->Sections[Idx];
488
53
489
53
  // Strictly speaking, a relocation section must be included in the
490
53
  // group of the section it relocates. However, LLVM 3.3 and earlier
491
53
  // would fail to do so, so we gracefully handle that case.
492
53
  if (Target == &InputSection::Discarded)
493
0
    return nullptr;
494
53
495
53
  if (!Target)
496
0
    fatal(toString(this) + ": unsupported relocation reference");
497
53
  return Target;
498
53
}
499
500
// Create a regular InputSection class that has the same contents
501
// as a given section.
502
3
static InputSection *toRegularSection(MergeInputSection *Sec) {
503
3
  return make<InputSection>(Sec->File, Sec->Flags, Sec->Type, Sec->Alignment,
504
3
                            Sec->Data, Sec->Name);
505
3
}
506
507
template <class ELFT>
508
202k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
509
202k
  StringRef Name = getSectionName(Sec);
510
202k
511
202k
  switch (Sec.sh_type) {
512
202k
  case SHT_ARM_ATTRIBUTES: {
513
9
    if (Config->EMachine != EM_ARM)
514
0
      break;
515
9
    ARMAttributeParser Attributes;
516
9
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
517
9
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
518
9
    updateSupportedARMFeatures(Attributes);
519
9
    // FIXME: Retain the first attribute section we see. The eglibc ARM
520
9
    // dynamic loaders require the presence of an attribute section for dlopen
521
9
    // to work. In a full implementation we would merge all attribute sections.
522
9
    if (InX::ARMAttributes == nullptr) {
523
6
      InX::ARMAttributes = make<InputSection>(*this, Sec, Name);
524
6
      return InX::ARMAttributes;
525
6
    }
526
3
    return &InputSection::Discarded;
527
3
  }
528
1.30k
  case SHT_RELA:
529
1.30k
  case SHT_REL: {
530
1.30k
    // Find the relocation target section and associate this
531
1.30k
    // section with it. Target can be discarded, for example
532
1.30k
    // if it is a duplicated member of SHT_GROUP section, we
533
1.30k
    // do not create or proccess relocatable sections then.
534
1.30k
    InputSectionBase *Target = getRelocTarget(Sec);
535
1.30k
    if (!Target)
536
3
      return nullptr;
537
1.30k
538
1.30k
    // This section contains relocation information.
539
1.30k
    // If -r is given, we do not interpret or apply relocation
540
1.30k
    // but just copy relocation sections to output.
541
1.30k
    if (Config->Relocatable)
542
56
      return make<InputSection>(*this, Sec, Name);
543
1.25k
544
1.25k
    if (Target->FirstRelocation)
545
0
      fatal(toString(this) +
546
0
            ": multiple relocation sections to one section are not supported");
547
1.25k
548
1.25k
    // Mergeable sections with relocations are tricky because relocations
549
1.25k
    // need to be taken into account when comparing section contents for
550
1.25k
    // merging. It's not worth supporting such mergeable sections because
551
1.25k
    // they are rare and it'd complicates the internal design (we usually
552
1.25k
    // have to determine if two sections are mergeable early in the link
553
1.25k
    // process much before applying relocations). We simply handle mergeable
554
1.25k
    // sections with relocations as non-mergeable.
555
1.25k
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
556
3
      Target = toRegularSection(MS);
557
3
      this->Sections[Sec.sh_info] = Target;
558
3
    }
559
1.25k
560
1.25k
    size_t NumRelocations;
561
1.25k
    if (Sec.sh_type == SHT_RELA) {
562
895
      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
563
895
      Target->FirstRelocation = Rels.begin();
564
895
      NumRelocations = Rels.size();
565
895
      Target->AreRelocsRela = true;
566
895
    } else {
567
355
      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
568
355
      Target->FirstRelocation = Rels.begin();
569
355
      NumRelocations = Rels.size();
570
355
      Target->AreRelocsRela = false;
571
355
    }
572
1.25k
    assert(isUInt<31>(NumRelocations));
573
1.25k
    Target->NumRelocations = NumRelocations;
574
1.25k
575
1.25k
    // Relocation sections processed by the linker are usually removed
576
1.25k
    // from the output, so returning `nullptr` for the normal case.
577
1.25k
    // However, if -emit-relocs is given, we need to leave them in the output.
578
1.25k
    // (Some post link analysis tools need this information.)
579
1.25k
    if (Config->EmitRelocs) {
580
22
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
581
22
      // We will not emit relocation section if target was discarded.
582
22
      Target->DependentSections.push_back(RelocSec);
583
22
      return RelocSec;
584
22
    }
585
1.22k
    return nullptr;
586
1.22k
  }
587
201k
  }
588
201k
589
201k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
590
201k
  // that the code in the object file does not expect that the stack is
591
201k
  // executable (in terms of NX bit). If all input files have the marker,
592
201k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
593
201k
  // make the stack non-executable. Most object files have this section as
594
201k
  // of 2017.
595
201k
  //
596
201k
  // But making the stack non-executable is a norm today for security
597
201k
  // reasons. Failure to do so may result in a serious security issue.
598
201k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
599
201k
  // explicitly told to do otherwise (by -z execstack). Because the stack
600
201k
  // executable-ness is controlled solely by command line options,
601
201k
  // .note.GNU-stack sections are simply ignored.
602
201k
  if (Name == ".note.GNU-stack")
603
153
    return &InputSection::Discarded;
604
201k
605
201k
  // Split stacks is a feature to support a discontiguous stack. At least
606
201k
  // as of 2017, it seems that the feature is not being used widely.
607
201k
  // Only GNU gold supports that. We don't. For the details about that,
608
201k
  // see https://gcc.gnu.org/wiki/SplitStacks
609
201k
  if (Name == ".note.GNU-split-stack") {
610
1
    error(toString(this) +
611
1
          ": object file compiled with -fsplit-stack is not supported");
612
1
    return &InputSection::Discarded;
613
1
  }
614
201k
615
201k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
616
201k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
617
201k
  // sections. Drop those sections to avoid duplicate symbol errors.
618
201k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
619
201k
  // fixed for a while.
620
201k
  if (Name.startswith(".gnu.linkonce."))
621
2
    return &InputSection::Discarded;
622
201k
623
201k
  // The linker merges EH (exception handling) frames and creates a
624
201k
  // .eh_frame_hdr section for runtime. So we handle them with a special
625
201k
  // class. For relocatable outputs, they are just passed through.
626
201k
  if (Name == ".eh_frame" && 
!Config->Relocatable103
)
627
98
    return make<EhInputSection>(*this, Sec, Name);
628
201k
629
201k
  if (shouldMerge(Sec))
630
90
    return make<MergeInputSection>(*this, Sec, Name);
631
201k
  return make<InputSection>(*this, Sec, Name);
632
201k
}
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
508
1.24k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
509
1.24k
  StringRef Name = getSectionName(Sec);
510
1.24k
511
1.24k
  switch (Sec.sh_type) {
512
1.24k
  case SHT_ARM_ATTRIBUTES: {
513
9
    if (Config->EMachine != EM_ARM)
514
0
      break;
515
9
    ARMAttributeParser Attributes;
516
9
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
517
9
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
518
9
    updateSupportedARMFeatures(Attributes);
519
9
    // FIXME: Retain the first attribute section we see. The eglibc ARM
520
9
    // dynamic loaders require the presence of an attribute section for dlopen
521
9
    // to work. In a full implementation we would merge all attribute sections.
522
9
    if (InX::ARMAttributes == nullptr) {
523
6
      InX::ARMAttributes = make<InputSection>(*this, Sec, Name);
524
6
      return InX::ARMAttributes;
525
6
    }
526
3
    return &InputSection::Discarded;
527
3
  }
528
297
  case SHT_RELA:
529
297
  case SHT_REL: {
530
297
    // Find the relocation target section and associate this
531
297
    // section with it. Target can be discarded, for example
532
297
    // if it is a duplicated member of SHT_GROUP section, we
533
297
    // do not create or proccess relocatable sections then.
534
297
    InputSectionBase *Target = getRelocTarget(Sec);
535
297
    if (!Target)
536
0
      return nullptr;
537
297
538
297
    // This section contains relocation information.
539
297
    // If -r is given, we do not interpret or apply relocation
540
297
    // but just copy relocation sections to output.
541
297
    if (Config->Relocatable)
542
12
      return make<InputSection>(*this, Sec, Name);
543
285
544
285
    if (Target->FirstRelocation)
545
0
      fatal(toString(this) +
546
0
            ": multiple relocation sections to one section are not supported");
547
285
548
285
    // Mergeable sections with relocations are tricky because relocations
549
285
    // need to be taken into account when comparing section contents for
550
285
    // merging. It's not worth supporting such mergeable sections because
551
285
    // they are rare and it'd complicates the internal design (we usually
552
285
    // have to determine if two sections are mergeable early in the link
553
285
    // process much before applying relocations). We simply handle mergeable
554
285
    // sections with relocations as non-mergeable.
555
285
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
556
0
      Target = toRegularSection(MS);
557
0
      this->Sections[Sec.sh_info] = Target;
558
0
    }
559
285
560
285
    size_t NumRelocations;
561
285
    if (Sec.sh_type == SHT_RELA) {
562
2
      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
563
2
      Target->FirstRelocation = Rels.begin();
564
2
      NumRelocations = Rels.size();
565
2
      Target->AreRelocsRela = true;
566
283
    } else {
567
283
      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
568
283
      Target->FirstRelocation = Rels.begin();
569
283
      NumRelocations = Rels.size();
570
283
      Target->AreRelocsRela = false;
571
283
    }
572
285
    assert(isUInt<31>(NumRelocations));
573
285
    Target->NumRelocations = NumRelocations;
574
285
575
285
    // Relocation sections processed by the linker are usually removed
576
285
    // from the output, so returning `nullptr` for the normal case.
577
285
    // However, if -emit-relocs is given, we need to leave them in the output.
578
285
    // (Some post link analysis tools need this information.)
579
285
    if (Config->EmitRelocs) {
580
1
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
581
1
      // We will not emit relocation section if target was discarded.
582
1
      Target->DependentSections.push_back(RelocSec);
583
1
      return RelocSec;
584
1
    }
585
284
    return nullptr;
586
284
  }
587
943
  }
588
943
589
943
  // The GNU linker uses .note.GNU-stack section as a marker indicating
590
943
  // that the code in the object file does not expect that the stack is
591
943
  // executable (in terms of NX bit). If all input files have the marker,
592
943
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
593
943
  // make the stack non-executable. Most object files have this section as
594
943
  // of 2017.
595
943
  //
596
943
  // But making the stack non-executable is a norm today for security
597
943
  // reasons. Failure to do so may result in a serious security issue.
598
943
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
599
943
  // explicitly told to do otherwise (by -z execstack). Because the stack
600
943
  // executable-ness is controlled solely by command line options,
601
943
  // .note.GNU-stack sections are simply ignored.
602
943
  if (Name == ".note.GNU-stack")
603
0
    return &InputSection::Discarded;
604
943
605
943
  // Split stacks is a feature to support a discontiguous stack. At least
606
943
  // as of 2017, it seems that the feature is not being used widely.
607
943
  // Only GNU gold supports that. We don't. For the details about that,
608
943
  // see https://gcc.gnu.org/wiki/SplitStacks
609
943
  if (Name == ".note.GNU-split-stack") {
610
0
    error(toString(this) +
611
0
          ": object file compiled with -fsplit-stack is not supported");
612
0
    return &InputSection::Discarded;
613
0
  }
614
943
615
943
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
616
943
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
617
943
  // sections. Drop those sections to avoid duplicate symbol errors.
618
943
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
619
943
  // fixed for a while.
620
943
  if (Name.startswith(".gnu.linkonce."))
621
0
    return &InputSection::Discarded;
622
943
623
943
  // The linker merges EH (exception handling) frames and creates a
624
943
  // .eh_frame_hdr section for runtime. So we handle them with a special
625
943
  // class. For relocatable outputs, they are just passed through.
626
943
  if (Name == ".eh_frame" && 
!Config->Relocatable4
)
627
4
    return make<EhInputSection>(*this, Sec, Name);
628
939
629
939
  if (shouldMerge(Sec))
630
5
    return make<MergeInputSection>(*this, Sec, Name);
631
934
  return make<InputSection>(*this, Sec, Name);
632
934
}
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
508
825
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
509
825
  StringRef Name = getSectionName(Sec);
510
825
511
825
  switch (Sec.sh_type) {
512
825
  case SHT_ARM_ATTRIBUTES: {
513
0
    if (Config->EMachine != EM_ARM)
514
0
      break;
515
0
    ARMAttributeParser Attributes;
516
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
517
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
518
0
    updateSupportedARMFeatures(Attributes);
519
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
520
0
    // dynamic loaders require the presence of an attribute section for dlopen
521
0
    // to work. In a full implementation we would merge all attribute sections.
522
0
    if (InX::ARMAttributes == nullptr) {
523
0
      InX::ARMAttributes = make<InputSection>(*this, Sec, Name);
524
0
      return InX::ARMAttributes;
525
0
    }
526
0
    return &InputSection::Discarded;
527
0
  }
528
88
  case SHT_RELA:
529
88
  case SHT_REL: {
530
88
    // Find the relocation target section and associate this
531
88
    // section with it. Target can be discarded, for example
532
88
    // if it is a duplicated member of SHT_GROUP section, we
533
88
    // do not create or proccess relocatable sections then.
534
88
    InputSectionBase *Target = getRelocTarget(Sec);
535
88
    if (!Target)
536
0
      return nullptr;
537
88
538
88
    // This section contains relocation information.
539
88
    // If -r is given, we do not interpret or apply relocation
540
88
    // but just copy relocation sections to output.
541
88
    if (Config->Relocatable)
542
8
      return make<InputSection>(*this, Sec, Name);
543
80
544
80
    if (Target->FirstRelocation)
545
0
      fatal(toString(this) +
546
0
            ": multiple relocation sections to one section are not supported");
547
80
548
80
    // Mergeable sections with relocations are tricky because relocations
549
80
    // need to be taken into account when comparing section contents for
550
80
    // merging. It's not worth supporting such mergeable sections because
551
80
    // they are rare and it'd complicates the internal design (we usually
552
80
    // have to determine if two sections are mergeable early in the link
553
80
    // process much before applying relocations). We simply handle mergeable
554
80
    // sections with relocations as non-mergeable.
555
80
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
556
0
      Target = toRegularSection(MS);
557
0
      this->Sections[Sec.sh_info] = Target;
558
0
    }
559
80
560
80
    size_t NumRelocations;
561
80
    if (Sec.sh_type == SHT_RELA) {
562
9
      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
563
9
      Target->FirstRelocation = Rels.begin();
564
9
      NumRelocations = Rels.size();
565
9
      Target->AreRelocsRela = true;
566
71
    } else {
567
71
      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
568
71
      Target->FirstRelocation = Rels.begin();
569
71
      NumRelocations = Rels.size();
570
71
      Target->AreRelocsRela = false;
571
71
    }
572
80
    assert(isUInt<31>(NumRelocations));
573
80
    Target->NumRelocations = NumRelocations;
574
80
575
80
    // Relocation sections processed by the linker are usually removed
576
80
    // from the output, so returning `nullptr` for the normal case.
577
80
    // However, if -emit-relocs is given, we need to leave them in the output.
578
80
    // (Some post link analysis tools need this information.)
579
80
    if (Config->EmitRelocs) {
580
0
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
581
0
      // We will not emit relocation section if target was discarded.
582
0
      Target->DependentSections.push_back(RelocSec);
583
0
      return RelocSec;
584
0
    }
585
80
    return nullptr;
586
80
  }
587
737
  }
588
737
589
737
  // The GNU linker uses .note.GNU-stack section as a marker indicating
590
737
  // that the code in the object file does not expect that the stack is
591
737
  // executable (in terms of NX bit). If all input files have the marker,
592
737
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
593
737
  // make the stack non-executable. Most object files have this section as
594
737
  // of 2017.
595
737
  //
596
737
  // But making the stack non-executable is a norm today for security
597
737
  // reasons. Failure to do so may result in a serious security issue.
598
737
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
599
737
  // explicitly told to do otherwise (by -z execstack). Because the stack
600
737
  // executable-ness is controlled solely by command line options,
601
737
  // .note.GNU-stack sections are simply ignored.
602
737
  if (Name == ".note.GNU-stack")
603
0
    return &InputSection::Discarded;
604
737
605
737
  // Split stacks is a feature to support a discontiguous stack. At least
606
737
  // as of 2017, it seems that the feature is not being used widely.
607
737
  // Only GNU gold supports that. We don't. For the details about that,
608
737
  // see https://gcc.gnu.org/wiki/SplitStacks
609
737
  if (Name == ".note.GNU-split-stack") {
610
0
    error(toString(this) +
611
0
          ": object file compiled with -fsplit-stack is not supported");
612
0
    return &InputSection::Discarded;
613
0
  }
614
737
615
737
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
616
737
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
617
737
  // sections. Drop those sections to avoid duplicate symbol errors.
618
737
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
619
737
  // fixed for a while.
620
737
  if (Name.startswith(".gnu.linkonce."))
621
0
    return &InputSection::Discarded;
622
737
623
737
  // The linker merges EH (exception handling) frames and creates a
624
737
  // .eh_frame_hdr section for runtime. So we handle them with a special
625
737
  // class. For relocatable outputs, they are just passed through.
626
737
  if (Name == ".eh_frame" && 
!Config->Relocatable1
)
627
1
    return make<EhInputSection>(*this, Sec, Name);
628
736
629
736
  if (shouldMerge(Sec))
630
1
    return make<MergeInputSection>(*this, Sec, Name);
631
735
  return make<InputSection>(*this, Sec, Name);
632
735
}
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
508
200k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
509
200k
  StringRef Name = getSectionName(Sec);
510
200k
511
200k
  switch (Sec.sh_type) {
512
200k
  case SHT_ARM_ATTRIBUTES: {
513
0
    if (Config->EMachine != EM_ARM)
514
0
      break;
515
0
    ARMAttributeParser Attributes;
516
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
517
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
518
0
    updateSupportedARMFeatures(Attributes);
519
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
520
0
    // dynamic loaders require the presence of an attribute section for dlopen
521
0
    // to work. In a full implementation we would merge all attribute sections.
522
0
    if (InX::ARMAttributes == nullptr) {
523
0
      InX::ARMAttributes = make<InputSection>(*this, Sec, Name);
524
0
      return InX::ARMAttributes;
525
0
    }
526
0
    return &InputSection::Discarded;
527
0
  }
528
871
  case SHT_RELA:
529
871
  case SHT_REL: {
530
871
    // Find the relocation target section and associate this
531
871
    // section with it. Target can be discarded, for example
532
871
    // if it is a duplicated member of SHT_GROUP section, we
533
871
    // do not create or proccess relocatable sections then.
534
871
    InputSectionBase *Target = getRelocTarget(Sec);
535
871
    if (!Target)
536
3
      return nullptr;
537
868
538
868
    // This section contains relocation information.
539
868
    // If -r is given, we do not interpret or apply relocation
540
868
    // but just copy relocation sections to output.
541
868
    if (Config->Relocatable)
542
36
      return make<InputSection>(*this, Sec, Name);
543
832
544
832
    if (Target->FirstRelocation)
545
0
      fatal(toString(this) +
546
0
            ": multiple relocation sections to one section are not supported");
547
832
548
832
    // Mergeable sections with relocations are tricky because relocations
549
832
    // need to be taken into account when comparing section contents for
550
832
    // merging. It's not worth supporting such mergeable sections because
551
832
    // they are rare and it'd complicates the internal design (we usually
552
832
    // have to determine if two sections are mergeable early in the link
553
832
    // process much before applying relocations). We simply handle mergeable
554
832
    // sections with relocations as non-mergeable.
555
832
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
556
3
      Target = toRegularSection(MS);
557
3
      this->Sections[Sec.sh_info] = Target;
558
3
    }
559
832
560
832
    size_t NumRelocations;
561
832
    if (Sec.sh_type == SHT_RELA) {
562
831
      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
563
831
      Target->FirstRelocation = Rels.begin();
564
831
      NumRelocations = Rels.size();
565
831
      Target->AreRelocsRela = true;
566
831
    } else {
567
1
      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
568
1
      Target->FirstRelocation = Rels.begin();
569
1
      NumRelocations = Rels.size();
570
1
      Target->AreRelocsRela = false;
571
1
    }
572
832
    assert(isUInt<31>(NumRelocations));
573
832
    Target->NumRelocations = NumRelocations;
574
832
575
832
    // Relocation sections processed by the linker are usually removed
576
832
    // from the output, so returning `nullptr` for the normal case.
577
832
    // However, if -emit-relocs is given, we need to leave them in the output.
578
832
    // (Some post link analysis tools need this information.)
579
832
    if (Config->EmitRelocs) {
580
21
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
581
21
      // We will not emit relocation section if target was discarded.
582
21
      Target->DependentSections.push_back(RelocSec);
583
21
      return RelocSec;
584
21
    }
585
811
    return nullptr;
586
811
  }
587
199k
  }
588
199k
589
199k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
590
199k
  // that the code in the object file does not expect that the stack is
591
199k
  // executable (in terms of NX bit). If all input files have the marker,
592
199k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
593
199k
  // make the stack non-executable. Most object files have this section as
594
199k
  // of 2017.
595
199k
  //
596
199k
  // But making the stack non-executable is a norm today for security
597
199k
  // reasons. Failure to do so may result in a serious security issue.
598
199k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
599
199k
  // explicitly told to do otherwise (by -z execstack). Because the stack
600
199k
  // executable-ness is controlled solely by command line options,
601
199k
  // .note.GNU-stack sections are simply ignored.
602
199k
  if (Name == ".note.GNU-stack")
603
153
    return &InputSection::Discarded;
604
199k
605
199k
  // Split stacks is a feature to support a discontiguous stack. At least
606
199k
  // as of 2017, it seems that the feature is not being used widely.
607
199k
  // Only GNU gold supports that. We don't. For the details about that,
608
199k
  // see https://gcc.gnu.org/wiki/SplitStacks
609
199k
  if (Name == ".note.GNU-split-stack") {
610
1
    error(toString(this) +
611
1
          ": object file compiled with -fsplit-stack is not supported");
612
1
    return &InputSection::Discarded;
613
1
  }
614
199k
615
199k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
616
199k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
617
199k
  // sections. Drop those sections to avoid duplicate symbol errors.
618
199k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
619
199k
  // fixed for a while.
620
199k
  if (Name.startswith(".gnu.linkonce."))
621
2
    return &InputSection::Discarded;
622
199k
623
199k
  // The linker merges EH (exception handling) frames and creates a
624
199k
  // .eh_frame_hdr section for runtime. So we handle them with a special
625
199k
  // class. For relocatable outputs, they are just passed through.
626
199k
  if (Name == ".eh_frame" && 
!Config->Relocatable95
)
627
90
    return make<EhInputSection>(*this, Sec, Name);
628
199k
629
199k
  if (shouldMerge(Sec))
630
84
    return make<MergeInputSection>(*this, Sec, Name);
631
199k
  return make<InputSection>(*this, Sec, Name);
632
199k
}
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
508
319
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
509
319
  StringRef Name = getSectionName(Sec);
510
319
511
319
  switch (Sec.sh_type) {
512
319
  case SHT_ARM_ATTRIBUTES: {
513
0
    if (Config->EMachine != EM_ARM)
514
0
      break;
515
0
    ARMAttributeParser Attributes;
516
0
    ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
517
0
    Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
518
0
    updateSupportedARMFeatures(Attributes);
519
0
    // FIXME: Retain the first attribute section we see. The eglibc ARM
520
0
    // dynamic loaders require the presence of an attribute section for dlopen
521
0
    // to work. In a full implementation we would merge all attribute sections.
522
0
    if (InX::ARMAttributes == nullptr) {
523
0
      InX::ARMAttributes = make<InputSection>(*this, Sec, Name);
524
0
      return InX::ARMAttributes;
525
0
    }
526
0
    return &InputSection::Discarded;
527
0
  }
528
53
  case SHT_RELA:
529
53
  case SHT_REL: {
530
53
    // Find the relocation target section and associate this
531
53
    // section with it. Target can be discarded, for example
532
53
    // if it is a duplicated member of SHT_GROUP section, we
533
53
    // do not create or proccess relocatable sections then.
534
53
    InputSectionBase *Target = getRelocTarget(Sec);
535
53
    if (!Target)
536
0
      return nullptr;
537
53
538
53
    // This section contains relocation information.
539
53
    // If -r is given, we do not interpret or apply relocation
540
53
    // but just copy relocation sections to output.
541
53
    if (Config->Relocatable)
542
0
      return make<InputSection>(*this, Sec, Name);
543
53
544
53
    if (Target->FirstRelocation)
545
0
      fatal(toString(this) +
546
0
            ": multiple relocation sections to one section are not supported");
547
53
548
53
    // Mergeable sections with relocations are tricky because relocations
549
53
    // need to be taken into account when comparing section contents for
550
53
    // merging. It's not worth supporting such mergeable sections because
551
53
    // they are rare and it'd complicates the internal design (we usually
552
53
    // have to determine if two sections are mergeable early in the link
553
53
    // process much before applying relocations). We simply handle mergeable
554
53
    // sections with relocations as non-mergeable.
555
53
    if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
556
0
      Target = toRegularSection(MS);
557
0
      this->Sections[Sec.sh_info] = Target;
558
0
    }
559
53
560
53
    size_t NumRelocations;
561
53
    if (Sec.sh_type == SHT_RELA) {
562
53
      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
563
53
      Target->FirstRelocation = Rels.begin();
564
53
      NumRelocations = Rels.size();
565
53
      Target->AreRelocsRela = true;
566
53
    } else {
567
0
      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
568
0
      Target->FirstRelocation = Rels.begin();
569
0
      NumRelocations = Rels.size();
570
0
      Target->AreRelocsRela = false;
571
0
    }
572
53
    assert(isUInt<31>(NumRelocations));
573
53
    Target->NumRelocations = NumRelocations;
574
53
575
53
    // Relocation sections processed by the linker are usually removed
576
53
    // from the output, so returning `nullptr` for the normal case.
577
53
    // However, if -emit-relocs is given, we need to leave them in the output.
578
53
    // (Some post link analysis tools need this information.)
579
53
    if (Config->EmitRelocs) {
580
0
      InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
581
0
      // We will not emit relocation section if target was discarded.
582
0
      Target->DependentSections.push_back(RelocSec);
583
0
      return RelocSec;
584
0
    }
585
53
    return nullptr;
586
53
  }
587
266
  }
588
266
589
266
  // The GNU linker uses .note.GNU-stack section as a marker indicating
590
266
  // that the code in the object file does not expect that the stack is
591
266
  // executable (in terms of NX bit). If all input files have the marker,
592
266
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
593
266
  // make the stack non-executable. Most object files have this section as
594
266
  // of 2017.
595
266
  //
596
266
  // But making the stack non-executable is a norm today for security
597
266
  // reasons. Failure to do so may result in a serious security issue.
598
266
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
599
266
  // explicitly told to do otherwise (by -z execstack). Because the stack
600
266
  // executable-ness is controlled solely by command line options,
601
266
  // .note.GNU-stack sections are simply ignored.
602
266
  if (Name == ".note.GNU-stack")
603
0
    return &InputSection::Discarded;
604
266
605
266
  // Split stacks is a feature to support a discontiguous stack. At least
606
266
  // as of 2017, it seems that the feature is not being used widely.
607
266
  // Only GNU gold supports that. We don't. For the details about that,
608
266
  // see https://gcc.gnu.org/wiki/SplitStacks
609
266
  if (Name == ".note.GNU-split-stack") {
610
0
    error(toString(this) +
611
0
          ": object file compiled with -fsplit-stack is not supported");
612
0
    return &InputSection::Discarded;
613
0
  }
614
266
615
266
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
616
266
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
617
266
  // sections. Drop those sections to avoid duplicate symbol errors.
618
266
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
619
266
  // fixed for a while.
620
266
  if (Name.startswith(".gnu.linkonce."))
621
0
    return &InputSection::Discarded;
622
266
623
266
  // The linker merges EH (exception handling) frames and creates a
624
266
  // .eh_frame_hdr section for runtime. So we handle them with a special
625
266
  // class. For relocatable outputs, they are just passed through.
626
266
  if (Name == ".eh_frame" && 
!Config->Relocatable3
)
627
3
    return make<EhInputSection>(*this, Sec, Name);
628
263
629
263
  if (shouldMerge(Sec))
630
0
    return make<MergeInputSection>(*this, Sec, Name);
631
263
  return make<InputSection>(*this, Sec, Name);
632
263
}
633
634
template <class ELFT>
635
202k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
636
202k
  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
637
202k
}
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
635
1.24k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
636
1.24k
  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
637
1.24k
}
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
635
825
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
636
825
  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
637
825
}
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
635
200k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
636
200k
  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
637
200k
}
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
635
319
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
636
319
  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
637
319
}
638
639
2.19k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
640
2.19k
  this->Symbols.reserve(this->ELFSyms.size());
641
2.19k
  for (const Elf_Sym &Sym : this->ELFSyms)
642
18.5k
    this->Symbols.push_back(createSymbol(&Sym));
643
2.19k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSymbols()
Line
Count
Source
639
267
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
640
267
  this->Symbols.reserve(this->ELFSyms.size());
641
267
  for (const Elf_Sym &Sym : this->ELFSyms)
642
1.90k
    this->Symbols.push_back(createSymbol(&Sym));
643
267
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSymbols()
Line
Count
Source
639
138
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
640
138
  this->Symbols.reserve(this->ELFSyms.size());
641
138
  for (const Elf_Sym &Sym : this->ELFSyms)
642
612
    this->Symbols.push_back(createSymbol(&Sym));
643
138
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSymbols()
Line
Count
Source
639
1.73k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
640
1.73k
  this->Symbols.reserve(this->ELFSyms.size());
641
1.73k
  for (const Elf_Sym &Sym : this->ELFSyms)
642
5.73k
    this->Symbols.push_back(createSymbol(&Sym));
643
1.73k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSymbols()
Line
Count
Source
639
59
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
640
59
  this->Symbols.reserve(this->ELFSyms.size());
641
59
  for (const Elf_Sym &Sym : this->ELFSyms)
642
10.2k
    this->Symbols.push_back(createSymbol(&Sym));
643
59
}
644
645
18.5k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
646
18.5k
  int Binding = Sym->getBinding();
647
18.5k
648
18.5k
  uint32_t SecIdx = this->getSectionIndex(*Sym);
649
18.5k
  if (SecIdx >= this->Sections.size())
650
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
651
18.5k
652
18.5k
  InputSectionBase *Sec = this->Sections[SecIdx];
653
18.5k
  uint8_t StOther = Sym->st_other;
654
18.5k
  uint8_t Type = Sym->getType();
655
18.5k
  uint64_t Value = Sym->st_value;
656
18.5k
  uint64_t Size = Sym->st_size;
657
18.5k
658
18.5k
  if (Binding == STB_LOCAL) {
659
4.05k
    if (Sym->getType() == STT_FILE)
660
162
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
661
4.05k
662
4.05k
    if (this->StringTable.size() <= Sym->st_name)
663
0
      fatal(toString(this) + ": invalid symbol name offset");
664
4.05k
665
4.05k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
666
4.05k
    if (Sym->st_shndx == SHN_UNDEF)
667
2.20k
      return make<Undefined>(this, Name, Binding, StOther, Type);
668
1.85k
669
1.85k
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
670
1.85k
  }
671
14.4k
672
14.4k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
673
14.4k
674
14.4k
  switch (Sym->st_shndx) {
675
14.4k
  case SHN_UNDEF:
676
1.02k
    return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
677
1.02k
                                      /*CanOmitFromDynSym=*/false, this);
678
14.4k
  case SHN_COMMON:
679
111
    if (Value == 0 || Value >= UINT32_MAX)
680
111
      fatal(toString(this) + ": common symbol '" + Name +
681
0
            "' has invalid alignment: " + Twine(Value));
682
111
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
683
13.3k
  }
684
13.3k
685
13.3k
  switch (Binding) {
686
13.3k
  default:
687
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
688
13.3k
  case STB_GLOBAL:
689
13.3k
  case STB_WEAK:
690
13.3k
  case STB_GNU_UNIQUE:
691
13.3k
    if (Sec == &InputSection::Discarded)
692
8
      return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
693
8
                                        /*CanOmitFromDynSym=*/false, this);
694
13.2k
    return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec,
695
13.2k
                              this);
696
0
  }
697
0
}
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
645
1.90k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
646
1.90k
  int Binding = Sym->getBinding();
647
1.90k
648
1.90k
  uint32_t SecIdx = this->getSectionIndex(*Sym);
649
1.90k
  if (SecIdx >= this->Sections.size())
650
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
651
1.90k
652
1.90k
  InputSectionBase *Sec = this->Sections[SecIdx];
653
1.90k
  uint8_t StOther = Sym->st_other;
654
1.90k
  uint8_t Type = Sym->getType();
655
1.90k
  uint64_t Value = Sym->st_value;
656
1.90k
  uint64_t Size = Sym->st_size;
657
1.90k
658
1.90k
  if (Binding == STB_LOCAL) {
659
977
    if (Sym->getType() == STT_FILE)
660
2
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
661
977
662
977
    if (this->StringTable.size() <= Sym->st_name)
663
0
      fatal(toString(this) + ": invalid symbol name offset");
664
977
665
977
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
666
977
    if (Sym->st_shndx == SHN_UNDEF)
667
267
      return make<Undefined>(this, Name, Binding, StOther, Type);
668
710
669
710
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
670
710
  }
671
931
672
931
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
673
931
674
931
  switch (Sym->st_shndx) {
675
931
  case SHN_UNDEF:
676
162
    return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
677
162
                                      /*CanOmitFromDynSym=*/false, this);
678
931
  case SHN_COMMON:
679
6
    if (Value == 0 || Value >= UINT32_MAX)
680
6
      fatal(toString(this) + ": common symbol '" + Name +
681
0
            "' has invalid alignment: " + Twine(Value));
682
6
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
683
763
  }
684
763
685
763
  switch (Binding) {
686
763
  default:
687
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
688
763
  case STB_GLOBAL:
689
763
  case STB_WEAK:
690
763
  case STB_GNU_UNIQUE:
691
763
    if (Sec == &InputSection::Discarded)
692
0
      return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
693
0
                                        /*CanOmitFromDynSym=*/false, this);
694
763
    return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec,
695
763
                              this);
696
0
  }
697
0
}
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
645
612
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
646
612
  int Binding = Sym->getBinding();
647
612
648
612
  uint32_t SecIdx = this->getSectionIndex(*Sym);
649
612
  if (SecIdx >= this->Sections.size())
650
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
651
612
652
612
  InputSectionBase *Sec = this->Sections[SecIdx];
653
612
  uint8_t StOther = Sym->st_other;
654
612
  uint8_t Type = Sym->getType();
655
612
  uint64_t Value = Sym->st_value;
656
612
  uint64_t Size = Sym->st_size;
657
612
658
612
  if (Binding == STB_LOCAL) {
659
264
    if (Sym->getType() == STT_FILE)
660
0
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
661
264
662
264
    if (this->StringTable.size() <= Sym->st_name)
663
0
      fatal(toString(this) + ": invalid symbol name offset");
664
264
665
264
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
666
264
    if (Sym->st_shndx == SHN_UNDEF)
667
138
      return make<Undefined>(this, Name, Binding, StOther, Type);
668
126
669
126
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
670
126
  }
671
348
672
348
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
673
348
674
348
  switch (Sym->st_shndx) {
675
348
  case SHN_UNDEF:
676
83
    return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
677
83
                                      /*CanOmitFromDynSym=*/false, this);
678
348
  case SHN_COMMON:
679
0
    if (Value == 0 || Value >= UINT32_MAX)
680
0
      fatal(toString(this) + ": common symbol '" + Name +
681
0
            "' has invalid alignment: " + Twine(Value));
682
0
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
683
265
  }
684
265
685
265
  switch (Binding) {
686
265
  default:
687
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
688
265
  case STB_GLOBAL:
689
265
  case STB_WEAK:
690
265
  case STB_GNU_UNIQUE:
691
265
    if (Sec == &InputSection::Discarded)
692
0
      return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
693
0
                                        /*CanOmitFromDynSym=*/false, this);
694
265
    return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec,
695
265
                              this);
696
0
  }
697
0
}
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
645
5.73k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
646
5.73k
  int Binding = Sym->getBinding();
647
5.73k
648
5.73k
  uint32_t SecIdx = this->getSectionIndex(*Sym);
649
5.73k
  if (SecIdx >= this->Sections.size())
650
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
651
5.73k
652
5.73k
  InputSectionBase *Sec = this->Sections[SecIdx];
653
5.73k
  uint8_t StOther = Sym->st_other;
654
5.73k
  uint8_t Type = Sym->getType();
655
5.73k
  uint64_t Value = Sym->st_value;
656
5.73k
  uint64_t Size = Sym->st_size;
657
5.73k
658
5.73k
  if (Binding == STB_LOCAL) {
659
2.71k
    if (Sym->getType() == STT_FILE)
660
160
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
661
2.71k
662
2.71k
    if (this->StringTable.size() <= Sym->st_name)
663
0
      fatal(toString(this) + ": invalid symbol name offset");
664
2.71k
665
2.71k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
666
2.71k
    if (Sym->st_shndx == SHN_UNDEF)
667
1.74k
      return make<Undefined>(this, Name, Binding, StOther, Type);
668
971
669
971
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
670
971
  }
671
3.02k
672
3.02k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
673
3.02k
674
3.02k
  switch (Sym->st_shndx) {
675
3.02k
  case SHN_UNDEF:
676
760
    return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
677
760
                                      /*CanOmitFromDynSym=*/false, this);
678
3.02k
  case SHN_COMMON:
679
103
    if (Value == 0 || Value >= UINT32_MAX)
680
103
      fatal(toString(this) + ": common symbol '" + Name +
681
0
            "' has invalid alignment: " + Twine(Value));
682
103
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
683
2.16k
  }
684
2.16k
685
2.16k
  switch (Binding) {
686
2.16k
  default:
687
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
688
2.16k
  case STB_GLOBAL:
689
2.16k
  case STB_WEAK:
690
2.16k
  case STB_GNU_UNIQUE:
691
2.16k
    if (Sec == &InputSection::Discarded)
692
8
      return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
693
8
                                        /*CanOmitFromDynSym=*/false, this);
694
2.15k
    return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec,
695
2.15k
                              this);
696
0
  }
697
0
}
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
645
10.2k
template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
646
10.2k
  int Binding = Sym->getBinding();
647
10.2k
648
10.2k
  uint32_t SecIdx = this->getSectionIndex(*Sym);
649
10.2k
  if (SecIdx >= this->Sections.size())
650
0
    fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
651
10.2k
652
10.2k
  InputSectionBase *Sec = this->Sections[SecIdx];
653
10.2k
  uint8_t StOther = Sym->st_other;
654
10.2k
  uint8_t Type = Sym->getType();
655
10.2k
  uint64_t Value = Sym->st_value;
656
10.2k
  uint64_t Size = Sym->st_size;
657
10.2k
658
10.2k
  if (Binding == STB_LOCAL) {
659
107
    if (Sym->getType() == STT_FILE)
660
0
      SourceFile = CHECK(Sym->getName(this->StringTable), this);
661
107
662
107
    if (this->StringTable.size() <= Sym->st_name)
663
0
      fatal(toString(this) + ": invalid symbol name offset");
664
107
665
107
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
666
107
    if (Sym->st_shndx == SHN_UNDEF)
667
59
      return make<Undefined>(this, Name, Binding, StOther, Type);
668
48
669
48
    return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
670
48
  }
671
10.1k
672
10.1k
  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
673
10.1k
674
10.1k
  switch (Sym->st_shndx) {
675
10.1k
  case SHN_UNDEF:
676
23
    return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
677
23
                                      /*CanOmitFromDynSym=*/false, this);
678
10.1k
  case SHN_COMMON:
679
2
    if (Value == 0 || Value >= UINT32_MAX)
680
2
      fatal(toString(this) + ": common symbol '" + Name +
681
0
            "' has invalid alignment: " + Twine(Value));
682
2
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
683
10.1k
  }
684
10.1k
685
10.1k
  switch (Binding) {
686
10.1k
  default:
687
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
688
10.1k
  case STB_GLOBAL:
689
10.1k
  case STB_WEAK:
690
10.1k
  case STB_GNU_UNIQUE:
691
10.1k
    if (Sec == &InputSection::Discarded)
692
0
      return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
693
0
                                        /*CanOmitFromDynSym=*/false, this);
694
10.1k
    return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec,
695
10.1k
                              this);
696
0
  }
697
0
}
698
699
ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
700
    : InputFile(ArchiveKind, File->getMemoryBufferRef()),
701
75
      File(std::move(File)) {}
702
703
73
template <class ELFT> void ArchiveFile::parse() {
704
73
  Symbols.reserve(File->getNumberOfSymbols());
705
73
  for (const Archive::Symbol &Sym : File->symbols())
706
141
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym));
707
73
}
void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
703
3
template <class ELFT> void ArchiveFile::parse() {
704
3
  Symbols.reserve(File->getNumberOfSymbols());
705
3
  for (const Archive::Symbol &Sym : File->symbols())
706
4
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym));
707
3
}
Unexecuted instantiation: void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
703
69
template <class ELFT> void ArchiveFile::parse() {
704
69
  Symbols.reserve(File->getNumberOfSymbols());
705
69
  for (const Archive::Symbol &Sym : File->symbols())
706
136
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym));
707
69
}
void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
703
1
template <class ELFT> void ArchiveFile::parse() {
704
1
  Symbols.reserve(File->getNumberOfSymbols());
705
1
  for (const Archive::Symbol &Sym : File->symbols())
706
1
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym));
707
1
}
708
709
// Returns a buffer pointing to a member file containing a given symbol.
710
std::pair<MemoryBufferRef, uint64_t>
711
51
ArchiveFile::getMember(const Archive::Symbol *Sym) {
712
51
  Archive::Child C =
713
51
      CHECK(Sym->getMember(), toString(this) +
714
51
                                  ": could not get the member for symbol " +
715
51
                                  Sym->getName());
716
51
717
51
  if (!Seen.insert(C.getChildOffset()).second)
718
0
    return {MemoryBufferRef(), 0};
719
51
720
51
  MemoryBufferRef Ret =
721
51
      CHECK(C.getMemoryBufferRef(),
722
51
            toString(this) +
723
51
                ": could not get the buffer for the member defining symbol " +
724
51
                Sym->getName());
725
51
726
51
  if (C.getParent()->isThin() && 
Tar3
)
727
1
    Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), Ret.getBuffer());
728
51
  if (C.getParent()->isThin())
729
3
    return {Ret, 0};
730
48
  return {Ret, C.getChildOffset()};
731
48
}
732
733
template <class ELFT>
734
SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
735
    : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
736
280
      IsNeeded(!Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
736
52
      IsNeeded(!Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
736
19
      IsNeeded(!Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
736
198
      IsNeeded(!Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
736
11
      IsNeeded(!Config->AsNeeded) {}
737
738
// Partially parse the shared object file so that we can call
739
// getSoName on this object.
740
276
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
741
276
  const Elf_Shdr *DynamicSec = nullptr;
742
276
  const ELFFile<ELFT> Obj = this->getObj();
743
276
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
744
276
745
276
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
746
3.27k
  for (const Elf_Shdr &Sec : Sections) {
747
3.27k
    switch (Sec.sh_type) {
748
3.27k
    default:
749
2.69k
      continue;
750
3.27k
    case SHT_DYNSYM:
751
274
      this->initSymtab(Sections, &Sec);
752
274
      break;
753
3.27k
    case SHT_DYNAMIC:
754
274
      DynamicSec = &Sec;
755
274
      break;
756
3.27k
    case SHT_SYMTAB_SHNDX:
757
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
758
0
      break;
759
3.27k
    case SHT_GNU_versym:
760
14
      this->VersymSec = &Sec;
761
14
      break;
762
3.27k
    case SHT_GNU_verdef:
763
14
      this->VerdefSec = &Sec;
764
14
      break;
765
3.27k
    }
766
3.27k
  }
767
276
768
276
  if (this->VersymSec && 
this->ELFSyms.empty()14
)
769
1
    error("SHT_GNU_versym should be associated with symbol table");
770
276
771
276
  // Search for a DT_SONAME tag to initialize this->SoName.
772
276
  if (!DynamicSec)
773
2
    return;
774
274
  ArrayRef<Elf_Dyn> Arr =
775
274
      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
776
1.93k
  for (const Elf_Dyn &Dyn : Arr) {
777
1.93k
    if (Dyn.d_tag == DT_SONAME) {
778
47
      uint64_t Val = Dyn.getVal();
779
47
      if (Val >= this->StringTable.size())
780
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
781
47
      SoName = this->StringTable.data() + Val;
782
47
      return;
783
47
    }
784
1.93k
  }
785
274
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseSoName()
Line
Count
Source
740
50
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
741
50
  const Elf_Shdr *DynamicSec = nullptr;
742
50
  const ELFFile<ELFT> Obj = this->getObj();
743
50
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
744
50
745
50
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
746
571
  for (const Elf_Shdr &Sec : Sections) {
747
571
    switch (Sec.sh_type) {
748
571
    default:
749
471
      continue;
750
571
    case SHT_DYNSYM:
751
50
      this->initSymtab(Sections, &Sec);
752
50
      break;
753
571
    case SHT_DYNAMIC:
754
50
      DynamicSec = &Sec;
755
50
      break;
756
571
    case SHT_SYMTAB_SHNDX:
757
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
758
0
      break;
759
571
    case SHT_GNU_versym:
760
0
      this->VersymSec = &Sec;
761
0
      break;
762
571
    case SHT_GNU_verdef:
763
0
      this->VerdefSec = &Sec;
764
0
      break;
765
571
    }
766
571
  }
767
50
768
50
  if (this->VersymSec && 
this->ELFSyms.empty()0
)
769
0
    error("SHT_GNU_versym should be associated with symbol table");
770
50
771
50
  // Search for a DT_SONAME tag to initialize this->SoName.
772
50
  if (!DynamicSec)
773
0
    return;
774
50
  ArrayRef<Elf_Dyn> Arr =
775
50
      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
776
260
  for (const Elf_Dyn &Dyn : Arr) {
777
260
    if (Dyn.d_tag == DT_SONAME) {
778
18
      uint64_t Val = Dyn.getVal();
779
18
      if (Val >= this->StringTable.size())
780
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
781
18
      SoName = this->StringTable.data() + Val;
782
18
      return;
783
18
    }
784
260
  }
785
50
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseSoName()
Line
Count
Source
740
19
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
741
19
  const Elf_Shdr *DynamicSec = nullptr;
742
19
  const ELFFile<ELFT> Obj = this->getObj();
743
19
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
744
19
745
19
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
746
286
  for (const Elf_Shdr &Sec : Sections) {
747
286
    switch (Sec.sh_type) {
748
286
    default:
749
248
      continue;
750
286
    case SHT_DYNSYM:
751
19
      this->initSymtab(Sections, &Sec);
752
19
      break;
753
286
    case SHT_DYNAMIC:
754
19
      DynamicSec = &Sec;
755
19
      break;
756
286
    case SHT_SYMTAB_SHNDX:
757
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
758
0
      break;
759
286
    case SHT_GNU_versym:
760
0
      this->VersymSec = &Sec;
761
0
      break;
762
286
    case SHT_GNU_verdef:
763
0
      this->VerdefSec = &Sec;
764
0
      break;
765
286
    }
766
286
  }
767
19
768
19
  if (this->VersymSec && 
this->ELFSyms.empty()0
)
769
0
    error("SHT_GNU_versym should be associated with symbol table");
770
19
771
19
  // Search for a DT_SONAME tag to initialize this->SoName.
772
19
  if (!DynamicSec)
773
0
    return;
774
19
  ArrayRef<Elf_Dyn> Arr =
775
19
      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
776
247
  for (const Elf_Dyn &Dyn : Arr) {
777
247
    if (Dyn.d_tag == DT_SONAME) {
778
0
      uint64_t Val = Dyn.getVal();
779
0
      if (Val >= this->StringTable.size())
780
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
781
0
      SoName = this->StringTable.data() + Val;
782
0
      return;
783
0
    }
784
247
  }
785
19
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseSoName()
Line
Count
Source
740
196
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
741
196
  const Elf_Shdr *DynamicSec = nullptr;
742
196
  const ELFFile<ELFT> Obj = this->getObj();
743
196
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
744
196
745
196
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
746
2.24k
  for (const Elf_Shdr &Sec : Sections) {
747
2.24k
    switch (Sec.sh_type) {
748
2.24k
    default:
749
1.83k
      continue;
750
2.24k
    case SHT_DYNSYM:
751
194
      this->initSymtab(Sections, &Sec);
752
194
      break;
753
2.24k
    case SHT_DYNAMIC:
754
194
      DynamicSec = &Sec;
755
194
      break;
756
2.24k
    case SHT_SYMTAB_SHNDX:
757
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
758
0
      break;
759
2.24k
    case SHT_GNU_versym:
760
13
      this->VersymSec = &Sec;
761
13
      break;
762
2.24k
    case SHT_GNU_verdef:
763
13
      this->VerdefSec = &Sec;
764
13
      break;
765
2.24k
    }
766
2.24k
  }
767
196
768
196
  if (this->VersymSec && 
this->ELFSyms.empty()13
)
769
1
    error("SHT_GNU_versym should be associated with symbol table");
770
196
771
196
  // Search for a DT_SONAME tag to initialize this->SoName.
772
196
  if (!DynamicSec)
773
2
    return;
774
194
  ArrayRef<Elf_Dyn> Arr =
775
194
      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
776
1.31k
  for (const Elf_Dyn &Dyn : Arr) {
777
1.31k
    if (Dyn.d_tag == DT_SONAME) {
778
28
      uint64_t Val = Dyn.getVal();
779
28
      if (Val >= this->StringTable.size())
780
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
781
28
      SoName = this->StringTable.data() + Val;
782
28
      return;
783
28
    }
784
1.31k
  }
785
194
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseSoName()
Line
Count
Source
740
11
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
741
11
  const Elf_Shdr *DynamicSec = nullptr;
742
11
  const ELFFile<ELFT> Obj = this->getObj();
743
11
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
744
11
745
11
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
746
170
  for (const Elf_Shdr &Sec : Sections) {
747
170
    switch (Sec.sh_type) {
748
170
    default:
749
146
      continue;
750
170
    case SHT_DYNSYM:
751
11
      this->initSymtab(Sections, &Sec);
752
11
      break;
753
170
    case SHT_DYNAMIC:
754
11
      DynamicSec = &Sec;
755
11
      break;
756
170
    case SHT_SYMTAB_SHNDX:
757
0
      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
758
0
      break;
759
170
    case SHT_GNU_versym:
760
1
      this->VersymSec = &Sec;
761
1
      break;
762
170
    case SHT_GNU_verdef:
763
1
      this->VerdefSec = &Sec;
764
1
      break;
765
170
    }
766
170
  }
767
11
768
11
  if (this->VersymSec && 
this->ELFSyms.empty()1
)
769
0
    error("SHT_GNU_versym should be associated with symbol table");
770
11
771
11
  // Search for a DT_SONAME tag to initialize this->SoName.
772
11
  if (!DynamicSec)
773
0
    return;
774
11
  ArrayRef<Elf_Dyn> Arr =
775
11
      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
776
117
  for (const Elf_Dyn &Dyn : Arr) {
777
117
    if (Dyn.d_tag == DT_SONAME) {
778
1
      uint64_t Val = Dyn.getVal();
779
1
      if (Val >= this->StringTable.size())
780
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
781
1
      SoName = this->StringTable.data() + Val;
782
1
      return;
783
1
    }
784
117
  }
785
11
}
786
787
// Parse the version definitions in the object file if present. Returns a vector
788
// whose nth element contains a pointer to the Elf_Verdef for version identifier
789
// n. Version identifiers that are not definitions map to nullptr. The array
790
// always has at least length 1.
791
template <class ELFT>
792
std::vector<const typename ELFT::Verdef *>
793
272
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
794
272
  std::vector<const Elf_Verdef *> Verdefs(1);
795
272
  // We only need to process symbol versions for this DSO if it has both a
796
272
  // versym and a verdef section, which indicates that the DSO contains symbol
797
272
  // version definitions.
798
272
  if (!VersymSec || 
!VerdefSec13
)
799
259
    return Verdefs;
800
13
801
13
  // The location of the first global versym entry.
802
13
  const char *Base = this->MB.getBuffer().data();
803
13
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
804
13
           this->FirstNonLocal;
805
13
806
13
  // We cannot determine the largest verdef identifier without inspecting
807
13
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
808
13
  // sequentially starting from 1, so we predict that the largest identifier
809
13
  // will be VerdefCount.
810
13
  unsigned VerdefCount = VerdefSec->sh_info;
811
13
  Verdefs.resize(VerdefCount + 1);
812
13
813
13
  // Build the Verdefs array by following the chain of Elf_Verdef objects
814
13
  // from the start of the .gnu.version_d section.
815
13
  const char *Verdef = Base + VerdefSec->sh_offset;
816
56
  for (unsigned I = 0; I != VerdefCount; 
++I43
) {
817
43
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
818
43
    Verdef += CurVerdef->vd_next;
819
43
    unsigned VerdefIndex = CurVerdef->vd_ndx;
820
43
    if (Verdefs.size() <= VerdefIndex)
821
0
      Verdefs.resize(VerdefIndex + 1);
822
43
    Verdefs[VerdefIndex] = CurVerdef;
823
43
  }
824
13
825
13
  return Verdefs;
826
13
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*&)
Line
Count
Source
793
48
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
794
48
  std::vector<const Elf_Verdef *> Verdefs(1);
795
48
  // We only need to process symbol versions for this DSO if it has both a
796
48
  // versym and a verdef section, which indicates that the DSO contains symbol
797
48
  // version definitions.
798
48
  if (!VersymSec || 
!VerdefSec0
)
799
48
    return Verdefs;
800
0
801
0
  // The location of the first global versym entry.
802
0
  const char *Base = this->MB.getBuffer().data();
803
0
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
804
0
           this->FirstNonLocal;
805
0
806
0
  // We cannot determine the largest verdef identifier without inspecting
807
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
808
0
  // sequentially starting from 1, so we predict that the largest identifier
809
0
  // will be VerdefCount.
810
0
  unsigned VerdefCount = VerdefSec->sh_info;
811
0
  Verdefs.resize(VerdefCount + 1);
812
0
813
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
814
0
  // from the start of the .gnu.version_d section.
815
0
  const char *Verdef = Base + VerdefSec->sh_offset;
816
0
  for (unsigned I = 0; I != VerdefCount; ++I) {
817
0
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
818
0
    Verdef += CurVerdef->vd_next;
819
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
820
0
    if (Verdefs.size() <= VerdefIndex)
821
0
      Verdefs.resize(VerdefIndex + 1);
822
0
    Verdefs[VerdefIndex] = CurVerdef;
823
0
  }
824
0
825
0
  return Verdefs;
826
0
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*&)
Line
Count
Source
793
19
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
794
19
  std::vector<const Elf_Verdef *> Verdefs(1);
795
19
  // We only need to process symbol versions for this DSO if it has both a
796
19
  // versym and a verdef section, which indicates that the DSO contains symbol
797
19
  // version definitions.
798
19
  if (!VersymSec || 
!VerdefSec0
)
799
19
    return Verdefs;
800
0
801
0
  // The location of the first global versym entry.
802
0
  const char *Base = this->MB.getBuffer().data();
803
0
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
804
0
           this->FirstNonLocal;
805
0
806
0
  // We cannot determine the largest verdef identifier without inspecting
807
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
808
0
  // sequentially starting from 1, so we predict that the largest identifier
809
0
  // will be VerdefCount.
810
0
  unsigned VerdefCount = VerdefSec->sh_info;
811
0
  Verdefs.resize(VerdefCount + 1);
812
0
813
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
814
0
  // from the start of the .gnu.version_d section.
815
0
  const char *Verdef = Base + VerdefSec->sh_offset;
816
0
  for (unsigned I = 0; I != VerdefCount; ++I) {
817
0
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
818
0
    Verdef += CurVerdef->vd_next;
819
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
820
0
    if (Verdefs.size() <= VerdefIndex)
821
0
      Verdefs.resize(VerdefIndex + 1);
822
0
    Verdefs[VerdefIndex] = CurVerdef;
823
0
  }
824
0
825
0
  return Verdefs;
826
0
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*&)
Line
Count
Source
793
194
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
794
194
  std::vector<const Elf_Verdef *> Verdefs(1);
795
194
  // We only need to process symbol versions for this DSO if it has both a
796
194
  // versym and a verdef section, which indicates that the DSO contains symbol
797
194
  // version definitions.
798
194
  if (!VersymSec || 
!VerdefSec12
)
799
182
    return Verdefs;
800
12
801
12
  // The location of the first global versym entry.
802
12
  const char *Base = this->MB.getBuffer().data();
803
12
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
804
12
           this->FirstNonLocal;
805
12
806
12
  // We cannot determine the largest verdef identifier without inspecting
807
12
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
808
12
  // sequentially starting from 1, so we predict that the largest identifier
809
12
  // will be VerdefCount.
810
12
  unsigned VerdefCount = VerdefSec->sh_info;
811
12
  Verdefs.resize(VerdefCount + 1);
812
12
813
12
  // Build the Verdefs array by following the chain of Elf_Verdef objects
814
12
  // from the start of the .gnu.version_d section.
815
12
  const char *Verdef = Base + VerdefSec->sh_offset;
816
47
  for (unsigned I = 0; I != VerdefCount; 
++I35
) {
817
35
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
818
35
    Verdef += CurVerdef->vd_next;
819
35
    unsigned VerdefIndex = CurVerdef->vd_ndx;
820
35
    if (Verdefs.size() <= VerdefIndex)
821
0
      Verdefs.resize(VerdefIndex + 1);
822
35
    Verdefs[VerdefIndex] = CurVerdef;
823
35
  }
824
12
825
12
  return Verdefs;
826
12
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*&)
Line
Count
Source
793
11
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
794
11
  std::vector<const Elf_Verdef *> Verdefs(1);
795
11
  // We only need to process symbol versions for this DSO if it has both a
796
11
  // versym and a verdef section, which indicates that the DSO contains symbol
797
11
  // version definitions.
798
11
  if (!VersymSec || 
!VerdefSec1
)
799
10
    return Verdefs;
800
1
801
1
  // The location of the first global versym entry.
802
1
  const char *Base = this->MB.getBuffer().data();
803
1
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
804
1
           this->FirstNonLocal;
805
1
806
1
  // We cannot determine the largest verdef identifier without inspecting
807
1
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
808
1
  // sequentially starting from 1, so we predict that the largest identifier
809
1
  // will be VerdefCount.
810
1
  unsigned VerdefCount = VerdefSec->sh_info;
811
1
  Verdefs.resize(VerdefCount + 1);
812
1
813
1
  // Build the Verdefs array by following the chain of Elf_Verdef objects
814
1
  // from the start of the .gnu.version_d section.
815
1
  const char *Verdef = Base + VerdefSec->sh_offset;
816
9
  for (unsigned I = 0; I != VerdefCount; 
++I8
) {
817
8
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
818
8
    Verdef += CurVerdef->vd_next;
819
8
    unsigned VerdefIndex = CurVerdef->vd_ndx;
820
8
    if (Verdefs.size() <= VerdefIndex)
821
0
      Verdefs.resize(VerdefIndex + 1);
822
8
    Verdefs[VerdefIndex] = CurVerdef;
823
8
  }
824
1
825
1
  return Verdefs;
826
1
}
827
828
// Fully parse the shared object file. This must be called after parseSoName().
829
272
template <class ELFT> void SharedFile<ELFT>::parseRest() {
830
272
  // Create mapping from version identifiers to Elf_Verdef entries.
831
272
  const Elf_Versym *Versym = nullptr;
832
272
  Verdefs = parseVerdefs(Versym);
833
272
834
272
  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
835
272
836
272
  // Add symbols to the symbol table.
837
272
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
838
717
  for (const Elf_Sym &Sym : Syms) {
839
717
    unsigned VersymIndex = VER_NDX_GLOBAL;
840
717
    if (Versym) {
841
106
      VersymIndex = Versym->vs_index;
842
106
      ++Versym;
843
106
    }
844
717
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
845
717
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
846
717
847
717
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
848
717
    if (Sym.isUndefined()) {
849
35
      Undefs.push_back(Name);
850
35
      continue;
851
35
    }
852
682
853
682
    if (Sym.getBinding() == STB_LOCAL) {
854
2
      warn("found local symbol '" + Name +
855
2
           "' in global part of symbol table in file " + toString(this));
856
2
      continue;
857
2
    }
858
680
859
680
    const Elf_Verdef *Ver = nullptr;
860
680
    if (VersymIndex != VER_NDX_GLOBAL) {
861
67
      if (VersymIndex >= Verdefs.size() || 
VersymIndex == VER_NDX_LOCAL51
) {
862
17
        error("corrupt input file: version definition index " +
863
17
              Twine(VersymIndex) + " for symbol " + Name +
864
17
              " is out of bounds\n>>> defined in " + toString(this));
865
17
        continue;
866
17
      }
867
50
      Ver = Verdefs[VersymIndex];
868
613
    } else {
869
613
      VersymIndex = 0;
870
613
    }
871
680
872
680
    // We do not usually care about alignments of data in shared object
873
680
    // files because the loader takes care of it. However, if we promote a
874
680
    // DSO symbol to point to .bss due to copy relocation, we need to keep
875
680
    // the original alignment requirements. We infer it here.
876
680
    uint64_t Alignment = 1;
877
663
    if (Sym.st_value)
878
620
      Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
879
663
    if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
880
628
      uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
881
628
      Alignment = std::min(Alignment, SecAlign);
882
628
    }
883
663
    if (Alignment > UINT32_MAX)
884
663
      
error(toString(this) + ": alignment too large: " + Name)0
;
885
663
886
663
    if (!Hidden)
887
649
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
888
663
889
663
    // Also add the symbol with the versioned name to handle undefined symbols
890
663
    // with explicit versions.
891
663
    if (Ver) {
892
50
      StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
893
50
      Name = Saver.save(Name + "@" + VerName);
894
50
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
895
50
    }
896
663
  }
897
272
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseRest()
Line
Count
Source
829
48
template <class ELFT> void SharedFile<ELFT>::parseRest() {
830
48
  // Create mapping from version identifiers to Elf_Verdef entries.
831
48
  const Elf_Versym *Versym = nullptr;
832
48
  Verdefs = parseVerdefs(Versym);
833
48
834
48
  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
835
48
836
48
  // Add symbols to the symbol table.
837
48
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
838
103
  for (const Elf_Sym &Sym : Syms) {
839
103
    unsigned VersymIndex = VER_NDX_GLOBAL;
840
103
    if (Versym) {
841
0
      VersymIndex = Versym->vs_index;
842
0
      ++Versym;
843
0
    }
844
103
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
845
103
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
846
103
847
103
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
848
103
    if (Sym.isUndefined()) {
849
2
      Undefs.push_back(Name);
850
2
      continue;
851
2
    }
852
101
853
101
    if (Sym.getBinding() == STB_LOCAL) {
854
0
      warn("found local symbol '" + Name +
855
0
           "' in global part of symbol table in file " + toString(this));
856
0
      continue;
857
0
    }
858
101
859
101
    const Elf_Verdef *Ver = nullptr;
860
101
    if (VersymIndex != VER_NDX_GLOBAL) {
861
0
      if (VersymIndex >= Verdefs.size() || VersymIndex == VER_NDX_LOCAL) {
862
0
        error("corrupt input file: version definition index " +
863
0
              Twine(VersymIndex) + " for symbol " + Name +
864
0
              " is out of bounds\n>>> defined in " + toString(this));
865
0
        continue;
866
0
      }
867
0
      Ver = Verdefs[VersymIndex];
868
101
    } else {
869
101
      VersymIndex = 0;
870
101
    }
871
101
872
101
    // We do not usually care about alignments of data in shared object
873
101
    // files because the loader takes care of it. However, if we promote a
874
101
    // DSO symbol to point to .bss due to copy relocation, we need to keep
875
101
    // the original alignment requirements. We infer it here.
876
101
    uint64_t Alignment = 1;
877
101
    if (Sym.st_value)
878
93
      Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
879
101
    if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
880
101
      uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
881
101
      Alignment = std::min(Alignment, SecAlign);
882
101
    }
883
101
    if (Alignment > UINT32_MAX)
884
101
      
error(toString(this) + ": alignment too large: " + Name)0
;
885
101
886
101
    if (!Hidden)
887
101
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
888
101
889
101
    // Also add the symbol with the versioned name to handle undefined symbols
890
101
    // with explicit versions.
891
101
    if (Ver) {
892
0
      StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
893
0
      Name = Saver.save(Name + "@" + VerName);
894
0
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
895
0
    }
896
101
  }
897
48
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseRest()
Line
Count
Source
829
19
template <class ELFT> void SharedFile<ELFT>::parseRest() {
830
19
  // Create mapping from version identifiers to Elf_Verdef entries.
831
19
  const Elf_Versym *Versym = nullptr;
832
19
  Verdefs = parseVerdefs(Versym);
833
19
834
19
  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
835
19
836
19
  // Add symbols to the symbol table.
837
19
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
838
70
  for (const Elf_Sym &Sym : Syms) {
839
70
    unsigned VersymIndex = VER_NDX_GLOBAL;
840
70
    if (Versym) {
841
0
      VersymIndex = Versym->vs_index;
842
0
      ++Versym;
843
0
    }
844
70
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
845
70
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
846
70
847
70
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
848
70
    if (Sym.isUndefined()) {
849
0
      Undefs.push_back(Name);
850
0
      continue;
851
0
    }
852
70
853
70
    if (Sym.getBinding() == STB_LOCAL) {
854
0
      warn("found local symbol '" + Name +
855
0
           "' in global part of symbol table in file " + toString(this));
856
0
      continue;
857
0
    }
858
70
859
70
    const Elf_Verdef *Ver = nullptr;
860
70
    if (VersymIndex != VER_NDX_GLOBAL) {
861
0
      if (VersymIndex >= Verdefs.size() || VersymIndex == VER_NDX_LOCAL) {
862
0
        error("corrupt input file: version definition index " +
863
0
              Twine(VersymIndex) + " for symbol " + Name +
864
0
              " is out of bounds\n>>> defined in " + toString(this));
865
0
        continue;
866
0
      }
867
0
      Ver = Verdefs[VersymIndex];
868
70
    } else {
869
70
      VersymIndex = 0;
870
70
    }
871
70
872
70
    // We do not usually care about alignments of data in shared object
873
70
    // files because the loader takes care of it. However, if we promote a
874
70
    // DSO symbol to point to .bss due to copy relocation, we need to keep
875
70
    // the original alignment requirements. We infer it here.
876
70
    uint64_t Alignment = 1;
877
70
    if (Sym.st_value)
878
68
      Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
879
70
    if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
880
69
      uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
881
69
      Alignment = std::min(Alignment, SecAlign);
882
69
    }
883
70
    if (Alignment > UINT32_MAX)
884
70
      
error(toString(this) + ": alignment too large: " + Name)0
;
885
70
886
70
    if (!Hidden)
887
70
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
888
70
889
70
    // Also add the symbol with the versioned name to handle undefined symbols
890
70
    // with explicit versions.
891
70
    if (Ver) {
892
0
      StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
893
0
      Name = Saver.save(Name + "@" + VerName);
894
0
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
895
0
    }
896
70
  }
897
19
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseRest()
Line
Count
Source
829
194
template <class ELFT> void SharedFile<ELFT>::parseRest() {
830
194
  // Create mapping from version identifiers to Elf_Verdef entries.
831
194
  const Elf_Versym *Versym = nullptr;
832
194
  Verdefs = parseVerdefs(Versym);
833
194
834
194
  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
835
194
836
194
  // Add symbols to the symbol table.
837
194
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
838
462
  for (const Elf_Sym &Sym : Syms) {
839
462
    unsigned VersymIndex = VER_NDX_GLOBAL;
840
462
    if (Versym) {
841
48
      VersymIndex = Versym->vs_index;
842
48
      ++Versym;
843
48
    }
844
462
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
845
462
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
846
462
847
462
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
848
462
    if (Sym.isUndefined()) {
849
33
      Undefs.push_back(Name);
850
33
      continue;
851
33
    }
852
429
853
429
    if (Sym.getBinding() == STB_LOCAL) {
854
2
      warn("found local symbol '" + Name +
855
2
           "' in global part of symbol table in file " + toString(this));
856
2
      continue;
857
2
    }
858
427
859
427
    const Elf_Verdef *Ver = nullptr;
860
427
    if (VersymIndex != VER_NDX_GLOBAL) {
861
33
      if (VersymIndex >= Verdefs.size() || VersymIndex == VER_NDX_LOCAL) {
862
0
        error("corrupt input file: version definition index " +
863
0
              Twine(VersymIndex) + " for symbol " + Name +
864
0
              " is out of bounds\n>>> defined in " + toString(this));
865
0
        continue;
866
0
      }
867
33
      Ver = Verdefs[VersymIndex];
868
394
    } else {
869
394
      VersymIndex = 0;
870
394
    }
871
427
872
427
    // We do not usually care about alignments of data in shared object
873
427
    // files because the loader takes care of it. However, if we promote a
874
427
    // DSO symbol to point to .bss due to copy relocation, we need to keep
875
427
    // the original alignment requirements. We infer it here.
876
427
    uint64_t Alignment = 1;
877
427
    if (Sym.st_value)
878
420
      Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
879
427
    if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
880
417
      uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
881
417
      Alignment = std::min(Alignment, SecAlign);
882
417
    }
883
427
    if (Alignment > UINT32_MAX)
884
427
      
error(toString(this) + ": alignment too large: " + Name)0
;
885
427
886
427
    if (!Hidden)
887
413
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
888
427
889
427
    // Also add the symbol with the versioned name to handle undefined symbols
890
427
    // with explicit versions.
891
427
    if (Ver) {
892
33
      StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
893
33
      Name = Saver.save(Name + "@" + VerName);
894
33
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
895
33
    }
896
427
  }
897
194
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseRest()
Line
Count
Source
829
11
template <class ELFT> void SharedFile<ELFT>::parseRest() {
830
11
  // Create mapping from version identifiers to Elf_Verdef entries.
831
11
  const Elf_Versym *Versym = nullptr;
832
11
  Verdefs = parseVerdefs(Versym);
833
11
834
11
  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
835
11
836
11
  // Add symbols to the symbol table.
837
11
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
838
82
  for (const Elf_Sym &Sym : Syms) {
839
82
    unsigned VersymIndex = VER_NDX_GLOBAL;
840
82
    if (Versym) {
841
58
      VersymIndex = Versym->vs_index;
842
58
      ++Versym;
843
58
    }
844
82
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
845
82
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
846
82
847
82
    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
848
82
    if (Sym.isUndefined()) {
849
0
      Undefs.push_back(Name);
850
0
      continue;
851
0
    }
852
82
853
82
    if (Sym.getBinding() == STB_LOCAL) {
854
0
      warn("found local symbol '" + Name +
855
0
           "' in global part of symbol table in file " + toString(this));
856
0
      continue;
857
0
    }
858
82
859
82
    const Elf_Verdef *Ver = nullptr;
860
82
    if (VersymIndex != VER_NDX_GLOBAL) {
861
34
      if (VersymIndex >= Verdefs.size() || 
VersymIndex == VER_NDX_LOCAL18
) {
862
17
        error("corrupt input file: version definition index " +
863
17
              Twine(VersymIndex) + " for symbol " + Name +
864
17
              " is out of bounds\n>>> defined in " + toString(this));
865
17
        continue;
866
17
      }
867
17
      Ver = Verdefs[VersymIndex];
868
48
    } else {
869
48
      VersymIndex = 0;
870
48
    }
871
82
872
82
    // We do not usually care about alignments of data in shared object
873
82
    // files because the loader takes care of it. However, if we promote a
874
82
    // DSO symbol to point to .bss due to copy relocation, we need to keep
875
82
    // the original alignment requirements. We infer it here.
876
82
    uint64_t Alignment = 1;
877
65
    if (Sym.st_value)
878
39
      Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
879
65
    if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
880
41
      uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
881
41
      Alignment = std::min(Alignment, SecAlign);
882
41
    }
883
65
    if (Alignment > UINT32_MAX)
884
65
      
error(toString(this) + ": alignment too large: " + Name)0
;
885
65
886
65
    if (!Hidden)
887
65
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
888
65
889
65
    // Also add the symbol with the versioned name to handle undefined symbols
890
65
    // with explicit versions.
891
65
    if (Ver) {
892
17
      StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
893
17
      Name = Saver.save(Name + "@" + VerName);
894
17
      Symtab->addShared(Name, *this, Sym, Alignment, VersymIndex);
895
17
    }
896
65
  }
897
11
}
898
899
172
static ELFKind getBitcodeELFKind(const Triple &T) {
900
172
  if (T.isLittleEndian())
901
172
    return T.isArch64Bit() ? ELF64LEKind : 
ELF32LEKind0
;
902
0
  return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
903
0
}
904
905
172
static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
906
172
  switch (T.getArch()) {
907
172
  case Triple::aarch64:
908
0
    return EM_AARCH64;
909
172
  case Triple::arm:
910
0
  case Triple::thumb:
911
0
    return EM_ARM;
912
0
  case Triple::avr:
913
0
    return EM_AVR;
914
0
  case Triple::mips:
915
0
  case Triple::mipsel:
916
0
  case Triple::mips64:
917
0
  case Triple::mips64el:
918
0
    return EM_MIPS;
919
0
  case Triple::ppc:
920
0
    return EM_PPC;
921
0
  case Triple::ppc64:
922
0
    return EM_PPC64;
923
0
  case Triple::x86:
924
0
    return T.isOSIAMCU() ? EM_IAMCU : EM_386;
925
172
  case Triple::x86_64:
926
172
    return EM_X86_64;
927
0
  default:
928
0
    fatal(Path + ": could not infer e_machine from bitcode target triple " +
929
0
          T.str());
930
0
  }
931
0
}
932
933
BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
934
                         uint64_t OffsetInArchive)
935
172
    : InputFile(BitcodeKind, MB) {
936
172
  this->ArchiveName = ArchiveName;
937
172
938
172
  // Here we pass a new MemoryBufferRef which is identified by ArchiveName
939
172
  // (the fully resolved path of the archive) + member name + offset of the
940
172
  // member in the archive.
941
172
  // ThinLTO uses the MemoryBufferRef identifier to access its internal
942
172
  // data structures and if two archives define two members with the same name,
943
172
  // this causes a collision which result in only one of the objects being
944
172
  // taken into consideration at LTO time (which very likely causes undefined
945
172
  // symbols later in the link stage).
946
172
  MemoryBufferRef MBRef(MB.getBuffer(),
947
172
                        Saver.save(ArchiveName + MB.getBufferIdentifier() +
948
172
                                   utostr(OffsetInArchive)));
949
172
  Obj = CHECK(lto::InputFile::create(MBRef), this);
950
172
951
172
  Triple T(Obj->getTargetTriple());
952
172
  EKind = getBitcodeELFKind(T);
953
172
  EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
954
172
}
955
956
293
static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
957
293
  switch (GvVisibility) {
958
293
  case GlobalValue::DefaultVisibility:
959
263
    return STV_DEFAULT;
960
293
  case GlobalValue::HiddenVisibility:
961
28
    return STV_HIDDEN;
962
293
  case GlobalValue::ProtectedVisibility:
963
2
    return STV_PROTECTED;
964
0
  }
965
0
  llvm_unreachable("unknown visibility");
966
0
}
967
968
template <class ELFT>
969
static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
970
                                   const lto::InputFile::Symbol &ObjSym,
971
293
                                   BitcodeFile &F) {
972
293
  StringRef NameRef = Saver.save(ObjSym.getName());
973
293
  uint32_t Binding = ObjSym.isWeak() ? 
STB_WEAK21
:
STB_GLOBAL272
;
974
293
975
293
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE291
;
976
293
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
977
293
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
978
293
979
293
  int C = ObjSym.getComdatIndex();
980
293
  if (C != -1 && 
!KeptComdats[C]7
)
981
3
    return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
982
3
                                      CanOmitFromDynSym, &F);
983
290
984
290
  if (ObjSym.isUndefined())
985
73
    return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
986
73
                                      CanOmitFromDynSym, &F);
987
217
988
217
  if (ObjSym.isCommon())
989
5
    return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
990
5
                             ObjSym.getCommonAlignment(), Binding, Visibility,
991
5
                             STT_OBJECT, F);
992
212
993
212
  return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
994
212
                            CanOmitFromDynSym, F);
995
212
}
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
971
293
                                   BitcodeFile &F) {
972
293
  StringRef NameRef = Saver.save(ObjSym.getName());
973
293
  uint32_t Binding = ObjSym.isWeak() ? 
STB_WEAK21
:
STB_GLOBAL272
;
974
293
975
293
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE291
;
976
293
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
977
293
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
978
293
979
293
  int C = ObjSym.getComdatIndex();
980
293
  if (C != -1 && 
!KeptComdats[C]7
)
981
3
    return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
982
3
                                      CanOmitFromDynSym, &F);
983
290
984
290
  if (ObjSym.isUndefined())
985
73
    return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
986
73
                                      CanOmitFromDynSym, &F);
987
217
988
217
  if (ObjSym.isCommon())
989
5
    return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
990
5
                             ObjSym.getCommonAlignment(), Binding, Visibility,
991
5
                             STT_OBJECT, F);
992
212
993
212
  return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
994
212
                            CanOmitFromDynSym, F);
995
212
}
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&)
996
997
template <class ELFT>
998
166
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
999
166
  std::vector<bool> KeptComdats;
1000
166
  for (StringRef S : Obj->getComdatTable())
1001
7
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
1002
166
1003
166
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
1004
293
    Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
1005
166
}
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
998
166
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
999
166
  std::vector<bool> KeptComdats;
1000
166
  for (StringRef S : Obj->getComdatTable())
1001
7
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
1002
166
1003
166
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
1004
293
    Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
1005
166
}
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
1006
1007
2.60k
static ELFKind getELFKind(MemoryBufferRef MB) {
1008
2.60k
  unsigned char Size;
1009
2.60k
  unsigned char Endian;
1010
2.60k
  std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
1011
2.60k
1012
2.60k
  if (Endian != ELFDATA2LSB && 
Endian != ELFDATA2MSB230
)
1013
0
    fatal(MB.getBufferIdentifier() + ": invalid data encoding");
1014
2.60k
  if (Size != ELFCLASS32 && 
Size != ELFCLASS642.12k
)
1015
0
    fatal(MB.getBufferIdentifier() + ": invalid file class");
1016
2.60k
1017
2.60k
  size_t BufSize = MB.getBuffer().size();
1018
2.60k
  if ((Size == ELFCLASS32 && 
BufSize < sizeof(Elf32_Ehdr)482
) ||
1019
2.60k
      (Size == ELFCLASS64 && 
BufSize < sizeof(Elf64_Ehdr)2.12k
))
1020
0
    fatal(MB.getBufferIdentifier() + ": file is too short");
1021
2.60k
1022
2.60k
  if (Size == ELFCLASS32)
1023
482
    return (Endian == ELFDATA2LSB) ? 
ELF32LEKind322
:
ELF32BEKind160
;
1024
2.12k
  return (Endian == ELFDATA2LSB) ? 
ELF64LEKind2.05k
:
ELF64BEKind70
;
1025
2.12k
}
1026
1027
5
void BinaryFile::parse() {
1028
5
  ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
1029
5
  auto *Section = make<InputSection>(nullptr, SHF_ALLOC | SHF_WRITE,
1030
5
                                     SHT_PROGBITS, 8, Data, ".data");
1031
5
  Sections.push_back(Section);
1032
5
1033
5
  // For each input file foo that is embedded to a result as a binary
1034
5
  // blob, we define _binary_foo_{start,end,size} symbols, so that
1035
5
  // user programs can access blobs by name. Non-alphanumeric
1036
5
  // characters in a filename are replaced with underscore.
1037
5
  std::string S = "_binary_" + MB.getBufferIdentifier().str();
1038
482
  for (size_t I = 0; I < S.size(); 
++I477
)
1039
477
    if (!isAlnum(S[I]))
1040
80
      S[I] = '_';
1041
5
1042
5
  Symtab->addRegular(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT, 0, 0,
1043
5
                     STB_GLOBAL, Section, nullptr);
1044
5
  Symtab->addRegular(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
1045
5
                     Data.size(), 0, STB_GLOBAL, Section, nullptr);
1046
5
  Symtab->addRegular(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
1047
5
                     Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
1048
5
}
1049
1050
2.50k
static bool isBitcode(MemoryBufferRef MB) {
1051
2.50k
  using namespace sys::fs;
1052
2.50k
  return identify_magic(MB.getBuffer()) == file_magic::bitcode;
1053
2.50k
}
1054
1055
InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
1056
2.48k
                                 uint64_t OffsetInArchive) {
1057
2.48k
  if (isBitcode(MB))
1058
172
    return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
1059
2.31k
1060
2.31k
  switch (getELFKind(MB)) {
1061
2.31k
  case ELF32LEKind:
1062
270
    return make<ObjFile<ELF32LE>>(MB, ArchiveName);
1063
2.31k
  case ELF32BEKind:
1064
141
    return make<ObjFile<ELF32BE>>(MB, ArchiveName);
1065
2.31k
  case ELF64LEKind:
1066
1.84k
    return make<ObjFile<ELF64LE>>(MB, ArchiveName);
1067
2.31k
  case ELF64BEKind:
1068
59
    return make<ObjFile<ELF64BE>>(MB, ArchiveName);
1069
2.31k
  default:
1070
0
    llvm_unreachable("getELFKind");
1071
0
  }
1072
0
}
1073
1074
280
InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
1075
280
  switch (getELFKind(MB)) {
1076
280
  case ELF32LEKind:
1077
52
    return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
1078
280
  case ELF32BEKind:
1079
19
    return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
1080
280
  case ELF64LEKind:
1081
198
    return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
1082
280
  case ELF64BEKind:
1083
11
    return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
1084
280
  default:
1085
0
    llvm_unreachable("getELFKind");
1086
0
  }
1087
0
}
1088
1089
14
MemoryBufferRef LazyObjFile::getBuffer() {
1090
14
  if (Seen)
1091
2
    return MemoryBufferRef();
1092
12
  Seen = true;
1093
12
  return MB;
1094
12
}
1095
1096
14
InputFile *LazyObjFile::fetch() {
1097
14
  MemoryBufferRef MBRef = getBuffer();
1098
14
  if (MBRef.getBuffer().empty())
1099
2
    return nullptr;
1100
12
  return createObjectFile(MBRef, ArchiveName, OffsetInArchive);
1101
12
}
1102
1103
19
template <class ELFT> void LazyObjFile::parse() {
1104
19
  for (StringRef Sym : getSymbolNames())
1105
25
    Symtab->addLazyObject<ELFT>(Sym, *this);
1106
19
}
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
1103
19
template <class ELFT> void LazyObjFile::parse() {
1104
19
  for (StringRef Sym : getSymbolNames())
1105
25
    Symtab->addLazyObject<ELFT>(Sym, *this);
1106
19
}
Unexecuted instantiation: void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
1107
1108
10
template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1109
10
  typedef typename ELFT::Shdr Elf_Shdr;
1110
10
  typedef typename ELFT::Sym Elf_Sym;
1111
10
  typedef typename ELFT::SymRange Elf_Sym_Range;
1112
10
1113
10
  ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
1114
10
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1115
47
  for (const Elf_Shdr &Sec : Sections) {
1116
47
    if (Sec.sh_type != SHT_SYMTAB)
1117
37
      continue;
1118
10
1119
10
    Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
1120
10
    uint32_t FirstNonLocal = Sec.sh_info;
1121
10
    StringRef StringTable =
1122
10
        CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1123
10
    std::vector<StringRef> V;
1124
10
1125
10
    for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1126
20
      if (Sym.st_shndx != SHN_UNDEF)
1127
16
        V.push_back(CHECK(Sym.getName(StringTable), this));
1128
10
    return V;
1129
10
  }
1130
10
  
return {}0
;
1131
10
}
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1108
10
template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1109
10
  typedef typename ELFT::Shdr Elf_Shdr;
1110
10
  typedef typename ELFT::Sym Elf_Sym;
1111
10
  typedef typename ELFT::SymRange Elf_Sym_Range;
1112
10
1113
10
  ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
1114
10
  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1115
47
  for (const Elf_Shdr &Sec : Sections) {
1116
47
    if (Sec.sh_type != SHT_SYMTAB)
1117
37
      continue;
1118
10
1119
10
    Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
1120
10
    uint32_t FirstNonLocal = Sec.sh_info;
1121
10
    StringRef StringTable =
1122
10
        CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1123
10
    std::vector<StringRef> V;
1124
10
1125
10
    for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1126
20
      if (Sym.st_shndx != SHN_UNDEF)
1127
16
        V.push_back(CHECK(Sym.getName(StringTable), this));
1128
10
    return V;
1129
10
  }
1130
10
  
return {}0
;
1131
10
}
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
1132
1133
9
std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
1134
9
  std::unique_ptr<lto::InputFile> Obj =
1135
9
      CHECK(lto::InputFile::create(this->MB), this);
1136
9
  std::vector<StringRef> V;
1137
9
  for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1138
11
    if (!Sym.isUndefined())
1139
9
      V.push_back(Saver.save(Sym.getName()));
1140
9
  return V;
1141
9
}
1142
1143
// Returns a vector of globally-visible defined symbol names.
1144
19
std::vector<StringRef> LazyObjFile::getSymbolNames() {
1145
19
  if (isBitcode(this->MB))
1146
9
    return getBitcodeSymbols();
1147
10
1148
10
  switch (getELFKind(this->MB)) {
1149
10
  case ELF32LEKind:
1150
0
    return getElfSymbols<ELF32LE>();
1151
10
  case ELF32BEKind:
1152
0
    return getElfSymbols<ELF32BE>();
1153
10
  case ELF64LEKind:
1154
10
    return getElfSymbols<ELF64LE>();
1155
10
  case ELF64BEKind:
1156
0
    return getElfSymbols<ELF64BE>();
1157
10
  default:
1158
0
    llvm_unreachable("getELFKind");
1159
0
  }
1160
0
}
1161
1162
template void ArchiveFile::parse<ELF32LE>();
1163
template void ArchiveFile::parse<ELF32BE>();
1164
template void ArchiveFile::parse<ELF64LE>();
1165
template void ArchiveFile::parse<ELF64BE>();
1166
1167
template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1168
template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1169
template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1170
template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1171
1172
template void LazyObjFile::parse<ELF32LE>();
1173
template void LazyObjFile::parse<ELF32BE>();
1174
template void LazyObjFile::parse<ELF64LE>();
1175
template void LazyObjFile::parse<ELF64BE>();
1176
1177
template class elf::ELFFileBase<ELF32LE>;
1178
template class elf::ELFFileBase<ELF32BE>;
1179
template class elf::ELFFileBase<ELF64LE>;
1180
template class elf::ELFFileBase<ELF64BE>;
1181
1182
template class elf::ObjFile<ELF32LE>;
1183
template class elf::ObjFile<ELF32BE>;
1184
template class elf::ObjFile<ELF64LE>;
1185
template class elf::ObjFile<ELF64BE>;
1186
1187
template class elf::SharedFile<ELF32LE>;
1188
template class elf::SharedFile<ELF32BE>;
1189
template class elf::SharedFile<ELF64LE>;
1190
template class elf::SharedFile<ELF64BE>;