[PATCH] D37009: [ELF] - Fix for "Bug 34238 - LTO is optimizing away symbols referenced from linker scripts"

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 22 05:53:42 PDT 2017


grimar created this revision.
Herald added subscribers: inglorion, emaste.

Code suggested by Rui Ueyama in PR34238 comments.

Previously LTO did optimized away symbols referenced from linker script
because did not see that them are used from regular objects.
Patch add such symbols as undefined earlier, before running LTO,
what sets IsUsedInRegularObj for them and fixes the issue.


https://reviews.llvm.org/D37009

Files:
  ELF/Driver.cpp
  test/ELF/lto/linker-script-symbols.ll


Index: test/ELF/lto/linker-script-symbols.ll
===================================================================
--- test/ELF/lto/linker-script-symbols.ll
+++ test/ELF/lto/linker-script-symbols.ll
@@ -0,0 +1,29 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: echo "foo = bar;" > %t.script
+
+; RUN: ld.lld -m elf_x86_64 %t.o -o %t2 --script %t.script -save-temps
+; RUN: llvm-readobj -symbols %t2.lto.o | FileCheck %s
+
+; CHECK-NOT:  zed
+; CHECK:      Symbol {
+; CHECK:        Name: bar
+; CHECK-NEXT:   Value:
+; CHECK-NEXT:   Size:
+; CHECK-NEXT:   Binding: Global
+; CHECK-NEXT:   Type: Function
+; CHECK-NEXT:   Other:
+; CHECK-NEXT:   Section:
+; CHECK-NEXT: }
+; CHECK-NOT:  zed
+
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @bar() {
+  ret void
+}
+
+define void @zed() {
+  ret void
+}
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -1012,6 +1012,13 @@
   for (InputFile *F : Files)
     Symtab->addFile<ELFT>(F);
 
+  // Some symbols (such as __ehdr_start) are defined lazily only when there
+  // are undefined symbols for them, so we add these to trigger that logic.
+  // We also want to add symbols referenced by linker script so that LTO
+  // can see them and perform DCE correctly.
+  for (StringRef Sym : Script->Opt.ReferencedSymbols)
+    Symtab->addUndefined<ELFT>(Sym);
+
   // If an entry symbol is in a static archive, pull out that file now
   // to complete the symbol table. After this, no new names except a
   // few linker-synthesized ones will be added to the symbol table.
@@ -1047,11 +1054,6 @@
   if (ErrorCount)
     return;
 
-  // Some symbols (such as __ehdr_start) are defined lazily only when there
-  // are undefined symbols for them, so we add these to trigger that logic.
-  for (StringRef Sym : Script->Opt.ReferencedSymbols)
-    Symtab->addUndefined<ELFT>(Sym);
-
   // Apply symbol renames for -wrap and -defsym
   Symtab->applySymbolRenames();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37009.112159.patch
Type: text/x-patch
Size: 2064 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170822/3b16ef27/attachment.bin>


More information about the llvm-commits mailing list