public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] ld: Ignore the new weak definition if needed
@ 2020-07-20 13:34 H.J. Lu
  2020-07-20 23:16 ` Alan Modra
  0 siblings, 1 reply; 8+ messages in thread
From: H.J. Lu @ 2020-07-20 13:34 UTC (permalink / raw)
  To: binutils

Ignore the new weak definition if the old definition comes from the LTO
IR object since plugin_notice will turn it into undefweak.

bfd/

	PR ld/26262
	* elflink.c (_bfd_elf_merge_symbol): Ignore the new weak
	definition if the old definition comes from the LTO IR object.

ld/

	PR ld/26262
	* testsuite/ld-plugin/lto.exp: Run PR ld/26262 test.
	* testsuite/ld-plugin/pr26262a.c: New file.
	* testsuite/ld-plugin/pr26262b.c: Likewise.
	* testsuite/ld-plugin/pr26262c.c: Likewise.
	* testsuite/ld-plugin/pr26262d.c: Likewise.
---
 bfd/elflink.c                     | 12 ++++++++++++
 ld/testsuite/ld-plugin/lto.exp    | 11 +++++++++++
 ld/testsuite/ld-plugin/pr26262a.c | 17 +++++++++++++++++
 ld/testsuite/ld-plugin/pr26262b.c |  5 +++++
 ld/testsuite/ld-plugin/pr26262c.c | 16 ++++++++++++++++
 ld/testsuite/ld-plugin/pr26262d.c |  6 ++++++
 6 files changed, 67 insertions(+)
 create mode 100644 ld/testsuite/ld-plugin/pr26262a.c
 create mode 100644 ld/testsuite/ld-plugin/pr26262b.c
 create mode 100644 ld/testsuite/ld-plugin/pr26262c.c
 create mode 100644 ld/testsuite/ld-plugin/pr26262d.c

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 286fc11250..15d25d52d9 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1269,6 +1269,18 @@ _bfd_elf_merge_symbol (bfd *abfd,
 	    && h->root.type != bfd_link_hash_undefweak
 	    && h->root.type != bfd_link_hash_common);
 
+  /* NB: Ignore the new weak definition if the old definition comes
+     from the LTO IR object since plugin_notice will turn it into
+     undefweak.  */
+  if (olddef
+      && oldbfd
+      && (oldbfd->flags & BFD_PLUGIN) != 0
+      && newweak)
+    {
+      *skip = TRUE;
+      return TRUE;
+    }
+
   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
      respectively, appear to be a function.  */
 
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index 2d6ca6888b..3b9f8d4c5d 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -384,6 +384,12 @@ set lto_link_elf_tests [list \
    "-shared -Wl,--exclude-libs,ALL tmpdir/pr25618a.o tmpdir/pr25618.a" \
    "-fpic" \
    {dummy.c} {{readelf {--dyn-syms --wide} pr25618.d}} "pr25618.so" "c++"] \
+  [list "Build pr26262c.o" \
+   "" "-O2 -fpic" \
+   {pr26262c.c} {} "" "c"] \
+  [list "Build pr26262d.o" \
+   "" "-O2 -fpic" \
+   {pr26262d.c} {} "" "c"] \
 ]
 
 # PR 14918 checks that libgcc is not spuriously included in a shared link of
@@ -510,6 +516,11 @@ set lto_run_tests [list \
    {pr26163b.c} "pr24406-2" "pass.out" \
    "-flto -O2" "c" "" \
    "tmpdir/pr26163a.o -Wl,--defsym,g=real_g"] \
+  [list "Run pr26262" \
+   "-O2 -flto" "" \
+   {pr26262a.c pr26262b.c} "pr26262" "pass.out" \
+   "-flto -O2" "c" "" \
+   "tmpdir/pr26262c.o tmpdir/pr26262d.o"] \
 ]
 
 if { [at_least_gcc_version 4 7] } {
diff --git a/ld/testsuite/ld-plugin/pr26262a.c b/ld/testsuite/ld-plugin/pr26262a.c
new file mode 100644
index 0000000000..b1867cd578
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262a.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int counter;
+extern void foo (void);
+extern void bar (void);
+extern void xxx (void);
+
+int
+main(void)
+{
+  bar ();
+  foo ();
+  xxx ();
+  if (counter == 1)
+    printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr26262b.c b/ld/testsuite/ld-plugin/pr26262b.c
new file mode 100644
index 0000000000..177e69c01e
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262b.c
@@ -0,0 +1,5 @@
+__attribute__ ((noinline))
+void
+bar (void)
+{
+}
diff --git a/ld/testsuite/ld-plugin/pr26262c.c b/ld/testsuite/ld-plugin/pr26262c.c
new file mode 100644
index 0000000000..91ec492ac7
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262c.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+
+extern int counter;
+
+void
+foo (void)
+{
+  counter++;
+}
+
+__attribute__((weak))
+void
+bar (void)
+{
+  abort ();
+}
diff --git a/ld/testsuite/ld-plugin/pr26262d.c b/ld/testsuite/ld-plugin/pr26262d.c
new file mode 100644
index 0000000000..fcc4941125
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262d.c
@@ -0,0 +1,6 @@
+extern void bar (void);
+void
+xxx (void)
+{
+  bar ();
+}
-- 
2.26.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Ignore the new weak definition if needed
  2020-07-20 13:34 [PATCH] ld: Ignore the new weak definition if needed H.J. Lu
@ 2020-07-20 23:16 ` Alan Modra
  2020-07-20 23:22   ` H.J. Lu
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Modra @ 2020-07-20 23:16 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Mon, Jul 20, 2020 at 06:34:29AM -0700, H.J. Lu via Binutils wrote:
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -1269,6 +1269,18 @@ _bfd_elf_merge_symbol (bfd *abfd,
>  	    && h->root.type != bfd_link_hash_undefweak
>  	    && h->root.type != bfd_link_hash_common);
>  
> +  /* NB: Ignore the new weak definition if the old definition comes
> +     from the LTO IR object since plugin_notice will turn it into
> +     undefweak.  */
> +  if (olddef
> +      && oldbfd
> +      && (oldbfd->flags & BFD_PLUGIN) != 0
> +      && newweak)
> +    {
> +      *skip = TRUE;
> +      return TRUE;
> +    }
> +
>    /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
>       respectively, appear to be a function.  */
>  

How do you square this with later code in _bfd_elf_merge_symbol?
See PR12696.

  /* Skip weak definitions of symbols that are already defined.  */
  if (newdef && olddef && newweak)
    {
      /* Don't skip new non-IR weak syms.  */
      if (!(oldbfd != NULL
	    && (oldbfd->flags & BFD_PLUGIN) != 0
	    && (abfd->flags & BFD_PLUGIN) == 0))
	{
	  newdef = FALSE;
	  *skip = TRUE;
	}


-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Ignore the new weak definition if needed
  2020-07-20 23:16 ` Alan Modra
