Mention IFUNC enhancements to testsuite in NEWS.
[platform/upstream/glibc.git] / elf / tlsdeschtab.h
1 /* Hash table for TLS descriptors.
2    Copyright (C) 2005, 2008 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Alexandre Oliva  <aoliva@redhat.com>
5
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.
10
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.
15
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/>.  */
19
20 #ifndef TLSDESCHTAB_H
21 # define TLSDESCHTAB_H 1
22
23 # ifdef SHARED
24
25 #  include <inline-hashtab.h>
26
27 inline static int
28 hash_tlsdesc (void *p)
29 {
30   struct tlsdesc_dynamic_arg *td = p;
31
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;
35 }
36
37 inline static int
38 eq_tlsdesc (void *p, void *q)
39 {
40   struct tlsdesc_dynamic_arg *tdp = p, *tdq = q;
41
42   return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset;
43 }
44
45 inline static int
46 map_generation (struct link_map *map)
47 {
48   size_t idx = map->l_tls_modid;
49   struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
50
51   /* Find the place in the dtv slotinfo list.  */
52   do
53     {
54       /* Does it fit in the array of this list element?  */
55       if (idx < listp->len)
56         {
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
60              module.  */
61           if (listp->slotinfo[idx].gen)
62             return listp->slotinfo[idx].gen;
63           else
64             break;
65         }
66       idx -= listp->len;
67       listp = listp->next;
68     }
69   while (listp != NULL);
70
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;
77 }
78
79 void *
80 internal_function
81 _dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset)
82 {
83   struct hashtab *ht;
84   void **entry;
85   struct tlsdesc_dynamic_arg *td, test;
86
87   /* FIXME: We could use a per-map lock here, but is it worth it?  */
88   __rtld_lock_lock_recursive (GL(dl_load_lock));
89
90   ht = map->l_mach.tlsdesc_table;
91   if (! ht)
92     {
93       ht = htab_create ();
94       if (! ht)
95         {
96           __rtld_lock_unlock_recursive (GL(dl_load_lock));
97           return 0;
98         }
99       map->l_mach.tlsdesc_table = ht;
100     }
101
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);
105   if (*entry)
106     {
107       td = *entry;
108       __rtld_lock_unlock_recursive (GL(dl_load_lock));
109       return td;
110     }
111
112   *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg));
113   /* This may be higher than the map's generation, but it doesn't
114      matter much.  Worst case, we'll have one extra DTV update per
115      thread.  */
116   td->gen_count = map_generation (map);
117   td->tlsinfo = test.tlsinfo;
118
119   __rtld_lock_unlock_recursive (GL(dl_load_lock));
120   return td;
121 }
122
123 # endif /* SHARED */
124
125 /* The idea of the following two functions is to stop multiple threads
126    from attempting to resolve the same TLS descriptor without busy
127    waiting.  Ideally, we should be able to release the lock right
128    after changing td->entry, and then using say a condition variable
129    or a futex wake to wake up any waiting threads, but let's try to
130    avoid introducing such dependencies.  */
131
132 inline static int
133 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc volatile *td, void *caller)
134 {
135   if (caller != td->entry)
136     return 1;
137
138   __rtld_lock_lock_recursive (GL(dl_load_lock));
139   if (caller != td->entry)
140     {
141       __rtld_lock_unlock_recursive (GL(dl_load_lock));
142       return 1;
143     }
144
145   td->entry = _dl_tlsdesc_resolve_hold;
146
147   return 0;
148 }
149
150 inline static void
151 _dl_tlsdesc_wake_up_held_fixups (void)
152 {
153   __rtld_lock_unlock_recursive (GL(dl_load_lock));
154 }
155
156 #endif