Coverage Report

Created: 2018-01-17 17:22

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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 "OutputSections.h"
26
#include "Strings.h"
27
#include "SymbolTable.h"
28
#include "Symbols.h"
29
#include "Target.h"
30
#include "Writer.h"
31
#include "lld/Common/Memory.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)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)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, 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
28
                                     const typename ELFT::Rela &Rel) {
55
28
  return Rel.r_addend;
56
28
}
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&)
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&)
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
28
                                     const typename ELFT::Rela &Rel) {
55
28
  return Rel.r_addend;
56
28
}
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&)
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
76
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
76
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
76
67
76
  // If a symbol is referenced in a live section, it is used.
68
76
  B.Used = true;
69
76
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
70
8
    if (!SS->isWeak())
71
6
      SS->getFile<ELFT>().IsNeeded = true;
72
76
73
76
  if (auto *D = dyn_cast<Defined>(&B)) {
74
57
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
75
57
    if (!RelSec)
76
1
      return;
77
56
    uint64_t Offset = D->Value;
78
56
    if (D->isSection())
79
39
      Offset += getAddend<ELFT>(Sec, Rel);
80
56
    Fn(RelSec, Offset);
81
56
    return;
82
56
  }
83
19
84
19
  if (!B.isDefined())
85
19
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
86
7
      Fn(Sec, 0);
87
19
}
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
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
15
67
15
  // If a symbol is referenced in a live section, it is used.
68
15
  B.Used = true;
69
15
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
70
0
    if (!SS->isWeak())
71
0
      SS->getFile<ELFT>().IsNeeded = true;
72
15
73
15
  if (auto *D = dyn_cast<Defined>(&B)) {
74
15
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
75
15
    if (!RelSec)
76
0
      return;
77
15
    uint64_t Offset = D->Value;
78
15
    if (D->isSection())
79
11
      Offset += getAddend<ELFT>(Sec, Rel);
80
15
    Fn(RelSec, Offset);
81
15
    return;
82
15
  }
83
0
84
0
  if (!B.isDefined())
85
0
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
86
0
      Fn(Sec, 0);
87
0
}
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, 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
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
1
67
1
  // If a symbol is referenced in a live section, it is used.
68
1
  B.Used = true;
69
1
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
70
0
    if (!SS->isWeak())
71
0
      SS->getFile<ELFT>().IsNeeded = true;
72
1
73
1
  if (auto *D = dyn_cast<Defined>(&B)) {
74
0
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
75
0
    if (!RelSec)
76
0
      return;
77
0
    uint64_t Offset = D->Value;
78
0
    if (D->isSection())
79
0
      Offset += getAddend<ELFT>(Sec, Rel);
80
0
    Fn(RelSec, Offset);
81
0
    return;
82
0
  }
83
1
84
1
  if (!B.isDefined())
85
1
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
86
0
      Fn(Sec, 0);
87
1
}
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
59
                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
65
59
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
59
67
59
  // If a symbol is referenced in a live section, it is used.
68
59
  B.Used = true;
69
59
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
70
8
    if (!SS->isWeak())
71
6
      SS->getFile<ELFT>().IsNeeded = true;
72
59
73
59
  if (auto *D = dyn_cast<Defined>(&B)) {
74
42
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
75
42
    if (!RelSec)
76
1
      return;
77
41
    uint64_t Offset = D->Value;
78
41
    if (D->isSection())
79
28
      Offset += getAddend<ELFT>(Sec, Rel);
80
41
    Fn(RelSec, Offset);
81
41
    return;
82
41
  }
83
17
84
17
  if (!B.isDefined())
85
17
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
86
7
      Fn(Sec, 0);
87
17
}
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)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
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
66
1
67
1
  // If a symbol is referenced in a live section, it is used.
68
1
  B.Used = true;
69
1
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
70
0
    if (!SS->isWeak())
71
0
      SS->getFile<ELFT>().IsNeeded = true;
72
1
73
1
  if (auto *D = dyn_cast<Defined>(&B)) {
74
0
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
75
0
    if (!RelSec)
76
0
      return;
77
0
    uint64_t Offset = D->Value;
78
0
    if (D->isSection())
79
0
      Offset += getAddend<ELFT>(Sec, Rel);
80
0
    Fn(RelSec, Offset);
81
0
    return;
82
0
  }
83
1
84
1
  if (!B.isDefined())
85
1
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
86
0
      Fn(Sec, 0);
87
1
}
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)>)
88
89
// Calls Fn for each section that Sec refers to via relocations.
90
template <class ELFT>
91
static void
92
forEachSuccessor(InputSection &Sec,
93
126
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
94
126
  if (Sec.AreRelocsRela) {
95
43
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
96
54
      resolveReloc<ELFT>(Sec, Rel, Fn);
97
83
  } else {
98
83
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
99
12
      resolveReloc<ELFT>(Sec, Rel, Fn);
100
83
  }
101
126
102
126
  for (InputSectionBase *IS : Sec.DependentSections)
103
8
    Fn(IS, 0);
104
126
}
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
93
15
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
94
15
  if (Sec.AreRelocsRela) {
95
0
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
96
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
97
15
  } else {
98
15
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
99
11
      resolveReloc<ELFT>(Sec, Rel, Fn);
100
15
  }
101
15
102
15
  for (InputSectionBase *IS : Sec.DependentSections)
103
4
    Fn(IS, 0);
104
15
}
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
93
5
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
94
5
  if (Sec.AreRelocsRela) {
95
0
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
96
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
97
5
  } else {
98
5
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
99
1
      resolveReloc<ELFT>(Sec, Rel, Fn);
100
5
  }