@ 2020-07-20 23:22   ` H.J. Lu
  2020-07-21 13:37     ` [PATCH] ld: Properly override the IR definition H.J. Lu
  0 siblings, 1 reply; 8+ messages in thread
From: H.J. Lu @ 2020-07-20 23:22 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

On Mon, Jul 20, 2020 at 4:17 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Mon, Jul 20, 2020 at 06:34:29AM -0700, H.J. Lu via Binutils wrote:
> > --- a/bfd/elflink.c
> > +++ b/bfd/elflink.c
> > @@ -1269,6 +1269,18 @@ _bfd_elf_merge_symbol (bfd *abfd,
> >           && h->root.type != bfd_link_hash_undefweak
> >           && h->root.type != bfd_link_hash_common);
> >
> > +  /* NB: Ignore the new weak definition if the old definition comes
> > +     from the LTO IR object since plugin_notice will turn it into
> > +     undefweak.  */
> > +  if (olddef
> > +      && oldbfd
> > +      && (oldbfd->flags & BFD_PLUGIN) != 0
> > +      && newweak)
> > +    {
> > +      *skip = TRUE;
> > +      return TRUE;
> > +    }
> > +
> >    /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
> >       respectively, appear to be a function.  */
> >
>
> How do you square this with later code in _bfd_elf_merge_symbol?
> See PR12696.
>
>   /* Skip weak definitions of symbols that are already defined.  */
>   if (newdef && olddef && newweak)
>     {
>       /* Don't skip new non-IR weak syms.  */
>       if (!(oldbfd != NULL
>             && (oldbfd->flags & BFD_PLUGIN) != 0
>             && (abfd->flags & BFD_PLUGIN) == 0))
>         {
>           newdef = FALSE;
>           *skip = TRUE;
>         }
>

My patch is wrong.


-- 
H.J.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] ld: Properly override the IR definition
  2020-07-20 23:22   ` H.J. Lu
@ 2020-07-21 13:37     ` H.J. Lu
  2020-07-22  1:01       ` Alan Modra
  0 siblings, 1 reply; 8+ messages in thread
From: H.J. Lu @ 2020-07-21 13:37 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 1737 bytes --]

On Mon, Jul 20, 2020 at 4:22 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Mon, Jul 20, 2020 at 4:17 PM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Mon, Jul 20, 2020 at 06:34:29AM -0700, H.J. Lu via Binutils wrote:
> > > --- a/bfd/elflink.c
> > > +++ b/bfd/elflink.c
> > > @@ -1269,6 +1269,18 @@ _bfd_elf_merge_symbol (bfd *abfd,
> > >           && h->root.type != bfd_link_hash_undefweak
> > >           && h->root.type != bfd_link_hash_common);
> > >
> > > +  /* NB: Ignore the new weak definition if the old definition comes
> > > +     from the LTO IR object since plugin_notice will turn it into
> > > +     undefweak.  */
> > > +  if (olddef
> > > +      && oldbfd
> > > +      && (oldbfd->flags & BFD_PLUGIN) != 0
> > > +      && newweak)
> > > +    {
> > > +      *skip = TRUE;
> > > +      return TRUE;
> > > +    }
> > > +
> > >    /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
> > >       respectively, appear to be a function.  */
> > >
> >
> > How do you square this with later code in _bfd_elf_merge_symbol?
> > See PR12696.
> >
> >   /* Skip weak definitions of symbols that are already defined.  */
> >   if (newdef && olddef && newweak)
> >     {
> >       /* Don't skip new non-IR weak syms.  */
> >       if (!(oldbfd != NULL
> >             && (oldbfd->flags & BFD_PLUGIN) != 0
> >             && (abfd->flags & BFD_PLUGIN) == 0))
> >         {
> >           newdef = FALSE;
> >           *skip = TRUE;
> >         }
> >
>

After all LTO symbols have been read, a new definition in real object
overrides the previous definition in the IR object.  Before all LTO
symbols have been read, a new non-weak definition overrides the weak
definition in the IR object.

OK for master?

Thanks.

-- 
H.J.

[-- Attachment #2: 0001-ld-Properly-override-the-IR-definition.patch --]
[-- Type: application/x-patch, Size: 8723 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Properly override the IR definition
  2020-07-21 13:37     ` [PATCH] ld: Properly override the IR definition H.J. Lu
