public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Fangrui Song <i@maskray.me>
Cc: Alan Modra <amodra@gmail.com>, Cary Coutant <ccoutant@gmail.com>,
	Michael Matz <matz@suse.de>,  Florian Weimer <fweimer@redhat.com>,
	Szabolcs Nagy <szabolcs.nagy@arm.com>,
	 "binutils@sourceware.org" <binutils@sourceware.org>,
	Fangrui Song <maskray@google.com>
Subject: Re: [PATCH] Allow direct access relocations referencing a protected function symbol
Date: Thu, 17 Jun 2021 10:25:09 -0700	[thread overview]
Message-ID: <CAMe9rOqWJa2FwE0F+Gc3rgRDV7k9yB=rv6hUEWubrzspsqWC+w@mail.gmail.com> (raw)
In-Reply-To: <20210617042404.cdojdbmlage6bw7y@gmail.com>

On Wed, Jun 16, 2021 at 9:24 PM Fangrui Song <i@maskray.me> wrote:
>
> On 2021-06-17, Alan Modra wrote:
> >On Wed, Jun 16, 2021 at 01:11:43AM -0700, Fangrui Song wrote:
> >> While (I think Cary/Michael) and I prefer that STV_PROTECTED STT_FUNC symbols don't have unneeded dynamic relocations in -shared mode,
> >
> >I strongly prefer that too.  The only thing I object to is incorrect
> >fixes..
>
> Thanks...
>
> >> I don't mind if GNU ld keeps producing them.
> >> Anyhow the important thing is the following spurious error:
> >>
> >>   % gcc -fpic -shared -fuse-ld=bfd a.s
> >>   /usr/bin/ld.bfd: /tmp/ccWPJCLw.o: relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
> >>   __attribute__((visibility("protected"))) void *foo () {
> >>     return (void *)foo;
> >>   }
> >
> >Agreed, that is an x86 linker bug.
>
> Sent https://sourceware.org/pipermail/binutils/2021-June/116985.html for
> x86-64.
>
> Hope some aarch64 folks in the CC list can fix aarch64...
>
> >Here is the result of your testcase on powerpc64le, power10 to get
> >pc-relative cpu support, and linked without startup files so just the
> >relocs in this code are seen.
> >
> >powerpc64le-linux-gcc -c -O2 -fPIC -mcpu=power10 prot2.c
> >powerpc64le-linux-ld -shared -o prot2 prot2.o
> >readelf -r prot2
> >There are no relocations in this file.
>
> So x86 and aarch64 have major issues. arm has a minor issue.
> other ports are probably good.
>
> x86-64: broken direct access relocation, unneeded GLOB_DAT
> aarch64: broken direct access relocation, unneeded GLOB_DAT
> arm: unneeded GLOB_DAT
> ppc64le: good
> mips64el: good
> riscv64: good
>
> >>
> >> If you decide to take over, fixing the error will be really great and will be an important step fixing protected symbols.
> >
> >It would be inappropriate for me to take over.  This is a target
> >issue, caused by a deliberate choice on the part of HJ to optimise in
> >the executable at the expense of shared library optimisation (*).  The
> >fix needs to be in the x86 target code, and it's a very long time
> >since I was an x86 binutils maintainer.
>
> nvm, I sent  https://sourceware.org/pipermail/binutils/2021-June/116985.html
>
> >*) I'll note that at the time, when non-pic executables were more
> >common, the trick HJ used made quite a lot of sense from an
> >optimisation viewpoint, but frankly I'm amazed that he managed to get
> >it all working.  In fact, the optimisation makes even more sense on
> >powerpc prior to power10 but I was unwilling to go against the ELF
> >gABI (when narrowly considering the resulting shared libraries in
> >isolation) just to shave off cycles in micro-benchmarks.
>
> Hmm, I think those changes don't improve -fno-pic or -fpie performance.
> ELF -fno-pic has always been using absolute relocations for
> data/functions. For -fpie, HAVE_LD_PIE_COPYRELOC has no benefit but a
> tiny bit of size decrease.  ppc64 ELFv2 toc-indirection to toc-relative
> optimization renders the GOT optimization mostly useless, as well.
> HAVE_LD_PIE_COPYRELOC, if implemented, may have a tiny bit of effects on
> other arches, but I am of the opinion that toolchain developers do not
> necessarily show mercy to applications where global variable access is a
> bottleneck.  The applications should fix their global variable usage:
> hidden/protected/local alias. Either can improve global variable access
> if they indeed decide to care. Unfortunately protected data accesses
> have unneeded GOT indirection for all GCC arches in -fpic mode.

On x86-64, function pointers in executable for external funtions may be
resolved to their PLT entries in executable.  If it happens, function
pointers of protected funtions in shared libraries must be resolved to
the PLT entries in executable, not addresses of protected funtions in
shared libraries.

I made a proposal to remove copy relocation and add canonical function
address:

https://gitlab.com/x86-psABIs/x86-64-ABI/-/issues/8

It should address this issue.  But this is an ABI change.  The question
is if we should do it silently which can result in run-time failure.

-- 
H.J.

  reply	other threads:[~2021-06-17 17:25 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-13 21:54 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 [this message]
2021-06-17 17:51                           ` Fangrui Song
2021-06-18  1:54                           ` Alan Modra
2021-06-18  2:41                             ` H.J. Lu

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='CAMe9rOqWJa2FwE0F+Gc3rgRDV7k9yB=rv6hUEWubrzspsqWC+w@mail.gmail.com' \
    --to=hjl.tools@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=ccoutant@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=i@maskray.me \
    --cc=maskray@google.com \
    --cc=matz@suse.de \
    --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).