The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=0e3dbc64d9f6c95cbb16dba60a32136ae116dada
commit 0e3dbc64d9f6c95cbb16dba60a32136ae116dada Author: Jessica Clarke <jrt...@freebsd.org> AuthorDate: 2025-05-28 20:23:10 +0000 Commit: Jessica Clarke <jrt...@freebsd.org> CommitDate: 2025-05-28 20:23:10 +0000 libc/riscv: Fix initial exec TLS mode for dynamically loaded shared objects The offset here is relative to the TCB, not whatever the thread pointer points to, so as with powerpc and powerpc64 we need to account for that. However, rather than using hard-coded offsets as they did, due to predating machine/tls.h, we can just re-use _tcb_get(). Note that if libthr is used, and its initialiser has been called, it will take a different path that uses _get_static_tls_base, which works just fine on riscv (adding the offset to thr->tcb). This only affects programs that aren't linked against libthr (or that are but manage to dlopen before the initialiser is called, if that's even possible). In future this code should be made MI by just reusing _tcb_get() and checking the TLS variant (since the offset here is positive even for variant II, where it should be subtracted), but this is a targeted fix that makes it clear what's changing. Reviewed by: kib Fixes: 5d00c5a6571c ("Fix initial exec TLS mode for dynamically loaded shared objects.") MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D50564 --- lib/libc/riscv/static_tls.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/libc/riscv/static_tls.h b/lib/libc/riscv/static_tls.h index ce9fa23338a6..40e9abd685e3 100644 --- a/lib/libc/riscv/static_tls.h +++ b/lib/libc/riscv/static_tls.h @@ -31,12 +31,14 @@ #ifndef _LIBC_RISCV_STATIC_TLS_H #define _LIBC_RISCV_STATIC_TLS_H +#include <machine/tls.h> + static __inline uintptr_t _libc_get_static_tls_base(size_t offset) { uintptr_t tlsbase; - __asm __volatile("mv %0, tp" : "=r"(tlsbase)); + tlsbase = (uintptr_t)_tcb_get(); tlsbase += offset; return (tlsbase); }