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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2016-07-02 16:50:05 +08:00
|
|
|
#include "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 "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2016-04-27 07:52:44 +08:00
|
|
|
#include "Target.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 "Writer.h"
|
|
|
|
#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-08-06 21:52:37 +08:00
|
|
|
namespace {
|
2016-04-27 07:52:44 +08:00
|
|
|
// A resolved relocation. The Sec and Offset fields are set if the relocation
|
|
|
|
// was resolved to an offset within a section.
|
2016-10-20 16:36:42 +08:00
|
|
|
template <class ELFT> struct ResolvedReloc {
|
2016-04-27 07:52:44 +08:00
|
|
|
InputSectionBase<ELFT> *Sec;
|
|
|
|
typename ELFT::uint Offset;
|
|
|
|
};
|
2016-08-06 21:52:37 +08:00
|
|
|
} // end anonymous namespace
|
2016-04-27 07:52:44 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
2016-07-19 00:05:09 +08:00
|
|
|
static typename ELFT::uint getAddend(InputSectionBase<ELFT> &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,
|
2016-04-27 07:52:44 +08:00
|
|
|
Rel.getType(Config->Mips64EL));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-07-19 00:05:09 +08:00
|
|
|
static typename ELFT::uint getAddend(InputSectionBase<ELFT> &Sec,
|
2016-04-27 07:52:44 +08:00
|
|
|
const typename ELFT::Rela &Rel) {
|
|
|
|
return Rel.r_addend;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT, class RelT>
|
2016-07-19 00:05:09 +08:00
|
|
|
static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
|
2016-05-02 21:49:42 +08:00
|
|
|
RelT &Rel) {
|
2016-07-19 00:05:09 +08:00
|
|
|
SymbolBody &B = Sec.getFile()->getRelocTargetSym(Rel);
|
2016-04-27 07:52:44 +08:00
|
|
|
auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
|
|
|
|
if (!D || !D->Section)
|
|
|
|
return {nullptr, 0};
|
|
|
|
typename ELFT::uint Offset = D->Value;
|
|
|
|
if (D->isSection())
|
|
|
|
Offset += getAddend(Sec, Rel);
|
|
|
|
return {D->Section->Repl, Offset};
|
|
|
|
}
|
|
|
|
|
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>
|
2016-07-19 00:05:09 +08:00
|
|
|
static void forEachSuccessor(InputSection<ELFT> &Sec,
|
2016-04-27 07:52:44 +08:00
|
|
|
std::function<void(ResolvedReloc<ELFT>)> Fn) {
|
2016-11-10 22:53:24 +08:00
|
|
|
if (Sec.AreRelocsRela) {
|
|
|
|
for (const typename ELFT::Rela &Rel : Sec.relas())
|
|
|
|
Fn(resolveReloc(Sec, Rel));
|
|
|
|
} else {
|
|
|
|
for (const typename ELFT::Rel &Rel : Sec.rels())
|
|
|
|
Fn(resolveReloc(Sec, Rel));
|
2016-09-14 03:56:27 +08:00
|
|
|
}
|
2017-02-16 16:41:19 +08:00
|
|
|
for (InputSectionBase<ELFT> *IS : Sec.DependentSections)
|
|
|
|
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>
|
|
|
|
static void
|
|
|
|
scanEhFrameSection(EhInputSection<ELFT> &EH, ArrayRef<RelTy> Rels,
|
|
|
|
std::function<void(ResolvedReloc<ELFT>)> Enqueue) {
|
|
|
|
const endianness E = ELFT::TargetEndianness;
|
|
|
|
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;
|
|
|
|
if (read32<E>(Piece.data().data() + 4) == 0) {
|
|
|
|
// This is a CIE, we only need to worry about the first relocation. It is
|
|
|
|
// known to point to the personality function.
|
|
|
|
Enqueue(resolveReloc(EH, Rels[FirstRelI]));
|
|
|
|
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.
|
|
|
|
typename ELFT::uint PieceEnd = Piece.InputOff + Piece.size();
|
|
|
|
for (unsigned I2 = FirstRelI, N2 = Rels.size(); I2 < N2; ++I2) {
|
|
|
|
const RelTy &Rel = Rels[I2];
|
|
|
|
if (Rel.r_offset >= PieceEnd)
|
|
|
|
break;
|
|
|
|
ResolvedReloc<ELFT> R = resolveReloc(EH, Rels[I2]);
|
|
|
|
if (!R.Sec || R.Sec == &InputSection<ELFT>::Discarded)
|
|
|
|
continue;
|
2016-10-26 20:36:56 +08:00
|
|
|
if (R.Sec->Flags & SHF_EXECINSTR)
|
2016-07-22 04:18:30 +08:00
|
|
|
continue;
|
|
|
|
Enqueue({R.Sec, 0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:14 +08:00
|
|
|
template <class ELFT>
|
2016-07-22 04:18:30 +08:00
|
|
|
static void
|
|
|
|
scanEhFrameSection(EhInputSection<ELFT> &EH,
|
|
|
|
std::function<void(ResolvedReloc<ELFT>)> Enqueue) {
|
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
|
|
|
|
|
|
|
// Unfortunately we need to split .eh_frame early since some relocations in
|
|
|
|
// .eh_frame keep other section alive and some don't.
|
|
|
|
EH.split();
|
|
|
|
|
2016-11-10 22:53:24 +08:00
|
|
|
if (EH.AreRelocsRela)
|
|
|
|
scanEhFrameSection(EH, EH.relas(), Enqueue);
|
2016-07-22 04:18:30 +08:00
|
|
|
else
|
2016-11-10 22:53:24 +08:00
|
|
|
scanEhFrameSection(EH, EH.rels(), 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
|
|
|
}
|
|
|
|
|
2016-09-26 16:32:41 +08:00
|
|
|
// We do not garbage-collect two types of sections:
|
2016-10-04 08:46:36 +08:00
|
|
|
// 1) Sections used by the loader (.init, .fini, .ctors, .dtors or .jcr)
|
|
|
|
// 2) Non-allocatable sections which typically contain debugging information
|
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> static bool isReserved(InputSectionBase<ELFT> *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:
|
2017-02-09 23:17:56 +08:00
|
|
|
if (Sec->Flags & SHF_LINK_ORDER)
|
|
|
|
return false;
|
|
|
|
|
2016-10-26 20:36:56 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
2016-10-04 08:46:36 +08:00
|
|
|
return true;
|
2016-02-25 16:40:26 +08:00
|
|
|
|
|
|
|
// We do not want to reclaim sections if they can be referred
|
|
|
|
// by __start_* and __stop_* symbols.
|
2016-10-04 08:46:36 +08:00
|
|
|
StringRef S = Sec->Name;
|
2016-02-25 16:40:26 +08:00
|
|
|
if (isValidCIdentifier(S))
|
|
|
|
return true;
|
|
|
|
|
2016-10-04 08:46:36 +08:00
|
|
|
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.
|
2016-05-03 03:30:42 +08:00
|
|
|
template <class ELFT> void elf::markLive() {
|
2015-11-13 03:42:43 +08:00
|
|
|
SmallVector<InputSection<ELFT> *, 256> Q;
|
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
|
|
|
auto Enqueue = [&](ResolvedReloc<ELFT> R) {
|
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.
|
|
|
|
if (!R.Sec || R.Sec == &InputSection<ELFT>::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
|
|
|
// We don't gc non alloc sections.
|
2016-10-26 20:36:56 +08:00
|
|
|
if (!(R.Sec->Flags & SHF_ALLOC))
|
2016-10-20 07:13:40 +08:00
|
|
|
return;
|
|
|
|
|
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.
|
|
|
|
if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(R.Sec))
|
|
|
|
MS->markLiveAt(R.Offset);
|
|
|
|
|
2016-04-27 07:52:44 +08:00
|
|
|
if (R.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;
|
2016-04-27 07:52:44 +08:00
|
|
|
R.Sec->Live = true;
|
2016-10-20 07:13:40 +08:00
|
|
|
// Add input section to the queue.
|
2016-04-27 07:52:44 +08:00
|
|
|
if (InputSection<ELFT> *S = dyn_cast<InputSection<ELFT>>(R.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
|
|
|
};
|
|
|
|
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
auto MarkSymbol = [&](const SymbolBody *Sym) {
|
|
|
|
if (auto *D = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
|
|
|
|
Enqueue({D->Section, 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.
|
2016-10-20 08:07:36 +08:00
|
|
|
MarkSymbol(Symtab<ELFT>::X->find(Config->Entry));
|
2016-05-03 03:30:42 +08:00
|
|
|
MarkSymbol(Symtab<ELFT>::X->find(Config->Init));
|
|
|
|
MarkSymbol(Symtab<ELFT>::X->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)
|
2016-05-03 03:30:42 +08:00
|
|
|
MarkSymbol(Symtab<ELFT>::X->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.
|
2016-05-03 03:30:42 +08:00
|
|
|
for (const Symbol *S : Symtab<ELFT>::X->getSymbols())
|
2016-04-27 00:26:45 +08:00
|
|
|
if (S->includeInDynsym())
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
MarkSymbol(S->body());
|
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.
|
2016-11-06 06:37:59 +08:00
|
|
|
for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
|
|
|
|
// .eh_frame is always marked as live now, but also it can reference to
|
|
|
|
// sections that contain personality. We preserve all non-text sections
|
|
|
|
// referred by .eh_frame here.
|
|
|
|
if (auto *EH = dyn_cast_or_null<EhInputSection<ELFT>>(Sec))
|
|
|
|
scanEhFrameSection<ELFT>(*EH, Enqueue);
|
|
|
|
if (isReserved(Sec) || Script<ELFT>::X->shouldKeep(Sec))
|
|
|
|
Enqueue({Sec, 0});
|
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
|
|
|
}
|
|
|
|
|
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>();
|