00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <dlfcn.h>
00032
00033
00034
00035
00036
00037
00038 #ifndef G_MODULE_HAVE_DLERROR
00039 # ifdef __NetBSD__
00040 # define dlerror() g_strerror (errno)
00041 # else
00042
00043 # define dlerror() "unknown dl-error"
00044 # endif
00045 #endif
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #ifndef RTLD_LAZY
00062 #define RTLD_LAZY 1
00063 #endif
00064 #ifndef RTLD_NOW
00065 #define RTLD_NOW 0
00066 #endif
00067
00068 #ifdef G_MODULE_BROKEN_RTLD_GLOBAL
00069 #undef RTLD_GLOBAL
00070 #endif
00071 #ifndef RTLD_GLOBAL
00072 #define RTLD_GLOBAL 0
00073 #endif
00074
00075
00076
00077 static gchar*
00078 fetch_dlerror (void)
00079 {
00080 gchar *msg = dlerror ();
00081
00082
00083
00084 return msg ? msg : "unknown dl-error";
00085 }
00086
00087 static gpointer
00088 _g_module_open (const gchar *file_name,
00089 gboolean bind_lazy)
00090 {
00091 gpointer handle;
00092
00093 handle = dlopen (file_name, RTLD_GLOBAL | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
00094 if (!handle)
00095 g_module_set_error (fetch_dlerror ());
00096
00097 return handle;
00098 }
00099
00100 static gpointer
00101 _g_module_self (void)
00102 {
00103 gpointer handle;
00104
00105
00106
00107
00108
00109 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
00110 if (!handle)
00111 g_module_set_error (fetch_dlerror ());
00112
00113 return handle;
00114 }
00115
00116 static void
00117 _g_module_close (gpointer handle,
00118 gboolean is_unref)
00119 {
00120
00121
00122
00123 is_unref |= 1;
00124
00125 if (is_unref)
00126 {
00127 if (dlclose (handle) != 0)
00128 g_module_set_error (fetch_dlerror ());
00129 }
00130 }
00131
00132 static gpointer
00133 _g_module_symbol (gpointer handle,
00134 const gchar *symbol_name)
00135 {
00136 gpointer p;
00137
00138 p = dlsym (handle, symbol_name);
00139 if (!p)
00140 g_module_set_error (fetch_dlerror ());
00141
00142 return p;
00143 }
00144
00145 static gchar*
00146 _g_module_build_path (const gchar *directory,
00147 const gchar *module_name)
00148 {
00149 if (directory && *directory) {
00150 if (strncmp (module_name, "lib", 3) == 0)
00151 return g_strconcat (directory, "/", module_name, NULL);
00152 else
00153 return g_strconcat (directory, "/lib", module_name, ".so", NULL);
00154 } else if (strncmp (module_name, "lib", 3) == 0)
00155 return g_strdup (module_name);
00156 else
00157 return g_strconcat ("lib", module_name, ".so", NULL);
00158 }