| Message ID | 20240630071825.11172-1-xry111@xry111.site |
|---|---|
| Headers |
Return-Path: <binutils-bounces+patchwork=sourceware.org@sourceware.org> 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 D837E3836E85 for <patchwork@sourceware.org>; 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 BF9133858C33 for <binutils@sourceware.org>; Sun, 30 Jun 2024 07:18:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BF9133858C33 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 BF9133858C33 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=ebEYhs2+Pzpfwj33vtK+0Mm+nYMvbl540I7Xvr1qpf4umhYgcH4v2tjqfQbl4LXIetE70fJZgOn0kXxnBQiTaFiLQNa9w+OZqJQMoHswDK/XkBQD24peug+whO1qHsqOu9rWQk59VTA9v8WSlkpNAW1wStPrf8tUyGQF8C2RuAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719731929; c=relaxed/simple; bh=58vLl+jd7Wj7mGSYn/Zce4lQlwlZSj+8ywfMXfPz/fs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uEu5hDlIIN26hsxZp3ar6nXmBSOhm8/5tlhN3mSG5dpPWaVOYFNgAJhKTjYL8rFKNRzXAnrmGY7Kzg4VqWq8VAaLr3qmv1JySf8uHMc4k8Iy3UMG+rF3qIY8xD9icpJn1cr8T27v8FLWBTfAxe0DiiCGBfoVN74rBxC+x/SpABg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1719731916; bh=58vLl+jd7Wj7mGSYn/Zce4lQlwlZSj+8ywfMXfPz/fs=; h=From:To:Cc:Subject:Date:From; b=NcO918wth50/4+g9Mpy8ftfobH+xkdBG3FaHdnx/ebuNzyL3RJS8EN6x9Vxb/eurf dNNcZzjU+riOZT74P0Na9wWgVmLljIT9PCCKS4RCQFaFa9RwYSjoQDvXuF92nKjcoc FXnLbsJgku/bfR4dyj+hrm/FX3pfgkqKpuAcEbEw= 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 CB0C11A3EDE; Sun, 30 Jun 2024 03:18:34 -0400 (EDT) From: Xi Ruoyao <xry111@xry111.site> To: binutils@sourceware.org Cc: mengqinggang@loongson.cn, Zhensong Liu <liuzhensong@loongson.cn>, i.swmail@xen0n.name, maskray@google.com, Szabolcs Nagy <szabolcs.nagy@arm.com>, Xi Ruoyao <xry111@xry111.site> Subject: [PATCH v3 0/5] LoongArch: Add DT_RELR (packing relative relocs) support Date: Sun, 30 Jun 2024 15:18:20 +0800 Message-ID: <20240630071825.11172-1-xry111@xry111.site> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no 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 <binutils.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/binutils>, <mailto:binutils-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/binutils/> List-Post: <mailto:binutils@sourceware.org> List-Help: <mailto:binutils-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/binutils>, <mailto:binutils-request@sourceware.org?subject=subscribe> Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org |
| Series |
LoongArch: Add DT_RELR (packing relative relocs) support
|
|
Message
Xi Ruoyao
June 30, 2024, 7:18 a.m. UTC
This series adds DT_RELR support for LoongArch.
The leading 3 patches pave the way for DT_RELR:
The first patch rejects turning R_LARCH_32 as a runtime reloc in
ELFCLASS64, specifically R_LARCH_32 won't be turned to an
R_LARCH_RELATIVE in ELFCLASS64 silently. This avoids an OOB write at
runtime, and also simplifies the logic of DT_RELR implementation.
The second patch fixes a long-standing bug causing some relocs missing
(overwritten by another reloc, or written out of the .rela.dyn section)
when multiple STT_GNU_IFUNC symbol is defined locally with different
visibilities and we are linking a shared object. Without DT_RELR in
most cases an R_LARCH_RELATIVE against __dso_handle is overwritten by
an R_LARCH_JUMP_SLOT and the issue is mostly latent. But with DT_RELR
the R_LARCH_RELATIVE is packed, and other relocs start to be
overwritten, causing Glibc test failures. Thus the bug must be fixed
before implementing DT_RELR.
The third patch makes STV_PROTECTED function (STT_FUNC and
STT_GNU_IFUNC) symbols referenced locally when linking a shared library.
The other ports don't do this only because they rely on copy relocation
and -mdirect-extern-access to remove unnecessary GOT access, and with
-mdirect-extern-access locally referencing STV_PROTECTED functions can
break pointer equality. This is not a problem for us because we don't
and won't support copy relocation, and a programmer shouldn't use
-mdirect-extern-access unless they can make sure no dynamic link is
performed. So we can just make STV_PROTECTED functions referenced
locally. This makes the code slightly faster, and also simplifies the
logic of DT_RELR implementation.
The fourth patch implements DT_RELR. The algorithm is based on the
AArch64 implementation by Szabolcs and simplified because we don't have
as many features as AArch64 (for example we don't have -z
dynamic-undefined-weak). The linker relaxation pass is adapted to fix
up the relative relocations to be packed when deleting bytes from the
section where the relative relocations are against.
The fifth patch adds tests for DT_RELR.
Besides these tests, the DT_RELR implementation is also tested by:
- Running ld bootstrap test with -z pack-relative-relocs. The test
has passed. The patch adding this test is
https://sourceware.org/pipermail/binutils/2024-June/135021.html but
it does not work with old Glibc, thus it cannot be upstreamed until we
figure out how to skip it for old Glibc.
- Building Glibc (which enables -z pack-relative-relocs by default if
available) with the patched linker and run its test suite. All
relative relocs in libc.so are packed and the test result is all clean.
- Building patched Linux kernel with DT_RELR enabled. All relative
relocs in vmlinux is packed, the kernel booted fine and it has been
running fine for 100+ hours.
Changes from v2:
Incremental changes can be reviewed at
https://github.com/xry111/binutils-gdb/commits/xry111/loongarch-relr-2024-v2.1/.
- Fix the comment of the relr field in struct
loongarch_elf_section_data.
- Reuse loongarch_elf_section_data macro for casting to
(struct loongarch_elf_section_data *).
- Allow R_LARCH_64 -> R_LARCH_RELATIVE in ELFCLASS32 for now since
there's no real reason it won't work. Using R_LARCH_64 in ELFCLASS32
may be stupid though and we can reconsider when we finalize ilp32
ABIs.
- Move R_LARCH_32 vs ELFCLASS64 diagnostic to check_relocs. A guard
against it is still in relocate_section to avoid an assertion failure.
(Note that even if check_relocs return false relocate_section is still
executed.)
- Fix relr-text-pie test to really link a PIE.
- Fix typos in comments describing allocating ifunc PLT step by step.
- Fix wrong indents in loongarch_elf_late_size_sections.
Changes from v1:
- No real code change.
- Rebase to current master branch (resolving a conflict with AArch64 ILP32
DT_RELR commit)
- "preemptable" -> "preemptible", "unpreemptable" -> "non-preemptible"
for all patches (commit messages & comments)
- Clarify "OOB write", fix a typo, and remove "for DLL [should be DSO] or
PIE" in the commit message of patch 1: it's not important if the output
is DSO/PIE or not; even for DSO/PIE the R_LARCH_32 relocs *resolved at
link time* are perfectly fine and they are massively used in debug
info. So the important thing is we cannot leave it unresolved for
Glibc.
- Reword comment for LARCH_REF_LOCAL in patch 3 to (hopefully) avoid some
confusion.
- Move the explanation of the test suite from this cover letter to patch
5 commit message.
Xi Ruoyao (5):
LoongArch: Reject R_LARCH_32 from becoming a runtime reloc in
ELFCLASS64
LoongArch: Fix bad reloc with mixed visibility ifunc symbols in shared
libraries
LoongArch: Make protected function symbols local for -shared
LoongArch: Add DT_RELR support
LoongArch: Add DT_RELR tests
bfd/elfnn-loongarch.c | 665 +++++++++++++++++-
binutils/testsuite/lib/binutils-common.exp | 1 +
ld/emulparams/elf64loongarch.sh | 1 +
ld/testsuite/ld-loongarch-elf/ifunc-reloc.d | 19 +
ld/testsuite/ld-loongarch-elf/ifunc-reloc.s | 55 ++
.../ld-loongarch-elf/ld-loongarch-elf.exp | 13 +
.../ld-loongarch-elf/protected-func.d | 6 +
.../ld-loongarch-elf/protected-func.s | 17 +
.../ld-loongarch-elf/r_larch_32_elf64.d | 4 +
.../ld-loongarch-elf/r_larch_32_elf64.s | 3 +
ld/testsuite/ld-loongarch-elf/relr-addend.d | 11 +
ld/testsuite/ld-loongarch-elf/relr-addend.s | 17 +
ld/testsuite/ld-loongarch-elf/relr-align.d | 22 +
ld/testsuite/ld-loongarch-elf/relr-align.s | 106 +++
ld/testsuite/ld-loongarch-elf/relr-data-pie.d | 18 +
.../ld-loongarch-elf/relr-data-shared.d | 18 +
ld/testsuite/ld-loongarch-elf/relr-data.s | 71 ++
.../ld-loongarch-elf/relr-discard-pie.d | 8 +
.../ld-loongarch-elf/relr-discard-shared.d | 11 +
ld/testsuite/ld-loongarch-elf/relr-discard.ld | 13 +
ld/testsuite/ld-loongarch-elf/relr-discard.s | 61 ++
ld/testsuite/ld-loongarch-elf/relr-got-pie.d | 15 +
.../ld-loongarch-elf/relr-got-shared.d | 15 +
ld/testsuite/ld-loongarch-elf/relr-got.s | 27 +
ld/testsuite/ld-loongarch-elf/relr-relocs.ld | 24 +
ld/testsuite/ld-loongarch-elf/relr-text-pie.d | 14 +
.../ld-loongarch-elf/relr-text-shared.d | 14 +
ld/testsuite/ld-loongarch-elf/relr-text.s | 10 +
28 files changed, 1224 insertions(+), 35 deletions(-)
create mode 100644 ld/testsuite/ld-loongarch-elf/ifunc-reloc.d
create mode 100644 ld/testsuite/ld-loongarch-elf/ifunc-reloc.s
create mode 100644 ld/testsuite/ld-loongarch-elf/protected-func.d
create mode 100644 ld/testsuite/ld-loongarch-elf/protected-func.s
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
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-addend.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-addend.s
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-align.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-align.s
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data-pie.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data-shared.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data.s
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard-pie.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard-shared.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard.ld
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard.s
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got-pie.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got-shared.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got.s
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-relocs.ld
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text-pie.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text-shared.d
create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text.s
Comments
This series patches have been applied, thank you! 在 2024/6/30 下午3:18, Xi Ruoyao 写道: > This series adds DT_RELR support for LoongArch. > > The leading 3 patches pave the way for DT_RELR: > > The first patch rejects turning R_LARCH_32 as a runtime reloc in > ELFCLASS64, specifically R_LARCH_32 won't be turned to an > R_LARCH_RELATIVE in ELFCLASS64 silently. This avoids an OOB write at > runtime, and also simplifies the logic of DT_RELR implementation. > > The second patch fixes a long-standing bug causing some relocs missing > (overwritten by another reloc, or written out of the .rela.dyn section) > when multiple STT_GNU_IFUNC symbol is defined locally with different > visibilities and we are linking a shared object. Without DT_RELR in > most cases an R_LARCH_RELATIVE against __dso_handle is overwritten by > an R_LARCH_JUMP_SLOT and the issue is mostly latent. But with DT_RELR > the R_LARCH_RELATIVE is packed, and other relocs start to be > overwritten, causing Glibc test failures. Thus the bug must be fixed > before implementing DT_RELR. > > The third patch makes STV_PROTECTED function (STT_FUNC and > STT_GNU_IFUNC) symbols referenced locally when linking a shared library. > The other ports don't do this only because they rely on copy relocation > and -mdirect-extern-access to remove unnecessary GOT access, and with > -mdirect-extern-access locally referencing STV_PROTECTED functions can > break pointer equality. This is not a problem for us because we don't > and won't support copy relocation, and a programmer shouldn't use > -mdirect-extern-access unless they can make sure no dynamic link is > performed. So we can just make STV_PROTECTED functions referenced > locally. This makes the code slightly faster, and also simplifies the > logic of DT_RELR implementation. > > The fourth patch implements DT_RELR. The algorithm is based on the > AArch64 implementation by Szabolcs and simplified because we don't have > as many features as AArch64 (for example we don't have -z > dynamic-undefined-weak). The linker relaxation pass is adapted to fix > up the relative relocations to be packed when deleting bytes from the > section where the relative relocations are against. > > The fifth patch adds tests for DT_RELR. > > Besides these tests, the DT_RELR implementation is also tested by: > > - Running ld bootstrap test with -z pack-relative-relocs. The test > has passed. The patch adding this test is > https://sourceware.org/pipermail/binutils/2024-June/135021.html but > it does not work with old Glibc, thus it cannot be upstreamed until we > figure out how to skip it for old Glibc. > - Building Glibc (which enables -z pack-relative-relocs by default if > available) with the patched linker and run its test suite. All > relative relocs in libc.so are packed and the test result is all clean. > - Building patched Linux kernel with DT_RELR enabled. All relative > relocs in vmlinux is packed, the kernel booted fine and it has been > running fine for 100+ hours. > > Changes from v2: > > Incremental changes can be reviewed at > https://github.com/xry111/binutils-gdb/commits/xry111/loongarch-relr-2024-v2.1/. > > - Fix the comment of the relr field in struct > loongarch_elf_section_data. > - Reuse loongarch_elf_section_data macro for casting to > (struct loongarch_elf_section_data *). > - Allow R_LARCH_64 -> R_LARCH_RELATIVE in ELFCLASS32 for now since > there's no real reason it won't work. Using R_LARCH_64 in ELFCLASS32 > may be stupid though and we can reconsider when we finalize ilp32 > ABIs. > - Move R_LARCH_32 vs ELFCLASS64 diagnostic to check_relocs. A guard > against it is still in relocate_section to avoid an assertion failure. > (Note that even if check_relocs return false relocate_section is still > executed.) > - Fix relr-text-pie test to really link a PIE. > - Fix typos in comments describing allocating ifunc PLT step by step. > - Fix wrong indents in loongarch_elf_late_size_sections. > > Changes from v1: > > - No real code change. > - Rebase to current master branch (resolving a conflict with AArch64 ILP32 > DT_RELR commit) > - "preemptable" -> "preemptible", "unpreemptable" -> "non-preemptible" > for all patches (commit messages & comments) > - Clarify "OOB write", fix a typo, and remove "for DLL [should be DSO] or > PIE" in the commit message of patch 1: it's not important if the output > is DSO/PIE or not; even for DSO/PIE the R_LARCH_32 relocs *resolved at > link time* are perfectly fine and they are massively used in debug > info. So the important thing is we cannot leave it unresolved for > Glibc. > - Reword comment for LARCH_REF_LOCAL in patch 3 to (hopefully) avoid some > confusion. > - Move the explanation of the test suite from this cover letter to patch > 5 commit message. > > Xi Ruoyao (5): > LoongArch: Reject R_LARCH_32 from becoming a runtime reloc in > ELFCLASS64 > LoongArch: Fix bad reloc with mixed visibility ifunc symbols in shared > libraries > LoongArch: Make protected function symbols local for -shared > LoongArch: Add DT_RELR support > LoongArch: Add DT_RELR tests > > bfd/elfnn-loongarch.c | 665 +++++++++++++++++- > binutils/testsuite/lib/binutils-common.exp | 1 + > ld/emulparams/elf64loongarch.sh | 1 + > ld/testsuite/ld-loongarch-elf/ifunc-reloc.d | 19 + > ld/testsuite/ld-loongarch-elf/ifunc-reloc.s | 55 ++ > .../ld-loongarch-elf/ld-loongarch-elf.exp | 13 + > .../ld-loongarch-elf/protected-func.d | 6 + > .../ld-loongarch-elf/protected-func.s | 17 + > .../ld-loongarch-elf/r_larch_32_elf64.d | 4 + > .../ld-loongarch-elf/r_larch_32_elf64.s | 3 + > ld/testsuite/ld-loongarch-elf/relr-addend.d | 11 + > ld/testsuite/ld-loongarch-elf/relr-addend.s | 17 + > ld/testsuite/ld-loongarch-elf/relr-align.d | 22 + > ld/testsuite/ld-loongarch-elf/relr-align.s | 106 +++ > ld/testsuite/ld-loongarch-elf/relr-data-pie.d | 18 + > .../ld-loongarch-elf/relr-data-shared.d | 18 + > ld/testsuite/ld-loongarch-elf/relr-data.s | 71 ++ > .../ld-loongarch-elf/relr-discard-pie.d | 8 + > .../ld-loongarch-elf/relr-discard-shared.d | 11 + > ld/testsuite/ld-loongarch-elf/relr-discard.ld | 13 + > ld/testsuite/ld-loongarch-elf/relr-discard.s | 61 ++ > ld/testsuite/ld-loongarch-elf/relr-got-pie.d | 15 + > .../ld-loongarch-elf/relr-got-shared.d | 15 + > ld/testsuite/ld-loongarch-elf/relr-got.s | 27 + > ld/testsuite/ld-loongarch-elf/relr-relocs.ld | 24 + > ld/testsuite/ld-loongarch-elf/relr-text-pie.d | 14 + > .../ld-loongarch-elf/relr-text-shared.d | 14 + > ld/testsuite/ld-loongarch-elf/relr-text.s | 10 + > 28 files changed, 1224 insertions(+), 35 deletions(-) > create mode 100644 ld/testsuite/ld-loongarch-elf/ifunc-reloc.d > create mode 100644 ld/testsuite/ld-loongarch-elf/ifunc-reloc.s > create mode 100644 ld/testsuite/ld-loongarch-elf/protected-func.d > create mode 100644 ld/testsuite/ld-loongarch-elf/protected-func.s > 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 > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-addend.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-addend.s > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-align.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-align.s > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data-pie.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data-shared.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-data.s > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard-pie.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard-shared.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard.ld > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-discard.s > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got-pie.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got-shared.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-got.s > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-relocs.ld > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text-pie.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text-shared.d > create mode 100644 ld/testsuite/ld-loongarch-elf/relr-text.s >