Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/MarkLive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MarkLive.cpp -------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements --gc-sections, which is a feature to remove unused
11
// sections from output. Unused sections are sections that are not reachable
12
// from known GC-root symbols or sections. Naturally the feature is
13
// implemented as a mark-sweep garbage collector.
14
//
15
// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
16
// by default. Starting with GC-root symbols or sections, markLive function
17
// defined in this file visits all reachable sections to set their Live
18
// bits. Writer will then ignore sections whose Live bits are off, so that
19
// such sections are not included into output.
20
//
21
//===----------------------------------------------------------------------===//
22
23
#include "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
28
                                     const typename ELFT::Rela &Rel) {
55
28
  return Rel.r_addend;
56
28
}
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela const&)
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, false>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela const&)
MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)1, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela const&)
Line
Count
Source
54
28
                                     const typename ELFT::Rela &Rel) {
55
28
  return Rel.r_addend;
56
28
}
Unexecuted instantiation: MarkLive.cpp:llvm::object::ELFType<(llvm::support::endianness)0, true>::uint getAddend<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela const&)
57
58
// There are normally few input sections whose names are valid C
59
// identifiers, so we just store a std::vector instead of a multimap.
60
static DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
61
62
template <class ELFT, class RelT>
63
static void
64
resolveReloc(InputSectionBase &Sec, RelT &Rel,
65
84
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
84
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
84
68
84
  // If a symbol is referenced in a live section, it is used.
69
84
  B.Used = true;
70
84
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
11
    if (!SS->isWeak())
72
8
      SS->getFile<ELFT>().IsNeeded = true;
73
84
74
84
  if (auto *D = dyn_cast<Defined>(&B)) {
75
61
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
76
61
    if (!RelSec)
77
1
      return;
78
60
    uint64_t Offset = D->Value;
79
60
    if (D->isSection())
80
39
      Offset += getAddend<ELFT>(Sec, Rel);
81
60
    Fn(RelSec, Offset);
82
60
    return;
83
60
  }
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
67
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
67
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
67
68
67
  // If a symbol is referenced in a live section, it is used.
69
67
  B.Used = true;
70
67
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
11
    if (!SS->isWeak())
72
8
      SS->getFile<ELFT>().IsNeeded = true;
73
67
74
67
  if (auto *D = dyn_cast<Defined>(&B)) {
75
46
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
76
46
    if (!RelSec)
77
1
      return;
78
45
    uint64_t Offset = D->Value;
79
45
    if (D->isSection())
80
28
      Offset += getAddend<ELFT>(Sec, Rel);
81
45
    Fn(RelSec, Offset);
82
45
    return;
83
45
  }
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
140
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
140
  if (Sec.AreRelocsRela) {
96
49
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
62
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
91
  } else {
99
91
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
12
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
91
  }
102
140
103
140
  for (InputSectionBase *IS : Sec.DependentSections)
104
9
    Fn(IS, 0);
105
140
}
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
117
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
117
  if (Sec.AreRelocsRela) {
96
48
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
61
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
69
  } else {
99
69
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
69
  }
102
117
103
117
  for (InputSectionBase *IS : Sec.DependentSections)
104
5
    Fn(IS, 0);
105
117
}
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
380
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
380
  switch (Sec->Type) {
174
380
  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
374
  default:
180
374
    StringRef S = Sec->Name;
181
374
    return S.startswith(".ctors") || 
S.startswith(".dtors")372
||
182
374
           
S.startswith(".init")370
||
S.startswith(".fini")370
||
183
374
           
S.startswith(".jcr")370
;
184
380
  }
185
380
}
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
325
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
325
  switch (Sec->Type) {
174
325
  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
319
  default:
180
319
    StringRef S = Sec->Name;
181
319
    return S.startswith(".ctors") || 
S.startswith(".dtors")317
||
182
319
           
S.startswith(".init")315
||
S.startswith(".fini")315
||
183
319
           
S.startswith(".jcr")315
;
184
325
  }
185
325
}
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
72
template <class ELFT> static void doGcSections() {
191
72
  SmallVector<InputSection *, 256> Q;
192
72
  CNamedSections.clear();
193
72
194
179
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
179
    // Skip over discarded sections. This in theory shouldn't happen, because
196
179
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
179
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
179
    // .eh_frame) so we need to add a check.
199
179
    if (Sec == &InputSection::Discarded)
200
0
      return;
201
179
202
179
203
179
    // Usually, a whole section is marked as live or dead, but in mergeable
204
179
    // (splittable) sections, each piece of data has independent liveness bit.
205
179
    // So we explicitly tell it which offset is in use.
206
179
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
9
      MS->getSectionPiece(Offset)->Live = true;
208
179
209
179
    if (Sec->Live)
210
31
      return;
211
148
    Sec->Live = true;
212
148
213
148
    // Add input section to the queue.
214
148
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
140
      Q.push_back(S);
216
148
  };
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
138
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
138
    // Skip over discarded sections. This in theory shouldn't happen, because
