gdb/gdb-dlfcn.c - gdb

Functions defined

Macros defined

Source code

  1. /* Platform independent shared object routines for GDB.

  2.    Copyright (C) 2011-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdb-dlfcn.h"

  16. #ifdef HAVE_DLFCN_H
  17. #include <dlfcn.h>
  18. #elif __MINGW32__
  19. #include <windows.h>
  20. #else
  21. /* Unsupported configuration. */
  22. #define NO_SHARED_LIB
  23. #endif

  24. #ifdef NO_SHARED_LIB

  25. void *
  26. gdb_dlopen (const char *filename)
  27. {
  28.   gdb_assert_not_reached ("gdb_dlopen should not be called on this platform.");
  29. }

  30. void *
  31. gdb_dlsym (void *handle, const char *symbol)
  32. {
  33.   gdb_assert_not_reached ("gdb_dlsym should not be called on this platform.");
  34. }

  35. struct cleanup *
  36. make_cleanup_dlclose (void *handle)
  37. {
  38.   gdb_assert_not_reached ("make_cleanup_dlclose should not be called on this "
  39.                           "platform.");
  40. }

  41. int
  42. gdb_dlclose (void *handle)
  43. {
  44.   gdb_assert_not_reached ("gdb_dlclose should not be called on this platform.");
  45. }

  46. int
  47. is_dl_available (void)
  48. {
  49.   return 0;
  50. }

  51. #else /* NO_SHARED_LIB */

  52. void *
  53. gdb_dlopen (const char *filename)
  54. {
  55.   void *result;
  56. #ifdef HAVE_DLFCN_H
  57.   result = dlopen (filename, RTLD_NOW);
  58. #elif __MINGW32__
  59.   result = (void *) LoadLibrary (filename);
  60. #endif
  61.   if (result != NULL)
  62.     return result;

  63. #ifdef HAVE_DLFCN_H
  64.   error (_("Could not load %s: %s"), filename, dlerror());
  65. #else
  66.   {
  67.     LPVOID buffer;
  68.     DWORD dw;

  69.     dw = GetLastError();

  70.     FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  71.                    FORMAT_MESSAGE_IGNORE_INSERTS,
  72.                    NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  73.                    (LPTSTR) &buffer,
  74.                    0, NULL);

  75.     error (_("Could not load %s: %s"), filename, (char *) buffer);
  76.   }
  77. #endif
  78. }

  79. void *
  80. gdb_dlsym (void *handle, const char *symbol)
  81. {
  82. #ifdef HAVE_DLFCN_H
  83.   return dlsym (handle, symbol);
  84. #elif __MINGW32__
  85.   return (void *) GetProcAddress (handle, symbol);
  86. #endif
  87. }

  88. int
  89. gdb_dlclose (void *handle)
  90. {
  91. #ifdef HAVE_DLFCN_H
  92.   return dlclose (handle);
  93. #elif __MINGW32__
  94.   return !((int) FreeLibrary (handle));
  95. #endif
  96. }

  97. static void
  98. do_dlclose_cleanup (void *handle)
  99. {
  100.   gdb_dlclose (handle);
  101. }

  102. struct cleanup *
  103. make_cleanup_dlclose (void *handle)
  104. {
  105.   return make_cleanup (do_dlclose_cleanup, handle);
  106. }

  107. int
  108. is_dl_available (void)
  109. {
  110.   return 1;
  111. }

  112. #endif /* NO_SHARED_LIB */