]>
code.delx.au - gnu-emacs/blob - src/dynlib.c
1 /* Portable API for dynamic loading.
3 Copyright 2015 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
10 (at 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. */
31 /* MS-Windows systems. */
36 dynlib_open (const char *path
)
39 return (dynlib_handle_ptr
) LoadLibrary (path
);
43 dynlib_sym (dynlib_handle_ptr h
, const char *sym
)
45 return GetProcAddress ((HMODULE
) h
, sym
);
49 dynlib_addr (void *ptr
, const char **path
, const char **sym
)
51 return false; /* not implemented */
57 /* TODO: use GetLastError(), FormatMessage(), ... */
58 return "Can't load DLL";
62 dynlib_close (dynlib_handle_ptr h
)
64 return FreeLibrary ((HMODULE
) h
) != 0;
67 #elif defined HAVE_UNISTD_H
74 dynlib_open (const char *path
)
76 return dlopen (path
, RTLD_LAZY
);
80 dynlib_sym (dynlib_handle_ptr h
, const char *sym
)
82 return dlsym (h
, sym
);
86 dynlib_addr (void *ptr
, const char **path
, const char **sym
)
90 if (dladdr (ptr
, &info
) && info
.dli_fname
&& info
.dli_sname
)
92 *path
= info
.dli_fname
;
93 *sym
= info
.dli_sname
;
107 dynlib_close (dynlib_handle_ptr h
)
109 return dlclose (h
) == 0;
114 #error "No dynamic loading for this system"