From nobody Mon Dec 15 17:00:20 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dVRBs5b8Pz6L1gf for ; Mon, 15 Dec 2025 17:00:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dVRBs0QxHz3pRP for ; Mon, 15 Dec 2025 17:00:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765818021; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cvBVhbSnqZdXcs/I6oiV5tb4nZ0jOQCgkfZA7Noxf8U=; b=ozOhLENUVrq4oYmBD8dRwCB68sS1b+K1vxIMmoPZ4XW7UBG4ln9JiG6+9pbUsTbuZpvg4k J9yBCM23Up7LQ5yaUolobEecpRrTthaCwtx9I8mEYHkQjd1JLF9SMY2ql+h4FdTqA/BVgO bWRcdIWV2wMAA9PQlzgyUv5THV61YwUGC/kXLrFO0RkSD1xJJD6bcDGUw0u8QJhGqaoNj7 jvpRLTB3lKCq8Edp/k2d5u528KIacXptXWqAKmtbBLWlLlkE2Ya3l5uglPsL9G4o9d+LOw A1BOiE/yKneEbFdiuPflVPdZo6dEB1a03U3qyLQ1D7z+QcIz713tgSe/Ttx5fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765818021; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cvBVhbSnqZdXcs/I6oiV5tb4nZ0jOQCgkfZA7Noxf8U=; b=tYy2MhuWmWMNSUZD1Bn1SdoHLHJ3xFW2S9XemR95R2RulP/ZKDWaXZ8/60YbSl44GCnURq KuYiQJ8RLI4rle99/MTVEkk/9V2Dfb0wmsLN9uFOg+dePAwK4fw6Aeoy2PSrOvSVuY8y2C NJGFfTYhr1adohNDipueQjEtg3G2t7qRDw3YOMefBsISUOYjKNKVk+WHpiY+Q4YE3qYSdH QB9BgF7PlD5Wi/jBzE1MJmZLS+ZGDgTYG7J8KI1NPBO19nhCx40zGRaS8hADgGOdTxY2BA nXWGWR1Q/5gQNagOcV/gIsjfm7W2Kr1jw0pshSCXn+wRF2XmqkeTWqWNxVQYTA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765818021; a=rsa-sha256; cv=none; b=eaei92hr3bPnYzIgHHlsPbsQ2tbJH5Je5eB4D1r/M1PQEUSJVH2d9EfHfgmWsI21e3XNlQ X4B6w7DQQ894tm+GzdIuu3wEdTi/kCT/jK7DSDWf+jxhie9JTtpOgcmEvtS9FzacSklCVg ksV8jCTY6p6MVZ6PY6IU0SyLWoYhLt1/JuwqM0WwjYL1sNBHLeusLbXq79I1VPZ/qQMf4Q PmSvkCMHhSe61ktTer5XLBZFdh/b46Vw9wQFwextK2TeRccXZxu/rTd6N8sJV/vWsskzWr ZTiLz9YJcSSw8oAAf7DmIDHK9zszaMTWxyGYJyzXt2M8SZFyS+FWarsq/0eccg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dVRBr75ZWzCnv for ; Mon, 15 Dec 2025 17:00:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id e119 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 15 Dec 2025 17:00:20 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Jessica Clarke Subject: git: 2ef0ba80e0b5 - stable/14 - libc: Don't bias DTV entries by TLS_DTV_OFFSET List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 2ef0ba80e0b51653e62cfd5623e7f6b844399440 Auto-Submitted: auto-generated Date: Mon, 15 Dec 2025 17:00:20 +0000 Message-Id: <69403ea4.e119.5f3be21b@gitrepo.freebsd.org> The branch stable/14 has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=2ef0ba80e0b51653e62cfd5623e7f6b844399440 commit 2ef0ba80e0b51653e62cfd5623e7f6b844399440 Author: Jessica Clarke AuthorDate: 2025-05-06 22:14:50 +0000 Commit: Jessica Clarke CommitDate: 2025-12-15 16:58:44 +0000 libc: Don't bias DTV entries by TLS_DTV_OFFSET PowerPC and RISC-V have a non-zero TLS_DTV_OFFSET. The intent behind this in the design is that DTV entries are biased by this, as are (in the other direction) the DTPOFF/DTPREL entries in the GOT. However, this is pretty pointless in practice, and both FreeBSD and glibc's run-time linkers don't bother to bias DTV entries, instead just adding the bias back on at the end in __tls_get_addr. In libc we also have a minimal implementation of this for statically-linked binaries, which is only in practice used for code compiled with -fPIC (not -fPIE) that is also linked without TLS relaxation support. PowerPC supports linker relaxation for TLS sequences, so this likely never gets hit there, but RISC-V does not, and so easily does if you compile an executable with -fPIC. In this implementation we add TLS_DTV_OFFSET both to the DTV entries in __libc_allocate_tls and to the result of __tls_get_addr, meaning that any TLS accesses using the General Dynamic model in static binaries on RISC-V end up off by 0x800. Historically this also did not matter as __tls_get_addr was a stub that always returned NULL, so although 6e16d0bc4376 ("Rework alignment handling in __libc_allocate_tls() for Variant I of TLS layout.") added this DTV implementation, nothing actually read the entries. However, now it's a real implementation, and dl_iterate_phdr also now relies on it, it does matter. Fix this by not biasing the DTV entries, just like RTLD. We could instead stop adding TLS_DTV_OFFSET in __tls_get_addr, but being consistent between libc and RTLD seems better. (Note this also applies to MIPS on stable/13) Reviewed by: kib Fixes: ca46b5698e8a ("libc: implement __tls_get_addr() for static binaries") MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D50181 (cherry picked from commit d04c93a2adccb4c3a17f7391126a9246326e3fea) --- lib/libc/gen/tls.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/libc/gen/tls.c b/lib/libc/gen/tls.c index 30b4fc583c98..8d453eccb801 100644 --- a/lib/libc/gen/tls.c +++ b/lib/libc/gen/tls.c @@ -151,7 +151,8 @@ libc_free_aligned(void *ptr) * where TP points (with bias) to TLS and TCB immediately precedes TLS without * any alignment gap[4]. Only TLS should be aligned. The TCB[0] points to DTV * vector and DTV values are biased by constant value (TLS_DTV_OFFSET) from - * real addresses[5]. + * real addresses. However, like RTLD, we don't actually bias the DTV values, + * instead we compensate in __tls_get_addr for ti_offset's bias. * * [1] Ulrich Drepper: ELF Handling for Thread-Local Storage * www.akkadia.org/drepper/tls.pdf @@ -167,8 +168,6 @@ libc_free_aligned(void *ptr) * but we must follow this rule due to suboptimal _tcb_set() * (aka _SET_TP) implementation. This function doesn't expect TP but * TCB as argument. - * - * [5] I'm not able to validate "values are biased" assertions. */ /* @@ -272,7 +271,7 @@ __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign) /* Adjust the DTV. */ dtv = tcb[0]; - dtv[2] = (Elf_Addr)(tls + TLS_DTV_OFFSET); + dtv[2] = (Elf_Addr)tls; } else { dtv = __je_bootstrap_malloc(3 * sizeof(Elf_Addr)); if (dtv == NULL) { @@ -283,7 +282,7 @@ __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign) tcb[0] = dtv; dtv[0] = 1; /* Generation. */ dtv[1] = 1; /* Segments count. */ - dtv[2] = (Elf_Addr)(tls + TLS_DTV_OFFSET); + dtv[2] = (Elf_Addr)tls; if (libc_tls_init_size > 0) memcpy(tls, libc_tls_init, libc_tls_init_size);