From patchwork Fri Nov 15 02:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lulu Cai X-Patchwork-Id: 101148 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 96CE63858C52 for ; Fri, 15 Nov 2024 02:52:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 96CE63858C52 X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 7F2B13858C66 for ; Fri, 15 Nov 2024 02:51:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F2B13858C66 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7F2B13858C66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731639119; cv=none; b=hsUVGIK/TtG1idMuZeX70l3GQviEub6b9e+CJz5leDMbNykNGwrgVMvvQ6DZFwFXvXmP/2tfPNrrP8S0DeeGV3ryFv3jiK7cxeQABI9Qwb7Yw+4uDpI/KjmkoSRHDXyZdhp4rQremO/k9s3jw8zk5QFM40WHTgA++XTWZN4c+K8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731639119; c=relaxed/simple; bh=RK1XmkGdD3XXUzPks46HhlZ4xPTJMZkQqcMstocXph4=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=mCLn2jdD1xVltJTKWJxyhFVgUIcTATx76fwA7sm0b7wN/my6MyCvhYPU48GX6enVz8CIMOD1qpbl1G67bjj0fZUPEzxjZcwBNqq8x6CNX+i7nK5Um5wNsX+tektZGUnCxKLu/+AtqXk99K+ls6cYnsGdsH/FNw/BYU6hdLDrTk0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F2B13858C66 Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8CxieBJtzZnpwk+AA--.56736S3; Fri, 15 Nov 2024 10:51:53 +0800 (CST) Received: from 5.. (unknown [10.2.6.5]) by front1 (Coremail) with SMTP id qMiowMCxyOJItzZn+iVWAA--.63928S4; Fri, 15 Nov 2024 10:51:52 +0800 (CST) From: Lulu Cai To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, mengqinggang@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, hejinyang@loongson.cn, Lulu Cai Subject: [PATCH v2] LoongArch: Add overflow check and new relaxations when relaxing pcalau12i+ld.d Date: Fri, 15 Nov 2024 10:51:51 +0800 Message-Id: <20241115025151.4020146-1-cailulu@loongson.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CM-TRANSID: qMiowMCxyOJItzZn+iVWAA--.63928S4 X-CM-SenderInfo: xfdlz3tox6z05rqj20fqof0/1tbiAgECB2c2hFEE2gADsf X-Coremail-Antispam: 1Uk129KBj93XoW3Xryrur15Xw1kAr4kWFWfZwc_yoWfKF48pr 9rZr93Ka18X3WxWr9rK34rua15XFn7uFy7tasrtr1IkrsrWry8Xa1Iyr4IgF43Xw43Z3Wf Zw1kt3ya9F4vywcCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkEb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y 6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AK xVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrx kI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v2 6r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8Jw CI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07jjwZcUUUUU = X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, 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~patchwork=sourceware.org@sourceware.org For pcalau12i+ld.d that can be relaxed, the relaxation is pcalau12i+ld.d => pcalau12i+addi.d => pcaddi and there is no overflow check. So the overflow check in the pcalau12i+ld.d relaxation and the relaxation of pcalau12i+ld.d => pcaddi are added. Changes from v1: * Add relaxation for pcalau12i+ld.d=>pcaddi. * Update test cases. v1: https://sourceware.org/pipermail/binutils/2024-November/137597.html --- bfd/elfnn-loongarch.c | 65 ++++++++++++--- .../ld-loongarch-elf/ld-loongarch-elf.exp | 1 + ld/testsuite/ld-loongarch-elf/relax-got.d | 79 +++++++++++++++++++ ld/testsuite/ld-loongarch-elf/relax-got.ld | 31 ++++++++ ld/testsuite/ld-loongarch-elf/relax-got.s | 69 ++++++++++++++++ 5 files changed, 233 insertions(+), 12 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/relax-got.d create mode 100644 ld/testsuite/ld-loongarch-elf/relax-got.ld create mode 100644 ld/testsuite/ld-loongarch-elf/relax-got.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 608d179c168..f079bb47703 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -5096,15 +5096,16 @@ loongarch_relax_call36 (bfd *abfd, asection *sec, asection *sym_sec, return true; } -/* Relax pcalau12i,ld.d => pcalau12i,addi.d. */ +/* Relax pcalau12i,ld.d => pcalau12i,addi.d + or => pcaddi. */ static bool loongarch_relax_pcala_ld (bfd *abfd, asection *sec, - asection *sym_sec ATTRIBUTE_UNUSED, + asection *sym_sec, Elf_Internal_Rela *rel_hi, - bfd_vma symval ATTRIBUTE_UNUSED, - struct bfd_link_info *info ATTRIBUTE_UNUSED, - bool *again ATTRIBUTE_UNUSED, - bfd_vma max_alignment ATTRIBUTE_UNUSED) + bfd_vma symval, + struct bfd_link_info *info, + bool *again, + bfd_vma max_alignment) { bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; Elf_Internal_Rela *rel_lo = rel_hi + 2; @@ -5113,12 +5114,55 @@ loongarch_relax_pcala_ld (bfd *abfd, asection *sec, uint32_t rd = LARCH_GET_RD (pca); uint32_t addi_d = LARCH_OP_ADDI_D; + /* This section's output_offset need to subtract the bytes of instructions + relaxed by the previous sections, so it needs to be updated beforehand. + size_input_section already took care of updating it after relaxation, + so we additionally update once here. */ + sec->output_offset = sec->output_section->size; + bfd_vma pc = sec_addr (sec) + rel_hi->r_offset; + + /* If pc and symbol not in the same segment, add/sub segment alignment. */ + if (!loongarch_two_sections_in_same_segment (info->output_bfd, + sec->output_section, + sym_sec->output_section)) + max_alignment = info->maxpagesize > max_alignment ? info->maxpagesize + : max_alignment; + + if (symval > pc) + pc -= (max_alignment > 4 ? max_alignment : 0); + else if (symval < pc) + pc += (max_alignment > 4 ? max_alignment : 0); + if ((ELFNN_R_TYPE (rel_lo->r_info) != R_LARCH_GOT_PC_LO12) || (LARCH_GET_RD (ld) != rd) || (LARCH_GET_RJ (ld) != rd) - || !LARCH_INSN_LD_D (ld)) + || !LARCH_INSN_LD_D (ld) + /* Within [-2G,2G) addressing range. */ + || (bfd_signed_vma)(symval - pc) < (bfd_signed_vma)(int32_t)0x80000000 + || (bfd_signed_vma)(symval - pc) > (bfd_signed_vma)(int32_t)0x7fffffff) return false; + /* Continue next relax trip. */ + *again = true; + + /* Can be relaxed to pcaddi? */ + if ((symval & 0x3) == 0 + && ((bfd_signed_vma)(symval - pc) >= (bfd_signed_vma)(int32_t)0xffe00000) + && ((bfd_signed_vma)(symval - pc) <= (bfd_signed_vma)(int32_t)0x1ffffc)) + { + const uint32_t pcaddi = LARCH_OP_PCADDI; + pca = pcaddi | rd; + bfd_put (32, abfd, pca, contents + rel_hi->r_offset); + + /* Adjust relocations. */ + rel_hi->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel_hi->r_info), + R_LARCH_PCREL20_S2); + rel_lo->r_info = ELFNN_R_INFO (0, R_LARCH_NONE); + loongarch_relax_delete_bytes (abfd, sec, rel_lo->r_offset, 4, info); + + return true; + } + addi_d = addi_d | (rd << 5) | rd; bfd_put (32, abfd, addi_d, contents + rel_lo->r_offset); @@ -5585,11 +5629,8 @@ loongarch_elf_relax_section (bfd *abfd, asection *sec, if (r_type == R_LARCH_GOT_PC_HI20 && !local_got) continue; - if (relax_func (abfd, sec, sym_sec, rel, symval, - info, again, max_alignment) - && relax_func == loongarch_relax_pcala_ld) - loongarch_relax_pcala_addi (abfd, sec, sym_sec, rel, symval, - info, again, max_alignment); + relax_func (abfd, sec, sym_sec, rel, symval, info, + again, max_alignment); } return true; diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 3313f72eee4..3eb8c59c0e9 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -47,6 +47,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "tls-le-relax" run_dump_test "relax-medium-call" run_dump_test "relax-medium-call-1" + run_dump_test "relax-got" } if [istarget "loongarch32-*-*"] { diff --git a/ld/testsuite/ld-loongarch-elf/relax-got.d b/ld/testsuite/ld-loongarch-elf/relax-got.d new file mode 100644 index 00000000000..ab6befc7f5c --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-got.d @@ -0,0 +1,79 @@ +#ld: -T relax-got.ld +#objdump: -D + +.* file format .* + + +Disassembly of section \.text: + +0+10000 <_start>: + 10000: 18080004 pcaddi \$a0, 16384 + 10004: 18080004 pcaddi \$a0, 16384 + 10008: 18080004 pcaddi \$a0, 16384 + 1000c: 1a01fe04 pcalau12i \$a0, 4080 + 10010: 02c00084 addi.d \$a0, \$a0, 0 + 10014: 1a01fe04 pcalau12i \$a0, 4080 + 10018: 02c02084 addi.d \$a0, \$a0, 8 + 1001c: 1a01fe04 pcalau12i \$a0, 4080 + 10020: 02c04084 addi.d \$a0, \$a0, 16 + 10024: 1a03fe04 pcalau12i \$a0, 8176 + 10028: 28c0c084 ld.d \$a0, \$a0, 48 + 1002c: 1a03fe04 pcalau12i \$a0, 8176 + 10030: 28c04084 ld.d \$a0, \$a0, 16 + 10034: 1a03fe04 pcalau12i \$a0, 8176 + 10038: 28c0e084 ld.d \$a0, \$a0, 56 + +0+1003c : + 1003c: 03400000 nop + +Disassembly of section \.pcaddi: + +0+20000 : + 20000: 19f801e4 pcaddi \$a0, -16369 + +0+20004 : + 20004: 19f801c4 pcaddi \$a0, -16370 + +0+20008 : + 20008: 19f801a4 pcaddi \$a0, -16371 + +Disassembly of section \.pcalau12i_addi: + +0+1000000 : + 1000000: 1bfe0204 pcalau12i \$a0, -4080 + 1000004: 02c0f084 addi.d \$a0, \$a0, 60 + +0+1000008 : + 1000008: 1bfe0204 pcalau12i \$a0, -4080 + 100000c: 02c0f084 addi.d \$a0, \$a0, 60 + +0+1000010 : + 1000010: 1bfe0204 pcalau12i \$a0, -4080 + 1000014: 02c0f084 addi.d \$a0, \$a0, 60 + +Disassembly of section \.got: + +0+2000000 <_GLOBAL_OFFSET_TABLE_>: + ... + 2000010: 80020008 .word 0x80020008 + ... + 2000030: 80020000 .word 0x80020000 + 2000034: 00000000 .word 0x00000000 + 2000038: 80020010 .word 0x80020010 + ... + 2000050: 0001003c .word 0x0001003c + 2000054: 00000000 .word 0x00000000 + +Disassembly of section \.pcalau12i_ld: + +0+80020000 : + 80020000: 1b03fc04 pcalau12i \$a0, -516128 + 80020004: 28c14084 ld.d \$a0, \$a0, 80 + +0+80020008 : + 80020008: 1b03fc04 pcalau12i \$a0, -516128 + 8002000c: 28c14084 ld.d \$a0, \$a0, 80 + +0+80020010 : + 80020010: 1b03fc04 pcalau12i \$a0, -516128 + 80020014: 28c14084 ld.d \$a0, \$a0, 80 diff --git a/ld/testsuite/ld-loongarch-elf/relax-got.ld b/ld/testsuite/ld-loongarch-elf/relax-got.ld new file mode 100644 index 00000000000..4510605caae --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-got.ld @@ -0,0 +1,31 @@ +OUTPUT_ARCH(loongarch) +ENTRY(_start) +SECTIONS +{ + PROVIDE (__executable_start = 0x8000); + . = 0x10000; + .text : + { + *(.text) + } =0 + . = 0x20000; + .pcaddi : + { + *(.pcaddi) + } + . = 0x1000000; + .pcalau12i_addi : + { + *(.pcalau12i_addi) + } + . = 0x2000000; + .got : + { + *(.got) + } + . = 0x80020000; + .pcalau12i_ld : + { + *(.pcalau12i_ld) + } +} diff --git a/ld/testsuite/ld-loongarch-elf/relax-got.s b/ld/testsuite/ld-loongarch-elf/relax-got.s new file mode 100644 index 00000000000..acc8d41f481 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-got.s @@ -0,0 +1,69 @@ + .text + .global _start +_start: + # relax to pcaddi + la.got $a0,sym_default_1 + la.got $a0,sym_hidden_1 + la.got $a0,sym_protected_1 + + # relax to pcalau12i + addi.d + la.got $a0,sym_default_2 + la.got $a0,sym_hidden_2 + la.got $a0,sym_protected_2 + + # dot not relax when overflow + la.got $a0,sym_default_3 + la.got $a0,sym_hidden_3 + la.got $a0,sym_protected_3 + + .global sym +sym: + nop + + + .section .pcaddi,"ax",@progbits + .global sym_default_1 +sym_default_1: + la.got $a0,sym + + .global sym_hidden_1 + .hidden sym_hidden_1 +sym_hidden_1: + la.got $a0,sym + + .global sym_protected_1 + .protected sym_protected_1 +sym_protected_1: + la.got $a0,sym + + + .section .pcalau12i_addi,"ax",@progbits + .global sym_default_2 +sym_default_2: + la.got $a0,sym + + .global sym_hidden_2 + .hidden sym_hidden_2 +sym_hidden_2: + la.got $a0,sym + + .global sym_protected_2 + .protected sym_protected_2 +sym_protected_2: + la.got $a0,sym + + + .section .pcalau12i_ld,"ax",@progbits + .global sym_default_3 +sym_default_3: + la.got $a0,sym + + .global sym_hidden_3 + .hidden sym_hidden_3 +sym_hidden_3: + la.got $a0,sym + + .global sym_protected_3 + .protected sym_protected_3 +sym_protected_3: + la.got $a0,sym