From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 3CE36384DEE3 for ; Wed, 26 Jun 2024 10:05:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3CE36384DEE3 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 3CE36384DEE3 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=1719396322; cv=none; b=c6prBtegG1rmfJNAkT8MShoOC7kMP1E08HHzt/o3kZldxZ0ALbl3sF6maDBgdIIoNF+/NPb5PYrsl5ToRUAYs1wXMET+Emh/NBGiv0TF1J+++7VNTqwg3UFS9d+IsjKq1rdLJOnZ7F0EpfCXDLPAzah+OTN/j+j3ZpcaX38ifnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719396322; c=relaxed/simple; bh=AC+e8pqGRa+WjwZsOeWmgpI8ed5DN4cakZQi0Dh/rm0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Qkcp4pIdV1hq8G82unQz9B4fIndaA7EJmAKTXxHTuD/Ogam7oRIUrldpGLlkXiljB6QvASYvyRQ9R1nuROYIfqdz2tFOstpP5E+OnQW1zXwk/BnlzjC38QljiRNdrZ0KsdoPLKaMOiFC40rWG7apDAjXvQs8M3Uf3NcWnn7KeLE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1719396312; bh=AC+e8pqGRa+WjwZsOeWmgpI8ed5DN4cakZQi0Dh/rm0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ivaXYhu85fD2M9kg1yJ+GN6DkIeGeZeAaBS6C+vvXLgkYwHRjHqhRu18W0qGuvFhV 3iov5dmG4ODU1Pl9grX07atf6D8nCz/41jh/T/zUxhM1AXDhQmpNPuTltwuc8qd/sz NF2yEWsQmDTYDIyrnwxiiijGKU7DpOCt8OvtKB1I= Received: from stargazer.. (unknown [113.200.174.20]) (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 462AE66D68; Wed, 26 Jun 2024 06:05:10 -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 v2 1/5] LoongArch: Reject R_LARCH_32 from becoming a runtime reloc in ELFCLASS64 Date: Wed, 26 Jun 2024 18:04:50 +0800 Message-ID: <20240626100453.98946-3-xry111@xry111.site> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240626100453.98946-2-xry111@xry111.site> References: <20240626100453.98946-2-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.9 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 List-Id: 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 | 23 ++++++++++++++++++- .../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, 30 insertions(+), 1 deletion(-) 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 b1720760475..3a55ac93e20 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -2861,7 +2861,28 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, /* No alloc space of func allocate_dynrelocs. */ if (unresolved_reloc && !(h && (h->is_weakalias || !h->dyn_relocs))) - loongarch_elf_append_rela (output_bfd, sreloc, &outrel); + { + if (is_pic && r_type != R_LARCH_NN) + { + /* Not to use ELFCLASSNN in string literal or it'll + puzzle gettext. */ + + /* xgettext:c-format */ + char *msg = bfd_asprintf ( + _("reloc is unresolved and cannot be turned to " + "a runtime reloc in ELFCLASS%d"), + NN); + + /* loongarch_reloc_is_fatal will output + "R_LARCH_32" or "R_LARCH_64" for us. */ + fatal = loongarch_reloc_is_fatal ( + info, input_bfd, input_section, rel, howto, + bfd_reloc_notsupported, is_undefweak, name, msg); + } + else + loongarch_elf_append_rela (output_bfd, sreloc, + &outrel); + } } relocation += rel->r_addend; 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..df61f3a36c5 --- /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 against `x':\nreloc is unresolved and cannot be turned to a runtime reloc 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 -- 2.45.2