Coverage Report

Created: 2019-01-18 03:29

/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
97
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
97
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
97
68
97
  // If a symbol is referenced in a live section, it is used.
69
97
  B.Used = true;
70
97
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
15
    if (!SS->isWeak())
72
11
      SS->getFile<ELFT>().IsNeeded = true;
73
97
74
97
  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
27
85
27
  if (!B.isDefined())
86
27
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
87
7
      Fn(Sec, 0);
88
27
}
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
80
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
80
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
80
68
80
  // If a symbol is referenced in a live section, it is used.
69
80
  B.Used = true;
70
80
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
15
    if (!SS->isWeak())
72
11
      SS->getFile<ELFT>().IsNeeded = true;
73
80
74
80
  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
25
85
25
  if (!B.isDefined())
86
25
    for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
87
7
      Fn(Sec, 0);
88
25
}
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
156
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
156
  if (Sec.AreRelocsRela) {
96
59
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
75
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
97
  } else {
99
97
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
12
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
97
  }
102
156
103
156
  for (InputSectionBase *IS : Sec.DependentSections)
104
12
    Fn(IS, 0);
105
156
}
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
133
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
133
  if (Sec.AreRelocsRela) {
96
58
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
74
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
75
  } else {
99
75
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
75
  }
102
133
103
133
  for (InputSectionBase *IS : Sec.DependentSections)
104
8
    Fn(IS, 0);
105
133
}
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
412
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
412
  switch (Sec->Type) {
174
412
  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
406
  default:
180
406
    StringRef S = Sec->Name;
181
406
    return S.startswith(".ctors") || 
S.startswith(".dtors")404
||
182
406
           
S.startswith(".init")402
||
S.startswith(".fini")402
||
183
406
           
S.startswith(".jcr")402
;
184
412
  }
185
412
}
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
357
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
357
  switch (Sec->Type) {
174
357
  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
351
  default:
180
351
    StringRef S = Sec->Name;
181
351
    return S.startswith(".ctors") || 
S.startswith(".dtors")349
||
182
351
           
S.startswith(".init")347
||
S.startswith(".fini")347
||
183
351
           
S.startswith(".jcr")347
;
184
357
  }
185
357
}
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
80
template <class ELFT> static void doGcSections() {
191
80
  SmallVector<InputSection *, 256> Q;
192
80
  CNamedSections.clear();
193
80
194
199
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
199
    // Skip over discarded sections. This in theory shouldn't happen, because
196
199
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
199
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
199
    // .eh_frame) so we need to add a check.
199
199
    if (Sec == &InputSection::Discarded)
200
1
      return;
201
198
202
198
203
198
    // Usually, a whole section is marked as live or dead, but in mergeable
204
198
    // (splittable) sections, each piece of data has independent liveness bit.
205
198
    // So we explicitly tell it which offset is in use.
206
198
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
9
      MS->getSectionPiece(Offset)->Live = true;
208
198
209
198
    if (Sec->Live)
210
34
      return;
211
164
    Sec->Live = true;
212
164
213
164
    // Add input section to the queue.
214
164
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
156
      Q.push_back(S);
216
164
  };
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
158
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
158
    // Skip over discarded sections. This in theory shouldn't happen, because
196
158
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
158
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
158
    // .eh_frame) so we need to add a check.
199
158
    if (Sec == &InputSection::Discarded)
200
1
      return;
201
157
202
157
203
157
    // Usually, a whole section is marked as live or dead, but in mergeable
204
157
    // (splittable) sections, each piece of data has independent liveness bit.
205
157
    // So we explicitly tell it which offset is in use.
206
157
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
7
      MS->getSectionPiece(Offset)->Live = true;
208
157
209
157
    if (Sec->Live)
210
18
      return;
211
139
    Sec->Live = true;
212
139
213
139
    // Add input section to the queue.
214
139
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
133
      Q.push_back(S);
216
139
  };
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
80
218
327
  auto MarkSymbol = [&](Symbol *Sym) {
219
327
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
109
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
105
        Enqueue(IS, D->Value);
222
327
  };
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
282
  auto MarkSymbol = [&](Symbol *Sym) {
219
282
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
85
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
81
        Enqueue(IS, D->Value);
222
282
  };
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
80
224
80
  // Add GC root symbols.
