Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 17 | #include "linker.h" |
| 18 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 19 | #include <dlfcn.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 24 | #include <bionic/pthread_internal.h> |
| 25 | #include <private/bionic_tls.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 26 | #include <private/ScopedPthreadMutexLocker.h> |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 27 | #include <private/ThreadLocalBuffer.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 28 | |
| 29 | /* This file hijacks the symbols stubbed out in libdl.so. */ |
| 30 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 31 | static pthread_mutex_t gDlMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 33 | static const char* __bionic_set_dlerror(char* new_value) { |
| 34 | void* tls = const_cast<void*>(__get_tls()); |
| 35 | char** dlerror_slot = &reinterpret_cast<char**>(tls)[TLS_SLOT_DLERROR]; |
| 36 | |
| 37 | const char* old_value = *dlerror_slot; |
| 38 | *dlerror_slot = new_value; |
| 39 | return old_value; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 42 | static void __bionic_format_dlerror(const char* msg, const char* detail) { |
| 43 | char* buffer = __get_thread()->dlerror_buffer; |
| 44 | strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE); |
| 45 | if (detail != NULL) { |
| 46 | strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE); |
| 47 | strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE); |
| 48 | } |
| 49 | |
| 50 | __bionic_set_dlerror(buffer); |
| 51 | } |
| 52 | |
| 53 | const char* dlerror() { |
| 54 | const char* old_value = __bionic_set_dlerror(NULL); |
| 55 | return old_value; |
| 56 | } |
| 57 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 58 | void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { |
| 59 | ScopedPthreadMutexLocker locker(&gDlMutex); |
| 60 | do_android_update_LD_LIBRARY_PATH(ld_library_path); |
| 61 | } |
| 62 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 63 | void* dlopen(const char* filename, int flags) { |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 64 | ScopedPthreadMutexLocker locker(&gDlMutex); |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 65 | soinfo* result = do_dlopen(filename, flags); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 66 | if (result == NULL) { |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 67 | __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 70 | return result; |
| 71 | } |
| 72 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 73 | void* dlsym(void* handle, const char* symbol) { |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 74 | ScopedPthreadMutexLocker locker(&gDlMutex); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 76 | if (handle == NULL) { |
| 77 | __bionic_format_dlerror("dlsym library handle is null", NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 80 | if (symbol == NULL) { |
| 81 | __bionic_format_dlerror("dlsym symbol name is null", NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | soinfo* found = NULL; |
| 86 | Elf32_Sym* sym = NULL; |
| 87 | if (handle == RTLD_DEFAULT) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 88 | sym = dlsym_linear_lookup(symbol, &found, NULL); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 89 | } else if (handle == RTLD_NEXT) { |
| 90 | void* ret_addr = __builtin_return_address(0); |
| 91 | soinfo* si = find_containing_library(ret_addr); |
| 92 | |
| 93 | sym = NULL; |
| 94 | if (si && si->next) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 95 | sym = dlsym_linear_lookup(symbol, &found, si->next); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 96 | } |
| 97 | } else { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 98 | found = reinterpret_cast<soinfo*>(handle); |
| 99 | sym = dlsym_handle_lookup(found, symbol); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 102 | if (sym != NULL) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 103 | unsigned bind = ELF32_ST_BIND(sym->st_info); |
| 104 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 105 | if (bind == STB_GLOBAL && sym->st_shndx != 0) { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 106 | unsigned ret = sym->st_value + found->load_bias; |
| 107 | return (void*) ret; |
| 108 | } |
| 109 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 110 | __bionic_format_dlerror("symbol found but not global", symbol); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 111 | return NULL; |
| 112 | } else { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 113 | __bionic_format_dlerror("undefined symbol", symbol); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | int dladdr(const void* addr, Dl_info* info) { |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 119 | ScopedPthreadMutexLocker locker(&gDlMutex); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 120 | |
| 121 | // Determine if this address can be found in any library currently mapped. |
| 122 | soinfo* si = find_containing_library(addr); |
| 123 | if (si == NULL) { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | memset(info, 0, sizeof(Dl_info)); |
| 128 | |
| 129 | info->dli_fname = si->name; |
| 130 | // Address at which the shared object is loaded. |
| 131 | info->dli_fbase = (void*) si->base; |
| 132 | |
| 133 | // Determine if any symbol in the library contains the specified address. |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 134 | Elf32_Sym *sym = dladdr_find_symbol(si, addr); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 135 | if (sym != NULL) { |
| 136 | info->dli_sname = si->strtab + sym->st_name; |
| 137 | info->dli_saddr = (void*)(si->load_bias + sym->st_value); |
| 138 | } |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | int dlclose(void* handle) { |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 144 | ScopedPthreadMutexLocker locker(&gDlMutex); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 145 | return do_dlclose(reinterpret_cast<soinfo*>(handle)); |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #if defined(ANDROID_ARM_LINKER) |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 149 | // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667 |
| 150 | // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890 |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 151 | #define ANDROID_LIBDL_STRTAB \ |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 152 | "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_unwind_find_exidx\0" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 153 | |
| 154 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 155 | // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667 |
| 156 | // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890 |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 157 | #define ANDROID_LIBDL_STRTAB \ |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 158 | "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_iterate_phdr\0" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 159 | #else |
| 160 | #error Unsupported architecture. Only ARM, MIPS, and x86 are presently supported. |
| 161 | #endif |
| 162 | |
| 163 | // name_offset: starting index of the name in libdl_info.strtab |
| 164 | #define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ |
| 165 | { name_offset, \ |
| 166 | reinterpret_cast<Elf32_Addr>(reinterpret_cast<void*>(value)), \ |
| 167 | /* st_size */ 0, \ |
| 168 | (shndx == 0) ? 0 : (STB_GLOBAL << 4), \ |
| 169 | /* st_other */ 0, \ |
| 170 | shndx } |
| 171 | |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 172 | static Elf32_Sym gLibDlSymtab[] = { |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 173 | // Total length of libdl_info.strtab, including trailing 0. |
| 174 | // This is actually the STH_UNDEF entry. Technically, it's |
| 175 | // supposed to have st_name == 0, but instead, it points to an index |
| 176 | // in the strtab with a \0 to make iterating through the symtab easier. |
| 177 | ELF32_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0), |
| 178 | ELF32_SYM_INITIALIZER( 0, &dlopen, 1), |
| 179 | ELF32_SYM_INITIALIZER( 7, &dlclose, 1), |
| 180 | ELF32_SYM_INITIALIZER(15, &dlsym, 1), |
| 181 | ELF32_SYM_INITIALIZER(21, &dlerror, 1), |
| 182 | ELF32_SYM_INITIALIZER(29, &dladdr, 1), |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 183 | ELF32_SYM_INITIALIZER(36, &android_update_LD_LIBRARY_PATH, 1), |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 184 | #if defined(ANDROID_ARM_LINKER) |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 185 | ELF32_SYM_INITIALIZER(67, &dl_unwind_find_exidx, 1), |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 186 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 187 | ELF32_SYM_INITIALIZER(67, &dl_iterate_phdr, 1), |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 188 | #endif |
| 189 | }; |
| 190 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 191 | // Fake out a hash table with a single bucket. |
| 192 | // A search of the hash table will look through |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 193 | // gLibDlSymtab starting with index [1], then |
| 194 | // use gLibDlChains to find the next index to |
| 195 | // look at. gLibDlChains should be set up to |
| 196 | // walk through every element in gLibDlSymtab, |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 197 | // and then end with 0 (sentinel value). |
| 198 | // |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 199 | // That is, gLibDlChains should look like |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 200 | // { 0, 2, 3, ... N, 0 } where N is the number |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 201 | // of actual symbols, or nelems(gLibDlSymtab)-1 |
| 202 | // (since the first element of gLibDlSymtab is not |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 203 | // a real symbol). |
| 204 | // |
| 205 | // (see soinfo_elf_lookup()) |
| 206 | // |
| 207 | // Note that adding any new symbols here requires |
| 208 | // stubbing them out in libdl. |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 209 | static unsigned gLibDlBuckets[1] = { 1 }; |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 210 | static unsigned gLibDlChains[8] = { 0, 2, 3, 4, 5, 6, 7, 0 }; |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 211 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 212 | // This is used by the dynamic linker. Every process gets these symbols for free. |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 213 | soinfo libdl_info = { |
Pavel Chupin | 20aa6c0 | 2012-10-25 12:17:05 +0400 | [diff] [blame] | 214 | "libdl.so", |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 215 | |
| 216 | phdr: 0, phnum: 0, |
| 217 | entry: 0, base: 0, size: 0, |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 218 | unused1: 0, dynamic: 0, unused2: 0, unused3: 0, |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 219 | next: 0, |
| 220 | |
| 221 | flags: FLAG_LINKED, |
| 222 | |
| 223 | strtab: ANDROID_LIBDL_STRTAB, |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 224 | symtab: gLibDlSymtab, |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 225 | |
| 226 | nbucket: 1, |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 227 | nchain: 8, |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 228 | bucket: gLibDlBuckets, |
| 229 | chain: gLibDlChains, |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 230 | |
| 231 | plt_got: 0, plt_rel: 0, plt_rel_count: 0, rel: 0, rel_count: 0, |
| 232 | preinit_array: 0, preinit_array_count: 0, init_array: 0, init_array_count: 0, |
| 233 | fini_array: 0, fini_array_count: 0, init_func: 0, fini_func: 0, |
| 234 | |
| 235 | #if defined(ANDROID_ARM_LINKER) |
| 236 | ARM_exidx: 0, ARM_exidx_count: 0, |
| 237 | #elif defined(ANDROID_MIPS_LINKER) |
| 238 | mips_symtabno: 0, mips_local_gotno: 0, mips_gotsym: 0, |
| 239 | #endif |
| 240 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 241 | ref_count: 0, |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 242 | { l_addr: 0, l_name: 0, l_ld: 0, l_next: 0, l_prev: 0, }, |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 243 | constructors_called: false, |
| 244 | load_bias: 0, |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 245 | has_text_relocations: false, |
| 246 | has_DT_SYMBOLIC: true, |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 247 | }; |