[go: nahoru, domu]

Skip to content

Commit

Permalink
backport https://reviews.llvm.org/D107127 to fix shared libraries usi…
Browse files Browse the repository at this point in the history
…ng emutls

Our emulated TLS implementation relies on local state (e.g. for the pthread
key), and if we duplicate this state across different shared libraries,
accesses to the same TLS variable from different shared libraries will yield
different results (see android/ndk#1551 for an
example). __emutls_get_address is the only external entry point for emulated
TLS, and by making it default visibility and weak, we can rely on the dynamic
linker to coalesce multiple copies at runtime and ensure a single unique copy
of TLS state. This is a best effort;

Also bump the libc++abi minor because now it picks up the __emutls_get_address
symbol.

ok kettenis@
  • Loading branch information
rnagy authored and mordak committed Nov 13, 2021
1 parent 94c957c commit 84a48c9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler-rt/lib/builtins/emutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,21 @@ emutls_get_address_array(uintptr_t index) {
return array;
}

#ifndef _WIN32
// Our emulated TLS implementation relies on local state (e.g. for the pthread
// key), and if we duplicate this state across different shared libraries,
// accesses to the same TLS variable from different shared libraries will yield
// different results (see https://github.com/android/ndk/issues/1551 for an
// example). __emutls_get_address is the only external entry point for emulated
// TLS, and by making it default visibility and weak, we can rely on the dynamic
// linker to coalesce multiple copies at runtime and ensure a single unique copy
// of TLS state. This is a best effort; it won't work if the user is linking
// with -Bsymbolic or -Bsymbolic-functions, and it also won't work on Windows,
// where the dynamic linker has no notion of coalescing weak symbols at runtime.
// A more robust solution would be to create a separate shared library for
// emulated TLS, to ensure a single copy of its state.
__attribute__((visibility("default"), weak))
#endif
void *__emutls_get_address(__emutls_control *control) {
uintptr_t index = emutls_get_index(control);
emutls_address_array *array = emutls_get_address_array(index--);
Expand Down

0 comments on commit 84a48c9

Please sign in to comment.