Skip to content

Commit 89c4e4c

Browse files
authored
Fix GH-11188: Error when building TSRM in ARM64 (for IR JIT) (#14459)
This is GH-11236 for IR JIT.
1 parent cb8a744 commit 89c4e4c

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

TSRM/TSRM.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,20 @@ TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void)
800800
asm("adrp %0, #__tsrm_ls_cache@TLVPPAGE\n\t"
801801
"ldr %0, [%0, #__tsrm_ls_cache@TLVPPAGEOFF]"
802802
: "=r" (ret));
803-
# else
803+
# elif defined(TSRM_TLS_MODEL_DEFAULT)
804+
/* Surplus Static TLS space isn't guaranteed. */
805+
ret = 0;
806+
# elif defined(TSRM_TLS_MODEL_INITIAL_EXEC)
807+
asm("adrp %0, :gottprel:_tsrm_ls_cache\n\t"
808+
"ldr %0, [%0, #:gottprel_lo12:_tsrm_ls_cache]"
809+
: "=r" (ret));
810+
# elif defined(TSRM_TLS_MODEL_LOCAL_EXEC)
804811
asm("mov %0, xzr\n\t"
805812
"add %0, %0, #:tprel_hi12:_tsrm_ls_cache, lsl #12\n\t"
806813
"add %0, %0, #:tprel_lo12_nc:_tsrm_ls_cache"
807814
: "=r" (ret));
815+
# else
816+
# error "TSRM TLS model not set"
808817
# endif
809818
return ret;
810819
#else

TSRM/TSRM.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,13 @@ TSRM_API bool tsrm_is_managed_thread(void);
154154

155155
#if !__has_attribute(tls_model) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__MUSL__) || defined(__HAIKU__)
156156
# define TSRM_TLS_MODEL_ATTR
157+
# define TSRM_TLS_MODEL_DEFAULT
157158
#elif __PIC__
158159
# define TSRM_TLS_MODEL_ATTR __attribute__((tls_model("initial-exec")))
160+
# define TSRM_TLS_MODEL_INITIAL_EXEC
159161
#else
160162
# define TSRM_TLS_MODEL_ATTR __attribute__((tls_model("local-exec")))
163+
# define TSRM_TLS_MODEL_LOCAL_EXEC
161164
#endif
162165

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

ext/opcache/jit/zend_jit_ir.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,15 @@ static void* zend_jit_stub_handlers[sizeof(zend_jit_stubs) / sizeof(zend_jit_stu
352352

353353
#if defined(IR_TARGET_AARCH64)
354354

355+
# ifdef __FreeBSD__
356+
/* https://github.com/freebsd/freebsd-src/blob/c52ca7dd09066648b1cc40f758289404d68ab886/libexec/rtld-elf/aarch64/reloc.c#L180-L184 */
357+
typedef struct TLSDescriptor {
358+
void* thunk;
359+
int index;
360+
size_t offset;
361+
} TLSDescriptor;
362+
# endif
363+
355364
#define IR_HAS_VENEERS (1U<<31) /* IR_RESERVED_FLAG_1 */
356365

357366
static const void *zend_jit_get_veneer(ir_ctx *ctx, const void *addr)
@@ -3185,7 +3194,28 @@ static void zend_jit_setup(void)
31853194
#ifdef ZTS
31863195
#if defined(IR_TARGET_AARCH64)
31873196
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
3197+
3198+
# ifdef __FreeBSD__
3199+
if (tsrm_ls_cache_tcb_offset == 0) {
3200+
TLSDescriptor **where;
3201+
3202+
__asm__(
3203+
"adrp %0, :tlsdesc:_tsrm_ls_cache\n"
3204+
"add %0, %0, :tlsdesc_lo12:_tsrm_ls_cache\n"
3205+
: "=r" (where));
3206+
/* See https://github.com/ARM-software/abi-aa/blob/2a70c42d62e9c3eb5887fa50b71257f20daca6f9/aaelf64/aaelf64.rst
3207+
* section "Relocations for thread-local storage".
3208+
* The first entry holds a pointer to the variable's TLS descriptor resolver function and the second entry holds
3209+
* a platform-specific offset or pointer. */
3210+
TLSDescriptor *tlsdesc = where[1];
3211+
3212+
tsrm_tls_offset = tlsdesc->offset;
3213+
/* Index is offset by 1 on FreeBSD (https://github.com/freebsd/freebsd-src/blob/22ca6db50f4e6bd75a141f57cf953d8de6531a06/lib/libc/gen/tls.c#L88) */
3214+
tsrm_tls_index = (tlsdesc->index + 1) * 8;
3215+
}
3216+
# else
31883217
ZEND_ASSERT(tsrm_ls_cache_tcb_offset != 0);
3218+
# endif
31893219
# elif defined(_WIN64)
31903220
tsrm_tls_index = _tls_index * sizeof(void*);
31913221

0 commit comments

Comments
 (0)