Bug 22263 - -fpie -pie generates dynamic relocations in text section
Summary: -fpie -pie generates dynamic relocations in text section
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 20179 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-10-05 10:27 UTC by H.J. Lu
Modified: 2024-05-21 11:12 UTC (History)
8 users (show)

See Also:
Host:
Target: tilegx,tilepro,sparc,alpha,riscv
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2017-10-05 10:27:07 UTC
[hjl@gnu-efi-2 build-tilepro-linux]$ cat x.c
__thread int * foo;

void
bar (void)
{
  *foo = 1;
}
[hjl@gnu-efi-2 build-tilepro-linux]$ cat y.c
extern __thread int *foo;
 
static int x;

extern void bar (void);

int
main ()
{
  foo = &x;
  return 0;
}
[hjl@gnu-efi-2 build-tilepro-linux]$ /export/gnu/import/git/toolchain/install/compilers/tilepro-linux-gnu/bin/tilepro-glibc-linux-gnu-gcc -pie -fpie x.c y.c -O2 -Wl,-z,text
/export/gnu/import/git/toolchain/install/compilers/tilepro-linux-gnu/lib/gcc/tilepro-glibc-linux-gnu/7.2.1/../../../../tilepro-glibc-linux-gnu/bin/ld: read-only segment has dynamic relocations.
collect2: error: ld returned 1 exit status
[hjl@gnu-efi-2 build-tilepro-linux]$
Comment 1 H.J. Lu 2017-10-08 00:28:44 UTC
The same thing happens on sparc:

[hjl@gnu-efi-2 pr22263]$ make
/export/gnu/import/git/toolchain/install/compilers/sparc64-linux-gnu/bin/sparc64-glibc-linux-gnu-gcc -fPIE -O2   -c -o x.o x.c
/export/gnu/import/git/toolchain/install/compilers/sparc64-linux-gnu/bin/sparc64-glibc-linux-gnu-gcc -fPIE -O2   -c -o y.o y.c
/export/gnu/import/git/toolchain/install/compilers/sparc64-linux-gnu/bin/sparc64-glibc-linux-gnu-gcc -pie -Wl,-z,text -o x x.o y.o
/export/gnu/import/git/toolchain/install/compilers/sparc64-linux-gnu/lib/gcc/sparc64-glibc-linux-gnu/7.2.1/../../../../sparc64-glibc-linux-gnu/bin/ld: read-only segment has dynamic relocations.
collect2: error: ld returned 1 exit status
make: *** [Makefile:16: x] Error 1
[hjl@gnu-efi-2 pr22263]$
Comment 2 H.J. Lu 2017-10-08 21:15:51 UTC
This is caused by incorrectly checking bfd_link_pic, which should check
bfd_link_executable instead.
Comment 3 H.J. Lu 2017-10-08 21:36:26 UTC
tilegx has the same issue:

[hjl@gnu-efi-2 pr22263]$ make
/export/gnu/import/git/toolchain/install/compilers/tilegx-linux-gnu/bin/tilegx-glibc-linux-gnu-gcc -fPIE -O2   -c -o x.o x.c
/export/gnu/import/git/toolchain/install/compilers/tilegx-linux-gnu/bin/tilegx-glibc-linux-gnu-gcc -fPIE -O2   -c -o y.o y.c
./ld -pie -z text -o x x.o y.o
./ld: read-only segment has dynamic relocations.
make: *** [Makefile:23: x] Error 1
[hjl@gnu-efi-2 pr22263]$
Comment 4 H.J. Lu 2017-10-08 22:06:44 UTC
*** Bug 20179 has been marked as a duplicate of this bug. ***
Comment 5 H.J. Lu 2017-10-12 21:07:54 UTC
alpha has the same issue.
Comment 6 Sourceware Commits 2017-10-12 21:56: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=74ffa566739c7e98568a9aa8b5ab8d8f3730d30d

commit 74ffa566739c7e98568a9aa8b5ab8d8f3730d30d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 12 14:41:22 2017 -0700

    Add a compile-time test for PR ld/22263
    
    This compile-time test requires a target C compiler to run.  It fails
    on many targets where ELF backend linkers incorrectly check bfd_link_pic
    for TLS relocations, which should check bfd_link_executable instead.
    
    	PR ld/22263
    	* testsuite/ld-elf/pr22263-1.rd: New file.
    	* testsuite/ld-elf/pr22263-1a.c: Likewise.
    	* testsuite/ld-elf/pr22263-1b.c: Likewise.
    	* testsuite/ld-elf/tls.exp: Likewise.