@ 2020-07-22  1:01       ` Alan Modra
  2020-07-22  1:50         ` H.J. Lu
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Modra @ 2020-07-22  1:01 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On Tue, Jul 21, 2020 at 06:37:31AM -0700, H.J. Lu wrote:
> After all LTO symbols have been read, a new definition in real object
> overrides the previous definition in the IR object.  Before all LTO
> symbols have been read, a new non-weak definition overrides the weak
> definition in the IR object.
> 
> OK for master?

diff --git a/ld/plugin.c b/ld/plugin.c
index b455af6d67..ff9f00e452 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -1433,12 +1433,20 @@ plugin_notice (struct bfd_link_info *info,
 	 new value from a real BFD.  Weak symbols are not normally
 	 overridden by a new weak definition, and strong symbols
 	 will normally cause multiple definition errors.  Avoid
-	 this by making the symbol appear to be undefined.  */
-      else if (((h->type == bfd_link_hash_defweak
-		 || h->type == bfd_link_hash_defined)
-		&& is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
-	       || (h->type == bfd_link_hash_common
-		   && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
+	 this by making the symbol appear to be undefined.
+
+	 NB: After all LTO symbols have been read, a new definition in
+	 real object overrides the previous definition in the IR object.
+	 Before all LTO symbols have been read, a new non-weak definition
+	 overrides the weak definition in the IR object.  */
+      else if ((((h->type == bfd_link_hash_defweak
+		  || h->type == bfd_link_hash_defined)
+		 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
+		|| (h->type == bfd_link_hash_common
+		    && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
+	       && (info->lto_all_symbols_read
+		   || ((flags & BSF_WEAK) == 0
+		       && h->type == bfd_link_hash_defweak)))
 	{
 	  h->type = bfd_link_hash_undefweak;
 	  h->u.undef.abfd = sym_bfd;

Can't this just be the following, since non-weak will override weak
without any changes to h->type here?

      else if ((((h->type == bfd_link_hash_defweak
		  || h->type == bfd_link_hash_defined)
		 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
		|| (h->type == bfd_link_hash_common
		    && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
	       && info->lto_all_symbols_read)

OK with that simplification, if you have tested your patch with a gcc
regression test.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Properly override the IR definition
  2020-07-22  1:01       ` Alan Modra
@ 2020-07-22  1:50         ` H.J. Lu
  2020-07-22 12:31           ` H.J. Lu
  0 siblings, 1 reply; 8+ messages in thread
From: H.J. Lu @ 2020-07-22  1:50 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 2800 bytes --]

On Tue, Jul 21, 2020 at 6:01 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Tue, Jul 21, 2020 at 06:37:31AM -0700, H.J. Lu wrote:
> > After all LTO symbols have been read, a new definition in real object
> > overrides the previous definition in the IR object.  Before all LTO
> > symbols have been read, a new non-weak definition overrides the weak
> > definition in the IR object.
> >
> > OK for master?
>
> diff --git a/ld/plugin.c b/ld/plugin.c
> index b455af6d67..ff9f00e452 100644
> --- a/ld/plugin.c
> +++ b/ld/plugin.c
> @@ -1433,12 +1433,20 @@ plugin_notice (struct bfd_link_info *info,
>          new value from a real BFD.  Weak symbols are not normally
>          overridden by a new weak definition, and strong symbols
>          will normally cause multiple definition errors.  Avoid
> -        this by making the symbol appear to be undefined.  */
> -      else if (((h->type == bfd_link_hash_defweak
> -                || h->type == bfd_link_hash_defined)
> -               && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
> -              || (h->type == bfd_link_hash_common
> -                  && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
> +        this by making the symbol appear to be undefined.
> +
> +        NB: After all LTO symbols have been read, a new definition in
> +        real object overrides the previous definition in the IR object.
> +        Before all LTO symbols have been read, a new non-weak definition
> +        overrides the weak definition in the IR object.  */
> +      else if ((((h->type == bfd_link_hash_defweak
> +                 || h->type == bfd_link_hash_defined)
> +                && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
> +               || (h->type == bfd_link_hash_common
> +                   && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
> +              && (info->lto_all_symbols_read
> +                  || ((flags & BSF_WEAK) == 0
> +                      && h->type == bfd_link_hash_defweak)))
>         {
>           h->type = bfd_link_hash_undefweak;
>           h->u.undef.abfd = sym_bfd;
>
> Can't this just be the following, since non-weak will override weak
> without any changes to h->type here?
>
>       else if ((((h->type == bfd_link_hash_defweak
>                   || h->type == bfd_link_hash_defined)
>                  && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
>                 || (h->type == bfd_link_hash_common
>                     && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
>                && info->lto_all_symbols_read)
>
> OK with that simplification, if you have tested your patch with a gcc
> regression test.

I am testing this patch with GCC regression test.  I will check it in if there
are no regressions.

Thanks.

-- 
H.J.

[-- Attachment #2: 0001-ld-Properly-override-the-IR-definition.patch --]
[-- Type: text/x-patch, Size: 8386 bytes --]

From 11688640775a271feef9acbe3997c593720f602a Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 20 Jul 2020 06:00:22 -0700
Subject: [PATCH] ld: Properly override the IR definition

We change the previous definition in the IR object to undefweak only
after all LTO symbols have been read.

include/

	PR ld/26262
	PR ld/26267
	* bfdlink.h (bfd_link_info): Add lto_all_symbols_read.

ld/

	PR ld/26262
	PR ld/26267
	* ldlang.c (lang_process): Set lto_all_symbols_read after all
	LTO IR symbols have been read.
	* plugin.c (plugin_notice): Override the IR definition only if
	all LTO IR symbols have been read or the new definition is
	non-weak and the the IR definition is weak
	* testsuite/ld-plugin/lto.exp: Run PR ld/26262 and ld/26267
	tests.
	* testsuite/ld-plugin/pr26262a.c: New file.
	* testsuite/ld-plugin/pr26262b.c: Likewise.
	* testsuite/ld-plugin/pr26262c.c: Likewise.
	* testsuite/ld-plugin/pr26267.err: Likewise.
	* testsuite/ld-plugin/pr26267a.c: Likewise.
	* testsuite/ld-plugin/pr26267b.c: Likewise.
	* testsuite/ld-plugin/pr26267c.c: Likewise.
---
 include/bfdlink.h                  |  3 +++
 ld/ldlang.c                        |  1 +
 ld/plugin.c                        | 16 +++++++-----
 ld/testsuite/ld-plugin/lto.exp     | 40 ++++++++++++++++++++++++++++++
 ld/testsuite/ld-plugin/pr26262a.c  | 21 ++++++++++++++++
 ld/testsuite/ld-plugin/pr26262b.c  | 16 ++++++++++++
 ld/testsuite/ld-plugin/pr26262c.c  |  6 +++++
 ld/testsuite/ld-plugin/pr26267.err |  3 +++
 ld/testsuite/ld-plugin/pr26267a.c  | 21 ++++++++++++++++
 ld/testsuite/ld-plugin/pr26267b.c  | 15 +++++++++++
 ld/testsuite/ld-plugin/pr26267c.c  |  6 +++++
 11 files changed, 142 insertions(+), 6 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pr26262a.c
 create mode 100644 ld/testsuite/ld-plugin/pr26262b.c
 create mode 100644 ld/testsuite/ld-plugin/pr26262c.c
 create mode 100644 ld/testsuite/ld-plugin/pr26267.err
 create mode 100644 ld/testsuite/ld-plugin/pr26267a.c
 create mode 100644 ld/testsuite/ld-plugin/pr26267b.c
 create mode 100644 ld/testsuite/ld-plugin/pr26267c.c

diff --git a/include/bfdlink.h b/include/bfdlink.h
index 7163433383..3badfbdb19 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -361,6 +361,9 @@ struct bfd_link_info
   /* TRUE if the LTO plugin is active.  */
   unsigned int lto_plugin_active: 1;
 
+  /* TRUE if all LTO IR symbols have been read.  */
+  unsigned int lto_all_symbols_read : 1;
+
   /* TRUE if global symbols in discarded sections should be stripped.  */
   unsigned int strip_discarded: 1;
 
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 2b3a5f9069..d3ed5d461e 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -7886,6 +7886,7 @@ lang_process (void)
       if (plugin_call_all_symbols_read ())
 	einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
 	       plugin_error_plugin ());
+      link_info.lto_all_symbols_read = TRUE;
       /* Open any newly added files, updating the file chains.  */
       plugin_undefs = link_info.hash->undefs_tail;
       open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
diff --git a/ld/plugin.c b/ld/plugin.c
index b455af6d67..d709ee10fe 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -1433,12 +1433,16 @@ plugin_notice (struct bfd_link_info *info,
 	 new value from a real BFD.  Weak symbols are not normally
 	 overridden by a new weak definition, and strong symbols
 	 will normally cause multiple definition errors.  Avoid
-	 this by making the symbol appear to be undefined.  */
-      else if (((h->type == bfd_link_hash_defweak
-		 || h->type == bfd_link_hash_defined)
-		&& is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
-	       || (h->type == bfd_link_hash_common
-		   && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
+	 this by making the symbol appear to be undefined.
+
+	 NB: We change the previous definition in the IR object to
+	 undefweak only after all LTO symbols have been read.  */
+      else if (info->lto_all_symbols_read
+	       && (((h->type == bfd_link_hash_defweak
+		     || h->type == bfd_link_hash_defined)
+		    && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
+		   || (h->type == bfd_link_hash_common
+		       && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))))
 	{
 	  h->type = bfd_link_hash_undefweak;
 	  h->u.undef.abfd = sym_bfd;
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index 2d6ca6888b..a44b6cf4b0 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -210,6 +210,36 @@ set lto_link_tests [list \
   [list "Build pr26163a.o" \
    "" "-O2 -fno-lto" \
    {pr26163a.c}] \
+  [list "Build pr26262b.o" \
+   "" "-O2" \
+   {pr26262b.c} {} "" "c"] \
+  [list "Build pr26262c.o" \
+   "" "-O2" \
+   {pr26262c.c} {} "" "c"] \
+  [list "Build pr26267a.o" \
+   "" "-O2 -flto $lto_no_fat" \
+   {pr26267a.c} {} "" "c"] \
+  [list "Build pr26267b.o" \
+   "" "-O2" \
+   {pr26267b.c} {} "" "c"] \
+  [list "Build pr26267c.o" \
+   "" "-O2" \
+   {pr26267c.c} {} "" "c"] \
+  [list "Build pr26267a" \
+   "" "-O2" \
+   {pr26267a.c} {} "" "c"] \
+  [list "Build pr26267a" \
+   "-flto tmpdir/pr26267a.o tmpdir/pr26267b.o tmpdir/pr26267c.o" \
+   "-flto $lto_no_fat" \
+   {dummy.c} \
+   {{error_output "pr26267.err"}} \
+   "pr26267a"] \
+  [list "Build pr26267b" \
+   "-flto tmpdir/pr26267b.o tmpdir/pr26267c.o tmpdir/pr26267a.o" \
+   "-flto $lto_no_fat" \
+   {dummy.c} \
+   {{error_output "pr26267.err"}} \
+   "pr26267b"] \
 ]
 
 if { [at_least_gcc_version 10 0] } {
@@ -510,6 +540,16 @@ set lto_run_tests [list \
    {pr26163b.c} "pr24406-2" "pass.out" \
    "-flto -O2" "c" "" \
    "tmpdir/pr26163a.o -Wl,--defsym,g=real_g"] \
+  [list "Run pr26262a" \
+   "-O2 -flto" "" \
+   {pr26262a.c} "pr26262a" "pass.out" \
+   "-flto -O2" "c" "" \
+   "tmpdir/pr26262b.o tmpdir/pr26262c.o"] \
+  [list "Run pr26262b" \
+   "-flto -O2 tmpdir/pr26262b.o tmpdir/pr26262c.o" "" \
+   {pr26262a.c} "pr26262b" "pass.out" \
+   "-flto -O2" "c" "" \
+   ""] \
 ]
 
 if { [at_least_gcc_version 4 7] } {
diff --git a/ld/testsuite/ld-plugin/pr26262a.c b/ld/testsuite/ld-plugin/pr26262a.c
new file mode 100644
index 0000000000..488470f4dd
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262a.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+int counter;
+extern void foo (void);
+extern void xxx (void);
+
+void
+bar (void)
+{
+}
+
+int
+main(void)
+{
+  bar ();
+  foo ();
+  xxx ();
+  if (counter == 1)
+    printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr26262b.c b/ld/testsuite/ld-plugin/pr26262b.c
new file mode 100644
index 0000000000..91ec492ac7
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262b.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+
+extern int counter;
+
+void
+foo (void)
+{
+  counter++;
+}
+
+__attribute__((weak))
+void
+bar (void)
+{
+  abort ();
+}
diff --git a/ld/testsuite/ld-plugin/pr26262c.c b/ld/testsuite/ld-plugin/pr26262c.c
new file mode 100644
index 0000000000..fcc4941125
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262c.c
@@ -0,0 +1,6 @@
+extern void bar (void);
+void
+xxx (void)
+{
+  bar ();
+}
diff --git a/ld/testsuite/ld-plugin/pr26267.err b/ld/testsuite/ld-plugin/pr26267.err
new file mode 100644
index 0000000000..b1b1c3a47d
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26267.err
@@ -0,0 +1,3 @@
+#...
+.*: multiple definition of `bar'; .*
+#...
diff --git a/ld/testsuite/ld-plugin/pr26267a.c b/ld/testsuite/ld-plugin/pr26267a.c
new file mode 100644
index 0000000000..488470f4dd
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26267a.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+int counter;
+extern void foo (void);
+extern void xxx (void);
+
+void
+bar (void)
+{
+}
+
+int
+main(void)
+{
+  bar ();
+  foo ();
+  xxx ();
+  if (counter == 1)
+    printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr26267b.c b/ld/testsuite/ld-plugin/pr26267b.c
new file mode 100644
index 0000000000..f1e32e25e3
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26267b.c
@@ -0,0 +1,15 @@
+#include <stdlib.h>
+
+extern int counter;
+
+void
+foo (void)
+{
+  counter++;
+}
+
+void
+bar (void)
+{
+  abort ();
+}
diff --git a/ld/testsuite/ld-plugin/pr26267c.c b/ld/testsuite/ld-plugin/pr26267c.c
new file mode 100644
index 0000000000..fcc4941125
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26267c.c
@@ -0,0 +1,6 @@
+extern void bar (void);
+void
+xxx (void)
+{
+  bar ();
+}
-- 
2.26.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Properly override the IR definition
  2020-07-22  1:50         ` H.J. Lu
@ 2020-07-22 12:31           ` H.J. Lu
  2020-07-22 12:44             ` Nick Clifton
  0 siblings, 1 reply; 8+ messages in thread
From: H.J. Lu @ 2020-07-22 12:31 UTC (permalink / raw)
  To: Alan Modra, Nick Clifton; +Cc: Binutils

On Tue, Jul 21, 2020 at 6:50 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Tue, Jul 21, 2020 at 6:01 PM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Tue, Jul 21, 2020 at 06:37:31AM -0700, H.J. Lu wrote:
> > > After all LTO symbols have been read, a new definition in real object
> > > overrides the previous definition in the IR object.  Before all LTO
> > > symbols have been read, a new non-weak definition overrides the weak
> > > definition in the IR object.
> > >
> > > OK for master?
> >
> > diff --git a/ld/plugin.c b/ld/plugin.c
> > index b455af6d67..ff9f00e452 100644
> > --- a/ld/plugin.c
> > +++ b/ld/plugin.c
> > @@ -1433,12 +1433,20 @@ plugin_notice (struct bfd_link_info *info,
> >          new value from a real BFD.  Weak symbols are not normally
> >          overridden by a new weak definition, and strong symbols
> >          will normally cause multiple definition errors.  Avoid
> > -        this by making the symbol appear to be undefined.  */
> > -      else if (((h->type == bfd_link_hash_defweak
> > -                || h->type == bfd_link_hash_defined)
> > -               && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
> > -              || (h->type == bfd_link_hash_common
> > -                  && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
> > +        this by making the symbol appear to be undefined.
> > +
> > +        NB: After all LTO symbols have been read, a new definition in
> > +        real object overrides the previous definition in the IR object.
> > +        Before all LTO symbols have been read, a new non-weak definition
> > +        overrides the weak definition in the IR object.  */
> > +      else if ((((h->type == bfd_link_hash_defweak
> > +                 || h->type == bfd_link_hash_defined)
> > +                && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
> > +               || (h->type == bfd_link_hash_common
> > +                   && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
> > +              && (info->lto_all_symbols_read
> > +                  || ((flags & BSF_WEAK) == 0
> > +                      && h->type == bfd_link_hash_defweak)))
> >         {
> >           h->type = bfd_link_hash_undefweak;
> >           h->u.undef.abfd = sym_bfd;
> >
> > Can't this just be the following, since non-weak will override weak
> > without any changes to h->type here?
> >
> >       else if ((((h->type == bfd_link_hash_defweak
> >                   || h->type == bfd_link_hash_defined)
> >                  && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
> >                 || (h->type == bfd_link_hash_common
> >                     && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
> >                && info->lto_all_symbols_read)
> >
> > OK with that simplification, if you have tested your patch with a gcc
> > regression test.
>
> I am testing this patch with GCC regression test.  I will check it in if there
> are no regressions.
>

Hi Nick,

Is it OK for 2.35 branch?


-- 
H.J.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ld: Properly override the IR definition
  2020-07-22 12:31           ` H.J. Lu
@ 2020-07-22 12:44             ` Nick Clifton
  0 siblings, 0 replies; 8+ messages in thread
From: Nick Clifton @ 2020-07-22 12:44 UTC (permalink / raw)
  To: H.J. Lu, Alan Modra; +Cc: Binutils

Hi H.J.

> Is it OK for 2.35 branch?

Yes it is - please go ahead and apply.

Cheers
  Nick



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-07-22 12:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-20 13:34 [PATCH] ld: Ignore the new weak definition if needed H.J. Lu
2020-07-20 23:16 ` Alan Modra
2020-07-20 23:22   ` H.J. Lu
2020-07-21 13:37     ` [PATCH] ld: Properly override the IR definition H.J. Lu
2020-07-22  1:01       ` Alan Modra
2020-07-22  1:50         ` H.J. Lu
2020-07-22 12:31           ` H.J. Lu
2020-07-22 12:44             ` Nick Clifton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).