Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/MarkLive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MarkLive.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
// This file implements --gc-sections, which is a feature to remove unused
11
// sections from output. Unused sections are sections that are not reachable
12
// from known GC-root symbols or sections. Naturally the feature is
13
// implemented as a mark-sweep garbage collector.
14
//
15
// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
16
// by default. Starting with GC-root symbols or sections, markLive function
17
// defined in this file visits all reachable sections to set their Live
18
// bits. Writer will then ignore sections whose Live bits are off, so that
19
// such sections are not included into output.
20
//
21
//===----------------------------------------------------------------------===//
22
23
#include "InputSection.h"
24
#include "LinkerScript.h"
25
#include "Memory.h"
26
#include "OutputSections.h"
27
#include "Strings.h"
28
#include "SymbolTable.h"
29
#include "Symbols.h"
30
#include "Target.h"
31
#include "Writer.h"
32
#include "llvm/ADT/STLExtras.h"
33
#include "llvm/Object/ELF.h"
34
#include <functional>
35
#include <vector>
36
37
using namespace llvm;
38
using namespace llvm::ELF;
39
using namespace llvm::object;
40
using namespace llvm::support::endian;
41
42
using namespace lld;
43
using namespace lld::elf;
44
45
template <class ELFT>
46
static typename ELFT::uint getAddend(InputSectionBase &Sec,
47
11
                                     const typename ELFT::Rel &Rel) {
48
11
  return Target->getImplicitAddend(Sec.Data.begin() + Rel.r_offset,
49
11
                                   Rel.getType(Config->IsMips64EL));
50
11
}
MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Rel const&)
Line
Count
Source
47
11
                                     const typename ELFT::Rel &Rel) {
48
11
  return Target->getImplicitAddend(Sec.Data.begin() + Rel.r_offset,
49
11
                                   Rel.getType(Config->IsMips64EL));
50
11
}
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Rel const&)
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Rel const&)
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rel const&)
51
52
template <class ELFT>
53
static typename ELFT::uint getAddend(InputSectionBase &Sec,
54
26
                                     const typename ELFT::Rela &Rel) {
55
26
  return Rel.r_addend;
56
26
}
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela const&)
MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela const&)
Line
Count
Source
54
26
                                     const typename ELFT::Rela &Rel) {
55
26
  return Rel.r_addend;
56
26
}
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela const&)
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela const&)
57
58
// There are normally few input sections whose names are valid C
59
// identifiers, so we just store a std::vector instead of a multimap.
60
static DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
61
62
template <class ELFT, class RelT>
63
static void resolveReloc(InputSectionBase &Sec, RelT &Rel,
64
61
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
61
  SymbolBody &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
61
67
61
  if (auto *
Sym61
= dyn_cast<DefinedCommon>(&B)) {
68
2
    Sym->Live = true;
69
2
    return;
70
2
  }
71
59
72
59
  
if (auto *59
D59
= dyn_cast<DefinedRegular>(&B)) {
73
50
    if (!D->Section)
74
0
      return;
75
50
    uint64_t Offset = D->Value;
76
50
    if (D->isSection())
77
37
      Offset += getAddend<ELFT>(Sec, Rel);
78
50
    Fn(cast<InputSectionBase>(D->Section), Offset);
79
50
    return;
80
50
  }
81
9
82
9
  
if (auto *9
U9
= dyn_cast<Undefined>(&B))
83
9
    for (InputSectionBase *Sec : CNamedSections.lookup(U->getName()))
84
5
      Fn(Sec, 0);
85
61
}
Unexecuted instantiation: MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Unexecuted instantiation: MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
64
15
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
15
  SymbolBody &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
15
67
15
  if (auto *
Sym15
= dyn_cast<DefinedCommon>(&B)) {
68
0
    Sym->Live = true;
69
0
    return;
70
0
  }
71
15
72
15
  
if (auto *15
D15
= dyn_cast<DefinedRegular>(&B)) {
73
15
    if (!D->Section)
74
0
      return;
75
15
    uint64_t Offset = D->Value;
76
15
    if (D->isSection())
77
11
      Offset += getAddend<ELFT>(Sec, Rel);
78
15
    Fn(cast<InputSectionBase>(D->Section), Offset);
79
15
    return;
80
15
  }
81
0
82
0
  
if (auto *0
U0
= dyn_cast<Undefined>(&B))
83
0
    for (InputSectionBase *Sec : CNamedSections.lookup(U->getName()))
84
0
      Fn(Sec, 0);
85
15
}
Unexecuted instantiation: MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
64
1
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
1
  SymbolBody &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
1
67
1
  if (auto *
Sym1
= dyn_cast<DefinedCommon>(&B)) {
68
0
    Sym->Live = true;
69
0
    return;
70
0
  }
71
1
72
1
  
if (auto *1
D1
= dyn_cast<DefinedRegular>(&B)) {
73
0
    if (!D->Section)
74
0
      return;
75
0
    uint64_t Offset = D->Value;
76
0
    if (D->isSection())
77
0
      Offset += getAddend<ELFT>(Sec, Rel);
78
0
    Fn(cast<InputSectionBase>(D->Section), Offset);
79
0
    return;
80
0
  }
81
1
82
1
  
if (auto *1
U1
= dyn_cast<Undefined>(&B))
83
1
    for (InputSectionBase *Sec : CNamedSections.lookup(U->getName()))
84
0
      Fn(Sec, 0);
