| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Manage TLS descriptors.  ARM version. | 
|  | 2 | Copyright (C) 2005-2016 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 | <http://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 | /* This function is used to lazily resolve TLS_DESC REL relocations | 
|  | 28 | Besides the TLS descriptor itself, we get the module's got address | 
|  | 29 | as the second parameter. */ | 
|  | 30 |  | 
|  | 31 | void | 
|  | 32 | attribute_hidden | 
|  | 33 | _dl_tlsdesc_lazy_resolver_fixup (struct tlsdesc volatile *td, | 
|  | 34 | Elf32_Addr *got) | 
|  | 35 | { | 
|  | 36 | struct link_map *l = (struct link_map *)got[1]; | 
|  | 37 | lookup_t result; | 
|  | 38 | unsigned long value; | 
|  | 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 | if (td->argument.value & 0x80000000) | 
|  | 45 | { | 
|  | 46 | /* A global symbol, this is the symbol index.  */ | 
|  | 47 | /* The code below was borrowed from _dl_fixup().  */ | 
|  | 48 | const Elf_Symndx symndx = td->argument.value ^ 0x80000000; | 
|  | 49 | const ElfW(Sym) *const symtab | 
|  | 50 | = (const void *) D_PTR (l, l_info[DT_SYMTAB]); | 
|  | 51 | const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]); | 
|  | 52 | const ElfW(Sym) *sym = &symtab[symndx]; | 
|  | 53 |  | 
|  | 54 | /* Look up the target symbol.  If the normal lookup rules are not | 
|  | 55 | used don't look in the global scope.  */ | 
|  | 56 | if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL | 
|  | 57 | && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0) | 
|  | 58 | { | 
|  | 59 | const struct r_found_version *version = NULL; | 
|  | 60 |  | 
|  | 61 | if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL) | 
|  | 62 | { | 
|  | 63 | const ElfW(Half) *vernum = | 
|  | 64 | (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]); | 
|  | 65 | ElfW(Half) ndx = vernum[symndx] & 0x7fff; | 
|  | 66 | version = &l->l_versions[ndx]; | 
|  | 67 | if (version->hash == 0) | 
|  | 68 | version = NULL; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | result = _dl_lookup_symbol_x | 
|  | 72 | (strtab + sym->st_name, l, &sym, | 
|  | 73 | l->l_scope, version, ELF_RTYPE_CLASS_PLT, | 
|  | 74 | DL_LOOKUP_ADD_DEPENDENCY, NULL); | 
|  | 75 | if (sym) | 
|  | 76 | value = sym->st_value; | 
|  | 77 | else | 
|  | 78 | { | 
|  | 79 | td->entry = _dl_tlsdesc_undefweak; | 
|  | 80 | goto done; | 
|  | 81 | } | 
|  | 82 | } | 
|  | 83 | else | 
|  | 84 | { | 
|  | 85 | /* We already found the symbol.  The module (and therefore its load | 
|  | 86 | address) is also known.  */ | 
|  | 87 | result = l; | 
|  | 88 | value = sym->st_value; | 
|  | 89 | } | 
|  | 90 | } | 
|  | 91 | else | 
|  | 92 | { | 
|  | 93 | /* A local symbol, this is the offset within our tls section. | 
|  | 94 | */ | 
|  | 95 | value = td->argument.value; | 
|  | 96 | result = l; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | #ifndef SHARED | 
|  | 100 | CHECK_STATIC_TLS (l, result); | 
|  | 101 | #else | 
|  | 102 | if (!TRY_STATIC_TLS (l, result)) | 
|  | 103 | { | 
|  | 104 | td->argument.pointer = _dl_make_tlsdesc_dynamic (result, value); | 
|  | 105 | td->entry = _dl_tlsdesc_dynamic; | 
|  | 106 | } | 
|  | 107 | else | 
|  | 108 | #endif | 
|  | 109 | { | 
|  | 110 | td->argument.value = value + result->l_tls_offset; | 
|  | 111 | td->entry = _dl_tlsdesc_return; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | done: | 
|  | 115 | _dl_tlsdesc_wake_up_held_fixups (); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | /* This function is used to avoid busy waiting for other threads to | 
|  | 119 | complete the lazy relocation.  Once another thread wins the race to | 
|  | 120 | relocate a TLS descriptor, it sets the descriptor up such that this | 
|  | 121 | function is called to wait until the resolver releases the | 
|  | 122 | lock.  */ | 
|  | 123 |  | 
|  | 124 | void | 
|  | 125 | attribute_hidden | 
|  | 126 | _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td, | 
|  | 127 | void *caller) | 
|  | 128 | { | 
|  | 129 | /* Maybe we're lucky and can return early.  */ | 
|  | 130 | if (caller != td->entry) | 
|  | 131 | return; | 
|  | 132 |  | 
|  | 133 | /* Locking here will stop execution until the running resolver runs | 
|  | 134 | _dl_tlsdesc_wake_up_held_fixups(), releasing the lock. | 
|  | 135 |  | 
|  | 136 | FIXME: We'd be better off waiting on a condition variable, such | 
|  | 137 | that we didn't have to hold the lock throughout the relocation | 
|  | 138 | processing.  */ | 
|  | 139 | __rtld_lock_lock_recursive (GL(dl_load_lock)); | 
|  | 140 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | /* Unmap the dynamic object, but also release its TLS descriptor table | 
|  | 144 | if there is one.  */ | 
|  | 145 |  | 
|  | 146 | void | 
|  | 147 | internal_function | 
|  | 148 | _dl_unmap (struct link_map *map) | 
|  | 149 | { | 
|  | 150 | _dl_unmap_segments (map); | 
|  | 151 |  | 
|  | 152 | #ifdef 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 | 
|  | 160 | } |