xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb-dlfcn.cc (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Platform independent shared object routines for GDB.
2 
3    Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "gdb-dlfcn.h"
21 
22 #ifdef HAVE_DLFCN_H
23 #include <dlfcn.h>
24 #elif __MINGW32__
25 #include <windows.h>
26 #else
27 /* Unsupported configuration. */
28 #define NO_SHARED_LIB
29 #endif
30 
31 #ifdef NO_SHARED_LIB
32 
33 gdb_dlhandle_up
34 gdb_dlopen (const char *filename)
35 {
36   gdb_assert_not_reached ("gdb_dlopen should not be called on this platform.");
37 }
38 
39 void *
40 gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol)
41 {
42   gdb_assert_not_reached ("gdb_dlsym should not be called on this platform.");
43 }
44 
45 void
46 dlclose_deleter::operator() (void *handle) const
47 {
48   gdb_assert_not_reached ("gdb_dlclose should not be called on this platform.");
49 }
50 
51 int
52 is_dl_available (void)
53 {
54   return 0;
55 }
56 
57 #else /* NO_SHARED_LIB */
58 
59 gdb_dlhandle_up
60 gdb_dlopen (const char *filename)
61 {
62   void *result;
63 #ifdef HAVE_DLFCN_H
64   result = dlopen (filename, RTLD_NOW);
65 #elif __MINGW32__
66   result = (void *) LoadLibrary (filename);
67 #endif
68   if (result != NULL)
69     return gdb_dlhandle_up (result);
70 
71 #ifdef HAVE_DLFCN_H
72   error (_("Could not load %s: %s"), filename, dlerror());
73 #else
74   {
75     LPVOID buffer;
76     DWORD dw;
77 
78     dw = GetLastError();
79 
80     FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
81 		   FORMAT_MESSAGE_IGNORE_INSERTS,
82 		   NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
83 		   (LPTSTR) &buffer,
84 		   0, NULL);
85 
86     error (_("Could not load %s: %s"), filename, (char *) buffer);
87   }
88 #endif
89 }
90 
91 void *
92 gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol)
93 {
94 #ifdef HAVE_DLFCN_H
95   return dlsym (handle.get (), symbol);
96 #elif __MINGW32__
97   return (void *) GetProcAddress ((HMODULE) handle.get (), symbol);
98 #endif
99 }
100 
101 void
102 dlclose_deleter::operator() (void *handle) const
103 {
104 #ifdef HAVE_DLFCN_H
105   dlclose (handle);
106 #elif __MINGW32__
107   FreeLibrary ((HMODULE) handle);
108 #endif
109 }
110 
111 int
112 is_dl_available (void)
113 {
114   return 1;
115 }
116 
117 #endif /* NO_SHARED_LIB */
118