xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/gdb-dlfcn.cc (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Platform independent shared object routines for GDB.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2011-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #include "common-defs.h"
217d62b00eSchristos #include "gdb-dlfcn.h"
227d62b00eSchristos 
237d62b00eSchristos #ifdef HAVE_DLFCN_H
247d62b00eSchristos #include <dlfcn.h>
257d62b00eSchristos #elif __MINGW32__
267d62b00eSchristos #include <windows.h>
277d62b00eSchristos #else
287d62b00eSchristos /* Unsupported configuration. */
297d62b00eSchristos #define NO_SHARED_LIB
307d62b00eSchristos #endif
317d62b00eSchristos 
327d62b00eSchristos #ifdef NO_SHARED_LIB
337d62b00eSchristos 
347d62b00eSchristos gdb_dlhandle_up
357d62b00eSchristos gdb_dlopen (const char *filename)
367d62b00eSchristos {
377d62b00eSchristos   gdb_assert_not_reached ("gdb_dlopen should not be called on this platform.");
387d62b00eSchristos }
397d62b00eSchristos 
407d62b00eSchristos void *
417d62b00eSchristos gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol)
427d62b00eSchristos {
437d62b00eSchristos   gdb_assert_not_reached ("gdb_dlsym should not be called on this platform.");
447d62b00eSchristos }
457d62b00eSchristos 
467d62b00eSchristos void
477d62b00eSchristos dlclose_deleter::operator() (void *handle) const
487d62b00eSchristos {
497d62b00eSchristos   gdb_assert_not_reached ("gdb_dlclose should not be called on this platform.");
507d62b00eSchristos }
517d62b00eSchristos 
527d62b00eSchristos int
537d62b00eSchristos is_dl_available (void)
547d62b00eSchristos {
557d62b00eSchristos   return 0;
567d62b00eSchristos }
577d62b00eSchristos 
587d62b00eSchristos #else /* NO_SHARED_LIB */
597d62b00eSchristos 
607d62b00eSchristos gdb_dlhandle_up
617d62b00eSchristos gdb_dlopen (const char *filename)
627d62b00eSchristos {
637d62b00eSchristos   void *result;
647d62b00eSchristos #ifdef HAVE_DLFCN_H
657d62b00eSchristos   result = dlopen (filename, RTLD_NOW);
667d62b00eSchristos #elif __MINGW32__
677d62b00eSchristos   result = (void *) LoadLibrary (filename);
687d62b00eSchristos #endif
697d62b00eSchristos   if (result != NULL)
707d62b00eSchristos     return gdb_dlhandle_up (result);
717d62b00eSchristos 
727d62b00eSchristos #ifdef HAVE_DLFCN_H
737d62b00eSchristos   error (_("Could not load %s: %s"), filename, dlerror());
747d62b00eSchristos #else
757d62b00eSchristos   {
767d62b00eSchristos     LPVOID buffer;
777d62b00eSchristos     DWORD dw;
787d62b00eSchristos 
797d62b00eSchristos     dw = GetLastError();
807d62b00eSchristos 
817d62b00eSchristos     FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
827d62b00eSchristos 		   FORMAT_MESSAGE_IGNORE_INSERTS,
837d62b00eSchristos 		   NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
847d62b00eSchristos 		   (LPTSTR) &buffer,
857d62b00eSchristos 		   0, NULL);
867d62b00eSchristos 
877d62b00eSchristos     error (_("Could not load %s: %s"), filename, (char *) buffer);
887d62b00eSchristos   }
897d62b00eSchristos #endif
907d62b00eSchristos }
917d62b00eSchristos 
927d62b00eSchristos void *
937d62b00eSchristos gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol)
947d62b00eSchristos {
957d62b00eSchristos #ifdef HAVE_DLFCN_H
967d62b00eSchristos   return dlsym (handle.get (), symbol);
977d62b00eSchristos #elif __MINGW32__
987d62b00eSchristos   return (void *) GetProcAddress ((HMODULE) handle.get (), symbol);
997d62b00eSchristos #endif
1007d62b00eSchristos }
1017d62b00eSchristos 
1027d62b00eSchristos void
1037d62b00eSchristos dlclose_deleter::operator() (void *handle) const
1047d62b00eSchristos {
1057d62b00eSchristos #ifdef HAVE_DLFCN_H
1067d62b00eSchristos   dlclose (handle);
1077d62b00eSchristos #elif __MINGW32__
1087d62b00eSchristos   FreeLibrary ((HMODULE) handle);
1097d62b00eSchristos #endif
1107d62b00eSchristos }
1117d62b00eSchristos 
1127d62b00eSchristos int
1137d62b00eSchristos is_dl_available (void)
1147d62b00eSchristos {
1157d62b00eSchristos   return 1;
1167d62b00eSchristos }
1177d62b00eSchristos 
1187d62b00eSchristos #endif /* NO_SHARED_LIB */
119