1 /* Hash table for TLS descriptors.
2 Copyright (C) 2005-2015 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
25 # include <inline-hashtab.h>
28 hash_tlsdesc (void *p
)
30 struct tlsdesc_dynamic_arg
*td
= p
;
32 /* We know all entries are for the same module, so ti_offset is the
33 only distinguishing entry. */
34 return td
->tlsinfo
.ti_offset
;
38 eq_tlsdesc (void *p
, void *q
)
40 struct tlsdesc_dynamic_arg
*tdp
= p
, *tdq
= q
;
42 return tdp
->tlsinfo
.ti_offset
== tdq
->tlsinfo
.ti_offset
;
46 map_generation (struct link_map
*map
)
48 size_t idx
= map
->l_tls_modid
;
49 struct dtv_slotinfo_list
*listp
= GL(dl_tls_dtv_slotinfo_list
);
51 /* Find the place in the dtv slotinfo list. */
54 /* Does it fit in the array of this list element? */
57 /* We should never get here for a module in static TLS, so
58 we can assume that, if the generation count is zero, we
59 still haven't determined the generation count for this
61 if (listp
->slotinfo
[idx
].gen
)
62 return listp
->slotinfo
[idx
].gen
;
69 while (listp
!= NULL
);
71 /* If we get to this point, the module still hasn't been assigned an
72 entry in the dtv slotinfo data structures, and it will when we're
73 done with relocations. At that point, the module will get a
74 generation number that is one past the current generation, so
75 return exactly that. */
76 return GL(dl_tls_generation
) + 1;
81 _dl_make_tlsdesc_dynamic (struct link_map
*map
, size_t ti_offset
)
85 struct tlsdesc_dynamic_arg
*td
, test
;
87 /* FIXME: We could use a per-map lock here, but is it worth it? */
88 __rtld_lock_lock_recursive (GL(dl_load_lock
));
90 ht
= map
->l_mach
.tlsdesc_table
;
96 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
99 map
->l_mach
.tlsdesc_table
= ht
;
102 test
.tlsinfo
.ti_module
= map
->l_tls_modid
;
103 test
.tlsinfo
.ti_offset
= ti_offset
;
104 entry
= htab_find_slot (ht
, &test
, 1, hash_tlsdesc
, eq_tlsdesc
);
107 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
114 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
118 *entry
= td
= malloc (sizeof (struct tlsdesc_dynamic_arg
));
119 /* This may be higher than the map's generation, but it doesn't
120 matter much. Worst case, we'll have one extra DTV update per
122 td
->gen_count
= map_generation (map
);
123 td
->tlsinfo
= test
.tlsinfo
;
125 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
131 /* The idea of the following two functions is to stop multiple threads
132 from attempting to resolve the same TLS descriptor without busy
133 waiting. Ideally, we should be able to release the lock right
134 after changing td->entry, and then using say a condition variable
135 or a futex wake to wake up any waiting threads, but let's try to
136 avoid introducing such dependencies. */
139 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc
volatile *td
, void *caller
)
141 if (caller
!= td
->entry
)
144 __rtld_lock_lock_recursive (GL(dl_load_lock
));
145 if (caller
!= td
->entry
)
147 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
151 td
->entry
= _dl_tlsdesc_resolve_hold
;
157 _dl_tlsdesc_wake_up_held_fixups (void)
159 __rtld_lock_unlock_recursive (GL(dl_load_lock
));