Index: ELF/CMakeLists.txt =================================================================== --- ELF/CMakeLists.txt +++ ELF/CMakeLists.txt @@ -9,6 +9,7 @@ InputFiles.cpp InputSection.cpp LinkerScript.cpp + MarkLive.cpp OutputSections.cpp SymbolTable.cpp Symbols.cpp Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -51,6 +51,7 @@ bool DiscardNone; bool EnableNewDtags; bool ExportDynamic; + bool GcSections; bool Mips64EL = false; bool NoInhibitExec; bool NoUndefined; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -142,6 +142,7 @@ Config->DiscardNone = Args.hasArg(OPT_discard_none); Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); + Config->GcSections = Args.hasArg(OPT_gc_sections); Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec); Config->NoUndefined = Args.hasArg(OPT_no_undefined); Config->Shared = Args.hasArg(OPT_shared); @@ -242,5 +243,7 @@ // Write the result to the file. Symtab.scanShlibUndefined(); + if (Config->GcSections) + markLive(&Symtab); writeResult(&Symtab); } Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -122,6 +122,7 @@ } Elf_Sym_Range getLocalSymbols(); + const Elf_Sym *getLocalSymbol(uintX_t SymIndex); const Elf_Shdr *getSymbolTable() const { return this->Symtab; }; ArrayRef getSymbolTableShndx() const { return SymtabSHNDX; }; Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -75,6 +75,16 @@ } template +const typename ObjectFile::Elf_Sym * +ObjectFile::getLocalSymbol(uintX_t SymIndex) { + uint32_t FirstNonLocal = this->Symtab->sh_info; + if (SymIndex >= FirstNonLocal) + return nullptr; + Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab); + return Syms.begin() + SymIndex; +} + +template void elf2::ObjectFile::parse(DenseSet &Comdats) { // Read section and symbol tables. initializeSections(Comdats); Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -10,6 +10,7 @@ #ifndef LLD_ELF_INPUT_SECTION_H #define LLD_ELF_INPUT_SECTION_H +#include "Config.h" #include "lld/Core/LLVM.h" #include "llvm/Object/ELF.h" @@ -39,6 +40,11 @@ Kind SectionKind); OutputSectionBase *OutSec = nullptr; + // Used for garbage collection. + // Live bit makes sense only when Config->GcSections is true. + bool isLive() const { return !Config->GcSections || Live; } + bool Live = false; + // Returns the size of this section (even if this is a common or BSS.) size_t getSize() const { return Header->sh_size; } Index: ELF/MarkLive.cpp =================================================================== --- /dev/null +++ ELF/MarkLive.cpp @@ -0,0 +1,125 @@ +//===- MarkLive.cpp -------------------------------------------------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "InputSection.h" +#include "OutputSections.h" +#include "SymbolTable.h" +#include "Symbols.h" +#include "Writer.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Object/ELF.h" +#include +#include + +using namespace llvm; +using namespace llvm::ELF; +using namespace llvm::object; + +using namespace lld; +using namespace lld::elf2; + +template +static void +doForEachSuccessor(InputSectionBase *Sec, + std::function *)> Fn, + iterator_range *> Rels) { + typedef typename ELFFile::Elf_Sym Elf_Sym; + typedef Elf_Rel_Impl RelType; + + ObjectFile *File = Sec->getFile(); + for (const RelType &RI : Rels) { + // Global symbol + uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); + if (SymbolBody *B = File->getSymbolBody(SymIndex)) { + if (auto *D = dyn_cast>(B->repl())) + Fn(&D->Section); + continue; + } + // Local symbol + if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex)) + if (InputSectionBase *Sec = File->getSection(*Sym)) + Fn(Sec); + } +} + +// Calls Fn for each section that Sec refers to. +template +static void forEachSuccessor(InputSection *Sec, + std::function *)> Fn) { + typedef typename ELFFile::Elf_Shdr Elf_Shdr; + for (const Elf_Shdr *RelSec : Sec->RelocSections) { + if (RelSec->sh_type == SHT_RELA) + doForEachSuccessor(Sec, Fn, Sec->getFile()->getObj().relas(RelSec)); + else + doForEachSuccessor(Sec, Fn, Sec->getFile()->getObj().rels(RelSec)); + } +} + +// Sections listed below are special because they are used by the loader +// just by being in an ELF file. They should not be garbage-collected. +template static bool isReserved(InputSectionBase *Sec) { + switch (Sec->getSectionHdr()->sh_type) { + case SHT_FINI_ARRAY: + case SHT_INIT_ARRAY: + case SHT_NOTE: + case SHT_PREINIT_ARRAY: + return true; + default: + StringRef S = Sec->getSectionName(); + return S.startswith(".ctors") || S.startswith(".dtors"); + } +} + +template void lld::elf2::markLive(SymbolTable *Symtab) { + SmallVector *, 256> Q; + + auto Enqueue = [&](InputSectionBase *Sec) { + if (!Sec || Sec->Live) + return; + Sec->Live = true; + Q.push_back(Sec); + }; + + auto MarkSymbol = [&](SymbolBody *Sym) { + if (Sym) + if (auto *D = dyn_cast_or_null>(Sym->repl())) + Enqueue(&D->Section); + }; + + // Add GC root symbols. + MarkSymbol(Config->EntrySym); + MarkSymbol(Symtab->find(Config->Init)); + MarkSymbol(Symtab->find(Config->Fini)); + for (StringRef S : Config->Undefined) + MarkSymbol(Symtab->find(S)); + + // Preserve externally-visible symbols. + for (const std::pair &P : Symtab->getSymbols()) { + SymbolBody *B = P.second->Body; + if (B->getVisibility() == STV_DEFAULT) + MarkSymbol(B); + } + + // Sections with special names should not be removed by GC. + for (const std::unique_ptr> &F : Symtab->getObjectFiles()) + for (InputSectionBase *Sec : F->getSections()) + if (Sec && Sec != &InputSection::Discarded) + if (isReserved(Sec)) + Enqueue(Sec); + + // Mark all reachable sections. + while (!Q.empty()) + if (auto *Sec = dyn_cast>(Q.pop_back_val())) + forEachSuccessor(Sec, Enqueue); +} + +template void lld::elf2::markLive(SymbolTable *); +template void lld::elf2::markLive(SymbolTable *); +template void lld::elf2::markLive(SymbolTable *); +template void lld::elf2::markLive(SymbolTable *); Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -46,6 +46,9 @@ def fini : Separate<["-"], "fini">, MetaVarName<"">, HelpText<"Specify a finalizer function">; +def gc_sections : Flag<["--"], "gc-sections">, + HelpText<"Enable garbage collection of unused sections">; + def init : Separate<["-"], "init">, MetaVarName<"">, HelpText<"Specify an initializer function">; @@ -123,7 +126,6 @@ def eh_frame_hdr : Flag<["--"], "eh-frame-hdr">; def end_group : Flag<["--"], "end-group">; def fatal_warnings : Flag<["--"], "fatal-warnings">; -def gc_sections : Flag<["--"], "gc-sections">; def hash_style : Joined<["--"], "hash-style=">; def no_add_needed : Flag<["--"], "no-add-needed">; def no_fatal_warnings : Flag<["--"], "no-fatal-warnings">; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -744,21 +744,23 @@ continue; auto *ESym = reinterpret_cast(Buf); - Buf += sizeof(*ESym); - ESym->st_name = StrTabSec.getFileOff(SymName); - ESym->st_size = Sym.st_size; - ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); uintX_t VA = 0; if (Sym.st_shndx == SHN_ABS) { ESym->st_shndx = SHN_ABS; VA = Sym.st_value; } else { const InputSectionBase *Section = File->getSection(Sym); + if (!Section->isLive()) + continue; const OutputSectionBase *OutSec = Section->OutSec; ESym->st_shndx = OutSec->SectionIndex; VA += OutSec->getVA() + Section->getOffset(Sym); } + ESym->st_name = StrTabSec.getFileOff(SymName); + ESym->st_size = Sym.st_size; + ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); ESym->st_value = VA; + Buf += sizeof(*ESym); } } } @@ -769,20 +771,19 @@ // pointed by Buf. auto *ESym = reinterpret_cast(Buf); for (SymbolBody *Body : Symbols) { - StringRef Name = Body->getName(); - - ESym->st_name = StrTabSec.getFileOff(Name); - const OutputSectionBase *OutSec = nullptr; - const InputSectionBase *Section = nullptr; switch (Body->kind()) { case SymbolBody::DefinedSyntheticKind: OutSec = &cast>(Body)->Section; break; - case SymbolBody::DefinedRegularKind: - Section = &cast>(Body)->Section; + case SymbolBody::DefinedRegularKind: { + auto *Sym = cast>(Body->repl()); + if (!Sym->Section.isLive()) + continue; + OutSec = Sym->Section.OutSec; break; + } case SymbolBody::DefinedCommonKind: OutSec = Out::Bss; break; @@ -793,6 +794,9 @@ break; } + StringRef Name = Body->getName(); + ESym->st_name = StrTabSec.getFileOff(Name); + unsigned char Type = STT_NOTYPE; uintX_t Size = 0; if (const auto *EBody = dyn_cast>(Body)) { @@ -806,9 +810,6 @@ ESym->setVisibility(Body->getVisibility()); ESym->st_value = getSymVA(*Body); - if (Section) - OutSec = Section->OutSec; - if (isa>(Body)) ESym->st_shndx = SHN_ABS; else if (OutSec) Index: ELF/SymbolTable.h =================================================================== --- ELF/SymbolTable.h +++ ELF/SymbolTable.h @@ -55,6 +55,7 @@ void addIgnoredSym(StringRef Name); bool isUndefined(StringRef Name); void scanShlibUndefined(); + SymbolBody *find(StringRef Name); private: Symbol *insert(SymbolBody *New); @@ -63,7 +64,6 @@ void addMemberFile(Lazy *Body); void checkCompatibility(std::unique_ptr &File); void resolve(SymbolBody *Body); - SymbolBody *find(StringRef Name); void reportConflict(const Twine &Message, const SymbolBody &Old, const SymbolBody &New, bool Warning); Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -225,7 +225,7 @@ return S->kind() == Base::DefinedRegularKind; } - const InputSectionBase &Section; + InputSectionBase &Section; }; template class DefinedSynthetic : public Defined { Index: ELF/Writer.h =================================================================== --- ELF/Writer.h +++ ELF/Writer.h @@ -16,6 +16,8 @@ template class SymbolTable; template void writeResult(SymbolTable *Symtab); + +template void markLive(SymbolTable *Symtab); } } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -422,7 +422,7 @@ for (const std::unique_ptr> &F : Symtab.getObjectFiles()) { for (InputSectionBase *C : F->getSections()) { - if (!C || C == &InputSection::Discarded) + if (!C || !C->isLive() || C == &InputSection::Discarded) continue; const Elf_Shdr *H = C->getSectionHdr(); uintX_t OutFlags = H->sh_flags & ~SHF_GROUP; @@ -493,7 +493,8 @@ for (InputSectionBase *B : F->getSections()) if (auto *S = dyn_cast_or_null>(B)) if (S != &InputSection::Discarded) - scanRelocs(*S); + if (S->isLive()) + scanRelocs(*S); // FIXME: Try to avoid the extra walk over all global symbols. std::vector *> CommonSymbols; Index: test/elf2/gc-sections.s =================================================================== --- /dev/null +++ test/elf2/gc-sections.s @@ -0,0 +1,70 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: ld.lld2 %t -o %t2 +# RUN: llvm-readobj -sections -symbols %t2 | FileCheck -check-prefix=NOGC %s +# RUN: ld.lld2 --gc-sections %t -o %t2 +# RUN: llvm-readobj -sections -symbols %t2 | FileCheck -check-prefix=GC %s + +# NOGC: Name: .text +# NOGC: Name: .init +# NOGC: Name: .fini +# NOGC: Name: a +# NOGC: Name: b +# NOGC: Name: c +# NOGC: Name: x +# NOGC: Name: y +# NOGC: Name: __preinit_array_start +# NOGC: Name: __preinit_array_end +# NOGC: Name: d + +# GC: Name: .text +# GC: Name: .init +# GC: Name: .fini +# GC: Name: a +# GC: Name: b +# GC: Name: c +# GC-NOT: Name: x +# GC-NOT: Name: y +# GC: Name: __preinit_array_start +# GC: Name: __preinit_array_end +# GC: Name: d + +.globl _start, d +.protected a, b, c, x, y +_start: + call a + +.section .text.a,"ax",@progbits +a: + call _start + call b + +.section .text.b,"ax",@progbits +b: + call c + +.section .text.c,"ax",@progbits +c: + nop + +.section .text.d,"ax",@progbits +d: + nop + +.section .text.x,"ax",@progbits +x: + call y + +.section .text.y,"ax",@progbits +y: + call x + +.section .init,"aw",@init_array + .quad 0 + +.section .fini,"aw",@fini_array + .quad 0 + +.section .preinit_array,"aw",@preinit_array + .quad 0