Comment 7 Sourceware Commits 2017-10-13 13:46:43 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=354bac403e9a19bbbfb676d854db5f11f0e84c27

commit 354bac403e9a19bbbfb676d854db5f11f0e84c27
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Oct 13 15:23:35 2017 +1030

    PR22263 testcase
    
    A number of targets need dynamic relocs in PIEs for reasons other than
    relocating thread variables.  The PR is about text relocations, and,
    reading between the lines, unnecessary dynamic tprel relocations.
    Change the test to check for those conditions rather than no dynamic
    relocations
    
    	PR ld/22263
    	* testsuite/ld-elf/tls.exp: Link with -z text.
    	* testsuite/ld-elf/pr22263-1.rd: Test for tprel relocs.
Comment 8 Sourceware Commits 2017-10-19 12:19:11 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=0fb7012e88683b8bd67a4fb8f782359fa0e11724

commit 0fb7012e88683b8bd67a4fb8f782359fa0e11724
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 19 05:18:07 2017 -0700

    sparc: Check bfd_link_executable for TLS check
    
    Copied from x86, check bfd_link_executable, instead of bfd_link_pic,
    for TLS transition check.  Not sure if it works correctly.  All usages
    of bfd_link_pic should be audited.
    
    	PR ld/22263
    	* elfxx-sparc.c (sparc_elf_tls_transition): Replace
    	bfd_link_pic with !bfd_link_executable, !bfd_link_pic with
    	bfd_link_executable for TLS check.
    	(_bfd_sparc_elf_check_relocs): Likewise.
    	(allocate_dynrelocs): Likewise.
    	(_bfd_sparc_elf_relocate_section): Likewise.
Comment 9 Sourceware Commits 2017-10-19 12:21:50 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=1ba1f393faac96f6ad80499d7133df23d6187b8c

commit 1ba1f393faac96f6ad80499d7133df23d6187b8c
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 19 05:20:44 2017 -0700

    tilepro: Check bfd_link_executable for TLS check
    
    Copied from x86, check bfd_link_executable, instead of bfd_link_pic,
    for TLS transition check.  Not sure if it works correctly.  All usages
    of bfd_link_pic should be audited.
    
    	PR ld/22263
    	* elf32-tilepro.c (tilepro_elf_tls_transition): Replace
    	bfd_link_pic with !bfd_link_executable, !bfd_link_pic with
    	bfd_link_executable for TLS check.
    	(tilepro_elf_check_relocs): Likewise.
    	(allocate_dynrelocs): Likewise.
    	(tilepro_elf_relocate_section): Likewise.
Comment 10 Sourceware Commits 2017-10-19 12:24:27 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=280958942b74f3164708700f195188c4d3446f58

commit 280958942b74f3164708700f195188c4d3446f58
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 19 05:22:23 2017 -0700

    tilegx: Check bfd_link_executable for TLS check
    
    Copied from x86, check bfd_link_executable, instead of bfd_link_pic,
    for TLS transition check.  Not sure if it works correctly.  All usages
    of bfd_link_pic should be audited.
    
    	PR ld/22263
    	* elfxx-tilegx.c (tilegx_elf_tls_transition): Replace
    	bfd_link_pic with !bfd_link_executable, !bfd_link_pic with
    	bfd_link_executable for TLS check.
    	(tilegx_elf_check_relocs): Likewise.
    	(allocate_dynrelocs): Likewise.
    	(tilegx_elf_relocate_section): Likewise.
Comment 11 Sourceware Commits 2017-11-27 11:54:27 UTC
The master branch has been updated by Szabolcs Nagy <nsz@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6dda7875a8021787f11bd95a69d81a079e408a57

commit 6dda7875a8021787f11bd95a69d81a079e408a57
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Nov 15 17:40:04 2017 +0000

    [PR ld/22263] aarch64: Avoid dynamic TLS relocs in PIE
    
    No dynamic relocs are needed for TLS defined in an executable, the
    TP relative offset is known at link time.
    
    Fixes
    FAIL: Build pr22263-1
    
    bfd/
    	PR ld/22263
    	* elfnn-aarch64.c (elfNN_aarch64_relocate_section): Use
    	bfd_link_executable instead of bfd_link_pic for TLS.
    	(elfNN_aarch64_allocate_dynrelocs): Likewise.
    	(aarch64_can_relax_tls): Likewise.
Comment 12 Sourceware Commits 2018-01-25 11:21:17 UTC
The master branch has been updated by Eric Botcazou <ebotcazou@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bb363086e7743506d78bc6b1e56face0fb1fc93f

