td_thr_tlsbase.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Locate TLS data for a thread.
  2. Copyright (C) 2003, 2006, 2007 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. td_err_e
  17. td_thr_tlsbase (const td_thrhandle_t *th,
  18. unsigned long int modid,
  19. psaddr_t *base)
  20. {
  21. td_err_e err;
  22. psaddr_t dtv, dtvslot, dtvptr, pd;
  23. if (modid < 1)
  24. return TD_NOTLS;
  25. pd = th->th_unique;
  26. if (pd == 0)
  27. {
  28. /* This is the fake handle for the main thread before libpthread
  29. initialization. We are using 0 for its th_unique because we can't
  30. trust that its thread register has been initialized. But we need
  31. a real pointer to have any TLS access work. In case of dlopen'd
  32. libpthread, initialization might not be for quite some time. So
  33. try looking up the thread register now. Worst case, it's nonzero
  34. uninitialized garbage and we get bogus results for TLS access
  35. attempted too early. Tough. */
  36. td_thrhandle_t main_th;
  37. err = __td_ta_lookup_th_unique (th->th_ta_p, ps_getpid (th->th_ta_p->ph),
  38. &main_th);
  39. if (err == 0)
  40. pd = main_th.th_unique;
  41. if (pd == 0)
  42. return TD_TLSDEFER;
  43. }
  44. /* Get the DTV pointer from the thread descriptor. */
  45. err = DB_GET_FIELD (dtv, th->th_ta_p, pd, pthread, dtvp, 0);
  46. if (err != TD_OK)
  47. return err;
  48. /* Find the corresponding entry in the DTV. */
  49. err = DB_GET_FIELD_ADDRESS (dtvslot, th->th_ta_p, dtv, dtv, dtv, modid);
  50. if (err != TD_OK)
  51. return err;
  52. /* Extract the TLS block address from that DTV slot. */
  53. err = DB_GET_FIELD (dtvptr, th->th_ta_p, dtvslot, dtv_t, pointer_val, 0);
  54. if (err != TD_OK)
  55. return err;
  56. /* It could be that the memory for this module is not allocated for
  57. the given thread. */
  58. if ((uintptr_t) dtvptr & 1)
  59. return TD_TLSDEFER;
  60. *base = dtvptr;
  61. return TD_OK;
  62. }