<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/71666>71666</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
lldb on AArch64 Linux cannot read thread local variables
</td>
</tr>
<tr>
<th>Labels</th>
<td>
lldb
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
DavidSpickett
</td>
</tr>
</table>
<pre>
Since https://github.com/llvm/llvm-project/commit/e8ea47602bdb74a88c707d289fc241f7670e1483 we have support for TLS on x86 Linux, but this does not work on AArch64.
For AArch64 we do now have the `tpidr` register accessible, which is one piece of the puzzle.
The other part is that the compiler does not emit any TLS hint in the DWARF. Example code:
```
#include <pthread.h>
__thread int tls1=10;
void *start(void* ptr) {
int a=10+tls1;
return NULL;
}
int main() {
pthread_t t1;
pthread_create(&t1, NULL, start, NULL);
void* r;
pthread_join(t1, &r);
return 0;
}
```
Compiled with clang for AArch64 we get this description:
```
0x00000023: DW_TAG_variable
DW_AT_name ("tls1")
DW_AT_type (0x0000002b "int")
DW_AT_external (true)
DW_AT_decl_file ("/tmp/test.c")
DW_AT_decl_line (3)
```
For x86 you get:
```
0x000000e6: DW_TAG_variable
DW_AT_name ("tls1")
DW_AT_decl_file ("/tmp/test.c")
DW_AT_decl_line (3)
DW_AT_decl_column (0x0e)
DW_AT_type (0x00000057 "int")
DW_AT_external (true)
DW_AT_location (DW_OP_const8u 0x0, DW_OP_GNU_push_tls_address)
```
Arm has not decided on a way to represent TLS variables in this same way (see `AArch64_ELFTargetObjectFile::Initialize`).
I briefly tried flipping that boolean so AArch64 did emit it, then using tpidr as the thread pointer. However the existing calculations do not work and I don't have time right now to see what the data structures are supposed to be.
My hacks aside, a vanilla LLDB doesn't see the variable at all:
```
(lldb) p tls1
error: <user expression 0>:1:1: use of undeclared identifier 'tls1'
1 | tls1
| ^
```
GDB can read it:
```
(gdb) p tls1
$1 = 10
```
Apparently GDB is using `thread_db` from glibc to work this out, but I don't have the details.
We would I think have to:
* Mark tpidr as the thread pointer register.
* Pass this information to glibc somehow, using the symbol's address as the offset.
* Read the resulting location.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl9v4zgO_zTKC3GBLad28pCHtJnMDdD9g50u-hjIEh1rK0uGRDfJfPqDZKdJ56YY3GHXaJNYIkVSJH8_ihD0wSKu2d09u9vOxECt8-uteNXqa6_lCxLNaqfO66_aSoSWqA-s2DC-Y3x30NQO9Vy6jvGdMa-Xr3_13v2FkhjfSdd1Ov7AJYpFVWa8VnW1EMulrLJK8eWqkXyRN1VZZZgvlgUcEVrxihCGvneeoHEenh6_grNwWpbwqO1wYvwB6oGAWh1AOQxgHcHR-Zcottl42ZaLOcu2LNuMnzvnL-vRgnJg3XE0RC0CKzPqtfKszMDjQQdCD0JKDEHXBqO9Y6tlCzqAswi9RongmqTcD9--GXxn7qlFcNSih154ilrUCkrS0nW9NuivfmOnCYQ9pzBbbQm0TaLb580fuzl8OomuN1FTYbz70U6ZTX_jKy-0lWZQCKx46Kn1KNS8ZcWnW7f2-3EDohEyIWfFNs9YcT9uvzqtgPFNIOGJ8WV8Z3wDPXnGV8CqSQ6SvhiV-f140NueRxq8hV__fHx8W2XV9taPqN4JbRlffnfw5PmegG7OvKxKj4IwaZWUx6wkK_wBJpcvC6sbfy5R-Ju1y4F_ueTEeBbjpX-vOoWS_SCO99f_MCZVwVFTC9IIe0iFe1NyB7zUKwbpdU_a2Y-ymZ2y9PCCFRsA2D7vnzaf96_CaxHLcXRv-7zfPO2t6BDSk-6Fp2xwHgOZonj_jFp07q9ab-ZqYJxrSz_XxxOht8IkffIDXuVHAYXS7BttcHKL8R11ffzEQHP5cwvpAKNtOqB4E_7upmJfR1g4uyHe8M8uFMt_6kL_qXhvd6UzQ2fhNm_4_-T5rvr78mycFLGUk8D2ef_b73vpbKDlANkpi201Ln7-9c99P4R2TybshVIeQ_goqRvfQStGcFQotUIVcV3AUZyBHH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