101
5
102
5
  for (InputSectionBase *IS : Sec.DependentSections)
103
0
    Fn(IS, 0);
104
5
}
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
93
103
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
94
103
  if (Sec.AreRelocsRela) {
95
42
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
96
53
      resolveReloc<ELFT>(Sec, Rel, Fn);
97
61
  } else {
98
61
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
99
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
100
61
  }
101
103
102
103
  for (InputSectionBase *IS : Sec.DependentSections)
103
4
    Fn(IS, 0);
104
103
}
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
93
3
                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
94
3
  if (Sec.AreRelocsRela) {
95
1
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
96
1
      resolveReloc<ELFT>(Sec, Rel, Fn);
97
2
  } else {
98
2
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
99
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
100
2
  }
101
3
102
3
  for (InputSectionBase *IS : Sec.DependentSections)
103
0
    Fn(IS, 0);
104
3
}
105
106
// The .eh_frame section is an unfortunate special case.
107
// The section is divided in CIEs and FDEs and the relocations it can have are
108
// * CIEs can refer to a personality function.
109
// * FDEs can refer to a LSDA
110
// * FDEs refer to the function they contain information about
111
// The last kind of relocation cannot keep the referred section alive, or they
112
// would keep everything alive in a common object file. In fact, each FDE is
113
// alive if the section it refers to is alive.
114
// To keep things simple, in here we just ignore the last relocation kind. The
115
// other two keep the referred section alive.
116
//
117
// A possible improvement would be to fully process .eh_frame in the middle of
118
// the gc pass. With that we would be able to also gc some sections holding
119
// LSDAs and personality functions if we found that they were unused.
120
template <class ELFT, class RelTy>
121
static void
122
scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels,
123
5
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
124
5
  const endianness E = ELFT::TargetEndianness;
125
5
126
17
  for (unsigned I = 0, N = EH.Pieces.size(); I < N; 
++I12
) {
127
12
    EhSectionPiece &Piece = EH.Pieces[I];
128
12
    unsigned FirstRelI = Piece.FirstRelocation;
129
12
    if (FirstRelI == (unsigned)-1)
130
3
      continue;
131
9
    if (read32<E>(Piece.data().data() + 4) == 0) {
132
2
      // This is a CIE, we only need to worry about the first relocation. It is
133
2
      // known to point to the personality function.
134
2
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
135
2
      continue;
136
2
    }
137
7
    // This is a FDE. The relocations point to the described function or to
138
7
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
139
7
    // points to executable sections.
140
7
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
141
15
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); I2 < N2; 
++I28
) {
142
10
      const RelTy &Rel = Rels[I2];
143
10
      if (Rel.r_offset >= PieceEnd)
144
2
        break;
145
8
      resolveReloc<ELFT>(EH, Rels[I2],
146
8
                         [&](InputSectionBase *Sec, uint64_t Offset) {
147
8
                           if (Sec && Sec != &InputSection::Discarded &&
148
8
                               !(Sec->Flags & SHF_EXECINSTR))
149
1
                             Fn(Sec, 0);
150
8
                         });
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, 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
146
2
                         [&](InputSectionBase *Sec, uint64_t Offset) {
147
2
                           if (Sec && Sec != &InputSection::Discarded &&
148
2
                               !(Sec->Flags & SHF_EXECINSTR))
149
0
                             Fn(Sec, 0);
150
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)>)::'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
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
146
6
                         [&](InputSectionBase *Sec, uint64_t Offset) {
147
6
                           if (Sec && Sec != &InputSection::Discarded &&
148
6
                               !(Sec->Flags & SHF_EXECINSTR))
149
1
                             Fn(Sec, 0);
150
6
                         });
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>, 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)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
151
8
    }
152
7
  }
153
5
}
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
123
2
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
124
2
  const endianness E = ELFT::TargetEndianness;
125
2
126
6
  for (unsigned I = 0, N = EH.Pieces.size(); I < N; 
++I4
) {
127
4
    EhSectionPiece &Piece = EH.Pieces[I];
128
4
    unsigned FirstRelI = Piece.FirstRelocation;
129
4
    if (FirstRelI == (unsigned)-1)
130
0
      continue;
131
4
    if (read32<E>(Piece.data().data() + 4) == 0) {
132
2
      // This is a CIE, we only need to worry about the first relocation. It is
133
2
      // known to point to the personality function.
134
2
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
135
2
      continue;
136
2
    }
137
2
    // This is a FDE. The relocations point to the described function or to
138
2
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
139
2
    // points to executable sections.
140
2
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
141
4
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); I2 < N2; 
++I22
) {
142
2
      const RelTy &Rel = Rels[I2];
143
2
      if (Rel.r_offset >= PieceEnd)
144
0
        break;
145
2
      resolveReloc<ELFT>(EH, Rels[I2],
146
2
                         [&](InputSectionBase *Sec, uint64_t Offset) {
147
2
                           if (Sec && Sec != &InputSection::Discarded &&
148
2
                               !(Sec->Flags & SHF_EXECINSTR))
149
2
                             Fn(Sec, 0);
150
2
                         });
151
2
    }
152
2
  }
153
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)>)
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)>)
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
123
3
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
124
3
  const endianness E = ELFT::TargetEndianness;