85
1
}
Unexecuted instantiation: MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
64
44
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
44
  SymbolBody &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
44
67
44
  if (auto *
Sym44
= dyn_cast<DefinedCommon>(&B)) {
68
2
    Sym->Live = true;
69
2
    return;
70
2
  }
71
42
72
42
  
if (auto *42
D42
= dyn_cast<DefinedRegular>(&B)) {
73
35
    if (!D->Section)
74
0
      return;
75
35
    uint64_t Offset = D->Value;
76
35
    if (D->isSection())
77
26
      Offset += getAddend<ELFT>(Sec, Rel);
78
35
    Fn(cast<InputSectionBase>(D->Section), Offset);
79
35
    return;
80
35
  }
81
7
82
7
  
if (auto *7
U7
= dyn_cast<Undefined>(&B))
83
7
    for (InputSectionBase *Sec : CNamedSections.lookup(U->getName()))
84
5
      Fn(Sec, 0);
85
44
}
MarkLive.cpp:void resolveReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(lld::elf::InputSectionBase&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
64
1
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
1
  SymbolBody &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
1
67
1
  if (auto *
Sym1
= dyn_cast<DefinedCommon>(&B)) {
68
0
    Sym->Live = true;
69
0
    return;
70
0
  }
71
1
72
1
  
if (auto *1
D1
= dyn_cast<DefinedRegular>(&B)) {
73
0
    if (!D->Section)
74
0
      return;
75
0
    uint64_t Offset = D->Value;
76
0
    if (D->isSection())
77
0
      Offset += getAddend<ELFT>(Sec, Rel);
78
0
    Fn(cast<InputSectionBase>(D->Section), Offset);
79
0
    return;
80
0
  }
81
1
82
1
  
if (auto *1
U1
= dyn_cast<Undefined>(&B))
83
1
    for (InputSectionBase *Sec : CNamedSections.lookup(U->getName()))
84
0
      Fn(Sec, 0);
85
1
}
86
87
// Calls Fn for each section that Sec refers to via relocations.
88
template <class ELFT>
89
static void
90
forEachSuccessor(InputSection &Sec,
91
108
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
92
108
  if (
Sec.AreRelocsRela108
) {
93
33
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
94
39
      resolveReloc<ELFT>(Sec, Rel, Fn);
95
108
  } else {
96
75
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
97
12
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
75
  }
99
108
100
108
  for (InputSectionBase *IS : Sec.DependentSections)
101
7
    Fn(IS, 0);
102
108
}
MarkLive.cpp:void forEachSuccessor<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
91
3
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
92
3
  if (
Sec.AreRelocsRela3
) {
93
1
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
94
1
      resolveReloc<ELFT>(Sec, Rel, Fn);
95
3
  } else {
96
2
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
97
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
2
  }
99
3
100
3
  for (InputSectionBase *IS : Sec.DependentSections)
101
0
    Fn(IS, 0);
102
3
}
MarkLive.cpp:void forEachSuccessor<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
91
85
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
92
85
  if (
Sec.AreRelocsRela85
) {
93
32
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
94
38
      resolveReloc<ELFT>(Sec, Rel, Fn);
95
85
  } else {
96
53
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
97
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
53
  }
99
85
100
85
  for (InputSectionBase *IS : Sec.DependentSections)
101
3
    Fn(IS, 0);
102
85
}
MarkLive.cpp:void forEachSuccessor<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
91
5
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
92
5
  if (
Sec.AreRelocsRela5
) {
93
0
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
94
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
95
5
  } else {
96
5
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
97
1
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
5
  }
99
5
100
5
  for (InputSectionBase *IS : Sec.DependentSections)
101
0
    Fn(IS, 0);
102
5
}
MarkLive.cpp:void forEachSuccessor<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
91
15
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
92
15
  if (
Sec.AreRelocsRela15
) {
93
0
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
94
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
95
15
  } else {
96
15
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
97
11
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
15
  }
99
15
100
15
  for (InputSectionBase *IS : Sec.DependentSections)
101
4
    Fn(IS, 0);
102
15
}
103
104
// The .eh_frame section is an unfortunate special case.
105
// The section is divided in CIEs and FDEs and the relocations it can have are
106
// * CIEs can refer to a personality function.
107
// * FDEs can refer to a LSDA
108
// * FDEs refer to the function they contain information about
109
// The last kind of relocation cannot keep the referred section alive, or they
110
// would keep everything alive in a common object file. In fact, each FDE is
111
// alive if the section it refers to is alive.
112
// To keep things simple, in here we just ignore the last relocation kind. The
113
// other two keep the referred section alive.
114
//
115
// A possible improvement would be to fully process .eh_frame in the middle of
116
// the gc pass. With that we would be able to also gc some sections holding
117
// LSDAs and personality functions if we found that they were unused.
118
template <class ELFT, class RelTy>
119
static void
120
scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels,
121
5
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
122
5
  const endianness E = ELFT::TargetEndianness;
