]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/i386/tlsdesc.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / i386 / tlsdesc.c
CommitLineData
c9ff0187 1/* Manage TLS descriptors. i386 version.
b168057a 2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
c9ff0187
UD
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
c9ff0187
UD
18
19#include <link.h>
20#include <ldsodefs.h>
21#include <elf/dynamic-link.h>
22#include <tls.h>
23#include <dl-tlsdesc.h>
fcccd512 24#include <dl-unmap-segments.h>
c9ff0187
UD
25#include <tlsdeschtab.h>
26
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. */
34
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. */
38
39void
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)
44{
45 ptrdiff_t addend = (ptrdiff_t) td->arg;
46
47 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
48 - entry_check_offset))
49 return;
50
51#ifndef SHARED
52 CHECK_STATIC_TLS (l, l);
53#else
54 if (!TRY_STATIC_TLS (l, l))
55 {
56 td->arg = _dl_make_tlsdesc_dynamic (l, addend);
57 td->entry = _dl_tlsdesc_dynamic;
58 }
59 else
60#endif
61 {
62 td->arg = (void*) (addend - l->l_tls_offset);
63 td->entry = _dl_tlsdesc_return;
64 }
65
66 _dl_tlsdesc_wake_up_held_fixups ();
67}
68
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. */
74
75void
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)
80{
81 const ElfW(Rel) *reloc = td->arg;
82
83 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
84 - entry_check_offset))
85 return;
86
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;
94
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)
99 {
100 const struct r_found_version *version = NULL;
101
102 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
103 {
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;
110 }
111
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);
115 }
116 else
117 {
118 /* We already found the symbol. The module (and therefore its load
119 address) is also known. */
120 result = l;
121 }
122
123 if (!sym)
124 {
125 td->arg = 0;
126 td->entry = _dl_tlsdesc_undefweak;
127 }
128 else
129 {
130# ifndef SHARED
131 CHECK_STATIC_TLS (l, result);
132# else
133 if (!TRY_STATIC_TLS (l, result))
134 {
135 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value);
136 td->entry = _dl_tlsdesc_dynamic;
137 }
138 else
139# endif
140 {
141 td->arg = (void*)(sym->st_value - result->l_tls_offset);
142 td->entry = _dl_tlsdesc_return;
143 }
144 }
145
146 _dl_tlsdesc_wake_up_held_fixups ();
147}
148
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. */
151
152void
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)
157{
158 const ElfW(Rela) *reloc = td->arg;
159
160 if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
161 - entry_check_offset))
162 return;
163
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;
171
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)
176 {
177 const struct r_found_version *version = NULL;
178
179 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
180 {
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;
187 }
188
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);
192 }
193 else
194 {
195 /* We already found the symbol. The module (and therefore its load
196 address) is also known. */
197 result = l;
198 }
199
200 if (!sym)
201 {
f05ac8f5 202 td->arg = (void*) reloc->r_addend;
c9ff0187
UD
203 td->entry = _dl_tlsdesc_undefweak;
204 }
205 else
206 {
207# ifndef SHARED
208 CHECK_STATIC_TLS (l, result);
209# else
210 if (!TRY_STATIC_TLS (l, result))
211 {
212 td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
213 + reloc->r_addend);
214 td->entry = _dl_tlsdesc_dynamic;
215 }
216 else
217# endif
218 {
f05ac8f5
UD
219 td->arg = (void*) (sym->st_value - result->l_tls_offset
220 + reloc->r_addend);
c9ff0187
UD
221 td->entry = _dl_tlsdesc_return;
222 }
223 }
224
225 _dl_tlsdesc_wake_up_held_fixups ();
226}
227
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. */
233
234void
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)
239{
240 /* Maybe we're lucky and can return early. */
241 if (__builtin_return_address (0) - entry_check_offset != td->entry)
242 return;
243
244 /* Locking here will stop execution until the running resolver runs
245 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
246
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));
252}
253
254
255/* Unmap the dynamic object, but also release its TLS descriptor table
256 if there is one. */
257
258void
259internal_function
260_dl_unmap (struct link_map *map)
261{
fcccd512 262 _dl_unmap_segments (map);
c9ff0187 263
82374e65 264#ifdef SHARED
c9ff0187
UD
265 if (map->l_mach.tlsdesc_table)
266 htab_delete (map->l_mach.tlsdesc_table);
267#endif
268}