| 1 | /* Hash table for TLS descriptors. | 
| 2 |    Copyright (C) 2005-2023 Free Software Foundation, Inc. | 
| 3 |    This file is part of the GNU C Library. | 
| 4 |  | 
| 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. | 
| 9 |  | 
| 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. | 
| 14 |  | 
| 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/>.  */ | 
| 18 |  | 
| 19 | #ifndef TLSDESCHTAB_H | 
| 20 | # define TLSDESCHTAB_H 1 | 
| 21 |  | 
| 22 | #include <atomic.h> | 
| 23 |  | 
| 24 | # ifdef SHARED | 
| 25 |  | 
| 26 | #  include <inline-hashtab.h> | 
| 27 |  | 
| 28 | inline static int | 
| 29 | hash_tlsdesc (void *p) | 
| 30 | { | 
| 31 |   struct tlsdesc_dynamic_arg *td = p; | 
| 32 |  | 
| 33 |   /* We know all entries are for the same module, so ti_offset is the | 
| 34 |      only distinguishing entry.  */ | 
| 35 |   return td->tlsinfo.ti_offset; | 
| 36 | } | 
| 37 |  | 
| 38 | inline static int | 
| 39 | eq_tlsdesc (void *p, void *q) | 
| 40 | { | 
| 41 |   struct tlsdesc_dynamic_arg *tdp = p, *tdq = q; | 
| 42 |  | 
| 43 |   return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset; | 
| 44 | } | 
| 45 |  | 
| 46 | inline static size_t | 
| 47 | map_generation (struct link_map *map) | 
| 48 | { | 
| 49 |   size_t idx = map->l_tls_modid; | 
| 50 |   struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list); | 
| 51 |  | 
| 52 |   /* Find the place in the dtv slotinfo list.  */ | 
| 53 |   do | 
| 54 |     { | 
| 55 |       /* Does it fit in the array of this list element?  */ | 
| 56 |       if (idx < listp->len) | 
| 57 | 	{ | 
| 58 | 	  /* We should never get here for a module in static TLS, so | 
| 59 | 	     we can assume that, if the generation count is zero, we | 
| 60 | 	     still haven't determined the generation count for this | 
| 61 | 	     module.  */ | 
| 62 | 	  if (listp->slotinfo[idx].map == map && listp->slotinfo[idx].gen) | 
| 63 | 	    return listp->slotinfo[idx].gen; | 
| 64 | 	  else | 
| 65 | 	    break; | 
| 66 | 	} | 
| 67 |       idx -= listp->len; | 
| 68 |       listp = listp->next; | 
| 69 |     } | 
| 70 |   while (listp != NULL); | 
| 71 |  | 
| 72 |   /* If we get to this point, the module still hasn't been assigned an | 
| 73 |      entry in the dtv slotinfo data structures, and it will when we're | 
| 74 |      done with relocations.  At that point, the module will get a | 
| 75 |      generation number that is one past the current generation, so | 
| 76 |      return exactly that.  */ | 
| 77 |   return GL(dl_tls_generation) + 1; | 
| 78 | } | 
| 79 |  | 
| 80 | /* Returns the data pointer for a given map and tls offset that is used | 
| 81 |    to fill in one of the GOT entries referenced by a TLSDESC relocation | 
| 82 |    when using dynamic TLS.  This requires allocation, returns NULL on | 
| 83 |    allocation failure.  */ | 
| 84 | void * | 
| 85 | _dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset) | 
| 86 | { | 
| 87 |   struct hashtab *ht; | 
| 88 |   void **entry; | 
| 89 |   struct tlsdesc_dynamic_arg *td, test; | 
| 90 |  | 
| 91 |   ht = map->l_mach.tlsdesc_table; | 
| 92 |   if (! ht) | 
| 93 |     { | 
| 94 |       ht = htab_create (); | 
| 95 |       if (! ht) | 
| 96 | 	return 0; | 
| 97 |       map->l_mach.tlsdesc_table = ht; | 
| 98 |     } | 
| 99 |  | 
| 100 |   test.tlsinfo.ti_module = map->l_tls_modid; | 
| 101 |   test.tlsinfo.ti_offset = ti_offset; | 
| 102 |   entry = htab_find_slot (ht, &test, 1, hash_tlsdesc, eq_tlsdesc); | 
| 103 |   if (! entry) | 
| 104 |     return 0; | 
| 105 |  | 
| 106 |   if (*entry) | 
| 107 |     { | 
| 108 |       td = *entry; | 
| 109 |       return td; | 
| 110 |     } | 
| 111 |  | 
| 112 |   *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg)); | 
| 113 |   if (! td) | 
| 114 |     return 0; | 
| 115 |   /* This may be higher than the map's generation, but it doesn't | 
| 116 |      matter much.  Worst case, we'll have one extra DTV update per | 
| 117 |      thread.  */ | 
| 118 |   td->gen_count = map_generation (map); | 
| 119 |   td->tlsinfo = test.tlsinfo; | 
| 120 |   return td; | 
| 121 | } | 
| 122 |  | 
| 123 | # endif /* SHARED */ | 
| 124 |  | 
| 125 | #endif | 
| 126 |  |