From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7803) id 64EB8385700D; Sat, 27 May 2023 01:25:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64EB8385700D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nelson Chu To: bfd-cvs@sourceware.org Subject: [binutils-gdb] [PR ld/22263][PR ld/25694] RISC-V: Avoid dynamic TLS relocs in PIE. X-Act-Checkin: binutils-gdb X-Git-Author: Nelson Chu X-Git-Refname: refs/heads/master X-Git-Oldrev: a70d5ba34cb64bbbb7df4f3055cf5b1967bdf61e X-Git-Newrev: 225df051d3d4cf714d1791b9035966a6686b3f3d Message-Id: <20230527012505.64EB8385700D@sourceware.org> Date: Sat, 27 May 2023 01:25:05 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 May 2023 01:25:05 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D225df051d3d4= cf714d1791b9035966a6686b3f3d commit 225df051d3d4cf714d1791b9035966a6686b3f3d Author: Nelson Chu Date: Thu May 4 17:08:50 2023 +0800 [PR ld/22263][PR ld/25694] RISC-V: Avoid dynamic TLS relocs in PIE. =20 Lots of targets already fixed the TEXTREL problem for TLS in PIE. =20 * For PR ld/25694, In the check_reloc, refer to spare and loongarch, they don't need to re= serve any local dynamic reloc for TLS LE in pie/pde, and similar to other tar= gets. So it seems like riscv was too conservative to estimate the TLS LE befo= re. Just break and don't goto static_reloc for TLS LE in pie/pde can fix the TEXTREL problem. =20 * For PR ld/22263, The risc-v code for TLS GD/IE in the relocate_section seems same as MIP= S port. So similar to MIPS, pr22570, commits 9143e72c6d4d and 1cb83cac9a89, it = seems also the right way to do the same thing for risc-v. =20 On risc-v, fixes FAIL: Build pr22263-1 =20 RISC-V haven't supported the TLS transitions, so will need the same fix= (use bfd_link_dll) in the future. =20 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 dynam= ic 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 r= ather than !bfd_link_pic in determining the dynamic symbol index. Av= oid the index of -1. Diff: --- bfd/elfnn-riscv.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 75af040cf92..762ea231c0b 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -824,7 +824,7 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info= *info, break; =20 case R_RISCV_TLS_GOT_HI20: - if (bfd_link_pic (info)) + if (bfd_link_dll (info)) info->flags |=3D DF_STATIC_TLS; if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE)) @@ -920,11 +920,12 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_in= fo *info, goto static_reloc; =20 case R_RISCV_TPREL_HI20: + /* This is not allowed in the pic, but okay in pie. */ if (!bfd_link_executable (info)) return bad_static_reloc (abfd, r_type, h); if (h !=3D NULL) riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE); - goto static_reloc; + break; =20 case R_RISCV_HI20: if (bfd_link_pic (info)) @@ -2880,24 +2881,20 @@ riscv_elf_relocate_section (bfd *output_bfd, if (htab->elf.srelgot =3D=3D NULL) abort (); =20 - if (h !=3D NULL) - { - bool dyn, pic; - dyn =3D htab->elf.dynamic_sections_created; - pic =3D bfd_link_pic (info); - - if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h) - && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h))) - indx =3D h->dynindx; - } + bool dyn =3D elf_hash_table (info)->dynamic_sections_created; + if (h !=3D NULL + && h->dynindx !=3D -1 + && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h) + && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h))) + indx =3D h->dynindx; =20 /* The GOT entries have not been initialized yet. Do it now, and emit any relocations. */ - if ((bfd_link_pic (info) || indx !=3D 0) + if ((bfd_link_dll (info) || indx !=3D 0) && (h =3D=3D NULL || ELF_ST_VISIBILITY (h->other) =3D=3D STV_DEFAULT || h->root.type !=3D bfd_link_hash_undefweak)) - need_relocs =3D true; + need_relocs =3D true; =20 if (tls_type & GOT_TLS_GD) {