public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] x86: Ignore protected visibility in shared libraries on Solaris
@ 2022-08-23 17:34 H.J. Lu
  2022-08-24  0:16 ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: H.J. Lu @ 2022-08-23 17:34 UTC (permalink / raw)
  To: binutils

On x86, the PLT entry in executable may be used as function address for
functions in shared libraries.  If functions are protected, the function
address used in executable can be different from the function address
used in shared library.  This will lead to incorrect run-time behavior
if function pointer equality is needed.  By default, x86 linker issues
an error in this case.

On Solaris, linker issued an error for

struct tm *tb = (kind == CPP_time_kind::FIXED ? gmtime : localtime) (&tt);

where gmtime is a protected function in libc.so.  Use gmtime's PLT entry
in executable as function address is safe since function pointer equality
isn't needed.  Ignore protected visibility in shared libraries on Solaris
to disable linker error.  If function pointer equality is needed, linker
will silently generate executable with incorrect run-time behavior on
Solaris.

	PR ld/29512
	* elf32-i386.c (elf_i386_scan_relocs): Ignore protected
	visibility in shared libraries on Solaris.
	* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
---
 bfd/elf32-i386.c   | 3 ++-
 bfd/elf64-x86-64.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 52b1db44546..9717e2c5ed6 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -1808,7 +1808,8 @@ elf_i386_scan_relocs (bfd *abfd,
 		      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
 		    h->plt.refcount = 1;
 
-		  if (h->pointer_equality_needed
+		  if (htab->elf.target_os != is_solaris
+		      && h->pointer_equality_needed
 		      && h->type == STT_FUNC
 		      && eh->def_protected
 		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 62a9a22317a..f3b54400013 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -2251,7 +2251,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
 		      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
 		    h->plt.refcount = 1;
 
-		  if (h->pointer_equality_needed
+		  if (htab->elf.target_os != is_solaris
+		      && h->pointer_equality_needed
 		      && h->type == STT_FUNC
 		      && eh->def_protected
 		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
-- 
2.37.2


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

* Re: [PATCH] x86: Ignore protected visibility in shared libraries on Solaris
  2022-08-23 17:34 [PATCH] x86: Ignore protected visibility in shared libraries on Solaris H.J. Lu
@ 2022-08-24  0:16 ` Alan Modra
  2022-08-24  1:40   ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Modra @ 2022-08-24  0:16 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Tue, Aug 23, 2022 at 10:34:32AM -0700, H.J. Lu via Binutils wrote:
> On x86, the PLT entry in executable may be used as function address for
> functions in shared libraries.  If functions are protected, the function
> address used in executable can be different from the function address
> used in shared library.  This will lead to incorrect run-time behavior
> if function pointer equality is needed.  By default, x86 linker issues
> an error in this case.
> 
> On Solaris, linker issued an error for
> 
> struct tm *tb = (kind == CPP_time_kind::FIXED ? gmtime : localtime) (&tt);
> 
> where gmtime is a protected function in libc.so.  Use gmtime's PLT entry
> in executable as function address is safe since function pointer equality
> isn't needed.

I'm curious as to how pointer_equality_needed came to be set for
gmtime.

>  Ignore protected visibility in shared libraries on Solaris
> to disable linker error.  If function pointer equality is needed, linker
> will silently generate executable with incorrect run-time behavior on
> Solaris.
> 
> 	PR ld/29512
> 	* elf32-i386.c (elf_i386_scan_relocs): Ignore protected
> 	visibility in shared libraries on Solaris.
> 	* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
> ---
>  bfd/elf32-i386.c   | 3 ++-
>  bfd/elf64-x86-64.c | 3 ++-
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
> index 52b1db44546..9717e2c5ed6 100644
> --- a/bfd/elf32-i386.c
> +++ b/bfd/elf32-i386.c
> @@ -1808,7 +1808,8 @@ elf_i386_scan_relocs (bfd *abfd,
>  		      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
>  		    h->plt.refcount = 1;
>  
> -		  if (h->pointer_equality_needed
> +		  if (htab->elf.target_os != is_solaris
> +		      && h->pointer_equality_needed
>  		      && h->type == STT_FUNC
>  		      && eh->def_protected
>  		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
> diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
> index 62a9a22317a..f3b54400013 100644
> --- a/bfd/elf64-x86-64.c
> +++ b/bfd/elf64-x86-64.c
> @@ -2251,7 +2251,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
>  		      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
>  		    h->plt.refcount = 1;
>  
> -		  if (h->pointer_equality_needed
> +		  if (htab->elf.target_os != is_solaris
> +		      && h->pointer_equality_needed
>  		      && h->type == STT_FUNC
>  		      && eh->def_protected
>  		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
> -- 
> 2.37.2

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] x86: Ignore protected visibility in shared libraries on Solaris
  2022-08-24  0:16 ` Alan Modra
@ 2022-08-24  1:40   ` Alan Modra
  2022-08-24 14:36     ` H.J. Lu
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Modra @ 2022-08-24  1:40 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Wed, Aug 24, 2022 at 09:46:32AM +0930, Alan Modra wrote:
> On Tue, Aug 23, 2022 at 10:34:32AM -0700, H.J. Lu via Binutils wrote:
> > On x86, the PLT entry in executable may be used as function address for
> > functions in shared libraries.  If functions are protected, the function
> > address used in executable can be different from the function address
> > used in shared library.  This will lead to incorrect run-time behavior
> > if function pointer equality is needed.  By default, x86 linker issues
> > an error in this case.
> > 
> > On Solaris, linker issued an error for
> > 
> > struct tm *tb = (kind == CPP_time_kind::FIXED ? gmtime : localtime) (&tt);
> > 
> > where gmtime is a protected function in libc.so.  Use gmtime's PLT entry
> > in executable as function address is safe since function pointer equality
> > isn't needed.
> 
> I'm curious as to how pointer_equality_needed came to be set for
> gmtime.

Hmm, I figure it was -fno-PIC code with a R_X86_64_32 or R_X86_64_64
referencing gmtime.  And yes, -fno-PIC -mcmodel=medium will generate
R_X86_64_32 in a testcase like

extern int f1 (int);
extern int f2 (int);
int foo (int what, int val) { return (what ? f1 : f2) (val); }

So why exclude R_X86_64_32 with an ABI_64_P test before setting
func_pointer_ref in elf_x86_64_scan_relocs?

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] x86: Ignore protected visibility in shared libraries on Solaris
  2022-08-24  1:40   ` Alan Modra
@ 2022-08-24 14:36     ` H.J. Lu
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2022-08-24 14:36 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

On Tue, Aug 23, 2022 at 6:41 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Aug 24, 2022 at 09:46:32AM +0930, Alan Modra wrote:
> > On Tue, Aug 23, 2022 at 10:34:32AM -0700, H.J. Lu via Binutils wrote:
> > > On x86, the PLT entry in executable may be used as function address for
> > > functions in shared libraries.  If functions are protected, the function
> > > address used in executable can be different from the function address
> > > used in shared library.  This will lead to incorrect run-time behavior
> > > if function pointer equality is needed.  By default, x86 linker issues
> > > an error in this case.
> > >
> > > On Solaris, linker issued an error for
> > >
> > > struct tm *tb = (kind == CPP_time_kind::FIXED ? gmtime : localtime) (&tt);
> > >
> > > where gmtime is a protected function in libc.so.  Use gmtime's PLT entry
> > > in executable as function address is safe since function pointer equality
> > > isn't needed.
> >
> > I'm curious as to how pointer_equality_needed came to be set for
> > gmtime.
>
> Hmm, I figure it was -fno-PIC code with a R_X86_64_32 or R_X86_64_64
> referencing gmtime.  And yes, -fno-PIC -mcmodel=medium will generate
> R_X86_64_32 in a testcase like
>
> extern int f1 (int);
> extern int f2 (int);
> int foo (int what, int val) { return (what ? f1 : f2) (val); }
>
> So why exclude R_X86_64_32 with an ABI_64_P test before setting
> func_pointer_ref in elf_x86_64_scan_relocs?

The whole condition is

                 /* At run-time, R_X86_64_64 can be resolved for both
                     x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
                     can only be resolved for x32.  Function pointer
                     reference doesn't need PLT for pointer equality.  */
                  if ((sec->flags & SEC_READONLY) == 0
                      && (r_type == R_X86_64_64
                          || (!ABI_64_P (abfd)
                              && (r_type == R_X86_64_32
                                  || r_type == R_X86_64_32S))))
                    func_pointer_ref = true;

This is for relocations in data section (not readonly).   The function
pointer reference must use R_X86_64_64 if ABI_64_P is true.

-- 
H.J.

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

end of thread, other threads:[~2022-08-24 14:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-23 17:34 [PATCH] x86: Ignore protected visibility in shared libraries on Solaris H.J. Lu
2022-08-24  0:16 ` Alan Modra
2022-08-24  1:40   ` Alan Modra
2022-08-24 14:36     ` 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).