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