125
3
126
11
  for (unsigned I = 0, N = EH.Pieces.size(); I < N; 
++I8
) {
127
8
    EhSectionPiece &Piece = EH.Pieces[I];
128
8
    unsigned FirstRelI = Piece.FirstRelocation;
129
8
    if (FirstRelI == (unsigned)-1)
130
3
      continue;
131
5
    if (read32<E>(Piece.data().data() + 4) == 0) {
132
0
      // This is a CIE, we only need to worry about the first relocation. It is
133
0
      // known to point to the personality function.
134
0
      resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
135
0
      continue;
136
0
    }
137
5
    // This is a FDE. The relocations point to the described function or to
138
5
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
139
5
    // points to executable sections.
140
5
    typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
141
11
    for (unsigned I2 = FirstRelI, N2 = Rels.size(); I2 < N2; 
++I26
) {
142
8
      const RelTy &Rel = Rels[I2];
143
8
      if (Rel.r_offset >= PieceEnd)
144
2
        break;
145
6
      resolveReloc<ELFT>(EH, Rels[I2],
146
6
                         [&](InputSectionBase *Sec, uint64_t Offset) {
147
6
                           if (Sec && Sec != &InputSection::Discarded &&
148
6
                               !(Sec->Flags & SHF_EXECINSTR))
149
6
                             Fn(Sec, 0);
150
6
                         });
151
6
    }
152
5
  }
153
3
}
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)>)
154
155
template <class ELFT>
156
static void
157
scanEhFrameSection(EhInputSection &EH,
158
7
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
159
7
  if (!EH.NumRelocations)
160
2
    return;
161
5
162
5
  // Unfortunately we need to split .eh_frame early since some relocations in
163
5
  // .eh_frame keep other section alive and some don't.
164
5
  EH.split<ELFT>();
165
5
166
5
  if (EH.AreRelocsRela)
167
3
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
168
2
  else
169
2
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
170
5
}
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
158
2
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
159
2
  if (!EH.NumRelocations)
160
0
    return;
161
2
162
2
  // Unfortunately we need to split .eh_frame early since some relocations in
163
2
  // .eh_frame keep other section alive and some don't.
164
2
  EH.split<ELFT>();
165
2
166
2
  if (EH.AreRelocsRela)
167
0
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
168
2
  else
169
2
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
170
2
}
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, true> >(lld::elf::EhInputSection&, std::__1::function<void (lld::elf::InputSectionBase*, unsigned long long)>)
Line
Count
Source
158
5
                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
159
5
  if (!EH.NumRelocations)
160
2
    return;
161
3
162
3
  // Unfortunately we need to split .eh_frame early since some relocations in
163
3
  // .eh_frame keep other section alive and some don't.
164
3
  EH.split<ELFT>();
165
3
166
3
  if (EH.AreRelocsRela)
167
3
    scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
168
0
  else
169
0
    scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
170
3
}
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)>)
171
172
// Some sections are used directly by the loader, so they should never be
173
// garbage-collected. This function returns true if a given section is such
174
// section.
175
324
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
176
324
  switch (Sec->Type) {
177
324
  case SHT_FINI_ARRAY:
178
6
  case SHT_INIT_ARRAY:
179
6
  case SHT_NOTE:
180
6
  case SHT_PREINIT_ARRAY:
181
6
    return true;
182
318
  default:
183
318
    StringRef S = Sec->Name;
184
318
    return S.startswith(".ctors") || 
S.startswith(".dtors")316
||
185
318
           
S.startswith(".init")314
||
S.startswith(".fini")314
||
186
318
           
S.startswith(".jcr")314
;
187
0
  }
188
0
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
175
27
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
176
27
  switch (Sec->Type) {
177
27
  case SHT_FINI_ARRAY:
178
0
  case SHT_INIT_ARRAY:
179
0
  case SHT_NOTE:
180
0
  case SHT_PREINIT_ARRAY:
181
0
    return true;
182
27
  default:
183
27
    StringRef S = Sec->Name;
184
27
    return S.startswith(".ctors") || S.startswith(".dtors") ||
185
27
           S.startswith(".init") || S.startswith(".fini") ||
186
27
           S.startswith(".jcr");
187
0
  }
188
0
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
175
17
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
176
17
  switch (Sec->Type) {
177
17
  case SHT_FINI_ARRAY:
178
0
  case SHT_INIT_ARRAY:
179
0
  case SHT_NOTE:
180
0
  case SHT_PREINIT_ARRAY:
181
0
    return true;
182
17
  default:
183
17
    StringRef S = Sec->Name;
184
17
    return S.startswith(".ctors") || S.startswith(".dtors") ||
185
17
           S.startswith(".init") || S.startswith(".fini") ||
186
17
           S.startswith(".jcr");
187
0
  }
188
0
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
175
269
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
176
269
  switch (Sec->Type) {
177
269
  case SHT_FINI_ARRAY:
178
6
  case SHT_INIT_ARRAY:
179
6
  case SHT_NOTE:
180
6
  case SHT_PREINIT_ARRAY:
181
6
    return true;
182
263
  default:
183
263
    StringRef S = Sec->Name;
184
263
    return S.startswith(".ctors") || 
S.startswith(".dtors")261
||
185
263
           
S.startswith(".init")259
||
S.startswith(".fini")259
||
186
263
           
S.startswith(".jcr")259
;
187
0
  }
188
0
}
MarkLive.cpp:bool isReserved<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
175
11
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
176
11
  switch (Sec->Type) {
177
11
  case SHT_FINI_ARRAY:
178
0
  case SHT_INIT_ARRAY:
179
0
  case SHT_NOTE:
180
0
  case SHT_PREINIT_ARRAY:
181
0
    return true;
182
11
  default:
183
11
    StringRef S = Sec->Name;
184
11
    return S.startswith(".ctors") || S.startswith(".dtors") ||
185
11
           S.startswith(".init") || S.startswith(".fini") ||
186
11
           S.startswith(".jcr");
187
0
  }
