1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <unistd.h>
18
19 #include "native_bridge_support/vdso/vdso.h"
20
21 extern "C" void __loader_add_thread_local_dtor(void* dso_handle) __attribute__((weak));
22 extern "C" void __loader_remove_thread_local_dtor(void* dso_handle) __attribute__((weak));
23
24 struct WrappedArg {
25 typedef void (*thread_atexit_fn_t)(void*);
26 thread_atexit_fn_t fn;
27 void* arg;
28 void* dso_handle;
29 };
30
WrappedFn(void * arg)31 static void WrappedFn(void* arg) {
32 WrappedArg* wrapped_arg = static_cast<WrappedArg*>(arg);
33 WrappedArg::thread_atexit_fn_t origin_fn = wrapped_arg->fn;
34 void* origin_arg = wrapped_arg->arg;
35 void* dso_handle = wrapped_arg->dso_handle;
36
37 delete wrapped_arg;
38
39 origin_fn(origin_arg);
40
41 if (__loader_remove_thread_local_dtor != nullptr) {
42 __loader_remove_thread_local_dtor(dso_handle);
43 }
44 }
45
__cxa_thread_atexit_impl(void (* func)(void *),void * arg,void * dso_handle)46 extern "C" int __cxa_thread_atexit_impl(void (*func)(void*), void* arg, void* dso_handle) {
47 WrappedArg* wrapped_arg = new WrappedArg();
48 wrapped_arg->fn = func;
49 wrapped_arg->arg = arg;
50 wrapped_arg->dso_handle = dso_handle;
51
52 if (__loader_add_thread_local_dtor != nullptr) {
53 __loader_add_thread_local_dtor(dso_handle);
54 }
55
56 typedef decltype(__cxa_thread_atexit_impl)* fn_t;
57 static fn_t __host_cxa_thread_atexit_impl = reinterpret_cast<fn_t>(
58 native_bridge_find_proxy_library_symbol("libc.so", "__cxa_thread_atexit_impl"));
59
60 return __host_cxa_thread_atexit_impl(WrappedFn, wrapped_arg, dso_handle);
61 }
62