196
138
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
138
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
138
    // .eh_frame) so we need to add a check.
199
138
    if (Sec == &InputSection::Discarded)
200
0
      return;
201
138
202
138
203
138
    // Usually, a whole section is marked as live or dead, but in mergeable
204
138
    // (splittable) sections, each piece of data has independent liveness bit.
205
138
    // So we explicitly tell it which offset is in use.
206
138
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
7
      MS->getSectionPiece(Offset)->Live = true;
208
138
209
138
    if (Sec->Live)
210
15
      return;
211
123
    Sec->Live = true;
212
123
213
123
    // Add input section to the queue.
214
123
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
117
      Q.push_back(S);
216
123
  };
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
72
218
296
  auto MarkSymbol = [&](Symbol *Sym) {
219
296
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
100
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
96
        Enqueue(IS, D->Value);
222
296
  };
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
251
  auto MarkSymbol = [&](Symbol *Sym) {
219
251
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
76
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
72
        Enqueue(IS, D->Value);
222
251
  };
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
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
186
    if (S->includeInDynsym())
237
76
      MarkSymbol(S);
238
72
239
72
  // Preserve special sections and those which are specified in linker
240
72
  // script KEEP command.
241
394
  for (InputSectionBase *Sec : InputSections) {
242
394
    // Mark .eh_frame sections as live because there are usually no relocations
243
394
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
394
    // all of them. We also want to preserve personality routines and LSDA
245
394
    // referenced by .eh_frame sections, so we scan them for that here.
246
394
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
7
      EH->Live = true;
248
7
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
7
    }
250
394
251
394
    if (Sec->Flags & SHF_LINK_ORDER)
252
14
      continue;
253
380
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)370
)
254
14
      Enqueue(Sec, 0);
255
366
    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
380
  }
260
72
261
72
  // Mark all reachable sections.
262
212
  while (!Q.empty())
263
140
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
264
72
}
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
64
template <class ELFT> static void doGcSections() {
191
64
  SmallVector<InputSection *, 256> Q;
192
64
  CNamedSections.clear();
193
64
194
64
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
64
    // Skip over discarded sections. This in theory shouldn't happen, because
196
64
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
64
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
64
    // .eh_frame) so we need to add a check.
199
64
    if (Sec == &InputSection::Discarded)
200
64
      return;
201
64
202
64
203
64
    // Usually, a whole section is marked as live or dead, but in mergeable
204
64
    // (splittable) sections, each piece of data has independent liveness bit.
205
64
    // So we explicitly tell it which offset is in use.
206
64
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
64
      MS->getSectionPiece(Offset)->Live = true;
208
64
209
64
    if (Sec->Live)
210
64
      return;
211
64
    Sec->Live = true;
212
64
213
64
    // Add input section to the queue.
214
64
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
64
      Q.push_back(S);
216
64
  };
217
64
218
64
  auto MarkSymbol = [&](Symbol *Sym) {
219
64
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
64
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
64
        Enqueue(IS, D->Value);
222
64
  };
223
64
224
64
  // Add GC root symbols.
225
64
  MarkSymbol(Symtab->find(Config->Entry));
226
64
  MarkSymbol(Symtab->find(Config->Init));
227
64
  MarkSymbol(Symtab->find(Config->Fini));
228
64
  for (StringRef S : Config->Undefined)
229
3
    MarkSymbol(Symtab->find(S));
230
64
  for (StringRef S : Script->ReferencedSymbols)
231
1
    MarkSymbol(Symtab->find(S));
232
64
233
64
  // Preserve externally-visible symbols if the symbols defined by this
234
64
  // file can interrupt other ELF file's symbols at runtime.
235
64
  for (Symbol *S : Symtab->getSymbols())
236
151
    if (S->includeInDynsym())
237
55
      MarkSymbol(S);
238
64
239
64
  // Preserve special sections and those which are specified in linker
240
64
  // script KEEP command.
241
332
  for (InputSectionBase *Sec : InputSections) {
242
332
    // Mark .eh_frame sections as live because there are usually no relocations
243
332
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
332
    // all of them. We also want to preserve personality routines and LSDA
245
332
    // referenced by .eh_frame sections, so we scan them for that here.
246
332
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
5
      EH->Live = true;
248
5
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
5
    }
