From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id BCCF23858C56 for ; Fri, 14 Oct 2022 05:08:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BCCF23858C56 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-ot1-x331.google.com with SMTP id t4-20020a9d7f84000000b00661c3d864f9so1262864otp.10 for ; Thu, 13 Oct 2022 22:08:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.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 :message-id:reply-to; bh=N1e6ABjP6wTwqT0ZXselWGkcbPZXlIVpGro//9qzlAA=; b=sK7uMWYnsEHoN3i9zWrka5xtJ/jFTdYiLJMSk3EfxWvP2m2e2wBkX3T8JlyQobQ8BM cUS+ds7ajtrGqmogiNubGdE4p1PfYSH3SdiFWVHia8+PjcljRL7A0tOm779cl2wGrxKu cqCdgbxF7V7HQmbMiF5SRwXy+L1Tc6kugTdd9pDgp2C71dErR3LBe15/aeirpoehCPgg 8UNhJn6PjfECvj9nIF/HwiKe8FkWcqPZvWxrGr9tSLCJAXEXqVRmFK0cLj6oJ3h7Q9zo Q+oUT6lYKQAlU+I6ezDQxw/ueRwKAkYp7NE5CZkaIDCDzTXkrNIa7V600KCt9TReEk0u jIzQ== 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:message-id:reply-to; bh=N1e6ABjP6wTwqT0ZXselWGkcbPZXlIVpGro//9qzlAA=; b=xfiGZVt/GNOWzhXOCZ8Mm6CLiLN+tMQF5CRsuVN0f3ymJaDyc1nb7pFBYZE9OSK1D/ pwR5VwmPENEKYUhkPAPEzAZBiqb4sfwOC1no9oDv9Lzjc1zSMZQRQ3Zrr612LlbNIZQ5 nyQjU6PvBaT3oCYkfM72U7NUzH9Uym2PjEwJmizi5+R/6XzRg/0qbrl+WWi98o034YmB LPGeYewc6kbx/9wjghDEJRr1/3Wqud2i83bmKyJucFhDpzG7iz1u2xQMiP3yqv6v+Heo 4AVZUK3x1yo+P4ukR6YW5xyPpWbLOQx5ZzltT2wKe47SBRHOvE3nVeQy/nwenBaYwcPE 3WBA== X-Gm-Message-State: ACrzQf2VR935rAZ85xbYmei7SciDCPF+TtA1FY83KW4bKYLnHhSLD5Xr n3SnTnP22fRqakhgqgtnv9enplV1HPnglTvmw64qcYDkWqUb5g== X-Google-Smtp-Source: AMsMyM4hOhZNWORpUR2IZu70+FvsuQaEo0F4001f2TNB8sKdJ2UiOJXrmftG4qcdxOfLUdpVKH3ZjmMSSBJuRWXzIfk= X-Received: by 2002:a9d:729e:0:b0:661:9724:7c65 with SMTP id t30-20020a9d729e000000b0066197247c65mr1730236otj.320.1665724095023; Thu, 13 Oct 2022 22:08:15 -0700 (PDT) MIME-Version: 1.0 References: <20221006114628.304185-1-chigot@adacore.com> In-Reply-To: From: Nelson Chu Date: Fri, 14 Oct 2022 13:08:04 +0800 Message-ID: Subject: Re: [PATCH] RISC-V: fix linker message when relaxation deletes bytes To: =?UTF-8?Q?Cl=C3=A9ment_Chigot?= Cc: binutils@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: Hi, On Thu, Oct 13, 2022 at 8:06 PM Cl=C3=A9ment Chigot via Binutils wrote: > > Gentle ping > > Thanks, > Cl=C3=A9ment > > On Thu, Oct 6, 2022 at 1:46 PM Cl=C3=A9ment Chigot w= rote: > > > > The section relaxation can delete some bytes resulting in the symbols' > > value being modified. > > As the linker messages retrieve a symbol information using the > > outsymbols field of abfd, it must be updated as well. Interesting, I never noticed this. If this is necessary, then other targets should also update the output symbols when relaxing (nds32 and sh), but it seems like they don't do that as well. > > bfd/ChangeLog: > > > > * elfnn-riscv.c (riscv_relax_delete_bytes): Update > > abfd->outsymbols. > > > > ld/ChangeLog: > > > > * testsuite/ld-riscv-elf/ld-riscv-elf.exp: New test. > > * testsuite/ld-riscv-elf/undefined-align.d: New test. > > * testsuite/ld-riscv-elf/undefined-align.s: New test. > > --- > > bfd/elfnn-riscv.c | 23 +++++++++++++++++++++ > > ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 + > > ld/testsuite/ld-riscv-elf/undefined-align.d | 5 +++++ > > ld/testsuite/ld-riscv-elf/undefined-align.s | 10 +++++++++ > > 4 files changed, 39 insertions(+) > > create mode 100644 ld/testsuite/ld-riscv-elf/undefined-align.d > > create mode 100644 ld/testsuite/ld-riscv-elf/undefined-align.s > > > > diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c > > index 3d2ddf4e651..7a6b66dcd8a 100644 > > --- a/bfd/elfnn-riscv.c > > +++ b/bfd/elfnn-riscv.c > > @@ -4060,6 +4060,7 @@ riscv_relax_delete_bytes (bfd *abfd, > > unsigned int sec_shndx =3D _bfd_elf_section_from_bfd_section (abfd, = sec); > > struct bfd_elf_section_data *data =3D elf_section_data (sec); > > bfd_byte *contents =3D data->this_hdr.contents; > > + asymbol **outsyms =3D bfd_get_outsymbols (abfd); > > > > /* Actually delete the bytes. */ > > sec->size -=3D count; > > @@ -4158,6 +4159,28 @@ riscv_relax_delete_bytes (bfd *abfd, > > } > > } > > > > + /* As linker messages are getting symbols through outsymbols field o= f abfd, > > + it must be adjusted too. */ > > + if (outsyms =3D=3D NULL) > > + { > > + if (!bfd_generic_link_read_symbols (abfd)) > > + link_info->callbacks->einfo (_("%F%P: %pB: could not read symbo= ls: %E\n"), abfd); > > + outsyms =3D bfd_get_outsymbols (abfd); > > + } > > + > > + for (i =3D 0; i < bfd_get_symcount (abfd); i++) > > + { > > + asymbol *sym =3D outsyms[i]; > > + > > + if (sym->section !=3D sec) > > + continue; > > + > > + /* If the symbol is in the range of memory we just moved, we > > + have to adjust its value. */ > > + if (sym->value > addr && sym->value <=3D toaddr) > > + sym->value -=3D count; > > + } > > + > > return true; > > } > > > > diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/= ld-riscv-elf/ld-riscv-elf.exp > > index df89e0ee68b..86f1d05bc08 100644 > > --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > > +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > > @@ -176,6 +176,7 @@ if [istarget "riscv*-*-*"] { > > [list "Weak reference 64" "-T weakref.ld -m[riscv_choose_lp64_e= mul]" "" \ > > "-march=3Drv64i -mabi=3Dlp64" {weakref64.s} \ > > {{objdump -d weakref64.d}} "weakref64"]] > > + run_dump_test "undefined-align" > > > > # The following tests require shared library support. > > if ![check_shared_lib_support] { > > diff --git a/ld/testsuite/ld-riscv-elf/undefined-align.d b/ld/testsuite= /ld-riscv-elf/undefined-align.d > > new file mode 100644 > > index 00000000000..c8cbb84ac7c > > --- /dev/null > > +++ b/ld/testsuite/ld-riscv-elf/undefined-align.d > > @@ -0,0 +1,5 @@ > > +#name: undefined with alignment > > +#source: undefined-align.s > > +#as: > > +#ld: --relax > > +#error: \A[^\n]*\.o: in function `_start':\n\(\.text\+0x0\): undefined= reference to `foo'\Z > > diff --git a/ld/testsuite/ld-riscv-elf/undefined-align.s b/ld/testsuite= /ld-riscv-elf/undefined-align.s > > new file mode 100644 > > index 00000000000..577557c663a > > --- /dev/null > > +++ b/ld/testsuite/ld-riscv-elf/undefined-align.s > > @@ -0,0 +1,10 @@ > > +# Make sure that the linker messages take into account the modificatio= n > > +# of a symbol's value made during the section relaxation. > > +# Here, "_start" will have an offset made by ".align 4" which will be > > +# removed during the relaxation of R_RISCV_ALIGN. > > + .text > > + .align 4 > > + .globl _start > > + .type _start, @function > > +_start: > > + call foo Tested with undefined-align.s, I get the following error message before applying this patch, % riscv64-unknown-elf-ld tmp.o riscv64-unknown-elf-ld: tmp.o:(.text+0x0): undefined reference to `foo' And get the error after applying this patch, % riscv64-unknown-elf-ld tmp.o riscv64-unknown-elf-ld: tmp.o: in function `_start': (.text+0x0): undefined reference to `foo' It reports more detail, but I cannot see if the removed offset may cause the wrong message? Thanks Nelson > > -- > > 2.25.1 > >