Imported Upstream version 0.1.0+git20131207+e452e83
[deb_libhybris.git] / hybris / common / ics / dlfcn.c
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 #define _GNU_SOURCE
17 #include <dlfcn.h>
18 #include <pthread.h>
19 #include <time.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include "linker.h"
23 #include "linker_format.h"
24
25 /* This file hijacks the symbols stubbed out in libdl.so. */
26
27 #define DL_SUCCESS 0
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
33
34 static char dl_err_buf[1024];
35 static const char *dl_err_str;
36
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",
43 };
44
45 #define likely(expr) __builtin_expect (expr, 1)
46 #define unlikely(expr) __builtin_expect (expr, 0)
47
48 static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
49
50 static void set_dlerror(int err)
51 {
52 format_buffer(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err],
53 linker_get_error());
54 dl_err_str = (const char *)&dl_err_buf[0];
55 };
56
57 void *android_dlopen(const char *filename, int flag)
58 {
59 soinfo *ret;
60
61 pthread_mutex_lock(&dl_lock);
62 ret = find_library(filename);
63 if (unlikely(ret == NULL)) {
64 set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
65 } else {
66 ret->refcount++;
67 }
68 pthread_mutex_unlock(&dl_lock);
69 return ret;
70 }
71
72 const char *android_dlerror(void)
73 {
74 const char *tmp = dl_err_str;
75 dl_err_str = NULL;
76 return (const char *)tmp;
77 }
78
79 void *android_dlsym(void *handle, const char *symbol)
80 {
81 soinfo *found;
82 Elf32_Sym *sym;
83 unsigned bind;
84
85 pthread_mutex_lock(&dl_lock);
86
87 if(unlikely(handle == 0)) {
88 set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE);
89 goto err;
90 }
91 if(unlikely(symbol == 0)) {
92 set_dlerror(DL_ERR_BAD_SYMBOL_NAME);
93 goto err;
94 }
95
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);
101
102 sym = NULL;
103 if(si && si->next) {
104 sym = lookup(symbol, &found, si->next);
105 }
106 } else {
107 found = (soinfo*)handle;
108 sym = lookup_in_library(found, symbol);
109 }
110
111 if(likely(sym != 0)) {
112 bind = ELF32_ST_BIND(sym->st_info);
113
114 if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
115 unsigned ret = sym->st_value + found->base;
116 pthread_mutex_unlock(&dl_lock);
117 return (void*)ret;
118 }
119
120 set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL);
121 }
122 else
123 set_dlerror(DL_ERR_SYMBOL_NOT_FOUND);
124
125 err:
126 pthread_mutex_unlock(&dl_lock);
127 return 0;
128 }
129
130 int android_dladdr(const void *addr, Dl_info *info)
131 {
132 int ret = 0;
133
134 pthread_mutex_lock(&dl_lock);
135
136 /* Determine if this address can be found in any library currently mapped */
137 soinfo *si = find_containing_library(addr);
138
139 if(si) {
140 memset(info, 0, sizeof(Dl_info));
141
142 info->dli_fname = si->name;
143 info->dli_fbase = (void*)si->base;
144
145 /* Determine if any symbol in the library contains the specified address */
146 Elf32_Sym *sym = find_containing_symbol(addr, si);
147
148 if(sym != NULL) {
149 info->dli_sname = si->strtab + sym->st_name;
150 info->dli_saddr = (void*)(si->base + sym->st_value);
151 }
152
153 ret = 1;
154 }
155
156 pthread_mutex_unlock(&dl_lock);
157
158 return ret;
159 }
160
161 int android_dlclose(void *handle)
162 {
163 pthread_mutex_lock(&dl_lock);
164 (void)unload_library((soinfo*)handle);
165 pthread_mutex_unlock(&dl_lock);
166 return 0;
167 }
168
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"
174
175 _Unwind_Ptr android_dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
176
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"
182
183 int android_dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),void *data);
184
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"
190
191 #else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
192 #error Unsupported architecture. Only ARM and x86 are presently supported.
193 #endif
194
195
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,
202 },
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,
206 st_shndx: 1,
207 },
208 { st_name: 7,
209 st_value: (Elf32_Addr) &android_dlclose,
210 st_info: STB_GLOBAL << 4,
211 st_shndx: 1,
212 },
213 { st_name: 15,
214 st_value: (Elf32_Addr) &android_dlsym,
215 st_info: STB_GLOBAL << 4,
216 st_shndx: 1,
217 },
218 { st_name: 21,
219 st_value: (Elf32_Addr) &android_dlerror,
220 st_info: STB_GLOBAL << 4,
221 st_shndx: 1,
222 },
223 { st_name: 29,
224 st_value: (Elf32_Addr) &android_dladdr,
225 st_info: STB_GLOBAL << 4,
226 st_shndx: 1,
227 },
228 #ifdef ANDROID_ARM_LINKER
229 { st_name: 36,
230 st_value: (Elf32_Addr) &android_dl_unwind_find_exidx,
231 st_info: STB_GLOBAL << 4,
232 st_shndx: 1,
233 },
234 #elif defined(ANDROID_X86_LINKER)
235 { st_name: 36,
236 st_value: (Elf32_Addr) &android_dl_iterate_phdr,
237 st_info: STB_GLOBAL << 4,
238 st_shndx: 1,
239 },
240 #elif defined(ANDROID_SH_LINKER)
241 { st_name: 36,
242 st_value: (Elf32_Addr) &android_dl_iterate_phdr,
243 st_info: STB_GLOBAL << 4,
244 st_shndx: 1,
245 },
246 #endif
247 };
248
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).
256 *
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
261 * a real symbol).
262 *
263 * (see _elf_lookup())
264 *
265 * Note that adding any new symbols here requires
266 * stubbing them out in libdl.
267 */
268 static unsigned libdl_buckets[1] = { 1 };
269 static unsigned libdl_chains[7] = { 0, 2, 3, 4, 5, 6, 0 };
270
271 soinfo libdl_info = {
272 name: "libdl.so",
273 flags: FLAG_LINKED,
274
275 strtab: ANDROID_LIBDL_STRTAB,
276 symtab: libdl_symtab,
277
278 nbucket: 1,
279 nchain: 7,
280 bucket: libdl_buckets,
281 chain: libdl_chains,
282 };
283