[PATCH] D55902: [ELF] SymbolTable::addShared: don't set IsNeeded unless IsUsedInRegularObj

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 19 14:17:53 PST 2018


MaskRay created this revision.
MaskRay added reviewers: ruiu, pcc.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.

In glibc, libc.so is a linker script with an as-needed dependency on ld-linux-x86-64.so.2

  GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )

ld-linux-x86-64.so.2 (as-needed) defines some symbols which resolve undefined references in libc.so.6, it will therefore be added as a DT_NEEDED entry, which isn't necessary.

The test case as-needed-not-in-regular.s emulates the libc.so scenario, where ld.bfd and gold don't add DT_NEEDED for a.so
This patch makes lld do the same thing.

The relevant code in gold/resolve.cc:

  // If we have a non-WEAK reference from a regular object to a
  // dynamic object, mark the dynamic object as needed.
  if (to->is_from_dynobj() && to->in_reg() && !to->is_undef_binding_weak())
    to->object()->set_is_needed();

in_reg() appears to do something similar to IsUsedInRegularObj.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D55902

Files:
  ELF/SymbolTable.cpp
  test/ELF/as-needed-not-in-regular.s


Index: test/ELF/as-needed-not-in-regular.s
===================================================================
--- /dev/null
+++ test/ELF/as-needed-not-in-regular.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+
+# RUN: echo '.globl a; .type a, @function; a: ret' | \
+# RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %ta.o
+# RUN: ld.lld %ta.o --shared --soname=a.so -o %ta.so
+
+# RUN: echo '.globl b; .type b, @function; b: jmp a at PLT' | \
+# RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tb.o
+# RUN: ld.lld %tb.o %ta.so --shared --soname=b.so -o %tb.so
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o %tb.so --as-needed %ta.so -o %t
+# RUN: llvm-readelf -d %t | FileCheck %s
+
+# The symbol a is not referenced by a regular object.
+# CHECK-NOT: a.so
+
+.global _start
+_start:
+  jmp b at PLT
Index: ELF/SymbolTable.cpp
===================================================================
--- ELF/SymbolTable.cpp
+++ ELF/SymbolTable.cpp
@@ -503,7 +503,8 @@
                                 Alignment, VerdefIndex);
     if (!WasInserted) {
       S->Binding = Binding;
-      if (!S->isWeak() && !Config->GcSections && WasUndefined)
+      if (!S->isWeak() && S->IsUsedInRegularObj && !Config->GcSections &&
+          WasUndefined)
         File.IsNeeded = true;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55902.178966.patch
Type: text/x-patch
Size: 1344 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181219/ed967a10/attachment.bin>


More information about the llvm-commits mailing list