Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/MarkLive.cpp
Line
Count
Source
1
//===- MarkLive.cpp -------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements --gc-sections, which is a feature to remove unused
10
// sections from output. Unused sections are sections that are not reachable
11
// from known GC-root symbols or sections. Naturally the feature is
12
// implemented as a mark-sweep garbage collector.
13
//
14
// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
15
// by default. Starting with GC-root symbols or sections, markLive function
16
// defined in this file visits all reachable sections to set their Live
17
// bits. Writer will then ignore sections whose Live bits are off, so that
18
// such sections are not included into output.
19
//
20
//===----------------------------------------------------------------------===//
21
22
#include "MarkLive.h"
23
#include "InputSection.h"
24
#include "LinkerScript.h"
25
#include "OutputSections.h"
26
#include "SymbolTable.h"
27
#include "Symbols.h"
28
#include "Target.h"
29
#include "lld/Common/Memory.h"
30
#include "lld/Common/Strings.h"
31
#include "llvm/ADT/STLExtras.h"
32
#include "llvm/Object/ELF.h"
33
#include <functional>
34
#include <vector>
35
36
using namespace llvm;
37
using namespace llvm::ELF;
38
using namespace llvm::object;
39
using namespace llvm::support::endian;
40
41
using namespace lld;
42
using namespace lld::elf;
43
44
namespace {
45
template <class ELFT> class MarkLive {
46
public:
47
  void run();
48
49
private:
50
  void enqueue(InputSectionBase *Sec, uint64_t Offset);
51
  void markSymbol(Symbol *Sym);
52
53
  template <class RelTy>
54
  void resolveReloc(InputSectionBase &Sec, RelTy &Rel, bool IsLSDA);
55
56
  template <class RelTy>
57
  void scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels);
58
59
  // A list of sections to visit.
60
  SmallVector<InputSection *, 256> Queue;
61
62
  // There are normally few input sections whose names are valid C
63
  // identifiers, so we just store a std::vector instead of a multimap.
64
  DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
65
};
66
} // namespace
67
68
template <class ELFT>
69
static uint64_t getAddend(InputSectionBase &Sec,
70
13
                          const typename ELFT::Rel &Rel) {
71
13
  return Target->getImplicitAddend(Sec.data().begin() + Rel.r_offset,
72
13
                                   Rel.getType(Config->IsMips64EL));
73
13
}
MarkLive.cpp:unsigned long long 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
70
13
                          const typename ELFT::Rel &Rel) {
71
13
  return Target->getImplicitAddend(Sec.data().begin() + Rel.r_offset,
72
13
                                   Rel.getType(Config->IsMips64EL));
73
13
}
Unexecuted instantiation: MarkLive.cpp:unsigned long long 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:unsigned long long 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:unsigned long long getAddend<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rel const&)
74
75
template <class ELFT>
76
static uint64_t getAddend(InputSectionBase &Sec,
77
36
                          const typename ELFT::Rela &Rel) {
78
36
  return Rel.r_addend;
79
36
}
Unexecuted instantiation: MarkLive.cpp:unsigned long long 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:unsigned long long getAddend<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela const&)
MarkLive.cpp:unsigned long long 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
77
36
                          const typename ELFT::Rela &Rel) {
78
36
  return Rel.r_addend;
79
36
}
Unexecuted instantiation: MarkLive.cpp:unsigned long long getAddend<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela const&)
80
81
template <class ELFT>
82
template <class RelTy>
83
void MarkLive<ELFT>::resolveReloc(InputSectionBase &Sec, RelTy &Rel,
84
101
                                  bool IsLSDA) {
85
101
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
86
101
87
101
  // If a symbol is referenced in a live section, it is used.
88
101
  Sym.Used = true;
89
101
90
101
  if (auto *D = dyn_cast<Defined>(&Sym)) {
91
74
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
92
74
    if (!RelSec)
93
2
      return;
94
72
95
72
    uint64_t Offset = D->Value;
96
72
    if (D->isSection())
97
49
      Offset += getAddend<ELFT>(Sec, Rel);
98
72
99
72
    if (!IsLSDA || 
!(RelSec->Flags & SHF_EXECINSTR)8
)
100
65
      enqueue(RelSec, Offset);
101
72
    return;
102
72
  }
103
27
104
27
  if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
105
15
    if (!SS->isWeak())
106
11
      SS->getFile().IsNeeded = true;
107
27
108
27
  for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
109
7
    enqueue(Sec, 0);
110
27
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::resolveReloc<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&, bool)
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::resolveReloc<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&, bool)
Line
Count
Source
84
17
                                  bool IsLSDA) {
85
17
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
86
17
87
17
  // If a symbol is referenced in a live section, it is used.
88
17
  Sym.Used = true;
89
17
90
17
  if (auto *D = dyn_cast<Defined>(&Sym)) {
91
17
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
92
17
    if (!RelSec)
93
0
      return;
94
17
95
17
    uint64_t Offset = D->Value;
96
17
    if (D->isSection())
97
13
      Offset += getAddend<ELFT>(Sec, Rel);
98
17
99
17
    if (!IsLSDA || 
!(RelSec->Flags & SHF_EXECINSTR)2
)
100
15
      enqueue(RelSec, Offset);
101
17
    return;
102
17
  }
103
0
104
0
  if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
105
0
    if (!SS->isWeak())
106
0
      SS->getFile().IsNeeded = true;
107
0
108
0
  for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
109
0
    enqueue(Sec, 0);
110
0
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::resolveReloc<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&, bool)
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::resolveReloc<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&, bool)
Line
Count
Source
84
1
                                  bool IsLSDA) {
85
1
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
86
1
87
1
  // If a symbol is referenced in a live section, it is used.
88
1
  Sym.Used = true;
89
1
90
1
  if (auto *D = dyn_cast<Defined>(&Sym)) {
91
0
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
92
0
    if (!RelSec)
93
0
      return;
94
0
95
0
    uint64_t Offset = D->Value;
96
0
    if (D->isSection())
97
0
      Offset += getAddend<ELFT>(Sec, Rel);
98
0
99
0
    if (!IsLSDA || !(RelSec->Flags & SHF_EXECINSTR))
100
0
      enqueue(RelSec, Offset);
101
0
    return;
102
0
  }
103
1
104
1
  if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
105
0
    if (!SS->isWeak())
106
0
      SS->getFile().IsNeeded = true;
107
1
108
1
  for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
109
0
    enqueue(Sec, 0);
110
1
}
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::resolveReloc<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&, bool)
Line
Count
Source
84
82
                                  bool IsLSDA) {
85
82
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
86
82
87
82
  // If a symbol is referenced in a live section, it is used.
88
82
  Sym.Used = true;
89
82
90
82
  if (auto *D = dyn_cast<Defined>(&Sym)) {
91
57
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
92
57
    if (!RelSec)
93
2
      return;
94
55
95
55
    uint64_t Offset = D->Value;
96
55
    if (D->isSection())
97
36
      Offset += getAddend<ELFT>(Sec, Rel);
98
55
99
55
    if (!IsLSDA || 
!(RelSec->Flags & SHF_EXECINSTR)6
)
100
50
      enqueue(RelSec, Offset);
101
55
    return;
102
55
  }
103
25
104
25
  if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
105
15
    if (!SS->isWeak())
106
11
      SS->getFile().IsNeeded = true;
107
25
108
25
  for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
109
7
    enqueue(Sec, 0);
110
25
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::resolveReloc<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&, bool)
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::resolveReloc<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&, bool)
Line
Count
Source
84
1
                                  bool IsLSDA) {
85
1
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
86
1
87
1
  // If a symbol is referenced in a live section, it is used.
88
1
  Sym.Used = true;
89
1
90
1
  if (auto *D = dyn_cast<Defined>(&Sym)) {
91
0
    auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
92
0
    if (!RelSec)
93
0
      return;
94
0
95
0
    uint64_t Offset = D->Value;
96
0
    if (D->isSection())
97
0
      Offset += getAddend<ELFT>(Sec, Rel);
98
0
99
0
    if (!IsLSDA || !(RelSec->Flags & SHF_EXECINSTR))
100
0
      enqueue(RelSec, Offset);
101
0
    return;
102
0
  }
103
1
104
1
  if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
105
0
    if (!SS->isWeak())
106
0
      SS->getFile().IsNeeded = true;
107
1
108
1
  for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
109
0
    enqueue(Sec, 0);
110
1
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::resolveReloc<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&, bool)
111
112
// The .eh_frame section is an unfortunate special case.
113
// The section is divided in CIEs and FDEs and the relocations it can have are
114
// * CIEs can refer to a personality function.
115
// * FDEs can refer to a LSDA
116
// * FDEs refer to the function they contain information about
117
// The last kind of relocation cannot keep the referred section alive, or they
118
// would keep everything alive in a common object file. In fact, each FDE is
119
// alive if the section it refers to is alive.
120
// To keep things simple, in here we just ignore the last relocation kind. The
121
// other two keep the referred section alive.
122
//
123
// A possible improvement would be to fully process .eh_frame in the middle of
124
// the gc pass. With that we would be able to also gc some sections holding
125
// LSDAs and personality functions if we found that they were unused.
126
template <class ELFT>
127
template <class RelTy>
128
void MarkLive<ELFT>::scanEhFrameSection(EhInputSection &EH,
129
5
                                        ArrayRef<RelTy> Rels) {
130
17
  for (size_t I = 0, End = EH.Pieces.size(); I < End; 
++I12
) {
131
12
    EhSectionPiece &Piece = EH.Pieces[I];
132
12
    size_t FirstRelI = Piece.FirstRelocation;
133
12
    if (FirstRelI == (unsigned)-1)
134
3
      continue;
135
9
136
9
    if (read32<ELFT::TargetEndianness>(Piece.data().data() + 4) == 0) {
137
2
      // This is a CIE, we only need to worry about the first relocation. It is
138
2
      // known to point to the personality function.
139
2
      resolveReloc(EH, Rels[FirstRelI], false);
140
2
      continue;
141
2
    }
142
7
143
7
    // This is a FDE. The relocations point to the described function or to
144
7
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
145
7
    // points to executable sections.
146
7
    uint64_t PieceEnd = Piece.InputOff + Piece.Size;
147
17
    for (size_t J = FirstRelI, End2 = Rels.size(); J < End2; 
++J10
)
148
10
      if (Rels[J].r_offset < PieceEnd)
149
8
        resolveReloc(EH, Rels[J], true);
150
7
  }
151
5
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::scanEhFrameSection<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> >)
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::scanEhFrameSection<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> >)
Line
Count
Source
129
2
                                        ArrayRef<RelTy> Rels) {
130
6
  for (size_t I = 0, End = EH.Pieces.size(); I < End; 
++I4
) {
131
4
    EhSectionPiece &Piece = EH.Pieces[I];
132
4
    size_t FirstRelI = Piece.FirstRelocation;
133
4
    if (FirstRelI == (unsigned)-1)
134
0
      continue;
135
4
136
4
    if (read32<ELFT::TargetEndianness>(Piece.data().data() + 4) == 0) {
137
2
      // This is a CIE, we only need to worry about the first relocation. It is
138
2
      // known to point to the personality function.
139
2
      resolveReloc(EH, Rels[FirstRelI], false);
140
2
      continue;
141
2
    }
142
2
143
2
    // This is a FDE. The relocations point to the described function or to
144
2
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
145
2
    // points to executable sections.
146
2
    uint64_t PieceEnd = Piece.InputOff + Piece.Size;
147
4
    for (size_t J = FirstRelI, End2 = Rels.size(); J < End2; 
++J2
)
148
2
      if (Rels[J].r_offset < PieceEnd)
149
2
        resolveReloc(EH, Rels[J], true);
150
2
  }
151
2
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::scanEhFrameSection<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> >)
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::scanEhFrameSection<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> >)
MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::scanEhFrameSection<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> >)
Line
Count
Source
129
3
                                        ArrayRef<RelTy> Rels) {
130
11
  for (size_t I = 0, End = EH.Pieces.size(); I < End; 
++I8
) {
131
8
    EhSectionPiece &Piece = EH.Pieces[I];
132
8
    size_t FirstRelI = Piece.FirstRelocation;
133
8
    if (FirstRelI == (unsigned)-1)
134
3
      continue;
135
5
136
5
    if (read32<ELFT::TargetEndianness>(Piece.data().data() + 4) == 0) {
137
0
      // This is a CIE, we only need to worry about the first relocation. It is
138
0
      // known to point to the personality function.
139
0
      resolveReloc(EH, Rels[FirstRelI], false);
140
0
      continue;
141
0
    }
142
5
143
5
    // This is a FDE. The relocations point to the described function or to
144
5
    // a LSDA. We only need to keep the LSDA alive, so ignore anything that
145
5
    // points to executable sections.
146
5
    uint64_t PieceEnd = Piece.InputOff + Piece.Size;
147
13
    for (size_t J = FirstRelI, End2 = Rels.size(); J < End2; 
++J8
)
148
8
      if (Rels[J].r_offset < PieceEnd)
149
6
        resolveReloc(EH, Rels[J], true);
150
5
  }
151
3
}
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::scanEhFrameSection<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> >)
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::scanEhFrameSection<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> >)
Unexecuted instantiation: MarkLive.cpp:void (anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::scanEhFrameSection<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> >)
152
153
// Some sections are used directly by the loader, so they should never be
154
// garbage-collected. This function returns true if a given section is such
155
// section.
156
423
static bool isReserved(InputSectionBase *Sec) {
157
423
  switch (Sec->Type) {
158
423
  case SHT_FINI_ARRAY:
159
6
  case SHT_INIT_ARRAY:
160
6
  case SHT_NOTE:
161
6
  case SHT_PREINIT_ARRAY:
162
6
    return true;
163
417
  default:
164
417
    StringRef S = Sec->Name;
165
417
    return S.startswith(".ctors") || 
S.startswith(".dtors")415
||
166
417
           
S.startswith(".init")413
||
S.startswith(".fini")413
||
167
417
           
S.startswith(".jcr")413
;
168
423
  }
169
423
}
170
171
template <class ELFT>
172
207
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
173
207
  // Skip over discarded sections. This in theory shouldn't happen, because
