Skip to content

Commit 7deb52a

Browse files
committed
Fix GH-11188: Error when building TSRM in ARM64
Although the issue mentioned FreeBSD, this is a broader problem: the current ARM64 code to load the TLS offset assumes a setup with the non-default TLS model. We have to call into the TLS descriptor similar as how the C compiler does it. Although it looks like a lot of inline assembly, the compiler actually optimizes this into only a few instructions on my emulator.
1 parent ad747d9 commit 7deb52a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

TSRM/TSRM.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,16 @@ TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void)
784784
asm("adrp %0, #__tsrm_ls_cache@TLVPPAGE\n\t"
785785
"ldr %0, [%0, #__tsrm_ls_cache@TLVPPAGEOFF]"
786786
: "=r" (ret));
787+
# elif defined(TSRM_TLS_MODEL_DEFAULT)
788+
asm("stp x29, x30, [sp, #-16]!\n\t"
789+
"adrp x0, #:tlsdesc:_tsrm_ls_cache\n\t"
790+
"ldr x8, [x0, #:tlsdesc_lo12:_tsrm_ls_cache]\n\t"
791+
"add x0, x0, #:tlsdesc_lo12:_tsrm_ls_cache\n\t"
792+
".tlsdesccall _tsrm_ls_cache\n\t"
793+
"blr x8\n\t"
794+
"mov %0, x0\n\t"
795+
"ldp x29, x30, [sp], #16"
796+
: "=r" (ret) : : "x0", "x8");
787797
# else
788798
asm("mov %0, xzr\n\t"
789799
"add %0, %0, #:tprel_hi12:_tsrm_ls_cache, lsl #12\n\t"

TSRM/TSRM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ TSRM_API const char *tsrm_api_name(void);
149149

150150
#if !__has_attribute(tls_model) || defined(__FreeBSD__) || defined(__MUSL__) || defined(__HAIKU__)
151151
# define TSRM_TLS_MODEL_ATTR
152+
# define TSRM_TLS_MODEL_DEFAULT
152153
#elif __PIC__
153154
# define TSRM_TLS_MODEL_ATTR __attribute__((tls_model("initial-exec")))
154155
#else

0 commit comments

Comments
 (0)