On terminal frames ignore minibuffer frame parameter (Bug#24758)
[emacs.git] / src / dynlib.c
blobada58373801d157fe31056cfb2507b423b488e81
1 /* Portable API for dynamic loading.
3 Copyright 2015-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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 (at
10 your option) any later version.
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.
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/>. */
21 /* Assume modules are enabled on modern systems... *Yes*, the
22 preprocessor macro checks could be more precise. I don't care.
24 If you think the abstraction is too leaky use libltdl (libtool),
25 don't reinvent the wheel by fixing this one. */
27 #include <config.h>
29 #include "dynlib.h"
31 #ifdef WINDOWSNT
33 /* MS-Windows systems. */
35 #include <errno.h>
36 #include "lisp.h"
37 #include "w32common.h" /* for os_subtype */
38 #include "w32.h"
40 static BOOL g_b_init_get_module_handle_ex;
41 static DWORD dynlib_last_err;
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
53 /* This needs to be called at startup to countermand any non-zero
54 values recorded by temacs. */
55 void dynlib_reset_last_error (void);
56 void
57 dynlib_reset_last_error (void)
59 g_b_init_get_module_handle_ex = 0;
60 dynlib_last_err = 0;
63 dynlib_handle_ptr
64 dynlib_open (const char *dll_fname)
66 HMODULE hdll;
67 char dll_fname_local[MAX_UTF8_PATH];
69 if (!dll_fname)
71 errno = ENOTSUP;
72 return NULL;
75 if (!dll_fname)
76 hdll = GetModuleHandle (NULL);
77 else
79 /* LoadLibrary wants backslashes. */
80 strcpy (dll_fname_local, dll_fname);
81 unixtodos_filename (dll_fname_local);
83 if (w32_unicode_filenames)
85 wchar_t dll_fname_w[MAX_PATH];
87 filename_to_utf16 (dll_fname_local, dll_fname_w);
88 hdll = LoadLibraryW (dll_fname_w);
90 else
92 char dll_fname_a[MAX_PATH];
94 filename_to_ansi (dll_fname_local, dll_fname_a);
95 hdll = LoadLibraryA (dll_fname_a);
99 if (!hdll)
100 dynlib_last_err = GetLastError ();
102 return (dynlib_handle_ptr) hdll;
105 void *
106 dynlib_sym (dynlib_handle_ptr h, const char *sym)
108 FARPROC sym_addr = NULL;
110 if (!h || h == INVALID_HANDLE_VALUE || !sym)
112 dynlib_last_err = ERROR_INVALID_PARAMETER;
113 return NULL;
116 sym_addr = GetProcAddress ((HMODULE) h, sym);
117 if (!sym_addr)
118 dynlib_last_err = GetLastError ();
120 return (void *)sym_addr;
123 bool
124 dynlib_addr (void *addr, const char **fname, const char **symname)
126 static char dll_filename[MAX_UTF8_PATH];
127 static char addr_str[22];
128 static GetModuleHandleExA_Proc s_pfn_Get_Module_HandleExA = NULL;
129 char *dll_fn = NULL;
130 HMODULE hm_kernel32 = NULL;
131 bool result = false;
132 HMODULE hm_dll = NULL;
133 wchar_t mfn_w[MAX_PATH];
134 char mfn_a[MAX_PATH];
136 /* Step 1: Find the handle of the module where ADDR lives. */
137 if (os_subtype == OS_9X
138 /* Windows NT family version before XP (v5.1). */
139 || ((w32_major_version + (w32_minor_version > 0)) < 6))
141 MEMORY_BASIC_INFORMATION mbi;
143 /* According to Matt Pietrek, the module handle is just the base
144 address where it's loaded in memory. */
145 if (VirtualQuery (addr, &mbi, sizeof(mbi)))
146 hm_dll = (HMODULE)mbi.AllocationBase;
148 else
150 /* Use the documented API when available (XP and later). */
151 if (g_b_init_get_module_handle_ex == 0)
153 g_b_init_get_module_handle_ex = 1;
154 hm_kernel32 = LoadLibrary ("kernel32.dll");
155 /* We load the ANSI version of the function because the
156 address we pass to it is not an address of a string, but
157 an address of a function. So we don't care about the
158 Unicode version. */
159 s_pfn_Get_Module_HandleExA =
160 (GetModuleHandleExA_Proc) GetProcAddress (hm_kernel32,
161 "GetModuleHandleExA");
163 if (s_pfn_Get_Module_HandleExA)
165 DWORD flags = (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
166 /* We don't want to call FreeLibrary at the
167 end, because then we'd need to remember
168 whether we obtained the handle by this
169 method or the above one. */
170 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);
172 if (!s_pfn_Get_Module_HandleExA (flags, addr, &hm_dll))
174 dynlib_last_err = GetLastError ();
175 hm_dll = NULL;
180 /* Step 2: Find the absolute file name of the module corresponding
181 to the hm_dll handle. */
182 if (hm_dll)
184 DWORD retval;
186 if (w32_unicode_filenames)
188 retval = GetModuleFileNameW (hm_dll, mfn_w, MAX_PATH);
189 if (retval > 0 && retval < MAX_PATH
190 && filename_from_utf16 (mfn_w, dll_filename) == 0)
191 dll_fn = dll_filename;
192 else if (retval == MAX_PATH)
193 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
194 else
195 dynlib_last_err = GetLastError ();
197 else
199 retval = GetModuleFileNameA (hm_dll, mfn_a, MAX_PATH);
200 if (retval > 0 && retval < MAX_PATH
201 && filename_from_ansi (mfn_a, dll_filename) == 0)
202 dll_fn = dll_filename;
203 else if (retval == MAX_PATH)
204 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
205 else
206 dynlib_last_err = GetLastError ();
208 if (dll_fn)
210 dostounix_filename (dll_fn);
211 /* We cannot easily produce the function name, since
212 typically all of the module functions will be unexported,
213 and probably even static, which means the symbols can be
214 obtained only if we link against libbfd (and the DLL can
215 be stripped anyway). So we just show the address and the
216 file name; they can use that with addr2line or GDB to
217 recover the symbolic name. */
218 sprintf (addr_str, "at 0x%x", (DWORD_PTR)addr);
219 *symname = addr_str;
220 result = true;
224 *fname = dll_fn;
225 return result;
228 const char *
229 dynlib_error (void)
231 char *error_string = NULL;
233 if (dynlib_last_err)
235 error_string = w32_strerror (dynlib_last_err);
236 dynlib_last_err = 0;
239 return error_string;
243 dynlib_close (dynlib_handle_ptr h)
245 if (!h || h == INVALID_HANDLE_VALUE)
247 dynlib_last_err = ERROR_INVALID_PARAMETER;
248 return -1;
250 /* If the handle is for the main module (the .exe file), it
251 shouldn't be passed to FreeLibrary, because GetModuleHandle
252 doesn't increment the refcount, but FreeLibrary does decrement
253 it. I don't think this should matter for the main module, but
254 just in case, we avoid the call here, relying on another call to
255 GetModuleHandle to return the same value. */
256 if (h == GetModuleHandle (NULL))
257 return 0;
259 if (!FreeLibrary ((HMODULE) h))
261 dynlib_last_err = GetLastError ();
262 return -1;
265 return 0;
268 #elif defined HAVE_UNISTD_H
270 /* POSIX systems. */
272 #include <dlfcn.h>
274 dynlib_handle_ptr
275 dynlib_open (const char *path)
277 return dlopen (path, RTLD_LAZY);
280 void *
281 dynlib_sym (dynlib_handle_ptr h, const char *sym)
283 return dlsym (h, sym);
286 bool
287 dynlib_addr (void *ptr, const char **path, const char **sym)
289 #ifdef HAVE_DLADDR
290 Dl_info info;
291 if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
293 *path = info.dli_fname;
294 *sym = info.dli_sname;
295 return true;
297 #endif
298 return false;
301 const char *
302 dynlib_error (void)
304 return dlerror ();
307 /* FIXME: Currently there is no way to unload a module, so this
308 function is never used. */
309 #if false
311 dynlib_close (dynlib_handle_ptr h)
313 return dlclose (h) == 0;
315 #endif
317 #else
319 #error "No dynamic loading for this system"
321 #endif
323 #if !HAVE_DLFUNC
324 # define dlfunc dynlib_sym
325 #endif
327 dynlib_function_ptr
328 dynlib_func (dynlib_handle_ptr h, const char *sym)
330 return (dynlib_function_ptr) dlfunc (h, sym);