From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zg8tmtyylji0my4xnjqumte4.icoremail.net (zg8tmtyylji0my4xnjqumte4.icoremail.net [162.243.164.118]) by sourceware.org (Postfix) with ESMTP id E05483858D28 for ; Tue, 27 Jun 2023 12:47:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E05483858D28 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 app1 (Coremail) with SMTP id EwgMCgCXNMRj2ppkDEorAA--.46040S4; Tue, 27 Jun 2023 20:47:31 +0800 (CST) From: Die Li To: binutils@sourceware.org Cc: nelson@rivosinc.com, kito.cheng@sifive.com, palmer@dabbelt.com, lidie@eswincomputing.com Subject: [PATCH] [RISC-V] Optimize GP-relative addressing for linker. Date: Tue, 27 Jun 2023 12:47:28 +0000 Message-Id: <20230627124728.3563-1-lidie@eswincomputing.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID:EwgMCgCXNMRj2ppkDEorAA--.46040S4 X-Coremail-Antispam: 1UD129KBjvJXoWxCr1xWF1rGrWUWw4xXw47urg_yoW7Gr4Dpr 48GF9IkFs8C3Z7X3yfGF4Uu3WSvryxCF1UG34fKw4akws2qrWxZr4Fkw1xXws8Kr15Wr4S vr4Yyw4rCrs0kr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 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.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H2,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 List-Id: Consider the following test: //test.c file struct Person { char name[20]; int age; int nation; int hobby[100]; }; int global_var; int global_array[920]; struct Person person; int main() { global_var = 3; global_array[365] = 16; sprintf(person.name, "Lee"); person.age = 27; person.nation = 77; return 0; } Cflags: -Xlinker --relax Link relaxation can be turned on by the above option, and in fact is turned on by default. After compiling, linking, and disassembling the test files, there are the following results: Before this patch: Disassembly of section \.text: 0+[0-9a-f]+
: .*:[ ]+[0-9a-f]+[ ]+sw[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ .*:[ ]+[0-9a-f]+[ ]+c\.lui[ ]+.* .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,[0-9a-f]+,[0-9]+ # [0-9a-f]+ .*:[ ]+[0-9a-f]+[ ]+c\.lui[ ]+.* .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,[0-9a-f]+,[0-9]+ # [0-9a-f]+ After this patch: Disassembly of section \.text: 0+[0-9a-f]+
: .*:[ ]+[0-9a-f]+[ ]+sw[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ After applying this patch, both array element and structure member accesses have been optimized. In fact, for both array elements and structure members, access is based on the data and the base address of the structure, which is the address of the first member of the array or structure. However, current linker takes into account the size of arrays and structures when performing GP-relative addressing. As a result, some array and structure bases within the GP-relative addressing range cannot benefit from relaxation optimization. This patch resolves this issue. Signed-off-by: Die Li ChangeLog: * bfd/elfnn-riscv.c (_bfd_riscv_relax_section): Update reserve_size. * ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp: New test entry. * ld/testsuite/ld-riscv-elf/gp-relax.d: New test. * ld/testsuite/ld-riscv-elf/gp-relax.s: New test. --- bfd/elfnn-riscv.c | 7 +++++ ld/testsuite/ld-riscv-elf/gp-relax.d | 12 +++++++++ ld/testsuite/ld-riscv-elf/gp-relax.s | 31 ++++++++++++++++++++++ ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 + 4 files changed, 51 insertions(+) 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..29fc5484e0f 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -5169,6 +5169,13 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, if (h->type != STT_FUNC) reserve_size = (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend; + + /* For global pointer relative addressing, it is sufficient to ensure + that the symbol's base address falls within the range of global + pointer relative addressing. */ + if (h->type == STT_OBJECT) + reserve_size = 0; + symtype = h->type; } 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..ec2f59b1b19 --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/gp-relax.d @@ -0,0 +1,12 @@ +#source: gp-relax.s +#ld: --relax +#objdump: -d -Mno-aliases + +.*:[ ]+file format .* + + +Disassembly of section \.text: + +0+[0-9a-f]+ <_start>: +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a5,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..05548888ebf --- /dev/null +++ b/ld/testsuite/ld-riscv-elf/gp-relax.s @@ -0,0 +1,31 @@ + .text + .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, 3680 +global_array: + .zero 3680 + .globl person + .align 3 + .type person, @object + .size person, 428 +person: + .zero 428 + .text + .align 1 + .globl _start + .type _start, @function +_start: + lui a5,%hi(global_array) + addi a5,a5,%lo(global_array) + + lui a4,%hi(person) + addi a4,a4,%lo(person) 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" -- 2.17.1