2
* Copyright (C) 2007 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
21
#include "linker_format.h"
23
/* This file hijacks the symbols stubbed out in libdl.so. */
26
#define DL_ERR_CANNOT_LOAD_LIBRARY 1
27
#define DL_ERR_INVALID_LIBRARY_HANDLE 2
28
#define DL_ERR_BAD_SYMBOL_NAME 3
29
#define DL_ERR_SYMBOL_NOT_FOUND 4
30
#define DL_ERR_SYMBOL_NOT_GLOBAL 5
32
static char dl_err_buf[1024];
33
static const char *dl_err_str;
35
static const char *dl_errors[] = {
36
[DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load library",
37
[DL_ERR_INVALID_LIBRARY_HANDLE] = "Invalid library handle",
38
[DL_ERR_BAD_SYMBOL_NAME] = "Invalid symbol name",
39
[DL_ERR_SYMBOL_NOT_FOUND] = "Symbol not found",
40
[DL_ERR_SYMBOL_NOT_GLOBAL] = "Symbol is not global",
43
#define likely(expr) __builtin_expect (expr, 1)
44
#define unlikely(expr) __builtin_expect (expr, 0)
46
static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
48
static void set_dlerror(int err)
50
format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err],
52
dl_err_str = (const char *)&dl_err_buf[0];
55
void *android_dlopen(const char *filename, int flag)
59
pthread_mutex_lock(&dl_lock);
60
ret = find_library(filename);
61
if (unlikely(ret == NULL)) {
62
set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
64
call_constructors_recursive(ret);
67
pthread_mutex_unlock(&dl_lock);
71
const char *android_dlerror(void)
73
const char *tmp = dl_err_str;
75
return (const char *)tmp;
78
void *android_dlsym(void *handle, const char *symbol)
84
pthread_mutex_lock(&dl_lock);
86
if(unlikely(handle == 0)) {
87
set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE);
90
if(unlikely(symbol == 0)) {
91
set_dlerror(DL_ERR_BAD_SYMBOL_NAME);
95
if(handle == RTLD_DEFAULT) {
96
sym = lookup(symbol, &found, NULL);
97
} else if(handle == RTLD_NEXT) {
98
void *ret_addr = __builtin_return_address(0);
99
soinfo *si = find_containing_library(ret_addr);
103
sym = lookup(symbol, &found, si->next);
106
found = (soinfo*)handle;
107
sym = lookup_in_library(found, symbol);
110
if(likely(sym != 0)) {
111
bind = ELF32_ST_BIND(sym->st_info);
113
if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
114
unsigned ret = sym->st_value + found->base;
115
pthread_mutex_unlock(&dl_lock);
119
set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL);
122
set_dlerror(DL_ERR_SYMBOL_NOT_FOUND);
125
pthread_mutex_unlock(&dl_lock);
129
int android_dladdr(const void *addr, Dl_info *info)
133
pthread_mutex_lock(&dl_lock);
135
/* Determine if this address can be found in any library currently mapped */
136
soinfo *si = find_containing_library(addr);
139
memset(info, 0, sizeof(Dl_info));
141
info->dli_fname = si->name;
142
info->dli_fbase = (void*)si->base;
144
/* Determine if any symbol in the library contains the specified address */
145
Elf32_Sym *sym = find_containing_symbol(addr, si);
148
info->dli_sname = si->strtab + sym->st_name;
149
info->dli_saddr = (void*)(si->base + sym->st_value);
155
pthread_mutex_unlock(&dl_lock);
160
int android_dlclose(void *handle)
162
pthread_mutex_lock(&dl_lock);
163
(void)unload_library((soinfo*)handle);
164
pthread_mutex_unlock(&dl_lock);
168
#if defined(ANDROID_ARM_LINKER)
169
// 0000000 00011111 111112 22222222 2333333 333344444444445555555
170
// 0123456 78901234 567890 12345678 9012345 678901234567890123456
171
#define ANDROID_LIBDL_STRTAB \
172
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
174
_Unwind_Ptr android_dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
176
#elif defined(ANDROID_X86_LINKER)
177
// 0000000 00011111 111112 22222222 2333333 3333444444444455
178
// 0123456 78901234 567890 12345678 9012345 6789012345678901
179
#define ANDROID_LIBDL_STRTAB \
180
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
182
int android_dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),void *data);
184
#elif defined(ANDROID_SH_LINKER)
185
// 0000000 00011111 111112 22222222 2333333 3333444444444455
186
// 0123456 78901234 567890 12345678 9012345 6789012345678901
187
#define ANDROID_LIBDL_STRTAB \
188
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
190
#else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
191
#error Unsupported architecture. Only ARM and x86 are presently supported.
195
static Elf32_Sym libdl_symtab[] = {
196
// total length of libdl_info.strtab, including trailing 0
197
// This is actually the the STH_UNDEF entry. Technically, it's
198
// supposed to have st_name == 0, but instead, it points to an index
199
// in the strtab with a \0 to make iterating through the symtab easier.
200
{ st_name: sizeof(ANDROID_LIBDL_STRTAB) - 1,
202
{ st_name: 0, // starting index of the name in libdl_info.strtab
203
st_value: (Elf32_Addr) &android_dlopen,
204
st_info: STB_GLOBAL << 4,
208
st_value: (Elf32_Addr) &android_dlclose,
209
st_info: STB_GLOBAL << 4,
213
st_value: (Elf32_Addr) &android_dlsym,
214
st_info: STB_GLOBAL << 4,
218
st_value: (Elf32_Addr) &android_dlerror,
219
st_info: STB_GLOBAL << 4,
223
st_value: (Elf32_Addr) &android_dladdr,
224
st_info: STB_GLOBAL << 4,
227
#ifdef ANDROID_ARM_LINKER
229
st_value: (Elf32_Addr) &android_dl_unwind_find_exidx,
230
st_info: STB_GLOBAL << 4,
233
#elif defined(ANDROID_X86_LINKER)
235
st_value: (Elf32_Addr) &android_dl_iterate_phdr,
236
st_info: STB_GLOBAL << 4,
239
#elif defined(ANDROID_SH_LINKER)
241
st_value: (Elf32_Addr) &android_dl_iterate_phdr,
242
st_info: STB_GLOBAL << 4,
248
/* Fake out a hash table with a single bucket.
249
* A search of the hash table will look through
250
* libdl_symtab starting with index [1], then
251
* use libdl_chains to find the next index to
252
* look at. libdl_chains should be set up to
253
* walk through every element in libdl_symtab,
254
* and then end with 0 (sentinel value).
256
* I.e., libdl_chains should look like
257
* { 0, 2, 3, ... N, 0 } where N is the number
258
* of actual symbols, or nelems(libdl_symtab)-1
259
* (since the first element of libdl_symtab is not
262
* (see _elf_lookup())
264
* Note that adding any new symbols here requires
265
* stubbing them out in libdl.
267
static unsigned libdl_buckets[1] = { 1 };
268
static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 };
270
soinfo libdl_info = {
274
strtab: ANDROID_LIBDL_STRTAB,
275
symtab: libdl_symtab,
279
bucket: libdl_buckets,