Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
12
#include "InputSection.h"
13
#include "LinkerScript.h"
14
#include "Memory.h"
15
#include "SymbolTable.h"
16
#include "Symbols.h"
17
#include "SyntheticSections.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/Path.h"
27
#include "llvm/Support/TarWriter.h"
28
#include "llvm/Support/raw_ostream.h"
29
30
using namespace llvm;
31
using namespace llvm::ELF;
32
using namespace llvm::object;
33
using namespace llvm::sys::fs;
34
35
using namespace lld;
36
using namespace lld::elf;
37
38
std::vector<BinaryFile *> elf::BinaryFiles;
39
std::vector<BitcodeFile *> elf::BitcodeFiles;
40
std::vector<InputFile *> elf::ObjectFiles;
41
std::vector<InputFile *> elf::SharedFiles;
42
43
TarWriter *elf::Tar;
44
45
2.54k
InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
46
47
2.95k
Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
48
2.95k
  // The --chroot option changes our virtual root directory.
49
2.95k
  // This is useful when you are dealing with files created by --reproduce.
50
2.95k
  if (
!Config->Chroot.empty() && 2.95k
Path.startswith("/")3
)
51
3
    Path = Saver.save(Config->Chroot + Path);
52
2.95k
53
2.95k
  log(Path);
54
2.95k
55
2.95k
  auto MBOrErr = MemoryBuffer::getFile(Path);
56
2.95k
  if (auto 
EC2.95k
= MBOrErr.getError()) {
57
87
    error("cannot open " + Path + ": " + EC.message());
58
87
    return None;
59
87
  }
60
2.86k
61
2.86k
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
62
2.86k
  MemoryBufferRef MBRef = MB->getMemBufferRef();
63
2.86k
  make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
64
2.86k
65
2.86k
  if (Tar)
66
16
    Tar->append(relativeToRoot(Path), MBRef.getBuffer());
67
2.95k
  return MBRef;
68
2.95k
}
69
70
122
template <class ELFT> void ObjFile<ELFT>::initializeDwarfLine() {
71
122
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
72
122
  const DWARFObject &Obj = Dwarf.getDWARFObj();
73
122
  DwarfLine.reset(new DWARFDebugLine);
74
122
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
75
122
                              Config->Wordsize);
76
122
77
122
  // The second parameter is offset in .debug_line section
78
122
  // for compilation unit (CU) of interest. We have only one
79
122
  // CU (object file), so offset is always 0.
80
122
  DwarfLine->getOrParseLineTable(LineData, 0);
81
122
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeDwarfLine()
Line
Count
Source
70
1
template <class ELFT> void ObjFile<ELFT>::initializeDwarfLine() {
71
1
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
72
1
  const DWARFObject &Obj = Dwarf.getDWARFObj();
73
1
  DwarfLine.reset(new DWARFDebugLine);
74
1
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
75
1
                              Config->Wordsize);
76
1
77
1
  // The second parameter is offset in .debug_line section
78
1
  // for compilation unit (CU) of interest. We have only one
79
1
  // CU (object file), so offset is always 0.
80
1
  DwarfLine->getOrParseLineTable(LineData, 0);
81
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeDwarfLine()
Line
Count
Source
70
1
template <class ELFT> void ObjFile<ELFT>::initializeDwarfLine() {
71
1
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
72
1
  const DWARFObject &Obj = Dwarf.getDWARFObj();
73
1
  DwarfLine.reset(new DWARFDebugLine);
74
1
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
75
1
                              Config->Wordsize);
76
1
77
1
  // The second parameter is offset in .debug_line section
78
1
  // for compilation unit (CU) of interest. We have only one
79
1
  // CU (object file), so offset is always 0.
80
1
  DwarfLine->getOrParseLineTable(LineData, 0);
81
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeDwarfLine()
Line
Count
Source
70
110
template <class ELFT> void ObjFile<ELFT>::initializeDwarfLine() {
71
110
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
72
110
  const DWARFObject &Obj = Dwarf.getDWARFObj();
73
110
  DwarfLine.reset(new DWARFDebugLine);
74
110
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
75
110
                              Config->Wordsize);
76
110
77
110
  // The second parameter is offset in .debug_line section
78
110
  // for compilation unit (CU) of interest. We have only one
79
110
  // CU (object file), so offset is always 0.
80
110
  DwarfLine->getOrParseLineTable(LineData, 0);
81
110
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeDwarfLine()
Line
Count
Source
70
10
template <class ELFT> void ObjFile<ELFT>::initializeDwarfLine() {
71
10
  DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
72
10
  const DWARFObject &Obj = Dwarf.getDWARFObj();
73
10
  DwarfLine.reset(new DWARFDebugLine);
74
10
  DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
75
10
                              Config->Wordsize);
76
10
77
10
  // The second parameter is offset in .debug_line section
78
10
  // for compilation unit (CU) of interest. We have only one
79
10
  // CU (object file), so offset is always 0.
80
10
  DwarfLine->getOrParseLineTable(LineData, 0);
81
10
}
82
83
// Returns source line information for a given offset
84
// using DWARF debug info.
85
template <class ELFT>
86
Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
87
167
                                                  uint64_t Offset) {
88
122
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
88
110
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
88
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
88
10
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
88
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
89
167
90
167
  // The offset to CU is 0.
91
167
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
92
167
  if (!Tbl)
93
0
    return None;
94
167
95
167
  // Use fake address calcuated by adding section file offset and offset in
96
167
  // section. See comments for ObjectInfo class.
97
167
  DILineInfo Info;
98
167
  Tbl->getFileLineInfoForAddress(
99
167
      S->getOffsetInFile() + Offset, nullptr,
100
167
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
101
167
  if (Info.Line == 0)
102
156
    return None;
103
11
  return Info;
104
11
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
87
1
                                                  uint64_t Offset) {
88
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
89
1
90
1
  // The offset to CU is 0.
91
1
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
92
1
  if (!Tbl)
93
0
    return None;
94
1
95
1
  // Use fake address calcuated by adding section file offset and offset in
96
1
  // section. See comments for ObjectInfo class.
97
1
  DILineInfo Info;
98
1
  Tbl->getFileLineInfoForAddress(
99
1
      S->getOffsetInFile() + Offset, nullptr,
100
1
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
101
1
  if (Info.Line == 0)
102
1
    return None;
103
0
  return Info;
104
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
87
15
                                                  uint64_t Offset) {
88
15
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
89
15
90
15
  // The offset to CU is 0.
91
15
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
92
15
  if (!Tbl)
93
0
    return None;
94
15
95
15
  // Use fake address calcuated by adding section file offset and offset in
96
15
  // section. See comments for ObjectInfo class.
97
15
  DILineInfo Info;
98
15
  Tbl->getFileLineInfoForAddress(
99
15
      S->getOffsetInFile() + Offset, nullptr,
100
15
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
101
15
  if (Info.Line == 0)
102
15
    return None;
103
0
  return Info;
104
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
87
1
                                                  uint64_t Offset) {
88
1
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
89
1
90
1
  // The offset to CU is 0.
91
1
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
92
1
  if (!Tbl)
93
0
    return None;
94
1
95
1
  // Use fake address calcuated by adding section file offset and offset in
96
1
  // section. See comments for ObjectInfo class.
97
1
  DILineInfo Info;
98
1
  Tbl->getFileLineInfoForAddress(
99
1
      S->getOffsetInFile() + Offset, nullptr,
100
1
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
101
1
  if (Info.Line == 0)
102
1
    return None;
103
0
  return Info;
104
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDILineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
87
150
                                                  uint64_t Offset) {
88
150
  llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
89
150
90
150
  // The offset to CU is 0.
91
150
  const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
92
150
  if (!Tbl)
93
0
    return None;
94
150
95
150
  // Use fake address calcuated by adding section file offset and offset in
96
150
  // section. See comments for ObjectInfo class.
97
150
  DILineInfo Info;
98
150
  Tbl->getFileLineInfoForAddress(
99
150
      S->getOffsetInFile() + Offset, nullptr,
100
150
      DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
101
150
  if (Info.Line == 0)
102
139
    return None;
103
11
  return Info;
104
11
}
105
106
// Returns source line information for a given offset
107
// using DWARF debug info.
108
template <class ELFT>
109
39
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
110
39
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
111
0
    return Info->FileName + ":" + std::to_string(Info->Line);
112
39
  return "";
113
39
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
109
1
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
110
1
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
111
0
    return Info->FileName + ":" + std::to_string(Info->Line);
112
1
  return "";
113
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
109
27
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
110
27
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
111
0
    return Info->FileName + ":" + std::to_string(Info->Line);
112
27
  return "";
113
27
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
109
1
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
110
1
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
111
0
    return Info->FileName + ":" + std::to_string(Info->Line);
112
1
  return "";
113
1
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getLineInfo(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
109
10
std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
110
10
  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
111
0
    return Info->FileName + ":" + std::to_string(Info->Line);
112
10
  return "";
113
10
}
114
115
// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
116
359k
std::string lld::toString(const InputFile *F) {
117
359k
  if (!F)
118
4.63k
    return "<internal>";
119
354k
120
354k
  
if (354k
F->ToStringCache.empty()354k
) {
121
2.41k
    if (F->ArchiveName.empty())
122
2.34k
      F->ToStringCache = F->getName();
123
2.41k
    else
124
77
      F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
125
2.41k
  }
126
359k
  return F->ToStringCache;
127
359k
}
128
129
template <class ELFT>
130
2.28k
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
131
2.28k
  if (ELFT::TargetEndianness == support::little)
132
2.08k
    
EKind = ELFT::Is64Bits ? 2.08k
ELF64LEKind1.81k
:
ELF32LEKind272
;
133
2.28k
  else
134
204
    
EKind = ELFT::Is64Bits ? 204
ELF64BEKind58
:
ELF32BEKind146
;
135
2.28k
136
2.28k
  EMachine = getObj().getHeader()->e_machine;
137
2.28k
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
138
2.28k
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
130
272
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
131
272
  if (ELFT::TargetEndianness == support::little)
132
272
    
EKind = ELFT::Is64Bits ? 272
ELF64LEKind0
:
ELF32LEKind272
;
133
272
  else
134
0
    
EKind = ELFT::Is64Bits ? 0
ELF64BEKind0
:
ELF32BEKind0
;
135
272
136
272
  EMachine = getObj().getHeader()->e_machine;
137
272
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
138
272
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
130
58
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
131
58
  if (ELFT::TargetEndianness == support::little)
132
0
    
EKind = ELFT::Is64Bits ? 0
ELF64LEKind0
:
ELF32LEKind0
;
133
58
  else
134
58
    
EKind = ELFT::Is64Bits ? 58
ELF64BEKind58
:
ELF32BEKind0
;
135
58
136
58
  EMachine = getObj().getHeader()->e_machine;
137
58
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
138
58
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
130
146
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
131
146
  if (ELFT::TargetEndianness == support::little)
132
0
    
EKind = ELFT::Is64Bits ? 0
ELF64LEKind0
:
ELF32LEKind0
;
133
146
  else
134
146
    
EKind = ELFT::Is64Bits ? 146
ELF64BEKind0
:
ELF32BEKind146
;
135
146
136
146
  EMachine = getObj().getHeader()->e_machine;
137
146
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
138
146
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >::ELFFileBase(lld::elf::InputFile::Kind, llvm::MemoryBufferRef)
Line
Count
Source
130
1.81k
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
131
1.81k
  if (ELFT::TargetEndianness == support::little)
132
1.81k
    
EKind = ELFT::Is64Bits ? 1.81k
ELF64LEKind1.81k
:
ELF32LEKind0
;
133
1.81k
  else
134
0
    
EKind = ELFT::Is64Bits ? 0
ELF64BEKind0
:
ELF32BEKind0
;
135
1.81k
136
1.81k
  EMachine = getObj().getHeader()->e_machine;
137
1.81k
  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
138
1.81k
}
139
140
template <class ELFT>
141
245
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
142
245
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
143
245
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getGlobalELFSyms()
Line
Count
Source
141
47
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
142
47
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
143
47
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getGlobalELFSyms()
Line
Count
Source
141
169
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
142
169
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
143
169
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getGlobalELFSyms()
Line
Count
Source
141
22
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
142
22
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
143
22
}
lld::elf::ELFFileBase<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getGlobalELFSyms()
Line
Count
Source
141
7
typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
142
7
  return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
143
7
}
144
145
template <class ELFT>
146
6.88k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147
6.88k
  return check(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
148
6.88k
               toString(this));
149
6.88k
}
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
146
1.10k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147
1.10k
  return check(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
148
1.10k
               toString(this));
149
1.10k
}
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
146
564
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147
564
  return check(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
148
564
               toString(this));
149
564
}
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
146
5.00k
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147
5.00k
  return check(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
148
5.00k
               toString(this));
149
5.00k
}
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
146
215
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147
215
  return check(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
148
215
               toString(this));
149
215
}
150
151
template <class ELFT>
152
void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
153
2.20k
                                   const Elf_Shdr *Symtab) {
154
2.20k
  FirstNonLocal = Symtab->sh_info;
155
2.20k
  ELFSyms = check(getObj().symbols(Symtab), toString(this));
156
2.20k
  if (
FirstNonLocal == 0 || 2.20k
FirstNonLocal > ELFSyms.size()2.20k
)
157
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
158
2.20k
159
2.20k
  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
160
2.20k
                      toString(this));
161
2.20k
}
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
153
58
                                   const Elf_Shdr *Symtab) {
154
58
  FirstNonLocal = Symtab->sh_info;
155
58
  ELFSyms = check(getObj().symbols(Symtab), toString(this));
156
58
  if (
FirstNonLocal == 0 || 58
FirstNonLocal > ELFSyms.size()58
)
157
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
158
58
159
58
  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
160
58
                      toString(this));
161
58
}
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
153
1.74k
                                   const Elf_Shdr *Symtab) {
154
1.74k
  FirstNonLocal = Symtab->sh_info;
155
1.74k
  ELFSyms = check(getObj().symbols(Symtab), toString(this));
156
1.74k
  if (
FirstNonLocal == 0 || 1.74k
FirstNonLocal > ELFSyms.size()1.74k
)
157
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
158
1.74k
159
1.74k
  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
160
1.74k
                      toString(this));
161
1.74k
}
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
153
143
                                   const Elf_Shdr *Symtab) {
154
143
  FirstNonLocal = Symtab->sh_info;
155
143
  ELFSyms = check(getObj().symbols(Symtab), toString(this));
156
143
  if (
FirstNonLocal == 0 || 143
FirstNonLocal > ELFSyms.size()143
)
157
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
158
143
159
143
  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
160
143
                      toString(this));
161
143
}
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
153
268
                                   const Elf_Shdr *Symtab) {
154
268
  FirstNonLocal = Symtab->sh_info;
155
268
  ELFSyms = check(getObj().symbols(Symtab), toString(this));
156
268
  if (
FirstNonLocal == 0 || 268
FirstNonLocal > ELFSyms.size()268
)
157
0
    fatal(toString(this) + ": invalid sh_info in symbol table");
158
268
159
268
  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
160
268
                      toString(this));
161
268
}
162
163
template <class ELFT>
164
ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
165
2.06k
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
166
2.06k
  this->ArchiveName = ArchiveName;
167
2.06k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
165
51
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
166
51
  this->ArchiveName = ArchiveName;
167
51
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
165
1.65k
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
166
1.65k
  this->ArchiveName = ArchiveName;
167
1.65k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
165
129
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
166
129
  this->ArchiveName = ArchiveName;
