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