The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [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 | #include <dlfcn.h> |
| 17 | #include <pthread.h> |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 19 | #include "linker.h" |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 20 | #include "linker_format.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 21 | |
| 22 | /* This file hijacks the symbols stubbed out in libdl.so. */ |
| 23 | |
| 24 | #define DL_SUCCESS 0 |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 25 | #define DL_ERR_CANNOT_LOAD_LIBRARY 1 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 26 | #define DL_ERR_INVALID_LIBRARY_HANDLE 2 |
| 27 | #define DL_ERR_BAD_SYMBOL_NAME 3 |
| 28 | #define DL_ERR_SYMBOL_NOT_FOUND 4 |
| 29 | #define DL_ERR_SYMBOL_NOT_GLOBAL 5 |
| 30 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 31 | static char dl_err_buf[1024]; |
| 32 | static const char *dl_err_str; |
| 33 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | static const char *dl_errors[] = { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 35 | [DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load library", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | [DL_ERR_INVALID_LIBRARY_HANDLE] = "Invalid library handle", |
| 37 | [DL_ERR_BAD_SYMBOL_NAME] = "Invalid symbol name", |
| 38 | [DL_ERR_SYMBOL_NOT_FOUND] = "Symbol not found", |
| 39 | [DL_ERR_SYMBOL_NOT_GLOBAL] = "Symbol is not global", |
| 40 | }; |
| 41 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | #define likely(expr) __builtin_expect (expr, 1) |
| 43 | #define unlikely(expr) __builtin_expect (expr, 0) |
| 44 | |
Pavel Chupin | e19d702 | 2012-02-20 10:49:13 +0400 | [diff] [blame] | 45 | pthread_mutex_t dl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 47 | static void set_dlerror(int err) |
| 48 | { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 49 | format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err], |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 50 | linker_get_error()); |
| 51 | dl_err_str = (const char *)&dl_err_buf[0]; |
| 52 | }; |
| 53 | |
| 54 | void *dlopen(const char *filename, int flag) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 55 | { |
| 56 | soinfo *ret; |
| 57 | |
| 58 | pthread_mutex_lock(&dl_lock); |
| 59 | ret = find_library(filename); |
| 60 | if (unlikely(ret == NULL)) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 61 | set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 | } else { |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 63 | soinfo_call_constructors(ret); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | ret->refcount++; |
| 65 | } |
| 66 | pthread_mutex_unlock(&dl_lock); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | const char *dlerror(void) |
| 71 | { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 72 | const char *tmp = dl_err_str; |
| 73 | dl_err_str = NULL; |
| 74 | return (const char *)tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void *dlsym(void *handle, const char *symbol) |
| 78 | { |
Iliyan Malchev | 9ea64da | 2009-09-28 18:21:30 -0700 | [diff] [blame] | 79 | soinfo *found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | Elf32_Sym *sym; |
| 81 | unsigned bind; |
| 82 | |
| 83 | pthread_mutex_lock(&dl_lock); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 84 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 85 | if(unlikely(handle == 0)) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 86 | set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | goto err; |
| 88 | } |
| 89 | if(unlikely(symbol == 0)) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 90 | set_dlerror(DL_ERR_BAD_SYMBOL_NAME); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | goto err; |
| 92 | } |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 94 | if(handle == RTLD_DEFAULT) { |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 95 | sym = lookup(symbol, &found, NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 96 | } else if(handle == RTLD_NEXT) { |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 97 | void *ret_addr = __builtin_return_address(0); |
| 98 | soinfo *si = find_containing_library(ret_addr); |
| 99 | |
| 100 | sym = NULL; |
| 101 | if(si && si->next) { |
| 102 | sym = lookup(symbol, &found, si->next); |
| 103 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | } else { |
Iliyan Malchev | 9ea64da | 2009-09-28 18:21:30 -0700 | [diff] [blame] | 105 | found = (soinfo*)handle; |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 106 | sym = soinfo_lookup(found, symbol); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if(likely(sym != 0)) { |
| 110 | bind = ELF32_ST_BIND(sym->st_info); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 111 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) { |
Raghu Gandham | c1993ca | 2012-09-21 12:21:02 -0700 | [diff] [blame] | 113 | unsigned ret = sym->st_value + found->load_bias; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 114 | pthread_mutex_unlock(&dl_lock); |
| 115 | return (void*)ret; |
| 116 | } |
| 117 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 118 | set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | } |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 120 | else |
| 121 | set_dlerror(DL_ERR_SYMBOL_NOT_FOUND); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | |
| 123 | err: |
| 124 | pthread_mutex_unlock(&dl_lock); |
| 125 | return 0; |
| 126 | } |
| 127 | |
Mathias Agopian | bda5da0 | 2011-09-27 22:30:19 -0700 | [diff] [blame] | 128 | int dladdr(const void *addr, Dl_info *info) |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 129 | { |
| 130 | int ret = 0; |
| 131 | |
| 132 | pthread_mutex_lock(&dl_lock); |
| 133 | |
| 134 | /* Determine if this address can be found in any library currently mapped */ |
| 135 | soinfo *si = find_containing_library(addr); |
| 136 | |
| 137 | if(si) { |
| 138 | memset(info, 0, sizeof(Dl_info)); |
| 139 | |
| 140 | info->dli_fname = si->name; |
Raghu Gandham | e6dc2a6 | 2012-09-25 15:50:33 -0700 | [diff] [blame^] | 141 | /* Address at which the shared object is loaded */ |
| 142 | info->dli_fbase = (void*)si->base; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 143 | |
| 144 | /* Determine if any symbol in the library contains the specified address */ |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 145 | Elf32_Sym *sym = soinfo_find_symbol(si, addr); |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 146 | |
| 147 | if(sym != NULL) { |
| 148 | info->dli_sname = si->strtab + sym->st_name; |
Raghu Gandham | c1993ca | 2012-09-21 12:21:02 -0700 | [diff] [blame] | 149 | info->dli_saddr = (void*)(si->load_bias + sym->st_value); |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | ret = 1; |
| 153 | } |
| 154 | |
| 155 | pthread_mutex_unlock(&dl_lock); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 160 | int dlclose(void* handle) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | pthread_mutex_lock(&dl_lock); |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 162 | int result = soinfo_unload((soinfo*)handle); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 163 | pthread_mutex_unlock(&dl_lock); |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 164 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | #if defined(ANDROID_ARM_LINKER) |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 168 | // 0000000 00011111 111112 22222222 2333333 333344444444445555555 |
| 169 | // 0123456 78901234 567890 12345678 9012345 678901234567890123456 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 170 | #define ANDROID_LIBDL_STRTAB \ |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 171 | "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 | |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 173 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 174 | // 0000000 00011111 111112 22222222 2333333 3333444444444455 |
| 175 | // 0123456 78901234 567890 12345678 9012345 6789012345678901 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | #define ANDROID_LIBDL_STRTAB \ |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 177 | "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0" |
David 'Digit' Turner | 177a770 | 2012-02-01 19:38:58 +0100 | [diff] [blame] | 178 | #else |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 179 | #error Unsupported architecture. Only ARM, MIPS, and x86 are presently supported. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | #endif |
| 181 | |
| 182 | |
| 183 | static Elf32_Sym libdl_symtab[] = { |
| 184 | // total length of libdl_info.strtab, including trailing 0 |
| 185 | // This is actually the the STH_UNDEF entry. Technically, it's |
| 186 | // supposed to have st_name == 0, but instead, it points to an index |
| 187 | // in the strtab with a \0 to make iterating through the symtab easier. |
| 188 | { st_name: sizeof(ANDROID_LIBDL_STRTAB) - 1, |
| 189 | }, |
| 190 | { st_name: 0, // starting index of the name in libdl_info.strtab |
| 191 | st_value: (Elf32_Addr) &dlopen, |
| 192 | st_info: STB_GLOBAL << 4, |
| 193 | st_shndx: 1, |
| 194 | }, |
| 195 | { st_name: 7, |
| 196 | st_value: (Elf32_Addr) &dlclose, |
| 197 | st_info: STB_GLOBAL << 4, |
| 198 | st_shndx: 1, |
| 199 | }, |
| 200 | { st_name: 15, |
| 201 | st_value: (Elf32_Addr) &dlsym, |
| 202 | st_info: STB_GLOBAL << 4, |
| 203 | st_shndx: 1, |
| 204 | }, |
| 205 | { st_name: 21, |
| 206 | st_value: (Elf32_Addr) &dlerror, |
| 207 | st_info: STB_GLOBAL << 4, |
| 208 | st_shndx: 1, |
| 209 | }, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 210 | { st_name: 29, |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 211 | st_value: (Elf32_Addr) &dladdr, |
| 212 | st_info: STB_GLOBAL << 4, |
| 213 | st_shndx: 1, |
| 214 | }, |
| 215 | #ifdef ANDROID_ARM_LINKER |
| 216 | { st_name: 36, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 217 | st_value: (Elf32_Addr) &dl_unwind_find_exidx, |
| 218 | st_info: STB_GLOBAL << 4, |
| 219 | st_shndx: 1, |
| 220 | }, |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 221 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 222 | { st_name: 36, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 223 | st_value: (Elf32_Addr) &dl_iterate_phdr, |
| 224 | st_info: STB_GLOBAL << 4, |
| 225 | st_shndx: 1, |
| 226 | }, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 227 | #endif |
| 228 | }; |
| 229 | |
| 230 | /* Fake out a hash table with a single bucket. |
| 231 | * A search of the hash table will look through |
| 232 | * libdl_symtab starting with index [1], then |
| 233 | * use libdl_chains to find the next index to |
| 234 | * look at. libdl_chains should be set up to |
| 235 | * walk through every element in libdl_symtab, |
| 236 | * and then end with 0 (sentinel value). |
| 237 | * |
| 238 | * I.e., libdl_chains should look like |
| 239 | * { 0, 2, 3, ... N, 0 } where N is the number |
| 240 | * of actual symbols, or nelems(libdl_symtab)-1 |
| 241 | * (since the first element of libdl_symtab is not |
| 242 | * a real symbol). |
| 243 | * |
| 244 | * (see _elf_lookup()) |
| 245 | * |
| 246 | * Note that adding any new symbols here requires |
| 247 | * stubbing them out in libdl. |
| 248 | */ |
| 249 | static unsigned libdl_buckets[1] = { 1 }; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 250 | static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 }; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 251 | |
| 252 | soinfo libdl_info = { |
| 253 | name: "libdl.so", |
| 254 | flags: FLAG_LINKED, |
| 255 | |
| 256 | strtab: ANDROID_LIBDL_STRTAB, |
| 257 | symtab: libdl_symtab, |
| 258 | |
| 259 | nbucket: 1, |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 260 | nchain: 7, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 261 | bucket: libdl_buckets, |
| 262 | chain: libdl_chains, |
| 263 | }; |