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 | |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 44 | static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 46 | static char* __bionic_set_dlerror(char* new_value) { |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 47 | char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR]; |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 49 | char* old_value = *dlerror_slot; |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 50 | *dlerror_slot = new_value; |
| 51 | return old_value; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 54 | static void __bionic_format_dlerror(const char* msg, const char* detail) { |
| 55 | char* buffer = __get_thread()->dlerror_buffer; |
| 56 | strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 57 | if (detail != nullptr) { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 58 | strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE); |
| 59 | strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE); |
| 60 | } |
| 61 | |
| 62 | __bionic_set_dlerror(buffer); |
| 63 | } |
| 64 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 65 | char* __dlerror() { |
Elliott Hughes | 5e071a1 | 2016-08-11 15:02:45 -0700 | [diff] [blame] | 66 | char* old_value = __bionic_set_dlerror(nullptr); |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 67 | return old_value; |
| 68 | } |
| 69 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 70 | void __android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 71 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 72 | do_android_get_LD_LIBRARY_PATH(buffer, buffer_size); |
| 73 | } |
| 74 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 75 | void __android_update_LD_LIBRARY_PATH(const char* ld_library_path) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 76 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 77 | do_android_update_LD_LIBRARY_PATH(ld_library_path); |
| 78 | } |
| 79 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 80 | static void* dlopen_ext(const char* filename, |
| 81 | int flags, |
| 82 | const android_dlextinfo* extinfo, |
| 83 | const void* caller_addr) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 84 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 85 | g_linker_logger.ResetState(); |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 86 | void* result = do_dlopen(filename, flags, extinfo, caller_addr); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 87 | if (result == nullptr) { |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 88 | __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 89 | return nullptr; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 90 | } |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 91 | return result; |
| 92 | } |
| 93 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 94 | void* __android_dlopen_ext(const char* filename, |
| 95 | int flags, |
| 96 | const android_dlextinfo* extinfo, |
| 97 | const void* caller_addr) { |
Dmitriy Ivanov | e5cfafe | 2015-07-17 10:36:10 -0700 | [diff] [blame] | 98 | return dlopen_ext(filename, flags, extinfo, caller_addr); |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 101 | void* __dlopen(const char* filename, int flags, const void* caller_addr) { |
Dmitriy Ivanov | e5cfafe | 2015-07-17 10:36:10 -0700 | [diff] [blame] | 102 | return dlopen_ext(filename, flags, nullptr, caller_addr); |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 105 | 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] | 106 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 107 | g_linker_logger.ResetState(); |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 108 | void* result; |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 109 | if (!do_dlsym(handle, symbol, version, caller_addr, &result)) { |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 110 | __bionic_format_dlerror(linker_get_error_buffer(), nullptr); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 111 | return nullptr; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 112 | } |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 113 | |
| 114 | return result; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 117 | void* __dlsym(void* handle, const char* symbol, const void* caller_addr) { |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 118 | return dlsym_impl(handle, symbol, nullptr, caller_addr); |
| 119 | } |
| 120 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 121 | void* __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] | 122 | return dlsym_impl(handle, symbol, version, caller_addr); |
| 123 | } |
| 124 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 125 | int __dladdr(const void* addr, Dl_info* info) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 126 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 127 | return do_dladdr(addr, info); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 130 | int __dlclose(void* handle) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 131 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 132 | int result = do_dlclose(handle); |
| 133 | if (result != 0) { |
| 134 | __bionic_format_dlerror("dlclose failed", linker_get_error_buffer()); |
| 135 | } |
| 136 | return result; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 139 | // This function is needed by libgcc.a (this is why there is no prefix for this one) |
Andreas Gampe | dcb846c | 2016-12-06 02:10:13 +0000 | [diff] [blame] | 140 | int 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] | 141 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
| 142 | return do_dl_iterate_phdr(cb, data); |
| 143 | } |
| 144 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 145 | #if defined(__arm__) |
| 146 | _Unwind_Ptr __dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { |
| 147 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
| 148 | return do_dl_unwind_find_exidx(pc, pcount); |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | void __android_set_application_target_sdk_version(uint32_t target) { |
Dmitriy Ivanov | d974e88 | 2015-05-27 18:29:41 -0700 | [diff] [blame] | 153 | // lock to avoid modification in the middle of dlopen. |
| 154 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 155 | set_application_target_sdk_version(target); |
| 156 | } |
| 157 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 158 | uint32_t __android_get_application_target_sdk_version() { |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 159 | return get_application_target_sdk_version(); |
| 160 | } |
| 161 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 162 | void __android_dlwarning(void* obj, void (*f)(void*, const char*)) { |
Dimitry Ivanov | 769b33f | 2016-07-21 11:33:40 -0700 | [diff] [blame] | 163 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
| 164 | get_dlwarning(obj, f); |
| 165 | } |
| 166 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 167 | bool __android_init_anonymous_namespace(const char* shared_libs_sonames, |
| 168 | const char* library_search_path) { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 169 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 170 | bool success = init_anonymous_namespace(shared_libs_sonames, library_search_path); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 171 | if (!success) { |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 172 | __bionic_format_dlerror("android_init_anonymous_namespace failed", linker_get_error_buffer()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return success; |
| 176 | } |
| 177 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 178 | android_namespace_t* __android_create_namespace(const char* name, |
| 179 | const char* ld_library_path, |
| 180 | const char* default_library_path, |
| 181 | uint64_t type, |
| 182 | const char* permitted_when_isolated_path, |
| 183 | android_namespace_t* parent_namespace, |
| 184 | const void* caller_addr) { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 185 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
| 186 | |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 187 | android_namespace_t* result = create_namespace(caller_addr, |
| 188 | name, |
| 189 | ld_library_path, |
| 190 | default_library_path, |
| 191 | type, |
| 192 | permitted_when_isolated_path, |
| 193 | parent_namespace); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 194 | |
| 195 | if (result == nullptr) { |
| 196 | __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer()); |
| 197 | } |
| 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 202 | bool __android_link_namespaces(android_namespace_t* namespace_from, |
| 203 | android_namespace_t* namespace_to, |
| 204 | const char* shared_libs_sonames) { |
| 205 | ScopedPthreadMutexLocker locker(&g_dl_mutex); |
| 206 | |
| 207 | bool success = link_namespaces(namespace_from, namespace_to, shared_libs_sonames); |
| 208 | |
| 209 | if (!success) { |
| 210 | __bionic_format_dlerror("android_link_namespaces failed", linker_get_error_buffer()); |
| 211 | } |
| 212 | |
| 213 | return success; |
| 214 | } |
| 215 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 216 | void __cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) { |
| 217 | CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc); |
| 218 | } |
| 219 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 220 | // name_offset: starting index of the name in libdl_info.strtab |
| 221 | #define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ |
| 222 | { name_offset, \ |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 223 | reinterpret_cast<Elf32_Addr>(value), \ |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 224 | /* st_size */ 0, \ |
Chih-Hung Hsieh | 1a5fd9c | 2016-06-10 11:07:21 -0700 | [diff] [blame] | 225 | ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \ |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 226 | /* st_other */ 0, \ |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 227 | shndx, \ |
| 228 | } |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 229 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 230 | #define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \ |
| 231 | { name_offset, \ |
Chih-Hung Hsieh | 1a5fd9c | 2016-06-10 11:07:21 -0700 | [diff] [blame] | 232 | ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \ |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 233 | /* st_other */ 0, \ |
| 234 | shndx, \ |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 235 | reinterpret_cast<Elf64_Addr>(value), \ |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 236 | /* st_size */ 0, \ |
| 237 | } |
| 238 | |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 239 | static const char ANDROID_LIBDL_STRTAB[] = |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 240 | // 0000000000111111 11112222222222333 333333344444444 44555555555566666 6666677777777778 8888888889999999999 |
| 241 | // 0123456789012345 67890123456789012 345678901234567 89012345678901234 5678901234567890 1234567890123456789 |
| 242 | "__loader_dlopen\0__loader_dlclose\0__loader_dlsym\0__loader_dlerror\0__loader_dladdr\0__loader_android_up" |
| 243 | // 1* |
| 244 | // 000000000011111111112 2222222223333333333444444444455555555 5566666666667777777777888 88888889999999999 |
| 245 | // 012345678901234567890 1234567890123456789012345678901234567 8901234567890123456789012 34567890123456789 |
| 246 | "date_LD_LIBRARY_PATH\0__loader_android_get_LD_LIBRARY_PATH\0__loader_dl_iterate_phdr\0__loader_android_" |
| 247 | // 2* |
| 248 | // 00000000001 1111111112222222222333333333344444444445555555555666 6666666777777777788888888889999999999 |
| 249 | // 01234567890 1234567890123456789012345678901234567890123456789012 3456789012345678901234567890123456789 |
| 250 | "dlopen_ext\0__loader_android_set_application_target_sdk_version\0__loader_android_get_application_targ" |
| 251 | // 3* |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 252 | // 000000000011111 111112222222222333333333344444444445555555 5556666666666777777777788888888889 999999999 |
| 253 | // 012345678901234 567890123456789012345678901234567890123456 7890123456789012345678901234567890 123456789 |
| 254 | "et_sdk_version\0__loader_android_init_anonymous_namespace\0__loader_android_create_namespace\0__loader_" |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 255 | // 4* |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 256 | // 0000000 000111111111122222222223333 333333444444444455 555555556666666666777777777788888 888889999999999 |
| 257 | // 0123456 789012345678901234567890123 456789012345678901 234567890123456789012345678901234 567890123456789 |
| 258 | "dlvsym\0__loader_android_dlwarning\0__loader_cfi_fail\0__loader_android_link_namespaces\0" |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 259 | #if defined(__arm__) |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 260 | // 485 |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 261 | "__loader_dl_unwind_find_exidx\0" |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 262 | #endif |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 263 | ; |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 264 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 265 | static ElfW(Sym) g_libdl_symtab[] = { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 266 | // Total length of libdl_info.strtab, including trailing 0. |
| 267 | // This is actually the STH_UNDEF entry. Technically, it's |
| 268 | // supposed to have st_name == 0, but instead, it points to an index |
| 269 | // in the strtab with a \0 to make iterating through the symtab easier. |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 270 | ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0), |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 271 | ELFW(SYM_INITIALIZER)( 0, &__dlopen, 1), |
| 272 | ELFW(SYM_INITIALIZER)( 16, &__dlclose, 1), |
| 273 | ELFW(SYM_INITIALIZER)( 33, &__dlsym, 1), |
| 274 | ELFW(SYM_INITIALIZER)( 48, &__dlerror, 1), |
| 275 | ELFW(SYM_INITIALIZER)( 65, &__dladdr, 1), |
| 276 | ELFW(SYM_INITIALIZER)( 81, &__android_update_LD_LIBRARY_PATH, 1), |
| 277 | ELFW(SYM_INITIALIZER)(121, &__android_get_LD_LIBRARY_PATH, 1), |
| 278 | ELFW(SYM_INITIALIZER)(158, &dl_iterate_phdr, 1), |
| 279 | ELFW(SYM_INITIALIZER)(183, &__android_dlopen_ext, 1), |
| 280 | ELFW(SYM_INITIALIZER)(211, &__android_set_application_target_sdk_version, 1), |
| 281 | ELFW(SYM_INITIALIZER)(263, &__android_get_application_target_sdk_version, 1), |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 282 | ELFW(SYM_INITIALIZER)(315, &__android_init_anonymous_namespace, 1), |
| 283 | ELFW(SYM_INITIALIZER)(357, &__android_create_namespace, 1), |
| 284 | ELFW(SYM_INITIALIZER)(391, &__dlvsym, 1), |
| 285 | ELFW(SYM_INITIALIZER)(407, &__android_dlwarning, 1), |
| 286 | ELFW(SYM_INITIALIZER)(434, &__cfi_fail, 1), |
| 287 | ELFW(SYM_INITIALIZER)(452, &__android_link_namespaces, 1), |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 288 | #if defined(__arm__) |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 289 | ELFW(SYM_INITIALIZER)(485, &__dl_unwind_find_exidx, 1), |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 290 | #endif |
| 291 | }; |
| 292 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 293 | // Fake out a hash table with a single bucket. |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 294 | // |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 295 | // A search of the hash table will look through g_libdl_symtab starting with index 1, then |
| 296 | // use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to |
| 297 | // walk through every element in g_libdl_symtab, and then end with 0 (sentinel value). |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 298 | // |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 299 | // That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number |
| 300 | // of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 301 | // a real symbol). (See soinfo_elf_lookup().) |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 302 | // |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 303 | // Note that adding any new symbols here requires stubbing them out in libdl. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 304 | static unsigned g_libdl_buckets[1] = { 1 }; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 305 | #if defined(__arm__) |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 306 | static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0 }; |
Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 307 | #else |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 308 | static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0 }; |
Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 309 | #endif |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 310 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 311 | static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8))); |
| 312 | static soinfo* __libdl_info = nullptr; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 313 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 314 | // This is used by the dynamic linker. Every process gets these symbols for free. |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 315 | soinfo* get_libdl_info(const char* linker_path) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 316 | if (__libdl_info == nullptr) { |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 317 | __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, linker_path, nullptr, 0, 0); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 318 | __libdl_info->flags_ |= FLAG_LINKED; |
| 319 | __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB; |
| 320 | __libdl_info->symtab_ = g_libdl_symtab; |
| 321 | __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned); |
| 322 | __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned); |
| 323 | __libdl_info->bucket_ = g_libdl_buckets; |
| 324 | __libdl_info->chain_ = g_libdl_chains; |
| 325 | __libdl_info->ref_count_ = 1; |
| 326 | __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB); |
| 327 | __libdl_info->local_group_root_ = __libdl_info; |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 328 | __libdl_info->soname_ = "ld-android.so"; |
Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 329 | __libdl_info->target_sdk_version_ = __ANDROID_API__; |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 330 | __libdl_info->generate_handle(); |
Mike Frysinger | 747d30e | 2015-10-20 14:06:25 -0400 | [diff] [blame] | 331 | #if defined(__work_around_b_24465209__) |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 332 | strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_)); |
| 333 | #endif |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 334 | } |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 335 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 336 | return __libdl_info; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 337 | } |