ld: Set non_ir_ref_regular on source for assignment

Message ID 20200623221606.1543244-1-hjl.tools@gmail.com
State New
Headers show
Series
  • ld: Set non_ir_ref_regular on source for assignment
Related show

Commit Message

David Faust via Binutils June 23, 2020, 10:16 p.m.
We need to set non_ir_ref_regular on the source for assignment to get
the correct LTO resolution:

190 a27be7f4ad90c5ce PREVAILING_DEF real_g

instead of

190 30c3b2d8f967f5ea PREVAILING_DEF_IRONLY real_g

	PR ld/26163
	* ldexp.c (exp_fold_tree_1): Set non_ir_ref_regular on the source
	for assignment.
	* testsuite/ld-plugin/lto.exp: Run ld/26163 test.
	* testsuite/ld-plugin/pr26163a.c: New file.
	* testsuite/ld-plugin/pr26163b.c: Likewise.
---
 ld/ldexp.c                        | 10 +++++++---
 ld/testsuite/ld-plugin/lto.exp    |  8 ++++++++
 ld/testsuite/ld-plugin/pr26163a.c |  9 +++++++++
 ld/testsuite/ld-plugin/pr26163b.c | 19 +++++++++++++++++++
 4 files changed, 43 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pr26163a.c
 create mode 100644 ld/testsuite/ld-plugin/pr26163b.c

-- 
2.26.2

Comments

David Faust via Binutils June 24, 2020, 2:55 a.m. | #1
On Tue, Jun 23, 2020 at 03:16:06PM -0700, H.J. Lu via Binutils wrote:
> 	PR ld/26163

> 	* ldexp.c (exp_fold_tree_1): Set non_ir_ref_regular on the source

> 	for assignment.

> 	* testsuite/ld-plugin/lto.exp: Run ld/26163 test.

> 	* testsuite/ld-plugin/pr26163a.c: New file.

> 	* testsuite/ld-plugin/pr26163b.c: Likewise.


Looks good to me.

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/ld/ldexp.c b/ld/ldexp.c
index 3ffabb8c1d..b4e7c41209 100644
--- a/ld/ldexp.c
+++ b/ld/ldexp.c
@@ -1217,15 +1217,19 @@  exp_fold_tree_1 (etree_type *tree)
 			bfd_link_hide_symbol (link_info.output_bfd,
 					      &link_info, h);
 
-		      /* Copy the symbol type if this is an expression only
+		      /* Copy the symbol type and set non_ir_ref_regular
+			 on the source if this is an expression only
 			 referencing a single symbol.  (If the expression
 			 contains ternary conditions, ignoring symbols on
 			 false branches.)  */
 		      if (expld.assign_src != NULL
 			  && (expld.assign_src
 			      != (struct bfd_link_hash_entry *) -1))
-			bfd_copy_link_hash_symbol_type (link_info.output_bfd,
-							h, expld.assign_src);
+			{
+			  bfd_copy_link_hash_symbol_type (link_info.output_bfd,
+							  h, expld.assign_src);
+			  expld.assign_src->non_ir_ref_regular = TRUE;
+			}
 		    }
 		}
 	    }
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index a7b9cacfd8..5a6ba7ad83 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -207,6 +207,9 @@  set lto_link_tests [list \
   [list "Build pr24406-2b.o" \
    "" "-O2 -fno-lto" \
    {pr24406-2b.c}] \
+  [list "Build pr26163a.o" \
+   "" "-O2 -fno-lto" \
+   {pr26163a.c}] \
 ]
 
 if { [at_least_gcc_version 10 0] } {
@@ -502,6 +505,11 @@  set lto_run_tests [list \
    {pr24406-2a.c} "pr24406-2" "pass.out" \
    "-flto -O2" "c" "" \
    "tmpdir/pr24406-2b.o -Wl,--wrap=cook"] \
+  [list "Run pr26163" \
+   "-O2 -flto" "" \
+   {pr26163b.c} "pr24406-2" "pass.out" \
+   "-flto -O2" "c" "" \
+   "tmpdir/pr26163a.o -Wl,--defsym,g=real_g"] \
 ]
 
 if { [at_least_gcc_version 4 7] } {
diff --git a/ld/testsuite/ld-plugin/pr26163a.c b/ld/testsuite/ld-plugin/pr26163a.c
new file mode 100644
index 0000000000..4c9979b850
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26163a.c
@@ -0,0 +1,9 @@ 
+extern int counter;
+
+extern void g(void);
+
+void f(void)
+{
+  g();
+  counter++;
+}
diff --git a/ld/testsuite/ld-plugin/pr26163b.c b/ld/testsuite/ld-plugin/pr26163b.c
new file mode 100644
index 0000000000..5524b4e3d9
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26163b.c
@@ -0,0 +1,19 @@ 
+#include <stdio.h>
+
+int counter;
+extern void f(void);
+
+void
+real_g(void)
+{
+  counter++;
+}
+
+int main()
+{
+  real_g();
+  f();
+  if (counter == 3)
+    printf ("PASS\n");
+  return 0;
+}