123
5
124
17
  for (unsigned I = 0, N = EH.Pieces.size(); 
I < N17
;
++I12
) {
125
12
    EhSectionPiece &Piece = EH.Pieces[I];
126
12
    unsigned FirstRelI = Piece.FirstRelocation;
127
12
    if (FirstRelI == (unsigned)-1)
128
3
      continue;
129
9
    
if (9
read32<E>(Piece.data().data() + 4) == 09
) {
130
2
      // This is a CIE, we only need to worry about the first relocation. It is
131
2
      // known to point to the personality function.
132
2
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
133
2
      continue;
134
2
    }
135
7
    // This is a FDE. The relocations point to the described function or to
136
7
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
137
7
    // points to executable sections.
138
7
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
139
15
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); 
I2 < N215
;
++I28
) {
140
10
      const RelTy &Rel = Rels[I2];
141
10
      if (Rel.r_offset >= PieceEnd)
142
2
        break;
143
8
      resolveReloc<ELFT>(EH, Rels[I2],
144
8
                         [&](InputSectionBase *Sec, uint64_t Offset) {
145
8
                           if (
Sec && 8
Sec != &InputSection::Discarded8
&&
146
8
                               !(Sec->Flags & SHF_EXECINSTR))
147
1
                             Fn(Sec, 0);
148
8
                         });
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
144
2
                         [&](InputSectionBase *Sec, uint64_t Offset) {
145
2
                           if (
Sec && 2
Sec != &InputSection::Discarded2
&&
146
2
                               !(Sec->Flags & SHF_EXECINSTR))
147
0
                             Fn(Sec, 0);
148
2
                         });
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
144
6
                         [&](InputSectionBase *Sec, uint64_t Offset) {
145
6
                           if (
Sec && 6
Sec != &InputSection::Discarded6
&&
146
6
                               !(Sec->Flags & SHF_EXECINSTR))
147
1
                             Fn(Sec, 0);
148
6
                         });
149
10
    }
150
12
  }
151
5
}
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
121
3
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
122
3
  const endianness E = ELFT::TargetEndianness;
123
3
124
11
  for (unsigned I = 0, N = EH.Pieces.size(); 
I < N11
;
++I8
) {
125
8
    EhSectionPiece &Piece = EH.Pieces[I];
126
8
    unsigned FirstRelI = Piece.FirstRelocation;
127
8
    if (FirstRelI == (unsigned)-1)
128
3
      continue;
129
5
    
if (5
read32<E>(Piece.data().data() + 4) == 05
) {
130
0
      // This is a CIE, we only need to worry about the first relocation. It is
131
0
      // known to point to the personality function.
132
0
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
133
0
      continue;
134
0
    }
135
5
    // This is a FDE. The relocations point to the described function or to
136
5
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
137
5
    // points to executable sections.
138
5
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
139
11
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); 
I2 < N211
;
++I26
) {
140
8
      const RelTy &Rel = Rels[I2];
141
8
      if (Rel.r_offset >= PieceEnd)
142
2
        break;
143
6
      resolveReloc<ELFT>(EH, Rels[I2],
144
6
                         [&](InputSectionBase *Sec, uint64_t Offset) {
145
6
                           if (Sec && Sec != &InputSection::Discarded &&
146
6
                               !(Sec->Flags & SHF_EXECINSTR))
147
6
                             Fn(Sec, 0);
148
6
                         });
149
6
    }
150
8
  }
151
3
}
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
121
2
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
122
2
  const endianness E = ELFT::TargetEndianness;
123
2
124
6
  for (unsigned I = 0, N = EH.Pieces.size(); 
I < N6
;
++I4
) {
125
4
    EhSectionPiece &Piece = EH.Pieces[I];
126
4
    unsigned FirstRelI = Piece.FirstRelocation;
127
4
    if (FirstRelI == (unsigned)-1)
128
0
      continue;
129
4
    
if (4
read32<E>(Piece.data().data() + 4) == 04
) {
130
2
      // This is a CIE, we only need to worry about the first relocation. It is
131
2
      // known to point to the personality function.
132
2
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
133
2
      continue;
134
2
    }
135
2
    // This is a FDE. The relocations point to the described function or to
136
2
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
137
2
    // points to executable sections.
138
2
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
139
4
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); 
I2 < N24
;
++I22
) {
140
2
      const RelTy &Rel = Rels[I2];
141
2
      if (Rel.r_offset >= PieceEnd)
142
0
        break;
143
2
      resolveReloc<ELFT>(EH, Rels[I2],
144
2
                         [&](InputSectionBase *Sec, uint64_t Offset) {
145
2
                           if (Sec && Sec != &InputSection::Discarded &&
146
2
                               !(Sec->Flags & SHF_EXECINSTR))
147
2
                             Fn(Sec, 0);
148
2
                         });
149
2
    }
150
4
  }
151
2
}
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhInputSection&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
152
153
template <class ELFT>
154
static void
155
scanEhFrameSection(EhInputSection &EH,
156
7
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
157
7
  if (!EH.NumRelocations)
158
2
    return;
159
5
160
5
  // Unfortunately we need to split .eh_frame early since some relocations in
161
5
  // .eh_frame keep other section alive and some don't.
162
5
  EH.split<ELFT>();
163
5
164
5
  if (EH.AreRelocsRela)
165
3
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
166
5
  else
167
2
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
168
7
}
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::EhInputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::EhInputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
156
5
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
157
5
  if (!EH.NumRelocations)
158
2
    return;
159
3
160
3
  // Unfortunately we need to split .eh_frame early since some relocations in
161
3
  // .eh_frame keep other section alive and some don't.
162
3
  EH.split<ELFT>();
163
3
164
3
  if (EH.AreRelocsRela)
165
3
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
166
3
  else
167
0
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
168
5
}
Unexecuted instantiation: MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::EhInputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
MarkLive.cpp:void scanEhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::EhInputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
156
2
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
157
2
  if (!EH.NumRelocations)
