From patchwork Wed Jul 5 08:42:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Die Li X-Patchwork-Id: 116027 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1719685vqx; Wed, 5 Jul 2023 01:42:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlEDxc+zIK2kGdRSuV+jei0LChymxDuvq2XQK7D+sNLKtMjAE0WaV3ZGNyo9LEAuLqXO9rTs X-Received: by 2002:a17:907:11c7:b0:992:a836:a194 with SMTP id va7-20020a17090711c700b00992a836a194mr11898375ejb.59.1688546554910; Wed, 05 Jul 2023 01:42:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688546554; cv=none; d=google.com; s=arc-20160816; b=gNnnfN+AXMD0U3HqPCbOXqBs7Bbv1HhbnYdHIAZyS9GXJaIFlIN69vfR40hwpHSlRJ LLKkkTd1yAVaTDA356NGtlnR2okhOxkuYs7P03ObbmJ4rOvZMUuSEMZ/nOzclzDeDRbM yjuj6zk5EBjfoFfAB51azLTElbEBtNjvRTETwUPlS+wqYfXUnTIezqR2RBKBJDzawW6t DM/YG5NXxgiIv7H2/h2ovVhyVHxjrFgm266C4j0Ktoq6icLmUm552G9zp3lMhNQH0M1K rEhkJcQnHFT4cJoDfdX26aqsp9nfGr36KbCgkU82Py/RFYgZPICb5ibm1A5dye5d0AvB QJ3Q== 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:message-id:date:subject:cc:to :from:dmarc-filter:delivered-to; bh=0BnybvHBjNj3UPRhOr3lnP14GPAnSKb7tpWtVy3r9U4=; fh=BtxteLQ5QoDmwSeF2tYQlikNG1xjxQbvqtQO9C9JY0Y=; b=pZRda+Ge2eB6/imy+ulNgP2CHDG8KE96pT3mlX0NpCHsJUBi5j77FiLGoNQxRjIfa9 9rRxbLvqL4u57V0DaxeNHVouNLZO2iS2HSxpNyw5j7yo/Q/wQVLWnJn4mWMtHgbiOYdA jrqiDseeqzVlOBjy5KKVv+0urcMB07ZdxSS66/XV541VuTGewR+s2lZLfqKaNYUUfCs3 rDfHLu18RzdVZo+veN9RcT1U2V1vIgEUSRuEUxFZ/yLOLRTg7KRniLDy4B+VRkYMA6zT 2PP2vCJrz9bAwun2/i3PMSx3xMRe/fcQajXwQIfxHzGeGvctkES1SA3L4aEzB7GmrBqt knKw== 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 server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n20-20020a17090695d400b0099381745ba7si2891711ejy.878.2023.07.05.01.42.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jul 2023 01:42: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 8A4313858288 for ; Wed, 5 Jul 2023 08:42:33 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from zg8tmja2lje4os4yms4ymjma.icoremail.net (zg8tmja2lje4os4yms4ymjma.icoremail.net [206.189.21.223]) by sourceware.org (Postfix) with ESMTP id 616683858CD1 for ; Wed, 5 Jul 2023 08:42:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 616683858CD1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eswincomputing.com Received: from host040-ubuntu-1804.lxd (unknown [10.12.130.38]) by app2 (Coremail) with SMTP id EggMCgCHzZPnLKVkt0EuAA--.8241S4; Wed, 05 Jul 2023 16:42:16 +0800 (CST) From: Die Li To: binutils@sourceware.org Cc: kito.cheng@sifive.com, palmer@dabbelt.com, nelson@rivosinc.com, Die Li Subject: [PATCH] [RISC-V] Fix the valid gp range for gp relax. Date: Wed, 5 Jul 2023 08:42:13 +0000 Message-Id: <20230705084213.91043-1-lidie@eswincomputing.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: EggMCgCHzZPnLKVkt0EuAA--.8241S4 X-Coremail-Antispam: 1UD129KBjvJXoWxKF4rAr4xKr4ftw4rGw13CFg_yoW7AFyfpr 4xKF90kFs5AFs3W3y3K34Uua1Fq3W0kFyUW3s3K3yfCw42qrWxZrWFyr47Ww45Gw1UWr45 ZrnIka1rAFW5Kw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk214x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc2xSY4AK6svPMxAI w28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfUoOJ5UUUUU X-CM-SenderInfo: 5olgxv46hv4xpqfrz1xxwl0woofrz/ X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, 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?1770569392725252339?= X-GMAIL-MSGID: =?utf-8?q?1770569392725252339?= Taking alignment into consideration, the conservative addressing range for gp relax is [gp - 12bit_imm + max_alignment, gp + 12bit_imm - max_alignment], which is smaller than the ideal GP addressing range [gp - 12bit_imm, gp + 12bit_imm]. Therefore, when symval < gp, it is necessary to ensure that both symval and symval + reserve_size fall within the range [gp - 12bit_imm + alignment, gp + 12bit_imm - alignment]. Current linker takes the range of gp conservatively, this patch allows more symbols to be accessed by gp relative addressing. Take the file named with gp-relax.s from this patch as the source file. After assembling and linking with --relax option, Before this patch: 0+[0-9a-f]+ <_start>: .*:[ ]+[0-9a-f]+[ ]+lui+.* .*:[ ]+[0-9a-f]+[ ]+addi+.* .*:[ ]+[0-9a-f]+[ ]+lui+.* .*:[ ]+[0-9a-f]+[ ]+addi+.* .*:[ ]+[0-9a-f]+[ ]+auipc+.* .*:[ ]+[0-9a-f]+[ ]+lw+.* Check the symbal table after linking, we have : Symbol table '.symtab' contains 19 entries: Num: Value Size Type Bind Vis Ndx Name ... 7: 0000000000011900 0 NOTYPE GLOBAL DEFAULT ABS __global_pointer$ ... 18: 0000000000011108 3600 OBJECT GLOBAL DEFAULT 3 global_array By comparing the values of `global_array` and `__global_pointer$`, the symbol of global_array can be accessed by gp relative addressing. After this patch: 0+[0-9a-f]+ <_start>: .*:[ ]+[0-9a-f]+[ ]+addi[ ]+a3,gp,\-[0-9]+ # [0-9a-f]+ .*:[ ]+[0-9a-f]+[ ]+addi[ ]+a4,gp,\-[0-9]+ # [0-9a-f]+ Co-Authored by: Fei Gao Signed-off-by: Die Li ChangeLog: * bfd/elfnn-riscv.c (_bfd_riscv_relax_lui): Fix the valid gp range. (_bfd_riscv_relax_pc): Likewise. * ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp: New test. * ld/testsuite/ld-riscv-elf/gp-relax.d: New test. * ld/testsuite/ld-riscv-elf/gp-relax.s: New test. --- bfd/elfnn-riscv.c | 6 +++-- ld/testsuite/ld-riscv-elf/gp-relax.d | 14 ++++++++++ ld/testsuite/ld-riscv-elf/gp-relax.s | 30 ++++++++++++++++++++++ ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 + 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ld/testsuite/ld-riscv-elf/gp-relax.d create mode 100644 ld/testsuite/ld-riscv-elf/gp-relax.s diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 09aa7be225e..00e692c6924 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4633,7 +4633,8 @@ _bfd_riscv_relax_lui (bfd *abfd, || (symval >= gp && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size)) || (symval < gp - && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))) + && VALID_ITYPE_IMM (symval - gp - max_alignment) + && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))) { unsigned sym = ELFNN_R_SYM (rel->r_info); switch (ELFNN_R_TYPE (rel->r_info)) @@ -4897,7 +4898,8 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, || (symval >= gp && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size)) || (symval < gp - && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))) + && VALID_ITYPE_IMM (symval - gp - max_alignment) + && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))) { unsigned sym = hi_reloc.hi_sym; switch (ELFNN_R_TYPE (rel->r_info)) diff --git a/ld/testsuite/ld-riscv-elf/gp-relax.d b/ld/testsuite/ld-riscv-elf/gp-relax.d new file mode 100644 index 00000000000..fa93c05ee2c --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/gp-relax.d @@ -0,0 +1,14 @@ +#source: gp-relax.s +#as: +#ld: --relax +#objdump: -d -Mno-aliases + +.*:[ ]+file format .* + + +Disassembly of section \.text: + +0+[0-9a-f]+ <_start>: +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a3,gp,\-[0-9]+ # [0-9a-f]+ +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a4,gp,\-[0-9]+ # [0-9a-f]+ diff --git a/ld/testsuite/ld-riscv-elf/gp-relax.s b/ld/testsuite/ld-riscv-elf/gp-relax.s new file mode 100644 index 00000000000..36addbc770c --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/gp-relax.s @@ -0,0 +1,30 @@ + .globl global_var + .section .sbss,"aw",@nobits + .align 2 + .type global_var, @object + .size global_var, 4 +global_var: + .zero 4 + + .globl global_array + .bss + .align 3 + .type global_array, @object + .size global_array, 3600 +global_array: + .zero 3600 + + .text + .align 1 + .globl _start + .type _start, @function +_start: + lui a3,%hi(global_array) + addi a3,a3,%lo(global_array) + + lui a4,%hi(global_array + 0x600) + addi a4,a4,%lo(global_array + 0x600) + +.LA0: + auipc a5,%pcrel_hi(global_array) + lw a0,%pcrel_lo(.LA0)(a5) diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp index 947a266ba72..6a04955b23b 100644 --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp @@ -124,6 +124,7 @@ if [istarget "riscv*-*-*"] { run_dump_test "pcgp-relax-01" run_dump_test "pcgp-relax-01-norelaxgp" run_dump_test "pcgp-relax-02" + run_dump_test "gp-relax" run_dump_test "c-lui" run_dump_test "c-lui-2" run_dump_test "disas-jalr"