1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2007 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <android/dlext.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <dlfcn.h>
19*8d67ca89SAndroid Build Coastguard Worker #include <link.h>
20*8d67ca89SAndroid Build Coastguard Worker #include <signal.h>
21*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
22*8d67ca89SAndroid Build Coastguard Worker
23*8d67ca89SAndroid Build Coastguard Worker // These functions are exported by the loader
24*8d67ca89SAndroid Build Coastguard Worker // TODO(dimitry): replace these with reference to libc.so
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker extern "C" {
27*8d67ca89SAndroid Build Coastguard Worker
28*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
29*8d67ca89SAndroid Build Coastguard Worker void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size);
30*8d67ca89SAndroid Build Coastguard Worker
31*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
32*8d67ca89SAndroid Build Coastguard Worker void* __loader_dlopen(const char* filename, int flags, const void* caller_addr);
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
35*8d67ca89SAndroid Build Coastguard Worker char* __loader_dlerror();
36*8d67ca89SAndroid Build Coastguard Worker
37*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
38*8d67ca89SAndroid Build Coastguard Worker void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr);
39*8d67ca89SAndroid Build Coastguard Worker
40*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
41*8d67ca89SAndroid Build Coastguard Worker void* __loader_dlvsym(void* handle,
42*8d67ca89SAndroid Build Coastguard Worker const char* symbol,
43*8d67ca89SAndroid Build Coastguard Worker const char* version,
44*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr);
45*8d67ca89SAndroid Build Coastguard Worker
46*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
47*8d67ca89SAndroid Build Coastguard Worker int __loader_dladdr(const void* addr, Dl_info* info);
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
50*8d67ca89SAndroid Build Coastguard Worker int __loader_dlclose(void* handle);
51*8d67ca89SAndroid Build Coastguard Worker
52*8d67ca89SAndroid Build Coastguard Worker #if defined(__arm__)
53*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
54*8d67ca89SAndroid Build Coastguard Worker _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
55*8d67ca89SAndroid Build Coastguard Worker #endif
56*8d67ca89SAndroid Build Coastguard Worker
57*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
58*8d67ca89SAndroid Build Coastguard Worker int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data),
59*8d67ca89SAndroid Build Coastguard Worker void* data);
60*8d67ca89SAndroid Build Coastguard Worker
61*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
62*8d67ca89SAndroid Build Coastguard Worker void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size);
63*8d67ca89SAndroid Build Coastguard Worker
64*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
65*8d67ca89SAndroid Build Coastguard Worker void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
66*8d67ca89SAndroid Build Coastguard Worker
67*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
68*8d67ca89SAndroid Build Coastguard Worker void* __loader_android_dlopen_ext(const char* filename,
69*8d67ca89SAndroid Build Coastguard Worker int flag,
70*8d67ca89SAndroid Build Coastguard Worker const android_dlextinfo* extinfo,
71*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr);
72*8d67ca89SAndroid Build Coastguard Worker
73*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default")))
74*8d67ca89SAndroid Build Coastguard Worker int __loader_android_get_application_target_sdk_version();
75*8d67ca89SAndroid Build Coastguard Worker
76*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__, visibility("default"))) bool __loader_android_handle_signal(
77*8d67ca89SAndroid Build Coastguard Worker int signal_number, siginfo_t* info, void* context);
78*8d67ca89SAndroid Build Coastguard Worker
79*8d67ca89SAndroid Build Coastguard Worker // Proxy calls to bionic loader
80*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
android_get_LD_LIBRARY_PATH(char * buffer,size_t buffer_size)81*8d67ca89SAndroid Build Coastguard Worker void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
82*8d67ca89SAndroid Build Coastguard Worker __loader_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
83*8d67ca89SAndroid Build Coastguard Worker }
84*8d67ca89SAndroid Build Coastguard Worker
85*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dlopen(const char * filename,int flag)86*8d67ca89SAndroid Build Coastguard Worker void* dlopen(const char* filename, int flag) {
87*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr = __builtin_return_address(0);
88*8d67ca89SAndroid Build Coastguard Worker return __loader_dlopen(filename, flag, caller_addr);
89*8d67ca89SAndroid Build Coastguard Worker }
90*8d67ca89SAndroid Build Coastguard Worker
91*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dlerror()92*8d67ca89SAndroid Build Coastguard Worker char* dlerror() {
93*8d67ca89SAndroid Build Coastguard Worker return __loader_dlerror();
94*8d67ca89SAndroid Build Coastguard Worker }
95*8d67ca89SAndroid Build Coastguard Worker
96*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dlsym(void * handle,const char * symbol)97*8d67ca89SAndroid Build Coastguard Worker void* dlsym(void* handle, const char* symbol) {
98*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr = __builtin_return_address(0);
99*8d67ca89SAndroid Build Coastguard Worker return __loader_dlsym(handle, symbol, caller_addr);
100*8d67ca89SAndroid Build Coastguard Worker }
101*8d67ca89SAndroid Build Coastguard Worker
102*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dlvsym(void * handle,const char * symbol,const char * version)103*8d67ca89SAndroid Build Coastguard Worker void* dlvsym(void* handle, const char* symbol, const char* version) {
104*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr = __builtin_return_address(0);
105*8d67ca89SAndroid Build Coastguard Worker return __loader_dlvsym(handle, symbol, version, caller_addr);
106*8d67ca89SAndroid Build Coastguard Worker }
107*8d67ca89SAndroid Build Coastguard Worker
108*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dladdr(const void * addr,Dl_info * info)109*8d67ca89SAndroid Build Coastguard Worker int dladdr(const void* addr, Dl_info* info) {
110*8d67ca89SAndroid Build Coastguard Worker return __loader_dladdr(addr, info);
111*8d67ca89SAndroid Build Coastguard Worker }
112*8d67ca89SAndroid Build Coastguard Worker
113*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dlclose(void * handle)114*8d67ca89SAndroid Build Coastguard Worker int dlclose(void* handle) {
115*8d67ca89SAndroid Build Coastguard Worker return __loader_dlclose(handle);
116*8d67ca89SAndroid Build Coastguard Worker }
117*8d67ca89SAndroid Build Coastguard Worker
118*8d67ca89SAndroid Build Coastguard Worker #if defined(__arm__)
119*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dl_unwind_find_exidx(_Unwind_Ptr pc,int * pcount)120*8d67ca89SAndroid Build Coastguard Worker _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
121*8d67ca89SAndroid Build Coastguard Worker return __loader_dl_unwind_find_exidx(pc, pcount);
122*8d67ca89SAndroid Build Coastguard Worker }
123*8d67ca89SAndroid Build Coastguard Worker #endif
124*8d67ca89SAndroid Build Coastguard Worker
125*8d67ca89SAndroid Build Coastguard Worker /*
126*8d67ca89SAndroid Build Coastguard Worker * This needs to be defined as weak because it is also defined in libc.a.
127*8d67ca89SAndroid Build Coastguard Worker * Without this, static executables will have a multiple definition error.
128*8d67ca89SAndroid Build Coastguard Worker */
129*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
dl_iterate_phdr(int (* cb)(struct dl_phdr_info * info,size_t size,void * data),void * data)130*8d67ca89SAndroid Build Coastguard Worker int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) {
131*8d67ca89SAndroid Build Coastguard Worker return __loader_dl_iterate_phdr(cb, data);
132*8d67ca89SAndroid Build Coastguard Worker }
133*8d67ca89SAndroid Build Coastguard Worker
134*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
android_dlopen_ext(const char * filename,int flag,const android_dlextinfo * extinfo)135*8d67ca89SAndroid Build Coastguard Worker void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo) {
136*8d67ca89SAndroid Build Coastguard Worker const void* caller_addr = __builtin_return_address(0);
137*8d67ca89SAndroid Build Coastguard Worker return __loader_android_dlopen_ext(filename, flag, extinfo, caller_addr);
138*8d67ca89SAndroid Build Coastguard Worker }
139*8d67ca89SAndroid Build Coastguard Worker
140*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__))
android_get_application_target_sdk_version()141*8d67ca89SAndroid Build Coastguard Worker int android_get_application_target_sdk_version() {
142*8d67ca89SAndroid Build Coastguard Worker return __loader_android_get_application_target_sdk_version();
143*8d67ca89SAndroid Build Coastguard Worker }
144*8d67ca89SAndroid Build Coastguard Worker
145*8d67ca89SAndroid Build Coastguard Worker // Returns true if this function handled the signal, false if the caller should handle the signal
146*8d67ca89SAndroid Build Coastguard Worker // itself. This function returns true if the sigchain handler should immediately return, which
147*8d67ca89SAndroid Build Coastguard Worker // happens when the signal came from GWP-ASan, and we've dumped a debuggerd report and patched up
148*8d67ca89SAndroid Build Coastguard Worker // the GWP-ASan allocator to recover from the fault, and regular execution of the program can
149*8d67ca89SAndroid Build Coastguard Worker // continue.
android_handle_signal(int signal_number,siginfo_t * info,void * context)150*8d67ca89SAndroid Build Coastguard Worker __attribute__((__weak__)) bool android_handle_signal(int signal_number, siginfo_t* info,
151*8d67ca89SAndroid Build Coastguard Worker void* context) {
152*8d67ca89SAndroid Build Coastguard Worker return __loader_android_handle_signal(signal_number, info, context);
153*8d67ca89SAndroid Build Coastguard Worker }
154*8d67ca89SAndroid Build Coastguard Worker
155*8d67ca89SAndroid Build Coastguard Worker } // extern "C"
156