167
129
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::ObjFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
165
227
    : ELFFileBase<ELFT>(Base::ObjKind, M) {
166
227
  this->ArchiveName = ArchiveName;
167
227
}
168
169
1.90k
template <class ELFT> ArrayRef<SymbolBody *> ObjFile<ELFT>::getLocalSymbols() {
170
1.90k
  if (this->Symbols.empty())
171
1
    return {};
172
1.90k
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
173
1.90k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getLocalSymbols()
Line
Count
Source
169
221
template <class ELFT> ArrayRef<SymbolBody *> ObjFile<ELFT>::getLocalSymbols() {
170
221
  if (this->Symbols.empty())
171
0
    return {};
172
221
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
173
221
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getLocalSymbols()
Line
Count
Source
169
124
template <class ELFT> ArrayRef<SymbolBody *> ObjFile<ELFT>::getLocalSymbols() {
170
124
  if (this->Symbols.empty())
171
0
    return {};
172
124
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
173
124
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getLocalSymbols()
Line
Count
Source
169
50
template <class ELFT> ArrayRef<SymbolBody *> ObjFile<ELFT>::getLocalSymbols() {
170
50
  if (this->Symbols.empty())
171
0
    return {};
172
50
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
173
50
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getLocalSymbols()
Line
Count
Source
169
1.50k
template <class ELFT> ArrayRef<SymbolBody *> ObjFile<ELFT>::getLocalSymbols() {
170
1.50k
  if (this->Symbols.empty())
171
1
    return {};
172
1.50k
  return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
173
1.50k
}
174
175
template <class ELFT>
176
1.95k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
177
1.95k
  // Read section and symbol tables.
178
1.95k
  initializeSections(ComdatGroups);
179
1.95k
  initializeSymbols();
180
1.95k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
176
224
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
177
224
  // Read section and symbol tables.
178
224
  initializeSections(ComdatGroups);
179
224
  initializeSymbols();
180
224
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
176
126
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
177
126
  // Read section and symbol tables.
178
126
  initializeSections(ComdatGroups);
179
126
  initializeSymbols();
180
126
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
176
1.55k
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
177
1.55k
  // Read section and symbol tables.
178
1.55k
  initializeSections(ComdatGroups);
179
1.55k
  initializeSymbols();
180
1.55k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parse(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
176
51
void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
177
51
  // Read section and symbol tables.
178
51
  initializeSections(ComdatGroups);
179
51
  initializeSymbols();
180
51
}
181
182
// Sections with SHT_GROUP and comdat bits define comdat section groups.
183
// They are identified and deduplicated by group name. This function
184
// returns a group name.
185
template <class ELFT>
186
StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
187
48
                                              const Elf_Shdr &Sec) {
188
48
  // Group signatures are stored as symbol names in object files.
189
48
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
190
48
  if (this->ELFSyms.empty())
191
41
    this->initSymtab(
192
41
        Sections,
193
41
        check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
194
48
195
48
  const Elf_Sym *Sym = check(
196
48
      object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), toString(this));
197
48
  StringRef Signature = check(Sym->getName(this->StringTable), toString(this));
198
48
199
48
  // As a special case, if a symbol is a section symbol and has no name,
200
48
  // we use a section name as a signature.
201
48
  //
202
48
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
203
48
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
204
48
  // older produce such sections as outputs for the -r option, so we need
205
48
  // a bug-compatibility.
206
48
  if (
Signature.empty() && 48
Sym->getType() == STT_SECTION4
)
207
4
    return getSectionName(Sec);
208
44
  return Signature;
209
44
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getShtGroupSignature(llvm::ArrayRef<llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > >, llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Line
Count
Source
187
2
                                              const Elf_Shdr &Sec) {
188
2
  // Group signatures are stored as symbol names in object files.
189
2
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
190
2
  if (this->ELFSyms.empty())
191
1
    this->initSymtab(
192
1
        Sections,
193
1
        check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
194
2
195
2
  const Elf_Sym *Sym = check(
196
2
      object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), toString(this));
197
2
  StringRef Signature = check(Sym->getName(this->StringTable), toString(this));
198
2
199
2
  // As a special case, if a symbol is a section symbol and has no name,
200
2
  // we use a section name as a signature.
201
2
  //
202
2
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
203
2
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
204
2
  // older produce such sections as outputs for the -r option, so we need
205
2
  // a bug-compatibility.
206
2
  if (
Signature.empty() && 2
Sym->getType() == STT_SECTION0
)
207
0
    return getSectionName(Sec);
208
2
  return Signature;
209
2
}
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&)
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
187
46
                                              const Elf_Shdr &Sec) {
188
46
  // Group signatures are stored as symbol names in object files.
189
46
  // sh_info contains a symbol index, so we fetch a symbol and read its name.
190
46
  if (this->ELFSyms.empty())
191
40
    this->initSymtab(
192
40
        Sections,
193
40
        check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
194
46
195
46
  const Elf_Sym *Sym = check(
196
46
      object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), toString(this));
197
46
  StringRef Signature = check(Sym->getName(this->StringTable), toString(this));
198
46
199
46
  // As a special case, if a symbol is a section symbol and has no name,
200
46
  // we use a section name as a signature.
201
46
  //
202
46
  // Such SHT_GROUP sections are invalid from the perspective of the ELF
203
46
  // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
204
46
  // older produce such sections as outputs for the -r option, so we need
205
46
  // a bug-compatibility.
206
46
  if (
Signature.empty() && 46
Sym->getType() == STT_SECTION4
)
207
4
    return getSectionName(Sec);
208
42
  return Signature;
209
42
}
210
211
template <class ELFT>
212
ArrayRef<typename ObjFile<ELFT>::Elf_Word>
213
17
ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
214
17
  const ELFFile<ELFT> &Obj = this->getObj();
215
17
  ArrayRef<Elf_Word> Entries = check(
216
17
      Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
217
17
  if (
Entries.empty() || 17
Entries[0] != GRP_COMDAT17
)
218
0
    fatal(toString(this) + ": unsupported SHT_GROUP format");
219
17
  return Entries.slice(1);
220
17
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getShtGroupEntries(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&)
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
213
17
ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
214
17
  const ELFFile<ELFT> &Obj = this->getObj();
215
17
  ArrayRef<Elf_Word> Entries = check(
216
17
      Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
217
17
  if (
Entries.empty() || 17
Entries[0] != GRP_COMDAT17
)
218
0
    fatal(toString(this) + ": unsupported SHT_GROUP format");
219
17
  return Entries.slice(1);
220
17
}
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getShtGroupEntries(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&)
Unexecuted instantiation: lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getShtGroupEntries(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&)
221
222
200k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
223
200k
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
224
200k
  // the linker significantly faster, although the output will be bigger.
225
200k
  if (Config->Optimize == 0)
226
15
    return false;
227
200k
228
200k
  // Do not merge sections if generating a relocatable object. It makes
229
200k
  // the code simpler because we do not need to update relocation addends
230
200k
  // to reflect changes introduced by merging. Instead of that we write
231
200k
  // such "merge" sections into separate OutputSections and keep SHF_MERGE
232
200k
  // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
233
200k
  // later during a final linking.
234
200k
  
if (200k
Config->Relocatable200k
)
235
221
    return false;
236
200k
237
200k
  // A mergeable section with size 0 is useless because they don't have
238
200k
  // any data to merge. A mergeable string section with size 0 can be
239
200k
  // argued as invalid because it doesn't end with a null character.
240
200k
  // We'll avoid a mess by handling them as if they were non-mergeable.
241
200k
  
if (200k
Sec.sh_size == 0200k
)
242
197k
    return false;
243
2.98k
244
2.98k
  // Check for sh_entsize. The ELF spec is not clear about the zero
245
2.98k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
246
2.98k
  // the section does not hold a table of fixed-size entries". We know
247
2.98k
  // that Rust 1.13 produces a string mergeable section with a zero
248
2.98k
  // sh_entsize. Here we just accept it rather than being picky about it.
249
2.98k
  uint64_t EntSize = Sec.sh_entsize;
250
2.98k
  if (EntSize == 0)
251
2.59k
    return false;
252
390
  
if (390
Sec.sh_size % EntSize390
)
253
0
    fatal(toString(this) +
254
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
255
390
256
390
  uint64_t Flags = Sec.sh_flags;
257
390
  if (!(Flags & SHF_MERGE))
258
319
    return false;
259
71
  
if (71
Flags & SHF_WRITE71
)
260
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
261
71
262
71
  // Don't try to merge if the alignment is larger than the sh_entsize and this
263
71
  // is not SHF_STRINGS.
264
71
  //
265
71
  // Since this is not a SHF_STRINGS, we would need to pad after every entity.
266
71
  // It would be equivalent for the producer of the .o to just set a larger
267
71
  // sh_entsize.
268
71
  
if (71
Flags & SHF_STRINGS71
)
269
52
    return true;
270
19
271
19
  return Sec.sh_addralign <= EntSize;
272
19
}
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
222
522
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
223
522
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
224
522
  // the linker significantly faster, although the output will be bigger.
225
522
  if (Config->Optimize == 0)
226
0
    return false;
227
522
228
522
  // Do not merge sections if generating a relocatable object. It makes
229
522
  // the code simpler because we do not need to update relocation addends
230
522
  // to reflect changes introduced by merging. Instead of that we write
231
522
  // such "merge" sections into separate OutputSections and keep SHF_MERGE
232
522
  // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
233
522
  // later during a final linking.
234
522
  
if (522
Config->Relocatable522
)
235
26
    return false;
236
496
237
496
  // A mergeable section with size 0 is useless because they don't have
238
496
  // any data to merge. A mergeable string section with size 0 can be
239
496
  // argued as invalid because it doesn't end with a null character.
240
496
  // We'll avoid a mess by handling them as if they were non-mergeable.
241
496
  
if (496
Sec.sh_size == 0496
)
242
122
    return false;
243
374
244
374
  // Check for sh_entsize. The ELF spec is not clear about the zero
245
374
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
246
374
  // the section does not hold a table of fixed-size entries". We know
247
374
  // that Rust 1.13 produces a string mergeable section with a zero
248
374
  // sh_entsize. Here we just accept it rather than being picky about it.
249
374
  uint64_t EntSize = Sec.sh_entsize;
250
374
  if (EntSize == 0)
251
343
    return false;
252
31
  
if (31
Sec.sh_size % EntSize31
)
253
0
    fatal(toString(this) +
254
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
255
31
256
31
  uint64_t Flags = Sec.sh_flags;
257
31
  if (!(Flags & SHF_MERGE))
258
28
    return false;
259
3
  
if (3
Flags & SHF_WRITE3
)
260
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
261
3
262
3
  // Don't try to merge if the alignment is larger than the sh_entsize and this
263
3
  // is not SHF_STRINGS.
264
3
  //
265
3
  // Since this is not a SHF_STRINGS, we would need to pad after every entity.
266
3
  // It would be equivalent for the producer of the .o to just set a larger
267
3
  // sh_entsize.
268
3
  
if (3
Flags & SHF_STRINGS3
)
269
2
    return true;
270
1
271
1
  return Sec.sh_addralign <= EntSize;
272
1
}
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
222
233
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
223
233
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
224
233
  // the linker significantly faster, although the output will be bigger.
225
233
  if (Config->Optimize == 0)
226
0
    return false;
227
233
228
233
  // Do not merge sections if generating a relocatable object. It makes
229
233
  // the code simpler because we do not need to update relocation addends
230
233
  // to reflect changes introduced by merging. Instead of that we write
231
233
  // such "merge" sections into separate OutputSections and keep SHF_MERGE
232
233
  // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
233
233
  // later during a final linking.
234
233
  
if (233
Config->Relocatable233
)
235
7
    return false;
236
226
237
226
  // A mergeable section with size 0 is useless because they don't have
238
226
  // any data to merge. A mergeable string section with size 0 can be
239
226
  // argued as invalid because it doesn't end with a null character.
240
226
  // We'll avoid a mess by handling them as if they were non-mergeable.
241
226
  
if (226
Sec.sh_size == 0226
)
242
74
    return false;
243
152
244
152
  // Check for sh_entsize. The ELF spec is not clear about the zero
245
152
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
246
152
  // the section does not hold a table of fixed-size entries". We know
247
152
  // that Rust 1.13 produces a string mergeable section with a zero
248
152
  // sh_entsize. Here we just accept it rather than being picky about it.
249
152
  uint64_t EntSize = Sec.sh_entsize;
250
152
  if (EntSize == 0)
251
87
    return false;
252
65
  
if (65
Sec.sh_size % EntSize65
)
253
0
    fatal(toString(this) +
254
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
255
65
256
65
  uint64_t Flags = Sec.sh_flags;
257
65
  if (!(Flags & SHF_MERGE))
258
65
    return false;
259
0
  
if (0
Flags & SHF_WRITE0
)
260
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
261
0
262
0
  // Don't try to merge if the alignment is larger than the sh_entsize and this
263
0
  // is not SHF_STRINGS.
264
0
  //
265
0
  // Since this is not a SHF_STRINGS, we would need to pad after every entity.
266
0
  // It would be equivalent for the producer of the .o to just set a larger
267
0
  // sh_entsize.
268
0
  
if (0
Flags & SHF_STRINGS0
)
269
0
    return true;
270
0
271
0
  return Sec.sh_addralign <= EntSize;
272
0
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::shouldMerge(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&)
Line
Count
Source
222
198k
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
223
198k
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
224
198k
  // the linker significantly faster, although the output will be bigger.
225
198k
  if (Config->Optimize == 0)
226
15
    return false;
227
198k
228
198k
  // Do not merge sections if generating a relocatable object. It makes
229
198k
  // the code simpler because we do not need to update relocation addends
230
198k
  // to reflect changes introduced by merging. Instead of that we write
231
198k
  // such "merge" sections into separate OutputSections and keep SHF_MERGE
232
198k
  // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
233
198k
  // later during a final linking.
234
198k
  
if (198k
Config->Relocatable198k
)
235
118
    return false;
236
198k
237
198k
  // A mergeable section with size 0 is useless because they don't have
238
198k
  // any data to merge. A mergeable string section with size 0 can be
239
198k
  // argued as invalid because it doesn't end with a null character.
240
198k
  // We'll avoid a mess by handling them as if they were non-mergeable.
241
198k
  
if (198k
Sec.sh_size == 0198k
)
242
196k
    return false;
243
2.06k
244
2.06k
  // Check for sh_entsize. The ELF spec is not clear about the zero
245
2.06k
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
246
2.06k
  // the section does not hold a table of fixed-size entries". We know
247
2.06k
  // that Rust 1.13 produces a string mergeable section with a zero
248
2.06k
  // sh_entsize. Here we just accept it rather than being picky about it.
249
2.06k
  uint64_t EntSize = Sec.sh_entsize;
250
2.06k
  if (EntSize == 0)
251
1.99k
    return false;
252
71
  
if (71
Sec.sh_size % EntSize71
)
253
0
    fatal(toString(this) +
254
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
255
71
256
71
  uint64_t Flags = Sec.sh_flags;
257
71
  if (!(Flags & SHF_MERGE))
258
4
    return false;
259
67
  
if (67
Flags & SHF_WRITE67
)
260
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
261
67
262
67
  // Don't try to merge if the alignment is larger than the sh_entsize and this
263
67
  // is not SHF_STRINGS.
264
67
  //
265
67
  // Since this is not a SHF_STRINGS, we would need to pad after every entity.
266
67
  // It would be equivalent for the producer of the .o to just set a larger
267
67
  // sh_entsize.
268
67
  
if (67
Flags & SHF_STRINGS67
)
269
49
    return true;
270
18
271
18
  return Sec.sh_addralign <= EntSize;
272
18
}
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
222
670
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
223
670
  // We don't merge sections if -O0 (default is -O1). This makes sometimes
224
670
  // the linker significantly faster, although the output will be bigger.
225
670
  if (Config->Optimize == 0)
226
0
    return false;
227
670
228
670
  // Do not merge sections if generating a relocatable object. It makes
229
670
  // the code simpler because we do not need to update relocation addends
230
670
  // to reflect changes introduced by merging. Instead of that we write
231
670
  // such "merge" sections into separate OutputSections and keep SHF_MERGE
232
670
  // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
233
670
  // later during a final linking.
234
670
  
if (670
Config->Relocatable670
)
235
70
    return false;
236
600
237
600
  // A mergeable section with size 0 is useless because they don't have
238
600
  // any data to merge. A mergeable string section with size 0 can be
239
600
  // argued as invalid because it doesn't end with a null character.
240
600
  // We'll avoid a mess by handling them as if they were non-mergeable.
241
600
  
if (600
Sec.sh_size == 0600
)
242
204
    return false;
243
396
244
396
  // Check for sh_entsize. The ELF spec is not clear about the zero
245
396
  // sh_entsize. It says that "the member [sh_entsize] contains 0 if
246
396
  // the section does not hold a table of fixed-size entries". We know
247
396
  // that Rust 1.13 produces a string mergeable section with a zero
248
396
  // sh_entsize. Here we just accept it rather than being picky about it.
249
396
  uint64_t EntSize = Sec.sh_entsize;
250
396
  if (EntSize == 0)
251
173
    return false;
252
223
  
if (223
Sec.sh_size % EntSize223
)
253
0
    fatal(toString(this) +
254
0
          ": SHF_MERGE section size must be a multiple of sh_entsize");
255
223
256
223
  uint64_t Flags = Sec.sh_flags;
257
223
  if (!(Flags & SHF_MERGE))
258
222
    return false;
259
1
  
if (1
Flags & SHF_WRITE1
)
260
0
    fatal(toString(this) + ": writable SHF_MERGE section is not supported");
261
1
262
1
  // Don't try to merge if the alignment is larger than the sh_entsize and this
263
1
  // is not SHF_STRINGS.
264
1
  //
265
1
  // Since this is not a SHF_STRINGS, we would need to pad after every entity.
266
1
  // It would be equivalent for the producer of the .o to just set a larger
267
1
  // sh_entsize.
268
1
  
if (1
Flags & SHF_STRINGS1
)
269
1
    return true;
270
0
271
0
  return Sec.sh_addralign <= EntSize;
272
0
}
273
274
template <class ELFT>
275
void ObjFile<ELFT>::initializeSections(
276
1.95k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
277
1.95k
  const ELFFile<ELFT> &Obj = this->getObj();
278
1.95k
279
1.95k
  ArrayRef<Elf_Shdr> ObjSections =
280
1.95k
      check(this->getObj().sections(), toString(this));
281
1.95k
  uint64_t Size = ObjSections.size();
282
1.95k
  this->Sections.resize(Size);
283
1.95k
  this->SectionStringTable =
284
1.95k
      check(Obj.getSectionStringTable(ObjSections), toString(this));
285
1.95k
286
209k
  for (size_t I = 0, E = ObjSections.size(); 
I < E209k
;
I++207k
) {
287
207k
    if (this->Sections[I] == &InputSection::Discarded)
288
18
      continue;
289
207k
    const Elf_Shdr &Sec = ObjSections[I];
290
207k
291
207k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
292
207k
    // if -r is given, we'll let the final link discard such sections.
293
207k
    // This is compatible with GNU.
294
207k
    if (
(Sec.sh_flags & SHF_EXCLUDE) && 207k
!Config->Relocatable2
) {
295
1
      this->Sections[I] = &InputSection::Discarded;
296
1
      continue;
297
1
    }
298
207k
299
207k
    switch (Sec.sh_type) {
300
48
    case SHT_GROUP: {
301
48
      // De-duplicate section groups by their signatures.
302
48
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
303
48
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
304
48
      this->Sections[I] = &InputSection::Discarded;
305
48
306
48
      // If it is a new section group, we want to keep group members.
307
48
      // Group leader sections, which contain indices of group members, are
308
48
      // discarded because they are useless beyond this point. The only
309
48
      // exception is the -r option because in order to produce re-linkable
310
48
      // object files, we want to pass through basically everything.
311
48
      if (
IsNew48
) {
312
31
        if (Config->Relocatable)
313
8
          this->Sections[I] = createInputSection(Sec);
314
31
        continue;
315
31
      }
316
17
317
17
      // Otherwise, discard group members.
318
17
      
for (uint32_t SecIndex : getShtGroupEntries(Sec)) 17
{
319
18
        if (SecIndex >= Size)
320
0
          fatal(toString(this) +
321
0
                ": invalid section index in group: " + Twine(SecIndex));
322
18
        this->Sections[SecIndex] = &InputSection::Discarded;
323
18
      }
324
17
      break;
325
17
    }
326
1.95k
    case SHT_SYMTAB:
327
1.95k
      this->initSymtab(ObjSections, &Sec);
328
1.95k
      break;
329
3
    case SHT_SYMTAB_SHNDX:
330
3
      this->SymtabSHNDX =
331
3
          check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
332
3
      break;
333
3.94k
    case SHT_STRTAB:
334
3.94k
    case SHT_NULL:
335
3.94k
      break;
336
201k
    default:
337
201k
      this->Sections[I] = createInputSection(Sec);
338
207k
    }
339
207k
340
207k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
341
207k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
342
207k
    
if (207k
Sec.sh_flags & SHF_LINK_ORDER207k
) {
343
63
      if (Sec.sh_link >= this->Sections.size())
344
0
        fatal(toString(this) + ": invalid sh_link index: " +
345
0
              Twine(Sec.sh_link));
346
63
      this->Sections[Sec.sh_link]->DependentSections.push_back(
347
63
          this->Sections[I]);
348
63
    }
349
207k
  }
350
1.95k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
276
51
    DenseSet<CachedHashStringRef> &ComdatGroups) {
277
51
  const ELFFile<ELFT> &Obj = this->getObj();
278
51
279
51
  ArrayRef<Elf_Shdr> ObjSections =
280
51
      check(this->getObj().sections(), toString(this));
281
51
  uint64_t Size = ObjSections.size();
282
51
  this->Sections.resize(Size);
283
51
  this->SectionStringTable =
284
51
      check(Obj.getSectionStringTable(ObjSections), toString(this));
285
51
286
488
  for (size_t I = 0, E = ObjSections.size(); 
I < E488
;
I++437
) {
287
437
    if (this->Sections[I] == &InputSection::Discarded)
288
0
      continue;
289
437
    const Elf_Shdr &Sec = ObjSections[I];
290
437
291
437
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
292
437
    // if -r is given, we'll let the final link discard such sections.
293
437
    // This is compatible with GNU.
294
437
    if (
(Sec.sh_flags & SHF_EXCLUDE) && 437
!Config->Relocatable0
) {
295
0
      this->Sections[I] = &InputSection::Discarded;
296
0
      continue;
297
0
    }
298
437
299
437
    switch (Sec.sh_type) {
300
0
    case SHT_GROUP: {
301
0
      // De-duplicate section groups by their signatures.
302
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
303
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
304
0
      this->Sections[I] = &InputSection::Discarded;
305
0
306
0
      // If it is a new section group, we want to keep group members.
307
0
      // Group leader sections, which contain indices of group members, are
308
0
      // discarded because they are useless beyond this point. The only
309
0
      // exception is the -r option because in order to produce re-linkable
310
0
      // object files, we want to pass through basically everything.
311
0
      if (
IsNew0
) {
312
0
        if (Config->Relocatable)
313
0
          this->Sections[I] = createInputSection(Sec);
314
0
        continue;
315
0
      }
316
0
317
0
      // Otherwise, discard group members.
318
0
      
for (uint32_t SecIndex : getShtGroupEntries(Sec)) 0
{
319
0
        if (SecIndex >= Size)
320
0
          fatal(toString(this) +
321
0
                ": invalid section index in group: " + Twine(SecIndex));
322
0
        this->Sections[SecIndex] = &InputSection::Discarded;
323
0
      }
324
0
      break;
325
0
    }
326
51
    case SHT_SYMTAB:
327
51
      this->initSymtab(ObjSections, &Sec);
328
51
      break;
329
0
    case SHT_SYMTAB_SHNDX:
330
0
      this->SymtabSHNDX =
331
0
          check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
332
0
      break;
333
104
    case SHT_STRTAB:
334
104
    case SHT_NULL:
335
104
      break;
336
282
    default:
337
282
      this->Sections[I] = createInputSection(Sec);
338
437
    }
339
437
340
437
    // .ARM.exidx sections have a reverse dependency on the InputSection they
341
437
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
342
437
    
if (437
Sec.sh_flags & SHF_LINK_ORDER437
) {
343
0
      if (Sec.sh_link >= this->Sections.size())
344
0
        fatal(toString(this) + ": invalid sh_link index: " +
345
0
              Twine(Sec.sh_link));
346
0
      this->Sections[Sec.sh_link]->DependentSections.push_back(
347
0
          this->Sections[I]);
348
0
    }
349
437
  }
350
51
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
276
126
    DenseSet<CachedHashStringRef> &ComdatGroups) {
277
126
  const ELFFile<ELFT> &Obj = this->getObj();
278
126
279
126
  ArrayRef<Elf_Shdr> ObjSections =
280
126
      check(this->getObj().sections(), toString(this));
281
126
  uint64_t Size = ObjSections.size();
282
126
  this->Sections.resize(Size);
283
126
  this->SectionStringTable =
284
126
      check(Obj.getSectionStringTable(ObjSections), toString(this));
285
126
286
1.26k
  for (size_t I = 0, E = ObjSections.size(); 
I < E1.26k
;
I++1.14k
) {
287
1.14k
    if (this->Sections[I] == &InputSection::Discarded)
288
0
      continue;
289
1.14k
    const Elf_Shdr &Sec = ObjSections[I];
290
1.14k
291
1.14k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
292
1.14k
    // if -r is given, we'll let the final link discard such sections.
293
1.14k
    // This is compatible with GNU.
294
1.14k
    if (
(Sec.sh_flags & SHF_EXCLUDE) && 1.14k
!Config->Relocatable0
) {
295
0
      this->Sections[I] = &InputSection::Discarded;
296
0
      continue;
297
0
    }
298
1.14k
299
1.14k
    switch (Sec.sh_type) {
300
0
    case SHT_GROUP: {
301
0
      // De-duplicate section groups by their signatures.
302
0
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
303
0
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
304
0
      this->Sections[I] = &InputSection::Discarded;
305
0
306
0
      // If it is a new section group, we want to keep group members.
307
0
      // Group leader sections, which contain indices of group members, are
308
0
      // discarded because they are useless beyond this point. The only
309
0
      // exception is the -r option because in order to produce re-linkable
310
0
      // object files, we want to pass through basically everything.
311
0
      if (
IsNew0
) {
312
0
        if (Config->Relocatable)
313
0
          this->Sections[I] = createInputSection(Sec);
314
0
        continue;
315
0
      }
316
0
317
0
      // Otherwise, discard group members.
318
0
      
for (uint32_t SecIndex : getShtGroupEntries(Sec)) 0
{
319
0
        if (SecIndex >= Size)
320
0
          fatal(toString(this) +
321
0
                ": invalid section index in group: " + Twine(SecIndex));
322
0
        this->Sections[SecIndex] = &InputSection::Discarded;
323
0
      }
324
0
      break;
325
0
    }
326
126
    case SHT_SYMTAB:
327
126
      this->initSymtab(ObjSections, &Sec);
328
126
      break;
329
0
    case SHT_SYMTAB_SHNDX:
330
0
      this->SymtabSHNDX =
331
0
          check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
332
0
      break;
333
263
    case SHT_STRTAB:
334
263
    case SHT_NULL:
335
263
      break;
336
751
    default:
337
751
      this->Sections[I] = createInputSection(Sec);
338
1.14k
    }
339
1.14k
340
1.14k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
341
1.14k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
342
1.14k
    
if (1.14k
Sec.sh_flags & SHF_LINK_ORDER1.14k
) {
343
0
      if (Sec.sh_link >= this->Sections.size())
344
0
        fatal(toString(this) + ": invalid sh_link index: " +
345
0
              Twine(Sec.sh_link));
346
0
      this->Sections[Sec.sh_link]->DependentSections.push_back(
347
0
          this->Sections[I]);
348
0
    }
349
1.14k
  }
350
126
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
276
224
    DenseSet<CachedHashStringRef> &ComdatGroups) {
277
224
  const ELFFile<ELFT> &Obj = this->getObj();
278
224
279
224
  ArrayRef<Elf_Shdr> ObjSections =
280
224
      check(this->getObj().sections(), toString(this));
281
224
  uint64_t Size = ObjSections.size();
282
224
  this->Sections.resize(Size);
283
224
  this->SectionStringTable =
284
224
      check(Obj.getSectionStringTable(ObjSections), toString(this));
285
224
286
1.65k
  for (size_t I = 0, E = ObjSections.size(); 
I < E1.65k
;
I++1.42k
) {
287
1.42k
    if (this->Sections[I] == &InputSection::Discarded)
288
0
      continue;
289
1.42k
    const Elf_Shdr &Sec = ObjSections[I];
290
1.42k
291
1.42k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
292
1.42k
    // if -r is given, we'll let the final link discard such sections.
293
1.42k
    // This is compatible with GNU.
294
1.42k
    if (
(Sec.sh_flags & SHF_EXCLUDE) && 1.42k
!Config->Relocatable0
) {
295
0
      this->Sections[I] = &InputSection::Discarded;
296
0
      continue;
297
0
    }
298
1.42k
299
1.42k
    switch (Sec.sh_type) {
300
2
    case SHT_GROUP: {
301
2
      // De-duplicate section groups by their signatures.
302
2
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
303
2
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
304
2
      this->Sections[I] = &InputSection::Discarded;
305
2
306
2
      // If it is a new section group, we want to keep group members.
307
2
      // Group leader sections, which contain indices of group members, are
308
2
      // discarded because they are useless beyond this point. The only
309
2
      // exception is the -r option because in order to produce re-linkable
310
2
      // object files, we want to pass through basically everything.
311
2
      if (
IsNew2
) {
312
2
        if (Config->Relocatable)
313
0
          this->Sections[I] = createInputSection(Sec);
314
2
        continue;
315
2
      }
316
0
317
0
      // Otherwise, discard group members.
318
0
      
for (uint32_t SecIndex : getShtGroupEntries(Sec)) 0
{
319
0
        if (SecIndex >= Size)
320
0
          fatal(toString(this) +
321
0
                ": invalid section index in group: " + Twine(SecIndex));
322
0
        this->Sections[SecIndex] = &InputSection::Discarded;
323
0
      }
324
0
      break;
325
0
    }
326
224
    case SHT_SYMTAB:
327
224
      this->initSymtab(ObjSections, &Sec);
328
224
      break;
329
0
    case SHT_SYMTAB_SHNDX:
330
0
      this->SymtabSHNDX =
331
0
          check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
332
0
      break;
333
457
    case SHT_STRTAB:
334
457
    case SHT_NULL:
335
457
      break;
336
745
    default:
337
745
      this->Sections[I] = createInputSection(Sec);
338
1.42k
    }
339
1.42k
340
1.42k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
341
1.42k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
342
1.42k
    
if (1.42k
Sec.sh_flags & SHF_LINK_ORDER1.42k
) {
343
50
      if (Sec.sh_link >= this->Sections.size())
344
0
        fatal(toString(this) + ": invalid sh_link index: " +
345
0
              Twine(Sec.sh_link));
346
50
      this->Sections[Sec.sh_link]->DependentSections.push_back(
347
50
          this->Sections[I]);
348
50
    }
349
1.42k
  }
350
224
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSections(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
276
1.55k
    DenseSet<CachedHashStringRef> &ComdatGroups) {
277
1.55k
  const ELFFile<ELFT> &Obj = this->getObj();
278
1.55k
279
1.55k
  ArrayRef<Elf_Shdr> ObjSections =
280
1.55k
      check(this->getObj().sections(), toString(this));
281
1.55k
  uint64_t Size = ObjSections.size();
282
1.55k
  this->Sections.resize(Size);
283
1.55k
  this->SectionStringTable =
284
1.55k
      check(Obj.getSectionStringTable(ObjSections), toString(this));
285
1.55k
286
206k
  for (size_t I = 0, E = ObjSections.size(); 
I < E206k
;
I++204k
) {
287
204k
    if (this->Sections[I] == &InputSection::Discarded)
288
18
      continue;
289
204k
    const Elf_Shdr &Sec = ObjSections[I];
290
204k
291
204k
    // SHF_EXCLUDE'ed sections are discarded by the linker. However,
292
204k
    // if -r is given, we'll let the final link discard such sections.
293
204k
    // This is compatible with GNU.
294
204k
    if (
(Sec.sh_flags & SHF_EXCLUDE) && 204k
!Config->Relocatable2
) {
295
1
      this->Sections[I] = &InputSection::Discarded;
296
1
      continue;
297
1
    }
298
204k
299
204k
    switch (Sec.sh_type) {
300
46
    case SHT_GROUP: {
301
46
      // De-duplicate section groups by their signatures.
302
46
      StringRef Signature = getShtGroupSignature(ObjSections, Sec);
303
46
      bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
304
46
      this->Sections[I] = &InputSection::Discarded;
305
46
306
46
      // If it is a new section group, we want to keep group members.
307
46
      // Group leader sections, which contain indices of group members, are
308
46
      // discarded because they are useless beyond this point. The only
309
46
      // exception is the -r option because in order to produce re-linkable
310
46
      // object files, we want to pass through basically everything.
311
46
      if (
IsNew46
) {
312
29
        if (Config->Relocatable)
313
8
          this->Sections[I] = createInputSection(Sec);
314
29
        continue;
315
29
      }
316
17
317
17
      // Otherwise, discard group members.
318
17
      
for (uint32_t SecIndex : getShtGroupEntries(Sec)) 17
{
319
18
        if (SecIndex >= Size)
320
0
          fatal(toString(this) +
321
0
                ": invalid section index in group: " + Twine(SecIndex));
322
18
        this->Sections[SecIndex] = &InputSection::Discarded;
323
18
      }
324
17
      break;
325
17
    }
326
1.54k
    case SHT_SYMTAB:
327
1.54k
      this->initSymtab(ObjSections, &Sec);
328
1.54k
      break;
329
3
    case SHT_SYMTAB_SHNDX:
330
3
      this->SymtabSHNDX =
331
3
          check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
332
3
      break;
333
3.12k
    case SHT_STRTAB:
334
3.12k
    case SHT_NULL:
335
3.12k
      break;
336
199k
    default:
337
199k
      this->Sections[I] = createInputSection(Sec);
338
204k
    }
339
204k
340
204k
    // .ARM.exidx sections have a reverse dependency on the InputSection they
341
204k
    // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
342
204k
    
if (204k
Sec.sh_flags & SHF_LINK_ORDER204k
) {
343
13
      if (Sec.sh_link >= this->Sections.size())
344
0
        fatal(toString(this) + ": invalid sh_link index: " +
345
0
              Twine(Sec.sh_link));
346
13
      this->Sections[Sec.sh_link]->DependentSections.push_back(
347
13
          this->Sections[I]);
348
13
    }
349
204k
  }
350
1.55k
}
351
352
template <class ELFT>
353
1.05k
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
354
1.05k
  uint32_t Idx = Sec.sh_info;
355
1.05k
  if (Idx >= this->Sections.size())
356
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
357
1.05k
  InputSectionBase *Target = this->Sections[Idx];
358
1.05k
359
1.05k
  // Strictly speaking, a relocation section must be included in the
360
1.05k
  // group of the section it relocates. However, LLVM 3.3 and earlier
361
1.05k
  // would fail to do so, so we gracefully handle that case.
362
1.05k
  if (Target == &InputSection::Discarded)
363
12
    return nullptr;
364
1.04k
365
1.04k
  
if (1.04k
!Target1.04k
)
366
0
    fatal(toString(this) + ": unsupported relocation reference");
367
1.04k
  return Target;
368
1.04k
}
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
353
46
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
354
46
  uint32_t Idx = Sec.sh_info;
355
46
  if (Idx >= this->Sections.size())
356
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
357
46
  InputSectionBase *Target = this->Sections[Idx];
358
46
359
46
  // Strictly speaking, a relocation section must be included in the
360
46
  // group of the section it relocates. However, LLVM 3.3 and earlier
361
46
  // would fail to do so, so we gracefully handle that case.
362
46
  if (Target == &InputSection::Discarded)
363
0
    return nullptr;
364
46
365
46
  
if (46
!Target46
)
366
0
    fatal(toString(this) + ": unsupported relocation reference");
367
46
  return Target;
368
46
}
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
353
715
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
354
715
  uint32_t Idx = Sec.sh_info;
355
715
  if (Idx >= this->Sections.size())
356
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
357
715
  InputSectionBase *Target = this->Sections[Idx];
358
715
359
715
  // Strictly speaking, a relocation section must be included in the
360
715
  // group of the section it relocates. However, LLVM 3.3 and earlier
361
715
  // would fail to do so, so we gracefully handle that case.
362
715
  if (Target == &InputSection::Discarded)
363
12
    return nullptr;
364
703
365
703
  
if (703
!Target703
)
366
0
    fatal(toString(this) + ": unsupported relocation reference");
367
703
  return Target;
368
703
}
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
353
80
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
354
80
  uint32_t Idx = Sec.sh_info;
355
80
  if (Idx >= this->Sections.size())
356
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
357
80
  InputSectionBase *Target = this->Sections[Idx];
358
80
359
80
  // Strictly speaking, a relocation section must be included in the
360
80
  // group of the section it relocates. However, LLVM 3.3 and earlier
361
80
  // would fail to do so, so we gracefully handle that case.
362
80
  if (Target == &InputSection::Discarded)
363
0
    return nullptr;
364
80
365
80
  
if (80
!Target80
)
366
0
    fatal(toString(this) + ": unsupported relocation reference");
367
80
  return Target;
368
80
}
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
353
212
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
354
212
  uint32_t Idx = Sec.sh_info;
