Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 May 2025 19:22:06 GMT
From:      Jessica Clarke <jrtc27@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 1ef9f0371e2d - main - libc: Consistently use uintptr_t for TLS implementation
Message-ID:  <202505071922.547JM6YN042957@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jrtc27:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ef9f0371e2dce8bc53bd43d6c95f0acba987853

commit 1ef9f0371e2dce8bc53bd43d6c95f0acba987853
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-05-07 19:19:49 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-05-07 19:19:49 +0000

    libc: Consistently use uintptr_t for TLS implementation
    
    Elf_Addr is the format of addresses in the ELF file with the current
    ABI's default class. This is normally the same as the format of an
    address at run time, though technically exceptions do exist outside of
    FreeBSD's currently-supported architectures (for example, IA-64's LP64
    supports both ELFCLASS32 and ELFCLASS64 file formats; LP64 vs ILP32 is
    an orthogonal EF_IA_64_ABI64 flag). On traditional architectures,
    including all currently-supported FreeBSD architectures, addresses and
    pointers are synonymous, but on CHERI they are not, as pointers are
    capabilities that contain metadata alongside the address. In the cases
    here, the quantities are run-time pointers, not addresses (and
    definitely not ELF file addresses), so we should use pointer-ish types.
    
    Note that we already use uintptr_t in struct tcb (both Variant I and
    Variant II) but still use Elf_Addr in various places here.
    
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D50225
---
 lib/libc/gen/tls.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/lib/libc/gen/tls.c b/lib/libc/gen/tls.c
index 218cd1da92df..ad6f43eb3534 100644
--- a/lib/libc/gen/tls.c
+++ b/lib/libc/gen/tls.c
@@ -200,10 +200,10 @@ get_tls_block_ptr(void *tcb, size_t tcbsize)
 void
 __libc_free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused)
 {
-	Elf_Addr *dtv;
-	Elf_Addr **tls;
+	uintptr_t *dtv;
+	uintptr_t **tls;
 
-	tls = (Elf_Addr **)tcb;
+	tls = (uintptr_t **)tcb;
 	dtv = tls[0];
 	__je_bootstrap_free(dtv);
 	libc_free_aligned(get_tls_block_ptr(tcb, tcbsize));
@@ -232,7 +232,7 @@ __libc_free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused)
 void *
 __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign)
 {
-	Elf_Addr *dtv, **tcb;
+	uintptr_t *dtv, **tcb;
 	char *tls_block, *tls;
 	size_t extra_size, maxalign, post_size, pre_size, tls_block_size;
 
@@ -261,7 +261,7 @@ __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign)
 		abort();
 	}
 	memset(tls_block, 0, tls_block_size);
-	tcb = (Elf_Addr **)(tls_block + pre_size + extra_size);
+	tcb = (uintptr_t **)(tls_block + pre_size + extra_size);
 	tls = (char *)tcb + TLS_TCB_SIZE + post_size;
 
 	if (oldtcb != NULL) {
@@ -271,9 +271,9 @@ __libc_allocate_tls(void *oldtcb, size_t tcbsize, size_t tcbalign)
 
 		/* Adjust the DTV. */
 		dtv = tcb[0];
-		dtv[2] = (Elf_Addr)tls;
+		dtv[2] = (uintptr_t)tls;
 	} else {
-		dtv = __je_bootstrap_malloc(3 * sizeof(Elf_Addr));
+		dtv = __je_bootstrap_malloc(3 * sizeof(uintptr_t));
 		if (dtv == NULL) {
 			tls_msg("__libc_allocate_tls: Out of memory.\n");
 			abort();
@@ -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;
+		dtv[2] = (uintptr_t)tls;
 
 		if (libc_tls_init_size > 0)
 			memcpy(tls, libc_tls_init, libc_tls_init_size);
@@ -302,8 +302,8 @@ void
 __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign)
 {
 	size_t size;
-	Elf_Addr* dtv;
-	Elf_Addr tlsstart, tlsend;
+	uintptr_t *dtv;
+	uintptr_t tlsstart, tlsend;
 
 	/*
 	 * Figure out the size of the initial TLS block so that we can
@@ -312,8 +312,8 @@ __libc_free_tls(void *tcb, size_t tcbsize __unused, size_t tcbalign)
 	tcbalign = MAX(tcbalign, libc_tls_init_align);
 	size = roundup2(libc_tls_static_space, tcbalign);
 
-	dtv = ((Elf_Addr**)tcb)[1];
-	tlsend = (Elf_Addr) tcb;
+	dtv = ((uintptr_t **)tcb)[1];
+	tlsend = (uintptr_t)tcb;
 	tlsstart = tlsend - size;
 	libc_free_aligned((void*)tlsstart);
 	__je_bootstrap_free(dtv);
@@ -327,29 +327,29 @@ __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
 {
 	size_t size;
 	char *tls;
-	Elf_Addr *dtv;
-	Elf_Addr segbase, oldsegbase;
+	uintptr_t *dtv;
+	uintptr_t segbase, oldsegbase;
 
 	tcbalign = MAX(tcbalign, libc_tls_init_align);
 	size = roundup2(libc_tls_static_space, tcbalign);
 
-	if (tcbsize < 2 * sizeof(Elf_Addr))
-		tcbsize = 2 * sizeof(Elf_Addr);
+	if (tcbsize < 2 * sizeof(uintptr_t))
+		tcbsize = 2 * sizeof(uintptr_t);
 	tls = libc_malloc_aligned(size + tcbsize, tcbalign);
 	if (tls == NULL) {
 		tls_msg("__libc_allocate_tls: Out of memory.\n");
 		abort();
 	}
 	memset(tls, 0, size + tcbsize);
-	dtv = __je_bootstrap_malloc(3 * sizeof(Elf_Addr));
+	dtv = __je_bootstrap_malloc(3 * sizeof(uintptr_t));
 	if (dtv == NULL) {
 		tls_msg("__libc_allocate_tls: Out of memory.\n");
 		abort();
 	}
 
-	segbase = (Elf_Addr)(tls + size);
-	((Elf_Addr*)segbase)[0] = segbase;
-	((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
+	segbase = (uintptr_t)(tls + size);
+	((uintptr_t *)segbase)[0] = segbase;
+	((uintptr_t *)segbase)[1] = (uintptr_t)dtv;
 
 	dtv[0] = 1;
 	dtv[1] = 1;
@@ -359,7 +359,7 @@ __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
 		/*
 		 * Copy the static TLS block over whole.
 		 */
-		oldsegbase = (Elf_Addr) oldtls;
+		oldsegbase = (uintptr_t)oldtls;
 		memcpy((void *)(segbase - libc_tls_static_space),
 		    (const void *)(oldsegbase - libc_tls_static_space),
 		    libc_tls_static_space);
@@ -368,7 +368,8 @@ __libc_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
 		 * We assume that this block was the one we created with
 		 * allocate_initial_tls().
 		 */
-		_rtld_free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
+		_rtld_free_tls(oldtls, 2 * sizeof(uintptr_t),
+		    sizeof(uintptr_t));
 	} else {
 		memcpy((void *)(segbase - libc_tls_static_space),
 		    libc_tls_init, libc_tls_init_size);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202505071922.547JM6YN042957>