| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 4 | * | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 14 | * | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 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. | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 27 | */ | 
|  | 28 |  | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 29 | #include "linker.h" | 
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 30 | #include "linker_cfi.h" | 
| Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 31 | #include "linker_globals.h" | 
| Dimitry Ivanov | 769b33f | 2016-07-21 11:33:40 -0700 | [diff] [blame] | 32 | #include "linker_dlwarning.h" | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 33 |  | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 34 | #include <pthread.h> | 
|  | 35 | #include <stdio.h> | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 36 | #include <stdlib.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 37 | #include <string.h> | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 38 | #include <android/api-level.h> | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 39 |  | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 40 | #include <bionic/pthread_internal.h> | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 41 | #include "private/bionic_tls.h" | 
|  | 42 | #include "private/ScopedPthreadMutexLocker.h" | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 43 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 44 | #define __LINKER_PUBLIC__ __attribute__((visibility("default"))) | 
|  | 45 |  | 
|  | 46 | extern "C" { | 
|  | 47 |  | 
|  | 48 | android_namespace_t* __loader_android_create_namespace(const char* name, | 
|  | 49 | const char* ld_library_path, | 
|  | 50 | const char* default_library_path, | 
|  | 51 | uint64_t type, | 
|  | 52 | const char* permitted_when_isolated_path, | 
|  | 53 | android_namespace_t* parent_namespace, | 
|  | 54 | const void* caller_addr) __LINKER_PUBLIC__; | 
|  | 55 | void* __loader_android_dlopen_ext(const char* filename, | 
|  | 56 | int flags, | 
|  | 57 | const android_dlextinfo* extinfo, | 
|  | 58 | const void* caller_addr) __LINKER_PUBLIC__; | 
|  | 59 | void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) __LINKER_PUBLIC__; | 
|  | 60 | uint32_t __loader_android_get_application_target_sdk_version() __LINKER_PUBLIC__; | 
|  | 61 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) __LINKER_PUBLIC__; | 
|  | 62 | android_namespace_t* __loader_android_get_exported_namespace(const char* name) __LINKER_PUBLIC__; | 
|  | 63 | bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames, | 
|  | 64 | const char* library_search_path) __LINKER_PUBLIC__; | 
|  | 65 | bool __loader_android_link_namespaces(android_namespace_t* namespace_from, | 
|  | 66 | android_namespace_t* namespace_to, | 
|  | 67 | const char* shared_libs_sonames) __LINKER_PUBLIC__; | 
|  | 68 | void __loader_android_set_application_target_sdk_version(uint32_t target) __LINKER_PUBLIC__; | 
|  | 69 | void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) __LINKER_PUBLIC__; | 
|  | 70 | void __loader_cfi_fail(uint64_t CallSiteTypeId, | 
|  | 71 | void* Ptr, | 
|  | 72 | void *DiagData, | 
|  | 73 | void *CallerPc) __LINKER_PUBLIC__; | 
|  | 74 | int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), | 
|  | 75 | void* data) __LINKER_PUBLIC__; | 
|  | 76 | int __loader_dladdr(const void* addr, Dl_info* info) __LINKER_PUBLIC__; | 
|  | 77 | int __loader_dlclose(void* handle) __LINKER_PUBLIC__; | 
|  | 78 | char* __loader_dlerror() __LINKER_PUBLIC__; | 
|  | 79 | void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) __LINKER_PUBLIC__; | 
|  | 80 | void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) __LINKER_PUBLIC__; | 
|  | 81 | void* __loader_dlvsym(void* handle, | 
|  | 82 | const char* symbol, | 
|  | 83 | const char* version, | 
|  | 84 | const void* caller_addr) __LINKER_PUBLIC__; | 
|  | 85 | #if defined(__arm__) | 
|  | 86 | _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) __LINKER_PUBLIC__; | 
|  | 87 | #endif | 
|  | 88 | } | 
|  | 89 |  | 
| Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 90 | static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 91 |  | 
| Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 92 | static char* __bionic_set_dlerror(char* new_value) { | 
| Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 93 | char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR]; | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 94 |  | 
| Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 95 | char* old_value = *dlerror_slot; | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 96 | *dlerror_slot = new_value; | 
| Elliott Hughes | 295082b | 2017-04-15 09:11:15 -0700 | [diff] [blame] | 97 | if (new_value != nullptr) LD_LOG(kLogErrors, "dlerror set to \"%s\"", new_value); | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 98 | return old_value; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 101 | static void __bionic_format_dlerror(const char* msg, const char* detail) { | 
|  | 102 | char* buffer = __get_thread()->dlerror_buffer; | 
|  | 103 | strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 104 | if (detail != nullptr) { | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 105 | strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE); | 
|  | 106 | strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | __bionic_set_dlerror(buffer); | 
|  | 110 | } | 
|  | 111 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 112 | char* __loader_dlerror() { | 
| Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 113 | char* old_value = __bionic_set_dlerror(nullptr); | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 114 | return old_value; | 
|  | 115 | } | 
|  | 116 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 117 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 118 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 119 | do_android_get_LD_LIBRARY_PATH(buffer, buffer_size); | 
|  | 120 | } | 
|  | 121 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 122 | void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 123 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 124 | do_android_update_LD_LIBRARY_PATH(ld_library_path); | 
|  | 125 | } | 
|  | 126 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 127 | static void* dlopen_ext(const char* filename, | 
|  | 128 | int flags, | 
|  | 129 | const android_dlextinfo* extinfo, | 
|  | 130 | const void* caller_addr) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 131 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 132 | g_linker_logger.ResetState(); | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 133 | void* result = do_dlopen(filename, flags, extinfo, caller_addr); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 134 | if (result == nullptr) { | 
| Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 135 | __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 136 | return nullptr; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 137 | } | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 138 | return result; | 
|  | 139 | } | 
|  | 140 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 141 | void* __loader_android_dlopen_ext(const char* filename, | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 142 | int flags, | 
|  | 143 | const android_dlextinfo* extinfo, | 
|  | 144 | const void* caller_addr) { | 
| Dmitriy Ivanov | e5cfafe | 2015-07-17 10:36:10 -0700 | [diff] [blame] | 145 | return dlopen_ext(filename, flags, extinfo, caller_addr); | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 148 | void* __loader_dlopen(const char* filename, int flags, const void* caller_addr) { | 
| Dmitriy Ivanov | e5cfafe | 2015-07-17 10:36:10 -0700 | [diff] [blame] | 149 | return dlopen_ext(filename, flags, nullptr, caller_addr); | 
| Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 152 | void* dlsym_impl(void* handle, const char* symbol, const char* version, const void* caller_addr) { | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 153 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 154 | g_linker_logger.ResetState(); | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 155 | void* result; | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 156 | if (!do_dlsym(handle, symbol, version, caller_addr, &result)) { | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 157 | __bionic_format_dlerror(linker_get_error_buffer(), nullptr); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 158 | return nullptr; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 159 | } | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 160 |  | 
|  | 161 | return result; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 164 | void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr) { | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 165 | return dlsym_impl(handle, symbol, nullptr, caller_addr); | 
|  | 166 | } | 
|  | 167 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 168 | void* __loader_dlvsym(void* handle, const char* symbol, const char* version, const void* caller_addr) { | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 169 | return dlsym_impl(handle, symbol, version, caller_addr); | 
|  | 170 | } | 
|  | 171 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 172 | int __loader_dladdr(const void* addr, Dl_info* info) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 173 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 174 | return do_dladdr(addr, info); | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 177 | int __loader_dlclose(void* handle) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 178 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 179 | int result = do_dlclose(handle); | 
|  | 180 | if (result != 0) { | 
|  | 181 | __bionic_format_dlerror("dlclose failed", linker_get_error_buffer()); | 
|  | 182 | } | 
|  | 183 | return result; | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 186 | int __loader_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) { | 
| Dmitriy Ivanov | 7271caf | 2015-06-29 14:48:25 -0700 | [diff] [blame] | 187 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
|  | 188 | return do_dl_iterate_phdr(cb, data); | 
|  | 189 | } | 
|  | 190 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 191 | // This function is needed by libgcc.a | 
|  | 192 | int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) { | 
|  | 193 | return __loader_dl_iterate_phdr(cb, data); | 
|  | 194 | } | 
|  | 195 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 196 | #if defined(__arm__) | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 197 | _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 198 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
|  | 199 | return do_dl_unwind_find_exidx(pc, pcount); | 
|  | 200 | } | 
|  | 201 | #endif | 
|  | 202 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 203 | void __loader_android_set_application_target_sdk_version(uint32_t target) { | 
| Dmitriy Ivanov | d974e88 | 2015-05-27 18:29:41 -0700 | [diff] [blame] | 204 | // lock to avoid modification in the middle of dlopen. | 
|  | 205 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 206 | set_application_target_sdk_version(target); | 
|  | 207 | } | 
|  | 208 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 209 | uint32_t __loader_android_get_application_target_sdk_version() { | 
| Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 210 | return get_application_target_sdk_version(); | 
|  | 211 | } | 
|  | 212 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 213 | void __loader_android_dlwarning(void* obj, void (*f)(void*, const char*)) { | 
| Dimitry Ivanov | 769b33f | 2016-07-21 11:33:40 -0700 | [diff] [blame] | 214 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
|  | 215 | get_dlwarning(obj, f); | 
|  | 216 | } | 
|  | 217 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 218 | bool __loader_android_init_anonymous_namespace(const char* shared_libs_sonames, | 
|  | 219 | const char* library_search_path) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 220 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 221 | bool success = init_anonymous_namespace(shared_libs_sonames, library_search_path); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 222 | if (!success) { | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 223 | __bionic_format_dlerror("android_init_anonymous_namespace failed", linker_get_error_buffer()); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | return success; | 
|  | 227 | } | 
|  | 228 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 229 | android_namespace_t* __loader_android_create_namespace(const char* name, | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 230 | const char* ld_library_path, | 
|  | 231 | const char* default_library_path, | 
|  | 232 | uint64_t type, | 
|  | 233 | const char* permitted_when_isolated_path, | 
|  | 234 | android_namespace_t* parent_namespace, | 
|  | 235 | const void* caller_addr) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 236 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
|  | 237 |  | 
| Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 238 | android_namespace_t* result = create_namespace(caller_addr, | 
|  | 239 | name, | 
|  | 240 | ld_library_path, | 
|  | 241 | default_library_path, | 
|  | 242 | type, | 
|  | 243 | permitted_when_isolated_path, | 
|  | 244 | parent_namespace); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 245 |  | 
|  | 246 | if (result == nullptr) { | 
|  | 247 | __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer()); | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | return result; | 
|  | 251 | } | 
|  | 252 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 253 | bool __loader_android_link_namespaces(android_namespace_t* namespace_from, | 
|  | 254 | android_namespace_t* namespace_to, | 
|  | 255 | const char* shared_libs_sonames) { | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 256 | ScopedPthreadMutexLocker locker(&g_dl_mutex); | 
|  | 257 |  | 
|  | 258 | bool success = link_namespaces(namespace_from, namespace_to, shared_libs_sonames); | 
|  | 259 |  | 
|  | 260 | if (!success) { | 
|  | 261 | __bionic_format_dlerror("android_link_namespaces failed", linker_get_error_buffer()); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | return success; | 
|  | 265 | } | 
|  | 266 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 267 | android_namespace_t* __loader_android_get_exported_namespace(const char* name) { | 
| Jiyong Park | 01de74e | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 268 | return get_exported_namespace(name); | 
|  | 269 | } | 
|  | 270 |  | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 271 | void __loader_cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) { | 
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 272 | CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc); | 
|  | 273 | } | 
|  | 274 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 275 | static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8))); | 
|  | 276 | static soinfo* __libdl_info = nullptr; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 277 |  | 
| Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 278 | // This is used by the dynamic linker. Every process gets these symbols for free. | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 279 | soinfo* get_libdl_info(const char* linker_path, | 
|  | 280 | const soinfo& linker_si, | 
|  | 281 | const link_map& linker_map) { | 
|  | 282 | CHECK((linker_si.flags_ & FLAG_GNU_HASH) != 0); | 
|  | 283 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 284 | if (__libdl_info == nullptr) { | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 285 | __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, linker_path, nullptr, 0, 0); | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 286 | __libdl_info->flags_ |= (FLAG_LINKED | FLAG_GNU_HASH); | 
|  | 287 | __libdl_info->strtab_ = linker_si.strtab_; | 
|  | 288 | __libdl_info->symtab_ = linker_si.symtab_; | 
|  | 289 | __libdl_info->load_bias = linker_si.load_bias; | 
|  | 290 |  | 
|  | 291 | __libdl_info->gnu_nbucket_ = linker_si.gnu_nbucket_; | 
|  | 292 | __libdl_info->gnu_maskwords_ = linker_si.gnu_maskwords_; | 
|  | 293 | __libdl_info->gnu_shift2_ = linker_si.gnu_shift2_; | 
|  | 294 | __libdl_info->gnu_bloom_filter_ = linker_si.gnu_bloom_filter_; | 
|  | 295 | __libdl_info->gnu_bucket_ = linker_si.gnu_bucket_; | 
|  | 296 | __libdl_info->gnu_chain_ = linker_si.gnu_chain_; | 
|  | 297 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 298 | __libdl_info->ref_count_ = 1; | 
| dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 299 | __libdl_info->strtab_size_ = linker_si.strtab_size_; | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 300 | __libdl_info->local_group_root_ = __libdl_info; | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 301 | __libdl_info->soname_ = "ld-android.so"; | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 302 | __libdl_info->target_sdk_version_ = __ANDROID_API__; | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 303 | __libdl_info->generate_handle(); | 
| Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 304 | __libdl_info->link_map_head.l_addr = linker_map.l_addr; | 
|  | 305 | __libdl_info->link_map_head.l_name = linker_map.l_name; | 
|  | 306 | __libdl_info->link_map_head.l_ld = linker_map.l_ld; | 
| Mike Frysinger | 747d30e | 2015-10-20 14:06:25 -0400 | [diff] [blame] | 307 | #if defined(__work_around_b_24465209__) | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 308 | strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_)); | 
|  | 309 | #endif | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 310 | } | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 311 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 312 | return __libdl_info; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 313 | } |