Coverage Report

Created: 2018-08-19 14:04

/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
30
                                     const typename ELFT::Rela &Rel) {
55
30
  return Rel.r_addend;
56
30
}
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
30
                                     const typename ELFT::Rela &Rel) {
55
30
  return Rel.r_addend;
56
30
}
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
89
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
89
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
89
68
89
  // If a symbol is referenced in a live section, it is used.
69
89
  B.Used = true;
70
89
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
11
    if (!SS->isWeak())
72
8
      SS->getFile<ELFT>().IsNeeded = true;
73
89
74
89
  if (auto *D = dyn_cast<Defined>(&B)) {
75
66
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
76
66
    if (!RelSec)
77
2
      return;
78
64
    uint64_t Offset = D->Value;
79
64
    if (D->isSection())
80
41
      Offset += getAddend<ELFT>(Sec, Rel);
81
64
    Fn(RelSec, Offset);
82
64
    return;
83
64
  }
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
72
             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
66
72
  Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
67
72
68
72
  // If a symbol is referenced in a live section, it is used.
69
72
  B.Used = true;
70
72
  if (auto *SS = dyn_cast<SharedSymbol>(&B))
71
11
    if (!SS->isWeak())
72
8
      SS->getFile<ELFT>().IsNeeded = true;
73
72
74
72
  if (auto *D = dyn_cast<Defined>(&B)) {
75
51
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
76
51
    if (!RelSec)
77
2
      return;
78
49
    uint64_t Offset = D->Value;
79
49
    if (D->isSection())
80
30
      Offset += getAddend<ELFT>(Sec, Rel);
81
49
    Fn(RelSec, Offset);
82
49
    return;
83
49
  }
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
145
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
145
  if (Sec.AreRelocsRela) {
96
53
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
67
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
92
  } else {
99
92
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
12
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
92
  }
102
145
103
145
  for (InputSectionBase *IS : Sec.DependentSections)
104
9
    Fn(IS, 0);
105
145
}
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
122
                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
95
122
  if (Sec.AreRelocsRela) {
96
52
    for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
97
66
      resolveReloc<ELFT>(Sec, Rel, Fn);
98
70
  } else {
99
70
    for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
100
0
      resolveReloc<ELFT>(Sec, Rel, Fn);
101
70
  }
102
122
103
122
  for (InputSectionBase *IS : Sec.DependentSections)
104
5
    Fn(IS, 0);
105
122
}
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
393
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
393
  switch (Sec->Type) {
174
393
  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
387
  default:
180
387
    StringRef S = Sec->Name;
181
387
    return S.startswith(".ctors") || 
S.startswith(".dtors")385
||
182
387
           
S.startswith(".init")383
||
S.startswith(".fini")383
||
183
387
           
S.startswith(".jcr")383
;
184
393
  }
185
393
}
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
338
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
173
338
  switch (Sec->Type) {
174
338
  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
332
  default:
180
332
    StringRef S = Sec->Name;
181
332
    return S.startswith(".ctors") || 
S.startswith(".dtors")330
||
182
332
           
S.startswith(".init")328
||
S.startswith(".fini")328
||
183
332
           
S.startswith(".jcr")328
;
184
338
  }
185
338
}
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
75
template <class ELFT> static void doGcSections() {
191
75
  SmallVector<InputSection *, 256> Q;
192
75
  CNamedSections.clear();
193
75
194
187
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
187
    // Skip over discarded sections. This in theory shouldn't happen, because
196
187
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
187
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
187
    // .eh_frame) so we need to add a check.
199
187
    if (Sec == &InputSection::Discarded)
200
1
      return;
201
186
202
186
203
186
    // Usually, a whole section is marked as live or dead, but in mergeable
204
186
    // (splittable) sections, each piece of data has independent liveness bit.
205
186
    // So we explicitly tell it which offset is in use.
206
186
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
9
      MS->getSectionPiece(Offset)->Live = true;
208
186
209
186
    if (Sec->Live)
210
33
      return;
211
153
    Sec->Live = true;
212
153
213
153
    // Add input section to the queue.
214
153
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
145
      Q.push_back(S);
216
153
  };
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
146
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
146
    // Skip over discarded sections. This in theory shouldn't happen, because
196
146
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
146
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
146
    // .eh_frame) so we need to add a check.
199
146
    if (Sec == &InputSection::Discarded)
200
1
      return;
201
145
202
145
203
145
    // Usually, a whole section is marked as live or dead, but in mergeable
