git: 250b2eda0acc - main - libc, libthr: Ditch MD __pthread_distribute_static_tls helpers

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Thu, 29 May 2025 16:07:29 UTC
The branch main has been updated by jrtc27:

URL: https://cgit.FreeBSD.org/src/commit/?id=250b2eda0acc44cf882d5ea8fcf28125e7501719

commit 250b2eda0acc44cf882d5ea8fcf28125e7501719
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-05-29 16:06:54 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-05-29 16:06:54 +0000

    libc, libthr: Ditch MD __pthread_distribute_static_tls helpers
    
    _libc_get_static_tls_base() is just _tcb_get() followed by adding (for
    Variant I) or subtracting (for Variant II) the offset, so just inline
    that as the implementation (like we do in rtld-elf) rather than having
    another copy (or equivalent) of _tcb_get()'s assembly.
    
    _get_static_tls_base() doesn't even have any MD assembly as it's
    reading thr->tcb, the only difference is whether to add or subtract, so
    again just inline that.
    
    Whilst here add some missing blank lines to comply with style(9) for
    elf_utils.c's includes, and use a pointer type rather than uintptr_t to
    reduce the need to cast, as is done in rtld-elf.
    
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D50592
---
 lib/libc/aarch64/static_tls.h                 | 44 -------------------------
 lib/libc/amd64/static_tls.h                   | 44 -------------------------
 lib/libc/arm/static_tls.h                     | 45 --------------------------
 lib/libc/gen/elf_utils.c                      | 17 +++++++---
 lib/libc/i386/static_tls.h                    | 44 -------------------------
 lib/libc/powerpc/static_tls.h                 | 44 -------------------------
 lib/libc/powerpc64/static_tls.h               | 44 -------------------------
 lib/libc/riscv/static_tls.h                   | 46 ---------------------------
 lib/libthr/arch/aarch64/include/pthread_tls.h | 44 -------------------------
 lib/libthr/arch/amd64/include/pthread_tls.h   | 44 -------------------------
 lib/libthr/arch/arm/include/pthread_tls.h     | 44 -------------------------
 lib/libthr/arch/i386/include/pthread_tls.h    | 44 -------------------------
 lib/libthr/arch/powerpc/include/pthread_tls.h | 44 -------------------------
 lib/libthr/arch/riscv/include/pthread_tls.h   | 44 -------------------------
 lib/libthr/thread/thr_list.c                  | 25 +++++++++------
 15 files changed, 28 insertions(+), 589 deletions(-)

diff --git a/lib/libc/aarch64/static_tls.h b/lib/libc/aarch64/static_tls.h
deleted file mode 100644
index 2bb717eb75ce..000000000000
--- a/lib/libc/aarch64/static_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_AARCH64_STATIC_TLS_H
-#define _LIBC_AARCH64_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("mrs	%x0, tpidr_el0" : "=r" (tlsbase));
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/amd64/static_tls.h b/lib/libc/amd64/static_tls.h
deleted file mode 100644
index 1ee738b231c7..000000000000
--- a/lib/libc/amd64/static_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_AMD64_STATIC_TLS_H
-#define	_LIBC_AMD64_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("movq %%fs:0, %0" : "=r" (tlsbase));
-	tlsbase -= offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/arm/static_tls.h b/lib/libc/arm/static_tls.h
deleted file mode 100644
index 557748d75347..000000000000
--- a/lib/libc/arm/static_tls.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_ARM_STATIC_TLS_H
-#define _LIBC_ARM_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("mrc  p15, 0, %0, c13, c0, 3" : "=r" (tlsbase));
-
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/gen/elf_utils.c b/lib/libc/gen/elf_utils.c
index 5b87e012d0eb..330aa8f17f7e 100644
--- a/lib/libc/gen/elf_utils.c
+++ b/lib/libc/gen/elf_utils.c
@@ -31,11 +31,14 @@
 #include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/sysctl.h>
+
+#include <machine/tls.h>
+
 #include <link.h>
 #include <stddef.h>
 #include <string.h>
+
 #include "libc_private.h"
-#include "static_tls.h"
 
 void __pthread_map_stacks_exec(void);
 void __pthread_distribute_static_tls(size_t, void *, size_t, size_t);
@@ -107,11 +110,15 @@ void
 __libc_distribute_static_tls(size_t offset, void *src, size_t len,
     size_t total_len)
 {
-	uintptr_t tlsbase;
+	char *tlsbase;
 
-	tlsbase = _libc_get_static_tls_base(offset);
-	memcpy((void *)tlsbase, src, len);
-	memset((char *)tlsbase + len, 0, total_len - len);
+#ifdef TLS_VARIANT_I
+	tlsbase = (char *)_tcb_get() + offset;
+#else
+	tlsbase = (char *)_tcb_get() - offset;
+#endif
+	memcpy(tlsbase, src, len);
+	memset(tlsbase + len, 0, total_len - len);
 }
 
 #pragma weak __pthread_distribute_static_tls
