From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 6417E3858D32 for ; Mon, 5 Sep 2022 01:17:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6417E3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from [10.20.4.152] (unknown [10.20.4.152]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx32sOThVjWVYRAA--.832S3; Mon, 05 Sep 2022 09:17:02 +0800 (CST) Subject: Re: [PATCH v2] LoongArch:ld: Fix relocation error of pcrel. To: Fangrui Song Cc: binutils@sourceware.org, i.swmail@xen0n.name, xry111@xry111.site References: <20220903084149.1612310-1-liuzhensong@loongson.cn> From: liuzhensong Message-ID: Date: Mon, 5 Sep 2022 09:17:02 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------6CAEEBA6881DF774502049A5" Content-Language: en-US X-CM-TRANSID:AQAAf8Bx32sOThVjWVYRAA--.832S3 X-Coremail-Antispam: 1UD129KBjvJXoWxWFWfJF45Zr15uw4kZw4fKrg_yoWrCF1kpa sxuw1Sk3WrJFn3Wr9xGFW5XFn8JFZ7KFy2va4fJa1I9rs3try8Xw10yrW3AF43Aw45Jr1x Z3W0vry5uFn8Zw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkKb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I 8E87Iv6xkF7I0E14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAv7VC0I7IY x2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4 x0Y48IcVAKI48JMx8GjcxK6IxK0xIIj40E5I8CrwCYjI0SjxkI62AI1cAE67vIY487MxkI ecxEwVCm-wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F4 0E14v26r106r1rMI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1l IxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxV AFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j 6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU5fCzt UUUUU== X-CM-SenderInfo: holx6xphqv003j6o00pqjv00gofq/ X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,HTML_MESSAGE,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_PASS,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: This is a multi-part message in MIME format. --------------6CAEEBA6881DF774502049A5 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 在 2022/9/4 上午12:55, Fangrui Song 写道: > On Sat, Sep 3, 2022 at 1:43 AM liuzhensong wrote: >> Patch for branch 2.39. >> Need to reduce the address of pc when using >> reloction R_LARCH_SOP_PUSH_PCREL. >> >> bfd/ >> * elfnn-loongarch.c >> --- >> bfd/elfnn-loongarch.c | 3 +- >> ld/testsuite/ld-loongarch-elf/pcrel-const.d | 14 +++++++ >> ld/testsuite/ld-loongarch-elf/pcrel-const.lds | 14 +++++++ >> ld/testsuite/ld-loongarch-elf/pcrel-const.s | 12 ++++++ >> ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++ >> 5 files changed, 81 insertions(+), 1 deletion(-) >> create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.d >> create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.lds >> create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.s >> create mode 100644 ld/testsuite/ld-loongarch-elf/pr.exp >> >> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c >> index 5b44901b9e0..fafdc7c7458 100644 >> --- a/bfd/elfnn-loongarch.c >> +++ b/bfd/elfnn-loongarch.c >> @@ -2341,9 +2341,10 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, >> case R_LARCH_SOP_PUSH_PLT_PCREL: >> unresolved_reloc = false; >> >> - if (resolved_to_const) >> + if (!is_undefweak && resolved_to_const) >> { >> relocation += rel->r_addend; >> + relocation -= pc; >> break; >> } >> else if (is_undefweak) >> diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.d b/ld/testsuite/ld-loongarch-elf/pcrel-const.d >> new file mode 100644 >> index 00000000000..ae4705aa531 >> --- /dev/null >> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.d >> @@ -0,0 +1,14 @@ >> +#as: -mla-global-with-pcrel >> +#objdump: -Drsz >> + >> +.*:[ ]+file format .* >> + >> + >> +Disassembly of section .text: >> + >> +.* : >> +#... >> +[ ]+8:[ ]+02c04084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+16\(0x10\) >> +#... >> +0+14 <__sec_end>: >> +#pass >> diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.lds b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds >> new file mode 100644 >> index 00000000000..34b9861f4fa >> --- /dev/null >> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds >> @@ -0,0 +1,14 @@ >> +ENTRY(foo); >> +SECTIONS >> +{ >> + .text : { >> + *(.text*) >> + } >> + >> + .data : { >> + __sec_start = .; >> + *(.gzdata) >> + __sec_end = .; >> + } >> +} >> +PROVIDE(__sec_size = __sec_end); > __sec_size is now the address of __sec_end instead of a size. Is this > intentional? __sec_size is not just the size of .gzdata, but a symbol represents some addresses. > >> diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.s b/ld/testsuite/ld-loongarch-elf/pcrel-const.s >> new file mode 100644 >> index 00000000000..8b4a9d7aea0 >> --- /dev/null >> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s >> @@ -0,0 +1,12 @@ >> + .text >> + .align 2 >> + .globl foo >> + .type foo, @function >> +foo: >> + nop >> + la.global $r4,__sec_size >> + ldptr.w $r4,$r4,0 >> + jr $r1 >> + .size foo, .-foo >> + .data >> + .word 1 >> diff --git a/ld/testsuite/ld-loongarch-elf/pr.exp b/ld/testsuite/ld-loongarch-elf/pr.exp >> new file mode 100644 >> index 00000000000..c0cc2fc30d2 >> --- /dev/null >> +++ b/ld/testsuite/ld-loongarch-elf/pr.exp >> @@ -0,0 +1,39 @@ >> +# Expect script for LoongArch ELF linker tests >> +# Copyright (C) 2022 Free Software Foundation, Inc. >> +# >> +# This file is part of the GNU Binutils. >> +# >> +# This program is free software; you can redistribute it and/or modify >> +# it under the terms of the GNU General Public License as published by >> +# the Free Software Foundation; either version 3 of the License, or >> +# (at your option) any later version. >> +# >> +# This program is distributed in the hope that it will be useful, >> +# but WITHOUT ANY WARRANTY; without even the implied warranty of >> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> +# GNU General Public License for more details. >> +# >> +# You should have received a copy of the GNU General Public License >> +# along with this program; if not, write to the Free Software >> +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, >> +# MA 02110-1301, USA. >> +# >> + >> +if ![istarget loongarch64-*-*] { >> + return >> +} >> + >> +set link_tests [list \ >> + [list \ >> + "pcrel const" \ >> + "-T pcrel-const.lds" "" \ >> + "-mla-global-with-pcrel" \ >> + { pcrel-const.s } \ >> + [list \ >> + [list objdump -D pcrel-const.d] \ >> + ] \ >> + "pcrel-const" \ >> + ] \ >> +] >> + >> +run_ld_link_tests $link_tests >> -- >> 2.31.1 >> > --------------6CAEEBA6881DF774502049A5--