From patchwork Sat May 27 01:36:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nelson Chu X-Patchwork-Id: 99734 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp68172vqr; Fri, 26 May 2023 18:36:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mAUDzEziRxxB4orKUYSDrtW7HlxAKACwj154U4BIbSsa9LPxVfHIdn3UHMrFKUAV27fVl X-Received: by 2002:a17:906:da83:b0:95e:d74d:c4e6 with SMTP id xh3-20020a170906da8300b0095ed74dc4e6mr3726688ejb.25.1685151394307; Fri, 26 May 2023 18:36:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685151394; cv=none; d=google.com; s=arc-20160816; b=Y0i2mYQe9c1hM0LHFMIUMTr6cVNj0BwsOq4DK33vCUFTvNFWuqQEOQH16LAIh8JFBL qpQ/IOTGGUt0lAU872RDU01Cn2lYyVgEaSQdorZG6YnANjHJlzkoz0THBnY6Ihs72fYx RzM435UHQAmNuDRYBVbCAnmUdRmrvjTDPs4bcIK8k6fKBnTkLIbVZqQAAvQw4AMeeie8 GGY4/t4rtoUgsKlGZqxYo3zJnU6WfNYwB6WA4nYvHQSenn1EABBWZbB4QTLb39zppFfJ uVpALv0EJ+yJQ0Azwb+fkt/KGbNwMzWxy5NJai4x9/OkLJGnXaIeB7ArHESBlFNvb++Q HUZA== 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=GCZNPgEd7bXjYeRfCM4/QfLNP3EoW9l38Z8Cpngyrow=; b=ncjFm5hkyp/vVpQfeE1o3E820Lt764/RFa+L8BHyZ1ZQgOLFcznsXkHe/TrdyrRd7X AwGmiFVEPKi07vCKRskmeqpsrCQodQl6oC3kaeEdH+9YIJpel0J4oh0tsze6JZUhCSz9 IsAmB0lp+kdwQpNibmuYNJWyvleFSr2iD6DVpQ499N8b5VX6EJbyR2CCawPJUNOJ8YOW OwfgeIuicPMEBSDL7XPZ0Nkfk+l2b9IjDZ7dchFKewj0ne/zWK9lb7YIg7mx05IjIkhB 7DTYo1a+58J22SjoeAMVqBPsbgXzH3qb1Ncy1xMV4acUUJSarT8Bch2PVJVPDfCsYGuh /Fyw== 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 dv23-20020a170906b81700b009738d042995si3302235ejb.813.2023.05.26.18.36.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 18:36:34 -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 A6B08385773E for ; Sat, 27 May 2023 01:36:32 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from NelsondeMBP.localdomain (114-25-75-174.dynamic-ip.hinet.net [114.25.75.174]) by sourceware.org (Postfix) with ESMTP id 20D663858C2C for ; Sat, 27 May 2023 01:36:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20D663858C2C 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 435A4D9A25C; Sat, 27 May 2023 09:36:22 +0800 (CST) From: Nelson Chu To: binutils@sourceware.org, jim.wilson.gcc@gmail.com, palmer@dabbelt.com, andrew@sifive.com Cc: nelson@rivosinc.com Subject: [PATCH] [PR ld/22263] RISC-V: Avoid spurious R_RISCV_NONE for pr22263-1 test. Date: Sat, 27 May 2023 09:36:20 +0800 Message-Id: <20230527013620.65127-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.2 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, PDS_RDNS_DYNAMIC_FP, 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?1767009308396566212?= X-GMAIL-MSGID: =?utf-8?q?1767009308396566212?= 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. bfd/ PR ld/22263 * 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. --- 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)))) +/* 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_dynrelocs 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) != 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; \ + if ((bfd_link_dll (INFO) || (INDX) != 0) \ + && ((H) == NULL \ + || ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \ + || (H)->root.type != bfd_link_hash_undefweak)) \ + (NEED_RELOC) = true; \ + } \ + while (0) + /* Internal relocations used exclusively by the relaxation pass. */ #define R_RISCV_DELETE (R_RISCV_max + 1) @@ -1297,18 +1316,24 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) dyn = htab->elf.dynamic_sections_created; if (tls_type & (GOT_TLS_GD | GOT_TLS_IE)) { + int indx = 0; + bool need_reloc = 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 += 2 * RISCV_ELF_WORD_BYTES; - htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela); + if (need_reloc) + htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela); } /* TLS_IE needs one dynamic reloc and one GOT slot. */ if (tls_type & GOT_TLS_IE) { s->size += RISCV_ELF_WORD_BYTES; - htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); + if (need_reloc) + htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); } } else @@ -2882,20 +2907,10 @@ riscv_elf_relocate_section (bfd *output_bfd, abort (); 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; + RISCV_TLS_GD_IE_NEED_DYN_RELOC (info, dyn, h, indx, need_relocs); /* The GOT entries have not been initialized yet. Do it now, and emit any relocations. */ - 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; - if (tls_type & GOT_TLS_GD) { if (need_relocs)