Bug 29377 - non-canonical reference to canonical protected function
Summary: non-canonical reference to canonical protected function
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.40
: P2 normal
Target Milestone: 2.39
Assignee: H.J. Lu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-18 17:48 UTC by H.J. Lu
Modified: 2022-07-19 16:13 UTC (History)
2 users (show)

See Also:
Host:
Target: i386,x86-64
Build:
Last reconfirmed:


Attachments
A patch (1.17 KB, patch)
2022-07-18 18:49 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2022-07-18 17:48:41 UTC
[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]$
Comment 1 H.J. Lu 2022-07-18 18:49:03 UTC
Created attachment 14217 [details]
A patch
Comment 2 Thiago Macieira 2022-07-18 18:54:55 UTC
(In reply to H.J. Lu from comment #1)
> Created attachment 14217 [details]
> A patch

Confirmed to work.
Comment 3 Fangrui Song 2022-07-18 23:33:36 UTC
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
Comment 4 Sourceware Commits 2022-07-19 01:16:36 UTC
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.
Comment 5 Sourceware Commits 2022-07-19 01:55:07 UTC
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.
Comment 6 Sourceware Commits 2022-07-19 16:08:22 UTC
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)
Comment 7 Sourceware Commits 2022-07-19 16:12:34 UTC
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)
Comment 8 H.J. Lu 2022-07-19 16:13:14 UTC
Fixed for 2.39 and on 2.38 branch.