355
212
  if (Idx >= this->Sections.size())
356
0
    fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
357
212
  InputSectionBase *Target = this->Sections[Idx];
358
212
359
212
  // Strictly speaking, a relocation section must be included in the
360
212
  // group of the section it relocates. However, LLVM 3.3 and earlier
361
212
  // would fail to do so, so we gracefully handle that case.
362
212
  if (Target == &InputSection::Discarded)
363
0
    return nullptr;
364
212
365
212
  
if (212
!Target212
)
366
0
    fatal(toString(this) + ": unsupported relocation reference");
367
212
  return Target;
368
212
}
369
370
// Create a regular InputSection class that has the same contents
371
// as a given section.
372
1
InputSectionBase *toRegularSection(MergeInputSection *Sec) {
373
1
  auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment,
374
1
                                 Sec->Data, Sec->Name);
375
1
  Ret->File = Sec->File;
376
1
  return Ret;
377
1
}
378
379
template <class ELFT>
380
201k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
381
201k
  StringRef Name = getSectionName(Sec);
382
201k
383
201k
  switch (Sec.sh_type) {
384
7
  case SHT_ARM_ATTRIBUTES:
385
7
    // FIXME: ARM meta-data section. Retain the first attribute section
386
7
    // we see. The eglibc ARM dynamic loaders require the presence of an
387
7
    // attribute section for dlopen to work.
388
7
    // In a full implementation we would merge all attribute sections.
389
7
    if (
InX::ARMAttributes == nullptr7
) {
390
4
      InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
391
4
      return InX::ARMAttributes;
392
4
    }
393
3
    return &InputSection::Discarded;
394
1.05k
  case SHT_RELA:
395
1.05k
  case SHT_REL: {
396
1.05k
    // Find the relocation target section and associate this
397
1.05k
    // section with it. Target can be discarded, for example
398
1.05k
    // if it is a duplicated member of SHT_GROUP section, we
399
1.05k
    // do not create or proccess relocatable sections then.
400
1.05k
    InputSectionBase *Target = getRelocTarget(Sec);
401
1.05k
    if (!Target)
402
12
      return nullptr;
403
1.04k
404
1.04k
    // This section contains relocation information.
405
1.04k
    // If -r is given, we do not interpret or apply relocation
406
1.04k
    // but just copy relocation sections to output.
407
1.04k
    
if (1.04k
Config->Relocatable1.04k
)
408
55
      return make<InputSection>(this, &Sec, Name);
409
986
410
986
    
if (986
Target->FirstRelocation986
)
411
0
      fatal(toString(this) +
412
0
            ": multiple relocation sections to one section are not supported");
413
986
414
986
    // Mergeable sections with relocations are tricky because relocations
415
986
    // need to be taken into account when comparing section contents for
416
986
    // merging. It's not worth supporting such mergeable sections because
417
986
    // they are rare and it'd complicates the internal design (we usually
418
986
    // have to determine if two sections are mergeable early in the link
419
986
    // process much before applying relocations). We simply handle mergeable
420
986
    // sections with relocations as non-mergeable.
421
986
    
if (auto *986
MS986
= dyn_cast<MergeInputSection>(Target)) {
422
1
      Target = toRegularSection(MS);
423
1
      this->Sections[Sec.sh_info] = Target;
424
1
    }
425
986
426
986
    size_t NumRelocations;
427
986
    if (
Sec.sh_type == SHT_RELA986
) {
428
722
      ArrayRef<Elf_Rela> Rels =
429
722
          check(this->getObj().relas(&Sec), toString(this));
430
722
      Target->FirstRelocation = Rels.begin();
431
722
      NumRelocations = Rels.size();
432
722
      Target->AreRelocsRela = true;
433
986
    } else {
434
264
      ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
435
264
      Target->FirstRelocation = Rels.begin();
436
264
      NumRelocations = Rels.size();
437
264
      Target->AreRelocsRela = false;
438
264
    }
439
986
    assert(isUInt<31>(NumRelocations));
440
986
    Target->NumRelocations = NumRelocations;
441
986
442
986
    // Relocation sections processed by the linker are usually removed
443
986
    // from the output, so returning `nullptr` for the normal case.
444
986
    // However, if -emit-relocs is given, we need to leave them in the output.
445
986
    // (Some post link analysis tools need this information.)
446
986
    if (
Config->EmitRelocs986
) {
447
16
      InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
448
16
      // We will not emit relocation section if target was discarded.
449
16
      Target->DependentSections.push_back(RelocSec);
450
16
      return RelocSec;
451
16
    }
452
970
    return nullptr;
453
970
  }
454
200k
  }