158
0
    return;
159
2
160
2
  // Unfortunately we need to split .eh_frame early since some relocations in
161
2
  // .eh_frame keep other section alive and some don't.
162
2
  EH.split<ELFT>();
163
2
164
2
  if (EH.AreRelocsRela)
165
0
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
166
2
  else
167
2
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
168
2
}
169
170
// We do not garbage-collect two types of sections:
171
// 1) Sections used by the loader (.init, .fini, .ctors, .dtors or .jcr)
172
// 2) Non-allocatable sections which typically contain debugging information
173
291
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
174
291
  switch (Sec->Type) {
175
6
  case SHT_FINI_ARRAY:
176
6
  case SHT_INIT_ARRAY:
177
6
  case SHT_NOTE:
178
6
  case SHT_PREINIT_ARRAY:
179
6
    return true;
180
285
  default:
181
285
    if (!(Sec->Flags & SHF_ALLOC))
182
84
      return true;
183
201
184
201
    StringRef S = Sec->Name;
185
199
    return S.startswith(".ctors") || S.startswith(".dtors") ||
186
201
           
S.startswith(".init")197
||
S.startswith(".fini")197
||
187
197
           S.startswith(".jcr");
188
291
  }
189
291
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
173
17
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
174
17
  switch (Sec->Type) {
175
0
  case SHT_FINI_ARRAY:
176
0
  case SHT_INIT_ARRAY:
177
0
  case SHT_NOTE:
178
0
  case SHT_PREINIT_ARRAY:
179
0
    return true;
180
17
  default:
181
17
    if (!(Sec->Flags & SHF_ALLOC))
182
2
      return true;
183
15
184
15
    StringRef S = Sec->Name;
185
15
    return S.startswith(".ctors") || S.startswith(".dtors") ||
186
15
           
S.startswith(".init")15
||
S.startswith(".fini")15
||
187
15
           S.startswith(".jcr");
188
17
  }
189
17
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
173
236
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
174
236
  switch (Sec->Type) {
175
6
  case SHT_FINI_ARRAY:
176
6
  case SHT_INIT_ARRAY:
177
6
  case SHT_NOTE:
178
6
  case SHT_PREINIT_ARRAY:
179
6
    return true;
180
230
  default:
181
230
    if (!(Sec->Flags & SHF_ALLOC))
182
75
      return true;
183
155
184
155
    StringRef S = Sec->Name;
185
153
    return S.startswith(".ctors") || S.startswith(".dtors") ||
186
155
           
S.startswith(".init")151
||
S.startswith(".fini")151
||
187
151
           S.startswith(".jcr");
188
236
  }
189
236
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
173
27
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
174
27
  switch (Sec->Type) {
175
0
  case SHT_FINI_ARRAY:
176
0
  case SHT_INIT_ARRAY:
177
0
  case SHT_NOTE:
178
0
  case SHT_PREINIT_ARRAY:
179
0
    return true;
180
27
  default:
181
27
    if (!(Sec->Flags & SHF_ALLOC))
182
6
      return true;
183
21
184
21
    StringRef S = Sec->Name;
185
21
    return S.startswith(".ctors") || S.startswith(".dtors") ||
186
21
           
S.startswith(".init")21
||
S.startswith(".fini")21
||
187
21
           S.startswith(".jcr");
188
27
  }
189
27
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
173
11
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
174
11
  switch (Sec->Type) {
175
0
  case SHT_FINI_ARRAY:
176
0
  case SHT_INIT_ARRAY:
177
0
  case SHT_NOTE:
178
0
  case SHT_PREINIT_ARRAY:
179
0
    return true;
180
11
  default:
181
11
    if (!(Sec->Flags & SHF_ALLOC))
182
1
      return true;
183
10
184
10
    StringRef S = Sec->Name;
185
10
    return S.startswith(".ctors") || S.startswith(".dtors") ||
186
10
           
S.startswith(".init")10
||
S.startswith(".fini")10
||
187
10
           S.startswith(".jcr");
188
11
  }
