From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1544385-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id DEAA915801B
	for <garchives@archives.gentoo.org>; Wed,  9 Aug 2023 03:11:19 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 2DD162BC01F;
	Wed,  9 Aug 2023 03:11:19 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 10E2F2BC01F
	for <gentoo-commits@lists.gentoo.org>; Wed,  9 Aug 2023 03:11:19 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 0DF0133BEFE
	for <gentoo-commits@lists.gentoo.org>; Wed,  9 Aug 2023 03:11:18 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 6D3D5F27
	for <gentoo-commits@lists.gentoo.org>; Wed,  9 Aug 2023 03:11:16 +0000 (UTC)
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" <sam@gentoo.org>
Message-ID: <1691550663.0b8e1cc4c541041f00338d897d34fabe66ae87b8.sam@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: sys-devel/mold/, sys-devel/mold/files/
X-VCS-Repository: repo/gentoo
X-VCS-Files: sys-devel/mold/files/mold-2.0.0-DT_RELR-dependency.patch sys-devel/mold/mold-2.0.0-r2.ebuild
X-VCS-Directories: sys-devel/mold/ sys-devel/mold/files/
X-VCS-Committer: sam
X-VCS-Committer-Name: Sam James
X-VCS-Revision: 0b8e1cc4c541041f00338d897d34fabe66ae87b8
X-VCS-Branch: master
Date: Wed,  9 Aug 2023 03:11:16 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 475aa85d-8e65-4ac7-846d-4c2e0df6de5d
X-Archives-Hash: 1ddb8e8145b23b77f1b6f894c10181ba

commit:     0b8e1cc4c541041f00338d897d34fabe66ae87b8
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Wed Aug  9 03:10:49 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Aug  9 03:11:03 2023 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=0b8e1cc4

sys-devel/mold: backport DT_RELR fix

Bug: https://bugs.gentoo.org/911591
Signed-off-by: Sam James <sam <AT> gentoo.org>

 .../mold/files/mold-2.0.0-DT_RELR-dependency.patch | 161 +++++++++++++++++++++
 sys-devel/mold/mold-2.0.0-r2.ebuild                |  94 ++++++++++++
 2 files changed, 255 insertions(+)

