From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id CFB5F3858CDB for ; Sat, 3 Sep 2022 16:56:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CFB5F3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=google.com Received: by mail-lj1-x231.google.com with SMTP id z20so5189795ljq.3 for ; Sat, 03 Sep 2022 09:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=Puuy7J7zBE3w9ieYIXRciwTrweDQoy2jMZvwn6Hh08s=; b=L0BbiJ1qd6p50HjtiQs9jWq3rTAOJcHatznXQQyTV42hdvXQEqw6r5C2eKaOMXVTQB 7dEw62+r0doUsG9B0a1RsaPYY6PQLbqfrLdoK3Fzt9yEEEId35PMffeQyv7aUEchJKqp d6TLg013zBExOd4GURlLEzQFfOiYp1dt/2V5fcTSA7PWBy/v/hXeaWtG9Ora4kr5Mey6 hfxPfPIgRje8cUVvVm9RqjxBbebl5lEU7uz9EhP8Tgq0vq1ycV3QYz7n1vM6qfh4P/4/ crd8jCK59vxqNmGYjPg1Fll6ScFhu8VEwxmgF1HF07icjZXidHvMp2Q3vrwiczcrfXc6 pOiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=Puuy7J7zBE3w9ieYIXRciwTrweDQoy2jMZvwn6Hh08s=; b=u20686+so/CeRhrpid4QJG/XkW1isQD8VFrlmX5byVtvothAi4h7DdiQCSIOkzchL6 V244QYn2KI8nx45FufrY2+UmF/7KaFqH9QhY18AsKpvj4+611kb8511uNHt52RNOS8Bi bjNFIpJQSycWbTB90e+5kulGlbX+wjDMPfc1R53GHbhxXivoNn6ih+SDTqkPK+Z6YcpI YatkVZUB9Rhb7/dLBv0szHiVOlF1YUgFpjqenOUjuo2iHt/agkGL5HDSvfSiA1lfld5r F5McwGY5hu5rir3fnyJUOOfs5P57PAD7aYKYWB9+867GEcknIOe7CBDEGzfR8Ibb5Qwo rGvg== X-Gm-Message-State: ACgBeo0G1f3iVDJM56CJjlRjtHMQV96JQBdGJws0T0PXX4aAeYx+4VPb N9q6z8HXHG2JpoFNeFULCZ+1O15a7KsxQNipbhI0xA== X-Google-Smtp-Source: AA6agR4G9iFOuresF7NMvfcDLeQDScD+EvCTGDR5TNL7NM0xNsaqKXEk2TIJMK+Vdf4dW/bUpinEZrBAH1ZF+qMkwIU= X-Received: by 2002:a2e:864d:0:b0:266:20a7:eba with SMTP id i13-20020a2e864d000000b0026620a70ebamr7040416ljj.304.1662224161894; Sat, 03 Sep 2022 09:56:01 -0700 (PDT) MIME-Version: 1.0 References: <20220903084149.1612310-1-liuzhensong@loongson.cn> In-Reply-To: <20220903084149.1612310-1-liuzhensong@loongson.cn> From: Fangrui Song Date: Sat, 3 Sep 2022 09:55:49 -0700 Message-ID: Subject: Re: [PATCH v2] LoongArch:ld: Fix relocation error of pcrel. To: liuzhensong Cc: binutils@sourceware.org, i.swmail@xen0n.name, xry111@xry111.site Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-22.9 required=5.0 tests=BAYES_00,BODY_8BITS,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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: 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, s= truct bfd_link_info *info, > case R_LARCH_SOP_PUSH_PLT_PCREL: > unresolved_reloc =3D false; > > - if (resolved_to_const) > + if (!is_undefweak && resolved_to_const) > { > relocation +=3D rel->r_addend; > + relocation -=3D pc; > break; > } > else if (is_undefweak) > diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.d b/ld/testsuite/l= d-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 =3D .; > + *(.gzdata) > + __sec_end =3D .; > + } > +} > +PROVIDE(__sec_size =3D __sec_end); __sec_size is now the address of __sec_end instead of a size. Is this intentional? > diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.s b/ld/testsuite/l= d-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-loong= arch-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 > --=20 =E5=AE=8B=E6=96=B9=E7=9D=BF