189
11
}
190
191
// This is the main function of the garbage collector.
192
// Starting from GC-root sections, this function visits all reachable
193
// sections to set their "Live" bits.
194
56
template <class ELFT> void elf::markLive() {
195
56
  SmallVector<InputSection *, 256> Q;
196
56
  CNamedSections.clear();
197
56
198
224
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
224
    // Skip over discarded sections. This in theory shouldn't happen, because
200
224
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
224
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
224
    // .eh_frame) so we need to add a check.
203
224
    if (Sec == &InputSection::Discarded)
204
0
      return;
205
224
206
224
    // We don't gc non alloc sections.
207
224
    
if (224
!(Sec->Flags & SHF_ALLOC)224
)
208
85
      return;
209
139
210
139
    // Usually, a whole section is marked as live or dead, but in mergeable
211
139
    // (splittable) sections, each piece of data has independent liveness bit.
212
139
    // So we explicitly tell it which offset is in use.
213
139
    
if (auto *139
MS139
= dyn_cast<MergeInputSection>(Sec))
214
8
      MS->markLiveAt(Offset);
215
139
216
139
    if (Sec->Live)
217
24
      return;
218
115
    Sec->Live = true;
219
115
220
115
    // Add input section to the queue.
221
115
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
108
      Q.push_back(S);
223
224
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
198
28
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
28
    // Skip over discarded sections. This in theory shouldn't happen, because
200
28
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
28
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
28
    // .eh_frame) so we need to add a check.
203
28
    if (Sec == &InputSection::Discarded)
204
0
      return;
205
28
206
28
    // We don't gc non alloc sections.
207
28
    
if (28
!(Sec->Flags & SHF_ALLOC)28
)
208
7
      return;
209
21
210
21
    // Usually, a whole section is marked as live or dead, but in mergeable
211
21
    // (splittable) sections, each piece of data has independent liveness bit.
212
21
    // So we explicitly tell it which offset is in use.
213
21
    
if (auto *21
MS21
= dyn_cast<MergeInputSection>(Sec))
214
2
      MS->markLiveAt(Offset);
215
21
216
21
    if (Sec->Live)
217
4
      return;
218
17
    Sec->Live = true;
219
17
220
17
    // Add input section to the queue.
221
17
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
15
      Q.push_back(S);
223
28
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
198
8
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
8
    // Skip over discarded sections. This in theory shouldn't happen, because
200
8
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
8
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
8
    // .eh_frame) so we need to add a check.
203
8
    if (Sec == &InputSection::Discarded)
204
0
      return;
205
8
206
8
    // We don't gc non alloc sections.
207
8
    
if (8
!(Sec->Flags & SHF_ALLOC)8
)
208
1
      return;
209
7
210
7
    // Usually, a whole section is marked as live or dead, but in mergeable
211
7
    // (splittable) sections, each piece of data has independent liveness bit.
212
7
    // So we explicitly tell it which offset is in use.
213
7
    
if (auto *7
MS7
= dyn_cast<MergeInputSection>(Sec))
214
0
      MS->markLiveAt(Offset);
215
7
216
7
    if (Sec->Live)
217
4
      return;
218
3
    Sec->Live = true;
219
3
220
3
    // Add input section to the queue.
221
3
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
3
      Q.push_back(S);
223
8
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
198
174
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
174
    // Skip over discarded sections. This in theory shouldn't happen, because
200
174
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
174
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
174
    // .eh_frame) so we need to add a check.
203
174
    if (Sec == &InputSection::Discarded)
204
0
      return;
205
174
206
174
    // We don't gc non alloc sections.
207
174
    
if (174
!(Sec->Flags & SHF_ALLOC)174
)
208
75
      return;
209
99
210
99
    // Usually, a whole section is marked as live or dead, but in mergeable
211
99
    // (splittable) sections, each piece of data has independent liveness bit.
212
99
    // So we explicitly tell it which offset is in use.
213
99
    
if (auto *99
MS99
= dyn_cast<MergeInputSection>(Sec))
214
6
      MS->markLiveAt(Offset);
215
99
216
99
    if (Sec->Live)
217
9
      return;
218
90
    Sec->Live = true;
219
90
220
90
    // Add input section to the queue.
221
90
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
85
      Q.push_back(S);
223
174
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(lld::elf::InputSectionBase*, unsigned long long)::operator()(lld::elf::InputSectionBase*, unsigned long long) const
Line
Count
Source
198
14
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
14
    // Skip over discarded sections. This in theory shouldn't happen, because
200
14
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
14
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
14
    // .eh_frame) so we need to add a check.
203
14
    if (Sec == &InputSection::Discarded)
204
0
      return;
205
14
206
14
    // We don't gc non alloc sections.
207
14
    
if (14
!(Sec->Flags & SHF_ALLOC)14
)
208
2
      return;
209
12
210
12
    // Usually, a whole section is marked as live or dead, but in mergeable
211
12
    // (splittable) sections, each piece of data has independent liveness bit.
212
12
    // So we explicitly tell it which offset is in use.
213
12
    
if (auto *12
MS12
= dyn_cast<MergeInputSection>(Sec))
214
0
      MS->markLiveAt(Offset);
215
12
216
12
    if (Sec->Live)
217
7
      return;
218
5
    Sec->Live = true;
219
5
220
5
    // Add input section to the queue.
221
5
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
5
      Q.push_back(S);
223
14
  };
224
56
225
215
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
215
    if (auto *
D215
= dyn_cast_or_null<DefinedRegular>(Sym)) {
227
72
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
71
        Enqueue(IS, D->Value);
229
72
      return;
230
72
    }
231
143
    
if (auto *143
S143
= dyn_cast_or_null<DefinedCommon>(Sym))
232
2
      S->Live = true;
233
215
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(lld::elf::SymbolBody*)::operator()(lld::elf::SymbolBody*) const
Line
Count
Source
225
170
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
170
    if (auto *
D170
= dyn_cast_or_null<DefinedRegular>(Sym)) {
227
48
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
47
        Enqueue(IS, D->Value);
229
48
      return;
230
48
    }
231
122
    
if (auto *122
S122
= dyn_cast_or_null<DefinedCommon>(Sym))
232
2
      S->Live = true;
233
170
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(lld::elf::SymbolBody*)::operator()(lld::elf::SymbolBody*) const
Line
Count
Source
225
10
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
10
    if (auto *
D10
= dyn_cast_or_null<DefinedRegular>(Sym)) {
227
7
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
7
        Enqueue(IS, D->Value);
229
7
      return;
230
7
    }
231
3
    
if (auto *3
S3
= dyn_cast_or_null<DefinedCommon>(Sym))
232
0
      S->Live = true;
