blob: 24006e2be1db42e75bc5aafe2e1af01db9b96d88 [file] [log] [blame]
Elliott Hughes3b297c42012-10-11 16:08:51 -07001/*
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
Elliott Hughes5419b942012-10-16 15:54:46 -070017#include "linker.h"
18
Elliott Hughes3b297c42012-10-11 16:08:51 -070019#include <dlfcn.h>
20#include <pthread.h>
21#include <stdio.h>
Elliott Hughes5419b942012-10-16 15:54:46 -070022#include <stdlib.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070023
Elliott Hughes5419b942012-10-16 15:54:46 -070024#include <bionic/pthread_internal.h>
25#include <private/bionic_tls.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070026#include <private/ScopedPthreadMutexLocker.h>
Elliott Hughes5419b942012-10-16 15:54:46 -070027#include <private/ThreadLocalBuffer.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070028
29/* This file hijacks the symbols stubbed out in libdl.so. */
30
Elliott Hughes22d62922012-10-12 10:50:21 -070031static pthread_mutex_t gDlMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
Elliott Hughes3b297c42012-10-11 16:08:51 -070032
Elliott Hughes5419b942012-10-16 15:54:46 -070033static const char* __bionic_set_dlerror(char* new_value) {
34 void* tls = const_cast<void*>(__get_tls());
35 char** dlerror_slot = &reinterpret_cast<char**>(tls)[TLS_SLOT_DLERROR];
36
37 const char* old_value = *dlerror_slot;
38 *dlerror_slot = new_value;
39 return old_value;
Elliott Hughes3b297c42012-10-11 16:08:51 -070040}
41
Elliott Hughes5419b942012-10-16 15:54:46 -070042static void __bionic_format_dlerror(const char* msg, const char* detail) {
43 char* buffer = __get_thread()->dlerror_buffer;
44 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE);
45 if (detail != NULL) {
46 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE);
47 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE);
48 }
49
50 __bionic_set_dlerror(buffer);
51}
52
53const char* dlerror() {
54 const char* old_value = __bionic_set_dlerror(NULL);
55 return old_value;
56}
57
58void* dlopen(const char* filename, int flag) {
Elliott Hughes22d62922012-10-12 10:50:21 -070059 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughesd23736e2012-11-01 15:16:56 -070060 soinfo* result = do_dlopen(filename);
Elliott Hughes3b297c42012-10-11 16:08:51 -070061 if (result == NULL) {
Elliott Hughes5419b942012-10-16 15:54:46 -070062 __bionic_format_dlerror("dlopen failed", linker_get_error());
Elliott Hughes3b297c42012-10-11 16:08:51 -070063 return NULL;
64 }
Elliott Hughes3b297c42012-10-11 16:08:51 -070065 return result;
66}
67
Elliott Hughes3b297c42012-10-11 16:08:51 -070068void* dlsym(void* handle, const char* symbol) {
Elliott Hughes22d62922012-10-12 10:50:21 -070069 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughes3b297c42012-10-11 16:08:51 -070070
Elliott Hughes5419b942012-10-16 15:54:46 -070071 if (handle == NULL) {
72 __bionic_format_dlerror("dlsym library handle is null", NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070073 return NULL;
74 }
Elliott Hughes5419b942012-10-16 15:54:46 -070075 if (symbol == NULL) {
76 __bionic_format_dlerror("dlsym symbol name is null", NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070077 return NULL;
78 }
79
80 soinfo* found = NULL;
81 Elf32_Sym* sym = NULL;
82 if (handle == RTLD_DEFAULT) {
83 sym = lookup(symbol, &found, NULL);
84 } else if (handle == RTLD_NEXT) {
85 void* ret_addr = __builtin_return_address(0);
86 soinfo* si = find_containing_library(ret_addr);
87
88 sym = NULL;
89 if (si && si->next) {
90 sym = lookup(symbol, &found, si->next);
91 }
92 } else {
93 found = (soinfo*) handle;
94 sym = soinfo_lookup(found, symbol);
95 }
96
Elliott Hughes5419b942012-10-16 15:54:46 -070097 if (sym != NULL) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070098 unsigned bind = ELF32_ST_BIND(sym->st_info);
99
Elliott Hughes5419b942012-10-16 15:54:46 -0700100 if (bind == STB_GLOBAL && sym->st_shndx != 0) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700101 unsigned ret = sym->st_value + found->load_bias;
102 return (void*) ret;
103 }
104
Elliott Hughes5419b942012-10-16 15:54:46 -0700105 __bionic_format_dlerror("symbol found but not global", symbol);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700106 return NULL;
107 } else {
Elliott Hughes5419b942012-10-16 15:54:46 -0700108 __bionic_format_dlerror("undefined symbol", symbol);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700109 return NULL;
110 }
111}
112
113int dladdr(const void* addr, Dl_info* info) {
Elliott Hughes22d62922012-10-12 10:50:21 -0700114 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700115
116 // Determine if this address can be found in any library currently mapped.
117 soinfo* si = find_containing_library(addr);
118 if (si == NULL) {
119 return 0;
120 }
121
122 memset(info, 0, sizeof(Dl_info));
123
124 info->dli_fname = si->name;
125 // Address at which the shared object is loaded.
126 info->dli_fbase = (void*) si->base;
127
128 // Determine if any symbol in the library contains the specified address.
129 Elf32_Sym *sym = soinfo_find_symbol(si, addr);
130 if (sym != NULL) {
131 info->dli_sname = si->strtab + sym->st_name;
132 info->dli_saddr = (void*)(si->load_bias + sym->st_value);
133 }
134
135 return 1;
136}
137
138int dlclose(void* handle) {
Elliott Hughes22d62922012-10-12 10:50:21 -0700139 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700140 return do_dlclose(reinterpret_cast<soinfo*>(handle));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700141}
142
143#if defined(ANDROID_ARM_LINKER)
144// 0000000 00011111 111112 22222222 2333333 333344444444445555555
145// 0123456 78901234 567890 12345678 9012345 678901234567890123456
146#define ANDROID_LIBDL_STRTAB \
147 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
148
149#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
150// 0000000 00011111 111112 22222222 2333333 3333444444444455
151// 0123456 78901234 567890 12345678 9012345 6789012345678901
152#define ANDROID_LIBDL_STRTAB \
153 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
154#else
155#error Unsupported architecture. Only ARM, MIPS, and x86 are presently supported.
156#endif
157
158// name_offset: starting index of the name in libdl_info.strtab
159#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
160 { name_offset, \
161 reinterpret_cast<Elf32_Addr>(reinterpret_cast<void*>(value)), \
162 /* st_size */ 0, \
163 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
164 /* st_other */ 0, \
165 shndx }
166
Elliott Hughes5419b942012-10-16 15:54:46 -0700167static Elf32_Sym gLibDlSymtab[] = {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700168 // Total length of libdl_info.strtab, including trailing 0.
169 // This is actually the STH_UNDEF entry. Technically, it's
170 // supposed to have st_name == 0, but instead, it points to an index
171 // in the strtab with a \0 to make iterating through the symtab easier.
172 ELF32_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0),
173 ELF32_SYM_INITIALIZER( 0, &dlopen, 1),
174 ELF32_SYM_INITIALIZER( 7, &dlclose, 1),
175 ELF32_SYM_INITIALIZER(15, &dlsym, 1),
176 ELF32_SYM_INITIALIZER(21, &dlerror, 1),
177 ELF32_SYM_INITIALIZER(29, &dladdr, 1),
178#if defined(ANDROID_ARM_LINKER)
179 ELF32_SYM_INITIALIZER(36, &dl_unwind_find_exidx, 1),
180#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
181 ELF32_SYM_INITIALIZER(36, &dl_iterate_phdr, 1),
182#endif
183};
184
Elliott Hughes22d62922012-10-12 10:50:21 -0700185// Fake out a hash table with a single bucket.
186// A search of the hash table will look through
Elliott Hughes5419b942012-10-16 15:54:46 -0700187// gLibDlSymtab starting with index [1], then
188// use gLibDlChains to find the next index to
189// look at. gLibDlChains should be set up to
190// walk through every element in gLibDlSymtab,
Elliott Hughes22d62922012-10-12 10:50:21 -0700191// and then end with 0 (sentinel value).
192//
Elliott Hughes5419b942012-10-16 15:54:46 -0700193// That is, gLibDlChains should look like
Elliott Hughes22d62922012-10-12 10:50:21 -0700194// { 0, 2, 3, ... N, 0 } where N is the number
Elliott Hughes5419b942012-10-16 15:54:46 -0700195// of actual symbols, or nelems(gLibDlSymtab)-1
196// (since the first element of gLibDlSymtab is not
Elliott Hughes22d62922012-10-12 10:50:21 -0700197// a real symbol).
198//
199// (see soinfo_elf_lookup())
200//
201// Note that adding any new symbols here requires
202// stubbing them out in libdl.
Elliott Hughes5419b942012-10-16 15:54:46 -0700203static unsigned gLibDlBuckets[1] = { 1 };
204static unsigned gLibDlChains[7] = { 0, 2, 3, 4, 5, 6, 0 };
Elliott Hughes3b297c42012-10-11 16:08:51 -0700205
Elliott Hughes22d62922012-10-12 10:50:21 -0700206// This is used by the dynamic linker. Every process gets these symbols for free.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700207soinfo libdl_info = {
Pavel Chupin20aa6c02012-10-25 12:17:05 +0400208 "libdl.so",
Elliott Hughes3b297c42012-10-11 16:08:51 -0700209
210 phdr: 0, phnum: 0,
211 entry: 0, base: 0, size: 0,
212 unused: 0, dynamic: 0, unused2: 0, unused3: 0,
213 next: 0,
214
215 flags: FLAG_LINKED,
216
217 strtab: ANDROID_LIBDL_STRTAB,
Elliott Hughes5419b942012-10-16 15:54:46 -0700218 symtab: gLibDlSymtab,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700219
220 nbucket: 1,
221 nchain: 7,
Elliott Hughes5419b942012-10-16 15:54:46 -0700222 bucket: gLibDlBuckets,
223 chain: gLibDlChains,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700224
225 plt_got: 0, plt_rel: 0, plt_rel_count: 0, rel: 0, rel_count: 0,
226 preinit_array: 0, preinit_array_count: 0, init_array: 0, init_array_count: 0,
227 fini_array: 0, fini_array_count: 0, init_func: 0, fini_func: 0,
228
229#if defined(ANDROID_ARM_LINKER)
230 ARM_exidx: 0, ARM_exidx_count: 0,
231#elif defined(ANDROID_MIPS_LINKER)
232 mips_symtabno: 0, mips_local_gotno: 0, mips_gotsym: 0,
233#endif
234
235 refcount: 0,
236 { l_addr: 0, l_name: 0, l_ld: 0, l_next: 0, l_prev: 0, },
Elliott Hughesd23736e2012-11-01 15:16:56 -0700237 constructors_called: false,
238 load_bias: 0,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200239 has_text_relocations: false,
240 has_DT_SYMBOLIC: true,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700241};