commit bb363086e7743506d78bc6b1e56face0fb1fc93f
Author: Eric Botcazou <ebotcazou@gcc.gnu.org>
Date:   Thu Jan 25 12:16:06 2018 +0100

    Fix PR ld/22727 (TLS breakage in PIC/PIE mode on SPARC).
    
    There are actually 2 different bugs:
     1. TLS transition is broken in PIE mode.
     2. TLS is broken in PIC/PIE mode when the __tls_get_addr symbol
        is versioned  (as is the case on Linux and Solaris at least).
    
    The 1st bug is fixed by reverting the problematic change for now
    (note that the associated test doesn't pass on SPARC because of another
    issue so there is  no formal regression in the testsuite). The 2nd bug
    is fixed by changing the  call to _bfd_generic_link_add_one_symbol
    on __tls_get_addr into a mere lookup in _bfd_sparc_elf_check_relocs.
    
    bfd/
    	* elfxx-sparc.c (_bfd_sparc_elf_check_relocs) <R_SPARC_TLS_GD_CALL>:
    	Do a mere lookup of the __tls_get_addr symbol instead of adding it.
    
    	Revert
    	2017-10-19  H.J. Lu  <hongjiu.lu@intel.com>
    
    	PR ld/22263
    	* elfxx-sparc.c (sparc_elf_tls_transition): Replace
    	bfd_link_pic with !bfd_link_executable, !bfd_link_pic with
    	bfd_link_executable for TLS check.
    	(_bfd_sparc_elf_check_relocs): Likewise.
    	(allocate_dynrelocs): Likewise.
    	(_bfd_sparc_elf_relocate_section): Likewise.
    ld/
    	* testsuite/ld-sparc/sparc.exp (32-bit: Helper shared library):
    	Link with a version script.
    	(32-bit: TLS -fpie): New test.
    	(64-bit: Helper shared library): Link with a version script.
    	(64-bit: TLS -fpie): New test.
    	(64-bit: GOTDATA relocations): Pass -Av9 to the assembler.
    	* testsuite/ld-sparc/tlslib.ver: New file.
    	* testsuite/ld-sparc/tlspie32.dd: Likewise.
    	* testsuite/ld-sparc/tlspie32.s: Likewise.
    	* testsuite/ld-sparc/tlspie64.dd: Likewise.
    	* testsuite/ld-sparc/tlspie64.s: Likewise.
    	* testsuite/ld-sparc/tlssunbin32.dd: Adjust for versioned symbol.
    	* testsuite/ld-sparc/tlssunbin32.rd: Likewise.
    	* testsuite/ld-sparc/tlssunbin32.sd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.dd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.rd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.sd: Likewise.
Comment 13 Sourceware Commits 2018-01-25 11:31:58 UTC
The binutils-2_30-branch branch has been updated by Eric Botcazou <ebotcazou@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=220cf7e75d1e554d0a003e1c505a335ed663725e

commit 220cf7e75d1e554d0a003e1c505a335ed663725e
Author: Eric Botcazou <ebotcazou@gcc.gnu.org>
Date:   Thu Jan 25 12:16:06 2018 +0100

    Fix PR ld/22727 (TLS breakage in PIC/PIE mode on SPARC).
    
    There are actually 2 different bugs:
     1. TLS transition is broken in PIE mode.
     2. TLS is broken in PIC/PIE mode when the __tls_get_addr symbol
        is versioned  (as is the case on Linux and Solaris at least).
    
    The 1st bug is fixed by reverting the problematic change for now
    (note that the associated test doesn't pass on SPARC because of another
    issue so there is  no formal regression in the testsuite). The 2nd bug
    is fixed by changing the  call to _bfd_generic_link_add_one_symbol
    on __tls_get_addr into a mere lookup in _bfd_sparc_elf_check_relocs.
    
    bfd/
    	* elfxx-sparc.c (_bfd_sparc_elf_check_relocs) <R_SPARC_TLS_GD_CALL>:
    	Do a mere lookup of the __tls_get_addr symbol instead of adding it.
    
    	Revert
    	2017-10-19  H.J. Lu  <hongjiu.lu@intel.com>
    
    	PR ld/22263
    	* elfxx-sparc.c (sparc_elf_tls_transition): Replace
    	bfd_link_pic with !bfd_link_executable, !bfd_link_pic with
    	bfd_link_executable for TLS check.
    	(_bfd_sparc_elf_check_relocs): Likewise.
    	(allocate_dynrelocs): Likewise.
    	(_bfd_sparc_elf_relocate_section): Likewise.
    ld/
    	* testsuite/ld-sparc/sparc.exp (32-bit: Helper shared library):
    	Link with a version script.
    	(32-bit: TLS -fpie): New test.
    	(64-bit: Helper shared library): Link with a version script.
    	(64-bit: TLS -fpie): New test.
    	(64-bit: GOTDATA relocations): Pass -Av9 to the assembler.
    	* testsuite/ld-sparc/tlslib.ver: New file.
    	* testsuite/ld-sparc/tlspie32.dd: Likewise.
    	* testsuite/ld-sparc/tlspie32.s: Likewise.
    	* testsuite/ld-sparc/tlspie64.dd: Likewise.
    	* testsuite/ld-sparc/tlspie64.s: Likewise.
    	* testsuite/ld-sparc/tlssunbin32.dd: Adjust for versioned symbol.
    	* testsuite/ld-sparc/tlssunbin32.rd: Likewise.
    	* testsuite/ld-sparc/tlssunbin32.sd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.dd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.rd: Likewise.
    	* testsuite/ld-sparc/tlssunbin64.sd: Likewise.
