From patchwork Sun Jun 30 07:18:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 93084 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 8CBD53836E88 for ; Sun, 30 Jun 2024 07:19:28 +0000 (GMT) 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 068793858429 for ; Sun, 30 Jun 2024 07:18:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 068793858429 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 068793858429 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=1719731929; cv=none; b=iflwi7wGD/XenvDKAoZjqYP90EA8TniyMs2QjbgLxXibMrYqBEjWNolFACKaqFy9VbKV/IM+QwUUojGj/MwJjk4N9ODRGMdomyUpNrlvK52C3lj3at2JXtTYZtaNf43K9BSBOEgC2SY1YJcJFF/ZwbbCl2/xIrL+h/oCwF29JrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719731929; c=relaxed/simple; bh=kAp8BWcNuXLTpkambD90Dxmz1O9qWRLmoHclRlKERdE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BICREQxeS1RnmQxhP38S6WrvP+NFa3Vyuz8g2Mlau8ZjVmtUKgZExKiXkRRaVXbhhwaAOraMxIlPDWx8itUGg94xh7pgbJKEzlzK6Cm7Kewj4AOnyj9219ppRMQgdnoCGs07Kyiq/nYYRjETQXBOrDNBZceMpgL0XSg+DnEP2Qc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1719731918; bh=kAp8BWcNuXLTpkambD90Dxmz1O9qWRLmoHclRlKERdE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V6RzC26hiHt7JqtDjtZ3gwAtryR0Y8C/RUb6UXLvXIEiDH1M1kHFAwnPhyKbZU6/2 pOwfDgovJqnvMNs5+DlzgNyhK98lGWt4ZvXN/CASB4vuTtvIq5vZ11UdSlzBhaX4Tl H4IOESM9hyIXuQZ2JVEFk9i1ZuBCk6n+ggXtCSfo= Received: from stargazer.. (unknown [113.140.11.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 11D7D1A3EDF; Sun, 30 Jun 2024 03:18:36 -0400 (EDT) From: Xi Ruoyao To: binutils@sourceware.org Cc: mengqinggang@loongson.cn, Zhensong Liu , i.swmail@xen0n.name, maskray@google.com, Szabolcs Nagy , Xi Ruoyao Subject: [PATCH v3 1/5] LoongArch: Reject R_LARCH_32 from becoming a runtime reloc in ELFCLASS64 Date: Sun, 30 Jun 2024 15:18:21 +0800 Message-ID: <20240630071825.11172-2-xry111@xry111.site> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240630071825.11172-1-xry111@xry111.site> References: <20240630071825.11172-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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 We were converting R_LARCH_32 to R_LARCH_RELATIVE for ELFCLASS64: $ cat t.s .data x: .4byte x .4byte 0xdeadbeef $ as/as-new t.s -o t.o $ ld/ld-new -shared t.o $ objdump -R a.out: file format elf64-loongarch DYNAMIC RELOCATION RECORDS OFFSET TYPE VALUE 00000000000001a8 R_LARCH_RELATIVE *ABS*+0x00000000000001a8 But this is just wrong: at runtime the dynamic linker will run *(uintptr *)&x += load_address, clobbering the next 4 bytes of data ("0xdeadbeef" in the example). If we keep the R_LARCH_32 reloc as-is in ELFCLASS64, it'll be rejected by the Glibc dynamic linker anyway. And it does not make too much sense to modify Glibc to support it. So we can just reject it like x86_64: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC or RISC-V: relocation R_RISCV_32 against non-absolute symbol `a local symbol' can not be used in RV64 when making a shared object Signed-off-by: Xi Ruoyao --- bfd/elfnn-loongarch.c | 30 +++++++++++++++++-- .../ld-loongarch-elf/ld-loongarch-elf.exp | 1 + .../ld-loongarch-elf/r_larch_32_elf64.d | 4 +++ .../ld-loongarch-elf/r_larch_32_elf64.s | 3 ++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.d create mode 100644 ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.s diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index da84aabc1ae..41463ea5c75 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -1036,8 +1036,32 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, only_need_pcrel = 1; break; - case R_LARCH_JUMP_SLOT: case R_LARCH_32: + if (ARCH_SIZE > 32 + && bfd_link_pic (info) + && (sec->flags & SEC_ALLOC) != 0) + { + bool is_abs_symbol = false; + + if (r_symndx < symtab_hdr->sh_info) + is_abs_symbol = isym->st_shndx == SHN_ABS; + else + is_abs_symbol = bfd_is_abs_symbol (&h->root); + + if (!is_abs_symbol) + { + _bfd_error_handler + (_("%pB: relocation R_LARCH_32 against non-absolute " + "symbol `%s' cannot be used in ELFCLASS64 when " + "making a shared object or PIE"), + abfd, h ? h->root.root.string : "a local symbol"); + bfd_set_error (bfd_error_bad_value); + return false; + } + } + + /* Fall through. */ + case R_LARCH_JUMP_SLOT: case R_LARCH_64: need_dynreloc = 1; @@ -2859,8 +2883,10 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, outrel.r_addend = relocation + rel->r_addend; } - /* No alloc space of func allocate_dynrelocs. */ + /* No alloc space of func allocate_dynrelocs. + No alloc space of invalid R_LARCH_32 in ELFCLASS64. */ if (unresolved_reloc + && (ARCH_SIZE == 32 || r_type != R_LARCH_32) && !(h && (h->is_weakalias || !h->dyn_relocs))) loongarch_elf_append_rela (output_bfd, sreloc, &outrel); } diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp index 2d67c4f2668..4d9f4aebaff 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -132,6 +132,7 @@ if [istarget "loongarch64-*-*"] { run_dump_test "reloc_le_with_shared" run_dump_test "reloc_ler_with_shared" run_dump_test "reloc_abs_with_shared" + run_dump_test "r_larch_32_elf64" } if [check_pie_support] { diff --git a/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.d b/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.d new file mode 100644 index 00000000000..3431329569c --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.d @@ -0,0 +1,4 @@ +#name: R_LARCH_32 in ELFCLASS64 +#source: r_larch_32_elf64.s +#ld: -shared -melf64loongarch +#error: R_LARCH_32 .* cannot be used in ELFCLASS64 diff --git a/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.s b/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.s new file mode 100644 index 00000000000..6649f2bce01 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/r_larch_32_elf64.s @@ -0,0 +1,3 @@ +.data +x: + .4byte x