Skip to content

Commit 068e554

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. This problem can also apply on some configurations on other platforms. For the default mode, 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. Similarly, this patch adds support for other TLS models too.
1 parent ad747d9 commit 068e554

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

TSRM/TSRM.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,27 @@ 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-
# else
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");
797+
# elif defined(TSRM_TLS_MODEL_INITIAL_EXEC)
798+
asm("adrp %0, :gottprel:_tsrm_ls_cache\n\t"
799+
"ldr %0, [%0, #:gottprel_lo12:_tsrm_ls_cache]"
800+
: "=r" (ret));
801+
# elif defined(TSRM_TLS_MODEL_LOCAL_EXEC)
788802
asm("mov %0, xzr\n\t"
789803
"add %0, %0, #:tprel_hi12:_tsrm_ls_cache, lsl #12\n\t"
790804
"add %0, %0, #:tprel_lo12_nc:_tsrm_ls_cache"
791805
: "=r" (ret));
806+
# else
807+
# error "TSRM TLS model not set"
792808
# endif
793809
return ret;
794810
#else

TSRM/TSRM.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ 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")))
155+
# define TSRM_TLS_MODEL_INITIAL_EXEC
154156
#else
155157
# define TSRM_TLS_MODEL_ATTR __attribute__((tls_model("local-exec")))
158+
# define TSRM_TLS_MODEL_LOCAL_EXEC
156159
#endif
157160

158161
#define TSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)

0 commit comments

Comments
 (0)