Comment 14 H.J. Lu 2018-01-31 21:20:19 UTC
*** Bug 22570 has been marked as a duplicate of this bug. ***
Comment 15 Sourceware Commits 2018-02-06 17:18:33 UTC
The master branch has been updated by Eric Botcazou <ebotcazou@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c20c30f615756ddfccc4bb75c65ccfc1a399466e

commit c20c30f615756ddfccc4bb75c65ccfc1a399466e
Author: Eric Botcazou <ebotcazou@gcc.gnu.org>
Date:   Tue Feb 6 18:15:56 2018 +0100

    Fix PR ld/22263 on SPARC.
    
    This is -fpie -pie generating dynamic relocations in the text section,
    simply because no TLS transitions are applied in PIE mode.  The meat
    of the patch is to turn calls to bfd_link_pic (info) in TLS-related code
    into !bfd_link_executable (info) and there are quite a lot of them...
    
    bfd/
    	* elfxx-sparc.c (sparc_elf_tls_transition): Turn call to bfd_link_pic
    	into call to !bfd_link_executable and tidy up.
    	(_bfd_sparc_elf_check_relocs): Fix formatting and tidy up.
    	<R_SPARC_TLS_LE_HIX22>: Turn call to bfd_link_pic into call to
    	!bfd_link_executable.
    	<R_SPARC_TLS_IE_HI22>: Likewise.
    	<GOT relocations>: Remove useless code, tidy and merge blocks.
    	<R_SPARC_TLS_GD_CALL>: Turn call to bfd_link_pic into call to
    	!bfd_link_executable.
    	<R_SPARC_WPLT30>: Tidy up.
    	(_bfd_sparc_elf_gc_mark_hook): Turn call to bfd_link_pic into call
    	to !bfd_link_executable.
    	(allocate_dynrelocs): Likewise.
    	(_bfd_sparc_elf_relocate_section): Fix formatting and tidy up.
    	<R_SPARC_TLS_GD_HI22>: Merge into...
    	<R_SPARC_TLS_GD_LO10>: ...this.  Adjust 4th argument in call to
    	sparc_elf_tls_transition and remove redundant code.
    	<R_SPARC_TLS_LDM_HI22>: Turn call to bfd_link_pic into call to
    	!bfd_link_executable.
    	<R_SPARC_TLS_LDO_HIX22>: Likewise.
    	<R_SPARC_TLS_LE_HIX22>: Likewise.  Tidy up.
    	<R_SPARC_TLS_LDM_CALL>: Likewise.
    	<R_SPARC_TLS_GD_CALL>: Likewise.  Tidy up.
    	<R_SPARC_TLS_GD_ADD>: Likewise.
    	<R_SPARC_TLS_LDM_ADD>: Likewise.
    	<R_SPARC_TLS_LDO_ADD>: Likewise.
    	<R_SPARC_TLS_IE_LD>: Likewise.
    ld/
    	* testsuite/ld-elf/tls.exp (AFLAGS_PIC): Define on SPARC.
    	(pr22263-1): Pass AFLAGS_PIC to the assembler.
    	* testsuite/ld-sparc/tlspie32.s: Add test for other 3 transitions.
    	* testsuite/ld-sparc/tlspie32.dd: Adjust to above.
    	* testsuite/ld-sparc/tlspie64.s: Add test for other 3 transitions.
    	* testsuite/ld-sparc/tlspie64.dd: Adjust to above.
