Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / aarch64 / tlsdesc.c
blob4821f8c08a83dfc3d1b251c0d99a7b7e283203e8
1 /* Manage TLS descriptors. AArch64 version.
3 Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #include <link.h>
22 #include <ldsodefs.h>
23 #include <elf/dynamic-link.h>
24 #include <tls.h>
25 #include <dl-tlsdesc.h>
26 #include <dl-unmap-segments.h>
27 #include <tlsdeschtab.h>
29 /* The following functions take an entry_check_offset argument. It's
30 computed by the caller as an offset between its entry point and the
31 call site, such that by adding the built-in return address that is
32 implicitly passed to the function with this offset, we can easily
33 obtain the caller's entry point to compare with the entry point
34 given in the TLS descriptor. If it's changed, we want to return
35 immediately. */
37 /* This function is used to lazily resolve TLS_DESC RELA relocations.
38 The argument location is used to hold a pointer to the relocation. */
40 void
41 attribute_hidden
42 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
43 struct link_map *l)
45 const ElfW(Rela) *reloc = td->arg;
47 if (_dl_tlsdesc_resolve_early_return_p
48 (td, (void*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_PLT)]) + l->l_addr)))
49 return;
51 /* The code below was borrowed from _dl_fixup(),
52 except for checking for STB_LOCAL. */
53 const ElfW(Sym) *const symtab
54 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
55 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
56 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
57 lookup_t result;
59 /* Look up the target symbol. If the normal lookup rules are not
60 used don't look in the global scope. */
61 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
62 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
64 const struct r_found_version *version = NULL;
66 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
68 const ElfW(Half) *vernum =
69 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
70 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
71 version = &l->l_versions[ndx];
72 if (version->hash == 0)
73 version = NULL;
76 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
77 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
78 DL_LOOKUP_ADD_DEPENDENCY, NULL);
80 else
82 /* We already found the symbol. The module (and therefore its load
83 address) is also known. */
84 result = l;
87 if (!sym)
89 td->arg = (void*) reloc->r_addend;
90 td->entry = _dl_tlsdesc_undefweak;
92 else
94 # ifndef SHARED
95 CHECK_STATIC_TLS (l, result);
96 # else
97 if (!TRY_STATIC_TLS (l, result))
99 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
100 + reloc->r_addend);
101 td->entry = _dl_tlsdesc_dynamic;
103 else
104 # endif
106 td->arg = (void*) (sym->st_value + result->l_tls_offset
107 + reloc->r_addend);
108 td->entry = _dl_tlsdesc_return;
112 _dl_tlsdesc_wake_up_held_fixups ();
115 /* This function is used to avoid busy waiting for other threads to
116 complete the lazy relocation. Once another thread wins the race to
117 relocate a TLS descriptor, it sets the descriptor up such that this
118 function is called to wait until the resolver releases the
119 lock. */
121 void
122 attribute_hidden
123 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
124 void *caller)
126 /* Maybe we're lucky and can return early. */
127 if (caller != td->entry)
128 return;
130 /* Locking here will stop execution until the running resolver runs
131 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
133 FIXME: We'd be better off waiting on a condition variable, such
134 that we didn't have to hold the lock throughout the relocation
135 processing. */
136 __rtld_lock_lock_recursive (GL(dl_load_lock));
137 __rtld_lock_unlock_recursive (GL(dl_load_lock));
141 /* Unmap the dynamic object, but also release its TLS descriptor table
142 if there is one. */
144 void
145 internal_function
146 _dl_unmap (struct link_map *map)
148 _dl_unmap_segments (map);
150 #ifdef SHARED
151 if (map->l_mach.tlsdesc_table)
152 htab_delete (map->l_mach.tlsdesc_table);
153 #endif