233
10
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(lld::elf::SymbolBody*)::operator()(lld::elf::SymbolBody*) const
Line
Count
Source
225
18
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
18
    if (auto *
D18
= dyn_cast_or_null<DefinedRegular>(Sym)) {
227
12
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
12
        Enqueue(IS, D->Value);
229
12
      return;
230
12
    }
231
6
    
if (auto *6
S6
= dyn_cast_or_null<DefinedCommon>(Sym))
232
0
      S->Live = true;
233
18
  };
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(lld::elf::SymbolBody*)::operator()(lld::elf::SymbolBody*) const
Line
Count
Source
225
17
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
17
    if (auto *
D17
= dyn_cast_or_null<DefinedRegular>(Sym)) {
227
5
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
5
        Enqueue(IS, D->Value);
229
5
      return;
230
5
    }
231
12
    
if (auto *12
S12
= dyn_cast_or_null<DefinedCommon>(Sym))
232
0
      S->Live = true;
233
17
  };
234
56
235
56
  // Add GC root symbols.
236
56
  MarkSymbol(Symtab->find(Config->Entry));
237
56
  MarkSymbol(Symtab->find(Config->Init));
238
56
  MarkSymbol(Symtab->find(Config->Fini));
239
56
  for (StringRef S : Config->Undefined)
240
1
    MarkSymbol(Symtab->find(S));
241
56
  for (StringRef S : Script->Opt.ReferencedSymbols)
242
1
    MarkSymbol(Symtab->find(S));
243
56
244
56
  // Preserve externally-visible symbols if the symbols defined by this
245
56
  // file can interrupt other ELF file's symbols at runtime.
246
56
  for (Symbol *S : Symtab->getSymbols())
247
110
    
if (110
S->includeInDynsym()110
)
248
45
      MarkSymbol(S->body());
249
56
250
56
  // Preserve special sections and those which are specified in linker
251
56
  // script KEEP command.
252
303
  for (InputSectionBase *Sec : InputSections) {
253
303
    // .eh_frame is always marked as live now, but also it can reference to
254
303
    // sections that contain personality. We preserve all non-text sections
255
303
    // referred by .eh_frame here.
256
303
    if (auto *EH = dyn_cast_or_null<EhInputSection>(Sec))
257
7
      scanEhFrameSection<ELFT>(*EH, Enqueue);
258
303
    if (Sec->Flags & SHF_LINK_ORDER)
259
12
      continue;
260
291
    
if (291
isReserved<ELFT>(Sec) || 291
Script->shouldKeep(Sec)197
)
261
98
      Enqueue(Sec, 0);
262
193
    else 
if (193
isValidCIdentifier(Sec->Name)193
) {
263
10
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
264
10
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
265
10
    }
266
303
  }
267
56
268
56
  // Mark all reachable sections.
269
164
  while (!Q.empty())
270
108
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
271
56
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
194
2
template <class ELFT> void elf::markLive() {
195
2
  SmallVector<InputSection *, 256> Q;
196
2
  CNamedSections.clear();
197
2
198
2
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
2
    // Skip over discarded sections. This in theory shouldn't happen, because
200
2
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
2
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
2
    // .eh_frame) so we need to add a check.
203
2
    if (Sec == &InputSection::Discarded)
204
2
      return;
205
2
206
2
    // We don't gc non alloc sections.
207
2
    if (!(Sec->Flags & SHF_ALLOC))
208
2
      return;
209
2
210
2
    // Usually, a whole section is marked as live or dead, but in mergeable
211
2
    // (splittable) sections, each piece of data has independent liveness bit.
212
2
    // So we explicitly tell it which offset is in use.
213
2
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
214
2
      MS->markLiveAt(Offset);
215
2
216
2
    if (Sec->Live)
217
2
      return;
218
2
    Sec->Live = true;
219
2
220
2
    // Add input section to the queue.
221
2
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
2
      Q.push_back(S);
223
2
  };
224
2
225
2
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
2
    if (auto *D = dyn_cast_or_null<DefinedRegular>(Sym)) {
227
2
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
2
        Enqueue(IS, D->Value);
229
2
      return;
230
2
    }
231
2
    if (auto *S = dyn_cast_or_null<DefinedCommon>(Sym))
232
2
      S->Live = true;
233
2
  };
234
2
235
2
  // Add GC root symbols.
236
2
  MarkSymbol(Symtab->find(Config->Entry));
237
2
  MarkSymbol(Symtab->find(Config->Init));
238
2
  MarkSymbol(Symtab->find(Config->Fini));
239
2
  for (StringRef S : Config->Undefined)
240
0
    MarkSymbol(Symtab->find(S));
241
2
  for (StringRef S : Script->Opt.ReferencedSymbols)
242
0
    MarkSymbol(Symtab->find(S));
243
2
244
2
  // Preserve externally-visible symbols if the symbols defined by this
245
2
  // file can interrupt other ELF file's symbols at runtime.
246
2
  for (Symbol *S : Symtab->getSymbols())
247
12
    
if (12
S->includeInDynsym()12
)
248
12
      MarkSymbol(S->body());
249
2
250
2
  // Preserve special sections and those which are specified in linker
251
2
  // script KEEP command.
