]> code.delx.au - gnu-emacs/blob - src/dynlib.c
190f183fa6122bba4c4cedbb852bb526199a865d
[gnu-emacs] / src / dynlib.c
1 /* Portable API for dynamic loading.
2
3 Copyright 2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs 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 GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Assume modules are enabled on modern systems... *Yes*, the
22 preprocessor macro checks could be more precise. I don't care.
23
24 If you think the abstraction is too leaky use libltdl (libtool),
25 don't reinvent the wheel by fixing this one. */
26
27 #include <config.h>
28
29 #include "dynlib.h"
30
31 #ifdef WINDOWSNT
32
33 /* MS-Windows systems. */
34
35 #include <errno.h>
36 #include "lisp.h"
37 #include "w32common.h" /* for os_subtype */
38 #include "w32.h"
39
40 static BOOL g_b_init_get_module_handle_ex;
41 static DWORD dynlib_last_err;
42
43 /* Some versions of w32api headers only expose the following when
44 _WIN32_WINNT is set to higher values that we use. */
45 typedef BOOL (WINAPI *GetModuleHandleExA_Proc) (DWORD,LPCSTR,HMODULE*);
46 #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
47 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
48 #endif
49 #ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
50 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
51 #endif
52
53 /* This needs to be called at startup to countermand any non-zero
54 values recorded by temacs. */
55 void
56 dynlib_reset_last_error (void)
57 {
58 g_b_init_get_module_handle_ex = 0;
59 dynlib_last_err = 0;
60 }
61
62 dynlib_handle_ptr
63 dynlib_open (const char *dll_fname)
64 {
65 HMODULE hdll;
66 char dll_fname_local[MAX_UTF8_PATH];
67
68 if (!dll_fname)
69 {
70 errno = ENOTSUP;
71 return NULL;
72 }
73
74 if (!dll_fname)
75 hdll = GetModuleHandle (NULL);
76 else
77 {
78 /* LoadLibrary wants backslashes. */
79 strcpy (dll_fname_local, dll_fname);
80 unixtodos_filename (dll_fname_local);
81
82 if (w32_unicode_filenames)
83 {
84 wchar_t dll_fname_w[MAX_PATH];
85
86 filename_to_utf16 (dll_fname_local, dll_fname_w);
87 hdll = LoadLibraryW (dll_fname_w);
88 }
89 else
90 {
91 char dll_fname_a[MAX_PATH];
92
93 filename_to_ansi (dll_fname_local, dll_fname_a);
94 hdll = LoadLibraryA (dll_fname_a);
95 }
96 }
97
98 if (!hdll)
99 dynlib_last_err = GetLastError ();
100
101 return (dynlib_handle_ptr) hdll;
102 }
103
104 void *
105 dynlib_sym (dynlib_handle_ptr h, const char *sym)
106 {
107 FARPROC sym_addr = NULL;
108
109 if (!h || h == INVALID_HANDLE_VALUE || !sym)
110 {
111 dynlib_last_err = ERROR_INVALID_PARAMETER;
112 return NULL;
113 }
114
115 sym_addr = GetProcAddress ((HMODULE) h, sym);
116 if (!sym_addr)
117 dynlib_last_err = GetLastError ();
118
119 return (void *)sym_addr;
120 }
121
122 bool
123 dynlib_addr (void *addr, const char **fname, const char **symname)
124 {
125 static char dll_filename[MAX_UTF8_PATH];
126 static char addr_str[22];
127 static GetModuleHandleExA_Proc s_pfn_Get_Module_HandleExA = NULL;
128 char *dll_fn = NULL;
129 HMODULE hm_kernel32 = NULL;
130 bool result = false;
131 HMODULE hm_dll = NULL;
132 wchar_t mfn_w[MAX_PATH];
133 char mfn_a[MAX_PATH];
134
135 /* Step 1: Find the handle of the module where ADDR lives. */
136 if (os_subtype == OS_9X
137 /* Windows NT family version before XP (v5.1). */
138 || ((w32_major_version + (w32_minor_version > 0)) < 6))
139 {
140 MEMORY_BASIC_INFORMATION mbi;
141
142 /* According to Matt Pietrek, the module handle is just the base
143 address where it's loaded in memory. */
144 if (VirtualQuery (addr, &mbi, sizeof(mbi)))
145 hm_dll = (HMODULE)mbi.AllocationBase;
146 }
147 else
148 {
149 /* Use the documented API when available (XP and later). */
150 if (g_b_init_get_module_handle_ex == 0)
151 {
152 g_b_init_get_module_handle_ex = 1;
153 hm_kernel32 = LoadLibrary ("kernel32.dll");
154 /* We load the ANSI version of the function because the
155 address we pass to it is not an address of a string, but
156 an address of a function. So we don't care about the
157 Unicode version. */
158 s_pfn_Get_Module_HandleExA =
159 (GetModuleHandleExA_Proc) GetProcAddress (hm_kernel32,
160 "GetModuleHandleExA");
161 }
162 if (s_pfn_Get_Module_HandleExA)
163 {
164 DWORD flags = (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
165 /* We don't want to call FreeLibrary at the
166 end, because then we'd need to remember
167 whether we obtained the handle by this
168 method or the above one. */
169 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);
170
171 if (!s_pfn_Get_Module_HandleExA (flags, addr, &hm_dll))
172 {
173 dynlib_last_err = GetLastError ();
174 hm_dll = NULL;
175 }
176 }
177 }
178
179 /* Step 2: Find the absolute file name of the module corresponding
180 to the hm_dll handle. */
181 if (hm_dll)
182 {
183 DWORD retval;
184
185 if (w32_unicode_filenames)
186 {
187 retval = GetModuleFileNameW (hm_dll, mfn_w, MAX_PATH);
188 if (retval > 0 && retval < MAX_PATH
189 && filename_from_utf16 (mfn_w, dll_filename) == 0)
190 dll_fn = dll_filename;
191 else if (retval == MAX_PATH)
192 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
193 else
194 dynlib_last_err = GetLastError ();
195 }
196 else
197 {
198 retval = GetModuleFileNameA (hm_dll, mfn_a, MAX_PATH);
199 if (retval > 0 && retval < MAX_PATH
200 && filename_from_ansi (mfn_a, dll_filename) == 0)
201 dll_fn = dll_filename;
202 else if (retval == MAX_PATH)
203 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
204 else
205 dynlib_last_err = GetLastError ();
206 }
207 if (dll_fn)
208 {
209 dostounix_filename (dll_fn);
210 /* We cannot easily produce the function name, since
211 typically all of the module functions will be unexported,
212 and probably even static, which means the symbols can be
213 obtained only if we link against libbfd (and the DLL can
214 be stripped anyway). So we just show the address and the
215 file name; they can use that with addr2line or GDB to
216 recover the symbolic name. */
217 sprintf (addr_str, "at 0x%x", (DWORD_PTR)addr);
218 *symname = addr_str;
219 result = true;
220 }
221 }
222
223 *fname = dll_fn;
224 return result;
225 }
226
227 const char *
228 dynlib_error (void)
229 {
230 char *error_string = NULL;
231
232 if (dynlib_last_err)
233 {
234 error_string = w32_strerror (dynlib_last_err);
235 dynlib_last_err = 0;
236 }
237
238 return error_string;
239 }
240
241 int
242 dynlib_close (dynlib_handle_ptr h)
243 {
244 if (!h || h == INVALID_HANDLE_VALUE)
245 {
246 dynlib_last_err = ERROR_INVALID_PARAMETER;
247 return -1;
248 }
249 /* If the handle is for the main module (the .exe file), it
250 shouldn't be passed to FreeLibrary, because GetModuleHandle
251 doesn't increment the refcount, but FreeLibrary does decrement
252 it. I don't think this should matter for the main module, but
253 just in case, we avoid the call here, relying on another call to
254 GetModuleHandle to return the same value. */
255 if (h == GetModuleHandle (NULL))
256 return 0;
257
258 if (!FreeLibrary ((HMODULE) h))
259 {
260 dynlib_last_err = GetLastError ();
261 return -1;
262 }
263
264 return 0;
265 }
266
267 #elif defined HAVE_UNISTD_H
268
269 /* POSIX systems. */
270
271 #include <dlfcn.h>
272
273 dynlib_handle_ptr
274 dynlib_open (const char *path)
275 {
276 return dlopen (path, RTLD_LAZY);
277 }
278
279 void *
280 dynlib_sym (dynlib_handle_ptr h, const char *sym)
281 {
282 return dlsym (h, sym);
283 }
284
285 bool
286 dynlib_addr (void *ptr, const char **path, const char **sym)
287 {
288 #ifdef HAVE_DLADDR
289 Dl_info info;
290 if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
291 {
292 *path = info.dli_fname;
293 *sym = info.dli_sname;
294 return true;
295 }
296 #endif
297 return false;
298 }
299
300 const char *
301 dynlib_error (void)
302 {
303 return dlerror ();
304 }
305
306 /* FIXME: Currently there is no way to unload a module, so this
307 function is never used. */
308 #if false
309 int
310 dynlib_close (dynlib_handle_ptr h)
311 {
312 return dlclose (h) == 0;
313 }
314 #endif
315
316 #else
317
318 #error "No dynamic loading for this system"
319
320 #endif
321
322 #if !HAVE_DLFUNC
323 # define dlfunc dynlib_sym
324 #endif
325
326 dynlib_function_ptr
327 dynlib_func (dynlib_handle_ptr h, const char *sym)
328 {
329 return (dynlib_function_ptr) dlfunc (h, sym);
330 }