]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdb-dlfcn.c
1a260d49b42877d76ec949be96fec6fe14a80fc1
[thirdparty/binutils-gdb.git] / gdb / gdb-dlfcn.c
1 /* Platform independent shared object routines for GDB.
2
3 Copyright (C) 2011-2013 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 "defs.h"
21 #include "gdb_assert.h"
22
23 #include "gdb-dlfcn.h"
24
25 #ifdef HAVE_DLFCN_H
26 #include <dlfcn.h>
27 #elif __MINGW32__
28 #include <windows.h>
29 #else
30 /* Unsupported configuration. */
31 #define NO_SHARED_LIB
32 #endif
33
34 #ifdef NO_SHARED_LIB
35
36 void *
37 gdb_dlopen (const char *filename)
38 {
39 gdb_assert_not_reached ("gdb_dlopen should not be called on this platform.");
40 }
41
42 void *
43 gdb_dlsym (void *handle, const char *symbol)
44 {
45 gdb_assert_not_reached ("gdb_dlsym should not be called on this platform.");
46 }
47
48 struct cleanup *
49 make_cleanup_dlclose (void *handle)
50 {
51 gdb_assert_not_reached ("make_cleanup_dlclose should not be called on this "
52 "platform.");
53 }
54
55 int
56 gdb_dlclose (void *handle)
57 {
58 gdb_assert_not_reached ("gdb_dlclose should not be called on this platform.");
59 }
60
61 int
62 is_dl_available (void)
63 {
64 return 0;
65 }
66
67 #else /* NO_SHARED_LIB */
68
69 void *
70 gdb_dlopen (const char *filename)
71 {
72 void *result;
73 #ifdef HAVE_DLFCN_H
74 result = dlopen (filename, RTLD_NOW);
75 #elif __MINGW32__
76 result = (void *) LoadLibrary (filename);
77 #endif
78 if (result != NULL)
79 return result;
80
81 #ifdef HAVE_DLFCN_H
82 error (_("Could not load %s: %s"), filename, dlerror());
83 #else
84 {
85 LPVOID buffer;
86 DWORD dw;
87
88 dw = GetLastError();
89
90 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
91 FORMAT_MESSAGE_IGNORE_INSERTS,
92 NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
93 (LPTSTR) &buffer,
94 0, NULL);
95
96 error (_("Could not load %s: %s"), filename, (char *) buffer);
97 }
98 #endif
99 }
100
101 void *
102 gdb_dlsym (void *handle, const char *symbol)
103 {
104 #ifdef HAVE_DLFCN_H
105 return dlsym (handle, symbol);
106 #elif __MINGW32__
107 return (void *) GetProcAddress (handle, symbol);
108 #endif
109 }
110
111 int
112 gdb_dlclose (void *handle)
113 {
114 #ifdef HAVE_DLFCN_H
115 return dlclose (handle);
116 #elif __MINGW32__
117 return !((int) FreeLibrary (handle));
118 #endif
119 }
120
121 static void
122 do_dlclose_cleanup (void *handle)
123 {
124 gdb_dlclose (handle);
125 }
126
127 struct cleanup *
128 make_cleanup_dlclose (void *handle)
129 {
130 return make_cleanup (do_dlclose_cleanup, handle);
131 }
132
133 int
134 is_dl_available (void)
135 {
136 return 1;
137 }
138
139 #endif /* NO_SHARED_LIB */