[PATCH v2] newlib: import TLS initialization and helpers from picolibc
Torbjörn SVENSSON
torbjorn.svensson@foss.st.com
Tue Oct 6 09:48:28 GMT 2026
Changes since v1:
- Dropped all references to RP2040
- Included re-generated newlib/Makefile.in
- Binutils (ld) requirement has been merged.
During GNU Tools Cauldron 2026 this weekend, I had a live review of this patch
with Richard and RP2040 code needs to go into libgloss or, be removed. I
choose to just remove it since I have no board to verify it and I think it has
very limited use.
Ok for master?
Kind regards,
Torbjörn
--
This patch introduces, optional, TLS (thread local storage) initialization to
the startup code. It also brings in the same interface that is exposed to the
application from picolibc so that an application can initialize additional
blocks as needed and switch to them when switching tasks.
The picolibc sources are kept as-is except for:
- _BEGIN_STD_C -> __BEGIN_DECLS
- _END_STD_C -> __END_DECLS
- #include <picolibc.h> -> #include <newlib.h>
- Removed references to RP2040.
Origin of the code is picolibc-1.8.12.
This new feature is activated by --enable-newlib-thread-local-storage.
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
---
libgloss/arm/crt0.S | 13 +++
newlib/Makefile.in | 153 ++++++++++++++++++++-------
newlib/configure | 21 ++++
newlib/configure.ac | 13 +++
newlib/libc/include/picotls.h | 76 +++++++++++++
newlib/libc/machine/arm/Makefile.inc | 1 +
newlib/libc/machine/arm/arm_tls.h | 49 +++++++++
newlib/libc/machine/arm/read_tp.S | 76 +++++++++++++
newlib/libc/machine/arm/set_tls.c | 74 +++++++++++++
newlib/libc/machine/arm/tls.c | 52 +++++++++
newlib/libc/misc/Makefile.inc | 1 +
newlib/libc/misc/inittls.c | 77 ++++++++++++++
newlib/libc/sys/arm/crt0.S | 13 +++
newlib/newlib.hin | 6 ++
14 files changed, 587 insertions(+), 38 deletions(-)
create mode 100644 newlib/libc/include/picotls.h
create mode 100644 newlib/libc/machine/arm/arm_tls.h
create mode 100644 newlib/libc/machine/arm/read_tp.S
create mode 100644 newlib/libc/machine/arm/set_tls.c
create mode 100644 newlib/libc/machine/arm/tls.c
create mode 100644 newlib/libc/misc/inittls.c
diff --git a/libgloss/arm/crt0.S b/libgloss/arm/crt0.S
index b9e768007..81033a3b2 100644
--- a/libgloss/arm/crt0.S
+++ b/libgloss/arm/crt0.S
@@ -388,6 +388,15 @@ __change_mode:
#endif
bl FUNCTION (memset)
+
+#ifdef __THREAD_LOCAL_STORAGE
+ /* Initialize TLS in local-exec mode. */
+ ldr a1, .LCtls_base /* First arg: TLS block. */
+ bl FUNCTION (_init_tls)
+ ldr a1, .LCtls_base /* First arg: TLS block. */
+ bl FUNCTION (_set_tls)
+#endif
+
#if !defined (ARM_RDP_MONITOR) && !defined (ARM_RDI_MONITOR)
/* Changes by toralf: Taken from libgloss/m68k/crt0.S
initialize target specific stuff. Only execute these
@@ -607,6 +616,10 @@ change_back:
.word __bss_start__
.LC2:
.word __bss_end__
+#ifdef __THREAD_LOCAL_STORAGE
+.LCtls_base:
+ .word __tls_base
+#endif
#ifdef __USES_INITFINI__
#ifdef _LITE_EXIT
.Latexit:
diff --git a/newlib/Makefile.in b/newlib/Makefile.in
index 4186ae553..65a24ca02 100644
--- a/newlib/Makefile.in
+++ b/newlib/Makefile.in
@@ -720,6 +720,7 @@ check_PROGRAMS =
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/aeabi_memcpy.c libc/machine/arm/aeabi_memcpy-armv7a.S \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/aeabi_memmove.c libc/machine/arm/aeabi_memmove-soft.S \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/aeabi_memset.c libc/machine/arm/aeabi_memset-soft.S libc/machine/arm/aeabi_memclr.c \
+@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/tls.c libc/machine/arm/set_tls.c libc/machine/arm/read_tp.S \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/memchr-stub.c \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/memchr.S \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/memcpy-stub.c \
@@ -1996,6 +1997,9 @@ am__objects_51 = libc/ssp/libc_a-chk_fail.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-aeabi_memset.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-aeabi_memset-soft.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-aeabi_memclr.$(OBJEXT) \
+@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-tls.$(OBJEXT) \
+@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-set_tls.$(OBJEXT) \
+@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-read_tp.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-memchr-stub.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-memchr.$(OBJEXT) \
@HAVE_LIBC_MACHINE_ARM_TRUE@ libc/machine/arm/libc_a-memcpy-stub.$(OBJEXT) \
@@ -2647,7 +2651,9 @@ am_libc_a_OBJECTS = $(am__objects_1) \
libc/errno/libc_a-errno.$(OBJEXT) \
libc/misc/libc_a-__dprintf.$(OBJEXT) \
libc/misc/libc_a-unctrl.$(OBJEXT) \
- libc/misc/libc_a-ffs.$(OBJEXT) libc/misc/libc_a-init.$(OBJEXT) \
+ libc/misc/libc_a-ffs.$(OBJEXT) \
+ libc/misc/libc_a-inittls.$(OBJEXT) \
+ libc/misc/libc_a-init.$(OBJEXT) \
libc/misc/libc_a-fini.$(OBJEXT) $(am__objects_30) \
$(am__objects_31) $(am__objects_33) $(am__objects_34) \
$(am__objects_36) $(am__objects_37) $(am__objects_24) \
@@ -4244,43 +4250,43 @@ libc_a_SOURCES = $(am__append_5) libc/stdlib/__adjust.c \
libc/reent/statr.c libc/reent/timesr.c libc/reent/unlinkr.c \
libc/reent/writer.c $(am__append_21) $(am__append_22) \
$(am__append_23) libc/errno/errno.c libc/misc/__dprintf.c \
- libc/misc/unctrl.c libc/misc/ffs.c libc/misc/init.c \
- libc/misc/fini.c $(am__append_24) $(am__append_25) \
- $(am__append_26) $(am__append_27) $(am__append_28) \
- $(am__append_29) $(am__append_30) $(am__append_31) \
- $(am__append_32) $(am__append_33) $(am__append_34) \
- $(am__append_35) $(am__append_36) $(am__append_38) \
- $(am__append_41) $(am__append_42) $(am__append_43) \
- $(am__append_44) $(am__append_45) $(am__append_46) \
- $(am__append_47) $(am__append_48) $(am__append_49) \
- $(am__append_50) $(am__append_51) $(am__append_52) \
- $(am__append_53) $(am__append_54) $(am__append_55) \
- $(am__append_56) $(am__append_57) $(am__append_58) \
- $(am__append_59) $(am__append_60) $(am__append_61) \
- $(am__append_62) $(am__append_63) $(am__append_64) \
- $(am__append_65) $(am__append_66) $(am__append_67) \
- $(am__append_68) $(am__append_69) $(am__append_70) \
- $(am__append_71) $(am__append_72) $(am__append_74) \
- $(am__append_75) $(am__append_76) $(am__append_77) \
- $(am__append_78) $(am__append_79) $(am__append_80) \
- $(am__append_81) $(am__append_82) $(am__append_83) \
- $(am__append_84) $(am__append_85) $(am__append_86) \
- $(am__append_87) $(am__append_88) $(am__append_89) \
- $(am__append_90) $(am__append_91) $(am__append_92) \
- $(am__append_93) $(am__append_94) $(am__append_95) \
- $(am__append_96) $(am__append_97) $(am__append_98) \
- $(am__append_99) $(am__append_100) $(am__append_101) \
- $(am__append_102) $(am__append_103) $(am__append_104) \
- $(am__append_105) $(am__append_106) $(am__append_107) \
- $(am__append_108) $(am__append_109) $(am__append_110) \
- $(am__append_111) $(am__append_112) $(am__append_113) \
- $(am__append_114) $(am__append_115) $(am__append_116) \
- $(am__append_117) $(am__append_118) $(am__append_119) \
- $(am__append_120) $(am__append_121) $(am__append_122) \
- $(am__append_123) $(am__append_124) $(am__append_125) \
- $(am__append_126) $(am__append_127) $(am__append_128) \
- $(am__append_129) $(am__append_130) $(am__append_131) \
- $(am__append_132)
+ libc/misc/unctrl.c libc/misc/ffs.c libc/misc/inittls.c \
+ libc/misc/init.c libc/misc/fini.c $(am__append_24) \
+ $(am__append_25) $(am__append_26) $(am__append_27) \
+ $(am__append_28) $(am__append_29) $(am__append_30) \
+ $(am__append_31) $(am__append_32) $(am__append_33) \
+ $(am__append_34) $(am__append_35) $(am__append_36) \
+ $(am__append_38) $(am__append_41) $(am__append_42) \
+ $(am__append_43) $(am__append_44) $(am__append_45) \
+ $(am__append_46) $(am__append_47) $(am__append_48) \
+ $(am__append_49) $(am__append_50) $(am__append_51) \
+ $(am__append_52) $(am__append_53) $(am__append_54) \
+ $(am__append_55) $(am__append_56) $(am__append_57) \
+ $(am__append_58) $(am__append_59) $(am__append_60) \
+ $(am__append_61) $(am__append_62) $(am__append_63) \
+ $(am__append_64) $(am__append_65) $(am__append_66) \
+ $(am__append_67) $(am__append_68) $(am__append_69) \
+ $(am__append_70) $(am__append_71) $(am__append_72) \
+ $(am__append_74) $(am__append_75) $(am__append_76) \
+ $(am__append_77) $(am__append_78) $(am__append_79) \
+ $(am__append_80) $(am__append_81) $(am__append_82) \
+ $(am__append_83) $(am__append_84) $(am__append_85) \
+ $(am__append_86) $(am__append_87) $(am__append_88) \
+ $(am__append_89) $(am__append_90) $(am__append_91) \
+ $(am__append_92) $(am__append_93) $(am__append_94) \
+ $(am__append_95) $(am__append_96) $(am__append_97) \
+ $(am__append_98) $(am__append_99) $(am__append_100) \
+ $(am__append_101) $(am__append_102) $(am__append_103) \
+ $(am__append_104) $(am__append_105) $(am__append_106) \
+ $(am__append_107) $(am__append_108) $(am__append_109) \
+ $(am__append_110) $(am__append_111) $(am__append_112) \
+ $(am__append_113) $(am__append_114) $(am__append_115) \
+ $(am__append_116) $(am__append_117) $(am__append_118) \
+ $(am__append_119) $(am__append_120) $(am__append_121) \
+ $(am__append_122) $(am__append_123) $(am__append_124) \
+ $(am__append_125) $(am__append_126) $(am__append_127) \
+ $(am__append_128) $(am__append_129) $(am__append_130) \
+ $(am__append_131) $(am__append_132)
libc_a_CFLAGS = $(AM_CFLAGS) $(libc_a_CFLAGS_$(subst /,_,$(@D))) $(libc_a_CFLAGS_$(subst /,_,$(@D)_$(<F)))
libc_a_CCASFLAGS = $(AM_CCASFLAGS) $(libc_a_CCASFLAGS_$(subst /,_,$(@D))) $(libc_a_CCASFLAGS_$(subst /,_,$(@D)_$(<F)))
libc_a_CPPFLAGS = $(AM_CPPFLAGS) $(libc_a_CPPFLAGS_$(subst /,_,$(@D))) $(libc_a_CPPFLAGS_$(subst /,_,$(@D)_$(<F)))
@@ -6748,6 +6754,8 @@ libc/misc/libc_a-unctrl.$(OBJEXT): libc/misc/$(am__dirstamp) \
libc/misc/$(DEPDIR)/$(am__dirstamp)
libc/misc/libc_a-ffs.$(OBJEXT): libc/misc/$(am__dirstamp) \
libc/misc/$(DEPDIR)/$(am__dirstamp)
+libc/misc/libc_a-inittls.$(OBJEXT): libc/misc/$(am__dirstamp) \
+ libc/misc/$(DEPDIR)/$(am__dirstamp)
libc/misc/libc_a-init.$(OBJEXT): libc/misc/$(am__dirstamp) \
libc/misc/$(DEPDIR)/$(am__dirstamp)
libc/misc/libc_a-fini.$(OBJEXT): libc/misc/$(am__dirstamp) \
@@ -8393,6 +8401,15 @@ libc/machine/arm/libc_a-aeabi_memset-soft.$(OBJEXT): \
libc/machine/arm/libc_a-aeabi_memclr.$(OBJEXT): \
libc/machine/arm/$(am__dirstamp) \
libc/machine/arm/$(DEPDIR)/$(am__dirstamp)
+libc/machine/arm/libc_a-tls.$(OBJEXT): \
+ libc/machine/arm/$(am__dirstamp) \
+ libc/machine/arm/$(DEPDIR)/$(am__dirstamp)
+libc/machine/arm/libc_a-set_tls.$(OBJEXT): \
+ libc/machine/arm/$(am__dirstamp) \
+ libc/machine/arm/$(DEPDIR)/$(am__dirstamp)
+libc/machine/arm/libc_a-read_tp.$(OBJEXT): \
+ libc/machine/arm/$(am__dirstamp) \
+ libc/machine/arm/$(DEPDIR)/$(am__dirstamp)
libc/machine/arm/libc_a-memchr-stub.$(OBJEXT): \
libc/machine/arm/$(am__dirstamp) \
libc/machine/arm/$(DEPDIR)/$(am__dirstamp)
@@ -12981,11 +12998,14 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-memchr.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-memcpy-stub.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-memcpy.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-setjmp.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-strcmp.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-strcpy.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-strlen-stub.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-strlen.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arm/$(DEPDIR)/libc_a-tls.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/bfin/$(DEPDIR)/libc_a-longjmp.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/bfin/$(DEPDIR)/libc_a-setjmp.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/cr16/$(DEPDIR)/libc_a-getenv.Po@am__quote@
@@ -13377,6 +13397,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-ffs.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-fini.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-init.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-inittls.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-lock.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/misc/$(DEPDIR)/libc_a-unctrl.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@libc/posix/$(DEPDIR)/libc_a-_isatty.Po@am__quote@
@@ -17581,6 +17602,20 @@ libc/machine/arm/libc_a-aeabi_memset-soft.obj: libc/machine/arm/aeabi_memset-sof
@AMDEP_TRUE@@am__fastdepCCAS_FALSE@ DEPDIR=$(DEPDIR) $(CCASDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCCAS_FALSE@ $(AM_V_CPPAS@am__nodep@)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -c -o libc/machine/arm/libc_a-aeabi_memset-soft.obj `if test -f 'libc/machine/arm/aeabi_memset-soft.S'; then $(CYGPATH_W) 'libc/machine/arm/aeabi_memset-soft.S'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/aeabi_memset-soft.S'; fi`
+libc/machine/arm/libc_a-read_tp.o: libc/machine/arm/read_tp.S
+@am__fastdepCCAS_TRUE@ $(AM_V_CPPAS)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -MT libc/machine/arm/libc_a-read_tp.o -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Tpo -c -o libc/machine/arm/libc_a-read_tp.o `test -f 'libc/machine/arm/read_tp.S' || echo '$(srcdir)/'`libc/machine/arm/read_tp.S
+@am__fastdepCCAS_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Tpo libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Po
+@AMDEP_TRUE@@am__fastdepCCAS_FALSE@ $(AM_V_CPPAS)source='libc/machine/arm/read_tp.S' object='libc/machine/arm/libc_a-read_tp.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCCAS_FALSE@ DEPDIR=$(DEPDIR) $(CCASDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCCAS_FALSE@ $(AM_V_CPPAS@am__nodep@)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -c -o libc/machine/arm/libc_a-read_tp.o `test -f 'libc/machine/arm/read_tp.S' || echo '$(srcdir)/'`libc/machine/arm/read_tp.S
+
+libc/machine/arm/libc_a-read_tp.obj: libc/machine/arm/read_tp.S
+@am__fastdepCCAS_TRUE@ $(AM_V_CPPAS)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -MT libc/machine/arm/libc_a-read_tp.obj -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Tpo -c -o libc/machine/arm/libc_a-read_tp.obj `if test -f 'libc/machine/arm/read_tp.S'; then $(CYGPATH_W) 'libc/machine/arm/read_tp.S'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/read_tp.S'; fi`
+@am__fastdepCCAS_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Tpo libc/machine/arm/$(DEPDIR)/libc_a-read_tp.Po
+@AMDEP_TRUE@@am__fastdepCCAS_FALSE@ $(AM_V_CPPAS)source='libc/machine/arm/read_tp.S' object='libc/machine/arm/libc_a-read_tp.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCCAS_FALSE@ DEPDIR=$(DEPDIR) $(CCASDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCCAS_FALSE@ $(AM_V_CPPAS@am__nodep@)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -c -o libc/machine/arm/libc_a-read_tp.obj `if test -f 'libc/machine/arm/read_tp.S'; then $(CYGPATH_W) 'libc/machine/arm/read_tp.S'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/read_tp.S'; fi`
+
libc/machine/arm/libc_a-memchr.o: libc/machine/arm/memchr.S
@am__fastdepCCAS_TRUE@ $(AM_V_CPPAS)$(CCAS) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CCASFLAGS) $(CCASFLAGS) -MT libc/machine/arm/libc_a-memchr.o -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-memchr.Tpo -c -o libc/machine/arm/libc_a-memchr.o `test -f 'libc/machine/arm/memchr.S' || echo '$(srcdir)/'`libc/machine/arm/memchr.S
@am__fastdepCCAS_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-memchr.Tpo libc/machine/arm/$(DEPDIR)/libc_a-memchr.Po
@@ -28825,6 +28860,20 @@ libc/misc/libc_a-ffs.obj: libc/misc/ffs.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/misc/libc_a-ffs.obj `if test -f 'libc/misc/ffs.c'; then $(CYGPATH_W) 'libc/misc/ffs.c'; else $(CYGPATH_W) '$(srcdir)/libc/misc/ffs.c'; fi`
+libc/misc/libc_a-inittls.o: libc/misc/inittls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/misc/libc_a-inittls.o -MD -MP -MF libc/misc/$(DEPDIR)/libc_a-inittls.Tpo -c -o libc/misc/libc_a-inittls.o `test -f 'libc/misc/inittls.c' || echo '$(srcdir)/'`libc/misc/inittls.c
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/misc/$(DEPDIR)/libc_a-inittls.Tpo libc/misc/$(DEPDIR)/libc_a-inittls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/misc/inittls.c' object='libc/misc/libc_a-inittls.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/misc/libc_a-inittls.o `test -f 'libc/misc/inittls.c' || echo '$(srcdir)/'`libc/misc/inittls.c
+
+libc/misc/libc_a-inittls.obj: libc/misc/inittls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/misc/libc_a-inittls.obj -MD -MP -MF libc/misc/$(DEPDIR)/libc_a-inittls.Tpo -c -o libc/misc/libc_a-inittls.obj `if test -f 'libc/misc/inittls.c'; then $(CYGPATH_W) 'libc/misc/inittls.c'; else $(CYGPATH_W) '$(srcdir)/libc/misc/inittls.c'; fi`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/misc/$(DEPDIR)/libc_a-inittls.Tpo libc/misc/$(DEPDIR)/libc_a-inittls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/misc/inittls.c' object='libc/misc/libc_a-inittls.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/misc/libc_a-inittls.obj `if test -f 'libc/misc/inittls.c'; then $(CYGPATH_W) 'libc/misc/inittls.c'; else $(CYGPATH_W) '$(srcdir)/libc/misc/inittls.c'; fi`
+
libc/misc/libc_a-init.o: libc/misc/init.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/misc/libc_a-init.o -MD -MP -MF libc/misc/$(DEPDIR)/libc_a-init.Tpo -c -o libc/misc/libc_a-init.o `test -f 'libc/misc/init.c' || echo '$(srcdir)/'`libc/misc/init.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/misc/$(DEPDIR)/libc_a-init.Tpo libc/misc/$(DEPDIR)/libc_a-init.Po
@@ -33879,6 +33928,34 @@ libc/machine/arm/libc_a-aeabi_memclr.obj: libc/machine/arm/aeabi_memclr.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/arm/libc_a-aeabi_memclr.obj `if test -f 'libc/machine/arm/aeabi_memclr.c'; then $(CYGPATH_W) 'libc/machine/arm/aeabi_memclr.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/aeabi_memclr.c'; fi`
+libc/machine/arm/libc_a-tls.o: libc/machine/arm/tls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/arm/libc_a-tls.o -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-tls.Tpo -c -o libc/machine/arm/libc_a-tls.o `test -f 'libc/machine/arm/tls.c' || echo '$(srcdir)/'`libc/machine/arm/tls.c
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-tls.Tpo libc/machine/arm/$(DEPDIR)/libc_a-tls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/machine/arm/tls.c' object='libc/machine/arm/libc_a-tls.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/arm/libc_a-tls.o `test -f 'libc/machine/arm/tls.c' || echo '$(srcdir)/'`libc/machine/arm/tls.c
+
+libc/machine/arm/libc_a-tls.obj: libc/machine/arm/tls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/arm/libc_a-tls.obj -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-tls.Tpo -c -o libc/machine/arm/libc_a-tls.obj `if test -f 'libc/machine/arm/tls.c'; then $(CYGPATH_W) 'libc/machine/arm/tls.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/tls.c'; fi`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-tls.Tpo libc/machine/arm/$(DEPDIR)/libc_a-tls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/machine/arm/tls.c' object='libc/machine/arm/libc_a-tls.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/arm/libc_a-tls.obj `if test -f 'libc/machine/arm/tls.c'; then $(CYGPATH_W) 'libc/machine/arm/tls.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/tls.c'; fi`
+
+libc/machine/arm/libc_a-set_tls.o: libc/machine/arm/set_tls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/arm/libc_a-set_tls.o -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Tpo -c -o libc/machine/arm/libc_a-set_tls.o `test -f 'libc/machine/arm/set_tls.c' || echo '$(srcdir)/'`libc/machine/arm/set_tls.c
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Tpo libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/machine/arm/set_tls.c' object='libc/machine/arm/libc_a-set_tls.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/arm/libc_a-set_tls.o `test -f 'libc/machine/arm/set_tls.c' || echo '$(srcdir)/'`libc/machine/arm/set_tls.c
+
+libc/machine/arm/libc_a-set_tls.obj: libc/machine/arm/set_tls.c
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/arm/libc_a-set_tls.obj -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Tpo -c -o libc/machine/arm/libc_a-set_tls.obj `if test -f 'libc/machine/arm/set_tls.c'; then $(CYGPATH_W) 'libc/machine/arm/set_tls.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/set_tls.c'; fi`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Tpo libc/machine/arm/$(DEPDIR)/libc_a-set_tls.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='libc/machine/arm/set_tls.c' object='libc/machine/arm/libc_a-set_tls.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/arm/libc_a-set_tls.obj `if test -f 'libc/machine/arm/set_tls.c'; then $(CYGPATH_W) 'libc/machine/arm/set_tls.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/arm/set_tls.c'; fi`
+
libc/machine/arm/libc_a-memchr-stub.o: libc/machine/arm/memchr-stub.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/arm/libc_a-memchr-stub.o -MD -MP -MF libc/machine/arm/$(DEPDIR)/libc_a-memchr-stub.Tpo -c -o libc/machine/arm/libc_a-memchr-stub.o `test -f 'libc/machine/arm/memchr-stub.c' || echo '$(srcdir)/'`libc/machine/arm/memchr-stub.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) libc/machine/arm/$(DEPDIR)/libc_a-memchr-stub.Tpo libc/machine/arm/$(DEPDIR)/libc_a-memchr-stub.Po
diff --git a/newlib/configure b/newlib/configure
index ab31510aa..0ff110a7c 100755
--- a/newlib/configure
+++ b/newlib/configure
@@ -988,6 +988,7 @@ enable_newlib_global_atexit
enable_newlib_reent_small
enable_newlib_reent_binary_compat
enable_newlib_reent_thread_local
+enable_newlib_thread_local_storage
enable_newlib_global_stdio_streams
enable_newlib_fvwrite_in_streamio
enable_newlib_fseek_optimization
@@ -1659,6 +1660,7 @@ Optional Features:
--enable-newlib-reent-small enable small reentrant struct support
--enable-newlib-reent-binary-compat enable backward binary compatibility for struct _reent
--enable-newlib-reent-thread-local enable thread-local storage objects as a replacment for struct _reent members
+ --enable-newlib-thread-local-storage enable the TLS runtime API
--enable-newlib-global-stdio-streams enable global stdio streams
--disable-newlib-fvwrite-in-streamio disable iov in streamio
--disable-newlib-fseek-optimization disable fseek optimization
@@ -2437,6 +2439,17 @@ else
newlib_reent_thread_local=no
fi
+# Check whether --enable-newlib-thread-local-storage was given.
+if test "${enable_newlib_thread_local_storage+set}" = set; then :
+ enableval=$enable_newlib_thread_local_storage; case "${enableval}" in
+ yes) newlib_thread_local_storage=yes ;;
+ no) newlib_thread_local_storage=no ;;
+ *) as_fn_error $? "bad value ${enableval} for newlib-thread-local-storage option" "$LINENO" 5 ;;
+ esac
+else
+ newlib_thread_local_storage=no
+fi
+
# Check whether --enable-newlib-global-stdio-streams was given.
if test "${enable_newlib_global_stdio_streams+set}" = set; then :
enableval=$enable_newlib_global_stdio_streams; case "${enableval}" in
@@ -6603,6 +6616,14 @@ $as_echo "#define _WANT_REENT_THREAD_LOCAL 1" >>confdefs.h
fi
+if test "${newlib_thread_local_storage}" = "yes"; then
+
+$as_echo "#define __THREAD_LOCAL_STORAGE 1" >>confdefs.h
+
+$as_echo "#define __THREAD_LOCAL_STORAGE_API 1" >>confdefs.h
+
+fi
+
_mb_len_max=1
if test "${newlib_mb}" = "yes"; then
diff --git a/newlib/configure.ac b/newlib/configure.ac
index a4807830e..0ade683a8 100644
--- a/newlib/configure.ac
+++ b/newlib/configure.ac
@@ -187,6 +187,14 @@ AC_ARG_ENABLE(newlib-reent-thread-local,
esac
fi], [newlib_reent_thread_local=no])dnl
+AC_ARG_ENABLE(newlib-thread-local-storage,
+[ --enable-newlib-thread-local-storage enable the TLS runtime API],
+[case "${enableval}" in
+ yes) newlib_thread_local_storage=yes ;;
+ no) newlib_thread_local_storage=no ;;
+ *) AC_MSG_ERROR(bad value ${enableval} for newlib-thread-local-storage option) ;;
+ esac], [newlib_thread_local_storage=no])dnl
+
dnl Support --enable-newlib-global-stdio-streams
dnl This is no longer optional. It is enabled in all Newlib configurations.
AC_ARG_ENABLE(newlib-global-stdio-streams,
@@ -478,6 +486,11 @@ if test "${newlib_reent_thread_local}" = "yes"; then
AC_DEFINE(_WANT_REENT_THREAD_LOCAL, 1, [Define to enable thread-local storage objects as a replacment for struct _reent members.])
fi
+if test "${newlib_thread_local_storage}" = "yes"; then
+ AC_DEFINE(__THREAD_LOCAL_STORAGE, 1, [Define to enable the TLS runtime API.])
+ AC_DEFINE(__THREAD_LOCAL_STORAGE_API, 1, [Define to enable the TLS runtime API.])
+fi
+
_mb_len_max=1
if test "${newlib_mb}" = "yes"; then
AC_DEFINE(_MB_CAPABLE, 1, [Multibyte supported.])
diff --git a/newlib/libc/include/picotls.h b/newlib/libc/include/picotls.h
new file mode 100644
index 000000000..e87f0b58a
--- /dev/null
+++ b/newlib/libc/include/picotls.h
@@ -0,0 +1,76 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2019 Keith Packard
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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 _PICOTLS_H_
+#define _PICOTLS_H_
+
+#include <sys/cdefs.h>
+
+#ifdef __THREAD_LOCAL_STORAGE
+
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+extern char __tls_size[];
+
+static inline size_t
+_tls_size(void)
+{
+ return (size_t)(uintptr_t)__tls_size;
+}
+
+extern char __tls_align[];
+
+static inline size_t
+_tls_align(void)
+{
+ return (size_t)(uintptr_t)__tls_align;
+}
+
+/*
+ * Initialize a TLS block, copying the data segment from flash and
+ * zeroing the BSS segment.
+ */
+void _init_tls(void *tls);
+
+/* Set the TLS pointer to the specific block */
+void _set_tls(void *tls);
+
+__END_DECLS
+
+#endif
+
+#endif /* _PICOTLS_H_ */
diff --git a/newlib/libc/machine/arm/Makefile.inc b/newlib/libc/machine/arm/Makefile.inc
index 2d6c08d71..253e54620 100644
--- a/newlib/libc/machine/arm/Makefile.inc
+++ b/newlib/libc/machine/arm/Makefile.inc
@@ -3,6 +3,7 @@ libc_a_SOURCES += \
%D%/aeabi_memcpy.c %D%/aeabi_memcpy-armv7a.S \
%D%/aeabi_memmove.c %D%/aeabi_memmove-soft.S \
%D%/aeabi_memset.c %D%/aeabi_memset-soft.S %D%/aeabi_memclr.c \
+ %D%/tls.c %D%/set_tls.c %D%/read_tp.S \
%D%/memchr-stub.c \
%D%/memchr.S \
%D%/memcpy-stub.c \
diff --git a/newlib/libc/machine/arm/arm_tls.h b/newlib/libc/machine/arm/arm_tls.h
new file mode 100644
index 000000000..a3094f4f7
--- /dev/null
+++ b/newlib/libc/machine/arm/arm_tls.h
@@ -0,0 +1,49 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2020 Keith Packard
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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.
+ */
+
+#if ((__ARM_FEATURE_COPROC & 1) || __ARM_ARCH >= 8) && __ARM_ARCH_PROFILE != 'M' && __ARM_ARCH >= 6
+#define ARM_TLS_CP15
+#endif
+
+#define ARM_TLS_COUNT 1
+
+#ifndef _IN_ASM
+/* This needs to be global so that __aeabi_read_tp can
+ * refer to it in an asm statement
+ */
+#ifndef ARM_TLS_CP15
+extern void *__tls[ARM_TLS_COUNT];
+#endif
+#endif
diff --git a/newlib/libc/machine/arm/read_tp.S b/newlib/libc/machine/arm/read_tp.S
new file mode 100644
index 000000000..f91b0d2c8
--- /dev/null
+++ b/newlib/libc/machine/arm/read_tp.S
@@ -0,0 +1,76 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2020 Keith Packard
+ * Copyright © 2024 Stephen Street
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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.
+ */
+#include <newlib.h>
+
+#ifdef __THREAD_LOCAL_STORAGE
+
+#define _IN_ASM
+#include "arm_tls.h"
+/*
+ * This cannot be a C ABI function as the compiler assumes that it
+ * does not modify anything other than r0 and lr.
+ */
+ .syntax unified
+ .section .text.__aeabi_read_tp
+ .align 4
+ .p2align 4,,15
+ .global __aeabi_read_tp
+ .type __aeabi_read_tp,%function
+#ifdef __thumb__
+ .thumb
+#endif
+
+__aeabi_read_tp:
+ .cfi_sections .debug_frame
+ .cfi_startproc
+
+#ifdef ARM_TLS_CP15
+ mrc p15, 0, r0, c13, c0, 3
+ bx lr
+#else
+
+#define TLS_SIZE 4
+
+ ldr r0,=__tls /* Load the address of __tls */
+ ldr r0,[r0] /* Dereference to get the value of __tls */
+ bx lr /* All done, return to caller */
+
+#endif
+
+ .cfi_endproc
+
+#endif
+
diff --git a/newlib/libc/machine/arm/set_tls.c b/newlib/libc/machine/arm/set_tls.c
new file mode 100644
index 000000000..1dfbc27d0
--- /dev/null
+++ b/newlib/libc/machine/arm/set_tls.c
@@ -0,0 +1,74 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2019 Keith Packard
+ * Copyright © 2024 Stephen Street
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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.
+ */
+
+#include <picotls.h>
+#include <string.h>
+#include <stdint.h>
+
+#ifdef __THREAD_LOCAL_STORAGE_API
+
+#include "arm_tls.h"
+
+/* This needs to be global so that __aeabi_read_tp can
+ * refer to it in an asm statement
+ */
+#ifndef ARM_TLS_CP15
+extern void *__tls[];
+#endif
+
+/* The size of the thread control block.
+ * TLS relocations are generated relative to
+ * a location this far *before* the first thread
+ * variable (!)
+ * NB: The actual size before tp also includes padding
+ * to align up to the alignment of .tdata/.tbss.
+ */
+#define TCB_SIZE 8
+extern char __arm32_tls_tcb_offset;
+#define TP_OFFSET ((size_t)&__arm32_tls_tcb_offset)
+
+void
+_set_tls(void *tls)
+{
+ tls = (uint8_t *)tls - TP_OFFSET;
+#ifdef ARM_TLS_CP15
+ __asm__("mcr p15, 0, %0, cr13, cr0, 3" : : "r"(tls));
+#else
+ __tls[0] = tls;
+#endif
+}
+
+#endif
diff --git a/newlib/libc/machine/arm/tls.c b/newlib/libc/machine/arm/tls.c
new file mode 100644
index 000000000..156b9d72a
--- /dev/null
+++ b/newlib/libc/machine/arm/tls.c
@@ -0,0 +1,52 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2019 Keith Packard
+ * Copyright © 2024 Stephen Street
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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.
+ */
+
+#include <picotls.h>
+#include <string.h>
+#include <stdint.h>
+
+#ifdef __THREAD_LOCAL_STORAGE
+
+#include "arm_tls.h"
+
+/* This needs to be global so that __aeabi_read_tp can
+ * refer to it in an asm statement
+ */
+#ifndef ARM_TLS_CP15
+void *__tls[ARM_TLS_COUNT];
+#endif
+
+#endif
diff --git a/newlib/libc/misc/Makefile.inc b/newlib/libc/misc/Makefile.inc
index 78bb022dd..1abd05295 100644
--- a/newlib/libc/misc/Makefile.inc
+++ b/newlib/libc/misc/Makefile.inc
@@ -2,6 +2,7 @@ libc_a_SOURCES += \
%D%/__dprintf.c \
%D%/unctrl.c \
%D%/ffs.c \
+ %D%/inittls.c \
%D%/init.c \
%D%/fini.c
diff --git a/newlib/libc/misc/inittls.c b/newlib/libc/misc/inittls.c
new file mode 100644
index 000000000..458013fbb
--- /dev/null
+++ b/newlib/libc/misc/inittls.c
@@ -0,0 +1,77 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright © 2019 Keith Packard
+ *
+ * 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.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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.
+ */
+
+#include <picotls.h>
+#include <string.h>
+#include <stdint.h>
+
+#ifdef __THREAD_LOCAL_STORAGE_API
+
+/*
+ * The TLS block consists of initialized data immediately followed by
+ * zero filled data
+ *
+ * These addresses must be defined by the loader configuration file
+ */
+
+extern char __tdata_source[]; /* Source of TLS initialization data (in ROM) */
+
+#ifdef __PICOCRT_RUNTIME_SIZE
+extern char __tdata_start[]; /* Start of static tdata area */
+extern char __tdata_end[]; /* End of static tdata area */
+extern char __tbss_start[]; /* Start of static zero-initialized TLS data */
+extern char __tbss_end[]; /* End of static zero-initialized TLS data */
+#define __tdata_size (__tdata_end - __tdata_start)
+#define __tbss_size (__tbss_end - __tbss_start)
+#define __tbss_offset (__tbss_start - __tdata_start)
+#else
+extern char __tdata_size[]; /* Size of TLS initized data */
+extern char __tbss_size[]; /* Size of TLS zero-filled data */
+extern char __tbss_offset[]; /* Offset from tdata to tbss */
+#endif
+
+void
+_init_tls(void *__tls)
+{
+ char *tls = __tls;
+
+ /* Copy tls initialized data */
+ memcpy(tls, __tdata_source, (uintptr_t)__tdata_size);
+
+ /* Clear tls zero data */
+ memset(tls + (uintptr_t)__tbss_offset, '\0', (uintptr_t)__tbss_size);
+}
+
+#endif
diff --git a/newlib/libc/sys/arm/crt0.S b/newlib/libc/sys/arm/crt0.S
index 51e86d549..ea2122d93 100644
--- a/newlib/libc/sys/arm/crt0.S
+++ b/newlib/libc/sys/arm/crt0.S
@@ -375,6 +375,15 @@ __change_mode:
#endif
bl FUNCTION (memset)
+
+#ifdef __THREAD_LOCAL_STORAGE
+ /* Initialize TLS in local-exec mode. */
+ ldr a1, .LCtls_base /* First arg: TLS block. */
+ bl FUNCTION (_init_tls)
+ ldr a1, .LCtls_base /* First arg: TLS block. */
+ bl FUNCTION (_set_tls)
+#endif
+
#if !defined (ARM_RDP_MONITOR) && !defined (ARM_RDI_MONITOR)
/* Changes by toralf: Taken from libgloss/m68k/crt0.S
initialize target specific stuff. Only execute these
@@ -598,6 +607,10 @@ change_back:
.word __bss_start__
.LC2:
.word __bss_end__
+#ifdef __THREAD_LOCAL_STORAGE
+.LCtls_base:
+ .word __tls_base
+#endif
#ifdef __USES_INITFINI__
#ifdef _LITE_EXIT
.Latexit:
diff --git a/newlib/newlib.hin b/newlib/newlib.hin
index 52cd500db..350762f2f 100644
--- a/newlib/newlib.hin
+++ b/newlib/newlib.hin
@@ -420,6 +420,12 @@
_reent members. */
#undef _WANT_REENT_THREAD_LOCAL
+/* Define to enable the TLS runtime API. */
+#undef __THREAD_LOCAL_STORAGE
+
+/* Define to enable the TLS runtime API. */
+#undef __THREAD_LOCAL_STORAGE_API
+
/* Register application finalization function using atexit. */
#undef _WANT_REGISTER_FINI
--
2.43.0
More information about the Newlib
mailing list