[hjl@gnu-tgl-3 protected]$ cat foo.c __attribute__ ((visibility ("protected"))) void foo (void) { } [hjl@gnu-tgl-3 protected]$ cat main.c extern void foo (void); void (*foo_p) (void); int main () { foo_p = foo; return 0; } [hjl@gnu-tgl-3 protected]$ gcc -mno-direct-extern-access foo.c main.c /usr/local/bin/ld: /tmp/cc4BdqMu.o: non-canonical reference to canonical protected function `foo' in /tmp/ccx682Zd.o /usr/local/bin/ld: failed to set dynamic section sizes: bad value collect2: error: ld returned 1 exit status [hjl@gnu-tgl-3 protected]$
Created attachment 14217 [details] A patch
(In reply to H.J. Lu from comment #1) > Created attachment 14217 [details] > A patch Confirmed to work.
This patch looks good to me. __attribute__((visibility("protected"))) void *foo() { return (void *)foo; } % gcc -m32 -fpic -shared -fuse-ld=bfd b.c /usr/bin/ld.bfd: /tmp/cc3Ay0Gh.o: relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object /usr/bin/ld.bfd: final link failed: bad value collect2: error: ld returned 1 exit status I'd still with that the x86 port considers https://sourceware.org/pipermail/binutils/2022-June/121423.html I have fixed GNU ld's aarch64/arm ports to match other ports and lld. This addresses all issues reported on https://maskray.me/blog/2021-01-09-copy-relocations-canonical-plt-entries-and-protected
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bd0736124c20950ac353f01e5029b7cb5d8a7010 commit bd0736124c20950ac353f01e5029b7cb5d8a7010 Author: H.J. Lu <hjl.tools@gmail.com> Date: Mon Jul 18 11:44:32 2022 -0700 x86: Properly check invalid relocation against protected symbol Only check invalid relocation against protected symbol defined in shared object. bfd/ PR ld/29377 * elf32-i386.c (elf_i386_scan_relocs): Only check invalid relocation against protected symbol defined in shared object. * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise. ld/ PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests. * testsuite/ld-elf/pr29377a.c: New file. * testsuite/ld-elf/pr29377b.c: Likewise.
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=76e4fa70e79ee959c0e11eaacd4cf863291b1ddb commit 76e4fa70e79ee959c0e11eaacd4cf863291b1ddb Author: H.J. Lu <hjl.tools@gmail.com> Date: Mon Jul 18 18:49:27 2022 -0700 ld: Pass -nostdlib to compiler with -r Pass -nostdlib to compiler with -r to avoid unnecessary .o file and libraries. PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Pass -nostdlib with -r.
The binutils-2_39-branch branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=17f1c83d5ba7a1d3898265face9027ea02c3fb8c commit 17f1c83d5ba7a1d3898265face9027ea02c3fb8c Author: H.J. Lu <hjl.tools@gmail.com> Date: Mon Jul 18 11:44:32 2022 -0700 x86: Properly check invalid relocation against protected symbol Only check invalid relocation against protected symbol defined in shared object. bfd/ PR ld/29377 * elf32-i386.c (elf_i386_scan_relocs): Only check invalid relocation against protected symbol defined in shared object. * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise. ld/ PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests. * testsuite/ld-elf/pr29377a.c: New file. * testsuite/ld-elf/pr29377b.c: Likewise. (cherry picked from commit bd0736124c20950ac353f01e5029b7cb5d8a7010) ld: Pass -nostdlib to compiler with -r Pass -nostdlib to compiler with -r to avoid unnecessary .o file and libraries. PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Pass -nostdlib with -r. (cherry picked from commit 76e4fa70e79ee959c0e11eaacd4cf863291b1ddb)
The binutils-2_38-branch branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=19892fedb7b7e9129d76a0c48b26721d4973fb20 commit 19892fedb7b7e9129d76a0c48b26721d4973fb20 Author: H.J. Lu <hjl.tools@gmail.com> Date: Mon Jul 18 11:44:32 2022 -0700 x86: Properly check invalid relocation against protected symbol Only check invalid relocation against protected symbol defined in shared object. bfd/ PR ld/29377 * elf32-i386.c (elf_i386_scan_relocs): Only check invalid relocation against protected symbol defined in shared object. * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise. ld/ PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests. * testsuite/ld-elf/pr29377a.c: New file. * testsuite/ld-elf/pr29377b.c: Likewise. (cherry picked from commit bd0736124c20950ac353f01e5029b7cb5d8a7010) ld: Pass -nostdlib to compiler with -r Pass -nostdlib to compiler with -r to avoid unnecessary .o file and libraries. PR ld/29377 * testsuite/ld-elf/linux-x86.exp: Pass -nostdlib with -r. (cherry picked from commit 76e4fa70e79ee959c0e11eaacd4cf863291b1ddb)
Fixed for 2.39 and on 2.38 branch.