Index: ELF/LTO.cpp =================================================================== --- ELF/LTO.cpp +++ 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" @@ -122,6 +123,14 @@ std::vector Syms = F.getSymbols(); std::vector Resols(Syms.size()); + DenseSet UsedStartStop; + for (Symbol *Sym : Symtab->getSymbols()) { + StringRef Name = Sym->body()->getName(); + for (StringRef Prefix : {"__start_", "__end_"}) + if (Name.startswith(Prefix)) + UsedStartStop.insert(Name.substr(Prefix.size())); + } + // Provide a resolution to the LTO API for each symbol. for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { Symbol *Sym = Syms[SymNum]; @@ -138,7 +147,7 @@ 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); Index: test/ELF/lto/section-name.ll =================================================================== --- test/ELF/lto/section-name.ll +++ test/ELF/lto/section-name.ll @@ -7,5 +7,14 @@ target triple = "x86_64-unknown-linux-gnu" @foo = hidden global i32 42, section "foo_section" +@bar = hidden global i32 42, section "bar_section" -; CHECK: foo_section PROGBITS +@__start_foo_section = external global i32 + +define i32* @use() { + ret i32* @__start_foo_section +} + +; CHECK-NOT: bar_section +; CHECK: foo_section PROGBITS +; CHECK-NOT: bar_section