252
17
  for (InputSectionBase *Sec : InputSections) {
253
17
    // .eh_frame is always marked as live now, but also it can reference to
254
17
    // sections that contain personality. We preserve all non-text sections
255
17
    // referred by .eh_frame here.
256
17
    if (auto *EH = dyn_cast_or_null<EhInputSection>(Sec))
257
0
      scanEhFrameSection<ELFT>(*EH, Enqueue);
258
17
    if (Sec->Flags & SHF_LINK_ORDER)
259
0
      continue;
260
17
    
if (17
isReserved<ELFT>(Sec) || 17
Script->shouldKeep(Sec)15
)
261
2
      Enqueue(Sec, 0);
262
15
    else 
if (15
isValidCIdentifier(Sec->Name)15
) {
263
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
264
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
265
0
    }
266
17
  }
267
2
268
2
  // Mark all reachable sections.
269
7
  while (!Q.empty())
270
5
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
271
2
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
194
1
template <class ELFT> void elf::markLive() {
195
1
  SmallVector<InputSection *, 256> Q;
196
1
  CNamedSections.clear();
197
1
198
1
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
1
    // Skip over discarded sections. This in theory shouldn't happen, because
200
1
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
1
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
1
    // .eh_frame) so we need to add a check.
203
1
    if (Sec == &InputSection::Discarded)
204
1
      return;
205
1
206
1
    // We don't gc non alloc sections.
207
1
    if (!(Sec->Flags & SHF_ALLOC))
208
1
      return;
209
1
210
1
    // Usually, a whole section is marked as live or dead, but in mergeable
211
1
    // (splittable) sections, each piece of data has independent liveness bit.
212
1
    // So we explicitly tell it which offset is in use.
213
1
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
214
1
      MS->markLiveAt(Offset);
215
1
216
1
    if (Sec->Live)
217
1
      return;
218
1
    Sec->Live = true;
219
1
220
1
    // Add input section to the queue.
221
1
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
1
      Q.push_back(S);
223
1
  };
224
1
225
1
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
1
    if (auto *D = dyn_cast_or_null<DefinedRegular>(Sym)) {
227
1
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
1
        Enqueue(IS, D->Value);
229
1
      return;
230
1
    }
231
1
    if (auto *S = dyn_cast_or_null<DefinedCommon>(Sym))
232
1
      S->Live = true;
233
1
  };
234
1
235
1
  // Add GC root symbols.
236
1
  MarkSymbol(Symtab->find(Config->Entry));
237
1
  MarkSymbol(Symtab->find(Config->Init));
238
1
  MarkSymbol(Symtab->find(Config->Fini));
239
1
  for (StringRef S : Config->Undefined)
240
0
    MarkSymbol(Symtab->find(S));
241
1
  for (StringRef S : Script->Opt.ReferencedSymbols)
242
0
    MarkSymbol(Symtab->find(S));
243
1
244
1
  // Preserve externally-visible symbols if the symbols defined by this
245
1
  // file can interrupt other ELF file's symbols at runtime.
246
1
  for (Symbol *S : Symtab->getSymbols())
247
7
    
if (7
S->includeInDynsym()7
)
248
7
      MarkSymbol(S->body());
249
1
250
1
  // Preserve special sections and those which are specified in linker
251
1
  // script KEEP command.
252
11
  for (InputSectionBase *Sec : InputSections) {
253
11
    // .eh_frame is always marked as live now, but also it can reference to
254
11
    // sections that contain personality. We preserve all non-text sections
255
11
    // referred by .eh_frame here.
256
11
    if (auto *EH = dyn_cast_or_null<EhInputSection>(Sec))
257
0
      scanEhFrameSection<ELFT>(*EH, Enqueue);
258
11
    if (Sec->Flags & SHF_LINK_ORDER)
259
0
      continue;
260
11
    
if (11
isReserved<ELFT>(Sec) || 11
Script->shouldKeep(Sec)10
)
261
1
      Enqueue(Sec, 0);
262
10
    else 
if (10
isValidCIdentifier(Sec->Name)10
) {
263
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
264
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
265
0
    }
266
11
  }
267
1
268
1
  // Mark all reachable sections.
269
4
  while (!Q.empty())
270
3
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
271
1
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
194
48
template <class ELFT> void elf::markLive() {
195
48
  SmallVector<InputSection *, 256> Q;
196
48
  CNamedSections.clear();
197
48
198
48
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
48
    // Skip over discarded sections. This in theory shouldn't happen, because
200
48
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
48
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
48
    // .eh_frame) so we need to add a check.
203
48
    if (Sec == &InputSection::Discarded)
204
48
      return;
205
48
206
48
    // We don't gc non alloc sections.
207
48
    if (!(Sec->Flags & SHF_ALLOC))
208
48
      return;
209
48
210
48
    // Usually, a whole section is marked as live or dead, but in mergeable
211
48
    // (splittable) sections, each piece of data has independent liveness bit.
212
48
    // So we explicitly tell it which offset is in use.
213
48
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
214
48
      MS->markLiveAt(Offset);
215
48
216
48
    if (Sec->Live)
217
48
      return;
218
48
    Sec->Live = true;
219
48
220
48
    // Add input section to the queue.
221
48
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
48
      Q.push_back(S);
223
48
  };
224
48
225
48
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
48
    if (auto *D = dyn_cast_or_null<DefinedRegular>(Sym)) {
227
48
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
48
        Enqueue(IS, D->Value);
229
48
      return;
230
48
    }
231
48
    if (auto *S = dyn_cast_or_null<DefinedCommon>(Sym))
232
48
      S->Live = true;
233
48
  };
234
48
235
48
  // Add GC root symbols.
236
48
  MarkSymbol(Symtab->find(Config->Entry));
237
48
  MarkSymbol(Symtab->find(Config->Init));
