1 /* Manage TLS descriptors. x86_64 version.
2 Copyright (C) 2005-2017 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/>. */
21 #include <elf/dynamic-link.h>
23 #include <dl-tlsdesc.h>
24 #include <dl-unmap-segments.h>
25 #include <tlsdeschtab.h>
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. */
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. */
36 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc
volatile *td
,
39 const ElfW(Rela
) *reloc
= td
->arg
;
41 if (_dl_tlsdesc_resolve_early_return_p
42 (td
, (void*)(D_PTR (l
, l_info
[ADDRIDX (DT_TLSDESC_PLT
)]) + l
->l_addr
)))
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
)];
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)
57 const struct r_found_version
*version
= NULL
;
59 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
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)
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
);
75 /* We already found the symbol. The module (and therefore its load
76 address) is also known. */
82 td
->arg
= (void*)reloc
->r_addend
;
83 td
->entry
= _dl_tlsdesc_undefweak
;
88 CHECK_STATIC_TLS (l
, result
);
90 if (!TRY_STATIC_TLS (l
, result
))
92 td
->arg
= _dl_make_tlsdesc_dynamic (result
, sym
->st_value
94 td
->entry
= _dl_tlsdesc_dynamic
;
99 td
->arg
= (void*)(sym
->st_value
- result
->l_tls_offset
101 td
->entry
= _dl_tlsdesc_return
;
105 _dl_tlsdesc_wake_up_held_fixups ();
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
116 _dl_tlsdesc_resolve_hold_fixup (struct tlsdesc
volatile *td
,
119 /* Maybe we're lucky and can return early. */
120 if (caller
!= td
->entry
)
123 /* Locking here will stop execution until the running resolver runs
124 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
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
129 __rtld_lock_lock_recursive (GL(dl_load_lock
));
130 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
133 /* Unmap the dynamic object, but also release its TLS descriptor table
137 _dl_unmap (struct link_map
*map
)
139 _dl_unmap_segments (map
);
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,
146 if (map
->l_mach
.tlsdesc_table
)
147 htab_delete (map
->l_mach
.tlsdesc_table
);