From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id 977C43858401 for ; Sat, 27 May 2023 01:45:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 977C43858401 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-397f10f861eso1083105b6e.0 for ; Fri, 26 May 2023 18:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1685151935; x=1687743935; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=d4IjxS/eTkAeUT+Gj0DCDl9bizVkM6PdwgLcRy3OJR8=; b=oqbY/qxuzkPV6F2kVwFnKegp50ITGs8lLcUNe9CCo8IhskUc1GtLgY3bU6qiyvmm1h rAphuI0bF+GRBz/kbtf6QjBeBDeTkTTE3A7w4gC1/3nXWzpnmg7ahzRWkpAjffbmTh54 BZGAKSDVfXWtqpem7Z15cPvNuUv1y/X/5zwM4edT9W6P4Ngu/nEOwUJh8p4MLNaeKBf8 zV7L1Cd6u3LvQSdGdvNbLG6nNr8I718ZKHXOqNuqi/L4i7G2MzD3DPOwASWlqFGxCK9/ 3fcJENCWvajSiyvNgT2d677zj+cPdnpR+vNXqmE4itYYQzerK9oLR93wdT4Tl0CAexy8 SVLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685151935; x=1687743935; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d4IjxS/eTkAeUT+Gj0DCDl9bizVkM6PdwgLcRy3OJR8=; b=F27b5P1KwjnsNSMGg1zzVw7qqtT5LGendiHX3n4ig88ZZNr9sc6TB0p4X2u9eT4QnY UBjGLoVWb2ISuvz+ieeiP0XgYFNw2RKt5AD9bBbK+4ldPWqvcp/5Nck7QZo/LnURWCqT olU7d2oARCMP81/eIqPH7UX2zZCfVikzHz7XYrq2J9lkeN/p2IoK/4QrNypnwfArkH2j waT/FoFXXBFsNCDnx0ySRKNd5TiM9IeBhZZT49ZypAgbQWx3o/1djs7uUBesp34L2H4D lRH3HqFRZWeVLoWdeSea05T/lR8cybnBLV4UofKWWpN7o+dSm/Goiu3e1pGOVHnpYooZ q3kg== X-Gm-Message-State: AC+VfDxmg9MWUkAxPHC9VhEBe88nHiNnapRYKh0HixYv9GHHBoVfYyMR JSRCmpku1inyOrMAu2x2KX0jc+oNzbzXoyzWzZ61A8WSFJ44rzi3Tu5SJw== X-Google-Smtp-Source: ACHHUZ7tocUWLOm+AJg8yuQm/PSHW6YhcbWGwDYStqM8aSoCTbOs7caQJ0PpHBmBjRlkHXnFB1gX2ThVrYfsaI29lV4= X-Received: by 2002:a05:6808:219d:b0:398:116e:726 with SMTP id be29-20020a056808219d00b00398116e0726mr507957oib.13.1685151935714; Fri, 26 May 2023 18:45:35 -0700 (PDT) MIME-Version: 1.0 References: <20230504090850.91004-1-nelson@rivosinc.com> In-Reply-To: <20230504090850.91004-1-nelson@rivosinc.com> From: Nelson Chu Date: Sat, 27 May 2023 09:45:24 +0800 Message-ID: Subject: Re: [PATCH] [PR ld/22263] [PR ld/25694] RISC-V: Avoid dynamic TLS relocs in PIE. To: binutils@sourceware.org, jim.wilson.gcc@gmail.com, palmer@dabbelt.com, andrew@sifive.com Cc: Nelson Chu Content-Type: multipart/alternative; boundary="00000000000085722805fca302f2" X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,HTML_MESSAGE,KAM_STOCKGEN,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --00000000000085722805fca302f2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Committed since it should fix the pr22263-1 test. There is still a problem mentioned by Andreas Schwab in the pr25694, that ld still generate the unexpected R_RISCV_NONE for the pr22263-1 test after applying this patch. Thanks for the tips from Alan that we should have the same condition checked for this in the relocate_section and allocate_dynrelocs. So here is the following fix for pr22263, https://sourceware.org/pipermail/binutils/2023-May/127653.html After applying the fix, it seems ld no longer generates the R_RISCV_NONE reloc for TLS GD/IE with -pie in pr22263-1 test. However, passing the riscv-gnu-toolchain regressions, so looks well so far. Thanks Nelson On Thu, May 4, 2023 at 5:08=E2=80=AFPM Nelson Chu wro= te: > From: Nelson Chu > > 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 wi= th > 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 li= ke > 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. > --- > 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 a23b91ac15c..52dbf55a9f7 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; > > 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_info *info, > goto static_reloc; > > 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; > > case R_RISCV_HI20: > if (bfd_link_pic (info)) > @@ -2797,24 +2798,20 @@ riscv_elf_relocate_section (bfd *output_bfd, > if (htab->elf.srelgot =3D=3D NULL) > abort (); > > - 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_create= d; > + 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; > > /* 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; > > if (tls_type & GOT_TLS_GD) > { > -- > 2.39.2 (Apple Git-143) > > --00000000000085722805fca302f2--