238
48
  MarkSymbol(Symtab->find(Config->Fini));
239
48
  for (StringRef S : Config->Undefined)
240
1
    MarkSymbol(Symtab->find(S));
241
48
  for (StringRef S : Script->Opt.ReferencedSymbols)
242
1
    MarkSymbol(Symtab->find(S));
243
48
244
48
  // Preserve externally-visible symbols if the symbols defined by this
245
48
  // file can interrupt other ELF file's symbols at runtime.
246
48
  for (Symbol *S : Symtab->getSymbols())
247
78
    
if (78
S->includeInDynsym()78
)
248
24
      MarkSymbol(S->body());
249
48
250
48
  // Preserve special sections and those which are specified in linker
251
48
  // script KEEP command.
252
241
  for (InputSectionBase *Sec : InputSections) {
253
241
    // .eh_frame is always marked as live now, but also it can reference to
254
241
    // sections that contain personality. We preserve all non-text sections
255
241
    // referred by .eh_frame here.
256
241
    if (auto *EH = dyn_cast_or_null<EhInputSection>(Sec))
257
5
      scanEhFrameSection<ELFT>(*EH, Enqueue);
258
241
    if (Sec->Flags & SHF_LINK_ORDER)
259
5
      continue;
260
236
    
if (236
isReserved<ELFT>(Sec) || 236
Script->shouldKeep(Sec)151
)
261
89
      Enqueue(Sec, 0);
262
147
    else 
if (147
isValidCIdentifier(Sec->Name)147
) {
263
10
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
264
10
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
265
10
    }
266
241
  }
267
48
268
48
  // Mark all reachable sections.
269
133
  while (!Q.empty())
270
85
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
271
48
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
194
5
template <class ELFT> void elf::markLive() {
195
5
  SmallVector<InputSection *, 256> Q;
196
5
  CNamedSections.clear();
197
5
198
5
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
199
5
    // Skip over discarded sections. This in theory shouldn't happen, because
200
5
    // the ELF spec doesn't allow a relocation to point to a deduplicated
201
5
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
202
5
    // .eh_frame) so we need to add a check.
203
5
    if (Sec == &InputSection::Discarded)
204
5
      return;
205
5
206
5
    // We don't gc non alloc sections.
207
5
    if (!(Sec->Flags & SHF_ALLOC))
208
5
      return;
209
5
210
5
    // Usually, a whole section is marked as live or dead, but in mergeable
211
5
    // (splittable) sections, each piece of data has independent liveness bit.
212
5
    // So we explicitly tell it which offset is in use.
213
5
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
214
5
      MS->markLiveAt(Offset);
215
5
216
5
    if (Sec->Live)
217
5
      return;
218
5
    Sec->Live = true;
219
5
220
5
    // Add input section to the queue.
221
5
    if (InputSection *S = dyn_cast<InputSection>(Sec))
222
5
      Q.push_back(S);
223
5
  };
224
5
225
5
  auto MarkSymbol = [&](SymbolBody *Sym) {
226
5
    if (auto *D = dyn_cast_or_null<DefinedRegular>(Sym)) {
227
5
      if (auto *IS = cast_or_null<InputSectionBase>(D->Section))
228
5
        Enqueue(IS, D->Value);
229
5
      return;
230
5
    }
231
5
    if (auto *S = dyn_cast_or_null<DefinedCommon>(Sym))
232
5
      S->Live = true;
233
5
  };
234
5
235
5
  // Add GC root symbols.
236
5
  MarkSymbol(Symtab->find(Config->Entry));
237
5
  MarkSymbol(Symtab->find(Config->Init));
238
5
  MarkSymbol(Symtab->find(Config->Fini));
239
5
  for (StringRef S : Config->Undefined)
240
0
    MarkSymbol(Symtab->find(S));
241
5
  for (StringRef S : Script->Opt.ReferencedSymbols)
242
0
    MarkSymbol(Symtab->find(S));
243
5
244
5
  // Preserve externally-visible symbols if the symbols defined by this
245
5
  // file can interrupt other ELF file's symbols at runtime.
246
5
  for (Symbol *S : Symtab->getSymbols())
247
13
    
if (13
S->includeInDynsym()13
)
248
2
      MarkSymbol(S->body());
249
5
250
5
  // Preserve special sections and those which are specified in linker
251
5
  // script KEEP command.
252
34
  for (InputSectionBase *Sec : InputSections) {
253
34
    // .eh_frame is always marked as live now, but also it can reference to
254
34
    // sections that contain personality. We preserve all non-text sections
255
34
    // referred by .eh_frame here.
256
34
    if (auto *EH = dyn_cast_or_null<EhInputSection>(Sec))
257
2
      scanEhFrameSection<ELFT>(*EH, Enqueue);
258
34
    if (Sec->Flags & SHF_LINK_ORDER)
259
7
      continue;
260
27
    
if (27
isReserved<ELFT>(Sec) || 27
Script->shouldKeep(Sec)21
)
261
6
      Enqueue(Sec, 0);
262
21
    else 
if (21
isValidCIdentifier(Sec->Name)21
) {
263
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
264
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
265
0
    }
266
34
  }
267
5
268
5
  // Mark all reachable sections.
269
20
  while (!Q.empty())
270
15
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
271
5
}
272
273
template void elf::markLive<ELF32LE>();
274
template void elf::markLive<ELF32BE>();
275
template void elf::markLive<ELF64LE>();
276
template void elf::markLive<ELF64BE>();