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