From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7803) id 1BD743858D20; Thu, 1 Jun 2023 04:31:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BD743858D20 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/24676] RISC-V: Avoid spurious R_RISCV_NONE for TLS GD/IE. X-Act-Checkin: binutils-gdb X-Git-Author: Nelson Chu X-Git-Refname: refs/heads/master X-Git-Oldrev: 6313825cbf834b1852007707cfff2ccd3b0dbd6b X-Git-Newrev: 20ef84ed2abb990da08d90e1c978f96d29f40606 Message-Id: <20230601043149.1BD743858D20@sourceware.org> Date: Thu, 1 Jun 2023 04:31:49 +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: Thu, 01 Jun 2023 04:31:49 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D20ef84ed2abb= 990da08d90e1c978f96d29f40606 commit 20ef84ed2abb990da08d90e1c978f96d29f40606 Author: Nelson Chu 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. =20 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. =20 After applying this patch, ld seems no longer generate the spurious R_RISCV_NONE for pr22263-1 test, and the test in pr24676. =20 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. Diff: --- bfd/elfnn-riscv.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 762ea231c0b..30d2faa405d 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -111,6 +111,25 @@ || (bfd_link_pic (INFO) \ && SYMBOL_REFERENCES_LOCAL ((INFO), (H)))) =20 +/* Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations, and INDX= will + be the dynamic index. PR22263, use the same check in allocate_dynreloc= s and + riscv_elf_relocate_section for TLS GD/IE. */ +#define RISCV_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \ + do \ + { \ + 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; \ + 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_RELOC) =3D true; \ + } \ + while (0) + /* Internal relocations used exclusively by the relaxation pass. */ #define R_RISCV_DELETE (R_RISCV_max + 1) =20 @@ -1297,18 +1316,24 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, = void *inf) dyn =3D htab->elf.dynamic_sections_created; if (tls_type & (GOT_TLS_GD | GOT_TLS_IE)) { + int indx =3D 0; + bool need_reloc =3D false; + RISCV_TLS_GD_IE_NEED_DYN_RELOC(info, dyn, h, indx, need_reloc); + /* TLS_GD needs two dynamic relocs and two GOT slots. */ if (tls_type & GOT_TLS_GD) { s->size +=3D 2 * RISCV_ELF_WORD_BYTES; - htab->elf.srelgot->size +=3D 2 * sizeof (ElfNN_External_Rela); + if (need_reloc) + htab->elf.srelgot->size +=3D 2 * sizeof (ElfNN_External_Rela); } =20 /* TLS_IE needs one dynamic reloc and one GOT slot. */ if (tls_type & GOT_TLS_IE) { s->size +=3D RISCV_ELF_WORD_BYTES; - htab->elf.srelgot->size +=3D sizeof (ElfNN_External_Rela); + if (need_reloc) + htab->elf.srelgot->size +=3D sizeof (ElfNN_External_Rela); } } else @@ -2882,20 +2907,10 @@ riscv_elf_relocate_section (bfd *output_bfd, abort (); =20 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; + RISCV_TLS_GD_IE_NEED_DYN_RELOC (info, dyn, h, indx, need_relocs); =20 /* The GOT entries have not been initialized yet. Do it now, and emit any relocations. */ - 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; - if (tls_type & GOT_TLS_GD) { if (need_relocs)