public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] LTO: Handle __real_SYM reference in IR
@ 2022-04-29 17:59 H.J. Lu
  2022-05-03 16:45 ` H.J. Lu
  2022-05-04  4:12 ` Alan Modra
  0 siblings, 2 replies; 4+ messages in thread
From: H.J. Lu @ 2022-04-29 17:59 UTC (permalink / raw)
  To: binutils

When an IR symbol SYM is referenced in IR via __real_SYM, its resolution
should be LDPR_PREVAILING_DEF, not PREVAILING_DEF_IRONLY, since LTO
doesn't know that __real_SYM should be resolved by SYM.

bfd/

	PR ld/29086
	* elflink.c (_bfd_elf_merge_symbol): Mark SYM is referenced in
	IR if __real_SYM is referenced in IR.

include/

	PR ld/29086
	* bfdlink.h (bfd_link_hash_entry): Add ir_ref_real.

ld/

	PR ld/29086
	* plugin.c (get_symbols): Resolve SYM definition to
	LDPR_PREVAILING_DEF for __real_SYM reference in IR.
	* testsuite/ld-plugin/lto.exp: Run PR ld/29086 test.
	* testsuite/ld-plugin/pr29086.c: New file.
---
 bfd/elflink.c                    | 15 +++++++++++++--
 include/bfdlink.h                |  4 ++++
 ld/plugin.c                      |  7 +++++--
 ld/testsuite/ld-plugin/lto.exp   |  8 ++++++++
 ld/testsuite/ld-plugin/pr29086.c | 19 +++++++++++++++++++
 5 files changed, 49 insertions(+), 4 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pr29086.c

diff --git a/bfd/elflink.c b/bfd/elflink.c
index b54ee517c75..ced07e40524 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1101,8 +1101,19 @@ _bfd_elf_merge_symbol (bfd *abfd,
   if (! bfd_is_und_section (sec))
     h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
   else
-    h = ((struct elf_link_hash_entry *)
-	 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
+    {
+      h = ((struct elf_link_hash_entry *)
+	   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false,
+					 false));
+      /* Mark SYM is referenced in IR if __real_SYM is referenced in
+	 IR.  */
+      if (h != NULL
+	  && info->wrap_hash != NULL
+	  && (abfd->flags & BFD_PLUGIN) != 0
+	  && strcmp (h->root.root.string,
+		     name + sizeof "__real_" - 1) == 0)
+	h->root.ir_ref_real = 1;
+    }
   if (h == NULL)
     return false;
   *sym_hash = h;
diff --git a/include/bfdlink.h b/include/bfdlink.h
index b16f3f44ca5..3b8cc7f8f96 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -114,6 +114,10 @@ struct bfd_link_hash_entry
      as distinct from a LTO IR object file.  */
   unsigned int non_ir_ref_dynamic : 1;
 
+  /* The symbol, SYM, is referenced by __real_SYM in an LTO IR object
+     file.  */
+  unsigned int ir_ref_real : 1;
+
   /* Symbol is a built-in define.  These will be overridden by PROVIDE
      in a linker script.  */
   unsigned int linker_def : 1;
diff --git a/ld/plugin.c b/ld/plugin.c
index 6db52d16802..c8cdf580a67 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -863,8 +863,11 @@ get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
 	  /* We need to know if the sym is referenced from non-IR files.  Or
 	     even potentially-referenced, perhaps in a future final link if
 	     this is a partial one, perhaps dynamically at load-time if the
-	     symbol is externally visible.  Also check for wrapper symbol.  */
-	  if (blhe->non_ir_ref_regular || wrap_status == wrapper)
+	     symbol is externally visible.  Also check for __real_SYM
+	     reference and wrapper symbol.  */
+	  if (blhe->non_ir_ref_regular
+	      || blhe->ir_ref_real
+	      || wrap_status == wrapper)
 	    res = LDPR_PREVAILING_DEF;
 	  else if (wrap_status == wrapped)
 	    res = LDPR_RESOLVED_IR;
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index 33af6c45a41..fa5d2500aa3 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -522,6 +522,14 @@ set lto_link_elf_tests [list \
    {{"nm" {-D} "pr28849.d"}} \
    "pr28849" \
   ] \
+  [list \
+   "PR ld/pr29086" \
+   "-Wl,--wrap=foo" \
+   "-O0 -flto" \
+   {pr29086.c} \
+   {} \
+   "pr29086" \
+  ] \
 ]
 
 # PR 14918 checks that libgcc is not spuriously included in a shared link of
diff --git a/ld/testsuite/ld-plugin/pr29086.c b/ld/testsuite/ld-plugin/pr29086.c
new file mode 100644
index 00000000000..d333d68e262
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr29086.c
@@ -0,0 +1,19 @@
+int
+foo (void)
+{
+  return 0;
+}
+
+int
+main ()
+{
+  return foo ();
+}
+
+extern int __real_foo (void);
+
+int
+__wrap_foo (void)
+{
+  return __real_foo ();
+}
-- 
2.35.1


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

