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 <stdbool.h>
21 #include <android/dlext.h>
22 
23 // These functions are exported by the loader
24 // TODO(dimitry): replace these with reference to libc.so
25 
26 __attribute__((__weak__, visibility("default")))
27 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr);
28 
29 __attribute__((__weak__, visibility("default")))
30 void* __loader_dlerror();
31 
32 __attribute__((__weak__, visibility("default")))
33 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr);
34 
35 __attribute__((__weak__, visibility("default")))
36 void* __loader_dlvsym(void* handle,
37                       const char* symbol,
38                       const char* version,
39                       const void* caller_addr);
40 
41 __attribute__((__weak__, visibility("default")))
42 int __loader_dladdr(const void* addr, Dl_info* info);
43 
44 __attribute__((__weak__, visibility("default")))
45 int __loader_dlclose(void* handle);
46 
47 #if defined(__arm__)
48 __attribute__((__weak__, visibility("default")))
49 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
50 #endif
51 
52 __attribute__((__weak__, visibility("default")))
53 int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data),
54                              void* data);
55 
56 __attribute__((__weak__, visibility("default")))
57 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size);
58 
59 __attribute__((__weak__, visibility("default")))
60 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
61 
62 __attribute__((__weak__, visibility("default")))
63 void* __loader_android_dlopen_ext(const char* filename,
64                                   int flag,
65                                   const android_dlextinfo* extinfo,
66                                   const void* caller_addr);
67 
68 __attribute__((__weak__, visibility("default")))
69 void __loader_android_set_application_target_sdk_version(uint32_t target);
70 
71 __attribute__((__weak__, visibility("default")))
72 uint32_t __loader_android_get_application_target_sdk_version();
73 
74 __attribute__((__weak__, visibility("default")))
75 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
76                                                const char* library_search_path);
77 
78 __attribute__((__weak__, visibility("default")))
79 struct android_namespace_t* __loader_android_create_namespace(
80                                 const char* name,
81                                 const char* ld_library_path,
82                                 const char* default_library_path,
83                                 uint64_t type,
84                                 const char* permitted_when_isolated_path,
85                                 struct android_namespace_t* parent,
86                                 const void* caller_addr);
87 
88 __attribute__((__weak__, visibility("default")))
89 bool __loader_android_link_namespaces(
90                                 struct android_namespace_t* namespace_from,
91                                 struct android_namespace_t* namespace_to,
92                                 const char* shared_libs_sonames);
93 
94 __attribute__((__weak__, visibility("default")))
95 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*));
96 
97 __attribute__((__weak__, visibility("default")))
98 struct android_namespace_t* __loader_android_get_exported_namespace(const char* name);
99 
100 // Proxy calls to bionic loader
dlopen(const char * filename,int flag)101 void* dlopen(const char* filename, int flag) {
102   const void* caller_addr = __builtin_return_address(0);
103   return __loader_dlopen(filename, flag, caller_addr);
104 }
105 
dlerror()106 char* dlerror() {
107   return __loader_dlerror();
108 }
109 
dlsym(void * handle,const char * symbol)110 void* dlsym(void* handle, const char* symbol) {
111   const void* caller_addr = __builtin_return_address(0);
112   return __loader_dlsym(handle, symbol, caller_addr);
113 }
114 
dlvsym(void * handle,const char * symbol,const char * version)115 void* dlvsym(void* handle, const char* symbol, const char* version) {
116   const void* caller_addr = __builtin_return_address(0);
117   return __loader_dlvsym(handle, symbol, version, caller_addr);
118 }
119 
dladdr(const void * addr,Dl_info * info)120 int dladdr(const void* addr, Dl_info* info) {
121   return __loader_dladdr(addr, info);
122 }
123 
dlclose(void * handle)124 int dlclose(void* handle) {
125   return __loader_dlclose(handle);
126 }
127 
128 #if defined(__arm__)
dl_unwind_find_exidx(_Unwind_Ptr pc,int * pcount)129 _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
130   return __loader_dl_unwind_find_exidx(pc, pcount);
131 }
132 #endif
133 
dl_iterate_phdr(int (* cb)(struct dl_phdr_info * info,size_t size,void * data),void * data)134 int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) {
135   return __loader_dl_iterate_phdr(cb, data);
136 }
137 
android_get_LD_LIBRARY_PATH(char * buffer,size_t buffer_size)138 void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
139   __loader_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
140 }
141 
android_update_LD_LIBRARY_PATH(const char * ld_library_path)142 void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
143   __loader_android_update_LD_LIBRARY_PATH(ld_library_path);
144 }
145 
android_dlopen_ext(const char * filename,int flag,const android_dlextinfo * extinfo)146 void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo) {
147   const void* caller_addr = __builtin_return_address(0);
148   return __loader_android_dlopen_ext(filename, flag, extinfo, caller_addr);
149 }
150 
android_set_application_target_sdk_version(uint32_t target)151 void android_set_application_target_sdk_version(uint32_t target) {
152   __loader_android_set_application_target_sdk_version(target);
153 }
android_get_application_target_sdk_version()154 uint32_t android_get_application_target_sdk_version() {
155   return __loader_android_get_application_target_sdk_version();
156 }
157 
android_init_anonymous_namespace(const char * shared_libs_sonames,const char * library_search_path)158 bool android_init_anonymous_namespace(const char* shared_libs_sonames,
159                                       const char* library_search_path) {
160   return __loader_android_init_anonymous_namespace(shared_libs_sonames, library_search_path);
161 }
162 
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)163 struct android_namespace_t* android_create_namespace(const char* name,
164                                                      const char* ld_library_path,
165                                                      const char* default_library_path,
166                                                      uint64_t type,
167                                                      const char* permitted_when_isolated_path,
168                                                      struct android_namespace_t* parent) {
169   const void* caller_addr = __builtin_return_address(0);
170   return __loader_android_create_namespace(name,
171                                            ld_library_path,
172                                            default_library_path,
173                                            type,
174                                            permitted_when_isolated_path,
175                                            parent,
176                                            caller_addr);
177 }
178 
android_link_namespaces(struct android_namespace_t * namespace_from,struct android_namespace_t * namespace_to,const char * shared_libs_sonames)179 bool android_link_namespaces(struct android_namespace_t* namespace_from,
180                              struct android_namespace_t* namespace_to,
181                              const char* shared_libs_sonames) {
182   return __loader_android_link_namespaces(namespace_from, namespace_to, shared_libs_sonames);
183 }
184 
android_dlwarning(void * obj,void (* f)(void *,const char *))185 void android_dlwarning(void* obj, void (*f)(void*, const char*)) {
186   __loader_android_dlwarning(obj, f);
187 }
188 
android_get_exported_namespace(const char * name)189 struct android_namespace_t* android_get_exported_namespace(const char* name) {
190   return __loader_android_get_exported_namespace(name);
191 }
192