Don't copy STT_SECTION from the inputs.
This matches the behavior of gold and bfd ld.
llvm-svn: 249326
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index cd92493..8992811 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -407,7 +407,12 @@
return B.isUsedInDynamicReloc();
}
-bool lld::elf2::shouldKeepInSymtab(StringRef SymName) {
+template <class ELFT>
+bool lld::elf2::shouldKeepInSymtab(StringRef SymName,
+ const typename ELFFile<ELFT>::Elf_Sym &Sym) {
+ if (Sym.getType() == STT_SECTION)
+ return false;
+
if (Config->DiscardNone)
return true;
@@ -451,7 +456,7 @@
Elf_Sym_Range Syms = File.getLocalSymbols();
for (const Elf_Sym &Sym : Syms) {
ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
- if (SymName && !shouldKeepInSymtab(*SymName))
+ if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym))
continue;
auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Buf += sizeof(*ESym);
@@ -630,5 +635,14 @@
template bool includeInSymtab<ELF32BE>(const SymbolBody &);
template bool includeInSymtab<ELF64LE>(const SymbolBody &);
template bool includeInSymtab<ELF64BE>(const SymbolBody &);
+
+template bool shouldKeepInSymtab<ELF32LE>(StringRef,
+ const ELFFile<ELF32LE>::Elf_Sym &);
+template bool shouldKeepInSymtab<ELF32BE>(StringRef,
+ const ELFFile<ELF32BE>::Elf_Sym &);
+template bool shouldKeepInSymtab<ELF64LE>(StringRef,
+ const ELFFile<ELF64LE>::Elf_Sym &);
+template bool shouldKeepInSymtab<ELF64BE>(StringRef,
+ const ELFFile<ELF64BE>::Elf_Sym &);
}
}
diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h
index af81de0..c22c7d7 100644
--- a/lld/ELF/OutputSections.h
+++ b/lld/ELF/OutputSections.h
@@ -44,7 +44,10 @@
template <class ELFT> bool includeInSymtab(const SymbolBody &B);
bool includeInDynamicSymtab(const SymbolBody &B);
-bool shouldKeepInSymtab(StringRef SymName);
+
+template <class ELFT>
+bool shouldKeepInSymtab(
+ StringRef Name, const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
// This represents a section in an output file.
// Different sub classes represent different types of sections. Some contain
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 105abd7..30f02ee 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -293,7 +293,7 @@
Elf_Sym_Range Syms = File.getLocalSymbols();
for (const Elf_Sym &Sym : Syms) {
ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
- if (SymName && shouldKeepInSymtab(*SymName))
+ if (SymName && shouldKeepInSymtab<ELFT>(*SymName, Sym))
SymTabSec.addSymbol(*SymName, true);
}
}
diff --git a/lld/test/elf2/section-symbol.s b/lld/test/elf2/section-symbol.s
new file mode 100644
index 0000000..a3284bf
--- /dev/null
+++ b/lld/test/elf2/section-symbol.s
@@ -0,0 +1,29 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+// RUN: ld.lld2 %t -o %t.so -shared -discard-none
+// RUN: llvm-readobj -t %t.so | FileCheck %s
+
+// Test that we don't include the section symbols from the .o in the .so
+
+// CHECK: Symbols [
+// CHECK-NEXT: Symbol {
+// CHECK-NEXT: Name: (0)
+// CHECK-NEXT: Value: 0x0
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Binding: Local
+// CHECK-NEXT: Type: None
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: Undefined
+// CHECK-NEXT: }
+// CHECK-NEXT: Symbol {
+// CHECK-NEXT: Name: foo
+// CHECK-NEXT: Value:
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Binding: Local
+// CHECK-NEXT: Type: None
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: .text
+// CHECK-NEXT: }
+// CHECK-NEXT: ]
+
+foo:
+ .quad foo