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 -------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
namespace {
|
|
|
|
template <class ELFT> class MarkLive {
|
|
|
|
public:
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
2019-04-10 18:37:10 +08:00
|
|
|
void enqueue(InputSectionBase *Sec, uint64_t Offset, bool IsLSDA);
|
2019-03-26 07:28:47 +08:00
|
|
|
void markSymbol(Symbol *Sym);
|
|
|
|
|
|
|
|
template <class RelTy>
|
|
|
|
void resolveReloc(InputSectionBase &Sec, RelTy &Rel, bool IsLSDA);
|
|
|
|
|
|
|
|
template <class RelTy>
|
|
|
|
void scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels);
|
|
|
|
|
|
|
|
// A list of sections to visit.
|
|
|
|
SmallVector<InputSection *, 256> Queue;
|
|
|
|
|
|
|
|
// There are normally few input sections whose names are valid C
|
|
|
|
// identifiers, so we just store a std::vector instead of a multimap.
|
|
|
|
DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2016-04-27 07:52:44 +08:00
|
|
|
template <class ELFT>
|
2019-03-26 07:28:47 +08:00
|
|
|
static uint64_t getAddend(InputSectionBase &Sec,
|
|
|
|
const typename ELFT::Rel &Rel) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +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>
|
2019-03-26 07:28:47 +08:00
|
|
|
static uint64_t getAddend(InputSectionBase &Sec,
|
|
|
|
const typename ELFT::Rela &Rel) {
|
2016-04-27 07:52:44 +08:00
|
|
|
return Rel.r_addend;
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
template <class ELFT>
|
|
|
|
template <class RelTy>
|
|
|
|
void MarkLive<ELFT>::resolveReloc(InputSectionBase &Sec, RelTy &Rel,
|
|
|
|
bool IsLSDA) {
|
|
|
|
Symbol &Sym = 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.
|
2019-03-26 07:28:47 +08:00
|
|
|
Sym.Used = true;
|
2017-11-29 04:17:58 +08:00
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
if (auto *D = dyn_cast<Defined>(&Sym)) {
|
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;
|
2019-03-26 07:28:47 +08:00
|
|
|
|
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);
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
if (!IsLSDA || !(RelSec->Flags & SHF_EXECINSTR))
|
2019-04-10 18:37:10 +08:00
|
|
|
enqueue(RelSec, Offset, IsLSDA);
|
2017-08-10 23:54:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(&Sym))
|
|
|
|
if (!SS->isWeak())
|
2019-04-09 01:35:55 +08:00
|
|
|
SS->getFile().IsNeeded = true;
|
2017-08-11 00:21:04 +08:00
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
|
2019-04-10 18:37:10 +08:00
|
|
|
enqueue(Sec, 0, IsLSDA);
|
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.
|
2019-03-26 07:28:47 +08:00
|
|
|
template <class ELFT>
|
|
|
|
template <class RelTy>
|
|
|
|
void MarkLive<ELFT>::scanEhFrameSection(EhInputSection &EH,
|
|
|
|
ArrayRef<RelTy> Rels) {
|
|
|
|
for (size_t I = 0, End = EH.Pieces.size(); I < End; ++I) {
|
2016-07-22 04:18:30 +08:00
|
|
|
EhSectionPiece &Piece = EH.Pieces[I];
|
2019-03-26 07:28:47 +08:00
|
|
|
size_t FirstRelI = Piece.FirstRelocation;
|
2016-07-22 04:18:30 +08:00
|
|
|
if (FirstRelI == (unsigned)-1)
|
|
|
|
continue;
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
if (read32<ELFT::TargetEndianness>(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.
|
2019-03-26 07:28:47 +08:00
|
|
|
resolveReloc(EH, Rels[FirstRelI], false);
|
2016-07-22 04:18:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-26 07:28:47 +08:00
|
|
|
|
2016-07-22 04:18:30 +08:00
|
|
|
// 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.
|
2019-03-26 07:28:47 +08:00
|
|
|
uint64_t PieceEnd = Piece.InputOff + Piece.Size;
|
|
|
|
for (size_t J = FirstRelI, End2 = Rels.size(); J < End2; ++J)
|
|
|
|
if (Rels[J].r_offset < PieceEnd)
|
|
|
|
resolveReloc(EH, Rels[J], true);
|
2016-07-22 04:18:30 +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.
|
2019-03-15 14:58:23 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
template <class ELFT>
|
2019-04-10 18:37:10 +08:00
|
|
|
void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset,
|
|
|
|
bool IsLSDA) {
|
2019-03-26 07:28:47 +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 (Sec == &InputSection::Discarded)
|
|
|
|
return;
|
2016-10-20 07:13:40 +08:00
|
|
|
|
2019-03-26 07:28:47 +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>(Sec))
|
|
|
|
MS->getSectionPiece(Offset)->Live = true;
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2019-04-10 18:37:10 +08:00
|
|
|
InputSection *S = dyn_cast<InputSection>(Sec);
|
|
|
|
// LSDA does not count as "live code or data" in the object file.
|
|
|
|
// The section may already have been marked live for LSDA in which
|
|
|
|
// case we still need to mark the file.
|
|
|
|
if (S && !IsLSDA && Sec->File)
|
|
|
|
Sec->getFile<ELFT>()->HasLiveCodeOrData = true;
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
if (Sec->Live)
|
|
|
|
return;
|
2017-08-11 00:21:04 +08:00
|
|
|
|
2019-04-10 18:37:10 +08:00
|
|
|
Sec->Live = true;
|
2019-03-26 07:28:47 +08:00
|
|
|
// Add input section to the queue.
|
2019-04-10 18:37:10 +08:00
|
|
|
if (S)
|
2019-03-26 07:28:47 +08:00
|
|
|
Queue.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
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
|
|
|
|
if (auto *D = dyn_cast_or_null<Defined>(Sym))
|
|
|
|
if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
|
2019-04-10 18:37:10 +08:00
|
|
|
enqueue(IS, D->Value, false);
|
2019-03-26 07:28:47 +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
|
|
|
|
2019-03-26 07:28:47 +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.
|
|
|
|
template <class ELFT> void MarkLive<ELFT>::run() {
|
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.
|
2019-03-26 07:28:47 +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)
|
2019-03-26 07:28:47 +08:00
|
|
|
markSymbol(Symtab->find(S));
|
2017-10-11 09:19:33 +08:00
|
|
|
for (StringRef S : Script->ReferencedSymbols)
|
2019-03-26 07:28:47 +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())
|
2019-03-26 07:28:47 +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;
|
2019-03-26 07:28:47 +08:00
|
|
|
if (!EH->NumRelocations)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (EH->AreRelocsRela)
|
|
|
|
scanEhFrameSection(*EH, EH->template relas<ELFT>());
|
|
|
|
else
|
|
|
|
scanEhFrameSection(*EH, EH->template rels<ELFT>());
|
2017-11-30 22:01:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-18 06:04:52 +08:00
|
|
|
if (Sec->Flags & SHF_LINK_ORDER)
|
|
|
|
continue;
|
2019-01-10 06:24:27 +08:00
|
|
|
|
2019-03-15 14:58:23 +08:00
|
|
|
if (isReserved(Sec) || Script->shouldKeep(Sec)) {
|
2019-04-10 18:37:10 +08:00
|
|
|
enqueue(Sec, 0, false);
|
2019-01-10 06:24:27 +08:00
|
|
|
} else if (isValidCIdentifier(Sec->Name)) {
|
2017-03-07 02:48:18 +08:00
|
|
|
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.
|
2019-03-26 07:28:47 +08:00
|
|
|
while (!Queue.empty()) {
|
|
|
|
InputSectionBase &Sec = *Queue.pop_back_val();
|
|
|
|
|
|
|
|
if (Sec.AreRelocsRela) {
|
|
|
|
for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
|
|
|
|
resolveReloc(Sec, Rel, false);
|
|
|
|
} else {
|
|
|
|
for (const typename ELFT::Rel &Rel : Sec.template rels<ELFT>())
|
|
|
|
resolveReloc(Sec, Rel, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (InputSectionBase *IS : Sec.DependentSections)
|
2019-04-10 18:37:10 +08:00
|
|
|
enqueue(IS, 0, false);
|
2019-03-26 07:28:47 +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
|
|
|
}
|
|
|
|
|
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() {
|
2019-03-26 07:28:47 +08:00
|
|
|
// If -gc-sections is not given, no sections are removed.
|
2017-10-11 06:59:32 +08:00
|
|
|
if (!Config->GcSections) {
|
|
|
|
for (InputSectionBase *Sec : InputSections)
|
|
|
|
Sec->Live = true;
|
[ELF] Move IsNeeded logic from SymbolTable::addShared to MarkLive, and check IsUsedInRegularObj
Summary:
In glibc, libc.so is a linker script with an as-needed dependency on ld-linux-x86-64.so.2
GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )
ld-linux-x86-64.so.2 (as-needed) defines some symbols which resolve undefined references in libc.so.6, it will therefore be added as a DT_NEEDED entry, which isn't necessary.
The test case as-needed-not-in-regular.s emulates the libc.so scenario, where ld.bfd and gold don't add DT_NEEDED for a.so
The relevant code in gold/resolve.cc:
// If we have a non-WEAK reference from a regular object to a
// dynamic object, mark the dynamic object as needed.
if (to->is_from_dynobj() && to->in_reg() && !to->is_undef_binding_weak())
to->object()->set_is_needed();
in_reg() appears to do something similar to IsUsedInRegularObj.
This patch makes lld do the similar thing, but moves the check from
addShared to a later stage MarkLive where all symbols are scanned.
Reviewers: ruiu, pcc, espindola
Reviewed By: ruiu
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D55902
llvm-svn: 349849
2018-12-21 06:46:01 +08:00
|
|
|
|
|
|
|
// If a DSO defines a symbol referenced in a regular object, it is needed.
|
|
|
|
for (Symbol *Sym : Symtab->getSymbols())
|
|
|
|
if (auto *S = dyn_cast<SharedSymbol>(Sym))
|
|
|
|
if (S->IsUsedInRegularObj && !S->isWeak())
|
2019-04-09 01:35:55 +08:00
|
|
|
S->getFile().IsNeeded = true;
|
2017-10-11 06:59:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
// Otheriwse, do mark-sweep GC.
|
|
|
|
//
|
2017-10-11 06:59:32 +08:00
|
|
|
// 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
|
2019-04-10 18:37:10 +08:00
|
|
|
// SHF_LINK_ORDER and SHT_REL/SHT_RELA sections and .debug 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);
|
2019-03-26 07:28:47 +08:00
|
|
|
|
2019-04-10 18:37:10 +08:00
|
|
|
if (!IsAlloc && !IsLinkOrder && !IsRel && !Sec->Debug)
|
2017-10-11 06:59:32 +08:00
|
|
|
Sec->Live = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow the graph to mark all live sections.
|
2019-03-26 07:28:47 +08:00
|
|
|
MarkLive<ELFT>().run();
|
2017-10-27 19:32:22 +08:00
|
|
|
|
2019-04-10 18:37:10 +08:00
|
|
|
// Mark debug sections as live in any object file that has a live
|
|
|
|
// Regular or Merge section.
|
|
|
|
for (InputSectionBase *Sec : InputSections)
|
|
|
|
if (Sec->Debug && Sec->getFile<ELFT>()->HasLiveCodeOrData)
|
|
|
|
Sec->Live = true;
|
|
|
|
|
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>();
|