455
200k
456
200k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
457
200k
  // that the code in the object file does not expect that the stack is
458
200k
  // executable (in terms of NX bit). If all input files have the marker,
459
200k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
460
200k
  // make the stack non-executable. Most object files have this section as
461
200k
  // of 2017.
462
200k
  //
463
200k
  // But making the stack non-executable is a norm today for security
464
200k
  // reasons. Failure to do so may result in a serious security issue.
465
200k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
466
200k
  // explicitly told to do otherwise (by -z execstack). Because the stack
467
200k
  // executable-ness is controlled solely by command line options,
468
200k
  // .note.GNU-stack sections are simply ignored.
469
200k
  
if (200k
Name == ".note.GNU-stack"200k
)
470
133
    return &InputSection::Discarded;
471
200k
472
200k
  // Split stacks is a feature to support a discontiguous stack. At least
473
200k
  // as of 2017, it seems that the feature is not being used widely.
474
200k
  // Only GNU gold supports that. We don't. For the details about that,
475
200k
  // see https://gcc.gnu.org/wiki/SplitStacks
476
200k
  
if (200k
Name == ".note.GNU-split-stack"200k
) {
477
1
    error(toString(this) +
478
1
          ": object file compiled with -fsplit-stack is not supported");
479
1
    return &InputSection::Discarded;
480
1
  }
481
200k
482
200k
  
if (200k
Config->Strip != StripPolicy::None && 200k
Name.startswith(".debug")17
)
483
12
    return &InputSection::Discarded;
484
200k
485
200k
  // If -gdb-index is given, LLD creates .gdb_index section, and that
486
200k
  // section serves the same purpose as .debug_gnu_pub{names,types} sections.
487
200k
  // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
488
200k
  // because they are redundant and can waste large amount of disk space
489
200k
  // (for example, they are about 400 MiB in total for a clang debug build.)
490
200k
  // We still create the section and mark it dead so that the gdb index code
491
200k
  // can use the InputSection to access the data.
492
200k
  
if (200k
Config->GdbIndex &&
493
200k
      
(Name == ".debug_gnu_pubnames" || 51
Name == ".debug_gnu_pubtypes"48
)) {
494
7
    auto *Ret = make<InputSection>(this, &Sec, Name);
495
7
    Script->discard({Ret});
496
7
    return Ret;
497
7
  }
498
200k
499
200k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
500
200k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
501
200k
  // sections. Drop those sections to avoid duplicate symbol errors.
502
200k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
503
200k
  // fixed for a while.
504
200k
  
if (200k
Name.startswith(".gnu.linkonce.")200k
)
505
2
    return &InputSection::Discarded;
506
200k
507
200k
  // The linker merges EH (exception handling) frames and creates a
508
200k
  // .eh_frame_hdr section for runtime. So we handle them with a special
509
200k
  // class. For relocatable outputs, they are just passed through.
510
200k
  
if (200k
Name == ".eh_frame" && 200k
!Config->Relocatable100
)
511
95
    return make<EhInputSection>(this, &Sec, Name);
512
200k
513
200k
  
if (200k
shouldMerge(Sec)200k
)
514
71
    return make<MergeInputSection>(this, &Sec, Name);
515
200k
  return make<InputSection>(this, &Sec, Name);
516
200k
}
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
380
199k
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
381
199k
  StringRef Name = getSectionName(Sec);
382
199k
383
199k
  switch (Sec.sh_type) {
384
0
  case SHT_ARM_ATTRIBUTES:
385
0
    // FIXME: ARM meta-data section. Retain the first attribute section
386
0
    // we see. The eglibc ARM dynamic loaders require the presence of an
387
0
    // attribute section for dlopen to work.
388
0
    // In a full implementation we would merge all attribute sections.
389
0
    if (
InX::ARMAttributes == nullptr0
) {
390
0
      InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
391
0
      return InX::ARMAttributes;
392
0
    }
393
0
    return &InputSection::Discarded;
394
715
  case SHT_RELA:
395
715
  case SHT_REL: {
396
715
    // Find the relocation target section and associate this
397
715
    // section with it. Target can be discarded, for example
398
715
    // if it is a duplicated member of SHT_GROUP section, we
399
715
    // do not create or proccess relocatable sections then.
400
715
    InputSectionBase *Target = getRelocTarget(Sec);
401
715
    if (!Target)
402
12
      return nullptr;
403
703
404
703
    // This section contains relocation information.
405
703
    // If -r is given, we do not interpret or apply relocation
406
703
    // but just copy relocation sections to output.
407
703
    
if (703
Config->Relocatable703
)
408
35
      return make<InputSection>(this, &Sec, Name);
409
668
410
668
    
if (668
Target->FirstRelocation668
)
411
0
      fatal(toString(this) +
412
0
            ": multiple relocation sections to one section are not supported");
413
668
414
668
    // Mergeable sections with relocations are tricky because relocations
415
668
    // need to be taken into account when comparing section contents for
416
668
    // merging. It's not worth supporting such mergeable sections because
417
668
    // they are rare and it'd complicates the internal design (we usually
418
668
    // have to determine if two sections are mergeable early in the link
419
668
    // process much before applying relocations). We simply handle mergeable
420
668
    // sections with relocations as non-mergeable.
421
668
    
if (auto *668
MS668
= dyn_cast<MergeInputSection>(Target)) {
422
1
      Target = toRegularSection(MS);
423
1
      this->Sections[Sec.sh_info] = Target;
424
1
    }
425
668
426
668
    size_t NumRelocations;
427
668
    if (
Sec.sh_type == SHT_RELA668
) {
428
667
      ArrayRef<Elf_Rela> Rels =
429
667
          check(this->getObj().relas(&Sec), toString(this));
430
667
      Target->FirstRelocation = Rels.begin();
431
667
      NumRelocations = Rels.size();
432
667
      Target->AreRelocsRela = true;
433
668
    } else {
434
1
      ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
435
1
      Target->FirstRelocation = Rels.begin();
436
1
      NumRelocations = Rels.size();
437
1
      Target->AreRelocsRela = false;
438
1
    }
439
668
    assert(isUInt<31>(NumRelocations));
440
668
    Target->NumRelocations = NumRelocations;
441
668
442
668
    // Relocation sections processed by the linker are usually removed
443
668
    // from the output, so returning `nullptr` for the normal case.
444
668
    // However, if -emit-relocs is given, we need to leave them in the output.
445
668
    // (Some post link analysis tools need this information.)
446
668
    if (
Config->EmitRelocs668
) {
447
16
      InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
448
16
      // We will not emit relocation section if target was discarded.
449
16
      Target->DependentSections.push_back(RelocSec);
450
16
      return RelocSec;
451
16
    }
452
652
    return nullptr;
453
652
  }
454
199k
  }
455
199k
456
199k
  // The GNU linker uses .note.GNU-stack section as a marker indicating
457
199k
  // that the code in the object file does not expect that the stack is
458
199k
  // executable (in terms of NX bit). If all input files have the marker,
459
199k
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
460
199k
  // make the stack non-executable. Most object files have this section as
461
199k
  // of 2017.
462
199k
  //
463
199k
  // But making the stack non-executable is a norm today for security
464
199k
  // reasons. Failure to do so may result in a serious security issue.
465
199k
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
466
199k
  // explicitly told to do otherwise (by -z execstack). Because the stack
467
199k
  // executable-ness is controlled solely by command line options,
468
199k
  // .note.GNU-stack sections are simply ignored.
469
199k
  
if (199k
Name == ".note.GNU-stack"199k
)
470
133
    return &InputSection::Discarded;
471
198k
472
198k
  // Split stacks is a feature to support a discontiguous stack. At least
473
198k
  // as of 2017, it seems that the feature is not being used widely.
474
198k
  // Only GNU gold supports that. We don't. For the details about that,
475
198k
  // see https://gcc.gnu.org/wiki/SplitStacks
476
198k
  
if (198k
Name == ".note.GNU-split-stack"198k
) {
477
1
    error(toString(this) +
478
1
          ": object file compiled with -fsplit-stack is not supported");
479
1
    return &InputSection::Discarded;
480
1
  }
481
198k
482
198k
  
if (198k
Config->Strip != StripPolicy::None && 198k
Name.startswith(".debug")17
)
483
12
    return &InputSection::Discarded;
484
198k
485
198k
  // If -gdb-index is given, LLD creates .gdb_index section, and that
486
198k
  // section serves the same purpose as .debug_gnu_pub{names,types} sections.
487
198k
  // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
488
198k
  // because they are redundant and can waste large amount of disk space
489
198k
  // (for example, they are about 400 MiB in total for a clang debug build.)
490
198k
  // We still create the section and mark it dead so that the gdb index code
491
198k
  // can use the InputSection to access the data.
492
198k
  
if (198k
Config->GdbIndex &&
493
198k
      
(Name == ".debug_gnu_pubnames" || 49
Name == ".debug_gnu_pubtypes"46
)) {
494
7
    auto *Ret = make<InputSection>(this, &Sec, Name);
495
7
    Script->discard({Ret});
496
7
    return Ret;
497
7
  }
498
198k
499
198k
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
500
198k
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
501
198k
  // sections. Drop those sections to avoid duplicate symbol errors.
502
198k
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
503
198k
  // fixed for a while.
504
198k
  
if (198k
Name.startswith(".gnu.linkonce.")198k
)
505
2
    return &InputSection::Discarded;
506
198k
507
198k
  // The linker merges EH (exception handling) frames and creates a
508
198k
  // .eh_frame_hdr section for runtime. So we handle them with a special
509
198k
  // class. For relocatable outputs, they are just passed through.
510
198k
  
if (198k
Name == ".eh_frame" && 198k
!Config->Relocatable92
)
511
87
    return make<EhInputSection>(this, &Sec, Name);
512
198k
513
198k
  
if (198k
shouldMerge(Sec)198k
)
514
67
    return make<MergeInputSection>(this, &Sec, Name);
515
198k
  return make<InputSection>(this, &Sec, Name);
516
198k
}
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
380
282
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
381
282
  StringRef Name = getSectionName(Sec);
382
282
383
282
  switch (Sec.sh_type) {
384
0
  case SHT_ARM_ATTRIBUTES:
385
0
    // FIXME: ARM meta-data section. Retain the first attribute section
386
0
    // we see. The eglibc ARM dynamic loaders require the presence of an
387
0
    // attribute section for dlopen to work.
388
0
    // In a full implementation we would merge all attribute sections.
389
0
    if (
InX::ARMAttributes == nullptr0
) {
390
0
      InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
391
0
      return InX::ARMAttributes;
392
0
    }
393
0
    return &InputSection::Discarded;
394
46
  case SHT_RELA:
395
46
  case SHT_REL: {
396
46
    // Find the relocation target section and associate this
397
46
    // section with it. Target can be discarded, for example
398
46
    // if it is a duplicated member of SHT_GROUP section, we
399
46
    // do not create or proccess relocatable sections then.
400
46
    InputSectionBase *Target = getRelocTarget(Sec);
401
46
    if (!Target)
402
0
      return nullptr;
403
46
404
46
    // This section contains relocation information.
405
46
    // If -r is given, we do not interpret or apply relocation
406
46
    // but just copy relocation sections to output.
407
46
    
if (46
Config->Relocatable46
)
408
0
      return make<InputSection>(this, &Sec, Name);
409
46
410
46
    
if (46
Target->FirstRelocation46
)
411
0
      fatal(toString(this) +
412
0
            ": multiple relocation sections to one section are not supported");
413
46
414
46
    // Mergeable sections with relocations are tricky because relocations
415
46
    // need to be taken into account when comparing section contents for
416
46
    // merging. It's not worth supporting such mergeable sections because
417
46
    // they are rare and it'd complicates the internal design (we usually
418
46
    // have to determine if two sections are mergeable early in the link
419
46
    // process much before applying relocations). We simply handle mergeable
420
46
    // sections with relocations as non-mergeable.
421
46
    
if (auto *46
MS46
= dyn_cast<MergeInputSection>(Target)) {
422
0
      Target = toRegularSection(MS);
423
0
      this->Sections[Sec.sh_info] = Target;
424
0
    }
425
46
426
46
    size_t NumRelocations;
427
46
    if (
Sec.sh_type == SHT_RELA46
) {
428
46
      ArrayRef<Elf_Rela> Rels =
429
46
          check(this->getObj().relas(&Sec), toString(this));
430
46
      Target->FirstRelocation = Rels.begin();
431
46
      NumRelocations = Rels.size();
432
46
      Target->AreRelocsRela = true;
433
46
    } else {
434
0
      ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
435
0
      Target->FirstRelocation = Rels.begin();
436
0
      NumRelocations = Rels.size();
437
0
      Target->AreRelocsRela = false;
438
0
    }
439
46
    assert(isUInt<31>(NumRelocations));
440
46
    Target->NumRelocations = NumRelocations;
441
46
442
46
    // Relocation sections processed by the linker are usually removed
443
46
    // from the output, so returning `nullptr` for the normal case.
444
46
    // However, if -emit-relocs is given, we need to leave them in the output.
445
46
    // (Some post link analysis tools need this information.)
446
46
    if (
Config->EmitRelocs46
) {
447
0
      InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
448
0
      // We will not emit relocation section if target was discarded.
449
0
      Target->DependentSections.push_back(RelocSec);
450
0
      return RelocSec;
451
0
    }
452
46
    return nullptr;
453
46
  }
454
236
  }
455
236
456
236
  // The GNU linker uses .note.GNU-stack section as a marker indicating
457
236
  // that the code in the object file does not expect that the stack is
458
236
  // executable (in terms of NX bit). If all input files have the marker,
459
236
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
460
236
  // make the stack non-executable. Most object files have this section as
461
236
  // of 2017.
462
236
  //
463
236
  // But making the stack non-executable is a norm today for security
464
236
  // reasons. Failure to do so may result in a serious security issue.
465
236
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
466
236
  // explicitly told to do otherwise (by -z execstack). Because the stack
467
236
  // executable-ness is controlled solely by command line options,
468
236
  // .note.GNU-stack sections are simply ignored.
469
236
  
if (236
Name == ".note.GNU-stack"236
)
470
0
    return &InputSection::Discarded;
471
236
472
236
  // Split stacks is a feature to support a discontiguous stack. At least
473
236
  // as of 2017, it seems that the feature is not being used widely.
474
236
  // Only GNU gold supports that. We don't. For the details about that,
475
236
  // see https://gcc.gnu.org/wiki/SplitStacks
476
236
  
if (236
Name == ".note.GNU-split-stack"236
) {
477
0
    error(toString(this) +
478
0
          ": object file compiled with -fsplit-stack is not supported");
479
0
    return &InputSection::Discarded;
480
0
  }
481
236
482
236
  
if (236
Config->Strip != StripPolicy::None && 236
Name.startswith(".debug")0
)
483
0
    return &InputSection::Discarded;
484
236
485
236
  // If -gdb-index is given, LLD creates .gdb_index section, and that
486
236
  // section serves the same purpose as .debug_gnu_pub{names,types} sections.
487
236
  // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
488
236
  // because they are redundant and can waste large amount of disk space
489
236
  // (for example, they are about 400 MiB in total for a clang debug build.)
490
236
  // We still create the section and mark it dead so that the gdb index code
491
236
  // can use the InputSection to access the data.
492
236
  
if (236
Config->GdbIndex &&
493
236
      
(Name == ".debug_gnu_pubnames" || 0
Name == ".debug_gnu_pubtypes"0
)) {
494
0
    auto *Ret = make<InputSection>(this, &Sec, Name);
495
0
    Script->discard({Ret});
496
0
    return Ret;
497
0
  }
498
236
499
236
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
500
236
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
501
236
  // sections. Drop those sections to avoid duplicate symbol errors.
502
236
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
503
236
  // fixed for a while.
504
236
  
if (236
Name.startswith(".gnu.linkonce.")236
)
505
0
    return &InputSection::Discarded;
506
236
507
236
  // The linker merges EH (exception handling) frames and creates a
508
236
  // .eh_frame_hdr section for runtime. So we handle them with a special
509
236
  // class. For relocatable outputs, they are just passed through.
510
236
  
if (236
Name == ".eh_frame" && 236
!Config->Relocatable3
)
511
3
    return make<EhInputSection>(this, &Sec, Name);
512
233
513
233
  
if (233
shouldMerge(Sec)233
)
514
0
    return make<MergeInputSection>(this, &Sec, Name);
515
233
  return make<InputSection>(this, &Sec, Name);
516
233
}
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
380
745
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
381
745
  StringRef Name = getSectionName(Sec);
382
745
383
745
  switch (Sec.sh_type) {
384
7
  case SHT_ARM_ATTRIBUTES:
385
7
    // FIXME: ARM meta-data section. Retain the first attribute section
386
7
    // we see. The eglibc ARM dynamic loaders require the presence of an
387
7
    // attribute section for dlopen to work.
388
7
    // In a full implementation we would merge all attribute sections.
389
7
    if (
InX::ARMAttributes == nullptr7
) {
390
4
      InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
391
4
      return InX::ARMAttributes;
392
4
    }
393
3
    return &InputSection::Discarded;
394
212
  case SHT_RELA:
395
212
  case SHT_REL: {
396
212
    // Find the relocation target section and associate this
397
212
    // section with it. Target can be discarded, for example
398
212
    // if it is a duplicated member of SHT_GROUP section, we
399
212
    // do not create or proccess relocatable sections then.
400
212
    InputSectionBase *Target = getRelocTarget(Sec);
401
212
    if (!Target)
402
0
      return nullptr;
403
212
404
212
    // This section contains relocation information.
405
212
    // If -r is given, we do not interpret or apply relocation
406
212
    // but just copy relocation sections to output.
407
212
    
if (212
Config->Relocatable212
)
408
12
      return make<InputSection>(this, &Sec, Name);
409
200
410
200
    
if (200
Target->FirstRelocation200
)
411
0
      fatal(toString(this) +
412
0
            ": multiple relocation sections to one section are not supported");
413
200
414
200
    // Mergeable sections with relocations are tricky because relocations
415
200
    // need to be taken into account when comparing section contents for
416
200
    // merging. It's not worth supporting such mergeable sections because
417
200
    // they are rare and it'd complicates the internal design (we usually
418
200
    // have to determine if two sections are mergeable early in the link
419
200
    // process much before applying relocations). We simply handle mergeable
420
200
    // sections with relocations as non-mergeable.
421
200
    
if (auto *200
MS200
= dyn_cast<MergeInputSection>(Target)) {
422
0
      Target = toRegularSection(MS);
423
0
      this->Sections[Sec.sh_info] = Target;
424
0
    }
425
200
426
200
    size_t NumRelocations;
427
200
    if (
Sec.sh_type == SHT_RELA200
) {
428
2
      ArrayRef<Elf_Rela> Rels =
429
2
          check(this->getObj().relas(&Sec), toString(this));
430
2
      Target->FirstRelocation = Rels.begin();
431
2
      NumRelocations = Rels.size();
432
2
      Target->AreRelocsRela = true;
433
200
    } else {
434
198
      ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
435
198
      Target->FirstRelocation = Rels.begin();
436
198
      NumRelocations = Rels.size();
437
198
      Target->AreRelocsRela = false;
438
198
    }
439
200
    assert(isUInt<31>(NumRelocations));
440
200
    Target->NumRelocations = NumRelocations;
441
200
442
200
    // Relocation sections processed by the linker are usually removed
443
200
    // from the output, so returning `nullptr` for the normal case.
444
200
    // However, if -emit-relocs is given, we need to leave them in the output.
445
200
    // (Some post link analysis tools need this information.)
446
200
    if (
Config->EmitRelocs200
) {
447
0
      InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
448
0
      // We will not emit relocation section if target was discarded.
449
0
      Target->DependentSections.push_back(RelocSec);
450
0
      return RelocSec;
451
0
    }
452
200
    return nullptr;
453
200
  }
454
526
  }