225
80
  MarkSymbol(Symtab->find(Config->Entry));
226
80
  MarkSymbol(Symtab->find(Config->Init));
227
80
  MarkSymbol(Symtab->find(Config->Fini));
228
80
  for (StringRef S : Config->Undefined)
229
3
    MarkSymbol(Symtab->find(S));
230
80
  for (StringRef S : Script->ReferencedSymbols)
231
1
    MarkSymbol(Symtab->find(S));
232
80
233
80
  // Preserve externally-visible symbols if the symbols defined by this
234
80
  // file can interrupt other ELF file's symbols at runtime.
235
80
  for (Symbol *S : Symtab->getSymbols())
236
204
    if (S->includeInDynsym())
237
83
      MarkSymbol(S);
238
80
239
80
  // Preserve special sections and those which are specified in linker
240
80
  // script KEEP command.
241
426
  for (InputSectionBase *Sec : InputSections) {
242
426
    // Mark .eh_frame sections as live because there are usually no relocations
243
426
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
426
    // all of them. We also want to preserve personality routines and LSDA
245
426
    // referenced by .eh_frame sections, so we scan them for that here.
246
426
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
7
      EH->Live = true;
248
7
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
7
    }
250
426
251
426
    if (Sec->Flags & SHF_LINK_ORDER)
252
14
      continue;
253
412
254
412
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)402
) {
255
14
      Enqueue(Sec, 0);
256
398
    } else if (isValidCIdentifier(Sec->Name)) {
257
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
258
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
259
18
    }
260
412
  }
261
80
262
80
  // Mark all reachable sections.
263
236
  while (!Q.empty())
264
156
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
265
80
}
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
254
27
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec)) {
255
0
      Enqueue(Sec, 0);
256
27
    } else if (isValidCIdentifier(Sec->Name)) {
257
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
258
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
259
0
    }
260
27
  }
261
5
262
5
  // Mark all reachable sections.
263
20
  while (!Q.empty())
264
15
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
265
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
254
17
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec)) {
255
0
      Enqueue(Sec, 0);
256
17
    } else if (isValidCIdentifier(Sec->Name)) {
257
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
258
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
259
0
    }
260
17
  }
261
2
262
2
  // Mark all reachable sections.
263
7
  while (!Q.empty())
264
5
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
265
2
}
MarkLive.cpp:void doGcSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
190
72
template <class ELFT> static void doGcSections() {
191
72
  SmallVector<InputSection *, 256> Q;
192
72
  CNamedSections.clear();
193
72
194
72
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
72
    // Skip over discarded sections. This in theory shouldn't happen, because
196
72
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
72
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
72
    // .eh_frame) so we need to add a check.
199
72
    if (Sec == &InputSection::Discarded)
200
72
      return;
201
72
202
72
203
72
    // Usually, a whole section is marked as live or dead, but in mergeable
204
72
    // (splittable) sections, each piece of data has independent liveness bit.
205
72
    // So we explicitly tell it which offset is in use.
206
72
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
72
      MS->getSectionPiece(Offset)->Live = true;
208
72
209
72
    if (Sec->Live)
210
72
      return;
211
72
    Sec->Live = true;
212
72
213
72
    // Add input section to the queue.
214
72
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
72
      Q.push_back(S);
216
72
  };
217
72
218
72
  auto MarkSymbol = [&](Symbol *Sym) {
219
72
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
72
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
72
        Enqueue(IS, D->Value);
222
72
  };
223
72
224
72
  // Add GC root symbols.
225
72
  MarkSymbol(Symtab->find(Config->Entry));
226
72
  MarkSymbol(Symtab->find(Config->Init));
227
72
  MarkSymbol(Symtab->find(Config->Fini));
228
72
  for (StringRef S : Config->Undefined)
229
3
    MarkSymbol(Symtab->find(S));
230
72
  for (StringRef S : Script->ReferencedSymbols)
231
1
    MarkSymbol(Symtab->find(S));
232
72
233
72
  // Preserve externally-visible symbols if the symbols defined by this
234
72
  // file can interrupt other ELF file's symbols at runtime.
235
72
  for (Symbol *S : Symtab->getSymbols())
236
169
    if (S->includeInDynsym())
237
62
      MarkSymbol(S);
238
72
239
72
  // Preserve special sections and those which are specified in linker
