Simplify interface of dynlib_attr.
[emacs.git] / src / dynlib.c
blob79e98b0f288b1bc550d61db1465abd62d6b308d9
1 /* Portable API for dynamic loading.
3 Copyright 2015-2017 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 #include <stddef.h>
33 #ifdef WINDOWSNT
35 /* MS-Windows systems. */
37 #include <errno.h>
38 #include "lisp.h"
39 #include "w32common.h" /* for os_subtype */
40 #include "w32.h"
42 static BOOL g_b_init_get_module_handle_ex;
43 static DWORD dynlib_last_err;
45 /* Some versions of w32api headers only expose the following when
46 _WIN32_WINNT is set to higher values that we use. */
47 typedef BOOL (WINAPI *GetModuleHandleExA_Proc) (DWORD,LPCSTR,HMODULE*);
48 #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
49 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
50 #endif
51 #ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
52 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
53 #endif
55 /* This needs to be called at startup to countermand any non-zero
56 values recorded by temacs. */
57 void dynlib_reset_last_error (void);
58 void
59 dynlib_reset_last_error (void)
61 g_b_init_get_module_handle_ex = 0;
62 dynlib_last_err = 0;
65 dynlib_handle_ptr
66 dynlib_open (const char *dll_fname)
68 HMODULE hdll;
69 char dll_fname_local[MAX_UTF8_PATH];
71 if (!dll_fname)
73 errno = ENOTSUP;
74 return NULL;
77 if (!dll_fname)
78 hdll = GetModuleHandle (NULL);
79 else
81 /* LoadLibrary wants backslashes. */
82 strcpy (dll_fname_local, dll_fname);
83 unixtodos_filename (dll_fname_local);
85 if (w32_unicode_filenames)
87 wchar_t dll_fname_w[MAX_PATH];
89 filename_to_utf16 (dll_fname_local, dll_fname_w);
90 hdll = LoadLibraryW (dll_fname_w);
92 else
94 char dll_fname_a[MAX_PATH];
96 filename_to_ansi (dll_fname_local, dll_fname_a);
97 hdll = LoadLibraryA (dll_fname_a);
101 if (!hdll)
102 dynlib_last_err = GetLastError ();
104 return (dynlib_handle_ptr) hdll;
107 void *
108 dynlib_sym (dynlib_handle_ptr h, const char *sym)
110 FARPROC sym_addr = NULL;
112 if (!h || h == INVALID_HANDLE_VALUE || !sym)
114 dynlib_last_err = ERROR_INVALID_PARAMETER;
115 return NULL;
118 sym_addr = GetProcAddress ((HMODULE) h, sym);
119 if (!sym_addr)
120 dynlib_last_err = GetLastError ();
122 return (void *)sym_addr;
125 void
126 dynlib_addr (void *addr, const char **fname, const char **symname)
128 static char dll_filename[MAX_UTF8_PATH];
129 static char addr_str[22];
130 static GetModuleHandleExA_Proc s_pfn_Get_Module_HandleExA = NULL;
131 char *dll_fn = NULL;
132 HMODULE hm_kernel32 = NULL;
133 HMODULE hm_dll = NULL;
134 wchar_t mfn_w[MAX_PATH];
135 char mfn_a[MAX_PATH];
137 /* Step 1: Find the handle of the module where ADDR lives. */
138 if (os_subtype == OS_9X
139 /* Windows NT family version before XP (v5.1). */
140 || ((w32_major_version + (w32_minor_version > 0)) < 6))
142 MEMORY_BASIC_INFORMATION mbi;
144 /* According to Matt Pietrek, the module handle is just the base
145 address where it's loaded in memory. */
146 if (VirtualQuery (addr, &mbi, sizeof(mbi)))
147 hm_dll = (HMODULE)mbi.AllocationBase;
149 else
151 /* Use the documented API when available (XP and later). */
152 if (g_b_init_get_module_handle_ex == 0)
154 g_b_init_get_module_handle_ex = 1;
155 hm_kernel32 = LoadLibrary ("kernel32.dll");
156 /* We load the ANSI version of the function because the
157 address we pass to it is not an address of a string, but
158 an address of a function. So we don't care about the
159 Unicode version. */
160 s_pfn_Get_Module_HandleExA =
161 (GetModuleHandleExA_Proc) GetProcAddress (hm_kernel32,
162 "GetModuleHandleExA");
164 if (s_pfn_Get_Module_HandleExA)
166 DWORD flags = (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
167 /* We don't want to call FreeLibrary at the
168 end, because then we'd need to remember
169 whether we obtained the handle by this
170 method or the above one. */
171 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);
173 if (!s_pfn_Get_Module_HandleExA (flags, addr, &hm_dll))
175 dynlib_last_err = GetLastError ();
176 hm_dll = NULL;
181 /* Step 2: Find the absolute file name of the module corresponding
182 to the hm_dll handle. */
183 if (hm_dll)
185 DWORD retval;
187 if (w32_unicode_filenames)
189 retval = GetModuleFileNameW (hm_dll, mfn_w, MAX_PATH);
190 if (retval > 0 && retval < MAX_PATH
191 && filename_from_utf16 (mfn_w, dll_filename) == 0)
192 dll_fn = dll_filename;
193 else if (retval == MAX_PATH)
194 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
195 else
196 dynlib_last_err = GetLastError ();
198 else
200 retval = GetModuleFileNameA (hm_dll, mfn_a, MAX_PATH);
201 if (retval > 0 && retval < MAX_PATH
202 && filename_from_ansi (mfn_a, dll_filename) == 0)
203 dll_fn = dll_filename;
204 else if (retval == MAX_PATH)
205 dynlib_last_err = ERROR_INSUFFICIENT_BUFFER;
206 else
207 dynlib_last_err = GetLastError ();
209 if (dll_fn)
210 dostounix_filename (dll_fn);
213 *fname = dll_fn;
215 /* We cannot easily produce the function name, since typically all
216 of the module functions will be unexported, and probably even
217 static, which means the symbols can be obtained only if we link
218 against libbfd (and the DLL can be stripped anyway). So we just
219 show the address and the file name; they can use that with
220 addr2line or GDB to recover the symbolic name. */
221 *symname = NULL;
224 const char *
225 dynlib_error (void)
227 char *error_string = NULL;
229 if (dynlib_last_err)
231 error_string = w32_strerror (dynlib_last_err);
232 dynlib_last_err = 0;
235 return error_string;
239 dynlib_close (dynlib_handle_ptr h)
241 if (!h || h == INVALID_HANDLE_VALUE)
243 dynlib_last_err = ERROR_INVALID_PARAMETER;
244 return -1;
246 /* If the handle is for the main module (the .exe file), it
247 shouldn't be passed to FreeLibrary, because GetModuleHandle
248 doesn't increment the refcount, but FreeLibrary does decrement
249 it. I don't think this should matter for the main module, but
250 just in case, we avoid the call here, relying on another call to
251 GetModuleHandle to return the same value. */
252 if (h == GetModuleHandle (NULL))
253 return 0;
255 if (!FreeLibrary ((HMODULE) h))
257 dynlib_last_err = GetLastError ();
258 return -1;
261 return 0;
264 #elif defined HAVE_UNISTD_H
266 /* POSIX systems. */
268 #include <dlfcn.h>
270 dynlib_handle_ptr
271 dynlib_open (const char *path)
273 return dlopen (path, RTLD_LAZY);
276 void *
277 dynlib_sym (dynlib_handle_ptr h, const char *sym)
279 return dlsym (h, sym);
282 void
283 dynlib_addr (void *ptr, const char **path, const char **sym)
285 *path = NULL;
286 *sym = NULL;
287 #ifdef HAVE_DLADDR
288 Dl_info info;
289 if (dladdr (ptr, &info) && info.dli_fname && info.dli_sname)
291 *path = info.dli_fname;
292 *sym = info.dli_sname;
294 #endif
297 const char *
298 dynlib_error (void)
300 return dlerror ();
303 /* FIXME: Currently there is no way to unload a module, so this
304 function is never used. */
305 #if false
307 dynlib_close (dynlib_handle_ptr h)
309 return dlclose (h) == 0;
311 #endif
313 #else
315 #error "No dynamic loading for this system"
317 #endif
319 #if !HAVE_DLFUNC
320 # define dlfunc dynlib_sym
321 #endif
323 dynlib_function_ptr
324 dynlib_func (dynlib_handle_ptr h, const char *sym)
326 return (dynlib_function_ptr) dlfunc (h, sym);