1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crazy_linker_wrappers.h"
10 #include "crazy_linker_debug.h"
11 #include "crazy_linker_globals.h"
12 #include "crazy_linker_library_list.h"
13 #include "crazy_linker_library_view.h"
14 #include "crazy_linker_shared_library.h"
15 #include "crazy_linker_thread.h"
16 #include "crazy_linker_util.h"
19 // On ARM, this function is exported by the dynamic linker but never
20 // declared in any official header. It is used at runtime to
21 // find the base address of the .ARM.exidx section for the
22 // shared library containing the instruction at |pc|, as well as
23 // the number of 8-byte entries in that section, written into |*pcount|
24 extern "C" _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int*);
26 // On other architectures, this function is exported by the dynamic linker
27 // but never declared in any official header. It is used at runtime to
28 // iterate over all loaded libraries and call the |cb|. When the function
29 // returns non-0, the iteration returns and the function returns its
31 extern "C" int dl_iterate_phdr(int (*cb)(dl_phdr_info* info,
42 extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
44 // On ARM, this function is defined as a weak symbol by libc.so.
45 // Unfortunately its address cannot be found through dlsym(), which will
46 // always return NULL. To work-around this, define a copy here that does
47 // exactly the same thing. The ARM EABI mandates the function's behaviour.
48 // __cxa_atexit() is implemented by the C library, but not declared by
49 // any official header. It's part of the low-level C++ support runtime.
50 int __aeabi_atexit(void* object, void (*destructor)(void*), void* dso_handle) {
51 return __cxa_atexit(destructor, object, dso_handle);
55 // Used to save the system dlerror() into our thread-specific data.
56 void SaveSystemError() {
57 ThreadData* data = GetThreadData();
58 data->SetError(::dlerror());
62 ThreadData* data = GetThreadData();
63 const char* error = data->GetError();
64 data->SwapErrorBuffers();
65 // dlerror() returns a 'char*', but no sane client code should ever
66 // try to write to this location.
67 return const_cast<char*>(error);
70 void* WrapDlopen(const char* path, int mode) {
71 ScopedGlobalLock lock;
73 // NOTE: If |path| is NULL, the wrapper should return a handle
74 // corresponding to the current executable. This can't be a crazy
75 // library, so don't try to handle it with the crazy linker.
77 LibraryList* lib_list = Globals::GetLibraries();
79 LibraryView* wrap = lib_list->LoadLibrary(path,
81 0U /* load_address */,
83 Globals::GetSearchPaths(),
90 // Try to load the executable with the system dlopen() instead.
92 void* system_lib = ::dlopen(path, mode);
93 if (system_lib == NULL) {
98 LibraryView* wrap_lib = new LibraryView();
99 wrap_lib->SetSystem(system_lib, path ? path : "<executable>");
100 Globals::GetLibraries()->AddLibrary(wrap_lib);
104 void* WrapDlsym(void* lib_handle, const char* symbol_name) {
105 LibraryView* wrap_lib = reinterpret_cast<LibraryView*>(lib_handle);
108 SetLinkerError("dlsym: NULL symbol name");
113 SetLinkerError("dlsym: NULL library handle");
117 // TODO(digit): Handle RTLD_DEFAULT / RTLD_NEXT
118 if (lib_handle == RTLD_DEFAULT || lib_handle == RTLD_NEXT) {
119 SetLinkerError("dlsym: RTLD_DEFAULT/RTLD_NEXT are not implemented!");
123 // NOTE: The Android dlsym() only looks inside the target library,
124 // while the GNU one will perform a breadth-first search into its
127 // This implementation performs a correct breadth-first search
128 // when |lib_handle| corresponds to a crazy library, except that
129 // it stops at system libraries that it depends on.
133 if (wrap_lib->IsSystem()) {
134 // Note: the system dlsym() only looks into the target library,
135 // while the GNU linker performs a breadth-first search.
136 result = ::dlsym(wrap_lib->GetSystem(), symbol_name);
139 LOG("dlsym:%s: could not find symbol '%s' from system library\n%s",
142 GetThreadData()->GetError());
147 if (wrap_lib->IsCrazy()) {
148 ScopedGlobalLock lock;
149 LibraryList* lib_list = Globals::GetLibraries();
150 void* addr = lib_list->FindSymbolFrom(symbol_name, wrap_lib);
154 SetLinkerError("dlsym: Could not find '%s' from library '%s'",
156 wrap_lib->GetName());
160 SetLinkerError("dlsym: Invalid library handle %p looking for '%s'",
166 int WrapDladdr(void* address, Dl_info* info) {
167 // First, perform search in crazy libraries.
169 ScopedGlobalLock lock;
170 LibraryList* lib_list = Globals::GetLibraries();
171 LibraryView* wrap = lib_list->FindLibraryForAddress(address);
172 if (wrap && wrap->IsCrazy()) {
175 SharedLibrary* lib = wrap->GetCrazy();
176 ::memset(info, 0, sizeof(*info));
177 info->dli_fname = lib->base_name();
178 info->dli_fbase = reinterpret_cast<void*>(lib->load_address());
180 // Determine if any symbol in the library contains the specified address.
181 (void)lib->FindNearestSymbolForAddress(
182 address, &info->dli_sname, &info->dli_saddr, &sym_size);
186 // Otherwise, use system version.
188 int ret = ::dladdr(address, info);
194 int WrapDlclose(void* lib_handle) {
195 LibraryView* wrap_lib = reinterpret_cast<LibraryView*>(lib_handle);
197 SetLinkerError("NULL library handle");
201 if (wrap_lib->IsSystem() || wrap_lib->IsCrazy()) {
202 ScopedGlobalLock lock;
203 LibraryList* lib_list = Globals::GetLibraries();
204 lib_list->UnloadLibrary(wrap_lib);
208 // Invalid library handle!!
209 SetLinkerError("Invalid library handle %p", lib_handle);
214 _Unwind_Ptr WrapDl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
215 // First lookup in crazy libraries.
217 ScopedGlobalLock lock;
218 LibraryList* list = Globals::GetLibraries();
219 _Unwind_Ptr result = list->FindArmExIdx(pc, pcount);
223 // Lookup in system libraries.
224 return ::dl_unwind_find_exidx(pc, pcount);
227 int WrapDl_iterate_phdr(int (*cb)(dl_phdr_info*, size_t, void*), void* data) {
228 // First, iterate over crazy libraries.
230 ScopedGlobalLock lock;
231 LibraryList* list = Globals::GetLibraries();
232 int result = list->IteratePhdr(cb, data);
236 // Then lookup through system ones.
237 return ::dl_iterate_phdr(cb, data);
243 void* WrapLinkerSymbol(const char* name) {
244 // Shortcut, since all names begin with 'dl'
245 // Take care of __aeabi_atexit on ARM though.
246 if (name[0] != 'd' || name[1] != 'l') {
248 if (name[0] == '_' && !strcmp("__aeabi_atexit", name))
249 return reinterpret_cast<void*>(&__aeabi_atexit);
254 static const struct {
258 {"dlopen", reinterpret_cast<void*>(&WrapDlopen)},
259 {"dlclose", reinterpret_cast<void*>(&WrapDlclose)},
260 {"dlerror", reinterpret_cast<void*>(&WrapDlerror)},
261 {"dlsym", reinterpret_cast<void*>(&WrapDlsym)},
262 {"dladdr", reinterpret_cast<void*>(&WrapDladdr)},
264 {"dl_unwind_find_exidx",
265 reinterpret_cast<void*>(&WrapDl_unwind_find_exidx)},
267 {"dl_iterate_phdr", reinterpret_cast<void*>(&WrapDl_iterate_phdr)},
270 static const size_t kCount = sizeof(kSymbols) / sizeof(kSymbols[0]);
271 for (size_t n = 0; n < kCount; ++n) {
272 if (!strcmp(kSymbols[n].name, name))
273 return kSymbols[n].address;