From 79862f24dae69c122381dc0dc1cfd55474b5a75d Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Tue, 28 May 2024 14:20:17 +0200 Subject: [PATCH] Fix TLS access in JIT on FreeBSD/amd64 DTV elements are 8 bytes in size a per ABI [1], and the index is offset by 1 on FreeBSD [2] [1] http://people.redhat.com/drepper/tls.pdf [2] https://github.com/freebsd/freebsd-src/blob/bf56e8b9c8639ac4447d223b83cdc128107cc3cd/libexec/rtld-elf/rtld.c#L5260 Closes GH-13928 --- NEWS | 1 + ext/opcache/jit/zend_jit_x86.dasc | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/NEWS b/NEWS index 0d787f184e4de..d7838aa7f4c0c 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,7 @@ PHP NEWS - Opcache: . Fixed bug GH-14267 (opcache.jit=off does not allow enabling JIT at runtime). (ilutov) + . Fixed TLS access in JIT on FreeBSD/amd64. (Arnaud) - Soap: . Fixed bug #47925 (PHPClient can't decompress response). (nielsdos) diff --git a/ext/opcache/jit/zend_jit_x86.dasc b/ext/opcache/jit/zend_jit_x86.dasc index d083bf47c76d9..7b28b2a767a82 100644 --- a/ext/opcache/jit/zend_jit_x86.dasc +++ b/ext/opcache/jit/zend_jit_x86.dasc @@ -2910,6 +2910,15 @@ static int zend_jit_setup(void) : "=a" (ti)); tsrm_tls_offset = ti[1]; tsrm_tls_index = ti[0] * 8; +#elif defined(__FreeBSD__) + size_t *ti; + + __asm__( + "leaq _tsrm_ls_cache@tlsgd(%%rip), %0\n" + : "=a" (ti)); + tsrm_tls_offset = ti[1]; + /* Index is offset by 1 on FreeBSD (https://github.com/freebsd/freebsd-src/blob/bf56e8b9c8639ac4447d223b83cdc128107cc3cd/libexec/rtld-elf/rtld.c#L5260) */ + tsrm_tls_index = (ti[0] + 1) * 8; #else size_t *ti;