1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "linker.h"
30 #include "linker_cfi.h"
31 #include "linker_globals.h"
32 #include "linker_dlwarning.h"
33
34 #include <link.h>
35 #include <pthread.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <android/api-level.h>
40
41 #include <bionic/pthread_internal.h>
42 #include "private/bionic_globals.h"
43 #include "private/bionic_tls.h"
44 #include "private/ScopedPthreadMutexLocker.h"
45
46 #define __LINKER_PUBLIC__ __attribute__((visibility("default")))
47
48 extern "C" {
49
50 android_namespace_t* __loader_android_create_namespace(const char* name,
51 const char* ld_library_path,
52 const char* default_library_path,
53 uint64_t type,
54 const char* permitted_when_isolated_path,
55 android_namespace_t* parent_namespace,
56 const void* caller_addr) __LINKER_PUBLIC__;
57 void* __loader_android_dlopen_ext(const char* filename,
58 int flags,
59 const android_dlextinfo* extinfo,
60 const void* caller_addr) __LINKER_PUBLIC__;
61 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) __LINKER_PUBLIC__;
62 int __loader_android_get_application_target_sdk_version() __LINKER_PUBLIC__;
63 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) __LINKER_PUBLIC__;
64 android_namespace_t* __loader_android_get_exported_namespace(const char* name) __LINKER_PUBLIC__;
65 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
66 const char* library_search_path) __LINKER_PUBLIC__;
67 bool __loader_android_link_namespaces(android_namespace_t* namespace_from,
68 android_namespace_t* namespace_to,
69 const char* shared_libs_sonames) __LINKER_PUBLIC__;
70 bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
71 android_namespace_t* namespace_to) __LINKER_PUBLIC__;
72 void __loader_android_set_application_target_sdk_version(int target) __LINKER_PUBLIC__;
73 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) __LINKER_PUBLIC__;
74 void __loader_cfi_fail(uint64_t CallSiteTypeId,
75 void* Ptr,
76 void *DiagData,
77 void *CallerPc) __LINKER_PUBLIC__;
78 int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data),
79 void* data) __LINKER_PUBLIC__;
80 int __loader_dladdr(const void* addr, Dl_info* info) __LINKER_PUBLIC__;
81 int __loader_dlclose(void* handle) __LINKER_PUBLIC__;
82 char* __loader_dlerror() __LINKER_PUBLIC__;
83 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) __LINKER_PUBLIC__;
84 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) __LINKER_PUBLIC__;
85 void* __loader_dlvsym(void* handle,
86 const char* symbol,
87 const char* version,
88 const void* caller_addr) __LINKER_PUBLIC__;
89 void __loader_add_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
90 void __loader_remove_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
91 libc_shared_globals* __loader_shared_globals() __LINKER_PUBLIC__;
92 #if defined(__arm__)
93 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) __LINKER_PUBLIC__;
94 #endif
95 }
96
97 static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
98
__bionic_set_dlerror(char * new_value)99 static char* __bionic_set_dlerror(char* new_value) {
100 char* old_value = __get_thread()->current_dlerror;
101 __get_thread()->current_dlerror = new_value;
102
103 if (new_value != nullptr) LD_LOG(kLogErrors, "dlerror set to \"%s\"", new_value);
104 return old_value;
105 }
106
__bionic_format_dlerror(const char * msg,const char * detail)107 static void __bionic_format_dlerror(const char* msg, const char* detail) {
108 char* buffer = __get_thread()->dlerror_buffer;
109 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE);
110 if (detail != nullptr) {
111 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE);
112 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE);
113 }
114
115 __bionic_set_dlerror(buffer);
116 }
117
__loader_dlerror()118 char* __loader_dlerror() {
119 char* old_value = __bionic_set_dlerror(nullptr);
120 return old_value;
121 }
122
__loader_android_get_LD_LIBRARY_PATH(char * buffer,size_t buffer_size)123 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
124 ScopedPthreadMutexLocker locker(&g_dl_mutex);
125 do_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
126 }
127
__loader_android_update_LD_LIBRARY_PATH(const char * ld_library_path)128 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
129 ScopedPthreadMutexLocker locker(&g_dl_mutex);
130 do_android_update_LD_LIBRARY_PATH(ld_library_path);
131 }
132
dlopen_ext(const char * filename,int flags,const android_dlextinfo * extinfo,const void * caller_addr)133 static void* dlopen_ext(const char* filename,
134 int flags,
135 const android_dlextinfo* extinfo,
136 const void* caller_addr) {
137 ScopedPthreadMutexLocker locker(&g_dl_mutex);
138 g_linker_logger.ResetState();
139 void* result = do_dlopen(filename, flags, extinfo, caller_addr);
140 if (result == nullptr) {
141 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
142 return nullptr;
143 }
144 return result;
145 }
146
__loader_android_dlopen_ext(const char * filename,int flags,const android_dlextinfo * extinfo,const void * caller_addr)147 void* __loader_android_dlopen_ext(const char* filename,
148 int flags,
149 const android_dlextinfo* extinfo,
150 const void* caller_addr) {
151 return dlopen_ext(filename, flags, extinfo, caller_addr);
152 }
153
__loader_dlopen(const char * filename,int flags,const void * caller_addr)154 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) {
155 return dlopen_ext(filename, flags, nullptr, caller_addr);
156 }
157
dlsym_impl(void * handle,const char * symbol,const char * version,const void * caller_addr)158 void* dlsym_impl(void* handle, const char* symbol, const char* version, const void* caller_addr) {
159 ScopedPthreadMutexLocker locker(&g_dl_mutex);
160 g_linker_logger.ResetState();
161 void* result;
162 if (!do_dlsym(handle, symbol, version, caller_addr, &result)) {
163 __bionic_format_dlerror(linker_get_error_buffer(), nullptr);
164 return nullptr;
165 }
166
167 return result;
168 }
169
__loader_dlsym(void * handle,const char * symbol,const void * caller_addr)170 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) {
171 return dlsym_impl(handle, symbol, nullptr, caller_addr);
172 }
173
__loader_dlvsym(void * handle,const char * symbol,const char * version,const void * caller_addr)174 void* __loader_dlvsym(void* handle, const char* symbol, const char* version, const void* caller_addr) {
175 return dlsym_impl(handle, symbol, version, caller_addr);
176 }
177
__loader_dladdr(const void * addr,Dl_info * info)178 int __loader_dladdr(const void* addr, Dl_info* info) {
179 ScopedPthreadMutexLocker locker(&g_dl_mutex);
180 return do_dladdr(addr, info);
181 }
182
__loader_dlclose(void * handle)183 int __loader_dlclose(void* handle) {
184 ScopedPthreadMutexLocker locker(&g_dl_mutex);
185 int result = do_dlclose(handle);
186 if (result != 0) {
187 __bionic_format_dlerror("dlclose failed", linker_get_error_buffer());
188 }
189 return result;
190 }
191
__loader_dl_iterate_phdr(int (* cb)(dl_phdr_info * info,size_t size,void * data),void * data)192 int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
193 ScopedPthreadMutexLocker locker(&g_dl_mutex);
194 return do_dl_iterate_phdr(cb, data);
195 }
196
197 #if defined(__arm__)
__loader_dl_unwind_find_exidx(_Unwind_Ptr pc,int * pcount)198 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
199 ScopedPthreadMutexLocker locker(&g_dl_mutex);
200 return do_dl_unwind_find_exidx(pc, pcount);
201 }
202 #endif
203
__loader_android_set_application_target_sdk_version(int target)204 void __loader_android_set_application_target_sdk_version(int target) {
205 // lock to avoid modification in the middle of dlopen.
206 ScopedPthreadMutexLocker locker(&g_dl_mutex);
207 set_application_target_sdk_version(target);
208 }
209
__loader_android_get_application_target_sdk_version()210 int __loader_android_get_application_target_sdk_version() {
211 return get_application_target_sdk_version();
212 }
213
__loader_android_dlwarning(void * obj,void (* f)(void *,const char *))214 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) {
215 ScopedPthreadMutexLocker locker(&g_dl_mutex);
216 get_dlwarning(obj, f);
217 }
218
__loader_android_init_anonymous_namespace(const char * shared_libs_sonames,const char * library_search_path)219 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
220 const char* library_search_path) {
221 ScopedPthreadMutexLocker locker(&g_dl_mutex);
222 bool success = init_anonymous_namespace(shared_libs_sonames, library_search_path);
223 if (!success) {
224 __bionic_format_dlerror("android_init_anonymous_namespace failed", linker_get_error_buffer());
225 }
226
227 return success;
228 }
229
__loader_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,android_namespace_t * parent_namespace,const void * caller_addr)230 android_namespace_t* __loader_android_create_namespace(const char* name,
231 const char* ld_library_path,
232 const char* default_library_path,
233 uint64_t type,
234 const char* permitted_when_isolated_path,
235 android_namespace_t* parent_namespace,
236 const void* caller_addr) {
237 ScopedPthreadMutexLocker locker(&g_dl_mutex);
238
239 android_namespace_t* result = create_namespace(caller_addr,
240 name,
241 ld_library_path,
242 default_library_path,
243 type,
244 permitted_when_isolated_path,
245 parent_namespace);
246
247 if (result == nullptr) {
248 __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer());
249 }
250
251 return result;
252 }
253
__loader_android_link_namespaces(android_namespace_t * namespace_from,android_namespace_t * namespace_to,const char * shared_libs_sonames)254 bool __loader_android_link_namespaces(android_namespace_t* namespace_from,
255 android_namespace_t* namespace_to,
256 const char* shared_libs_sonames) {
257 ScopedPthreadMutexLocker locker(&g_dl_mutex);
258
259 bool success = link_namespaces(namespace_from, namespace_to, shared_libs_sonames);
260
261 if (!success) {
262 __bionic_format_dlerror("android_link_namespaces failed", linker_get_error_buffer());
263 }
264
265 return success;
266 }
267
__loader_android_link_namespaces_all_libs(android_namespace_t * namespace_from,android_namespace_t * namespace_to)268 bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
269 android_namespace_t* namespace_to) {
270 ScopedPthreadMutexLocker locker(&g_dl_mutex);
271
272 bool success = link_namespaces_all_libs(namespace_from, namespace_to);
273
274 if (!success) {
275 __bionic_format_dlerror("android_link_namespaces_all_libs failed", linker_get_error_buffer());
276 }
277
278 return success;
279 }
280
__loader_android_get_exported_namespace(const char * name)281 android_namespace_t* __loader_android_get_exported_namespace(const char* name) {
282 ScopedPthreadMutexLocker locker(&g_dl_mutex);
283 return get_exported_namespace(name);
284 }
285
__loader_cfi_fail(uint64_t CallSiteTypeId,void * Ptr,void * DiagData,void * CallerPc)286 void __loader_cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) {
287 ScopedPthreadMutexLocker locker(&g_dl_mutex);
288 CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc);
289 }
290
__loader_add_thread_local_dtor(void * dso_handle)291 void __loader_add_thread_local_dtor(void* dso_handle) {
292 ScopedPthreadMutexLocker locker(&g_dl_mutex);
293 increment_dso_handle_reference_counter(dso_handle);
294 }
295
__loader_remove_thread_local_dtor(void * dso_handle)296 void __loader_remove_thread_local_dtor(void* dso_handle) {
297 ScopedPthreadMutexLocker locker(&g_dl_mutex);
298 decrement_dso_handle_reference_counter(dso_handle);
299 }
300
__loader_shared_globals()301 libc_shared_globals* __loader_shared_globals() {
302 return __libc_shared_globals();
303 }
304
305 static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
306 static soinfo* __libdl_info = nullptr;
307
308 // This is used by the dynamic linker. Every process gets these symbols for free.
get_libdl_info(const soinfo & linker_si)309 soinfo* get_libdl_info(const soinfo& linker_si) {
310 CHECK((linker_si.flags_ & FLAG_GNU_HASH) != 0);
311
312 if (__libdl_info == nullptr) {
313 __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, nullptr, nullptr, 0, 0);
314 __libdl_info->flags_ |= (FLAG_LINKED | FLAG_GNU_HASH);
315 __libdl_info->strtab_ = linker_si.strtab_;
316 __libdl_info->symtab_ = linker_si.symtab_;
317 __libdl_info->load_bias = linker_si.load_bias;
318 __libdl_info->phdr = linker_si.phdr;
319 __libdl_info->phnum = linker_si.phnum;
320
321 __libdl_info->gnu_nbucket_ = linker_si.gnu_nbucket_;
322 __libdl_info->gnu_maskwords_ = linker_si.gnu_maskwords_;
323 __libdl_info->gnu_shift2_ = linker_si.gnu_shift2_;
324 __libdl_info->gnu_bloom_filter_ = linker_si.gnu_bloom_filter_;
325 __libdl_info->gnu_bucket_ = linker_si.gnu_bucket_;
326 __libdl_info->gnu_chain_ = linker_si.gnu_chain_;
327
328 __libdl_info->ref_count_ = 1;
329 __libdl_info->strtab_size_ = linker_si.strtab_size_;
330 __libdl_info->local_group_root_ = __libdl_info;
331 __libdl_info->soname_ = linker_si.soname_;
332 __libdl_info->target_sdk_version_ = __ANDROID_API__;
333 __libdl_info->generate_handle();
334 #if defined(__work_around_b_24465209__)
335 strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_));
336 #endif
337 }
338
339 return __libdl_info;
340 }
341