455
526
456
526
  // The GNU linker uses .note.GNU-stack section as a marker indicating
457
526
  // that the code in the object file does not expect that the stack is
458
526
  // executable (in terms of NX bit). If all input files have the marker,
459
526
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
460
526
  // make the stack non-executable. Most object files have this section as
461
526
  // of 2017.
462
526
  //
463
526
  // But making the stack non-executable is a norm today for security
464
526
  // reasons. Failure to do so may result in a serious security issue.
465
526
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
466
526
  // explicitly told to do otherwise (by -z execstack). Because the stack
467
526
  // executable-ness is controlled solely by command line options,
468
526
  // .note.GNU-stack sections are simply ignored.
469
526
  
if (526
Name == ".note.GNU-stack"526
)
470
0
    return &InputSection::Discarded;
471
526
472
526
  // Split stacks is a feature to support a discontiguous stack. At least
473
526
  // as of 2017, it seems that the feature is not being used widely.
474
526
  // Only GNU gold supports that. We don't. For the details about that,
475
526
  // see https://gcc.gnu.org/wiki/SplitStacks
476
526
  
if (526
Name == ".note.GNU-split-stack"526
) {
477
0
    error(toString(this) +
478
0
          ": object file compiled with -fsplit-stack is not supported");
479
0
    return &InputSection::Discarded;
480
0
  }
481
526
482
526
  
if (526
Config->Strip != StripPolicy::None && 526
Name.startswith(".debug")0
)
483
0
    return &InputSection::Discarded;
484
526
485
526
  // If -gdb-index is given, LLD creates .gdb_index section, and that
486
526
  // section serves the same purpose as .debug_gnu_pub{names,types} sections.
487
526
  // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
488
526
  // because they are redundant and can waste large amount of disk space
489
526
  // (for example, they are about 400 MiB in total for a clang debug build.)
490
526
  // We still create the section and mark it dead so that the gdb index code
491
526
  // can use the InputSection to access the data.
492
526
  
if (526
Config->GdbIndex &&
493
526
      
(Name == ".debug_gnu_pubnames" || 2
Name == ".debug_gnu_pubtypes"2
)) {
494
0
    auto *Ret = make<InputSection>(this, &Sec, Name);
495
0
    Script->discard({Ret});
496
0
    return Ret;
497
0
  }
498
526
499
526
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
500
526
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
501
526
  // sections. Drop those sections to avoid duplicate symbol errors.
502
526
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
503
526
  // fixed for a while.
504
526
  
if (526
Name.startswith(".gnu.linkonce.")526
)
505
0
    return &InputSection::Discarded;
506
526
507
526
  // The linker merges EH (exception handling) frames and creates a
508
526
  // .eh_frame_hdr section for runtime. So we handle them with a special
509
526
  // class. For relocatable outputs, they are just passed through.
510
526
  
if (526
Name == ".eh_frame" && 526
!Config->Relocatable4
)
511
4
    return make<EhInputSection>(this, &Sec, Name);
512
522
513
522
  
if (522
shouldMerge(Sec)522
)
514
3
    return make<MergeInputSection>(this, &Sec, Name);
515
519
  return make<InputSection>(this, &Sec, Name);
516
519
}
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
380
751
InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
381
751
  StringRef Name = getSectionName(Sec);
382
751
383
751
  switch (Sec.sh_type) {
384
0
  case SHT_ARM_ATTRIBUTES:
385
0
    // FIXME: ARM meta-data section. Retain the first attribute section
386
0
    // we see. The eglibc ARM dynamic loaders require the presence of an
387
0
    // attribute section for dlopen to work.
388
0
    // In a full implementation we would merge all attribute sections.
389
0
    if (
InX::ARMAttributes == nullptr0
) {
390
0
      InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
391
0
      return InX::ARMAttributes;
392
0
    }
393
0
    return &InputSection::Discarded;
394
80
  case SHT_RELA:
395
80
  case SHT_REL: {
396
80
    // Find the relocation target section and associate this
397
80
    // section with it. Target can be discarded, for example
398
80
    // if it is a duplicated member of SHT_GROUP section, we
399
80
    // do not create or proccess relocatable sections then.
400
80
    InputSectionBase *Target = getRelocTarget(Sec);
401
80
    if (!Target)
402
0
      return nullptr;
403
80
404
80
    // This section contains relocation information.
405
80
    // If -r is given, we do not interpret or apply relocation
406
80
    // but just copy relocation sections to output.
407
80
    
if (80
Config->Relocatable80
)
408
8
      return make<InputSection>(this, &Sec, Name);
409
72
410
72
    
if (72
Target->FirstRelocation72
)
411
0
      fatal(toString(this) +
412
0
            ": multiple relocation sections to one section are not supported");
413
72
414
72
    // Mergeable sections with relocations are tricky because relocations
415
72
    // need to be taken into account when comparing section contents for
416
72
    // merging. It's not worth supporting such mergeable sections because
417
72
    // they are rare and it'd complicates the internal design (we usually
418
72
    // have to determine if two sections are mergeable early in the link
419
72
    // process much before applying relocations). We simply handle mergeable
420
72
    // sections with relocations as non-mergeable.
421
72
    
if (auto *72
MS72
= dyn_cast<MergeInputSection>(Target)) {
422
0
      Target = toRegularSection(MS);
423
0
      this->Sections[Sec.sh_info] = Target;
424
0
    }
425
72
426
72
    size_t NumRelocations;
427
72
    if (
Sec.sh_type == SHT_RELA72
) {
428
7
      ArrayRef<Elf_Rela> Rels =
429
7
          check(this->getObj().relas(&Sec), toString(this));
430
7
      Target->FirstRelocation = Rels.begin();
431
7
      NumRelocations = Rels.size();
432
7
      Target->AreRelocsRela = true;
433
72
    } else {
434
65
      ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
435
65
      Target->FirstRelocation = Rels.begin();
436
65
      NumRelocations = Rels.size();
437
65
      Target->AreRelocsRela = false;
438
65
    }
439
72
    assert(isUInt<31>(NumRelocations));
440
72
    Target->NumRelocations = NumRelocations;
441
72
442
72
    // Relocation sections processed by the linker are usually removed
443
72
    // from the output, so returning `nullptr` for the normal case.
444
72
    // However, if -emit-relocs is given, we need to leave them in the output.
445
72
    // (Some post link analysis tools need this information.)
446
72
    if (
Config->EmitRelocs72
) {
447
0
      InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
448
0
      // We will not emit relocation section if target was discarded.
449
0
      Target->DependentSections.push_back(RelocSec);
450
0
      return RelocSec;
451
0
    }
452
72
    return nullptr;
453
72
  }
454
671
  }
455
671
456
671
  // The GNU linker uses .note.GNU-stack section as a marker indicating
457
671
  // that the code in the object file does not expect that the stack is
458
671
  // executable (in terms of NX bit). If all input files have the marker,
459
671
  // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
460
671
  // make the stack non-executable. Most object files have this section as
461
671
  // of 2017.
462
671
  //
463
671
  // But making the stack non-executable is a norm today for security
464
671
  // reasons. Failure to do so may result in a serious security issue.
465
671
  // Therefore, we make LLD always add PT_GNU_STACK unless it is
466
671
  // explicitly told to do otherwise (by -z execstack). Because the stack
467
671
  // executable-ness is controlled solely by command line options,
468
671
  // .note.GNU-stack sections are simply ignored.
469
671
  
if (671
Name == ".note.GNU-stack"671
)
470
0
    return &InputSection::Discarded;
471
671
472
671
  // Split stacks is a feature to support a discontiguous stack. At least
473
671
  // as of 2017, it seems that the feature is not being used widely.
474
671
  // Only GNU gold supports that. We don't. For the details about that,
475
671
  // see https://gcc.gnu.org/wiki/SplitStacks
476
671
  
if (671
Name == ".note.GNU-split-stack"671
) {
477
0
    error(toString(this) +
478
0
          ": object file compiled with -fsplit-stack is not supported");
479
0
    return &InputSection::Discarded;
480
0
  }
481
671
482
671
  
if (671
Config->Strip != StripPolicy::None && 671
Name.startswith(".debug")0
)
483
0
    return &InputSection::Discarded;
484
671
485
671
  // If -gdb-index is given, LLD creates .gdb_index section, and that
486
671
  // section serves the same purpose as .debug_gnu_pub{names,types} sections.
487
671
  // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
488
671
  // because they are redundant and can waste large amount of disk space
489
671
  // (for example, they are about 400 MiB in total for a clang debug build.)
490
671
  // We still create the section and mark it dead so that the gdb index code
491
671
  // can use the InputSection to access the data.
492
671
  
if (671
Config->GdbIndex &&
493
671
      
(Name == ".debug_gnu_pubnames" || 0
Name == ".debug_gnu_pubtypes"0
)) {
494
0
    auto *Ret = make<InputSection>(this, &Sec, Name);
495
0
    Script->discard({Ret});
496
0
    return Ret;
497
0
  }
498
671
499
671
  // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
500
671
  // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
501
671
  // sections. Drop those sections to avoid duplicate symbol errors.
502
671
  // FIXME: This is glibc PR20543, we should remove this hack once that has been
503
671
  // fixed for a while.
504
671
  
if (671
Name.startswith(".gnu.linkonce.")671
)
505
0
    return &InputSection::Discarded;
506
671
507
671
  // The linker merges EH (exception handling) frames and creates a
508
671
  // .eh_frame_hdr section for runtime. So we handle them with a special
509
671
  // class. For relocatable outputs, they are just passed through.
510
671
  
if (671
Name == ".eh_frame" && 671
!Config->Relocatable1
)
511
1
    return make<EhInputSection>(this, &Sec, Name);
512
670
513
670
  
if (670
shouldMerge(Sec)670
)
514
1
    return make<MergeInputSection>(this, &Sec, Name);
515
669
  return make<InputSection>(this, &Sec, Name);
516
669
}
517
518
template <class ELFT>
519
201k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
520
201k
  return check(this->getObj().getSectionName(&Sec, SectionStringTable),
521
201k
               toString(this));
522
201k
}
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
519
751
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
520
751
  return check(this->getObj().getSectionName(&Sec, SectionStringTable),
521
751
               toString(this));
522
751
}
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
519
745
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
520
745
  return check(this->getObj().getSectionName(&Sec, SectionStringTable),
521
745
               toString(this));
522
745
}
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
519
282
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
520
282
  return check(this->getObj().getSectionName(&Sec, SectionStringTable),
521
282
               toString(this));
522
282
}
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
519
199k
StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
520
199k
  return check(this->getObj().getSectionName(&Sec, SectionStringTable),
521
199k
               toString(this));
522
199k
}
523
524
1.95k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
525
1.95k
  this->Symbols.reserve(this->ELFSyms.size());
526
1.95k
  for (const Elf_Sym &Sym : this->ELFSyms)
527
6.86k
    this->Symbols.push_back(createSymbolBody(&Sym));
528
1.95k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::initializeSymbols()
Line
Count
Source
524
51
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
525
51
  this->Symbols.reserve(this->ELFSyms.size());
526
51
  for (const Elf_Sym &Sym : this->ELFSyms)
527
215
    this->Symbols.push_back(createSymbolBody(&Sym));
528
51
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::initializeSymbols()
Line
Count
Source
524
1.55k
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
525
1.55k
  this->Symbols.reserve(this->ELFSyms.size());
526
1.55k
  for (const Elf_Sym &Sym : this->ELFSyms)
527
4.98k
    this->Symbols.push_back(createSymbolBody(&Sym));
528
1.55k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::initializeSymbols()
Line
Count
Source
524
126
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
525
126
  this->Symbols.reserve(this->ELFSyms.size());
526
126
  for (const Elf_Sym &Sym : this->ELFSyms)
527
564
    this->Symbols.push_back(createSymbolBody(&Sym));
528
126
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::initializeSymbols()
Line
Count
Source
524
224
template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
525
224
  this->Symbols.reserve(this->ELFSyms.size());
526
224
  for (const Elf_Sym &Sym : this->ELFSyms)
527
1.10k
    this->Symbols.push_back(createSymbolBody(&Sym));
528
224
}
529
530
template <class ELFT>
531
6.86k
InputSectionBase *ObjFile<ELFT>::getSection(const Elf_Sym &Sym) const {
532
6.86k
  uint32_t Index = this->getSectionIndex(Sym);
533
6.86k
  if (Index >= this->Sections.size())
534
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
535
6.86k
  InputSectionBase *S = this->Sections[Index];
536
6.86k
537
6.86k
  // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
538
6.86k
  // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
539
6.86k
  // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
540
6.86k
  // In this case it is fine for section to be null here as we do not
541
6.86k
  // allocate sections of these types.
542
6.86k
  if (
!S6.86k
) {
543
3.25k
    if (
Index == 0 || 3.25k
Sym.getType() == STT_SECTION5
||
544
1
        Sym.getType() == STT_NOTYPE)
545
3.25k
      return nullptr;
546
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
547
0
  }
548
3.60k
549
3.60k
  
if (3.60k
S == &InputSection::Discarded3.60k
)
550
54
    return S;
551
3.55k
  return S->Repl;
552
3.55k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&) const
Line
Count
Source
531
1.10k
InputSectionBase *ObjFile<ELFT>::getSection(const Elf_Sym &Sym) const {
532
1.10k
  uint32_t Index = this->getSectionIndex(Sym);
533
1.10k
  if (Index >= this->Sections.size())
534
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
535
1.10k
  InputSectionBase *S = this->Sections[Index];
536
1.10k
537
1.10k
  // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
538
1.10k
  // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
539
1.10k
  // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
540
1.10k
  // In this case it is fine for section to be null here as we do not
541
1.10k
  // allocate sections of these types.
542
1.10k
  if (
!S1.10k
) {
543
402
    if (
Index == 0 || 402
Sym.getType() == STT_SECTION0
||
544
0
        Sym.getType() == STT_NOTYPE)
545
402
      return nullptr;
546
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
547
0
  }
548
699
549
699
  
if (699
S == &InputSection::Discarded699
)
550
0
    return S;
551
699
  return S->Repl;
552
699
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&) const
Line
Count
Source
531
564
InputSectionBase *ObjFile<ELFT>::getSection(const Elf_Sym &Sym) const {
532
564
  uint32_t Index = this->getSectionIndex(Sym);
533
564
  if (Index >= this->Sections.size())
534
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
535
564
  InputSectionBase *S = this->Sections[Index];
536
564
537
564
  // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
538
564
  // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
539
564
  // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
540
564
  // In this case it is fine for section to be null here as we do not
541
564
  // allocate sections of these types.
542
564
  if (
!S564
) {
543
206
    if (
Index == 0 || 206
Sym.getType() == STT_SECTION0
||
544
0
        Sym.getType() == STT_NOTYPE)
545
206
      return nullptr;
546
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
547
0
  }
548
358
549
358
  
if (358
S == &InputSection::Discarded358
)
550
0
    return S;
551
358
  return S->Repl;
552
358
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&) const
Line
Count
Source
531
215
InputSectionBase *ObjFile<ELFT>::getSection(const Elf_Sym &Sym) const {
532
215
  uint32_t Index = this->getSectionIndex(Sym);
533
215
  if (Index >= this->Sections.size())
534
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
535
215
  InputSectionBase *S = this->Sections[Index];
536
215
537
215
  // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
538
215
  // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
539
215
  // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
540
215
  // In this case it is fine for section to be null here as we do not
541
215
  // allocate sections of these types.
542
215
  if (
!S215
) {
543
76
    if (
Index == 0 || 76
Sym.getType() == STT_SECTION0
||
544
0
        Sym.getType() == STT_NOTYPE)
545
76
      return nullptr;
546
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
547
0
  }
548
139
549
139
  
if (139
S == &InputSection::Discarded139
)
550
0
    return S;
551
139
  return S->Repl;
552
139
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&) const
Line
Count
Source
531
4.98k
InputSectionBase *ObjFile<ELFT>::getSection(const Elf_Sym &Sym) const {
532
4.98k
  uint32_t Index = this->getSectionIndex(Sym);
533
4.98k
  if (Index >= this->Sections.size())
534
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
535
4.98k
  InputSectionBase *S = this->Sections[Index];
536
4.98k
537
4.98k
  // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
538
4.98k
  // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
539
4.98k
  // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
540
4.98k
  // In this case it is fine for section to be null here as we do not
541
4.98k
  // allocate sections of these types.
542
4.98k
  if (
!S4.98k
) {
543
2.57k
    if (
Index == 0 || 2.57k
Sym.getType() == STT_SECTION5
||
544
1
        Sym.getType() == STT_NOTYPE)
545
2.57k
      return nullptr;
546
0
    fatal(toString(this) + ": invalid section index: " + Twine(Index));
547
0
  }
548
2.40k
549
2.40k
  
if (2.40k
S == &InputSection::Discarded2.40k
)
550
54
    return S;
551
2.35k
  return S->Repl;
552
2.35k
}
553
554
template <class ELFT>
555
6.86k
SymbolBody *ObjFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
556
6.86k
  int Binding = Sym->getBinding();
557
6.86k
  InputSectionBase *Sec = getSection(*Sym);
558
6.86k
559
6.86k
  uint8_t StOther = Sym->st_other;
560
6.86k
  uint8_t Type = Sym->getType();
561
6.86k
  uint64_t Value = Sym->st_value;
562
6.86k
  uint64_t Size = Sym->st_size;
563
6.86k
564
6.86k
  if (
Binding == STB_LOCAL6.86k
) {
565
3.26k
    if (Sym->getType() == STT_FILE)
566
138
      SourceFile = check(Sym->getName(this->StringTable), toString(this));
567
3.26k
568
3.26k
    if (this->StringTable.size() <= Sym->st_name)
569
0
      fatal(toString(this) + ": invalid symbol name offset");
570
3.26k
571
3.26k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
572
3.26k
    if (Sym->st_shndx == SHN_UNDEF)
573
1.95k
      return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type);
