Coverage Report

Created: 2018-10-20 06:24

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