From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 956A93858C56 for ; Tue, 12 Apr 2022 16:26:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 956A93858C56 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-pj1-x1029.google.com with SMTP id mm4-20020a17090b358400b001cb93d8b137so3461613pjb.2 for ; Tue, 12 Apr 2022 09:26:38 -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=GtguOMWAQrEDlpRnXGK8XBrLRek1ofyZ4KMMzaITaNg=; b=LmBtA0BfJCSlKjNbjQ7yfBmqCtwP2GIhX/JP4ppYYJFza4UztDj8TRhwdyPXh948YU NOKpbcQk/1TdczkuKI08N5e8Cx4lICsSmHNy+c7OYFLnhEbbQr0M1WhU93/mocbNM2+V wDgK/OascFaJRbXVRacQ34fR9cRjnEGbk1IqG5vUpcDCPnUhq5BOp2gSznv55SW9AhrV vD5FD1MacsVHSvFfXNWEdoOlYBP2FVyIxmg1UurqLx2Zni/5Woy5JGD6xugHszQsdm4t XsSnchaLYaZMNGzFoJAXat5Xw3e7GQLl95gu2cvBs1lvuBLeUUxs5dddtKthNBgd9OL/ Bgzg== 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=GtguOMWAQrEDlpRnXGK8XBrLRek1ofyZ4KMMzaITaNg=; b=UKGIV9cb3GQS4Lci+YkooBNNML0Tb3xvrzzq8sgeAS3cWfeuMnGodWnyji5Jm+wNTT 9aJngZmn4P8cW4jSSyzzoB8HvDaRWwiH/ziPkAPEaKU0CU/ibHQ3OaWjrbNl96k0RQ0q uIMc12G2Sc0w69cZUtrvvNAmf6KO6/1md38edXomjdBhdLCbEv4DQFNmTuPSvkMXJyK9 KvB3ZVaHu8weTGBSKMb2CAJQowAQFgabAwyyjGNl29ixKBN8SGj/qcAjVvve5OgsOckq lCMzCZeJDX1fONaotXe1M8i8RioAIJ57eM0LLTw77hNPr03DoXQCA0RTlOR/CrxWr8qW KdTQ== X-Gm-Message-State: AOAM532docEVgwFAHQbfmS5xEHRJON54As0VonpZrpirYO/eCTpV95ar 6J41Ijd2XZ9kVb1eCfsWmVwkYQeMcyoP71WC X-Google-Smtp-Source: ABdhPJwI+rJ9MSxLtdwMc+2c87PEUzfqSa8m3EjHZkYz4xgqWLL4//AH+taN7Y4556UGm98Vwzi1aA== X-Received: by 2002:a17:902:e8d1:b0:156:5651:777 with SMTP id v17-20020a170902e8d100b0015656510777mr38359772plg.65.1649780797026; Tue, 12 Apr 2022 09:26:37 -0700 (PDT) Received: from patrick-ThinkPad-X1-Carbon-Gen-8.hq.rivosinc.com ([12.3.194.138]) by smtp.gmail.com with ESMTPSA id x5-20020aa79a45000000b00505c1ab148esm8385246pfj.131.2022.04.12.09.26.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 09:26:36 -0700 (PDT) From: Patrick O'Neill To: binutils@sourceware.org Cc: gnu-toolchain@rivosinc.com, palmer@dabbelt.com, andrew@sifive.com, Patrick O'Neill Subject: [PATCH 2/4] RISCV: Arrange DELETE pass after .align pass Date: Tue, 12 Apr 2022 09:25:59 -0700 Message-Id: <20220412162601.146507-3-patrick@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412162601.146507-1-patrick@rivosinc.com> References: <20220412162601.146507-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: Tue, 12 Apr 2022 16:26: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-11 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/4. --- 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 8f9f0d8a86..17f9607744 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