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"
|
2019-05-29 11:55:20 +08:00
|
|
|
#include "SyntheticSections.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"
|
2020-01-29 00:05:13 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.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 <functional>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::object;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace lld;
|
|
|
|
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:
|
2019-05-29 11:55:20 +08:00
|
|
|
MarkLive(unsigned partition) : partition(partition) {}
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
void run();
|
2019-05-29 11:55:20 +08:00
|
|
|
void moveToMain();
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
private:
|
2019-05-17 07:33:06 +08:00
|
|
|
void enqueue(InputSectionBase *sec, uint64_t offset);
|
2019-03-26 07:28:47 +08:00
|
|
|
void markSymbol(Symbol *sym);
|
2019-05-29 11:55:20 +08:00
|
|
|
void mark();
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
template <class RelTy>
|
2020-11-18 01:11:19 +08:00
|
|
|
void resolveReloc(InputSectionBase &sec, RelTy &rel, bool fromFDE);
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
template <class RelTy>
|
|
|
|
void scanEhFrameSection(EhInputSection &eh, ArrayRef<RelTy> rels);
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
// The index of the partition that we are currently processing.
|
|
|
|
unsigned partition;
|
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
// A list of sections to visit.
|
2020-11-30 06:01:32 +08:00
|
|
|
SmallVector<InputSection *, 0> queue;
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
// There are normally few input sections whose names are valid C
|
2021-12-31 08:08:26 +08:00
|
|
|
// identifiers, so we just store a SmallVector instead of a multimap.
|
|
|
|
DenseMap<StringRef, SmallVector<InputSectionBase *, 0>> cNamedSections;
|
2019-03-26 07:28:47 +08:00
|
|
|
};
|
|
|
|
} // 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,
|
2020-11-18 01:11:19 +08:00
|
|
|
bool fromFDE) {
|
2019-03-26 07:28:47 +08:00
|
|
|
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
|
|
|
|
2020-11-18 01:11:19 +08:00
|
|
|
// fromFDE being true means this is referenced by a FDE in a .eh_frame
|
|
|
|
// piece. The relocation points to the described function or to a LSDA. We
|
|
|
|
// only need to keep the LSDA live, so ignore anything that points to
|
2021-02-06 13:35:27 +08:00
|
|
|
// executable sections. If the LSDA is in a section group or has the
|
|
|
|
// SHF_LINK_ORDER flag, we ignore the relocation as well because (a) if the
|
|
|
|
// associated text section is live, the LSDA will be retained due to section
|
|
|
|
// group/SHF_LINK_ORDER rules (b) if the associated text section should be
|
|
|
|
// discarded, marking the LSDA will unnecessarily retain the text section.
|
|
|
|
if (!(fromFDE && ((relSec->flags & (SHF_EXECINSTR | SHF_LINK_ORDER)) ||
|
|
|
|
relSec->nextInSectionGroup)))
|
2019-05-17 07:33:06 +08:00
|
|
|
enqueue(relSec, offset);
|
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-05-17 07:33:06 +08:00
|
|
|
enqueue(sec, 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.
|
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
|
|
|
|
2020-05-15 13:18:58 +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
|
|
|
|
|
|
|
uint64_t pieceEnd = piece.inputOff + piece.size;
|
2020-09-08 00:36:14 +08:00
|
|
|
for (size_t j = firstRelI, end2 = rels.size();
|
|
|
|
j < end2 && rels[j].r_offset < pieceEnd; ++j)
|
|
|
|
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_PREINIT_ARRAY:
|
2021-07-23 08:09:23 +08:00
|
|
|
return true;
|
2021-07-28 07:34:32 +08:00
|
|
|
case SHT_NOTE:
|
|
|
|
// SHT_NOTE sections in a group are subject to garbage collection.
|
|
|
|
return !sec->nextInSectionGroup;
|
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
|
|
|
default:
|
2021-12-29 08:40:51 +08:00
|
|
|
// Support SHT_PROGBITS .init_array (https://golang.org/issue/50295) and
|
|
|
|
// .init_array.N (https://github.com/rust-lang/rust/issues/92181) for a
|
|
|
|
// while.
|
2016-10-04 08:46:36 +08:00
|
|
|
StringRef s = sec->name;
|
2021-12-29 08:40:51 +08:00
|
|
|
return s == ".init" || s == ".fini" || s.startswith(".init_array") ||
|
|
|
|
s == ".jcr" || s.startswith(".ctors") || s.startswith(".dtors");
|
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-05-17 07:33:06 +08:00
|
|
|
void MarkLive<ELFT>::enqueue(InputSectionBase *sec, uint64_t offset) {
|
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-05-29 11:55:20 +08:00
|
|
|
// Set Sec->Partition to the meet (i.e. the "minimum") of Partition and
|
|
|
|
// Sec->Partition in the following lattice: 1 < other < 0. If Sec->Partition
|
|
|
|
// doesn't change, we don't need to do anything.
|
|
|
|
if (sec->partition == 1 || sec->partition == partition)
|
2019-03-26 07:28:47 +08:00
|
|
|
return;
|
2019-05-29 11:55:20 +08:00
|
|
|
sec->partition = sec->partition ? 1 : partition;
|
2019-05-17 07:33:06 +08:00
|
|
|
|
2019-03-26 07:28:47 +08:00
|
|
|
// Add input section to the queue.
|
2019-05-17 07:33:06 +08:00
|
|
|
if (InputSection *s = dyn_cast<InputSection>(sec))
|
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 *isec = dyn_cast_or_null<InputSectionBase>(d->section))
|
2019-05-17 07:33:06 +08:00
|
|
|
enqueue(isec, d->value);
|
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-05-29 11:55:20 +08:00
|
|
|
|
|
|
|
// Preserve externally-visible symbols if the symbols defined by this
|
|
|
|
// file can interrupt other ELF file's symbols at runtime.
|
2019-11-21 03:16:15 +08:00
|
|
|
for (Symbol *sym : symtab->symbols())
|
2019-05-29 11:55:20 +08:00
|
|
|
if (sym->includeInDynsym() && sym->partition == partition)
|
|
|
|
markSymbol(sym);
|
|
|
|
|
|
|
|
// If this isn't the main partition, that's all that we need to preserve.
|
|
|
|
if (partition != 1) {
|
|
|
|
mark();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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)) {
|
2019-05-29 11:55:20 +08:00
|
|
|
eh->markLive();
|
2019-03-26 07:28:47 +08:00
|
|
|
|
2021-10-28 00:51:06 +08:00
|
|
|
const RelsOrRelas<ELFT> rels = eh->template relsOrRelas<ELFT>();
|
|
|
|
if (rels.areRelocsRel())
|
|
|
|
scanEhFrameSection(*eh, rels.rels);
|
|
|
|
else if (rels.relas.size())
|
|
|
|
scanEhFrameSection(*eh, rels.relas);
|
2021-12-24 01:53:08 +08:00
|
|
|
continue;
|
2017-11-30 22:01:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-05 01:23:01 +08:00
|
|
|
if (sec->flags & SHF_GNU_RETAIN) {
|
|
|
|
enqueue(sec, 0);
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-18 06:04:52 +08:00
|
|
|
if (sec->flags & SHF_LINK_ORDER)
|
|
|
|
continue;
|
2019-01-10 06:24:27 +08:00
|
|
|
|
2021-12-24 01:53:08 +08:00
|
|
|
// Usually, non-SHF_ALLOC sections are not removed even if they are
|
|
|
|
// 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 want to keep it.) When a
|
|
|
|
// non-SHF_ALLOC section is retained, we also retain sections dependent on
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Note on nextInSectionGroup: The ELF spec says that group sections are
|
|
|
|
// included or omitted as a unit. We take the interpretation that:
|
|
|
|
//
|
|
|
|
// - Group members (nextInSectionGroup != nullptr) are subject to garbage
|
|
|
|
// collection.
|
|
|
|
// - Groups members are retained or discarded as a unit.
|
|
|
|
if (!(sec->flags & SHF_ALLOC)) {
|
|
|
|
bool isRel = sec->type == SHT_REL || sec->type == SHT_RELA;
|
|
|
|
if (!isRel && !sec->nextInSectionGroup) {
|
|
|
|
sec->markLive();
|
|
|
|
for (InputSection *isec : sec->dependentSections)
|
|
|
|
isec->markLive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve special sections and those which are specified in linker
|
|
|
|
// script KEEP command.
|
2019-03-15 14:58:23 +08:00
|
|
|
if (isReserved(sec) || script->shouldKeep(sec)) {
|
2019-05-17 07:33:06 +08:00
|
|
|
enqueue(sec, 0);
|
2021-04-17 03:18:45 +08:00
|
|
|
} else if ((!config->zStartStopGC || sec->name.startswith("__libc_")) &&
|
|
|
|
isValidCIdentifier(sec->name)) {
|
|
|
|
// As a workaround for glibc libc.a before 2.34
|
|
|
|
// (https://sourceware.org/PR27492), retain __libc_atexit and similar
|
|
|
|
// sections regardless of zStartStopGC.
|
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
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
mark();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void MarkLive<ELFT>::mark() {
|
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();
|
|
|
|
|
2021-10-28 00:51:06 +08:00
|
|
|
const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
|
|
|
|
for (const typename ELFT::Rel &rel : rels.rels)
|
|
|
|
resolveReloc(sec, rel, false);
|
|
|
|
for (const typename ELFT::Rela &rel : rels.relas)
|
|
|
|
resolveReloc(sec, rel, false);
|
2019-03-26 07:28:47 +08:00
|
|
|
|
|
|
|
for (InputSectionBase *isec : sec.dependentSections)
|
2019-05-17 07:33:06 +08:00
|
|
|
enqueue(isec, 0);
|
2019-11-19 13:56:58 +08:00
|
|
|
|
|
|
|
// Mark the next group member.
|
|
|
|
if (sec.nextInSectionGroup)
|
|
|
|
enqueue(sec.nextInSectionGroup, 0);
|
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-05-29 11:55:20 +08:00
|
|
|
// Move the sections for some symbols to the main partition, specifically ifuncs
|
|
|
|
// (because they can result in an IRELATIVE being added to the main partition's
|
|
|
|
// GOT, which means that the ifunc must be available when the main partition is
|
|
|
|
// loaded) and TLS symbols (because we only know how to correctly process TLS
|
|
|
|
// relocations for the main partition).
|
2019-08-09 08:57:54 +08:00
|
|
|
//
|
|
|
|
// We also need to move sections whose names are C identifiers that are referred
|
|
|
|
// to from __start_/__stop_ symbols because there will only be one set of
|
|
|
|
// symbols for the whole program.
|
2019-05-29 11:55:20 +08:00
|
|
|
template <class ELFT> void MarkLive<ELFT>::moveToMain() {
|
2021-12-15 16:37:10 +08:00
|
|
|
for (ELFFileBase *file : objectFiles)
|
2019-05-29 11:55:20 +08:00
|
|
|
for (Symbol *s : file->getSymbols())
|
|
|
|
if (auto *d = dyn_cast<Defined>(s))
|
|
|
|
if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section &&
|
|
|
|
d->section->isLive())
|
|
|
|
markSymbol(s);
|
|
|
|
|
2019-08-09 08:57:54 +08:00
|
|
|
for (InputSectionBase *sec : inputSections) {
|
|
|
|
if (!sec->isLive() || !isValidCIdentifier(sec->name))
|
|
|
|
continue;
|
|
|
|
if (symtab->find(("__start_" + sec->name).str()) ||
|
|
|
|
symtab->find(("__stop_" + sec->name).str()))
|
|
|
|
enqueue(sec, 0);
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
mark();
|
|
|
|
}
|
|
|
|
|
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.
|
2020-05-15 13:18:58 +08:00
|
|
|
template <class ELFT> void elf::markLive() {
|
2020-01-29 00:05:13 +08:00
|
|
|
llvm::TimeTraceScope timeScope("markLive");
|
2021-10-26 03:52:06 +08:00
|
|
|
// If --gc-sections is not given, retain all input sections.
|
2017-10-11 06:59:32 +08:00
|
|
|
if (!config->gcSections) {
|
|
|
|
for (InputSectionBase *sec : inputSections)
|
2019-05-29 11:55:20 +08:00
|
|
|
sec->markLive();
|
[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.
|
2019-11-21 03:16:15 +08:00
|
|
|
for (Symbol *sym : symtab->symbols())
|
[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 (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow the graph to mark all live sections.
|
2019-05-29 11:55:20 +08:00
|
|
|
for (unsigned curPart = 1; curPart <= partitions.size(); ++curPart)
|
|
|
|
MarkLive<ELFT>(curPart).run();
|
|
|
|
|
|
|
|
// If we have multiple partitions, some sections need to live in the main
|
|
|
|
// partition even if they were allocated to a loadable partition. Move them
|
|
|
|
// there now.
|
|
|
|
if (partitions.size() != 1)
|
|
|
|
MarkLive<ELFT>(1).moveToMain();
|
2017-10-27 19:32:22 +08:00
|
|
|
|
|
|
|
// Report garbage-collected sections.
|
|
|
|
if (config->printGcSections)
|
|
|
|
for (InputSectionBase *sec : inputSections)
|
2019-05-29 11:55:20 +08:00
|
|
|
if (!sec->isLive())
|
2018-02-17 08:09:49 +08:00
|
|
|
message("removing unused section " + toString(sec));
|
2017-10-11 06:59:32 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template void elf::markLive<ELF32LE>();
|
|
|
|
template void elf::markLive<ELF32BE>();
|
|
|
|
template void elf::markLive<ELF64LE>();
|
|
|
|
template void elf::markLive<ELF64BE>();
|