From patchwork Sat Oct 14 05:03:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 152897 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2305915vqb; Fri, 13 Oct 2023 22:03:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXcjXoJ1glr+qC+0+2ZDWA8ggeZz9o1vkeMbcmAKDdC8e3EpgXIGOEmP7WptykEZ9wnNez X-Received: by 2002:ac8:1383:0:b0:418:11de:ce1 with SMTP id h3-20020ac81383000000b0041811de0ce1mr25584564qtj.24.1697259837429; Fri, 13 Oct 2023 22:03:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697259837; cv=pass; d=google.com; s=arc-20160816; b=reGBgtpblsG0ZYYWOufOXZLVEZkViEbPgAb6pVobNBXys42FJ1khSSf214TW4mllco 9o/JaGFl/xw1BUZS+3ZoTqnKPJ6nqhaLWaewEina77b0uAPNkZfzpMMozx/z4AdVRBWg hfYYHk5LzIok+dWY1aml1IjpARP/sm8fGqDsq1VP+3Ju08+BzPaHxV43zmF9hY7XTn0R sMQWGfwiINFgM9WPWfgOIN/C0K01Djf+m5hXQTj62772pspdU8bNb5nCyk51kiRYs750 BMO92JKPc1ycB+EGNDnVCuyUnywDgCsIEQkIU4qnOMvQSNvMBxMl5I4QCtX3wyzZIfax mOtQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dmarc-filter:arc-filter:delivered-to; bh=PqOf4wByx4soVQoeC9mMLesAEUJ4c6BFbGklwcykFYw=; fh=Q+ZPQdyJDzJx4BfovHNTR1lP4msTSfN6TDra8V1lyf8=; b=D2gVMqegPFuAhX9gmapDg01opfZ4v0PXxI3Y03fTttLZw6AiLPRI0VZwJDJoePqErD H2zr4oePWfxhBeeW+Ya1l6Nuaswx686f73AjMcXZbV14tdtha8G7Vl0wgFso48ujD2KE /C/OVRRS+FO1BWb6m3tFGmHtaZT3E3E1+iL0gfHh1ztKIgnAB7l7N134p6fnFKBS3cMX iKWqQAP1ckXnYgpt7Nc+p8aq5x5pQy2XLQmKL73mGZNvQXFOr005FwIiEMduSRfOzMRQ jZGVayT9meOMHSNFUsOqikHdPJIRZdxfZ38MWyWMRgQrcnob5/+Eg5jLAdYuHS/cQd/Z sn7A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@irq.a4lg.com header.s=2017s01 header.b=aiJ9TcT9; arc=pass (i=1); 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=irq.a4lg.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o11-20020a05622a008b00b004182092dcacsi2237116qtw.321.2023.10.13.22.03.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 22:03:57 -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; dkim=pass header.i=@irq.a4lg.com header.s=2017s01 header.b=aiJ9TcT9; arc=pass (i=1); 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=irq.a4lg.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 377AC3857725 for ; Sat, 14 Oct 2023 05:03:57 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-sender-0.a4lg.com (mail-sender.a4lg.com [153.120.152.154]) by sourceware.org (Postfix) with ESMTPS id 9BA1A385771C for ; Sat, 14 Oct 2023 05:03:50 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9BA1A385771C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=153.120.152.154 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697259832; cv=none; b=EaXTljLxLrNN43WJpJD2cBj+X+EfzZ5h13eq9q2OUTq1Tr3XtzGx8Cb79uK552akWNEquEEOJUd24v1H9qFY5eiFVbf6ZdjfjpdN1GxRcH7sQRe7YubTGWGYUsqBdt9zRlnOq9c92KWwAziJDBmhaNn+ffr863eVMjMFxDmWlXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697259832; c=relaxed/simple; bh=S1NvQq5pXdEGsN9f8bSa8PKAJxZA70CbfvMwaohNoo0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=Tw1ZYzm2lVz3dph3OTpVBE97sh+AzIC8mUSv8Tt7LjMjqu7Sr/hzu6bh982rmP43C3bFmtHFbsd1zkjJrr37w4Zxfn/oXIpf1XaDusa0VWQMPfliR624U3QsB5KTVzciIQk/6Bq8qK8OR4jcV34IYXsrUconZ+4foCHngtywQuw= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9BA1A385771C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=irq.a4lg.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=irq.a4lg.com Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id 70A77300089; Sat, 14 Oct 2023 05:03:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1697259826; bh=PqOf4wByx4soVQoeC9mMLesAEUJ4c6BFbGklwcykFYw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=aiJ9TcT9DSZzwwCb6IUoS0QvV6VzcCAA7NdjdyCYxgrKQJ2EZC3DVYYZZDg8TZwMv WGtMkHFYvBmySA/OnwlGk3OvqGWYE7VQxIWma+JozmVQFxS9EtkCNhY/tAnBz5HMFF wABgut5zc81b8Rrlubkew2rNiXIU5XMZCSOAKwZ4= From: Tsukasa OI To: Tsukasa OI , Palmer Dabbelt , Andrew Waterman , Jim Wilson , Nelson Chu , Kito Cheng Cc: binutils@sourceware.org Subject: [PATCH 1/2] RISC-V: Group relaxation features Date: Sat, 14 Oct 2023 05:03:21 +0000 Message-ID: <08e0064b49da8f235f75a45cc04a55e242edca97.1697259797.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_MANYTO, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779705931080849366 X-GMAIL-MSGID: 1779705931080849366 From: Tsukasa OI It does not only deduplicate multiple relaxation feature detection, but enable more complex features so that querying the feature availability will get too slow if we perform it per-relocation (not per-section). Even if that wouldn't happen any time soon, it will improve the maintainability around the linker relaxation code. bfd/ChangeLog: * elfnn-riscv.c (RISCV_RELAX_RVC, RISCV_RELAX_GP): New. (relax_func_t): Add new relax_features argument. (_bfd_riscv_relax_call): Likewise. Move feature detection to _bfd_riscv_relax_section. Use bool for simplicity. (_bfd_riscv_relax_lui): Likewise. Move feature detection to _bfd_riscv_relax_section. (_bfd_riscv_relax_tls_le): Likewise but features are not used. (_bfd_riscv_relax_align): Likewise but features are not used. (_bfd_riscv_relax_pc): Likewise. Move feature detection to _bfd_riscv_relax_section. (_bfd_riscv_relax_section): Detect relaxation-related features and pass the flags to each relaxation function. --- bfd/elfnn-riscv.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 09aa7be225ef..15652a08296d 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4085,6 +4085,10 @@ _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info) return false; } +/* Enabled relaxation features to use. */ +#define RISCV_RELAX_RVC 0x01U +#define RISCV_RELAX_GP 0x02U + /* A second format for recording PC-relative hi relocations. This stores the information required to relax them to GP-relative addresses. */ @@ -4471,7 +4475,8 @@ typedef bool (*relax_func_t) (bfd *, asection *, asection *, Elf_Internal_Rela *, bfd_vma, bfd_vma, bfd_vma, bool *, riscv_pcgp_relocs *, - bool undefined_weak); + bool undefined_weak, + unsigned relax_features); /* Relax AUIPC + JALR into JAL. */ @@ -4484,13 +4489,15 @@ _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again, riscv_pcgp_relocs *pcgp_relocs, - bool undefined_weak ATTRIBUTE_UNUSED) + bool undefined_weak ATTRIBUTE_UNUSED, + unsigned relax_features) { bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset); bool near_zero = (symval + RISCV_IMM_REACH / 2) < RISCV_IMM_REACH; + bool rvc = (relax_features & RISCV_RELAX_RVC) != 0; bfd_vma auipc, jalr; - int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC; + int rd, r_type, len = 4; /* If the call crosses section boundaries, an alignment directive could cause the PC-relative offset to later increase, so we need to add in the @@ -4590,15 +4597,16 @@ _bfd_riscv_relax_lui (bfd *abfd, bfd_vma reserve_size, bool *again, riscv_pcgp_relocs *pcgp_relocs, - bool undefined_weak) + bool undefined_weak, + unsigned relax_features) { struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info); bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; /* Can relax to x0 even when gp relaxation is disabled. */ - bfd_vma gp = htab->params->relax_gp + bfd_vma gp = (relax_features & RISCV_RELAX_GP) != 0 ? riscv_global_pointer_value (link_info) : 0; - int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC; + bool use_rvc = (relax_features & RISCV_RELAX_RVC) != 0; BFD_ASSERT (rel->r_offset + 4 <= sec->size); @@ -4703,7 +4711,8 @@ _bfd_riscv_relax_tls_le (bfd *abfd, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again, riscv_pcgp_relocs *pcgp_relocs, - bool undefined_weak ATTRIBUTE_UNUSED) + bool undefined_weak ATTRIBUTE_UNUSED, + unsigned relax_features ATTRIBUTE_UNUSED) { /* See if this symbol is in range of tp. */ if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0) @@ -4745,7 +4754,8 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again ATTRIBUTE_UNUSED, riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED, - bool undefined_weak ATTRIBUTE_UNUSED) + bool undefined_weak ATTRIBUTE_UNUSED, + unsigned relax_features ATTRIBUTE_UNUSED) { bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; bfd_vma alignment = 1, pos; @@ -4805,11 +4815,12 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma reserve_size, bool *again, riscv_pcgp_relocs *pcgp_relocs, - bool undefined_weak) + bool undefined_weak, + unsigned relax_features) { struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info); /* Can relax to x0 even when gp relaxation is disabled. */ - bfd_vma gp = htab->params->relax_gp + bfd_vma gp = (relax_features & RISCV_RELAX_GP) != 0 ? riscv_global_pointer_value (link_info) : 0; @@ -4965,6 +4976,13 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, bfd_vma max_alignment, reserve_size = 0; riscv_pcgp_relocs pcgp_relocs; static asection *first_section = NULL; + unsigned relax_features = 0; + + /* Detect available/enabled relaxation features. */ + if (elf_elfheader (abfd)->e_flags & EF_RISCV_RVC) + relax_features |= RISCV_RELAX_RVC; + if (htab->params->relax_gp) + relax_features |= RISCV_RELAX_GP; *again = false; @@ -5208,7 +5226,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, if (!relax_func (abfd, sec, sym_sec, info, rel, symval, max_alignment, reserve_size, again, - &pcgp_relocs, undefined_weak)) + &pcgp_relocs, undefined_weak, relax_features)) goto fail; } From patchwork Sat Oct 14 05:03:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 152898 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2305978vqb; Fri, 13 Oct 2023 22:04:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTweLxNbnCCy7Ga6Cqj45mCGzwZpunw8IK/g7MJ7/d4AhwYwsRwmSUjn3f1P8TQRC9CJ61 X-Received: by 2002:a05:620a:80d:b0:777:2792:4a2d with SMTP id s13-20020a05620a080d00b0077727924a2dmr11802198qks.65.1697259848657; Fri, 13 Oct 2023 22:04:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697259848; cv=pass; d=google.com; s=arc-20160816; b=mvAtC49q8OOEVhIb38rzj76keUqTUu/vhKC7Ts+jFQODoHx56IqQeYkHUPjMjR0esf Yp8a8l8rF51i1pOAkmwHBFQu61YZTis7tT7G/xejxq1XrL/5IakvjQ0kjJ2zUU3NHGaI hkpFRn1Am5al4jd8B5L0FF6rSWFjHpsU+PmHIltwOeZKBPQMCtIM3hG0oM8C6fp6ROTr jk51Nnu+W3r5LfiqYNw+afhStyPfqARzQumFOqIBR2kC6gF/tABU1Z8CHsOxPni4wtsl oqws8itxBIcyv7gw+Bm0ljzlxHXnorFvCelcCvV/tJAKVbHlgRkWb0BaiBl96cw2pRsP sFhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dmarc-filter:arc-filter:delivered-to; bh=KxUlm19z1lbTikHkaP6wPXbWQN/VrxBULPDskdQKudg=; fh=Q+ZPQdyJDzJx4BfovHNTR1lP4msTSfN6TDra8V1lyf8=; b=yJ18SvifzbTijpt6WFtD2s6zmudKFmNCrl+hq+XWmzumEgSiXKj4SXSS5nC0vGO+sT 79wEfZ/Ib6/T8MpKQ0gs5x3fthOo8MsjO1B0z/C7u4Ly02QaWmxe3bnZwFduXxc4cFl+ 0ZP9XRNe1QqVY9OIdpeEpA31MqwTIy/5qritBFxTmp0qr9H+9/WO4f2xJiTGyZMqWqyo GgEf2aQQ9lp/TGGf+P5sv+DDx9PBP6voTqjJF9Csi/dCCTESlA6vet3w89ooHwbR025R SmmVq5UGrG9WUL04t5z98bavoKTRhEIZrtyVL+h0CFCC23kYcxxpLz/CgL5qFhC1NfjS yhjA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@irq.a4lg.com header.s=2017s01 header.b=g1q7Fc5F; arc=pass (i=1); 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=irq.a4lg.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ox8-20020a05620a828800b007759c22e907si2135537qkn.142.2023.10.13.22.04.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 22:04:08 -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; dkim=pass header.i=@irq.a4lg.com header.s=2017s01 header.b=g1q7Fc5F; arc=pass (i=1); 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=irq.a4lg.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A44043856254 for ; Sat, 14 Oct 2023 05:04:06 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-sender-0.a4lg.com (mail-sender-0.a4lg.com [IPv6:2401:2500:203:30b:4000:6bfe:4757:0]) by sourceware.org (Postfix) with ESMTPS id 1230D3857718 for ; Sat, 14 Oct 2023 05:03:59 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1230D3857718 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2401:2500:203:30b:4000:6bfe:4757:0 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697259841; cv=none; b=gKoAMxpOqSyLxRrQQkwCVExxFBbwlqfaBkxx6idGDV60vo5C5hWmYQFhfgat5wDDFbuQ/MjBm2InmrOBa+Sy2WMr4wwOFS/312xUo90oZW2pXpWL2rZWBN86mGmQT8KWmxpN5iTXx1ncUKIqjZPVGY2T5+7kGHaAslI1Wc1WvJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697259841; c=relaxed/simple; bh=Df3XUVZGr3d1GyFP7Czq+g94y6xHTPUI3auUT4j1GTA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=dRdzB1Uw7capAn9XxTqKsvDy9bUPxa6//HDJ7toBVTbvxbdtKOYEiTAKaJhg1V7xBMgsB23TW7+PztVXoLq8rsNcDrNZJ4ZRdZgpj3EACwvyuj6aiGuLASboKYJ7DqODQWcKXN4W7PcbWO4jjvw5iopCLG6DVRMw29tVe+EHRio= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1230D3857718 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=irq.a4lg.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=irq.a4lg.com Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id 3ED8D300089; Sat, 14 Oct 2023 05:03:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1697259837; bh=KxUlm19z1lbTikHkaP6wPXbWQN/VrxBULPDskdQKudg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=g1q7Fc5FF+GqH3bDFDdJyHYspS+5jBspviJQyutyePpADuwDh6IWsnyfSfMrxJaaY X/FiLFFTZCenW9bCfD57wOouQjnrWhLFFtbzrc8HltWKzhtnnHz6xRjn6YByVLuYRd EGJXzSppmy1OKmh85R8ZtJncr/8oZoYJFiV5VniE= From: Tsukasa OI To: Tsukasa OI , Palmer Dabbelt , Andrew Waterman , Jim Wilson , Nelson Chu , Kito Cheng Cc: binutils@sourceware.org Subject: [PATCH 2/2] RISC-V: Prepare for more generic PCREL relaxations Date: Sat, 14 Oct 2023 05:03:22 +0000 Message-ID: <7a9385432f0ce7e28e80f2ef9d9e418ba50ec387.1697259797.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_MANYTO, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779705942639052649 X-GMAIL-MSGID: 1779705942639052649 From: Tsukasa OI There's not only Global-pointer Relaxation defined by the RISC-V psABI Specification. There is also the Zero-page relaxation, making the name "pcgp" not suitable for storage to PC-relative relocations used in the linker relaxation passes. This commit prepares for future changes including proper zero-page relaxation. bfd/ChangeLog: * elfnn-riscv.c (struct riscv_pcgp_hi_reloc): Rename to... (struct riscv_pc_relax_hi_reloc): ...here. (struct riscv_pcgp_lo_reloc): Rename to... (struct riscv_pc_relax_lo_reloc): ...here. (riscv_init_pcgp_relocs): Rename to... (riscv_init_pc_relax_relocs): ...here. (riscv_free_pcgp_relocs): Rename to... (riscv_free_pc_relax_relocs): ...here. (riscv_record_pcgp_hi_reloc): Rename to... (riscv_record_pc_relax_hi_reloc): ...here. (riscv_find_pcgp_hi_reloc): Rename to... (riscv_find_pc_relax_hi_reloc): ...here. (riscv_record_pcgp_lo_reloc): Rename to... (riscv_record_pc_relax_lo_reloc): ...here. (riscv_find_pcgp_lo_reloc): Rename to... (riscv_find_pc_relax_lo_reloc): ...here. (riscv_update_pcgp_relocs): Rename to... (riscv_update_pc_relax_relocs): ...here. (_riscv_relax_delete_bytes, _riscv_relax_delete_piecewise, _riscv_relax_delete_immediate, _bfd_riscv_relax_call, _bfd_riscv_relax_lui, _bfd_riscv_relax_tls_le, _bfd_riscv_relax_align, _bfd_riscv_relax_pc, _bfd_riscv_relax_section): Reflect name changes. --- bfd/elfnn-riscv.c | 138 ++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 66 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 15652a08296d..10ecfe35d170 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4090,10 +4090,10 @@ _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info) #define RISCV_RELAX_GP 0x02U /* A second format for recording PC-relative hi relocations. This stores the - information required to relax them to GP-relative addresses. */ + information required to relax them to other kinds of relative addresses. */ -typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc; -struct riscv_pcgp_hi_reloc +typedef struct riscv_pc_relax_hi_reloc riscv_pc_relax_hi_reloc; +struct riscv_pc_relax_hi_reloc { bfd_vma hi_sec_off; bfd_vma hi_addend; @@ -4101,68 +4101,69 @@ struct riscv_pcgp_hi_reloc unsigned hi_sym; asection *sym_sec; bool undefined_weak; - riscv_pcgp_hi_reloc *next; + riscv_pc_relax_hi_reloc *next; }; -typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc; -struct riscv_pcgp_lo_reloc +typedef struct riscv_pc_relax_lo_reloc riscv_pc_relax_lo_reloc; +struct riscv_pc_relax_lo_reloc { bfd_vma hi_sec_off; - riscv_pcgp_lo_reloc *next; + riscv_pc_relax_lo_reloc *next; }; typedef struct { - riscv_pcgp_hi_reloc *hi; - riscv_pcgp_lo_reloc *lo; -} riscv_pcgp_relocs; + riscv_pc_relax_hi_reloc *hi; + riscv_pc_relax_lo_reloc *lo; +} riscv_pc_relax_relocs; -/* Initialize the pcgp reloc info in P. */ +/* Initialize the pc relaxation reloc info in P. */ static bool -riscv_init_pcgp_relocs (riscv_pcgp_relocs *p) +riscv_init_pc_relax_relocs (riscv_pc_relax_relocs *p) { p->hi = NULL; p->lo = NULL; return true; } -/* Free the pcgp reloc info in P. */ +/* Free the pc relaxation reloc info in P. */ static void -riscv_free_pcgp_relocs (riscv_pcgp_relocs *p, - bfd *abfd ATTRIBUTE_UNUSED, - asection *sec ATTRIBUTE_UNUSED) +riscv_free_pc_relax_relocs (riscv_pc_relax_relocs *p, + bfd *abfd ATTRIBUTE_UNUSED, + asection *sec ATTRIBUTE_UNUSED) { - riscv_pcgp_hi_reloc *c; - riscv_pcgp_lo_reloc *l; + riscv_pc_relax_hi_reloc *c; + riscv_pc_relax_lo_reloc *l; for (c = p->hi; c != NULL; ) { - riscv_pcgp_hi_reloc *next = c->next; + riscv_pc_relax_hi_reloc *next = c->next; free (c); c = next; } for (l = p->lo; l != NULL; ) { - riscv_pcgp_lo_reloc *next = l->next; + riscv_pc_relax_lo_reloc *next = l->next; free (l); l = next; } } -/* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index. - The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to - relax the corresponding lo part reloc. */ +/* Record pc relaxation hi part reloc info in P, using HI_SEC_OFF as + the lookup index. The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args + contain info required to relax the corresponding lo part reloc. */ static bool -riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off, - bfd_vma hi_addend, bfd_vma hi_addr, - unsigned hi_sym, asection *sym_sec, - bool undefined_weak) +riscv_record_pc_relax_hi_reloc (riscv_pc_relax_relocs *p, + bfd_vma hi_sec_off, + bfd_vma hi_addend, bfd_vma hi_addr, + unsigned hi_sym, asection *sym_sec, + bool undefined_weak) { - riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof (*new)); + riscv_pc_relax_hi_reloc *new = bfd_malloc (sizeof (*new)); if (!new) return false; new->hi_sec_off = hi_sec_off; @@ -4179,10 +4180,10 @@ riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off, /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index. This is used by a lo part reloc to find the corresponding hi part reloc. */ -static riscv_pcgp_hi_reloc * -riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) +static riscv_pc_relax_hi_reloc * +riscv_find_pc_relax_hi_reloc (riscv_pc_relax_relocs *p, bfd_vma hi_sec_off) { - riscv_pcgp_hi_reloc *c; + riscv_pc_relax_hi_reloc *c; for (c = p->hi; c != NULL; c = c->next) if (c->hi_sec_off == hi_sec_off) @@ -4194,9 +4195,10 @@ riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) This is used to record relocs that can't be relaxed. */ static bool -riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) +riscv_record_pc_relax_lo_reloc (riscv_pc_relax_relocs *p, + bfd_vma hi_sec_off) { - riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof (*new)); + riscv_pc_relax_lo_reloc *new = bfd_malloc (sizeof (*new)); if (!new) return false; new->hi_sec_off = hi_sec_off; @@ -4209,9 +4211,9 @@ riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) This is used by a hi part reloc to find the corresponding lo part reloc. */ static bool -riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) +riscv_find_pc_relax_lo_reloc (riscv_pc_relax_relocs *p, bfd_vma hi_sec_off) { - riscv_pcgp_lo_reloc *c; + riscv_pc_relax_lo_reloc *c; for (c = p->lo; c != NULL; c = c->next) if (c->hi_sec_off == hi_sec_off) @@ -4220,14 +4222,16 @@ riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off) } static void -riscv_update_pcgp_relocs (riscv_pcgp_relocs *p, asection *deleted_sec, - bfd_vma deleted_addr, size_t deleted_count) +riscv_update_pc_relax_relocs (riscv_pc_relax_relocs *p, + asection *deleted_sec, + bfd_vma deleted_addr, + size_t deleted_count) { /* Bytes have already been deleted and toaddr should match the old section size for our checks, so adjust it here. */ bfd_vma toaddr = deleted_sec->size + deleted_count; - riscv_pcgp_lo_reloc *l; - riscv_pcgp_hi_reloc *h; + riscv_pc_relax_lo_reloc *l; + riscv_pc_relax_hi_reloc *h; /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo entries where they occur after the deleted bytes. */ @@ -4258,7 +4262,7 @@ _riscv_relax_delete_bytes (bfd *abfd, bfd_vma addr, size_t count, struct bfd_link_info *link_info, - riscv_pcgp_relocs *p, + riscv_pc_relax_relocs *p, bfd_vma delete_total, bfd_vma toaddr) { @@ -4287,7 +4291,7 @@ _riscv_relax_delete_bytes (bfd *abfd, /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs table for which these values occur after the deleted bytes. */ if (p) - riscv_update_pcgp_relocs (p, sec, addr, count); + riscv_update_pc_relax_relocs (p, sec, addr, count); /* Adjust the local symbols defined in this section. */ for (i = 0; i < symtab_hdr->sh_info; i++) @@ -4376,7 +4380,7 @@ _riscv_relax_delete_bytes (bfd *abfd, typedef bool (*relax_delete_t) (bfd *, asection *, bfd_vma, size_t, struct bfd_link_info *, - riscv_pcgp_relocs *, + riscv_pc_relax_relocs *, Elf_Internal_Rela *); static relax_delete_t riscv_relax_delete_bytes; @@ -4390,7 +4394,7 @@ _riscv_relax_delete_piecewise (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma addr, size_t count, struct bfd_link_info *link_info ATTRIBUTE_UNUSED, - riscv_pcgp_relocs *p ATTRIBUTE_UNUSED, + riscv_pc_relax_relocs *p ATTRIBUTE_UNUSED, Elf_Internal_Rela *rel) { if (rel == NULL) @@ -4409,7 +4413,7 @@ _riscv_relax_delete_immediate (bfd *abfd, bfd_vma addr, size_t count, struct bfd_link_info *link_info, - riscv_pcgp_relocs *p, + riscv_pc_relax_relocs *p, Elf_Internal_Rela *rel) { if (rel != NULL) @@ -4474,7 +4478,7 @@ typedef bool (*relax_func_t) (bfd *, asection *, asection *, struct bfd_link_info *, Elf_Internal_Rela *, bfd_vma, bfd_vma, bfd_vma, bool *, - riscv_pcgp_relocs *, + riscv_pc_relax_relocs *, bool undefined_weak, unsigned relax_features); @@ -4488,7 +4492,7 @@ _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec, bfd_vma max_alignment, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again, - riscv_pcgp_relocs *pcgp_relocs, + riscv_pc_relax_relocs *pc_relax_relocs, bool undefined_weak ATTRIBUTE_UNUSED, unsigned relax_features) { @@ -4554,7 +4558,7 @@ _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec, /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */ *again = true; return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len, - link_info, pcgp_relocs, rel + 1); + link_info, pc_relax_relocs, rel + 1); } /* Traverse all output sections and return the max alignment. @@ -4596,7 +4600,7 @@ _bfd_riscv_relax_lui (bfd *abfd, bfd_vma max_alignment, bfd_vma reserve_size, bool *again, - riscv_pcgp_relocs *pcgp_relocs, + riscv_pc_relax_relocs *pc_relax_relocs, bool undefined_weak, unsigned relax_features) { @@ -4658,7 +4662,8 @@ _bfd_riscv_relax_lui (bfd *abfd, /* Delete unnecessary LUI and reuse the reloc. */ *again = true; return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, - link_info, pcgp_relocs, rel); + link_info, pc_relax_relocs, + rel); default: abort (); @@ -4692,7 +4697,8 @@ _bfd_riscv_relax_lui (bfd *abfd, /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */ *again = true; return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2, - link_info, pcgp_relocs, rel + 1); + link_info, pc_relax_relocs, + rel + 1); } return true; @@ -4710,7 +4716,7 @@ _bfd_riscv_relax_tls_le (bfd *abfd, bfd_vma max_alignment ATTRIBUTE_UNUSED, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again, - riscv_pcgp_relocs *pcgp_relocs, + riscv_pc_relax_relocs *pc_relax_relocs, bool undefined_weak ATTRIBUTE_UNUSED, unsigned relax_features ATTRIBUTE_UNUSED) { @@ -4734,7 +4740,7 @@ _bfd_riscv_relax_tls_le (bfd *abfd, /* Delete unnecessary instruction and reuse the reloc. */ *again = true; return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info, - pcgp_relocs, rel); + pc_relax_relocs, rel); default: abort (); @@ -4753,7 +4759,7 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec, bfd_vma max_alignment ATTRIBUTE_UNUSED, bfd_vma reserve_size ATTRIBUTE_UNUSED, bool *again ATTRIBUTE_UNUSED, - riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED, + riscv_pc_relax_relocs *pc_relax_relocs ATTRIBUTE_UNUSED, bool undefined_weak ATTRIBUTE_UNUSED, unsigned relax_features ATTRIBUTE_UNUSED) { @@ -4814,7 +4820,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma max_alignment, bfd_vma reserve_size, bool *again, - riscv_pcgp_relocs *pcgp_relocs, + riscv_pc_relax_relocs *pc_relax_relocs, bool undefined_weak, unsigned relax_features) { @@ -4828,7 +4834,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, /* Chain the _LO relocs to their cooresponding _HI reloc to compute the actual target address. */ - riscv_pcgp_hi_reloc hi_reloc; + riscv_pc_relax_hi_reloc hi_reloc; memset (&hi_reloc, 0, sizeof (hi_reloc)); switch (ELFNN_R_TYPE (rel->r_info)) { @@ -4840,11 +4846,11 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, hi part instruction. So we must subtract it here for the lookup. It is still used below in the final symbol address. */ bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend; - riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs, - hi_sec_off); + riscv_pc_relax_hi_reloc *hi = + riscv_find_pc_relax_hi_reloc (pc_relax_relocs, hi_sec_off); if (hi == NULL) { - riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off); + riscv_record_pc_relax_lo_reloc (pc_relax_relocs, hi_sec_off); return true; } @@ -4855,7 +4861,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, /* We can not know whether the undefined weak symbol is referenced according to the information of R_RISCV_PCREL_LO12_I/S. Therefore, we have to record the 'undefined_weak' flag when handling the - corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */ + corresponding R_RISCV_HI20 reloc in riscv_record_pc_relax_hi_reloc. */ undefined_weak = hi_reloc.undefined_weak; } break; @@ -4868,7 +4874,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, /* If the cooresponding lo relocation has already been seen then it's not safe to relax this relocation. */ - if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset)) + if (riscv_find_pc_relax_lo_reloc (pc_relax_relocs, rel->r_offset)) return true; break; @@ -4924,7 +4930,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, return true; case R_RISCV_PCREL_HI20: - riscv_record_pcgp_hi_reloc (pcgp_relocs, + riscv_record_pc_relax_hi_reloc (pc_relax_relocs, rel->r_offset, rel->r_addend, symval, @@ -4934,7 +4940,7 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, /* Delete unnecessary AUIPC and reuse the reloc. */ *again = true; riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info, - pcgp_relocs, rel); + pc_relax_relocs, rel); return true; default: @@ -4974,7 +4980,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, bool ret = false; unsigned int i; bfd_vma max_alignment, reserve_size = 0; - riscv_pcgp_relocs pcgp_relocs; + riscv_pc_relax_relocs pc_relax_relocs; static asection *first_section = NULL; unsigned relax_features = 0; @@ -5005,7 +5011,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, else if (first_section == sec) htab->max_alignment_for_gp = -1; - riscv_init_pcgp_relocs (&pcgp_relocs); + riscv_init_pc_relax_relocs (&pc_relax_relocs); /* Read this BFD's relocs if we haven't done so already. */ if (data->relocs) @@ -5226,7 +5232,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, if (!relax_func (abfd, sec, sym_sec, info, rel, symval, max_alignment, reserve_size, again, - &pcgp_relocs, undefined_weak, relax_features)) + &pc_relax_relocs, undefined_weak, relax_features)) goto fail; } @@ -5239,7 +5245,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, fail: if (relocs != data->relocs) free (relocs); - riscv_free_pcgp_relocs (&pcgp_relocs, abfd, sec); + riscv_free_pc_relax_relocs (&pc_relax_relocs, abfd, sec); return ret; }