Updated to fedora-glibc-20090427T1419
[glibc/history.git] / sysdeps / i386 / tlsdesc.c
blob28287d574b708f4b05e4b3b221bc94a537abe9ad
1 /* Manage TLS descriptors. i386 version.
2 Copyright (C) 2005, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <link.h>
21 #include <ldsodefs.h>
22 #include <elf/dynamic-link.h>
23 #include <tls.h>
24 #include <dl-tlsdesc.h>
25 #include <tlsdeschtab.h>
27 /* The following 4 functions take an entry_check_offset argument.
28 It's computed by the caller as an offset between its entry point
29 and the call site, such that by adding the built-in return address
30 that is implicitly passed to the function with this offset, we can
31 easily obtain the caller's entry point to compare with the entry
32 point given in the TLS descriptor. If it's changed, we want to
33 return immediately. */
35 /* This function is used to lazily resolve TLS_DESC REL relocations
36 that reference the *ABS* segment in their own link maps. The
37 argument is the addend originally stored there. */
39 void
40 __attribute__ ((regparm (3))) attribute_hidden
41 _dl_tlsdesc_resolve_abs_plus_addend_fixup (struct tlsdesc volatile *td,
42 struct link_map *l,
43 ptrdiff_t entry_check_offset)
45 ptrdiff_t addend = (ptrdiff_t) td->arg;
47 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
48 - entry_check_offset))
49 return;
51 #ifndef SHARED
52 CHECK_STATIC_TLS (l, l);
53 #else
54 if (!TRY_STATIC_TLS (l, l))
56 td->arg = _dl_make_tlsdesc_dynamic (l, addend);
57 td->entry = _dl_tlsdesc_dynamic;
59 else
60 #endif
62 td->arg = (void*) (addend - l->l_tls_offset);
63 td->entry = _dl_tlsdesc_return;
66 _dl_tlsdesc_wake_up_held_fixups ();
69 /* This function is used to lazily resolve TLS_DESC REL relocations
70 that originally had zero addends. The argument location, that
71 originally held the addend, is used to hold a pointer to the
72 relocation, but it has to be restored before we call the function
73 that applies relocations. */
75 void
76 __attribute__ ((regparm (3))) attribute_hidden
77 _dl_tlsdesc_resolve_rel_fixup (struct tlsdesc volatile *td,
78 struct link_map *l,
79 ptrdiff_t entry_check_offset)
81 const ElfW(Rel) *reloc = td->arg;
83 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
84 - entry_check_offset))
85 return;
87 /* The code below was borrowed from _dl_fixup(),
88 except for checking for STB_LOCAL. */
89 const ElfW(Sym) *const symtab
90 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
91 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
92 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
93 lookup_t result;
95 /* Look up the target symbol. If the normal lookup rules are not
96 used don't look in the global scope. */
97 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
98 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
100 const struct r_found_version *version = NULL;
102 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
104 const ElfW(Half) *vernum =
105 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
106 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
107 version = &l->l_versions[ndx];
108 if (version->hash == 0)
109 version = NULL;
112 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
113 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
114 DL_LOOKUP_ADD_DEPENDENCY, NULL);
116 else
118 /* We already found the symbol. The module (and therefore its load
119 address) is also known. */
120 result = l;
123 if (!sym)
125 td->arg = 0;
126 td->entry = _dl_tlsdesc_undefweak;
128 else
130 # ifndef SHARED
131 CHECK_STATIC_TLS (l, result);
132 # else
133 if (!TRY_STATIC_TLS (l, result))
135 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value);
136 td->entry = _dl_tlsdesc_dynamic;
138 else
139 # endif
141 td->arg = (void*)(sym->st_value - result->l_tls_offset);
142 td->entry = _dl_tlsdesc_return;
146 _dl_tlsdesc_wake_up_held_fixups ();
149 /* This function is used to lazily resolve TLS_DESC RELA relocations.
150 The argument location is used to hold a pointer to the relocation. */
152 void
153 __attribute__ ((regparm (3))) attribute_hidden
154 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
155 struct link_map *l,
156 ptrdiff_t entry_check_offset)
158 const ElfW(Rela) *reloc = td->arg;
160 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
161 - entry_check_offset))
162 return;
164 /* The code below was borrowed from _dl_fixup(),
165 except for checking for STB_LOCAL. */
166 const ElfW(Sym) *const symtab
167 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
168 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
169 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
170 lookup_t result;
172 /* Look up the target symbol. If the normal lookup rules are not
173 used don't look in the global scope. */
174 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
175 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
177 const struct r_found_version *version = NULL;
179 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
181 const ElfW(Half) *vernum =
182 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
183 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
184 version = &l->l_versions[ndx];
185 if (version->hash == 0)
186 version = NULL;
189 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
190 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
191 DL_LOOKUP_ADD_DEPENDENCY, NULL);
193 else
195 /* We already found the symbol. The module (and therefore its load
196 address) is also known. */
197 result = l;
200 if (!sym)
202 td->arg = (void*) reloc->r_addend;
203 td->entry = _dl_tlsdesc_undefweak;
205 else
207 # ifndef SHARED
208 CHECK_STATIC_TLS (l, result);
209 # else
210 if (!TRY_STATIC_TLS (l, result))
212 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
213 + reloc->r_addend);
214 td->entry = _dl_tlsdesc_dynamic;
216 else
217 # endif
219 td->arg = (void*) (sym->st_value - result->l_tls_offset
220 + reloc->r_addend);
221 td->entry = _dl_tlsdesc_return;
225 _dl_tlsdesc_wake_up_held_fixups ();
228 /* This function is used to avoid busy waiting for other threads to
229 complete the lazy relocation. Once another thread wins the race to
230 relocate a TLS descriptor, it sets the descriptor up such that this
231 function is called to wait until the resolver releases the
232 lock. */
234 void
235 __attribute__ ((regparm (3))) attribute_hidden
236 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
237 struct link_map *l __attribute__((__unused__)),
238 ptrdiff_t entry_check_offset)
240 /* Maybe we're lucky and can return early. */
241 if (__builtin_return_address (0) - entry_check_offset != td->entry)
242 return;
244 /* Locking here will stop execution until the running resolver runs
245 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
247 FIXME: We'd be better off waiting on a condition variable, such
248 that we didn't have to hold the lock throughout the relocation
249 processing. */
250 __rtld_lock_lock_recursive (GL(dl_load_lock));
251 __rtld_lock_unlock_recursive (GL(dl_load_lock));
255 /* Unmap the dynamic object, but also release its TLS descriptor table
256 if there is one. */
258 void
259 internal_function
260 _dl_unmap (struct link_map *map)
262 __munmap ((void *) (map)->l_map_start,
263 (map)->l_map_end - (map)->l_map_start);
265 #if SHARED
266 if (map->l_mach.tlsdesc_table)
267 htab_delete (map->l_mach.tlsdesc_table);
268 #endif