574
1.31k
575
1.31k
    return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
576
1.31k
                                Size, Sec);
577
1.31k
  }
578
3.59k
579
3.59k
  StringRef Name = check(Sym->getName(this->StringTable), toString(this));
580
3.59k
581
3.59k
  switch (Sym->st_shndx) {
582
888
  case SHN_UNDEF:
583
888
    return Symtab
584
888
        ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
585
888
                             /*CanOmitFromDynSym=*/false, this)
586
888
        ->body();
587
111
  case SHN_COMMON:
588
111
    if (
Value == 0 || 111
Value >= UINT32_MAX111
)
589
0
      fatal(toString(this) + ": common symbol '" + Name +
590
0
            "' has invalid alignment: " + Twine(Value));
591
111
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
592
111
        ->body();
593
2.59k
  }
594
2.59k
595
2.59k
  switch (Binding) {
596
0
  default:
597
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
598
2.59k
  case STB_GLOBAL:
599
2.59k
  case STB_WEAK:
600
2.59k
  case STB_GNU_UNIQUE:
601
2.59k
    if (Sec == &InputSection::Discarded)
602
8
      return Symtab
603
8
          ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
604
8
                               /*CanOmitFromDynSym=*/false, this)
605
8
          ->body();
606
2.58k
    return Symtab
607
2.58k
        ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
608
2.58k
        ->body();
609
6.86k
  }
610
6.86k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::createSymbolBody(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*)
Line
Count
Source
555
1.10k
SymbolBody *ObjFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
556
1.10k
  int Binding = Sym->getBinding();
557
1.10k
  InputSectionBase *Sec = getSection(*Sym);
558
1.10k
559
1.10k
  uint8_t StOther = Sym->st_other;
560
1.10k
  uint8_t Type = Sym->getType();
561
1.10k
  uint64_t Value = Sym->st_value;
562
1.10k
  uint64_t Size = Sym->st_size;
563
1.10k
564
1.10k
  if (
Binding == STB_LOCAL1.10k
) {
565
565
    if (Sym->getType() == STT_FILE)
566
0
      SourceFile = check(Sym->getName(this->StringTable), toString(this));
567
565
568
565
    if (this->StringTable.size() <= Sym->st_name)
569
0
      fatal(toString(this) + ": invalid symbol name offset");
570
565
571
565
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
572
565
    if (Sym->st_shndx == SHN_UNDEF)
573
224
      return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type);
574
341
575
341
    return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
576
341
                                Size, Sec);
577
341
  }
578
536
579
536
  StringRef Name = check(Sym->getName(this->StringTable), toString(this));
580
536
581
536
  switch (Sym->st_shndx) {
582
132
  case SHN_UNDEF:
583
132
    return Symtab
584
132
        ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
585
132
                             /*CanOmitFromDynSym=*/false, this)
586
132
        ->body();
587
6
  case SHN_COMMON:
588
6
    if (
Value == 0 || 6
Value >= UINT32_MAX6
)
589
0
      fatal(toString(this) + ": common symbol '" + Name +
590
0
            "' has invalid alignment: " + Twine(Value));
591
6
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
592
6
        ->body();
593
398
  }
594
398
595
398
  switch (Binding) {
596
0
  default:
597
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
598
398
  case STB_GLOBAL:
599
398
  case STB_WEAK:
600
398
  case STB_GNU_UNIQUE:
601
398
    if (Sec == &InputSection::Discarded)
602
0
      return Symtab
603
0
          ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
604
0
                               /*CanOmitFromDynSym=*/false, this)
605
0
          ->body();
606
398
    return Symtab
607
398
        ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
608
398
        ->body();
609
1.10k
  }
610
1.10k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::createSymbolBody(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*)
Line
Count
Source
555
564
SymbolBody *ObjFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
556
564
  int Binding = Sym->getBinding();
557
564
  InputSectionBase *Sec = getSection(*Sym);
558
564
559
564
  uint8_t StOther = Sym->st_other;
560
564
  uint8_t Type = Sym->getType();
561
564
  uint64_t Value = Sym->st_value;
562
564
  uint64_t Size = Sym->st_size;
563
564
564
564
  if (
Binding == STB_LOCAL564
) {
565
241
    if (Sym->getType() == STT_FILE)
566
0
      SourceFile = check(Sym->getName(this->StringTable), toString(this));
567
241
568
241
    if (this->StringTable.size() <= Sym->st_name)
569
0
      fatal(toString(this) + ": invalid symbol name offset");
570
241
571
241
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
572
241
    if (Sym->st_shndx == SHN_UNDEF)
573
126
      return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type);
574
115
575
115
    return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
576
115
                                Size, Sec);
577
115
  }
578
323
579
323
  StringRef Name = check(Sym->getName(this->StringTable), toString(this));
580
323
581
323
  switch (Sym->st_shndx) {
582
79
  case SHN_UNDEF:
583
79
    return Symtab
584
79
        ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
585
79
                             /*CanOmitFromDynSym=*/false, this)
586
79
        ->body();
587
0
  case SHN_COMMON:
588
0
    if (
Value == 0 || 0
Value >= UINT32_MAX0
)
589
0
      fatal(toString(this) + ": common symbol '" + Name +
590
0
            "' has invalid alignment: " + Twine(Value));
591
0
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
592
0
        ->body();
593
244
  }
594
244
595
244
  switch (Binding) {
596
0
  default:
597
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
598
244
  case STB_GLOBAL:
599
244
  case STB_WEAK:
600
244
  case STB_GNU_UNIQUE:
601
244
    if (Sec == &InputSection::Discarded)
602
0
      return Symtab
603
0
          ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
604
0
                               /*CanOmitFromDynSym=*/false, this)
605
0
          ->body();
606
244
    return Symtab
607
244
        ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
608
244
        ->body();
609
564
  }
610
564
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::createSymbolBody(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*)
Line
Count
Source
555
4.98k
SymbolBody *ObjFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
556
4.98k
  int Binding = Sym->getBinding();
557
4.98k
  InputSectionBase *Sec = getSection(*Sym);
558
4.98k
559
4.98k
  uint8_t StOther = Sym->st_other;
560
4.98k
  uint8_t Type = Sym->getType();
561
4.98k
  uint64_t Value = Sym->st_value;
562
4.98k
  uint64_t Size = Sym->st_size;
563
4.98k
564
4.98k
  if (
Binding == STB_LOCAL4.98k
) {
565
2.36k
    if (Sym->getType() == STT_FILE)
566
138
      SourceFile = check(Sym->getName(this->StringTable), toString(this));
567
2.36k
568
2.36k
    if (this->StringTable.size() <= Sym->st_name)
569
0
      fatal(toString(this) + ": invalid symbol name offset");
570
2.36k
571
2.36k
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
572
2.36k
    if (Sym->st_shndx == SHN_UNDEF)
573
1.55k
      return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type);
574
815
575
815
    return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
576
815
                                Size, Sec);
577
815
  }
578
2.61k
579
2.61k
  StringRef Name = check(Sym->getName(this->StringTable), toString(this));
580
2.61k
581
2.61k
  switch (Sym->st_shndx) {
582
659
  case SHN_UNDEF:
583
659
    return Symtab
584
659
        ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
585
659
                             /*CanOmitFromDynSym=*/false, this)
586
659
        ->body();
587
103
  case SHN_COMMON:
588
103
    if (
Value == 0 || 103
Value >= UINT32_MAX103
)
589
0
      fatal(toString(this) + ": common symbol '" + Name +
590
0
            "' has invalid alignment: " + Twine(Value));
591
103
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
592
103
        ->body();
593
1.85k
  }
594
1.85k
595
1.85k
  switch (Binding) {
596
0
  default:
597
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
598
1.85k
  case STB_GLOBAL:
599
1.85k
  case STB_WEAK:
600
1.85k
  case STB_GNU_UNIQUE:
601
1.85k
    if (Sec == &InputSection::Discarded)
602
8
      return Symtab
603
8
          ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
604
8
                               /*CanOmitFromDynSym=*/false, this)
605
8
          ->body();
606
1.84k
    return Symtab
607
1.84k
        ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
608
1.84k
        ->body();
609
4.98k
  }
610
4.98k
}
lld::elf::ObjFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::createSymbolBody(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*)
Line
Count
Source
555
215
SymbolBody *ObjFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
556
215
  int Binding = Sym->getBinding();
557
215
  InputSectionBase *Sec = getSection(*Sym);
558
215
559
215
  uint8_t StOther = Sym->st_other;
560
215
  uint8_t Type = Sym->getType();
561
215
  uint64_t Value = Sym->st_value;
562
215
  uint64_t Size = Sym->st_size;
563
215
564
215
  if (
Binding == STB_LOCAL215
) {
565
93
    if (Sym->getType() == STT_FILE)
566
0
      SourceFile = check(Sym->getName(this->StringTable), toString(this));
567
93
568
93
    if (this->StringTable.size() <= Sym->st_name)
569
0
      fatal(toString(this) + ": invalid symbol name offset");
570
93
571
93
    StringRefZ Name = this->StringTable.data() + Sym->st_name;
572
93
    if (Sym->st_shndx == SHN_UNDEF)
573
51
      return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type);
574
42
575
42
    return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
576
42
                                Size, Sec);
577
42
  }
578
122
579
122
  StringRef Name = check(Sym->getName(this->StringTable), toString(this));
580
122
581
122
  switch (Sym->st_shndx) {
582
18
  case SHN_UNDEF:
583
18
    return Symtab
584
18
        ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
585
18
                             /*CanOmitFromDynSym=*/false, this)
586
18
        ->body();
587
2
  case SHN_COMMON:
588
2
    if (
Value == 0 || 2
Value >= UINT32_MAX2
)
589
0
      fatal(toString(this) + ": common symbol '" + Name +
590
0
            "' has invalid alignment: " + Twine(Value));
591
2
    return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
592
2
        ->body();
593
102
  }
594
102
595
102
  switch (Binding) {
596
0
  default:
597
0
    fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
598
102
  case STB_GLOBAL:
599
102
  case STB_WEAK:
600
102
  case STB_GNU_UNIQUE:
601
102
    if (Sec == &InputSection::Discarded)
602
0
      return Symtab
603
0
          ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
604
0
                               /*CanOmitFromDynSym=*/false, this)
605
0
          ->body();
606
102
    return Symtab
607
102
        ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
608
102
        ->body();
609
215
  }
610
215
}
611
612
ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
613
    : InputFile(ArchiveKind, File->getMemoryBufferRef()),
614
71
      File(std::move(File)) {}
615
616
69
template <class ELFT> void ArchiveFile::parse() {
617
69
  Symbols.reserve(File->getNumberOfSymbols());
618
69
  for (const Archive::Symbol &Sym : File->symbols())
619
137
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(this, Sym)->body());
620
69
}
Unexecuted instantiation: void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Unexecuted instantiation: void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
616
67
template <class ELFT> void ArchiveFile::parse() {
617
67
  Symbols.reserve(File->getNumberOfSymbols());
618
67
  for (const Archive::Symbol &Sym : File->symbols())
619
134
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(this, Sym)->body());
620
67
}
void lld::elf::ArchiveFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
616
2
template <class ELFT> void ArchiveFile::parse() {
617
2
  Symbols.reserve(File->getNumberOfSymbols());
618
2
  for (const Archive::Symbol &Sym : File->symbols())
619
3
    Symbols.push_back(Symtab->addLazyArchive<ELFT>(this, Sym)->body());
620
2
}
621
622
// Returns a buffer pointing to a member file containing a given symbol.
623
std::pair<MemoryBufferRef, uint64_t>
624
51
ArchiveFile::getMember(const Archive::Symbol *Sym) {
625
51
  Archive::Child C =
626
51
      check(Sym->getMember(), toString(this) +
627
51
                                  ": could not get the member for symbol " +
628
51
                                  Sym->getName());
629
51
630
51
  if (!Seen.insert(C.getChildOffset()).second)
631
0
    return {MemoryBufferRef(), 0};
632
51
633
51
  MemoryBufferRef Ret =
634
51
      check(C.getMemoryBufferRef(),
635
51
            toString(this) +
636
51
                ": could not get the buffer for the member defining symbol " +
637
51
                Sym->getName());
638
51
639
51
  if (
C.getParent()->isThin() && 51
Tar3
)
640
1
    Tar->append(relativeToRoot(check(C.getFullName(), toString(this))),
641
1
                Ret.getBuffer());
642
51
  if (C.getParent()->isThin())
643
3
    return {Ret, 0};
644
48
  return {Ret, C.getChildOffset()};
645
48
}
646
647
template <class ELFT>
648
SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
649
    : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
650
224
      AsNeeded(Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
650
45
      AsNeeded(Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
650
7
      AsNeeded(Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
650
17
      AsNeeded(Config->AsNeeded) {}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SharedFile(llvm::MemoryBufferRef, llvm::StringRef)
Line
Count
Source
650
155
      AsNeeded(Config->AsNeeded) {}
651
652
template <class ELFT>
653
const typename ELFT::Shdr *
654
29
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
655
29
  return check(
656
29
      this->getObj().getSection(&Sym, this->ELFSyms, this->SymtabSHNDX),
657
29
      toString(this));
658
29
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const&) const
Line
Count
Source
654
6
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
655
6
  return check(
656
6
      this->getObj().getSection(&Sym, this->ELFSyms, this->SymtabSHNDX),
657
6
      toString(this));
658
6
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const&) const
Line
Count
Source
654
5
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
655
5
  return check(
656
5
      this->getObj().getSection(&Sym, this->ELFSyms, this->SymtabSHNDX),
657
5
      toString(this));
658
5
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const&) const
Line
Count
Source
654
18
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
655
18
  return check(
656
18
      this->getObj().getSection(&Sym, this->ELFSyms, this->SymtabSHNDX),
657
18
      toString(this));
658
18
}
Unexecuted instantiation: lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSection(llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const&) const
659
660
// Partially parse the shared object file so that we can call
661
// getSoName on this object.
662
220
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
663
220
  const Elf_Shdr *DynamicSec = nullptr;
664
220
  const ELFFile<ELFT> Obj = this->getObj();
665
220
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
666
220
667
220
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
668
2.43k
  for (const Elf_Shdr &Sec : Sections) {
669
2.43k
    switch (Sec.sh_type) {
670
1.97k
    default:
671
1.97k
      continue;
672
218
    case SHT_DYNSYM:
673
218
      this->initSymtab(Sections, &Sec);
674
218
      break;
675
218
    case SHT_DYNAMIC:
676
218
      DynamicSec = &Sec;
677
218
      break;
678
0
    case SHT_SYMTAB_SHNDX:
679
0
      this->SymtabSHNDX =
680
0
          check(Obj.getSHNDXTable(Sec, Sections), toString(this));
681
0
      break;
682
13
    case SHT_GNU_versym:
683
13
      this->VersymSec = &Sec;
684
13
      break;
685
13
    case SHT_GNU_verdef:
686
13
      this->VerdefSec = &Sec;
687
13
      break;
688
220
    }
689
220
  }
690
220
691
220
  
if (220
this->VersymSec && 220
this->ELFSyms.empty()13
)
692
1
    error("SHT_GNU_versym should be associated with symbol table");
693
220
694
220
  // Search for a DT_SONAME tag to initialize this->SoName.
695
220
  if (!DynamicSec)
696
2
    return;
697
218
  ArrayRef<Elf_Dyn> Arr =
698
218
      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
699
218
            toString(this));
700
1.41k
  for (const Elf_Dyn &Dyn : Arr) {
701
1.41k
    if (
Dyn.d_tag == DT_SONAME1.41k
) {
702
41
      uint64_t Val = Dyn.getVal();
703
41
      if (Val >= this->StringTable.size())
704
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
705
41
      SoName = this->StringTable.data() + Val;
706
41
      return;
707
41
    }
708
1.41k
  }
709
220
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseSoName()
Line
Count
Source
662
153
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
663
153
  const Elf_Shdr *DynamicSec = nullptr;
664
153
  const ELFFile<ELFT> Obj = this->getObj();
665
153
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
666
153
667
153
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
668
1.62k
  for (const Elf_Shdr &Sec : Sections) {
669
1.62k
    switch (Sec.sh_type) {
670
1.29k
    default:
671
1.29k
      continue;
672
151
    case SHT_DYNSYM:
673
151
      this->initSymtab(Sections, &Sec);
674
151
      break;
675
151
    case SHT_DYNAMIC:
676
151
      DynamicSec = &Sec;
677
151
      break;
678
0
    case SHT_SYMTAB_SHNDX:
679
0
      this->SymtabSHNDX =
680
0
          check(Obj.getSHNDXTable(Sec, Sections), toString(this));
681
0
      break;
682
13
    case SHT_GNU_versym:
683
13
      this->VersymSec = &Sec;
684
13
      break;
685
13
    case SHT_GNU_verdef:
686
13
      this->VerdefSec = &Sec;
687
13
      break;
688
153
    }
689
153
  }
690
153
691
153
  
if (153
this->VersymSec && 153
this->ELFSyms.empty()13
)
692
1
    error("SHT_GNU_versym should be associated with symbol table");
693
153
694
153
  // Search for a DT_SONAME tag to initialize this->SoName.
695
153
  if (!DynamicSec)
696
2
    return;
697
151
  ArrayRef<Elf_Dyn> Arr =
698
151
      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
699
151
            toString(this));
700
902
  for (const Elf_Dyn &Dyn : Arr) {
701
902
    if (
Dyn.d_tag == DT_SONAME902
) {
702
26
      uint64_t Val = Dyn.getVal();
703
26
      if (Val >= this->StringTable.size())
704
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
705
26
      SoName = this->StringTable.data() + Val;
706
26
      return;
707
26
    }
708
902
  }
709
153
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseSoName()
Line
Count
Source
662
7
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
663
7
  const Elf_Shdr *DynamicSec = nullptr;
664
7
  const ELFFile<ELFT> Obj = this->getObj();
665
7
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
666
7
667
7
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
668
100
  for (const Elf_Shdr &Sec : Sections) {
669
100
    switch (Sec.sh_type) {
670
86
    default:
671
86
      continue;
672
7
    case SHT_DYNSYM:
673
7
      this->initSymtab(Sections, &Sec);
674
7
      break;
675
7
    case SHT_DYNAMIC:
676
7
      DynamicSec = &Sec;
677
7
      break;
678
0
    case SHT_SYMTAB_SHNDX:
679
0
      this->SymtabSHNDX =
680
0
          check(Obj.getSHNDXTable(Sec, Sections), toString(this));
681
0
      break;
682
0
    case SHT_GNU_versym:
683
0
      this->VersymSec = &Sec;
684
0
      break;
685
0
    case SHT_GNU_verdef:
686
0
      this->VerdefSec = &Sec;
687
0
      break;
688
7
    }
689
7
  }
690
7
691
7
  
if (7
this->VersymSec && 7
this->ELFSyms.empty()0
)
692
0
    error("SHT_GNU_versym should be associated with symbol table");