250
332
251
332
    if (Sec->Flags & SHF_LINK_ORDER)
252
7
      continue;
253
325
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)315
)
254
14
      Enqueue(Sec, 0);
255
311
    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
325
  }
260
64
261
64
  // Mark all reachable sections.
262
181
  while (!Q.empty())
263
117
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
264
64
}
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.13k
template <class ELFT> void elf::markLive() {
270
2.13k
  // If -gc-sections is missing, no sections are removed.
271
2.13k
  if (!Config->GcSections) {
272
2.06k
    for (InputSectionBase *Sec : InputSections)
273
204k
      Sec->Live = true;
274
2.06k
    return;
275
2.06k
  }
276
72
277
72
  // The -gc-sections option works only for SHF_ALLOC sections
278
72
  // (sections that are memory-mapped at runtime). So we can
279
72
  // unconditionally make non-SHF_ALLOC sections alive except
280
72
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
72
  //
282
72
  // Usually, SHF_ALLOC sections are not removed even if they are
283
72
  // unreachable through relocations because reachability is not
284
72
  // a good signal whether they are garbage or not (e.g. there is
285
72
  // usually no section referring to a .comment section, but we
286
72
  // want to keep it.).
287
72
  //
288
72
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
72
  // have a reverse dependency on the InputSection they are linked with.
290
72
  // We are able to garbage collect them.
291
72
  //
292
72
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
72
  // or -emit-reloc were given. And they are subject of garbage
294
72
  // collection because, if we remove a text section, we also
295
72
  // remove its relocation section.
296
394
  
for (InputSectionBase *Sec : InputSections)72
{
297
394
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
394
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
394
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
394
    if (!IsAlloc && 
!IsLinkOrder105
&&
!IsRel103
)
301
101
      Sec->Live = true;
302
394
  }
303
72
304
72
  // Follow the graph to mark all live sections.
305
72
  doGcSections<ELFT>();
306
72
307
72
  // Report garbage-collected sections.
308
72
  if (Config->PrintGcSections)
309
6
    for (InputSectionBase *Sec : InputSections)
310
28
      if (!Sec->Live)
311
16
        message("removing unused section " + toString(Sec));
312
72
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
269
256
template <class ELFT> void elf::markLive() {
270
256
  // If -gc-sections is missing, no sections are removed.
271
256
  if (!Config->GcSections) {
272
251
    for (InputSectionBase *Sec : InputSections)
273
1.23k
      Sec->Live = true;
274
251
    return;
275
251
  }
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
933
      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.68k
template <class ELFT> void elf::markLive() {
270
1.68k
  // If -gc-sections is missing, no sections are removed.
271
1.68k
  if (!Config->GcSections) {
272
1.61k
    for (InputSectionBase *Sec : InputSections)
273
201k
      Sec->Live = true;
274
1.61k
    return;
275
1.61k
  }
276
64
277
64
  // The -gc-sections option works only for SHF_ALLOC sections
278
64
  // (sections that are memory-mapped at runtime). So we can
279
64
  // unconditionally make non-SHF_ALLOC sections alive except
280
64
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
64
  //
282
64
  // Usually, SHF_ALLOC sections are not removed even if they are
283
64
  // unreachable through relocations because reachability is not
284
64
  // a good signal whether they are garbage or not (e.g. there is
285
64
  // usually no section referring to a .comment section, but we
286
64
  // want to keep it.).
287
64
  //
288
64
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
64
  // have a reverse dependency on the InputSection they are linked with.
290
64
  // We are able to garbage collect them.
291
64
  //
292
64
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
64
  // or -emit-reloc were given. And they are subject of garbage
294
64
  // collection because, if we remove a text section, we also
295
64
  // remove its relocation section.
296
332
  
for (InputSectionBase *Sec : InputSections)64
{
297
332
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
332
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
332
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
332
    if (!IsAlloc && 
!IsLinkOrder96
&&
!IsRel94
)
301
92
      Sec->Live = true;
302
332
  }
303
64
304
64
  // Follow the graph to mark all live sections.
305
64
  doGcSections<ELFT>();
306
64
307
64
  // Report garbage-collected sections.
308
64
  if (Config->PrintGcSections)
309
6
    for (InputSectionBase *Sec : InputSections)
310
28
      if (!Sec->Live)
311
16
        message("removing unused section " + toString(Sec));
312
64
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
269
76
template <class ELFT> void elf::markLive() {
270
76
  // If -gc-sections is missing, no sections are removed.
271
76
  if (!Config->GcSections) {
272
75
    for (InputSectionBase *Sec : InputSections)
273
394
      Sec->Live = true;
274
75
    return;
275
75
  }
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>();