188
0
}
189
190
// This is the main function of the garbage collector.
191
// Starting from GC-root sections, this function visits all reachable
192
// sections to set their "Live" bits.
193
63
template <class ELFT> static void doGcSections() {
194
63
  SmallVector<InputSection *, 256> Q;
195
63
  CNamedSections.clear();
196
63
197
162
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
162
    // Skip over discarded sections. This in theory shouldn't happen, because
199
162
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
162
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
162
    // .eh_frame) so we need to add a check.
202
162
    if (Sec == &InputSection::Discarded)
203
0
      return;
204
162
205
162
206
162
    // Usually, a whole section is marked as live or dead, but in mergeable
207
162
    // (splittable) sections, each piece of data has independent liveness bit.
208
162
    // So we explicitly tell it which offset is in use.
209
162
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
8
      MS->markLiveAt(Offset);
211
162
212
162
    if (Sec->Live)
213
29
      return;
214
133
    Sec->Live = true;
215
133
216
133
    // Add input section to the queue.
217
133
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
126
      Q.push_back(S);
219
133
  };
MarkLive.cpp:void doGcSections<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
197
22
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
22
    // Skip over discarded sections. This in theory shouldn't happen, because
199
22
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
22
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
22
    // .eh_frame) so we need to add a check.
202
22
    if (Sec == &InputSection::Discarded)
203
0
      return;
204
22
205
22
206
22
    // Usually, a whole section is marked as live or dead, but in mergeable
207
22
    // (splittable) sections, each piece of data has independent liveness bit.
208
22
    // So we explicitly tell it which offset is in use.
209
22
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
2
      MS->markLiveAt(Offset);
211
22
212
22
    if (Sec->Live)
213
5
      return;
214
17
    Sec->Live = true;
215
17
216
17
    // Add input section to the queue.
217
17
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
15
      Q.push_back(S);
219
17
  };
MarkLive.cpp:void doGcSections<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
197
12
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
12
    // Skip over discarded sections. This in theory shouldn't happen, because
199
12
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
12
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
12
    // .eh_frame) so we need to add a check.
202
12
    if (Sec == &InputSection::Discarded)
203
0
      return;
204
12
205
12
206
12
    // Usually, a whole section is marked as live or dead, but in mergeable
207
12
    // (splittable) sections, each piece of data has independent liveness bit.
208
12
    // So we explicitly tell it which offset is in use.
209
12
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
0
      MS->markLiveAt(Offset);
211
12
212
12
    if (Sec->Live)
213
7
      return;
214
5
    Sec->Live = true;
215
5
216
5
    // Add input section to the queue.
217
5
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
5
      Q.push_back(S);
219
5
  };
MarkLive.cpp:void doGcSections<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
197
121
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
121
    // Skip over discarded sections. This in theory shouldn't happen, because
199
121
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
121
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
121
    // .eh_frame) so we need to add a check.
202
121
    if (Sec == &InputSection::Discarded)
203
0
      return;
204
121
205
121
206
121
    // Usually, a whole section is marked as live or dead, but in mergeable
207
121
    // (splittable) sections, each piece of data has independent liveness bit.
208
121
    // So we explicitly tell it which offset is in use.
209
121
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
6
      MS->markLiveAt(Offset);
211
121
212
121
    if (Sec->Live)
213
13
      return;
214
108
    Sec->Live = true;
215
108
216
108
    // Add input section to the queue.
217
108
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
103
      Q.push_back(S);
219
108
  };
MarkLive.cpp:void doGcSections<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
197
7
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
7
    // Skip over discarded sections. This in theory shouldn't happen, because
199
7
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
7
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
7
    // .eh_frame) so we need to add a check.
202
7
    if (Sec == &InputSection::Discarded)
203
0
      return;
204
7
205
7
206
7
    // Usually, a whole section is marked as live or dead, but in mergeable
207
7
    // (splittable) sections, each piece of data has independent liveness bit.
208
7
    // So we explicitly tell it which offset is in use.
209
7
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
0
      MS->markLiveAt(Offset);
211
7
212
7
    if (Sec->Live)
213
4
      return;
214
3
    Sec->Live = true;
215
3
216
3
    // Add input section to the queue.
217
3
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
3
      Q.push_back(S);
219
3
  };
220
63
221
257
  auto MarkSymbol = [&](Symbol *Sym) {
222
257
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
86
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
84
        Enqueue(IS, D->Value);
225
257
  };
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
221
17
  auto MarkSymbol = [&](Symbol *Sym) {
222
17
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
5
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
5
        Enqueue(IS, D->Value);
225
17
  };
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
221
18
  auto MarkSymbol = [&](Symbol *Sym) {
222
18
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
12
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
12
        Enqueue(IS, D->Value);
225
18
  };
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
221
212
  auto MarkSymbol = [&](Symbol *Sym) {
222
212
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
62
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
60
        Enqueue(IS, D->Value);
225
212
  };
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
221
10
  auto MarkSymbol = [&](Symbol *Sym) {
222
10
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
7
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
7
        Enqueue(IS, D->Value);
225
10
  };
226
63
227
63
  // Add GC root symbols.
228
63
  MarkSymbol(Symtab->find(Config->Entry));
229
63
  MarkSymbol(Symtab->find(Config->Init));
230
63
  MarkSymbol(Symtab->find(Config->Fini));
231
63
  for (StringRef S : Config->Undefined)
232
1
    MarkSymbol(Symtab->find(S));
233
63
  for (StringRef S : Script->ReferencedSymbols)