Comment 16 Khem Raj 2018-07-03 13:13:32 UTC
this bug is present in mips ld too
Comment 17 Rich Felker 2018-07-03 14:37:25 UTC
Yes, see #22570 in the list of duplicates, which includes a proposed patch for MIPS.
Comment 18 Sourceware Commits 2018-11-27 12:43:38 UTC
The master branch has been updated by Tamar Christina <tnfchris@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9fca35fc3486283562a7fcd9eb0ff845b0152d98

commit 9fca35fc3486283562a7fcd9eb0ff845b0152d98
Author: Tamar Christina <tamar.christina@arm.com>
Date:   Tue Nov 27 12:33:21 2018 +0000

    AArch64: Fix regression in Cortex A53 erratum when PIE. (PR ld/23904)
    
    The fix for PR ld/22263 causes TLS relocations using ADRP to be relaxed
    into MOVZ, however this causes issues for the erratum code.
    
    The erratum code scans the input sections looking for ADRP instructions
    and notes their location in the stream.
    
    It then later tries to find them again in order to generate the linker
    stubs.  Due to the relaxation it instead finds a MOVZ and hard aborts.
    
    Since this relaxation is a valid one, and in which case the erratum no
    longer applies, it shouldn't abort but instead just continue.
    
    This changes the TLS relaxation code such that when it finds an ADRP and
    it relaxes it, it removes the erratum entry from the work list by changing
    the stub type into none so the stub is ignored.
    
    The entry is not actually removed as removal is a more expensive operation
    and we have already allocated the memory anyway.
    
    The clearing is done for IE->LE and GD->LE relaxations, and a testcase is
    added for the IE case. The GD case I believe to be impossible to get together
    with the erratum sequence due to the required BL which would break the sequence.
    However to cover all basis I have added the guard there as well.
    
    build on native hardware and regtested on
      aarch64-none-elf, aarch64-none-elf (32 bit host),
      aarch64-none-linux-gnu, aarch64-none-linux-gnu (32 bit host)
    
    Cross-compiled and regtested on
      aarch64-none-linux-gnu, aarch64_be-none-linux-gnu
    
    Testcase in PR23940 tested and works as expected now and benchmarks ran on A53
    showing no regressions and no issues.
    
    bfd/ChangeLog:
    
    	PR ld/23904
    	* elfnn-aarch64.c (_bfd_aarch64_adrp_p): Use existing constants.
    	(_bfd_aarch64_erratum_843419_branch_to_stub): Use _bfd_aarch64_adrp_p.
    	(struct erratum_835769_branch_to_stub_clear_data): New.
    	(_bfd_aarch64_erratum_843419_clear_stub): New.
    	(clear_erratum_843419_entry): New.
    	(elfNN_aarch64_tls_relax): Use it.
    	(elfNN_aarch64_relocate_section): Pass input_section.
    	(aarch64_map_one_stub): Handle branch type none as valid.
    
    ld/ChangeLog:
    
    	PR ld/23904
    	* testsuite/ld-aarch64/aarch64-elf.exp: Add erratum843419_tls_ie.
    	* testsuite/ld-aarch64/erratum843419_tls_ie.d: New test.
    	* testsuite/ld-aarch64/erratum843419_tls_ie.s: New test.
Comment 19 Sourceware Commits 2018-11-27 12:54:23 UTC
The binutils-2_31-branch branch has been updated by Tamar Christina <tnfchris@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=513092d696472fc06cf7812e14160e16b2da5286

