/src/glib/gmodule/gmodule-dl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GMODULE - GLIB wrapper code for dynamic module loading |
2 | | * Copyright (C) 1998, 2000 Tim Janik |
3 | | * |
4 | | * This library is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * This library is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | /* |
19 | | * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
20 | | * file for a list of people on the GLib Team. See the ChangeLog |
21 | | * files for a list of changes. These files are distributed with |
22 | | * GLib at ftp://ftp.gtk.org/pub/gtk/. |
23 | | */ |
24 | | |
25 | | /* |
26 | | * MT safe |
27 | | */ |
28 | | #include "config.h" |
29 | | |
30 | | #include <dlfcn.h> |
31 | | |
32 | | /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systems? */ |
33 | | |
34 | | |
35 | | /* dlerror() is not implemented on all systems |
36 | | */ |
37 | | #ifndef G_MODULE_HAVE_DLERROR |
38 | | # ifdef __NetBSD__ |
39 | | # define dlerror() g_strerror (errno) |
40 | | # else /* !__NetBSD__ */ |
41 | | /* could we rely on errno's state here? */ |
42 | | # define dlerror() "unknown dl-error" |
43 | | # endif /* !__NetBSD__ */ |
44 | | #endif /* G_MODULE_HAVE_DLERROR */ |
45 | | |
46 | | /* some flags are missing on some systems, so we provide |
47 | | * harmless defaults. |
48 | | * The Perl sources say, RTLD_LAZY needs to be defined as (1), |
49 | | * at least for Solaris 1. |
50 | | * |
51 | | * Mandatory: |
52 | | * RTLD_LAZY - resolve undefined symbols as code from the dynamic library |
53 | | * is executed. |
54 | | * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail |
55 | | * if this cannot be done. |
56 | | * Optionally: |
57 | | * RTLD_GLOBAL - the external symbols defined in the library will be made |
58 | | * available to subsequently loaded libraries. |
59 | | */ |
60 | | #ifndef HAVE_RTLD_LAZY |
61 | | #define RTLD_LAZY 1 |
62 | | #endif /* RTLD_LAZY */ |
63 | | #ifndef HAVE_RTLD_NOW |
64 | | #define RTLD_NOW 0 |
65 | | #endif /* RTLD_NOW */ |
66 | | /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */ |
67 | | #ifdef G_MODULE_BROKEN_RTLD_GLOBAL |
68 | | #undef RTLD_GLOBAL |
69 | | #undef HAVE_RTLD_GLOBAL |
70 | | #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */ |
71 | | #ifndef HAVE_RTLD_GLOBAL |
72 | | #define RTLD_GLOBAL 0 |
73 | | #endif /* RTLD_GLOBAL */ |
74 | | |
75 | | |
76 | | /* --- functions --- */ |
77 | | static gchar* |
78 | | fetch_dlerror (gboolean replace_null) |
79 | 0 | { |
80 | 0 | gchar *msg = dlerror (); |
81 | | |
82 | | /* make sure we always return an error message != NULL, if |
83 | | * expected to do so. */ |
84 | |
|
85 | 0 | if (!msg && replace_null) |
86 | 0 | return "unknown dl-error"; |
87 | | |
88 | 0 | return msg; |
89 | 0 | } |
90 | | |
91 | | static gpointer |
92 | | _g_module_open (const gchar *file_name, |
93 | | gboolean bind_lazy, |
94 | | gboolean bind_local) |
95 | 0 | { |
96 | 0 | gpointer handle; |
97 | | |
98 | 0 | handle = dlopen (file_name, |
99 | 0 | (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW)); |
100 | 0 | if (!handle) |
101 | 0 | g_module_set_error (fetch_dlerror (TRUE)); |
102 | | |
103 | 0 | return handle; |
104 | 0 | } |
105 | | |
106 | | static gpointer |
107 | | _g_module_self (void) |
108 | 0 | { |
109 | 0 | gpointer handle; |
110 | | |
111 | | /* to query symbols from the program itself, special link options |
112 | | * are required on some systems. |
113 | | */ |
114 | | |
115 | | /* On Android 32 bit (i.e. not __LP64__), dlopen(NULL) |
116 | | * does not work reliable and generally no symbols are found |
117 | | * at all. RTLD_DEFAULT works though. |
118 | | * On Android 64 bit, dlopen(NULL) seems to work but dlsym(handle) |
119 | | * always returns 'undefined symbol'. Only if RTLD_DEFAULT or |
120 | | * NULL is given, dlsym returns an appropriate pointer. |
121 | | */ |
122 | | #if defined(__BIONIC__) |
123 | | handle = RTLD_DEFAULT; |
124 | | #else |
125 | 0 | handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY); |
126 | 0 | #endif |
127 | 0 | if (!handle) |
128 | 0 | g_module_set_error (fetch_dlerror (TRUE)); |
129 | | |
130 | 0 | return handle; |
131 | 0 | } |
132 | | |
133 | | static void |
134 | | _g_module_close (gpointer handle) |
135 | 0 | { |
136 | | #if defined(__BIONIC__) |
137 | | if (handle != RTLD_DEFAULT) |
138 | | #endif |
139 | 0 | { |
140 | 0 | if (dlclose (handle) != 0) |
141 | 0 | g_module_set_error (fetch_dlerror (TRUE)); |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | static gpointer |
146 | | _g_module_symbol (gpointer handle, |
147 | | const gchar *symbol_name) |
148 | 0 | { |
149 | 0 | gpointer p; |
150 | 0 | gchar *msg; |
151 | |
|
152 | 0 | fetch_dlerror (FALSE); |
153 | 0 | p = dlsym (handle, symbol_name); |
154 | 0 | msg = fetch_dlerror (FALSE); |
155 | 0 | if (msg) |
156 | 0 | g_module_set_error (msg); |
157 | | |
158 | 0 | return p; |
159 | 0 | } |
160 | | |
161 | | static gchar* |
162 | | _g_module_build_path (const gchar *directory, |
163 | | const gchar *module_name) |
164 | 0 | { |
165 | 0 | if (directory && *directory) { |
166 | 0 | if (strncmp (module_name, "lib", 3) == 0) |
167 | 0 | return g_strconcat (directory, "/", module_name, NULL); |
168 | 0 | else |
169 | 0 | return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL); |
170 | 0 | } else if (strncmp (module_name, "lib", 3) == 0) |
171 | 0 | return g_strdup (module_name); |
172 | 0 | else |
173 | 0 | return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL); |
174 | 0 | } |