[PATCH] ld: Allow R_X86_64_GOTPCREL for call *__tls_get_addr@GOTPCREL(%rip)
H.J. Lu
hjl.tools@gmail.com
Fri Jan 6 21:13:57 GMT 2023
On Fri, Jan 6, 2023 at 10:48 AM Fangrui Song <maskray@google.com> wrote:
>
> On Fri, Jan 6, 2023 at 9:04 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Thu, Jan 5, 2023 at 1:06 PM Fangrui Song via Binutils
> > <binutils@sourceware.org> wrote:
> > >
> > > _Thread_local int a;
> > > int main() { return a; }
> > >
> > > % gcc -fno-plt -fpic a.c -fuse-ld=bfd -Wa,-mrelax-relocations=no
> > > /usr/bin/ld.bfd: /tmp/ccSSBgrg.o: TLS transition from R_X86_64_TLSGD to R_X86_64_GOTTPOFF against `a' at 0xd in section `.text' failed
> > > /usr/bin/ld.bfd: failed to set dynamic section sizes: bad value
> > > collect2: error: ld returned 1 exit status
> > >
> > > This commit fixes the issue.
> > >
> > > PR ld/24784
> > > * bfd/elf64-x86-64.c (elf_x86_64_check_tls_transition): Allow
> > > R_X86_64_GOTPCREL.
> > > ---
> > > bfd/elf64-x86-64.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
> > > index 914f82d0151..095fe2e0fe6 100644
> > > --- a/bfd/elf64-x86-64.c
> > > +++ b/bfd/elf64-x86-64.c
> > > @@ -1241,7 +1241,7 @@ elf_x86_64_check_tls_transition (bfd *abfd,
> > > if (largepic)
> > > return r_type == R_X86_64_PLTOFF64;
> > > else if (indirect_call)
> > > - return r_type == R_X86_64_GOTPCRELX;
> > > + return (r_type == R_X86_64_GOTPCRELX || r_type == R_X86_64_GOTPCREL);
> > > else
> > > return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
> > > }
> > > --
> > > 2.39.0.314.g84b9a713c41-goog
> > >
> >
> > Since the new TLS sequence was added after R_X86_64_GOTPCRELX was
> > required for call, R_X86_64_GOTPCREL should be invalid in this TLS sequence.
> >
> > --
> > H.J.
>
> I have multiple arguments (albeit no single one is very strong) that
> this 1-deletion-1-addition change provides benefits for users (IMHO
> with no burden to binutils at all).
>
> Some projects may add -Wa,-mrelax-relocations=no to work around older
> GNU ld. Then the project's toolchain requirement may increase and no
> longer need to work around older GNU ld.
> But a distribution may for some reason use a global -fno-plt (e.g.
> Arch Linux) and then run into this TLS GD/LD->IE/LE optimization
> issue.
>
> rust src/ci/docker/host-x86_64/*musl/Dockerfile
> openjdk/jdk19u make/autoconf/flags-cflags.m4 (this file appears to be
> copied into quite a few projects)
> Linux kernel arch/x86/boot/compressed/Makefile (not a good example as
> it doesn't use TLS AFAICT)
>
> R_X86_64_GOTPCREL isn't purely usefull. It may help linker design: for
> R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX, the linker can make a
> decision upfront whether a GOT entry is needed
> (this affects the size of .got, which may affect section layout and
> whether other relocations may overflow).
> This may increase risk of 32-bit relocation overflow.
> R_X86_64_GOTPCREL can mitigate the risk while being aware to the user.
>
> rustc somehow disables x86 relaxed relocations and defaults to `-Z
Why is that?
> plt=no` and now relies on llvm-project to work around the GNU ld
> compatibility issue.
>
>
>
>
> --
> 宋方睿
--
H.J.
More information about the Binutils
mailing list