From nobody Mon Dec 15 18:18:48 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 4dVSxP1BBRz6L7Wj for ; Mon, 15 Dec 2025 18:18:49 +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 4dVSxN2nsXz4LGk for ; Mon, 15 Dec 2025 18:18:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765822728; 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=Iber83cBzgO/pmnrPLv8M4mfi81w+mlL+F/EhUoKz24=; b=Iqw60RX8civV1p3h6vFlW9MFqeaey2uQbJMf4VLRTV2B++ECVV8vWIcsCgBoXDKWLOBBMi FVoxHC1O0I9nXlzmAc3y2epEyVRYU8qAyl5pKRReJSdX7Ep0yNmEoxvTk/96V7YlF/kVYj 1/SufEUUewlRAxx0tdSxoio8ju0PJUnvikoD4QmEZpbTM4ZNJfNZ+EH5IpTWxPfRv4dJhu 8kop+jW2zvnrzFSuwEcdCj2NhLz3kcFKd0YLv3pA8+T9ARUrqpkBfV5frjWSYQk59zb7lq JJPr0Fno/GQP+HQkk4e/pOlzoAoXbYwWbAx20nD0VfFZQ7d+Xo88qIwbjSVsXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765822728; 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=Iber83cBzgO/pmnrPLv8M4mfi81w+mlL+F/EhUoKz24=; b=A5kNUIKymR1AieEbCvxbYZ08s87Dy1yXPmBayaO1sDuOsMaSV2WxMhrLnIAG4vVZCA5nsM j6TkE8yIEzcBPnv9ZBQCb9p4GNC5LPFpkv1i5/jrrSeWPrVdvqFPa/4WAgQk8WSUX82bWb tId3Xa7iYpbhn2t6nGg1X/xWa+WufCPBYB5Txf7jM+joLzc2EosxuR7V2T0kaUqxzVW4H7 CGVl6owLaBmGlu87G1+K33VjUsq/rL/8B8xQ5CCZB7gVz3gheXkUrsp4OZXjNRHK4Rd473 tq+Pr1jOIyTDZGEtj2DPbDJfjO6YNdtBbBUJJuhLw7cICrUrevnhKgu+MWhHPg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765822728; a=rsa-sha256; cv=none; b=DxmzK6I3VJR4M7xKbMKegWf8UgRAl3YQ0lV6jcRuCphSZdgsh5/f7ePVcn7RThwq5vVNfn 26eXNVcVZSIA9xSjX1Y3HELQMrMWe6JHV4qmbVYEKn/nQTK0sw4x/xSCDbmzYR5WzOJ/wB o4RlPeriugSykZdIW/MqqFMNO+LAolSQBpReXF1yi7DI3W6Bzlftuy25YDAcqm/V/3nYk6 Bcb1ZOlzol8HYY+ly8w6sK3yyE8rupIxyNmw0sBRGGV2bXtpxPQG0dnuNBwep2sU8Asjsh 1hfw3DxIbb3WdHuCET3ly2nhO0HrQbPjyADjAPauZTjZ5ivE7DEhQfMcXiIYKA== 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 4dVSxN2PX8zWqd for ; Mon, 15 Dec 2025 18:18:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 237e1 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 15 Dec 2025 18:18:48 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Jessica Clarke Subject: git: 915a65a92a9f - stable/13 - 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/13 X-Git-Reftype: branch X-Git-Commit: 915a65a92a9f01f31d0af8b492eba0b781ba0999 Auto-Submitted: auto-generated Date: Mon, 15 Dec 2025 18:18:48 +0000 Message-Id: <69405108.237e1.411a5f2c@gitrepo.freebsd.org> The branch stable/13 has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=915a65a92a9f01f31d0af8b492eba0b781ba0999 commit 915a65a92a9f01f31d0af8b492eba0b781ba0999 Author: Jessica Clarke AuthorDate: 2025-05-06 22:14:50 +0000 Commit: Jessica Clarke CommitDate: 2025-12-15 17:56:36 +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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/libc/gen/tls.c b/lib/libc/gen/tls.c index 0db0f42484e9..a6a1d6a2fed2 100644 --- a/lib/libc/gen/tls.c +++ b/lib/libc/gen/tls.c @@ -150,7 +150,9 @@ libc_free_aligned(void *ptr) * described in [3] 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]. + * value (TLS_DTV_OFFSET) from 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 @@ -166,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. */ /* @@ -271,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) { @@ -282,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);