From patchwork Thu May 4 09:08:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nelson Chu X-Patchwork-Id: 90003 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp162949vqo; Thu, 4 May 2023 02:09:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ737AvuW4k26KE2VgMvB4ThiY6ty52nqjPg0Tet40erKQd9+pSom3skdIRUVttjeV4OCfvd X-Received: by 2002:a17:907:6d99:b0:962:9ffa:bdf4 with SMTP id sb25-20020a1709076d9900b009629ffabdf4mr6323020ejc.2.1683191342296; Thu, 04 May 2023 02:09:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683191342; cv=none; d=google.com; s=arc-20160816; b=ayPivQKlFtztwpPdjYYXA/JGzx1gh3PwwK1ERJnBA5zuJkHVnRDzOPjD3MxKcX3Z5W 6PO9BSjc+ZawU5mIXnnI328+ga9StGlOGmmSXNUIAmj9Q67kMhBqAokyJdzqSXAiDOvL otJ6jU1wPuWAPbThY8TXOoY5UPv7bVSv2Q5ovAqT4+q96oHCjpDIst3hhnMtBbOfJoe7 dxnJOKPjC3VZuoDo14LprIx+wZJrdb97H8h96QsTjQ3/ti8batQgna4DYY06egZUa2xw eut0VPNN87t1aqxAwmMR1t8ftGbpgljno7a0tS9VsRNKoCfgak9+5P3xnZL0EllIJ+QP jsZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dmarc-filter :delivered-to; bh=ifBw1Z6VBXhLFMr4Q3LqO8gjJPoZlDAo2Ea4KMa1cT0=; b=w+vGd4/DAQArR7iBhn/XH0rwrHPpx4O7X7lfF0riYyQZz9LMND4l8xgcLZRyD6F7d9 RDvISwyKvo/MpB1cdPAuLUF0HvmEHlHBZRSF54519yUnfQ+0Y6H+KraFg5bSbC6GF/N8 iNvaR4++xOGbSerSpJLnCMGDT0DgHXGwa+HXGd8aCN/bIdWgt1u62xuQDmuq8h1WI46Q pgiGT7E5afdN3U5ubX1QiOvVyct8CrrWk2n7SL8fTjcO+UsoCRltXoXPGm7CGu1772Dm V+J0kP685R7NV2JVHksfsoXEmB40ahWAw9PfOGJhHfSbZ1B5pQIfbuHbb7br9F5FM3iU 1BLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y12-20020a170906558c00b0094a6d38db2fsi15377447ejp.294.2023.05.04.02.09.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 02:09:02 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5A97385734F for ; Thu, 4 May 2023 09:09:00 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from NelsondeMBP.localdomain (114-25-63-225.dynamic-ip.hinet.net [114.25.63.225]) by sourceware.org (Postfix) with ESMTP id 79C6D3858D28 for ; Thu, 4 May 2023 09:08:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 79C6D3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=NelsondeMBP.localdomain Received: by NelsondeMBP.localdomain (Postfix, from userid 501) id 9DB7FC71CBE; Thu, 4 May 2023 17:08:52 +0800 (CST) From: Nelson Chu To: binutils@sourceware.org, jim.wilson.gcc@gmail.com, palmer@dabbelt.com, andrew@sifive.com Cc: nelson@rivosinc.com, Nelson Chu Subject: [PATCH] [PR ld/22263] [PR ld/25694] RISC-V: Avoid dynamic TLS relocs in PIE. Date: Thu, 4 May 2023 17:08:50 +0800 Message-Id: <20230504090850.91004-1-nelson@rivosinc.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) MIME-Version: 1.0 X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_STOCKGEN, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764954044830843660?= X-GMAIL-MSGID: =?utf-8?q?1764954044830843660?= 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 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. --- 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 |= 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 != 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 == NULL) abort (); - if (h != NULL) - { - bool dyn, pic; - dyn = htab->elf.dynamic_sections_created; - pic = bfd_link_pic (info); - - if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h) - && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h))) - indx = h->dynindx; - } + bool dyn = elf_hash_table (info)->dynamic_sections_created; + if (h != NULL + && h->dynindx != -1 + && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h) + && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h))) + indx = h->dynindx; /* The GOT entries have not been initialized yet. Do it now, and emit any relocations. */ - if ((bfd_link_pic (info) || indx != 0) + if ((bfd_link_dll (info) || indx != 0) && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT || h->root.type != bfd_link_hash_undefweak)) - need_relocs = true; + need_relocs = true; if (tls_type & GOT_TLS_GD) {