dlfcn.cpp revision aae859cc3ca127d890e853cbf12b731e05624a22
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 19#include <dlfcn.h> 20#include <pthread.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include <string.h> 24#include <android/dlext.h> 25 26#include <bionic/pthread_internal.h> 27#include "private/bionic_tls.h" 28#include "private/ScopedPthreadMutexLocker.h" 29#include "private/ThreadLocalBuffer.h" 30 31/* This file hijacks the symbols stubbed out in libdl.so. */ 32 33static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 34 35static const char* __bionic_set_dlerror(char* new_value) { 36 char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR]; 37 38 const char* old_value = *dlerror_slot; 39 *dlerror_slot = new_value; 40 return old_value; 41} 42 43static void __bionic_format_dlerror(const char* msg, const char* detail) { 44 char* buffer = __get_thread()->dlerror_buffer; 45 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE); 46 if (detail != nullptr) { 47 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE); 48 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE); 49 } 50 51 __bionic_set_dlerror(buffer); 52} 53 54const char* dlerror() { 55 const char* old_value = __bionic_set_dlerror(nullptr); 56 return old_value; 57} 58 59void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { 60 ScopedPthreadMutexLocker locker(&g_dl_mutex); 61 do_android_get_LD_LIBRARY_PATH(buffer, buffer_size); 62} 63 64void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { 65 ScopedPthreadMutexLocker locker(&g_dl_mutex); 66 do_android_update_LD_LIBRARY_PATH(ld_library_path); 67} 68 69static void* dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) { 70 ScopedPthreadMutexLocker locker(&g_dl_mutex); 71 soinfo* result = do_dlopen(filename, flags, extinfo); 72 if (result == nullptr) { 73 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); 74 return nullptr; 75 } 76 return result; 77} 78 79void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) { 80 return dlopen_ext(filename, flags, extinfo); 81} 82 83void* dlopen(const char* filename, int flags) { 84 return dlopen_ext(filename, flags, nullptr); 85} 86 87void* dlsym(void* handle, const char* symbol) { 88 ScopedPthreadMutexLocker locker(&g_dl_mutex); 89 90#if !defined(__LP64__) 91 if (handle == nullptr) { 92 __bionic_format_dlerror("dlsym library handle is null", nullptr); 93 return nullptr; 94 } 95#endif 96 97 if (symbol == nullptr) { 98 __bionic_format_dlerror("dlsym symbol name is null", nullptr); 99 return nullptr; 100 } 101 102 soinfo* found = nullptr; 103 ElfW(Sym)* sym = nullptr; 104 void* caller_addr = __builtin_return_address(0); 105 soinfo* caller = find_containing_library(caller_addr); 106 107 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) { 108 sym = dlsym_linear_lookup(symbol, &found, caller, handle); 109 } else { 110 sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, symbol); 111 } 112 113 if (sym != nullptr) { 114 unsigned bind = ELF_ST_BIND(sym->st_info); 115 116 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) { 117 return reinterpret_cast<void*>(found->resolve_symbol_address(sym)); 118 } 119 120 __bionic_format_dlerror("symbol found but not global", symbol); 121 return nullptr; 122 } else { 123 __bionic_format_dlerror("undefined symbol", symbol); 124 return nullptr; 125 } 126} 127 128int dladdr(const void* addr, Dl_info* info) { 129 ScopedPthreadMutexLocker locker(&g_dl_mutex); 130 131 // Determine if this address can be found in any library currently mapped. 132 soinfo* si = find_containing_library(addr); 133 if (si == nullptr) { 134 return 0; 135 } 136 137 memset(info, 0, sizeof(Dl_info)); 138 139 info->dli_fname = si->get_realpath(); 140 // Address at which the shared object is loaded. 141 info->dli_fbase = reinterpret_cast<void*>(si->base); 142 143 // Determine if any symbol in the library contains the specified address. 144 ElfW(Sym)* sym = si->find_symbol_by_address(addr); 145 if (sym != nullptr) { 146 info->dli_sname = si->get_string(sym->st_name); 147 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym)); 148 } 149 150 return 1; 151} 152 153int dlclose(void* handle) { 154 ScopedPthreadMutexLocker locker(&g_dl_mutex); 155 do_dlclose(reinterpret_cast<soinfo*>(handle)); 156 // dlclose has no defined errors. 157 return 0; 158} 159 160// name_offset: starting index of the name in libdl_info.strtab 161#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ 162 { name_offset, \ 163 reinterpret_cast<Elf32_Addr>(value), \ 164 /* st_size */ 0, \ 165 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \ 166 /* st_other */ 0, \ 167 shndx, \ 168 } 169 170#define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \ 171 { name_offset, \ 172 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \ 173 /* st_other */ 0, \ 174 shndx, \ 175 reinterpret_cast<Elf64_Addr>(value), \ 176 /* st_size */ 0, \ 177 } 178 179#if defined(__arm__) 180 // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222 333333333344444444445 181 // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789 012345678901234567890 182# define ANDROID_LIBDL_STRTAB \ 183 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\0dl_unwind_find_exidx\0" 184#elif defined(__aarch64__) || defined(__i386__) || defined(__mips__) || defined(__x86_64__) 185 // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222 186 // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789 187# define ANDROID_LIBDL_STRTAB \ 188 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\0" 189#else 190# error Unsupported architecture. Only arm, arm64, mips, mips64, x86 and x86_64 are presently supported. 191#endif 192 193static ElfW(Sym) g_libdl_symtab[] = { 194 // Total length of libdl_info.strtab, including trailing 0. 195 // This is actually the STH_UNDEF entry. Technically, it's 196 // supposed to have st_name == 0, but instead, it points to an index 197 // in the strtab with a \0 to make iterating through the symtab easier. 198 ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0), 199 ELFW(SYM_INITIALIZER)( 0, &dlopen, 1), 200 ELFW(SYM_INITIALIZER)( 7, &dlclose, 1), 201 ELFW(SYM_INITIALIZER)( 15, &dlsym, 1), 202 ELFW(SYM_INITIALIZER)( 21, &dlerror, 1), 203 ELFW(SYM_INITIALIZER)( 29, &dladdr, 1), 204 ELFW(SYM_INITIALIZER)( 36, &android_update_LD_LIBRARY_PATH, 1), 205 ELFW(SYM_INITIALIZER)( 67, &android_get_LD_LIBRARY_PATH, 1), 206 ELFW(SYM_INITIALIZER)( 95, &dl_iterate_phdr, 1), 207 ELFW(SYM_INITIALIZER)(111, &android_dlopen_ext, 1), 208#if defined(__arm__) 209 ELFW(SYM_INITIALIZER)(130, &dl_unwind_find_exidx, 1), 210#endif 211}; 212 213// Fake out a hash table with a single bucket. 214// 215// A search of the hash table will look through g_libdl_symtab starting with index 1, then 216// use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to 217// walk through every element in g_libdl_symtab, and then end with 0 (sentinel value). 218// 219// That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number 220// of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not 221// a real symbol). (See soinfo_elf_lookup().) 222// 223// Note that adding any new symbols here requires stubbing them out in libdl. 224static unsigned g_libdl_buckets[1] = { 1 }; 225#if defined(__arm__) 226static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 }; 227#else 228static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 229#endif 230 231static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8))); 232static soinfo* __libdl_info = nullptr; 233 234// This is used by the dynamic linker. Every process gets these symbols for free. 235soinfo* get_libdl_info() { 236 if (__libdl_info == nullptr) { 237 __libdl_info = new (__libdl_info_buf) soinfo("libdl.so", nullptr, 0, RTLD_GLOBAL); 238 __libdl_info->flags_ |= FLAG_LINKED; 239 __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB; 240 __libdl_info->symtab_ = g_libdl_symtab; 241 __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned); 242 __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned); 243 __libdl_info->bucket_ = g_libdl_buckets; 244 __libdl_info->chain_ = g_libdl_chains; 245 __libdl_info->ref_count_ = 1; 246 __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB); 247 __libdl_info->local_group_root_ = __libdl_info; 248 __libdl_info->soname_ = "libdl.so"; 249#if defined(__arm__) 250 strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_)); 251#endif 252 } 253 254 return __libdl_info; 255} 256