174
207
  // the ELF spec doesn't allow a relocation to point to a deduplicated
175
207
  // COMDAT section directly. Unfortunately this happens in practice (e.g.
176
207
  // .eh_frame) so we need to add a check.
177
207
  if (Sec == &InputSection::Discarded)
178
1
    return;
179
206
180
206
  // Usually, a whole section is marked as live or dead, but in mergeable
181
206
  // (splittable) sections, each piece of data has independent liveness bit.
182
206
  // So we explicitly tell it which offset is in use.
183
206
  if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
9
    MS->getSectionPiece(Offset)->Live = true;
185
206
186
206
  if (Sec->Live)
187
34
    return;
188
172
  Sec->Live = true;
189
172
190
172
  // Add input section to the queue.
191
172
  if (InputSection *S = dyn_cast<InputSection>(Sec))
192
164
    Queue.push_back(S);
193
172
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::enqueue(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
172
26
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
173
26
  // Skip over discarded sections. This in theory shouldn't happen, because
174
26
  // the ELF spec doesn't allow a relocation to point to a deduplicated
175
26
  // COMDAT section directly. Unfortunately this happens in practice (e.g.
176
26
  // .eh_frame) so we need to add a check.
177
26
  if (Sec == &InputSection::Discarded)
178
0
    return;
179
26
180
26
  // Usually, a whole section is marked as live or dead, but in mergeable
181
26
  // (splittable) sections, each piece of data has independent liveness bit.
182
26
  // So we explicitly tell it which offset is in use.
183
26
  if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
2
    MS->getSectionPiece(Offset)->Live = true;
185
26
186
26
  if (Sec->Live)
187
5
    return;
188
21
  Sec->Live = true;
189
21
190
21
  // Add input section to the queue.
191
21
  if (InputSection *S = dyn_cast<InputSection>(Sec))
192
19
    Queue.push_back(S);
193
21
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::enqueue(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
172
12
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
173
12
  // Skip over discarded sections. This in theory shouldn't happen, because
174
12
  // the ELF spec doesn't allow a relocation to point to a deduplicated
175
12
  // COMDAT section directly. Unfortunately this happens in practice (e.g.
176
12
  // .eh_frame) so we need to add a check.
177
12
  if (Sec == &InputSection::Discarded)
178
0
    return;
179
12
180
12
  // Usually, a whole section is marked as live or dead, but in mergeable
181
12
  // (splittable) sections, each piece of data has independent liveness bit.
182
12
  // So we explicitly tell it which offset is in use.
183
12
  if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
0
    MS->getSectionPiece(Offset)->Live = true;
185
12
186
12
  if (Sec->Live)
187
7
    return;
188
5
  Sec->Live = true;
189
5
190
5
  // Add input section to the queue.
191
5
  if (InputSection *S = dyn_cast<InputSection>(Sec))
192
5
    Queue.push_back(S);
193
5
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::enqueue(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
172
162
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
173
162
  // Skip over discarded sections. This in theory shouldn't happen, because
174
162
  // the ELF spec doesn't allow a relocation to point to a deduplicated
175
162
  // COMDAT section directly. Unfortunately this happens in practice (e.g.
176
162
  // .eh_frame) so we need to add a check.
177
162
  if (Sec == &InputSection::Discarded)
178
1
    return;
179
161
180
161
  // Usually, a whole section is marked as live or dead, but in mergeable
181
161
  // (splittable) sections, each piece of data has independent liveness bit.
182
161
  // So we explicitly tell it which offset is in use.
183
161
  if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
7
    MS->getSectionPiece(Offset)->Live = true;
185
161
186
161
  if (Sec->Live)
187
18
    return;
188
143
  Sec->Live = true;
189
143
190
143
  // Add input section to the queue.
191
143
  if (InputSection *S = dyn_cast<InputSection>(Sec))
192
137
    Queue.push_back(S);
193
143
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::enqueue(lld::elf::InputSectionBase*, unsigned long long)
Line
Count
Source
172
7
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
173
7
  // Skip over discarded sections. This in theory shouldn't happen, because
174
7
  // the ELF spec doesn't allow a relocation to point to a deduplicated
175
7
  // COMDAT section directly. Unfortunately this happens in practice (e.g.
176
7
  // .eh_frame) so we need to add a check.
177
7
  if (Sec == &InputSection::Discarded)
178
0
    return;
179
7
180
7
  // Usually, a whole section is marked as live or dead, but in mergeable
181
7
  // (splittable) sections, each piece of data has independent liveness bit.
182
7
  // So we explicitly tell it which offset is in use.
183
7
  if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
0
    MS->getSectionPiece(Offset)->Live = true;
185
7
186
7
  if (Sec->Live)
187
4
    return;
188
3
  Sec->Live = true;
189
3
190
3
  // Add input section to the queue.
191
3
  if (InputSection *S = dyn_cast<InputSection>(Sec))
192
3
    Queue.push_back(S);
193
3
}
194
195
339
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
339
  if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
113
    if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
198
109
      enqueue(IS, D->Value);
199
339
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::markSymbol(lld::elf::Symbol*)
Line
Count
Source
195
23
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
23
  if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
7
    if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
198
7
      enqueue(IS, D->Value);
199
23
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::markSymbol(lld::elf::Symbol*)
Line
Count
Source
195
18
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
18
  if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
12
    if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
198
12
      enqueue(IS, D->Value);
199
18
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::markSymbol(lld::elf::Symbol*)
Line
Count
Source
195
288
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
288
  if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
87
    if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
198
83
      enqueue(IS, D->Value);
199
288
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::markSymbol(lld::elf::Symbol*)
Line
Count
Source
195
10
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
10
  if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
7
    if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
198
7
      enqueue(IS, D->Value);
199
10
}
200
201
// This is the main function of the garbage collector.
202
// Starting from GC-root sections, this function visits all reachable
203
// sections to set their "Live" bits.
204
84
template <class ELFT> void MarkLive<ELFT>::run() {
205
84
  // Add GC root symbols.
206
84
  markSymbol(Symtab->find(Config->Entry));
207
84
  markSymbol(Symtab->find(Config->Init));
208
84
  markSymbol(Symtab->find(Config->Fini));
209
84
  for (StringRef S : Config->Undefined)
210
3
    markSymbol(Symtab->find(S));
211
84
  for (StringRef S : Script->ReferencedSymbols)
212
1
    markSymbol(Symtab->find(S));
213
84
214
84
  // Preserve externally-visible symbols if the symbols defined by this
215
84
  // file can interrupt other ELF file's symbols at runtime.
216
84
  for (Symbol *S : Symtab->getSymbols())
217
208
    if (S->includeInDynsym())
218
83
      markSymbol(S);
219
84
220
84
  // Preserve special sections and those which are specified in linker
221
84
  // script KEEP command.
222
439
  for (InputSectionBase *Sec : InputSections) {
223
439
    // Mark .eh_frame sections as live because there are usually no relocations
224
439
    // that point to .eh_frames. Otherwise, the garbage collector would drop
225
439
    // all of them. We also want to preserve personality routines and LSDA
226
439
    // referenced by .eh_frame sections, so we scan them for that here.
227
439
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
228
7
      EH->Live = true;
229
7
      if (!EH->NumRelocations)
230
2
        continue;
231
5
232
5
      if (EH->AreRelocsRela)
233
3
        scanEhFrameSection(*EH, EH->template relas<ELFT>());
234
2
      else
235
2
        scanEhFrameSection(*EH, EH->template rels<ELFT>());
236
5
    }
237
439
238
439
    
if (437
Sec->Flags & SHF_LINK_ORDER437
)
239
14
      continue;
240
423
241
423
    if (isReserved(Sec) || 
Script->shouldKeep(Sec)413
) {
242
14
      enqueue(Sec, 0);
243
409
    } else if (isValidCIdentifier(Sec->Name)) {
244
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
245
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
246
18
    }
247
423
  }
248
84
249
84
  // Mark all reachable sections.
250
248
  while (!Queue.empty()) {
251
164
    InputSectionBase &Sec = *Queue.pop_back_val();
252
164
253
164
    if (Sec.AreRelocsRela) {
254
61
      for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
255
77
        resolveReloc(Sec, Rel, false);
256
103
    } else {
257
103
      for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
258
14
        resolveReloc(Sec, Rel, false);
259
103
    }
260
164
261
164
    for (InputSectionBase *IS : Sec.DependentSections)
262
12
      enqueue(IS, 0);
263
164
  }
264
84
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()
Line
Count
Source
204
7
template <class ELFT> void MarkLive<ELFT>::run() {
205
7
  // Add GC root symbols.
206
7
  markSymbol(Symtab->find(Config->Entry));
207
7
  markSymbol(Symtab->find(Config->Init));
208
7
  markSymbol(Symtab->find(Config->Fini));
209
7
  for (StringRef S : Config->Undefined)
210
0
    markSymbol(Symtab->find(S));
211
7
  for (StringRef S : Script->ReferencedSymbols)
212
0
    markSymbol(Symtab->find(S));
213
7
214
7
  // Preserve externally-visible symbols if the symbols defined by this
215
7
  // file can interrupt other ELF file's symbols at runtime.
216
7
  for (Symbol *S : Symtab->getSymbols())
217
15
    if (S->includeInDynsym())
218
2
      markSymbol(S);
219
7
220
7
  // Preserve special sections and those which are specified in linker
221
7
  // script KEEP command.
222
41
  for (InputSectionBase *Sec : InputSections) {
223
41
    // Mark .eh_frame sections as live because there are usually no relocations
224
41
    // that point to .eh_frames. Otherwise, the garbage collector would drop
225
41
    // all of them. We also want to preserve personality routines and LSDA
226
41
    // referenced by .eh_frame sections, so we scan them for that here.
227
41
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
228
2
      EH->Live = true;
229
2
      if (!EH->NumRelocations)
230
0
        continue;
231
2
232
2
      if (EH->AreRelocsRela)
233
0
        scanEhFrameSection(*EH, EH->template relas<ELFT>());
234
2
      else
235
2
        scanEhFrameSection(*EH, EH->template rels<ELFT>());
236
2
    }
237
41
238
41
    if (Sec->Flags & SHF_LINK_ORDER)
239
7
      continue;
240
34
241
34
    if (isReserved(Sec) || Script->shouldKeep(Sec)) {
242
0
      enqueue(Sec, 0);
243
34
    } else if (isValidCIdentifier(Sec->Name)) {
244
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
245
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
246
0
    }
247
34
  }
248
7
249
7
  // Mark all reachable sections.
250
26
  while (!Queue.empty()) {
251
19
    InputSectionBase &Sec = *Queue.pop_back_val();
252
19
253
19
    if (Sec.AreRelocsRela) {
254
0
      for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
255
0
        resolveReloc(Sec, Rel, false);
256
19
    } else {
257
19
      for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
258
13
        resolveReloc(Sec, Rel, false);
259
19
    }
260
19
261
19
    for (InputSectionBase *IS : Sec.DependentSections)
262
4
      enqueue(IS, 0);
263
19
  }
264
7
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()
Line
Count
Source
204
2
template <class ELFT> void MarkLive<ELFT>::run() {
205
2
  // Add GC root symbols.
206
2
  markSymbol(Symtab->find(Config->Entry));
207
2
  markSymbol(Symtab->find(Config->Init));
208
2
  markSymbol(Symtab->find(Config->Fini));
209
2
  for (StringRef S : Config->Undefined)
210
0
    markSymbol(Symtab->find(S));
211
2
  for (StringRef S : Script->ReferencedSymbols)
212
0
    markSymbol(Symtab->find(S));
213
2
214
2
  // Preserve externally-visible symbols if the symbols defined by this
215
2
  // file can interrupt other ELF file's symbols at runtime.
216
2
  for (Symbol *S : Symtab->getSymbols())
217
14
    if (S->includeInDynsym())
218
12
      markSymbol(S);
219
2
220
2
  // Preserve special sections and those which are specified in linker
221
2
  // script KEEP command.
222
17
  for (InputSectionBase *Sec : InputSections) {
223
17
    // Mark .eh_frame sections as live because there are usually no relocations
224
17
    // that point to .eh_frames. Otherwise, the garbage collector would drop
225
17
    // all of them. We also want to preserve personality routines and LSDA
226
17
    // referenced by .eh_frame sections, so we scan them for that here.
227
17
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
228
0
      EH->Live = true;
229
0
      if (!EH->NumRelocations)
230
0
        continue;
231
0
232
0
      if (EH->AreRelocsRela)
233
0
        scanEhFrameSection(*EH, EH->template relas<ELFT>());
234
0
      else
235
0
        scanEhFrameSection(*EH, EH->template rels<ELFT>());
236
0
    }
237
17
238
17
    if (Sec->Flags & SHF_LINK_ORDER)
239
0
      continue;
240
17
241
17
    if (isReserved(Sec) || Script->shouldKeep(Sec)) {
242
0
      enqueue(Sec, 0);
243
17
    } else if (isValidCIdentifier(Sec->Name)) {
244
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
245
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
246
0
    }
247
17
  }
248
2
249
2
  // Mark all reachable sections.
250
7
  while (!Queue.empty()) {
251
5
    InputSectionBase &Sec = *Queue.pop_back_val();
252
5
253
5
    if (Sec.AreRelocsRela) {
254
0
      for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
255
0
        resolveReloc(Sec, Rel, false);
256
5
    } else {
257
5
      for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
258
1
        resolveReloc(Sec, Rel, false);
259
5
    }
260
5
261
5
    for (InputSectionBase *IS : Sec.DependentSections)
262
0
      enqueue(IS, 0);
263
5
  }
264
2
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()
Line
Count
Source
204
74
template <class ELFT> void MarkLive<ELFT>::run() {
205
74
  // Add GC root symbols.
206
74
  markSymbol(Symtab->find(Config->Entry));
207
74
  markSymbol(Symtab->find(Config->Init));
208
74
  markSymbol(Symtab->find(Config->Fini));
209
74
  for (StringRef S : Config->Undefined)
210
3
    markSymbol(Symtab->find(S));
211
74
  for (StringRef S : Script->ReferencedSymbols)
212
1
    markSymbol(Symtab->find(S));
213
74
214
74
  // Preserve externally-visible symbols if the symbols defined by this
215
74
  // file can interrupt other ELF file's symbols at runtime.
216
74
  for (Symbol *S : Symtab->getSymbols())
217
171
    if (S->includeInDynsym())
218
62
      markSymbol(S);
219
74
220
74
  // Preserve special sections and those which are specified in linker
221
74
  // script KEEP command.
222
370
  for (InputSectionBase *Sec : InputSections) {
223
370
    // Mark .eh_frame sections as live because there are usually no relocations
224
370
    // that point to .eh_frames. Otherwise, the garbage collector would drop
225
370
    // all of them. We also want to preserve personality routines and LSDA
226
370
    // referenced by .eh_frame sections, so we scan them for that here.
227
370
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
228
5
      EH->Live = true;
229
5
      if (!EH->NumRelocations)
230
2
        continue;
231
3
232
3
      if (EH->AreRelocsRela)
233
3
        scanEhFrameSection(*EH, EH->template relas<ELFT>());
234
0
      else
235
0
        scanEhFrameSection(*EH, EH->template rels<ELFT>());
236
3
    }
237
370
238
370
    
if (368
Sec->Flags & SHF_LINK_ORDER368
)
239
7
      continue;
240
361
241
361
    if (isReserved(Sec) || 
Script->shouldKeep(Sec)351
) {
242
14
      enqueue(Sec, 0);
243
347
    } else if (isValidCIdentifier(Sec->Name)) {
244
18
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
245
18
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
246
18
    }
247
361
  }
248
74
249
74
  // Mark all reachable sections.
250
211
  while (!Queue.empty()) {
251
137
    InputSectionBase &Sec = *Queue.pop_back_val();
252
137
253
137
    if (Sec.AreRelocsRela) {
254
60
      for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
255
76
        resolveReloc(Sec, Rel, false);
256
77
    } else {
257
77
      for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
258
0
        resolveReloc(Sec, Rel, false);
259
77
    }
260
137
261
137
    for (InputSectionBase *IS : Sec.DependentSections)
262
8
      enqueue(IS, 0);
263
137
  }
264
74
}
MarkLive.cpp:(anonymous namespace)::MarkLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()
Line
Count
Source
204
1
template <class ELFT> void MarkLive<ELFT>::run() {
205
1
  // Add GC root symbols.
206
1
  markSymbol(Symtab->find(Config->Entry));
207
1
  markSymbol(Symtab->find(Config->Init));
208
1
  markSymbol(Symtab->find(Config->Fini));
209
1
  for (StringRef S : Config->Undefined)
210
0
    markSymbol(Symtab->find(S));
211
1
  for (StringRef S : Script->ReferencedSymbols)
212
0
    markSymbol(Symtab->find(S));
213
1
214
1
  // Preserve externally-visible symbols if the symbols defined by this
215
1
  // file can interrupt other ELF file's symbols at runtime.
216
1
  for (Symbol *S : Symtab->getSymbols())
217
8
    if (S->includeInDynsym())
218
7
      markSymbol(S);
219
1
220
1
  // Preserve special sections and those which are specified in linker
221
1
  // script KEEP command.
222
11
  for (InputSectionBase *Sec : InputSections) {
223
11
    // Mark .eh_frame sections as live because there are usually no relocations
224
11
    // that point to .eh_frames. Otherwise, the garbage collector would drop
225
11
    // all of them. We also want to preserve personality routines and LSDA
226
11
    // referenced by .eh_frame sections, so we scan them for that here.
227
11
    if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
228
0
      EH->Live = true;
229
0
      if (!EH->NumRelocations)
230
0
        continue;
231
0
232
0
      if (EH->AreRelocsRela)
233
0
        scanEhFrameSection(*EH, EH->template relas<ELFT>());
234
0
      else
235
0
        scanEhFrameSection(*EH, EH->template rels<ELFT>());
236
0
    }
237
11
238
11
    if (Sec->Flags & SHF_LINK_ORDER)
239
0
      continue;
240
11
241
11
    if (isReserved(Sec) || Script->shouldKeep(Sec)) {
242
0
      enqueue(Sec, 0);
243
11
    } else if (isValidCIdentifier(Sec->Name)) {
244
0
      CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
245
0
      CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
246
0
    }
247
11
  }
