td_thr_tlsbase.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Locate TLS data for a thread.
  2. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "thread_dbP.h"
  16. /* Value used for dtv entries for which the allocation is delayed. */
  17. # define TLS_DTV_UNALLOCATED ((void *) -1l)
  18. td_err_e
  19. td_thr_tlsbase (const td_thrhandle_t *th,
  20. unsigned long int modid,
  21. psaddr_t *base)
  22. {
  23. if (modid < 1)
  24. return TD_NOTLS;
  25. #ifdef __UCLIBC_HAS_TLS__
  26. union dtv pdtv, *dtvp;
  27. LOG ("td_thr_tlsbase");
  28. psaddr_t dtvpp = th->th_unique;
  29. #if defined(TLS_TCB_AT_TP)
  30. dtvpp += offsetof (struct _pthread_descr_struct, p_header.data.dtvp);
  31. #elif defined(TLS_DTV_AT_TP)
  32. /* Special case hack. If TLS_TCB_SIZE == 0 (on PowerPC), there is no TCB
  33. containing the DTV at the TP, but actually the TCB lies behind the TP,
  34. i.e. at the very end of the area covered by TLS_PRE_TCB_SIZE. */
  35. dtvpp += TLS_PRE_TCB_SIZE + offsetof (tcbhead_t, dtv)
  36. - (TLS_TCB_SIZE == 0 ? sizeof (tcbhead_t) : 0);
  37. #else
  38. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined."
  39. #endif
  40. /* Get the DTV pointer from the thread descriptor. */
  41. if (ps_pdread (th->th_ta_p->ph, dtvpp, &dtvp, sizeof dtvp) != PS_OK)
  42. return TD_ERR; /* XXX Other error value? */
  43. /* Get the corresponding entry in the DTV. */
  44. if (ps_pdread (th->th_ta_p->ph, dtvp + modid,
  45. &pdtv, sizeof (union dtv)) != PS_OK)
  46. return TD_ERR; /* XXX Other error value? */
  47. /* It could be that the memory for this module is not allocated for
  48. the given thread. */
  49. if (pdtv.pointer.val == TLS_DTV_UNALLOCATED)
  50. return TD_TLSDEFER;
  51. *base = (char *) pdtv.pointer.val;
  52. return TD_OK;
  53. #else
  54. return TD_ERR;
  55. #endif
  56. }