204
145
    // (splittable) sections, each piece of data has independent liveness bit.
205
145
    // So we explicitly tell it which offset is in use.
206
145
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
7
      MS->getSectionPiece(Offset)->Live = true;
208
145
209
145
    if (Sec->Live)
210
17
      return;
211
128
    Sec->Live = true;
212
128
213
128
    // Add input section to the queue.
214
128
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
122
      Q.push_back(S);
216
128
  };
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
75
218
306
  auto MarkSymbol = [&](Symbol *Sym) {
219
306
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
104
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
100
        Enqueue(IS, D->Value);
222
306
  };
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
261
  auto MarkSymbol = [&](Symbol *Sym) {
219
261
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
80
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
76
        Enqueue(IS, D->Value);
222
261
  };
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
75
224
75
  // Add GC root symbols.
225
75
  MarkSymbol(Symtab->find(Config->Entry));
226
75
  MarkSymbol(Symtab->find(Config->Init));
227
75
  MarkSymbol(Symtab->find(Config->Fini));
228
75
  for (StringRef S : Config->Undefined)
229
3
    MarkSymbol(Symtab->find(S));
230
75
  for (StringRef S : Script->ReferencedSymbols)
231
1
    MarkSymbol(Symtab->find(S));
232
75
233
75
  // Preserve externally-visible symbols if the symbols defined by this
234
75
  // file can interrupt other ELF file's symbols at runtime.
235
75
  for (Symbol *S : Symtab->getSymbols())
236
192
    if (S->includeInDynsym())
237
77
      MarkSymbol(S);
238
75
239
75
  // Preserve special sections and those which are specified in linker
240
75
  // script KEEP command.
241
407
  for (InputSectionBase *Sec : InputSections) {
242
407
    // Mark .eh_frame sections as live because there are usually no relocations
243
407
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
407
    // all of them. We also want to preserve personality routines and LSDA
245
407
    // referenced by .eh_frame sections, so we scan them for that here.
246
407
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
7
      EH->Live = true;
248
7
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
7
    }
250
407
251
407
    if (Sec->Flags & SHF_LINK_ORDER)
252
14
      continue;
253
393
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)383
)
254
14
      Enqueue(Sec, 0);
255
379
    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
393
  }
260
75
261
75
  // Mark all reachable sections.
262
220
  while (!Q.empty())
263
145
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
264
75
}
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
67
template <class ELFT> static void doGcSections() {
191
67
  SmallVector<InputSection *, 256> Q;
192
67
  CNamedSections.clear();
193
67
194
67
  auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
195
67
    // Skip over discarded sections. This in theory shouldn't happen, because
196
67
    // the ELF spec doesn't allow a relocation to point to a deduplicated
197
67
    // COMDAT section directly. Unfortunately this happens in practice (e.g.
198
67
    // .eh_frame) so we need to add a check.
199
67
    if (Sec == &InputSection::Discarded)
200
67
      return;
201
67
202
67
203
67
    // Usually, a whole section is marked as live or dead, but in mergeable
204
67
    // (splittable) sections, each piece of data has independent liveness bit.
205
67
    // So we explicitly tell it which offset is in use.
206
67
    if (auto *MS = dyn_cast<MergeInputSection>(Sec))
207
67
      MS->getSectionPiece(Offset)->Live = true;
208
67
209
67
    if (Sec->Live)
210
67
      return;
211
67
    Sec->Live = true;
212
67
213
67
    // Add input section to the queue.
214
67
    if (InputSection *S = dyn_cast<InputSection>(Sec))
215
67
      Q.push_back(S);
216
67
  };
217
67
218
67
  auto MarkSymbol = [&](Symbol *Sym) {
219
67
    if (auto *D = dyn_cast_or_null<Defined>(Sym))
220
67
      if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
221
67
        Enqueue(IS, D->Value);
222
67
  };
223
67
224
67
  // Add GC root symbols.
225
67
  MarkSymbol(Symtab->find(Config->Entry));
226
67
  MarkSymbol(Symtab->find(Config->Init));
227
67
  MarkSymbol(Symtab->find(Config->Fini));
228
67
  for (StringRef S : Config->Undefined)
229
3
    MarkSymbol(Symtab->find(S));
230
67
  for (StringRef S : Script->ReferencedSymbols)
231
1
    MarkSymbol(Symtab->find(S));
232
67
233
67
  // Preserve externally-visible symbols if the symbols defined by this
234
67
  // file can interrupt other ELF file's symbols at runtime.