234
1
    MarkSymbol(Symtab->find(S));
235
63
236
63
  // Preserve externally-visible symbols if the symbols defined by this
237
63
  // file can interrupt other ELF file's symbols at runtime.
238
63
  for (Symbol *S : Symtab->getSymbols())
239
140
    if (S->includeInDynsym())
240
66
      MarkSymbol(S);
241
63
242
63
  // Preserve special sections and those which are specified in linker
243
63
  // script KEEP command.
244
336
  for (InputSectionBase *Sec : InputSections) {
245
336
    // Mark .eh_frame sections as live because there are usually no relocations
246
336
    // that point to .eh_frames. Otherwise, the garbage collector would drop
247
336
    // all of them. We also want to preserve personality routines and LSDA
248
336
    // referenced by .eh_frame sections, so we scan them for that here.
249
336
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
250
7
      EH->Live = true;
251
7
      scanEhFrameSection<ELFT>(*EH, Enqueue);
252
7
    }
253
336
254
336
    if (Sec->Flags & SHF_LINK_ORDER)
255
12
      continue;
256
324
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)314
)
257
14
      Enqueue(Sec, 0);
258
310
    else if (isValidCIdentifier(Sec->Name)) {
259
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
260
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
261
18
    }
262
324
  }
263
63
264
63
  // Mark all reachable sections.
265
189
  while (!Q.empty())
266
126
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
267
63
}
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
193
5
template <class ELFT> static void doGcSections() {
194
5
  SmallVector<InputSection *, 256> Q;
195
5
  CNamedSections.clear();
196
5
197
5
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
5
    // Skip over discarded sections. This in theory shouldn't happen, because
199
5
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
5
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
5
    // .eh_frame) so we need to add a check.
202
5
    if (Sec == &InputSection::Discarded)
203
5
      return;
204
5
205
5
206
5
    // Usually, a whole section is marked as live or dead, but in mergeable
207
5
    // (splittable) sections, each piece of data has independent liveness bit.
208
5
    // So we explicitly tell it which offset is in use.
209
5
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
5
      MS->markLiveAt(Offset);
211
5
212
5
    if (Sec->Live)
213
5
      return;
214
5
    Sec->Live = true;
215
5
216
5
    // Add input section to the queue.
217
5
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
5
      Q.push_back(S);
219
5
  };
220
5
221
5
  auto MarkSymbol = [&](Symbol *Sym) {
222
5
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
5
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
5
        Enqueue(IS, D->Value);
225
5
  };
226
5
227
5
  // Add GC root symbols.
228
5
  MarkSymbol(Symtab->find(Config->Entry));
229
5
  MarkSymbol(Symtab->find(Config->Init));
230
5
  MarkSymbol(Symtab->find(Config->Fini));
231
5
  for (StringRef S : Config->Undefined)
232
0
    MarkSymbol(Symtab->find(S));
233
5
  for (StringRef S : Script->ReferencedSymbols)
234
0
    MarkSymbol(Symtab->find(S));
235
5
236
5
  // Preserve externally-visible symbols if the symbols defined by this
237
5
  // file can interrupt other ELF file's symbols at runtime.
238
5
  for (Symbol *S : Symtab->getSymbols())
239
13
    if (S->includeInDynsym())
240
2
      MarkSymbol(S);
241
5
242
5
  // Preserve special sections and those which are specified in linker
243
5
  // script KEEP command.
244
34
  for (InputSectionBase *Sec : InputSections) {
245
34
    // Mark .eh_frame sections as live because there are usually no relocations
246
34
    // that point to .eh_frames. Otherwise, the garbage collector would drop
247
34
    // all of them. We also want to preserve personality routines and LSDA
248
34
    // referenced by .eh_frame sections, so we scan them for that here.
249
34
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
250
2
      EH->Live = true;
251
2
      scanEhFrameSection<ELFT>(*EH, Enqueue);
252
2
    }
253
34
254
34
    if (Sec->Flags & SHF_LINK_ORDER)
255
7
      continue;
256
27
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec))
257
0
      Enqueue(Sec, 0);
258
27
    else if (isValidCIdentifier(Sec->Name)) {
259
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
260
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
261
0
    }
262
27
  }
263
5
264
5
  // Mark all reachable sections.
265
20
  while (!Q.empty())
266
15
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
267
5
}
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
193
2
template <class ELFT> static void doGcSections() {
194
2
  SmallVector<InputSection *, 256> Q;
195
2
  CNamedSections.clear();
196
2
197
2
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
2
    // Skip over discarded sections. This in theory shouldn't happen, because
199
2
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
2
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
2
    // .eh_frame) so we need to add a check.
202
2
    if (Sec == &InputSection::Discarded)
203
2
      return;
204
2
205
2
206
2
    // Usually, a whole section is marked as live or dead, but in mergeable
207
2
    // (splittable) sections, each piece of data has independent liveness bit.
208
2
    // So we explicitly tell it which offset is in use.
209
2
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
2
      MS->markLiveAt(Offset);
211
2
212
2
    if (Sec->Live)
213
2
      return;
214
2
    Sec->Live = true;
215
2
216
2
    // Add input section to the queue.
217
2
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
2
      Q.push_back(S);
219
2
  };
220
2
221
2
  auto MarkSymbol = [&](Symbol *Sym) {
222
2
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
2
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
2
        Enqueue(IS, D->Value);
225
2
  };
226
2
227
2
  // Add GC root symbols.
228
2
  MarkSymbol(Symtab->find(Config->Entry));
229
2
  MarkSymbol(Symtab->find(Config->Init));
230
2
  MarkSymbol(Symtab->find(Config->Fini));