diff --git a/sys-devel/mold/files/mold-2.0.0-DT_RELR-dependency.patch b/sys-devel/mold/files/mold-2.0.0-DT_RELR-dependency.patch
new file mode 100644
index 000000000000..267afde230d2
--- /dev/null
+++ b/sys-devel/mold/files/mold-2.0.0-DT_RELR-dependency.patch
@@ -0,0 +1,161 @@
+https://bugs.gentoo.org/911591
+https://bugzilla.mozilla.org/show_bug.cgi?id=1847697
+https://github.com/rui314/mold/issues/653#event-10041847648
+https://github.com/rui314/mold/commit/f467ad1add2ab6e381e0e458f026df197e63d487
+
+From f467ad1add2ab6e381e0e458f026df197e63d487 Mon Sep 17 00:00:00 2001
+From: Rui Ueyama <ruiu@bluewhale.systems>
+Date: Wed, 9 Aug 2023 11:40:09 +0900
+Subject: [PATCH] Create a symbol version dependency to GLIBC_ABI_DT_RELR
+
+Fixes https://github.com/rui314/mold/issues/653
+---
+ elf/cmdline.cc                     |  2 ++
+ elf/mold.h                         |  1 +
+ elf/output-chunks.cc               | 44 +++++++++++++++++++++++++-----
+ test/elf/z-pack-relative-relocs.sh | 16 +++++++++++
+ 4 files changed, 56 insertions(+), 7 deletions(-)
+ create mode 100755 test/elf/z-pack-relative-relocs.sh
+
+diff --git a/elf/cmdline.cc b/elf/cmdline.cc
+index c568ce086..82a0e6869 100644
+--- a/elf/cmdline.cc
++++ b/elf/cmdline.cc
+@@ -875,8 +875,10 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
+       ctx.arg.z_nodefaultlib = true;
+     } else if (read_z_flag("pack-relative-relocs")) {
+       ctx.arg.pack_dyn_relocs_relr = true;
++      ctx.arg.z_pack_relative_relocs = true;
+     } else if (read_z_flag("nopack-relative-relocs")) {
+       ctx.arg.pack_dyn_relocs_relr = false;
++      ctx.arg.z_pack_relative_relocs = false;
+     } else if (read_z_flag("separate-loadable-segments")) {
+       z_separate_code = SEPARATE_LOADABLE_SEGMENTS;
+     } else if (read_z_flag("separate-code")) {
+diff --git a/elf/mold.h b/elf/mold.h
+index e5532211c..3a027f1e9 100644
+--- a/elf/mold.h
++++ b/elf/mold.h
+@@ -1831,6 +1831,7 @@ struct Context {
+     bool z_nodefaultlib = false;
+     bool z_now = false;
+     bool z_origin = false;
++    bool z_pack_relative_relocs = false;
+     bool z_relro = true;
+     bool z_sectionheader = true;
+     bool z_shstk = false;
+diff --git a/elf/output-chunks.cc b/elf/output-chunks.cc
+index 726a4da2b..3896a2991 100644
+--- a/elf/output-chunks.cc
++++ b/elf/output-chunks.cc
+@@ -2373,12 +2373,13 @@ void VerneedSection<E>::construct(Context<E> &ctx) {
+            std::tuple(((SharedFile<E> *)b->file)->soname, b->ver_idx);
+   });
+ 
+-  // Resize of .gnu.version
++  // Resize .gnu.version
+   ctx.versym->contents.resize(ctx.dynsym->symbols.size(), 1);
+   ctx.versym->contents[0] = 0;
+ 
+   // Allocate a large enough buffer for .gnu.version_r.
+-  contents.resize((sizeof(ElfVerneed<E>) + sizeof(ElfVernaux<E>)) * syms.size());
++  contents.resize((sizeof(ElfVerneed<E>) + sizeof(ElfVernaux<E>)) *
++                  (syms.size() + 1));
+ 
+   // Fill .gnu.version_r.
+   u8 *buf = (u8 *)&contents[0];
+@@ -2394,14 +2395,14 @@ void VerneedSection<E>::construct(Context<E> &ctx) {
+       verneed->vn_next = ptr - (u8 *)verneed;
+ 
+     verneed = (ElfVerneed<E> *)ptr;
+-    ptr += sizeof(*verneed);
++    ptr += sizeof(ElfVerneed<E>);
+     verneed->vn_version = 1;
+     verneed->vn_file = ctx.dynstr->find_string(((SharedFile<E> *)file)->soname);
+     verneed->vn_aux = sizeof(ElfVerneed<E>);
+     aux = nullptr;
+   };
+ 
+-  auto add_entry = [&](Symbol<E> *sym) {
++  auto add_entry = [&](std::string_view verstr) {
+     verneed->vn_cnt++;
+ 
+     if (aux)
+@@ -2409,23 +2410,52 @@ void VerneedSection<E>::construct(Context<E> &ctx) {
+     aux = (ElfVernaux<E> *)ptr;
+     ptr += sizeof(*aux);
+ 
+-    std::string_view verstr = sym->get_version();
+     aux->vna_hash = elf_hash(verstr);
+     aux->vna_other = ++veridx;
+     aux->vna_name = ctx.dynstr->add_string(verstr);
+   };
+ 
++  // Create version entries.
+   for (i64 i = 0; i < syms.size(); i++) {
+     if (i == 0 || syms[i - 1]->file != syms[i]->file) {
+       start_group(syms[i]->file);
+-      add_entry(syms[i]);
++      add_entry(syms[i]->get_version());
+     } else if (syms[i - 1]->ver_idx != syms[i]->ver_idx) {
+-      add_entry(syms[i]);
++      add_entry(syms[i]->get_version());
+     }
+ 
+     ctx.versym->contents[syms[i]->get_dynsym_idx(ctx)] = veridx;
+   }
+ 
++  if (ctx.arg.z_pack_relative_relocs) {
++    // If `-z pack-relative-relocs` is specified, we'll create a .relr.dyn
++    // section and store base relocation records to that section instead of
++    // to the usual .rela.dyn section.
++    //
++    // .relr.dyn is relatively new feature and not supported by glibc until
++    // 2.38 which was released in 2022. Executables built with `-z
++    // pack-relative-relocs` don't work and usually crash immediately on
++    // startup if libc doesn't support it.
++    //
++    // In the following code, we'll add a dependency to a dummy version name
++    // "GLIBC_ABI_DT_RELR" so that executables built with the option failed
++    // with a more friendly "version `GLIBC_ABI_DT_RELR' not found" error
++    // message. glibc 2.38 or later knows about this dummy version name and
++    // simply ignores it.
++    auto find_glibc2 = [&]() -> InputFile<E> * {
++      for (Symbol<E> *sym : syms)
++        if (((SharedFile<E> *)sym->file)->soname.starts_with("libc.so.") &&
++            sym->get_version().starts_with("GLIBC_2."))
++          return sym->file;
++      return nullptr;
++    };
++
++    if (InputFile<E> *file = find_glibc2()) {
++      start_group(file);
++      add_entry("GLIBC_ABI_DT_RELR");
++    }
++  }
++
+   // Resize .gnu.version_r to fit to its contents.
+   contents.resize(ptr - buf);
+ }
+diff --git a/test/elf/z-pack-relative-relocs.sh b/test/elf/z-pack-relative-relocs.sh
+new file mode 100755
+index 000000000..e09d441e7
+--- /dev/null
++++ b/test/elf/z-pack-relative-relocs.sh
+@@ -0,0 +1,16 @@
++#!/bin/bash
++. $(dirname $0)/common.inc
++
++cat <<EOF | $CC -o $t/a.o -fPIC -c -xc -
++#include <stdio.h>
++int main() {
++  printf("Hello world\n");
++}
++EOF
++
++$CC -B. -o $t/exe $t/a.o -pie -Wl,-z,pack-relative-relocs
++
++readelf -W -V $t/exe > $t/log
++grep -Fq GLIBC_2. $t/log || skip
++
++grep -q GLIBC_ABI_DT_RELR $t/log

diff --git a/sys-devel/mold/mold-2.0.0-r2.ebuild b/sys-devel/mold/mold-2.0.0-r2.ebuild
new file mode 100644
index 000000000000..72bb46594d66
--- /dev/null
+++ b/sys-devel/mold/mold-2.0.0-r2.ebuild
@@ -0,0 +1,94 @@
+# Copyright 2021-2023 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+
+inherit cmake toolchain-funcs
+
+DESCRIPTION="A Modern Linker"
+HOMEPAGE="https://github.com/rui314/mold"
+if [[ ${PV} == 9999 ]] ; then
+	EGIT_REPO_URI="https://github.com/rui314/mold.git"
+	inherit git-r3
+else
+	SRC_URI="https://github.com/rui314/mold/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"
+	KEYWORDS="~amd64 ~arm64 ~ppc64 ~riscv ~x86"
+fi
+
+# mold (MIT)
+#  - xxhash (BSD-2)
+LICENSE="MIT BSD-2"
+SLOT="0"
+
+RDEPEND="
+	app-arch/zstd:=
+	>=dev-cpp/tbb-2021.7.0-r1:=
+	sys-libs/zlib
+	!kernel_Darwin? (
+		>=dev-libs/mimalloc-2:=
+		dev-libs/openssl:=
+	)
+"
+DEPEND="${RDEPEND}"
+
+PATCHES=(
+	"${FILESDIR}"/${P}-reloc-test-fix.patch
+	"${FILESDIR}"/${P}-DT_RELR-dependency.patch
+)
+
+pkg_pretend() {
+	# Requires a c++20 compiler, see #831473
+	if [[ ${MERGE_TYPE} != binary ]]; then
+		if tc-is-gcc && [[ $(gcc-major-version) -lt 10 ]]; then
+			die "${PN} needs at least gcc 10"
+		elif tc-is-clang && [[ $(clang-major-version) -lt 12 ]]; then
+			die "${PN} needs at least clang 12"
+		fi
+	fi
+}
+
+src_prepare() {
+	cmake_src_prepare
+
+	# Needs unpackaged dwarfdump
+	rm test/elf/{{dead,compress}-debug-sections,compressed-debug-info}.sh || die
+
+	# Heavy tests, need qemu
+	rm test/elf/gdb-index-{compress-output,dwarf{2,3,4,5}}.sh || die
+	rm test/elf/lto-{archive,dso,gcc,llvm,version-script}.sh || die
+
+	# Sandbox sadness
+	rm test/elf/run.sh || die
+	sed -i 's|`pwd`/mold-wrapper.so|"& ${LD_PRELOAD}"|' \
+		test/elf/mold-wrapper{,2}.sh || die
+
+	# static-pie tests require glibc built with static-pie support
+	if ! has_version -d 'sys-libs/glibc[static-pie(+)]'; then
+		rm test/elf/{,ifunc-}static-pie.sh || die
+	fi
+}
+
+src_configure() {
+	local mycmakeargs=(
+		-DMOLD_ENABLE_QEMU_TESTS=OFF
+		-DMOLD_LTO=OFF # Should be up to the user to decide this with CXXFLAGS.
+		-DMOLD_USE_SYSTEM_MIMALLOC=ON
+		-DMOLD_USE_SYSTEM_TBB=ON
+	)
+	cmake_src_configure
+}
+
+src_install() {
+	dobin "${BUILD_DIR}"/${PN}
+
+	# https://bugs.gentoo.org/872773
+	insinto /usr/$(get_libdir)/mold
+	doins "${BUILD_DIR}"/${PN}-wrapper.so
+
+	dodoc docs/{design,execstack}.md
+	doman docs/${PN}.1
+
+	dosym ${PN} /usr/bin/ld.${PN}
+	dosym ${PN} /usr/bin/ld64.${PN}
+	dosym ../../../usr/bin/${PN} /usr/libexec/${PN}/ld
+}