[php-src] PR #18939: Fix linker failure when building opcache statically
| From: | arnaud-lb | Date: | Wed, 25 Jun 2025 09:14:00 +0000 |
| Subject: | [php-src] PR #18939: Fix linker failure when building opcache statically | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32203@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18939
Author: arnaud-lb
Spun of https://github.com/php/php-src/pull/18660
RFC: https://wiki.php.net/rfc/make_opcache_required
We use linker relocations to fetch the TLS index and offset of
_tsrm_ls_cache. When
building Opcache statically, linkers may attempt to optimize that into a more efficient code
sequence (relaxing from "General Dynamic" to "Local Exec" model [1]).
Unfortunately, linkers will fail, rather than ignore our relocations, when they don't recognize
the exact code sequence they are expecting.
This results in errors as reported by https://github.com/php/php-src/issues/15074:
TLS transition from R_X86_64_TLSGD to R_X86_64_GOTTPOFF against
_tsrm_ls_cache' at 0x12fc3 in section .text' failed"
Here I take a different approach:
* Emit the exact full code sequence expected by linkers
* Extract the TLS index/offset by inspecting the linked ASM code, rather than executing it
(execution would give us the thread-local address).
* We detect when the code was relaxed, in which case we can extract the TCB offset instead.
* This is done in a conservative way so that if the linker did something we didn't expect, we
fallback to a safer (but slower) mechanism.
One benefit of that is we are now able to use the Local Exec model in more cases, in JIT'ed
code.
This is tested on Linux (glibc, musl), FreeBSD, MacOS; x86, x86_64, aarch64 (not MacOS/Apple
Silicon, as JIT+ZTS is not supported on this combo yet), with various combinations of
static/shared/dl(). The PR includes these tests. Other OSes fallback to the slower mechanism.
[1] https://www.akkadia.org/drepper/tls.pdf