]>
Commit | Line | Data |
---|---|---|
c9ff0187 | 1 | /* Manage TLS descriptors. x86_64 version. |
d4697bc9 | 2 | Copyright (C) 2005-2014 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> | |
24 | #include <tlsdeschtab.h> | |
25 | ||
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. */ | |
29 | ||
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. */ | |
32 | ||
33 | void | |
34 | attribute_hidden | |
35 | _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td, | |
36 | struct link_map *l) | |
37 | { | |
38 | const ElfW(Rela) *reloc = td->arg; | |
39 | ||
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; | |
43 | ||
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; | |
50 | ||
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) | |
55 | { | |
56 | const struct r_found_version *version = NULL; | |
57 | ||
58 | if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL) | |
59 | { | |
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; | |
66 | } | |
67 | ||
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); | |
71 | } | |
72 | else | |
73 | { | |
74 | /* We already found the symbol. The module (and therefore its load | |
75 | address) is also known. */ | |
76 | result = l; | |
77 | } | |
78 | ||
79 | if (! sym) | |
80 | { | |
81 | td->arg = (void*)reloc->r_addend; | |
82 | td->entry = _dl_tlsdesc_undefweak; | |
83 | } | |
84 | else | |
85 | { | |
86 | # ifndef SHARED | |
87 | CHECK_STATIC_TLS (l, result); | |
88 | # else | |
89 | if (!TRY_STATIC_TLS (l, result)) | |
90 | { | |
91 | td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value | |
92 | + reloc->r_addend); | |
93 | td->entry = _dl_tlsdesc_dynamic; | |
94 | } | |
95 | else | |
96 | # endif | |
97 | { | |
98 | td->arg = (void*)(sym->st_value - result->l_tls_offset | |
99 | + reloc->r_addend); | |
100 | td->entry = _dl_tlsdesc_return; | |
101 | } | |
102 | } | |
103 | ||
104 | _dl_tlsdesc_wake_up_held_fixups (); | |
105 | } | |
106 | ||
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. */ | |
112 | ||
113 | void | |
114 | attribute_hidden | |
115 | _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td, | |
116 | void *caller) | |
117 | { | |
118 | /* Maybe we're lucky and can return early. */ | |
119 | if (caller != td->entry) | |
120 | return; | |
121 | ||
122 | /* Locking here will stop execution until the running resolver runs | |
123 | _dl_tlsdesc_wake_up_held_fixups(), releasing the lock. | |
124 | ||
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)); | |
130 | } | |
131 | ||
132 | /* Unmap the dynamic object, but also release its TLS descriptor table | |
133 | if there is one. */ | |
134 | ||
135 | void | |
136 | internal_function | |
137 | _dl_unmap (struct link_map *map) | |
138 | { | |
139 | __munmap ((void *) (map)->l_map_start, | |
140 | (map)->l_map_end - (map)->l_map_start); | |
141 | ||
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 | |
150 | } |