235
67
  for (Symbol *S : Symtab->getSymbols())
236
157
    if (S->includeInDynsym())
237
56
      MarkSymbol(S);
238
67
239
67
  // Preserve special sections and those which are specified in linker
240
67
  // script KEEP command.
241
345
  for (InputSectionBase *Sec : InputSections) {
242
345
    // Mark .eh_frame sections as live because there are usually no relocations
243
345
    // that point to .eh_frames. Otherwise, the garbage collector would drop
244
345
    // all of them. We also want to preserve personality routines and LSDA
245
345
    // referenced by .eh_frame sections, so we scan them for that here.
246
345
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
247
5
      EH->Live = true;
248
5
      scanEhFrameSection<ELFT>(*EH, Enqueue);
249
5
    }
250
345
251
345
    if (Sec->Flags & SHF_LINK_ORDER)
252
7
      continue;
253
338
    if (isReserved<ELFT>(Sec) || 
Script->shouldKeep(Sec)328
)
254
14
      Enqueue(Sec, 0);
255
324
    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
338
  }
260
67
261
67
  // Mark all reachable sections.
262
189
  while (!Q.empty())
263
122
    forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
264
67
}
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.23k
template <class ELFT> void elf::markLive() {
270
2.23k
  // If -gc-sections is missing, no sections are removed.
271
2.23k
  if (!Config->GcSections) {
272
2.15k
    for (InputSectionBase *Sec : InputSections)
273
337k
      Sec->Live = true;
274
2.15k
    return;
275
2.15k
  }
276
75
277
75
  // The -gc-sections option works only for SHF_ALLOC sections
278
75
  // (sections that are memory-mapped at runtime). So we can
279
75
  // unconditionally make non-SHF_ALLOC sections alive except
280
75
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
75
  //
282
75
  // Usually, SHF_ALLOC sections are not removed even if they are
283
75
  // unreachable through relocations because reachability is not
284
75
  // a good signal whether they are garbage or not (e.g. there is
285
75
  // usually no section referring to a .comment section, but we
286
75
  // want to keep it.).
287
75
  //
288
75
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
75
  // have a reverse dependency on the InputSection they are linked with.
290
75
  // We are able to garbage collect them.
291
75
  //
292
75
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
75
  // or -emit-reloc were given. And they are subject of garbage
294
75
  // collection because, if we remove a text section, we also
295
75
  // remove its relocation section.
296
407
  
for (InputSectionBase *Sec : InputSections)75
{
297
407
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
407
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
407
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
407
    if (!IsAlloc && 
!IsLinkOrder108
&&
!IsRel106
)
301
104
      Sec->Live = true;
302
407
  }
303
75
304
75
  // Follow the graph to mark all live sections.
305
75
  doGcSections<ELFT>();
306
75
307
75
  // Report garbage-collected sections.
308
75
  if (Config->PrintGcSections)
309
6
    for (InputSectionBase *Sec : InputSections)
310
28
      if (!Sec->Live)
311
16
        message("removing unused section " + toString(Sec));
312
75
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
269
269
template <class ELFT> void elf::markLive() {
270
269
  // If -gc-sections is missing, no sections are removed.
271
269
  if (!Config->GcSections) {
272
264
    for (InputSectionBase *Sec : InputSections)
273
1.28k
      Sec->Live = true;
274
264
    return;
275
264
  }
276
5
277
5
  // The -gc-sections option works only for SHF_ALLOC sections
278
5
  // (sections that are memory-mapped at runtime). So we can
279
5
  // unconditionally make non-SHF_ALLOC sections alive except
280
5
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
5
  //
282
5
  // Usually, SHF_ALLOC sections are not removed even if they are
283
5
  // unreachable through relocations because reachability is not
284
5
  // a good signal whether they are garbage or not (e.g. there is
285
5
  // usually no section referring to a .comment section, but we
286
5
  // want to keep it.).
287
5
  //
288
5
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
5
  // have a reverse dependency on the InputSection they are linked with.
290
5
  // We are able to garbage collect them.
291
5
  //
292
5
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
5
  // or -emit-reloc were given. And they are subject of garbage
294
5
  // collection because, if we remove a text section, we also
295
5
  // remove its relocation section.
296
34
  
for (InputSectionBase *Sec : InputSections)5
{
297
34
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
34
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
34
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
34
    if (!IsAlloc && 
!IsLinkOrder6
&&
!IsRel6
)
301
6
      Sec->Live = true;
302
34
  }
303
5
304
5
  // Follow the graph to mark all live sections.
305
5
  doGcSections<ELFT>();
306
5
307
5
  // Report garbage-collected sections.
308
5
  if (Config->PrintGcSections)
309
0
    for (InputSectionBase *Sec : InputSections)
310
0
      if (!Sec->Live)
311
0
        message("removing unused section " + toString(Sec));
312
5
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
269
119
template <class ELFT> void elf::markLive() {
270
119
  // If -gc-sections is missing, no sections are removed.
271
119
  if (!Config->GcSections) {
272
117
    for (InputSectionBase *Sec : InputSections)
273
911
      Sec->Live = true;
274
117
    return;
275
117
  }
276
2
277
2
  // The -gc-sections option works only for SHF_ALLOC sections
278
2
  // (sections that are memory-mapped at runtime). So we can
279
2
  // unconditionally make non-SHF_ALLOC sections alive except
280
2
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
2
  //
282
2
  // Usually, SHF_ALLOC sections are not removed even if they are
283
2
  // unreachable through relocations because reachability is not
284
2
  // a good signal whether they are garbage or not (e.g. there is
285
2
  // usually no section referring to a .comment section, but we
286
2
  // want to keep it.).
287
2
  //
288
2
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
2
  // have a reverse dependency on the InputSection they are linked with.
290
2
  // We are able to garbage collect them.
291
2
  //
292
2
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
2
  // or -emit-reloc were given. And they are subject of garbage
294
2
  // collection because, if we remove a text section, we also
295
2
  // remove its relocation section.
296
17
  
for (InputSectionBase *Sec : InputSections)2
{
297
17
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
17
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
17
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
17
    if (!IsAlloc && 
!IsLinkOrder2
&&
!IsRel2
)
301
2
      Sec->Live = true;
302
17
  }
303
2
304
2
  // Follow the graph to mark all live sections.
305
2
  doGcSections<ELFT>();
306
2
307
2
  // Report garbage-collected sections.
308
2
  if (Config->PrintGcSections)
309
0
    for (InputSectionBase *Sec : InputSections)
310
0
      if (!Sec->Live)
311
0
        message("removing unused section " + toString(Sec));
312
2
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
269
1.76k
template <class ELFT> void elf::markLive() {
270
1.76k
  // If -gc-sections is missing, no sections are removed.
271
1.76k
  if (!Config->GcSections) {
272
1.69k
    for (InputSectionBase *Sec : InputSections)
273
335k
      Sec->Live = true;
274
1.69k
    return;
275
1.69k
  }
276
67
277
67
  // The -gc-sections option works only for SHF_ALLOC sections
278
67
  // (sections that are memory-mapped at runtime). So we can
279
67
  // unconditionally make non-SHF_ALLOC sections alive except
280
67
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
281
67
  //
282
67
  // Usually, SHF_ALLOC sections are not removed even if they are
283
67
  // unreachable through relocations because reachability is not
284
67
  // a good signal whether they are garbage or not (e.g. there is
285
67
  // usually no section referring to a .comment section, but we
286
67
  // want to keep it.).
287
67
  //
288
67
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
289
67
  // have a reverse dependency on the InputSection they are linked with.
290
67
  // We are able to garbage collect them.
291
67
  //
292
67
  // Note on SHF_REL{,A}: Such sections reach here only when -r
293
67
  // or -emit-reloc were given. And they are subject of garbage
294
67
  // collection because, if we remove a text section, we also
295
67
  // remove its relocation section.
296
345
  
for (InputSectionBase *Sec : InputSections)67
{
297
345
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
298
345
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
299
345
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
300
345
    if (!IsAlloc && 
!IsLinkOrder99
&&
!IsRel97
)
301
95
      Sec->Live = true;
302
345
  }
303
67
304
67
  // Follow the graph to mark all live sections.
305
67
  doGcSections<ELFT>();
306
67
307
67
  // Report garbage-collected sections.
308
67
  if (Config->PrintGcSections)
309
6
    for (InputSectionBase *Sec : InputSections)
310
28
      if (!Sec->Live)
311
16
        message("removing unused section " + toString(Sec));
312
67
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
269
85
template <class ELFT> void elf::markLive() {
270
85
  // If -gc-sections is missing, no sections are removed.
271
85
  if (!Config->GcSections) {
272
84
    for (InputSectionBase *Sec : InputSections)
273
421
      Sec->Live = true;
274
84
    return;
275
84
  }
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>();