public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Fangrui Song <i@maskray.me>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: GCC Development <gcc@gcc.gnu.org>, Florian Weimer <fw@deneb.enyo.de>
Subject: Re: Add -fdirect-access-external-data
Date: Thu, 7 Jan 2021 19:38:25 -0800	[thread overview]
Message-ID: <MWHPR1201MB0110BBE4756308546212BC9FCBAE0@MWHPR1201MB0110.namprd12.prod.outlook.com> (raw)
In-Reply-To: <CAMe9rOrj+OXwe7O+QjNw9mXiWqJJ8pYSbhQ6Kqg56wqn0qBJVg@mail.gmail.com>

On Thu, Jan 7, 2021 at 6:07 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Jan 6, 2021 at 10:32 PM Fangrui Song <i@maskray.me> wrote:
> >
> > On Sat, Dec 26, 2020 at 7:39 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > On Sat, Dec 26, 2020 at 7:32 AM Florian Weimer <fw@deneb.enyo.de> wrote:
> > > >
> > > > * Fangrui Song:
> > > >
> > > > > Hi, I filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98112 which
> > > > > proposes -fdirect-access-external-data to address some x86-64
> > > > > GCC/binutils pain[1] and also benefit non-x86 architectures (also see [1]
> > > > > it can prevent copy relocations).
> > > > >
> > > > > [1] Mentioned in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98112#c2
> > > > >
> > > > > Since I am going to add this option to Clang and I hope (once GCC decides to
> > > > > implement this option the two compilers can use the same option name), I bring
> > > > > it to your attention.
> > > >
> > > > One worry I have is that people start building shared objects with
> > > > direct data access, expecting the main program to be built with
> > > > indirect access.  We already see this today with Qt.  It's not really
> > > > supported well by the toolchain and causes frequent issues.
> > >
> > > It can be solved by ABI extension implemented in linker, ld.so and
> > > compiler.
> > >
> > > > Depending on the ELF ABI in question, the new pair of -f options might
> > > > not actually be meaningful.  It really depends on whether you have
> > > > reasonably-sized displacements available.  I think there are some ABIs
> > > > where the optimization is theoretically possible, but impractical
> > > > because the ilimit it imposes on data segment (think AArch64 without
> > > > adrp).
> > >
> > >
> > >
> > > --
> > > H.J.
> >
> > Please check out new comments on
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98112
> >
> > -fdirect-access-external-data is still the best name. The option is
> > useful to avoid copy relocations / "canonical PLT entry"
> > (st_shndx=0,st_value!=0) in -fno-pic code.
> > I will proceed with my Clang patch.
>
> If I understand it correctly, you want to treat all accesses to protected
> definitions as local access
> and all read/write accesses to undefined symbols
> should go through GOT.  Branches to undefined symbols can use PLT.
> -fdirect-access-external-data doesn't reflect it.

My apologies. Direct/indirect access to protected definitions is a separate
topic, unrelated to -f[no-]direct-access-external-data.

(
If anyone is interested, there was a heated discussion about accesses to
protected definitions
https://sourceware.org/legacy-ml/binutils/2016-03/msg00312.html basically a lot
of folks considered that copy relocations are best-effort support provided by
the toolchain. For protected symbols, copy relocations do not necessarily work.

Clang always treats protected similar to hidden/internal, no special
logic for x86-64 protected.
)

Branches to undefined symbols is yet another separate topic.

(
On x86-64, there is no PIC vs non-PIC PLT distinction and an R_X86_64_PLT32
relocation is produced for both `call/jmp foo` and `call/jmp foo@PLT` with newer
(2018) GNU as/LLVM integrated assembler.

On i386, there are 2 types of PLTs, PIC and non-PIC. Currently the informal
convention is to use R_386_PC32 for non-PIC PLT and R_386_PLT32 for PIC PLT, but
R_386_PLT32 is arguably preferable for -fno-pic code as well: this can avoid a
"canonical PLT entry" (st_shndx=0, st_value!=0) if the symbol turns out to be
defined externally.

My idea is that we can always use R_386_PLT32 in -fno-pic mode.
)

Taking the address of an external function is related to
-f[no-]direct-access-external-data. A function pointer of an external function
is very similar to external data.

A canonical PLT entry can be caused by either a branch (R_386_PC32/R_386_32)
or an address taken operation (R_386_PC32/R_386_32) if the symbol
turns out to be external.
-fno-direct-access-external-data can only address the function pointer usage.

  reply	other threads:[~2021-01-08  3:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-17  7:10 Fangrui Song
2020-12-26 15:31 ` Florian Weimer
2020-12-26 15:39   ` H.J. Lu
2021-01-07  6:26     ` Fangrui Song
     [not found]     ` <MWHPR1201MB011025EE98F5F40BA9AE62E0CBAF0@MWHPR1201MB0110.namprd12.prod.outlook.com>
2021-01-08  2:07       ` H.J. Lu
2021-01-08  3:38         ` Fangrui Song [this message]
     [not found]         ` <MWHPR1201MB01103E59742F469B75B35BDBCBAE0@MWHPR1201MB0110.namprd12.prod.outlook.com>
2021-01-08  4:11           ` 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=MWHPR1201MB0110BBE4756308546212BC9FCBAE0@MWHPR1201MB0110.namprd12.prod.outlook.com \
    --to=i@maskray.me \
    --cc=fw@deneb.enyo.de \
    --cc=gcc@gcc.gnu.org \
    --cc=hjl.tools@gmail.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).