2003-03-15 23:55:23 +00:00
|
|
|
/* Locate TLS data for a thread.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2003-2021 Free Software Foundation, Inc.
|
2003-03-15 23:55:23 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2003-03-15 23:55:23 +00:00
|
|
|
|
|
|
|
#include "thread_dbP.h"
|
2015-03-17 04:14:11 +00:00
|
|
|
#include <link.h>
|
2003-03-15 23:55:23 +00:00
|
|
|
|
2015-03-17 04:14:11 +00:00
|
|
|
/* Get the DTV slotinfo list head entry from the dynamic loader state
|
|
|
|
into *LISTHEAD. */
|
|
|
|
static td_err_e
|
|
|
|
dtv_slotinfo_list (td_thragent_t *ta,
|
|
|
|
psaddr_t *listhead)
|
|
|
|
{
|
|
|
|
td_err_e err;
|
|
|
|
psaddr_t head;
|
|
|
|
|
2020-11-16 18:33:30 +00:00
|
|
|
if (__td_ta_rtld_global (ta))
|
2015-03-17 04:14:11 +00:00
|
|
|
{
|
|
|
|
err = DB_GET_FIELD (head, ta, ta->ta_addr__rtld_global,
|
|
|
|
rtld_global, _dl_tls_dtv_slotinfo_list, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ta->ta_addr__dl_tls_dtv_slotinfo_list == 0
|
|
|
|
&& td_mod_lookup (ta->ph, NULL, SYM__dl_tls_dtv_slotinfo_list,
|
|
|
|
&ta->ta_addr__dl_tls_dtv_slotinfo_list) != PS_OK)
|
|
|
|
return TD_ERR;
|
|
|
|
|
|
|
|
err = _td_fetch_value (ta, ta->ta_var__dl_tls_dtv_slotinfo_list,
|
|
|
|
SYM_DESC__dl_tls_dtv_slotinfo_list,
|
|
|
|
0, ta->ta_addr__dl_tls_dtv_slotinfo_list, &head);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*listhead = head;
|
|
|
|
return TD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the address of the DTV slotinfo entry for MODID into
|
|
|
|
*DTVSLOTINFO. */
|
|
|
|
static td_err_e
|
|
|
|
dtv_slotinfo (td_thragent_t *ta,
|
|
|
|
unsigned long int modid,
|
|
|
|
psaddr_t *dtvslotinfo)
|
|
|
|
{
|
|
|
|
td_err_e err;
|
|
|
|
psaddr_t slot, temp;
|
|
|
|
size_t slbase = 0;
|
|
|
|
|
|
|
|
err = dtv_slotinfo_list (ta, &slot);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
while (slot)
|
|
|
|
{
|
|
|
|
/* Get the number of entries in this list entry's array. */
|
|
|
|
err = DB_GET_FIELD (temp, ta, slot, dtv_slotinfo_list, len, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
size_t len = (uintptr_t)temp;
|
|
|
|
|
|
|
|
/* Did we find the list entry for modid? */
|
|
|
|
if (modid < slbase + len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We didn't, so get the next list entry. */
|
|
|
|
slbase += len;
|
|
|
|
err = DB_GET_FIELD (temp, ta, slot, dtv_slotinfo_list,
|
|
|
|
next, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
slot = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We reached the end of the list and found nothing. */
|
|
|
|
if (!slot)
|
|
|
|
return TD_ERR;
|
|
|
|
|
|
|
|
/* Take the slotinfo for modid from the list entry. */
|
|
|
|
err = DB_GET_FIELD_ADDRESS (temp, ta, slot, dtv_slotinfo_list,
|
|
|
|
slotinfo, modid - slbase);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
slot = temp;
|
|
|
|
|
|
|
|
*dtvslotinfo = slot;
|
|
|
|
return TD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return in *BASE the base address of the TLS block for MODID within
|
|
|
|
TH.
|
|
|
|
|
|
|
|
It should return success and yield the correct pointer in any
|
|
|
|
circumstance where the TLS block for the module and thread
|
|
|
|
requested has already been initialized.
|
|
|
|
|
|
|
|
It should fail with TD_TLSDEFER only when the thread could not
|
|
|
|
possibly have observed any values in that TLS block. That way, the
|
|
|
|
debugger can fall back to showing initial values from the PT_TLS
|
|
|
|
segment (and refusing attempts to mutate) for the TD_TLSDEFER case,
|
|
|
|
and never fail to make the values the program will actually see
|
|
|
|
available to the user of the debugger. */
|
2003-03-15 23:55:23 +00:00
|
|
|
td_err_e
|
|
|
|
td_thr_tlsbase (const td_thrhandle_t *th,
|
|
|
|
unsigned long int modid,
|
|
|
|
psaddr_t *base)
|
|
|
|
{
|
2003-09-09 07:01:01 +00:00
|
|
|
td_err_e err;
|
2015-03-17 04:14:11 +00:00
|
|
|
psaddr_t dtv, dtvslot, dtvptr, temp;
|
2003-09-09 07:01:01 +00:00
|
|
|
|
2003-03-15 23:55:23 +00:00
|
|
|
if (modid < 1)
|
|
|
|
return TD_NOTLS;
|
|
|
|
|
2007-06-26 06:47:56 +00:00
|
|
|
psaddr_t pd = th->th_unique;
|
|
|
|
if (pd == 0)
|
|
|
|
{
|
|
|
|
/* This is the fake handle for the main thread before libpthread
|
|
|
|
initialization. We are using 0 for its th_unique because we can't
|
|
|
|
trust that its thread register has been initialized. But we need
|
|
|
|
a real pointer to have any TLS access work. In case of dlopen'd
|
|
|
|
libpthread, initialization might not be for quite some time. So
|
|
|
|
try looking up the thread register now. Worst case, it's nonzero
|
|
|
|
uninitialized garbage and we get bogus results for TLS access
|
|
|
|
attempted too early. Tough. */
|
|
|
|
|
|
|
|
td_thrhandle_t main_th;
|
|
|
|
err = __td_ta_lookup_th_unique (th->th_ta_p, ps_getpid (th->th_ta_p->ph),
|
|
|
|
&main_th);
|
|
|
|
if (err == 0)
|
|
|
|
pd = main_th.th_unique;
|
|
|
|
if (pd == 0)
|
|
|
|
return TD_TLSDEFER;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:14:11 +00:00
|
|
|
err = dtv_slotinfo (th->th_ta_p, modid, &temp);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
psaddr_t slot;
|
|
|
|
err = DB_GET_STRUCT (slot, th->th_ta_p, temp, dtv_slotinfo);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Take the link_map from the slotinfo. */
|
|
|
|
psaddr_t map;
|
|
|
|
err = DB_GET_FIELD_LOCAL (map, th->th_ta_p, slot, dtv_slotinfo, map, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
if (!map)
|
|
|
|
return TD_ERR;
|
|
|
|
|
|
|
|
/* Ok, the modid is good, now find out what DTV generation it
|
|
|
|
requires. */
|
|
|
|
err = DB_GET_FIELD_LOCAL (temp, th->th_ta_p, slot, dtv_slotinfo, gen, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
size_t modgen = (uintptr_t)temp;
|
|
|
|
|
2003-03-15 23:55:23 +00:00
|
|
|
/* Get the DTV pointer from the thread descriptor. */
|
2007-06-26 06:47:56 +00:00
|
|
|
err = DB_GET_FIELD (dtv, th->th_ta_p, pd, pthread, dtvp, 0);
|
2003-09-09 07:01:01 +00:00
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
2003-03-15 23:55:23 +00:00
|
|
|
|
2015-03-17 04:14:11 +00:00
|
|
|
psaddr_t dtvgenloc;
|
|
|
|
/* Get the DTV generation count at dtv[0].counter. */
|
|
|
|
err = DB_GET_FIELD_ADDRESS (dtvgenloc, th->th_ta_p, dtv, dtv, dtv, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
err = DB_GET_FIELD (temp, th->th_ta_p, dtvgenloc, dtv_t, counter, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
size_t dtvgen = (uintptr_t)temp;
|
|
|
|
|
|
|
|
/* Is the DTV current enough? */
|
|
|
|
if (dtvgen < modgen)
|
|
|
|
{
|
|
|
|
try_static_tls:
|
|
|
|
/* If the module uses Static TLS, we're still good. */
|
|
|
|
err = DB_GET_FIELD (temp, th->th_ta_p, map, link_map, l_tls_offset, 0);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
ptrdiff_t tlsoff = (uintptr_t)temp;
|
|
|
|
|
|
|
|
if (tlsoff != FORCED_DYNAMIC_TLS_OFFSET
|
|
|
|
&& tlsoff != NO_TLS_OFFSET)
|
|
|
|
{
|
|
|
|
psaddr_t tp = pd;
|
|
|
|
|
|
|
|
#if TLS_TCB_AT_TP
|
|
|
|
dtvptr = tp - tlsoff;
|
|
|
|
#elif TLS_DTV_AT_TP
|
|
|
|
dtvptr = tp + tlsoff + TLS_PRE_TCB_SIZE;
|
|
|
|
#else
|
|
|
|
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*base = dtvptr;
|
|
|
|
return TD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TD_TLSDEFER;
|
|
|
|
}
|
|
|
|
|
2006-02-04 00:48:40 +00:00
|
|
|
/* Find the corresponding entry in the DTV. */
|
|
|
|
err = DB_GET_FIELD_ADDRESS (dtvslot, th->th_ta_p, dtv, dtv, dtv, modid);
|
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Extract the TLS block address from that DTV slot. */
|
|
|
|
err = DB_GET_FIELD (dtvptr, th->th_ta_p, dtvslot, dtv_t, pointer_val, 0);
|
2003-09-09 07:01:01 +00:00
|
|
|
if (err != TD_OK)
|
|
|
|
return err;
|
2003-03-15 23:55:23 +00:00
|
|
|
|
|
|
|
/* It could be that the memory for this module is not allocated for
|
|
|
|
the given thread. */
|
2003-09-09 07:01:01 +00:00
|
|
|
if ((uintptr_t) dtvptr & 1)
|
2015-03-17 04:14:11 +00:00
|
|
|
goto try_static_tls;
|
2003-03-15 23:55:23 +00:00
|
|
|
|
2003-09-09 07:01:01 +00:00
|
|
|
*base = dtvptr;
|
2003-03-15 23:55:23 +00:00
|
|
|
return TD_OK;
|
|
|
|
}
|