@@ -110,13 +110,22 @@ static void* dasm_labels[zend_lb_MAX];
110
110
111
111
#if ZTS
112
112
static size_t tsrm_ls_cache_tcb_offset = 0;
113
+ static size_t tsrm_tls_index = 0;
114
+ static size_t tsrm_tls_offset = 0;
113
115
# ifdef __APPLE__
114
116
struct TLVDescriptor {
115
117
void* (*thunk)(struct TLVDescriptor*);
116
118
uint64_t key;
117
119
uint64_t offset;
118
120
};
119
121
typedef struct TLVDescriptor TLVDescriptor;
122
+ # elif defined(__FreeBSD__)
123
+ /* https://github.com/freebsd/freebsd-src/blob/c52ca7dd09066648b1cc40f758289404d68ab886/libexec/rtld-elf/aarch64/reloc.c#L180-L184 */
124
+ typedef struct TLSDescriptor {
125
+ void* thunk;
126
+ int index;
127
+ size_t offset;
128
+ } TLSDescriptor;
120
129
# endif
121
130
#endif
122
131
@@ -494,8 +503,14 @@ static bool logical_immediate_p(uint64_t value, uint32_t reg_size)
494
503
| MEM_ACCESS_64_WITH_UOFFSET_64 ldr, reg, TMP3, (((TLVDescriptor*)tsrm_ls_cache_tcb_offset)->offset), TMP1
495
504
||#else
496
505
| .long 0xd53bd051 // TODO: hard-coded: mrs TMP3, tpidr_el0
497
- || ZEND_ASSERT(tsrm_ls_cache_tcb_offset <= LDR_STR_PIMM64);
498
- | ldr reg, [TMP3, #tsrm_ls_cache_tcb_offset]
506
+ || if (tsrm_ls_cache_tcb_offset == 0) {
507
+ | ldr TMP3, [TMP3, #0]
508
+ | MEM_ACCESS_64_WITH_UOFFSET_64 ldr, TMP3, TMP3, tsrm_tls_index, TMP1
509
+ | MEM_ACCESS_64_WITH_UOFFSET_64 ldr, reg, TMP3, tsrm_tls_offset, TMP1
510
+ || } else {
511
+ || ZEND_ASSERT(tsrm_ls_cache_tcb_offset <= LDR_STR_PIMM64);
512
+ | ldr reg, [TMP3, #tsrm_ls_cache_tcb_offset]
513
+ || }
499
514
||#endif
500
515
|.endmacro
501
516
@@ -2754,7 +2769,24 @@ static int zend_jit_setup(void)
2754
2769
2755
2770
#if ZTS
2756
2771
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
2772
+ # if defined(__FreeBSD__)
2773
+ if (tsrm_ls_cache_tcb_offset == 0) {
2774
+ TLSDescriptor **where;
2775
+
2776
+ __asm__(
2777
+ "adrp %0, :tlsdesc:_tsrm_ls_cache\n"
2778
+ "add %0, %0, :tlsdesc_lo12:_tsrm_ls_cache\n"
2779
+ : "=r" (where));
2780
+ /* https://github.com/freebsd/freebsd-src/blob/c52ca7dd09066648b1cc40f758289404d68ab886/libexec/rtld-elf/aarch64/reloc.c#L225-L233 */
2781
+ TLSDescriptor *tlsdesc = where[1];
2782
+
2783
+ tsrm_tls_offset = tlsdesc->offset;
2784
+ /* Index is offset by 1 on FreeBSD (https://github.com/freebsd/freebsd-src/blob/22ca6db50f4e6bd75a141f57cf953d8de6531a06/lib/libc/gen/tls.c#L88) */
2785
+ tsrm_tls_index = (tlsdesc->index + 1) * 8;
2786
+ }
2787
+ # else
2757
2788
ZEND_ASSERT(tsrm_ls_cache_tcb_offset != 0);
2789
+ # endif
2758
2790
#endif
2759
2791
2760
2792
memset(sp_adj, 0, sizeof(sp_adj));
0 commit comments