From patchwork Wed Dec 25 04:41:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 103676 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6E1F3858D1E for ; Wed, 25 Dec 2024 04:43:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6E1F3858D1E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=xry111.site header.i=@xry111.site header.a=rsa-sha256 header.s=default header.b=ja+n+tSG X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 2827A3858D21 for ; Wed, 25 Dec 2024 04:42:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2827A3858D21 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2827A3858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735101725; cv=none; b=XhoCDvHFbjucrwkyOfhvZiqQrn7tUvyW6cUUIYN/3ukfMJu6oWM4T/hcr7sQHbmtM9IPOAIvfJhUxIWh7iEcnK0m8t47oKB78oIHhIoHLfhm3I4/+OFDiZUIxLRYyESa0j4rAmwg/GzWwbfl92fRvLDzzg7xPleQ3zk4MRdI6X4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735101725; c=relaxed/simple; bh=pMuaXfrX2Sbvpxrh7DOVnCsNoxHWxL5jBZUsHtrD8Qw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JQ4GWC3Z99tfklzBKE074cnOnKQfnncoPSbIEQHEkfLPOr7IuAzlfjTYOLf4Yyf3xX0TweGtnig6Rmn0ci6sI0AI/Odm2G7ZDpkeD5j3Jbz+dFdxSL7z8aTEtiEtrMNHQUwCP3Lho3t1JJdmofahhvPkyAX6yB5BOpIlGIcz86Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2827A3858D21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xry111.site; s=default; t=1735101724; bh=jRsr4SfS7M/v3lrKWQDIVlUDnu8F2pzm/pCZtVjcBTo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ja+n+tSG1B4cC8k4HVm5i6G4RX2G9A4kJLsx5lpehh7/D05aSjFHQ4gWsexwfiG6X x/NPhoXkZvSAU7nLCIgqWDyF6PBI9vfRcay4ETCKm9v7l8Bbb7ZjGadP0TRobxk7E4 9KHpRWZz4H3nTdm4prcSJcG3oP0I7RMZQmbDIzjc= Received: from stargazer.. (unknown [113.200.174.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id C443966EC8; Tue, 24 Dec 2024 23:42:02 -0500 (EST) From: Xi Ruoyao To: binutils@sourceware.org Cc: Xi Ruoyao , mengqinggang@loongson.cn, Lulu Cai , Chenghua Xu , Huacai Chen , WANG Xuerui Subject: [PATCH v2 2/3] LoongArch: Allow R_LARCH_PCALA_HI20 or R_LARCH_PCREL20_S2 against undefined weak symbols for static PIE Date: Wed, 25 Dec 2024 12:41:44 +0800 Message-ID: <20241225044146.142652-3-xry111@xry111.site> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241225044146.142652-1-xry111@xry111.site> References: <20241225044146.142652-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, LIKELY_SPAM_FROM, 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 server2.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 In a static PIE, undefined weak symbols should be just resolved to runtime address 0, like those symbols with non-default visibility. This was silently broken in all prior Binutils releases with "-static-pie -mdirect-extern-access": $ cat t.c int x (void) __attribute__ ((weak)); int main (void) { __builtin_printf("%p\n", x); } $ gcc t.c -static-pie -mdirect-extern-access $ ./a.out 0x7ffff1d64000 Since commit 4cb77761d687 ("LoongArch: Check PC-relative relocations for shared libraries), the situation has been improved: the linker errors out instead of silently producing a wrong output file. But logically, using -mdirect-extern-access for a static PIE perfectly makes sense, and we should not prevent that even if the programmer uses weak symbols. Linux kernel is such an example, and Linux < 6.10 now fails to build with Binutils trunk. (The silent breakage with prior Binutils releases was "benign" due to some blind luck.) While since the 6.10 release Linux has removed those potentially undefined weak symbols (due to performance issue), we still should support weak symbols in -mdirect-extern-access -static-pie and unbreak building old kernels. Link: https://lore.kernel.org/loongarch/20241206085810.112341-1-chenhuacai@loongson.cn/ Signed-off-by: Xi Ruoyao --- bfd/elfnn-loongarch.c | 27 ++++++++++++------- .../ld-loongarch-elf/ld-loongarch-elf.exp | 1 + .../ld-loongarch-elf/weak-undef-static-pie.d | 13 +++++++++ .../ld-loongarch-elf/weak-undef-static-pie.s | 8 ++++++ 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 4cf1512cefe..9d3c0eef5e3 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1096,12 +1096,16 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, /* Since shared library global symbols interpose, any PC-relative relocations against external symbols - should not be used to build shared libraries. */ + should not be used to build shared libraries. + In static PIE undefined weak symbols may be allowed + by rewriting pcaddi to addi.w if addend is in [-2048, 2048). */ case R_LARCH_PCREL20_S2: if (bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0 && (sec->flags & SEC_READONLY) != 0 - && ! LARCH_REF_LOCAL (info, h)) + && ! LARCH_REF_LOCAL (info, h) + && (!info->nointerp + || h->root.type != bfd_link_hash_undefweak)) return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL); break; @@ -1124,12 +1128,16 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, } /* PC-relative relocations are allowed For first version - medium cmodel function call. */ + medium cmodel function call. Those against undefined + weak symbol are allowed for static PIE by rewritting + pcalau12i to lu12i.w. */ if (h != NULL && !h->needs_plt && bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0 && (sec->flags & SEC_READONLY) != 0 - && ! LARCH_REF_LOCAL (info, h)) + && ! LARCH_REF_LOCAL (info, h) + && (!info->nointerp + || h->root.type != bfd_link_hash_undefweak)) return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL); break; @@ -4113,12 +4121,13 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, case R_LARCH_PCALA_HI20: unresolved_reloc = false; - /* If sym is hidden undefined weak, (sym + addend) should be - resolved to runtime address (0 + addend). */ + /* If sym is undef weak and it's hidden or we are doing a static + link, (sym + addend) should be resolved to runtime address + (0 + addend). */ resolve_pcrel_undef_weak = - (is_undefweak - && h - && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT); + ((info->nointerp + || (h && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)) + && is_undefweak); if (resolve_pcrel_undef_weak) pc = 0; diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 35a3a4fc430..0b609c0245b 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -196,6 +196,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "relr-text-pie" run_dump_test "abssym_pie" run_dump_test "weak-undef-hidden-pie" + run_dump_test "weak-undef-static-pie" } run_dump_test "max_imm_b16" diff --git a/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d new file mode 100644 index 00000000000..d95aa122fce --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d @@ -0,0 +1,13 @@ +#ld: -static -pie --no-dynamic-linker -z text -Ttext=0x1111222233334ff0 +#objdump: -d + +#... +1111222233334ff0:[ ]+159999a4[ ]+lu12i.w[ ]+\$a0,[ ]+-209715 +1111222233334ff4:[ ]+02f77405[ ]+li.d[ ]+\$a1,[ ]+-547 +1111222233334ff8:[ ]+17777765[ ]+lu32i.d[ ]+\$a1,[ ]+-279621 +1111222233334ffc:[ ]+032aa8a5[ ]+lu52i.d[ ]+\$a1,[ ]+\$a1,[ ]+-1366 +1111222233335000:[ ]+00109484[ ]+add.d[ ]+\$a0,[ ]+\$a0,[ ]+\$a1 +1111222233335004:[ ]+029ffc06[ ]+li.w[ ]+\$a2,[ ]+2047 +1111222233335008:[ ]+02a00007[ ]+li.w[ ]+\$a3,[ ]+-2048 +111122223333500c:[ ]+4c000020[ ]+ret +#pass diff --git a/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s new file mode 100644 index 00000000000..d6b2c7a6c20 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s @@ -0,0 +1,8 @@ +.weak undef + +.globl _start +_start: + la.pcrel $a0, $a1, undef + 0xaaabbbbbcccccddd + pcaddi $a2, undef + 0x7ff + pcaddi $a3, undef - 0x800 + ret