a9feee
diff -rup binutils.orig/include/bfdlink.h binutils-2.30/include/bfdlink.h
a9feee
--- binutils.orig/include/bfdlink.h	2020-11-02 10:04:27.457826514 +0000
a9feee
+++ binutils-2.30/include/bfdlink.h	2020-11-02 10:05:14.723537971 +0000
a9feee
@@ -339,6 +339,9 @@ struct bfd_link_info
a9feee
   /* TRUE if the LTO plugin is active.  */
a9feee
   unsigned int lto_plugin_active: 1;
a9feee
 
a9feee
+  /* TRUE if all LTO IR symbols have been read.  */
a9feee
+  unsigned int lto_all_symbols_read : 1;
a9feee
+
a9feee
   /* TRUE if global symbols in discarded sections should be stripped.  */
a9feee
   unsigned int strip_discarded: 1;
a9feee
 
a9feee
diff -rup binutils.orig/ld/ldlang.c binutils-2.30/ld/ldlang.c
a9feee
--- binutils.orig/ld/ldlang.c	2020-11-02 10:04:16.852890551 +0000
a9feee
+++ binutils-2.30/ld/ldlang.c	2020-11-02 10:06:12.285185841 +0000
a9feee
@@ -7162,6 +7162,7 @@ lang_process (void)
a9feee
       if (plugin_call_all_symbols_read ())
a9feee
 	einfo (_("%P%F: %s: plugin reported error after all symbols read\n"),
a9feee
 	       plugin_error_plugin ());
a9feee
+      link_info.lto_all_symbols_read = TRUE;
a9feee
       /* Open any newly added files, updating the file chains.  */
a9feee
       open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
a9feee
       /* Restore the global list pointer now they have all been added.  */
