Coverage Report

Created: 2018-10-23 15:26

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