• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 <dlfcn.h>
18 #include <link.h>
19 #include <stdlib.h>
20 #include <android/dlext.h>
21 
22 // These functions are exported by the loader
23 // TODO(dimitry): replace these with reference to libc.so
24 
25 extern "C" {
26 
27 __attribute__((__weak__, visibility("default")))
28 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr);
29 
30 __attribute__((__weak__, visibility("default")))
31 char* __loader_dlerror();
32 
33 __attribute__((__weak__, visibility("default")))
34 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr);
35 
36 __attribute__((__weak__, visibility("default")))
37 void* __loader_dlvsym(void* handle,
38                       const char* symbol,
39                       const char* version,
40                       const void* caller_addr);
41 
42 __attribute__((__weak__, visibility("default")))
43 int __loader_dladdr(const void* addr, Dl_info* info);
44 
45 __attribute__((__weak__, visibility("default")))
46 int __loader_dlclose(void* handle);
47 
48 #if defined(__arm__)
49 __attribute__((__weak__, visibility("default")))
50 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
51 #endif
52 
53 __attribute__((__weak__, visibility("default")))
54 int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data),
55                              void* data);
56 
57 __attribute__((__weak__, visibility("default")))
58 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size);
59 
60 __attribute__((__weak__, visibility("default")))
61 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
62 
63 __attribute__((__weak__, visibility("default")))
64 void* __loader_android_dlopen_ext(const char* filename,
65                                   int flag,
66                                   const android_dlextinfo* extinfo,
67                                   const void* caller_addr);
68 
69 __attribute__((__weak__, visibility("default")))
70 void __loader_android_set_application_target_sdk_version(uint32_t target);
71 
72 __attribute__((__weak__, visibility("default")))
73 uint32_t __loader_android_get_application_target_sdk_version();
74 
75 __attribute__((__weak__, visibility("default")))
76 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
77                                                const char* library_search_path);
78 
79 __attribute__((__weak__, visibility("default")))
80 struct android_namespace_t* __loader_android_create_namespace(
81                                 const char* name,
82                                 const char* ld_library_path,
83                                 const char* default_library_path,
84                                 uint64_t type,
85                                 const char* permitted_when_isolated_path,
86                                 struct android_namespace_t* parent,
87                                 const void* caller_addr);
88 
89 __attribute__((__weak__, visibility("default")))
90 bool __loader_android_link_namespaces(
91                                 struct android_namespace_t* namespace_from,
92                                 struct android_namespace_t* namespace_to,
93                                 const char* shared_libs_sonames);
94 
95 __attribute__((__weak__, visibility("default")))
96 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*));
97 
98 __attribute__((__weak__, visibility("default")))
99 struct android_namespace_t* __loader_android_get_exported_namespace(const char* name);
100 
101 // Proxy calls to bionic loader
102 __attribute__((__weak__))
dlopen(const char * filename,int flag)103 void* dlopen(const char* filename, int flag) {
104   const void* caller_addr = __builtin_return_address(0);
105   return __loader_dlopen(filename, flag, caller_addr);
106 }
107 
108 __attribute__((__weak__))
dlerror()109 char* dlerror() {
110   return __loader_dlerror();
111 }
112 
113 __attribute__((__weak__))
dlsym(void * handle,const char * symbol)114 void* dlsym(void* handle, const char* symbol) {
115   const void* caller_addr = __builtin_return_address(0);
116   return __loader_dlsym(handle, symbol, caller_addr);
117 }
118 
119 __attribute__((__weak__))
dlvsym(void * handle,const char * symbol,const char * version)120 void* dlvsym(void* handle, const char* symbol, const char* version) {
121   const void* caller_addr = __builtin_return_address(0);
122   return __loader_dlvsym(handle, symbol, version, caller_addr);
123 }
124 
125 __attribute__((__weak__))
dladdr(const void * addr,Dl_info * info)126 int dladdr(const void* addr, Dl_info* info) {
127   return __loader_dladdr(addr, info);
128 }
129 
130 __attribute__((__weak__))
dlclose(void * handle)131 int dlclose(void* handle) {
132   return __loader_dlclose(handle);
133 }
134 
135 #if defined(__arm__)
136 __attribute__((__weak__))
dl_unwind_find_exidx(_Unwind_Ptr pc,int * pcount)137 _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
138   return __loader_dl_unwind_find_exidx(pc, pcount);
139 }
140 #endif
141 
142 /*
143  * This needs to be defined as weak because it is also defined in libc.a.
144  * Without this, static executables will have a multiple definition error.
145  */
146 __attribute__((__weak__))
dl_iterate_phdr(int (* cb)(struct dl_phdr_info * info,size_t size,void * data),void * data)147 int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) {
148   return __loader_dl_iterate_phdr(cb, data);
149 }
150 
151 __attribute__((__weak__))
android_get_LD_LIBRARY_PATH(char * buffer,size_t buffer_size)152 void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
153   __loader_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
154 }
155 
156 __attribute__((__weak__))
android_update_LD_LIBRARY_PATH(const char * ld_library_path)157 void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
158   __loader_android_update_LD_LIBRARY_PATH(ld_library_path);
159 }
160 
161 __attribute__((__weak__))
android_dlopen_ext(const char * filename,int flag,const android_dlextinfo * extinfo)162 void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo) {
163   const void* caller_addr = __builtin_return_address(0);
164   return __loader_android_dlopen_ext(filename, flag, extinfo, caller_addr);
165 }
166 
167 __attribute__((__weak__))
android_set_application_target_sdk_version(uint32_t target)168 void android_set_application_target_sdk_version(uint32_t target) {
169   __loader_android_set_application_target_sdk_version(target);
170 }
171 
172 __attribute__((__weak__))
android_get_application_target_sdk_version()173 uint32_t android_get_application_target_sdk_version() {
174   return __loader_android_get_application_target_sdk_version();
175 }
176 
177 __attribute__((__weak__))
android_init_anonymous_namespace(const char * shared_libs_sonames,const char * library_search_path)178 bool android_init_anonymous_namespace(const char* shared_libs_sonames,
179                                       const char* library_search_path) {
180   return __loader_android_init_anonymous_namespace(shared_libs_sonames, library_search_path);
181 }
182 
183 __attribute__((__weak__))
android_create_namespace(const char * name,const char * ld_library_path,const char * default_library_path,uint64_t type,const char * permitted_when_isolated_path,struct android_namespace_t * parent)184 struct android_namespace_t* android_create_namespace(const char* name,
185                                                      const char* ld_library_path,
186                                                      const char* default_library_path,
187                                                      uint64_t type,
188                                                      const char* permitted_when_isolated_path,
189                                                      struct android_namespace_t* parent) {
190   const void* caller_addr = __builtin_return_address(0);
191   return __loader_android_create_namespace(name,
192                                            ld_library_path,
193                                            default_library_path,
194                                            type,
195                                            permitted_when_isolated_path,
196                                            parent,
197                                            caller_addr);
198 }
199 
200 __attribute__((__weak__))
android_link_namespaces(struct android_namespace_t * namespace_from,struct android_namespace_t * namespace_to,const char * shared_libs_sonames)201 bool android_link_namespaces(struct android_namespace_t* namespace_from,
202                              struct android_namespace_t* namespace_to,
203                              const char* shared_libs_sonames) {
204   return __loader_android_link_namespaces(namespace_from, namespace_to, shared_libs_sonames);
205 }
206 
207 __attribute__((__weak__))
android_dlwarning(void * obj,void (* f)(void *,const char *))208 void android_dlwarning(void* obj, void (*f)(void*, const char*)) {
209   __loader_android_dlwarning(obj, f);
210 }
211 
212 __attribute__((__weak__))
android_get_exported_namespace(const char * name)213 struct android_namespace_t* android_get_exported_namespace(const char* name) {
214   return __loader_android_get_exported_namespace(name);
215 }
216 
217 #if defined(__arm__)
218 // An arm32 unwinding table has an R_ARM_NONE relocation to
219 // __aeabi_unwind_cpp_pr0. This shared library will never invoke the unwinder,
220 // so it doesn't actually need the routine. Define a dummy version here,
221 // because the real version calls libc functions (e.g. memcpy, abort), which
222 // would create a dependency cycle with libc.so.
223 __attribute__((visibility("hidden")))
__aeabi_unwind_cpp_pr0()224 void __aeabi_unwind_cpp_pr0() {
225   __builtin_trap();
226 }
227 #endif
228 
229 } // extern "C"
230