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