231
2
  for (StringRef S : Config->Undefined)
232
0
    MarkSymbol(Symtab->find(S));
233
2
  for (StringRef S : Script->ReferencedSymbols)
234
0
    MarkSymbol(Symtab->find(S));
235
2
236
2
  // Preserve externally-visible symbols if the symbols defined by this
237
2
  // file can interrupt other ELF file's symbols at runtime.
238
2
  for (Symbol *S : Symtab->getSymbols())
239
14
    if (S->includeInDynsym())
240
12
      MarkSymbol(S);
241
2
242
2
  // Preserve special sections and those which are specified in linker
243
2
  // script KEEP command.
244
17
  for (InputSectionBase *Sec : InputSections) {
245
17
    // Mark .eh_frame sections as live because there are usually no relocations
246
17
    // that point to .eh_frames. Otherwise, the garbage collector would drop
247
17
    // all of them. We also want to preserve personality routines and LSDA
248
17
    // referenced by .eh_frame sections, so we scan them for that here.
249
17
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
250
0
      EH->Live = true;
251
0
      scanEhFrameSection<ELFT>(*EH, Enqueue);
252
0
    }
253
17
254
17
    if (Sec->Flags & SHF_LINK_ORDER)
255
0
      continue;
256
17
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec))
257
0
      Enqueue(Sec, 0);
258
17
    else if (isValidCIdentifier(Sec->Name)) {
259
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
260
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
261
0
    }
262
17
  }
263
2
264
2
  // Mark all reachable sections.
265
7
  while (!Q.empty())
266
5
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
267
2
}
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
193
55
template <class ELFT> static void doGcSections() {
194
55
  SmallVector<InputSection *, 256> Q;
195
55
  CNamedSections.clear();
196
55
197
55
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
55
    // Skip over discarded sections. This in theory shouldn't happen, because
199
55
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
55
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
55
    // .eh_frame) so we need to add a check.
202
55
    if (Sec == &InputSection::Discarded)
203
55
      return;
204
55
205
55
206
55
    // Usually, a whole section is marked as live or dead, but in mergeable
207
55
    // (splittable) sections, each piece of data has independent liveness bit.
208
55
    // So we explicitly tell it which offset is in use.
209
55
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
55
      MS->markLiveAt(Offset);
211
55
212
55
    if (Sec->Live)
213
55
      return;
214
55
    Sec->Live = true;
215
55
216
55
    // Add input section to the queue.
217
55
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
55
      Q.push_back(S);
219
55
  };
220
55
221
55
  auto MarkSymbol = [&](Symbol *Sym) {
222
55
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
55
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
55
        Enqueue(IS, D->Value);
225
55
  };
226
55
227
55
  // Add GC root symbols.
228
55
  MarkSymbol(Symtab->find(Config->Entry));
229
55
  MarkSymbol(Symtab->find(Config->Init));
230
55
  MarkSymbol(Symtab->find(Config->Fini));
231
55
  for (StringRef S : Config->Undefined)
232
1
    MarkSymbol(Symtab->find(S));
233
55
  for (StringRef S : Script->ReferencedSymbols)
234
1
    MarkSymbol(Symtab->find(S));
235
55
236
55
  // Preserve externally-visible symbols if the symbols defined by this
237
55
  // file can interrupt other ELF file's symbols at runtime.
238
55
  for (Symbol *S : Symtab->getSymbols())
239
105
    if (S->includeInDynsym())
240
45
      MarkSymbol(S);
241
55
242
55
  // Preserve special sections and those which are specified in linker
243
55
  // script KEEP command.
244
274
  for (InputSectionBase *Sec : InputSections) {
245
274
    // Mark .eh_frame sections as live because there are usually no relocations
246
274
    // that point to .eh_frames. Otherwise, the garbage collector would drop
247
274
    // all of them. We also want to preserve personality routines and LSDA
248
274
    // referenced by .eh_frame sections, so we scan them for that here.
249
274
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
250
5
      EH->Live = true;
251
5
      scanEhFrameSection<ELFT>(*EH, Enqueue);
252
5
    }
253
274
254
274
    if (Sec->Flags & SHF_LINK_ORDER)
255
5
      continue;
256
269
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)259
)
257
14
      Enqueue(Sec, 0);
258
255
    else if (isValidCIdentifier(Sec->Name)) {
259
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
260
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
261
18
    }
262
269
  }
263
55
264
55
  // Mark all reachable sections.
265
158
  while (!Q.empty())
266
103
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
267
55
}
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
193
1
template <class ELFT> static void doGcSections() {
194
1
  SmallVector<InputSection *, 256> Q;
195
1
  CNamedSections.clear();
196
1
197
1
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
198
1
    // Skip over discarded sections. This in theory shouldn't happen, because
199
1
    // the ELF spec doesn't allow a relocation to point to a deduplicated
200
1
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
201
1
    // .eh_frame) so we need to add a check.
202
1
    if (Sec == &InputSection::Discarded)
203
1
      return;
204
1
205
1
206
1
    // Usually, a whole section is marked as live or dead, but in mergeable
207
1
    // (splittable) sections, each piece of data has independent liveness bit.
208
1
    // So we explicitly tell it which offset is in use.
209
1
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
210
1
      MS->markLiveAt(Offset);
211
1
212
1
    if (Sec->Live)
213
1
      return;
214
1
    Sec->Live = true;
215
1
216
1
    // Add input section to the queue.
217
1
    if (InputSection *S = dyn_cast<InputSection>(Sec))
218
1
      Q.push_back(S);
219
1
  };
