Remember we switched to single-request mode.
[glibc.git] / sysdeps / x86_64 / tlsdesc.c
blob0cc32aab694814577e93dde0234e13d9e6d8bbba
1 /* Manage TLS descriptors. x86_64 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 2 functions take a caller argument, that contains the
28 address expected to be in the TLS descriptor. If it's changed, we
29 want to return immediately. */
31 /* This function is used to lazily resolve TLS_DESC RELA relocations.
32 The argument location is used to hold a pointer to the relocation. */
34 void
35 attribute_hidden
36 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
37 struct link_map *l)
39 const ElfW(Rela) *reloc = td->arg;
41 if (_dl_tlsdesc_resolve_early_return_p
42 (td, (void*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_PLT)]) + l->l_addr)))
43 return;
45 /* The code below was borrowed from _dl_fixup(). */
46 const ElfW(Sym) *const symtab
47 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
48 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
49 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
50 lookup_t result;
52 /* Look up the target symbol. If the normal lookup rules are not
53 used don't look in the global scope. */
54 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
55 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
57 const struct r_found_version *version = NULL;
59 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
61 const ElfW(Half) *vernum =
62 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
63 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
64 version = &l->l_versions[ndx];
65 if (version->hash == 0)
66 version = NULL;
69 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
70 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
71 DL_LOOKUP_ADD_DEPENDENCY, NULL);
73 else
75 /* We already found the symbol. The module (and therefore its load
76 address) is also known. */
77 result = l;
80 if (! sym)
82 td->arg = (void*)reloc->r_addend;
83 td->entry = _dl_tlsdesc_undefweak;
85 else
87 # ifndef SHARED
88 CHECK_STATIC_TLS (l, result);
89 # else
90 if (!TRY_STATIC_TLS (l, result))
92 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
93 + reloc->r_addend);
94 td->entry = _dl_tlsdesc_dynamic;
96 else
97 # endif
99 td->arg = (void*)(sym->st_value - result->l_tls_offset
100 + reloc->r_addend);
101 td->entry = _dl_tlsdesc_return;
105 _dl_tlsdesc_wake_up_held_fixups ();
108 /* This function is used to avoid busy waiting for other threads to
109 complete the lazy relocation. Once another thread wins the race to
110 relocate a TLS descriptor, it sets the descriptor up such that this
111 function is called to wait until the resolver releases the
112 lock. */
114 void
115 attribute_hidden
116 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
117 void *caller)
119 /* Maybe we're lucky and can return early. */
120 if (caller != td->entry)
121 return;
123 /* Locking here will stop execution until the running resolver runs
124 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
126 FIXME: We'd be better off waiting on a condition variable, such
127 that we didn't have to hold the lock throughout the relocation
128 processing. */
129 __rtld_lock_lock_recursive (GL(dl_load_lock));
130 __rtld_lock_unlock_recursive (GL(dl_load_lock));
133 /* Unmap the dynamic object, but also release its TLS descriptor table
134 if there is one. */
136 void
137 internal_function
138 _dl_unmap (struct link_map *map)
140 __munmap ((void *) (map)->l_map_start,
141 (map)->l_map_end - (map)->l_map_start);
143 #if SHARED
144 /* _dl_unmap is only called for dlopen()ed libraries, for which
145 calling free() is safe, or before we've completed the initial
146 relocation, in which case calling free() is probably pointless,
147 but still safe. */
148 if (map->l_mach.tlsdesc_table)
149 htab_delete (map->l_mach.tlsdesc_table);
150 #endif