693
7
694
7
  // Search for a DT_SONAME tag to initialize this->SoName.
695
7
  if (!DynamicSec)
696
0
    return;
697
7
  ArrayRef<Elf_Dyn> Arr =
698
7
      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
699
7
            toString(this));
700
81
  for (const Elf_Dyn &Dyn : Arr) {
701
81
    if (
Dyn.d_tag == DT_SONAME81
) {
702
0
      uint64_t Val = Dyn.getVal();
703
0
      if (Val >= this->StringTable.size())
704
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
705
0
      SoName = this->StringTable.data() + Val;
706
0
      return;
707
0
    }
708
81
  }
709
7
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseSoName()
Line
Count
Source
662
43
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
663
43
  const Elf_Shdr *DynamicSec = nullptr;
664
43
  const ELFFile<ELFT> Obj = this->getObj();
665
43
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
666
43
667
43
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
668
456
  for (const Elf_Shdr &Sec : Sections) {
669
456
    switch (Sec.sh_type) {
670
370
    default:
671
370
      continue;
672
43
    case SHT_DYNSYM:
673
43
      this->initSymtab(Sections, &Sec);
674
43
      break;
675
43
    case SHT_DYNAMIC:
676
43
      DynamicSec = &Sec;
677
43
      break;
678
0
    case SHT_SYMTAB_SHNDX:
679
0
      this->SymtabSHNDX =
680
0
          check(Obj.getSHNDXTable(Sec, Sections), toString(this));
681
0
      break;
682
0
    case SHT_GNU_versym:
683
0
      this->VersymSec = &Sec;
684
0
      break;
685
0
    case SHT_GNU_verdef:
686
0
      this->VerdefSec = &Sec;
687
0
      break;
688
43
    }
689
43
  }
690
43
691
43
  
if (43
this->VersymSec && 43
this->ELFSyms.empty()0
)
692
0
    error("SHT_GNU_versym should be associated with symbol table");
693
43
694
43
  // Search for a DT_SONAME tag to initialize this->SoName.
695
43
  if (!DynamicSec)
696
0
    return;
697
43
  ArrayRef<Elf_Dyn> Arr =
698
43
      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
699
43
            toString(this));
700
206
  for (const Elf_Dyn &Dyn : Arr) {
701
206
    if (
Dyn.d_tag == DT_SONAME206
) {
702
15
      uint64_t Val = Dyn.getVal();
703
15
      if (Val >= this->StringTable.size())
704
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
705
15
      SoName = this->StringTable.data() + Val;
706
15
      return;
707
15
    }
708
206
  }
709
43
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseSoName()
Line
Count
Source
662
17
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
663
17
  const Elf_Shdr *DynamicSec = nullptr;
664
17
  const ELFFile<ELFT> Obj = this->getObj();
665
17
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
666
17
667
17
  // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
668
256
  for (const Elf_Shdr &Sec : Sections) {
669
256
    switch (Sec.sh_type) {
670
222
    default:
671
222
      continue;
672
17
    case SHT_DYNSYM:
673
17
      this->initSymtab(Sections, &Sec);
674
17
      break;
675
17
    case SHT_DYNAMIC:
676
17
      DynamicSec = &Sec;
677
17
      break;
678
0
    case SHT_SYMTAB_SHNDX:
679
0
      this->SymtabSHNDX =
680
0
          check(Obj.getSHNDXTable(Sec, Sections), toString(this));
681
0
      break;
682
0
    case SHT_GNU_versym:
683
0
      this->VersymSec = &Sec;
684
0
      break;
685
0
    case SHT_GNU_verdef:
686
0
      this->VerdefSec = &Sec;
687
0
      break;
688
17
    }
689
17
  }
690
17
691
17
  
if (17
this->VersymSec && 17
this->ELFSyms.empty()0
)
692
0
    error("SHT_GNU_versym should be associated with symbol table");
693
17
694
17
  // Search for a DT_SONAME tag to initialize this->SoName.
695
17
  if (!DynamicSec)
696
0
    return;
697
17
  ArrayRef<Elf_Dyn> Arr =
698
17
      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
699
17
            toString(this));
700
221
  for (const Elf_Dyn &Dyn : Arr) {
701
221
    if (
Dyn.d_tag == DT_SONAME221
) {
702
0
      uint64_t Val = Dyn.getVal();
703
0
      if (Val >= this->StringTable.size())
704
0
        fatal(toString(this) + ": invalid DT_SONAME entry");
705
0
      SoName = this->StringTable.data() + Val;
706
0
      return;
707
0
    }
708
221
  }
709
17
}
710
711
// Parse the version definitions in the object file if present. Returns a vector
712
// whose nth element contains a pointer to the Elf_Verdef for version identifier
713
// n. Version identifiers that are not definitions map to nullptr. The array
714
// always has at least length 1.
715
template <class ELFT>
716
std::vector<const typename ELFT::Verdef *>
717
216
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
718
216
  std::vector<const Elf_Verdef *> Verdefs(1);
719
216
  // We only need to process symbol versions for this DSO if it has both a
720
216
  // versym and a verdef section, which indicates that the DSO contains symbol
721
216
  // version definitions.
722
216
  if (
!VersymSec || 216
!VerdefSec12
)
723
204
    return Verdefs;
724
12
725
12
  // The location of the first global versym entry.
726
12
  const char *Base = this->MB.getBuffer().data();
727
12
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
728
12
           this->FirstNonLocal;
729
12
730
12
  // We cannot determine the largest verdef identifier without inspecting
731
12
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
732
12
  // sequentially starting from 1, so we predict that the largest identifier
733
12
  // will be VerdefCount.
734
12
  unsigned VerdefCount = VerdefSec->sh_info;
735
12
  Verdefs.resize(VerdefCount + 1);
736
12
737
12
  // Build the Verdefs array by following the chain of Elf_Verdef objects
738
12
  // from the start of the .gnu.version_d section.
739
12
  const char *Verdef = Base + VerdefSec->sh_offset;
740
47
  for (unsigned I = 0; 
I != VerdefCount47
;
++I35
) {
741
35
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
742
35
    Verdef += CurVerdef->vd_next;
743
35
    unsigned VerdefIndex = CurVerdef->vd_ndx;
744
35
    if (Verdefs.size() <= VerdefIndex)
745
0
      Verdefs.resize(VerdefIndex + 1);
746
35
    Verdefs[VerdefIndex] = CurVerdef;
747
35
  }
748
216
749
216
  return Verdefs;
750
216
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*&)
Line
Count
Source
717
151
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
718
151
  std::vector<const Elf_Verdef *> Verdefs(1);
719
151
  // We only need to process symbol versions for this DSO if it has both a
720
151
  // versym and a verdef section, which indicates that the DSO contains symbol
721
151
  // version definitions.
722
151
  if (
!VersymSec || 151
!VerdefSec12
)
723
139
    return Verdefs;
724
12
725
12
  // The location of the first global versym entry.
726
12
  const char *Base = this->MB.getBuffer().data();
727
12
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
728
12
           this->FirstNonLocal;
729
12
730
12
  // We cannot determine the largest verdef identifier without inspecting
731
12
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
732
12
  // sequentially starting from 1, so we predict that the largest identifier
733
12
  // will be VerdefCount.
734
12
  unsigned VerdefCount = VerdefSec->sh_info;
735
12
  Verdefs.resize(VerdefCount + 1);
736
12
737
12
  // Build the Verdefs array by following the chain of Elf_Verdef objects
738
12
  // from the start of the .gnu.version_d section.
739
12
  const char *Verdef = Base + VerdefSec->sh_offset;
740
47
  for (unsigned I = 0; 
I != VerdefCount47
;
++I35
) {
741
35
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
742
35
    Verdef += CurVerdef->vd_next;
743
35
    unsigned VerdefIndex = CurVerdef->vd_ndx;
744
35
    if (Verdefs.size() <= VerdefIndex)
745
0
      Verdefs.resize(VerdefIndex + 1);
746
35
    Verdefs[VerdefIndex] = CurVerdef;
747
35
  }
748
151
749
151
  return Verdefs;
750
151
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*&)
Line
Count
Source
717
41
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
718
41
  std::vector<const Elf_Verdef *> Verdefs(1);
719
41
  // We only need to process symbol versions for this DSO if it has both a
720
41
  // versym and a verdef section, which indicates that the DSO contains symbol
721
41
  // version definitions.
722
41
  if (
!VersymSec || 41
!VerdefSec0
)
723
41
    return Verdefs;
724
0
725
0
  // The location of the first global versym entry.
726
0
  const char *Base = this->MB.getBuffer().data();
727
0
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
728
0
           this->FirstNonLocal;
729
0
730
0
  // We cannot determine the largest verdef identifier without inspecting
731
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
732
0
  // sequentially starting from 1, so we predict that the largest identifier
733
0
  // will be VerdefCount.
734
0
  unsigned VerdefCount = VerdefSec->sh_info;
735
0
  Verdefs.resize(VerdefCount + 1);
736
0
737
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
738
0
  // from the start of the .gnu.version_d section.
739
0
  const char *Verdef = Base + VerdefSec->sh_offset;
740
0
  for (unsigned I = 0; 
I != VerdefCount0
;
++I0
) {
741
0
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
742
0
    Verdef += CurVerdef->vd_next;
743
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
744
0
    if (Verdefs.size() <= VerdefIndex)
745
0
      Verdefs.resize(VerdefIndex + 1);
746
0
    Verdefs[VerdefIndex] = CurVerdef;
747
0
  }
748
41
749
41
  return Verdefs;
750
41
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*&)
Line
Count
Source
717
17
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
718
17
  std::vector<const Elf_Verdef *> Verdefs(1);
719
17
  // We only need to process symbol versions for this DSO if it has both a
720
17
  // versym and a verdef section, which indicates that the DSO contains symbol
721
17
  // version definitions.
722
17
  if (
!VersymSec || 17
!VerdefSec0
)
723
17
    return Verdefs;
724
0
725
0
  // The location of the first global versym entry.
726
0
  const char *Base = this->MB.getBuffer().data();
727
0
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
728
0
           this->FirstNonLocal;
729
0
730
0
  // We cannot determine the largest verdef identifier without inspecting
731
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
732
0
  // sequentially starting from 1, so we predict that the largest identifier
733
0
  // will be VerdefCount.
734
0
  unsigned VerdefCount = VerdefSec->sh_info;
735
0
  Verdefs.resize(VerdefCount + 1);
736
0
737
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
738
0
  // from the start of the .gnu.version_d section.
739
0
  const char *Verdef = Base + VerdefSec->sh_offset;
740
0
  for (unsigned I = 0; 
I != VerdefCount0
;
++I0
) {
741
0
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
742
0
    Verdef += CurVerdef->vd_next;
743
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
744
0
    if (Verdefs.size() <= VerdefIndex)
745
0
      Verdefs.resize(VerdefIndex + 1);
746
0
    Verdefs[VerdefIndex] = CurVerdef;
747
0
  }
748
17
749
17
  return Verdefs;
750
17
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseVerdefs(llvm::object::Elf_Versym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*&)
Line
Count
Source
717
7
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
718
7
  std::vector<const Elf_Verdef *> Verdefs(1);
719
7
  // We only need to process symbol versions for this DSO if it has both a
720
7
  // versym and a verdef section, which indicates that the DSO contains symbol
721
7
  // version definitions.
722
7
  if (
!VersymSec || 7
!VerdefSec0
)
723
7
    return Verdefs;
724
0
725
0
  // The location of the first global versym entry.
726
0
  const char *Base = this->MB.getBuffer().data();
727
0
  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
728
0
           this->FirstNonLocal;
729
0
730
0
  // We cannot determine the largest verdef identifier without inspecting
731
0
  // every Elf_Verdef, but both bfd and gold assign verdef identifiers
732
0
  // sequentially starting from 1, so we predict that the largest identifier
733
0
  // will be VerdefCount.
734
0
  unsigned VerdefCount = VerdefSec->sh_info;
735
0
  Verdefs.resize(VerdefCount + 1);
736
0
737
0
  // Build the Verdefs array by following the chain of Elf_Verdef objects
738
0
  // from the start of the .gnu.version_d section.
739
0
  const char *Verdef = Base + VerdefSec->sh_offset;
740
0
  for (unsigned I = 0; 
I != VerdefCount0
;
++I0
) {
741
0
    auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
742
0
    Verdef += CurVerdef->vd_next;
743
0
    unsigned VerdefIndex = CurVerdef->vd_ndx;
744
0
    if (Verdefs.size() <= VerdefIndex)
745
0
      Verdefs.resize(VerdefIndex + 1);
746
0
    Verdefs[VerdefIndex] = CurVerdef;
747
0
  }
748
7
749
7
  return Verdefs;
750
7
}
751
752
// Fully parse the shared object file. This must be called after parseSoName().
753
216
template <class ELFT> void SharedFile<ELFT>::parseRest() {
754
216
  // Create mapping from version identifiers to Elf_Verdef entries.
755
216
  const Elf_Versym *Versym = nullptr;
756
216
  std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
757
216
758
216
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
759
563
  for (const Elf_Sym &Sym : Syms) {
760
563
    unsigned VersymIndex = 0;
761
563
    if (
Versym563
) {
762
60
      VersymIndex = Versym->vs_index;
763
60
      ++Versym;
764
60
    }
765
563
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
766
563
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
767
563
768
563
    StringRef Name = check(Sym.getName(this->StringTable), toString(this));
769
563
    if (
Sym.isUndefined()563
) {
770
32
      Undefs.push_back(Name);
771
32
      continue;
772
32
    }
773
531
774
531
    // Ignore local symbols.
775
531
    
if (531
Versym && 531
VersymIndex == VER_NDX_LOCAL52
)
776
0
      continue;
777
531
778
531
    const Elf_Verdef *V =
779
531
        VersymIndex == VER_NDX_GLOBAL ? 
nullptr7
:
Verdefs[VersymIndex]524
;
780
531
781
531
    if (!Hidden)
782
517
      Symtab->addShared(this, Name, Sym, V);
783
531
784
531
    // Also add the symbol with the versioned name to handle undefined symbols
785
531
    // with explicit versions.
786
531
    if (
V531
) {
787
45
      StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
788
45
      Name = Saver.save(Name + "@" + VerName);
789
45
      Symtab->addShared(this, Name, Sym, V);
790
45
    }
791
563
  }
792
216
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::parseRest()
Line
Count
Source
753
151
template <class ELFT> void SharedFile<ELFT>::parseRest() {
754
151
  // Create mapping from version identifiers to Elf_Verdef entries.
755
151
  const Elf_Versym *Versym = nullptr;
756
151
  std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
757
151
758
151
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
759
393
  for (const Elf_Sym &Sym : Syms) {
760
393
    unsigned VersymIndex = 0;
761
393
    if (
Versym393
) {
762
60
      VersymIndex = Versym->vs_index;
763
60
      ++Versym;
764
60
    }
765
393
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
766
393
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
767
393
768
393
    StringRef Name = check(Sym.getName(this->StringTable), toString(this));
769
393
    if (
Sym.isUndefined()393
) {
770
30
      Undefs.push_back(Name);
771
30
      continue;
772
30
    }
773
363
774
363
    // Ignore local symbols.
775
363
    
if (363
Versym && 363
VersymIndex == VER_NDX_LOCAL52
)
776
0
      continue;
777
363
778
363
    const Elf_Verdef *V =
779
363
        VersymIndex == VER_NDX_GLOBAL ? 
nullptr7
:
Verdefs[VersymIndex]356
;
780
363
781
363
    if (!Hidden)
782
349
      Symtab->addShared(this, Name, Sym, V);
783
363
784
363
    // Also add the symbol with the versioned name to handle undefined symbols
785
363
    // with explicit versions.
786
363
    if (
V363
) {
787
45
      StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
788
45
      Name = Saver.save(Name + "@" + VerName);
789
45
      Symtab->addShared(this, Name, Sym, V);
790
45
    }
791
393
  }
792
151
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::parseRest()
Line
Count
Source
753
7
template <class ELFT> void SharedFile<ELFT>::parseRest() {
754
7
  // Create mapping from version identifiers to Elf_Verdef entries.
755
7
  const Elf_Versym *Versym = nullptr;
756
7
  std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
757
7
758
7
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
759
17
  for (const Elf_Sym &Sym : Syms) {
760
17
    unsigned VersymIndex = 0;
761
17
    if (
Versym17
) {
762
0
      VersymIndex = Versym->vs_index;
763
0
      ++Versym;
764
0
    }
765
17
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
766
17
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
767
17
768
17
    StringRef Name = check(Sym.getName(this->StringTable), toString(this));
769
17
    if (
Sym.isUndefined()17
) {
770
0
      Undefs.push_back(Name);
771
0
      continue;
772
0
    }
773
17
774
17
    // Ignore local symbols.
775
17
    
if (17
Versym && 17
VersymIndex == VER_NDX_LOCAL0
)
776
0
      continue;
777
17
778
17
    const Elf_Verdef *V =
779
17
        VersymIndex == VER_NDX_GLOBAL ? 
nullptr0
:
Verdefs[VersymIndex]17
;
780
17
781
17
    if (!Hidden)
782
17
      Symtab->addShared(this, Name, Sym, V);
783
17
784
17
    // Also add the symbol with the versioned name to handle undefined symbols
785
17
    // with explicit versions.
786
17
    if (
V17
) {
787
0
      StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
788
0
      Name = Saver.save(Name + "@" + VerName);
789
0
      Symtab->addShared(this, Name, Sym, V);
790
0
    }
791
17
  }
792
7
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::parseRest()
Line
Count
Source
753
17
template <class ELFT> void SharedFile<ELFT>::parseRest() {
754
17
  // Create mapping from version identifiers to Elf_Verdef entries.
755
17
  const Elf_Versym *Versym = nullptr;
756
17
  std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
757
17
758
17
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
759
63
  for (const Elf_Sym &Sym : Syms) {
760
63
    unsigned VersymIndex = 0;
761
63
    if (
Versym63
) {
762
0
      VersymIndex = Versym->vs_index;
763
0
      ++Versym;
764
0
    }
765
63
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
766
63
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
767
63
768
63
    StringRef Name = check(Sym.getName(this->StringTable), toString(this));
769
63
    if (
Sym.isUndefined()63
) {
770
0
      Undefs.push_back(Name);
771
0
      continue;
772
0
    }
773
63
774
63
    // Ignore local symbols.
775
63
    
if (63
Versym && 63
VersymIndex == VER_NDX_LOCAL0
)
776
0
      continue;
777
63
778
63
    const Elf_Verdef *V =
779
63
        VersymIndex == VER_NDX_GLOBAL ? 
nullptr0
:
Verdefs[VersymIndex]63
;
780
63
781
63
    if (!Hidden)
782
63
      Symtab->addShared(this, Name, Sym, V);
783
63
784
63
    // Also add the symbol with the versioned name to handle undefined symbols
785
63
    // with explicit versions.
786
63
    if (
V63
) {
787
0
      StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
788
0
      Name = Saver.save(Name + "@" + VerName);
789
0
      Symtab->addShared(this, Name, Sym, V);
790
0
    }
791
63
  }
792
17
}
lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::parseRest()
Line
Count
Source
753
41
template <class ELFT> void SharedFile<ELFT>::parseRest() {
754
41
  // Create mapping from version identifiers to Elf_Verdef entries.
755
41
  const Elf_Versym *Versym = nullptr;
756
41
  std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
757
41
758
41
  Elf_Sym_Range Syms = this->getGlobalELFSyms();
759
90
  for (const Elf_Sym &Sym : Syms) {
760
90
    unsigned VersymIndex = 0;
761
90
    if (
Versym90
) {
762
0
      VersymIndex = Versym->vs_index;
763
0
      ++Versym;
764
0
    }
765
90
    bool Hidden = VersymIndex & VERSYM_HIDDEN;
766
90
    VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
767
90
768
90
    StringRef Name = check(Sym.getName(this->StringTable), toString(this));
769
90
    if (
Sym.isUndefined()90
) {
770
2
      Undefs.push_back(Name);
771
2
      continue;
772
2
    }
773
88
774
88
    // Ignore local symbols.
775
88
    
if (88
Versym && 88
VersymIndex == VER_NDX_LOCAL0
)
776
0
      continue;
777
88
778
88
    const Elf_Verdef *V =
779
88
        VersymIndex == VER_NDX_GLOBAL ? 
nullptr0
:
Verdefs[VersymIndex]88
;
780
88
781
88
    if (!Hidden)
782
88
      Symtab->addShared(this, Name, Sym, V);
783
88
784
88
    // Also add the symbol with the versioned name to handle undefined symbols
785
88
    // with explicit versions.
786
88
    if (
V88
) {
787
0
      StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
788
0
      Name = Saver.save(Name + "@" + VerName);
789
0
      Symtab->addShared(this, Name, Sym, V);
790
0
    }
791
90
  }
792
41
}
793
794
167
static ELFKind getBitcodeELFKind(const Triple &T) {
795
167
  if (T.isLittleEndian())
796
167
    
return T.isArch64Bit() ? 167
ELF64LEKind167
:
ELF32LEKind0
;
797
0
  
return T.isArch64Bit() ? 0
ELF64BEKind0
:
ELF32BEKind0
;
798
167
}
799
800
167
static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
801
167
  switch (T.getArch()) {
802
0
  case Triple::aarch64:
803
0
    return EM_AARCH64;
804
0
  case Triple::arm:
805
0
  case Triple::thumb:
806
0
    return EM_ARM;
807
0
  case Triple::avr:
808
0
    return EM_AVR;
809
0
  case Triple::mips:
810
0
  case Triple::mipsel:
811
0
  case Triple::mips64:
812
0
  case Triple::mips64el:
813
0
    return EM_MIPS;
814
0
  case Triple::ppc:
815
0
    return EM_PPC;
816
0
  case Triple::ppc64:
817
0
    return EM_PPC64;
818
0
  case Triple::x86:
819
0
    return T.isOSIAMCU() ? 
EM_IAMCU0
:
EM_3860
;
820
167
  case Triple::x86_64:
821
167
    return EM_X86_64;
822
0
  default:
823
0
    fatal(Path + ": could not infer e_machine from bitcode target triple " +
824
0
          T.str());
825
0
  }
826
0
}
827
828
BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
829
                         uint64_t OffsetInArchive)
830
167
    : InputFile(BitcodeKind, MB) {
831
167
  this->ArchiveName = ArchiveName;
832
167
833
167
  // Here we pass a new MemoryBufferRef which is identified by ArchiveName
834
167
  // (the fully resolved path of the archive) + member name + offset of the
835
167
  // member in the archive.
836
167
  // ThinLTO uses the MemoryBufferRef identifier to access its internal
837
167
  // data structures and if two archives define two members with the same name,
838
167
  // this causes a collision which result in only one of the objects being
839
167
  // taken into consideration at LTO time (which very likely causes undefined
840
167
  // symbols later in the link stage).
841
167
  MemoryBufferRef MBRef(MB.getBuffer(),
842
167
                        Saver.save(ArchiveName + MB.getBufferIdentifier() +
843
167
                                   utostr(OffsetInArchive)));
844
167
  Obj = check(lto::InputFile::create(MBRef), toString(this));
845
167
846
167
  Triple T(Obj->getTargetTriple());
847
167
  EKind = getBitcodeELFKind(T);
848
167
  EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
849
167
}
850
851
285
static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
852
285
  switch (GvVisibility) {
853
255
  case GlobalValue::DefaultVisibility:
854
255
    return STV_DEFAULT;
855
28
  case GlobalValue::HiddenVisibility:
856
28
    return STV_HIDDEN;
857
2
  case GlobalValue::ProtectedVisibility:
858
2
    return STV_PROTECTED;
859
0
  }
860
0
  
llvm_unreachable0
("unknown visibility");
861
0
}
862
863
template <class ELFT>
864
static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
865
                                   const lto::InputFile::Symbol &ObjSym,
