dlfcn.cpp revision 3b297c40794b23d50cb5240f9b03f6ef25fd98db
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 <dlfcn.h> 18#include <pthread.h> 19#include <stdio.h> 20 21#include <private/ScopedPthreadMutexLocker.h> 22 23#include "linker.h" 24#include "linker_format.h" 25 26/* This file hijacks the symbols stubbed out in libdl.so. */ 27 28static char dl_err_buf[1024]; 29static const char* dl_err_str; 30 31#define likely(expr) __builtin_expect (expr, 1) 32#define unlikely(expr) __builtin_expect (expr, 0) 33 34pthread_mutex_t dl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; 35 36static void set_dlerror(const char* msg, const char* detail) { 37 if (detail != NULL) { 38 format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s: %s", msg, detail); 39 } else { 40 format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s", msg); 41 } 42 dl_err_str = (const char*) &dl_err_buf[0]; 43} 44 45void *dlopen(const char* filename, int flag) { 46 ScopedPthreadMutexLocker locker(&dl_lock); 47 soinfo* result = find_library(filename); 48 if (result == NULL) { 49 set_dlerror("dlopen failed", linker_get_error()); 50 return NULL; 51 } 52 soinfo_call_constructors(result); 53 result->refcount++; 54 return result; 55} 56 57const char* dlerror() { 58 const char* old_value = dl_err_str; 59 dl_err_str = NULL; 60 return (const char*) old_value; 61} 62 63void* dlsym(void* handle, const char* symbol) { 64 ScopedPthreadMutexLocker locker(&dl_lock); 65 66 if (unlikely(handle == 0)) { 67 set_dlerror("dlsym library handle is null", NULL); 68 return NULL; 69 } 70 if (unlikely(symbol == 0)) { 71 set_dlerror("dlsym symbol name is null", NULL); 72 return NULL; 73 } 74 75 soinfo* found = NULL; 76 Elf32_Sym* sym = NULL; 77 if (handle == RTLD_DEFAULT) { 78 sym = lookup(symbol, &found, NULL); 79 } else if (handle == RTLD_NEXT) { 80 void* ret_addr = __builtin_return_address(0); 81 soinfo* si = find_containing_library(ret_addr); 82 83 sym = NULL; 84 if (si && si->next) { 85 sym = lookup(symbol, &found, si->next); 86 } 87 } else { 88 found = (soinfo*) handle; 89 sym = soinfo_lookup(found, symbol); 90 } 91 92 if (likely(sym != 0)) { 93 unsigned bind = ELF32_ST_BIND(sym->st_info); 94 95 if (likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) { 96 unsigned ret = sym->st_value + found->load_bias; 97 return (void*) ret; 98 } 99 100 set_dlerror("symbol found but not global", symbol); 101 return NULL; 102 } else { 103 set_dlerror("undefined symbol", symbol); 104 return NULL; 105 } 106} 107 108int dladdr(const void* addr, Dl_info* info) { 109 ScopedPthreadMutexLocker locker(&dl_lock); 110 111 // Determine if this address can be found in any library currently mapped. 112 soinfo* si = find_containing_library(addr); 113 if (si == NULL) { 114 return 0; 115 } 116 117 memset(info, 0, sizeof(Dl_info)); 118 119 info->dli_fname = si->name; 120 // Address at which the shared object is loaded. 121 info->dli_fbase = (void*) si->base; 122 123 // Determine if any symbol in the library contains the specified address. 124 Elf32_Sym *sym = soinfo_find_symbol(si, addr); 125 if (sym != NULL) { 126 info->dli_sname = si->strtab + sym->st_name; 127 info->dli_saddr = (void*)(si->load_bias + sym->st_value); 128 } 129 130 return 1; 131} 132 133int dlclose(void* handle) { 134 ScopedPthreadMutexLocker locker(&dl_lock); 135 return soinfo_unload((soinfo*) handle); 136} 137 138#if defined(ANDROID_ARM_LINKER) 139// 0000000 00011111 111112 22222222 2333333 333344444444445555555 140// 0123456 78901234 567890 12345678 9012345 678901234567890123456 141#define ANDROID_LIBDL_STRTAB \ 142 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0" 143 144#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) 145// 0000000 00011111 111112 22222222 2333333 3333444444444455 146// 0123456 78901234 567890 12345678 9012345 6789012345678901 147#define ANDROID_LIBDL_STRTAB \ 148 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0" 149#else 150#error Unsupported architecture. Only ARM, MIPS, and x86 are presently supported. 151#endif 152 153// name_offset: starting index of the name in libdl_info.strtab 154#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ 155 { name_offset, \ 156 reinterpret_cast<Elf32_Addr>(reinterpret_cast<void*>(value)), \ 157 /* st_size */ 0, \ 158 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \ 159 /* st_other */ 0, \ 160 shndx } 161 162static Elf32_Sym libdl_symtab[] = { 163 // Total length of libdl_info.strtab, including trailing 0. 164 // This is actually the STH_UNDEF entry. Technically, it's 165 // supposed to have st_name == 0, but instead, it points to an index 166 // in the strtab with a \0 to make iterating through the symtab easier. 167 ELF32_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0), 168 ELF32_SYM_INITIALIZER( 0, &dlopen, 1), 169 ELF32_SYM_INITIALIZER( 7, &dlclose, 1), 170 ELF32_SYM_INITIALIZER(15, &dlsym, 1), 171 ELF32_SYM_INITIALIZER(21, &dlerror, 1), 172 ELF32_SYM_INITIALIZER(29, &dladdr, 1), 173#if defined(ANDROID_ARM_LINKER) 174 ELF32_SYM_INITIALIZER(36, &dl_unwind_find_exidx, 1), 175#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) 176 ELF32_SYM_INITIALIZER(36, &dl_iterate_phdr, 1), 177#endif 178}; 179 180/* Fake out a hash table with a single bucket. 181 * A search of the hash table will look through 182 * libdl_symtab starting with index [1], then 183 * use libdl_chains to find the next index to 184 * look at. libdl_chains should be set up to 185 * walk through every element in libdl_symtab, 186 * and then end with 0 (sentinel value). 187 * 188 * I.e., libdl_chains should look like 189 * { 0, 2, 3, ... N, 0 } where N is the number 190 * of actual symbols, or nelems(libdl_symtab)-1 191 * (since the first element of libdl_symtab is not 192 * a real symbol). 193 * 194 * (see _elf_lookup()) 195 * 196 * Note that adding any new symbols here requires 197 * stubbing them out in libdl. 198 */ 199static unsigned libdl_buckets[1] = { 1 }; 200static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 }; 201 202soinfo libdl_info = { 203 name: "libdl.so", 204 205 phdr: 0, phnum: 0, 206 entry: 0, base: 0, size: 0, 207 unused: 0, dynamic: 0, unused2: 0, unused3: 0, 208 next: 0, 209 210 flags: FLAG_LINKED, 211 212 strtab: ANDROID_LIBDL_STRTAB, 213 symtab: libdl_symtab, 214 215 nbucket: 1, 216 nchain: 7, 217 bucket: libdl_buckets, 218 chain: libdl_chains, 219 220 plt_got: 0, plt_rel: 0, plt_rel_count: 0, rel: 0, rel_count: 0, 221 preinit_array: 0, preinit_array_count: 0, init_array: 0, init_array_count: 0, 222 fini_array: 0, fini_array_count: 0, init_func: 0, fini_func: 0, 223 224#if defined(ANDROID_ARM_LINKER) 225 ARM_exidx: 0, ARM_exidx_count: 0, 226#elif defined(ANDROID_MIPS_LINKER) 227 mips_symtabno: 0, mips_local_gotno: 0, mips_gotsym: 0, 228#endif 229 230 refcount: 0, 231 { l_addr: 0, l_name: 0, l_ld: 0, l_next: 0, l_prev: 0, }, 232 constructors_called: 0, load_bias: 0, has_text_relocations: 0, 233}; 234