220
1
221
1
  auto MarkSymbol = [&](Symbol *Sym) {
222
1
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
223
1
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
224
1
        Enqueue(IS, D->Value);
225
1
  };
226
1
227
1
  // Add GC root symbols.
228
1
  MarkSymbol(Symtab->find(Config->Entry));
229
1
  MarkSymbol(Symtab->find(Config->Init));
230
1
  MarkSymbol(Symtab->find(Config->Fini));
231
1
  for (StringRef S : Config->Undefined)
232
0
    MarkSymbol(Symtab->find(S));
233
1
  for (StringRef S : Script->ReferencedSymbols)
234
0
    MarkSymbol(Symtab->find(S));
235
1
236
1
  // Preserve externally-visible symbols if the symbols defined by this
237
1
  // file can interrupt other ELF file's symbols at runtime.
238
1
  for (Symbol *S : Symtab->getSymbols())
239
8
    if (S->includeInDynsym())
240
7
      MarkSymbol(S);
241
1
242
1
  // Preserve special sections and those which are specified in linker
243
1
  // script KEEP command.
244
11
  for (InputSectionBase *Sec : InputSections) {
245
11
    // Mark .eh_frame sections as live because there are usually no relocations
246
11
    // that point to .eh_frames. Otherwise, the garbage collector would drop
247
11
    // all of them. We also want to preserve personality routines and LSDA
248
11
    // referenced by .eh_frame sections, so we scan them for that here.
249
11
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
250
0
      EH->Live = true;
251
0
      scanEhFrameSection<ELFT>(*EH, Enqueue);
252
0
    }
253
11
254
11
    if (Sec->Flags & SHF_LINK_ORDER)
255
0
      continue;
256
11
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec))
257
0
      Enqueue(Sec, 0);
258
11
    else if (isValidCIdentifier(Sec->Name)) {
259
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
260
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
261
0
    }
262
11
  }
263
1
264
1
  // Mark all reachable sections.
265
4
  while (!Q.empty())
