blob: 30f5f4cef4230ce3288f9506461fd655bd8555e1 [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"
20
21/* This file hijacks the symbols stubbed out in libdl.so. */
22
23#define DL_SUCCESS 0
Dima Zavin2e855792009-05-20 18:28:09 -070024#define DL_ERR_CANNOT_LOAD_LIBRARY 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025#define DL_ERR_INVALID_LIBRARY_HANDLE 2
26#define DL_ERR_BAD_SYMBOL_NAME 3
27#define DL_ERR_SYMBOL_NOT_FOUND 4
28#define DL_ERR_SYMBOL_NOT_GLOBAL 5
29
Dima Zavin2e855792009-05-20 18:28:09 -070030static char dl_err_buf[1024];
31static const char *dl_err_str;
32
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033static const char *dl_errors[] = {
Dima Zavin2e855792009-05-20 18:28:09 -070034 [DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load library",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035 [DL_ERR_INVALID_LIBRARY_HANDLE] = "Invalid library handle",
36 [DL_ERR_BAD_SYMBOL_NAME] = "Invalid symbol name",
37 [DL_ERR_SYMBOL_NOT_FOUND] = "Symbol not found",
38 [DL_ERR_SYMBOL_NOT_GLOBAL] = "Symbol is not global",
39};
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#define likely(expr) __builtin_expect (expr, 1)
42#define unlikely(expr) __builtin_expect (expr, 0)
43
44static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
45
Dima Zavin2e855792009-05-20 18:28:09 -070046static void set_dlerror(int err)
47{
48 snprintf(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err],
49 linker_get_error());
50 dl_err_str = (const char *)&dl_err_buf[0];
51};
52
53void *dlopen(const char *filename, int flag)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054{
55 soinfo *ret;
56
57 pthread_mutex_lock(&dl_lock);
58 ret = find_library(filename);
59 if (unlikely(ret == NULL)) {
Dima Zavin2e855792009-05-20 18:28:09 -070060 set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061 } else {
62 ret->refcount++;
63 }
64 pthread_mutex_unlock(&dl_lock);
65 return ret;
66}
67
68const char *dlerror(void)
69{
Dima Zavin2e855792009-05-20 18:28:09 -070070 const char *tmp = dl_err_str;
71 dl_err_str = NULL;
72 return (const char *)tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073}
74
75void *dlsym(void *handle, const char *symbol)
76{
Iliyan Malchev9ea64da2009-09-28 18:21:30 -070077 soinfo *found;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078 Elf32_Sym *sym;
79 unsigned bind;
80
81 pthread_mutex_lock(&dl_lock);
Dima Zavin2e855792009-05-20 18:28:09 -070082
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083 if(unlikely(handle == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -070084 set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085 goto err;
86 }
87 if(unlikely(symbol == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -070088 set_dlerror(DL_ERR_BAD_SYMBOL_NAME);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089 goto err;
90 }
Dima Zavin2e855792009-05-20 18:28:09 -070091
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092 if(handle == RTLD_DEFAULT) {
Matt Fischer1698d9e2009-12-31 12:17:56 -060093 sym = lookup(symbol, &found, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094 } else if(handle == RTLD_NEXT) {
Matt Fischer1698d9e2009-12-31 12:17:56 -060095 void *ret_addr = __builtin_return_address(0);
96 soinfo *si = find_containing_library(ret_addr);
97
98 sym = NULL;
99 if(si && si->next) {
100 sym = lookup(symbol, &found, si->next);
101 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102 } else {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700103 found = (soinfo*)handle;
104 sym = lookup_in_library(found, symbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105 }
106
107 if(likely(sym != 0)) {
108 bind = ELF32_ST_BIND(sym->st_info);
Dima Zavin2e855792009-05-20 18:28:09 -0700109
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110 if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700111 unsigned ret = sym->st_value + found->base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112 pthread_mutex_unlock(&dl_lock);
113 return (void*)ret;
114 }
115
Dima Zavin2e855792009-05-20 18:28:09 -0700116 set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117 }
Dima Zavin2e855792009-05-20 18:28:09 -0700118 else
119 set_dlerror(DL_ERR_SYMBOL_NOT_FOUND);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
121err:
122 pthread_mutex_unlock(&dl_lock);
123 return 0;
124}
125
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600126int dladdr(void *addr, Dl_info *info)
127{
128 int ret = 0;
129
130 pthread_mutex_lock(&dl_lock);
131
132 /* Determine if this address can be found in any library currently mapped */
133 soinfo *si = find_containing_library(addr);
134
135 if(si) {
136 memset(info, 0, sizeof(Dl_info));
137
138 info->dli_fname = si->name;
139 info->dli_fbase = (void*)si->base;
140
141 /* Determine if any symbol in the library contains the specified address */
142 Elf32_Sym *sym = find_containing_symbol(addr, si);
143
144 if(sym != NULL) {
145 info->dli_sname = si->strtab + sym->st_name;
146 info->dli_saddr = (void*)(si->base + sym->st_value);
147 }
148
149 ret = 1;
150 }
151
152 pthread_mutex_unlock(&dl_lock);
153
154 return ret;
155}
156
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157int dlclose(void *handle)
158{
159 pthread_mutex_lock(&dl_lock);
160 (void)unload_library((soinfo*)handle);
161 pthread_mutex_unlock(&dl_lock);
162 return 0;
163}
164
165#if defined(ANDROID_ARM_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600166// 0000000 00011111 111112 22222222 2333333 333344444444445555555
167// 0123456 78901234 567890 12345678 9012345 678901234567890123456
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600169 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170
171#elif defined(ANDROID_X86_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600172// 0000000 00011111 111112 22222222 2333333 3333444444444455
173// 0123456 78901234 567890 12345678 9012345 6789012345678901
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600175 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900177#elif defined(ANDROID_SH_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600178// 0000000 00011111 111112 22222222 2333333 3333444444444455
179// 0123456 78901234 567890 12345678 9012345 6789012345678901
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900180#define ANDROID_LIBDL_STRTAB \
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600181 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183#else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
184#error Unsupported architecture. Only ARM and x86 are presently supported.
185#endif
186
187
188static Elf32_Sym libdl_symtab[] = {
189 // total length of libdl_info.strtab, including trailing 0
190 // This is actually the the STH_UNDEF entry. Technically, it's
191 // supposed to have st_name == 0, but instead, it points to an index
192 // in the strtab with a \0 to make iterating through the symtab easier.
193 { st_name: sizeof(ANDROID_LIBDL_STRTAB) - 1,
194 },
195 { st_name: 0, // starting index of the name in libdl_info.strtab
196 st_value: (Elf32_Addr) &dlopen,
197 st_info: STB_GLOBAL << 4,
198 st_shndx: 1,
199 },
200 { st_name: 7,
201 st_value: (Elf32_Addr) &dlclose,
202 st_info: STB_GLOBAL << 4,
203 st_shndx: 1,
204 },
205 { st_name: 15,
206 st_value: (Elf32_Addr) &dlsym,
207 st_info: STB_GLOBAL << 4,
208 st_shndx: 1,
209 },
210 { st_name: 21,
211 st_value: (Elf32_Addr) &dlerror,
212 st_info: STB_GLOBAL << 4,
213 st_shndx: 1,
214 },
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215 { st_name: 29,
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600216 st_value: (Elf32_Addr) &dladdr,
217 st_info: STB_GLOBAL << 4,
218 st_shndx: 1,
219 },
220#ifdef ANDROID_ARM_LINKER
221 { st_name: 36,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222 st_value: (Elf32_Addr) &dl_unwind_find_exidx,
223 st_info: STB_GLOBAL << 4,
224 st_shndx: 1,
225 },
226#elif defined(ANDROID_X86_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600227 { st_name: 36,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228 st_value: (Elf32_Addr) &dl_iterate_phdr,
229 st_info: STB_GLOBAL << 4,
230 st_shndx: 1,
231 },
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900232#elif defined(ANDROID_SH_LINKER)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600233 { st_name: 36,
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900234 st_value: (Elf32_Addr) &dl_iterate_phdr,
235 st_info: STB_GLOBAL << 4,
236 st_shndx: 1,
237 },
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238#endif
239};
240
241/* Fake out a hash table with a single bucket.
242 * A search of the hash table will look through
243 * libdl_symtab starting with index [1], then
244 * use libdl_chains to find the next index to
245 * look at. libdl_chains should be set up to
246 * walk through every element in libdl_symtab,
247 * and then end with 0 (sentinel value).
248 *
249 * I.e., libdl_chains should look like
250 * { 0, 2, 3, ... N, 0 } where N is the number
251 * of actual symbols, or nelems(libdl_symtab)-1
252 * (since the first element of libdl_symtab is not
253 * a real symbol).
254 *
255 * (see _elf_lookup())
256 *
257 * Note that adding any new symbols here requires
258 * stubbing them out in libdl.
259 */
260static unsigned libdl_buckets[1] = { 1 };
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600261static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262
263soinfo libdl_info = {
264 name: "libdl.so",
265 flags: FLAG_LINKED,
266
267 strtab: ANDROID_LIBDL_STRTAB,
268 symtab: libdl_symtab,
269
270 nbucket: 1,
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600271 nchain: 7,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272 bucket: libdl_buckets,
273 chain: libdl_chains,
274};
275