From patchwork Fri Dec 13 05:53:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 102951 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 9D4003858D35 for ; Fri, 13 Dec 2024 05:58:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D4003858D35 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=VWvy/Kri 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 926C6385840B for ; Fri, 13 Dec 2024 05:54:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 926C6385840B 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 926C6385840B 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=1734069297; cv=none; b=TZxMbquXu+d+6kQJVuW+vQy5EA7qTKsSjRwTd8GngMg9EpRFh54BPCVrdHbx9XCOVtfO0VRq8gHeLGzjCDrRYSaKLKTSRKv/RDMm2ZEI93dyuO9LH0nbKJ3FJbB/HyGnS/+J2kGjr/ie18vyw8gVGB1oxO1m4ZtdE7PfwZa9jX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734069297; c=relaxed/simple; bh=1/op7VmudnaRZRmy4XaoZcRs9pggZGSX73AMJZ0aCSM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=khNsTRF4su8FfjbtDuNjH/yi/jrbSVWlKosyMv3e+mEKiSe6sIYUg7/U2kOnjj/maufyhGTVAvLawKUc5Rk2/5JF9l7NnL8R1VxacN59AbBf49CD0NNWNNiAgbo+MBU6qy8dMK9Pc9CPUGOtkae2lLderRSe7K1lmTOXRuJ6+jY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 926C6385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xry111.site; s=default; t=1734069296; bh=8OXG2PfME6Fey1iHwuZwDQagAV4Cm9JU56KnivrL+EM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VWvy/Kri/ZMRuvHz/PQ1AZDkaxuVoPwJUPGo3OQ1xas5q+bGkDskCozKI87vfkpQs ajcaaldQGWX2iPimvXo9Q6F9h4vhg3kXw60vm1Gsgyxd2DrutHw01f3MtqkHI0InRc J8b1//s9hnJJ2rIZjedAL7/vdJU7PfW/o1zcee9Q= Received: from stargazer.. (unknown [IPv6:240e:457:1120:870:571c:7044:9d4:458a]) (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 B1B49676A2; Fri, 13 Dec 2024 00:54:51 -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 1/3] LoongArch: Fix resolution of undefined weak hidden/protected symbols Date: Fri, 13 Dec 2024 13:53:50 +0800 Message-ID: <20241213055427.579306-2-xry111@xry111.site> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241213055427.579306-1-xry111@xry111.site> References: <20241213055427.579306-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 An undefined weak hidden/protect symbol should be resolved to runtime address 0, but we were actually resolving it to link-time address 0. So in PIE or DSO the runtime address would be incorrect. Fix the issue by rewriting pcalau12i to lu12i.w, and pcaddi to addi.w. The latter does not always work because the immediate field of addi.w is narrower, report an error in the case the addend is too large. Signed-off-by: Xi Ruoyao --- bfd/elfnn-loongarch.c | 83 ++++++++++++++----- include/opcode/loongarch.h | 2 + .../ld-loongarch-elf/ld-loongarch-elf.exp | 2 + .../ld-loongarch-elf/weak-undef-hidden-pie.d | 14 ++++ .../weak-undef-hidden-shared.d | 14 ++++ .../ld-loongarch-elf/weak-undef-hidden.s | 9 ++ 6 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-hidden-pie.d create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-hidden-shared.d create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-hidden.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 2ce5f73ae80..b657ddc5666 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -3256,6 +3256,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, char tls_type; bfd_vma relocation, off, ie_off, desc_off; int i, j; + bool resolve_pcrel_undef_weak = false; /* When an unrecognized relocation is encountered, which usually occurs when using a newer assembler but an older linker, an error @@ -4107,23 +4108,74 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, break; + case R_LARCH_PCALA64_HI12: + pc -= 4; + /* Fall through. */ + case R_LARCH_PCALA64_LO20: + pc -= 8; + /* Fall through. */ case R_LARCH_PCREL20_S2: - unresolved_reloc = false; - if (h && h->plt.offset != MINUS_ONE) - relocation = sec_addr (plt) + h->plt.offset; - else - relocation += rel->r_addend; - relocation -= pc; - break; - case R_LARCH_PCALA_HI20: unresolved_reloc = false; + + /* If sym is hidden undefined weak, (sym + addend) should be + resolved to runtime address (0 + addend). */ + resolve_pcrel_undef_weak = + (is_undefweak + && h + && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT); + + if (resolve_pcrel_undef_weak) + pc = 0; + if (h && h->plt.offset != MINUS_ONE) relocation = sec_addr (plt) + h->plt.offset; else relocation += rel->r_addend; - RELOCATE_CALC_PC32_HI20 (relocation, pc); + switch (r_type) + { + case R_LARCH_PCREL20_S2: + relocation -= pc; + if (resolve_pcrel_undef_weak) + { + bfd_signed_vma addr = (bfd_signed_vma) relocation; + if (addr >= 2048 || addr < -2048) + { + const char *msg = + _("cannot resolve R_LARCH_PCREL20_S2 against " + "undefined weak symbol with addend out of " + "[-2048, 2048)"); + fatal = + loongarch_reloc_is_fatal (info, input_bfd, + input_section, rel, + howto, + bfd_reloc_notsupported, + is_undefweak, name, msg); + break; + } + + uint32_t insn = bfd_get (32, input_bfd, + contents + rel->r_offset); + insn = LARCH_GET_RD (insn) | LARCH_OP_ADDI_W; + insn |= (relocation & 0xfff) << 10; + bfd_put_32 (input_bfd, insn, contents + rel->r_offset); + r = bfd_reloc_continue; + } + break; + case R_LARCH_PCALA_HI20: + RELOCATE_CALC_PC32_HI20 (relocation, pc); + if (resolve_pcrel_undef_weak) + { + uint32_t insn = bfd_get (32, input_bfd, + contents + rel->r_offset); + insn = LARCH_GET_RD (insn) | LARCH_OP_LU12I_W; + bfd_put_32 (input_bfd, insn, contents + rel->r_offset); + } + break; + default: + RELOCATE_CALC_PC64_HI32 (relocation, pc); + } break; case R_LARCH_TLS_LE_HI20_R: @@ -4160,19 +4212,6 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, } break; - case R_LARCH_PCALA64_HI12: - pc -= 4; - /* Fall through. */ - case R_LARCH_PCALA64_LO20: - if (h && h->plt.offset != MINUS_ONE) - relocation = sec_addr (plt) + h->plt.offset; - else - relocation += rel->r_addend; - - RELOCATE_CALC_PC64_HI32 (relocation, pc - 8); - - break; - case R_LARCH_GOT_PC_HI20: case R_LARCH_GOT_HI20: /* Calc got offset. */ diff --git a/include/opcode/loongarch.h b/include/opcode/loongarch.h index 493fe9bcce3..35799696c55 100644 --- a/include/opcode/loongarch.h +++ b/include/opcode/loongarch.h @@ -36,6 +36,7 @@ extern "C" #define LARCH_MK_ADDI_D 0xffc00000 #define LARCH_OP_ADDI_D 0x02c00000 + #define LARCH_OP_ADDI_W 0x02800000 #define LARCH_MK_PCADDI 0xfe000000 #define LARCH_OP_PCADDI 0x18000000 #define LARCH_MK_B 0xfc000000 @@ -44,6 +45,7 @@ extern "C" #define LARCH_OP_BL 0x54000000 #define LARCH_MK_ORI 0xffc00000 #define LARCH_OP_ORI 0x03800000 + #define LARCH_OP_OR 0x00150000 #define LARCH_MK_LU12I_W 0xfe000000 #define LARCH_OP_LU12I_W 0x14000000 #define LARCH_MK_LD_D 0xffc00000 diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index d7c2b311f2f..c09815703d5 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -184,6 +184,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "bad_pcala_hi20_weak_pie" run_dump_test "bad_pcrel20_s2_global" run_dump_test "bad_pcrel20_s2_weak" + run_dump_test "weak-undef-hidden-shared" } if [check_pie_support] { @@ -194,6 +195,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "relr-got-start" run_dump_test "relr-text-pie" run_dump_test "abssym_pie" + run_dump_test "weak-undef-hidden-pie" } run_dump_test "max_imm_b16" diff --git a/ld/testsuite/ld-loongarch-elf/weak-undef-hidden-pie.d b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden-pie.d new file mode 100644 index 00000000000..913bc96f70b --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden-pie.d @@ -0,0 +1,14 @@ +#source: weak-undef-hidden.s +#ld: -pie -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-hidden-shared.d b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden-shared.d new file mode 100644 index 00000000000..fdb215e6dd4 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden-shared.d @@ -0,0 +1,14 @@ +#source: weak-undef-hidden.s +#ld: -shared -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-hidden.s b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden.s new file mode 100644 index 00000000000..ac67007da26 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/weak-undef-hidden.s @@ -0,0 +1,9 @@ +.weak undef +.hidden undef + +.globl _start +_start: + la.pcrel $a0, $a1, undef + 0xaaabbbbbcccccddd + pcaddi $a2, undef + 0x7ff + pcaddi $a3, undef - 0x800 + ret From patchwork Fri Dec 13 05:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 102950 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 2C25C3858D38 for ; Fri, 13 Dec 2024 05:58:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C25C3858D38 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=gZKgZhpO 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 A855C3858C35 for ; Fri, 13 Dec 2024 05:55:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A855C3858C35 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 A855C3858C35 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=1734069304; cv=none; b=l440Gs3SpTaVUN0oDyFTtNlUmAw/dFkWsyqWe50vnkB2OWQJeJkb/htVcdcWJ4Yo8BgztWG9a9L2i+w0PijKOkojKdagHmFSq0JmAV+HiWK/94nA62OuD38JJaeZt2biH/8f2x/BzxP3Zqe4xkVVWS0cp/vbTLvVl23HXwg0Ffs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734069304; c=relaxed/simple; bh=9YbcvVKvcotSr/Y9rCIP7zM1YmChEUt2WOZuLdxNU2M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ERTf4ciyk8Fd5D96RYy1n+Djpo7q6mpUDW3yPlO1PX9p60vnSX4dQB3rpCbUtoFFdJvJu7ll4LzqBobJpE/4HULeLj6ZH5hQhl+Ll8dVR7bB3YGYH4RYLc2cbuaTMv7bK2UpIx8rNN4ouMHRVCOqevwmQCAOTxvoTLn2I8mhm4k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A855C3858C35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xry111.site; s=default; t=1734069304; bh=KrvHvXHk43ZyDqDXH52Qv98GQl/jgKtRniLD1mTKntk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gZKgZhpO3Uy2EEsFXDjva/NPBQPg29FKLNohYnopdWluYSSU7X7lIN0bd50K9XtJX 5yP1XWlw0zjomDFTrbQwqbMh4L0kT2agGfrWJEXfNzyPUgBGeMnhX4xznrcXg0ULcg JyIHbe7Xpo7UvSEz7nxPXQipJ09vCcEzk2U1J7w0= Received: from stargazer.. (unknown [IPv6:240e:457:1120:870:571c:7044:9d4:458a]) (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 0617E676A3; Fri, 13 Dec 2024 00:54:57 -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 2/3] LoongArch: Allow R_LARCH_PCALA_HI20 or R_LARCH_PCREL20_S2 against undefined weak symbols for static PIE Date: Fri, 13 Dec 2024 13:53:51 +0800 Message-ID: <20241213055427.579306-3-xry111@xry111.site> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241213055427.579306-1-xry111@xry111.site> References: <20241213055427.579306-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-8.7 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 599df6e2db17d1c4 ("ld, LoongArch: print error about linking without -fPIC or -fPIE flag in more detail"), 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 b657ddc5666..5d252bcccb9 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1101,12 +1101,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; @@ -1129,12 +1133,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; @@ -4118,12 +4126,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 c09815703d5..e5fbe35c4fd 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 From patchwork Fri Dec 13 05:53:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 102952 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 0BFA63858429 for ; Fri, 13 Dec 2024 06:01:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BFA63858429 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=jfq3vDiz 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 4726B3858423 for ; Fri, 13 Dec 2024 05:55:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4726B3858423 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 4726B3858423 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=1734069311; cv=none; b=xXuqmXMO8ckmDQyMEbIddYBqLmLzo1dg/cKV4H00iB91BC9Kp+fWn2H8m2D9I6UCaDY1uzCBlqmSfqPZMsMqfCYHtypfM0WX4sLSdZ951jCRjbs8fSieoTXl8MFcuVhpP1fOY/4XkUFXDohJ7Wg9Q0CaCH7UQ6aVk/otJQxGA1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734069311; c=relaxed/simple; bh=935MtlpgZFyzvoRVtRC78BTGRfY1yjfh/55a65YYJ84=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jAVr2fZcfF82gnOxg54pK+CBJ0jtqwK+y9fzNL2niWJcR76xL6TbigAZY5FdAfN5Bx8IfxduTafpcNvILhn/mg5y/Oxs3J/BgGvdnt3GJ+ipG0s7hbJNaQNXuA1FeaZp7Aqpp9FMLzfa8pQxoUQGVhzYhxZpZs6H5GbV5ypoc94= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4726B3858423 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xry111.site; s=default; t=1734069310; bh=csFK0eSW32/HuY0L/9R6Sr4Gp01mVXERkP4fcwjCcn8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jfq3vDizgOFyT46SVUEMeJZFWreVQi89DsN8qAa6nwpL9n1EpGA1qZuiElSySsPGV 7rEjnlZ56Y4oRHi8XW5KIVEd/a8VMKXq8TNhOJ5aMWAKp5tYDseDxY9CKqVaHYL0o5 waMs3ifz53G3Fsfsf/vhbquMNoOsYFENZrJwLlp4= Received: from stargazer.. (unknown [IPv6:240e:457:1120:870:571c:7044:9d4:458a]) (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 63BD6676A1; Fri, 13 Dec 2024 00:55:04 -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 3/3] LoongArch: Reword message for unresolvable PC-relative relocs Date: Fri, 13 Dec 2024 13:53:52 +0800 Message-ID: <20241213055427.579306-4-xry111@xry111.site> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241213055427.579306-1-xry111@xry111.site> References: <20241213055427.579306-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-8.8 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 If we hit such a bad reloc, it means the programmer has somehow wrongly instructed the compiler to generate PC-relative relocs against external symbols. "Recompiling with -fPIC" (or -fPIE) is just misleading. Signed-off-by: Xi Ruoyao --- bfd/elfnn-loongarch.c | 4 ++++ ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d | 2 +- ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d | 2 +- ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d | 2 +- ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d | 2 +- ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d | 2 +- ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d | 2 +- 7 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index 5d252bcccb9..93368275b46 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -894,6 +894,10 @@ bad_static_reloc (struct bfd_link_info *info, pic = _("; recompile with -fPIE"); } + if (r_type == R_LARCH_PCREL20_S2 || r_type == R_LARCH_PCALA_HI20) + pic = _("; recompile with -mno-direct-extern-access and check the " + "symbol visibility"); + (*_bfd_error_handler) (_("%pB:(%pA+%#lx): relocation %s against `%s` can not be used when making " "%s%s"), diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d index 6ecefd1d48a..fac02d036ba 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global.d @@ -2,4 +2,4 @@ #source: bad_pcala_hi20_global.s #target: [check_shared_lib_support] #ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 -#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC +#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -mno-direct-extern-access and check the symbol visibility diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d index 5dc2f7c4e4b..a5cd359ac63 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d @@ -1,4 +1,4 @@ #name: PC-relative relocation making executable #source: bad_pcala_hi20_global_pie.s #ld: -pie -z undefs --defsym _start=0 -#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE +#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -mno-direct-extern-access and check the symbol visibility diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d index cefc42cf89f..fa6cff370be 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak.d @@ -2,4 +2,4 @@ #source: bad_pcala_hi20_weak.s #target: [check_shared_lib_support] #ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 -#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -fPIC +#error: .*: relocation R_LARCH_PCALA_HI20 against `global_b` can not be used when making a shared object; recompile with -mno-direct-extern-access and check the symbol visibility diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d index 5515f257191..62a3d8dae25 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d @@ -1,4 +1,4 @@ #name: PC-relative relocation making executable #source: bad_pcala_hi20_weak_pie.s #ld: -pie --defsym _start=0 -#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE +#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -mno-direct-extern-access and check the symbol visibility diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d index 8e063dfb0ec..b517f22c20c 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_global.d @@ -2,4 +2,4 @@ #source: bad_pcrel20_s2_global.s #target: [check_shared_lib_support] #ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 -#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC +#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -mno-direct-extern-access and check the symbol visibility diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d index 605df24988a..bf551ef6db2 100644 --- a/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d +++ b/ld/testsuite/ld-loongarch-elf/bad_pcrel20_s2_weak.d @@ -2,4 +2,4 @@ #source: bad_pcrel20_s2_weak.s #target: [check_shared_lib_support] #ld: -shared --defsym global_a=0x10 --defsym global_b=0x20 -#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -fPIC +#error: .*: relocation R_LARCH_PCREL20_S2 against `global_b` can not be used when making a shared object; recompile with -mno-direct-extern-access and check the symbol visibility