266
3
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
267
1
}
268
269
// Before calling this function, Live bits are off for all
270
// input sections. This function make some or all of them on
271
// so that they are emitted to the output file.
272
1.79k
template <class ELFT> void elf::markLive() {
273
1.79k
  // If -gc-sections is missing, no sections are removed.
274
1.79k
  if (!Config->GcSections) {
275
1.73k
    for (InputSectionBase *Sec : InputSections)
276
202k
      Sec->Live = true;
277
1.73k
    return;
278
1.73k
  }
279
63
280
63
  // The -gc-sections option works only for SHF_ALLOC sections
281
63
  // (sections that are memory-mapped at runtime). So we can
282
63
  // unconditionally make non-SHF_ALLOC sections alive.
283
63
  //
284
63
  // Non SHF_ALLOC sections are not removed even if they are
285
63
  // unreachable through relocations because reachability is not
286
63
  // a good signal whether they are garbage or not (e.g. there is
287
63
  // usually no section referring to a .comment section, but we
288
63
  // want to keep it.)
289
63
  //
290
63
  // Note on SHF_REL{,A}: Such sections reach here only when -r
291
63
  // or -emit-reloc were given. And they are subject of garbage
292
63
  // collection because, if we remove a text section, we also
293
63
  // remove its relocation section.
294
336
  
for (InputSectionBase *Sec : InputSections)63
{
295
336
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
296
336
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
297
336
    if (!IsAlloc && 
!IsRel92
)
298
90
      Sec->Live = true;
299
336
  }
300
63
301
63
  // Follow the graph to mark all live sections.
302
63
  doGcSections<ELFT>();
303
63
304
63
  // Report garbage-collected sections.
305
63
  if (Config->PrintGcSections)
306
6
    for (InputSectionBase *Sec : InputSections)
307
28
      if (!Sec->Live)
308
16
        message("removing unused section from '" + Sec->Name + "' in file '" +
309
16
                Sec->File->getName() + "'");
310
63
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
272
230
template <class ELFT> void elf::markLive() {
273
230
  // If -gc-sections is missing, no sections are removed.
274
230
  if (!Config->GcSections) {
275
225
    for (InputSectionBase *Sec : InputSections)
276
1.14k
      Sec->Live = true;
277
225
    return;
278
225
  }
279
5
280
5
  // The -gc-sections option works only for SHF_ALLOC sections
281
5
  // (sections that are memory-mapped at runtime). So we can
282
5
  // unconditionally make non-SHF_ALLOC sections alive.
283
5
  //
284
5
  // Non SHF_ALLOC sections are not removed even if they are
285
5
  // unreachable through relocations because reachability is not
286
5
  // a good signal whether they are garbage or not (e.g. there is
287
5
  // usually no section referring to a .comment section, but we
288
5
  // want to keep it.)
289
5
  //
290
5
  // Note on SHF_REL{,A}: Such sections reach here only when -r
291
5
  // or -emit-reloc were given. And they are subject of garbage
292
5
  // collection because, if we remove a text section, we also
293
5
  // remove its relocation section.
294
34
  
for (InputSectionBase *Sec : InputSections)5
{
295
34
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
296
34
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
297
34
    if (!IsAlloc && 
!IsRel6
)
298
6
      Sec->Live = true;
299
34
  }
300
5
301
5
  // Follow the graph to mark all live sections.
302
5
  doGcSections<ELFT>();
303
5
304
5
  // Report garbage-collected sections.
305
5
  if (Config->PrintGcSections)
306
0
    for (InputSectionBase *Sec : InputSections)
307
0
      if (!Sec->Live)
308
0
        message("removing unused section from '" + Sec->Name + "' in file '" +
309
0
                Sec->File->getName() + "'");
310
5
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
272
103
template <class ELFT> void elf::markLive() {
273
103
  // If -gc-sections is missing, no sections are removed.
274
103
  if (!Config->GcSections) {
275
101
    for (InputSectionBase *Sec : InputSections)
276
818
      Sec->Live = true;
277
101
    return;
278
101
  }
279
2
280
2
  // The -gc-sections option works only for SHF_ALLOC sections
281
2
  // (sections that are memory-mapped at runtime). So we can
282
2
  // unconditionally make non-SHF_ALLOC sections alive.
283
2
  //
284
2
  // Non SHF_ALLOC sections are not removed even if they are
285
2
  // unreachable through relocations because reachability is not
286
2
  // a good signal whether they are garbage or not (e.g. there is
287
2
  // usually no section referring to a .comment section, but we
288
2
  // want to keep it.)
289
2
  //
290
2
  // Note on SHF_REL{,A}: Such sections reach here only when -r
291
2
  // or -emit-reloc were given. And they are subject of garbage
292
2
  // collection because, if we remove a text section, we also
293
2
  // remove its relocation section.
294
17
  
for (InputSectionBase *Sec : InputSections)2
{
295
17
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
296
17
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
297
17
    if (!IsAlloc && 
!IsRel2
)
298
2
      Sec->Live = true;
299
17
  }
300
2
301
2
  // Follow the graph to mark all live sections.
302
2
  doGcSections<ELFT>();
303
2
304
2
  // Report garbage-collected sections.
305
2
  if (Config->PrintGcSections)
306
0
    for (InputSectionBase *Sec : InputSections)
307
0
      if (!Sec->Live)
308
0
        message("removing unused section from '" + Sec->Name + "' in file '" +
309
0
                Sec->File->getName() + "'");
310
2
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
272
1.41k
template <class ELFT> void elf::markLive() {
273
1.41k
  // If -gc-sections is missing, no sections are removed.
274
1.41k
  if (!Config->GcSections) {
275
1.36k
    for (InputSectionBase *Sec : InputSections)
276
200k
      Sec->Live = true;
277
1.36k
    return;
278
1.36k
  }
279
55
280
55
  // The -gc-sections option works only for SHF_ALLOC sections
281
55
  // (sections that are memory-mapped at runtime). So we can
282
55
  // unconditionally make non-SHF_ALLOC sections alive.
283
55
  //
284
55
  // Non SHF_ALLOC sections are not removed even if they are
285
55
  // unreachable through relocations because reachability is not
286
55
  // a good signal whether they are garbage or not (e.g. there is
287
55
  // usually no section referring to a .comment section, but we
288
55
  // want to keep it.)
289
55
  //
290
55
  // Note on SHF_REL{,A}: Such sections reach here only when -r
291
55
  // or -emit-reloc were given. And they are subject of garbage
292
55
  // collection because, if we remove a text section, we also
293
55
  // remove its relocation section.
294
274
  
for (InputSectionBase *Sec : InputSections)55
{
295
274
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
296
274
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
297
274
    if (!IsAlloc && 
!IsRel83
)
298
81
      Sec->Live = true;
299
274
  }
300
55
301
55
  // Follow the graph to mark all live sections.
302
55
  doGcSections<ELFT>();
303
55
304
55
  // Report garbage-collected sections.
305
55
  if (Config->PrintGcSections)
306
6
    for (InputSectionBase *Sec : InputSections)
307
28
      if (!Sec->Live)
308
16
        message("removing unused section from '" + Sec->Name + "' in file '" +
309
16
                Sec->File->getName() + "'");
310
55
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
272
51
template <class ELFT> void elf::markLive() {
273
51
  // If -gc-sections is missing, no sections are removed.
274
51
  if (!Config->GcSections) {
275
50
    for (InputSectionBase *Sec : InputSections)
276
302
      Sec->Live = true;
277
50
    return;
278
50
  }
279
1
280
1
  // The -gc-sections option works only for SHF_ALLOC sections
281
1
  // (sections that are memory-mapped at runtime). So we can
282
1
  // unconditionally make non-SHF_ALLOC sections alive.
283
1
  //
284
1
  // Non SHF_ALLOC sections are not removed even if they are
285
1
  // unreachable through relocations because reachability is not
286
1
  // a good signal whether they are garbage or not (e.g. there is
287
1
  // usually no section referring to a .comment section, but we
288
1
  // want to keep it.)
289
1
  //
290
1
  // Note on SHF_REL{,A}: Such sections reach here only when -r
291
1
  // or -emit-reloc were given. And they are subject of garbage
292
1
  // collection because, if we remove a text section, we also
293
1
  // remove its relocation section.
294
11
  
for (InputSectionBase *Sec : InputSections)1
{
295
11
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
296
11
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
297
11
    if (!IsAlloc && 
!IsRel1
)
298
1
      Sec->Live = true;
299
11
  }
300
1
301
1
  // Follow the graph to mark all live sections.
302
1
  doGcSections<ELFT>();
303
1
304
1
  // Report garbage-collected sections.
305
1
  if (Config->PrintGcSections)
306
0
    for (InputSectionBase *Sec : InputSections)
307
0
      if (!Sec->Live)
308
0
        message("removing unused section from '" + Sec->Name + "' in file '" +
309
0
                Sec->File->getName() + "'");
310
1
}
311
312
template void elf::markLive<ELF32LE>();
313
template void elf::markLive<ELF32BE>();
314
template void elf::markLive<ELF64LE>();
315
template void elf::markLive<ELF64BE>();