From 4eaa268f334ee1d240d06340231211f17edef618 Mon Sep 17 00:00:00 2001 From: Alex Langford Date: Mon, 4 Aug 2025 11:44:17 -0700 Subject: [PATCH] [lldb] Fix TLS support on Darwin platforms (#151601) When I wrote this previously, I was unaware that the TLS function already adds the offset. The test was working previously because the offset was 0 in this case (only 1 thread-local variable). I added another thread-local variable to the test to make sure the offset is indeed handled correctly. rdar://156547548 (cherry picked from commit a27d34b3f22b1134b2d47590c25b7f81eb7710b2) --- .../DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp | 7 +++---- lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py | 10 ++++++++++ lldb/test/API/lang/c/tls_globals/main.c | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp index 0b89b43b7474b..b41843fb7f2b3 100644 --- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp +++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp @@ -1173,9 +1173,8 @@ DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp, // size_t offset; // } // - // The strategy is to take get_addr, call it with the address of the - // containing TLS_Thunk structure, and add the offset to the resulting - // pointer to get the data block. + // The strategy is to take get_addr and call it with the address of the + // containing TLS_Thunk structure. // // On older apple platforms, the key is treated as a pthread_key_t and passed // to pthread_getspecific. The pointer returned from that call is added to @@ -1204,7 +1203,7 @@ DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp, const addr_t tls_data = evaluate_tls_address( thunk_load_addr, llvm::ArrayRef(tls_load_addr)); if (tls_data != LLDB_INVALID_ADDRESS) - return tls_data + tls_offset; + return tls_data; } } diff --git a/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py b/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py index 2bffd2eea123a..7e0340c9b97dc 100644 --- a/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py +++ b/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py @@ -73,6 +73,11 @@ def test(self): VARIABLES_DISPLAYED_CORRECTLY, patterns=["\(int\) \$.* = 88"], ) + self.expect( + "expr var_static2", + VARIABLES_DISPLAYED_CORRECTLY, + patterns=[r"\(int\) \$.* = 66"], + ) self.expect( "expr var_shared", VARIABLES_DISPLAYED_CORRECTLY, @@ -104,6 +109,11 @@ def test(self): VARIABLES_DISPLAYED_CORRECTLY, patterns=["\(int\) \$.* = 44"], ) + self.expect( + "expr var_static2", + VARIABLES_DISPLAYED_CORRECTLY, + patterns=[r"\(int\) \$.* = 22"], + ) self.expect( "expr var_shared", VARIABLES_DISPLAYED_CORRECTLY, diff --git a/lldb/test/API/lang/c/tls_globals/main.c b/lldb/test/API/lang/c/tls_globals/main.c index bdfd78c1ac34b..fac760b350ab2 100644 --- a/lldb/test/API/lang/c/tls_globals/main.c +++ b/lldb/test/API/lang/c/tls_globals/main.c @@ -10,10 +10,12 @@ touch_shared(); // Create some TLS storage within the static executable. __thread int var_static = 44; +__thread int var_static2 = 22; void *fn_static(void *param) { var_static *= 2; + var_static2 *= 3; shared_check(); usleep(1); // thread breakpoint for(;;)