blob: a61cfa8f679a0674a94dddb37deb6ab8cb515a97 [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
Elliott Hughescade4c32012-12-20 14:42:14 -080058void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
59 ScopedPthreadMutexLocker locker(&gDlMutex);
60 do_android_update_LD_LIBRARY_PATH(ld_library_path);
61}
62
Elliott Hughese66190d2012-12-18 15:57:55 -080063void* dlopen(const char* filename, int flags) {
Elliott Hughes22d62922012-10-12 10:50:21 -070064 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughese66190d2012-12-18 15:57:55 -080065 soinfo* result = do_dlopen(filename, flags);
Elliott Hughes3b297c42012-10-11 16:08:51 -070066 if (result == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -080067 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
Elliott Hughes3b297c42012-10-11 16:08:51 -070068 return NULL;
69 }
Elliott Hughes3b297c42012-10-11 16:08:51 -070070 return result;
71}
72
Elliott Hughes3b297c42012-10-11 16:08:51 -070073void* dlsym(void* handle, const char* symbol) {
Elliott Hughes22d62922012-10-12 10:50:21 -070074 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughes3b297c42012-10-11 16:08:51 -070075
Elliott Hughes5419b942012-10-16 15:54:46 -070076 if (handle == NULL) {
77 __bionic_format_dlerror("dlsym library handle is null", NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070078 return NULL;
79 }
Elliott Hughes5419b942012-10-16 15:54:46 -070080 if (symbol == NULL) {
81 __bionic_format_dlerror("dlsym symbol name is null", NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070082 return NULL;
83 }
84
85 soinfo* found = NULL;
Elliott Hughesc6200592013-09-30 18:43:46 -070086 Elf_Sym* sym = NULL;
Elliott Hughes3b297c42012-10-11 16:08:51 -070087 if (handle == RTLD_DEFAULT) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080088 sym = dlsym_linear_lookup(symbol, &found, NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070089 } else if (handle == RTLD_NEXT) {
90 void* ret_addr = __builtin_return_address(0);
91 soinfo* si = find_containing_library(ret_addr);
92
93 sym = NULL;
94 if (si && si->next) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080095 sym = dlsym_linear_lookup(symbol, &found, si->next);
Elliott Hughes3b297c42012-10-11 16:08:51 -070096 }
97 } else {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080098 found = reinterpret_cast<soinfo*>(handle);
99 sym = dlsym_handle_lookup(found, symbol);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700100 }
101
Elliott Hughes5419b942012-10-16 15:54:46 -0700102 if (sym != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700103 unsigned bind = ELF_ST_BIND(sym->st_info);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700104
Elliott Hughes5419b942012-10-16 15:54:46 -0700105 if (bind == STB_GLOBAL && sym->st_shndx != 0) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700106 return reinterpret_cast<void*>(sym->st_value + found->load_bias);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700107 }
108
Elliott Hughes5419b942012-10-16 15:54:46 -0700109 __bionic_format_dlerror("symbol found but not global", symbol);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700110 return NULL;
111 } else {
Elliott Hughes5419b942012-10-16 15:54:46 -0700112 __bionic_format_dlerror("undefined symbol", symbol);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700113 return NULL;
114 }
115}
116
117int dladdr(const void* addr, Dl_info* info) {
Elliott Hughes22d62922012-10-12 10:50:21 -0700118 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700119
120 // Determine if this address can be found in any library currently mapped.
121 soinfo* si = find_containing_library(addr);
122 if (si == NULL) {
123 return 0;
124 }
125
126 memset(info, 0, sizeof(Dl_info));
127
128 info->dli_fname = si->name;
129 // Address at which the shared object is loaded.
130 info->dli_fbase = (void*) si->base;
131
132 // Determine if any symbol in the library contains the specified address.
Elliott Hughesc6200592013-09-30 18:43:46 -0700133 Elf_Sym *sym = dladdr_find_symbol(si, addr);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700134 if (sym != NULL) {
135 info->dli_sname = si->strtab + sym->st_name;
136 info->dli_saddr = (void*)(si->load_bias + sym->st_value);
137 }
138
139 return 1;
140}
141
142int dlclose(void* handle) {
Elliott Hughes22d62922012-10-12 10:50:21 -0700143 ScopedPthreadMutexLocker locker(&gDlMutex);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700144 return do_dlclose(reinterpret_cast<soinfo*>(handle));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700145}
146
147#if defined(ANDROID_ARM_LINKER)
Christopher Ferris24053a42013-08-19 17:45:09 -0700148// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888 8888888
149// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012 3456789
Elliott Hughes3b297c42012-10-11 16:08:51 -0700150#define ANDROID_LIBDL_STRTAB \
Christopher Ferris24053a42013-08-19 17:45:09 -0700151 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_iterate_phdr\0dl_unwind_find_exidx\0"
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700152#elif defined(ANDROID_MIPS_LINKER) || defined(ANDROID_X86_LINKER) || defined(ANDROID_X86_64_LINKER)
Elliott Hughescade4c32012-12-20 14:42:14 -0800153// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667
154// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890
Elliott Hughes3b297c42012-10-11 16:08:51 -0700155#define ANDROID_LIBDL_STRTAB \
Elliott Hughescade4c32012-12-20 14:42:14 -0800156 "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_iterate_phdr\0"
Elliott Hughes3b297c42012-10-11 16:08:51 -0700157#else
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700158#error Unsupported architecture. Only ARM, MIPS, x86, and x86_64 are presently supported.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700159#endif
160
161// name_offset: starting index of the name in libdl_info.strtab
162#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
163 { name_offset, \
164 reinterpret_cast<Elf32_Addr>(reinterpret_cast<void*>(value)), \
165 /* st_size */ 0, \
166 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
167 /* st_other */ 0, \
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700168 shndx, \
169 }
Elliott Hughes3b297c42012-10-11 16:08:51 -0700170
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700171#define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \
172 { name_offset, \
173 (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
174 /* st_other */ 0, \
175 shndx, \
176 reinterpret_cast<Elf64_Addr>(reinterpret_cast<void*>(value)), \
177 /* st_size */ 0, \
178 }
179
180#if defined(__LP64__)
181# define ELF_SYM_INITIALIZER ELF64_SYM_INITIALIZER
182#else
183# define ELF_SYM_INITIALIZER ELF32_SYM_INITIALIZER
184#endif
185
186static Elf_Sym gLibDlSymtab[] = {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700187 // Total length of libdl_info.strtab, including trailing 0.
188 // This is actually the STH_UNDEF entry. Technically, it's
189 // supposed to have st_name == 0, but instead, it points to an index
190 // in the strtab with a \0 to make iterating through the symtab easier.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700191 ELF_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0),
192 ELF_SYM_INITIALIZER( 0, &dlopen, 1),
193 ELF_SYM_INITIALIZER( 7, &dlclose, 1),
194 ELF_SYM_INITIALIZER(15, &dlsym, 1),
195 ELF_SYM_INITIALIZER(21, &dlerror, 1),
196 ELF_SYM_INITIALIZER(29, &dladdr, 1),
197 ELF_SYM_INITIALIZER(36, &android_update_LD_LIBRARY_PATH, 1),
198 ELF_SYM_INITIALIZER(67, &dl_iterate_phdr, 1),
Christopher Ferris24053a42013-08-19 17:45:09 -0700199#if defined(ANDROID_ARM_LINKER)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700200 ELF_SYM_INITIALIZER(83, &dl_unwind_find_exidx, 1),
Elliott Hughes3b297c42012-10-11 16:08:51 -0700201#endif
202};
203
Elliott Hughes22d62922012-10-12 10:50:21 -0700204// Fake out a hash table with a single bucket.
205// A search of the hash table will look through
Elliott Hughes5419b942012-10-16 15:54:46 -0700206// gLibDlSymtab starting with index [1], then
207// use gLibDlChains to find the next index to
208// look at. gLibDlChains should be set up to
209// walk through every element in gLibDlSymtab,
Elliott Hughes22d62922012-10-12 10:50:21 -0700210// and then end with 0 (sentinel value).
211//
Elliott Hughes5419b942012-10-16 15:54:46 -0700212// That is, gLibDlChains should look like
Elliott Hughes22d62922012-10-12 10:50:21 -0700213// { 0, 2, 3, ... N, 0 } where N is the number
Elliott Hughes5419b942012-10-16 15:54:46 -0700214// of actual symbols, or nelems(gLibDlSymtab)-1
215// (since the first element of gLibDlSymtab is not
Elliott Hughes22d62922012-10-12 10:50:21 -0700216// a real symbol).
217//
218// (see soinfo_elf_lookup())
219//
220// Note that adding any new symbols here requires
221// stubbing them out in libdl.
Elliott Hughes5419b942012-10-16 15:54:46 -0700222static unsigned gLibDlBuckets[1] = { 1 };
Christopher Ferris24053a42013-08-19 17:45:09 -0700223#if defined(ANDROID_ARM_LINKER)
224static unsigned gLibDlChains[9] = { 0, 2, 3, 4, 5, 6, 7, 8, 0 };
225#else
Elliott Hughescade4c32012-12-20 14:42:14 -0800226static unsigned gLibDlChains[8] = { 0, 2, 3, 4, 5, 6, 7, 0 };
Christopher Ferris24053a42013-08-19 17:45:09 -0700227#endif
Elliott Hughes3b297c42012-10-11 16:08:51 -0700228
Elliott Hughes22d62922012-10-12 10:50:21 -0700229// This is used by the dynamic linker. Every process gets these symbols for free.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700230soinfo libdl_info = {
Pavel Chupin20aa6c02012-10-25 12:17:05 +0400231 "libdl.so",
Elliott Hughes3b297c42012-10-11 16:08:51 -0700232
Elliott Hughesafac15d2013-10-08 09:39:52 -0700233 .phdr = 0,
234 .phnum = 0,
235 .entry = 0,
236 .base = 0,
237 .size = 0,
238
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700239#if !defined(__LP64__)
Elliott Hughesafac15d2013-10-08 09:39:52 -0700240 .unused1 = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700241#endif
Elliott Hughesafac15d2013-10-08 09:39:52 -0700242
243 .dynamic = 0,
244
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700245#if !defined(__LP64__)
Elliott Hughesafac15d2013-10-08 09:39:52 -0700246 .unused2 = 0, .unused3 = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700247#endif
Elliott Hughes3b297c42012-10-11 16:08:51 -0700248
Elliott Hughesafac15d2013-10-08 09:39:52 -0700249 .next = 0,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700250
Elliott Hughesafac15d2013-10-08 09:39:52 -0700251 .flags = FLAG_LINKED,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700252
Elliott Hughesafac15d2013-10-08 09:39:52 -0700253 .strtab = ANDROID_LIBDL_STRTAB,
254 .symtab = gLibDlSymtab,
255
256 .nbucket = sizeof(gLibDlBuckets)/sizeof(unsigned),
257 .nchain = sizeof(gLibDlChains)/sizeof(unsigned),
258 .bucket = gLibDlBuckets,
259 .chain = gLibDlChains,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700260
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700261#if defined(ANDROID_X86_64_LINKER)
Elliott Hughesafac15d2013-10-08 09:39:52 -0700262 .plt_rela = 0,
263 .plt_rela_count = 0,
264 .rela = 0,
265 .rela_count = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700266#else
Elliott Hughesafac15d2013-10-08 09:39:52 -0700267 .plt_got = 0,
268 .plt_rel = 0,
269 .plt_rel_count = 0,
270 .rel = 0,
271 .rel_count = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700272#endif
273
Elliott Hughesafac15d2013-10-08 09:39:52 -0700274 .preinit_array = 0,
275 .preinit_array_count = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700276
Elliott Hughesafac15d2013-10-08 09:39:52 -0700277 .init_array = 0,
278 .init_array_count = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700279
Elliott Hughesafac15d2013-10-08 09:39:52 -0700280 .fini_array = 0,
281 .fini_array_count = 0,
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700282
Elliott Hughesafac15d2013-10-08 09:39:52 -0700283 .init_func = 0,
284 .fini_func = 0,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700285
286#if defined(ANDROID_ARM_LINKER)
Elliott Hughesafac15d2013-10-08 09:39:52 -0700287 .ARM_exidx = 0,
288 .ARM_exidx_count = 0,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700289#elif defined(ANDROID_MIPS_LINKER)
Elliott Hughesafac15d2013-10-08 09:39:52 -0700290 .mips_symtabno = 0,
291 .mips_local_gotno = 0,
292 .mips_gotsym = 0,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700293#endif
294
Elliott Hughesafac15d2013-10-08 09:39:52 -0700295 .ref_count = 0,
296 { .l_addr = 0, .l_name = 0, .l_ld = 0, .l_next = 0, .l_prev = 0, },
297 .constructors_called = false,
298 .load_bias = 0,
299 .has_text_relocations = false,
300 .has_DT_SYMBOLIC = true,
Elliott Hughes3b297c42012-10-11 16:08:51 -0700301};