Coverage Report

Created: 2018-08-19 14:04

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