1 /* Hash table for TLS descriptors.
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
21 # define TLSDESCHTAB_H 1
27 # include <inline-hashtab.h>
30 hash_tlsdesc (void *p
)
32 struct tlsdesc_dynamic_arg
*td
= p
;
34 /* We know all entries are for the same module, so ti_offset is the
35 only distinguishing entry. */
36 return td
->tlsinfo
.ti_offset
;
40 eq_tlsdesc (void *p
, void *q
)
42 struct tlsdesc_dynamic_arg
*tdp
= p
, *tdq
= q
;
44 return tdp
->tlsinfo
.ti_offset
== tdq
->tlsinfo
.ti_offset
;
48 map_generation (struct link_map
*map
)
50 size_t idx
= map
->l_tls_modid
;
51 struct dtv_slotinfo_list
*listp
= GL(dl_tls_dtv_slotinfo_list
);
53 /* Find the place in the dtv slotinfo list. */
56 /* Does it fit in the array of this list element? */
59 /* We should never get here for a module in static TLS, so
60 we can assume that, if the generation count is zero, we
61 still haven't determined the generation count for this
63 if (listp
->slotinfo
[idx
].map
== map
&& listp
->slotinfo
[idx
].gen
)
64 return listp
->slotinfo
[idx
].gen
;
71 while (listp
!= NULL
);
73 /* If we get to this point, the module still hasn't been assigned an
74 entry in the dtv slotinfo data structures, and it will when we're
75 done with relocations. At that point, the module will get a
76 generation number that is one past the current generation, so
77 return exactly that. */
78 return GL(dl_tls_generation
) + 1;
83 _dl_make_tlsdesc_dynamic (struct link_map
*map
, size_t ti_offset
)
87 struct tlsdesc_dynamic_arg
*td
, test
;
89 /* FIXME: We could use a per-map lock here, but is it worth it? */
90 __rtld_lock_lock_recursive (GL(dl_load_lock
));
92 ht
= map
->l_mach
.tlsdesc_table
;
98 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
101 map
->l_mach
.tlsdesc_table
= ht
;
104 test
.tlsinfo
.ti_module
= map
->l_tls_modid
;
105 test
.tlsinfo
.ti_offset
= ti_offset
;
106 entry
= htab_find_slot (ht
, &test
, 1, hash_tlsdesc
, eq_tlsdesc
);
109 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
116 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
120 *entry
= td
= malloc (sizeof (struct tlsdesc_dynamic_arg
));
121 /* This may be higher than the map's generation, but it doesn't
122 matter much. Worst case, we'll have one extra DTV update per
124 td
->gen_count
= map_generation (map
);
125 td
->tlsinfo
= test
.tlsinfo
;
127 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
133 /* The idea of the following two functions is to stop multiple threads
134 from attempting to resolve the same TLS descriptor without busy
135 waiting. Ideally, we should be able to release the lock right
136 after changing td->entry, and then using say a condition variable
137 or a futex wake to wake up any waiting threads, but let's try to
138 avoid introducing such dependencies. */
141 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc
volatile *td
, void *caller
)
143 if (caller
!= atomic_load_relaxed (&td
->entry
))
146 __rtld_lock_lock_recursive (GL(dl_load_lock
));
147 if (caller
!= atomic_load_relaxed (&td
->entry
))
149 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
153 atomic_store_relaxed (&td
->entry
, _dl_tlsdesc_resolve_hold
);
159 _dl_tlsdesc_wake_up_held_fixups (void)
161 __rtld_lock_unlock_recursive (GL(dl_load_lock
));