[binutils-gdb] ld: Add more tests for --as-needed

H.J. Lu hjl@sourceware.org
Thu Sep 10 14:53:02 GMT 2020


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=93d49941edbb5274bee6ce3e547a07c7ce4c4301

commit 93d49941edbb5274bee6ce3e547a07c7ce4c4301
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Sep 10 07:52:03 2020 -0700

    ld: Add more tests for --as-needed
    
    Prior to
    
    commit 1e3b96fd6cf0c7d018083994ad951ccf92aba582
    Author: Alan Modra <amodra@gmail.com>
    Date:   Fri Sep 4 13:54:21 2020 +0930
    
        Allow plugin syms to mark as-needed shared libs needed
    
    when removing unused IR symbol references, ld didn't add unnecessary
    DT_NEEDED libraries which may lead to undefined symbol reference in a
    --as-needed library when the symbol is defined in a prior --as-needed
    library and there is no reference in relocatable inputs.  This behavior
    is desirable since it ensures that both lazy and non-lazy bindings work
    the same way.  The problem is with --as-needed libraries, which happens
    with and without LTO.  Now, the linker may add many unnecessary DT_NEEDED
    libraries for IR inputs.
    
            PR ld/26590
            * testsuite/ld-elf/pr26590.err: New file.
            * testsuite/ld-elf/pr26590a.c: Likewise.
            * testsuite/ld-elf/pr26590b.c: Likewise.
            * testsuite/ld-elf/pr26590c.c: Likewise.
            * testsuite/ld-elf/pr26590d.c: Likewise.
            * testsuite/ld-elf/shared.exp: Run ld/26590 tests.

Diff:
---
 ld/ChangeLog                    | 10 ++++++++
 ld/testsuite/ld-elf/pr26590.err |  3 +++
 ld/testsuite/ld-elf/pr26590a.c  | 10 ++++++++
 ld/testsuite/ld-elf/pr26590b.c  | 10 ++++++++
 ld/testsuite/ld-elf/pr26590c.c  | 11 +++++++++
 ld/testsuite/ld-elf/pr26590d.c  |  1 +
 ld/testsuite/ld-elf/shared.exp  | 54 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 99 insertions(+)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index 46a4aca2a56..3e8662868ae 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,13 @@
+2020-09-10  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR ld/26590
+	* testsuite/ld-elf/pr26590.err: New file.
+	* testsuite/ld-elf/pr26590a.c: Likewise.
+	* testsuite/ld-elf/pr26590b.c: Likewise.
+	* testsuite/ld-elf/pr26590c.c: Likewise.
+	* testsuite/ld-elf/pr26590d.c: Likewise.
+	* testsuite/ld-elf/shared.exp: Run ld/26590 tests.
+
 2020-09-10  Alan Modra  <amodra@gmail.com>
 
 	* testsuite/ld-elf/pr26580-2.sd: Accept undefined symbol.
diff --git a/ld/testsuite/ld-elf/pr26590.err b/ld/testsuite/ld-elf/pr26590.err
new file mode 100644
index 00000000000..d8341486bd6
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26590.err
@@ -0,0 +1,3 @@
+#...
+.*: tmpdir/libpr26590b.so: undefined reference to `f1'
+#pass
diff --git a/ld/testsuite/ld-elf/pr26590a.c b/ld/testsuite/ld-elf/pr26590a.c
new file mode 100644
index 00000000000..0ae3a20c146
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26590a.c
@@ -0,0 +1,10 @@
+int select (void) { return 1; }
+
+extern int f2 (int);
+
+int f1 (int x)
+{
+  if (x > 0)
+    return x * f2 (x - 1);
+  return 1;
+}
diff --git a/ld/testsuite/ld-elf/pr26590b.c b/ld/testsuite/ld-elf/pr26590b.c
new file mode 100644
index 00000000000..da546be79f5
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26590b.c
@@ -0,0 +1,10 @@
+int select (void) { return 2; }
+
+extern int f1 (int);
+
+int f2 (int x)
+{
+  if (x > 0)
+    return x * f1 (x - 1);
+  return 22222;
+}
diff --git a/ld/testsuite/ld-elf/pr26590c.c b/ld/testsuite/ld-elf/pr26590c.c
new file mode 100644
index 00000000000..930cf231ef7
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26590c.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+extern int select ();
+extern int f2 (int);
+
+int main (void)
+{
+  if (select () == 0 && f2 (0) == 22222)
+    printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/pr26590d.c b/ld/testsuite/ld-elf/pr26590d.c
new file mode 100644
index 00000000000..cfb27cf38a9
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26590d.c
@@ -0,0 +1 @@
+int select (void) { return 0; }
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index 6f53f9c3170..bc68bb8435c 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -809,6 +809,56 @@ run_cc_link_tests [list \
 	{} \
 	"libpr2404b.a" \
     ] \
+    [list \
+	"Build libpr26590a.so" \
+	"-shared" \
+	"-fPIC" \
+	{pr26590a.c} \
+	{} \
+	"libpr26590a.so" \
+    ] \
+    [list \
+	"Build libpr26590b.so (1)" \
+	"-shared" \
+	"-fPIC" \
+	{pr26590b.c} \
+	{} \
+	"libpr26590b.so" \
+    ] \
+    [list \
+	"Build pr26590c.o and pr26590d.o" \
+	"" \
+	"" \
+	{pr26590c.c pr26590d.c} \
+	{} \
+    ] \
+    [list \
+	"Build pr26590 (1)" \
+	"tmpdir/pr26590c.o tmpdir/pr26590d.o \
+	 -Wl,--as-needed tmpdir/libpr26590a.so tmpdir/libpr26590b.so" \
+	"" \
+	{dummy.c} \
+	{{error_output pr26590.err}} \
+	"pr26590" \
+    ] \
+    [list \
+	"Build libpr26590b.so (2)" \
+	"-shared -Wl,--no-as-needed \
+	 tmpdir/libpr26590a.so" \
+	"-fPIC" \
+	{pr26590b.c} \
+	{} \
+	"libpr26590b.so" \
+    ] \
+    [list \
+	"Build pr26590 (2)" \
+	"tmpdir/pr26590c.o tmpdir/pr26590d.o \
+	 -Wl,--as-needed tmpdir/libpr26590a.so tmpdir/libpr26590b.so" \
+	"" \
+	{dummy.c} \
+	{} \
+	"pr26590" \
+    ] \
 ]
 
 # pr19073.s uses .set, which has a different meaning on alpha.
@@ -1055,6 +1105,10 @@ set run_tests [list \
      "" "" \
      {pr26580-a.c} "pr26580-4" "pr26580-4.out" "-fcommon" "c" "" \
      "-Wl,--no-as-needed tmpdir/libpr26580-2.so" ] \
+    [list "Run pr26590 (2)" \
+     "" "" \
+     {pr26590c.c pr26590d.c} "pr26590" "pass.out" "" "c" "" \
+     "-Wl,--as-needed tmpdir/libpr26590a.so tmpdir/libpr26590b.so" ] \
 ]
 
 # NetBSD ELF systems do not currently support the .*_array sections.


More information about the Binutils-cvs mailing list