commit 513092d696472fc06cf7812e14160e16b2da5286
Author: Tamar Christina <tamar.christina@arm.com>
Date:   Tue Nov 27 12:33:21 2018 +0000

    AArch64: Fix regression in Cortex A53 erratum when PIE. (PR ld/23904)
    
    The fix for PR ld/22263 causes TLS relocations using ADRP to be relaxed
    into MOVZ, however this causes issues for the erratum code.
    
    The erratum code scans the input sections looking for ADRP instructions
    and notes their location in the stream.
    
    It then later tries to find them again in order to generate the linker
    stubs.  Due to the relaxation it instead finds a MOVZ and hard aborts.
    
    Since this relaxation is a valid one, and in which case the erratum no
    longer applies, it shouldn't abort but instead just continue.
    
    This changes the TLS relaxation code such that when it finds an ADRP and
    it relaxes it, it removes the erratum entry from the work list by changing
    the stub type into none so the stub is ignored.
    
    The entry is not actually removed as removal is a more expensive operation
    and we have already allocated the memory anyway.
    
    The clearing is done for IE->LE and GD->LE relaxations, and a testcase is
    added for the IE case. The GD case I believe to be impossible to get together
    with the erratum sequence due to the required BL which would break the sequence.
    However to cover all basis I have added the guard there as well.
    
    build on native hardware and regtested on
      aarch64-none-elf, aarch64-none-elf (32 bit host),
      aarch64-none-linux-gnu, aarch64-none-linux-gnu (32 bit host)
    
    Cross-compiled and regtested on
      aarch64-none-linux-gnu, aarch64_be-none-linux-gnu
    
    Testcase in PR23940 tested and works as expected now and benchmarks ran on A53
    showing no regressions and no issues.
    
    bfd/ChangeLog:
    
    	PR ld/23904
    	* elfnn-aarch64.c (_bfd_aarch64_adrp_p): Use existing constants.
    	(_bfd_aarch64_erratum_843419_branch_to_stub): Use _bfd_aarch64_adrp_p.
    	(struct erratum_835769_branch_to_stub_clear_data): New.
    	(_bfd_aarch64_erratum_843419_clear_stub): New.
    	(clear_erratum_843419_entry): New.
    	(elfNN_aarch64_tls_relax): Use it.
    	(elfNN_aarch64_relocate_section): Pass input_section.
    	(aarch64_map_one_stub): Handle branch type none as valid.
    
    ld/ChangeLog:
    
    	PR ld/23904
    	* testsuite/ld-aarch64/aarch64-elf.exp: Add erratum843419_tls_ie.
    	* testsuite/ld-aarch64/erratum843419_tls_ie.d: New test.
    	* testsuite/ld-aarch64/erratum843419_tls_ie.s: New test.
    
    (cherry picked from commit 9fca35fc3486283562a7fcd9eb0ff845b0152d98)
Comment 20 Sourceware Commits 2019-10-04 09:58:42 UTC
The master branch has been updated by Szabolcs Nagy <nsz@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9cb09e33e04feb12df2aaa6e81d61b82ad609ce5

commit 9cb09e33e04feb12df2aaa6e81d61b82ad609ce5
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Oct 2 19:46:46 2019 +0100

    [PR ld/22263][PR ld/25056] arm: Avoid dynamic TLS relocs in PIE
    
    Dynamic relocs are only needed in an executable for TLS symbols if
    those are defined in an external module and even then TLS access
    can be relaxed to use IE model instead of GD.
    
    Several bfd_link_pic checks are turned into bfd_link_dll checks
    to fix TLS handling in PIE, for the same fix some other targets
    used !bfd_link_executable checks, but that includes relocatable
    objects so dll seems safer (in most cases either should work, since
    dynamic relocations are not applied in relocatable objects).
    
    On arm* fixes
    FAIL: Build pr22263-1
    
    bfd/
    
    	PR ld/22263
    	PR ld/25056
    	* elf32-arm.c (elf32_arm_tls_transition): Use bfd_link_dll instead of
    	bfd_link_pic for TLS checks.
    	(elf32_arm_final_link_relocate): Likewise.
    	(allocate_dynrelocs_for_symbol): Likewise.
Comment 21 Sourceware Commits 2019-10-10 11:29:10 UTC
The binutils-2_33-branch branch has been updated by Szabolcs Nagy <nsz@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b094948c0943c996460cbc9ab3c14207dc520445

commit b094948c0943c996460cbc9ab3c14207dc520445
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Oct 2 19:46:46 2019 +0100

    [PR ld/22263][PR ld/25056] arm: Avoid dynamic TLS relocs in PIE
    
    Dynamic relocs are only needed in an executable for TLS symbols if
    those are defined in an external module and even then TLS access
    can be relaxed to use IE model instead of GD.
    
    Several bfd_link_pic checks are turned into bfd_link_dll checks
    to fix TLS handling in PIE, for the same fix some other targets
    used !bfd_link_executable checks, but that includes relocatable
    objects so dll seems safer (in most cases either should work, since
    dynamic relocations are not applied in relocatable objects).
    
    On arm* fixes
    FAIL: Build pr22263-1
    
    bfd/
    
    	PR ld/22263
    	PR ld/25056
    	* elf32-arm.c (elf32_arm_tls_transition): Use bfd_link_dll instead of
    	bfd_link_pic for TLS checks.
    	(elf32_arm_final_link_relocate): Likewise.
    	(allocate_dynrelocs_for_symbol): Likewise.
