[lld] r255355 - Discard local symbols from SHF_MERGE sections.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 11 10:49:29 PST 2015


Author: rafael
Date: Fri Dec 11 12:49:29 2015
New Revision: 255355

URL: http://llvm.org/viewvc/llvm-project?rev=255355&view=rev
Log:
Discard local symbols from SHF_MERGE sections.

This matches the behavior of both gold and bfd ld.

Added:
    lld/trunk/test/ELF/discard-merge-locals.s
Modified:
    lld/trunk/ELF/OutputSections.cpp

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=255355&r1=255354&r2=255355&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Fri Dec 11 12:49:29 2015
@@ -1168,15 +1168,26 @@ bool lld::elf2::shouldKeepInSymtab(const
   if (Sym.getType() == STT_SECTION)
     return false;
 
+  InputSectionBase<ELFT> *Sec = File.getSection(Sym);
   // If sym references a section in a discarded group, don't keep it.
-  if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
+  if (Sec == &InputSection<ELFT>::Discarded)
     return false;
 
   if (Config->DiscardNone)
     return true;
 
-  // ELF defines dynamic locals as symbols which name starts with ".L".
-  return !(Config->DiscardLocals && SymName.startswith(".L"));
+  // In ELF assembly .L symbols are normally discarded by the assembler.
+  // If the assembler fails to do so, the linker discards them if
+  // * --discard-locals is used.
+  // * The symbol is in a SHF_MERGE section, which is normally the reason for
+  //   the assembler keeping the .L symbol.
+  if (!SymName.startswith(".L"))
+    return true;
+
+  if (Config->DiscardLocals)
+    return false;
+
+  return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
 }
 
 template <class ELFT>

Added: lld/trunk/test/ELF/discard-merge-locals.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/discard-merge-locals.s?rev=255355&view=auto
==============================================================================
--- lld/trunk/test/ELF/discard-merge-locals.s (added)
+++ lld/trunk/test/ELF/discard-merge-locals.s Fri Dec 11 12:49:29 2015
@@ -0,0 +1,24 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+// RUN: ld.lld %t -o %t2 -shared
+// RUN: llvm-readobj -t %t2 | FileCheck %s
+// REQUIRES: x86
+
+	leaq	.L.str(%rip), %rdi
+
+	.section	.rodata.str1.1,"aMS", at progbits,1
+.L.str:
+	.asciz	"foobar"
+
+// Test that the .L symbol is omitted
+
+// 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: ]




More information about the llvm-commits mailing list