public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <maskray@google.com>
To: binutils@sourceware.org, Szabolcs Nagy <szabolcs.nagy@arm.com>
Cc: Fangrui Song <maskray@google.com>
Subject: [PATCH] aarch64: Allow PC-relative relocations against protected STT_FUNC
Date: Wed,  1 Jun 2022 23:50:38 -0700	[thread overview]
Message-ID: <20220602065038.3198997-1-maskray@google.com> (raw)

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

gcc -fpic -shared incorrectly fails with:

    relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `foo' which may bind externally can not be used when making a shared object; recompile with -fPIC

Call _bfd_elf_symbol_refs_local_p with local_protected==true to suppress
the error.  The new behavior matches gold and ld.lld.

Note: if some code tries to use direct access relocations to take the
address of foo (likely due to -fno-pic), the pointer equality will
break, but the error should be reported on that link.
---
 bfd/elfnn-aarch64.c                           |  2 +-
 ld/testsuite/ld-aarch64/aarch64-elf.exp       |  1 +
 ld/testsuite/ld-aarch64/pcrel-protected.s     | 13 +++++++++++++
 ld/testsuite/ld-aarch64/pcrel_pic_protected.d |  7 +++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-aarch64/pcrel-protected.s
 create mode 100644 ld/testsuite/ld-aarch64/pcrel_pic_protected.d

diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index cb316a928ef..2c8e3439d43 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -5878,7 +5878,7 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
       if (bfd_link_pic (info)
 	  && (input_section->flags & SEC_ALLOC) != 0
 	  && (input_section->flags & SEC_READONLY) != 0
-	  && !SYMBOL_REFERENCES_LOCAL (info, h))
+	  && !_bfd_elf_symbol_refs_local_p (h, info, 1))
 	{
 	  int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
 
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index 64476f111e0..565a67aa9aa 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -248,6 +248,7 @@ run_dump_test_lp64 "local-addend-r"
 # test error handling on pcrel relocation for shared libraries.
 run_dump_test_lp64 "pcrel_pic_undefined"
 run_dump_test_lp64 "pcrel_pic_defined"
+run_dump_test_lp64 "pcrel_pic_protected"
 
 run_dump_test "limit-b"
 run_dump_test "limit-bl"
diff --git a/ld/testsuite/ld-aarch64/pcrel-protected.s b/ld/testsuite/ld-aarch64/pcrel-protected.s
new file mode 100644
index 00000000000..ce2a92851ad
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/pcrel-protected.s
@@ -0,0 +1,13 @@
+.protected protected_a
+.protected protected_b
+.type protected_b, %object
+.protected protected_c
+.type protected_c, %function
+
+.text
+	adrp    x0, protected_a
+	add     x0, x0, :lo12:protected_a
+	adrp    x0, protected_b
+	add     x0, x0, :lo12:protected_b
+	adrp    x0, protected_c
+	add     x0, x0, :lo12:protected_c
diff --git a/ld/testsuite/ld-aarch64/pcrel_pic_protected.d b/ld/testsuite/ld-aarch64/pcrel_pic_protected.d
new file mode 100644
index 00000000000..1b6f24cf2fb
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/pcrel_pic_protected.d
@@ -0,0 +1,7 @@
+#name: PC-Rel relocation against protected
+#source: pcrel-protected.s
+#target: [check_shared_lib_support]
+#ld: -shared -e0 --defsym protected_a=0x1000 --defsym protected_b=0x1010 --defsym protected_c=0x1020
+#readelf: -r -W
+#...
+There are no relocations in this file.
-- 
2.36.1.255.ge46751e96f-goog


             reply	other threads:[~2022-06-02  6:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-02  6:50 Fangrui Song [this message]
2022-06-22 18:02 ` Fangrui Song
2022-06-23  8:00 ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220602065038.3198997-1-maskray@google.com \
    --to=maskray@google.com \
    --cc=binutils@sourceware.org \
    --cc=szabolcs.nagy@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).