From 83c325007c5599fa9b60b8d5f7b84842160e1d1b Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 23 Jun 2022 01:10:44 -0700 Subject: aarch64: Allow PC-relative relocations against protected STT_FUNC for -shared __attribute__((visibility("protected"))) void *foo() { return (void *)foo; } gcc -fpic -shared -fuse-ld=bfd fails with the confusing diagnostic: 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 the executable link, not on the innocent shared object link. glibc 2.36 will give a warning at relocation resolving time. --- ld/testsuite/ld-aarch64/aarch64-elf.exp | 1 + ld/testsuite/ld-aarch64/pcrel-protected.s | 11 +++++++++++ ld/testsuite/ld-aarch64/pcrel_pic_protected.d | 7 +++++++ 3 files changed, 19 insertions(+) create mode 100644 ld/testsuite/ld-aarch64/pcrel-protected.s create mode 100644 ld/testsuite/ld-aarch64/pcrel_pic_protected.d (limited to 'ld') diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp index 3116227..3c45f87 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 0000000..24c6232 --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel-protected.s @@ -0,0 +1,11 @@ +.protected protected_a, protected_b, protected_c +.type protected_b, %object +.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 0000000..1b6f24c --- /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. -- cgit v1.1