public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Allow direct access relocations referencing a protected function symbol
@ 2021-06-13 21:54 Fangrui Song
  2021-06-14 13:20 ` Alan Modra
  0 siblings, 1 reply; 18+ messages in thread
From: Fangrui Song @ 2021-06-13 21:54 UTC (permalink / raw)
  To: binutils
  Cc: H.J. Lu, Adhemerval Zanella, Carlos O'Donell, Michael Matz,
	Florian Weimer, Szabolcs Nagy, Fangrui Song

This fixes the bogus "relocation R_* against protected symbol `foo'
can not be used when making a shared object" for function symbols for
at least aarch64/i386/x86-64.

The controversial "copy relocations on protected data symbols" (which has some
fragile glibc support) is irrelevant to function symbols.

This patch makes the following program buildable with gcc -fpic -shared
-fuse-ld=bfd:

    __attribute__((visibility("protected"))) void *foo () {
      return (void *)foo;
    }

gold has always been good.

    PR ld/27973
    * ld-i386/protected1.d: Update.
    * ld-x86-64/protected1.d: Likewise.
    * ld-x86-64/protected7a.d: Likewise.
---
 bfd/elflink.c                        | 8 ++++++--
 ld/testsuite/ld-i386/protected1.d    | 4 +++-
 ld/testsuite/ld-x86-64/protected1.d  | 4 +++-
 ld/testsuite/ld-x86-64/protected7a.d | 4 +++-
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index c9a6e78be79..8e349af68e3 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -3294,9 +3294,13 @@ _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
   if (h == NULL)
     return true;
 
-  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
+  /* Non-STV_DEFAULT symbols must be local. However, STV_PROTECTED data symbols
+   * may cause controversial copy relocations on x86 which has some fragile
+   * support in glibc.  */
   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
-      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
+      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
+      || (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED
+	  && h->type == STT_FUNC))
     return true;
 
   /* Forced local symbols resolve locally.  */
diff --git a/ld/testsuite/ld-i386/protected1.d b/ld/testsuite/ld-i386/protected1.d
index a3cb5cef140..bbf5d77784d 100644
--- a/ld/testsuite/ld-i386/protected1.d
+++ b/ld/testsuite/ld-i386/protected1.d
@@ -1,3 +1,5 @@
 #as: --32
 #ld: -shared -melf_i386
-#error: .*relocation R_386_GOTOFF against protected function `foo' can not be used when making a shared object
+#readelf: -r
+
+There are no relocations in this file.
diff --git a/ld/testsuite/ld-x86-64/protected1.d b/ld/testsuite/ld-x86-64/protected1.d
index 783b85a1a6f..63f8d990370 100644
--- a/ld/testsuite/ld-x86-64/protected1.d
+++ b/ld/testsuite/ld-x86-64/protected1.d
@@ -1,3 +1,5 @@
 #as: --64
 #ld: -shared -melf_x86_64
-#error: .*relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
+#readelf: -r
+
+There are no relocations in this file.
diff --git a/ld/testsuite/ld-x86-64/protected7a.d b/ld/testsuite/ld-x86-64/protected7a.d
index 3082084a7b8..d675d5460ff 100644
--- a/ld/testsuite/ld-x86-64/protected7a.d
+++ b/ld/testsuite/ld-x86-64/protected7a.d
@@ -1,4 +1,6 @@
 #source: protected7.s
 #as: --64
 #ld: -shared -melf_x86_64
-#error: .*relocation R_X86_64_GOTOFF64 against protected function `foo' can not be used when making a shared object
+#readelf: -r
+
+There are no relocations in this file.
-- 
2.32.0.272.g935e593368-goog


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

end of thread, other threads:[~2021-06-18  2:42 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-13 21:54 [PATCH] Allow direct access relocations referencing a protected function symbol Fangrui Song
2021-06-14 13:20 ` Alan Modra
2021-06-14 14:03   ` Michael Matz
2021-06-14 14:48     ` H.J. Lu
2021-06-14 17:43       ` Fangrui Song
2021-06-15  2:46         ` Alan Modra
2021-06-15  3:19           ` Fangrui Song
2021-06-16  3:53             ` Alan Modra
2021-06-16  4:42               ` Fangrui Song
2021-06-16  6:31                 ` Alan Modra
2021-06-16  8:11                   ` Fangrui Song
2021-06-16 14:06                     ` Michael Matz
2021-06-17  2:59                     ` Alan Modra
2021-06-17  4:24                       ` Fangrui Song
2021-06-17 17:25                         ` H.J. Lu
2021-06-17 17:51                           ` Fangrui Song
2021-06-18  1:54                           ` Alan Modra
2021-06-18  2:41                             ` 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).