1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 // Contains wrapper to missing dladdr in API < 8
30 
31 #if !__LP64__
32 
33 #include <dlfcn.h>
34 #include <android/api-level.h>
35 
36 #if __ANDROID_API__ < 8
37 typedef int Dl_info;
38 #endif // __ANDROID_API__ >= 8
39 
40 typedef int (*dladdr_func_t)(const void *addr, Dl_info *info);
41 
42 #if defined(__cplusplus)
43 extern "C"
44 #endif
my_dladdr(const void * addr,Dl_info * info)45 int my_dladdr(const void *addr, Dl_info *info)
46 {
47     static int initialized = 0;
48     static dladdr_func_t p_dladdr = 0;
49     if (!p_dladdr && !initialized) {
50         void *libdl = dlopen("libdl.so", RTLD_NOW);
51       // Other thread may enter here simultaneously but p_dladdr should be
52       // set to the same addres for "dladdr"
53         if (libdl) {
54 #if __ANDROID_API__ < 8
55 	   p_dladdr = 0;
56 #else
57 	   p_dladdr = (dladdr_func_t)dlsym(libdl, "dladdr");
58 #endif
59         }
60         initialized = 1;
61     }
62     return p_dladdr? p_dladdr(addr, info) : 0;
63 }
64 
65 
66 #ifdef TEST
67 #include <stdio.h>
main()68 int main()
69 {
70     void *libdl = dlopen("libc.so", RTLD_NOW);
71     void *h = dlsym(libdl, "printf");
72     Dl_info info;
73     int r = my_dladdr((char*)h+1, &info);
74     if (r)
75         printf("%p: %s, %x, %s, %p\n", h, info.dli_fname, info.dli_fbase, info.dli_sname, info.dli_saddr);
76 }
77 
78 #endif // TEST
79 #endif // __LP64__
80