240
72
  // script KEEP command.
241
364
  for (InputSectionBase *Sec : InputSections) {
242
364
    // Mark .eh_frame sections as live because there are usually no relocations
243
364
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
364
    // all of them. We also want to preserve personality routines and LSDA
245
364
    // referenced by .eh_frame sections, so we scan them for that here.
246
364
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
5
      EH->Live = true;
248
5
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
5
    }
250
364
251
364
    if (Sec->Flags & SHF_LINK_ORDER)
252
7
      continue;
253
357
254
357
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)347
) {
255
14
      Enqueue(Sec, 0);
256
343
    } else if (isValidCIdentifier(Sec->Name)) {
257
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
258
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
259
18
    }
260
357
  }
261
72
262
72
  // Mark all reachable sections.
263
205
  while (!Q.empty())
264
133
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
265
72
}
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
254
11
    if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec)) {
255
0
      Enqueue(Sec, 0);
256
11
    } else if (isValidCIdentifier(Sec->Name)) {
257
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
258
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
259
0
    }
260
11
  }
261
1
262
1
  // Mark all reachable sections.
263
4
  while (!Q.empty())
264
3
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
265
1
}
266
267
// Before calling this function, Live bits are off for all
268
// input sections. This function make some or all of them on
269
// so that they are emitted to the output file.
270
2.40k
template <class ELFT> void elf::markLive() {
271
2.40k
  if (!Config->GcSections) {
272
2.32k
    // If -gc-sections is missing, no sections are removed.
273
2.32k
    for (InputSectionBase *Sec : InputSections)
274
338k
      Sec->Live = true;
275
2.32k
276
2.32k
    // If a DSO defines a symbol referenced in a regular object, it is needed.
277
2.32k
    for (Symbol *Sym : Symtab->getSymbols())
278
16.1k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
279
719
        if (S->IsUsedInRegularObj && 
!S->isWeak()370
)
280
354
          S->getFile<ELFT>().IsNeeded = true;
281
2.32k
    return;
282
2.32k
  }
283
80
284
80
  // The -gc-sections option works only for SHF_ALLOC sections
285
80
  // (sections that are memory-mapped at runtime). So we can
286
80
  // unconditionally make non-SHF_ALLOC sections alive except
287
80
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
288
80
  //
289
80
  // Usually, SHF_ALLOC sections are not removed even if they are
290
80
  // unreachable through relocations because reachability is not
291
80
  // a good signal whether they are garbage or not (e.g. there is
292
80
  // usually no section referring to a .comment section, but we
293
80
  // want to keep it.).
294
80
  //
295
80
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
296
80
  // have a reverse dependency on the InputSection they are linked with.
297
80
  // We are able to garbage collect them.
298
80
  //
299
80
  // Note on SHF_REL{,A}: Such sections reach here only when -r
300
80
  // or -emit-reloc were given. And they are subject of garbage
301
80
  // collection because, if we remove a text section, we also
302
80
  // remove its relocation section.
303
426
  
for (InputSectionBase *Sec : InputSections)80
{
304
426
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
305
426
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
306
426
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
307
426
    if (!IsAlloc && 
!IsLinkOrder117
&&
!IsRel115
)
308
109
      Sec->Live = true;
309
426
  }
310
80
311
80
  // Follow the graph to mark all live sections.
312
80
  doGcSections<ELFT>();
313
80
314
80
  // Report garbage-collected sections.
315
80
  if (Config->PrintGcSections)
316
6
    for (InputSectionBase *Sec : InputSections)
317
28
      if (!Sec->Live)
318
16
        message("removing unused section " + toString(Sec));
319
80
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
270
302
template <class ELFT> void elf::markLive() {
271
302
  if (!Config->GcSections) {
272
297
    // If -gc-sections is missing, no sections are removed.
273
297
    for (InputSectionBase *Sec : InputSections)
274
1.44k
      Sec->Live = true;
275
297
276
297
    // If a DSO defines a symbol referenced in a regular object, it is needed.
277
297
    for (Symbol *Sym : Symtab->getSymbols())
278
1.04k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
279
112
        if (S->IsUsedInRegularObj && 
!S->isWeak()75
)
280
69
          S->getFile<ELFT>().IsNeeded = true;
281
297
    return;
282
297
  }
283
5
284
5
  // The -gc-sections option works only for SHF_ALLOC sections
285
5
  // (sections that are memory-mapped at runtime). So we can
286
5
  // unconditionally make non-SHF_ALLOC sections alive except
287
5
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
288
5
  //
289
5
  // Usually, SHF_ALLOC sections are not removed even if they are
290
5
  // unreachable through relocations because reachability is not
291
5
  // a good signal whether they are garbage or not (e.g. there is
292
5
  // usually no section referring to a .comment section, but we
293
5
  // want to keep it.).
294
5
  //
295
5
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
296
5
  // have a reverse dependency on the InputSection they are linked with.
297
5
  // We are able to garbage collect them.
298
5
  //
299
5
  // Note on SHF_REL{,A}: Such sections reach here only when -r
300
5
  // or -emit-reloc were given. And they are subject of garbage
301
5
  // collection because, if we remove a text section, we also
302
5
  // remove its relocation section.
303
34
  
for (InputSectionBase *Sec : InputSections)5
{
304
34
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
305
34
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
306
34
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
307
34
    if (!IsAlloc && 
!IsLinkOrder6
&&
!IsRel6
)
308
6
      Sec->Live = true;
309
34
  }
310
5
311
5
  // Follow the graph to mark all live sections.
312
5
  doGcSections<ELFT>();
313
5
314
5
  // Report garbage-collected sections.
315
5
  if (Config->PrintGcSections)
316
0
    for (InputSectionBase *Sec : InputSections)
317
0
      if (!Sec->Live)
318
0
        message("removing unused section " + toString(Sec));
319
5
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
270
124
template <class ELFT> void elf::markLive() {
271
124
  if (!Config->GcSections) {
272
122
    // If -gc-sections is missing, no sections are removed.
273
122
    for (InputSectionBase *Sec : InputSections)
274
949
      Sec->Live = true;
275
122
276
122
    // If a DSO defines a symbol referenced in a regular object, it is needed.
277
122
    for (Symbol *Sym : Symtab->getSymbols())
278
503
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
279
95
        if (S->IsUsedInRegularObj && 
!S->isWeak()27
)
280
27
          S->getFile<ELFT>().IsNeeded = true;
281
122
    return;
282
122
  }
283
2
284
2
  // The -gc-sections option works only for SHF_ALLOC sections
285
2
  // (sections that are memory-mapped at runtime). So we can
286
2
  // unconditionally make non-SHF_ALLOC sections alive except
287
2
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
288
2
  //
289
2
  // Usually, SHF_ALLOC sections are not removed even if they are
290
2
  // unreachable through relocations because reachability is not
291
2
  // a good signal whether they are garbage or not (e.g. there is
292
2
  // usually no section referring to a .comment section, but we
293
2
  // want to keep it.).
294
2
  //
295
2
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
296
2
  // have a reverse dependency on the InputSection they are linked with.
297
2
  // We are able to garbage collect them.
298
2
  //
299
2
  // Note on SHF_REL{,A}: Such sections reach here only when -r
300
2
  // or -emit-reloc were given. And they are subject of garbage
301
2
  // collection because, if we remove a text section, we also
302
2
  // remove its relocation section.
303
17
  
for (InputSectionBase *Sec : InputSections)2
{
304
17
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
305
17
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
306
17
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
307
17
    if (!IsAlloc && 
!IsLinkOrder2
&&
!IsRel2
)
308
2
      Sec->Live = true;
309
17
  }
310
2
311
2
  // Follow the graph to mark all live sections.
312
2
  doGcSections<ELFT>();
313
2
314
2
  // Report garbage-collected sections.
315
2
  if (Config->PrintGcSections)
316
0
    for (InputSectionBase *Sec : InputSections)
317
0
      if (!Sec->Live)
318
0
        message("removing unused section " + toString(Sec));
319
2
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
270
1.87k
template <class ELFT> void elf::markLive() {
271
1.87k
  if (!Config->GcSections) {
272
1.79k
    // If -gc-sections is missing, no sections are removed.
273
1.79k
    for (InputSectionBase *Sec : InputSections)
274
335k
      Sec->Live = true;
275
1.79k
276
1.79k
    // If a DSO defines a symbol referenced in a regular object, it is needed.
277
1.79k
    for (Symbol *Sym : Symtab->getSymbols())
278
4.19k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
279
476
        if (S->IsUsedInRegularObj && 
!S->isWeak()247
)
280
237
          S->getFile<ELFT>().IsNeeded = true;
281
1.79k
    return;
282
1.79k
  }
283
72
284
72
  // The -gc-sections option works only for SHF_ALLOC sections
285
72
  // (sections that are memory-mapped at runtime). So we can
286
72
  // unconditionally make non-SHF_ALLOC sections alive except
287
72
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
288
72
  //
289
72
  // Usually, SHF_ALLOC sections are not removed even if they are
290
72
  // unreachable through relocations because reachability is not
291
72
  // a good signal whether they are garbage or not (e.g. there is
292
72
  // usually no section referring to a .comment section, but we
293
72
  // want to keep it.).
294
72
  //
295
72
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
296
72
  // have a reverse dependency on the InputSection they are linked with.
297
72
  // We are able to garbage collect them.
298
72
  //
299
72
  // Note on SHF_REL{,A}: Such sections reach here only when -r
300
72
  // or -emit-reloc were given. And they are subject of garbage
301
72
  // collection because, if we remove a text section, we also
302
72
  // remove its relocation section.
303
364
  
for (InputSectionBase *Sec : InputSections)72
{
304
364
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
305
364
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
306
364
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
307
364
    if (!IsAlloc && 
!IsLinkOrder108
&&
!IsRel106
)
308
100
      Sec->Live = true;
309
364
  }
310
72
311
72
  // Follow the graph to mark all live sections.
312
72
  doGcSections<ELFT>();
313
72
314
72
  // Report garbage-collected sections.
315
72
  if (Config->PrintGcSections)
316
6
    for (InputSectionBase *Sec : InputSections)
317
28
      if (!Sec->Live)
318
16
        message("removing unused section " + toString(Sec));
319
72
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
270
111
template <class ELFT> void elf::markLive() {
271
111
  if (!Config->GcSections) {
272
110
    // If -gc-sections is missing, no sections are removed.
273
110
    for (InputSectionBase *Sec : InputSections)
274
503
      Sec->Live = true;
275
110
276
110
    // If a DSO defines a symbol referenced in a regular object, it is needed.
277
110
    for (Symbol *Sym : Symtab->getSymbols())
278
10.3k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
279
36
        if (S->IsUsedInRegularObj && 
!S->isWeak()21
)
280
21
          S->getFile<ELFT>().IsNeeded = true;
281
110
    return;
282
110
  }
283
1
284
1
  // The -gc-sections option works only for SHF_ALLOC sections
285
1
  // (sections that are memory-mapped at runtime). So we can
286
1
  // unconditionally make non-SHF_ALLOC sections alive except
287
1
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
288
1
  //
289
1
  // Usually, SHF_ALLOC sections are not removed even if they are
290
1
  // unreachable through relocations because reachability is not
291
1
  // a good signal whether they are garbage or not (e.g. there is
292
1
  // usually no section referring to a .comment section, but we
293
1
  // want to keep it.).
294
1
  //
295
1
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
296
1
  // have a reverse dependency on the InputSection they are linked with.
297
1
  // We are able to garbage collect them.
298
1
  //
299
1
  // Note on SHF_REL{,A}: Such sections reach here only when -r
300
1
  // or -emit-reloc were given. And they are subject of garbage
301
1
  // collection because, if we remove a text section, we also
302
1
  // remove its relocation section.
303
11
  
for (InputSectionBase *Sec : InputSections)1
{
304
11
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
305
11
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
306
11
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
307
11
    if (!IsAlloc && 
!IsLinkOrder1
&&
!IsRel1
)
308
1
      Sec->Live = true;
309
11
  }
310
1
311
1
  // Follow the graph to mark all live sections.
312
1
  doGcSections<ELFT>();
313
1
314
1
  // Report garbage-collected sections.
315
1
  if (Config->PrintGcSections)
316
0
    for (InputSectionBase *Sec : InputSections)
317
0
      if (!Sec->Live)
318
0
        message("removing unused section " + toString(Sec));
319
1
}
320
321
template void elf::markLive<ELF32LE>();
322
template void elf::markLive<ELF32BE>();
323
template void elf::markLive<ELF64LE>();
324
template void elf::markLive<ELF64BE>();