From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id 3209F3858C50 for ; Mon, 2 May 2022 13:51:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3209F3858C50 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-qk1-x734.google.com with SMTP id a76so3793453qkg.12 for ; Mon, 02 May 2022 06:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HEuCWm0d6mXKFR/aSmToH6jUEF35K9ID7XQpTKrLycw=; b=0jL+dL6gjoOfV+Ptl1gVkBxUClCf5L0EsqoN/9o8dpnneOAheySA0hbB5apxnkyuDZ QsDTT631X73GZwAVR8OaaGOMoO23fopWJSfRgse5hg2k3EqpC27ryHwUCuA3j3p1dUnK /VFBbYzvX2NWhKIa7zMO2X5OrNI+AyOehKNVpLKegb7xI+UDXPyYDppEbTAfxevOKODP IgNlnoFWhYT/GQkTblZggw81aNKU7ZuDPjq7akcIIFs02Qd4m0YQ6bwXhoiYjiGH0h5Q bYWHjqABqC+As70IMUPlvMJNz6XhkrOLZHTy9wgD3s/WQEK4YoOe29JuR/pxKk26LKOF K5qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HEuCWm0d6mXKFR/aSmToH6jUEF35K9ID7XQpTKrLycw=; b=mDZdR5AgyyWRwFmFabRGpu52LM6wxowEz/liitgI/h2KB3nDhioU898UmUgOysDPC0 zjNVNAacpMm3D9q2FC0R592ogWrdzS2qbtH+OZ5ziYpN7Vxl87LLMHcrrG8AZnL8rPkO 3VsO7DFMziKTO9f+6VJKaHob0DJQR7AbwOlzY/ucVK0HTFGb99FkAsS5Sd6BP8o9K0gv XISKXx63P4SgT3gePCX4gI/bqufgO7590CwnAgjOKymMRqsEu+94ld25ZwOODAkcKnPQ yQv5UTCXDs8bTeAM+PzN7GezLtJ6HPzpP6/u/eiwUQli6BCASfEZPrf2AurV1FhBcYbr wPGQ== X-Gm-Message-State: AOAM533UgD+gpQJFA3bIGiV4+LZcukzf/47+0RDK8gXIEvJr+jhKXevl wJVeop+zZ7e4/pPjRw/YZe6FdvUHaOKHLQ== X-Google-Smtp-Source: ABdhPJzGYwRncvW3ZlF2Xvp1UAJA+Abp0drZ4Ad7QhRoEipqXlkWlqpMUtWjLoN/sGx9L6JV0VQoqQ== X-Received: by 2002:ae9:f504:0:b0:69f:ba2d:29ff with SMTP id o4-20020ae9f504000000b0069fba2d29ffmr8684278qkg.583.1651499498257; Mon, 02 May 2022 06:51:38 -0700 (PDT) Received: from localhost.localdomain ([38.101.220.237]) by smtp.gmail.com with ESMTPSA id v19-20020ac873d3000000b002f39b99f69asm4232132qtp.52.2022.05.02.06.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 06:51:37 -0700 (PDT) From: Patrick O'Neill To: binutils@sourceware.org Cc: kito.cheng@gmail.com, palmer@dabbelt.com, gnu-toolchain@rivosinc.com, Patrick O'Neill Subject: [PATCH v2 2/5] RISCV: Arrange DELETE pass after .align pass Date: Mon, 2 May 2022 06:50:45 -0700 Message-Id: <20220502135048.1392596-3-patrick@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220502135048.1392596-1-patrick@rivosinc.com> References: <20220412162601.146507-1-patrick@rivosinc.com> <20220502135048.1392596-1-patrick@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-List-Received-Date: Mon, 02 May 2022 13:51:40 -0000 By moving the deletion pass after the align pass, the linker can touch each byte once via piecewise deletion. Otherwise, it may need to move the same bytes twice. 2022-04-29 Patrick O'Neill * elfnn-riscv.c (_bfd_riscv_relax_align): Count to-be-deleted bytes so the alignments are still accurate. * elfnn-riscv.c (_bfd_riscv_relax_section): Move DELETE pass after ALIGN pass. Signed-off-by: Patrick O'Neill --- This gives us an O(n^2) runtime when the align pass counts the number of deleted bytes preceding it, but this will be fixed in Patch 4/5. --- bfd/elfnn-riscv.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 8f9f0d8a86a..17f9607744f 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4435,7 +4435,7 @@ _bfd_riscv_relax_tls_le (bfd *abfd, static bool _bfd_riscv_relax_align (bfd *abfd, asection *sec, asection *sym_sec, - struct bfd_link_info *link_info, + struct bfd_link_info *link_info ATTRIBUTE_UNUSED, Elf_Internal_Rela *rel, bfd_vma symval, bfd_vma max_alignment ATTRIBUTE_UNUSED, @@ -4449,6 +4449,18 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec, while (alignment <= rel->r_addend) alignment *= 2; + Elf_Internal_Rela *relocs = elf_section_data (sec)->relocs; + for (unsigned int i = 0; i < sec->reloc_count; i++) + { + Elf_Internal_Rela *reloc = relocs + i; + /* Ignore annotations after this alignment directive. */ + if (reloc == rel) + break; + /* Account for to-be-deleted bytes */ + else if (ELFNN_R_TYPE (reloc->r_info) == R_RISCV_DELETE) + symval -= reloc->r_addend; + } + symval -= rel->r_addend; bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment; bfd_vma nop_bytes = aligned_addr - symval; @@ -4468,12 +4480,12 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec, return false; } - /* Delete the reloc. */ - rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE); - - /* If the number of NOPs is already correct, there's nothing to do. */ + /* If the number of NOPs is already correct, delete the reloc. */ if (nop_bytes == rel->r_addend) - return true; + { + rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE); + return true; + } /* Write as many RISC-V NOPs as we need. */ for (pos = 0; pos < (nop_bytes & -4); pos += 4) @@ -4483,10 +4495,11 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec, if (nop_bytes % 4 != 0) bfd_putl16 (RVC_NOP, contents + rel->r_offset + pos); - /* Delete the excess bytes. */ - return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes, - rel->r_addend - nop_bytes, link_info, - NULL); + /* Mark the excess bytes for deletion. */ + rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE); + rel->r_addend = rel->r_addend - nop_bytes; + rel->r_offset = rel->r_offset + nop_bytes; + return true; } /* Relax PC-relative references to GP-relative references. */ @@ -4677,8 +4690,8 @@ bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info *info, /* Relax a section. Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs. - Pass 1: Deletes the bytes that PCREL relaxation in pass 0 made obsolete. - Pass 2: Which cannot be disabled, handles code alignment directives. */ + Pass 1: Which cannot be disabled, handles code alignment directives. + Pass 2: Handle DELETE directives. */ static bool _bfd_riscv_relax_section (bfd *abfd, asection *sec, @@ -4697,7 +4710,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, *again = false; if (bfd_link_relocatable (info) - || sec->sec_flg0 + || (sec->sec_flg0 && info->relax_pass == 0) || (sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0 || (info->disable_target_specific_optimizations @@ -4771,10 +4784,10 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, /* Skip over the R_RISCV_RELAX. */ i++; } - else if (info->relax_pass == 1 && type == R_RISCV_DELETE) - relax_func = _bfd_riscv_relax_delete; - else if (info->relax_pass == 2 && type == R_RISCV_ALIGN) + else if (info->relax_pass == 1 && type == R_RISCV_ALIGN) relax_func = _bfd_riscv_relax_align; + else if (info->relax_pass == 2 && type == R_RISCV_DELETE) + relax_func = _bfd_riscv_relax_delete; else continue; -- 2.25.1