From nobody Tue May 6 22:15:29 2025 X-Original-To: dev-commits-src-main@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 4ZsXlP4cVFz5vXc1; Tue, 06 May 2025 22:15:29 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZsXlP3lyyz3cxs; Tue, 06 May 2025 22:15:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746569729; 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=6CVDtcSBxQzZqyE+w0HiM7fvftdnkJ1bvxMZgXdb5OA=; b=a67u98iW+BRQyNcM0EPL9aNyk0x0sW1MFMlfe/VNB3o5iuDE1TbKjI+ABdz6tvyj+M8Yo/ GIqB5FT0nlkMd6zyhzYx2ew15Ohi71tnPI2YeBKotL7vqELvClMG/NJIODPnAsJW2gHBYL dRhHr4p62jWT2ZdqDTR526/LIg17goMwgQXhEXi5xCVQ1rGbwvn6w5nbBCvoD50cxO5ZQZ 9n8+FXb6BpTs/4Oga3DdeuWtfdUWMT1z876u39HRSTt8wUKZlkiS4O9fAlnE6uD3lNl1qL +NfZv8G+WYWlwiOEoIjx24wJ3YRu9JQ6VpUuZCRY6vRz+KEjqkDtRyonrdq5SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746569729; 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=6CVDtcSBxQzZqyE+w0HiM7fvftdnkJ1bvxMZgXdb5OA=; b=qaktiNRrZ1CQoYBQQvySb8oei3jKhdGfimnYZc99fQ5Fp5oo2m5oY1tJxycp32/ej8Th88 s0R/rkA9cR5HPLLHu91lsRG8dMyIvAvUz9UKbH4aswZ7XN0zgqQNKP/ksrygUpAy4F4MAn PxdhTWkDuRRHkkNws5c3H+I5SkZv7i+P4LEcs2e+TDZ4GZIZz96PXYbJlePbeEw9qjMfC+ XqScF3qmU2UluY/W3QH4bA+4YNEWti7I6sSmG9xTJg9pZ/l0++P4YK7LRLSjHrQXr/d57Z 4GWp/XcHmywByEYJVB1qeQOHydUJTB7v0a3P56FvbaCL2NsCGEx7gCL4VcBm8Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1746569729; a=rsa-sha256; cv=none; b=SRkY98+uNjWomTPlPEfoR6sWqflxvj5Wmadn+tVv4pYnPiPCHb9wD8+/fLmLSTXRfuoovi ZDXS7kKKsGoa2v2biG8DeVK+TI0k7T4NFWvormFng/tAIf3s11yAj8UQK3atvQv8P8gF4Q K5+E+icGvnf0kKbaYcPDcb8ROVUZ6BPKxxtZfL7Q+nIQ5pO1luf+xQH0duIA6i/iWu1w0V a9aEhw+hquam3aDVSK2tQJyAK490Um8YtKpz5rFmhxbLLw+nDruFkcshRgWFFuDJbIIVqs oScQgYK89cQfSSjid3p8FH/8F325Mjj2SzDuNv+/9eZmIgA8NfUdrpURl4vhUw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZsXlP3KJdz1GvL; Tue, 06 May 2025 22:15:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 546MFTZp062898; Tue, 6 May 2025 22:15:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 546MFTrJ062895; Tue, 6 May 2025 22:15:29 GMT (envelope-from git) Date: Tue, 6 May 2025 22:15:29 GMT Message-Id: <202505062215.546MFTrJ062895@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: d04c93a2adcc - main - libc: Don't bias DTV entries by TLS_DTV_OFFSET List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: d04c93a2adccb4c3a17f7391126a9246326e3fea Auto-Submitted: auto-generated The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=d04c93a2adccb4c3a17f7391126a9246326e3fea commit d04c93a2adccb4c3a17f7391126a9246326e3fea Author: Jessica Clarke AuthorDate: 2025-05-06 22:14:50 +0000 Commit: Jessica Clarke CommitDate: 2025-05-06 22:14:50 +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 --- 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);