dlfcn.cpp revision c12acef96bd80c419654e159e1dc24a69513a86d
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 "linker.h" 18#include "linker_globals.h" 19#include "linker_dlwarning.h" 20 21#include <pthread.h> 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25#include <android/api-level.h> 26 27#include <bionic/pthread_internal.h> 28#include "private/bionic_tls.h" 29#include "private/ScopedPthreadMutexLocker.h" 30#include "private/ThreadLocalBuffer.h" 31 32/* This file hijacks the symbols stubbed out in libdl.so. */ 33 34static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 35 36static char* __bionic_set_dlerror(char* new_value) { 37 char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR]; 38 39 char* old_value = *dlerror_slot; 40 *dlerror_slot = new_value; 41 return old_value; 42} 43 44static void __bionic_format_dlerror(const char* msg, const char* detail) { 45 char* buffer = __get_thread()->dlerror_buffer; 46 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE); 47 if (detail != nullptr) { 48 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE); 49 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE); 50 } 51 52 __bionic_set_dlerror(buffer); 53} 54 55char* __dlerror() { 56 char* old_value = __bionic_set_dlerror(nullptr); 57 return old_value; 58} 59 60void __android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { 61 ScopedPthreadMutexLocker locker(&g_dl_mutex); 62 do_android_get_LD_LIBRARY_PATH(buffer, buffer_size); 63} 64 65void __android_update_LD_LIBRARY_PATH(const char* ld_library_path) { 66 ScopedPthreadMutexLocker locker(&g_dl_mutex); 67 do_android_update_LD_LIBRARY_PATH(ld_library_path); 68} 69 70static void* dlopen_ext(const char* filename, 71 int flags, 72 const android_dlextinfo* extinfo, 73 const void* caller_addr) { 74 ScopedPthreadMutexLocker locker(&g_dl_mutex); 75 g_linker_logger.ResetState(); 76 void* result = do_dlopen(filename, flags, extinfo, caller_addr); 77 if (result == nullptr) { 78 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); 79 return nullptr; 80 } 81 return result; 82} 83 84void* __android_dlopen_ext(const char* filename, 85 int flags, 86 const android_dlextinfo* extinfo, 87 const void* caller_addr) { 88 return dlopen_ext(filename, flags, extinfo, caller_addr); 89} 90 91void* __dlopen(const char* filename, int flags, const void* caller_addr) { 92 return dlopen_ext(filename, flags, nullptr, caller_addr); 93} 94 95void* dlsym_impl(void* handle, const char* symbol, const char* version, const void* caller_addr) { 96 ScopedPthreadMutexLocker locker(&g_dl_mutex); 97 g_linker_logger.ResetState(); 98 void* result; 99 if (!do_dlsym(handle, symbol, version, caller_addr, &result)) { 100 __bionic_format_dlerror(linker_get_error_buffer(), nullptr); 101 return nullptr; 102 } 103 104 return result; 105} 106 107void* __dlsym(void* handle, const char* symbol, const void* caller_addr) { 108 return dlsym_impl(handle, symbol, nullptr, caller_addr); 109} 110 111void* __dlvsym(void* handle, const char* symbol, const char* version, const void* caller_addr) { 112 return dlsym_impl(handle, symbol, version, caller_addr); 113} 114 115int __dladdr(const void* addr, Dl_info* info) { 116 ScopedPthreadMutexLocker locker(&g_dl_mutex); 117 return do_dladdr(addr, info); 118} 119 120int __dlclose(void* handle) { 121 ScopedPthreadMutexLocker locker(&g_dl_mutex); 122 int result = do_dlclose(handle); 123 if (result != 0) { 124 __bionic_format_dlerror("dlclose failed", linker_get_error_buffer()); 125 } 126 return result; 127} 128 129int __dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) { 130 ScopedPthreadMutexLocker locker(&g_dl_mutex); 131 return do_dl_iterate_phdr(cb, data); 132} 133 134#if defined(__arm__) 135_Unwind_Ptr __dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { 136 ScopedPthreadMutexLocker locker(&g_dl_mutex); 137 return do_dl_unwind_find_exidx(pc, pcount); 138} 139#endif 140 141void __android_set_application_target_sdk_version(uint32_t target) { 142 // lock to avoid modification in the middle of dlopen. 143 ScopedPthreadMutexLocker locker(&g_dl_mutex); 144 set_application_target_sdk_version(target); 145} 146 147uint32_t __android_get_application_target_sdk_version() { 148 return get_application_target_sdk_version(); 149} 150 151void __android_dlwarning(void* obj, void (*f)(void*, const char*)) { 152 ScopedPthreadMutexLocker locker(&g_dl_mutex); 153 get_dlwarning(obj, f); 154} 155 156bool __android_init_namespaces(const char* public_ns_sonames, 157 const char* anon_ns_library_path) { 158 ScopedPthreadMutexLocker locker(&g_dl_mutex); 159 bool success = init_namespaces(public_ns_sonames, anon_ns_library_path); 160 if (!success) { 161 __bionic_format_dlerror("android_init_namespaces failed", linker_get_error_buffer()); 162 } 163 164 return success; 165} 166 167android_namespace_t* __android_create_namespace(const char* name, 168 const char* ld_library_path, 169 const char* default_library_path, 170 uint64_t type, 171 const char* permitted_when_isolated_path, 172 android_namespace_t* parent_namespace, 173 const void* caller_addr) { 174 ScopedPthreadMutexLocker locker(&g_dl_mutex); 175 176 android_namespace_t* result = create_namespace(caller_addr, 177 name, 178 ld_library_path, 179 default_library_path, 180 type, 181 permitted_when_isolated_path, 182 parent_namespace); 183 184 if (result == nullptr) { 185 __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer()); 186 } 187 188 return result; 189} 190 191// name_offset: starting index of the name in libdl_info.strtab 192#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ 193 { name_offset, \ 194 reinterpret_cast<Elf32_Addr>(value), \ 195 /* st_size */ 0, \ 196 ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \ 197 /* st_other */ 0, \ 198 shndx, \ 199 } 200 201#define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \ 202 { name_offset, \ 203 ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \ 204 /* st_other */ 0, \ 205 shndx, \ 206 reinterpret_cast<Elf64_Addr>(value), \ 207 /* st_size */ 0, \ 208 } 209 210static const char ANDROID_LIBDL_STRTAB[] = 211 // 0000000000111111 11112222222222333 333333344444444 44555555555566666 6666677777777778 8888888889999999999 212 // 0123456789012345 67890123456789012 345678901234567 89012345678901234 5678901234567890 1234567890123456789 213 "__loader_dlopen\0__loader_dlclose\0__loader_dlsym\0__loader_dlerror\0__loader_dladdr\0__loader_android_up" 214 // 1* 215 // 000000000011111111112 2222222223333333333444444444455555555 5566666666667777777777888 88888889999999999 216 // 012345678901234567890 1234567890123456789012345678901234567 8901234567890123456789012 34567890123456789 217 "date_LD_LIBRARY_PATH\0__loader_android_get_LD_LIBRARY_PATH\0__loader_dl_iterate_phdr\0__loader_android_" 218 // 2* 219 // 00000000001 1111111112222222222333333333344444444445555555555666 6666666777777777788888888889999999999 220 // 01234567890 1234567890123456789012345678901234567890123456789012 3456789012345678901234567890123456789 221 "dlopen_ext\0__loader_android_set_application_target_sdk_version\0__loader_android_get_application_targ" 222 // 3* 223 // 000000000011111 111112222222222333333333344444444 4455555555556666666666777777777788 888888889999999 999 224 // 012345678901234 567890123456789012345678901234567 8901234567890123456789012345678901 234567890123456 789 225 "et_sdk_version\0__loader_android_init_namespaces\0__loader_android_create_namespace\0__loader_dlvsym\0__" 226 // 4* 227 // 0000000000111111111122222 2222233333333334444444 4445555555555666666666677777777778 8888888889999999 999 228 // 0123456789012345678901234 5678901234567890123456 7890123456789012345678901234567890 1234567890123456 789 229 "loader_android_dlwarning\0" 230#if defined(__arm__) 231 // 425 232 "__loader_dl_unwind_find_exidx\0" 233#endif 234 ; 235 236static ElfW(Sym) g_libdl_symtab[] = { 237 // Total length of libdl_info.strtab, including trailing 0. 238 // This is actually the STH_UNDEF entry. Technically, it's 239 // supposed to have st_name == 0, but instead, it points to an index 240 // in the strtab with a \0 to make iterating through the symtab easier. 241 ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0), 242 ELFW(SYM_INITIALIZER)( 0, &__dlopen, 1), 243 ELFW(SYM_INITIALIZER)( 16, &__dlclose, 1), 244 ELFW(SYM_INITIALIZER)( 33, &__dlsym, 1), 245 ELFW(SYM_INITIALIZER)( 48, &__dlerror, 1), 246 ELFW(SYM_INITIALIZER)( 65, &__dladdr, 1), 247 ELFW(SYM_INITIALIZER)( 81, &__android_update_LD_LIBRARY_PATH, 1), 248 ELFW(SYM_INITIALIZER)(121, &__android_get_LD_LIBRARY_PATH, 1), 249 ELFW(SYM_INITIALIZER)(158, &__dl_iterate_phdr, 1), 250 ELFW(SYM_INITIALIZER)(183, &__android_dlopen_ext, 1), 251 ELFW(SYM_INITIALIZER)(211, &__android_set_application_target_sdk_version, 1), 252 ELFW(SYM_INITIALIZER)(263, &__android_get_application_target_sdk_version, 1), 253 ELFW(SYM_INITIALIZER)(315, &__android_init_namespaces, 1), 254 ELFW(SYM_INITIALIZER)(348, &__android_create_namespace, 1), 255 ELFW(SYM_INITIALIZER)(382, &__dlvsym, 1), 256 ELFW(SYM_INITIALIZER)(397, &__android_dlwarning, 1), 257#if defined(__arm__) 258 ELFW(SYM_INITIALIZER)(425, &__dl_unwind_find_exidx, 1), 259#endif 260}; 261 262// Fake out a hash table with a single bucket. 263// 264// A search of the hash table will look through g_libdl_symtab starting with index 1, then 265// use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to 266// walk through every element in g_libdl_symtab, and then end with 0 (sentinel value). 267// 268// That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number 269// of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not 270// a real symbol). (See soinfo_elf_lookup().) 271// 272// Note that adding any new symbols here requires stubbing them out in libdl. 273static unsigned g_libdl_buckets[1] = { 1 }; 274#if defined(__arm__) 275static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 }; 276#else 277static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 }; 278#endif 279 280static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8))); 281static soinfo* __libdl_info = nullptr; 282 283// This is used by the dynamic linker. Every process gets these symbols for free. 284soinfo* get_libdl_info(const char* linker_path) { 285 if (__libdl_info == nullptr) { 286 __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, linker_path, nullptr, 0, 0); 287 __libdl_info->flags_ |= FLAG_LINKED; 288 __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB; 289 __libdl_info->symtab_ = g_libdl_symtab; 290 __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned); 291 __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned); 292 __libdl_info->bucket_ = g_libdl_buckets; 293 __libdl_info->chain_ = g_libdl_chains; 294 __libdl_info->ref_count_ = 1; 295 __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB); 296 __libdl_info->local_group_root_ = __libdl_info; 297 __libdl_info->soname_ = "ld-android.so"; 298 __libdl_info->target_sdk_version_ = __ANDROID_API__; 299 __libdl_info->generate_handle(); 300#if defined(__work_around_b_24465209__) 301 strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_)); 302#endif 303 } 304 305 return __libdl_info; 306} 307