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_debuggerd.h"
32 #include "linker_dlwarning.h"
33 #include "linker_globals.h"
34
35 #include <link.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <android/api-level.h>
41
42 #include <bionic/pthread_internal.h>
43 #include "private/bionic_globals.h"
44 #include "private/bionic_tls.h"
45 #include "private/ScopedPthreadMutexLocker.h"
46
47 #define __LINKER_PUBLIC__ __attribute__((visibility("default")))
48
49 extern "C" {
50
51 android_namespace_t* __loader_android_create_namespace(const char* name,
52 const char* ld_library_path,
53 const char* default_library_path,
54 uint64_t type,
55 const char* permitted_when_isolated_path,
56 android_namespace_t* parent_namespace,
57 const void* caller_addr) __LINKER_PUBLIC__;
58 void* __loader_android_dlopen_ext(const char* filename,
59 int flags,
60 const android_dlextinfo* extinfo,
61 const void* caller_addr) __LINKER_PUBLIC__;
62 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) __LINKER_PUBLIC__;
63 int __loader_android_get_application_target_sdk_version() __LINKER_PUBLIC__;
64 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) __LINKER_PUBLIC__;
65 android_namespace_t* __loader_android_get_exported_namespace(const char* name) __LINKER_PUBLIC__;
66 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
67 const char* library_search_path) __LINKER_PUBLIC__;
68 bool __loader_android_link_namespaces(android_namespace_t* namespace_from,
69 android_namespace_t* namespace_to,
70 const char* shared_libs_sonames) __LINKER_PUBLIC__;
71 bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
72 android_namespace_t* namespace_to) __LINKER_PUBLIC__;
73 void __loader_android_set_application_target_sdk_version(int target) __LINKER_PUBLIC__;
74 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) __LINKER_PUBLIC__;
75 void __loader_cfi_fail(uint64_t CallSiteTypeId,
76 void* Ptr,
77 void *DiagData,
78 void *CallerPc) __LINKER_PUBLIC__;
79 int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data),
80 void* data) __LINKER_PUBLIC__;
81 int __loader_dladdr(const void* addr, Dl_info* info) __LINKER_PUBLIC__;
82 int __loader_dlclose(void* handle) __LINKER_PUBLIC__;
83 char* __loader_dlerror() __LINKER_PUBLIC__;
84 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) __LINKER_PUBLIC__;
85 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) __LINKER_PUBLIC__;
86 void* __loader_dlvsym(void* handle,
87 const char* symbol,
88 const char* version,
89 const void* caller_addr) __LINKER_PUBLIC__;
90 void __loader_add_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
91 void __loader_remove_thread_local_dtor(void* dso_handle) __LINKER_PUBLIC__;
92 libc_shared_globals* __loader_shared_globals() __LINKER_PUBLIC__;
93 #if defined(__arm__)
94 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) __LINKER_PUBLIC__;
95 #endif
96 bool __loader_android_handle_signal(int signal_number, siginfo_t* info,
97 void* context) __LINKER_PUBLIC__;
98 }
99
100 static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
101
__bionic_set_dlerror(char * new_value)102 static char* __bionic_set_dlerror(char* new_value) {
103 char* old_value = __get_thread()->current_dlerror;
104 __get_thread()->current_dlerror = new_value;
105
106 if (new_value != nullptr) LD_LOG(kLogErrors, "dlerror set to \"%s\"", new_value);
107 return old_value;
108 }
109
__bionic_format_dlerror(const char * msg,const char * detail)110 static void __bionic_format_dlerror(const char* msg, const char* detail) {
111 char* buffer = __get_thread()->dlerror_buffer;
112 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE);
113 if (detail != nullptr) {
114 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE);
115 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE);
116 }
117
118 __bionic_set_dlerror(buffer);
119 }
120
__loader_dlerror()121 char* __loader_dlerror() {
122 char* old_value = __bionic_set_dlerror(nullptr);
123 return old_value;
124 }
125
__loader_android_get_LD_LIBRARY_PATH(char * buffer,size_t buffer_size)126 void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
127 ScopedPthreadMutexLocker locker(&g_dl_mutex);
128 do_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
129 }
130
__loader_android_update_LD_LIBRARY_PATH(const char * ld_library_path)131 void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
132 ScopedPthreadMutexLocker locker(&g_dl_mutex);
133 do_android_update_LD_LIBRARY_PATH(ld_library_path);
134 }
135
dlopen_ext(const char * filename,int flags,const android_dlextinfo * extinfo,const void * caller_addr)136 static void* dlopen_ext(const char* filename,
137 int flags,
138 const android_dlextinfo* extinfo,
139 const void* caller_addr) {
140 ScopedPthreadMutexLocker locker(&g_dl_mutex);
141 g_linker_logger.ResetState();
142 void* result = do_dlopen(filename, flags, extinfo, caller_addr);
143 if (result == nullptr) {
144 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
145 return nullptr;
146 }
147 return result;
148 }
149
__loader_android_dlopen_ext(const char * filename,int flags,const android_dlextinfo * extinfo,const void * caller_addr)150 void* __loader_android_dlopen_ext(const char* filename,
151 int flags,
152 const android_dlextinfo* extinfo,
153 const void* caller_addr) {
154 return dlopen_ext(filename, flags, extinfo, caller_addr);
155 }
156
__loader_dlopen(const char * filename,int flags,const void * caller_addr)157 void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) {
158 return dlopen_ext(filename, flags, nullptr, caller_addr);
159 }
160
dlsym_impl(void * handle,const char * symbol,const char * version,const void * caller_addr)161 void* dlsym_impl(void* handle, const char* symbol, const char* version, const void* caller_addr) {
162 ScopedPthreadMutexLocker locker(&g_dl_mutex);
163 g_linker_logger.ResetState();
164 void* result;
165 if (!do_dlsym(handle, symbol, version, caller_addr, &result)) {
166 __bionic_format_dlerror(linker_get_error_buffer(), nullptr);
167 return nullptr;
168 }
169
170 return result;
171 }
172
__loader_dlsym(void * handle,const char * symbol,const void * caller_addr)173 void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) {
174 return dlsym_impl(handle, symbol, nullptr, caller_addr);
175 }
176
__loader_dlvsym(void * handle,const char * symbol,const char * version,const void * caller_addr)177 void* __loader_dlvsym(void* handle, const char* symbol, const char* version, const void* caller_addr) {
178 return dlsym_impl(handle, symbol, version, caller_addr);
179 }
180
__loader_dladdr(const void * addr,Dl_info * info)181 int __loader_dladdr(const void* addr, Dl_info* info) {
182 ScopedPthreadMutexLocker locker(&g_dl_mutex);
183 return do_dladdr(addr, info);
184 }
185
__loader_dlclose(void * handle)186 int __loader_dlclose(void* handle) {
187 ScopedPthreadMutexLocker locker(&g_dl_mutex);
188 int result = do_dlclose(handle);
189 if (result != 0) {
190 __bionic_format_dlerror("dlclose failed", linker_get_error_buffer());
191 }
192 return result;
193 }
194
__loader_dl_iterate_phdr(int (* cb)(dl_phdr_info * info,size_t size,void * data),void * data)195 int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
196 ScopedPthreadMutexLocker locker(&g_dl_mutex);
197 return do_dl_iterate_phdr(cb, data);
198 }
199
200 #if defined(__arm__)
__loader_dl_unwind_find_exidx(_Unwind_Ptr pc,int * pcount)201 _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
202 ScopedPthreadMutexLocker locker(&g_dl_mutex);
203 return do_dl_unwind_find_exidx(pc, pcount);
204 }
205 #endif
206
__loader_android_set_application_target_sdk_version(int target)207 void __loader_android_set_application_target_sdk_version(int target) {
208 // lock to avoid modification in the middle of dlopen.
209 ScopedPthreadMutexLocker locker(&g_dl_mutex);
210 set_application_target_sdk_version(target);
211 }
212
__loader_android_get_application_target_sdk_version()213 int __loader_android_get_application_target_sdk_version() {
214 return get_application_target_sdk_version();
215 }
216
__loader_android_dlwarning(void * obj,void (* f)(void *,const char *))217 void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) {
218 ScopedPthreadMutexLocker locker(&g_dl_mutex);
219 get_dlwarning(obj, f);
220 }
221
__loader_android_init_anonymous_namespace(const char * shared_libs_sonames,const char * library_search_path)222 bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames,
223 const char* library_search_path) {
224 ScopedPthreadMutexLocker locker(&g_dl_mutex);
225 bool success = init_anonymous_namespace(shared_libs_sonames, library_search_path);
226 if (!success) {
227 __bionic_format_dlerror("android_init_anonymous_namespace failed", linker_get_error_buffer());
228 }
229
230 return success;
231 }
232
__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)233 android_namespace_t* __loader_android_create_namespace(const char* name,
234 const char* ld_library_path,
235 const char* default_library_path,
236 uint64_t type,
237 const char* permitted_when_isolated_path,
238 android_namespace_t* parent_namespace,
239 const void* caller_addr) {
240 ScopedPthreadMutexLocker locker(&g_dl_mutex);
241
242 android_namespace_t* result = create_namespace(caller_addr,
243 name,
244 ld_library_path,
245 default_library_path,
246 type,
247 permitted_when_isolated_path,
248 parent_namespace);
249
250 if (result == nullptr) {
251 __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer());
252 }
253
254 return result;
255 }
256
__loader_android_link_namespaces(android_namespace_t * namespace_from,android_namespace_t * namespace_to,const char * shared_libs_sonames)257 bool __loader_android_link_namespaces(android_namespace_t* namespace_from,
258 android_namespace_t* namespace_to,
259 const char* shared_libs_sonames) {
260 ScopedPthreadMutexLocker locker(&g_dl_mutex);
261
262 bool success = link_namespaces(namespace_from, namespace_to, shared_libs_sonames);
263
264 if (!success) {
265 __bionic_format_dlerror("android_link_namespaces failed", linker_get_error_buffer());
266 }
267
268 return success;
269 }
270
__loader_android_link_namespaces_all_libs(android_namespace_t * namespace_from,android_namespace_t * namespace_to)271 bool __loader_android_link_namespaces_all_libs(android_namespace_t* namespace_from,
272 android_namespace_t* namespace_to) {
273 ScopedPthreadMutexLocker locker(&g_dl_mutex);
274
275 bool success = link_namespaces_all_libs(namespace_from, namespace_to);
276
277 if (!success) {
278 __bionic_format_dlerror("android_link_namespaces_all_libs failed", linker_get_error_buffer());
279 }
280
281 return success;
282 }
283
__loader_android_get_exported_namespace(const char * name)284 android_namespace_t* __loader_android_get_exported_namespace(const char* name) {
285 ScopedPthreadMutexLocker locker(&g_dl_mutex);
286 return get_exported_namespace(name);
287 }
288
__loader_cfi_fail(uint64_t CallSiteTypeId,void * Ptr,void * DiagData,void * CallerPc)289 void __loader_cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) {
290 ScopedPthreadMutexLocker locker(&g_dl_mutex);
291 CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc);
292 }
293
__loader_add_thread_local_dtor(void * dso_handle)294 void __loader_add_thread_local_dtor(void* dso_handle) {
295 ScopedPthreadMutexLocker locker(&g_dl_mutex);
296 increment_dso_handle_reference_counter(dso_handle);
297 }
298
__loader_remove_thread_local_dtor(void * dso_handle)299 void __loader_remove_thread_local_dtor(void* dso_handle) {
300 ScopedPthreadMutexLocker locker(&g_dl_mutex);
301 decrement_dso_handle_reference_counter(dso_handle);
302 }
303
__loader_shared_globals()304 libc_shared_globals* __loader_shared_globals() {
305 return __libc_shared_globals();
306 }
307
__loader_android_handle_signal(int signal_number,siginfo_t * info,void * context)308 bool __loader_android_handle_signal(int signal_number, siginfo_t* info, void* context) {
309 return debuggerd_handle_signal(signal_number, info, context);
310 }
311
312 static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
313 static soinfo* __libdl_info = nullptr;
314
315 // This is used by the dynamic linker. Every process gets these symbols for free.
get_libdl_info(const soinfo & linker_si)316 soinfo* get_libdl_info(const soinfo& linker_si) {
317 CHECK((linker_si.flags_ & FLAG_GNU_HASH) != 0);
318
319 if (__libdl_info == nullptr) {
320 __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, nullptr, nullptr, 0, 0);
321 __libdl_info->flags_ |= (FLAG_LINKED | FLAG_GNU_HASH);
322 __libdl_info->strtab_ = linker_si.strtab_;
323 __libdl_info->symtab_ = linker_si.symtab_;
324 __libdl_info->load_bias = linker_si.load_bias;
325 __libdl_info->phdr = linker_si.phdr;
326 __libdl_info->phnum = linker_si.phnum;
327
328 __libdl_info->gnu_nbucket_ = linker_si.gnu_nbucket_;
329 __libdl_info->gnu_maskwords_ = linker_si.gnu_maskwords_;
330 __libdl_info->gnu_shift2_ = linker_si.gnu_shift2_;
331 __libdl_info->gnu_bloom_filter_ = linker_si.gnu_bloom_filter_;
332 __libdl_info->gnu_bucket_ = linker_si.gnu_bucket_;
333 __libdl_info->gnu_chain_ = linker_si.gnu_chain_;
334
335 __libdl_info->ref_count_ = 1;
336 __libdl_info->strtab_size_ = linker_si.strtab_size_;
337 __libdl_info->local_group_root_ = __libdl_info;
338 __libdl_info->soname_ = linker_si.soname_;
339 __libdl_info->target_sdk_version_ = __ANDROID_API__;
340 __libdl_info->generate_handle();
341 #if defined(__work_around_b_24465209__)
342 strlcpy(__libdl_info->old_name_, __libdl_info->soname_.c_str(),
343 sizeof(__libdl_info->old_name_));
344 #endif
345 }
346
347 return __libdl_info;
348 }
349