From patchwork Mon May 11 04:28:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivian Wang X-Patchwork-Id: 134749 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6DFB4BA23D3 for ; Mon, 11 May 2026 04:28:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6DFB4BA23D3 X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from cstnet.cn (smtp25.cstnet.cn [159.226.251.25]) by sourceware.org (Postfix) with ESMTPS id 2232C4BA23CC for ; Mon, 11 May 2026 04:28:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2232C4BA23CC Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=iscas.ac.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2232C4BA23CC Authentication-Results: sourceware.org; arc=none smtp.remote-ip=159.226.251.25 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778473698; cv=none; b=hy6cOSQkWTYYR3PludcHWbJFzFKZU8M6tr3PT2T0Ww5jlUaIlrbZOY1VDTXvP009jcs6HJuM9TL5ZBuG1UfWB3z3C1cEql1jVhXEGvnySJia+HzsILNJbRwh6SrDtIh13N+u6xOSKt1FyiCuY14BReUswtlbnh+EuG89DBCDTR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778473698; c=relaxed/simple; bh=0HPiyLDJwdp3EHxLxFWv6XP8KygO/DeIVwGD8Nh8PAI=; h=Message-ID:Date:MIME-Version:To:From:Subject; b=LEl3f1AHn4xpfVqKvmN6zBjKThA66nDwW6+lwZOtP9eiOZ+MAMhVui7imWptqk2odseWyAmF3u0m1BTnwKPuPU4U79uNCl7MuZY3BZQI+SM86tV5BgCmavt7t0gIwG5eGnhMOHtilT9bT4VH7N1J/AmJftTbIiyXkYteZhBgRDU= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2232C4BA23CC Received: from [10.213.19.70] (unknown [36.110.52.2]) by APP-05 (Coremail) with SMTP id zQCowAAHlgnfWgFqOhzvDw--.1089S2; Mon, 11 May 2026 12:28:15 +0800 (CST) Message-ID: <9253a1f8-4f1a-4482-801b-a1e05d91c5ea@iscas.ac.cn> Date: Mon, 11 May 2026 12:28:14 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: binutils@sourceware.org Cc: Nelson Chu , Palmer Dabbelt , Andrew Waterman , Jim Wilson From: Vivian Wang Subject: [PATCH] RISC-V: Also output undefined symbols as dynamic symbols X-CM-TRANSID: zQCowAAHlgnfWgFqOhzvDw--.1089S2 X-Coremail-Antispam: 1UD129KBjvJXoWxXr4kKrWxAry8XrWxXr4xWFg_yoWrJr17pr 98tF12qry8JFZ2gw17JFWYgF13ua4kua17Jr90ga1vkr98Jr1fXFZIgrWfuay5K3Z7J3W7 ZFWIv3y5uryrZrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkFb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVW8JVWxJw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7CjxVAaw2AFwI0_JF0_Jw1l42xK 82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGw C20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48J MIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMI IF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E 87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07bwIDcUUUUU= X-Originating-IP: [36.110.52.2] X-CM-SenderInfo: pzdqw2pxlnt03j6l2u1dvotugofq/ X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 Consider this program, which refers to an undefined symbol foo: int foo(void); int main() { foo(); } If linking with --unresolved-symbols=ignore-in-object-file, what should happen is the symbol foo is left undefined and left for the dynamic linker to resolve. (See commit 4295841be439 ("RISC-V: Go PLT for CALL/JUMP/RVC_JUMP if `h->plt.offset' isn't -1").) Instead, with binutils 2.46: 1. With -no-pie, ld generates an auipc/jalr call to address 0, crashing at runtime instead of generating an undefined symbol message. 2. With -pie, ld generates this bogus error message (-fPIC does not change this, and R_RISCV_CALL_PLT is correct here) and stops, failing to produce an executable: ld: relocation R_RISCV_CALL_PLT against `foo' which may bind externally can not be used when making a shared object; recompile with -fPIC Bisection leads to commit 9e10fcf71c11 ("RISC-V: Fix the assert fail when linking discarded sections under -pie for got"), which stopped outputting dynamic symbols for non-undefweak symbols. Therefore, in order to fix the issue, also output undefined symbols as dynamic symbols. Fixes test case ld-elf/dwarf.exp, specifically dwarf3, where the bogus error prevents the second "undefined reference" error from appearing. No regressions found for the ld tests on riscv64. bfd/: * elfnn-riscv.c (allocate_dynrelocs): Also output undefined symbols as dynamic symbols Fixes: 9e10fcf71c11 ("RISC-V: Fix the assert fail when linking discarded sections under -pie for got") --- This is my first patch to binutils. Please advise if I'm doing something wrong. Thanks. Vivian "dramforever" Wang --- bfd/elfnn-riscv.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index eb3d7926c..d66c42b78 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -1439,10 +1439,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) && h->plt.refcount > 0) { /* Make sure this symbol is output as a dynamic symbol. - Undefined weak syms won't yet be marked as dynamic. */ + Undefined and undefweak syms won't yet be marked as dynamic. */ if (h->dynindx == -1 && !h->forced_local - && h->root.type == bfd_link_hash_undefweak + && (h->root.type == bfd_link_hash_undefweak + || h->root.type == bfd_link_hash_undefined) && !bfd_elf_link_record_dynamic_symbol (info, h)) return false; @@ -1500,11 +1501,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) int tls_type = riscv_elf_hash_entry (h)->tls_type; /* Make sure this symbol is output as a dynamic symbol. - Undefined weak syms won't yet be marked as dynamic. */ + Undefined and undefweak syms won't yet be marked as dynamic. */ if (dyn && h->dynindx == -1 && !h->forced_local - && h->root.type == bfd_link_hash_undefweak + && (h->root.type == bfd_link_hash_undefweak + || h->root.type == bfd_link_hash_undefined) && !bfd_elf_link_record_dynamic_symbol (info, h)) return false; @@ -1611,10 +1613,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) || h->root.type == bfd_link_hash_undefined)))) { /* Make sure this symbol is output as a dynamic symbol. - Undefined weak syms won't yet be marked as dynamic. */ + Undefined and undefweak syms won't yet be marked as dynamic. */ if (h->dynindx == -1 && !h->forced_local - && h->root.type == bfd_link_hash_undefweak + && (h->root.type == bfd_link_hash_undefweak + || h->root.type == bfd_link_hash_undefined) && !bfd_elf_link_record_dynamic_symbol (info, h)) return false;