a9feee
diff -rup binutils.orig/ld/plugin.c binutils-2.30/ld/plugin.c
a9feee
--- binutils.orig/ld/plugin.c	2020-11-02 10:04:16.852890551 +0000
a9feee
+++ binutils-2.30/ld/plugin.c	2020-11-02 10:08:11.010459546 +0000
a9feee
@@ -1355,12 +1355,16 @@ plugin_notice (struct bfd_link_info *inf
a9feee
 	     new value from a real BFD.  Weak symbols are not normally
a9feee
 	     overridden by a new weak definition, and strong symbols
a9feee
 	     will normally cause multiple definition errors.  Avoid
a9feee
-	     this by making the symbol appear to be undefined.  */
a9feee
-	  if (((h->type == bfd_link_hash_defweak
a9feee
-		|| h->type == bfd_link_hash_defined)
a9feee
-	       && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
a9feee
-	      || (h->type == bfd_link_hash_common
a9feee
-		  && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
a9feee
+	     this by making the symbol appear to be undefined.
a9feee
+
a9feee
+	     NB: We change the previous definition in the IR object to
a9feee
+	     undefweak only after all LTO symbols have been read.  */
a9feee
+	  if (info->lto_all_symbols_read
a9feee
+              && (((h->type == bfd_link_hash_defweak
a9feee
+                    || h->type == bfd_link_hash_defined)
a9feee
+                   && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
a9feee
+                  || (h->type == bfd_link_hash_common
a9feee
+                      && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))))
a9feee
 	    {
a9feee
 	      h->type = bfd_link_hash_undefweak;
a9feee
 	      h->u.undef.abfd = sym_bfd;
a9feee
diff -rup binutils.orig/ld/testsuite/ld-plugin/lto.exp binutils-2.30/ld/testsuite/ld-plugin/lto.exp
a9feee
--- binutils.orig/ld/testsuite/ld-plugin/lto.exp	2020-11-02 10:04:16.926890104 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/lto.exp	2020-11-02 10:12:09.551000288 +0000
a9feee
@@ -214,6 +214,36 @@ set lto_link_tests [list \
a9feee
   [list "Build pr22502b.o" \
a9feee
    "$plug_opt" "-flto $lto_no_fat" \
a9feee
    {pr22502b.c}] \
a9feee
+  [list "Build pr26262b.o" \
a9feee
+   "" "-O2" \
a9feee
+   {pr26262b.c} {} "" "c"] \
a9feee
+  [list "Build pr26262c.o" \
a9feee
+   "" "-O2" \
a9feee
+   {pr26262c.c} {} "" "c"] \
a9feee
+  [list "Build pr26267a.o" \
a9feee
+   "" "-O2 -flto $lto_no_fat" \
a9feee
+   {pr26267a.c} {} "" "c"] \
a9feee
+  [list "Build pr26267b.o" \
a9feee
+   "" "-O2" \
a9feee
+   {pr26267b.c} {} "" "c"] \
a9feee
+  [list "Build pr26267c.o" \
a9feee
+   "" "-O2" \
a9feee
+   {pr26267c.c} {} "" "c"] \
a9feee
+  [list "Build pr26267a" \
a9feee
+   "" "-O2" \
a9feee
+   {pr26267a.c} {} "" "c"] \
a9feee
+  [list "Build pr26267a" \
a9feee
+   "-flto tmpdir/pr26267a.o tmpdir/pr26267b.o tmpdir/pr26267c.o" \
a9feee
+   "-flto $lto_no_fat" \
a9feee
+   {dummy.c} \
a9feee
+   {{error_output "pr26267.err"}} \
a9feee
+   "pr26267a"] \
a9feee
+  [list "Build pr26267b" \
a9feee
+   "-flto tmpdir/pr26267b.o tmpdir/pr26267c.o tmpdir/pr26267a.o" \
a9feee
+   "-flto $lto_no_fat" \
a9feee
+   {dummy.c} \
a9feee
+   {{error_output "pr26267.err"}} \
a9feee
+   "pr26267b"] \
a9feee
 ]
a9feee
 
a9feee
 if { [at_least_gcc_version 4 7] } {
a9feee
@@ -373,6 +403,16 @@ set lto_run_tests [list \
a9feee
   [list "Run pr22502" \
a9feee
    "-O2 -flto tmpdir/pr22502a.o tmpdir/pr22502b.o" "" \
a9feee
    {dummy.c} "pr20267" "pass.out" "-flto -O2" "c"] \
a9feee
+  [list "Run pr26262a" \
a9feee
+   "-O2 -flto" "" \
a9feee
+   {pr26262a.c} "pr26262a" "pass.out" \
a9feee
+   "-flto -O2" "c" "" \
a9feee
+   "tmpdir/pr26262b.o tmpdir/pr26262c.o"] \
a9feee
+  [list "Run pr26262b" \
a9feee
+   "-flto -O2 tmpdir/pr26262b.o tmpdir/pr26262c.o" "" \
a9feee
+   {pr26262a.c} "pr26262b" "pass.out" \
a9feee
+   "-flto -O2" "c" "" \
a9feee
+   ""] \
a9feee
 ]
a9feee
 
a9feee
 if { [at_least_gcc_version 4 7] } {
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26262a.c
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26262b.c
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26267.err
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26267a.c
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26267b.c
a9feee
Only in binutils-2.30/ld/testsuite/ld-plugin: pr26267c.c
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26262a.c	2020-11-02 10:13:16.624589913 +0000
a9feee
@@ -0,0 +1,21 @@
a9feee
+#include <stdio.h>
a9feee
+
a9feee
+int counter;
a9feee
+extern void foo (void);
a9feee
+extern void xxx (void);
a9feee
+
a9feee
+void
a9feee
+bar (void)
a9feee
+{
a9feee
+}
a9feee
+
a9feee
+int
a9feee
+main(void)
a9feee
+{
a9feee
+  bar ();
a9feee
+  foo ();
a9feee
+  xxx ();
a9feee
+  if (counter == 1)
a9feee
+    printf ("PASS\n");
a9feee
+  return 0;
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26262b.c	2020-11-02 10:13:27.358523487 +0000
a9feee
@@ -0,0 +1,16 @@
a9feee
+#include <stdlib.h>
a9feee
+
a9feee
+extern int counter;
a9feee
+
a9feee
+void
a9feee
+foo (void)
a9feee
+{
a9feee
+  counter++;
a9feee
+}
a9feee
+
a9feee
+__attribute__((weak))
a9feee
+void
a9feee
+bar (void)
a9feee
+{
a9feee
+  abort ();
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26262c.c	2020-11-02 10:47:59.031665605 +0000
a9feee
@@ -0,0 +1,6 @@
a9feee
+extern void bar (void);
a9feee
+void
a9feee
+xxx (void)
a9feee
+{
a9feee
+  bar ();
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26267c.c	2020-11-02 10:13:39.665447327 +0000
a9feee
@@ -0,0 +1,6 @@
a9feee
+extern void bar (void);
a9feee
+void
a9feee
+xxx (void)
a9feee
+{
a9feee
+  bar ();
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26267b.c	2020-11-02 10:13:43.648422679 +0000
a9feee
@@ -0,0 +1,15 @@
a9feee
+#include <stdlib.h>
a9feee
+
a9feee
+extern int counter;
a9feee
+
a9feee
+void
a9feee
+foo (void)
a9feee
+{
a9feee
+  counter++;
a9feee
+}
a9feee
+
a9feee
+void
a9feee
+bar (void)
a9feee
+{
a9feee
+  abort ();
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26267a.c	2020-11-02 10:13:47.556398495 +0000
a9feee
@@ -0,0 +1,21 @@
a9feee
+#include <stdio.h>
a9feee
+
a9feee
+int counter;
a9feee
+extern void foo (void);
a9feee
+extern void xxx (void);
a9feee
+
a9feee
+void
a9feee
+bar (void)
a9feee
+{
a9feee
+}
a9feee
+
a9feee
+int
a9feee
+main(void)
a9feee
+{
a9feee
+  bar ();
a9feee
+  foo ();
a9feee
+  xxx ();
a9feee
+  if (counter == 1)
a9feee
+    printf ("PASS\n");
a9feee
+  return 0;
a9feee
+}
a9feee
--- /dev/null	2020-11-02 08:23:19.196542384 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26267.err	2020-11-02 10:14:01.785310441 +0000
a9feee
@@ -0,0 +1,3 @@
a9feee
+#...
a9feee
+.*: multiple definition of `bar'; .*
a9feee
+#...
a9feee
diff -rup binutils.orig/ld/testsuite/ld-plugin/pr26267.err binutils-2.30/ld/testsuite/ld-plugin/pr26267.err
a9feee
--- binutils.orig/ld/testsuite/ld-plugin/pr26267.err	2020-11-02 12:51:28.751137533 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/pr26267.err	2020-11-02 13:01:38.430679516 +0000
a9feee
@@ -1,3 +1,3 @@
a9feee
 #...
a9feee
-.*: multiple definition of `bar'; .*
a9feee
+.*: multiple definition of `bar'.*
a9feee
 #...
a9feee
--- binutils.orig/ld/testsuite/ld-plugin/lto.exp	2020-11-02 12:51:28.751137533 +0000
a9feee
+++ binutils-2.30/ld/testsuite/ld-plugin/lto.exp	2020-11-02 13:10:49.531708566 +0000
a9feee
@@ -404,15 +404,13 @@ set lto_run_tests [list \
a9feee
    "-O2 -flto tmpdir/pr22502a.o tmpdir/pr22502b.o" "" \
a9feee
    {dummy.c} "pr20267" "pass.out" "-flto -O2" "c"] \
a9feee
   [list "Run pr26262a" \
a9feee
-   "-O2 -flto" "" \
a9feee
+   "-O2 -flto tmpdir/pr26262b.o tmpdir/pr26262c.o" "" \
a9feee
    {pr26262a.c} "pr26262a" "pass.out" \
a9feee
-   "-flto -O2" "c" "" \
a9feee
-   "tmpdir/pr26262b.o tmpdir/pr26262c.o"] \
a9feee
+   "-flto -O2" "c" "" ] \
a9feee
   [list "Run pr26262b" \
a9feee
    "-flto -O2 tmpdir/pr26262b.o tmpdir/pr26262c.o" "" \
a9feee
    {pr26262a.c} "pr26262b" "pass.out" \
a9feee
-   "-flto -O2" "c" "" \
a9feee
-   ""] \
a9feee
+   "-flto -O2" "c" "" ] \
a9feee
 ]
a9feee
 
a9feee
 if { [at_least_gcc_version 4 7] } {