sl@0: /* GMODULE - GLIB wrapper code for dynamic module loading sl@0: * Copyright (C) 1998, 2000 Tim Janik sl@0: * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: /* sl@0: * MT safe sl@0: */ sl@0: #include "config.h" sl@0: sl@0: #include sl@0: sl@0: /* Perl includes and instead of on some systmes? */ sl@0: sl@0: sl@0: /* dlerror() is not implemented on all systems sl@0: */ sl@0: #ifndef G_MODULE_HAVE_DLERROR sl@0: # ifdef __NetBSD__ sl@0: # define dlerror() g_strerror (errno) sl@0: # else /* !__NetBSD__ */ sl@0: /* could we rely on errno's state here? */ sl@0: # define dlerror() "unknown dl-error" sl@0: # endif /* !__NetBSD__ */ sl@0: #endif /* G_MODULE_HAVE_DLERROR */ sl@0: sl@0: /* some flags are missing on some systems, so we provide sl@0: * harmless defaults. sl@0: * The Perl sources say, RTLD_LAZY needs to be defined as (1), sl@0: * at least for Solaris 1. sl@0: * sl@0: * Mandatory: sl@0: * RTLD_LAZY - resolve undefined symbols as code from the dynamic library sl@0: * is executed. sl@0: * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail sl@0: * if this cannot be done. sl@0: * Optionally: sl@0: * RTLD_GLOBAL - the external symbols defined in the library will be made sl@0: * available to subsequently loaded libraries. sl@0: */ sl@0: #ifndef RTLD_LAZY sl@0: #define RTLD_LAZY 1 sl@0: #endif /* RTLD_LAZY */ sl@0: #ifndef RTLD_NOW sl@0: #define RTLD_NOW 0 sl@0: #endif /* RTLD_NOW */ sl@0: /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */ sl@0: #ifdef G_MODULE_BROKEN_RTLD_GLOBAL sl@0: #undef RTLD_GLOBAL sl@0: #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */ sl@0: #ifndef RTLD_GLOBAL sl@0: #define RTLD_GLOBAL 0 sl@0: #endif /* RTLD_GLOBAL */ sl@0: sl@0: sl@0: /* --- functions --- */ sl@0: static gchar* sl@0: fetch_dlerror (gboolean replace_null) sl@0: { sl@0: gchar *msg = dlerror (); sl@0: sl@0: /* make sure we always return an error message != NULL, if sl@0: * expected to do so. */ sl@0: sl@0: if (!msg && replace_null) sl@0: return "unknown dl-error"; sl@0: sl@0: return msg; sl@0: } sl@0: sl@0: static gpointer sl@0: _g_module_open (const gchar *file_name, sl@0: gboolean bind_lazy, sl@0: gboolean bind_local) sl@0: { sl@0: gpointer handle; sl@0: sl@0: handle = dlopen (file_name, sl@0: (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW)); sl@0: if (!handle) sl@0: g_module_set_error (fetch_dlerror (TRUE)); sl@0: sl@0: return handle; sl@0: } sl@0: sl@0: static gpointer sl@0: _g_module_self (void) sl@0: { sl@0: gpointer handle; sl@0: sl@0: /* to query symbols from the program itself, special link options sl@0: * are required on some systems. sl@0: */ sl@0: sl@0: handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY); sl@0: if (!handle) sl@0: g_module_set_error (fetch_dlerror (TRUE)); sl@0: sl@0: return handle; sl@0: } sl@0: sl@0: static void sl@0: _g_module_close (gpointer handle, sl@0: gboolean is_unref) sl@0: { sl@0: /* are there any systems out there that have dlopen()/dlclose() sl@0: * without a reference count implementation? sl@0: */ sl@0: is_unref |= 1; sl@0: sl@0: if (is_unref) sl@0: { sl@0: if (dlclose (handle) != 0) sl@0: g_module_set_error (fetch_dlerror (TRUE)); sl@0: } sl@0: } sl@0: sl@0: static gpointer sl@0: _g_module_symbol (gpointer handle, sl@0: const gchar *symbol_name) sl@0: { sl@0: gpointer p; sl@0: gchar *msg; sl@0: sl@0: fetch_dlerror (FALSE); sl@0: p = dlsym (handle, symbol_name); sl@0: msg = fetch_dlerror (FALSE); sl@0: if (msg) sl@0: g_module_set_error (msg); sl@0: sl@0: return p; sl@0: } sl@0: sl@0: static gchar* sl@0: _g_module_build_path (const gchar *directory, sl@0: const gchar *module_name) sl@0: { sl@0: if (directory && *directory) { sl@0: #ifdef __SYMBIAN32__ sl@0: return g_strconcat (directory, "\\", module_name, "." G_MODULE_SUFFIX, NULL); sl@0: #else sl@0: if (strncmp (module_name, "lib", 3) == 0) sl@0: return g_strconcat (directory, "/", module_name, NULL); sl@0: else sl@0: return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL); sl@0: #endif /* __SYMBIAN32__ */ sl@0: } else if (strncmp (module_name, "lib", 3) == 0) sl@0: return g_strdup (module_name); sl@0: else sl@0: #ifdef __SYMBIAN32__ sl@0: return g_strconcat (module_name, "." G_MODULE_SUFFIX, NULL); sl@0: #else sl@0: return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL); sl@0: #endif /* __SYMBIAN32__ */ sl@0: }