1 /* Locate TLS data for a thread.
2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include "thread_dbP.h"
22 /* Get the DTV slotinfo list head entry from the dynamic loader state
25 dtv_slotinfo_list (td_thragent_t
*ta
,
31 if (__td_ta_rtld_global (ta
))
33 err
= DB_GET_FIELD (head
, ta
, ta
->ta_addr__rtld_global
,
34 rtld_global
, _dl_tls_dtv_slotinfo_list
, 0);
40 if (ta
->ta_addr__dl_tls_dtv_slotinfo_list
== 0
41 && td_mod_lookup (ta
->ph
, NULL
, SYM__dl_tls_dtv_slotinfo_list
,
42 &ta
->ta_addr__dl_tls_dtv_slotinfo_list
) != PS_OK
)
45 err
= _td_fetch_value (ta
, ta
->ta_var__dl_tls_dtv_slotinfo_list
,
46 SYM_DESC__dl_tls_dtv_slotinfo_list
,
47 0, ta
->ta_addr__dl_tls_dtv_slotinfo_list
, &head
);
56 /* Get the address of the DTV slotinfo entry for MODID into
59 dtv_slotinfo (td_thragent_t
*ta
,
60 unsigned long int modid
,
61 psaddr_t
*dtvslotinfo
)
67 err
= dtv_slotinfo_list (ta
, &slot
);
73 /* Get the number of entries in this list entry's array. */
74 err
= DB_GET_FIELD (temp
, ta
, slot
, dtv_slotinfo_list
, len
, 0);
77 size_t len
= (uintptr_t)temp
;
79 /* Did we find the list entry for modid? */
80 if (modid
< slbase
+ len
)
83 /* We didn't, so get the next list entry. */
85 err
= DB_GET_FIELD (temp
, ta
, slot
, dtv_slotinfo_list
,
92 /* We reached the end of the list and found nothing. */
96 /* Take the slotinfo for modid from the list entry. */
97 err
= DB_GET_FIELD_ADDRESS (temp
, ta
, slot
, dtv_slotinfo_list
,
98 slotinfo
, modid
- slbase
);
107 /* Return in *BASE the base address of the TLS block for MODID within
110 It should return success and yield the correct pointer in any
111 circumstance where the TLS block for the module and thread
112 requested has already been initialized.
114 It should fail with TD_TLSDEFER only when the thread could not
115 possibly have observed any values in that TLS block. That way, the
116 debugger can fall back to showing initial values from the PT_TLS
117 segment (and refusing attempts to mutate) for the TD_TLSDEFER case,
118 and never fail to make the values the program will actually see
119 available to the user of the debugger. */
121 td_thr_tlsbase (const td_thrhandle_t
*th
,
122 unsigned long int modid
,
126 psaddr_t dtv
, dtvslot
, dtvptr
, temp
;
131 psaddr_t pd
= th
->th_unique
;
134 /* This is the fake handle for the main thread before libpthread
135 initialization. We are using 0 for its th_unique because we can't
136 trust that its thread register has been initialized. But we need
137 a real pointer to have any TLS access work. In case of dlopen'd
138 libpthread, initialization might not be for quite some time. So
139 try looking up the thread register now. Worst case, it's nonzero
140 uninitialized garbage and we get bogus results for TLS access
141 attempted too early. Tough. */
143 td_thrhandle_t main_th
;
144 err
= __td_ta_lookup_th_unique (th
->th_ta_p
, ps_getpid (th
->th_ta_p
->ph
),
147 pd
= main_th
.th_unique
;
152 err
= dtv_slotinfo (th
->th_ta_p
, modid
, &temp
);
157 err
= DB_GET_STRUCT (slot
, th
->th_ta_p
, temp
, dtv_slotinfo
);
161 /* Take the link_map from the slotinfo. */
163 err
= DB_GET_FIELD_LOCAL (map
, th
->th_ta_p
, slot
, dtv_slotinfo
, map
, 0);
169 /* Ok, the modid is good, now find out what DTV generation it
171 err
= DB_GET_FIELD_LOCAL (temp
, th
->th_ta_p
, slot
, dtv_slotinfo
, gen
, 0);
174 size_t modgen
= (uintptr_t)temp
;
176 /* Get the DTV pointer from the thread descriptor. */
177 err
= DB_GET_FIELD (dtv
, th
->th_ta_p
, pd
, pthread
, dtvp
, 0);
182 /* Get the DTV generation count at dtv[0].counter. */
183 err
= DB_GET_FIELD_ADDRESS (dtvgenloc
, th
->th_ta_p
, dtv
, dtv
, dtv
, 0);
186 err
= DB_GET_FIELD (temp
, th
->th_ta_p
, dtvgenloc
, dtv_t
, counter
, 0);
189 size_t dtvgen
= (uintptr_t)temp
;
191 /* Is the DTV current enough? */
195 /* If the module uses Static TLS, we're still good. */
196 err
= DB_GET_FIELD (temp
, th
->th_ta_p
, map
, link_map
, l_tls_offset
, 0);
199 ptrdiff_t tlsoff
= (uintptr_t)temp
;
201 if (tlsoff
!= FORCED_DYNAMIC_TLS_OFFSET
202 && tlsoff
!= NO_TLS_OFFSET
)
207 dtvptr
= tp
- tlsoff
;
209 dtvptr
= tp
+ tlsoff
+ TLS_PRE_TCB_SIZE
;
211 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
221 /* Find the corresponding entry in the DTV. */
222 err
= DB_GET_FIELD_ADDRESS (dtvslot
, th
->th_ta_p
, dtv
, dtv
, dtv
, modid
);
226 /* Extract the TLS block address from that DTV slot. */
227 err
= DB_GET_FIELD (dtvptr
, th
->th_ta_p
, dtvslot
, dtv_t
, pointer_val
, 0);
231 /* It could be that the memory for this module is not allocated for
233 if ((uintptr_t) dtvptr
& 1)