From patchwork Thu Oct 6 11:46:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Cl=C3=A9ment_Chigot?= X-Patchwork-Id: 1781 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp267872wrs; Thu, 6 Oct 2022 04:47:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7D7Cdi26iIeVJ/svyANjvKnZgcW2JP+75y/79HB/2xFW42WrNEHPysN4IscFE1jtNu1CqK X-Received: by 2002:a17:907:a064:b0:78d:3add:d2a8 with SMTP id ia4-20020a170907a06400b0078d3addd2a8mr3545193ejc.451.1665056835028; Thu, 06 Oct 2022 04:47:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665056835; cv=none; d=google.com; s=arc-20160816; b=odQ3JFtRRsK2M/MGeX/vgiTyyGQbcKpWaaZ1JwZozZB9o9KHTdwFljnZLQ0Arwak7V aDMOg5OhI/VsCjEO5+fX2H5F1JUzF096kInY36xxWI1wcm7tOli8s1Oy45oC9U3Peho4 FARaQXOj6/W8VC5m3YS/UFMd09a8hfa5og+rOtYJXhy84/mvE6vK+rRAPMSzQyQQa9ni K7nrNAG8vvpOej4HBUtv75PDGYNZwZLfqOLDSm9NpSNWgkhIhRdYyukqtA6/n5PA+Vty mv3v/E1k3l2eQbk5PMys1kMt6BQjaZJmaN5gxogZiBDimiK+2XVpGxRt6X+xe1jLFH2M 4L8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=fww+p/OHtQF3lNhRBehkAOkxgX9zFfq1+M1BQl/O2hY=; b=YzDQ6aP/EYuyvE2eMl0O+apGTaIfAdpOEb78gWl+AqbbbwlPD2bu3BkGnyphLjonFI fRZxA+UFXRm5ijSMEllJ+siz5V6gimCQ5V4+AjAuNH+/KDNiCcOD17TiRbYlha+/pdGK d2JyFP6UlMvLOUq1Z/C9zH77SEMJGFjiJpDWj2St6Oz5GMg6RJPABuiqPJPxeIE3w2Dl u08N/MLuNBRk2cG9Z3zUW3+gcMt3imhzuygRQopVCrDMzLjRfmPG8txHOcoMctpUVA61 b5tmPpKgrHwerpq0hLeP2OMotuBoys4qZLMReONJT07pHWGU/0L6bk1acjFvPpza/g29 clbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="n/lmxEHT"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ht19-20020a170907609300b007836227b96fsi16328279ejc.888.2022.10.06.04.47.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 04:47:15 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="n/lmxEHT"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C82BD381FE6D for ; Thu, 6 Oct 2022 11:47:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C82BD381FE6D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1665056833; bh=fww+p/OHtQF3lNhRBehkAOkxgX9zFfq1+M1BQl/O2hY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n/lmxEHTkssQnWR+9Ua/Jr+Xp7qlr2NfGNsKu6ctBbNCkqWJvvWJg5gaGFqFcmKTQ WoLvucxlYzMxbchEKVU0+Fpef1LAgfshumHpPPcnfBtZ4d1vZZ48Q9CvFxIZfGWI9X jhI6PruqBZk0CvxDGSgVzfU6uTo0qlnoLGux2xZk= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 9FD9F3854164 for ; Thu, 6 Oct 2022 11:46:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9FD9F3854164 Received: by mail-wr1-x42d.google.com with SMTP id j7so2324582wrr.3 for ; Thu, 06 Oct 2022 04:46:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=fww+p/OHtQF3lNhRBehkAOkxgX9zFfq1+M1BQl/O2hY=; b=40gUZzHY3zvt1TTGzie5FJUl3gwk0dnTSxkXSmejUZo6V+v9rxTEAumfkSDEce8Cw7 24kvO/pt7TgyLF5DP0wqrCyKw/kIp5TkiwDBrCigWlRfnc7i3enFecbInLS+iqb9DytU jw0/UylShqzDOIaLjO5ugICOH+uvJVKoSzKT1HhRzNE1ZGp0bNKp1N0hBCBMtCXEF7uS aKCMgQeRC1MAlKvxoAXmmvaY/fMszrhDA1f1iSpPmWO2XTB9NVSZ+kdlHnZrhDQ45ZuM 99j6Vg/7yjo4NgjL3NFOAYJayjSrBv3AqN9XyvZAG94YzCOD9F9+iDOQjGvgtz9EmC1p U+rQ== X-Gm-Message-State: ACrzQf0fLBcyiL7lOgpMDOd77hQlxYE8DEQQJIP0fwBstlK6RYKwqMIT ehx3ug7ecByl48woHgbGPACVy5BCcnIIYQ== X-Received: by 2002:a05:6000:144f:b0:22a:7098:6472 with SMTP id v15-20020a056000144f00b0022a70986472mr2926334wrx.685.1665056811153; Thu, 06 Oct 2022 04:46:51 -0700 (PDT) Received: from chigot-Dell.home ([2a01:cb15:8123:8100:c0cd:af8a:42c9:f157]) by smtp.gmail.com with ESMTPSA id f6-20020a1c3806000000b003b3365b38f9sm4642364wma.10.2022.10.06.04.46.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 04:46:50 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH] RISC-V: fix linker message when relaxation deletes bytes Date: Thu, 6 Oct 2022 13:46:28 +0200 Message-Id: <20221006114628.304185-1-chigot@adacore.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Cl=C3=A9ment_Chigot_via_Binutils?= From: =?utf-8?q?Cl=C3=A9ment_Chigot?= Reply-To: =?utf-8?q?Cl=C3=A9ment_Chigot?= Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745938635373515119?= X-GMAIL-MSGID: =?utf-8?q?1745938635373515119?= 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. 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 = _bfd_elf_section_from_bfd_section (abfd, sec); struct bfd_elf_section_data *data = elf_section_data (sec); bfd_byte *contents = data->this_hdr.contents; + asymbol **outsyms = bfd_get_outsymbols (abfd); /* Actually delete the bytes. */ sec->size -= count; @@ -4158,6 +4159,28 @@ riscv_relax_delete_bytes (bfd *abfd, } } + /* As linker messages are getting symbols through outsymbols field of abfd, + it must be adjusted too. */ + if (outsyms == NULL) + { + if (!bfd_generic_link_read_symbols (abfd)) + link_info->callbacks->einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd); + outsyms = bfd_get_outsymbols (abfd); + } + + for (i = 0; i < bfd_get_symcount (abfd); i++) + { + asymbol *sym = outsyms[i]; + + if (sym->section != 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 <= toaddr) + sym->value -= 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_emul]" "" \ "-march=rv64i -mabi=lp64" {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 modification +# 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