[PATCH 3/7] Platform agnostic dynamic loading code.
Sanjoy Das
sanjoy@playingwithpointers.com
Wed Aug 24 18:57:00 GMT 2011
gdb-dlfcn.h and gdb-dlfcn.c are added, which implement the (cross
platform) functions gdb_dlopen, gdb_dlsym and gdb_dlclose. They should
work correctly on POSIX and windows systems.
---
gdb/Makefile.in | 6 ++--
gdb/config.in | 3 ++
gdb/configure | 46 ++++++++++++++++++++++++++
gdb/configure.ac | 2 +
gdb/gdb-dlfcn.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
gdb/gdb-dlfcn.h | 46 ++++++++++++++++++++++++++
6 files changed, 195 insertions(+), 3 deletions(-)
create mode 100644 gdb/gdb-dlfcn.c
create mode 100644 gdb/gdb-dlfcn.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index c7276dd..dfddc48 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -739,7 +739,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
annotate.c common/signals.c copying.c dfp.c gdb.c inf-child.c \
regset.c sol-thread.c windows-termcap.c \
common/common-utils.c common/xml-utils.c \
- common/ptid.c common/buffer.c
+ common/ptid.c common/buffer.c gdb-dlfcn.c
LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
@@ -820,7 +820,7 @@ solib-darwin.h solib-ia64-hpux.h solib-spu.h windows-nat.h xcoffread.h \
gnulib/extra/arg-nonnull.h gnulib/extra/c++defs.h gnulib/extra/warn-on-use.h \
gnulib/stddef.in.h inline-frame.h \
common/common-utils.h common/xml-utils.h common/buffer.h common/ptid.h \
-common/linux-osdata.h
+common/linux-osdata.h gdb-dlfcn.h
# Header files that already have srcdir in them, or which are in objdir.
@@ -907,7 +907,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
target-descriptions.o target-memory.o xml-tdesc.o xml-builtin.o \
inferior.o osdata.o gdb_usleep.o record.o gcore.o \
jit.o progspace.o \
- common-utils.o buffer.o ptid.o
+ common-utils.o buffer.o ptid.o gdb-dlfcn.o
TSOBS = inflow.o
diff --git a/gdb/config.in b/gdb/config.in
index b1aef82..89c0eb4 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -988,3 +988,6 @@
/* Define if JIT_READER_DIR should be relocated when GDB is moved. */
#undef JIT_READER_DIR_RELOCATABLE
+
+/* Define if -ldl will work. */
+#undef HAVE_LIBDL
diff --git a/gdb/configure b/gdb/configure
index c6dfbf7..b7753bf 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -9934,6 +9934,52 @@ fi
ac_config_files="$ac_config_files jit-reader.h:jit-reader.in"
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlopen in -ldl" >&5
+$as_echo_n "checking for dlopen in -ldl... " >&6; }
+if test "${ac_cv_lib_dl_dlopen+set}" = set; then :
+ $as_echo_n "(cached) " >&6
+else
+ ac_check_lib_save_LIBS=$LIBS
+LIBS="-ldl $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+/* Override any GCC internal prototype to avoid an error.
+ Use char because int might match the return type of a GCC
+ builtin and then its argument prototype would still apply. */
+#ifdef __cplusplus
+extern "C"
+#endif
+char dlopen ();
+int
+main ()
+{
+return dlopen ();
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ ac_cv_lib_dl_dlopen=yes
+else
+ ac_cv_lib_dl_dlopen=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dl_dlopen" >&5
+$as_echo "$ac_cv_lib_dl_dlopen" >&6; }
+if test "x$ac_cv_lib_dl_dlopen" = x""yes; then :
+ cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBDL 1
+_ACEOF
+
+ LIBS="-ldl $LIBS"
+
+fi
+
+
# Check whether --with-jit-reader-dir was given.
diff --git a/gdb/configure.ac b/gdb/configure.ac
index ee73e9b..0ce5ac0 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -595,6 +595,8 @@ fi
AC_SUBST(TARGET_PTR)
AC_CONFIG_FILES([jit-reader.h:jit-reader.in])
+AC_CHECK_LIB([dl], [dlopen], [], [], [])
+
GDB_AC_WITH_DIR([JIT_READER_DIR], [jit-reader-dir],
[directory to load the JIT readers from],
[${libdir}/gdb])
diff --git a/gdb/gdb-dlfcn.c b/gdb/gdb-dlfcn.c
new file mode 100644
index 0000000..34c58d7
--- /dev/null
+++ b/gdb/gdb-dlfcn.c
@@ -0,0 +1,95 @@
+/* Platform independent shared object routines for GDB.
+
+ Copyright (C) 2011 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdb-dlfcn.h"
+
+#include "defs.h"
+
+#ifdef HAVE_LIBDL
+#include <dlfcn.h>
+#elif __MINGW32__
+#include <windows.h>
+#else
+/* Unsupported configuration. See Eg. gdb_dlopen for details. */
+#error API to load shared library missing (Eg. libdl)
+#endif
+
+void *
+gdb_dlopen (const char *filename)
+{
+ void *result;
+#ifdef HAVE_LIBDL
+ result = dlopen (filename, RTLD_NOW);
+#elif __MINGW32__
+ result = (void *) LoadLibrary (filename);
+#endif
+ if (result != NULL)
+ return result;
+
+#ifdef HAVE_LIBDL
+ error (_("Could not load %s: %s"), filename, dlerror());
+#else
+ {
+ LPVOID buffer;
+ DWORD dw;
+
+ dw = GetLastError();
+
+ FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR) &buffer
+ 0, NULL);
+
+ error (_("Could not load %s: %s"), filename, (char *) buffer);
+ }
+#endif
+}
+
+void *
+gdb_dlsym (void *handle, const char *symbol)
+{
+#ifdef HAVE_LIBDL
+ return dlsym (handle, symbol);
+#elif __MINGW32__
+ return (void *) GetProcAddress (handle, symbol);
+#endif
+}
+
+int
+gdb_dlclose (void *handle)
+{
+#ifdef HAVE_LIBDL
+ return dlclose (handle);
+#elif __MINGW32__
+ return !((int) FreeLibrary (handle));
+#endif
+}
+
+static void
+do_dlclose_cleanup (void *handle)
+{
+ gdb_dlclose (handle);
+}
+
+struct cleanup *
+make_cleanup_dlclose (void *handle)
+{
+ return make_cleanup (do_dlclose_cleanup, handle);
+}
diff --git a/gdb/gdb-dlfcn.h b/gdb/gdb-dlfcn.h
new file mode 100644
index 0000000..df6c05c
--- /dev/null
+++ b/gdb/gdb-dlfcn.h
@@ -0,0 +1,46 @@
+/* Platform independent shared object routines for GDB.
+
+ Copyright (C) 2011 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDB_DLFCN_H
+#define GDB_DLFCN_H
+
+#include "defs.h"
+
+/* Load the dynamic library file named FILENAME, and return a handle
+ for that dynamic library. Return NULL if the loading fails for any
+ reason. */
+
+void *gdb_dlopen (const char *filename);
+
+/* Return the address of the symbol named SYMBOL inside the shared
+ library whose handle is HANDLE. Return NULL when the symbol could
+ not be found. */
+
+void *gdb_dlsym (void *handle, const char *symbol);
+
+/* Install a cleanup routine which closes the handle HANDLE. */
+
+struct cleanup *make_cleanup_dlclose (void *handle);
+
+/* Cleanup the shared object pointed to by HANDLE. Return 0 on success
+ and nonzero on failure. */
+
+int gdb_dlclose (void *handle);
+
+#endif /* GDB_DLFCN_H */
--
1.7.5.4
More information about the Gdb-patches
mailing list