From owner-svn-src-stable@freebsd.org  Tue Sep  3 14:06:46 2019
Return-Path: <owner-svn-src-stable@freebsd.org>
Delivered-To: svn-src-stable@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2952EDCEF0;
 Tue,  3 Sep 2019 14:06:46 +0000 (UTC)
 (envelope-from yuripv@freebsd.org)
Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "freefall.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 46N7zn6Ry1z4Pt3;
 Tue,  3 Sep 2019 14:06:45 +0000 (UTC)
 (envelope-from yuripv@freebsd.org)
Received: by freefall.freebsd.org (Postfix, from userid 1452)
 id DDCB41AB43; Tue,  3 Sep 2019 14:06:16 +0000 (UTC)
X-Original-To: yuripv@localmail.freebsd.org
Delivered-To: yuripv@localmail.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
 (Client CN "mx1.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by freefall.freebsd.org (Postfix) with ESMTPS id BD6A014D5F;
 Fri, 12 Apr 2019 15:15:35 +0000 (UTC)
 (envelope-from owner-src-committers@freebsd.org)
Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "freefall.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 2ECBD776B2;
 Fri, 12 Apr 2019 15:15:35 +0000 (UTC)
 (envelope-from owner-src-committers@freebsd.org)
Received: by freefall.freebsd.org (Postfix, from userid 538)
 id 1046214D32; Fri, 12 Apr 2019 15:15:35 +0000 (UTC)
Delivered-To: src-committers@localmail.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
 (Client CN "mx1.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by freefall.freebsd.org (Postfix) with ESMTPS id A5BD514D2C
 for <src-committers@localmail.freebsd.org>;
 Fri, 12 Apr 2019 15:15:32 +0000 (UTC) (envelope-from kib@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)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 83AAA776AB;
 Fri, 12 Apr 2019 15:15:32 +0000 (UTC) (envelope-from kib@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 714FC2326E;
 Fri, 12 Apr 2019 15:15:32 +0000 (UTC) (envelope-from kib@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x3CFFWIr012098;
 Fri, 12 Apr 2019 15:15:32 GMT (envelope-from kib@FreeBSD.org)
Received: (from kib@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id x3CFFRA8012071;
 Fri, 12 Apr 2019 15:15:27 GMT (envelope-from kib@FreeBSD.org)
Message-Id: <201904121515.x3CFFRA8012071@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org
 using -f
From: Konstantin Belousov <kib@FreeBSD.org>
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject: svn commit: r346156 - in stable/11: lib/libc/aarch64 lib/libc/amd64
 lib/libc/arm lib/libc/gen lib/libc/i386 lib/libc/include lib/libc/mips
 lib/libc/powerpc lib/libc/powerpc64 lib/libc/riscv lib/lib...
X-SVN-Group: stable-11
X-SVN-Commit-Author: kib
X-SVN-Commit-Paths: in stable/11: lib/libc/aarch64 lib/libc/amd64 lib/libc/arm
 lib/libc/gen lib/libc/i386 lib/libc/include lib/libc/mips lib/libc/powerpc
 lib/libc/powerpc64 lib/libc/riscv lib/libc/sparc64 lib/libc/sys li...
X-SVN-Commit-Revision: 346156
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Precedence: bulk
X-Loop: FreeBSD.org
Sender: owner-src-committers@freebsd.org
X-Rspamd-Queue-Id: 2ECBD776B2
X-Spamd-Bar: --
Authentication-Results: mx1.freebsd.org
X-Spamd-Result: default: False [-2.97 / 15.00];
 local_wl_from(0.00)[freebsd.org];
 NEURAL_HAM_MEDIUM(-1.00)[-0.999,0];
 NEURAL_HAM_SHORT(-0.97)[-0.973,0];
 NEURAL_HAM_LONG(-1.00)[-1.000,0];
 ASN(0.00)[asn:11403, ipnet:96.47.64.0/20, country:US]
Status: O
X-BeenThere: svn-src-stable@freebsd.org
X-Mailman-Version: 2.1.29
List-Id: SVN commit messages for all the -stable branches of the src tree
 <svn-src-stable.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-stable>, 
 <mailto:svn-src-stable-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-stable/>
List-Post: <mailto:svn-src-stable@freebsd.org>
List-Help: <mailto:svn-src-stable-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-stable>,
 <mailto:svn-src-stable-request@freebsd.org?subject=subscribe>
Date: Tue, 03 Sep 2019 14:06:46 -0000
X-Original-Date: Fri, 12 Apr 2019 15:15:27 +0000 (UTC)
X-List-Received-Date: Tue, 03 Sep 2019 14:06:46 -0000

Author: kib
Date: Fri Apr 12 15:15:27 2019
New Revision: 346156
URL: https://svnweb.freebsd.org/changeset/base/346156

Log:
  MFC r345703:
  Fix initial exec TLS mode for dynamically loaded shared objects.

Added:
  stable/11/lib/libc/aarch64/static_tls.h
     - copied unchanged from r345703, head/lib/libc/aarch64/static_tls.h
  stable/11/lib/libc/amd64/static_tls.h
     - copied unchanged from r345703, head/lib/libc/amd64/static_tls.h
  stable/11/lib/libc/arm/static_tls.h
     - copied unchanged from r345703, head/lib/libc/arm/static_tls.h
  stable/11/lib/libc/i386/static_tls.h
     - copied unchanged from r345703, head/lib/libc/i386/static_tls.h
  stable/11/lib/libc/mips/static_tls.h
     - copied unchanged from r345703, head/lib/libc/mips/static_tls.h
  stable/11/lib/libc/powerpc/static_tls.h
     - copied unchanged from r345703, head/lib/libc/powerpc/static_tls.h
  stable/11/lib/libc/powerpc64/static_tls.h
     - copied unchanged from r345703, head/lib/libc/powerpc64/static_tls.h
  stable/11/lib/libc/riscv/static_tls.h
     - copied unchanged from r345703, head/lib/libc/riscv/static_tls.h
  stable/11/lib/libc/sparc64/static_tls.h
     - copied unchanged from r345703, head/lib/libc/sparc64/static_tls.h
  stable/11/lib/libthr/arch/aarch64/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/aarch64/include/pthread_tls.h
  stable/11/lib/libthr/arch/amd64/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/amd64/include/pthread_tls.h
  stable/11/lib/libthr/arch/arm/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/arm/include/pthread_tls.h
  stable/11/lib/libthr/arch/i386/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/i386/include/pthread_tls.h
  stable/11/lib/libthr/arch/mips/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/mips/include/pthread_tls.h
  stable/11/lib/libthr/arch/powerpc/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/powerpc/include/pthread_tls.h
  stable/11/lib/libthr/arch/riscv/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/riscv/include/pthread_tls.h
  stable/11/lib/libthr/arch/sparc64/include/pthread_tls.h
     - copied unchanged from r345703, head/lib/libthr/arch/sparc64/include/pthread_tls.h
Modified:
  stable/11/lib/libc/gen/Symbol.map
  stable/11/lib/libc/gen/elf_utils.c
  stable/11/lib/libc/include/libc_private.h
  stable/11/lib/libc/sys/interposing_table.c
  stable/11/lib/libthr/Makefile
  stable/11/lib/libthr/pthread.map
  stable/11/lib/libthr/thread/thr_list.c
  stable/11/lib/libthr/thread/thr_private.h
  stable/11/libexec/rtld-elf/rtld.c
  stable/11/libexec/rtld-elf/rtld.h
Directory Properties:
  stable/11/   (props changed)

Copied: stable/11/lib/libc/aarch64/static_tls.h (from r345703, head/lib/libc/aarch64/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/aarch64/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/aarch64/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libc/amd64/static_tls.h (from r345703, head/lib/libc/amd64/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/amd64/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/amd64/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libc/arm/static_tls.h (from r345703, head/lib/libc/arm/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/arm/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/arm/static_tls.h)
@@ -0,0 +1,51 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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;
+
+#ifdef ARM_TP_ADDRESS
+	tlsbase = *(uintptr_t *)ARM_TP_ADDRESS;
+#else
+	__asm __volatile("mrc  p15, 0, %0, c13, c0, 3" : "=r" (tlsbase));
+#endif
+
+	tlsbase += offset;
+	return (tlsbase);
+}
+
+#endif

Modified: stable/11/lib/libc/gen/Symbol.map
==============================================================================
--- stable/11/lib/libc/gen/Symbol.map	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libc/gen/Symbol.map	Fri Apr 12 15:15:27 2019	(r346156)
@@ -545,6 +545,7 @@ FBSDprivate_1.0 {
 	__libc_tcdrain;
 
 	__elf_aux_vector;
+	__pthread_distribute_static_tls;
 	__pthread_map_stacks_exec;
 	__fillcontextx;
 	__fillcontextx2;

Modified: stable/11/lib/libc/gen/elf_utils.c
==============================================================================
--- stable/11/lib/libc/gen/elf_utils.c	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libc/gen/elf_utils.c	Fri Apr 12 15:15:27 2019	(r346156)
@@ -32,10 +32,13 @@
 #include <sys/sysctl.h>
 #include <link.h>
 #include <stddef.h>
+#include <string.h>
 #include "libc_private.h"
+#include "static_tls.h"
 
 int __elf_phdr_match_addr(struct dl_phdr_info *, void *);
 void __pthread_map_stacks_exec(void);
+void __pthread_distribute_static_tls(size_t, void *, size_t, size_t);
 
 int
 __elf_phdr_match_addr(struct dl_phdr_info *phdr_info, void *addr)
@@ -81,4 +84,25 @@ __pthread_map_stacks_exec(void)
 {
 
 	((void (*)(void))__libc_interposing[INTERPOS_map_stacks_exec])();
+}
+
+void
+__libc_distribute_static_tls(size_t offset, void *src, size_t len,
+    size_t total_len)
+{
+	uintptr_t tlsbase;
+
+	tlsbase = _libc_get_static_tls_base(offset);
+	memcpy((void *)tlsbase, src, len);
+	memset((char *)tlsbase + len, 0, total_len - len);
+}
+
+#pragma weak __pthread_distribute_static_tls
+void
+__pthread_distribute_static_tls(size_t offset, void *src, size_t len,
+    size_t total_len)
+{
+
+	((void (*)(size_t, void *, size_t, size_t))__libc_interposing[
+	    INTERPOS_distribute_static_tls])(offset, src, len, total_len);
 }

Copied: stable/11/lib/libc/i386/static_tls.h (from r345703, head/lib/libc/i386/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/i386/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/i386/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Modified: stable/11/lib/libc/include/libc_private.h
==============================================================================
--- stable/11/lib/libc/include/libc_private.h	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libc/include/libc_private.h	Fri Apr 12 15:15:27 2019	(r346156)
@@ -233,6 +233,7 @@ enum {
 	INTERPOS_map_stacks_exec,
 	INTERPOS_fdatasync,
 	INTERPOS_clock_nanosleep,
+	INTERPOS_distribute_static_tls,
 	INTERPOS_MAX
 };
 
@@ -403,6 +404,8 @@ struct dl_phdr_info;
 int __elf_phdr_match_addr(struct dl_phdr_info *, void *);
 void __init_elf_aux_vector(void);
 void __libc_map_stacks_exec(void);
+void __libc_distribute_static_tls(__size_t, void *, __size_t, __size_t);
+__uintptr_t __libc_static_tls_base(__size_t);
 
 void	_pthread_cancel_enter(int);
 void	_pthread_cancel_leave(int);

Copied: stable/11/lib/libc/mips/static_tls.h (from r345703, head/lib/libc/mips/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/mips/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/mips/static_tls.h)
@@ -0,0 +1,64 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _LIBC_MIPS_STATIC_TLS_H
+#define _LIBC_MIPS_STATIC_TLS_H
+
+#include <machine/tls.h>
+
+static __inline uintptr_t
+_libc_get_static_tls_base(size_t offset)
+{
+	uintptr_t tlsbase;
+
+#if defined(__mips_n64)
+	__asm__ __volatile__ (
+	    ".set\tpush\n\t"
+	    ".set\tmips64r2\n\t"
+	    "rdhwr\t%0, $29\n\t"
+	    ".set\tpop"
+	    : "=r" (tlsbase));
+	tlsbase -= TLS_TP_OFFSET + TLS_TCB_SIZE;
+#else /* mips 32 */
+	__asm__ __volatile__ (
+	    ".set\tpush\n\t"
+	    ".set\tmips32r2\n\t"
+	    "rdhwr\t%0, $29\n\t"
+	    ".set\tpop"
+	    : "=r" (tlsbase));
+	tlsbase -= TLS_TP_OFFSET + TLS_TCB_SIZE;
+#endif /* ! __mips_n64 */
+	tlsbase += offset;
+	return (tlsbase);
+}
+
+#endif

Copied: stable/11/lib/libc/powerpc/static_tls.h (from r345703, head/lib/libc/powerpc/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/powerpc/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/powerpc/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libc/powerpc64/static_tls.h (from r345703, head/lib/libc/powerpc64/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/powerpc64/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/powerpc64/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libc/riscv/static_tls.h (from r345703, head/lib/libc/riscv/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/riscv/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/riscv/static_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _LIBC_RISCV_STATIC_TLS_H
+#define _LIBC_RISCV_STATIC_TLS_H
+
+static __inline uintptr_t
+_libc_get_static_tls_base(size_t offset)
+{
+	uintptr_t tlsbase;
+
+	__asm __volatile("mv %0, tp" : "=r"(tlsbase));
+	tlsbase += offset;
+	return (tlsbase);
+}
+
+#endif

Copied: stable/11/lib/libc/sparc64/static_tls.h (from r345703, head/lib/libc/sparc64/static_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libc/sparc64/static_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libc/sparc64/static_tls.h)
@@ -0,0 +1,44 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _LIBC_SPARC64_STATIC_TLS_H
+#define _LIBC_SPARC64_STATIC_TLS_H
+
+static __inline uintptr_t
+_libc_get_static_tls_base(size_t offset)
+{
+	register uintptr_t tlsbase __asm("%g7");
+
+	return (tlsbase + offset);
+}
+
+#endif

Modified: stable/11/lib/libc/sys/interposing_table.c
==============================================================================
--- stable/11/lib/libc/sys/interposing_table.c	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libc/sys/interposing_table.c	Fri Apr 12 15:15:27 2019	(r346156)
@@ -81,6 +81,7 @@ interpos_func_t __libc_interposing[INTERPOS_MAX] = {
 	SLOT(map_stacks_exec, __libc_map_stacks_exec),
 	SLOT(fdatasync, __sys_fdatasync),
 	SLOT(clock_nanosleep, __sys_clock_nanosleep),
+	SLOT(distribute_static_tls, __libc_distribute_static_tls),
 };
 #undef SLOT
 

Modified: stable/11/lib/libthr/Makefile
==============================================================================
--- stable/11/lib/libthr/Makefile	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libthr/Makefile	Fri Apr 12 15:15:27 2019	(r346156)
@@ -19,8 +19,10 @@ SHLIB_MAJOR= 3
 WARNS?=	3
 NO_WTHREAD_SAFETY=1
 CFLAGS+=-DPTHREAD_KERNEL
-CFLAGS+=-I${SRCTOP}/lib/libc/include -I${.CURDIR}/thread \
-	-I${SRCTOP}/include
+CFLAGS+=-I${SRCTOP}/lib/libc/include
+CFLAGS+=-I${SRCTOP}/lib/libc/${MACHINE_CPUARCH}
+CFLAGS+=-I${.CURDIR}/thread
+CFLAGS+=-I${SRCTOP}/include
 CFLAGS+=-I${.CURDIR}/arch/${MACHINE_CPUARCH}/include
 CFLAGS+=-I${.CURDIR}/sys
 CFLAGS+=-I${SRCTOP}/libexec/rtld-elf

Copied: stable/11/lib/libthr/arch/aarch64/include/pthread_tls.h (from r345703, head/lib/libthr/arch/aarch64/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/aarch64/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/aarch64/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/amd64/include/pthread_tls.h (from r345703, head/lib/libthr/arch/amd64/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/amd64/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/amd64/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/arm/include/pthread_tls.h (from r345703, head/lib/libthr/arch/arm/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/arm/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/arm/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/i386/include/pthread_tls.h (from r345703, head/lib/libthr/arch/i386/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/i386/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/i386/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/mips/include/pthread_tls.h (from r345703, head/lib/libthr/arch/mips/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/mips/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/mips/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _ARCH_MIPS_PTHREAD_TLS_H
+#define	_ARCH_MIPS_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

Copied: stable/11/lib/libthr/arch/powerpc/include/pthread_tls.h (from r345703, head/lib/libthr/arch/powerpc/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/powerpc/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/powerpc/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/riscv/include/pthread_tls.h (from r345703, head/lib/libthr/arch/riscv/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/riscv/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/riscv/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#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

Copied: stable/11/lib/libthr/arch/sparc64/include/pthread_tls.h (from r345703, head/lib/libthr/arch/sparc64/include/pthread_tls.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/lib/libthr/arch/sparc64/include/pthread_tls.h	Fri Apr 12 15:15:27 2019	(r346156, copy of r345703, head/lib/libthr/arch/sparc64/include/pthread_tls.h)
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _ARCH_SPARC64_PTHREAD_TLS_H
+#define	_ARCH_SPARC64_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

Modified: stable/11/lib/libthr/pthread.map
==============================================================================
--- stable/11/lib/libthr/pthread.map	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libthr/pthread.map	Fri Apr 12 15:15:27 2019	(r346156)
@@ -137,6 +137,7 @@ FBSDprivate_1.0 {
 	__pthread_mutex_lock;
 	__pthread_mutex_timedlock;
 	__pthread_mutex_trylock;
+	__pthread_distribute_static_tls;
 	_pthread_atfork;
 	_pthread_barrier_destroy;
 	_pthread_barrier_init;

Modified: stable/11/lib/libthr/thread/thr_list.c
==============================================================================
--- stable/11/lib/libthr/thread/thr_list.c	Fri Apr 12 15:12:08 2019	(r346155)
+++ stable/11/lib/libthr/thread/thr_list.c	Fri Apr 12 15:15:27 2019	(r346156)
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
 
 #include "libc_private.h"
 #include "thr_private.h"
+#include "static_tls.h"
 
 /*#define DEBUG_THREAD_LIST */
 #ifdef DEBUG_THREAD_LIST
@@ -359,4 +360,36 @@ _thr_find_thread(struct pthread *curthread, struct pth
 	}
 	THREAD_LIST_UNLOCK(curthread);
 	return (ret);
+}
+
+#include "pthread_tls.h"
+
+static void
+thr_distribute_static_tls(uintptr_t tlsbase, void *src, size_t len,
+    size_t total_len)
+{
+
+	memcpy((void *)tlsbase, src, len);
+	memset((char *)tlsbase + len, 0, total_len - len);
+}
+
+void
+__pthread_distribute_static_tls(size_t offset, void *src, size_t len,
+    size_t total_len)
+{
+	struct pthread *curthread, *thrd;
+	uintptr_t tlsbase;
+
+	if (!_thr_is_inited()) {
+		tlsbase = _libc_get_static_tls_base(offset);
+		thr_distribute_static_tls(tlsbase, src, len, total_len);
+		return;
+	}
+	curthread = _get_curthread();

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***