ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
//===- MarkLive.cpp -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements --gc-sections, which is a feature to remove unused
|
|
|
|
// sections from output. Unused sections are sections that are not reachable
|
|
|
|
// from known GC-root symbols or sections. Naturally the feature is
|
|
|
|
// implemented as a mark-sweep garbage collector.
|
|
|
|
//
|
|
|
|
// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
|
|
|
|
// by default. Starting with GC-root symbols or sections, markLive function
|
|
|
|
// defined in this file visits all reachable sections to set their Live
|
|
|
|
// bits. Writer will then ignore sections whose Live bits are off, so that
|
2016-01-06 00:35:46 +08:00
|
|
|
// such sections are not included into output.
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-02-21 06:09:59 +08:00
|
|
|
#include "MarkLive.h"
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
#include "InputSection.h"
|
2016-02-23 15:47:54 +08:00
|
|
|
#include "LinkerScript.h"
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2016-04-27 07:52:44 +08:00
|
|
|
#include "Target.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
#include <functional>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::object;
|
2016-07-22 04:18:30 +08:00
|
|
|
using namespace llvm::support::endian;
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
2016-04-27 07:52:44 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static typename ELFT::uint getAddend(InputSectionBase &Sec,
|
2016-04-27 07:52:44 +08:00
|
|
|
const typename ELFT::Rel &Rel) {
|
2016-09-29 09:20:40 +08:00
|
|
|
return Target->getImplicitAddend(Sec.Data.begin() + Rel.r_offset,
|
2017-03-18 07:29:01 +08:00
|
|
|
Rel.getType(Config->IsMips64EL));
|
2016-04-27 07:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static typename ELFT::uint getAddend(InputSectionBase &Sec,
|
2016-04-27 07:52:44 +08:00
|
|
|
const typename ELFT::Rela &Rel) {
|
|
|
|
return Rel.r_addend;
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:48:18 +08:00
|
|
|
// There are normally few input sections whose names are valid C
|
|
|
|
// identifiers, so we just store a std::vector instead of a multimap.
|
|
|
|
static DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
|
|
|
|
|
2016-04-27 07:52:44 +08:00
|
|
|
template <class ELFT, class RelT>
|
2017-03-03 03:29:28 +08:00
|
|
|
static void resolveReloc(InputSectionBase &Sec, RelT &Rel,
|
2017-08-11 00:21:04 +08:00
|
|
|
std::function<void(InputSectionBase *, uint64_t)> Fn) {
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
|
2017-08-10 23:54:27 +08:00
|
|
|
|
2017-11-29 04:17:58 +08:00
|
|
|
// If a symbol is referenced in a live section, it is used.
|
|
|
|
B.Used = true;
|
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(&B))
|
|
|
|
if (!SS->isWeak())
|
2017-12-21 00:28:19 +08:00
|
|
|
SS->getFile<ELFT>().IsNeeded = true;
|
2017-11-29 04:17:58 +08:00
|
|
|
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *D = dyn_cast<Defined>(&B)) {
|
2017-12-12 06:47:43 +08:00
|
|
|
auto *RelSec = dyn_cast_or_null<InputSectionBase>(D->Section);
|
|
|
|
if (!RelSec)
|
2017-03-07 02:48:18 +08:00
|
|
|
return;
|
2017-08-11 00:21:04 +08:00
|
|
|
uint64_t Offset = D->Value;
|
2017-03-07 02:48:18 +08:00
|
|
|
if (D->isSection())
|
|
|
|
Offset += getAddend<ELFT>(Sec, Rel);
|
2017-12-12 06:47:43 +08:00
|
|
|
Fn(RelSec, Offset);
|
2017-08-10 23:54:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 12:39:07 +08:00
|
|
|
if (!B.isDefined())
|
2017-10-14 02:18:36 +08:00
|
|
|
for (InputSectionBase *Sec : CNamedSections.lookup(B.getName()))
|
2017-08-11 00:21:04 +08:00
|
|
|
Fn(Sec, 0);
|
2016-04-27 07:52:44 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 00:35:46 +08:00
|
|
|
// Calls Fn for each section that Sec refers to via relocations.
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
template <class ELFT>
|
2017-08-11 00:21:04 +08:00
|
|
|
static void
|
|
|
|
forEachSuccessor(InputSection &Sec,
|
|
|
|
std::function<void(InputSectionBase *, uint64_t)> Fn) {
|
2016-11-10 22:53:24 +08:00
|
|
|
if (Sec.AreRelocsRela) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
|
2017-03-03 03:29:28 +08:00
|
|
|
resolveReloc<ELFT>(Sec, Rel, Fn);
|
2016-11-10 22:53:24 +08:00
|
|
|
} else {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
|
2017-03-03 03:29:28 +08:00
|
|
|
resolveReloc<ELFT>(Sec, Rel, Fn);
|
2016-09-14 03:56:27 +08:00
|
|
|
}
|
2017-08-11 00:21:04 +08:00
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *IS : Sec.DependentSections)
|
2017-08-11 00:21:04 +08:00
|
|
|
Fn(IS, 0);
|
2016-05-02 21:49:42 +08:00
|
|
|
}
|
|
|
|
|
2016-07-22 04:18:30 +08:00
|
|
|
// The .eh_frame section is an unfortunate special case.
|
|
|
|
// The section is divided in CIEs and FDEs and the relocations it can have are
|
|
|
|
// * CIEs can refer to a personality function.
|
|
|
|
// * FDEs can refer to a LSDA
|
|
|
|
// * FDEs refer to the function they contain information about
|
|
|
|
// The last kind of relocation cannot keep the referred section alive, or they
|
|
|
|
// would keep everything alive in a common object file. In fact, each FDE is
|
|
|
|
// alive if the section it refers to is alive.
|
|
|
|
// To keep things simple, in here we just ignore the last relocation kind. The
|
|
|
|
// other two keep the referred section alive.
|
|
|
|
//
|
|
|
|
// A possible improvement would be to fully process .eh_frame in the middle of
|
|
|
|
// the gc pass. With that we would be able to also gc some sections holding
|
|
|
|
// LSDAs and personality functions if we found that they were unused.
|
|
|
|
template <class ELFT, class RelTy>
|
2017-08-11 00:21:04 +08:00
|
|
|
static void
|
|
|
|
scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels,
|
|
|
|
std::function<void(InputSectionBase *, uint64_t)> Fn) {
|
2016-07-22 04:18:30 +08:00
|
|
|
const endianness E = ELFT::TargetEndianness;
|
2017-08-11 00:21:04 +08:00
|
|
|
|
2016-07-22 04:18:30 +08:00
|
|
|
for (unsigned I = 0, N = EH.Pieces.size(); I < N; ++I) {
|
|
|
|
EhSectionPiece &Piece = EH.Pieces[I];
|
|
|
|
unsigned FirstRelI = Piece.FirstRelocation;
|
|
|
|
if (FirstRelI == (unsigned)-1)
|
|
|
|
continue;
|
2017-09-20 16:03:18 +08:00
|
|
|
if (read32<E>(Piece.data().data() + 4) == 0) {
|
2016-07-22 04:18:30 +08:00
|
|
|
// This is a CIE, we only need to worry about the first relocation. It is
|
|
|
|
// known to point to the personality function.
|
2017-08-11 00:21:04 +08:00
|
|
|
resolveReloc<ELFT>(EH, Rels[FirstRelI], Fn);
|
2016-07-22 04:18:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// This is a FDE. The relocations point to the described function or to
|
|
|
|
// a LSDA. We only need to keep the LSDA alive, so ignore anything that
|
|
|
|
// points to executable sections.
|
2017-09-19 07:07:09 +08:00
|
|
|
typename ELFT::uint PieceEnd = Piece.InputOff + Piece.Size;
|
2016-07-22 04:18:30 +08:00
|
|
|
for (unsigned I2 = FirstRelI, N2 = Rels.size(); I2 < N2; ++I2) {
|
|
|
|
const RelTy &Rel = Rels[I2];
|
|
|
|
if (Rel.r_offset >= PieceEnd)
|
|
|
|
break;
|
2017-08-11 00:21:04 +08:00
|
|
|
resolveReloc<ELFT>(EH, Rels[I2],
|
|
|
|
[&](InputSectionBase *Sec, uint64_t Offset) {
|
|
|
|
if (Sec && Sec != &InputSection::Discarded &&
|
|
|
|
!(Sec->Flags & SHF_EXECINSTR))
|
|
|
|
Fn(Sec, 0);
|
|
|
|
});
|
2016-07-22 04:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:14 +08:00
|
|
|
template <class ELFT>
|
2017-08-11 00:21:04 +08:00
|
|
|
static void
|
|
|
|
scanEhFrameSection(EhInputSection &EH,
|
|
|
|
std::function<void(InputSectionBase *, uint64_t)> Fn) {
|
2016-11-10 22:53:24 +08:00
|
|
|
if (!EH.NumRelocations)
|
2016-05-02 21:49:42 +08:00
|
|
|
return;
|
2016-07-22 04:18:30 +08:00
|
|
|
|
2016-11-10 22:53:24 +08:00
|
|
|
if (EH.AreRelocsRela)
|
2017-08-11 00:21:04 +08:00
|
|
|
scanEhFrameSection<ELFT>(EH, EH.template relas<ELFT>(), Fn);
|
2016-07-22 04:18:30 +08:00
|
|
|
else
|
2017-08-11 00:21:04 +08:00
|
|
|
scanEhFrameSection<ELFT>(EH, EH.template rels<ELFT>(), Fn);
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 16:26:32 +08:00
|
|
|
// Some sections are used directly by the loader, so they should never be
|
|
|
|
// garbage-collected. This function returns true if a given section is such
|
|
|
|
// section.
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> static bool isReserved(InputSectionBase *Sec) {
|
2016-10-26 20:36:56 +08:00
|
|
|
switch (Sec->Type) {
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
case SHT_FINI_ARRAY:
|
|
|
|
case SHT_INIT_ARRAY:
|
|
|
|
case SHT_NOTE:
|
|
|
|
case SHT_PREINIT_ARRAY:
|
|
|
|
return true;
|
|
|
|
default:
|
2016-10-04 08:46:36 +08:00
|
|
|
StringRef S = Sec->Name;
|
|
|
|
return S.startswith(".ctors") || S.startswith(".dtors") ||
|
2015-12-24 17:52:11 +08:00
|
|
|
S.startswith(".init") || S.startswith(".fini") ||
|
2015-11-10 01:44:10 +08:00
|
|
|
S.startswith(".jcr");
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 17:50:22 +08:00
|
|
|
// This is the main function of the garbage collector.
|
|
|
|
// Starting from GC-root sections, this function visits all reachable
|
|
|
|
// sections to set their "Live" bits.
|
2017-10-11 06:59:32 +08:00
|
|
|
template <class ELFT> static void doGcSections() {
|
2017-02-24 00:49:07 +08:00
|
|
|
SmallVector<InputSection *, 256> Q;
|
2017-03-07 02:48:18 +08:00
|
|
|
CNamedSections.clear();
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
2017-08-11 00:21:04 +08:00
|
|
|
auto Enqueue = [&](InputSectionBase *Sec, uint64_t Offset) {
|
2016-09-26 17:04:16 +08:00
|
|
|
// Skip over discarded sections. This in theory shouldn't happen, because
|
|
|
|
// the ELF spec doesn't allow a relocation to point to a deduplicated
|
|
|
|
// COMDAT section directly. Unfortunately this happens in practice (e.g.
|
|
|
|
// .eh_frame) so we need to add a check.
|
2017-08-11 00:21:04 +08:00
|
|
|
if (Sec == &InputSection::Discarded)
|
2016-04-23 06:09:35 +08:00
|
|
|
return;
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2016-10-20 07:13:40 +08:00
|
|
|
|
2016-05-24 00:55:43 +08:00
|
|
|
// Usually, a whole section is marked as live or dead, but in mergeable
|
|
|
|
// (splittable) sections, each piece of data has independent liveness bit.
|
|
|
|
// So we explicitly tell it which offset is in use.
|
2017-08-11 00:21:04 +08:00
|
|
|
if (auto *MS = dyn_cast<MergeInputSection>(Sec))
|
2018-04-28 00:29:57 +08:00
|
|
|
MS->getSectionPiece(Offset)->Live = true;
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2017-08-11 00:21:04 +08:00
|
|
|
if (Sec->Live)
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
return;
|
2017-08-11 00:21:04 +08:00
|
|
|
Sec->Live = true;
|
|
|
|
|
2016-10-20 07:13:40 +08:00
|
|
|
// Add input section to the queue.
|
2017-08-11 00:21:04 +08:00
|
|
|
if (InputSection *S = dyn_cast<InputSection>(Sec))
|
2016-10-20 07:13:40 +08:00
|
|
|
Q.push_back(S);
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
};
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
auto MarkSymbol = [&](Symbol *Sym) {
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *D = dyn_cast_or_null<Defined>(Sym))
|
2017-12-12 03:45:36 +08:00
|
|
|
if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
|
2017-08-11 00:21:04 +08:00
|
|
|
Enqueue(IS, D->Value);
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add GC root symbols.
|
2017-07-27 02:42:48 +08:00
|
|
|
MarkSymbol(Symtab->find(Config->Entry));
|
|
|
|
MarkSymbol(Symtab->find(Config->Init));
|
|
|
|
MarkSymbol(Symtab->find(Config->Fini));
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
for (StringRef S : Config->Undefined)
|
2017-07-27 02:42:48 +08:00
|
|
|
MarkSymbol(Symtab->find(S));
|
2017-10-11 09:19:33 +08:00
|
|
|
for (StringRef S : Script->ReferencedSymbols)
|
2017-07-27 02:42:48 +08:00
|
|
|
MarkSymbol(Symtab->find(S));
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
|
|
|
// Preserve externally-visible symbols if the symbols defined by this
|
2016-01-06 00:35:46 +08:00
|
|
|
// file can interrupt other ELF file's symbols at runtime.
|
2017-11-04 05:21:47 +08:00
|
|
|
for (Symbol *S : Symtab->getSymbols())
|
2017-03-07 02:48:18 +08:00
|
|
|
if (S->includeInDynsym())
|
2017-11-01 00:07:41 +08:00
|
|
|
MarkSymbol(S);
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
2016-02-23 15:47:54 +08:00
|
|
|
// Preserve special sections and those which are specified in linker
|
|
|
|
// script KEEP command.
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *Sec : InputSections) {
|
2017-11-30 22:01:06 +08:00
|
|
|
// Mark .eh_frame sections as live because there are usually no relocations
|
|
|
|
// that point to .eh_frames. Otherwise, the garbage collector would drop
|
|
|
|
// all of them. We also want to preserve personality routines and LSDA
|
|
|
|
// referenced by .eh_frame sections, so we scan them for that here.
|
2017-12-21 04:46:08 +08:00
|
|
|
if (auto *EH = dyn_cast<EhInputSection>(Sec)) {
|
2017-11-30 22:01:06 +08:00
|
|
|
EH->Live = true;
|
2016-11-06 06:37:59 +08:00
|
|
|
scanEhFrameSection<ELFT>(*EH, Enqueue);
|
2017-11-30 22:01:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-18 06:04:52 +08:00
|
|
|
if (Sec->Flags & SHF_LINK_ORDER)
|
|
|
|
continue;
|
2017-03-20 18:09:58 +08:00
|
|
|
if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec))
|
2017-08-11 00:21:04 +08:00
|
|
|
Enqueue(Sec, 0);
|
2017-03-07 02:48:18 +08:00
|
|
|
else if (isValidCIdentifier(Sec->Name)) {
|
|
|
|
CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
|
2017-07-27 06:52:53 +08:00
|
|
|
CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
|
2017-03-07 02:48:18 +08:00
|
|
|
}
|
2016-09-14 08:05:51 +08:00
|
|
|
}
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
|
|
|
// Mark all reachable sections.
|
|
|
|
while (!Q.empty())
|
2016-07-19 00:05:09 +08:00
|
|
|
forEachSuccessor<ELFT>(*Q.pop_back_val(), Enqueue);
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 06:59:32 +08:00
|
|
|
// Before calling this function, Live bits are off for all
|
|
|
|
// input sections. This function make some or all of them on
|
|
|
|
// so that they are emitted to the output file.
|
|
|
|
template <class ELFT> void elf::markLive() {
|
|
|
|
// If -gc-sections is missing, no sections are removed.
|
|
|
|
if (!Config->GcSections) {
|
|
|
|
for (InputSectionBase *Sec : InputSections)
|
|
|
|
Sec->Live = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The -gc-sections option works only for SHF_ALLOC sections
|
|
|
|
// (sections that are memory-mapped at runtime). So we can
|
2018-05-17 18:00:34 +08:00
|
|
|
// unconditionally make non-SHF_ALLOC sections alive except
|
|
|
|
// SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
|
2017-10-11 06:59:32 +08:00
|
|
|
//
|
2018-05-17 18:00:34 +08:00
|
|
|
// Usually, SHF_ALLOC sections are not removed even if they are
|
2017-10-11 06:59:32 +08:00
|
|
|
// unreachable through relocations because reachability is not
|
|
|
|
// a good signal whether they are garbage or not (e.g. there is
|
|
|
|
// usually no section referring to a .comment section, but we
|
2018-05-17 18:00:34 +08:00
|
|
|
// want to keep it.).
|
|
|
|
//
|
|
|
|
// Note on SHF_LINK_ORDER: Such sections contain metadata and they
|
|
|
|
// have a reverse dependency on the InputSection they are linked with.
|
|
|
|
// We are able to garbage collect them.
|
2017-10-11 06:59:32 +08:00
|
|
|
//
|
|
|
|
// Note on SHF_REL{,A}: Such sections reach here only when -r
|
|
|
|
// or -emit-reloc were given. And they are subject of garbage
|
|
|
|
// collection because, if we remove a text section, we also
|
|
|
|
// remove its relocation section.
|
|
|
|
for (InputSectionBase *Sec : InputSections) {
|
|
|
|
bool IsAlloc = (Sec->Flags & SHF_ALLOC);
|
2018-05-17 18:00:34 +08:00
|
|
|
bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
|
2017-10-11 06:59:32 +08:00
|
|
|
bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
|
2018-05-17 18:00:34 +08:00
|
|
|
if (!IsAlloc && !IsLinkOrder && !IsRel)
|
2017-10-11 06:59:32 +08:00
|
|
|
Sec->Live = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow the graph to mark all live sections.
|
|
|
|
doGcSections<ELFT>();
|
2017-10-27 19:32:22 +08:00
|
|
|
|
|
|
|
// Report garbage-collected sections.
|
|
|
|
if (Config->PrintGcSections)
|
|
|
|
for (InputSectionBase *Sec : InputSections)
|
|
|
|
if (!Sec->Live)
|
2018-02-17 08:09:49 +08:00
|
|
|
message("removing unused section " + toString(Sec));
|
2017-10-11 06:59:32 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:30:42 +08:00
|
|
|
template void elf::markLive<ELF32LE>();
|
|
|
|
template void elf::markLive<ELF32BE>();
|
|
|
|
template void elf::markLive<ELF64LE>();
|
|
|
|
template void elf::markLive<ELF64BE>();
|