dlfcn.cpp revision 5ae44f302b7d1d19f25c4c6f125e32dc369961d9
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
24#include <bionic/pthread_internal.h>
25#include <private/bionic_tls.h>
26#include <private/ScopedPthreadMutexLocker.h>
27#include <private/ThreadLocalBuffer.h>
28
29/* This file hijacks the symbols stubbed out in libdl.so. */
30
31static pthread_mutex_t gDlMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
32
33static 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;
40}
41
42static 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) {
59  ScopedPthreadMutexLocker locker(&gDlMutex);
60  soinfo* result = find_library(filename);
61  if (result == NULL) {
62    __bionic_format_dlerror("dlopen failed", linker_get_error());
63    return NULL;
64  }
65  soinfo_call_constructors(result);
66  result->refcount++;
67  return result;
68}
69
70void* dlsym(void* handle, const char* symbol) {
71  ScopedPthreadMutexLocker locker(&gDlMutex);
72
73  if (handle == NULL) {
74    __bionic_format_dlerror("dlsym library handle is null", NULL);
75    return NULL;
76  }
77  if (symbol == NULL) {
78    __bionic_format_dlerror("dlsym symbol name is null", NULL);
79    return NULL;
80  }
81
82  soinfo* found = NULL;
83  Elf32_Sym* sym = NULL;
84  if (handle == RTLD_DEFAULT) {
85    sym = lookup(symbol, &found, NULL);
86  } else if (handle == RTLD_NEXT) {
87    void* ret_addr = __builtin_return_address(0);
88    soinfo* si = find_containing_library(ret_addr);
89
90    sym = NULL;
91    if (si && si->next) {
92      sym = lookup(symbol, &found, si->next);
93    }
94  } else {
95    found = (soinfo*) handle;
96    sym = soinfo_lookup(found, symbol);
97  }
98
99  if (sym != NULL) {
100    unsigned bind = ELF32_ST_BIND(sym->st_info);
101
102    if (bind == STB_GLOBAL && sym->st_shndx != 0) {
103      unsigned ret = sym->st_value + found->load_bias;
104      return (void*) ret;
105    }
106
107    __bionic_format_dlerror("symbol found but not global", symbol);
108    return NULL;
109  } else {
110    __bionic_format_dlerror("undefined symbol", symbol);
111    return NULL;
112  }
113}
114
115int dladdr(const void* addr, Dl_info* info) {
116  ScopedPthreadMutexLocker locker(&gDlMutex);
117
118  // Determine if this address can be found in any library currently mapped.
119  soinfo* si = find_containing_library(addr);
120  if (si == NULL) {
121    return 0;
122  }
123
124  memset(info, 0, sizeof(Dl_info));
125
126  info->dli_fname = si->name;
127  // Address at which the shared object is loaded.
128  info->dli_fbase = (void*) si->base;
129
130  // Determine if any symbol in the library contains the specified address.
131  Elf32_Sym *sym = soinfo_find_symbol(si, addr);
132  if (sym != NULL) {
133    info->dli_sname = si->strtab + sym->st_name;
134    info->dli_saddr = (void*)(si->load_bias + sym->st_value);
135  }
136
137  return 1;
138}
139
140int dlclose(void* handle) {
141  ScopedPthreadMutexLocker locker(&gDlMutex);
142  return soinfo_unload((soinfo*) handle);
143}
144
145#if defined(ANDROID_ARM_LINKER)
146//                     0000000 00011111 111112 22222222 2333333 333344444444445555555
147//                     0123456 78901234 567890 12345678 9012345 678901234567890123456
148#define ANDROID_LIBDL_STRTAB \
149                      "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_unwind_find_exidx\0"
150
151#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
152//                     0000000 00011111 111112 22222222 2333333 3333444444444455
153//                     0123456 78901234 567890 12345678 9012345 6789012345678901
154#define ANDROID_LIBDL_STRTAB \
155                      "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0dl_iterate_phdr\0"
156#else
157#error Unsupported architecture. Only ARM, MIPS, and x86 are presently supported.
158#endif
159
160// name_offset: starting index of the name in libdl_info.strtab
161#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
162    { name_offset, \
163      reinterpret_cast<Elf32_Addr>(reinterpret_cast<void*>(value)), \
164      /* st_size */ 0, \
165      (shndx == 0) ? 0 : (STB_GLOBAL << 4), \
166      /* st_other */ 0, \
167      shndx }
168
169static Elf32_Sym gLibDlSymtab[] = {
170  // Total length of libdl_info.strtab, including trailing 0.
171  // This is actually the STH_UNDEF entry. Technically, it's
172  // supposed to have st_name == 0, but instead, it points to an index
173  // in the strtab with a \0 to make iterating through the symtab easier.
174  ELF32_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0),
175  ELF32_SYM_INITIALIZER( 0, &dlopen, 1),
176  ELF32_SYM_INITIALIZER( 7, &dlclose, 1),
177  ELF32_SYM_INITIALIZER(15, &dlsym, 1),
178  ELF32_SYM_INITIALIZER(21, &dlerror, 1),
179  ELF32_SYM_INITIALIZER(29, &dladdr, 1),
180#if defined(ANDROID_ARM_LINKER)
181  ELF32_SYM_INITIALIZER(36, &dl_unwind_find_exidx, 1),
182#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
183  ELF32_SYM_INITIALIZER(36, &dl_iterate_phdr, 1),
184#endif
185};
186
187// Fake out a hash table with a single bucket.
188// A search of the hash table will look through
189// gLibDlSymtab starting with index [1], then
190// use gLibDlChains to find the next index to
191// look at.  gLibDlChains should be set up to
192// walk through every element in gLibDlSymtab,
193// and then end with 0 (sentinel value).
194//
195// That is, gLibDlChains should look like
196// { 0, 2, 3, ... N, 0 } where N is the number
197// of actual symbols, or nelems(gLibDlSymtab)-1
198// (since the first element of gLibDlSymtab is not
199// a real symbol).
200//
201// (see soinfo_elf_lookup())
202//
203// Note that adding any new symbols here requires
204// stubbing them out in libdl.
205static unsigned gLibDlBuckets[1] = { 1 };
206static unsigned gLibDlChains[7] = { 0, 2, 3, 4, 5, 6, 0 };
207
208// This is used by the dynamic linker. Every process gets these symbols for free.
209soinfo libdl_info = {
210    "libdl.so",
211
212    phdr: 0, phnum: 0,
213    entry: 0, base: 0, size: 0,
214    unused: 0, dynamic: 0, unused2: 0, unused3: 0,
215    next: 0,
216
217    flags: FLAG_LINKED,
218
219    strtab: ANDROID_LIBDL_STRTAB,
220    symtab: gLibDlSymtab,
221
222    nbucket: 1,
223    nchain: 7,
224    bucket: gLibDlBuckets,
225    chain: gLibDlChains,
226
227    plt_got: 0, plt_rel: 0, plt_rel_count: 0, rel: 0, rel_count: 0,
228    preinit_array: 0, preinit_array_count: 0, init_array: 0, init_array_count: 0,
229    fini_array: 0, fini_array_count: 0, init_func: 0, fini_func: 0,
230
231#if defined(ANDROID_ARM_LINKER)
232    ARM_exidx: 0, ARM_exidx_count: 0,
233#elif defined(ANDROID_MIPS_LINKER)
234    mips_symtabno: 0, mips_local_gotno: 0, mips_gotsym: 0,
235#endif
236
237    refcount: 0,
238    { l_addr: 0, l_name: 0, l_ld: 0, l_next: 0, l_prev: 0, },
239    constructors_called: 0, load_bias: 0,
240    has_text_relocations: false,
241    has_DT_SYMBOLIC: true,
242};
243