diff --git a/lib/libc/i386/static_tls.h b/lib/libc/i386/static_tls.h
deleted file mode 100644
index baf9dfc59a5a..000000000000
--- a/lib/libc/i386/static_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_I386_STATIC_TLS_H
-#define _LIBC_I386_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("movl %%gs:0, %0" : "=r" (tlsbase));
-	tlsbase -= offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/powerpc/static_tls.h b/lib/libc/powerpc/static_tls.h
deleted file mode 100644
index 9ae38c71a515..000000000000
--- a/lib/libc/powerpc/static_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_POWERPC_STATIC_TLS_H
-#define _LIBC_POWERPC_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("mr %0,2" : "=r"(tlsbase));
-	tlsbase += offset - 0x7008;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/powerpc64/static_tls.h b/lib/libc/powerpc64/static_tls.h
deleted file mode 100644
index 014b8a931453..000000000000
--- a/lib/libc/powerpc64/static_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_POWERPC64_STATIC_TLS_H
-#define _LIBC_POWERPC64_STATIC_TLS_H
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	__asm __volatile("mr %0,13" : "=r"(tlsbase));
-	tlsbase += offset - 0x7010;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libc/riscv/static_tls.h b/lib/libc/riscv/static_tls.h
deleted file mode 100644
index 40e9abd685e3..000000000000
--- a/lib/libc/riscv/static_tls.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _LIBC_RISCV_STATIC_TLS_H
-#define _LIBC_RISCV_STATIC_TLS_H
-
-#include <machine/tls.h>
-
-static __inline uintptr_t
-_libc_get_static_tls_base(size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)_tcb_get();
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/aarch64/include/pthread_tls.h b/lib/libthr/arch/aarch64/include/pthread_tls.h
deleted file mode 100644
index 4e02f8d4e03f..000000000000
--- a/lib/libthr/arch/aarch64/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_AARCH64_PTHREAD_TLS_H
-#define	_ARCH_AARCH64_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/amd64/include/pthread_tls.h b/lib/libthr/arch/amd64/include/pthread_tls.h
deleted file mode 100644
index 2af0aeda4c85..000000000000
--- a/lib/libthr/arch/amd64/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_AMD64_PTHREAD_TLS_H
-#define	_ARCH_AMD64_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase -= offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/arm/include/pthread_tls.h b/lib/libthr/arch/arm/include/pthread_tls.h
deleted file mode 100644
index 27a07f69f474..000000000000
--- a/lib/libthr/arch/arm/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_ARM_PTHREAD_TLS_H
-#define	_ARCH_ARM_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/i386/include/pthread_tls.h b/lib/libthr/arch/i386/include/pthread_tls.h
deleted file mode 100644
index b2d0f2dbe845..000000000000
--- a/lib/libthr/arch/i386/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_I386_PTHREAD_TLS_H
-#define	_ARCH_I386_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase -= offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/powerpc/include/pthread_tls.h b/lib/libthr/arch/powerpc/include/pthread_tls.h
deleted file mode 100644
index e53164436018..000000000000
--- a/lib/libthr/arch/powerpc/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_POWERPC_PTHREAD_TLS_H
-#define	_ARCH_POWERPC_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/arch/riscv/include/pthread_tls.h b/lib/libthr/arch/riscv/include/pthread_tls.h
deleted file mode 100644
index 0af1ddd4cfaf..000000000000
--- a/lib/libthr/arch/riscv/include/pthread_tls.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 The FreeBSD Foundation
- *
- * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _ARCH_RISCV_PTHREAD_TLS_H
-#define	_ARCH_RISCV_PTHREAD_TLS_H
-
-static __inline uintptr_t
-_get_static_tls_base(struct pthread *thr, size_t offset)
-{
-	uintptr_t tlsbase;
-
-	tlsbase = (uintptr_t)thr->tcb;
-	tlsbase += offset;
-	return (tlsbase);
-}
-
-#endif
diff --git a/lib/libthr/thread/thr_list.c b/lib/libthr/thread/thr_list.c
index 5578abdc8727..820766f6f5e0 100644
--- a/lib/libthr/thread/thr_list.c
+++ b/lib/libthr/thread/thr_list.c
@@ -30,13 +30,14 @@
 #include <sys/types.h>
 #include <sys/queue.h>
 
+#include <machine/tls.h>
+
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
 
 #include "libc_private.h"
 #include "thr_private.h"
-#include "static_tls.h"
 
 /*#define DEBUG_THREAD_LIST */
 #ifdef DEBUG_THREAD_LIST
@@ -363,15 +364,13 @@ _thr_find_thread(struct pthread *curthread, struct pthread *thread,
 	return (ret);
 }
 
-#include "pthread_tls.h"
-
 static void
-thr_distribute_static_tls(uintptr_t tlsbase, void *src, size_t len,
+thr_distribute_static_tls(char *tlsbase, void *src, size_t len,
     size_t total_len)
 {
 
-	memcpy((void *)tlsbase, src, len);
-	memset((char *)tlsbase + len, 0, total_len - len);
+	memcpy(tlsbase, src, len);
+	memset(tlsbase + len, 0, total_len - len);
 }
 
 void
@@ -379,17 +378,25 @@ __pthread_distribute_static_tls(size_t offset, void *src, size_t len,
     size_t total_len)
 {
 	struct pthread *curthread, *thrd;
-	uintptr_t tlsbase;
+	char *tlsbase;
 
 	if (!_thr_is_inited()) {
-		tlsbase = _libc_get_static_tls_base(offset);
+#ifdef TLS_VARIANT_I
+		tlsbase = (char *)_tcb_get() + offset;
+#else
+		tlsbase = (char *)_tcb_get() - offset;
+#endif
 		thr_distribute_static_tls(tlsbase, src, len, total_len);
 		return;
 	}
 	curthread = _get_curthread();
 	THREAD_LIST_RDLOCK(curthread);
 	TAILQ_FOREACH(thrd, &_thread_list, tle) {
-		tlsbase = _get_static_tls_base(thrd, offset);
+#ifdef TLS_VARIANT_I
+		tlsbase = (char *)thrd->tcb + offset;
+#else
+		tlsbase = (char *)thrd->tcb - offset;
+#endif
 		thr_distribute_static_tls(tlsbase, src, len, total_len);
 	}
 	THREAD_LIST_UNLOCK(curthread);