gdb-dlfcn.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Platform independent shared object routines for GDB.
  2. Copyright (C) 2011-2022 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 "common-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. gdb_dlhandle_up
  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 (const gdb_dlhandle_up &handle, const char *symbol)
  32. {
  33. gdb_assert_not_reached ("gdb_dlsym should not be called on this platform.");
  34. }
  35. void
  36. dlclose_deleter::operator() (void *handle) const
  37. {
  38. gdb_assert_not_reached ("gdb_dlclose should not be called on this platform.");
  39. }
  40. int
  41. is_dl_available (void)
  42. {
  43. return 0;
  44. }
  45. #else /* NO_SHARED_LIB */
  46. gdb_dlhandle_up
  47. gdb_dlopen (const char *filename)
  48. {
  49. void *result;
  50. #ifdef HAVE_DLFCN_H
  51. result = dlopen (filename, RTLD_NOW);
  52. #elif __MINGW32__
  53. result = (void *) LoadLibrary (filename);
  54. #endif
  55. if (result != NULL)
  56. return gdb_dlhandle_up (result);
  57. #ifdef HAVE_DLFCN_H
  58. error (_("Could not load %s: %s"), filename, dlerror());
  59. #else
  60. {
  61. LPVOID buffer;
  62. DWORD dw;
  63. dw = GetLastError();
  64. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  65. FORMAT_MESSAGE_IGNORE_INSERTS,
  66. NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  67. (LPTSTR) &buffer,
  68. 0, NULL);
  69. error (_("Could not load %s: %s"), filename, (char *) buffer);
  70. }
  71. #endif
  72. }
  73. void *
  74. gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol)
  75. {
  76. #ifdef HAVE_DLFCN_H
  77. return dlsym (handle.get (), symbol);
  78. #elif __MINGW32__
  79. return (void *) GetProcAddress ((HMODULE) handle.get (), symbol);
  80. #endif
  81. }
  82. void
  83. dlclose_deleter::operator() (void *handle) const
  84. {
  85. #ifdef HAVE_DLFCN_H
  86. dlclose (handle);
  87. #elif __MINGW32__
  88. FreeLibrary ((HMODULE) handle);
  89. #endif
  90. }
  91. int
  92. is_dl_available (void)
  93. {
  94. return 1;
  95. }
  96. #endif /* NO_SHARED_LIB */