Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / android_crazy_linker / src / src / crazy_linker_wrappers.cpp
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.
4
5 #include "crazy_linker_wrappers.h"
6
7 #include <dlfcn.h>
8 #include <link.h>
9
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"
17
18 #ifdef __arm__
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*);
25 #else
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
30 // value.
31 extern "C" int dl_iterate_phdr(int (*cb)(dl_phdr_info* info,
32                                          size_t size,
33                                          void* data),
34                                void* data);
35 #endif
36
37 namespace crazy {
38
39 namespace {
40
41 #ifdef __arm__
42 extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
43
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);
52 }
53 #endif
54
55 // Used to save the system dlerror() into our thread-specific data.
56 void SaveSystemError() {
57   ThreadData* data = GetThreadData();
58   data->SetError(::dlerror());
59 }
60
61 char* WrapDlerror() {
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);
68 }
69
70 void* WrapDlopen(const char* path, int mode) {
71   ScopedGlobalLock lock;
72
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.
76   if (path) {
77     LibraryList* lib_list = Globals::GetLibraries();
78     Error error;
79     LibraryView* wrap = lib_list->LoadLibrary(path,
80                                               mode,
81                                               0U /* load_address */,
82                                               0U /* file_offset */,
83                                               Globals::GetSearchPaths(),
84                                               false,
85                                               &error);
86     if (wrap)
87       return wrap;
88   }
89
90   // Try to load the executable with the system dlopen() instead.
91   ::dlerror();
92   void* system_lib = ::dlopen(path, mode);
93   if (system_lib == NULL) {
94     SaveSystemError();
95     return NULL;
96   }
97
98   LibraryView* wrap_lib = new LibraryView();
99   wrap_lib->SetSystem(system_lib, path ? path : "<executable>");
100   Globals::GetLibraries()->AddLibrary(wrap_lib);
101   return wrap_lib;
102 }
103
104 void* WrapDlsym(void* lib_handle, const char* symbol_name) {
105   LibraryView* wrap_lib = reinterpret_cast<LibraryView*>(lib_handle);
106
107   if (!symbol_name) {
108     SetLinkerError("dlsym: NULL symbol name");
109     return NULL;
110   }
111
112   if (!lib_handle) {
113     SetLinkerError("dlsym: NULL library handle");
114     return NULL;
115   }
116
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!");
120     return NULL;
121   }
122
123   // NOTE: The Android dlsym() only looks inside the target library,
124   // while the GNU one will perform a breadth-first search into its
125   // dependency tree.
126
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.
130
131   void* result = NULL;
132
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);
137     if (!result) {
138       SaveSystemError();
139       LOG("dlsym:%s: could not find symbol '%s' from system library\n%s",
140           wrap_lib->GetName(),
141           symbol_name,
142           GetThreadData()->GetError());
143     }
144     return result;
145   }
146
147   if (wrap_lib->IsCrazy()) {
148     ScopedGlobalLock lock;
149     LibraryList* lib_list = Globals::GetLibraries();
150     void* addr = lib_list->FindSymbolFrom(symbol_name, wrap_lib);
151     if (addr)
152       return addr;
153
154     SetLinkerError("dlsym: Could not find '%s' from library '%s'",
155                    symbol_name,
156                    wrap_lib->GetName());
157     return NULL;
158   }
159
160   SetLinkerError("dlsym: Invalid library handle %p looking for '%s'",
161                  lib_handle,
162                  symbol_name);
163   return NULL;
164 }
165
166 int WrapDladdr(void* address, Dl_info* info) {
167   // First, perform search in crazy libraries.
168   {
169     ScopedGlobalLock lock;
170     LibraryList* lib_list = Globals::GetLibraries();
171     LibraryView* wrap = lib_list->FindLibraryForAddress(address);
172     if (wrap && wrap->IsCrazy()) {
173       size_t sym_size = 0;
174
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());
179
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);
183       return 0;
184     }
185   }
186   // Otherwise, use system version.
187   ::dlerror();
188   int ret = ::dladdr(address, info);
189   if (ret != 0)
190     SaveSystemError();
191   return ret;
192 }
193
194 int WrapDlclose(void* lib_handle) {
195   LibraryView* wrap_lib = reinterpret_cast<LibraryView*>(lib_handle);
196   if (!wrap_lib) {
197     SetLinkerError("NULL library handle");
198     return -1;
199   }
200
201   if (wrap_lib->IsSystem() || wrap_lib->IsCrazy()) {
202     ScopedGlobalLock lock;
203     LibraryList* lib_list = Globals::GetLibraries();
204     lib_list->UnloadLibrary(wrap_lib);
205     return 0;
206   }
207
208   // Invalid library handle!!
209   SetLinkerError("Invalid library handle %p", lib_handle);
210   return -1;
211 }
212
213 #ifdef __arm__
214 _Unwind_Ptr WrapDl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
215   // First lookup in crazy libraries.
216   {
217     ScopedGlobalLock lock;
218     LibraryList* list = Globals::GetLibraries();
219     _Unwind_Ptr result = list->FindArmExIdx(pc, pcount);
220     if (result)
221       return result;
222   }
223   // Lookup in system libraries.
224   return ::dl_unwind_find_exidx(pc, pcount);
225 }
226 #else  // !__arm__
227 int WrapDl_iterate_phdr(int (*cb)(dl_phdr_info*, size_t, void*), void* data) {
228   // First, iterate over crazy libraries.
229   {
230     ScopedGlobalLock lock;
231     LibraryList* list = Globals::GetLibraries();
232     int result = list->IteratePhdr(cb, data);
233     if (result)
234       return result;
235   }
236   // Then lookup through system ones.
237   return ::dl_iterate_phdr(cb, data);
238 }
239 #endif  // !__arm__
240
241 }  // namespace
242
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') {
247 #ifdef __arm__
248     if (name[0] == '_' && !strcmp("__aeabi_atexit", name))
249       return reinterpret_cast<void*>(&__aeabi_atexit);
250 #endif
251     return NULL;
252   }
253
254   static const struct {
255     const char* name;
256     void* address;
257   } kSymbols[] = {
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)},
263 #ifdef __arm__
264         {"dl_unwind_find_exidx",
265          reinterpret_cast<void*>(&WrapDl_unwind_find_exidx)},
266 #else
267         {"dl_iterate_phdr", reinterpret_cast<void*>(&WrapDl_iterate_phdr)},
268 #endif
269     };
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;
274   }
275   return NULL;
276 }
277
278 }  // namespace crazy