[lld] r267558 - Handle --as-needed with symbols, not relocations.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 26 06:56:27 PDT 2016


Author: rafael
Date: Tue Apr 26 08:56:26 2016
New Revision: 267558

URL: http://llvm.org/viewvc/llvm-project?rev=267558&view=rev
Log:
Handle --as-needed with symbols, not relocations.

This matches the behavior of both gold and bfd.

Added:
    lld/trunk/test/ELF/as-needed-no-reloc.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=267558&r1=267557&r2=267558&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Apr 26 08:56:26 2016
@@ -521,9 +521,7 @@ void Writer<ELFT>::scanRelocs(InputSecti
   for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
     const RelTy &RI = *I;
     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
-    SymbolBody &OrigBody = File.getSymbolBody(SymIndex);
-    Symbol *Sym = OrigBody.Backref;
-    SymbolBody &Body = Sym ? *Sym->Body : OrigBody;
+    SymbolBody &Body = File.getSymbolBody(SymIndex).repl();
     uint32_t Type = RI.getType(Config->Mips64EL);
 
     // Ignore "hint" relocation because it is for optional code optimization.
@@ -534,11 +532,6 @@ void Writer<ELFT>::scanRelocs(InputSecti
     if (Offset == (uintX_t)-1)
       continue;
 
-    // Set "used" bit for --as-needed.
-    if (OrigBody.isUndefined() && Sym && !Sym->isWeak())
-      if (auto *S = dyn_cast<SharedSymbol<ELFT>>(&Body))
-        S->File->IsUsed = true;
-
     RelExpr Expr = Target->getRelExpr(Type, Body);
 
     // This relocation does not require got entry, but it is relative to got and
@@ -1344,6 +1337,12 @@ template <class ELFT> void Writer<ELFT>:
   std::vector<DefinedCommon *> CommonSymbols;
   for (Symbol *S : Symtab.getSymbols()) {
     SymbolBody *Body = S->Body;
+
+    // Set "used" bit for --as-needed.
+    if (S->IsUsedInRegularObj && !S->isWeak())
+      if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
+        SS->File->IsUsed = true;
+
     if (Body->isUndefined() && !S->isWeak()) {
       auto *U = dyn_cast<UndefinedElf<ELFT>>(Body);
       if (!U || !U->canKeepUndefined())

Added: lld/trunk/test/ELF/as-needed-no-reloc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/as-needed-no-reloc.s?rev=267558&view=auto
==============================================================================
--- lld/trunk/test/ELF/as-needed-no-reloc.s (added)
+++ lld/trunk/test/ELF/as-needed-no-reloc.s Tue Apr 26 08:56:26 2016
@@ -0,0 +1,23 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/shared.s -o %t2.o
+# RUN: ld.lld -shared %t2.o -o %t2.so
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -o %t %t.o --as-needed %t2.so
+# RUN: llvm-readobj --dynamic-table --dyn-symbols %t | FileCheck %s
+
+
+# There must be a NEEDED entry for each undefined
+
+# CHECK:      Name: bar
+# CHECK-NEXT: Value: 0x0
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Binding: Global
+# CHECK-NEXT: Type: Function
+# CHECK-NEXT: Other: 0
+# CHECK-NEXT: Section: Undefined
+
+# CHECK: NEEDED SharedLibrary ({{.*}}2.so)
+
+        .globl _start
+_start:
+        .global bar




More information about the llvm-commits mailing list