Skip to content

Commit 01e9dbf

Browse files
am11jkotas
andauthored
Disable TLS optimization for musl riscv64 (#121662)
On linux-musl-riscv64, when corehost (dotnet) dlopen()s libcoreclr, we get: > \# dotnet --version > Failed to load /root/.dotnet/shared/Microsoft.NETCore.App/10.0.0-rtm.25564.199/libcoreclr.so, error: Error relocating /root/.dotnet/shared/Microsoft.NETCore.App/10.0.0-rtm.25564.199/libcoreclr.so: Uq???&?J?N?R?V?Z? .????: initial-exec TLS resolves to dynamic definition in /root/.dotnet/shared/Microsoft.NETCore.App/10.0.0-rtm.25564.199/libcoreclr.so Failed to bind to CoreCLR at '/root/.dotnet/shared/Microsoft.NETCore.App/10.0.0-rtm.25564.199/' Failed to create CoreCLR, HRESULT: 0x80008088 musl maintainer described the issue on this golang thread: golang/go#54805 (comment). When I tried `LD_PRELOAD=/root/.dotnet/shared/Microsoft.NETCore.App/10.0.0-rtm.25564.199/libcoreclr.so dotnet --version`, it started working and SDK started working. By disabling the optimization as we have done for linux-musl-arm64 and removing `la.tls.ie` from binary works without the `LD_PRELOAD` hack. --------- Co-authored-by: Jan Kotas <[email protected]>
1 parent 776e86a commit 01e9dbf

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/coreclr/vm/riscv64/asmhelpers.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,14 @@ LEAF_END JIT_PatchpointForced, _TEXT
848848
// size_t GetThreadStaticsVariableOffset()
849849

850850
// Load offset of native thread local variable `t_ThreadStatics` in TCB and return it in `a0` register.
851+
#if !defined(TARGET_LINUX_MUSL)
852+
// Uses initial-exec TLS model which is faster but incompatible with musl's dynamic loading.
853+
// On musl, this function is not compiled and the optimization is disabled in threadstatics.cpp.
851854
LEAF_ENTRY GetThreadStaticsVariableOffset, _TEXT
852855
la.tls.ie a0, t_ThreadStatics
853856
EPILOG_RETURN
854857
LEAF_END GetThreadStaticsVariableOffset, _TEXT
858+
#endif // !TARGET_LINUX_MUSL
855859

856860
LEAF_ENTRY JIT_PollGC, _TEXT
857861
PREPARE_EXTERNAL_VAR g_TrapReturningThreads, t0

src/coreclr/vm/threadstatics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ bool CanJITOptimizeTLSAccess()
917917
// Optimization is disabled for linux/x86
918918
#elif defined(TARGET_LINUX_MUSL) && defined(TARGET_ARM64)
919919
// Optimization is disabled for linux musl arm64
920+
#elif defined(TARGET_LINUX_MUSL) && defined(TARGET_RISCV64)
921+
// Optimization is disabled for linux musl riscv64
920922
#elif defined(TARGET_FREEBSD) && defined(TARGET_ARM64)
921923
// Optimization is disabled for FreeBSD/arm64
922924
#elif defined(TARGET_ANDROID)
@@ -1096,7 +1098,10 @@ void GetThreadLocalStaticBlocksInfo(CORINFO_THREAD_STATIC_BLOCKS_INFO* pInfo)
10961098
// For Linux arm64/loongarch64/riscv64, just get the offset of thread static variable, and during execution,
10971099
// this offset, arm64 taken from trpid_elp0 system register gives back the thread variable address.
10981100
// this offset, loongarch64 taken from $tp register gives back the thread variable address.
1101+
#if !(defined(TARGET_LINUX_MUSL) && defined(TARGET_RISCV64))
1102+
// On musl riscv64, this optimization is disabled due to initial-exec TLS incompatibility.
10991103
threadStaticBaseOffset = GetThreadStaticsVariableOffset();
1104+
#endif
11001105

11011106
#else
11021107
_ASSERTE_MSG(false, "Unsupported scenario of optimizing TLS access on Linux Arm32/x86 and Android");

0 commit comments

Comments
 (0)