* Re: [PATCH] LTO: Handle __real_SYM reference in IR
  2022-04-29 17:59 [PATCH] LTO: Handle __real_SYM reference in IR H.J. Lu
@ 2022-05-03 16:45 ` H.J. Lu
  2022-05-04  4:12 ` Alan Modra
  1 sibling, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2022-05-03 16:45 UTC (permalink / raw)
  To: binutils, Alan Modra, Nick Clifton

On Fri, Apr 29, 2022 at 10:59 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> When an IR symbol SYM is referenced in IR via __real_SYM, its resolution
> should be LDPR_PREVAILING_DEF, not PREVAILING_DEF_IRONLY, since LTO
> doesn't know that __real_SYM should be resolved by SYM.
>
> bfd/
>
>         PR ld/29086
>         * elflink.c (_bfd_elf_merge_symbol): Mark SYM is referenced in
>         IR if __real_SYM is referenced in IR.
>
> include/
>
>         PR ld/29086
>         * bfdlink.h (bfd_link_hash_entry): Add ir_ref_real.
>
> ld/
>
>         PR ld/29086
>         * plugin.c (get_symbols): Resolve SYM definition to
>         LDPR_PREVAILING_DEF for __real_SYM reference in IR.
>         * testsuite/ld-plugin/lto.exp: Run PR ld/29086 test.
>         * testsuite/ld-plugin/pr29086.c: New file.
> ---
>  bfd/elflink.c                    | 15 +++++++++++++--
>  include/bfdlink.h                |  4 ++++
>  ld/plugin.c                      |  7 +++++--
>  ld/testsuite/ld-plugin/lto.exp   |  8 ++++++++
>  ld/testsuite/ld-plugin/pr29086.c | 19 +++++++++++++++++++
>  5 files changed, 49 insertions(+), 4 deletions(-)
>  create mode 100644 ld/testsuite/ld-plugin/pr29086.c
>
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index b54ee517c75..ced07e40524 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -1101,8 +1101,19 @@ _bfd_elf_merge_symbol (bfd *abfd,
>    if (! bfd_is_und_section (sec))
>      h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
>    else
> -    h = ((struct elf_link_hash_entry *)
> -        bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
> +    {
> +      h = ((struct elf_link_hash_entry *)
> +          bfd_wrapped_link_hash_lookup (abfd, info, name, true, false,
> +                                        false));
> +      /* Mark SYM is referenced in IR if __real_SYM is referenced in
> +        IR.  */
> +      if (h != NULL
> +         && info->wrap_hash != NULL
> +         && (abfd->flags & BFD_PLUGIN) != 0
> +         && strcmp (h->root.root.string,
> +                    name + sizeof "__real_" - 1) == 0)
> +       h->root.ir_ref_real = 1;
> +    }
>    if (h == NULL)
>      return false;
>    *sym_hash = h;
> diff --git a/include/bfdlink.h b/include/bfdlink.h
> index b16f3f44ca5..3b8cc7f8f96 100644
> --- a/include/bfdlink.h
> +++ b/include/bfdlink.h
> @@ -114,6 +114,10 @@ struct bfd_link_hash_entry
>       as distinct from a LTO IR object file.  */
>    unsigned int non_ir_ref_dynamic : 1;
>
> +  /* The symbol, SYM, is referenced by __real_SYM in an LTO IR object
> +     file.  */
> +  unsigned int ir_ref_real : 1;
> +
>    /* Symbol is a built-in define.  These will be overridden by PROVIDE
>       in a linker script.  */
>    unsigned int linker_def : 1;
> diff --git a/ld/plugin.c b/ld/plugin.c
> index 6db52d16802..c8cdf580a67 100644
> --- a/ld/plugin.c
> +++ b/ld/plugin.c
> @@ -863,8 +863,11 @@ get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
>           /* We need to know if the sym is referenced from non-IR files.  Or
>              even potentially-referenced, perhaps in a future final link if
>              this is a partial one, perhaps dynamically at load-time if the
> -            symbol is externally visible.  Also check for wrapper symbol.  */
> -         if (blhe->non_ir_ref_regular || wrap_status == wrapper)
> +            symbol is externally visible.  Also check for __real_SYM
> +            reference and wrapper symbol.  */
> +         if (blhe->non_ir_ref_regular
> +             || blhe->ir_ref_real
> +             || wrap_status == wrapper)
>             res = LDPR_PREVAILING_DEF;
>           else if (wrap_status == wrapped)
>             res = LDPR_RESOLVED_IR;
> diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
> index 33af6c45a41..fa5d2500aa3 100644
> --- a/ld/testsuite/ld-plugin/lto.exp
> +++ b/ld/testsuite/ld-plugin/lto.exp
> @@ -522,6 +522,14 @@ set lto_link_elf_tests [list \
>     {{"nm" {-D} "pr28849.d"}} \
>     "pr28849" \
>    ] \
> +  [list \
> +   "PR ld/pr29086" \
> +   "-Wl,--wrap=foo" \
> +   "-O0 -flto" \
> +   {pr29086.c} \
> +   {} \
> +   "pr29086" \
> +  ] \
>  ]
>
>  # PR 14918 checks that libgcc is not spuriously included in a shared link of
> diff --git a/ld/testsuite/ld-plugin/pr29086.c b/ld/testsuite/ld-plugin/pr29086.c
> new file mode 100644
> index 00000000000..d333d68e262
> --- /dev/null
> +++ b/ld/testsuite/ld-plugin/pr29086.c
> @@ -0,0 +1,19 @@
> +int
> +foo (void)
> +{
> +  return 0;
> +}
> +
> +int
> +main ()
> +{
> +  return foo ();
> +}
> +
> +extern int __real_foo (void);
> +
> +int
> +__wrap_foo (void)
> +{
> +  return __real_foo ();
> +}
> --
> 2.35.1
>

Hi Nick, Alan,

Can you review this?

Thanks.

-- 
H.J.

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

* Re: [PATCH] LTO: Handle __real_SYM reference in IR
  2022-04-29 17:59 [PATCH] LTO: Handle __real_SYM reference in IR H.J. Lu
  2022-05-03 16:45 ` H.J. Lu
@ 2022-05-04  4:12 ` Alan Modra
  2022-05-04 23:26   ` H.J. Lu
  1 sibling, 1 reply; 4+ messages in thread
From: Alan Modra @ 2022-05-04  4:12 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Fri, Apr 29, 2022 at 10:59:32AM -0700, H.J. Lu via Binutils wrote:
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -1101,8 +1101,19 @@ _bfd_elf_merge_symbol (bfd *abfd,
>    if (! bfd_is_und_section (sec))
>      h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
>    else
> -    h = ((struct elf_link_hash_entry *)
> -	 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
> +    {
> +      h = ((struct elf_link_hash_entry *)
> +	   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false,
> +					 false));
> +      /* Mark SYM is referenced in IR if __real_SYM is referenced in
> +	 IR.  */
> +      if (h != NULL
> +	  && info->wrap_hash != NULL
> +	  && (abfd->flags & BFD_PLUGIN) != 0
> +	  && strcmp (h->root.root.string,
> +		     name + sizeof "__real_" - 1) == 0)
> +	h->root.ir_ref_real = 1;
> +    }
>    if (h == NULL)
>      return false;
>    *sym_hash = h;

How about putting the above in linker.c:bfd_wrapped_link_hash_lookup
instead, to avoid the extra strcmp?  That seems like a better place to
me since there isn't really anything ELF specific about wrapped symbol
lookup.

Hmm, and maybe without the BFD_PLUGIN test and losing "ir" from the
flag name.

If that works for you, OK with those changes.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] LTO: Handle __real_SYM reference in IR
  2022-05-04  4:12 ` Alan Modra
@ 2022-05-04 23:26   ` H.J. Lu
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2022-05-04 23:26 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

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

On Tue, May 3, 2022 at 9:12 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Apr 29, 2022 at 10:59:32AM -0700, H.J. Lu via Binutils wrote:
> > --- a/bfd/elflink.c
> > +++ b/bfd/elflink.c
> > @@ -1101,8 +1101,19 @@ _bfd_elf_merge_symbol (bfd *abfd,
> >    if (! bfd_is_und_section (sec))
> >      h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
> >    else
> > -    h = ((struct elf_link_hash_entry *)
> > -      bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
> > +    {
> > +      h = ((struct elf_link_hash_entry *)
> > +        bfd_wrapped_link_hash_lookup (abfd, info, name, true, false,
> > +                                      false));
> > +      /* Mark SYM is referenced in IR if __real_SYM is referenced in
> > +      IR.  */
> > +      if (h != NULL
> > +       && info->wrap_hash != NULL
> > +       && (abfd->flags & BFD_PLUGIN) != 0
> > +       && strcmp (h->root.root.string,
> > +                  name + sizeof "__real_" - 1) == 0)
> > +     h->root.ir_ref_real = 1;
> > +    }
> >    if (h == NULL)
> >      return false;
> >    *sym_hash = h;
>
> How about putting the above in linker.c:bfd_wrapped_link_hash_lookup
> instead, to avoid the extra strcmp?  That seems like a better place to
> me since there isn't really anything ELF specific about wrapped symbol
> lookup.
>
> Hmm, and maybe without the BFD_PLUGIN test and losing "ir" from the
> flag name.
>
> If that works for you, OK with those changes.
>

It works.  This is what I am checking in.

Thanks.

-- 
H.J.

[-- Attachment #2: v2-0001-LTO-Handle-__real_SYM-reference-in-IR.patch --]
[-- Type: text/x-patch, Size: 3750 bytes --]

From 3463163b5b9901a15567b98837c55e05f33f6b9f Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 25 Apr 2022 10:51:39 -0700
Subject: [PATCH v2] LTO: Handle __real_SYM reference in IR

When an IR symbol SYM is referenced in IR via __real_SYM, its resolution
should be LDPR_PREVAILING_DEF, not PREVAILING_DEF_IRONLY, since LTO
doesn't know that __real_SYM should be resolved by SYM.

bfd/

	PR ld/29086
	* linker.c (bfd_wrapped_link_hash_lookup): Mark SYM is referenced
	via __real_SYM.

include/

	PR ld/29086
	* bfdlink.h (bfd_link_hash_entry): Add ref_real.

ld/

	PR ld/29086
	* plugin.c (get_symbols): Resolve SYM definition to
	LDPR_PREVAILING_DEF for __real_SYM reference.
	* testsuite/ld-plugin/lto.exp: Run PR ld/29086 test.
	* testsuite/ld-plugin/pr29086.c: New file.
---
 bfd/linker.c                     |  1 +
 include/bfdlink.h                |  3 +++
 ld/plugin.c                      |  7 +++++--
 ld/testsuite/ld-plugin/lto.exp   |  8 ++++++++
 ld/testsuite/ld-plugin/pr29086.c | 19 +++++++++++++++++++
 5 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pr29086.c

diff --git a/bfd/linker.c b/bfd/linker.c
index 088c1d06f3d..e9ebdbad040 100644
--- a/bfd/linker.c
+++ b/bfd/linker.c
@@ -599,6 +599,7 @@ bfd_wrapped_link_hash_lookup (bfd *abfd,
 	  n[1] = '\0';
 	  strcat (n, l + sizeof REAL - 1);
 	  h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
+	  h->ref_real = 1;
 	  free (n);
 	  return h;
 	}
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 88cc8e26183..27a8e11cc22 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -114,6 +114,9 @@ struct bfd_link_hash_entry
      as distinct from a LTO IR object file.  */
   unsigned int non_ir_ref_dynamic : 1;
 
+  /* The symbol, SYM, is referenced by __real_SYM in an object file.  */
+  unsigned int ref_real : 1;
+
   /* Symbol is a built-in define.  These will be overridden by PROVIDE
      in a linker script.  */
   unsigned int linker_def : 1;
diff --git a/ld/plugin.c b/ld/plugin.c
index 6db52d16802..fe203104677 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -863,8 +863,11 @@ get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
 	  /* We need to know if the sym is referenced from non-IR files.  Or
 	     even potentially-referenced, perhaps in a future final link if
 	     this is a partial one, perhaps dynamically at load-time if the
-	     symbol is externally visible.  Also check for wrapper symbol.  */
-	  if (blhe->non_ir_ref_regular || wrap_status == wrapper)
+	     symbol is externally visible.  Also check for __real_SYM
+	     reference and wrapper symbol.  */
+	  if (blhe->non_ir_ref_regular
+	      || blhe->ref_real
+	      || wrap_status == wrapper)
 	    res = LDPR_PREVAILING_DEF;
 	  else if (wrap_status == wrapped)
 	    res = LDPR_RESOLVED_IR;
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index f4ea1d48533..4e8e2dc7748 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -522,6 +522,14 @@ set lto_link_elf_tests [list \
    {{"nm" {-D} "pr28849.d"}} \
    "pr28849" \
   ] \
+  [list \
+   "PR ld/pr29086" \
+   "-Wl,--wrap=foo" \
+   "-O0 -flto" \
+   {pr29086.c} \
+   {} \
+   "pr29086" \
+  ] \
 ]
 
 # PR 14918 checks that libgcc is not spuriously included in a shared link of
diff --git a/ld/testsuite/ld-plugin/pr29086.c b/ld/testsuite/ld-plugin/pr29086.c
new file mode 100644
index 00000000000..d333d68e262
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr29086.c
@@ -0,0 +1,19 @@
+int
+foo (void)
+{
+  return 0;
+}
+
+int
+main ()
+{
+  return foo ();
+}
+
+extern int __real_foo (void);
+
+int
+__wrap_foo (void)
+{
+  return __real_foo ();
+}
-- 
2.35.1


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

end of thread, other threads:[~2022-05-04 23:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-29 17:59 [PATCH] LTO: Handle __real_SYM reference in IR H.J. Lu
2022-05-03 16:45 ` H.J. Lu
2022-05-04  4:12 ` Alan Modra
2022-05-04 23:26   ` H.J. Lu

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).