1 /* Locate TLS data for a thread.
2 Copyright (C) 2003-2017 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 <http://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 (ta
->ta_addr__rtld_global
== 0
32 && td_mod_lookup (ta
->ph
, LD_SO
, SYM__rtld_global
,
33 &ta
->ta_addr__rtld_global
) != PS_OK
)
34 ta
->ta_addr__rtld_global
= (void*)-1;
36 if (ta
->ta_addr__rtld_global
!= (void*)-1)
38 err
= DB_GET_FIELD (head
, ta
, ta
->ta_addr__rtld_global
,
39 rtld_global
, _dl_tls_dtv_slotinfo_list
, 0);
45 if (ta
->ta_addr__dl_tls_dtv_slotinfo_list
== 0
46 && td_mod_lookup (ta
->ph
, NULL
, SYM__dl_tls_dtv_slotinfo_list
,
47 &ta
->ta_addr__dl_tls_dtv_slotinfo_list
) != PS_OK
)
50 err
= _td_fetch_value (ta
, ta
->ta_var__dl_tls_dtv_slotinfo_list
,
51 SYM_DESC__dl_tls_dtv_slotinfo_list
,
52 0, ta
->ta_addr__dl_tls_dtv_slotinfo_list
, &head
);
61 /* Get the address of the DTV slotinfo entry for MODID into
64 dtv_slotinfo (td_thragent_t
*ta
,
65 unsigned long int modid
,
66 psaddr_t
*dtvslotinfo
)
72 err
= dtv_slotinfo_list (ta
, &slot
);
78 /* Get the number of entries in this list entry's array. */
79 err
= DB_GET_FIELD (temp
, ta
, slot
, dtv_slotinfo_list
, len
, 0);
82 size_t len
= (uintptr_t)temp
;
84 /* Did we find the list entry for modid? */
85 if (modid
< slbase
+ len
)
88 /* We didn't, so get the next list entry. */
90 err
= DB_GET_FIELD (temp
, ta
, slot
, dtv_slotinfo_list
,
97 /* We reached the end of the list and found nothing. */
101 /* Take the slotinfo for modid from the list entry. */
102 err
= DB_GET_FIELD_ADDRESS (temp
, ta
, slot
, dtv_slotinfo_list
,
103 slotinfo
, modid
- slbase
);
112 /* Return in *BASE the base address of the TLS block for MODID within
115 It should return success and yield the correct pointer in any
116 circumstance where the TLS block for the module and thread
117 requested has already been initialized.
119 It should fail with TD_TLSDEFER only when the thread could not
120 possibly have observed any values in that TLS block. That way, the
121 debugger can fall back to showing initial values from the PT_TLS
122 segment (and refusing attempts to mutate) for the TD_TLSDEFER case,
123 and never fail to make the values the program will actually see
124 available to the user of the debugger. */
126 td_thr_tlsbase (const td_thrhandle_t
*th
,
127 unsigned long int modid
,
131 psaddr_t dtv
, dtvslot
, dtvptr
, temp
;
136 psaddr_t pd
= th
->th_unique
;
139 /* This is the fake handle for the main thread before libpthread
140 initialization. We are using 0 for its th_unique because we can't
141 trust that its thread register has been initialized. But we need
142 a real pointer to have any TLS access work. In case of dlopen'd
143 libpthread, initialization might not be for quite some time. So
144 try looking up the thread register now. Worst case, it's nonzero
145 uninitialized garbage and we get bogus results for TLS access
146 attempted too early. Tough. */
148 td_thrhandle_t main_th
;
149 err
= __td_ta_lookup_th_unique (th
->th_ta_p
, ps_getpid (th
->th_ta_p
->ph
),
152 pd
= main_th
.th_unique
;
157 err
= dtv_slotinfo (th
->th_ta_p
, modid
, &temp
);
162 err
= DB_GET_STRUCT (slot
, th
->th_ta_p
, temp
, dtv_slotinfo
);
166 /* Take the link_map from the slotinfo. */
168 err
= DB_GET_FIELD_LOCAL (map
, th
->th_ta_p
, slot
, dtv_slotinfo
, map
, 0);
174 /* Ok, the modid is good, now find out what DTV generation it
176 err
= DB_GET_FIELD_LOCAL (temp
, th
->th_ta_p
, slot
, dtv_slotinfo
, gen
, 0);
179 size_t modgen
= (uintptr_t)temp
;
181 /* Get the DTV pointer from the thread descriptor. */
182 err
= DB_GET_FIELD (dtv
, th
->th_ta_p
, pd
, pthread
, dtvp
, 0);
187 /* Get the DTV generation count at dtv[0].counter. */
188 err
= DB_GET_FIELD_ADDRESS (dtvgenloc
, th
->th_ta_p
, dtv
, dtv
, dtv
, 0);
191 err
= DB_GET_FIELD (temp
, th
->th_ta_p
, dtvgenloc
, dtv_t
, counter
, 0);
194 size_t dtvgen
= (uintptr_t)temp
;
196 /* Is the DTV current enough? */
200 /* If the module uses Static TLS, we're still good. */
201 err
= DB_GET_FIELD (temp
, th
->th_ta_p
, map
, link_map
, l_tls_offset
, 0);
204 ptrdiff_t tlsoff
= (uintptr_t)temp
;
206 if (tlsoff
!= FORCED_DYNAMIC_TLS_OFFSET
207 && tlsoff
!= NO_TLS_OFFSET
)
212 dtvptr
= tp
- tlsoff
;
214 dtvptr
= tp
+ tlsoff
+ TLS_PRE_TCB_SIZE
;
216 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
226 /* Find the corresponding entry in the DTV. */
227 err
= DB_GET_FIELD_ADDRESS (dtvslot
, th
->th_ta_p
, dtv
, dtv
, dtv
, modid
);
231 /* Extract the TLS block address from that DTV slot. */
232 err
= DB_GET_FIELD (dtvptr
, th
->th_ta_p
, dtvslot
, dtv_t
, pointer_val
, 0);
236 /* It could be that the memory for this module is not allocated for
238 if ((uintptr_t) dtvptr
& 1)