Comment 22 Michael Forney 2020-03-05 22:58:00 UTC
I believe riscv64 might have the same issue. I get the same error for the example in the description, and the pr22263-1 test is failing (on current master).
Comment 23 Sergei Trofimovich 2020-03-19 21:41:31 UTC
bug #25694 was reported against riscv64. At a cursory glance seems to be the same  link_pic vs. link_executable mismatch.
Comment 24 Sourceware Commits 2022-04-28 12:52:27 UTC
The master branch has been updated by Andreas Krebbel <krebbel@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=26b1426577b5dcb32d149c64cca3e603b81948a9

commit 26b1426577b5dcb32d149c64cca3e603b81948a9
Author: Stefan Liebler <stli@linux.ibm.com>
Date:   Thu Apr 28 14:29:58 2022 +0200

    s390: Avoid dynamic TLS relocs in PIE
    
    No dynamic relocs are needed for TLS defined in an executable, the
    TP relative offset is known at link time.
    
    Fixes
    FAIL: Build pr22263-1
    
    bfd/
            PR ld/22263
            * elf64-s390.c (elf_s390_tls_transition): Use bfd_link_dll
            instead of bfd_link_pic for TLS.
            (elf_s390_check_relocs): Likewise.
            (allocate_dynrelocs): Likewise.
            (elf_s390_relocate_section): Likewise.
Comment 25 Sourceware Commits 2022-04-28 12:54:28 UTC
The binutils-2_38-branch branch has been updated by Andreas Krebbel <krebbel@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=82a5bb730a16f8c7962568030268e784b4fb42c8

commit 82a5bb730a16f8c7962568030268e784b4fb42c8
Author: Stefan Liebler <stli@linux.ibm.com>
Date:   Thu Apr 28 14:29:58 2022 +0200

    s390: Avoid dynamic TLS relocs in PIE
    
    No dynamic relocs are needed for TLS defined in an executable, the
    TP relative offset is known at link time.
    
    Fixes
    FAIL: Build pr22263-1
    
    bfd/
            PR ld/22263
            * elf64-s390.c (elf_s390_tls_transition): Use bfd_link_dll
            instead of bfd_link_pic for TLS.
            (elf_s390_check_relocs): Likewise.
            (allocate_dynrelocs): Likewise.
            (elf_s390_relocate_section): Likewise.
    
    (cherry picked from commit 26b1426577b5dcb32d149c64cca3e603b81948a9)
Comment 26 Sourceware Commits 2022-04-28 12:55:11 UTC
The binutils-2_37-branch branch has been updated by Andreas Krebbel <krebbel@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e20961e9f9d058fab00ce511b429570a901396e6

commit e20961e9f9d058fab00ce511b429570a901396e6
Author: Stefan Liebler <stli@linux.ibm.com>
Date:   Thu Apr 28 14:29:58 2022 +0200

    s390: Avoid dynamic TLS relocs in PIE
    
    No dynamic relocs are needed for TLS defined in an executable, the
    TP relative offset is known at link time.
    
    Fixes
    FAIL: Build pr22263-1
    
    bfd/
            PR ld/22263
            * elf64-s390.c (elf_s390_tls_transition): Use bfd_link_dll
            instead of bfd_link_pic for TLS.
            (elf_s390_check_relocs): Likewise.
            (allocate_dynrelocs): Likewise.
            (elf_s390_relocate_section): Likewise.
    
    (cherry picked from commit 26b1426577b5dcb32d149c64cca3e603b81948a9)
Comment 27 matoro 2022-07-04 19:28:08 UTC
Hi, could this be extended to riscv please?
Comment 28 Alan Modra 2023-05-26 01:47:26 UTC
A number of targets that I test regularly fail the "Build pr22263-1" test for various reasons.

arm-linux-gnueabi fails with ld errors "undefined reference to `__aeabi_read_tp'"
ia64-linux-gnu fails with a gas warning "Explicit stops are ignored in auto mode"
m68k-linux-gnu fails with ld errors "undefined reference to `__m68k_read_tp'"
microblaze-linux-gnu fails with ld errors "undefined reference to `__tls_get_addr'"
nios2-linux-gnu, s390-linux-gnu and sh4-linux-gnu have a tprel reloc in .got
riscv64-linux-gnu has a dynamic relocation in text

So only riscv really fails the pr.  The rest fail due to test issues or failure to implement linker optimisations.
Comment 29 Alan Modra 2023-05-26 01:49:44 UTC
I'll split the test into two parts, checking -z text and the tprel optimisation
Comment 30 Sourceware Commits 2023-05-26 03:12:34 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9dc7ab5c01347b303c578e0a9221248dbe227b59

