Define VERSYMIDX/VALIDX/ADDRIDX in ldsodefs.h
[glibc.git] / sysdeps / x86_64 / tlsdesc.c
blob19d55a7c91992439cee17d05f50593dfab9922f4
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <link.h>
20 #include <ldsodefs.h>
21 #include <elf/dynamic-link.h>
22 #include <tls.h>
23 #include <dl-tlsdesc.h>
24 #include <tlsdeschtab.h>
26 /* The following 2 functions take a caller argument, that contains the
27 address expected to be in the TLS descriptor. If it's changed, we
28 want to return immediately. */
30 /* This function is used to lazily resolve TLS_DESC RELA relocations.
31 The argument location is used to hold a pointer to the relocation. */
33 void
34 attribute_hidden
35 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
36 struct link_map *l)
38 const ElfW(Rela) *reloc = td->arg;
40 if (_dl_tlsdesc_resolve_early_return_p
41 (td, (void*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_PLT)]) + l->l_addr)))
42 return;
44 /* The code below was borrowed from _dl_fixup(). */
45 const ElfW(Sym) *const symtab
46 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
47 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
48 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
49 lookup_t result;
51 /* Look up the target symbol. If the normal lookup rules are not
52 used don't look in the global scope. */
53 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
54 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
56 const struct r_found_version *version = NULL;
58 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
60 const ElfW(Half) *vernum =
61 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
62 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
63 version = &l->l_versions[ndx];
64 if (version->hash == 0)
65 version = NULL;
68 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
69 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
70 DL_LOOKUP_ADD_DEPENDENCY, NULL);
72 else
74 /* We already found the symbol. The module (and therefore its load
75 address) is also known. */
76 result = l;
79 if (! sym)
81 td->arg = (void*)reloc->r_addend;
82 td->entry = _dl_tlsdesc_undefweak;
84 else
86 # ifndef SHARED
87 CHECK_STATIC_TLS (l, result);
88 # else
89 if (!TRY_STATIC_TLS (l, result))
91 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
92 + reloc->r_addend);
93 td->entry = _dl_tlsdesc_dynamic;
95 else
96 # endif
98 td->arg = (void*)(sym->st_value - result->l_tls_offset
99 + reloc->r_addend);
100 td->entry = _dl_tlsdesc_return;
104 _dl_tlsdesc_wake_up_held_fixups ();
107 /* This function is used to avoid busy waiting for other threads to
108 complete the lazy relocation. Once another thread wins the race to
109 relocate a TLS descriptor, it sets the descriptor up such that this
110 function is called to wait until the resolver releases the
111 lock. */
113 void
114 attribute_hidden
115 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
116 void *caller)
118 /* Maybe we're lucky and can return early. */
119 if (caller != td->entry)
120 return;
122 /* Locking here will stop execution until the running resolver runs
123 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
125 FIXME: We'd be better off waiting on a condition variable, such
126 that we didn't have to hold the lock throughout the relocation
127 processing. */
128 __rtld_lock_lock_recursive (GL(dl_load_lock));
129 __rtld_lock_unlock_recursive (GL(dl_load_lock));
132 /* Unmap the dynamic object, but also release its TLS descriptor table
133 if there is one. */
135 void
136 internal_function
137 _dl_unmap (struct link_map *map)
139 __munmap ((void *) (map)->l_map_start,
140 (map)->l_map_end - (map)->l_map_start);
142 #if SHARED
143 /* _dl_unmap is only called for dlopen()ed libraries, for which
144 calling free() is safe, or before we've completed the initial
145 relocation, in which case calling free() is probably pointless,
146 but still safe. */
147 if (map->l_mach.tlsdesc_table)
148 htab_delete (map->l_mach.tlsdesc_table);
149 #endif