866
285
                                   BitcodeFile *F) {
867
285
  StringRef NameRef = Saver.save(ObjSym.getName());
868
285
  uint32_t Binding = ObjSym.isWeak() ? 
STB_WEAK21
:
STB_GLOBAL264
;
869
285
870
285
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE283
;
871
285
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
872
285
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
873
285
874
285
  int C = ObjSym.getComdatIndex();
875
285
  if (
C != -1 && 285
!KeptComdats[C]7
)
876
3
    return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
877
3
                                      Visibility, Type, CanOmitFromDynSym, F);
878
282
879
282
  
if (282
ObjSym.isUndefined()282
)
880
71
    return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
881
71
                                      Visibility, Type, CanOmitFromDynSym, F);
882
211
883
211
  
if (211
ObjSym.isCommon()211
)
884
5
    return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
885
5
                             ObjSym.getCommonAlignment(), Binding, Visibility,
886
5
                             STT_OBJECT, F);
887
206
888
206
  return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
889
206
                            CanOmitFromDynSym, F);
890
206
}
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*)
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
866
285
                                   BitcodeFile *F) {
867
285
  StringRef NameRef = Saver.save(ObjSym.getName());
868
285
  uint32_t Binding = ObjSym.isWeak() ? 
STB_WEAK21
:
STB_GLOBAL264
;
869
285
870
285
  uint8_t Type = ObjSym.isTLS() ? 
STT_TLS2
:
STT_NOTYPE283
;
871
285
  uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
872
285
  bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
873
285
874
285
  int C = ObjSym.getComdatIndex();
875
285
  if (
C != -1 && 285
!KeptComdats[C]7
)
876
3
    return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
877
3
                                      Visibility, Type, CanOmitFromDynSym, F);
878
282
879
282
  
if (282
ObjSym.isUndefined()282
)
880
71
    return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
881
71
                                      Visibility, Type, CanOmitFromDynSym, F);
882
211
883
211
  
if (211
ObjSym.isCommon()211
)
884
5
    return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
885
5
                             ObjSym.getCommonAlignment(), Binding, Visibility,
886
5
                             STT_OBJECT, F);
887
206
888
206
  return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
889
206
                            CanOmitFromDynSym, F);
890
206
}
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*)
Unexecuted instantiation: InputFiles.cpp:lld::elf::Symbol* createBitcodeSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(std::__1::vector<bool, std::__1::allocator<bool> > const&, llvm::lto::InputFile::Symbol const&, lld::elf::BitcodeFile*)
891
892
template <class ELFT>
893
161
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
894
161
  std::vector<bool> KeptComdats;
895
161
  for (StringRef S : Obj->getComdatTable())
896
7
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
897
161
898
161
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
899
285
    Symbols.push_back(
900
285
        createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this)->body());
901
161
}
void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Line
Count
Source
893
161
void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
894
161
  std::vector<bool> KeptComdats;
895
161
  for (StringRef S : Obj->getComdatTable())
896
7
    KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
897
161
898
161
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
899
285
    Symbols.push_back(
900
285
        createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this)->body());
901
161
}
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
Unexecuted instantiation: void lld::elf::BitcodeFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::DenseSet<llvm::CachedHashStringRef, llvm::DenseMapInfo<llvm::CachedHashStringRef> >&)
902
903
2.29k
static ELFKind getELFKind(MemoryBufferRef MB) {
904
2.29k
  unsigned char Size;
905
2.29k
  unsigned char Endian;
906
2.29k
  std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
907
2.29k
908
2.29k
  if (
Endian != ELFDATA2LSB && 2.29k
Endian != ELFDATA2MSB204
)
909
0
    fatal(MB.getBufferIdentifier() + ": invalid data encoding");
910
2.29k
  
if (2.29k
Size != ELFCLASS32 && 2.29k
Size != ELFCLASS641.87k
)
911
0
    fatal(MB.getBufferIdentifier() + ": invalid file class");
912
2.29k
913
2.29k
  size_t BufSize = MB.getBuffer().size();
914
2.29k
  if (
(Size == ELFCLASS32 && 2.29k
BufSize < sizeof(Elf32_Ehdr)418
) ||
915
2.29k
      
(Size == ELFCLASS64 && 2.29k
BufSize < sizeof(Elf64_Ehdr)1.87k
))
916
0
    fatal(MB.getBufferIdentifier() + ": file is too short");
917
2.29k
918
2.29k
  
if (2.29k
Size == ELFCLASS322.29k
)
919
418
    
return (Endian == ELFDATA2LSB) ? 418
ELF32LEKind272
:
ELF32BEKind146
;
920
1.87k
  
return (Endian == ELFDATA2LSB) ? 1.87k
ELF64LEKind1.82k
:
ELF64BEKind58
;
921
2.29k
}
922
923
5
template <class ELFT> void BinaryFile::parse() {
924
5
  ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
925
5
  auto *Section =
926
5
      make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
927
5
  Sections.push_back(Section);
928
5
929
5
  // For each input file foo that is embedded to a result as a binary
930
5
  // blob, we define _binary_foo_{start,end,size} symbols, so that
931
5
  // user programs can access blobs by name. Non-alphanumeric
932
5
  // characters in a filename are replaced with underscore.
933
5
  std::string S = "_binary_" + MB.getBufferIdentifier().str();
934
494
  for (size_t I = 0; 
I < S.size()494
;
++I489
)
935
489
    
if (489
!elf::isAlnum(S[I])489
)
936
73
      S[I] = '_';
937
5
938
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,
939
5
                           0, 0, STB_GLOBAL, Section, nullptr);
940
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
941
5
                           Data.size(), 0, STB_GLOBAL, Section, nullptr);
942
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
943
5
                           Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
944
5
}
Unexecuted instantiation: void lld::elf::BinaryFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Unexecuted instantiation: void lld::elf::BinaryFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
void lld::elf::BinaryFile::parse<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
923
5
template <class ELFT> void BinaryFile::parse() {
924
5
  ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
925
5
  auto *Section =
926
5
      make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
927
5
  Sections.push_back(Section);
928
5
929
5
  // For each input file foo that is embedded to a result as a binary
930
5
  // blob, we define _binary_foo_{start,end,size} symbols, so that
931
5
  // user programs can access blobs by name. Non-alphanumeric
932
5
  // characters in a filename are replaced with underscore.
933
5
  std::string S = "_binary_" + MB.getBufferIdentifier().str();
934
494
  for (size_t I = 0; 
I < S.size()494
;
++I489
)
935
489
    
if (489
!elf::isAlnum(S[I])489
)
936
73
      S[I] = '_';
937
5
938
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,
939
5
                           0, 0, STB_GLOBAL, Section, nullptr);
940
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
941
5
                           Data.size(), 0, STB_GLOBAL, Section, nullptr);
942
5
  Symtab->addRegular<ELFT>(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
943
5
                           Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
944
5
}
Unexecuted instantiation: void lld::elf::BinaryFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
945
946
2.24k
static bool isBitcode(MemoryBufferRef MB) {
947
2.24k
  using namespace sys::fs;
948
2.24k
  return identify_magic(MB.getBuffer()) == file_magic::bitcode;
949
2.24k
}
950
951
InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
952
2.22k
                                 uint64_t OffsetInArchive) {
953
2.22k
  if (isBitcode(MB))
954
167
    return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
955
2.06k
956
2.06k
  switch (getELFKind(MB)) {
957
227
  case ELF32LEKind:
958
227
    return make<ObjFile<ELF32LE>>(MB, ArchiveName);
959
129
  case ELF32BEKind:
960
129
    return make<ObjFile<ELF32BE>>(MB, ArchiveName);
961
1.65k
  case ELF64LEKind:
962
1.65k
    return make<ObjFile<ELF64LE>>(MB, ArchiveName);
963
51
  case ELF64BEKind:
964
51
    return make<ObjFile<ELF64BE>>(MB, ArchiveName);
965
0
  default:
966
0
    llvm_unreachable("getELFKind");
967
0
  }
968
0
}
969
970
224
InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
971
224
  switch (getELFKind(MB)) {
972
45
  case ELF32LEKind:
973
45
    return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
974
17
  case ELF32BEKind:
975
17
    return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
976
155
  case ELF64LEKind:
977
155
    return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
978
7
  case ELF64BEKind:
979
7
    return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
980
0
  default:
981
0
    llvm_unreachable("getELFKind");
982
0
  }
983
0
}
984
985
14
MemoryBufferRef LazyObjFile::getBuffer() {
986
14
  if (Seen)
987
2
    return MemoryBufferRef();
988
12
  Seen = true;
989
12
  return MB;
990
12
}
991
992
14
InputFile *LazyObjFile::fetch() {
993
14
  MemoryBufferRef MBRef = getBuffer();
994
14
  if (MBRef.getBuffer().empty())
995
2
    return nullptr;
996
12
  return createObjectFile(MBRef, ArchiveName, OffsetInArchive);
997
12
}
998
999
19
template <class ELFT> void LazyObjFile::parse() {
1000
19
  for (StringRef Sym : getSymbolNames())
1001
25
    Symtab->addLazyObject<ELFT>(Sym, *this);
1002
19
}
Unexecuted instantiation: void lld::elf::LazyObjFile::parse<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
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
999
19
template <class ELFT> void LazyObjFile::parse() {
1000
19
  for (StringRef Sym : getSymbolNames())
1001
25
    Symtab->addLazyObject<ELFT>(Sym, *this);
1002
19
}
1003
1004
10
template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1005
10
  typedef typename ELFT::Shdr Elf_Shdr;
1006
10
  typedef typename ELFT::Sym Elf_Sym;
1007
10
  typedef typename ELFT::SymRange Elf_Sym_Range;
1008
10
1009
10
  const ELFFile<ELFT> Obj(this->MB.getBuffer());
1010
10
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
1011
47
  for (const Elf_Shdr &Sec : Sections) {
1012
47
    if (Sec.sh_type != SHT_SYMTAB)
1013
37
      continue;
1014
10
1015
10
    Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this));
1016
10
    uint32_t FirstNonLocal = Sec.sh_info;
1017
10
    StringRef StringTable =
1018
10
        check(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
1019
10
    std::vector<StringRef> V;
1020
10
1021
10
    for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1022
20
      
if (20
Sym.st_shndx != SHN_UNDEF20
)
1023
16
        V.push_back(check(Sym.getName(StringTable), toString(this)));
1024
47
    return V;
1025
47
  }
1026
0
  return {};
1027
0
}
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1004
10
template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1005
10
  typedef typename ELFT::Shdr Elf_Shdr;
1006
10
  typedef typename ELFT::Sym Elf_Sym;
1007
10
  typedef typename ELFT::SymRange Elf_Sym_Range;
1008
10
1009
10
  const ELFFile<ELFT> Obj(this->MB.getBuffer());
1010
10
  ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
1011
47
  for (const Elf_Shdr &Sec : Sections) {
1012
47
    if (Sec.sh_type != SHT_SYMTAB)
1013
37
      continue;
1014
10
1015
10
    Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this));
1016
10
    uint32_t FirstNonLocal = Sec.sh_info;
1017
10
    StringRef StringTable =
1018
10
        check(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
1019
10
    std::vector<StringRef> V;
1020
10
1021
10
    for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1022
20
      
if (20
Sym.st_shndx != SHN_UNDEF20
)
1023
16
        V.push_back(check(Sym.getName(StringTable), toString(this)));
1024
47
    return V;
1025
47
  }
1026
0
  return {};
1027
0
}
Unexecuted instantiation: std::__1::vector<llvm::StringRef, std::__1::allocator<llvm::StringRef> > lld::elf::LazyObjFile::getElfSymbols<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
1028
1029
9
std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
1030
9
  std::unique_ptr<lto::InputFile> Obj =
1031
9
      check(lto::InputFile::create(this->MB), toString(this));
1032
9
  std::vector<StringRef> V;
1033
9
  for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1034
11
    
if (11
!Sym.isUndefined()11
)
1035
9
      V.push_back(Saver.save(Sym.getName()));
1036
9
  return V;
1037
9
}
1038
1039
// Returns a vector of globally-visible defined symbol names.
1040
19
std::vector<StringRef> LazyObjFile::getSymbolNames() {
1041
19
  if (isBitcode(this->MB))
1042
9
    return getBitcodeSymbols();
1043
10
1044
10
  switch (getELFKind(this->MB)) {
1045
0
  case ELF32LEKind:
1046
0
    return getElfSymbols<ELF32LE>();
1047
0
  case ELF32BEKind:
1048
0
    return getElfSymbols<ELF32BE>();
1049
10
  case ELF64LEKind:
1050
10
    return getElfSymbols<ELF64LE>();
1051
0
  case ELF64BEKind:
1052
0
    return getElfSymbols<ELF64BE>();
1053
0
  default:
1054
0
    llvm_unreachable("getELFKind");
1055
0
  }
1056
0
}
1057
1058
template void ArchiveFile::parse<ELF32LE>();
1059
template void ArchiveFile::parse<ELF32BE>();
1060
template void ArchiveFile::parse<ELF64LE>();
1061
template void ArchiveFile::parse<ELF64BE>();
1062
1063
template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1064
template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1065
template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1066
template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1067
1068
template void LazyObjFile::parse<ELF32LE>();
1069
template void LazyObjFile::parse<ELF32BE>();
1070
template void LazyObjFile::parse<ELF64LE>();
1071
template void LazyObjFile::parse<ELF64BE>();
1072
1073
template class elf::ELFFileBase<ELF32LE>;
1074
template class elf::ELFFileBase<ELF32BE>;
1075
template class elf::ELFFileBase<ELF64LE>;
1076
template class elf::ELFFileBase<ELF64BE>;
1077
1078
template class elf::ObjFile<ELF32LE>;
1079
template class elf::ObjFile<ELF32BE>;
1080
template class elf::ObjFile<ELF64LE>;
1081
template class elf::ObjFile<ELF64BE>;
1082
1083
template class elf::SharedFile<ELF32LE>;
1084
template class elf::SharedFile<ELF32BE>;
1085
template class elf::SharedFile<ELF64LE>;
1086
template class elf::SharedFile<ELF64BE>;
1087
1088
template void BinaryFile::parse<ELF32LE>();
1089
template void BinaryFile::parse<ELF32BE>();
1090
template void BinaryFile::parse<ELF64LE>();
1091
template void BinaryFile::parse<ELF64BE>();