Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / arm / tlsdesc.c
blobfc754d6c42876b5cfb743563100243922ff18a3d
1 /* Manage TLS descriptors. ARM version.
2 Copyright (C) 2005-2014 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 /* This function is used to lazily resolve TLS_DESC REL relocations
27 Besides the TLS descriptor itself, we get the module's got address
28 as the second parameter. */
30 void
31 attribute_hidden
32 _dl_tlsdesc_lazy_resolver_fixup (struct tlsdesc volatile *td,
33 Elf32_Addr *got)
35 struct link_map *l = (struct link_map *)got[1];
36 lookup_t result;
37 unsigned long value;
39 if (_dl_tlsdesc_resolve_early_return_p
40 (td, (void*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_PLT)]) + l->l_addr)))
41 return;
43 if (td->argument.value & 0x80000000)
45 /* A global symbol, this is the symbol index. */
46 /* The code below was borrowed from _dl_fixup(). */
47 const Elf_Symndx symndx = td->argument.value ^ 0x80000000;
48 const ElfW(Sym) *const symtab
49 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
50 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
51 const ElfW(Sym) *sym = &symtab[symndx];
53 /* Look up the target symbol. If the normal lookup rules are not
54 used don't look in the global scope. */
55 if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
56 && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
58 const struct r_found_version *version = NULL;
60 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
62 const ElfW(Half) *vernum =
63 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
64 ElfW(Half) ndx = vernum[symndx] & 0x7fff;
65 version = &l->l_versions[ndx];
66 if (version->hash == 0)
67 version = NULL;
70 result = _dl_lookup_symbol_x
71 (strtab + sym->st_name, l, &sym,
72 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
73 DL_LOOKUP_ADD_DEPENDENCY, NULL);
74 if (sym)
75 value = sym->st_value;
76 else
78 td->entry = _dl_tlsdesc_undefweak;
79 goto done;
82 else
84 /* We already found the symbol. The module (and therefore its load
85 address) is also known. */
86 result = l;
87 value = sym->st_value;
90 else
92 /* A local symbol, this is the offset within our tls section.
94 value = td->argument.value;
95 result = l;
98 #ifndef SHARED
99 CHECK_STATIC_TLS (l, result);
100 #else
101 if (!TRY_STATIC_TLS (l, result))
103 td->argument.pointer = _dl_make_tlsdesc_dynamic (result, value);
104 td->entry = _dl_tlsdesc_dynamic;
106 else
107 #endif
109 td->argument.value = value + result->l_tls_offset;
110 td->entry = _dl_tlsdesc_return;
113 done:
114 _dl_tlsdesc_wake_up_held_fixups ();
117 /* This function is used to avoid busy waiting for other threads to
118 complete the lazy relocation. Once another thread wins the race to
119 relocate a TLS descriptor, it sets the descriptor up such that this
120 function is called to wait until the resolver releases the
121 lock. */
123 void
124 attribute_hidden
125 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
126 void *caller)
128 /* Maybe we're lucky and can return early. */
129 if (caller != td->entry)
130 return;
132 /* Locking here will stop execution until the running resolver runs
133 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
135 FIXME: We'd be better off waiting on a condition variable, such
136 that we didn't have to hold the lock throughout the relocation
137 processing. */
138 __rtld_lock_lock_recursive (GL(dl_load_lock));
139 __rtld_lock_unlock_recursive (GL(dl_load_lock));
142 /* Unmap the dynamic object, but also release its TLS descriptor table
143 if there is one. */
145 void
146 internal_function
147 _dl_unmap (struct link_map *map)
149 __munmap ((void *) (map)->l_map_start,
150 (map)->l_map_end - (map)->l_map_start);
152 #if SHARED
153 /* _dl_unmap is only called for dlopen()ed libraries, for which
154 calling free() is safe, or before we've completed the initial
155 relocation, in which case calling free() is probably pointless,
156 but still safe. */
157 if (map->l_mach.tlsdesc_table)
158 htab_delete (map->l_mach.tlsdesc_table);
159 #endif