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.
23 #include "linker_format.h"
25 /* This file hijacks the symbols stubbed out in libdl.so. */
28 #define DL_ERR_CANNOT_LOAD_LIBRARY 1
29 #define DL_ERR_INVALID_LIBRARY_HANDLE 2
30 #define DL_ERR_BAD_SYMBOL_NAME 3
31 #define DL_ERR_SYMBOL_NOT_FOUND 4
32 #define DL_ERR_SYMBOL_NOT_GLOBAL 5
34 static char dl_err_buf
[1024];
35 static const char *dl_err_str
;
37 static const char *dl_errors
[] = {
38 [DL_ERR_CANNOT_LOAD_LIBRARY
] = "Cannot load library",
39 [DL_ERR_INVALID_LIBRARY_HANDLE
] = "Invalid library handle",
40 [DL_ERR_BAD_SYMBOL_NAME
] = "Invalid symbol name",
41 [DL_ERR_SYMBOL_NOT_FOUND
] = "Symbol not found",
42 [DL_ERR_SYMBOL_NOT_GLOBAL
] = "Symbol is not global",
45 #define likely(expr) __builtin_expect (expr, 1)
46 #define unlikely(expr) __builtin_expect (expr, 0)
48 static pthread_mutex_t dl_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 static void set_dlerror(int err
)
52 format_buffer(dl_err_buf
, sizeof(dl_err_buf
), "%s: %s", dl_errors
[err
],
54 dl_err_str
= (const char *)&dl_err_buf
[0];
57 void *android_dlopen(const char *filename
, int flag
)
61 pthread_mutex_lock(&dl_lock
);
62 ret
= find_library(filename
);
63 if (unlikely(ret
== NULL
)) {
64 set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY
);
68 pthread_mutex_unlock(&dl_lock
);
72 const char *android_dlerror(void)
74 const char *tmp
= dl_err_str
;
76 return (const char *)tmp
;
79 void *android_dlsym(void *handle
, const char *symbol
)
85 pthread_mutex_lock(&dl_lock
);
87 if(unlikely(handle
== 0)) {
88 set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE
);
91 if(unlikely(symbol
== 0)) {
92 set_dlerror(DL_ERR_BAD_SYMBOL_NAME
);
96 if(handle
== RTLD_DEFAULT
) {
97 sym
= lookup(symbol
, &found
, NULL
);
98 } else if(handle
== RTLD_NEXT
) {
99 void *ret_addr
= __builtin_return_address(0);
100 soinfo
*si
= find_containing_library(ret_addr
);
104 sym
= lookup(symbol
, &found
, si
->next
);
107 found
= (soinfo
*)handle
;
108 sym
= lookup_in_library(found
, symbol
);
111 if(likely(sym
!= 0)) {
112 bind
= ELF32_ST_BIND(sym
->st_info
);
114 if(likely((bind
== STB_GLOBAL
) && (sym
->st_shndx
!= 0))) {
115 unsigned ret
= sym
->st_value
+ found
->base
;
116 pthread_mutex_unlock(&dl_lock
);
120 set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL
);
123 set_dlerror(DL_ERR_SYMBOL_NOT_FOUND
);
126 pthread_mutex_unlock(&dl_lock
);
130 int android_dladdr(const void *addr
, Dl_info
*info
)
134 pthread_mutex_lock(&dl_lock
);
136 /* Determine if this address can be found in any library currently mapped */
137 soinfo
*si
= find_containing_library(addr
);
140 memset(info
, 0, sizeof(Dl_info
));
142 info
->dli_fname
= si
->name
;
143 info
->dli_fbase
= (void*)si
->base
;
145 /* Determine if any symbol in the library contains the specified address */
146 Elf32_Sym
*sym
= find_containing_symbol(addr
, si
);
149 info
->dli_sname
= si
->strtab
+ sym
->st_name
;
150 info
->dli_saddr
= (void*)(si
->base
+ sym
->st_value
);
156 pthread_mutex_unlock(&dl_lock
);
161 int android_dlclose(void *handle
)
163 pthread_mutex_lock(&dl_lock
);
164 (void)unload_library((soinfo
*)handle
);
165 pthread_mutex_unlock(&dl_lock
);
169 #if defined(ANDROID_ARM_LINKER)
170 // 0000000 00011111 111112 22222222 2333333 333344444444445555555
171 // 0123456 78901234 567890 12345678 9012345 678901234567890123456
172 #define ANDROID_LIBDL_STRTAB \
173 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
175 _Unwind_Ptr
android_dl_unwind_find_exidx(_Unwind_Ptr pc
, int *pcount
);
177 #elif defined(ANDROID_X86_LINKER)
178 // 0000000 00011111 111112 22222222 2333333 3333444444444455
179 // 0123456 78901234 567890 12345678 9012345 6789012345678901
180 #define ANDROID_LIBDL_STRTAB \
181 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
183 int android_dl_iterate_phdr(int (*cb
)(struct dl_phdr_info
*info
, size_t size
, void *data
),void *data
);
185 #elif defined(ANDROID_SH_LINKER)
186 // 0000000 00011111 111112 22222222 2333333 3333444444444455
187 // 0123456 78901234 567890 12345678 9012345 6789012345678901
188 #define ANDROID_LIBDL_STRTAB \
189 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
191 #else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
192 #error Unsupported architecture. Only ARM and x86 are presently supported.
196 static Elf32_Sym libdl_symtab
[] = {
197 // total length of libdl_info.strtab, including trailing 0
198 // This is actually the the STH_UNDEF entry. Technically, it's
199 // supposed to have st_name == 0, but instead, it points to an index
200 // in the strtab with a \0 to make iterating through the symtab easier.
201 { st_name
: sizeof(ANDROID_LIBDL_STRTAB
) - 1,
203 { st_name
: 0, // starting index of the name in libdl_info.strtab
204 st_value
: (Elf32_Addr
) &android_dlopen
,
205 st_info
: STB_GLOBAL
<< 4,
209 st_value
: (Elf32_Addr
) &android_dlclose
,
210 st_info
: STB_GLOBAL
<< 4,
214 st_value
: (Elf32_Addr
) &android_dlsym
,
215 st_info
: STB_GLOBAL
<< 4,
219 st_value
: (Elf32_Addr
) &android_dlerror
,
220 st_info
: STB_GLOBAL
<< 4,
224 st_value
: (Elf32_Addr
) &android_dladdr
,
225 st_info
: STB_GLOBAL
<< 4,
228 #ifdef ANDROID_ARM_LINKER
230 st_value
: (Elf32_Addr
) &android_dl_unwind_find_exidx
,
231 st_info
: STB_GLOBAL
<< 4,
234 #elif defined(ANDROID_X86_LINKER)
236 st_value
: (Elf32_Addr
) &android_dl_iterate_phdr
,
237 st_info
: STB_GLOBAL
<< 4,
240 #elif defined(ANDROID_SH_LINKER)
242 st_value
: (Elf32_Addr
) &android_dl_iterate_phdr
,
243 st_info
: STB_GLOBAL
<< 4,
249 /* Fake out a hash table with a single bucket.
250 * A search of the hash table will look through
251 * libdl_symtab starting with index [1], then
252 * use libdl_chains to find the next index to
253 * look at. libdl_chains should be set up to
254 * walk through every element in libdl_symtab,
255 * and then end with 0 (sentinel value).
257 * I.e., libdl_chains should look like
258 * { 0, 2, 3, ... N, 0 } where N is the number
259 * of actual symbols, or nelems(libdl_symtab)-1
260 * (since the first element of libdl_symtab is not
263 * (see _elf_lookup())
265 * Note that adding any new symbols here requires
266 * stubbing them out in libdl.
268 static unsigned libdl_buckets
[1] = { 1 };
269 static unsigned libdl_chains
[7] = { 0, 2, 3, 4, 5, 6, 0 };
271 soinfo libdl_info
= {
275 strtab
: ANDROID_LIBDL_STRTAB
,
276 symtab
: libdl_symtab
,
280 bucket
: libdl_buckets
,