1 /* Manage TLS descriptors. i386 version.
2 Copyright (C) 2005, 2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <elf/dynamic-link.h>
24 #include <dl-tlsdesc.h>
25 #include <tlsdeschtab.h>
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. */
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. */
40 __attribute__ ((regparm (3))) attribute_hidden
41 _dl_tlsdesc_resolve_abs_plus_addend_fixup (struct tlsdesc
volatile *td
,
43 ptrdiff_t entry_check_offset
)
45 ptrdiff_t addend
= (ptrdiff_t) td
->arg
;
47 if (_dl_tlsdesc_resolve_early_return_p (td
, __builtin_return_address (0)
48 - entry_check_offset
))
52 CHECK_STATIC_TLS (l
, l
);
54 if (!TRY_STATIC_TLS (l
, l
))
56 td
->arg
= _dl_make_tlsdesc_dynamic (l
, addend
);
57 td
->entry
= _dl_tlsdesc_dynamic
;
62 td
->arg
= (void*) (addend
- l
->l_tls_offset
);
63 td
->entry
= _dl_tlsdesc_return
;
66 _dl_tlsdesc_wake_up_held_fixups ();
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. */
76 __attribute__ ((regparm (3))) attribute_hidden
77 _dl_tlsdesc_resolve_rel_fixup (struct tlsdesc
volatile *td
,
79 ptrdiff_t entry_check_offset
)
81 const ElfW(Rel
) *reloc
= td
->arg
;
83 if (_dl_tlsdesc_resolve_early_return_p (td
, __builtin_return_address (0)
84 - entry_check_offset
))
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
)];
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)
100 const struct r_found_version
*version
= NULL
;
102 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
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)
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
);
118 /* We already found the symbol. The module (and therefore its load
119 address) is also known. */
126 td
->entry
= _dl_tlsdesc_undefweak
;
131 CHECK_STATIC_TLS (l
, result
);
133 if (!TRY_STATIC_TLS (l
, result
))
135 td
->arg
= _dl_make_tlsdesc_dynamic (result
, sym
->st_value
);
136 td
->entry
= _dl_tlsdesc_dynamic
;
141 td
->arg
= (void*)(sym
->st_value
- result
->l_tls_offset
);
142 td
->entry
= _dl_tlsdesc_return
;
146 _dl_tlsdesc_wake_up_held_fixups ();
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. */
153 __attribute__ ((regparm (3))) attribute_hidden
154 _dl_tlsdesc_resolve_rela_fixup (struct tlsdesc
volatile *td
,
156 ptrdiff_t entry_check_offset
)
158 const ElfW(Rela
) *reloc
= td
->arg
;
160 if (_dl_tlsdesc_resolve_early_return_p (td
, __builtin_return_address (0)
161 - entry_check_offset
))
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
)];
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)
177 const struct r_found_version
*version
= NULL
;
179 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
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)
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
);
195 /* We already found the symbol. The module (and therefore its load
196 address) is also known. */
202 td
->arg
= (void*) reloc
->r_addend
;
203 td
->entry
= _dl_tlsdesc_undefweak
;
208 CHECK_STATIC_TLS (l
, result
);
210 if (!TRY_STATIC_TLS (l
, result
))
212 td
->arg
= _dl_make_tlsdesc_dynamic (result
, sym
->st_value
214 td
->entry
= _dl_tlsdesc_dynamic
;
219 td
->arg
= (void*) (sym
->st_value
- result
->l_tls_offset
221 td
->entry
= _dl_tlsdesc_return
;
225 _dl_tlsdesc_wake_up_held_fixups ();
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
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
)
240 /* Maybe we're lucky and can return early. */
241 if (__builtin_return_address (0) - entry_check_offset
!= td
->entry
)
244 /* Locking here will stop execution until the running resolver runs
245 _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
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
250 __rtld_lock_lock_recursive (GL(dl_load_lock
));
251 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
255 /* Unmap the dynamic object, but also release its TLS descriptor table
260 _dl_unmap (struct link_map
*map
)
262 __munmap ((void *) (map
)->l_map_start
,
263 (map
)->l_map_end
- (map
)->l_map_start
);
266 if (map
->l_mach
.tlsdesc_table
)
267 htab_delete (map
->l_mach
.tlsdesc_table
);