From 4b075bb21879f25b24391e669eb4e910262dfff8 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 26 Jul 2017 23:39:10 +0000 Subject: [PATCH] Make __start_sec __end_sec handling more precise. With this we only ask LTO to keep a C named section if there is a __start_ or __end symbol. This is not as strict as lld's --gc-sections, but is as good as we can get without having a far more detailed ir summary. llvm-svn: 309232 --- lld/ELF/LTO.cpp | 12 ++++++++++-- lld/ELF/LTO.h | 2 ++ lld/test/ELF/lto/section-name.ll | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index 66f2a2a..bf2dadc 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -11,6 +11,7 @@ #include "Config.h" #include "Error.h" #include "InputFiles.h" +#include "SymbolTable.h" #include "Symbols.h" #include "lld/Core/TargetOptionsCommandFlags.h" #include "llvm/ADT/STLExtras.h" @@ -107,7 +108,14 @@ static std::unique_ptr createLTO() { Config->LTOPartitions); } -BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {} +BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) { + for (Symbol *Sym : Symtab->getSymbols()) { + StringRef Name = Sym->body()->getName(); + for (StringRef Prefix : {"__start_", "__stop_"}) + if (Name.startswith(Prefix)) + UsedStartStop.insert(Name.substr(Prefix.size())); + } +} BitcodeCompiler::~BitcodeCompiler() = default; @@ -138,7 +146,7 @@ void BitcodeCompiler::add(BitcodeFile &F) { R.VisibleToRegularObj = Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym()) || - isValidCIdentifier(ObjSym.getSectionName()); + UsedStartStop.count(ObjSym.getSectionName()); if (R.Prevailing) undefine(Sym); R.LinkerRedefined = Config->RenamedSymbols.count(Sym); diff --git a/lld/ELF/LTO.h b/lld/ELF/LTO.h index d19923c..79dd232 100644 --- a/lld/ELF/LTO.h +++ b/lld/ELF/LTO.h @@ -22,6 +22,7 @@ #define LLD_ELF_LTO_H #include "lld/Core/LLVM.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallString.h" #include #include @@ -50,6 +51,7 @@ private: std::unique_ptr LTOObj; std::vector> Buff; std::vector> Files; + llvm::DenseSet UsedStartStop; }; } // namespace elf } // namespace lld diff --git a/lld/test/ELF/lto/section-name.ll b/lld/test/ELF/lto/section-name.ll index 1e62769..cc5f914 100644 --- a/lld/test/ELF/lto/section-name.ll +++ b/lld/test/ELF/lto/section-name.ll @@ -7,5 +7,21 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @foo = hidden global i32 42, section "foo_section" +@bar = hidden global i32 42, section "bar_section" +@zed = hidden global i32 42, section "zed_section" -; CHECK: foo_section PROGBITS +@__start_foo_section = external global i32 +@__stop_bar_section = external global i32 + +define i32* @use1() { + ret i32* @__start_foo_section +} + +define i32* @use2() { + ret i32* @__stop_bar_section +} + +; CHECK-NOT: zed_section +; CHECK: foo_section PROGBITS +; CHECK-NEXT: bar_section PROGBITS +; CHECK-NOT: zed_section -- 2.7.4