dlfcn.cpp revision d974e8804689058714dc4fe9adcb57ee9a6996a8
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
17#include "linker.h"
18
19#include <dlfcn.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <android/dlext.h>
25
26#include <bionic/pthread_internal.h>
27#include "private/bionic_tls.h"
28#include "private/ScopedPthreadMutexLocker.h"
29#include "private/ThreadLocalBuffer.h"
30
31/* This file hijacks the symbols stubbed out in libdl.so. */
32
33static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
34
35static const char* __bionic_set_dlerror(char* new_value) {
36  char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR];
37
38  const char* old_value = *dlerror_slot;
39  *dlerror_slot = new_value;
40  return old_value;
41}
42
43static void __bionic_format_dlerror(const char* msg, const char* detail) {
44  char* buffer = __get_thread()->dlerror_buffer;
45  strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE);
46  if (detail != nullptr) {
47    strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE);
48    strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE);
49  }
50
51  __bionic_set_dlerror(buffer);
52}
53
54const char* dlerror() {
55  const char* old_value = __bionic_set_dlerror(nullptr);
56  return old_value;
57}
58
59void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
60  ScopedPthreadMutexLocker locker(&g_dl_mutex);
61  do_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
62}
63
64void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
65  ScopedPthreadMutexLocker locker(&g_dl_mutex);
66  do_android_update_LD_LIBRARY_PATH(ld_library_path);
67}
68
69static void* dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) {
70  ScopedPthreadMutexLocker locker(&g_dl_mutex);
71  soinfo* result = do_dlopen(filename, flags, extinfo);
72  if (result == nullptr) {
73    __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
74    return nullptr;
75  }
76  return result;
77}
78
79void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) {
80  return dlopen_ext(filename, flags, extinfo);
81}
82
83void* dlopen(const char* filename, int flags) {
84  return dlopen_ext(filename, flags, nullptr);
85}
86
87void* dlsym(void* handle, const char* symbol) {
88  ScopedPthreadMutexLocker locker(&g_dl_mutex);
89
90#if !defined(__LP64__)
91  if (handle == nullptr) {
92    __bionic_format_dlerror("dlsym library handle is null", nullptr);
93    return nullptr;
94  }
95#endif
96
97  if (symbol == nullptr) {
98    __bionic_format_dlerror("dlsym symbol name is null", nullptr);
99    return nullptr;
100  }
101
102  soinfo* found = nullptr;
103  const ElfW(Sym)* sym = nullptr;
104  void* caller_addr = __builtin_return_address(0);
105  soinfo* caller = find_containing_library(caller_addr);
106
107  if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
108    sym = dlsym_linear_lookup(symbol, &found, caller, handle);
109  } else {
110    sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, symbol);
111  }
112
113  if (sym != nullptr) {
114    unsigned bind = ELF_ST_BIND(sym->st_info);
115
116    if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
117      return reinterpret_cast<void*>(found->resolve_symbol_address(sym));
118    }
119
120    __bionic_format_dlerror("symbol found but not global", symbol);
121    return nullptr;
122  } else {
123    __bionic_format_dlerror("undefined symbol", symbol);
124    return nullptr;
125  }
126}
127
128int dladdr(const void* addr, Dl_info* info) {
129  ScopedPthreadMutexLocker locker(&g_dl_mutex);
130
131  // Determine if this address can be found in any library currently mapped.
132  soinfo* si = find_containing_library(addr);
133  if (si == nullptr) {
134    return 0;
135  }
136
137  memset(info, 0, sizeof(Dl_info));
138
139  info->dli_fname = si->get_realpath();
140  // Address at which the shared object is loaded.
141  info->dli_fbase = reinterpret_cast<void*>(si->base);
142
143  // Determine if any symbol in the library contains the specified address.
144  ElfW(Sym)* sym = si->find_symbol_by_address(addr);
145  if (sym != nullptr) {
146    info->dli_sname = si->get_string(sym->st_name);
147    info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
148  }
149
150  return 1;
151}
152
153int dlclose(void* handle) {
154  ScopedPthreadMutexLocker locker(&g_dl_mutex);
155  do_dlclose(reinterpret_cast<soinfo*>(handle));
156  // dlclose has no defined errors.
157  return 0;
158}
159
160void android_set_application_target_sdk_version(uint32_t target) {
161  // lock to avoid modification in the middle of dlopen.
162  ScopedPthreadMutexLocker locker(&g_dl_mutex);
163  set_application_target_sdk_version(target);
164}
165
166uint32_t android_get_application_target_sdk_version() {
167  return get_application_target_sdk_version();
168}
169
170// name_offset: starting index of the name in libdl_info.strtab
171#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
172    { name_offset, \
173      reinterpret_cast<Elf32_Addr>(value), \
174      /* st_size */ 0, \
175      (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
176      /* st_other */ 0, \
177      shndx, \
178    }
179
180#define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \
181    { name_offset, \
182      (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
183      /* st_other */ 0, \
184      shndx, \
185      reinterpret_cast<Elf64_Addr>(value), \
186      /* st_size */ 0, \
187    }
188
189static const char ANDROID_LIBDL_STRTAB[] =
190  // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 99999
191  // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 56789
192    "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_it"
193  // 00000000001 1111111112222222222 3333333333444444444455555555556666666666777 777777788888888889999999999
194  // 01234567890 1234567890123456789 0123456789012345678901234567890123456789012 345678901234567890123456789
195    "erate_phdr\0android_dlopen_ext\0android_set_application_target_sdk_version\0android_get_application_tar"
196  // 0000000000111111
197  // 0123456789012345
198    "get_sdk_version\0"
199#if defined(__arm__)
200  // 216
201    "dl_unwind_find_exidx\0"
202#endif
203    ;
204
205static ElfW(Sym) g_libdl_symtab[] = {
206  // Total length of libdl_info.strtab, including trailing 0.
207  // This is actually the STH_UNDEF entry. Technically, it's
208  // supposed to have st_name == 0, but instead, it points to an index
209  // in the strtab with a \0 to make iterating through the symtab easier.
210  ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0),
211  ELFW(SYM_INITIALIZER)(  0, &dlopen, 1),
212  ELFW(SYM_INITIALIZER)(  7, &dlclose, 1),
213  ELFW(SYM_INITIALIZER)( 15, &dlsym, 1),
214  ELFW(SYM_INITIALIZER)( 21, &dlerror, 1),
215  ELFW(SYM_INITIALIZER)( 29, &dladdr, 1),
216  ELFW(SYM_INITIALIZER)( 36, &android_update_LD_LIBRARY_PATH, 1),
217  ELFW(SYM_INITIALIZER)( 67, &android_get_LD_LIBRARY_PATH, 1),
218  ELFW(SYM_INITIALIZER)( 95, &dl_iterate_phdr, 1),
219  ELFW(SYM_INITIALIZER)(111, &android_dlopen_ext, 1),
220  ELFW(SYM_INITIALIZER)(130, &android_set_application_target_sdk_version, 1),
221  ELFW(SYM_INITIALIZER)(173, &android_get_application_target_sdk_version, 1),
222#if defined(__arm__)
223  ELFW(SYM_INITIALIZER)(216, &dl_unwind_find_exidx, 1),
224#endif
225};
226
227// Fake out a hash table with a single bucket.
228//
229// A search of the hash table will look through g_libdl_symtab starting with index 1, then
230// use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to
231// walk through every element in g_libdl_symtab, and then end with 0 (sentinel value).
232//
233// That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number
234// of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not
235// a real symbol). (See soinfo_elf_lookup().)
236//
237// Note that adding any new symbols here requires stubbing them out in libdl.
238static unsigned g_libdl_buckets[1] = { 1 };
239#if defined(__arm__)
240static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0 };
241#else
242static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0 };
243#endif
244
245static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
246static soinfo* __libdl_info = nullptr;
247
248// This is used by the dynamic linker. Every process gets these symbols for free.
249soinfo* get_libdl_info() {
250  if (__libdl_info == nullptr) {
251    __libdl_info = new (__libdl_info_buf) soinfo("libdl.so", nullptr, 0, RTLD_GLOBAL);
252    __libdl_info->flags_ |= FLAG_LINKED;
253    __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB;
254    __libdl_info->symtab_ = g_libdl_symtab;
255    __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned);
256    __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned);
257    __libdl_info->bucket_ = g_libdl_buckets;
258    __libdl_info->chain_ = g_libdl_chains;
259    __libdl_info->ref_count_ = 1;
260    __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB);
261    __libdl_info->local_group_root_ = __libdl_info;
262    __libdl_info->soname_ = "libdl.so";
263#if defined(__arm__)
264    strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_));
265#endif
266  }
267
268  return __libdl_info;
269}
270