248
1
249
1
  // Mark all reachable sections.
250
4
  while (!Queue.empty()) {
251
3
    InputSectionBase &Sec = *Queue.pop_back_val();
252
3
253
3
    if (Sec.AreRelocsRela) {
254
1
      for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
255
1
        resolveReloc(Sec, Rel, false);
256
2
    } else {
257
2
      for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
258
0
        resolveReloc(Sec, Rel, false);
259
2
    }
260
3
261
3
    for (InputSectionBase *IS : Sec.DependentSections)
262
0
      enqueue(IS, 0);
263
3
  }
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.52k
template <class ELFT> void elf::markLive() {
270
2.52k
  // If -gc-sections is not given, no sections are removed.
271
2.52k
  if (!Config->GcSections) {
272
2.44k
    for (InputSectionBase *Sec : InputSections)
273
338k
      Sec->Live = true;
274
2.44k
275
2.44k
    // If a DSO defines a symbol referenced in a regular object, it is needed.
276
2.44k
    for (Symbol *Sym : Symtab->getSymbols())
277
16.3k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
278
730
        if (S->IsUsedInRegularObj && 
!S->isWeak()379
)
279
363
          S->getFile().IsNeeded = true;
280
2.44k
    return;
281
2.44k
  }
282
84
283
84
  // Otheriwse, do mark-sweep GC.
284
84
  //
285
84
  // The -gc-sections option works only for SHF_ALLOC sections
286
84
  // (sections that are memory-mapped at runtime). So we can
287
84
  // unconditionally make non-SHF_ALLOC sections alive except
288
84
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
289
84
  //
290
84
  // Usually, SHF_ALLOC sections are not removed even if they are
291
84
  // unreachable through relocations because reachability is not
292
84
  // a good signal whether they are garbage or not (e.g. there is
293
84
  // usually no section referring to a .comment section, but we
294
84
  // want to keep it.).
295
84
  //
296
84
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
297
84
  // have a reverse dependency on the InputSection they are linked with.
298
84
  // We are able to garbage collect them.
299
84
  //
300
84
  // Note on SHF_REL{,A}: Such sections reach here only when -r
301
84
  // or -emit-reloc were given. And they are subject of garbage
302
84
  // collection because, if we remove a text section, we also
303
84
  // remove its relocation section.
304
439
  
for (InputSectionBase *Sec : InputSections)84
{
305
439
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
306
439
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
439
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
439
309
439
    if (!IsAlloc && 
!IsLinkOrder122
&&
!IsRel120
)
310
114
      Sec->Live = true;
311
439
  }
312
84
313
84
  // Follow the graph to mark all live sections.
314
84
  MarkLive<ELFT>().run();
315
84
316
84
  // Report garbage-collected sections.
317
84
  if (Config->PrintGcSections)
318
6
    for (InputSectionBase *Sec : InputSections)
319
28
      if (!Sec->Live)
320
16
        message("removing unused section " + toString(Sec));
321
84
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
269
319
template <class ELFT> void elf::markLive() {
270
319
  // If -gc-sections is not given, no sections are removed.
271
319
  if (!Config->GcSections) {
272
312
    for (InputSectionBase *Sec : InputSections)
273
1.54k
      Sec->Live = true;
274
312
275
312
    // If a DSO defines a symbol referenced in a regular object, it is needed.
276
312
    for (Symbol *Sym : Symtab->getSymbols())
277
1.08k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
278
114
        if (S->IsUsedInRegularObj && 
!S->isWeak()77
)
279
71
          S->getFile().IsNeeded = true;
280
312
    return;
281
312
  }
282
7
283
7
  // Otheriwse, do mark-sweep GC.
284
7
  //
285
7
  // The -gc-sections option works only for SHF_ALLOC sections
286
7
  // (sections that are memory-mapped at runtime). So we can
287
7
  // unconditionally make non-SHF_ALLOC sections alive except
288
7
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
289
7
  //
290
7
  // Usually, SHF_ALLOC sections are not removed even if they are
291
7
  // unreachable through relocations because reachability is not
292
7
  // a good signal whether they are garbage or not (e.g. there is
293
7
  // usually no section referring to a .comment section, but we
294
7
  // want to keep it.).
295
7
  //
296
7
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
297
7
  // have a reverse dependency on the InputSection they are linked with.
298
7
  // We are able to garbage collect them.
299
7
  //
300
7
  // Note on SHF_REL{,A}: Such sections reach here only when -r
301
7
  // or -emit-reloc were given. And they are subject of garbage
302
7
  // collection because, if we remove a text section, we also
303
7
  // remove its relocation section.
304
41
  
for (InputSectionBase *Sec : InputSections)7
{
305
41
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
306
41
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
41
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
41
309
41
    if (!IsAlloc && 
!IsLinkOrder9
&&
!IsRel9
)
310
9
      Sec->Live = true;
311
41
  }
312
7
313
7
  // Follow the graph to mark all live sections.
314
7
  MarkLive<ELFT>().run();
315
7
316
7
  // Report garbage-collected sections.
317
7
  if (Config->PrintGcSections)
318
0
    for (InputSectionBase *Sec : InputSections)
319
0
      if (!Sec->Live)
320
0
        message("removing unused section " + toString(Sec));
321
7
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
269
133
template <class ELFT> void elf::markLive() {
270
133
  // If -gc-sections is not given, no sections are removed.
271
133
  if (!Config->GcSections) {
272
131
    for (InputSectionBase *Sec : InputSections)
273
1.02k
      Sec->Live = true;
274
131
275
131
    // If a DSO defines a symbol referenced in a regular object, it is needed.
276
131
    for (Symbol *Sym : Symtab->getSymbols())
277
535
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
278
95
        if (S->IsUsedInRegularObj && 
!S->isWeak()27
)
279
27
          S->getFile().IsNeeded = true;
280
131
    return;
281
131
  }
282
2
283
2
  // Otheriwse, do mark-sweep GC.
284
2
  //
285
2
  // The -gc-sections option works only for SHF_ALLOC sections
286
2
  // (sections that are memory-mapped at runtime). So we can
287
2
  // unconditionally make non-SHF_ALLOC sections alive except
288
2
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
289
2
  //
290
2
  // Usually, SHF_ALLOC sections are not removed even if they are
291
2
  // unreachable through relocations because reachability is not
292
2
  // a good signal whether they are garbage or not (e.g. there is
293
2
  // usually no section referring to a .comment section, but we
294
2
  // want to keep it.).
295
2
  //
296
2
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
297
2
  // have a reverse dependency on the InputSection they are linked with.
298
2
  // We are able to garbage collect them.
299
2
  //
300
2
  // Note on SHF_REL{,A}: Such sections reach here only when -r
301
2
  // or -emit-reloc were given. And they are subject of garbage
302
2
  // collection because, if we remove a text section, we also
303
2
  // remove its relocation section.
304
17
  
for (InputSectionBase *Sec : InputSections)2
{
305
17
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
306
17
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
17
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
17
309
17
    if (!IsAlloc && 
!IsLinkOrder2
&&
!IsRel2
)
310
2
      Sec->Live = true;
311
17
  }
312
2
313
2
  // Follow the graph to mark all live sections.
314
2
  MarkLive<ELFT>().run();
315
2
316
2
  // Report garbage-collected sections.
317
2
  if (Config->PrintGcSections)
318
0
    for (InputSectionBase *Sec : InputSections)
319
0
      if (!Sec->Live)
320
0
        message("removing unused section " + toString(Sec));
321
2
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
269
1.96k
template <class ELFT> void elf::markLive() {
270
1.96k
  // If -gc-sections is not given, no sections are removed.
271
1.96k
  if (!Config->GcSections) {
272
1.88k
    for (InputSectionBase *Sec : InputSections)
273
335k
      Sec->Live = true;
274
1.88k
275
1.88k
    // If a DSO defines a symbol referenced in a regular object, it is needed.
276
1.88k
    for (Symbol *Sym : Symtab->getSymbols())
277
4.37k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
278
486
        if (S->IsUsedInRegularObj && 
!S->isWeak()254
)
279
244
          S->getFile().IsNeeded = true;
280
1.88k
    return;
281
1.88k
  }
282
74
283
74
  // Otheriwse, do mark-sweep GC.
284
74
  //
285
74
  // The -gc-sections option works only for SHF_ALLOC sections
286
74
  // (sections that are memory-mapped at runtime). So we can
287
74
  // unconditionally make non-SHF_ALLOC sections alive except
288
74
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
289
74
  //
290
74
  // Usually, SHF_ALLOC sections are not removed even if they are
291
74
  // unreachable through relocations because reachability is not
292
74
  // a good signal whether they are garbage or not (e.g. there is
293
74
  // usually no section referring to a .comment section, but we
294
74
  // want to keep it.).
295
74
  //
296
74
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
297
74
  // have a reverse dependency on the InputSection they are linked with.
298
74
  // We are able to garbage collect them.
299
74
  //
300
74
  // Note on SHF_REL{,A}: Such sections reach here only when -r
301
74
  // or -emit-reloc were given. And they are subject of garbage
302
74
  // collection because, if we remove a text section, we also
303
74
  // remove its relocation section.
304
370
  
for (InputSectionBase *Sec : InputSections)74
{
305
370
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
306
370
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
370
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
370
309
370
    if (!IsAlloc && 
!IsLinkOrder110
&&
!IsRel108
)
310
102
      Sec->Live = true;
311
370
  }
312
74
313
74
  // Follow the graph to mark all live sections.
314
74
  MarkLive<ELFT>().run();
315
74
316
74
  // Report garbage-collected sections.
317
74
  if (Config->PrintGcSections)
318
6
    for (InputSectionBase *Sec : InputSections)
319
28
      if (!Sec->Live)
320
16
        message("removing unused section " + toString(Sec));
321
74
}
void lld::elf::markLive<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
269
115
template <class ELFT> void elf::markLive() {
270
115
  // If -gc-sections is not given, no sections are removed.
271
115
  if (!Config->GcSections) {
272
114
    for (InputSectionBase *Sec : InputSections)
273
521
      Sec->Live = true;
274
114
275
114
    // If a DSO defines a symbol referenced in a regular object, it is needed.
276
114
    for (Symbol *Sym : Symtab->getSymbols())
277
10.3k
      if (auto *S = dyn_cast<SharedSymbol>(Sym))
278
35
        if (S->IsUsedInRegularObj && 
!S->isWeak()21
)
279
21
          S->getFile().IsNeeded = true;
280
114
    return;
281
114
  }
282
1
283
1
  // Otheriwse, do mark-sweep GC.
284
1
  //
285
1
  // The -gc-sections option works only for SHF_ALLOC sections
286
1
  // (sections that are memory-mapped at runtime). So we can
287
1
  // unconditionally make non-SHF_ALLOC sections alive except
288
1
  // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
289
1
  //
290
1
  // Usually, SHF_ALLOC sections are not removed even if they are
291
1
  // unreachable through relocations because reachability is not
292
1
  // a good signal whether they are garbage or not (e.g. there is
293
1
  // usually no section referring to a .comment section, but we
294
1
  // want to keep it.).
295
1
  //
296
1
  // Note on SHF_LINK_ORDER: Such sections contain metadata and they
297
1
  // have a reverse dependency on the InputSection they are linked with.
298
1
  // We are able to garbage collect them.
299
1
  //
300
1
  // Note on SHF_REL{,A}: Such sections reach here only when -r
301
1
  // or -emit-reloc were given. And they are subject of garbage
302
1
  // collection because, if we remove a text section, we also
303
1
  // remove its relocation section.
304
11
  
for (InputSectionBase *Sec : InputSections)1
{
305
11
    bool IsAlloc = (Sec->Flags & SHF_ALLOC);
306
11
    bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
11
    bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
11
309
11
    if (!IsAlloc && 
!IsLinkOrder1
&&
!IsRel1
)
310
1
      Sec->Live = true;
311
11
  }
312
1
313
1
  // Follow the graph to mark all live sections.
314
1
  MarkLive<ELFT>().run();
315
1
316
1
  // Report garbage-collected sections.
317
1
  if (Config->PrintGcSections)
318
0
    for (InputSectionBase *Sec : InputSections)
319
0
      if (!Sec->Live)
320
0
        message("removing unused section " + toString(Sec));
321
1
}
322
323
template void elf::markLive<ELF32LE>();
324
template void elf::markLive<ELF32BE>();
325
template void elf::markLive<ELF64LE>();
326
template void elf::markLive<ELF64BE>();