blob: 7161455163ce1f6f7f45c68bc36cbef28d81b241 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Zavin2e855792009-05-20 18:28:09 -070018#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080019#include "linker.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080020#include "linker_format.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080021
22/* This file hijacks the symbols stubbed out in libdl.so. */
23
24#define DL_SUCCESS 0
Dima Zavin2e855792009-05-20 18:28:09 -070025#define DL_ERR_CANNOT_LOAD_LIBRARY 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080026#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 Zavin2e855792009-05-20 18:28:09 -070031static char dl_err_buf[1024];
32static const char *dl_err_str;
33
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034static const char *dl_errors[] = {
Dima Zavin2e855792009-05-20 18:28:09 -070035 [DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load library",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036 [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 Project1dc9e472009-03-03 19:28:35 -080042#define likely(expr) __builtin_expect (expr, 1)
43#define unlikely(expr) __builtin_expect (expr, 0)
44
45static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
46
Dima Zavin2e855792009-05-20 18:28:09 -070047static void set_dlerror(int err)
48{
David 'Digit' Turner5c734642010-01-20 12:36:51 -080049 format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err],
Dima Zavin2e855792009-05-20 18:28:09 -070050 linker_get_error());
51 dl_err_str = (const char *)&dl_err_buf[0];
52};
53
54void *dlopen(const char *filename, int flag)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055{
56 soinfo *ret;
57
58 pthread_mutex_lock(&dl_lock);
59 ret = find_library(filename);
60 if (unlikely(ret == NULL)) {
Dima Zavin2e855792009-05-20 18:28:09 -070061 set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062 } else {
63 ret->refcount++;
64 }
65 pthread_mutex_unlock(&dl_lock);
66 return ret;
67}
68
69const char *dlerror(void)
70{
Dima Zavin2e855792009-05-20 18:28:09 -070071 const char *tmp = dl_err_str;
72 dl_err_str = NULL;
73 return (const char *)tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074}
75
76void *dlsym(void *handle, const char *symbol)
77{
Iliyan Malchev9ea64da2009-09-28 18:21:30 -070078 soinfo *found;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079 Elf32_Sym *sym;
80 unsigned bind;
81
82 pthread_mutex_lock(&dl_lock);
Dima Zavin2e855792009-05-20 18:28:09 -070083
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084 if(unlikely(handle == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -070085 set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086 goto err;
87 }
88 if(unlikely(symbol == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -070089 set_dlerror(DL_ERR_BAD_SYMBOL_NAME);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090 goto err;
91 }
Dima Zavin2e855792009-05-20 18:28:09 -070092
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093 if(handle == RTLD_DEFAULT) {
Matt Fischer1698d9e2009-12-31 12:17:56 -060094 sym = lookup(symbol, &found, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095 } else if(handle == RTLD_NEXT) {
Matt Fischer1698d9e2009-12-31 12:17:56 -060096 void *ret_addr = __builtin_return_address(0);
97 soinfo *si = find_containing_library(ret_addr);
98
99 sym = NULL;
100 if(si && si->next) {
101 sym = lookup(symbol, &found, si->next);
102 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 } else {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700104 found = (soinfo*)handle;
105 sym = lookup_in_library(found, symbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106 }
107
108 if(likely(sym != 0)) {
109 bind = ELF32_ST_BIND(sym->st_info);
Dima Zavin2e855792009-05-20 18:28:09 -0700110
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111 if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700112 unsigned ret = sym->st_value + found->base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113 pthread_mutex_unlock(&dl_lock);
114 return (void*)ret;
115 }
116
Dima Zavin2e855792009-05-20 18:28:09 -0700117 set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 }
Dima Zavin2e855792009-05-20 18:28:09 -0700119 else
120 set_dlerror(DL_ERR_SYMBOL_NOT_FOUND);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
122err:
123 pthread_mutex_unlock(&dl_lock);
124 return 0;
125}
126
Mathias Agopianbda5da02011-09-27 22:30:19 -0700127int dladdr(const void *addr, Dl_info *info)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600128{
129 int ret = 0;
130
131 pthread_mutex_lock(&dl_lock);
132
133 /* Determine if this address can be found in any library currently mapped */
134 soinfo *si = find_containing_library(addr);
135
136 if(si) {
137 memset(info, 0, sizeof(Dl_info));
138
139 info->dli_fname = si->name;
140 info->dli_fbase = (void*)si->base;
141
142 /* Determine if any symbol in the library contains the specified address */
143 Elf32_Sym *sym = find_containing_symbol(addr, si);
144
145 if(sym != NULL) {
146 info->dli_sname = si->strtab + sym->st_name;
147 info->dli_saddr = (void*)(si->base + sym->st_value);
148 }
149
150 ret = 1;
151 }
152
153 pthread_mutex_unlock(&dl_lock);
154
155 return ret;
156}
157
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158int dlclose(void *handle)
159{
160 pthread_mutex_lock(&dl_lock);
161 (void)unload_library((soinfo*)handle);
162 pthread_mutex_unlock(&dl_lock);
163 return 0;
164}
165
166#if defined(ANDROID_ARM_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600167// 0000000 00011111 111112 22222222 2333333 333344444444445555555
168// 0123456 78901234 567890 12345678 9012345 678901234567890123456
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600170 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171
172#elif defined(ANDROID_X86_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600173// 0000000 00011111 111112 22222222 2333333 3333444444444455
174// 0123456 78901234 567890 12345678 9012345 6789012345678901
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600176 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900178#elif defined(ANDROID_SH_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600179// 0000000 00011111 111112 22222222 2333333 3333444444444455
180// 0123456 78901234 567890 12345678 9012345 6789012345678901
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900181#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600182 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900183
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184#else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
185#error Unsupported architecture. Only ARM and x86 are presently supported.
186#endif
187
188
189static Elf32_Sym libdl_symtab[] = {
190 // total length of libdl_info.strtab, including trailing 0
191 // This is actually the the STH_UNDEF entry. Technically, it's
192 // supposed to have st_name == 0, but instead, it points to an index
193 // in the strtab with a \0 to make iterating through the symtab easier.
194 { st_name: sizeof(ANDROID_LIBDL_STRTAB) - 1,
195 },
196 { st_name: 0, // starting index of the name in libdl_info.strtab
197 st_value: (Elf32_Addr) &dlopen,
198 st_info: STB_GLOBAL << 4,
199 st_shndx: 1,
200 },
201 { st_name: 7,
202 st_value: (Elf32_Addr) &dlclose,
203 st_info: STB_GLOBAL << 4,
204 st_shndx: 1,
205 },
206 { st_name: 15,
207 st_value: (Elf32_Addr) &dlsym,
208 st_info: STB_GLOBAL << 4,
209 st_shndx: 1,
210 },
211 { st_name: 21,
212 st_value: (Elf32_Addr) &dlerror,
213 st_info: STB_GLOBAL << 4,
214 st_shndx: 1,
215 },
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216 { st_name: 29,
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600217 st_value: (Elf32_Addr) &dladdr,
218 st_info: STB_GLOBAL << 4,
219 st_shndx: 1,
220 },
221#ifdef ANDROID_ARM_LINKER
222 { st_name: 36,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223 st_value: (Elf32_Addr) &dl_unwind_find_exidx,
224 st_info: STB_GLOBAL << 4,
225 st_shndx: 1,
226 },
227#elif defined(ANDROID_X86_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600228 { st_name: 36,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229 st_value: (Elf32_Addr) &dl_iterate_phdr,
230 st_info: STB_GLOBAL << 4,
231 st_shndx: 1,
232 },
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900233#elif defined(ANDROID_SH_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600234 { st_name: 36,
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900235 st_value: (Elf32_Addr) &dl_iterate_phdr,
236 st_info: STB_GLOBAL << 4,
237 st_shndx: 1,
238 },
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239#endif
240};
241
242/* Fake out a hash table with a single bucket.
243 * A search of the hash table will look through
244 * libdl_symtab starting with index [1], then
245 * use libdl_chains to find the next index to
246 * look at. libdl_chains should be set up to
247 * walk through every element in libdl_symtab,
248 * and then end with 0 (sentinel value).
249 *
250 * I.e., libdl_chains should look like
251 * { 0, 2, 3, ... N, 0 } where N is the number
252 * of actual symbols, or nelems(libdl_symtab)-1
253 * (since the first element of libdl_symtab is not
254 * a real symbol).
255 *
256 * (see _elf_lookup())
257 *
258 * Note that adding any new symbols here requires
259 * stubbing them out in libdl.
260 */
261static unsigned libdl_buckets[1] = { 1 };
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600262static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263
264soinfo libdl_info = {
265 name: "libdl.so",
266 flags: FLAG_LINKED,
267
268 strtab: ANDROID_LIBDL_STRTAB,
269 symtab: libdl_symtab,
270
271 nbucket: 1,
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600272 nchain: 7,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273 bucket: libdl_buckets,
274 chain: libdl_chains,
275};
276