commit 9dc7ab5c01347b303c578e0a9221248dbe227b59
Author: Alan Modra <amodra@gmail.com>
Date:   Fri May 26 11:51:57 2023 +0930

    PR22263 ld test
    
    A number of targets that I test regularly fail the "Build pr22263-1"
    test for various reasons.
    
    arm-linux-gnueabi: "undefined reference to `__aeabi_read_tp'"
    ia64-linux-gnu: "Explicit stops are ignored in auto mode"
    m68k-linux-gnu: "undefined reference to `__m68k_read_tp'"
    microblaze-linux-gnu: "undefined reference to `__tls_get_addr'"
    nios2-linux-gnu, s390-linux-gnu and sh4-linux-gnu have a tprel reloc in .got
    riscv64-linux-gnu has a dynamic relocation in text
    
    So only riscv really fails the pr.  The rest fail due to test issues
    or lack of a linker optimisation.  Lack of an optimisation isn't
    really a fail, but it's worth keeping the test to ensure those
    optimisations don't regress.  The xfail targets may not be an
    exhaustive list.  This just tidies test results for those for which I
    have cross compilers installed.
    
            PR 22263
            * testsuite/ld-elf/tls.exp: Split pr22263 test into two parts,
            one to check for -z text errors, the other to check tprel
            linker optimisation.  Supply needed symbols and assembler flags.
            xfail the linker optimisation on targets known to fail.
Comment 31 Sourceware Commits 2023-05-27 01:25:07 UTC
The master branch has been updated by Nelson Chu <nelsonc1225@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=225df051d3d4cf714d1791b9035966a6686b3f3d

commit 225df051d3d4cf714d1791b9035966a6686b3f3d
Author: Nelson Chu <nelson@nelson.ba.rivosinc.com>
Date:   Thu May 4 17:08:50 2023 +0800

    [PR ld/22263][PR ld/25694] RISC-V: Avoid dynamic TLS relocs in PIE.
    
    Lots of targets already fixed the TEXTREL problem for TLS in PIE.
    
    * For PR ld/25694,
    In the check_reloc, refer to spare and loongarch, they don't need to reserve
    any local dynamic reloc for TLS LE in pie/pde, and similar to other targets.
    So it seems like riscv was too conservative to estimate the TLS LE before.
    Just break and don't goto static_reloc for TLS LE in pie/pde can fix the
    TEXTREL problem.
    
    * For PR ld/22263,
    The risc-v code for TLS GD/IE in the relocate_section seems same as MIPS port.
    So similar to MIPS, pr22570, commits 9143e72c6d4d and 1cb83cac9a89, it seems
    also the right way to do the same thing for risc-v.
    
    On risc-v, fixes
    FAIL: Build pr22263-1
    
    RISC-V haven't supported the TLS transitions, so will need the same fix (use
    bfd_link_dll) in the future.
    
    bfd/
            PR ld/22263
            PR ld/25694
            * elfnn-riscv.c (riscv_elf_check_relocs): Replace bfd_link_pic with
            bfd_link_dll for TLS IE.  Don't need to reserve the local dynamic
            relocation for TLS LE in pie/pde, and report error in pic just like
            before.
            (riscv_elf_relocate_section): For TLS GD/IE, use bfd_link_dll rather
            than !bfd_link_pic in determining the dynamic symbol index.  Avoid
            the index of -1.
Comment 32 Sourceware Commits 2023-06-01 04:31:51 UTC
The master branch has been updated by Nelson Chu <nelsonc1225@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=20ef84ed2abb990da08d90e1c978f96d29f40606

commit 20ef84ed2abb990da08d90e1c978f96d29f40606
Author: Nelson Chu <nelson@rivosinc.com>
Date:   Fri May 26 18:05:34 2023 +0800

    [PR ld/22263][PR ld/24676] RISC-V: Avoid spurious R_RISCV_NONE for TLS GD/IE.
    
    For TLS GD/IE, add the same condition with the relocate_section in the
    allocate_dynrelocs, to make sure we won't reserve redundant spaces
    for dynamic relocations since the conservative estimatation.
    
    After applying this patch, ld seems no longer generate the spurious
    R_RISCV_NONE for pr22263-1 test, and the test in pr24676.
    
    bfd/
            PR ld/22263
            PR ld/24676
            * elfnn-riscv.c (RISCV_TLS_GD_IE_NEED_DYN_RELOC): New defined.
            Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations,
            and INDX will be the dynamic index.
            (allocate_dynrelocs): Don't reserve extra spaces in the rela.got
            if RISCV_TLS_GD_IE_NEED_DYN_RELOC set need_reloc to false.  This
            condition needs to be same as relocate_section.
            (relocate_section): Likewise, use the same condition as
            allocate_dynrelocs.